FreeBSD ZFS
The Zettabyte File System

dnode_sync.c

Go to the documentation of this file.
00001 /*
00002  * CDDL HEADER START
00003  *
00004  * The contents of this file are subject to the terms of the
00005  * Common Development and Distribution License (the "License").
00006  * You may not use this file except in compliance with the License.
00007  *
00008  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00009  * or http://www.opensolaris.org/os/licensing.
00010  * See the License for the specific language governing permissions
00011  * and limitations under the License.
00012  *
00013  * When distributing Covered Code, include this CDDL HEADER in each
00014  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00015  * If applicable, add the following below this CDDL HEADER, with the
00016  * fields enclosed by brackets "[]" replaced with your own identifying
00017  * information: Portions Copyright [yyyy] [name of copyright owner]
00018  *
00019  * CDDL HEADER END
00020  */
00021 
00022 /*
00023  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
00024  * Copyright (c) 2012 by Delphix. All rights reserved.
00025  */
00026 
00027 #include <sys/zfs_context.h>
00028 #include <sys/dbuf.h>
00029 #include <sys/dnode.h>
00030 #include <sys/dmu.h>
00031 #include <sys/dmu_tx.h>
00032 #include <sys/dmu_objset.h>
00033 #include <sys/dsl_dataset.h>
00034 #include <sys/spa.h>
00035 
00036 static void
00037 dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
00038 {
00039         dmu_buf_impl_t *db;
00040         int txgoff = tx->tx_txg & TXG_MASK;
00041         int nblkptr = dn->dn_phys->dn_nblkptr;
00042         int old_toplvl = dn->dn_phys->dn_nlevels - 1;
00043         int new_level = dn->dn_next_nlevels[txgoff];
00044         int i;
00045 
00046         rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
00047 
00048         /* this dnode can't be paged out because it's dirty */
00049         ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
00050         ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
00051         ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
00052 
00053         db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
00054         ASSERT(db != NULL);
00055 
00056         dn->dn_phys->dn_nlevels = new_level;
00057         dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
00058             dn->dn_object, dn->dn_phys->dn_nlevels);
00059 
00060         /* check for existing blkptrs in the dnode */
00061         for (i = 0; i < nblkptr; i++)
00062                 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
00063                         break;
00064         if (i != nblkptr) {
00065                 /* transfer dnode's block pointers to new indirect block */
00066                 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
00067                 ASSERT(db->db.db_data);
00068                 ASSERT(arc_released(db->db_buf));
00069                 ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
00070                 bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
00071                     sizeof (blkptr_t) * nblkptr);
00072                 arc_buf_freeze(db->db_buf);
00073         }
00074 
00075         /* set dbuf's parent pointers to new indirect buf */
00076         for (i = 0; i < nblkptr; i++) {
00077                 dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
00078 
00079                 if (child == NULL)
00080                         continue;
00081 #ifdef  DEBUG
00082                 DB_DNODE_ENTER(child);
00083                 ASSERT3P(DB_DNODE(child), ==, dn);
00084                 DB_DNODE_EXIT(child);
00085 #endif  /* DEBUG */
00086                 if (child->db_parent && child->db_parent != dn->dn_dbuf) {
00087                         ASSERT(child->db_parent->db_level == db->db_level);
00088                         ASSERT(child->db_blkptr !=
00089                             &dn->dn_phys->dn_blkptr[child->db_blkid]);
00090                         mutex_exit(&child->db_mtx);
00091                         continue;
00092                 }
00093                 ASSERT(child->db_parent == NULL ||
00094                     child->db_parent == dn->dn_dbuf);
00095 
00096                 child->db_parent = db;
00097                 dbuf_add_ref(db, child);
00098                 if (db->db.db_data)
00099                         child->db_blkptr = (blkptr_t *)db->db.db_data + i;
00100                 else
00101                         child->db_blkptr = NULL;
00102                 dprintf_dbuf_bp(child, child->db_blkptr,
00103                     "changed db_blkptr to new indirect %s", "");
00104 
00105                 mutex_exit(&child->db_mtx);
00106         }
00107 
00108         bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
00109 
00110         dbuf_rele(db, FTAG);
00111 
00112         rw_exit(&dn->dn_struct_rwlock);
00113 }
00114 
00115 static int
00116 free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
00117 {
00118         dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
00119         uint64_t bytesfreed = 0;
00120         int i, blocks_freed = 0;
00121 
00122         dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
00123 
00124         for (i = 0; i < num; i++, bp++) {
00125                 if (BP_IS_HOLE(bp))
00126                         continue;
00127 
00128                 bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
00129                 ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
00130                 bzero(bp, sizeof (blkptr_t));
00131                 blocks_freed += 1;
00132         }
00133         dnode_diduse_space(dn, -bytesfreed);
00134         return (blocks_freed);
00135 }
00136 
00137 #ifdef ZFS_DEBUG
00138 static void
00139 free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
00140 {
00141         int off, num;
00142         int i, err, epbs;
00143         uint64_t txg = tx->tx_txg;
00144         dnode_t *dn;
00145 
00146         DB_DNODE_ENTER(db);
00147         dn = DB_DNODE(db);
00148         epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
00149         off = start - (db->db_blkid * 1<<epbs);
00150         num = end - start + 1;
00151 
00152         ASSERT3U(off, >=, 0);
00153         ASSERT3U(num, >=, 0);
00154         ASSERT3U(db->db_level, >, 0);
00155         ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
00156         ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
00157         ASSERT(db->db_blkptr != NULL);
00158 
00159         for (i = off; i < off+num; i++) {
00160                 uint64_t *buf;
00161                 dmu_buf_impl_t *child;
00162                 dbuf_dirty_record_t *dr;
00163                 int j;
00164 
00165                 ASSERT(db->db_level == 1);
00166 
00167                 rw_enter(&dn->dn_struct_rwlock, RW_READER);
00168                 err = dbuf_hold_impl(dn, db->db_level-1,
00169                     (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
00170                 rw_exit(&dn->dn_struct_rwlock);
00171                 if (err == ENOENT)
00172                         continue;
00173                 ASSERT(err == 0);
00174                 ASSERT(child->db_level == 0);
00175                 dr = child->db_last_dirty;
00176                 while (dr && dr->dr_txg > txg)
00177                         dr = dr->dr_next;
00178                 ASSERT(dr == NULL || dr->dr_txg == txg);
00179 
00180                 /* data_old better be zeroed */
00181                 if (dr) {
00182                         buf = dr->dt.dl.dr_data->b_data;
00183                         for (j = 0; j < child->db.db_size >> 3; j++) {
00184                                 if (buf[j] != 0) {
00185                                         panic("freed data not zero: "
00186                                             "child=%p i=%d off=%d num=%d\n",
00187                                             (void *)child, i, off, num);
00188                                 }
00189                         }
00190                 }
00191 
00192                 /*
00193                  * db_data better be zeroed unless it's dirty in a
00194                  * future txg.
00195                  */
00196                 mutex_enter(&child->db_mtx);
00197                 buf = child->db.db_data;
00198                 if (buf != NULL && child->db_state != DB_FILL &&
00199                     child->db_last_dirty == NULL) {
00200                         for (j = 0; j < child->db.db_size >> 3; j++) {
00201                                 if (buf[j] != 0) {
00202                                         panic("freed data not zero: "
00203                                             "child=%p i=%d off=%d num=%d\n",
00204                                             (void *)child, i, off, num);
00205                                 }
00206                         }
00207                 }
00208                 mutex_exit(&child->db_mtx);
00209 
00210                 dbuf_rele(child, FTAG);
00211         }
00212         DB_DNODE_EXIT(db);
00213 }
00214 #endif
00215 
00216 #define ALL -1
00217 
00218 static int
00219 free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
00220     dmu_tx_t *tx)
00221 {
00222         dnode_t *dn;
00223         blkptr_t *bp;
00224         dmu_buf_impl_t *subdb;
00225         uint64_t start, end, dbstart, dbend, i;
00226         int epbs, shift, err;
00227         int all = TRUE;
00228         int blocks_freed = 0;
00229 
00230         /*
00231          * There is a small possibility that this block will not be cached:
00232          *   1 - if level > 1 and there are no children with level <= 1
00233          *   2 - if we didn't get a dirty hold (because this block had just
00234          *       finished being written -- and so had no holds), and then this
00235          *       block got evicted before we got here.
00236          */
00237         if (db->db_state != DB_CACHED)
00238                 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
00239 
00240         dbuf_release_bp(db);
00241         bp = (blkptr_t *)db->db.db_data;
00242 
00243         DB_DNODE_ENTER(db);
00244         dn = DB_DNODE(db);
00245         epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
00246         shift = (db->db_level - 1) * epbs;
00247         dbstart = db->db_blkid << epbs;
00248         start = blkid >> shift;
00249         if (dbstart < start) {
00250                 bp += start - dbstart;
00251                 all = FALSE;
00252         } else {
00253                 start = dbstart;
00254         }
00255         dbend = ((db->db_blkid + 1) << epbs) - 1;
00256         end = (blkid + nblks - 1) >> shift;
00257         if (dbend <= end)
00258                 end = dbend;
00259         else if (all)
00260                 all = trunc;
00261         ASSERT3U(start, <=, end);
00262 
00263         if (db->db_level == 1) {
00264                 FREE_VERIFY(db, start, end, tx);
00265                 blocks_freed = free_blocks(dn, bp, end-start+1, tx);
00266                 arc_buf_freeze(db->db_buf);
00267                 ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
00268                 DB_DNODE_EXIT(db);
00269                 return (all ? ALL : blocks_freed);
00270         }
00271 
00272         for (i = start; i <= end; i++, bp++) {
00273                 if (BP_IS_HOLE(bp))
00274                         continue;
00275                 rw_enter(&dn->dn_struct_rwlock, RW_READER);
00276                 err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
00277                 ASSERT0(err);
00278                 rw_exit(&dn->dn_struct_rwlock);
00279 
00280                 if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) {
00281                         ASSERT3P(subdb->db_blkptr, ==, bp);
00282                         blocks_freed += free_blocks(dn, bp, 1, tx);
00283                 } else {
00284                         all = FALSE;
00285                 }
00286                 dbuf_rele(subdb, FTAG);
00287         }
00288         DB_DNODE_EXIT(db);
00289         arc_buf_freeze(db->db_buf);
00290 #ifdef ZFS_DEBUG
00291         bp -= (end-start)+1;
00292         for (i = start; i <= end; i++, bp++) {
00293                 if (i == start && blkid != 0)
00294                         continue;
00295                 else if (i == end && !trunc)
00296                         continue;
00297                 ASSERT0(bp->blk_birth);
00298         }
00299 #endif
00300         ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
00301         return (all ? ALL : blocks_freed);
00302 }
00303 
00308 static void
00309 dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
00310 {
00311         blkptr_t *bp = dn->dn_phys->dn_blkptr;
00312         dmu_buf_impl_t *db;
00313         int trunc, start, end, shift, i, err;
00314         int dnlevel = dn->dn_phys->dn_nlevels;
00315 
00316         if (blkid > dn->dn_phys->dn_maxblkid)
00317                 return;
00318 
00319         ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
00320         trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
00321         if (trunc)
00322                 nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
00323 
00324         /* There are no indirect blocks in the object */
00325         if (dnlevel == 1) {
00326                 if (blkid >= dn->dn_phys->dn_nblkptr) {
00327                         /* this range was never made persistent */
00328                         return;
00329                 }
00330                 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
00331                 (void) free_blocks(dn, bp + blkid, nblks, tx);
00332                 if (trunc) {
00333                         uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
00334                             (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
00335                         dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
00336                         ASSERT(off < dn->dn_phys->dn_maxblkid ||
00337                             dn->dn_phys->dn_maxblkid == 0 ||
00338                             dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
00339                 }
00340                 return;
00341         }
00342 
00343         shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
00344         start = blkid >> shift;
00345         ASSERT(start < dn->dn_phys->dn_nblkptr);
00346         end = (blkid + nblks - 1) >> shift;
00347         bp += start;
00348         for (i = start; i <= end; i++, bp++) {
00349                 if (BP_IS_HOLE(bp))
00350                         continue;
00351                 rw_enter(&dn->dn_struct_rwlock, RW_READER);
00352                 err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
00353                 ASSERT0(err);
00354                 rw_exit(&dn->dn_struct_rwlock);
00355 
00356                 if (free_children(db, blkid, nblks, trunc, tx) == ALL) {
00357                         ASSERT3P(db->db_blkptr, ==, bp);
00358                         (void) free_blocks(dn, bp, 1, tx);
00359                 }
00360                 dbuf_rele(db, FTAG);
00361         }
00362         if (trunc) {
00363                 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
00364                     (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
00365                 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
00366                 ASSERT(off < dn->dn_phys->dn_maxblkid ||
00367                     dn->dn_phys->dn_maxblkid == 0 ||
00368                     dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
00369         }
00370 }
00371 
00375 void
00376 dnode_evict_dbufs(dnode_t *dn)
00377 {
00378         int progress;
00379         int pass = 0;
00380 
00381         do {
00382                 dmu_buf_impl_t *db, marker;
00383                 int evicting = FALSE;
00384 
00385                 progress = FALSE;
00386                 mutex_enter(&dn->dn_dbufs_mtx);
00387                 list_insert_tail(&dn->dn_dbufs, &marker);
00388                 db = list_head(&dn->dn_dbufs);
00389                 for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
00390                         list_remove(&dn->dn_dbufs, db);
00391                         list_insert_tail(&dn->dn_dbufs, db);
00392 #ifdef  DEBUG
00393                         DB_DNODE_ENTER(db);
00394                         ASSERT3P(DB_DNODE(db), ==, dn);
00395                         DB_DNODE_EXIT(db);
00396 #endif  /* DEBUG */
00397 
00398                         mutex_enter(&db->db_mtx);
00399                         if (db->db_state == DB_EVICTING) {
00400                                 progress = TRUE;
00401                                 evicting = TRUE;
00402                                 mutex_exit(&db->db_mtx);
00403                         } else if (refcount_is_zero(&db->db_holds)) {
00404                                 progress = TRUE;
00405                                 dbuf_clear(db); /* exits db_mtx for us */
00406                         } else {
00407                                 mutex_exit(&db->db_mtx);
00408                         }
00409 
00410                 }
00411                 list_remove(&dn->dn_dbufs, &marker);
00412                 /*
00413                  * NB: we need to drop dn_dbufs_mtx between passes so
00414                  * that any DB_EVICTING dbufs can make progress.
00415                  * Ideally, we would have some cv we could wait on, but
00416                  * since we don't, just wait a bit to give the other
00417                  * thread a chance to run.
00418                  */
00419                 mutex_exit(&dn->dn_dbufs_mtx);
00420                 if (evicting)
00421                         delay(1);
00422                 pass++;
00423                 ASSERT(pass < 100); /* sanity check */
00424         } while (progress);
00425 
00426         rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
00427         if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
00428                 mutex_enter(&dn->dn_bonus->db_mtx);
00429                 dbuf_evict(dn->dn_bonus);
00430                 dn->dn_bonus = NULL;
00431         }
00432         rw_exit(&dn->dn_struct_rwlock);
00433 }
00434 
00435 static void
00436 dnode_undirty_dbufs(list_t *list)
00437 {
00438         dbuf_dirty_record_t *dr;
00439 
00440         while (dr = list_head(list)) {
00441                 dmu_buf_impl_t *db = dr->dr_dbuf;
00442                 uint64_t txg = dr->dr_txg;
00443 
00444                 if (db->db_level != 0)
00445                         dnode_undirty_dbufs(&dr->dt.di.dr_children);
00446 
00447                 mutex_enter(&db->db_mtx);
00448                 /* XXX - use dbuf_undirty()? */
00449                 list_remove(list, dr);
00450                 ASSERT(db->db_last_dirty == dr);
00451                 db->db_last_dirty = NULL;
00452                 db->db_dirtycnt -= 1;
00453                 if (db->db_level == 0) {
00454                         ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
00455                             dr->dt.dl.dr_data == db->db_buf);
00456                         dbuf_unoverride(dr);
00457                 } else {
00458                         list_destroy(&dr->dt.di.dr_children);
00459                         mutex_destroy(&dr->dt.di.dr_mtx);
00460                 }
00461                 kmem_free(dr, sizeof (dbuf_dirty_record_t));
00462                 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
00463         }
00464 }
00465 
00466 static void
00467 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
00468 {
00469         int txgoff = tx->tx_txg & TXG_MASK;
00470 
00471         ASSERT(dmu_tx_is_syncing(tx));
00472 
00473         /*
00474          * Our contents should have been freed in dnode_sync() by the
00475          * free range record inserted by the caller of dnode_free().
00476          */
00477         ASSERT0(DN_USED_BYTES(dn->dn_phys));
00478         ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
00479 
00480         dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
00481         dnode_evict_dbufs(dn);
00482         ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
00483 
00484         /*
00485          * XXX - It would be nice to assert this, but we may still
00486          * have residual holds from async evictions from the arc...
00487          *
00488          * zfs_obj_to_path() also depends on this being
00489          * commented out.
00490          *
00491          * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
00492          */
00493 
00494         /* Undirty next bits */
00495         dn->dn_next_nlevels[txgoff] = 0;
00496         dn->dn_next_indblkshift[txgoff] = 0;
00497         dn->dn_next_blksz[txgoff] = 0;
00498 
00499         /* ASSERT(blkptrs are zero); */
00500         ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
00501         ASSERT(dn->dn_type != DMU_OT_NONE);
00502 
00503         ASSERT(dn->dn_free_txg > 0);
00504         if (dn->dn_allocated_txg != dn->dn_free_txg)
00505                 dbuf_will_dirty(dn->dn_dbuf, tx);
00506         bzero(dn->dn_phys, sizeof (dnode_phys_t));
00507 
00508         mutex_enter(&dn->dn_mtx);
00509         dn->dn_type = DMU_OT_NONE;
00510         dn->dn_maxblkid = 0;
00511         dn->dn_allocated_txg = 0;
00512         dn->dn_free_txg = 0;
00513         dn->dn_have_spill = B_FALSE;
00514         mutex_exit(&dn->dn_mtx);
00515 
00516         ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
00517 
00518         dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
00519         /*
00520          * Now that we've released our hold, the dnode may
00521          * be evicted, so we musn't access it.
00522          */
00523 }
00524 
00528 void
00529 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
00530 {
00531         free_range_t *rp;
00532         dnode_phys_t *dnp = dn->dn_phys;
00533         int txgoff = tx->tx_txg & TXG_MASK;
00534         list_t *list = &dn->dn_dirty_records[txgoff];
00535         static const dnode_phys_t zerodn = { 0 };
00536         boolean_t kill_spill = B_FALSE;
00537 
00538         ASSERT(dmu_tx_is_syncing(tx));
00539         ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
00540         ASSERT(dnp->dn_type != DMU_OT_NONE ||
00541             bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
00542         DNODE_VERIFY(dn);
00543 
00544         ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
00545 
00546         if (dmu_objset_userused_enabled(dn->dn_objset) &&
00547             !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
00548                 mutex_enter(&dn->dn_mtx);
00549                 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
00550                 dn->dn_oldflags = dn->dn_phys->dn_flags;
00551                 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
00552                 mutex_exit(&dn->dn_mtx);
00553                 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
00554         } else {
00555                 /* Once we account for it, we should always account for it. */
00556                 ASSERT(!(dn->dn_phys->dn_flags &
00557                     DNODE_FLAG_USERUSED_ACCOUNTED));
00558         }
00559 
00560         mutex_enter(&dn->dn_mtx);
00561         if (dn->dn_allocated_txg == tx->tx_txg) {
00562                 /* The dnode is newly allocated or reallocated */
00563                 if (dnp->dn_type == DMU_OT_NONE) {
00564                         /* this is a first alloc, not a realloc */
00565                         dnp->dn_nlevels = 1;
00566                         dnp->dn_nblkptr = dn->dn_nblkptr;
00567                 }
00568 
00569                 dnp->dn_type = dn->dn_type;
00570                 dnp->dn_bonustype = dn->dn_bonustype;
00571                 dnp->dn_bonuslen = dn->dn_bonuslen;
00572         }
00573 
00574         ASSERT(dnp->dn_nlevels > 1 ||
00575             BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
00576             BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
00577             dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
00578 
00579         if (dn->dn_next_blksz[txgoff]) {
00580                 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
00581                     SPA_MINBLOCKSIZE) == 0);
00582                 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
00583                     dn->dn_maxblkid == 0 || list_head(list) != NULL ||
00584                     avl_last(&dn->dn_ranges[txgoff]) ||
00585                     dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
00586                     dnp->dn_datablkszsec);
00587                 dnp->dn_datablkszsec =
00588                     dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
00589                 dn->dn_next_blksz[txgoff] = 0;
00590         }
00591 
00592         if (dn->dn_next_bonuslen[txgoff]) {
00593                 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
00594                         dnp->dn_bonuslen = 0;
00595                 else
00596                         dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
00597                 ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
00598                 dn->dn_next_bonuslen[txgoff] = 0;
00599         }
00600 
00601         if (dn->dn_next_bonustype[txgoff]) {
00602                 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
00603                 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
00604                 dn->dn_next_bonustype[txgoff] = 0;
00605         }
00606 
00607         /*
00608          * We will either remove a spill block when a file is being removed
00609          * or we have been asked to remove it.
00610          */
00611         if (dn->dn_rm_spillblk[txgoff] ||
00612             ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) &&
00613             dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg)) {
00614                 if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
00615                         kill_spill = B_TRUE;
00616                 dn->dn_rm_spillblk[txgoff] = 0;
00617         }
00618 
00619         if (dn->dn_next_indblkshift[txgoff]) {
00620                 ASSERT(dnp->dn_nlevels == 1);
00621                 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
00622                 dn->dn_next_indblkshift[txgoff] = 0;
00623         }
00624 
00625         /*
00626          * Just take the live (open-context) values for checksum and compress.
00627          * Strictly speaking it's a future leak, but nothing bad happens if we
00628          * start using the new checksum or compress algorithm a little early.
00629          */
00630         dnp->dn_checksum = dn->dn_checksum;
00631         dnp->dn_compress = dn->dn_compress;
00632 
00633         mutex_exit(&dn->dn_mtx);
00634 
00635         if (kill_spill) {
00636                 (void) free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
00637                 mutex_enter(&dn->dn_mtx);
00638                 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
00639                 mutex_exit(&dn->dn_mtx);
00640         }
00641 
00642         /* process all the "freed" ranges in the file */
00643         while (rp = avl_last(&dn->dn_ranges[txgoff])) {
00644                 dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
00645                 /* grab the mutex so we don't race with dnode_block_freed() */
00646                 mutex_enter(&dn->dn_mtx);
00647                 avl_remove(&dn->dn_ranges[txgoff], rp);
00648                 mutex_exit(&dn->dn_mtx);
00649                 kmem_free(rp, sizeof (free_range_t));
00650         }
00651 
00652         if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
00653                 dnode_sync_free(dn, tx);
00654                 return;
00655         }
00656 
00657         if (dn->dn_next_nblkptr[txgoff]) {
00658                 /* this should only happen on a realloc */
00659                 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
00660                 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
00661                         /* zero the new blkptrs we are gaining */
00662                         bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
00663                             sizeof (blkptr_t) *
00664                             (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
00665 #ifdef ZFS_DEBUG
00666                 } else {
00667                         int i;
00668                         ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
00669                         /* the blkptrs we are losing better be unallocated */
00670                         for (i = dn->dn_next_nblkptr[txgoff];
00671                             i < dnp->dn_nblkptr; i++)
00672                                 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
00673 #endif
00674                 }
00675                 mutex_enter(&dn->dn_mtx);
00676                 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
00677                 dn->dn_next_nblkptr[txgoff] = 0;
00678                 mutex_exit(&dn->dn_mtx);
00679         }
00680 
00681         if (dn->dn_next_nlevels[txgoff]) {
00682                 dnode_increase_indirection(dn, tx);
00683                 dn->dn_next_nlevels[txgoff] = 0;
00684         }
00685 
00686         dbuf_sync_list(list, tx);
00687 
00688         if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
00689                 ASSERT3P(list_head(list), ==, NULL);
00690                 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
00691         }
00692 
00693         /*
00694          * Although we have dropped our reference to the dnode, it
00695          * can't be evicted until its written, and we haven't yet
00696          * initiated the IO for the dnode's dbuf.
00697          */
00698 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines