Print this page
Possibility to physically reserve space without writing leaf blocks
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/dnode.c
+++ new/usr/src/uts/common/fs/zfs/dnode.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 24 */
25 25
26 26 #include <sys/zfs_context.h>
27 27 #include <sys/dbuf.h>
28 28 #include <sys/dnode.h>
29 29 #include <sys/dmu.h>
30 30 #include <sys/dmu_impl.h>
31 31 #include <sys/dmu_tx.h>
32 32 #include <sys/dmu_objset.h>
33 33 #include <sys/dsl_dir.h>
34 34 #include <sys/dsl_dataset.h>
35 35 #include <sys/spa.h>
36 36 #include <sys/zio.h>
37 37 #include <sys/dmu_zfetch.h>
38 38 #include <sys/range_tree.h>
39 39
40 40 static kmem_cache_t *dnode_cache;
41 41 /*
42 42 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
43 43 * turned on when DEBUG is also defined.
44 44 */
45 45 #ifdef DEBUG
46 46 #define DNODE_STATS
47 47 #endif /* DEBUG */
48 48
49 49 #ifdef DNODE_STATS
50 50 #define DNODE_STAT_ADD(stat) ((stat)++)
51 51 #else
52 52 #define DNODE_STAT_ADD(stat) /* nothing */
53 53 #endif /* DNODE_STATS */
54 54
55 55 static dnode_phys_t dnode_phys_zero;
56 56
57 57 int zfs_default_bs = SPA_MINBLOCKSHIFT;
58 58 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
59 59
60 60 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
61 61
62 62 static int
63 63 dbuf_compare(const void *x1, const void *x2)
64 64 {
65 65 const dmu_buf_impl_t *d1 = x1;
66 66 const dmu_buf_impl_t *d2 = x2;
67 67
68 68 if (d1->db_level < d2->db_level) {
69 69 return (-1);
70 70 }
71 71 if (d1->db_level > d2->db_level) {
72 72 return (1);
73 73 }
74 74
75 75 if (d1->db_blkid < d2->db_blkid) {
76 76 return (-1);
77 77 }
78 78 if (d1->db_blkid > d2->db_blkid) {
79 79 return (1);
80 80 }
81 81
82 82 if (d1->db_state < d2->db_state) {
83 83 return (-1);
84 84 }
85 85 if (d1->db_state > d2->db_state) {
86 86 return (1);
87 87 }
88 88
89 89 ASSERT3S(d1->db_state, !=, DB_SEARCH);
90 90 ASSERT3S(d2->db_state, !=, DB_SEARCH);
91 91
92 92 if ((uintptr_t)d1 < (uintptr_t)d2) {
93 93 return (-1);
94 94 }
95 95 if ((uintptr_t)d1 > (uintptr_t)d2) {
96 96 return (1);
97 97 }
98 98 return (0);
99 99 }
100 100
101 101 /* ARGSUSED */
102 102 static int
103 103 dnode_cons(void *arg, void *unused, int kmflag)
104 104 {
105 105 dnode_t *dn = arg;
106 106 int i;
107 107
108 108 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
109 109 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
110 110 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
111 111 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
112 112
113 113 /*
114 114 * Every dbuf has a reference, and dropping a tracked reference is
115 115 * O(number of references), so don't track dn_holds.
116 116 */
117 117 refcount_create_untracked(&dn->dn_holds);
118 118 refcount_create(&dn->dn_tx_holds);
119 119 list_link_init(&dn->dn_link);
120 120
121 121 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
122 122 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
123 123 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
124 124 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
125 125 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
126 126 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
127 127 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
128 128
129 129 for (i = 0; i < TXG_SIZE; i++) {
130 130 list_link_init(&dn->dn_dirty_link[i]);
131 131 dn->dn_free_ranges[i] = NULL;
132 132 list_create(&dn->dn_dirty_records[i],
133 133 sizeof (dbuf_dirty_record_t),
134 134 offsetof(dbuf_dirty_record_t, dr_dirty_node));
135 135 }
136 136
137 137 dn->dn_allocated_txg = 0;
138 138 dn->dn_free_txg = 0;
139 139 dn->dn_assigned_txg = 0;
140 140 dn->dn_dirtyctx = 0;
141 141 dn->dn_dirtyctx_firstset = NULL;
142 142 dn->dn_bonus = NULL;
143 143 dn->dn_have_spill = B_FALSE;
144 144 dn->dn_zio = NULL;
145 145 dn->dn_oldused = 0;
146 146 dn->dn_oldflags = 0;
147 147 dn->dn_olduid = 0;
148 148 dn->dn_oldgid = 0;
149 149 dn->dn_newuid = 0;
150 150 dn->dn_newgid = 0;
151 151 dn->dn_id_flags = 0;
152 152
153 153 dn->dn_dbufs_count = 0;
154 154 dn->dn_unlisted_l0_blkid = 0;
155 155 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
156 156 offsetof(dmu_buf_impl_t, db_link));
157 157
158 158 dn->dn_moved = 0;
159 159 return (0);
160 160 }
161 161
162 162 /* ARGSUSED */
163 163 static void
164 164 dnode_dest(void *arg, void *unused)
165 165 {
166 166 int i;
167 167 dnode_t *dn = arg;
168 168
169 169 rw_destroy(&dn->dn_struct_rwlock);
170 170 mutex_destroy(&dn->dn_mtx);
171 171 mutex_destroy(&dn->dn_dbufs_mtx);
172 172 cv_destroy(&dn->dn_notxholds);
173 173 refcount_destroy(&dn->dn_holds);
174 174 refcount_destroy(&dn->dn_tx_holds);
175 175 ASSERT(!list_link_active(&dn->dn_link));
176 176
177 177 for (i = 0; i < TXG_SIZE; i++) {
178 178 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
179 179 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
180 180 list_destroy(&dn->dn_dirty_records[i]);
181 181 ASSERT0(dn->dn_next_nblkptr[i]);
182 182 ASSERT0(dn->dn_next_nlevels[i]);
183 183 ASSERT0(dn->dn_next_indblkshift[i]);
184 184 ASSERT0(dn->dn_next_bonustype[i]);
185 185 ASSERT0(dn->dn_rm_spillblk[i]);
186 186 ASSERT0(dn->dn_next_bonuslen[i]);
187 187 ASSERT0(dn->dn_next_blksz[i]);
188 188 }
189 189
190 190 ASSERT0(dn->dn_allocated_txg);
191 191 ASSERT0(dn->dn_free_txg);
192 192 ASSERT0(dn->dn_assigned_txg);
193 193 ASSERT0(dn->dn_dirtyctx);
194 194 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
195 195 ASSERT3P(dn->dn_bonus, ==, NULL);
196 196 ASSERT(!dn->dn_have_spill);
197 197 ASSERT3P(dn->dn_zio, ==, NULL);
198 198 ASSERT0(dn->dn_oldused);
199 199 ASSERT0(dn->dn_oldflags);
200 200 ASSERT0(dn->dn_olduid);
201 201 ASSERT0(dn->dn_oldgid);
202 202 ASSERT0(dn->dn_newuid);
203 203 ASSERT0(dn->dn_newgid);
204 204 ASSERT0(dn->dn_id_flags);
205 205
206 206 ASSERT0(dn->dn_dbufs_count);
207 207 ASSERT0(dn->dn_unlisted_l0_blkid);
208 208 avl_destroy(&dn->dn_dbufs);
209 209 }
210 210
211 211 void
212 212 dnode_init(void)
213 213 {
214 214 ASSERT(dnode_cache == NULL);
215 215 dnode_cache = kmem_cache_create("dnode_t",
216 216 sizeof (dnode_t),
217 217 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
218 218 kmem_cache_set_move(dnode_cache, dnode_move);
219 219 }
220 220
221 221 void
222 222 dnode_fini(void)
223 223 {
224 224 kmem_cache_destroy(dnode_cache);
225 225 dnode_cache = NULL;
226 226 }
227 227
228 228
229 229 #ifdef ZFS_DEBUG
230 230 void
231 231 dnode_verify(dnode_t *dn)
232 232 {
233 233 int drop_struct_lock = FALSE;
234 234
235 235 ASSERT(dn->dn_phys);
236 236 ASSERT(dn->dn_objset);
237 237 ASSERT(dn->dn_handle->dnh_dnode == dn);
238 238
239 239 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
240 240
241 241 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
242 242 return;
243 243
244 244 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
245 245 rw_enter(&dn->dn_struct_rwlock, RW_READER);
246 246 drop_struct_lock = TRUE;
247 247 }
248 248 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
249 249 int i;
250 250 ASSERT3U(dn->dn_indblkshift, >=, 0);
251 251 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
252 252 if (dn->dn_datablkshift) {
253 253 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
254 254 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
255 255 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
256 256 }
257 257 ASSERT3U(dn->dn_nlevels, <=, 30);
258 258 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
259 259 ASSERT3U(dn->dn_nblkptr, >=, 1);
260 260 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
261 261 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
262 262 ASSERT3U(dn->dn_datablksz, ==,
263 263 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
264 264 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
265 265 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
266 266 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
267 267 for (i = 0; i < TXG_SIZE; i++) {
268 268 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
269 269 }
270 270 }
271 271 if (dn->dn_phys->dn_type != DMU_OT_NONE)
272 272 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
273 273 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
274 274 if (dn->dn_dbuf != NULL) {
275 275 ASSERT3P(dn->dn_phys, ==,
276 276 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
277 277 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
278 278 }
279 279 if (drop_struct_lock)
280 280 rw_exit(&dn->dn_struct_rwlock);
281 281 }
282 282 #endif
283 283
284 284 void
285 285 dnode_byteswap(dnode_phys_t *dnp)
286 286 {
287 287 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
288 288 int i;
289 289
290 290 if (dnp->dn_type == DMU_OT_NONE) {
291 291 bzero(dnp, sizeof (dnode_phys_t));
292 292 return;
293 293 }
294 294
295 295 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
296 296 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
297 297 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
298 298 dnp->dn_used = BSWAP_64(dnp->dn_used);
299 299
300 300 /*
301 301 * dn_nblkptr is only one byte, so it's OK to read it in either
302 302 * byte order. We can't read dn_bouslen.
303 303 */
304 304 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
305 305 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
306 306 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
307 307 buf64[i] = BSWAP_64(buf64[i]);
308 308
309 309 /*
310 310 * OK to check dn_bonuslen for zero, because it won't matter if
311 311 * we have the wrong byte order. This is necessary because the
312 312 * dnode dnode is smaller than a regular dnode.
313 313 */
314 314 if (dnp->dn_bonuslen != 0) {
315 315 /*
316 316 * Note that the bonus length calculated here may be
317 317 * longer than the actual bonus buffer. This is because
318 318 * we always put the bonus buffer after the last block
319 319 * pointer (instead of packing it against the end of the
320 320 * dnode buffer).
321 321 */
322 322 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
323 323 size_t len = DN_MAX_BONUSLEN - off;
324 324 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
325 325 dmu_object_byteswap_t byteswap =
326 326 DMU_OT_BYTESWAP(dnp->dn_bonustype);
327 327 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
328 328 }
329 329
330 330 /* Swap SPILL block if we have one */
331 331 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
332 332 byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
333 333
334 334 }
335 335
336 336 void
337 337 dnode_buf_byteswap(void *vbuf, size_t size)
338 338 {
339 339 dnode_phys_t *buf = vbuf;
340 340 int i;
341 341
342 342 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
343 343 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
344 344
345 345 size >>= DNODE_SHIFT;
346 346 for (i = 0; i < size; i++) {
347 347 dnode_byteswap(buf);
348 348 buf++;
349 349 }
350 350 }
351 351
352 352 void
353 353 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
354 354 {
355 355 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
356 356
357 357 dnode_setdirty(dn, tx);
358 358 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
359 359 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
360 360 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
361 361 dn->dn_bonuslen = newsize;
362 362 if (newsize == 0)
363 363 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
364 364 else
365 365 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
366 366 rw_exit(&dn->dn_struct_rwlock);
367 367 }
368 368
369 369 void
370 370 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
371 371 {
372 372 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
373 373 dnode_setdirty(dn, tx);
374 374 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
375 375 dn->dn_bonustype = newtype;
376 376 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
377 377 rw_exit(&dn->dn_struct_rwlock);
378 378 }
379 379
380 380 void
381 381 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
382 382 {
383 383 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
384 384 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
385 385 dnode_setdirty(dn, tx);
386 386 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
387 387 dn->dn_have_spill = B_FALSE;
388 388 }
389 389
390 390 static void
391 391 dnode_setdblksz(dnode_t *dn, int size)
392 392 {
393 393 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
394 394 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
395 395 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
396 396 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
397 397 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
398 398 dn->dn_datablksz = size;
399 399 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
400 400 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
401 401 }
402 402
403 403 static dnode_t *
404 404 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
405 405 uint64_t object, dnode_handle_t *dnh)
406 406 {
407 407 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
408 408
409 409 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
410 410 dn->dn_moved = 0;
411 411
412 412 /*
413 413 * Defer setting dn_objset until the dnode is ready to be a candidate
414 414 * for the dnode_move() callback.
415 415 */
416 416 dn->dn_object = object;
417 417 dn->dn_dbuf = db;
418 418 dn->dn_handle = dnh;
419 419 dn->dn_phys = dnp;
420 420
421 421 if (dnp->dn_datablkszsec) {
422 422 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
423 423 } else {
424 424 dn->dn_datablksz = 0;
425 425 dn->dn_datablkszsec = 0;
426 426 dn->dn_datablkshift = 0;
427 427 }
428 428 dn->dn_indblkshift = dnp->dn_indblkshift;
429 429 dn->dn_nlevels = dnp->dn_nlevels;
430 430 dn->dn_type = dnp->dn_type;
431 431 dn->dn_nblkptr = dnp->dn_nblkptr;
432 432 dn->dn_checksum = dnp->dn_checksum;
433 433 dn->dn_compress = dnp->dn_compress;
434 434 dn->dn_bonustype = dnp->dn_bonustype;
435 435 dn->dn_bonuslen = dnp->dn_bonuslen;
436 436 dn->dn_maxblkid = dnp->dn_maxblkid;
437 437 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
438 438 dn->dn_id_flags = 0;
439 439
440 440 dmu_zfetch_init(&dn->dn_zfetch, dn);
441 441
442 442 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
443 443
444 444 mutex_enter(&os->os_lock);
445 445 list_insert_head(&os->os_dnodes, dn);
446 446 membar_producer();
447 447 /*
448 448 * Everything else must be valid before assigning dn_objset makes the
449 449 * dnode eligible for dnode_move().
450 450 */
451 451 dn->dn_objset = os;
452 452 mutex_exit(&os->os_lock);
453 453
454 454 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
455 455 return (dn);
456 456 }
457 457
458 458 /*
459 459 * Caller must be holding the dnode handle, which is released upon return.
460 460 */
461 461 static void
462 462 dnode_destroy(dnode_t *dn)
463 463 {
464 464 objset_t *os = dn->dn_objset;
465 465
466 466 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
467 467
468 468 mutex_enter(&os->os_lock);
469 469 POINTER_INVALIDATE(&dn->dn_objset);
470 470 list_remove(&os->os_dnodes, dn);
471 471 mutex_exit(&os->os_lock);
472 472
473 473 /* the dnode can no longer move, so we can release the handle */
474 474 zrl_remove(&dn->dn_handle->dnh_zrlock);
475 475
476 476 dn->dn_allocated_txg = 0;
477 477 dn->dn_free_txg = 0;
478 478 dn->dn_assigned_txg = 0;
479 479
480 480 dn->dn_dirtyctx = 0;
481 481 if (dn->dn_dirtyctx_firstset != NULL) {
482 482 kmem_free(dn->dn_dirtyctx_firstset, 1);
483 483 dn->dn_dirtyctx_firstset = NULL;
484 484 }
485 485 if (dn->dn_bonus != NULL) {
486 486 mutex_enter(&dn->dn_bonus->db_mtx);
487 487 dbuf_evict(dn->dn_bonus);
488 488 dn->dn_bonus = NULL;
489 489 }
490 490 dn->dn_zio = NULL;
491 491
492 492 dn->dn_have_spill = B_FALSE;
493 493 dn->dn_oldused = 0;
494 494 dn->dn_oldflags = 0;
495 495 dn->dn_olduid = 0;
496 496 dn->dn_oldgid = 0;
497 497 dn->dn_newuid = 0;
498 498 dn->dn_newgid = 0;
499 499 dn->dn_id_flags = 0;
500 500 dn->dn_unlisted_l0_blkid = 0;
501 501
502 502 dmu_zfetch_rele(&dn->dn_zfetch);
503 503 kmem_cache_free(dnode_cache, dn);
504 504 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
505 505 }
506 506
507 507 void
508 508 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
509 509 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
510 510 {
511 511 int i;
512 512
513 513 if (blocksize == 0)
514 514 blocksize = 1 << zfs_default_bs;
515 515 else if (blocksize > SPA_MAXBLOCKSIZE)
516 516 blocksize = SPA_MAXBLOCKSIZE;
517 517 else
518 518 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
519 519
520 520 if (ibs == 0)
521 521 ibs = zfs_default_ibs;
522 522
523 523 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
524 524
525 525 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
526 526 dn->dn_object, tx->tx_txg, blocksize, ibs);
527 527
528 528 ASSERT(dn->dn_type == DMU_OT_NONE);
529 529 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
530 530 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
531 531 ASSERT(ot != DMU_OT_NONE);
532 532 ASSERT(DMU_OT_IS_VALID(ot));
533 533 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
534 534 (bonustype == DMU_OT_SA && bonuslen == 0) ||
535 535 (bonustype != DMU_OT_NONE && bonuslen != 0));
536 536 ASSERT(DMU_OT_IS_VALID(bonustype));
537 537 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
538 538 ASSERT(dn->dn_type == DMU_OT_NONE);
539 539 ASSERT0(dn->dn_maxblkid);
540 540 ASSERT0(dn->dn_allocated_txg);
541 541 ASSERT0(dn->dn_assigned_txg);
542 542 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
543 543 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
544 544 ASSERT(avl_is_empty(&dn->dn_dbufs));
545 545
546 546 for (i = 0; i < TXG_SIZE; i++) {
547 547 ASSERT0(dn->dn_next_nblkptr[i]);
548 548 ASSERT0(dn->dn_next_nlevels[i]);
549 549 ASSERT0(dn->dn_next_indblkshift[i]);
550 550 ASSERT0(dn->dn_next_bonuslen[i]);
551 551 ASSERT0(dn->dn_next_bonustype[i]);
552 552 ASSERT0(dn->dn_rm_spillblk[i]);
553 553 ASSERT0(dn->dn_next_blksz[i]);
554 554 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
555 555 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
556 556 ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
557 557 }
558 558
559 559 dn->dn_type = ot;
560 560 dnode_setdblksz(dn, blocksize);
561 561 dn->dn_indblkshift = ibs;
562 562 dn->dn_nlevels = 1;
563 563 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
564 564 dn->dn_nblkptr = 1;
565 565 else
566 566 dn->dn_nblkptr = 1 +
567 567 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
568 568 dn->dn_bonustype = bonustype;
569 569 dn->dn_bonuslen = bonuslen;
570 570 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
571 571 dn->dn_compress = ZIO_COMPRESS_INHERIT;
572 572 dn->dn_dirtyctx = 0;
573 573
574 574 dn->dn_free_txg = 0;
575 575 if (dn->dn_dirtyctx_firstset) {
576 576 kmem_free(dn->dn_dirtyctx_firstset, 1);
577 577 dn->dn_dirtyctx_firstset = NULL;
578 578 }
579 579
580 580 dn->dn_allocated_txg = tx->tx_txg;
581 581 dn->dn_id_flags = 0;
582 582
583 583 dnode_setdirty(dn, tx);
584 584 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
585 585 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
586 586 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
587 587 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
588 588 }
589 589
590 590 void
591 591 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
592 592 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
593 593 {
594 594 int nblkptr;
595 595
596 596 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
597 597 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
598 598 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
599 599 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
600 600 ASSERT(tx->tx_txg != 0);
601 601 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
602 602 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
603 603 (bonustype == DMU_OT_SA && bonuslen == 0));
604 604 ASSERT(DMU_OT_IS_VALID(bonustype));
605 605 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
606 606
607 607 /* clean up any unreferenced dbufs */
608 608 dnode_evict_dbufs(dn);
609 609
610 610 dn->dn_id_flags = 0;
611 611
612 612 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
613 613 dnode_setdirty(dn, tx);
614 614 if (dn->dn_datablksz != blocksize) {
615 615 /* change blocksize */
616 616 ASSERT(dn->dn_maxblkid == 0 &&
617 617 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
618 618 dnode_block_freed(dn, 0)));
619 619 dnode_setdblksz(dn, blocksize);
620 620 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
621 621 }
622 622 if (dn->dn_bonuslen != bonuslen)
623 623 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
624 624
625 625 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
626 626 nblkptr = 1;
627 627 else
628 628 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
629 629 if (dn->dn_bonustype != bonustype)
630 630 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
631 631 if (dn->dn_nblkptr != nblkptr)
632 632 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
633 633 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
634 634 dbuf_rm_spill(dn, tx);
635 635 dnode_rm_spill(dn, tx);
636 636 }
637 637 rw_exit(&dn->dn_struct_rwlock);
638 638
639 639 /* change type */
640 640 dn->dn_type = ot;
641 641
642 642 /* change bonus size and type */
643 643 mutex_enter(&dn->dn_mtx);
644 644 dn->dn_bonustype = bonustype;
645 645 dn->dn_bonuslen = bonuslen;
646 646 dn->dn_nblkptr = nblkptr;
647 647 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
648 648 dn->dn_compress = ZIO_COMPRESS_INHERIT;
649 649 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
650 650
651 651 /* fix up the bonus db_size */
652 652 if (dn->dn_bonus) {
653 653 dn->dn_bonus->db.db_size =
654 654 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
655 655 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
656 656 }
657 657
658 658 dn->dn_allocated_txg = tx->tx_txg;
659 659 mutex_exit(&dn->dn_mtx);
660 660 }
661 661
662 662 #ifdef DNODE_STATS
663 663 static struct {
664 664 uint64_t dms_dnode_invalid;
665 665 uint64_t dms_dnode_recheck1;
666 666 uint64_t dms_dnode_recheck2;
667 667 uint64_t dms_dnode_special;
668 668 uint64_t dms_dnode_handle;
669 669 uint64_t dms_dnode_rwlock;
670 670 uint64_t dms_dnode_active;
671 671 } dnode_move_stats;
672 672 #endif /* DNODE_STATS */
673 673
674 674 static void
675 675 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
676 676 {
677 677 int i;
678 678
679 679 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
680 680 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
681 681 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
682 682 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
683 683
684 684 /* Copy fields. */
685 685 ndn->dn_objset = odn->dn_objset;
686 686 ndn->dn_object = odn->dn_object;
687 687 ndn->dn_dbuf = odn->dn_dbuf;
688 688 ndn->dn_handle = odn->dn_handle;
689 689 ndn->dn_phys = odn->dn_phys;
690 690 ndn->dn_type = odn->dn_type;
691 691 ndn->dn_bonuslen = odn->dn_bonuslen;
692 692 ndn->dn_bonustype = odn->dn_bonustype;
693 693 ndn->dn_nblkptr = odn->dn_nblkptr;
694 694 ndn->dn_checksum = odn->dn_checksum;
695 695 ndn->dn_compress = odn->dn_compress;
696 696 ndn->dn_nlevels = odn->dn_nlevels;
697 697 ndn->dn_indblkshift = odn->dn_indblkshift;
698 698 ndn->dn_datablkshift = odn->dn_datablkshift;
699 699 ndn->dn_datablkszsec = odn->dn_datablkszsec;
700 700 ndn->dn_datablksz = odn->dn_datablksz;
701 701 ndn->dn_maxblkid = odn->dn_maxblkid;
702 702 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
703 703 sizeof (odn->dn_next_nblkptr));
704 704 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
705 705 sizeof (odn->dn_next_nlevels));
706 706 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
707 707 sizeof (odn->dn_next_indblkshift));
708 708 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
709 709 sizeof (odn->dn_next_bonustype));
710 710 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
711 711 sizeof (odn->dn_rm_spillblk));
712 712 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
713 713 sizeof (odn->dn_next_bonuslen));
714 714 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
715 715 sizeof (odn->dn_next_blksz));
716 716 for (i = 0; i < TXG_SIZE; i++) {
717 717 list_move_tail(&ndn->dn_dirty_records[i],
718 718 &odn->dn_dirty_records[i]);
719 719 }
720 720 bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
721 721 sizeof (odn->dn_free_ranges));
722 722 ndn->dn_allocated_txg = odn->dn_allocated_txg;
723 723 ndn->dn_free_txg = odn->dn_free_txg;
724 724 ndn->dn_assigned_txg = odn->dn_assigned_txg;
725 725 ndn->dn_dirtyctx = odn->dn_dirtyctx;
726 726 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
727 727 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
728 728 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
729 729 ASSERT(avl_is_empty(&ndn->dn_dbufs));
730 730 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs);
731 731 ndn->dn_dbufs_count = odn->dn_dbufs_count;
732 732 ndn->dn_unlisted_l0_blkid = odn->dn_unlisted_l0_blkid;
733 733 ndn->dn_bonus = odn->dn_bonus;
734 734 ndn->dn_have_spill = odn->dn_have_spill;
735 735 ndn->dn_zio = odn->dn_zio;
736 736 ndn->dn_oldused = odn->dn_oldused;
737 737 ndn->dn_oldflags = odn->dn_oldflags;
738 738 ndn->dn_olduid = odn->dn_olduid;
739 739 ndn->dn_oldgid = odn->dn_oldgid;
740 740 ndn->dn_newuid = odn->dn_newuid;
741 741 ndn->dn_newgid = odn->dn_newgid;
742 742 ndn->dn_id_flags = odn->dn_id_flags;
743 743 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
744 744 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
745 745 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
746 746 ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
747 747 ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
748 748
749 749 /*
750 750 * Update back pointers. Updating the handle fixes the back pointer of
751 751 * every descendant dbuf as well as the bonus dbuf.
752 752 */
753 753 ASSERT(ndn->dn_handle->dnh_dnode == odn);
754 754 ndn->dn_handle->dnh_dnode = ndn;
755 755 if (ndn->dn_zfetch.zf_dnode == odn) {
756 756 ndn->dn_zfetch.zf_dnode = ndn;
757 757 }
758 758
759 759 /*
760 760 * Invalidate the original dnode by clearing all of its back pointers.
761 761 */
762 762 odn->dn_dbuf = NULL;
763 763 odn->dn_handle = NULL;
764 764 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t),
765 765 offsetof(dmu_buf_impl_t, db_link));
766 766 odn->dn_dbufs_count = 0;
767 767 odn->dn_unlisted_l0_blkid = 0;
768 768 odn->dn_bonus = NULL;
769 769 odn->dn_zfetch.zf_dnode = NULL;
770 770
771 771 /*
772 772 * Set the low bit of the objset pointer to ensure that dnode_move()
773 773 * recognizes the dnode as invalid in any subsequent callback.
774 774 */
775 775 POINTER_INVALIDATE(&odn->dn_objset);
776 776
777 777 /*
778 778 * Satisfy the destructor.
779 779 */
780 780 for (i = 0; i < TXG_SIZE; i++) {
781 781 list_create(&odn->dn_dirty_records[i],
782 782 sizeof (dbuf_dirty_record_t),
783 783 offsetof(dbuf_dirty_record_t, dr_dirty_node));
784 784 odn->dn_free_ranges[i] = NULL;
785 785 odn->dn_next_nlevels[i] = 0;
786 786 odn->dn_next_indblkshift[i] = 0;
787 787 odn->dn_next_bonustype[i] = 0;
788 788 odn->dn_rm_spillblk[i] = 0;
789 789 odn->dn_next_bonuslen[i] = 0;
790 790 odn->dn_next_blksz[i] = 0;
791 791 }
792 792 odn->dn_allocated_txg = 0;
793 793 odn->dn_free_txg = 0;
794 794 odn->dn_assigned_txg = 0;
795 795 odn->dn_dirtyctx = 0;
796 796 odn->dn_dirtyctx_firstset = NULL;
797 797 odn->dn_have_spill = B_FALSE;
798 798 odn->dn_zio = NULL;
799 799 odn->dn_oldused = 0;
800 800 odn->dn_oldflags = 0;
801 801 odn->dn_olduid = 0;
802 802 odn->dn_oldgid = 0;
803 803 odn->dn_newuid = 0;
804 804 odn->dn_newgid = 0;
805 805 odn->dn_id_flags = 0;
806 806
807 807 /*
808 808 * Mark the dnode.
809 809 */
810 810 ndn->dn_moved = 1;
811 811 odn->dn_moved = (uint8_t)-1;
812 812 }
813 813
814 814 #ifdef _KERNEL
815 815 /*ARGSUSED*/
816 816 static kmem_cbrc_t
817 817 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
818 818 {
819 819 dnode_t *odn = buf, *ndn = newbuf;
820 820 objset_t *os;
821 821 int64_t refcount;
822 822 uint32_t dbufs;
823 823
824 824 /*
825 825 * The dnode is on the objset's list of known dnodes if the objset
826 826 * pointer is valid. We set the low bit of the objset pointer when
827 827 * freeing the dnode to invalidate it, and the memory patterns written
828 828 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
829 829 * A newly created dnode sets the objset pointer last of all to indicate
830 830 * that the dnode is known and in a valid state to be moved by this
831 831 * function.
832 832 */
833 833 os = odn->dn_objset;
834 834 if (!POINTER_IS_VALID(os)) {
835 835 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
836 836 return (KMEM_CBRC_DONT_KNOW);
837 837 }
838 838
839 839 /*
840 840 * Ensure that the objset does not go away during the move.
841 841 */
842 842 rw_enter(&os_lock, RW_WRITER);
843 843 if (os != odn->dn_objset) {
844 844 rw_exit(&os_lock);
845 845 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
846 846 return (KMEM_CBRC_DONT_KNOW);
847 847 }
848 848
849 849 /*
850 850 * If the dnode is still valid, then so is the objset. We know that no
851 851 * valid objset can be freed while we hold os_lock, so we can safely
852 852 * ensure that the objset remains in use.
853 853 */
854 854 mutex_enter(&os->os_lock);
855 855
856 856 /*
857 857 * Recheck the objset pointer in case the dnode was removed just before
858 858 * acquiring the lock.
859 859 */
860 860 if (os != odn->dn_objset) {
861 861 mutex_exit(&os->os_lock);
862 862 rw_exit(&os_lock);
863 863 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
864 864 return (KMEM_CBRC_DONT_KNOW);
865 865 }
866 866
867 867 /*
868 868 * At this point we know that as long as we hold os->os_lock, the dnode
869 869 * cannot be freed and fields within the dnode can be safely accessed.
870 870 * The objset listing this dnode cannot go away as long as this dnode is
871 871 * on its list.
872 872 */
873 873 rw_exit(&os_lock);
874 874 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
875 875 mutex_exit(&os->os_lock);
876 876 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
877 877 return (KMEM_CBRC_NO);
878 878 }
879 879 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
880 880
881 881 /*
882 882 * Lock the dnode handle to prevent the dnode from obtaining any new
883 883 * holds. This also prevents the descendant dbufs and the bonus dbuf
884 884 * from accessing the dnode, so that we can discount their holds. The
885 885 * handle is safe to access because we know that while the dnode cannot
886 886 * go away, neither can its handle. Once we hold dnh_zrlock, we can
887 887 * safely move any dnode referenced only by dbufs.
888 888 */
889 889 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
890 890 mutex_exit(&os->os_lock);
891 891 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
892 892 return (KMEM_CBRC_LATER);
893 893 }
894 894
895 895 /*
896 896 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
897 897 * We need to guarantee that there is a hold for every dbuf in order to
898 898 * determine whether the dnode is actively referenced. Falsely matching
899 899 * a dbuf to an active hold would lead to an unsafe move. It's possible
900 900 * that a thread already having an active dnode hold is about to add a
901 901 * dbuf, and we can't compare hold and dbuf counts while the add is in
902 902 * progress.
903 903 */
904 904 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
905 905 zrl_exit(&odn->dn_handle->dnh_zrlock);
906 906 mutex_exit(&os->os_lock);
907 907 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
908 908 return (KMEM_CBRC_LATER);
909 909 }
910 910
911 911 /*
912 912 * A dbuf may be removed (evicted) without an active dnode hold. In that
913 913 * case, the dbuf count is decremented under the handle lock before the
914 914 * dbuf's hold is released. This order ensures that if we count the hold
915 915 * after the dbuf is removed but before its hold is released, we will
916 916 * treat the unmatched hold as active and exit safely. If we count the
917 917 * hold before the dbuf is removed, the hold is discounted, and the
918 918 * removal is blocked until the move completes.
919 919 */
920 920 refcount = refcount_count(&odn->dn_holds);
921 921 ASSERT(refcount >= 0);
922 922 dbufs = odn->dn_dbufs_count;
923 923
924 924 /* We can't have more dbufs than dnode holds. */
925 925 ASSERT3U(dbufs, <=, refcount);
926 926 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
927 927 uint32_t, dbufs);
928 928
929 929 if (refcount > dbufs) {
930 930 rw_exit(&odn->dn_struct_rwlock);
931 931 zrl_exit(&odn->dn_handle->dnh_zrlock);
932 932 mutex_exit(&os->os_lock);
933 933 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
934 934 return (KMEM_CBRC_LATER);
935 935 }
936 936
937 937 rw_exit(&odn->dn_struct_rwlock);
938 938
939 939 /*
940 940 * At this point we know that anyone with a hold on the dnode is not
941 941 * actively referencing it. The dnode is known and in a valid state to
942 942 * move. We're holding the locks needed to execute the critical section.
943 943 */
944 944 dnode_move_impl(odn, ndn);
945 945
946 946 list_link_replace(&odn->dn_link, &ndn->dn_link);
947 947 /* If the dnode was safe to move, the refcount cannot have changed. */
948 948 ASSERT(refcount == refcount_count(&ndn->dn_holds));
949 949 ASSERT(dbufs == ndn->dn_dbufs_count);
950 950 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
951 951 mutex_exit(&os->os_lock);
952 952
953 953 return (KMEM_CBRC_YES);
954 954 }
955 955 #endif /* _KERNEL */
956 956
957 957 void
958 958 dnode_special_close(dnode_handle_t *dnh)
959 959 {
960 960 dnode_t *dn = dnh->dnh_dnode;
961 961
962 962 /*
963 963 * Wait for final references to the dnode to clear. This can
964 964 * only happen if the arc is asyncronously evicting state that
965 965 * has a hold on this dnode while we are trying to evict this
966 966 * dnode.
967 967 */
968 968 while (refcount_count(&dn->dn_holds) > 0)
969 969 delay(1);
970 970 zrl_add(&dnh->dnh_zrlock);
971 971 dnode_destroy(dn); /* implicit zrl_remove() */
972 972 zrl_destroy(&dnh->dnh_zrlock);
973 973 dnh->dnh_dnode = NULL;
974 974 }
975 975
976 976 dnode_t *
977 977 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
978 978 dnode_handle_t *dnh)
979 979 {
980 980 dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh);
981 981 dnh->dnh_dnode = dn;
982 982 zrl_init(&dnh->dnh_zrlock);
983 983 DNODE_VERIFY(dn);
984 984 return (dn);
985 985 }
986 986
987 987 static void
988 988 dnode_buf_pageout(dmu_buf_t *db, void *arg)
989 989 {
990 990 dnode_children_t *children_dnodes = arg;
991 991 int i;
992 992 int epb = db->db_size >> DNODE_SHIFT;
993 993
994 994 ASSERT(epb == children_dnodes->dnc_count);
995 995
996 996 for (i = 0; i < epb; i++) {
997 997 dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
998 998 dnode_t *dn;
999 999
1000 1000 /*
1001 1001 * The dnode handle lock guards against the dnode moving to
1002 1002 * another valid address, so there is no need here to guard
1003 1003 * against changes to or from NULL.
1004 1004 */
1005 1005 if (dnh->dnh_dnode == NULL) {
1006 1006 zrl_destroy(&dnh->dnh_zrlock);
1007 1007 continue;
1008 1008 }
1009 1009
1010 1010 zrl_add(&dnh->dnh_zrlock);
1011 1011 dn = dnh->dnh_dnode;
1012 1012 /*
1013 1013 * If there are holds on this dnode, then there should
1014 1014 * be holds on the dnode's containing dbuf as well; thus
1015 1015 * it wouldn't be eligible for eviction and this function
1016 1016 * would not have been called.
1017 1017 */
1018 1018 ASSERT(refcount_is_zero(&dn->dn_holds));
1019 1019 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
1020 1020
1021 1021 dnode_destroy(dn); /* implicit zrl_remove() */
1022 1022 zrl_destroy(&dnh->dnh_zrlock);
1023 1023 dnh->dnh_dnode = NULL;
1024 1024 }
1025 1025 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1026 1026 epb * sizeof (dnode_handle_t));
1027 1027 }
1028 1028
1029 1029 /*
1030 1030 * errors:
1031 1031 * EINVAL - invalid object number.
1032 1032 * EIO - i/o error.
1033 1033 * succeeds even for free dnodes.
1034 1034 */
1035 1035 int
1036 1036 dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1037 1037 void *tag, dnode_t **dnp)
1038 1038 {
1039 1039 int epb, idx, err;
1040 1040 int drop_struct_lock = FALSE;
1041 1041 int type;
1042 1042 uint64_t blk;
1043 1043 dnode_t *mdn, *dn;
1044 1044 dmu_buf_impl_t *db;
1045 1045 dnode_children_t *children_dnodes;
1046 1046 dnode_handle_t *dnh;
1047 1047
1048 1048 /*
1049 1049 * If you are holding the spa config lock as writer, you shouldn't
1050 1050 * be asking the DMU to do *anything* unless it's the root pool
1051 1051 * which may require us to read from the root filesystem while
1052 1052 * holding some (not all) of the locks as writer.
1053 1053 */
1054 1054 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1055 1055 (spa_is_root(os->os_spa) &&
1056 1056 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1057 1057
1058 1058 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1059 1059 dn = (object == DMU_USERUSED_OBJECT) ?
1060 1060 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1061 1061 if (dn == NULL)
1062 1062 return (SET_ERROR(ENOENT));
1063 1063 type = dn->dn_type;
1064 1064 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1065 1065 return (SET_ERROR(ENOENT));
1066 1066 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1067 1067 return (SET_ERROR(EEXIST));
1068 1068 DNODE_VERIFY(dn);
1069 1069 (void) refcount_add(&dn->dn_holds, tag);
1070 1070 *dnp = dn;
1071 1071 return (0);
1072 1072 }
1073 1073
1074 1074 if (object == 0 || object >= DN_MAX_OBJECT)
1075 1075 return (SET_ERROR(EINVAL));
1076 1076
1077 1077 mdn = DMU_META_DNODE(os);
1078 1078 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1079 1079
1080 1080 DNODE_VERIFY(mdn);
1081 1081
1082 1082 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1083 1083 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1084 1084 drop_struct_lock = TRUE;
1085 1085 }
1086 1086
1087 1087 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
1088 1088
1089 1089 db = dbuf_hold(mdn, blk, FTAG);
1090 1090 if (drop_struct_lock)
1091 1091 rw_exit(&mdn->dn_struct_rwlock);
1092 1092 if (db == NULL)
1093 1093 return (SET_ERROR(EIO));
1094 1094 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1095 1095 if (err) {
1096 1096 dbuf_rele(db, FTAG);
1097 1097 return (err);
1098 1098 }
1099 1099
1100 1100 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1101 1101 epb = db->db.db_size >> DNODE_SHIFT;
1102 1102
1103 1103 idx = object & (epb-1);
1104 1104
1105 1105 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1106 1106 children_dnodes = dmu_buf_get_user(&db->db);
1107 1107 if (children_dnodes == NULL) {
1108 1108 int i;
1109 1109 dnode_children_t *winner;
1110 1110 children_dnodes = kmem_alloc(sizeof (dnode_children_t) +
1111 1111 epb * sizeof (dnode_handle_t), KM_SLEEP);
1112 1112 children_dnodes->dnc_count = epb;
1113 1113 dnh = &children_dnodes->dnc_children[0];
1114 1114 for (i = 0; i < epb; i++) {
1115 1115 zrl_init(&dnh[i].dnh_zrlock);
1116 1116 dnh[i].dnh_dnode = NULL;
1117 1117 }
1118 1118 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
1119 1119 dnode_buf_pageout)) {
1120 1120
1121 1121 for (i = 0; i < epb; i++) {
1122 1122 zrl_destroy(&dnh[i].dnh_zrlock);
1123 1123 }
1124 1124
1125 1125 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1126 1126 epb * sizeof (dnode_handle_t));
1127 1127 children_dnodes = winner;
1128 1128 }
1129 1129 }
1130 1130 ASSERT(children_dnodes->dnc_count == epb);
1131 1131
1132 1132 dnh = &children_dnodes->dnc_children[idx];
1133 1133 zrl_add(&dnh->dnh_zrlock);
1134 1134 if ((dn = dnh->dnh_dnode) == NULL) {
1135 1135 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1136 1136 dnode_t *winner;
1137 1137
1138 1138 dn = dnode_create(os, phys, db, object, dnh);
1139 1139 winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn);
1140 1140 if (winner != NULL) {
1141 1141 zrl_add(&dnh->dnh_zrlock);
1142 1142 dnode_destroy(dn); /* implicit zrl_remove() */
1143 1143 dn = winner;
1144 1144 }
1145 1145 }
1146 1146
1147 1147 mutex_enter(&dn->dn_mtx);
1148 1148 type = dn->dn_type;
1149 1149 if (dn->dn_free_txg ||
1150 1150 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
1151 1151 ((flag & DNODE_MUST_BE_FREE) &&
1152 1152 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1153 1153 mutex_exit(&dn->dn_mtx);
1154 1154 zrl_remove(&dnh->dnh_zrlock);
1155 1155 dbuf_rele(db, FTAG);
1156 1156 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1157 1157 }
1158 1158 mutex_exit(&dn->dn_mtx);
1159 1159
1160 1160 if (refcount_add(&dn->dn_holds, tag) == 1)
1161 1161 dbuf_add_ref(db, dnh);
1162 1162 /* Now we can rely on the hold to prevent the dnode from moving. */
1163 1163 zrl_remove(&dnh->dnh_zrlock);
1164 1164
1165 1165 DNODE_VERIFY(dn);
1166 1166 ASSERT3P(dn->dn_dbuf, ==, db);
1167 1167 ASSERT3U(dn->dn_object, ==, object);
1168 1168 dbuf_rele(db, FTAG);
1169 1169
1170 1170 *dnp = dn;
1171 1171 return (0);
1172 1172 }
1173 1173
1174 1174 /*
1175 1175 * Return held dnode if the object is allocated, NULL if not.
1176 1176 */
1177 1177 int
1178 1178 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1179 1179 {
1180 1180 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1181 1181 }
1182 1182
1183 1183 /*
1184 1184 * Can only add a reference if there is already at least one
1185 1185 * reference on the dnode. Returns FALSE if unable to add a
1186 1186 * new reference.
1187 1187 */
1188 1188 boolean_t
1189 1189 dnode_add_ref(dnode_t *dn, void *tag)
1190 1190 {
1191 1191 mutex_enter(&dn->dn_mtx);
1192 1192 if (refcount_is_zero(&dn->dn_holds)) {
1193 1193 mutex_exit(&dn->dn_mtx);
1194 1194 return (FALSE);
1195 1195 }
1196 1196 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1197 1197 mutex_exit(&dn->dn_mtx);
1198 1198 return (TRUE);
1199 1199 }
1200 1200
1201 1201 void
1202 1202 dnode_rele(dnode_t *dn, void *tag)
1203 1203 {
1204 1204 uint64_t refs;
1205 1205 /* Get while the hold prevents the dnode from moving. */
1206 1206 dmu_buf_impl_t *db = dn->dn_dbuf;
1207 1207 dnode_handle_t *dnh = dn->dn_handle;
1208 1208
1209 1209 mutex_enter(&dn->dn_mtx);
1210 1210 refs = refcount_remove(&dn->dn_holds, tag);
1211 1211 mutex_exit(&dn->dn_mtx);
1212 1212
1213 1213 /*
1214 1214 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1215 1215 * indirectly by dbuf_rele() while relying on the dnode handle to
1216 1216 * prevent the dnode from moving, since releasing the last hold could
1217 1217 * result in the dnode's parent dbuf evicting its dnode handles. For
1218 1218 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1219 1219 * other direct or indirect hold on the dnode must first drop the dnode
1220 1220 * handle.
1221 1221 */
1222 1222 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1223 1223
1224 1224 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1225 1225 if (refs == 0 && db != NULL) {
1226 1226 /*
1227 1227 * Another thread could add a hold to the dnode handle in
1228 1228 * dnode_hold_impl() while holding the parent dbuf. Since the
1229 1229 * hold on the parent dbuf prevents the handle from being
1230 1230 * destroyed, the hold on the handle is OK. We can't yet assert
1231 1231 * that the handle has zero references, but that will be
1232 1232 * asserted anyway when the handle gets destroyed.
1233 1233 */
1234 1234 dbuf_rele(db, dnh);
1235 1235 }
1236 1236 }
1237 1237
1238 1238 void
1239 1239 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1240 1240 {
1241 1241 objset_t *os = dn->dn_objset;
1242 1242 uint64_t txg = tx->tx_txg;
1243 1243
1244 1244 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1245 1245 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1246 1246 return;
1247 1247 }
1248 1248
1249 1249 DNODE_VERIFY(dn);
1250 1250
1251 1251 #ifdef ZFS_DEBUG
1252 1252 mutex_enter(&dn->dn_mtx);
1253 1253 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1254 1254 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1255 1255 mutex_exit(&dn->dn_mtx);
1256 1256 #endif
1257 1257
1258 1258 /*
1259 1259 * Determine old uid/gid when necessary
1260 1260 */
1261 1261 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1262 1262
1263 1263 mutex_enter(&os->os_lock);
1264 1264
1265 1265 /*
1266 1266 * If we are already marked dirty, we're done.
1267 1267 */
1268 1268 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1269 1269 mutex_exit(&os->os_lock);
1270 1270 return;
1271 1271 }
1272 1272
1273 1273 ASSERT(!refcount_is_zero(&dn->dn_holds) ||
1274 1274 !avl_is_empty(&dn->dn_dbufs));
1275 1275 ASSERT(dn->dn_datablksz != 0);
1276 1276 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1277 1277 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1278 1278 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1279 1279
1280 1280 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1281 1281 dn->dn_object, txg);
1282 1282
1283 1283 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1284 1284 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1285 1285 } else {
1286 1286 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1287 1287 }
1288 1288
1289 1289 mutex_exit(&os->os_lock);
1290 1290
1291 1291 /*
↓ open down ↓ |
1291 lines elided |
↑ open up ↑ |
1292 1292 * The dnode maintains a hold on its containing dbuf as
1293 1293 * long as there are holds on it. Each instantiated child
1294 1294 * dbuf maintains a hold on the dnode. When the last child
1295 1295 * drops its hold, the dnode will drop its hold on the
1296 1296 * containing dbuf. We add a "dirty hold" here so that the
1297 1297 * dnode will hang around after we finish processing its
1298 1298 * children.
1299 1299 */
1300 1300 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1301 1301
1302 - (void) dbuf_dirty(dn->dn_dbuf, tx);
1302 + (void) dbuf_dirty(dn->dn_dbuf, tx, B_FALSE);
1303 1303
1304 1304 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1305 1305 }
1306 1306
1307 1307 void
1308 1308 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1309 1309 {
1310 1310 int txgoff = tx->tx_txg & TXG_MASK;
1311 1311
1312 1312 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1313 1313
1314 1314 /* we should be the only holder... hopefully */
1315 1315 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1316 1316
1317 1317 mutex_enter(&dn->dn_mtx);
1318 1318 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1319 1319 mutex_exit(&dn->dn_mtx);
1320 1320 return;
1321 1321 }
1322 1322 dn->dn_free_txg = tx->tx_txg;
1323 1323 mutex_exit(&dn->dn_mtx);
1324 1324
1325 1325 /*
1326 1326 * If the dnode is already dirty, it needs to be moved from
1327 1327 * the dirty list to the free list.
1328 1328 */
1329 1329 mutex_enter(&dn->dn_objset->os_lock);
1330 1330 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
1331 1331 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
1332 1332 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1333 1333 mutex_exit(&dn->dn_objset->os_lock);
1334 1334 } else {
1335 1335 mutex_exit(&dn->dn_objset->os_lock);
1336 1336 dnode_setdirty(dn, tx);
1337 1337 }
1338 1338 }
1339 1339
1340 1340 /*
1341 1341 * Try to change the block size for the indicated dnode. This can only
1342 1342 * succeed if there are no blocks allocated or dirty beyond first block
1343 1343 */
1344 1344 int
1345 1345 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1346 1346 {
1347 1347 dmu_buf_impl_t *db;
1348 1348 int err;
1349 1349
1350 1350 if (size == 0)
1351 1351 size = SPA_MINBLOCKSIZE;
1352 1352 if (size > SPA_MAXBLOCKSIZE)
1353 1353 size = SPA_MAXBLOCKSIZE;
1354 1354 else
1355 1355 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1356 1356
1357 1357 if (ibs == dn->dn_indblkshift)
1358 1358 ibs = 0;
1359 1359
1360 1360 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1361 1361 return (0);
1362 1362
1363 1363 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1364 1364
1365 1365 /* Check for any allocated blocks beyond the first */
1366 1366 if (dn->dn_maxblkid != 0)
1367 1367 goto fail;
1368 1368
1369 1369 mutex_enter(&dn->dn_dbufs_mtx);
1370 1370 for (db = avl_first(&dn->dn_dbufs); db != NULL;
1371 1371 db = AVL_NEXT(&dn->dn_dbufs, db)) {
1372 1372 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1373 1373 db->db_blkid != DMU_SPILL_BLKID) {
1374 1374 mutex_exit(&dn->dn_dbufs_mtx);
1375 1375 goto fail;
1376 1376 }
1377 1377 }
1378 1378 mutex_exit(&dn->dn_dbufs_mtx);
1379 1379
1380 1380 if (ibs && dn->dn_nlevels != 1)
1381 1381 goto fail;
1382 1382
1383 1383 /* resize the old block */
1384 1384 err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
1385 1385 if (err == 0)
1386 1386 dbuf_new_size(db, size, tx);
1387 1387 else if (err != ENOENT)
1388 1388 goto fail;
1389 1389
1390 1390 dnode_setdblksz(dn, size);
1391 1391 dnode_setdirty(dn, tx);
1392 1392 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1393 1393 if (ibs) {
1394 1394 dn->dn_indblkshift = ibs;
1395 1395 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1396 1396 }
1397 1397 /* rele after we have fixed the blocksize in the dnode */
1398 1398 if (db)
1399 1399 dbuf_rele(db, FTAG);
1400 1400
1401 1401 rw_exit(&dn->dn_struct_rwlock);
1402 1402 return (0);
1403 1403
1404 1404 fail:
1405 1405 rw_exit(&dn->dn_struct_rwlock);
1406 1406 return (SET_ERROR(ENOTSUP));
1407 1407 }
1408 1408
1409 1409 /* read-holding callers must not rely on the lock being continuously held */
1410 1410 void
1411 1411 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1412 1412 {
1413 1413 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1414 1414 int epbs, new_nlevels;
1415 1415 uint64_t sz;
1416 1416
1417 1417 ASSERT(blkid != DMU_BONUS_BLKID);
1418 1418
1419 1419 ASSERT(have_read ?
1420 1420 RW_READ_HELD(&dn->dn_struct_rwlock) :
1421 1421 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1422 1422
1423 1423 /*
1424 1424 * if we have a read-lock, check to see if we need to do any work
1425 1425 * before upgrading to a write-lock.
1426 1426 */
1427 1427 if (have_read) {
1428 1428 if (blkid <= dn->dn_maxblkid)
1429 1429 return;
1430 1430
1431 1431 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1432 1432 rw_exit(&dn->dn_struct_rwlock);
1433 1433 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1434 1434 }
1435 1435 }
1436 1436
1437 1437 if (blkid <= dn->dn_maxblkid)
1438 1438 goto out;
1439 1439
1440 1440 dn->dn_maxblkid = blkid;
1441 1441
1442 1442 /*
1443 1443 * Compute the number of levels necessary to support the new maxblkid.
1444 1444 */
1445 1445 new_nlevels = 1;
1446 1446 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1447 1447 for (sz = dn->dn_nblkptr;
1448 1448 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1449 1449 new_nlevels++;
1450 1450
1451 1451 if (new_nlevels > dn->dn_nlevels) {
1452 1452 int old_nlevels = dn->dn_nlevels;
1453 1453 dmu_buf_impl_t *db;
1454 1454 list_t *list;
↓ open down ↓ |
142 lines elided |
↑ open up ↑ |
1455 1455 dbuf_dirty_record_t *new, *dr, *dr_next;
1456 1456
1457 1457 dn->dn_nlevels = new_nlevels;
1458 1458
1459 1459 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1460 1460 dn->dn_next_nlevels[txgoff] = new_nlevels;
1461 1461
1462 1462 /* dirty the left indirects */
1463 1463 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1464 1464 ASSERT(db != NULL);
1465 - new = dbuf_dirty(db, tx);
1465 + new = dbuf_dirty(db, tx, B_FALSE);
1466 1466 dbuf_rele(db, FTAG);
1467 1467
1468 1468 /* transfer the dirty records to the new indirect */
1469 1469 mutex_enter(&dn->dn_mtx);
1470 1470 mutex_enter(&new->dt.di.dr_mtx);
1471 1471 list = &dn->dn_dirty_records[txgoff];
1472 1472 for (dr = list_head(list); dr; dr = dr_next) {
1473 1473 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1474 1474 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1475 1475 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1476 1476 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1477 1477 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1478 1478 list_remove(&dn->dn_dirty_records[txgoff], dr);
1479 1479 list_insert_tail(&new->dt.di.dr_children, dr);
1480 1480 dr->dr_parent = new;
1481 1481 }
1482 1482 }
1483 1483 mutex_exit(&new->dt.di.dr_mtx);
1484 1484 mutex_exit(&dn->dn_mtx);
1485 1485 }
1486 1486
1487 1487 out:
1488 1488 if (have_read)
1489 1489 rw_downgrade(&dn->dn_struct_rwlock);
1490 1490 }
1491 1491
1492 1492 void
1493 1493 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1494 1494 {
1495 1495 dmu_buf_impl_t *db;
1496 1496 uint64_t blkoff, blkid, nblks;
1497 1497 int blksz, blkshift, head, tail;
1498 1498 int trunc = FALSE;
1499 1499 int epbs;
1500 1500
1501 1501 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1502 1502 blksz = dn->dn_datablksz;
1503 1503 blkshift = dn->dn_datablkshift;
1504 1504 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1505 1505
1506 1506 if (len == DMU_OBJECT_END) {
1507 1507 len = UINT64_MAX - off;
1508 1508 trunc = TRUE;
1509 1509 }
1510 1510
1511 1511 /*
1512 1512 * First, block align the region to free:
1513 1513 */
1514 1514 if (ISP2(blksz)) {
1515 1515 head = P2NPHASE(off, blksz);
1516 1516 blkoff = P2PHASE(off, blksz);
1517 1517 if ((off >> blkshift) > dn->dn_maxblkid)
1518 1518 goto out;
1519 1519 } else {
1520 1520 ASSERT(dn->dn_maxblkid == 0);
1521 1521 if (off == 0 && len >= blksz) {
1522 1522 /*
1523 1523 * Freeing the whole block; fast-track this request.
1524 1524 * Note that we won't dirty any indirect blocks,
1525 1525 * which is fine because we will be freeing the entire
1526 1526 * file and thus all indirect blocks will be freed
1527 1527 * by free_children().
1528 1528 */
1529 1529 blkid = 0;
1530 1530 nblks = 1;
1531 1531 goto done;
1532 1532 } else if (off >= blksz) {
1533 1533 /* Freeing past end-of-data */
1534 1534 goto out;
1535 1535 } else {
1536 1536 /* Freeing part of the block. */
1537 1537 head = blksz - off;
1538 1538 ASSERT3U(head, >, 0);
1539 1539 }
1540 1540 blkoff = off;
1541 1541 }
1542 1542 /* zero out any partial block data at the start of the range */
1543 1543 if (head) {
1544 1544 ASSERT3U(blkoff + head, ==, blksz);
1545 1545 if (len < head)
1546 1546 head = len;
1547 1547 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1548 1548 FTAG, &db) == 0) {
1549 1549 caddr_t data;
1550 1550
1551 1551 /* don't dirty if it isn't on disk and isn't dirty */
1552 1552 if (db->db_last_dirty ||
1553 1553 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1554 1554 rw_exit(&dn->dn_struct_rwlock);
1555 1555 dmu_buf_will_dirty(&db->db, tx);
1556 1556 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1557 1557 data = db->db.db_data;
1558 1558 bzero(data + blkoff, head);
1559 1559 }
1560 1560 dbuf_rele(db, FTAG);
1561 1561 }
1562 1562 off += head;
1563 1563 len -= head;
1564 1564 }
1565 1565
1566 1566 /* If the range was less than one block, we're done */
1567 1567 if (len == 0)
1568 1568 goto out;
1569 1569
1570 1570 /* If the remaining range is past end of file, we're done */
1571 1571 if ((off >> blkshift) > dn->dn_maxblkid)
1572 1572 goto out;
1573 1573
1574 1574 ASSERT(ISP2(blksz));
1575 1575 if (trunc)
1576 1576 tail = 0;
1577 1577 else
1578 1578 tail = P2PHASE(len, blksz);
1579 1579
1580 1580 ASSERT0(P2PHASE(off, blksz));
1581 1581 /* zero out any partial block data at the end of the range */
1582 1582 if (tail) {
1583 1583 if (len < tail)
1584 1584 tail = len;
1585 1585 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1586 1586 TRUE, FTAG, &db) == 0) {
1587 1587 /* don't dirty if not on disk and not dirty */
1588 1588 if (db->db_last_dirty ||
1589 1589 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1590 1590 rw_exit(&dn->dn_struct_rwlock);
1591 1591 dmu_buf_will_dirty(&db->db, tx);
1592 1592 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1593 1593 bzero(db->db.db_data, tail);
1594 1594 }
1595 1595 dbuf_rele(db, FTAG);
1596 1596 }
1597 1597 len -= tail;
1598 1598 }
1599 1599
1600 1600 /* If the range did not include a full block, we are done */
1601 1601 if (len == 0)
1602 1602 goto out;
1603 1603
1604 1604 ASSERT(IS_P2ALIGNED(off, blksz));
1605 1605 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1606 1606 blkid = off >> blkshift;
1607 1607 nblks = len >> blkshift;
1608 1608 if (trunc)
1609 1609 nblks += 1;
1610 1610
1611 1611 /*
1612 1612 * Dirty the first and last indirect blocks, as they (and/or their
1613 1613 * parents) will need to be written out if they were only
1614 1614 * partially freed. Interior indirect blocks will be themselves freed,
1615 1615 * by free_children(), so they need not be dirtied. Note that these
1616 1616 * interior blocks have already been prefetched by dmu_tx_hold_free().
1617 1617 */
1618 1618 if (dn->dn_nlevels > 1) {
1619 1619 uint64_t first, last;
1620 1620
1621 1621 first = blkid >> epbs;
1622 1622 if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
1623 1623 dmu_buf_will_dirty(&db->db, tx);
1624 1624 dbuf_rele(db, FTAG);
1625 1625 }
1626 1626 if (trunc)
1627 1627 last = dn->dn_maxblkid >> epbs;
1628 1628 else
1629 1629 last = (blkid + nblks - 1) >> epbs;
1630 1630 if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
1631 1631 dmu_buf_will_dirty(&db->db, tx);
1632 1632 dbuf_rele(db, FTAG);
1633 1633 }
1634 1634 }
1635 1635
1636 1636 done:
1637 1637 /*
1638 1638 * Add this range to the dnode range list.
1639 1639 * We will finish up this free operation in the syncing phase.
1640 1640 */
1641 1641 mutex_enter(&dn->dn_mtx);
1642 1642 int txgoff = tx->tx_txg & TXG_MASK;
1643 1643 if (dn->dn_free_ranges[txgoff] == NULL) {
1644 1644 dn->dn_free_ranges[txgoff] =
1645 1645 range_tree_create(NULL, NULL, &dn->dn_mtx);
1646 1646 }
1647 1647 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
1648 1648 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
1649 1649 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1650 1650 blkid, nblks, tx->tx_txg);
1651 1651 mutex_exit(&dn->dn_mtx);
1652 1652
1653 1653 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1654 1654 dnode_setdirty(dn, tx);
1655 1655 out:
1656 1656
1657 1657 rw_exit(&dn->dn_struct_rwlock);
1658 1658 }
1659 1659
1660 1660 static boolean_t
1661 1661 dnode_spill_freed(dnode_t *dn)
1662 1662 {
1663 1663 int i;
1664 1664
1665 1665 mutex_enter(&dn->dn_mtx);
1666 1666 for (i = 0; i < TXG_SIZE; i++) {
1667 1667 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
1668 1668 break;
1669 1669 }
1670 1670 mutex_exit(&dn->dn_mtx);
1671 1671 return (i < TXG_SIZE);
1672 1672 }
1673 1673
1674 1674 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1675 1675 uint64_t
1676 1676 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1677 1677 {
1678 1678 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1679 1679 int i;
1680 1680
1681 1681 if (blkid == DMU_BONUS_BLKID)
1682 1682 return (FALSE);
1683 1683
1684 1684 /*
1685 1685 * If we're in the process of opening the pool, dp will not be
1686 1686 * set yet, but there shouldn't be anything dirty.
1687 1687 */
1688 1688 if (dp == NULL)
1689 1689 return (FALSE);
1690 1690
1691 1691 if (dn->dn_free_txg)
1692 1692 return (TRUE);
1693 1693
1694 1694 if (blkid == DMU_SPILL_BLKID)
1695 1695 return (dnode_spill_freed(dn));
1696 1696
1697 1697 mutex_enter(&dn->dn_mtx);
1698 1698 for (i = 0; i < TXG_SIZE; i++) {
1699 1699 if (dn->dn_free_ranges[i] != NULL &&
1700 1700 range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
1701 1701 break;
1702 1702 }
1703 1703 mutex_exit(&dn->dn_mtx);
1704 1704 return (i < TXG_SIZE);
1705 1705 }
1706 1706
1707 1707 /* call from syncing context when we actually write/free space for this dnode */
1708 1708 void
1709 1709 dnode_diduse_space(dnode_t *dn, int64_t delta)
1710 1710 {
1711 1711 uint64_t space;
1712 1712 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1713 1713 dn, dn->dn_phys,
1714 1714 (u_longlong_t)dn->dn_phys->dn_used,
1715 1715 (longlong_t)delta);
1716 1716
1717 1717 mutex_enter(&dn->dn_mtx);
1718 1718 space = DN_USED_BYTES(dn->dn_phys);
1719 1719 if (delta > 0) {
1720 1720 ASSERT3U(space + delta, >=, space); /* no overflow */
1721 1721 } else {
1722 1722 ASSERT3U(space, >=, -delta); /* no underflow */
1723 1723 }
1724 1724 space += delta;
1725 1725 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1726 1726 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1727 1727 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
1728 1728 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1729 1729 } else {
1730 1730 dn->dn_phys->dn_used = space;
1731 1731 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1732 1732 }
1733 1733 mutex_exit(&dn->dn_mtx);
1734 1734 }
1735 1735
1736 1736 /*
1737 1737 * Call when we think we're going to write/free space in open context to track
1738 1738 * the amount of memory in use by the currently open txg.
1739 1739 */
1740 1740 void
1741 1741 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1742 1742 {
1743 1743 objset_t *os = dn->dn_objset;
1744 1744 dsl_dataset_t *ds = os->os_dsl_dataset;
1745 1745 int64_t aspace = spa_get_asize(os->os_spa, space);
1746 1746
1747 1747 if (ds != NULL) {
1748 1748 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
1749 1749 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
1750 1750 }
1751 1751
1752 1752 dmu_tx_willuse_space(tx, aspace);
1753 1753 }
1754 1754
1755 1755 /*
1756 1756 * Scans a block at the indicated "level" looking for a hole or data,
1757 1757 * depending on 'flags'.
1758 1758 *
1759 1759 * If level > 0, then we are scanning an indirect block looking at its
1760 1760 * pointers. If level == 0, then we are looking at a block of dnodes.
1761 1761 *
1762 1762 * If we don't find what we are looking for in the block, we return ESRCH.
1763 1763 * Otherwise, return with *offset pointing to the beginning (if searching
1764 1764 * forwards) or end (if searching backwards) of the range covered by the
1765 1765 * block pointer we matched on (or dnode).
1766 1766 *
1767 1767 * The basic search algorithm used below by dnode_next_offset() is to
1768 1768 * use this function to search up the block tree (widen the search) until
1769 1769 * we find something (i.e., we don't return ESRCH) and then search back
1770 1770 * down the tree (narrow the search) until we reach our original search
1771 1771 * level.
1772 1772 */
1773 1773 static int
1774 1774 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1775 1775 int lvl, uint64_t blkfill, uint64_t txg)
1776 1776 {
1777 1777 dmu_buf_impl_t *db = NULL;
1778 1778 void *data = NULL;
1779 1779 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1780 1780 uint64_t epb = 1ULL << epbs;
1781 1781 uint64_t minfill, maxfill;
1782 1782 boolean_t hole;
1783 1783 int i, inc, error, span;
1784 1784
1785 1785 dprintf("probing object %llu offset %llx level %d of %u\n",
1786 1786 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1787 1787
1788 1788 hole = ((flags & DNODE_FIND_HOLE) != 0);
1789 1789 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1790 1790 ASSERT(txg == 0 || !hole);
1791 1791
1792 1792 if (lvl == dn->dn_phys->dn_nlevels) {
1793 1793 error = 0;
1794 1794 epb = dn->dn_phys->dn_nblkptr;
1795 1795 data = dn->dn_phys->dn_blkptr;
1796 1796 } else {
1797 1797 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1798 1798 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1799 1799 if (error) {
1800 1800 if (error != ENOENT)
1801 1801 return (error);
1802 1802 if (hole)
1803 1803 return (0);
1804 1804 /*
1805 1805 * This can only happen when we are searching up
1806 1806 * the block tree for data. We don't really need to
1807 1807 * adjust the offset, as we will just end up looking
1808 1808 * at the pointer to this block in its parent, and its
1809 1809 * going to be unallocated, so we will skip over it.
1810 1810 */
1811 1811 return (SET_ERROR(ESRCH));
1812 1812 }
1813 1813 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1814 1814 if (error) {
1815 1815 dbuf_rele(db, FTAG);
1816 1816 return (error);
1817 1817 }
1818 1818 data = db->db.db_data;
1819 1819 }
1820 1820
1821 1821
1822 1822 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
1823 1823 db->db_blkptr->blk_birth <= txg ||
1824 1824 BP_IS_HOLE(db->db_blkptr))) {
1825 1825 /*
1826 1826 * This can only happen when we are searching up the tree
1827 1827 * and these conditions mean that we need to keep climbing.
1828 1828 */
1829 1829 error = SET_ERROR(ESRCH);
1830 1830 } else if (lvl == 0) {
1831 1831 dnode_phys_t *dnp = data;
1832 1832 span = DNODE_SHIFT;
1833 1833 ASSERT(dn->dn_type == DMU_OT_DNODE);
1834 1834
1835 1835 for (i = (*offset >> span) & (blkfill - 1);
1836 1836 i >= 0 && i < blkfill; i += inc) {
1837 1837 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1838 1838 break;
1839 1839 *offset += (1ULL << span) * inc;
1840 1840 }
1841 1841 if (i < 0 || i == blkfill)
1842 1842 error = SET_ERROR(ESRCH);
1843 1843 } else {
1844 1844 blkptr_t *bp = data;
1845 1845 uint64_t start = *offset;
1846 1846 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1847 1847 minfill = 0;
1848 1848 maxfill = blkfill << ((lvl - 1) * epbs);
1849 1849
1850 1850 if (hole)
1851 1851 maxfill--;
1852 1852 else
1853 1853 minfill++;
1854 1854
1855 1855 *offset = *offset >> span;
1856 1856 for (i = BF64_GET(*offset, 0, epbs);
1857 1857 i >= 0 && i < epb; i += inc) {
1858 1858 if (BP_GET_FILL(&bp[i]) >= minfill &&
1859 1859 BP_GET_FILL(&bp[i]) <= maxfill &&
1860 1860 (hole || bp[i].blk_birth > txg))
1861 1861 break;
1862 1862 if (inc > 0 || *offset > 0)
1863 1863 *offset += inc;
1864 1864 }
1865 1865 *offset = *offset << span;
1866 1866 if (inc < 0) {
1867 1867 /* traversing backwards; position offset at the end */
1868 1868 ASSERT3U(*offset, <=, start);
1869 1869 *offset = MIN(*offset + (1ULL << span) - 1, start);
1870 1870 } else if (*offset < start) {
1871 1871 *offset = start;
1872 1872 }
1873 1873 if (i < 0 || i >= epb)
1874 1874 error = SET_ERROR(ESRCH);
1875 1875 }
1876 1876
1877 1877 if (db)
1878 1878 dbuf_rele(db, FTAG);
1879 1879
1880 1880 return (error);
1881 1881 }
1882 1882
1883 1883 /*
1884 1884 * Find the next hole, data, or sparse region at or after *offset.
1885 1885 * The value 'blkfill' tells us how many items we expect to find
1886 1886 * in an L0 data block; this value is 1 for normal objects,
1887 1887 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1888 1888 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1889 1889 *
1890 1890 * Examples:
1891 1891 *
1892 1892 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1893 1893 * Finds the next/previous hole/data in a file.
1894 1894 * Used in dmu_offset_next().
1895 1895 *
1896 1896 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1897 1897 * Finds the next free/allocated dnode an objset's meta-dnode.
1898 1898 * Only finds objects that have new contents since txg (ie.
1899 1899 * bonus buffer changes and content removal are ignored).
1900 1900 * Used in dmu_object_next().
1901 1901 *
1902 1902 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1903 1903 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1904 1904 * Used in dmu_object_alloc().
1905 1905 */
1906 1906 int
1907 1907 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1908 1908 int minlvl, uint64_t blkfill, uint64_t txg)
1909 1909 {
1910 1910 uint64_t initial_offset = *offset;
1911 1911 int lvl, maxlvl;
1912 1912 int error = 0;
1913 1913
1914 1914 if (!(flags & DNODE_FIND_HAVELOCK))
1915 1915 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1916 1916
1917 1917 if (dn->dn_phys->dn_nlevels == 0) {
1918 1918 error = SET_ERROR(ESRCH);
1919 1919 goto out;
1920 1920 }
1921 1921
1922 1922 if (dn->dn_datablkshift == 0) {
1923 1923 if (*offset < dn->dn_datablksz) {
1924 1924 if (flags & DNODE_FIND_HOLE)
1925 1925 *offset = dn->dn_datablksz;
1926 1926 } else {
1927 1927 error = SET_ERROR(ESRCH);
1928 1928 }
1929 1929 goto out;
1930 1930 }
1931 1931
1932 1932 maxlvl = dn->dn_phys->dn_nlevels;
1933 1933
1934 1934 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1935 1935 error = dnode_next_offset_level(dn,
1936 1936 flags, offset, lvl, blkfill, txg);
1937 1937 if (error != ESRCH)
1938 1938 break;
1939 1939 }
1940 1940
1941 1941 while (error == 0 && --lvl >= minlvl) {
1942 1942 error = dnode_next_offset_level(dn,
1943 1943 flags, offset, lvl, blkfill, txg);
1944 1944 }
1945 1945
1946 1946 /*
1947 1947 * There's always a "virtual hole" at the end of the object, even
1948 1948 * if all BP's which physically exist are non-holes.
1949 1949 */
1950 1950 if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 &&
1951 1951 minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) {
1952 1952 error = 0;
1953 1953 }
1954 1954
1955 1955 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
1956 1956 initial_offset < *offset : initial_offset > *offset))
1957 1957 error = SET_ERROR(ESRCH);
1958 1958 out:
1959 1959 if (!(flags & DNODE_FIND_HAVELOCK))
1960 1960 rw_exit(&dn->dn_struct_rwlock);
1961 1961
1962 1962 return (error);
1963 1963 }
↓ open down ↓ |
488 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX