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 /* 26*555d674dSSerapheim Dimitropoulos * Copyright (c) 2012, 2018 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. 4417f11284SSerapheim Dimitropoulos * Larger blocks entail fewer I/O operations, but they also cause the 4517f11284SSerapheim 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 4917f11284SSerapheim Dimitropoulos /* 5017f11284SSerapheim Dimitropoulos * Enabled whenever we want to stress test the use of double-word 5117f11284SSerapheim Dimitropoulos * space map entries. 5217f11284SSerapheim Dimitropoulos */ 5317f11284SSerapheim Dimitropoulos boolean_t zfs_force_some_double_word_sm_entries = B_FALSE; 5417f11284SSerapheim Dimitropoulos 55221813c1SMatthew Ahrens /* 56221813c1SMatthew Ahrens * Override the default indirect block size of 128K, instead using 16K for 57221813c1SMatthew Ahrens * spacemaps (2^14 bytes). This dramatically reduces write inflation since 58221813c1SMatthew Ahrens * appending to a spacemap typically has to write one data block (4KB) and one 59221813c1SMatthew Ahrens * or two indirect blocks (16K-32K, rather than 128K). 60221813c1SMatthew Ahrens */ 61221813c1SMatthew Ahrens int space_map_ibs = 14; 62221813c1SMatthew Ahrens 6317f11284SSerapheim Dimitropoulos boolean_t 6417f11284SSerapheim Dimitropoulos sm_entry_is_debug(uint64_t e) 6517f11284SSerapheim Dimitropoulos { 6617f11284SSerapheim Dimitropoulos return (SM_PREFIX_DECODE(e) == SM_DEBUG_PREFIX); 6717f11284SSerapheim Dimitropoulos } 6817f11284SSerapheim Dimitropoulos 6917f11284SSerapheim Dimitropoulos boolean_t 7017f11284SSerapheim Dimitropoulos sm_entry_is_single_word(uint64_t e) 7117f11284SSerapheim Dimitropoulos { 7217f11284SSerapheim Dimitropoulos uint8_t prefix = SM_PREFIX_DECODE(e); 7317f11284SSerapheim Dimitropoulos return (prefix != SM_DEBUG_PREFIX && prefix != SM2_PREFIX); 7417f11284SSerapheim Dimitropoulos } 7517f11284SSerapheim Dimitropoulos 7617f11284SSerapheim Dimitropoulos boolean_t 7717f11284SSerapheim Dimitropoulos sm_entry_is_double_word(uint64_t e) 7817f11284SSerapheim Dimitropoulos { 7917f11284SSerapheim Dimitropoulos return (SM_PREFIX_DECODE(e) == SM2_PREFIX); 8017f11284SSerapheim Dimitropoulos } 8117f11284SSerapheim Dimitropoulos 82ecc2d604Sbonwick /* 835cabbc6bSPrashanth Sreenivasa * Iterate through the space map, invoking the callback on each (non-debug) 84*555d674dSSerapheim Dimitropoulos * space map entry. Stop after reading 'end' bytes of the space map. 85ecc2d604Sbonwick */ 86fa9e4066Sahrens int 87*555d674dSSerapheim Dimitropoulos space_map_iterate(space_map_t *sm, uint64_t end, sm_cb_t callback, void *arg) 88fa9e4066Sahrens { 89*555d674dSSerapheim Dimitropoulos uint64_t blksz = sm->sm_blksz; 90*555d674dSSerapheim Dimitropoulos 91*555d674dSSerapheim Dimitropoulos ASSERT3U(blksz, !=, 0); 92*555d674dSSerapheim Dimitropoulos ASSERT3U(end, <=, space_map_length(sm)); 93*555d674dSSerapheim Dimitropoulos ASSERT0(P2PHASE(end, sizeof (uint64_t))); 9417f11284SSerapheim Dimitropoulos 95*555d674dSSerapheim Dimitropoulos dmu_prefetch(sm->sm_os, space_map_object(sm), 0, 0, end, 9617f11284SSerapheim Dimitropoulos ZIO_PRIORITY_SYNC_READ); 9717f11284SSerapheim Dimitropoulos 980a4e9518Sgw int error = 0; 99*555d674dSSerapheim Dimitropoulos for (uint64_t block_base = 0; block_base < end && error == 0; 10017f11284SSerapheim Dimitropoulos block_base += blksz) { 10117f11284SSerapheim Dimitropoulos dmu_buf_t *db; 10217f11284SSerapheim Dimitropoulos error = dmu_buf_hold(sm->sm_os, space_map_object(sm), 10317f11284SSerapheim Dimitropoulos block_base, FTAG, &db, DMU_READ_PREFETCH); 10417f11284SSerapheim Dimitropoulos if (error != 0) 10517f11284SSerapheim Dimitropoulos return (error); 106fa9e4066Sahrens 10717f11284SSerapheim Dimitropoulos uint64_t *block_start = db->db_data; 108*555d674dSSerapheim Dimitropoulos uint64_t block_length = MIN(end - block_base, blksz); 10917f11284SSerapheim Dimitropoulos uint64_t *block_end = block_start + 11017f11284SSerapheim Dimitropoulos (block_length / sizeof (uint64_t)); 111fa9e4066Sahrens 11217f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(block_length, sizeof (uint64_t))); 11317f11284SSerapheim Dimitropoulos VERIFY3U(block_length, !=, 0); 11417f11284SSerapheim Dimitropoulos ASSERT3U(blksz, ==, db->db_size); 115ecc2d604Sbonwick 11617f11284SSerapheim Dimitropoulos for (uint64_t *block_cursor = block_start; 11717f11284SSerapheim Dimitropoulos block_cursor < block_end && error == 0; block_cursor++) { 11817f11284SSerapheim Dimitropoulos uint64_t e = *block_cursor; 119ecc2d604Sbonwick 12017f11284SSerapheim Dimitropoulos if (sm_entry_is_debug(e)) /* Skip debug entries */ 12117f11284SSerapheim Dimitropoulos continue; 122fa9e4066Sahrens 12317f11284SSerapheim Dimitropoulos uint64_t raw_offset, raw_run, vdev_id; 12417f11284SSerapheim Dimitropoulos maptype_t type; 12517f11284SSerapheim Dimitropoulos if (sm_entry_is_single_word(e)) { 12617f11284SSerapheim Dimitropoulos type = SM_TYPE_DECODE(e); 12717f11284SSerapheim Dimitropoulos vdev_id = SM_NO_VDEVID; 12817f11284SSerapheim Dimitropoulos raw_offset = SM_OFFSET_DECODE(e); 12917f11284SSerapheim Dimitropoulos raw_run = SM_RUN_DECODE(e); 13017f11284SSerapheim Dimitropoulos } else { 13117f11284SSerapheim Dimitropoulos /* it is a two-word entry */ 13217f11284SSerapheim Dimitropoulos ASSERT(sm_entry_is_double_word(e)); 13317f11284SSerapheim Dimitropoulos raw_run = SM2_RUN_DECODE(e); 13417f11284SSerapheim Dimitropoulos vdev_id = SM2_VDEV_DECODE(e); 13517f11284SSerapheim Dimitropoulos 13617f11284SSerapheim Dimitropoulos /* move on to the second word */ 13717f11284SSerapheim Dimitropoulos block_cursor++; 13817f11284SSerapheim Dimitropoulos e = *block_cursor; 13917f11284SSerapheim Dimitropoulos VERIFY3P(block_cursor, <=, block_end); 14017f11284SSerapheim Dimitropoulos 14117f11284SSerapheim Dimitropoulos type = SM2_TYPE_DECODE(e); 14217f11284SSerapheim Dimitropoulos raw_offset = SM2_OFFSET_DECODE(e); 14317f11284SSerapheim Dimitropoulos } 144ecc2d604Sbonwick 14517f11284SSerapheim Dimitropoulos uint64_t entry_offset = (raw_offset << sm->sm_shift) + 14617f11284SSerapheim Dimitropoulos sm->sm_start; 14717f11284SSerapheim Dimitropoulos uint64_t entry_run = raw_run << sm->sm_shift; 148fa9e4066Sahrens 14917f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_offset, 1ULL << sm->sm_shift)); 15017f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_run, 1ULL << sm->sm_shift)); 15117f11284SSerapheim Dimitropoulos ASSERT3U(entry_offset, >=, sm->sm_start); 15217f11284SSerapheim Dimitropoulos ASSERT3U(entry_offset, <, sm->sm_start + sm->sm_size); 15317f11284SSerapheim Dimitropoulos ASSERT3U(entry_run, <=, sm->sm_size); 15417f11284SSerapheim Dimitropoulos ASSERT3U(entry_offset + entry_run, <=, 15517f11284SSerapheim Dimitropoulos sm->sm_start + sm->sm_size); 156fa9e4066Sahrens 15717f11284SSerapheim Dimitropoulos space_map_entry_t sme = { 15817f11284SSerapheim Dimitropoulos .sme_type = type, 15917f11284SSerapheim Dimitropoulos .sme_vdev = vdev_id, 16017f11284SSerapheim Dimitropoulos .sme_offset = entry_offset, 16117f11284SSerapheim Dimitropoulos .sme_run = entry_run 16217f11284SSerapheim Dimitropoulos }; 16317f11284SSerapheim Dimitropoulos error = callback(&sme, arg); 16417f11284SSerapheim Dimitropoulos } 16517f11284SSerapheim Dimitropoulos dmu_buf_rele(db, FTAG); 16617f11284SSerapheim Dimitropoulos } 16717f11284SSerapheim Dimitropoulos return (error); 16817f11284SSerapheim Dimitropoulos } 169fa9e4066Sahrens 17017f11284SSerapheim Dimitropoulos /* 17117f11284SSerapheim Dimitropoulos * Reads the entries from the last block of the space map into 17217f11284SSerapheim Dimitropoulos * buf in reverse order. Populates nwords with number of words 17317f11284SSerapheim Dimitropoulos * in the last block. 17417f11284SSerapheim Dimitropoulos * 17517f11284SSerapheim Dimitropoulos * Refer to block comment within space_map_incremental_destroy() 17617f11284SSerapheim Dimitropoulos * to understand why this function is needed. 17717f11284SSerapheim Dimitropoulos */ 17817f11284SSerapheim Dimitropoulos static int 17917f11284SSerapheim Dimitropoulos space_map_reversed_last_block_entries(space_map_t *sm, uint64_t *buf, 18017f11284SSerapheim Dimitropoulos uint64_t bufsz, uint64_t *nwords) 18117f11284SSerapheim Dimitropoulos { 18217f11284SSerapheim Dimitropoulos int error = 0; 18317f11284SSerapheim Dimitropoulos dmu_buf_t *db; 1840713e232SGeorge Wilson 18517f11284SSerapheim Dimitropoulos /* 18617f11284SSerapheim Dimitropoulos * Find the offset of the last word in the space map and use 18717f11284SSerapheim Dimitropoulos * that to read the last block of the space map with 18817f11284SSerapheim Dimitropoulos * dmu_buf_hold(). 18917f11284SSerapheim Dimitropoulos */ 19017f11284SSerapheim Dimitropoulos uint64_t last_word_offset = 191*555d674dSSerapheim Dimitropoulos sm->sm_phys->smp_length - sizeof (uint64_t); 19217f11284SSerapheim Dimitropoulos error = dmu_buf_hold(sm->sm_os, space_map_object(sm), last_word_offset, 19317f11284SSerapheim Dimitropoulos FTAG, &db, DMU_READ_NO_PREFETCH); 19417f11284SSerapheim Dimitropoulos if (error != 0) 19517f11284SSerapheim Dimitropoulos return (error); 19617f11284SSerapheim Dimitropoulos 19717f11284SSerapheim Dimitropoulos ASSERT3U(sm->sm_object, ==, db->db_object); 19817f11284SSerapheim Dimitropoulos ASSERT3U(sm->sm_blksz, ==, db->db_size); 19917f11284SSerapheim Dimitropoulos ASSERT3U(bufsz, >=, db->db_size); 20017f11284SSerapheim Dimitropoulos ASSERT(nwords != NULL); 20117f11284SSerapheim Dimitropoulos 20217f11284SSerapheim Dimitropoulos uint64_t *words = db->db_data; 20317f11284SSerapheim Dimitropoulos *nwords = 204*555d674dSSerapheim Dimitropoulos (sm->sm_phys->smp_length - db->db_offset) / sizeof (uint64_t); 20517f11284SSerapheim Dimitropoulos 20617f11284SSerapheim Dimitropoulos ASSERT3U(*nwords, <=, bufsz / sizeof (uint64_t)); 20717f11284SSerapheim Dimitropoulos 20817f11284SSerapheim Dimitropoulos uint64_t n = *nwords; 20917f11284SSerapheim Dimitropoulos uint64_t j = n - 1; 21017f11284SSerapheim Dimitropoulos for (uint64_t i = 0; i < n; i++) { 21117f11284SSerapheim Dimitropoulos uint64_t entry = words[i]; 21217f11284SSerapheim Dimitropoulos if (sm_entry_is_double_word(entry)) { 21317f11284SSerapheim Dimitropoulos /* 21417f11284SSerapheim Dimitropoulos * Since we are populating the buffer backwards 21517f11284SSerapheim Dimitropoulos * we have to be extra careful and add the two 21617f11284SSerapheim Dimitropoulos * words of the double-word entry in the right 21717f11284SSerapheim Dimitropoulos * order. 21817f11284SSerapheim Dimitropoulos */ 21917f11284SSerapheim Dimitropoulos ASSERT3U(j, >, 0); 22017f11284SSerapheim Dimitropoulos buf[j - 1] = entry; 22117f11284SSerapheim Dimitropoulos 22217f11284SSerapheim Dimitropoulos i++; 22317f11284SSerapheim Dimitropoulos ASSERT3U(i, <, n); 22417f11284SSerapheim Dimitropoulos entry = words[i]; 22517f11284SSerapheim Dimitropoulos buf[j] = entry; 22617f11284SSerapheim Dimitropoulos j -= 2; 22717f11284SSerapheim Dimitropoulos } else { 22817f11284SSerapheim Dimitropoulos ASSERT(sm_entry_is_debug(entry) || 22917f11284SSerapheim Dimitropoulos sm_entry_is_single_word(entry)); 23017f11284SSerapheim Dimitropoulos buf[j] = entry; 23117f11284SSerapheim Dimitropoulos j--; 232fa9e4066Sahrens } 233fa9e4066Sahrens } 234fa9e4066Sahrens 23517f11284SSerapheim Dimitropoulos /* 23617f11284SSerapheim Dimitropoulos * Assert that we wrote backwards all the 23717f11284SSerapheim Dimitropoulos * way to the beginning of the buffer. 23817f11284SSerapheim Dimitropoulos */ 23917f11284SSerapheim Dimitropoulos ASSERT3S(j, ==, -1); 24017f11284SSerapheim Dimitropoulos 24117f11284SSerapheim Dimitropoulos dmu_buf_rele(db, FTAG); 2425cabbc6bSPrashanth Sreenivasa return (error); 2435cabbc6bSPrashanth Sreenivasa } 2445cabbc6bSPrashanth Sreenivasa 24586714001SSerapheim Dimitropoulos /* 24686714001SSerapheim Dimitropoulos * Note: This function performs destructive actions - specifically 24786714001SSerapheim Dimitropoulos * it deletes entries from the end of the space map. Thus, callers 24886714001SSerapheim Dimitropoulos * should ensure that they are holding the appropriate locks for 24986714001SSerapheim Dimitropoulos * the space map that they provide. 25086714001SSerapheim Dimitropoulos */ 25186714001SSerapheim Dimitropoulos int 25286714001SSerapheim Dimitropoulos space_map_incremental_destroy(space_map_t *sm, sm_cb_t callback, void *arg, 25386714001SSerapheim Dimitropoulos dmu_tx_t *tx) 25486714001SSerapheim Dimitropoulos { 25517f11284SSerapheim Dimitropoulos uint64_t bufsz = MAX(sm->sm_blksz, SPA_MINBLOCKSIZE); 25617f11284SSerapheim Dimitropoulos uint64_t *buf = zio_buf_alloc(bufsz); 25786714001SSerapheim Dimitropoulos 25886714001SSerapheim Dimitropoulos dmu_buf_will_dirty(sm->sm_dbuf, tx); 25986714001SSerapheim Dimitropoulos 26086714001SSerapheim Dimitropoulos /* 26117f11284SSerapheim Dimitropoulos * Ideally we would want to iterate from the beginning of the 26217f11284SSerapheim Dimitropoulos * space map to the end in incremental steps. The issue with this 26317f11284SSerapheim Dimitropoulos * approach is that we don't have any field on-disk that points 26417f11284SSerapheim Dimitropoulos * us where to start between each step. We could try zeroing out 26517f11284SSerapheim Dimitropoulos * entries that we've destroyed, but this doesn't work either as 26617f11284SSerapheim Dimitropoulos * an entry that is 0 is a valid one (ALLOC for range [0x0:0x200]). 26717f11284SSerapheim Dimitropoulos * 26817f11284SSerapheim Dimitropoulos * As a result, we destroy its entries incrementally starting from 26917f11284SSerapheim Dimitropoulos * the end after applying the callback to each of them. 27086714001SSerapheim Dimitropoulos * 27117f11284SSerapheim Dimitropoulos * The problem with this approach is that we cannot literally 27217f11284SSerapheim Dimitropoulos * iterate through the words in the space map backwards as we 27317f11284SSerapheim Dimitropoulos * can't distinguish two-word space map entries from their second 27417f11284SSerapheim Dimitropoulos * word. Thus we do the following: 27586714001SSerapheim Dimitropoulos * 27617f11284SSerapheim Dimitropoulos * 1] We get all the entries from the last block of the space map 27717f11284SSerapheim Dimitropoulos * and put them into a buffer in reverse order. This way the 27817f11284SSerapheim Dimitropoulos * last entry comes first in the buffer, the second to last is 27917f11284SSerapheim Dimitropoulos * second, etc. 28017f11284SSerapheim Dimitropoulos * 2] We iterate through the entries in the buffer and we apply 28117f11284SSerapheim Dimitropoulos * the callback to each one. As we move from entry to entry we 28217f11284SSerapheim Dimitropoulos * we decrease the size of the space map, deleting effectively 28317f11284SSerapheim Dimitropoulos * each entry. 28417f11284SSerapheim Dimitropoulos * 3] If there are no more entries in the space map or the callback 28517f11284SSerapheim Dimitropoulos * returns a value other than 0, we stop iterating over the 28617f11284SSerapheim Dimitropoulos * space map. If there are entries remaining and the callback 28717f11284SSerapheim Dimitropoulos * returned 0, we go back to step [1]. 28886714001SSerapheim Dimitropoulos */ 28917f11284SSerapheim Dimitropoulos int error = 0; 29017f11284SSerapheim Dimitropoulos while (space_map_length(sm) > 0 && error == 0) { 29117f11284SSerapheim Dimitropoulos uint64_t nwords = 0; 29217f11284SSerapheim Dimitropoulos error = space_map_reversed_last_block_entries(sm, buf, bufsz, 29317f11284SSerapheim Dimitropoulos &nwords); 29486714001SSerapheim Dimitropoulos if (error != 0) 29586714001SSerapheim Dimitropoulos break; 29686714001SSerapheim Dimitropoulos 29717f11284SSerapheim Dimitropoulos ASSERT3U(nwords, <=, bufsz / sizeof (uint64_t)); 29886714001SSerapheim Dimitropoulos 29917f11284SSerapheim Dimitropoulos for (uint64_t i = 0; i < nwords; i++) { 30017f11284SSerapheim Dimitropoulos uint64_t e = buf[i]; 30186714001SSerapheim Dimitropoulos 30217f11284SSerapheim Dimitropoulos if (sm_entry_is_debug(e)) { 303*555d674dSSerapheim Dimitropoulos sm->sm_phys->smp_length -= sizeof (uint64_t); 30486714001SSerapheim Dimitropoulos continue; 30586714001SSerapheim Dimitropoulos } 30686714001SSerapheim Dimitropoulos 30717f11284SSerapheim Dimitropoulos int words = 1; 30817f11284SSerapheim Dimitropoulos uint64_t raw_offset, raw_run, vdev_id; 30917f11284SSerapheim Dimitropoulos maptype_t type; 31017f11284SSerapheim Dimitropoulos if (sm_entry_is_single_word(e)) { 31117f11284SSerapheim Dimitropoulos type = SM_TYPE_DECODE(e); 31217f11284SSerapheim Dimitropoulos vdev_id = SM_NO_VDEVID; 31317f11284SSerapheim Dimitropoulos raw_offset = SM_OFFSET_DECODE(e); 31417f11284SSerapheim Dimitropoulos raw_run = SM_RUN_DECODE(e); 31517f11284SSerapheim Dimitropoulos } else { 31617f11284SSerapheim Dimitropoulos ASSERT(sm_entry_is_double_word(e)); 31717f11284SSerapheim Dimitropoulos words = 2; 31817f11284SSerapheim Dimitropoulos 31917f11284SSerapheim Dimitropoulos raw_run = SM2_RUN_DECODE(e); 32017f11284SSerapheim Dimitropoulos vdev_id = SM2_VDEV_DECODE(e); 32117f11284SSerapheim Dimitropoulos 32217f11284SSerapheim Dimitropoulos /* move to the second word */ 32317f11284SSerapheim Dimitropoulos i++; 32417f11284SSerapheim Dimitropoulos e = buf[i]; 32517f11284SSerapheim Dimitropoulos 32617f11284SSerapheim Dimitropoulos ASSERT3P(i, <=, nwords); 32717f11284SSerapheim Dimitropoulos 32817f11284SSerapheim Dimitropoulos type = SM2_TYPE_DECODE(e); 32917f11284SSerapheim Dimitropoulos raw_offset = SM2_OFFSET_DECODE(e); 33017f11284SSerapheim Dimitropoulos } 33117f11284SSerapheim Dimitropoulos 33217f11284SSerapheim Dimitropoulos uint64_t entry_offset = 33317f11284SSerapheim Dimitropoulos (raw_offset << sm->sm_shift) + sm->sm_start; 33417f11284SSerapheim Dimitropoulos uint64_t entry_run = raw_run << sm->sm_shift; 33586714001SSerapheim Dimitropoulos 33686714001SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_offset, 1ULL << sm->sm_shift)); 33717f11284SSerapheim Dimitropoulos VERIFY0(P2PHASE(entry_run, 1ULL << sm->sm_shift)); 33886714001SSerapheim Dimitropoulos VERIFY3U(entry_offset, >=, sm->sm_start); 33917f11284SSerapheim Dimitropoulos VERIFY3U(entry_offset, <, sm->sm_start + sm->sm_size); 34017f11284SSerapheim Dimitropoulos VERIFY3U(entry_run, <=, sm->sm_size); 34117f11284SSerapheim Dimitropoulos VERIFY3U(entry_offset + entry_run, <=, 34286714001SSerapheim Dimitropoulos sm->sm_start + sm->sm_size); 34386714001SSerapheim Dimitropoulos 34417f11284SSerapheim Dimitropoulos space_map_entry_t sme = { 34517f11284SSerapheim Dimitropoulos .sme_type = type, 34617f11284SSerapheim Dimitropoulos .sme_vdev = vdev_id, 34717f11284SSerapheim Dimitropoulos .sme_offset = entry_offset, 34817f11284SSerapheim Dimitropoulos .sme_run = entry_run 34917f11284SSerapheim Dimitropoulos }; 35017f11284SSerapheim Dimitropoulos error = callback(&sme, arg); 35186714001SSerapheim Dimitropoulos if (error != 0) 35286714001SSerapheim Dimitropoulos break; 35386714001SSerapheim Dimitropoulos 35486714001SSerapheim Dimitropoulos if (type == SM_ALLOC) 35517f11284SSerapheim Dimitropoulos sm->sm_phys->smp_alloc -= entry_run; 35686714001SSerapheim Dimitropoulos else 35717f11284SSerapheim Dimitropoulos sm->sm_phys->smp_alloc += entry_run; 358*555d674dSSerapheim Dimitropoulos sm->sm_phys->smp_length -= words * sizeof (uint64_t); 35986714001SSerapheim Dimitropoulos } 36086714001SSerapheim Dimitropoulos } 36186714001SSerapheim Dimitropoulos 36217f11284SSerapheim Dimitropoulos if (space_map_length(sm) == 0) { 36386714001SSerapheim Dimitropoulos ASSERT0(error); 364*555d674dSSerapheim Dimitropoulos ASSERT0(space_map_allocated(sm)); 36586714001SSerapheim Dimitropoulos } 36686714001SSerapheim Dimitropoulos 36717f11284SSerapheim Dimitropoulos zio_buf_free(buf, bufsz); 36886714001SSerapheim Dimitropoulos return (error); 36986714001SSerapheim Dimitropoulos } 37086714001SSerapheim Dimitropoulos 3715cabbc6bSPrashanth Sreenivasa typedef struct space_map_load_arg { 3725cabbc6bSPrashanth Sreenivasa space_map_t *smla_sm; 3735cabbc6bSPrashanth Sreenivasa range_tree_t *smla_rt; 3745cabbc6bSPrashanth Sreenivasa maptype_t smla_type; 3755cabbc6bSPrashanth Sreenivasa } space_map_load_arg_t; 3765cabbc6bSPrashanth Sreenivasa 3775cabbc6bSPrashanth Sreenivasa static int 37817f11284SSerapheim Dimitropoulos space_map_load_callback(space_map_entry_t *sme, void *arg) 3795cabbc6bSPrashanth Sreenivasa { 3805cabbc6bSPrashanth Sreenivasa space_map_load_arg_t *smla = arg; 38117f11284SSerapheim Dimitropoulos if (sme->sme_type == smla->smla_type) { 38217f11284SSerapheim Dimitropoulos VERIFY3U(range_tree_space(smla->smla_rt) + sme->sme_run, <=, 3835cabbc6bSPrashanth Sreenivasa smla->smla_sm->sm_size); 38417f11284SSerapheim Dimitropoulos range_tree_add(smla->smla_rt, sme->sme_offset, sme->sme_run); 3855cabbc6bSPrashanth Sreenivasa } else { 38617f11284SSerapheim Dimitropoulos range_tree_remove(smla->smla_rt, sme->sme_offset, sme->sme_run); 3875cabbc6bSPrashanth Sreenivasa } 3885cabbc6bSPrashanth Sreenivasa 3895cabbc6bSPrashanth Sreenivasa return (0); 3905cabbc6bSPrashanth Sreenivasa } 3915cabbc6bSPrashanth Sreenivasa 3925cabbc6bSPrashanth Sreenivasa /* 393*555d674dSSerapheim Dimitropoulos * Load the spacemap into the rangetree, like space_map_load. But only 394*555d674dSSerapheim Dimitropoulos * read the first 'length' bytes of the spacemap. 3955cabbc6bSPrashanth Sreenivasa */ 3965cabbc6bSPrashanth Sreenivasa int 397*555d674dSSerapheim Dimitropoulos space_map_load_length(space_map_t *sm, range_tree_t *rt, maptype_t maptype, 398*555d674dSSerapheim Dimitropoulos uint64_t length) 3995cabbc6bSPrashanth Sreenivasa { 4005cabbc6bSPrashanth Sreenivasa space_map_load_arg_t smla; 4015cabbc6bSPrashanth Sreenivasa 4025cabbc6bSPrashanth Sreenivasa VERIFY0(range_tree_space(rt)); 4035cabbc6bSPrashanth Sreenivasa 404*555d674dSSerapheim Dimitropoulos if (maptype == SM_FREE) 4055cabbc6bSPrashanth Sreenivasa range_tree_add(rt, sm->sm_start, sm->sm_size); 4065cabbc6bSPrashanth Sreenivasa 4075cabbc6bSPrashanth Sreenivasa smla.smla_rt = rt; 4085cabbc6bSPrashanth Sreenivasa smla.smla_sm = sm; 4095cabbc6bSPrashanth Sreenivasa smla.smla_type = maptype; 410*555d674dSSerapheim Dimitropoulos int err = space_map_iterate(sm, length, 411*555d674dSSerapheim Dimitropoulos space_map_load_callback, &smla); 4125cabbc6bSPrashanth Sreenivasa 413*555d674dSSerapheim Dimitropoulos if (err != 0) 4140713e232SGeorge Wilson range_tree_vacate(rt, NULL, NULL); 415b8493d5dSvl 4165cabbc6bSPrashanth Sreenivasa return (err); 4170713e232SGeorge Wilson } 418ecc2d604Sbonwick 419*555d674dSSerapheim Dimitropoulos /* 420*555d674dSSerapheim Dimitropoulos * Load the space map disk into the specified range tree. Segments of maptype 421*555d674dSSerapheim Dimitropoulos * are added to the range tree, other segment types are removed. 422*555d674dSSerapheim Dimitropoulos */ 423*555d674dSSerapheim Dimitropoulos int 424*555d674dSSerapheim Dimitropoulos space_map_load(space_map_t *sm, range_tree_t *rt, maptype_t maptype) 425*555d674dSSerapheim Dimitropoulos { 426*555d674dSSerapheim Dimitropoulos return (space_map_load_length(sm, rt, maptype, space_map_length(sm))); 427*555d674dSSerapheim Dimitropoulos } 428*555d674dSSerapheim Dimitropoulos 4290713e232SGeorge Wilson void 4300713e232SGeorge Wilson space_map_histogram_clear(space_map_t *sm) 4310713e232SGeorge Wilson { 4320713e232SGeorge Wilson if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t)) 4330713e232SGeorge Wilson return; 434ecc2d604Sbonwick 4350713e232SGeorge Wilson bzero(sm->sm_phys->smp_histogram, sizeof (sm->sm_phys->smp_histogram)); 4360713e232SGeorge Wilson } 437ecc2d604Sbonwick 4380713e232SGeorge Wilson boolean_t 4390713e232SGeorge Wilson space_map_histogram_verify(space_map_t *sm, range_tree_t *rt) 4400713e232SGeorge Wilson { 4410713e232SGeorge Wilson /* 4420713e232SGeorge Wilson * Verify that the in-core range tree does not have any 4430713e232SGeorge Wilson * ranges smaller than our sm_shift size. 4440713e232SGeorge Wilson */ 4450713e232SGeorge Wilson for (int i = 0; i < sm->sm_shift; i++) { 4460713e232SGeorge Wilson if (rt->rt_histogram[i] != 0) 4470713e232SGeorge Wilson return (B_FALSE); 4480713e232SGeorge Wilson } 4490713e232SGeorge Wilson return (B_TRUE); 450fa9e4066Sahrens } 451fa9e4066Sahrens 452fa9e4066Sahrens void 4530713e232SGeorge Wilson space_map_histogram_add(space_map_t *sm, range_tree_t *rt, dmu_tx_t *tx) 454ecc2d604Sbonwick { 4550713e232SGeorge Wilson int idx = 0; 4560713e232SGeorge Wilson 4570713e232SGeorge Wilson ASSERT(dmu_tx_is_syncing(tx)); 4580713e232SGeorge Wilson VERIFY3U(space_map_object(sm), !=, 0); 459ecc2d604Sbonwick 4600713e232SGeorge Wilson if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t)) 4610713e232SGeorge Wilson return; 462ecc2d604Sbonwick 4630713e232SGeorge Wilson dmu_buf_will_dirty(sm->sm_dbuf, tx); 464ecc2d604Sbonwick 4650713e232SGeorge Wilson ASSERT(space_map_histogram_verify(sm, rt)); 4660713e232SGeorge Wilson /* 4670713e232SGeorge Wilson * Transfer the content of the range tree histogram to the space 4680713e232SGeorge Wilson * map histogram. The space map histogram contains 32 buckets ranging 4690713e232SGeorge Wilson * between 2^sm_shift to 2^(32+sm_shift-1). The range tree, 4700713e232SGeorge Wilson * however, can represent ranges from 2^0 to 2^63. Since the space 4710713e232SGeorge Wilson * map only cares about allocatable blocks (minimum of sm_shift) we 4720713e232SGeorge Wilson * can safely ignore all ranges in the range tree smaller than sm_shift. 4730713e232SGeorge Wilson */ 4740713e232SGeorge Wilson for (int i = sm->sm_shift; i < RANGE_TREE_HISTOGRAM_SIZE; i++) { 4750713e232SGeorge Wilson 4760713e232SGeorge Wilson /* 4770713e232SGeorge Wilson * Since the largest histogram bucket in the space map is 4780713e232SGeorge Wilson * 2^(32+sm_shift-1), we need to normalize the values in 4790713e232SGeorge Wilson * the range tree for any bucket larger than that size. For 4800713e232SGeorge Wilson * example given an sm_shift of 9, ranges larger than 2^40 4810713e232SGeorge Wilson * would get normalized as if they were 1TB ranges. Assume 4820713e232SGeorge Wilson * the range tree had a count of 5 in the 2^44 (16TB) bucket, 4830713e232SGeorge Wilson * the calculation below would normalize this to 5 * 2^4 (16). 4840713e232SGeorge Wilson */ 4850713e232SGeorge Wilson ASSERT3U(i, >=, idx + sm->sm_shift); 4860713e232SGeorge Wilson sm->sm_phys->smp_histogram[idx] += 4870713e232SGeorge Wilson rt->rt_histogram[i] << (i - idx - sm->sm_shift); 4880713e232SGeorge Wilson 4890713e232SGeorge Wilson /* 4900713e232SGeorge Wilson * Increment the space map's index as long as we haven't 4910713e232SGeorge Wilson * reached the maximum bucket size. Accumulate all ranges 4920713e232SGeorge Wilson * larger than the max bucket size into the last bucket. 4930713e232SGeorge Wilson */ 4942e4c9986SGeorge Wilson if (idx < SPACE_MAP_HISTOGRAM_SIZE - 1) { 4950713e232SGeorge Wilson ASSERT3U(idx + sm->sm_shift, ==, i); 4960713e232SGeorge Wilson idx++; 4972e4c9986SGeorge Wilson ASSERT3U(idx, <, SPACE_MAP_HISTOGRAM_SIZE); 4980713e232SGeorge Wilson } 4990713e232SGeorge Wilson } 500d6e555bdSGeorge Wilson } 501d6e555bdSGeorge Wilson 50217f11284SSerapheim Dimitropoulos static void 50317f11284SSerapheim Dimitropoulos space_map_write_intro_debug(space_map_t *sm, maptype_t maptype, dmu_tx_t *tx) 504ecc2d604Sbonwick { 50517f11284SSerapheim Dimitropoulos dmu_buf_will_dirty(sm->sm_dbuf, tx); 50617f11284SSerapheim Dimitropoulos 50717f11284SSerapheim Dimitropoulos uint64_t dentry = SM_PREFIX_ENCODE(SM_DEBUG_PREFIX) | 50817f11284SSerapheim Dimitropoulos SM_DEBUG_ACTION_ENCODE(maptype) | 50917f11284SSerapheim Dimitropoulos SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(tx->tx_pool->dp_spa)) | 51017f11284SSerapheim Dimitropoulos SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx)); 51117f11284SSerapheim Dimitropoulos 512*555d674dSSerapheim Dimitropoulos dmu_write(sm->sm_os, space_map_object(sm), sm->sm_phys->smp_length, 51317f11284SSerapheim Dimitropoulos sizeof (dentry), &dentry, tx); 51417f11284SSerapheim Dimitropoulos 515*555d674dSSerapheim Dimitropoulos sm->sm_phys->smp_length += sizeof (dentry); 51617f11284SSerapheim Dimitropoulos } 51717f11284SSerapheim Dimitropoulos 51817f11284SSerapheim Dimitropoulos /* 51917f11284SSerapheim Dimitropoulos * Writes one or more entries given a segment. 52017f11284SSerapheim Dimitropoulos * 52117f11284SSerapheim Dimitropoulos * Note: The function may release the dbuf from the pointer initially 52217f11284SSerapheim Dimitropoulos * passed to it, and return a different dbuf. Also, the space map's 52317f11284SSerapheim Dimitropoulos * dbuf must be dirty for the changes in sm_phys to take effect. 52417f11284SSerapheim Dimitropoulos */ 52517f11284SSerapheim Dimitropoulos static void 52617f11284SSerapheim Dimitropoulos space_map_write_seg(space_map_t *sm, range_seg_t *rs, maptype_t maptype, 52717f11284SSerapheim Dimitropoulos uint64_t vdev_id, uint8_t words, dmu_buf_t **dbp, void *tag, dmu_tx_t *tx) 52817f11284SSerapheim Dimitropoulos { 52917f11284SSerapheim Dimitropoulos ASSERT3U(words, !=, 0); 53017f11284SSerapheim Dimitropoulos ASSERT3U(words, <=, 2); 53117f11284SSerapheim Dimitropoulos 53217f11284SSerapheim Dimitropoulos /* ensure the vdev_id can be represented by the space map */ 53317f11284SSerapheim Dimitropoulos ASSERT3U(vdev_id, <=, SM_NO_VDEVID); 53417f11284SSerapheim Dimitropoulos 53517f11284SSerapheim Dimitropoulos /* 53617f11284SSerapheim Dimitropoulos * if this is a single word entry, ensure that no vdev was 53717f11284SSerapheim Dimitropoulos * specified. 53817f11284SSerapheim Dimitropoulos */ 53917f11284SSerapheim Dimitropoulos IMPLY(words == 1, vdev_id == SM_NO_VDEVID); 54017f11284SSerapheim Dimitropoulos 54117f11284SSerapheim Dimitropoulos dmu_buf_t *db = *dbp; 54217f11284SSerapheim Dimitropoulos ASSERT3U(db->db_size, ==, sm->sm_blksz); 54317f11284SSerapheim Dimitropoulos 54417f11284SSerapheim Dimitropoulos uint64_t *block_base = db->db_data; 54517f11284SSerapheim Dimitropoulos uint64_t *block_end = block_base + (sm->sm_blksz / sizeof (uint64_t)); 54617f11284SSerapheim Dimitropoulos uint64_t *block_cursor = block_base + 547*555d674dSSerapheim Dimitropoulos (sm->sm_phys->smp_length - db->db_offset) / sizeof (uint64_t); 54817f11284SSerapheim Dimitropoulos 54917f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, <=, block_end); 55017f11284SSerapheim Dimitropoulos 55117f11284SSerapheim Dimitropoulos uint64_t size = (rs->rs_end - rs->rs_start) >> sm->sm_shift; 55217f11284SSerapheim Dimitropoulos uint64_t start = (rs->rs_start - sm->sm_start) >> sm->sm_shift; 55317f11284SSerapheim Dimitropoulos uint64_t run_max = (words == 2) ? SM2_RUN_MAX : SM_RUN_MAX; 55417f11284SSerapheim Dimitropoulos 55517f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_start, >=, sm->sm_start); 55617f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_start, <, sm->sm_start + sm->sm_size); 55717f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_end - rs->rs_start, <=, sm->sm_size); 55817f11284SSerapheim Dimitropoulos ASSERT3U(rs->rs_end, <=, sm->sm_start + sm->sm_size); 55917f11284SSerapheim Dimitropoulos 56017f11284SSerapheim Dimitropoulos while (size != 0) { 56117f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, <=, block_end); 56217f11284SSerapheim Dimitropoulos 56317f11284SSerapheim Dimitropoulos /* 56417f11284SSerapheim Dimitropoulos * If we are at the end of this block, flush it and start 56517f11284SSerapheim Dimitropoulos * writing again from the beginning. 56617f11284SSerapheim Dimitropoulos */ 56717f11284SSerapheim Dimitropoulos if (block_cursor == block_end) { 56817f11284SSerapheim Dimitropoulos dmu_buf_rele(db, tag); 569ecc2d604Sbonwick 570*555d674dSSerapheim Dimitropoulos uint64_t next_word_offset = sm->sm_phys->smp_length; 57117f11284SSerapheim Dimitropoulos VERIFY0(dmu_buf_hold(sm->sm_os, 57217f11284SSerapheim Dimitropoulos space_map_object(sm), next_word_offset, 57317f11284SSerapheim Dimitropoulos tag, &db, DMU_READ_PREFETCH)); 57417f11284SSerapheim Dimitropoulos dmu_buf_will_dirty(db, tx); 57517f11284SSerapheim Dimitropoulos 57617f11284SSerapheim Dimitropoulos /* update caller's dbuf */ 57717f11284SSerapheim Dimitropoulos *dbp = db; 57817f11284SSerapheim Dimitropoulos 57917f11284SSerapheim Dimitropoulos ASSERT3U(db->db_size, ==, sm->sm_blksz); 58017f11284SSerapheim Dimitropoulos 58117f11284SSerapheim Dimitropoulos block_base = db->db_data; 58217f11284SSerapheim Dimitropoulos block_cursor = block_base; 58317f11284SSerapheim Dimitropoulos block_end = block_base + 58417f11284SSerapheim Dimitropoulos (db->db_size / sizeof (uint64_t)); 58517f11284SSerapheim Dimitropoulos } 58617f11284SSerapheim Dimitropoulos 58717f11284SSerapheim Dimitropoulos /* 58817f11284SSerapheim Dimitropoulos * If we are writing a two-word entry and we only have one 58917f11284SSerapheim Dimitropoulos * word left on this block, just pad it with an empty debug 59017f11284SSerapheim Dimitropoulos * entry and write the two-word entry in the next block. 59117f11284SSerapheim Dimitropoulos */ 59217f11284SSerapheim Dimitropoulos uint64_t *next_entry = block_cursor + 1; 59317f11284SSerapheim Dimitropoulos if (next_entry == block_end && words > 1) { 59417f11284SSerapheim Dimitropoulos ASSERT3U(words, ==, 2); 59517f11284SSerapheim Dimitropoulos *block_cursor = SM_PREFIX_ENCODE(SM_DEBUG_PREFIX) | 59617f11284SSerapheim Dimitropoulos SM_DEBUG_ACTION_ENCODE(0) | 59717f11284SSerapheim Dimitropoulos SM_DEBUG_SYNCPASS_ENCODE(0) | 59817f11284SSerapheim Dimitropoulos SM_DEBUG_TXG_ENCODE(0); 59917f11284SSerapheim Dimitropoulos block_cursor++; 600*555d674dSSerapheim Dimitropoulos sm->sm_phys->smp_length += sizeof (uint64_t); 60117f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, ==, block_end); 60217f11284SSerapheim Dimitropoulos continue; 60317f11284SSerapheim Dimitropoulos } 60417f11284SSerapheim Dimitropoulos 60517f11284SSerapheim Dimitropoulos uint64_t run_len = MIN(size, run_max); 60617f11284SSerapheim Dimitropoulos switch (words) { 60717f11284SSerapheim Dimitropoulos case 1: 60817f11284SSerapheim Dimitropoulos *block_cursor = SM_OFFSET_ENCODE(start) | 60917f11284SSerapheim Dimitropoulos SM_TYPE_ENCODE(maptype) | 61017f11284SSerapheim Dimitropoulos SM_RUN_ENCODE(run_len); 61117f11284SSerapheim Dimitropoulos block_cursor++; 61217f11284SSerapheim Dimitropoulos break; 61317f11284SSerapheim Dimitropoulos case 2: 61417f11284SSerapheim Dimitropoulos /* write the first word of the entry */ 61517f11284SSerapheim Dimitropoulos *block_cursor = SM_PREFIX_ENCODE(SM2_PREFIX) | 61617f11284SSerapheim Dimitropoulos SM2_RUN_ENCODE(run_len) | 61717f11284SSerapheim Dimitropoulos SM2_VDEV_ENCODE(vdev_id); 61817f11284SSerapheim Dimitropoulos block_cursor++; 61917f11284SSerapheim Dimitropoulos 62017f11284SSerapheim Dimitropoulos /* move on to the second word of the entry */ 62117f11284SSerapheim Dimitropoulos ASSERT3P(block_cursor, <, block_end); 62217f11284SSerapheim Dimitropoulos *block_cursor = SM2_TYPE_ENCODE(maptype) | 62317f11284SSerapheim Dimitropoulos SM2_OFFSET_ENCODE(start); 62417f11284SSerapheim Dimitropoulos block_cursor++; 62517f11284SSerapheim Dimitropoulos break; 62617f11284SSerapheim Dimitropoulos default: 62717f11284SSerapheim Dimitropoulos panic("%d-word space map entries are not supported", 62817f11284SSerapheim Dimitropoulos words); 62917f11284SSerapheim Dimitropoulos break; 63017f11284SSerapheim Dimitropoulos } 631*555d674dSSerapheim Dimitropoulos sm->sm_phys->smp_length += words * sizeof (uint64_t); 63217f11284SSerapheim Dimitropoulos 63317f11284SSerapheim Dimitropoulos start += run_len; 63417f11284SSerapheim Dimitropoulos size -= run_len; 63517f11284SSerapheim Dimitropoulos } 63617f11284SSerapheim Dimitropoulos ASSERT0(size); 63717f11284SSerapheim Dimitropoulos 63817f11284SSerapheim Dimitropoulos } 63917f11284SSerapheim Dimitropoulos 64017f11284SSerapheim Dimitropoulos /* 64117f11284SSerapheim Dimitropoulos * Note: The space map's dbuf must be dirty for the changes in sm_phys to 64217f11284SSerapheim Dimitropoulos * take effect. 64317f11284SSerapheim Dimitropoulos */ 64417f11284SSerapheim Dimitropoulos static void 64517f11284SSerapheim Dimitropoulos space_map_write_impl(space_map_t *sm, range_tree_t *rt, maptype_t maptype, 64617f11284SSerapheim Dimitropoulos uint64_t vdev_id, dmu_tx_t *tx) 64717f11284SSerapheim Dimitropoulos { 64817f11284SSerapheim Dimitropoulos spa_t *spa = tx->tx_pool->dp_spa; 64917f11284SSerapheim Dimitropoulos dmu_buf_t *db; 65017f11284SSerapheim Dimitropoulos 65117f11284SSerapheim Dimitropoulos space_map_write_intro_debug(sm, maptype, tx); 65217f11284SSerapheim Dimitropoulos 65317f11284SSerapheim Dimitropoulos #ifdef DEBUG 6540713e232SGeorge Wilson /* 65517f11284SSerapheim Dimitropoulos * We do this right after we write the intro debug entry 65617f11284SSerapheim Dimitropoulos * because the estimate does not take it into account. 6570713e232SGeorge Wilson */ 658*555d674dSSerapheim Dimitropoulos uint64_t initial_objsize = sm->sm_phys->smp_length; 65917f11284SSerapheim Dimitropoulos uint64_t estimated_growth = 66017f11284SSerapheim Dimitropoulos space_map_estimate_optimal_size(sm, rt, SM_NO_VDEVID); 66117f11284SSerapheim Dimitropoulos uint64_t estimated_final_objsize = initial_objsize + estimated_growth; 66217f11284SSerapheim Dimitropoulos #endif 663ecc2d604Sbonwick 6640713e232SGeorge Wilson /* 66517f11284SSerapheim Dimitropoulos * Find the offset right after the last word in the space map 66617f11284SSerapheim Dimitropoulos * and use that to get a hold of the last block, so we can 66717f11284SSerapheim Dimitropoulos * start appending to it. 6680713e232SGeorge Wilson */ 669*555d674dSSerapheim Dimitropoulos uint64_t next_word_offset = sm->sm_phys->smp_length; 67017f11284SSerapheim Dimitropoulos VERIFY0(dmu_buf_hold(sm->sm_os, space_map_object(sm), 67117f11284SSerapheim Dimitropoulos next_word_offset, FTAG, &db, DMU_READ_PREFETCH)); 67217f11284SSerapheim Dimitropoulos ASSERT3U(db->db_size, ==, sm->sm_blksz); 67317f11284SSerapheim Dimitropoulos 67417f11284SSerapheim Dimitropoulos dmu_buf_will_dirty(db, tx); 67517f11284SSerapheim Dimitropoulos 67617f11284SSerapheim Dimitropoulos avl_tree_t *t = &rt->rt_root; 67717f11284SSerapheim Dimitropoulos for (range_seg_t *rs = avl_first(t); rs != NULL; rs = AVL_NEXT(t, rs)) { 67817f11284SSerapheim Dimitropoulos uint64_t offset = (rs->rs_start - sm->sm_start) >> sm->sm_shift; 67917f11284SSerapheim Dimitropoulos uint64_t length = (rs->rs_end - rs->rs_start) >> sm->sm_shift; 68017f11284SSerapheim Dimitropoulos uint8_t words = 1; 68117f11284SSerapheim Dimitropoulos 68217f11284SSerapheim Dimitropoulos /* 68317f11284SSerapheim Dimitropoulos * We only write two-word entries when both of the following 68417f11284SSerapheim Dimitropoulos * are true: 68517f11284SSerapheim Dimitropoulos * 68617f11284SSerapheim Dimitropoulos * [1] The feature is enabled. 68717f11284SSerapheim Dimitropoulos * [2] The offset or run is too big for a single-word entry, 688221813c1SMatthew Ahrens * or the vdev_id is set (meaning not equal to 689221813c1SMatthew Ahrens * SM_NO_VDEVID). 69017f11284SSerapheim Dimitropoulos * 69117f11284SSerapheim Dimitropoulos * Note that for purposes of testing we've added the case that 69217f11284SSerapheim Dimitropoulos * we write two-word entries occasionally when the feature is 69317f11284SSerapheim Dimitropoulos * enabled and zfs_force_some_double_word_sm_entries has been 69417f11284SSerapheim Dimitropoulos * set. 69517f11284SSerapheim Dimitropoulos */ 69617f11284SSerapheim Dimitropoulos if (spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_V2) && 69717f11284SSerapheim Dimitropoulos (offset >= (1ULL << SM_OFFSET_BITS) || 69817f11284SSerapheim Dimitropoulos length > SM_RUN_MAX || 69917f11284SSerapheim Dimitropoulos vdev_id != SM_NO_VDEVID || 70017f11284SSerapheim Dimitropoulos (zfs_force_some_double_word_sm_entries && 70117f11284SSerapheim Dimitropoulos spa_get_random(100) == 0))) 70217f11284SSerapheim Dimitropoulos words = 2; 70317f11284SSerapheim Dimitropoulos 70417f11284SSerapheim Dimitropoulos space_map_write_seg(sm, rs, maptype, vdev_id, words, 70517f11284SSerapheim Dimitropoulos &db, FTAG, tx); 7060713e232SGeorge Wilson } 70717f11284SSerapheim Dimitropoulos 70817f11284SSerapheim Dimitropoulos dmu_buf_rele(db, FTAG); 70917f11284SSerapheim Dimitropoulos 71017f11284SSerapheim Dimitropoulos #ifdef DEBUG 71117f11284SSerapheim Dimitropoulos /* 71217f11284SSerapheim Dimitropoulos * We expect our estimation to be based on the worst case 71317f11284SSerapheim Dimitropoulos * scenario [see comment in space_map_estimate_optimal_size()]. 71417f11284SSerapheim Dimitropoulos * Therefore we expect the actual objsize to be equal or less 71517f11284SSerapheim Dimitropoulos * than whatever we estimated it to be. 71617f11284SSerapheim Dimitropoulos */ 717*555d674dSSerapheim Dimitropoulos ASSERT3U(estimated_final_objsize, >=, sm->sm_phys->smp_length); 71817f11284SSerapheim Dimitropoulos #endif 719ecc2d604Sbonwick } 720ecc2d604Sbonwick 72117f11284SSerapheim Dimitropoulos /* 72217f11284SSerapheim Dimitropoulos * Note: This function manipulates the state of the given space map but 72317f11284SSerapheim Dimitropoulos * does not hold any locks implicitly. Thus the caller is responsible 72417f11284SSerapheim Dimitropoulos * for synchronizing writes to the space map. 72517f11284SSerapheim Dimitropoulos */ 726ecc2d604Sbonwick void 7270713e232SGeorge Wilson space_map_write(space_map_t *sm, range_tree_t *rt, maptype_t maptype, 72817f11284SSerapheim Dimitropoulos uint64_t vdev_id, dmu_tx_t *tx) 729fa9e4066Sahrens { 7300713e232SGeorge Wilson objset_t *os = sm->sm_os; 731fa9e4066Sahrens 7320713e232SGeorge Wilson ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 7330713e232SGeorge Wilson VERIFY3U(space_map_object(sm), !=, 0); 73417f11284SSerapheim Dimitropoulos 7350713e232SGeorge Wilson dmu_buf_will_dirty(sm->sm_dbuf, tx); 736fa9e4066Sahrens 7370713e232SGeorge Wilson /* 7380713e232SGeorge Wilson * This field is no longer necessary since the in-core space map 7390713e232SGeorge Wilson * now contains the object number but is maintained for backwards 7400713e232SGeorge Wilson * compatibility. 7410713e232SGeorge Wilson */ 7420713e232SGeorge Wilson sm->sm_phys->smp_object = sm->sm_object; 743fa9e4066Sahrens 74486714001SSerapheim Dimitropoulos if (range_tree_is_empty(rt)) { 7450713e232SGeorge Wilson VERIFY3U(sm->sm_object, ==, sm->sm_phys->smp_object); 7460713e232SGeorge Wilson return; 7470713e232SGeorge Wilson } 748fa9e4066Sahrens 749ecc2d604Sbonwick if (maptype == SM_ALLOC) 7500713e232SGeorge Wilson sm->sm_phys->smp_alloc += range_tree_space(rt); 751ecc2d604Sbonwick else 7520713e232SGeorge Wilson sm->sm_phys->smp_alloc -= range_tree_space(rt); 753ecc2d604Sbonwick 75417f11284SSerapheim Dimitropoulos uint64_t nodes = avl_numnodes(&rt->rt_root); 75517f11284SSerapheim Dimitropoulos uint64_t rt_space = range_tree_space(rt); 7560713e232SGeorge Wilson 75717f11284SSerapheim Dimitropoulos space_map_write_impl(sm, rt, maptype, vdev_id, tx); 758fa9e4066Sahrens 75901f55e48SGeorge Wilson /* 76001f55e48SGeorge Wilson * Ensure that the space_map's accounting wasn't changed 76101f55e48SGeorge Wilson * while we were in the middle of writing it out. 76201f55e48SGeorge Wilson */ 7630713e232SGeorge Wilson VERIFY3U(nodes, ==, avl_numnodes(&rt->rt_root)); 7640713e232SGeorge Wilson VERIFY3U(range_tree_space(rt), ==, rt_space); 765fa9e4066Sahrens } 766fa9e4066Sahrens 7670713e232SGeorge Wilson static int 7680713e232SGeorge Wilson space_map_open_impl(space_map_t *sm) 769fa9e4066Sahrens { 7700713e232SGeorge Wilson int error; 7710713e232SGeorge Wilson u_longlong_t blocks; 772fa9e4066Sahrens 7730713e232SGeorge Wilson error = dmu_bonus_hold(sm->sm_os, sm->sm_object, sm, &sm->sm_dbuf); 7740713e232SGeorge Wilson if (error) 7750713e232SGeorge Wilson return (error); 7760713e232SGeorge Wilson 7770713e232SGeorge Wilson dmu_object_size_from_db(sm->sm_dbuf, &sm->sm_blksz, &blocks); 7780713e232SGeorge Wilson sm->sm_phys = sm->sm_dbuf->db_data; 7790713e232SGeorge Wilson return (0); 780fa9e4066Sahrens } 7818ad4d6ddSJeff Bonwick 7820713e232SGeorge Wilson int 7830713e232SGeorge Wilson space_map_open(space_map_t **smp, objset_t *os, uint64_t object, 7845cabbc6bSPrashanth Sreenivasa uint64_t start, uint64_t size, uint8_t shift) 7858ad4d6ddSJeff Bonwick { 7860713e232SGeorge Wilson space_map_t *sm; 7870713e232SGeorge Wilson int error; 7888ad4d6ddSJeff Bonwick 7890713e232SGeorge Wilson ASSERT(*smp == NULL); 7900713e232SGeorge Wilson ASSERT(os != NULL); 7910713e232SGeorge Wilson ASSERT(object != 0); 7928ad4d6ddSJeff Bonwick 7930713e232SGeorge Wilson sm = kmem_zalloc(sizeof (space_map_t), KM_SLEEP); 7948ad4d6ddSJeff Bonwick 7950713e232SGeorge Wilson sm->sm_start = start; 7960713e232SGeorge Wilson sm->sm_size = size; 7970713e232SGeorge Wilson sm->sm_shift = shift; 7980713e232SGeorge Wilson sm->sm_os = os; 7990713e232SGeorge Wilson sm->sm_object = object; 8008ad4d6ddSJeff Bonwick 8010713e232SGeorge Wilson error = space_map_open_impl(sm); 8020713e232SGeorge Wilson if (error != 0) { 8030713e232SGeorge Wilson space_map_close(sm); 8040713e232SGeorge Wilson return (error); 8050713e232SGeorge Wilson } 8060713e232SGeorge Wilson *smp = sm; 8070713e232SGeorge Wilson 8080713e232SGeorge Wilson return (0); 8098ad4d6ddSJeff Bonwick } 8108ad4d6ddSJeff Bonwick 8118ad4d6ddSJeff Bonwick void 8120713e232SGeorge Wilson space_map_close(space_map_t *sm) 8138ad4d6ddSJeff Bonwick { 8140713e232SGeorge Wilson if (sm == NULL) 8150713e232SGeorge Wilson return; 8168ad4d6ddSJeff Bonwick 8170713e232SGeorge Wilson if (sm->sm_dbuf != NULL) 8180713e232SGeorge Wilson dmu_buf_rele(sm->sm_dbuf, sm); 8190713e232SGeorge Wilson sm->sm_dbuf = NULL; 8200713e232SGeorge Wilson sm->sm_phys = NULL; 8218ad4d6ddSJeff Bonwick 8220713e232SGeorge Wilson kmem_free(sm, sizeof (*sm)); 8238ad4d6ddSJeff Bonwick } 8248ad4d6ddSJeff Bonwick 8258ad4d6ddSJeff Bonwick void 82686714001SSerapheim Dimitropoulos space_map_truncate(space_map_t *sm, int blocksize, dmu_tx_t *tx) 8278ad4d6ddSJeff Bonwick { 8280713e232SGeorge Wilson objset_t *os = sm->sm_os; 8290713e232SGeorge Wilson spa_t *spa = dmu_objset_spa(os); 8300713e232SGeorge Wilson dmu_object_info_t doi; 8310713e232SGeorge Wilson 8320713e232SGeorge Wilson ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 8330713e232SGeorge Wilson ASSERT(dmu_tx_is_syncing(tx)); 8343991b535SGeorge Wilson VERIFY3U(dmu_tx_get_txg(tx), <=, spa_final_dirty_txg(spa)); 8350713e232SGeorge Wilson 8360713e232SGeorge Wilson dmu_object_info_from_db(sm->sm_dbuf, &doi); 8370713e232SGeorge Wilson 838b1be2892SMatthew Ahrens /* 839b1be2892SMatthew Ahrens * If the space map has the wrong bonus size (because 840b1be2892SMatthew Ahrens * SPA_FEATURE_SPACEMAP_HISTOGRAM has recently been enabled), or 841b1be2892SMatthew Ahrens * the wrong block size (because space_map_blksz has changed), 842b1be2892SMatthew Ahrens * free and re-allocate its object with the updated sizes. 843b1be2892SMatthew Ahrens * 844b1be2892SMatthew Ahrens * Otherwise, just truncate the current object. 845b1be2892SMatthew Ahrens */ 846b1be2892SMatthew Ahrens if ((spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM) && 847b1be2892SMatthew Ahrens doi.doi_bonus_size != sizeof (space_map_phys_t)) || 848221813c1SMatthew Ahrens doi.doi_data_block_size != blocksize || 849221813c1SMatthew Ahrens doi.doi_metadata_block_size != 1 << space_map_ibs) { 8503991b535SGeorge Wilson zfs_dbgmsg("txg %llu, spa %s, sm %p, reallocating " 8513991b535SGeorge Wilson "object[%llu]: old bonus %u, old blocksz %u", 8523991b535SGeorge Wilson dmu_tx_get_txg(tx), spa_name(spa), sm, sm->sm_object, 8533991b535SGeorge Wilson doi.doi_bonus_size, doi.doi_data_block_size); 854b1be2892SMatthew Ahrens 855b1be2892SMatthew Ahrens space_map_free(sm, tx); 856b1be2892SMatthew Ahrens dmu_buf_rele(sm->sm_dbuf, sm); 857b1be2892SMatthew Ahrens 85886714001SSerapheim Dimitropoulos sm->sm_object = space_map_alloc(sm->sm_os, blocksize, tx); 859b1be2892SMatthew Ahrens VERIFY0(space_map_open_impl(sm)); 860b1be2892SMatthew Ahrens } else { 861b1be2892SMatthew Ahrens VERIFY0(dmu_free_range(os, space_map_object(sm), 0, -1ULL, tx)); 862b1be2892SMatthew Ahrens 863b1be2892SMatthew Ahrens /* 864b1be2892SMatthew Ahrens * If the spacemap is reallocated, its histogram 865b1be2892SMatthew Ahrens * will be reset. Do the same in the common case so that 866b1be2892SMatthew Ahrens * bugs related to the uncommon case do not go unnoticed. 867b1be2892SMatthew Ahrens */ 868b1be2892SMatthew Ahrens bzero(sm->sm_phys->smp_histogram, 869b1be2892SMatthew Ahrens sizeof (sm->sm_phys->smp_histogram)); 8700713e232SGeorge Wilson } 8710713e232SGeorge Wilson 8720713e232SGeorge Wilson dmu_buf_will_dirty(sm->sm_dbuf, tx); 873*555d674dSSerapheim Dimitropoulos sm->sm_phys->smp_length = 0; 8740713e232SGeorge Wilson sm->sm_phys->smp_alloc = 0; 8758ad4d6ddSJeff Bonwick } 8768ad4d6ddSJeff Bonwick 8770713e232SGeorge Wilson uint64_t 87886714001SSerapheim Dimitropoulos space_map_alloc(objset_t *os, int blocksize, dmu_tx_t *tx) 8790713e232SGeorge Wilson { 8800713e232SGeorge Wilson spa_t *spa = dmu_objset_spa(os); 8810713e232SGeorge Wilson uint64_t object; 8820713e232SGeorge Wilson int bonuslen; 8830713e232SGeorge Wilson 8842acef22dSMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 8852acef22dSMatthew Ahrens spa_feature_incr(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM, tx); 8860713e232SGeorge Wilson bonuslen = sizeof (space_map_phys_t); 8870713e232SGeorge Wilson ASSERT3U(bonuslen, <=, dmu_bonus_max()); 8880713e232SGeorge Wilson } else { 8890713e232SGeorge Wilson bonuslen = SPACE_MAP_SIZE_V0; 8900713e232SGeorge Wilson } 8910713e232SGeorge Wilson 892221813c1SMatthew Ahrens object = dmu_object_alloc_ibs(os, DMU_OT_SPACE_MAP, blocksize, 893221813c1SMatthew Ahrens space_map_ibs, DMU_OT_SPACE_MAP_HEADER, bonuslen, tx); 8940713e232SGeorge Wilson 8950713e232SGeorge Wilson return (object); 8968ad4d6ddSJeff Bonwick } 8978ad4d6ddSJeff Bonwick 8988ad4d6ddSJeff Bonwick void 8995cabbc6bSPrashanth Sreenivasa space_map_free_obj(objset_t *os, uint64_t smobj, dmu_tx_t *tx) 9008ad4d6ddSJeff Bonwick { 9015cabbc6bSPrashanth Sreenivasa spa_t *spa = dmu_objset_spa(os); 9022acef22dSMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 9030713e232SGeorge Wilson dmu_object_info_t doi; 9048ad4d6ddSJeff Bonwick 9055cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_info(os, smobj, &doi)); 9060713e232SGeorge Wilson if (doi.doi_bonus_size != SPACE_MAP_SIZE_V0) { 9072acef22dSMatthew Ahrens spa_feature_decr(spa, 9082acef22dSMatthew Ahrens SPA_FEATURE_SPACEMAP_HISTOGRAM, tx); 9098ad4d6ddSJeff Bonwick } 9108ad4d6ddSJeff Bonwick } 9110713e232SGeorge Wilson 9125cabbc6bSPrashanth Sreenivasa VERIFY0(dmu_object_free(os, smobj, tx)); 9135cabbc6bSPrashanth Sreenivasa } 9145cabbc6bSPrashanth Sreenivasa 9155cabbc6bSPrashanth Sreenivasa void 9165cabbc6bSPrashanth Sreenivasa space_map_free(space_map_t *sm, dmu_tx_t *tx) 9175cabbc6bSPrashanth Sreenivasa { 9185cabbc6bSPrashanth Sreenivasa if (sm == NULL) 9195cabbc6bSPrashanth Sreenivasa return; 9205cabbc6bSPrashanth Sreenivasa 9215cabbc6bSPrashanth Sreenivasa space_map_free_obj(sm->sm_os, space_map_object(sm), tx); 9220713e232SGeorge Wilson sm->sm_object = 0; 9230713e232SGeorge Wilson } 9240713e232SGeorge Wilson 92517f11284SSerapheim Dimitropoulos /* 92617f11284SSerapheim Dimitropoulos * Given a range tree, it makes a worst-case estimate of how much 92717f11284SSerapheim Dimitropoulos * space would the tree's segments take if they were written to 92817f11284SSerapheim Dimitropoulos * the given space map. 92917f11284SSerapheim Dimitropoulos */ 93017f11284SSerapheim Dimitropoulos uint64_t 93117f11284SSerapheim Dimitropoulos space_map_estimate_optimal_size(space_map_t *sm, range_tree_t *rt, 93217f11284SSerapheim Dimitropoulos uint64_t vdev_id) 93317f11284SSerapheim Dimitropoulos { 93417f11284SSerapheim Dimitropoulos spa_t *spa = dmu_objset_spa(sm->sm_os); 93517f11284SSerapheim Dimitropoulos uint64_t shift = sm->sm_shift; 93617f11284SSerapheim Dimitropoulos uint64_t *histogram = rt->rt_histogram; 93717f11284SSerapheim Dimitropoulos uint64_t entries_for_seg = 0; 93817f11284SSerapheim Dimitropoulos 93917f11284SSerapheim Dimitropoulos /* 94017f11284SSerapheim Dimitropoulos * In order to get a quick estimate of the optimal size that this 94117f11284SSerapheim Dimitropoulos * range tree would have on-disk as a space map, we iterate through 94217f11284SSerapheim Dimitropoulos * its histogram buckets instead of iterating through its nodes. 94317f11284SSerapheim Dimitropoulos * 94417f11284SSerapheim Dimitropoulos * Note that this is a highest-bound/worst-case estimate for the 94517f11284SSerapheim Dimitropoulos * following reasons: 94617f11284SSerapheim Dimitropoulos * 94717f11284SSerapheim Dimitropoulos * 1] We assume that we always add a debug padding for each block 94817f11284SSerapheim Dimitropoulos * we write and we also assume that we start at the last word 94917f11284SSerapheim Dimitropoulos * of a block attempting to write a two-word entry. 95017f11284SSerapheim Dimitropoulos * 2] Rounding up errors due to the way segments are distributed 95117f11284SSerapheim Dimitropoulos * in the buckets of the range tree's histogram. 95217f11284SSerapheim Dimitropoulos * 3] The activation of zfs_force_some_double_word_sm_entries 95317f11284SSerapheim Dimitropoulos * (tunable) when testing. 95417f11284SSerapheim Dimitropoulos * 95517f11284SSerapheim Dimitropoulos * = Math and Rounding Errors = 95617f11284SSerapheim Dimitropoulos * 95717f11284SSerapheim Dimitropoulos * rt_histogram[i] bucket of a range tree represents the number 95817f11284SSerapheim Dimitropoulos * of entries in [2^i, (2^(i+1))-1] of that range_tree. Given 95917f11284SSerapheim Dimitropoulos * that, we want to divide the buckets into groups: Buckets that 96017f11284SSerapheim Dimitropoulos * can be represented using a single-word entry, ones that can 96117f11284SSerapheim Dimitropoulos * be represented with a double-word entry, and ones that can 96217f11284SSerapheim Dimitropoulos * only be represented with multiple two-word entries. 96317f11284SSerapheim Dimitropoulos * 96417f11284SSerapheim Dimitropoulos * [Note that if the new encoding feature is not enabled there 96517f11284SSerapheim Dimitropoulos * are only two groups: single-word entry buckets and multiple 96617f11284SSerapheim Dimitropoulos * single-word entry buckets. The information below assumes 96717f11284SSerapheim Dimitropoulos * two-word entries enabled, but it can easily applied when 96817f11284SSerapheim Dimitropoulos * the feature is not enabled] 96917f11284SSerapheim Dimitropoulos * 97017f11284SSerapheim Dimitropoulos * To find the highest bucket that can be represented with a 97117f11284SSerapheim Dimitropoulos * single-word entry we look at the maximum run that such entry 97217f11284SSerapheim Dimitropoulos * can have, which is 2^(SM_RUN_BITS + sm_shift) [remember that 97317f11284SSerapheim Dimitropoulos * the run of a space map entry is shifted by sm_shift, thus we 97417f11284SSerapheim Dimitropoulos * add it to the exponent]. This way, excluding the value of the 97517f11284SSerapheim Dimitropoulos * maximum run that can be represented by a single-word entry, 97617f11284SSerapheim Dimitropoulos * all runs that are smaller exist in buckets 0 to 97717f11284SSerapheim Dimitropoulos * SM_RUN_BITS + shift - 1. 97817f11284SSerapheim Dimitropoulos * 97917f11284SSerapheim Dimitropoulos * To find the highest bucket that can be represented with a 98017f11284SSerapheim Dimitropoulos * double-word entry, we follow the same approach. Finally, any 98117f11284SSerapheim Dimitropoulos * bucket higher than that are represented with multiple two-word 98217f11284SSerapheim Dimitropoulos * entries. To be more specific, if the highest bucket whose 98317f11284SSerapheim Dimitropoulos * segments can be represented with a single two-word entry is X, 98417f11284SSerapheim Dimitropoulos * then bucket X+1 will need 2 two-word entries for each of its 98517f11284SSerapheim Dimitropoulos * segments, X+2 will need 4, X+3 will need 8, ...etc. 98617f11284SSerapheim Dimitropoulos * 98717f11284SSerapheim Dimitropoulos * With all of the above we make our estimation based on bucket 98817f11284SSerapheim Dimitropoulos * groups. There is a rounding error though. As we mentioned in 98917f11284SSerapheim Dimitropoulos * the example with the one-word entry, the maximum run that can 99017f11284SSerapheim Dimitropoulos * be represented in a one-word entry 2^(SM_RUN_BITS + shift) is 99117f11284SSerapheim Dimitropoulos * not part of bucket SM_RUN_BITS + shift - 1. Thus, segments of 99217f11284SSerapheim Dimitropoulos * that length fall into the next bucket (and bucket group) where 99317f11284SSerapheim Dimitropoulos * we start counting two-word entries and this is one more reason 99417f11284SSerapheim Dimitropoulos * why the estimated size may end up being bigger than the actual 99517f11284SSerapheim Dimitropoulos * size written. 99617f11284SSerapheim Dimitropoulos */ 99717f11284SSerapheim Dimitropoulos uint64_t size = 0; 99817f11284SSerapheim Dimitropoulos uint64_t idx = 0; 99917f11284SSerapheim Dimitropoulos 100017f11284SSerapheim Dimitropoulos if (!spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2) || 100117f11284SSerapheim Dimitropoulos (vdev_id == SM_NO_VDEVID && sm->sm_size < SM_OFFSET_MAX)) { 100217f11284SSerapheim Dimitropoulos 100317f11284SSerapheim Dimitropoulos /* 100417f11284SSerapheim Dimitropoulos * If we are trying to force some double word entries just 100517f11284SSerapheim Dimitropoulos * assume the worst-case of every single word entry being 100617f11284SSerapheim Dimitropoulos * written as a double word entry. 100717f11284SSerapheim Dimitropoulos */ 100817f11284SSerapheim Dimitropoulos uint64_t entry_size = 100917f11284SSerapheim Dimitropoulos (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2) && 101017f11284SSerapheim Dimitropoulos zfs_force_some_double_word_sm_entries) ? 101117f11284SSerapheim Dimitropoulos (2 * sizeof (uint64_t)) : sizeof (uint64_t); 101217f11284SSerapheim Dimitropoulos 101317f11284SSerapheim Dimitropoulos uint64_t single_entry_max_bucket = SM_RUN_BITS + shift - 1; 101417f11284SSerapheim Dimitropoulos for (; idx <= single_entry_max_bucket; idx++) 101517f11284SSerapheim Dimitropoulos size += histogram[idx] * entry_size; 101617f11284SSerapheim Dimitropoulos 101717f11284SSerapheim Dimitropoulos if (!spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2)) { 101817f11284SSerapheim Dimitropoulos for (; idx < RANGE_TREE_HISTOGRAM_SIZE; idx++) { 101917f11284SSerapheim Dimitropoulos ASSERT3U(idx, >=, single_entry_max_bucket); 102017f11284SSerapheim Dimitropoulos entries_for_seg = 102117f11284SSerapheim Dimitropoulos 1ULL << (idx - single_entry_max_bucket); 102217f11284SSerapheim Dimitropoulos size += histogram[idx] * 102317f11284SSerapheim Dimitropoulos entries_for_seg * entry_size; 102417f11284SSerapheim Dimitropoulos } 102517f11284SSerapheim Dimitropoulos return (size); 102617f11284SSerapheim Dimitropoulos } 102717f11284SSerapheim Dimitropoulos } 102817f11284SSerapheim Dimitropoulos 102917f11284SSerapheim Dimitropoulos ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_V2)); 103017f11284SSerapheim Dimitropoulos 103117f11284SSerapheim Dimitropoulos uint64_t double_entry_max_bucket = SM2_RUN_BITS + shift - 1; 103217f11284SSerapheim Dimitropoulos for (; idx <= double_entry_max_bucket; idx++) 103317f11284SSerapheim Dimitropoulos size += histogram[idx] * 2 * sizeof (uint64_t); 103417f11284SSerapheim Dimitropoulos 103517f11284SSerapheim Dimitropoulos for (; idx < RANGE_TREE_HISTOGRAM_SIZE; idx++) { 103617f11284SSerapheim Dimitropoulos ASSERT3U(idx, >=, double_entry_max_bucket); 103717f11284SSerapheim Dimitropoulos entries_for_seg = 1ULL << (idx - double_entry_max_bucket); 103817f11284SSerapheim Dimitropoulos size += histogram[idx] * 103917f11284SSerapheim Dimitropoulos entries_for_seg * 2 * sizeof (uint64_t); 104017f11284SSerapheim Dimitropoulos } 104117f11284SSerapheim Dimitropoulos 104217f11284SSerapheim Dimitropoulos /* 104317f11284SSerapheim Dimitropoulos * Assume the worst case where we start with the padding at the end 104417f11284SSerapheim Dimitropoulos * of the current block and we add an extra padding entry at the end 104517f11284SSerapheim Dimitropoulos * of all subsequent blocks. 104617f11284SSerapheim Dimitropoulos */ 104717f11284SSerapheim Dimitropoulos size += ((size / sm->sm_blksz) + 1) * sizeof (uint64_t); 104817f11284SSerapheim Dimitropoulos 104917f11284SSerapheim Dimitropoulos return (size); 105017f11284SSerapheim Dimitropoulos } 105117f11284SSerapheim Dimitropoulos 10520713e232SGeorge Wilson uint64_t 10530713e232SGeorge Wilson space_map_object(space_map_t *sm) 10540713e232SGeorge Wilson { 10550713e232SGeorge Wilson return (sm != NULL ? sm->sm_object : 0); 10560713e232SGeorge Wilson } 10570713e232SGeorge Wilson 1058*555d674dSSerapheim Dimitropoulos int64_t 10590713e232SGeorge Wilson space_map_allocated(space_map_t *sm) 10600713e232SGeorge Wilson { 1061*555d674dSSerapheim Dimitropoulos return (sm != NULL ? sm->sm_phys->smp_alloc : 0); 10620713e232SGeorge Wilson } 10630713e232SGeorge Wilson 10640713e232SGeorge Wilson uint64_t 10650713e232SGeorge Wilson space_map_length(space_map_t *sm) 10660713e232SGeorge Wilson { 1067*555d674dSSerapheim Dimitropoulos return (sm != NULL ? sm->sm_phys->smp_length : 0); 10688ad4d6ddSJeff Bonwick } 1069