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