xref: /freebsd/sys/contrib/openzfs/include/sys/dmu_impl.h (revision 5e2fc2c3c59455ddd6354e765ca474182d90ba28)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28  * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
29  */
30 
31 #ifndef _SYS_DMU_IMPL_H
32 #define	_SYS_DMU_IMPL_H
33 
34 #include <sys/txg_impl.h>
35 #include <sys/zio.h>
36 #include <sys/dnode.h>
37 #include <sys/zfs_context.h>
38 #include <sys/zfs_ioctl.h>
39 #include <sys/uio.h>
40 #include <sys/abd.h>
41 #include <sys/arc.h>
42 #include <sys/dbuf.h>
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 /*
49  * This is the locking strategy for the DMU.  Numbers in parenthesis are
50  * cases that use that lock order, referenced below:
51  *
52  * ARC is self-contained
53  * bplist is self-contained
54  * refcount is self-contained
55  * txg is self-contained (hopefully!)
56  * zst_lock
57  * zf_rwlock
58  *
59  * XXX try to improve evicting path?
60  *
61  * dp_config_rwlock > os_obj_lock > dn_struct_rwlock >
62  * 	dn_dbufs_mtx > hash_mutexes > db_mtx > dd_lock > leafs
63  *
64  * dp_config_rwlock
65  *    must be held before: everything
66  *    protects dd namespace changes
67  *    protects property changes globally
68  *    held from:
69  *    	dsl_dir_open/r:
70  *    	dsl_dir_create_sync/w:
71  *    	dsl_dir_sync_destroy/w:
72  *    	dsl_dir_rename_sync/w:
73  *    	dsl_prop_changed_notify/r:
74  *
75  * os_obj_lock
76  *   must be held before:
77  *   	everything except dp_config_rwlock
78  *   protects os_obj_next
79  *   held from:
80  *   	dmu_object_alloc: dn_dbufs_mtx, db_mtx, hash_mutexes, dn_struct_rwlock
81  *
82  * dn_struct_rwlock
83  *   must be held before:
84  *   	everything except dp_config_rwlock and os_obj_lock
85  *   protects structure of dnode (eg. nlevels)
86  *   	db_blkptr can change when syncing out change to nlevels
87  *   	dn_maxblkid
88  *   	dn_nlevels
89  *   	dn_*blksz*
90  *   	phys nlevels, maxblkid, physical blkptr_t's (?)
91  *   held from:
92  *   	callers of dbuf_read_impl, dbuf_hold[_impl], dbuf_prefetch
93  *   	dmu_object_info_from_dnode: dn_dirty_mtx (dn_datablksz)
94  *   	dbuf_read_impl: db_mtx, dmu_zfetch()
95  *   	dmu_zfetch: zf_rwlock/r, zst_lock, dbuf_prefetch()
96  *   	dbuf_new_size: db_mtx
97  *   	dbuf_dirty: db_mtx
98  *	dbuf_findbp: (callers, phys? - the real need)
99  *	dbuf_create: dn_dbufs_mtx, hash_mutexes, db_mtx (phys?)
100  *	dbuf_prefetch: dn_dirty_mtx, hash_mutexes, db_mtx, dn_dbufs_mtx
101  *	dbuf_hold_impl: hash_mutexes, db_mtx, dn_dbufs_mtx, dbuf_findbp()
102  *	dnode_sync/w (increase_indirection): db_mtx (phys)
103  *	dnode_set_blksz/w: dn_dbufs_mtx (dn_*blksz*)
104  *	dnode_new_blkid/w: (dn_maxblkid)
105  *	dnode_free_range/w: dn_dirty_mtx (dn_maxblkid)
106  *	dnode_next_offset: (phys)
107  *
108  * dn_dbufs_mtx
109  *    must be held before:
110  *    	db_mtx, hash_mutexes
111  *    protects:
112  *    	dn_dbufs
113  *    	dn_evicted
114  *    held from:
115  *    	dmu_evict_user: db_mtx (dn_dbufs)
116  *    	dbuf_free_range: db_mtx (dn_dbufs)
117  *    	dbuf_remove_ref: db_mtx, callees:
118  *    		dbuf_hash_remove: hash_mutexes, db_mtx
119  *    	dbuf_create: hash_mutexes, db_mtx (dn_dbufs)
120  *    	dnode_set_blksz: (dn_dbufs)
121  *
122  * hash_mutexes (global)
123  *   must be held before:
124  *   	db_mtx
125  *   protects dbuf_hash_table (global) and db_hash_next
126  *   held from:
127  *   	dbuf_find: db_mtx
128  *   	dbuf_hash_insert: db_mtx
129  *   	dbuf_hash_remove: db_mtx
130  *
131  * db_mtx (meta-leaf)
132  *   must be held before:
133  *   	dn_mtx, dn_dirty_mtx, dd_lock (leaf mutexes)
134  *   protects:
135  *   	db_state
136  * 	db_holds
137  * 	db_buf
138  * 	db_changed
139  * 	db_data_pending
140  * 	db_dirtied
141  * 	db_link
142  * 	db_dirty_records
143  * 	db_dirtycnt
144  * 	db_d.*
145  * 	db.*
146  *   held from:
147  * 	dbuf_dirty: dn_mtx, dn_dirty_mtx
148  * 	dbuf_dirty->dsl_dir_willuse_space: dd_lock
149  * 	dbuf_dirty->dbuf_new_block->dsl_dataset_block_freeable: dd_lock
150  * 	dbuf_undirty: dn_dirty_mtx (db_d)
151  * 	dbuf_write_done: dn_dirty_mtx (db_state)
152  * 	dbuf_*
153  * 	dmu_buf_update_user: none (db_d)
154  * 	dmu_evict_user: none (db_d) (maybe can eliminate)
155  *   	dbuf_find: none (db_holds)
156  *   	dbuf_hash_insert: none (db_holds)
157  *   	dmu_buf_read_array_impl: none (db_state, db_changed)
158  *   	dmu_sync: none (db_dirty_records, db_d)
159  *   	dnode_reallocate: none (db)
160  *   	dmu_write_direct: none (db_dirty_records, db_d)
161  *   	dmu_write_direct_done: none (db_dirty_records, db_d)
162  *
163  * dn_mtx (leaf)
164  *   protects:
165  *   	dn_dirty_dbufs
166  *   	dn_ranges
167  *   	phys accounting
168  * 	dn_allocated_txg
169  * 	dn_free_txg
170  * 	dn_assigned_txg
171  * 	dn_dirtycnt
172  * 	dd_assigned_tx
173  * 	dn_notxholds
174  *	dn_nodnholds
175  * 	(dn_phys copy fields?)
176  * 	(dn_phys contents?)
177  *   held from:
178  *   	dnode_*
179  *   	dbuf_dirty: none
180  *   	dbuf_sync: none (phys accounting)
181  *   	dbuf_undirty: none (dn_ranges, dn_dirty_dbufs)
182  *   	dbuf_write_done: none (phys accounting)
183  *   	dmu_object_info_from_dnode: none (accounting)
184  *   	dmu_tx_commit: none
185  *   	dmu_tx_hold_object_impl: none
186  *   	dmu_tx_try_assign: dn_notxholds(cv)
187  *   	dmu_tx_unassign: none
188  *
189  * dd_lock
190  *    must be held before:
191  *      ds_lock
192  *      ancestors' dd_lock
193  *    protects:
194  *    	dd_prop_cbs
195  *    	dd_sync_*
196  *    	dd_used_bytes
197  *    	dd_tempreserved
198  *    	dd_space_towrite
199  *    	dd_myname
200  *    	dd_phys accounting?
201  *    held from:
202  *    	dsl_dir_*
203  *    	dsl_prop_changed_notify: none (dd_prop_cbs)
204  *    	dsl_prop_register: none (dd_prop_cbs)
205  *    	dsl_prop_unregister: none (dd_prop_cbs)
206  *
207  * os_lock (leaf)
208  *   protects:
209  *   	os_dirty_dnodes
210  *   	os_free_dnodes
211  *   	os_dnodes
212  *   	os_downgraded_dbufs
213  *   	dn_dirtyblksz
214  *   	dn_dirty_link
215  *   held from:
216  *   	dnode_create: none (os_dnodes)
217  *   	dnode_destroy: none (os_dnodes)
218  *   	dnode_setdirty: none (dn_dirtyblksz, os_*_dnodes)
219  *   	dnode_free: none (dn_dirtyblksz, os_*_dnodes)
220  *
221  * ds_lock
222  *    protects:
223  *    	ds_objset
224  *    	ds_open_refcount
225  *    	ds_snapname
226  *    	ds_phys accounting
227  *	ds_phys userrefs zapobj
228  *	ds_reserved
229  *    held from:
230  *    	dsl_dataset_*
231  *
232  * dr_mtx (leaf)
233  *    protects:
234  *	dr_children
235  *    held from:
236  *	dbuf_dirty
237  *	dbuf_undirty
238  *	dbuf_sync_indirect
239  *	dnode_new_blkid
240  */
241 
242 struct dmu_pool;
243 struct dmu_buf;
244 struct zgd;
245 
246 typedef struct dmu_sendstatus {
247 	list_node_t dss_link;
248 	int dss_outfd;
249 	proc_t *dss_proc;
250 	offset_t *dss_off;
251 	uint64_t dss_blocks; /* blocks visited during the sending process */
252 } dmu_sendstatus_t;
253 
254 /*
255  * dmu_sync_{ready/done} args
256  */
257 typedef struct {
258 	dbuf_dirty_record_t	*dsa_dr;
259 	void (*dsa_done)(struct zgd *, int);
260 	struct zgd		*dsa_zgd;
261 	dmu_tx_t		*dsa_tx;
262 } dmu_sync_arg_t;
263 
264 void dmu_sync_done(zio_t *, arc_buf_t *buf, void *varg);
265 void dmu_sync_ready(zio_t *, arc_buf_t *buf, void *varg);
266 
267 void dmu_object_zapify(objset_t *, uint64_t, dmu_object_type_t, dmu_tx_t *);
268 void dmu_object_free_zapified(objset_t *, uint64_t, dmu_tx_t *);
269 
270 int dmu_write_direct(zio_t *, dmu_buf_impl_t *, abd_t *, dmu_tx_t *);
271 int dmu_read_abd(dnode_t *, uint64_t, uint64_t, abd_t *, dmu_flags_t);
272 int dmu_write_abd(dnode_t *, uint64_t, uint64_t, abd_t *, dmu_flags_t,
273     dmu_tx_t *);
274 #if defined(_KERNEL)
275 int dmu_read_uio_direct(dnode_t *, zfs_uio_t *, uint64_t, dmu_flags_t);
276 int dmu_write_uio_direct(dnode_t *, zfs_uio_t *, uint64_t, dmu_flags_t,
277     dmu_tx_t *);
278 #endif
279 
280 #ifdef	__cplusplus
281 }
282 #endif
283 
284 #endif	/* _SYS_DMU_IMPL_H */
285