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 */ 211c17160aSKevin Crowe 22fa9e4066Sahrens /* 2306e0070dSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2494c2d0ebSMatthew Ahrens * Copyright (c) 2012, 2017 by Delphix. All rights reserved. 25aad02571SSaso Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 26*f67950b2SNasf-Fan * Copyright 2019 Joyent, Inc. 27bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 2812380e1eSArne Jansen * Copyright (c) 2015, STRATO AG, Inc. All rights reserved. 29c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 301c17160aSKevin Crowe * Copyright 2017 Nexenta Systems, Inc. 31fa9e4066Sahrens */ 32fa9e4066Sahrens 3355da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */ 3455da60b9SMark J Musante 35*f67950b2SNasf-Fan #include <sys/zfeature.h> 36ecd6cf80Smarks #include <sys/cred.h> 37fa9e4066Sahrens #include <sys/zfs_context.h> 38fa9e4066Sahrens #include <sys/dmu_objset.h> 39fa9e4066Sahrens #include <sys/dsl_dir.h> 40fa9e4066Sahrens #include <sys/dsl_dataset.h> 41fa9e4066Sahrens #include <sys/dsl_prop.h> 42fa9e4066Sahrens #include <sys/dsl_pool.h> 431d452cf5Sahrens #include <sys/dsl_synctask.h> 44ecd6cf80Smarks #include <sys/dsl_deleg.h> 45fa9e4066Sahrens #include <sys/dnode.h> 46fa9e4066Sahrens #include <sys/dbuf.h> 47a2eea2e1Sahrens #include <sys/zvol.h> 48fa9e4066Sahrens #include <sys/dmu_tx.h> 49fa9e4066Sahrens #include <sys/zap.h> 50fa9e4066Sahrens #include <sys/zil.h> 51fa9e4066Sahrens #include <sys/dmu_impl.h> 52ecd6cf80Smarks #include <sys/zfs_ioctl.h> 530a586ceaSMark Shellenbaum #include <sys/sa.h> 5499d5e173STim Haley #include <sys/zfs_onexit.h> 553b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 5612380e1eSArne Jansen #include <sys/vdev.h> 575cabbc6bSPrashanth Sreenivasa #include <sys/zfeature.h> 58*f67950b2SNasf-Fan #include <sys/spa_impl.h> 59eb633035STom Caputi #include <sys/dmu_recv.h> 60*f67950b2SNasf-Fan #include <sys/zfs_project.h> 615ac95da7SSerapheim Dimitropoulos #include "zfs_namecheck.h" 62fa9e4066Sahrens 63744947dcSTom Erickson /* 64744947dcSTom Erickson * Needed to close a window in dnode_move() that allows the objset to be freed 65744947dcSTom Erickson * before it can be safely accessed. 66744947dcSTom Erickson */ 67744947dcSTom Erickson krwlock_t os_lock; 68744947dcSTom Erickson 6912380e1eSArne Jansen /* 7012380e1eSArne Jansen * Tunable to overwrite the maximum number of threads for the parallization 7112380e1eSArne Jansen * of dmu_objset_find_dp, needed to speed up the import of pools with many 7212380e1eSArne Jansen * datasets. 7312380e1eSArne Jansen * Default is 4 times the number of leaf vdevs. 7412380e1eSArne Jansen */ 7512380e1eSArne Jansen int dmu_find_threads = 0; 7612380e1eSArne Jansen 77af346df5SNed Bass /* 78af346df5SNed Bass * Backfill lower metadnode objects after this many have been freed. 79af346df5SNed Bass * Backfilling negatively impacts object creation rates, so only do it 80af346df5SNed Bass * if there are enough holes to fill. 81af346df5SNed Bass */ 82af346df5SNed Bass int dmu_rescan_dnode_threshold = 131072; 83af346df5SNed Bass 8412380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg); 8512380e1eSArne Jansen 86*f67950b2SNasf-Fan static void dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb); 87*f67950b2SNasf-Fan static void dmu_objset_upgrade_stop(objset_t *os); 88*f67950b2SNasf-Fan 89744947dcSTom Erickson void 90744947dcSTom Erickson dmu_objset_init(void) 91744947dcSTom Erickson { 92744947dcSTom Erickson rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 93744947dcSTom Erickson } 94744947dcSTom Erickson 95744947dcSTom Erickson void 96744947dcSTom Erickson dmu_objset_fini(void) 97744947dcSTom Erickson { 98744947dcSTom Erickson rw_destroy(&os_lock); 99744947dcSTom Erickson } 100744947dcSTom Erickson 101fa9e4066Sahrens spa_t * 102fa9e4066Sahrens dmu_objset_spa(objset_t *os) 103fa9e4066Sahrens { 104503ad85cSMatthew Ahrens return (os->os_spa); 105fa9e4066Sahrens } 106fa9e4066Sahrens 107fa9e4066Sahrens zilog_t * 108fa9e4066Sahrens dmu_objset_zil(objset_t *os) 109fa9e4066Sahrens { 110503ad85cSMatthew Ahrens return (os->os_zil); 111fa9e4066Sahrens } 112fa9e4066Sahrens 113fa9e4066Sahrens dsl_pool_t * 114fa9e4066Sahrens dmu_objset_pool(objset_t *os) 115fa9e4066Sahrens { 116fa9e4066Sahrens dsl_dataset_t *ds; 117fa9e4066Sahrens 118503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 119fa9e4066Sahrens return (ds->ds_dir->dd_pool); 120fa9e4066Sahrens else 121503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 122fa9e4066Sahrens } 123fa9e4066Sahrens 124fa9e4066Sahrens dsl_dataset_t * 125fa9e4066Sahrens dmu_objset_ds(objset_t *os) 126fa9e4066Sahrens { 127503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 128fa9e4066Sahrens } 129fa9e4066Sahrens 130fa9e4066Sahrens dmu_objset_type_t 131fa9e4066Sahrens dmu_objset_type(objset_t *os) 132fa9e4066Sahrens { 133503ad85cSMatthew Ahrens return (os->os_phys->os_type); 134fa9e4066Sahrens } 135fa9e4066Sahrens 136fa9e4066Sahrens void 137fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 138fa9e4066Sahrens { 139503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 140fa9e4066Sahrens } 141fa9e4066Sahrens 142fa9e4066Sahrens uint64_t 143fa9e4066Sahrens dmu_objset_id(objset_t *os) 144fa9e4066Sahrens { 145503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 146fa9e4066Sahrens 147fa9e4066Sahrens return (ds ? ds->ds_object : 0); 148fa9e4066Sahrens } 149fa9e4066Sahrens 15054811da5SToomas Soome uint64_t 15154811da5SToomas Soome dmu_objset_dnodesize(objset_t *os) 15254811da5SToomas Soome { 15354811da5SToomas Soome return (os->os_dnodesize); 15454811da5SToomas Soome } 15554811da5SToomas Soome 156edf345e6SMatthew Ahrens zfs_sync_type_t 15755da60b9SMark J Musante dmu_objset_syncprop(objset_t *os) 15855da60b9SMark J Musante { 15955da60b9SMark J Musante return (os->os_sync); 16055da60b9SMark J Musante } 16155da60b9SMark J Musante 162edf345e6SMatthew Ahrens zfs_logbias_op_t 163e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os) 164e09fa4daSNeil Perrin { 165e09fa4daSNeil Perrin return (os->os_logbias); 166e09fa4daSNeil Perrin } 167e09fa4daSNeil Perrin 168fa9e4066Sahrens static void 169fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 170fa9e4066Sahrens { 171503ad85cSMatthew Ahrens objset_t *os = arg; 172fa9e4066Sahrens 173fa9e4066Sahrens /* 174fa9e4066Sahrens * Inheritance should have been done by now. 175fa9e4066Sahrens */ 176fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 177fa9e4066Sahrens 178503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 179fa9e4066Sahrens } 180fa9e4066Sahrens 181fa9e4066Sahrens static void 182fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 183fa9e4066Sahrens { 184503ad85cSMatthew Ahrens objset_t *os = arg; 185fa9e4066Sahrens 186fa9e4066Sahrens /* 187fa9e4066Sahrens * Inheritance and range checking should have been done by now. 188fa9e4066Sahrens */ 189fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 190fa9e4066Sahrens 191db1741f5SJustin T. Gibbs os->os_compress = zio_compress_select(os->os_spa, newval, 192db1741f5SJustin T. Gibbs ZIO_COMPRESS_ON); 193fa9e4066Sahrens } 194fa9e4066Sahrens 195d0ad202dSahrens static void 196d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 197d0ad202dSahrens { 198503ad85cSMatthew Ahrens objset_t *os = arg; 199d0ad202dSahrens 200d0ad202dSahrens /* 201d0ad202dSahrens * Inheritance and range checking should have been done by now. 202d0ad202dSahrens */ 203d0ad202dSahrens ASSERT(newval > 0); 204503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 205d0ad202dSahrens 206503ad85cSMatthew Ahrens os->os_copies = newval; 207d0ad202dSahrens } 208d0ad202dSahrens 209b24ab676SJeff Bonwick static void 210b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval) 211b24ab676SJeff Bonwick { 212b24ab676SJeff Bonwick objset_t *os = arg; 213b24ab676SJeff Bonwick spa_t *spa = os->os_spa; 214b24ab676SJeff Bonwick enum zio_checksum checksum; 215b24ab676SJeff Bonwick 216b24ab676SJeff Bonwick /* 217b24ab676SJeff Bonwick * Inheritance should have been done by now. 218b24ab676SJeff Bonwick */ 219b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT); 220b24ab676SJeff Bonwick 221b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 222b24ab676SJeff Bonwick 223b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 224b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 225b24ab676SJeff Bonwick } 226b24ab676SJeff Bonwick 2273baa08fcSek static void 2283baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 2293baa08fcSek { 230503ad85cSMatthew Ahrens objset_t *os = arg; 2313baa08fcSek 2323baa08fcSek /* 2333baa08fcSek * Inheritance and range checking should have been done by now. 2343baa08fcSek */ 2353baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 2363baa08fcSek newval == ZFS_CACHE_METADATA); 2373baa08fcSek 238503ad85cSMatthew Ahrens os->os_primary_cache = newval; 2393baa08fcSek } 2403baa08fcSek 2413baa08fcSek static void 2423baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 2433baa08fcSek { 244503ad85cSMatthew Ahrens objset_t *os = arg; 2453baa08fcSek 2463baa08fcSek /* 2473baa08fcSek * Inheritance and range checking should have been done by now. 2483baa08fcSek */ 2493baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 2503baa08fcSek newval == ZFS_CACHE_METADATA); 2513baa08fcSek 252503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 2533baa08fcSek } 2543baa08fcSek 25555da60b9SMark J Musante static void 25655da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval) 25755da60b9SMark J Musante { 25855da60b9SMark J Musante objset_t *os = arg; 25955da60b9SMark J Musante 26055da60b9SMark J Musante /* 26155da60b9SMark J Musante * Inheritance and range checking should have been done by now. 26255da60b9SMark J Musante */ 26355da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 26455da60b9SMark J Musante newval == ZFS_SYNC_DISABLED); 26555da60b9SMark J Musante 26655da60b9SMark J Musante os->os_sync = newval; 26755da60b9SMark J Musante if (os->os_zil) 26855da60b9SMark J Musante zil_set_sync(os->os_zil, newval); 26955da60b9SMark J Musante } 27055da60b9SMark J Musante 271edf345e6SMatthew Ahrens static void 272edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval) 273edf345e6SMatthew Ahrens { 274edf345e6SMatthew Ahrens objset_t *os = arg; 275edf345e6SMatthew Ahrens 276edf345e6SMatthew Ahrens /* 277edf345e6SMatthew Ahrens * Inheritance and range checking should have been done by now. 278edf345e6SMatthew Ahrens */ 279edf345e6SMatthew Ahrens ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL || 280edf345e6SMatthew Ahrens newval == ZFS_REDUNDANT_METADATA_MOST); 281edf345e6SMatthew Ahrens 282edf345e6SMatthew Ahrens os->os_redundant_metadata = newval; 283edf345e6SMatthew Ahrens } 284edf345e6SMatthew Ahrens 28554811da5SToomas Soome static void 28654811da5SToomas Soome dnodesize_changed_cb(void *arg, uint64_t newval) 28754811da5SToomas Soome { 28854811da5SToomas Soome objset_t *os = arg; 28954811da5SToomas Soome 29054811da5SToomas Soome switch (newval) { 29154811da5SToomas Soome case ZFS_DNSIZE_LEGACY: 29254811da5SToomas Soome os->os_dnodesize = DNODE_MIN_SIZE; 29354811da5SToomas Soome break; 29454811da5SToomas Soome case ZFS_DNSIZE_AUTO: 29554811da5SToomas Soome /* 29654811da5SToomas Soome * Choose a dnode size that will work well for most 29754811da5SToomas Soome * workloads if the user specified "auto". Future code 29854811da5SToomas Soome * improvements could dynamically select a dnode size 29954811da5SToomas Soome * based on observed workload patterns. 30054811da5SToomas Soome */ 30154811da5SToomas Soome os->os_dnodesize = DNODE_MIN_SIZE * 2; 30254811da5SToomas Soome break; 30354811da5SToomas Soome case ZFS_DNSIZE_1K: 30454811da5SToomas Soome case ZFS_DNSIZE_2K: 30554811da5SToomas Soome case ZFS_DNSIZE_4K: 30654811da5SToomas Soome case ZFS_DNSIZE_8K: 30754811da5SToomas Soome case ZFS_DNSIZE_16K: 30854811da5SToomas Soome os->os_dnodesize = newval; 30954811da5SToomas Soome break; 31054811da5SToomas Soome } 31154811da5SToomas Soome } 31254811da5SToomas Soome 313663207adSDon Brady static void 314663207adSDon Brady smallblk_changed_cb(void *arg, uint64_t newval) 315663207adSDon Brady { 316663207adSDon Brady objset_t *os = arg; 317663207adSDon Brady 318663207adSDon Brady /* 319663207adSDon Brady * Inheritance and range checking should have been done by now. 320663207adSDon Brady */ 321663207adSDon Brady ASSERT(newval <= SPA_OLD_MAXBLOCKSIZE); 322663207adSDon Brady ASSERT(ISP2(newval)); 323663207adSDon Brady 324663207adSDon Brady os->os_zpl_special_smallblock = newval; 325663207adSDon Brady } 326663207adSDon Brady 327e09fa4daSNeil Perrin static void 328e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval) 329e09fa4daSNeil Perrin { 330e09fa4daSNeil Perrin objset_t *os = arg; 331e09fa4daSNeil Perrin 332e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY || 333e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT); 334e09fa4daSNeil Perrin os->os_logbias = newval; 335e09fa4daSNeil Perrin if (os->os_zil) 336e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval); 337e09fa4daSNeil Perrin } 338e09fa4daSNeil Perrin 339b5152584SMatthew Ahrens static void 340b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval) 341b5152584SMatthew Ahrens { 342b5152584SMatthew Ahrens objset_t *os = arg; 343b5152584SMatthew Ahrens 344b5152584SMatthew Ahrens os->os_recordsize = newval; 345b5152584SMatthew Ahrens } 346b5152584SMatthew Ahrens 347fa9e4066Sahrens void 348fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 349fa9e4066Sahrens { 350fa9e4066Sahrens objset_phys_t *osp = buf; 351fa9e4066Sahrens 352*f67950b2SNasf-Fan ASSERT(size == OBJSET_PHYS_SIZE_V1 || size == OBJSET_PHYS_SIZE_V2 || 353*f67950b2SNasf-Fan size == sizeof (objset_phys_t)); 354fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 355fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 356fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 35714843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 358*f67950b2SNasf-Fan if (size >= OBJSET_PHYS_SIZE_V2) { 35914843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 36014843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 361*f67950b2SNasf-Fan if (size >= sizeof (objset_phys_t)) 362*f67950b2SNasf-Fan dnode_byteswap(&osp->os_projectused_dnode); 36314843421SMatthew Ahrens } 364fa9e4066Sahrens } 365fa9e4066Sahrens 36694c2d0ebSMatthew Ahrens /* 36794c2d0ebSMatthew Ahrens * The hash is a CRC-based hash of the objset_t pointer and the object number. 36894c2d0ebSMatthew Ahrens */ 36994c2d0ebSMatthew Ahrens static uint64_t 37094c2d0ebSMatthew Ahrens dnode_hash(const objset_t *os, uint64_t obj) 37194c2d0ebSMatthew Ahrens { 37294c2d0ebSMatthew Ahrens uintptr_t osv = (uintptr_t)os; 37394c2d0ebSMatthew Ahrens uint64_t crc = -1ULL; 37494c2d0ebSMatthew Ahrens 37594c2d0ebSMatthew Ahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 37694c2d0ebSMatthew Ahrens /* 37794c2d0ebSMatthew Ahrens * The low 6 bits of the pointer don't have much entropy, because 37894c2d0ebSMatthew Ahrens * the objset_t is larger than 2^6 bytes long. 37994c2d0ebSMatthew Ahrens */ 38094c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF]; 38194c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF]; 38294c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF]; 38394c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF]; 38494c2d0ebSMatthew Ahrens 38594c2d0ebSMatthew Ahrens crc ^= (osv>>14) ^ (obj>>24); 38694c2d0ebSMatthew Ahrens 38794c2d0ebSMatthew Ahrens return (crc); 38894c2d0ebSMatthew Ahrens } 38994c2d0ebSMatthew Ahrens 39094c2d0ebSMatthew Ahrens unsigned int 39194c2d0ebSMatthew Ahrens dnode_multilist_index_func(multilist_t *ml, void *obj) 39294c2d0ebSMatthew Ahrens { 39394c2d0ebSMatthew Ahrens dnode_t *dn = obj; 39494c2d0ebSMatthew Ahrens return (dnode_hash(dn->dn_objset, dn->dn_object) % 39594c2d0ebSMatthew Ahrens multilist_get_num_sublists(ml)); 39694c2d0ebSMatthew Ahrens } 39794c2d0ebSMatthew Ahrens 39894c2d0ebSMatthew Ahrens /* 39994c2d0ebSMatthew Ahrens * Instantiates the objset_t in-memory structure corresponding to the 40094c2d0ebSMatthew Ahrens * objset_phys_t that's pointed to by the specified blkptr_t. 40194c2d0ebSMatthew Ahrens */ 402ea8dc4b6Seschrock int 403ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 404503ad85cSMatthew Ahrens objset_t **osp) 405fa9e4066Sahrens { 406503ad85cSMatthew Ahrens objset_t *os; 407088f3894Sahrens int i, err; 408fa9e4066Sahrens 40991ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 41091ebeef5Sahrens 411a3874b8bSToomas Soome #if 0 4125cabbc6bSPrashanth Sreenivasa /* 4135cabbc6bSPrashanth Sreenivasa * The $ORIGIN dataset (if it exists) doesn't have an associated 4145cabbc6bSPrashanth Sreenivasa * objset, so there's no reason to open it. The $ORIGIN dataset 4155cabbc6bSPrashanth Sreenivasa * will not exist on pools older than SPA_VERSION_ORIGIN. 4165cabbc6bSPrashanth Sreenivasa */ 4175cabbc6bSPrashanth Sreenivasa if (ds != NULL && spa_get_dsl(spa) != NULL && 4185cabbc6bSPrashanth Sreenivasa spa_get_dsl(spa)->dp_origin_snap != NULL) { 4195cabbc6bSPrashanth Sreenivasa ASSERT3P(ds->ds_dir, !=, 4205cabbc6bSPrashanth Sreenivasa spa_get_dsl(spa)->dp_origin_snap->ds_dir); 4215cabbc6bSPrashanth Sreenivasa } 422a3874b8bSToomas Soome #endif 4235cabbc6bSPrashanth Sreenivasa 424503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 425503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 426503ad85cSMatthew Ahrens os->os_spa = spa; 427503ad85cSMatthew Ahrens os->os_rootbp = bp; 428503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 4297adb730bSGeorge Wilson arc_flags_t aflags = ARC_FLAG_WAIT; 4307802d7bfSMatthew Ahrens zbookmark_phys_t zb; 431*f67950b2SNasf-Fan int size; 432eb633035STom Caputi enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 433b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 434b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 435b24ab676SJeff Bonwick 436503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 4377adb730bSGeorge Wilson aflags |= ARC_FLAG_L2CACHE; 438ea8dc4b6Seschrock 439eb633035STom Caputi if (ds != NULL && ds->ds_dir->dd_crypto_obj != 0) { 440eb633035STom Caputi ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); 441eb633035STom Caputi ASSERT(BP_IS_AUTHENTICATED(bp)); 442eb633035STom Caputi zio_flags |= ZIO_FLAG_RAW; 443eb633035STom Caputi } 444eb633035STom Caputi 445503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 4461b912ec7SGeorge Wilson err = arc_read(NULL, spa, os->os_rootbp, 447503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 448eb633035STom Caputi ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 4493b2aab18SMatthew Ahrens if (err != 0) { 450503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 451b87f3af3Sperrin /* convert checksum errors into IO errors */ 452b87f3af3Sperrin if (err == ECKSUM) 453be6fd75aSMatthew Ahrens err = SET_ERROR(EIO); 454ea8dc4b6Seschrock return (err); 455ea8dc4b6Seschrock } 45614843421SMatthew Ahrens 457*f67950b2SNasf-Fan if (spa_version(spa) < SPA_VERSION_USERSPACE) 458*f67950b2SNasf-Fan size = OBJSET_PHYS_SIZE_V1; 459*f67950b2SNasf-Fan else if (!spa_feature_is_enabled(spa, 460*f67950b2SNasf-Fan SPA_FEATURE_PROJECT_QUOTA)) 461*f67950b2SNasf-Fan size = OBJSET_PHYS_SIZE_V2; 462*f67950b2SNasf-Fan else 463*f67950b2SNasf-Fan size = sizeof (objset_phys_t); 464*f67950b2SNasf-Fan 46514843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 466*f67950b2SNasf-Fan if (arc_buf_size(os->os_phys_buf) < size) { 4675602294fSDan Kimmel arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf, 468*f67950b2SNasf-Fan ARC_BUFC_METADATA, size); 469*f67950b2SNasf-Fan bzero(buf->b_data, size); 470503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 471503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 472dcbf3bd6SGeorge Wilson arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 473503ad85cSMatthew Ahrens os->os_phys_buf = buf; 47414843421SMatthew Ahrens } 47514843421SMatthew Ahrens 476503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 477503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 478fa9e4066Sahrens } else { 47914843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 480*f67950b2SNasf-Fan sizeof (objset_phys_t) : OBJSET_PHYS_SIZE_V1; 4815602294fSDan Kimmel os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf, 4825602294fSDan Kimmel ARC_BUFC_METADATA, size); 483503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 484503ad85cSMatthew Ahrens bzero(os->os_phys, size); 485fa9e4066Sahrens } 486fa9e4066Sahrens 487fa9e4066Sahrens /* 488fa9e4066Sahrens * Note: the changed_cb will be called once before the register 489fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 4903baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 4913baa08fcSek * checksum/compression/copies. 492fa9e4066Sahrens */ 4935d7b4d43SMatthew Ahrens if (ds != NULL) { 4949c3fd121SMatthew Ahrens boolean_t needlock = B_FALSE; 4959c3fd121SMatthew Ahrens 496eb633035STom Caputi os->os_encrypted = (ds->ds_dir->dd_crypto_obj != 0); 497eb633035STom Caputi 4989c3fd121SMatthew Ahrens /* 4999c3fd121SMatthew Ahrens * Note: it's valid to open the objset if the dataset is 5009c3fd121SMatthew Ahrens * long-held, in which case the pool_config lock will not 5019c3fd121SMatthew Ahrens * be held. 5029c3fd121SMatthew Ahrens */ 5039c3fd121SMatthew Ahrens if (!dsl_pool_config_held(dmu_objset_pool(os))) { 5049c3fd121SMatthew Ahrens needlock = B_TRUE; 5059c3fd121SMatthew Ahrens dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 5069c3fd121SMatthew Ahrens } 507eb633035STom Caputi 5083b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5093b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE), 510503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 5113b2aab18SMatthew Ahrens if (err == 0) { 5123b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5133b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE), 514503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 5153b2aab18SMatthew Ahrens } 516bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) { 5173b2aab18SMatthew Ahrens if (err == 0) { 5183b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5193b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CHECKSUM), 520503ad85cSMatthew Ahrens checksum_changed_cb, os); 5213b2aab18SMatthew Ahrens } 5223b2aab18SMatthew Ahrens if (err == 0) { 5233b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5243b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COMPRESSION), 525503ad85cSMatthew Ahrens compression_changed_cb, os); 5263b2aab18SMatthew Ahrens } 5273b2aab18SMatthew Ahrens if (err == 0) { 5283b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5293b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COPIES), 530503ad85cSMatthew Ahrens copies_changed_cb, os); 5313b2aab18SMatthew Ahrens } 5323b2aab18SMatthew Ahrens if (err == 0) { 5333b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5343b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_DEDUP), 535b24ab676SJeff Bonwick dedup_changed_cb, os); 5363b2aab18SMatthew Ahrens } 5373b2aab18SMatthew Ahrens if (err == 0) { 5383b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5393b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_LOGBIAS), 540e09fa4daSNeil Perrin logbias_changed_cb, os); 5413b2aab18SMatthew Ahrens } 5423b2aab18SMatthew Ahrens if (err == 0) { 5433b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5443b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SYNC), 54555da60b9SMark J Musante sync_changed_cb, os); 5463b2aab18SMatthew Ahrens } 547edf345e6SMatthew Ahrens if (err == 0) { 548edf345e6SMatthew Ahrens err = dsl_prop_register(ds, 549edf345e6SMatthew Ahrens zfs_prop_to_name( 550edf345e6SMatthew Ahrens ZFS_PROP_REDUNDANT_METADATA), 551edf345e6SMatthew Ahrens redundant_metadata_changed_cb, os); 552edf345e6SMatthew Ahrens } 553b5152584SMatthew Ahrens if (err == 0) { 554b5152584SMatthew Ahrens err = dsl_prop_register(ds, 555b5152584SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RECORDSIZE), 556b5152584SMatthew Ahrens recordsize_changed_cb, os); 557b5152584SMatthew Ahrens } 55854811da5SToomas Soome if (err == 0) { 55954811da5SToomas Soome err = dsl_prop_register(ds, 56054811da5SToomas Soome zfs_prop_to_name(ZFS_PROP_DNODESIZE), 56154811da5SToomas Soome dnodesize_changed_cb, os); 56254811da5SToomas Soome } 563663207adSDon Brady if (err == 0) { 564663207adSDon Brady err = dsl_prop_register(ds, 565663207adSDon Brady zfs_prop_to_name( 566663207adSDon Brady ZFS_PROP_SPECIAL_SMALL_BLOCKS), 567663207adSDon Brady smallblk_changed_cb, os); 568663207adSDon Brady } 5693baa08fcSek } 5709c3fd121SMatthew Ahrens if (needlock) 5719c3fd121SMatthew Ahrens dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 5723b2aab18SMatthew Ahrens if (err != 0) { 573dcbf3bd6SGeorge Wilson arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 574503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 575ea8dc4b6Seschrock return (err); 576ea8dc4b6Seschrock } 5775d7b4d43SMatthew Ahrens } else { 578fa9e4066Sahrens /* It's the meta-objset. */ 579503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 580db1741f5SJustin T. Gibbs os->os_compress = ZIO_COMPRESS_ON; 581eb633035STom Caputi os->os_encrypted = B_FALSE; 582503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 583b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 584edf345e6SMatthew Ahrens os->os_dedup_verify = B_FALSE; 585edf345e6SMatthew Ahrens os->os_logbias = ZFS_LOGBIAS_LATENCY; 586edf345e6SMatthew Ahrens os->os_sync = ZFS_SYNC_STANDARD; 587503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 588503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 58954811da5SToomas Soome os->os_dnodesize = DNODE_MIN_SIZE; 590fa9e4066Sahrens } 591adb52d92SMatthew Ahrens /* 592adb52d92SMatthew Ahrens * These properties will be filled in by the logic in zfs_get_zplprop() 593adb52d92SMatthew Ahrens * when they are queried for the first time. 594adb52d92SMatthew Ahrens */ 595adb52d92SMatthew Ahrens os->os_version = OBJSET_PROP_UNINITIALIZED; 596adb52d92SMatthew Ahrens os->os_normalization = OBJSET_PROP_UNINITIALIZED; 597adb52d92SMatthew Ahrens os->os_utf8only = OBJSET_PROP_UNINITIALIZED; 598adb52d92SMatthew Ahrens os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED; 599fa9e4066Sahrens 600bc9014e6SJustin Gibbs if (ds == NULL || !ds->ds_is_snapshot) 6016e0cbcaaSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 602503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 603fa9e4066Sahrens 604fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 60594c2d0ebSMatthew Ahrens os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t), 60694c2d0ebSMatthew Ahrens offsetof(dnode_t, dn_dirty_link[i]), 60794c2d0ebSMatthew Ahrens dnode_multilist_index_func); 608fa9e4066Sahrens } 609503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 610fa9e4066Sahrens offsetof(dnode_t, dn_link)); 611503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 612fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 613fa9e4066Sahrens 614503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 61594c2d0ebSMatthew Ahrens mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL); 616503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 617503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 61854811da5SToomas Soome os->os_obj_next_percpu_len = boot_ncpus; 61954811da5SToomas Soome os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len * 62054811da5SToomas Soome sizeof (os->os_obj_next_percpu[0]), KM_SLEEP); 621503ad85cSMatthew Ahrens 622bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_meta_dnode, 623bc9014e6SJustin Gibbs DMU_META_DNODE_OBJECT, &os->os_meta_dnode); 624*f67950b2SNasf-Fan if (OBJSET_BUF_HAS_USERUSED(os->os_phys_buf)) { 625bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_userused_dnode, 626bc9014e6SJustin Gibbs DMU_USERUSED_OBJECT, &os->os_userused_dnode); 627bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_groupused_dnode, 628bc9014e6SJustin Gibbs DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode); 629*f67950b2SNasf-Fan if (OBJSET_BUF_HAS_PROJECTUSED(os->os_phys_buf)) 630*f67950b2SNasf-Fan dnode_special_open(os, 631*f67950b2SNasf-Fan &os->os_phys->os_projectused_dnode, 632*f67950b2SNasf-Fan DMU_PROJECTUSED_OBJECT, &os->os_projectused_dnode); 63314843421SMatthew Ahrens } 634fa9e4066Sahrens 635*f67950b2SNasf-Fan mutex_init(&os->os_upgrade_lock, NULL, MUTEX_DEFAULT, NULL); 636*f67950b2SNasf-Fan 637503ad85cSMatthew Ahrens *osp = os; 638ea8dc4b6Seschrock return (0); 639fa9e4066Sahrens } 640fa9e4066Sahrens 641503ad85cSMatthew Ahrens int 642503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 6433cb34c60Sahrens { 644503ad85cSMatthew Ahrens int err = 0; 6453cb34c60Sahrens 6469c3fd121SMatthew Ahrens /* 6479c3fd121SMatthew Ahrens * We shouldn't be doing anything with dsl_dataset_t's unless the 6489c3fd121SMatthew Ahrens * pool_config lock is held, or the dataset is long-held. 6499c3fd121SMatthew Ahrens */ 6509c3fd121SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) || 6519c3fd121SMatthew Ahrens dsl_dataset_long_held(ds)); 6529c3fd121SMatthew Ahrens 6533cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 6545d7b4d43SMatthew Ahrens if (ds->ds_objset == NULL) { 6555d7b4d43SMatthew Ahrens objset_t *os; 656c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 6573cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 6585d7b4d43SMatthew Ahrens ds, dsl_dataset_get_blkptr(ds), &os); 659c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 6605d7b4d43SMatthew Ahrens 6615d7b4d43SMatthew Ahrens if (err == 0) { 6625d7b4d43SMatthew Ahrens mutex_enter(&ds->ds_lock); 6635d7b4d43SMatthew Ahrens ASSERT(ds->ds_objset == NULL); 6645d7b4d43SMatthew Ahrens ds->ds_objset = os; 6655d7b4d43SMatthew Ahrens mutex_exit(&ds->ds_lock); 6665d7b4d43SMatthew Ahrens } 6673cb34c60Sahrens } 6685d7b4d43SMatthew Ahrens *osp = ds->ds_objset; 6693cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 670503ad85cSMatthew Ahrens return (err); 6713cb34c60Sahrens } 6723cb34c60Sahrens 6733b2aab18SMatthew Ahrens /* 6743b2aab18SMatthew Ahrens * Holds the pool while the objset is held. Therefore only one objset 6753b2aab18SMatthew Ahrens * can be held at a time. 6763b2aab18SMatthew Ahrens */ 6773cb34c60Sahrens int 678eb633035STom Caputi dmu_objset_hold_flags(const char *name, boolean_t decrypt, void *tag, 679eb633035STom Caputi objset_t **osp) 6803cb34c60Sahrens { 6813b2aab18SMatthew Ahrens dsl_pool_t *dp; 682503ad85cSMatthew Ahrens dsl_dataset_t *ds; 6833cb34c60Sahrens int err; 684eb633035STom Caputi ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 6853cb34c60Sahrens 6863b2aab18SMatthew Ahrens err = dsl_pool_hold(name, tag, &dp); 6873b2aab18SMatthew Ahrens if (err != 0) 6883b2aab18SMatthew Ahrens return (err); 689eb633035STom Caputi err = dsl_dataset_hold_flags(dp, name, flags, tag, &ds); 6903b2aab18SMatthew Ahrens if (err != 0) { 6913b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag); 692503ad85cSMatthew Ahrens return (err); 6933b2aab18SMatthew Ahrens } 694503ad85cSMatthew Ahrens 695503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 6963b2aab18SMatthew Ahrens if (err != 0) { 697503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 6983b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag); 6993b2aab18SMatthew Ahrens } 700503ad85cSMatthew Ahrens 7013cb34c60Sahrens return (err); 7023cb34c60Sahrens } 7033cb34c60Sahrens 704eb633035STom Caputi int 705eb633035STom Caputi dmu_objset_hold(const char *name, void *tag, objset_t **osp) 706eb633035STom Caputi { 707eb633035STom Caputi return (dmu_objset_hold_flags(name, B_FALSE, tag, osp)); 708eb633035STom Caputi } 709eb633035STom Caputi 710eb633035STom Caputi /* ARGSUSED */ 71112380e1eSArne Jansen static int 71212380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type, 713eb633035STom Caputi boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) 71412380e1eSArne Jansen { 71512380e1eSArne Jansen int err; 71612380e1eSArne Jansen 71712380e1eSArne Jansen err = dmu_objset_from_ds(ds, osp); 71812380e1eSArne Jansen if (err != 0) { 719eb633035STom Caputi return (err); 72012380e1eSArne Jansen } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 72112380e1eSArne Jansen return (SET_ERROR(EINVAL)); 72212380e1eSArne Jansen } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 723eb633035STom Caputi return (SET_ERROR(EROFS)); 724eb633035STom Caputi } else if (!readonly && decrypt && 725eb633035STom Caputi dsl_dir_incompatible_encryption_version(ds->ds_dir)) { 72612380e1eSArne Jansen return (SET_ERROR(EROFS)); 72712380e1eSArne Jansen } 728eb633035STom Caputi 729eb633035STom Caputi /* if we are decrypting, we can now check MACs in os->os_phys_buf */ 730eb633035STom Caputi if (decrypt && arc_is_unauthenticated((*osp)->os_phys_buf)) { 731eb633035STom Caputi zbookmark_phys_t zb; 732eb633035STom Caputi 733eb633035STom Caputi SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT, 734eb633035STom Caputi ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 735eb633035STom Caputi err = arc_untransform((*osp)->os_phys_buf, (*osp)->os_spa, 736eb633035STom Caputi &zb, B_FALSE); 737eb633035STom Caputi if (err != 0) 738eb633035STom Caputi return (err); 739eb633035STom Caputi 740eb633035STom Caputi ASSERT0(arc_is_unauthenticated((*osp)->os_phys_buf)); 741eb633035STom Caputi } 742eb633035STom Caputi 743eb633035STom Caputi return (0); 74412380e1eSArne Jansen } 74512380e1eSArne Jansen 7463b2aab18SMatthew Ahrens /* 7473b2aab18SMatthew Ahrens * dsl_pool must not be held when this is called. 7483b2aab18SMatthew Ahrens * Upon successful return, there will be a longhold on the dataset, 7493b2aab18SMatthew Ahrens * and the dsl_pool will not be held. 7503b2aab18SMatthew Ahrens */ 751fa9e4066Sahrens int 752503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 753eb633035STom Caputi boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) 754fa9e4066Sahrens { 7553b2aab18SMatthew Ahrens dsl_pool_t *dp; 756f18faf3fSek dsl_dataset_t *ds; 757f18faf3fSek int err; 758eb633035STom Caputi ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 759fa9e4066Sahrens 7603b2aab18SMatthew Ahrens err = dsl_pool_hold(name, FTAG, &dp); 7613b2aab18SMatthew Ahrens if (err != 0) 7623b2aab18SMatthew Ahrens return (err); 763eb633035STom Caputi err = dsl_dataset_own(dp, name, flags, tag, &ds); 7643b2aab18SMatthew Ahrens if (err != 0) { 7653b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 766fa9e4066Sahrens return (err); 7673b2aab18SMatthew Ahrens } 768eb633035STom Caputi err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp); 769eb633035STom Caputi if (err != 0) { 770eb633035STom Caputi dsl_dataset_disown(ds, flags, tag); 771eb633035STom Caputi dsl_pool_rele(dp, FTAG); 772eb633035STom Caputi return (err); 773eb633035STom Caputi } 774eb633035STom Caputi 775*f67950b2SNasf-Fan /* 776*f67950b2SNasf-Fan * User accounting requires the dataset to be decrypted and rw. 777*f67950b2SNasf-Fan * We also don't begin user accounting during claiming to help 778*f67950b2SNasf-Fan * speed up pool import times and to keep this txg reserved 779*f67950b2SNasf-Fan * completely for recovery work. 780*f67950b2SNasf-Fan */ 781*f67950b2SNasf-Fan if ((dmu_objset_userobjspace_upgradable(*osp) || 782*f67950b2SNasf-Fan dmu_objset_projectquota_upgradable(*osp)) && 783*f67950b2SNasf-Fan !readonly && !dp->dp_spa->spa_claiming && 784*f67950b2SNasf-Fan (ds->ds_dir->dd_crypto_obj == 0 || decrypt)) 785*f67950b2SNasf-Fan dmu_objset_id_quota_upgrade(*osp); 78612380e1eSArne Jansen 787*f67950b2SNasf-Fan dsl_pool_rele(dp, FTAG); 788eb633035STom Caputi return (0); 789fa9e4066Sahrens } 790fa9e4066Sahrens 79112380e1eSArne Jansen int 79212380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type, 793eb633035STom Caputi boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp) 79412380e1eSArne Jansen { 79512380e1eSArne Jansen dsl_dataset_t *ds; 79612380e1eSArne Jansen int err; 797eb633035STom Caputi ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 79812380e1eSArne Jansen 799eb633035STom Caputi err = dsl_dataset_own_obj(dp, obj, flags, tag, &ds); 80012380e1eSArne Jansen if (err != 0) 80112380e1eSArne Jansen return (err); 80212380e1eSArne Jansen 803eb633035STom Caputi err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp); 804eb633035STom Caputi if (err != 0) { 805eb633035STom Caputi dsl_dataset_disown(ds, flags, tag); 806eb633035STom Caputi return (err); 807eb633035STom Caputi } 808eb633035STom Caputi 809eb633035STom Caputi return (0); 81012380e1eSArne Jansen } 81112380e1eSArne Jansen 812fa9e4066Sahrens void 813eb633035STom Caputi dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, void *tag) 814fa9e4066Sahrens { 815eb633035STom Caputi ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0; 816eb633035STom Caputi 8173b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os); 818eb633035STom Caputi dsl_dataset_rele_flags(os->os_dsl_dataset, flags, tag); 8193b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag); 820503ad85cSMatthew Ahrens } 821503ad85cSMatthew Ahrens 822eb633035STom Caputi void 823eb633035STom Caputi dmu_objset_rele(objset_t *os, void *tag) 824eb633035STom Caputi { 825eb633035STom Caputi dmu_objset_rele_flags(os, B_FALSE, tag); 826eb633035STom Caputi } 827eb633035STom Caputi 82891948b51SKeith M Wesolowski /* 82991948b51SKeith M Wesolowski * When we are called, os MUST refer to an objset associated with a dataset 83091948b51SKeith M Wesolowski * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner 83191948b51SKeith M Wesolowski * == tag. We will then release and reacquire ownership of the dataset while 83291948b51SKeith M Wesolowski * holding the pool config_rwlock to avoid intervening namespace or ownership 83391948b51SKeith M Wesolowski * changes may occur. 83491948b51SKeith M Wesolowski * 83591948b51SKeith M Wesolowski * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to 83691948b51SKeith M Wesolowski * release the hold on its dataset and acquire a new one on the dataset of the 83791948b51SKeith M Wesolowski * same name so that it can be partially torn down and reconstructed. 83891948b51SKeith M Wesolowski */ 83991948b51SKeith M Wesolowski void 8405f5913bbSAndriy Gapon dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds, 841eb633035STom Caputi boolean_t decrypt, void *tag) 84291948b51SKeith M Wesolowski { 84391948b51SKeith M Wesolowski dsl_pool_t *dp; 8449adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 84591948b51SKeith M Wesolowski 84691948b51SKeith M Wesolowski VERIFY3P(ds, !=, NULL); 84791948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, tag); 84891948b51SKeith M Wesolowski VERIFY(dsl_dataset_long_held(ds)); 84991948b51SKeith M Wesolowski 85091948b51SKeith M Wesolowski dsl_dataset_name(ds, name); 8515f5913bbSAndriy Gapon dp = ds->ds_dir->dd_pool; 85291948b51SKeith M Wesolowski dsl_pool_config_enter(dp, FTAG); 853eb633035STom Caputi 854eb633035STom Caputi dsl_dataset_disown(ds, 0, tag); 855eb633035STom Caputi VERIFY0(dsl_dataset_own(dp, name, 856eb633035STom Caputi (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag, newds)); 85791948b51SKeith M Wesolowski dsl_pool_config_exit(dp, FTAG); 85891948b51SKeith M Wesolowski } 85991948b51SKeith M Wesolowski 860503ad85cSMatthew Ahrens void 861eb633035STom Caputi dmu_objset_disown(objset_t *os, boolean_t decrypt, void *tag) 862503ad85cSMatthew Ahrens { 863*f67950b2SNasf-Fan /* 864*f67950b2SNasf-Fan * Stop upgrading thread 865*f67950b2SNasf-Fan */ 866*f67950b2SNasf-Fan dmu_objset_upgrade_stop(os); 867eb633035STom Caputi dsl_dataset_disown(os->os_dsl_dataset, 868eb633035STom Caputi (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag); 869fa9e4066Sahrens } 870fa9e4066Sahrens 8713b2aab18SMatthew Ahrens void 8721934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 873ea8dc4b6Seschrock { 874bc9014e6SJustin Gibbs dnode_t dn_marker; 875ea8dc4b6Seschrock dnode_t *dn; 876c543ec06Sahrens 877503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 878bc9014e6SJustin Gibbs dn = list_head(&os->os_dnodes); 879bc9014e6SJustin Gibbs while (dn != NULL) { 880bc9014e6SJustin Gibbs /* 881bc9014e6SJustin Gibbs * Skip dnodes without holds. We have to do this dance 882bc9014e6SJustin Gibbs * because dnode_add_ref() only works if there is already a 883bc9014e6SJustin Gibbs * hold. If the dnode has no holds, then it has no dbufs. 884bc9014e6SJustin Gibbs */ 885bc9014e6SJustin Gibbs if (dnode_add_ref(dn, FTAG)) { 886bc9014e6SJustin Gibbs list_insert_after(&os->os_dnodes, dn, &dn_marker); 887bc9014e6SJustin Gibbs mutex_exit(&os->os_lock); 888c543ec06Sahrens 889bc9014e6SJustin Gibbs dnode_evict_dbufs(dn); 890bc9014e6SJustin Gibbs dnode_rele(dn, FTAG); 891c543ec06Sahrens 892bc9014e6SJustin Gibbs mutex_enter(&os->os_lock); 893bc9014e6SJustin Gibbs dn = list_next(&os->os_dnodes, &dn_marker); 894bc9014e6SJustin Gibbs list_remove(&os->os_dnodes, &dn_marker); 895bc9014e6SJustin Gibbs } else { 896bc9014e6SJustin Gibbs dn = list_next(&os->os_dnodes, dn); 897bc9014e6SJustin Gibbs } 898ea8dc4b6Seschrock } 899503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 900bc9014e6SJustin Gibbs 901bc9014e6SJustin Gibbs if (DMU_USERUSED_DNODE(os) != NULL) { 902*f67950b2SNasf-Fan if (DMU_PROJECTUSED_DNODE(os) != NULL) 903*f67950b2SNasf-Fan dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os)); 904bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os)); 905bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_USERUSED_DNODE(os)); 906bc9014e6SJustin Gibbs } 907bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_META_DNODE(os)); 908ea8dc4b6Seschrock } 909ea8dc4b6Seschrock 910bc9014e6SJustin Gibbs /* 911bc9014e6SJustin Gibbs * Objset eviction processing is split into into two pieces. 912bc9014e6SJustin Gibbs * The first marks the objset as evicting, evicts any dbufs that 913bc9014e6SJustin Gibbs * have a refcount of zero, and then queues up the objset for the 914bc9014e6SJustin Gibbs * second phase of eviction. Once os->os_dnodes has been cleared by 915bc9014e6SJustin Gibbs * dnode_buf_pageout()->dnode_destroy(), the second phase is executed. 916bc9014e6SJustin Gibbs * The second phase closes the special dnodes, dequeues the objset from 917bc9014e6SJustin Gibbs * the list of those undergoing eviction, and finally frees the objset. 918bc9014e6SJustin Gibbs * 919bc9014e6SJustin Gibbs * NOTE: Due to asynchronous eviction processing (invocation of 920bc9014e6SJustin Gibbs * dnode_buf_pageout()), it is possible for the meta dnode for the 921bc9014e6SJustin Gibbs * objset to have no holds even though os->os_dnodes is not empty. 922bc9014e6SJustin Gibbs */ 923fa9e4066Sahrens void 924503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 925fa9e4066Sahrens { 926503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 927fa9e4066Sahrens 928b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 929b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t)); 930fa9e4066Sahrens 93103bad06fSJustin Gibbs if (ds) 93203bad06fSJustin Gibbs dsl_prop_unregister_all(ds, os); 933fa9e4066Sahrens 9340a586ceaSMark Shellenbaum if (os->os_sa) 9350a586ceaSMark Shellenbaum sa_tear_down(os); 9360a586ceaSMark Shellenbaum 9373b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(os); 938ea8dc4b6Seschrock 939bc9014e6SJustin Gibbs mutex_enter(&os->os_lock); 940bc9014e6SJustin Gibbs spa_evicting_os_register(os->os_spa, os); 941bc9014e6SJustin Gibbs if (list_is_empty(&os->os_dnodes)) { 942bc9014e6SJustin Gibbs mutex_exit(&os->os_lock); 943bc9014e6SJustin Gibbs dmu_objset_evict_done(os); 944bc9014e6SJustin Gibbs } else { 945bc9014e6SJustin Gibbs mutex_exit(&os->os_lock); 946bc9014e6SJustin Gibbs } 947eb633035STom Caputi 948eb633035STom Caputi 949bc9014e6SJustin Gibbs } 950bc9014e6SJustin Gibbs 951bc9014e6SJustin Gibbs void 952bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os) 953bc9014e6SJustin Gibbs { 954bc9014e6SJustin Gibbs ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 955bc9014e6SJustin Gibbs 956744947dcSTom Erickson dnode_special_close(&os->os_meta_dnode); 957744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)) { 958*f67950b2SNasf-Fan if (DMU_PROJECTUSED_DNODE(os)) 959*f67950b2SNasf-Fan dnode_special_close(&os->os_projectused_dnode); 960744947dcSTom Erickson dnode_special_close(&os->os_userused_dnode); 961744947dcSTom Erickson dnode_special_close(&os->os_groupused_dnode); 96214843421SMatthew Ahrens } 963503ad85cSMatthew Ahrens zil_free(os->os_zil); 964fa9e4066Sahrens 965dcbf3bd6SGeorge Wilson arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 966744947dcSTom Erickson 967744947dcSTom Erickson /* 968744947dcSTom Erickson * This is a barrier to prevent the objset from going away in 969744947dcSTom Erickson * dnode_move() until we can safely ensure that the objset is still in 970744947dcSTom Erickson * use. We consider the objset valid before the barrier and invalid 971744947dcSTom Erickson * after the barrier. 972744947dcSTom Erickson */ 973744947dcSTom Erickson rw_enter(&os_lock, RW_READER); 974744947dcSTom Erickson rw_exit(&os_lock); 975744947dcSTom Erickson 97654811da5SToomas Soome kmem_free(os->os_obj_next_percpu, 97754811da5SToomas Soome os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0])); 97854811da5SToomas Soome 979503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 98094c2d0ebSMatthew Ahrens mutex_destroy(&os->os_userused_lock); 981503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 982503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 98394c2d0ebSMatthew Ahrens for (int i = 0; i < TXG_SIZE; i++) { 98494c2d0ebSMatthew Ahrens multilist_destroy(os->os_dirty_dnodes[i]); 98594c2d0ebSMatthew Ahrens } 986bc9014e6SJustin Gibbs spa_evicting_os_deregister(os->os_spa, os); 987503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 988fa9e4066Sahrens } 989fa9e4066Sahrens 99071eb0538SChris Kirby timestruc_t 99171eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os) 99271eb0538SChris Kirby { 99371eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 99471eb0538SChris Kirby } 99571eb0538SChris Kirby 996eb633035STom Caputi /* ARGSUSED */ 997503ad85cSMatthew Ahrens objset_t * 998eb633035STom Caputi dmu_objset_create_impl_dnstats(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 999eb633035STom Caputi dmu_objset_type_t type, int levels, int blksz, int ibs, dmu_tx_t *tx) 1000fa9e4066Sahrens { 1001503ad85cSMatthew Ahrens objset_t *os; 1002fa9e4066Sahrens dnode_t *mdn; 1003fa9e4066Sahrens 1004fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 10053b2aab18SMatthew Ahrens 1006eb633035STom Caputi if (blksz == 0) 1007eb633035STom Caputi blksz = 1 << DNODE_BLOCK_SHIFT; 1008eb633035STom Caputi if (ibs == 0) 1009eb633035STom Caputi ibs = DN_MAX_INDBLKSHIFT; 1010eb633035STom Caputi 1011feaa74e4SMark Maybee if (ds != NULL) 10123b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 1013feaa74e4SMark Maybee else 10143b2aab18SMatthew Ahrens VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os)); 1015feaa74e4SMark Maybee 1016744947dcSTom Erickson mdn = DMU_META_DNODE(os); 1017fa9e4066Sahrens 101854811da5SToomas Soome dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT, 101954811da5SToomas Soome DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx); 1020fa9e4066Sahrens 1021fa9e4066Sahrens /* 1022fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 1023fa9e4066Sahrens * later, because then we could do it in quescing context while 1024fa9e4066Sahrens * we are also accessing it in open context. 1025fa9e4066Sahrens * 1026fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 1027fa9e4066Sahrens * because the MOS is only updated in syncing context. 1028fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 1029fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 1030fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 1031fa9e4066Sahrens */ 1032ea8dc4b6Seschrock if (ds != NULL) { 1033eb633035STom Caputi if (levels == 0) { 1034eb633035STom Caputi levels = 1; 1035eb633035STom Caputi 1036eb633035STom Caputi /* 1037eb633035STom Caputi * Determine the number of levels necessary for the 1038eb633035STom Caputi * meta-dnode to contain DN_MAX_OBJECT dnodes. Note 1039eb633035STom Caputi * that in order to ensure that we do not overflow 1040eb633035STom Caputi * 64 bits, there has to be a nlevels that gives us a 1041eb633035STom Caputi * number of blocks > DN_MAX_OBJECT but < 2^64. 1042eb633035STom Caputi * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) 1043eb633035STom Caputi * (10) must be less than (64 - log2(DN_MAX_OBJECT)) 1044eb633035STom Caputi * (16). 1045eb633035STom Caputi */ 1046eb633035STom Caputi while ((uint64_t)mdn->dn_nblkptr << 1047eb633035STom Caputi (mdn->dn_datablkshift - DNODE_SHIFT + (levels - 1) * 1048eb633035STom Caputi (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 1049eb633035STom Caputi DN_MAX_OBJECT) 1050eb633035STom Caputi levels++; 1051eb633035STom Caputi } 1052ea8dc4b6Seschrock 1053fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 1054ea8dc4b6Seschrock mdn->dn_nlevels = levels; 1055ea8dc4b6Seschrock } 1056fa9e4066Sahrens 1057fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 1058fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 1059fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 1060503ad85cSMatthew Ahrens os->os_phys->os_type = type; 1061eb633035STom Caputi 1062eb633035STom Caputi /* 1063eb633035STom Caputi * Enable user accounting if it is enabled and this is not an 1064eb633035STom Caputi * encrypted receive. 1065eb633035STom Caputi */ 1066eb633035STom Caputi if (dmu_objset_userused_enabled(os) && 1067eb633035STom Caputi (!os->os_encrypted || !dmu_objset_is_receiving(os))) { 1068503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 1069*f67950b2SNasf-Fan if (dmu_objset_userobjused_enabled(os)) { 1070*f67950b2SNasf-Fan ds->ds_feature_activation_needed[ 1071*f67950b2SNasf-Fan SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE; 1072*f67950b2SNasf-Fan os->os_phys->os_flags |= 1073*f67950b2SNasf-Fan OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE; 1074*f67950b2SNasf-Fan } 1075*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os)) { 1076*f67950b2SNasf-Fan ds->ds_feature_activation_needed[ 1077*f67950b2SNasf-Fan SPA_FEATURE_PROJECT_QUOTA] = B_TRUE; 1078*f67950b2SNasf-Fan os->os_phys->os_flags |= 1079*f67950b2SNasf-Fan OBJSET_FLAG_PROJECTQUOTA_COMPLETE; 1080*f67950b2SNasf-Fan } 1081503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 108214843421SMatthew Ahrens } 1083fa9e4066Sahrens 1084fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 1085fa9e4066Sahrens 1086503ad85cSMatthew Ahrens return (os); 1087fa9e4066Sahrens } 1088fa9e4066Sahrens 1089eb633035STom Caputi /* called from dsl for meta-objset */ 1090eb633035STom Caputi objset_t * 1091eb633035STom Caputi dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 1092eb633035STom Caputi dmu_objset_type_t type, dmu_tx_t *tx) 1093eb633035STom Caputi { 1094eb633035STom Caputi return (dmu_objset_create_impl_dnstats(spa, ds, bp, type, 0, 0, 0, tx)); 1095eb633035STom Caputi } 1096eb633035STom Caputi 10973b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg { 10983b2aab18SMatthew Ahrens const char *doca_name; 10993b2aab18SMatthew Ahrens cred_t *doca_cred; 11003b2aab18SMatthew Ahrens void (*doca_userfunc)(objset_t *os, void *arg, 11013b2aab18SMatthew Ahrens cred_t *cr, dmu_tx_t *tx); 11023b2aab18SMatthew Ahrens void *doca_userarg; 11033b2aab18SMatthew Ahrens dmu_objset_type_t doca_type; 11043b2aab18SMatthew Ahrens uint64_t doca_flags; 1105eb633035STom Caputi dsl_crypto_params_t *doca_dcp; 11063b2aab18SMatthew Ahrens } dmu_objset_create_arg_t; 1107fa9e4066Sahrens 1108ecd6cf80Smarks /*ARGSUSED*/ 1109fa9e4066Sahrens static int 11103b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx) 1111fa9e4066Sahrens { 11123b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg; 11133b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11143b2aab18SMatthew Ahrens dsl_dir_t *pdd; 11153b2aab18SMatthew Ahrens const char *tail; 11163b2aab18SMatthew Ahrens int error; 11171d452cf5Sahrens 11183b2aab18SMatthew Ahrens if (strchr(doca->doca_name, '@') != NULL) 1119be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 11201d452cf5Sahrens 11219adfa60dSMatthew Ahrens if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN) 11229adfa60dSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 11239adfa60dSMatthew Ahrens 11245ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(doca->doca_name) != 0) 11255ac95da7SSerapheim Dimitropoulos return (SET_ERROR(ENAMETOOLONG)); 11265ac95da7SSerapheim Dimitropoulos 11273b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail); 11283b2aab18SMatthew Ahrens if (error != 0) 11293b2aab18SMatthew Ahrens return (error); 11303b2aab18SMatthew Ahrens if (tail == NULL) { 11313b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 1132be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 11331d452cf5Sahrens } 1134eb633035STom Caputi 1135eb633035STom Caputi error = dmu_objset_create_crypt_check(pdd, doca->doca_dcp, NULL); 1136eb633035STom Caputi if (error != 0) { 1137eb633035STom Caputi dsl_dir_rele(pdd, FTAG); 1138eb633035STom Caputi return (error); 1139eb633035STom Caputi } 1140eb633035STom Caputi 1141a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 1142a2afb611SJerry Jelinek doca->doca_cred); 1143eb633035STom Caputi 11443b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 1145ecd6cf80Smarks 1146a2afb611SJerry Jelinek return (error); 11471d452cf5Sahrens } 11481d452cf5Sahrens 11491d452cf5Sahrens static void 11503b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx) 11511d452cf5Sahrens { 11523b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg; 11533b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 1154eb633035STom Caputi spa_t *spa = dp->dp_spa; 11553b2aab18SMatthew Ahrens dsl_dir_t *pdd; 11563b2aab18SMatthew Ahrens const char *tail; 11574445fffbSMatthew Ahrens dsl_dataset_t *ds; 11583b2aab18SMatthew Ahrens uint64_t obj; 11594445fffbSMatthew Ahrens blkptr_t *bp; 11603b2aab18SMatthew Ahrens objset_t *os; 1161eb633035STom Caputi zio_t *rzio; 1162fa9e4066Sahrens 11633b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail)); 1164fa9e4066Sahrens 11653b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags, 1166eb633035STom Caputi doca->doca_cred, doca->doca_dcp, tx); 1167fa9e4066Sahrens 1168eb633035STom Caputi VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj, 1169eb633035STom Caputi DS_HOLD_FLAG_DECRYPT, FTAG, &ds)); 1170c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 11714445fffbSMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 1172eb633035STom Caputi os = dmu_objset_create_impl(spa, ds, bp, doca->doca_type, tx); 1173c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 1174fa9e4066Sahrens 11753b2aab18SMatthew Ahrens if (doca->doca_userfunc != NULL) { 11763b2aab18SMatthew Ahrens doca->doca_userfunc(os, doca->doca_userarg, 11773b2aab18SMatthew Ahrens doca->doca_cred, tx); 1178fa9e4066Sahrens } 1179ecd6cf80Smarks 1180eb633035STom Caputi /* 1181eb633035STom Caputi * The doca_userfunc() may write out some data that needs to be 1182eb633035STom Caputi * encrypted if the dataset is encrypted (specifically the root 1183eb633035STom Caputi * directory). This data must be written out before the encryption 1184eb633035STom Caputi * key mapping is removed by dsl_dataset_rele_flags(). Force the 1185eb633035STom Caputi * I/O to occur immediately by invoking the relevant sections of 1186eb633035STom Caputi * dsl_pool_sync(). 1187eb633035STom Caputi */ 1188eb633035STom Caputi if (os->os_encrypted) { 1189eb633035STom Caputi dsl_dataset_t *tmpds = NULL; 1190eb633035STom Caputi boolean_t need_sync_done = B_FALSE; 1191eb633035STom Caputi 1192eb633035STom Caputi mutex_enter(&ds->ds_lock); 1193eb633035STom Caputi ds->ds_owner = FTAG; 1194eb633035STom Caputi mutex_exit(&ds->ds_lock); 1195eb633035STom Caputi 1196eb633035STom Caputi rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1197eb633035STom Caputi tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds, 1198eb633035STom Caputi tx->tx_txg); 1199eb633035STom Caputi if (tmpds != NULL) { 1200eb633035STom Caputi dsl_dataset_sync(ds, rzio, tx); 1201eb633035STom Caputi need_sync_done = B_TRUE; 1202eb633035STom Caputi } 1203eb633035STom Caputi VERIFY0(zio_wait(rzio)); 1204eb633035STom Caputi dmu_objset_do_userquota_updates(os, tx); 1205eb633035STom Caputi taskq_wait(dp->dp_sync_taskq); 1206eb633035STom Caputi if (txg_list_member(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 1207eb633035STom Caputi ASSERT3P(ds->ds_key_mapping, !=, NULL); 1208eb633035STom Caputi key_mapping_rele(spa, ds->ds_key_mapping, ds); 1209eb633035STom Caputi } 1210eb633035STom Caputi 1211eb633035STom Caputi rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1212eb633035STom Caputi tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds, 1213eb633035STom Caputi tx->tx_txg); 1214eb633035STom Caputi if (tmpds != NULL) { 1215eb633035STom Caputi dmu_buf_rele(ds->ds_dbuf, ds); 1216eb633035STom Caputi dsl_dataset_sync(ds, rzio, tx); 1217eb633035STom Caputi } 1218eb633035STom Caputi VERIFY0(zio_wait(rzio)); 1219eb633035STom Caputi 1220eb633035STom Caputi if (need_sync_done) { 1221eb633035STom Caputi ASSERT3P(ds->ds_key_mapping, !=, NULL); 1222eb633035STom Caputi key_mapping_rele(spa, ds->ds_key_mapping, ds); 1223eb633035STom Caputi dsl_dataset_sync_done(ds, tx); 1224eb633035STom Caputi } 1225eb633035STom Caputi 1226eb633035STom Caputi mutex_enter(&ds->ds_lock); 1227eb633035STom Caputi ds->ds_owner = NULL; 1228eb633035STom Caputi mutex_exit(&ds->ds_lock); 1229eb633035STom Caputi } 1230eb633035STom Caputi 12313b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "create", tx, ""); 1232eb633035STom Caputi dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG); 12333b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 1234fa9e4066Sahrens } 1235fa9e4066Sahrens 1236fa9e4066Sahrens int 1237ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 1238eb633035STom Caputi dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg) 1239fa9e4066Sahrens { 12403b2aab18SMatthew Ahrens dmu_objset_create_arg_t doca; 1241eb633035STom Caputi dsl_crypto_params_t tmp_dcp = { 0 }; 1242fa9e4066Sahrens 12433b2aab18SMatthew Ahrens doca.doca_name = name; 12443b2aab18SMatthew Ahrens doca.doca_cred = CRED(); 12453b2aab18SMatthew Ahrens doca.doca_flags = flags; 12463b2aab18SMatthew Ahrens doca.doca_userfunc = func; 12473b2aab18SMatthew Ahrens doca.doca_userarg = arg; 12483b2aab18SMatthew Ahrens doca.doca_type = type; 1249ecd6cf80Smarks 1250eb633035STom Caputi /* 1251eb633035STom Caputi * Some callers (mostly for testing) do not provide a dcp on their 1252eb633035STom Caputi * own but various code inside the sync task will require it to be 1253eb633035STom Caputi * allocated. Rather than adding NULL checks throughout this code 1254eb633035STom Caputi * or adding dummy dcp's to all of the callers we simply create a 1255eb633035STom Caputi * dummy one here and use that. This zero dcp will have the same 1256eb633035STom Caputi * effect as asking for inheritence of all encryption params. 1257eb633035STom Caputi */ 1258eb633035STom Caputi doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp; 1259eb633035STom Caputi 12603b2aab18SMatthew Ahrens return (dsl_sync_task(name, 12617d46dc6cSMatthew Ahrens dmu_objset_create_check, dmu_objset_create_sync, &doca, 1262eb633035STom Caputi 6, ZFS_SPACE_CHECK_NORMAL)); 1263ae46e4c7SMatthew Ahrens } 1264ae46e4c7SMatthew Ahrens 12653b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg { 12663b2aab18SMatthew Ahrens const char *doca_clone; 12673b2aab18SMatthew Ahrens const char *doca_origin; 12683b2aab18SMatthew Ahrens cred_t *doca_cred; 12693b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t; 12703b2aab18SMatthew Ahrens 12713b2aab18SMatthew Ahrens /*ARGSUSED*/ 12723b2aab18SMatthew Ahrens static int 12733b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx) 1274ae46e4c7SMatthew Ahrens { 12753b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg; 1276ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 1277ae46e4c7SMatthew Ahrens const char *tail; 12783b2aab18SMatthew Ahrens int error; 12793b2aab18SMatthew Ahrens dsl_dataset_t *origin; 12803b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 1281ae46e4c7SMatthew Ahrens 12823b2aab18SMatthew Ahrens if (strchr(doca->doca_clone, '@') != NULL) 1283be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 12843b2aab18SMatthew Ahrens 12859adfa60dSMatthew Ahrens if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN) 12869adfa60dSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 12879adfa60dSMatthew Ahrens 12883b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail); 12893b2aab18SMatthew Ahrens if (error != 0) 12903b2aab18SMatthew Ahrens return (error); 1291ae46e4c7SMatthew Ahrens if (tail == NULL) { 12923b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 1293be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 1294fa9e4066Sahrens } 129503b1c297SAlexander Eremin 1296a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 1297a2afb611SJerry Jelinek doca->doca_cred); 1298a2afb611SJerry Jelinek if (error != 0) { 1299a2afb611SJerry Jelinek dsl_dir_rele(pdd, FTAG); 1300a2afb611SJerry Jelinek return (SET_ERROR(EDQUOT)); 1301a2afb611SJerry Jelinek } 1302fa9e4066Sahrens 13033b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin); 1304eb633035STom Caputi if (error != 0) { 1305eb633035STom Caputi dsl_dir_rele(pdd, FTAG); 130699d5e173STim Haley return (error); 1307eb633035STom Caputi } 130899d5e173STim Haley 13093b2aab18SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 1310bc9014e6SJustin Gibbs if (!origin->ds_is_snapshot) { 13113b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 1312eb633035STom Caputi dsl_dir_rele(pdd, FTAG); 1313be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 131492241e0bSTom Erickson } 1315eb633035STom Caputi 13163b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 1317eb633035STom Caputi dsl_dir_rele(pdd, FTAG); 131899d5e173STim Haley 13193b2aab18SMatthew Ahrens return (0); 1320ea2f5b9eSMatthew Ahrens } 13211d452cf5Sahrens 13223b2aab18SMatthew Ahrens static void 13233b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx) 13241d452cf5Sahrens { 13253b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg; 13263b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 13273b2aab18SMatthew Ahrens dsl_dir_t *pdd; 13283b2aab18SMatthew Ahrens const char *tail; 13293b2aab18SMatthew Ahrens dsl_dataset_t *origin, *ds; 13303b2aab18SMatthew Ahrens uint64_t obj; 13319adfa60dSMatthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 13324445fffbSMatthew Ahrens 13333b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail)); 13343b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin)); 13354445fffbSMatthew Ahrens 13363b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, origin, 0, 1337eb633035STom Caputi doca->doca_cred, NULL, tx); 1338f2e10be3Srm 13393b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); 13403b2aab18SMatthew Ahrens dsl_dataset_name(origin, namebuf); 13413b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "clone", tx, 13423b2aab18SMatthew Ahrens "origin=%s (%llu)", namebuf, origin->ds_object); 13433b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 13443b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 13453b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 13461d452cf5Sahrens } 13471d452cf5Sahrens 13481d452cf5Sahrens int 13493b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin) 13501d452cf5Sahrens { 13513b2aab18SMatthew Ahrens dmu_objset_clone_arg_t doca; 13524445fffbSMatthew Ahrens 13533b2aab18SMatthew Ahrens doca.doca_clone = clone; 13543b2aab18SMatthew Ahrens doca.doca_origin = origin; 13553b2aab18SMatthew Ahrens doca.doca_cred = CRED(); 135699d5e173STim Haley 13573b2aab18SMatthew Ahrens return (dsl_sync_task(clone, 13587d46dc6cSMatthew Ahrens dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 1359eb633035STom Caputi 6, ZFS_SPACE_CHECK_NORMAL)); 13604445fffbSMatthew Ahrens } 13614445fffbSMatthew Ahrens 13625cabbc6bSPrashanth Sreenivasa static int 13635cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg) 13645cabbc6bSPrashanth Sreenivasa { 13655cabbc6bSPrashanth Sreenivasa int error = 0; 13665cabbc6bSPrashanth Sreenivasa uint64_t object = 0; 13675cabbc6bSPrashanth Sreenivasa while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 13685cabbc6bSPrashanth Sreenivasa error = dmu_object_remap_indirects(os, object, 13695cabbc6bSPrashanth Sreenivasa last_removed_txg); 13705cabbc6bSPrashanth Sreenivasa /* 13715cabbc6bSPrashanth Sreenivasa * If the ZPL removed the object before we managed to dnode_hold 13725cabbc6bSPrashanth Sreenivasa * it, we would get an ENOENT. If the ZPL declares its intent 13735cabbc6bSPrashanth Sreenivasa * to remove the object (dnode_free) before we manage to 13745cabbc6bSPrashanth Sreenivasa * dnode_hold it, we would get an EEXIST. In either case, we 13755cabbc6bSPrashanth Sreenivasa * want to continue remapping the other objects in the objset; 13765cabbc6bSPrashanth Sreenivasa * in all other cases, we want to break early. 13775cabbc6bSPrashanth Sreenivasa */ 13785cabbc6bSPrashanth Sreenivasa if (error != 0 && error != ENOENT && error != EEXIST) { 13795cabbc6bSPrashanth Sreenivasa break; 13805cabbc6bSPrashanth Sreenivasa } 13815cabbc6bSPrashanth Sreenivasa } 13825cabbc6bSPrashanth Sreenivasa if (error == ESRCH) { 13835cabbc6bSPrashanth Sreenivasa error = 0; 13845cabbc6bSPrashanth Sreenivasa } 13855cabbc6bSPrashanth Sreenivasa return (error); 13865cabbc6bSPrashanth Sreenivasa } 13875cabbc6bSPrashanth Sreenivasa 13885cabbc6bSPrashanth Sreenivasa int 13895cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects(const char *fsname) 13905cabbc6bSPrashanth Sreenivasa { 13915cabbc6bSPrashanth Sreenivasa int error = 0; 13925cabbc6bSPrashanth Sreenivasa objset_t *os = NULL; 13935cabbc6bSPrashanth Sreenivasa uint64_t last_removed_txg; 13945cabbc6bSPrashanth Sreenivasa uint64_t remap_start_txg; 13955cabbc6bSPrashanth Sreenivasa dsl_dir_t *dd; 13965cabbc6bSPrashanth Sreenivasa 13975cabbc6bSPrashanth Sreenivasa error = dmu_objset_hold(fsname, FTAG, &os); 13985cabbc6bSPrashanth Sreenivasa if (error != 0) { 13995cabbc6bSPrashanth Sreenivasa return (error); 14005cabbc6bSPrashanth Sreenivasa } 14015cabbc6bSPrashanth Sreenivasa dd = dmu_objset_ds(os)->ds_dir; 14025cabbc6bSPrashanth Sreenivasa 14035cabbc6bSPrashanth Sreenivasa if (!spa_feature_is_enabled(dmu_objset_spa(os), 14045cabbc6bSPrashanth Sreenivasa SPA_FEATURE_OBSOLETE_COUNTS)) { 14055cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 14065cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENOTSUP)); 14075cabbc6bSPrashanth Sreenivasa } 14085cabbc6bSPrashanth Sreenivasa 14095cabbc6bSPrashanth Sreenivasa if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) { 14105cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 14115cabbc6bSPrashanth Sreenivasa return (SET_ERROR(EINVAL)); 14125cabbc6bSPrashanth Sreenivasa } 14135cabbc6bSPrashanth Sreenivasa 14145cabbc6bSPrashanth Sreenivasa /* 14155cabbc6bSPrashanth Sreenivasa * If there has not been a removal, we're done. 14165cabbc6bSPrashanth Sreenivasa */ 14175cabbc6bSPrashanth Sreenivasa last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os)); 14185cabbc6bSPrashanth Sreenivasa if (last_removed_txg == -1ULL) { 14195cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 14205cabbc6bSPrashanth Sreenivasa return (0); 14215cabbc6bSPrashanth Sreenivasa } 14225cabbc6bSPrashanth Sreenivasa 14235cabbc6bSPrashanth Sreenivasa /* 14245cabbc6bSPrashanth Sreenivasa * If we have remapped since the last removal, we're done. 14255cabbc6bSPrashanth Sreenivasa */ 14265cabbc6bSPrashanth Sreenivasa if (dsl_dir_is_zapified(dd)) { 14275cabbc6bSPrashanth Sreenivasa uint64_t last_remap_txg; 14285cabbc6bSPrashanth Sreenivasa if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)), 14295cabbc6bSPrashanth Sreenivasa dd->dd_object, DD_FIELD_LAST_REMAP_TXG, 14305cabbc6bSPrashanth Sreenivasa sizeof (last_remap_txg), 1, &last_remap_txg) == 0 && 14315cabbc6bSPrashanth Sreenivasa last_remap_txg > last_removed_txg) { 14325cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 14335cabbc6bSPrashanth Sreenivasa return (0); 14345cabbc6bSPrashanth Sreenivasa } 14355cabbc6bSPrashanth Sreenivasa } 14365cabbc6bSPrashanth Sreenivasa 14375cabbc6bSPrashanth Sreenivasa dsl_dataset_long_hold(dmu_objset_ds(os), FTAG); 14385cabbc6bSPrashanth Sreenivasa dsl_pool_rele(dmu_objset_pool(os), FTAG); 14395cabbc6bSPrashanth Sreenivasa 14405cabbc6bSPrashanth Sreenivasa remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os)); 14415cabbc6bSPrashanth Sreenivasa error = dmu_objset_remap_indirects_impl(os, last_removed_txg); 14425cabbc6bSPrashanth Sreenivasa if (error == 0) { 14435cabbc6bSPrashanth Sreenivasa /* 14445cabbc6bSPrashanth Sreenivasa * We update the last_remap_txg to be the start txg so that 14455cabbc6bSPrashanth Sreenivasa * we can guarantee that every block older than last_remap_txg 14465cabbc6bSPrashanth Sreenivasa * that can be remapped has been remapped. 14475cabbc6bSPrashanth Sreenivasa */ 14485cabbc6bSPrashanth Sreenivasa error = dsl_dir_update_last_remap_txg(dd, remap_start_txg); 14495cabbc6bSPrashanth Sreenivasa } 14505cabbc6bSPrashanth Sreenivasa 14515cabbc6bSPrashanth Sreenivasa dsl_dataset_long_rele(dmu_objset_ds(os), FTAG); 14525cabbc6bSPrashanth Sreenivasa dsl_dataset_rele(dmu_objset_ds(os), FTAG); 14535cabbc6bSPrashanth Sreenivasa 14545cabbc6bSPrashanth Sreenivasa return (error); 14555cabbc6bSPrashanth Sreenivasa } 14565cabbc6bSPrashanth Sreenivasa 14574445fffbSMatthew Ahrens int 14584445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname) 14594445fffbSMatthew Ahrens { 14604445fffbSMatthew Ahrens int err; 14614445fffbSMatthew Ahrens char *longsnap = kmem_asprintf("%s@%s", fsname, snapname); 14624445fffbSMatthew Ahrens nvlist_t *snaps = fnvlist_alloc(); 14634445fffbSMatthew Ahrens 14644445fffbSMatthew Ahrens fnvlist_add_boolean(snaps, longsnap); 14654445fffbSMatthew Ahrens strfree(longsnap); 14663b2aab18SMatthew Ahrens err = dsl_dataset_snapshot(snaps, NULL, NULL); 14673b2aab18SMatthew Ahrens fnvlist_free(snaps); 14684445fffbSMatthew Ahrens return (err); 14694445fffbSMatthew Ahrens } 14704445fffbSMatthew Ahrens 1471*f67950b2SNasf-Fan static void 1472*f67950b2SNasf-Fan dmu_objset_upgrade_task_cb(void *data) 1473*f67950b2SNasf-Fan { 1474*f67950b2SNasf-Fan objset_t *os = data; 1475*f67950b2SNasf-Fan 1476*f67950b2SNasf-Fan mutex_enter(&os->os_upgrade_lock); 1477*f67950b2SNasf-Fan os->os_upgrade_status = EINTR; 1478*f67950b2SNasf-Fan if (!os->os_upgrade_exit) { 1479*f67950b2SNasf-Fan mutex_exit(&os->os_upgrade_lock); 1480*f67950b2SNasf-Fan 1481*f67950b2SNasf-Fan os->os_upgrade_status = os->os_upgrade_cb(os); 1482*f67950b2SNasf-Fan mutex_enter(&os->os_upgrade_lock); 1483*f67950b2SNasf-Fan } 1484*f67950b2SNasf-Fan os->os_upgrade_exit = B_TRUE; 1485*f67950b2SNasf-Fan os->os_upgrade_id = 0; 1486*f67950b2SNasf-Fan mutex_exit(&os->os_upgrade_lock); 1487*f67950b2SNasf-Fan } 1488*f67950b2SNasf-Fan 1489*f67950b2SNasf-Fan static void 1490*f67950b2SNasf-Fan dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb) 1491*f67950b2SNasf-Fan { 1492*f67950b2SNasf-Fan if (os->os_upgrade_id != 0) 1493*f67950b2SNasf-Fan return; 1494*f67950b2SNasf-Fan 1495*f67950b2SNasf-Fan mutex_enter(&os->os_upgrade_lock); 1496*f67950b2SNasf-Fan if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) { 1497*f67950b2SNasf-Fan os->os_upgrade_exit = B_FALSE; 1498*f67950b2SNasf-Fan os->os_upgrade_cb = cb; 1499*f67950b2SNasf-Fan os->os_upgrade_id = taskq_dispatch( 1500*f67950b2SNasf-Fan os->os_spa->spa_upgrade_taskq, 1501*f67950b2SNasf-Fan dmu_objset_upgrade_task_cb, os, TQ_SLEEP); 1502*f67950b2SNasf-Fan if (os->os_upgrade_id == 0) 1503*f67950b2SNasf-Fan os->os_upgrade_status = ENOMEM; 1504*f67950b2SNasf-Fan } 1505*f67950b2SNasf-Fan mutex_exit(&os->os_upgrade_lock); 1506*f67950b2SNasf-Fan } 1507*f67950b2SNasf-Fan 1508*f67950b2SNasf-Fan static void 1509*f67950b2SNasf-Fan dmu_objset_upgrade_stop(objset_t *os) 1510*f67950b2SNasf-Fan { 1511*f67950b2SNasf-Fan mutex_enter(&os->os_upgrade_lock); 1512*f67950b2SNasf-Fan os->os_upgrade_exit = B_TRUE; 1513*f67950b2SNasf-Fan if (os->os_upgrade_id != 0) { 1514*f67950b2SNasf-Fan os->os_upgrade_id = 0; 1515*f67950b2SNasf-Fan mutex_exit(&os->os_upgrade_lock); 1516*f67950b2SNasf-Fan 1517*f67950b2SNasf-Fan taskq_wait(os->os_spa->spa_upgrade_taskq); 1518*f67950b2SNasf-Fan } else { 1519*f67950b2SNasf-Fan mutex_exit(&os->os_upgrade_lock); 1520*f67950b2SNasf-Fan } 1521*f67950b2SNasf-Fan } 1522*f67950b2SNasf-Fan 1523fa9e4066Sahrens static void 152494c2d0ebSMatthew Ahrens dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx) 1525fa9e4066Sahrens { 1526c717a561Smaybee dnode_t *dn; 1527faafa6e3Sahrens 152894c2d0ebSMatthew Ahrens while ((dn = multilist_sublist_head(list)) != NULL) { 1529c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 1530c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 1531c717a561Smaybee /* 153214843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 153314843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 1534c717a561Smaybee */ 1535c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 1536c717a561Smaybee ASSERT(dn->dn_zio); 1537faafa6e3Sahrens 1538c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 153994c2d0ebSMatthew Ahrens multilist_sublist_remove(list, dn); 154014843421SMatthew Ahrens 1541aa02ea01STom Caputi /* 1542aa02ea01STom Caputi * If we are not doing useraccounting (os_synced_dnodes == NULL) 1543aa02ea01STom Caputi * we are done with this dnode for this txg. Unset dn_dirty_txg 1544aa02ea01STom Caputi * if later txgs aren't dirtying it so that future holders do 1545aa02ea01STom Caputi * not get a stale value. Otherwise, we will do this in 1546aa02ea01STom Caputi * userquota_updates_task() when processing has completely 1547aa02ea01STom Caputi * finished for this txg. 1548aa02ea01STom Caputi */ 154994c2d0ebSMatthew Ahrens multilist_t *newlist = dn->dn_objset->os_synced_dnodes; 155094c2d0ebSMatthew Ahrens if (newlist != NULL) { 155114843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 155294c2d0ebSMatthew Ahrens multilist_insert(newlist, dn); 1553aa02ea01STom Caputi } else { 1554aa02ea01STom Caputi mutex_enter(&dn->dn_mtx); 1555aa02ea01STom Caputi if (dn->dn_dirty_txg == tx->tx_txg) 1556aa02ea01STom Caputi dn->dn_dirty_txg = 0; 1557aa02ea01STom Caputi mutex_exit(&dn->dn_mtx); 155814843421SMatthew Ahrens } 155914843421SMatthew Ahrens 1560c717a561Smaybee dnode_sync(dn, tx); 1561fa9e4066Sahrens } 1562fa9e4066Sahrens } 1563fa9e4066Sahrens 1564fa9e4066Sahrens /* ARGSUSED */ 1565fa9e4066Sahrens static void 1566b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 1567fa9e4066Sahrens { 1568e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 1569503ad85cSMatthew Ahrens objset_t *os = arg; 1570c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 1571eb633035STom Caputi uint64_t fill = 0; 1572fa9e4066Sahrens 15735d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); 15743b2aab18SMatthew Ahrens ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET); 15750a4e9518Sgw 1576fa9e4066Sahrens /* 157714843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 157814843421SMatthew Ahrens * allocated in the object set (not counting the "special" 157914843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 1580*f67950b2SNasf-Fan * dnode and user/group/project accounting objects). 1581fa9e4066Sahrens */ 1582e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 1583eb633035STom Caputi fill += BP_GET_FILL(&dnp->dn_blkptr[i]); 1584eb633035STom Caputi 1585eb633035STom Caputi BP_SET_FILL(bp, fill); 1586eb633035STom Caputi 1587c166b69dSPaul Dagnelie if (os->os_dsl_dataset != NULL) 1588c166b69dSPaul Dagnelie rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG); 1589c166b69dSPaul Dagnelie *os->os_rootbp = *bp; 1590c166b69dSPaul Dagnelie if (os->os_dsl_dataset != NULL) 1591c166b69dSPaul Dagnelie rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG); 1592b24ab676SJeff Bonwick } 1593b24ab676SJeff Bonwick 1594b24ab676SJeff Bonwick /* ARGSUSED */ 1595b24ab676SJeff Bonwick static void 1596b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1597b24ab676SJeff Bonwick { 1598b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 1599b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 1600b24ab676SJeff Bonwick objset_t *os = arg; 16010a4e9518Sgw 1602e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1603b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig)); 1604e14bb325SJeff Bonwick } else { 1605b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 1606b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx; 1607b24ab676SJeff Bonwick 1608b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1609b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx); 16100a4e9518Sgw } 1611c166b69dSPaul Dagnelie kmem_free(bp, sizeof (*bp)); 1612c717a561Smaybee } 1613c717a561Smaybee 161494c2d0ebSMatthew Ahrens typedef struct sync_dnodes_arg { 161594c2d0ebSMatthew Ahrens multilist_t *sda_list; 161694c2d0ebSMatthew Ahrens int sda_sublist_idx; 161794c2d0ebSMatthew Ahrens multilist_t *sda_newlist; 161894c2d0ebSMatthew Ahrens dmu_tx_t *sda_tx; 161994c2d0ebSMatthew Ahrens } sync_dnodes_arg_t; 162094c2d0ebSMatthew Ahrens 162194c2d0ebSMatthew Ahrens static void 162294c2d0ebSMatthew Ahrens sync_dnodes_task(void *arg) 162394c2d0ebSMatthew Ahrens { 162494c2d0ebSMatthew Ahrens sync_dnodes_arg_t *sda = arg; 162594c2d0ebSMatthew Ahrens 162694c2d0ebSMatthew Ahrens multilist_sublist_t *ms = 162794c2d0ebSMatthew Ahrens multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx); 162894c2d0ebSMatthew Ahrens 162994c2d0ebSMatthew Ahrens dmu_objset_sync_dnodes(ms, sda->sda_tx); 163094c2d0ebSMatthew Ahrens 163194c2d0ebSMatthew Ahrens multilist_sublist_unlock(ms); 163294c2d0ebSMatthew Ahrens 163394c2d0ebSMatthew Ahrens kmem_free(sda, sizeof (*sda)); 163494c2d0ebSMatthew Ahrens } 163594c2d0ebSMatthew Ahrens 163694c2d0ebSMatthew Ahrens 1637fa9e4066Sahrens /* called from dsl */ 1638fa9e4066Sahrens void 1639503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1640fa9e4066Sahrens { 1641fa9e4066Sahrens int txgoff; 16427802d7bfSMatthew Ahrens zbookmark_phys_t zb; 1643b24ab676SJeff Bonwick zio_prop_t zp; 1644c717a561Smaybee zio_t *zio; 1645c717a561Smaybee list_t *list; 1646c717a561Smaybee dbuf_dirty_record_t *dr; 1647c166b69dSPaul Dagnelie blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP); 1648c166b69dSPaul Dagnelie *blkptr_copy = *os->os_rootbp; 1649c717a561Smaybee 1650c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1651fa9e4066Sahrens 1652fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1653fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 1654fa9e4066Sahrens os->os_synctx = tx; 1655fa9e4066Sahrens 165687bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 165787bd5c1eSahrens /* 165887bd5c1eSahrens * This is the MOS. If we have upgraded, 165987bd5c1eSahrens * spa_max_replication() could change, so reset 166087bd5c1eSahrens * os_copies here. 166187bd5c1eSahrens */ 166287bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 166387bd5c1eSahrens } 166487bd5c1eSahrens 1665fa9e4066Sahrens /* 1666c717a561Smaybee * Create the root block IO 1667fa9e4066Sahrens */ 1668b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1669b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1670b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 16711b912ec7SGeorge Wilson arc_release(os->os_phys_buf, &os->os_phys_buf); 1672b24ab676SJeff Bonwick 1673adaec86aSMatthew Ahrens dmu_write_policy(os, NULL, 0, 0, &zp); 1674b24ab676SJeff Bonwick 1675eb633035STom Caputi /* 1676eb633035STom Caputi * If we are either claiming the ZIL or doing a raw receive, write 1677eb633035STom Caputi * out the os_phys_buf raw. Neither of these actions will effect the 1678eb633035STom Caputi * MAC at this point. 1679eb633035STom Caputi */ 1680eb633035STom Caputi if (os->os_raw_receive || 1681eb633035STom Caputi os->os_next_write_raw[tx->tx_txg & TXG_MASK]) { 1682eb633035STom Caputi ASSERT(os->os_encrypted); 1683eb633035STom Caputi arc_convert_to_raw(os->os_phys_buf, 1684eb633035STom Caputi os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER, 1685eb633035STom Caputi DMU_OT_OBJSET, NULL, NULL, NULL); 1686eb633035STom Caputi } 1687eb633035STom Caputi 1688b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg, 1689c166b69dSPaul Dagnelie blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), 16908df0bcf0SPaul Dagnelie &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done, 16918df0bcf0SPaul Dagnelie os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1692c717a561Smaybee 1693c717a561Smaybee /* 169414843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 1695c717a561Smaybee */ 1696744947dcSTom Erickson DMU_META_DNODE(os)->dn_zio = zio; 1697744947dcSTom Erickson dnode_sync(DMU_META_DNODE(os), tx); 1698c717a561Smaybee 169914843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 170014843421SMatthew Ahrens 1701744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) && 1702744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1703744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_zio = zio; 1704744947dcSTom Erickson dnode_sync(DMU_USERUSED_DNODE(os), tx); 1705744947dcSTom Erickson DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1706744947dcSTom Erickson dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 170714843421SMatthew Ahrens } 170814843421SMatthew Ahrens 1709*f67950b2SNasf-Fan if (DMU_PROJECTUSED_DNODE(os) && 1710*f67950b2SNasf-Fan DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1711*f67950b2SNasf-Fan DMU_PROJECTUSED_DNODE(os)->dn_zio = zio; 1712*f67950b2SNasf-Fan dnode_sync(DMU_PROJECTUSED_DNODE(os), tx); 1713*f67950b2SNasf-Fan } 1714*f67950b2SNasf-Fan 1715c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 1716fa9e4066Sahrens 1717eb633035STom Caputi if (dmu_objset_userused_enabled(os) && 1718eb633035STom Caputi (!os->os_encrypted || !dmu_objset_is_receiving(os))) { 171914843421SMatthew Ahrens /* 172014843421SMatthew Ahrens * We must create the list here because it uses the 172194c2d0ebSMatthew Ahrens * dn_dirty_link[] of this txg. But it may already 172294c2d0ebSMatthew Ahrens * exist because we call dsl_dataset_sync() twice per txg. 172314843421SMatthew Ahrens */ 172494c2d0ebSMatthew Ahrens if (os->os_synced_dnodes == NULL) { 172594c2d0ebSMatthew Ahrens os->os_synced_dnodes = 172694c2d0ebSMatthew Ahrens multilist_create(sizeof (dnode_t), 172794c2d0ebSMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff]), 172894c2d0ebSMatthew Ahrens dnode_multilist_index_func); 172994c2d0ebSMatthew Ahrens } else { 173094c2d0ebSMatthew Ahrens ASSERT3U(os->os_synced_dnodes->ml_offset, ==, 173194c2d0ebSMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 173294c2d0ebSMatthew Ahrens } 173314843421SMatthew Ahrens } 173414843421SMatthew Ahrens 173594c2d0ebSMatthew Ahrens for (int i = 0; 173694c2d0ebSMatthew Ahrens i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) { 173794c2d0ebSMatthew Ahrens sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP); 173894c2d0ebSMatthew Ahrens sda->sda_list = os->os_dirty_dnodes[txgoff]; 173994c2d0ebSMatthew Ahrens sda->sda_sublist_idx = i; 174094c2d0ebSMatthew Ahrens sda->sda_tx = tx; 174194c2d0ebSMatthew Ahrens (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, 174294c2d0ebSMatthew Ahrens sync_dnodes_task, sda, 0); 174394c2d0ebSMatthew Ahrens /* callback frees sda */ 174494c2d0ebSMatthew Ahrens } 174594c2d0ebSMatthew Ahrens taskq_wait(dmu_objset_pool(os)->dp_sync_taskq); 1746fa9e4066Sahrens 1747744947dcSTom Erickson list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 174894c2d0ebSMatthew Ahrens while ((dr = list_head(list)) != NULL) { 17493b2aab18SMatthew Ahrens ASSERT0(dr->dr_dbuf->db_level); 1750c717a561Smaybee list_remove(list, dr); 1751c717a561Smaybee if (dr->dr_zio) 1752c717a561Smaybee zio_nowait(dr->dr_zio); 1753c717a561Smaybee } 1754af346df5SNed Bass 1755af346df5SNed Bass /* Enable dnode backfill if enough objects have been freed. */ 1756af346df5SNed Bass if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) { 1757af346df5SNed Bass os->os_rescan_dnodes = B_TRUE; 1758af346df5SNed Bass os->os_freed_dnodes = 0; 1759af346df5SNed Bass } 1760af346df5SNed Bass 1761c717a561Smaybee /* 1762c717a561Smaybee * Free intent log blocks up to this tx. 1763c717a561Smaybee */ 1764c717a561Smaybee zil_sync(os->os_zil, tx); 1765088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 1766c717a561Smaybee zio_nowait(zio); 1767fa9e4066Sahrens } 1768fa9e4066Sahrens 1769b24ab676SJeff Bonwick boolean_t 1770b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1771b24ab676SJeff Bonwick { 177294c2d0ebSMatthew Ahrens return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK])); 1773b24ab676SJeff Bonwick } 1774b24ab676SJeff Bonwick 1775744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 177614843421SMatthew Ahrens 177714843421SMatthew Ahrens void 177814843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 177914843421SMatthew Ahrens { 178014843421SMatthew Ahrens used_cbs[ost] = cb; 178114843421SMatthew Ahrens } 178214843421SMatthew Ahrens 178314843421SMatthew Ahrens boolean_t 1784503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 178514843421SMatthew Ahrens { 178614843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1787744947dcSTom Erickson used_cbs[os->os_phys->os_type] != NULL && 1788744947dcSTom Erickson DMU_USERUSED_DNODE(os) != NULL); 178914843421SMatthew Ahrens } 179014843421SMatthew Ahrens 1791*f67950b2SNasf-Fan boolean_t 1792*f67950b2SNasf-Fan dmu_objset_userobjused_enabled(objset_t *os) 1793*f67950b2SNasf-Fan { 1794*f67950b2SNasf-Fan return (dmu_objset_userused_enabled(os) && 1795*f67950b2SNasf-Fan spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING)); 1796*f67950b2SNasf-Fan } 1797*f67950b2SNasf-Fan 1798*f67950b2SNasf-Fan boolean_t 1799*f67950b2SNasf-Fan dmu_objset_projectquota_enabled(objset_t *os) 1800*f67950b2SNasf-Fan { 1801*f67950b2SNasf-Fan return (used_cbs[os->os_phys->os_type] != NULL && 1802*f67950b2SNasf-Fan DMU_PROJECTUSED_DNODE(os) != NULL && 1803*f67950b2SNasf-Fan spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA)); 1804*f67950b2SNasf-Fan } 1805*f67950b2SNasf-Fan 18068b70546bSMatthew Ahrens typedef struct userquota_node { 1807*f67950b2SNasf-Fan /* must be in the first field, see userquota_update_cache() */ 1808*f67950b2SNasf-Fan char uqn_id[20 + DMU_OBJACCT_PREFIX_LEN]; 1809*f67950b2SNasf-Fan int64_t uqn_delta; 1810*f67950b2SNasf-Fan avl_node_t uqn_node; 18118b70546bSMatthew Ahrens } userquota_node_t; 18128b70546bSMatthew Ahrens 18138b70546bSMatthew Ahrens typedef struct userquota_cache { 18148b70546bSMatthew Ahrens avl_tree_t uqc_user_deltas; 18158b70546bSMatthew Ahrens avl_tree_t uqc_group_deltas; 1816*f67950b2SNasf-Fan avl_tree_t uqc_project_deltas; 18178b70546bSMatthew Ahrens } userquota_cache_t; 18188b70546bSMatthew Ahrens 18198b70546bSMatthew Ahrens static int 18208b70546bSMatthew Ahrens userquota_compare(const void *l, const void *r) 18218b70546bSMatthew Ahrens { 18228b70546bSMatthew Ahrens const userquota_node_t *luqn = l; 18238b70546bSMatthew Ahrens const userquota_node_t *ruqn = r; 1824*f67950b2SNasf-Fan int rv; 18258b70546bSMatthew Ahrens 1826*f67950b2SNasf-Fan /* 1827*f67950b2SNasf-Fan * NB: can only access uqn_id because userquota_update_cache() doesn't 1828*f67950b2SNasf-Fan * pass in an entire userquota_node_t. 1829*f67950b2SNasf-Fan */ 1830*f67950b2SNasf-Fan rv = strcmp(luqn->uqn_id, ruqn->uqn_id); 1831*f67950b2SNasf-Fan 1832*f67950b2SNasf-Fan return (AVL_ISIGN(rv)); 18338b70546bSMatthew Ahrens } 18348b70546bSMatthew Ahrens 18359966ca11SMatthew Ahrens static void 18368b70546bSMatthew Ahrens do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx) 18378b70546bSMatthew Ahrens { 18388b70546bSMatthew Ahrens void *cookie; 18398b70546bSMatthew Ahrens userquota_node_t *uqn; 18408b70546bSMatthew Ahrens 18418b70546bSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 18428b70546bSMatthew Ahrens 18438b70546bSMatthew Ahrens cookie = NULL; 18448b70546bSMatthew Ahrens while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas, 18458b70546bSMatthew Ahrens &cookie)) != NULL) { 184694c2d0ebSMatthew Ahrens /* 184794c2d0ebSMatthew Ahrens * os_userused_lock protects against concurrent calls to 184894c2d0ebSMatthew Ahrens * zap_increment_int(). It's needed because zap_increment_int() 184994c2d0ebSMatthew Ahrens * is not thread-safe (i.e. not atomic). 185094c2d0ebSMatthew Ahrens */ 185194c2d0ebSMatthew Ahrens mutex_enter(&os->os_userused_lock); 1852*f67950b2SNasf-Fan VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT, 18538b70546bSMatthew Ahrens uqn->uqn_id, uqn->uqn_delta, tx)); 185494c2d0ebSMatthew Ahrens mutex_exit(&os->os_userused_lock); 18558b70546bSMatthew Ahrens kmem_free(uqn, sizeof (*uqn)); 18568b70546bSMatthew Ahrens } 18578b70546bSMatthew Ahrens avl_destroy(&cache->uqc_user_deltas); 18588b70546bSMatthew Ahrens 18598b70546bSMatthew Ahrens cookie = NULL; 18608b70546bSMatthew Ahrens while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas, 18618b70546bSMatthew Ahrens &cookie)) != NULL) { 186294c2d0ebSMatthew Ahrens mutex_enter(&os->os_userused_lock); 1863*f67950b2SNasf-Fan VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT, 18648b70546bSMatthew Ahrens uqn->uqn_id, uqn->uqn_delta, tx)); 186594c2d0ebSMatthew Ahrens mutex_exit(&os->os_userused_lock); 18668b70546bSMatthew Ahrens kmem_free(uqn, sizeof (*uqn)); 18678b70546bSMatthew Ahrens } 18688b70546bSMatthew Ahrens avl_destroy(&cache->uqc_group_deltas); 1869*f67950b2SNasf-Fan 1870*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os)) { 1871*f67950b2SNasf-Fan cookie = NULL; 1872*f67950b2SNasf-Fan while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas, 1873*f67950b2SNasf-Fan &cookie)) != NULL) { 1874*f67950b2SNasf-Fan mutex_enter(&os->os_userused_lock); 1875*f67950b2SNasf-Fan VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT, 1876*f67950b2SNasf-Fan uqn->uqn_id, uqn->uqn_delta, tx)); 1877*f67950b2SNasf-Fan mutex_exit(&os->os_userused_lock); 1878*f67950b2SNasf-Fan kmem_free(uqn, sizeof (*uqn)); 1879*f67950b2SNasf-Fan } 1880*f67950b2SNasf-Fan avl_destroy(&cache->uqc_project_deltas); 1881*f67950b2SNasf-Fan } 18828b70546bSMatthew Ahrens } 18838b70546bSMatthew Ahrens 18848b70546bSMatthew Ahrens static void 1885*f67950b2SNasf-Fan userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta) 18868b70546bSMatthew Ahrens { 1887*f67950b2SNasf-Fan userquota_node_t *uqn; 18888b70546bSMatthew Ahrens avl_index_t idx; 18898b70546bSMatthew Ahrens 1890*f67950b2SNasf-Fan ASSERT(strlen(id) < sizeof (uqn->uqn_id)); 1891*f67950b2SNasf-Fan /* 1892*f67950b2SNasf-Fan * Use id directly for searching because uqn_id is the first field of 1893*f67950b2SNasf-Fan * userquota_node_t and fields after uqn_id won't be accessed in 1894*f67950b2SNasf-Fan * avl_find(). 1895*f67950b2SNasf-Fan */ 1896*f67950b2SNasf-Fan uqn = avl_find(avl, (const void *)id, &idx); 18978b70546bSMatthew Ahrens if (uqn == NULL) { 18988b70546bSMatthew Ahrens uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP); 1899*f67950b2SNasf-Fan (void) strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id)); 19008b70546bSMatthew Ahrens avl_insert(avl, uqn, idx); 19018b70546bSMatthew Ahrens } 19028b70546bSMatthew Ahrens uqn->uqn_delta += delta; 19038b70546bSMatthew Ahrens } 19048b70546bSMatthew Ahrens 19058b70546bSMatthew Ahrens static void 1906*f67950b2SNasf-Fan do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used, 1907*f67950b2SNasf-Fan uint64_t flags, uint64_t user, uint64_t group, uint64_t project, 1908*f67950b2SNasf-Fan boolean_t subtract) 19099966ca11SMatthew Ahrens { 19100a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 191154811da5SToomas Soome int64_t delta = DNODE_MIN_SIZE + used; 1912*f67950b2SNasf-Fan char name[20]; 1913*f67950b2SNasf-Fan 19149966ca11SMatthew Ahrens if (subtract) 19159966ca11SMatthew Ahrens delta = -delta; 19168b70546bSMatthew Ahrens 1917*f67950b2SNasf-Fan (void) sprintf(name, "%llx", (longlong_t)user); 1918*f67950b2SNasf-Fan userquota_update_cache(&cache->uqc_user_deltas, name, delta); 1919*f67950b2SNasf-Fan 1920*f67950b2SNasf-Fan (void) sprintf(name, "%llx", (longlong_t)group); 1921*f67950b2SNasf-Fan userquota_update_cache(&cache->uqc_group_deltas, name, delta); 1922*f67950b2SNasf-Fan 1923*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os)) { 1924*f67950b2SNasf-Fan (void) sprintf(name, "%llx", (longlong_t)project); 1925*f67950b2SNasf-Fan userquota_update_cache(&cache->uqc_project_deltas, 1926*f67950b2SNasf-Fan name, delta); 1927*f67950b2SNasf-Fan } 1928*f67950b2SNasf-Fan } 1929*f67950b2SNasf-Fan } 1930*f67950b2SNasf-Fan 1931*f67950b2SNasf-Fan static void 1932*f67950b2SNasf-Fan do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags, 1933*f67950b2SNasf-Fan uint64_t user, uint64_t group, uint64_t project, boolean_t subtract) 1934*f67950b2SNasf-Fan { 1935*f67950b2SNasf-Fan if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) { 1936*f67950b2SNasf-Fan char name[20 + DMU_OBJACCT_PREFIX_LEN]; 1937*f67950b2SNasf-Fan int delta = subtract ? -1 : 1; 1938*f67950b2SNasf-Fan 1939*f67950b2SNasf-Fan (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx", 1940*f67950b2SNasf-Fan (longlong_t)user); 1941*f67950b2SNasf-Fan userquota_update_cache(&cache->uqc_user_deltas, name, delta); 1942*f67950b2SNasf-Fan 1943*f67950b2SNasf-Fan (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx", 1944*f67950b2SNasf-Fan (longlong_t)group); 1945*f67950b2SNasf-Fan userquota_update_cache(&cache->uqc_group_deltas, name, delta); 1946*f67950b2SNasf-Fan 1947*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os)) { 1948*f67950b2SNasf-Fan (void) snprintf(name, sizeof (name), 1949*f67950b2SNasf-Fan DMU_OBJACCT_PREFIX "%llx", (longlong_t)project); 1950*f67950b2SNasf-Fan userquota_update_cache(&cache->uqc_project_deltas, 1951*f67950b2SNasf-Fan name, delta); 1952*f67950b2SNasf-Fan } 19539966ca11SMatthew Ahrens } 19549966ca11SMatthew Ahrens } 19559966ca11SMatthew Ahrens 195694c2d0ebSMatthew Ahrens typedef struct userquota_updates_arg { 195794c2d0ebSMatthew Ahrens objset_t *uua_os; 195894c2d0ebSMatthew Ahrens int uua_sublist_idx; 195994c2d0ebSMatthew Ahrens dmu_tx_t *uua_tx; 196094c2d0ebSMatthew Ahrens } userquota_updates_arg_t; 196194c2d0ebSMatthew Ahrens 196294c2d0ebSMatthew Ahrens static void 196394c2d0ebSMatthew Ahrens userquota_updates_task(void *arg) 196414843421SMatthew Ahrens { 196594c2d0ebSMatthew Ahrens userquota_updates_arg_t *uua = arg; 196694c2d0ebSMatthew Ahrens objset_t *os = uua->uua_os; 196794c2d0ebSMatthew Ahrens dmu_tx_t *tx = uua->uua_tx; 196814843421SMatthew Ahrens dnode_t *dn; 19698b70546bSMatthew Ahrens userquota_cache_t cache = { 0 }; 197014843421SMatthew Ahrens 197194c2d0ebSMatthew Ahrens multilist_sublist_t *list = 197294c2d0ebSMatthew Ahrens multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx); 197314843421SMatthew Ahrens 197494c2d0ebSMatthew Ahrens ASSERT(multilist_sublist_head(list) == NULL || 197594c2d0ebSMatthew Ahrens dmu_objset_userused_enabled(os)); 19768b70546bSMatthew Ahrens avl_create(&cache.uqc_user_deltas, userquota_compare, 19778b70546bSMatthew Ahrens sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); 19788b70546bSMatthew Ahrens avl_create(&cache.uqc_group_deltas, userquota_compare, 19798b70546bSMatthew Ahrens sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); 1980*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os)) 1981*f67950b2SNasf-Fan avl_create(&cache.uqc_project_deltas, userquota_compare, 1982*f67950b2SNasf-Fan sizeof (userquota_node_t), offsetof(userquota_node_t, 1983*f67950b2SNasf-Fan uqn_node)); 19848b70546bSMatthew Ahrens 198594c2d0ebSMatthew Ahrens while ((dn = multilist_sublist_head(list)) != NULL) { 19861d8ccc7bSMark Shellenbaum int flags; 198714843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 198814843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 198914843421SMatthew Ahrens dn->dn_phys->dn_flags & 199014843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 199114843421SMatthew Ahrens 19921d8ccc7bSMark Shellenbaum flags = dn->dn_id_flags; 19931d8ccc7bSMark Shellenbaum ASSERT(flags); 19941d8ccc7bSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 1995*f67950b2SNasf-Fan do_userquota_update(os, &cache, dn->dn_oldused, 1996*f67950b2SNasf-Fan dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid, 1997*f67950b2SNasf-Fan dn->dn_oldprojid, B_TRUE); 1998*f67950b2SNasf-Fan do_userobjquota_update(os, &cache, dn->dn_oldflags, 1999*f67950b2SNasf-Fan dn->dn_olduid, dn->dn_oldgid, 2000*f67950b2SNasf-Fan dn->dn_oldprojid, B_TRUE); 20010a586ceaSMark Shellenbaum } 20021d8ccc7bSMark Shellenbaum if (flags & DN_ID_NEW_EXIST) { 2003*f67950b2SNasf-Fan do_userquota_update(os, &cache, 2004*f67950b2SNasf-Fan DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags, 2005*f67950b2SNasf-Fan dn->dn_newuid, dn->dn_newgid, 2006*f67950b2SNasf-Fan dn->dn_newprojid, B_FALSE); 2007*f67950b2SNasf-Fan do_userobjquota_update(os, &cache, 2008*f67950b2SNasf-Fan dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid, 2009*f67950b2SNasf-Fan dn->dn_newprojid, B_FALSE); 20100a586ceaSMark Shellenbaum } 20110a586ceaSMark Shellenbaum 20121d8ccc7bSMark Shellenbaum mutex_enter(&dn->dn_mtx); 20130a586ceaSMark Shellenbaum dn->dn_oldused = 0; 20140a586ceaSMark Shellenbaum dn->dn_oldflags = 0; 20150a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 20160a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid; 20170a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid; 2018*f67950b2SNasf-Fan dn->dn_oldprojid = dn->dn_newprojid; 20190a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 20200a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0) 20210a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 20220a586ceaSMark Shellenbaum else 20230a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 20240a586ceaSMark Shellenbaum } 202528d97a71SMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 2026aa02ea01STom Caputi if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa)) 2027aa02ea01STom Caputi dn->dn_dirty_txg = 0; 202814843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 202914843421SMatthew Ahrens 203094c2d0ebSMatthew Ahrens multilist_sublist_remove(list, dn); 203194c2d0ebSMatthew Ahrens dnode_rele(dn, os->os_synced_dnodes); 203214843421SMatthew Ahrens } 20338b70546bSMatthew Ahrens do_userquota_cacheflush(os, &cache, tx); 203494c2d0ebSMatthew Ahrens multilist_sublist_unlock(list); 203594c2d0ebSMatthew Ahrens kmem_free(uua, sizeof (*uua)); 203694c2d0ebSMatthew Ahrens } 203794c2d0ebSMatthew Ahrens 203894c2d0ebSMatthew Ahrens void 203994c2d0ebSMatthew Ahrens dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 204094c2d0ebSMatthew Ahrens { 204194c2d0ebSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 204294c2d0ebSMatthew Ahrens return; 204394c2d0ebSMatthew Ahrens 2044*f67950b2SNasf-Fan /* 2045*f67950b2SNasf-Fan * If this is a raw receive just return and handle accounting 2046*f67950b2SNasf-Fan * later when we have the keys loaded. We also don't do user 2047*f67950b2SNasf-Fan * accounting during claiming since the datasets are not owned 2048*f67950b2SNasf-Fan * for the duration of claiming and this txg should only be 2049*f67950b2SNasf-Fan * used for recovery. 2050*f67950b2SNasf-Fan */ 2051eb633035STom Caputi if (os->os_encrypted && dmu_objset_is_receiving(os)) 2052eb633035STom Caputi return; 2053eb633035STom Caputi 2054*f67950b2SNasf-Fan /* Allocate the user/group/project used objects if necessary. */ 205594c2d0ebSMatthew Ahrens if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 205694c2d0ebSMatthew Ahrens VERIFY0(zap_create_claim(os, 205794c2d0ebSMatthew Ahrens DMU_USERUSED_OBJECT, 205894c2d0ebSMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 205994c2d0ebSMatthew Ahrens VERIFY0(zap_create_claim(os, 206094c2d0ebSMatthew Ahrens DMU_GROUPUSED_OBJECT, 206194c2d0ebSMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 206294c2d0ebSMatthew Ahrens } 206394c2d0ebSMatthew Ahrens 2064*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os) && 2065*f67950b2SNasf-Fan DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 2066*f67950b2SNasf-Fan VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT, 2067*f67950b2SNasf-Fan DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 2068*f67950b2SNasf-Fan } 2069*f67950b2SNasf-Fan 207094c2d0ebSMatthew Ahrens for (int i = 0; 207194c2d0ebSMatthew Ahrens i < multilist_get_num_sublists(os->os_synced_dnodes); i++) { 207294c2d0ebSMatthew Ahrens userquota_updates_arg_t *uua = 207394c2d0ebSMatthew Ahrens kmem_alloc(sizeof (*uua), KM_SLEEP); 207494c2d0ebSMatthew Ahrens uua->uua_os = os; 207594c2d0ebSMatthew Ahrens uua->uua_sublist_idx = i; 207694c2d0ebSMatthew Ahrens uua->uua_tx = tx; 207794c2d0ebSMatthew Ahrens /* note: caller does taskq_wait() */ 207894c2d0ebSMatthew Ahrens (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, 207994c2d0ebSMatthew Ahrens userquota_updates_task, uua, 0); 208094c2d0ebSMatthew Ahrens /* callback frees uua */ 208194c2d0ebSMatthew Ahrens } 208214843421SMatthew Ahrens } 208314843421SMatthew Ahrens 208406e0070dSMark Shellenbaum /* 208506e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from 208606e0070dSMark Shellenbaum * 208706e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't 208806e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed 208906e0070dSMark Shellenbaum * the uid/gid aren't changing. 209006e0070dSMark Shellenbaum */ 209106e0070dSMark Shellenbaum static void * 209206e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 209306e0070dSMark Shellenbaum { 209406e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp; 209506e0070dSMark Shellenbaum void *data; 209606e0070dSMark Shellenbaum 209706e0070dSMark Shellenbaum if (db->db_dirtycnt == 0) 209806e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */ 209906e0070dSMark Shellenbaum 210006e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 210106e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg) 210206e0070dSMark Shellenbaum break; 210306e0070dSMark Shellenbaum 2104744947dcSTom Erickson if (dr == NULL) { 210506e0070dSMark Shellenbaum data = NULL; 2106744947dcSTom Erickson } else { 2107744947dcSTom Erickson dnode_t *dn; 2108744947dcSTom Erickson 2109744947dcSTom Erickson DB_DNODE_ENTER(dr->dr_dbuf); 2110744947dcSTom Erickson dn = DB_DNODE(dr->dr_dbuf); 2111744947dcSTom Erickson 2112744947dcSTom Erickson if (dn->dn_bonuslen == 0 && 2113744947dcSTom Erickson dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 2114744947dcSTom Erickson data = dr->dt.dl.dr_data->b_data; 2115744947dcSTom Erickson else 2116744947dcSTom Erickson data = dr->dt.dl.dr_data; 2117744947dcSTom Erickson 2118744947dcSTom Erickson DB_DNODE_EXIT(dr->dr_dbuf); 2119744947dcSTom Erickson } 2120744947dcSTom Erickson 212106e0070dSMark Shellenbaum return (data); 212206e0070dSMark Shellenbaum } 212306e0070dSMark Shellenbaum 21240a586ceaSMark Shellenbaum void 212506e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 21260a586ceaSMark Shellenbaum { 21270a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset; 21280a586ceaSMark Shellenbaum void *data = NULL; 212906e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL; 2130d5285caeSGeorge Wilson uint64_t *user = NULL; 2131d5285caeSGeorge Wilson uint64_t *group = NULL; 2132*f67950b2SNasf-Fan uint64_t *project = NULL; 21330a586ceaSMark Shellenbaum int flags = dn->dn_id_flags; 21340a586ceaSMark Shellenbaum int error; 213506e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE; 21360a586ceaSMark Shellenbaum 21370a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset)) 21380a586ceaSMark Shellenbaum return; 21390a586ceaSMark Shellenbaum 2140eb633035STom Caputi /* 2141eb633035STom Caputi * Raw receives introduce a problem with user accounting. Raw 2142eb633035STom Caputi * receives cannot update the user accounting info because the 2143eb633035STom Caputi * user ids and the sizes are encrypted. To guarantee that we 2144eb633035STom Caputi * never end up with bad user accounting, we simply disable it 2145eb633035STom Caputi * during raw receives. We also disable this for normal receives 2146eb633035STom Caputi * so that an incremental raw receive may be done on top of an 2147eb633035STom Caputi * existing non-raw receive. 2148eb633035STom Caputi */ 2149eb633035STom Caputi if (os->os_encrypted && dmu_objset_is_receiving(os)) 2150eb633035STom Caputi return; 2151eb633035STom Caputi 21520a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 21530a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL))) 21540a586ceaSMark Shellenbaum return; 21550a586ceaSMark Shellenbaum 21560a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0) 21570a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 215806e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) { 215906e0070dSMark Shellenbaum if (dn->dn_bonus) { 216006e0070dSMark Shellenbaum db = dn->dn_bonus; 216106e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 216206e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx); 216306e0070dSMark Shellenbaum } else { 216406e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 216506e0070dSMark Shellenbaum } 216606e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 21670a586ceaSMark Shellenbaum int rf = 0; 21680a586ceaSMark Shellenbaum 21690a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 21700a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT; 21711d8ccc7bSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn, 21721d8ccc7bSMark Shellenbaum rf | DB_RF_MUST_SUCCEED, 217306e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db); 21740a586ceaSMark Shellenbaum ASSERT(error == 0); 217506e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 217606e0070dSMark Shellenbaum data = (before) ? db->db.db_data : 217706e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx); 217806e0070dSMark Shellenbaum have_spill = B_TRUE; 21790a586ceaSMark Shellenbaum } else { 21800a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 21810a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 21820a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 21830a586ceaSMark Shellenbaum return; 21840a586ceaSMark Shellenbaum } 21850a586ceaSMark Shellenbaum 21860a586ceaSMark Shellenbaum if (before) { 218706e0070dSMark Shellenbaum ASSERT(data); 21880a586ceaSMark Shellenbaum user = &dn->dn_olduid; 21890a586ceaSMark Shellenbaum group = &dn->dn_oldgid; 2190*f67950b2SNasf-Fan project = &dn->dn_oldprojid; 219106e0070dSMark Shellenbaum } else if (data) { 21920a586ceaSMark Shellenbaum user = &dn->dn_newuid; 21930a586ceaSMark Shellenbaum group = &dn->dn_newgid; 2194*f67950b2SNasf-Fan project = &dn->dn_newprojid; 21950a586ceaSMark Shellenbaum } 21960a586ceaSMark Shellenbaum 219706e0070dSMark Shellenbaum /* 219806e0070dSMark Shellenbaum * Must always call the callback in case the object 219906e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track 220006e0070dSMark Shellenbaum */ 22010a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 2202*f67950b2SNasf-Fan user, group, project); 22030a586ceaSMark Shellenbaum 220406e0070dSMark Shellenbaum /* 220506e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine 220606e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST. 220706e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid. 220806e0070dSMark Shellenbaum * If we don't know what the old values are then just assign 220906e0070dSMark Shellenbaum * them to 0, since that is a new file being created. 221006e0070dSMark Shellenbaum */ 221106e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) { 221206e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 221306e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid; 221406e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid; 2215*f67950b2SNasf-Fan dn->dn_newgid = dn->dn_oldprojid; 221606e0070dSMark Shellenbaum } else { 221706e0070dSMark Shellenbaum dn->dn_newuid = 0; 221806e0070dSMark Shellenbaum dn->dn_newgid = 0; 2219*f67950b2SNasf-Fan dn->dn_newprojid = ZFS_DEFAULT_PROJID; 222006e0070dSMark Shellenbaum } 222106e0070dSMark Shellenbaum error = 0; 222206e0070dSMark Shellenbaum } 222306e0070dSMark Shellenbaum 222406e0070dSMark Shellenbaum if (db) 222506e0070dSMark Shellenbaum mutex_exit(&db->db_mtx); 222606e0070dSMark Shellenbaum 22270a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 22280a586ceaSMark Shellenbaum if (error == 0 && before) 22290a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 22300a586ceaSMark Shellenbaum if (error == 0 && !before) 22310a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST; 22320a586ceaSMark Shellenbaum 223306e0070dSMark Shellenbaum if (have_spill) { 22340a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 22350a586ceaSMark Shellenbaum } else { 22360a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 22370a586ceaSMark Shellenbaum } 22380a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 223906e0070dSMark Shellenbaum if (have_spill) 224006e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG); 22410a586ceaSMark Shellenbaum } 22420a586ceaSMark Shellenbaum 224314843421SMatthew Ahrens boolean_t 224414843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 224514843421SMatthew Ahrens { 2246503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 224714843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 224814843421SMatthew Ahrens } 224914843421SMatthew Ahrens 2250*f67950b2SNasf-Fan boolean_t 2251*f67950b2SNasf-Fan dmu_objset_userobjspace_present(objset_t *os) 2252*f67950b2SNasf-Fan { 2253*f67950b2SNasf-Fan return (os->os_phys->os_flags & 2254*f67950b2SNasf-Fan OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE); 2255*f67950b2SNasf-Fan } 2256*f67950b2SNasf-Fan 2257*f67950b2SNasf-Fan boolean_t 2258*f67950b2SNasf-Fan dmu_objset_projectquota_present(objset_t *os) 2259*f67950b2SNasf-Fan { 2260*f67950b2SNasf-Fan return (os->os_phys->os_flags & 2261*f67950b2SNasf-Fan OBJSET_FLAG_PROJECTQUOTA_COMPLETE); 2262*f67950b2SNasf-Fan } 2263*f67950b2SNasf-Fan 2264*f67950b2SNasf-Fan static int 2265*f67950b2SNasf-Fan dmu_objset_space_upgrade(objset_t *os) 226614843421SMatthew Ahrens { 226714843421SMatthew Ahrens uint64_t obj; 226814843421SMatthew Ahrens int err = 0; 226914843421SMatthew Ahrens 227014843421SMatthew Ahrens /* 227114843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 227214843421SMatthew Ahrens * synced out and now accounted. If this is called 227314843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 227414843421SMatthew Ahrens * that's fine, since we track each object's accounted state 227514843421SMatthew Ahrens * independently. 227614843421SMatthew Ahrens */ 227714843421SMatthew Ahrens 227814843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 227968857716SLin Ling dmu_tx_t *tx; 228014843421SMatthew Ahrens dmu_buf_t *db; 228114843421SMatthew Ahrens int objerr; 228214843421SMatthew Ahrens 2283*f67950b2SNasf-Fan mutex_enter(&os->os_upgrade_lock); 2284*f67950b2SNasf-Fan if (os->os_upgrade_exit) 2285*f67950b2SNasf-Fan err = SET_ERROR(EINTR); 2286*f67950b2SNasf-Fan mutex_exit(&os->os_upgrade_lock); 2287*f67950b2SNasf-Fan if (err != 0) 2288*f67950b2SNasf-Fan return (err); 2289*f67950b2SNasf-Fan 2290*f67950b2SNasf-Fan /* 2291*f67950b2SNasf-Fan * The following is only valid on Linux since we cannot send 2292*f67950b2SNasf-Fan * a signal to a kernel thread on illumos (because we have no 2293*f67950b2SNasf-Fan * lwp and never return to user-land). 2294*f67950b2SNasf-Fan * 2295*f67950b2SNasf-Fan * if (issig(JUSTLOOKING) && issig(FORREAL)) 2296*f67950b2SNasf-Fan * return (SET_ERROR(EINTR)); 2297*f67950b2SNasf-Fan */ 229814843421SMatthew Ahrens 229914843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 23003b2aab18SMatthew Ahrens if (objerr != 0) 230114843421SMatthew Ahrens continue; 230268857716SLin Ling tx = dmu_tx_create(os); 230314843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 230414843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 23053b2aab18SMatthew Ahrens if (objerr != 0) { 230614843421SMatthew Ahrens dmu_tx_abort(tx); 230714843421SMatthew Ahrens continue; 230814843421SMatthew Ahrens } 230914843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 231014843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 231114843421SMatthew Ahrens dmu_tx_commit(tx); 231214843421SMatthew Ahrens } 2313*f67950b2SNasf-Fan return (0); 2314*f67950b2SNasf-Fan } 2315*f67950b2SNasf-Fan 2316*f67950b2SNasf-Fan int 2317*f67950b2SNasf-Fan dmu_objset_userspace_upgrade(objset_t *os) 2318*f67950b2SNasf-Fan { 2319*f67950b2SNasf-Fan int err = 0; 2320*f67950b2SNasf-Fan 2321*f67950b2SNasf-Fan if (dmu_objset_userspace_present(os)) 2322*f67950b2SNasf-Fan return (0); 2323*f67950b2SNasf-Fan if (dmu_objset_is_snapshot(os)) 2324*f67950b2SNasf-Fan return (SET_ERROR(EINVAL)); 2325*f67950b2SNasf-Fan if (!dmu_objset_userused_enabled(os)) 2326*f67950b2SNasf-Fan return (SET_ERROR(ENOTSUP)); 2327*f67950b2SNasf-Fan 2328*f67950b2SNasf-Fan err = dmu_objset_space_upgrade(os); 2329*f67950b2SNasf-Fan if (err) 2330*f67950b2SNasf-Fan return (err); 233114843421SMatthew Ahrens 2332503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 233314843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 233414843421SMatthew Ahrens return (0); 233514843421SMatthew Ahrens } 233614843421SMatthew Ahrens 2337*f67950b2SNasf-Fan static int 2338*f67950b2SNasf-Fan dmu_objset_id_quota_upgrade_cb(objset_t *os) 2339*f67950b2SNasf-Fan { 2340*f67950b2SNasf-Fan int err = 0; 2341*f67950b2SNasf-Fan 2342*f67950b2SNasf-Fan if (dmu_objset_userobjspace_present(os) && 2343*f67950b2SNasf-Fan dmu_objset_projectquota_present(os)) 2344*f67950b2SNasf-Fan return (0); 2345*f67950b2SNasf-Fan if (dmu_objset_is_snapshot(os)) 2346*f67950b2SNasf-Fan return (SET_ERROR(EINVAL)); 2347*f67950b2SNasf-Fan if (!dmu_objset_userobjused_enabled(os)) 2348*f67950b2SNasf-Fan return (SET_ERROR(ENOTSUP)); 2349*f67950b2SNasf-Fan if (!dmu_objset_projectquota_enabled(os) && 2350*f67950b2SNasf-Fan dmu_objset_userobjspace_present(os)) 2351*f67950b2SNasf-Fan return (SET_ERROR(ENOTSUP)); 2352*f67950b2SNasf-Fan 2353*f67950b2SNasf-Fan dmu_objset_ds(os)->ds_feature_activation_needed[ 2354*f67950b2SNasf-Fan SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE; 2355*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os)) 2356*f67950b2SNasf-Fan dmu_objset_ds(os)->ds_feature_activation_needed[ 2357*f67950b2SNasf-Fan SPA_FEATURE_PROJECT_QUOTA] = B_TRUE; 2358*f67950b2SNasf-Fan 2359*f67950b2SNasf-Fan err = dmu_objset_space_upgrade(os); 2360*f67950b2SNasf-Fan if (err) 2361*f67950b2SNasf-Fan return (err); 2362*f67950b2SNasf-Fan 2363*f67950b2SNasf-Fan os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE; 2364*f67950b2SNasf-Fan if (dmu_objset_projectquota_enabled(os)) 2365*f67950b2SNasf-Fan os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE; 2366*f67950b2SNasf-Fan 2367*f67950b2SNasf-Fan txg_wait_synced(dmu_objset_pool(os), 0); 2368*f67950b2SNasf-Fan return (0); 2369*f67950b2SNasf-Fan } 2370*f67950b2SNasf-Fan 2371*f67950b2SNasf-Fan void 2372*f67950b2SNasf-Fan dmu_objset_id_quota_upgrade(objset_t *os) 2373*f67950b2SNasf-Fan { 2374*f67950b2SNasf-Fan dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb); 2375*f67950b2SNasf-Fan } 2376*f67950b2SNasf-Fan 2377*f67950b2SNasf-Fan boolean_t 2378*f67950b2SNasf-Fan dmu_objset_userobjspace_upgradable(objset_t *os) 2379*f67950b2SNasf-Fan { 2380*f67950b2SNasf-Fan return (dmu_objset_type(os) == DMU_OST_ZFS && 2381*f67950b2SNasf-Fan !dmu_objset_is_snapshot(os) && 2382*f67950b2SNasf-Fan dmu_objset_userobjused_enabled(os) && 2383*f67950b2SNasf-Fan !dmu_objset_userobjspace_present(os) && 2384*f67950b2SNasf-Fan spa_writeable(dmu_objset_spa(os))); 2385*f67950b2SNasf-Fan } 2386*f67950b2SNasf-Fan 2387*f67950b2SNasf-Fan boolean_t 2388*f67950b2SNasf-Fan dmu_objset_projectquota_upgradable(objset_t *os) 2389*f67950b2SNasf-Fan { 2390*f67950b2SNasf-Fan return (dmu_objset_type(os) == DMU_OST_ZFS && 2391*f67950b2SNasf-Fan !dmu_objset_is_snapshot(os) && 2392*f67950b2SNasf-Fan dmu_objset_projectquota_enabled(os) && 2393*f67950b2SNasf-Fan !dmu_objset_projectquota_present(os)); 2394*f67950b2SNasf-Fan } 2395*f67950b2SNasf-Fan 2396fa9e4066Sahrens void 2397a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 2398a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 2399fa9e4066Sahrens { 2400503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 2401a2eea2e1Sahrens usedobjsp, availobjsp); 2402a2eea2e1Sahrens } 2403a2eea2e1Sahrens 2404a2eea2e1Sahrens uint64_t 2405a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 2406a2eea2e1Sahrens { 2407503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 2408a2eea2e1Sahrens } 2409a2eea2e1Sahrens 2410a2eea2e1Sahrens void 2411a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 2412a2eea2e1Sahrens { 2413503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 2414503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 2415503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 2416a2eea2e1Sahrens } 2417a2eea2e1Sahrens 2418a2eea2e1Sahrens void 2419a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 2420a2eea2e1Sahrens { 2421503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 2422503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 2423a2eea2e1Sahrens 2424503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 2425503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 2426a2eea2e1Sahrens 2427a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 2428503ad85cSMatthew Ahrens os->os_phys->os_type); 242914843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 243014843421SMatthew Ahrens dmu_objset_userspace_present(os)); 2431fa9e4066Sahrens } 2432fa9e4066Sahrens 2433fa9e4066Sahrens int 2434fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 2435fa9e4066Sahrens { 2436503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 2437bc9014e6SJustin Gibbs return (os->os_dsl_dataset->ds_is_snapshot); 2438fa9e4066Sahrens else 2439fa9e4066Sahrens return (B_FALSE); 2440fa9e4066Sahrens } 2441fa9e4066Sahrens 2442ab04eb8eStimh int 2443ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 2444ab04eb8eStimh boolean_t *conflict) 2445ab04eb8eStimh { 2446503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 2447ab04eb8eStimh uint64_t ignored; 2448ab04eb8eStimh 2449c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 2450be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 2451ab04eb8eStimh 2452ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 2453c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored, 24541c17160aSKevin Crowe MT_NORMALIZE, real, maxlen, conflict)); 2455ab04eb8eStimh } 2456ab04eb8eStimh 2457fa9e4066Sahrens int 2458fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 2459b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 2460fa9e4066Sahrens { 2461503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 2462fa9e4066Sahrens zap_cursor_t cursor; 2463fa9e4066Sahrens zap_attribute_t attr; 2464fa9e4066Sahrens 24653b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dmu_objset_pool(os))); 24663b2aab18SMatthew Ahrens 2467c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 2468be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 2469fa9e4066Sahrens 2470fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 2471fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 2472c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp); 2473fa9e4066Sahrens 247487e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 247587e5029aSahrens zap_cursor_fini(&cursor); 2476be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 247787e5029aSahrens } 247887e5029aSahrens 247987e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 248087e5029aSahrens zap_cursor_fini(&cursor); 2481be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 248287e5029aSahrens } 248387e5029aSahrens 248487e5029aSahrens (void) strcpy(name, attr.za_name); 248587e5029aSahrens if (idp) 248687e5029aSahrens *idp = attr.za_first_integer; 2487b38f0970Sck if (case_conflict) 2488b38f0970Sck *case_conflict = attr.za_normalization_conflict; 248987e5029aSahrens zap_cursor_advance(&cursor); 249087e5029aSahrens *offp = zap_cursor_serialize(&cursor); 249187e5029aSahrens zap_cursor_fini(&cursor); 249287e5029aSahrens 249387e5029aSahrens return (0); 249487e5029aSahrens } 249587e5029aSahrens 249687e5029aSahrens int 249787e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 249887e5029aSahrens uint64_t *idp, uint64_t *offp) 249987e5029aSahrens { 2500503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 250187e5029aSahrens zap_cursor_t cursor; 250287e5029aSahrens zap_attribute_t attr; 250387e5029aSahrens 250487e5029aSahrens /* there is no next dir on a snapshot! */ 2505503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 2506c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj) 2507be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 2508fa9e4066Sahrens 250987e5029aSahrens zap_cursor_init_serialized(&cursor, 251087e5029aSahrens dd->dd_pool->dp_meta_objset, 2511c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp); 251287e5029aSahrens 251387e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 251487e5029aSahrens zap_cursor_fini(&cursor); 2515be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 251687e5029aSahrens } 251787e5029aSahrens 251887e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 251987e5029aSahrens zap_cursor_fini(&cursor); 2520be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 252187e5029aSahrens } 2522fa9e4066Sahrens 2523fa9e4066Sahrens (void) strcpy(name, attr.za_name); 252487e5029aSahrens if (idp) 252587e5029aSahrens *idp = attr.za_first_integer; 2526fa9e4066Sahrens zap_cursor_advance(&cursor); 2527fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 252887e5029aSahrens zap_cursor_fini(&cursor); 2529fa9e4066Sahrens 2530fa9e4066Sahrens return (0); 2531fa9e4066Sahrens } 2532fa9e4066Sahrens 253312380e1eSArne Jansen typedef struct dmu_objset_find_ctx { 253412380e1eSArne Jansen taskq_t *dc_tq; 253512380e1eSArne Jansen dsl_pool_t *dc_dp; 253612380e1eSArne Jansen uint64_t dc_ddobj; 25377588687eSMatthew Ahrens char *dc_ddname; /* last component of ddobj's name */ 253812380e1eSArne Jansen int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *); 253912380e1eSArne Jansen void *dc_arg; 254012380e1eSArne Jansen int dc_flags; 254112380e1eSArne Jansen kmutex_t *dc_error_lock; 254212380e1eSArne Jansen int *dc_error; 254312380e1eSArne Jansen } dmu_objset_find_ctx_t; 254412380e1eSArne Jansen 254512380e1eSArne Jansen static void 254612380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp) 2547088f3894Sahrens { 254812380e1eSArne Jansen dsl_pool_t *dp = dcp->dc_dp; 25493b2aab18SMatthew Ahrens dsl_dir_t *dd; 25503b2aab18SMatthew Ahrens dsl_dataset_t *ds; 25513b2aab18SMatthew Ahrens zap_cursor_t zc; 25523b2aab18SMatthew Ahrens zap_attribute_t *attr; 25533b2aab18SMatthew Ahrens uint64_t thisobj; 255412380e1eSArne Jansen int err = 0; 25553b2aab18SMatthew Ahrens 255612380e1eSArne Jansen /* don't process if there already was an error */ 255712380e1eSArne Jansen if (*dcp->dc_error != 0) 255812380e1eSArne Jansen goto out; 25593b2aab18SMatthew Ahrens 25607588687eSMatthew Ahrens /* 25617588687eSMatthew Ahrens * Note: passing the name (dc_ddname) here is optional, but it 25627588687eSMatthew Ahrens * improves performance because we don't need to call 25637588687eSMatthew Ahrens * zap_value_search() to determine the name. 25647588687eSMatthew Ahrens */ 25657588687eSMatthew Ahrens err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd); 25663b2aab18SMatthew Ahrens if (err != 0) 256712380e1eSArne Jansen goto out; 25683b2aab18SMatthew Ahrens 25693b2aab18SMatthew Ahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 25703b2aab18SMatthew Ahrens if (dd->dd_myname[0] == '$') { 25713b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 257212380e1eSArne Jansen goto out; 25733b2aab18SMatthew Ahrens } 25743b2aab18SMatthew Ahrens 2575c1379625SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 25763b2aab18SMatthew Ahrens attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 25773b2aab18SMatthew Ahrens 25783b2aab18SMatthew Ahrens /* 25793b2aab18SMatthew Ahrens * Iterate over all children. 25803b2aab18SMatthew Ahrens */ 258112380e1eSArne Jansen if (dcp->dc_flags & DS_FIND_CHILDREN) { 25823b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2583c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj); 25843b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0; 25853b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) { 25863b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 25873b2aab18SMatthew Ahrens sizeof (uint64_t)); 25883b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 25893b2aab18SMatthew Ahrens 25907588687eSMatthew Ahrens dmu_objset_find_ctx_t *child_dcp = 25917588687eSMatthew Ahrens kmem_alloc(sizeof (*child_dcp), KM_SLEEP); 259212380e1eSArne Jansen *child_dcp = *dcp; 259312380e1eSArne Jansen child_dcp->dc_ddobj = attr->za_first_integer; 25947588687eSMatthew Ahrens child_dcp->dc_ddname = spa_strdup(attr->za_name); 259512380e1eSArne Jansen if (dcp->dc_tq != NULL) 259612380e1eSArne Jansen (void) taskq_dispatch(dcp->dc_tq, 259712380e1eSArne Jansen dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP); 259812380e1eSArne Jansen else 259912380e1eSArne Jansen dmu_objset_find_dp_impl(child_dcp); 26003b2aab18SMatthew Ahrens } 26013b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 26023b2aab18SMatthew Ahrens } 26033b2aab18SMatthew Ahrens 26043b2aab18SMatthew Ahrens /* 26053b2aab18SMatthew Ahrens * Iterate over all snapshots. 26063b2aab18SMatthew Ahrens */ 260712380e1eSArne Jansen if (dcp->dc_flags & DS_FIND_SNAPSHOTS) { 26083b2aab18SMatthew Ahrens dsl_dataset_t *ds; 26093b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 26103b2aab18SMatthew Ahrens 26113b2aab18SMatthew Ahrens if (err == 0) { 2612c1379625SJustin T. Gibbs uint64_t snapobj; 2613c1379625SJustin T. Gibbs 2614c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 26153b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 26163b2aab18SMatthew Ahrens 26173b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 26183b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0; 26193b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) { 26203b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 26213b2aab18SMatthew Ahrens sizeof (uint64_t)); 26223b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 26233b2aab18SMatthew Ahrens 26243b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 26253b2aab18SMatthew Ahrens attr->za_first_integer, FTAG, &ds); 26263b2aab18SMatthew Ahrens if (err != 0) 26273b2aab18SMatthew Ahrens break; 262812380e1eSArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg); 26293b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 26303b2aab18SMatthew Ahrens if (err != 0) 26313b2aab18SMatthew Ahrens break; 26323b2aab18SMatthew Ahrens } 26333b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 26343b2aab18SMatthew Ahrens } 26353b2aab18SMatthew Ahrens } 26363b2aab18SMatthew Ahrens 26373b2aab18SMatthew Ahrens kmem_free(attr, sizeof (zap_attribute_t)); 26383b2aab18SMatthew Ahrens 26397588687eSMatthew Ahrens if (err != 0) { 26407588687eSMatthew Ahrens dsl_dir_rele(dd, FTAG); 264112380e1eSArne Jansen goto out; 26427588687eSMatthew Ahrens } 26433b2aab18SMatthew Ahrens 26443b2aab18SMatthew Ahrens /* 26453b2aab18SMatthew Ahrens * Apply to self. 26463b2aab18SMatthew Ahrens */ 26473b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 26487588687eSMatthew Ahrens 26497588687eSMatthew Ahrens /* 26507588687eSMatthew Ahrens * Note: we hold the dir while calling dsl_dataset_hold_obj() so 26517588687eSMatthew Ahrens * that the dir will remain cached, and we won't have to re-instantiate 26527588687eSMatthew Ahrens * it (which could be expensive due to finding its name via 26537588687eSMatthew Ahrens * zap_value_search()). 26547588687eSMatthew Ahrens */ 26557588687eSMatthew Ahrens dsl_dir_rele(dd, FTAG); 26563b2aab18SMatthew Ahrens if (err != 0) 265712380e1eSArne Jansen goto out; 265812380e1eSArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg); 26593b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 266012380e1eSArne Jansen 266112380e1eSArne Jansen out: 266212380e1eSArne Jansen if (err != 0) { 266312380e1eSArne Jansen mutex_enter(dcp->dc_error_lock); 266412380e1eSArne Jansen /* only keep first error */ 266512380e1eSArne Jansen if (*dcp->dc_error == 0) 266612380e1eSArne Jansen *dcp->dc_error = err; 266712380e1eSArne Jansen mutex_exit(dcp->dc_error_lock); 266812380e1eSArne Jansen } 266912380e1eSArne Jansen 26707588687eSMatthew Ahrens if (dcp->dc_ddname != NULL) 26717588687eSMatthew Ahrens spa_strfree(dcp->dc_ddname); 267212380e1eSArne Jansen kmem_free(dcp, sizeof (*dcp)); 267312380e1eSArne Jansen } 267412380e1eSArne Jansen 267512380e1eSArne Jansen static void 267612380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg) 267712380e1eSArne Jansen { 267812380e1eSArne Jansen dmu_objset_find_ctx_t *dcp = arg; 267912380e1eSArne Jansen dsl_pool_t *dp = dcp->dc_dp; 268012380e1eSArne Jansen 26811d3f896fSArne Jansen /* 26821d3f896fSArne Jansen * We need to get a pool_config_lock here, as there are several 26831d3f896fSArne Jansen * asssert(pool_config_held) down the stack. Getting a lock via 26841d3f896fSArne Jansen * dsl_pool_config_enter is risky, as it might be stalled by a 26851d3f896fSArne Jansen * pending writer. This would deadlock, as the write lock can 26861d3f896fSArne Jansen * only be granted when our parent thread gives up the lock. 26871d3f896fSArne Jansen * The _prio interface gives us priority over a pending writer. 26881d3f896fSArne Jansen */ 26891d3f896fSArne Jansen dsl_pool_config_enter_prio(dp, FTAG); 269012380e1eSArne Jansen 269112380e1eSArne Jansen dmu_objset_find_dp_impl(dcp); 269212380e1eSArne Jansen 269312380e1eSArne Jansen dsl_pool_config_exit(dp, FTAG); 269412380e1eSArne Jansen } 269512380e1eSArne Jansen 269612380e1eSArne Jansen /* 269712380e1eSArne Jansen * Find objsets under and including ddobj, call func(ds) on each. 269812380e1eSArne Jansen * The order for the enumeration is completely undefined. 269912380e1eSArne Jansen * func is called with dsl_pool_config held. 270012380e1eSArne Jansen */ 270112380e1eSArne Jansen int 270212380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj, 270312380e1eSArne Jansen int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags) 270412380e1eSArne Jansen { 270512380e1eSArne Jansen int error = 0; 270612380e1eSArne Jansen taskq_t *tq = NULL; 270712380e1eSArne Jansen int ntasks; 270812380e1eSArne Jansen dmu_objset_find_ctx_t *dcp; 270912380e1eSArne Jansen kmutex_t err_lock; 271012380e1eSArne Jansen 271112380e1eSArne Jansen mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL); 271212380e1eSArne Jansen dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP); 271312380e1eSArne Jansen dcp->dc_tq = NULL; 271412380e1eSArne Jansen dcp->dc_dp = dp; 271512380e1eSArne Jansen dcp->dc_ddobj = ddobj; 27167588687eSMatthew Ahrens dcp->dc_ddname = NULL; 271712380e1eSArne Jansen dcp->dc_func = func; 271812380e1eSArne Jansen dcp->dc_arg = arg; 271912380e1eSArne Jansen dcp->dc_flags = flags; 272012380e1eSArne Jansen dcp->dc_error_lock = &err_lock; 272112380e1eSArne Jansen dcp->dc_error = &error; 272212380e1eSArne Jansen 272312380e1eSArne Jansen if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) { 272412380e1eSArne Jansen /* 272512380e1eSArne Jansen * In case a write lock is held we can't make use of 272612380e1eSArne Jansen * parallelism, as down the stack of the worker threads 272712380e1eSArne Jansen * the lock is asserted via dsl_pool_config_held. 272812380e1eSArne Jansen * In case of a read lock this is solved by getting a read 272912380e1eSArne Jansen * lock in each worker thread, which isn't possible in case 273012380e1eSArne Jansen * of a writer lock. So we fall back to the synchronous path 273112380e1eSArne Jansen * here. 273212380e1eSArne Jansen * In the future it might be possible to get some magic into 273312380e1eSArne Jansen * dsl_pool_config_held in a way that it returns true for 273412380e1eSArne Jansen * the worker threads so that a single lock held from this 273512380e1eSArne Jansen * thread suffices. For now, stay single threaded. 273612380e1eSArne Jansen */ 273712380e1eSArne Jansen dmu_objset_find_dp_impl(dcp); 27382bad2258SSteven Hartland mutex_destroy(&err_lock); 273912380e1eSArne Jansen 274012380e1eSArne Jansen return (error); 274112380e1eSArne Jansen } 274212380e1eSArne Jansen 274312380e1eSArne Jansen ntasks = dmu_find_threads; 274412380e1eSArne Jansen if (ntasks == 0) 274512380e1eSArne Jansen ntasks = vdev_count_leaves(dp->dp_spa) * 4; 274612380e1eSArne Jansen tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks, 274712380e1eSArne Jansen INT_MAX, 0); 274812380e1eSArne Jansen if (tq == NULL) { 274912380e1eSArne Jansen kmem_free(dcp, sizeof (*dcp)); 27502bad2258SSteven Hartland mutex_destroy(&err_lock); 27512bad2258SSteven Hartland 275212380e1eSArne Jansen return (SET_ERROR(ENOMEM)); 275312380e1eSArne Jansen } 275412380e1eSArne Jansen dcp->dc_tq = tq; 275512380e1eSArne Jansen 275612380e1eSArne Jansen /* dcp will be freed by task */ 275712380e1eSArne Jansen (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP); 275812380e1eSArne Jansen 275912380e1eSArne Jansen /* 276012380e1eSArne Jansen * PORTING: this code relies on the property of taskq_wait to wait 276112380e1eSArne Jansen * until no more tasks are queued and no more tasks are active. As 276212380e1eSArne Jansen * we always queue new tasks from within other tasks, task_wait 276312380e1eSArne Jansen * reliably waits for the full recursion to finish, even though we 276412380e1eSArne Jansen * enqueue new tasks after taskq_wait has been called. 276512380e1eSArne Jansen * On platforms other than illumos, taskq_wait may not have this 276612380e1eSArne Jansen * property. 276712380e1eSArne Jansen */ 276812380e1eSArne Jansen taskq_wait(tq); 276912380e1eSArne Jansen taskq_destroy(tq); 277012380e1eSArne Jansen mutex_destroy(&err_lock); 277112380e1eSArne Jansen 277212380e1eSArne Jansen return (error); 2773088f3894Sahrens } 2774088f3894Sahrens 2775088f3894Sahrens /* 27763b2aab18SMatthew Ahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 27773b2aab18SMatthew Ahrens * The dp_config_rwlock must not be held when this is called, and it 27783b2aab18SMatthew Ahrens * will not be held when the callback is called. 27793b2aab18SMatthew Ahrens * Therefore this function should only be used when the pool is not changing 27803b2aab18SMatthew Ahrens * (e.g. in syncing context), or the callback can deal with the possible races. 2781088f3894Sahrens */ 27823b2aab18SMatthew Ahrens static int 27833b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name, 27843b2aab18SMatthew Ahrens int func(const char *, void *), void *arg, int flags) 2785fa9e4066Sahrens { 2786fa9e4066Sahrens dsl_dir_t *dd; 27873b2aab18SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 2788088f3894Sahrens dsl_dataset_t *ds; 2789fa9e4066Sahrens zap_cursor_t zc; 2790b7661cccSmmusante zap_attribute_t *attr; 2791fa9e4066Sahrens char *child; 2792088f3894Sahrens uint64_t thisobj; 2793088f3894Sahrens int err; 2794fa9e4066Sahrens 27953b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 27963b2aab18SMatthew Ahrens 27973b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, NULL); 27983b2aab18SMatthew Ahrens if (err != 0) { 27993b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 28001d452cf5Sahrens return (err); 28013b2aab18SMatthew Ahrens } 2802fa9e4066Sahrens 2803088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 2804088f3894Sahrens if (dd->dd_myname[0] == '$') { 28053b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 28063b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 2807088f3894Sahrens return (0); 2808088f3894Sahrens } 2809088f3894Sahrens 2810c1379625SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 2811b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 2812fa9e4066Sahrens 2813fa9e4066Sahrens /* 2814fa9e4066Sahrens * Iterate over all children. 2815fa9e4066Sahrens */ 28160b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 2817088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2818c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj); 2819b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 28200b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 28213b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 28223b2aab18SMatthew Ahrens sizeof (uint64_t)); 28233b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 2824fa9e4066Sahrens 2825486ae710SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name); 28263b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 28273b2aab18SMatthew Ahrens err = dmu_objset_find_impl(spa, child, 28283b2aab18SMatthew Ahrens func, arg, flags); 28293b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 2830486ae710SMatthew Ahrens strfree(child); 28313b2aab18SMatthew Ahrens if (err != 0) 28320b69c2f0Sahrens break; 28330b69c2f0Sahrens } 28340b69c2f0Sahrens zap_cursor_fini(&zc); 28351d452cf5Sahrens 28363b2aab18SMatthew Ahrens if (err != 0) { 28373b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 28383b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 2839b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 28400b69c2f0Sahrens return (err); 28410b69c2f0Sahrens } 2842fa9e4066Sahrens } 2843fa9e4066Sahrens 2844fa9e4066Sahrens /* 2845fa9e4066Sahrens * Iterate over all snapshots. 2846fa9e4066Sahrens */ 2847088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 2848088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 2849088f3894Sahrens 2850088f3894Sahrens if (err == 0) { 2851c1379625SJustin T. Gibbs uint64_t snapobj; 2852c1379625SJustin T. Gibbs 2853c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 2854088f3894Sahrens dsl_dataset_rele(ds, FTAG); 2855088f3894Sahrens 2856088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 2857088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 2858088f3894Sahrens (void) zap_cursor_advance(&zc)) { 28593b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 2860088f3894Sahrens sizeof (uint64_t)); 28613b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 2862088f3894Sahrens 2863486ae710SMatthew Ahrens child = kmem_asprintf("%s@%s", 2864486ae710SMatthew Ahrens name, attr->za_name); 28653b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 28663b2aab18SMatthew Ahrens err = func(child, arg); 28673b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 2868486ae710SMatthew Ahrens strfree(child); 28693b2aab18SMatthew Ahrens if (err != 0) 2870088f3894Sahrens break; 2871088f3894Sahrens } 2872088f3894Sahrens zap_cursor_fini(&zc); 2873fa9e4066Sahrens } 2874fa9e4066Sahrens } 2875fa9e4066Sahrens 28763b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 2877b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 28783b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 2879fa9e4066Sahrens 28803b2aab18SMatthew Ahrens if (err != 0) 28811d452cf5Sahrens return (err); 28821d452cf5Sahrens 28833b2aab18SMatthew Ahrens /* Apply to self. */ 28843b2aab18SMatthew Ahrens return (func(name, arg)); 2885fa9e4066Sahrens } 2886f18faf3fSek 28873b2aab18SMatthew Ahrens /* 28883b2aab18SMatthew Ahrens * See comment above dmu_objset_find_impl(). 28893b2aab18SMatthew Ahrens */ 28907f73c863SRich Morris int 28913b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg, 28923b2aab18SMatthew Ahrens int flags) 28937f73c863SRich Morris { 28943b2aab18SMatthew Ahrens spa_t *spa; 28953b2aab18SMatthew Ahrens int error; 28967f73c863SRich Morris 28973b2aab18SMatthew Ahrens error = spa_open(name, &spa, FTAG); 28983b2aab18SMatthew Ahrens if (error != 0) 28993b2aab18SMatthew Ahrens return (error); 29003b2aab18SMatthew Ahrens error = dmu_objset_find_impl(spa, name, func, arg, flags); 29013b2aab18SMatthew Ahrens spa_close(spa, FTAG); 29023b2aab18SMatthew Ahrens return (error); 29037f73c863SRich Morris } 29047f73c863SRich Morris 2905eb633035STom Caputi boolean_t 2906eb633035STom Caputi dmu_objset_incompatible_encryption_version(objset_t *os) 2907eb633035STom Caputi { 2908eb633035STom Caputi return (dsl_dir_incompatible_encryption_version( 2909eb633035STom Caputi os->os_dsl_dataset->ds_dir)); 2910eb633035STom Caputi } 2911eb633035STom Caputi 2912f18faf3fSek void 2913f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 2914f18faf3fSek { 2915503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 2916503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 2917f18faf3fSek } 2918f18faf3fSek 2919f18faf3fSek void * 2920f18faf3fSek dmu_objset_get_user(objset_t *os) 2921f18faf3fSek { 2922503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 2923503ad85cSMatthew Ahrens return (os->os_user_ptr); 2924f18faf3fSek } 29253b2aab18SMatthew Ahrens 29263b2aab18SMatthew Ahrens /* 29273b2aab18SMatthew Ahrens * Determine name of filesystem, given name of snapshot. 29289adfa60dSMatthew Ahrens * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes 29293b2aab18SMatthew Ahrens */ 29303b2aab18SMatthew Ahrens int 29313b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf) 29323b2aab18SMatthew Ahrens { 29333b2aab18SMatthew Ahrens char *atp = strchr(snapname, '@'); 29343b2aab18SMatthew Ahrens if (atp == NULL) 2935be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 29369adfa60dSMatthew Ahrens if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN) 2937be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 29383b2aab18SMatthew Ahrens (void) strlcpy(buf, snapname, atp - snapname + 1); 29393b2aab18SMatthew Ahrens return (0); 29403b2aab18SMatthew Ahrens } 294161e255ceSMatthew Ahrens 294261e255ceSMatthew Ahrens /* 294361e255ceSMatthew Ahrens * Call when we think we're going to write/free space in open context to track 294461e255ceSMatthew Ahrens * the amount of dirty data in the open txg, which is also the amount 294561e255ceSMatthew Ahrens * of memory that can not be evicted until this txg syncs. 294661e255ceSMatthew Ahrens */ 294761e255ceSMatthew Ahrens void 294861e255ceSMatthew Ahrens dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx) 294961e255ceSMatthew Ahrens { 295061e255ceSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 295161e255ceSMatthew Ahrens int64_t aspace = spa_get_worst_case_asize(os->os_spa, space); 295261e255ceSMatthew Ahrens 295361e255ceSMatthew Ahrens if (ds != NULL) { 295461e255ceSMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, aspace, tx); 295561e255ceSMatthew Ahrens dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx); 295661e255ceSMatthew Ahrens } 295761e255ceSMatthew Ahrens } 2958