Print this page
2882 implement libzfs_core
2883 changing "canmount" property to "on" should not always remount dataset
2900 "zfs snapshot" should be able to create multiple, arbitrary snapshots at once
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Chris Siden <christopher.siden@delphix.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Bill Pijewski <wdp@joyent.com>
Reviewed by: Dan Kruchinin <dan.kruchinin@gmail.com>


 897          * data from the origin snapshots zil header.
 898          */
 899         if (origin != NULL) {
 900                 dsl_dataset_t *ds;
 901                 objset_t *os;
 902 
 903                 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
 904                 VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
 905                 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
 906                 dsl_dataset_dirty(ds, tx);
 907                 dsl_dataset_rele(ds, FTAG);
 908         }
 909 
 910         return (dsobj);
 911 }
 912 
 913 /*
 914  * The snapshots must all be in the same pool.
 915  */
 916 int
 917 dmu_snapshots_destroy_nvl(nvlist_t *snaps, boolean_t defer, char *failed)

 918 {
 919         int err;
 920         dsl_sync_task_t *dst;
 921         spa_t *spa;
 922         nvpair_t *pair;
 923         dsl_sync_task_group_t *dstg;
 924 
 925         pair = nvlist_next_nvpair(snaps, NULL);
 926         if (pair == NULL)
 927                 return (0);
 928 
 929         err = spa_open(nvpair_name(pair), &spa, FTAG);
 930         if (err)
 931                 return (err);
 932         dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
 933 
 934         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
 935             pair = nvlist_next_nvpair(snaps, pair)) {
 936                 dsl_dataset_t *ds;
 937 
 938                 err = dsl_dataset_own(nvpair_name(pair), B_TRUE, dstg, &ds);
 939                 if (err == 0) {
 940                         struct dsl_ds_destroyarg *dsda;
 941 
 942                         dsl_dataset_make_exclusive(ds, dstg);
 943                         dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg),
 944                             KM_SLEEP);
 945                         dsda->ds = ds;
 946                         dsda->defer = defer;
 947                         dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
 948                             dsl_dataset_destroy_sync, dsda, dstg, 0);
 949                 } else if (err == ENOENT) {
 950                         err = 0;
 951                 } else {
 952                         (void) strcpy(failed, nvpair_name(pair));
 953                         break;
 954                 }
 955         }
 956 
 957         if (err == 0)
 958                 err = dsl_sync_task_group_wait(dstg);
 959 
 960         for (dst = list_head(&dstg->dstg_tasks); dst;
 961             dst = list_next(&dstg->dstg_tasks, dst)) {
 962                 struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
 963                 dsl_dataset_t *ds = dsda->ds;
 964 
 965                 /*
 966                  * Return the file system name that triggered the error
 967                  */
 968                 if (dst->dst_err) {
 969                         dsl_dataset_name(ds, failed);


 970                 }
 971                 ASSERT3P(dsda->rm_origin, ==, NULL);
 972                 dsl_dataset_disown(ds, dstg);
 973                 kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
 974         }
 975 
 976         dsl_sync_task_group_destroy(dstg);
 977         spa_close(spa, FTAG);
 978         return (err);
 979 
 980 }
 981 
 982 static boolean_t
 983 dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
 984 {
 985         boolean_t might_destroy = B_FALSE;
 986 
 987         mutex_enter(&ds->ds_lock);
 988         if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
 989             DS_IS_DEFER_DESTROY(ds))


