1cde58dbcSMatthew Ahrens /* 2cde58dbcSMatthew Ahrens * CDDL HEADER START 3cde58dbcSMatthew Ahrens * 4cde58dbcSMatthew Ahrens * The contents of this file are subject to the terms of the 5cde58dbcSMatthew Ahrens * Common Development and Distribution License (the "License"). 6cde58dbcSMatthew Ahrens * You may not use this file except in compliance with the License. 7cde58dbcSMatthew Ahrens * 8cde58dbcSMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9cde58dbcSMatthew Ahrens * or http://www.opensolaris.org/os/licensing. 10cde58dbcSMatthew Ahrens * See the License for the specific language governing permissions 11cde58dbcSMatthew Ahrens * and limitations under the License. 12cde58dbcSMatthew Ahrens * 13cde58dbcSMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each 14cde58dbcSMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15cde58dbcSMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the 16cde58dbcSMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying 17cde58dbcSMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18cde58dbcSMatthew Ahrens * 19cde58dbcSMatthew Ahrens * CDDL HEADER END 20cde58dbcSMatthew Ahrens */ 21cde58dbcSMatthew Ahrens /* 22cde58dbcSMatthew Ahrens * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 23*f1745736SMatthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved. 24cde58dbcSMatthew Ahrens */ 25cde58dbcSMatthew Ahrens 26cde58dbcSMatthew Ahrens #include <sys/dsl_dataset.h> 27cde58dbcSMatthew Ahrens #include <sys/dmu.h> 28cde58dbcSMatthew Ahrens #include <sys/refcount.h> 29cde58dbcSMatthew Ahrens #include <sys/zap.h> 30cde58dbcSMatthew Ahrens #include <sys/zfs_context.h> 31cde58dbcSMatthew Ahrens #include <sys/dsl_pool.h> 32cde58dbcSMatthew Ahrens 3319b94df9SMatthew Ahrens /* 3419b94df9SMatthew Ahrens * Deadlist concurrency: 3519b94df9SMatthew Ahrens * 3619b94df9SMatthew Ahrens * Deadlists can only be modified from the syncing thread. 3719b94df9SMatthew Ahrens * 3819b94df9SMatthew Ahrens * Except for dsl_deadlist_insert(), it can only be modified with the 3919b94df9SMatthew Ahrens * dp_config_rwlock held with RW_WRITER. 4019b94df9SMatthew Ahrens * 4119b94df9SMatthew Ahrens * The accessors (dsl_deadlist_space() and dsl_deadlist_space_range()) can 4219b94df9SMatthew Ahrens * be called concurrently, from open context, with the dl_config_rwlock held 4319b94df9SMatthew Ahrens * with RW_READER. 4419b94df9SMatthew Ahrens * 4519b94df9SMatthew Ahrens * Therefore, we only need to provide locking between dsl_deadlist_insert() and 4619b94df9SMatthew Ahrens * the accessors, protecting: 4719b94df9SMatthew Ahrens * dl_phys->dl_used,comp,uncomp 4819b94df9SMatthew Ahrens * and protecting the dl_tree from being loaded. 4919b94df9SMatthew Ahrens * The locking is provided by dl_lock. Note that locking on the bpobj_t 5019b94df9SMatthew Ahrens * provides its own locking, and dl_oldfmt is immutable. 5119b94df9SMatthew Ahrens */ 5219b94df9SMatthew Ahrens 53cde58dbcSMatthew Ahrens static int 54cde58dbcSMatthew Ahrens dsl_deadlist_compare(const void *arg1, const void *arg2) 55cde58dbcSMatthew Ahrens { 56cde58dbcSMatthew Ahrens const dsl_deadlist_entry_t *dle1 = arg1; 57cde58dbcSMatthew Ahrens const dsl_deadlist_entry_t *dle2 = arg2; 58cde58dbcSMatthew Ahrens 59cde58dbcSMatthew Ahrens if (dle1->dle_mintxg < dle2->dle_mintxg) 60cde58dbcSMatthew Ahrens return (-1); 61cde58dbcSMatthew Ahrens else if (dle1->dle_mintxg > dle2->dle_mintxg) 62cde58dbcSMatthew Ahrens return (+1); 63cde58dbcSMatthew Ahrens else 64cde58dbcSMatthew Ahrens return (0); 65cde58dbcSMatthew Ahrens } 66cde58dbcSMatthew Ahrens 67cde58dbcSMatthew Ahrens static void 68cde58dbcSMatthew Ahrens dsl_deadlist_load_tree(dsl_deadlist_t *dl) 69cde58dbcSMatthew Ahrens { 70cde58dbcSMatthew Ahrens zap_cursor_t zc; 71cde58dbcSMatthew Ahrens zap_attribute_t za; 72cde58dbcSMatthew Ahrens 73cde58dbcSMatthew Ahrens ASSERT(!dl->dl_oldfmt); 74cde58dbcSMatthew Ahrens if (dl->dl_havetree) 75cde58dbcSMatthew Ahrens return; 76cde58dbcSMatthew Ahrens 77cde58dbcSMatthew Ahrens avl_create(&dl->dl_tree, dsl_deadlist_compare, 78cde58dbcSMatthew Ahrens sizeof (dsl_deadlist_entry_t), 79cde58dbcSMatthew Ahrens offsetof(dsl_deadlist_entry_t, dle_node)); 80cde58dbcSMatthew Ahrens for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object); 81cde58dbcSMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 82cde58dbcSMatthew Ahrens zap_cursor_advance(&zc)) { 83cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle = kmem_alloc(sizeof (*dle), KM_SLEEP); 84cde58dbcSMatthew Ahrens dle->dle_mintxg = strtonum(za.za_name, NULL); 85b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, 86cde58dbcSMatthew Ahrens za.za_first_integer)); 87cde58dbcSMatthew Ahrens avl_add(&dl->dl_tree, dle); 88cde58dbcSMatthew Ahrens } 89cde58dbcSMatthew Ahrens zap_cursor_fini(&zc); 90cde58dbcSMatthew Ahrens dl->dl_havetree = B_TRUE; 91cde58dbcSMatthew Ahrens } 92cde58dbcSMatthew Ahrens 93cde58dbcSMatthew Ahrens void 94cde58dbcSMatthew Ahrens dsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object) 95cde58dbcSMatthew Ahrens { 96cde58dbcSMatthew Ahrens dmu_object_info_t doi; 97cde58dbcSMatthew Ahrens 98cde58dbcSMatthew Ahrens mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL); 99cde58dbcSMatthew Ahrens dl->dl_os = os; 100cde58dbcSMatthew Ahrens dl->dl_object = object; 101b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, object, dl, &dl->dl_dbuf)); 102cde58dbcSMatthew Ahrens dmu_object_info_from_db(dl->dl_dbuf, &doi); 103cde58dbcSMatthew Ahrens if (doi.doi_type == DMU_OT_BPOBJ) { 104cde58dbcSMatthew Ahrens dmu_buf_rele(dl->dl_dbuf, dl); 105cde58dbcSMatthew Ahrens dl->dl_dbuf = NULL; 106cde58dbcSMatthew Ahrens dl->dl_oldfmt = B_TRUE; 107b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&dl->dl_bpobj, os, object)); 108cde58dbcSMatthew Ahrens return; 109cde58dbcSMatthew Ahrens } 110cde58dbcSMatthew Ahrens 111cde58dbcSMatthew Ahrens dl->dl_oldfmt = B_FALSE; 112cde58dbcSMatthew Ahrens dl->dl_phys = dl->dl_dbuf->db_data; 113cde58dbcSMatthew Ahrens dl->dl_havetree = B_FALSE; 114cde58dbcSMatthew Ahrens } 115cde58dbcSMatthew Ahrens 116cde58dbcSMatthew Ahrens void 117cde58dbcSMatthew Ahrens dsl_deadlist_close(dsl_deadlist_t *dl) 118cde58dbcSMatthew Ahrens { 119cde58dbcSMatthew Ahrens void *cookie = NULL; 120cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle; 121cde58dbcSMatthew Ahrens 122cde58dbcSMatthew Ahrens if (dl->dl_oldfmt) { 123cde58dbcSMatthew Ahrens dl->dl_oldfmt = B_FALSE; 124cde58dbcSMatthew Ahrens bpobj_close(&dl->dl_bpobj); 125cde58dbcSMatthew Ahrens return; 126cde58dbcSMatthew Ahrens } 127cde58dbcSMatthew Ahrens 128cde58dbcSMatthew Ahrens if (dl->dl_havetree) { 129cde58dbcSMatthew Ahrens while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie)) 130cde58dbcSMatthew Ahrens != NULL) { 131cde58dbcSMatthew Ahrens bpobj_close(&dle->dle_bpobj); 132cde58dbcSMatthew Ahrens kmem_free(dle, sizeof (*dle)); 133cde58dbcSMatthew Ahrens } 134cde58dbcSMatthew Ahrens avl_destroy(&dl->dl_tree); 135cde58dbcSMatthew Ahrens } 136cde58dbcSMatthew Ahrens dmu_buf_rele(dl->dl_dbuf, dl); 137cde58dbcSMatthew Ahrens mutex_destroy(&dl->dl_lock); 138cde58dbcSMatthew Ahrens dl->dl_dbuf = NULL; 139cde58dbcSMatthew Ahrens dl->dl_phys = NULL; 140cde58dbcSMatthew Ahrens } 141cde58dbcSMatthew Ahrens 142cde58dbcSMatthew Ahrens uint64_t 143cde58dbcSMatthew Ahrens dsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx) 144cde58dbcSMatthew Ahrens { 145cde58dbcSMatthew Ahrens if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS) 146cde58dbcSMatthew Ahrens return (bpobj_alloc(os, SPA_MAXBLOCKSIZE, tx)); 147cde58dbcSMatthew Ahrens return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR, 148cde58dbcSMatthew Ahrens sizeof (dsl_deadlist_phys_t), tx)); 149cde58dbcSMatthew Ahrens } 150cde58dbcSMatthew Ahrens 151cde58dbcSMatthew Ahrens void 152cde58dbcSMatthew Ahrens dsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx) 153cde58dbcSMatthew Ahrens { 154cde58dbcSMatthew Ahrens dmu_object_info_t doi; 155cde58dbcSMatthew Ahrens zap_cursor_t zc; 156cde58dbcSMatthew Ahrens zap_attribute_t za; 157cde58dbcSMatthew Ahrens 158b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(os, dlobj, &doi)); 159cde58dbcSMatthew Ahrens if (doi.doi_type == DMU_OT_BPOBJ) { 160cde58dbcSMatthew Ahrens bpobj_free(os, dlobj, tx); 161cde58dbcSMatthew Ahrens return; 162cde58dbcSMatthew Ahrens } 163cde58dbcSMatthew Ahrens 164cde58dbcSMatthew Ahrens for (zap_cursor_init(&zc, os, dlobj); 165cde58dbcSMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 166*f1745736SMatthew Ahrens zap_cursor_advance(&zc)) { 167*f1745736SMatthew Ahrens uint64_t obj = za.za_first_integer; 168*f1745736SMatthew Ahrens if (obj == dmu_objset_pool(os)->dp_empty_bpobj) 169*f1745736SMatthew Ahrens bpobj_decr_empty(os, tx); 170*f1745736SMatthew Ahrens else 171*f1745736SMatthew Ahrens bpobj_free(os, obj, tx); 172*f1745736SMatthew Ahrens } 173cde58dbcSMatthew Ahrens zap_cursor_fini(&zc); 174b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_free(os, dlobj, tx)); 175cde58dbcSMatthew Ahrens } 176cde58dbcSMatthew Ahrens 177*f1745736SMatthew Ahrens static void 178*f1745736SMatthew Ahrens dle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle, 179*f1745736SMatthew Ahrens const blkptr_t *bp, dmu_tx_t *tx) 180*f1745736SMatthew Ahrens { 181*f1745736SMatthew Ahrens if (dle->dle_bpobj.bpo_object == 182*f1745736SMatthew Ahrens dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) { 183*f1745736SMatthew Ahrens uint64_t obj = bpobj_alloc(dl->dl_os, SPA_MAXBLOCKSIZE, tx); 184*f1745736SMatthew Ahrens bpobj_close(&dle->dle_bpobj); 185*f1745736SMatthew Ahrens bpobj_decr_empty(dl->dl_os, tx); 186*f1745736SMatthew Ahrens VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj)); 187*f1745736SMatthew Ahrens VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object, 188*f1745736SMatthew Ahrens dle->dle_mintxg, obj, tx)); 189*f1745736SMatthew Ahrens } 190*f1745736SMatthew Ahrens bpobj_enqueue(&dle->dle_bpobj, bp, tx); 191*f1745736SMatthew Ahrens } 192*f1745736SMatthew Ahrens 193*f1745736SMatthew Ahrens static void 194*f1745736SMatthew Ahrens dle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle, 195*f1745736SMatthew Ahrens uint64_t obj, dmu_tx_t *tx) 196*f1745736SMatthew Ahrens { 197*f1745736SMatthew Ahrens if (dle->dle_bpobj.bpo_object != 198*f1745736SMatthew Ahrens dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) { 199*f1745736SMatthew Ahrens bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx); 200*f1745736SMatthew Ahrens } else { 201*f1745736SMatthew Ahrens bpobj_close(&dle->dle_bpobj); 202*f1745736SMatthew Ahrens bpobj_decr_empty(dl->dl_os, tx); 203*f1745736SMatthew Ahrens VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj)); 204*f1745736SMatthew Ahrens VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object, 205*f1745736SMatthew Ahrens dle->dle_mintxg, obj, tx)); 206*f1745736SMatthew Ahrens } 207*f1745736SMatthew Ahrens } 208*f1745736SMatthew Ahrens 209cde58dbcSMatthew Ahrens void 210cde58dbcSMatthew Ahrens dsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, dmu_tx_t *tx) 211cde58dbcSMatthew Ahrens { 212cde58dbcSMatthew Ahrens dsl_deadlist_entry_t dle_tofind; 213cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle; 214cde58dbcSMatthew Ahrens avl_index_t where; 215cde58dbcSMatthew Ahrens 216cde58dbcSMatthew Ahrens if (dl->dl_oldfmt) { 217cde58dbcSMatthew Ahrens bpobj_enqueue(&dl->dl_bpobj, bp, tx); 218cde58dbcSMatthew Ahrens return; 219cde58dbcSMatthew Ahrens } 220cde58dbcSMatthew Ahrens 221cde58dbcSMatthew Ahrens dsl_deadlist_load_tree(dl); 222cde58dbcSMatthew Ahrens 223cde58dbcSMatthew Ahrens dmu_buf_will_dirty(dl->dl_dbuf, tx); 224cde58dbcSMatthew Ahrens mutex_enter(&dl->dl_lock); 225cde58dbcSMatthew Ahrens dl->dl_phys->dl_used += 226cde58dbcSMatthew Ahrens bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp); 227cde58dbcSMatthew Ahrens dl->dl_phys->dl_comp += BP_GET_PSIZE(bp); 228cde58dbcSMatthew Ahrens dl->dl_phys->dl_uncomp += BP_GET_UCSIZE(bp); 229cde58dbcSMatthew Ahrens mutex_exit(&dl->dl_lock); 230cde58dbcSMatthew Ahrens 231cde58dbcSMatthew Ahrens dle_tofind.dle_mintxg = bp->blk_birth; 232cde58dbcSMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 233cde58dbcSMatthew Ahrens if (dle == NULL) 234cde58dbcSMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE); 235cde58dbcSMatthew Ahrens else 236cde58dbcSMatthew Ahrens dle = AVL_PREV(&dl->dl_tree, dle); 237*f1745736SMatthew Ahrens dle_enqueue(dl, dle, bp, tx); 238cde58dbcSMatthew Ahrens } 239cde58dbcSMatthew Ahrens 240cde58dbcSMatthew Ahrens /* 241cde58dbcSMatthew Ahrens * Insert new key in deadlist, which must be > all current entries. 242cde58dbcSMatthew Ahrens * mintxg is not inclusive. 243cde58dbcSMatthew Ahrens */ 244cde58dbcSMatthew Ahrens void 245cde58dbcSMatthew Ahrens dsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx) 246cde58dbcSMatthew Ahrens { 247cde58dbcSMatthew Ahrens uint64_t obj; 248cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle; 249cde58dbcSMatthew Ahrens 250cde58dbcSMatthew Ahrens if (dl->dl_oldfmt) 251cde58dbcSMatthew Ahrens return; 252cde58dbcSMatthew Ahrens 253cde58dbcSMatthew Ahrens dsl_deadlist_load_tree(dl); 254cde58dbcSMatthew Ahrens 255cde58dbcSMatthew Ahrens dle = kmem_alloc(sizeof (*dle), KM_SLEEP); 256cde58dbcSMatthew Ahrens dle->dle_mintxg = mintxg; 257*f1745736SMatthew Ahrens obj = bpobj_alloc_empty(dl->dl_os, SPA_MAXBLOCKSIZE, tx); 258b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj)); 259cde58dbcSMatthew Ahrens avl_add(&dl->dl_tree, dle); 260cde58dbcSMatthew Ahrens 261b420f3adSRichard Lowe VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, dl->dl_object, 262cde58dbcSMatthew Ahrens mintxg, obj, tx)); 263cde58dbcSMatthew Ahrens } 264cde58dbcSMatthew Ahrens 265cde58dbcSMatthew Ahrens /* 266cde58dbcSMatthew Ahrens * Remove this key, merging its entries into the previous key. 267cde58dbcSMatthew Ahrens */ 268cde58dbcSMatthew Ahrens void 269cde58dbcSMatthew Ahrens dsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx) 270cde58dbcSMatthew Ahrens { 271cde58dbcSMatthew Ahrens dsl_deadlist_entry_t dle_tofind; 272cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle, *dle_prev; 273cde58dbcSMatthew Ahrens 274cde58dbcSMatthew Ahrens if (dl->dl_oldfmt) 275cde58dbcSMatthew Ahrens return; 276cde58dbcSMatthew Ahrens 277cde58dbcSMatthew Ahrens dsl_deadlist_load_tree(dl); 278cde58dbcSMatthew Ahrens 279cde58dbcSMatthew Ahrens dle_tofind.dle_mintxg = mintxg; 280cde58dbcSMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, NULL); 281cde58dbcSMatthew Ahrens dle_prev = AVL_PREV(&dl->dl_tree, dle); 282cde58dbcSMatthew Ahrens 283*f1745736SMatthew Ahrens dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx); 284cde58dbcSMatthew Ahrens 285cde58dbcSMatthew Ahrens avl_remove(&dl->dl_tree, dle); 286cde58dbcSMatthew Ahrens bpobj_close(&dle->dle_bpobj); 287cde58dbcSMatthew Ahrens kmem_free(dle, sizeof (*dle)); 288cde58dbcSMatthew Ahrens 289b420f3adSRichard Lowe VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx)); 290cde58dbcSMatthew Ahrens } 291cde58dbcSMatthew Ahrens 292cde58dbcSMatthew Ahrens /* 293cde58dbcSMatthew Ahrens * Walk ds's snapshots to regenerate generate ZAP & AVL. 294cde58dbcSMatthew Ahrens */ 295cde58dbcSMatthew Ahrens static void 296cde58dbcSMatthew Ahrens dsl_deadlist_regenerate(objset_t *os, uint64_t dlobj, 297cde58dbcSMatthew Ahrens uint64_t mrs_obj, dmu_tx_t *tx) 298cde58dbcSMatthew Ahrens { 299cde58dbcSMatthew Ahrens dsl_deadlist_t dl; 300cde58dbcSMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os); 301cde58dbcSMatthew Ahrens 302cde58dbcSMatthew Ahrens dsl_deadlist_open(&dl, os, dlobj); 303cde58dbcSMatthew Ahrens if (dl.dl_oldfmt) { 304cde58dbcSMatthew Ahrens dsl_deadlist_close(&dl); 305cde58dbcSMatthew Ahrens return; 306cde58dbcSMatthew Ahrens } 307cde58dbcSMatthew Ahrens 308cde58dbcSMatthew Ahrens while (mrs_obj != 0) { 309cde58dbcSMatthew Ahrens dsl_dataset_t *ds; 310b420f3adSRichard Lowe VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds)); 311cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&dl, ds->ds_phys->ds_prev_snap_txg, tx); 312cde58dbcSMatthew Ahrens mrs_obj = ds->ds_phys->ds_prev_snap_obj; 313cde58dbcSMatthew Ahrens dsl_dataset_rele(ds, FTAG); 314cde58dbcSMatthew Ahrens } 315cde58dbcSMatthew Ahrens dsl_deadlist_close(&dl); 316cde58dbcSMatthew Ahrens } 317cde58dbcSMatthew Ahrens 318cde58dbcSMatthew Ahrens uint64_t 319cde58dbcSMatthew Ahrens dsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg, 320cde58dbcSMatthew Ahrens uint64_t mrs_obj, dmu_tx_t *tx) 321cde58dbcSMatthew Ahrens { 322cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle; 323cde58dbcSMatthew Ahrens uint64_t newobj; 324cde58dbcSMatthew Ahrens 325cde58dbcSMatthew Ahrens newobj = dsl_deadlist_alloc(dl->dl_os, tx); 326cde58dbcSMatthew Ahrens 327cde58dbcSMatthew Ahrens if (dl->dl_oldfmt) { 328cde58dbcSMatthew Ahrens dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx); 329cde58dbcSMatthew Ahrens return (newobj); 330cde58dbcSMatthew Ahrens } 331cde58dbcSMatthew Ahrens 332cde58dbcSMatthew Ahrens dsl_deadlist_load_tree(dl); 333cde58dbcSMatthew Ahrens 334cde58dbcSMatthew Ahrens for (dle = avl_first(&dl->dl_tree); dle; 335cde58dbcSMatthew Ahrens dle = AVL_NEXT(&dl->dl_tree, dle)) { 336cde58dbcSMatthew Ahrens uint64_t obj; 337cde58dbcSMatthew Ahrens 338cde58dbcSMatthew Ahrens if (dle->dle_mintxg >= maxtxg) 339cde58dbcSMatthew Ahrens break; 340cde58dbcSMatthew Ahrens 341*f1745736SMatthew Ahrens obj = bpobj_alloc_empty(dl->dl_os, SPA_MAXBLOCKSIZE, tx); 342b420f3adSRichard Lowe VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, newobj, 343cde58dbcSMatthew Ahrens dle->dle_mintxg, obj, tx)); 344cde58dbcSMatthew Ahrens } 345cde58dbcSMatthew Ahrens return (newobj); 346cde58dbcSMatthew Ahrens } 347cde58dbcSMatthew Ahrens 348cde58dbcSMatthew Ahrens void 349cde58dbcSMatthew Ahrens dsl_deadlist_space(dsl_deadlist_t *dl, 350cde58dbcSMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 351cde58dbcSMatthew Ahrens { 352cde58dbcSMatthew Ahrens if (dl->dl_oldfmt) { 353b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&dl->dl_bpobj, 354cde58dbcSMatthew Ahrens usedp, compp, uncompp)); 355cde58dbcSMatthew Ahrens return; 356cde58dbcSMatthew Ahrens } 357cde58dbcSMatthew Ahrens 358cde58dbcSMatthew Ahrens mutex_enter(&dl->dl_lock); 359cde58dbcSMatthew Ahrens *usedp = dl->dl_phys->dl_used; 360cde58dbcSMatthew Ahrens *compp = dl->dl_phys->dl_comp; 361cde58dbcSMatthew Ahrens *uncompp = dl->dl_phys->dl_uncomp; 362cde58dbcSMatthew Ahrens mutex_exit(&dl->dl_lock); 363cde58dbcSMatthew Ahrens } 364cde58dbcSMatthew Ahrens 365cde58dbcSMatthew Ahrens /* 366cde58dbcSMatthew Ahrens * return space used in the range (mintxg, maxtxg]. 367cde58dbcSMatthew Ahrens * Includes maxtxg, does not include mintxg. 368cde58dbcSMatthew Ahrens * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is 36919b94df9SMatthew Ahrens * larger than any bp in the deadlist (eg. UINT64_MAX)). 370cde58dbcSMatthew Ahrens */ 371cde58dbcSMatthew Ahrens void 372cde58dbcSMatthew Ahrens dsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg, 373cde58dbcSMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 374cde58dbcSMatthew Ahrens { 3758ac09fceSRichard Lowe dsl_deadlist_entry_t *dle; 37619b94df9SMatthew Ahrens dsl_deadlist_entry_t dle_tofind; 377cde58dbcSMatthew Ahrens avl_index_t where; 378cde58dbcSMatthew Ahrens 379cde58dbcSMatthew Ahrens if (dl->dl_oldfmt) { 380b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space_range(&dl->dl_bpobj, 381cde58dbcSMatthew Ahrens mintxg, maxtxg, usedp, compp, uncompp)); 382cde58dbcSMatthew Ahrens return; 383cde58dbcSMatthew Ahrens } 384cde58dbcSMatthew Ahrens 385cde58dbcSMatthew Ahrens *usedp = *compp = *uncompp = 0; 386cde58dbcSMatthew Ahrens 38719b94df9SMatthew Ahrens mutex_enter(&dl->dl_lock); 38819b94df9SMatthew Ahrens dsl_deadlist_load_tree(dl); 389cde58dbcSMatthew Ahrens dle_tofind.dle_mintxg = mintxg; 390cde58dbcSMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 391cde58dbcSMatthew Ahrens /* 392cde58dbcSMatthew Ahrens * If we don't find this mintxg, there shouldn't be anything 393cde58dbcSMatthew Ahrens * after it either. 394cde58dbcSMatthew Ahrens */ 395cde58dbcSMatthew Ahrens ASSERT(dle != NULL || 396cde58dbcSMatthew Ahrens avl_nearest(&dl->dl_tree, where, AVL_AFTER) == NULL); 39719b94df9SMatthew Ahrens 398cde58dbcSMatthew Ahrens for (; dle && dle->dle_mintxg < maxtxg; 399cde58dbcSMatthew Ahrens dle = AVL_NEXT(&dl->dl_tree, dle)) { 400cde58dbcSMatthew Ahrens uint64_t used, comp, uncomp; 401cde58dbcSMatthew Ahrens 402b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj, 403cde58dbcSMatthew Ahrens &used, &comp, &uncomp)); 404cde58dbcSMatthew Ahrens 405cde58dbcSMatthew Ahrens *usedp += used; 406cde58dbcSMatthew Ahrens *compp += comp; 407cde58dbcSMatthew Ahrens *uncompp += uncomp; 408cde58dbcSMatthew Ahrens } 40919b94df9SMatthew Ahrens mutex_exit(&dl->dl_lock); 410cde58dbcSMatthew Ahrens } 411cde58dbcSMatthew Ahrens 412cde58dbcSMatthew Ahrens static void 413cde58dbcSMatthew Ahrens dsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth, 414cde58dbcSMatthew Ahrens dmu_tx_t *tx) 415cde58dbcSMatthew Ahrens { 416cde58dbcSMatthew Ahrens dsl_deadlist_entry_t dle_tofind; 417cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle; 418cde58dbcSMatthew Ahrens avl_index_t where; 419cde58dbcSMatthew Ahrens uint64_t used, comp, uncomp; 420cde58dbcSMatthew Ahrens bpobj_t bpo; 421cde58dbcSMatthew Ahrens 422b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj)); 423b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&bpo, &used, &comp, &uncomp)); 424cde58dbcSMatthew Ahrens bpobj_close(&bpo); 425cde58dbcSMatthew Ahrens 426cde58dbcSMatthew Ahrens dsl_deadlist_load_tree(dl); 427cde58dbcSMatthew Ahrens 428cde58dbcSMatthew Ahrens dmu_buf_will_dirty(dl->dl_dbuf, tx); 429cde58dbcSMatthew Ahrens mutex_enter(&dl->dl_lock); 430cde58dbcSMatthew Ahrens dl->dl_phys->dl_used += used; 431cde58dbcSMatthew Ahrens dl->dl_phys->dl_comp += comp; 432cde58dbcSMatthew Ahrens dl->dl_phys->dl_uncomp += uncomp; 433cde58dbcSMatthew Ahrens mutex_exit(&dl->dl_lock); 434cde58dbcSMatthew Ahrens 435cde58dbcSMatthew Ahrens dle_tofind.dle_mintxg = birth; 436cde58dbcSMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 437cde58dbcSMatthew Ahrens if (dle == NULL) 438cde58dbcSMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE); 439*f1745736SMatthew Ahrens dle_enqueue_subobj(dl, dle, obj, tx); 440cde58dbcSMatthew Ahrens } 441cde58dbcSMatthew Ahrens 442cde58dbcSMatthew Ahrens static int 443cde58dbcSMatthew Ahrens dsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 444cde58dbcSMatthew Ahrens { 445cde58dbcSMatthew Ahrens dsl_deadlist_t *dl = arg; 446cde58dbcSMatthew Ahrens dsl_deadlist_insert(dl, bp, tx); 447cde58dbcSMatthew Ahrens return (0); 448cde58dbcSMatthew Ahrens } 449cde58dbcSMatthew Ahrens 450cde58dbcSMatthew Ahrens /* 451cde58dbcSMatthew Ahrens * Merge the deadlist pointed to by 'obj' into dl. obj will be left as 452cde58dbcSMatthew Ahrens * an empty deadlist. 453cde58dbcSMatthew Ahrens */ 454cde58dbcSMatthew Ahrens void 455cde58dbcSMatthew Ahrens dsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx) 456cde58dbcSMatthew Ahrens { 457cde58dbcSMatthew Ahrens zap_cursor_t zc; 458cde58dbcSMatthew Ahrens zap_attribute_t za; 459cde58dbcSMatthew Ahrens dmu_buf_t *bonus; 460cde58dbcSMatthew Ahrens dsl_deadlist_phys_t *dlp; 461cde58dbcSMatthew Ahrens dmu_object_info_t doi; 462cde58dbcSMatthew Ahrens 463b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_object_info(dl->dl_os, obj, &doi)); 464cde58dbcSMatthew Ahrens if (doi.doi_type == DMU_OT_BPOBJ) { 465cde58dbcSMatthew Ahrens bpobj_t bpo; 466b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj)); 467b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_iterate(&bpo, 468cde58dbcSMatthew Ahrens dsl_deadlist_insert_cb, dl, tx)); 469cde58dbcSMatthew Ahrens bpobj_close(&bpo); 470cde58dbcSMatthew Ahrens return; 471cde58dbcSMatthew Ahrens } 472cde58dbcSMatthew Ahrens 473cde58dbcSMatthew Ahrens for (zap_cursor_init(&zc, dl->dl_os, obj); 474cde58dbcSMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 475cde58dbcSMatthew Ahrens zap_cursor_advance(&zc)) { 476cde58dbcSMatthew Ahrens uint64_t mintxg = strtonum(za.za_name, NULL); 477cde58dbcSMatthew Ahrens dsl_deadlist_insert_bpobj(dl, za.za_first_integer, mintxg, tx); 478b420f3adSRichard Lowe VERIFY3U(0, ==, zap_remove_int(dl->dl_os, obj, mintxg, tx)); 479cde58dbcSMatthew Ahrens } 480cde58dbcSMatthew Ahrens zap_cursor_fini(&zc); 481cde58dbcSMatthew Ahrens 482b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus)); 483cde58dbcSMatthew Ahrens dlp = bonus->db_data; 484cde58dbcSMatthew Ahrens dmu_buf_will_dirty(bonus, tx); 485cde58dbcSMatthew Ahrens bzero(dlp, sizeof (*dlp)); 486cde58dbcSMatthew Ahrens dmu_buf_rele(bonus, FTAG); 487cde58dbcSMatthew Ahrens } 488cde58dbcSMatthew Ahrens 489cde58dbcSMatthew Ahrens /* 490cde58dbcSMatthew Ahrens * Remove entries on dl that are >= mintxg, and put them on the bpobj. 491cde58dbcSMatthew Ahrens */ 492cde58dbcSMatthew Ahrens void 493cde58dbcSMatthew Ahrens dsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg, 494cde58dbcSMatthew Ahrens dmu_tx_t *tx) 495cde58dbcSMatthew Ahrens { 496cde58dbcSMatthew Ahrens dsl_deadlist_entry_t dle_tofind; 497cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle; 498cde58dbcSMatthew Ahrens avl_index_t where; 499cde58dbcSMatthew Ahrens 500cde58dbcSMatthew Ahrens ASSERT(!dl->dl_oldfmt); 501cde58dbcSMatthew Ahrens dmu_buf_will_dirty(dl->dl_dbuf, tx); 502cde58dbcSMatthew Ahrens dsl_deadlist_load_tree(dl); 503cde58dbcSMatthew Ahrens 504cde58dbcSMatthew Ahrens dle_tofind.dle_mintxg = mintxg; 505cde58dbcSMatthew Ahrens dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 506cde58dbcSMatthew Ahrens if (dle == NULL) 507cde58dbcSMatthew Ahrens dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER); 508cde58dbcSMatthew Ahrens while (dle) { 509cde58dbcSMatthew Ahrens uint64_t used, comp, uncomp; 510cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle_next; 511cde58dbcSMatthew Ahrens 512cde58dbcSMatthew Ahrens bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx); 513cde58dbcSMatthew Ahrens 514b420f3adSRichard Lowe VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj, 515cde58dbcSMatthew Ahrens &used, &comp, &uncomp)); 516cde58dbcSMatthew Ahrens mutex_enter(&dl->dl_lock); 517cde58dbcSMatthew Ahrens ASSERT3U(dl->dl_phys->dl_used, >=, used); 518cde58dbcSMatthew Ahrens ASSERT3U(dl->dl_phys->dl_comp, >=, comp); 519cde58dbcSMatthew Ahrens ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp); 520cde58dbcSMatthew Ahrens dl->dl_phys->dl_used -= used; 521cde58dbcSMatthew Ahrens dl->dl_phys->dl_comp -= comp; 522cde58dbcSMatthew Ahrens dl->dl_phys->dl_uncomp -= uncomp; 523cde58dbcSMatthew Ahrens mutex_exit(&dl->dl_lock); 524cde58dbcSMatthew Ahrens 525b420f3adSRichard Lowe VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, 526cde58dbcSMatthew Ahrens dle->dle_mintxg, tx)); 527cde58dbcSMatthew Ahrens 528cde58dbcSMatthew Ahrens dle_next = AVL_NEXT(&dl->dl_tree, dle); 529cde58dbcSMatthew Ahrens avl_remove(&dl->dl_tree, dle); 530cde58dbcSMatthew Ahrens bpobj_close(&dle->dle_bpobj); 531cde58dbcSMatthew Ahrens kmem_free(dle, sizeof (*dle)); 532cde58dbcSMatthew Ahrens dle = dle_next; 533cde58dbcSMatthew Ahrens } 534cde58dbcSMatthew Ahrens } 535