Path: blob/main/sys/contrib/openzfs/module/zfs/dmu.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.23* Copyright (c) 2011, 2020 by Delphix. All rights reserved.24* Copyright (c) 2013 by Saso Kiselkov. All rights reserved.25* Copyright (c) 2013, Joyent, Inc. All rights reserved.26* Copyright (c) 2016, Nexenta Systems, Inc. All rights reserved.27* Copyright (c) 2015 by Chunwei Chen. All rights reserved.28* Copyright (c) 2019 Datto Inc.29* Copyright (c) 2019, 2023, Klara Inc.30* Copyright (c) 2019, Allan Jude31* Copyright (c) 2022 Hewlett Packard Enterprise Development LP.32* Copyright (c) 2021, 2022 by Pawel Jakub Dawidek33*/3435#include <sys/dmu.h>36#include <sys/dmu_impl.h>37#include <sys/dmu_tx.h>38#include <sys/dbuf.h>39#include <sys/dnode.h>40#include <sys/zfs_context.h>41#include <sys/dmu_objset.h>42#include <sys/dmu_traverse.h>43#include <sys/dsl_dataset.h>44#include <sys/dsl_dir.h>45#include <sys/dsl_pool.h>46#include <sys/dsl_synctask.h>47#include <sys/dsl_prop.h>48#include <sys/dmu_zfetch.h>49#include <sys/zfs_ioctl.h>50#include <sys/zap.h>51#include <sys/zio_checksum.h>52#include <sys/zio_compress.h>53#include <sys/sa.h>54#include <sys/zfeature.h>55#include <sys/abd.h>56#include <sys/brt.h>57#include <sys/trace_zfs.h>58#include <sys/zfs_racct.h>59#include <sys/zfs_rlock.h>60#ifdef _KERNEL61#include <sys/vmsystm.h>62#include <sys/zfs_znode.h>63#endif6465/*66* Enable/disable nopwrite feature.67*/68static int zfs_nopwrite_enabled = 1;6970/*71* Tunable to control percentage of dirtied L1 blocks from frees allowed into72* one TXG. After this threshold is crossed, additional dirty blocks from frees73* will wait until the next TXG.74* A value of zero will disable this throttle.75*/76static uint_t zfs_per_txg_dirty_frees_percent = 30;7778/*79* Enable/disable forcing txg sync when dirty checking for holes with lseek().80* By default this is enabled to ensure accurate hole reporting, it can result81* in a significant performance penalty for lseek(SEEK_HOLE) heavy workloads.82* Disabling this option will result in holes never being reported in dirty83* files which is always safe.84*/85static int zfs_dmu_offset_next_sync = 1;8687/*88* Limit the amount we can prefetch with one call to this amount. This89* helps to limit the amount of memory that can be used by prefetching.90* Larger objects should be prefetched a bit at a time.91*/92#ifdef _ILP3293uint_t dmu_prefetch_max = 8 * 1024 * 1024;94#else95uint_t dmu_prefetch_max = 8 * SPA_MAXBLOCKSIZE;96#endif9798/*99* Override copies= for dedup state objects. 0 means the traditional behaviour100* (ie the default for the containing objset ie 3 for the MOS).101*/102uint_t dmu_ddt_copies = 0;103104const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {105{DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "unallocated" },106{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "object directory" },107{DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "object array" },108{DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "packed nvlist" },109{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "packed nvlist size" },110{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj" },111{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj header" },112{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map header" },113{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map" },114{DMU_BSWAP_UINT64, TRUE, FALSE, TRUE, "ZIL intent log" },115{DMU_BSWAP_DNODE, TRUE, FALSE, TRUE, "DMU dnode" },116{DMU_BSWAP_OBJSET, TRUE, TRUE, FALSE, "DMU objset" },117{DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL directory" },118{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL directory child map"},119{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset snap map" },120{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL props" },121{DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL dataset" },122{DMU_BSWAP_ZNODE, TRUE, FALSE, FALSE, "ZFS znode" },123{DMU_BSWAP_OLDACL, TRUE, FALSE, TRUE, "ZFS V0 ACL" },124{DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "ZFS plain file" },125{DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS directory" },126{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "ZFS master node" },127{DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS delete queue" },128{DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "zvol object" },129{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "zvol prop" },130{DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "other uint8[]" },131{DMU_BSWAP_UINT64, FALSE, FALSE, TRUE, "other uint64[]" },132{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "other ZAP" },133{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "persistent error log" },134{DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "SPA history" },135{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA history offsets" },136{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "Pool properties" },137{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL permissions" },138{DMU_BSWAP_ACL, TRUE, FALSE, TRUE, "ZFS ACL" },139{DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "ZFS SYSACL" },140{DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "FUID table" },141{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "FUID table size" },142{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset next clones"},143{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan work queue" },144{DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project used" },145{DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project quota"},146{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "snapshot refcount tags"},147{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT ZAP algorithm" },148{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT statistics" },149{DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "System attributes" },150{DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA master node" },151{DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr registration" },152{DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr layouts" },153{DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan translations" },154{DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "deduplicated block" },155{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL deadlist map" },156{DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL deadlist map hdr" },157{DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dir clones" },158{DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj subobj" }159};160161dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {162{ byteswap_uint8_array, "uint8" },163{ byteswap_uint16_array, "uint16" },164{ byteswap_uint32_array, "uint32" },165{ byteswap_uint64_array, "uint64" },166{ zap_byteswap, "zap" },167{ dnode_buf_byteswap, "dnode" },168{ dmu_objset_byteswap, "objset" },169{ zfs_znode_byteswap, "znode" },170{ zfs_oldacl_byteswap, "oldacl" },171{ zfs_acl_byteswap, "acl" }172};173174int175dmu_buf_hold_noread_by_dnode(dnode_t *dn, uint64_t offset,176const void *tag, dmu_buf_t **dbp)177{178uint64_t blkid;179dmu_buf_impl_t *db;180181rw_enter(&dn->dn_struct_rwlock, RW_READER);182blkid = dbuf_whichblock(dn, 0, offset);183db = dbuf_hold(dn, blkid, tag);184rw_exit(&dn->dn_struct_rwlock);185186if (db == NULL) {187*dbp = NULL;188return (SET_ERROR(EIO));189}190191*dbp = &db->db;192return (0);193}194195int196dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset,197const void *tag, dmu_buf_t **dbp)198{199dnode_t *dn;200uint64_t blkid;201dmu_buf_impl_t *db;202int err;203204err = dnode_hold(os, object, FTAG, &dn);205if (err)206return (err);207rw_enter(&dn->dn_struct_rwlock, RW_READER);208blkid = dbuf_whichblock(dn, 0, offset);209db = dbuf_hold(dn, blkid, tag);210rw_exit(&dn->dn_struct_rwlock);211dnode_rele(dn, FTAG);212213if (db == NULL) {214*dbp = NULL;215return (SET_ERROR(EIO));216}217218*dbp = &db->db;219return (err);220}221222int223dmu_buf_hold_by_dnode(dnode_t *dn, uint64_t offset,224const void *tag, dmu_buf_t **dbp, dmu_flags_t flags)225{226int err;227228err = dmu_buf_hold_noread_by_dnode(dn, offset, tag, dbp);229if (err == 0) {230dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);231err = dbuf_read(db, NULL, flags | DB_RF_CANFAIL);232if (err != 0) {233dbuf_rele(db, tag);234*dbp = NULL;235}236}237238return (err);239}240241int242dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,243const void *tag, dmu_buf_t **dbp, dmu_flags_t flags)244{245int err;246247err = dmu_buf_hold_noread(os, object, offset, tag, dbp);248if (err == 0) {249dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);250err = dbuf_read(db, NULL, flags | DB_RF_CANFAIL);251if (err != 0) {252dbuf_rele(db, tag);253*dbp = NULL;254}255}256257return (err);258}259260int261dmu_bonus_max(void)262{263return (DN_OLD_MAX_BONUSLEN);264}265266int267dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)268{269dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;270dnode_t *dn;271int error;272273if (newsize < 0 || newsize > db_fake->db_size)274return (SET_ERROR(EINVAL));275276DB_DNODE_ENTER(db);277dn = DB_DNODE(db);278279if (dn->dn_bonus != db) {280error = SET_ERROR(EINVAL);281} else {282dnode_setbonuslen(dn, newsize, tx);283error = 0;284}285286DB_DNODE_EXIT(db);287return (error);288}289290int291dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)292{293dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;294dnode_t *dn;295int error;296297if (!DMU_OT_IS_VALID(type))298return (SET_ERROR(EINVAL));299300DB_DNODE_ENTER(db);301dn = DB_DNODE(db);302303if (dn->dn_bonus != db) {304error = SET_ERROR(EINVAL);305} else {306dnode_setbonus_type(dn, type, tx);307error = 0;308}309310DB_DNODE_EXIT(db);311return (error);312}313314dmu_object_type_t315dmu_get_bonustype(dmu_buf_t *db_fake)316{317dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;318dmu_object_type_t type;319320DB_DNODE_ENTER(db);321type = DB_DNODE(db)->dn_bonustype;322DB_DNODE_EXIT(db);323324return (type);325}326327int328dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)329{330dnode_t *dn;331int error;332333error = dnode_hold(os, object, FTAG, &dn);334dbuf_rm_spill(dn, tx);335rw_enter(&dn->dn_struct_rwlock, RW_WRITER);336dnode_rm_spill(dn, tx);337rw_exit(&dn->dn_struct_rwlock);338dnode_rele(dn, FTAG);339return (error);340}341342/*343* Lookup and hold the bonus buffer for the provided dnode. If the dnode344* has not yet been allocated a new bonus dbuf a will be allocated.345* Returns ENOENT, EIO, or 0.346*/347int dmu_bonus_hold_by_dnode(dnode_t *dn, const void *tag, dmu_buf_t **dbp,348dmu_flags_t flags)349{350dmu_buf_impl_t *db;351int error;352353rw_enter(&dn->dn_struct_rwlock, RW_READER);354if (dn->dn_bonus == NULL) {355if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {356rw_exit(&dn->dn_struct_rwlock);357rw_enter(&dn->dn_struct_rwlock, RW_WRITER);358}359if (dn->dn_bonus == NULL)360dbuf_create_bonus(dn);361}362db = dn->dn_bonus;363364/* as long as the bonus buf is held, the dnode will be held */365if (zfs_refcount_add(&db->db_holds, tag) == 1) {366VERIFY(dnode_add_ref(dn, db));367atomic_inc_32(&dn->dn_dbufs_count);368}369370/*371* Wait to drop dn_struct_rwlock until after adding the bonus dbuf's372* hold and incrementing the dbuf count to ensure that dnode_move() sees373* a dnode hold for every dbuf.374*/375rw_exit(&dn->dn_struct_rwlock);376377error = dbuf_read(db, NULL, flags | DB_RF_CANFAIL);378if (error) {379dnode_evict_bonus(dn);380dbuf_rele(db, tag);381*dbp = NULL;382return (error);383}384385*dbp = &db->db;386return (0);387}388389int390dmu_bonus_hold(objset_t *os, uint64_t object, const void *tag, dmu_buf_t **dbp)391{392dnode_t *dn;393int error;394395error = dnode_hold(os, object, FTAG, &dn);396if (error)397return (error);398399error = dmu_bonus_hold_by_dnode(dn, tag, dbp, DMU_READ_NO_PREFETCH);400dnode_rele(dn, FTAG);401402return (error);403}404405/*406* returns ENOENT, EIO, or 0.407*408* This interface will allocate a blank spill dbuf when a spill blk409* doesn't already exist on the dnode.410*411* if you only want to find an already existing spill db, then412* dmu_spill_hold_existing() should be used.413*/414int415dmu_spill_hold_by_dnode(dnode_t *dn, dmu_flags_t flags, const void *tag,416dmu_buf_t **dbp)417{418dmu_buf_impl_t *db = NULL;419int err;420421if ((flags & DB_RF_HAVESTRUCT) == 0)422rw_enter(&dn->dn_struct_rwlock, RW_READER);423424db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);425426if ((flags & DB_RF_HAVESTRUCT) == 0)427rw_exit(&dn->dn_struct_rwlock);428429if (db == NULL) {430*dbp = NULL;431return (SET_ERROR(EIO));432}433err = dbuf_read(db, NULL, flags);434if (err == 0)435*dbp = &db->db;436else {437dbuf_rele(db, tag);438*dbp = NULL;439}440return (err);441}442443int444dmu_spill_hold_existing(dmu_buf_t *bonus, const void *tag, dmu_buf_t **dbp)445{446dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;447dnode_t *dn;448int err;449450DB_DNODE_ENTER(db);451dn = DB_DNODE(db);452453if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {454err = SET_ERROR(EINVAL);455} else {456rw_enter(&dn->dn_struct_rwlock, RW_READER);457458if (!dn->dn_have_spill) {459err = SET_ERROR(ENOENT);460} else {461err = dmu_spill_hold_by_dnode(dn,462DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);463}464465rw_exit(&dn->dn_struct_rwlock);466}467468DB_DNODE_EXIT(db);469return (err);470}471472int473dmu_spill_hold_by_bonus(dmu_buf_t *bonus, dmu_flags_t flags, const void *tag,474dmu_buf_t **dbp)475{476dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;477int err;478479DB_DNODE_ENTER(db);480err = dmu_spill_hold_by_dnode(DB_DNODE(db), flags, tag, dbp);481DB_DNODE_EXIT(db);482483return (err);484}485486/*487* Note: longer-term, we should modify all of the dmu_buf_*() interfaces488* to take a held dnode rather than <os, object> -- the lookup is wasteful,489* and can induce severe lock contention when writing to several files490* whose dnodes are in the same block.491*/492int493dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,494boolean_t read, const void *tag, int *numbufsp, dmu_buf_t ***dbpp,495dmu_flags_t flags)496{497dmu_buf_t **dbp;498zstream_t *zs = NULL;499uint64_t blkid, nblks, i;500dmu_flags_t dbuf_flags;501int err;502zio_t *zio = NULL;503boolean_t missed = B_FALSE;504505ASSERT(!read || length <= DMU_MAX_ACCESS);506507/*508* Note: We directly notify the prefetch code of this read, so that509* we can tell it about the multi-block read. dbuf_read() only knows510* about the one block it is accessing.511*/512dbuf_flags = (flags & ~DMU_READ_PREFETCH) | DMU_READ_NO_PREFETCH |513DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT;514515rw_enter(&dn->dn_struct_rwlock, RW_READER);516if (dn->dn_datablkshift) {517int blkshift = dn->dn_datablkshift;518nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) -519P2ALIGN_TYPED(offset, 1ULL << blkshift, uint64_t))520>> blkshift;521} else {522if (offset + length > dn->dn_datablksz) {523zfs_panic_recover("zfs: accessing past end of object "524"%llx/%llx (size=%u access=%llu+%llu)",525(longlong_t)dn->dn_objset->526os_dsl_dataset->ds_object,527(longlong_t)dn->dn_object, dn->dn_datablksz,528(longlong_t)offset, (longlong_t)length);529rw_exit(&dn->dn_struct_rwlock);530return (SET_ERROR(EIO));531}532nblks = 1;533}534dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);535536if (read)537zio = zio_root(dn->dn_objset->os_spa, NULL, NULL,538ZIO_FLAG_CANFAIL);539blkid = dbuf_whichblock(dn, 0, offset);540if ((flags & DMU_READ_NO_PREFETCH) == 0) {541/*542* Prepare the zfetch before initiating the demand reads, so543* that if multiple threads block on same indirect block, we544* base predictions on the original less racy request order.545*/546zs = dmu_zfetch_prepare(&dn->dn_zfetch, blkid, nblks,547read && !(flags & DMU_DIRECTIO), B_TRUE);548}549for (i = 0; i < nblks; i++) {550dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag);551if (db == NULL) {552if (zs) {553dmu_zfetch_run(&dn->dn_zfetch, zs, missed,554B_TRUE, (flags & DMU_UNCACHEDIO));555}556rw_exit(&dn->dn_struct_rwlock);557dmu_buf_rele_array(dbp, nblks, tag);558if (read)559zio_nowait(zio);560return (SET_ERROR(EIO));561}562563/*564* Initiate async demand data read.565* We check the db_state after calling dbuf_read() because566* (1) dbuf_read() may change the state to CACHED due to a567* hit in the ARC, and (2) on a cache miss, a child will568* have been added to "zio" but not yet completed, so the569* state will not yet be CACHED.570*/571if (read) {572if (i == nblks - 1 && blkid + i < dn->dn_maxblkid &&573offset + length < db->db.db_offset +574db->db.db_size) {575if (offset <= db->db.db_offset)576dbuf_flags |= DMU_PARTIAL_FIRST;577else578dbuf_flags |= DMU_PARTIAL_MORE;579}580(void) dbuf_read(db, zio, dbuf_flags);581if (db->db_state != DB_CACHED)582missed = B_TRUE;583}584dbp[i] = &db->db;585}586587/*588* If we are doing O_DIRECT we still hold the dbufs, even for reads,589* but we do not issue any reads here. We do not want to account for590* writes in this case.591*592* O_DIRECT write/read accounting takes place in593* dmu_{write/read}_abd().594*/595if (!read && ((flags & DMU_DIRECTIO) == 0))596zfs_racct_write(dn->dn_objset->os_spa, length, nblks, flags);597598if (zs) {599dmu_zfetch_run(&dn->dn_zfetch, zs, missed, B_TRUE,600(flags & DMU_UNCACHEDIO));601}602rw_exit(&dn->dn_struct_rwlock);603604if (read) {605/* wait for async read i/o */606err = zio_wait(zio);607if (err) {608dmu_buf_rele_array(dbp, nblks, tag);609return (err);610}611612/* wait for other io to complete */613for (i = 0; i < nblks; i++) {614dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];615mutex_enter(&db->db_mtx);616while (db->db_state == DB_READ ||617db->db_state == DB_FILL)618cv_wait(&db->db_changed, &db->db_mtx);619if (db->db_state == DB_UNCACHED)620err = SET_ERROR(EIO);621mutex_exit(&db->db_mtx);622if (err) {623dmu_buf_rele_array(dbp, nblks, tag);624return (err);625}626}627}628629*numbufsp = nblks;630*dbpp = dbp;631return (0);632}633634int635dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,636uint64_t length, int read, const void *tag, int *numbufsp,637dmu_buf_t ***dbpp)638{639dnode_t *dn;640int err;641642err = dnode_hold(os, object, FTAG, &dn);643if (err)644return (err);645646err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,647numbufsp, dbpp, DMU_READ_PREFETCH);648649dnode_rele(dn, FTAG);650651return (err);652}653654int655dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,656uint64_t length, boolean_t read, const void *tag, int *numbufsp,657dmu_buf_t ***dbpp)658{659dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;660int err;661662DB_DNODE_ENTER(db);663err = dmu_buf_hold_array_by_dnode(DB_DNODE(db), offset, length, read,664tag, numbufsp, dbpp, DMU_READ_PREFETCH);665DB_DNODE_EXIT(db);666667return (err);668}669670void671dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, const void *tag)672{673int i;674dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;675676if (numbufs == 0)677return;678679for (i = 0; i < numbufs; i++) {680if (dbp[i])681dbuf_rele(dbp[i], tag);682}683684kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);685}686687/*688* Issue prefetch I/Os for the given blocks. If level is greater than 0, the689* indirect blocks prefetched will be those that point to the blocks containing690* the data starting at offset, and continuing to offset + len. If the range691* is too long, prefetch the first dmu_prefetch_max bytes as requested, while692* for the rest only a higher level, also fitting within dmu_prefetch_max. It693* should primarily help random reads, since for long sequential reads there is694* a speculative prefetcher.695*696* Note that if the indirect blocks above the blocks being prefetched are not697* in cache, they will be asynchronously read in. Dnode read by dnode_hold()698* is currently synchronous.699*/700void701dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset,702uint64_t len, zio_priority_t pri)703{704dnode_t *dn;705706if (dmu_prefetch_max == 0 || len == 0) {707dmu_prefetch_dnode(os, object, pri);708return;709}710711if (dnode_hold(os, object, FTAG, &dn) != 0)712return;713714dmu_prefetch_by_dnode(dn, level, offset, len, pri);715716dnode_rele(dn, FTAG);717}718719void720dmu_prefetch_by_dnode(dnode_t *dn, int64_t level, uint64_t offset,721uint64_t len, zio_priority_t pri)722{723int64_t level2 = level;724uint64_t start, end, start2, end2;725726/*727* Depending on len we may do two prefetches: blocks [start, end) at728* level, and following blocks [start2, end2) at higher level2.729*/730rw_enter(&dn->dn_struct_rwlock, RW_READER);731if (dn->dn_datablkshift != 0) {732733/*734* Limit prefetch to present blocks.735*/736uint64_t size = (dn->dn_maxblkid + 1) << dn->dn_datablkshift;737if (offset >= size) {738rw_exit(&dn->dn_struct_rwlock);739return;740}741if (offset + len < offset || offset + len > size)742len = size - offset;743744/*745* The object has multiple blocks. Calculate the full range746* of blocks [start, end2) and then split it into two parts,747* so that the first [start, end) fits into dmu_prefetch_max.748*/749start = dbuf_whichblock(dn, level, offset);750end2 = dbuf_whichblock(dn, level, offset + len - 1) + 1;751uint8_t ibs = dn->dn_indblkshift;752uint8_t bs = (level == 0) ? dn->dn_datablkshift : ibs;753uint_t limit = P2ROUNDUP(dmu_prefetch_max, 1 << bs) >> bs;754start2 = end = MIN(end2, start + limit);755756/*757* Find level2 where [start2, end2) fits into dmu_prefetch_max.758*/759uint8_t ibps = ibs - SPA_BLKPTRSHIFT;760limit = P2ROUNDUP(dmu_prefetch_max, 1 << ibs) >> ibs;761if (limit == 0)762end2 = start2;763do {764level2++;765start2 = P2ROUNDUP(start2, 1 << ibps) >> ibps;766end2 = P2ROUNDUP(end2, 1 << ibps) >> ibps;767} while (end2 - start2 > limit);768} else {769/* There is only one block. Prefetch it or nothing. */770start = start2 = end2 = 0;771end = start + (level == 0 && offset < dn->dn_datablksz);772}773774for (uint64_t i = start; i < end; i++)775dbuf_prefetch(dn, level, i, pri, 0);776for (uint64_t i = start2; i < end2; i++)777dbuf_prefetch(dn, level2, i, pri, 0);778rw_exit(&dn->dn_struct_rwlock);779}780781typedef struct {782kmutex_t dpa_lock;783kcondvar_t dpa_cv;784uint64_t dpa_pending_io;785} dmu_prefetch_arg_t;786787static void788dmu_prefetch_done(void *arg, uint64_t level, uint64_t blkid, boolean_t issued)789{790(void) level; (void) blkid; (void)issued;791dmu_prefetch_arg_t *dpa = arg;792793ASSERT0(level);794795mutex_enter(&dpa->dpa_lock);796ASSERT3U(dpa->dpa_pending_io, >, 0);797if (--dpa->dpa_pending_io == 0)798cv_broadcast(&dpa->dpa_cv);799mutex_exit(&dpa->dpa_lock);800}801802static void803dmu_prefetch_wait_by_dnode(dnode_t *dn, uint64_t offset, uint64_t len)804{805dmu_prefetch_arg_t dpa;806807mutex_init(&dpa.dpa_lock, NULL, MUTEX_DEFAULT, NULL);808cv_init(&dpa.dpa_cv, NULL, CV_DEFAULT, NULL);809810rw_enter(&dn->dn_struct_rwlock, RW_READER);811812uint64_t start = dbuf_whichblock(dn, 0, offset);813uint64_t end = dbuf_whichblock(dn, 0, offset + len - 1) + 1;814dpa.dpa_pending_io = end - start;815816for (uint64_t blk = start; blk < end; blk++) {817(void) dbuf_prefetch_impl(dn, 0, blk, ZIO_PRIORITY_ASYNC_READ,8180, dmu_prefetch_done, &dpa);819}820821rw_exit(&dn->dn_struct_rwlock);822823/* wait for prefetch L0 reads to finish */824mutex_enter(&dpa.dpa_lock);825while (dpa.dpa_pending_io > 0) {826cv_wait(&dpa.dpa_cv, &dpa.dpa_lock);827828}829mutex_exit(&dpa.dpa_lock);830831mutex_destroy(&dpa.dpa_lock);832cv_destroy(&dpa.dpa_cv);833}834835/*836* Issue prefetch I/Os for the given L0 block range and wait for the I/O837* to complete. This does not enforce dmu_prefetch_max and will prefetch838* the entire range. The blocks are read from disk into the ARC but no839* decompression occurs (i.e., the dbuf cache is not required).840*/841int842dmu_prefetch_wait(objset_t *os, uint64_t object, uint64_t offset, uint64_t size)843{844dnode_t *dn;845int err = 0;846847err = dnode_hold(os, object, FTAG, &dn);848if (err != 0)849return (err);850851/*852* Chunk the requests (16 indirects worth) so that we can be interrupted853*/854uint64_t chunksize;855if (dn->dn_indblkshift) {856uint64_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);857chunksize = (nbps * 16) << dn->dn_datablkshift;858} else {859chunksize = dn->dn_datablksz;860}861862while (size > 0) {863uint64_t mylen = MIN(size, chunksize);864865dmu_prefetch_wait_by_dnode(dn, offset, mylen);866867offset += mylen;868size -= mylen;869870if (issig()) {871err = SET_ERROR(EINTR);872break;873}874}875876dnode_rele(dn, FTAG);877878return (err);879}880881/*882* Issue prefetch I/Os for the given object's dnode.883*/884void885dmu_prefetch_dnode(objset_t *os, uint64_t object, zio_priority_t pri)886{887if (object == 0 || object >= DN_MAX_OBJECT)888return;889890dnode_t *dn = DMU_META_DNODE(os);891rw_enter(&dn->dn_struct_rwlock, RW_READER);892uint64_t blkid = dbuf_whichblock(dn, 0, object * sizeof (dnode_phys_t));893dbuf_prefetch(dn, 0, blkid, pri, 0);894rw_exit(&dn->dn_struct_rwlock);895}896897/*898* Get the next "chunk" of file data to free. We traverse the file from899* the end so that the file gets shorter over time (if we crash in the900* middle, this will leave us in a better state). We find allocated file901* data by simply searching the allocated level 1 indirects.902*903* On input, *start should be the first offset that does not need to be904* freed (e.g. "offset + length"). On return, *start will be the first905* offset that should be freed and l1blks is set to the number of level 1906* indirect blocks found within the chunk.907*/908static int909get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum, uint64_t *l1blks)910{911uint64_t blks;912uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);913/* bytes of data covered by a level-1 indirect block */914uint64_t iblkrange = (uint64_t)dn->dn_datablksz *915EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);916917ASSERT3U(minimum, <=, *start);918919/* dn_nlevels == 1 means we don't have any L1 blocks */920if (dn->dn_nlevels <= 1) {921*l1blks = 0;922*start = minimum;923return (0);924}925926/*927* Check if we can free the entire range assuming that all of the928* L1 blocks in this range have data. If we can, we use this929* worst case value as an estimate so we can avoid having to look930* at the object's actual data.931*/932uint64_t total_l1blks =933(roundup(*start, iblkrange) - (minimum / iblkrange * iblkrange)) /934iblkrange;935if (total_l1blks <= maxblks) {936*l1blks = total_l1blks;937*start = minimum;938return (0);939}940ASSERT(ISP2(iblkrange));941942for (blks = 0; *start > minimum && blks < maxblks; blks++) {943int err;944945/*946* dnode_next_offset(BACKWARDS) will find an allocated L1947* indirect block at or before the input offset. We must948* decrement *start so that it is at the end of the region949* to search.950*/951(*start)--;952953err = dnode_next_offset(dn,954DNODE_FIND_BACKWARDS, start, 2, 1, 0);955956/* if there are no indirect blocks before start, we are done */957if (err == ESRCH) {958*start = minimum;959break;960} else if (err != 0) {961*l1blks = blks;962return (err);963}964965/* set start to the beginning of this L1 indirect */966*start = P2ALIGN_TYPED(*start, iblkrange, uint64_t);967}968if (*start < minimum)969*start = minimum;970*l1blks = blks;971972return (0);973}974975/*976* If this objset is of type OST_ZFS return true if vfs's unmounted flag is set,977* otherwise return false.978* Used below in dmu_free_long_range_impl() to enable abort when unmounting979*/980static boolean_t981dmu_objset_zfs_unmounting(objset_t *os)982{983#ifdef _KERNEL984if (dmu_objset_type(os) == DMU_OST_ZFS)985return (zfs_get_vfs_flag_unmounted(os));986#else987(void) os;988#endif989return (B_FALSE);990}991992static int993dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,994uint64_t length)995{996uint64_t object_size;997int err;998uint64_t dirty_frees_threshold;999dsl_pool_t *dp = dmu_objset_pool(os);10001001if (dn == NULL)1002return (SET_ERROR(EINVAL));10031004object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;1005if (offset >= object_size)1006return (0);10071008if (zfs_per_txg_dirty_frees_percent <= 100)1009dirty_frees_threshold =1010zfs_per_txg_dirty_frees_percent * zfs_dirty_data_max / 100;1011else1012dirty_frees_threshold = zfs_dirty_data_max / 20;10131014if (length == DMU_OBJECT_END || offset + length > object_size)1015length = object_size - offset;10161017while (length != 0) {1018uint64_t chunk_end, chunk_begin, chunk_len;1019uint64_t l1blks;1020dmu_tx_t *tx;10211022if (dmu_objset_zfs_unmounting(dn->dn_objset))1023return (SET_ERROR(EINTR));10241025chunk_end = chunk_begin = offset + length;10261027/* move chunk_begin backwards to the beginning of this chunk */1028err = get_next_chunk(dn, &chunk_begin, offset, &l1blks);1029if (err)1030return (err);1031ASSERT3U(chunk_begin, >=, offset);1032ASSERT3U(chunk_begin, <=, chunk_end);10331034chunk_len = chunk_end - chunk_begin;10351036tx = dmu_tx_create(os);1037dmu_tx_hold_free(tx, dn->dn_object, chunk_begin, chunk_len);10381039/*1040* Mark this transaction as typically resulting in a net1041* reduction in space used.1042*/1043dmu_tx_mark_netfree(tx);1044err = dmu_tx_assign(tx, DMU_TX_WAIT);1045if (err) {1046dmu_tx_abort(tx);1047return (err);1048}10491050uint64_t txg = dmu_tx_get_txg(tx);10511052mutex_enter(&dp->dp_lock);1053uint64_t long_free_dirty =1054dp->dp_long_free_dirty_pertxg[txg & TXG_MASK];1055mutex_exit(&dp->dp_lock);10561057/*1058* To avoid filling up a TXG with just frees, wait for1059* the next TXG to open before freeing more chunks if1060* we have reached the threshold of frees.1061*/1062if (dirty_frees_threshold != 0 &&1063long_free_dirty >= dirty_frees_threshold) {1064DMU_TX_STAT_BUMP(dmu_tx_dirty_frees_delay);1065dmu_tx_commit(tx);1066txg_wait_open(dp, 0, B_TRUE);1067continue;1068}10691070/*1071* In order to prevent unnecessary write throttling, for each1072* TXG, we track the cumulative size of L1 blocks being dirtied1073* in dnode_free_range() below. We compare this number to a1074* tunable threshold, past which we prevent new L1 dirty freeing1075* blocks from being added into the open TXG. See1076* dmu_free_long_range_impl() for details. The threshold1077* prevents write throttle activation due to dirty freeing L11078* blocks taking up a large percentage of zfs_dirty_data_max.1079*/1080mutex_enter(&dp->dp_lock);1081dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] +=1082l1blks << dn->dn_indblkshift;1083mutex_exit(&dp->dp_lock);1084DTRACE_PROBE3(free__long__range,1085uint64_t, long_free_dirty, uint64_t, chunk_len,1086uint64_t, txg);1087dnode_free_range(dn, chunk_begin, chunk_len, tx);10881089dmu_tx_commit(tx);10901091length -= chunk_len;1092}1093return (0);1094}10951096int1097dmu_free_long_range(objset_t *os, uint64_t object,1098uint64_t offset, uint64_t length)1099{1100dnode_t *dn;1101int err;11021103err = dnode_hold(os, object, FTAG, &dn);1104if (err != 0)1105return (err);1106err = dmu_free_long_range_impl(os, dn, offset, length);11071108/*1109* It is important to zero out the maxblkid when freeing the entire1110* file, so that (a) subsequent calls to dmu_free_long_range_impl()1111* will take the fast path, and (b) dnode_reallocate() can verify1112* that the entire file has been freed.1113*/1114if (err == 0 && offset == 0 && length == DMU_OBJECT_END)1115dn->dn_maxblkid = 0;11161117dnode_rele(dn, FTAG);1118return (err);1119}11201121int1122dmu_free_long_object(objset_t *os, uint64_t object)1123{1124dmu_tx_t *tx;1125int err;11261127err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);1128if (err != 0)1129return (err);11301131tx = dmu_tx_create(os);1132dmu_tx_hold_bonus(tx, object);1133dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);1134dmu_tx_mark_netfree(tx);1135err = dmu_tx_assign(tx, DMU_TX_WAIT);1136if (err == 0) {1137err = dmu_object_free(os, object, tx);1138dmu_tx_commit(tx);1139} else {1140dmu_tx_abort(tx);1141}11421143return (err);1144}11451146int1147dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,1148uint64_t size, dmu_tx_t *tx)1149{1150dnode_t *dn;1151int err = dnode_hold(os, object, FTAG, &dn);1152if (err)1153return (err);1154ASSERT(offset < UINT64_MAX);1155ASSERT(size == DMU_OBJECT_END || size <= UINT64_MAX - offset);1156dnode_free_range(dn, offset, size, tx);1157dnode_rele(dn, FTAG);1158return (0);1159}11601161static int1162dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size,1163void *buf, dmu_flags_t flags)1164{1165dmu_buf_t **dbp;1166int numbufs, err = 0;11671168/*1169* Deal with odd block sizes, where there can't be data past the first1170* block. If we ever do the tail block optimization, we will need to1171* handle that here as well.1172*/1173if (dn->dn_maxblkid == 0) {1174uint64_t newsz = offset > dn->dn_datablksz ? 0 :1175MIN(size, dn->dn_datablksz - offset);1176memset((char *)buf + newsz, 0, size - newsz);1177size = newsz;1178}11791180if (size == 0)1181return (0);11821183/* Allow Direct I/O when requested and properly aligned */1184if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned(buf) &&1185zfs_dio_aligned(offset, size, PAGESIZE)) {1186abd_t *data = abd_get_from_buf(buf, size);1187err = dmu_read_abd(dn, offset, size, data, flags);1188abd_free(data);1189return (err);1190}1191flags &= ~DMU_DIRECTIO;11921193while (size > 0) {1194uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);1195int i;11961197/*1198* NB: we could do this block-at-a-time, but it's nice1199* to be reading in parallel.1200*/1201err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,1202TRUE, FTAG, &numbufs, &dbp, flags);1203if (err)1204break;12051206for (i = 0; i < numbufs; i++) {1207uint64_t tocpy;1208int64_t bufoff;1209dmu_buf_t *db = dbp[i];12101211ASSERT(size > 0);12121213bufoff = offset - db->db_offset;1214tocpy = MIN(db->db_size - bufoff, size);12151216ASSERT(db->db_data != NULL);1217(void) memcpy(buf, (char *)db->db_data + bufoff, tocpy);12181219offset += tocpy;1220size -= tocpy;1221buf = (char *)buf + tocpy;1222}1223dmu_buf_rele_array(dbp, numbufs, FTAG);1224}1225return (err);1226}12271228int1229dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1230void *buf, dmu_flags_t flags)1231{1232dnode_t *dn;1233int err;12341235err = dnode_hold(os, object, FTAG, &dn);1236if (err != 0)1237return (err);12381239err = dmu_read_impl(dn, offset, size, buf, flags);1240dnode_rele(dn, FTAG);1241return (err);1242}12431244int1245dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf,1246dmu_flags_t flags)1247{1248return (dmu_read_impl(dn, offset, size, buf, flags));1249}12501251static void1252dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size,1253const void *buf, dmu_tx_t *tx, dmu_flags_t flags)1254{1255int i;12561257for (i = 0; i < numbufs; i++) {1258uint64_t tocpy;1259int64_t bufoff;1260dmu_buf_t *db = dbp[i];12611262ASSERT(size > 0);12631264bufoff = offset - db->db_offset;1265tocpy = MIN(db->db_size - bufoff, size);12661267ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);12681269if (tocpy == db->db_size) {1270dmu_buf_will_fill_flags(db, tx, B_FALSE, flags);1271} else {1272if (i == numbufs - 1 && bufoff + tocpy < db->db_size) {1273if (bufoff == 0)1274flags |= DMU_PARTIAL_FIRST;1275else1276flags |= DMU_PARTIAL_MORE;1277}1278dmu_buf_will_dirty_flags(db, tx, flags);1279}12801281ASSERT(db->db_data != NULL);1282(void) memcpy((char *)db->db_data + bufoff, buf, tocpy);12831284if (tocpy == db->db_size)1285dmu_buf_fill_done(db, tx, B_FALSE);12861287offset += tocpy;1288size -= tocpy;1289buf = (char *)buf + tocpy;1290}1291}12921293void1294dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1295const void *buf, dmu_tx_t *tx)1296{1297dmu_buf_t **dbp;1298int numbufs;12991300if (size == 0)1301return;13021303VERIFY0(dmu_buf_hold_array(os, object, offset, size,1304FALSE, FTAG, &numbufs, &dbp));1305dmu_write_impl(dbp, numbufs, offset, size, buf, tx, DMU_READ_PREFETCH);1306dmu_buf_rele_array(dbp, numbufs, FTAG);1307}13081309int1310dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size,1311const void *buf, dmu_tx_t *tx, dmu_flags_t flags)1312{1313dmu_buf_t **dbp;1314int numbufs;1315int error;13161317if (size == 0)1318return (0);13191320/* Allow Direct I/O when requested and properly aligned */1321if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned((void *)buf) &&1322zfs_dio_aligned(offset, size, dn->dn_datablksz)) {1323abd_t *data = abd_get_from_buf((void *)buf, size);1324error = dmu_write_abd(dn, offset, size, data, flags, tx);1325abd_free(data);1326return (error);1327}1328flags &= ~DMU_DIRECTIO;13291330VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size,1331FALSE, FTAG, &numbufs, &dbp, flags));1332dmu_write_impl(dbp, numbufs, offset, size, buf, tx, flags);1333dmu_buf_rele_array(dbp, numbufs, FTAG);1334return (0);1335}13361337void1338dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1339dmu_tx_t *tx)1340{1341dmu_buf_t **dbp;1342int numbufs, i;13431344if (size == 0)1345return;13461347VERIFY0(dmu_buf_hold_array(os, object, offset, size,1348FALSE, FTAG, &numbufs, &dbp));13491350for (i = 0; i < numbufs; i++) {1351dmu_buf_t *db = dbp[i];13521353dmu_buf_will_not_fill(db, tx);1354}1355dmu_buf_rele_array(dbp, numbufs, FTAG);1356}13571358void1359dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,1360void *data, uint8_t etype, uint8_t comp, int uncompressed_size,1361int compressed_size, int byteorder, dmu_tx_t *tx)1362{1363dmu_buf_t *db;13641365ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);1366ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);1367VERIFY0(dmu_buf_hold_noread(os, object, offset,1368FTAG, &db));13691370dmu_buf_write_embedded(db,1371data, (bp_embedded_type_t)etype, (enum zio_compress)comp,1372uncompressed_size, compressed_size, byteorder, tx);13731374dmu_buf_rele(db, FTAG);1375}13761377void1378dmu_redact(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1379dmu_tx_t *tx)1380{1381int numbufs, i;1382dmu_buf_t **dbp;13831384VERIFY0(dmu_buf_hold_array(os, object, offset, size, FALSE, FTAG,1385&numbufs, &dbp));1386for (i = 0; i < numbufs; i++)1387dmu_buf_redact(dbp[i], tx);1388dmu_buf_rele_array(dbp, numbufs, FTAG);1389}13901391#ifdef _KERNEL1392int1393dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size,1394dmu_flags_t flags)1395{1396dmu_buf_t **dbp;1397int numbufs, i, err;13981399if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT))1400return (dmu_read_uio_direct(dn, uio, size, flags));1401flags &= ~DMU_DIRECTIO;14021403/*1404* NB: we could do this block-at-a-time, but it's nice1405* to be reading in parallel.1406*/1407err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size,1408TRUE, FTAG, &numbufs, &dbp, flags);1409if (err)1410return (err);14111412for (i = 0; i < numbufs; i++) {1413uint64_t tocpy;1414int64_t bufoff;1415dmu_buf_t *db = dbp[i];14161417ASSERT(size > 0);14181419bufoff = zfs_uio_offset(uio) - db->db_offset;1420tocpy = MIN(db->db_size - bufoff, size);14211422ASSERT(db->db_data != NULL);1423err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy,1424UIO_READ, uio);14251426if (err)1427break;14281429size -= tocpy;1430}1431dmu_buf_rele_array(dbp, numbufs, FTAG);14321433return (err);1434}14351436/*1437* Read 'size' bytes into the uio buffer.1438* From object zdb->db_object.1439* Starting at zfs_uio_offset(uio).1440*1441* If the caller already has a dbuf in the target object1442* (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),1443* because we don't have to find the dnode_t for the object.1444*/1445int1446dmu_read_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,1447dmu_flags_t flags)1448{1449dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;1450int err;14511452if (size == 0)1453return (0);14541455DB_DNODE_ENTER(db);1456err = dmu_read_uio_dnode(DB_DNODE(db), uio, size, flags);1457DB_DNODE_EXIT(db);14581459return (err);1460}14611462/*1463* Read 'size' bytes into the uio buffer.1464* From the specified object1465* Starting at offset zfs_uio_offset(uio).1466*/1467int1468dmu_read_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,1469dmu_flags_t flags)1470{1471dnode_t *dn;1472int err;14731474if (size == 0)1475return (0);14761477err = dnode_hold(os, object, FTAG, &dn);1478if (err)1479return (err);14801481err = dmu_read_uio_dnode(dn, uio, size, flags);14821483dnode_rele(dn, FTAG);14841485return (err);1486}14871488int1489dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx,1490dmu_flags_t flags)1491{1492dmu_buf_t **dbp;1493int numbufs;1494int err = 0;1495uint64_t write_size;1496dmu_flags_t oflags = flags;14971498top:1499write_size = size;15001501/*1502* We only allow Direct I/O writes to happen if we are block1503* sized aligned. Otherwise, we pass the write off to the ARC.1504*/1505if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) &&1506(write_size >= dn->dn_datablksz)) {1507if (zfs_dio_aligned(zfs_uio_offset(uio), write_size,1508dn->dn_datablksz)) {1509return (dmu_write_uio_direct(dn, uio, size, flags, tx));1510} else if (write_size > dn->dn_datablksz &&1511zfs_dio_offset_aligned(zfs_uio_offset(uio),1512dn->dn_datablksz)) {1513write_size =1514dn->dn_datablksz * (write_size / dn->dn_datablksz);1515err = dmu_write_uio_direct(dn, uio, write_size, flags,1516tx);1517if (err == 0) {1518size -= write_size;1519goto top;1520} else {1521return (err);1522}1523} else {1524write_size =1525P2PHASE(zfs_uio_offset(uio), dn->dn_datablksz);1526}1527}1528flags &= ~DMU_DIRECTIO;15291530err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), write_size,1531FALSE, FTAG, &numbufs, &dbp, flags);1532if (err)1533return (err);15341535for (int i = 0; i < numbufs; i++) {1536uint64_t tocpy;1537int64_t bufoff;1538dmu_buf_t *db = dbp[i];15391540ASSERT(write_size > 0);15411542offset_t off = zfs_uio_offset(uio);1543bufoff = off - db->db_offset;1544tocpy = MIN(db->db_size - bufoff, write_size);15451546ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);15471548if (tocpy == db->db_size) {1549dmu_buf_will_fill_flags(db, tx, B_TRUE, flags);1550} else {1551if (i == numbufs - 1 && bufoff + tocpy < db->db_size) {1552if (bufoff == 0)1553flags |= DMU_PARTIAL_FIRST;1554else1555flags |= DMU_PARTIAL_MORE;1556}1557dmu_buf_will_dirty_flags(db, tx, flags);1558}15591560ASSERT(db->db_data != NULL);1561err = zfs_uio_fault_move((char *)db->db_data + bufoff,1562tocpy, UIO_WRITE, uio);15631564if (tocpy == db->db_size && dmu_buf_fill_done(db, tx, err)) {1565/* The fill was reverted. Undo any uio progress. */1566zfs_uio_advance(uio, off - zfs_uio_offset(uio));1567}15681569if (err)1570break;15711572write_size -= tocpy;1573size -= tocpy;1574}15751576IMPLY(err == 0, write_size == 0);15771578dmu_buf_rele_array(dbp, numbufs, FTAG);15791580if ((oflags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) &&1581err == 0 && size > 0) {1582flags = oflags;1583goto top;1584}1585IMPLY(err == 0, size == 0);15861587return (err);1588}15891590/*1591* Write 'size' bytes from the uio buffer.1592* To object zdb->db_object.1593* Starting at offset zfs_uio_offset(uio).1594*1595* If the caller already has a dbuf in the target object1596* (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),1597* because we don't have to find the dnode_t for the object.1598*/1599int1600dmu_write_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,1601dmu_tx_t *tx, dmu_flags_t flags)1602{1603dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;1604int err;16051606if (size == 0)1607return (0);16081609DB_DNODE_ENTER(db);1610err = dmu_write_uio_dnode(DB_DNODE(db), uio, size, tx, flags);1611DB_DNODE_EXIT(db);16121613return (err);1614}16151616/*1617* Write 'size' bytes from the uio buffer.1618* To the specified object.1619* Starting at offset zfs_uio_offset(uio).1620*/1621int1622dmu_write_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,1623dmu_tx_t *tx, dmu_flags_t flags)1624{1625dnode_t *dn;1626int err;16271628if (size == 0)1629return (0);16301631err = dnode_hold(os, object, FTAG, &dn);1632if (err)1633return (err);16341635err = dmu_write_uio_dnode(dn, uio, size, tx, flags);16361637dnode_rele(dn, FTAG);16381639return (err);1640}1641#endif /* _KERNEL */16421643static void1644dmu_cached_bps(spa_t *spa, blkptr_t *bps, uint_t nbps,1645uint64_t *l1sz, uint64_t *l2sz)1646{1647int cached_flags;16481649if (bps == NULL)1650return;16511652for (size_t blk_off = 0; blk_off < nbps; blk_off++) {1653blkptr_t *bp = &bps[blk_off];16541655if (BP_IS_HOLE(bp))1656continue;16571658cached_flags = arc_cached(spa, bp);1659if (cached_flags == 0)1660continue;16611662if ((cached_flags & (ARC_CACHED_IN_L1 | ARC_CACHED_IN_L2)) ==1663ARC_CACHED_IN_L2)1664*l2sz += BP_GET_LSIZE(bp);1665else1666*l1sz += BP_GET_LSIZE(bp);1667}1668}16691670/*1671* Estimate DMU object cached size.1672*/1673int1674dmu_object_cached_size(objset_t *os, uint64_t object,1675uint64_t *l1sz, uint64_t *l2sz)1676{1677dnode_t *dn;1678dmu_object_info_t doi;1679int err = 0;16801681*l1sz = *l2sz = 0;16821683if (dnode_hold(os, object, FTAG, &dn) != 0)1684return (0);16851686if (dn->dn_nlevels < 2) {1687dnode_rele(dn, FTAG);1688return (0);1689}16901691dmu_object_info_from_dnode(dn, &doi);16921693for (uint64_t off = 0; off < doi.doi_max_offset &&1694dmu_prefetch_max > 0; off += dmu_prefetch_max) {1695/* dbuf_read doesn't prefetch L1 blocks. */1696dmu_prefetch_by_dnode(dn, 1, off,1697dmu_prefetch_max, ZIO_PRIORITY_SYNC_READ);1698}16991700/*1701* Hold all valid L1 blocks, asking ARC the status of each BP1702* contained in each such L1 block.1703*/1704uint_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);1705uint64_t l1blks = 1 + (dn->dn_maxblkid / nbps);17061707rw_enter(&dn->dn_struct_rwlock, RW_READER);1708for (uint64_t blk = 0; blk < l1blks; blk++) {1709dmu_buf_impl_t *db = NULL;17101711if (issig()) {1712/*1713* On interrupt, get out, and bubble up EINTR1714*/1715err = EINTR;1716break;1717}17181719/*1720* If we get an i/o error here, the L1 can't be read,1721* and nothing under it could be cached, so we just1722* continue. Ignoring the error from dbuf_hold_impl1723* or from dbuf_read is then a reasonable choice.1724*/1725err = dbuf_hold_impl(dn, 1, blk, B_TRUE, B_FALSE, FTAG, &db);1726if (err != 0) {1727/*1728* ignore error and continue1729*/1730err = 0;1731continue;1732}17331734err = dbuf_read(db, NULL, DB_RF_CANFAIL);1735if (err == 0) {1736dmu_cached_bps(dmu_objset_spa(os), db->db.db_data,1737nbps, l1sz, l2sz);1738}1739/*1740* error may be ignored, and we continue1741*/1742err = 0;1743dbuf_rele(db, FTAG);1744}1745rw_exit(&dn->dn_struct_rwlock);17461747dnode_rele(dn, FTAG);1748return (err);1749}17501751/*1752* Allocate a loaned anonymous arc buffer.1753*/1754arc_buf_t *1755dmu_request_arcbuf(dmu_buf_t *handle, int size)1756{1757dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;17581759return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size));1760}17611762/*1763* Free a loaned arc buffer.1764*/1765void1766dmu_return_arcbuf(arc_buf_t *buf)1767{1768arc_return_buf(buf, FTAG);1769arc_buf_destroy(buf, FTAG);1770}17711772/*1773* A "lightweight" write is faster than a regular write (e.g.1774* dmu_write_by_dnode() or dmu_assign_arcbuf_by_dnode()), because it avoids the1775* CPU cost of creating a dmu_buf_impl_t and arc_buf_[hdr_]_t. However, the1776* data can not be read or overwritten until the transaction's txg has been1777* synced. This makes it appropriate for workloads that are known to be1778* (temporarily) write-only, like "zfs receive".1779*1780* A single block is written, starting at the specified offset in bytes. If1781* the call is successful, it returns 0 and the provided abd has been1782* consumed (the caller should not free it).1783*/1784int1785dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd,1786const zio_prop_t *zp, zio_flag_t flags, dmu_tx_t *tx)1787{1788dbuf_dirty_record_t *dr =1789dbuf_dirty_lightweight(dn, dbuf_whichblock(dn, 0, offset), tx);1790if (dr == NULL)1791return (SET_ERROR(EIO));1792dr->dt.dll.dr_abd = abd;1793dr->dt.dll.dr_props = *zp;1794dr->dt.dll.dr_flags = flags;1795return (0);1796}17971798/*1799* When possible directly assign passed loaned arc buffer to a dbuf.1800* If this is not possible copy the contents of passed arc buf via1801* dmu_write().1802*/1803int1804dmu_assign_arcbuf_by_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf,1805dmu_tx_t *tx, dmu_flags_t flags)1806{1807dmu_buf_impl_t *db;1808objset_t *os = dn->dn_objset;1809uint32_t blksz = (uint32_t)arc_buf_lsize(buf);1810uint64_t blkid;18111812rw_enter(&dn->dn_struct_rwlock, RW_READER);1813blkid = dbuf_whichblock(dn, 0, offset);1814db = dbuf_hold(dn, blkid, FTAG);1815rw_exit(&dn->dn_struct_rwlock);1816if (db == NULL)1817return (SET_ERROR(EIO));18181819/*1820* We can only assign if the offset is aligned and the arc buf is the1821* same size as the dbuf.1822*/1823if (offset == db->db.db_offset && blksz == db->db.db_size) {1824zfs_racct_write(os->os_spa, blksz, 1, flags);1825dbuf_assign_arcbuf(db, buf, tx, flags);1826dbuf_rele(db, FTAG);1827} else {1828/* compressed bufs must always be assignable to their dbuf */1829ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF);1830ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED));18311832dbuf_rele(db, FTAG);1833dmu_write_by_dnode(dn, offset, blksz, buf->b_data, tx, flags);1834dmu_return_arcbuf(buf);1835}18361837return (0);1838}18391840int1841dmu_assign_arcbuf_by_dbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,1842dmu_tx_t *tx, dmu_flags_t flags)1843{1844int err;1845dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;18461847DB_DNODE_ENTER(db);1848err = dmu_assign_arcbuf_by_dnode(DB_DNODE(db), offset, buf, tx, flags);1849DB_DNODE_EXIT(db);18501851return (err);1852}18531854void1855dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)1856{1857(void) buf;1858dmu_sync_arg_t *dsa = varg;18591860if (zio->io_error == 0) {1861dbuf_dirty_record_t *dr = dsa->dsa_dr;1862blkptr_t *bp = zio->io_bp;18631864if (BP_IS_HOLE(bp)) {1865dmu_buf_t *db = NULL;1866if (dr)1867db = &(dr->dr_dbuf->db);1868else1869db = dsa->dsa_zgd->zgd_db;1870/*1871* A block of zeros may compress to a hole, but the1872* block size still needs to be known for replay.1873*/1874BP_SET_LSIZE(bp, db->db_size);1875} else if (!BP_IS_EMBEDDED(bp)) {1876ASSERT0(BP_GET_LEVEL(bp));1877BP_SET_FILL(bp, 1);1878}1879}1880}18811882static void1883dmu_sync_late_arrival_ready(zio_t *zio)1884{1885dmu_sync_ready(zio, NULL, zio->io_private);1886}18871888void1889dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)1890{1891(void) buf;1892dmu_sync_arg_t *dsa = varg;1893dbuf_dirty_record_t *dr = dsa->dsa_dr;1894dmu_buf_impl_t *db = dr->dr_dbuf;1895zgd_t *zgd = dsa->dsa_zgd;18961897/*1898* Record the vdev(s) backing this blkptr so they can be flushed after1899* the writes for the lwb have completed.1900*/1901if (zgd && zio->io_error == 0) {1902zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);1903}19041905mutex_enter(&db->db_mtx);1906ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);1907if (zio->io_error == 0) {1908ASSERT0(dr->dt.dl.dr_has_raw_params);1909dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);1910if (dr->dt.dl.dr_nopwrite) {1911blkptr_t *bp = zio->io_bp;1912blkptr_t *bp_orig = &zio->io_bp_orig;1913uint8_t chksum = BP_GET_CHECKSUM(bp_orig);19141915ASSERT(BP_EQUAL(bp, bp_orig));1916VERIFY(BP_EQUAL(bp, db->db_blkptr));1917ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);1918VERIFY(zio_checksum_table[chksum].ci_flags &1919ZCHECKSUM_FLAG_NOPWRITE);1920}1921dr->dt.dl.dr_overridden_by = *zio->io_bp;1922dr->dt.dl.dr_override_state = DR_OVERRIDDEN;1923dr->dt.dl.dr_copies = zio->io_prop.zp_copies;1924dr->dt.dl.dr_gang_copies = zio->io_prop.zp_gang_copies;19251926/*1927* Old style holes are filled with all zeros, whereas1928* new-style holes maintain their lsize, type, level,1929* and birth time (see zio_write_compress). While we1930* need to reset the BP_SET_LSIZE() call that happened1931* in dmu_sync_ready for old style holes, we do *not*1932* want to wipe out the information contained in new1933* style holes. Thus, only zero out the block pointer if1934* it's an old style hole.1935*/1936if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&1937BP_GET_LOGICAL_BIRTH(&dr->dt.dl.dr_overridden_by) == 0)1938BP_ZERO(&dr->dt.dl.dr_overridden_by);1939} else {1940dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;1941}19421943cv_broadcast(&db->db_changed);1944mutex_exit(&db->db_mtx);19451946if (dsa->dsa_done)1947dsa->dsa_done(dsa->dsa_zgd, zio->io_error);19481949kmem_free(dsa, sizeof (*dsa));1950}19511952static void1953dmu_sync_late_arrival_done(zio_t *zio)1954{1955blkptr_t *bp = zio->io_bp;1956dmu_sync_arg_t *dsa = zio->io_private;1957zgd_t *zgd = dsa->dsa_zgd;19581959if (zio->io_error == 0) {1960/*1961* Record the vdev(s) backing this blkptr so they can be1962* flushed after the writes for the lwb have completed.1963*/1964zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);19651966if (!BP_IS_HOLE(bp)) {1967blkptr_t *bp_orig __maybe_unused = &zio->io_bp_orig;1968ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE));1969ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));1970ASSERT(BP_GET_BIRTH(zio->io_bp) == zio->io_txg);1971ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));1972zio_free(zio->io_spa, zio->io_txg, zio->io_bp);1973}1974}19751976dmu_tx_commit(dsa->dsa_tx);19771978dsa->dsa_done(dsa->dsa_zgd, zio->io_error);19791980abd_free(zio->io_abd);1981kmem_free(dsa, sizeof (*dsa));1982}19831984static int1985dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,1986zio_prop_t *zp, zbookmark_phys_t *zb)1987{1988dmu_sync_arg_t *dsa;1989dmu_tx_t *tx;1990int error;19911992error = dbuf_read((dmu_buf_impl_t *)zgd->zgd_db, NULL,1993DB_RF_CANFAIL | DMU_READ_NO_PREFETCH | DMU_KEEP_CACHING);1994if (error != 0)1995return (error);19961997tx = dmu_tx_create(os);1998dmu_tx_hold_space(tx, zgd->zgd_db->db_size);1999/*2000* This transaction does not produce any dirty data or log blocks, so2001* it should not be throttled. All other cases wait for TXG sync, by2002* which time the log block we are writing will be obsolete, so we can2003* skip waiting and just return error here instead.2004*/2005if (dmu_tx_assign(tx, DMU_TX_NOWAIT | DMU_TX_NOTHROTTLE) != 0) {2006dmu_tx_abort(tx);2007/* Make zl_get_data do txg_waited_synced() */2008return (SET_ERROR(EIO));2009}20102011/*2012* In order to prevent the zgd's lwb from being free'd prior to2013* dmu_sync_late_arrival_done() being called, we have to ensure2014* the lwb's "max txg" takes this tx's txg into account.2015*/2016zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx));20172018dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);2019dsa->dsa_dr = NULL;2020dsa->dsa_done = done;2021dsa->dsa_zgd = zgd;2022dsa->dsa_tx = tx;20232024/*2025* Since we are currently syncing this txg, it's nontrivial to2026* determine what BP to nopwrite against, so we disable nopwrite.2027*2028* When syncing, the db_blkptr is initially the BP of the previous2029* txg. We can not nopwrite against it because it will be changed2030* (this is similar to the non-late-arrival case where the dbuf is2031* dirty in a future txg).2032*2033* Then dbuf_write_ready() sets bp_blkptr to the location we will write.2034* We can not nopwrite against it because although the BP will not2035* (typically) be changed, the data has not yet been persisted to this2036* location.2037*2038* Finally, when dbuf_write_done() is called, it is theoretically2039* possible to always nopwrite, because the data that was written in2040* this txg is the same data that we are trying to write. However we2041* would need to check that this dbuf is not dirty in any future2042* txg's (as we do in the normal dmu_sync() path). For simplicity, we2043* don't nopwrite in this case.2044*/2045zp->zp_nopwrite = B_FALSE;20462047zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,2048abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size),2049zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp,2050dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done,2051dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));20522053return (0);2054}20552056/*2057* Intent log support: sync the block associated with db to disk.2058* N.B. and XXX: the caller is responsible for making sure that the2059* data isn't changing while dmu_sync() is writing it.2060*2061* Return values:2062*2063* EEXIST: this txg has already been synced, so there's nothing to do.2064* The caller should not log the write.2065*2066* ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.2067* The caller should not log the write.2068*2069* EALREADY: this block is already in the process of being synced.2070* The caller should track its progress (somehow).2071*2072* EIO: could not do the I/O.2073* The caller should do a txg_wait_synced().2074*2075* 0: the I/O has been initiated.2076* The caller should log this blkptr in the done callback.2077* It is possible that the I/O will fail, in which case2078* the error will be reported to the done callback and2079* propagated to pio from zio_done().2080*/2081int2082dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)2083{2084dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;2085objset_t *os = db->db_objset;2086dsl_dataset_t *ds = os->os_dsl_dataset;2087dbuf_dirty_record_t *dr, *dr_next;2088dmu_sync_arg_t *dsa;2089zbookmark_phys_t zb;2090zio_prop_t zp;20912092ASSERT(pio != NULL);2093ASSERT(txg != 0);20942095SET_BOOKMARK(&zb, ds->ds_object,2096db->db.db_object, db->db_level, db->db_blkid);20972098DB_DNODE_ENTER(db);2099dmu_write_policy(os, DB_DNODE(db), db->db_level, WP_DMU_SYNC, &zp);2100DB_DNODE_EXIT(db);21012102/*2103* If we're frozen (running ziltest), we always need to generate a bp.2104*/2105if (txg > spa_freeze_txg(os->os_spa))2106return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));21072108/*2109* Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()2110* and us. If we determine that this txg is not yet syncing,2111* but it begins to sync a moment later, that's OK because the2112* sync thread will block in dbuf_sync_leaf() until we drop db_mtx.2113*/2114mutex_enter(&db->db_mtx);21152116if (txg <= spa_last_synced_txg(os->os_spa)) {2117/*2118* This txg has already synced. There's nothing to do.2119*/2120mutex_exit(&db->db_mtx);2121return (SET_ERROR(EEXIST));2122}21232124if (txg <= spa_syncing_txg(os->os_spa)) {2125/*2126* This txg is currently syncing, so we can't mess with2127* the dirty record anymore; just write a new log block.2128*/2129mutex_exit(&db->db_mtx);2130return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));2131}21322133dr = dbuf_find_dirty_eq(db, txg);21342135if (dr == NULL) {2136/*2137* There's no dr for this dbuf, so it must have been freed.2138* There's no need to log writes to freed blocks, so we're done.2139*/2140mutex_exit(&db->db_mtx);2141return (SET_ERROR(ENOENT));2142}21432144dr_next = list_next(&db->db_dirty_records, dr);2145ASSERT(dr_next == NULL || dr_next->dr_txg < txg);21462147if (db->db_blkptr != NULL) {2148/*2149* We need to fill in zgd_bp with the current blkptr so that2150* the nopwrite code can check if we're writing the same2151* data that's already on disk. We can only nopwrite if we2152* are sure that after making the copy, db_blkptr will not2153* change until our i/o completes. We ensure this by2154* holding the db_mtx, and only allowing nopwrite if the2155* block is not already dirty (see below). This is verified2156* by dmu_sync_done(), which VERIFYs that the db_blkptr has2157* not changed.2158*/2159*zgd->zgd_bp = *db->db_blkptr;2160}21612162/*2163* Assume the on-disk data is X, the current syncing data (in2164* txg - 1) is Y, and the current in-memory data is Z (currently2165* in dmu_sync).2166*2167* We usually want to perform a nopwrite if X and Z are the2168* same. However, if Y is different (i.e. the BP is going to2169* change before this write takes effect), then a nopwrite will2170* be incorrect - we would override with X, which could have2171* been freed when Y was written.2172*2173* (Note that this is not a concern when we are nop-writing from2174* syncing context, because X and Y must be identical, because2175* all previous txgs have been synced.)2176*2177* Therefore, we disable nopwrite if the current BP could change2178* before this TXG. There are two ways it could change: by2179* being dirty (dr_next is non-NULL), or by being freed2180* (dnode_block_freed()). This behavior is verified by2181* zio_done(), which VERIFYs that the override BP is identical2182* to the on-disk BP.2183*/2184if (dr_next != NULL) {2185zp.zp_nopwrite = B_FALSE;2186} else {2187DB_DNODE_ENTER(db);2188if (dnode_block_freed(DB_DNODE(db), db->db_blkid))2189zp.zp_nopwrite = B_FALSE;2190DB_DNODE_EXIT(db);2191}21922193ASSERT(dr->dr_txg == txg);2194if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||2195dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {2196/*2197* We have already issued a sync write for this buffer,2198* or this buffer has already been synced. It could not2199* have been dirtied since, or we would have cleared the state.2200*/2201mutex_exit(&db->db_mtx);2202return (SET_ERROR(EALREADY));2203}22042205ASSERT0(dr->dt.dl.dr_has_raw_params);2206ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);2207dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;2208mutex_exit(&db->db_mtx);22092210dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);2211dsa->dsa_dr = dr;2212dsa->dsa_done = done;2213dsa->dsa_zgd = zgd;2214dsa->dsa_tx = NULL;22152216zio_nowait(arc_write(pio, os->os_spa, txg, zgd->zgd_bp,2217dr->dt.dl.dr_data, !DBUF_IS_CACHEABLE(db),2218dbuf_is_l2cacheable(db, NULL), &zp, dmu_sync_ready, NULL,2219dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL,2220&zb));22212222return (0);2223}22242225int2226dmu_object_set_nlevels(objset_t *os, uint64_t object, int nlevels, dmu_tx_t *tx)2227{2228dnode_t *dn;2229int err;22302231err = dnode_hold(os, object, FTAG, &dn);2232if (err)2233return (err);2234err = dnode_set_nlevels(dn, nlevels, tx);2235dnode_rele(dn, FTAG);2236return (err);2237}22382239int2240dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,2241dmu_tx_t *tx)2242{2243dnode_t *dn;2244int err;22452246err = dnode_hold(os, object, FTAG, &dn);2247if (err)2248return (err);2249err = dnode_set_blksz(dn, size, ibs, tx);2250dnode_rele(dn, FTAG);2251return (err);2252}22532254int2255dmu_object_set_maxblkid(objset_t *os, uint64_t object, uint64_t maxblkid,2256dmu_tx_t *tx)2257{2258dnode_t *dn;2259int err;22602261err = dnode_hold(os, object, FTAG, &dn);2262if (err)2263return (err);2264rw_enter(&dn->dn_struct_rwlock, RW_WRITER);2265dnode_new_blkid(dn, maxblkid, tx, B_FALSE, B_TRUE);2266rw_exit(&dn->dn_struct_rwlock);2267dnode_rele(dn, FTAG);2268return (0);2269}22702271void2272dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,2273dmu_tx_t *tx)2274{2275dnode_t *dn;22762277/*2278* Send streams include each object's checksum function. This2279* check ensures that the receiving system can understand the2280* checksum function transmitted.2281*/2282ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);22832284VERIFY0(dnode_hold(os, object, FTAG, &dn));2285ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);2286dn->dn_checksum = checksum;2287dnode_setdirty(dn, tx);2288dnode_rele(dn, FTAG);2289}22902291void2292dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,2293dmu_tx_t *tx)2294{2295dnode_t *dn;22962297/*2298* Send streams include each object's compression function. This2299* check ensures that the receiving system can understand the2300* compression function transmitted.2301*/2302ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);23032304VERIFY0(dnode_hold(os, object, FTAG, &dn));2305dn->dn_compress = compress;2306dnode_setdirty(dn, tx);2307dnode_rele(dn, FTAG);2308}23092310/*2311* When the "redundant_metadata" property is set to "most", only indirect2312* blocks of this level and higher will have an additional ditto block.2313*/2314static const int zfs_redundant_metadata_most_ditto_level = 2;23152316void2317dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)2318{2319dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;2320boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||2321(wp & WP_SPILL));2322enum zio_checksum checksum = os->os_checksum;2323enum zio_compress compress = os->os_compress;2324uint8_t complevel = os->os_complevel;2325enum zio_checksum dedup_checksum = os->os_dedup_checksum;2326boolean_t dedup = B_FALSE;2327boolean_t nopwrite = B_FALSE;2328boolean_t dedup_verify = os->os_dedup_verify;2329boolean_t encrypt = B_FALSE;2330int copies = os->os_copies;2331int gang_copies = os->os_copies;23322333/*2334* We maintain different write policies for each of the following2335* types of data:2336* 1. metadata2337* 2. preallocated blocks (i.e. level-0 blocks of a dump device)2338* 3. all other level 0 blocks2339*/2340if (ismd) {2341/*2342* XXX -- we should design a compression algorithm2343* that specializes in arrays of bps.2344*/2345compress = zio_compress_select(os->os_spa,2346ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);23472348/*2349* Metadata always gets checksummed. If the data2350* checksum is multi-bit correctable, and it's not a2351* ZBT-style checksum, then it's suitable for metadata2352* as well. Otherwise, the metadata checksum defaults2353* to fletcher4.2354*/2355if (!(zio_checksum_table[checksum].ci_flags &2356ZCHECKSUM_FLAG_METADATA) ||2357(zio_checksum_table[checksum].ci_flags &2358ZCHECKSUM_FLAG_EMBEDDED))2359checksum = ZIO_CHECKSUM_FLETCHER_4;23602361switch (os->os_redundant_metadata) {2362case ZFS_REDUNDANT_METADATA_ALL:2363copies++;2364gang_copies++;2365break;2366case ZFS_REDUNDANT_METADATA_MOST:2367if (level >= zfs_redundant_metadata_most_ditto_level ||2368DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))2369copies++;2370if (level + 1 >=2371zfs_redundant_metadata_most_ditto_level ||2372DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))2373gang_copies++;2374break;2375case ZFS_REDUNDANT_METADATA_SOME:2376if (DMU_OT_IS_CRITICAL(type, level)) {2377copies++;2378gang_copies++;2379} else if (DMU_OT_IS_METADATA(type)) {2380gang_copies++;2381}2382break;2383case ZFS_REDUNDANT_METADATA_NONE:2384break;2385}23862387if (dmu_ddt_copies > 0) {2388/*2389* If this tunable is set, and this is a write for a2390* dedup entry store (zap or log), then we treat it2391* something like ZFS_REDUNDANT_METADATA_MOST on a2392* regular dataset: this many copies, and one more for2393* "higher" indirect blocks. This specific exception is2394* necessary because dedup objects are stored in the2395* MOS, which always has the highest possible copies.2396*/2397dmu_object_type_t stype =2398dn ? dn->dn_storage_type : DMU_OT_NONE;2399if (stype == DMU_OT_NONE)2400stype = type;2401if (stype == DMU_OT_DDT_ZAP) {2402copies = dmu_ddt_copies;2403if (level >=2404zfs_redundant_metadata_most_ditto_level)2405copies++;2406}2407}2408} else if (wp & WP_NOFILL) {2409ASSERT0(level);24102411/*2412* If we're writing preallocated blocks, we aren't actually2413* writing them so don't set any policy properties. These2414* blocks are currently only used by an external subsystem2415* outside of zfs (i.e. dump) and not written by the zio2416* pipeline.2417*/2418compress = ZIO_COMPRESS_OFF;2419checksum = ZIO_CHECKSUM_OFF;2420} else {2421compress = zio_compress_select(os->os_spa, dn->dn_compress,2422compress);2423complevel = zio_complevel_select(os->os_spa, compress,2424complevel, complevel);24252426/*2427* Storing many references to an all zeros block in the dedup2428* table would be expensive. Instead, if dedup is enabled,2429* store them as holes even if compression is not enabled.2430*/2431if (compress == ZIO_COMPRESS_OFF &&2432dedup_checksum != ZIO_CHECKSUM_OFF)2433compress = ZIO_COMPRESS_EMPTY;24342435checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?2436zio_checksum_select(dn->dn_checksum, checksum) :2437dedup_checksum;24382439/*2440* Determine dedup setting. If we are in dmu_sync(),2441* we won't actually dedup now because that's all2442* done in syncing context; but we do want to use the2443* dedup checksum. If the checksum is not strong2444* enough to ensure unique signatures, force2445* dedup_verify.2446*/2447if (dedup_checksum != ZIO_CHECKSUM_OFF) {2448dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;2449if (!(zio_checksum_table[checksum].ci_flags &2450ZCHECKSUM_FLAG_DEDUP))2451dedup_verify = B_TRUE;2452}24532454/*2455* Enable nopwrite if we have secure enough checksum2456* algorithm (see comment in zio_nop_write) and2457* compression is enabled. We don't enable nopwrite if2458* dedup is enabled as the two features are mutually2459* exclusive.2460*/2461nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &2462ZCHECKSUM_FLAG_NOPWRITE) &&2463compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);24642465if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL ||2466(os->os_redundant_metadata ==2467ZFS_REDUNDANT_METADATA_MOST &&2468zfs_redundant_metadata_most_ditto_level <= 1))2469gang_copies++;2470}24712472/*2473* All objects in an encrypted objset are protected from modification2474* via a MAC. Encrypted objects store their IV and salt in the last DVA2475* in the bp, so we cannot use all copies. Encrypted objects are also2476* not subject to nopwrite since writing the same data will still2477* result in a new ciphertext. Only encrypted blocks can be dedup'd2478* to avoid ambiguity in the dedup code since the DDT does not store2479* object types.2480*/2481if (os->os_encrypted && (wp & WP_NOFILL) == 0) {2482encrypt = B_TRUE;24832484if (DMU_OT_IS_ENCRYPTED(type)) {2485copies = MIN(copies, SPA_DVAS_PER_BP - 1);2486gang_copies = MIN(gang_copies, SPA_DVAS_PER_BP - 1);2487nopwrite = B_FALSE;2488} else {2489dedup = B_FALSE;2490}24912492if (level <= 0 &&2493(type == DMU_OT_DNODE || type == DMU_OT_OBJSET)) {2494compress = ZIO_COMPRESS_EMPTY;2495}2496}24972498zp->zp_compress = compress;2499zp->zp_complevel = complevel;2500zp->zp_checksum = checksum;2501zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;2502zp->zp_level = level;2503zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));2504zp->zp_gang_copies = MIN(MAX(gang_copies, copies),2505spa_max_replication(os->os_spa));2506zp->zp_dedup = dedup;2507zp->zp_dedup_verify = dedup && dedup_verify;2508zp->zp_nopwrite = nopwrite;2509zp->zp_encrypt = encrypt;2510zp->zp_byteorder = ZFS_HOST_BYTEORDER;2511zp->zp_direct_write = (wp & WP_DIRECT_WR) ? B_TRUE : B_FALSE;2512zp->zp_rewrite = B_FALSE;2513memset(zp->zp_salt, 0, ZIO_DATA_SALT_LEN);2514memset(zp->zp_iv, 0, ZIO_DATA_IV_LEN);2515memset(zp->zp_mac, 0, ZIO_DATA_MAC_LEN);2516zp->zp_zpl_smallblk = (DMU_OT_IS_FILE(zp->zp_type) ||2517zp->zp_type == DMU_OT_ZVOL) ?2518os->os_zpl_special_smallblock : 0;2519zp->zp_storage_type = dn ? dn->dn_storage_type : DMU_OT_NONE;25202521ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT);2522}25232524/*2525* Reports the location of data and holes in an object. In order to2526* accurately report holes all dirty data must be synced to disk. This2527* causes extremely poor performance when seeking for holes in a dirty file.2528* As a compromise, only provide hole data when the dnode is clean. When2529* a dnode is dirty report the dnode as having no holes by returning EBUSY2530* which is always safe to do.2531*/2532int2533dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)2534{2535dnode_t *dn;2536uint64_t txg, maxtxg = 0;2537int err;25382539restart:2540err = dnode_hold(os, object, FTAG, &dn);2541if (err)2542return (err);25432544rw_enter(&dn->dn_struct_rwlock, RW_READER);25452546if (dnode_is_dirty(dn)) {2547/*2548* If the zfs_dmu_offset_next_sync module option is enabled2549* then hole reporting has been requested. Dirty dnodes2550* must be synced to disk to accurately report holes.2551*2552* Provided a RL_READER rangelock spanning 0-UINT64_MAX is2553* held by the caller only limited restarts will be required.2554* We tolerate callers which do not hold the rangelock by2555* returning EBUSY and not reporting holes after at most2556* TXG_CONCURRENT_STATES (3) restarts.2557*/2558if (zfs_dmu_offset_next_sync) {2559rw_exit(&dn->dn_struct_rwlock);2560dnode_rele(dn, FTAG);25612562if (maxtxg == 0) {2563txg = spa_last_synced_txg(dmu_objset_spa(os));2564maxtxg = txg + TXG_CONCURRENT_STATES;2565} else if (txg >= maxtxg)2566return (SET_ERROR(EBUSY));25672568txg_wait_synced(dmu_objset_pool(os), ++txg);2569goto restart;2570}25712572err = SET_ERROR(EBUSY);2573} else {2574err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK |2575(hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);2576}25772578rw_exit(&dn->dn_struct_rwlock);2579dnode_rele(dn, FTAG);25802581return (err);2582}25832584int2585dmu_read_l0_bps(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,2586blkptr_t *bps, size_t *nbpsp)2587{2588dmu_buf_t **dbp, *dbuf;2589dmu_buf_impl_t *db;2590blkptr_t *bp;2591int error, numbufs;25922593error = dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,2594&numbufs, &dbp);2595if (error != 0) {2596if (error == ESRCH) {2597error = SET_ERROR(ENXIO);2598}2599return (error);2600}26012602ASSERT3U(numbufs, <=, *nbpsp);26032604for (int i = 0; i < numbufs; i++) {2605dbuf = dbp[i];2606db = (dmu_buf_impl_t *)dbuf;26072608mutex_enter(&db->db_mtx);26092610if (!list_is_empty(&db->db_dirty_records)) {2611dbuf_dirty_record_t *dr;26122613dr = list_head(&db->db_dirty_records);2614if (dr->dt.dl.dr_brtwrite) {2615/*2616* This is very special case where we clone a2617* block and in the same transaction group we2618* read its BP (most likely to clone the clone).2619*/2620bp = &dr->dt.dl.dr_overridden_by;2621} else {2622/*2623* The block was modified in the same2624* transaction group.2625*/2626mutex_exit(&db->db_mtx);2627error = SET_ERROR(EAGAIN);2628goto out;2629}2630} else {2631bp = db->db_blkptr;2632}26332634mutex_exit(&db->db_mtx);26352636if (bp == NULL) {2637/*2638* The file size was increased, but the block was never2639* written, otherwise we would either have the block2640* pointer or the dirty record and would not get here.2641* It is effectively a hole, so report it as such.2642*/2643BP_ZERO(&bps[i]);2644continue;2645}2646/*2647* Make sure we clone only data blocks.2648*/2649if (BP_IS_METADATA(bp) && !BP_IS_HOLE(bp)) {2650error = SET_ERROR(EINVAL);2651goto out;2652}26532654/*2655* If the block was allocated in transaction group that is not2656* yet synced, we could clone it, but we couldn't write this2657* operation into ZIL, or it may be impossible to replay, since2658* the block may appear not yet allocated at that point.2659*/2660if (BP_GET_PHYSICAL_BIRTH(bp) > spa_freeze_txg(os->os_spa)) {2661error = SET_ERROR(EINVAL);2662goto out;2663}2664if (BP_GET_PHYSICAL_BIRTH(bp) >2665spa_last_synced_txg(os->os_spa)) {2666error = SET_ERROR(EAGAIN);2667goto out;2668}26692670bps[i] = *bp;2671}26722673*nbpsp = numbufs;2674out:2675dmu_buf_rele_array(dbp, numbufs, FTAG);26762677return (error);2678}26792680int2681dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,2682dmu_tx_t *tx, const blkptr_t *bps, size_t nbps)2683{2684spa_t *spa;2685dmu_buf_t **dbp, *dbuf;2686dmu_buf_impl_t *db;2687struct dirty_leaf *dl;2688dbuf_dirty_record_t *dr;2689const blkptr_t *bp;2690int error = 0, i, numbufs;26912692spa = os->os_spa;26932694VERIFY0(dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,2695&numbufs, &dbp));2696ASSERT3U(nbps, ==, numbufs);26972698/*2699* Before we start cloning make sure that the dbufs sizes match new BPs2700* sizes. If they don't, that's a no-go, as we are not able to shrink2701* dbufs.2702*/2703for (i = 0; i < numbufs; i++) {2704dbuf = dbp[i];2705db = (dmu_buf_impl_t *)dbuf;2706bp = &bps[i];27072708ASSERT3U(db->db.db_object, !=, DMU_META_DNODE_OBJECT);2709ASSERT0(db->db_level);2710ASSERT(db->db_blkid != DMU_BONUS_BLKID);2711ASSERT(db->db_blkid != DMU_SPILL_BLKID);27122713if (!BP_IS_HOLE(bp) && BP_GET_LSIZE(bp) != dbuf->db_size) {2714error = SET_ERROR(EXDEV);2715goto out;2716}2717}27182719for (i = 0; i < numbufs; i++) {2720dbuf = dbp[i];2721db = (dmu_buf_impl_t *)dbuf;2722bp = &bps[i];27232724dmu_buf_will_clone_or_dio(dbuf, tx);27252726mutex_enter(&db->db_mtx);27272728dr = list_head(&db->db_dirty_records);2729VERIFY(dr != NULL);2730ASSERT3U(dr->dr_txg, ==, tx->tx_txg);2731dl = &dr->dt.dl;2732ASSERT0(dl->dr_has_raw_params);2733dl->dr_overridden_by = *bp;2734if (!BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) != 0) {2735if (!BP_IS_EMBEDDED(bp)) {2736BP_SET_BIRTH(&dl->dr_overridden_by, dr->dr_txg,2737BP_GET_PHYSICAL_BIRTH(bp));2738BP_SET_REWRITE(&dl->dr_overridden_by, 0);2739} else {2740BP_SET_LOGICAL_BIRTH(&dl->dr_overridden_by,2741dr->dr_txg);2742}2743}2744dl->dr_brtwrite = B_TRUE;2745dl->dr_override_state = DR_OVERRIDDEN;27462747mutex_exit(&db->db_mtx);27482749/*2750* When data in embedded into BP there is no need to create2751* BRT entry as there is no data block. Just copy the BP as2752* it contains the data.2753*/2754if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {2755brt_pending_add(spa, bp, tx);2756}2757}2758out:2759dmu_buf_rele_array(dbp, numbufs, FTAG);27602761return (error);2762}27632764void2765__dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)2766{2767dnode_phys_t *dnp = dn->dn_phys;27682769doi->doi_data_block_size = dn->dn_datablksz;2770doi->doi_metadata_block_size = dn->dn_indblkshift ?27711ULL << dn->dn_indblkshift : 0;2772doi->doi_type = dn->dn_type;2773doi->doi_bonus_type = dn->dn_bonustype;2774doi->doi_bonus_size = dn->dn_bonuslen;2775doi->doi_dnodesize = dn->dn_num_slots << DNODE_SHIFT;2776doi->doi_indirection = dn->dn_nlevels;2777doi->doi_checksum = dn->dn_checksum;2778doi->doi_compress = dn->dn_compress;2779doi->doi_nblkptr = dn->dn_nblkptr;2780doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;2781doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;2782doi->doi_fill_count = 0;2783for (int i = 0; i < dnp->dn_nblkptr; i++)2784doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);2785}27862787void2788dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)2789{2790rw_enter(&dn->dn_struct_rwlock, RW_READER);2791mutex_enter(&dn->dn_mtx);27922793__dmu_object_info_from_dnode(dn, doi);27942795mutex_exit(&dn->dn_mtx);2796rw_exit(&dn->dn_struct_rwlock);2797}27982799/*2800* Get information on a DMU object.2801* If doi is NULL, just indicates whether the object exists.2802*/2803int2804dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)2805{2806dnode_t *dn;2807int err = dnode_hold(os, object, FTAG, &dn);28082809if (err)2810return (err);28112812if (doi != NULL)2813dmu_object_info_from_dnode(dn, doi);28142815dnode_rele(dn, FTAG);2816return (0);2817}28182819/*2820* As above, but faster; can be used when you have a held dbuf in hand.2821*/2822void2823dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)2824{2825dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;28262827DB_DNODE_ENTER(db);2828dmu_object_info_from_dnode(DB_DNODE(db), doi);2829DB_DNODE_EXIT(db);2830}28312832/*2833* Faster still when you only care about the size.2834*/2835void2836dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,2837u_longlong_t *nblk512)2838{2839dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;2840dnode_t *dn;28412842DB_DNODE_ENTER(db);2843dn = DB_DNODE(db);28442845*blksize = dn->dn_datablksz;2846/* add in number of slots used for the dnode itself */2847*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>2848SPA_MINBLOCKSHIFT) + dn->dn_num_slots;2849DB_DNODE_EXIT(db);2850}28512852void2853dmu_object_dnsize_from_db(dmu_buf_t *db_fake, int *dnsize)2854{2855dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;28562857DB_DNODE_ENTER(db);2858*dnsize = DB_DNODE(db)->dn_num_slots << DNODE_SHIFT;2859DB_DNODE_EXIT(db);2860}28612862void2863byteswap_uint64_array(void *vbuf, size_t size)2864{2865uint64_t *buf = vbuf;2866size_t count = size >> 3;2867int i;28682869ASSERT0((size & 7));28702871for (i = 0; i < count; i++)2872buf[i] = BSWAP_64(buf[i]);2873}28742875void2876byteswap_uint32_array(void *vbuf, size_t size)2877{2878uint32_t *buf = vbuf;2879size_t count = size >> 2;2880int i;28812882ASSERT0((size & 3));28832884for (i = 0; i < count; i++)2885buf[i] = BSWAP_32(buf[i]);2886}28872888void2889byteswap_uint16_array(void *vbuf, size_t size)2890{2891uint16_t *buf = vbuf;2892size_t count = size >> 1;2893int i;28942895ASSERT0((size & 1));28962897for (i = 0; i < count; i++)2898buf[i] = BSWAP_16(buf[i]);2899}29002901void2902byteswap_uint8_array(void *vbuf, size_t size)2903{2904(void) vbuf, (void) size;2905}29062907void2908dmu_init(void)2909{2910abd_init();2911zfs_dbgmsg_init();2912sa_cache_init();2913dmu_objset_init();2914dnode_init();2915zfetch_init();2916dmu_tx_init();2917l2arc_init();2918arc_init();2919dbuf_init();2920}29212922void2923dmu_fini(void)2924{2925arc_fini(); /* arc depends on l2arc, so arc must go first */2926l2arc_fini();2927dmu_tx_fini();2928zfetch_fini();2929dbuf_fini();2930dnode_fini();2931dmu_objset_fini();2932sa_cache_fini();2933zfs_dbgmsg_fini();2934abd_fini();2935}29362937EXPORT_SYMBOL(dmu_bonus_hold);2938EXPORT_SYMBOL(dmu_bonus_hold_by_dnode);2939EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus);2940EXPORT_SYMBOL(dmu_buf_rele_array);2941EXPORT_SYMBOL(dmu_prefetch);2942EXPORT_SYMBOL(dmu_prefetch_by_dnode);2943EXPORT_SYMBOL(dmu_prefetch_dnode);2944EXPORT_SYMBOL(dmu_free_range);2945EXPORT_SYMBOL(dmu_free_long_range);2946EXPORT_SYMBOL(dmu_free_long_object);2947EXPORT_SYMBOL(dmu_read);2948EXPORT_SYMBOL(dmu_read_by_dnode);2949EXPORT_SYMBOL(dmu_read_uio);2950EXPORT_SYMBOL(dmu_read_uio_dbuf);2951EXPORT_SYMBOL(dmu_read_uio_dnode);2952EXPORT_SYMBOL(dmu_write);2953EXPORT_SYMBOL(dmu_write_by_dnode);2954EXPORT_SYMBOL(dmu_write_uio);2955EXPORT_SYMBOL(dmu_write_uio_dbuf);2956EXPORT_SYMBOL(dmu_write_uio_dnode);2957EXPORT_SYMBOL(dmu_prealloc);2958EXPORT_SYMBOL(dmu_object_info);2959EXPORT_SYMBOL(dmu_object_info_from_dnode);2960EXPORT_SYMBOL(dmu_object_info_from_db);2961EXPORT_SYMBOL(dmu_object_size_from_db);2962EXPORT_SYMBOL(dmu_object_dnsize_from_db);2963EXPORT_SYMBOL(dmu_object_set_nlevels);2964EXPORT_SYMBOL(dmu_object_set_blocksize);2965EXPORT_SYMBOL(dmu_object_set_maxblkid);2966EXPORT_SYMBOL(dmu_object_set_checksum);2967EXPORT_SYMBOL(dmu_object_set_compress);2968EXPORT_SYMBOL(dmu_offset_next);2969EXPORT_SYMBOL(dmu_write_policy);2970EXPORT_SYMBOL(dmu_sync);2971EXPORT_SYMBOL(dmu_request_arcbuf);2972EXPORT_SYMBOL(dmu_return_arcbuf);2973EXPORT_SYMBOL(dmu_assign_arcbuf_by_dnode);2974EXPORT_SYMBOL(dmu_assign_arcbuf_by_dbuf);2975EXPORT_SYMBOL(dmu_buf_hold);2976EXPORT_SYMBOL(dmu_ot);29772978ZFS_MODULE_PARAM(zfs, zfs_, nopwrite_enabled, INT, ZMOD_RW,2979"Enable NOP writes");29802981ZFS_MODULE_PARAM(zfs, zfs_, per_txg_dirty_frees_percent, UINT, ZMOD_RW,2982"Percentage of dirtied blocks from frees in one TXG");29832984ZFS_MODULE_PARAM(zfs, zfs_, dmu_offset_next_sync, INT, ZMOD_RW,2985"Enable forcing txg sync to find holes");29862987ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,2988"Limit one prefetch call to this size");29892990ZFS_MODULE_PARAM(zfs, , dmu_ddt_copies, UINT, ZMOD_RW,2991"Override copies= for dedup objects");299229932994