1028                 dsda->rm_origin = origin;
1029                 dsl_dataset_make_exclusive(origin, tag);
1030         }
1031 
1032         return (0);
1033 }
1034 
1035 /*
1036  * ds must be opened as OWNER.  On return (whether successful or not),
1037  * ds will be closed and caller can no longer dereference it.
1038  */
1039 int
1040 dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
1041 {
1042         int err;
1043         dsl_sync_task_group_t *dstg;
1044         objset_t *os;
1045         dsl_dir_t *dd;
1046         uint64_t obj;
1047         struct dsl_ds_destroyarg dsda = { 0 };
1048         dsl_dataset_t dummy_ds = { 0 };
1049 
1050         dsda.ds = ds;
1051 
1052         if (dsl_dataset_is_snapshot(ds)) {
1053                 /* Destroying a snapshot is simpler */
1054                 dsl_dataset_make_exclusive(ds, tag);
1055 
1056                 dsda.defer = defer;
1057                 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1058                     dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
1059                     &dsda, tag, 0);
1060                 ASSERT3P(dsda.rm_origin, ==, NULL);
1061                 goto out;
1062         } else if (defer) {
1063                 err = EINVAL;
1064                 goto out;
1065         }
1066 
1067         dd = ds->ds_dir;
1068         dummy_ds.ds_dir = dd;
1069         dummy_ds.ds_object = ds->ds_object;
1070 
1071         /*
1072          * Check for errors and mark this ds as inconsistent, in
1073          * case we crash while freeing the objects.
1074          */
1075         err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
1076             dsl_dataset_destroy_begin_sync, ds, NULL, 0);
1077         if (err)
1078                 goto out;
1079 
1080         err = dmu_objset_from_ds(ds, &os);
1081         if (err)
1082                 goto out;
1083 
1084         /*
1085          * If async destruction is not enabled try to remove all objects
1086          * while in the open context so that there is less work to do in
1087          * the syncing context.
1088          */
1089         if (!spa_feature_is_enabled(dsl_dataset_get_spa(ds),


1136          */
1137         dsl_dataset_make_exclusive(ds, tag);
1138         /*
1139          * If we're removing a clone, we might also need to remove its
1140          * origin.
1141          */
1142         do {
1143                 dsda.need_prep = B_FALSE;
1144                 if (dsl_dir_is_clone(dd)) {
1145                         err = dsl_dataset_origin_rm_prep(&dsda, tag);
1146                         if (err) {
1147                                 dsl_dir_close(dd, FTAG);
1148                                 goto out;
1149                         }
1150                 }
1151 
1152                 dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
1153                 dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
1154                     dsl_dataset_destroy_sync, &dsda, tag, 0);
1155                 dsl_sync_task_create(dstg, dsl_dir_destroy_check,
1156                     dsl_dir_destroy_sync, &dummy_ds, FTAG, 0);
1157                 err = dsl_sync_task_group_wait(dstg);
1158                 dsl_sync_task_group_destroy(dstg);
1159 
1160                 /*
1161                  * We could be racing against 'zfs release' or 'zfs destroy -d'
1162                  * on the origin snap, in which case we can get EBUSY if we
1163                  * needed to destroy the origin snap but were not ready to
1164                  * do so.
1165                  */
1166                 if (dsda.need_prep) {
1167                         ASSERT(err == EBUSY);
1168                         ASSERT(dsl_dir_is_clone(dd));
1169                         ASSERT(dsda.rm_origin == NULL);
1170                 }
1171         } while (dsda.need_prep);
1172 
1173         if (dsda.rm_origin != NULL)
1174                 dsl_dataset_disown(dsda.rm_origin, tag);
1175 
1176         /* if it is successful, dsl_dir_destroy_sync will close the dd */


1311 
1312         /*
1313          * This is really a dsl_dir thing, but check it here so that
1314          * we'll be less likely to leave this dataset inconsistent &
1315          * nearly destroyed.
1316          */
1317         err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
1318         if (err)
1319                 return (err);
1320         if (count != 0)
1321                 return (EEXIST);
1322 
1323         return (0);
1324 }
1325 
1326 /* ARGSUSED */
1327 static void
1328 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1329 {
1330         dsl_dataset_t *ds = arg1;
1331         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1332 
1333         /* Mark it as inconsistent on-disk, in case we crash */
1334         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1335         ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1336 
1337         spa_history_log_internal(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
1338             "dataset = %llu", ds->ds_object);
1339 }
1340 
1341 static int
1342 dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
1343     dmu_tx_t *tx)
1344 {
1345         dsl_dataset_t *ds = dsda->ds;
1346         dsl_dataset_t *ds_prev = ds->ds_prev;
1347 
1348         if (dsl_dataset_might_destroy_origin(ds_prev)) {
1349                 struct dsl_ds_destroyarg ndsda = {0};
1350 
1351                 /*
1352                  * If we're not prepared to remove the origin, don't remove
1353                  * the clone either.
1354                  */
1355                 if (dsda->rm_origin == NULL) {
1356                         dsda->need_prep = B_TRUE;
1357                         return (EBUSY);
1358                 }


1643         int after_branch_point = FALSE;
1644         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1645         objset_t *mos = dp->dp_meta_objset;
1646         dsl_dataset_t *ds_prev = NULL;
1647         boolean_t wont_destroy;
1648         uint64_t obj;
1649 
1650         wont_destroy = (dsda->defer &&
1651             (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1));
1652 
1653         ASSERT(ds->ds_owner || wont_destroy);
1654         ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
1655         ASSERT(ds->ds_prev == NULL ||
1656             ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
1657         ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
1658 
1659         if (wont_destroy) {
1660                 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
1661                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1662                 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;

1663                 return;
1664         }
1665 



1666         /* signal any waiters that this dataset is going away */
1667         mutex_enter(&ds->ds_lock);
1668         ds->ds_owner = dsl_reaper;
1669         cv_broadcast(&ds->ds_exclusive_cv);
1670         mutex_exit(&ds->ds_lock);
1671 
1672         /* Remove our reservation */
1673         if (ds->ds_reserved != 0) {
1674                 dsl_prop_setarg_t psa;
1675                 uint64_t value = 0;
1676 
1677                 dsl_prop_setarg_init_uint64(&psa, "refreservation",
1678                     (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
1679                     &value);
1680                 psa.psa_effective_value = 0;    /* predict default value */
1681 
1682                 dsl_dataset_set_reservation_sync(ds, &psa, tx);
1683                 ASSERT3U(ds->ds_reserved, ==, 0);
1684         }
1685 


1940                 VERIFY(0 == dsl_dataset_get_snapname(ds));
1941 #ifdef ZFS_DEBUG
1942                 {
1943                         uint64_t val;
1944 
1945                         err = dsl_dataset_snap_lookup(ds_head,
1946                             ds->ds_snapname, &val);
1947                         ASSERT3U(err, ==, 0);
1948                         ASSERT3U(val, ==, obj);
1949                 }
1950 #endif
1951                 err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1952                 ASSERT(err == 0);
1953                 dsl_dataset_rele(ds_head, FTAG);
1954         }
1955 
1956         if (ds_prev && ds->ds_prev != ds_prev)
1957                 dsl_dataset_rele(ds_prev, FTAG);
1958 
1959         spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
1960         spa_history_log_internal(LOG_DS_DESTROY, dp->dp_spa, tx,
1961             "dataset = %llu", ds->ds_object);
1962 
1963         if (ds->ds_phys->ds_next_clones_obj != 0) {
1964                 uint64_t count;
1965                 ASSERT(0 == zap_count(mos,
1966                     ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
1967                 VERIFY(0 == dmu_object_free(mos,
1968                     ds->ds_phys->ds_next_clones_obj, tx));
1969         }
1970         if (ds->ds_phys->ds_props_obj != 0)
1971                 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
1972         if (ds->ds_phys->ds_userrefs_obj != 0)
1973                 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
1974         dsl_dir_close(ds->ds_dir, ds);
1975         ds->ds_dir = NULL;
1976         dsl_dataset_drain_refs(ds, tag);
1977         VERIFY(0 == dmu_object_free(mos, obj, tx));
1978 
1979         if (dsda->rm_origin) {
1980                 /*
1981                  * Remove the origin of the clone we just destroyed.


1989 
1990 static int
1991 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1992 {
1993         uint64_t asize;
1994 
1995         if (!dmu_tx_is_syncing(tx))
1996                 return (0);
1997 
1998         /*
1999          * If there's an fs-only reservation, any blocks that might become
2000          * owned by the snapshot dataset must be accommodated by space
2001          * outside of the reservation.
2002          */
2003         ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
2004         asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
2005         if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
2006                 return (ENOSPC);
2007 
2008         /*
2009          * Propogate any reserved space for this snapshot to other
2010          * snapshot checks in this sync group.
2011          */
2012         if (asize > 0)
2013                 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
2014 
2015         return (0);
2016 }
2017 
2018 int
2019 dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)

2020 {
2021         dsl_dataset_t *ds = arg1;
2022         const char *snapname = arg2;
2023         int err;
2024         uint64_t value;
2025 
2026         /*
2027          * We don't allow multiple snapshots of the same txg.  If there
2028          * is already one, try again.
2029          */
2030         if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
2031                 return (EAGAIN);
2032 
2033         /*
2034          * Check for conflicting name snapshot name.
2035          */
2036         err = dsl_dataset_snap_lookup(ds, snapname, &value);
2037         if (err == 0)
2038                 return (EEXIST);
2039         if (err != ENOENT)
2040                 return (err);
2041 
2042         /*
2043          * Check that the dataset's name is not too long.  Name consists
2044          * of the dataset's length + 1 for the @-sign + snapshot name's length
2045          */
2046         if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
2047                 return (ENAMETOOLONG);
2048 
2049         err = dsl_dataset_snapshot_reserve_space(ds, tx);
2050         if (err)
2051                 return (err);
2052 
2053         ds->ds_trysnap_txg = tx->tx_txg;
2054         return (0);
2055 }
2056 
2057 void
2058 dsl_dataset_snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)

2059 {
2060         dsl_dataset_t *ds = arg1;
2061         const char *snapname = arg2;
2062         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2063         dmu_buf_t *dbuf;
2064         dsl_dataset_phys_t *dsphys;
2065         uint64_t dsobj, crtxg;
2066         objset_t *mos = dp->dp_meta_objset;
2067         int err;
2068 
2069         ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
2070 
2071         /*
2072          * The origin's ds_creation_txg has to be < TXG_INITIAL
2073          */
2074         if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
2075                 crtxg = 1;
2076         else
2077                 crtxg = tx->tx_txg;
2078 
2079         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
2080             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
2081         VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));


2147         ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
2148         ds->ds_phys->ds_prev_snap_obj = dsobj;
2149         ds->ds_phys->ds_prev_snap_txg = crtxg;
2150         ds->ds_phys->ds_unique_bytes = 0;
2151         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
2152                 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
2153 
2154         err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
2155             snapname, 8, 1, &dsobj, tx);
2156         ASSERT(err == 0);
2157 
2158         if (ds->ds_prev)
2159                 dsl_dataset_drop_ref(ds->ds_prev, ds);
2160         VERIFY(0 == dsl_dataset_get_ref(dp,
2161             ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
2162 
2163         dsl_scan_ds_snapshotted(ds, tx);
2164 
2165         dsl_dir_snap_cmtime_update(ds->ds_dir);
2166 
2167         spa_history_log_internal(LOG_DS_SNAPSHOT, dp->dp_spa, tx,
2168             "dataset = %llu", dsobj);
2169 }
2170 
2171 void
2172 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
2173 {
2174         ASSERT(dmu_tx_is_syncing(tx));
2175         ASSERT(ds->ds_objset != NULL);
2176         ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
2177 
2178         /*
2179          * in case we had to change ds_fsid_guid when we opened it,
2180          * sync it out now.
2181          */
2182         dmu_buf_will_dirty(ds->ds_dbuf, tx);
2183         ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
2184 
2185         dsl_dir_dirty(ds->ds_dir, tx);
2186         dmu_objset_sync(ds->ds_objset, zio, tx);
2187 }
2188 


2235                         continue;
2236                 dsl_dir_name(clone->ds_dir, buf);
2237                 VERIFY(nvlist_add_boolean(val, buf) == 0);
2238                 dsl_dataset_rele(clone, FTAG);
2239         }
2240         zap_cursor_fini(&zc);
2241         VERIFY(nvlist_add_nvlist(propval, ZPROP_VALUE, val) == 0);
2242         VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
2243             propval) == 0);
2244 fail:
2245         nvlist_free(val);
2246         nvlist_free(propval);
2247         rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
2248 }
2249 
2250 void
2251 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2252 {
2253         uint64_t refd, avail, uobjs, aobjs, ratio;
2254 












2255         dsl_dir_stats(ds->ds_dir, nv);

2256 
2257         dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
2258         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
2259         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
2260 
2261         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2262             ds->ds_phys->ds_creation_time);
2263         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2264             ds->ds_phys->ds_creation_txg);
2265         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2266             ds->ds_quota);
2267         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2268             ds->ds_reserved);
2269         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2270             ds->ds_phys->ds_guid);
2271         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2272             ds->ds_phys->ds_unique_bytes);
2273         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2274             ds->ds_object);
2275         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,


