120
121 /* ARGSUSED */
122 static void
123 dnode_dest(void *arg, void *unused)
124 {
125 int i;
126 dnode_t *dn = arg;
127
128 rw_destroy(&dn->dn_struct_rwlock);
129 mutex_destroy(&dn->dn_mtx);
130 mutex_destroy(&dn->dn_dbufs_mtx);
131 cv_destroy(&dn->dn_notxholds);
132 refcount_destroy(&dn->dn_holds);
133 refcount_destroy(&dn->dn_tx_holds);
134 ASSERT(!list_link_active(&dn->dn_link));
135
136 for (i = 0; i < TXG_SIZE; i++) {
137 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
138 avl_destroy(&dn->dn_ranges[i]);
139 list_destroy(&dn->dn_dirty_records[i]);
140 ASSERT3U(dn->dn_next_nblkptr[i], ==, 0);
141 ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
142 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
143 ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
144 ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
145 ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
146 ASSERT3U(dn->dn_next_blksz[i], ==, 0);
147 }
148
149 ASSERT3U(dn->dn_allocated_txg, ==, 0);
150 ASSERT3U(dn->dn_free_txg, ==, 0);
151 ASSERT3U(dn->dn_assigned_txg, ==, 0);
152 ASSERT3U(dn->dn_dirtyctx, ==, 0);
153 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
154 ASSERT3P(dn->dn_bonus, ==, NULL);
155 ASSERT(!dn->dn_have_spill);
156 ASSERT3P(dn->dn_zio, ==, NULL);
157 ASSERT3U(dn->dn_oldused, ==, 0);
158 ASSERT3U(dn->dn_oldflags, ==, 0);
159 ASSERT3U(dn->dn_olduid, ==, 0);
160 ASSERT3U(dn->dn_oldgid, ==, 0);
161 ASSERT3U(dn->dn_newuid, ==, 0);
162 ASSERT3U(dn->dn_newgid, ==, 0);
163 ASSERT3U(dn->dn_id_flags, ==, 0);
164
165 ASSERT3U(dn->dn_dbufs_count, ==, 0);
166 list_destroy(&dn->dn_dbufs);
167 }
168
169 void
170 dnode_init(void)
171 {
172 ASSERT(dnode_cache == NULL);
173 dnode_cache = kmem_cache_create("dnode_t",
174 sizeof (dnode_t),
175 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
176 kmem_cache_set_move(dnode_cache, dnode_move);
177 }
178
179 void
180 dnode_fini(void)
181 {
182 kmem_cache_destroy(dnode_cache);
183 dnode_cache = NULL;
184 }
185
344 dnode_setdirty(dn, tx);
345 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
346 dn->dn_bonustype = newtype;
347 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
348 rw_exit(&dn->dn_struct_rwlock);
349 }
350
351 void
352 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
353 {
354 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
355 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
356 dnode_setdirty(dn, tx);
357 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
358 dn->dn_have_spill = B_FALSE;
359 }
360
361 static void
362 dnode_setdblksz(dnode_t *dn, int size)
363 {
364 ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
365 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
366 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
367 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
368 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
369 dn->dn_datablksz = size;
370 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
371 dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
372 }
373
374 static dnode_t *
375 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
376 uint64_t object, dnode_handle_t *dnh)
377 {
378 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
379
380 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
381 dn->dn_moved = 0;
382
383 /*
384 * Defer setting dn_objset until the dnode is ready to be a candidate
489
490 if (ibs == 0)
491 ibs = zfs_default_ibs;
492
493 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
494
495 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
496 dn->dn_object, tx->tx_txg, blocksize, ibs);
497
498 ASSERT(dn->dn_type == DMU_OT_NONE);
499 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
500 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
501 ASSERT(ot != DMU_OT_NONE);
502 ASSERT(DMU_OT_IS_VALID(ot));
503 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
504 (bonustype == DMU_OT_SA && bonuslen == 0) ||
505 (bonustype != DMU_OT_NONE && bonuslen != 0));
506 ASSERT(DMU_OT_IS_VALID(bonustype));
507 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
508 ASSERT(dn->dn_type == DMU_OT_NONE);
509 ASSERT3U(dn->dn_maxblkid, ==, 0);
510 ASSERT3U(dn->dn_allocated_txg, ==, 0);
511 ASSERT3U(dn->dn_assigned_txg, ==, 0);
512 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
513 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
514 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
515
516 for (i = 0; i < TXG_SIZE; i++) {
517 ASSERT3U(dn->dn_next_nblkptr[i], ==, 0);
518 ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
519 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
520 ASSERT3U(dn->dn_next_bonuslen[i], ==, 0);
521 ASSERT3U(dn->dn_next_bonustype[i], ==, 0);
522 ASSERT3U(dn->dn_rm_spillblk[i], ==, 0);
523 ASSERT3U(dn->dn_next_blksz[i], ==, 0);
524 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
525 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
526 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
527 }
528
529 dn->dn_type = ot;
530 dnode_setdblksz(dn, blocksize);
531 dn->dn_indblkshift = ibs;
532 dn->dn_nlevels = 1;
533 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
534 dn->dn_nblkptr = 1;
535 else
536 dn->dn_nblkptr = 1 +
537 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
538 dn->dn_bonustype = bonustype;
539 dn->dn_bonuslen = bonuslen;
540 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
541 dn->dn_compress = ZIO_COMPRESS_INHERIT;
542 dn->dn_dirtyctx = 0;
543
544 dn->dn_free_txg = 0;
545 if (dn->dn_dirtyctx_firstset) {
546 kmem_free(dn->dn_dirtyctx_firstset, 1);
548 }
549
550 dn->dn_allocated_txg = tx->tx_txg;
551 dn->dn_id_flags = 0;
552
553 dnode_setdirty(dn, tx);
554 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
555 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
556 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
557 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
558 }
559
560 void
561 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
562 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
563 {
564 int nblkptr;
565
566 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
567 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
568 ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
569 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
570 ASSERT(tx->tx_txg != 0);
571 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
572 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
573 (bonustype == DMU_OT_SA && bonuslen == 0));
574 ASSERT(DMU_OT_IS_VALID(bonustype));
575 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
576
577 /* clean up any unreferenced dbufs */
578 dnode_evict_dbufs(dn);
579
580 dn->dn_id_flags = 0;
581
582 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
583 dnode_setdirty(dn, tx);
584 if (dn->dn_datablksz != blocksize) {
585 /* change blocksize */
586 ASSERT(dn->dn_maxblkid == 0 &&
587 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
588 dnode_block_freed(dn, 0)));
1218 mutex_exit(&dn->dn_mtx);
1219 #endif
1220
1221 /*
1222 * Determine old uid/gid when necessary
1223 */
1224 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1225
1226 mutex_enter(&os->os_lock);
1227
1228 /*
1229 * If we are already marked dirty, we're done.
1230 */
1231 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1232 mutex_exit(&os->os_lock);
1233 return;
1234 }
1235
1236 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
1237 ASSERT(dn->dn_datablksz != 0);
1238 ASSERT3U(dn->dn_next_bonuslen[txg&TXG_MASK], ==, 0);
1239 ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
1240 ASSERT3U(dn->dn_next_bonustype[txg&TXG_MASK], ==, 0);
1241
1242 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1243 dn->dn_object, txg);
1244
1245 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1246 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1247 } else {
1248 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1249 }
1250
1251 mutex_exit(&os->os_lock);
1252
1253 /*
1254 * The dnode maintains a hold on its containing dbuf as
1255 * long as there are holds on it. Each instantiated child
1256 * dbuf maintains a hold on the dnode. When the last child
1257 * drops its hold, the dnode will drop its hold on the
1258 * containing dbuf. We add a "dirty hold" here so that the
1259 * dnode will hang around after we finish processing its
1260 * children.
1570 dbuf_rele(db, FTAG);
1571 }
1572 off += head;
1573 len -= head;
1574 }
1575
1576 /* If the range was less than one block, we're done */
1577 if (len == 0)
1578 goto out;
1579
1580 /* If the remaining range is past end of file, we're done */
1581 if ((off >> blkshift) > dn->dn_maxblkid)
1582 goto out;
1583
1584 ASSERT(ISP2(blksz));
1585 if (trunc)
1586 tail = 0;
1587 else
1588 tail = P2PHASE(len, blksz);
1589
1590 ASSERT3U(P2PHASE(off, blksz), ==, 0);
1591 /* zero out any partial block data at the end of the range */
1592 if (tail) {
1593 if (len < tail)
1594 tail = len;
1595 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1596 TRUE, FTAG, &db) == 0) {
1597 /* don't dirty if not on disk and not dirty */
1598 if (db->db_last_dirty ||
1599 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1600 rw_exit(&dn->dn_struct_rwlock);
1601 dbuf_will_dirty(db, tx);
1602 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1603 bzero(db->db.db_data, tail);
1604 }
1605 dbuf_rele(db, FTAG);
1606 }
1607 len -= tail;
1608 }
1609
1610 /* If the range did not include a full block, we are done */
1752 /* call from syncing context when we actually write/free space for this dnode */
1753 void
1754 dnode_diduse_space(dnode_t *dn, int64_t delta)
1755 {
1756 uint64_t space;
1757 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1758 dn, dn->dn_phys,
1759 (u_longlong_t)dn->dn_phys->dn_used,
1760 (longlong_t)delta);
1761
1762 mutex_enter(&dn->dn_mtx);
1763 space = DN_USED_BYTES(dn->dn_phys);
1764 if (delta > 0) {
1765 ASSERT3U(space + delta, >=, space); /* no overflow */
1766 } else {
1767 ASSERT3U(space, >=, -delta); /* no underflow */
1768 }
1769 space += delta;
1770 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1771 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1772 ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
1773 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1774 } else {
1775 dn->dn_phys->dn_used = space;
1776 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1777 }
1778 mutex_exit(&dn->dn_mtx);
1779 }
1780
1781 /*
1782 * Call when we think we're going to write/free space in open context.
1783 * Be conservative (ie. OK to write less than this or free more than
1784 * this, but don't write more or free less).
1785 */
1786 void
1787 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1788 {
1789 objset_t *os = dn->dn_objset;
1790 dsl_dataset_t *ds = os->os_dsl_dataset;
1791
1792 if (space > 0)
|
120
121 /* ARGSUSED */
122 static void
123 dnode_dest(void *arg, void *unused)
124 {
125 int i;
126 dnode_t *dn = arg;
127
128 rw_destroy(&dn->dn_struct_rwlock);
129 mutex_destroy(&dn->dn_mtx);
130 mutex_destroy(&dn->dn_dbufs_mtx);
131 cv_destroy(&dn->dn_notxholds);
132 refcount_destroy(&dn->dn_holds);
133 refcount_destroy(&dn->dn_tx_holds);
134 ASSERT(!list_link_active(&dn->dn_link));
135
136 for (i = 0; i < TXG_SIZE; i++) {
137 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
138 avl_destroy(&dn->dn_ranges[i]);
139 list_destroy(&dn->dn_dirty_records[i]);
140 ASSERT0(dn->dn_next_nblkptr[i]);
141 ASSERT0(dn->dn_next_nlevels[i]);
142 ASSERT0(dn->dn_next_indblkshift[i]);
143 ASSERT0(dn->dn_next_bonustype[i]);
144 ASSERT0(dn->dn_rm_spillblk[i]);
145 ASSERT0(dn->dn_next_bonuslen[i]);
146 ASSERT0(dn->dn_next_blksz[i]);
147 }
148
149 ASSERT0(dn->dn_allocated_txg);
150 ASSERT0(dn->dn_free_txg);
151 ASSERT0(dn->dn_assigned_txg);
152 ASSERT0(dn->dn_dirtyctx);
153 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
154 ASSERT3P(dn->dn_bonus, ==, NULL);
155 ASSERT(!dn->dn_have_spill);
156 ASSERT3P(dn->dn_zio, ==, NULL);
157 ASSERT0(dn->dn_oldused);
158 ASSERT0(dn->dn_oldflags);
159 ASSERT0(dn->dn_olduid);
160 ASSERT0(dn->dn_oldgid);
161 ASSERT0(dn->dn_newuid);
162 ASSERT0(dn->dn_newgid);
163 ASSERT0(dn->dn_id_flags);
164
165 ASSERT0(dn->dn_dbufs_count);
166 list_destroy(&dn->dn_dbufs);
167 }
168
169 void
170 dnode_init(void)
171 {
172 ASSERT(dnode_cache == NULL);
173 dnode_cache = kmem_cache_create("dnode_t",
174 sizeof (dnode_t),
175 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
176 kmem_cache_set_move(dnode_cache, dnode_move);
177 }
178
179 void
180 dnode_fini(void)
181 {
182 kmem_cache_destroy(dnode_cache);
183 dnode_cache = NULL;
184 }
185
344 dnode_setdirty(dn, tx);
345 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
346 dn->dn_bonustype = newtype;
347 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
348 rw_exit(&dn->dn_struct_rwlock);
349 }
350
351 void
352 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
353 {
354 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
355 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
356 dnode_setdirty(dn, tx);
357 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
358 dn->dn_have_spill = B_FALSE;
359 }
360
361 static void
362 dnode_setdblksz(dnode_t *dn, int size)
363 {
364 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
365 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
366 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
367 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
368 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
369 dn->dn_datablksz = size;
370 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
371 dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
372 }
373
374 static dnode_t *
375 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
376 uint64_t object, dnode_handle_t *dnh)
377 {
378 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
379
380 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
381 dn->dn_moved = 0;
382
383 /*
384 * Defer setting dn_objset until the dnode is ready to be a candidate
489
490 if (ibs == 0)
491 ibs = zfs_default_ibs;
492
493 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
494
495 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
496 dn->dn_object, tx->tx_txg, blocksize, ibs);
497
498 ASSERT(dn->dn_type == DMU_OT_NONE);
499 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
500 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
501 ASSERT(ot != DMU_OT_NONE);
502 ASSERT(DMU_OT_IS_VALID(ot));
503 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
504 (bonustype == DMU_OT_SA && bonuslen == 0) ||
505 (bonustype != DMU_OT_NONE && bonuslen != 0));
506 ASSERT(DMU_OT_IS_VALID(bonustype));
507 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
508 ASSERT(dn->dn_type == DMU_OT_NONE);
509 ASSERT0(dn->dn_maxblkid);
510 ASSERT0(dn->dn_allocated_txg);
511 ASSERT0(dn->dn_assigned_txg);
512 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
513 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
514 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
515
516 for (i = 0; i < TXG_SIZE; i++) {
517 ASSERT0(dn->dn_next_nblkptr[i]);
518 ASSERT0(dn->dn_next_nlevels[i]);
519 ASSERT0(dn->dn_next_indblkshift[i]);
520 ASSERT0(dn->dn_next_bonuslen[i]);
521 ASSERT0(dn->dn_next_bonustype[i]);
522 ASSERT0(dn->dn_rm_spillblk[i]);
523 ASSERT0(dn->dn_next_blksz[i]);
524 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
525 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
526 ASSERT0(avl_numnodes(&dn->dn_ranges[i]));
527 }
528
529 dn->dn_type = ot;
530 dnode_setdblksz(dn, blocksize);
531 dn->dn_indblkshift = ibs;
532 dn->dn_nlevels = 1;
533 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
534 dn->dn_nblkptr = 1;
535 else
536 dn->dn_nblkptr = 1 +
537 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
538 dn->dn_bonustype = bonustype;
539 dn->dn_bonuslen = bonuslen;
540 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
541 dn->dn_compress = ZIO_COMPRESS_INHERIT;
542 dn->dn_dirtyctx = 0;
543
544 dn->dn_free_txg = 0;
545 if (dn->dn_dirtyctx_firstset) {
546 kmem_free(dn->dn_dirtyctx_firstset, 1);
548 }
549
550 dn->dn_allocated_txg = tx->tx_txg;
551 dn->dn_id_flags = 0;
552
553 dnode_setdirty(dn, tx);
554 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
555 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
556 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
557 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
558 }
559
560 void
561 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
562 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
563 {
564 int nblkptr;
565
566 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
567 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
568 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
569 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
570 ASSERT(tx->tx_txg != 0);
571 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
572 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
573 (bonustype == DMU_OT_SA && bonuslen == 0));
574 ASSERT(DMU_OT_IS_VALID(bonustype));
575 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
576
577 /* clean up any unreferenced dbufs */
578 dnode_evict_dbufs(dn);
579
580 dn->dn_id_flags = 0;
581
582 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
583 dnode_setdirty(dn, tx);
584 if (dn->dn_datablksz != blocksize) {
585 /* change blocksize */
586 ASSERT(dn->dn_maxblkid == 0 &&
587 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
588 dnode_block_freed(dn, 0)));
1218 mutex_exit(&dn->dn_mtx);
1219 #endif
1220
1221 /*
1222 * Determine old uid/gid when necessary
1223 */
1224 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1225
1226 mutex_enter(&os->os_lock);
1227
1228 /*
1229 * If we are already marked dirty, we're done.
1230 */
1231 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1232 mutex_exit(&os->os_lock);
1233 return;
1234 }
1235
1236 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
1237 ASSERT(dn->dn_datablksz != 0);
1238 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1239 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1240 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1241
1242 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1243 dn->dn_object, txg);
1244
1245 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1246 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1247 } else {
1248 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1249 }
1250
1251 mutex_exit(&os->os_lock);
1252
1253 /*
1254 * The dnode maintains a hold on its containing dbuf as
1255 * long as there are holds on it. Each instantiated child
1256 * dbuf maintains a hold on the dnode. When the last child
1257 * drops its hold, the dnode will drop its hold on the
1258 * containing dbuf. We add a "dirty hold" here so that the
1259 * dnode will hang around after we finish processing its
1260 * children.
1570 dbuf_rele(db, FTAG);
1571 }
1572 off += head;
1573 len -= head;
1574 }
1575
1576 /* If the range was less than one block, we're done */
1577 if (len == 0)
1578 goto out;
1579
1580 /* If the remaining range is past end of file, we're done */
1581 if ((off >> blkshift) > dn->dn_maxblkid)
1582 goto out;
1583
1584 ASSERT(ISP2(blksz));
1585 if (trunc)
1586 tail = 0;
1587 else
1588 tail = P2PHASE(len, blksz);
1589
1590 ASSERT0(P2PHASE(off, blksz));
1591 /* zero out any partial block data at the end of the range */
1592 if (tail) {
1593 if (len < tail)
1594 tail = len;
1595 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1596 TRUE, FTAG, &db) == 0) {
1597 /* don't dirty if not on disk and not dirty */
1598 if (db->db_last_dirty ||
1599 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1600 rw_exit(&dn->dn_struct_rwlock);
1601 dbuf_will_dirty(db, tx);
1602 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1603 bzero(db->db.db_data, tail);
1604 }
1605 dbuf_rele(db, FTAG);
1606 }
1607 len -= tail;
1608 }
1609
1610 /* If the range did not include a full block, we are done */
1752 /* call from syncing context when we actually write/free space for this dnode */
1753 void
1754 dnode_diduse_space(dnode_t *dn, int64_t delta)
1755 {
1756 uint64_t space;
1757 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1758 dn, dn->dn_phys,
1759 (u_longlong_t)dn->dn_phys->dn_used,
1760 (longlong_t)delta);
1761
1762 mutex_enter(&dn->dn_mtx);
1763 space = DN_USED_BYTES(dn->dn_phys);
1764 if (delta > 0) {
1765 ASSERT3U(space + delta, >=, space); /* no overflow */
1766 } else {
1767 ASSERT3U(space, >=, -delta); /* no underflow */
1768 }
1769 space += delta;
1770 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1771 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1772 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
1773 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1774 } else {
1775 dn->dn_phys->dn_used = space;
1776 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1777 }
1778 mutex_exit(&dn->dn_mtx);
1779 }
1780
1781 /*
1782 * Call when we think we're going to write/free space in open context.
1783 * Be conservative (ie. OK to write less than this or free more than
1784 * this, but don't write more or free less).
1785 */
1786 void
1787 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1788 {
1789 objset_t *os = dn->dn_objset;
1790 dsl_dataset_t *ds = os->os_dsl_dataset;
1791
1792 if (space > 0)
|