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.
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.
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]
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include <libnvpair.h>
36 #include <sys/types.h>
41 #include <libbe_priv.h>
43 /* Private function prototypes */
44 static int be_rollback_check_callback(zfs_handle_t
*, void *);
45 static int be_rollback_callback(zfs_handle_t
*, void *);
48 /* ******************************************************************** */
49 /* Public Functions */
50 /* ******************************************************************** */
53 * Function: be_create_snapshot
54 * Description: Creates a recursive snapshot of all the datasets within a BE.
55 * If the name of the BE to snapshot is not provided, it assumes
56 * we're snapshotting the currently running BE. If the snapshot
57 * name is not provided it creates an auto named snapshot, which
58 * will be returned to the caller upon success.
60 * be_attrs - pointer to nvlist_t of attributes being passed in.
61 * The following attributes are used by this function:
63 * BE_ATTR_ORIG_BE_NAME *optional
64 * BE_ATTR_SNAP_NAME *optional
65 * BE_ATTR_POLICY *optional
67 * If the BE_ATTR_SNAP_NAME was not passed in, upon
68 * successful BE snapshot creation, the following
69 * attribute value will be returned to the caller by
70 * setting it in the be_attrs parameter passed in:
75 * BE_SUCCESS - Success
76 * be_errno_t - Failure
81 be_create_snapshot(nvlist_t
*be_attrs
)
84 char *snap_name
= NULL
;
86 boolean_t autoname
= B_FALSE
;
89 /* Initialize libzfs handle */
93 /* Get original BE name if one was provided */
94 if (nvlist_lookup_pairs(be_attrs
, NV_FLAG_NOENTOK
,
95 BE_ATTR_ORIG_BE_NAME
, DATA_TYPE_STRING
, &be_name
, NULL
) != 0) {
96 be_print_err(gettext("be_create_snapshot: failed to "
97 "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
99 return (BE_ERR_INVAL
);
102 /* Validate original BE name if one was provided */
103 if (be_name
!= NULL
&& !be_valid_be_name(be_name
)) {
104 be_print_err(gettext("be_create_snapshot: "
105 "invalid BE name %s\n"), be_name
);
107 return (BE_ERR_INVAL
);
110 /* Get snapshot name to create if one was provided */
111 if (nvlist_lookup_pairs(be_attrs
, NV_FLAG_NOENTOK
,
112 BE_ATTR_SNAP_NAME
, DATA_TYPE_STRING
, &snap_name
, NULL
) != 0) {
113 be_print_err(gettext("be_create_snapshot: "
114 "failed to lookup BE_ATTR_SNAP_NAME attribute\n"));
116 return (BE_ERR_INVAL
);
119 /* Get BE policy to create this snapshot under */
120 if (nvlist_lookup_pairs(be_attrs
, NV_FLAG_NOENTOK
,
121 BE_ATTR_POLICY
, DATA_TYPE_STRING
, &policy
, NULL
) != 0) {
122 be_print_err(gettext("be_create_snapshot: "
123 "failed to lookup BE_ATTR_POLICY attribute\n"));
125 return (BE_ERR_INVAL
);
129 * If no snap_name ws provided, we're going to create an
130 * auto named snapshot. Set flag so that we know to pass
131 * the auto named snapshot to the caller later.
133 if (snap_name
== NULL
)
136 if ((ret
= _be_create_snapshot(be_name
, &snap_name
, policy
))
138 if (autoname
== B_TRUE
) {
140 * Set auto named snapshot name in the
141 * nvlist passed in by the caller.
143 if (nvlist_add_string(be_attrs
, BE_ATTR_SNAP_NAME
,
145 be_print_err(gettext("be_create_snapshot: "
146 "failed to add auto snap name (%s) to "
147 "be_attrs\n"), snap_name
);
159 * Function: be_destroy_snapshot
160 * Description: Iterates through all the datasets of the BE and deletes
161 * the snapshots of each one with the specified name. If the
162 * BE name is not provided, it assumes we're operating on the
163 * currently running BE. The name of the snapshot name to
164 * destroy must be provided.
166 * be_attrs - pointer to nvlist_t of attributes being passed in.
167 * The following attribute values are used by this
170 * BE_ATTR_ORIG_BE_NAME *optional
171 * BE_ATTR_SNAP_NAME *required
173 * BE_SUCCESS - Success
174 * be_errno_t - Failure
179 be_destroy_snapshot(nvlist_t
*be_attrs
)
181 char *be_name
= NULL
;
182 char *snap_name
= NULL
;
183 int ret
= BE_SUCCESS
;
185 /* Initialize libzfs handle */
187 return (BE_ERR_INIT
);
189 /* Get original BE name if one was provided */
190 if (nvlist_lookup_pairs(be_attrs
, NV_FLAG_NOENTOK
,
191 BE_ATTR_ORIG_BE_NAME
, DATA_TYPE_STRING
, &be_name
, NULL
) != 0) {
192 be_print_err(gettext("be_destroy_snapshot: "
193 "failed to lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
194 return (BE_ERR_INVAL
);
197 /* Validate original BE name if one was provided */
198 if (be_name
!= NULL
&& !be_valid_be_name(be_name
)) {
199 be_print_err(gettext("be_destroy_snapshot: "
200 "invalid BE name %s\n"), be_name
);
201 return (BE_ERR_INVAL
);
204 /* Get snapshot name to destroy */
205 if (nvlist_lookup_string(be_attrs
, BE_ATTR_SNAP_NAME
, &snap_name
)
207 be_print_err(gettext("be_destroy_snapshot: "
208 "failed to lookup BE_ATTR_SNAP_NAME attribute.\n"));
209 return (BE_ERR_INVAL
);
212 ret
= _be_destroy_snapshot(be_name
, snap_name
);
220 * Function: be_rollback
221 * Description: Rolls back a BE and all of its children datasets to the
222 * named snapshot. All of the BE's datasets must have the
223 * named snapshot for this function to succeed. If the name
224 * of the BE is not passed in, this function assumes we're
225 * operating on the currently booted live BE.
227 * Note - This function does not check if the BE has any
228 * younger snapshots than the one we're trying to rollback to.
229 * If it does, then those younger snapshots and their dependent
230 * clone file systems will get destroyed in the process of
234 * be_attrs - pointer to nvlist_t of attributes being passed in.
235 * The following attributes are used by this function:
237 * BE_ATTR_ORIG_BE_NAME *optional
238 * BE_ATTR_SNAP_NAME *required
241 * BE_SUCCESS - Success
242 * be_errno_t - Failure
247 be_rollback(nvlist_t
*be_attrs
)
249 be_transaction_data_t bt
= { 0 };
250 zfs_handle_t
*zhp
= NULL
;
251 char obe_root_ds
[MAXPATHLEN
];
252 int zret
= 0, ret
= BE_SUCCESS
;
254 /* Initialize libzfs handle */
256 return (BE_ERR_INIT
);
258 /* Get original BE name if one was provided */
259 if (nvlist_lookup_pairs(be_attrs
, NV_FLAG_NOENTOK
,
260 BE_ATTR_ORIG_BE_NAME
, DATA_TYPE_STRING
, &bt
.obe_name
, NULL
) != 0) {
261 be_print_err(gettext("be_rollback: "
262 "failed to lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
263 return (BE_ERR_INVAL
);
266 /* If original BE name not provided, use current BE */
267 if (bt
.obe_name
== NULL
) {
268 if ((ret
= be_find_current_be(&bt
)) != BE_SUCCESS
) {
272 /* Validate original BE name */
273 if (!be_valid_be_name(bt
.obe_name
)) {
274 be_print_err(gettext("be_rollback: "
275 "invalid BE name %s\n"), bt
.obe_name
);
276 return (BE_ERR_INVAL
);
280 /* Get snapshot name to rollback to */
281 if (nvlist_lookup_string(be_attrs
, BE_ATTR_SNAP_NAME
, &bt
.obe_snap_name
)
283 be_print_err(gettext("be_rollback: "
284 "failed to lookup BE_ATTR_SNAP_NAME attribute.\n"));
285 return (BE_ERR_INVAL
);
288 /* Find which zpool obe_name lives in */
289 if ((zret
= zpool_iter(g_zfs
, be_find_zpool_callback
, &bt
)) == 0) {
290 be_print_err(gettext("be_rollback: "
291 "failed to find zpool for BE (%s)\n"), bt
.obe_name
);
292 return (BE_ERR_BE_NOENT
);
293 } else if (zret
< 0) {
294 be_print_err(gettext("be_rollback: "
295 "zpool_iter failed: %s\n"),
296 libzfs_error_description(g_zfs
));
297 return (zfs_err_to_be_err(g_zfs
));
300 /* Generate string for BE's root dataset */
301 be_make_root_ds(bt
.obe_zpool
, bt
.obe_name
, obe_root_ds
,
302 sizeof (obe_root_ds
));
303 bt
.obe_root_ds
= obe_root_ds
;
305 /* Get handle to BE's root dataset */
306 if ((zhp
= zfs_open(g_zfs
, bt
.obe_root_ds
, ZFS_TYPE_DATASET
)) == NULL
) {
307 be_print_err(gettext("be_rollback: "
308 "failed to open BE root dataset (%s): %s\n"),
309 bt
.obe_root_ds
, libzfs_error_description(g_zfs
));
310 return (zfs_err_to_be_err(g_zfs
));
314 * Check that snapshot name exists for this BE and all of its
315 * children file systems. This call will end up closing the
316 * zfs handle passed in whether it succeeds or fails.
318 if ((ret
= be_rollback_check_callback(zhp
, bt
.obe_snap_name
)) != 0) {
323 /* Get handle to BE's root dataset */
324 if ((zhp
= zfs_open(g_zfs
, bt
.obe_root_ds
, ZFS_TYPE_DATASET
)) == NULL
) {
325 be_print_err(gettext("be_rollback: "
326 "failed to open BE root dataset (%s): %s\n"),
327 bt
.obe_root_ds
, libzfs_error_description(g_zfs
));
328 return (zfs_err_to_be_err(g_zfs
));
332 * Iterate through a BE's datasets and roll them all back to
333 * the specified snapshot. This call will end up closing the
334 * zfs handle passed in whether it succeeds or fails.
336 if ((ret
= be_rollback_callback(zhp
, bt
.obe_snap_name
)) != 0) {
338 be_print_err(gettext("be_rollback: "
339 "failed to rollback BE %s to %s\n"), bt
.obe_name
,
349 /* ******************************************************************** */
350 /* Semi-Private Functions */
351 /* ******************************************************************** */
354 * Function: _be_create_snapshot
355 * Description: see be_create_snapshot
357 * be_name - The name of the BE that we're taking a snapshot of.
358 * snap_name - The name of the snapshot we're creating. If
359 * snap_name is NULL an auto generated name will be used,
360 * and upon success, will return that name via this
361 * reference pointer. The caller is responsible for
362 * freeing the returned name.
363 * policy - The clean-up policy type. (library wide use only)
365 * BE_SUCCESS - Success
366 * be_errno_t - Failure
368 * Semi-private (library wide use only)
371 _be_create_snapshot(char *be_name
, char **snap_name
, char *policy
)
373 be_transaction_data_t bt
= { 0 };
374 zfs_handle_t
*zhp
= NULL
;
375 nvlist_t
*ss_props
= NULL
;
377 char root_ds
[MAXPATHLEN
];
378 int pool_version
= 0;
380 int zret
= 0, ret
= BE_SUCCESS
;
381 boolean_t autoname
= B_FALSE
;
383 /* Set parameters in bt structure */
384 bt
.obe_name
= be_name
;
385 bt
.obe_snap_name
= *snap_name
;
388 /* If original BE name not supplied, use current BE */
389 if (bt
.obe_name
== NULL
) {
390 if ((ret
= be_find_current_be(&bt
)) != BE_SUCCESS
) {
395 /* Find which zpool obe_name lives in */
396 if ((zret
= zpool_iter(g_zfs
, be_find_zpool_callback
, &bt
)) == 0) {
397 be_print_err(gettext("be_create_snapshot: failed to "
398 "find zpool for BE (%s)\n"), bt
.obe_name
);
399 return (BE_ERR_BE_NOENT
);
400 } else if (zret
< 0) {
401 be_print_err(gettext("be_create_snapshot: "
402 "zpool_iter failed: %s\n"),
403 libzfs_error_description(g_zfs
));
404 return (zfs_err_to_be_err(g_zfs
));
407 be_make_root_ds(bt
.obe_zpool
, bt
.obe_name
, root_ds
,
409 bt
.obe_root_ds
= root_ds
;
411 /* If BE policy not specified, use the default policy */
412 if (bt
.policy
== NULL
) {
413 bt
.policy
= be_default_policy();
415 /* Validate policy type */
416 if (!valid_be_policy(bt
.policy
)) {
417 be_print_err(gettext("be_create_snapshot: "
418 "invalid BE policy type (%s)\n"), bt
.policy
);
419 return (BE_ERR_INVAL
);
424 * If snapshot name not specified, set auto name flag and
425 * generate auto snapshot name.
427 if (bt
.obe_snap_name
== NULL
) {
429 if ((bt
.obe_snap_name
= be_auto_snap_name())
431 be_print_err(gettext("be_create_snapshot: "
432 "failed to create auto snapshot name\n"));
433 ret
= BE_ERR_AUTONAME
;
438 /* Generate the name of the snapshot to take. */
439 (void) snprintf(ss
, sizeof (ss
), "%s@%s", bt
.obe_root_ds
,
442 /* Get handle to BE's root dataset */
443 if ((zhp
= zfs_open(g_zfs
, bt
.obe_root_ds
, ZFS_TYPE_DATASET
))
445 be_print_err(gettext("be_create_snapshot: "
446 "failed to open BE root dataset (%s): %s\n"),
447 bt
.obe_root_ds
, libzfs_error_description(g_zfs
));
448 ret
= zfs_err_to_be_err(g_zfs
);
452 /* Get the ZFS pool version of the pool where this dataset resides */
453 if (zfs_spa_version(zhp
, &pool_version
) != 0) {
454 be_print_err(gettext("be_create_snapshot: failed to "
455 "get ZFS pool version for %s: %s\n"), zfs_get_name(zhp
),
456 libzfs_error_description(g_zfs
));
460 * If ZFS pool version supports snapshot user properties, store
461 * cleanup policy there. Otherwise don't set one - this snapshot
462 * will always inherit the cleanup policy from its parent.
464 if (pool_version
>= SPA_VERSION_SNAP_PROPS
) {
465 if (nvlist_alloc(&ss_props
, NV_UNIQUE_NAME
, 0) != 0) {
466 be_print_err(gettext("be_create_snapshot: internal "
467 "error: out of memory\n"));
468 return (BE_ERR_NOMEM
);
470 if (nvlist_add_string(ss_props
, BE_POLICY_PROPERTY
, bt
.policy
)
472 be_print_err(gettext("be_create_snapshot: internal "
473 "error: out of memory\n"));
474 nvlist_free(ss_props
);
475 return (BE_ERR_NOMEM
);
477 } else if (policy
!= NULL
) {
479 * If an explicit cleanup policy was requested
480 * by the caller and we don't support it, error out.
482 be_print_err(gettext("be_create_snapshot: cannot set "
483 "cleanup policy: ZFS pool version is %d\n"), pool_version
);
484 return (BE_ERR_NOTSUP
);
487 /* Create the snapshots recursively */
488 if (zfs_snapshot(g_zfs
, ss
, B_TRUE
, ss_props
) != 0) {
489 if (!autoname
|| libzfs_errno(g_zfs
) != EZFS_EXISTS
) {
490 be_print_err(gettext("be_create_snapshot: "
491 "recursive snapshot of %s failed: %s\n"),
492 ss
, libzfs_error_description(g_zfs
));
494 if (libzfs_errno(g_zfs
) == EZFS_EXISTS
)
495 ret
= BE_ERR_SS_EXISTS
;
497 ret
= zfs_err_to_be_err(g_zfs
);
501 for (i
= 1; i
< BE_AUTO_NAME_MAX_TRY
; i
++) {
503 /* Sleep 1 before retrying */
506 /* Generate new auto snapshot name. */
507 free(bt
.obe_snap_name
);
508 if ((bt
.obe_snap_name
=
509 be_auto_snap_name()) == NULL
) {
510 be_print_err(gettext(
511 "be_create_snapshot: failed to "
512 "create auto snapshot name\n"));
513 ret
= BE_ERR_AUTONAME
;
517 /* Generate string of the snapshot to take. */
518 (void) snprintf(ss
, sizeof (ss
), "%s@%s",
519 bt
.obe_root_ds
, bt
.obe_snap_name
);
521 /* Create the snapshots recursively */
522 if (zfs_snapshot(g_zfs
, ss
, B_TRUE
, ss_props
)
524 if (libzfs_errno(g_zfs
) !=
526 be_print_err(gettext(
527 "be_create_snapshot: "
528 "recursive snapshot of %s "
530 libzfs_error_description(
532 ret
= zfs_err_to_be_err(g_zfs
);
541 * If we exhausted the maximum number of tries,
542 * free the auto snap name and set error.
544 if (i
== BE_AUTO_NAME_MAX_TRY
) {
545 be_print_err(gettext("be_create_snapshot: "
546 "failed to create unique auto snapshot "
548 free(bt
.obe_snap_name
);
549 bt
.obe_snap_name
= NULL
;
550 ret
= BE_ERR_AUTONAME
;
556 * If we succeeded in creating an auto named snapshot, store
557 * the name in the nvlist passed in by the caller.
559 if (autoname
&& bt
.obe_snap_name
) {
560 *snap_name
= bt
.obe_snap_name
;
566 if (ss_props
!= NULL
)
567 nvlist_free(ss_props
);
573 * Function: _be_destroy_snapshot
574 * Description: see be_destroy_snapshot
576 * be_name - The name of the BE that the snapshot belongs to.
577 * snap_name - The name of the snapshot we're destroying.
579 * BE_SUCCESS - Success
580 * be_errno_t - Failure
582 * Semi-private (library wide use only)
585 _be_destroy_snapshot(char *be_name
, char *snap_name
)
587 be_transaction_data_t bt
= { 0 };
590 char root_ds
[MAXPATHLEN
];
591 int err
= BE_SUCCESS
, ret
= BE_SUCCESS
;
593 /* Make sure we actaully have a snapshot name */
594 if (snap_name
== NULL
) {
595 be_print_err(gettext("be_destroy_snapshot: "
596 "invalid snapshot name\n"));
597 return (BE_ERR_INVAL
);
600 /* Set parameters in bt structure */
601 bt
.obe_name
= be_name
;
602 bt
.obe_snap_name
= snap_name
;
604 /* If original BE name not supplied, use current BE */
605 if (bt
.obe_name
== NULL
) {
606 if ((err
= be_find_current_be(&bt
)) != BE_SUCCESS
) {
611 /* Find which zpool be_name lives in */
612 if ((ret
= zpool_iter(g_zfs
, be_find_zpool_callback
, &bt
)) == 0) {
613 be_print_err(gettext("be_destroy_snapshot: "
614 "failed to find zpool for BE (%s)\n"), bt
.obe_name
);
615 return (BE_ERR_BE_NOENT
);
616 } else if (ret
< 0) {
617 be_print_err(gettext("be_destroy_snapshot: "
618 "zpool_iter failed: %s\n"),
619 libzfs_error_description(g_zfs
));
620 return (zfs_err_to_be_err(g_zfs
));
623 be_make_root_ds(bt
.obe_zpool
, bt
.obe_name
, root_ds
,
625 bt
.obe_root_ds
= root_ds
;
627 zhp
= zfs_open(g_zfs
, bt
.obe_root_ds
, ZFS_TYPE_DATASET
);
630 * The zfs_open failed, return an error.
632 be_print_err(gettext("be_destroy_snapshot: "
633 "failed to open BE root dataset (%s): %s\n"),
634 bt
.obe_root_ds
, libzfs_error_description(g_zfs
));
635 err
= zfs_err_to_be_err(g_zfs
);
638 * Generate the name of the snapshot to take.
640 (void) snprintf(ss
, sizeof (ss
), "%s@%s", bt
.obe_name
,
644 * destroy the snapshot.
647 * The boolean set to B_FALSE and passed to zfs_destroy_snaps()
648 * tells zfs to process and destroy the snapshots now.
649 * Otherwise the call will potentially return where the
650 * snapshot isn't actually destroyed yet, and ZFS is waiting
651 * until all the references to the snapshot have been
652 * released before actually destroying the snapshot.
654 if (zfs_destroy_snaps(zhp
, bt
.obe_snap_name
, B_FALSE
) != 0) {
655 err
= zfs_err_to_be_err(g_zfs
);
656 be_print_err(gettext("be_destroy_snapshot: "
657 "failed to destroy snapshot %s: %s\n"), ss
,
658 libzfs_error_description(g_zfs
));
667 /* ******************************************************************** */
668 /* Private Functions */
669 /* ******************************************************************** */
672 * Function: be_rollback_check_callback
673 * Description: Callback function used to iterate through a BE's filesystems
674 * to check if a given snapshot name exists.
676 * zhp - zfs_handle_t pointer to filesystem being processed.
677 * data - name of the snapshot to check for.
679 * 0 - Success, snapshot name exists for all filesystems.
680 * be_errno_t - Failure, snapshot name does not exist for all
686 be_rollback_check_callback(zfs_handle_t
*zhp
, void *data
)
688 char *snap_name
= data
;
690 int ret
= BE_SUCCESS
;
692 /* Generate string for this filesystem's snapshot name */
693 (void) snprintf(ss
, sizeof (ss
), "%s@%s", zfs_get_name(zhp
), snap_name
);
695 /* Check if snapshot exists */
696 if (!zfs_dataset_exists(g_zfs
, ss
, ZFS_TYPE_SNAPSHOT
)) {
697 be_print_err(gettext("be_rollback_check_callback: "
698 "snapshot does not exist %s\n"), ss
);
700 return (BE_ERR_SS_NOENT
);
703 /* Iterate this dataset's children and check them */
704 if ((ret
= zfs_iter_filesystems(zhp
, be_rollback_check_callback
,
715 * Function: be_rollback_callback
716 * Description: Callback function used to iterate through a BE's filesystems
717 * and roll them all back to the specified snapshot name.
719 * zhp - zfs_handle_t pointer to filesystem being processed.
720 * data - name of snapshot to rollback to.
723 * be_errno_t - Failure
728 be_rollback_callback(zfs_handle_t
*zhp
, void *data
)
730 zfs_handle_t
*zhp_snap
= NULL
;
731 char *snap_name
= data
;
735 /* Generate string for this filesystem's snapshot name */
736 (void) snprintf(ss
, sizeof (ss
), "%s@%s", zfs_get_name(zhp
), snap_name
);
738 /* Get handle to this filesystem's snapshot */
739 if ((zhp_snap
= zfs_open(g_zfs
, ss
, ZFS_TYPE_SNAPSHOT
)) == NULL
) {
740 be_print_err(gettext("be_rollback_callback: "
741 "failed to open snapshot %s: %s\n"), zfs_get_name(zhp
),
742 libzfs_error_description(g_zfs
));
743 ret
= zfs_err_to_be_err(g_zfs
);
748 /* Rollback dataset */
749 if (zfs_rollback(zhp
, zhp_snap
, B_FALSE
) != 0) {
750 be_print_err(gettext("be_rollback_callback: "
751 "failed to rollback BE dataset %s to snapshot %s: %s\n"),
752 zfs_get_name(zhp
), ss
, libzfs_error_description(g_zfs
));
753 ret
= zfs_err_to_be_err(g_zfs
);
760 /* Iterate this dataset's children and roll them back */
761 if ((ret
= zfs_iter_filesystems(zhp
, be_rollback_callback
,