1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22cdb0ab79Smaybee * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #include <sys/dmu.h> 27fa9e4066Sahrens #include <sys/dmu_impl.h> 28fa9e4066Sahrens #include <sys/dmu_tx.h> 29fa9e4066Sahrens #include <sys/dbuf.h> 30fa9e4066Sahrens #include <sys/dnode.h> 31fa9e4066Sahrens #include <sys/zfs_context.h> 32fa9e4066Sahrens #include <sys/dmu_objset.h> 33fa9e4066Sahrens #include <sys/dmu_traverse.h> 34fa9e4066Sahrens #include <sys/dsl_dataset.h> 35fa9e4066Sahrens #include <sys/dsl_dir.h> 36fa9e4066Sahrens #include <sys/dsl_pool.h> 371d452cf5Sahrens #include <sys/dsl_synctask.h> 38a2eea2e1Sahrens #include <sys/dsl_prop.h> 39fa9e4066Sahrens #include <sys/dmu_zfetch.h> 40fa9e4066Sahrens #include <sys/zfs_ioctl.h> 41fa9e4066Sahrens #include <sys/zap.h> 42ea8dc4b6Seschrock #include <sys/zio_checksum.h> 4344eda4d7Smaybee #ifdef _KERNEL 4444eda4d7Smaybee #include <sys/vmsystm.h> 450fab61baSJonathan W Adams #include <sys/zfs_znode.h> 4644eda4d7Smaybee #endif 47fa9e4066Sahrens 48fa9e4066Sahrens const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = { 49fa9e4066Sahrens { byteswap_uint8_array, TRUE, "unallocated" }, 50fa9e4066Sahrens { zap_byteswap, TRUE, "object directory" }, 51fa9e4066Sahrens { byteswap_uint64_array, TRUE, "object array" }, 52fa9e4066Sahrens { byteswap_uint8_array, TRUE, "packed nvlist" }, 53fa9e4066Sahrens { byteswap_uint64_array, TRUE, "packed nvlist size" }, 54fa9e4066Sahrens { byteswap_uint64_array, TRUE, "bplist" }, 55fa9e4066Sahrens { byteswap_uint64_array, TRUE, "bplist header" }, 56fa9e4066Sahrens { byteswap_uint64_array, TRUE, "SPA space map header" }, 57fa9e4066Sahrens { byteswap_uint64_array, TRUE, "SPA space map" }, 58fa9e4066Sahrens { byteswap_uint64_array, TRUE, "ZIL intent log" }, 59fa9e4066Sahrens { dnode_buf_byteswap, TRUE, "DMU dnode" }, 60fa9e4066Sahrens { dmu_objset_byteswap, TRUE, "DMU objset" }, 61fa9e4066Sahrens { byteswap_uint64_array, TRUE, "DSL directory" }, 62fa9e4066Sahrens { zap_byteswap, TRUE, "DSL directory child map"}, 63fa9e4066Sahrens { zap_byteswap, TRUE, "DSL dataset snap map" }, 64fa9e4066Sahrens { zap_byteswap, TRUE, "DSL props" }, 65fa9e4066Sahrens { byteswap_uint64_array, TRUE, "DSL dataset" }, 66fa9e4066Sahrens { zfs_znode_byteswap, TRUE, "ZFS znode" }, 67da6c28aaSamw { zfs_oldacl_byteswap, TRUE, "ZFS V0 ACL" }, 68fa9e4066Sahrens { byteswap_uint8_array, FALSE, "ZFS plain file" }, 69fa9e4066Sahrens { zap_byteswap, TRUE, "ZFS directory" }, 70fa9e4066Sahrens { zap_byteswap, TRUE, "ZFS master node" }, 71fa9e4066Sahrens { zap_byteswap, TRUE, "ZFS delete queue" }, 72fa9e4066Sahrens { byteswap_uint8_array, FALSE, "zvol object" }, 73fa9e4066Sahrens { zap_byteswap, TRUE, "zvol prop" }, 74fa9e4066Sahrens { byteswap_uint8_array, FALSE, "other uint8[]" }, 75fa9e4066Sahrens { byteswap_uint64_array, FALSE, "other uint64[]" }, 76fa9e4066Sahrens { zap_byteswap, TRUE, "other ZAP" }, 77ea8dc4b6Seschrock { zap_byteswap, TRUE, "persistent error log" }, 7806eeb2adSek110237 { byteswap_uint8_array, TRUE, "SPA history" }, 7906eeb2adSek110237 { byteswap_uint64_array, TRUE, "SPA history offsets" }, 80b1b8ab34Slling { zap_byteswap, TRUE, "Pool properties" }, 81da6c28aaSamw { zap_byteswap, TRUE, "DSL permissions" }, 82da6c28aaSamw { zfs_acl_byteswap, TRUE, "ZFS ACL" }, 83da6c28aaSamw { byteswap_uint8_array, TRUE, "ZFS SYSACL" }, 84da6c28aaSamw { byteswap_uint8_array, TRUE, "FUID table" }, 85add89791Smarks { byteswap_uint64_array, TRUE, "FUID table size" }, 86088f3894Sahrens { zap_byteswap, TRUE, "DSL dataset next clones"}, 87088f3894Sahrens { zap_byteswap, TRUE, "scrub work queue" }, 88fa9e4066Sahrens }; 89fa9e4066Sahrens 90fa9e4066Sahrens int 91ea8dc4b6Seschrock dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset, 92ea8dc4b6Seschrock void *tag, dmu_buf_t **dbp) 93fa9e4066Sahrens { 94fa9e4066Sahrens dnode_t *dn; 95fa9e4066Sahrens uint64_t blkid; 96fa9e4066Sahrens dmu_buf_impl_t *db; 97ea8dc4b6Seschrock int err; 98fa9e4066Sahrens 99ea8dc4b6Seschrock err = dnode_hold(os->os, object, FTAG, &dn); 100ea8dc4b6Seschrock if (err) 101ea8dc4b6Seschrock return (err); 102fa9e4066Sahrens blkid = dbuf_whichblock(dn, offset); 103fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 104ea8dc4b6Seschrock db = dbuf_hold(dn, blkid, tag); 105fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 106ea8dc4b6Seschrock if (db == NULL) { 107ea8dc4b6Seschrock err = EIO; 108ea8dc4b6Seschrock } else { 109ea8dc4b6Seschrock err = dbuf_read(db, NULL, DB_RF_CANFAIL); 110ea8dc4b6Seschrock if (err) { 111ea8dc4b6Seschrock dbuf_rele(db, tag); 112ea8dc4b6Seschrock db = NULL; 113ea8dc4b6Seschrock } 114fa9e4066Sahrens } 115fa9e4066Sahrens 116ea8dc4b6Seschrock dnode_rele(dn, FTAG); 117ea8dc4b6Seschrock *dbp = &db->db; 118ea8dc4b6Seschrock return (err); 119fa9e4066Sahrens } 120fa9e4066Sahrens 121fa9e4066Sahrens int 122fa9e4066Sahrens dmu_bonus_max(void) 123fa9e4066Sahrens { 124fa9e4066Sahrens return (DN_MAX_BONUSLEN); 125fa9e4066Sahrens } 126fa9e4066Sahrens 1271934e92fSmaybee int 1281934e92fSmaybee dmu_set_bonus(dmu_buf_t *db, int newsize, dmu_tx_t *tx) 1291934e92fSmaybee { 1301934e92fSmaybee dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode; 1311934e92fSmaybee 1321934e92fSmaybee if (dn->dn_bonus != (dmu_buf_impl_t *)db) 1331934e92fSmaybee return (EINVAL); 1341934e92fSmaybee if (newsize < 0 || newsize > db->db_size) 1351934e92fSmaybee return (EINVAL); 1361934e92fSmaybee dnode_setbonuslen(dn, newsize, tx); 1371934e92fSmaybee return (0); 1381934e92fSmaybee } 1391934e92fSmaybee 140fa9e4066Sahrens /* 141ea8dc4b6Seschrock * returns ENOENT, EIO, or 0. 142fa9e4066Sahrens */ 143ea8dc4b6Seschrock int 144ea8dc4b6Seschrock dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp) 145fa9e4066Sahrens { 146ea8dc4b6Seschrock dnode_t *dn; 147fa9e4066Sahrens dmu_buf_impl_t *db; 1481934e92fSmaybee int error; 149fa9e4066Sahrens 1501934e92fSmaybee error = dnode_hold(os->os, object, FTAG, &dn); 1511934e92fSmaybee if (error) 1521934e92fSmaybee return (error); 153fa9e4066Sahrens 154fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 155ea8dc4b6Seschrock if (dn->dn_bonus == NULL) { 156fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 157ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 158ea8dc4b6Seschrock if (dn->dn_bonus == NULL) 1591934e92fSmaybee dbuf_create_bonus(dn); 160fa9e4066Sahrens } 161ea8dc4b6Seschrock db = dn->dn_bonus; 162ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 1631934e92fSmaybee 1641934e92fSmaybee /* as long as the bonus buf is held, the dnode will be held */ 1651934e92fSmaybee if (refcount_add(&db->db_holds, tag) == 1) 1661934e92fSmaybee VERIFY(dnode_add_ref(dn, db)); 1671934e92fSmaybee 168fa9e4066Sahrens dnode_rele(dn, FTAG); 169ea8dc4b6Seschrock 170ea8dc4b6Seschrock VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED)); 171ea8dc4b6Seschrock 172ea8dc4b6Seschrock *dbp = &db->db; 173ea8dc4b6Seschrock return (0); 174fa9e4066Sahrens } 175fa9e4066Sahrens 17613506d1eSmaybee /* 17713506d1eSmaybee * Note: longer-term, we should modify all of the dmu_buf_*() interfaces 17813506d1eSmaybee * to take a held dnode rather than <os, object> -- the lookup is wasteful, 17913506d1eSmaybee * and can induce severe lock contention when writing to several files 18013506d1eSmaybee * whose dnodes are in the same block. 18113506d1eSmaybee */ 18213506d1eSmaybee static int 18313506d1eSmaybee dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, 184ea8dc4b6Seschrock uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 185fa9e4066Sahrens { 18605715f94SMark Maybee dsl_pool_t *dp = NULL; 187fa9e4066Sahrens dmu_buf_t **dbp; 188fa9e4066Sahrens uint64_t blkid, nblks, i; 189ea8dc4b6Seschrock uint32_t flags; 190ea8dc4b6Seschrock int err; 191ea8dc4b6Seschrock zio_t *zio; 19205715f94SMark Maybee hrtime_t start; 193ea8dc4b6Seschrock 194ea8dc4b6Seschrock ASSERT(length <= DMU_MAX_ACCESS); 195fa9e4066Sahrens 196ea8dc4b6Seschrock flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT; 197e1930233Sbonwick if (length > zfetch_array_rd_sz) 198ea8dc4b6Seschrock flags |= DB_RF_NOPREFETCH; 199ea8dc4b6Seschrock 200fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 201fa9e4066Sahrens if (dn->dn_datablkshift) { 202fa9e4066Sahrens int blkshift = dn->dn_datablkshift; 203fa9e4066Sahrens nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) - 204fa9e4066Sahrens P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift; 205fa9e4066Sahrens } else { 2060125049cSahrens if (offset + length > dn->dn_datablksz) { 2070125049cSahrens zfs_panic_recover("zfs: accessing past end of object " 2080125049cSahrens "%llx/%llx (size=%u access=%llu+%llu)", 2090125049cSahrens (longlong_t)dn->dn_objset-> 2100125049cSahrens os_dsl_dataset->ds_object, 2110125049cSahrens (longlong_t)dn->dn_object, dn->dn_datablksz, 2120125049cSahrens (longlong_t)offset, (longlong_t)length); 2130125049cSahrens return (EIO); 2140125049cSahrens } 215fa9e4066Sahrens nblks = 1; 216fa9e4066Sahrens } 217ea8dc4b6Seschrock dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP); 218fa9e4066Sahrens 21905715f94SMark Maybee if (dn->dn_objset->os_dsl_dataset) 22005715f94SMark Maybee dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool; 22105715f94SMark Maybee if (dp && dsl_pool_sync_context(dp)) 22205715f94SMark Maybee start = gethrtime(); 223*e14bb325SJeff Bonwick zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL); 224fa9e4066Sahrens blkid = dbuf_whichblock(dn, offset); 225fa9e4066Sahrens for (i = 0; i < nblks; i++) { 226ea8dc4b6Seschrock dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag); 227ea8dc4b6Seschrock if (db == NULL) { 228ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 229ea8dc4b6Seschrock dmu_buf_rele_array(dbp, nblks, tag); 230ea8dc4b6Seschrock zio_nowait(zio); 231ea8dc4b6Seschrock return (EIO); 232ea8dc4b6Seschrock } 233ea8dc4b6Seschrock /* initiate async i/o */ 23413506d1eSmaybee if (read) { 235ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 236ea8dc4b6Seschrock (void) dbuf_read(db, zio, flags); 237ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_READER); 238ea8dc4b6Seschrock } 239ea8dc4b6Seschrock dbp[i] = &db->db; 240fa9e4066Sahrens } 241fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 242fa9e4066Sahrens 243ea8dc4b6Seschrock /* wait for async i/o */ 244ea8dc4b6Seschrock err = zio_wait(zio); 24505715f94SMark Maybee /* track read overhead when we are in sync context */ 24605715f94SMark Maybee if (dp && dsl_pool_sync_context(dp)) 24705715f94SMark Maybee dp->dp_read_overhead += gethrtime() - start; 248ea8dc4b6Seschrock if (err) { 249ea8dc4b6Seschrock dmu_buf_rele_array(dbp, nblks, tag); 250ea8dc4b6Seschrock return (err); 251ea8dc4b6Seschrock } 252ea8dc4b6Seschrock 253ea8dc4b6Seschrock /* wait for other io to complete */ 254ea8dc4b6Seschrock if (read) { 255ea8dc4b6Seschrock for (i = 0; i < nblks; i++) { 256ea8dc4b6Seschrock dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i]; 257ea8dc4b6Seschrock mutex_enter(&db->db_mtx); 258ea8dc4b6Seschrock while (db->db_state == DB_READ || 259ea8dc4b6Seschrock db->db_state == DB_FILL) 260ea8dc4b6Seschrock cv_wait(&db->db_changed, &db->db_mtx); 261ea8dc4b6Seschrock if (db->db_state == DB_UNCACHED) 262ea8dc4b6Seschrock err = EIO; 263ea8dc4b6Seschrock mutex_exit(&db->db_mtx); 264ea8dc4b6Seschrock if (err) { 265ea8dc4b6Seschrock dmu_buf_rele_array(dbp, nblks, tag); 266ea8dc4b6Seschrock return (err); 267ea8dc4b6Seschrock } 268ea8dc4b6Seschrock } 269ea8dc4b6Seschrock } 270ea8dc4b6Seschrock 271ea8dc4b6Seschrock *numbufsp = nblks; 272ea8dc4b6Seschrock *dbpp = dbp; 273ea8dc4b6Seschrock return (0); 274fa9e4066Sahrens } 275fa9e4066Sahrens 276a2eea2e1Sahrens static int 27713506d1eSmaybee dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset, 27813506d1eSmaybee uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 27913506d1eSmaybee { 28013506d1eSmaybee dnode_t *dn; 28113506d1eSmaybee int err; 28213506d1eSmaybee 28313506d1eSmaybee err = dnode_hold(os->os, object, FTAG, &dn); 28413506d1eSmaybee if (err) 28513506d1eSmaybee return (err); 28613506d1eSmaybee 28713506d1eSmaybee err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 28813506d1eSmaybee numbufsp, dbpp); 28913506d1eSmaybee 29013506d1eSmaybee dnode_rele(dn, FTAG); 29113506d1eSmaybee 29213506d1eSmaybee return (err); 29313506d1eSmaybee } 29413506d1eSmaybee 29513506d1eSmaybee int 29613506d1eSmaybee dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset, 29713506d1eSmaybee uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp) 29813506d1eSmaybee { 29913506d1eSmaybee dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode; 30013506d1eSmaybee int err; 30113506d1eSmaybee 30213506d1eSmaybee err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag, 30313506d1eSmaybee numbufsp, dbpp); 30413506d1eSmaybee 30513506d1eSmaybee return (err); 30613506d1eSmaybee } 30713506d1eSmaybee 308fa9e4066Sahrens void 309ea8dc4b6Seschrock dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag) 310fa9e4066Sahrens { 311fa9e4066Sahrens int i; 312fa9e4066Sahrens dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake; 313fa9e4066Sahrens 314fa9e4066Sahrens if (numbufs == 0) 315fa9e4066Sahrens return; 316fa9e4066Sahrens 317ea8dc4b6Seschrock for (i = 0; i < numbufs; i++) { 318ea8dc4b6Seschrock if (dbp[i]) 319ea8dc4b6Seschrock dbuf_rele(dbp[i], tag); 320ea8dc4b6Seschrock } 321fa9e4066Sahrens 322fa9e4066Sahrens kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs); 323fa9e4066Sahrens } 324fa9e4066Sahrens 325fa9e4066Sahrens void 326fa9e4066Sahrens dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len) 327fa9e4066Sahrens { 328fa9e4066Sahrens dnode_t *dn; 329fa9e4066Sahrens uint64_t blkid; 330ea8dc4b6Seschrock int nblks, i, err; 331fa9e4066Sahrens 332416e0cd8Sek110237 if (zfs_prefetch_disable) 333416e0cd8Sek110237 return; 334416e0cd8Sek110237 335fa9e4066Sahrens if (len == 0) { /* they're interested in the bonus buffer */ 336fa9e4066Sahrens dn = os->os->os_meta_dnode; 337fa9e4066Sahrens 338fa9e4066Sahrens if (object == 0 || object >= DN_MAX_OBJECT) 339fa9e4066Sahrens return; 340fa9e4066Sahrens 341fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 342fa9e4066Sahrens blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t)); 343fa9e4066Sahrens dbuf_prefetch(dn, blkid); 344fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 345fa9e4066Sahrens return; 346fa9e4066Sahrens } 347fa9e4066Sahrens 348fa9e4066Sahrens /* 349fa9e4066Sahrens * XXX - Note, if the dnode for the requested object is not 350fa9e4066Sahrens * already cached, we will do a *synchronous* read in the 351fa9e4066Sahrens * dnode_hold() call. The same is true for any indirects. 352fa9e4066Sahrens */ 353ea8dc4b6Seschrock err = dnode_hold(os->os, object, FTAG, &dn); 354ea8dc4b6Seschrock if (err != 0) 355fa9e4066Sahrens return; 356fa9e4066Sahrens 357fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 358fa9e4066Sahrens if (dn->dn_datablkshift) { 359fa9e4066Sahrens int blkshift = dn->dn_datablkshift; 360fa9e4066Sahrens nblks = (P2ROUNDUP(offset+len, 1<<blkshift) - 361fa9e4066Sahrens P2ALIGN(offset, 1<<blkshift)) >> blkshift; 362fa9e4066Sahrens } else { 363fa9e4066Sahrens nblks = (offset < dn->dn_datablksz); 364fa9e4066Sahrens } 365fa9e4066Sahrens 366fa9e4066Sahrens if (nblks != 0) { 367fa9e4066Sahrens blkid = dbuf_whichblock(dn, offset); 368fa9e4066Sahrens for (i = 0; i < nblks; i++) 369fa9e4066Sahrens dbuf_prefetch(dn, blkid+i); 370fa9e4066Sahrens } 371fa9e4066Sahrens 372fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 373fa9e4066Sahrens 374fa9e4066Sahrens dnode_rele(dn, FTAG); 375fa9e4066Sahrens } 376fa9e4066Sahrens 377cdb0ab79Smaybee static int 378cdb0ab79Smaybee get_next_chunk(dnode_t *dn, uint64_t *offset, uint64_t limit) 379cdb0ab79Smaybee { 3801c8564a7SMark Maybee uint64_t len = *offset - limit; 381cdb0ab79Smaybee uint64_t chunk_len = dn->dn_datablksz * DMU_MAX_DELETEBLKCNT; 3821c8564a7SMark Maybee uint64_t subchunk = 3831c8564a7SMark Maybee dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT); 384cdb0ab79Smaybee 385cdb0ab79Smaybee ASSERT(limit <= *offset); 386cdb0ab79Smaybee 3871c8564a7SMark Maybee if (len <= chunk_len) { 388cdb0ab79Smaybee *offset = limit; 389cdb0ab79Smaybee return (0); 390cdb0ab79Smaybee } 391cdb0ab79Smaybee 3921c8564a7SMark Maybee ASSERT(ISP2(subchunk)); 3931c8564a7SMark Maybee 394cdb0ab79Smaybee while (*offset > limit) { 3951c8564a7SMark Maybee uint64_t initial_offset = P2ROUNDUP(*offset, subchunk); 396cdb0ab79Smaybee uint64_t delta; 3971c8564a7SMark Maybee int err; 398cdb0ab79Smaybee 399cdb0ab79Smaybee /* skip over allocated data */ 400cdb0ab79Smaybee err = dnode_next_offset(dn, 401cdb0ab79Smaybee DNODE_FIND_HOLE|DNODE_FIND_BACKWARDS, offset, 1, 1, 0); 402cdb0ab79Smaybee if (err == ESRCH) 403cdb0ab79Smaybee *offset = limit; 404cdb0ab79Smaybee else if (err) 405cdb0ab79Smaybee return (err); 406cdb0ab79Smaybee 407cdb0ab79Smaybee ASSERT3U(*offset, <=, initial_offset); 4081c8564a7SMark Maybee *offset = P2ALIGN(*offset, subchunk); 409cdb0ab79Smaybee delta = initial_offset - *offset; 410cdb0ab79Smaybee if (delta >= chunk_len) { 411cdb0ab79Smaybee *offset += delta - chunk_len; 412cdb0ab79Smaybee return (0); 413cdb0ab79Smaybee } 414cdb0ab79Smaybee chunk_len -= delta; 415cdb0ab79Smaybee 416cdb0ab79Smaybee /* skip over unallocated data */ 417cdb0ab79Smaybee err = dnode_next_offset(dn, 418cdb0ab79Smaybee DNODE_FIND_BACKWARDS, offset, 1, 1, 0); 419cdb0ab79Smaybee if (err == ESRCH) 420cdb0ab79Smaybee *offset = limit; 421cdb0ab79Smaybee else if (err) 422cdb0ab79Smaybee return (err); 423cdb0ab79Smaybee 424cdb0ab79Smaybee if (*offset < limit) 425cdb0ab79Smaybee *offset = limit; 426cdb0ab79Smaybee ASSERT3U(*offset, <, initial_offset); 427cdb0ab79Smaybee } 428cdb0ab79Smaybee return (0); 429cdb0ab79Smaybee } 430cdb0ab79Smaybee 431cdb0ab79Smaybee static int 432cdb0ab79Smaybee dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset, 433cdb0ab79Smaybee uint64_t length, boolean_t free_dnode) 434cdb0ab79Smaybee { 435cdb0ab79Smaybee dmu_tx_t *tx; 436cdb0ab79Smaybee uint64_t object_size, start, end, len; 437cdb0ab79Smaybee boolean_t trunc = (length == DMU_OBJECT_END); 438cdb0ab79Smaybee int align, err; 439cdb0ab79Smaybee 440cdb0ab79Smaybee align = 1 << dn->dn_datablkshift; 441cdb0ab79Smaybee ASSERT(align > 0); 442cdb0ab79Smaybee object_size = align == 1 ? dn->dn_datablksz : 443cdb0ab79Smaybee (dn->dn_maxblkid + 1) << dn->dn_datablkshift; 444cdb0ab79Smaybee 445cdb0ab79Smaybee if (trunc || (end = offset + length) > object_size) 446cdb0ab79Smaybee end = object_size; 447cdb0ab79Smaybee if (end <= offset) 448cdb0ab79Smaybee return (0); 449cdb0ab79Smaybee length = end - offset; 450cdb0ab79Smaybee 451cdb0ab79Smaybee while (length) { 452cdb0ab79Smaybee start = end; 453cdb0ab79Smaybee err = get_next_chunk(dn, &start, offset); 454cdb0ab79Smaybee if (err) 455cdb0ab79Smaybee return (err); 456cdb0ab79Smaybee len = trunc ? DMU_OBJECT_END : end - start; 457cdb0ab79Smaybee 458cdb0ab79Smaybee tx = dmu_tx_create(os); 459cdb0ab79Smaybee dmu_tx_hold_free(tx, dn->dn_object, start, len); 460cdb0ab79Smaybee err = dmu_tx_assign(tx, TXG_WAIT); 461cdb0ab79Smaybee if (err) { 462cdb0ab79Smaybee dmu_tx_abort(tx); 463cdb0ab79Smaybee return (err); 464cdb0ab79Smaybee } 465cdb0ab79Smaybee 466cdb0ab79Smaybee dnode_free_range(dn, start, trunc ? -1 : len, tx); 467cdb0ab79Smaybee 4681c8564a7SMark Maybee if (start == 0 && free_dnode) { 4691c8564a7SMark Maybee ASSERT(trunc); 470cdb0ab79Smaybee dnode_free(dn, tx); 4711c8564a7SMark Maybee } 472cdb0ab79Smaybee 473cdb0ab79Smaybee length -= end - start; 474cdb0ab79Smaybee 475cdb0ab79Smaybee dmu_tx_commit(tx); 476cdb0ab79Smaybee end = start; 477cdb0ab79Smaybee } 478cdb0ab79Smaybee return (0); 479cdb0ab79Smaybee } 480cdb0ab79Smaybee 481cdb0ab79Smaybee int 482cdb0ab79Smaybee dmu_free_long_range(objset_t *os, uint64_t object, 483cdb0ab79Smaybee uint64_t offset, uint64_t length) 484cdb0ab79Smaybee { 485cdb0ab79Smaybee dnode_t *dn; 486cdb0ab79Smaybee int err; 487cdb0ab79Smaybee 488cdb0ab79Smaybee err = dnode_hold(os->os, object, FTAG, &dn); 489cdb0ab79Smaybee if (err != 0) 490cdb0ab79Smaybee return (err); 491cdb0ab79Smaybee err = dmu_free_long_range_impl(os, dn, offset, length, FALSE); 492cdb0ab79Smaybee dnode_rele(dn, FTAG); 493cdb0ab79Smaybee return (err); 494cdb0ab79Smaybee } 495cdb0ab79Smaybee 496cdb0ab79Smaybee int 497cdb0ab79Smaybee dmu_free_object(objset_t *os, uint64_t object) 498cdb0ab79Smaybee { 499cdb0ab79Smaybee dnode_t *dn; 500cdb0ab79Smaybee dmu_tx_t *tx; 501cdb0ab79Smaybee int err; 502cdb0ab79Smaybee 503cdb0ab79Smaybee err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED, 504cdb0ab79Smaybee FTAG, &dn); 505cdb0ab79Smaybee if (err != 0) 506cdb0ab79Smaybee return (err); 507cdb0ab79Smaybee if (dn->dn_nlevels == 1) { 508cdb0ab79Smaybee tx = dmu_tx_create(os); 509cdb0ab79Smaybee dmu_tx_hold_bonus(tx, object); 510cdb0ab79Smaybee dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END); 511cdb0ab79Smaybee err = dmu_tx_assign(tx, TXG_WAIT); 512cdb0ab79Smaybee if (err == 0) { 513cdb0ab79Smaybee dnode_free_range(dn, 0, DMU_OBJECT_END, tx); 514cdb0ab79Smaybee dnode_free(dn, tx); 515cdb0ab79Smaybee dmu_tx_commit(tx); 516cdb0ab79Smaybee } else { 517cdb0ab79Smaybee dmu_tx_abort(tx); 518cdb0ab79Smaybee } 519cdb0ab79Smaybee } else { 520cdb0ab79Smaybee err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE); 521cdb0ab79Smaybee } 522cdb0ab79Smaybee dnode_rele(dn, FTAG); 523cdb0ab79Smaybee return (err); 524cdb0ab79Smaybee } 525cdb0ab79Smaybee 526ea8dc4b6Seschrock int 527fa9e4066Sahrens dmu_free_range(objset_t *os, uint64_t object, uint64_t offset, 528fa9e4066Sahrens uint64_t size, dmu_tx_t *tx) 529fa9e4066Sahrens { 530ea8dc4b6Seschrock dnode_t *dn; 531ea8dc4b6Seschrock int err = dnode_hold(os->os, object, FTAG, &dn); 532ea8dc4b6Seschrock if (err) 533ea8dc4b6Seschrock return (err); 534fa9e4066Sahrens ASSERT(offset < UINT64_MAX); 535fa9e4066Sahrens ASSERT(size == -1ULL || size <= UINT64_MAX - offset); 536fa9e4066Sahrens dnode_free_range(dn, offset, size, tx); 537fa9e4066Sahrens dnode_rele(dn, FTAG); 538ea8dc4b6Seschrock return (0); 539fa9e4066Sahrens } 540fa9e4066Sahrens 541ea8dc4b6Seschrock int 542ea8dc4b6Seschrock dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 543ea8dc4b6Seschrock void *buf) 544fa9e4066Sahrens { 545fa9e4066Sahrens dnode_t *dn; 546fa9e4066Sahrens dmu_buf_t **dbp; 547ea8dc4b6Seschrock int numbufs, i, err; 548fa9e4066Sahrens 549ea8dc4b6Seschrock err = dnode_hold(os->os, object, FTAG, &dn); 550ea8dc4b6Seschrock if (err) 551ea8dc4b6Seschrock return (err); 552feb08c6bSbillm 553feb08c6bSbillm /* 554feb08c6bSbillm * Deal with odd block sizes, where there can't be data past the first 555feb08c6bSbillm * block. If we ever do the tail block optimization, we will need to 556feb08c6bSbillm * handle that here as well. 557feb08c6bSbillm */ 558fa9e4066Sahrens if (dn->dn_datablkshift == 0) { 559fa9e4066Sahrens int newsz = offset > dn->dn_datablksz ? 0 : 560fa9e4066Sahrens MIN(size, dn->dn_datablksz - offset); 561fa9e4066Sahrens bzero((char *)buf + newsz, size - newsz); 562fa9e4066Sahrens size = newsz; 563fa9e4066Sahrens } 564fa9e4066Sahrens 565fa9e4066Sahrens while (size > 0) { 566fa9e4066Sahrens uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2); 567fa9e4066Sahrens 568fa9e4066Sahrens /* 569fa9e4066Sahrens * NB: we could do this block-at-a-time, but it's nice 570fa9e4066Sahrens * to be reading in parallel. 571fa9e4066Sahrens */ 572a2eea2e1Sahrens err = dmu_buf_hold_array_by_dnode(dn, offset, mylen, 573ea8dc4b6Seschrock TRUE, FTAG, &numbufs, &dbp); 574ea8dc4b6Seschrock if (err) 5751934e92fSmaybee break; 576fa9e4066Sahrens 577fa9e4066Sahrens for (i = 0; i < numbufs; i++) { 578fa9e4066Sahrens int tocpy; 579fa9e4066Sahrens int bufoff; 580fa9e4066Sahrens dmu_buf_t *db = dbp[i]; 581fa9e4066Sahrens 582fa9e4066Sahrens ASSERT(size > 0); 583fa9e4066Sahrens 584fa9e4066Sahrens bufoff = offset - db->db_offset; 585fa9e4066Sahrens tocpy = (int)MIN(db->db_size - bufoff, size); 586fa9e4066Sahrens 587fa9e4066Sahrens bcopy((char *)db->db_data + bufoff, buf, tocpy); 588fa9e4066Sahrens 589fa9e4066Sahrens offset += tocpy; 590fa9e4066Sahrens size -= tocpy; 591fa9e4066Sahrens buf = (char *)buf + tocpy; 592fa9e4066Sahrens } 593ea8dc4b6Seschrock dmu_buf_rele_array(dbp, numbufs, FTAG); 594fa9e4066Sahrens } 595a2eea2e1Sahrens dnode_rele(dn, FTAG); 5961934e92fSmaybee return (err); 597fa9e4066Sahrens } 598fa9e4066Sahrens 599fa9e4066Sahrens void 600fa9e4066Sahrens dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 601fa9e4066Sahrens const void *buf, dmu_tx_t *tx) 602fa9e4066Sahrens { 603fa9e4066Sahrens dmu_buf_t **dbp; 604fa9e4066Sahrens int numbufs, i; 605fa9e4066Sahrens 60613506d1eSmaybee if (size == 0) 60713506d1eSmaybee return; 60813506d1eSmaybee 609ea8dc4b6Seschrock VERIFY(0 == dmu_buf_hold_array(os, object, offset, size, 610ea8dc4b6Seschrock FALSE, FTAG, &numbufs, &dbp)); 611fa9e4066Sahrens 612fa9e4066Sahrens for (i = 0; i < numbufs; i++) { 613fa9e4066Sahrens int tocpy; 614fa9e4066Sahrens int bufoff; 615fa9e4066Sahrens dmu_buf_t *db = dbp[i]; 616fa9e4066Sahrens 617fa9e4066Sahrens ASSERT(size > 0); 618fa9e4066Sahrens 619fa9e4066Sahrens bufoff = offset - db->db_offset; 620fa9e4066Sahrens tocpy = (int)MIN(db->db_size - bufoff, size); 621fa9e4066Sahrens 622fa9e4066Sahrens ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 623fa9e4066Sahrens 624fa9e4066Sahrens if (tocpy == db->db_size) 625fa9e4066Sahrens dmu_buf_will_fill(db, tx); 626fa9e4066Sahrens else 627fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 628fa9e4066Sahrens 629fa9e4066Sahrens bcopy(buf, (char *)db->db_data + bufoff, tocpy); 630fa9e4066Sahrens 631fa9e4066Sahrens if (tocpy == db->db_size) 632fa9e4066Sahrens dmu_buf_fill_done(db, tx); 633fa9e4066Sahrens 634fa9e4066Sahrens offset += tocpy; 635fa9e4066Sahrens size -= tocpy; 636fa9e4066Sahrens buf = (char *)buf + tocpy; 637fa9e4066Sahrens } 638ea8dc4b6Seschrock dmu_buf_rele_array(dbp, numbufs, FTAG); 639fa9e4066Sahrens } 640fa9e4066Sahrens 641fa9e4066Sahrens #ifdef _KERNEL 642fa9e4066Sahrens int 643feb08c6bSbillm dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size) 644feb08c6bSbillm { 645feb08c6bSbillm dmu_buf_t **dbp; 646feb08c6bSbillm int numbufs, i, err; 647feb08c6bSbillm 648feb08c6bSbillm /* 649feb08c6bSbillm * NB: we could do this block-at-a-time, but it's nice 650feb08c6bSbillm * to be reading in parallel. 651feb08c6bSbillm */ 652feb08c6bSbillm err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG, 653feb08c6bSbillm &numbufs, &dbp); 654feb08c6bSbillm if (err) 655feb08c6bSbillm return (err); 656feb08c6bSbillm 657feb08c6bSbillm for (i = 0; i < numbufs; i++) { 658feb08c6bSbillm int tocpy; 659feb08c6bSbillm int bufoff; 660feb08c6bSbillm dmu_buf_t *db = dbp[i]; 661feb08c6bSbillm 662feb08c6bSbillm ASSERT(size > 0); 663feb08c6bSbillm 664feb08c6bSbillm bufoff = uio->uio_loffset - db->db_offset; 665feb08c6bSbillm tocpy = (int)MIN(db->db_size - bufoff, size); 666feb08c6bSbillm 667feb08c6bSbillm err = uiomove((char *)db->db_data + bufoff, tocpy, 668feb08c6bSbillm UIO_READ, uio); 669feb08c6bSbillm if (err) 670feb08c6bSbillm break; 671feb08c6bSbillm 672feb08c6bSbillm size -= tocpy; 673feb08c6bSbillm } 674feb08c6bSbillm dmu_buf_rele_array(dbp, numbufs, FTAG); 675feb08c6bSbillm 676feb08c6bSbillm return (err); 677feb08c6bSbillm } 678feb08c6bSbillm 679feb08c6bSbillm int 680feb08c6bSbillm dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size, 681feb08c6bSbillm dmu_tx_t *tx) 682fa9e4066Sahrens { 683fa9e4066Sahrens dmu_buf_t **dbp; 684fa9e4066Sahrens int numbufs, i; 685fa9e4066Sahrens int err = 0; 686fa9e4066Sahrens 68713506d1eSmaybee if (size == 0) 68813506d1eSmaybee return (0); 68913506d1eSmaybee 690feb08c6bSbillm err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, 691ea8dc4b6Seschrock FALSE, FTAG, &numbufs, &dbp); 692ea8dc4b6Seschrock if (err) 693ea8dc4b6Seschrock return (err); 694fa9e4066Sahrens 695fa9e4066Sahrens for (i = 0; i < numbufs; i++) { 696fa9e4066Sahrens int tocpy; 697fa9e4066Sahrens int bufoff; 698fa9e4066Sahrens dmu_buf_t *db = dbp[i]; 699fa9e4066Sahrens 700fa9e4066Sahrens ASSERT(size > 0); 701fa9e4066Sahrens 702feb08c6bSbillm bufoff = uio->uio_loffset - db->db_offset; 703fa9e4066Sahrens tocpy = (int)MIN(db->db_size - bufoff, size); 704fa9e4066Sahrens 705fa9e4066Sahrens ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 706fa9e4066Sahrens 707fa9e4066Sahrens if (tocpy == db->db_size) 708fa9e4066Sahrens dmu_buf_will_fill(db, tx); 709fa9e4066Sahrens else 710fa9e4066Sahrens dmu_buf_will_dirty(db, tx); 711fa9e4066Sahrens 712fa9e4066Sahrens /* 713fa9e4066Sahrens * XXX uiomove could block forever (eg. nfs-backed 714fa9e4066Sahrens * pages). There needs to be a uiolockdown() function 715fa9e4066Sahrens * to lock the pages in memory, so that uiomove won't 716fa9e4066Sahrens * block. 717fa9e4066Sahrens */ 718fa9e4066Sahrens err = uiomove((char *)db->db_data + bufoff, tocpy, 719fa9e4066Sahrens UIO_WRITE, uio); 720fa9e4066Sahrens 721fa9e4066Sahrens if (tocpy == db->db_size) 722fa9e4066Sahrens dmu_buf_fill_done(db, tx); 723fa9e4066Sahrens 724fa9e4066Sahrens if (err) 725fa9e4066Sahrens break; 726fa9e4066Sahrens 727fa9e4066Sahrens size -= tocpy; 728fa9e4066Sahrens } 729ea8dc4b6Seschrock dmu_buf_rele_array(dbp, numbufs, FTAG); 730fa9e4066Sahrens return (err); 731fa9e4066Sahrens } 73244eda4d7Smaybee 73344eda4d7Smaybee int 73444eda4d7Smaybee dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 73544eda4d7Smaybee page_t *pp, dmu_tx_t *tx) 73644eda4d7Smaybee { 73744eda4d7Smaybee dmu_buf_t **dbp; 73844eda4d7Smaybee int numbufs, i; 73944eda4d7Smaybee int err; 74044eda4d7Smaybee 74144eda4d7Smaybee if (size == 0) 74244eda4d7Smaybee return (0); 74344eda4d7Smaybee 74444eda4d7Smaybee err = dmu_buf_hold_array(os, object, offset, size, 74544eda4d7Smaybee FALSE, FTAG, &numbufs, &dbp); 74644eda4d7Smaybee if (err) 74744eda4d7Smaybee return (err); 74844eda4d7Smaybee 74944eda4d7Smaybee for (i = 0; i < numbufs; i++) { 75044eda4d7Smaybee int tocpy, copied, thiscpy; 75144eda4d7Smaybee int bufoff; 75244eda4d7Smaybee dmu_buf_t *db = dbp[i]; 75344eda4d7Smaybee caddr_t va; 75444eda4d7Smaybee 75544eda4d7Smaybee ASSERT(size > 0); 75644eda4d7Smaybee ASSERT3U(db->db_size, >=, PAGESIZE); 75744eda4d7Smaybee 75844eda4d7Smaybee bufoff = offset - db->db_offset; 75944eda4d7Smaybee tocpy = (int)MIN(db->db_size - bufoff, size); 76044eda4d7Smaybee 76144eda4d7Smaybee ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 76244eda4d7Smaybee 76344eda4d7Smaybee if (tocpy == db->db_size) 76444eda4d7Smaybee dmu_buf_will_fill(db, tx); 76544eda4d7Smaybee else 76644eda4d7Smaybee dmu_buf_will_dirty(db, tx); 76744eda4d7Smaybee 76844eda4d7Smaybee for (copied = 0; copied < tocpy; copied += PAGESIZE) { 76944eda4d7Smaybee ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff); 77044eda4d7Smaybee thiscpy = MIN(PAGESIZE, tocpy - copied); 7710fab61baSJonathan W Adams va = zfs_map_page(pp, S_READ); 77244eda4d7Smaybee bcopy(va, (char *)db->db_data + bufoff, thiscpy); 7730fab61baSJonathan W Adams zfs_unmap_page(pp, va); 77444eda4d7Smaybee pp = pp->p_next; 77544eda4d7Smaybee bufoff += PAGESIZE; 77644eda4d7Smaybee } 77744eda4d7Smaybee 77844eda4d7Smaybee if (tocpy == db->db_size) 77944eda4d7Smaybee dmu_buf_fill_done(db, tx); 78044eda4d7Smaybee 78144eda4d7Smaybee if (err) 78244eda4d7Smaybee break; 78344eda4d7Smaybee 78444eda4d7Smaybee offset += tocpy; 78544eda4d7Smaybee size -= tocpy; 78644eda4d7Smaybee } 78744eda4d7Smaybee dmu_buf_rele_array(dbp, numbufs, FTAG); 78844eda4d7Smaybee return (err); 78944eda4d7Smaybee } 790fa9e4066Sahrens #endif 791fa9e4066Sahrens 792c5c6ffa0Smaybee typedef struct { 793c717a561Smaybee dbuf_dirty_record_t *dr; 794c5c6ffa0Smaybee dmu_sync_cb_t *done; 795c5c6ffa0Smaybee void *arg; 796c717a561Smaybee } dmu_sync_arg_t; 797c5c6ffa0Smaybee 798c5c6ffa0Smaybee /* ARGSUSED */ 799c5c6ffa0Smaybee static void 800*e14bb325SJeff Bonwick dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg) 801*e14bb325SJeff Bonwick { 802*e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 803*e14bb325SJeff Bonwick 804*e14bb325SJeff Bonwick if (!BP_IS_HOLE(bp)) { 805*e14bb325SJeff Bonwick dmu_sync_arg_t *in = varg; 806*e14bb325SJeff Bonwick dbuf_dirty_record_t *dr = in->dr; 807*e14bb325SJeff Bonwick dmu_buf_impl_t *db = dr->dr_dbuf; 808*e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(bp) == db->db_dnode->dn_type); 809*e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(bp) == 0); 810*e14bb325SJeff Bonwick bp->blk_fill = 1; 811*e14bb325SJeff Bonwick } 812*e14bb325SJeff Bonwick } 813*e14bb325SJeff Bonwick 814*e14bb325SJeff Bonwick /* ARGSUSED */ 815*e14bb325SJeff Bonwick static void 816c5c6ffa0Smaybee dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg) 817c5c6ffa0Smaybee { 818c717a561Smaybee dmu_sync_arg_t *in = varg; 819c717a561Smaybee dbuf_dirty_record_t *dr = in->dr; 820c717a561Smaybee dmu_buf_impl_t *db = dr->dr_dbuf; 821c5c6ffa0Smaybee dmu_sync_cb_t *done = in->done; 822c5c6ffa0Smaybee 823c5c6ffa0Smaybee mutex_enter(&db->db_mtx); 824c717a561Smaybee ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC); 825c717a561Smaybee dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */ 826c717a561Smaybee dr->dt.dl.dr_override_state = DR_OVERRIDDEN; 827c5c6ffa0Smaybee cv_broadcast(&db->db_changed); 828c5c6ffa0Smaybee mutex_exit(&db->db_mtx); 829c5c6ffa0Smaybee 830c5c6ffa0Smaybee if (done) 831c717a561Smaybee done(&(db->db), in->arg); 832c717a561Smaybee 833c717a561Smaybee kmem_free(in, sizeof (dmu_sync_arg_t)); 834c5c6ffa0Smaybee } 835c5c6ffa0Smaybee 836fa9e4066Sahrens /* 837c5c6ffa0Smaybee * Intent log support: sync the block associated with db to disk. 838c5c6ffa0Smaybee * N.B. and XXX: the caller is responsible for making sure that the 839c5c6ffa0Smaybee * data isn't changing while dmu_sync() is writing it. 840fa9e4066Sahrens * 841fa9e4066Sahrens * Return values: 842fa9e4066Sahrens * 843c5c6ffa0Smaybee * EEXIST: this txg has already been synced, so there's nothing to to. 844fa9e4066Sahrens * The caller should not log the write. 845fa9e4066Sahrens * 846fa9e4066Sahrens * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do. 847fa9e4066Sahrens * The caller should not log the write. 848fa9e4066Sahrens * 849c5c6ffa0Smaybee * EALREADY: this block is already in the process of being synced. 850c5c6ffa0Smaybee * The caller should track its progress (somehow). 851fa9e4066Sahrens * 852c5c6ffa0Smaybee * EINPROGRESS: the IO has been initiated. 853c5c6ffa0Smaybee * The caller should log this blkptr in the callback. 854fa9e4066Sahrens * 855c5c6ffa0Smaybee * 0: completed. Sets *bp to the blkptr just written. 856c5c6ffa0Smaybee * The caller should log this blkptr immediately. 857fa9e4066Sahrens */ 858fa9e4066Sahrens int 859c5c6ffa0Smaybee dmu_sync(zio_t *pio, dmu_buf_t *db_fake, 860c5c6ffa0Smaybee blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg) 861fa9e4066Sahrens { 862c5c6ffa0Smaybee dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 863c5c6ffa0Smaybee objset_impl_t *os = db->db_objset; 864c5c6ffa0Smaybee dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; 865fa9e4066Sahrens tx_state_t *tx = &dp->dp_tx; 866c717a561Smaybee dbuf_dirty_record_t *dr; 867c717a561Smaybee dmu_sync_arg_t *in; 868ea8dc4b6Seschrock zbookmark_t zb; 869088f3894Sahrens writeprops_t wp = { 0 }; 870c717a561Smaybee zio_t *zio; 871c5c6ffa0Smaybee int err; 872fa9e4066Sahrens 873fa9e4066Sahrens ASSERT(BP_IS_HOLE(bp)); 874fa9e4066Sahrens ASSERT(txg != 0); 875fa9e4066Sahrens 876fa9e4066Sahrens dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n", 877fa9e4066Sahrens txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg); 878fa9e4066Sahrens 879fa9e4066Sahrens /* 880c5c6ffa0Smaybee * XXX - would be nice if we could do this without suspending... 881ea8dc4b6Seschrock */ 882c5c6ffa0Smaybee txg_suspend(dp); 883ea8dc4b6Seschrock 884ea8dc4b6Seschrock /* 885fa9e4066Sahrens * If this txg already synced, there's nothing to do. 886fa9e4066Sahrens */ 887fa9e4066Sahrens if (txg <= tx->tx_synced_txg) { 888c5c6ffa0Smaybee txg_resume(dp); 889fa9e4066Sahrens /* 890fa9e4066Sahrens * If we're running ziltest, we need the blkptr regardless. 891fa9e4066Sahrens */ 892fa9e4066Sahrens if (txg > spa_freeze_txg(dp->dp_spa)) { 893fa9e4066Sahrens /* if db_blkptr == NULL, this was an empty write */ 894fa9e4066Sahrens if (db->db_blkptr) 895fa9e4066Sahrens *bp = *db->db_blkptr; /* structure assignment */ 896fa9e4066Sahrens return (0); 897fa9e4066Sahrens } 898c5c6ffa0Smaybee return (EEXIST); 899fa9e4066Sahrens } 900fa9e4066Sahrens 901fa9e4066Sahrens mutex_enter(&db->db_mtx); 902fa9e4066Sahrens 903c5c6ffa0Smaybee if (txg == tx->tx_syncing_txg) { 904c5c6ffa0Smaybee while (db->db_data_pending) { 905c5c6ffa0Smaybee /* 906c5c6ffa0Smaybee * IO is in-progress. Wait for it to finish. 907c5c6ffa0Smaybee * XXX - would be nice to be able to somehow "attach" 908c5c6ffa0Smaybee * this zio to the parent zio passed in. 909c5c6ffa0Smaybee */ 910c5c6ffa0Smaybee cv_wait(&db->db_changed, &db->db_mtx); 91113506d1eSmaybee if (!db->db_data_pending && 91213506d1eSmaybee db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) { 91313506d1eSmaybee /* 91413506d1eSmaybee * IO was compressed away 91513506d1eSmaybee */ 91613506d1eSmaybee *bp = *db->db_blkptr; /* structure assignment */ 91713506d1eSmaybee mutex_exit(&db->db_mtx); 91813506d1eSmaybee txg_resume(dp); 91913506d1eSmaybee return (0); 92013506d1eSmaybee } 921c5c6ffa0Smaybee ASSERT(db->db_data_pending || 922c5c6ffa0Smaybee (db->db_blkptr && db->db_blkptr->blk_birth == txg)); 923c5c6ffa0Smaybee } 924c5c6ffa0Smaybee 925c5c6ffa0Smaybee if (db->db_blkptr && db->db_blkptr->blk_birth == txg) { 926c5c6ffa0Smaybee /* 927c5c6ffa0Smaybee * IO is already completed. 928c5c6ffa0Smaybee */ 929c5c6ffa0Smaybee *bp = *db->db_blkptr; /* structure assignment */ 930c5c6ffa0Smaybee mutex_exit(&db->db_mtx); 931c5c6ffa0Smaybee txg_resume(dp); 932c5c6ffa0Smaybee return (0); 933c5c6ffa0Smaybee } 934c5c6ffa0Smaybee } 935c5c6ffa0Smaybee 936c717a561Smaybee dr = db->db_last_dirty; 937c717a561Smaybee while (dr && dr->dr_txg > txg) 938c717a561Smaybee dr = dr->dr_next; 939c717a561Smaybee if (dr == NULL || dr->dr_txg < txg) { 940c5c6ffa0Smaybee /* 941c5c6ffa0Smaybee * This dbuf isn't dirty, must have been free_range'd. 942fa9e4066Sahrens * There's no need to log writes to freed blocks, so we're done. 943fa9e4066Sahrens */ 944fa9e4066Sahrens mutex_exit(&db->db_mtx); 945c5c6ffa0Smaybee txg_resume(dp); 946fa9e4066Sahrens return (ENOENT); 947fa9e4066Sahrens } 948fa9e4066Sahrens 949c717a561Smaybee ASSERT(dr->dr_txg == txg); 950c717a561Smaybee if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) { 951c5c6ffa0Smaybee /* 952c717a561Smaybee * We have already issued a sync write for this buffer. 953c5c6ffa0Smaybee */ 954c717a561Smaybee mutex_exit(&db->db_mtx); 955c717a561Smaybee txg_resume(dp); 956c717a561Smaybee return (EALREADY); 957c717a561Smaybee } else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 958c717a561Smaybee /* 959c717a561Smaybee * This buffer has already been synced. It could not 960c717a561Smaybee * have been dirtied since, or we would have cleared the state. 961c717a561Smaybee */ 962c717a561Smaybee *bp = dr->dt.dl.dr_overridden_by; /* structure assignment */ 963c717a561Smaybee mutex_exit(&db->db_mtx); 964c717a561Smaybee txg_resume(dp); 965c717a561Smaybee return (0); 966c717a561Smaybee } 967c717a561Smaybee 968c717a561Smaybee dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC; 969c717a561Smaybee in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP); 970c717a561Smaybee in->dr = dr; 971c5c6ffa0Smaybee in->done = done; 972c5c6ffa0Smaybee in->arg = arg; 973fa9e4066Sahrens mutex_exit(&db->db_mtx); 974c5c6ffa0Smaybee txg_resume(dp); 975fa9e4066Sahrens 976c5c6ffa0Smaybee zb.zb_objset = os->os_dsl_dataset->ds_object; 977ea8dc4b6Seschrock zb.zb_object = db->db.db_object; 978ea8dc4b6Seschrock zb.zb_level = db->db_level; 979ea8dc4b6Seschrock zb.zb_blkid = db->db_blkid; 980*e14bb325SJeff Bonwick 981088f3894Sahrens wp.wp_type = db->db_dnode->dn_type; 982088f3894Sahrens wp.wp_level = db->db_level; 983*e14bb325SJeff Bonwick wp.wp_copies = os->os_copies; 984088f3894Sahrens wp.wp_dnchecksum = db->db_dnode->dn_checksum; 985088f3894Sahrens wp.wp_oschecksum = os->os_checksum; 986088f3894Sahrens wp.wp_dncompress = db->db_dnode->dn_compress; 987088f3894Sahrens wp.wp_oscompress = os->os_compress; 988*e14bb325SJeff Bonwick 989*e14bb325SJeff Bonwick ASSERT(BP_IS_HOLE(bp)); 990*e14bb325SJeff Bonwick 991*e14bb325SJeff Bonwick zio = arc_write(pio, os->os_spa, &wp, DBUF_IS_L2CACHEABLE(db), 992*e14bb325SJeff Bonwick txg, bp, dr->dt.dl.dr_data, dmu_sync_ready, dmu_sync_done, in, 993*e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 994fa9e4066Sahrens 995c717a561Smaybee if (pio) { 996c717a561Smaybee zio_nowait(zio); 997c717a561Smaybee err = EINPROGRESS; 998c717a561Smaybee } else { 999c717a561Smaybee err = zio_wait(zio); 1000c717a561Smaybee ASSERT(err == 0); 1001c717a561Smaybee } 1002c717a561Smaybee return (err); 1003fa9e4066Sahrens } 1004fa9e4066Sahrens 1005fa9e4066Sahrens int 1006fa9e4066Sahrens dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs, 1007fa9e4066Sahrens dmu_tx_t *tx) 1008fa9e4066Sahrens { 1009ea8dc4b6Seschrock dnode_t *dn; 1010ea8dc4b6Seschrock int err; 1011ea8dc4b6Seschrock 1012ea8dc4b6Seschrock err = dnode_hold(os->os, object, FTAG, &dn); 1013ea8dc4b6Seschrock if (err) 1014ea8dc4b6Seschrock return (err); 1015ea8dc4b6Seschrock err = dnode_set_blksz(dn, size, ibs, tx); 1016fa9e4066Sahrens dnode_rele(dn, FTAG); 1017fa9e4066Sahrens return (err); 1018fa9e4066Sahrens } 1019fa9e4066Sahrens 1020fa9e4066Sahrens void 1021fa9e4066Sahrens dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum, 1022fa9e4066Sahrens dmu_tx_t *tx) 1023fa9e4066Sahrens { 1024ea8dc4b6Seschrock dnode_t *dn; 1025ea8dc4b6Seschrock 1026ea8dc4b6Seschrock /* XXX assumes dnode_hold will not get an i/o error */ 1027ea8dc4b6Seschrock (void) dnode_hold(os->os, object, FTAG, &dn); 1028fa9e4066Sahrens ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS); 1029fa9e4066Sahrens dn->dn_checksum = checksum; 1030fa9e4066Sahrens dnode_setdirty(dn, tx); 1031fa9e4066Sahrens dnode_rele(dn, FTAG); 1032fa9e4066Sahrens } 1033fa9e4066Sahrens 1034fa9e4066Sahrens void 1035fa9e4066Sahrens dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress, 1036fa9e4066Sahrens dmu_tx_t *tx) 1037fa9e4066Sahrens { 1038ea8dc4b6Seschrock dnode_t *dn; 1039ea8dc4b6Seschrock 1040ea8dc4b6Seschrock /* XXX assumes dnode_hold will not get an i/o error */ 1041ea8dc4b6Seschrock (void) dnode_hold(os->os, object, FTAG, &dn); 1042fa9e4066Sahrens ASSERT(compress < ZIO_COMPRESS_FUNCTIONS); 1043fa9e4066Sahrens dn->dn_compress = compress; 1044fa9e4066Sahrens dnode_setdirty(dn, tx); 1045fa9e4066Sahrens dnode_rele(dn, FTAG); 1046fa9e4066Sahrens } 1047fa9e4066Sahrens 104844cd46caSbillm int 1049fa9e4066Sahrens dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off) 1050fa9e4066Sahrens { 1051fa9e4066Sahrens dnode_t *dn; 1052fa9e4066Sahrens int i, err; 1053fa9e4066Sahrens 1054ea8dc4b6Seschrock err = dnode_hold(os->os, object, FTAG, &dn); 1055ea8dc4b6Seschrock if (err) 1056ea8dc4b6Seschrock return (err); 1057fa9e4066Sahrens /* 1058fa9e4066Sahrens * Sync any current changes before 1059fa9e4066Sahrens * we go trundling through the block pointers. 1060fa9e4066Sahrens */ 1061fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 1062c543ec06Sahrens if (list_link_active(&dn->dn_dirty_link[i])) 1063fa9e4066Sahrens break; 1064fa9e4066Sahrens } 1065fa9e4066Sahrens if (i != TXG_SIZE) { 1066fa9e4066Sahrens dnode_rele(dn, FTAG); 1067fa9e4066Sahrens txg_wait_synced(dmu_objset_pool(os), 0); 1068ea8dc4b6Seschrock err = dnode_hold(os->os, object, FTAG, &dn); 1069ea8dc4b6Seschrock if (err) 1070ea8dc4b6Seschrock return (err); 1071fa9e4066Sahrens } 1072fa9e4066Sahrens 1073cdb0ab79Smaybee err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0); 1074fa9e4066Sahrens dnode_rele(dn, FTAG); 1075fa9e4066Sahrens 1076fa9e4066Sahrens return (err); 1077fa9e4066Sahrens } 1078fa9e4066Sahrens 1079fa9e4066Sahrens void 1080fa9e4066Sahrens dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi) 1081fa9e4066Sahrens { 1082fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 1083fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 1084fa9e4066Sahrens 1085fa9e4066Sahrens doi->doi_data_block_size = dn->dn_datablksz; 1086fa9e4066Sahrens doi->doi_metadata_block_size = dn->dn_indblkshift ? 1087fa9e4066Sahrens 1ULL << dn->dn_indblkshift : 0; 1088fa9e4066Sahrens doi->doi_indirection = dn->dn_nlevels; 1089fa9e4066Sahrens doi->doi_checksum = dn->dn_checksum; 1090fa9e4066Sahrens doi->doi_compress = dn->dn_compress; 109199653d4eSeschrock doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) + 109299653d4eSeschrock SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT; 1093fa9e4066Sahrens doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid; 1094fa9e4066Sahrens doi->doi_type = dn->dn_type; 1095fa9e4066Sahrens doi->doi_bonus_size = dn->dn_bonuslen; 1096fa9e4066Sahrens doi->doi_bonus_type = dn->dn_bonustype; 1097fa9e4066Sahrens 1098fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1099fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 1100fa9e4066Sahrens } 1101fa9e4066Sahrens 1102fa9e4066Sahrens /* 1103fa9e4066Sahrens * Get information on a DMU object. 1104fa9e4066Sahrens * If doi is NULL, just indicates whether the object exists. 1105fa9e4066Sahrens */ 1106fa9e4066Sahrens int 1107fa9e4066Sahrens dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi) 1108fa9e4066Sahrens { 1109ea8dc4b6Seschrock dnode_t *dn; 1110ea8dc4b6Seschrock int err = dnode_hold(os->os, object, FTAG, &dn); 1111fa9e4066Sahrens 1112ea8dc4b6Seschrock if (err) 1113ea8dc4b6Seschrock return (err); 1114fa9e4066Sahrens 1115fa9e4066Sahrens if (doi != NULL) 1116fa9e4066Sahrens dmu_object_info_from_dnode(dn, doi); 1117fa9e4066Sahrens 1118fa9e4066Sahrens dnode_rele(dn, FTAG); 1119fa9e4066Sahrens return (0); 1120fa9e4066Sahrens } 1121fa9e4066Sahrens 1122fa9e4066Sahrens /* 1123fa9e4066Sahrens * As above, but faster; can be used when you have a held dbuf in hand. 1124fa9e4066Sahrens */ 1125fa9e4066Sahrens void 1126fa9e4066Sahrens dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi) 1127fa9e4066Sahrens { 1128fa9e4066Sahrens dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi); 1129fa9e4066Sahrens } 1130fa9e4066Sahrens 1131fa9e4066Sahrens /* 1132fa9e4066Sahrens * Faster still when you only care about the size. 1133fa9e4066Sahrens * This is specifically optimized for zfs_getattr(). 1134fa9e4066Sahrens */ 1135fa9e4066Sahrens void 1136fa9e4066Sahrens dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512) 1137fa9e4066Sahrens { 1138fa9e4066Sahrens dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode; 1139fa9e4066Sahrens 1140fa9e4066Sahrens *blksize = dn->dn_datablksz; 114199653d4eSeschrock /* add 1 for dnode space */ 114299653d4eSeschrock *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >> 114399653d4eSeschrock SPA_MINBLOCKSHIFT) + 1; 1144fa9e4066Sahrens } 1145fa9e4066Sahrens 1146fa9e4066Sahrens void 1147fa9e4066Sahrens byteswap_uint64_array(void *vbuf, size_t size) 1148fa9e4066Sahrens { 1149fa9e4066Sahrens uint64_t *buf = vbuf; 1150fa9e4066Sahrens size_t count = size >> 3; 1151fa9e4066Sahrens int i; 1152fa9e4066Sahrens 1153fa9e4066Sahrens ASSERT((size & 7) == 0); 1154fa9e4066Sahrens 1155fa9e4066Sahrens for (i = 0; i < count; i++) 1156fa9e4066Sahrens buf[i] = BSWAP_64(buf[i]); 1157fa9e4066Sahrens } 1158fa9e4066Sahrens 1159fa9e4066Sahrens void 1160fa9e4066Sahrens byteswap_uint32_array(void *vbuf, size_t size) 1161fa9e4066Sahrens { 1162fa9e4066Sahrens uint32_t *buf = vbuf; 1163fa9e4066Sahrens size_t count = size >> 2; 1164fa9e4066Sahrens int i; 1165fa9e4066Sahrens 1166fa9e4066Sahrens ASSERT((size & 3) == 0); 1167fa9e4066Sahrens 1168fa9e4066Sahrens for (i = 0; i < count; i++) 1169fa9e4066Sahrens buf[i] = BSWAP_32(buf[i]); 1170fa9e4066Sahrens } 1171fa9e4066Sahrens 1172fa9e4066Sahrens void 1173fa9e4066Sahrens byteswap_uint16_array(void *vbuf, size_t size) 1174fa9e4066Sahrens { 1175fa9e4066Sahrens uint16_t *buf = vbuf; 1176fa9e4066Sahrens size_t count = size >> 1; 1177fa9e4066Sahrens int i; 1178fa9e4066Sahrens 1179fa9e4066Sahrens ASSERT((size & 1) == 0); 1180fa9e4066Sahrens 1181fa9e4066Sahrens for (i = 0; i < count; i++) 1182fa9e4066Sahrens buf[i] = BSWAP_16(buf[i]); 1183fa9e4066Sahrens } 1184fa9e4066Sahrens 1185fa9e4066Sahrens /* ARGSUSED */ 1186fa9e4066Sahrens void 1187fa9e4066Sahrens byteswap_uint8_array(void *vbuf, size_t size) 1188fa9e4066Sahrens { 1189fa9e4066Sahrens } 1190fa9e4066Sahrens 1191fa9e4066Sahrens void 1192fa9e4066Sahrens dmu_init(void) 1193fa9e4066Sahrens { 1194fa9e4066Sahrens dbuf_init(); 1195fa9e4066Sahrens dnode_init(); 1196fa9e4066Sahrens arc_init(); 1197fa94a07fSbrendan l2arc_init(); 1198fa9e4066Sahrens } 1199fa9e4066Sahrens 1200fa9e4066Sahrens void 1201fa9e4066Sahrens dmu_fini(void) 1202fa9e4066Sahrens { 1203fa9e4066Sahrens arc_fini(); 1204fa9e4066Sahrens dnode_fini(); 1205fa9e4066Sahrens dbuf_fini(); 1206fa94a07fSbrendan l2arc_fini(); 1207fa9e4066Sahrens } 1208