2280         if (ds->ds_phys->ds_prev_snap_obj != 0) {
2281                 uint64_t written, comp, uncomp;
2282                 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2283                 dsl_dataset_t *prev;
2284 
2285                 rw_enter(&dp->dp_config_rwlock, RW_READER);
2286                 int err = dsl_dataset_hold_obj(dp,
2287                     ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
2288                 rw_exit(&dp->dp_config_rwlock);
2289                 if (err == 0) {
2290                         err = dsl_dataset_space_written(prev, ds, &written,
2291                             &comp, &uncomp);
2292                         dsl_dataset_rele(prev, FTAG);
2293                         if (err == 0) {
2294                                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2295                                     written);
2296                         }
2297                 }
2298         }
2299 
2300         ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
2301             (ds->ds_phys->ds_uncompressed_bytes * 100 /
2302             ds->ds_phys->ds_compressed_bytes);
2303         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
2304 
2305         if (ds->ds_phys->ds_next_snap_obj) {
2306                 /*
2307                  * This is a snapshot; override the dd's space used with
2308                  * our unique space and compression ratio.
2309                  */
2310                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2311                     ds->ds_phys->ds_unique_bytes);
2312                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
2313 
2314                 get_clones_stat(ds, nv);
2315         }
2316 }
2317 
2318 void
2319 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2320 {
2321         stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
2322         stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
2323         stat->dds_guid = ds->ds_phys->ds_guid;
2324         if (ds->ds_phys->ds_next_snap_obj) {

2325                 stat->dds_is_snapshot = B_TRUE;
2326                 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
2327         } else {
2328                 stat->dds_is_snapshot = B_FALSE;
2329                 stat->dds_num_clones = 0;
2330         }
2331 
2332         /* clone origin is really a dsl_dir thing... */
2333         rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
2334         if (dsl_dir_is_clone(ds->ds_dir)) {
2335                 dsl_dataset_t *ods;
2336 
2337                 VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
2338                     ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
2339                 dsl_dataset_name(ods, stat->dds_origin);
2340                 dsl_dataset_drop_ref(ods, FTAG);
2341         } else {
2342                 stat->dds_origin[0] = '\0';
2343         }
2344         rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);

2345 }
2346 
2347 uint64_t
2348 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2349 {
2350         return (ds->ds_fsid_guid);
2351 }
2352 
2353 void
2354 dsl_dataset_space(dsl_dataset_t *ds,
2355     uint64_t *refdbytesp, uint64_t *availbytesp,
2356     uint64_t *usedobjsp, uint64_t *availobjsp)
2357 {
2358         *refdbytesp = ds->ds_phys->ds_referenced_bytes;
2359         *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2360         if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
2361                 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
2362         if (ds->ds_quota != 0) {
2363                 /*
2364                  * Adjust available bytes according to refquota


2441         dsl_dir_t *dd = ds->ds_dir;
2442         objset_t *mos = dd->dd_pool->dp_meta_objset;
2443         dsl_dataset_t *hds;
2444         int err;
2445 
2446         ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2447 
2448         VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
2449             dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2450 
2451         VERIFY(0 == dsl_dataset_get_snapname(ds));
2452         err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2453         ASSERT3U(err, ==, 0);
2454         mutex_enter(&ds->ds_lock);
2455         (void) strcpy(ds->ds_snapname, newsnapname);
2456         mutex_exit(&ds->ds_lock);
2457         err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
2458             ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2459         ASSERT3U(err, ==, 0);
2460 
2461         spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
2462             "dataset = %llu", ds->ds_object);
2463         dsl_dataset_rele(hds, FTAG);
2464 }
2465 
2466 struct renamesnaparg {
2467         dsl_sync_task_group_t *dstg;
2468         char failed[MAXPATHLEN];
2469         char *oldsnap;
2470         char *newsnap;
2471 };
2472 
2473 static int
2474 dsl_snapshot_rename_one(const char *name, void *arg)
2475 {
2476         struct renamesnaparg *ra = arg;
2477         dsl_dataset_t *ds = NULL;
2478         char *snapname;
2479         int err;
2480 
2481         snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
2482         (void) strlcpy(ra->failed, snapname, sizeof (ra->failed));


2922 
2923         delta = pa->cloneusedsnap -
2924             dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2925         ASSERT3S(delta, >=, 0);
2926         ASSERT3U(pa->used, >=, delta);
2927         dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2928         dsl_dir_diduse_space(dd, DD_USED_HEAD,
2929             pa->used - delta, pa->comp, pa->uncomp, tx);
2930 
2931         delta = pa->originusedsnap -
2932             odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2933         ASSERT3S(delta, <=, 0);
2934         ASSERT3U(pa->used, >=, -delta);
2935         dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2936         dsl_dir_diduse_space(odd, DD_USED_HEAD,
2937             -pa->used - delta, -pa->comp, -pa->uncomp, tx);
2938 
2939         origin_ds->ds_phys->ds_unique_bytes = pa->unique;
2940 
2941         /* log history record */
2942         spa_history_log_internal(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
2943             "dataset = %llu", hds->ds_object);
2944 
2945         dsl_dir_close(odd, FTAG);
2946 }
2947 
2948 static char *snaplist_tag = "snaplist";
2949 /*
2950  * Make a list of dsl_dataset_t's for the snapshots between first_obj
2951  * (exclusive) and last_obj (inclusive).  The list will be in reverse
2952  * order (last_obj will be the list_head()).  If first_obj == 0, do all
2953  * snapshots back to this dataset's origin.
2954  */
2955 static int
2956 snaplist_make(dsl_pool_t *dp, boolean_t own,
2957     uint64_t first_obj, uint64_t last_obj, list_t *l)
2958 {
2959         uint64_t obj = last_obj;
2960 
2961         ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
2962 
2963         list_create(l, sizeof (struct promotenode),


3281         SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
3282             csa->cds->ds_phys->ds_unique_bytes);
3283 
3284         /* apply any parent delta for change in unconsumed refreservation */
3285         dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
3286             csa->unused_refres_delta, 0, 0, tx);
3287 
3288         /*
3289          * Swap deadlists.
3290          */
3291         dsl_deadlist_close(&csa->cds->ds_deadlist);
3292         dsl_deadlist_close(&csa->ohds->ds_deadlist);
3293         SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
3294             csa->cds->ds_phys->ds_deadlist_obj);
3295         dsl_deadlist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
3296             csa->cds->ds_phys->ds_deadlist_obj);
3297         dsl_deadlist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
3298             csa->ohds->ds_phys->ds_deadlist_obj);
3299 
3300         dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx);



