1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
15 * Copyright (c) 2013 Steven Hartland. All rights reserved.
16 */
17
18 #include <sys/zfs_context.h>
19 #include <sys/dsl_userhold.h>
20 #include <sys/dsl_dataset.h>
21 #include <sys/dsl_destroy.h>
22 #include <sys/dsl_synctask.h>
23 #include <sys/dmu_tx.h>
24 #include <sys/zfs_onexit.h>
25 #include <sys/dsl_pool.h>
26 #include <sys/dsl_dir.h>
27 #include <sys/zfs_ioctl.h>
28 #include <sys/zap.h>
29
30 typedef struct dsl_dataset_user_hold_arg {
31 nvlist_t *dduha_holds;
32 nvlist_t *dduha_chkholds;
33 nvlist_t *dduha_errlist;
34 minor_t dduha_minor;
35 } dsl_dataset_user_hold_arg_t;
36
37 /*
38 * If you add new checks here, you may need to add additional checks to the
39 * "temporary" case in snapshot_check() in dmu_objset.c.
40 */
41 int
dsl_dataset_user_hold_check_one(dsl_dataset_t * ds,const char * htag,boolean_t temphold,dmu_tx_t * tx)42 dsl_dataset_user_hold_check_one(dsl_dataset_t *ds, const char *htag,
43 boolean_t temphold, dmu_tx_t *tx)
44 {
45 dsl_pool_t *dp = dmu_tx_pool(tx);
46 objset_t *mos = dp->dp_meta_objset;
47 int error = 0;
48
49 ASSERT(dsl_pool_config_held(dp));
50
51 if (strlen(htag) > MAXNAMELEN)
52 return (SET_ERROR(E2BIG));
53 /* Tempholds have a more restricted length */
54 if (temphold && strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
55 return (SET_ERROR(E2BIG));
56
57 /* tags must be unique (if ds already exists) */
58 if (ds != NULL && dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
59 uint64_t value;
60
61 error = zap_lookup(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
62 htag, 8, 1, &value);
63 if (error == 0)
64 error = SET_ERROR(EEXIST);
65 else if (error == ENOENT)
66 error = 0;
67 }
68
69 return (error);
70 }
71
72 static int
dsl_dataset_user_hold_check(void * arg,dmu_tx_t * tx)73 dsl_dataset_user_hold_check(void *arg, dmu_tx_t *tx)
74 {
75 dsl_dataset_user_hold_arg_t *dduha = arg;
76 dsl_pool_t *dp = dmu_tx_pool(tx);
77 nvlist_t *tmp_holds;
78
79 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS)
80 return (SET_ERROR(ENOTSUP));
81
82 if (!dmu_tx_is_syncing(tx))
83 return (0);
84
85 /*
86 * Ensure the list has no duplicates by copying name/values from
87 * non-unique dduha_holds to unique tmp_holds, and comparing counts.
88 */
89 tmp_holds = fnvlist_alloc();
90 for (nvpair_t *pair = nvlist_next_nvpair(dduha->dduha_holds, NULL);
91 pair != NULL; pair = nvlist_next_nvpair(dduha->dduha_holds, pair)) {
92 size_t len = strlen(nvpair_name(pair)) +
93 strlen(fnvpair_value_string(pair));
94 char *nameval = kmem_zalloc(len + 2, KM_SLEEP);
95 (void) strlcpy(nameval, nvpair_name(pair), len + 2);
96 (void) strlcat(nameval, "@", len + 2);
97 (void) strlcat(nameval, fnvpair_value_string(pair), len + 2);
98 fnvlist_add_string(tmp_holds, nameval, "");
99 kmem_free(nameval, len + 2);
100 }
101 size_t tmp_count = fnvlist_num_pairs(tmp_holds);
102 fnvlist_free(tmp_holds);
103 if (tmp_count != fnvlist_num_pairs(dduha->dduha_holds))
104 return (SET_ERROR(EEXIST));
105 for (nvpair_t *pair = nvlist_next_nvpair(dduha->dduha_holds, NULL);
106 pair != NULL; pair = nvlist_next_nvpair(dduha->dduha_holds, pair)) {
107 dsl_dataset_t *ds;
108 int error = 0;
109 const char *htag, *name;
110
111 /* must be a snapshot */
112 name = nvpair_name(pair);
113 if (strchr(name, '@') == NULL)
114 error = SET_ERROR(EINVAL);
115
116 if (error == 0)
117 error = nvpair_value_string(pair, &htag);
118
119 if (error == 0)
120 error = dsl_dataset_hold(dp, name, FTAG, &ds);
121
122 if (error == 0) {
123 error = dsl_dataset_user_hold_check_one(ds, htag,
124 dduha->dduha_minor != 0, tx);
125 dsl_dataset_rele(ds, FTAG);
126 }
127
128 if (error == 0) {
129 fnvlist_add_string(dduha->dduha_chkholds, name, htag);
130 } else {
131 /*
132 * We register ENOENT errors so they can be correctly
133 * reported if needed, such as when all holds fail.
134 */
135 fnvlist_add_int32(dduha->dduha_errlist, name, error);
136 if (error != ENOENT)
137 return (error);
138 }
139 }
140
141 return (0);
142 }
143
144
145 static void
dsl_dataset_user_hold_sync_one_impl(nvlist_t * tmpholds,dsl_dataset_t * ds,const char * htag,minor_t minor,uint64_t now,dmu_tx_t * tx)146 dsl_dataset_user_hold_sync_one_impl(nvlist_t *tmpholds, dsl_dataset_t *ds,
147 const char *htag, minor_t minor, uint64_t now, dmu_tx_t *tx)
148 {
149 dsl_pool_t *dp = ds->ds_dir->dd_pool;
150 objset_t *mos = dp->dp_meta_objset;
151 uint64_t zapobj;
152
153 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
154
155 if (dsl_dataset_phys(ds)->ds_userrefs_obj == 0) {
156 /*
157 * This is the first user hold for this dataset. Create
158 * the userrefs zap object.
159 */
160 dmu_buf_will_dirty(ds->ds_dbuf, tx);
161 zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj =
162 zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
163 } else {
164 zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj;
165 }
166 ds->ds_userrefs++;
167
168 VERIFY0(zap_add(mos, zapobj, htag, 8, 1, &now, tx));
169
170 if (minor != 0) {
171 char name[MAXNAMELEN];
172 nvlist_t *tags;
173
174 VERIFY0(dsl_pool_user_hold(dp, ds->ds_object,
175 htag, now, tx));
176 (void) snprintf(name, sizeof (name), "%llx",
177 (u_longlong_t)ds->ds_object);
178
179 if (nvlist_lookup_nvlist(tmpholds, name, &tags) != 0) {
180 tags = fnvlist_alloc();
181 fnvlist_add_boolean(tags, htag);
182 fnvlist_add_nvlist(tmpholds, name, tags);
183 fnvlist_free(tags);
184 } else {
185 fnvlist_add_boolean(tags, htag);
186 }
187 }
188
189 spa_history_log_internal_ds(ds, "hold", tx,
190 "tag=%s temp=%d refs=%llu",
191 htag, minor != 0, (u_longlong_t)ds->ds_userrefs);
192 }
193
194 typedef struct zfs_hold_cleanup_arg {
195 char zhca_spaname[ZFS_MAX_DATASET_NAME_LEN];
196 uint64_t zhca_spa_load_guid;
197 nvlist_t *zhca_holds;
198 } zfs_hold_cleanup_arg_t;
199
200 static void
dsl_dataset_user_release_onexit(void * arg)201 dsl_dataset_user_release_onexit(void *arg)
202 {
203 zfs_hold_cleanup_arg_t *ca = arg;
204 spa_t *spa;
205 int error;
206
207 error = spa_open(ca->zhca_spaname, &spa, FTAG);
208 if (error != 0) {
209 zfs_dbgmsg("couldn't release holds on pool=%s "
210 "because pool is no longer loaded",
211 ca->zhca_spaname);
212 return;
213 }
214 if (spa_load_guid(spa) != ca->zhca_spa_load_guid) {
215 zfs_dbgmsg("couldn't release holds on pool=%s "
216 "because pool is no longer loaded (guid doesn't match)",
217 ca->zhca_spaname);
218 spa_close(spa, FTAG);
219 return;
220 }
221
222 (void) dsl_dataset_user_release_tmp(spa_get_dsl(spa), ca->zhca_holds);
223 fnvlist_free(ca->zhca_holds);
224 kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t));
225 spa_close(spa, FTAG);
226 }
227
228 static void
dsl_onexit_hold_cleanup(spa_t * spa,nvlist_t * holds,minor_t minor)229 dsl_onexit_hold_cleanup(spa_t *spa, nvlist_t *holds, minor_t minor)
230 {
231 zfs_hold_cleanup_arg_t *ca;
232
233 if (minor == 0 || nvlist_empty(holds)) {
234 fnvlist_free(holds);
235 return;
236 }
237
238 ASSERT(spa != NULL);
239 ca = kmem_alloc(sizeof (*ca), KM_SLEEP);
240
241 (void) strlcpy(ca->zhca_spaname, spa_name(spa),
242 sizeof (ca->zhca_spaname));
243 ca->zhca_spa_load_guid = spa_load_guid(spa);
244 ca->zhca_holds = holds;
245 VERIFY0(zfs_onexit_add_cb(minor,
246 dsl_dataset_user_release_onexit, ca, NULL));
247 }
248
249 void
dsl_dataset_user_hold_sync_one(dsl_dataset_t * ds,const char * htag,minor_t minor,uint64_t now,dmu_tx_t * tx)250 dsl_dataset_user_hold_sync_one(dsl_dataset_t *ds, const char *htag,
251 minor_t minor, uint64_t now, dmu_tx_t *tx)
252 {
253 nvlist_t *tmpholds;
254
255 if (minor != 0)
256 tmpholds = fnvlist_alloc();
257 else
258 tmpholds = NULL;
259 dsl_dataset_user_hold_sync_one_impl(tmpholds, ds, htag, minor, now, tx);
260 dsl_onexit_hold_cleanup(dsl_dataset_get_spa(ds), tmpholds, minor);
261 }
262
263 static void
dsl_dataset_user_hold_sync(void * arg,dmu_tx_t * tx)264 dsl_dataset_user_hold_sync(void *arg, dmu_tx_t *tx)
265 {
266 dsl_dataset_user_hold_arg_t *dduha = arg;
267 dsl_pool_t *dp = dmu_tx_pool(tx);
268 nvlist_t *tmpholds;
269 uint64_t now = gethrestime_sec();
270
271 if (dduha->dduha_minor != 0)
272 tmpholds = fnvlist_alloc();
273 else
274 tmpholds = NULL;
275 for (nvpair_t *pair = nvlist_next_nvpair(dduha->dduha_chkholds, NULL);
276 pair != NULL;
277 pair = nvlist_next_nvpair(dduha->dduha_chkholds, pair)) {
278 dsl_dataset_t *ds;
279
280 VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
281 dsl_dataset_user_hold_sync_one_impl(tmpholds, ds,
282 fnvpair_value_string(pair), dduha->dduha_minor, now, tx);
283 dsl_dataset_rele(ds, FTAG);
284 }
285 dsl_onexit_hold_cleanup(dp->dp_spa, tmpholds, dduha->dduha_minor);
286 }
287
288 /*
289 * The full semantics of this function are described in the comment above
290 * lzc_hold().
291 *
292 * To summarize:
293 * holds is nvl of snapname -> holdname
294 * errlist will be filled in with snapname -> error
295 *
296 * The snapshots must all be in the same pool.
297 *
298 * Holds for snapshots that don't exist will be skipped.
299 *
300 * If none of the snapshots for requested holds exist then ENOENT will be
301 * returned.
302 *
303 * If cleanup_minor is not 0, the holds will be temporary, which will be cleaned
304 * up when the process exits.
305 *
306 * On success all the holds, for snapshots that existed, will be created and 0
307 * will be returned.
308 *
309 * On failure no holds will be created, the errlist will be filled in,
310 * and an errno will returned.
311 *
312 * In all cases the errlist will contain entries for holds where the snapshot
313 * didn't exist.
314 */
315 int
dsl_dataset_user_hold(nvlist_t * holds,minor_t cleanup_minor,nvlist_t * errlist)316 dsl_dataset_user_hold(nvlist_t *holds, minor_t cleanup_minor, nvlist_t *errlist)
317 {
318 dsl_dataset_user_hold_arg_t dduha;
319 nvpair_t *pair;
320 int ret;
321
322 pair = nvlist_next_nvpair(holds, NULL);
323 if (pair == NULL)
324 return (0);
325
326 dduha.dduha_holds = holds;
327 /* chkholds can have non-unique name */
328 VERIFY0(nvlist_alloc(&dduha.dduha_chkholds, 0, KM_SLEEP));
329 dduha.dduha_errlist = errlist;
330 dduha.dduha_minor = cleanup_minor;
331
332 ret = dsl_sync_task(nvpair_name(pair), dsl_dataset_user_hold_check,
333 dsl_dataset_user_hold_sync, &dduha,
334 fnvlist_num_pairs(holds), ZFS_SPACE_CHECK_RESERVED);
335 fnvlist_free(dduha.dduha_chkholds);
336
337 return (ret);
338 }
339
340 typedef int (dsl_holdfunc_t)(dsl_pool_t *dp, const char *name, const void *tag,
341 dsl_dataset_t **dsp);
342
343 typedef struct dsl_dataset_user_release_arg {
344 dsl_holdfunc_t *ddura_holdfunc;
345 nvlist_t *ddura_holds;
346 nvlist_t *ddura_todelete;
347 nvlist_t *ddura_errlist;
348 nvlist_t *ddura_chkholds;
349 } dsl_dataset_user_release_arg_t;
350
351 /* Place a dataset hold on the snapshot identified by passed dsobj string */
352 static int
dsl_dataset_hold_obj_string(dsl_pool_t * dp,const char * dsobj,const void * tag,dsl_dataset_t ** dsp)353 dsl_dataset_hold_obj_string(dsl_pool_t *dp, const char *dsobj, const void *tag,
354 dsl_dataset_t **dsp)
355 {
356 return (dsl_dataset_hold_obj(dp, zfs_strtonum(dsobj, NULL), tag, dsp));
357 }
358
359 static int
dsl_dataset_user_release_check_one(dsl_dataset_user_release_arg_t * ddura,dsl_dataset_t * ds,nvlist_t * holds,const char * snapname)360 dsl_dataset_user_release_check_one(dsl_dataset_user_release_arg_t *ddura,
361 dsl_dataset_t *ds, nvlist_t *holds, const char *snapname)
362 {
363 uint64_t zapobj;
364 nvlist_t *holds_found;
365 objset_t *mos;
366 int numholds;
367
368 if (!ds->ds_is_snapshot)
369 return (SET_ERROR(EINVAL));
370
371 if (nvlist_empty(holds))
372 return (0);
373
374 numholds = 0;
375 mos = ds->ds_dir->dd_pool->dp_meta_objset;
376 zapobj = dsl_dataset_phys(ds)->ds_userrefs_obj;
377 VERIFY0(nvlist_alloc(&holds_found, NV_UNIQUE_NAME, KM_SLEEP));
378
379 for (nvpair_t *pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
380 pair = nvlist_next_nvpair(holds, pair)) {
381 uint64_t tmp;
382 int error;
383 const char *holdname = nvpair_name(pair);
384
385 if (zapobj != 0)
386 error = zap_lookup(mos, zapobj, holdname, 8, 1, &tmp);
387 else
388 error = SET_ERROR(ENOENT);
389
390 /*
391 * Non-existent holds are put on the errlist, but don't
392 * cause an overall failure.
393 */
394 if (error == ENOENT) {
395 if (ddura->ddura_errlist != NULL) {
396 char *errtag = kmem_asprintf("%s#%s",
397 snapname, holdname);
398 fnvlist_add_int32(ddura->ddura_errlist, errtag,
399 ENOENT);
400 kmem_strfree(errtag);
401 }
402 continue;
403 }
404
405 if (error != 0) {
406 fnvlist_free(holds_found);
407 return (error);
408 }
409
410 fnvlist_add_boolean(holds_found, holdname);
411 numholds++;
412 }
413
414 if (DS_IS_DEFER_DESTROY(ds) &&
415 dsl_dataset_phys(ds)->ds_num_children == 1 &&
416 ds->ds_userrefs == numholds) {
417 /* we need to destroy the snapshot as well */
418 if (dsl_dataset_long_held(ds)) {
419 fnvlist_free(holds_found);
420 return (SET_ERROR(EBUSY));
421 }
422 fnvlist_add_boolean(ddura->ddura_todelete, snapname);
423 }
424
425 if (numholds != 0) {
426 fnvlist_add_nvlist(ddura->ddura_chkholds, snapname,
427 holds_found);
428 }
429 fnvlist_free(holds_found);
430
431 return (0);
432 }
433
434 static int
dsl_dataset_user_release_check(void * arg,dmu_tx_t * tx)435 dsl_dataset_user_release_check(void *arg, dmu_tx_t *tx)
436 {
437 dsl_dataset_user_release_arg_t *ddura;
438 dsl_holdfunc_t *holdfunc;
439 dsl_pool_t *dp;
440
441 if (!dmu_tx_is_syncing(tx))
442 return (0);
443
444 dp = dmu_tx_pool(tx);
445
446 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
447
448 ddura = arg;
449 holdfunc = ddura->ddura_holdfunc;
450
451 for (nvpair_t *pair = nvlist_next_nvpair(ddura->ddura_holds, NULL);
452 pair != NULL; pair = nvlist_next_nvpair(ddura->ddura_holds, pair)) {
453 int error;
454 dsl_dataset_t *ds;
455 nvlist_t *holds;
456 const char *snapname = nvpair_name(pair);
457
458 error = nvpair_value_nvlist(pair, &holds);
459 if (error != 0)
460 error = (SET_ERROR(EINVAL));
461 else
462 error = holdfunc(dp, snapname, FTAG, &ds);
463 if (error == 0) {
464 error = dsl_dataset_user_release_check_one(ddura, ds,
465 holds, snapname);
466 dsl_dataset_rele(ds, FTAG);
467 }
468 if (error != 0) {
469 if (ddura->ddura_errlist != NULL) {
470 fnvlist_add_int32(ddura->ddura_errlist,
471 snapname, error);
472 }
473 /*
474 * Non-existent snapshots are put on the errlist,
475 * but don't cause an overall failure.
476 */
477 if (error != ENOENT)
478 return (error);
479 }
480 }
481
482 return (0);
483 }
484
485 static void
dsl_dataset_user_release_sync_one(dsl_dataset_t * ds,nvlist_t * holds,dmu_tx_t * tx)486 dsl_dataset_user_release_sync_one(dsl_dataset_t *ds, nvlist_t *holds,
487 dmu_tx_t *tx)
488 {
489 dsl_pool_t *dp = ds->ds_dir->dd_pool;
490 objset_t *mos = dp->dp_meta_objset;
491
492 for (nvpair_t *pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
493 pair = nvlist_next_nvpair(holds, pair)) {
494 int error;
495 const char *holdname = nvpair_name(pair);
496
497 /* Remove temporary hold if one exists. */
498 error = dsl_pool_user_release(dp, ds->ds_object, holdname, tx);
499 VERIFY(error == 0 || error == ENOENT);
500
501 VERIFY0(zap_remove(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
502 holdname, tx));
503 ds->ds_userrefs--;
504
505 spa_history_log_internal_ds(ds, "release", tx,
506 "tag=%s refs=%lld", holdname, (longlong_t)ds->ds_userrefs);
507 }
508 }
509
510 static void
dsl_dataset_user_release_sync(void * arg,dmu_tx_t * tx)511 dsl_dataset_user_release_sync(void *arg, dmu_tx_t *tx)
512 {
513 dsl_dataset_user_release_arg_t *ddura = arg;
514 dsl_holdfunc_t *holdfunc = ddura->ddura_holdfunc;
515 dsl_pool_t *dp = dmu_tx_pool(tx);
516
517 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
518
519 for (nvpair_t *pair = nvlist_next_nvpair(ddura->ddura_chkholds, NULL);
520 pair != NULL; pair = nvlist_next_nvpair(ddura->ddura_chkholds,
521 pair)) {
522 dsl_dataset_t *ds;
523 const char *name = nvpair_name(pair);
524
525 VERIFY0(holdfunc(dp, name, FTAG, &ds));
526
527 dsl_dataset_user_release_sync_one(ds,
528 fnvpair_value_nvlist(pair), tx);
529 if (nvlist_exists(ddura->ddura_todelete, name)) {
530 ASSERT(ds->ds_userrefs == 0 &&
531 dsl_dataset_phys(ds)->ds_num_children == 1 &&
532 DS_IS_DEFER_DESTROY(ds));
533 dsl_destroy_snapshot_sync_impl(ds, B_FALSE, tx);
534 }
535 dsl_dataset_rele(ds, FTAG);
536 }
537 }
538
539 /*
540 * The full semantics of this function are described in the comment above
541 * lzc_release().
542 *
543 * To summarize:
544 * Releases holds specified in the nvl holds.
545 *
546 * holds is nvl of snapname -> { holdname, ... }
547 * errlist will be filled in with snapname -> error
548 *
549 * If tmpdp is not NULL the names for holds should be the dsobj's of snapshots,
550 * otherwise they should be the names of snapshots.
551 *
552 * As a release may cause snapshots to be destroyed this tries to ensure they
553 * aren't mounted.
554 *
555 * The release of non-existent holds are skipped.
556 *
557 * At least one hold must have been released for the this function to succeed
558 * and return 0.
559 */
560 static int
dsl_dataset_user_release_impl(nvlist_t * holds,nvlist_t * errlist,dsl_pool_t * tmpdp)561 dsl_dataset_user_release_impl(nvlist_t *holds, nvlist_t *errlist,
562 dsl_pool_t *tmpdp)
563 {
564 dsl_dataset_user_release_arg_t ddura;
565 nvpair_t *pair;
566 const char *pool;
567 int error;
568
569 pair = nvlist_next_nvpair(holds, NULL);
570 if (pair == NULL)
571 return (0);
572
573 /*
574 * The release may cause snapshots to be destroyed; make sure they
575 * are not mounted.
576 */
577 if (tmpdp != NULL) {
578 /* Temporary holds are specified by dsobj string. */
579 ddura.ddura_holdfunc = dsl_dataset_hold_obj_string;
580 pool = spa_name(tmpdp->dp_spa);
581 #ifdef _KERNEL
582 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
583 pair = nvlist_next_nvpair(holds, pair)) {
584 dsl_dataset_t *ds;
585
586 dsl_pool_config_enter(tmpdp, FTAG);
587 error = dsl_dataset_hold_obj_string(tmpdp,
588 nvpair_name(pair), FTAG, &ds);
589 if (error == 0) {
590 char name[ZFS_MAX_DATASET_NAME_LEN];
591 dsl_dataset_name(ds, name);
592 dsl_pool_config_exit(tmpdp, FTAG);
593 dsl_dataset_rele(ds, FTAG);
594 (void) zfs_unmount_snap(name);
595 } else {
596 dsl_pool_config_exit(tmpdp, FTAG);
597 }
598 }
599 #endif
600 } else {
601 /* Non-temporary holds are specified by name. */
602 ddura.ddura_holdfunc = dsl_dataset_hold;
603 pool = nvpair_name(pair);
604 #ifdef _KERNEL
605 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
606 pair = nvlist_next_nvpair(holds, pair)) {
607 (void) zfs_unmount_snap(nvpair_name(pair));
608 }
609 #endif
610 }
611
612 ddura.ddura_holds = holds;
613 ddura.ddura_errlist = errlist;
614 VERIFY0(nvlist_alloc(&ddura.ddura_todelete, NV_UNIQUE_NAME,
615 KM_SLEEP));
616 VERIFY0(nvlist_alloc(&ddura.ddura_chkholds, NV_UNIQUE_NAME,
617 KM_SLEEP));
618
619 error = dsl_sync_task(pool, dsl_dataset_user_release_check,
620 dsl_dataset_user_release_sync, &ddura, 0,
621 ZFS_SPACE_CHECK_EXTRA_RESERVED);
622 fnvlist_free(ddura.ddura_todelete);
623 fnvlist_free(ddura.ddura_chkholds);
624
625 return (error);
626 }
627
628 /*
629 * holds is nvl of snapname -> { holdname, ... }
630 * errlist will be filled in with snapname -> error
631 */
632 int
dsl_dataset_user_release(nvlist_t * holds,nvlist_t * errlist)633 dsl_dataset_user_release(nvlist_t *holds, nvlist_t *errlist)
634 {
635 return (dsl_dataset_user_release_impl(holds, errlist, NULL));
636 }
637
638 /*
639 * holds is nvl of snapdsobj -> { holdname, ... }
640 */
641 void
dsl_dataset_user_release_tmp(struct dsl_pool * dp,nvlist_t * holds)642 dsl_dataset_user_release_tmp(struct dsl_pool *dp, nvlist_t *holds)
643 {
644 ASSERT(dp != NULL);
645 (void) dsl_dataset_user_release_impl(holds, NULL, dp);
646 }
647
648 int
dsl_dataset_get_holds(const char * dsname,nvlist_t * nvl)649 dsl_dataset_get_holds(const char *dsname, nvlist_t *nvl)
650 {
651 dsl_pool_t *dp;
652 dsl_dataset_t *ds;
653 int err;
654
655 err = dsl_pool_hold(dsname, FTAG, &dp);
656 if (err != 0)
657 return (err);
658 err = dsl_dataset_hold(dp, dsname, FTAG, &ds);
659 if (err != 0) {
660 dsl_pool_rele(dp, FTAG);
661 return (err);
662 }
663
664 if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
665 zap_attribute_t *za;
666 zap_cursor_t zc;
667
668 za = zap_attribute_alloc();
669 for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
670 dsl_dataset_phys(ds)->ds_userrefs_obj);
671 zap_cursor_retrieve(&zc, za) == 0;
672 zap_cursor_advance(&zc)) {
673 fnvlist_add_uint64(nvl, za->za_name,
674 za->za_first_integer);
675 }
676 zap_cursor_fini(&zc);
677 zap_attribute_free(za);
678 }
679 dsl_dataset_rele(ds, FTAG);
680 dsl_pool_rele(dp, FTAG);
681 return (0);
682 }
683