153089ab7Seschrock /* 253089ab7Seschrock * CDDL HEADER START 353089ab7Seschrock * 453089ab7Seschrock * The contents of this file are subject to the terms of the 553089ab7Seschrock * Common Development and Distribution License (the "License"). 653089ab7Seschrock * You may not use this file except in compliance with the License. 753089ab7Seschrock * 853089ab7Seschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 953089ab7Seschrock * or http://www.opensolaris.org/os/licensing. 1053089ab7Seschrock * See the License for the specific language governing permissions 1153089ab7Seschrock * and limitations under the License. 1253089ab7Seschrock * 1353089ab7Seschrock * When distributing Covered Code, include this CDDL HEADER in each 1453089ab7Seschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1553089ab7Seschrock * If applicable, add the following below this CDDL HEADER, with the 1653089ab7Seschrock * fields enclosed by brackets "[]" replaced with your own identifying 1753089ab7Seschrock * information: Portions Copyright [yyyy] [name of copyright owner] 1853089ab7Seschrock * 1953089ab7Seschrock * CDDL HEADER END 2053089ab7Seschrock */ 2153089ab7Seschrock 2253089ab7Seschrock /* 23*7802d7bfSMatthew Ahrens * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 2453089ab7Seschrock */ 2553089ab7Seschrock 2653089ab7Seschrock #include <sys/arc.h> 2753089ab7Seschrock #include <sys/bptree.h> 2853089ab7Seschrock #include <sys/dmu.h> 2953089ab7Seschrock #include <sys/dmu_objset.h> 3053089ab7Seschrock #include <sys/dmu_tx.h> 3153089ab7Seschrock #include <sys/dmu_traverse.h> 3253089ab7Seschrock #include <sys/dsl_dataset.h> 3353089ab7Seschrock #include <sys/dsl_dir.h> 3453089ab7Seschrock #include <sys/dsl_pool.h> 3553089ab7Seschrock #include <sys/dnode.h> 3653089ab7Seschrock #include <sys/refcount.h> 3753089ab7Seschrock #include <sys/spa.h> 3853089ab7Seschrock 3953089ab7Seschrock /* 4053089ab7Seschrock * A bptree is a queue of root block pointers from destroyed datasets. When a 4153089ab7Seschrock * dataset is destroyed its root block pointer is put on the end of the pool's 4253089ab7Seschrock * bptree queue so the dataset's blocks can be freed asynchronously by 4353089ab7Seschrock * dsl_scan_sync. This allows the delete operation to finish without traversing 4453089ab7Seschrock * all the dataset's blocks. 4553089ab7Seschrock * 46f7170741SWill Andrews * Note that while bt_begin and bt_end are only ever incremented in this code, 4753089ab7Seschrock * they are effectively reset to 0 every time the entire bptree is freed because 4853089ab7Seschrock * the bptree's object is destroyed and re-created. 4953089ab7Seschrock */ 5053089ab7Seschrock 5153089ab7Seschrock struct bptree_args { 5253089ab7Seschrock bptree_phys_t *ba_phys; /* data in bonus buffer, dirtied if freeing */ 5353089ab7Seschrock boolean_t ba_free; /* true if freeing during traversal */ 5453089ab7Seschrock 5553089ab7Seschrock bptree_itor_t *ba_func; /* function to call for each blockpointer */ 5653089ab7Seschrock void *ba_arg; /* caller supplied argument to ba_func */ 5753089ab7Seschrock dmu_tx_t *ba_tx; /* caller supplied tx, NULL if not freeing */ 5853089ab7Seschrock } bptree_args_t; 5953089ab7Seschrock 6053089ab7Seschrock uint64_t 6153089ab7Seschrock bptree_alloc(objset_t *os, dmu_tx_t *tx) 6253089ab7Seschrock { 6353089ab7Seschrock uint64_t obj; 6453089ab7Seschrock dmu_buf_t *db; 6553089ab7Seschrock bptree_phys_t *bt; 6653089ab7Seschrock 6753089ab7Seschrock obj = dmu_object_alloc(os, DMU_OTN_UINT64_METADATA, 6853089ab7Seschrock SPA_MAXBLOCKSIZE, DMU_OTN_UINT64_METADATA, 6953089ab7Seschrock sizeof (bptree_phys_t), tx); 7053089ab7Seschrock 7153089ab7Seschrock /* 7253089ab7Seschrock * Bonus buffer contents are already initialized to 0, but for 7353089ab7Seschrock * readability we make it explicit. 7453089ab7Seschrock */ 75b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 7653089ab7Seschrock dmu_buf_will_dirty(db, tx); 7753089ab7Seschrock bt = db->db_data; 7853089ab7Seschrock bt->bt_begin = 0; 7953089ab7Seschrock bt->bt_end = 0; 8053089ab7Seschrock bt->bt_bytes = 0; 8153089ab7Seschrock bt->bt_comp = 0; 8253089ab7Seschrock bt->bt_uncomp = 0; 8353089ab7Seschrock dmu_buf_rele(db, FTAG); 8453089ab7Seschrock 8553089ab7Seschrock return (obj); 8653089ab7Seschrock } 8753089ab7Seschrock 8853089ab7Seschrock int 8953089ab7Seschrock bptree_free(objset_t *os, uint64_t obj, dmu_tx_t *tx) 9053089ab7Seschrock { 9153089ab7Seschrock dmu_buf_t *db; 9253089ab7Seschrock bptree_phys_t *bt; 9353089ab7Seschrock 94b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 9553089ab7Seschrock bt = db->db_data; 9653089ab7Seschrock ASSERT3U(bt->bt_begin, ==, bt->bt_end); 97fb09f5aaSMadhav Suresh ASSERT0(bt->bt_bytes); 98fb09f5aaSMadhav Suresh ASSERT0(bt->bt_comp); 99fb09f5aaSMadhav Suresh ASSERT0(bt->bt_uncomp); 10053089ab7Seschrock dmu_buf_rele(db, FTAG); 10153089ab7Seschrock 10253089ab7Seschrock return (dmu_object_free(os, obj, tx)); 10353089ab7Seschrock } 10453089ab7Seschrock 1057fd05ac4SMatthew Ahrens boolean_t 1067fd05ac4SMatthew Ahrens bptree_is_empty(objset_t *os, uint64_t obj) 1077fd05ac4SMatthew Ahrens { 1087fd05ac4SMatthew Ahrens dmu_buf_t *db; 1097fd05ac4SMatthew Ahrens bptree_phys_t *bt; 1107fd05ac4SMatthew Ahrens boolean_t rv; 1117fd05ac4SMatthew Ahrens 1127fd05ac4SMatthew Ahrens VERIFY0(dmu_bonus_hold(os, obj, FTAG, &db)); 1137fd05ac4SMatthew Ahrens bt = db->db_data; 1147fd05ac4SMatthew Ahrens rv = (bt->bt_begin == bt->bt_end); 1157fd05ac4SMatthew Ahrens dmu_buf_rele(db, FTAG); 1167fd05ac4SMatthew Ahrens return (rv); 1177fd05ac4SMatthew Ahrens } 1187fd05ac4SMatthew Ahrens 11953089ab7Seschrock void 12053089ab7Seschrock bptree_add(objset_t *os, uint64_t obj, blkptr_t *bp, uint64_t birth_txg, 12153089ab7Seschrock uint64_t bytes, uint64_t comp, uint64_t uncomp, dmu_tx_t *tx) 12253089ab7Seschrock { 12353089ab7Seschrock dmu_buf_t *db; 12453089ab7Seschrock bptree_phys_t *bt; 1257fd05ac4SMatthew Ahrens bptree_entry_phys_t bte = { 0 }; 12653089ab7Seschrock 12753089ab7Seschrock /* 12853089ab7Seschrock * bptree objects are in the pool mos, therefore they can only be 12953089ab7Seschrock * modified in syncing context. Furthermore, this is only modified 13053089ab7Seschrock * by the sync thread, so no locking is necessary. 13153089ab7Seschrock */ 13253089ab7Seschrock ASSERT(dmu_tx_is_syncing(tx)); 13353089ab7Seschrock 134b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 13553089ab7Seschrock bt = db->db_data; 13653089ab7Seschrock 13753089ab7Seschrock bte.be_birth_txg = birth_txg; 13853089ab7Seschrock bte.be_bp = *bp; 13953089ab7Seschrock dmu_write(os, obj, bt->bt_end * sizeof (bte), sizeof (bte), &bte, tx); 14053089ab7Seschrock 14153089ab7Seschrock dmu_buf_will_dirty(db, tx); 14253089ab7Seschrock bt->bt_end++; 14353089ab7Seschrock bt->bt_bytes += bytes; 14453089ab7Seschrock bt->bt_comp += comp; 14553089ab7Seschrock bt->bt_uncomp += uncomp; 14653089ab7Seschrock dmu_buf_rele(db, FTAG); 14753089ab7Seschrock } 14853089ab7Seschrock 14953089ab7Seschrock /* ARGSUSED */ 15053089ab7Seschrock static int 1511b912ec7SGeorge Wilson bptree_visit_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 152*7802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 15353089ab7Seschrock { 15453089ab7Seschrock int err; 15553089ab7Seschrock struct bptree_args *ba = arg; 15653089ab7Seschrock 15743466aaeSMax Grossman if (BP_IS_HOLE(bp)) 15853089ab7Seschrock return (0); 15953089ab7Seschrock 16053089ab7Seschrock err = ba->ba_func(ba->ba_arg, bp, ba->ba_tx); 16153089ab7Seschrock if (err == 0 && ba->ba_free) { 16253089ab7Seschrock ba->ba_phys->bt_bytes -= bp_get_dsize_sync(spa, bp); 16353089ab7Seschrock ba->ba_phys->bt_comp -= BP_GET_PSIZE(bp); 16453089ab7Seschrock ba->ba_phys->bt_uncomp -= BP_GET_UCSIZE(bp); 16553089ab7Seschrock } 16653089ab7Seschrock return (err); 16753089ab7Seschrock } 16853089ab7Seschrock 1697fd05ac4SMatthew Ahrens /* 1707fd05ac4SMatthew Ahrens * If "free" is set: 1717fd05ac4SMatthew Ahrens * - It is assumed that "func" will be freeing the block pointers. 1727fd05ac4SMatthew Ahrens * - If "func" returns nonzero, the bookmark will be remembered and 1737fd05ac4SMatthew Ahrens * iteration will be restarted from this point on next invocation. 1747fd05ac4SMatthew Ahrens * - If an i/o error is encountered (e.g. "func" returns EIO or ECKSUM), 1757fd05ac4SMatthew Ahrens * bptree_iterate will remember the bookmark, continue traversing 1767fd05ac4SMatthew Ahrens * any additional entries, and return 0. 1777fd05ac4SMatthew Ahrens * 1787fd05ac4SMatthew Ahrens * If "free" is not set, traversal will stop and return an error if 1797fd05ac4SMatthew Ahrens * an i/o error is encountered. 1807fd05ac4SMatthew Ahrens * 1817fd05ac4SMatthew Ahrens * In either case, if zfs_free_leak_on_eio is set, i/o errors will be 1827fd05ac4SMatthew Ahrens * ignored and traversal will continue (i.e. TRAVERSE_HARD will be passed to 1837fd05ac4SMatthew Ahrens * traverse_dataset_destroyed()). 1847fd05ac4SMatthew Ahrens */ 18553089ab7Seschrock int 18653089ab7Seschrock bptree_iterate(objset_t *os, uint64_t obj, boolean_t free, bptree_itor_t func, 18753089ab7Seschrock void *arg, dmu_tx_t *tx) 18853089ab7Seschrock { 1897fd05ac4SMatthew Ahrens boolean_t ioerr = B_FALSE; 19053089ab7Seschrock int err; 19153089ab7Seschrock uint64_t i; 19253089ab7Seschrock dmu_buf_t *db; 19353089ab7Seschrock struct bptree_args ba; 19453089ab7Seschrock 19553089ab7Seschrock ASSERT(!free || dmu_tx_is_syncing(tx)); 19653089ab7Seschrock 19753089ab7Seschrock err = dmu_bonus_hold(os, obj, FTAG, &db); 19853089ab7Seschrock if (err != 0) 19953089ab7Seschrock return (err); 20053089ab7Seschrock 20153089ab7Seschrock if (free) 20253089ab7Seschrock dmu_buf_will_dirty(db, tx); 20353089ab7Seschrock 20453089ab7Seschrock ba.ba_phys = db->db_data; 20553089ab7Seschrock ba.ba_free = free; 20653089ab7Seschrock ba.ba_func = func; 20753089ab7Seschrock ba.ba_arg = arg; 20853089ab7Seschrock ba.ba_tx = tx; 20953089ab7Seschrock 21053089ab7Seschrock err = 0; 21153089ab7Seschrock for (i = ba.ba_phys->bt_begin; i < ba.ba_phys->bt_end; i++) { 21253089ab7Seschrock bptree_entry_phys_t bte; 2138b36997aSMatthew Ahrens int flags = TRAVERSE_PREFETCH_METADATA | TRAVERSE_POST; 21453089ab7Seschrock 21553089ab7Seschrock err = dmu_read(os, obj, i * sizeof (bte), sizeof (bte), 21653089ab7Seschrock &bte, DMU_READ_NO_PREFETCH); 21753089ab7Seschrock if (err != 0) 21853089ab7Seschrock break; 21953089ab7Seschrock 2207fd05ac4SMatthew Ahrens if (zfs_free_leak_on_eio) 2218b36997aSMatthew Ahrens flags |= TRAVERSE_HARD; 2227fd05ac4SMatthew Ahrens zfs_dbgmsg("bptree index %d: traversing from min_txg=%lld " 2237fd05ac4SMatthew Ahrens "bookmark %lld/%lld/%lld/%lld", 2247fd05ac4SMatthew Ahrens i, (longlong_t)bte.be_birth_txg, 2257fd05ac4SMatthew Ahrens (longlong_t)bte.be_zb.zb_objset, 2267fd05ac4SMatthew Ahrens (longlong_t)bte.be_zb.zb_object, 2277fd05ac4SMatthew Ahrens (longlong_t)bte.be_zb.zb_level, 2287fd05ac4SMatthew Ahrens (longlong_t)bte.be_zb.zb_blkid); 22953089ab7Seschrock err = traverse_dataset_destroyed(os->os_spa, &bte.be_bp, 2308b36997aSMatthew Ahrens bte.be_birth_txg, &bte.be_zb, flags, 23153089ab7Seschrock bptree_visit_cb, &ba); 23253089ab7Seschrock if (free) { 2337fd05ac4SMatthew Ahrens /* 2347fd05ac4SMatthew Ahrens * The callback has freed the visited block pointers. 2357fd05ac4SMatthew Ahrens * Record our traversal progress on disk, either by 2367fd05ac4SMatthew Ahrens * updating this record's bookmark, or by logically 2377fd05ac4SMatthew Ahrens * removing this record by advancing bt_begin. 2387fd05ac4SMatthew Ahrens */ 2397fd05ac4SMatthew Ahrens if (err != 0) { 24053089ab7Seschrock /* save bookmark for future resume */ 24153089ab7Seschrock ASSERT3U(bte.be_zb.zb_objset, ==, 24253089ab7Seschrock ZB_DESTROYED_OBJSET); 243fb09f5aaSMadhav Suresh ASSERT0(bte.be_zb.zb_level); 24453089ab7Seschrock dmu_write(os, obj, i * sizeof (bte), 24553089ab7Seschrock sizeof (bte), &bte, tx); 2467fd05ac4SMatthew Ahrens if (err == EIO || err == ECKSUM || 2477fd05ac4SMatthew Ahrens err == ENXIO) { 2487fd05ac4SMatthew Ahrens /* 2497fd05ac4SMatthew Ahrens * Skip the rest of this tree and 2507fd05ac4SMatthew Ahrens * continue on to the next entry. 2517fd05ac4SMatthew Ahrens */ 2527fd05ac4SMatthew Ahrens err = 0; 2537fd05ac4SMatthew Ahrens ioerr = B_TRUE; 2547fd05ac4SMatthew Ahrens } else { 2557fd05ac4SMatthew Ahrens break; 2567fd05ac4SMatthew Ahrens } 2577fd05ac4SMatthew Ahrens } else if (ioerr) { 2588b36997aSMatthew Ahrens /* 2597fd05ac4SMatthew Ahrens * This entry is finished, but there were 2607fd05ac4SMatthew Ahrens * i/o errors on previous entries, so we 2617fd05ac4SMatthew Ahrens * can't adjust bt_begin. Set this entry's 2627fd05ac4SMatthew Ahrens * be_birth_txg such that it will be 2637fd05ac4SMatthew Ahrens * treated as a no-op in future traversals. 2648b36997aSMatthew Ahrens */ 2657fd05ac4SMatthew Ahrens bte.be_birth_txg = UINT64_MAX; 2667fd05ac4SMatthew Ahrens dmu_write(os, obj, i * sizeof (bte), 2677fd05ac4SMatthew Ahrens sizeof (bte), &bte, tx); 2688b36997aSMatthew Ahrens } 2698b36997aSMatthew Ahrens 2707fd05ac4SMatthew Ahrens if (!ioerr) { 2717fd05ac4SMatthew Ahrens ba.ba_phys->bt_begin++; 2727fd05ac4SMatthew Ahrens (void) dmu_free_range(os, obj, 2737fd05ac4SMatthew Ahrens i * sizeof (bte), sizeof (bte), tx); 2747fd05ac4SMatthew Ahrens } 2757fd05ac4SMatthew Ahrens } else if (err != 0) { 2767fd05ac4SMatthew Ahrens break; 27753089ab7Seschrock } 27853089ab7Seschrock } 27953089ab7Seschrock 2807fd05ac4SMatthew Ahrens ASSERT(!free || err != 0 || ioerr || 2817fd05ac4SMatthew Ahrens ba.ba_phys->bt_begin == ba.ba_phys->bt_end); 28253089ab7Seschrock 28353089ab7Seschrock /* if all blocks are free there should be no used space */ 28453089ab7Seschrock if (ba.ba_phys->bt_begin == ba.ba_phys->bt_end) { 2857fd05ac4SMatthew Ahrens if (zfs_free_leak_on_eio) { 2867fd05ac4SMatthew Ahrens ba.ba_phys->bt_bytes = 0; 2877fd05ac4SMatthew Ahrens ba.ba_phys->bt_comp = 0; 2887fd05ac4SMatthew Ahrens ba.ba_phys->bt_uncomp = 0; 2897fd05ac4SMatthew Ahrens } 2907fd05ac4SMatthew Ahrens 291fb09f5aaSMadhav Suresh ASSERT0(ba.ba_phys->bt_bytes); 292fb09f5aaSMadhav Suresh ASSERT0(ba.ba_phys->bt_comp); 293fb09f5aaSMadhav Suresh ASSERT0(ba.ba_phys->bt_uncomp); 29453089ab7Seschrock } 29553089ab7Seschrock 29653089ab7Seschrock dmu_buf_rele(db, FTAG); 29753089ab7Seschrock 29853089ab7Seschrock return (err); 29953089ab7Seschrock } 300