xref: /titanic_41/usr/src/uts/common/fs/zfs/sys/dnode.h (revision 1022fd2a9aa2c967697116c2ca51a238a3c550ac)
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  */
24 
25 #ifndef	_SYS_DNODE_H
26 #define	_SYS_DNODE_H
27 
28 #include <sys/zfs_context.h>
29 #include <sys/avl.h>
30 #include <sys/spa.h>
31 #include <sys/txg.h>
32 #include <sys/zio.h>
33 #include <sys/refcount.h>
34 #include <sys/dmu_zfetch.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * dnode_hold() flags.
42  */
43 #define	DNODE_MUST_BE_ALLOCATED	1
44 #define	DNODE_MUST_BE_FREE	2
45 
46 /*
47  * dnode_next_offset() flags.
48  */
49 #define	DNODE_FIND_HOLE		1
50 #define	DNODE_FIND_BACKWARDS	2
51 #define	DNODE_FIND_HAVELOCK	4
52 
53 /*
54  * Fixed constants.
55  */
56 #define	DNODE_SHIFT		9	/* 512 bytes */
57 #define	DN_MIN_INDBLKSHIFT	10	/* 1k */
58 #define	DN_MAX_INDBLKSHIFT	14	/* 16k */
59 #define	DNODE_BLOCK_SHIFT	14	/* 16k */
60 #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
61 #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
62 #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
63 
64 /*
65  * dnode id flags
66  *
67  * Note: a file will never ever have its
68  * ids moved from bonus->spill
69  * and only in a crypto environment would it be on spill
70  */
71 #define	DN_ID_CHKED_BONUS	0x1
72 #define	DN_ID_CHKED_SPILL	0x2
73 #define	DN_ID_OLD_EXIST		0x4
74 #define	DN_ID_NEW_EXIST		0x8
75 
76 /*
77  * Derived constants.
78  */
79 #define	DNODE_SIZE	(1 << DNODE_SHIFT)
80 #define	DN_MAX_NBLKPTR	((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
81 #define	DN_MAX_BONUSLEN	(DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT))
82 #define	DN_MAX_OBJECT	(1ULL << DN_MAX_OBJECT_SHIFT)
83 #define	DN_ZERO_BONUSLEN	(DN_MAX_BONUSLEN + 1)
84 #define	DN_KILL_SPILLBLK (1)
85 
86 #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
87 #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
88 #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
89 #define	DNODES_PER_LEVEL	(1ULL << DNODES_PER_LEVEL_SHIFT)
90 
91 /* The +2 here is a cheesy way to round up */
92 #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
93 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
94 
95 #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
96 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
97 
98 #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
99 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
100 
101 #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
102 
103 struct dmu_buf_impl;
104 struct objset;
105 struct zio;
106 
107 enum dnode_dirtycontext {
108 	DN_UNDIRTIED,
109 	DN_DIRTY_OPEN,
110 	DN_DIRTY_SYNC
111 };
112 
113 /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
114 #define	DNODE_FLAG_USED_BYTES		(1<<0)
115 #define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
116 
117 /* Does dnode have a SA spill blkptr in bonus? */
118 #define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
119 
120 typedef struct dnode_phys {
121 	uint8_t dn_type;		/* dmu_object_type_t */
122 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
123 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
124 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
125 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
126 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
127 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
128 	uint8_t dn_flags;		/* DNODE_FLAG_* */
129 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
130 	uint16_t dn_bonuslen;		/* length of dn_bonus */
131 	uint8_t dn_pad2[4];
132 
133 	/* accounting is protected by dn_dirty_mtx */
134 	uint64_t dn_maxblkid;		/* largest allocated block ID */
135 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
136 
137 	uint64_t dn_pad3[4];
138 
139 	blkptr_t dn_blkptr[1];
140 	uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)];
141 	blkptr_t dn_spill;
142 } dnode_phys_t;
143 
144 typedef struct dnode {
145 	/*
146 	 * dn_struct_rwlock protects the structure of the dnode,
147 	 * including the number of levels of indirection (dn_nlevels),
148 	 * dn_maxblkid, and dn_next_*
149 	 */
150 	krwlock_t dn_struct_rwlock;
151 
152 	/* Our link on dn_objset->os_dnodes list; protected by os_lock.  */
153 	list_node_t dn_link;
154 
155 	/* immutable: */
156 	struct objset *dn_objset;
157 	uint64_t dn_object;
158 	struct dmu_buf_impl *dn_dbuf;
159 	dnode_phys_t *dn_phys; /* pointer into dn->dn_dbuf->db.db_data */
160 
161 	/*
162 	 * Copies of stuff in dn_phys.  They're valid in the open
163 	 * context (eg. even before the dnode is first synced).
164 	 * Where necessary, these are protected by dn_struct_rwlock.
165 	 */
166 	dmu_object_type_t dn_type;	/* object type */
167 	uint16_t dn_bonuslen;		/* bonus length */
168 	uint8_t dn_bonustype;		/* bonus type */
169 	uint8_t dn_nblkptr;		/* number of blkptrs (immutable) */
170 	uint8_t dn_checksum;		/* ZIO_CHECKSUM type */
171 	uint8_t dn_compress;		/* ZIO_COMPRESS type */
172 	uint8_t dn_nlevels;
173 	uint8_t dn_indblkshift;
174 	uint8_t dn_datablkshift;	/* zero if blksz not power of 2! */
175 	uint16_t dn_datablkszsec;	/* in 512b sectors */
176 	uint32_t dn_datablksz;		/* in bytes */
177 	uint64_t dn_maxblkid;
178 	uint8_t dn_next_nblkptr[TXG_SIZE];
179 	uint8_t dn_next_nlevels[TXG_SIZE];
180 	uint8_t dn_next_indblkshift[TXG_SIZE];
181 	uint8_t dn_next_bonustype[TXG_SIZE];
182 	uint8_t dn_rm_spillblk[TXG_SIZE];	/* for removing spill blk */
183 	uint16_t dn_next_bonuslen[TXG_SIZE];
184 	uint32_t dn_next_blksz[TXG_SIZE];	/* next block size in bytes */
185 
186 	/* protected by os_lock: */
187 	list_node_t dn_dirty_link[TXG_SIZE];	/* next on dataset's dirty */
188 
189 	/* protected by dn_mtx: */
190 	kmutex_t dn_mtx;
191 	list_t dn_dirty_records[TXG_SIZE];
192 	avl_tree_t dn_ranges[TXG_SIZE];
193 	uint64_t dn_allocated_txg;
194 	uint64_t dn_free_txg;
195 	uint64_t dn_assigned_txg;
196 	kcondvar_t dn_notxholds;
197 	enum dnode_dirtycontext dn_dirtyctx;
198 	uint8_t *dn_dirtyctx_firstset;		/* dbg: contents meaningless */
199 
200 	/* protected by own devices */
201 	refcount_t dn_tx_holds;
202 	refcount_t dn_holds;
203 
204 	kmutex_t dn_dbufs_mtx;
205 	list_t dn_dbufs;		/* linked list of descendent dbuf_t's */
206 	struct dmu_buf_impl *dn_bonus;	/* bonus buffer dbuf */
207 	boolean_t dn_have_spill;	/* have spill or are spilling */
208 
209 	/* parent IO for current sync write */
210 	zio_t *dn_zio;
211 
212 	/* used in syncing context */
213 	uint64_t dn_oldused;	/* old phys used bytes */
214 	uint64_t dn_oldflags;	/* old phys dn_flags */
215 	uint64_t dn_olduid, dn_oldgid;
216 	uint64_t dn_newuid, dn_newgid;
217 	int dn_id_flags;
218 
219 	/* holds prefetch structure */
220 	struct zfetch	dn_zfetch;
221 } dnode_t;
222 
223 typedef struct free_range {
224 	avl_node_t fr_node;
225 	uint64_t fr_blkid;
226 	uint64_t fr_nblks;
227 } free_range_t;
228 
229 dnode_t *dnode_special_open(struct objset *dd, dnode_phys_t *dnp,
230     uint64_t object);
231 void dnode_special_close(dnode_t *dn);
232 
233 void dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx);
234 void dnode_setbonus_type(dnode_t *dn, dmu_object_type_t, dmu_tx_t *tx);
235 void dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx);
236 
237 int dnode_hold(struct objset *dd, uint64_t object,
238     void *ref, dnode_t **dnp);
239 int dnode_hold_impl(struct objset *dd, uint64_t object, int flag,
240     void *ref, dnode_t **dnp);
241 boolean_t dnode_add_ref(dnode_t *dn, void *ref);
242 void dnode_rele(dnode_t *dn, void *ref);
243 void dnode_setdirty(dnode_t *dn, dmu_tx_t *tx);
244 void dnode_sync(dnode_t *dn, dmu_tx_t *tx);
245 void dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
246     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
247 void dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
248     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
249 void dnode_free(dnode_t *dn, dmu_tx_t *tx);
250 void dnode_byteswap(dnode_phys_t *dnp);
251 void dnode_buf_byteswap(void *buf, size_t size);
252 void dnode_verify(dnode_t *dn);
253 int dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx);
254 uint64_t dnode_current_max_length(dnode_t *dn);
255 void dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx);
256 void dnode_clear_range(dnode_t *dn, uint64_t blkid,
257     uint64_t nblks, dmu_tx_t *tx);
258 void dnode_diduse_space(dnode_t *dn, int64_t space);
259 void dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx);
260 void dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t);
261 uint64_t dnode_block_freed(dnode_t *dn, uint64_t blkid);
262 void dnode_init(void);
263 void dnode_fini(void);
264 int dnode_next_offset(dnode_t *dn, int flags, uint64_t *off,
265     int minlvl, uint64_t blkfill, uint64_t txg);
266 void dnode_evict_dbufs(dnode_t *dn);
267 
268 #ifdef ZFS_DEBUG
269 
270 /*
271  * There should be a ## between the string literal and fmt, to make it
272  * clear that we're joining two strings together, but that piece of shit
273  * gcc doesn't support that preprocessor token.
274  */
275 #define	dprintf_dnode(dn, fmt, ...) do { \
276 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
277 	char __db_buf[32]; \
278 	uint64_t __db_obj = (dn)->dn_object; \
279 	if (__db_obj == DMU_META_DNODE_OBJECT) \
280 		(void) strcpy(__db_buf, "mdn"); \
281 	else \
282 		(void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
283 		    (u_longlong_t)__db_obj);\
284 	dprintf_ds((dn)->dn_objset->os_dsl_dataset, "obj=%s " fmt, \
285 	    __db_buf, __VA_ARGS__); \
286 	} \
287 _NOTE(CONSTCOND) } while (0)
288 
289 #define	DNODE_VERIFY(dn)		dnode_verify(dn)
290 #define	FREE_VERIFY(db, start, end, tx)	free_verify(db, start, end, tx)
291 
292 #else
293 
294 #define	dprintf_dnode(db, fmt, ...)
295 #define	DNODE_VERIFY(dn)
296 #define	FREE_VERIFY(db, start, end, tx)
297 
298 #endif
299 
300 #ifdef	__cplusplus
301 }
302 #endif
303 
304 #endif	/* _SYS_DNODE_H */
305