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 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 txg_index = 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[txg_index])); 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 } 255 dbuf_remove_ref(subdb, FTAG); 256 } 257 #ifdef ZFS_DEBUG 258 bp -= (end-start)+1; 259 for (i = start; i <= end; i++, bp++) { 260 if (i == start && blkid != 0) 261 continue; 262 else if (i == end && !trunc) 263 continue; 264 ASSERT3U(bp->blk_birth, ==, 0); 265 } 266 #endif 267 ASSERT(all || list_link_active(&db->db_dirty_node[txg_index])); 268 return (all); 269 } 270 271 /* 272 * free_range: Traverse the indicated range of the provided file 273 * and "free" all the blocks contained there. 274 */ 275 static void 276 dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 277 { 278 blkptr_t *bp = dn->dn_phys->dn_blkptr; 279 dmu_buf_impl_t *db; 280 int trunc, start, end, shift, i, err; 281 int dnlevel = dn->dn_phys->dn_nlevels; 282 283 if (blkid > dn->dn_phys->dn_maxblkid) 284 return; 285 286 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); 287 trunc = blkid + nblks > dn->dn_phys->dn_maxblkid; 288 if (trunc) 289 nblks = dn->dn_phys->dn_maxblkid - blkid + 1; 290 291 /* There are no indirect blocks in the object */ 292 if (dnlevel == 1) { 293 if (blkid >= dn->dn_phys->dn_nblkptr) { 294 /* this range was never made persistent */ 295 return; 296 } 297 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); 298 free_blocks(dn, bp + blkid, nblks, tx); 299 if (trunc) { 300 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 301 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 302 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 303 ASSERT(off < dn->dn_phys->dn_maxblkid || 304 dn->dn_phys->dn_maxblkid == 0 || 305 dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH); 306 } 307 return; 308 } 309 310 shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); 311 start = blkid >> shift; 312 ASSERT(start < dn->dn_phys->dn_nblkptr); 313 end = (blkid + nblks - 1) >> shift; 314 bp += start; 315 for (i = start; i <= end; i++, bp++) { 316 if (BP_IS_HOLE(bp)) 317 continue; 318 rw_enter(&dn->dn_struct_rwlock, RW_READER); 319 err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db); 320 ASSERT3U(err, ==, 0); 321 rw_exit(&dn->dn_struct_rwlock); 322 323 if (free_children(db, blkid, nblks, trunc, tx)) { 324 ASSERT3P(db->db_blkptr, ==, bp); 325 free_blocks(dn, bp, 1, tx); 326 } 327 dbuf_remove_ref(db, FTAG); 328 } 329 if (trunc) { 330 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 331 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 332 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0); 333 ASSERT(off < dn->dn_phys->dn_maxblkid || 334 dn->dn_phys->dn_maxblkid == 0 || 335 dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH); 336 } 337 } 338 339 static int 340 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) 341 { 342 dmu_buf_impl_t *db; 343 int txgoff = tx->tx_txg & TXG_MASK; 344 345 ASSERT(dmu_tx_is_syncing(tx)); 346 347 /* Undirty all buffers */ 348 while (db = list_head(&dn->dn_dirty_dbufs[txgoff])) { 349 mutex_enter(&db->db_mtx); 350 /* XXX - use dbuf_undirty()? */ 351 list_remove(&dn->dn_dirty_dbufs[txgoff], db); 352 if (db->db_level == 0) { 353 ASSERT3P(db->db_d.db_data_old[txgoff], ==, db->db_buf); 354 if (db->db_d.db_overridden_by[txgoff]) 355 dbuf_unoverride(db, tx->tx_txg); 356 db->db_d.db_data_old[txgoff] = NULL; 357 } 358 db->db_dirtycnt -= 1; 359 mutex_exit(&db->db_mtx); 360 dbuf_remove_ref(db, (void *)(uintptr_t)tx->tx_txg); 361 } 362 363 ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); 364 365 /* Undirty next bits */ 366 dn->dn_next_nlevels[txgoff] = 0; 367 dn->dn_next_indblkshift[txgoff] = 0; 368 369 /* free up all the blocks in the file. */ 370 dbuf_free_range(dn, 0, -1, tx); 371 dnode_sync_free_range(dn, 0, dn->dn_phys->dn_maxblkid+1, tx); 372 ASSERT3U(dn->dn_phys->dn_secphys, ==, 0); 373 374 /* 375 * All dbufs should be gone, since all holds are gone... 376 */ 377 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 378 379 /* ASSERT(blkptrs are zero); */ 380 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); 381 ASSERT(dn->dn_type != DMU_OT_NONE); 382 383 ASSERT(dn->dn_free_txg > 0); 384 if (dn->dn_allocated_txg != dn->dn_free_txg) 385 dbuf_will_dirty(dn->dn_dbuf, tx); 386 bzero(dn->dn_phys, sizeof (dnode_phys_t)); 387 388 mutex_enter(&dn->dn_mtx); 389 dn->dn_type = DMU_OT_NONE; 390 dn->dn_dirtyblksz[txgoff] = 0; 391 dn->dn_maxblkid = 0; 392 dn->dn_allocated_txg = 0; 393 mutex_exit(&dn->dn_mtx); 394 395 ASSERT(!IS_DNODE_DNODE(dn->dn_object)); 396 397 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 398 /* 399 * Now that we've released our hold, the dnode may 400 * be evicted, so we musn't access it. 401 */ 402 return (1); 403 } 404 405 /* 406 * Write out the dnode's dirty buffers at the specified level. 407 * This may create more dirty buffers at the next level up. 408 * 409 * NOTE: The dnode is kept in memory by being dirty. Once the 410 * dirty bit is cleared, it may be evicted. Beware of this! 411 */ 412 int 413 dnode_sync(dnode_t *dn, int level, zio_t *zio, dmu_tx_t *tx) 414 { 415 free_range_t *rp; 416 int txgoff = tx->tx_txg & TXG_MASK; 417 dnode_phys_t *dnp = dn->dn_phys; 418 419 /* ASSERT(dn->dn_objset->dd_snapshot == NULL); */ 420 ASSERT(dmu_tx_is_syncing(tx)); 421 ASSERT(IS_DNODE_DNODE(dn->dn_object) || 422 dn->dn_dirtyblksz[txgoff] > 0); 423 424 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); 425 DNODE_VERIFY(dn); 426 /* 427 * Make sure the dbuf for the dn_phys is released before we modify it. 428 */ 429 if (dn->dn_dbuf) 430 arc_release(dn->dn_dbuf->db_buf, dn->dn_dbuf); 431 432 mutex_enter(&dn->dn_mtx); 433 if (dn->dn_allocated_txg == tx->tx_txg) { 434 /* The dnode is newly allocated or reallocated */ 435 if (dnp->dn_type == DMU_OT_NONE) { 436 /* this is a first alloc, not a realloc */ 437 /* XXX shouldn't the phys already be zeroed? */ 438 bzero(dnp, DNODE_CORE_SIZE); 439 dnp->dn_datablkszsec = dn->dn_datablkszsec; 440 dnp->dn_indblkshift = dn->dn_indblkshift; 441 dnp->dn_nlevels = 1; 442 } 443 444 if (dn->dn_nblkptr > dnp->dn_nblkptr) { 445 /* zero the new blkptrs we are gaining */ 446 bzero(dnp->dn_blkptr + dnp->dn_nblkptr, 447 sizeof (blkptr_t) * 448 (dn->dn_nblkptr - dnp->dn_nblkptr)); 449 } 450 dnp->dn_type = dn->dn_type; 451 dnp->dn_bonustype = dn->dn_bonustype; 452 dnp->dn_bonuslen = dn->dn_bonuslen; 453 dnp->dn_nblkptr = dn->dn_nblkptr; 454 } 455 456 if (dn->dn_dirtyblksz[txgoff]) { 457 ASSERT(P2PHASE(dn->dn_dirtyblksz[txgoff], 458 SPA_MINBLOCKSIZE) == 0); 459 dnp->dn_datablkszsec = 460 dn->dn_dirtyblksz[txgoff] >> SPA_MINBLOCKSHIFT; 461 } 462 463 if (dn->dn_next_indblkshift[txgoff]) { 464 ASSERT(dnp->dn_nlevels == 1); 465 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; 466 dn->dn_next_indblkshift[txgoff] = 0; 467 } 468 469 /* 470 * Just take the live (open-context) values for checksum and compress. 471 * Strictly speaking it's a future leak, but nothing bad happens if we 472 * start using the new checksum or compress algorithm a little early. 473 */ 474 dnp->dn_checksum = dn->dn_checksum; 475 dnp->dn_compress = dn->dn_compress; 476 477 mutex_exit(&dn->dn_mtx); 478 479 /* process all the "freed" ranges in the file */ 480 if (dn->dn_free_txg == 0 || dn->dn_free_txg > tx->tx_txg) { 481 for (rp = avl_first(&dn->dn_ranges[txgoff]); rp != NULL; 482 rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp)) 483 dnode_sync_free_range(dn, 484 rp->fr_blkid, rp->fr_nblks, tx); 485 } 486 mutex_enter(&dn->dn_mtx); 487 for (rp = avl_first(&dn->dn_ranges[txgoff]); rp; ) { 488 free_range_t *last = rp; 489 rp = AVL_NEXT(&dn->dn_ranges[txgoff], rp); 490 avl_remove(&dn->dn_ranges[txgoff], last); 491 kmem_free(last, sizeof (free_range_t)); 492 } 493 mutex_exit(&dn->dn_mtx); 494 495 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) { 496 ASSERT3U(level, ==, 0); 497 return (dnode_sync_free(dn, tx)); 498 } 499 500 if (dn->dn_next_nlevels[txgoff]) { 501 int new_lvl = dn->dn_next_nlevels[txgoff]; 502 503 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 504 while (new_lvl > dnp->dn_nlevels) 505 dnode_increase_indirection(dn, tx); 506 rw_exit(&dn->dn_struct_rwlock); 507 dn->dn_next_nlevels[txgoff] = 0; 508 } 509 510 if (level == dnp->dn_nlevels) { 511 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) * 512 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT); 513 514 /* we've already synced out all data and indirect blocks */ 515 /* there are no more dirty dbufs under this dnode */ 516 ASSERT3P(list_head(&dn->dn_dirty_dbufs[txgoff]), ==, NULL); 517 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= tx->tx_txg); 518 519 /* XXX this is expensive. remove once 6343073 is closed. */ 520 /* NB: the "off < maxblkid" is to catch overflow */ 521 /* 522 * NB: if blocksize is changing, we could get confused, 523 * so only bother if there are multiple blocks and thus 524 * it can't be changing. 525 */ 526 ASSERT(off < dn->dn_phys->dn_maxblkid || 527 dn->dn_phys->dn_maxblkid == 0 || 528 dnode_next_offset(dn, FALSE, &off, 1, 1) == ESRCH); 529 530 dn->dn_dirtyblksz[txgoff] = 0; 531 532 533 if (!IS_DNODE_DNODE(dn->dn_object)) { 534 dbuf_will_dirty(dn->dn_dbuf, tx); 535 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); 536 } 537 538 /* 539 * Now that we've dropped the reference, the dnode may 540 * be evicted, so we musn't access it. 541 */ 542 return (1); 543 } else { 544 dmu_buf_impl_t *db, *db_next; 545 list_t *list = &dn->dn_dirty_dbufs[txgoff]; 546 /* 547 * Iterate over the list, removing and sync'ing dbufs 548 * which are on the level we want, and leaving others. 549 */ 550 for (db = list_head(list); db; db = db_next) { 551 db_next = list_next(list, db); 552 if (db->db_level == level) { 553 list_remove(list, db); 554 dbuf_sync(db, zio, tx); 555 } 556 } 557 return (0); 558 } 559 } 560