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