3301 }
3302 
3303 /*
3304  * Swap 'clone' with its origin head datasets.  Used at the end of "zfs
3305  * recv" into an existing fs to swizzle the file system to the new
3306  * version, and by "zfs rollback".  Can also be used to swap two
3307  * independent head datasets if neither has any snapshots.
3308  */
3309 int
3310 dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
3311     boolean_t force)
3312 {
3313         struct cloneswaparg csa;
3314         int error;
3315 
3316         ASSERT(clone->ds_owner);
3317         ASSERT(origin_head->ds_owner);
3318 retry:
3319         /*
3320          * Need exclusive access for the swap. If we're swapping these


3437 
3438         return (0);
3439 }
3440 
3441 extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *);
3442 
3443 void
3444 dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3445 {
3446         dsl_dataset_t *ds = arg1;
3447         dsl_prop_setarg_t *psa = arg2;
3448         uint64_t effective_value = psa->psa_effective_value;
3449 
3450         dsl_prop_set_sync(ds, psa, tx);
3451         DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3452 
3453         if (ds->ds_quota != effective_value) {
3454                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3455                 ds->ds_quota = effective_value;
3456 
3457                 spa_history_log_internal(LOG_DS_REFQUOTA,
3458                     ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu ",
3459                     (longlong_t)ds->ds_quota, ds->ds_object);
3460         }
3461 }
3462 
3463 int
3464 dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
3465 {
3466         dsl_dataset_t *ds;
3467         dsl_prop_setarg_t psa;
3468         int err;
3469 
3470         dsl_prop_setarg_init_uint64(&psa, "refquota", source, &quota);
3471 
3472         err = dsl_dataset_hold(dsname, FTAG, &ds);
3473         if (err)
3474                 return (err);
3475 
3476         /*
3477          * If someone removes a file, then tries to set the quota, we
3478          * want to make sure the file freeing takes effect.
3479          */


3544         uint64_t unique;
3545         int64_t delta;
3546 
3547         dsl_prop_set_sync(ds, psa, tx);
3548         DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3549 
3550         dmu_buf_will_dirty(ds->ds_dbuf, tx);
3551 
3552         mutex_enter(&ds->ds_dir->dd_lock);
3553         mutex_enter(&ds->ds_lock);
3554         ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3555         unique = ds->ds_phys->ds_unique_bytes;
3556         delta = MAX(0, (int64_t)(effective_value - unique)) -
3557             MAX(0, (int64_t)(ds->ds_reserved - unique));
3558         ds->ds_reserved = effective_value;
3559         mutex_exit(&ds->ds_lock);
3560 
3561         dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3562         mutex_exit(&ds->ds_dir->dd_lock);
3563 
3564         spa_history_log_internal(LOG_DS_REFRESERV,
3565             ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu",
3566             (longlong_t)effective_value, ds->ds_object);
3567 }
3568 
3569 int
3570 dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
3571     uint64_t reservation)
3572 {
3573         dsl_dataset_t *ds;
3574         dsl_prop_setarg_t psa;
3575         int err;
3576 
3577         dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
3578             &reservation);
3579 
3580         err = dsl_dataset_hold(dsname, FTAG, &ds);
3581         if (err)
3582                 return (err);
3583 
3584         err = dsl_sync_task_do(ds->ds_dir->dd_pool,
3585             dsl_dataset_set_reservation_check,
3586             dsl_dataset_set_reservation_sync, ds, &psa, 0);


3612         zfs_hold_cleanup_arg_t *ca;
3613 
3614         ca = kmem_alloc(sizeof (zfs_hold_cleanup_arg_t), KM_SLEEP);
3615         ca->dp = ds->ds_dir->dd_pool;
3616         ca->dsobj = ds->ds_object;
3617         (void) strlcpy(ca->htag, htag, sizeof (ca->htag));
3618         VERIFY3U(0, ==, zfs_onexit_add_cb(minor,
3619             dsl_dataset_user_release_onexit, ca, NULL));
3620 }
3621 
3622 /*
3623  * If you add new checks here, you may need to add
3624  * additional checks to the "temporary" case in
3625  * snapshot_check() in dmu_objset.c.
3626  */
3627 static int
3628 dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
3629 {
3630         dsl_dataset_t *ds = arg1;
3631         struct dsl_ds_holdarg *ha = arg2;
3632         char *htag = ha->htag;
3633         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3634         int error = 0;
3635 
3636         if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3637                 return (ENOTSUP);
3638 
3639         if (!dsl_dataset_is_snapshot(ds))
3640                 return (EINVAL);
3641 
3642         /* tags must be unique */
3643         mutex_enter(&ds->ds_lock);
3644         if (ds->ds_phys->ds_userrefs_obj) {
3645                 error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
3646                     8, 1, tx);
3647                 if (error == 0)
3648                         error = EEXIST;
3649                 else if (error == ENOENT)
3650                         error = 0;
3651         }
3652         mutex_exit(&ds->ds_lock);
3653 
3654         if (error == 0 && ha->temphold &&
3655             strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
3656                 error = E2BIG;
3657 
3658         return (error);
3659 }
3660 
3661 void
3662 dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3663 {
3664         dsl_dataset_t *ds = arg1;
3665         struct dsl_ds_holdarg *ha = arg2;
3666         char *htag = ha->htag;
3667         dsl_pool_t *dp = ds->ds_dir->dd_pool;
3668         objset_t *mos = dp->dp_meta_objset;
3669         uint64_t now = gethrestime_sec();
3670         uint64_t zapobj;
3671 
3672         mutex_enter(&ds->ds_lock);
3673         if (ds->ds_phys->ds_userrefs_obj == 0) {
3674                 /*
3675                  * This is the first user hold for this dataset.  Create
3676                  * the userrefs zap object.
3677                  */
3678                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3679                 zapobj = ds->ds_phys->ds_userrefs_obj =
3680                     zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
3681         } else {
3682                 zapobj = ds->ds_phys->ds_userrefs_obj;
3683         }
3684         ds->ds_userrefs++;
3685         mutex_exit(&ds->ds_lock);
3686 
3687         VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
3688 
3689         if (ha->temphold) {
3690                 VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
3691                     htag, &now, tx));
3692         }
3693 
3694         spa_history_log_internal(LOG_DS_USER_HOLD,
3695             dp->dp_spa, tx, "<%s> temp = %d dataset = %llu", htag,
3696             (int)ha->temphold, ds->ds_object);
3697 }
3698 
3699 static int
3700 dsl_dataset_user_hold_one(const char *dsname, void *arg)
3701 {
3702         struct dsl_ds_holdarg *ha = arg;
3703         dsl_dataset_t *ds;
3704         int error;
3705         char *name;
3706 
3707         /* alloc a buffer to hold dsname@snapname plus terminating NULL */
3708         name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3709         error = dsl_dataset_hold(name, ha->dstg, &ds);
3710         strfree(name);
3711         if (error == 0) {
3712                 ha->gotone = B_TRUE;
3713                 dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
3714                     dsl_dataset_user_hold_sync, ds, ha, 0);
3715         } else if (error == ENOENT && ha->recursive) {
3716                 error = 0;


3883                          */
3884                         if (!ra->own)
3885                                 return (EBUSY);
3886                 }
3887                 dsda.ds = ds;
3888                 dsda.releasing = B_TRUE;
3889                 return (dsl_dataset_destroy_check(&dsda, tag, tx));
3890         }
3891 
3892         return (0);
3893 }
3894 
3895 static void
3896 dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx)
3897 {
3898         struct dsl_ds_releasearg *ra = arg1;
3899         dsl_dataset_t *ds = ra->ds;
3900         dsl_pool_t *dp = ds->ds_dir->dd_pool;
3901         objset_t *mos = dp->dp_meta_objset;
3902         uint64_t zapobj;
3903         uint64_t dsobj = ds->ds_object;
3904         uint64_t refs;
3905         int error;
3906 
3907         mutex_enter(&ds->ds_lock);
3908         ds->ds_userrefs--;
3909         refs = ds->ds_userrefs;
3910         mutex_exit(&ds->ds_lock);
3911         error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
3912         VERIFY(error == 0 || error == ENOENT);
3913         zapobj = ds->ds_phys->ds_userrefs_obj;
3914         VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
3915         if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
3916             DS_IS_DEFER_DESTROY(ds)) {
3917                 struct dsl_ds_destroyarg dsda = {0};
3918 
3919                 ASSERT(ra->own);
3920                 dsda.ds = ds;
3921                 dsda.releasing = B_TRUE;
3922                 /* We already did the destroy_check */
3923                 dsl_dataset_destroy_sync(&dsda, tag, tx);
3924         }
3925 
3926         spa_history_log_internal(LOG_DS_USER_RELEASE,
3927             dp->dp_spa, tx, "<%s> %lld dataset = %llu",
3928             ra->htag, (longlong_t)refs, dsobj);
3929 }
3930 
3931 static int
3932 dsl_dataset_user_release_one(const char *dsname, void *arg)
3933 {
3934         struct dsl_ds_holdarg *ha = arg;
3935         struct dsl_ds_releasearg *ra;
3936         dsl_dataset_t *ds;
3937         int error;
3938         void *dtag = ha->dstg;
3939         char *name;
3940         boolean_t own = B_FALSE;
3941         boolean_t might_destroy;
3942 
3943         /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
3944         name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3945         error = dsl_dataset_hold(name, dtag, &ds);
3946         strfree(name);
3947         if (error == ENOENT && ha->recursive)
3948                 return (0);




 897          * data from the origin snapshots zil header.
 898          */
 899         if (origin != NULL) {
 900                 dsl_dataset_t *ds;
 901                 objset_t *os;
 902 
 903                 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
 904                 VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
 905                 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
 906                 dsl_dataset_dirty(ds, tx);
 907                 dsl_dataset_rele(ds, FTAG);
 908         }
 909 
 910         return (dsobj);
 911 }
 912 
 913 /*
 914  * The snapshots must all be in the same pool.
 915  */
 916 int
 917 dmu_snapshots_destroy_nvl(nvlist_t *snaps, boolean_t defer,
 918     nvlist_t *errlist)
 919 {
 920         int err;
 921         dsl_sync_task_t *dst;
 922         spa_t *spa;
 923         nvpair_t *pair;
 924         dsl_sync_task_group_t *dstg;
 925 
 926         pair = nvlist_next_nvpair(snaps, NULL);
 927         if (pair == NULL)
 928                 return (0);
 929 
 930         err = spa_open(nvpair_name(pair), &spa, FTAG);
 931         if (err)
 932                 return (err);
 933         dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
 934 
 935         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
 936             pair = nvlist_next_nvpair(snaps, pair)) {
 937                 dsl_dataset_t *ds;
 938 
 939                 err = dsl_dataset_own(nvpair_name(pair), B_TRUE, dstg, &ds);
 940                 if (err == 0) {
 941                         struct dsl_ds_destroyarg *dsda;
 942 
 943                         dsl_dataset_make_exclusive(ds, dstg);
 944                         dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg),
 945                             KM_SLEEP);
 946                         dsda->ds = ds;
 947                         dsda->defer = defer;
 948                         dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
 949                             dsl_dataset_destroy_sync, dsda, dstg, 0);
 950                 } else if (err == ENOENT) {
 951                         err = 0;
 952                 } else {
 953                         fnvlist_add_int32(errlist, nvpair_name(pair), err);
 954                         break;
 955                 }
 956         }
 957 
 958         if (err == 0)
 959                 err = dsl_sync_task_group_wait(dstg);
 960 
 961         for (dst = list_head(&dstg->dstg_tasks); dst;
 962             dst = list_next(&dstg->dstg_tasks, dst)) {
 963                 struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
 964                 dsl_dataset_t *ds = dsda->ds;
 965 
 966                 /*
 967                  * Return the snapshots that triggered the error.
 968                  */
 969                 if (dst->dst_err != 0) {
 970                         char name[ZFS_MAXNAMELEN];
 971                         dsl_dataset_name(ds, name);
 972                         fnvlist_add_int32(errlist, name, dst->dst_err);
 973                 }
 974                 ASSERT3P(dsda->rm_origin, ==, NULL);
 975                 dsl_dataset_disown(ds, dstg);
 976                 kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
 977         }
 978 
 979         dsl_sync_task_group_destroy(dstg);
 980         spa_close(spa, FTAG);
 981         return (err);
 982 
 983 }
 984 
 985 static boolean_t
 986 dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
 987 {
 988         boolean_t might_destroy = B_FALSE;
 989 
 990         mutex_enter(&ds->ds_lock);
 991         if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
 992             DS_IS_DEFER_DESTROY(ds))


