xref: /freebsd/sys/contrib/openzfs/include/sys/dmu_impl.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
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_dirty_txg
172  * 	dd_assigned_tx
173  * 	dn_notxholds
174  *	dn_nodnholds
175  * 	dn_dirtyctx
176  * 	dn_dirtyctx_firstset
177  * 	(dn_phys copy fields?)
178  * 	(dn_phys contents?)
179  *   held from:
180  *   	dnode_*
181  *   	dbuf_dirty: none
182  *   	dbuf_sync: none (phys accounting)
183  *   	dbuf_undirty: none (dn_ranges, dn_dirty_dbufs)
184  *   	dbuf_write_done: none (phys accounting)
185  *   	dmu_object_info_from_dnode: none (accounting)
186  *   	dmu_tx_commit: none
187  *   	dmu_tx_hold_object_impl: none
188  *   	dmu_tx_try_assign: dn_notxholds(cv)
189  *   	dmu_tx_unassign: none
190  *
191  * dd_lock
192  *    must be held before:
193  *      ds_lock
194  *      ancestors' dd_lock
195  *    protects:
196  *    	dd_prop_cbs
197  *    	dd_sync_*
198  *    	dd_used_bytes
199  *    	dd_tempreserved
200  *    	dd_space_towrite
201  *    	dd_myname
202  *    	dd_phys accounting?
203  *    held from:
204  *    	dsl_dir_*
205  *    	dsl_prop_changed_notify: none (dd_prop_cbs)
206  *    	dsl_prop_register: none (dd_prop_cbs)
207  *    	dsl_prop_unregister: none (dd_prop_cbs)
208  *
209  * os_lock (leaf)
210  *   protects:
211  *   	os_dirty_dnodes
212  *   	os_free_dnodes
213  *   	os_dnodes
214  *   	os_downgraded_dbufs
215  *   	dn_dirtyblksz
216  *   	dn_dirty_link
217  *   held from:
218  *   	dnode_create: none (os_dnodes)
219  *   	dnode_destroy: none (os_dnodes)
220  *   	dnode_setdirty: none (dn_dirtyblksz, os_*_dnodes)
221  *   	dnode_free: none (dn_dirtyblksz, os_*_dnodes)
222  *
223  * ds_lock
224  *    protects:
225  *    	ds_objset
226  *    	ds_open_refcount
227  *    	ds_snapname
228  *    	ds_phys accounting
229  *	ds_phys userrefs zapobj
230  *	ds_reserved
231  *    held from:
232  *    	dsl_dataset_*
233  *
234  * dr_mtx (leaf)
235  *    protects:
236  *	dr_children
237  *    held from:
238  *	dbuf_dirty
239  *	dbuf_undirty
240  *	dbuf_sync_indirect
241  *	dnode_new_blkid
242  */
243 
244 struct dmu_pool;
245 struct dmu_buf;
246 struct zgd;
247 
248 typedef struct dmu_sendstatus {
249 	list_node_t dss_link;
250 	int dss_outfd;
251 	proc_t *dss_proc;
252 	offset_t *dss_off;
253 	uint64_t dss_blocks; /* blocks visited during the sending process */
254 } dmu_sendstatus_t;
255 
256 /*
257  * dmu_sync_{ready/done} args
258  */
259 typedef struct {
260 	dbuf_dirty_record_t	*dsa_dr;
261 	void (*dsa_done)(struct zgd *, int);
262 	struct zgd		*dsa_zgd;
263 	dmu_tx_t		*dsa_tx;
264 } dmu_sync_arg_t;
265 
266 void dmu_sync_done(zio_t *, arc_buf_t *buf, void *varg);
267 void dmu_sync_ready(zio_t *, arc_buf_t *buf, void *varg);
268 
269 void dmu_object_zapify(objset_t *, uint64_t, dmu_object_type_t, dmu_tx_t *);
270 void dmu_object_free_zapified(objset_t *, uint64_t, dmu_tx_t *);
271 
272 int dmu_write_direct(zio_t *, dmu_buf_impl_t *, abd_t *, dmu_tx_t *);
273 int dmu_read_abd(dnode_t *, uint64_t, uint64_t, abd_t *, uint32_t flags);
274 int dmu_write_abd(dnode_t *, uint64_t, uint64_t, abd_t *, uint32_t, dmu_tx_t *);
275 #if defined(_KERNEL)
276 int dmu_read_uio_direct(dnode_t *, zfs_uio_t *, uint64_t);
277 int dmu_write_uio_direct(dnode_t *, zfs_uio_t *, uint64_t, dmu_tx_t *);
278 #endif
279 
280 #ifdef	__cplusplus
281 }
282 #endif
283 
284 #endif	/* _SYS_DMU_IMPL_H */
285