]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_dataset.c
Illumos 5959 - clean up per-dataset feature count code
[mirror_zfs.git] / module / zfs / dsl_dataset.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
241b5415 23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
788eb90c 24 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
36f92e93 25 * Copyright (c) 2014 RackTop Systems.
0c66c32d 26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
34dc7c2f
BB
27 */
28
34dc7c2f
BB
29#include <sys/dmu_objset.h>
30#include <sys/dsl_dataset.h>
31#include <sys/dsl_dir.h>
32#include <sys/dsl_prop.h>
33#include <sys/dsl_synctask.h>
34#include <sys/dmu_traverse.h>
37abac6d 35#include <sys/dmu_impl.h>
34dc7c2f
BB
36#include <sys/dmu_tx.h>
37#include <sys/arc.h>
38#include <sys/zio.h>
39#include <sys/zap.h>
9ae529ec 40#include <sys/zfeature.h>
34dc7c2f
BB
41#include <sys/unique.h>
42#include <sys/zfs_context.h>
43#include <sys/zfs_ioctl.h>
44#include <sys/spa.h>
b128c09f 45#include <sys/zfs_znode.h>
572e2857 46#include <sys/zfs_onexit.h>
45d1cae3 47#include <sys/zvol.h>
428870ff
BB
48#include <sys/dsl_scan.h>
49#include <sys/dsl_deadlist.h>
13fe0198
MA
50#include <sys/dsl_destroy.h>
51#include <sys/dsl_userhold.h>
da536844 52#include <sys/dsl_bookmark.h>
34dc7c2f 53
f1512ee6
MA
54/*
55 * The SPA supports block sizes up to 16MB. However, very large blocks
56 * can have an impact on i/o latency (e.g. tying up a spinning disk for
57 * ~300ms), and also potentially on the memory allocator. Therefore,
58 * we do not allow the recordsize to be set larger than zfs_max_recordsize
59 * (default 1MB). Larger blocks can be created by changing this tunable,
60 * and pools with larger blocks can always be imported and used, regardless
61 * of this setting.
62 */
63int zfs_max_recordsize = 1 * 1024 * 1024;
64
428870ff
BB
65#define SWITCH64(x, y) \
66 { \
67 uint64_t __tmp = (x); \
68 (x) = (y); \
69 (y) = __tmp; \
70 }
71
34dc7c2f
BB
72#define DS_REF_MAX (1ULL << 62)
73
d683ddbb 74extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
d683ddbb 75
34dc7c2f
BB
76/*
77 * Figure out how much of this delta should be propogated to the dsl_dir
78 * layer. If there's a refreservation, that space has already been
79 * partially accounted for in our ancestors.
80 */
81static int64_t
82parent_delta(dsl_dataset_t *ds, int64_t delta)
83{
d683ddbb 84 dsl_dataset_phys_t *ds_phys;
34dc7c2f
BB
85 uint64_t old_bytes, new_bytes;
86
87 if (ds->ds_reserved == 0)
88 return (delta);
89
d683ddbb
JG
90 ds_phys = dsl_dataset_phys(ds);
91 old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
92 new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
34dc7c2f
BB
93
94 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
95 return (new_bytes - old_bytes);
96}
97
98void
428870ff 99dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
34dc7c2f 100{
d6320ddb 101 int used, compressed, uncompressed;
34dc7c2f
BB
102 int64_t delta;
103
d6320ddb
BB
104 used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
105 compressed = BP_GET_PSIZE(bp);
106 uncompressed = BP_GET_UCSIZE(bp);
107
428870ff 108 dprintf_bp(bp, "ds=%p", ds);
34dc7c2f
BB
109
110 ASSERT(dmu_tx_is_syncing(tx));
111 /* It could have been compressed away to nothing */
112 if (BP_IS_HOLE(bp))
113 return;
114 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
9ae529ec 115 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
34dc7c2f 116 if (ds == NULL) {
29809a6c
MA
117 dsl_pool_mos_diduse_space(tx->tx_pool,
118 used, compressed, uncompressed);
34dc7c2f
BB
119 return;
120 }
428870ff 121
a169a625 122 dmu_buf_will_dirty(ds->ds_dbuf, tx);
34dc7c2f
BB
123 mutex_enter(&ds->ds_lock);
124 delta = parent_delta(ds, used);
d683ddbb
JG
125 dsl_dataset_phys(ds)->ds_referenced_bytes += used;
126 dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
127 dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
128 dsl_dataset_phys(ds)->ds_unique_bytes += used;
241b5415
MA
129 if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
130 ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
131 B_TRUE;
132 }
34dc7c2f 133 mutex_exit(&ds->ds_lock);
b128c09f
BB
134 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
135 compressed, uncompressed, tx);
136 dsl_dir_transfer_space(ds->ds_dir, used - delta,
137 DD_USED_REFRSRV, DD_USED_HEAD, tx);
34dc7c2f
BB
138}
139
b128c09f 140int
428870ff
BB
141dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
142 boolean_t async)
34dc7c2f 143{
b0bc7a84
MG
144 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
145 int compressed = BP_GET_PSIZE(bp);
146 int uncompressed = BP_GET_UCSIZE(bp);
d6320ddb 147
34dc7c2f 148 if (BP_IS_HOLE(bp))
b128c09f 149 return (0);
34dc7c2f 150
428870ff
BB
151 ASSERT(dmu_tx_is_syncing(tx));
152 ASSERT(bp->blk_birth <= tx->tx_txg);
153
34dc7c2f 154 if (ds == NULL) {
428870ff 155 dsl_free(tx->tx_pool, tx->tx_txg, bp);
29809a6c
MA
156 dsl_pool_mos_diduse_space(tx->tx_pool,
157 -used, -compressed, -uncompressed);
b128c09f 158 return (used);
34dc7c2f
BB
159 }
160 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
161
0c66c32d 162 ASSERT(!ds->ds_is_snapshot);
34dc7c2f
BB
163 dmu_buf_will_dirty(ds->ds_dbuf, tx);
164
d683ddbb 165 if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
34dc7c2f
BB
166 int64_t delta;
167
428870ff
BB
168 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
169 dsl_free(tx->tx_pool, tx->tx_txg, bp);
34dc7c2f
BB
170
171 mutex_enter(&ds->ds_lock);
d683ddbb 172 ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
34dc7c2f
BB
173 !DS_UNIQUE_IS_ACCURATE(ds));
174 delta = parent_delta(ds, -used);
d683ddbb 175 dsl_dataset_phys(ds)->ds_unique_bytes -= used;
34dc7c2f 176 mutex_exit(&ds->ds_lock);
b128c09f 177 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
34dc7c2f 178 delta, -compressed, -uncompressed, tx);
b128c09f
BB
179 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
180 DD_USED_REFRSRV, DD_USED_HEAD, tx);
34dc7c2f
BB
181 } else {
182 dprintf_bp(bp, "putting on dead list: %s", "");
428870ff
BB
183 if (async) {
184 /*
185 * We are here as part of zio's write done callback,
186 * which means we're a zio interrupt thread. We can't
187 * call dsl_deadlist_insert() now because it may block
188 * waiting for I/O. Instead, put bp on the deferred
189 * queue and let dsl_pool_sync() finish the job.
190 */
191 bplist_append(&ds->ds_pending_deadlist, bp);
192 } else {
193 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
194 }
34dc7c2f 195 ASSERT3U(ds->ds_prev->ds_object, ==,
d683ddbb
JG
196 dsl_dataset_phys(ds)->ds_prev_snap_obj);
197 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
34dc7c2f 198 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
d683ddbb 199 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
34dc7c2f 200 ds->ds_object && bp->blk_birth >
d683ddbb 201 dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
34dc7c2f
BB
202 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
203 mutex_enter(&ds->ds_prev->ds_lock);
d683ddbb 204 dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
34dc7c2f
BB
205 mutex_exit(&ds->ds_prev->ds_lock);
206 }
428870ff 207 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
b128c09f
BB
208 dsl_dir_transfer_space(ds->ds_dir, used,
209 DD_USED_HEAD, DD_USED_SNAP, tx);
210 }
34dc7c2f
BB
211 }
212 mutex_enter(&ds->ds_lock);
d683ddbb
JG
213 ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
214 dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
215 ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
216 dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
217 ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
218 dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
34dc7c2f 219 mutex_exit(&ds->ds_lock);
b128c09f
BB
220
221 return (used);
34dc7c2f
BB
222}
223
224uint64_t
225dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
226{
227 uint64_t trysnap = 0;
228
229 if (ds == NULL)
230 return (0);
231 /*
232 * The snapshot creation could fail, but that would cause an
233 * incorrect FALSE return, which would only result in an
234 * overestimation of the amount of space that an operation would
235 * consume, which is OK.
236 *
237 * There's also a small window where we could miss a pending
238 * snapshot, because we could set the sync task in the quiescing
239 * phase. So this should only be used as a guess.
240 */
241 if (ds->ds_trysnap_txg >
242 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
243 trysnap = ds->ds_trysnap_txg;
d683ddbb 244 return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap));
34dc7c2f
BB
245}
246
9babb374 247boolean_t
428870ff
BB
248dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
249 uint64_t blk_birth)
34dc7c2f 250{
b0bc7a84
MG
251 if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
252 (bp != NULL && BP_IS_HOLE(bp)))
428870ff
BB
253 return (B_FALSE);
254
572e2857 255 ddt_prefetch(dsl_dataset_get_spa(ds), bp);
428870ff
BB
256
257 return (B_TRUE);
34dc7c2f
BB
258}
259
34dc7c2f 260static void
0c66c32d 261dsl_dataset_evict(void *dbu)
34dc7c2f 262{
0c66c32d 263 dsl_dataset_t *ds = dbu;
34dc7c2f 264
13fe0198 265 ASSERT(ds->ds_owner == NULL);
34dc7c2f 266
0c66c32d
JG
267 ds->ds_dbuf = NULL;
268
34dc7c2f
BB
269 unique_remove(ds->ds_fsid_guid);
270
428870ff
BB
271 if (ds->ds_objset != NULL)
272 dmu_objset_evict(ds->ds_objset);
34dc7c2f
BB
273
274 if (ds->ds_prev) {
13fe0198 275 dsl_dataset_rele(ds->ds_prev, ds);
34dc7c2f
BB
276 ds->ds_prev = NULL;
277 }
278
428870ff 279 bplist_destroy(&ds->ds_pending_deadlist);
0c66c32d 280 if (ds->ds_deadlist.dl_os != NULL)
428870ff 281 dsl_deadlist_close(&ds->ds_deadlist);
b128c09f 282 if (ds->ds_dir)
0c66c32d 283 dsl_dir_async_rele(ds->ds_dir, ds);
34dc7c2f
BB
284
285 ASSERT(!list_link_active(&ds->ds_synced_link));
286
287 mutex_destroy(&ds->ds_lock);
288 mutex_destroy(&ds->ds_opening_lock);
58c4aa00 289 mutex_destroy(&ds->ds_sendstream_lock);
13fe0198 290 refcount_destroy(&ds->ds_longholds);
34dc7c2f
BB
291
292 kmem_free(ds, sizeof (dsl_dataset_t));
293}
294
13fe0198 295int
34dc7c2f
BB
296dsl_dataset_get_snapname(dsl_dataset_t *ds)
297{
298 dsl_dataset_phys_t *headphys;
299 int err;
300 dmu_buf_t *headdbuf;
301 dsl_pool_t *dp = ds->ds_dir->dd_pool;
302 objset_t *mos = dp->dp_meta_objset;
303
304 if (ds->ds_snapname[0])
305 return (0);
d683ddbb 306 if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
34dc7c2f
BB
307 return (0);
308
d683ddbb 309 err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
34dc7c2f 310 FTAG, &headdbuf);
13fe0198 311 if (err != 0)
34dc7c2f
BB
312 return (err);
313 headphys = headdbuf->db_data;
314 err = zap_value_search(dp->dp_meta_objset,
315 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
d439f63f
TC
316 if (err != 0 && zfs_recover == B_TRUE) {
317 err = 0;
318 (void) snprintf(ds->ds_snapname, sizeof (ds->ds_snapname),
319 "SNAPOBJ=%llu-ERR=%d",
320 (unsigned long long)ds->ds_object, err);
321 }
34dc7c2f
BB
322 dmu_buf_rele(headdbuf, FTAG);
323 return (err);
324}
325
6772fb67 326int
b128c09f 327dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
34dc7c2f 328{
b128c09f 329 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
d683ddbb 330 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
34dc7c2f
BB
331 matchtype_t mt;
332 int err;
333
d683ddbb 334 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
34dc7c2f
BB
335 mt = MT_FIRST;
336 else
337 mt = MT_EXACT;
338
b128c09f 339 err = zap_lookup_norm(mos, snapobj, name, 8, 1,
34dc7c2f
BB
340 value, mt, NULL, 0, NULL);
341 if (err == ENOTSUP && mt == MT_FIRST)
b128c09f 342 err = zap_lookup(mos, snapobj, name, 8, 1, value);
34dc7c2f
BB
343 return (err);
344}
345
13fe0198 346int
788eb90c
JJ
347dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
348 boolean_t adj_cnt)
34dc7c2f 349{
b128c09f 350 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
d683ddbb 351 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
34dc7c2f
BB
352 matchtype_t mt;
353 int err;
354
428870ff
BB
355 dsl_dir_snap_cmtime_update(ds->ds_dir);
356
d683ddbb 357 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
34dc7c2f
BB
358 mt = MT_FIRST;
359 else
360 mt = MT_EXACT;
361
b128c09f 362 err = zap_remove_norm(mos, snapobj, name, mt, tx);
34dc7c2f 363 if (err == ENOTSUP && mt == MT_FIRST)
b128c09f 364 err = zap_remove(mos, snapobj, name, tx);
788eb90c
JJ
365
366 if (err == 0 && adj_cnt)
367 dsl_fs_ss_count_adjust(ds->ds_dir, -1,
368 DD_FIELD_SNAPSHOT_COUNT, tx);
369
34dc7c2f
BB
370 return (err);
371}
372
6ebebace
JG
373boolean_t
374dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
375{
6186e297
JG
376 dmu_buf_t *dbuf = ds->ds_dbuf;
377 boolean_t result = B_FALSE;
378
379 if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
380 ds->ds_object, DMU_BONUS_BLKID, tag)) {
381
382 if (ds == dmu_buf_get_user(dbuf))
383 result = B_TRUE;
384 else
385 dmu_buf_rele(dbuf, tag);
386 }
387
388 return (result);
6ebebace
JG
389}
390
13fe0198
MA
391int
392dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
b128c09f 393 dsl_dataset_t **dsp)
34dc7c2f 394{
34dc7c2f
BB
395 objset_t *mos = dp->dp_meta_objset;
396 dmu_buf_t *dbuf;
397 dsl_dataset_t *ds;
398 int err;
572e2857 399 dmu_object_info_t doi;
34dc7c2f 400
13fe0198 401 ASSERT(dsl_pool_config_held(dp));
34dc7c2f
BB
402
403 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
13fe0198 404 if (err != 0)
34dc7c2f 405 return (err);
572e2857
BB
406
407 /* Make sure dsobj has the correct object type. */
408 dmu_object_info_from_db(dbuf, &doi);
fa86b5db 409 if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
3a84951d 410 dmu_buf_rele(dbuf, tag);
2e528b49 411 return (SET_ERROR(EINVAL));
3a84951d 412 }
572e2857 413
34dc7c2f
BB
414 ds = dmu_buf_get_user(dbuf);
415 if (ds == NULL) {
d4ed6673 416 dsl_dataset_t *winner = NULL;
34dc7c2f 417
79c76d5b 418 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
34dc7c2f
BB
419 ds->ds_dbuf = dbuf;
420 ds->ds_object = dsobj;
0c66c32d 421 ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
98f72a53 422 list_link_init(&ds->ds_synced_link);
34dc7c2f
BB
423
424 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
425 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
37abac6d 426 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
13fe0198 427 refcount_create(&ds->ds_longholds);
34dc7c2f 428
428870ff
BB
429 bplist_create(&ds->ds_pending_deadlist);
430 dsl_deadlist_open(&ds->ds_deadlist,
d683ddbb 431 mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
428870ff 432
37abac6d
BP
433 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
434 offsetof(dmu_sendarg_t, dsa_link));
435
f1512ee6 436 if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
241b5415
MA
437 spa_feature_t f;
438
439 for (f = 0; f < SPA_FEATURES; f++) {
440 if (!(spa_feature_table[f].fi_flags &
441 ZFEATURE_FLAG_PER_DATASET))
442 continue;
443 err = zap_contains(mos, dsobj,
444 spa_feature_table[f].fi_guid);
445 if (err == 0) {
446 ds->ds_feature_inuse[f] = B_TRUE;
447 } else {
448 ASSERT3U(err, ==, ENOENT);
449 err = 0;
450 }
19b3b1d2 451 }
f1512ee6
MA
452 }
453
241b5415
MA
454 err = dsl_dir_hold_obj(dp,
455 dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
13fe0198 456 if (err != 0) {
34dc7c2f
BB
457 mutex_destroy(&ds->ds_lock);
458 mutex_destroy(&ds->ds_opening_lock);
58c4aa00 459 mutex_destroy(&ds->ds_sendstream_lock);
13fe0198 460 refcount_destroy(&ds->ds_longholds);
428870ff
BB
461 bplist_destroy(&ds->ds_pending_deadlist);
462 dsl_deadlist_close(&ds->ds_deadlist);
34dc7c2f
BB
463 kmem_free(ds, sizeof (dsl_dataset_t));
464 dmu_buf_rele(dbuf, tag);
465 return (err);
466 }
467
0c66c32d 468 if (!ds->ds_is_snapshot) {
34dc7c2f 469 ds->ds_snapname[0] = '\0';
d683ddbb 470 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
13fe0198 471 err = dsl_dataset_hold_obj(dp,
d683ddbb 472 dsl_dataset_phys(ds)->ds_prev_snap_obj,
b128c09f 473 ds, &ds->ds_prev);
34dc7c2f 474 }
da536844
MA
475 if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
476 int zaperr = zap_lookup(mos, ds->ds_object,
477 DS_FIELD_BOOKMARK_NAMES,
478 sizeof (ds->ds_bookmarks), 1,
479 &ds->ds_bookmarks);
480 if (zaperr != ENOENT)
481 VERIFY0(zaperr);
482 }
45d1cae3
BB
483 } else {
484 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
485 err = dsl_dataset_get_snapname(ds);
d683ddbb
JG
486 if (err == 0 &&
487 dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
45d1cae3
BB
488 err = zap_count(
489 ds->ds_dir->dd_pool->dp_meta_objset,
d683ddbb 490 dsl_dataset_phys(ds)->ds_userrefs_obj,
45d1cae3
BB
491 &ds->ds_userrefs);
492 }
34dc7c2f
BB
493 }
494
0c66c32d 495 if (err == 0 && !ds->ds_is_snapshot) {
13fe0198
MA
496 err = dsl_prop_get_int_ds(ds,
497 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
498 &ds->ds_reserved);
34dc7c2f 499 if (err == 0) {
13fe0198
MA
500 err = dsl_prop_get_int_ds(ds,
501 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
502 &ds->ds_quota);
34dc7c2f 503 }
34dc7c2f
BB
504 } else {
505 ds->ds_reserved = ds->ds_quota = 0;
506 }
507
0c66c32d
JG
508 dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict, &ds->ds_dbuf);
509 if (err == 0)
510 winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
511
512 if (err != 0 || winner != NULL) {
428870ff
BB
513 bplist_destroy(&ds->ds_pending_deadlist);
514 dsl_deadlist_close(&ds->ds_deadlist);
b128c09f 515 if (ds->ds_prev)
13fe0198
MA
516 dsl_dataset_rele(ds->ds_prev, ds);
517 dsl_dir_rele(ds->ds_dir, ds);
34dc7c2f
BB
518 mutex_destroy(&ds->ds_lock);
519 mutex_destroy(&ds->ds_opening_lock);
58c4aa00 520 mutex_destroy(&ds->ds_sendstream_lock);
13fe0198 521 refcount_destroy(&ds->ds_longholds);
34dc7c2f 522 kmem_free(ds, sizeof (dsl_dataset_t));
13fe0198 523 if (err != 0) {
34dc7c2f
BB
524 dmu_buf_rele(dbuf, tag);
525 return (err);
526 }
527 ds = winner;
528 } else {
529 ds->ds_fsid_guid =
d683ddbb 530 unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
34dc7c2f
BB
531 }
532 }
533 ASSERT3P(ds->ds_dbuf, ==, dbuf);
d683ddbb
JG
534 ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
535 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
b128c09f
BB
536 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
537 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
34dc7c2f
BB
538 *dsp = ds;
539 return (0);
540}
541
b128c09f 542int
13fe0198 543dsl_dataset_hold(dsl_pool_t *dp, const char *name,
428870ff 544 void *tag, dsl_dataset_t **dsp)
34dc7c2f
BB
545{
546 dsl_dir_t *dd;
b128c09f 547 const char *snapname;
34dc7c2f 548 uint64_t obj;
34dc7c2f
BB
549 int err = 0;
550
13fe0198
MA
551 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
552 if (err != 0)
34dc7c2f
BB
553 return (err);
554
13fe0198 555 ASSERT(dsl_pool_config_held(dp));
d683ddbb 556 obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
13fe0198
MA
557 if (obj != 0)
558 err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
b128c09f 559 else
2e528b49 560 err = SET_ERROR(ENOENT);
34dc7c2f 561
b128c09f
BB
562 /* we may be looking for a snapshot */
563 if (err == 0 && snapname != NULL) {
13fe0198 564 dsl_dataset_t *ds;
34dc7c2f 565
b128c09f
BB
566 if (*snapname++ != '@') {
567 dsl_dataset_rele(*dsp, tag);
13fe0198 568 dsl_dir_rele(dd, FTAG);
2e528b49 569 return (SET_ERROR(ENOENT));
34dc7c2f 570 }
34dc7c2f 571
b128c09f
BB
572 dprintf("looking for snapshot '%s'\n", snapname);
573 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
574 if (err == 0)
13fe0198 575 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
b128c09f
BB
576 dsl_dataset_rele(*dsp, tag);
577
13fe0198 578 if (err == 0) {
b128c09f
BB
579 mutex_enter(&ds->ds_lock);
580 if (ds->ds_snapname[0] == 0)
581 (void) strlcpy(ds->ds_snapname, snapname,
582 sizeof (ds->ds_snapname));
583 mutex_exit(&ds->ds_lock);
13fe0198 584 *dsp = ds;
34dc7c2f 585 }
34dc7c2f 586 }
13fe0198
MA
587
588 dsl_dir_rele(dd, FTAG);
34dc7c2f
BB
589 return (err);
590}
591
592int
13fe0198 593dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
428870ff 594 void *tag, dsl_dataset_t **dsp)
34dc7c2f 595{
13fe0198
MA
596 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
597 if (err != 0)
598 return (err);
599 if (!dsl_dataset_tryown(*dsp, tag)) {
600 dsl_dataset_rele(*dsp, tag);
601 *dsp = NULL;
2e528b49 602 return (SET_ERROR(EBUSY));
13fe0198
MA
603 }
604 return (0);
605}
606
607int
608dsl_dataset_own(dsl_pool_t *dp, const char *name,
609 void *tag, dsl_dataset_t **dsp)
610{
611 int err = dsl_dataset_hold(dp, name, tag, dsp);
612 if (err != 0)
b128c09f 613 return (err);
13fe0198 614 if (!dsl_dataset_tryown(*dsp, tag)) {
428870ff 615 dsl_dataset_rele(*dsp, tag);
2e528b49 616 return (SET_ERROR(EBUSY));
b128c09f
BB
617 }
618 return (0);
34dc7c2f
BB
619}
620
13fe0198
MA
621/*
622 * See the comment above dsl_pool_hold() for details. In summary, a long
623 * hold is used to prevent destruction of a dataset while the pool hold
624 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
625 *
626 * The dataset and pool must be held when this function is called. After it
627 * is called, the pool hold may be released while the dataset is still held
628 * and accessed.
629 */
630void
631dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
632{
633 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
634 (void) refcount_add(&ds->ds_longholds, tag);
635}
636
637void
638dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
639{
640 (void) refcount_remove(&ds->ds_longholds, tag);
641}
642
643/* Return B_TRUE if there are any long holds on this dataset. */
644boolean_t
645dsl_dataset_long_held(dsl_dataset_t *ds)
646{
647 return (!refcount_is_zero(&ds->ds_longholds));
648}
649
34dc7c2f
BB
650void
651dsl_dataset_name(dsl_dataset_t *ds, char *name)
652{
653 if (ds == NULL) {
654 (void) strcpy(name, "mos");
655 } else {
656 dsl_dir_name(ds->ds_dir, name);
13fe0198 657 VERIFY0(dsl_dataset_get_snapname(ds));
34dc7c2f
BB
658 if (ds->ds_snapname[0]) {
659 (void) strcat(name, "@");
b128c09f
BB
660 /*
661 * We use a "recursive" mutex so that we
662 * can call dprintf_ds() with ds_lock held.
663 */
34dc7c2f 664 if (!MUTEX_HELD(&ds->ds_lock)) {
34dc7c2f
BB
665 mutex_enter(&ds->ds_lock);
666 (void) strcat(name, ds->ds_snapname);
667 mutex_exit(&ds->ds_lock);
668 } else {
669 (void) strcat(name, ds->ds_snapname);
670 }
671 }
672 }
673}
674
34dc7c2f 675void
b128c09f
BB
676dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
677{
13fe0198 678 dmu_buf_rele(ds->ds_dbuf, tag);
b128c09f
BB
679}
680
681void
428870ff 682dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
34dc7c2f 683{
945dd935
JG
684 ASSERT3P(ds->ds_owner, ==, tag);
685 ASSERT(ds->ds_dbuf != NULL);
b128c09f 686
34dc7c2f 687 mutex_enter(&ds->ds_lock);
b128c09f 688 ds->ds_owner = NULL;
34dc7c2f 689 mutex_exit(&ds->ds_lock);
13fe0198 690 dsl_dataset_long_rele(ds, tag);
945dd935 691 dsl_dataset_rele(ds, tag);
34dc7c2f
BB
692}
693
694boolean_t
13fe0198 695dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
34dc7c2f 696{
b128c09f
BB
697 boolean_t gotit = FALSE;
698
34dc7c2f 699 mutex_enter(&ds->ds_lock);
13fe0198 700 if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
428870ff 701 ds->ds_owner = tag;
13fe0198 702 dsl_dataset_long_hold(ds, tag);
b128c09f 703 gotit = TRUE;
34dc7c2f
BB
704 }
705 mutex_exit(&ds->ds_lock);
b128c09f 706 return (gotit);
34dc7c2f
BB
707}
708
241b5415
MA
709static void
710dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
711{
712 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
713 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
714 uint64_t zero = 0;
715
716 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
717
718 spa_feature_incr(spa, f, tx);
719 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
720
721 VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
722 sizeof (zero), 1, &zero, tx));
723}
724
725void
726dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
727{
728 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
729 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
730
731 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
732
733 VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
734 spa_feature_decr(spa, f, tx);
735}
736
34dc7c2f 737uint64_t
b128c09f 738dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
34dc7c2f
BB
739 uint64_t flags, dmu_tx_t *tx)
740{
741 dsl_pool_t *dp = dd->dd_pool;
742 dmu_buf_t *dbuf;
743 dsl_dataset_phys_t *dsphys;
744 uint64_t dsobj;
745 objset_t *mos = dp->dp_meta_objset;
746
b128c09f
BB
747 if (origin == NULL)
748 origin = dp->dp_origin_snap;
749
34dc7c2f 750 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
d683ddbb 751 ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
34dc7c2f 752 ASSERT(dmu_tx_is_syncing(tx));
d683ddbb 753 ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
34dc7c2f
BB
754
755 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
756 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13fe0198 757 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
34dc7c2f
BB
758 dmu_buf_will_dirty(dbuf, tx);
759 dsphys = dbuf->db_data;
b128c09f 760 bzero(dsphys, sizeof (dsl_dataset_phys_t));
34dc7c2f
BB
761 dsphys->ds_dir_obj = dd->dd_object;
762 dsphys->ds_flags = flags;
763 dsphys->ds_fsid_guid = unique_create();
764 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
765 sizeof (dsphys->ds_guid));
766 dsphys->ds_snapnames_zapobj =
767 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
768 DMU_OT_NONE, 0, tx);
769 dsphys->ds_creation_time = gethrestime_sec();
b128c09f 770 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
34dc7c2f 771
428870ff
BB
772 if (origin == NULL) {
773 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
774 } else {
241b5415 775 spa_feature_t f;
13fe0198 776 dsl_dataset_t *ohds; /* head of the origin snapshot */
428870ff 777
34dc7c2f
BB
778 dsphys->ds_prev_snap_obj = origin->ds_object;
779 dsphys->ds_prev_snap_txg =
d683ddbb 780 dsl_dataset_phys(origin)->ds_creation_txg;
9ae529ec 781 dsphys->ds_referenced_bytes =
d683ddbb 782 dsl_dataset_phys(origin)->ds_referenced_bytes;
34dc7c2f 783 dsphys->ds_compressed_bytes =
d683ddbb 784 dsl_dataset_phys(origin)->ds_compressed_bytes;
34dc7c2f 785 dsphys->ds_uncompressed_bytes =
d683ddbb
JG
786 dsl_dataset_phys(origin)->ds_uncompressed_bytes;
787 dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
4b20a6f5
MA
788
789 /*
790 * Inherit flags that describe the dataset's contents
791 * (INCONSISTENT) or properties (Case Insensitive).
792 */
d683ddbb 793 dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
4b20a6f5 794 (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
34dc7c2f 795
241b5415
MA
796 for (f = 0; f < SPA_FEATURES; f++) {
797 if (origin->ds_feature_inuse[f])
798 dsl_dataset_activate_feature(dsobj, f, tx);
799 }
f1512ee6 800
34dc7c2f 801 dmu_buf_will_dirty(origin->ds_dbuf, tx);
d683ddbb 802 dsl_dataset_phys(origin)->ds_num_children++;
34dc7c2f 803
13fe0198 804 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
805 dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
806 FTAG, &ohds));
428870ff
BB
807 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
808 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
809 dsl_dataset_rele(ohds, FTAG);
810
b128c09f 811 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
d683ddbb
JG
812 if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
813 dsl_dataset_phys(origin)->ds_next_clones_obj =
b128c09f
BB
814 zap_create(mos,
815 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
816 }
13fe0198 817 VERIFY0(zap_add_int(mos,
d683ddbb
JG
818 dsl_dataset_phys(origin)->ds_next_clones_obj,
819 dsobj, tx));
b128c09f
BB
820 }
821
34dc7c2f 822 dmu_buf_will_dirty(dd->dd_dbuf, tx);
d683ddbb 823 dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
428870ff 824 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
d683ddbb 825 if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
428870ff 826 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
d683ddbb 827 dsl_dir_phys(origin->ds_dir)->dd_clones =
428870ff
BB
828 zap_create(mos,
829 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
830 }
13fe0198 831 VERIFY0(zap_add_int(mos,
d683ddbb
JG
832 dsl_dir_phys(origin->ds_dir)->dd_clones,
833 dsobj, tx));
428870ff 834 }
34dc7c2f
BB
835 }
836
837 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
838 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
839
840 dmu_buf_rele(dbuf, FTAG);
841
842 dmu_buf_will_dirty(dd->dd_dbuf, tx);
d683ddbb 843 dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
34dc7c2f
BB
844
845 return (dsobj);
846}
847
13fe0198
MA
848static void
849dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
850{
851 objset_t *os;
852
853 VERIFY0(dmu_objset_from_ds(ds, &os));
854 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
855 dsl_dataset_dirty(ds, tx);
856}
857
34dc7c2f
BB
858uint64_t
859dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
860 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
861{
862 dsl_pool_t *dp = pdd->dd_pool;
863 uint64_t dsobj, ddobj;
864 dsl_dir_t *dd;
865
13fe0198 866 ASSERT(dmu_tx_is_syncing(tx));
34dc7c2f
BB
867 ASSERT(lastname[0] != '@');
868
b128c09f 869 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
13fe0198 870 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
34dc7c2f 871
13fe0198
MA
872 dsobj = dsl_dataset_create_sync_dd(dd, origin,
873 flags & ~DS_CREATE_FLAG_NODIRTY, tx);
34dc7c2f
BB
874
875 dsl_deleg_set_create_perms(dd, tx, cr);
876
788eb90c
JJ
877 /*
878 * Since we're creating a new node we know it's a leaf, so we can
879 * initialize the counts if the limit feature is active.
880 */
881 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
882 uint64_t cnt = 0;
883 objset_t *os = dd->dd_pool->dp_meta_objset;
884
885 dsl_dir_zapify(dd, tx);
886 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
887 sizeof (cnt), 1, &cnt, tx));
888 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
889 sizeof (cnt), 1, &cnt, tx));
890 }
891
13fe0198 892 dsl_dir_rele(dd, FTAG);
34dc7c2f 893
572e2857
BB
894 /*
895 * If we are creating a clone, make sure we zero out any stale
896 * data from the origin snapshots zil header.
897 */
13fe0198 898 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
572e2857 899 dsl_dataset_t *ds;
572e2857 900
13fe0198
MA
901 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
902 dsl_dataset_zero_zil(ds, tx);
572e2857
BB
903 dsl_dataset_rele(ds, FTAG);
904 }
905
34dc7c2f
BB
906 return (dsobj);
907}
908
34dc7c2f 909/*
13fe0198
MA
910 * The unique space in the head dataset can be calculated by subtracting
911 * the space used in the most recent snapshot, that is still being used
912 * in this file system, from the space currently in use. To figure out
913 * the space in the most recent snapshot still in use, we need to take
914 * the total space used in the snapshot and subtract out the space that
915 * has been freed up since the snapshot was taken.
34dc7c2f 916 */
13fe0198
MA
917void
918dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
34dc7c2f 919{
13fe0198
MA
920 uint64_t mrs_used;
921 uint64_t dlused, dlcomp, dluncomp;
34dc7c2f 922
0c66c32d 923 ASSERT(!ds->ds_is_snapshot);
34dc7c2f 924
d683ddbb
JG
925 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
926 mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
13fe0198
MA
927 else
928 mrs_used = 0;
45d1cae3 929
13fe0198 930 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
34dc7c2f 931
13fe0198 932 ASSERT3U(dlused, <=, mrs_used);
d683ddbb
JG
933 dsl_dataset_phys(ds)->ds_unique_bytes =
934 dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
330d06f9 935
13fe0198
MA
936 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
937 SPA_VERSION_UNIQUE_ACCURATE)
d683ddbb 938 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
34dc7c2f
BB
939}
940
13fe0198
MA
941void
942dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
943 dmu_tx_t *tx)
45d1cae3 944{
13fe0198
MA
945 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
946 int err;
947 ASSERTV(uint64_t count);
948
d683ddbb
JG
949 ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
950 err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
951 obj, tx);
13fe0198
MA
952 /*
953 * The err should not be ENOENT, but a bug in a previous version
954 * of the code could cause upgrade_clones_cb() to not set
955 * ds_next_snap_obj when it should, leading to a missing entry.
956 * If we knew that the pool was created after
957 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
958 * ENOENT. However, at least we can check that we don't have
959 * too many entries in the next_clones_obj even after failing to
960 * remove this one.
961 */
962 if (err != ENOENT)
963 VERIFY0(err);
d683ddbb 964 ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
13fe0198 965 &count));
d683ddbb 966 ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
13fe0198 967}
45d1cae3 968
45d1cae3 969
13fe0198
MA
970blkptr_t *
971dsl_dataset_get_blkptr(dsl_dataset_t *ds)
972{
d683ddbb 973 return (&dsl_dataset_phys(ds)->ds_bp);
45d1cae3
BB
974}
975
13fe0198
MA
976void
977dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
45d1cae3 978{
13fe0198
MA
979 ASSERT(dmu_tx_is_syncing(tx));
980 /* If it's the meta-objset, set dp_meta_rootbp */
981 if (ds == NULL) {
982 tx->tx_pool->dp_meta_rootbp = *bp;
983 } else {
984 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 985 dsl_dataset_phys(ds)->ds_bp = *bp;
45d1cae3 986 }
13fe0198 987}
45d1cae3 988
13fe0198
MA
989spa_t *
990dsl_dataset_get_spa(dsl_dataset_t *ds)
991{
992 return (ds->ds_dir->dd_pool->dp_spa);
45d1cae3
BB
993}
994
13fe0198
MA
995void
996dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
34dc7c2f 997{
13fe0198 998 dsl_pool_t *dp;
45d1cae3 999
13fe0198
MA
1000 if (ds == NULL) /* this is the meta-objset */
1001 return;
34dc7c2f 1002
428870ff 1003 ASSERT(ds->ds_objset != NULL);
34dc7c2f 1004
d683ddbb 1005 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
34dc7c2f
BB
1006 panic("dirtying snapshot!");
1007
1008 dp = ds->ds_dir->dd_pool;
1009
13fe0198 1010 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
34dc7c2f
BB
1011 /* up the hold count until we can be written out */
1012 dmu_buf_add_ref(ds->ds_dbuf, ds);
1013 }
1014}
1015
04434775
MA
1016boolean_t
1017dsl_dataset_is_dirty(dsl_dataset_t *ds)
1018{
1019 int t;
1020
1021 for (t = 0; t < TXG_SIZE; t++) {
1022 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1023 ds, t))
1024 return (B_TRUE);
1025 }
1026 return (B_FALSE);
1027}
1028
34dc7c2f 1029static int
13fe0198 1030dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
34dc7c2f 1031{
13fe0198 1032 uint64_t asize;
34dc7c2f 1033
13fe0198 1034 if (!dmu_tx_is_syncing(tx))
b128c09f
BB
1035 return (0);
1036
34dc7c2f 1037 /*
13fe0198
MA
1038 * If there's an fs-only reservation, any blocks that might become
1039 * owned by the snapshot dataset must be accommodated by space
1040 * outside of the reservation.
34dc7c2f 1041 */
13fe0198 1042 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
d683ddbb 1043 asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
13fe0198 1044 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
2e528b49 1045 return (SET_ERROR(ENOSPC));
34dc7c2f
BB
1046
1047 /*
13fe0198
MA
1048 * Propagate any reserved space for this snapshot to other
1049 * snapshot checks in this sync group.
34dc7c2f 1050 */
13fe0198
MA
1051 if (asize > 0)
1052 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
34dc7c2f
BB
1053
1054 return (0);
1055}
1056
13fe0198
MA
1057typedef struct dsl_dataset_snapshot_arg {
1058 nvlist_t *ddsa_snaps;
1059 nvlist_t *ddsa_props;
1060 nvlist_t *ddsa_errors;
788eb90c 1061 cred_t *ddsa_cr;
13fe0198 1062} dsl_dataset_snapshot_arg_t;
45d1cae3 1063
34dc7c2f 1064int
13fe0198 1065dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
788eb90c 1066 dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
34dc7c2f 1067{
13fe0198
MA
1068 int error;
1069 uint64_t value;
34dc7c2f 1070
13fe0198 1071 ds->ds_trysnap_txg = tx->tx_txg;
b128c09f 1072
13fe0198 1073 if (!dmu_tx_is_syncing(tx))
45d1cae3 1074 return (0);
34dc7c2f
BB
1075
1076 /*
13fe0198
MA
1077 * We don't allow multiple snapshots of the same txg. If there
1078 * is already one, try again.
34dc7c2f 1079 */
d683ddbb 1080 if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
2e528b49 1081 return (SET_ERROR(EAGAIN));
34dc7c2f
BB
1082
1083 /*
13fe0198 1084 * Check for conflicting snapshot name.
34dc7c2f 1085 */
13fe0198
MA
1086 error = dsl_dataset_snap_lookup(ds, snapname, &value);
1087 if (error == 0)
2e528b49 1088 return (SET_ERROR(EEXIST));
13fe0198
MA
1089 if (error != ENOENT)
1090 return (error);
45d1cae3 1091
96c2e961
KW
1092 /*
1093 * We don't allow taking snapshots of inconsistent datasets, such as
1094 * those into which we are currently receiving. However, if we are
1095 * creating this snapshot as part of a receive, this check will be
1096 * executed atomically with respect to the completion of the receive
1097 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1098 * case we ignore this, knowing it will be fixed up for us shortly in
1099 * dmu_recv_end_sync().
1100 */
1101 if (!recv && DS_IS_INCONSISTENT(ds))
1102 return (SET_ERROR(EBUSY));
1103
788eb90c
JJ
1104 /*
1105 * Skip the check for temporary snapshots or if we have already checked
1106 * the counts in dsl_dataset_snapshot_check. This means we really only
1107 * check the count here when we're receiving a stream.
1108 */
1109 if (cnt != 0 && cr != NULL) {
1110 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1111 ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1112 if (error != 0)
1113 return (error);
1114 }
1115
13fe0198
MA
1116 error = dsl_dataset_snapshot_reserve_space(ds, tx);
1117 if (error != 0)
1118 return (error);
45d1cae3 1119
34dc7c2f
BB
1120 return (0);
1121}
1122
13fe0198
MA
1123static int
1124dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
b128c09f 1125{
13fe0198
MA
1126 dsl_dataset_snapshot_arg_t *ddsa = arg;
1127 dsl_pool_t *dp = dmu_tx_pool(tx);
1128 nvpair_t *pair;
1129 int rv = 0;
b128c09f 1130
788eb90c
JJ
1131 /*
1132 * Pre-compute how many total new snapshots will be created for each
1133 * level in the tree and below. This is needed for validating the
1134 * snapshot limit when either taking a recursive snapshot or when
1135 * taking multiple snapshots.
1136 *
1137 * The problem is that the counts are not actually adjusted when
1138 * we are checking, only when we finally sync. For a single snapshot,
1139 * this is easy, the count will increase by 1 at each node up the tree,
1140 * but its more complicated for the recursive/multiple snapshot case.
1141 *
1142 * The dsl_fs_ss_limit_check function does recursively check the count
1143 * at each level up the tree but since it is validating each snapshot
1144 * independently we need to be sure that we are validating the complete
1145 * count for the entire set of snapshots. We do this by rolling up the
1146 * counts for each component of the name into an nvlist and then
1147 * checking each of those cases with the aggregated count.
1148 *
1149 * This approach properly handles not only the recursive snapshot
1150 * case (where we get all of those on the ddsa_snaps list) but also
1151 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1152 * validate the limit on 'a' using a count of 2).
1153 *
1154 * We validate the snapshot names in the third loop and only report
1155 * name errors once.
1156 */
1157 if (dmu_tx_is_syncing(tx)) {
1158 char *nm;
1159 nvlist_t *cnt_track = NULL;
1160 cnt_track = fnvlist_alloc();
1161
1162 nm = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1163
1164 /* Rollup aggregated counts into the cnt_track list */
1165 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1166 pair != NULL;
1167 pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1168 char *pdelim;
1169 uint64_t val;
1170
1171 (void) strlcpy(nm, nvpair_name(pair), MAXPATHLEN);
1172 pdelim = strchr(nm, '@');
1173 if (pdelim == NULL)
1174 continue;
1175 *pdelim = '\0';
1176
1177 do {
1178 if (nvlist_lookup_uint64(cnt_track, nm,
1179 &val) == 0) {
1180 /* update existing entry */
1181 fnvlist_add_uint64(cnt_track, nm,
1182 val + 1);
1183 } else {
1184 /* add to list */
1185 fnvlist_add_uint64(cnt_track, nm, 1);
1186 }
1187
1188 pdelim = strrchr(nm, '/');
1189 if (pdelim != NULL)
1190 *pdelim = '\0';
1191 } while (pdelim != NULL);
1192 }
1193
1194 kmem_free(nm, MAXPATHLEN);
1195
1196 /* Check aggregated counts at each level */
1197 for (pair = nvlist_next_nvpair(cnt_track, NULL);
1198 pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1199 int error = 0;
1200 char *name;
1201 uint64_t cnt = 0;
1202 dsl_dataset_t *ds;
1203
1204 name = nvpair_name(pair);
1205 cnt = fnvpair_value_uint64(pair);
1206 ASSERT(cnt > 0);
1207
1208 error = dsl_dataset_hold(dp, name, FTAG, &ds);
1209 if (error == 0) {
1210 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1211 ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1212 ddsa->ddsa_cr);
1213 dsl_dataset_rele(ds, FTAG);
1214 }
1215
1216 if (error != 0) {
1217 if (ddsa->ddsa_errors != NULL)
1218 fnvlist_add_int32(ddsa->ddsa_errors,
1219 name, error);
1220 rv = error;
1221 /* only report one error for this check */
1222 break;
1223 }
1224 }
1225 nvlist_free(cnt_track);
1226 }
1227
13fe0198
MA
1228 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1229 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1230 int error = 0;
1231 dsl_dataset_t *ds;
1232 char *name, *atp;
1233 char dsname[MAXNAMELEN];
1234
1235 name = nvpair_name(pair);
1236 if (strlen(name) >= MAXNAMELEN)
2e528b49 1237 error = SET_ERROR(ENAMETOOLONG);
13fe0198
MA
1238 if (error == 0) {
1239 atp = strchr(name, '@');
1240 if (atp == NULL)
2e528b49 1241 error = SET_ERROR(EINVAL);
13fe0198
MA
1242 if (error == 0)
1243 (void) strlcpy(dsname, name, atp - name + 1);
1244 }
1245 if (error == 0)
1246 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1247 if (error == 0) {
788eb90c 1248 /* passing 0/NULL skips dsl_fs_ss_limit_check */
13fe0198 1249 error = dsl_dataset_snapshot_check_impl(ds,
788eb90c 1250 atp + 1, tx, B_FALSE, 0, NULL);
13fe0198
MA
1251 dsl_dataset_rele(ds, FTAG);
1252 }
b128c09f 1253
13fe0198
MA
1254 if (error != 0) {
1255 if (ddsa->ddsa_errors != NULL) {
1256 fnvlist_add_int32(ddsa->ddsa_errors,
1257 name, error);
1258 }
1259 rv = error;
1260 }
1261 }
788eb90c 1262
13fe0198 1263 return (rv);
b128c09f
BB
1264}
1265
13fe0198
MA
1266void
1267dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1268 dmu_tx_t *tx)
428870ff 1269{
13fe0198
MA
1270 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1271 dmu_buf_t *dbuf;
1272 dsl_dataset_phys_t *dsphys;
1273 uint64_t dsobj, crtxg;
1274 objset_t *mos = dp->dp_meta_objset;
241b5415 1275 spa_feature_t f;
13fe0198
MA
1276 ASSERTV(static zil_header_t zero_zil);
1277 ASSERTV(objset_t *os);
1278
1279 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
428870ff 1280
428870ff 1281 /*
13fe0198
MA
1282 * If we are on an old pool, the zil must not be active, in which
1283 * case it will be zeroed. Usually zil_suspend() accomplishes this.
428870ff 1284 */
13fe0198
MA
1285 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1286 dmu_objset_from_ds(ds, &os) != 0 ||
1287 bcmp(&os->os_phys->os_zil_header, &zero_zil,
1288 sizeof (zero_zil)) == 0);
428870ff 1289
788eb90c 1290 dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
428870ff
BB
1291
1292 /*
13fe0198 1293 * The origin's ds_creation_txg has to be < TXG_INITIAL
b128c09f
BB
1294 */
1295 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1296 crtxg = 1;
1297 else
1298 crtxg = tx->tx_txg;
1299
34dc7c2f
BB
1300 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1301 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13fe0198 1302 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
34dc7c2f
BB
1303 dmu_buf_will_dirty(dbuf, tx);
1304 dsphys = dbuf->db_data;
b128c09f 1305 bzero(dsphys, sizeof (dsl_dataset_phys_t));
34dc7c2f
BB
1306 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1307 dsphys->ds_fsid_guid = unique_create();
1308 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1309 sizeof (dsphys->ds_guid));
d683ddbb
JG
1310 dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1311 dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
34dc7c2f
BB
1312 dsphys->ds_next_snap_obj = ds->ds_object;
1313 dsphys->ds_num_children = 1;
1314 dsphys->ds_creation_time = gethrestime_sec();
b128c09f 1315 dsphys->ds_creation_txg = crtxg;
d683ddbb
JG
1316 dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1317 dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1318 dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1319 dsphys->ds_uncompressed_bytes =
1320 dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1321 dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1322 dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
34dc7c2f
BB
1323 dmu_buf_rele(dbuf, FTAG);
1324
241b5415
MA
1325 for (f = 0; f < SPA_FEATURES; f++) {
1326 if (ds->ds_feature_inuse[f])
1327 dsl_dataset_activate_feature(dsobj, f, tx);
1328 }
f1512ee6 1329
d683ddbb
JG
1330 ASSERT3U(ds->ds_prev != 0, ==,
1331 dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
34dc7c2f 1332 if (ds->ds_prev) {
b128c09f 1333 uint64_t next_clones_obj =
d683ddbb
JG
1334 dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1335 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
34dc7c2f 1336 ds->ds_object ||
d683ddbb
JG
1337 dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1338 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1339 ds->ds_object) {
34dc7c2f 1340 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
d683ddbb
JG
1341 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1342 dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1343 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
b128c09f 1344 } else if (next_clones_obj != 0) {
13fe0198 1345 dsl_dataset_remove_from_next_clones(ds->ds_prev,
428870ff 1346 dsphys->ds_next_snap_obj, tx);
13fe0198 1347 VERIFY0(zap_add_int(mos,
b128c09f 1348 next_clones_obj, dsobj, tx));
34dc7c2f
BB
1349 }
1350 }
1351
1352 /*
1353 * If we have a reference-reservation on this dataset, we will
1354 * need to increase the amount of refreservation being charged
1355 * since our unique space is going to zero.
1356 */
1357 if (ds->ds_reserved) {
428870ff
BB
1358 int64_t delta;
1359 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
d683ddbb
JG
1360 delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1361 ds->ds_reserved);
b128c09f 1362 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
428870ff 1363 delta, 0, 0, tx);
34dc7c2f
BB
1364 }
1365
34dc7c2f 1366 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb
JG
1367 dsl_dataset_phys(ds)->ds_deadlist_obj =
1368 dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1369 dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
428870ff 1370 dsl_deadlist_close(&ds->ds_deadlist);
d683ddbb
JG
1371 dsl_deadlist_open(&ds->ds_deadlist, mos,
1372 dsl_dataset_phys(ds)->ds_deadlist_obj);
428870ff 1373 dsl_deadlist_add_key(&ds->ds_deadlist,
d683ddbb 1374 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
428870ff 1375
d683ddbb
JG
1376 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1377 dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1378 dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1379 dsl_dataset_phys(ds)->ds_unique_bytes = 0;
34dc7c2f 1380 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
d683ddbb 1381 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
34dc7c2f 1382
d683ddbb 1383 VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
13fe0198 1384 snapname, 8, 1, &dsobj, tx));
34dc7c2f
BB
1385
1386 if (ds->ds_prev)
13fe0198
MA
1387 dsl_dataset_rele(ds->ds_prev, ds);
1388 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb 1389 dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
b128c09f 1390
428870ff
BB
1391 dsl_scan_ds_snapshotted(ds, tx);
1392
1393 dsl_dir_snap_cmtime_update(ds->ds_dir);
34dc7c2f 1394
6f1ffb06 1395 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
34dc7c2f
BB
1396}
1397
13fe0198
MA
1398static void
1399dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1400{
13fe0198
MA
1401 dsl_dataset_snapshot_arg_t *ddsa = arg;
1402 dsl_pool_t *dp = dmu_tx_pool(tx);
1403 nvpair_t *pair;
34dc7c2f 1404
13fe0198
MA
1405 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1406 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1407 dsl_dataset_t *ds;
1408 char *name, *atp;
1409 char dsname[MAXNAMELEN];
1410
1411 name = nvpair_name(pair);
1412 atp = strchr(name, '@');
1413 (void) strlcpy(dsname, name, atp - name + 1);
1414 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1415
1416 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1417 if (ddsa->ddsa_props != NULL) {
1418 dsl_props_set_sync_impl(ds->ds_prev,
1419 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1420 }
1421 dsl_dataset_rele(ds, FTAG);
1422 }
34dc7c2f
BB
1423}
1424
13fe0198
MA
1425/*
1426 * The snapshots must all be in the same pool.
1427 * All-or-nothing: if there are any failures, nothing will be modified.
1428 */
1429int
1430dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
330d06f9 1431{
13fe0198
MA
1432 dsl_dataset_snapshot_arg_t ddsa;
1433 nvpair_t *pair;
1434 boolean_t needsuspend;
1435 int error;
1436 spa_t *spa;
1437 char *firstname;
1438 nvlist_t *suspended = NULL;
330d06f9 1439
13fe0198
MA
1440 pair = nvlist_next_nvpair(snaps, NULL);
1441 if (pair == NULL)
1442 return (0);
1443 firstname = nvpair_name(pair);
1444
1445 error = spa_open(firstname, &spa, FTAG);
1446 if (error != 0)
1447 return (error);
1448 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1449 spa_close(spa, FTAG);
1450
1451 if (needsuspend) {
1452 suspended = fnvlist_alloc();
1453 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1454 pair = nvlist_next_nvpair(snaps, pair)) {
1455 char fsname[MAXNAMELEN];
1456 char *snapname = nvpair_name(pair);
1457 char *atp;
1458 void *cookie;
1459
1460 atp = strchr(snapname, '@');
1461 if (atp == NULL) {
2e528b49 1462 error = SET_ERROR(EINVAL);
13fe0198
MA
1463 break;
1464 }
1465 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1466
1467 error = zil_suspend(fsname, &cookie);
1468 if (error != 0)
1469 break;
1470 fnvlist_add_uint64(suspended, fsname,
1471 (uintptr_t)cookie);
1472 }
1473 }
1474
1475 ddsa.ddsa_snaps = snaps;
1476 ddsa.ddsa_props = props;
1477 ddsa.ddsa_errors = errors;
788eb90c 1478 ddsa.ddsa_cr = CRED();
13fe0198
MA
1479
1480 if (error == 0) {
1481 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1482 dsl_dataset_snapshot_sync, &ddsa,
3d45fdd6 1483 fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
13fe0198
MA
1484 }
1485
1486 if (suspended != NULL) {
1487 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1488 pair = nvlist_next_nvpair(suspended, pair)) {
1489 zil_resume((void *)(uintptr_t)
1490 fnvpair_value_uint64(pair));
1491 }
1492 fnvlist_free(suspended);
1493 }
1494
ba6a2402
BB
1495#ifdef _KERNEL
1496 if (error == 0) {
1497 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
d1d7e268 1498 pair = nvlist_next_nvpair(snaps, pair)) {
ba6a2402
BB
1499 char *snapname = nvpair_name(pair);
1500 zvol_create_minors(snapname);
1501 }
1502 }
1503#endif
1504
13fe0198
MA
1505 return (error);
1506}
1507
1508typedef struct dsl_dataset_snapshot_tmp_arg {
1509 const char *ddsta_fsname;
1510 const char *ddsta_snapname;
1511 minor_t ddsta_cleanup_minor;
1512 const char *ddsta_htag;
1513} dsl_dataset_snapshot_tmp_arg_t;
1514
1515static int
1516dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1517{
1518 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1519 dsl_pool_t *dp = dmu_tx_pool(tx);
1520 dsl_dataset_t *ds;
1521 int error;
1522
1523 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1524 if (error != 0)
1525 return (error);
1526
788eb90c 1527 /* NULL cred means no limit check for tmp snapshot */
96c2e961 1528 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
788eb90c 1529 tx, B_FALSE, 0, NULL);
13fe0198
MA
1530 if (error != 0) {
1531 dsl_dataset_rele(ds, FTAG);
1532 return (error);
1533 }
1534
1535 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1536 dsl_dataset_rele(ds, FTAG);
2e528b49 1537 return (SET_ERROR(ENOTSUP));
13fe0198
MA
1538 }
1539 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1540 B_TRUE, tx);
1541 if (error != 0) {
1542 dsl_dataset_rele(ds, FTAG);
1543 return (error);
1544 }
1545
1546 dsl_dataset_rele(ds, FTAG);
1547 return (0);
1548}
1549
1550static void
1551dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1552{
1553 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1554 dsl_pool_t *dp = dmu_tx_pool(tx);
1555 dsl_dataset_t *ds;
1556
1557 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1558
1559 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1560 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1561 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1562 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1563
1564 dsl_dataset_rele(ds, FTAG);
1565}
1566
1567int
1568dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1569 minor_t cleanup_minor, const char *htag)
1570{
1571 dsl_dataset_snapshot_tmp_arg_t ddsta;
1572 int error;
1573 spa_t *spa;
1574 boolean_t needsuspend;
1575 void *cookie;
1576
1577 ddsta.ddsta_fsname = fsname;
1578 ddsta.ddsta_snapname = snapname;
1579 ddsta.ddsta_cleanup_minor = cleanup_minor;
1580 ddsta.ddsta_htag = htag;
1581
1582 error = spa_open(fsname, &spa, FTAG);
1583 if (error != 0)
1584 return (error);
1585 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1586 spa_close(spa, FTAG);
1587
1588 if (needsuspend) {
1589 error = zil_suspend(fsname, &cookie);
1590 if (error != 0)
1591 return (error);
1592 }
1593
1594 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
3d45fdd6 1595 dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
13fe0198
MA
1596
1597 if (needsuspend)
1598 zil_resume(cookie);
1599 return (error);
1600}
1601
1602
1603void
1604dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1605{
241b5415
MA
1606 spa_feature_t f;
1607
13fe0198
MA
1608 ASSERT(dmu_tx_is_syncing(tx));
1609 ASSERT(ds->ds_objset != NULL);
d683ddbb 1610 ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
13fe0198
MA
1611
1612 /*
1613 * in case we had to change ds_fsid_guid when we opened it,
1614 * sync it out now.
1615 */
1616 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb 1617 dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
13fe0198
MA
1618
1619 dmu_objset_sync(ds->ds_objset, zio, tx);
f1512ee6 1620
241b5415
MA
1621 for (f = 0; f < SPA_FEATURES; f++) {
1622 if (ds->ds_feature_activation_needed[f]) {
1623 if (ds->ds_feature_inuse[f])
1624 continue;
1625 dsl_dataset_activate_feature(ds->ds_object, f, tx);
1626 ds->ds_feature_inuse[f] = B_TRUE;
1627 }
f1512ee6 1628 }
13fe0198
MA
1629}
1630
1631static void
1632get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1633{
1634 uint64_t count = 0;
1635 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1636 zap_cursor_t zc;
1637 zap_attribute_t za;
1638 nvlist_t *propval = fnvlist_alloc();
1639 nvlist_t *val = fnvlist_alloc();
1640
1641 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
330d06f9
MA
1642
1643 /*
13fe0198 1644 * There may be missing entries in ds_next_clones_obj
330d06f9
MA
1645 * due to a bug in a previous version of the code.
1646 * Only trust it if it has the right number of entries.
1647 */
d683ddbb
JG
1648 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1649 VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
330d06f9
MA
1650 &count));
1651 }
d683ddbb 1652 if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
330d06f9 1653 goto fail;
d683ddbb
JG
1654 for (zap_cursor_init(&zc, mos,
1655 dsl_dataset_phys(ds)->ds_next_clones_obj);
330d06f9
MA
1656 zap_cursor_retrieve(&zc, &za) == 0;
1657 zap_cursor_advance(&zc)) {
1658 dsl_dataset_t *clone;
1659 char buf[ZFS_MAXNAMELEN];
13fe0198
MA
1660 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1661 za.za_first_integer, FTAG, &clone));
330d06f9 1662 dsl_dir_name(clone->ds_dir, buf);
13fe0198 1663 fnvlist_add_boolean(val, buf);
330d06f9
MA
1664 dsl_dataset_rele(clone, FTAG);
1665 }
1666 zap_cursor_fini(&zc);
13fe0198
MA
1667 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1668 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
330d06f9
MA
1669fail:
1670 nvlist_free(val);
1671 nvlist_free(propval);
330d06f9
MA
1672}
1673
34dc7c2f
BB
1674void
1675dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1676{
f5fc4aca 1677 uint64_t refd, avail, uobjs, aobjs, ratio;
13fe0198
MA
1678 ASSERTV(dsl_pool_t *dp = ds->ds_dir->dd_pool);
1679
1680 ASSERT(dsl_pool_config_held(dp));
34dc7c2f 1681
d683ddbb
JG
1682 ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
1683 (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
1684 dsl_dataset_phys(ds)->ds_compressed_bytes);
6f1ffb06
MA
1685
1686 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
24a64651 1687 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
d683ddbb 1688 dsl_dataset_phys(ds)->ds_uncompressed_bytes);
6f1ffb06 1689
0c66c32d 1690 if (ds->ds_is_snapshot) {
6f1ffb06
MA
1691 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1692 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
d683ddbb 1693 dsl_dataset_phys(ds)->ds_unique_bytes);
6f1ffb06
MA
1694 get_clones_stat(ds, nv);
1695 } else {
1696 dsl_dir_stats(ds->ds_dir, nv);
1697 }
34dc7c2f
BB
1698
1699 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1700 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1701 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1702
1703 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
d683ddbb 1704 dsl_dataset_phys(ds)->ds_creation_time);
34dc7c2f 1705 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
d683ddbb 1706 dsl_dataset_phys(ds)->ds_creation_txg);
34dc7c2f
BB
1707 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1708 ds->ds_quota);
1709 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1710 ds->ds_reserved);
b128c09f 1711 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
d683ddbb 1712 dsl_dataset_phys(ds)->ds_guid);
428870ff 1713 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
d683ddbb 1714 dsl_dataset_phys(ds)->ds_unique_bytes);
428870ff
BB
1715 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1716 ds->ds_object);
1717 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1718 ds->ds_userrefs);
45d1cae3
BB
1719 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1720 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
34dc7c2f 1721
d683ddbb 1722 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
330d06f9
MA
1723 uint64_t written, comp, uncomp;
1724 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1725 dsl_dataset_t *prev;
1726 int err;
1727
330d06f9 1728 err = dsl_dataset_hold_obj(dp,
d683ddbb 1729 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
330d06f9
MA
1730 if (err == 0) {
1731 err = dsl_dataset_space_written(prev, ds, &written,
1732 &comp, &uncomp);
1733 dsl_dataset_rele(prev, FTAG);
1734 if (err == 0) {
1735 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1736 written);
1737 }
1738 }
1739 }
1740
34dc7c2f
BB
1741}
1742
1743void
1744dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1745{
13fe0198
MA
1746 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1747 ASSERT(dsl_pool_config_held(dp));
1748
d683ddbb
JG
1749 stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
1750 stat->dds_inconsistent =
1751 dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
1752 stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
6f1ffb06 1753 stat->dds_origin[0] = '\0';
0c66c32d 1754 if (ds->ds_is_snapshot) {
34dc7c2f 1755 stat->dds_is_snapshot = B_TRUE;
d683ddbb
JG
1756 stat->dds_num_clones =
1757 dsl_dataset_phys(ds)->ds_num_children - 1;
fb5f0bc8
BB
1758 } else {
1759 stat->dds_is_snapshot = B_FALSE;
1760 stat->dds_num_clones = 0;
34dc7c2f 1761
6f1ffb06
MA
1762 if (dsl_dir_is_clone(ds->ds_dir)) {
1763 dsl_dataset_t *ods;
34dc7c2f 1764
13fe0198 1765 VERIFY0(dsl_dataset_hold_obj(dp,
d683ddbb
JG
1766 dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
1767 FTAG, &ods));
6f1ffb06 1768 dsl_dataset_name(ods, stat->dds_origin);
13fe0198 1769 dsl_dataset_rele(ods, FTAG);
6f1ffb06 1770 }
34dc7c2f 1771 }
34dc7c2f
BB
1772}
1773
1774uint64_t
1775dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1776{
1777 return (ds->ds_fsid_guid);
1778}
1779
1780void
1781dsl_dataset_space(dsl_dataset_t *ds,
1782 uint64_t *refdbytesp, uint64_t *availbytesp,
1783 uint64_t *usedobjsp, uint64_t *availobjsp)
1784{
d683ddbb 1785 *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
34dc7c2f 1786 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
d683ddbb
JG
1787 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
1788 *availbytesp +=
1789 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
34dc7c2f
BB
1790 if (ds->ds_quota != 0) {
1791 /*
1792 * Adjust available bytes according to refquota
1793 */
1794 if (*refdbytesp < ds->ds_quota)
1795 *availbytesp = MIN(*availbytesp,
1796 ds->ds_quota - *refdbytesp);
1797 else
1798 *availbytesp = 0;
1799 }
d683ddbb 1800 *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
34dc7c2f
BB
1801 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1802}
1803
1804boolean_t
19580676 1805dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
34dc7c2f 1806{
19580676
MA
1807 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1808 if (snap == NULL)
34dc7c2f 1809 return (B_FALSE);
d683ddbb
JG
1810 if (dsl_dataset_phys(ds)->ds_bp.blk_birth >
1811 dsl_dataset_phys(snap)->ds_creation_txg) {
19580676 1812 objset_t *os, *os_snap;
572e2857
BB
1813 /*
1814 * It may be that only the ZIL differs, because it was
1815 * reset in the head. Don't count that as being
1816 * modified.
1817 */
1818 if (dmu_objset_from_ds(ds, &os) != 0)
1819 return (B_TRUE);
19580676 1820 if (dmu_objset_from_ds(snap, &os_snap) != 0)
572e2857
BB
1821 return (B_TRUE);
1822 return (bcmp(&os->os_phys->os_meta_dnode,
19580676 1823 &os_snap->os_phys->os_meta_dnode,
572e2857
BB
1824 sizeof (os->os_phys->os_meta_dnode)) != 0);
1825 }
34dc7c2f
BB
1826 return (B_FALSE);
1827}
1828
13fe0198
MA
1829typedef struct dsl_dataset_rename_snapshot_arg {
1830 const char *ddrsa_fsname;
1831 const char *ddrsa_oldsnapname;
1832 const char *ddrsa_newsnapname;
1833 boolean_t ddrsa_recursive;
1834 dmu_tx_t *ddrsa_tx;
1835} dsl_dataset_rename_snapshot_arg_t;
1836
34dc7c2f
BB
1837/* ARGSUSED */
1838static int
13fe0198
MA
1839dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1840 dsl_dataset_t *hds, void *arg)
34dc7c2f 1841{
13fe0198
MA
1842 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1843 int error;
34dc7c2f 1844 uint64_t val;
34dc7c2f 1845
13fe0198
MA
1846 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1847 if (error != 0) {
1848 /* ignore nonexistent snapshots */
1849 return (error == ENOENT ? 0 : error);
1850 }
34dc7c2f 1851
13fe0198
MA
1852 /* new name should not exist */
1853 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1854 if (error == 0)
2e528b49 1855 error = SET_ERROR(EEXIST);
13fe0198
MA
1856 else if (error == ENOENT)
1857 error = 0;
34dc7c2f
BB
1858
1859 /* dataset name + 1 for the "@" + the new snapshot name must fit */
13fe0198
MA
1860 if (dsl_dir_namelen(hds->ds_dir) + 1 +
1861 strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
2e528b49 1862 error = SET_ERROR(ENAMETOOLONG);
34dc7c2f 1863
13fe0198 1864 return (error);
34dc7c2f
BB
1865}
1866
13fe0198
MA
1867static int
1868dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1869{
13fe0198
MA
1870 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1871 dsl_pool_t *dp = dmu_tx_pool(tx);
34dc7c2f 1872 dsl_dataset_t *hds;
13fe0198 1873 int error;
34dc7c2f 1874
13fe0198
MA
1875 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1876 if (error != 0)
1877 return (error);
34dc7c2f 1878
13fe0198
MA
1879 if (ddrsa->ddrsa_recursive) {
1880 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1881 dsl_dataset_rename_snapshot_check_impl, ddrsa,
1882 DS_FIND_CHILDREN);
1883 } else {
1884 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1885 }
b128c09f 1886 dsl_dataset_rele(hds, FTAG);
13fe0198 1887 return (error);
34dc7c2f
BB
1888}
1889
34dc7c2f 1890static int
13fe0198
MA
1891dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1892 dsl_dataset_t *hds, void *arg)
34dc7c2f 1893{
13fe0198
MA
1894 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1895 dsl_dataset_t *ds;
1896 uint64_t val;
1897 dmu_tx_t *tx = ddrsa->ddrsa_tx;
1898 int error;
34dc7c2f 1899
13fe0198
MA
1900 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1901 ASSERT(error == 0 || error == ENOENT);
1902 if (error == ENOENT) {
1903 /* ignore nonexistent snapshots */
1904 return (0);
34dc7c2f
BB
1905 }
1906
13fe0198
MA
1907 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1908
1909 /* log before we change the name */
1910 spa_history_log_internal_ds(ds, "rename", tx,
1911 "-> @%s", ddrsa->ddrsa_newsnapname);
34dc7c2f 1912
788eb90c
JJ
1913 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
1914 B_FALSE));
13fe0198
MA
1915 mutex_enter(&ds->ds_lock);
1916 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1917 mutex_exit(&ds->ds_lock);
d683ddbb
JG
1918 VERIFY0(zap_add(dp->dp_meta_objset,
1919 dsl_dataset_phys(hds)->ds_snapnames_zapobj,
13fe0198 1920 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
34dc7c2f 1921
13fe0198 1922 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
1923 return (0);
1924}
1925
13fe0198
MA
1926static void
1927dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1928{
13fe0198
MA
1929 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1930 dsl_pool_t *dp = dmu_tx_pool(tx);
1931 dsl_dataset_t *hds;
34dc7c2f 1932
13fe0198
MA
1933 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1934 ddrsa->ddrsa_tx = tx;
1935 if (ddrsa->ddrsa_recursive) {
1936 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1937 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1938 DS_FIND_CHILDREN));
1939 } else {
1940 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
34dc7c2f 1941 }
13fe0198 1942 dsl_dataset_rele(hds, FTAG);
34dc7c2f
BB
1943}
1944
13fe0198
MA
1945int
1946dsl_dataset_rename_snapshot(const char *fsname,
1947 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
34dc7c2f 1948{
2078f210
SS
1949#ifdef _KERNEL
1950 char *oldname, *newname;
1951#endif
1952 int error;
1953
13fe0198 1954 dsl_dataset_rename_snapshot_arg_t ddrsa;
34dc7c2f 1955
13fe0198
MA
1956 ddrsa.ddrsa_fsname = fsname;
1957 ddrsa.ddrsa_oldsnapname = oldsnapname;
1958 ddrsa.ddrsa_newsnapname = newsnapname;
1959 ddrsa.ddrsa_recursive = recursive;
34dc7c2f 1960
2078f210 1961 error = dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
3d45fdd6
MA
1962 dsl_dataset_rename_snapshot_sync, &ddrsa,
1963 1, ZFS_SPACE_CHECK_RESERVED);
2078f210
SS
1964
1965 if (error)
1966 return (SET_ERROR(error));
1967
1968#ifdef _KERNEL
1969 oldname = kmem_asprintf("%s@%s", fsname, oldsnapname);
1970 newname = kmem_asprintf("%s@%s", fsname, newsnapname);
1971 zvol_rename_minors(oldname, newname);
1972 strfree(newname);
1973 strfree(oldname);
1974#endif
1975
1976 return (0);
34dc7c2f
BB
1977}
1978
831baf06
KW
1979/*
1980 * If we're doing an ownership handoff, we need to make sure that there is
1981 * only one long hold on the dataset. We're not allowed to change anything here
1982 * so we don't permanently release the long hold or regular hold here. We want
1983 * to do this only when syncing to avoid the dataset unexpectedly going away
1984 * when we release the long hold.
1985 */
1986static int
1987dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
1988{
1989 boolean_t held;
1990
1991 if (!dmu_tx_is_syncing(tx))
1992 return (0);
1993
1994 if (owner != NULL) {
1995 VERIFY3P(ds->ds_owner, ==, owner);
1996 dsl_dataset_long_rele(ds, owner);
1997 }
1998
1999 held = dsl_dataset_long_held(ds);
2000
2001 if (owner != NULL)
2002 dsl_dataset_long_hold(ds, owner);
2003
2004 if (held)
2005 return (SET_ERROR(EBUSY));
2006
2007 return (0);
2008}
2009
2010typedef struct dsl_dataset_rollback_arg {
2011 const char *ddra_fsname;
2012 void *ddra_owner;
46ba1e59 2013 nvlist_t *ddra_result;
831baf06
KW
2014} dsl_dataset_rollback_arg_t;
2015
13fe0198
MA
2016static int
2017dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
34dc7c2f 2018{
831baf06 2019 dsl_dataset_rollback_arg_t *ddra = arg;
13fe0198 2020 dsl_pool_t *dp = dmu_tx_pool(tx);
34dc7c2f 2021 dsl_dataset_t *ds;
13fe0198
MA
2022 int64_t unused_refres_delta;
2023 int error;
da536844
MA
2024 nvpair_t *pair;
2025 nvlist_t *proprequest, *bookmarks;
34dc7c2f 2026
831baf06 2027 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
13fe0198
MA
2028 if (error != 0)
2029 return (error);
428870ff 2030
13fe0198 2031 /* must not be a snapshot */
0c66c32d 2032 if (ds->ds_is_snapshot) {
13fe0198 2033 dsl_dataset_rele(ds, FTAG);
2e528b49 2034 return (SET_ERROR(EINVAL));
13fe0198 2035 }
34dc7c2f 2036
13fe0198 2037 /* must have a most recent snapshot */
d683ddbb 2038 if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
13fe0198 2039 dsl_dataset_rele(ds, FTAG);
2e528b49 2040 return (SET_ERROR(EINVAL));
13fe0198 2041 }
34dc7c2f 2042
da536844 2043 /* must not have any bookmarks after the most recent snapshot */
79c76d5b 2044 proprequest = fnvlist_alloc();
da536844 2045 fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
79c76d5b 2046 bookmarks = fnvlist_alloc();
da536844
MA
2047 error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
2048 fnvlist_free(proprequest);
2049 if (error != 0)
2050 return (error);
2051 for (pair = nvlist_next_nvpair(bookmarks, NULL);
2052 pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
2053 nvlist_t *valuenv =
2054 fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
2055 zfs_prop_to_name(ZFS_PROP_CREATETXG));
2056 uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
d683ddbb 2057 if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
da536844
MA
2058 fnvlist_free(bookmarks);
2059 dsl_dataset_rele(ds, FTAG);
2060 return (SET_ERROR(EEXIST));
2061 }
2062 }
2063 fnvlist_free(bookmarks);
2064
831baf06
KW
2065 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
2066 if (error != 0) {
13fe0198 2067 dsl_dataset_rele(ds, FTAG);
831baf06 2068 return (error);
34dc7c2f 2069 }
428870ff 2070
13fe0198
MA
2071 /*
2072 * Check if the snap we are rolling back to uses more than
2073 * the refquota.
2074 */
2075 if (ds->ds_quota != 0 &&
d683ddbb 2076 dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
13fe0198 2077 dsl_dataset_rele(ds, FTAG);
2e528b49 2078 return (SET_ERROR(EDQUOT));
34dc7c2f
BB
2079 }
2080
13fe0198
MA
2081 /*
2082 * When we do the clone swap, we will temporarily use more space
2083 * due to the refreservation (the head will no longer have any
2084 * unique space, so the entire amount of the refreservation will need
2085 * to be free). We will immediately destroy the clone, freeing
2086 * this space, but the freeing happens over many txg's.
2087 */
2088 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
d683ddbb 2089 dsl_dataset_phys(ds)->ds_unique_bytes);
34dc7c2f 2090
13fe0198
MA
2091 if (unused_refres_delta > 0 &&
2092 unused_refres_delta >
2093 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
2094 dsl_dataset_rele(ds, FTAG);
2e528b49 2095 return (SET_ERROR(ENOSPC));
13fe0198 2096 }
34dc7c2f 2097
13fe0198
MA
2098 dsl_dataset_rele(ds, FTAG);
2099 return (0);
2100}
34dc7c2f 2101
13fe0198
MA
2102static void
2103dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
2104{
831baf06 2105 dsl_dataset_rollback_arg_t *ddra = arg;
13fe0198
MA
2106 dsl_pool_t *dp = dmu_tx_pool(tx);
2107 dsl_dataset_t *ds, *clone;
2108 uint64_t cloneobj;
46ba1e59 2109 char namebuf[ZFS_MAXNAMELEN];
34dc7c2f 2110
831baf06 2111 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
34dc7c2f 2112
46ba1e59
MA
2113 dsl_dataset_name(ds->ds_prev, namebuf);
2114 fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2115
13fe0198
MA
2116 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
2117 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
2118
2119 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
2120
2121 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
2122 dsl_dataset_zero_zil(ds, tx);
2123
2124 dsl_destroy_head_sync_impl(clone, tx);
2125
2126 dsl_dataset_rele(clone, FTAG);
2127 dsl_dataset_rele(ds, FTAG);
2128}
2129
831baf06 2130/*
46ba1e59
MA
2131 * Rolls back the given filesystem or volume to the most recent snapshot.
2132 * The name of the most recent snapshot will be returned under key "target"
2133 * in the result nvlist.
831baf06 2134 *
46ba1e59 2135 * If owner != NULL:
831baf06
KW
2136 * - The existing dataset MUST be owned by the specified owner at entry
2137 * - Upon return, dataset will still be held by the same owner, whether we
2138 * succeed or not.
2139 *
2140 * This mode is required any time the existing filesystem is mounted. See
2141 * notes above zfs_suspend_fs() for further details.
2142 */
13fe0198 2143int
46ba1e59 2144dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
13fe0198 2145{
831baf06
KW
2146 dsl_dataset_rollback_arg_t ddra;
2147
2148 ddra.ddra_fsname = fsname;
2149 ddra.ddra_owner = owner;
46ba1e59 2150 ddra.ddra_result = result;
831baf06 2151
13fe0198 2152 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
3d45fdd6
MA
2153 dsl_dataset_rollback_sync, &ddra,
2154 1, ZFS_SPACE_CHECK_RESERVED));
34dc7c2f
BB
2155}
2156
b128c09f
BB
2157struct promotenode {
2158 list_node_t link;
2159 dsl_dataset_t *ds;
2160};
2161
13fe0198
MA
2162typedef struct dsl_dataset_promote_arg {
2163 const char *ddpa_clonename;
2164 dsl_dataset_t *ddpa_clone;
b128c09f 2165 list_t shared_snaps, origin_snaps, clone_snaps;
13fe0198 2166 dsl_dataset_t *origin_origin; /* origin of the origin */
b128c09f 2167 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
428870ff 2168 char *err_ds;
788eb90c 2169 cred_t *cr;
13fe0198 2170} dsl_dataset_promote_arg_t;
34dc7c2f 2171
b128c09f 2172static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
13fe0198
MA
2173static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
2174 void *tag);
2175static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
b128c09f 2176
34dc7c2f 2177static int
13fe0198 2178dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
34dc7c2f 2179{
13fe0198
MA
2180 dsl_dataset_promote_arg_t *ddpa = arg;
2181 dsl_pool_t *dp = dmu_tx_pool(tx);
2182 dsl_dataset_t *hds;
2183 struct promotenode *snap;
2184 dsl_dataset_t *origin_ds;
34dc7c2f 2185 int err;
428870ff 2186 uint64_t unused;
788eb90c 2187 uint64_t ss_mv_cnt;
fec41709 2188 size_t max_snap_len;
34dc7c2f 2189
13fe0198
MA
2190 err = promote_hold(ddpa, dp, FTAG);
2191 if (err != 0)
2192 return (err);
34dc7c2f 2193
13fe0198 2194 hds = ddpa->ddpa_clone;
fec41709 2195 max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
34dc7c2f 2196
d683ddbb 2197 if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
13fe0198 2198 promote_rele(ddpa, FTAG);
2e528b49 2199 return (SET_ERROR(EXDEV));
13fe0198
MA
2200 }
2201
2202 /*
2203 * Compute and check the amount of space to transfer. Since this is
2204 * so expensive, don't do the preliminary check.
2205 */
2206 if (!dmu_tx_is_syncing(tx)) {
2207 promote_rele(ddpa, FTAG);
2208 return (0);
2209 }
2210
2211 snap = list_head(&ddpa->shared_snaps);
2212 origin_ds = snap->ds;
34dc7c2f
BB
2213
2214 /* compute origin's new unique space */
13fe0198 2215 snap = list_tail(&ddpa->clone_snaps);
d683ddbb
JG
2216 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2217 origin_ds->ds_object);
428870ff 2218 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
d683ddbb 2219 dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
13fe0198 2220 &ddpa->unique, &unused, &unused);
34dc7c2f 2221
b128c09f
BB
2222 /*
2223 * Walk the snapshots that we are moving
2224 *
2225 * Compute space to transfer. Consider the incremental changes
13fe0198 2226 * to used by each snapshot:
b128c09f
BB
2227 * (my used) = (prev's used) + (blocks born) - (blocks killed)
2228 * So each snapshot gave birth to:
2229 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2230 * So a sequence would look like:
2231 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2232 * Which simplifies to:
2233 * uN + kN + kN-1 + ... + k1 + k0
2234 * Note however, if we stop before we reach the ORIGIN we get:
2235 * uN + kN + kN-1 + ... + kM - uM-1
2236 */
788eb90c 2237 ss_mv_cnt = 0;
d683ddbb
JG
2238 ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2239 ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2240 ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
13fe0198
MA
2241 for (snap = list_head(&ddpa->shared_snaps); snap;
2242 snap = list_next(&ddpa->shared_snaps, snap)) {
34dc7c2f 2243 uint64_t val, dlused, dlcomp, dluncomp;
b128c09f 2244 dsl_dataset_t *ds = snap->ds;
34dc7c2f 2245
788eb90c
JJ
2246 ss_mv_cnt++;
2247
13fe0198
MA
2248 /*
2249 * If there are long holds, we won't be able to evict
2250 * the objset.
2251 */
2252 if (dsl_dataset_long_held(ds)) {
2e528b49 2253 err = SET_ERROR(EBUSY);
13fe0198
MA
2254 goto out;
2255 }
2256
34dc7c2f 2257 /* Check that the snapshot name does not conflict */
13fe0198 2258 VERIFY0(dsl_dataset_get_snapname(ds));
fec41709
AG
2259 if (strlen(ds->ds_snapname) >= max_snap_len) {
2260 err = SET_ERROR(ENAMETOOLONG);
2261 goto out;
2262 }
b128c09f 2263 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
428870ff 2264 if (err == 0) {
13fe0198 2265 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2e528b49 2266 err = SET_ERROR(EEXIST);
428870ff
BB
2267 goto out;
2268 }
b128c09f 2269 if (err != ENOENT)
428870ff 2270 goto out;
34dc7c2f 2271
b128c09f 2272 /* The very first snapshot does not have a deadlist */
d683ddbb 2273 if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
b128c09f 2274 continue;
34dc7c2f 2275
428870ff
BB
2276 dsl_deadlist_space(&ds->ds_deadlist,
2277 &dlused, &dlcomp, &dluncomp);
13fe0198
MA
2278 ddpa->used += dlused;
2279 ddpa->comp += dlcomp;
2280 ddpa->uncomp += dluncomp;
b128c09f 2281 }
34dc7c2f 2282
b128c09f
BB
2283 /*
2284 * If we are a clone of a clone then we never reached ORIGIN,
2285 * so we need to subtract out the clone origin's used space.
2286 */
13fe0198 2287 if (ddpa->origin_origin) {
d683ddbb
JG
2288 ddpa->used -=
2289 dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2290 ddpa->comp -=
2291 dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
13fe0198 2292 ddpa->uncomp -=
d683ddbb
JG
2293 dsl_dataset_phys(ddpa->origin_origin)->
2294 ds_uncompressed_bytes;
34dc7c2f
BB
2295 }
2296
788eb90c 2297 /* Check that there is enough space and limit headroom here */
b128c09f 2298 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
788eb90c 2299 0, ss_mv_cnt, ddpa->used, ddpa->cr);
13fe0198
MA
2300 if (err != 0)
2301 goto out;
34dc7c2f 2302
b128c09f
BB
2303 /*
2304 * Compute the amounts of space that will be used by snapshots
2305 * after the promotion (for both origin and clone). For each,
2306 * it is the amount of space that will be on all of their
2307 * deadlists (that was not born before their new origin).
2308 */
d683ddbb 2309 if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
b128c09f
BB
2310 uint64_t space;
2311
2312 /*
2313 * Note, typically this will not be a clone of a clone,
428870ff
BB
2314 * so dd_origin_txg will be < TXG_INITIAL, so
2315 * these snaplist_space() -> dsl_deadlist_space_range()
b128c09f
BB
2316 * calls will be fast because they do not have to
2317 * iterate over all bps.
2318 */
13fe0198
MA
2319 snap = list_head(&ddpa->origin_snaps);
2320 err = snaplist_space(&ddpa->shared_snaps,
2321 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2322 if (err != 0)
2323 goto out;
b128c09f 2324
13fe0198 2325 err = snaplist_space(&ddpa->clone_snaps,
428870ff 2326 snap->ds->ds_dir->dd_origin_txg, &space);
13fe0198
MA
2327 if (err != 0)
2328 goto out;
2329 ddpa->cloneusedsnap += space;
b128c09f 2330 }
d683ddbb
JG
2331 if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2332 DD_FLAG_USED_BREAKDOWN) {
13fe0198 2333 err = snaplist_space(&ddpa->origin_snaps,
d683ddbb
JG
2334 dsl_dataset_phys(origin_ds)->ds_creation_txg,
2335 &ddpa->originusedsnap);
13fe0198
MA
2336 if (err != 0)
2337 goto out;
b128c09f
BB
2338 }
2339
428870ff 2340out:
13fe0198 2341 promote_rele(ddpa, FTAG);
428870ff 2342 return (err);
34dc7c2f
BB
2343}
2344
2345static void
13fe0198 2346dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 2347{
13fe0198
MA
2348 dsl_dataset_promote_arg_t *ddpa = arg;
2349 dsl_pool_t *dp = dmu_tx_pool(tx);
2350 dsl_dataset_t *hds;
2351 struct promotenode *snap;
2352 dsl_dataset_t *origin_ds;
b128c09f 2353 dsl_dataset_t *origin_head;
13fe0198 2354 dsl_dir_t *dd;
34dc7c2f 2355 dsl_dir_t *odd = NULL;
b128c09f
BB
2356 uint64_t oldnext_obj;
2357 int64_t delta;
34dc7c2f 2358
13fe0198
MA
2359 VERIFY0(promote_hold(ddpa, dp, FTAG));
2360 hds = ddpa->ddpa_clone;
2361
d683ddbb 2362 ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
13fe0198
MA
2363
2364 snap = list_head(&ddpa->shared_snaps);
2365 origin_ds = snap->ds;
2366 dd = hds->ds_dir;
34dc7c2f 2367
13fe0198 2368 snap = list_head(&ddpa->origin_snaps);
b128c09f
BB
2369 origin_head = snap->ds;
2370
34dc7c2f
BB
2371 /*
2372 * We need to explicitly open odd, since origin_ds's dd will be
2373 * changing.
2374 */
13fe0198 2375 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
34dc7c2f
BB
2376 NULL, FTAG, &odd));
2377
b128c09f
BB
2378 /* change origin's next snap */
2379 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
d683ddbb 2380 oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
13fe0198 2381 snap = list_tail(&ddpa->clone_snaps);
d683ddbb
JG
2382 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2383 origin_ds->ds_object);
2384 dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
b128c09f
BB
2385
2386 /* change the origin's next clone */
d683ddbb 2387 if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
13fe0198
MA
2388 dsl_dataset_remove_from_next_clones(origin_ds,
2389 snap->ds->ds_object, tx);
2390 VERIFY0(zap_add_int(dp->dp_meta_objset,
d683ddbb 2391 dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
b128c09f
BB
2392 oldnext_obj, tx));
2393 }
2394
2395 /* change origin */
2396 dmu_buf_will_dirty(dd->dd_dbuf, tx);
d683ddbb
JG
2397 ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2398 dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
428870ff 2399 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
b128c09f 2400 dmu_buf_will_dirty(odd->dd_dbuf, tx);
d683ddbb 2401 dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
428870ff 2402 origin_head->ds_dir->dd_origin_txg =
d683ddbb 2403 dsl_dataset_phys(origin_ds)->ds_creation_txg;
428870ff
BB
2404
2405 /* change dd_clone entries */
2406 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
13fe0198 2407 VERIFY0(zap_remove_int(dp->dp_meta_objset,
d683ddbb 2408 dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
13fe0198 2409 VERIFY0(zap_add_int(dp->dp_meta_objset,
d683ddbb 2410 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
428870ff
BB
2411 hds->ds_object, tx));
2412
13fe0198 2413 VERIFY0(zap_remove_int(dp->dp_meta_objset,
d683ddbb 2414 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
428870ff 2415 origin_head->ds_object, tx));
d683ddbb
JG
2416 if (dsl_dir_phys(dd)->dd_clones == 0) {
2417 dsl_dir_phys(dd)->dd_clones =
2418 zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2419 DMU_OT_NONE, 0, tx);
428870ff 2420 }
13fe0198 2421 VERIFY0(zap_add_int(dp->dp_meta_objset,
d683ddbb 2422 dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
428870ff 2423 }
34dc7c2f 2424
b128c09f 2425 /* move snapshots to this dir */
13fe0198
MA
2426 for (snap = list_head(&ddpa->shared_snaps); snap;
2427 snap = list_next(&ddpa->shared_snaps, snap)) {
b128c09f
BB
2428 dsl_dataset_t *ds = snap->ds;
2429
13fe0198
MA
2430 /*
2431 * Property callbacks are registered to a particular
2432 * dsl_dir. Since ours is changing, evict the objset
2433 * so that they will be unregistered from the old dsl_dir.
2434 */
428870ff
BB
2435 if (ds->ds_objset) {
2436 dmu_objset_evict(ds->ds_objset);
2437 ds->ds_objset = NULL;
b128c09f 2438 }
13fe0198 2439
34dc7c2f 2440 /* move snap name entry */
13fe0198
MA
2441 VERIFY0(dsl_dataset_get_snapname(ds));
2442 VERIFY0(dsl_dataset_snap_remove(origin_head,
788eb90c 2443 ds->ds_snapname, tx, B_TRUE));
13fe0198 2444 VERIFY0(zap_add(dp->dp_meta_objset,
d683ddbb 2445 dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
34dc7c2f 2446 8, 1, &ds->ds_object, tx));
788eb90c
JJ
2447 dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2448 DD_FIELD_SNAPSHOT_COUNT, tx);
428870ff 2449
34dc7c2f
BB
2450 /* change containing dsl_dir */
2451 dmu_buf_will_dirty(ds->ds_dbuf, tx);
d683ddbb
JG
2452 ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
2453 dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
34dc7c2f 2454 ASSERT3P(ds->ds_dir, ==, odd);
13fe0198
MA
2455 dsl_dir_rele(ds->ds_dir, ds);
2456 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
34dc7c2f
BB
2457 NULL, ds, &ds->ds_dir));
2458
428870ff 2459 /* move any clone references */
d683ddbb 2460 if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
428870ff
BB
2461 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2462 zap_cursor_t zc;
2463 zap_attribute_t za;
2464
2465 for (zap_cursor_init(&zc, dp->dp_meta_objset,
d683ddbb 2466 dsl_dataset_phys(ds)->ds_next_clones_obj);
428870ff 2467 zap_cursor_retrieve(&zc, &za) == 0;
13fe0198
MA
2468 zap_cursor_advance(&zc)) {
2469 dsl_dataset_t *cnds;
2470 uint64_t o;
34dc7c2f 2471
13fe0198
MA
2472 if (za.za_first_integer == oldnext_obj) {
2473 /*
2474 * We've already moved the
2475 * origin's reference.
2476 */
2477 continue;
2478 }
34dc7c2f 2479
13fe0198
MA
2480 VERIFY0(dsl_dataset_hold_obj(dp,
2481 za.za_first_integer, FTAG, &cnds));
d683ddbb
JG
2482 o = dsl_dir_phys(cnds->ds_dir)->
2483 dd_head_dataset_obj;
34dc7c2f 2484
13fe0198 2485 VERIFY0(zap_remove_int(dp->dp_meta_objset,
d683ddbb 2486 dsl_dir_phys(odd)->dd_clones, o, tx));
13fe0198 2487 VERIFY0(zap_add_int(dp->dp_meta_objset,
d683ddbb 2488 dsl_dir_phys(dd)->dd_clones, o, tx));
13fe0198
MA
2489 dsl_dataset_rele(cnds, FTAG);
2490 }
2491 zap_cursor_fini(&zc);
2492 }
34dc7c2f 2493
13fe0198 2494 ASSERT(!dsl_prop_hascb(ds));
34dc7c2f
BB
2495 }
2496
34dc7c2f 2497 /*
13fe0198
MA
2498 * Change space accounting.
2499 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2500 * both be valid, or both be 0 (resulting in delta == 0). This
2501 * is true for each of {clone,origin} independently.
34dc7c2f 2502 */
570827e1 2503
13fe0198 2504 delta = ddpa->cloneusedsnap -
d683ddbb 2505 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
13fe0198
MA
2506 ASSERT3S(delta, >=, 0);
2507 ASSERT3U(ddpa->used, >=, delta);
2508 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2509 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2510 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
34dc7c2f 2511
13fe0198 2512 delta = ddpa->originusedsnap -
d683ddbb 2513 dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
13fe0198
MA
2514 ASSERT3S(delta, <=, 0);
2515 ASSERT3U(ddpa->used, >=, -delta);
2516 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2517 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2518 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2519
d683ddbb 2520 dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
13fe0198
MA
2521
2522 /* log history record */
2523 spa_history_log_internal_ds(hds, "promote", tx, "");
2524
2525 dsl_dir_rele(odd, FTAG);
2526 promote_rele(ddpa, FTAG);
34dc7c2f
BB
2527}
2528
13fe0198
MA
2529/*
2530 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2531 * (exclusive) and last_obj (inclusive). The list will be in reverse
2532 * order (last_obj will be the list_head()). If first_obj == 0, do all
2533 * snapshots back to this dataset's origin.
2534 */
34dc7c2f 2535static int
13fe0198
MA
2536snaplist_make(dsl_pool_t *dp,
2537 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
34dc7c2f 2538{
13fe0198 2539 uint64_t obj = last_obj;
34dc7c2f 2540
13fe0198
MA
2541 list_create(l, sizeof (struct promotenode),
2542 offsetof(struct promotenode, link));
34dc7c2f 2543
13fe0198
MA
2544 while (obj != first_obj) {
2545 dsl_dataset_t *ds;
2546 struct promotenode *snap;
2547 int err;
428870ff 2548
13fe0198
MA
2549 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2550 ASSERT(err != ENOENT);
2551 if (err != 0)
2552 return (err);
34dc7c2f 2553
13fe0198 2554 if (first_obj == 0)
d683ddbb 2555 first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
13fe0198 2556
79c76d5b 2557 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
13fe0198
MA
2558 snap->ds = ds;
2559 list_insert_tail(l, snap);
d683ddbb 2560 obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
13fe0198 2561 }
34dc7c2f
BB
2562
2563 return (0);
2564}
2565
13fe0198
MA
2566static int
2567snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
34dc7c2f 2568{
13fe0198 2569 struct promotenode *snap;
6f1ffb06 2570
13fe0198
MA
2571 *spacep = 0;
2572 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2573 uint64_t used, comp, uncomp;
2574 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2575 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2576 *spacep += used;
428870ff 2577 }
13fe0198 2578 return (0);
34dc7c2f
BB
2579}
2580
13fe0198
MA
2581static void
2582snaplist_destroy(list_t *l, void *tag)
34dc7c2f 2583{
13fe0198 2584 struct promotenode *snap;
428870ff 2585
13fe0198
MA
2586 if (l == NULL || !list_link_active(&l->list_head))
2587 return;
34dc7c2f 2588
13fe0198
MA
2589 while ((snap = list_tail(l)) != NULL) {
2590 list_remove(l, snap);
2591 dsl_dataset_rele(snap->ds, tag);
2592 kmem_free(snap, sizeof (*snap));
2593 }
2594 list_destroy(l);
34dc7c2f
BB
2595}
2596
2597static int
13fe0198 2598promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
34dc7c2f 2599{
13fe0198
MA
2600 int error;
2601 dsl_dir_t *dd;
2602 struct promotenode *snap;
34dc7c2f 2603
13fe0198
MA
2604 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2605 &ddpa->ddpa_clone);
2606 if (error != 0)
2607 return (error);
2608 dd = ddpa->ddpa_clone->ds_dir;
34dc7c2f 2609
0c66c32d 2610 if (ddpa->ddpa_clone->ds_is_snapshot ||
13fe0198
MA
2611 !dsl_dir_is_clone(dd)) {
2612 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2e528b49 2613 return (SET_ERROR(EINVAL));
13fe0198 2614 }
34dc7c2f 2615
d683ddbb 2616 error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
13fe0198
MA
2617 &ddpa->shared_snaps, tag);
2618 if (error != 0)
2619 goto out;
34dc7c2f 2620
13fe0198
MA
2621 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2622 &ddpa->clone_snaps, tag);
2623 if (error != 0)
2624 goto out;
34dc7c2f 2625
13fe0198 2626 snap = list_head(&ddpa->shared_snaps);
d683ddbb
JG
2627 ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
2628 error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
2629 dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
13fe0198
MA
2630 &ddpa->origin_snaps, tag);
2631 if (error != 0)
2632 goto out;
d164b209 2633
d683ddbb 2634 if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
13fe0198 2635 error = dsl_dataset_hold_obj(dp,
d683ddbb 2636 dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
13fe0198
MA
2637 tag, &ddpa->origin_origin);
2638 if (error != 0)
2639 goto out;
d164b209 2640 }
13fe0198
MA
2641out:
2642 if (error != 0)
2643 promote_rele(ddpa, tag);
2644 return (error);
34dc7c2f
BB
2645}
2646
34dc7c2f 2647static void
13fe0198 2648promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
34dc7c2f 2649{
13fe0198
MA
2650 snaplist_destroy(&ddpa->shared_snaps, tag);
2651 snaplist_destroy(&ddpa->clone_snaps, tag);
2652 snaplist_destroy(&ddpa->origin_snaps, tag);
2653 if (ddpa->origin_origin != NULL)
2654 dsl_dataset_rele(ddpa->origin_origin, tag);
2655 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2656}
428870ff 2657
13fe0198
MA
2658/*
2659 * Promote a clone.
2660 *
2661 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2662 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2663 */
2664int
2665dsl_dataset_promote(const char *name, char *conflsnap)
2666{
2667 dsl_dataset_promote_arg_t ddpa = { 0 };
2668 uint64_t numsnaps;
2669 int error;
2670 objset_t *os;
34dc7c2f 2671
13fe0198
MA
2672 /*
2673 * We will modify space proportional to the number of
2674 * snapshots. Compute numsnaps.
2675 */
2676 error = dmu_objset_hold(name, FTAG, &os);
2677 if (error != 0)
2678 return (error);
2679 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
d683ddbb
JG
2680 dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2681 &numsnaps);
13fe0198
MA
2682 dmu_objset_rele(os, FTAG);
2683 if (error != 0)
2684 return (error);
34dc7c2f 2685
13fe0198
MA
2686 ddpa.ddpa_clonename = name;
2687 ddpa.err_ds = conflsnap;
788eb90c 2688 ddpa.cr = CRED();
6f1ffb06 2689
13fe0198 2690 return (dsl_sync_task(name, dsl_dataset_promote_check,
3d45fdd6
MA
2691 dsl_dataset_promote_sync, &ddpa,
2692 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
34dc7c2f
BB
2693}
2694
2695int
13fe0198 2696dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
831baf06 2697 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
34dc7c2f 2698{
13fe0198 2699 int64_t unused_refres_delta;
34dc7c2f 2700
13fe0198 2701 /* they should both be heads */
0c66c32d
JG
2702 if (clone->ds_is_snapshot ||
2703 origin_head->ds_is_snapshot)
2e528b49 2704 return (SET_ERROR(EINVAL));
428870ff 2705
19580676
MA
2706 /* if we are not forcing, the branch point should be just before them */
2707 if (!force && clone->ds_prev != origin_head->ds_prev)
2e528b49 2708 return (SET_ERROR(EINVAL));
34dc7c2f 2709
13fe0198
MA
2710 /* clone should be the clone (unless they are unrelated) */
2711 if (clone->ds_prev != NULL &&
2712 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
19580676 2713 origin_head->ds_dir != clone->ds_prev->ds_dir)
2e528b49 2714 return (SET_ERROR(EINVAL));
428870ff 2715
13fe0198
MA
2716 /* the clone should be a child of the origin */
2717 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2e528b49 2718 return (SET_ERROR(EINVAL));
45d1cae3 2719
13fe0198 2720 /* origin_head shouldn't be modified unless 'force' */
19580676
MA
2721 if (!force &&
2722 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2e528b49 2723 return (SET_ERROR(ETXTBSY));
572e2857 2724
13fe0198 2725 /* origin_head should have no long holds (e.g. is not mounted) */
831baf06 2726 if (dsl_dataset_handoff_check(origin_head, owner, tx))
2e528b49 2727 return (SET_ERROR(EBUSY));
13fe0198
MA
2728
2729 /* check amount of any unconsumed refreservation */
2730 unused_refres_delta =
2731 (int64_t)MIN(origin_head->ds_reserved,
d683ddbb 2732 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
13fe0198 2733 (int64_t)MIN(origin_head->ds_reserved,
d683ddbb 2734 dsl_dataset_phys(clone)->ds_unique_bytes);
13fe0198
MA
2735
2736 if (unused_refres_delta > 0 &&
2737 unused_refres_delta >
2738 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2e528b49 2739 return (SET_ERROR(ENOSPC));
13fe0198
MA
2740
2741 /* clone can't be over the head's refquota */
2742 if (origin_head->ds_quota != 0 &&
d683ddbb
JG
2743 dsl_dataset_phys(clone)->ds_referenced_bytes >
2744 origin_head->ds_quota)
2e528b49 2745 return (SET_ERROR(EDQUOT));
572e2857 2746
13fe0198 2747 return (0);
572e2857
BB
2748}
2749
2750void
13fe0198
MA
2751dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2752 dsl_dataset_t *origin_head, dmu_tx_t *tx)
572e2857 2753{
241b5415 2754 spa_feature_t f;
13fe0198
MA
2755 dsl_pool_t *dp = dmu_tx_pool(tx);
2756 int64_t unused_refres_delta;
428870ff 2757
13fe0198
MA
2758 ASSERT(clone->ds_reserved == 0);
2759 ASSERT(origin_head->ds_quota == 0 ||
d683ddbb 2760 dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota);
19580676 2761 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
45d1cae3 2762
241b5415
MA
2763 /*
2764 * Swap per-dataset feature flags.
2765 */
2766 for (f = 0; f < SPA_FEATURES; f++) {
2767 boolean_t clone_inuse;
2768 boolean_t origin_head_inuse;
2769
2770 if (!(spa_feature_table[f].fi_flags &
2771 ZFEATURE_FLAG_PER_DATASET)) {
2772 ASSERT(!clone->ds_feature_inuse[f]);
2773 ASSERT(!origin_head->ds_feature_inuse[f]);
2774 continue;
2775 }
2776
2777 clone_inuse = clone->ds_feature_inuse[f];
2778 origin_head_inuse = origin_head->ds_feature_inuse[f];
2779
2780 if (clone_inuse) {
2781 dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
2782 clone->ds_feature_inuse[f] = B_FALSE;
2783 }
2784 if (origin_head_inuse) {
2785 dsl_dataset_deactivate_feature(origin_head->ds_object,
2786 f, tx);
2787 origin_head->ds_feature_inuse[f] = B_FALSE;
2788 }
2789 if (clone_inuse) {
2790 dsl_dataset_activate_feature(origin_head->ds_object,
2791 f, tx);
2792 origin_head->ds_feature_inuse[f] = B_TRUE;
2793 }
2794 if (origin_head_inuse) {
2795 dsl_dataset_activate_feature(clone->ds_object, f, tx);
2796 clone->ds_feature_inuse[f] = B_TRUE;
2797 }
2798 }
2799
13fe0198
MA
2800 dmu_buf_will_dirty(clone->ds_dbuf, tx);
2801 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
45d1cae3 2802
13fe0198
MA
2803 if (clone->ds_objset != NULL) {
2804 dmu_objset_evict(clone->ds_objset);
2805 clone->ds_objset = NULL;
2806 }
45d1cae3 2807
13fe0198
MA
2808 if (origin_head->ds_objset != NULL) {
2809 dmu_objset_evict(origin_head->ds_objset);
2810 origin_head->ds_objset = NULL;
45d1cae3 2811 }
45d1cae3 2812
13fe0198
MA
2813 unused_refres_delta =
2814 (int64_t)MIN(origin_head->ds_reserved,
d683ddbb 2815 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
13fe0198 2816 (int64_t)MIN(origin_head->ds_reserved,
d683ddbb 2817 dsl_dataset_phys(clone)->ds_unique_bytes);
13fe0198
MA
2818
2819 /*
2820 * Reset origin's unique bytes, if it exists.
2821 */
2822 if (clone->ds_prev) {
2823 dsl_dataset_t *origin = clone->ds_prev;
2824 uint64_t comp, uncomp;
2825
2826 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2827 dsl_deadlist_space_range(&clone->ds_deadlist,
d683ddbb
JG
2828 dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
2829 &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
13fe0198
MA
2830 }
2831
2832 /* swap blkptrs */
2833 {
2834 blkptr_t tmp;
d683ddbb
JG
2835 tmp = dsl_dataset_phys(origin_head)->ds_bp;
2836 dsl_dataset_phys(origin_head)->ds_bp =
2837 dsl_dataset_phys(clone)->ds_bp;
2838 dsl_dataset_phys(clone)->ds_bp = tmp;
13fe0198
MA
2839 }
2840
2841 /* set dd_*_bytes */
2842 {
2843 int64_t dused, dcomp, duncomp;
2844 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2845 uint64_t odl_used, odl_comp, odl_uncomp;
2846
d683ddbb 2847 ASSERT3U(dsl_dir_phys(clone->ds_dir)->
13fe0198
MA
2848 dd_used_breakdown[DD_USED_SNAP], ==, 0);
2849
2850 dsl_deadlist_space(&clone->ds_deadlist,
2851 &cdl_used, &cdl_comp, &cdl_uncomp);
2852 dsl_deadlist_space(&origin_head->ds_deadlist,
2853 &odl_used, &odl_comp, &odl_uncomp);
428870ff 2854
d683ddbb
JG
2855 dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
2856 cdl_used -
2857 (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
2858 odl_used);
2859 dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
2860 cdl_comp -
2861 (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
2862 odl_comp);
2863 duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
13fe0198 2864 cdl_uncomp -
d683ddbb
JG
2865 (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
2866 odl_uncomp);
45d1cae3 2867
13fe0198
MA
2868 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2869 dused, dcomp, duncomp, tx);
2870 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2871 -dused, -dcomp, -duncomp, tx);
45d1cae3 2872
45d1cae3 2873 /*
13fe0198
MA
2874 * The difference in the space used by snapshots is the
2875 * difference in snapshot space due to the head's
2876 * deadlist (since that's the only thing that's
2877 * changing that affects the snapused).
45d1cae3 2878 */
13fe0198
MA
2879 dsl_deadlist_space_range(&clone->ds_deadlist,
2880 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2881 &cdl_used, &cdl_comp, &cdl_uncomp);
2882 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2883 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2884 &odl_used, &odl_comp, &odl_uncomp);
2885 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2886 DD_USED_HEAD, DD_USED_SNAP, tx);
45d1cae3 2887 }
45d1cae3 2888
13fe0198 2889 /* swap ds_*_bytes */
d683ddbb
JG
2890 SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
2891 dsl_dataset_phys(clone)->ds_referenced_bytes);
2892 SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
2893 dsl_dataset_phys(clone)->ds_compressed_bytes);
2894 SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
2895 dsl_dataset_phys(clone)->ds_uncompressed_bytes);
2896 SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
2897 dsl_dataset_phys(clone)->ds_unique_bytes);
45d1cae3 2898
13fe0198
MA
2899 /* apply any parent delta for change in unconsumed refreservation */
2900 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2901 unused_refres_delta, 0, 0, tx);
45d1cae3 2902
13fe0198
MA
2903 /*
2904 * Swap deadlists.
2905 */
2906 dsl_deadlist_close(&clone->ds_deadlist);
2907 dsl_deadlist_close(&origin_head->ds_deadlist);
d683ddbb
JG
2908 SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
2909 dsl_dataset_phys(clone)->ds_deadlist_obj);
13fe0198 2910 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
d683ddbb 2911 dsl_dataset_phys(clone)->ds_deadlist_obj);
13fe0198 2912 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
d683ddbb 2913 dsl_dataset_phys(origin_head)->ds_deadlist_obj);
45d1cae3 2914
13fe0198 2915 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
45d1cae3 2916
13fe0198
MA
2917 spa_history_log_internal_ds(clone, "clone swap", tx,
2918 "parent=%s", origin_head->ds_dir->dd_myname);
45d1cae3
BB
2919}
2920
13fe0198
MA
2921/*
2922 * Given a pool name and a dataset object number in that pool,
2923 * return the name of that dataset.
2924 */
572e2857 2925int
13fe0198 2926dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
572e2857 2927{
13fe0198
MA
2928 dsl_pool_t *dp;
2929 dsl_dataset_t *ds;
572e2857
BB
2930 int error;
2931
13fe0198
MA
2932 error = dsl_pool_hold(pname, FTAG, &dp);
2933 if (error != 0)
2934 return (error);
2935
2936 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2937 if (error == 0) {
2938 dsl_dataset_name(ds, buf);
2939 dsl_dataset_rele(ds, FTAG);
2940 }
2941 dsl_pool_rele(dp, FTAG);
572e2857
BB
2942
2943 return (error);
2944}
2945
45d1cae3 2946int
13fe0198
MA
2947dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2948 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
45d1cae3 2949{
13fe0198 2950 int error = 0;
45d1cae3 2951
13fe0198 2952 ASSERT3S(asize, >, 0);
45d1cae3 2953
13fe0198
MA
2954 /*
2955 * *ref_rsrv is the portion of asize that will come from any
2956 * unconsumed refreservation space.
2957 */
2958 *ref_rsrv = 0;
45d1cae3 2959
13fe0198
MA
2960 mutex_enter(&ds->ds_lock);
2961 /*
2962 * Make a space adjustment for reserved bytes.
2963 */
d683ddbb 2964 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
13fe0198 2965 ASSERT3U(*used, >=,
d683ddbb
JG
2966 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
2967 *used -=
2968 (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
13fe0198
MA
2969 *ref_rsrv =
2970 asize - MIN(asize, parent_delta(ds, asize + inflight));
45d1cae3
BB
2971 }
2972
13fe0198
MA
2973 if (!check_quota || ds->ds_quota == 0) {
2974 mutex_exit(&ds->ds_lock);
2975 return (0);
45d1cae3 2976 }
13fe0198
MA
2977 /*
2978 * If they are requesting more space, and our current estimate
2979 * is over quota, they get to try again unless the actual
2980 * on-disk is over quota and there are no pending changes (which
2981 * may free up space for us).
2982 */
d683ddbb
JG
2983 if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
2984 ds->ds_quota) {
13fe0198 2985 if (inflight > 0 ||
d683ddbb 2986 dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
2e528b49 2987 error = SET_ERROR(ERESTART);
13fe0198 2988 else
2e528b49 2989 error = SET_ERROR(EDQUOT);
45d1cae3 2990 }
13fe0198 2991 mutex_exit(&ds->ds_lock);
45d1cae3 2992
45d1cae3
BB
2993 return (error);
2994}
2995
13fe0198
MA
2996typedef struct dsl_dataset_set_qr_arg {
2997 const char *ddsqra_name;
2998 zprop_source_t ddsqra_source;
2999 uint64_t ddsqra_value;
3000} dsl_dataset_set_qr_arg_t;
45d1cae3 3001
13fe0198
MA
3002
3003/* ARGSUSED */
45d1cae3 3004static int
13fe0198 3005dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
45d1cae3 3006{
13fe0198
MA
3007 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3008 dsl_pool_t *dp = dmu_tx_pool(tx);
3009 dsl_dataset_t *ds;
45d1cae3 3010 int error;
13fe0198 3011 uint64_t newval;
45d1cae3 3012
13fe0198 3013 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2e528b49 3014 return (SET_ERROR(ENOTSUP));
45d1cae3 3015
13fe0198
MA
3016 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3017 if (error != 0)
3018 return (error);
3019
0c66c32d 3020 if (ds->ds_is_snapshot) {
13fe0198 3021 dsl_dataset_rele(ds, FTAG);
2e528b49 3022 return (SET_ERROR(EINVAL));
45d1cae3
BB
3023 }
3024
13fe0198
MA
3025 error = dsl_prop_predict(ds->ds_dir,
3026 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3027 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3028 if (error != 0) {
3029 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
3030 return (error);
3031 }
3032
13fe0198
MA
3033 if (newval == 0) {
3034 dsl_dataset_rele(ds, FTAG);
3035 return (0);
3036 }
3037
d683ddbb 3038 if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
13fe0198
MA
3039 newval < ds->ds_reserved) {
3040 dsl_dataset_rele(ds, FTAG);
2e528b49 3041 return (SET_ERROR(ENOSPC));
13fe0198 3042 }
45d1cae3 3043
13fe0198 3044 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
3045 return (0);
3046}
3047
13fe0198
MA
3048static void
3049dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
45d1cae3 3050{
13fe0198
MA
3051 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3052 dsl_pool_t *dp = dmu_tx_pool(tx);
3053 dsl_dataset_t *ds;
3054 uint64_t newval;
45d1cae3 3055
13fe0198 3056 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
45d1cae3 3057
13fe0198
MA
3058 dsl_prop_set_sync_impl(ds,
3059 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3060 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
3061 &ddsqra->ddsqra_value, tx);
45d1cae3 3062
13fe0198
MA
3063 VERIFY0(dsl_prop_get_int_ds(ds,
3064 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
45d1cae3 3065
13fe0198
MA
3066 if (ds->ds_quota != newval) {
3067 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3068 ds->ds_quota = newval;
45d1cae3 3069 }
13fe0198 3070 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
3071}
3072
13fe0198
MA
3073int
3074dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
3075 uint64_t refquota)
45d1cae3 3076{
13fe0198 3077 dsl_dataset_set_qr_arg_t ddsqra;
428870ff 3078
13fe0198
MA
3079 ddsqra.ddsqra_name = dsname;
3080 ddsqra.ddsqra_source = source;
3081 ddsqra.ddsqra_value = refquota;
3082
3083 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
3d45fdd6 3084 dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
45d1cae3
BB
3085}
3086
3087static int
13fe0198 3088dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
45d1cae3 3089{
13fe0198
MA
3090 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3091 dsl_pool_t *dp = dmu_tx_pool(tx);
45d1cae3
BB
3092 dsl_dataset_t *ds;
3093 int error;
13fe0198 3094 uint64_t newval, unique;
428870ff 3095
13fe0198 3096 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2e528b49 3097 return (SET_ERROR(ENOTSUP));
45d1cae3 3098
13fe0198
MA
3099 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3100 if (error != 0)
45d1cae3 3101 return (error);
45d1cae3 3102
0c66c32d 3103 if (ds->ds_is_snapshot) {
13fe0198 3104 dsl_dataset_rele(ds, FTAG);
2e528b49 3105 return (SET_ERROR(EINVAL));
45d1cae3
BB
3106 }
3107
13fe0198
MA
3108 error = dsl_prop_predict(ds->ds_dir,
3109 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3110 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3111 if (error != 0) {
3112 dsl_dataset_rele(ds, FTAG);
45d1cae3
BB
3113 return (error);
3114 }
3115
13fe0198
MA
3116 /*
3117 * If we are doing the preliminary check in open context, the
3118 * space estimates may be inaccurate.
3119 */
3120 if (!dmu_tx_is_syncing(tx)) {
3121 dsl_dataset_rele(ds, FTAG);
3122 return (0);
45d1cae3 3123 }
45d1cae3 3124
13fe0198
MA
3125 mutex_enter(&ds->ds_lock);
3126 if (!DS_UNIQUE_IS_ACCURATE(ds))
3127 dsl_dataset_recalc_head_uniq(ds);
d683ddbb 3128 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
13fe0198 3129 mutex_exit(&ds->ds_lock);
45d1cae3 3130
13fe0198
MA
3131 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
3132 uint64_t delta = MAX(unique, newval) -
3133 MAX(unique, ds->ds_reserved);
45d1cae3 3134
13fe0198
MA
3135 if (delta >
3136 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
3137 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
3138 dsl_dataset_rele(ds, FTAG);
2e528b49 3139 return (SET_ERROR(ENOSPC));
13fe0198 3140 }
45d1cae3
BB
3141 }
3142
13fe0198
MA
3143 dsl_dataset_rele(ds, FTAG);
3144 return (0);
45d1cae3
BB
3145}
3146
13fe0198
MA
3147void
3148dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
3149 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
428870ff 3150{
13fe0198
MA
3151 uint64_t newval;
3152 uint64_t unique;
3153 int64_t delta;
428870ff 3154
13fe0198
MA
3155 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3156 source, sizeof (value), 1, &value, tx);
428870ff 3157
13fe0198
MA
3158 VERIFY0(dsl_prop_get_int_ds(ds,
3159 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
572e2857 3160
13fe0198
MA
3161 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3162 mutex_enter(&ds->ds_dir->dd_lock);
3163 mutex_enter(&ds->ds_lock);
3164 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
d683ddbb 3165 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
13fe0198
MA
3166 delta = MAX(0, (int64_t)(newval - unique)) -
3167 MAX(0, (int64_t)(ds->ds_reserved - unique));
3168 ds->ds_reserved = newval;
3169 mutex_exit(&ds->ds_lock);
572e2857 3170
13fe0198
MA
3171 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3172 mutex_exit(&ds->ds_dir->dd_lock);
428870ff
BB
3173}
3174
13fe0198
MA
3175static void
3176dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
45d1cae3 3177{
13fe0198
MA
3178 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3179 dsl_pool_t *dp = dmu_tx_pool(tx);
45d1cae3 3180 dsl_dataset_t *ds;
45d1cae3 3181
13fe0198
MA
3182 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3183 dsl_dataset_set_refreservation_sync_impl(ds,
3184 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
45d1cae3 3185 dsl_dataset_rele(ds, FTAG);
45d1cae3 3186}
428870ff 3187
428870ff 3188int
13fe0198
MA
3189dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
3190 uint64_t refreservation)
428870ff 3191{
13fe0198 3192 dsl_dataset_set_qr_arg_t ddsqra;
428870ff 3193
13fe0198
MA
3194 ddsqra.ddsqra_name = dsname;
3195 ddsqra.ddsqra_source = source;
3196 ddsqra.ddsqra_value = refreservation;
c28b2279 3197
13fe0198 3198 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
3d45fdd6
MA
3199 dsl_dataset_set_refreservation_sync, &ddsqra,
3200 0, ZFS_SPACE_CHECK_NONE));
13fe0198 3201}
330d06f9
MA
3202
3203/*
3204 * Return (in *usedp) the amount of space written in new that is not
3205 * present in oldsnap. New may be a snapshot or the head. Old must be
3206 * a snapshot before new, in new's filesystem (or its origin). If not then
3207 * fail and return EINVAL.
3208 *
3209 * The written space is calculated by considering two components: First, we
3210 * ignore any freed space, and calculate the written as new's used space
3211 * minus old's used space. Next, we add in the amount of space that was freed
3212 * between the two snapshots, thus reducing new's used space relative to old's.
3213 * Specifically, this is the space that was born before old->ds_creation_txg,
3214 * and freed before new (ie. on new's deadlist or a previous deadlist).
3215 *
3216 * space freed [---------------------]
3217 * snapshots ---O-------O--------O-------O------
3218 * oldsnap new
3219 */
3220int
3221dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
3222 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3223{
3224 int err = 0;
3225 uint64_t snapobj;
3226 dsl_pool_t *dp = new->ds_dir->dd_pool;
3227
13fe0198
MA
3228 ASSERT(dsl_pool_config_held(dp));
3229
330d06f9 3230 *usedp = 0;
d683ddbb
JG
3231 *usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3232 *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
330d06f9
MA
3233
3234 *compp = 0;
d683ddbb
JG
3235 *compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3236 *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
330d06f9
MA
3237
3238 *uncompp = 0;
d683ddbb
JG
3239 *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3240 *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
330d06f9 3241
330d06f9
MA
3242 snapobj = new->ds_object;
3243 while (snapobj != oldsnap->ds_object) {
3244 dsl_dataset_t *snap;
3245 uint64_t used, comp, uncomp;
3246
9ae529ec
CS
3247 if (snapobj == new->ds_object) {
3248 snap = new;
3249 } else {
3250 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3251 if (err != 0)
3252 break;
3253 }
330d06f9 3254
d683ddbb
JG
3255 if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3256 dsl_dataset_phys(oldsnap)->ds_creation_txg) {
330d06f9
MA
3257 /*
3258 * The blocks in the deadlist can not be born after
3259 * ds_prev_snap_txg, so get the whole deadlist space,
3260 * which is more efficient (especially for old-format
3261 * deadlists). Unfortunately the deadlist code
3262 * doesn't have enough information to make this
3263 * optimization itself.
3264 */
3265 dsl_deadlist_space(&snap->ds_deadlist,
3266 &used, &comp, &uncomp);
3267 } else {
3268 dsl_deadlist_space_range(&snap->ds_deadlist,
d683ddbb 3269 0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
330d06f9
MA
3270 &used, &comp, &uncomp);
3271 }
3272 *usedp += used;
3273 *compp += comp;
3274 *uncompp += uncomp;
3275
3276 /*
3277 * If we get to the beginning of the chain of snapshots
3278 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
3279 * was not a snapshot of/before new.
3280 */
d683ddbb 3281 snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
9ae529ec
CS
3282 if (snap != new)
3283 dsl_dataset_rele(snap, FTAG);
330d06f9 3284 if (snapobj == 0) {
2e528b49 3285 err = SET_ERROR(EINVAL);
330d06f9
MA
3286 break;
3287 }
3288
3289 }
330d06f9
MA
3290 return (err);
3291}
3292
3293/*
3294 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
3295 * lastsnap, and all snapshots in between are deleted.
3296 *
3297 * blocks that would be freed [---------------------------]
3298 * snapshots ---O-------O--------O-------O--------O
3299 * firstsnap lastsnap
3300 *
3301 * This is the set of blocks that were born after the snap before firstsnap,
3302 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
3303 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
3304 * We calculate this by iterating over the relevant deadlists (from the snap
3305 * after lastsnap, backward to the snap after firstsnap), summing up the
3306 * space on the deadlist that was born after the snap before firstsnap.
3307 */
3308int
3309dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
3310 dsl_dataset_t *lastsnap,
3311 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3312{
3313 int err = 0;
3314 uint64_t snapobj;
3315 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
3316
0c66c32d
JG
3317 ASSERT(firstsnap->ds_is_snapshot);
3318 ASSERT(lastsnap->ds_is_snapshot);
330d06f9
MA
3319
3320 /*
3321 * Check that the snapshots are in the same dsl_dir, and firstsnap
3322 * is before lastsnap.
3323 */
3324 if (firstsnap->ds_dir != lastsnap->ds_dir ||
d683ddbb
JG
3325 dsl_dataset_phys(firstsnap)->ds_creation_txg >
3326 dsl_dataset_phys(lastsnap)->ds_creation_txg)
2e528b49 3327 return (SET_ERROR(EINVAL));
330d06f9
MA
3328
3329 *usedp = *compp = *uncompp = 0;
3330
d683ddbb 3331 snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
330d06f9
MA
3332 while (snapobj != firstsnap->ds_object) {
3333 dsl_dataset_t *ds;
3334 uint64_t used, comp, uncomp;
3335
3336 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
3337 if (err != 0)
3338 break;
3339
3340 dsl_deadlist_space_range(&ds->ds_deadlist,
d683ddbb 3341 dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
330d06f9
MA
3342 &used, &comp, &uncomp);
3343 *usedp += used;
3344 *compp += comp;
3345 *uncompp += uncomp;
3346
d683ddbb 3347 snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
330d06f9
MA
3348 ASSERT3U(snapobj, !=, 0);
3349 dsl_dataset_rele(ds, FTAG);
3350 }
330d06f9
MA
3351 return (err);
3352}
3353
13fe0198
MA
3354/*
3355 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3356 * For example, they could both be snapshots of the same filesystem, and
3357 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3358 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3359 * filesystem. Or 'earlier' could be the origin's origin.
da536844
MA
3360 *
3361 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
13fe0198
MA
3362 */
3363boolean_t
da536844
MA
3364dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
3365 uint64_t earlier_txg)
13fe0198
MA
3366{
3367 dsl_pool_t *dp = later->ds_dir->dd_pool;
3368 int error;
3369 boolean_t ret;
3370 dsl_dataset_t *origin;
3371
3372 ASSERT(dsl_pool_config_held(dp));
0c66c32d 3373 ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
da536844
MA
3374
3375 if (earlier_txg == 0)
d683ddbb 3376 earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
13fe0198 3377
0c66c32d 3378 if (later->ds_is_snapshot &&
d683ddbb 3379 earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
13fe0198
MA
3380 return (B_FALSE);
3381
3382 if (later->ds_dir == earlier->ds_dir)
3383 return (B_TRUE);
3384 if (!dsl_dir_is_clone(later->ds_dir))
3385 return (B_FALSE);
3386
d683ddbb 3387 if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
13fe0198
MA
3388 return (B_TRUE);
3389 error = dsl_dataset_hold_obj(dp,
d683ddbb 3390 dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
13fe0198
MA
3391 if (error != 0)
3392 return (B_FALSE);
da536844 3393 ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
13fe0198
MA
3394 dsl_dataset_rele(origin, FTAG);
3395 return (ret);
3396}
3397
fa86b5db
MA
3398void
3399dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
3400{
3401 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3402 dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
3403}
3404
c28b2279 3405#if defined(_KERNEL) && defined(HAVE_SPL)
f1512ee6
MA
3406#if defined(_LP64)
3407module_param(zfs_max_recordsize, int, 0644);
3408MODULE_PARM_DESC(zfs_max_recordsize, "Max allowed record size");
3409#else
3410/* Limited to 1M on 32-bit platforms due to lack of virtual address space */
3411module_param(zfs_max_recordsize, int, 0444);
3412MODULE_PARM_DESC(zfs_max_recordsize, "Max allowed record size");
3413#endif
3414
c28b2279
BB
3415EXPORT_SYMBOL(dsl_dataset_hold);
3416EXPORT_SYMBOL(dsl_dataset_hold_obj);
3417EXPORT_SYMBOL(dsl_dataset_own);
3418EXPORT_SYMBOL(dsl_dataset_own_obj);
3419EXPORT_SYMBOL(dsl_dataset_name);
3420EXPORT_SYMBOL(dsl_dataset_rele);
3421EXPORT_SYMBOL(dsl_dataset_disown);
c28b2279 3422EXPORT_SYMBOL(dsl_dataset_tryown);
c28b2279
BB
3423EXPORT_SYMBOL(dsl_dataset_create_sync);
3424EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
c28b2279
BB
3425EXPORT_SYMBOL(dsl_dataset_snapshot_check);
3426EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
c28b2279 3427EXPORT_SYMBOL(dsl_dataset_promote);
c28b2279
BB
3428EXPORT_SYMBOL(dsl_dataset_user_hold);
3429EXPORT_SYMBOL(dsl_dataset_user_release);
c28b2279
BB
3430EXPORT_SYMBOL(dsl_dataset_get_holds);
3431EXPORT_SYMBOL(dsl_dataset_get_blkptr);
3432EXPORT_SYMBOL(dsl_dataset_set_blkptr);
3433EXPORT_SYMBOL(dsl_dataset_get_spa);
19580676 3434EXPORT_SYMBOL(dsl_dataset_modified_since_snap);
330d06f9
MA
3435EXPORT_SYMBOL(dsl_dataset_space_written);
3436EXPORT_SYMBOL(dsl_dataset_space_wouldfree);
c28b2279
BB
3437EXPORT_SYMBOL(dsl_dataset_sync);
3438EXPORT_SYMBOL(dsl_dataset_block_born);
3439EXPORT_SYMBOL(dsl_dataset_block_kill);
3440EXPORT_SYMBOL(dsl_dataset_block_freeable);
3441EXPORT_SYMBOL(dsl_dataset_prev_snap_txg);
3442EXPORT_SYMBOL(dsl_dataset_dirty);
3443EXPORT_SYMBOL(dsl_dataset_stats);
3444EXPORT_SYMBOL(dsl_dataset_fast_stat);
3445EXPORT_SYMBOL(dsl_dataset_space);
3446EXPORT_SYMBOL(dsl_dataset_fsid_guid);
3447EXPORT_SYMBOL(dsl_dsobj_to_dsname);
3448EXPORT_SYMBOL(dsl_dataset_check_quota);
13fe0198
MA
3449EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl);
3450EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl);
c28b2279 3451#endif