1031                 dsda->rm_origin = origin;
1032                 dsl_dataset_make_exclusive(origin, tag);
1033         }
1034 
1035         return (0);
1036 }
1037 
1038 /*
1039  * ds must be opened as OWNER.  On return (whether successful or not),
1040  * ds will be closed and caller can no longer dereference it.
1041  */
1042 int
1043 dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
1044 {
1045         int err;
1046         dsl_sync_task_group_t *dstg;
1047         objset_t *os;
1048         dsl_dir_t *dd;
1049         uint64_t obj;
1050         struct dsl_ds_destroyarg dsda = { 0 };

1051 
1052         dsda.ds = ds;
1053 
1054         if (dsl_dataset_is_snapshot(ds)) {
1055                 /* Destroying a snapshot is simpler */
1056                 dsl_dataset_make_exclusive(ds, tag);
1057 
1058                 dsda.defer = defer;
1059                 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1060                     dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
1061                     &dsda, tag, 0);
1062                 ASSERT3P(dsda.rm_origin, ==, NULL);
1063                 goto out;
1064         } else if (defer) {
1065                 err = EINVAL;
1066                 goto out;
1067         }
1068 
1069         dd = ds->ds_dir;


1070 
1071         /*
1072          * Check for errors and mark this ds as inconsistent, in
1073          * case we crash while freeing the objects.
1074          */
1075         err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
1076             dsl_dataset_destroy_begin_sync, ds, NULL, 0);
1077         if (err)
1078                 goto out;
1079 
1080         err = dmu_objset_from_ds(ds, &os);
1081         if (err)
1082                 goto out;
1083 
1084         /*
1085          * If async destruction is not enabled try to remove all objects
1086          * while in the open context so that there is less work to do in
1087          * the syncing context.
1088          */
1089         if (!spa_feature_is_enabled(dsl_dataset_get_spa(ds),


1136          */
1137         dsl_dataset_make_exclusive(ds, tag);
1138         /*
1139          * If we're removing a clone, we might also need to remove its
1140          * origin.
1141          */
1142         do {
1143                 dsda.need_prep = B_FALSE;
1144                 if (dsl_dir_is_clone(dd)) {
1145                         err = dsl_dataset_origin_rm_prep(&dsda, tag);
1146                         if (err) {
1147                                 dsl_dir_close(dd, FTAG);
1148                                 goto out;
1149                         }
1150                 }
1151 
1152                 dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
1153                 dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
1154                     dsl_dataset_destroy_sync, &dsda, tag, 0);
1155                 dsl_sync_task_create(dstg, dsl_dir_destroy_check,
1156                     dsl_dir_destroy_sync, dd, FTAG, 0);
1157                 err = dsl_sync_task_group_wait(dstg);
1158                 dsl_sync_task_group_destroy(dstg);
1159 
1160                 /*
1161                  * We could be racing against 'zfs release' or 'zfs destroy -d'
1162                  * on the origin snap, in which case we can get EBUSY if we
1163                  * needed to destroy the origin snap but were not ready to
1164                  * do so.
1165                  */
1166                 if (dsda.need_prep) {
1167                         ASSERT(err == EBUSY);
1168                         ASSERT(dsl_dir_is_clone(dd));
1169                         ASSERT(dsda.rm_origin == NULL);
1170                 }
1171         } while (dsda.need_prep);
1172 
1173         if (dsda.rm_origin != NULL)
1174                 dsl_dataset_disown(dsda.rm_origin, tag);
1175 
1176         /* if it is successful, dsl_dir_destroy_sync will close the dd */


1311 
1312         /*
1313          * This is really a dsl_dir thing, but check it here so that
1314          * we'll be less likely to leave this dataset inconsistent &
1315          * nearly destroyed.
1316          */
1317         err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
1318         if (err)
1319                 return (err);
1320         if (count != 0)
1321                 return (EEXIST);
1322 
1323         return (0);
1324 }
1325 
1326 /* ARGSUSED */
1327 static void
1328 dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1329 {
1330         dsl_dataset_t *ds = arg1;

1331 
1332         /* Mark it as inconsistent on-disk, in case we crash */
1333         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1334         ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1335 
1336         spa_history_log_internal_ds(ds, "destroy begin", tx, "");

1337 }
1338 
1339 static int
1340 dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
1341     dmu_tx_t *tx)
1342 {
1343         dsl_dataset_t *ds = dsda->ds;
1344         dsl_dataset_t *ds_prev = ds->ds_prev;
1345 
1346         if (dsl_dataset_might_destroy_origin(ds_prev)) {
1347                 struct dsl_ds_destroyarg ndsda = {0};
1348 
1349                 /*
1350                  * If we're not prepared to remove the origin, don't remove
1351                  * the clone either.
1352                  */
1353                 if (dsda->rm_origin == NULL) {
1354                         dsda->need_prep = B_TRUE;
1355                         return (EBUSY);
1356                 }


1641         int after_branch_point = FALSE;
1642         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1643         objset_t *mos = dp->dp_meta_objset;
1644         dsl_dataset_t *ds_prev = NULL;
1645         boolean_t wont_destroy;
1646         uint64_t obj;
1647 
1648         wont_destroy = (dsda->defer &&
1649             (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1));
1650 
1651         ASSERT(ds->ds_owner || wont_destroy);
1652         ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
1653         ASSERT(ds->ds_prev == NULL ||
1654             ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
1655         ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
1656 
1657         if (wont_destroy) {
1658                 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
1659                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1660                 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
1661                 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
1662                 return;
1663         }
1664 
1665         /* We need to log before removing it from the namespace. */
1666         spa_history_log_internal_ds(ds, "destroy", tx, "");
1667 
1668         /* signal any waiters that this dataset is going away */
1669         mutex_enter(&ds->ds_lock);
1670         ds->ds_owner = dsl_reaper;
1671         cv_broadcast(&ds->ds_exclusive_cv);
1672         mutex_exit(&ds->ds_lock);
1673 
1674         /* Remove our reservation */
1675         if (ds->ds_reserved != 0) {
1676                 dsl_prop_setarg_t psa;
1677                 uint64_t value = 0;
1678 
1679                 dsl_prop_setarg_init_uint64(&psa, "refreservation",
1680                     (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
1681                     &value);
1682                 psa.psa_effective_value = 0;    /* predict default value */
1683 
1684                 dsl_dataset_set_reservation_sync(ds, &psa, tx);
1685                 ASSERT3U(ds->ds_reserved, ==, 0);
1686         }
1687 


1942                 VERIFY(0 == dsl_dataset_get_snapname(ds));
1943 #ifdef ZFS_DEBUG
1944                 {
1945                         uint64_t val;
1946 
1947                         err = dsl_dataset_snap_lookup(ds_head,
1948                             ds->ds_snapname, &val);
1949                         ASSERT3U(err, ==, 0);
1950                         ASSERT3U(val, ==, obj);
1951                 }
1952 #endif
1953                 err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1954                 ASSERT(err == 0);
1955                 dsl_dataset_rele(ds_head, FTAG);
1956         }
1957 
1958         if (ds_prev && ds->ds_prev != ds_prev)
1959                 dsl_dataset_rele(ds_prev, FTAG);
1960 
1961         spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);


1962 
1963         if (ds->ds_phys->ds_next_clones_obj != 0) {
1964                 uint64_t count;
1965                 ASSERT(0 == zap_count(mos,
1966                     ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
1967                 VERIFY(0 == dmu_object_free(mos,
1968                     ds->ds_phys->ds_next_clones_obj, tx));
1969         }
1970         if (ds->ds_phys->ds_props_obj != 0)
1971                 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
1972         if (ds->ds_phys->ds_userrefs_obj != 0)
1973                 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
1974         dsl_dir_close(ds->ds_dir, ds);
1975         ds->ds_dir = NULL;
1976         dsl_dataset_drain_refs(ds, tag);
1977         VERIFY(0 == dmu_object_free(mos, obj, tx));
1978 
1979         if (dsda->rm_origin) {
1980                 /*
1981                  * Remove the origin of the clone we just destroyed.


1989 
1990 static int
1991 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1992 {
1993         uint64_t asize;
1994 
1995         if (!dmu_tx_is_syncing(tx))
1996                 return (0);
1997 
1998         /*
1999          * If there's an fs-only reservation, any blocks that might become
2000          * owned by the snapshot dataset must be accommodated by space
2001          * outside of the reservation.
2002          */
2003         ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
2004         asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
2005         if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
2006                 return (ENOSPC);
2007 
2008         /*
2009          * Propagate any reserved space for this snapshot to other
2010          * snapshot checks in this sync group.
2011          */
2012         if (asize > 0)
2013                 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
2014 
2015         return (0);
2016 }
2017 
2018 int
2019 dsl_dataset_snapshot_check(dsl_dataset_t *ds, const char *snapname,
2020     dmu_tx_t *tx)
2021 {


2022         int err;
2023         uint64_t value;
2024 
2025         /*
2026          * We don't allow multiple snapshots of the same txg.  If there
2027          * is already one, try again.
2028          */
2029         if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
2030                 return (EAGAIN);
2031 
2032         /*
2033          * Check for conflicting snapshot name.
2034          */
2035         err = dsl_dataset_snap_lookup(ds, snapname, &value);
2036         if (err == 0)
2037                 return (EEXIST);
2038         if (err != ENOENT)
2039                 return (err);
2040 
2041         /*
2042          * Check that the dataset's name is not too long.  Name consists
2043          * of the dataset's length + 1 for the @-sign + snapshot name's length
2044          */
2045         if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
2046                 return (ENAMETOOLONG);
2047 
2048         err = dsl_dataset_snapshot_reserve_space(ds, tx);
2049         if (err)
2050                 return (err);
2051 
2052         ds->ds_trysnap_txg = tx->tx_txg;
2053         return (0);
2054 }
2055 
2056 void
2057 dsl_dataset_snapshot_sync(dsl_dataset_t *ds, const char *snapname,
2058     dmu_tx_t *tx)
2059 {


2060         dsl_pool_t *dp = ds->ds_dir->dd_pool;
2061         dmu_buf_t *dbuf;
2062         dsl_dataset_phys_t *dsphys;
2063         uint64_t dsobj, crtxg;
2064         objset_t *mos = dp->dp_meta_objset;
2065         int err;
2066 
2067         ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
2068 
2069         /*
2070          * The origin's ds_creation_txg has to be < TXG_INITIAL
2071          */
2072         if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
2073                 crtxg = 1;
2074         else
2075                 crtxg = tx->tx_txg;
2076 
2077         dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
2078             DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
2079         VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));


2145         ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
2146         ds->ds_phys->ds_prev_snap_obj = dsobj;
2147         ds->ds_phys->ds_prev_snap_txg = crtxg;
2148         ds->ds_phys->ds_unique_bytes = 0;
2149         if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
2150                 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
2151 
2152         err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
2153             snapname, 8, 1, &dsobj, tx);
2154         ASSERT(err == 0);
2155 
2156         if (ds->ds_prev)
2157                 dsl_dataset_drop_ref(ds->ds_prev, ds);
2158         VERIFY(0 == dsl_dataset_get_ref(dp,
2159             ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
2160 
2161         dsl_scan_ds_snapshotted(ds, tx);
2162 
2163         dsl_dir_snap_cmtime_update(ds->ds_dir);
2164 
2165         spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");

2166 }
2167 
2168 void
2169 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
2170 {
2171         ASSERT(dmu_tx_is_syncing(tx));
2172         ASSERT(ds->ds_objset != NULL);
2173         ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
2174 
2175         /*
2176          * in case we had to change ds_fsid_guid when we opened it,
2177          * sync it out now.
2178          */
2179         dmu_buf_will_dirty(ds->ds_dbuf, tx);
2180         ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
2181 
2182         dsl_dir_dirty(ds->ds_dir, tx);
2183         dmu_objset_sync(ds->ds_objset, zio, tx);
2184 }
2185 


2232                         continue;
2233                 dsl_dir_name(clone->ds_dir, buf);
2234                 VERIFY(nvlist_add_boolean(val, buf) == 0);
2235                 dsl_dataset_rele(clone, FTAG);
2236         }
2237         zap_cursor_fini(&zc);
2238         VERIFY(nvlist_add_nvlist(propval, ZPROP_VALUE, val) == 0);
2239         VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
2240             propval) == 0);
2241 fail:
2242         nvlist_free(val);
2243         nvlist_free(propval);
2244         rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
2245 }
2246 
2247 void
2248 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2249 {
2250         uint64_t refd, avail, uobjs, aobjs, ratio;
2251 
2252         ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
2253             (ds->ds_phys->ds_uncompressed_bytes * 100 /
2254             ds->ds_phys->ds_compressed_bytes);
2255 
2256         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
2257 
2258         if (dsl_dataset_is_snapshot(ds)) {
2259                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
2260                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2261                     ds->ds_phys->ds_unique_bytes);
2262                 get_clones_stat(ds, nv);
2263         } else {
2264                 dsl_dir_stats(ds->ds_dir, nv);
2265         }
2266 
2267         dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
2268         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
2269         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
2270 
2271         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2272             ds->ds_phys->ds_creation_time);
2273         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2274             ds->ds_phys->ds_creation_txg);
2275         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2276             ds->ds_quota);
2277         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2278             ds->ds_reserved);
2279         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2280             ds->ds_phys->ds_guid);
2281         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2282             ds->ds_phys->ds_unique_bytes);
2283         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2284             ds->ds_object);
2285         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,


