Home | History | Annotate | Line # | Download | only in zfs
      1      1.1  haad /*
      2      1.1  haad  * CDDL HEADER START
      3      1.1  haad  *
      4      1.1  haad  * The contents of this file are subject to the terms of the
      5      1.1  haad  * Common Development and Distribution License (the "License").
      6      1.1  haad  * You may not use this file except in compliance with the License.
      7      1.1  haad  *
      8      1.1  haad  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9      1.1  haad  * or http://www.opensolaris.org/os/licensing.
     10      1.1  haad  * See the License for the specific language governing permissions
     11      1.1  haad  * and limitations under the License.
     12      1.1  haad  *
     13      1.1  haad  * When distributing Covered Code, include this CDDL HEADER in each
     14      1.1  haad  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15      1.1  haad  * If applicable, add the following below this CDDL HEADER, with the
     16      1.1  haad  * fields enclosed by brackets "[]" replaced with your own identifying
     17      1.1  haad  * information: Portions Copyright [yyyy] [name of copyright owner]
     18      1.1  haad  *
     19      1.1  haad  * CDDL HEADER END
     20      1.1  haad  */
     21      1.1  haad /*
     22  1.1.1.3   chs  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     23  1.1.1.3   chs  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
     24  1.1.1.3   chs  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
     25  1.1.1.3   chs  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
     26  1.1.1.3   chs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
     27  1.1.1.3   chs  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
     28  1.1.1.3   chs  * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
     29  1.1.1.3   chs  * Copyright (c) 2014 Integros [integros.com]
     30      1.1  haad  */
     31      1.1  haad 
     32  1.1.1.3   chs /* Portions Copyright 2010 Robert Milkowski */
     33  1.1.1.3   chs 
     34      1.1  haad #include <sys/cred.h>
     35      1.1  haad #include <sys/zfs_context.h>
     36      1.1  haad #include <sys/dmu_objset.h>
     37      1.1  haad #include <sys/dsl_dir.h>
     38      1.1  haad #include <sys/dsl_dataset.h>
     39      1.1  haad #include <sys/dsl_prop.h>
     40      1.1  haad #include <sys/dsl_pool.h>
     41      1.1  haad #include <sys/dsl_synctask.h>
     42      1.1  haad #include <sys/dsl_deleg.h>
     43      1.1  haad #include <sys/dnode.h>
     44      1.1  haad #include <sys/dbuf.h>
     45      1.1  haad #include <sys/zvol.h>
     46      1.1  haad #include <sys/dmu_tx.h>
     47      1.1  haad #include <sys/zap.h>
     48      1.1  haad #include <sys/zil.h>
     49      1.1  haad #include <sys/dmu_impl.h>
     50      1.1  haad #include <sys/zfs_ioctl.h>
     51  1.1.1.3   chs #include <sys/sa.h>
     52  1.1.1.3   chs #include <sys/zfs_onexit.h>
     53  1.1.1.3   chs #include <sys/dsl_destroy.h>
     54  1.1.1.3   chs #include <sys/vdev.h>
     55  1.1.1.3   chs 
     56  1.1.1.3   chs /*
     57  1.1.1.3   chs  * Needed to close a window in dnode_move() that allows the objset to be freed
     58  1.1.1.3   chs  * before it can be safely accessed.
     59  1.1.1.3   chs  */
     60  1.1.1.3   chs krwlock_t os_lock;
     61  1.1.1.3   chs 
     62  1.1.1.3   chs /*
     63  1.1.1.3   chs  * Tunable to overwrite the maximum number of threads for the parallization
     64  1.1.1.3   chs  * of dmu_objset_find_dp, needed to speed up the import of pools with many
     65  1.1.1.3   chs  * datasets.
     66  1.1.1.3   chs  * Default is 4 times the number of leaf vdevs.
     67  1.1.1.3   chs  */
     68  1.1.1.3   chs int dmu_find_threads = 0;
     69  1.1.1.3   chs 
     70  1.1.1.3   chs static void dmu_objset_find_dp_cb(void *arg);
     71  1.1.1.3   chs 
     72  1.1.1.3   chs void
     73  1.1.1.3   chs dmu_objset_init(void)
     74  1.1.1.3   chs {
     75  1.1.1.3   chs 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
     76  1.1.1.3   chs }
     77  1.1.1.3   chs 
     78  1.1.1.3   chs void
     79  1.1.1.3   chs dmu_objset_fini(void)
     80  1.1.1.3   chs {
     81  1.1.1.3   chs 	rw_destroy(&os_lock);
     82  1.1.1.3   chs }
     83      1.1  haad 
     84      1.1  haad spa_t *
     85      1.1  haad dmu_objset_spa(objset_t *os)
     86      1.1  haad {
     87  1.1.1.2  haad 	return (os->os_spa);
     88      1.1  haad }
     89      1.1  haad 
     90      1.1  haad zilog_t *
     91      1.1  haad dmu_objset_zil(objset_t *os)
     92      1.1  haad {
     93  1.1.1.2  haad 	return (os->os_zil);
     94      1.1  haad }
     95      1.1  haad 
     96      1.1  haad dsl_pool_t *
     97      1.1  haad dmu_objset_pool(objset_t *os)
     98      1.1  haad {
     99      1.1  haad 	dsl_dataset_t *ds;
    100      1.1  haad 
    101  1.1.1.2  haad 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
    102      1.1  haad 		return (ds->ds_dir->dd_pool);
    103      1.1  haad 	else
    104  1.1.1.2  haad 		return (spa_get_dsl(os->os_spa));
    105      1.1  haad }
    106      1.1  haad 
    107      1.1  haad dsl_dataset_t *
    108      1.1  haad dmu_objset_ds(objset_t *os)
    109      1.1  haad {
    110  1.1.1.2  haad 	return (os->os_dsl_dataset);
    111      1.1  haad }
    112      1.1  haad 
    113      1.1  haad dmu_objset_type_t
    114      1.1  haad dmu_objset_type(objset_t *os)
    115      1.1  haad {
    116  1.1.1.2  haad 	return (os->os_phys->os_type);
    117      1.1  haad }
    118      1.1  haad 
    119      1.1  haad void
    120      1.1  haad dmu_objset_name(objset_t *os, char *buf)
    121      1.1  haad {
    122  1.1.1.2  haad 	dsl_dataset_name(os->os_dsl_dataset, buf);
    123      1.1  haad }
    124      1.1  haad 
    125      1.1  haad uint64_t
    126      1.1  haad dmu_objset_id(objset_t *os)
    127      1.1  haad {
    128  1.1.1.2  haad 	dsl_dataset_t *ds = os->os_dsl_dataset;
    129      1.1  haad 
    130      1.1  haad 	return (ds ? ds->ds_object : 0);
    131      1.1  haad }
    132      1.1  haad 
    133  1.1.1.3   chs zfs_sync_type_t
    134  1.1.1.3   chs dmu_objset_syncprop(objset_t *os)
    135  1.1.1.3   chs {
    136  1.1.1.3   chs 	return (os->os_sync);
    137  1.1.1.3   chs }
    138  1.1.1.3   chs 
    139  1.1.1.3   chs zfs_logbias_op_t
    140  1.1.1.2  haad dmu_objset_logbias(objset_t *os)
    141  1.1.1.2  haad {
    142  1.1.1.2  haad 	return (os->os_logbias);
    143  1.1.1.2  haad }
    144  1.1.1.2  haad 
    145      1.1  haad static void
    146      1.1  haad checksum_changed_cb(void *arg, uint64_t newval)
    147      1.1  haad {
    148  1.1.1.2  haad 	objset_t *os = arg;
    149      1.1  haad 
    150      1.1  haad 	/*
    151      1.1  haad 	 * Inheritance should have been done by now.
    152      1.1  haad 	 */
    153      1.1  haad 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
    154      1.1  haad 
    155  1.1.1.2  haad 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
    156      1.1  haad }
    157      1.1  haad 
    158      1.1  haad static void
    159      1.1  haad compression_changed_cb(void *arg, uint64_t newval)
    160      1.1  haad {
    161  1.1.1.2  haad 	objset_t *os = arg;
    162      1.1  haad 
    163      1.1  haad 	/*
    164      1.1  haad 	 * Inheritance and range checking should have been done by now.
    165      1.1  haad 	 */
    166      1.1  haad 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
    167      1.1  haad 
    168  1.1.1.3   chs 	os->os_compress = zio_compress_select(os->os_spa, newval,
    169  1.1.1.3   chs 	    ZIO_COMPRESS_ON);
    170      1.1  haad }
    171      1.1  haad 
    172      1.1  haad static void
    173      1.1  haad copies_changed_cb(void *arg, uint64_t newval)
    174      1.1  haad {
    175  1.1.1.2  haad 	objset_t *os = arg;
    176      1.1  haad 
    177      1.1  haad 	/*
    178      1.1  haad 	 * Inheritance and range checking should have been done by now.
    179      1.1  haad 	 */
    180      1.1  haad 	ASSERT(newval > 0);
    181  1.1.1.2  haad 	ASSERT(newval <= spa_max_replication(os->os_spa));
    182  1.1.1.2  haad 
    183  1.1.1.2  haad 	os->os_copies = newval;
    184  1.1.1.2  haad }
    185  1.1.1.2  haad 
    186  1.1.1.2  haad static void
    187  1.1.1.2  haad dedup_changed_cb(void *arg, uint64_t newval)
    188  1.1.1.2  haad {
    189  1.1.1.2  haad 	objset_t *os = arg;
    190  1.1.1.2  haad 	spa_t *spa = os->os_spa;
    191  1.1.1.2  haad 	enum zio_checksum checksum;
    192  1.1.1.2  haad 
    193  1.1.1.2  haad 	/*
    194  1.1.1.2  haad 	 * Inheritance should have been done by now.
    195  1.1.1.2  haad 	 */
    196  1.1.1.2  haad 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
    197  1.1.1.2  haad 
    198  1.1.1.2  haad 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
    199      1.1  haad 
    200  1.1.1.2  haad 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
    201  1.1.1.2  haad 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
    202      1.1  haad }
    203      1.1  haad 
    204      1.1  haad static void
    205      1.1  haad primary_cache_changed_cb(void *arg, uint64_t newval)
    206      1.1  haad {
    207  1.1.1.2  haad 	objset_t *os = arg;
    208      1.1  haad 
    209      1.1  haad 	/*
    210      1.1  haad 	 * Inheritance and range checking should have been done by now.
    211      1.1  haad 	 */
    212      1.1  haad 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
    213      1.1  haad 	    newval == ZFS_CACHE_METADATA);
    214      1.1  haad 
    215  1.1.1.2  haad 	os->os_primary_cache = newval;
    216      1.1  haad }
    217      1.1  haad 
    218      1.1  haad static void
    219      1.1  haad secondary_cache_changed_cb(void *arg, uint64_t newval)
    220      1.1  haad {
    221  1.1.1.2  haad 	objset_t *os = arg;
    222      1.1  haad 
    223      1.1  haad 	/*
    224      1.1  haad 	 * Inheritance and range checking should have been done by now.
    225      1.1  haad 	 */
    226      1.1  haad 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
    227      1.1  haad 	    newval == ZFS_CACHE_METADATA);
    228      1.1  haad 
    229  1.1.1.2  haad 	os->os_secondary_cache = newval;
    230  1.1.1.2  haad }
    231  1.1.1.2  haad 
    232  1.1.1.2  haad static void
    233  1.1.1.3   chs sync_changed_cb(void *arg, uint64_t newval)
    234  1.1.1.3   chs {
    235  1.1.1.3   chs 	objset_t *os = arg;
    236  1.1.1.3   chs 
    237  1.1.1.3   chs 	/*
    238  1.1.1.3   chs 	 * Inheritance and range checking should have been done by now.
    239  1.1.1.3   chs 	 */
    240  1.1.1.3   chs 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
    241  1.1.1.3   chs 	    newval == ZFS_SYNC_DISABLED);
    242  1.1.1.3   chs 
    243  1.1.1.3   chs 	os->os_sync = newval;
    244  1.1.1.3   chs 	if (os->os_zil)
    245  1.1.1.3   chs 		zil_set_sync(os->os_zil, newval);
    246  1.1.1.3   chs }
    247  1.1.1.3   chs 
    248  1.1.1.3   chs static void
    249  1.1.1.3   chs redundant_metadata_changed_cb(void *arg, uint64_t newval)
    250  1.1.1.3   chs {
    251  1.1.1.3   chs 	objset_t *os = arg;
    252  1.1.1.3   chs 
    253  1.1.1.3   chs 	/*
    254  1.1.1.3   chs 	 * Inheritance and range checking should have been done by now.
    255  1.1.1.3   chs 	 */
    256  1.1.1.3   chs 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
    257  1.1.1.3   chs 	    newval == ZFS_REDUNDANT_METADATA_MOST);
    258  1.1.1.3   chs 
    259  1.1.1.3   chs 	os->os_redundant_metadata = newval;
    260  1.1.1.3   chs }
    261  1.1.1.3   chs 
    262  1.1.1.3   chs static void
    263  1.1.1.2  haad logbias_changed_cb(void *arg, uint64_t newval)
    264  1.1.1.2  haad {
    265  1.1.1.2  haad 	objset_t *os = arg;
    266  1.1.1.2  haad 
    267  1.1.1.2  haad 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
    268  1.1.1.2  haad 	    newval == ZFS_LOGBIAS_THROUGHPUT);
    269  1.1.1.2  haad 	os->os_logbias = newval;
    270  1.1.1.2  haad 	if (os->os_zil)
    271  1.1.1.2  haad 		zil_set_logbias(os->os_zil, newval);
    272      1.1  haad }
    273      1.1  haad 
    274  1.1.1.3   chs static void
    275  1.1.1.3   chs recordsize_changed_cb(void *arg, uint64_t newval)
    276  1.1.1.3   chs {
    277  1.1.1.3   chs 	objset_t *os = arg;
    278  1.1.1.3   chs 
    279  1.1.1.3   chs 	os->os_recordsize = newval;
    280  1.1.1.3   chs }
    281  1.1.1.3   chs 
    282      1.1  haad void
    283      1.1  haad dmu_objset_byteswap(void *buf, size_t size)
    284      1.1  haad {
    285      1.1  haad 	objset_phys_t *osp = buf;
    286      1.1  haad 
    287  1.1.1.2  haad 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
    288      1.1  haad 	dnode_byteswap(&osp->os_meta_dnode);
    289      1.1  haad 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
    290      1.1  haad 	osp->os_type = BSWAP_64(osp->os_type);
    291  1.1.1.2  haad 	osp->os_flags = BSWAP_64(osp->os_flags);
    292  1.1.1.2  haad 	if (size == sizeof (objset_phys_t)) {
    293  1.1.1.2  haad 		dnode_byteswap(&osp->os_userused_dnode);
    294  1.1.1.2  haad 		dnode_byteswap(&osp->os_groupused_dnode);
    295  1.1.1.2  haad 	}
    296      1.1  haad }
    297      1.1  haad 
    298      1.1  haad int
    299      1.1  haad dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
    300  1.1.1.2  haad     objset_t **osp)
    301      1.1  haad {
    302  1.1.1.2  haad 	objset_t *os;
    303      1.1  haad 	int i, err;
    304      1.1  haad 
    305      1.1  haad 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
    306      1.1  haad 
    307  1.1.1.2  haad 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
    308  1.1.1.2  haad 	os->os_dsl_dataset = ds;
    309  1.1.1.2  haad 	os->os_spa = spa;
    310  1.1.1.2  haad 	os->os_rootbp = bp;
    311  1.1.1.2  haad 	if (!BP_IS_HOLE(os->os_rootbp)) {
    312  1.1.1.3   chs 		arc_flags_t aflags = ARC_FLAG_WAIT;
    313  1.1.1.3   chs 		zbookmark_phys_t zb;
    314  1.1.1.2  haad 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
    315  1.1.1.2  haad 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
    316  1.1.1.2  haad 
    317  1.1.1.2  haad 		if (DMU_OS_IS_L2CACHEABLE(os))
    318  1.1.1.3   chs 			aflags |= ARC_FLAG_L2CACHE;
    319      1.1  haad 
    320  1.1.1.2  haad 		dprintf_bp(os->os_rootbp, "reading %s", "");
    321  1.1.1.3   chs 		err = arc_read(NULL, spa, os->os_rootbp,
    322  1.1.1.2  haad 		    arc_getbuf_func, &os->os_phys_buf,
    323      1.1  haad 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
    324  1.1.1.3   chs 		if (err != 0) {
    325  1.1.1.2  haad 			kmem_free(os, sizeof (objset_t));
    326      1.1  haad 			/* convert checksum errors into IO errors */
    327      1.1  haad 			if (err == ECKSUM)
    328  1.1.1.3   chs 				err = SET_ERROR(EIO);
    329      1.1  haad 			return (err);
    330      1.1  haad 		}
    331  1.1.1.2  haad 
    332  1.1.1.2  haad 		/* Increase the blocksize if we are permitted. */
    333  1.1.1.2  haad 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
    334  1.1.1.2  haad 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
    335  1.1.1.3   chs 			arc_buf_t *buf = arc_alloc_buf(spa,
    336  1.1.1.2  haad 			    sizeof (objset_phys_t), &os->os_phys_buf,
    337  1.1.1.2  haad 			    ARC_BUFC_METADATA);
    338  1.1.1.2  haad 			bzero(buf->b_data, sizeof (objset_phys_t));
    339  1.1.1.2  haad 			bcopy(os->os_phys_buf->b_data, buf->b_data,
    340  1.1.1.2  haad 			    arc_buf_size(os->os_phys_buf));
    341  1.1.1.3   chs 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
    342  1.1.1.2  haad 			os->os_phys_buf = buf;
    343  1.1.1.2  haad 		}
    344  1.1.1.2  haad 
    345  1.1.1.2  haad 		os->os_phys = os->os_phys_buf->b_data;
    346  1.1.1.2  haad 		os->os_flags = os->os_phys->os_flags;
    347      1.1  haad 	} else {
    348  1.1.1.2  haad 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
    349  1.1.1.2  haad 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
    350  1.1.1.3   chs 		os->os_phys_buf = arc_alloc_buf(spa, size,
    351  1.1.1.2  haad 		    &os->os_phys_buf, ARC_BUFC_METADATA);
    352  1.1.1.2  haad 		os->os_phys = os->os_phys_buf->b_data;
    353  1.1.1.2  haad 		bzero(os->os_phys, size);
    354      1.1  haad 	}
    355      1.1  haad 
    356      1.1  haad 	/*
    357      1.1  haad 	 * Note: the changed_cb will be called once before the register
    358      1.1  haad 	 * func returns, thus changing the checksum/compression from the
    359      1.1  haad 	 * default (fletcher2/off).  Snapshots don't need to know about
    360      1.1  haad 	 * checksum/compression/copies.
    361      1.1  haad 	 */
    362  1.1.1.3   chs 	if (ds != NULL) {
    363  1.1.1.3   chs 		boolean_t needlock = B_FALSE;
    364  1.1.1.3   chs 
    365  1.1.1.3   chs 		/*
    366  1.1.1.3   chs 		 * Note: it's valid to open the objset if the dataset is
    367  1.1.1.3   chs 		 * long-held, in which case the pool_config lock will not
    368  1.1.1.3   chs 		 * be held.
    369  1.1.1.3   chs 		 */
    370  1.1.1.3   chs 		if (!dsl_pool_config_held(dmu_objset_pool(os))) {
    371  1.1.1.3   chs 			needlock = B_TRUE;
    372  1.1.1.3   chs 			dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
    373  1.1.1.3   chs 		}
    374  1.1.1.3   chs 		err = dsl_prop_register(ds,
    375  1.1.1.3   chs 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
    376  1.1.1.2  haad 		    primary_cache_changed_cb, os);
    377  1.1.1.3   chs 		if (err == 0) {
    378  1.1.1.3   chs 			err = dsl_prop_register(ds,
    379  1.1.1.3   chs 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
    380  1.1.1.2  haad 			    secondary_cache_changed_cb, os);
    381  1.1.1.3   chs 		}
    382  1.1.1.3   chs 		if (!ds->ds_is_snapshot) {
    383  1.1.1.3   chs 			if (err == 0) {
    384  1.1.1.3   chs 				err = dsl_prop_register(ds,
    385  1.1.1.3   chs 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
    386  1.1.1.2  haad 				    checksum_changed_cb, os);
    387  1.1.1.3   chs 			}
    388  1.1.1.3   chs 			if (err == 0) {
    389  1.1.1.3   chs 				err = dsl_prop_register(ds,
    390  1.1.1.3   chs 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
    391  1.1.1.2  haad 				    compression_changed_cb, os);
    392  1.1.1.3   chs 			}
    393  1.1.1.3   chs 			if (err == 0) {
    394  1.1.1.3   chs 				err = dsl_prop_register(ds,
    395  1.1.1.3   chs 				    zfs_prop_to_name(ZFS_PROP_COPIES),
    396  1.1.1.2  haad 				    copies_changed_cb, os);
    397  1.1.1.3   chs 			}
    398  1.1.1.3   chs 			if (err == 0) {
    399  1.1.1.3   chs 				err = dsl_prop_register(ds,
    400  1.1.1.3   chs 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
    401  1.1.1.2  haad 				    dedup_changed_cb, os);
    402  1.1.1.3   chs 			}
    403  1.1.1.3   chs 			if (err == 0) {
    404  1.1.1.3   chs 				err = dsl_prop_register(ds,
    405  1.1.1.3   chs 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
    406  1.1.1.2  haad 				    logbias_changed_cb, os);
    407  1.1.1.3   chs 			}
    408  1.1.1.3   chs 			if (err == 0) {
    409  1.1.1.3   chs 				err = dsl_prop_register(ds,
    410  1.1.1.3   chs 				    zfs_prop_to_name(ZFS_PROP_SYNC),
    411  1.1.1.3   chs 				    sync_changed_cb, os);
    412  1.1.1.3   chs 			}
    413  1.1.1.3   chs 			if (err == 0) {
    414  1.1.1.3   chs 				err = dsl_prop_register(ds,
    415  1.1.1.3   chs 				    zfs_prop_to_name(
    416  1.1.1.3   chs 				    ZFS_PROP_REDUNDANT_METADATA),
    417  1.1.1.3   chs 				    redundant_metadata_changed_cb, os);
    418  1.1.1.3   chs 			}
    419  1.1.1.3   chs 			if (err == 0) {
    420  1.1.1.3   chs 				err = dsl_prop_register(ds,
    421  1.1.1.3   chs 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
    422  1.1.1.3   chs 				    recordsize_changed_cb, os);
    423  1.1.1.3   chs 			}
    424      1.1  haad 		}
    425  1.1.1.3   chs 		if (needlock)
    426  1.1.1.3   chs 			dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
    427  1.1.1.3   chs 		if (err != 0) {
    428  1.1.1.3   chs 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
    429  1.1.1.2  haad 			kmem_free(os, sizeof (objset_t));
    430      1.1  haad 			return (err);
    431      1.1  haad 		}
    432  1.1.1.3   chs 	} else {
    433      1.1  haad 		/* It's the meta-objset. */
    434  1.1.1.2  haad 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
    435  1.1.1.3   chs 		os->os_compress = ZIO_COMPRESS_ON;
    436  1.1.1.2  haad 		os->os_copies = spa_max_replication(spa);
    437  1.1.1.2  haad 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
    438  1.1.1.3   chs 		os->os_dedup_verify = B_FALSE;
    439  1.1.1.3   chs 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
    440  1.1.1.3   chs 		os->os_sync = ZFS_SYNC_STANDARD;
    441  1.1.1.2  haad 		os->os_primary_cache = ZFS_CACHE_ALL;
    442  1.1.1.2  haad 		os->os_secondary_cache = ZFS_CACHE_ALL;
    443      1.1  haad 	}
    444      1.1  haad 
    445  1.1.1.3   chs 	if (ds == NULL || !ds->ds_is_snapshot)
    446  1.1.1.3   chs 		os->os_zil_header = os->os_phys->os_zil_header;
    447  1.1.1.2  haad 	os->os_zil = zil_alloc(os, &os->os_zil_header);
    448      1.1  haad 
    449      1.1  haad 	for (i = 0; i < TXG_SIZE; i++) {
    450  1.1.1.2  haad 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
    451      1.1  haad 		    offsetof(dnode_t, dn_dirty_link[i]));
    452  1.1.1.2  haad 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
    453      1.1  haad 		    offsetof(dnode_t, dn_dirty_link[i]));
    454      1.1  haad 	}
    455  1.1.1.2  haad 	list_create(&os->os_dnodes, sizeof (dnode_t),
    456      1.1  haad 	    offsetof(dnode_t, dn_link));
    457  1.1.1.2  haad 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
    458      1.1  haad 	    offsetof(dmu_buf_impl_t, db_link));
    459      1.1  haad 
    460  1.1.1.2  haad 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
    461  1.1.1.2  haad 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
    462  1.1.1.2  haad 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
    463  1.1.1.2  haad 
    464  1.1.1.3   chs 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
    465  1.1.1.3   chs 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
    466  1.1.1.2  haad 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
    467  1.1.1.3   chs 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
    468  1.1.1.3   chs 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
    469  1.1.1.3   chs 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
    470  1.1.1.3   chs 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
    471      1.1  haad 	}
    472      1.1  haad 
    473  1.1.1.2  haad 	*osp = os;
    474      1.1  haad 	return (0);
    475      1.1  haad }
    476      1.1  haad 
    477  1.1.1.2  haad int
    478  1.1.1.2  haad dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
    479      1.1  haad {
    480  1.1.1.2  haad 	int err = 0;
    481      1.1  haad 
    482  1.1.1.3   chs 	/*
    483  1.1.1.3   chs 	 * We shouldn't be doing anything with dsl_dataset_t's unless the
    484  1.1.1.3   chs 	 * pool_config lock is held, or the dataset is long-held.
    485  1.1.1.3   chs 	 */
    486  1.1.1.3   chs 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
    487  1.1.1.3   chs 	    dsl_dataset_long_held(ds));
    488  1.1.1.3   chs 
    489      1.1  haad 	mutex_enter(&ds->ds_opening_lock);
    490  1.1.1.3   chs 	if (ds->ds_objset == NULL) {
    491  1.1.1.3   chs 		objset_t *os;
    492  1.1.1.3   chs 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
    493      1.1  haad 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
    494  1.1.1.3   chs 		    ds, dsl_dataset_get_blkptr(ds), &os);
    495  1.1.1.3   chs 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
    496  1.1.1.3   chs 
    497  1.1.1.3   chs 		if (err == 0) {
    498  1.1.1.3   chs 			mutex_enter(&ds->ds_lock);
    499  1.1.1.3   chs 			ASSERT(ds->ds_objset == NULL);
    500  1.1.1.3   chs 			ds->ds_objset = os;
    501  1.1.1.3   chs 			mutex_exit(&ds->ds_lock);
    502  1.1.1.3   chs 		}
    503      1.1  haad 	}
    504  1.1.1.3   chs 	*osp = ds->ds_objset;
    505      1.1  haad 	mutex_exit(&ds->ds_opening_lock);
    506  1.1.1.2  haad 	return (err);
    507      1.1  haad }
    508      1.1  haad 
    509  1.1.1.3   chs /*
    510  1.1.1.3   chs  * Holds the pool while the objset is held.  Therefore only one objset
    511  1.1.1.3   chs  * can be held at a time.
    512  1.1.1.3   chs  */
    513      1.1  haad int
    514  1.1.1.2  haad dmu_objset_hold(const char *name, void *tag, objset_t **osp)
    515      1.1  haad {
    516  1.1.1.3   chs 	dsl_pool_t *dp;
    517  1.1.1.2  haad 	dsl_dataset_t *ds;
    518      1.1  haad 	int err;
    519      1.1  haad 
    520  1.1.1.3   chs 	err = dsl_pool_hold(name, tag, &dp);
    521  1.1.1.3   chs 	if (err != 0)
    522  1.1.1.3   chs 		return (err);
    523  1.1.1.3   chs 	err = dsl_dataset_hold(dp, name, tag, &ds);
    524  1.1.1.3   chs 	if (err != 0) {
    525  1.1.1.3   chs 		dsl_pool_rele(dp, tag);
    526  1.1.1.2  haad 		return (err);
    527  1.1.1.3   chs 	}
    528  1.1.1.2  haad 
    529  1.1.1.2  haad 	err = dmu_objset_from_ds(ds, osp);
    530  1.1.1.3   chs 	if (err != 0) {
    531  1.1.1.2  haad 		dsl_dataset_rele(ds, tag);
    532  1.1.1.3   chs 		dsl_pool_rele(dp, tag);
    533  1.1.1.3   chs 	}
    534  1.1.1.2  haad 
    535      1.1  haad 	return (err);
    536      1.1  haad }
    537      1.1  haad 
    538  1.1.1.3   chs static int
    539  1.1.1.3   chs dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
    540  1.1.1.2  haad     boolean_t readonly, void *tag, objset_t **osp)
    541      1.1  haad {
    542      1.1  haad 	int err;
    543      1.1  haad 
    544  1.1.1.2  haad 	err = dmu_objset_from_ds(ds, osp);
    545  1.1.1.3   chs 	if (err != 0) {
    546  1.1.1.2  haad 		dsl_dataset_disown(ds, tag);
    547  1.1.1.2  haad 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
    548  1.1.1.3   chs 		dsl_dataset_disown(ds, tag);
    549  1.1.1.3   chs 		return (SET_ERROR(EINVAL));
    550  1.1.1.2  haad 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
    551  1.1.1.3   chs 		dsl_dataset_disown(ds, tag);
    552  1.1.1.3   chs 		return (SET_ERROR(EROFS));
    553      1.1  haad 	}
    554      1.1  haad 	return (err);
    555      1.1  haad }
    556      1.1  haad 
    557  1.1.1.3   chs /*
    558  1.1.1.3   chs  * dsl_pool must not be held when this is called.
    559  1.1.1.3   chs  * Upon successful return, there will be a longhold on the dataset,
    560  1.1.1.3   chs  * and the dsl_pool will not be held.
    561  1.1.1.3   chs  */
    562  1.1.1.3   chs int
    563  1.1.1.3   chs dmu_objset_own(const char *name, dmu_objset_type_t type,
    564  1.1.1.3   chs     boolean_t readonly, void *tag, objset_t **osp)
    565  1.1.1.3   chs {
    566  1.1.1.3   chs 	dsl_pool_t *dp;
    567  1.1.1.3   chs 	dsl_dataset_t *ds;
    568  1.1.1.3   chs 	int err;
    569  1.1.1.3   chs 
    570  1.1.1.3   chs 	err = dsl_pool_hold(name, FTAG, &dp);
    571  1.1.1.3   chs 	if (err != 0)
    572  1.1.1.3   chs 		return (err);
    573  1.1.1.3   chs 	err = dsl_dataset_own(dp, name, tag, &ds);
    574  1.1.1.3   chs 	if (err != 0) {
    575  1.1.1.3   chs 		dsl_pool_rele(dp, FTAG);
    576  1.1.1.3   chs 		return (err);
    577  1.1.1.3   chs 	}
    578  1.1.1.3   chs 	err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
    579  1.1.1.3   chs 	dsl_pool_rele(dp, FTAG);
    580  1.1.1.3   chs 
    581  1.1.1.3   chs 	return (err);
    582  1.1.1.3   chs }
    583  1.1.1.3   chs 
    584  1.1.1.3   chs int
    585  1.1.1.3   chs dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
    586  1.1.1.3   chs     boolean_t readonly, void *tag, objset_t **osp)
    587  1.1.1.3   chs {
    588  1.1.1.3   chs 	dsl_dataset_t *ds;
    589  1.1.1.3   chs 	int err;
    590  1.1.1.3   chs 
    591  1.1.1.3   chs 	err = dsl_dataset_own_obj(dp, obj, tag, &ds);
    592  1.1.1.3   chs 	if (err != 0)
    593  1.1.1.3   chs 		return (err);
    594  1.1.1.3   chs 
    595  1.1.1.3   chs 	return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
    596  1.1.1.3   chs }
    597  1.1.1.3   chs 
    598      1.1  haad void
    599  1.1.1.2  haad dmu_objset_rele(objset_t *os, void *tag)
    600      1.1  haad {
    601  1.1.1.3   chs 	dsl_pool_t *dp = dmu_objset_pool(os);
    602  1.1.1.2  haad 	dsl_dataset_rele(os->os_dsl_dataset, tag);
    603  1.1.1.3   chs 	dsl_pool_rele(dp, tag);
    604  1.1.1.3   chs }
    605  1.1.1.3   chs 
    606  1.1.1.3   chs /*
    607  1.1.1.3   chs  * When we are called, os MUST refer to an objset associated with a dataset
    608  1.1.1.3   chs  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
    609  1.1.1.3   chs  * == tag.  We will then release and reacquire ownership of the dataset while
    610  1.1.1.3   chs  * holding the pool config_rwlock to avoid intervening namespace or ownership
    611  1.1.1.3   chs  * changes may occur.
    612  1.1.1.3   chs  *
    613  1.1.1.3   chs  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
    614  1.1.1.3   chs  * release the hold on its dataset and acquire a new one on the dataset of the
    615  1.1.1.3   chs  * same name so that it can be partially torn down and reconstructed.
    616  1.1.1.3   chs  */
    617  1.1.1.3   chs void
    618  1.1.1.3   chs dmu_objset_refresh_ownership(objset_t *os, void *tag)
    619  1.1.1.3   chs {
    620  1.1.1.3   chs 	dsl_pool_t *dp;
    621  1.1.1.3   chs 	dsl_dataset_t *ds, *newds;
    622  1.1.1.3   chs 	char name[ZFS_MAX_DATASET_NAME_LEN];
    623  1.1.1.3   chs 
    624  1.1.1.3   chs 	ds = os->os_dsl_dataset;
    625  1.1.1.3   chs 	VERIFY3P(ds, !=, NULL);
    626  1.1.1.3   chs 	VERIFY3P(ds->ds_owner, ==, tag);
    627  1.1.1.3   chs 	VERIFY(dsl_dataset_long_held(ds));
    628  1.1.1.3   chs 
    629  1.1.1.3   chs 	dsl_dataset_name(ds, name);
    630  1.1.1.3   chs 	dp = dmu_objset_pool(os);
    631  1.1.1.3   chs 	dsl_pool_config_enter(dp, FTAG);
    632  1.1.1.3   chs 	dmu_objset_disown(os, tag);
    633  1.1.1.3   chs 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
    634  1.1.1.3   chs 	VERIFY3P(newds, ==, os->os_dsl_dataset);
    635  1.1.1.3   chs 	dsl_pool_config_exit(dp, FTAG);
    636  1.1.1.2  haad }
    637  1.1.1.2  haad 
    638  1.1.1.2  haad void
    639  1.1.1.2  haad dmu_objset_disown(objset_t *os, void *tag)
    640  1.1.1.2  haad {
    641  1.1.1.2  haad 	dsl_dataset_disown(os->os_dsl_dataset, tag);
    642      1.1  haad }
    643      1.1  haad 
    644  1.1.1.3   chs void
    645      1.1  haad dmu_objset_evict_dbufs(objset_t *os)
    646      1.1  haad {
    647  1.1.1.3   chs 	dnode_t dn_marker;
    648      1.1  haad 	dnode_t *dn;
    649      1.1  haad 
    650  1.1.1.2  haad 	mutex_enter(&os->os_lock);
    651  1.1.1.3   chs 	dn = list_head(&os->os_dnodes);
    652  1.1.1.3   chs 	while (dn != NULL) {
    653  1.1.1.3   chs 		/*
    654  1.1.1.3   chs 		 * Skip dnodes without holds.  We have to do this dance
    655  1.1.1.3   chs 		 * because dnode_add_ref() only works if there is already a
    656  1.1.1.3   chs 		 * hold.  If the dnode has no holds, then it has no dbufs.
    657  1.1.1.3   chs 		 */
    658  1.1.1.3   chs 		if (dnode_add_ref(dn, FTAG)) {
    659  1.1.1.3   chs 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
    660  1.1.1.3   chs 			mutex_exit(&os->os_lock);
    661  1.1.1.3   chs 
    662  1.1.1.3   chs 			dnode_evict_dbufs(dn);
    663  1.1.1.3   chs 			dnode_rele(dn, FTAG);
    664  1.1.1.3   chs 
    665  1.1.1.3   chs 			mutex_enter(&os->os_lock);
    666  1.1.1.3   chs 			dn = list_next(&os->os_dnodes, &dn_marker);
    667  1.1.1.3   chs 			list_remove(&os->os_dnodes, &dn_marker);
    668  1.1.1.3   chs 		} else {
    669  1.1.1.3   chs 			dn = list_next(&os->os_dnodes, dn);
    670  1.1.1.3   chs 		}
    671      1.1  haad 	}
    672  1.1.1.2  haad 	mutex_exit(&os->os_lock);
    673  1.1.1.3   chs 
    674  1.1.1.3   chs 	if (DMU_USERUSED_DNODE(os) != NULL) {
    675  1.1.1.3   chs 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
    676  1.1.1.3   chs 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
    677  1.1.1.3   chs 	}
    678  1.1.1.3   chs 	dnode_evict_dbufs(DMU_META_DNODE(os));
    679      1.1  haad }
    680      1.1  haad 
    681  1.1.1.3   chs /*
    682  1.1.1.3   chs  * Objset eviction processing is split into into two pieces.
    683  1.1.1.3   chs  * The first marks the objset as evicting, evicts any dbufs that
    684  1.1.1.3   chs  * have a refcount of zero, and then queues up the objset for the
    685  1.1.1.3   chs  * second phase of eviction.  Once os->os_dnodes has been cleared by
    686  1.1.1.3   chs  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
    687  1.1.1.3   chs  * The second phase closes the special dnodes, dequeues the objset from
    688  1.1.1.3   chs  * the list of those undergoing eviction, and finally frees the objset.
    689  1.1.1.3   chs  *
    690  1.1.1.3   chs  * NOTE: Due to asynchronous eviction processing (invocation of
    691  1.1.1.3   chs  *       dnode_buf_pageout()), it is possible for the meta dnode for the
    692  1.1.1.3   chs  *       objset to have no holds even though os->os_dnodes is not empty.
    693  1.1.1.3   chs  */
    694      1.1  haad void
    695  1.1.1.2  haad dmu_objset_evict(objset_t *os)
    696      1.1  haad {
    697  1.1.1.2  haad 	dsl_dataset_t *ds = os->os_dsl_dataset;
    698      1.1  haad 
    699  1.1.1.2  haad 	for (int t = 0; t < TXG_SIZE; t++)
    700  1.1.1.2  haad 		ASSERT(!dmu_objset_is_dirty(os, t));
    701      1.1  haad 
    702  1.1.1.3   chs 	if (ds)
    703  1.1.1.3   chs 		dsl_prop_unregister_all(ds, os);
    704  1.1.1.3   chs 
    705  1.1.1.3   chs 	if (os->os_sa)
    706  1.1.1.3   chs 		sa_tear_down(os);
    707  1.1.1.3   chs 
    708  1.1.1.3   chs 	dmu_objset_evict_dbufs(os);
    709  1.1.1.3   chs 
    710  1.1.1.3   chs 	mutex_enter(&os->os_lock);
    711  1.1.1.3   chs 	spa_evicting_os_register(os->os_spa, os);
    712  1.1.1.3   chs 	if (list_is_empty(&os->os_dnodes)) {
    713  1.1.1.3   chs 		mutex_exit(&os->os_lock);
    714  1.1.1.3   chs 		dmu_objset_evict_done(os);
    715  1.1.1.3   chs 	} else {
    716  1.1.1.3   chs 		mutex_exit(&os->os_lock);
    717      1.1  haad 	}
    718  1.1.1.3   chs }
    719      1.1  haad 
    720  1.1.1.3   chs void
    721  1.1.1.3   chs dmu_objset_evict_done(objset_t *os)
    722  1.1.1.3   chs {
    723  1.1.1.3   chs 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
    724  1.1.1.3   chs 
    725  1.1.1.3   chs 	dnode_special_close(&os->os_meta_dnode);
    726  1.1.1.3   chs 	if (DMU_USERUSED_DNODE(os)) {
    727  1.1.1.3   chs 		dnode_special_close(&os->os_userused_dnode);
    728  1.1.1.3   chs 		dnode_special_close(&os->os_groupused_dnode);
    729  1.1.1.2  haad 	}
    730  1.1.1.2  haad 	zil_free(os->os_zil);
    731  1.1.1.2  haad 
    732  1.1.1.3   chs 	arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
    733  1.1.1.3   chs 
    734  1.1.1.3   chs 	/*
    735  1.1.1.3   chs 	 * This is a barrier to prevent the objset from going away in
    736  1.1.1.3   chs 	 * dnode_move() until we can safely ensure that the objset is still in
    737  1.1.1.3   chs 	 * use. We consider the objset valid before the barrier and invalid
    738  1.1.1.3   chs 	 * after the barrier.
    739  1.1.1.3   chs 	 */
    740  1.1.1.3   chs 	rw_enter(&os_lock, RW_READER);
    741  1.1.1.3   chs 	rw_exit(&os_lock);
    742  1.1.1.2  haad 
    743  1.1.1.2  haad 	mutex_destroy(&os->os_lock);
    744  1.1.1.2  haad 	mutex_destroy(&os->os_obj_lock);
    745  1.1.1.2  haad 	mutex_destroy(&os->os_user_ptr_lock);
    746  1.1.1.3   chs 	spa_evicting_os_deregister(os->os_spa, os);
    747  1.1.1.2  haad 	kmem_free(os, sizeof (objset_t));
    748  1.1.1.2  haad }
    749  1.1.1.2  haad 
    750  1.1.1.2  haad timestruc_t
    751  1.1.1.2  haad dmu_objset_snap_cmtime(objset_t *os)
    752  1.1.1.2  haad {
    753  1.1.1.2  haad 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
    754      1.1  haad }
    755      1.1  haad 
    756      1.1  haad /* called from dsl for meta-objset */
    757  1.1.1.2  haad objset_t *
    758      1.1  haad dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
    759      1.1  haad     dmu_objset_type_t type, dmu_tx_t *tx)
    760      1.1  haad {
    761  1.1.1.2  haad 	objset_t *os;
    762      1.1  haad 	dnode_t *mdn;
    763      1.1  haad 
    764      1.1  haad 	ASSERT(dmu_tx_is_syncing(tx));
    765  1.1.1.3   chs 
    766  1.1.1.3   chs 	if (ds != NULL)
    767  1.1.1.3   chs 		VERIFY0(dmu_objset_from_ds(ds, &os));
    768  1.1.1.3   chs 	else
    769  1.1.1.3   chs 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
    770  1.1.1.3   chs 
    771  1.1.1.3   chs 	mdn = DMU_META_DNODE(os);
    772      1.1  haad 
    773      1.1  haad 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
    774      1.1  haad 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
    775      1.1  haad 
    776      1.1  haad 	/*
    777      1.1  haad 	 * We don't want to have to increase the meta-dnode's nlevels
    778      1.1  haad 	 * later, because then we could do it in quescing context while
    779      1.1  haad 	 * we are also accessing it in open context.
    780      1.1  haad 	 *
    781      1.1  haad 	 * This precaution is not necessary for the MOS (ds == NULL),
    782      1.1  haad 	 * because the MOS is only updated in syncing context.
    783      1.1  haad 	 * This is most fortunate: the MOS is the only objset that
    784      1.1  haad 	 * needs to be synced multiple times as spa_sync() iterates
    785      1.1  haad 	 * to convergence, so minimizing its dn_nlevels matters.
    786      1.1  haad 	 */
    787      1.1  haad 	if (ds != NULL) {
    788      1.1  haad 		int levels = 1;
    789      1.1  haad 
    790      1.1  haad 		/*
    791      1.1  haad 		 * Determine the number of levels necessary for the meta-dnode
    792  1.1.1.3   chs 		 * to contain DN_MAX_OBJECT dnodes.  Note that in order to
    793  1.1.1.3   chs 		 * ensure that we do not overflow 64 bits, there has to be
    794  1.1.1.3   chs 		 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
    795  1.1.1.3   chs 		 * but < 2^64.  Therefore,
    796  1.1.1.3   chs 		 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
    797  1.1.1.3   chs 		 * less than (64 - log2(DN_MAX_OBJECT)) (16).
    798      1.1  haad 		 */
    799  1.1.1.3   chs 		while ((uint64_t)mdn->dn_nblkptr <<
    800  1.1.1.3   chs 		    (mdn->dn_datablkshift - DNODE_SHIFT +
    801      1.1  haad 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
    802  1.1.1.3   chs 		    DN_MAX_OBJECT)
    803      1.1  haad 			levels++;
    804      1.1  haad 
    805      1.1  haad 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
    806      1.1  haad 		    mdn->dn_nlevels = levels;
    807      1.1  haad 	}
    808      1.1  haad 
    809      1.1  haad 	ASSERT(type != DMU_OST_NONE);
    810      1.1  haad 	ASSERT(type != DMU_OST_ANY);
    811      1.1  haad 	ASSERT(type < DMU_OST_NUMTYPES);
    812  1.1.1.2  haad 	os->os_phys->os_type = type;
    813  1.1.1.2  haad 	if (dmu_objset_userused_enabled(os)) {
    814  1.1.1.2  haad 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
    815  1.1.1.2  haad 		os->os_flags = os->os_phys->os_flags;
    816  1.1.1.2  haad 	}
    817      1.1  haad 
    818      1.1  haad 	dsl_dataset_dirty(ds, tx);
    819      1.1  haad 
    820  1.1.1.2  haad 	return (os);
    821      1.1  haad }
    822      1.1  haad 
    823  1.1.1.3   chs typedef struct dmu_objset_create_arg {
    824  1.1.1.3   chs 	const char *doca_name;
    825  1.1.1.3   chs 	cred_t *doca_cred;
    826  1.1.1.3   chs 	void (*doca_userfunc)(objset_t *os, void *arg,
    827  1.1.1.3   chs 	    cred_t *cr, dmu_tx_t *tx);
    828  1.1.1.3   chs 	void *doca_userarg;
    829  1.1.1.3   chs 	dmu_objset_type_t doca_type;
    830  1.1.1.3   chs 	uint64_t doca_flags;
    831  1.1.1.3   chs } dmu_objset_create_arg_t;
    832      1.1  haad 
    833      1.1  haad /*ARGSUSED*/
    834      1.1  haad static int
    835  1.1.1.3   chs dmu_objset_create_check(void *arg, dmu_tx_t *tx)
    836      1.1  haad {
    837  1.1.1.3   chs 	dmu_objset_create_arg_t *doca = arg;
    838  1.1.1.3   chs 	dsl_pool_t *dp = dmu_tx_pool(tx);
    839  1.1.1.3   chs 	dsl_dir_t *pdd;
    840  1.1.1.3   chs 	const char *tail;
    841  1.1.1.3   chs 	int error;
    842  1.1.1.3   chs 
    843  1.1.1.3   chs 	if (strchr(doca->doca_name, '@') != NULL)
    844  1.1.1.3   chs 		return (SET_ERROR(EINVAL));
    845      1.1  haad 
    846  1.1.1.3   chs 	if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
    847  1.1.1.3   chs 		return (SET_ERROR(ENAMETOOLONG));
    848  1.1.1.3   chs 
    849  1.1.1.3   chs 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
    850  1.1.1.3   chs 	if (error != 0)
    851  1.1.1.3   chs 		return (error);
    852  1.1.1.3   chs 	if (tail == NULL) {
    853  1.1.1.3   chs 		dsl_dir_rele(pdd, FTAG);
    854  1.1.1.3   chs 		return (SET_ERROR(EEXIST));
    855      1.1  haad 	}
    856  1.1.1.3   chs 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
    857  1.1.1.3   chs 	    doca->doca_cred);
    858  1.1.1.3   chs 	dsl_dir_rele(pdd, FTAG);
    859      1.1  haad 
    860  1.1.1.3   chs 	return (error);
    861      1.1  haad }
    862      1.1  haad 
    863      1.1  haad static void
    864  1.1.1.3   chs dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
    865      1.1  haad {
    866  1.1.1.3   chs 	dmu_objset_create_arg_t *doca = arg;
    867  1.1.1.3   chs 	dsl_pool_t *dp = dmu_tx_pool(tx);
    868  1.1.1.3   chs 	dsl_dir_t *pdd;
    869  1.1.1.3   chs 	const char *tail;
    870  1.1.1.3   chs 	dsl_dataset_t *ds;
    871  1.1.1.3   chs 	uint64_t obj;
    872  1.1.1.3   chs 	blkptr_t *bp;
    873  1.1.1.3   chs 	objset_t *os;
    874      1.1  haad 
    875  1.1.1.3   chs 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
    876      1.1  haad 
    877  1.1.1.3   chs 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
    878  1.1.1.3   chs 	    doca->doca_cred, tx);
    879  1.1.1.2  haad 
    880  1.1.1.3   chs 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
    881  1.1.1.3   chs 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
    882  1.1.1.3   chs 	bp = dsl_dataset_get_blkptr(ds);
    883  1.1.1.3   chs 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
    884  1.1.1.3   chs 	    ds, bp, doca->doca_type, tx);
    885  1.1.1.3   chs 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
    886      1.1  haad 
    887  1.1.1.3   chs 	if (doca->doca_userfunc != NULL) {
    888  1.1.1.3   chs 		doca->doca_userfunc(os, doca->doca_userarg,
    889  1.1.1.3   chs 		    doca->doca_cred, tx);
    890      1.1  haad 	}
    891      1.1  haad 
    892  1.1.1.3   chs 	spa_history_log_internal_ds(ds, "create", tx, "");
    893  1.1.1.3   chs 	dsl_dataset_rele(ds, FTAG);
    894  1.1.1.3   chs 	dsl_dir_rele(pdd, FTAG);
    895      1.1  haad }
    896      1.1  haad 
    897      1.1  haad int
    898  1.1.1.2  haad dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
    899      1.1  haad     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
    900      1.1  haad {
    901  1.1.1.3   chs 	dmu_objset_create_arg_t doca;
    902      1.1  haad 
    903  1.1.1.3   chs 	doca.doca_name = name;
    904  1.1.1.3   chs 	doca.doca_cred = CRED();
    905  1.1.1.3   chs 	doca.doca_flags = flags;
    906  1.1.1.3   chs 	doca.doca_userfunc = func;
    907  1.1.1.3   chs 	doca.doca_userarg = arg;
    908  1.1.1.3   chs 	doca.doca_type = type;
    909  1.1.1.3   chs 
    910  1.1.1.3   chs 	return (dsl_sync_task(name,
    911  1.1.1.3   chs 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
    912  1.1.1.3   chs 	    5, ZFS_SPACE_CHECK_NORMAL));
    913  1.1.1.3   chs }
    914  1.1.1.3   chs 
    915  1.1.1.3   chs typedef struct dmu_objset_clone_arg {
    916  1.1.1.3   chs 	const char *doca_clone;
    917  1.1.1.3   chs 	const char *doca_origin;
    918  1.1.1.3   chs 	cred_t *doca_cred;
    919  1.1.1.3   chs } dmu_objset_clone_arg_t;
    920      1.1  haad 
    921  1.1.1.3   chs /*ARGSUSED*/
    922  1.1.1.3   chs static int
    923  1.1.1.3   chs dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
    924      1.1  haad {
    925  1.1.1.3   chs 	dmu_objset_clone_arg_t *doca = arg;
    926  1.1.1.2  haad 	dsl_dir_t *pdd;
    927  1.1.1.2  haad 	const char *tail;
    928  1.1.1.3   chs 	int error;
    929  1.1.1.3   chs 	dsl_dataset_t *origin;
    930  1.1.1.3   chs 	dsl_pool_t *dp = dmu_tx_pool(tx);
    931  1.1.1.2  haad 
    932  1.1.1.3   chs 	if (strchr(doca->doca_clone, '@') != NULL)
    933  1.1.1.3   chs 		return (SET_ERROR(EINVAL));
    934      1.1  haad 
    935  1.1.1.3   chs 	if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
    936  1.1.1.3   chs 		return (SET_ERROR(ENAMETOOLONG));
    937      1.1  haad 
    938  1.1.1.3   chs 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
    939  1.1.1.3   chs 	if (error != 0)
    940  1.1.1.3   chs 		return (error);
    941  1.1.1.3   chs 	if (tail == NULL) {
    942  1.1.1.3   chs 		dsl_dir_rele(pdd, FTAG);
    943  1.1.1.3   chs 		return (SET_ERROR(EEXIST));
    944  1.1.1.2  haad 	}
    945  1.1.1.2  haad 
    946  1.1.1.3   chs 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
    947  1.1.1.3   chs 	    doca->doca_cred);
    948  1.1.1.3   chs 	if (error != 0) {
    949  1.1.1.3   chs 		dsl_dir_rele(pdd, FTAG);
    950  1.1.1.3   chs 		return (SET_ERROR(EDQUOT));
    951  1.1.1.3   chs 	}
    952  1.1.1.3   chs 	dsl_dir_rele(pdd, FTAG);
    953      1.1  haad 
    954  1.1.1.3   chs 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
    955  1.1.1.3   chs 	if (error != 0)
    956  1.1.1.3   chs 		return (error);
    957      1.1  haad 
    958  1.1.1.3   chs 	/* You can only clone snapshots, not the head datasets. */
    959  1.1.1.3   chs 	if (!origin->ds_is_snapshot) {
    960  1.1.1.3   chs 		dsl_dataset_rele(origin, FTAG);
    961  1.1.1.3   chs 		return (SET_ERROR(EINVAL));
    962  1.1.1.3   chs 	}
    963  1.1.1.3   chs 	dsl_dataset_rele(origin, FTAG);
    964  1.1.1.2  haad 
    965  1.1.1.3   chs 	return (0);
    966  1.1.1.2  haad }
    967  1.1.1.2  haad 
    968  1.1.1.2  haad static void
    969  1.1.1.3   chs dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
    970  1.1.1.2  haad {
    971  1.1.1.3   chs 	dmu_objset_clone_arg_t *doca = arg;
    972  1.1.1.3   chs 	dsl_pool_t *dp = dmu_tx_pool(tx);
    973  1.1.1.3   chs 	dsl_dir_t *pdd;
    974  1.1.1.3   chs 	const char *tail;
    975  1.1.1.3   chs 	dsl_dataset_t *origin, *ds;
    976  1.1.1.3   chs 	uint64_t obj;
    977  1.1.1.3   chs 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
    978  1.1.1.2  haad 
    979  1.1.1.3   chs 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
    980  1.1.1.3   chs 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
    981  1.1.1.2  haad 
    982  1.1.1.3   chs 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
    983  1.1.1.3   chs 	    doca->doca_cred, tx);
    984  1.1.1.3   chs 
    985  1.1.1.3   chs 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
    986  1.1.1.3   chs 	dsl_dataset_name(origin, namebuf);
    987  1.1.1.3   chs 	spa_history_log_internal_ds(ds, "clone", tx,
    988  1.1.1.3   chs 	    "origin=%s (%llu)", namebuf, origin->ds_object);
    989  1.1.1.3   chs 	dsl_dataset_rele(ds, FTAG);
    990  1.1.1.3   chs 	dsl_dataset_rele(origin, FTAG);
    991  1.1.1.3   chs 	dsl_dir_rele(pdd, FTAG);
    992  1.1.1.2  haad }
    993      1.1  haad 
    994  1.1.1.3   chs int
    995  1.1.1.3   chs dmu_objset_clone(const char *clone, const char *origin)
    996      1.1  haad {
    997  1.1.1.3   chs 	dmu_objset_clone_arg_t doca;
    998  1.1.1.2  haad 
    999  1.1.1.3   chs 	doca.doca_clone = clone;
   1000  1.1.1.3   chs 	doca.doca_origin = origin;
   1001  1.1.1.3   chs 	doca.doca_cred = CRED();
   1002      1.1  haad 
   1003  1.1.1.3   chs 	return (dsl_sync_task(clone,
   1004  1.1.1.3   chs 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
   1005  1.1.1.3   chs 	    5, ZFS_SPACE_CHECK_NORMAL));
   1006      1.1  haad }
   1007      1.1  haad 
   1008      1.1  haad int
   1009  1.1.1.3   chs dmu_objset_snapshot_one(const char *fsname, const char *snapname)
   1010      1.1  haad {
   1011      1.1  haad 	int err;
   1012  1.1.1.3   chs 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
   1013  1.1.1.3   chs 	nvlist_t *snaps = fnvlist_alloc();
   1014      1.1  haad 
   1015  1.1.1.3   chs 	fnvlist_add_boolean(snaps, longsnap);
   1016  1.1.1.3   chs 	strfree(longsnap);
   1017  1.1.1.3   chs 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
   1018  1.1.1.3   chs 	fnvlist_free(snaps);
   1019      1.1  haad 	return (err);
   1020      1.1  haad }
   1021      1.1  haad 
   1022      1.1  haad static void
   1023  1.1.1.2  haad dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
   1024      1.1  haad {
   1025      1.1  haad 	dnode_t *dn;
   1026      1.1  haad 
   1027      1.1  haad 	while (dn = list_head(list)) {
   1028      1.1  haad 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
   1029      1.1  haad 		ASSERT(dn->dn_dbuf->db_data_pending);
   1030      1.1  haad 		/*
   1031  1.1.1.2  haad 		 * Initialize dn_zio outside dnode_sync() because the
   1032  1.1.1.2  haad 		 * meta-dnode needs to set it ouside dnode_sync().
   1033      1.1  haad 		 */
   1034      1.1  haad 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
   1035      1.1  haad 		ASSERT(dn->dn_zio);
   1036      1.1  haad 
   1037      1.1  haad 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
   1038      1.1  haad 		list_remove(list, dn);
   1039  1.1.1.2  haad 
   1040  1.1.1.2  haad 		if (newlist) {
   1041  1.1.1.2  haad 			(void) dnode_add_ref(dn, newlist);
   1042  1.1.1.2  haad 			list_insert_tail(newlist, dn);
   1043  1.1.1.2  haad 		}
   1044  1.1.1.2  haad 
   1045      1.1  haad 		dnode_sync(dn, tx);
   1046      1.1  haad 	}
   1047      1.1  haad }
   1048      1.1  haad 
   1049      1.1  haad /* ARGSUSED */
   1050      1.1  haad static void
   1051  1.1.1.2  haad dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
   1052      1.1  haad {
   1053      1.1  haad 	blkptr_t *bp = zio->io_bp;
   1054  1.1.1.2  haad 	objset_t *os = arg;
   1055      1.1  haad 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
   1056      1.1  haad 
   1057  1.1.1.3   chs 	ASSERT(!BP_IS_EMBEDDED(bp));
   1058  1.1.1.3   chs 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
   1059  1.1.1.3   chs 	ASSERT0(BP_GET_LEVEL(bp));
   1060      1.1  haad 
   1061      1.1  haad 	/*
   1062  1.1.1.2  haad 	 * Update rootbp fill count: it should be the number of objects
   1063  1.1.1.2  haad 	 * allocated in the object set (not counting the "special"
   1064  1.1.1.2  haad 	 * objects that are stored in the objset_phys_t -- the meta
   1065  1.1.1.2  haad 	 * dnode and user/group accounting objects).
   1066      1.1  haad 	 */
   1067  1.1.1.2  haad 	bp->blk_fill = 0;
   1068      1.1  haad 	for (int i = 0; i < dnp->dn_nblkptr; i++)
   1069  1.1.1.3   chs 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
   1070  1.1.1.3   chs 	if (os->os_dsl_dataset != NULL)
   1071  1.1.1.3   chs 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
   1072  1.1.1.3   chs 	*os->os_rootbp = *bp;
   1073  1.1.1.3   chs 	if (os->os_dsl_dataset != NULL)
   1074  1.1.1.3   chs 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
   1075  1.1.1.2  haad }
   1076  1.1.1.2  haad 
   1077  1.1.1.2  haad /* ARGSUSED */
   1078  1.1.1.2  haad static void
   1079  1.1.1.2  haad dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
   1080  1.1.1.2  haad {
   1081  1.1.1.2  haad 	blkptr_t *bp = zio->io_bp;
   1082  1.1.1.2  haad 	blkptr_t *bp_orig = &zio->io_bp_orig;
   1083  1.1.1.2  haad 	objset_t *os = arg;
   1084      1.1  haad 
   1085      1.1  haad 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
   1086  1.1.1.2  haad 		ASSERT(BP_EQUAL(bp, bp_orig));
   1087      1.1  haad 	} else {
   1088  1.1.1.2  haad 		dsl_dataset_t *ds = os->os_dsl_dataset;
   1089  1.1.1.2  haad 		dmu_tx_t *tx = os->os_synctx;
   1090  1.1.1.2  haad 
   1091  1.1.1.2  haad 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
   1092  1.1.1.2  haad 		dsl_dataset_block_born(ds, bp, tx);
   1093      1.1  haad 	}
   1094  1.1.1.3   chs 	kmem_free(bp, sizeof (*bp));
   1095      1.1  haad }
   1096      1.1  haad 
   1097      1.1  haad /* called from dsl */
   1098      1.1  haad void
   1099  1.1.1.2  haad dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
   1100      1.1  haad {
   1101      1.1  haad 	int txgoff;
   1102  1.1.1.3   chs 	zbookmark_phys_t zb;
   1103  1.1.1.2  haad 	zio_prop_t zp;
   1104      1.1  haad 	zio_t *zio;
   1105      1.1  haad 	list_t *list;
   1106  1.1.1.2  haad 	list_t *newlist = NULL;
   1107      1.1  haad 	dbuf_dirty_record_t *dr;
   1108  1.1.1.3   chs 	blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
   1109  1.1.1.3   chs 	*blkptr_copy = *os->os_rootbp;
   1110      1.1  haad 
   1111      1.1  haad 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
   1112      1.1  haad 
   1113      1.1  haad 	ASSERT(dmu_tx_is_syncing(tx));
   1114      1.1  haad 	/* XXX the write_done callback should really give us the tx... */
   1115      1.1  haad 	os->os_synctx = tx;
   1116      1.1  haad 
   1117      1.1  haad 	if (os->os_dsl_dataset == NULL) {
   1118      1.1  haad 		/*
   1119      1.1  haad 		 * This is the MOS.  If we have upgraded,
   1120      1.1  haad 		 * spa_max_replication() could change, so reset
   1121      1.1  haad 		 * os_copies here.
   1122      1.1  haad 		 */
   1123      1.1  haad 		os->os_copies = spa_max_replication(os->os_spa);
   1124      1.1  haad 	}
   1125      1.1  haad 
   1126      1.1  haad 	/*
   1127      1.1  haad 	 * Create the root block IO
   1128      1.1  haad 	 */
   1129  1.1.1.2  haad 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
   1130  1.1.1.2  haad 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
   1131  1.1.1.2  haad 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
   1132  1.1.1.3   chs 	arc_release(os->os_phys_buf, &os->os_phys_buf);
   1133  1.1.1.2  haad 
   1134  1.1.1.2  haad 	dmu_write_policy(os, NULL, 0, 0, &zp);
   1135  1.1.1.2  haad 
   1136  1.1.1.2  haad 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
   1137  1.1.1.3   chs 	    blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
   1138  1.1.1.3   chs 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
   1139  1.1.1.3   chs 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
   1140      1.1  haad 
   1141      1.1  haad 	/*
   1142  1.1.1.2  haad 	 * Sync special dnodes - the parent IO for the sync is the root block
   1143      1.1  haad 	 */
   1144  1.1.1.3   chs 	DMU_META_DNODE(os)->dn_zio = zio;
   1145  1.1.1.3   chs 	dnode_sync(DMU_META_DNODE(os), tx);
   1146      1.1  haad 
   1147  1.1.1.2  haad 	os->os_phys->os_flags = os->os_flags;
   1148  1.1.1.2  haad 
   1149  1.1.1.3   chs 	if (DMU_USERUSED_DNODE(os) &&
   1150  1.1.1.3   chs 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
   1151  1.1.1.3   chs 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
   1152  1.1.1.3   chs 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
   1153  1.1.1.3   chs 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
   1154  1.1.1.3   chs 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
   1155  1.1.1.2  haad 	}
   1156  1.1.1.2  haad 
   1157      1.1  haad 	txgoff = tx->tx_txg & TXG_MASK;
   1158      1.1  haad 
   1159  1.1.1.2  haad 	if (dmu_objset_userused_enabled(os)) {
   1160  1.1.1.2  haad 		newlist = &os->os_synced_dnodes;
   1161  1.1.1.2  haad 		/*
   1162  1.1.1.2  haad 		 * We must create the list here because it uses the
   1163  1.1.1.2  haad 		 * dn_dirty_link[] of this txg.
   1164  1.1.1.2  haad 		 */
   1165  1.1.1.2  haad 		list_create(newlist, sizeof (dnode_t),
   1166  1.1.1.2  haad 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
   1167  1.1.1.2  haad 	}
   1168  1.1.1.2  haad 
   1169  1.1.1.2  haad 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
   1170  1.1.1.2  haad 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
   1171      1.1  haad 
   1172  1.1.1.3   chs 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
   1173      1.1  haad 	while (dr = list_head(list)) {
   1174  1.1.1.3   chs 		ASSERT0(dr->dr_dbuf->db_level);
   1175      1.1  haad 		list_remove(list, dr);
   1176      1.1  haad 		if (dr->dr_zio)
   1177      1.1  haad 			zio_nowait(dr->dr_zio);
   1178      1.1  haad 	}
   1179      1.1  haad 	/*
   1180      1.1  haad 	 * Free intent log blocks up to this tx.
   1181      1.1  haad 	 */
   1182      1.1  haad 	zil_sync(os->os_zil, tx);
   1183      1.1  haad 	os->os_phys->os_zil_header = os->os_zil_header;
   1184      1.1  haad 	zio_nowait(zio);
   1185      1.1  haad }
   1186      1.1  haad 
   1187  1.1.1.2  haad boolean_t
   1188  1.1.1.2  haad dmu_objset_is_dirty(objset_t *os, uint64_t txg)
   1189  1.1.1.2  haad {
   1190  1.1.1.2  haad 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
   1191  1.1.1.2  haad 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
   1192  1.1.1.2  haad }
   1193  1.1.1.2  haad 
   1194  1.1.1.2  haad static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
   1195  1.1.1.2  haad 
   1196  1.1.1.2  haad void
   1197  1.1.1.2  haad dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
   1198  1.1.1.2  haad {
   1199  1.1.1.2  haad 	used_cbs[ost] = cb;
   1200  1.1.1.2  haad }
   1201  1.1.1.2  haad 
   1202  1.1.1.2  haad boolean_t
   1203  1.1.1.2  haad dmu_objset_userused_enabled(objset_t *os)
   1204  1.1.1.2  haad {
   1205  1.1.1.2  haad 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
   1206  1.1.1.3   chs 	    used_cbs[os->os_phys->os_type] != NULL &&
   1207  1.1.1.3   chs 	    DMU_USERUSED_DNODE(os) != NULL);
   1208  1.1.1.3   chs }
   1209  1.1.1.3   chs 
   1210  1.1.1.3   chs typedef struct userquota_node {
   1211  1.1.1.3   chs 	uint64_t uqn_id;
   1212  1.1.1.3   chs 	int64_t uqn_delta;
   1213  1.1.1.3   chs 	avl_node_t uqn_node;
   1214  1.1.1.3   chs } userquota_node_t;
   1215  1.1.1.3   chs 
   1216  1.1.1.3   chs typedef struct userquota_cache {
   1217  1.1.1.3   chs 	avl_tree_t uqc_user_deltas;
   1218  1.1.1.3   chs 	avl_tree_t uqc_group_deltas;
   1219  1.1.1.3   chs } userquota_cache_t;
   1220  1.1.1.3   chs 
   1221  1.1.1.3   chs static int
   1222  1.1.1.3   chs userquota_compare(const void *l, const void *r)
   1223  1.1.1.3   chs {
   1224  1.1.1.3   chs 	const userquota_node_t *luqn = l;
   1225  1.1.1.3   chs 	const userquota_node_t *ruqn = r;
   1226  1.1.1.3   chs 
   1227  1.1.1.3   chs 	if (luqn->uqn_id < ruqn->uqn_id)
   1228  1.1.1.3   chs 		return (-1);
   1229  1.1.1.3   chs 	if (luqn->uqn_id > ruqn->uqn_id)
   1230  1.1.1.3   chs 		return (1);
   1231  1.1.1.3   chs 	return (0);
   1232  1.1.1.2  haad }
   1233  1.1.1.2  haad 
   1234  1.1.1.2  haad static void
   1235  1.1.1.3   chs do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
   1236  1.1.1.2  haad {
   1237  1.1.1.3   chs 	void *cookie;
   1238  1.1.1.3   chs 	userquota_node_t *uqn;
   1239  1.1.1.2  haad 
   1240  1.1.1.3   chs 	ASSERT(dmu_tx_is_syncing(tx));
   1241  1.1.1.3   chs 
   1242  1.1.1.3   chs 	cookie = NULL;
   1243  1.1.1.3   chs 	while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
   1244  1.1.1.3   chs 	    &cookie)) != NULL) {
   1245  1.1.1.3   chs 		VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT,
   1246  1.1.1.3   chs 		    uqn->uqn_id, uqn->uqn_delta, tx));
   1247  1.1.1.3   chs 		kmem_free(uqn, sizeof (*uqn));
   1248  1.1.1.3   chs 	}
   1249  1.1.1.3   chs 	avl_destroy(&cache->uqc_user_deltas);
   1250  1.1.1.3   chs 
   1251  1.1.1.3   chs 	cookie = NULL;
   1252  1.1.1.3   chs 	while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
   1253  1.1.1.3   chs 	    &cookie)) != NULL) {
   1254  1.1.1.3   chs 		VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT,
   1255  1.1.1.3   chs 		    uqn->uqn_id, uqn->uqn_delta, tx));
   1256  1.1.1.3   chs 		kmem_free(uqn, sizeof (*uqn));
   1257  1.1.1.3   chs 	}
   1258  1.1.1.3   chs 	avl_destroy(&cache->uqc_group_deltas);
   1259  1.1.1.3   chs }
   1260  1.1.1.3   chs 
   1261  1.1.1.3   chs static void
   1262  1.1.1.3   chs userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta)
   1263  1.1.1.3   chs {
   1264  1.1.1.3   chs 	userquota_node_t search = { .uqn_id = id };
   1265  1.1.1.3   chs 	avl_index_t idx;
   1266  1.1.1.3   chs 
   1267  1.1.1.3   chs 	userquota_node_t *uqn = avl_find(avl, &search, &idx);
   1268  1.1.1.3   chs 	if (uqn == NULL) {
   1269  1.1.1.3   chs 		uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
   1270  1.1.1.3   chs 		uqn->uqn_id = id;
   1271  1.1.1.3   chs 		avl_insert(avl, uqn, idx);
   1272  1.1.1.3   chs 	}
   1273  1.1.1.3   chs 	uqn->uqn_delta += delta;
   1274  1.1.1.3   chs }
   1275  1.1.1.3   chs 
   1276  1.1.1.3   chs static void
   1277  1.1.1.3   chs do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
   1278  1.1.1.3   chs     uint64_t user, uint64_t group, boolean_t subtract)
   1279  1.1.1.3   chs {
   1280  1.1.1.3   chs 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
   1281  1.1.1.3   chs 		int64_t delta = DNODE_SIZE + used;
   1282  1.1.1.2  haad 		if (subtract)
   1283  1.1.1.2  haad 			delta = -delta;
   1284  1.1.1.3   chs 
   1285  1.1.1.3   chs 		userquota_update_cache(&cache->uqc_user_deltas, user, delta);
   1286  1.1.1.3   chs 		userquota_update_cache(&cache->uqc_group_deltas, group, delta);
   1287  1.1.1.2  haad 	}
   1288  1.1.1.2  haad }
   1289  1.1.1.2  haad 
   1290  1.1.1.2  haad void
   1291  1.1.1.3   chs dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
   1292  1.1.1.2  haad {
   1293  1.1.1.2  haad 	dnode_t *dn;
   1294  1.1.1.2  haad 	list_t *list = &os->os_synced_dnodes;
   1295  1.1.1.3   chs 	userquota_cache_t cache = { 0 };
   1296  1.1.1.2  haad 
   1297  1.1.1.2  haad 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
   1298  1.1.1.2  haad 
   1299  1.1.1.3   chs 	avl_create(&cache.uqc_user_deltas, userquota_compare,
   1300  1.1.1.3   chs 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
   1301  1.1.1.3   chs 	avl_create(&cache.uqc_group_deltas, userquota_compare,
   1302  1.1.1.3   chs 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
   1303  1.1.1.3   chs 
   1304  1.1.1.2  haad 	while (dn = list_head(list)) {
   1305  1.1.1.3   chs 		int flags;
   1306  1.1.1.2  haad 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
   1307  1.1.1.2  haad 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
   1308  1.1.1.2  haad 		    dn->dn_phys->dn_flags &
   1309  1.1.1.2  haad 		    DNODE_FLAG_USERUSED_ACCOUNTED);
   1310  1.1.1.2  haad 
   1311  1.1.1.2  haad 		/* Allocate the user/groupused objects if necessary. */
   1312  1.1.1.3   chs 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
   1313  1.1.1.3   chs 			VERIFY0(zap_create_claim(os,
   1314  1.1.1.2  haad 			    DMU_USERUSED_OBJECT,
   1315  1.1.1.2  haad 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
   1316  1.1.1.3   chs 			VERIFY0(zap_create_claim(os,
   1317  1.1.1.2  haad 			    DMU_GROUPUSED_OBJECT,
   1318  1.1.1.2  haad 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
   1319  1.1.1.2  haad 		}
   1320  1.1.1.2  haad 
   1321  1.1.1.3   chs 		flags = dn->dn_id_flags;
   1322  1.1.1.3   chs 		ASSERT(flags);
   1323  1.1.1.3   chs 		if (flags & DN_ID_OLD_EXIST)  {
   1324  1.1.1.3   chs 			do_userquota_update(&cache,
   1325  1.1.1.3   chs 			    dn->dn_oldused, dn->dn_oldflags,
   1326  1.1.1.3   chs 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE);
   1327  1.1.1.3   chs 		}
   1328  1.1.1.3   chs 		if (flags & DN_ID_NEW_EXIST) {
   1329  1.1.1.3   chs 			do_userquota_update(&cache,
   1330  1.1.1.3   chs 			    DN_USED_BYTES(dn->dn_phys),
   1331  1.1.1.3   chs 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
   1332  1.1.1.3   chs 			    dn->dn_newgid, B_FALSE);
   1333  1.1.1.3   chs 		}
   1334  1.1.1.2  haad 
   1335  1.1.1.2  haad 		mutex_enter(&dn->dn_mtx);
   1336  1.1.1.3   chs 		dn->dn_oldused = 0;
   1337  1.1.1.3   chs 		dn->dn_oldflags = 0;
   1338  1.1.1.3   chs 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
   1339  1.1.1.3   chs 			dn->dn_olduid = dn->dn_newuid;
   1340  1.1.1.3   chs 			dn->dn_oldgid = dn->dn_newgid;
   1341  1.1.1.3   chs 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
   1342  1.1.1.3   chs 			if (dn->dn_bonuslen == 0)
   1343  1.1.1.3   chs 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
   1344  1.1.1.3   chs 			else
   1345  1.1.1.3   chs 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
   1346  1.1.1.3   chs 		}
   1347  1.1.1.3   chs 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
   1348  1.1.1.2  haad 		mutex_exit(&dn->dn_mtx);
   1349  1.1.1.2  haad 
   1350  1.1.1.2  haad 		list_remove(list, dn);
   1351  1.1.1.2  haad 		dnode_rele(dn, list);
   1352  1.1.1.2  haad 	}
   1353  1.1.1.3   chs 	do_userquota_cacheflush(os, &cache, tx);
   1354  1.1.1.3   chs }
   1355  1.1.1.3   chs 
   1356  1.1.1.3   chs /*
   1357  1.1.1.3   chs  * Returns a pointer to data to find uid/gid from
   1358  1.1.1.3   chs  *
   1359  1.1.1.3   chs  * If a dirty record for transaction group that is syncing can't
   1360  1.1.1.3   chs  * be found then NULL is returned.  In the NULL case it is assumed
   1361  1.1.1.3   chs  * the uid/gid aren't changing.
   1362  1.1.1.3   chs  */
   1363  1.1.1.3   chs static void *
   1364  1.1.1.3   chs dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
   1365  1.1.1.3   chs {
   1366  1.1.1.3   chs 	dbuf_dirty_record_t *dr, **drp;
   1367  1.1.1.3   chs 	void *data;
   1368  1.1.1.3   chs 
   1369  1.1.1.3   chs 	if (db->db_dirtycnt == 0)
   1370  1.1.1.3   chs 		return (db->db.db_data);  /* Nothing is changing */
   1371  1.1.1.3   chs 
   1372  1.1.1.3   chs 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
   1373  1.1.1.3   chs 		if (dr->dr_txg == tx->tx_txg)
   1374  1.1.1.3   chs 			break;
   1375  1.1.1.3   chs 
   1376  1.1.1.3   chs 	if (dr == NULL) {
   1377  1.1.1.3   chs 		data = NULL;
   1378  1.1.1.3   chs 	} else {
   1379  1.1.1.3   chs 		dnode_t *dn;
   1380  1.1.1.3   chs 
   1381  1.1.1.3   chs 		DB_DNODE_ENTER(dr->dr_dbuf);
   1382  1.1.1.3   chs 		dn = DB_DNODE(dr->dr_dbuf);
   1383  1.1.1.3   chs 
   1384  1.1.1.3   chs 		if (dn->dn_bonuslen == 0 &&
   1385  1.1.1.3   chs 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
   1386  1.1.1.3   chs 			data = dr->dt.dl.dr_data->b_data;
   1387  1.1.1.3   chs 		else
   1388  1.1.1.3   chs 			data = dr->dt.dl.dr_data;
   1389  1.1.1.3   chs 
   1390  1.1.1.3   chs 		DB_DNODE_EXIT(dr->dr_dbuf);
   1391  1.1.1.3   chs 	}
   1392  1.1.1.3   chs 
   1393  1.1.1.3   chs 	return (data);
   1394  1.1.1.3   chs }
   1395  1.1.1.3   chs 
   1396  1.1.1.3   chs void
   1397  1.1.1.3   chs dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
   1398  1.1.1.3   chs {
   1399  1.1.1.3   chs 	objset_t *os = dn->dn_objset;
   1400  1.1.1.3   chs 	void *data = NULL;
   1401  1.1.1.3   chs 	dmu_buf_impl_t *db = NULL;
   1402  1.1.1.3   chs 	uint64_t *user = NULL;
   1403  1.1.1.3   chs 	uint64_t *group = NULL;
   1404  1.1.1.3   chs 	int flags = dn->dn_id_flags;
   1405  1.1.1.3   chs 	int error;
   1406  1.1.1.3   chs 	boolean_t have_spill = B_FALSE;
   1407  1.1.1.3   chs 
   1408  1.1.1.3   chs 	if (!dmu_objset_userused_enabled(dn->dn_objset))
   1409  1.1.1.3   chs 		return;
   1410  1.1.1.3   chs 
   1411  1.1.1.3   chs 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
   1412  1.1.1.3   chs 	    DN_ID_CHKED_SPILL)))
   1413  1.1.1.3   chs 		return;
   1414  1.1.1.3   chs 
   1415  1.1.1.3   chs 	if (before && dn->dn_bonuslen != 0)
   1416  1.1.1.3   chs 		data = DN_BONUS(dn->dn_phys);
   1417  1.1.1.3   chs 	else if (!before && dn->dn_bonuslen != 0) {
   1418  1.1.1.3   chs 		if (dn->dn_bonus) {
   1419  1.1.1.3   chs 			db = dn->dn_bonus;
   1420  1.1.1.3   chs 			mutex_enter(&db->db_mtx);
   1421  1.1.1.3   chs 			data = dmu_objset_userquota_find_data(db, tx);
   1422  1.1.1.3   chs 		} else {
   1423  1.1.1.3   chs 			data = DN_BONUS(dn->dn_phys);
   1424  1.1.1.3   chs 		}
   1425  1.1.1.3   chs 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
   1426  1.1.1.3   chs 			int rf = 0;
   1427  1.1.1.3   chs 
   1428  1.1.1.3   chs 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
   1429  1.1.1.3   chs 				rf |= DB_RF_HAVESTRUCT;
   1430  1.1.1.3   chs 			error = dmu_spill_hold_by_dnode(dn,
   1431  1.1.1.3   chs 			    rf | DB_RF_MUST_SUCCEED,
   1432  1.1.1.3   chs 			    FTAG, (dmu_buf_t **)&db);
   1433  1.1.1.3   chs 			ASSERT(error == 0);
   1434  1.1.1.3   chs 			mutex_enter(&db->db_mtx);
   1435  1.1.1.3   chs 			data = (before) ? db->db.db_data :
   1436  1.1.1.3   chs 			    dmu_objset_userquota_find_data(db, tx);
   1437  1.1.1.3   chs 			have_spill = B_TRUE;
   1438  1.1.1.3   chs 	} else {
   1439  1.1.1.3   chs 		mutex_enter(&dn->dn_mtx);
   1440  1.1.1.3   chs 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
   1441  1.1.1.3   chs 		mutex_exit(&dn->dn_mtx);
   1442  1.1.1.3   chs 		return;
   1443  1.1.1.3   chs 	}
   1444  1.1.1.3   chs 
   1445  1.1.1.3   chs 	if (before) {
   1446  1.1.1.3   chs 		ASSERT(data);
   1447  1.1.1.3   chs 		user = &dn->dn_olduid;
   1448  1.1.1.3   chs 		group = &dn->dn_oldgid;
   1449  1.1.1.3   chs 	} else if (data) {
   1450  1.1.1.3   chs 		user = &dn->dn_newuid;
   1451  1.1.1.3   chs 		group = &dn->dn_newgid;
   1452  1.1.1.3   chs 	}
   1453  1.1.1.3   chs 
   1454  1.1.1.3   chs 	/*
   1455  1.1.1.3   chs 	 * Must always call the callback in case the object
   1456  1.1.1.3   chs 	 * type has changed and that type isn't an object type to track
   1457  1.1.1.3   chs 	 */
   1458  1.1.1.3   chs 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
   1459  1.1.1.3   chs 	    user, group);
   1460  1.1.1.3   chs 
   1461  1.1.1.3   chs 	/*
   1462  1.1.1.3   chs 	 * Preserve existing uid/gid when the callback can't determine
   1463  1.1.1.3   chs 	 * what the new uid/gid are and the callback returned EEXIST.
   1464  1.1.1.3   chs 	 * The EEXIST error tells us to just use the existing uid/gid.
   1465  1.1.1.3   chs 	 * If we don't know what the old values are then just assign
   1466  1.1.1.3   chs 	 * them to 0, since that is a new file  being created.
   1467  1.1.1.3   chs 	 */
   1468  1.1.1.3   chs 	if (!before && data == NULL && error == EEXIST) {
   1469  1.1.1.3   chs 		if (flags & DN_ID_OLD_EXIST) {
   1470  1.1.1.3   chs 			dn->dn_newuid = dn->dn_olduid;
   1471  1.1.1.3   chs 			dn->dn_newgid = dn->dn_oldgid;
   1472  1.1.1.3   chs 		} else {
   1473  1.1.1.3   chs 			dn->dn_newuid = 0;
   1474  1.1.1.3   chs 			dn->dn_newgid = 0;
   1475  1.1.1.3   chs 		}
   1476  1.1.1.3   chs 		error = 0;
   1477  1.1.1.3   chs 	}
   1478  1.1.1.3   chs 
   1479  1.1.1.3   chs 	if (db)
   1480  1.1.1.3   chs 		mutex_exit(&db->db_mtx);
   1481  1.1.1.3   chs 
   1482  1.1.1.3   chs 	mutex_enter(&dn->dn_mtx);
   1483  1.1.1.3   chs 	if (error == 0 && before)
   1484  1.1.1.3   chs 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
   1485  1.1.1.3   chs 	if (error == 0 && !before)
   1486  1.1.1.3   chs 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
   1487  1.1.1.3   chs 
   1488  1.1.1.3   chs 	if (have_spill) {
   1489  1.1.1.3   chs 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
   1490  1.1.1.3   chs 	} else {
   1491  1.1.1.3   chs 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
   1492  1.1.1.3   chs 	}
   1493  1.1.1.3   chs 	mutex_exit(&dn->dn_mtx);
   1494  1.1.1.3   chs 	if (have_spill)
   1495  1.1.1.3   chs 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
   1496  1.1.1.2  haad }
   1497  1.1.1.2  haad 
   1498  1.1.1.2  haad boolean_t
   1499  1.1.1.2  haad dmu_objset_userspace_present(objset_t *os)
   1500  1.1.1.2  haad {
   1501  1.1.1.2  haad 	return (os->os_phys->os_flags &
   1502  1.1.1.2  haad 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
   1503  1.1.1.2  haad }
   1504  1.1.1.2  haad 
   1505  1.1.1.2  haad int
   1506  1.1.1.2  haad dmu_objset_userspace_upgrade(objset_t *os)
   1507  1.1.1.2  haad {
   1508  1.1.1.2  haad 	uint64_t obj;
   1509  1.1.1.2  haad 	int err = 0;
   1510  1.1.1.2  haad 
   1511  1.1.1.2  haad 	if (dmu_objset_userspace_present(os))
   1512  1.1.1.2  haad 		return (0);
   1513  1.1.1.2  haad 	if (!dmu_objset_userused_enabled(os))
   1514  1.1.1.3   chs 		return (SET_ERROR(ENOTSUP));
   1515  1.1.1.2  haad 	if (dmu_objset_is_snapshot(os))
   1516  1.1.1.3   chs 		return (SET_ERROR(EINVAL));
   1517  1.1.1.2  haad 
   1518  1.1.1.2  haad 	/*
   1519  1.1.1.2  haad 	 * We simply need to mark every object dirty, so that it will be
   1520  1.1.1.2  haad 	 * synced out and now accounted.  If this is called
   1521  1.1.1.2  haad 	 * concurrently, or if we already did some work before crashing,
   1522  1.1.1.2  haad 	 * that's fine, since we track each object's accounted state
   1523  1.1.1.2  haad 	 * independently.
   1524  1.1.1.2  haad 	 */
   1525  1.1.1.2  haad 
   1526  1.1.1.2  haad 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
   1527  1.1.1.2  haad 		dmu_tx_t *tx;
   1528  1.1.1.2  haad 		dmu_buf_t *db;
   1529  1.1.1.2  haad 		int objerr;
   1530  1.1.1.2  haad 
   1531  1.1.1.2  haad 		if (issig(JUSTLOOKING) && issig(FORREAL))
   1532  1.1.1.3   chs 			return (SET_ERROR(EINTR));
   1533  1.1.1.2  haad 
   1534  1.1.1.2  haad 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
   1535  1.1.1.3   chs 		if (objerr != 0)
   1536  1.1.1.2  haad 			continue;
   1537  1.1.1.2  haad 		tx = dmu_tx_create(os);
   1538  1.1.1.2  haad 		dmu_tx_hold_bonus(tx, obj);
   1539  1.1.1.2  haad 		objerr = dmu_tx_assign(tx, TXG_WAIT);
   1540  1.1.1.3   chs 		if (objerr != 0) {
   1541  1.1.1.2  haad 			dmu_tx_abort(tx);
   1542  1.1.1.2  haad 			continue;
   1543  1.1.1.2  haad 		}
   1544  1.1.1.2  haad 		dmu_buf_will_dirty(db, tx);
   1545  1.1.1.2  haad 		dmu_buf_rele(db, FTAG);
   1546  1.1.1.2  haad 		dmu_tx_commit(tx);
   1547  1.1.1.2  haad 	}
   1548  1.1.1.2  haad 
   1549  1.1.1.2  haad 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
   1550  1.1.1.2  haad 	txg_wait_synced(dmu_objset_pool(os), 0);
   1551  1.1.1.2  haad 	return (0);
   1552  1.1.1.2  haad }
   1553  1.1.1.2  haad 
   1554      1.1  haad void
   1555      1.1  haad dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
   1556      1.1  haad     uint64_t *usedobjsp, uint64_t *availobjsp)
   1557      1.1  haad {
   1558  1.1.1.2  haad 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
   1559      1.1  haad 	    usedobjsp, availobjsp);
   1560      1.1  haad }
   1561      1.1  haad 
   1562      1.1  haad uint64_t
   1563      1.1  haad dmu_objset_fsid_guid(objset_t *os)
   1564      1.1  haad {
   1565  1.1.1.2  haad 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
   1566      1.1  haad }
   1567      1.1  haad 
   1568      1.1  haad void
   1569      1.1  haad dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
   1570      1.1  haad {
   1571  1.1.1.2  haad 	stat->dds_type = os->os_phys->os_type;
   1572  1.1.1.2  haad 	if (os->os_dsl_dataset)
   1573  1.1.1.2  haad 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
   1574      1.1  haad }
   1575      1.1  haad 
   1576      1.1  haad void
   1577      1.1  haad dmu_objset_stats(objset_t *os, nvlist_t *nv)
   1578      1.1  haad {
   1579  1.1.1.2  haad 	ASSERT(os->os_dsl_dataset ||
   1580  1.1.1.2  haad 	    os->os_phys->os_type == DMU_OST_META);
   1581      1.1  haad 
   1582  1.1.1.2  haad 	if (os->os_dsl_dataset != NULL)
   1583  1.1.1.2  haad 		dsl_dataset_stats(os->os_dsl_dataset, nv);
   1584      1.1  haad 
   1585      1.1  haad 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
   1586  1.1.1.2  haad 	    os->os_phys->os_type);
   1587  1.1.1.2  haad 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
   1588  1.1.1.2  haad 	    dmu_objset_userspace_present(os));
   1589      1.1  haad }
   1590      1.1  haad 
   1591      1.1  haad int
   1592      1.1  haad dmu_objset_is_snapshot(objset_t *os)
   1593      1.1  haad {
   1594  1.1.1.2  haad 	if (os->os_dsl_dataset != NULL)
   1595  1.1.1.3   chs 		return (os->os_dsl_dataset->ds_is_snapshot);
   1596      1.1  haad 	else
   1597      1.1  haad 		return (B_FALSE);
   1598      1.1  haad }
   1599      1.1  haad 
   1600      1.1  haad int
   1601      1.1  haad dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
   1602      1.1  haad     boolean_t *conflict)
   1603      1.1  haad {
   1604  1.1.1.2  haad 	dsl_dataset_t *ds = os->os_dsl_dataset;
   1605      1.1  haad 	uint64_t ignored;
   1606      1.1  haad 
   1607  1.1.1.3   chs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
   1608  1.1.1.3   chs 		return (SET_ERROR(ENOENT));
   1609      1.1  haad 
   1610      1.1  haad 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
   1611  1.1.1.3   chs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
   1612  1.1.1.3   chs 	    MT_FIRST, real, maxlen, conflict));
   1613      1.1  haad }
   1614      1.1  haad 
   1615      1.1  haad int
   1616      1.1  haad dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
   1617      1.1  haad     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
   1618      1.1  haad {
   1619  1.1.1.2  haad 	dsl_dataset_t *ds = os->os_dsl_dataset;
   1620      1.1  haad 	zap_cursor_t cursor;
   1621      1.1  haad 	zap_attribute_t attr;
   1622      1.1  haad 
   1623  1.1.1.3   chs 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
   1624  1.1.1.3   chs 
   1625  1.1.1.3   chs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
   1626  1.1.1.3   chs 		return (SET_ERROR(ENOENT));
   1627      1.1  haad 
   1628      1.1  haad 	zap_cursor_init_serialized(&cursor,
   1629      1.1  haad 	    ds->ds_dir->dd_pool->dp_meta_objset,
   1630  1.1.1.3   chs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
   1631      1.1  haad 
   1632      1.1  haad 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
   1633      1.1  haad 		zap_cursor_fini(&cursor);
   1634  1.1.1.3   chs 		return (SET_ERROR(ENOENT));
   1635      1.1  haad 	}
   1636      1.1  haad 
   1637      1.1  haad 	if (strlen(attr.za_name) + 1 > namelen) {
   1638      1.1  haad 		zap_cursor_fini(&cursor);
   1639  1.1.1.3   chs 		return (SET_ERROR(ENAMETOOLONG));
   1640      1.1  haad 	}
   1641      1.1  haad 
   1642      1.1  haad 	(void) strcpy(name, attr.za_name);
   1643      1.1  haad 	if (idp)
   1644      1.1  haad 		*idp = attr.za_first_integer;
   1645      1.1  haad 	if (case_conflict)
   1646      1.1  haad 		*case_conflict = attr.za_normalization_conflict;
   1647      1.1  haad 	zap_cursor_advance(&cursor);
   1648      1.1  haad 	*offp = zap_cursor_serialize(&cursor);
   1649      1.1  haad 	zap_cursor_fini(&cursor);
   1650      1.1  haad 
   1651      1.1  haad 	return (0);
   1652      1.1  haad }
   1653      1.1  haad 
   1654      1.1  haad int
   1655      1.1  haad dmu_dir_list_next(objset_t *os, int namelen, char *name,
   1656      1.1  haad     uint64_t *idp, uint64_t *offp)
   1657      1.1  haad {
   1658  1.1.1.2  haad 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
   1659      1.1  haad 	zap_cursor_t cursor;
   1660      1.1  haad 	zap_attribute_t attr;
   1661      1.1  haad 
   1662      1.1  haad 	/* there is no next dir on a snapshot! */
   1663  1.1.1.2  haad 	if (os->os_dsl_dataset->ds_object !=
   1664  1.1.1.3   chs 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
   1665  1.1.1.3   chs 		return (SET_ERROR(ENOENT));
   1666      1.1  haad 
   1667      1.1  haad 	zap_cursor_init_serialized(&cursor,
   1668      1.1  haad 	    dd->dd_pool->dp_meta_objset,
   1669  1.1.1.3   chs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
   1670      1.1  haad 
   1671      1.1  haad 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
   1672      1.1  haad 		zap_cursor_fini(&cursor);
   1673  1.1.1.3   chs 		return (SET_ERROR(ENOENT));
   1674      1.1  haad 	}
   1675      1.1  haad 
   1676      1.1  haad 	if (strlen(attr.za_name) + 1 > namelen) {
   1677      1.1  haad 		zap_cursor_fini(&cursor);
   1678  1.1.1.3   chs 		return (SET_ERROR(ENAMETOOLONG));
   1679      1.1  haad 	}
   1680      1.1  haad 
   1681      1.1  haad 	(void) strcpy(name, attr.za_name);
   1682      1.1  haad 	if (idp)
   1683      1.1  haad 		*idp = attr.za_first_integer;
   1684      1.1  haad 	zap_cursor_advance(&cursor);
   1685      1.1  haad 	*offp = zap_cursor_serialize(&cursor);
   1686      1.1  haad 	zap_cursor_fini(&cursor);
   1687      1.1  haad 
   1688      1.1  haad 	return (0);
   1689      1.1  haad }
   1690      1.1  haad 
   1691  1.1.1.3   chs typedef struct dmu_objset_find_ctx {
   1692  1.1.1.3   chs 	taskq_t		*dc_tq;
   1693  1.1.1.3   chs 	dsl_pool_t	*dc_dp;
   1694  1.1.1.3   chs 	uint64_t	dc_ddobj;
   1695  1.1.1.3   chs 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
   1696  1.1.1.3   chs 	void		*dc_arg;
   1697  1.1.1.3   chs 	int		dc_flags;
   1698  1.1.1.3   chs 	kmutex_t	*dc_error_lock;
   1699  1.1.1.3   chs 	int		*dc_error;
   1700  1.1.1.3   chs } dmu_objset_find_ctx_t;
   1701      1.1  haad 
   1702  1.1.1.3   chs static void
   1703  1.1.1.3   chs dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
   1704      1.1  haad {
   1705  1.1.1.3   chs 	dsl_pool_t *dp = dcp->dc_dp;
   1706  1.1.1.3   chs 	dmu_objset_find_ctx_t *child_dcp;
   1707  1.1.1.3   chs 	dsl_dir_t *dd;
   1708  1.1.1.3   chs 	dsl_dataset_t *ds;
   1709  1.1.1.3   chs 	zap_cursor_t zc;
   1710  1.1.1.3   chs 	zap_attribute_t *attr;
   1711  1.1.1.3   chs 	uint64_t thisobj;
   1712  1.1.1.3   chs 	int err = 0;
   1713  1.1.1.3   chs 
   1714  1.1.1.3   chs 	/* don't process if there already was an error */
   1715  1.1.1.3   chs 	if (*dcp->dc_error != 0)
   1716  1.1.1.3   chs 		goto out;
   1717  1.1.1.3   chs 
   1718  1.1.1.3   chs 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
   1719  1.1.1.3   chs 	if (err != 0)
   1720  1.1.1.3   chs 		goto out;
   1721  1.1.1.3   chs 
   1722  1.1.1.3   chs 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
   1723  1.1.1.3   chs 	if (dd->dd_myname[0] == '$') {
   1724  1.1.1.3   chs 		dsl_dir_rele(dd, FTAG);
   1725  1.1.1.3   chs 		goto out;
   1726  1.1.1.3   chs 	}
   1727  1.1.1.3   chs 
   1728  1.1.1.3   chs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
   1729  1.1.1.3   chs 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
   1730  1.1.1.3   chs 
   1731  1.1.1.3   chs 	/*
   1732  1.1.1.3   chs 	 * Iterate over all children.
   1733  1.1.1.3   chs 	 */
   1734  1.1.1.3   chs 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
   1735  1.1.1.3   chs 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
   1736  1.1.1.3   chs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
   1737  1.1.1.3   chs 		    zap_cursor_retrieve(&zc, attr) == 0;
   1738  1.1.1.3   chs 		    (void) zap_cursor_advance(&zc)) {
   1739  1.1.1.3   chs 			ASSERT3U(attr->za_integer_length, ==,
   1740  1.1.1.3   chs 			    sizeof (uint64_t));
   1741  1.1.1.3   chs 			ASSERT3U(attr->za_num_integers, ==, 1);
   1742  1.1.1.3   chs 
   1743  1.1.1.3   chs 			child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
   1744  1.1.1.3   chs 			*child_dcp = *dcp;
   1745  1.1.1.3   chs 			child_dcp->dc_ddobj = attr->za_first_integer;
   1746  1.1.1.3   chs 			if (dcp->dc_tq != NULL)
   1747  1.1.1.3   chs 				(void) taskq_dispatch(dcp->dc_tq,
   1748  1.1.1.3   chs 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
   1749  1.1.1.3   chs 			else
   1750  1.1.1.3   chs 				dmu_objset_find_dp_impl(child_dcp);
   1751  1.1.1.3   chs 		}
   1752  1.1.1.3   chs 		zap_cursor_fini(&zc);
   1753  1.1.1.3   chs 	}
   1754  1.1.1.3   chs 
   1755  1.1.1.3   chs 	/*
   1756  1.1.1.3   chs 	 * Iterate over all snapshots.
   1757  1.1.1.3   chs 	 */
   1758  1.1.1.3   chs 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
   1759  1.1.1.3   chs 		dsl_dataset_t *ds;
   1760  1.1.1.3   chs 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
   1761  1.1.1.3   chs 
   1762  1.1.1.3   chs 		if (err == 0) {
   1763  1.1.1.3   chs 			uint64_t snapobj;
   1764  1.1.1.3   chs 
   1765  1.1.1.3   chs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
   1766  1.1.1.3   chs 			dsl_dataset_rele(ds, FTAG);
   1767  1.1.1.3   chs 
   1768  1.1.1.3   chs 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
   1769  1.1.1.3   chs 			    zap_cursor_retrieve(&zc, attr) == 0;
   1770  1.1.1.3   chs 			    (void) zap_cursor_advance(&zc)) {
   1771  1.1.1.3   chs 				ASSERT3U(attr->za_integer_length, ==,
   1772  1.1.1.3   chs 				    sizeof (uint64_t));
   1773  1.1.1.3   chs 				ASSERT3U(attr->za_num_integers, ==, 1);
   1774  1.1.1.3   chs 
   1775  1.1.1.3   chs 				err = dsl_dataset_hold_obj(dp,
   1776  1.1.1.3   chs 				    attr->za_first_integer, FTAG, &ds);
   1777  1.1.1.3   chs 				if (err != 0)
   1778  1.1.1.3   chs 					break;
   1779  1.1.1.3   chs 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
   1780  1.1.1.3   chs 				dsl_dataset_rele(ds, FTAG);
   1781  1.1.1.3   chs 				if (err != 0)
   1782  1.1.1.3   chs 					break;
   1783  1.1.1.3   chs 			}
   1784  1.1.1.3   chs 			zap_cursor_fini(&zc);
   1785  1.1.1.3   chs 		}
   1786  1.1.1.3   chs 	}
   1787  1.1.1.3   chs 
   1788  1.1.1.3   chs 	dsl_dir_rele(dd, FTAG);
   1789  1.1.1.3   chs 	kmem_free(attr, sizeof (zap_attribute_t));
   1790  1.1.1.3   chs 
   1791  1.1.1.3   chs 	if (err != 0)
   1792  1.1.1.3   chs 		goto out;
   1793  1.1.1.3   chs 
   1794  1.1.1.3   chs 	/*
   1795  1.1.1.3   chs 	 * Apply to self.
   1796  1.1.1.3   chs 	 */
   1797  1.1.1.3   chs 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
   1798  1.1.1.3   chs 	if (err != 0)
   1799  1.1.1.3   chs 		goto out;
   1800  1.1.1.3   chs 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
   1801  1.1.1.3   chs 	dsl_dataset_rele(ds, FTAG);
   1802  1.1.1.3   chs 
   1803  1.1.1.3   chs out:
   1804  1.1.1.3   chs 	if (err != 0) {
   1805  1.1.1.3   chs 		mutex_enter(dcp->dc_error_lock);
   1806  1.1.1.3   chs 		/* only keep first error */
   1807  1.1.1.3   chs 		if (*dcp->dc_error == 0)
   1808  1.1.1.3   chs 			*dcp->dc_error = err;
   1809  1.1.1.3   chs 		mutex_exit(dcp->dc_error_lock);
   1810  1.1.1.3   chs 	}
   1811  1.1.1.3   chs 
   1812  1.1.1.3   chs 	kmem_free(dcp, sizeof (*dcp));
   1813  1.1.1.3   chs }
   1814  1.1.1.3   chs 
   1815  1.1.1.3   chs static void
   1816  1.1.1.3   chs dmu_objset_find_dp_cb(void *arg)
   1817  1.1.1.3   chs {
   1818  1.1.1.3   chs 	dmu_objset_find_ctx_t *dcp = arg;
   1819  1.1.1.3   chs 	dsl_pool_t *dp = dcp->dc_dp;
   1820  1.1.1.3   chs 
   1821  1.1.1.3   chs 	/*
   1822  1.1.1.3   chs 	 * We need to get a pool_config_lock here, as there are several
   1823  1.1.1.3   chs 	 * asssert(pool_config_held) down the stack. Getting a lock via
   1824  1.1.1.3   chs 	 * dsl_pool_config_enter is risky, as it might be stalled by a
   1825  1.1.1.3   chs 	 * pending writer. This would deadlock, as the write lock can
   1826  1.1.1.3   chs 	 * only be granted when our parent thread gives up the lock.
   1827  1.1.1.3   chs 	 * The _prio interface gives us priority over a pending writer.
   1828  1.1.1.3   chs 	 */
   1829  1.1.1.3   chs 	dsl_pool_config_enter_prio(dp, FTAG);
   1830  1.1.1.3   chs 
   1831  1.1.1.3   chs 	dmu_objset_find_dp_impl(dcp);
   1832  1.1.1.3   chs 
   1833  1.1.1.3   chs 	dsl_pool_config_exit(dp, FTAG);
   1834      1.1  haad }
   1835      1.1  haad 
   1836      1.1  haad /*
   1837  1.1.1.3   chs  * Find objsets under and including ddobj, call func(ds) on each.
   1838  1.1.1.3   chs  * The order for the enumeration is completely undefined.
   1839  1.1.1.3   chs  * func is called with dsl_pool_config held.
   1840      1.1  haad  */
   1841      1.1  haad int
   1842  1.1.1.3   chs dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
   1843  1.1.1.3   chs     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
   1844      1.1  haad {
   1845  1.1.1.3   chs 	int error = 0;
   1846  1.1.1.3   chs 	taskq_t *tq = NULL;
   1847  1.1.1.3   chs 	int ntasks;
   1848  1.1.1.3   chs 	dmu_objset_find_ctx_t *dcp;
   1849  1.1.1.3   chs 	kmutex_t err_lock;
   1850  1.1.1.3   chs 
   1851  1.1.1.3   chs 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
   1852  1.1.1.3   chs 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
   1853  1.1.1.3   chs 	dcp->dc_tq = NULL;
   1854  1.1.1.3   chs 	dcp->dc_dp = dp;
   1855  1.1.1.3   chs 	dcp->dc_ddobj = ddobj;
   1856  1.1.1.3   chs 	dcp->dc_func = func;
   1857  1.1.1.3   chs 	dcp->dc_arg = arg;
   1858  1.1.1.3   chs 	dcp->dc_flags = flags;
   1859  1.1.1.3   chs 	dcp->dc_error_lock = &err_lock;
   1860  1.1.1.3   chs 	dcp->dc_error = &error;
   1861  1.1.1.3   chs 
   1862  1.1.1.3   chs 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
   1863  1.1.1.3   chs 		/*
   1864  1.1.1.3   chs 		 * In case a write lock is held we can't make use of
   1865  1.1.1.3   chs 		 * parallelism, as down the stack of the worker threads
   1866  1.1.1.3   chs 		 * the lock is asserted via dsl_pool_config_held.
   1867  1.1.1.3   chs 		 * In case of a read lock this is solved by getting a read
   1868  1.1.1.3   chs 		 * lock in each worker thread, which isn't possible in case
   1869  1.1.1.3   chs 		 * of a writer lock. So we fall back to the synchronous path
   1870  1.1.1.3   chs 		 * here.
   1871  1.1.1.3   chs 		 * In the future it might be possible to get some magic into
   1872  1.1.1.3   chs 		 * dsl_pool_config_held in a way that it returns true for
   1873  1.1.1.3   chs 		 * the worker threads so that a single lock held from this
   1874  1.1.1.3   chs 		 * thread suffices. For now, stay single threaded.
   1875  1.1.1.3   chs 		 */
   1876  1.1.1.3   chs 		dmu_objset_find_dp_impl(dcp);
   1877  1.1.1.3   chs 		mutex_destroy(&err_lock);
   1878  1.1.1.3   chs 
   1879  1.1.1.3   chs 		return (error);
   1880  1.1.1.3   chs 	}
   1881  1.1.1.3   chs 
   1882  1.1.1.3   chs 	ntasks = dmu_find_threads;
   1883  1.1.1.3   chs 	if (ntasks == 0)
   1884  1.1.1.3   chs 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
   1885  1.1.1.3   chs 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
   1886  1.1.1.3   chs 	    INT_MAX, 0);
   1887  1.1.1.3   chs 	if (tq == NULL) {
   1888  1.1.1.3   chs 		kmem_free(dcp, sizeof (*dcp));
   1889  1.1.1.3   chs 		mutex_destroy(&err_lock);
   1890  1.1.1.3   chs 
   1891  1.1.1.3   chs 		return (SET_ERROR(ENOMEM));
   1892  1.1.1.3   chs 	}
   1893  1.1.1.3   chs 	dcp->dc_tq = tq;
   1894  1.1.1.3   chs 
   1895  1.1.1.3   chs 	/* dcp will be freed by task */
   1896  1.1.1.3   chs 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
   1897  1.1.1.3   chs 
   1898  1.1.1.3   chs 	/*
   1899  1.1.1.3   chs 	 * PORTING: this code relies on the property of taskq_wait to wait
   1900  1.1.1.3   chs 	 * until no more tasks are queued and no more tasks are active. As
   1901  1.1.1.3   chs 	 * we always queue new tasks from within other tasks, task_wait
   1902  1.1.1.3   chs 	 * reliably waits for the full recursion to finish, even though we
   1903  1.1.1.3   chs 	 * enqueue new tasks after taskq_wait has been called.
   1904  1.1.1.3   chs 	 * On platforms other than illumos, taskq_wait may not have this
   1905  1.1.1.3   chs 	 * property.
   1906  1.1.1.3   chs 	 */
   1907  1.1.1.3   chs 	taskq_wait(tq);
   1908  1.1.1.3   chs 	taskq_destroy(tq);
   1909  1.1.1.3   chs 	mutex_destroy(&err_lock);
   1910  1.1.1.3   chs 
   1911  1.1.1.3   chs 	return (error);
   1912      1.1  haad }
   1913      1.1  haad 
   1914      1.1  haad /*
   1915  1.1.1.3   chs  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
   1916  1.1.1.3   chs  * The dp_config_rwlock must not be held when this is called, and it
   1917  1.1.1.3   chs  * will not be held when the callback is called.
   1918  1.1.1.3   chs  * Therefore this function should only be used when the pool is not changing
   1919  1.1.1.3   chs  * (e.g. in syncing context), or the callback can deal with the possible races.
   1920      1.1  haad  */
   1921  1.1.1.3   chs static int
   1922  1.1.1.3   chs dmu_objset_find_impl(spa_t *spa, const char *name,
   1923  1.1.1.3   chs     int func(const char *, void *), void *arg, int flags)
   1924      1.1  haad {
   1925      1.1  haad 	dsl_dir_t *dd;
   1926  1.1.1.3   chs 	dsl_pool_t *dp = spa_get_dsl(spa);
   1927      1.1  haad 	dsl_dataset_t *ds;
   1928      1.1  haad 	zap_cursor_t zc;
   1929      1.1  haad 	zap_attribute_t *attr;
   1930      1.1  haad 	char *child;
   1931      1.1  haad 	uint64_t thisobj;
   1932      1.1  haad 	int err;
   1933      1.1  haad 
   1934  1.1.1.3   chs 	dsl_pool_config_enter(dp, FTAG);
   1935  1.1.1.3   chs 
   1936  1.1.1.3   chs 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
   1937  1.1.1.3   chs 	if (err != 0) {
   1938  1.1.1.3   chs 		dsl_pool_config_exit(dp, FTAG);
   1939      1.1  haad 		return (err);
   1940  1.1.1.3   chs 	}
   1941      1.1  haad 
   1942      1.1  haad 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
   1943      1.1  haad 	if (dd->dd_myname[0] == '$') {
   1944  1.1.1.3   chs 		dsl_dir_rele(dd, FTAG);
   1945  1.1.1.3   chs 		dsl_pool_config_exit(dp, FTAG);
   1946      1.1  haad 		return (0);
   1947      1.1  haad 	}
   1948      1.1  haad 
   1949  1.1.1.3   chs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
   1950      1.1  haad 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
   1951      1.1  haad 
   1952      1.1  haad 	/*
   1953      1.1  haad 	 * Iterate over all children.
   1954      1.1  haad 	 */
   1955      1.1  haad 	if (flags & DS_FIND_CHILDREN) {
   1956      1.1  haad 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
   1957  1.1.1.3   chs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
   1958      1.1  haad 		    zap_cursor_retrieve(&zc, attr) == 0;
   1959      1.1  haad 		    (void) zap_cursor_advance(&zc)) {
   1960  1.1.1.3   chs 			ASSERT3U(attr->za_integer_length, ==,
   1961  1.1.1.3   chs 			    sizeof (uint64_t));
   1962  1.1.1.3   chs 			ASSERT3U(attr->za_num_integers, ==, 1);
   1963      1.1  haad 
   1964  1.1.1.2  haad 			child = kmem_asprintf("%s/%s", name, attr->za_name);
   1965  1.1.1.3   chs 			dsl_pool_config_exit(dp, FTAG);
   1966  1.1.1.3   chs 			err = dmu_objset_find_impl(spa, child,
   1967  1.1.1.3   chs 			    func, arg, flags);
   1968  1.1.1.3   chs 			dsl_pool_config_enter(dp, FTAG);
   1969  1.1.1.2  haad 			strfree(child);
   1970  1.1.1.3   chs 			if (err != 0)
   1971      1.1  haad 				break;
   1972      1.1  haad 		}
   1973      1.1  haad 		zap_cursor_fini(&zc);
   1974      1.1  haad 
   1975  1.1.1.3   chs 		if (err != 0) {
   1976  1.1.1.3   chs 			dsl_dir_rele(dd, FTAG);
   1977  1.1.1.3   chs 			dsl_pool_config_exit(dp, FTAG);
   1978      1.1  haad 			kmem_free(attr, sizeof (zap_attribute_t));
   1979      1.1  haad 			return (err);
   1980      1.1  haad 		}
   1981      1.1  haad 	}
   1982      1.1  haad 
   1983      1.1  haad 	/*
   1984      1.1  haad 	 * Iterate over all snapshots.
   1985      1.1  haad 	 */
   1986      1.1  haad 	if (flags & DS_FIND_SNAPSHOTS) {
   1987      1.1  haad 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
   1988      1.1  haad 
   1989      1.1  haad 		if (err == 0) {
   1990  1.1.1.3   chs 			uint64_t snapobj;
   1991  1.1.1.3   chs 
   1992  1.1.1.3   chs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
   1993      1.1  haad 			dsl_dataset_rele(ds, FTAG);
   1994      1.1  haad 
   1995      1.1  haad 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
   1996      1.1  haad 			    zap_cursor_retrieve(&zc, attr) == 0;
   1997      1.1  haad 			    (void) zap_cursor_advance(&zc)) {
   1998  1.1.1.3   chs 				ASSERT3U(attr->za_integer_length, ==,
   1999      1.1  haad 				    sizeof (uint64_t));
   2000  1.1.1.3   chs 				ASSERT3U(attr->za_num_integers, ==, 1);
   2001      1.1  haad 
   2002  1.1.1.2  haad 				child = kmem_asprintf("%s@%s",
   2003  1.1.1.2  haad 				    name, attr->za_name);
   2004  1.1.1.3   chs 				dsl_pool_config_exit(dp, FTAG);
   2005  1.1.1.3   chs 				err = func(child, arg);
   2006  1.1.1.3   chs 				dsl_pool_config_enter(dp, FTAG);
   2007  1.1.1.2  haad 				strfree(child);
   2008  1.1.1.3   chs 				if (err != 0)
   2009      1.1  haad 					break;
   2010      1.1  haad 			}
   2011      1.1  haad 			zap_cursor_fini(&zc);
   2012      1.1  haad 		}
   2013      1.1  haad 	}
   2014      1.1  haad 
   2015  1.1.1.3   chs 	dsl_dir_rele(dd, FTAG);
   2016      1.1  haad 	kmem_free(attr, sizeof (zap_attribute_t));
   2017  1.1.1.3   chs 	dsl_pool_config_exit(dp, FTAG);
   2018      1.1  haad 
   2019  1.1.1.3   chs 	if (err != 0)
   2020      1.1  haad 		return (err);
   2021      1.1  haad 
   2022  1.1.1.3   chs 	/* Apply to self. */
   2023  1.1.1.3   chs 	return (func(name, arg));
   2024      1.1  haad }
   2025      1.1  haad 
   2026  1.1.1.3   chs /*
   2027  1.1.1.3   chs  * See comment above dmu_objset_find_impl().
   2028  1.1.1.3   chs  */
   2029      1.1  haad int
   2030  1.1.1.3   chs dmu_objset_find(char *name, int func(const char *, void *), void *arg,
   2031  1.1.1.3   chs     int flags)
   2032      1.1  haad {
   2033  1.1.1.3   chs 	spa_t *spa;
   2034  1.1.1.3   chs 	int error;
   2035      1.1  haad 
   2036  1.1.1.3   chs 	error = spa_open(name, &spa, FTAG);
   2037  1.1.1.3   chs 	if (error != 0)
   2038  1.1.1.3   chs 		return (error);
   2039  1.1.1.3   chs 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
   2040  1.1.1.3   chs 	spa_close(spa, FTAG);
   2041  1.1.1.3   chs 	return (error);
   2042      1.1  haad }
   2043      1.1  haad 
   2044      1.1  haad void
   2045      1.1  haad dmu_objset_set_user(objset_t *os, void *user_ptr)
   2046      1.1  haad {
   2047  1.1.1.2  haad 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
   2048  1.1.1.2  haad 	os->os_user_ptr = user_ptr;
   2049      1.1  haad }
   2050      1.1  haad 
   2051      1.1  haad void *
   2052      1.1  haad dmu_objset_get_user(objset_t *os)
   2053      1.1  haad {
   2054  1.1.1.2  haad 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
   2055  1.1.1.2  haad 	return (os->os_user_ptr);
   2056      1.1  haad }
   2057  1.1.1.3   chs 
   2058  1.1.1.3   chs /*
   2059  1.1.1.3   chs  * Determine name of filesystem, given name of snapshot.
   2060  1.1.1.3   chs  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
   2061  1.1.1.3   chs  */
   2062  1.1.1.3   chs int
   2063  1.1.1.3   chs dmu_fsname(const char *snapname, char *buf)
   2064  1.1.1.3   chs {
   2065  1.1.1.3   chs 	char *atp = strchr(snapname, '@');
   2066  1.1.1.3   chs 	if (atp == NULL)
   2067  1.1.1.3   chs 		return (SET_ERROR(EINVAL));
   2068  1.1.1.3   chs 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
   2069  1.1.1.3   chs 		return (SET_ERROR(ENAMETOOLONG));
   2070  1.1.1.3   chs 	(void) strlcpy(buf, snapname, atp - snapname + 1);
   2071  1.1.1.3   chs 	return (0);
   2072  1.1.1.3   chs }
   2073