]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_dataset.c
Add arc_state_t stats to arcstats
[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.
f5fc4aca 23 * Copyright (c) 2011 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
34dc7c2f
BB
26#include <sys/dmu_objset.h>
27#include <sys/dsl_dataset.h>
28#include <sys/dsl_dir.h>
29#include <sys/dsl_prop.h>
30#include <sys/dsl_synctask.h>
31#include <sys/dmu_traverse.h>
32#include <sys/dmu_tx.h>
33#include <sys/arc.h>
34#include <sys/zio.h>
35#include <sys/zap.h>
36#include <sys/unique.h>
37#include <sys/zfs_context.h>
38#include <sys/zfs_ioctl.h>
39#include <sys/spa.h>
b128c09f 40#include <sys/zfs_znode.h>
572e2857 41#include <sys/zfs_onexit.h>
45d1cae3 42#include <sys/zvol.h>
428870ff
BB
43#include <sys/dsl_scan.h>
44#include <sys/dsl_deadlist.h>
45
b128c09f
BB
46static char *dsl_reaper = "the grim reaper";
47
34dc7c2f
BB
48static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
49static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
34dc7c2f
BB
50static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
51
428870ff
BB
52#define SWITCH64(x, y) \
53 { \
54 uint64_t __tmp = (x); \
55 (x) = (y); \
56 (y) = __tmp; \
57 }
58
34dc7c2f
BB
59#define DS_REF_MAX (1ULL << 62)
60
61#define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE
62
b128c09f
BB
63#define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper)
64
34dc7c2f
BB
65
66/*
67 * Figure out how much of this delta should be propogated to the dsl_dir
68 * layer. If there's a refreservation, that space has already been
69 * partially accounted for in our ancestors.
70 */
71static int64_t
72parent_delta(dsl_dataset_t *ds, int64_t delta)
73{
74 uint64_t old_bytes, new_bytes;
75
76 if (ds->ds_reserved == 0)
77 return (delta);
78
79 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
80 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
81
82 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
83 return (new_bytes - old_bytes);
84}
85
86void
428870ff 87dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
34dc7c2f 88{
d6320ddb 89 int used, compressed, uncompressed;
34dc7c2f
BB
90 int64_t delta;
91
d6320ddb
BB
92 used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
93 compressed = BP_GET_PSIZE(bp);
94 uncompressed = BP_GET_UCSIZE(bp);
95
428870ff 96 dprintf_bp(bp, "ds=%p", ds);
34dc7c2f
BB
97
98 ASSERT(dmu_tx_is_syncing(tx));
99 /* It could have been compressed away to nothing */
100 if (BP_IS_HOLE(bp))
101 return;
102 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
103 ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
104 if (ds == NULL) {
105 /*
106 * Account for the meta-objset space in its placeholder
107 * dsl_dir.
108 */
109 ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
b128c09f 110 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
34dc7c2f
BB
111 used, compressed, uncompressed, tx);
112 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
113 return;
114 }
115 dmu_buf_will_dirty(ds->ds_dbuf, tx);
428870ff 116
b128c09f 117 mutex_enter(&ds->ds_dir->dd_lock);
34dc7c2f
BB
118 mutex_enter(&ds->ds_lock);
119 delta = parent_delta(ds, used);
120 ds->ds_phys->ds_used_bytes += used;
121 ds->ds_phys->ds_compressed_bytes += compressed;
122 ds->ds_phys->ds_uncompressed_bytes += uncompressed;
123 ds->ds_phys->ds_unique_bytes += used;
124 mutex_exit(&ds->ds_lock);
b128c09f
BB
125 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
126 compressed, uncompressed, tx);
127 dsl_dir_transfer_space(ds->ds_dir, used - delta,
128 DD_USED_REFRSRV, DD_USED_HEAD, tx);
129 mutex_exit(&ds->ds_dir->dd_lock);
34dc7c2f
BB
130}
131
b128c09f 132int
428870ff
BB
133dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
134 boolean_t async)
34dc7c2f 135{
d6320ddb
BB
136 int used, compressed, uncompressed;
137
34dc7c2f 138 if (BP_IS_HOLE(bp))
b128c09f 139 return (0);
34dc7c2f 140
428870ff
BB
141 ASSERT(dmu_tx_is_syncing(tx));
142 ASSERT(bp->blk_birth <= tx->tx_txg);
143
d6320ddb
BB
144 used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
145 compressed = BP_GET_PSIZE(bp);
146 uncompressed = BP_GET_UCSIZE(bp);
428870ff 147
34dc7c2f
BB
148 ASSERT(used > 0);
149 if (ds == NULL) {
34dc7c2f
BB
150 /*
151 * Account for the meta-objset space in its placeholder
152 * dataset.
153 */
428870ff 154 dsl_free(tx->tx_pool, tx->tx_txg, bp);
34dc7c2f 155
b128c09f 156 dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
34dc7c2f
BB
157 -used, -compressed, -uncompressed, tx);
158 dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
b128c09f 159 return (used);
34dc7c2f
BB
160 }
161 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
162
b128c09f 163 ASSERT(!dsl_dataset_is_snapshot(ds));
34dc7c2f
BB
164 dmu_buf_will_dirty(ds->ds_dbuf, tx);
165
166 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
34dc7c2f
BB
167 int64_t delta;
168
428870ff
BB
169 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
170 dsl_free(tx->tx_pool, tx->tx_txg, bp);
34dc7c2f 171
b128c09f 172 mutex_enter(&ds->ds_dir->dd_lock);
34dc7c2f
BB
173 mutex_enter(&ds->ds_lock);
174 ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
175 !DS_UNIQUE_IS_ACCURATE(ds));
176 delta = parent_delta(ds, -used);
177 ds->ds_phys->ds_unique_bytes -= used;
178 mutex_exit(&ds->ds_lock);
b128c09f 179 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
34dc7c2f 180 delta, -compressed, -uncompressed, tx);
b128c09f
BB
181 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
182 DD_USED_REFRSRV, DD_USED_HEAD, tx);
183 mutex_exit(&ds->ds_dir->dd_lock);
34dc7c2f
BB
184 } else {
185 dprintf_bp(bp, "putting on dead list: %s", "");
428870ff
BB
186 if (async) {
187 /*
188 * We are here as part of zio's write done callback,
189 * which means we're a zio interrupt thread. We can't
190 * call dsl_deadlist_insert() now because it may block
191 * waiting for I/O. Instead, put bp on the deferred
192 * queue and let dsl_pool_sync() finish the job.
193 */
194 bplist_append(&ds->ds_pending_deadlist, bp);
195 } else {
196 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
197 }
34dc7c2f
BB
198 ASSERT3U(ds->ds_prev->ds_object, ==,
199 ds->ds_phys->ds_prev_snap_obj);
200 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
201 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
202 if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
203 ds->ds_object && bp->blk_birth >
204 ds->ds_prev->ds_phys->ds_prev_snap_txg) {
205 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
206 mutex_enter(&ds->ds_prev->ds_lock);
207 ds->ds_prev->ds_phys->ds_unique_bytes += used;
208 mutex_exit(&ds->ds_prev->ds_lock);
209 }
428870ff 210 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
b128c09f
BB
211 dsl_dir_transfer_space(ds->ds_dir, used,
212 DD_USED_HEAD, DD_USED_SNAP, tx);
213 }
34dc7c2f
BB
214 }
215 mutex_enter(&ds->ds_lock);
216 ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
217 ds->ds_phys->ds_used_bytes -= used;
218 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
219 ds->ds_phys->ds_compressed_bytes -= compressed;
220 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
221 ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
222 mutex_exit(&ds->ds_lock);
b128c09f
BB
223
224 return (used);
34dc7c2f
BB
225}
226
227uint64_t
228dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
229{
230 uint64_t trysnap = 0;
231
232 if (ds == NULL)
233 return (0);
234 /*
235 * The snapshot creation could fail, but that would cause an
236 * incorrect FALSE return, which would only result in an
237 * overestimation of the amount of space that an operation would
238 * consume, which is OK.
239 *
240 * There's also a small window where we could miss a pending
241 * snapshot, because we could set the sync task in the quiescing
242 * phase. So this should only be used as a guess.
243 */
244 if (ds->ds_trysnap_txg >
245 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
246 trysnap = ds->ds_trysnap_txg;
247 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
248}
249
9babb374 250boolean_t
428870ff
BB
251dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
252 uint64_t blk_birth)
34dc7c2f 253{
428870ff
BB
254 if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
255 return (B_FALSE);
256
572e2857 257 ddt_prefetch(dsl_dataset_get_spa(ds), bp);
428870ff
BB
258
259 return (B_TRUE);
34dc7c2f
BB
260}
261
262/* ARGSUSED */
263static void
264dsl_dataset_evict(dmu_buf_t *db, void *dsv)
265{
266 dsl_dataset_t *ds = dsv;
267
b128c09f 268 ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
34dc7c2f 269
34dc7c2f
BB
270 unique_remove(ds->ds_fsid_guid);
271
428870ff
BB
272 if (ds->ds_objset != NULL)
273 dmu_objset_evict(ds->ds_objset);
34dc7c2f
BB
274
275 if (ds->ds_prev) {
b128c09f 276 dsl_dataset_drop_ref(ds->ds_prev, ds);
34dc7c2f
BB
277 ds->ds_prev = NULL;
278 }
279
428870ff
BB
280 bplist_destroy(&ds->ds_pending_deadlist);
281 if (db != NULL) {
282 dsl_deadlist_close(&ds->ds_deadlist);
283 } else {
284 ASSERT(ds->ds_deadlist.dl_dbuf == NULL);
285 ASSERT(!ds->ds_deadlist.dl_oldfmt);
286 }
b128c09f
BB
287 if (ds->ds_dir)
288 dsl_dir_close(ds->ds_dir, ds);
34dc7c2f
BB
289
290 ASSERT(!list_link_active(&ds->ds_synced_link));
291
292 mutex_destroy(&ds->ds_lock);
45d1cae3 293 mutex_destroy(&ds->ds_recvlock);
34dc7c2f 294 mutex_destroy(&ds->ds_opening_lock);
b128c09f
BB
295 rw_destroy(&ds->ds_rwlock);
296 cv_destroy(&ds->ds_exclusive_cv);
34dc7c2f
BB
297
298 kmem_free(ds, sizeof (dsl_dataset_t));
299}
300
301static int
302dsl_dataset_get_snapname(dsl_dataset_t *ds)
303{
304 dsl_dataset_phys_t *headphys;
305 int err;
306 dmu_buf_t *headdbuf;
307 dsl_pool_t *dp = ds->ds_dir->dd_pool;
308 objset_t *mos = dp->dp_meta_objset;
309
310 if (ds->ds_snapname[0])
311 return (0);
312 if (ds->ds_phys->ds_next_snap_obj == 0)
313 return (0);
314
315 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
316 FTAG, &headdbuf);
317 if (err)
318 return (err);
319 headphys = headdbuf->db_data;
320 err = zap_value_search(dp->dp_meta_objset,
321 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
322 dmu_buf_rele(headdbuf, FTAG);
323 return (err);
324}
325
326static int
b128c09f 327dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
34dc7c2f 328{
b128c09f
BB
329 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
330 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
34dc7c2f
BB
331 matchtype_t mt;
332 int err;
333
b128c09f 334 if (ds->ds_phys->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
346static int
b128c09f 347dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
34dc7c2f 348{
b128c09f
BB
349 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
350 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
34dc7c2f
BB
351 matchtype_t mt;
352 int err;
353
428870ff
BB
354 dsl_dir_snap_cmtime_update(ds->ds_dir);
355
b128c09f 356 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
34dc7c2f
BB
357 mt = MT_FIRST;
358 else
359 mt = MT_EXACT;
360
b128c09f 361 err = zap_remove_norm(mos, snapobj, name, mt, tx);
34dc7c2f 362 if (err == ENOTSUP && mt == MT_FIRST)
b128c09f 363 err = zap_remove(mos, snapobj, name, tx);
34dc7c2f
BB
364 return (err);
365}
366
b128c09f
BB
367static int
368dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
369 dsl_dataset_t **dsp)
34dc7c2f 370{
34dc7c2f
BB
371 objset_t *mos = dp->dp_meta_objset;
372 dmu_buf_t *dbuf;
373 dsl_dataset_t *ds;
374 int err;
572e2857 375 dmu_object_info_t doi;
34dc7c2f
BB
376
377 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
378 dsl_pool_sync_context(dp));
379
380 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
381 if (err)
382 return (err);
572e2857
BB
383
384 /* Make sure dsobj has the correct object type. */
385 dmu_object_info_from_db(dbuf, &doi);
386 if (doi.doi_type != DMU_OT_DSL_DATASET)
387 return (EINVAL);
388
34dc7c2f
BB
389 ds = dmu_buf_get_user(dbuf);
390 if (ds == NULL) {
d4ed6673 391 dsl_dataset_t *winner = NULL;
34dc7c2f
BB
392
393 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
394 ds->ds_dbuf = dbuf;
395 ds->ds_object = dsobj;
396 ds->ds_phys = dbuf->db_data;
98f72a53 397 list_link_init(&ds->ds_synced_link);
34dc7c2f
BB
398
399 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
45d1cae3 400 mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 401 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
ef5319df 402 rw_init(&ds->ds_rwlock, NULL, RW_DEFAULT, NULL);
b128c09f 403 cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f 404
428870ff
BB
405 bplist_create(&ds->ds_pending_deadlist);
406 dsl_deadlist_open(&ds->ds_deadlist,
34dc7c2f 407 mos, ds->ds_phys->ds_deadlist_obj);
428870ff 408
34dc7c2f
BB
409 if (err == 0) {
410 err = dsl_dir_open_obj(dp,
411 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
412 }
413 if (err) {
34dc7c2f 414 mutex_destroy(&ds->ds_lock);
45d1cae3 415 mutex_destroy(&ds->ds_recvlock);
34dc7c2f 416 mutex_destroy(&ds->ds_opening_lock);
b128c09f
BB
417 rw_destroy(&ds->ds_rwlock);
418 cv_destroy(&ds->ds_exclusive_cv);
428870ff
BB
419 bplist_destroy(&ds->ds_pending_deadlist);
420 dsl_deadlist_close(&ds->ds_deadlist);
34dc7c2f
BB
421 kmem_free(ds, sizeof (dsl_dataset_t));
422 dmu_buf_rele(dbuf, tag);
423 return (err);
424 }
425
b128c09f 426 if (!dsl_dataset_is_snapshot(ds)) {
34dc7c2f
BB
427 ds->ds_snapname[0] = '\0';
428 if (ds->ds_phys->ds_prev_snap_obj) {
b128c09f
BB
429 err = dsl_dataset_get_ref(dp,
430 ds->ds_phys->ds_prev_snap_obj,
431 ds, &ds->ds_prev);
34dc7c2f 432 }
45d1cae3
BB
433 } else {
434 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
435 err = dsl_dataset_get_snapname(ds);
436 if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
437 err = zap_count(
438 ds->ds_dir->dd_pool->dp_meta_objset,
439 ds->ds_phys->ds_userrefs_obj,
440 &ds->ds_userrefs);
441 }
34dc7c2f
BB
442 }
443
b128c09f 444 if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
34dc7c2f
BB
445 /*
446 * In sync context, we're called with either no lock
447 * or with the write lock. If we're not syncing,
448 * we're always called with the read lock held.
449 */
450 boolean_t need_lock =
451 !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
452 dsl_pool_sync_context(dp);
453
454 if (need_lock)
455 rw_enter(&dp->dp_config_rwlock, RW_READER);
456
b128c09f 457 err = dsl_prop_get_ds(ds,
34dc7c2f
BB
458 "refreservation", sizeof (uint64_t), 1,
459 &ds->ds_reserved, NULL);
460 if (err == 0) {
b128c09f 461 err = dsl_prop_get_ds(ds,
34dc7c2f
BB
462 "refquota", sizeof (uint64_t), 1,
463 &ds->ds_quota, NULL);
464 }
465
466 if (need_lock)
467 rw_exit(&dp->dp_config_rwlock);
468 } else {
469 ds->ds_reserved = ds->ds_quota = 0;
470 }
471
472 if (err == 0) {
473 winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
474 dsl_dataset_evict);
475 }
476 if (err || winner) {
428870ff
BB
477 bplist_destroy(&ds->ds_pending_deadlist);
478 dsl_deadlist_close(&ds->ds_deadlist);
b128c09f
BB
479 if (ds->ds_prev)
480 dsl_dataset_drop_ref(ds->ds_prev, ds);
34dc7c2f
BB
481 dsl_dir_close(ds->ds_dir, ds);
482 mutex_destroy(&ds->ds_lock);
45d1cae3 483 mutex_destroy(&ds->ds_recvlock);
34dc7c2f 484 mutex_destroy(&ds->ds_opening_lock);
b128c09f
BB
485 rw_destroy(&ds->ds_rwlock);
486 cv_destroy(&ds->ds_exclusive_cv);
34dc7c2f
BB
487 kmem_free(ds, sizeof (dsl_dataset_t));
488 if (err) {
489 dmu_buf_rele(dbuf, tag);
490 return (err);
491 }
492 ds = winner;
493 } else {
494 ds->ds_fsid_guid =
495 unique_insert(ds->ds_phys->ds_fsid_guid);
496 }
497 }
498 ASSERT3P(ds->ds_dbuf, ==, dbuf);
499 ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
b128c09f
BB
500 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
501 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
502 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
34dc7c2f 503 mutex_enter(&ds->ds_lock);
b128c09f 504 if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
34dc7c2f 505 mutex_exit(&ds->ds_lock);
b128c09f
BB
506 dmu_buf_rele(ds->ds_dbuf, tag);
507 return (ENOENT);
34dc7c2f 508 }
34dc7c2f 509 mutex_exit(&ds->ds_lock);
34dc7c2f
BB
510 *dsp = ds;
511 return (0);
512}
513
b128c09f
BB
514static int
515dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
516{
517 dsl_pool_t *dp = ds->ds_dir->dd_pool;
518
519 /*
520 * In syncing context we don't want the rwlock lock: there
521 * may be an existing writer waiting for sync phase to
522 * finish. We don't need to worry about such writers, since
523 * sync phase is single-threaded, so the writer can't be
524 * doing anything while we are active.
525 */
526 if (dsl_pool_sync_context(dp)) {
527 ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
528 return (0);
529 }
530
531 /*
532 * Normal users will hold the ds_rwlock as a READER until they
533 * are finished (i.e., call dsl_dataset_rele()). "Owners" will
534 * drop their READER lock after they set the ds_owner field.
535 *
536 * If the dataset is being destroyed, the destroy thread will
537 * obtain a WRITER lock for exclusive access after it's done its
538 * open-context work and then change the ds_owner to
539 * dsl_reaper once destruction is assured. So threads
540 * may block here temporarily, until the "destructability" of
541 * the dataset is determined.
542 */
543 ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
544 mutex_enter(&ds->ds_lock);
545 while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
546 rw_exit(&dp->dp_config_rwlock);
547 cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
548 if (DSL_DATASET_IS_DESTROYED(ds)) {
549 mutex_exit(&ds->ds_lock);
550 dsl_dataset_drop_ref(ds, tag);
551 rw_enter(&dp->dp_config_rwlock, RW_READER);
552 return (ENOENT);
553 }
428870ff
BB
554 /*
555 * The dp_config_rwlock lives above the ds_lock. And
556 * we need to check DSL_DATASET_IS_DESTROYED() while
557 * holding the ds_lock, so we have to drop and reacquire
558 * the ds_lock here.
559 */
560 mutex_exit(&ds->ds_lock);
b128c09f 561 rw_enter(&dp->dp_config_rwlock, RW_READER);
428870ff 562 mutex_enter(&ds->ds_lock);
b128c09f
BB
563 }
564 mutex_exit(&ds->ds_lock);
565 return (0);
566}
567
34dc7c2f 568int
b128c09f
BB
569dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
570 dsl_dataset_t **dsp)
571{
572 int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
573
574 if (err)
575 return (err);
576 return (dsl_dataset_hold_ref(*dsp, tag));
577}
578
579int
428870ff
BB
580dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok,
581 void *tag, dsl_dataset_t **dsp)
b128c09f 582{
428870ff 583 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
b128c09f
BB
584 if (err)
585 return (err);
428870ff
BB
586 if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
587 dsl_dataset_rele(*dsp, tag);
9babb374 588 *dsp = NULL;
b128c09f
BB
589 return (EBUSY);
590 }
591 return (0);
592}
593
594int
595dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
34dc7c2f
BB
596{
597 dsl_dir_t *dd;
598 dsl_pool_t *dp;
b128c09f 599 const char *snapname;
34dc7c2f 600 uint64_t obj;
34dc7c2f
BB
601 int err = 0;
602
b128c09f 603 err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
34dc7c2f
BB
604 if (err)
605 return (err);
606
607 dp = dd->dd_pool;
608 obj = dd->dd_phys->dd_head_dataset_obj;
609 rw_enter(&dp->dp_config_rwlock, RW_READER);
b128c09f
BB
610 if (obj)
611 err = dsl_dataset_get_ref(dp, obj, tag, dsp);
612 else
34dc7c2f 613 err = ENOENT;
b128c09f 614 if (err)
34dc7c2f 615 goto out;
34dc7c2f 616
b128c09f 617 err = dsl_dataset_hold_ref(*dsp, tag);
34dc7c2f 618
b128c09f
BB
619 /* we may be looking for a snapshot */
620 if (err == 0 && snapname != NULL) {
621 dsl_dataset_t *ds = NULL;
34dc7c2f 622
b128c09f
BB
623 if (*snapname++ != '@') {
624 dsl_dataset_rele(*dsp, tag);
34dc7c2f
BB
625 err = ENOENT;
626 goto out;
627 }
34dc7c2f 628
b128c09f
BB
629 dprintf("looking for snapshot '%s'\n", snapname);
630 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
631 if (err == 0)
632 err = dsl_dataset_get_ref(dp, obj, tag, &ds);
633 dsl_dataset_rele(*dsp, tag);
634
635 ASSERT3U((err == 0), ==, (ds != NULL));
636
637 if (ds) {
638 mutex_enter(&ds->ds_lock);
639 if (ds->ds_snapname[0] == 0)
640 (void) strlcpy(ds->ds_snapname, snapname,
641 sizeof (ds->ds_snapname));
642 mutex_exit(&ds->ds_lock);
643 err = dsl_dataset_hold_ref(ds, tag);
644 *dsp = err ? NULL : ds;
34dc7c2f 645 }
34dc7c2f 646 }
34dc7c2f
BB
647out:
648 rw_exit(&dp->dp_config_rwlock);
649 dsl_dir_close(dd, FTAG);
34dc7c2f
BB
650 return (err);
651}
652
653int
428870ff
BB
654dsl_dataset_own(const char *name, boolean_t inconsistentok,
655 void *tag, dsl_dataset_t **dsp)
34dc7c2f 656{
428870ff 657 int err = dsl_dataset_hold(name, tag, dsp);
b128c09f
BB
658 if (err)
659 return (err);
428870ff
BB
660 if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
661 dsl_dataset_rele(*dsp, tag);
b128c09f
BB
662 return (EBUSY);
663 }
664 return (0);
34dc7c2f
BB
665}
666
667void
668dsl_dataset_name(dsl_dataset_t *ds, char *name)
669{
670 if (ds == NULL) {
671 (void) strcpy(name, "mos");
672 } else {
673 dsl_dir_name(ds->ds_dir, name);
674 VERIFY(0 == dsl_dataset_get_snapname(ds));
675 if (ds->ds_snapname[0]) {
676 (void) strcat(name, "@");
b128c09f
BB
677 /*
678 * We use a "recursive" mutex so that we
679 * can call dprintf_ds() with ds_lock held.
680 */
34dc7c2f 681 if (!MUTEX_HELD(&ds->ds_lock)) {
34dc7c2f
BB
682 mutex_enter(&ds->ds_lock);
683 (void) strcat(name, ds->ds_snapname);
684 mutex_exit(&ds->ds_lock);
685 } else {
686 (void) strcat(name, ds->ds_snapname);
687 }
688 }
689 }
690}
691
692static int
693dsl_dataset_namelen(dsl_dataset_t *ds)
694{
695 int result;
696
697 if (ds == NULL) {
698 result = 3; /* "mos" */
699 } else {
700 result = dsl_dir_namelen(ds->ds_dir);
701 VERIFY(0 == dsl_dataset_get_snapname(ds));
702 if (ds->ds_snapname[0]) {
703 ++result; /* adding one for the @-sign */
704 if (!MUTEX_HELD(&ds->ds_lock)) {
34dc7c2f
BB
705 mutex_enter(&ds->ds_lock);
706 result += strlen(ds->ds_snapname);
707 mutex_exit(&ds->ds_lock);
708 } else {
709 result += strlen(ds->ds_snapname);
710 }
711 }
712 }
713
714 return (result);
715}
716
717void
b128c09f 718dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
34dc7c2f 719{
34dc7c2f
BB
720 dmu_buf_rele(ds->ds_dbuf, tag);
721}
722
723void
b128c09f
BB
724dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
725{
726 if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
727 rw_exit(&ds->ds_rwlock);
728 }
729 dsl_dataset_drop_ref(ds, tag);
730}
731
732void
428870ff 733dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
34dc7c2f 734{
428870ff 735 ASSERT((ds->ds_owner == tag && ds->ds_dbuf) ||
b128c09f
BB
736 (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
737
34dc7c2f 738 mutex_enter(&ds->ds_lock);
b128c09f
BB
739 ds->ds_owner = NULL;
740 if (RW_WRITE_HELD(&ds->ds_rwlock)) {
741 rw_exit(&ds->ds_rwlock);
742 cv_broadcast(&ds->ds_exclusive_cv);
743 }
34dc7c2f 744 mutex_exit(&ds->ds_lock);
b128c09f 745 if (ds->ds_dbuf)
428870ff 746 dsl_dataset_drop_ref(ds, tag);
b128c09f 747 else
428870ff 748 dsl_dataset_evict(NULL, ds);
34dc7c2f
BB
749}
750
751boolean_t
428870ff 752dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag)
34dc7c2f 753{
b128c09f
BB
754 boolean_t gotit = FALSE;
755
34dc7c2f 756 mutex_enter(&ds->ds_lock);
b128c09f
BB
757 if (ds->ds_owner == NULL &&
758 (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
428870ff 759 ds->ds_owner = tag;
b128c09f
BB
760 if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
761 rw_exit(&ds->ds_rwlock);
762 gotit = TRUE;
34dc7c2f
BB
763 }
764 mutex_exit(&ds->ds_lock);
b128c09f 765 return (gotit);
34dc7c2f
BB
766}
767
768void
b128c09f 769dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
34dc7c2f 770{
b128c09f
BB
771 ASSERT3P(owner, ==, ds->ds_owner);
772 if (!RW_WRITE_HELD(&ds->ds_rwlock))
773 rw_enter(&ds->ds_rwlock, RW_WRITER);
34dc7c2f
BB
774}
775
776uint64_t
b128c09f 777dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
34dc7c2f
BB
778 uint64_t flags, dmu_tx_t *tx)
779{
780 dsl_pool_t *dp = dd->dd_pool;
781 dmu_buf_t *dbuf;
782 dsl_dataset_phys_t *dsphys;
783 uint64_t dsobj;
784 objset_t *mos = dp->dp_meta_objset;
785
b128c09f
BB
786 if (origin == NULL)
787 origin = dp->dp_origin_snap;
788
34dc7c2f
BB
789 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
790 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
791 ASSERT(dmu_tx_is_syncing(tx));
792 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
793
794 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
795 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
796 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
797 dmu_buf_will_dirty(dbuf, tx);
798 dsphys = dbuf->db_data;
b128c09f 799 bzero(dsphys, sizeof (dsl_dataset_phys_t));
34dc7c2f
BB
800 dsphys->ds_dir_obj = dd->dd_object;
801 dsphys->ds_flags = flags;
802 dsphys->ds_fsid_guid = unique_create();
803 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
804 sizeof (dsphys->ds_guid));
805 dsphys->ds_snapnames_zapobj =
806 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
807 DMU_OT_NONE, 0, tx);
808 dsphys->ds_creation_time = gethrestime_sec();
b128c09f 809 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
34dc7c2f 810
428870ff
BB
811 if (origin == NULL) {
812 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
813 } else {
814 dsl_dataset_t *ohds;
815
34dc7c2f
BB
816 dsphys->ds_prev_snap_obj = origin->ds_object;
817 dsphys->ds_prev_snap_txg =
818 origin->ds_phys->ds_creation_txg;
819 dsphys->ds_used_bytes =
820 origin->ds_phys->ds_used_bytes;
821 dsphys->ds_compressed_bytes =
822 origin->ds_phys->ds_compressed_bytes;
823 dsphys->ds_uncompressed_bytes =
824 origin->ds_phys->ds_uncompressed_bytes;
825 dsphys->ds_bp = origin->ds_phys->ds_bp;
826 dsphys->ds_flags |= origin->ds_phys->ds_flags;
827
828 dmu_buf_will_dirty(origin->ds_dbuf, tx);
829 origin->ds_phys->ds_num_children++;
830
428870ff
BB
831 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
832 origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
833 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
834 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
835 dsl_dataset_rele(ohds, FTAG);
836
b128c09f
BB
837 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
838 if (origin->ds_phys->ds_next_clones_obj == 0) {
839 origin->ds_phys->ds_next_clones_obj =
840 zap_create(mos,
841 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
842 }
843 VERIFY(0 == zap_add_int(mos,
844 origin->ds_phys->ds_next_clones_obj,
845 dsobj, tx));
846 }
847
34dc7c2f
BB
848 dmu_buf_will_dirty(dd->dd_dbuf, tx);
849 dd->dd_phys->dd_origin_obj = origin->ds_object;
428870ff
BB
850 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
851 if (origin->ds_dir->dd_phys->dd_clones == 0) {
852 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
853 origin->ds_dir->dd_phys->dd_clones =
854 zap_create(mos,
855 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
856 }
857 VERIFY3U(0, ==, zap_add_int(mos,
858 origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
859 }
34dc7c2f
BB
860 }
861
862 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
863 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
864
865 dmu_buf_rele(dbuf, FTAG);
866
867 dmu_buf_will_dirty(dd->dd_dbuf, tx);
868 dd->dd_phys->dd_head_dataset_obj = dsobj;
869
870 return (dsobj);
871}
872
873uint64_t
874dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
875 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
876{
877 dsl_pool_t *dp = pdd->dd_pool;
878 uint64_t dsobj, ddobj;
879 dsl_dir_t *dd;
880
881 ASSERT(lastname[0] != '@');
882
b128c09f 883 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
34dc7c2f
BB
884 VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
885
b128c09f 886 dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
34dc7c2f
BB
887
888 dsl_deleg_set_create_perms(dd, tx, cr);
889
890 dsl_dir_close(dd, FTAG);
891
572e2857
BB
892 /*
893 * If we are creating a clone, make sure we zero out any stale
894 * data from the origin snapshots zil header.
895 */
896 if (origin != NULL) {
897 dsl_dataset_t *ds;
898 objset_t *os;
899
900 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
901 VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
902 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
903 dsl_dataset_dirty(ds, tx);
904 dsl_dataset_rele(ds, FTAG);
905 }
906
34dc7c2f
BB
907 return (dsobj);
908}
909
910struct destroyarg {
911 dsl_sync_task_group_t *dstg;
912 char *snapname;
913 char *failed;
45d1cae3 914 boolean_t defer;
34dc7c2f
BB
915};
916
917static int
428870ff 918dsl_snapshot_destroy_one(const char *name, void *arg)
34dc7c2f
BB
919{
920 struct destroyarg *da = arg;
921 dsl_dataset_t *ds;
34dc7c2f 922 int err;
45d1cae3 923 char *dsname;
428870ff
BB
924
925 dsname = kmem_asprintf("%s@%s", name, da->snapname);
926 err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds);
927 strfree(dsname);
b128c09f 928 if (err == 0) {
45d1cae3
BB
929 struct dsl_ds_destroyarg *dsda;
930
b128c09f 931 dsl_dataset_make_exclusive(ds, da->dstg);
45d1cae3
BB
932 dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP);
933 dsda->ds = ds;
934 dsda->defer = da->defer;
b128c09f 935 dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
45d1cae3 936 dsl_dataset_destroy_sync, dsda, da->dstg, 0);
b128c09f
BB
937 } else if (err == ENOENT) {
938 err = 0;
939 } else {
34dc7c2f 940 (void) strcpy(da->failed, name);
34dc7c2f 941 }
b128c09f 942 return (err);
34dc7c2f
BB
943}
944
945/*
946 * Destroy 'snapname' in all descendants of 'fsname'.
947 */
948#pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
949int
45d1cae3 950dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer)
34dc7c2f
BB
951{
952 int err;
953 struct destroyarg da;
954 dsl_sync_task_t *dst;
955 spa_t *spa;
956
957 err = spa_open(fsname, &spa, FTAG);
958 if (err)
959 return (err);
960 da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
961 da.snapname = snapname;
962 da.failed = fsname;
45d1cae3 963 da.defer = defer;
34dc7c2f
BB
964
965 err = dmu_objset_find(fsname,
966 dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
967
968 if (err == 0)
969 err = dsl_sync_task_group_wait(da.dstg);
970
971 for (dst = list_head(&da.dstg->dstg_tasks); dst;
972 dst = list_next(&da.dstg->dstg_tasks, dst)) {
45d1cae3
BB
973 struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
974 dsl_dataset_t *ds = dsda->ds;
975
b128c09f
BB
976 /*
977 * Return the file system name that triggered the error
978 */
34dc7c2f
BB
979 if (dst->dst_err) {
980 dsl_dataset_name(ds, fsname);
981 *strchr(fsname, '@') = '\0';
982 }
45d1cae3 983 ASSERT3P(dsda->rm_origin, ==, NULL);
b128c09f 984 dsl_dataset_disown(ds, da.dstg);
45d1cae3 985 kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
34dc7c2f
BB
986 }
987
988 dsl_sync_task_group_destroy(da.dstg);
989 spa_close(spa, FTAG);
990 return (err);
991}
992
45d1cae3
BB
993static boolean_t
994dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
995{
996 boolean_t might_destroy = B_FALSE;
997
998 mutex_enter(&ds->ds_lock);
999 if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
1000 DS_IS_DEFER_DESTROY(ds))
1001 might_destroy = B_TRUE;
1002 mutex_exit(&ds->ds_lock);
1003
1004 return (might_destroy);
1005}
1006
45d1cae3
BB
1007/*
1008 * If we're removing a clone, and these three conditions are true:
1009 * 1) the clone's origin has no other children
1010 * 2) the clone's origin has no user references
1011 * 3) the clone's origin has been marked for deferred destruction
1012 * Then, prepare to remove the origin as part of this sync task group.
1013 */
1014static int
1015dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag)
1016{
1017 dsl_dataset_t *ds = dsda->ds;
1018 dsl_dataset_t *origin = ds->ds_prev;
1019
1020 if (dsl_dataset_might_destroy_origin(origin)) {
1021 char *name;
1022 int namelen;
1023 int error;
1024
1025 namelen = dsl_dataset_namelen(origin) + 1;
1026 name = kmem_alloc(namelen, KM_SLEEP);
1027 dsl_dataset_name(origin, name);
1028#ifdef _KERNEL
1029 error = zfs_unmount_snap(name, NULL);
1030 if (error) {
1031 kmem_free(name, namelen);
1032 return (error);
1033 }
45d1cae3 1034#endif
428870ff 1035 error = dsl_dataset_own(name, B_TRUE, tag, &origin);
45d1cae3
BB
1036 kmem_free(name, namelen);
1037 if (error)
1038 return (error);
1039 dsda->rm_origin = origin;
1040 dsl_dataset_make_exclusive(origin, tag);
1041 }
1042
1043 return (0);
1044}
1045
34dc7c2f 1046/*
b128c09f
BB
1047 * ds must be opened as OWNER. On return (whether successful or not),
1048 * ds will be closed and caller can no longer dereference it.
34dc7c2f
BB
1049 */
1050int
45d1cae3 1051dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
34dc7c2f
BB
1052{
1053 int err;
1054 dsl_sync_task_group_t *dstg;
1055 objset_t *os;
1056 dsl_dir_t *dd;
1057 uint64_t obj;
428870ff 1058 struct dsl_ds_destroyarg dsda = { 0 };
81a49663 1059 dsl_dataset_t *dummy_ds;
45d1cae3
BB
1060
1061 dsda.ds = ds;
34dc7c2f 1062
34dc7c2f
BB
1063 if (dsl_dataset_is_snapshot(ds)) {
1064 /* Destroying a snapshot is simpler */
b128c09f
BB
1065 dsl_dataset_make_exclusive(ds, tag);
1066
45d1cae3 1067 dsda.defer = defer;
34dc7c2f
BB
1068 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1069 dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
45d1cae3
BB
1070 &dsda, tag, 0);
1071 ASSERT3P(dsda.rm_origin, ==, NULL);
34dc7c2f 1072 goto out;
428870ff
BB
1073 } else if (defer) {
1074 err = EINVAL;
1075 goto out;
34dc7c2f
BB
1076 }
1077
1078 dd = ds->ds_dir;
81a49663
BB
1079 dummy_ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
1080 dummy_ds->ds_dir = dd;
1081 dummy_ds->ds_object = ds->ds_object;
34dc7c2f
BB
1082
1083 /*
1084 * Check for errors and mark this ds as inconsistent, in
1085 * case we crash while freeing the objects.
1086 */
1087 err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
1088 dsl_dataset_destroy_begin_sync, ds, NULL, 0);
1089 if (err)
81a49663 1090 goto out_free;
34dc7c2f 1091
428870ff 1092 err = dmu_objset_from_ds(ds, &os);
34dc7c2f 1093 if (err)
81a49663 1094 goto out_free;
34dc7c2f
BB
1095
1096 /*
1097 * remove the objects in open context, so that we won't
1098 * have too much to do in syncing context.
1099 */
1100 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
1101 ds->ds_phys->ds_prev_snap_txg)) {
b128c09f
BB
1102 /*
1103 * Ignore errors, if there is not enough disk space
1104 * we will deal with it in dsl_dataset_destroy_sync().
1105 */
1106 (void) dmu_free_object(os, obj);
34dc7c2f 1107 }
572e2857 1108 if (err != ESRCH)
81a49663 1109 goto out_free;
34dc7c2f 1110
9babb374 1111 /*
572e2857
BB
1112 * Only the ZIL knows how to free log blocks.
1113 */
1114 zil_destroy(dmu_objset_zil(os), B_FALSE);
1115
1116 /*
1117 * Sync out all in-flight IO.
9babb374
BB
1118 */
1119 txg_wait_synced(dd->dd_pool, 0);
1120
1121 /*
1122 * If we managed to free all the objects in open
1123 * context, the user space accounting should be zero.
1124 */
1125 if (ds->ds_phys->ds_bp.blk_fill == 0 &&
428870ff 1126 dmu_objset_userused_enabled(os)) {
1fde1e37 1127 ASSERTV(uint64_t count);
9babb374
BB
1128 ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 ||
1129 count == 0);
1130 ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 ||
1131 count == 0);
1132 }
1133
34dc7c2f
BB
1134 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
1135 err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
1136 rw_exit(&dd->dd_pool->dp_config_rwlock);
1137
1138 if (err)
81a49663 1139 goto out_free;
34dc7c2f
BB
1140
1141 /*
1142 * Blow away the dsl_dir + head dataset.
1143 */
b128c09f 1144 dsl_dataset_make_exclusive(ds, tag);
45d1cae3
BB
1145 /*
1146 * If we're removing a clone, we might also need to remove its
1147 * origin.
1148 */
1149 do {
1150 dsda.need_prep = B_FALSE;
1151 if (dsl_dir_is_clone(dd)) {
1152 err = dsl_dataset_origin_rm_prep(&dsda, tag);
1153 if (err) {
1154 dsl_dir_close(dd, FTAG);
81a49663 1155 goto out_free;
45d1cae3
BB
1156 }
1157 }
1158
1159 dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
1160 dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
1161 dsl_dataset_destroy_sync, &dsda, tag, 0);
1162 dsl_sync_task_create(dstg, dsl_dir_destroy_check,
81a49663 1163 dsl_dir_destroy_sync, dummy_ds, FTAG, 0);
45d1cae3
BB
1164 err = dsl_sync_task_group_wait(dstg);
1165 dsl_sync_task_group_destroy(dstg);
1166
1167 /*
1168 * We could be racing against 'zfs release' or 'zfs destroy -d'
1169 * on the origin snap, in which case we can get EBUSY if we
1170 * needed to destroy the origin snap but were not ready to
1171 * do so.
1172 */
1173 if (dsda.need_prep) {
1174 ASSERT(err == EBUSY);
1175 ASSERT(dsl_dir_is_clone(dd));
1176 ASSERT(dsda.rm_origin == NULL);
1177 }
1178 } while (dsda.need_prep);
1179
1180 if (dsda.rm_origin != NULL)
1181 dsl_dataset_disown(dsda.rm_origin, tag);
1182
b128c09f 1183 /* if it is successful, dsl_dir_destroy_sync will close the dd */
34dc7c2f
BB
1184 if (err)
1185 dsl_dir_close(dd, FTAG);
81a49663
BB
1186
1187out_free:
1188 kmem_free(dummy_ds, sizeof (dsl_dataset_t));
34dc7c2f 1189out:
b128c09f 1190 dsl_dataset_disown(ds, tag);
34dc7c2f
BB
1191 return (err);
1192}
1193
34dc7c2f
BB
1194blkptr_t *
1195dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1196{
1197 return (&ds->ds_phys->ds_bp);
1198}
1199
1200void
1201dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1202{
1203 ASSERT(dmu_tx_is_syncing(tx));
1204 /* If it's the meta-objset, set dp_meta_rootbp */
1205 if (ds == NULL) {
1206 tx->tx_pool->dp_meta_rootbp = *bp;
1207 } else {
1208 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1209 ds->ds_phys->ds_bp = *bp;
1210 }
1211}
1212
1213spa_t *
1214dsl_dataset_get_spa(dsl_dataset_t *ds)
1215{
1216 return (ds->ds_dir->dd_pool->dp_spa);
1217}
1218
1219void
1220dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1221{
1222 dsl_pool_t *dp;
1223
1224 if (ds == NULL) /* this is the meta-objset */
1225 return;
1226
428870ff 1227 ASSERT(ds->ds_objset != NULL);
34dc7c2f
BB
1228
1229 if (ds->ds_phys->ds_next_snap_obj != 0)
1230 panic("dirtying snapshot!");
1231
1232 dp = ds->ds_dir->dd_pool;
1233
1234 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1235 /* up the hold count until we can be written out */
1236 dmu_buf_add_ref(ds->ds_dbuf, ds);
1237 }
1238}
1239
1240/*
1241 * The unique space in the head dataset can be calculated by subtracting
1242 * the space used in the most recent snapshot, that is still being used
1243 * in this file system, from the space currently in use. To figure out
1244 * the space in the most recent snapshot still in use, we need to take
1245 * the total space used in the snapshot and subtract out the space that
1246 * has been freed up since the snapshot was taken.
1247 */
1248static void
1249dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1250{
1251 uint64_t mrs_used;
1252 uint64_t dlused, dlcomp, dluncomp;
1253
428870ff 1254 ASSERT(!dsl_dataset_is_snapshot(ds));
34dc7c2f
BB
1255
1256 if (ds->ds_phys->ds_prev_snap_obj != 0)
1257 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
1258 else
1259 mrs_used = 0;
1260
428870ff 1261 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
34dc7c2f
BB
1262
1263 ASSERT3U(dlused, <=, mrs_used);
1264 ds->ds_phys->ds_unique_bytes =
1265 ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
1266
428870ff 1267 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
34dc7c2f
BB
1268 SPA_VERSION_UNIQUE_ACCURATE)
1269 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1270}
1271
34dc7c2f 1272struct killarg {
b128c09f 1273 dsl_dataset_t *ds;
34dc7c2f
BB
1274 dmu_tx_t *tx;
1275};
1276
b128c09f 1277/* ARGSUSED */
34dc7c2f 1278static int
428870ff
BB
1279kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
1280 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
34dc7c2f
BB
1281{
1282 struct killarg *ka = arg;
428870ff 1283 dmu_tx_t *tx = ka->tx;
34dc7c2f 1284
b128c09f
BB
1285 if (bp == NULL)
1286 return (0);
1287
428870ff
BB
1288 if (zb->zb_level == ZB_ZIL_LEVEL) {
1289 ASSERT(zilog != NULL);
9babb374
BB
1290 /*
1291 * It's a block in the intent log. It has no
1292 * accounting, so just free it.
1293 */
428870ff 1294 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
9babb374 1295 } else {
428870ff 1296 ASSERT(zilog == NULL);
9babb374 1297 ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
428870ff 1298 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
9babb374 1299 }
34dc7c2f 1300
34dc7c2f
BB
1301 return (0);
1302}
1303
34dc7c2f
BB
1304/* ARGSUSED */
1305static int
1306dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
1307{
1308 dsl_dataset_t *ds = arg1;
1309 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1310 uint64_t count;
1311 int err;
1312
1313 /*
1314 * Can't delete a head dataset if there are snapshots of it.
1315 * (Except if the only snapshots are from the branch we cloned
1316 * from.)
1317 */
1318 if (ds->ds_prev != NULL &&
1319 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
428870ff 1320 return (EBUSY);
34dc7c2f
BB
1321
1322 /*
1323 * This is really a dsl_dir thing, but check it here so that
1324 * we'll be less likely to leave this dataset inconsistent &
1325 * nearly destroyed.
1326 */
1327 err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
1328 if (err)
1329 return (err);
1330 if (count != 0)
1331 return (EEXIST);
1332
1333 return (0);
1334}
1335
1336/* ARGSUSED */
1337static void
428870ff 1338dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
1339{
1340 dsl_dataset_t *ds = arg1;
1341 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1342
1343 /* Mark it as inconsistent on-disk, in case we crash */
1344 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1345 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1346
428870ff
BB
1347 spa_history_log_internal(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
1348 "dataset = %llu", ds->ds_object);
34dc7c2f
BB
1349}
1350
45d1cae3
BB
1351static int
1352dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
1353 dmu_tx_t *tx)
1354{
1355 dsl_dataset_t *ds = dsda->ds;
1356 dsl_dataset_t *ds_prev = ds->ds_prev;
1357
1358 if (dsl_dataset_might_destroy_origin(ds_prev)) {
1359 struct dsl_ds_destroyarg ndsda = {0};
1360
1361 /*
1362 * If we're not prepared to remove the origin, don't remove
1363 * the clone either.
1364 */
1365 if (dsda->rm_origin == NULL) {
1366 dsda->need_prep = B_TRUE;
1367 return (EBUSY);
1368 }
1369
1370 ndsda.ds = ds_prev;
1371 ndsda.is_origin_rm = B_TRUE;
1372 return (dsl_dataset_destroy_check(&ndsda, tag, tx));
1373 }
1374
1375 /*
1376 * If we're not going to remove the origin after all,
1377 * undo the open context setup.
1378 */
1379 if (dsda->rm_origin != NULL) {
1380 dsl_dataset_disown(dsda->rm_origin, tag);
1381 dsda->rm_origin = NULL;
1382 }
1383
1384 return (0);
1385}
1386
572e2857
BB
1387/*
1388 * If you add new checks here, you may need to add
1389 * additional checks to the "temporary" case in
1390 * snapshot_check() in dmu_objset.c.
1391 */
34dc7c2f
BB
1392/* ARGSUSED */
1393int
1394dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
1395{
45d1cae3
BB
1396 struct dsl_ds_destroyarg *dsda = arg1;
1397 dsl_dataset_t *ds = dsda->ds;
34dc7c2f 1398
b128c09f
BB
1399 /* we have an owner hold, so noone else can destroy us */
1400 ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
1401
45d1cae3
BB
1402 /*
1403 * Only allow deferred destroy on pools that support it.
1404 * NOTE: deferred destroy is only supported on snapshots.
1405 */
1406 if (dsda->defer) {
1407 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
1408 SPA_VERSION_USERREFS)
1409 return (ENOTSUP);
1410 ASSERT(dsl_dataset_is_snapshot(ds));
1411 return (0);
1412 }
34dc7c2f
BB
1413
1414 /*
1415 * Can't delete a head dataset if there are snapshots of it.
1416 * (Except if the only snapshots are from the branch we cloned
1417 * from.)
1418 */
1419 if (ds->ds_prev != NULL &&
1420 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
428870ff 1421 return (EBUSY);
34dc7c2f
BB
1422
1423 /*
1424 * If we made changes this txg, traverse_dsl_dataset won't find
1425 * them. Try again.
1426 */
1427 if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1428 return (EAGAIN);
1429
45d1cae3
BB
1430 if (dsl_dataset_is_snapshot(ds)) {
1431 /*
1432 * If this snapshot has an elevated user reference count,
1433 * we can't destroy it yet.
1434 */
1435 if (ds->ds_userrefs > 0 && !dsda->releasing)
1436 return (EBUSY);
1437
1438 mutex_enter(&ds->ds_lock);
1439 /*
1440 * Can't delete a branch point. However, if we're destroying
1441 * a clone and removing its origin due to it having a user
1442 * hold count of 0 and having been marked for deferred destroy,
1443 * it's OK for the origin to have a single clone.
1444 */
1445 if (ds->ds_phys->ds_num_children >
1446 (dsda->is_origin_rm ? 2 : 1)) {
1447 mutex_exit(&ds->ds_lock);
1448 return (EEXIST);
1449 }
1450 mutex_exit(&ds->ds_lock);
1451 } else if (dsl_dir_is_clone(ds->ds_dir)) {
1452 return (dsl_dataset_origin_check(dsda, arg2, tx));
1453 }
1454
34dc7c2f
BB
1455 /* XXX we should do some i/o error checking... */
1456 return (0);
1457}
1458
b128c09f
BB
1459struct refsarg {
1460 kmutex_t lock;
1461 boolean_t gone;
1462 kcondvar_t cv;
1463};
1464
1465/* ARGSUSED */
1466static void
1467dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
1468{
1469 struct refsarg *arg = argv;
1470
1471 mutex_enter(&arg->lock);
1472 arg->gone = TRUE;
1473 cv_signal(&arg->cv);
1474 mutex_exit(&arg->lock);
1475}
1476
1477static void
1478dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
1479{
1480 struct refsarg arg;
1481
1482 mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
1483 cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
1484 arg.gone = FALSE;
1485 (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
1486 dsl_dataset_refs_gone);
1487 dmu_buf_rele(ds->ds_dbuf, tag);
1488 mutex_enter(&arg.lock);
1489 while (!arg.gone)
1490 cv_wait(&arg.cv, &arg.lock);
1491 ASSERT(arg.gone);
1492 mutex_exit(&arg.lock);
1493 ds->ds_dbuf = NULL;
1494 ds->ds_phys = NULL;
1495 mutex_destroy(&arg.lock);
1496 cv_destroy(&arg.cv);
1497}
1498
428870ff
BB
1499static void
1500remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx)
1501{
1502 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
428870ff 1503 int err;
1fde1e37 1504 ASSERTV(uint64_t count);
428870ff
BB
1505
1506 ASSERT(ds->ds_phys->ds_num_children >= 2);
1507 err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
1508 /*
1509 * The err should not be ENOENT, but a bug in a previous version
1510 * of the code could cause upgrade_clones_cb() to not set
1511 * ds_next_snap_obj when it should, leading to a missing entry.
1512 * If we knew that the pool was created after
1513 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1514 * ENOENT. However, at least we can check that we don't have
1515 * too many entries in the next_clones_obj even after failing to
1516 * remove this one.
1517 */
1518 if (err != ENOENT) {
1519 VERIFY3U(err, ==, 0);
1520 }
1521 ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1522 &count));
1523 ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
1524}
1525
1526static void
1527dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
1528{
1529 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1530 zap_cursor_t zc;
1531 zap_attribute_t za;
1532
1533 /*
1534 * If it is the old version, dd_clones doesn't exist so we can't
1535 * find the clones, but deadlist_remove_key() is a no-op so it
1536 * doesn't matter.
1537 */
1538 if (ds->ds_dir->dd_phys->dd_clones == 0)
1539 return;
1540
1541 for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
1542 zap_cursor_retrieve(&zc, &za) == 0;
1543 zap_cursor_advance(&zc)) {
1544 dsl_dataset_t *clone;
1545
1546 VERIFY3U(0, ==, dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1547 za.za_first_integer, FTAG, &clone));
1548 if (clone->ds_dir->dd_origin_txg > mintxg) {
1549 dsl_deadlist_remove_key(&clone->ds_deadlist,
1550 mintxg, tx);
1551 dsl_dataset_remove_clones_key(clone, mintxg, tx);
1552 }
1553 dsl_dataset_rele(clone, FTAG);
1554 }
1555 zap_cursor_fini(&zc);
1556}
1557
1558struct process_old_arg {
1559 dsl_dataset_t *ds;
1560 dsl_dataset_t *ds_prev;
1561 boolean_t after_branch_point;
1562 zio_t *pio;
1563 uint64_t used, comp, uncomp;
1564};
1565
1566static int
1567process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1568{
1569 struct process_old_arg *poa = arg;
1570 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
1571
1572 if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
1573 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
1574 if (poa->ds_prev && !poa->after_branch_point &&
1575 bp->blk_birth >
1576 poa->ds_prev->ds_phys->ds_prev_snap_txg) {
1577 poa->ds_prev->ds_phys->ds_unique_bytes +=
1578 bp_get_dsize_sync(dp->dp_spa, bp);
1579 }
1580 } else {
1581 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
1582 poa->comp += BP_GET_PSIZE(bp);
1583 poa->uncomp += BP_GET_UCSIZE(bp);
1584 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
1585 }
1586 return (0);
1587}
1588
1589static void
1590process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
1591 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
1592{
1593 struct process_old_arg poa = { 0 };
1594 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1595 objset_t *mos = dp->dp_meta_objset;
1596
1597 ASSERT(ds->ds_deadlist.dl_oldfmt);
1598 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
1599
1600 poa.ds = ds;
1601 poa.ds_prev = ds_prev;
1602 poa.after_branch_point = after_branch_point;
1603 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1604 VERIFY3U(0, ==, bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
1605 process_old_cb, &poa, tx));
1606 VERIFY3U(zio_wait(poa.pio), ==, 0);
1607 ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
1608
1609 /* change snapused */
1610 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1611 -poa.used, -poa.comp, -poa.uncomp, tx);
1612
1613 /* swap next's deadlist to our deadlist */
1614 dsl_deadlist_close(&ds->ds_deadlist);
1615 dsl_deadlist_close(&ds_next->ds_deadlist);
1616 SWITCH64(ds_next->ds_phys->ds_deadlist_obj,
1617 ds->ds_phys->ds_deadlist_obj);
1618 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1619 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
1620 ds_next->ds_phys->ds_deadlist_obj);
1621}
1622
34dc7c2f 1623void
428870ff 1624dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
34dc7c2f 1625{
45d1cae3
BB
1626 struct dsl_ds_destroyarg *dsda = arg1;
1627 dsl_dataset_t *ds = dsda->ds;
34dc7c2f
BB
1628 int err;
1629 int after_branch_point = FALSE;
1630 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1631 objset_t *mos = dp->dp_meta_objset;
1632 dsl_dataset_t *ds_prev = NULL;
572e2857 1633 boolean_t wont_destroy;
34dc7c2f
BB
1634 uint64_t obj;
1635
572e2857
BB
1636 wont_destroy = (dsda->defer &&
1637 (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1));
1638
1639 ASSERT(ds->ds_owner || wont_destroy);
45d1cae3 1640 ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
34dc7c2f
BB
1641 ASSERT(ds->ds_prev == NULL ||
1642 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
1643 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
1644
572e2857 1645 if (wont_destroy) {
45d1cae3 1646 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
572e2857
BB
1647 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1648 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
1649 return;
45d1cae3
BB
1650 }
1651
b128c09f
BB
1652 /* signal any waiters that this dataset is going away */
1653 mutex_enter(&ds->ds_lock);
1654 ds->ds_owner = dsl_reaper;
1655 cv_broadcast(&ds->ds_exclusive_cv);
1656 mutex_exit(&ds->ds_lock);
1657
34dc7c2f
BB
1658 /* Remove our reservation */
1659 if (ds->ds_reserved != 0) {
428870ff
BB
1660 dsl_prop_setarg_t psa;
1661 uint64_t value = 0;
1662
1663 dsl_prop_setarg_init_uint64(&psa, "refreservation",
1664 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
1665 &value);
1666 psa.psa_effective_value = 0; /* predict default value */
1667
1668 dsl_dataset_set_reservation_sync(ds, &psa, tx);
34dc7c2f
BB
1669 ASSERT3U(ds->ds_reserved, ==, 0);
1670 }
1671
1672 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1673
428870ff 1674 dsl_scan_ds_destroyed(ds, tx);
b128c09f 1675
34dc7c2f
BB
1676 obj = ds->ds_object;
1677
1678 if (ds->ds_phys->ds_prev_snap_obj != 0) {
1679 if (ds->ds_prev) {
1680 ds_prev = ds->ds_prev;
1681 } else {
b128c09f
BB
1682 VERIFY(0 == dsl_dataset_hold_obj(dp,
1683 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
34dc7c2f
BB
1684 }
1685 after_branch_point =
1686 (ds_prev->ds_phys->ds_next_snap_obj != obj);
1687
1688 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
b128c09f
BB
1689 if (after_branch_point &&
1690 ds_prev->ds_phys->ds_next_clones_obj != 0) {
428870ff 1691 remove_from_next_clones(ds_prev, obj, tx);
b128c09f
BB
1692 if (ds->ds_phys->ds_next_snap_obj != 0) {
1693 VERIFY(0 == zap_add_int(mos,
1694 ds_prev->ds_phys->ds_next_clones_obj,
1695 ds->ds_phys->ds_next_snap_obj, tx));
1696 }
1697 }
34dc7c2f
BB
1698 if (after_branch_point &&
1699 ds->ds_phys->ds_next_snap_obj == 0) {
1700 /* This clone is toast. */
1701 ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1702 ds_prev->ds_phys->ds_num_children--;
45d1cae3
BB
1703
1704 /*
1705 * If the clone's origin has no other clones, no
1706 * user holds, and has been marked for deferred
1707 * deletion, then we should have done the necessary
1708 * destroy setup for it.
1709 */
1710 if (ds_prev->ds_phys->ds_num_children == 1 &&
1711 ds_prev->ds_userrefs == 0 &&
1712 DS_IS_DEFER_DESTROY(ds_prev)) {
1713 ASSERT3P(dsda->rm_origin, !=, NULL);
1714 } else {
1715 ASSERT3P(dsda->rm_origin, ==, NULL);
1716 }
34dc7c2f
BB
1717 } else if (!after_branch_point) {
1718 ds_prev->ds_phys->ds_next_snap_obj =
1719 ds->ds_phys->ds_next_snap_obj;
1720 }
1721 }
1722
428870ff 1723 if (dsl_dataset_is_snapshot(ds)) {
34dc7c2f 1724 dsl_dataset_t *ds_next;
34dc7c2f 1725 uint64_t old_unique;
428870ff 1726 uint64_t used = 0, comp = 0, uncomp = 0;
34dc7c2f 1727
b128c09f
BB
1728 VERIFY(0 == dsl_dataset_hold_obj(dp,
1729 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
34dc7c2f
BB
1730 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1731
428870ff 1732 old_unique = ds_next->ds_phys->ds_unique_bytes;
34dc7c2f
BB
1733
1734 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1735 ds_next->ds_phys->ds_prev_snap_obj =
1736 ds->ds_phys->ds_prev_snap_obj;
1737 ds_next->ds_phys->ds_prev_snap_txg =
1738 ds->ds_phys->ds_prev_snap_txg;
1739 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1740 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1741
34dc7c2f 1742
428870ff
BB
1743 if (ds_next->ds_deadlist.dl_oldfmt) {
1744 process_old_deadlist(ds, ds_prev, ds_next,
1745 after_branch_point, tx);
1746 } else {
1747 /* Adjust prev's unique space. */
1748 if (ds_prev && !after_branch_point) {
1749 dsl_deadlist_space_range(&ds_next->ds_deadlist,
1750 ds_prev->ds_phys->ds_prev_snap_txg,
1751 ds->ds_phys->ds_prev_snap_txg,
1752 &used, &comp, &uncomp);
1753 ds_prev->ds_phys->ds_unique_bytes += used;
1754 }
b128c09f 1755
428870ff
BB
1756 /* Adjust snapused. */
1757 dsl_deadlist_space_range(&ds_next->ds_deadlist,
1758 ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1759 &used, &comp, &uncomp);
1760 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1761 -used, -comp, -uncomp, tx);
1762
1763 /* Move blocks to be freed to pool's free list. */
1764 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
1765 &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
1766 tx);
1767 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
1768 DD_USED_HEAD, used, comp, uncomp, tx);
1769 dsl_dir_dirty(tx->tx_pool->dp_free_dir, tx);
1770
1771 /* Merge our deadlist into next's and free it. */
1772 dsl_deadlist_merge(&ds_next->ds_deadlist,
1773 ds->ds_phys->ds_deadlist_obj, tx);
1774 }
1775 dsl_deadlist_close(&ds->ds_deadlist);
1776 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
b128c09f 1777
428870ff
BB
1778 /* Collapse range in clone heads */
1779 dsl_dataset_remove_clones_key(ds,
1780 ds->ds_phys->ds_creation_txg, tx);
34dc7c2f 1781
428870ff
BB
1782 if (dsl_dataset_is_snapshot(ds_next)) {
1783 dsl_dataset_t *ds_nextnext;
d6320ddb 1784 dsl_dataset_t *hds;
34dc7c2f 1785
34dc7c2f
BB
1786 /*
1787 * Update next's unique to include blocks which
1788 * were previously shared by only this snapshot
1789 * and it. Those blocks will be born after the
1790 * prev snap and before this snap, and will have
1791 * died after the next snap and before the one
1792 * after that (ie. be on the snap after next's
1793 * deadlist).
34dc7c2f 1794 */
b128c09f
BB
1795 VERIFY(0 == dsl_dataset_hold_obj(dp,
1796 ds_next->ds_phys->ds_next_snap_obj,
428870ff
BB
1797 FTAG, &ds_nextnext));
1798 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
b128c09f 1799 ds->ds_phys->ds_prev_snap_txg,
428870ff
BB
1800 ds->ds_phys->ds_creation_txg,
1801 &used, &comp, &uncomp);
1802 ds_next->ds_phys->ds_unique_bytes += used;
1803 dsl_dataset_rele(ds_nextnext, FTAG);
34dc7c2f 1804 ASSERT3P(ds_next->ds_prev, ==, NULL);
428870ff
BB
1805
1806 /* Collapse range in this head. */
428870ff
BB
1807 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
1808 ds->ds_dir->dd_phys->dd_head_dataset_obj,
1809 FTAG, &hds));
1810 dsl_deadlist_remove_key(&hds->ds_deadlist,
1811 ds->ds_phys->ds_creation_txg, tx);
1812 dsl_dataset_rele(hds, FTAG);
1813
34dc7c2f
BB
1814 } else {
1815 ASSERT3P(ds_next->ds_prev, ==, ds);
b128c09f
BB
1816 dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
1817 ds_next->ds_prev = NULL;
34dc7c2f 1818 if (ds_prev) {
b128c09f
BB
1819 VERIFY(0 == dsl_dataset_get_ref(dp,
1820 ds->ds_phys->ds_prev_snap_obj,
1821 ds_next, &ds_next->ds_prev));
34dc7c2f
BB
1822 }
1823
1824 dsl_dataset_recalc_head_uniq(ds_next);
1825
1826 /*
1827 * Reduce the amount of our unconsmed refreservation
1828 * being charged to our parent by the amount of
1829 * new unique data we have gained.
1830 */
1831 if (old_unique < ds_next->ds_reserved) {
1832 int64_t mrsdelta;
1833 uint64_t new_unique =
1834 ds_next->ds_phys->ds_unique_bytes;
1835
1836 ASSERT(old_unique <= new_unique);
1837 mrsdelta = MIN(new_unique - old_unique,
1838 ds_next->ds_reserved - old_unique);
b128c09f
BB
1839 dsl_dir_diduse_space(ds->ds_dir,
1840 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
34dc7c2f
BB
1841 }
1842 }
b128c09f 1843 dsl_dataset_rele(ds_next, FTAG);
34dc7c2f
BB
1844 } else {
1845 /*
1846 * There's no next snapshot, so this is a head dataset.
1847 * Destroy the deadlist. Unless it's a clone, the
1848 * deadlist should be empty. (If it's a clone, it's
1849 * safe to ignore the deadlist contents.)
1850 */
1851 struct killarg ka;
1852
428870ff
BB
1853 dsl_deadlist_close(&ds->ds_deadlist);
1854 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
34dc7c2f
BB
1855 ds->ds_phys->ds_deadlist_obj = 0;
1856
1857 /*
1858 * Free everything that we point to (that's born after
1859 * the previous snapshot, if we are a clone)
1860 *
b128c09f
BB
1861 * NB: this should be very quick, because we already
1862 * freed all the objects in open context.
34dc7c2f 1863 */
b128c09f 1864 ka.ds = ds;
34dc7c2f 1865 ka.tx = tx;
b128c09f
BB
1866 err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1867 TRAVERSE_POST, kill_blkptr, &ka);
34dc7c2f 1868 ASSERT3U(err, ==, 0);
9babb374 1869 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
b128c09f 1870 ds->ds_phys->ds_unique_bytes == 0);
34dc7c2f 1871
428870ff
BB
1872 if (ds->ds_prev != NULL) {
1873 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1874 VERIFY3U(0, ==, zap_remove_int(mos,
1875 ds->ds_prev->ds_dir->dd_phys->dd_clones,
1876 ds->ds_object, tx));
1877 }
1878 dsl_dataset_rele(ds->ds_prev, ds);
1879 ds->ds_prev = ds_prev = NULL;
1880 }
1881 }
34dc7c2f 1882
572e2857
BB
1883 /*
1884 * This must be done after the dsl_traverse(), because it will
1885 * re-open the objset.
1886 */
1887 if (ds->ds_objset) {
1888 dmu_objset_evict(ds->ds_objset);
1889 ds->ds_objset = NULL;
1890 }
1891
34dc7c2f 1892 if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
b128c09f 1893 /* Erase the link in the dir */
34dc7c2f
BB
1894 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1895 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
b128c09f
BB
1896 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1897 err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1898 ASSERT(err == 0);
34dc7c2f
BB
1899 } else {
1900 /* remove from snapshot namespace */
1901 dsl_dataset_t *ds_head;
b128c09f
BB
1902 ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
1903 VERIFY(0 == dsl_dataset_hold_obj(dp,
1904 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
34dc7c2f
BB
1905 VERIFY(0 == dsl_dataset_get_snapname(ds));
1906#ifdef ZFS_DEBUG
1907 {
1908 uint64_t val;
1909
b128c09f 1910 err = dsl_dataset_snap_lookup(ds_head,
34dc7c2f
BB
1911 ds->ds_snapname, &val);
1912 ASSERT3U(err, ==, 0);
1913 ASSERT3U(val, ==, obj);
1914 }
1915#endif
b128c09f 1916 err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
34dc7c2f 1917 ASSERT(err == 0);
b128c09f 1918 dsl_dataset_rele(ds_head, FTAG);
34dc7c2f
BB
1919 }
1920
1921 if (ds_prev && ds->ds_prev != ds_prev)
b128c09f 1922 dsl_dataset_rele(ds_prev, FTAG);
34dc7c2f
BB
1923
1924 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
428870ff
BB
1925 spa_history_log_internal(LOG_DS_DESTROY, dp->dp_spa, tx,
1926 "dataset = %llu", ds->ds_object);
34dc7c2f 1927
b128c09f 1928 if (ds->ds_phys->ds_next_clones_obj != 0) {
1fde1e37 1929 ASSERTV(uint64_t count);
b128c09f
BB
1930 ASSERT(0 == zap_count(mos,
1931 ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
1932 VERIFY(0 == dmu_object_free(mos,
1933 ds->ds_phys->ds_next_clones_obj, tx));
1934 }
1935 if (ds->ds_phys->ds_props_obj != 0)
1936 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
45d1cae3
BB
1937 if (ds->ds_phys->ds_userrefs_obj != 0)
1938 VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
b128c09f
BB
1939 dsl_dir_close(ds->ds_dir, ds);
1940 ds->ds_dir = NULL;
1941 dsl_dataset_drain_refs(ds, tag);
34dc7c2f 1942 VERIFY(0 == dmu_object_free(mos, obj, tx));
45d1cae3
BB
1943
1944 if (dsda->rm_origin) {
1945 /*
1946 * Remove the origin of the clone we just destroyed.
1947 */
45d1cae3
BB
1948 struct dsl_ds_destroyarg ndsda = {0};
1949
428870ff
BB
1950 ndsda.ds = dsda->rm_origin;
1951 dsl_dataset_destroy_sync(&ndsda, tag, tx);
45d1cae3 1952 }
34dc7c2f
BB
1953}
1954
1955static int
1956dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1957{
1958 uint64_t asize;
1959
1960 if (!dmu_tx_is_syncing(tx))
1961 return (0);
1962
1963 /*
1964 * If there's an fs-only reservation, any blocks that might become
1965 * owned by the snapshot dataset must be accommodated by space
1966 * outside of the reservation.
1967 */
428870ff
BB
1968 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1969 asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
572e2857 1970 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
34dc7c2f
BB
1971 return (ENOSPC);
1972
1973 /*
1974 * Propogate any reserved space for this snapshot to other
1975 * snapshot checks in this sync group.
1976 */
1977 if (asize > 0)
1978 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1979
1980 return (0);
1981}
1982
34dc7c2f
BB
1983int
1984dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
1985{
1986 dsl_dataset_t *ds = arg1;
1987 const char *snapname = arg2;
34dc7c2f
BB
1988 int err;
1989 uint64_t value;
1990
1991 /*
1992 * We don't allow multiple snapshots of the same txg. If there
1993 * is already one, try again.
1994 */
1995 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
1996 return (EAGAIN);
1997
1998 /*
1999 * Check for conflicting name snapshot name.
2000 */
b128c09f 2001 err = dsl_dataset_snap_lookup(ds, snapname, &value);
34dc7c2f
BB
2002 if (err == 0)
2003 return (EEXIST);
2004 if (err != ENOENT)
2005 return (err);
2006
2007 /*
2008 * Check that the dataset's name is not too long. Name consists
2009 * of the dataset's length + 1 for the @-sign + snapshot name's length
2010 */
2011 if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
2012 return (ENAMETOOLONG);
2013
2014 err = dsl_dataset_snapshot_reserve_space(ds, tx);
2015 if (err)
2016 return (err);
2017
2018 ds->ds_trysnap_txg = tx->tx_txg;
2019 return (0);
2020}
2021
2022void
428870ff 2023dsl_dataset_snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
2024{
2025 dsl_dataset_t *ds = arg1;
2026 const char *snapname = arg2;
2027 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2028 dmu_buf_t *dbuf;
2029 dsl_dataset_phys_t *dsphys;
b128c09f 2030 uint64_t dsobj, crtxg;
34dc7c2f
BB
2031 objset_t *mos = dp->dp_meta_objset;
2032 int err;
2033
34dc7c2f
BB
2034 ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
2035
b128c09f
BB
2036 /*
2037 * The origin's ds_creation_txg has to be < TXG_INITIAL
2038 */
2039 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
2040 crtxg = 1;
2041 else
2042 crtxg = tx->tx_txg;
2043
34dc7c2f
BB
2044 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
2045 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
2046 VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
2047 dmu_buf_will_dirty(dbuf, tx);
2048 dsphys = dbuf->db_data;
b128c09f 2049 bzero(dsphys, sizeof (dsl_dataset_phys_t));
34dc7c2f
BB
2050 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
2051 dsphys->ds_fsid_guid = unique_create();
2052 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
2053 sizeof (dsphys->ds_guid));
2054 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
2055 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
2056 dsphys->ds_next_snap_obj = ds->ds_object;
2057 dsphys->ds_num_children = 1;
2058 dsphys->ds_creation_time = gethrestime_sec();
b128c09f 2059 dsphys->ds_creation_txg = crtxg;
34dc7c2f
BB
2060 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
2061 dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
2062 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
2063 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
2064 dsphys->ds_flags = ds->ds_phys->ds_flags;
2065 dsphys->ds_bp = ds->ds_phys->ds_bp;
2066 dmu_buf_rele(dbuf, FTAG);
2067
2068 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
2069 if (ds->ds_prev) {
b128c09f
BB
2070 uint64_t next_clones_obj =
2071 ds->ds_prev->ds_phys->ds_next_clones_obj;
34dc7c2f
BB
2072 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
2073 ds->ds_object ||
2074 ds->ds_prev->ds_phys->ds_num_children > 1);
2075 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
2076 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2077 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
2078 ds->ds_prev->ds_phys->ds_creation_txg);
2079 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
b128c09f 2080 } else if (next_clones_obj != 0) {
428870ff
BB
2081 remove_from_next_clones(ds->ds_prev,
2082 dsphys->ds_next_snap_obj, tx);
b128c09f
BB
2083 VERIFY3U(0, ==, zap_add_int(mos,
2084 next_clones_obj, dsobj, tx));
34dc7c2f
BB
2085 }
2086 }
2087
2088 /*
2089 * If we have a reference-reservation on this dataset, we will
2090 * need to increase the amount of refreservation being charged
2091 * since our unique space is going to zero.
2092 */
2093 if (ds->ds_reserved) {
428870ff
BB
2094 int64_t delta;
2095 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2096 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
b128c09f 2097 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
428870ff 2098 delta, 0, 0, tx);
34dc7c2f
BB
2099 }
2100
34dc7c2f 2101 dmu_buf_will_dirty(ds->ds_dbuf, tx);
428870ff
BB
2102 zfs_dbgmsg("taking snapshot %s@%s/%llu; newkey=%llu",
2103 ds->ds_dir->dd_myname, snapname, dsobj,
2104 ds->ds_phys->ds_prev_snap_txg);
2105 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
2106 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
2107 dsl_deadlist_close(&ds->ds_deadlist);
2108 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
2109 dsl_deadlist_add_key(&ds->ds_deadlist,
2110 ds->ds_phys->ds_prev_snap_txg, tx);
2111
34dc7c2f
BB
2112 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
2113 ds->ds_phys->ds_prev_snap_obj = dsobj;
b128c09f 2114 ds->ds_phys->ds_prev_snap_txg = crtxg;
34dc7c2f
BB
2115 ds->ds_phys->ds_unique_bytes = 0;
2116 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
2117 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
34dc7c2f 2118
34dc7c2f
BB
2119 err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
2120 snapname, 8, 1, &dsobj, tx);
2121 ASSERT(err == 0);
2122
2123 if (ds->ds_prev)
b128c09f
BB
2124 dsl_dataset_drop_ref(ds->ds_prev, ds);
2125 VERIFY(0 == dsl_dataset_get_ref(dp,
2126 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
2127
428870ff
BB
2128 dsl_scan_ds_snapshotted(ds, tx);
2129
2130 dsl_dir_snap_cmtime_update(ds->ds_dir);
34dc7c2f 2131
428870ff 2132 spa_history_log_internal(LOG_DS_SNAPSHOT, dp->dp_spa, tx,
34dc7c2f
BB
2133 "dataset = %llu", dsobj);
2134}
2135
2136void
2137dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
2138{
2139 ASSERT(dmu_tx_is_syncing(tx));
428870ff 2140 ASSERT(ds->ds_objset != NULL);
34dc7c2f
BB
2141 ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
2142
2143 /*
2144 * in case we had to change ds_fsid_guid when we opened it,
2145 * sync it out now.
2146 */
2147 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2148 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
2149
2150 dsl_dir_dirty(ds->ds_dir, tx);
428870ff 2151 dmu_objset_sync(ds->ds_objset, zio, tx);
34dc7c2f
BB
2152}
2153
2154void
2155dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2156{
f5fc4aca 2157 uint64_t refd, avail, uobjs, aobjs, ratio;
34dc7c2f
BB
2158
2159 dsl_dir_stats(ds->ds_dir, nv);
2160
2161 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
2162 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
2163 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
2164
2165 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2166 ds->ds_phys->ds_creation_time);
2167 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2168 ds->ds_phys->ds_creation_txg);
2169 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2170 ds->ds_quota);
2171 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2172 ds->ds_reserved);
b128c09f
BB
2173 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2174 ds->ds_phys->ds_guid);
428870ff
BB
2175 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2176 ds->ds_phys->ds_unique_bytes);
2177 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2178 ds->ds_object);
2179 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
2180 ds->ds_userrefs);
45d1cae3
BB
2181 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2182 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
34dc7c2f 2183
f5fc4aca
MA
2184 ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
2185 (ds->ds_phys->ds_uncompressed_bytes * 100 /
2186 ds->ds_phys->ds_compressed_bytes);
2187 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
2188
34dc7c2f
BB
2189 if (ds->ds_phys->ds_next_snap_obj) {
2190 /*
2191 * This is a snapshot; override the dd's space used with
2192 * our unique space and compression ratio.
2193 */
2194 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2195 ds->ds_phys->ds_unique_bytes);
f5fc4aca 2196 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
34dc7c2f
BB
2197 }
2198}
2199
2200void
2201dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2202{
2203 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
2204 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
2205 stat->dds_guid = ds->ds_phys->ds_guid;
2206 if (ds->ds_phys->ds_next_snap_obj) {
2207 stat->dds_is_snapshot = B_TRUE;
2208 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
fb5f0bc8
BB
2209 } else {
2210 stat->dds_is_snapshot = B_FALSE;
2211 stat->dds_num_clones = 0;
34dc7c2f
BB
2212 }
2213
2214 /* clone origin is really a dsl_dir thing... */
2215 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
b128c09f 2216 if (dsl_dir_is_clone(ds->ds_dir)) {
34dc7c2f
BB
2217 dsl_dataset_t *ods;
2218
b128c09f
BB
2219 VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
2220 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
34dc7c2f 2221 dsl_dataset_name(ods, stat->dds_origin);
b128c09f 2222 dsl_dataset_drop_ref(ods, FTAG);
fb5f0bc8
BB
2223 } else {
2224 stat->dds_origin[0] = '\0';
34dc7c2f
BB
2225 }
2226 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
2227}
2228
2229uint64_t
2230dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2231{
2232 return (ds->ds_fsid_guid);
2233}
2234
2235void
2236dsl_dataset_space(dsl_dataset_t *ds,
2237 uint64_t *refdbytesp, uint64_t *availbytesp,
2238 uint64_t *usedobjsp, uint64_t *availobjsp)
2239{
2240 *refdbytesp = ds->ds_phys->ds_used_bytes;
2241 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2242 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
2243 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
2244 if (ds->ds_quota != 0) {
2245 /*
2246 * Adjust available bytes according to refquota
2247 */
2248 if (*refdbytesp < ds->ds_quota)
2249 *availbytesp = MIN(*availbytesp,
2250 ds->ds_quota - *refdbytesp);
2251 else
2252 *availbytesp = 0;
2253 }
2254 *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
2255 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
2256}
2257
2258boolean_t
2259dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
2260{
1fde1e37 2261 ASSERTV(dsl_pool_t *dp = ds->ds_dir->dd_pool);
34dc7c2f
BB
2262
2263 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
2264 dsl_pool_sync_context(dp));
2265 if (ds->ds_prev == NULL)
2266 return (B_FALSE);
2267 if (ds->ds_phys->ds_bp.blk_birth >
572e2857
BB
2268 ds->ds_prev->ds_phys->ds_creation_txg) {
2269 objset_t *os, *os_prev;
2270 /*
2271 * It may be that only the ZIL differs, because it was
2272 * reset in the head. Don't count that as being
2273 * modified.
2274 */
2275 if (dmu_objset_from_ds(ds, &os) != 0)
2276 return (B_TRUE);
2277 if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
2278 return (B_TRUE);
2279 return (bcmp(&os->os_phys->os_meta_dnode,
2280 &os_prev->os_phys->os_meta_dnode,
2281 sizeof (os->os_phys->os_meta_dnode)) != 0);
2282 }
34dc7c2f
BB
2283 return (B_FALSE);
2284}
2285
2286/* ARGSUSED */
2287static int
2288dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2289{
2290 dsl_dataset_t *ds = arg1;
2291 char *newsnapname = arg2;
2292 dsl_dir_t *dd = ds->ds_dir;
34dc7c2f
BB
2293 dsl_dataset_t *hds;
2294 uint64_t val;
2295 int err;
2296
b128c09f
BB
2297 err = dsl_dataset_hold_obj(dd->dd_pool,
2298 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
34dc7c2f
BB
2299 if (err)
2300 return (err);
2301
2302 /* new name better not be in use */
b128c09f
BB
2303 err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
2304 dsl_dataset_rele(hds, FTAG);
34dc7c2f
BB
2305
2306 if (err == 0)
2307 err = EEXIST;
2308 else if (err == ENOENT)
2309 err = 0;
2310
2311 /* dataset name + 1 for the "@" + the new snapshot name must fit */
2312 if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
2313 err = ENAMETOOLONG;
2314
2315 return (err);
2316}
2317
2318static void
428870ff 2319dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
2320{
2321 dsl_dataset_t *ds = arg1;
2322 const char *newsnapname = arg2;
2323 dsl_dir_t *dd = ds->ds_dir;
2324 objset_t *mos = dd->dd_pool->dp_meta_objset;
2325 dsl_dataset_t *hds;
2326 int err;
2327
2328 ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2329
b128c09f
BB
2330 VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
2331 dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
34dc7c2f
BB
2332
2333 VERIFY(0 == dsl_dataset_get_snapname(ds));
b128c09f 2334 err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
34dc7c2f
BB
2335 ASSERT3U(err, ==, 0);
2336 mutex_enter(&ds->ds_lock);
2337 (void) strcpy(ds->ds_snapname, newsnapname);
2338 mutex_exit(&ds->ds_lock);
2339 err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
2340 ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2341 ASSERT3U(err, ==, 0);
2342
428870ff
BB
2343 spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
2344 "dataset = %llu", ds->ds_object);
b128c09f 2345 dsl_dataset_rele(hds, FTAG);
34dc7c2f
BB
2346}
2347
2348struct renamesnaparg {
2349 dsl_sync_task_group_t *dstg;
2350 char failed[MAXPATHLEN];
2351 char *oldsnap;
2352 char *newsnap;
2353};
2354
2355static int
428870ff 2356dsl_snapshot_rename_one(const char *name, void *arg)
34dc7c2f
BB
2357{
2358 struct renamesnaparg *ra = arg;
2359 dsl_dataset_t *ds = NULL;
428870ff 2360 char *snapname;
34dc7c2f
BB
2361 int err;
2362
428870ff
BB
2363 snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
2364 (void) strlcpy(ra->failed, snapname, sizeof (ra->failed));
34dc7c2f
BB
2365
2366 /*
2367 * For recursive snapshot renames the parent won't be changing
2368 * so we just pass name for both the to/from argument.
2369 */
428870ff
BB
2370 err = zfs_secpolicy_rename_perms(snapname, snapname, CRED());
2371 if (err != 0) {
2372 strfree(snapname);
2373 return (err == ENOENT ? 0 : err);
34dc7c2f
BB
2374 }
2375
c28b2279
BB
2376/* XXX: Ignore for SPL version until mounting the FS is supported */
2377#if defined(_KERNEL) && !defined(HAVE_SPL)
b128c09f
BB
2378 /*
2379 * For all filesystems undergoing rename, we'll need to unmount it.
2380 */
428870ff 2381 (void) zfs_unmount_snap(snapname, NULL);
34dc7c2f 2382#endif
428870ff
BB
2383 err = dsl_dataset_hold(snapname, ra->dstg, &ds);
2384 strfree(snapname);
2385 if (err != 0)
2386 return (err == ENOENT ? 0 : err);
34dc7c2f
BB
2387
2388 dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
2389 dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
2390
2391 return (0);
2392}
2393
2394static int
2395dsl_recursive_rename(char *oldname, const char *newname)
2396{
2397 int err;
2398 struct renamesnaparg *ra;
2399 dsl_sync_task_t *dst;
2400 spa_t *spa;
2401 char *cp, *fsname = spa_strdup(oldname);
428870ff 2402 int len = strlen(oldname) + 1;
34dc7c2f
BB
2403
2404 /* truncate the snapshot name to get the fsname */
2405 cp = strchr(fsname, '@');
2406 *cp = '\0';
2407
2408 err = spa_open(fsname, &spa, FTAG);
2409 if (err) {
428870ff 2410 kmem_free(fsname, len);
34dc7c2f
BB
2411 return (err);
2412 }
2413 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
2414 ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
2415
2416 ra->oldsnap = strchr(oldname, '@') + 1;
2417 ra->newsnap = strchr(newname, '@') + 1;
2418 *ra->failed = '\0';
2419
2420 err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
2421 DS_FIND_CHILDREN);
428870ff 2422 kmem_free(fsname, len);
34dc7c2f
BB
2423
2424 if (err == 0) {
2425 err = dsl_sync_task_group_wait(ra->dstg);
2426 }
2427
2428 for (dst = list_head(&ra->dstg->dstg_tasks); dst;
2429 dst = list_next(&ra->dstg->dstg_tasks, dst)) {
2430 dsl_dataset_t *ds = dst->dst_arg1;
2431 if (dst->dst_err) {
2432 dsl_dir_name(ds->ds_dir, ra->failed);
428870ff
BB
2433 (void) strlcat(ra->failed, "@", sizeof (ra->failed));
2434 (void) strlcat(ra->failed, ra->newsnap,
2435 sizeof (ra->failed));
34dc7c2f 2436 }
b128c09f 2437 dsl_dataset_rele(ds, ra->dstg);
34dc7c2f
BB
2438 }
2439
2440 if (err)
428870ff 2441 (void) strlcpy(oldname, ra->failed, sizeof (ra->failed));
34dc7c2f
BB
2442
2443 dsl_sync_task_group_destroy(ra->dstg);
2444 kmem_free(ra, sizeof (struct renamesnaparg));
2445 spa_close(spa, FTAG);
2446 return (err);
2447}
2448
2449static int
428870ff 2450dsl_valid_rename(const char *oldname, void *arg)
34dc7c2f
BB
2451{
2452 int delta = *(int *)arg;
2453
2454 if (strlen(oldname) + delta >= MAXNAMELEN)
2455 return (ENAMETOOLONG);
2456
2457 return (0);
2458}
2459
2460#pragma weak dmu_objset_rename = dsl_dataset_rename
2461int
b128c09f 2462dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
34dc7c2f
BB
2463{
2464 dsl_dir_t *dd;
2465 dsl_dataset_t *ds;
2466 const char *tail;
2467 int err;
2468
2469 err = dsl_dir_open(oldname, FTAG, &dd, &tail);
2470 if (err)
2471 return (err);
428870ff 2472
34dc7c2f
BB
2473 if (tail == NULL) {
2474 int delta = strlen(newname) - strlen(oldname);
2475
b128c09f 2476 /* if we're growing, validate child name lengths */
34dc7c2f
BB
2477 if (delta > 0)
2478 err = dmu_objset_find(oldname, dsl_valid_rename,
2479 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2480
428870ff 2481 if (err == 0)
34dc7c2f
BB
2482 err = dsl_dir_rename(dd, newname);
2483 dsl_dir_close(dd, FTAG);
2484 return (err);
2485 }
428870ff 2486
34dc7c2f 2487 if (tail[0] != '@') {
428870ff 2488 /* the name ended in a nonexistent component */
34dc7c2f
BB
2489 dsl_dir_close(dd, FTAG);
2490 return (ENOENT);
2491 }
2492
2493 dsl_dir_close(dd, FTAG);
2494
2495 /* new name must be snapshot in same filesystem */
2496 tail = strchr(newname, '@');
2497 if (tail == NULL)
2498 return (EINVAL);
2499 tail++;
2500 if (strncmp(oldname, newname, tail - newname) != 0)
2501 return (EXDEV);
2502
2503 if (recursive) {
2504 err = dsl_recursive_rename(oldname, newname);
2505 } else {
b128c09f 2506 err = dsl_dataset_hold(oldname, FTAG, &ds);
34dc7c2f
BB
2507 if (err)
2508 return (err);
2509
2510 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2511 dsl_dataset_snapshot_rename_check,
2512 dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
2513
b128c09f 2514 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
2515 }
2516
2517 return (err);
2518}
2519
b128c09f
BB
2520struct promotenode {
2521 list_node_t link;
2522 dsl_dataset_t *ds;
2523};
2524
34dc7c2f 2525struct promotearg {
b128c09f 2526 list_t shared_snaps, origin_snaps, clone_snaps;
428870ff 2527 dsl_dataset_t *origin_origin;
b128c09f 2528 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
428870ff 2529 char *err_ds;
34dc7c2f
BB
2530};
2531
b128c09f
BB
2532static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
2533
34dc7c2f
BB
2534static int
2535dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
2536{
2537 dsl_dataset_t *hds = arg1;
2538 struct promotearg *pa = arg2;
b128c09f
BB
2539 struct promotenode *snap = list_head(&pa->shared_snaps);
2540 dsl_dataset_t *origin_ds = snap->ds;
34dc7c2f 2541 int err;
428870ff 2542 uint64_t unused;
34dc7c2f 2543
b128c09f
BB
2544 /* Check that it is a real clone */
2545 if (!dsl_dir_is_clone(hds->ds_dir))
34dc7c2f
BB
2546 return (EINVAL);
2547
2548 /* Since this is so expensive, don't do the preliminary check */
2549 if (!dmu_tx_is_syncing(tx))
2550 return (0);
2551
b128c09f
BB
2552 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
2553 return (EXDEV);
34dc7c2f
BB
2554
2555 /* compute origin's new unique space */
b128c09f
BB
2556 snap = list_tail(&pa->clone_snaps);
2557 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
428870ff
BB
2558 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2559 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2560 &pa->unique, &unused, &unused);
34dc7c2f 2561
b128c09f
BB
2562 /*
2563 * Walk the snapshots that we are moving
2564 *
2565 * Compute space to transfer. Consider the incremental changes
2566 * to used for each snapshot:
2567 * (my used) = (prev's used) + (blocks born) - (blocks killed)
2568 * So each snapshot gave birth to:
2569 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2570 * So a sequence would look like:
2571 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2572 * Which simplifies to:
2573 * uN + kN + kN-1 + ... + k1 + k0
2574 * Note however, if we stop before we reach the ORIGIN we get:
2575 * uN + kN + kN-1 + ... + kM - uM-1
2576 */
2577 pa->used = origin_ds->ds_phys->ds_used_bytes;
2578 pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
2579 pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
2580 for (snap = list_head(&pa->shared_snaps); snap;
2581 snap = list_next(&pa->shared_snaps, snap)) {
34dc7c2f 2582 uint64_t val, dlused, dlcomp, dluncomp;
b128c09f 2583 dsl_dataset_t *ds = snap->ds;
34dc7c2f
BB
2584
2585 /* Check that the snapshot name does not conflict */
b128c09f
BB
2586 VERIFY(0 == dsl_dataset_get_snapname(ds));
2587 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
428870ff
BB
2588 if (err == 0) {
2589 err = EEXIST;
2590 goto out;
2591 }
b128c09f 2592 if (err != ENOENT)
428870ff 2593 goto out;
34dc7c2f 2594
b128c09f 2595 /* The very first snapshot does not have a deadlist */
34dc7c2f 2596 if (ds->ds_phys->ds_prev_snap_obj == 0)
b128c09f 2597 continue;
34dc7c2f 2598
428870ff
BB
2599 dsl_deadlist_space(&ds->ds_deadlist,
2600 &dlused, &dlcomp, &dluncomp);
b128c09f
BB
2601 pa->used += dlused;
2602 pa->comp += dlcomp;
2603 pa->uncomp += dluncomp;
2604 }
34dc7c2f 2605
b128c09f
BB
2606 /*
2607 * If we are a clone of a clone then we never reached ORIGIN,
2608 * so we need to subtract out the clone origin's used space.
2609 */
2610 if (pa->origin_origin) {
2611 pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
2612 pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
2613 pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
34dc7c2f
BB
2614 }
2615
2616 /* Check that there is enough space here */
b128c09f
BB
2617 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2618 pa->used);
2619 if (err)
2620 return (err);
34dc7c2f 2621
b128c09f
BB
2622 /*
2623 * Compute the amounts of space that will be used by snapshots
2624 * after the promotion (for both origin and clone). For each,
2625 * it is the amount of space that will be on all of their
2626 * deadlists (that was not born before their new origin).
2627 */
2628 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2629 uint64_t space;
2630
2631 /*
2632 * Note, typically this will not be a clone of a clone,
428870ff
BB
2633 * so dd_origin_txg will be < TXG_INITIAL, so
2634 * these snaplist_space() -> dsl_deadlist_space_range()
b128c09f
BB
2635 * calls will be fast because they do not have to
2636 * iterate over all bps.
2637 */
2638 snap = list_head(&pa->origin_snaps);
2639 err = snaplist_space(&pa->shared_snaps,
428870ff 2640 snap->ds->ds_dir->dd_origin_txg, &pa->cloneusedsnap);
b128c09f
BB
2641 if (err)
2642 return (err);
2643
2644 err = snaplist_space(&pa->clone_snaps,
428870ff 2645 snap->ds->ds_dir->dd_origin_txg, &space);
b128c09f
BB
2646 if (err)
2647 return (err);
2648 pa->cloneusedsnap += space;
2649 }
2650 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2651 err = snaplist_space(&pa->origin_snaps,
2652 origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
2653 if (err)
2654 return (err);
2655 }
2656
2657 return (0);
428870ff
BB
2658out:
2659 pa->err_ds = snap->ds->ds_snapname;
2660 return (err);
34dc7c2f
BB
2661}
2662
2663static void
428870ff 2664dsl_dataset_promote_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
2665{
2666 dsl_dataset_t *hds = arg1;
2667 struct promotearg *pa = arg2;
b128c09f
BB
2668 struct promotenode *snap = list_head(&pa->shared_snaps);
2669 dsl_dataset_t *origin_ds = snap->ds;
2670 dsl_dataset_t *origin_head;
34dc7c2f
BB
2671 dsl_dir_t *dd = hds->ds_dir;
2672 dsl_pool_t *dp = hds->ds_dir->dd_pool;
2673 dsl_dir_t *odd = NULL;
b128c09f
BB
2674 uint64_t oldnext_obj;
2675 int64_t delta;
34dc7c2f 2676
34dc7c2f
BB
2677 ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
2678
b128c09f
BB
2679 snap = list_head(&pa->origin_snaps);
2680 origin_head = snap->ds;
2681
34dc7c2f
BB
2682 /*
2683 * We need to explicitly open odd, since origin_ds's dd will be
2684 * changing.
2685 */
2686 VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
2687 NULL, FTAG, &odd));
2688
b128c09f
BB
2689 /* change origin's next snap */
2690 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2691 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
2692 snap = list_tail(&pa->clone_snaps);
2693 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2694 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2695
2696 /* change the origin's next clone */
2697 if (origin_ds->ds_phys->ds_next_clones_obj) {
428870ff 2698 remove_from_next_clones(origin_ds, snap->ds->ds_object, tx);
b128c09f
BB
2699 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2700 origin_ds->ds_phys->ds_next_clones_obj,
2701 oldnext_obj, tx));
2702 }
2703
2704 /* change origin */
2705 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2706 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2707 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
428870ff 2708 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
b128c09f
BB
2709 dmu_buf_will_dirty(odd->dd_dbuf, tx);
2710 odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
428870ff
BB
2711 origin_head->ds_dir->dd_origin_txg =
2712 origin_ds->ds_phys->ds_creation_txg;
2713
2714 /* change dd_clone entries */
2715 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2716 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
2717 odd->dd_phys->dd_clones, hds->ds_object, tx));
2718 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2719 pa->origin_origin->ds_dir->dd_phys->dd_clones,
2720 hds->ds_object, tx));
2721
2722 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
2723 pa->origin_origin->ds_dir->dd_phys->dd_clones,
2724 origin_head->ds_object, tx));
2725 if (dd->dd_phys->dd_clones == 0) {
2726 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2727 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2728 }
2729 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2730 dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2731
2732 }
34dc7c2f 2733
b128c09f
BB
2734 /* move snapshots to this dir */
2735 for (snap = list_head(&pa->shared_snaps); snap;
2736 snap = list_next(&pa->shared_snaps, snap)) {
2737 dsl_dataset_t *ds = snap->ds;
2738
2739 /* unregister props as dsl_dir is changing */
428870ff
BB
2740 if (ds->ds_objset) {
2741 dmu_objset_evict(ds->ds_objset);
2742 ds->ds_objset = NULL;
b128c09f 2743 }
34dc7c2f 2744 /* move snap name entry */
b128c09f
BB
2745 VERIFY(0 == dsl_dataset_get_snapname(ds));
2746 VERIFY(0 == dsl_dataset_snap_remove(origin_head,
2747 ds->ds_snapname, tx));
34dc7c2f
BB
2748 VERIFY(0 == zap_add(dp->dp_meta_objset,
2749 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2750 8, 1, &ds->ds_object, tx));
428870ff 2751
34dc7c2f
BB
2752 /* change containing dsl_dir */
2753 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2754 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2755 ds->ds_phys->ds_dir_obj = dd->dd_object;
2756 ASSERT3P(ds->ds_dir, ==, odd);
2757 dsl_dir_close(ds->ds_dir, ds);
2758 VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
2759 NULL, ds, &ds->ds_dir));
2760
428870ff
BB
2761 /* move any clone references */
2762 if (ds->ds_phys->ds_next_clones_obj &&
2763 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2764 zap_cursor_t zc;
2765 zap_attribute_t za;
2766
2767 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2768 ds->ds_phys->ds_next_clones_obj);
2769 zap_cursor_retrieve(&zc, &za) == 0;
2770 zap_cursor_advance(&zc)) {
2771 dsl_dataset_t *cnds;
2772 uint64_t o;
2773
2774 if (za.za_first_integer == oldnext_obj) {
2775 /*
2776 * We've already moved the
2777 * origin's reference.
2778 */
2779 continue;
2780 }
2781
2782 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
2783 za.za_first_integer, FTAG, &cnds));
2784 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2785
2786 VERIFY3U(zap_remove_int(dp->dp_meta_objset,
2787 odd->dd_phys->dd_clones, o, tx), ==, 0);
2788 VERIFY3U(zap_add_int(dp->dp_meta_objset,
2789 dd->dd_phys->dd_clones, o, tx), ==, 0);
2790 dsl_dataset_rele(cnds, FTAG);
2791 }
2792 zap_cursor_fini(&zc);
2793 }
2794
34dc7c2f 2795 ASSERT3U(dsl_prop_numcb(ds), ==, 0);
34dc7c2f 2796 }
34dc7c2f 2797
b128c09f
BB
2798 /*
2799 * Change space accounting.
2800 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2801 * both be valid, or both be 0 (resulting in delta == 0). This
2802 * is true for each of {clone,origin} independently.
2803 */
34dc7c2f 2804
b128c09f
BB
2805 delta = pa->cloneusedsnap -
2806 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2807 ASSERT3S(delta, >=, 0);
2808 ASSERT3U(pa->used, >=, delta);
2809 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2810 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2811 pa->used - delta, pa->comp, pa->uncomp, tx);
2812
2813 delta = pa->originusedsnap -
2814 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2815 ASSERT3S(delta, <=, 0);
2816 ASSERT3U(pa->used, >=, -delta);
2817 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2818 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2819 -pa->used - delta, -pa->comp, -pa->uncomp, tx);
34dc7c2f 2820
34dc7c2f
BB
2821 origin_ds->ds_phys->ds_unique_bytes = pa->unique;
2822
2823 /* log history record */
428870ff
BB
2824 spa_history_log_internal(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
2825 "dataset = %llu", hds->ds_object);
34dc7c2f
BB
2826
2827 dsl_dir_close(odd, FTAG);
34dc7c2f
BB
2828}
2829
b128c09f
BB
2830static char *snaplist_tag = "snaplist";
2831/*
2832 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2833 * (exclusive) and last_obj (inclusive). The list will be in reverse
2834 * order (last_obj will be the list_head()). If first_obj == 0, do all
2835 * snapshots back to this dataset's origin.
2836 */
2837static int
2838snaplist_make(dsl_pool_t *dp, boolean_t own,
2839 uint64_t first_obj, uint64_t last_obj, list_t *l)
2840{
2841 uint64_t obj = last_obj;
2842
2843 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
2844
2845 list_create(l, sizeof (struct promotenode),
2846 offsetof(struct promotenode, link));
2847
2848 while (obj != first_obj) {
2849 dsl_dataset_t *ds;
2850 struct promotenode *snap;
2851 int err;
2852
2853 if (own) {
2854 err = dsl_dataset_own_obj(dp, obj,
2855 0, snaplist_tag, &ds);
2856 if (err == 0)
2857 dsl_dataset_make_exclusive(ds, snaplist_tag);
2858 } else {
2859 err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
2860 }
2861 if (err == ENOENT) {
2862 /* lost race with snapshot destroy */
2863 struct promotenode *last = list_tail(l);
2864 ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
2865 obj = last->ds->ds_phys->ds_prev_snap_obj;
2866 continue;
2867 } else if (err) {
2868 return (err);
2869 }
2870
2871 if (first_obj == 0)
2872 first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2873
2874 snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
2875 snap->ds = ds;
2876 list_insert_tail(l, snap);
2877 obj = ds->ds_phys->ds_prev_snap_obj;
2878 }
2879
2880 return (0);
2881}
2882
2883static int
2884snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2885{
2886 struct promotenode *snap;
2887
2888 *spacep = 0;
2889 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
428870ff
BB
2890 uint64_t used, comp, uncomp;
2891 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2892 mintxg, UINT64_MAX, &used, &comp, &uncomp);
b128c09f
BB
2893 *spacep += used;
2894 }
2895 return (0);
2896}
2897
2898static void
2899snaplist_destroy(list_t *l, boolean_t own)
2900{
2901 struct promotenode *snap;
2902
9babb374 2903 if (!l || !list_link_active(&l->list_head))
b128c09f
BB
2904 return;
2905
2906 while ((snap = list_tail(l)) != NULL) {
2907 list_remove(l, snap);
2908 if (own)
2909 dsl_dataset_disown(snap->ds, snaplist_tag);
2910 else
2911 dsl_dataset_rele(snap->ds, snaplist_tag);
2912 kmem_free(snap, sizeof (struct promotenode));
2913 }
2914 list_destroy(l);
2915}
2916
2917/*
2918 * Promote a clone. Nomenclature note:
2919 * "clone" or "cds": the original clone which is being promoted
2920 * "origin" or "ods": the snapshot which is originally clone's origin
2921 * "origin head" or "ohds": the dataset which is the head
2922 * (filesystem/volume) for the origin
2923 * "origin origin": the origin of the origin's filesystem (typically
2924 * NULL, indicating that the clone is not a clone of a clone).
2925 */
34dc7c2f 2926int
428870ff 2927dsl_dataset_promote(const char *name, char *conflsnap)
34dc7c2f
BB
2928{
2929 dsl_dataset_t *ds;
b128c09f
BB
2930 dsl_dir_t *dd;
2931 dsl_pool_t *dp;
34dc7c2f 2932 dmu_object_info_t doi;
2598c001 2933 struct promotearg pa;
b128c09f
BB
2934 struct promotenode *snap;
2935 int err;
34dc7c2f 2936
2598c001 2937 bzero(&pa, sizeof(struct promotearg));
b128c09f 2938 err = dsl_dataset_hold(name, FTAG, &ds);
34dc7c2f
BB
2939 if (err)
2940 return (err);
b128c09f
BB
2941 dd = ds->ds_dir;
2942 dp = dd->dd_pool;
34dc7c2f 2943
b128c09f 2944 err = dmu_object_info(dp->dp_meta_objset,
34dc7c2f
BB
2945 ds->ds_phys->ds_snapnames_zapobj, &doi);
2946 if (err) {
b128c09f 2947 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
2948 return (err);
2949 }
2950
b128c09f
BB
2951 if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
2952 dsl_dataset_rele(ds, FTAG);
2953 return (EINVAL);
2954 }
2955
2956 /*
2957 * We are going to inherit all the snapshots taken before our
2958 * origin (i.e., our new origin will be our parent's origin).
2959 * Take ownership of them so that we can rename them into our
2960 * namespace.
2961 */
2962 rw_enter(&dp->dp_config_rwlock, RW_READER);
2963
2964 err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
2965 &pa.shared_snaps);
2966 if (err != 0)
2967 goto out;
2968
2969 err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
2970 if (err != 0)
2971 goto out;
2972
2973 snap = list_head(&pa.shared_snaps);
2974 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2975 err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
2976 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
2977 if (err != 0)
2978 goto out;
2979
428870ff
BB
2980 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2981 err = dsl_dataset_hold_obj(dp,
b128c09f 2982 snap->ds->ds_dir->dd_phys->dd_origin_obj,
428870ff 2983 FTAG, &pa.origin_origin);
b128c09f
BB
2984 if (err != 0)
2985 goto out;
2986 }
2987
2988out:
2989 rw_exit(&dp->dp_config_rwlock);
2990
34dc7c2f
BB
2991 /*
2992 * Add in 128x the snapnames zapobj size, since we will be moving
2993 * a bunch of snapnames to the promoted ds, and dirtying their
2994 * bonus buffers.
2995 */
b128c09f
BB
2996 if (err == 0) {
2997 err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
2998 dsl_dataset_promote_sync, ds, &pa,
428870ff
BB
2999 2 + 2 * doi.doi_physical_blocks_512);
3000 if (err && pa.err_ds && conflsnap)
3001 (void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN);
b128c09f
BB
3002 }
3003
3004 snaplist_destroy(&pa.shared_snaps, B_TRUE);
3005 snaplist_destroy(&pa.clone_snaps, B_FALSE);
3006 snaplist_destroy(&pa.origin_snaps, B_FALSE);
3007 if (pa.origin_origin)
428870ff 3008 dsl_dataset_rele(pa.origin_origin, FTAG);
b128c09f 3009 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
3010 return (err);
3011}
3012
3013struct cloneswaparg {
3014 dsl_dataset_t *cds; /* clone dataset */
3015 dsl_dataset_t *ohds; /* origin's head dataset */
3016 boolean_t force;
3017 int64_t unused_refres_delta; /* change in unconsumed refreservation */
3018};
3019
3020/* ARGSUSED */
3021static int
3022dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
3023{
3024 struct cloneswaparg *csa = arg1;
3025
3026 /* they should both be heads */
3027 if (dsl_dataset_is_snapshot(csa->cds) ||
3028 dsl_dataset_is_snapshot(csa->ohds))
3029 return (EINVAL);
3030
3031 /* the branch point should be just before them */
3032 if (csa->cds->ds_prev != csa->ohds->ds_prev)
3033 return (EINVAL);
3034
428870ff
BB
3035 /* cds should be the clone (unless they are unrelated) */
3036 if (csa->cds->ds_prev != NULL &&
3037 csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap &&
3038 csa->ohds->ds_object !=
3039 csa->cds->ds_prev->ds_phys->ds_next_snap_obj)
34dc7c2f
BB
3040 return (EINVAL);
3041
3042 /* the clone should be a child of the origin */
3043 if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
3044 return (EINVAL);
3045
3046 /* ohds shouldn't be modified unless 'force' */
3047 if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
3048 return (ETXTBSY);
3049
3050 /* adjust amount of any unconsumed refreservation */
3051 csa->unused_refres_delta =
3052 (int64_t)MIN(csa->ohds->ds_reserved,
3053 csa->ohds->ds_phys->ds_unique_bytes) -
3054 (int64_t)MIN(csa->ohds->ds_reserved,
3055 csa->cds->ds_phys->ds_unique_bytes);
3056
3057 if (csa->unused_refres_delta > 0 &&
3058 csa->unused_refres_delta >
3059 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
3060 return (ENOSPC);
3061
428870ff
BB
3062 if (csa->ohds->ds_quota != 0 &&
3063 csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota)
3064 return (EDQUOT);
3065
34dc7c2f
BB
3066 return (0);
3067}
3068
3069/* ARGSUSED */
3070static void
428870ff 3071dsl_dataset_clone_swap_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
3072{
3073 struct cloneswaparg *csa = arg1;
3074 dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
34dc7c2f
BB
3075
3076 ASSERT(csa->cds->ds_reserved == 0);
428870ff
BB
3077 ASSERT(csa->ohds->ds_quota == 0 ||
3078 csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota);
34dc7c2f
BB
3079
3080 dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
3081 dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
34dc7c2f 3082
428870ff
BB
3083 if (csa->cds->ds_objset != NULL) {
3084 dmu_objset_evict(csa->cds->ds_objset);
3085 csa->cds->ds_objset = NULL;
34dc7c2f
BB
3086 }
3087
428870ff
BB
3088 if (csa->ohds->ds_objset != NULL) {
3089 dmu_objset_evict(csa->ohds->ds_objset);
3090 csa->ohds->ds_objset = NULL;
34dc7c2f
BB
3091 }
3092
428870ff
BB
3093 /*
3094 * Reset origin's unique bytes, if it exists.
3095 */
3096 if (csa->cds->ds_prev) {
3097 dsl_dataset_t *origin = csa->cds->ds_prev;
3098 uint64_t comp, uncomp;
3099
3100 dmu_buf_will_dirty(origin->ds_dbuf, tx);
3101 dsl_deadlist_space_range(&csa->cds->ds_deadlist,
3102 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
3103 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
3104 }
34dc7c2f
BB
3105
3106 /* swap blkptrs */
3107 {
3108 blkptr_t tmp;
3109 tmp = csa->ohds->ds_phys->ds_bp;
3110 csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
3111 csa->cds->ds_phys->ds_bp = tmp;
3112 }
3113
3114 /* set dd_*_bytes */
3115 {
3116 int64_t dused, dcomp, duncomp;
3117 uint64_t cdl_used, cdl_comp, cdl_uncomp;
3118 uint64_t odl_used, odl_comp, odl_uncomp;
3119
b128c09f
BB
3120 ASSERT3U(csa->cds->ds_dir->dd_phys->
3121 dd_used_breakdown[DD_USED_SNAP], ==, 0);
3122
428870ff
BB
3123 dsl_deadlist_space(&csa->cds->ds_deadlist,
3124 &cdl_used, &cdl_comp, &cdl_uncomp);
3125 dsl_deadlist_space(&csa->ohds->ds_deadlist,
3126 &odl_used, &odl_comp, &odl_uncomp);
b128c09f 3127
34dc7c2f
BB
3128 dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
3129 (csa->ohds->ds_phys->ds_used_bytes + odl_used);
3130 dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
3131 (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
3132 duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
3133 cdl_uncomp -
3134 (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
3135
b128c09f 3136 dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
34dc7c2f 3137 dused, dcomp, duncomp, tx);
b128c09f 3138 dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
34dc7c2f 3139 -dused, -dcomp, -duncomp, tx);
b128c09f
BB
3140
3141 /*
3142 * The difference in the space used by snapshots is the
3143 * difference in snapshot space due to the head's
3144 * deadlist (since that's the only thing that's
3145 * changing that affects the snapused).
3146 */
428870ff
BB
3147 dsl_deadlist_space_range(&csa->cds->ds_deadlist,
3148 csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX,
3149 &cdl_used, &cdl_comp, &cdl_uncomp);
3150 dsl_deadlist_space_range(&csa->ohds->ds_deadlist,
3151 csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX,
3152 &odl_used, &odl_comp, &odl_uncomp);
b128c09f
BB
3153 dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
3154 DD_USED_HEAD, DD_USED_SNAP, tx);
34dc7c2f
BB
3155 }
3156
34dc7c2f
BB
3157 /* swap ds_*_bytes */
3158 SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
3159 csa->cds->ds_phys->ds_used_bytes);
3160 SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
3161 csa->cds->ds_phys->ds_compressed_bytes);
3162 SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
3163 csa->cds->ds_phys->ds_uncompressed_bytes);
3164 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
3165 csa->cds->ds_phys->ds_unique_bytes);
3166
3167 /* apply any parent delta for change in unconsumed refreservation */
b128c09f
BB
3168 dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
3169 csa->unused_refres_delta, 0, 0, tx);
34dc7c2f 3170
428870ff
BB
3171 /*
3172 * Swap deadlists.
3173 */
3174 dsl_deadlist_close(&csa->cds->ds_deadlist);
3175 dsl_deadlist_close(&csa->ohds->ds_deadlist);
34dc7c2f
BB
3176 SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
3177 csa->cds->ds_phys->ds_deadlist_obj);
428870ff
BB
3178 dsl_deadlist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
3179 csa->cds->ds_phys->ds_deadlist_obj);
3180 dsl_deadlist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
3181 csa->ohds->ds_phys->ds_deadlist_obj);
b128c09f 3182
428870ff 3183 dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx);
34dc7c2f
BB
3184}
3185
3186/*
428870ff
BB
3187 * Swap 'clone' with its origin head datasets. Used at the end of "zfs
3188 * recv" into an existing fs to swizzle the file system to the new
3189 * version, and by "zfs rollback". Can also be used to swap two
3190 * independent head datasets if neither has any snapshots.
34dc7c2f
BB
3191 */
3192int
3193dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
3194 boolean_t force)
3195{
3196 struct cloneswaparg csa;
b128c09f 3197 int error;
34dc7c2f 3198
b128c09f
BB
3199 ASSERT(clone->ds_owner);
3200 ASSERT(origin_head->ds_owner);
3201retry:
572e2857
BB
3202 /*
3203 * Need exclusive access for the swap. If we're swapping these
3204 * datasets back after an error, we already hold the locks.
3205 */
3206 if (!RW_WRITE_HELD(&clone->ds_rwlock))
3207 rw_enter(&clone->ds_rwlock, RW_WRITER);
3208 if (!RW_WRITE_HELD(&origin_head->ds_rwlock) &&
3209 !rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
b128c09f
BB
3210 rw_exit(&clone->ds_rwlock);
3211 rw_enter(&origin_head->ds_rwlock, RW_WRITER);
3212 if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
3213 rw_exit(&origin_head->ds_rwlock);
3214 goto retry;
3215 }
3216 }
34dc7c2f
BB
3217 csa.cds = clone;
3218 csa.ohds = origin_head;
3219 csa.force = force;
b128c09f 3220 error = dsl_sync_task_do(clone->ds_dir->dd_pool,
34dc7c2f 3221 dsl_dataset_clone_swap_check,
b128c09f
BB
3222 dsl_dataset_clone_swap_sync, &csa, NULL, 9);
3223 return (error);
34dc7c2f
BB
3224}
3225
3226/*
3227 * Given a pool name and a dataset object number in that pool,
3228 * return the name of that dataset.
3229 */
3230int
3231dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3232{
3233 spa_t *spa;
3234 dsl_pool_t *dp;
b128c09f 3235 dsl_dataset_t *ds;
34dc7c2f
BB
3236 int error;
3237
3238 if ((error = spa_open(pname, &spa, FTAG)) != 0)
3239 return (error);
3240 dp = spa_get_dsl(spa);
3241 rw_enter(&dp->dp_config_rwlock, RW_READER);
b128c09f
BB
3242 if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
3243 dsl_dataset_name(ds, buf);
3244 dsl_dataset_rele(ds, FTAG);
34dc7c2f 3245 }
34dc7c2f
BB
3246 rw_exit(&dp->dp_config_rwlock);
3247 spa_close(spa, FTAG);
3248
b128c09f 3249 return (error);
34dc7c2f
BB
3250}
3251
3252int
3253dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
b128c09f 3254 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
34dc7c2f
BB
3255{
3256 int error = 0;
3257
3258 ASSERT3S(asize, >, 0);
3259
3260 /*
3261 * *ref_rsrv is the portion of asize that will come from any
3262 * unconsumed refreservation space.
3263 */
3264 *ref_rsrv = 0;
3265
3266 mutex_enter(&ds->ds_lock);
3267 /*
3268 * Make a space adjustment for reserved bytes.
3269 */
3270 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
3271 ASSERT3U(*used, >=,
3272 ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
3273 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
3274 *ref_rsrv =
3275 asize - MIN(asize, parent_delta(ds, asize + inflight));
3276 }
3277
3278 if (!check_quota || ds->ds_quota == 0) {
3279 mutex_exit(&ds->ds_lock);
3280 return (0);
3281 }
3282 /*
3283 * If they are requesting more space, and our current estimate
3284 * is over quota, they get to try again unless the actual
3285 * on-disk is over quota and there are no pending changes (which
3286 * may free up space for us).
3287 */
3288 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
3289 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
3290 error = ERESTART;
3291 else
3292 error = EDQUOT;
3293 }
3294 mutex_exit(&ds->ds_lock);
3295
3296 return (error);
3297}
3298
3299/* ARGSUSED */
3300static int
3301dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
3302{
3303 dsl_dataset_t *ds = arg1;
428870ff
BB
3304 dsl_prop_setarg_t *psa = arg2;
3305 int err;
34dc7c2f
BB
3306
3307 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
3308 return (ENOTSUP);
3309
428870ff
BB
3310 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
3311 return (err);
3312
3313 if (psa->psa_effective_value == 0)
34dc7c2f
BB
3314 return (0);
3315
428870ff
BB
3316 if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes ||
3317 psa->psa_effective_value < ds->ds_reserved)
34dc7c2f
BB
3318 return (ENOSPC);
3319
3320 return (0);
3321}
3322
428870ff
BB
3323extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *);
3324
34dc7c2f 3325void
428870ff 3326dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
3327{
3328 dsl_dataset_t *ds = arg1;
428870ff
BB
3329 dsl_prop_setarg_t *psa = arg2;
3330 uint64_t effective_value = psa->psa_effective_value;
34dc7c2f 3331
428870ff
BB
3332 dsl_prop_set_sync(ds, psa, tx);
3333 DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
34dc7c2f 3334
428870ff
BB
3335 if (ds->ds_quota != effective_value) {
3336 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3337 ds->ds_quota = effective_value;
34dc7c2f 3338
428870ff
BB
3339 spa_history_log_internal(LOG_DS_REFQUOTA,
3340 ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu ",
3341 (longlong_t)ds->ds_quota, ds->ds_object);
3342 }
34dc7c2f
BB
3343}
3344
3345int
428870ff 3346dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
34dc7c2f
BB
3347{
3348 dsl_dataset_t *ds;
428870ff 3349 dsl_prop_setarg_t psa;
34dc7c2f
BB
3350 int err;
3351
428870ff
BB
3352 dsl_prop_setarg_init_uint64(&psa, "refquota", source, &quota);
3353
b128c09f 3354 err = dsl_dataset_hold(dsname, FTAG, &ds);
34dc7c2f
BB
3355 if (err)
3356 return (err);
3357
428870ff
BB
3358 /*
3359 * If someone removes a file, then tries to set the quota, we
3360 * want to make sure the file freeing takes effect.
3361 */
3362 txg_wait_open(ds->ds_dir->dd_pool, 0);
3363
3364 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
3365 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
3366 ds, &psa, 0);
34dc7c2f 3367
b128c09f 3368 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
3369 return (err);
3370}
3371
3372static int
3373dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
3374{
3375 dsl_dataset_t *ds = arg1;
428870ff
BB
3376 dsl_prop_setarg_t *psa = arg2;
3377 uint64_t effective_value;
34dc7c2f 3378 uint64_t unique;
428870ff 3379 int err;
34dc7c2f 3380
34dc7c2f
BB
3381 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
3382 SPA_VERSION_REFRESERVATION)
3383 return (ENOTSUP);
3384
3385 if (dsl_dataset_is_snapshot(ds))
3386 return (EINVAL);
3387
428870ff
BB
3388 if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
3389 return (err);
3390
3391 effective_value = psa->psa_effective_value;
3392
34dc7c2f
BB
3393 /*
3394 * If we are doing the preliminary check in open context, the
3395 * space estimates may be inaccurate.
3396 */
3397 if (!dmu_tx_is_syncing(tx))
3398 return (0);
3399
3400 mutex_enter(&ds->ds_lock);
428870ff
BB
3401 if (!DS_UNIQUE_IS_ACCURATE(ds))
3402 dsl_dataset_recalc_head_uniq(ds);
3403 unique = ds->ds_phys->ds_unique_bytes;
34dc7c2f
BB
3404 mutex_exit(&ds->ds_lock);
3405
428870ff
BB
3406 if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) {
3407 uint64_t delta = MAX(unique, effective_value) -
d164b209
BB
3408 MAX(unique, ds->ds_reserved);
3409
3410 if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
3411 return (ENOSPC);
3412 if (ds->ds_quota > 0 &&
428870ff 3413 effective_value > ds->ds_quota)
d164b209
BB
3414 return (ENOSPC);
3415 }
34dc7c2f
BB
3416
3417 return (0);
3418}
3419
34dc7c2f 3420static void
428870ff 3421dsl_dataset_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
34dc7c2f
BB
3422{
3423 dsl_dataset_t *ds = arg1;
428870ff
BB
3424 dsl_prop_setarg_t *psa = arg2;
3425 uint64_t effective_value = psa->psa_effective_value;
34dc7c2f
BB
3426 uint64_t unique;
3427 int64_t delta;
3428
428870ff
BB
3429 dsl_prop_set_sync(ds, psa, tx);
3430 DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3431
34dc7c2f
BB
3432 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3433
b128c09f 3434 mutex_enter(&ds->ds_dir->dd_lock);
34dc7c2f 3435 mutex_enter(&ds->ds_lock);
428870ff
BB
3436 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3437 unique = ds->ds_phys->ds_unique_bytes;
3438 delta = MAX(0, (int64_t)(effective_value - unique)) -
34dc7c2f 3439 MAX(0, (int64_t)(ds->ds_reserved - unique));
428870ff 3440 ds->ds_reserved = effective_value;
34dc7c2f
BB
3441 mutex_exit(&ds->ds_lock);
3442
b128c09f
BB
3443 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3444 mutex_exit(&ds->ds_dir->dd_lock);
34dc7c2f 3445
428870ff
BB
3446 spa_history_log_internal(LOG_DS_REFRESERV,
3447 ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu",
3448 (longlong_t)effective_value, ds->ds_object);
34dc7c2f
BB
3449}
3450
3451int
428870ff
BB
3452dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
3453 uint64_t reservation)
34dc7c2f
BB
3454{
3455 dsl_dataset_t *ds;
428870ff 3456 dsl_prop_setarg_t psa;
34dc7c2f
BB
3457 int err;
3458
428870ff
BB
3459 dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
3460 &reservation);
3461
b128c09f 3462 err = dsl_dataset_hold(dsname, FTAG, &ds);
34dc7c2f
BB
3463 if (err)
3464 return (err);
3465
3466 err = dsl_sync_task_do(ds->ds_dir->dd_pool,
3467 dsl_dataset_set_reservation_check,
428870ff
BB
3468 dsl_dataset_set_reservation_sync, ds, &psa, 0);
3469
b128c09f 3470 dsl_dataset_rele(ds, FTAG);
34dc7c2f
BB
3471 return (err);
3472}
45d1cae3 3473
572e2857
BB
3474typedef struct zfs_hold_cleanup_arg {
3475 dsl_pool_t *dp;
3476 uint64_t dsobj;
3477 char htag[MAXNAMELEN];
3478} zfs_hold_cleanup_arg_t;
3479
3480static void
3481dsl_dataset_user_release_onexit(void *arg)
3482{
3483 zfs_hold_cleanup_arg_t *ca = arg;
3484
3485 (void) dsl_dataset_user_release_tmp(ca->dp, ca->dsobj, ca->htag,
3486 B_TRUE);
3487 kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t));
3488}
3489
3490void
3491dsl_register_onexit_hold_cleanup(dsl_dataset_t *ds, const char *htag,
3492 minor_t minor)
3493{
3494 zfs_hold_cleanup_arg_t *ca;
3495
3496 ca = kmem_alloc(sizeof (zfs_hold_cleanup_arg_t), KM_SLEEP);
3497 ca->dp = ds->ds_dir->dd_pool;
3498 ca->dsobj = ds->ds_object;
3499 (void) strlcpy(ca->htag, htag, sizeof (ca->htag));
3500 VERIFY3U(0, ==, zfs_onexit_add_cb(minor,
3501 dsl_dataset_user_release_onexit, ca, NULL));
3502}
428870ff
BB
3503
3504/*
572e2857
BB
3505 * If you add new checks here, you may need to add
3506 * additional checks to the "temporary" case in
3507 * snapshot_check() in dmu_objset.c.
428870ff 3508 */
45d1cae3
BB
3509static int
3510dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
3511{
3512 dsl_dataset_t *ds = arg1;
428870ff
BB
3513 struct dsl_ds_holdarg *ha = arg2;
3514 char *htag = ha->htag;
45d1cae3
BB
3515 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3516 int error = 0;
3517
3518 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3519 return (ENOTSUP);
3520
3521 if (!dsl_dataset_is_snapshot(ds))
3522 return (EINVAL);
3523
45d1cae3
BB
3524 /* tags must be unique */
3525 mutex_enter(&ds->ds_lock);
3526 if (ds->ds_phys->ds_userrefs_obj) {
3527 error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
3528 8, 1, tx);
3529 if (error == 0)
3530 error = EEXIST;
3531 else if (error == ENOENT)
3532 error = 0;
3533 }
3534 mutex_exit(&ds->ds_lock);
3535
428870ff
BB
3536 if (error == 0 && ha->temphold &&
3537 strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
3538 error = E2BIG;
3539
45d1cae3
BB
3540 return (error);
3541}
3542
572e2857 3543void
428870ff 3544dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx)
45d1cae3
BB
3545{
3546 dsl_dataset_t *ds = arg1;
428870ff
BB
3547 struct dsl_ds_holdarg *ha = arg2;
3548 char *htag = ha->htag;
3549 dsl_pool_t *dp = ds->ds_dir->dd_pool;
3550 objset_t *mos = dp->dp_meta_objset;
3551 uint64_t now = gethrestime_sec();
45d1cae3
BB
3552 uint64_t zapobj;
3553
3554 mutex_enter(&ds->ds_lock);
3555 if (ds->ds_phys->ds_userrefs_obj == 0) {
3556 /*
3557 * This is the first user hold for this dataset. Create
3558 * the userrefs zap object.
3559 */
3560 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3561 zapobj = ds->ds_phys->ds_userrefs_obj =
3562 zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
3563 } else {
3564 zapobj = ds->ds_phys->ds_userrefs_obj;
3565 }
3566 ds->ds_userrefs++;
3567 mutex_exit(&ds->ds_lock);
3568
3569 VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
3570
428870ff
BB
3571 if (ha->temphold) {
3572 VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
3573 htag, &now, tx));
3574 }
45d1cae3 3575
428870ff
BB
3576 spa_history_log_internal(LOG_DS_USER_HOLD,
3577 dp->dp_spa, tx, "<%s> temp = %d dataset = %llu", htag,
3578 (int)ha->temphold, ds->ds_object);
3579}
45d1cae3
BB
3580
3581static int
428870ff 3582dsl_dataset_user_hold_one(const char *dsname, void *arg)
45d1cae3
BB
3583{
3584 struct dsl_ds_holdarg *ha = arg;
3585 dsl_dataset_t *ds;
3586 int error;
3587 char *name;
45d1cae3
BB
3588
3589 /* alloc a buffer to hold dsname@snapname plus terminating NULL */
428870ff 3590 name = kmem_asprintf("%s@%s", dsname, ha->snapname);
45d1cae3 3591 error = dsl_dataset_hold(name, ha->dstg, &ds);
428870ff 3592 strfree(name);
45d1cae3 3593 if (error == 0) {
428870ff 3594 ha->gotone = B_TRUE;
45d1cae3 3595 dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
428870ff 3596 dsl_dataset_user_hold_sync, ds, ha, 0);
45d1cae3
BB
3597 } else if (error == ENOENT && ha->recursive) {
3598 error = 0;
3599 } else {
428870ff 3600 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
45d1cae3
BB
3601 }
3602 return (error);
3603}
3604
572e2857
BB
3605int
3606dsl_dataset_user_hold_for_send(dsl_dataset_t *ds, char *htag,
3607 boolean_t temphold)
3608{
3609 struct dsl_ds_holdarg *ha;
3610 int error;
3611
3612 ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3613 ha->htag = htag;
3614 ha->temphold = temphold;
3615 error = dsl_sync_task_do(ds->ds_dir->dd_pool,
3616 dsl_dataset_user_hold_check, dsl_dataset_user_hold_sync,
3617 ds, ha, 0);
3618 kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3619
3620 return (error);
3621}
3622
45d1cae3
BB
3623int
3624dsl_dataset_user_hold(char *dsname, char *snapname, char *htag,
572e2857 3625 boolean_t recursive, boolean_t temphold, int cleanup_fd)
45d1cae3
BB
3626{
3627 struct dsl_ds_holdarg *ha;
3628 dsl_sync_task_t *dst;
3629 spa_t *spa;
3630 int error;
572e2857
BB
3631 minor_t minor = 0;
3632
3633 if (cleanup_fd != -1) {
3634 /* Currently we only support cleanup-on-exit of tempholds. */
3635 if (!temphold)
3636 return (EINVAL);
3637 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
3638 if (error)
3639 return (error);
3640 }
45d1cae3
BB
3641
3642 ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3643
3644 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3645
3646 error = spa_open(dsname, &spa, FTAG);
3647 if (error) {
3648 kmem_free(ha, sizeof (struct dsl_ds_holdarg));
572e2857
BB
3649 if (cleanup_fd != -1)
3650 zfs_onexit_fd_rele(cleanup_fd);
45d1cae3
BB
3651 return (error);
3652 }
3653
3654 ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
3655 ha->htag = htag;
3656 ha->snapname = snapname;
3657 ha->recursive = recursive;
428870ff 3658 ha->temphold = temphold;
572e2857 3659
45d1cae3
BB
3660 if (recursive) {
3661 error = dmu_objset_find(dsname, dsl_dataset_user_hold_one,
3662 ha, DS_FIND_CHILDREN);
3663 } else {
3664 error = dsl_dataset_user_hold_one(dsname, ha);
3665 }
3666 if (error == 0)
3667 error = dsl_sync_task_group_wait(ha->dstg);
3668
3669 for (dst = list_head(&ha->dstg->dstg_tasks); dst;
3670 dst = list_next(&ha->dstg->dstg_tasks, dst)) {
3671 dsl_dataset_t *ds = dst->dst_arg1;
3672
3673 if (dst->dst_err) {
3674 dsl_dataset_name(ds, ha->failed);
3675 *strchr(ha->failed, '@') = '\0';
572e2857
BB
3676 } else if (error == 0 && minor != 0 && temphold) {
3677 /*
3678 * If this hold is to be released upon process exit,
3679 * register that action now.
3680 */
3681 dsl_register_onexit_hold_cleanup(ds, htag, minor);
45d1cae3
BB
3682 }
3683 dsl_dataset_rele(ds, ha->dstg);
3684 }
3685
428870ff
BB
3686 if (error == 0 && recursive && !ha->gotone)
3687 error = ENOENT;
3688
45d1cae3 3689 if (error)
428870ff 3690 (void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
45d1cae3
BB
3691
3692 dsl_sync_task_group_destroy(ha->dstg);
572e2857 3693
45d1cae3
BB
3694 kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3695 spa_close(spa, FTAG);
572e2857
BB
3696 if (cleanup_fd != -1)
3697 zfs_onexit_fd_rele(cleanup_fd);
45d1cae3
BB
3698 return (error);
3699}
3700
3701struct dsl_ds_releasearg {
3702 dsl_dataset_t *ds;
3703 const char *htag;
3704 boolean_t own; /* do we own or just hold ds? */
3705};
3706
3707static int
3708dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag,
3709 boolean_t *might_destroy)
3710{
3711 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3712 uint64_t zapobj;
3713 uint64_t tmp;
3714 int error;
3715
3716 *might_destroy = B_FALSE;
3717
3718 mutex_enter(&ds->ds_lock);
3719 zapobj = ds->ds_phys->ds_userrefs_obj;
3720 if (zapobj == 0) {
3721 /* The tag can't possibly exist */
3722 mutex_exit(&ds->ds_lock);
3723 return (ESRCH);
3724 }
3725
3726 /* Make sure the tag exists */
3727 error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp);
3728 if (error) {
3729 mutex_exit(&ds->ds_lock);
3730 if (error == ENOENT)
3731 error = ESRCH;
3732 return (error);
3733 }
3734
3735 if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 &&
3736 DS_IS_DEFER_DESTROY(ds))
3737 *might_destroy = B_TRUE;
3738
3739 mutex_exit(&ds->ds_lock);
3740 return (0);
3741}
3742
3743static int
3744dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx)
3745{
3746 struct dsl_ds_releasearg *ra = arg1;
3747 dsl_dataset_t *ds = ra->ds;
3748 boolean_t might_destroy;
3749 int error;
3750
3751 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3752 return (ENOTSUP);
3753
3754 error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy);
3755 if (error)
3756 return (error);
3757
3758 if (might_destroy) {
3759 struct dsl_ds_destroyarg dsda = {0};
3760
3761 if (dmu_tx_is_syncing(tx)) {
3762 /*
3763 * If we're not prepared to remove the snapshot,
3764 * we can't allow the release to happen right now.
3765 */
3766 if (!ra->own)
3767 return (EBUSY);
45d1cae3
BB
3768 }
3769 dsda.ds = ds;
3770 dsda.releasing = B_TRUE;
3771 return (dsl_dataset_destroy_check(&dsda, tag, tx));
3772 }
3773
3774 return (0);
3775}
3776
3777static void
428870ff 3778dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx)
45d1cae3
BB
3779{
3780 struct dsl_ds_releasearg *ra = arg1;
3781 dsl_dataset_t *ds = ra->ds;
428870ff
BB
3782 dsl_pool_t *dp = ds->ds_dir->dd_pool;
3783 objset_t *mos = dp->dp_meta_objset;
45d1cae3
BB
3784 uint64_t zapobj;
3785 uint64_t dsobj = ds->ds_object;
3786 uint64_t refs;
428870ff
BB
3787 int error;
3788
45d1cae3
BB
3789 mutex_enter(&ds->ds_lock);
3790 ds->ds_userrefs--;
3791 refs = ds->ds_userrefs;
3792 mutex_exit(&ds->ds_lock);
428870ff
BB
3793 error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
3794 VERIFY(error == 0 || error == ENOENT);
45d1cae3
BB
3795 zapobj = ds->ds_phys->ds_userrefs_obj;
3796 VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
3797 if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
3798 DS_IS_DEFER_DESTROY(ds)) {
3799 struct dsl_ds_destroyarg dsda = {0};
3800
3801 ASSERT(ra->own);
3802 dsda.ds = ds;
3803 dsda.releasing = B_TRUE;
3804 /* We already did the destroy_check */
428870ff 3805 dsl_dataset_destroy_sync(&dsda, tag, tx);
45d1cae3
BB
3806 }
3807
428870ff
BB
3808 spa_history_log_internal(LOG_DS_USER_RELEASE,
3809 dp->dp_spa, tx, "<%s> %lld dataset = %llu",
45d1cae3
BB
3810 ra->htag, (longlong_t)refs, dsobj);
3811}
3812
3813static int
428870ff 3814dsl_dataset_user_release_one(const char *dsname, void *arg)
45d1cae3
BB
3815{
3816 struct dsl_ds_holdarg *ha = arg;
3817 struct dsl_ds_releasearg *ra;
3818 dsl_dataset_t *ds;
3819 int error;
3820 void *dtag = ha->dstg;
3821 char *name;
45d1cae3
BB
3822 boolean_t own = B_FALSE;
3823 boolean_t might_destroy;
3824
45d1cae3 3825 /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
428870ff 3826 name = kmem_asprintf("%s@%s", dsname, ha->snapname);
45d1cae3 3827 error = dsl_dataset_hold(name, dtag, &ds);
428870ff 3828 strfree(name);
45d1cae3
BB
3829 if (error == ENOENT && ha->recursive)
3830 return (0);
428870ff 3831 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
45d1cae3
BB
3832 if (error)
3833 return (error);
3834
428870ff
BB
3835 ha->gotone = B_TRUE;
3836
45d1cae3
BB
3837 ASSERT(dsl_dataset_is_snapshot(ds));
3838
3839 error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy);
3840 if (error) {
3841 dsl_dataset_rele(ds, dtag);
3842 return (error);
3843 }
3844
3845 if (might_destroy) {
3846#ifdef _KERNEL
428870ff 3847 name = kmem_asprintf("%s@%s", dsname, ha->snapname);
45d1cae3 3848 error = zfs_unmount_snap(name, NULL);
428870ff 3849 strfree(name);
45d1cae3
BB
3850 if (error) {
3851 dsl_dataset_rele(ds, dtag);
3852 return (error);
3853 }
3854#endif
428870ff 3855 if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) {
45d1cae3
BB
3856 dsl_dataset_rele(ds, dtag);
3857 return (EBUSY);
3858 } else {
3859 own = B_TRUE;
3860 dsl_dataset_make_exclusive(ds, dtag);
3861 }
3862 }
3863
3864 ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP);
3865 ra->ds = ds;
3866 ra->htag = ha->htag;
3867 ra->own = own;
3868 dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check,
3869 dsl_dataset_user_release_sync, ra, dtag, 0);
3870
3871 return (0);
3872}
3873
3874int
3875dsl_dataset_user_release(char *dsname, char *snapname, char *htag,
3876 boolean_t recursive)
3877{
3878 struct dsl_ds_holdarg *ha;
3879 dsl_sync_task_t *dst;
3880 spa_t *spa;
3881 int error;
3882
428870ff 3883top:
45d1cae3
BB
3884 ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3885
3886 (void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3887
3888 error = spa_open(dsname, &spa, FTAG);
3889 if (error) {
3890 kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3891 return (error);
3892 }
3893
3894 ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
3895 ha->htag = htag;
3896 ha->snapname = snapname;
3897 ha->recursive = recursive;
3898 if (recursive) {
3899 error = dmu_objset_find(dsname, dsl_dataset_user_release_one,
3900 ha, DS_FIND_CHILDREN);
3901 } else {
3902 error = dsl_dataset_user_release_one(dsname, ha);
3903 }
3904 if (error == 0)
3905 error = dsl_sync_task_group_wait(ha->dstg);
3906
3907 for (dst = list_head(&ha->dstg->dstg_tasks); dst;
3908 dst = list_next(&ha->dstg->dstg_tasks, dst)) {
3909 struct dsl_ds_releasearg *ra = dst->dst_arg1;
3910 dsl_dataset_t *ds = ra->ds;
3911
3912 if (dst->dst_err)
3913 dsl_dataset_name(ds, ha->failed);
3914
3915 if (ra->own)
3916 dsl_dataset_disown(ds, ha->dstg);
3917 else
3918 dsl_dataset_rele(ds, ha->dstg);
3919
3920 kmem_free(ra, sizeof (struct dsl_ds_releasearg));
3921 }
3922
428870ff
BB
3923 if (error == 0 && recursive && !ha->gotone)
3924 error = ENOENT;
3925
3926 if (error && error != EBUSY)
3927 (void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
45d1cae3
BB
3928
3929 dsl_sync_task_group_destroy(ha->dstg);
3930 kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3931 spa_close(spa, FTAG);
428870ff
BB
3932
3933 /*
3934 * We can get EBUSY if we were racing with deferred destroy and
3935 * dsl_dataset_user_release_check() hadn't done the necessary
3936 * open context setup. We can also get EBUSY if we're racing
3937 * with destroy and that thread is the ds_owner. Either way
3938 * the busy condition should be transient, and we should retry
3939 * the release operation.
3940 */
3941 if (error == EBUSY)
3942 goto top;
3943
45d1cae3
BB
3944 return (error);
3945}
3946
428870ff 3947/*
572e2857
BB
3948 * Called at spa_load time (with retry == B_FALSE) to release a stale
3949 * temporary user hold. Also called by the onexit code (with retry == B_TRUE).
428870ff
BB
3950 */
3951int
572e2857
BB
3952dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag,
3953 boolean_t retry)
428870ff
BB
3954{
3955 dsl_dataset_t *ds;
3956 char *snap;
3957 char *name;
3958 int namelen;
3959 int error;
3960
572e2857
BB
3961 do {
3962 rw_enter(&dp->dp_config_rwlock, RW_READER);
3963 error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
3964 rw_exit(&dp->dp_config_rwlock);
3965 if (error)
3966 return (error);
3967 namelen = dsl_dataset_namelen(ds)+1;
3968 name = kmem_alloc(namelen, KM_SLEEP);
3969 dsl_dataset_name(ds, name);
3970 dsl_dataset_rele(ds, FTAG);
428870ff 3971
572e2857
BB
3972 snap = strchr(name, '@');
3973 *snap = '\0';
3974 ++snap;
3975 error = dsl_dataset_user_release(name, snap, htag, B_FALSE);
3976 kmem_free(name, namelen);
3977
3978 /*
3979 * The object can't have been destroyed because we have a hold,
3980 * but it might have been renamed, resulting in ENOENT. Retry
3981 * if we've been requested to do so.
3982 *
3983 * It would be nice if we could use the dsobj all the way
3984 * through and avoid ENOENT entirely. But we might need to
3985 * unmount the snapshot, and there's currently no way to lookup
3986 * a vfsp using a ZFS object id.
3987 */
3988 } while ((error == ENOENT) && retry);
3989
3990 return (error);
428870ff
BB
3991}
3992
45d1cae3
BB
3993int
3994dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp)
3995{
3996 dsl_dataset_t *ds;
3997 int err;
3998
3999 err = dsl_dataset_hold(dsname, FTAG, &ds);
4000 if (err)
4001 return (err);
4002
4003 VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP));
4004 if (ds->ds_phys->ds_userrefs_obj != 0) {
4005 zap_attribute_t *za;
4006 zap_cursor_t zc;
4007
4008 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
4009 for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
4010 ds->ds_phys->ds_userrefs_obj);
4011 zap_cursor_retrieve(&zc, za) == 0;
4012 zap_cursor_advance(&zc)) {
4013 VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name,
4014 za->za_first_integer));
4015 }
4016 zap_cursor_fini(&zc);
4017 kmem_free(za, sizeof (zap_attribute_t));
4018 }
4019 dsl_dataset_rele(ds, FTAG);
4020 return (0);
4021}
428870ff
BB
4022
4023/*
4024 * Note, this fuction is used as the callback for dmu_objset_find(). We
4025 * always return 0 so that we will continue to find and process
4026 * inconsistent datasets, even if we encounter an error trying to
4027 * process one of them.
4028 */
4029/* ARGSUSED */
4030int
4031dsl_destroy_inconsistent(const char *dsname, void *arg)
4032{
4033 dsl_dataset_t *ds;
4034
4035 if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) {
4036 if (DS_IS_INCONSISTENT(ds))
4037 (void) dsl_dataset_destroy(ds, FTAG, B_FALSE);
4038 else
4039 dsl_dataset_disown(ds, FTAG);
4040 }
4041 return (0);
4042}
c28b2279
BB
4043
4044#if defined(_KERNEL) && defined(HAVE_SPL)
4045EXPORT_SYMBOL(dsl_dataset_hold);
4046EXPORT_SYMBOL(dsl_dataset_hold_obj);
4047EXPORT_SYMBOL(dsl_dataset_own);
4048EXPORT_SYMBOL(dsl_dataset_own_obj);
4049EXPORT_SYMBOL(dsl_dataset_name);
4050EXPORT_SYMBOL(dsl_dataset_rele);
4051EXPORT_SYMBOL(dsl_dataset_disown);
4052EXPORT_SYMBOL(dsl_dataset_drop_ref);
4053EXPORT_SYMBOL(dsl_dataset_tryown);
4054EXPORT_SYMBOL(dsl_dataset_make_exclusive);
4055EXPORT_SYMBOL(dsl_dataset_create_sync);
4056EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
4057EXPORT_SYMBOL(dsl_dataset_destroy);
4058EXPORT_SYMBOL(dsl_snapshots_destroy);
4059EXPORT_SYMBOL(dsl_dataset_destroy_check);
4060EXPORT_SYMBOL(dsl_dataset_destroy_sync);
4061EXPORT_SYMBOL(dsl_dataset_snapshot_check);
4062EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
4063EXPORT_SYMBOL(dsl_dataset_rename);
4064EXPORT_SYMBOL(dsl_dataset_promote);
4065EXPORT_SYMBOL(dsl_dataset_clone_swap);
4066EXPORT_SYMBOL(dsl_dataset_user_hold);
4067EXPORT_SYMBOL(dsl_dataset_user_release);
4068EXPORT_SYMBOL(dsl_dataset_user_release_tmp);
4069EXPORT_SYMBOL(dsl_dataset_get_holds);
4070EXPORT_SYMBOL(dsl_dataset_get_blkptr);
4071EXPORT_SYMBOL(dsl_dataset_set_blkptr);
4072EXPORT_SYMBOL(dsl_dataset_get_spa);
4073EXPORT_SYMBOL(dsl_dataset_modified_since_lastsnap);
4074EXPORT_SYMBOL(dsl_dataset_sync);
4075EXPORT_SYMBOL(dsl_dataset_block_born);
4076EXPORT_SYMBOL(dsl_dataset_block_kill);
4077EXPORT_SYMBOL(dsl_dataset_block_freeable);
4078EXPORT_SYMBOL(dsl_dataset_prev_snap_txg);
4079EXPORT_SYMBOL(dsl_dataset_dirty);
4080EXPORT_SYMBOL(dsl_dataset_stats);
4081EXPORT_SYMBOL(dsl_dataset_fast_stat);
4082EXPORT_SYMBOL(dsl_dataset_space);
4083EXPORT_SYMBOL(dsl_dataset_fsid_guid);
4084EXPORT_SYMBOL(dsl_dsobj_to_dsname);
4085EXPORT_SYMBOL(dsl_dataset_check_quota);
4086EXPORT_SYMBOL(dsl_dataset_set_quota);
4087EXPORT_SYMBOL(dsl_dataset_set_quota_sync);
4088EXPORT_SYMBOL(dsl_dataset_set_reservation);
4089EXPORT_SYMBOL(dsl_destroy_inconsistent);
4090#endif