2290         if (ds->ds_phys->ds_prev_snap_obj != 0) {
2291                 uint64_t written, comp, uncomp;
2292                 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2293                 dsl_dataset_t *prev;
2294 
2295                 rw_enter(&dp->dp_config_rwlock, RW_READER);
2296                 int err = dsl_dataset_hold_obj(dp,
2297                     ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
2298                 rw_exit(&dp->dp_config_rwlock);
2299                 if (err == 0) {
2300                         err = dsl_dataset_space_written(prev, ds, &written,
2301                             &comp, &uncomp);
2302                         dsl_dataset_rele(prev, FTAG);
2303                         if (err == 0) {
2304                                 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2305                                     written);
2306                         }
2307                 }
2308         }
2309 
















2310 }
2311 
2312 void
2313 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2314 {
2315         stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
2316         stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
2317         stat->dds_guid = ds->ds_phys->ds_guid;
2318         stat->dds_origin[0] = '\0';
2319         if (dsl_dataset_is_snapshot(ds)) {
2320                 stat->dds_is_snapshot = B_TRUE;
2321                 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
2322         } else {
2323                 stat->dds_is_snapshot = B_FALSE;
2324                 stat->dds_num_clones = 0;

2325 

2326                 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
2327                 if (dsl_dir_is_clone(ds->ds_dir)) {
2328                         dsl_dataset_t *ods;
2329 
2330                         VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
2331                             ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
2332                         dsl_dataset_name(ods, stat->dds_origin);
2333                         dsl_dataset_drop_ref(ods, FTAG);


2334                 }
2335                 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
2336         }
2337 }
2338 
2339 uint64_t
2340 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2341 {
2342         return (ds->ds_fsid_guid);
2343 }
2344 
2345 void
2346 dsl_dataset_space(dsl_dataset_t *ds,
2347     uint64_t *refdbytesp, uint64_t *availbytesp,
2348     uint64_t *usedobjsp, uint64_t *availobjsp)
2349 {
2350         *refdbytesp = ds->ds_phys->ds_referenced_bytes;
2351         *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2352         if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
2353                 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
2354         if (ds->ds_quota != 0) {
2355                 /*
2356                  * Adjust available bytes according to refquota


2433         dsl_dir_t *dd = ds->ds_dir;
2434         objset_t *mos = dd->dd_pool->dp_meta_objset;
2435         dsl_dataset_t *hds;
2436         int err;
2437 
2438         ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2439 
2440         VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
2441             dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2442 
2443         VERIFY(0 == dsl_dataset_get_snapname(ds));
2444         err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2445         ASSERT3U(err, ==, 0);
2446         mutex_enter(&ds->ds_lock);
2447         (void) strcpy(ds->ds_snapname, newsnapname);
2448         mutex_exit(&ds->ds_lock);
2449         err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
2450             ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2451         ASSERT3U(err, ==, 0);
2452 
2453         spa_history_log_internal_ds(ds, "rename", tx,
2454             "-> @%s", newsnapname);
2455         dsl_dataset_rele(hds, FTAG);
2456 }
2457 
2458 struct renamesnaparg {
2459         dsl_sync_task_group_t *dstg;
2460         char failed[MAXPATHLEN];
2461         char *oldsnap;
2462         char *newsnap;
2463 };
2464 
2465 static int
2466 dsl_snapshot_rename_one(const char *name, void *arg)
2467 {
2468         struct renamesnaparg *ra = arg;
2469         dsl_dataset_t *ds = NULL;
2470         char *snapname;
2471         int err;
2472 
2473         snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
2474         (void) strlcpy(ra->failed, snapname, sizeof (ra->failed));


2914 
2915         delta = pa->cloneusedsnap -
2916             dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2917         ASSERT3S(delta, >=, 0);
2918         ASSERT3U(pa->used, >=, delta);
2919         dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2920         dsl_dir_diduse_space(dd, DD_USED_HEAD,
2921             pa->used - delta, pa->comp, pa->uncomp, tx);
2922 
2923         delta = pa->originusedsnap -
2924             odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2925         ASSERT3S(delta, <=, 0);
2926         ASSERT3U(pa->used, >=, -delta);
2927         dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2928         dsl_dir_diduse_space(odd, DD_USED_HEAD,
2929             -pa->used - delta, -pa->comp, -pa->uncomp, tx);
2930 
2931         origin_ds->ds_phys->ds_unique_bytes = pa->unique;
2932 
2933         /* log history record */
2934         spa_history_log_internal_ds(hds, "promote", tx, "");

2935 
2936         dsl_dir_close(odd, FTAG);
2937 }
2938 
2939 static char *snaplist_tag = "snaplist";
2940 /*
2941  * Make a list of dsl_dataset_t's for the snapshots between first_obj
2942  * (exclusive) and last_obj (inclusive).  The list will be in reverse
2943  * order (last_obj will be the list_head()).  If first_obj == 0, do all
2944  * snapshots back to this dataset's origin.
2945  */
2946 static int
2947 snaplist_make(dsl_pool_t *dp, boolean_t own,
2948     uint64_t first_obj, uint64_t last_obj, list_t *l)
2949 {
2950         uint64_t obj = last_obj;
2951 
2952         ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
2953 
2954         list_create(l, sizeof (struct promotenode),


3272         SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
3273             csa->cds->ds_phys->ds_unique_bytes);
3274 
3275         /* apply any parent delta for change in unconsumed refreservation */
3276         dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
3277             csa->unused_refres_delta, 0, 0, tx);
3278 
3279         /*
3280          * Swap deadlists.
3281          */
3282         dsl_deadlist_close(&csa->cds->ds_deadlist);
3283         dsl_deadlist_close(&csa->ohds->ds_deadlist);
3284         SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
3285             csa->cds->ds_phys->ds_deadlist_obj);
3286         dsl_deadlist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
3287             csa->cds->ds_phys->ds_deadlist_obj);
3288         dsl_deadlist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
3289             csa->ohds->ds_phys->ds_deadlist_obj);
3290 
3291         dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx);
3292 
3293         spa_history_log_internal_ds(csa->cds, "clone swap", tx,
3294             "parent=%s", csa->ohds->ds_dir->dd_myname);
3295 }
3296 
3297 /*
3298  * Swap 'clone' with its origin head datasets.  Used at the end of "zfs
3299  * recv" into an existing fs to swizzle the file system to the new
3300  * version, and by "zfs rollback".  Can also be used to swap two
3301  * independent head datasets if neither has any snapshots.
3302  */
3303 int
3304 dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
3305     boolean_t force)
3306 {
3307         struct cloneswaparg csa;
3308         int error;
3309 
3310         ASSERT(clone->ds_owner);
3311         ASSERT(origin_head->ds_owner);
3312 retry:
3313         /*
3314          * Need exclusive access for the swap. If we're swapping these


3431 
3432         return (0);
3433 }
3434 
3435 extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *);
3436 
3437 void
3438 dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3439 {
3440         dsl_dataset_t *ds = arg1;
3441         dsl_prop_setarg_t *psa = arg2;
3442         uint64_t effective_value = psa->psa_effective_value;
3443 
3444         dsl_prop_set_sync(ds, psa, tx);
3445         DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3446 
3447         if (ds->ds_quota != effective_value) {
3448                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3449                 ds->ds_quota = effective_value;
3450 
3451                 spa_history_log_internal_ds(ds, "set refquota", tx,
3452                     "refquota=%lld", (longlong_t)ds->ds_quota);

3453         }
3454 }
3455 
3456 int
3457 dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
3458 {
3459         dsl_dataset_t *ds;
3460         dsl_prop_setarg_t psa;
3461         int err;
3462 
3463         dsl_prop_setarg_init_uint64(&psa, "refquota", source, &quota);
3464 
3465         err = dsl_dataset_hold(dsname, FTAG, &ds);
3466         if (err)
3467                 return (err);
3468 
3469         /*
3470          * If someone removes a file, then tries to set the quota, we
3471          * want to make sure the file freeing takes effect.
3472          */


3537         uint64_t unique;
3538         int64_t delta;
3539 
3540         dsl_prop_set_sync(ds, psa, tx);
3541         DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3542 
3543         dmu_buf_will_dirty(ds->ds_dbuf, tx);
3544 
3545         mutex_enter(&ds->ds_dir->dd_lock);
3546         mutex_enter(&ds->ds_lock);
3547         ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3548         unique = ds->ds_phys->ds_unique_bytes;
3549         delta = MAX(0, (int64_t)(effective_value - unique)) -
3550             MAX(0, (int64_t)(ds->ds_reserved - unique));
3551         ds->ds_reserved = effective_value;
3552         mutex_exit(&ds->ds_lock);
3553 
3554         dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3555         mutex_exit(&ds->ds_dir->dd_lock);
3556 
3557         spa_history_log_internal_ds(ds, "set refreservation", tx,
3558             "refreservation=%lld", (longlong_t)effective_value);

3559 }
3560 
3561 int
3562 dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
3563     uint64_t reservation)
3564 {
3565         dsl_dataset_t *ds;
3566         dsl_prop_setarg_t psa;
3567         int err;
3568 
3569         dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
3570             &reservation);
3571 
3572         err = dsl_dataset_hold(dsname, FTAG, &ds);
3573         if (err)
3574                 return (err);
3575 
3576         err = dsl_sync_task_do(ds->ds_dir->dd_pool,
3577             dsl_dataset_set_reservation_check,
3578             dsl_dataset_set_reservation_sync, ds, &psa, 0);


