Home | History | Annotate | Download | only in zfs
      1    789    ahrens /*
      2    789    ahrens  * CDDL HEADER START
      3    789    ahrens  *
      4    789    ahrens  * The contents of this file are subject to the terms of the
      5   1544  eschrock  * Common Development and Distribution License (the "License").
      6   1544  eschrock  * You may not use this file except in compliance with the License.
      7    789    ahrens  *
      8    789    ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9    789    ahrens  * or http://www.opensolaris.org/os/licensing.
     10    789    ahrens  * See the License for the specific language governing permissions
     11    789    ahrens  * and limitations under the License.
     12    789    ahrens  *
     13    789    ahrens  * When distributing Covered Code, include this CDDL HEADER in each
     14    789    ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15    789    ahrens  * If applicable, add the following below this CDDL HEADER, with the
     16    789    ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
     17    789    ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
     18    789    ahrens  *
     19    789    ahrens  * CDDL HEADER END
     20    789    ahrens  */
     21    789    ahrens /*
     22   9355   Matthew  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23    789    ahrens  * Use is subject to license terms.
     24    789    ahrens  */
     25    789    ahrens 
     26   4543     marks #include <sys/cred.h>
     27    789    ahrens #include <sys/zfs_context.h>
     28    789    ahrens #include <sys/dmu_objset.h>
     29    789    ahrens #include <sys/dsl_dir.h>
     30    789    ahrens #include <sys/dsl_dataset.h>
     31    789    ahrens #include <sys/dsl_prop.h>
     32    789    ahrens #include <sys/dsl_pool.h>
     33   2199    ahrens #include <sys/dsl_synctask.h>
     34   4543     marks #include <sys/dsl_deleg.h>
     35    789    ahrens #include <sys/dnode.h>
     36    789    ahrens #include <sys/dbuf.h>
     37   2885    ahrens #include <sys/zvol.h>
     38    789    ahrens #include <sys/dmu_tx.h>
     39    789    ahrens #include <sys/zap.h>
     40    789    ahrens #include <sys/zil.h>
     41    789    ahrens #include <sys/dmu_impl.h>
     42   4543     marks #include <sys/zfs_ioctl.h>
     43  11165   Matthew #include <sys/sunddi.h>
     44    789    ahrens 
     45    789    ahrens spa_t *
     46    789    ahrens dmu_objset_spa(objset_t *os)
     47    789    ahrens {
     48  10298   Matthew 	return (os->os_spa);
     49    789    ahrens }
     50    789    ahrens 
     51    789    ahrens zilog_t *
     52    789    ahrens dmu_objset_zil(objset_t *os)
     53    789    ahrens {
     54  10298   Matthew 	return (os->os_zil);
     55    789    ahrens }
     56    789    ahrens 
     57    789    ahrens dsl_pool_t *
     58    789    ahrens dmu_objset_pool(objset_t *os)
     59    789    ahrens {
     60    789    ahrens 	dsl_dataset_t *ds;
     61    789    ahrens 
     62  10298   Matthew 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
     63    789    ahrens 		return (ds->ds_dir->dd_pool);
     64    789    ahrens 	else
     65  10298   Matthew 		return (spa_get_dsl(os->os_spa));
     66    789    ahrens }
     67    789    ahrens 
     68    789    ahrens dsl_dataset_t *
     69    789    ahrens dmu_objset_ds(objset_t *os)
     70    789    ahrens {
     71  10298   Matthew 	return (os->os_dsl_dataset);
     72    789    ahrens }
     73    789    ahrens 
     74    789    ahrens dmu_objset_type_t
     75    789    ahrens dmu_objset_type(objset_t *os)
     76    789    ahrens {
     77  10298   Matthew 	return (os->os_phys->os_type);
     78    789    ahrens }
     79    789    ahrens 
     80    789    ahrens void
     81    789    ahrens dmu_objset_name(objset_t *os, char *buf)
     82    789    ahrens {
     83  10298   Matthew 	dsl_dataset_name(os->os_dsl_dataset, buf);
     84    789    ahrens }
     85    789    ahrens 
     86    789    ahrens uint64_t
     87    789    ahrens dmu_objset_id(objset_t *os)
     88    789    ahrens {
     89  10298   Matthew 	dsl_dataset_t *ds = os->os_dsl_dataset;
     90    789    ahrens 
     91    789    ahrens 	return (ds ? ds->ds_object : 0);
     92    789    ahrens }
     93    789    ahrens 
     94  10310      Neil uint64_t
     95  10310      Neil dmu_objset_logbias(objset_t *os)
     96  10310      Neil {
     97  10310      Neil 	return (os->os_logbias);
     98  10310      Neil }
     99  10310      Neil 
    100    789    ahrens static void
    101    789    ahrens checksum_changed_cb(void *arg, uint64_t newval)
    102    789    ahrens {
    103  10298   Matthew 	objset_t *os = arg;
    104    789    ahrens 
    105    789    ahrens 	/*
    106    789    ahrens 	 * Inheritance should have been done by now.
    107    789    ahrens 	 */
    108    789    ahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
    109    789    ahrens 
    110  10298   Matthew 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
    111    789    ahrens }
    112    789    ahrens 
    113    789    ahrens static void
    114    789    ahrens compression_changed_cb(void *arg, uint64_t newval)
    115    789    ahrens {
    116  10298   Matthew 	objset_t *os = arg;
    117    789    ahrens 
    118    789    ahrens 	/*
    119    789    ahrens 	 * Inheritance and range checking should have been done by now.
    120    789    ahrens 	 */
    121    789    ahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
    122    789    ahrens 
    123  10298   Matthew 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
    124    789    ahrens }
    125    789    ahrens 
    126   3835    ahrens static void
    127   3835    ahrens copies_changed_cb(void *arg, uint64_t newval)
    128   3835    ahrens {
    129  10298   Matthew 	objset_t *os = arg;
    130   3835    ahrens 
    131   3835    ahrens 	/*
    132   3835    ahrens 	 * Inheritance and range checking should have been done by now.
    133   3835    ahrens 	 */
    134   3835    ahrens 	ASSERT(newval > 0);
    135  10298   Matthew 	ASSERT(newval <= spa_max_replication(os->os_spa));
    136   3835    ahrens 
    137  10298   Matthew 	os->os_copies = newval;
    138   3835    ahrens }
    139   3835    ahrens 
    140   7237  ek110237 static void
    141  10922      Jeff dedup_changed_cb(void *arg, uint64_t newval)
    142  10922      Jeff {
    143  10922      Jeff 	objset_t *os = arg;
    144  10922      Jeff 	spa_t *spa = os->os_spa;
    145  10922      Jeff 	enum zio_checksum checksum;
    146  10922      Jeff 
    147  10922      Jeff 	/*
    148  10922      Jeff 	 * Inheritance should have been done by now.
    149  10922      Jeff 	 */
    150  10922      Jeff 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
    151  10922      Jeff 
    152  10922      Jeff 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
    153  10922      Jeff 
    154  10922      Jeff 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
    155  10922      Jeff 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
    156  10922      Jeff }
    157  10922      Jeff 
    158  10922      Jeff static void
    159   7237  ek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
    160   7237  ek110237 {
    161  10298   Matthew 	objset_t *os = arg;
    162   7237  ek110237 
    163   7237  ek110237 	/*
    164   7237  ek110237 	 * Inheritance and range checking should have been done by now.
    165   7237  ek110237 	 */
    166   7237  ek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
    167   7237  ek110237 	    newval == ZFS_CACHE_METADATA);
    168   7237  ek110237 
    169  10298   Matthew 	os->os_primary_cache = newval;
    170   7237  ek110237 }
    171   7237  ek110237 
    172   7237  ek110237 static void
    173   7237  ek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
    174   7237  ek110237 {
    175  10298   Matthew 	objset_t *os = arg;
    176   7237  ek110237 
    177   7237  ek110237 	/*
    178   7237  ek110237 	 * Inheritance and range checking should have been done by now.
    179   7237  ek110237 	 */
    180   7237  ek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
    181   7237  ek110237 	    newval == ZFS_CACHE_METADATA);
    182   7237  ek110237 
    183  10298   Matthew 	os->os_secondary_cache = newval;
    184  10310      Neil }
    185  10310      Neil 
    186  10310      Neil static void
    187  10310      Neil logbias_changed_cb(void *arg, uint64_t newval)
    188  10310      Neil {
    189  10310      Neil 	objset_t *os = arg;
    190  10310      Neil 
    191  10310      Neil 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
    192  10310      Neil 	    newval == ZFS_LOGBIAS_THROUGHPUT);
    193  10310      Neil 	os->os_logbias = newval;
    194  10310      Neil 	if (os->os_zil)
    195  10310      Neil 		zil_set_logbias(os->os_zil, newval);
    196   7237  ek110237 }
    197   7237  ek110237 
    198    789    ahrens void
    199    789    ahrens dmu_objset_byteswap(void *buf, size_t size)
    200    789    ahrens {
    201    789    ahrens 	objset_phys_t *osp = buf;
    202    789    ahrens 
    203   9396   Matthew 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
    204    789    ahrens 	dnode_byteswap(&osp->os_meta_dnode);
    205    789    ahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
    206    789    ahrens 	osp->os_type = BSWAP_64(osp->os_type);
    207   9396   Matthew 	osp->os_flags = BSWAP_64(osp->os_flags);
    208   9396   Matthew 	if (size == sizeof (objset_phys_t)) {
    209   9396   Matthew 		dnode_byteswap(&osp->os_userused_dnode);
    210   9396   Matthew 		dnode_byteswap(&osp->os_groupused_dnode);
    211   9396   Matthew 	}
    212    789    ahrens }
    213    789    ahrens 
    214   1544  eschrock int
    215   1544  eschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
    216  10298   Matthew     objset_t **osp)
    217    789    ahrens {
    218  10298   Matthew 	objset_t *os;
    219   7046    ahrens 	int i, err;
    220   4787    ahrens 
    221   4787    ahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
    222    789    ahrens 
    223  10298   Matthew 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
    224  10298   Matthew 	os->os_dsl_dataset = ds;
    225  10298   Matthew 	os->os_spa = spa;
    226  10298   Matthew 	os->os_rootbp = bp;
    227  10298   Matthew 	if (!BP_IS_HOLE(os->os_rootbp)) {
    228   2391    maybee 		uint32_t aflags = ARC_WAIT;
    229   1544  eschrock 		zbookmark_t zb;
    230  10922      Jeff 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
    231  10922      Jeff 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
    232  10922      Jeff 
    233  10298   Matthew 		if (DMU_OS_IS_L2CACHEABLE(os))
    234   7237  ek110237 			aflags |= ARC_L2CACHE;
    235   1544  eschrock 
    236  10298   Matthew 		dprintf_bp(os->os_rootbp, "reading %s", "");
    237   7046    ahrens 		/*
    238   7046    ahrens 		 * NB: when bprewrite scrub can change the bp,
    239   7046    ahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
    240   7046    ahrens 		 * could change, and we'll need a lock.
    241   7046    ahrens 		 */
    242  10298   Matthew 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
    243  10298   Matthew 		    arc_getbuf_func, &os->os_phys_buf,
    244   2391    maybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
    245   1544  eschrock 		if (err) {
    246  10298   Matthew 			kmem_free(os, sizeof (objset_t));
    247   7294    perrin 			/* convert checksum errors into IO errors */
    248   7294    perrin 			if (err == ECKSUM)
    249   7294    perrin 				err = EIO;
    250   1544  eschrock 			return (err);
    251   1544  eschrock 		}
    252   9396   Matthew 
    253   9396   Matthew 		/* Increase the blocksize if we are permitted. */
    254   9396   Matthew 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
    255  10298   Matthew 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
    256   9396   Matthew 			arc_buf_t *buf = arc_buf_alloc(spa,
    257  10298   Matthew 			    sizeof (objset_phys_t), &os->os_phys_buf,
    258   9396   Matthew 			    ARC_BUFC_METADATA);
    259   9396   Matthew 			bzero(buf->b_data, sizeof (objset_phys_t));
    260  10298   Matthew 			bcopy(os->os_phys_buf->b_data, buf->b_data,
    261  10298   Matthew 			    arc_buf_size(os->os_phys_buf));
    262  10298   Matthew 			(void) arc_buf_remove_ref(os->os_phys_buf,
    263  10298   Matthew 			    &os->os_phys_buf);
    264  10298   Matthew 			os->os_phys_buf = buf;
    265   9396   Matthew 		}
    266   9396   Matthew 
    267  10298   Matthew 		os->os_phys = os->os_phys_buf->b_data;
    268  10298   Matthew 		os->os_flags = os->os_phys->os_flags;
    269    789    ahrens 	} else {
    270   9396   Matthew 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
    271   9396   Matthew 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
    272  10298   Matthew 		os->os_phys_buf = arc_buf_alloc(spa, size,
    273  10298   Matthew 		    &os->os_phys_buf, ARC_BUFC_METADATA);
    274  10298   Matthew 		os->os_phys = os->os_phys_buf->b_data;
    275  10298   Matthew 		bzero(os->os_phys, size);
    276    789    ahrens 	}
    277    789    ahrens 
    278    789    ahrens 	/*
    279    789    ahrens 	 * Note: the changed_cb will be called once before the register
    280    789    ahrens 	 * func returns, thus changing the checksum/compression from the
    281   7237  ek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
    282   7237  ek110237 	 * checksum/compression/copies.
    283    789    ahrens 	 */
    284   7237  ek110237 	if (ds) {
    285   7237  ek110237 		err = dsl_prop_register(ds, "primarycache",
    286  10298   Matthew 		    primary_cache_changed_cb, os);
    287   1544  eschrock 		if (err == 0)
    288   7237  ek110237 			err = dsl_prop_register(ds, "secondarycache",
    289  10298   Matthew 			    secondary_cache_changed_cb, os);
    290   7237  ek110237 		if (!dsl_dataset_is_snapshot(ds)) {
    291   7237  ek110237 			if (err == 0)
    292   7237  ek110237 				err = dsl_prop_register(ds, "checksum",
    293  10298   Matthew 				    checksum_changed_cb, os);
    294   7237  ek110237 			if (err == 0)
    295   7237  ek110237 				err = dsl_prop_register(ds, "compression",
    296  10298   Matthew 				    compression_changed_cb, os);
    297   7237  ek110237 			if (err == 0)
    298   7237  ek110237 				err = dsl_prop_register(ds, "copies",
    299  10298   Matthew 				    copies_changed_cb, os);
    300  10310      Neil 			if (err == 0)
    301  10922      Jeff 				err = dsl_prop_register(ds, "dedup",
    302  10922      Jeff 				    dedup_changed_cb, os);
    303  10922      Jeff 			if (err == 0)
    304  10310      Neil 				err = dsl_prop_register(ds, "logbias",
    305  10310      Neil 				    logbias_changed_cb, os);
    306   7237  ek110237 		}
    307   1544  eschrock 		if (err) {
    308  10298   Matthew 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
    309  10298   Matthew 			    &os->os_phys_buf) == 1);
    310  10298   Matthew 			kmem_free(os, sizeof (objset_t));
    311   1544  eschrock 			return (err);
    312   1544  eschrock 		}
    313   2082  eschrock 	} else if (ds == NULL) {
    314    789    ahrens 		/* It's the meta-objset. */
    315  10298   Matthew 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
    316  10298   Matthew 		os->os_compress = ZIO_COMPRESS_LZJB;
    317  10298   Matthew 		os->os_copies = spa_max_replication(spa);
    318  10922      Jeff 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
    319  10922      Jeff 		os->os_dedup_verify = 0;
    320  10922      Jeff 		os->os_logbias = 0;
    321  10298   Matthew 		os->os_primary_cache = ZFS_CACHE_ALL;
    322  10298   Matthew 		os->os_secondary_cache = ZFS_CACHE_ALL;
    323    789    ahrens 	}
    324   1544  eschrock 
    325  10298   Matthew 	os->os_zil_header = os->os_phys->os_zil_header;
    326  10298   Matthew 	os->os_zil = zil_alloc(os, &os->os_zil_header);
    327    789    ahrens 
    328    789    ahrens 	for (i = 0; i < TXG_SIZE; i++) {
    329  10298   Matthew 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
    330    789    ahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
    331  10298   Matthew 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
    332    789    ahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
    333    789    ahrens 	}
    334  10298   Matthew 	list_create(&os->os_dnodes, sizeof (dnode_t),
    335    789    ahrens 	    offsetof(dnode_t, dn_link));
    336  10298   Matthew 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
    337    789    ahrens 	    offsetof(dmu_buf_impl_t, db_link));
    338    789    ahrens 
    339  10298   Matthew 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
    340  10298   Matthew 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
    341  10298   Matthew 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
    342   2856  nd150628 
    343  10298   Matthew 	os->os_meta_dnode = dnode_special_open(os,
    344  10298   Matthew 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
    345  10298   Matthew 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
    346  10298   Matthew 		os->os_userused_dnode = dnode_special_open(os,
    347  10298   Matthew 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
    348  10298   Matthew 		os->os_groupused_dnode = dnode_special_open(os,
    349  10298   Matthew 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
    350   9396   Matthew 	}
    351    789    ahrens 
    352   4787    ahrens 	/*
    353   4787    ahrens 	 * We should be the only thread trying to do this because we
    354   4787    ahrens 	 * have ds_opening_lock
    355   4787    ahrens 	 */
    356   4787    ahrens 	if (ds) {
    357  10298   Matthew 		mutex_enter(&ds->ds_lock);
    358  10298   Matthew 		ASSERT(ds->ds_objset == NULL);
    359  10298   Matthew 		ds->ds_objset = os;
    360  10298   Matthew 		mutex_exit(&ds->ds_lock);
    361    789    ahrens 	}
    362    789    ahrens 
    363  10298   Matthew 	*osp = os;
    364   5367    ahrens 	return (0);
    365   5367    ahrens }
    366   5367    ahrens 
    367   5367    ahrens int
    368  10298   Matthew dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
    369   5367    ahrens {
    370  10298   Matthew 	int err = 0;
    371   5367    ahrens 
    372  10298   Matthew 	mutex_enter(&ds->ds_opening_lock);
    373  10298   Matthew 	*osp = ds->ds_objset;
    374  10298   Matthew 	if (*osp == NULL) {
    375  10298   Matthew 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
    376  10298   Matthew 		    ds, &ds->ds_phys->ds_bp, osp);
    377  10298   Matthew 	}
    378  10298   Matthew 	mutex_exit(&ds->ds_opening_lock);
    379   5367    ahrens 	return (err);
    380   5367    ahrens }
    381   5367    ahrens 
    382    789    ahrens /* called from zpl */
    383    789    ahrens int
    384  10298   Matthew dmu_objset_hold(const char *name, void *tag, objset_t **osp)
    385    789    ahrens {
    386    789    ahrens 	dsl_dataset_t *ds;
    387    789    ahrens 	int err;
    388   5367    ahrens 
    389  10298   Matthew 	err = dsl_dataset_hold(name, tag, &ds);
    390  10298   Matthew 	if (err)
    391  10298   Matthew 		return (err);
    392    789    ahrens 
    393  10298   Matthew 	err = dmu_objset_from_ds(ds, osp);
    394  10298   Matthew 	if (err)
    395  10298   Matthew 		dsl_dataset_rele(ds, tag);
    396  10298   Matthew 
    397  10298   Matthew 	return (err);
    398  10298   Matthew }
    399  10298   Matthew 
    400  10298   Matthew /* called from zpl */
    401  10298   Matthew int
    402  10298   Matthew dmu_objset_own(const char *name, dmu_objset_type_t type,
    403  10298   Matthew     boolean_t readonly, void *tag, objset_t **osp)
    404  10298   Matthew {
    405  10298   Matthew 	dsl_dataset_t *ds;
    406  10298   Matthew 	int err;
    407  10298   Matthew 
    408  10298   Matthew 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
    409  10298   Matthew 	if (err)
    410  10298   Matthew 		return (err);
    411  10298   Matthew 
    412  10298   Matthew 	err = dmu_objset_from_ds(ds, osp);
    413    789    ahrens 	if (err) {
    414  10298   Matthew 		dsl_dataset_disown(ds, tag);
    415  10588      Eric 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
    416  10298   Matthew 		dmu_objset_disown(*osp, tag);
    417  10298   Matthew 		return (EINVAL);
    418  10588      Eric 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
    419  10588      Eric 		dmu_objset_disown(*osp, tag);
    420  10588      Eric 		return (EROFS);
    421    789    ahrens 	}
    422   5367    ahrens 	return (err);
    423    789    ahrens }
    424    789    ahrens 
    425    789    ahrens void
    426  10298   Matthew dmu_objset_rele(objset_t *os, void *tag)
    427    789    ahrens {
    428  10298   Matthew 	dsl_dataset_rele(os->os_dsl_dataset, tag);
    429  10298   Matthew }
    430   6689    maybee 
    431  10298   Matthew void
    432  10298   Matthew dmu_objset_disown(objset_t *os, void *tag)
    433  10298   Matthew {
    434  10298   Matthew 	dsl_dataset_disown(os->os_dsl_dataset, tag);
    435    789    ahrens }
    436    789    ahrens 
    437   1646    perrin int
    438   4944    maybee dmu_objset_evict_dbufs(objset_t *os)
    439   1544  eschrock {
    440   1544  eschrock 	dnode_t *dn;
    441   1596    ahrens 
    442  10298   Matthew 	mutex_enter(&os->os_lock);
    443   1596    ahrens 
    444   1596    ahrens 	/* process the mdn last, since the other dnodes have holds on it */
    445  10298   Matthew 	list_remove(&os->os_dnodes, os->os_meta_dnode);
    446  10298   Matthew 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
    447   1544  eschrock 
    448   1544  eschrock 	/*
    449   1596    ahrens 	 * Find the first dnode with holds.  We have to do this dance
    450   1596    ahrens 	 * because dnode_add_ref() only works if you already have a
    451   1596    ahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
    452   1596    ahrens 	 * skip.
    453   1544  eschrock 	 */
    454  10298   Matthew 	for (dn = list_head(&os->os_dnodes);
    455   4944    maybee 	    dn && !dnode_add_ref(dn, FTAG);
    456  10298   Matthew 	    dn = list_next(&os->os_dnodes, dn))
    457   1596    ahrens 		continue;
    458   1596    ahrens 
    459   1596    ahrens 	while (dn) {
    460   1596    ahrens 		dnode_t *next_dn = dn;
    461   1596    ahrens 
    462   1596    ahrens 		do {
    463  10298   Matthew 			next_dn = list_next(&os->os_dnodes, next_dn);
    464   4944    maybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
    465   1596    ahrens 
    466  10298   Matthew 		mutex_exit(&os->os_lock);
    467   4944    maybee 		dnode_evict_dbufs(dn);
    468   1596    ahrens 		dnode_rele(dn, FTAG);
    469  10298   Matthew 		mutex_enter(&os->os_lock);
    470   1596    ahrens 		dn = next_dn;
    471   1544  eschrock 	}
    472  10298   Matthew 	mutex_exit(&os->os_lock);
    473  10298   Matthew 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
    474   1544  eschrock }
    475   1544  eschrock 
    476   1544  eschrock void
    477  10298   Matthew dmu_objset_evict(objset_t *os)
    478    789    ahrens {
    479  10298   Matthew 	dsl_dataset_t *ds = os->os_dsl_dataset;
    480    789    ahrens 
    481  10922      Jeff 	for (int t = 0; t < TXG_SIZE; t++)
    482  10922      Jeff 		ASSERT(!dmu_objset_is_dirty(os, t));
    483    789    ahrens 
    484   7237  ek110237 	if (ds) {
    485   7237  ek110237 		if (!dsl_dataset_is_snapshot(ds)) {
    486   7237  ek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
    487  10298   Matthew 			    checksum_changed_cb, os));
    488   7237  ek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
    489  10298   Matthew 			    compression_changed_cb, os));
    490   7237  ek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
    491  10298   Matthew 			    copies_changed_cb, os));
    492  10922      Jeff 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
    493  10922      Jeff 			    dedup_changed_cb, os));
    494  10310      Neil 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
    495  10310      Neil 			    logbias_changed_cb, os));
    496   7237  ek110237 		}
    497   7237  ek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
    498  10298   Matthew 		    primary_cache_changed_cb, os));
    499   7237  ek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
    500  10298   Matthew 		    secondary_cache_changed_cb, os));
    501    789    ahrens 	}
    502   1544  eschrock 
    503   1544  eschrock 	/*
    504   1544  eschrock 	 * We should need only a single pass over the dnode list, since
    505   1544  eschrock 	 * nothing can be added to the list at this point.
    506   1544  eschrock 	 */
    507  10298   Matthew 	(void) dmu_objset_evict_dbufs(os);
    508    789    ahrens 
    509  10298   Matthew 	dnode_special_close(os->os_meta_dnode);
    510  10298   Matthew 	if (os->os_userused_dnode) {
    511  10298   Matthew 		dnode_special_close(os->os_userused_dnode);
    512  10298   Matthew 		dnode_special_close(os->os_groupused_dnode);
    513   9396   Matthew 	}
    514  10298   Matthew 	zil_free(os->os_zil);
    515    789    ahrens 
    516  10298   Matthew 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
    517    789    ahrens 
    518  10298   Matthew 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
    519  10298   Matthew 	mutex_destroy(&os->os_lock);
    520  10298   Matthew 	mutex_destroy(&os->os_obj_lock);
    521  10298   Matthew 	mutex_destroy(&os->os_user_ptr_lock);
    522  10298   Matthew 	kmem_free(os, sizeof (objset_t));
    523  10373     chris }
    524  10373     chris 
    525  10373     chris timestruc_t
    526  10373     chris dmu_objset_snap_cmtime(objset_t *os)
    527  10373     chris {
    528  10373     chris 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
    529    789    ahrens }
    530    789    ahrens 
    531    789    ahrens /* called from dsl for meta-objset */
    532  10298   Matthew objset_t *
    533   3547    maybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
    534   3547    maybee     dmu_objset_type_t type, dmu_tx_t *tx)
    535    789    ahrens {
    536  10298   Matthew 	objset_t *os;
    537    789    ahrens 	dnode_t *mdn;
    538    789    ahrens 
    539    789    ahrens 	ASSERT(dmu_tx_is_syncing(tx));
    540   4787    ahrens 	if (ds)
    541   4787    ahrens 		mutex_enter(&ds->ds_opening_lock);
    542  10298   Matthew 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
    543   4787    ahrens 	if (ds)
    544   4787    ahrens 		mutex_exit(&ds->ds_opening_lock);
    545  10298   Matthew 	mdn = os->os_meta_dnode;
    546    789    ahrens 
    547    789    ahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
    548    789    ahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
    549    789    ahrens 
    550    789    ahrens 	/*
    551    789    ahrens 	 * We don't want to have to increase the meta-dnode's nlevels
    552    789    ahrens 	 * later, because then we could do it in quescing context while
    553    789    ahrens 	 * we are also accessing it in open context.
    554    789    ahrens 	 *
    555    789    ahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
    556    789    ahrens 	 * because the MOS is only updated in syncing context.
    557    789    ahrens 	 * This is most fortunate: the MOS is the only objset that
    558    789    ahrens 	 * needs to be synced multiple times as spa_sync() iterates
    559    789    ahrens 	 * to convergence, so minimizing its dn_nlevels matters.
    560    789    ahrens 	 */
    561   1544  eschrock 	if (ds != NULL) {
    562   1544  eschrock 		int levels = 1;
    563   1544  eschrock 
    564   1544  eschrock 		/*
    565   1544  eschrock 		 * Determine the number of levels necessary for the meta-dnode
    566   1544  eschrock 		 * to contain DN_MAX_OBJECT dnodes.
    567   1544  eschrock 		 */
    568   1544  eschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
    569   1544  eschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
    570   1544  eschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
    571   1544  eschrock 			levels++;
    572   1544  eschrock 
    573    789    ahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
    574   1544  eschrock 		    mdn->dn_nlevels = levels;
    575   1544  eschrock 	}
    576    789    ahrens 
    577    789    ahrens 	ASSERT(type != DMU_OST_NONE);
    578    789    ahrens 	ASSERT(type != DMU_OST_ANY);
    579    789    ahrens 	ASSERT(type < DMU_OST_NUMTYPES);
    580  10298   Matthew 	os->os_phys->os_type = type;
    581  10298   Matthew 	if (dmu_objset_userused_enabled(os)) {
    582  10298   Matthew 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
    583  10298   Matthew 		os->os_flags = os->os_phys->os_flags;
    584   9396   Matthew 	}
    585    789    ahrens 
    586    789    ahrens 	dsl_dataset_dirty(ds, tx);
    587    789    ahrens 
    588  10298   Matthew 	return (os);
    589    789    ahrens }
    590    789    ahrens 
    591    789    ahrens struct oscarg {
    592   4543     marks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
    593    789    ahrens 	void *userarg;
    594  10272   Matthew 	dsl_dataset_t *clone_origin;
    595    789    ahrens 	const char *lastname;
    596    789    ahrens 	dmu_objset_type_t type;
    597   6492      timh 	uint64_t flags;
    598    789    ahrens };
    599    789    ahrens 
    600   4543     marks /*ARGSUSED*/
    601    789    ahrens static int
    602   2199    ahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
    603    789    ahrens {
    604   2199    ahrens 	dsl_dir_t *dd = arg1;
    605   2199    ahrens 	struct oscarg *oa = arg2;
    606   2199    ahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
    607   2199    ahrens 	int err;
    608   2199    ahrens 	uint64_t ddobj;
    609   2199    ahrens 
    610   2199    ahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
    611   2199    ahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
    612   2199    ahrens 	if (err != ENOENT)
    613   2199    ahrens 		return (err ? err : EEXIST);
    614   2199    ahrens 
    615  10272   Matthew 	if (oa->clone_origin != NULL) {
    616  10272   Matthew 		/* You can't clone across pools. */
    617  10272   Matthew 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
    618   2199    ahrens 			return (EXDEV);
    619   2199    ahrens 
    620  10272   Matthew 		/* You can only clone snapshots, not the head datasets. */
    621  10272   Matthew 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
    622   2199    ahrens 			return (EINVAL);
    623   2199    ahrens 	}
    624   4543     marks 
    625   2199    ahrens 	return (0);
    626   2199    ahrens }
    627   2199    ahrens 
    628   2199    ahrens static void
    629   4543     marks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
    630   2199    ahrens {
    631   2199    ahrens 	dsl_dir_t *dd = arg1;
    632   2199    ahrens 	struct oscarg *oa = arg2;
    633   2199    ahrens 	uint64_t dsobj;
    634    789    ahrens 
    635    789    ahrens 	ASSERT(dmu_tx_is_syncing(tx));
    636    789    ahrens 
    637   2199    ahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
    638  10272   Matthew 	    oa->clone_origin, oa->flags, cr, tx);
    639    789    ahrens 
    640  10272   Matthew 	if (oa->clone_origin == NULL) {
    641  10272   Matthew 		dsl_dataset_t *ds;
    642  10272   Matthew 		blkptr_t *bp;
    643  10298   Matthew 		objset_t *os;
    644    789    ahrens 
    645  10272   Matthew 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
    646  10272   Matthew 		    FTAG, &ds));
    647  10272   Matthew 		bp = dsl_dataset_get_blkptr(ds);
    648  10272   Matthew 		ASSERT(BP_IS_HOLE(bp));
    649  10272   Matthew 
    650  10298   Matthew 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
    651   3547    maybee 		    ds, bp, oa->type, tx);
    652    789    ahrens 
    653    789    ahrens 		if (oa->userfunc)
    654  10298   Matthew 			oa->userfunc(os, oa->userarg, cr, tx);
    655  10272   Matthew 		dsl_dataset_rele(ds, FTAG);
    656    789    ahrens 	}
    657   4543     marks 
    658   4543     marks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
    659   4543     marks 	    tx, cr, "dataset = %llu", dsobj);
    660    789    ahrens }
    661    789    ahrens 
    662    789    ahrens int
    663  10272   Matthew dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
    664   4543     marks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
    665    789    ahrens {
    666   2199    ahrens 	dsl_dir_t *pdd;
    667    789    ahrens 	const char *tail;
    668    789    ahrens 	int err = 0;
    669   2199    ahrens 	struct oscarg oa = { 0 };
    670    789    ahrens 
    671   2199    ahrens 	ASSERT(strchr(name, '@') == NULL);
    672   2199    ahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
    673   1544  eschrock 	if (err)
    674   1544  eschrock 		return (err);
    675    789    ahrens 	if (tail == NULL) {
    676   2199    ahrens 		dsl_dir_close(pdd, FTAG);
    677    789    ahrens 		return (EEXIST);
    678    789    ahrens 	}
    679    789    ahrens 
    680   2199    ahrens 	oa.userfunc = func;
    681   2199    ahrens 	oa.userarg = arg;
    682   2199    ahrens 	oa.lastname = tail;
    683   2199    ahrens 	oa.type = type;
    684   6492      timh 	oa.flags = flags;
    685   4543     marks 
    686  10272   Matthew 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
    687  10272   Matthew 	    dmu_objset_create_sync, pdd, &oa, 5);
    688  10272   Matthew 	dsl_dir_close(pdd, FTAG);
    689  10272   Matthew 	return (err);
    690  10272   Matthew }
    691  10272   Matthew 
    692  10272   Matthew int
    693  10272   Matthew dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
    694  10272   Matthew {
    695  10272   Matthew 	dsl_dir_t *pdd;
    696  10272   Matthew 	const char *tail;
    697  10272   Matthew 	int err = 0;
    698  10272   Matthew 	struct oscarg oa = { 0 };
    699  10272   Matthew 
    700  10272   Matthew 	ASSERT(strchr(name, '@') == NULL);
    701  10272   Matthew 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
    702  10272   Matthew 	if (err)
    703  10272   Matthew 		return (err);
    704  10272   Matthew 	if (tail == NULL) {
    705  10272   Matthew 		dsl_dir_close(pdd, FTAG);
    706  10272   Matthew 		return (EEXIST);
    707    789    ahrens 	}
    708  10272   Matthew 
    709  10272   Matthew 	oa.lastname = tail;
    710  10272   Matthew 	oa.clone_origin = clone_origin;
    711  10272   Matthew 	oa.flags = flags;
    712  10272   Matthew 
    713   2199    ahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
    714   2199    ahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
    715   2199    ahrens 	dsl_dir_close(pdd, FTAG);
    716    789    ahrens 	return (err);
    717    789    ahrens }
    718    789    ahrens 
    719    789    ahrens int
    720  10242     chris dmu_objset_destroy(const char *name, boolean_t defer)
    721    789    ahrens {
    722  10298   Matthew 	dsl_dataset_t *ds;
    723    789    ahrens 	int error;
    724    789    ahrens 
    725    789    ahrens 	/*
    726  10272   Matthew 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
    727  10272   Matthew 	 * intent log, but if there is an active log, it has blocks that
    728  10272   Matthew 	 * are allocated, but may not yet be reflected in the on-disk
    729  10272   Matthew 	 * structure.  Only the ZIL knows how to free them, so we have
    730  10272   Matthew 	 * to call into it here.
    731    789    ahrens 	 */
    732  10298   Matthew 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
    733    789    ahrens 	if (error == 0) {
    734  10298   Matthew 		objset_t *os;
    735  10298   Matthew 		if (dmu_objset_from_ds(ds, &os) == 0)
    736  10298   Matthew 			zil_destroy(dmu_objset_zil(os), B_FALSE);
    737  10298   Matthew 		error = dsl_dataset_destroy(ds, FTAG, defer);
    738  10272   Matthew 		/* dsl_dataset_destroy() closes the ds. */
    739    789    ahrens 	}
    740    789    ahrens 
    741   5367    ahrens 	return (error);
    742   2199    ahrens }
    743   2199    ahrens 
    744   2199    ahrens struct snaparg {
    745   2199    ahrens 	dsl_sync_task_group_t *dstg;
    746   2199    ahrens 	char *snapname;
    747   2199    ahrens 	char failed[MAXPATHLEN];
    748   4543     marks 	boolean_t checkperms;
    749   9355   Matthew 	nvlist_t *props;
    750   5367    ahrens };
    751   5367    ahrens 
    752   9355   Matthew static int
    753   9355   Matthew snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
    754   9355   Matthew {
    755   9355   Matthew 	objset_t *os = arg1;
    756   9355   Matthew 	struct snaparg *sn = arg2;
    757   9355   Matthew 
    758   9355   Matthew 	/* The props have already been checked by zfs_check_userprops(). */
    759   9355   Matthew 
    760  10298   Matthew 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
    761   9355   Matthew 	    sn->snapname, tx));
    762   9355   Matthew }
    763   9355   Matthew 
    764   9355   Matthew static void
    765   9355   Matthew snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
    766   9355   Matthew {
    767   9355   Matthew 	objset_t *os = arg1;
    768  10298   Matthew 	dsl_dataset_t *ds = os->os_dsl_dataset;
    769   9355   Matthew 	struct snaparg *sn = arg2;
    770   9355   Matthew 
    771   9355   Matthew 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
    772   9355   Matthew 
    773  11022       Tom 	if (sn->props) {
    774  11022       Tom 		dsl_props_arg_t pa;
    775  11022       Tom 		pa.pa_props = sn->props;
    776  11022       Tom 		pa.pa_source = ZPROP_SRC_LOCAL;
    777  11022       Tom 		dsl_props_set_sync(ds->ds_prev, &pa, cr, tx);
    778  11022       Tom 	}
    779   9355   Matthew }
    780   2199    ahrens 
    781   2199    ahrens static int
    782   2199    ahrens dmu_objset_snapshot_one(char *name, void *arg)
    783   2199    ahrens {
    784   2199    ahrens 	struct snaparg *sn = arg;
    785   2199    ahrens 	objset_t *os;
    786   2199    ahrens 	int err;
    787   2199    ahrens 
    788   2199    ahrens 	(void) strcpy(sn->failed, name);
    789   4543     marks 
    790   4543     marks 	/*
    791   4543     marks 	 * Check permissions only when requested.  This only applies when
    792   4543     marks 	 * doing a recursive snapshot.  The permission checks for the starting
    793   4543     marks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
    794   4543     marks 	 */
    795   4543     marks 	if (sn->checkperms == B_TRUE &&
    796   4543     marks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
    797   4543     marks 		return (err);
    798   2199    ahrens 
    799  10298   Matthew 	err = dmu_objset_hold(name, sn, &os);
    800   2199    ahrens 	if (err != 0)
    801   2199    ahrens 		return (err);
    802   3637  rm160521 
    803   6689    maybee 	/* If the objset is in an inconsistent state, return busy */
    804  10298   Matthew 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
    805  10298   Matthew 		dmu_objset_rele(os, sn);
    806   3637  rm160521 		return (EBUSY);
    807   3637  rm160521 	}
    808   2199    ahrens 
    809   2199    ahrens 	/*
    810   2199    ahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
    811   2199    ahrens 	 * so that we snapshot those changes.  zil_suspend does this as
    812   2199    ahrens 	 * a side effect.
    813   2199    ahrens 	 */
    814   2199    ahrens 	err = zil_suspend(dmu_objset_zil(os));
    815   2199    ahrens 	if (err == 0) {
    816   9355   Matthew 		dsl_sync_task_create(sn->dstg, snapshot_check,
    817   9355   Matthew 		    snapshot_sync, os, sn, 3);
    818   3637  rm160521 	} else {
    819  10298   Matthew 		dmu_objset_rele(os, sn);
    820   2199    ahrens 	}
    821   3637  rm160521 
    822   2199    ahrens 	return (err);
    823   2199    ahrens }
    824   2199    ahrens 
    825   2199    ahrens int
    826   9355   Matthew dmu_objset_snapshot(char *fsname, char *snapname,
    827   9355   Matthew     nvlist_t *props, boolean_t recursive)
    828   2199    ahrens {
    829   2199    ahrens 	dsl_sync_task_t *dst;
    830   9355   Matthew 	struct snaparg sn;
    831   2199    ahrens 	spa_t *spa;
    832   2199    ahrens 	int err;
    833   2199    ahrens 
    834   2199    ahrens 	(void) strcpy(sn.failed, fsname);
    835   2199    ahrens 
    836   4603    ahrens 	err = spa_open(fsname, &spa, FTAG);
    837   2199    ahrens 	if (err)
    838   2199    ahrens 		return (err);
    839   2199    ahrens 
    840   2199    ahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
    841   2199    ahrens 	sn.snapname = snapname;
    842   9355   Matthew 	sn.props = props;
    843   2199    ahrens 
    844   2417    ahrens 	if (recursive) {
    845   4543     marks 		sn.checkperms = B_TRUE;
    846   2417    ahrens 		err = dmu_objset_find(fsname,
    847   2417    ahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
    848   2417    ahrens 	} else {
    849   4543     marks 		sn.checkperms = B_FALSE;
    850   2199    ahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
    851   2417    ahrens 	}
    852   2199    ahrens 
    853   9355   Matthew 	if (err == 0)
    854   9355   Matthew 		err = dsl_sync_task_group_wait(sn.dstg);
    855   2199    ahrens 
    856   2199    ahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
    857   2199    ahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
    858   9355   Matthew 		objset_t *os = dst->dst_arg1;
    859  10298   Matthew 		dsl_dataset_t *ds = os->os_dsl_dataset;
    860   2199    ahrens 		if (dst->dst_err)
    861   5367    ahrens 			dsl_dataset_name(ds, sn.failed);
    862   9355   Matthew 		zil_resume(dmu_objset_zil(os));
    863  10298   Matthew 		dmu_objset_rele(os, &sn);
    864   2199    ahrens 	}
    865   6174    ahrens 
    866   2199    ahrens 	if (err)
    867   2199    ahrens 		(void) strcpy(fsname, sn.failed);
    868   2199    ahrens 	dsl_sync_task_group_destroy(sn.dstg);
    869   2199    ahrens 	spa_close(spa, FTAG);
    870    789    ahrens 	return (err);
    871    789    ahrens }
    872    789    ahrens 
    873    789    ahrens static void
    874   9396   Matthew dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
    875    789    ahrens {
    876   3547    maybee 	dnode_t *dn;
    877    789    ahrens 
    878   3547    maybee 	while (dn = list_head(list)) {
    879   3547    maybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
    880   3547    maybee 		ASSERT(dn->dn_dbuf->db_data_pending);
    881   3547    maybee 		/*
    882   9396   Matthew 		 * Initialize dn_zio outside dnode_sync() because the
    883   9396   Matthew 		 * meta-dnode needs to set it ouside dnode_sync().
    884   3547    maybee 		 */
    885   3547    maybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
    886   3547    maybee 		ASSERT(dn->dn_zio);
    887    789    ahrens 
    888   3547    maybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
    889   3547    maybee 		list_remove(list, dn);
    890   9396   Matthew 
    891   9396   Matthew 		if (newlist) {
    892   9396   Matthew 			(void) dnode_add_ref(dn, newlist);
    893   9396   Matthew 			list_insert_tail(newlist, dn);
    894   9396   Matthew 		}
    895   9396   Matthew 
    896   3547    maybee 		dnode_sync(dn, tx);
    897   3547    maybee 	}
    898   3547    maybee }
    899    789    ahrens 
    900   3547    maybee /* ARGSUSED */
    901   3547    maybee static void
    902  10922      Jeff dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
    903   3547    maybee {
    904   7754      Jeff 	blkptr_t *bp = zio->io_bp;
    905  10298   Matthew 	objset_t *os = arg;
    906   3547    maybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
    907    789    ahrens 
    908   7754      Jeff 	ASSERT(bp == os->os_rootbp);
    909   7754      Jeff 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
    910   7754      Jeff 	ASSERT(BP_GET_LEVEL(bp) == 0);
    911   5329   gw25295 
    912   3547    maybee 	/*
    913   9396   Matthew 	 * Update rootbp fill count: it should be the number of objects
    914   9396   Matthew 	 * allocated in the object set (not counting the "special"
    915   9396   Matthew 	 * objects that are stored in the objset_phys_t -- the meta
    916   9396   Matthew 	 * dnode and user/group accounting objects).
    917   3547    maybee 	 */
    918   9396   Matthew 	bp->blk_fill = 0;
    919   7754      Jeff 	for (int i = 0; i < dnp->dn_nblkptr; i++)
    920   3547    maybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
    921  10922      Jeff }
    922  10922      Jeff 
    923  10922      Jeff /* ARGSUSED */
    924  10922      Jeff static void
    925  10922      Jeff dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
    926  10922      Jeff {
    927  10922      Jeff 	blkptr_t *bp = zio->io_bp;
    928  10922      Jeff 	blkptr_t *bp_orig = &zio->io_bp_orig;
    929  10922      Jeff 	objset_t *os = arg;
    930   5329   gw25295 
    931   7754      Jeff 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
    932  10922      Jeff 		ASSERT(BP_EQUAL(bp, bp_orig));
    933   7754      Jeff 	} else {
    934  10922      Jeff 		dsl_dataset_t *ds = os->os_dsl_dataset;
    935  10922      Jeff 		dmu_tx_t *tx = os->os_synctx;
    936  10922      Jeff 
    937  10922      Jeff 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
    938  10922      Jeff 		dsl_dataset_block_born(ds, bp, tx);
    939   5329   gw25295 	}
    940    789    ahrens }
    941    789    ahrens 
    942    789    ahrens /* called from dsl */
    943    789    ahrens void
    944  10298   Matthew dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
    945    789    ahrens {
    946    789    ahrens 	int txgoff;
    947   1544  eschrock 	zbookmark_t zb;
    948  10922      Jeff 	zio_prop_t zp;
    949   3547    maybee 	zio_t *zio;
    950   3547    maybee 	list_t *list;
    951   9396   Matthew 	list_t *newlist = NULL;
    952   3547    maybee 	dbuf_dirty_record_t *dr;
    953   3547    maybee 
    954   3547    maybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
    955    789    ahrens 
    956    789    ahrens 	ASSERT(dmu_tx_is_syncing(tx));
    957    789    ahrens 	/* XXX the write_done callback should really give us the tx... */
    958    789    ahrens 	os->os_synctx = tx;
    959    789    ahrens 
    960   3882    ahrens 	if (os->os_dsl_dataset == NULL) {
    961   3882    ahrens 		/*
    962   3882    ahrens 		 * This is the MOS.  If we have upgraded,
    963   3882    ahrens 		 * spa_max_replication() could change, so reset
    964   3882    ahrens 		 * os_copies here.
    965   3882    ahrens 		 */
    966   3882    ahrens 		os->os_copies = spa_max_replication(os->os_spa);
    967   3882    ahrens 	}
    968   3882    ahrens 
    969   3547    maybee 	/*
    970   3547    maybee 	 * Create the root block IO
    971   3547    maybee 	 */
    972   7046    ahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
    973   9396   Matthew 
    974  10922      Jeff 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
    975  10922      Jeff 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
    976  10922      Jeff 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
    977  10922      Jeff 
    978  10922      Jeff 	dmu_write_policy(os, NULL, 0, 0, &zp);
    979  10922      Jeff 
    980  10922      Jeff 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
    981  10922      Jeff 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
    982  10922      Jeff 	    dmu_objset_write_ready, dmu_objset_write_done, os,
    983   7754      Jeff 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
    984   3547    maybee 
    985   3547    maybee 	/*
    986   9396   Matthew 	 * Sync special dnodes - the parent IO for the sync is the root block
    987   3547    maybee 	 */
    988   3547    maybee 	os->os_meta_dnode->dn_zio = zio;
    989   3547    maybee 	dnode_sync(os->os_meta_dnode, tx);
    990    789    ahrens 
    991   9396   Matthew 	os->os_phys->os_flags = os->os_flags;
    992   9396   Matthew 
    993   9396   Matthew 	if (os->os_userused_dnode &&
    994   9396   Matthew 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
    995   9396   Matthew 		os->os_userused_dnode->dn_zio = zio;
    996   9396   Matthew 		dnode_sync(os->os_userused_dnode, tx);
    997   9396   Matthew 		os->os_groupused_dnode->dn_zio = zio;
    998   9396   Matthew 		dnode_sync(os->os_groupused_dnode, tx);
    999   9396   Matthew 	}
   1000   9396   Matthew 
   1001    789    ahrens 	txgoff = tx->tx_txg & TXG_MASK;
   1002    789    ahrens 
   1003   9396   Matthew 	if (dmu_objset_userused_enabled(os)) {
   1004   9396   Matthew 		newlist = &os->os_synced_dnodes;
   1005   9396   Matthew 		/*
   1006   9396   Matthew 		 * We must create the list here because it uses the
   1007   9396   Matthew 		 * dn_dirty_link[] of this txg.
   1008   9396   Matthew 		 */
   1009   9396   Matthew 		list_create(newlist, sizeof (dnode_t),
   1010   9396   Matthew 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
   1011   9396   Matthew 	}
   1012   9396   Matthew 
   1013   9396   Matthew 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
   1014   9396   Matthew 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
   1015    789    ahrens 
   1016   3547    maybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
   1017   3547    maybee 	while (dr = list_head(list)) {
   1018   3547    maybee 		ASSERT(dr->dr_dbuf->db_level == 0);
   1019   3547    maybee 		list_remove(list, dr);
   1020   3547    maybee 		if (dr->dr_zio)
   1021   3547    maybee 			zio_nowait(dr->dr_zio);
   1022   3547    maybee 	}
   1023    789    ahrens 	/*
   1024    789    ahrens 	 * Free intent log blocks up to this tx.
   1025    789    ahrens 	 */
   1026    789    ahrens 	zil_sync(os->os_zil, tx);
   1027   7046    ahrens 	os->os_phys->os_zil_header = os->os_zil_header;
   1028   3547    maybee 	zio_nowait(zio);
   1029  10922      Jeff }
   1030  10922      Jeff 
   1031  10922      Jeff boolean_t
   1032  10922      Jeff dmu_objset_is_dirty(objset_t *os, uint64_t txg)
   1033  10922      Jeff {
   1034  10922      Jeff 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
   1035  10922      Jeff 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
   1036   9396   Matthew }
   1037   9396   Matthew 
   1038   9396   Matthew static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
   1039   9396   Matthew 
   1040   9396   Matthew void
   1041   9396   Matthew dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
   1042   9396   Matthew {
   1043   9396   Matthew 	used_cbs[ost] = cb;
   1044   9396   Matthew }
   1045   9396   Matthew 
   1046   9396   Matthew boolean_t
   1047  10298   Matthew dmu_objset_userused_enabled(objset_t *os)
   1048   9396   Matthew {
   1049   9396   Matthew 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
   1050   9396   Matthew 	    used_cbs[os->os_phys->os_type] &&
   1051   9396   Matthew 	    os->os_userused_dnode);
   1052   9396   Matthew }
   1053   9396   Matthew 
   1054  10407   Matthew static void
   1055  10407   Matthew do_userquota_callback(objset_t *os, dnode_phys_t *dnp,
   1056  10407   Matthew     boolean_t subtract, dmu_tx_t *tx)
   1057  10407   Matthew {
   1058  10407   Matthew 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
   1059  10407   Matthew 	uint64_t user, group;
   1060  10407   Matthew 
   1061  10407   Matthew 	ASSERT(dnp->dn_type != 0 ||
   1062  10407   Matthew 	    (bcmp(DN_BONUS(dnp), zerobuf, DN_MAX_BONUSLEN) == 0 &&
   1063  10407   Matthew 	    DN_USED_BYTES(dnp) == 0));
   1064  10407   Matthew 
   1065  10407   Matthew 	if ((dnp->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) &&
   1066  10407   Matthew 	    0 == used_cbs[os->os_phys->os_type](dnp->dn_bonustype,
   1067  10407   Matthew 	    DN_BONUS(dnp), &user, &group)) {
   1068  10407   Matthew 		int64_t delta = DNODE_SIZE + DN_USED_BYTES(dnp);
   1069  10407   Matthew 		if (subtract)
   1070  10407   Matthew 			delta = -delta;
   1071  10801   Matthew 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
   1072  10407   Matthew 		    user, delta, tx));
   1073  10801   Matthew 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
   1074  10407   Matthew 		    group, delta, tx));
   1075  10407   Matthew 	}
   1076  10407   Matthew }
   1077  10407   Matthew 
   1078   9396   Matthew void
   1079  10298   Matthew dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
   1080   9396   Matthew {
   1081   9396   Matthew 	dnode_t *dn;
   1082   9396   Matthew 	list_t *list = &os->os_synced_dnodes;
   1083   9396   Matthew 
   1084   9396   Matthew 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
   1085   9396   Matthew 
   1086   9396   Matthew 	while (dn = list_head(list)) {
   1087   9396   Matthew 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
   1088   9396   Matthew 		ASSERT(dn->dn_oldphys);
   1089   9396   Matthew 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
   1090   9396   Matthew 		    dn->dn_phys->dn_flags &
   1091   9396   Matthew 		    DNODE_FLAG_USERUSED_ACCOUNTED);
   1092   9396   Matthew 
   1093   9396   Matthew 		/* Allocate the user/groupused objects if necessary. */
   1094   9396   Matthew 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
   1095  10298   Matthew 			VERIFY(0 == zap_create_claim(os,
   1096   9396   Matthew 			    DMU_USERUSED_OBJECT,
   1097   9396   Matthew 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
   1098  10298   Matthew 			VERIFY(0 == zap_create_claim(os,
   1099   9396   Matthew 			    DMU_GROUPUSED_OBJECT,
   1100   9396   Matthew 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
   1101   9396   Matthew 		}
   1102   9396   Matthew 
   1103   9396   Matthew 		/*
   1104  10407   Matthew 		 * We intentionally modify the zap object even if the
   1105  10407   Matthew 		 * net delta (due to phys-oldphys) is zero.  Otherwise
   1106  10407   Matthew 		 * the block of the zap obj could be shared between
   1107  10407   Matthew 		 * datasets but need to be different between them after
   1108  10407   Matthew 		 * a bprewrite.
   1109   9396   Matthew 		 */
   1110  10407   Matthew 		do_userquota_callback(os, dn->dn_oldphys, B_TRUE, tx);
   1111  10407   Matthew 		do_userquota_callback(os, dn->dn_phys, B_FALSE, tx);
   1112   9396   Matthew 
   1113   9396   Matthew 		/*
   1114   9396   Matthew 		 * The mutex is needed here for interlock with dnode_allocate.
   1115   9396   Matthew 		 */
   1116   9396   Matthew 		mutex_enter(&dn->dn_mtx);
   1117   9396   Matthew 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
   1118   9396   Matthew 		dn->dn_oldphys = NULL;
   1119   9396   Matthew 		mutex_exit(&dn->dn_mtx);
   1120   9396   Matthew 
   1121   9396   Matthew 		list_remove(list, dn);
   1122   9396   Matthew 		dnode_rele(dn, list);
   1123   9396   Matthew 	}
   1124   9396   Matthew }
   1125   9396   Matthew 
   1126   9396   Matthew boolean_t
   1127   9396   Matthew dmu_objset_userspace_present(objset_t *os)
   1128   9396   Matthew {
   1129  10298   Matthew 	return (os->os_phys->os_flags &
   1130   9396   Matthew 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
   1131   9396   Matthew }
   1132   9396   Matthew 
   1133   9396   Matthew int
   1134   9396   Matthew dmu_objset_userspace_upgrade(objset_t *os)
   1135   9396   Matthew {
   1136   9396   Matthew 	uint64_t obj;
   1137   9396   Matthew 	int err = 0;
   1138   9396   Matthew 
   1139   9396   Matthew 	if (dmu_objset_userspace_present(os))
   1140   9396   Matthew 		return (0);
   1141  10298   Matthew 	if (!dmu_objset_userused_enabled(os))
   1142   9396   Matthew 		return (ENOTSUP);
   1143   9396   Matthew 	if (dmu_objset_is_snapshot(os))
   1144   9396   Matthew 		return (EINVAL);
   1145   9396   Matthew 
   1146   9396   Matthew 	/*
   1147   9396   Matthew 	 * We simply need to mark every object dirty, so that it will be
   1148   9396   Matthew 	 * synced out and now accounted.  If this is called
   1149   9396   Matthew 	 * concurrently, or if we already did some work before crashing,
   1150   9396   Matthew 	 * that's fine, since we track each object's accounted state
   1151   9396   Matthew 	 * independently.
   1152   9396   Matthew 	 */
   1153   9396   Matthew 
   1154   9396   Matthew 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
   1155   9951       Lin 		dmu_tx_t *tx;
   1156   9396   Matthew 		dmu_buf_t *db;
   1157   9396   Matthew 		int objerr;
   1158   9396   Matthew 
   1159   9396   Matthew 		if (issig(JUSTLOOKING) && issig(FORREAL))
   1160   9396   Matthew 			return (EINTR);
   1161   9396   Matthew 
   1162   9396   Matthew 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
   1163   9396   Matthew 		if (objerr)
   1164   9396   Matthew 			continue;
   1165   9951       Lin 		tx = dmu_tx_create(os);
   1166   9396   Matthew 		dmu_tx_hold_bonus(tx, obj);
   1167   9396   Matthew 		objerr = dmu_tx_assign(tx, TXG_WAIT);
   1168   9396   Matthew 		if (objerr) {
   1169   9396   Matthew 			dmu_tx_abort(tx);
   1170   9396   Matthew 			continue;
   1171   9396   Matthew 		}
   1172   9396   Matthew 		dmu_buf_will_dirty(db, tx);
   1173   9396   Matthew 		dmu_buf_rele(db, FTAG);
   1174   9396   Matthew 		dmu_tx_commit(tx);
   1175   9396   Matthew 	}
   1176   9396   Matthew 
   1177  10298   Matthew 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
   1178   9396   Matthew 	txg_wait_synced(dmu_objset_pool(os), 0);
   1179   9396   Matthew 	return (0);
   1180    789    ahrens }
   1181    789    ahrens 
   1182    789    ahrens void
   1183   2885    ahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
   1184   2885    ahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
   1185    789    ahrens {
   1186  10298   Matthew 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
   1187   2885    ahrens 	    usedobjsp, availobjsp);
   1188   2885    ahrens }
   1189   2885    ahrens 
   1190   2885    ahrens uint64_t
   1191   2885    ahrens dmu_objset_fsid_guid(objset_t *os)
   1192   2885    ahrens {
   1193  10298   Matthew 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
   1194   2885    ahrens }
   1195   2885    ahrens 
   1196   2885    ahrens void
   1197   2885    ahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
   1198   2885    ahrens {
   1199  10298   Matthew 	stat->dds_type = os->os_phys->os_type;
   1200  10298   Matthew 	if (os->os_dsl_dataset)
   1201  10298   Matthew 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
   1202   2885    ahrens }
   1203   2885    ahrens 
   1204   2885    ahrens void
   1205   2885    ahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
   1206   2885    ahrens {
   1207  10298   Matthew 	ASSERT(os->os_dsl_dataset ||
   1208  10298   Matthew 	    os->os_phys->os_type == DMU_OST_META);
   1209   2885    ahrens 
   1210  10298   Matthew 	if (os->os_dsl_dataset != NULL)
   1211  10298   Matthew 		dsl_dataset_stats(os->os_dsl_dataset, nv);
   1212   2885    ahrens 
   1213   2885    ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
   1214  10298   Matthew 	    os->os_phys->os_type);
   1215   9396   Matthew 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
   1216   9396   Matthew 	    dmu_objset_userspace_present(os));
   1217    789    ahrens }
   1218    789    ahrens 
   1219    789    ahrens int
   1220    789    ahrens dmu_objset_is_snapshot(objset_t *os)
   1221    789    ahrens {
   1222  10298   Matthew 	if (os->os_dsl_dataset != NULL)
   1223  10298   Matthew 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
   1224    789    ahrens 	else
   1225    789    ahrens 		return (B_FALSE);
   1226    789    ahrens }
   1227    789    ahrens 
   1228    789    ahrens int
   1229   6492      timh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
   1230   6492      timh     boolean_t *conflict)
   1231   6492      timh {
   1232  10298   Matthew 	dsl_dataset_t *ds = os->os_dsl_dataset;
   1233   6492      timh 	uint64_t ignored;
   1234   6492      timh 
   1235   6492      timh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
   1236   6492      timh 		return (ENOENT);
   1237   6492      timh 
   1238   6492      timh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
   1239   6492      timh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
   1240   6492      timh 	    real, maxlen, conflict));
   1241   6492      timh }
   1242   6492      timh 
   1243   6492      timh int
   1244    789    ahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
   1245   5663  ck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
   1246    789    ahrens {
   1247  10298   Matthew 	dsl_dataset_t *ds = os->os_dsl_dataset;
   1248    789    ahrens 	zap_cursor_t cursor;
   1249    789    ahrens 	zap_attribute_t attr;
   1250    789    ahrens 
   1251    789    ahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
   1252    789    ahrens 		return (ENOENT);
   1253    789    ahrens 
   1254    789    ahrens 	zap_cursor_init_serialized(&cursor,
   1255    789    ahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
   1256    789    ahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
   1257    789    ahrens 
   1258    885    ahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
   1259    885    ahrens 		zap_cursor_fini(&cursor);
   1260    885    ahrens 		return (ENOENT);
   1261    885    ahrens 	}
   1262    885    ahrens 
   1263    885    ahrens 	if (strlen(attr.za_name) + 1 > namelen) {
   1264    885    ahrens 		zap_cursor_fini(&cursor);
   1265    885    ahrens 		return (ENAMETOOLONG);
   1266    885    ahrens 	}
   1267    885    ahrens 
   1268    885    ahrens 	(void) strcpy(name, attr.za_name);
   1269    885    ahrens 	if (idp)
   1270    885    ahrens 		*idp = attr.za_first_integer;
   1271   5663  ck153898 	if (case_conflict)
   1272   5663  ck153898 		*case_conflict = attr.za_normalization_conflict;
   1273    885    ahrens 	zap_cursor_advance(&cursor);
   1274    885    ahrens 	*offp = zap_cursor_serialize(&cursor);
   1275    885    ahrens 	zap_cursor_fini(&cursor);
   1276    885    ahrens 
   1277    885    ahrens 	return (0);
   1278    885    ahrens }
   1279    885    ahrens 
   1280    885    ahrens int
   1281    885    ahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
   1282    885    ahrens     uint64_t *idp, uint64_t *offp)
   1283    885    ahrens {
   1284  10298   Matthew 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
   1285    885    ahrens 	zap_cursor_t cursor;
   1286    885    ahrens 	zap_attribute_t attr;
   1287    885    ahrens 
   1288    885    ahrens 	/* there is no next dir on a snapshot! */
   1289  10298   Matthew 	if (os->os_dsl_dataset->ds_object !=
   1290    885    ahrens 	    dd->dd_phys->dd_head_dataset_obj)
   1291    885    ahrens 		return (ENOENT);
   1292    885    ahrens 
   1293    885    ahrens 	zap_cursor_init_serialized(&cursor,
   1294    885    ahrens 	    dd->dd_pool->dp_meta_objset,
   1295    885    ahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
   1296    885    ahrens 
   1297    885    ahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
   1298    885    ahrens 		zap_cursor_fini(&cursor);
   1299    885    ahrens 		return (ENOENT);
   1300    885    ahrens 	}
   1301    885    ahrens 
   1302    885    ahrens 	if (strlen(attr.za_name) + 1 > namelen) {
   1303    885    ahrens 		zap_cursor_fini(&cursor);
   1304    789    ahrens 		return (ENAMETOOLONG);
   1305    885    ahrens 	}
   1306    789    ahrens 
   1307    789    ahrens 	(void) strcpy(name, attr.za_name);
   1308    885    ahrens 	if (idp)
   1309    885    ahrens 		*idp = attr.za_first_integer;
   1310    789    ahrens 	zap_cursor_advance(&cursor);
   1311    789    ahrens 	*offp = zap_cursor_serialize(&cursor);
   1312    885    ahrens 	zap_cursor_fini(&cursor);
   1313    789    ahrens 
   1314    789    ahrens 	return (0);
   1315    789    ahrens }
   1316    789    ahrens 
   1317   7046    ahrens struct findarg {
   1318   7046    ahrens 	int (*func)(char *, void *);
   1319   7046    ahrens 	void *arg;
   1320   7046    ahrens };
   1321   7046    ahrens 
   1322   7046    ahrens /* ARGSUSED */
   1323   7046    ahrens static int
   1324   7046    ahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
   1325   7046    ahrens {
   1326   7046    ahrens 	struct findarg *fa = arg;
   1327   7046    ahrens 	return (fa->func((char *)dsname, fa->arg));
   1328   7046    ahrens }
   1329   7046    ahrens 
   1330    789    ahrens /*
   1331    789    ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
   1332   7046    ahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
   1333    789    ahrens  */
   1334   2199    ahrens int
   1335   2199    ahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
   1336    789    ahrens {
   1337   7046    ahrens 	struct findarg fa;
   1338   7046    ahrens 	fa.func = func;
   1339   7046    ahrens 	fa.arg = arg;
   1340   7046    ahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
   1341   7046    ahrens }
   1342   7046    ahrens 
   1343   7046    ahrens /*
   1344   7046    ahrens  * Find all objsets under name, call func on each
   1345   7046    ahrens  */
   1346   7046    ahrens int
   1347   7046    ahrens dmu_objset_find_spa(spa_t *spa, const char *name,
   1348   7046    ahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
   1349   7046    ahrens {
   1350    789    ahrens 	dsl_dir_t *dd;
   1351   7046    ahrens 	dsl_pool_t *dp;
   1352   7046    ahrens 	dsl_dataset_t *ds;
   1353    789    ahrens 	zap_cursor_t zc;
   1354   3978  mmusante 	zap_attribute_t *attr;
   1355    789    ahrens 	char *child;
   1356   7046    ahrens 	uint64_t thisobj;
   1357   7046    ahrens 	int err;
   1358    789    ahrens 
   1359   7046    ahrens 	if (name == NULL)
   1360   7046    ahrens 		name = spa_name(spa);
   1361   7046    ahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
   1362   1544  eschrock 	if (err)
   1363   2199    ahrens 		return (err);
   1364    789    ahrens 
   1365   7046    ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
   1366   7046    ahrens 	if (dd->dd_myname[0] == '$') {
   1367   7046    ahrens 		dsl_dir_close(dd, FTAG);
   1368   7046    ahrens 		return (0);
   1369   7046    ahrens 	}
   1370   7046    ahrens 
   1371   7046    ahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
   1372   3978  mmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
   1373   7046    ahrens 	dp = dd->dd_pool;
   1374    789    ahrens 
   1375    789    ahrens 	/*
   1376    789    ahrens 	 * Iterate over all children.
   1377    789    ahrens 	 */
   1378   2417    ahrens 	if (flags & DS_FIND_CHILDREN) {
   1379   7046    ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
   1380   2417    ahrens 		    dd->dd_phys->dd_child_dir_zapobj);
   1381   3978  mmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
   1382   2417    ahrens 		    (void) zap_cursor_advance(&zc)) {
   1383   3978  mmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
   1384   3978  mmusante 			ASSERT(attr->za_num_integers == 1);
   1385    789    ahrens 
   1386  11165   Matthew 			child = kmem_asprintf("%s/%s", name, attr->za_name);
   1387   7046    ahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
   1388  11165   Matthew 			strfree(child);
   1389   2417    ahrens 			if (err)
   1390   2417    ahrens 				break;
   1391   2417    ahrens 		}
   1392   2417    ahrens 		zap_cursor_fini(&zc);
   1393   2199    ahrens 
   1394   2417    ahrens 		if (err) {
   1395   2417    ahrens 			dsl_dir_close(dd, FTAG);
   1396   3978  mmusante 			kmem_free(attr, sizeof (zap_attribute_t));
   1397   2417    ahrens 			return (err);
   1398   2417    ahrens 		}
   1399    789    ahrens 	}
   1400    789    ahrens 
   1401    789    ahrens 	/*
   1402    789    ahrens 	 * Iterate over all snapshots.
   1403    789    ahrens 	 */
   1404   7046    ahrens 	if (flags & DS_FIND_SNAPSHOTS) {
   1405   7046    ahrens 		if (!dsl_pool_sync_context(dp))
   1406   7046    ahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
   1407   7046    ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
   1408   7046    ahrens 		if (!dsl_pool_sync_context(dp))
   1409   7046    ahrens 			rw_exit(&dp->dp_config_rwlock);
   1410    789    ahrens 
   1411   7046    ahrens 		if (err == 0) {
   1412   7046    ahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
   1413   7046    ahrens 			dsl_dataset_rele(ds, FTAG);
   1414    789    ahrens 
   1415   7046    ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
   1416   7046    ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
   1417   7046    ahrens 			    (void) zap_cursor_advance(&zc)) {
   1418   7046    ahrens 				ASSERT(attr->za_integer_length ==
   1419   7046    ahrens 				    sizeof (uint64_t));
   1420   7046    ahrens 				ASSERT(attr->za_num_integers == 1);
   1421    789    ahrens 
   1422  11165   Matthew 				child = kmem_asprintf("%s@%s",
   1423  11165   Matthew 				    name, attr->za_name);
   1424   7046    ahrens 				err = func(spa, attr->za_first_integer,
   1425   7046    ahrens 				    child, arg);
   1426  11165   Matthew 				strfree(child);
   1427   7046    ahrens 				if (err)
   1428   7046    ahrens 					break;
   1429   7046    ahrens 			}
   1430   7046    ahrens 			zap_cursor_fini(&zc);
   1431    789    ahrens 		}
   1432    789    ahrens 	}
   1433    789    ahrens 
   1434    789    ahrens 	dsl_dir_close(dd, FTAG);
   1435   3978  mmusante 	kmem_free(attr, sizeof (zap_attribute_t));
   1436    789    ahrens 
   1437   2199    ahrens 	if (err)
   1438   2199    ahrens 		return (err);
   1439   2199    ahrens 
   1440    789    ahrens 	/*
   1441    789    ahrens 	 * Apply to self if appropriate.
   1442    789    ahrens 	 */
   1443   7046    ahrens 	err = func(spa, thisobj, name, arg);
   1444   2199    ahrens 	return (err);
   1445    789    ahrens }
   1446   5326  ek110237 
   1447   8415   Richard /* ARGSUSED */
   1448   8415   Richard int
   1449   8415   Richard dmu_objset_prefetch(char *name, void *arg)
   1450   8415   Richard {
   1451   8415   Richard 	dsl_dataset_t *ds;
   1452   8415   Richard 
   1453   8415   Richard 	if (dsl_dataset_hold(name, FTAG, &ds))
   1454   8415   Richard 		return (0);
   1455   8415   Richard 
   1456   8415   Richard 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
   1457   8415   Richard 		mutex_enter(&ds->ds_opening_lock);
   1458  10298   Matthew 		if (ds->ds_objset == NULL) {
   1459   8415   Richard 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
   1460   8415   Richard 			zbookmark_t zb;
   1461   8415   Richard 
   1462  10922      Jeff 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
   1463  10922      Jeff 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
   1464   8415   Richard 
   1465   8415   Richard 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
   1466   8415   Richard 			    &ds->ds_phys->ds_bp, NULL, NULL,
   1467   8415   Richard 			    ZIO_PRIORITY_ASYNC_READ,
   1468   8415   Richard 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
   1469   8415   Richard 			    &aflags, &zb);
   1470   8415   Richard 		}
   1471   8415   Richard 		mutex_exit(&ds->ds_opening_lock);
   1472   8415   Richard 	}
   1473   8415   Richard 
   1474   8415   Richard 	dsl_dataset_rele(ds, FTAG);
   1475   8415   Richard 	return (0);
   1476   8415   Richard }
   1477   8415   Richard 
   1478   5326  ek110237 void
   1479   5326  ek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
   1480   5326  ek110237 {
   1481  10298   Matthew 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
   1482  10298   Matthew 	os->os_user_ptr = user_ptr;
   1483   5326  ek110237 }
   1484   5326  ek110237 
   1485   5326  ek110237 void *
   1486   5326  ek110237 dmu_objset_get_user(objset_t *os)
   1487   5326  ek110237 {
   1488  10298   Matthew 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
   1489  10298   Matthew 	return (os->os_user_ptr);
   1490   5326  ek110237 }
   1491