xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dnode.h (revision 653af1b809998570c7e89fe7a0d3f90992bf0216)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25  */
26 
27 #ifndef	_SYS_DNODE_H
28 #define	_SYS_DNODE_H
29 
30 #include <sys/zfs_context.h>
31 #include <sys/avl.h>
32 #include <sys/spa.h>
33 #include <sys/txg.h>
34 #include <sys/zio.h>
35 #include <sys/refcount.h>
36 #include <sys/dmu_zfetch.h>
37 #include <sys/zrlock.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * dnode_hold() flags.
45  */
46 #define	DNODE_MUST_BE_ALLOCATED	1
47 #define	DNODE_MUST_BE_FREE	2
48 
49 /*
50  * dnode_next_offset() flags.
51  */
52 #define	DNODE_FIND_HOLE		1
53 #define	DNODE_FIND_BACKWARDS	2
54 #define	DNODE_FIND_HAVELOCK	4
55 
56 /*
57  * Fixed constants.
58  */
59 #define	DNODE_SHIFT		9	/* 512 bytes */
60 #define	DN_MIN_INDBLKSHIFT	12	/* 4k */
61 #define	DN_MAX_INDBLKSHIFT	17	/* 128k */
62 #define	DNODE_BLOCK_SHIFT	14	/* 16k */
63 #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
64 #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
65 #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
66 
67 /*
68  * dnode id flags
69  *
70  * Note: a file will never ever have its
71  * ids moved from bonus->spill
72  * and only in a crypto environment would it be on spill
73  */
74 #define	DN_ID_CHKED_BONUS	0x1
75 #define	DN_ID_CHKED_SPILL	0x2
76 #define	DN_ID_OLD_EXIST		0x4
77 #define	DN_ID_NEW_EXIST		0x8
78 
79 /*
80  * Derived constants.
81  */
82 #define	DNODE_SIZE	(1 << DNODE_SHIFT)
83 #define	DN_MAX_NBLKPTR	((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
84 #define	DN_MAX_BONUSLEN	(DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT))
85 #define	DN_MAX_OBJECT	(1ULL << DN_MAX_OBJECT_SHIFT)
86 #define	DN_ZERO_BONUSLEN	(DN_MAX_BONUSLEN + 1)
87 #define	DN_KILL_SPILLBLK (1)
88 
89 #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
90 #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
91 
92 /*
93  * This is inaccurate if the indblkshift of the particular object is not the
94  * max.  But it's only used by userland to calculate the zvol reservation.
95  */
96 #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
97 #define	DNODES_PER_LEVEL	(1ULL << DNODES_PER_LEVEL_SHIFT)
98 
99 /* The +2 here is a cheesy way to round up */
100 #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
101 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
102 
103 #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
104 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
105 
106 #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
107 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
108 
109 #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
110 
111 struct dmu_buf_impl;
112 struct objset;
113 struct zio;
114 
115 enum dnode_dirtycontext {
116 	DN_UNDIRTIED,
117 	DN_DIRTY_OPEN,
118 	DN_DIRTY_SYNC
119 };
120 
121 /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
122 #define	DNODE_FLAG_USED_BYTES		(1<<0)
123 #define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
124 
125 /* Does dnode have a SA spill blkptr in bonus? */
126 #define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
127 
128 typedef struct dnode_phys {
129 	uint8_t dn_type;		/* dmu_object_type_t */
130 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
131 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
132 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
133 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
134 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
135 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
136 	uint8_t dn_flags;		/* DNODE_FLAG_* */
137 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
138 	uint16_t dn_bonuslen;		/* length of dn_bonus */
139 	uint8_t dn_pad2[4];
140 
141 	/* accounting is protected by dn_dirty_mtx */
142 	uint64_t dn_maxblkid;		/* largest allocated block ID */
143 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
144 
145 	uint64_t dn_pad3[4];
146 
147 	blkptr_t dn_blkptr[1];
148 	uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)];
149 	blkptr_t dn_spill;
150 } dnode_phys_t;
151 
152 struct dnode {
153 	/*
154 	 * Protects the structure of the dnode, including the number of levels
155 	 * of indirection (dn_nlevels), dn_maxblkid, and dn_next_*
156 	 */
157 	krwlock_t dn_struct_rwlock;
158 
159 	/* Our link on dn_objset->os_dnodes list; protected by os_lock.  */
160 	list_node_t dn_link;
161 
162 	/* immutable: */
163 	struct objset *dn_objset;
164 	uint64_t dn_object;
165 	struct dmu_buf_impl *dn_dbuf;
166 	struct dnode_handle *dn_handle;
167 	dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
168 
169 	/*
170 	 * Copies of stuff in dn_phys.  They're valid in the open
171 	 * context (eg. even before the dnode is first synced).
172 	 * Where necessary, these are protected by dn_struct_rwlock.
173 	 */
174 	dmu_object_type_t dn_type;	/* object type */
175 	uint16_t dn_bonuslen;		/* bonus length */
176 	uint8_t dn_bonustype;		/* bonus type */
177 	uint8_t dn_nblkptr;		/* number of blkptrs (immutable) */
178 	uint8_t dn_checksum;		/* ZIO_CHECKSUM type */
179 	uint8_t dn_compress;		/* ZIO_COMPRESS type */
180 	uint8_t dn_nlevels;
181 	uint8_t dn_indblkshift;
182 	uint8_t dn_datablkshift;	/* zero if blksz not power of 2! */
183 	uint8_t dn_moved;		/* Has this dnode been moved? */
184 	uint16_t dn_datablkszsec;	/* in 512b sectors */
185 	uint32_t dn_datablksz;		/* in bytes */
186 	uint64_t dn_maxblkid;
187 	uint8_t dn_next_type[TXG_SIZE];
188 	uint8_t dn_next_nblkptr[TXG_SIZE];
189 	uint8_t dn_next_nlevels[TXG_SIZE];
190 	uint8_t dn_next_indblkshift[TXG_SIZE];
191 	uint8_t dn_next_bonustype[TXG_SIZE];
192 	uint8_t dn_rm_spillblk[TXG_SIZE];	/* for removing spill blk */
193 	uint16_t dn_next_bonuslen[TXG_SIZE];
194 	uint32_t dn_next_blksz[TXG_SIZE];	/* next block size in bytes */
195 
196 	/* protected by dn_dbufs_mtx; declared here to fill 32-bit hole */
197 	uint32_t dn_dbufs_count;	/* count of dn_dbufs */
198 
199 	/* protected by os_lock: */
200 	list_node_t dn_dirty_link[TXG_SIZE];	/* next on dataset's dirty */
201 
202 	/* protected by dn_mtx: */
203 	kmutex_t dn_mtx;
204 	list_t dn_dirty_records[TXG_SIZE];
205 	struct range_tree *dn_free_ranges[TXG_SIZE];
206 	uint64_t dn_allocated_txg;
207 	uint64_t dn_free_txg;
208 	uint64_t dn_assigned_txg;
209 	kcondvar_t dn_notxholds;
210 	enum dnode_dirtycontext dn_dirtyctx;
211 	uint8_t *dn_dirtyctx_firstset;		/* dbg: contents meaningless */
212 
213 	/* protected by own devices */
214 	refcount_t dn_tx_holds;
215 	refcount_t dn_holds;
216 
217 	kmutex_t dn_dbufs_mtx;
218 	/*
219 	 * Descendent dbufs, ordered by dbuf_compare. Note that dn_dbufs
220 	 * can contain multiple dbufs of the same (level, blkid) when a
221 	 * dbuf is marked DB_EVICTING without being removed from
222 	 * dn_dbufs. To maintain the avl invariant that there cannot be
223 	 * duplicate entries, we order the dbufs by an arbitrary value -
224 	 * their address in memory. This means that dn_dbufs cannot be used to
225 	 * directly look up a dbuf. Instead, callers must use avl_walk, have
226 	 * a reference to the dbuf, or look up a non-existant node with
227 	 * db_state = DB_SEARCH (see dbuf_free_range for an example).
228 	 */
229 	avl_tree_t dn_dbufs;
230 
231 	/* protected by dn_struct_rwlock */
232 	struct dmu_buf_impl *dn_bonus;	/* bonus buffer dbuf */
233 
234 	boolean_t dn_have_spill;	/* have spill or are spilling */
235 
236 	/* parent IO for current sync write */
237 	zio_t *dn_zio;
238 
239 	/* used in syncing context */
240 	uint64_t dn_oldused;	/* old phys used bytes */
241 	uint64_t dn_oldflags;	/* old phys dn_flags */
242 	uint64_t dn_olduid, dn_oldgid;
243 	uint64_t dn_newuid, dn_newgid;
244 	int dn_id_flags;
245 
246 	/* holds prefetch structure */
247 	struct zfetch	dn_zfetch;
248 };
249 
250 /*
251  * Adds a level of indirection between the dbuf and the dnode to avoid
252  * iterating descendent dbufs in dnode_move(). Handles are not allocated
253  * individually, but as an array of child dnodes in dnode_hold_impl().
254  */
255 typedef struct dnode_handle {
256 	/* Protects dnh_dnode from modification by dnode_move(). */
257 	zrlock_t dnh_zrlock;
258 	dnode_t *dnh_dnode;
259 } dnode_handle_t;
260 
261 typedef struct dnode_children {
262 	dmu_buf_user_t dnc_dbu;		/* User evict data */
263 	size_t dnc_count;		/* number of children */
264 	dnode_handle_t dnc_children[];	/* sized dynamically */
265 } dnode_children_t;
266 
267 typedef struct free_range {
268 	avl_node_t fr_node;
269 	uint64_t fr_blkid;
270 	uint64_t fr_nblks;
271 } free_range_t;
272 
273 void dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
274     uint64_t object, dnode_handle_t *dnh);
275 void dnode_special_close(dnode_handle_t *dnh);
276 
277 void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
278 void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
279 void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
280 
281 int dnode_hold(struct objset *dd, uint64_t object,
282     void *ref, dnode_t **dnp);
283 int dnode_hold_impl(struct objset *dd, uint64_t object, int flag,
284     void *ref, dnode_t **dnp);
285 boolean_t dnode_add_ref(dnode_t *dn, void *ref);
286 void dnode_rele(dnode_t *dn, void *ref);
287 void dnode_rele_and_unlock(dnode_t *dn, void *tag);
288 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
289 void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
290 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
291     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
292 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
293     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
294 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
295 void dnode_byteswap(dnode_phys_t *dnp);
296 void dnode_buf_byteswap(void *buf, size_t size);
297 void dnode_verify(dnode_t *dn);
298 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
299 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
300 void dnode_diduse_space(dnode_t *dn, int64_t space);
301 void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx);
302 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t);
303 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
304 void dnode_init(void);
305 void dnode_fini(void);
306 int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
307     int minlvl, uint64_t blkfill, uint64_t txg);
308 void dnode_evict_dbufs(dnode_t *dn);
309 void dnode_evict_bonus(dnode_t *dn);
310 
311 #define	DNODE_IS_CACHEABLE(_dn)						\
312 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
313 	(DMU_OT_IS_METADATA((_dn)->dn_type) &&				\
314 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA))
315 
316 #define	DNODE_META_IS_CACHEABLE(_dn)					\
317 	((_dn)->dn_objset->os_primary_cache == ZFS_CACHE_ALL ||		\
318 	(_dn)->dn_objset->os_primary_cache == ZFS_CACHE_METADATA)
319 
320 #ifdef ZFS_DEBUG
321 
322 /*
323  * There should be a ## between the string literal and fmt, to make it
324  * clear that we're joining two strings together, but that piece of shit
325  * gcc doesn't support that preprocessor token.
326  */
327 #define	dprintf_dnode(dn, fmt, ...) do { \
328 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
329 	char __db_buf[32]; \
330 	uint64_t __db_obj = (dn)->dn_object; \
331 	if (__db_obj == DMU_META_DNODE_OBJECT) \
332 		(void) strcpy(__db_buf, "mdn"); \
333 	else \
334 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
335 		    (u_longlong_t)__db_obj);\
336 	dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
337 	    __db_buf, __VA_ARGS__); \
338 	} \
339 _NOTE(CONSTCOND) } while (0)
340 
341 #define	DNODE_VERIFY(dn)		dnode_verify(dn)
342 #define	FREE_VERIFY(db, start, end, tx)	free_verify(db, start, end, tx)
343 
344 #else
345 
346 #define	dprintf_dnode(db, fmt, ...)
347 #define	DNODE_VERIFY(dn)
348 #define	FREE_VERIFY(db, start, end, tx)
349 
350 #endif
351 
352 #ifdef	__cplusplus
353 }
354 #endif
355 
356 #endif	/* _SYS_DNODE_H */
357