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