usr/src/uts/common/fs/zfs/dsl_dataset.c
changeset 13700 2889e2596bd6
parent 13686 4bc0783f6064
child 13743 95aba6e49b9f
equal deleted inserted replaced
13699:733714f4dc24 13700:2889e2596bd6
    18  *
    18  *
    19  * CDDL HEADER END
    19  * CDDL HEADER END
    20  */
    20  */
    21 /*
    21 /*
    22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
    22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
    23  * Copyright (c) 2011 by Delphix. All rights reserved.
    23  * Copyright (c) 2012 by Delphix. All rights reserved.
    24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
    24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
    25  */
    25  */
    26 
    26 
    27 #include <sys/dmu_objset.h>
    27 #include <sys/dmu_objset.h>
    28 #include <sys/dsl_dataset.h>
    28 #include <sys/dsl_dataset.h>
    33 #include <sys/dmu_impl.h>
    33 #include <sys/dmu_impl.h>
    34 #include <sys/dmu_tx.h>
    34 #include <sys/dmu_tx.h>
    35 #include <sys/arc.h>
    35 #include <sys/arc.h>
    36 #include <sys/zio.h>
    36 #include <sys/zio.h>
    37 #include <sys/zap.h>
    37 #include <sys/zap.h>
       
    38 #include <sys/zfeature.h>
    38 #include <sys/unique.h>
    39 #include <sys/unique.h>
    39 #include <sys/zfs_context.h>
    40 #include <sys/zfs_context.h>
    40 #include <sys/zfs_ioctl.h>
    41 #include <sys/zfs_ioctl.h>
    41 #include <sys/spa.h>
    42 #include <sys/spa.h>
    42 #include <sys/zfs_znode.h>
    43 #include <sys/zfs_znode.h>
    98 	ASSERT(dmu_tx_is_syncing(tx));
    99 	ASSERT(dmu_tx_is_syncing(tx));
    99 	/* It could have been compressed away to nothing */
   100 	/* It could have been compressed away to nothing */
   100 	if (BP_IS_HOLE(bp))
   101 	if (BP_IS_HOLE(bp))
   101 		return;
   102 		return;
   102 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
   103 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
   103 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
   104 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
   104 	if (ds == NULL) {
   105 	if (ds == NULL) {
   105 		/*
   106 		/*
   106 		 * Account for the meta-objset space in its placeholder
   107 		 * Account for the meta-objset space in its placeholder
   107 		 * dsl_dir.
   108 		 * dsl_dir.
   108 		 */
   109 		 */
   115 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
   116 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
   116 
   117 
   117 	mutex_enter(&ds->ds_dir->dd_lock);
   118 	mutex_enter(&ds->ds_dir->dd_lock);
   118 	mutex_enter(&ds->ds_lock);
   119 	mutex_enter(&ds->ds_lock);
   119 	delta = parent_delta(ds, used);
   120 	delta = parent_delta(ds, used);
   120 	ds->ds_phys->ds_used_bytes += used;
   121 	ds->ds_phys->ds_referenced_bytes += used;
   121 	ds->ds_phys->ds_compressed_bytes += compressed;
   122 	ds->ds_phys->ds_compressed_bytes += compressed;
   122 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
   123 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
   123 	ds->ds_phys->ds_unique_bytes += used;
   124 	ds->ds_phys->ds_unique_bytes += used;
   124 	mutex_exit(&ds->ds_lock);
   125 	mutex_exit(&ds->ds_lock);
   125 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
   126 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
   209 			dsl_dir_transfer_space(ds->ds_dir, used,
   210 			dsl_dir_transfer_space(ds->ds_dir, used,
   210 			    DD_USED_HEAD, DD_USED_SNAP, tx);
   211 			    DD_USED_HEAD, DD_USED_SNAP, tx);
   211 		}
   212 		}
   212 	}
   213 	}
   213 	mutex_enter(&ds->ds_lock);
   214 	mutex_enter(&ds->ds_lock);
   214 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
   215 	ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
   215 	ds->ds_phys->ds_used_bytes -= used;
   216 	ds->ds_phys->ds_referenced_bytes -= used;
   216 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
   217 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
   217 	ds->ds_phys->ds_compressed_bytes -= compressed;
   218 	ds->ds_phys->ds_compressed_bytes -= compressed;
   218 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
   219 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
   219 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
   220 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
   220 	mutex_exit(&ds->ds_lock);
   221 	mutex_exit(&ds->ds_lock);
   816 		dsl_dataset_t *ohds;
   817 		dsl_dataset_t *ohds;
   817 
   818 
   818 		dsphys->ds_prev_snap_obj = origin->ds_object;
   819 		dsphys->ds_prev_snap_obj = origin->ds_object;
   819 		dsphys->ds_prev_snap_txg =
   820 		dsphys->ds_prev_snap_txg =
   820 		    origin->ds_phys->ds_creation_txg;
   821 		    origin->ds_phys->ds_creation_txg;
   821 		dsphys->ds_used_bytes =
   822 		dsphys->ds_referenced_bytes =
   822 		    origin->ds_phys->ds_used_bytes;
   823 		    origin->ds_phys->ds_referenced_bytes;
   823 		dsphys->ds_compressed_bytes =
   824 		dsphys->ds_compressed_bytes =
   824 		    origin->ds_phys->ds_compressed_bytes;
   825 		    origin->ds_phys->ds_compressed_bytes;
   825 		dsphys->ds_uncompressed_bytes =
   826 		dsphys->ds_uncompressed_bytes =
   826 		    origin->ds_phys->ds_uncompressed_bytes;
   827 		    origin->ds_phys->ds_uncompressed_bytes;
   827 		dsphys->ds_bp = origin->ds_phys->ds_bp;
   828 		dsphys->ds_bp = origin->ds_phys->ds_bp;
   931 	dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
   932 	dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
   932 
   933 
   933 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
   934 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
   934 	    pair = nvlist_next_nvpair(snaps, pair)) {
   935 	    pair = nvlist_next_nvpair(snaps, pair)) {
   935 		dsl_dataset_t *ds;
   936 		dsl_dataset_t *ds;
   936 		int err;
       
   937 
   937 
   938 		err = dsl_dataset_own(nvpair_name(pair), B_TRUE, dstg, &ds);
   938 		err = dsl_dataset_own(nvpair_name(pair), B_TRUE, dstg, &ds);
   939 		if (err == 0) {
   939 		if (err == 0) {
   940 			struct dsl_ds_destroyarg *dsda;
   940 			struct dsl_ds_destroyarg *dsda;
   941 
   941 
  1080 	err = dmu_objset_from_ds(ds, &os);
  1080 	err = dmu_objset_from_ds(ds, &os);
  1081 	if (err)
  1081 	if (err)
  1082 		goto out;
  1082 		goto out;
  1083 
  1083 
  1084 	/*
  1084 	/*
  1085 	 * remove the objects in open context, so that we won't
  1085 	 * If async destruction is not enabled try to remove all objects
  1086 	 * have too much to do in syncing context.
  1086 	 * while in the open context so that there is less work to do in
  1087 	 */
  1087 	 * the syncing context.
  1088 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
  1088 	 */
  1089 	    ds->ds_phys->ds_prev_snap_txg)) {
  1089 	if (!spa_feature_is_enabled(dsl_dataset_get_spa(ds),
  1090 		/*
  1090 	    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
  1091 		 * Ignore errors, if there is not enough disk space
  1091 		for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
  1092 		 * we will deal with it in dsl_dataset_destroy_sync().
  1092 		    ds->ds_phys->ds_prev_snap_txg)) {
  1093 		 */
  1093 			/*
  1094 		(void) dmu_free_object(os, obj);
  1094 			 * Ignore errors, if there is not enough disk space
  1095 	}
  1095 			 * we will deal with it in dsl_dataset_destroy_sync().
  1096 	if (err != ESRCH)
  1096 			 */
  1097 		goto out;
  1097 			(void) dmu_free_object(os, obj);
       
  1098 		}
       
  1099 		if (err != ESRCH)
       
  1100 			goto out;
       
  1101 	}
  1098 
  1102 
  1099 	/*
  1103 	/*
  1100 	 * Only the ZIL knows how to free log blocks.
  1104 	 * Only the ZIL knows how to free log blocks.
  1101 	 */
  1105 	 */
  1102 	zil_destroy(dmu_objset_zil(os), B_FALSE);
  1106 	zil_destroy(dmu_objset_zil(os), B_FALSE);
  1238 	uint64_t dlused, dlcomp, dluncomp;
  1242 	uint64_t dlused, dlcomp, dluncomp;
  1239 
  1243 
  1240 	ASSERT(!dsl_dataset_is_snapshot(ds));
  1244 	ASSERT(!dsl_dataset_is_snapshot(ds));
  1241 
  1245 
  1242 	if (ds->ds_phys->ds_prev_snap_obj != 0)
  1246 	if (ds->ds_phys->ds_prev_snap_obj != 0)
  1243 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
  1247 		mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
  1244 	else
  1248 	else
  1245 		mrs_used = 0;
  1249 		mrs_used = 0;
  1246 
  1250 
  1247 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
  1251 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
  1248 
  1252 
  1249 	ASSERT3U(dlused, <=, mrs_used);
  1253 	ASSERT3U(dlused, <=, mrs_used);
  1250 	ds->ds_phys->ds_unique_bytes =
  1254 	ds->ds_phys->ds_unique_bytes =
  1251 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
  1255 	    ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
  1252 
  1256 
  1253 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
  1257 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
  1254 	    SPA_VERSION_UNIQUE_ACCURATE)
  1258 	    SPA_VERSION_UNIQUE_ACCURATE)
  1255 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
  1259 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
  1256 }
  1260 }
  1602 	SWITCH64(ds_next->ds_phys->ds_deadlist_obj,
  1606 	SWITCH64(ds_next->ds_phys->ds_deadlist_obj,
  1603 	    ds->ds_phys->ds_deadlist_obj);
  1607 	    ds->ds_phys->ds_deadlist_obj);
  1604 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
  1608 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
  1605 	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
  1609 	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
  1606 	    ds_next->ds_phys->ds_deadlist_obj);
  1610 	    ds_next->ds_phys->ds_deadlist_obj);
       
  1611 }
       
  1612 
       
  1613 static int
       
  1614 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
       
  1615 {
       
  1616 	int err;
       
  1617 	struct killarg ka;
       
  1618 
       
  1619 	/*
       
  1620 	 * Free everything that we point to (that's born after
       
  1621 	 * the previous snapshot, if we are a clone)
       
  1622 	 *
       
  1623 	 * NB: this should be very quick, because we already
       
  1624 	 * freed all the objects in open context.
       
  1625 	 */
       
  1626 	ka.ds = ds;
       
  1627 	ka.tx = tx;
       
  1628 	err = traverse_dataset(ds,
       
  1629 	    ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
       
  1630 	    kill_blkptr, &ka);
       
  1631 	ASSERT3U(err, ==, 0);
       
  1632 	ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
       
  1633 
       
  1634 	return (err);
  1607 }
  1635 }
  1608 
  1636 
  1609 void
  1637 void
  1610 dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
  1638 dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
  1611 {
  1639 {
  1750 			dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
  1778 			dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
  1751 			    &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
  1779 			    &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
  1752 			    tx);
  1780 			    tx);
  1753 			dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
  1781 			dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
  1754 			    DD_USED_HEAD, used, comp, uncomp, tx);
  1782 			    DD_USED_HEAD, used, comp, uncomp, tx);
  1755 			dsl_dir_dirty(tx->tx_pool->dp_free_dir, tx);
       
  1756 
  1783 
  1757 			/* Merge our deadlist into next's and free it. */
  1784 			/* Merge our deadlist into next's and free it. */
  1758 			dsl_deadlist_merge(&ds_next->ds_deadlist,
  1785 			dsl_deadlist_merge(&ds_next->ds_deadlist,
  1759 			    ds->ds_phys->ds_deadlist_obj, tx);
  1786 			    ds->ds_phys->ds_deadlist_obj, tx);
  1760 		}
  1787 		}
  1826 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
  1853 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
  1827 			}
  1854 			}
  1828 		}
  1855 		}
  1829 		dsl_dataset_rele(ds_next, FTAG);
  1856 		dsl_dataset_rele(ds_next, FTAG);
  1830 	} else {
  1857 	} else {
       
  1858 		zfeature_info_t *async_destroy =
       
  1859 		    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY];
       
  1860 
  1831 		/*
  1861 		/*
  1832 		 * There's no next snapshot, so this is a head dataset.
  1862 		 * There's no next snapshot, so this is a head dataset.
  1833 		 * Destroy the deadlist.  Unless it's a clone, the
  1863 		 * Destroy the deadlist.  Unless it's a clone, the
  1834 		 * deadlist should be empty.  (If it's a clone, it's
  1864 		 * deadlist should be empty.  (If it's a clone, it's
  1835 		 * safe to ignore the deadlist contents.)
  1865 		 * safe to ignore the deadlist contents.)
  1836 		 */
  1866 		 */
  1837 		struct killarg ka;
       
  1838 
       
  1839 		dsl_deadlist_close(&ds->ds_deadlist);
  1867 		dsl_deadlist_close(&ds->ds_deadlist);
  1840 		dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
  1868 		dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
  1841 		ds->ds_phys->ds_deadlist_obj = 0;
  1869 		ds->ds_phys->ds_deadlist_obj = 0;
  1842 
  1870 
  1843 		/*
  1871 		if (!spa_feature_is_enabled(dp->dp_spa, async_destroy)) {
  1844 		 * Free everything that we point to (that's born after
  1872 			err = old_synchronous_dataset_destroy(ds, tx);
  1845 		 * the previous snapshot, if we are a clone)
  1873 		} else {
  1846 		 *
  1874 			/*
  1847 		 * NB: this should be very quick, because we already
  1875 			 * Move the bptree into the pool's list of trees to
  1848 		 * freed all the objects in open context.
  1876 			 * clean up and update space accounting information.
  1849 		 */
  1877 			 */
  1850 		ka.ds = ds;
  1878 			uint64_t used, comp, uncomp;
  1851 		ka.tx = tx;
  1879 
  1852 		err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
  1880 			ASSERT(err == 0 || err == EBUSY);
  1853 		    TRAVERSE_POST, kill_blkptr, &ka);
  1881 			if (!spa_feature_is_active(dp->dp_spa, async_destroy)) {
  1854 		ASSERT3U(err, ==, 0);
  1882 				spa_feature_incr(dp->dp_spa, async_destroy, tx);
  1855 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
  1883 				dp->dp_bptree_obj = bptree_alloc(
  1856 		    ds->ds_phys->ds_unique_bytes == 0);
  1884 				    dp->dp_meta_objset, tx);
       
  1885 				VERIFY(zap_add(dp->dp_meta_objset,
       
  1886 				    DMU_POOL_DIRECTORY_OBJECT,
       
  1887 				    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
       
  1888 				    &dp->dp_bptree_obj, tx) == 0);
       
  1889 			}
       
  1890 
       
  1891 			used = ds->ds_dir->dd_phys->dd_used_bytes;
       
  1892 			comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
       
  1893 			uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
       
  1894 
       
  1895 			ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
       
  1896 			    ds->ds_phys->ds_unique_bytes == used);
       
  1897 
       
  1898 			bptree_add(dp->dp_meta_objset, dp->dp_bptree_obj,
       
  1899 			    &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
       
  1900 			    used, comp, uncomp, tx);
       
  1901 			dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
       
  1902 			    -used, -comp, -uncomp, tx);
       
  1903 			dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
       
  1904 			    used, comp, uncomp, tx);
       
  1905 		}
  1857 
  1906 
  1858 		if (ds->ds_prev != NULL) {
  1907 		if (ds->ds_prev != NULL) {
  1859 			if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
  1908 			if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
  1860 				VERIFY3U(0, ==, zap_remove_int(mos,
  1909 				VERIFY3U(0, ==, zap_remove_int(mos,
  1861 				    ds->ds_prev->ds_dir->dd_phys->dd_clones,
  1910 				    ds->ds_prev->ds_dir->dd_phys->dd_clones,
  2042 	dsphys->ds_next_snap_obj = ds->ds_object;
  2091 	dsphys->ds_next_snap_obj = ds->ds_object;
  2043 	dsphys->ds_num_children = 1;
  2092 	dsphys->ds_num_children = 1;
  2044 	dsphys->ds_creation_time = gethrestime_sec();
  2093 	dsphys->ds_creation_time = gethrestime_sec();
  2045 	dsphys->ds_creation_txg = crtxg;
  2094 	dsphys->ds_creation_txg = crtxg;
  2046 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
  2095 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
  2047 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
  2096 	dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
  2048 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
  2097 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
  2049 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
  2098 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
  2050 	dsphys->ds_flags = ds->ds_phys->ds_flags;
  2099 	dsphys->ds_flags = ds->ds_phys->ds_flags;
  2051 	dsphys->ds_bp = ds->ds_phys->ds_bp;
  2100 	dsphys->ds_bp = ds->ds_phys->ds_bp;
  2052 	dmu_buf_rele(dbuf, FTAG);
  2101 	dmu_buf_rele(dbuf, FTAG);
  2166 	for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
  2215 	for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
  2167 	    zap_cursor_retrieve(&zc, &za) == 0;
  2216 	    zap_cursor_retrieve(&zc, &za) == 0;
  2168 	    zap_cursor_advance(&zc)) {
  2217 	    zap_cursor_advance(&zc)) {
  2169 		dsl_dataset_t *clone;
  2218 		dsl_dataset_t *clone;
  2170 		char buf[ZFS_MAXNAMELEN];
  2219 		char buf[ZFS_MAXNAMELEN];
       
  2220 		/*
       
  2221 		 * Even though we hold the dp_config_rwlock, the dataset
       
  2222 		 * may fail to open, returning ENOENT.  If there is a
       
  2223 		 * thread concurrently attempting to destroy this
       
  2224 		 * dataset, it will have the ds_rwlock held for
       
  2225 		 * RW_WRITER.  Our call to dsl_dataset_hold_obj() ->
       
  2226 		 * dsl_dataset_hold_ref() will fail its
       
  2227 		 * rw_tryenter(&ds->ds_rwlock, RW_READER), drop the
       
  2228 		 * dp_config_rwlock, and wait for the destroy progress
       
  2229 		 * and signal ds_exclusive_cv.  If the destroy was
       
  2230 		 * successful, we will see that
       
  2231 		 * DSL_DATASET_IS_DESTROYED(), and return ENOENT.
       
  2232 		 */
  2171 		if (dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
  2233 		if (dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
  2172 		    za.za_first_integer, FTAG, &clone) != 0) {
  2234 		    za.za_first_integer, FTAG, &clone) != 0)
  2173 			goto fail;
  2235 			continue;
  2174 		}
       
  2175 		dsl_dir_name(clone->ds_dir, buf);
  2236 		dsl_dir_name(clone->ds_dir, buf);
  2176 		VERIFY(nvlist_add_boolean(val, buf) == 0);
  2237 		VERIFY(nvlist_add_boolean(val, buf) == 0);
  2177 		dsl_dataset_rele(clone, FTAG);
  2238 		dsl_dataset_rele(clone, FTAG);
  2178 	}
  2239 	}
  2179 	zap_cursor_fini(&zc);
  2240 	zap_cursor_fini(&zc);
  2292 void
  2353 void
  2293 dsl_dataset_space(dsl_dataset_t *ds,
  2354 dsl_dataset_space(dsl_dataset_t *ds,
  2294     uint64_t *refdbytesp, uint64_t *availbytesp,
  2355     uint64_t *refdbytesp, uint64_t *availbytesp,
  2295     uint64_t *usedobjsp, uint64_t *availobjsp)
  2356     uint64_t *usedobjsp, uint64_t *availobjsp)
  2296 {
  2357 {
  2297 	*refdbytesp = ds->ds_phys->ds_used_bytes;
  2358 	*refdbytesp = ds->ds_phys->ds_referenced_bytes;
  2298 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
  2359 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
  2299 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
  2360 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
  2300 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
  2361 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
  2301 	if (ds->ds_quota != 0) {
  2362 	if (ds->ds_quota != 0) {
  2302 		/*
  2363 		/*
  2629 	 * Which simplifies to:
  2690 	 * Which simplifies to:
  2630 	 * uN + kN + kN-1 + ... + k1 + k0
  2691 	 * uN + kN + kN-1 + ... + k1 + k0
  2631 	 * Note however, if we stop before we reach the ORIGIN we get:
  2692 	 * Note however, if we stop before we reach the ORIGIN we get:
  2632 	 * uN + kN + kN-1 + ... + kM - uM-1
  2693 	 * uN + kN + kN-1 + ... + kM - uM-1
  2633 	 */
  2694 	 */
  2634 	pa->used = origin_ds->ds_phys->ds_used_bytes;
  2695 	pa->used = origin_ds->ds_phys->ds_referenced_bytes;
  2635 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
  2696 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
  2636 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
  2697 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
  2637 	for (snap = list_head(&pa->shared_snaps); snap;
  2698 	for (snap = list_head(&pa->shared_snaps); snap;
  2638 	    snap = list_next(&pa->shared_snaps, snap)) {
  2699 	    snap = list_next(&pa->shared_snaps, snap)) {
  2639 		uint64_t val, dlused, dlcomp, dluncomp;
  2700 		uint64_t val, dlused, dlcomp, dluncomp;
  2663 	/*
  2724 	/*
  2664 	 * If we are a clone of a clone then we never reached ORIGIN,
  2725 	 * If we are a clone of a clone then we never reached ORIGIN,
  2665 	 * so we need to subtract out the clone origin's used space.
  2726 	 * so we need to subtract out the clone origin's used space.
  2666 	 */
  2727 	 */
  2667 	if (pa->origin_origin) {
  2728 	if (pa->origin_origin) {
  2668 		pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
  2729 		pa->used -= pa->origin_origin->ds_phys->ds_referenced_bytes;
  2669 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
  2730 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
  2670 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
  2731 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
  2671 	}
  2732 	}
  2672 
  2733 
  2673 	/* Check that there is enough space here */
  2734 	/* Check that there is enough space here */
  3179 		dsl_deadlist_space(&csa->cds->ds_deadlist,
  3240 		dsl_deadlist_space(&csa->cds->ds_deadlist,
  3180 		    &cdl_used, &cdl_comp, &cdl_uncomp);
  3241 		    &cdl_used, &cdl_comp, &cdl_uncomp);
  3181 		dsl_deadlist_space(&csa->ohds->ds_deadlist,
  3242 		dsl_deadlist_space(&csa->ohds->ds_deadlist,
  3182 		    &odl_used, &odl_comp, &odl_uncomp);
  3243 		    &odl_used, &odl_comp, &odl_uncomp);
  3183 
  3244 
  3184 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
  3245 		dused = csa->cds->ds_phys->ds_referenced_bytes + cdl_used -
  3185 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
  3246 		    (csa->ohds->ds_phys->ds_referenced_bytes + odl_used);
  3186 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
  3247 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
  3187 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
  3248 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
  3188 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
  3249 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
  3189 		    cdl_uncomp -
  3250 		    cdl_uncomp -
  3190 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
  3251 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
  3209 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
  3270 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
  3210 		    DD_USED_HEAD, DD_USED_SNAP, tx);
  3271 		    DD_USED_HEAD, DD_USED_SNAP, tx);
  3211 	}
  3272 	}
  3212 
  3273 
  3213 	/* swap ds_*_bytes */
  3274 	/* swap ds_*_bytes */
  3214 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
  3275 	SWITCH64(csa->ohds->ds_phys->ds_referenced_bytes,
  3215 	    csa->cds->ds_phys->ds_used_bytes);
  3276 	    csa->cds->ds_phys->ds_referenced_bytes);
  3216 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
  3277 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
  3217 	    csa->cds->ds_phys->ds_compressed_bytes);
  3278 	    csa->cds->ds_phys->ds_compressed_bytes);
  3218 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
  3279 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
  3219 	    csa->cds->ds_phys->ds_uncompressed_bytes);
  3280 	    csa->cds->ds_phys->ds_uncompressed_bytes);
  3220 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
  3281 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
  3339 	 * If they are requesting more space, and our current estimate
  3400 	 * If they are requesting more space, and our current estimate
  3340 	 * is over quota, they get to try again unless the actual
  3401 	 * is over quota, they get to try again unless the actual
  3341 	 * on-disk is over quota and there are no pending changes (which
  3402 	 * on-disk is over quota and there are no pending changes (which
  3342 	 * may free up space for us).
  3403 	 * may free up space for us).
  3343 	 */
  3404 	 */
  3344 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
  3405 	if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
  3345 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
  3406 		if (inflight > 0 ||
       
  3407 		    ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
  3346 			error = ERESTART;
  3408 			error = ERESTART;
  3347 		else
  3409 		else
  3348 			error = EDQUOT;
  3410 			error = EDQUOT;
  3349 	}
  3411 	}
  3350 	mutex_exit(&ds->ds_lock);
  3412 	mutex_exit(&ds->ds_lock);
  3367 		return (err);
  3429 		return (err);
  3368 
  3430 
  3369 	if (psa->psa_effective_value == 0)
  3431 	if (psa->psa_effective_value == 0)
  3370 		return (0);
  3432 		return (0);
  3371 
  3433 
  3372 	if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes ||
  3434 	if (psa->psa_effective_value < ds->ds_phys->ds_referenced_bytes ||
  3373 	    psa->psa_effective_value < ds->ds_reserved)
  3435 	    psa->psa_effective_value < ds->ds_reserved)
  3374 		return (ENOSPC);
  3436 		return (ENOSPC);
  3375 
  3437 
  3376 	return (0);
  3438 	return (0);
  3377 }
  3439 }
  4121 	int err = 0;
  4183 	int err = 0;
  4122 	uint64_t snapobj;
  4184 	uint64_t snapobj;
  4123 	dsl_pool_t *dp = new->ds_dir->dd_pool;
  4185 	dsl_pool_t *dp = new->ds_dir->dd_pool;
  4124 
  4186 
  4125 	*usedp = 0;
  4187 	*usedp = 0;
  4126 	*usedp += new->ds_phys->ds_used_bytes;
  4188 	*usedp += new->ds_phys->ds_referenced_bytes;
  4127 	*usedp -= oldsnap->ds_phys->ds_used_bytes;
  4189 	*usedp -= oldsnap->ds_phys->ds_referenced_bytes;
  4128 
  4190 
  4129 	*compp = 0;
  4191 	*compp = 0;
  4130 	*compp += new->ds_phys->ds_compressed_bytes;
  4192 	*compp += new->ds_phys->ds_compressed_bytes;
  4131 	*compp -= oldsnap->ds_phys->ds_compressed_bytes;
  4193 	*compp -= oldsnap->ds_phys->ds_compressed_bytes;
  4132 
  4194 
  4138 	snapobj = new->ds_object;
  4200 	snapobj = new->ds_object;
  4139 	while (snapobj != oldsnap->ds_object) {
  4201 	while (snapobj != oldsnap->ds_object) {
  4140 		dsl_dataset_t *snap;
  4202 		dsl_dataset_t *snap;
  4141 		uint64_t used, comp, uncomp;
  4203 		uint64_t used, comp, uncomp;
  4142 
  4204 
  4143 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
  4205 		if (snapobj == new->ds_object) {
  4144 		if (err != 0)
  4206 			snap = new;
  4145 			break;
  4207 		} else {
       
  4208 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
       
  4209 			if (err != 0)
       
  4210 				break;
       
  4211 		}
  4146 
  4212 
  4147 		if (snap->ds_phys->ds_prev_snap_txg ==
  4213 		if (snap->ds_phys->ds_prev_snap_txg ==
  4148 		    oldsnap->ds_phys->ds_creation_txg) {
  4214 		    oldsnap->ds_phys->ds_creation_txg) {
  4149 			/*
  4215 			/*
  4150 			 * The blocks in the deadlist can not be born after
  4216 			 * The blocks in the deadlist can not be born after
  4169 		 * If we get to the beginning of the chain of snapshots
  4235 		 * If we get to the beginning of the chain of snapshots
  4170 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
  4236 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
  4171 		 * was not a snapshot of/before new.
  4237 		 * was not a snapshot of/before new.
  4172 		 */
  4238 		 */
  4173 		snapobj = snap->ds_phys->ds_prev_snap_obj;
  4239 		snapobj = snap->ds_phys->ds_prev_snap_obj;
  4174 		dsl_dataset_rele(snap, FTAG);
  4240 		if (snap != new)
       
  4241 			dsl_dataset_rele(snap, FTAG);
  4175 		if (snapobj == 0) {
  4242 		if (snapobj == 0) {
  4176 			err = EINVAL;
  4243 			err = EINVAL;
  4177 			break;
  4244 			break;
  4178 		}
  4245 		}
  4179 
  4246