xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dmu_impl.h (revision b390f3a9)
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 http://www.opensolaris.org/os/licensing.
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, 2015 by Delphix. All rights reserved.
28  * Copyright (c) 2018 DilOS
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 
40 #ifdef	__cplusplus
41 extern "C" {
42 #endif
43 
44 /*
45  * This is the locking strategy for the DMU.  Numbers in parenthesis are
46  * cases that use that lock order, referenced below:
47  *
48  * ARC is self-contained
49  * bplist is self-contained
50  * refcount is self-contained
51  * txg is self-contained (hopefully!)
52  * zst_lock
53  * zf_rwlock
54  *
55  * XXX try to improve evicting path?
56  *
57  * dp_config_rwlock > os_obj_lock > dn_struct_rwlock >
58  *	dn_dbufs_mtx > hash_mutexes > db_mtx > dd_lock > leafs
59  *
60  * dp_config_rwlock
61  *    must be held before: everything
62  *    protects dd namespace changes
63  *    protects property changes globally
64  *    held from:
65  *	dsl_dir_open/r:
66  *	dsl_dir_create_sync/w:
67  *	dsl_dir_sync_destroy/w:
68  *	dsl_dir_rename_sync/w:
69  *	dsl_prop_changed_notify/r:
70  *
71  * os_obj_lock
72  *   must be held before:
73  *	everything except dp_config_rwlock
74  *   protects os_obj_next
75  *   held from:
76  *	dmu_object_alloc: dn_dbufs_mtx, db_mtx, hash_mutexes, dn_struct_rwlock
77  *
78  * dn_struct_rwlock
79  *   must be held before:
80  *	everything except dp_config_rwlock and os_obj_lock
81  *   protects structure of dnode (eg. nlevels)
82  *	db_blkptr can change when syncing out change to nlevels
83  *	dn_maxblkid
84  *	dn_nlevels
85  *	dn_*blksz*
86  *	phys nlevels, maxblkid, physical blkptr_t's (?)
87  *   held from:
88  *	callers of dbuf_read_impl, dbuf_hold[_impl], dbuf_prefetch
89  *	dmu_object_info_from_dnode: dn_dirty_mtx (dn_datablksz)
90  *	dbuf_read_impl: db_mtx, dmu_zfetch()
91  *	dmu_zfetch: zf_rwlock/r, zst_lock, dbuf_prefetch()
92  *	dbuf_new_size: db_mtx
93  *	dbuf_dirty: db_mtx
94  *	dbuf_findbp: (callers, phys? - the real need)
95  *	dbuf_create: dn_dbufs_mtx, hash_mutexes, db_mtx (phys?)
96  *	dbuf_prefetch: dn_dirty_mtx, hash_mutexes, db_mtx, dn_dbufs_mtx
97  *	dbuf_hold_impl: hash_mutexes, db_mtx, dn_dbufs_mtx, dbuf_findbp()
98  *	dnode_sync/w (increase_indirection): db_mtx (phys)
99  *	dnode_set_blksz/w: dn_dbufs_mtx (dn_*blksz*)
100  *	dnode_new_blkid/w: (dn_maxblkid)
101  *	dnode_free_range/w: dn_dirty_mtx (dn_maxblkid)
102  *	dnode_next_offset: (phys)
103  *
104  * dn_dbufs_mtx
105  *    must be held before:
106  *	db_mtx, hash_mutexes
107  *    protects:
108  *	dn_dbufs
109  *	dn_evicted
110  *    held from:
111  *	dmu_evict_user: db_mtx (dn_dbufs)
112  *	dbuf_free_range: db_mtx (dn_dbufs)
113  *	dbuf_remove_ref: db_mtx, callees:
114  *		dbuf_hash_remove: hash_mutexes, db_mtx
115  *	dbuf_create: hash_mutexes, db_mtx (dn_dbufs)
116  *	dnode_set_blksz: (dn_dbufs)
117  *
118  * hash_mutexes (global)
119  *   must be held before:
120  *	db_mtx
121  *   protects dbuf_hash_table (global) and db_hash_next
122  *   held from:
123  *	dbuf_find: db_mtx
124  *	dbuf_hash_insert: db_mtx
125  *	dbuf_hash_remove: db_mtx
126  *
127  * db_mtx (meta-leaf)
128  *   must be held before:
129  *	dn_mtx, dn_dirty_mtx, dd_lock (leaf mutexes)
130  *   protects:
131  *	db_state
132  *	db_holds
133  *	db_buf
134  *	db_changed
135  *	db_data_pending
136  *	db_dirtied
137  *	db_link
138  *	db_dirty_node (??)
139  *	db_dirtycnt
140  *	db_d.*
141  *	db.*
142  *   held from:
143  *	dbuf_dirty: dn_mtx, dn_dirty_mtx
144  *	dbuf_dirty->dsl_dir_willuse_space: dd_lock
145  *	dbuf_dirty->dbuf_new_block->dsl_dataset_block_freeable: dd_lock
146  *	dbuf_undirty: dn_dirty_mtx (db_d)
147  *	dbuf_write_done: dn_dirty_mtx (db_state)
148  *	dbuf_*
149  *	dmu_buf_update_user: none (db_d)
150  *	dmu_evict_user: none (db_d) (maybe can eliminate)
151  *	dbuf_find: none (db_holds)
152  *	dbuf_hash_insert: none (db_holds)
153  *	dmu_buf_read_array_impl: none (db_state, db_changed)
154  *	dmu_sync: none (db_dirty_node, db_d)
155  *	dnode_reallocate: none (db)
156  *
157  * dn_mtx (leaf)
158  *   protects:
159  *	dn_dirty_dbufs
160  *	dn_ranges
161  *	phys accounting
162  *	dn_allocated_txg
163  *	dn_free_txg
164  *	dn_assigned_txg
165  *	dn_dirty_txg
166  *	dd_assigned_tx
167  *	dn_notxholds
168  *	dn_nodnholds
169  *	dn_dirtyctx
170  *	dn_dirtyctx_firstset
171  *	(dn_phys copy fields?)
172  *	(dn_phys contents?)
173  *   held from:
174  *	dnode_*
175  *	dbuf_dirty: none
176  *	dbuf_sync: none (phys accounting)
177  *	dbuf_undirty: none (dn_ranges, dn_dirty_dbufs)
178  *	dbuf_write_done: none (phys accounting)
179  *	dmu_object_info_from_dnode: none (accounting)
180  *	dmu_tx_commit: none
181  *	dmu_tx_hold_object_impl: none
182  *	dmu_tx_try_assign: dn_notxholds(cv)
183  *	dmu_tx_unassign: none
184  *
185  * dd_lock
186  *    must be held before:
187  *	ds_lock
188  *	ancestors' dd_lock
189  *    protects:
190  *	dd_prop_cbs
191  *	dd_sync_*
192  *	dd_used_bytes
193  *	dd_tempreserved
194  *	dd_space_towrite
195  *	dd_myname
196  *	dd_phys accounting?
197  *    held from:
198  *	dsl_dir_*
199  *	dsl_prop_changed_notify: none (dd_prop_cbs)
200  *	dsl_prop_register: none (dd_prop_cbs)
201  *	dsl_prop_unregister: none (dd_prop_cbs)
202  *
203  * os_lock (leaf)
204  *   protects:
205  *	os_dirty_dnodes
206  *	os_free_dnodes
207  *	os_dnodes
208  *	os_downgraded_dbufs
209  *	dn_dirtyblksz
210  *	dn_dirty_link
211  *   held from:
212  *	dnode_create: none (os_dnodes)
213  *	dnode_destroy: none (os_dnodes)
214  *	dnode_setdirty: none (dn_dirtyblksz, os_*_dnodes)
215  *	dnode_free: none (dn_dirtyblksz, os_*_dnodes)
216  *
217  * ds_lock
218  *    protects:
219  *	ds_objset
220  *	ds_open_refcount
221  *	ds_snapname
222  *	ds_phys accounting
223  *	ds_phys userrefs zapobj
224  *	ds_reserved
225  *    held from:
226  *	dsl_dataset_*
227  *
228  * dr_mtx (leaf)
229  *    protects:
230  *	dr_children
231  *    held from:
232  *	dbuf_dirty
233  *	dbuf_undirty
234  *	dbuf_sync_indirect
235  *	dnode_new_blkid
236  */
237 
238 struct objset;
239 struct dmu_pool;
240 
241 typedef struct dmu_xuio {
242 	int next;
243 	int cnt;
244 	struct arc_buf **bufs;
245 	iovec_t *iovp;
246 } dmu_xuio_t;
247 
248 typedef struct xuio_stats {
249 	/* loaned yet not returned arc_buf */
250 	kstat_named_t xuiostat_onloan_rbuf;
251 	kstat_named_t xuiostat_onloan_wbuf;
252 	/* whether a copy is made when loaning out a read buffer */
253 	kstat_named_t xuiostat_rbuf_copied;
254 	kstat_named_t xuiostat_rbuf_nocopy;
255 	/* whether a copy is made when assigning a write buffer */
256 	kstat_named_t xuiostat_wbuf_copied;
257 	kstat_named_t xuiostat_wbuf_nocopy;
258 } xuio_stats_t;
259 
260 /*
261  * The list of data whose inclusion in a send stream can be pending from
262  * one call to backup_cb to another.  Multiple calls to dump_free() and
263  * dump_freeobjects() can be aggregated into a single DRR_FREE or
264  * DRR_FREEOBJECTS replay record.
265  */
266 typedef enum {
267 	PENDING_NONE,
268 	PENDING_FREE,
269 	PENDING_FREEOBJECTS
270 } dmu_pendop_t;
271 
272 typedef struct dmu_sendarg {
273 	list_node_t dsa_link;
274 	dmu_replay_record_t *dsa_drr;
275 	vnode_t *dsa_vp;
276 	int dsa_outfd;
277 	struct proc *dsa_proc;
278 	offset_t *dsa_off;
279 	objset_t *dsa_os;
280 	zio_cksum_t dsa_zc;
281 	uint64_t dsa_toguid;
282 	uint64_t dsa_fromtxg;
283 	int dsa_err;
284 	dmu_pendop_t dsa_pending_op;
285 	uint64_t dsa_featureflags;
286 	uint64_t dsa_last_data_object;
287 	uint64_t dsa_last_data_offset;
288 	uint64_t dsa_resume_object;
289 	uint64_t dsa_resume_offset;
290 	boolean_t dsa_sent_begin;
291 	boolean_t dsa_sent_end;
292 } dmu_sendarg_t;
293 
294 void dmu_object_zapify(objset_t *, uint64_t, dmu_object_type_t, dmu_tx_t *);
295 void dmu_object_free_zapified(objset_t *, uint64_t, dmu_tx_t *);
296 int dmu_buf_hold_noread(objset_t *, uint64_t, uint64_t,
297     void *, dmu_buf_t **);
298 
299 #ifdef	__cplusplus
300 }
301 #endif
302 
303 #endif	/* _SYS_DMU_IMPL_H */
304