Path: blob/main/sys/contrib/openzfs/module/zfs/dmu.c
107787 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, dmu_flags_t flags)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, flags);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, dmu_flags_t flags)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, flags);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 be853* interrupted. Prefetch at least SPA_MAXBLOCKSIZE at a time854* to better utilize pools with smaller block sizes.855*/856uint64_t chunksize;857if (dn->dn_indblkshift) {858uint64_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);859chunksize = (nbps * 16) << dn->dn_datablkshift;860chunksize = MAX(chunksize, SPA_MAXBLOCKSIZE);861} else {862chunksize = dn->dn_datablksz;863}864865while (size > 0) {866uint64_t mylen = MIN(size, chunksize);867868dmu_prefetch_wait_by_dnode(dn, offset, mylen);869870offset += mylen;871size -= mylen;872873if (issig()) {874err = SET_ERROR(EINTR);875break;876}877}878879dnode_rele(dn, FTAG);880881return (err);882}883884/*885* Issue prefetch I/Os for the given object's dnode.886*/887void888dmu_prefetch_dnode(objset_t *os, uint64_t object, zio_priority_t pri)889{890if (object == 0 || object >= DN_MAX_OBJECT)891return;892893dnode_t *dn = DMU_META_DNODE(os);894rw_enter(&dn->dn_struct_rwlock, RW_READER);895uint64_t blkid = dbuf_whichblock(dn, 0, object * sizeof (dnode_phys_t));896dbuf_prefetch(dn, 0, blkid, pri, 0);897rw_exit(&dn->dn_struct_rwlock);898}899900/*901* Get the next "chunk" of file data to free. We traverse the file from902* the end so that the file gets shorter over time (if we crash in the903* middle, this will leave us in a better state). We find allocated file904* data by simply searching the allocated level 1 indirects.905*906* On input, *start should be the first offset that does not need to be907* freed (e.g. "offset + length"). On return, *start will be the first908* offset that should be freed and l1blks is set to the number of level 1909* indirect blocks found within the chunk.910*/911static int912get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum, uint64_t *l1blks)913{914uint64_t blks;915uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);916/* bytes of data covered by a level-1 indirect block */917uint64_t iblkrange = (uint64_t)dn->dn_datablksz *918EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);919920ASSERT3U(minimum, <=, *start);921922/* dn_nlevels == 1 means we don't have any L1 blocks */923if (dn->dn_nlevels <= 1) {924*l1blks = 0;925*start = minimum;926return (0);927}928929/*930* Check if we can free the entire range assuming that all of the931* L1 blocks in this range have data. If we can, we use this932* worst case value as an estimate so we can avoid having to look933* at the object's actual data.934*/935uint64_t total_l1blks =936(roundup(*start, iblkrange) - (minimum / iblkrange * iblkrange)) /937iblkrange;938if (total_l1blks <= maxblks) {939*l1blks = total_l1blks;940*start = minimum;941return (0);942}943ASSERT(ISP2(iblkrange));944945for (blks = 0; *start > minimum && blks < maxblks; blks++) {946int err;947948/*949* dnode_next_offset(BACKWARDS) will find an allocated L1950* indirect block at or before the input offset. We must951* decrement *start so that it is at the end of the region952* to search.953*/954(*start)--;955956err = dnode_next_offset(dn,957DNODE_FIND_BACKWARDS, start, 2, 1, 0);958959/* if there are no indirect blocks before start, we are done */960if (err == ESRCH) {961*start = minimum;962break;963} else if (err != 0) {964*l1blks = blks;965return (err);966}967968/* set start to the beginning of this L1 indirect */969*start = P2ALIGN_TYPED(*start, iblkrange, uint64_t);970}971if (*start < minimum)972*start = minimum;973*l1blks = blks;974975return (0);976}977978/*979* If this objset is of type OST_ZFS return true if vfs's unmounted flag is set,980* otherwise return false.981* Used below in dmu_free_long_range_impl() to enable abort when unmounting982*/983static boolean_t984dmu_objset_zfs_unmounting(objset_t *os)985{986#ifdef _KERNEL987if (dmu_objset_type(os) == DMU_OST_ZFS)988return (zfs_get_vfs_flag_unmounted(os));989#else990(void) os;991#endif992return (B_FALSE);993}994995static int996dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,997uint64_t length)998{999uint64_t object_size;1000int err;1001uint64_t dirty_frees_threshold;1002dsl_pool_t *dp = dmu_objset_pool(os);10031004if (dn == NULL)1005return (SET_ERROR(EINVAL));10061007object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;1008if (offset >= object_size)1009return (0);10101011if (zfs_per_txg_dirty_frees_percent <= 100)1012dirty_frees_threshold =1013zfs_per_txg_dirty_frees_percent * zfs_dirty_data_max / 100;1014else1015dirty_frees_threshold = zfs_dirty_data_max / 20;10161017if (length == DMU_OBJECT_END || offset + length > object_size)1018length = object_size - offset;10191020while (length != 0) {1021uint64_t chunk_end, chunk_begin, chunk_len;1022uint64_t l1blks;1023dmu_tx_t *tx;10241025if (dmu_objset_zfs_unmounting(dn->dn_objset))1026return (SET_ERROR(EINTR));10271028chunk_end = chunk_begin = offset + length;10291030/* move chunk_begin backwards to the beginning of this chunk */1031err = get_next_chunk(dn, &chunk_begin, offset, &l1blks);1032if (err)1033return (err);1034ASSERT3U(chunk_begin, >=, offset);1035ASSERT3U(chunk_begin, <=, chunk_end);10361037chunk_len = chunk_end - chunk_begin;10381039tx = dmu_tx_create(os);1040dmu_tx_hold_free(tx, dn->dn_object, chunk_begin, chunk_len);10411042/*1043* Mark this transaction as typically resulting in a net1044* reduction in space used.1045*/1046dmu_tx_mark_netfree(tx);1047err = dmu_tx_assign(tx, DMU_TX_WAIT);1048if (err) {1049dmu_tx_abort(tx);1050return (err);1051}10521053uint64_t txg = dmu_tx_get_txg(tx);10541055mutex_enter(&dp->dp_lock);1056uint64_t long_free_dirty =1057dp->dp_long_free_dirty_pertxg[txg & TXG_MASK];1058mutex_exit(&dp->dp_lock);10591060/*1061* To avoid filling up a TXG with just frees, wait for1062* the next TXG to open before freeing more chunks if1063* we have reached the threshold of frees.1064*/1065if (dirty_frees_threshold != 0 &&1066long_free_dirty >= dirty_frees_threshold) {1067DMU_TX_STAT_BUMP(dmu_tx_dirty_frees_delay);1068dmu_tx_commit(tx);1069txg_wait_open(dp, 0, B_TRUE);1070continue;1071}10721073/*1074* In order to prevent unnecessary write throttling, for each1075* TXG, we track the cumulative size of L1 blocks being dirtied1076* in dnode_free_range() below. We compare this number to a1077* tunable threshold, past which we prevent new L1 dirty freeing1078* blocks from being added into the open TXG. See1079* dmu_free_long_range_impl() for details. The threshold1080* prevents write throttle activation due to dirty freeing L11081* blocks taking up a large percentage of zfs_dirty_data_max.1082*/1083mutex_enter(&dp->dp_lock);1084dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] +=1085l1blks << dn->dn_indblkshift;1086mutex_exit(&dp->dp_lock);1087DTRACE_PROBE3(free__long__range,1088uint64_t, long_free_dirty, uint64_t, chunk_len,1089uint64_t, txg);1090dnode_free_range(dn, chunk_begin, chunk_len, tx);10911092dmu_tx_commit(tx);10931094length -= chunk_len;1095}1096return (0);1097}10981099int1100dmu_free_long_range(objset_t *os, uint64_t object,1101uint64_t offset, uint64_t length)1102{1103dnode_t *dn;1104int err;11051106err = dnode_hold(os, object, FTAG, &dn);1107if (err != 0)1108return (err);1109err = dmu_free_long_range_impl(os, dn, offset, length);11101111/*1112* It is important to zero out the maxblkid when freeing the entire1113* file, so that (a) subsequent calls to dmu_free_long_range_impl()1114* will take the fast path, and (b) dnode_reallocate() can verify1115* that the entire file has been freed.1116*/1117if (err == 0 && offset == 0 && length == DMU_OBJECT_END)1118dn->dn_maxblkid = 0;11191120dnode_rele(dn, FTAG);1121return (err);1122}11231124int1125dmu_free_long_object(objset_t *os, uint64_t object)1126{1127dmu_tx_t *tx;1128int err;11291130err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);1131if (err != 0)1132return (err);11331134tx = dmu_tx_create(os);1135dmu_tx_hold_bonus(tx, object);1136dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);1137dmu_tx_mark_netfree(tx);1138err = dmu_tx_assign(tx, DMU_TX_WAIT);1139if (err == 0) {1140err = dmu_object_free(os, object, tx);1141dmu_tx_commit(tx);1142} else {1143dmu_tx_abort(tx);1144}11451146return (err);1147}11481149int1150dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,1151uint64_t size, dmu_tx_t *tx)1152{1153dnode_t *dn;1154int err = dnode_hold(os, object, FTAG, &dn);1155if (err)1156return (err);1157ASSERT(offset < UINT64_MAX);1158ASSERT(size == DMU_OBJECT_END || size <= UINT64_MAX - offset);1159dnode_free_range(dn, offset, size, tx);1160dnode_rele(dn, FTAG);1161return (0);1162}11631164static int1165dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size,1166void *buf, dmu_flags_t flags)1167{1168dmu_buf_t **dbp;1169int numbufs, err = 0;11701171/*1172* Deal with odd block sizes, where there can't be data past the first1173* block. If we ever do the tail block optimization, we will need to1174* handle that here as well.1175*/1176if (dn->dn_maxblkid == 0) {1177uint64_t newsz = offset > dn->dn_datablksz ? 0 :1178MIN(size, dn->dn_datablksz - offset);1179memset((char *)buf + newsz, 0, size - newsz);1180size = newsz;1181}11821183if (size == 0)1184return (0);11851186/* Allow Direct I/O when requested and properly aligned */1187if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned(buf) &&1188zfs_dio_aligned(offset, size, PAGESIZE)) {1189abd_t *data = abd_get_from_buf(buf, size);1190err = dmu_read_abd(dn, offset, size, data, flags);1191abd_free(data);1192return (err);1193}1194flags &= ~DMU_DIRECTIO;11951196while (size > 0) {1197uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);1198int i;11991200/*1201* NB: we could do this block-at-a-time, but it's nice1202* to be reading in parallel.1203*/1204err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,1205TRUE, FTAG, &numbufs, &dbp, flags);1206if (err)1207break;12081209for (i = 0; i < numbufs; i++) {1210uint64_t tocpy;1211int64_t bufoff;1212dmu_buf_t *db = dbp[i];12131214ASSERT(size > 0);12151216bufoff = offset - db->db_offset;1217tocpy = MIN(db->db_size - bufoff, size);12181219ASSERT(db->db_data != NULL);1220(void) memcpy(buf, (char *)db->db_data + bufoff, tocpy);12211222offset += tocpy;1223size -= tocpy;1224buf = (char *)buf + tocpy;1225}1226dmu_buf_rele_array(dbp, numbufs, FTAG);1227}1228return (err);1229}12301231int1232dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1233void *buf, dmu_flags_t flags)1234{1235dnode_t *dn;1236int err;12371238err = dnode_hold(os, object, FTAG, &dn);1239if (err != 0)1240return (err);12411242err = dmu_read_impl(dn, offset, size, buf, flags);1243dnode_rele(dn, FTAG);1244return (err);1245}12461247int1248dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf,1249dmu_flags_t flags)1250{1251return (dmu_read_impl(dn, offset, size, buf, flags));1252}12531254static void1255dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size,1256const void *buf, dmu_tx_t *tx, dmu_flags_t flags)1257{1258int i;12591260for (i = 0; i < numbufs; i++) {1261uint64_t tocpy;1262int64_t bufoff;1263dmu_buf_t *db = dbp[i];12641265ASSERT(size > 0);12661267bufoff = offset - db->db_offset;1268tocpy = MIN(db->db_size - bufoff, size);12691270ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);12711272if (tocpy == db->db_size) {1273dmu_buf_will_fill_flags(db, tx, B_FALSE, flags);1274} else {1275if (i == numbufs - 1 && bufoff + tocpy < db->db_size) {1276if (bufoff == 0)1277flags |= DMU_PARTIAL_FIRST;1278else1279flags |= DMU_PARTIAL_MORE;1280}1281dmu_buf_will_dirty_flags(db, tx, flags);1282}12831284ASSERT(db->db_data != NULL);1285(void) memcpy((char *)db->db_data + bufoff, buf, tocpy);12861287if (tocpy == db->db_size)1288dmu_buf_fill_done(db, tx, B_FALSE);12891290offset += tocpy;1291size -= tocpy;1292buf = (char *)buf + tocpy;1293}1294}12951296void1297dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1298const void *buf, dmu_tx_t *tx, dmu_flags_t flags)1299{1300dmu_buf_t **dbp;1301int numbufs;13021303if (size == 0)1304return;13051306VERIFY0(dmu_buf_hold_array(os, object, offset, size,1307FALSE, FTAG, &numbufs, &dbp, flags));1308dmu_write_impl(dbp, numbufs, offset, size, buf, tx, flags);1309dmu_buf_rele_array(dbp, numbufs, FTAG);1310}13111312int1313dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size,1314const void *buf, dmu_tx_t *tx, dmu_flags_t flags)1315{1316dmu_buf_t **dbp;1317int numbufs;1318int error;13191320if (size == 0)1321return (0);13221323/* Allow Direct I/O when requested and properly aligned */1324if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned((void *)buf) &&1325zfs_dio_aligned(offset, size, dn->dn_datablksz)) {1326abd_t *data = abd_get_from_buf((void *)buf, size);1327error = dmu_write_abd(dn, offset, size, data, flags, tx);1328abd_free(data);1329return (error);1330}1331flags &= ~DMU_DIRECTIO;13321333VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size,1334FALSE, FTAG, &numbufs, &dbp, flags));1335dmu_write_impl(dbp, numbufs, offset, size, buf, tx, flags);1336dmu_buf_rele_array(dbp, numbufs, FTAG);1337return (0);1338}13391340void1341dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1342dmu_tx_t *tx)1343{1344dmu_buf_t **dbp;1345int numbufs, i;13461347if (size == 0)1348return;13491350VERIFY0(dmu_buf_hold_array(os, object, offset, size,1351FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH));13521353for (i = 0; i < numbufs; i++) {1354dmu_buf_t *db = dbp[i];13551356dmu_buf_will_not_fill(db, tx);1357}1358dmu_buf_rele_array(dbp, numbufs, FTAG);1359}13601361void1362dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,1363void *data, uint8_t etype, uint8_t comp, int uncompressed_size,1364int compressed_size, int byteorder, dmu_tx_t *tx)1365{1366dmu_buf_t *db;13671368ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);1369ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);1370VERIFY0(dmu_buf_hold_noread(os, object, offset,1371FTAG, &db));13721373dmu_buf_write_embedded(db,1374data, (bp_embedded_type_t)etype, (enum zio_compress)comp,1375uncompressed_size, compressed_size, byteorder, tx);13761377dmu_buf_rele(db, FTAG);1378}13791380void1381dmu_redact(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,1382dmu_tx_t *tx)1383{1384int numbufs, i;1385dmu_buf_t **dbp;13861387VERIFY0(dmu_buf_hold_array(os, object, offset, size, FALSE, FTAG,1388&numbufs, &dbp, DMU_READ_PREFETCH));1389for (i = 0; i < numbufs; i++)1390dmu_buf_redact(dbp[i], tx);1391dmu_buf_rele_array(dbp, numbufs, FTAG);1392}13931394#ifdef _KERNEL1395int1396dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size,1397dmu_flags_t flags)1398{1399dmu_buf_t **dbp;1400int numbufs, i, err;14011402if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT))1403return (dmu_read_uio_direct(dn, uio, size, flags));1404flags &= ~DMU_DIRECTIO;14051406/*1407* NB: we could do this block-at-a-time, but it's nice1408* to be reading in parallel.1409*/1410err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size,1411TRUE, FTAG, &numbufs, &dbp, flags);1412if (err)1413return (err);14141415for (i = 0; i < numbufs; i++) {1416uint64_t tocpy;1417int64_t bufoff;1418dmu_buf_t *db = dbp[i];14191420ASSERT(size > 0);14211422bufoff = zfs_uio_offset(uio) - db->db_offset;1423tocpy = MIN(db->db_size - bufoff, size);14241425ASSERT(db->db_data != NULL);1426err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy,1427UIO_READ, uio);14281429if (err)1430break;14311432size -= tocpy;1433}1434dmu_buf_rele_array(dbp, numbufs, FTAG);14351436return (err);1437}14381439/*1440* Read 'size' bytes into the uio buffer.1441* From object zdb->db_object.1442* Starting at zfs_uio_offset(uio).1443*1444* If the caller already has a dbuf in the target object1445* (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),1446* because we don't have to find the dnode_t for the object.1447*/1448int1449dmu_read_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,1450dmu_flags_t flags)1451{1452dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;1453int err;14541455if (size == 0)1456return (0);14571458DB_DNODE_ENTER(db);1459err = dmu_read_uio_dnode(DB_DNODE(db), uio, size, flags);1460DB_DNODE_EXIT(db);14611462return (err);1463}14641465/*1466* Read 'size' bytes into the uio buffer.1467* From the specified object1468* Starting at offset zfs_uio_offset(uio).1469*/1470int1471dmu_read_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,1472dmu_flags_t flags)1473{1474dnode_t *dn;1475int err;14761477if (size == 0)1478return (0);14791480err = dnode_hold(os, object, FTAG, &dn);1481if (err)1482return (err);14831484err = dmu_read_uio_dnode(dn, uio, size, flags);14851486dnode_rele(dn, FTAG);14871488return (err);1489}14901491int1492dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx,1493dmu_flags_t flags)1494{1495dmu_buf_t **dbp;1496int numbufs;1497int err = 0;1498uint64_t write_size;1499dmu_flags_t oflags = flags;15001501top:1502write_size = size;15031504/*1505* We only allow Direct I/O writes to happen if we are block1506* sized aligned. Otherwise, we pass the write off to the ARC.1507*/1508if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) &&1509(write_size >= dn->dn_datablksz)) {1510if (zfs_dio_aligned(zfs_uio_offset(uio), write_size,1511dn->dn_datablksz)) {1512return (dmu_write_uio_direct(dn, uio, size, flags, tx));1513} else if (write_size > dn->dn_datablksz &&1514zfs_dio_offset_aligned(zfs_uio_offset(uio),1515dn->dn_datablksz)) {1516write_size =1517dn->dn_datablksz * (write_size / dn->dn_datablksz);1518err = dmu_write_uio_direct(dn, uio, write_size, flags,1519tx);1520if (err == 0) {1521size -= write_size;1522goto top;1523} else {1524return (err);1525}1526} else {1527write_size =1528P2PHASE(zfs_uio_offset(uio), dn->dn_datablksz);1529}1530}1531flags &= ~DMU_DIRECTIO;15321533err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), write_size,1534FALSE, FTAG, &numbufs, &dbp, flags);1535if (err)1536return (err);15371538for (int i = 0; i < numbufs; i++) {1539uint64_t tocpy;1540int64_t bufoff;1541dmu_buf_t *db = dbp[i];15421543ASSERT(write_size > 0);15441545offset_t off = zfs_uio_offset(uio);1546bufoff = off - db->db_offset;1547tocpy = MIN(db->db_size - bufoff, write_size);15481549ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);15501551if (tocpy == db->db_size) {1552dmu_buf_will_fill_flags(db, tx, B_TRUE, flags);1553} else {1554if (i == numbufs - 1 && bufoff + tocpy < db->db_size) {1555if (bufoff == 0)1556flags |= DMU_PARTIAL_FIRST;1557else1558flags |= DMU_PARTIAL_MORE;1559}1560dmu_buf_will_dirty_flags(db, tx, flags);1561}15621563ASSERT(db->db_data != NULL);1564err = zfs_uio_fault_move((char *)db->db_data + bufoff,1565tocpy, UIO_WRITE, uio);15661567if (tocpy == db->db_size && dmu_buf_fill_done(db, tx, err)) {1568/* The fill was reverted. Undo any uio progress. */1569zfs_uio_advance(uio, off - zfs_uio_offset(uio));1570}15711572if (err)1573break;15741575write_size -= tocpy;1576size -= tocpy;1577}15781579IMPLY(err == 0, write_size == 0);15801581dmu_buf_rele_array(dbp, numbufs, FTAG);15821583if ((oflags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) &&1584err == 0 && size > 0) {1585flags = oflags;1586goto top;1587}1588IMPLY(err == 0, size == 0);15891590return (err);1591}15921593/*1594* Write 'size' bytes from the uio buffer.1595* To object zdb->db_object.1596* Starting at offset zfs_uio_offset(uio).1597*1598* If the caller already has a dbuf in the target object1599* (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),1600* because we don't have to find the dnode_t for the object.1601*/1602int1603dmu_write_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,1604dmu_tx_t *tx, dmu_flags_t flags)1605{1606dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;1607int err;16081609if (size == 0)1610return (0);16111612DB_DNODE_ENTER(db);1613err = dmu_write_uio_dnode(DB_DNODE(db), uio, size, tx, flags);1614DB_DNODE_EXIT(db);16151616return (err);1617}16181619/*1620* Write 'size' bytes from the uio buffer.1621* To the specified object.1622* Starting at offset zfs_uio_offset(uio).1623*/1624int1625dmu_write_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,1626dmu_tx_t *tx, dmu_flags_t flags)1627{1628dnode_t *dn;1629int err;16301631if (size == 0)1632return (0);16331634err = dnode_hold(os, object, FTAG, &dn);1635if (err)1636return (err);16371638err = dmu_write_uio_dnode(dn, uio, size, tx, flags);16391640dnode_rele(dn, FTAG);16411642return (err);1643}1644#endif /* _KERNEL */16451646static void1647dmu_cached_bps(spa_t *spa, blkptr_t *bps, uint_t nbps,1648uint64_t *l1sz, uint64_t *l2sz)1649{1650int cached_flags;16511652if (bps == NULL)1653return;16541655for (size_t blk_off = 0; blk_off < nbps; blk_off++) {1656blkptr_t *bp = &bps[blk_off];16571658if (BP_IS_HOLE(bp))1659continue;16601661cached_flags = arc_cached(spa, bp);1662if (cached_flags == 0)1663continue;16641665if ((cached_flags & (ARC_CACHED_IN_L1 | ARC_CACHED_IN_L2)) ==1666ARC_CACHED_IN_L2)1667*l2sz += BP_GET_LSIZE(bp);1668else1669*l1sz += BP_GET_LSIZE(bp);1670}1671}16721673/*1674* Estimate DMU object cached size.1675*/1676int1677dmu_object_cached_size(objset_t *os, uint64_t object,1678uint64_t *l1sz, uint64_t *l2sz)1679{1680dnode_t *dn;1681dmu_object_info_t doi;1682int err = 0;16831684*l1sz = *l2sz = 0;16851686if (dnode_hold(os, object, FTAG, &dn) != 0)1687return (0);16881689if (dn->dn_nlevels < 2) {1690dnode_rele(dn, FTAG);1691return (0);1692}16931694dmu_object_info_from_dnode(dn, &doi);16951696for (uint64_t off = 0; off < doi.doi_max_offset &&1697dmu_prefetch_max > 0; off += dmu_prefetch_max) {1698/* dbuf_read doesn't prefetch L1 blocks. */1699dmu_prefetch_by_dnode(dn, 1, off,1700dmu_prefetch_max, ZIO_PRIORITY_SYNC_READ);1701}17021703/*1704* Hold all valid L1 blocks, asking ARC the status of each BP1705* contained in each such L1 block.1706*/1707uint_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);1708uint64_t l1blks = 1 + (dn->dn_maxblkid / nbps);17091710rw_enter(&dn->dn_struct_rwlock, RW_READER);1711for (uint64_t blk = 0; blk < l1blks; blk++) {1712dmu_buf_impl_t *db = NULL;17131714if (issig()) {1715/*1716* On interrupt, get out, and bubble up EINTR1717*/1718err = EINTR;1719break;1720}17211722/*1723* If we get an i/o error here, the L1 can't be read,1724* and nothing under it could be cached, so we just1725* continue. Ignoring the error from dbuf_hold_impl1726* or from dbuf_read is then a reasonable choice.1727*/1728err = dbuf_hold_impl(dn, 1, blk, B_TRUE, B_FALSE, FTAG, &db);1729if (err != 0) {1730/*1731* ignore error and continue1732*/1733err = 0;1734continue;1735}17361737err = dbuf_read(db, NULL, DB_RF_CANFAIL);1738if (err == 0) {1739dmu_cached_bps(dmu_objset_spa(os), db->db.db_data,1740nbps, l1sz, l2sz);1741}1742/*1743* error may be ignored, and we continue1744*/1745err = 0;1746dbuf_rele(db, FTAG);1747}1748rw_exit(&dn->dn_struct_rwlock);17491750dnode_rele(dn, FTAG);1751return (err);1752}17531754/*1755* Allocate a loaned anonymous arc buffer.1756*/1757arc_buf_t *1758dmu_request_arcbuf(dmu_buf_t *handle, int size)1759{1760dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;17611762return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size));1763}17641765/*1766* Free a loaned arc buffer.1767*/1768void1769dmu_return_arcbuf(arc_buf_t *buf)1770{1771arc_return_buf(buf, FTAG);1772arc_buf_destroy(buf, FTAG);1773}17741775/*1776* A "lightweight" write is faster than a regular write (e.g.1777* dmu_write_by_dnode() or dmu_assign_arcbuf_by_dnode()), because it avoids the1778* CPU cost of creating a dmu_buf_impl_t and arc_buf_[hdr_]_t. However, the1779* data can not be read or overwritten until the transaction's txg has been1780* synced. This makes it appropriate for workloads that are known to be1781* (temporarily) write-only, like "zfs receive".1782*1783* A single block is written, starting at the specified offset in bytes. If1784* the call is successful, it returns 0 and the provided abd has been1785* consumed (the caller should not free it).1786*/1787int1788dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd,1789const zio_prop_t *zp, zio_flag_t flags, dmu_tx_t *tx)1790{1791dbuf_dirty_record_t *dr =1792dbuf_dirty_lightweight(dn, dbuf_whichblock(dn, 0, offset), tx);1793if (dr == NULL)1794return (SET_ERROR(EIO));1795dr->dt.dll.dr_abd = abd;1796dr->dt.dll.dr_props = *zp;1797dr->dt.dll.dr_flags = flags;1798return (0);1799}18001801/*1802* When possible directly assign passed loaned arc buffer to a dbuf.1803* If this is not possible copy the contents of passed arc buf via1804* dmu_write().1805*/1806int1807dmu_assign_arcbuf_by_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf,1808dmu_tx_t *tx, dmu_flags_t flags)1809{1810dmu_buf_impl_t *db;1811objset_t *os = dn->dn_objset;1812uint32_t blksz = (uint32_t)arc_buf_lsize(buf);1813uint64_t blkid;18141815rw_enter(&dn->dn_struct_rwlock, RW_READER);1816blkid = dbuf_whichblock(dn, 0, offset);1817db = dbuf_hold(dn, blkid, FTAG);1818rw_exit(&dn->dn_struct_rwlock);1819if (db == NULL)1820return (SET_ERROR(EIO));18211822/*1823* We can only assign if the offset is aligned and the arc buf is the1824* same size as the dbuf.1825*/1826if (offset == db->db.db_offset && blksz == db->db.db_size) {1827zfs_racct_write(os->os_spa, blksz, 1, flags);1828dbuf_assign_arcbuf(db, buf, tx, flags);1829dbuf_rele(db, FTAG);1830} else {1831/* compressed bufs must always be assignable to their dbuf */1832ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF);1833ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED));18341835dbuf_rele(db, FTAG);1836dmu_write_by_dnode(dn, offset, blksz, buf->b_data, tx, flags);1837dmu_return_arcbuf(buf);1838}18391840return (0);1841}18421843int1844dmu_assign_arcbuf_by_dbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,1845dmu_tx_t *tx, dmu_flags_t flags)1846{1847int err;1848dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;18491850DB_DNODE_ENTER(db);1851err = dmu_assign_arcbuf_by_dnode(DB_DNODE(db), offset, buf, tx, flags);1852DB_DNODE_EXIT(db);18531854return (err);1855}18561857void1858dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)1859{1860(void) buf;1861dmu_sync_arg_t *dsa = varg;18621863if (zio->io_error == 0) {1864dbuf_dirty_record_t *dr = dsa->dsa_dr;1865blkptr_t *bp = zio->io_bp;18661867if (BP_IS_HOLE(bp)) {1868dmu_buf_t *db = NULL;1869if (dr)1870db = &(dr->dr_dbuf->db);1871else1872db = dsa->dsa_zgd->zgd_db;1873/*1874* A block of zeros may compress to a hole, but the1875* block size still needs to be known for replay.1876*/1877BP_SET_LSIZE(bp, db->db_size);1878} else if (!BP_IS_EMBEDDED(bp)) {1879ASSERT0(BP_GET_LEVEL(bp));1880BP_SET_FILL(bp, 1);1881}1882}1883}18841885static void1886dmu_sync_late_arrival_ready(zio_t *zio)1887{1888dmu_sync_ready(zio, NULL, zio->io_private);1889}18901891void1892dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)1893{1894(void) buf;1895dmu_sync_arg_t *dsa = varg;1896dbuf_dirty_record_t *dr = dsa->dsa_dr;1897dmu_buf_impl_t *db = dr->dr_dbuf;1898zgd_t *zgd = dsa->dsa_zgd;18991900/*1901* Record the vdev(s) backing this blkptr so they can be flushed after1902* the writes for the lwb have completed.1903*/1904if (zgd && zio->io_error == 0) {1905zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);1906}19071908mutex_enter(&db->db_mtx);1909ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);1910if (zio->io_error == 0) {1911ASSERT0(dr->dt.dl.dr_has_raw_params);1912dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);1913if (dr->dt.dl.dr_nopwrite) {1914blkptr_t *bp = zio->io_bp;1915blkptr_t *bp_orig = &zio->io_bp_orig;1916uint8_t chksum = BP_GET_CHECKSUM(bp_orig);19171918ASSERT(BP_EQUAL(bp, bp_orig));1919VERIFY(BP_EQUAL(bp, db->db_blkptr));1920ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);1921VERIFY(zio_checksum_table[chksum].ci_flags &1922ZCHECKSUM_FLAG_NOPWRITE);1923}1924dr->dt.dl.dr_overridden_by = *zio->io_bp;1925dr->dt.dl.dr_override_state = DR_OVERRIDDEN;1926dr->dt.dl.dr_copies = zio->io_prop.zp_copies;1927dr->dt.dl.dr_gang_copies = zio->io_prop.zp_gang_copies;19281929/*1930* Old style holes are filled with all zeros, whereas1931* new-style holes maintain their lsize, type, level,1932* and birth time (see zio_write_compress). While we1933* need to reset the BP_SET_LSIZE() call that happened1934* in dmu_sync_ready for old style holes, we do *not*1935* want to wipe out the information contained in new1936* style holes. Thus, only zero out the block pointer if1937* it's an old style hole.1938*/1939if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&1940BP_GET_LOGICAL_BIRTH(&dr->dt.dl.dr_overridden_by) == 0)1941BP_ZERO(&dr->dt.dl.dr_overridden_by);1942} else {1943dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;1944}19451946cv_broadcast(&db->db_changed);1947mutex_exit(&db->db_mtx);19481949if (dsa->dsa_done)1950dsa->dsa_done(dsa->dsa_zgd, zio->io_error);19511952kmem_free(dsa, sizeof (*dsa));1953}19541955static void1956dmu_sync_late_arrival_done(zio_t *zio)1957{1958blkptr_t *bp = zio->io_bp;1959dmu_sync_arg_t *dsa = zio->io_private;1960zgd_t *zgd = dsa->dsa_zgd;19611962if (zio->io_error == 0) {1963/*1964* Record the vdev(s) backing this blkptr so they can be1965* flushed after the writes for the lwb have completed.1966*/1967zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);19681969if (!BP_IS_HOLE(bp)) {1970blkptr_t *bp_orig __maybe_unused = &zio->io_bp_orig;1971ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE));1972ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));1973ASSERT(BP_GET_BIRTH(zio->io_bp) == zio->io_txg);1974ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));1975zio_free(zio->io_spa, zio->io_txg, zio->io_bp);1976}1977}19781979dmu_tx_commit(dsa->dsa_tx);19801981dsa->dsa_done(dsa->dsa_zgd, zio->io_error);19821983abd_free(zio->io_abd);1984kmem_free(dsa, sizeof (*dsa));1985}19861987static int1988dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,1989zio_prop_t *zp, zbookmark_phys_t *zb)1990{1991dmu_sync_arg_t *dsa;1992dmu_tx_t *tx;1993int error;19941995error = dbuf_read((dmu_buf_impl_t *)zgd->zgd_db, NULL,1996DB_RF_CANFAIL | DMU_READ_NO_PREFETCH | DMU_KEEP_CACHING);1997if (error != 0)1998return (error);19992000tx = dmu_tx_create(os);2001dmu_tx_hold_space(tx, zgd->zgd_db->db_size);2002/*2003* This transaction does not produce any dirty data or log blocks, so2004* it should not be throttled. All other cases wait for TXG sync, by2005* which time the log block we are writing will be obsolete, so we can2006* skip waiting and just return error here instead.2007*/2008if (dmu_tx_assign(tx, DMU_TX_NOWAIT | DMU_TX_NOTHROTTLE) != 0) {2009dmu_tx_abort(tx);2010/* Make zl_get_data do txg_waited_synced() */2011return (SET_ERROR(EIO));2012}20132014/*2015* In order to prevent the zgd's lwb from being free'd prior to2016* dmu_sync_late_arrival_done() being called, we have to ensure2017* the lwb's "max txg" takes this tx's txg into account.2018*/2019zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx));20202021dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);2022dsa->dsa_dr = NULL;2023dsa->dsa_done = done;2024dsa->dsa_zgd = zgd;2025dsa->dsa_tx = tx;20262027/*2028* Since we are currently syncing this txg, it's nontrivial to2029* determine what BP to nopwrite against, so we disable nopwrite.2030*2031* When syncing, the db_blkptr is initially the BP of the previous2032* txg. We can not nopwrite against it because it will be changed2033* (this is similar to the non-late-arrival case where the dbuf is2034* dirty in a future txg).2035*2036* Then dbuf_write_ready() sets bp_blkptr to the location we will write.2037* We can not nopwrite against it because although the BP will not2038* (typically) be changed, the data has not yet been persisted to this2039* location.2040*2041* Finally, when dbuf_write_done() is called, it is theoretically2042* possible to always nopwrite, because the data that was written in2043* this txg is the same data that we are trying to write. However we2044* would need to check that this dbuf is not dirty in any future2045* txg's (as we do in the normal dmu_sync() path). For simplicity, we2046* don't nopwrite in this case.2047*/2048zp->zp_nopwrite = B_FALSE;20492050zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,2051abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size),2052zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp,2053dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done,2054dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));20552056return (0);2057}20582059/*2060* Intent log support: sync the block associated with db to disk.2061* N.B. and XXX: the caller is responsible for making sure that the2062* data isn't changing while dmu_sync() is writing it.2063*2064* Return values:2065*2066* EEXIST: this txg has already been synced, so there's nothing to do.2067* The caller should not log the write.2068*2069* ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.2070* The caller should not log the write.2071*2072* EALREADY: this block is already in the process of being synced.2073* The caller should track its progress (somehow).2074*2075* EIO: could not do the I/O.2076* The caller should do a txg_wait_synced().2077*2078* 0: the I/O has been initiated.2079* The caller should log this blkptr in the done callback.2080* It is possible that the I/O will fail, in which case2081* the error will be reported to the done callback and2082* propagated to pio from zio_done().2083*/2084int2085dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)2086{2087dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;2088objset_t *os = db->db_objset;2089dsl_dataset_t *ds = os->os_dsl_dataset;2090dbuf_dirty_record_t *dr, *dr_next;2091dmu_sync_arg_t *dsa;2092zbookmark_phys_t zb;2093zio_prop_t zp;20942095ASSERT(pio != NULL);2096ASSERT(txg != 0);20972098SET_BOOKMARK(&zb, ds->ds_object,2099db->db.db_object, db->db_level, db->db_blkid);21002101DB_DNODE_ENTER(db);2102dmu_write_policy(os, DB_DNODE(db), db->db_level, WP_DMU_SYNC, &zp);2103DB_DNODE_EXIT(db);21042105/*2106* If we're frozen (running ziltest), we always need to generate a bp.2107*/2108if (txg > spa_freeze_txg(os->os_spa))2109return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));21102111/*2112* Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()2113* and us. If we determine that this txg is not yet syncing,2114* but it begins to sync a moment later, that's OK because the2115* sync thread will block in dbuf_sync_leaf() until we drop db_mtx.2116*/2117mutex_enter(&db->db_mtx);21182119if (txg <= spa_last_synced_txg(os->os_spa)) {2120/*2121* This txg has already synced. There's nothing to do.2122*/2123mutex_exit(&db->db_mtx);2124return (SET_ERROR(EEXIST));2125}21262127if (txg <= spa_syncing_txg(os->os_spa)) {2128/*2129* This txg is currently syncing, so we can't mess with2130* the dirty record anymore; just write a new log block.2131*/2132mutex_exit(&db->db_mtx);2133return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));2134}21352136dr = dbuf_find_dirty_eq(db, txg);21372138if (dr == NULL) {2139/*2140* There's no dr for this dbuf, so it must have been freed.2141* There's no need to log writes to freed blocks, so we're done.2142*/2143mutex_exit(&db->db_mtx);2144return (SET_ERROR(ENOENT));2145}21462147dr_next = list_next(&db->db_dirty_records, dr);2148ASSERT(dr_next == NULL || dr_next->dr_txg < txg);21492150if (db->db_blkptr != NULL) {2151/*2152* We need to fill in zgd_bp with the current blkptr so that2153* the nopwrite code can check if we're writing the same2154* data that's already on disk. We can only nopwrite if we2155* are sure that after making the copy, db_blkptr will not2156* change until our i/o completes. We ensure this by2157* holding the db_mtx, and only allowing nopwrite if the2158* block is not already dirty (see below). This is verified2159* by dmu_sync_done(), which VERIFYs that the db_blkptr has2160* not changed.2161*/2162*zgd->zgd_bp = *db->db_blkptr;2163}21642165/*2166* Assume the on-disk data is X, the current syncing data (in2167* txg - 1) is Y, and the current in-memory data is Z (currently2168* in dmu_sync).2169*2170* We usually want to perform a nopwrite if X and Z are the2171* same. However, if Y is different (i.e. the BP is going to2172* change before this write takes effect), then a nopwrite will2173* be incorrect - we would override with X, which could have2174* been freed when Y was written.2175*2176* (Note that this is not a concern when we are nop-writing from2177* syncing context, because X and Y must be identical, because2178* all previous txgs have been synced.)2179*2180* Therefore, we disable nopwrite if the current BP could change2181* before this TXG. There are two ways it could change: by2182* being dirty (dr_next is non-NULL), or by being freed2183* (dnode_block_freed()). This behavior is verified by2184* zio_done(), which VERIFYs that the override BP is identical2185* to the on-disk BP.2186*/2187if (dr_next != NULL) {2188zp.zp_nopwrite = B_FALSE;2189} else {2190DB_DNODE_ENTER(db);2191if (dnode_block_freed(DB_DNODE(db), db->db_blkid))2192zp.zp_nopwrite = B_FALSE;2193DB_DNODE_EXIT(db);2194}21952196ASSERT(dr->dr_txg == txg);2197if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||2198dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {2199/*2200* We have already issued a sync write for this buffer,2201* or this buffer has already been synced. It could not2202* have been dirtied since, or we would have cleared the state.2203*/2204mutex_exit(&db->db_mtx);2205return (SET_ERROR(EALREADY));2206}22072208ASSERT0(dr->dt.dl.dr_has_raw_params);2209ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);2210dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;2211mutex_exit(&db->db_mtx);22122213dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);2214dsa->dsa_dr = dr;2215dsa->dsa_done = done;2216dsa->dsa_zgd = zgd;2217dsa->dsa_tx = NULL;22182219zio_nowait(arc_write(pio, os->os_spa, txg, zgd->zgd_bp,2220dr->dt.dl.dr_data, !DBUF_IS_CACHEABLE(db),2221dbuf_is_l2cacheable(db, NULL), &zp, dmu_sync_ready, NULL,2222dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL,2223&zb));22242225return (0);2226}22272228int2229dmu_object_set_nlevels(objset_t *os, uint64_t object, int nlevels, dmu_tx_t *tx)2230{2231dnode_t *dn;2232int err;22332234err = dnode_hold(os, object, FTAG, &dn);2235if (err)2236return (err);2237err = dnode_set_nlevels(dn, nlevels, tx);2238dnode_rele(dn, FTAG);2239return (err);2240}22412242int2243dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,2244dmu_tx_t *tx)2245{2246dnode_t *dn;2247int err;22482249err = dnode_hold(os, object, FTAG, &dn);2250if (err)2251return (err);2252err = dnode_set_blksz(dn, size, ibs, tx);2253dnode_rele(dn, FTAG);2254return (err);2255}22562257int2258dmu_object_set_maxblkid(objset_t *os, uint64_t object, uint64_t maxblkid,2259dmu_tx_t *tx)2260{2261dnode_t *dn;2262int err;22632264err = dnode_hold(os, object, FTAG, &dn);2265if (err)2266return (err);2267rw_enter(&dn->dn_struct_rwlock, RW_WRITER);2268dnode_new_blkid(dn, maxblkid, tx, B_FALSE, B_TRUE);2269rw_exit(&dn->dn_struct_rwlock);2270dnode_rele(dn, FTAG);2271return (0);2272}22732274void2275dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,2276dmu_tx_t *tx)2277{2278dnode_t *dn;22792280/*2281* Send streams include each object's checksum function. This2282* check ensures that the receiving system can understand the2283* checksum function transmitted.2284*/2285ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);22862287VERIFY0(dnode_hold(os, object, FTAG, &dn));2288ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);2289dn->dn_checksum = checksum;2290dnode_setdirty(dn, tx);2291dnode_rele(dn, FTAG);2292}22932294void2295dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,2296dmu_tx_t *tx)2297{2298dnode_t *dn;22992300/*2301* Send streams include each object's compression function. This2302* check ensures that the receiving system can understand the2303* compression function transmitted.2304*/2305ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);23062307VERIFY0(dnode_hold(os, object, FTAG, &dn));2308dn->dn_compress = compress;2309dnode_setdirty(dn, tx);2310dnode_rele(dn, FTAG);2311}23122313/*2314* When the "redundant_metadata" property is set to "most", only indirect2315* blocks of this level and higher will have an additional ditto block.2316*/2317static const int zfs_redundant_metadata_most_ditto_level = 2;23182319void2320dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)2321{2322dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;2323boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||2324(wp & WP_SPILL));2325enum zio_checksum checksum = os->os_checksum;2326enum zio_compress compress = os->os_compress;2327uint8_t complevel = os->os_complevel;2328enum zio_checksum dedup_checksum = os->os_dedup_checksum;2329boolean_t dedup = B_FALSE;2330boolean_t nopwrite = B_FALSE;2331boolean_t dedup_verify = os->os_dedup_verify;2332boolean_t encrypt = B_FALSE;2333int copies = os->os_copies;2334int gang_copies = os->os_copies;23352336/*2337* We maintain different write policies for each of the following2338* types of data:2339* 1. metadata2340* 2. preallocated blocks (i.e. level-0 blocks of a dump device)2341* 3. all other level 0 blocks2342*/2343if (ismd) {2344/*2345* XXX -- we should design a compression algorithm2346* that specializes in arrays of bps.2347*/2348compress = zio_compress_select(os->os_spa,2349ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);23502351/*2352* Metadata always gets checksummed. If the data2353* checksum is multi-bit correctable, and it's not a2354* ZBT-style checksum, then it's suitable for metadata2355* as well. Otherwise, the metadata checksum defaults2356* to fletcher4.2357*/2358if (!(zio_checksum_table[checksum].ci_flags &2359ZCHECKSUM_FLAG_METADATA) ||2360(zio_checksum_table[checksum].ci_flags &2361ZCHECKSUM_FLAG_EMBEDDED))2362checksum = ZIO_CHECKSUM_FLETCHER_4;23632364switch (os->os_redundant_metadata) {2365case ZFS_REDUNDANT_METADATA_ALL:2366copies++;2367gang_copies++;2368break;2369case ZFS_REDUNDANT_METADATA_MOST:2370if (level >= zfs_redundant_metadata_most_ditto_level ||2371DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))2372copies++;2373if (level + 1 >=2374zfs_redundant_metadata_most_ditto_level ||2375DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))2376gang_copies++;2377break;2378case ZFS_REDUNDANT_METADATA_SOME:2379if (DMU_OT_IS_CRITICAL(type, level)) {2380copies++;2381gang_copies++;2382} else if (DMU_OT_IS_METADATA(type)) {2383gang_copies++;2384}2385break;2386case ZFS_REDUNDANT_METADATA_NONE:2387break;2388}23892390if (dmu_ddt_copies > 0) {2391/*2392* If this tunable is set, and this is a write for a2393* dedup entry store (zap or log), then we treat it2394* something like ZFS_REDUNDANT_METADATA_MOST on a2395* regular dataset: this many copies, and one more for2396* "higher" indirect blocks. This specific exception is2397* necessary because dedup objects are stored in the2398* MOS, which always has the highest possible copies.2399*/2400dmu_object_type_t stype =2401dn ? dn->dn_storage_type : DMU_OT_NONE;2402if (stype == DMU_OT_NONE)2403stype = type;2404if (stype == DMU_OT_DDT_ZAP) {2405copies = dmu_ddt_copies;2406if (level >=2407zfs_redundant_metadata_most_ditto_level)2408copies++;2409}2410}2411} else if (wp & WP_NOFILL) {2412ASSERT0(level);24132414/*2415* If we're writing preallocated blocks, we aren't actually2416* writing them so don't set any policy properties. These2417* blocks are currently only used by an external subsystem2418* outside of zfs (i.e. dump) and not written by the zio2419* pipeline.2420*/2421compress = ZIO_COMPRESS_OFF;2422checksum = ZIO_CHECKSUM_OFF;2423} else {2424compress = zio_compress_select(os->os_spa, dn->dn_compress,2425compress);2426complevel = zio_complevel_select(os->os_spa, compress,2427complevel, complevel);24282429/*2430* Storing many references to an all zeros block in the dedup2431* table would be expensive. Instead, if dedup is enabled,2432* store them as holes even if compression is not enabled.2433*/2434if (compress == ZIO_COMPRESS_OFF &&2435dedup_checksum != ZIO_CHECKSUM_OFF)2436compress = ZIO_COMPRESS_EMPTY;24372438checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?2439zio_checksum_select(dn->dn_checksum, checksum) :2440dedup_checksum;24412442/*2443* Determine dedup setting. If we are in dmu_sync(),2444* we won't actually dedup now because that's all2445* done in syncing context; but we do want to use the2446* dedup checksum. If the checksum is not strong2447* enough to ensure unique signatures, force2448* dedup_verify.2449*/2450if (dedup_checksum != ZIO_CHECKSUM_OFF) {2451dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;2452if (!(zio_checksum_table[checksum].ci_flags &2453ZCHECKSUM_FLAG_DEDUP))2454dedup_verify = B_TRUE;2455}24562457/*2458* Enable nopwrite if we have secure enough checksum2459* algorithm (see comment in zio_nop_write) and2460* compression is enabled. We don't enable nopwrite if2461* dedup is enabled as the two features are mutually2462* exclusive.2463*/2464nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &2465ZCHECKSUM_FLAG_NOPWRITE) &&2466compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);24672468if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL ||2469(os->os_redundant_metadata ==2470ZFS_REDUNDANT_METADATA_MOST &&2471zfs_redundant_metadata_most_ditto_level <= 1))2472gang_copies++;2473}24742475/*2476* All objects in an encrypted objset are protected from modification2477* via a MAC. Encrypted objects store their IV and salt in the last DVA2478* in the bp, so we cannot use all copies. Encrypted objects are also2479* not subject to nopwrite since writing the same data will still2480* result in a new ciphertext. Only encrypted blocks can be dedup'd2481* to avoid ambiguity in the dedup code since the DDT does not store2482* object types.2483*/2484if (os->os_encrypted && (wp & WP_NOFILL) == 0) {2485encrypt = B_TRUE;24862487if (DMU_OT_IS_ENCRYPTED(type)) {2488copies = MIN(copies, SPA_DVAS_PER_BP - 1);2489gang_copies = MIN(gang_copies, SPA_DVAS_PER_BP - 1);2490nopwrite = B_FALSE;2491} else {2492dedup = B_FALSE;2493}24942495if (level <= 0 &&2496(type == DMU_OT_DNODE || type == DMU_OT_OBJSET)) {2497compress = ZIO_COMPRESS_EMPTY;2498}2499}25002501zp->zp_compress = compress;2502zp->zp_complevel = complevel;2503zp->zp_checksum = checksum;2504zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;2505zp->zp_level = level;2506zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));2507zp->zp_gang_copies = MIN(MAX(gang_copies, copies),2508spa_max_replication(os->os_spa));2509zp->zp_dedup = dedup;2510zp->zp_dedup_verify = dedup && dedup_verify;2511zp->zp_nopwrite = nopwrite;2512zp->zp_encrypt = encrypt;2513zp->zp_byteorder = ZFS_HOST_BYTEORDER;2514zp->zp_direct_write = (wp & WP_DIRECT_WR) ? B_TRUE : B_FALSE;2515zp->zp_rewrite = B_FALSE;2516memset(zp->zp_salt, 0, ZIO_DATA_SALT_LEN);2517memset(zp->zp_iv, 0, ZIO_DATA_IV_LEN);2518memset(zp->zp_mac, 0, ZIO_DATA_MAC_LEN);2519zp->zp_zpl_smallblk = (DMU_OT_IS_FILE(zp->zp_type) ||2520zp->zp_type == DMU_OT_ZVOL) ?2521os->os_zpl_special_smallblock : 0;2522zp->zp_storage_type = dn ? dn->dn_storage_type : DMU_OT_NONE;25232524ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT);2525}25262527/*2528* Reports the location of data and holes in an object. In order to2529* accurately report holes all dirty data must be synced to disk. This2530* causes extremely poor performance when seeking for holes in a dirty file.2531* As a compromise, only provide hole data when the dnode is clean. When2532* a dnode is dirty report the dnode as having no holes by returning EBUSY2533* which is always safe to do.2534*/2535int2536dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)2537{2538dnode_t *dn;2539uint64_t txg, maxtxg = 0;2540int err;25412542restart:2543err = dnode_hold(os, object, FTAG, &dn);2544if (err)2545return (err);25462547rw_enter(&dn->dn_struct_rwlock, RW_READER);25482549if (dnode_is_dirty(dn)) {2550/*2551* If the zfs_dmu_offset_next_sync module option is enabled2552* then hole reporting has been requested. Dirty dnodes2553* must be synced to disk to accurately report holes.2554*2555* Provided a RL_READER rangelock spanning 0-UINT64_MAX is2556* held by the caller only limited restarts will be required.2557* We tolerate callers which do not hold the rangelock by2558* returning EBUSY and not reporting holes after at most2559* TXG_CONCURRENT_STATES (3) restarts.2560*/2561if (zfs_dmu_offset_next_sync) {2562rw_exit(&dn->dn_struct_rwlock);2563dnode_rele(dn, FTAG);25642565if (maxtxg == 0) {2566txg = spa_last_synced_txg(dmu_objset_spa(os));2567maxtxg = txg + TXG_CONCURRENT_STATES;2568} else if (txg >= maxtxg)2569return (SET_ERROR(EBUSY));25702571txg_wait_synced(dmu_objset_pool(os), ++txg);2572goto restart;2573}25742575err = SET_ERROR(EBUSY);2576} else {2577err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK |2578(hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);2579}25802581rw_exit(&dn->dn_struct_rwlock);2582dnode_rele(dn, FTAG);25832584return (err);2585}25862587int2588dmu_read_l0_bps(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,2589blkptr_t *bps, size_t *nbpsp)2590{2591dmu_buf_t **dbp, *dbuf;2592dmu_buf_impl_t *db;2593blkptr_t *bp;2594int error, numbufs;25952596error = dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,2597&numbufs, &dbp, DMU_READ_PREFETCH);2598if (error != 0) {2599if (error == ESRCH) {2600error = SET_ERROR(ENXIO);2601}2602return (error);2603}26042605ASSERT3U(numbufs, <=, *nbpsp);26062607for (int i = 0; i < numbufs; i++) {2608dbuf = dbp[i];2609db = (dmu_buf_impl_t *)dbuf;26102611mutex_enter(&db->db_mtx);26122613if (!list_is_empty(&db->db_dirty_records)) {2614dbuf_dirty_record_t *dr;26152616dr = list_head(&db->db_dirty_records);2617if (dr->dt.dl.dr_brtwrite) {2618/*2619* This is very special case where we clone a2620* block and in the same transaction group we2621* read its BP (most likely to clone the clone).2622*/2623bp = &dr->dt.dl.dr_overridden_by;2624} else {2625/*2626* The block was modified in the same2627* transaction group.2628*/2629mutex_exit(&db->db_mtx);2630error = SET_ERROR(EAGAIN);2631goto out;2632}2633} else {2634bp = db->db_blkptr;2635}26362637mutex_exit(&db->db_mtx);26382639if (bp == NULL) {2640/*2641* The file size was increased, but the block was never2642* written, otherwise we would either have the block2643* pointer or the dirty record and would not get here.2644* It is effectively a hole, so report it as such.2645*/2646BP_ZERO(&bps[i]);2647continue;2648}2649/*2650* Make sure we clone only data blocks.2651*/2652if (BP_IS_METADATA(bp) && !BP_IS_HOLE(bp)) {2653error = SET_ERROR(EINVAL);2654goto out;2655}26562657/*2658* If the block was allocated in transaction group that is not2659* yet synced, we could clone it, but we couldn't write this2660* operation into ZIL, or it may be impossible to replay, since2661* the block may appear not yet allocated at that point.2662*/2663if (BP_GET_PHYSICAL_BIRTH(bp) > spa_freeze_txg(os->os_spa)) {2664error = SET_ERROR(EINVAL);2665goto out;2666}2667if (BP_GET_PHYSICAL_BIRTH(bp) >2668spa_last_synced_txg(os->os_spa)) {2669error = SET_ERROR(EAGAIN);2670goto out;2671}26722673bps[i] = *bp;2674}26752676*nbpsp = numbufs;2677out:2678dmu_buf_rele_array(dbp, numbufs, FTAG);26792680return (error);2681}26822683int2684dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,2685dmu_tx_t *tx, const blkptr_t *bps, size_t nbps)2686{2687spa_t *spa;2688dmu_buf_t **dbp, *dbuf;2689dmu_buf_impl_t *db;2690struct dirty_leaf *dl;2691dbuf_dirty_record_t *dr;2692const blkptr_t *bp;2693int error = 0, i, numbufs;26942695spa = os->os_spa;26962697VERIFY0(dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,2698&numbufs, &dbp, DMU_READ_PREFETCH));2699ASSERT3U(nbps, ==, numbufs);27002701/*2702* Before we start cloning make sure that the dbufs sizes match new BPs2703* sizes. If they don't, that's a no-go, as we are not able to shrink2704* dbufs.2705*/2706for (i = 0; i < numbufs; i++) {2707dbuf = dbp[i];2708db = (dmu_buf_impl_t *)dbuf;2709bp = &bps[i];27102711ASSERT3U(db->db.db_object, !=, DMU_META_DNODE_OBJECT);2712ASSERT0(db->db_level);2713ASSERT(db->db_blkid != DMU_BONUS_BLKID);2714ASSERT(db->db_blkid != DMU_SPILL_BLKID);27152716if (!BP_IS_HOLE(bp) && BP_GET_LSIZE(bp) != dbuf->db_size) {2717error = SET_ERROR(EXDEV);2718goto out;2719}2720}27212722for (i = 0; i < numbufs; i++) {2723dbuf = dbp[i];2724db = (dmu_buf_impl_t *)dbuf;2725bp = &bps[i];27262727dmu_buf_will_clone_or_dio(dbuf, tx);27282729mutex_enter(&db->db_mtx);27302731dr = list_head(&db->db_dirty_records);2732VERIFY(dr != NULL);2733ASSERT3U(dr->dr_txg, ==, tx->tx_txg);2734dl = &dr->dt.dl;2735ASSERT0(dl->dr_has_raw_params);2736dl->dr_overridden_by = *bp;2737if (!BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) != 0) {2738if (!BP_IS_EMBEDDED(bp)) {2739BP_SET_BIRTH(&dl->dr_overridden_by, dr->dr_txg,2740BP_GET_PHYSICAL_BIRTH(bp));2741BP_SET_REWRITE(&dl->dr_overridden_by, 0);2742} else {2743BP_SET_LOGICAL_BIRTH(&dl->dr_overridden_by,2744dr->dr_txg);2745}2746}2747dl->dr_brtwrite = B_TRUE;2748dl->dr_override_state = DR_OVERRIDDEN;27492750mutex_exit(&db->db_mtx);27512752/*2753* When data in embedded into BP there is no need to create2754* BRT entry as there is no data block. Just copy the BP as2755* it contains the data.2756*/2757if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {2758brt_pending_add(spa, bp, tx);2759}2760}2761out:2762dmu_buf_rele_array(dbp, numbufs, FTAG);27632764return (error);2765}27662767void2768__dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)2769{2770dnode_phys_t *dnp = dn->dn_phys;27712772doi->doi_data_block_size = dn->dn_datablksz;2773doi->doi_metadata_block_size = dn->dn_indblkshift ?27741ULL << dn->dn_indblkshift : 0;2775doi->doi_type = dn->dn_type;2776doi->doi_bonus_type = dn->dn_bonustype;2777doi->doi_bonus_size = dn->dn_bonuslen;2778doi->doi_dnodesize = dn->dn_num_slots << DNODE_SHIFT;2779doi->doi_indirection = dn->dn_nlevels;2780doi->doi_checksum = dn->dn_checksum;2781doi->doi_compress = dn->dn_compress;2782doi->doi_nblkptr = dn->dn_nblkptr;2783doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;2784doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;2785doi->doi_fill_count = 0;2786for (int i = 0; i < dnp->dn_nblkptr; i++)2787doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);2788}27892790void2791dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)2792{2793rw_enter(&dn->dn_struct_rwlock, RW_READER);2794mutex_enter(&dn->dn_mtx);27952796__dmu_object_info_from_dnode(dn, doi);27972798mutex_exit(&dn->dn_mtx);2799rw_exit(&dn->dn_struct_rwlock);2800}28012802/*2803* Get information on a DMU object.2804* If doi is NULL, just indicates whether the object exists.2805*/2806int2807dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)2808{2809dnode_t *dn;2810int err = dnode_hold(os, object, FTAG, &dn);28112812if (err)2813return (err);28142815if (doi != NULL)2816dmu_object_info_from_dnode(dn, doi);28172818dnode_rele(dn, FTAG);2819return (0);2820}28212822/*2823* As above, but faster; can be used when you have a held dbuf in hand.2824*/2825void2826dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)2827{2828dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;28292830DB_DNODE_ENTER(db);2831dmu_object_info_from_dnode(DB_DNODE(db), doi);2832DB_DNODE_EXIT(db);2833}28342835/*2836* Faster still when you only care about the size.2837*/2838void2839dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,2840u_longlong_t *nblk512)2841{2842dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;2843dnode_t *dn;28442845DB_DNODE_ENTER(db);2846dn = DB_DNODE(db);28472848*blksize = dn->dn_datablksz;2849/* add in number of slots used for the dnode itself */2850*nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>2851SPA_MINBLOCKSHIFT) + dn->dn_num_slots;2852DB_DNODE_EXIT(db);2853}28542855void2856dmu_object_dnsize_from_db(dmu_buf_t *db_fake, int *dnsize)2857{2858dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;28592860DB_DNODE_ENTER(db);2861*dnsize = DB_DNODE(db)->dn_num_slots << DNODE_SHIFT;2862DB_DNODE_EXIT(db);2863}28642865void2866byteswap_uint64_array(void *vbuf, size_t size)2867{2868uint64_t *buf = vbuf;2869size_t count = size >> 3;2870int i;28712872ASSERT0((size & 7));28732874for (i = 0; i < count; i++)2875buf[i] = BSWAP_64(buf[i]);2876}28772878void2879byteswap_uint32_array(void *vbuf, size_t size)2880{2881uint32_t *buf = vbuf;2882size_t count = size >> 2;2883int i;28842885ASSERT0((size & 3));28862887for (i = 0; i < count; i++)2888buf[i] = BSWAP_32(buf[i]);2889}28902891void2892byteswap_uint16_array(void *vbuf, size_t size)2893{2894uint16_t *buf = vbuf;2895size_t count = size >> 1;2896int i;28972898ASSERT0((size & 1));28992900for (i = 0; i < count; i++)2901buf[i] = BSWAP_16(buf[i]);2902}29032904void2905byteswap_uint8_array(void *vbuf, size_t size)2906{2907(void) vbuf, (void) size;2908}29092910void2911dmu_init(void)2912{2913abd_init();2914zfs_dbgmsg_init();2915sa_cache_init();2916dmu_objset_init();2917dnode_init();2918zfetch_init();2919dmu_tx_init();2920l2arc_init();2921arc_init();2922dbuf_init();2923}29242925void2926dmu_fini(void)2927{2928arc_fini(); /* arc depends on l2arc, so arc must go first */2929l2arc_fini();2930dmu_tx_fini();2931zfetch_fini();2932dbuf_fini();2933dnode_fini();2934dmu_objset_fini();2935sa_cache_fini();2936zfs_dbgmsg_fini();2937abd_fini();2938}29392940EXPORT_SYMBOL(dmu_bonus_hold);2941EXPORT_SYMBOL(dmu_bonus_hold_by_dnode);2942EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus);2943EXPORT_SYMBOL(dmu_buf_rele_array);2944EXPORT_SYMBOL(dmu_prefetch);2945EXPORT_SYMBOL(dmu_prefetch_by_dnode);2946EXPORT_SYMBOL(dmu_prefetch_dnode);2947EXPORT_SYMBOL(dmu_free_range);2948EXPORT_SYMBOL(dmu_free_long_range);2949EXPORT_SYMBOL(dmu_free_long_object);2950EXPORT_SYMBOL(dmu_read);2951EXPORT_SYMBOL(dmu_read_by_dnode);2952EXPORT_SYMBOL(dmu_read_uio);2953EXPORT_SYMBOL(dmu_read_uio_dbuf);2954EXPORT_SYMBOL(dmu_read_uio_dnode);2955EXPORT_SYMBOL(dmu_write);2956EXPORT_SYMBOL(dmu_write_by_dnode);2957EXPORT_SYMBOL(dmu_write_uio);2958EXPORT_SYMBOL(dmu_write_uio_dbuf);2959EXPORT_SYMBOL(dmu_write_uio_dnode);2960EXPORT_SYMBOL(dmu_prealloc);2961EXPORT_SYMBOL(dmu_object_info);2962EXPORT_SYMBOL(dmu_object_info_from_dnode);2963EXPORT_SYMBOL(dmu_object_info_from_db);2964EXPORT_SYMBOL(dmu_object_size_from_db);2965EXPORT_SYMBOL(dmu_object_dnsize_from_db);2966EXPORT_SYMBOL(dmu_object_set_nlevels);2967EXPORT_SYMBOL(dmu_object_set_blocksize);2968EXPORT_SYMBOL(dmu_object_set_maxblkid);2969EXPORT_SYMBOL(dmu_object_set_checksum);2970EXPORT_SYMBOL(dmu_object_set_compress);2971EXPORT_SYMBOL(dmu_offset_next);2972EXPORT_SYMBOL(dmu_write_policy);2973EXPORT_SYMBOL(dmu_sync);2974EXPORT_SYMBOL(dmu_request_arcbuf);2975EXPORT_SYMBOL(dmu_return_arcbuf);2976EXPORT_SYMBOL(dmu_assign_arcbuf_by_dnode);2977EXPORT_SYMBOL(dmu_assign_arcbuf_by_dbuf);2978EXPORT_SYMBOL(dmu_buf_hold);2979EXPORT_SYMBOL(dmu_ot);29802981ZFS_MODULE_PARAM(zfs, zfs_, nopwrite_enabled, INT, ZMOD_RW,2982"Enable NOP writes");29832984ZFS_MODULE_PARAM(zfs, zfs_, per_txg_dirty_frees_percent, UINT, ZMOD_RW,2985"Percentage of dirtied blocks from frees in one TXG");29862987ZFS_MODULE_PARAM(zfs, zfs_, dmu_offset_next_sync, INT, ZMOD_RW,2988"Enable forcing txg sync to find holes");29892990ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,2991"Limit one prefetch call to this size");29922993ZFS_MODULE_PARAM(zfs, , dmu_ddt_copies, UINT, ZMOD_RW,2994"Override copies= for dedup objects");299529962997