4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
28 #include <sys/dnode.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dsl_dataset.h>
36 dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
39 int txgoff = tx->tx_txg & TXG_MASK;
40 int nblkptr = dn->dn_phys->dn_nblkptr;
41 int old_toplvl = dn->dn_phys->dn_nlevels - 1;
42 int new_level = dn->dn_next_nlevels[txgoff];
45 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
47 /* this dnode can't be paged out because it's dirty */
48 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
49 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
50 ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
52 db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
55 dn->dn_phys->dn_nlevels = new_level;
56 dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
57 dn->dn_object, dn->dn_phys->dn_nlevels);
59 /* check for existing blkptrs in the dnode */
60 for (i = 0; i < nblkptr; i++)
61 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
64 /* transfer dnode's block pointers to new indirect block */
65 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
66 ASSERT(db->db.db_data);
67 ASSERT(arc_released(db->db_buf));
68 ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
69 bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
70 sizeof (blkptr_t) * nblkptr);
71 arc_buf_freeze(db->db_buf);
74 /* set dbuf's parent pointers to new indirect buf */
75 for (i = 0; i < nblkptr; i++) {
76 dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
80 ASSERT3P(child->db_dnode, ==, dn);
81 if (child->db_parent && child->db_parent != dn->dn_dbuf) {
82 ASSERT(child->db_parent->db_level == db->db_level);
83 ASSERT(child->db_blkptr !=
84 &dn->dn_phys->dn_blkptr[child->db_blkid]);
85 mutex_exit(&child->db_mtx);
88 ASSERT(child->db_parent == NULL ||
89 child->db_parent == dn->dn_dbuf);
91 child->db_parent = db;
92 dbuf_add_ref(db, child);
94 child->db_blkptr = (blkptr_t *)db->db.db_data + i;
96 child->db_blkptr = NULL;
97 dprintf_dbuf_bp(child, child->db_blkptr,
98 "changed db_blkptr to new indirect %s", "");
100 mutex_exit(&child->db_mtx);
103 bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
107 rw_exit(&dn->dn_struct_rwlock);
111 free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
113 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
114 uint64_t bytesfreed = 0;
115 int i, blocks_freed = 0;
117 dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
119 for (i = 0; i < num; i++, bp++) {
123 bytesfreed += dsl_dataset_block_kill(ds, bp, dn->dn_zio, tx);
124 ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
125 bzero(bp, sizeof (blkptr_t));
128 dnode_diduse_space(dn, -bytesfreed);
129 return (blocks_freed);
134 free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
138 uint64_t txg = tx->tx_txg;
140 epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
141 off = start - (db->db_blkid * 1<<epbs);
142 num = end - start + 1;
144 ASSERT3U(off, >=, 0);
145 ASSERT3U(num, >=, 0);
146 ASSERT3U(db->db_level, >, 0);
147 ASSERT3U(db->db.db_size, ==, 1<<db->db_dnode->dn_phys->dn_indblkshift);
148 ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
149 ASSERT(db->db_blkptr != NULL);
151 for (i = off; i < off+num; i++) {
153 dmu_buf_impl_t *child;
154 dbuf_dirty_record_t *dr;
157 ASSERT(db->db_level == 1);
159 rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
160 err = dbuf_hold_impl(db->db_dnode, db->db_level-1,
161 (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
162 rw_exit(&db->db_dnode->dn_struct_rwlock);
166 ASSERT(child->db_level == 0);
167 dr = child->db_last_dirty;
168 while (dr && dr->dr_txg > txg)
170 ASSERT(dr == NULL || dr->dr_txg == txg);
172 /* data_old better be zeroed */
174 buf = dr->dt.dl.dr_data->b_data;
175 for (j = 0; j < child->db.db_size >> 3; j++) {
177 panic("freed data not zero: "
178 "child=%p i=%d off=%d num=%d\n",
179 (void *)child, i, off, num);
185 * db_data better be zeroed unless it's dirty in a
188 mutex_enter(&child->db_mtx);
189 buf = child->db.db_data;
190 if (buf != NULL && child->db_state != DB_FILL &&
191 child->db_last_dirty == NULL) {
192 for (j = 0; j < child->db.db_size >> 3; j++) {
194 panic("freed data not zero: "
195 "child=%p i=%d off=%d num=%d\n",
196 (void *)child, i, off, num);
200 mutex_exit(&child->db_mtx);
202 dbuf_rele(child, FTAG);
210 free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
213 dnode_t *dn = db->db_dnode;
215 dmu_buf_impl_t *subdb;
216 uint64_t start, end, dbstart, dbend, i;
217 int epbs, shift, err;
219 int blocks_freed = 0;
222 * There is a small possibility that this block will not be cached:
223 * 1 - if level > 1 and there are no children with level <= 1
224 * 2 - if we didn't get a dirty hold (because this block had just
225 * finished being written -- and so had no holds), and then this
226 * block got evicted before we got here.
228 if (db->db_state != DB_CACHED)
229 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
231 arc_release(db->db_buf, db);
232 bp = (blkptr_t *)db->db.db_data;
234 epbs = db->db_dnode->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
235 shift = (db->db_level - 1) * epbs;
236 dbstart = db->db_blkid << epbs;
237 start = blkid >> shift;
238 if (dbstart < start) {
239 bp += start - dbstart;
244 dbend = ((db->db_blkid + 1) << epbs) - 1;
245 end = (blkid + nblks - 1) >> shift;
250 ASSERT3U(start, <=, end);
252 if (db->db_level == 1) {
253 FREE_VERIFY(db, start, end, tx);
254 blocks_freed = free_blocks(dn, bp, end-start+1, tx);
255 arc_buf_freeze(db->db_buf);
256 ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
257 return (all ? ALL : blocks_freed);
260 for (i = start; i <= end; i++, bp++) {
263 rw_enter(&dn->dn_struct_rwlock, RW_READER);
264 err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
265 ASSERT3U(err, ==, 0);
266 rw_exit(&dn->dn_struct_rwlock);
268 if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) {
269 ASSERT3P(subdb->db_blkptr, ==, bp);
270 blocks_freed += free_blocks(dn, bp, 1, tx);
274 dbuf_rele(subdb, FTAG);
276 arc_buf_freeze(db->db_buf);
279 for (i = start; i <= end; i++, bp++) {
280 if (i == start && blkid != 0)
282 else if (i == end && !trunc)
284 ASSERT3U(bp->blk_birth, ==, 0);
287 ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
288 return (all ? ALL : blocks_freed);
292 * free_range: Traverse the indicated range of the provided file
293 * and "free" all the blocks contained there.
296 dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
298 blkptr_t *bp = dn->dn_phys->dn_blkptr;
300 int trunc, start, end, shift, i, err;
301 int dnlevel = dn->dn_phys->dn_nlevels;
303 if (blkid > dn->dn_phys->dn_maxblkid)
306 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
307 trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
309 nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
311 /* There are no indirect blocks in the object */
313 if (blkid >= dn->dn_phys->dn_nblkptr) {
314 /* this range was never made persistent */
317 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
318 (void) free_blocks(dn, bp + blkid, nblks, tx);
320 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
321 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
322 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
323 ASSERT(off < dn->dn_phys->dn_maxblkid ||
324 dn->dn_phys->dn_maxblkid == 0 ||
325 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
330 shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
331 start = blkid >> shift;
332 ASSERT(start < dn->dn_phys->dn_nblkptr);
333 end = (blkid + nblks - 1) >> shift;
335 for (i = start; i <= end; i++, bp++) {
338 rw_enter(&dn->dn_struct_rwlock, RW_READER);
339 err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
340 ASSERT3U(err, ==, 0);
341 rw_exit(&dn->dn_struct_rwlock);
343 if (free_children(db, blkid, nblks, trunc, tx) == ALL) {
344 ASSERT3P(db->db_blkptr, ==, bp);
345 (void) free_blocks(dn, bp, 1, tx);
350 uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
351 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT);
352 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
353 ASSERT(off < dn->dn_phys->dn_maxblkid ||
354 dn->dn_phys->dn_maxblkid == 0 ||
355 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
360 * Try to kick all the dnodes dbufs out of the cache...
363 dnode_evict_dbufs(dnode_t *dn)
369 dmu_buf_impl_t *db, marker;
370 int evicting = FALSE;
373 mutex_enter(&dn->dn_dbufs_mtx);
374 list_insert_tail(&dn->dn_dbufs, &marker);
375 db = list_head(&dn->dn_dbufs);
376 for (; db != ▮ db = list_head(&dn->dn_dbufs)) {
377 list_remove(&dn->dn_dbufs, db);
378 list_insert_tail(&dn->dn_dbufs, db);
379 ASSERT3P(db->db_dnode, ==, dn);
381 mutex_enter(&db->db_mtx);
382 if (db->db_state == DB_EVICTING) {
385 mutex_exit(&db->db_mtx);
386 } else if (refcount_is_zero(&db->db_holds)) {
388 dbuf_clear(db); /* exits db_mtx for us */
390 mutex_exit(&db->db_mtx);
394 list_remove(&dn->dn_dbufs, &marker);
396 * NB: we need to drop dn_dbufs_mtx between passes so
397 * that any DB_EVICTING dbufs can make progress.
398 * Ideally, we would have some cv we could wait on, but
399 * since we don't, just wait a bit to give the other
400 * thread a chance to run.
402 mutex_exit(&dn->dn_dbufs_mtx);
406 ASSERT(pass < 100); /* sanity check */
409 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
410 if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
411 mutex_enter(&dn->dn_bonus->db_mtx);
412 dbuf_evict(dn->dn_bonus);
415 rw_exit(&dn->dn_struct_rwlock);
419 dnode_undirty_dbufs(list_t *list)
421 dbuf_dirty_record_t *dr;
423 while (dr = list_head(list)) {
424 dmu_buf_impl_t *db = dr->dr_dbuf;
425 uint64_t txg = dr->dr_txg;
427 mutex_enter(&db->db_mtx);
428 /* XXX - use dbuf_undirty()? */
429 list_remove(list, dr);
430 ASSERT(db->db_last_dirty == dr);
431 db->db_last_dirty = NULL;
432 db->db_dirtycnt -= 1;
433 if (db->db_level == 0) {
434 ASSERT(db->db_blkid == DB_BONUS_BLKID ||
435 dr->dt.dl.dr_data == db->db_buf);
437 mutex_exit(&db->db_mtx);
439 mutex_exit(&db->db_mtx);
440 dnode_undirty_dbufs(&dr->dt.di.dr_children);
441 list_destroy(&dr->dt.di.dr_children);
442 mutex_destroy(&dr->dt.di.dr_mtx);
444 kmem_free(dr, sizeof (dbuf_dirty_record_t));
445 dbuf_rele(db, (void *)(uintptr_t)txg);
450 dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
452 int txgoff = tx->tx_txg & TXG_MASK;
454 ASSERT(dmu_tx_is_syncing(tx));
457 * Our contents should have been freed in dnode_sync() by the
458 * free range record inserted by the caller of dnode_free().
460 ASSERT3U(DN_USED_BYTES(dn->dn_phys), ==, 0);
461 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
463 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
464 dnode_evict_dbufs(dn);
465 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
468 * XXX - It would be nice to assert this, but we may still
469 * have residual holds from async evictions from the arc...
471 * zfs_obj_to_path() also depends on this being
474 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
477 /* Undirty next bits */
478 dn->dn_next_nlevels[txgoff] = 0;
479 dn->dn_next_indblkshift[txgoff] = 0;
480 dn->dn_next_blksz[txgoff] = 0;
482 /* ASSERT(blkptrs are zero); */
483 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
484 ASSERT(dn->dn_type != DMU_OT_NONE);
486 ASSERT(dn->dn_free_txg > 0);
487 if (dn->dn_allocated_txg != dn->dn_free_txg)
488 dbuf_will_dirty(dn->dn_dbuf, tx);
489 bzero(dn->dn_phys, sizeof (dnode_phys_t));
491 mutex_enter(&dn->dn_mtx);
492 dn->dn_type = DMU_OT_NONE;
494 dn->dn_allocated_txg = 0;
496 mutex_exit(&dn->dn_mtx);
498 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
500 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
502 * Now that we've released our hold, the dnode may
503 * be evicted, so we musn't access it.
508 * Write out the dnode's dirty buffers.
510 * NOTE: The dnode is kept in memory by being dirty. Once the
511 * dirty bit is cleared, it may be evicted. Beware of this!
514 dnode_sync(dnode_t *dn, dmu_tx_t *tx)
517 dnode_phys_t *dnp = dn->dn_phys;
518 int txgoff = tx->tx_txg & TXG_MASK;
519 list_t *list = &dn->dn_dirty_records[txgoff];
521 ASSERT(dmu_tx_is_syncing(tx));
522 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
525 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
527 mutex_enter(&dn->dn_mtx);
528 if (dn->dn_allocated_txg == tx->tx_txg) {
529 /* The dnode is newly allocated or reallocated */
530 if (dnp->dn_type == DMU_OT_NONE) {
531 /* this is a first alloc, not a realloc */
532 /* XXX shouldn't the phys already be zeroed? */
533 bzero(dnp, DNODE_CORE_SIZE);
535 dnp->dn_nblkptr = dn->dn_nblkptr;
538 dnp->dn_type = dn->dn_type;
539 dnp->dn_bonustype = dn->dn_bonustype;
540 dnp->dn_bonuslen = dn->dn_bonuslen;
543 ASSERT(dnp->dn_nlevels > 1 ||
544 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
545 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
546 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
548 if (dn->dn_next_blksz[txgoff]) {
549 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
550 SPA_MINBLOCKSIZE) == 0);
551 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
552 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
553 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
554 dnp->dn_datablkszsec);
555 dnp->dn_datablkszsec =
556 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
557 dn->dn_next_blksz[txgoff] = 0;
560 if (dn->dn_next_bonuslen[txgoff]) {
561 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
562 dnp->dn_bonuslen = 0;
564 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
565 ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
566 dn->dn_next_bonuslen[txgoff] = 0;
569 if (dn->dn_next_indblkshift[txgoff]) {
570 ASSERT(dnp->dn_nlevels == 1);
571 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
572 dn->dn_next_indblkshift[txgoff] = 0;
576 * Just take the live (open-context) values for checksum and compress.
577 * Strictly speaking it's a future leak, but nothing bad happens if we
578 * start using the new checksum or compress algorithm a little early.
580 dnp->dn_checksum = dn->dn_checksum;
581 dnp->dn_compress = dn->dn_compress;
583 mutex_exit(&dn->dn_mtx);
585 /* process all the "freed" ranges in the file */
586 while (rp = avl_last(&dn->dn_ranges[txgoff])) {
587 dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
588 /* grab the mutex so we don't race with dnode_block_freed() */
589 mutex_enter(&dn->dn_mtx);
590 avl_remove(&dn->dn_ranges[txgoff], rp);
591 mutex_exit(&dn->dn_mtx);
592 kmem_free(rp, sizeof (free_range_t));
595 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
596 dnode_sync_free(dn, tx);
600 if (dn->dn_next_nblkptr[txgoff]) {
601 /* this should only happen on a realloc */
602 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
603 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
604 /* zero the new blkptrs we are gaining */
605 bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
607 (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
611 ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
612 /* the blkptrs we are losing better be unallocated */
613 for (i = dn->dn_next_nblkptr[txgoff];
614 i < dnp->dn_nblkptr; i++)
615 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
618 mutex_enter(&dn->dn_mtx);
619 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
620 dn->dn_next_nblkptr[txgoff] = 0;
621 mutex_exit(&dn->dn_mtx);
624 if (dn->dn_next_nlevels[txgoff]) {
625 dnode_increase_indirection(dn, tx);
626 dn->dn_next_nlevels[txgoff] = 0;
629 dbuf_sync_list(list, tx);
631 if (dn->dn_object != DMU_META_DNODE_OBJECT) {
632 ASSERT3P(list_head(list), ==, NULL);
633 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
637 * Although we have dropped our reference to the dnode, it
638 * can't be evicted until its written, and we haven't yet
639 * initiated the IO for the dnode's dbuf.