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