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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/zfs_context.h> 30 #include <sys/dbuf.h> 31 #include <sys/dnode.h> 32 #include <sys/dmu.h> 33 #include <sys/dmu_tx.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/dsl_dataset.h> 36 #include <sys/spa.h> 37 #include <sys/zio.h> 38 39 static void 40 dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) 41 { 42 dmu_buf_impl_t *db; 43 int i; 44 uint64_t txg = tx->tx_txg; 45 46 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 47 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 48 /* this dnode can't be paged out because it's dirty */ 49 50 db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); 51 for (i = 0; i < dn->dn_phys->dn_nblkptr; i++) 52 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) 53 break; 54 if (i != dn->dn_phys->dn_nblkptr) { 55 ASSERT(list_link_active(&db->db_dirty_node[txg&TXG_MASK])); 56 57 dbuf_read_havestruct(db); 58 arc_release(db->db_buf, db); 59 /* copy dnode's block pointers to new indirect block */ 60 ASSERT3U(sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr, <=, 61 db->db.db_size); 62 bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, 63 sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr); 64 } 65 66 dn->dn_phys->dn_nlevels += 1; 67 dprintf("os=%p obj=%llu, increase to %d\n", 68 dn->dn_objset, dn->dn_object, 69 dn->dn_phys->dn_nlevels); 70 71 /* set dbuf's parent pointers to new indirect buf */ 72 for (i = 0; i < dn->dn_phys->dn_nblkptr; i++) { 73 dmu_buf_impl_t *child = 74 dbuf_find(dn, dn->dn_phys->dn_nlevels-2, i); 75 if (child == NULL) 76 continue; 77 if (child->db_dnode == NULL) { 78 mutex_exit(&child->db_mtx); 79 continue; 80 } 81 82 if (child->db_parent == NULL || 83 child->db_parent == dn->dn_dbuf) { 84 dprintf_dbuf_bp(child, child->db_blkptr, 85 "changing db_blkptr to new indirect %s", ""); 86 child->db_parent = db; 87 dbuf_add_ref(db, child); 88 if (db->db.db_data) { 89 child->db_blkptr = 90 (blkptr_t *)db->db.db_data + i; 91 } else { 92 child->db_blkptr = NULL; 93 } 94 dprintf_dbuf_bp(child, child->db_blkptr, 95 "changed db_blkptr to new indirect %s", ""); 96 } 97 ASSERT3P(child->db_parent, ==, db); 98 99 mutex_exit(&child->db_mtx); 100 } 101 102 bzero(dn->dn_phys->dn_blkptr, 103 sizeof (blkptr_t) * dn->dn_phys->dn_nblkptr); 104 105 dbuf_remove_ref(db, FTAG); 106 } 107 108 static void 109 free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) 110 { 111 objset_impl_t *os = dn->dn_objset; 112 uint64_t bytesfreed = 0; 113 int i; 114 115 dprintf("os=%p obj=%llx num=%d\n", os, dn->dn_object, num); 116 117 for (i = 0; i < num; i++, bp++) { 118 if (BP_IS_HOLE(bp)) 119 continue; 120 121 bytesfreed += BP_GET_ASIZE(bp); 122 ASSERT3U(bytesfreed >> DEV_BSHIFT, <=, dn->dn_phys->dn_secphys); 123 dsl_dataset_block_kill(os->os_dsl_dataset, bp, tx); 124 } 125 dnode_diduse_space(dn, -bytesfreed); 126 } 127 128 #ifdef ZFS_DEBUG 129 static void 130 free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) 131 { 132 int off, num; 133 int i, err, epbs; 134 uint64_t txg = tx->tx_txg; 135 136 epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 137 off = start - (db->db_blkid * 1<<epbs); 138 num = end - start + 1; 139 140 ASSERT3U(off, >=, 0); 141 ASSERT3U(num, >=, 0); 142 ASSERT3U(db->db_level, >, 0); 143 ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift); 144 ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); 145 ASSERT(db->db_blkptr != NULL); 146 147 for (i = off; i < off+num; i++) { 148 uint64_t *buf; 149 int j; 150 dmu_buf_impl_t *child; 151 152 ASSERT(db->db_level == 1); 153 154 rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER); 155 err = dbuf_hold_impl(db->db_dnode, db->db_level-1, 156 (db->db_blkid << epbs) + i, TRUE, FTAG, &child); 157 rw_exit(&db->db_dnode->dn_struct_rwlock); 158 if (err == ENOENT) 159 continue; 160 ASSERT(err == 0); 161 ASSERT(child->db_level == 0); 162 ASSERT(!list_link_active(&child->db_dirty_node[txg&TXG_MASK])); 163 164 /* db_data_old better be zeroed */ 165 if (child->db_d.db_data_old[txg & TXG_MASK]) { 166 buf = (child->db_d.db_data_old[txg & TXG_MASK])->b_data; 167 for (j = 0; j < child->db.db_size >> 3; j++) { 168 if (buf[j] != 0) { 169 panic("freed data not zero: " 170 "child=%p i=%d off=%d num=%d\n", 171 child, i, off, num); 172 } 173 } 174 } 175 176 /* 177 * db_data better be zeroed unless it's dirty in a 178 * future txg. 179 */ 180 mutex_enter(&child->db_mtx); 181 buf = child->db.db_data; 182 if (buf != NULL && child->db_state != DB_FILL && 183 !list_link_active(&child->db_dirty_node 184 [(txg+1) & TXG_MASK]) && 185 !list_link_active(&child->db_dirty_node 186 [(txg+2) & TXG_MASK])) { 187 for (j = 0; j < child->db.db_size >> 3; j++) { 188 if (buf[j] != 0) { 189 panic("freed data not zero: " 190 "child=%p i=%d off=%d num=%d\n", 191 child, i, off, num); 192 } 193 } 194 } 195 mutex_exit(&child->db_mtx); 196 197 dbuf_remove_ref(child, FTAG); 198 } 199 } 200 #endif 201 202 static int 203 free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc, 204 dmu_tx_t *tx) 205 { 206 dnode_t *dn = db->db_dnode; 207 blkptr_t *bp; 208 dmu_buf_impl_t *subdb; 209 uint64_t start, end, dbstart, dbend, i; 210 int epbs, shift, err; 211 int txgoff = tx->tx_txg & TXG_MASK; 212 int all = TRUE; 213 214 dbuf_read(db); 215 arc_release(db->db_buf, db); 216 bp = (blkptr_t *)db->db.db_data; 217 218 epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 219 shift = (db->db_level - 1) * epbs; 220 dbstart = db->db_blkid << epbs; 221 start = blkid >> shift; 222 if (dbstart < start) { 223 bp += start - dbstart; 224 all = FALSE; 225 } else { 226 start = dbstart; 227 } 228 dbend = ((db->db_blkid + 1) << epbs) - 1; 229 end = (blkid + nblks - 1) >> shift; 230 if (dbend <= end) 231 end = dbend; 232 else if (all) 233 all = trunc; 234 ASSERT3U(start, <=, end); 235 236 if (db->db_level == 1) { 237 FREE_VERIFY(db, start, end, tx); 238 free_blocks(dn, bp, end-start+1, tx); 239 ASSERT(all || list_link_active(&db->db_dirty_node[txgoff])); 240 return (all); 241 } 242 243 for (i = start; i <= end; i++, bp++) { 244 if (BP_IS_HOLE(bp)) 245 continue; 246 rw_enter(&dn->dn_struct_rwlock, RW_READER); 247 err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb); 248 ASSERT3U(err, ==, 0); 249 rw_exit(&dn->dn_struct_rwlock); 250 251 if (free_children(subdb, blkid, nblks, trunc, tx)) { 252 ASSERT3P(subdb->db_blkptr, ==, bp); 253 free_blocks(dn, bp, 1, tx); 254 } else { 255 all = FALSE; 256 } 257 dbuf_remove_ref(subdb, FTAG); 258 } 259 #ifdef ZFS_DEBUG 260 bp -= (end-start)+1; 261 for (i = start; i <= end; i++, bp++) { 262 if (i == start && blkid != 0) 263 continue; 264 else if (i == end && !trunc) 265 continue; 266 ASSERT3U(bp->blk_birth, ==, 0); 267 } 268 #endif 269 ASSERT(all || list_link_active(&db->db_dirty_node[txgoff])); 270 return (all); 271 } 272 273 /* 274 * free_range: Traverse the indicated range of the provided file 275 * and "free" all the blocks contained there. 276 */ 277 static void 278 dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 279 { 280 blkptr_t *bp = dn->dn_phys->dn_blkptr; 281 dmu_buf_impl_t *db; 282 int trunc, start, end, shift, i, err; 283 int dnlevel = dn->dn_phys->dn_nlevels; 284 285 if (blkid > dn->dn_phys->dn_maxblkid) 286 return; 287 288 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 289 trunc = blkid + nblks > dn->dn_phys->dn_maxblkid; 290 if (trunc) 291 nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 292 293 /* There are no indirect blocks in the object */ 294 if (dnlevel == 1) { 295 if (blkid >= dn->dn_phys->dn_nblkptr) { 296 /* this range was never made persistent */ 297 return; 298 } 299 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 300 free_blocks(dn, bp + blkid, nblks, tx); 301 if (trunc) { 302 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 303 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 304 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 305 ASSERT(off < dn->dn_phys->dn_maxblkid || 306 dn->dn_phys->dn_maxblkid == 0 || 307 dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH); 308 } 309 return; 310 } 311 312 shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 313 start = blkid >> shift; 314 ASSERT(start < dn->dn_phys->dn_nblkptr); 315 end = (blkid + nblks - 1) >> shift; 316 bp += start; 317 for (i = start; i <= end; i++, bp++) { 318 if (BP_IS_HOLE(bp)) 319 continue; 320 rw_enter(&dn->dn_struct_rwlock, RW_READER); 321 err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db); 322 ASSERT3U(err, ==, 0); 323 rw_exit(&dn->dn_struct_rwlock); 324 325 if (free_children(db, blkid, nblks, trunc, tx)) { 326 ASSERT3P(db->db_blkptr, ==, bp); 327 free_blocks(dn, bp, 1, tx); 328 } 329 dbuf_remove_ref(db, FTAG); 330 } 331 if (trunc) { 332 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 333 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 334 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 335 ASSERT(off < dn->dn_phys->dn_maxblkid || 336 dn->dn_phys->dn_maxblkid == 0 || 337 dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH); 338 } 339 } 340 341 static int 342 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 343 { 344 dmu_buf_impl_t *db; 345 int txgoff = tx->tx_txg & TXG_MASK; 346 347 ASSERT(dmu_tx_is_syncing(tx)); 348 349 /* Undirty all buffers */ 350 while (db = list_head(&dn->dn_dirty_dbufs[txgoff])) { 351 mutex_enter(&db->db_mtx); 352 /* XXX - use dbuf_undirty()? */ 353 list_remove(&dn->dn_dirty_dbufs[txgoff], db); 354 if (db->db_level == 0) { 355 ASSERT3P(db->db_d.db_data_old[txgoff], ==, db->db_buf); 356 if (db->db_d.db_overridden_by[txgoff]) 357 dbuf_unoverride(db, tx->tx_txg); 358 db->db_d.db_data_old[txgoff] = NULL; 359 } 360 db->db_dirtycnt -= 1; 361 mutex_exit(&db->db_mtx); 362 dbuf_remove_ref(db, (void *)(uintptr_t)tx->tx_txg); 363 } 364 365 ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 366 367 /* Undirty next bits */ 368 dn->dn_next_nlevels[txgoff] = 0; 369 dn->dn_next_indblkshift[txgoff] = 0; 370 371 /* free up all the blocks in the file. */ 372 dbuf_free_range(dn, 0, -1, tx); 373 dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx); 374 ASSERT3U(dn->dn_phys->dn_secphys, ==, 0); 375 376 /* 377 * All dbufs should be gone, since all holds are gone... 378 */ 379 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 380 381 /* ASSERT(blkptrs are zero); */ 382 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 383 ASSERT(dn->dn_type != DMU_OT_NONE); 384 385 ASSERT(dn->dn_free_txg > 0); 386 if (dn->dn_allocated_txg != dn->dn_free_txg) 387 dbuf_will_dirty(dn->dn_dbuf, tx); 388 bzero(dn->dn_phys, sizeof (dnode_phys_t)); 389 390 mutex_enter(&dn->dn_mtx); 391 dn->dn_type = DMU_OT_NONE; 392 dn->dn_dirtyblksz[txgoff] = 0; 393 dn->dn_maxblkid = 0; 394 dn->dn_allocated_txg = 0; 395 mutex_exit(&dn->dn_mtx); 396 397 ASSERT(!IS_DNODE_DNODE(dn->dn_object)); 398 399 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 400 /* 401 * Now that we've released our hold, the dnode may 402 * be evicted, so we musn't access it. 403 */ 404 return (1); 405 } 406 407 /* 408 * Write out the dnode's dirty buffers at the specified level. 409 * This may create more dirty buffers at the next level up. 410 * 411 * NOTE: The dnode is kept in memory by being dirty. Once the 412 * dirty bit is cleared, it may be evicted. Beware of this! 413 */ 414 int 415 dnode_sync(dnode_t *dn, int level, zio_t *zio, dmu_tx_t *tx) 416 { 417 free_range_t *rp; 418 int txgoff = tx->tx_txg & TXG_MASK; 419 dnode_phys_t *dnp = dn->dn_phys; 420 421 /* ASSERT(dn->dn_objset->dd_snapshot == NULL); */ 422 ASSERT(dmu_tx_is_syncing(tx)); 423 ASSERT(IS_DNODE_DNODE(dn->dn_object) || 424 dn->dn_dirtyblksz[txgoff] > 0); 425 426 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 427 DNODE_VERIFY(dn); 428 /* 429 * Make sure the dbuf for the dn_phys is released before we modify it. 430 */ 431 if (dn->dn_dbuf) 432 arc_release(dn->dn_dbuf->db_buf, dn->dn_dbuf); 433 434 mutex_enter(&dn->dn_mtx); 435 if (dn->dn_allocated_txg == tx->tx_txg) { 436 /* The dnode is newly allocated or reallocated */ 437 if (dnp->dn_type == DMU_OT_NONE) { 438 /* this is a first alloc, not a realloc */ 439 /* XXX shouldn't the phys already be zeroed? */ 440 bzero(dnp, DNODE_CORE_SIZE); 441 dnp->dn_datablkszsec = dn->dn_datablkszsec; 442 dnp->dn_indblkshift = dn->dn_indblkshift; 443 dnp->dn_nlevels = 1; 444 } 445 446 if (dn->dn_nblkptr > dnp->dn_nblkptr) { 447 /* zero the new blkptrs we are gaining */ 448 bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 449 sizeof (blkptr_t) * 450 (dn->dn_nblkptr - dnp->dn_nblkptr)); 451 } 452 dnp->dn_type = dn->dn_type; 453 dnp->dn_bonustype = dn->dn_bonustype; 454 dnp->dn_bonuslen = dn->dn_bonuslen; 455 dnp->dn_nblkptr = dn->dn_nblkptr; 456 } 457 458 if (dn->dn_dirtyblksz[txgoff]) { 459 ASSERT(P2PHASE(dn->dn_dirtyblksz[txgoff], 460 SPA_MINBLOCKSIZE) == 0); 461 dnp->dn_datablkszsec = 462 dn->dn_dirtyblksz[txgoff] >> SPA_MINBLOCKSHIFT; 463 } 464 465 if (dn->dn_next_indblkshift[txgoff]) { 466 ASSERT(dnp->dn_nlevels == 1); 467 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 468 dn->dn_next_indblkshift[txgoff] = 0; 469 } 470 471 /* 472 * Just take the live (open-context) values for checksum and compress. 473 * Strictly speaking it's a future leak, but nothing bad happens if we 474 * start using the new checksum or compress algorithm a little early. 475 */ 476 dnp->dn_checksum = dn->dn_checksum; 477 dnp->dn_compress = dn->dn_compress; 478 479 mutex_exit(&dn->dn_mtx); 480 481 /* process all the "freed" ranges in the file */ 482 if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) { 483 for (rp = avl_last(&dn->dn_ranges[txgoff]); rp != NULL; 484 rp = AVL_PREV(&dn->dn_ranges[txgoff], rp)) 485 dnode_sync_free_range(dn, 486 rp->fr_blkid, rp->fr_nblks, tx); 487 } 488 mutex_enter(&dn->dn_mtx); 489 for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) { 490 free_range_t *last = rp; 491 rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp); 492 avl_remove(&dn->dn_ranges[txgoff], last); 493 kmem_free(last, sizeof (free_range_t)); 494 } 495 mutex_exit(&dn->dn_mtx); 496 497 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 498 ASSERT3U(level, ==, 0); 499 return (dnode_sync_free(dn, tx)); 500 } 501 502 if (dn->dn_next_nlevels[txgoff]) { 503 int new_lvl = dn->dn_next_nlevels[txgoff]; 504 505 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 506 while (new_lvl > dnp->dn_nlevels) 507 dnode_increase_indirection(dn, tx); 508 rw_exit(&dn->dn_struct_rwlock); 509 dn->dn_next_nlevels[txgoff] = 0; 510 } 511 512 if (level == dnp->dn_nlevels) { 513 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 514 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 515 516 /* we've already synced out all data and indirect blocks */ 517 /* there are no more dirty dbufs under this dnode */ 518 ASSERT3P(list_head(&dn->dn_dirty_dbufs[txgoff]), ==, NULL); 519 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= tx->tx_txg); 520 521 /* XXX this is expensive. remove once 6343073 is closed. */ 522 /* NB: the "off < maxblkid" is to catch overflow */ 523 /* 524 * NB: if blocksize is changing, we could get confused, 525 * so only bother if there are multiple blocks and thus 526 * it can't be changing. 527 */ 528 if (!(off < dn->dn_phys->dn_maxblkid || 529 dn->dn_phys->dn_maxblkid == 0 || 530 dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH)) 531 panic("data after EOF: off=%llu\n", (u_longlong_t)off); 532 533 dn->dn_dirtyblksz[txgoff] = 0; 534 535 536 if (!IS_DNODE_DNODE(dn->dn_object)) { 537 dbuf_will_dirty(dn->dn_dbuf, tx); 538 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 539 } 540 541 /* 542 * Now that we've dropped the reference, the dnode may 543 * be evicted, so we musn't access it. 544 */ 545 return (1); 546 } else { 547 dmu_buf_impl_t *db, *db_next; 548 list_t *list = &dn->dn_dirty_dbufs[txgoff]; 549 /* 550 * Iterate over the list, removing and sync'ing dbufs 551 * which are on the level we want, and leaving others. 552 */ 553 for (db = list_head(list); db; db = db_next) { 554 db_next = list_next(list, db); 555 if (db->db_level == level) { 556 list_remove(list, db); 557 dbuf_sync(db, zio, tx); 558 } 559 } 560 return (0); 561 } 562 } 563