3604         zfs_hold_cleanup_arg_t *ca;
3605 
3606         ca = kmem_alloc(sizeof (zfs_hold_cleanup_arg_t), KM_SLEEP);
3607         ca->dp = ds->ds_dir->dd_pool;
3608         ca->dsobj = ds->ds_object;
3609         (void) strlcpy(ca->htag, htag, sizeof (ca->htag));
3610         VERIFY3U(0, ==, zfs_onexit_add_cb(minor,
3611             dsl_dataset_user_release_onexit, ca, NULL));
3612 }
3613 
3614 /*
3615  * If you add new checks here, you may need to add
3616  * additional checks to the "temporary" case in
3617  * snapshot_check() in dmu_objset.c.
3618  */
3619 static int
3620 dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
3621 {
3622         dsl_dataset_t *ds = arg1;
3623         struct dsl_ds_holdarg *ha = arg2;
3624         const char *htag = ha->htag;
3625         objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3626         int error = 0;
3627 
3628         if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3629                 return (ENOTSUP);
3630 
3631         if (!dsl_dataset_is_snapshot(ds))
3632                 return (EINVAL);
3633 
3634         /* tags must be unique */
3635         mutex_enter(&ds->ds_lock);
3636         if (ds->ds_phys->ds_userrefs_obj) {
3637                 error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
3638                     8, 1, tx);
3639                 if (error == 0)
3640                         error = EEXIST;
3641                 else if (error == ENOENT)
3642                         error = 0;
3643         }
3644         mutex_exit(&ds->ds_lock);
3645 
3646         if (error == 0 && ha->temphold &&
3647             strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
3648                 error = E2BIG;
3649 
3650         return (error);
3651 }
3652 
3653 void
3654 dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3655 {
3656         dsl_dataset_t *ds = arg1;
3657         struct dsl_ds_holdarg *ha = arg2;
3658         const char *htag = ha->htag;
3659         dsl_pool_t *dp = ds->ds_dir->dd_pool;
3660         objset_t *mos = dp->dp_meta_objset;
3661         uint64_t now = gethrestime_sec();
3662         uint64_t zapobj;
3663 
3664         mutex_enter(&ds->ds_lock);
3665         if (ds->ds_phys->ds_userrefs_obj == 0) {
3666                 /*
3667                  * This is the first user hold for this dataset.  Create
3668                  * the userrefs zap object.
3669                  */
3670                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3671                 zapobj = ds->ds_phys->ds_userrefs_obj =
3672                     zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
3673         } else {
3674                 zapobj = ds->ds_phys->ds_userrefs_obj;
3675         }
3676         ds->ds_userrefs++;
3677         mutex_exit(&ds->ds_lock);
3678 
3679         VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
3680 
3681         if (ha->temphold) {
3682                 VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
3683                     htag, &now, tx));
3684         }
3685 
3686         spa_history_log_internal_ds(ds, "hold", tx,
3687             "tag = %s temp = %d holds now = %llu",
3688             htag, (int)ha->temphold, ds->ds_userrefs);
3689 }
3690 
3691 static int
3692 dsl_dataset_user_hold_one(const char *dsname, void *arg)
3693 {
3694         struct dsl_ds_holdarg *ha = arg;
3695         dsl_dataset_t *ds;
3696         int error;
3697         char *name;
3698 
3699         /* alloc a buffer to hold dsname@snapname plus terminating NULL */
3700         name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3701         error = dsl_dataset_hold(name, ha->dstg, &ds);
3702         strfree(name);
3703         if (error == 0) {
3704                 ha->gotone = B_TRUE;
3705                 dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
3706                     dsl_dataset_user_hold_sync, ds, ha, 0);
3707         } else if (error == ENOENT && ha->recursive) {
3708                 error = 0;


3875                          */
3876                         if (!ra->own)
3877                                 return (EBUSY);
3878                 }
3879                 dsda.ds = ds;
3880                 dsda.releasing = B_TRUE;
3881                 return (dsl_dataset_destroy_check(&dsda, tag, tx));
3882         }
3883 
3884         return (0);
3885 }
3886 
3887 static void
3888 dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx)
3889 {
3890         struct dsl_ds_releasearg *ra = arg1;
3891         dsl_dataset_t *ds = ra->ds;
3892         dsl_pool_t *dp = ds->ds_dir->dd_pool;
3893         objset_t *mos = dp->dp_meta_objset;
3894         uint64_t zapobj;

3895         uint64_t refs;
3896         int error;
3897 
3898         mutex_enter(&ds->ds_lock);
3899         ds->ds_userrefs--;
3900         refs = ds->ds_userrefs;
3901         mutex_exit(&ds->ds_lock);
3902         error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
3903         VERIFY(error == 0 || error == ENOENT);
3904         zapobj = ds->ds_phys->ds_userrefs_obj;
3905         VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
3906         if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
3907             DS_IS_DEFER_DESTROY(ds)) {
3908                 struct dsl_ds_destroyarg dsda = {0};
3909 
3910                 ASSERT(ra->own);
3911                 dsda.ds = ds;
3912                 dsda.releasing = B_TRUE;
3913                 /* We already did the destroy_check */
3914                 dsl_dataset_destroy_sync(&dsda, tag, tx);
3915         }
3916 
3917         spa_history_log_internal_ds(ds, "release", tx,
3918             "tag = %s refs now = %lld", ra->htag, (longlong_t)refs);

3919 }
3920 
3921 static int
3922 dsl_dataset_user_release_one(const char *dsname, void *arg)
3923 {
3924         struct dsl_ds_holdarg *ha = arg;
3925         struct dsl_ds_releasearg *ra;
3926         dsl_dataset_t *ds;
3927         int error;
3928         void *dtag = ha->dstg;
3929         char *name;
3930         boolean_t own = B_FALSE;
3931         boolean_t might_destroy;
3932 
3933         /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
3934         name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3935         error = dsl_dataset_hold(name, dtag, &ds);
3936         strfree(name);
3937         if (error == ENOENT && ha->recursive)
3938                 return (0);