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#ifndef _SYS_DMU_IMPL_H
27#define	_SYS_DMU_IMPL_H
28
29#include <sys/txg_impl.h>
30#include <sys/zio.h>
31#include <sys/dnode.h>
32#include <sys/zfs_context.h>
33
34#ifdef	__cplusplus
35extern "C" {
36#endif
37
38/*
39 * This is the locking strategy for the DMU.  Numbers in parenthesis are
40 * cases that use that lock order, referenced below:
41 *
42 * ARC is self-contained
43 * bplist is self-contained
44 * refcount is self-contained
45 * txg is self-contained (hopefully!)
46 * zst_lock
47 * zf_rwlock
48 *
49 * XXX try to improve evicting path?
50 *
51 * dp_config_rwlock > os_obj_lock > dn_struct_rwlock >
52 * 	dn_dbufs_mtx > hash_mutexes > db_mtx > dd_lock > leafs
53 *
54 * dp_config_rwlock
55 *    must be held before: everything
56 *    protects dd namespace changes
57 *    protects property changes globally
58 *    held from:
59 *    	dsl_dir_open/r:
60 *    	dsl_dir_create_sync/w:
61 *    	dsl_dir_sync_destroy/w:
62 *    	dsl_dir_rename_sync/w:
63 *    	dsl_prop_changed_notify/r:
64 *
65 * os_obj_lock
66 *   must be held before:
67 *   	everything except dp_config_rwlock
68 *   protects os_obj_next
69 *   held from:
70 *   	dmu_object_alloc: dn_dbufs_mtx, db_mtx, hash_mutexes, dn_struct_rwlock
71 *
72 * dn_struct_rwlock
73 *   must be held before:
74 *   	everything except dp_config_rwlock and os_obj_lock
75 *   protects structure of dnode (eg. nlevels)
76 *   	db_blkptr can change when syncing out change to nlevels
77 *   	dn_maxblkid
78 *   	dn_nlevels
79 *   	dn_*blksz*
80 *   	phys nlevels, maxblkid, physical blkptr_t's (?)
81 *   held from:
82 *   	callers of dbuf_read_impl, dbuf_hold[_impl], dbuf_prefetch
83 *   	dmu_object_info_from_dnode: dn_dirty_mtx (dn_datablksz)
84 *   	dmu_tx_count_free:
85 *   	dbuf_read_impl: db_mtx, dmu_zfetch()
86 *   	dmu_zfetch: zf_rwlock/r, zst_lock, dbuf_prefetch()
87 *   	dbuf_new_size: db_mtx
88 *   	dbuf_dirty: db_mtx
89 *	dbuf_findbp: (callers, phys? - the real need)
90 *	dbuf_create: dn_dbufs_mtx, hash_mutexes, db_mtx (phys?)
91 *	dbuf_prefetch: dn_dirty_mtx, hash_mutexes, db_mtx, dn_dbufs_mtx
92 *	dbuf_hold_impl: hash_mutexes, db_mtx, dn_dbufs_mtx, dbuf_findbp()
93 *	dnode_sync/w (increase_indirection): db_mtx (phys)
94 *	dnode_set_blksz/w: dn_dbufs_mtx (dn_*blksz*)
95 *	dnode_new_blkid/w: (dn_maxblkid)
96 *	dnode_free_range/w: dn_dirty_mtx (dn_maxblkid)
97 *	dnode_next_offset: (phys)
98 *
99 * dn_dbufs_mtx
100 *    must be held before:
101 *    	db_mtx, hash_mutexes
102 *    protects:
103 *    	dn_dbufs
104 *    	dn_evicted
105 *    held from:
106 *    	dmu_evict_user: db_mtx (dn_dbufs)
107 *    	dbuf_free_range: db_mtx (dn_dbufs)
108 *    	dbuf_remove_ref: db_mtx, callees:
109 *    		dbuf_hash_remove: hash_mutexes, db_mtx
110 *    	dbuf_create: hash_mutexes, db_mtx (dn_dbufs)
111 *    	dnode_set_blksz: (dn_dbufs)
112 *
113 * hash_mutexes (global)
114 *   must be held before:
115 *   	db_mtx
116 *   protects dbuf_hash_table (global) and db_hash_next
117 *   held from:
118 *   	dbuf_find: db_mtx
119 *   	dbuf_hash_insert: db_mtx
120 *   	dbuf_hash_remove: db_mtx
121 *
122 * db_mtx (meta-leaf)
123 *   must be held before:
124 *   	dn_mtx, dn_dirty_mtx, dd_lock (leaf mutexes)
125 *   protects:
126 *   	db_state
127 * 	db_holds
128 * 	db_buf
129 * 	db_changed
130 * 	db_data_pending
131 * 	db_dirtied
132 * 	db_link
133 * 	db_dirty_node (??)
134 * 	db_dirtycnt
135 * 	db_d.*
136 * 	db.*
137 *   held from:
138 * 	dbuf_dirty: dn_mtx, dn_dirty_mtx
139 * 	dbuf_dirty->dsl_dir_willuse_space: dd_lock
140 * 	dbuf_dirty->dbuf_new_block->dsl_dataset_block_freeable: dd_lock
141 * 	dbuf_undirty: dn_dirty_mtx (db_d)
142 * 	dbuf_write_done: dn_dirty_mtx (db_state)
143 * 	dbuf_*
144 * 	dmu_buf_update_user: none (db_d)
145 * 	dmu_evict_user: none (db_d) (maybe can eliminate)
146 *   	dbuf_find: none (db_holds)
147 *   	dbuf_hash_insert: none (db_holds)
148 *   	dmu_buf_read_array_impl: none (db_state, db_changed)
149 *   	dmu_sync: none (db_dirty_node, db_d)
150 *   	dnode_reallocate: none (db)
151 *
152 * dn_mtx (leaf)
153 *   protects:
154 *   	dn_dirty_dbufs
155 *   	dn_ranges
156 *   	phys accounting
157 * 	dn_allocated_txg
158 * 	dn_free_txg
159 * 	dn_assigned_txg
160 * 	dd_assigned_tx
161 * 	dn_notxholds
162 * 	dn_dirtyctx
163 * 	dn_dirtyctx_firstset
164 * 	(dn_phys copy fields?)
165 * 	(dn_phys contents?)
166 *   held from:
167 *   	dnode_*
168 *   	dbuf_dirty: none
169 *   	dbuf_sync: none (phys accounting)
170 *   	dbuf_undirty: none (dn_ranges, dn_dirty_dbufs)
171 *   	dbuf_write_done: none (phys accounting)
172 *   	dmu_object_info_from_dnode: none (accounting)
173 *   	dmu_tx_commit: none
174 *   	dmu_tx_hold_object_impl: none
175 *   	dmu_tx_try_assign: dn_notxholds(cv)
176 *   	dmu_tx_unassign: none
177 *
178 * dd_lock
179 *    must be held before:
180 *      ds_lock
181 *      ancestors' dd_lock
182 *    protects:
183 *    	dd_prop_cbs
184 *    	dd_sync_*
185 *    	dd_used_bytes
186 *    	dd_tempreserved
187 *    	dd_space_towrite
188 *    	dd_myname
189 *    	dd_phys accounting?
190 *    held from:
191 *    	dsl_dir_*
192 *    	dsl_prop_changed_notify: none (dd_prop_cbs)
193 *    	dsl_prop_register: none (dd_prop_cbs)
194 *    	dsl_prop_unregister: none (dd_prop_cbs)
195 *    	dsl_dataset_block_freeable: none (dd_sync_*)
196 *
197 * os_lock (leaf)
198 *   protects:
199 *   	os_dirty_dnodes
200 *   	os_free_dnodes
201 *   	os_dnodes
202 *   	os_downgraded_dbufs
203 *   	dn_dirtyblksz
204 *   	dn_dirty_link
205 *   held from:
206 *   	dnode_create: none (os_dnodes)
207 *   	dnode_destroy: none (os_dnodes)
208 *   	dnode_setdirty: none (dn_dirtyblksz, os_*_dnodes)
209 *   	dnode_free: none (dn_dirtyblksz, os_*_dnodes)
210 *
211 * ds_lock
212 *    protects:
213 *    	ds_objset
214 *    	ds_open_refcount
215 *    	ds_snapname
216 *    	ds_phys accounting
217 *	ds_phys userrefs zapobj
218 *	ds_reserved
219 *    held from:
220 *    	dsl_dataset_*
221 *
222 * dr_mtx (leaf)
223 *    protects:
224 *	dr_children
225 *    held from:
226 *	dbuf_dirty
227 *	dbuf_undirty
228 *	dbuf_sync_indirect
229 *	dnode_new_blkid
230 */
231
232struct objset;
233struct dmu_pool;
234
235typedef struct dmu_xuio {
236	int next;
237	int cnt;
238	struct arc_buf **bufs;
239	iovec_t *iovp;
240} dmu_xuio_t;
241
242typedef struct xuio_stats {
243	/* loaned yet not returned arc_buf */
244	kstat_named_t xuiostat_onloan_rbuf;
245	kstat_named_t xuiostat_onloan_wbuf;
246	/* whether a copy is made when loaning out a read buffer */
247	kstat_named_t xuiostat_rbuf_copied;
248	kstat_named_t xuiostat_rbuf_nocopy;
249	/* whether a copy is made when assigning a write buffer */
250	kstat_named_t xuiostat_wbuf_copied;
251	kstat_named_t xuiostat_wbuf_nocopy;
252} xuio_stats_t;
253
254static xuio_stats_t xuio_stats = {
255	{ "onloan_read_buf",	KSTAT_DATA_UINT64 },
256	{ "onloan_write_buf",	KSTAT_DATA_UINT64 },
257	{ "read_buf_copied",	KSTAT_DATA_UINT64 },
258	{ "read_buf_nocopy",	KSTAT_DATA_UINT64 },
259	{ "write_buf_copied",	KSTAT_DATA_UINT64 },
260	{ "write_buf_nocopy",	KSTAT_DATA_UINT64 }
261};
262
263#define	XUIOSTAT_INCR(stat, val)	\
264	atomic_add_64(&xuio_stats.stat.value.ui64, (val))
265#define	XUIOSTAT_BUMP(stat)	XUIOSTAT_INCR(stat, 1)
266
267
268#ifdef	__cplusplus
269}
270#endif
271
272#endif	/* _SYS_DMU_IMPL_H */
273