1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22d6e555bdSGeorge Wilson * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fb09f5aaSMadhav Suresh /* 2686714001SSerapheim Dimitropoulos * Copyright (c) 2012, 2017 by Delphix. All rights reserved. 27fb09f5aaSMadhav Suresh */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <sys/zfs_context.h> 30fa9e4066Sahrens #include <sys/spa.h> 31fa9e4066Sahrens #include <sys/dmu.h> 320713e232SGeorge Wilson #include <sys/dmu_tx.h> 330713e232SGeorge Wilson #include <sys/dnode.h> 340713e232SGeorge Wilson #include <sys/dsl_pool.h> 35ecc2d604Sbonwick #include <sys/zio.h> 36fa9e4066Sahrens #include <sys/space_map.h> 370713e232SGeorge Wilson #include <sys/refcount.h> 380713e232SGeorge Wilson #include <sys/zfeature.h> 39fa9e4066Sahrens 40ecc2d604Sbonwick /* 4186714001SSerapheim Dimitropoulos * Note on space map block size: 4286714001SSerapheim Dimitropoulos * 43b1be2892SMatthew Ahrens * The data for a given space map can be kept on blocks of any size. 44*17f11284SSerapheim Dimitropoulos * Larger blocks entail fewer I/O operations, but they also cause the 45*17f11284SSerapheim Dimitropoulos * DMU to keep more data in-core, and also to waste more I/O bandwidth 46b1be2892SMatthew Ahrens * when only a few blocks have changed since the last transaction group. 47ecc2d604Sbonwick */ 48ecc2d604Sbonwick 49*17f11284SSerapheim Dimitropoulos /* 50*17f11284SSerapheim Dimitropoulos * Enabled whenever we want to stress test the use of double-word 51*17f11284SSerapheim Dimitropoulos * space map entries. 52*17f11284SSerapheim Dimitropoulos */ 53*17f11284SSerapheim Dimitropoulos boolean_t zfs_force_some_double_word_sm_entries = B_FALSE; 54*17f11284SSerapheim Dimitropoulos 55*17f11284SSerapheim Dimitropoulos boolean_t 56*17f11284SSerapheim Dimitropoulos sm_entry_is_debug(uint64_t e) 57*17f11284SSerapheim Dimitropoulos { 58*17f11284SSerapheim Dimitropoulos return (SM_PREFIX_DECODE(e) == SM_DEBUG_PREFIX); 59*17f11284SSerapheim Dimitropoulos } 60*17f11284SSerapheim Dimitropoulos 61*17f11284SSerapheim Dimitropoulos boolean_t 62*17f11284SSerapheim Dimitropoulos sm_entry_is_single_word(uint64_t e) 63*17f11284SSerapheim Dimitropoulos { 64*17f11284SSerapheim Dimitropoulos uint8_t prefix = SM_PREFIX_DECODE(e); 65*17f11284SSerapheim Dimitropoulos return (prefix != SM_DEBUG_PREFIX && prefix != SM2_PREFIX); 66*17f11284SSerapheim Dimitropoulos } 67*17f11284SSerapheim Dimitropoulos 68*17f11284SSerapheim Dimitropoulos boolean_t 69*17f11284SSerapheim Dimitropoulos sm_entry_is_double_word(uint64_t e) 70*17f11284SSerapheim Dimitropoulos { 71*17f11284SSerapheim Dimitropoulos return (SM_PREFIX_DECODE(e) == SM2_PREFIX); 72*17f11284SSerapheim Dimitropoulos } 73*17f11284SSerapheim Dimitropoulos 74ecc2d604Sbonwick /* 755cabbc6bSPrashanth Sreenivasa * Iterate through the space map, invoking the callback on each (non-debug) 765cabbc6bSPrashanth Sreenivasa * space map entry. 77ecc2d604Sbonwick */ 78fa9e4066Sahrens int 795cabbc6bSPrashanth Sreenivasa space_map_iterate(space_map_t *sm, sm_cb_t callback, void *arg) 80fa9e4066Sahrens { 81*17f11284SSerapheim Dimitropoulos uint64_t sm_len = space_map_length(sm); 82*17f11284SSerapheim Dimitropoulos ASSERT3U(sm->sm_blksz, !=, 0); 83*17f11284SSerapheim Dimitropoulos 84*17f11284SSerapheim Dimitropoulos dmu_prefetch(sm->sm_os, space_map_object(sm), 0, 0, sm_len, 85*17f11284SSerapheim Dimitropoulos ZIO_PRIORITY_SYNC_READ); 86*17f11284SSerapheim Dimitropoulos 87*17f11284SSerapheim Dimitropoulos uint64_t blksz = sm->sm_blksz; 880a4e9518Sgw int error = 0; 89*17f11284SSerapheim Dimitropoulos for (uint64_t block_base = 0; block_base < sm_len && error == 0; 90*17f11284SSerapheim Dimitropoulos block_base += blksz) { 91*17f11284SSerapheim Dimitropoulos dmu_buf_t *db; 92*17f11284SSerapheim Dimitropoulos error = dmu_buf_hold(sm->sm_os, space_map_object(sm), 93*17f11284SSerapheim Dimitropoulos block_base, FTAG, &db, DMU_READ_PREFETCH); 94*17f11284SSerapheim Dimitropoulos if (error != 0) 95*17f11284SSerapheim Dimitropoulos return (error); 96fa9e4066Sahrens 97*17f11284SSerapheim Dimitropoulos uint64_t *block_start = db->db_data; 98*17f11284SSerapheim Dimitropoulos uint64_t block_length = MIN(sm_len - block_base, blksz); 99*17f11284SSerapheim Dimitropoulos uint64_t *block_end = block_start + 100*17f11284SSerapheim Dimitropoulos (block_length / sizeof (uint64_t)); 101fa9e4066Sahrens 102*17f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(block_length, sizeof (uint64_t))); 103*17f11284SSerapheim Dimitropoulos VERIFY3U(block_length, !=, 0); 104*17f11284SSerapheim Dimitropoulos ASSERT3U(blksz, ==, db->db_size); 105ecc2d604Sbonwick 106*17f11284SSerapheim Dimitropoulos for (uint64_t *block_cursor = block_start; 107*17f11284SSerapheim Dimitropoulos block_cursor < block_end && error == 0; block_cursor++) { 108*17f11284SSerapheim Dimitropoulos uint64_t e = *block_cursor; 109ecc2d604Sbonwick 110*17f11284SSerapheim Dimitropoulos if (sm_entry_is_debug(e)) /* Skip debug entries */ 111*17f11284SSerapheim Dimitropoulos continue; 112fa9e4066Sahrens 113*17f11284SSerapheim Dimitropoulos uint64_t raw_offset, raw_run, vdev_id; 114*17f11284SSerapheim Dimitropoulos maptype_t type; 115*17f11284SSerapheim Dimitropoulos if (sm_entry_is_single_word(e)) { 116*17f11284SSerapheim Dimitropoulos type = SM_TYPE_DECODE(e); 117*17f11284SSerapheim Dimitropoulos vdev_id = SM_NO_VDEVID; 118*17f11284SSerapheim Dimitropoulos raw_offset = SM_OFFSET_DECODE(e); 119*17f11284SSerapheim Dimitropoulos raw_run = SM_RUN_DECODE(e); 120*17f11284SSerapheim Dimitropoulos } else { 121*17f11284SSerapheim Dimitropoulos /* it is a two-word entry */ 122*17f11284SSerapheim Dimitropoulos ASSERT(sm_entry_is_double_word(e)); 123*17f11284SSerapheim Dimitropoulos raw_run = SM2_RUN_DECODE(e); 124*17f11284SSerapheim Dimitropoulos vdev_id = SM2_VDEV_DECODE(e); 125*17f11284SSerapheim Dimitropoulos 126*17f11284SSerapheim Dimitropoulos /* move on to the second word */ 127*17f11284SSerapheim Dimitropoulos block_cursor++; 128*17f11284SSerapheim Dimitropoulos e = *block_cursor; 129*17f11284SSerapheim Dimitropoulos VERIFY3P(block_cursor, <=, block_end); 130*17f11284SSerapheim Dimitropoulos 131*17f11284SSerapheim Dimitropoulos type = SM2_TYPE_DECODE(e); 132*17f11284SSerapheim Dimitropoulos raw_offset = SM2_OFFSET_DECODE(e); 133*17f11284SSerapheim Dimitropoulos } 134ecc2d604Sbonwick 135*17f11284SSerapheim Dimitropoulos uint64_t entry_offset = (raw_offset << sm->sm_shift) + 136*17f11284SSerapheim Dimitropoulos sm->sm_start; 137*17f11284SSerapheim Dimitropoulos uint64_t entry_run = raw_run << sm->sm_shift; 138fa9e4066Sahrens 139*17f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_offset, 1ULL << sm->sm_shift)); 140*17f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_run, 1ULL << sm->sm_shift)); 141*17f11284SSerapheim Dimitropoulos ASSERT3U(entry_offset, >=, sm->sm_start); 142*17f11284SSerapheim Dimitropoulos ASSERT3U(entry_offset, <, sm->sm_start + sm->sm_size); 143*17f11284SSerapheim Dimitropoulos ASSERT3U(entry_run, <=, sm->sm_size); 144*17f11284SSerapheim Dimitropoulos ASSERT3U(entry_offset + entry_run, <=, 145*17f11284SSerapheim Dimitropoulos sm->sm_start + sm->sm_size); 146fa9e4066Sahrens 147*17f11284SSerapheim Dimitropoulos space_map_entry_t sme = { 148*17f11284SSerapheim Dimitropoulos .sme_type = type, 149*17f11284SSerapheim Dimitropoulos .sme_vdev = vdev_id, 150*17f11284SSerapheim Dimitropoulos .sme_offset = entry_offset, 151*17f11284SSerapheim Dimitropoulos .sme_run = entry_run 152*17f11284SSerapheim Dimitropoulos }; 153*17f11284SSerapheim Dimitropoulos error = callback(&sme, arg); 154*17f11284SSerapheim Dimitropoulos } 155*17f11284SSerapheim Dimitropoulos dmu_buf_rele(db, FTAG); 156*17f11284SSerapheim Dimitropoulos } 157*17f11284SSerapheim Dimitropoulos return (error); 158*17f11284SSerapheim Dimitropoulos } 159fa9e4066Sahrens 160*17f11284SSerapheim Dimitropoulos /* 161*17f11284SSerapheim Dimitropoulos * Reads the entries from the last block of the space map into 162*17f11284SSerapheim Dimitropoulos * buf in reverse order. Populates nwords with number of words 163*17f11284SSerapheim Dimitropoulos * in the last block. 164*17f11284SSerapheim Dimitropoulos * 165*17f11284SSerapheim Dimitropoulos * Refer to block comment within space_map_incremental_destroy() 166*17f11284SSerapheim Dimitropoulos * to understand why this function is needed. 167*17f11284SSerapheim Dimitropoulos */ 168*17f11284SSerapheim Dimitropoulos static int 169*17f11284SSerapheim Dimitropoulos space_map_reversed_last_block_entries(space_map_t *sm, uint64_t *buf, 170*17f11284SSerapheim Dimitropoulos uint64_t bufsz, uint64_t *nwords) 171*17f11284SSerapheim Dimitropoulos { 172*17f11284SSerapheim Dimitropoulos int error = 0; 173*17f11284SSerapheim Dimitropoulos dmu_buf_t *db; 1740713e232SGeorge Wilson 175*17f11284SSerapheim Dimitropoulos /* 176*17f11284SSerapheim Dimitropoulos * Find the offset of the last word in the space map and use 177*17f11284SSerapheim Dimitropoulos * that to read the last block of the space map with 178*17f11284SSerapheim Dimitropoulos * dmu_buf_hold(). 179*17f11284SSerapheim Dimitropoulos */ 180*17f11284SSerapheim Dimitropoulos uint64_t last_word_offset = 181*17f11284SSerapheim Dimitropoulos sm->sm_phys->smp_objsize - sizeof (uint64_t); 182*17f11284SSerapheim Dimitropoulos error = dmu_buf_hold(sm->sm_os, space_map_object(sm), last_word_offset, 183*17f11284SSerapheim Dimitropoulos FTAG, &db, DMU_READ_NO_PREFETCH); 184*17f11284SSerapheim Dimitropoulos if (error != 0) 185*17f11284SSerapheim Dimitropoulos return (error); 186*17f11284SSerapheim Dimitropoulos 187*17f11284SSerapheim Dimitropoulos ASSERT3U(sm->sm_object, ==, db->db_object); 188*17f11284SSerapheim Dimitropoulos ASSERT3U(sm->sm_blksz, ==, db->db_size); 189*17f11284SSerapheim Dimitropoulos ASSERT3U(bufsz, >=, db->db_size); 190*17f11284SSerapheim Dimitropoulos ASSERT(nwords != NULL); 191*17f11284SSerapheim Dimitropoulos 192*17f11284SSerapheim Dimitropoulos uint64_t *words = db->db_data; 193*17f11284SSerapheim Dimitropoulos *nwords = 194*17f11284SSerapheim Dimitropoulos (sm->sm_phys->smp_objsize - db->db_offset) / sizeof (uint64_t); 195*17f11284SSerapheim Dimitropoulos 196*17f11284SSerapheim Dimitropoulos ASSERT3U(*nwords, <=, bufsz / sizeof (uint64_t)); 197*17f11284SSerapheim Dimitropoulos 198*17f11284SSerapheim Dimitropoulos uint64_t n = *nwords; 199*17f11284SSerapheim Dimitropoulos uint64_t j = n - 1; 200*17f11284SSerapheim Dimitropoulos for (uint64_t i = 0; i < n; i++) { 201*17f11284SSerapheim Dimitropoulos uint64_t entry = words[i]; 202*17f11284SSerapheim Dimitropoulos if (sm_entry_is_double_word(entry)) { 203*17f11284SSerapheim Dimitropoulos /* 204*17f11284SSerapheim Dimitropoulos * Since we are populating the buffer backwards 205*17f11284SSerapheim Dimitropoulos * we have to be extra careful and add the two 206*17f11284SSerapheim Dimitropoulos * words of the double-word entry in the right 207*17f11284SSerapheim Dimitropoulos * order. 208*17f11284SSerapheim Dimitropoulos */ 209*17f11284SSerapheim Dimitropoulos ASSERT3U(j, >, 0); 210*17f11284SSerapheim Dimitropoulos buf[j - 1] = entry; 211*17f11284SSerapheim Dimitropoulos 212*17f11284SSerapheim Dimitropoulos i++; 213*17f11284SSerapheim Dimitropoulos ASSERT3U(i, <, n); 214*17f11284SSerapheim Dimitropoulos entry = words[i]; 215*17f11284SSerapheim Dimitropoulos buf[j] = entry; 216*17f11284SSerapheim Dimitropoulos j -= 2; 217*17f11284SSerapheim Dimitropoulos } else { 218*17f11284SSerapheim Dimitropoulos ASSERT(sm_entry_is_debug(entry) || 219*17f11284SSerapheim Dimitropoulos sm_entry_is_single_word(entry)); 220*17f11284SSerapheim Dimitropoulos buf[j] = entry; 221*17f11284SSerapheim Dimitropoulos j--; 222fa9e4066Sahrens } 223fa9e4066Sahrens } 224fa9e4066Sahrens 225*17f11284SSerapheim Dimitropoulos /* 226*17f11284SSerapheim Dimitropoulos * Assert that we wrote backwards all the 227*17f11284SSerapheim Dimitropoulos * way to the beginning of the buffer. 228*17f11284SSerapheim Dimitropoulos */ 229*17f11284SSerapheim Dimitropoulos ASSERT3S(j, ==, -1); 230*17f11284SSerapheim Dimitropoulos 231*17f11284SSerapheim Dimitropoulos dmu_buf_rele(db, FTAG); 2325cabbc6bSPrashanth Sreenivasa return (error); 2335cabbc6bSPrashanth Sreenivasa } 2345cabbc6bSPrashanth Sreenivasa 23586714001SSerapheim Dimitropoulos /* 23686714001SSerapheim Dimitropoulos * Note: This function performs destructive actions - specifically 23786714001SSerapheim Dimitropoulos * it deletes entries from the end of the space map. Thus, callers 23886714001SSerapheim Dimitropoulos * should ensure that they are holding the appropriate locks for 23986714001SSerapheim Dimitropoulos * the space map that they provide. 24086714001SSerapheim Dimitropoulos */ 24186714001SSerapheim Dimitropoulos int 24286714001SSerapheim Dimitropoulos space_map_incremental_destroy(space_map_t *sm, sm_cb_t callback, void *arg, 24386714001SSerapheim Dimitropoulos dmu_tx_t *tx) 24486714001SSerapheim Dimitropoulos { 245*17f11284SSerapheim Dimitropoulos uint64_t bufsz = MAX(sm->sm_blksz, SPA_MINBLOCKSIZE); 246*17f11284SSerapheim Dimitropoulos uint64_t *buf = zio_buf_alloc(bufsz); 24786714001SSerapheim Dimitropoulos 24886714001SSerapheim Dimitropoulos dmu_buf_will_dirty(sm->sm_dbuf, tx); 24986714001SSerapheim Dimitropoulos 25086714001SSerapheim Dimitropoulos /* 251*17f11284SSerapheim Dimitropoulos * Ideally we would want to iterate from the beginning of the 252*17f11284SSerapheim Dimitropoulos * space map to the end in incremental steps. The issue with this 253*17f11284SSerapheim Dimitropoulos * approach is that we don't have any field on-disk that points 254*17f11284SSerapheim Dimitropoulos * us where to start between each step. We could try zeroing out 255*17f11284SSerapheim Dimitropoulos * entries that we've destroyed, but this doesn't work either as 256*17f11284SSerapheim Dimitropoulos * an entry that is 0 is a valid one (ALLOC for range [0x0:0x200]). 257*17f11284SSerapheim Dimitropoulos * 258*17f11284SSerapheim Dimitropoulos * As a result, we destroy its entries incrementally starting from 259*17f11284SSerapheim Dimitropoulos * the end after applying the callback to each of them. 26086714001SSerapheim Dimitropoulos * 261*17f11284SSerapheim Dimitropoulos * The problem with this approach is that we cannot literally 262*17f11284SSerapheim Dimitropoulos * iterate through the words in the space map backwards as we 263*17f11284SSerapheim Dimitropoulos * can't distinguish two-word space map entries from their second 264*17f11284SSerapheim Dimitropoulos * word. Thus we do the following: 26586714001SSerapheim Dimitropoulos * 266*17f11284SSerapheim Dimitropoulos * 1] We get all the entries from the last block of the space map 267*17f11284SSerapheim Dimitropoulos * and put them into a buffer in reverse order. This way the 268*17f11284SSerapheim Dimitropoulos * last entry comes first in the buffer, the second to last is 269*17f11284SSerapheim Dimitropoulos * second, etc. 270*17f11284SSerapheim Dimitropoulos * 2] We iterate through the entries in the buffer and we apply 271*17f11284SSerapheim Dimitropoulos * the callback to each one. As we move from entry to entry we 272*17f11284SSerapheim Dimitropoulos * we decrease the size of the space map, deleting effectively 273*17f11284SSerapheim Dimitropoulos * each entry. 274*17f11284SSerapheim Dimitropoulos * 3] If there are no more entries in the space map or the callback 275*17f11284SSerapheim Dimitropoulos * returns a value other than 0, we stop iterating over the 276*17f11284SSerapheim Dimitropoulos * space map. If there are entries remaining and the callback 277*17f11284SSerapheim Dimitropoulos * returned 0, we go back to step [1]. 27886714001SSerapheim Dimitropoulos */ 279*17f11284SSerapheim Dimitropoulos int error = 0; 280*17f11284SSerapheim Dimitropoulos while (space_map_length(sm) > 0 && error == 0) { 281*17f11284SSerapheim Dimitropoulos uint64_t nwords = 0; 282*17f11284SSerapheim Dimitropoulos error = space_map_reversed_last_block_entries(sm, buf, bufsz, 283*17f11284SSerapheim Dimitropoulos &nwords); 28486714001SSerapheim Dimitropoulos if (error != 0) 28586714001SSerapheim Dimitropoulos break; 28686714001SSerapheim Dimitropoulos 287*17f11284SSerapheim Dimitropoulos ASSERT3U(nwords, <=, bufsz / sizeof (uint64_t)); 28886714001SSerapheim Dimitropoulos 289*17f11284SSerapheim Dimitropoulos for (uint64_t i = 0; i < nwords; i++) { 290*17f11284SSerapheim Dimitropoulos uint64_t e = buf[i]; 29186714001SSerapheim Dimitropoulos 292*17f11284SSerapheim Dimitropoulos if (sm_entry_is_debug(e)) { 29386714001SSerapheim Dimitropoulos sm->sm_phys->smp_objsize -= sizeof (uint64_t); 29486714001SSerapheim Dimitropoulos space_map_update(sm); 29586714001SSerapheim Dimitropoulos continue; 29686714001SSerapheim Dimitropoulos } 29786714001SSerapheim Dimitropoulos 298*17f11284SSerapheim Dimitropoulos int words = 1; 299*17f11284SSerapheim Dimitropoulos uint64_t raw_offset, raw_run, vdev_id; 300*17f11284SSerapheim Dimitropoulos maptype_t type; 301*17f11284SSerapheim Dimitropoulos if (sm_entry_is_single_word(e)) { 302*17f11284SSerapheim Dimitropoulos type = SM_TYPE_DECODE(e); 303*17f11284SSerapheim Dimitropoulos vdev_id = SM_NO_VDEVID; 304*17f11284SSerapheim Dimitropoulos raw_offset = SM_OFFSET_DECODE(e); 305*17f11284SSerapheim Dimitropoulos raw_run = SM_RUN_DECODE(e); 306*17f11284SSerapheim Dimitropoulos } else { 307*17f11284SSerapheim Dimitropoulos ASSERT(sm_entry_is_double_word(e)); 308*17f11284SSerapheim Dimitropoulos words = 2; 309*17f11284SSerapheim Dimitropoulos 310*17f11284SSerapheim Dimitropoulos raw_run = SM2_RUN_DECODE(e); 311*17f11284SSerapheim Dimitropoulos vdev_id = SM2_VDEV_DECODE(e); 312*17f11284SSerapheim Dimitropoulos 313*17f11284SSerapheim Dimitropoulos /* move to the second word */ 314*17f11284SSerapheim Dimitropoulos i++; 315*17f11284SSerapheim Dimitropoulos e = buf[i]; 316*17f11284SSerapheim Dimitropoulos 317*17f11284SSerapheim Dimitropoulos ASSERT3P(i, <=, nwords); 318*17f11284SSerapheim Dimitropoulos 319*17f11284SSerapheim Dimitropoulos type = SM2_TYPE_DECODE(e); 320*17f11284SSerapheim Dimitropoulos raw_offset = SM2_OFFSET_DECODE(e); 321*17f11284SSerapheim Dimitropoulos } 322*17f11284SSerapheim Dimitropoulos 323*17f11284SSerapheim Dimitropoulos uint64_t entry_offset = 324*17f11284SSerapheim Dimitropoulos (raw_offset << sm->sm_shift) + sm->sm_start; 325*17f11284SSerapheim Dimitropoulos uint64_t entry_run = raw_run << sm->sm_shift; 32686714001SSerapheim Dimitropoulos 32786714001SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_offset, 1ULL << sm->sm_shift)); 328*17f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_run, 1ULL << sm->sm_shift)); 32986714001SSerapheim Dimitropoulos VERIFY3U(entry_offset, >=, sm->sm_start); 330*17f11284SSerapheim Dimitropoulos VERIFY3U(entry_offset, <, sm->sm_start + sm->sm_size); 331*17f11284SSerapheim Dimitropoulos VERIFY3U(entry_run, <=, sm->sm_size); 332*17f11284SSerapheim Dimitropoulos VERIFY3U(entry_offset + entry_run, <=, 33386714001SSerapheim Dimitropoulos sm->sm_start + sm->sm_size); 33486714001SSerapheim Dimitropoulos 335*17f11284SSerapheim Dimitropoulos space_map_entry_t sme = { 336*17f11284SSerapheim Dimitropoulos .sme_type = type, 337*17f11284SSerapheim Dimitropoulos .sme_vdev = vdev_id, 338*17f11284SSerapheim Dimitropoulos .sme_offset = entry_offset, 339*17f11284SSerapheim Dimitropoulos .sme_run = entry_run 340*17f11284SSerapheim Dimitropoulos }; 341*17f11284SSerapheim Dimitropoulos error = callback(&sme, arg); 34286714001SSerapheim Dimitropoulos if (error != 0) 34386714001SSerapheim Dimitropoulos break; 34486714001SSerapheim Dimitropoulos 34586714001SSerapheim Dimitropoulos if (type == SM_ALLOC) 346*17f11284SSerapheim Dimitropoulos sm->sm_phys->smp_alloc -= entry_run; 34786714001SSerapheim Dimitropoulos else 348*17f11284SSerapheim Dimitropoulos sm->sm_phys->smp_alloc += entry_run; 349*17f11284SSerapheim Dimitropoulos sm->sm_phys->smp_objsize -= words * sizeof (uint64_t); 35086714001SSerapheim Dimitropoulos space_map_update(sm); 35186714001SSerapheim Dimitropoulos } 35286714001SSerapheim Dimitropoulos } 35386714001SSerapheim Dimitropoulos 354*17f11284SSerapheim Dimitropoulos if (space_map_length(sm) == 0) { 35586714001SSerapheim Dimitropoulos ASSERT0(error); 35686714001SSerapheim Dimitropoulos ASSERT0(sm->sm_phys->smp_objsize); 35786714001SSerapheim Dimitropoulos ASSERT0(sm->sm_alloc); 35886714001SSerapheim Dimitropoulos } 35986714001SSerapheim Dimitropoulos 360*17f11284SSerapheim Dimitropoulos zio_buf_free(buf, bufsz); 36186714001SSerapheim Dimitropoulos return (error); 36286714001SSerapheim Dimitropoulos } 36386714001SSerapheim Dimitropoulos 3645cabbc6bSPrashanth Sreenivasa typedef struct space_map_load_arg { 3655cabbc6bSPrashanth Sreenivasa space_map_t *smla_sm; 3665cabbc6bSPrashanth Sreenivasa range_tree_t *smla_rt; 3675cabbc6bSPrashanth Sreenivasa maptype_t smla_type; 3685cabbc6bSPrashanth Sreenivasa } space_map_load_arg_t; 3695cabbc6bSPrashanth Sreenivasa 3705cabbc6bSPrashanth Sreenivasa static int 371*17f11284SSerapheim Dimitropoulos space_map_load_callback(space_map_entry_t *sme, void *arg) 3725cabbc6bSPrashanth Sreenivasa { 3735cabbc6bSPrashanth Sreenivasa space_map_load_arg_t *smla = arg; 374*17f11284SSerapheim Dimitropoulos if (sme->sme_type == smla->smla_type) { 375*17f11284SSerapheim Dimitropoulos VERIFY3U(range_tree_space(smla->smla_rt) + sme->sme_run, <=, 3765cabbc6bSPrashanth Sreenivasa smla->smla_sm->sm_size); 377*17f11284SSerapheim Dimitropoulos range_tree_add(smla->smla_rt, sme->sme_offset, sme->sme_run); 3785cabbc6bSPrashanth Sreenivasa } else { 379*17f11284SSerapheim Dimitropoulos range_tree_remove(smla->smla_rt, sme->sme_offset, sme->sme_run); 3805cabbc6bSPrashanth Sreenivasa } 3815cabbc6bSPrashanth Sreenivasa 3825cabbc6bSPrashanth Sreenivasa return (0); 3835cabbc6bSPrashanth Sreenivasa } 3845cabbc6bSPrashanth Sreenivasa 3855cabbc6bSPrashanth Sreenivasa /* 3865cabbc6bSPrashanth Sreenivasa * Load the space map disk into the specified range tree. Segments of maptype 3875cabbc6bSPrashanth Sreenivasa * are added to the range tree, other segment types are removed. 3885cabbc6bSPrashanth Sreenivasa */ 3895cabbc6bSPrashanth Sreenivasa int 3905cabbc6bSPrashanth Sreenivasa space_map_load(space_map_t *sm, range_tree_t *rt, maptype_t maptype) 3915cabbc6bSPrashanth Sreenivasa { 3925cabbc6bSPrashanth Sreenivasa uint64_t space; 3935cabbc6bSPrashanth Sreenivasa int err; 3945cabbc6bSPrashanth Sreenivasa space_map_load_arg_t smla; 3955cabbc6bSPrashanth Sreenivasa 3965cabbc6bSPrashanth Sreenivasa VERIFY0(range_tree_space(rt)); 3975cabbc6bSPrashanth Sreenivasa space = space_map_allocated(sm); 3985cabbc6bSPrashanth Sreenivasa 3995cabbc6bSPrashanth Sreenivasa if (maptype == SM_FREE) { 4005cabbc6bSPrashanth Sreenivasa range_tree_add(rt, sm->sm_start, sm->sm_size); 4015cabbc6bSPrashanth Sreenivasa space = sm->sm_size - space; 4025cabbc6bSPrashanth Sreenivasa } 4035cabbc6bSPrashanth Sreenivasa 4045cabbc6bSPrashanth Sreenivasa smla.smla_rt = rt; 4055cabbc6bSPrashanth Sreenivasa smla.smla_sm = sm; 4065cabbc6bSPrashanth Sreenivasa smla.smla_type = maptype; 4075cabbc6bSPrashanth Sreenivasa err = space_map_iterate(sm, space_map_load_callback, &smla); 4085cabbc6bSPrashanth Sreenivasa 4095cabbc6bSPrashanth Sreenivasa if (err == 0) { 4100713e232SGeorge Wilson VERIFY3U(range_tree_space(rt), ==, space); 4115cabbc6bSPrashanth Sreenivasa } else { 4120713e232SGeorge Wilson range_tree_vacate(rt, NULL, NULL); 4135cabbc6bSPrashanth Sreenivasa } 414b8493d5dSvl 4155cabbc6bSPrashanth Sreenivasa return (err); 4160713e232SGeorge Wilson } 417ecc2d604Sbonwick 4180713e232SGeorge Wilson void 4190713e232SGeorge Wilson space_map_histogram_clear(space_map_t *sm) 4200713e232SGeorge Wilson { 4210713e232SGeorge Wilson if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t)) 4220713e232SGeorge Wilson return; 423ecc2d604Sbonwick 4240713e232SGeorge Wilson bzero(sm->sm_phys->smp_histogram, sizeof (sm->sm_phys->smp_histogram)); 4250713e232SGeorge Wilson } 426ecc2d604Sbonwick 4270713e232SGeorge Wilson boolean_t 4280713e232SGeorge Wilson space_map_histogram_verify(space_map_t *sm, range_tree_t *rt) 4290713e232SGeorge Wilson { 4300713e232SGeorge Wilson /* 4310713e232SGeorge Wilson * Verify that the in-core range tree does not have any 4320713e232SGeorge Wilson * ranges smaller than our sm_shift size. 4330713e232SGeorge Wilson */ 4340713e232SGeorge Wilson for (int i = 0; i < sm->sm_shift; i++) { 4350713e232SGeorge Wilson if (rt->rt_histogram[i] != 0) 4360713e232SGeorge Wilson return (B_FALSE); 4370713e232SGeorge Wilson } 4380713e232SGeorge Wilson return (B_TRUE); 439fa9e4066Sahrens } 440fa9e4066Sahrens 441fa9e4066Sahrens void 4420713e232SGeorge Wilson space_map_histogram_add(space_map_t *sm, range_tree_t *rt, dmu_tx_t *tx) 443ecc2d604Sbonwick { 4440713e232SGeorge Wilson int idx = 0; 4450713e232SGeorge Wilson 4460713e232SGeorge Wilson ASSERT(dmu_tx_is_syncing(tx)); 4470713e232SGeorge Wilson VERIFY3U(space_map_object(sm), !=, 0); 448ecc2d604Sbonwick 4490713e232SGeorge Wilson if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t)) 4500713e232SGeorge Wilson return; 451ecc2d604Sbonwick 4520713e232SGeorge Wilson dmu_buf_will_dirty(sm->sm_dbuf, tx); 453ecc2d604Sbonwick 4540713e232SGeorge Wilson ASSERT(space_map_histogram_verify(sm, rt)); 4550713e232SGeorge Wilson /* 4560713e232SGeorge Wilson * Transfer the content of the range tree histogram to the space 4570713e232SGeorge Wilson * map histogram. The space map histogram contains 32 buckets ranging 4580713e232SGeorge Wilson * between 2^sm_shift to 2^(32+sm_shift-1). The range tree, 4590713e232SGeorge Wilson * however, can represent ranges from 2^0 to 2^63. Since the space 4600713e232SGeorge Wilson * map only cares about allocatable blocks (minimum of sm_shift) we 4610713e232SGeorge Wilson * can safely ignore all ranges in the range tree smaller than sm_shift. 4620713e232SGeorge Wilson */ 4630713e232SGeorge Wilson for (int i = sm->sm_shift; i < RANGE_TREE_HISTOGRAM_SIZE; i++) { 4640713e232SGeorge Wilson 4650713e232SGeorge Wilson /* 4660713e232SGeorge Wilson * Since the largest histogram bucket in the space map is 4670713e232SGeorge Wilson * 2^(32+sm_shift-1), we need to normalize the values in 4680713e232SGeorge Wilson * the range tree for any bucket larger than that size. For 4690713e232SGeorge Wilson * example given an sm_shift of 9, ranges larger than 2^40 4700713e232SGeorge Wilson * would get normalized as if they were 1TB ranges. Assume 4710713e232SGeorge Wilson * the range tree had a count of 5 in the 2^44 (16TB) bucket, 4720713e232SGeorge Wilson * the calculation below would normalize this to 5 * 2^4 (16). 4730713e232SGeorge Wilson */ 4740713e232SGeorge Wilson ASSERT3U(i, >=, idx + sm->sm_shift); 4750713e232SGeorge Wilson sm->sm_phys->smp_histogram[idx] += 4760713e232SGeorge Wilson rt->rt_histogram[i] << (i - idx - sm->sm_shift); 4770713e232SGeorge Wilson 4780713e232SGeorge Wilson /* 4790713e232SGeorge Wilson * Increment the space map's index as long as we haven't 4800713e232SGeorge Wilson * reached the maximum bucket size. Accumulate all ranges 4810713e232SGeorge Wilson * larger than the max bucket size into the last bucket. 4820713e232SGeorge Wilson */ 4832e4c9986SGeorge Wilson if (idx < SPACE_MAP_HISTOGRAM_SIZE - 1) { 4840713e232SGeorge Wilson ASSERT3U(idx + sm->sm_shift, ==, i); 4850713e232SGeorge Wilson idx++; 4862e4c9986SGeorge Wilson ASSERT3U(idx, <, SPACE_MAP_HISTOGRAM_SIZE); 4870713e232SGeorge Wilson } 4880713e232SGeorge Wilson } 489d6e555bdSGeorge Wilson } 490d6e555bdSGeorge Wilson 491*17f11284SSerapheim Dimitropoulos static void 492*17f11284SSerapheim Dimitropoulos space_map_write_intro_debug(space_map_t *sm, maptype_t maptype, dmu_tx_t *tx) 493ecc2d604Sbonwick { 494*17f11284SSerapheim Dimitropoulos dmu_buf_will_dirty(sm->sm_dbuf, tx); 495*17f11284SSerapheim Dimitropoulos 496*17f11284SSerapheim Dimitropoulos uint64_t dentry = SM_PREFIX_ENCODE(SM_DEBUG_PREFIX) | 497*17f11284SSerapheim Dimitropoulos SM_DEBUG_ACTION_ENCODE(maptype) | 498*17f11284SSerapheim Dimitropoulos SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(tx->tx_pool->dp_spa)) | 499*17f11284SSerapheim Dimitropoulos SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx)); 500*17f11284SSerapheim Dimitropoulos 501*17f11284SSerapheim Dimitropoulos dmu_write(sm->sm_os, space_map_object(sm), sm->sm_phys->smp_objsize, 502*17f11284SSerapheim Dimitropoulos sizeof (dentry), &dentry, tx); 503*17f11284SSerapheim Dimitropoulos 504*17f11284SSerapheim Dimitropoulos sm->sm_phys->smp_objsize += sizeof (dentry); 505*17f11284SSerapheim Dimitropoulos } 506*17f11284SSerapheim Dimitropoulos 507*17f11284SSerapheim Dimitropoulos /* 508*17f11284SSerapheim Dimitropoulos * Writes one or more entries given a segment. 509*17f11284SSerapheim Dimitropoulos * 510*17f11284SSerapheim Dimitropoulos * Note: The function may release the dbuf from the pointer initially 511*17f11284SSerapheim Dimitropoulos * passed to it, and return a different dbuf. Also, the space map's 512*17f11284SSerapheim Dimitropoulos * dbuf must be dirty for the changes in sm_phys to take effect. 513*17f11284SSerapheim Dimitropoulos */ 514*17f11284SSerapheim Dimitropoulos static void 515*17f11284SSerapheim Dimitropoulos space_map_write_seg(space_map_t *sm, range_seg_t *rs, maptype_t maptype, 516*17f11284SSerapheim Dimitropoulos uint64_t vdev_id, uint8_t words, dmu_buf_t **dbp, void *tag, dmu_tx_t *tx) 517*17f11284SSerapheim Dimitropoulos { 518*17f11284SSerapheim Dimitropoulos ASSERT3U(words, !=, 0); 519*17f11284SSerapheim Dimitropoulos ASSERT3U(words, <=, 2); 520*17f11284SSerapheim Dimitropoulos 521*17f11284SSerapheim Dimitropoulos /* ensure the vdev_id can be represented by the space map */ 522*17f11284SSerapheim Dimitropoulos ASSERT3U(vdev_id, <=, SM_NO_VDEVID); 523*17f11284SSerapheim Dimitropoulos 524*17f11284SSerapheim Dimitropoulos /* 525*17f11284SSerapheim Dimitropoulos * if this is a single word entry, ensure that no vdev was 526*17f11284SSerapheim Dimitropoulos * specified. 527*17f11284SSerapheim Dimitropoulos */ 528*17f11284SSerapheim Dimitropoulos IMPLY(words == 1, vdev_id == SM_NO_VDEVID); 529*17f11284SSerapheim Dimitropoulos 530*17f11284SSerapheim Dimitropoulos dmu_buf_t *db = *dbp; 531*17f11284SSerapheim Dimitropoulos ASSERT3U(db->db_size, ==, sm->sm_blksz); 532*17f11284SSerapheim Dimitropoulos 533*17f11284SSerapheim Dimitropoulos uint64_t *block_base = db->db_data; 534*17f11284SSerapheim Dimitropoulos uint64_t *block_end = block_base + (sm->sm_blksz / sizeof (uint64_t)); 535*17f11284SSerapheim Dimitropoulos uint64_t *block_cursor = block_base + 536*17f11284SSerapheim Dimitropoulos (sm->sm_phys->smp_objsize - db->db_offset) / sizeof (uint64_t); 537*17f11284SSerapheim Dimitropoulos 538*17f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, <=, block_end); 539*17f11284SSerapheim Dimitropoulos 540*17f11284SSerapheim Dimitropoulos uint64_t size = (rs->rs_end - rs->rs_start) >> sm->sm_shift; 541*17f11284SSerapheim Dimitropoulos uint64_t start = (rs->rs_start - sm->sm_start) >> sm->sm_shift; 542*17f11284SSerapheim Dimitropoulos uint64_t run_max = (words == 2) ? SM2_RUN_MAX : SM_RUN_MAX; 543*17f11284SSerapheim Dimitropoulos 544*17f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_start, >=, sm->sm_start); 545*17f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_start, <, sm->sm_start + sm->sm_size); 546*17f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_end - rs->rs_start, <=, sm->sm_size); 547*17f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_end, <=, sm->sm_start + sm->sm_size); 548*17f11284SSerapheim Dimitropoulos 549*17f11284SSerapheim Dimitropoulos while (size != 0) { 550*17f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, <=, block_end); 551*17f11284SSerapheim Dimitropoulos 552*17f11284SSerapheim Dimitropoulos /* 553*17f11284SSerapheim Dimitropoulos * If we are at the end of this block, flush it and start 554*17f11284SSerapheim Dimitropoulos * writing again from the beginning. 555*17f11284SSerapheim Dimitropoulos */ 556*17f11284SSerapheim Dimitropoulos if (block_cursor == block_end) { 557*17f11284SSerapheim Dimitropoulos dmu_buf_rele(db, tag); 558ecc2d604Sbonwick 559*17f11284SSerapheim Dimitropoulos uint64_t next_word_offset = sm->sm_phys->smp_objsize; 560*17f11284SSerapheim Dimitropoulos VERIFY0(dmu_buf_hold(sm->sm_os, 561*17f11284SSerapheim Dimitropoulos space_map_object(sm), next_word_offset, 562*17f11284SSerapheim Dimitropoulos tag, &db, DMU_READ_PREFETCH)); 563*17f11284SSerapheim Dimitropoulos dmu_buf_will_dirty(db, tx); 564*17f11284SSerapheim Dimitropoulos 565*17f11284SSerapheim Dimitropoulos /* update caller's dbuf */ 566*17f11284SSerapheim Dimitropoulos *dbp = db; 567*17f11284SSerapheim Dimitropoulos 568*17f11284SSerapheim Dimitropoulos ASSERT3U(db->db_size, ==, sm->sm_blksz); 569*17f11284SSerapheim Dimitropoulos 570*17f11284SSerapheim Dimitropoulos block_base = db->db_data; 571*17f11284SSerapheim Dimitropoulos block_cursor = block_base; 572*17f11284SSerapheim Dimitropoulos block_end = block_base + 573*17f11284SSerapheim Dimitropoulos (db->db_size / sizeof (uint64_t)); 574*17f11284SSerapheim Dimitropoulos } 575*17f11284SSerapheim Dimitropoulos 576*17f11284SSerapheim Dimitropoulos /* 577*17f11284SSerapheim Dimitropoulos * If we are writing a two-word entry and we only have one 578*17f11284SSerapheim Dimitropoulos * word left on this block, just pad it with an empty debug 579*17f11284SSerapheim Dimitropoulos * entry and write the two-word entry in the next block. 580*17f11284SSerapheim Dimitropoulos */ 581*17f11284SSerapheim Dimitropoulos uint64_t *next_entry = block_cursor + 1; 582*17f11284SSerapheim Dimitropoulos if (next_entry == block_end && words > 1) { 583*17f11284SSerapheim Dimitropoulos ASSERT3U(words, ==, 2); 584*17f11284SSerapheim Dimitropoulos *block_cursor = SM_PREFIX_ENCODE(SM_DEBUG_PREFIX) | 585*17f11284SSerapheim Dimitropoulos SM_DEBUG_ACTION_ENCODE(0) | 586*17f11284SSerapheim Dimitropoulos SM_DEBUG_SYNCPASS_ENCODE(0) | 587*17f11284SSerapheim Dimitropoulos SM_DEBUG_TXG_ENCODE(0); 588*17f11284SSerapheim Dimitropoulos block_cursor++; 589*17f11284SSerapheim Dimitropoulos sm->sm_phys->smp_objsize += sizeof (uint64_t); 590*17f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, ==, block_end); 591*17f11284SSerapheim Dimitropoulos continue; 592*17f11284SSerapheim Dimitropoulos } 593*17f11284SSerapheim Dimitropoulos 594*17f11284SSerapheim Dimitropoulos uint64_t run_len = MIN(size, run_max); 595*17f11284SSerapheim Dimitropoulos switch (words) { 596*17f11284SSerapheim Dimitropoulos case 1: 597*17f11284SSerapheim Dimitropoulos *block_cursor = SM_OFFSET_ENCODE(start) | 598*17f11284SSerapheim Dimitropoulos SM_TYPE_ENCODE(maptype) | 599*17f11284SSerapheim Dimitropoulos SM_RUN_ENCODE(run_len); 600*17f11284SSerapheim Dimitropoulos block_cursor++; 601*17f11284SSerapheim Dimitropoulos break; 602*17f11284SSerapheim Dimitropoulos case 2: 603*17f11284SSerapheim Dimitropoulos /* write the first word of the entry */ 604*17f11284SSerapheim Dimitropoulos *block_cursor = SM_PREFIX_ENCODE(SM2_PREFIX) | 605*17f11284SSerapheim Dimitropoulos SM2_RUN_ENCODE(run_len) | 606*17f11284SSerapheim Dimitropoulos SM2_VDEV_ENCODE(vdev_id); 607*17f11284SSerapheim Dimitropoulos block_cursor++; 608*17f11284SSerapheim Dimitropoulos 609*17f11284SSerapheim Dimitropoulos /* move on to the second word of the entry */ 610*17f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, <, block_end); 611*17f11284SSerapheim Dimitropoulos *block_cursor = SM2_TYPE_ENCODE(maptype) | 612*17f11284SSerapheim Dimitropoulos SM2_OFFSET_ENCODE(start); 613*17f11284SSerapheim Dimitropoulos block_cursor++; 614*17f11284SSerapheim Dimitropoulos break; 615*17f11284SSerapheim Dimitropoulos default: 616*17f11284SSerapheim Dimitropoulos panic("%d-word space map entries are not supported", 617*17f11284SSerapheim Dimitropoulos words); 618*17f11284SSerapheim Dimitropoulos break; 619*17f11284SSerapheim Dimitropoulos } 620*17f11284SSerapheim Dimitropoulos sm->sm_phys->smp_objsize += words * sizeof (uint64_t); 621*17f11284SSerapheim Dimitropoulos 622*17f11284SSerapheim Dimitropoulos start += run_len; 623*17f11284SSerapheim Dimitropoulos size -= run_len; 624*17f11284SSerapheim Dimitropoulos } 625*17f11284SSerapheim Dimitropoulos ASSERT0(size); 626*17f11284SSerapheim Dimitropoulos 627*17f11284SSerapheim Dimitropoulos } 628*17f11284SSerapheim Dimitropoulos 629*17f11284SSerapheim Dimitropoulos /* 630*17f11284SSerapheim Dimitropoulos * Note: The space map's dbuf must be dirty for the changes in sm_phys to 631*17f11284SSerapheim Dimitropoulos * take effect. 632*17f11284SSerapheim Dimitropoulos */ 633*17f11284SSerapheim Dimitropoulos static void 634*17f11284SSerapheim Dimitropoulos space_map_write_impl(space_map_t *sm, range_tree_t *rt, maptype_t maptype, 635*17f11284SSerapheim Dimitropoulos uint64_t vdev_id, dmu_tx_t *tx) 636*17f11284SSerapheim Dimitropoulos { 637*17f11284SSerapheim Dimitropoulos spa_t *spa = tx->tx_pool->dp_spa; 638*17f11284SSerapheim Dimitropoulos dmu_buf_t *db; 639*17f11284SSerapheim Dimitropoulos 640*17f11284SSerapheim Dimitropoulos space_map_write_intro_debug(sm, maptype, tx); 641*17f11284SSerapheim Dimitropoulos 642*17f11284SSerapheim Dimitropoulos #ifdef DEBUG 6430713e232SGeorge Wilson /* 644*17f11284SSerapheim Dimitropoulos * We do this right after we write the intro debug entry 645*17f11284SSerapheim Dimitropoulos * because the estimate does not take it into account. 6460713e232SGeorge Wilson */ 647*17f11284SSerapheim Dimitropoulos uint64_t initial_objsize = sm->sm_phys->smp_objsize; 648*17f11284SSerapheim Dimitropoulos uint64_t estimated_growth = 649*17f11284SSerapheim Dimitropoulos space_map_estimate_optimal_size(sm, rt, SM_NO_VDEVID); 650*17f11284SSerapheim Dimitropoulos uint64_t estimated_final_objsize = initial_objsize + estimated_growth; 651*17f11284SSerapheim Dimitropoulos #endif 652ecc2d604Sbonwick 6530713e232SGeorge Wilson /* 654*17f11284SSerapheim Dimitropoulos * Find the offset right after the last word in the space map 655*17f11284SSerapheim Dimitropoulos * and use that to get a hold of the last block, so we can 656*17f11284SSerapheim Dimitropoulos * start appending to it. 6570713e232SGeorge Wilson */ 658*17f11284SSerapheim Dimitropoulos uint64_t next_word_offset = sm->sm_phys->smp_objsize; 659*17f11284SSerapheim Dimitropoulos VERIFY0(dmu_buf_hold(sm->sm_os, space_map_object(sm), 660*17f11284SSerapheim Dimitropoulos next_word_offset, FTAG, &db, DMU_READ_PREFETCH)); 661*17f11284SSerapheim Dimitropoulos ASSERT3U(db->db_size, ==, sm->sm_blksz); 662*17f11284SSerapheim Dimitropoulos 663*17f11284SSerapheim Dimitropoulos dmu_buf_will_dirty(db, tx); 664*17f11284SSerapheim Dimitropoulos 665*17f11284SSerapheim Dimitropoulos avl_tree_t *t = &rt->rt_root; 666*17f11284SSerapheim Dimitropoulos for (range_seg_t *rs = avl_first(t); rs != NULL; rs = AVL_NEXT(t, rs)) { 667*17f11284SSerapheim Dimitropoulos uint64_t offset = (rs->rs_start - sm->sm_start) >> sm->sm_shift; 668*17f11284SSerapheim Dimitropoulos uint64_t length = (rs->rs_end - rs->rs_start) >> sm->sm_shift; 669*17f11284SSerapheim Dimitropoulos uint8_t words = 1; 670*17f11284SSerapheim Dimitropoulos 671*17f11284SSerapheim Dimitropoulos /* 672*17f11284SSerapheim Dimitropoulos * We only write two-word entries when both of the following 673*17f11284SSerapheim Dimitropoulos * are true: 674*17f11284SSerapheim Dimitropoulos * 675*17f11284SSerapheim Dimitropoulos * [1] The feature is enabled. 676*17f11284SSerapheim Dimitropoulos * [2] The offset or run is too big for a single-word entry, 677*17f11284SSerapheim Dimitropoulos * or the vdev_id is set (meaning not equal to 678*17f11284SSerapheim Dimitropoulos * SM_NO_VDEVID). 679*17f11284SSerapheim Dimitropoulos * 680*17f11284SSerapheim Dimitropoulos * Note that for purposes of testing we've added the case that 681*17f11284SSerapheim Dimitropoulos * we write two-word entries occasionally when the feature is 682*17f11284SSerapheim Dimitropoulos * enabled and zfs_force_some_double_word_sm_entries has been 683*17f11284SSerapheim Dimitropoulos * set. 684*17f11284SSerapheim Dimitropoulos */ 685*17f11284SSerapheim Dimitropoulos if (spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_V2) && 686*17f11284SSerapheim Dimitropoulos (offset >= (1ULL << SM_OFFSET_BITS) || 687*17f11284SSerapheim Dimitropoulos length > SM_RUN_MAX || 688*17f11284SSerapheim Dimitropoulos vdev_id != SM_NO_VDEVID || 689*17f11284SSerapheim Dimitropoulos (zfs_force_some_double_word_sm_entries && 690*17f11284SSerapheim Dimitropoulos spa_get_random(100) == 0))) 691*17f11284SSerapheim Dimitropoulos words = 2; 692*17f11284SSerapheim Dimitropoulos 693*17f11284SSerapheim Dimitropoulos space_map_write_seg(sm, rs, maptype, vdev_id, words, 694*17f11284SSerapheim Dimitropoulos &db, FTAG, tx); 6950713e232SGeorge Wilson } 696*17f11284SSerapheim Dimitropoulos 697*17f11284SSerapheim Dimitropoulos dmu_buf_rele(db, FTAG); 698*17f11284SSerapheim Dimitropoulos 699*17f11284SSerapheim Dimitropoulos #ifdef DEBUG 700*17f11284SSerapheim Dimitropoulos /* 701*17f11284SSerapheim Dimitropoulos * We expect our estimation to be based on the worst case 702*17f11284SSerapheim Dimitropoulos * scenario [see comment in space_map_estimate_optimal_size()]. 703*17f11284SSerapheim Dimitropoulos * Therefore we expect the actual objsize to be equal or less 704*17f11284SSerapheim Dimitropoulos * than whatever we estimated it to be. 705*17f11284SSerapheim Dimitropoulos */ 706*17f11284SSerapheim Dimitropoulos ASSERT3U(estimated_final_objsize, >=, sm->sm_phys->smp_objsize); 707*17f11284SSerapheim Dimitropoulos #endif 708ecc2d604Sbonwick } 709ecc2d604Sbonwick 710*17f11284SSerapheim Dimitropoulos /* 711*17f11284SSerapheim Dimitropoulos * Note: This function manipulates the state of the given space map but 712*17f11284SSerapheim Dimitropoulos * does not hold any locks implicitly. Thus the caller is responsible 713*17f11284SSerapheim Dimitropoulos * for synchronizing writes to the space map. 714*17f11284SSerapheim Dimitropoulos */ 715ecc2d604Sbonwick void 7160713e232SGeorge Wilson space_map_write(space_map_t *sm, range_tree_t *rt, maptype_t maptype, 717*17f11284SSerapheim Dimitropoulos uint64_t vdev_id, dmu_tx_t *tx) 718fa9e4066Sahrens { 7190713e232SGeorge Wilson objset_t *os = sm->sm_os; 720fa9e4066Sahrens 7210713e232SGeorge Wilson ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 7220713e232SGeorge Wilson VERIFY3U(space_map_object(sm), !=, 0); 723*17f11284SSerapheim Dimitropoulos 7240713e232SGeorge Wilson dmu_buf_will_dirty(sm->sm_dbuf, tx); 725fa9e4066Sahrens 7260713e232SGeorge Wilson /* 7270713e232SGeorge Wilson * This field is no longer necessary since the in-core space map 7280713e232SGeorge Wilson * now contains the object number but is maintained for backwards 7290713e232SGeorge Wilson * compatibility. 7300713e232SGeorge Wilson */ 7310713e232SGeorge Wilson sm->sm_phys->smp_object = sm->sm_object; 732fa9e4066Sahrens 73386714001SSerapheim Dimitropoulos if (range_tree_is_empty(rt)) { 7340713e232SGeorge Wilson VERIFY3U(sm->sm_object, ==, sm->sm_phys->smp_object); 7350713e232SGeorge Wilson return; 7360713e232SGeorge Wilson } 737fa9e4066Sahrens 738ecc2d604Sbonwick if (maptype == SM_ALLOC) 7390713e232SGeorge Wilson sm->sm_phys->smp_alloc += range_tree_space(rt); 740ecc2d604Sbonwick else 7410713e232SGeorge Wilson sm->sm_phys->smp_alloc -= range_tree_space(rt); 742ecc2d604Sbonwick 743*17f11284SSerapheim Dimitropoulos uint64_t nodes = avl_numnodes(&rt->rt_root); 744*17f11284SSerapheim Dimitropoulos uint64_t rt_space = range_tree_space(rt); 7450713e232SGeorge Wilson 746*17f11284SSerapheim Dimitropoulos space_map_write_impl(sm, rt, maptype, vdev_id, tx); 747fa9e4066Sahrens 74801f55e48SGeorge Wilson /* 74901f55e48SGeorge Wilson * Ensure that the space_map's accounting wasn't changed 75001f55e48SGeorge Wilson * while we were in the middle of writing it out. 75101f55e48SGeorge Wilson */ 7520713e232SGeorge Wilson VERIFY3U(nodes, ==, avl_numnodes(&rt->rt_root)); 7530713e232SGeorge Wilson VERIFY3U(range_tree_space(rt), ==, rt_space); 754fa9e4066Sahrens } 755fa9e4066Sahrens 7560713e232SGeorge Wilson static int 7570713e232SGeorge Wilson space_map_open_impl(space_map_t *sm) 758fa9e4066Sahrens { 7590713e232SGeorge Wilson int error; 7600713e232SGeorge Wilson u_longlong_t blocks; 761fa9e4066Sahrens 7620713e232SGeorge Wilson error = dmu_bonus_hold(sm->sm_os, sm->sm_object, sm, &sm->sm_dbuf); 7630713e232SGeorge Wilson if (error) 7640713e232SGeorge Wilson return (error); 7650713e232SGeorge Wilson 7660713e232SGeorge Wilson dmu_object_size_from_db(sm->sm_dbuf, &sm->sm_blksz, &blocks); 7670713e232SGeorge Wilson sm->sm_phys = sm->sm_dbuf->db_data; 7680713e232SGeorge Wilson return (0); 769fa9e4066Sahrens } 7708ad4d6ddSJeff Bonwick 7710713e232SGeorge Wilson int 7720713e232SGeorge Wilson space_map_open(space_map_t **smp, objset_t *os, uint64_t object, 7735cabbc6bSPrashanth Sreenivasa uint64_t start, uint64_t size, uint8_t shift) 7748ad4d6ddSJeff Bonwick { 7750713e232SGeorge Wilson space_map_t *sm; 7760713e232SGeorge Wilson int error; 7778ad4d6ddSJeff Bonwick 7780713e232SGeorge Wilson ASSERT(*smp == NULL); 7790713e232SGeorge Wilson ASSERT(os != NULL); 7800713e232SGeorge Wilson ASSERT(object != 0); 7818ad4d6ddSJeff Bonwick 7820713e232SGeorge Wilson sm = kmem_zalloc(sizeof (space_map_t), KM_SLEEP); 7838ad4d6ddSJeff Bonwick 7840713e232SGeorge Wilson sm->sm_start = start; 7850713e232SGeorge Wilson sm->sm_size = size; 7860713e232SGeorge Wilson sm->sm_shift = shift; 7870713e232SGeorge Wilson sm->sm_os = os; 7880713e232SGeorge Wilson sm->sm_object = object; 7898ad4d6ddSJeff Bonwick 7900713e232SGeorge Wilson error = space_map_open_impl(sm); 7910713e232SGeorge Wilson if (error != 0) { 7920713e232SGeorge Wilson space_map_close(sm); 7930713e232SGeorge Wilson return (error); 7940713e232SGeorge Wilson } 7950713e232SGeorge Wilson *smp = sm; 7960713e232SGeorge Wilson 7970713e232SGeorge Wilson return (0); 7988ad4d6ddSJeff Bonwick } 7998ad4d6ddSJeff Bonwick 8008ad4d6ddSJeff Bonwick void 8010713e232SGeorge Wilson space_map_close(space_map_t *sm) 8028ad4d6ddSJeff Bonwick { 8030713e232SGeorge Wilson if (sm == NULL) 8040713e232SGeorge Wilson return; 8058ad4d6ddSJeff Bonwick 8060713e232SGeorge Wilson if (sm->sm_dbuf != NULL) 8070713e232SGeorge Wilson dmu_buf_rele(sm->sm_dbuf, sm); 8080713e232SGeorge Wilson sm->sm_dbuf = NULL; 8090713e232SGeorge Wilson sm->sm_phys = NULL; 8108ad4d6ddSJeff Bonwick 8110713e232SGeorge Wilson kmem_free(sm, sizeof (*sm)); 8128ad4d6ddSJeff Bonwick } 8138ad4d6ddSJeff Bonwick 8148ad4d6ddSJeff Bonwick void 81586714001SSerapheim Dimitropoulos space_map_truncate(space_map_t *sm, int blocksize, dmu_tx_t *tx) 8168ad4d6ddSJeff Bonwick { 8170713e232SGeorge Wilson objset_t *os = sm->sm_os; 8180713e232SGeorge Wilson spa_t *spa = dmu_objset_spa(os); 8190713e232SGeorge Wilson dmu_object_info_t doi; 8200713e232SGeorge Wilson 8210713e232SGeorge Wilson ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 8220713e232SGeorge Wilson ASSERT(dmu_tx_is_syncing(tx)); 8233991b535SGeorge Wilson VERIFY3U(dmu_tx_get_txg(tx), <=, spa_final_dirty_txg(spa)); 8240713e232SGeorge Wilson 8250713e232SGeorge Wilson dmu_object_info_from_db(sm->sm_dbuf, &doi); 8260713e232SGeorge Wilson 827b1be2892SMatthew Ahrens /* 828b1be2892SMatthew Ahrens * If the space map has the wrong bonus size (because 829b1be2892SMatthew Ahrens * SPA_FEATURE_SPACEMAP_HISTOGRAM has recently been enabled), or 830b1be2892SMatthew Ahrens * the wrong block size (because space_map_blksz has changed), 831b1be2892SMatthew Ahrens * free and re-allocate its object with the updated sizes. 832b1be2892SMatthew Ahrens * 833b1be2892SMatthew Ahrens * Otherwise, just truncate the current object. 834b1be2892SMatthew Ahrens */ 835b1be2892SMatthew Ahrens if ((spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM) && 836b1be2892SMatthew Ahrens doi.doi_bonus_size != sizeof (space_map_phys_t)) || 83786714001SSerapheim Dimitropoulos doi.doi_data_block_size != blocksize) { 8383991b535SGeorge Wilson zfs_dbgmsg("txg %llu, spa %s, sm %p, reallocating " 8393991b535SGeorge Wilson "object[%llu]: old bonus %u, old blocksz %u", 8403991b535SGeorge Wilson dmu_tx_get_txg(tx), spa_name(spa), sm, sm->sm_object, 8413991b535SGeorge Wilson doi.doi_bonus_size, doi.doi_data_block_size); 842b1be2892SMatthew Ahrens 843b1be2892SMatthew Ahrens space_map_free(sm, tx); 844b1be2892SMatthew Ahrens dmu_buf_rele(sm->sm_dbuf, sm); 845b1be2892SMatthew Ahrens 84686714001SSerapheim Dimitropoulos sm->sm_object = space_map_alloc(sm->sm_os, blocksize, tx); 847b1be2892SMatthew Ahrens VERIFY0(space_map_open_impl(sm)); 848b1be2892SMatthew Ahrens } else { 849b1be2892SMatthew Ahrens VERIFY0(dmu_free_range(os, space_map_object(sm), 0, -1ULL, tx)); 850b1be2892SMatthew Ahrens 851b1be2892SMatthew Ahrens /* 852b1be2892SMatthew Ahrens * If the spacemap is reallocated, its histogram 853b1be2892SMatthew Ahrens * will be reset. Do the same in the common case so that 854b1be2892SMatthew Ahrens * bugs related to the uncommon case do not go unnoticed. 855b1be2892SMatthew Ahrens */ 856b1be2892SMatthew Ahrens bzero(sm->sm_phys->smp_histogram, 857b1be2892SMatthew Ahrens sizeof (sm->sm_phys->smp_histogram)); 8580713e232SGeorge Wilson } 8590713e232SGeorge Wilson 8600713e232SGeorge Wilson dmu_buf_will_dirty(sm->sm_dbuf, tx); 8610713e232SGeorge Wilson sm->sm_phys->smp_objsize = 0; 8620713e232SGeorge Wilson sm->sm_phys->smp_alloc = 0; 8638ad4d6ddSJeff Bonwick } 8648ad4d6ddSJeff Bonwick 8658ad4d6ddSJeff Bonwick /* 8660713e232SGeorge Wilson * Update the in-core space_map allocation and length values. 8678ad4d6ddSJeff Bonwick */ 8688ad4d6ddSJeff Bonwick void 8690713e232SGeorge Wilson space_map_update(space_map_t *sm) 8708ad4d6ddSJeff Bonwick { 8710713e232SGeorge Wilson if (sm == NULL) 8720713e232SGeorge Wilson return; 8738ad4d6ddSJeff Bonwick 8740713e232SGeorge Wilson sm->sm_alloc = sm->sm_phys->smp_alloc; 8750713e232SGeorge Wilson sm->sm_length = sm->sm_phys->smp_objsize; 8760713e232SGeorge Wilson } 8770713e232SGeorge Wilson 8780713e232SGeorge Wilson uint64_t 87986714001SSerapheim Dimitropoulos space_map_alloc(objset_t *os, int blocksize, dmu_tx_t *tx) 8800713e232SGeorge Wilson { 8810713e232SGeorge Wilson spa_t *spa = dmu_objset_spa(os); 8820713e232SGeorge Wilson uint64_t object; 8830713e232SGeorge Wilson int bonuslen; 8840713e232SGeorge Wilson 8852acef22dSMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 8862acef22dSMatthew Ahrens spa_feature_incr(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM, tx); 8870713e232SGeorge Wilson bonuslen = sizeof (space_map_phys_t); 8880713e232SGeorge Wilson ASSERT3U(bonuslen, <=, dmu_bonus_max()); 8890713e232SGeorge Wilson } else { 8900713e232SGeorge Wilson bonuslen = SPACE_MAP_SIZE_V0; 8910713e232SGeorge Wilson } 8920713e232SGeorge Wilson 89386714001SSerapheim Dimitropoulos object = dmu_object_alloc(os, DMU_OT_SPACE_MAP, blocksize, 8940713e232SGeorge Wilson DMU_OT_SPACE_MAP_HEADER, bonuslen, tx); 8950713e232SGeorge Wilson 8960713e232SGeorge Wilson return (object); 8978ad4d6ddSJeff Bonwick } 8988ad4d6ddSJeff Bonwick 8998ad4d6ddSJeff Bonwick void 9005cabbc6bSPrashanth Sreenivasa space_map_free_obj(objset_t *os, uint64_t smobj, dmu_tx_t *tx) 9018ad4d6ddSJeff Bonwick { 9025cabbc6bSPrashanth Sreenivasa spa_t *spa = dmu_objset_spa(os); 9032acef22dSMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 9040713e232SGeorge Wilson dmu_object_info_t doi; 9058ad4d6ddSJeff Bonwick 9065cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_info(os, smobj, &doi)); 9070713e232SGeorge Wilson if (doi.doi_bonus_size != SPACE_MAP_SIZE_V0) { 9082acef22dSMatthew Ahrens spa_feature_decr(spa, 9092acef22dSMatthew Ahrens SPA_FEATURE_SPACEMAP_HISTOGRAM, tx); 9108ad4d6ddSJeff Bonwick } 9118ad4d6ddSJeff Bonwick } 9120713e232SGeorge Wilson 9135cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(os, smobj, tx)); 9145cabbc6bSPrashanth Sreenivasa } 9155cabbc6bSPrashanth Sreenivasa 9165cabbc6bSPrashanth Sreenivasa void 9175cabbc6bSPrashanth Sreenivasa space_map_free(space_map_t *sm, dmu_tx_t *tx) 9185cabbc6bSPrashanth Sreenivasa { 9195cabbc6bSPrashanth Sreenivasa if (sm == NULL) 9205cabbc6bSPrashanth Sreenivasa return; 9215cabbc6bSPrashanth Sreenivasa 9225cabbc6bSPrashanth Sreenivasa space_map_free_obj(sm->sm_os, space_map_object(sm), tx); 9230713e232SGeorge Wilson sm->sm_object = 0; 9240713e232SGeorge Wilson } 9250713e232SGeorge Wilson 926*17f11284SSerapheim Dimitropoulos /* 927*17f11284SSerapheim Dimitropoulos * Given a range tree, it makes a worst-case estimate of how much 928*17f11284SSerapheim Dimitropoulos * space would the tree's segments take if they were written to 929*17f11284SSerapheim Dimitropoulos * the given space map. 930*17f11284SSerapheim Dimitropoulos */ 931*17f11284SSerapheim Dimitropoulos uint64_t 932*17f11284SSerapheim Dimitropoulos space_map_estimate_optimal_size(space_map_t *sm, range_tree_t *rt, 933*17f11284SSerapheim Dimitropoulos uint64_t vdev_id) 934*17f11284SSerapheim Dimitropoulos { 935*17f11284SSerapheim Dimitropoulos spa_t *spa = dmu_objset_spa(sm->sm_os); 936*17f11284SSerapheim Dimitropoulos uint64_t shift = sm->sm_shift; 937*17f11284SSerapheim Dimitropoulos uint64_t *histogram = rt->rt_histogram; 938*17f11284SSerapheim Dimitropoulos uint64_t entries_for_seg = 0; 939*17f11284SSerapheim Dimitropoulos 940*17f11284SSerapheim Dimitropoulos /* 941*17f11284SSerapheim Dimitropoulos * In order to get a quick estimate of the optimal size that this 942*17f11284SSerapheim Dimitropoulos * range tree would have on-disk as a space map, we iterate through 943*17f11284SSerapheim Dimitropoulos * its histogram buckets instead of iterating through its nodes. 944*17f11284SSerapheim Dimitropoulos * 945*17f11284SSerapheim Dimitropoulos * Note that this is a highest-bound/worst-case estimate for the 946*17f11284SSerapheim Dimitropoulos * following reasons: 947*17f11284SSerapheim Dimitropoulos * 948*17f11284SSerapheim Dimitropoulos * 1] We assume that we always add a debug padding for each block 949*17f11284SSerapheim Dimitropoulos * we write and we also assume that we start at the last word 950*17f11284SSerapheim Dimitropoulos * of a block attempting to write a two-word entry. 951*17f11284SSerapheim Dimitropoulos * 2] Rounding up errors due to the way segments are distributed 952*17f11284SSerapheim Dimitropoulos * in the buckets of the range tree's histogram. 953*17f11284SSerapheim Dimitropoulos * 3] The activation of zfs_force_some_double_word_sm_entries 954*17f11284SSerapheim Dimitropoulos * (tunable) when testing. 955*17f11284SSerapheim Dimitropoulos * 956*17f11284SSerapheim Dimitropoulos * = Math and Rounding Errors = 957*17f11284SSerapheim Dimitropoulos * 958*17f11284SSerapheim Dimitropoulos * rt_histogram[i] bucket of a range tree represents the number 959*17f11284SSerapheim Dimitropoulos * of entries in [2^i, (2^(i+1))-1] of that range_tree. Given 960*17f11284SSerapheim Dimitropoulos * that, we want to divide the buckets into groups: Buckets that 961*17f11284SSerapheim Dimitropoulos * can be represented using a single-word entry, ones that can 962*17f11284SSerapheim Dimitropoulos * be represented with a double-word entry, and ones that can 963*17f11284SSerapheim Dimitropoulos * only be represented with multiple two-word entries. 964*17f11284SSerapheim Dimitropoulos * 965*17f11284SSerapheim Dimitropoulos * [Note that if the new encoding feature is not enabled there 966*17f11284SSerapheim Dimitropoulos * are only two groups: single-word entry buckets and multiple 967*17f11284SSerapheim Dimitropoulos * single-word entry buckets. The information below assumes 968*17f11284SSerapheim Dimitropoulos * two-word entries enabled, but it can easily applied when 969*17f11284SSerapheim Dimitropoulos * the feature is not enabled] 970*17f11284SSerapheim Dimitropoulos * 971*17f11284SSerapheim Dimitropoulos * To find the highest bucket that can be represented with a 972*17f11284SSerapheim Dimitropoulos * single-word entry we look at the maximum run that such entry 973*17f11284SSerapheim Dimitropoulos * can have, which is 2^(SM_RUN_BITS + sm_shift) [remember that 974*17f11284SSerapheim Dimitropoulos * the run of a space map entry is shifted by sm_shift, thus we 975*17f11284SSerapheim Dimitropoulos * add it to the exponent]. This way, excluding the value of the 976*17f11284SSerapheim Dimitropoulos * maximum run that can be represented by a single-word entry, 977*17f11284SSerapheim Dimitropoulos * all runs that are smaller exist in buckets 0 to 978*17f11284SSerapheim Dimitropoulos * SM_RUN_BITS + shift - 1. 979*17f11284SSerapheim Dimitropoulos * 980*17f11284SSerapheim Dimitropoulos * To find the highest bucket that can be represented with a 981*17f11284SSerapheim Dimitropoulos * double-word entry, we follow the same approach. Finally, any 982*17f11284SSerapheim Dimitropoulos * bucket higher than that are represented with multiple two-word 983*17f11284SSerapheim Dimitropoulos * entries. To be more specific, if the highest bucket whose 984*17f11284SSerapheim Dimitropoulos * segments can be represented with a single two-word entry is X, 985*17f11284SSerapheim Dimitropoulos * then bucket X+1 will need 2 two-word entries for each of its 986*17f11284SSerapheim Dimitropoulos * segments, X+2 will need 4, X+3 will need 8, ...etc. 987*17f11284SSerapheim Dimitropoulos * 988*17f11284SSerapheim Dimitropoulos * With all of the above we make our estimation based on bucket 989*17f11284SSerapheim Dimitropoulos * groups. There is a rounding error though. As we mentioned in 990*17f11284SSerapheim Dimitropoulos * the example with the one-word entry, the maximum run that can 991*17f11284SSerapheim Dimitropoulos * be represented in a one-word entry 2^(SM_RUN_BITS + shift) is 992*17f11284SSerapheim Dimitropoulos * not part of bucket SM_RUN_BITS + shift - 1. Thus, segments of 993*17f11284SSerapheim Dimitropoulos * that length fall into the next bucket (and bucket group) where 994*17f11284SSerapheim Dimitropoulos * we start counting two-word entries and this is one more reason 995*17f11284SSerapheim Dimitropoulos * why the estimated size may end up being bigger than the actual 996*17f11284SSerapheim Dimitropoulos * size written. 997*17f11284SSerapheim Dimitropoulos */ 998*17f11284SSerapheim Dimitropoulos uint64_t size = 0; 999*17f11284SSerapheim Dimitropoulos uint64_t idx = 0; 1000*17f11284SSerapheim Dimitropoulos 1001*17f11284SSerapheim Dimitropoulos if (!spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2) || 1002*17f11284SSerapheim Dimitropoulos (vdev_id == SM_NO_VDEVID && sm->sm_size < SM_OFFSET_MAX)) { 1003*17f11284SSerapheim Dimitropoulos 1004*17f11284SSerapheim Dimitropoulos /* 1005*17f11284SSerapheim Dimitropoulos * If we are trying to force some double word entries just 1006*17f11284SSerapheim Dimitropoulos * assume the worst-case of every single word entry being 1007*17f11284SSerapheim Dimitropoulos * written as a double word entry. 1008*17f11284SSerapheim Dimitropoulos */ 1009*17f11284SSerapheim Dimitropoulos uint64_t entry_size = 1010*17f11284SSerapheim Dimitropoulos (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2) && 1011*17f11284SSerapheim Dimitropoulos zfs_force_some_double_word_sm_entries) ? 1012*17f11284SSerapheim Dimitropoulos (2 * sizeof (uint64_t)) : sizeof (uint64_t); 1013*17f11284SSerapheim Dimitropoulos 1014*17f11284SSerapheim Dimitropoulos uint64_t single_entry_max_bucket = SM_RUN_BITS + shift - 1; 1015*17f11284SSerapheim Dimitropoulos for (; idx <= single_entry_max_bucket; idx++) 1016*17f11284SSerapheim Dimitropoulos size += histogram[idx] * entry_size; 1017*17f11284SSerapheim Dimitropoulos 1018*17f11284SSerapheim Dimitropoulos if (!spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2)) { 1019*17f11284SSerapheim Dimitropoulos for (; idx < RANGE_TREE_HISTOGRAM_SIZE; idx++) { 1020*17f11284SSerapheim Dimitropoulos ASSERT3U(idx, >=, single_entry_max_bucket); 1021*17f11284SSerapheim Dimitropoulos entries_for_seg = 1022*17f11284SSerapheim Dimitropoulos 1ULL << (idx - single_entry_max_bucket); 1023*17f11284SSerapheim Dimitropoulos size += histogram[idx] * 1024*17f11284SSerapheim Dimitropoulos entries_for_seg * entry_size; 1025*17f11284SSerapheim Dimitropoulos } 1026*17f11284SSerapheim Dimitropoulos return (size); 1027*17f11284SSerapheim Dimitropoulos } 1028*17f11284SSerapheim Dimitropoulos } 1029*17f11284SSerapheim Dimitropoulos 1030*17f11284SSerapheim Dimitropoulos ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2)); 1031*17f11284SSerapheim Dimitropoulos 1032*17f11284SSerapheim Dimitropoulos uint64_t double_entry_max_bucket = SM2_RUN_BITS + shift - 1; 1033*17f11284SSerapheim Dimitropoulos for (; idx <= double_entry_max_bucket; idx++) 1034*17f11284SSerapheim Dimitropoulos size += histogram[idx] * 2 * sizeof (uint64_t); 1035*17f11284SSerapheim Dimitropoulos 1036*17f11284SSerapheim Dimitropoulos for (; idx < RANGE_TREE_HISTOGRAM_SIZE; idx++) { 1037*17f11284SSerapheim Dimitropoulos ASSERT3U(idx, >=, double_entry_max_bucket); 1038*17f11284SSerapheim Dimitropoulos entries_for_seg = 1ULL << (idx - double_entry_max_bucket); 1039*17f11284SSerapheim Dimitropoulos size += histogram[idx] * 1040*17f11284SSerapheim Dimitropoulos entries_for_seg * 2 * sizeof (uint64_t); 1041*17f11284SSerapheim Dimitropoulos } 1042*17f11284SSerapheim Dimitropoulos 1043*17f11284SSerapheim Dimitropoulos /* 1044*17f11284SSerapheim Dimitropoulos * Assume the worst case where we start with the padding at the end 1045*17f11284SSerapheim Dimitropoulos * of the current block and we add an extra padding entry at the end 1046*17f11284SSerapheim Dimitropoulos * of all subsequent blocks. 1047*17f11284SSerapheim Dimitropoulos */ 1048*17f11284SSerapheim Dimitropoulos size += ((size / sm->sm_blksz) + 1) * sizeof (uint64_t); 1049*17f11284SSerapheim Dimitropoulos 1050*17f11284SSerapheim Dimitropoulos return (size); 1051*17f11284SSerapheim Dimitropoulos } 1052*17f11284SSerapheim Dimitropoulos 10530713e232SGeorge Wilson uint64_t 10540713e232SGeorge Wilson space_map_object(space_map_t *sm) 10550713e232SGeorge Wilson { 10560713e232SGeorge Wilson return (sm != NULL ? sm->sm_object : 0); 10570713e232SGeorge Wilson } 10580713e232SGeorge Wilson 10590713e232SGeorge Wilson /* 10600713e232SGeorge Wilson * Returns the already synced, on-disk allocated space. 10610713e232SGeorge Wilson */ 10620713e232SGeorge Wilson uint64_t 10630713e232SGeorge Wilson space_map_allocated(space_map_t *sm) 10640713e232SGeorge Wilson { 10650713e232SGeorge Wilson return (sm != NULL ? sm->sm_alloc : 0); 10660713e232SGeorge Wilson } 10670713e232SGeorge Wilson 10680713e232SGeorge Wilson /* 10690713e232SGeorge Wilson * Returns the already synced, on-disk length; 10700713e232SGeorge Wilson */ 10710713e232SGeorge Wilson uint64_t 10720713e232SGeorge Wilson space_map_length(space_map_t *sm) 10730713e232SGeorge Wilson { 10740713e232SGeorge Wilson return (sm != NULL ? sm->sm_length : 0); 10750713e232SGeorge Wilson } 10760713e232SGeorge Wilson 10770713e232SGeorge Wilson /* 10780713e232SGeorge Wilson * Returns the allocated space that is currently syncing. 10790713e232SGeorge Wilson */ 10800713e232SGeorge Wilson int64_t 10810713e232SGeorge Wilson space_map_alloc_delta(space_map_t *sm) 10820713e232SGeorge Wilson { 10830713e232SGeorge Wilson if (sm == NULL) 10840713e232SGeorge Wilson return (0); 10850713e232SGeorge Wilson ASSERT(sm->sm_dbuf != NULL); 10860713e232SGeorge Wilson return (sm->sm_phys->smp_alloc - space_map_allocated(sm)); 10878ad4d6ddSJeff Bonwick } 1088