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. 26a2afb611SJerry Jelinek * Copyright (c) 2013, Joyent, Inc. All rights reserved. 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 35ecd6cf80Smarks #include <sys/cred.h> 36fa9e4066Sahrens #include <sys/zfs_context.h> 37fa9e4066Sahrens #include <sys/dmu_objset.h> 38fa9e4066Sahrens #include <sys/dsl_dir.h> 39fa9e4066Sahrens #include <sys/dsl_dataset.h> 40fa9e4066Sahrens #include <sys/dsl_prop.h> 41fa9e4066Sahrens #include <sys/dsl_pool.h> 421d452cf5Sahrens #include <sys/dsl_synctask.h> 43ecd6cf80Smarks #include <sys/dsl_deleg.h> 44fa9e4066Sahrens #include <sys/dnode.h> 45fa9e4066Sahrens #include <sys/dbuf.h> 46a2eea2e1Sahrens #include <sys/zvol.h> 47fa9e4066Sahrens #include <sys/dmu_tx.h> 48fa9e4066Sahrens #include <sys/zap.h> 49fa9e4066Sahrens #include <sys/zil.h> 50fa9e4066Sahrens #include <sys/dmu_impl.h> 51ecd6cf80Smarks #include <sys/zfs_ioctl.h> 520a586ceaSMark Shellenbaum #include <sys/sa.h> 5399d5e173STim Haley #include <sys/zfs_onexit.h> 543b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 5512380e1eSArne Jansen #include <sys/vdev.h> 565cabbc6bSPrashanth Sreenivasa #include <sys/zfeature.h> 575ac95da7SSerapheim Dimitropoulos #include "zfs_namecheck.h" 58fa9e4066Sahrens 59744947dcSTom Erickson /* 60744947dcSTom Erickson * Needed to close a window in dnode_move() that allows the objset to be freed 61744947dcSTom Erickson * before it can be safely accessed. 62744947dcSTom Erickson */ 63744947dcSTom Erickson krwlock_t os_lock; 64744947dcSTom Erickson 6512380e1eSArne Jansen /* 6612380e1eSArne Jansen * Tunable to overwrite the maximum number of threads for the parallization 6712380e1eSArne Jansen * of dmu_objset_find_dp, needed to speed up the import of pools with many 6812380e1eSArne Jansen * datasets. 6912380e1eSArne Jansen * Default is 4 times the number of leaf vdevs. 7012380e1eSArne Jansen */ 7112380e1eSArne Jansen int dmu_find_threads = 0; 7212380e1eSArne Jansen 73af346df5SNed Bass /* 74af346df5SNed Bass * Backfill lower metadnode objects after this many have been freed. 75af346df5SNed Bass * Backfilling negatively impacts object creation rates, so only do it 76af346df5SNed Bass * if there are enough holes to fill. 77af346df5SNed Bass */ 78af346df5SNed Bass int dmu_rescan_dnode_threshold = 131072; 79af346df5SNed Bass 8012380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg); 8112380e1eSArne Jansen 82744947dcSTom Erickson void 83744947dcSTom Erickson dmu_objset_init(void) 84744947dcSTom Erickson { 85744947dcSTom Erickson rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 86744947dcSTom Erickson } 87744947dcSTom Erickson 88744947dcSTom Erickson void 89744947dcSTom Erickson dmu_objset_fini(void) 90744947dcSTom Erickson { 91744947dcSTom Erickson rw_destroy(&os_lock); 92744947dcSTom Erickson } 93744947dcSTom Erickson 94fa9e4066Sahrens spa_t * 95fa9e4066Sahrens dmu_objset_spa(objset_t *os) 96fa9e4066Sahrens { 97503ad85cSMatthew Ahrens return (os->os_spa); 98fa9e4066Sahrens } 99fa9e4066Sahrens 100fa9e4066Sahrens zilog_t * 101fa9e4066Sahrens dmu_objset_zil(objset_t *os) 102fa9e4066Sahrens { 103503ad85cSMatthew Ahrens return (os->os_zil); 104fa9e4066Sahrens } 105fa9e4066Sahrens 106fa9e4066Sahrens dsl_pool_t * 107fa9e4066Sahrens dmu_objset_pool(objset_t *os) 108fa9e4066Sahrens { 109fa9e4066Sahrens dsl_dataset_t *ds; 110fa9e4066Sahrens 111503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 112fa9e4066Sahrens return (ds->ds_dir->dd_pool); 113fa9e4066Sahrens else 114503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa)); 115fa9e4066Sahrens } 116fa9e4066Sahrens 117fa9e4066Sahrens dsl_dataset_t * 118fa9e4066Sahrens dmu_objset_ds(objset_t *os) 119fa9e4066Sahrens { 120503ad85cSMatthew Ahrens return (os->os_dsl_dataset); 121fa9e4066Sahrens } 122fa9e4066Sahrens 123fa9e4066Sahrens dmu_objset_type_t 124fa9e4066Sahrens dmu_objset_type(objset_t *os) 125fa9e4066Sahrens { 126503ad85cSMatthew Ahrens return (os->os_phys->os_type); 127fa9e4066Sahrens } 128fa9e4066Sahrens 129fa9e4066Sahrens void 130fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf) 131fa9e4066Sahrens { 132503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf); 133fa9e4066Sahrens } 134fa9e4066Sahrens 135fa9e4066Sahrens uint64_t 136fa9e4066Sahrens dmu_objset_id(objset_t *os) 137fa9e4066Sahrens { 138503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 139fa9e4066Sahrens 140fa9e4066Sahrens return (ds ? ds->ds_object : 0); 141fa9e4066Sahrens } 142fa9e4066Sahrens 143*54811da5SToomas Soome uint64_t 144*54811da5SToomas Soome dmu_objset_dnodesize(objset_t *os) 145*54811da5SToomas Soome { 146*54811da5SToomas Soome return (os->os_dnodesize); 147*54811da5SToomas Soome } 148*54811da5SToomas Soome 149edf345e6SMatthew Ahrens zfs_sync_type_t 15055da60b9SMark J Musante dmu_objset_syncprop(objset_t *os) 15155da60b9SMark J Musante { 15255da60b9SMark J Musante return (os->os_sync); 15355da60b9SMark J Musante } 15455da60b9SMark J Musante 155edf345e6SMatthew Ahrens zfs_logbias_op_t 156e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os) 157e09fa4daSNeil Perrin { 158e09fa4daSNeil Perrin return (os->os_logbias); 159e09fa4daSNeil Perrin } 160e09fa4daSNeil Perrin 161fa9e4066Sahrens static void 162fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval) 163fa9e4066Sahrens { 164503ad85cSMatthew Ahrens objset_t *os = arg; 165fa9e4066Sahrens 166fa9e4066Sahrens /* 167fa9e4066Sahrens * Inheritance should have been done by now. 168fa9e4066Sahrens */ 169fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT); 170fa9e4066Sahrens 171503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 172fa9e4066Sahrens } 173fa9e4066Sahrens 174fa9e4066Sahrens static void 175fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval) 176fa9e4066Sahrens { 177503ad85cSMatthew Ahrens objset_t *os = arg; 178fa9e4066Sahrens 179fa9e4066Sahrens /* 180fa9e4066Sahrens * Inheritance and range checking should have been done by now. 181fa9e4066Sahrens */ 182fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT); 183fa9e4066Sahrens 184db1741f5SJustin T. Gibbs os->os_compress = zio_compress_select(os->os_spa, newval, 185db1741f5SJustin T. Gibbs ZIO_COMPRESS_ON); 186fa9e4066Sahrens } 187fa9e4066Sahrens 188d0ad202dSahrens static void 189d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval) 190d0ad202dSahrens { 191503ad85cSMatthew Ahrens objset_t *os = arg; 192d0ad202dSahrens 193d0ad202dSahrens /* 194d0ad202dSahrens * Inheritance and range checking should have been done by now. 195d0ad202dSahrens */ 196d0ad202dSahrens ASSERT(newval > 0); 197503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa)); 198d0ad202dSahrens 199503ad85cSMatthew Ahrens os->os_copies = newval; 200d0ad202dSahrens } 201d0ad202dSahrens 202b24ab676SJeff Bonwick static void 203b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval) 204b24ab676SJeff Bonwick { 205b24ab676SJeff Bonwick objset_t *os = arg; 206b24ab676SJeff Bonwick spa_t *spa = os->os_spa; 207b24ab676SJeff Bonwick enum zio_checksum checksum; 208b24ab676SJeff Bonwick 209b24ab676SJeff Bonwick /* 210b24ab676SJeff Bonwick * Inheritance should have been done by now. 211b24ab676SJeff Bonwick */ 212b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT); 213b24ab676SJeff Bonwick 214b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 215b24ab676SJeff Bonwick 216b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 217b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 218b24ab676SJeff Bonwick } 219b24ab676SJeff Bonwick 2203baa08fcSek static void 2213baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval) 2223baa08fcSek { 223503ad85cSMatthew Ahrens objset_t *os = arg; 2243baa08fcSek 2253baa08fcSek /* 2263baa08fcSek * Inheritance and range checking should have been done by now. 2273baa08fcSek */ 2283baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 2293baa08fcSek newval == ZFS_CACHE_METADATA); 2303baa08fcSek 231503ad85cSMatthew Ahrens os->os_primary_cache = newval; 2323baa08fcSek } 2333baa08fcSek 2343baa08fcSek static void 2353baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval) 2363baa08fcSek { 237503ad85cSMatthew Ahrens objset_t *os = arg; 2383baa08fcSek 2393baa08fcSek /* 2403baa08fcSek * Inheritance and range checking should have been done by now. 2413baa08fcSek */ 2423baa08fcSek ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 2433baa08fcSek newval == ZFS_CACHE_METADATA); 2443baa08fcSek 245503ad85cSMatthew Ahrens os->os_secondary_cache = newval; 2463baa08fcSek } 2473baa08fcSek 24855da60b9SMark J Musante static void 24955da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval) 25055da60b9SMark J Musante { 25155da60b9SMark J Musante objset_t *os = arg; 25255da60b9SMark J Musante 25355da60b9SMark J Musante /* 25455da60b9SMark J Musante * Inheritance and range checking should have been done by now. 25555da60b9SMark J Musante */ 25655da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 25755da60b9SMark J Musante newval == ZFS_SYNC_DISABLED); 25855da60b9SMark J Musante 25955da60b9SMark J Musante os->os_sync = newval; 26055da60b9SMark J Musante if (os->os_zil) 26155da60b9SMark J Musante zil_set_sync(os->os_zil, newval); 26255da60b9SMark J Musante } 26355da60b9SMark J Musante 264edf345e6SMatthew Ahrens static void 265edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval) 266edf345e6SMatthew Ahrens { 267edf345e6SMatthew Ahrens objset_t *os = arg; 268edf345e6SMatthew Ahrens 269edf345e6SMatthew Ahrens /* 270edf345e6SMatthew Ahrens * Inheritance and range checking should have been done by now. 271edf345e6SMatthew Ahrens */ 272edf345e6SMatthew Ahrens ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL || 273edf345e6SMatthew Ahrens newval == ZFS_REDUNDANT_METADATA_MOST); 274edf345e6SMatthew Ahrens 275edf345e6SMatthew Ahrens os->os_redundant_metadata = newval; 276edf345e6SMatthew Ahrens } 277edf345e6SMatthew Ahrens 278*54811da5SToomas Soome static void 279*54811da5SToomas Soome dnodesize_changed_cb(void *arg, uint64_t newval) 280*54811da5SToomas Soome { 281*54811da5SToomas Soome objset_t *os = arg; 282*54811da5SToomas Soome 283*54811da5SToomas Soome switch (newval) { 284*54811da5SToomas Soome case ZFS_DNSIZE_LEGACY: 285*54811da5SToomas Soome os->os_dnodesize = DNODE_MIN_SIZE; 286*54811da5SToomas Soome break; 287*54811da5SToomas Soome case ZFS_DNSIZE_AUTO: 288*54811da5SToomas Soome /* 289*54811da5SToomas Soome * Choose a dnode size that will work well for most 290*54811da5SToomas Soome * workloads if the user specified "auto". Future code 291*54811da5SToomas Soome * improvements could dynamically select a dnode size 292*54811da5SToomas Soome * based on observed workload patterns. 293*54811da5SToomas Soome */ 294*54811da5SToomas Soome os->os_dnodesize = DNODE_MIN_SIZE * 2; 295*54811da5SToomas Soome break; 296*54811da5SToomas Soome case ZFS_DNSIZE_1K: 297*54811da5SToomas Soome case ZFS_DNSIZE_2K: 298*54811da5SToomas Soome case ZFS_DNSIZE_4K: 299*54811da5SToomas Soome case ZFS_DNSIZE_8K: 300*54811da5SToomas Soome case ZFS_DNSIZE_16K: 301*54811da5SToomas Soome os->os_dnodesize = newval; 302*54811da5SToomas Soome break; 303*54811da5SToomas Soome } 304*54811da5SToomas Soome } 305*54811da5SToomas Soome 306e09fa4daSNeil Perrin static void 307e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval) 308e09fa4daSNeil Perrin { 309e09fa4daSNeil Perrin objset_t *os = arg; 310e09fa4daSNeil Perrin 311e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY || 312e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT); 313e09fa4daSNeil Perrin os->os_logbias = newval; 314e09fa4daSNeil Perrin if (os->os_zil) 315e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval); 316e09fa4daSNeil Perrin } 317e09fa4daSNeil Perrin 318b5152584SMatthew Ahrens static void 319b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval) 320b5152584SMatthew Ahrens { 321b5152584SMatthew Ahrens objset_t *os = arg; 322b5152584SMatthew Ahrens 323b5152584SMatthew Ahrens os->os_recordsize = newval; 324b5152584SMatthew Ahrens } 325b5152584SMatthew Ahrens 326fa9e4066Sahrens void 327fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size) 328fa9e4066Sahrens { 329fa9e4066Sahrens objset_phys_t *osp = buf; 330fa9e4066Sahrens 33114843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 332fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode); 333fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 334fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type); 33514843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags); 33614843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) { 33714843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode); 33814843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode); 33914843421SMatthew Ahrens } 340fa9e4066Sahrens } 341fa9e4066Sahrens 34294c2d0ebSMatthew Ahrens /* 34394c2d0ebSMatthew Ahrens * The hash is a CRC-based hash of the objset_t pointer and the object number. 34494c2d0ebSMatthew Ahrens */ 34594c2d0ebSMatthew Ahrens static uint64_t 34694c2d0ebSMatthew Ahrens dnode_hash(const objset_t *os, uint64_t obj) 34794c2d0ebSMatthew Ahrens { 34894c2d0ebSMatthew Ahrens uintptr_t osv = (uintptr_t)os; 34994c2d0ebSMatthew Ahrens uint64_t crc = -1ULL; 35094c2d0ebSMatthew Ahrens 35194c2d0ebSMatthew Ahrens ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 35294c2d0ebSMatthew Ahrens /* 35394c2d0ebSMatthew Ahrens * The low 6 bits of the pointer don't have much entropy, because 35494c2d0ebSMatthew Ahrens * the objset_t is larger than 2^6 bytes long. 35594c2d0ebSMatthew Ahrens */ 35694c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF]; 35794c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF]; 35894c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF]; 35994c2d0ebSMatthew Ahrens crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF]; 36094c2d0ebSMatthew Ahrens 36194c2d0ebSMatthew Ahrens crc ^= (osv>>14) ^ (obj>>24); 36294c2d0ebSMatthew Ahrens 36394c2d0ebSMatthew Ahrens return (crc); 36494c2d0ebSMatthew Ahrens } 36594c2d0ebSMatthew Ahrens 36694c2d0ebSMatthew Ahrens unsigned int 36794c2d0ebSMatthew Ahrens dnode_multilist_index_func(multilist_t *ml, void *obj) 36894c2d0ebSMatthew Ahrens { 36994c2d0ebSMatthew Ahrens dnode_t *dn = obj; 37094c2d0ebSMatthew Ahrens return (dnode_hash(dn->dn_objset, dn->dn_object) % 37194c2d0ebSMatthew Ahrens multilist_get_num_sublists(ml)); 37294c2d0ebSMatthew Ahrens } 37394c2d0ebSMatthew Ahrens 37494c2d0ebSMatthew Ahrens /* 37594c2d0ebSMatthew Ahrens * Instantiates the objset_t in-memory structure corresponding to the 37694c2d0ebSMatthew Ahrens * objset_phys_t that's pointed to by the specified blkptr_t. 37794c2d0ebSMatthew Ahrens */ 378ea8dc4b6Seschrock int 379ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 380503ad85cSMatthew Ahrens objset_t **osp) 381fa9e4066Sahrens { 382503ad85cSMatthew Ahrens objset_t *os; 383088f3894Sahrens int i, err; 384fa9e4066Sahrens 38591ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 38691ebeef5Sahrens 3875cabbc6bSPrashanth Sreenivasa /* 3885cabbc6bSPrashanth Sreenivasa * The $ORIGIN dataset (if it exists) doesn't have an associated 3895cabbc6bSPrashanth Sreenivasa * objset, so there's no reason to open it. The $ORIGIN dataset 3905cabbc6bSPrashanth Sreenivasa * will not exist on pools older than SPA_VERSION_ORIGIN. 3915cabbc6bSPrashanth Sreenivasa */ 3925cabbc6bSPrashanth Sreenivasa if (ds != NULL && spa_get_dsl(spa) != NULL && 3935cabbc6bSPrashanth Sreenivasa spa_get_dsl(spa)->dp_origin_snap != NULL) { 3945cabbc6bSPrashanth Sreenivasa ASSERT3P(ds->ds_dir, !=, 3955cabbc6bSPrashanth Sreenivasa spa_get_dsl(spa)->dp_origin_snap->ds_dir); 3965cabbc6bSPrashanth Sreenivasa } 3975cabbc6bSPrashanth Sreenivasa 398503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 399503ad85cSMatthew Ahrens os->os_dsl_dataset = ds; 400503ad85cSMatthew Ahrens os->os_spa = spa; 401503ad85cSMatthew Ahrens os->os_rootbp = bp; 402503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) { 4037adb730bSGeorge Wilson arc_flags_t aflags = ARC_FLAG_WAIT; 4047802d7bfSMatthew Ahrens zbookmark_phys_t zb; 405b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 406b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 407b24ab676SJeff Bonwick 408503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os)) 4097adb730bSGeorge Wilson aflags |= ARC_FLAG_L2CACHE; 410ea8dc4b6Seschrock 411503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", ""); 4121b912ec7SGeorge Wilson err = arc_read(NULL, spa, os->os_rootbp, 413503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf, 41413506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 4153b2aab18SMatthew Ahrens if (err != 0) { 416503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 417b87f3af3Sperrin /* convert checksum errors into IO errors */ 418b87f3af3Sperrin if (err == ECKSUM) 419be6fd75aSMatthew Ahrens err = SET_ERROR(EIO); 420ea8dc4b6Seschrock return (err); 421ea8dc4b6Seschrock } 42214843421SMatthew Ahrens 42314843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */ 42414843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE && 425503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 4265602294fSDan Kimmel arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf, 4275602294fSDan Kimmel ARC_BUFC_METADATA, sizeof (objset_phys_t)); 42814843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t)); 429503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data, 430503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf)); 431dcbf3bd6SGeorge Wilson arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 432503ad85cSMatthew Ahrens os->os_phys_buf = buf; 43314843421SMatthew Ahrens } 43414843421SMatthew Ahrens 435503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 436503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 437fa9e4066Sahrens } else { 43814843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 43914843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 4405602294fSDan Kimmel os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf, 4415602294fSDan Kimmel ARC_BUFC_METADATA, size); 442503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data; 443503ad85cSMatthew Ahrens bzero(os->os_phys, size); 444fa9e4066Sahrens } 445fa9e4066Sahrens 446fa9e4066Sahrens /* 447fa9e4066Sahrens * Note: the changed_cb will be called once before the register 448fa9e4066Sahrens * func returns, thus changing the checksum/compression from the 4493baa08fcSek * default (fletcher2/off). Snapshots don't need to know about 4503baa08fcSek * checksum/compression/copies. 451fa9e4066Sahrens */ 4525d7b4d43SMatthew Ahrens if (ds != NULL) { 4539c3fd121SMatthew Ahrens boolean_t needlock = B_FALSE; 4549c3fd121SMatthew Ahrens 4559c3fd121SMatthew Ahrens /* 4569c3fd121SMatthew Ahrens * Note: it's valid to open the objset if the dataset is 4579c3fd121SMatthew Ahrens * long-held, in which case the pool_config lock will not 4589c3fd121SMatthew Ahrens * be held. 4599c3fd121SMatthew Ahrens */ 4609c3fd121SMatthew Ahrens if (!dsl_pool_config_held(dmu_objset_pool(os))) { 4619c3fd121SMatthew Ahrens needlock = B_TRUE; 4629c3fd121SMatthew Ahrens dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 4639c3fd121SMatthew Ahrens } 4643b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 4653b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE), 466503ad85cSMatthew Ahrens primary_cache_changed_cb, os); 4673b2aab18SMatthew Ahrens if (err == 0) { 4683b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 4693b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE), 470503ad85cSMatthew Ahrens secondary_cache_changed_cb, os); 4713b2aab18SMatthew Ahrens } 472bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) { 4733b2aab18SMatthew Ahrens if (err == 0) { 4743b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 4753b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CHECKSUM), 476503ad85cSMatthew Ahrens checksum_changed_cb, os); 4773b2aab18SMatthew Ahrens } 4783b2aab18SMatthew Ahrens if (err == 0) { 4793b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 4803b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COMPRESSION), 481503ad85cSMatthew Ahrens compression_changed_cb, os); 4823b2aab18SMatthew Ahrens } 4833b2aab18SMatthew Ahrens if (err == 0) { 4843b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 4853b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COPIES), 486503ad85cSMatthew Ahrens copies_changed_cb, os); 4873b2aab18SMatthew Ahrens } 4883b2aab18SMatthew Ahrens if (err == 0) { 4893b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 4903b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_DEDUP), 491b24ab676SJeff Bonwick dedup_changed_cb, os); 4923b2aab18SMatthew Ahrens } 4933b2aab18SMatthew Ahrens if (err == 0) { 4943b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 4953b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_LOGBIAS), 496e09fa4daSNeil Perrin logbias_changed_cb, os); 4973b2aab18SMatthew Ahrens } 4983b2aab18SMatthew Ahrens if (err == 0) { 4993b2aab18SMatthew Ahrens err = dsl_prop_register(ds, 5003b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SYNC), 50155da60b9SMark J Musante sync_changed_cb, os); 5023b2aab18SMatthew Ahrens } 503edf345e6SMatthew Ahrens if (err == 0) { 504edf345e6SMatthew Ahrens err = dsl_prop_register(ds, 505edf345e6SMatthew Ahrens zfs_prop_to_name( 506edf345e6SMatthew Ahrens ZFS_PROP_REDUNDANT_METADATA), 507edf345e6SMatthew Ahrens redundant_metadata_changed_cb, os); 508edf345e6SMatthew Ahrens } 509b5152584SMatthew Ahrens if (err == 0) { 510b5152584SMatthew Ahrens err = dsl_prop_register(ds, 511b5152584SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RECORDSIZE), 512b5152584SMatthew Ahrens recordsize_changed_cb, os); 513b5152584SMatthew Ahrens } 514*54811da5SToomas Soome if (err == 0) { 515*54811da5SToomas Soome err = dsl_prop_register(ds, 516*54811da5SToomas Soome zfs_prop_to_name(ZFS_PROP_DNODESIZE), 517*54811da5SToomas Soome dnodesize_changed_cb, os); 518*54811da5SToomas Soome } 5193baa08fcSek } 5209c3fd121SMatthew Ahrens if (needlock) 5219c3fd121SMatthew Ahrens dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 5223b2aab18SMatthew Ahrens if (err != 0) { 523dcbf3bd6SGeorge Wilson arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 524503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 525ea8dc4b6Seschrock return (err); 526ea8dc4b6Seschrock } 5275d7b4d43SMatthew Ahrens } else { 528fa9e4066Sahrens /* It's the meta-objset. */ 529503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 530db1741f5SJustin T. Gibbs os->os_compress = ZIO_COMPRESS_ON; 531503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa); 532b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 533edf345e6SMatthew Ahrens os->os_dedup_verify = B_FALSE; 534edf345e6SMatthew Ahrens os->os_logbias = ZFS_LOGBIAS_LATENCY; 535edf345e6SMatthew Ahrens os->os_sync = ZFS_SYNC_STANDARD; 536503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL; 537503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL; 538*54811da5SToomas Soome os->os_dnodesize = DNODE_MIN_SIZE; 539fa9e4066Sahrens } 540adb52d92SMatthew Ahrens /* 541adb52d92SMatthew Ahrens * These properties will be filled in by the logic in zfs_get_zplprop() 542adb52d92SMatthew Ahrens * when they are queried for the first time. 543adb52d92SMatthew Ahrens */ 544adb52d92SMatthew Ahrens os->os_version = OBJSET_PROP_UNINITIALIZED; 545adb52d92SMatthew Ahrens os->os_normalization = OBJSET_PROP_UNINITIALIZED; 546adb52d92SMatthew Ahrens os->os_utf8only = OBJSET_PROP_UNINITIALIZED; 547adb52d92SMatthew Ahrens os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED; 548fa9e4066Sahrens 549bc9014e6SJustin Gibbs if (ds == NULL || !ds->ds_is_snapshot) 5506e0cbcaaSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header; 551503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header); 552fa9e4066Sahrens 553fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 55494c2d0ebSMatthew Ahrens os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t), 55594c2d0ebSMatthew Ahrens offsetof(dnode_t, dn_dirty_link[i]), 55694c2d0ebSMatthew Ahrens dnode_multilist_index_func); 557fa9e4066Sahrens } 558503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t), 559fa9e4066Sahrens offsetof(dnode_t, dn_link)); 560503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 561fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link)); 562fa9e4066Sahrens 563503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 56494c2d0ebSMatthew Ahrens mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL); 565503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 566503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 567*54811da5SToomas Soome os->os_obj_next_percpu_len = boot_ncpus; 568*54811da5SToomas Soome os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len * 569*54811da5SToomas Soome sizeof (os->os_obj_next_percpu[0]), KM_SLEEP); 570503ad85cSMatthew Ahrens 571bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_meta_dnode, 572bc9014e6SJustin Gibbs DMU_META_DNODE_OBJECT, &os->os_meta_dnode); 573503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 574bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_userused_dnode, 575bc9014e6SJustin Gibbs DMU_USERUSED_OBJECT, &os->os_userused_dnode); 576bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_groupused_dnode, 577bc9014e6SJustin Gibbs DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode); 57814843421SMatthew Ahrens } 579fa9e4066Sahrens 580503ad85cSMatthew Ahrens *osp = os; 581ea8dc4b6Seschrock return (0); 582fa9e4066Sahrens } 583fa9e4066Sahrens 584503ad85cSMatthew Ahrens int 585503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 5863cb34c60Sahrens { 587503ad85cSMatthew Ahrens int err = 0; 5883cb34c60Sahrens 5899c3fd121SMatthew Ahrens /* 5909c3fd121SMatthew Ahrens * We shouldn't be doing anything with dsl_dataset_t's unless the 5919c3fd121SMatthew Ahrens * pool_config lock is held, or the dataset is long-held. 5929c3fd121SMatthew Ahrens */ 5939c3fd121SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) || 5949c3fd121SMatthew Ahrens dsl_dataset_long_held(ds)); 5959c3fd121SMatthew Ahrens 5963cb34c60Sahrens mutex_enter(&ds->ds_opening_lock); 5975d7b4d43SMatthew Ahrens if (ds->ds_objset == NULL) { 5985d7b4d43SMatthew Ahrens objset_t *os; 599c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 6003cb34c60Sahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 6015d7b4d43SMatthew Ahrens ds, dsl_dataset_get_blkptr(ds), &os); 602c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 6035d7b4d43SMatthew Ahrens 6045d7b4d43SMatthew Ahrens if (err == 0) { 6055d7b4d43SMatthew Ahrens mutex_enter(&ds->ds_lock); 6065d7b4d43SMatthew Ahrens ASSERT(ds->ds_objset == NULL); 6075d7b4d43SMatthew Ahrens ds->ds_objset = os; 6085d7b4d43SMatthew Ahrens mutex_exit(&ds->ds_lock); 6095d7b4d43SMatthew Ahrens } 6103cb34c60Sahrens } 6115d7b4d43SMatthew Ahrens *osp = ds->ds_objset; 6123cb34c60Sahrens mutex_exit(&ds->ds_opening_lock); 613503ad85cSMatthew Ahrens return (err); 6143cb34c60Sahrens } 6153cb34c60Sahrens 6163b2aab18SMatthew Ahrens /* 6173b2aab18SMatthew Ahrens * Holds the pool while the objset is held. Therefore only one objset 6183b2aab18SMatthew Ahrens * can be held at a time. 6193b2aab18SMatthew Ahrens */ 6203cb34c60Sahrens int 621503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp) 6223cb34c60Sahrens { 6233b2aab18SMatthew Ahrens dsl_pool_t *dp; 624503ad85cSMatthew Ahrens dsl_dataset_t *ds; 6253cb34c60Sahrens int err; 6263cb34c60Sahrens 6273b2aab18SMatthew Ahrens err = dsl_pool_hold(name, tag, &dp); 6283b2aab18SMatthew Ahrens if (err != 0) 6293b2aab18SMatthew Ahrens return (err); 6303b2aab18SMatthew Ahrens err = dsl_dataset_hold(dp, name, tag, &ds); 6313b2aab18SMatthew Ahrens if (err != 0) { 6323b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag); 633503ad85cSMatthew Ahrens return (err); 6343b2aab18SMatthew Ahrens } 635503ad85cSMatthew Ahrens 636503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp); 6373b2aab18SMatthew Ahrens if (err != 0) { 638503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag); 6393b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag); 6403b2aab18SMatthew Ahrens } 641503ad85cSMatthew Ahrens 6423cb34c60Sahrens return (err); 6433cb34c60Sahrens } 6443cb34c60Sahrens 64512380e1eSArne Jansen static int 64612380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type, 64712380e1eSArne Jansen boolean_t readonly, void *tag, objset_t **osp) 64812380e1eSArne Jansen { 64912380e1eSArne Jansen int err; 65012380e1eSArne Jansen 65112380e1eSArne Jansen err = dmu_objset_from_ds(ds, osp); 65212380e1eSArne Jansen if (err != 0) { 65312380e1eSArne Jansen dsl_dataset_disown(ds, tag); 65412380e1eSArne Jansen } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 65512380e1eSArne Jansen dsl_dataset_disown(ds, tag); 65612380e1eSArne Jansen return (SET_ERROR(EINVAL)); 65712380e1eSArne Jansen } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 65812380e1eSArne Jansen dsl_dataset_disown(ds, tag); 65912380e1eSArne Jansen return (SET_ERROR(EROFS)); 66012380e1eSArne Jansen } 66112380e1eSArne Jansen return (err); 66212380e1eSArne Jansen } 66312380e1eSArne Jansen 6643b2aab18SMatthew Ahrens /* 6653b2aab18SMatthew Ahrens * dsl_pool must not be held when this is called. 6663b2aab18SMatthew Ahrens * Upon successful return, there will be a longhold on the dataset, 6673b2aab18SMatthew Ahrens * and the dsl_pool will not be held. 6683b2aab18SMatthew Ahrens */ 669fa9e4066Sahrens int 670503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type, 671503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp) 672fa9e4066Sahrens { 6733b2aab18SMatthew Ahrens dsl_pool_t *dp; 674f18faf3fSek dsl_dataset_t *ds; 675f18faf3fSek int err; 676fa9e4066Sahrens 6773b2aab18SMatthew Ahrens err = dsl_pool_hold(name, FTAG, &dp); 6783b2aab18SMatthew Ahrens if (err != 0) 6793b2aab18SMatthew Ahrens return (err); 6803b2aab18SMatthew Ahrens err = dsl_dataset_own(dp, name, tag, &ds); 6813b2aab18SMatthew Ahrens if (err != 0) { 6823b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 683fa9e4066Sahrens return (err); 6843b2aab18SMatthew Ahrens } 68512380e1eSArne Jansen err = dmu_objset_own_impl(ds, type, readonly, tag, osp); 6863b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 68712380e1eSArne Jansen 6883cb34c60Sahrens return (err); 689fa9e4066Sahrens } 690fa9e4066Sahrens 69112380e1eSArne Jansen int 69212380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type, 69312380e1eSArne Jansen boolean_t readonly, void *tag, objset_t **osp) 69412380e1eSArne Jansen { 69512380e1eSArne Jansen dsl_dataset_t *ds; 69612380e1eSArne Jansen int err; 69712380e1eSArne Jansen 69812380e1eSArne Jansen err = dsl_dataset_own_obj(dp, obj, tag, &ds); 69912380e1eSArne Jansen if (err != 0) 70012380e1eSArne Jansen return (err); 70112380e1eSArne Jansen 70212380e1eSArne Jansen return (dmu_objset_own_impl(ds, type, readonly, tag, osp)); 70312380e1eSArne Jansen } 70412380e1eSArne Jansen 705fa9e4066Sahrens void 706503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag) 707fa9e4066Sahrens { 7083b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os); 709503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag); 7103b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag); 711503ad85cSMatthew Ahrens } 712503ad85cSMatthew Ahrens 71391948b51SKeith M Wesolowski /* 71491948b51SKeith M Wesolowski * When we are called, os MUST refer to an objset associated with a dataset 71591948b51SKeith M Wesolowski * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner 71691948b51SKeith M Wesolowski * == tag. We will then release and reacquire ownership of the dataset while 71791948b51SKeith M Wesolowski * holding the pool config_rwlock to avoid intervening namespace or ownership 71891948b51SKeith M Wesolowski * changes may occur. 71991948b51SKeith M Wesolowski * 72091948b51SKeith M Wesolowski * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to 72191948b51SKeith M Wesolowski * release the hold on its dataset and acquire a new one on the dataset of the 72291948b51SKeith M Wesolowski * same name so that it can be partially torn down and reconstructed. 72391948b51SKeith M Wesolowski */ 72491948b51SKeith M Wesolowski void 7255f5913bbSAndriy Gapon dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds, 7265f5913bbSAndriy Gapon void *tag) 72791948b51SKeith M Wesolowski { 72891948b51SKeith M Wesolowski dsl_pool_t *dp; 7299adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 73091948b51SKeith M Wesolowski 73191948b51SKeith M Wesolowski VERIFY3P(ds, !=, NULL); 73291948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, tag); 73391948b51SKeith M Wesolowski VERIFY(dsl_dataset_long_held(ds)); 73491948b51SKeith M Wesolowski 73591948b51SKeith M Wesolowski dsl_dataset_name(ds, name); 7365f5913bbSAndriy Gapon dp = ds->ds_dir->dd_pool; 73791948b51SKeith M Wesolowski dsl_pool_config_enter(dp, FTAG); 7385f5913bbSAndriy Gapon dsl_dataset_disown(ds, tag); 7395f5913bbSAndriy Gapon VERIFY0(dsl_dataset_own(dp, name, tag, newds)); 74091948b51SKeith M Wesolowski dsl_pool_config_exit(dp, FTAG); 74191948b51SKeith M Wesolowski } 74291948b51SKeith M Wesolowski 743503ad85cSMatthew Ahrens void 744503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag) 745503ad85cSMatthew Ahrens { 746503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag); 747fa9e4066Sahrens } 748fa9e4066Sahrens 7493b2aab18SMatthew Ahrens void 7501934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os) 751ea8dc4b6Seschrock { 752bc9014e6SJustin Gibbs dnode_t dn_marker; 753ea8dc4b6Seschrock dnode_t *dn; 754c543ec06Sahrens 755503ad85cSMatthew Ahrens mutex_enter(&os->os_lock); 756bc9014e6SJustin Gibbs dn = list_head(&os->os_dnodes); 757bc9014e6SJustin Gibbs while (dn != NULL) { 758bc9014e6SJustin Gibbs /* 759bc9014e6SJustin Gibbs * Skip dnodes without holds. We have to do this dance 760bc9014e6SJustin Gibbs * because dnode_add_ref() only works if there is already a 761bc9014e6SJustin Gibbs * hold. If the dnode has no holds, then it has no dbufs. 762bc9014e6SJustin Gibbs */ 763bc9014e6SJustin Gibbs if (dnode_add_ref(dn, FTAG)) { 764bc9014e6SJustin Gibbs list_insert_after(&os->os_dnodes, dn, &dn_marker); 765bc9014e6SJustin Gibbs mutex_exit(&os->os_lock); 766c543ec06Sahrens 767bc9014e6SJustin Gibbs dnode_evict_dbufs(dn); 768bc9014e6SJustin Gibbs dnode_rele(dn, FTAG); 769c543ec06Sahrens 770bc9014e6SJustin Gibbs mutex_enter(&os->os_lock); 771bc9014e6SJustin Gibbs dn = list_next(&os->os_dnodes, &dn_marker); 772bc9014e6SJustin Gibbs list_remove(&os->os_dnodes, &dn_marker); 773bc9014e6SJustin Gibbs } else { 774bc9014e6SJustin Gibbs dn = list_next(&os->os_dnodes, dn); 775bc9014e6SJustin Gibbs } 776ea8dc4b6Seschrock } 777503ad85cSMatthew Ahrens mutex_exit(&os->os_lock); 778bc9014e6SJustin Gibbs 779bc9014e6SJustin Gibbs if (DMU_USERUSED_DNODE(os) != NULL) { 780bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os)); 781bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_USERUSED_DNODE(os)); 782bc9014e6SJustin Gibbs } 783bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_META_DNODE(os)); 784ea8dc4b6Seschrock } 785ea8dc4b6Seschrock 786bc9014e6SJustin Gibbs /* 787bc9014e6SJustin Gibbs * Objset eviction processing is split into into two pieces. 788bc9014e6SJustin Gibbs * The first marks the objset as evicting, evicts any dbufs that 789bc9014e6SJustin Gibbs * have a refcount of zero, and then queues up the objset for the 790bc9014e6SJustin Gibbs * second phase of eviction. Once os->os_dnodes has been cleared by 791bc9014e6SJustin Gibbs * dnode_buf_pageout()->dnode_destroy(), the second phase is executed. 792bc9014e6SJustin Gibbs * The second phase closes the special dnodes, dequeues the objset from 793bc9014e6SJustin Gibbs * the list of those undergoing eviction, and finally frees the objset. 794bc9014e6SJustin Gibbs * 795bc9014e6SJustin Gibbs * NOTE: Due to asynchronous eviction processing (invocation of 796bc9014e6SJustin Gibbs * dnode_buf_pageout()), it is possible for the meta dnode for the 797bc9014e6SJustin Gibbs * objset to have no holds even though os->os_dnodes is not empty. 798bc9014e6SJustin Gibbs */ 799fa9e4066Sahrens void 800503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os) 801fa9e4066Sahrens { 802503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 803fa9e4066Sahrens 804b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 805b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t)); 806fa9e4066Sahrens 80703bad06fSJustin Gibbs if (ds) 80803bad06fSJustin Gibbs dsl_prop_unregister_all(ds, os); 809fa9e4066Sahrens 8100a586ceaSMark Shellenbaum if (os->os_sa) 8110a586ceaSMark Shellenbaum sa_tear_down(os); 8120a586ceaSMark Shellenbaum 8133b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(os); 814ea8dc4b6Seschrock 815bc9014e6SJustin Gibbs mutex_enter(&os->os_lock); 816bc9014e6SJustin Gibbs spa_evicting_os_register(os->os_spa, os); 817bc9014e6SJustin Gibbs if (list_is_empty(&os->os_dnodes)) { 818bc9014e6SJustin Gibbs mutex_exit(&os->os_lock); 819bc9014e6SJustin Gibbs dmu_objset_evict_done(os); 820bc9014e6SJustin Gibbs } else { 821bc9014e6SJustin Gibbs mutex_exit(&os->os_lock); 822bc9014e6SJustin Gibbs } 823bc9014e6SJustin Gibbs } 824bc9014e6SJustin Gibbs 825bc9014e6SJustin Gibbs void 826bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os) 827bc9014e6SJustin Gibbs { 828bc9014e6SJustin Gibbs ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 829bc9014e6SJustin Gibbs 830744947dcSTom Erickson dnode_special_close(&os->os_meta_dnode); 831744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)) { 832744947dcSTom Erickson dnode_special_close(&os->os_userused_dnode); 833744947dcSTom Erickson dnode_special_close(&os->os_groupused_dnode); 83414843421SMatthew Ahrens } 835503ad85cSMatthew Ahrens zil_free(os->os_zil); 836fa9e4066Sahrens 837dcbf3bd6SGeorge Wilson arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf); 838744947dcSTom Erickson 839744947dcSTom Erickson /* 840744947dcSTom Erickson * This is a barrier to prevent the objset from going away in 841744947dcSTom Erickson * dnode_move() until we can safely ensure that the objset is still in 842744947dcSTom Erickson * use. We consider the objset valid before the barrier and invalid 843744947dcSTom Erickson * after the barrier. 844744947dcSTom Erickson */ 845744947dcSTom Erickson rw_enter(&os_lock, RW_READER); 846744947dcSTom Erickson rw_exit(&os_lock); 847744947dcSTom Erickson 848*54811da5SToomas Soome kmem_free(os->os_obj_next_percpu, 849*54811da5SToomas Soome os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0])); 850*54811da5SToomas Soome 851503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock); 85294c2d0ebSMatthew Ahrens mutex_destroy(&os->os_userused_lock); 853503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock); 854503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock); 85594c2d0ebSMatthew Ahrens for (int i = 0; i < TXG_SIZE; i++) { 85694c2d0ebSMatthew Ahrens multilist_destroy(os->os_dirty_dnodes[i]); 85794c2d0ebSMatthew Ahrens } 858bc9014e6SJustin Gibbs spa_evicting_os_deregister(os->os_spa, os); 859503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t)); 860fa9e4066Sahrens } 861fa9e4066Sahrens 86271eb0538SChris Kirby timestruc_t 86371eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os) 86471eb0538SChris Kirby { 86571eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 86671eb0538SChris Kirby } 86771eb0538SChris Kirby 868fa9e4066Sahrens /* called from dsl for meta-objset */ 869503ad85cSMatthew Ahrens objset_t * 870c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 871c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx) 872fa9e4066Sahrens { 873503ad85cSMatthew Ahrens objset_t *os; 874fa9e4066Sahrens dnode_t *mdn; 875fa9e4066Sahrens 876fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 8773b2aab18SMatthew Ahrens 878feaa74e4SMark Maybee if (ds != NULL) 8793b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 880feaa74e4SMark Maybee else 8813b2aab18SMatthew Ahrens VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os)); 882feaa74e4SMark Maybee 883744947dcSTom Erickson mdn = DMU_META_DNODE(os); 884fa9e4066Sahrens 885*54811da5SToomas Soome dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT, 886*54811da5SToomas Soome DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx); 887fa9e4066Sahrens 888fa9e4066Sahrens /* 889fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels 890fa9e4066Sahrens * later, because then we could do it in quescing context while 891fa9e4066Sahrens * we are also accessing it in open context. 892fa9e4066Sahrens * 893fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL), 894fa9e4066Sahrens * because the MOS is only updated in syncing context. 895fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that 896fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates 897fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters. 898fa9e4066Sahrens */ 899ea8dc4b6Seschrock if (ds != NULL) { 900ea8dc4b6Seschrock int levels = 1; 901ea8dc4b6Seschrock 902ea8dc4b6Seschrock /* 903ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode 9044b5c8e93SMatthew Ahrens * to contain DN_MAX_OBJECT dnodes. Note that in order to 9054b5c8e93SMatthew Ahrens * ensure that we do not overflow 64 bits, there has to be 9064b5c8e93SMatthew Ahrens * a nlevels that gives us a number of blocks > DN_MAX_OBJECT 9074b5c8e93SMatthew Ahrens * but < 2^64. Therefore, 9084b5c8e93SMatthew Ahrens * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be 9094b5c8e93SMatthew Ahrens * less than (64 - log2(DN_MAX_OBJECT)) (16). 910ea8dc4b6Seschrock */ 9114b5c8e93SMatthew Ahrens while ((uint64_t)mdn->dn_nblkptr << 9124b5c8e93SMatthew Ahrens (mdn->dn_datablkshift - DNODE_SHIFT + 913ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 9144b5c8e93SMatthew Ahrens DN_MAX_OBJECT) 915ea8dc4b6Seschrock levels++; 916ea8dc4b6Seschrock 917fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 918ea8dc4b6Seschrock mdn->dn_nlevels = levels; 919ea8dc4b6Seschrock } 920fa9e4066Sahrens 921fa9e4066Sahrens ASSERT(type != DMU_OST_NONE); 922fa9e4066Sahrens ASSERT(type != DMU_OST_ANY); 923fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES); 924503ad85cSMatthew Ahrens os->os_phys->os_type = type; 925503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 926503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 927503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags; 92814843421SMatthew Ahrens } 929fa9e4066Sahrens 930fa9e4066Sahrens dsl_dataset_dirty(ds, tx); 931fa9e4066Sahrens 932503ad85cSMatthew Ahrens return (os); 933fa9e4066Sahrens } 934fa9e4066Sahrens 9353b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg { 9363b2aab18SMatthew Ahrens const char *doca_name; 9373b2aab18SMatthew Ahrens cred_t *doca_cred; 9383b2aab18SMatthew Ahrens void (*doca_userfunc)(objset_t *os, void *arg, 9393b2aab18SMatthew Ahrens cred_t *cr, dmu_tx_t *tx); 9403b2aab18SMatthew Ahrens void *doca_userarg; 9413b2aab18SMatthew Ahrens dmu_objset_type_t doca_type; 9423b2aab18SMatthew Ahrens uint64_t doca_flags; 9433b2aab18SMatthew Ahrens } dmu_objset_create_arg_t; 944fa9e4066Sahrens 945ecd6cf80Smarks /*ARGSUSED*/ 946fa9e4066Sahrens static int 9473b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx) 948fa9e4066Sahrens { 9493b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg; 9503b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 9513b2aab18SMatthew Ahrens dsl_dir_t *pdd; 9523b2aab18SMatthew Ahrens const char *tail; 9533b2aab18SMatthew Ahrens int error; 9541d452cf5Sahrens 9553b2aab18SMatthew Ahrens if (strchr(doca->doca_name, '@') != NULL) 956be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 9571d452cf5Sahrens 9589adfa60dSMatthew Ahrens if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN) 9599adfa60dSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 9609adfa60dSMatthew Ahrens 9615ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(doca->doca_name) != 0) 9625ac95da7SSerapheim Dimitropoulos return (SET_ERROR(ENAMETOOLONG)); 9635ac95da7SSerapheim Dimitropoulos 9643b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail); 9653b2aab18SMatthew Ahrens if (error != 0) 9663b2aab18SMatthew Ahrens return (error); 9673b2aab18SMatthew Ahrens if (tail == NULL) { 9683b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 969be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 9701d452cf5Sahrens } 971a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 972a2afb611SJerry Jelinek doca->doca_cred); 9733b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 974ecd6cf80Smarks 975a2afb611SJerry Jelinek return (error); 9761d452cf5Sahrens } 9771d452cf5Sahrens 9781d452cf5Sahrens static void 9793b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx) 9801d452cf5Sahrens { 9813b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg; 9823b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 9833b2aab18SMatthew Ahrens dsl_dir_t *pdd; 9843b2aab18SMatthew Ahrens const char *tail; 9854445fffbSMatthew Ahrens dsl_dataset_t *ds; 9863b2aab18SMatthew Ahrens uint64_t obj; 9874445fffbSMatthew Ahrens blkptr_t *bp; 9883b2aab18SMatthew Ahrens objset_t *os; 989fa9e4066Sahrens 9903b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail)); 991fa9e4066Sahrens 9923b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags, 9933b2aab18SMatthew Ahrens doca->doca_cred, tx); 994fa9e4066Sahrens 9953b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); 996c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 9974445fffbSMatthew Ahrens bp = dsl_dataset_get_blkptr(ds); 9983b2aab18SMatthew Ahrens os = dmu_objset_create_impl(pdd->dd_pool->dp_spa, 9993b2aab18SMatthew Ahrens ds, bp, doca->doca_type, tx); 1000c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 1001fa9e4066Sahrens 10023b2aab18SMatthew Ahrens if (doca->doca_userfunc != NULL) { 10033b2aab18SMatthew Ahrens doca->doca_userfunc(os, doca->doca_userarg, 10043b2aab18SMatthew Ahrens doca->doca_cred, tx); 1005fa9e4066Sahrens } 1006ecd6cf80Smarks 10073b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "create", tx, ""); 10084445fffbSMatthew Ahrens dsl_dataset_rele(ds, FTAG); 10093b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 1010fa9e4066Sahrens } 1011fa9e4066Sahrens 1012fa9e4066Sahrens int 1013ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 1014ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 1015fa9e4066Sahrens { 10163b2aab18SMatthew Ahrens dmu_objset_create_arg_t doca; 1017fa9e4066Sahrens 10183b2aab18SMatthew Ahrens doca.doca_name = name; 10193b2aab18SMatthew Ahrens doca.doca_cred = CRED(); 10203b2aab18SMatthew Ahrens doca.doca_flags = flags; 10213b2aab18SMatthew Ahrens doca.doca_userfunc = func; 10223b2aab18SMatthew Ahrens doca.doca_userarg = arg; 10233b2aab18SMatthew Ahrens doca.doca_type = type; 1024ecd6cf80Smarks 10253b2aab18SMatthew Ahrens return (dsl_sync_task(name, 10267d46dc6cSMatthew Ahrens dmu_objset_create_check, dmu_objset_create_sync, &doca, 10277d46dc6cSMatthew Ahrens 5, ZFS_SPACE_CHECK_NORMAL)); 1028ae46e4c7SMatthew Ahrens } 1029ae46e4c7SMatthew Ahrens 10303b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg { 10313b2aab18SMatthew Ahrens const char *doca_clone; 10323b2aab18SMatthew Ahrens const char *doca_origin; 10333b2aab18SMatthew Ahrens cred_t *doca_cred; 10343b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t; 10353b2aab18SMatthew Ahrens 10363b2aab18SMatthew Ahrens /*ARGSUSED*/ 10373b2aab18SMatthew Ahrens static int 10383b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx) 1039ae46e4c7SMatthew Ahrens { 10403b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg; 1041ae46e4c7SMatthew Ahrens dsl_dir_t *pdd; 1042ae46e4c7SMatthew Ahrens const char *tail; 10433b2aab18SMatthew Ahrens int error; 10443b2aab18SMatthew Ahrens dsl_dataset_t *origin; 10453b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 1046ae46e4c7SMatthew Ahrens 10473b2aab18SMatthew Ahrens if (strchr(doca->doca_clone, '@') != NULL) 1048be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 10493b2aab18SMatthew Ahrens 10509adfa60dSMatthew Ahrens if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN) 10519adfa60dSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 10529adfa60dSMatthew Ahrens 10533b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail); 10543b2aab18SMatthew Ahrens if (error != 0) 10553b2aab18SMatthew Ahrens return (error); 1056ae46e4c7SMatthew Ahrens if (tail == NULL) { 10573b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 1058be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 1059fa9e4066Sahrens } 106003b1c297SAlexander Eremin 1061a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 1062a2afb611SJerry Jelinek doca->doca_cred); 1063a2afb611SJerry Jelinek if (error != 0) { 1064a2afb611SJerry Jelinek dsl_dir_rele(pdd, FTAG); 1065a2afb611SJerry Jelinek return (SET_ERROR(EDQUOT)); 1066a2afb611SJerry Jelinek } 10673b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 1068fa9e4066Sahrens 10693b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin); 10703b2aab18SMatthew Ahrens if (error != 0) 107199d5e173STim Haley return (error); 107299d5e173STim Haley 10733b2aab18SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */ 1074bc9014e6SJustin Gibbs if (!origin->ds_is_snapshot) { 10753b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 1076be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 107792241e0bSTom Erickson } 10783b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 107999d5e173STim Haley 10803b2aab18SMatthew Ahrens return (0); 1081ea2f5b9eSMatthew Ahrens } 10821d452cf5Sahrens 10833b2aab18SMatthew Ahrens static void 10843b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx) 10851d452cf5Sahrens { 10863b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg; 10873b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 10883b2aab18SMatthew Ahrens dsl_dir_t *pdd; 10893b2aab18SMatthew Ahrens const char *tail; 10903b2aab18SMatthew Ahrens dsl_dataset_t *origin, *ds; 10913b2aab18SMatthew Ahrens uint64_t obj; 10929adfa60dSMatthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 10934445fffbSMatthew Ahrens 10943b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail)); 10953b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin)); 10964445fffbSMatthew Ahrens 10973b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, origin, 0, 10983b2aab18SMatthew Ahrens doca->doca_cred, tx); 1099f2e10be3Srm 11003b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); 11013b2aab18SMatthew Ahrens dsl_dataset_name(origin, namebuf); 11023b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "clone", tx, 11033b2aab18SMatthew Ahrens "origin=%s (%llu)", namebuf, origin->ds_object); 11043b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 11053b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 11063b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG); 11071d452cf5Sahrens } 11081d452cf5Sahrens 11091d452cf5Sahrens int 11103b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin) 11111d452cf5Sahrens { 11123b2aab18SMatthew Ahrens dmu_objset_clone_arg_t doca; 11134445fffbSMatthew Ahrens 11143b2aab18SMatthew Ahrens doca.doca_clone = clone; 11153b2aab18SMatthew Ahrens doca.doca_origin = origin; 11163b2aab18SMatthew Ahrens doca.doca_cred = CRED(); 111799d5e173STim Haley 11183b2aab18SMatthew Ahrens return (dsl_sync_task(clone, 11197d46dc6cSMatthew Ahrens dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 11207d46dc6cSMatthew Ahrens 5, ZFS_SPACE_CHECK_NORMAL)); 11214445fffbSMatthew Ahrens } 11224445fffbSMatthew Ahrens 11235cabbc6bSPrashanth Sreenivasa static int 11245cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg) 11255cabbc6bSPrashanth Sreenivasa { 11265cabbc6bSPrashanth Sreenivasa int error = 0; 11275cabbc6bSPrashanth Sreenivasa uint64_t object = 0; 11285cabbc6bSPrashanth Sreenivasa while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 11295cabbc6bSPrashanth Sreenivasa error = dmu_object_remap_indirects(os, object, 11305cabbc6bSPrashanth Sreenivasa last_removed_txg); 11315cabbc6bSPrashanth Sreenivasa /* 11325cabbc6bSPrashanth Sreenivasa * If the ZPL removed the object before we managed to dnode_hold 11335cabbc6bSPrashanth Sreenivasa * it, we would get an ENOENT. If the ZPL declares its intent 11345cabbc6bSPrashanth Sreenivasa * to remove the object (dnode_free) before we manage to 11355cabbc6bSPrashanth Sreenivasa * dnode_hold it, we would get an EEXIST. In either case, we 11365cabbc6bSPrashanth Sreenivasa * want to continue remapping the other objects in the objset; 11375cabbc6bSPrashanth Sreenivasa * in all other cases, we want to break early. 11385cabbc6bSPrashanth Sreenivasa */ 11395cabbc6bSPrashanth Sreenivasa if (error != 0 && error != ENOENT && error != EEXIST) { 11405cabbc6bSPrashanth Sreenivasa break; 11415cabbc6bSPrashanth Sreenivasa } 11425cabbc6bSPrashanth Sreenivasa } 11435cabbc6bSPrashanth Sreenivasa if (error == ESRCH) { 11445cabbc6bSPrashanth Sreenivasa error = 0; 11455cabbc6bSPrashanth Sreenivasa } 11465cabbc6bSPrashanth Sreenivasa return (error); 11475cabbc6bSPrashanth Sreenivasa } 11485cabbc6bSPrashanth Sreenivasa 11495cabbc6bSPrashanth Sreenivasa int 11505cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects(const char *fsname) 11515cabbc6bSPrashanth Sreenivasa { 11525cabbc6bSPrashanth Sreenivasa int error = 0; 11535cabbc6bSPrashanth Sreenivasa objset_t *os = NULL; 11545cabbc6bSPrashanth Sreenivasa uint64_t last_removed_txg; 11555cabbc6bSPrashanth Sreenivasa uint64_t remap_start_txg; 11565cabbc6bSPrashanth Sreenivasa dsl_dir_t *dd; 11575cabbc6bSPrashanth Sreenivasa 11585cabbc6bSPrashanth Sreenivasa error = dmu_objset_hold(fsname, FTAG, &os); 11595cabbc6bSPrashanth Sreenivasa if (error != 0) { 11605cabbc6bSPrashanth Sreenivasa return (error); 11615cabbc6bSPrashanth Sreenivasa } 11625cabbc6bSPrashanth Sreenivasa dd = dmu_objset_ds(os)->ds_dir; 11635cabbc6bSPrashanth Sreenivasa 11645cabbc6bSPrashanth Sreenivasa if (!spa_feature_is_enabled(dmu_objset_spa(os), 11655cabbc6bSPrashanth Sreenivasa SPA_FEATURE_OBSOLETE_COUNTS)) { 11665cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 11675cabbc6bSPrashanth Sreenivasa return (SET_ERROR(ENOTSUP)); 11685cabbc6bSPrashanth Sreenivasa } 11695cabbc6bSPrashanth Sreenivasa 11705cabbc6bSPrashanth Sreenivasa if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) { 11715cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 11725cabbc6bSPrashanth Sreenivasa return (SET_ERROR(EINVAL)); 11735cabbc6bSPrashanth Sreenivasa } 11745cabbc6bSPrashanth Sreenivasa 11755cabbc6bSPrashanth Sreenivasa /* 11765cabbc6bSPrashanth Sreenivasa * If there has not been a removal, we're done. 11775cabbc6bSPrashanth Sreenivasa */ 11785cabbc6bSPrashanth Sreenivasa last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os)); 11795cabbc6bSPrashanth Sreenivasa if (last_removed_txg == -1ULL) { 11805cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 11815cabbc6bSPrashanth Sreenivasa return (0); 11825cabbc6bSPrashanth Sreenivasa } 11835cabbc6bSPrashanth Sreenivasa 11845cabbc6bSPrashanth Sreenivasa /* 11855cabbc6bSPrashanth Sreenivasa * If we have remapped since the last removal, we're done. 11865cabbc6bSPrashanth Sreenivasa */ 11875cabbc6bSPrashanth Sreenivasa if (dsl_dir_is_zapified(dd)) { 11885cabbc6bSPrashanth Sreenivasa uint64_t last_remap_txg; 11895cabbc6bSPrashanth Sreenivasa if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)), 11905cabbc6bSPrashanth Sreenivasa dd->dd_object, DD_FIELD_LAST_REMAP_TXG, 11915cabbc6bSPrashanth Sreenivasa sizeof (last_remap_txg), 1, &last_remap_txg) == 0 && 11925cabbc6bSPrashanth Sreenivasa last_remap_txg > last_removed_txg) { 11935cabbc6bSPrashanth Sreenivasa dmu_objset_rele(os, FTAG); 11945cabbc6bSPrashanth Sreenivasa return (0); 11955cabbc6bSPrashanth Sreenivasa } 11965cabbc6bSPrashanth Sreenivasa } 11975cabbc6bSPrashanth Sreenivasa 11985cabbc6bSPrashanth Sreenivasa dsl_dataset_long_hold(dmu_objset_ds(os), FTAG); 11995cabbc6bSPrashanth Sreenivasa dsl_pool_rele(dmu_objset_pool(os), FTAG); 12005cabbc6bSPrashanth Sreenivasa 12015cabbc6bSPrashanth Sreenivasa remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os)); 12025cabbc6bSPrashanth Sreenivasa error = dmu_objset_remap_indirects_impl(os, last_removed_txg); 12035cabbc6bSPrashanth Sreenivasa if (error == 0) { 12045cabbc6bSPrashanth Sreenivasa /* 12055cabbc6bSPrashanth Sreenivasa * We update the last_remap_txg to be the start txg so that 12065cabbc6bSPrashanth Sreenivasa * we can guarantee that every block older than last_remap_txg 12075cabbc6bSPrashanth Sreenivasa * that can be remapped has been remapped. 12085cabbc6bSPrashanth Sreenivasa */ 12095cabbc6bSPrashanth Sreenivasa error = dsl_dir_update_last_remap_txg(dd, remap_start_txg); 12105cabbc6bSPrashanth Sreenivasa } 12115cabbc6bSPrashanth Sreenivasa 12125cabbc6bSPrashanth Sreenivasa dsl_dataset_long_rele(dmu_objset_ds(os), FTAG); 12135cabbc6bSPrashanth Sreenivasa dsl_dataset_rele(dmu_objset_ds(os), FTAG); 12145cabbc6bSPrashanth Sreenivasa 12155cabbc6bSPrashanth Sreenivasa return (error); 12165cabbc6bSPrashanth Sreenivasa } 12175cabbc6bSPrashanth Sreenivasa 12184445fffbSMatthew Ahrens int 12194445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname) 12204445fffbSMatthew Ahrens { 12214445fffbSMatthew Ahrens int err; 12224445fffbSMatthew Ahrens char *longsnap = kmem_asprintf("%s@%s", fsname, snapname); 12234445fffbSMatthew Ahrens nvlist_t *snaps = fnvlist_alloc(); 12244445fffbSMatthew Ahrens 12254445fffbSMatthew Ahrens fnvlist_add_boolean(snaps, longsnap); 12264445fffbSMatthew Ahrens strfree(longsnap); 12273b2aab18SMatthew Ahrens err = dsl_dataset_snapshot(snaps, NULL, NULL); 12283b2aab18SMatthew Ahrens fnvlist_free(snaps); 12294445fffbSMatthew Ahrens return (err); 12304445fffbSMatthew Ahrens } 12314445fffbSMatthew Ahrens 1232fa9e4066Sahrens static void 123394c2d0ebSMatthew Ahrens dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx) 1234fa9e4066Sahrens { 1235c717a561Smaybee dnode_t *dn; 1236faafa6e3Sahrens 123794c2d0ebSMatthew Ahrens while ((dn = multilist_sublist_head(list)) != NULL) { 1238c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 1239c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending); 1240c717a561Smaybee /* 124114843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the 124214843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync(). 1243c717a561Smaybee */ 1244c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 1245c717a561Smaybee ASSERT(dn->dn_zio); 1246faafa6e3Sahrens 1247c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 124894c2d0ebSMatthew Ahrens multilist_sublist_remove(list, dn); 124914843421SMatthew Ahrens 125094c2d0ebSMatthew Ahrens multilist_t *newlist = dn->dn_objset->os_synced_dnodes; 125194c2d0ebSMatthew Ahrens if (newlist != NULL) { 125214843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist); 125394c2d0ebSMatthew Ahrens multilist_insert(newlist, dn); 125414843421SMatthew Ahrens } 125514843421SMatthew Ahrens 1256c717a561Smaybee dnode_sync(dn, tx); 1257fa9e4066Sahrens } 1258fa9e4066Sahrens } 1259fa9e4066Sahrens 1260fa9e4066Sahrens /* ARGSUSED */ 1261fa9e4066Sahrens static void 1262b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 1263fa9e4066Sahrens { 1264e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 1265503ad85cSMatthew Ahrens objset_t *os = arg; 1266c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 1267fa9e4066Sahrens 12685d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); 12693b2aab18SMatthew Ahrens ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET); 12703b2aab18SMatthew Ahrens ASSERT0(BP_GET_LEVEL(bp)); 12710a4e9518Sgw 1272fa9e4066Sahrens /* 127314843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects 127414843421SMatthew Ahrens * allocated in the object set (not counting the "special" 127514843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta 127614843421SMatthew Ahrens * dnode and user/group accounting objects). 1277fa9e4066Sahrens */ 127814843421SMatthew Ahrens bp->blk_fill = 0; 1279e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++) 12805d7b4d43SMatthew Ahrens bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]); 1281c166b69dSPaul Dagnelie if (os->os_dsl_dataset != NULL) 1282c166b69dSPaul Dagnelie rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG); 1283c166b69dSPaul Dagnelie *os->os_rootbp = *bp; 1284c166b69dSPaul Dagnelie if (os->os_dsl_dataset != NULL) 1285c166b69dSPaul Dagnelie rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG); 1286b24ab676SJeff Bonwick } 1287b24ab676SJeff Bonwick 1288b24ab676SJeff Bonwick /* ARGSUSED */ 1289b24ab676SJeff Bonwick static void 1290b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1291b24ab676SJeff Bonwick { 1292b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 1293b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig; 1294b24ab676SJeff Bonwick objset_t *os = arg; 12950a4e9518Sgw 1296e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1297b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig)); 1298e14bb325SJeff Bonwick } else { 1299b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 1300b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx; 1301b24ab676SJeff Bonwick 1302b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1303b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx); 13040a4e9518Sgw } 1305c166b69dSPaul Dagnelie kmem_free(bp, sizeof (*bp)); 1306c717a561Smaybee } 1307c717a561Smaybee 130894c2d0ebSMatthew Ahrens typedef struct sync_dnodes_arg { 130994c2d0ebSMatthew Ahrens multilist_t *sda_list; 131094c2d0ebSMatthew Ahrens int sda_sublist_idx; 131194c2d0ebSMatthew Ahrens multilist_t *sda_newlist; 131294c2d0ebSMatthew Ahrens dmu_tx_t *sda_tx; 131394c2d0ebSMatthew Ahrens } sync_dnodes_arg_t; 131494c2d0ebSMatthew Ahrens 131594c2d0ebSMatthew Ahrens static void 131694c2d0ebSMatthew Ahrens sync_dnodes_task(void *arg) 131794c2d0ebSMatthew Ahrens { 131894c2d0ebSMatthew Ahrens sync_dnodes_arg_t *sda = arg; 131994c2d0ebSMatthew Ahrens 132094c2d0ebSMatthew Ahrens multilist_sublist_t *ms = 132194c2d0ebSMatthew Ahrens multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx); 132294c2d0ebSMatthew Ahrens 132394c2d0ebSMatthew Ahrens dmu_objset_sync_dnodes(ms, sda->sda_tx); 132494c2d0ebSMatthew Ahrens 132594c2d0ebSMatthew Ahrens multilist_sublist_unlock(ms); 132694c2d0ebSMatthew Ahrens 132794c2d0ebSMatthew Ahrens kmem_free(sda, sizeof (*sda)); 132894c2d0ebSMatthew Ahrens } 132994c2d0ebSMatthew Ahrens 133094c2d0ebSMatthew Ahrens 1331fa9e4066Sahrens /* called from dsl */ 1332fa9e4066Sahrens void 1333503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1334fa9e4066Sahrens { 1335fa9e4066Sahrens int txgoff; 13367802d7bfSMatthew Ahrens zbookmark_phys_t zb; 1337b24ab676SJeff Bonwick zio_prop_t zp; 1338c717a561Smaybee zio_t *zio; 1339c717a561Smaybee list_t *list; 1340c717a561Smaybee dbuf_dirty_record_t *dr; 1341c166b69dSPaul Dagnelie blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP); 1342c166b69dSPaul Dagnelie *blkptr_copy = *os->os_rootbp; 1343c717a561Smaybee 1344c717a561Smaybee dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1345fa9e4066Sahrens 1346fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1347fa9e4066Sahrens /* XXX the write_done callback should really give us the tx... */ 1348fa9e4066Sahrens os->os_synctx = tx; 1349fa9e4066Sahrens 135087bd5c1eSahrens if (os->os_dsl_dataset == NULL) { 135187bd5c1eSahrens /* 135287bd5c1eSahrens * This is the MOS. If we have upgraded, 135387bd5c1eSahrens * spa_max_replication() could change, so reset 135487bd5c1eSahrens * os_copies here. 135587bd5c1eSahrens */ 135687bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa); 135787bd5c1eSahrens } 135887bd5c1eSahrens 1359fa9e4066Sahrens /* 1360c717a561Smaybee * Create the root block IO 1361fa9e4066Sahrens */ 1362b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1363b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1364b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 13651b912ec7SGeorge Wilson arc_release(os->os_phys_buf, &os->os_phys_buf); 1366b24ab676SJeff Bonwick 1367adaec86aSMatthew Ahrens dmu_write_policy(os, NULL, 0, 0, &zp); 1368b24ab676SJeff Bonwick 1369b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg, 1370c166b69dSPaul Dagnelie blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), 13718df0bcf0SPaul Dagnelie &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done, 13728df0bcf0SPaul Dagnelie os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 1373c717a561Smaybee 1374c717a561Smaybee /* 137514843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block 1376c717a561Smaybee */ 1377744947dcSTom Erickson DMU_META_DNODE(os)->dn_zio = zio; 1378744947dcSTom Erickson dnode_sync(DMU_META_DNODE(os), tx); 1379c717a561Smaybee 138014843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags; 138114843421SMatthew Ahrens 1382744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) && 1383744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1384744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_zio = zio; 1385744947dcSTom Erickson dnode_sync(DMU_USERUSED_DNODE(os), tx); 1386744947dcSTom Erickson DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1387744947dcSTom Erickson dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 138814843421SMatthew Ahrens } 138914843421SMatthew Ahrens 1390c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK; 1391fa9e4066Sahrens 139214843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) { 139314843421SMatthew Ahrens /* 139414843421SMatthew Ahrens * We must create the list here because it uses the 139594c2d0ebSMatthew Ahrens * dn_dirty_link[] of this txg. But it may already 139694c2d0ebSMatthew Ahrens * exist because we call dsl_dataset_sync() twice per txg. 139714843421SMatthew Ahrens */ 139894c2d0ebSMatthew Ahrens if (os->os_synced_dnodes == NULL) { 139994c2d0ebSMatthew Ahrens os->os_synced_dnodes = 140094c2d0ebSMatthew Ahrens multilist_create(sizeof (dnode_t), 140194c2d0ebSMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff]), 140294c2d0ebSMatthew Ahrens dnode_multilist_index_func); 140394c2d0ebSMatthew Ahrens } else { 140494c2d0ebSMatthew Ahrens ASSERT3U(os->os_synced_dnodes->ml_offset, ==, 140594c2d0ebSMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff])); 140694c2d0ebSMatthew Ahrens } 140714843421SMatthew Ahrens } 140814843421SMatthew Ahrens 140994c2d0ebSMatthew Ahrens for (int i = 0; 141094c2d0ebSMatthew Ahrens i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) { 141194c2d0ebSMatthew Ahrens sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP); 141294c2d0ebSMatthew Ahrens sda->sda_list = os->os_dirty_dnodes[txgoff]; 141394c2d0ebSMatthew Ahrens sda->sda_sublist_idx = i; 141494c2d0ebSMatthew Ahrens sda->sda_tx = tx; 141594c2d0ebSMatthew Ahrens (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, 141694c2d0ebSMatthew Ahrens sync_dnodes_task, sda, 0); 141794c2d0ebSMatthew Ahrens /* callback frees sda */ 141894c2d0ebSMatthew Ahrens } 141994c2d0ebSMatthew Ahrens taskq_wait(dmu_objset_pool(os)->dp_sync_taskq); 1420fa9e4066Sahrens 1421744947dcSTom Erickson list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 142294c2d0ebSMatthew Ahrens while ((dr = list_head(list)) != NULL) { 14233b2aab18SMatthew Ahrens ASSERT0(dr->dr_dbuf->db_level); 1424c717a561Smaybee list_remove(list, dr); 1425c717a561Smaybee if (dr->dr_zio) 1426c717a561Smaybee zio_nowait(dr->dr_zio); 1427c717a561Smaybee } 1428af346df5SNed Bass 1429af346df5SNed Bass /* Enable dnode backfill if enough objects have been freed. */ 1430af346df5SNed Bass if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) { 1431af346df5SNed Bass os->os_rescan_dnodes = B_TRUE; 1432af346df5SNed Bass os->os_freed_dnodes = 0; 1433af346df5SNed Bass } 1434af346df5SNed Bass 1435c717a561Smaybee /* 1436c717a561Smaybee * Free intent log blocks up to this tx. 1437c717a561Smaybee */ 1438c717a561Smaybee zil_sync(os->os_zil, tx); 1439088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header; 1440c717a561Smaybee zio_nowait(zio); 1441fa9e4066Sahrens } 1442fa9e4066Sahrens 1443b24ab676SJeff Bonwick boolean_t 1444b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1445b24ab676SJeff Bonwick { 144694c2d0ebSMatthew Ahrens return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK])); 1447b24ab676SJeff Bonwick } 1448b24ab676SJeff Bonwick 1449744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 145014843421SMatthew Ahrens 145114843421SMatthew Ahrens void 145214843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 145314843421SMatthew Ahrens { 145414843421SMatthew Ahrens used_cbs[ost] = cb; 145514843421SMatthew Ahrens } 145614843421SMatthew Ahrens 145714843421SMatthew Ahrens boolean_t 1458503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os) 145914843421SMatthew Ahrens { 146014843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1461744947dcSTom Erickson used_cbs[os->os_phys->os_type] != NULL && 1462744947dcSTom Erickson DMU_USERUSED_DNODE(os) != NULL); 146314843421SMatthew Ahrens } 146414843421SMatthew Ahrens 14658b70546bSMatthew Ahrens typedef struct userquota_node { 14668b70546bSMatthew Ahrens uint64_t uqn_id; 14678b70546bSMatthew Ahrens int64_t uqn_delta; 14688b70546bSMatthew Ahrens avl_node_t uqn_node; 14698b70546bSMatthew Ahrens } userquota_node_t; 14708b70546bSMatthew Ahrens 14718b70546bSMatthew Ahrens typedef struct userquota_cache { 14728b70546bSMatthew Ahrens avl_tree_t uqc_user_deltas; 14738b70546bSMatthew Ahrens avl_tree_t uqc_group_deltas; 14748b70546bSMatthew Ahrens } userquota_cache_t; 14758b70546bSMatthew Ahrens 14768b70546bSMatthew Ahrens static int 14778b70546bSMatthew Ahrens userquota_compare(const void *l, const void *r) 14788b70546bSMatthew Ahrens { 14798b70546bSMatthew Ahrens const userquota_node_t *luqn = l; 14808b70546bSMatthew Ahrens const userquota_node_t *ruqn = r; 14818b70546bSMatthew Ahrens 14828b70546bSMatthew Ahrens if (luqn->uqn_id < ruqn->uqn_id) 14838b70546bSMatthew Ahrens return (-1); 14848b70546bSMatthew Ahrens if (luqn->uqn_id > ruqn->uqn_id) 14858b70546bSMatthew Ahrens return (1); 14868b70546bSMatthew Ahrens return (0); 14878b70546bSMatthew Ahrens } 14888b70546bSMatthew Ahrens 14899966ca11SMatthew Ahrens static void 14908b70546bSMatthew Ahrens do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx) 14918b70546bSMatthew Ahrens { 14928b70546bSMatthew Ahrens void *cookie; 14938b70546bSMatthew Ahrens userquota_node_t *uqn; 14948b70546bSMatthew Ahrens 14958b70546bSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 14968b70546bSMatthew Ahrens 14978b70546bSMatthew Ahrens cookie = NULL; 14988b70546bSMatthew Ahrens while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas, 14998b70546bSMatthew Ahrens &cookie)) != NULL) { 150094c2d0ebSMatthew Ahrens /* 150194c2d0ebSMatthew Ahrens * os_userused_lock protects against concurrent calls to 150294c2d0ebSMatthew Ahrens * zap_increment_int(). It's needed because zap_increment_int() 150394c2d0ebSMatthew Ahrens * is not thread-safe (i.e. not atomic). 150494c2d0ebSMatthew Ahrens */ 150594c2d0ebSMatthew Ahrens mutex_enter(&os->os_userused_lock); 15068b70546bSMatthew Ahrens VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT, 15078b70546bSMatthew Ahrens uqn->uqn_id, uqn->uqn_delta, tx)); 150894c2d0ebSMatthew Ahrens mutex_exit(&os->os_userused_lock); 15098b70546bSMatthew Ahrens kmem_free(uqn, sizeof (*uqn)); 15108b70546bSMatthew Ahrens } 15118b70546bSMatthew Ahrens avl_destroy(&cache->uqc_user_deltas); 15128b70546bSMatthew Ahrens 15138b70546bSMatthew Ahrens cookie = NULL; 15148b70546bSMatthew Ahrens while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas, 15158b70546bSMatthew Ahrens &cookie)) != NULL) { 151694c2d0ebSMatthew Ahrens mutex_enter(&os->os_userused_lock); 15178b70546bSMatthew Ahrens VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT, 15188b70546bSMatthew Ahrens uqn->uqn_id, uqn->uqn_delta, tx)); 151994c2d0ebSMatthew Ahrens mutex_exit(&os->os_userused_lock); 15208b70546bSMatthew Ahrens kmem_free(uqn, sizeof (*uqn)); 15218b70546bSMatthew Ahrens } 15228b70546bSMatthew Ahrens avl_destroy(&cache->uqc_group_deltas); 15238b70546bSMatthew Ahrens } 15248b70546bSMatthew Ahrens 15258b70546bSMatthew Ahrens static void 15268b70546bSMatthew Ahrens userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta) 15278b70546bSMatthew Ahrens { 15288b70546bSMatthew Ahrens userquota_node_t search = { .uqn_id = id }; 15298b70546bSMatthew Ahrens avl_index_t idx; 15308b70546bSMatthew Ahrens 15318b70546bSMatthew Ahrens userquota_node_t *uqn = avl_find(avl, &search, &idx); 15328b70546bSMatthew Ahrens if (uqn == NULL) { 15338b70546bSMatthew Ahrens uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP); 15348b70546bSMatthew Ahrens uqn->uqn_id = id; 15358b70546bSMatthew Ahrens avl_insert(avl, uqn, idx); 15368b70546bSMatthew Ahrens } 15378b70546bSMatthew Ahrens uqn->uqn_delta += delta; 15388b70546bSMatthew Ahrens } 15398b70546bSMatthew Ahrens 15408b70546bSMatthew Ahrens static void 15418b70546bSMatthew Ahrens do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags, 15428b70546bSMatthew Ahrens uint64_t user, uint64_t group, boolean_t subtract) 15439966ca11SMatthew Ahrens { 15440a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 1545*54811da5SToomas Soome int64_t delta = DNODE_MIN_SIZE + used; 15469966ca11SMatthew Ahrens if (subtract) 15479966ca11SMatthew Ahrens delta = -delta; 15488b70546bSMatthew Ahrens 15498b70546bSMatthew Ahrens userquota_update_cache(&cache->uqc_user_deltas, user, delta); 15508b70546bSMatthew Ahrens userquota_update_cache(&cache->uqc_group_deltas, group, delta); 15519966ca11SMatthew Ahrens } 15529966ca11SMatthew Ahrens } 15539966ca11SMatthew Ahrens 155494c2d0ebSMatthew Ahrens typedef struct userquota_updates_arg { 155594c2d0ebSMatthew Ahrens objset_t *uua_os; 155694c2d0ebSMatthew Ahrens int uua_sublist_idx; 155794c2d0ebSMatthew Ahrens dmu_tx_t *uua_tx; 155894c2d0ebSMatthew Ahrens } userquota_updates_arg_t; 155994c2d0ebSMatthew Ahrens 156094c2d0ebSMatthew Ahrens static void 156194c2d0ebSMatthew Ahrens userquota_updates_task(void *arg) 156214843421SMatthew Ahrens { 156394c2d0ebSMatthew Ahrens userquota_updates_arg_t *uua = arg; 156494c2d0ebSMatthew Ahrens objset_t *os = uua->uua_os; 156594c2d0ebSMatthew Ahrens dmu_tx_t *tx = uua->uua_tx; 156614843421SMatthew Ahrens dnode_t *dn; 15678b70546bSMatthew Ahrens userquota_cache_t cache = { 0 }; 156814843421SMatthew Ahrens 156994c2d0ebSMatthew Ahrens multilist_sublist_t *list = 157094c2d0ebSMatthew Ahrens multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx); 157114843421SMatthew Ahrens 157294c2d0ebSMatthew Ahrens ASSERT(multilist_sublist_head(list) == NULL || 157394c2d0ebSMatthew Ahrens dmu_objset_userused_enabled(os)); 15748b70546bSMatthew Ahrens avl_create(&cache.uqc_user_deltas, userquota_compare, 15758b70546bSMatthew Ahrens sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); 15768b70546bSMatthew Ahrens avl_create(&cache.uqc_group_deltas, userquota_compare, 15778b70546bSMatthew Ahrens sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node)); 15788b70546bSMatthew Ahrens 157994c2d0ebSMatthew Ahrens while ((dn = multilist_sublist_head(list)) != NULL) { 15801d8ccc7bSMark Shellenbaum int flags; 158114843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 158214843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 158314843421SMatthew Ahrens dn->dn_phys->dn_flags & 158414843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED); 158514843421SMatthew Ahrens 15861d8ccc7bSMark Shellenbaum flags = dn->dn_id_flags; 15871d8ccc7bSMark Shellenbaum ASSERT(flags); 15881d8ccc7bSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 15898b70546bSMatthew Ahrens do_userquota_update(&cache, 15908b70546bSMatthew Ahrens dn->dn_oldused, dn->dn_oldflags, 15918b70546bSMatthew Ahrens dn->dn_olduid, dn->dn_oldgid, B_TRUE); 15920a586ceaSMark Shellenbaum } 15931d8ccc7bSMark Shellenbaum if (flags & DN_ID_NEW_EXIST) { 15948b70546bSMatthew Ahrens do_userquota_update(&cache, 15958b70546bSMatthew Ahrens DN_USED_BYTES(dn->dn_phys), 15960a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid, 15978b70546bSMatthew Ahrens dn->dn_newgid, B_FALSE); 15980a586ceaSMark Shellenbaum } 15990a586ceaSMark Shellenbaum 16001d8ccc7bSMark Shellenbaum mutex_enter(&dn->dn_mtx); 16010a586ceaSMark Shellenbaum dn->dn_oldused = 0; 16020a586ceaSMark Shellenbaum dn->dn_oldflags = 0; 16030a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 16040a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid; 16050a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid; 16060a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 16070a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0) 16080a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 16090a586ceaSMark Shellenbaum else 16100a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 16110a586ceaSMark Shellenbaum } 161228d97a71SMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 161314843421SMatthew Ahrens mutex_exit(&dn->dn_mtx); 161414843421SMatthew Ahrens 161594c2d0ebSMatthew Ahrens multilist_sublist_remove(list, dn); 161694c2d0ebSMatthew Ahrens dnode_rele(dn, os->os_synced_dnodes); 161714843421SMatthew Ahrens } 16188b70546bSMatthew Ahrens do_userquota_cacheflush(os, &cache, tx); 161994c2d0ebSMatthew Ahrens multilist_sublist_unlock(list); 162094c2d0ebSMatthew Ahrens kmem_free(uua, sizeof (*uua)); 162194c2d0ebSMatthew Ahrens } 162294c2d0ebSMatthew Ahrens 162394c2d0ebSMatthew Ahrens void 162494c2d0ebSMatthew Ahrens dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 162594c2d0ebSMatthew Ahrens { 162694c2d0ebSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 162794c2d0ebSMatthew Ahrens return; 162894c2d0ebSMatthew Ahrens 162994c2d0ebSMatthew Ahrens /* Allocate the user/groupused objects if necessary. */ 163094c2d0ebSMatthew Ahrens if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 163194c2d0ebSMatthew Ahrens VERIFY0(zap_create_claim(os, 163294c2d0ebSMatthew Ahrens DMU_USERUSED_OBJECT, 163394c2d0ebSMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 163494c2d0ebSMatthew Ahrens VERIFY0(zap_create_claim(os, 163594c2d0ebSMatthew Ahrens DMU_GROUPUSED_OBJECT, 163694c2d0ebSMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 163794c2d0ebSMatthew Ahrens } 163894c2d0ebSMatthew Ahrens 163994c2d0ebSMatthew Ahrens for (int i = 0; 164094c2d0ebSMatthew Ahrens i < multilist_get_num_sublists(os->os_synced_dnodes); i++) { 164194c2d0ebSMatthew Ahrens userquota_updates_arg_t *uua = 164294c2d0ebSMatthew Ahrens kmem_alloc(sizeof (*uua), KM_SLEEP); 164394c2d0ebSMatthew Ahrens uua->uua_os = os; 164494c2d0ebSMatthew Ahrens uua->uua_sublist_idx = i; 164594c2d0ebSMatthew Ahrens uua->uua_tx = tx; 164694c2d0ebSMatthew Ahrens /* note: caller does taskq_wait() */ 164794c2d0ebSMatthew Ahrens (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq, 164894c2d0ebSMatthew Ahrens userquota_updates_task, uua, 0); 164994c2d0ebSMatthew Ahrens /* callback frees uua */ 165094c2d0ebSMatthew Ahrens } 165114843421SMatthew Ahrens } 165214843421SMatthew Ahrens 165306e0070dSMark Shellenbaum /* 165406e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from 165506e0070dSMark Shellenbaum * 165606e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't 165706e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed 165806e0070dSMark Shellenbaum * the uid/gid aren't changing. 165906e0070dSMark Shellenbaum */ 166006e0070dSMark Shellenbaum static void * 166106e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 166206e0070dSMark Shellenbaum { 166306e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp; 166406e0070dSMark Shellenbaum void *data; 166506e0070dSMark Shellenbaum 166606e0070dSMark Shellenbaum if (db->db_dirtycnt == 0) 166706e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */ 166806e0070dSMark Shellenbaum 166906e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 167006e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg) 167106e0070dSMark Shellenbaum break; 167206e0070dSMark Shellenbaum 1673744947dcSTom Erickson if (dr == NULL) { 167406e0070dSMark Shellenbaum data = NULL; 1675744947dcSTom Erickson } else { 1676744947dcSTom Erickson dnode_t *dn; 1677744947dcSTom Erickson 1678744947dcSTom Erickson DB_DNODE_ENTER(dr->dr_dbuf); 1679744947dcSTom Erickson dn = DB_DNODE(dr->dr_dbuf); 1680744947dcSTom Erickson 1681744947dcSTom Erickson if (dn->dn_bonuslen == 0 && 1682744947dcSTom Erickson dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1683744947dcSTom Erickson data = dr->dt.dl.dr_data->b_data; 1684744947dcSTom Erickson else 1685744947dcSTom Erickson data = dr->dt.dl.dr_data; 1686744947dcSTom Erickson 1687744947dcSTom Erickson DB_DNODE_EXIT(dr->dr_dbuf); 1688744947dcSTom Erickson } 1689744947dcSTom Erickson 169006e0070dSMark Shellenbaum return (data); 169106e0070dSMark Shellenbaum } 169206e0070dSMark Shellenbaum 16930a586ceaSMark Shellenbaum void 169406e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 16950a586ceaSMark Shellenbaum { 16960a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset; 16970a586ceaSMark Shellenbaum void *data = NULL; 169806e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL; 1699d5285caeSGeorge Wilson uint64_t *user = NULL; 1700d5285caeSGeorge Wilson uint64_t *group = NULL; 17010a586ceaSMark Shellenbaum int flags = dn->dn_id_flags; 17020a586ceaSMark Shellenbaum int error; 170306e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE; 17040a586ceaSMark Shellenbaum 17050a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset)) 17060a586ceaSMark Shellenbaum return; 17070a586ceaSMark Shellenbaum 17080a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 17090a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL))) 17100a586ceaSMark Shellenbaum return; 17110a586ceaSMark Shellenbaum 17120a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0) 17130a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 171406e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) { 171506e0070dSMark Shellenbaum if (dn->dn_bonus) { 171606e0070dSMark Shellenbaum db = dn->dn_bonus; 171706e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 171806e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx); 171906e0070dSMark Shellenbaum } else { 172006e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys); 172106e0070dSMark Shellenbaum } 172206e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 17230a586ceaSMark Shellenbaum int rf = 0; 17240a586ceaSMark Shellenbaum 17250a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 17260a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT; 17271d8ccc7bSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn, 17281d8ccc7bSMark Shellenbaum rf | DB_RF_MUST_SUCCEED, 172906e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db); 17300a586ceaSMark Shellenbaum ASSERT(error == 0); 173106e0070dSMark Shellenbaum mutex_enter(&db->db_mtx); 173206e0070dSMark Shellenbaum data = (before) ? db->db.db_data : 173306e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx); 173406e0070dSMark Shellenbaum have_spill = B_TRUE; 17350a586ceaSMark Shellenbaum } else { 17360a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 17370a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 17380a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 17390a586ceaSMark Shellenbaum return; 17400a586ceaSMark Shellenbaum } 17410a586ceaSMark Shellenbaum 17420a586ceaSMark Shellenbaum if (before) { 174306e0070dSMark Shellenbaum ASSERT(data); 17440a586ceaSMark Shellenbaum user = &dn->dn_olduid; 17450a586ceaSMark Shellenbaum group = &dn->dn_oldgid; 174606e0070dSMark Shellenbaum } else if (data) { 17470a586ceaSMark Shellenbaum user = &dn->dn_newuid; 17480a586ceaSMark Shellenbaum group = &dn->dn_newgid; 17490a586ceaSMark Shellenbaum } 17500a586ceaSMark Shellenbaum 175106e0070dSMark Shellenbaum /* 175206e0070dSMark Shellenbaum * Must always call the callback in case the object 175306e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track 175406e0070dSMark Shellenbaum */ 17550a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 17560a586ceaSMark Shellenbaum user, group); 17570a586ceaSMark Shellenbaum 175806e0070dSMark Shellenbaum /* 175906e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine 176006e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST. 176106e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid. 176206e0070dSMark Shellenbaum * If we don't know what the old values are then just assign 176306e0070dSMark Shellenbaum * them to 0, since that is a new file being created. 176406e0070dSMark Shellenbaum */ 176506e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) { 176606e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) { 176706e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid; 176806e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid; 176906e0070dSMark Shellenbaum } else { 177006e0070dSMark Shellenbaum dn->dn_newuid = 0; 177106e0070dSMark Shellenbaum dn->dn_newgid = 0; 177206e0070dSMark Shellenbaum } 177306e0070dSMark Shellenbaum error = 0; 177406e0070dSMark Shellenbaum } 177506e0070dSMark Shellenbaum 177606e0070dSMark Shellenbaum if (db) 177706e0070dSMark Shellenbaum mutex_exit(&db->db_mtx); 177806e0070dSMark Shellenbaum 17790a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx); 17800a586ceaSMark Shellenbaum if (error == 0 && before) 17810a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST; 17820a586ceaSMark Shellenbaum if (error == 0 && !before) 17830a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST; 17840a586ceaSMark Shellenbaum 178506e0070dSMark Shellenbaum if (have_spill) { 17860a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL; 17870a586ceaSMark Shellenbaum } else { 17880a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS; 17890a586ceaSMark Shellenbaum } 17900a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx); 179106e0070dSMark Shellenbaum if (have_spill) 179206e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG); 17930a586ceaSMark Shellenbaum } 17940a586ceaSMark Shellenbaum 179514843421SMatthew Ahrens boolean_t 179614843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os) 179714843421SMatthew Ahrens { 1798503ad85cSMatthew Ahrens return (os->os_phys->os_flags & 179914843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE); 180014843421SMatthew Ahrens } 180114843421SMatthew Ahrens 180214843421SMatthew Ahrens int 180314843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os) 180414843421SMatthew Ahrens { 180514843421SMatthew Ahrens uint64_t obj; 180614843421SMatthew Ahrens int err = 0; 180714843421SMatthew Ahrens 180814843421SMatthew Ahrens if (dmu_objset_userspace_present(os)) 180914843421SMatthew Ahrens return (0); 1810503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os)) 1811be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 181214843421SMatthew Ahrens if (dmu_objset_is_snapshot(os)) 1813be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 181414843421SMatthew Ahrens 181514843421SMatthew Ahrens /* 181614843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be 181714843421SMatthew Ahrens * synced out and now accounted. If this is called 181814843421SMatthew Ahrens * concurrently, or if we already did some work before crashing, 181914843421SMatthew Ahrens * that's fine, since we track each object's accounted state 182014843421SMatthew Ahrens * independently. 182114843421SMatthew Ahrens */ 182214843421SMatthew Ahrens 182314843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 182468857716SLin Ling dmu_tx_t *tx; 182514843421SMatthew Ahrens dmu_buf_t *db; 182614843421SMatthew Ahrens int objerr; 182714843421SMatthew Ahrens 182814843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) 1829be6fd75aSMatthew Ahrens return (SET_ERROR(EINTR)); 183014843421SMatthew Ahrens 183114843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db); 18323b2aab18SMatthew Ahrens if (objerr != 0) 183314843421SMatthew Ahrens continue; 183468857716SLin Ling tx = dmu_tx_create(os); 183514843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj); 183614843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT); 18373b2aab18SMatthew Ahrens if (objerr != 0) { 183814843421SMatthew Ahrens dmu_tx_abort(tx); 183914843421SMatthew Ahrens continue; 184014843421SMatthew Ahrens } 184114843421SMatthew Ahrens dmu_buf_will_dirty(db, tx); 184214843421SMatthew Ahrens dmu_buf_rele(db, FTAG); 184314843421SMatthew Ahrens dmu_tx_commit(tx); 184414843421SMatthew Ahrens } 184514843421SMatthew Ahrens 1846503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 184714843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 184814843421SMatthew Ahrens return (0); 184914843421SMatthew Ahrens } 185014843421SMatthew Ahrens 1851fa9e4066Sahrens void 1852a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1853a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1854fa9e4066Sahrens { 1855503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1856a2eea2e1Sahrens usedobjsp, availobjsp); 1857a2eea2e1Sahrens } 1858a2eea2e1Sahrens 1859a2eea2e1Sahrens uint64_t 1860a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os) 1861a2eea2e1Sahrens { 1862503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1863a2eea2e1Sahrens } 1864a2eea2e1Sahrens 1865a2eea2e1Sahrens void 1866a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1867a2eea2e1Sahrens { 1868503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type; 1869503ad85cSMatthew Ahrens if (os->os_dsl_dataset) 1870503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1871a2eea2e1Sahrens } 1872a2eea2e1Sahrens 1873a2eea2e1Sahrens void 1874a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv) 1875a2eea2e1Sahrens { 1876503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset || 1877503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META); 1878a2eea2e1Sahrens 1879503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1880503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv); 1881a2eea2e1Sahrens 1882a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1883503ad85cSMatthew Ahrens os->os_phys->os_type); 188414843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 188514843421SMatthew Ahrens dmu_objset_userspace_present(os)); 1886fa9e4066Sahrens } 1887fa9e4066Sahrens 1888fa9e4066Sahrens int 1889fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os) 1890fa9e4066Sahrens { 1891503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL) 1892bc9014e6SJustin Gibbs return (os->os_dsl_dataset->ds_is_snapshot); 1893fa9e4066Sahrens else 1894fa9e4066Sahrens return (B_FALSE); 1895fa9e4066Sahrens } 1896fa9e4066Sahrens 1897ab04eb8eStimh int 1898ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1899ab04eb8eStimh boolean_t *conflict) 1900ab04eb8eStimh { 1901503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1902ab04eb8eStimh uint64_t ignored; 1903ab04eb8eStimh 1904c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 1905be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 1906ab04eb8eStimh 1907ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1908c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored, 19091c17160aSKevin Crowe MT_NORMALIZE, real, maxlen, conflict)); 1910ab04eb8eStimh } 1911ab04eb8eStimh 1912fa9e4066Sahrens int 1913fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1914b38f0970Sck uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1915fa9e4066Sahrens { 1916503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 1917fa9e4066Sahrens zap_cursor_t cursor; 1918fa9e4066Sahrens zap_attribute_t attr; 1919fa9e4066Sahrens 19203b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dmu_objset_pool(os))); 19213b2aab18SMatthew Ahrens 1922c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 1923be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 1924fa9e4066Sahrens 1925fa9e4066Sahrens zap_cursor_init_serialized(&cursor, 1926fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset, 1927c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp); 1928fa9e4066Sahrens 192987e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 193087e5029aSahrens zap_cursor_fini(&cursor); 1931be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 193287e5029aSahrens } 193387e5029aSahrens 193487e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 193587e5029aSahrens zap_cursor_fini(&cursor); 1936be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 193787e5029aSahrens } 193887e5029aSahrens 193987e5029aSahrens (void) strcpy(name, attr.za_name); 194087e5029aSahrens if (idp) 194187e5029aSahrens *idp = attr.za_first_integer; 1942b38f0970Sck if (case_conflict) 1943b38f0970Sck *case_conflict = attr.za_normalization_conflict; 194487e5029aSahrens zap_cursor_advance(&cursor); 194587e5029aSahrens *offp = zap_cursor_serialize(&cursor); 194687e5029aSahrens zap_cursor_fini(&cursor); 194787e5029aSahrens 194887e5029aSahrens return (0); 194987e5029aSahrens } 195087e5029aSahrens 195187e5029aSahrens int 195287e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name, 195387e5029aSahrens uint64_t *idp, uint64_t *offp) 195487e5029aSahrens { 1955503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 195687e5029aSahrens zap_cursor_t cursor; 195787e5029aSahrens zap_attribute_t attr; 195887e5029aSahrens 195987e5029aSahrens /* there is no next dir on a snapshot! */ 1960503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object != 1961c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj) 1962be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 1963fa9e4066Sahrens 196487e5029aSahrens zap_cursor_init_serialized(&cursor, 196587e5029aSahrens dd->dd_pool->dp_meta_objset, 1966c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp); 196787e5029aSahrens 196887e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) { 196987e5029aSahrens zap_cursor_fini(&cursor); 1970be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 197187e5029aSahrens } 197287e5029aSahrens 197387e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) { 197487e5029aSahrens zap_cursor_fini(&cursor); 1975be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 197687e5029aSahrens } 1977fa9e4066Sahrens 1978fa9e4066Sahrens (void) strcpy(name, attr.za_name); 197987e5029aSahrens if (idp) 198087e5029aSahrens *idp = attr.za_first_integer; 1981fa9e4066Sahrens zap_cursor_advance(&cursor); 1982fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor); 198387e5029aSahrens zap_cursor_fini(&cursor); 1984fa9e4066Sahrens 1985fa9e4066Sahrens return (0); 1986fa9e4066Sahrens } 1987fa9e4066Sahrens 198812380e1eSArne Jansen typedef struct dmu_objset_find_ctx { 198912380e1eSArne Jansen taskq_t *dc_tq; 199012380e1eSArne Jansen dsl_pool_t *dc_dp; 199112380e1eSArne Jansen uint64_t dc_ddobj; 19927588687eSMatthew Ahrens char *dc_ddname; /* last component of ddobj's name */ 199312380e1eSArne Jansen int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *); 199412380e1eSArne Jansen void *dc_arg; 199512380e1eSArne Jansen int dc_flags; 199612380e1eSArne Jansen kmutex_t *dc_error_lock; 199712380e1eSArne Jansen int *dc_error; 199812380e1eSArne Jansen } dmu_objset_find_ctx_t; 199912380e1eSArne Jansen 200012380e1eSArne Jansen static void 200112380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp) 2002088f3894Sahrens { 200312380e1eSArne Jansen dsl_pool_t *dp = dcp->dc_dp; 20043b2aab18SMatthew Ahrens dsl_dir_t *dd; 20053b2aab18SMatthew Ahrens dsl_dataset_t *ds; 20063b2aab18SMatthew Ahrens zap_cursor_t zc; 20073b2aab18SMatthew Ahrens zap_attribute_t *attr; 20083b2aab18SMatthew Ahrens uint64_t thisobj; 200912380e1eSArne Jansen int err = 0; 20103b2aab18SMatthew Ahrens 201112380e1eSArne Jansen /* don't process if there already was an error */ 201212380e1eSArne Jansen if (*dcp->dc_error != 0) 201312380e1eSArne Jansen goto out; 20143b2aab18SMatthew Ahrens 20157588687eSMatthew Ahrens /* 20167588687eSMatthew Ahrens * Note: passing the name (dc_ddname) here is optional, but it 20177588687eSMatthew Ahrens * improves performance because we don't need to call 20187588687eSMatthew Ahrens * zap_value_search() to determine the name. 20197588687eSMatthew Ahrens */ 20207588687eSMatthew Ahrens err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd); 20213b2aab18SMatthew Ahrens if (err != 0) 202212380e1eSArne Jansen goto out; 20233b2aab18SMatthew Ahrens 20243b2aab18SMatthew Ahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 20253b2aab18SMatthew Ahrens if (dd->dd_myname[0] == '$') { 20263b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 202712380e1eSArne Jansen goto out; 20283b2aab18SMatthew Ahrens } 20293b2aab18SMatthew Ahrens 2030c1379625SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 20313b2aab18SMatthew Ahrens attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 20323b2aab18SMatthew Ahrens 20333b2aab18SMatthew Ahrens /* 20343b2aab18SMatthew Ahrens * Iterate over all children. 20353b2aab18SMatthew Ahrens */ 203612380e1eSArne Jansen if (dcp->dc_flags & DS_FIND_CHILDREN) { 20373b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2038c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj); 20393b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0; 20403b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) { 20413b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 20423b2aab18SMatthew Ahrens sizeof (uint64_t)); 20433b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 20443b2aab18SMatthew Ahrens 20457588687eSMatthew Ahrens dmu_objset_find_ctx_t *child_dcp = 20467588687eSMatthew Ahrens kmem_alloc(sizeof (*child_dcp), KM_SLEEP); 204712380e1eSArne Jansen *child_dcp = *dcp; 204812380e1eSArne Jansen child_dcp->dc_ddobj = attr->za_first_integer; 20497588687eSMatthew Ahrens child_dcp->dc_ddname = spa_strdup(attr->za_name); 205012380e1eSArne Jansen if (dcp->dc_tq != NULL) 205112380e1eSArne Jansen (void) taskq_dispatch(dcp->dc_tq, 205212380e1eSArne Jansen dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP); 205312380e1eSArne Jansen else 205412380e1eSArne Jansen dmu_objset_find_dp_impl(child_dcp); 20553b2aab18SMatthew Ahrens } 20563b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 20573b2aab18SMatthew Ahrens } 20583b2aab18SMatthew Ahrens 20593b2aab18SMatthew Ahrens /* 20603b2aab18SMatthew Ahrens * Iterate over all snapshots. 20613b2aab18SMatthew Ahrens */ 206212380e1eSArne Jansen if (dcp->dc_flags & DS_FIND_SNAPSHOTS) { 20633b2aab18SMatthew Ahrens dsl_dataset_t *ds; 20643b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 20653b2aab18SMatthew Ahrens 20663b2aab18SMatthew Ahrens if (err == 0) { 2067c1379625SJustin T. Gibbs uint64_t snapobj; 2068c1379625SJustin T. Gibbs 2069c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 20703b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 20713b2aab18SMatthew Ahrens 20723b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 20733b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0; 20743b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) { 20753b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 20763b2aab18SMatthew Ahrens sizeof (uint64_t)); 20773b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 20783b2aab18SMatthew Ahrens 20793b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 20803b2aab18SMatthew Ahrens attr->za_first_integer, FTAG, &ds); 20813b2aab18SMatthew Ahrens if (err != 0) 20823b2aab18SMatthew Ahrens break; 208312380e1eSArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg); 20843b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 20853b2aab18SMatthew Ahrens if (err != 0) 20863b2aab18SMatthew Ahrens break; 20873b2aab18SMatthew Ahrens } 20883b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 20893b2aab18SMatthew Ahrens } 20903b2aab18SMatthew Ahrens } 20913b2aab18SMatthew Ahrens 20923b2aab18SMatthew Ahrens kmem_free(attr, sizeof (zap_attribute_t)); 20933b2aab18SMatthew Ahrens 20947588687eSMatthew Ahrens if (err != 0) { 20957588687eSMatthew Ahrens dsl_dir_rele(dd, FTAG); 209612380e1eSArne Jansen goto out; 20977588687eSMatthew Ahrens } 20983b2aab18SMatthew Ahrens 20993b2aab18SMatthew Ahrens /* 21003b2aab18SMatthew Ahrens * Apply to self. 21013b2aab18SMatthew Ahrens */ 21023b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 21037588687eSMatthew Ahrens 21047588687eSMatthew Ahrens /* 21057588687eSMatthew Ahrens * Note: we hold the dir while calling dsl_dataset_hold_obj() so 21067588687eSMatthew Ahrens * that the dir will remain cached, and we won't have to re-instantiate 21077588687eSMatthew Ahrens * it (which could be expensive due to finding its name via 21087588687eSMatthew Ahrens * zap_value_search()). 21097588687eSMatthew Ahrens */ 21107588687eSMatthew Ahrens dsl_dir_rele(dd, FTAG); 21113b2aab18SMatthew Ahrens if (err != 0) 211212380e1eSArne Jansen goto out; 211312380e1eSArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg); 21143b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 211512380e1eSArne Jansen 211612380e1eSArne Jansen out: 211712380e1eSArne Jansen if (err != 0) { 211812380e1eSArne Jansen mutex_enter(dcp->dc_error_lock); 211912380e1eSArne Jansen /* only keep first error */ 212012380e1eSArne Jansen if (*dcp->dc_error == 0) 212112380e1eSArne Jansen *dcp->dc_error = err; 212212380e1eSArne Jansen mutex_exit(dcp->dc_error_lock); 212312380e1eSArne Jansen } 212412380e1eSArne Jansen 21257588687eSMatthew Ahrens if (dcp->dc_ddname != NULL) 21267588687eSMatthew Ahrens spa_strfree(dcp->dc_ddname); 212712380e1eSArne Jansen kmem_free(dcp, sizeof (*dcp)); 212812380e1eSArne Jansen } 212912380e1eSArne Jansen 213012380e1eSArne Jansen static void 213112380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg) 213212380e1eSArne Jansen { 213312380e1eSArne Jansen dmu_objset_find_ctx_t *dcp = arg; 213412380e1eSArne Jansen dsl_pool_t *dp = dcp->dc_dp; 213512380e1eSArne Jansen 21361d3f896fSArne Jansen /* 21371d3f896fSArne Jansen * We need to get a pool_config_lock here, as there are several 21381d3f896fSArne Jansen * asssert(pool_config_held) down the stack. Getting a lock via 21391d3f896fSArne Jansen * dsl_pool_config_enter is risky, as it might be stalled by a 21401d3f896fSArne Jansen * pending writer. This would deadlock, as the write lock can 21411d3f896fSArne Jansen * only be granted when our parent thread gives up the lock. 21421d3f896fSArne Jansen * The _prio interface gives us priority over a pending writer. 21431d3f896fSArne Jansen */ 21441d3f896fSArne Jansen dsl_pool_config_enter_prio(dp, FTAG); 214512380e1eSArne Jansen 214612380e1eSArne Jansen dmu_objset_find_dp_impl(dcp); 214712380e1eSArne Jansen 214812380e1eSArne Jansen dsl_pool_config_exit(dp, FTAG); 214912380e1eSArne Jansen } 215012380e1eSArne Jansen 215112380e1eSArne Jansen /* 215212380e1eSArne Jansen * Find objsets under and including ddobj, call func(ds) on each. 215312380e1eSArne Jansen * The order for the enumeration is completely undefined. 215412380e1eSArne Jansen * func is called with dsl_pool_config held. 215512380e1eSArne Jansen */ 215612380e1eSArne Jansen int 215712380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj, 215812380e1eSArne Jansen int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags) 215912380e1eSArne Jansen { 216012380e1eSArne Jansen int error = 0; 216112380e1eSArne Jansen taskq_t *tq = NULL; 216212380e1eSArne Jansen int ntasks; 216312380e1eSArne Jansen dmu_objset_find_ctx_t *dcp; 216412380e1eSArne Jansen kmutex_t err_lock; 216512380e1eSArne Jansen 216612380e1eSArne Jansen mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL); 216712380e1eSArne Jansen dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP); 216812380e1eSArne Jansen dcp->dc_tq = NULL; 216912380e1eSArne Jansen dcp->dc_dp = dp; 217012380e1eSArne Jansen dcp->dc_ddobj = ddobj; 21717588687eSMatthew Ahrens dcp->dc_ddname = NULL; 217212380e1eSArne Jansen dcp->dc_func = func; 217312380e1eSArne Jansen dcp->dc_arg = arg; 217412380e1eSArne Jansen dcp->dc_flags = flags; 217512380e1eSArne Jansen dcp->dc_error_lock = &err_lock; 217612380e1eSArne Jansen dcp->dc_error = &error; 217712380e1eSArne Jansen 217812380e1eSArne Jansen if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) { 217912380e1eSArne Jansen /* 218012380e1eSArne Jansen * In case a write lock is held we can't make use of 218112380e1eSArne Jansen * parallelism, as down the stack of the worker threads 218212380e1eSArne Jansen * the lock is asserted via dsl_pool_config_held. 218312380e1eSArne Jansen * In case of a read lock this is solved by getting a read 218412380e1eSArne Jansen * lock in each worker thread, which isn't possible in case 218512380e1eSArne Jansen * of a writer lock. So we fall back to the synchronous path 218612380e1eSArne Jansen * here. 218712380e1eSArne Jansen * In the future it might be possible to get some magic into 218812380e1eSArne Jansen * dsl_pool_config_held in a way that it returns true for 218912380e1eSArne Jansen * the worker threads so that a single lock held from this 219012380e1eSArne Jansen * thread suffices. For now, stay single threaded. 219112380e1eSArne Jansen */ 219212380e1eSArne Jansen dmu_objset_find_dp_impl(dcp); 21932bad2258SSteven Hartland mutex_destroy(&err_lock); 219412380e1eSArne Jansen 219512380e1eSArne Jansen return (error); 219612380e1eSArne Jansen } 219712380e1eSArne Jansen 219812380e1eSArne Jansen ntasks = dmu_find_threads; 219912380e1eSArne Jansen if (ntasks == 0) 220012380e1eSArne Jansen ntasks = vdev_count_leaves(dp->dp_spa) * 4; 220112380e1eSArne Jansen tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks, 220212380e1eSArne Jansen INT_MAX, 0); 220312380e1eSArne Jansen if (tq == NULL) { 220412380e1eSArne Jansen kmem_free(dcp, sizeof (*dcp)); 22052bad2258SSteven Hartland mutex_destroy(&err_lock); 22062bad2258SSteven Hartland 220712380e1eSArne Jansen return (SET_ERROR(ENOMEM)); 220812380e1eSArne Jansen } 220912380e1eSArne Jansen dcp->dc_tq = tq; 221012380e1eSArne Jansen 221112380e1eSArne Jansen /* dcp will be freed by task */ 221212380e1eSArne Jansen (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP); 221312380e1eSArne Jansen 221412380e1eSArne Jansen /* 221512380e1eSArne Jansen * PORTING: this code relies on the property of taskq_wait to wait 221612380e1eSArne Jansen * until no more tasks are queued and no more tasks are active. As 221712380e1eSArne Jansen * we always queue new tasks from within other tasks, task_wait 221812380e1eSArne Jansen * reliably waits for the full recursion to finish, even though we 221912380e1eSArne Jansen * enqueue new tasks after taskq_wait has been called. 222012380e1eSArne Jansen * On platforms other than illumos, taskq_wait may not have this 222112380e1eSArne Jansen * property. 222212380e1eSArne Jansen */ 222312380e1eSArne Jansen taskq_wait(tq); 222412380e1eSArne Jansen taskq_destroy(tq); 222512380e1eSArne Jansen mutex_destroy(&err_lock); 222612380e1eSArne Jansen 222712380e1eSArne Jansen return (error); 2228088f3894Sahrens } 2229088f3894Sahrens 2230088f3894Sahrens /* 22313b2aab18SMatthew Ahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'. 22323b2aab18SMatthew Ahrens * The dp_config_rwlock must not be held when this is called, and it 22333b2aab18SMatthew Ahrens * will not be held when the callback is called. 22343b2aab18SMatthew Ahrens * Therefore this function should only be used when the pool is not changing 22353b2aab18SMatthew Ahrens * (e.g. in syncing context), or the callback can deal with the possible races. 2236088f3894Sahrens */ 22373b2aab18SMatthew Ahrens static int 22383b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name, 22393b2aab18SMatthew Ahrens int func(const char *, void *), void *arg, int flags) 2240fa9e4066Sahrens { 2241fa9e4066Sahrens dsl_dir_t *dd; 22423b2aab18SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 2243088f3894Sahrens dsl_dataset_t *ds; 2244fa9e4066Sahrens zap_cursor_t zc; 2245b7661cccSmmusante zap_attribute_t *attr; 2246fa9e4066Sahrens char *child; 2247088f3894Sahrens uint64_t thisobj; 2248088f3894Sahrens int err; 2249fa9e4066Sahrens 22503b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 22513b2aab18SMatthew Ahrens 22523b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, NULL); 22533b2aab18SMatthew Ahrens if (err != 0) { 22543b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 22551d452cf5Sahrens return (err); 22563b2aab18SMatthew Ahrens } 2257fa9e4066Sahrens 2258088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 2259088f3894Sahrens if (dd->dd_myname[0] == '$') { 22603b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 22613b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 2262088f3894Sahrens return (0); 2263088f3894Sahrens } 2264088f3894Sahrens 2265c1379625SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 2266b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 2267fa9e4066Sahrens 2268fa9e4066Sahrens /* 2269fa9e4066Sahrens * Iterate over all children. 2270fa9e4066Sahrens */ 22710b69c2f0Sahrens if (flags & DS_FIND_CHILDREN) { 2272088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2273c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj); 2274b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0; 22750b69c2f0Sahrens (void) zap_cursor_advance(&zc)) { 22763b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 22773b2aab18SMatthew Ahrens sizeof (uint64_t)); 22783b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 2279fa9e4066Sahrens 2280486ae710SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name); 22813b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 22823b2aab18SMatthew Ahrens err = dmu_objset_find_impl(spa, child, 22833b2aab18SMatthew Ahrens func, arg, flags); 22843b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 2285486ae710SMatthew Ahrens strfree(child); 22863b2aab18SMatthew Ahrens if (err != 0) 22870b69c2f0Sahrens break; 22880b69c2f0Sahrens } 22890b69c2f0Sahrens zap_cursor_fini(&zc); 22901d452cf5Sahrens 22913b2aab18SMatthew Ahrens if (err != 0) { 22923b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 22933b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 2294b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 22950b69c2f0Sahrens return (err); 22960b69c2f0Sahrens } 2297fa9e4066Sahrens } 2298fa9e4066Sahrens 2299fa9e4066Sahrens /* 2300fa9e4066Sahrens * Iterate over all snapshots. 2301fa9e4066Sahrens */ 2302088f3894Sahrens if (flags & DS_FIND_SNAPSHOTS) { 2303088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 2304088f3894Sahrens 2305088f3894Sahrens if (err == 0) { 2306c1379625SJustin T. Gibbs uint64_t snapobj; 2307c1379625SJustin T. Gibbs 2308c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 2309088f3894Sahrens dsl_dataset_rele(ds, FTAG); 2310088f3894Sahrens 2311088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 2312088f3894Sahrens zap_cursor_retrieve(&zc, attr) == 0; 2313088f3894Sahrens (void) zap_cursor_advance(&zc)) { 23143b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==, 2315088f3894Sahrens sizeof (uint64_t)); 23163b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1); 2317088f3894Sahrens 2318486ae710SMatthew Ahrens child = kmem_asprintf("%s@%s", 2319486ae710SMatthew Ahrens name, attr->za_name); 23203b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 23213b2aab18SMatthew Ahrens err = func(child, arg); 23223b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 2323486ae710SMatthew Ahrens strfree(child); 23243b2aab18SMatthew Ahrens if (err != 0) 2325088f3894Sahrens break; 2326088f3894Sahrens } 2327088f3894Sahrens zap_cursor_fini(&zc); 2328fa9e4066Sahrens } 2329fa9e4066Sahrens } 2330fa9e4066Sahrens 23313b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 2332b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t)); 23333b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 2334fa9e4066Sahrens 23353b2aab18SMatthew Ahrens if (err != 0) 23361d452cf5Sahrens return (err); 23371d452cf5Sahrens 23383b2aab18SMatthew Ahrens /* Apply to self. */ 23393b2aab18SMatthew Ahrens return (func(name, arg)); 2340fa9e4066Sahrens } 2341f18faf3fSek 23423b2aab18SMatthew Ahrens /* 23433b2aab18SMatthew Ahrens * See comment above dmu_objset_find_impl(). 23443b2aab18SMatthew Ahrens */ 23457f73c863SRich Morris int 23463b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg, 23473b2aab18SMatthew Ahrens int flags) 23487f73c863SRich Morris { 23493b2aab18SMatthew Ahrens spa_t *spa; 23503b2aab18SMatthew Ahrens int error; 23517f73c863SRich Morris 23523b2aab18SMatthew Ahrens error = spa_open(name, &spa, FTAG); 23533b2aab18SMatthew Ahrens if (error != 0) 23543b2aab18SMatthew Ahrens return (error); 23553b2aab18SMatthew Ahrens error = dmu_objset_find_impl(spa, name, func, arg, flags); 23563b2aab18SMatthew Ahrens spa_close(spa, FTAG); 23573b2aab18SMatthew Ahrens return (error); 23587f73c863SRich Morris } 23597f73c863SRich Morris 2360f18faf3fSek void 2361f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr) 2362f18faf3fSek { 2363503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 2364503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr; 2365f18faf3fSek } 2366f18faf3fSek 2367f18faf3fSek void * 2368f18faf3fSek dmu_objset_get_user(objset_t *os) 2369f18faf3fSek { 2370503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 2371503ad85cSMatthew Ahrens return (os->os_user_ptr); 2372f18faf3fSek } 23733b2aab18SMatthew Ahrens 23743b2aab18SMatthew Ahrens /* 23753b2aab18SMatthew Ahrens * Determine name of filesystem, given name of snapshot. 23769adfa60dSMatthew Ahrens * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes 23773b2aab18SMatthew Ahrens */ 23783b2aab18SMatthew Ahrens int 23793b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf) 23803b2aab18SMatthew Ahrens { 23813b2aab18SMatthew Ahrens char *atp = strchr(snapname, '@'); 23823b2aab18SMatthew Ahrens if (atp == NULL) 2383be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 23849adfa60dSMatthew Ahrens if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN) 2385be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 23863b2aab18SMatthew Ahrens (void) strlcpy(buf, snapname, atp - snapname + 1); 23873b2aab18SMatthew Ahrens return (0); 23883b2aab18SMatthew Ahrens } 238961e255ceSMatthew Ahrens 239061e255ceSMatthew Ahrens /* 239161e255ceSMatthew Ahrens * Call when we think we're going to write/free space in open context to track 239261e255ceSMatthew Ahrens * the amount of dirty data in the open txg, which is also the amount 239361e255ceSMatthew Ahrens * of memory that can not be evicted until this txg syncs. 239461e255ceSMatthew Ahrens */ 239561e255ceSMatthew Ahrens void 239661e255ceSMatthew Ahrens dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx) 239761e255ceSMatthew Ahrens { 239861e255ceSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset; 239961e255ceSMatthew Ahrens int64_t aspace = spa_get_worst_case_asize(os->os_spa, space); 240061e255ceSMatthew Ahrens 240161e255ceSMatthew Ahrens if (ds != NULL) { 240261e255ceSMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, aspace, tx); 240361e255ceSMatthew Ahrens dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx); 240461e255ceSMatthew Ahrens } 240561e255ceSMatthew Ahrens } 2406