1f169c0eaSGlenn Lagasse /*
2f169c0eaSGlenn Lagasse * CDDL HEADER START
3f169c0eaSGlenn Lagasse *
4f169c0eaSGlenn Lagasse * The contents of this file are subject to the terms of the
5f169c0eaSGlenn Lagasse * Common Development and Distribution License (the "License").
6f169c0eaSGlenn Lagasse * You may not use this file except in compliance with the License.
7f169c0eaSGlenn Lagasse *
8f169c0eaSGlenn Lagasse * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f169c0eaSGlenn Lagasse * or http://www.opensolaris.org/os/licensing.
10f169c0eaSGlenn Lagasse * See the License for the specific language governing permissions
11f169c0eaSGlenn Lagasse * and limitations under the License.
12f169c0eaSGlenn Lagasse *
13f169c0eaSGlenn Lagasse * When distributing Covered Code, include this CDDL HEADER in each
14f169c0eaSGlenn Lagasse * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f169c0eaSGlenn Lagasse * If applicable, add the following below this CDDL HEADER, with the
16f169c0eaSGlenn Lagasse * fields enclosed by brackets "[]" replaced with your own identifying
17f169c0eaSGlenn Lagasse * information: Portions Copyright [yyyy] [name of copyright owner]
18f169c0eaSGlenn Lagasse *
19f169c0eaSGlenn Lagasse * CDDL HEADER END
20f169c0eaSGlenn Lagasse */
21f169c0eaSGlenn Lagasse
22f169c0eaSGlenn Lagasse /*
23f169c0eaSGlenn Lagasse * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24f169c0eaSGlenn Lagasse */
25f169c0eaSGlenn Lagasse
26de1ab35cSAlexander Eremin /*
277e0e2549SAlexander Eremin * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
28*ec8422d0SAndy Fiddaman * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
29de1ab35cSAlexander Eremin */
30de1ab35cSAlexander Eremin
31f169c0eaSGlenn Lagasse /*
32f169c0eaSGlenn Lagasse * System includes
33f169c0eaSGlenn Lagasse */
34f169c0eaSGlenn Lagasse #include <assert.h>
35f169c0eaSGlenn Lagasse #include <libintl.h>
36f169c0eaSGlenn Lagasse #include <libnvpair.h>
37f169c0eaSGlenn Lagasse #include <libzfs.h>
38f169c0eaSGlenn Lagasse #include <stdio.h>
39f169c0eaSGlenn Lagasse #include <stdlib.h>
40f169c0eaSGlenn Lagasse #include <string.h>
41f169c0eaSGlenn Lagasse #include <sys/types.h>
42f169c0eaSGlenn Lagasse #include <sys/stat.h>
43f169c0eaSGlenn Lagasse #include <unistd.h>
44f169c0eaSGlenn Lagasse
45f169c0eaSGlenn Lagasse #include <libbe.h>
46f169c0eaSGlenn Lagasse #include <libbe_priv.h>
47f169c0eaSGlenn Lagasse
48f169c0eaSGlenn Lagasse /* Private function prototypes */
49f169c0eaSGlenn Lagasse static int be_rollback_check_callback(zfs_handle_t *, void *);
50f169c0eaSGlenn Lagasse static int be_rollback_callback(zfs_handle_t *, void *);
51f169c0eaSGlenn Lagasse
52f169c0eaSGlenn Lagasse
53f169c0eaSGlenn Lagasse /* ******************************************************************** */
54f169c0eaSGlenn Lagasse /* Public Functions */
55f169c0eaSGlenn Lagasse /* ******************************************************************** */
56f169c0eaSGlenn Lagasse
57f169c0eaSGlenn Lagasse /*
58f169c0eaSGlenn Lagasse * Function: be_create_snapshot
59f169c0eaSGlenn Lagasse * Description: Creates a recursive snapshot of all the datasets within a BE.
60f169c0eaSGlenn Lagasse * If the name of the BE to snapshot is not provided, it assumes
61f169c0eaSGlenn Lagasse * we're snapshotting the currently running BE. If the snapshot
62f169c0eaSGlenn Lagasse * name is not provided it creates an auto named snapshot, which
63f169c0eaSGlenn Lagasse * will be returned to the caller upon success.
64f169c0eaSGlenn Lagasse * Parameters:
65f169c0eaSGlenn Lagasse * be_attrs - pointer to nvlist_t of attributes being passed in.
66f169c0eaSGlenn Lagasse * The following attributes are used by this function:
67f169c0eaSGlenn Lagasse *
68f169c0eaSGlenn Lagasse * BE_ATTR_ORIG_BE_NAME *optional
69f169c0eaSGlenn Lagasse * BE_ATTR_SNAP_NAME *optional
70f169c0eaSGlenn Lagasse * BE_ATTR_POLICY *optional
71f169c0eaSGlenn Lagasse *
72f169c0eaSGlenn Lagasse * If the BE_ATTR_SNAP_NAME was not passed in, upon
73f169c0eaSGlenn Lagasse * successful BE snapshot creation, the following
74f169c0eaSGlenn Lagasse * attribute value will be returned to the caller by
75f169c0eaSGlenn Lagasse * setting it in the be_attrs parameter passed in:
76f169c0eaSGlenn Lagasse *
77f169c0eaSGlenn Lagasse * BE_ATTR_SNAP_NAME
78f169c0eaSGlenn Lagasse *
79f169c0eaSGlenn Lagasse * Return:
80f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
81f169c0eaSGlenn Lagasse * be_errno_t - Failure
82f169c0eaSGlenn Lagasse * Scope:
83f169c0eaSGlenn Lagasse * Public
84f169c0eaSGlenn Lagasse */
85f169c0eaSGlenn Lagasse int
be_create_snapshot(nvlist_t * be_attrs)86f169c0eaSGlenn Lagasse be_create_snapshot(nvlist_t *be_attrs)
87f169c0eaSGlenn Lagasse {
88f169c0eaSGlenn Lagasse char *be_name = NULL;
89f169c0eaSGlenn Lagasse char *snap_name = NULL;
90f169c0eaSGlenn Lagasse char *policy = NULL;
91f169c0eaSGlenn Lagasse boolean_t autoname = B_FALSE;
92*ec8422d0SAndy Fiddaman int ret = BE_SUCCESS;
93f169c0eaSGlenn Lagasse
94f169c0eaSGlenn Lagasse /* Initialize libzfs handle */
95f169c0eaSGlenn Lagasse if (!be_zfs_init())
96f169c0eaSGlenn Lagasse return (BE_ERR_INIT);
97f169c0eaSGlenn Lagasse
98f169c0eaSGlenn Lagasse /* Get original BE name if one was provided */
99f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
100f169c0eaSGlenn Lagasse BE_ATTR_ORIG_BE_NAME, DATA_TYPE_STRING, &be_name, NULL) != 0) {
101f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: failed to "
102f169c0eaSGlenn Lagasse "lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
103f169c0eaSGlenn Lagasse be_zfs_fini();
104f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
105f169c0eaSGlenn Lagasse }
106f169c0eaSGlenn Lagasse
107f169c0eaSGlenn Lagasse /* Validate original BE name if one was provided */
108f169c0eaSGlenn Lagasse if (be_name != NULL && !be_valid_be_name(be_name)) {
109f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
110f169c0eaSGlenn Lagasse "invalid BE name %s\n"), be_name);
111f169c0eaSGlenn Lagasse be_zfs_fini();
112f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
113f169c0eaSGlenn Lagasse }
114f169c0eaSGlenn Lagasse
115f169c0eaSGlenn Lagasse /* Get snapshot name to create if one was provided */
116f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
117f169c0eaSGlenn Lagasse BE_ATTR_SNAP_NAME, DATA_TYPE_STRING, &snap_name, NULL) != 0) {
118f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
119f169c0eaSGlenn Lagasse "failed to lookup BE_ATTR_SNAP_NAME attribute\n"));
120f169c0eaSGlenn Lagasse be_zfs_fini();
121f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
122f169c0eaSGlenn Lagasse }
123f169c0eaSGlenn Lagasse
124f169c0eaSGlenn Lagasse /* Get BE policy to create this snapshot under */
125f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
126f169c0eaSGlenn Lagasse BE_ATTR_POLICY, DATA_TYPE_STRING, &policy, NULL) != 0) {
127f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
128f169c0eaSGlenn Lagasse "failed to lookup BE_ATTR_POLICY attribute\n"));
129f169c0eaSGlenn Lagasse be_zfs_fini();
130f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
131f169c0eaSGlenn Lagasse }
132f169c0eaSGlenn Lagasse
133f169c0eaSGlenn Lagasse /*
134f169c0eaSGlenn Lagasse * If no snap_name ws provided, we're going to create an
135f169c0eaSGlenn Lagasse * auto named snapshot. Set flag so that we know to pass
136f169c0eaSGlenn Lagasse * the auto named snapshot to the caller later.
137f169c0eaSGlenn Lagasse */
138f169c0eaSGlenn Lagasse if (snap_name == NULL)
139f169c0eaSGlenn Lagasse autoname = B_TRUE;
140f169c0eaSGlenn Lagasse
141f169c0eaSGlenn Lagasse if ((ret = _be_create_snapshot(be_name, &snap_name, policy))
142f169c0eaSGlenn Lagasse == BE_SUCCESS) {
143f169c0eaSGlenn Lagasse if (autoname == B_TRUE) {
144f169c0eaSGlenn Lagasse /*
145f169c0eaSGlenn Lagasse * Set auto named snapshot name in the
146f169c0eaSGlenn Lagasse * nvlist passed in by the caller.
147f169c0eaSGlenn Lagasse */
148f169c0eaSGlenn Lagasse if (nvlist_add_string(be_attrs, BE_ATTR_SNAP_NAME,
149f169c0eaSGlenn Lagasse snap_name) != 0) {
150f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
151f169c0eaSGlenn Lagasse "failed to add auto snap name (%s) to "
152f169c0eaSGlenn Lagasse "be_attrs\n"), snap_name);
153f169c0eaSGlenn Lagasse ret = BE_ERR_NOMEM;
154f169c0eaSGlenn Lagasse }
155f169c0eaSGlenn Lagasse }
156f169c0eaSGlenn Lagasse }
157f169c0eaSGlenn Lagasse
158f169c0eaSGlenn Lagasse be_zfs_fini();
159f169c0eaSGlenn Lagasse
160f169c0eaSGlenn Lagasse return (ret);
161f169c0eaSGlenn Lagasse }
162f169c0eaSGlenn Lagasse
163f169c0eaSGlenn Lagasse /*
164f169c0eaSGlenn Lagasse * Function: be_destroy_snapshot
165f169c0eaSGlenn Lagasse * Description: Iterates through all the datasets of the BE and deletes
166f169c0eaSGlenn Lagasse * the snapshots of each one with the specified name. If the
167f169c0eaSGlenn Lagasse * BE name is not provided, it assumes we're operating on the
168f169c0eaSGlenn Lagasse * currently running BE. The name of the snapshot name to
169f169c0eaSGlenn Lagasse * destroy must be provided.
170f169c0eaSGlenn Lagasse * Parameters:
171f169c0eaSGlenn Lagasse * be_attrs - pointer to nvlist_t of attributes being passed in.
172f169c0eaSGlenn Lagasse * The following attribute values are used by this
173f169c0eaSGlenn Lagasse * function:
174f169c0eaSGlenn Lagasse *
175f169c0eaSGlenn Lagasse * BE_ATTR_ORIG_BE_NAME *optional
176f169c0eaSGlenn Lagasse * BE_ATTR_SNAP_NAME *required
177f169c0eaSGlenn Lagasse * Return:
178f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
179f169c0eaSGlenn Lagasse * be_errno_t - Failure
180f169c0eaSGlenn Lagasse * Scope:
181f169c0eaSGlenn Lagasse * Public
182f169c0eaSGlenn Lagasse */
183f169c0eaSGlenn Lagasse int
be_destroy_snapshot(nvlist_t * be_attrs)184f169c0eaSGlenn Lagasse be_destroy_snapshot(nvlist_t *be_attrs)
185f169c0eaSGlenn Lagasse {
186f169c0eaSGlenn Lagasse char *be_name = NULL;
187f169c0eaSGlenn Lagasse char *snap_name = NULL;
188*ec8422d0SAndy Fiddaman int ret = BE_SUCCESS;
189f169c0eaSGlenn Lagasse
190f169c0eaSGlenn Lagasse /* Initialize libzfs handle */
191f169c0eaSGlenn Lagasse if (!be_zfs_init())
192f169c0eaSGlenn Lagasse return (BE_ERR_INIT);
193f169c0eaSGlenn Lagasse
194f169c0eaSGlenn Lagasse /* Get original BE name if one was provided */
195f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
196f169c0eaSGlenn Lagasse BE_ATTR_ORIG_BE_NAME, DATA_TYPE_STRING, &be_name, NULL) != 0) {
197f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
198f169c0eaSGlenn Lagasse "failed to lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
199f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
200f169c0eaSGlenn Lagasse }
201f169c0eaSGlenn Lagasse
202f169c0eaSGlenn Lagasse /* Validate original BE name if one was provided */
203f169c0eaSGlenn Lagasse if (be_name != NULL && !be_valid_be_name(be_name)) {
204f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
205f169c0eaSGlenn Lagasse "invalid BE name %s\n"), be_name);
206f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
207f169c0eaSGlenn Lagasse }
208f169c0eaSGlenn Lagasse
209f169c0eaSGlenn Lagasse /* Get snapshot name to destroy */
210f169c0eaSGlenn Lagasse if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME, &snap_name)
211f169c0eaSGlenn Lagasse != 0) {
212f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
213f169c0eaSGlenn Lagasse "failed to lookup BE_ATTR_SNAP_NAME attribute.\n"));
214f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
215f169c0eaSGlenn Lagasse }
216f169c0eaSGlenn Lagasse
217f169c0eaSGlenn Lagasse ret = _be_destroy_snapshot(be_name, snap_name);
218f169c0eaSGlenn Lagasse
219f169c0eaSGlenn Lagasse be_zfs_fini();
220f169c0eaSGlenn Lagasse
221f169c0eaSGlenn Lagasse return (ret);
222f169c0eaSGlenn Lagasse }
223f169c0eaSGlenn Lagasse
224f169c0eaSGlenn Lagasse /*
225f169c0eaSGlenn Lagasse * Function: be_rollback
226f169c0eaSGlenn Lagasse * Description: Rolls back a BE and all of its children datasets to the
227f169c0eaSGlenn Lagasse * named snapshot. All of the BE's datasets must have the
228f169c0eaSGlenn Lagasse * named snapshot for this function to succeed. If the name
229f169c0eaSGlenn Lagasse * of the BE is not passed in, this function assumes we're
230f169c0eaSGlenn Lagasse * operating on the currently booted live BE.
231f169c0eaSGlenn Lagasse *
232f169c0eaSGlenn Lagasse * Note - This function does not check if the BE has any
233f169c0eaSGlenn Lagasse * younger snapshots than the one we're trying to rollback to.
234f169c0eaSGlenn Lagasse * If it does, then those younger snapshots and their dependent
235f169c0eaSGlenn Lagasse * clone file systems will get destroyed in the process of
236f169c0eaSGlenn Lagasse * rolling back.
237f169c0eaSGlenn Lagasse *
238f169c0eaSGlenn Lagasse * Parameters:
239f169c0eaSGlenn Lagasse * be_attrs - pointer to nvlist_t of attributes being passed in.
240f169c0eaSGlenn Lagasse * The following attributes are used by this function:
241f169c0eaSGlenn Lagasse *
242f169c0eaSGlenn Lagasse * BE_ATTR_ORIG_BE_NAME *optional
243f169c0eaSGlenn Lagasse * BE_ATTR_SNAP_NAME *required
244f169c0eaSGlenn Lagasse *
245f169c0eaSGlenn Lagasse * Returns:
246f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
247f169c0eaSGlenn Lagasse * be_errno_t - Failure
248f169c0eaSGlenn Lagasse * Scope:
249f169c0eaSGlenn Lagasse * Public
250f169c0eaSGlenn Lagasse */
251f169c0eaSGlenn Lagasse int
be_rollback(nvlist_t * be_attrs)252f169c0eaSGlenn Lagasse be_rollback(nvlist_t *be_attrs)
253f169c0eaSGlenn Lagasse {
254f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 };
255f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL;
256de1ab35cSAlexander Eremin zpool_handle_t *zphp;
257f169c0eaSGlenn Lagasse char obe_root_ds[MAXPATHLEN];
258de1ab35cSAlexander Eremin char *obe_name = NULL;
259f169c0eaSGlenn Lagasse int zret = 0, ret = BE_SUCCESS;
260de1ab35cSAlexander Eremin struct be_defaults be_defaults;
261f169c0eaSGlenn Lagasse
262f169c0eaSGlenn Lagasse /* Initialize libzfs handle */
263f169c0eaSGlenn Lagasse if (!be_zfs_init())
264f169c0eaSGlenn Lagasse return (BE_ERR_INIT);
265f169c0eaSGlenn Lagasse
266de1ab35cSAlexander Eremin if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
267de1ab35cSAlexander Eremin return (ret);
268de1ab35cSAlexander Eremin }
269de1ab35cSAlexander Eremin
270f169c0eaSGlenn Lagasse /* Get original BE name if one was provided */
271f169c0eaSGlenn Lagasse if (nvlist_lookup_pairs(be_attrs, NV_FLAG_NOENTOK,
272de1ab35cSAlexander Eremin BE_ATTR_ORIG_BE_NAME, DATA_TYPE_STRING, &obe_name, NULL) != 0) {
273f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback: "
274f169c0eaSGlenn Lagasse "failed to lookup BE_ATTR_ORIG_BE_NAME attribute\n"));
275f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
276f169c0eaSGlenn Lagasse }
277f169c0eaSGlenn Lagasse
278de1ab35cSAlexander Eremin be_get_defaults(&be_defaults);
279de1ab35cSAlexander Eremin
280f169c0eaSGlenn Lagasse /* If original BE name not provided, use current BE */
281de1ab35cSAlexander Eremin if (obe_name != NULL) {
282de1ab35cSAlexander Eremin bt.obe_name = obe_name;
283f169c0eaSGlenn Lagasse /* Validate original BE name */
284f169c0eaSGlenn Lagasse if (!be_valid_be_name(bt.obe_name)) {
285f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback: "
286f169c0eaSGlenn Lagasse "invalid BE name %s\n"), bt.obe_name);
287f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
288f169c0eaSGlenn Lagasse }
289f169c0eaSGlenn Lagasse }
290f169c0eaSGlenn Lagasse
291f169c0eaSGlenn Lagasse /* Get snapshot name to rollback to */
292f169c0eaSGlenn Lagasse if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME, &bt.obe_snap_name)
293f169c0eaSGlenn Lagasse != 0) {
294f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback: "
295f169c0eaSGlenn Lagasse "failed to lookup BE_ATTR_SNAP_NAME attribute.\n"));
296f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
297f169c0eaSGlenn Lagasse }
298f169c0eaSGlenn Lagasse
299de1ab35cSAlexander Eremin if (be_defaults.be_deflt_rpool_container) {
300de1ab35cSAlexander Eremin if ((zphp = zpool_open(g_zfs, bt.obe_zpool)) == NULL) {
301de1ab35cSAlexander Eremin be_print_err(gettext("be_rollback: failed to "
302de1ab35cSAlexander Eremin "open rpool (%s): %s\n"), bt.obe_zpool,
303de1ab35cSAlexander Eremin libzfs_error_description(g_zfs));
304de1ab35cSAlexander Eremin return (zfs_err_to_be_err(g_zfs));
305de1ab35cSAlexander Eremin }
306de1ab35cSAlexander Eremin zret = be_find_zpool_callback(zphp, &bt);
307de1ab35cSAlexander Eremin } else {
308de1ab35cSAlexander Eremin /* Find which zpool obe_name lives in */
309de1ab35cSAlexander Eremin if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) ==
310de1ab35cSAlexander Eremin 0) {
311de1ab35cSAlexander Eremin be_print_err(gettext("be_rollback: "
312de1ab35cSAlexander Eremin "failed to find zpool for BE (%s)\n"), bt.obe_name);
313de1ab35cSAlexander Eremin return (BE_ERR_BE_NOENT);
314de1ab35cSAlexander Eremin } else if (zret < 0) {
315de1ab35cSAlexander Eremin be_print_err(gettext("be_rollback: "
316de1ab35cSAlexander Eremin "zpool_iter failed: %s\n"),
317de1ab35cSAlexander Eremin libzfs_error_description(g_zfs));
318de1ab35cSAlexander Eremin return (zfs_err_to_be_err(g_zfs));
319de1ab35cSAlexander Eremin }
320f169c0eaSGlenn Lagasse }
321f169c0eaSGlenn Lagasse
322f169c0eaSGlenn Lagasse /* Generate string for BE's root dataset */
323*ec8422d0SAndy Fiddaman if ((ret = be_make_root_ds(bt.obe_zpool, bt.obe_name, obe_root_ds,
324*ec8422d0SAndy Fiddaman sizeof (obe_root_ds))) != BE_SUCCESS) {
325*ec8422d0SAndy Fiddaman be_print_err(gettext("%s: failed to get BE container dataset "
326*ec8422d0SAndy Fiddaman "for %s/%s\n"), __func__, bt.obe_zpool, bt.obe_name);
327*ec8422d0SAndy Fiddaman return (ret);
328*ec8422d0SAndy Fiddaman }
329f169c0eaSGlenn Lagasse bt.obe_root_ds = obe_root_ds;
330f169c0eaSGlenn Lagasse
3317e0e2549SAlexander Eremin if (getzoneid() != GLOBAL_ZONEID) {
3327e0e2549SAlexander Eremin if (!be_zone_compare_uuids(bt.obe_root_ds)) {
3337e0e2549SAlexander Eremin be_print_err(gettext("be_rollback: rolling back zone "
3347e0e2549SAlexander Eremin "root dataset from non-active global BE is not "
3357e0e2549SAlexander Eremin "supported\n"));
3367e0e2549SAlexander Eremin return (BE_ERR_NOTSUP);
3377e0e2549SAlexander Eremin }
3387e0e2549SAlexander Eremin }
3397e0e2549SAlexander Eremin
340f169c0eaSGlenn Lagasse /* Get handle to BE's root dataset */
341f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET)) == NULL) {
342f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback: "
343f169c0eaSGlenn Lagasse "failed to open BE root dataset (%s): %s\n"),
344f169c0eaSGlenn Lagasse bt.obe_root_ds, libzfs_error_description(g_zfs));
345f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs));
346f169c0eaSGlenn Lagasse }
347f169c0eaSGlenn Lagasse
348f169c0eaSGlenn Lagasse /*
349f169c0eaSGlenn Lagasse * Check that snapshot name exists for this BE and all of its
350f169c0eaSGlenn Lagasse * children file systems. This call will end up closing the
351f169c0eaSGlenn Lagasse * zfs handle passed in whether it succeeds or fails.
352f169c0eaSGlenn Lagasse */
353f169c0eaSGlenn Lagasse if ((ret = be_rollback_check_callback(zhp, bt.obe_snap_name)) != 0) {
354f169c0eaSGlenn Lagasse zhp = NULL;
355f169c0eaSGlenn Lagasse return (ret);
356f169c0eaSGlenn Lagasse }
357f169c0eaSGlenn Lagasse
358f169c0eaSGlenn Lagasse /* Get handle to BE's root dataset */
359f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET)) == NULL) {
360f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback: "
361f169c0eaSGlenn Lagasse "failed to open BE root dataset (%s): %s\n"),
362f169c0eaSGlenn Lagasse bt.obe_root_ds, libzfs_error_description(g_zfs));
363f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs));
364f169c0eaSGlenn Lagasse }
365f169c0eaSGlenn Lagasse
366f169c0eaSGlenn Lagasse /*
367f169c0eaSGlenn Lagasse * Iterate through a BE's datasets and roll them all back to
368f169c0eaSGlenn Lagasse * the specified snapshot. This call will end up closing the
369f169c0eaSGlenn Lagasse * zfs handle passed in whether it succeeds or fails.
370f169c0eaSGlenn Lagasse */
371f169c0eaSGlenn Lagasse if ((ret = be_rollback_callback(zhp, bt.obe_snap_name)) != 0) {
372f169c0eaSGlenn Lagasse zhp = NULL;
373f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback: "
374f169c0eaSGlenn Lagasse "failed to rollback BE %s to %s\n"), bt.obe_name,
375f169c0eaSGlenn Lagasse bt.obe_snap_name);
376f169c0eaSGlenn Lagasse return (ret);
377f169c0eaSGlenn Lagasse }
378f169c0eaSGlenn Lagasse zhp = NULL;
379f169c0eaSGlenn Lagasse be_zfs_fini();
380f169c0eaSGlenn Lagasse return (BE_SUCCESS);
381f169c0eaSGlenn Lagasse }
382f169c0eaSGlenn Lagasse
383f169c0eaSGlenn Lagasse
384f169c0eaSGlenn Lagasse /* ******************************************************************** */
385f169c0eaSGlenn Lagasse /* Semi-Private Functions */
386f169c0eaSGlenn Lagasse /* ******************************************************************** */
387f169c0eaSGlenn Lagasse
388f169c0eaSGlenn Lagasse /*
389f169c0eaSGlenn Lagasse * Function: _be_create_snapshot
390f169c0eaSGlenn Lagasse * Description: see be_create_snapshot
391f169c0eaSGlenn Lagasse * Parameters:
392f169c0eaSGlenn Lagasse * be_name - The name of the BE that we're taking a snapshot of.
393f169c0eaSGlenn Lagasse * snap_name - The name of the snapshot we're creating. If
394f169c0eaSGlenn Lagasse * snap_name is NULL an auto generated name will be used,
395f169c0eaSGlenn Lagasse * and upon success, will return that name via this
396f169c0eaSGlenn Lagasse * reference pointer. The caller is responsible for
397f169c0eaSGlenn Lagasse * freeing the returned name.
398f169c0eaSGlenn Lagasse * policy - The clean-up policy type. (library wide use only)
399f169c0eaSGlenn Lagasse * Return:
400f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
401f169c0eaSGlenn Lagasse * be_errno_t - Failure
402f169c0eaSGlenn Lagasse * Scope:
403f169c0eaSGlenn Lagasse * Semi-private (library wide use only)
404f169c0eaSGlenn Lagasse */
405f169c0eaSGlenn Lagasse int
_be_create_snapshot(char * be_name,char ** snap_name,char * policy)406f169c0eaSGlenn Lagasse _be_create_snapshot(char *be_name, char **snap_name, char *policy)
407f169c0eaSGlenn Lagasse {
408f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 };
409f169c0eaSGlenn Lagasse zfs_handle_t *zhp = NULL;
410f169c0eaSGlenn Lagasse nvlist_t *ss_props = NULL;
411f169c0eaSGlenn Lagasse char ss[MAXPATHLEN];
412f169c0eaSGlenn Lagasse char root_ds[MAXPATHLEN];
413f169c0eaSGlenn Lagasse int pool_version = 0;
414f169c0eaSGlenn Lagasse int i = 0;
415f169c0eaSGlenn Lagasse int zret = 0, ret = BE_SUCCESS;
416f169c0eaSGlenn Lagasse boolean_t autoname = B_FALSE;
417f169c0eaSGlenn Lagasse
418f169c0eaSGlenn Lagasse /* Set parameters in bt structure */
419f169c0eaSGlenn Lagasse bt.obe_name = be_name;
420f169c0eaSGlenn Lagasse bt.obe_snap_name = *snap_name;
421f169c0eaSGlenn Lagasse bt.policy = policy;
422f169c0eaSGlenn Lagasse
423f169c0eaSGlenn Lagasse /* If original BE name not supplied, use current BE */
424f169c0eaSGlenn Lagasse if (bt.obe_name == NULL) {
425f169c0eaSGlenn Lagasse if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
426f169c0eaSGlenn Lagasse return (ret);
427f169c0eaSGlenn Lagasse }
428f169c0eaSGlenn Lagasse }
429f169c0eaSGlenn Lagasse
430f169c0eaSGlenn Lagasse /* Find which zpool obe_name lives in */
431f169c0eaSGlenn Lagasse if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
432f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: failed to "
433f169c0eaSGlenn Lagasse "find zpool for BE (%s)\n"), bt.obe_name);
434f169c0eaSGlenn Lagasse return (BE_ERR_BE_NOENT);
435f169c0eaSGlenn Lagasse } else if (zret < 0) {
436f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
437f169c0eaSGlenn Lagasse "zpool_iter failed: %s\n"),
438f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
439f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs));
440f169c0eaSGlenn Lagasse }
441f169c0eaSGlenn Lagasse
442*ec8422d0SAndy Fiddaman if ((ret = be_make_root_ds(bt.obe_zpool, bt.obe_name, root_ds,
443*ec8422d0SAndy Fiddaman sizeof (root_ds))) != BE_SUCCESS) {
444*ec8422d0SAndy Fiddaman be_print_err(gettext("%s: failed to get BE container dataset "
445*ec8422d0SAndy Fiddaman "for %s/%s\n"), __func__, bt.obe_zpool, bt.obe_name);
446*ec8422d0SAndy Fiddaman return (ret);
447*ec8422d0SAndy Fiddaman }
448f169c0eaSGlenn Lagasse bt.obe_root_ds = root_ds;
449f169c0eaSGlenn Lagasse
4507e0e2549SAlexander Eremin if (getzoneid() != GLOBAL_ZONEID) {
4517e0e2549SAlexander Eremin if (!be_zone_compare_uuids(bt.obe_root_ds)) {
4527e0e2549SAlexander Eremin be_print_err(gettext("be_create_snapshot: creating "
4537e0e2549SAlexander Eremin "snapshot for the zone root dataset from "
4547e0e2549SAlexander Eremin "non-active global BE is not "
4557e0e2549SAlexander Eremin "supported\n"));
4567e0e2549SAlexander Eremin return (BE_ERR_NOTSUP);
4577e0e2549SAlexander Eremin }
4587e0e2549SAlexander Eremin }
4597e0e2549SAlexander Eremin
460f169c0eaSGlenn Lagasse /* If BE policy not specified, use the default policy */
461f169c0eaSGlenn Lagasse if (bt.policy == NULL) {
462f169c0eaSGlenn Lagasse bt.policy = be_default_policy();
463f169c0eaSGlenn Lagasse } else {
464f169c0eaSGlenn Lagasse /* Validate policy type */
465f169c0eaSGlenn Lagasse if (!valid_be_policy(bt.policy)) {
466f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
467f169c0eaSGlenn Lagasse "invalid BE policy type (%s)\n"), bt.policy);
468f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
469f169c0eaSGlenn Lagasse }
470f169c0eaSGlenn Lagasse }
471f169c0eaSGlenn Lagasse
472f169c0eaSGlenn Lagasse /*
473f169c0eaSGlenn Lagasse * If snapshot name not specified, set auto name flag and
474f169c0eaSGlenn Lagasse * generate auto snapshot name.
475f169c0eaSGlenn Lagasse */
476f169c0eaSGlenn Lagasse if (bt.obe_snap_name == NULL) {
477f169c0eaSGlenn Lagasse autoname = B_TRUE;
478f169c0eaSGlenn Lagasse if ((bt.obe_snap_name = be_auto_snap_name())
479f169c0eaSGlenn Lagasse == NULL) {
480f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
481f169c0eaSGlenn Lagasse "failed to create auto snapshot name\n"));
482f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME;
483f169c0eaSGlenn Lagasse goto done;
484f169c0eaSGlenn Lagasse }
485f169c0eaSGlenn Lagasse }
486f169c0eaSGlenn Lagasse
487f169c0eaSGlenn Lagasse /* Generate the name of the snapshot to take. */
488f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s", bt.obe_root_ds,
489f169c0eaSGlenn Lagasse bt.obe_snap_name);
490f169c0eaSGlenn Lagasse
491f169c0eaSGlenn Lagasse /* Get handle to BE's root dataset */
492f169c0eaSGlenn Lagasse if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET))
493f169c0eaSGlenn Lagasse == NULL) {
494f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
495f169c0eaSGlenn Lagasse "failed to open BE root dataset (%s): %s\n"),
496f169c0eaSGlenn Lagasse bt.obe_root_ds, libzfs_error_description(g_zfs));
497f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
498f169c0eaSGlenn Lagasse goto done;
499f169c0eaSGlenn Lagasse }
500f169c0eaSGlenn Lagasse
501f169c0eaSGlenn Lagasse /* Get the ZFS pool version of the pool where this dataset resides */
502f169c0eaSGlenn Lagasse if (zfs_spa_version(zhp, &pool_version) != 0) {
503f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: failed to "
504f169c0eaSGlenn Lagasse "get ZFS pool version for %s: %s\n"), zfs_get_name(zhp),
505f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
506f169c0eaSGlenn Lagasse }
507f169c0eaSGlenn Lagasse
508f169c0eaSGlenn Lagasse /*
509f169c0eaSGlenn Lagasse * If ZFS pool version supports snapshot user properties, store
510f169c0eaSGlenn Lagasse * cleanup policy there. Otherwise don't set one - this snapshot
511f169c0eaSGlenn Lagasse * will always inherit the cleanup policy from its parent.
512f169c0eaSGlenn Lagasse */
5137e0e2549SAlexander Eremin if (getzoneid() == GLOBAL_ZONEID) {
5147e0e2549SAlexander Eremin if (pool_version >= SPA_VERSION_SNAP_PROPS) {
5157e0e2549SAlexander Eremin if (nvlist_alloc(&ss_props, NV_UNIQUE_NAME, 0) != 0) {
5167e0e2549SAlexander Eremin be_print_err(gettext("be_create_snapshot: "
5177e0e2549SAlexander Eremin "internal error: out of memory\n"));
5187e0e2549SAlexander Eremin return (BE_ERR_NOMEM);
5197e0e2549SAlexander Eremin }
5207e0e2549SAlexander Eremin if (nvlist_add_string(ss_props, BE_POLICY_PROPERTY,
5217e0e2549SAlexander Eremin bt.policy) != 0) {
5227e0e2549SAlexander Eremin be_print_err(gettext("be_create_snapshot: "
5237e0e2549SAlexander Eremin "internal error: out of memory\n"));
5247e0e2549SAlexander Eremin nvlist_free(ss_props);
5257e0e2549SAlexander Eremin return (BE_ERR_NOMEM);
5267e0e2549SAlexander Eremin }
5277e0e2549SAlexander Eremin } else if (policy != NULL) {
5287e0e2549SAlexander Eremin /*
5297e0e2549SAlexander Eremin * If an explicit cleanup policy was requested
5307e0e2549SAlexander Eremin * by the caller and we don't support it, error out.
5317e0e2549SAlexander Eremin */
5327e0e2549SAlexander Eremin be_print_err(gettext("be_create_snapshot: cannot set "
5337e0e2549SAlexander Eremin "cleanup policy: ZFS pool version is %d\n"),
5347e0e2549SAlexander Eremin pool_version);
5357e0e2549SAlexander Eremin return (BE_ERR_NOTSUP);
536f169c0eaSGlenn Lagasse }
537f169c0eaSGlenn Lagasse }
538f169c0eaSGlenn Lagasse
539f169c0eaSGlenn Lagasse /* Create the snapshots recursively */
540f169c0eaSGlenn Lagasse if (zfs_snapshot(g_zfs, ss, B_TRUE, ss_props) != 0) {
541f169c0eaSGlenn Lagasse if (!autoname || libzfs_errno(g_zfs) != EZFS_EXISTS) {
542f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
543f169c0eaSGlenn Lagasse "recursive snapshot of %s failed: %s\n"),
544f169c0eaSGlenn Lagasse ss, libzfs_error_description(g_zfs));
545f169c0eaSGlenn Lagasse
546f169c0eaSGlenn Lagasse if (libzfs_errno(g_zfs) == EZFS_EXISTS)
547f169c0eaSGlenn Lagasse ret = BE_ERR_SS_EXISTS;
548f169c0eaSGlenn Lagasse else
549f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
550f169c0eaSGlenn Lagasse
551f169c0eaSGlenn Lagasse goto done;
552f169c0eaSGlenn Lagasse } else {
553f169c0eaSGlenn Lagasse for (i = 1; i < BE_AUTO_NAME_MAX_TRY; i++) {
554f169c0eaSGlenn Lagasse
555f169c0eaSGlenn Lagasse /* Sleep 1 before retrying */
556f169c0eaSGlenn Lagasse (void) sleep(1);
557f169c0eaSGlenn Lagasse
558f169c0eaSGlenn Lagasse /* Generate new auto snapshot name. */
559f169c0eaSGlenn Lagasse free(bt.obe_snap_name);
560f169c0eaSGlenn Lagasse if ((bt.obe_snap_name =
561f169c0eaSGlenn Lagasse be_auto_snap_name()) == NULL) {
562f169c0eaSGlenn Lagasse be_print_err(gettext(
563f169c0eaSGlenn Lagasse "be_create_snapshot: failed to "
564f169c0eaSGlenn Lagasse "create auto snapshot name\n"));
565f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME;
566f169c0eaSGlenn Lagasse goto done;
567f169c0eaSGlenn Lagasse }
568f169c0eaSGlenn Lagasse
569f169c0eaSGlenn Lagasse /* Generate string of the snapshot to take. */
570f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s",
571f169c0eaSGlenn Lagasse bt.obe_root_ds, bt.obe_snap_name);
572f169c0eaSGlenn Lagasse
573f169c0eaSGlenn Lagasse /* Create the snapshots recursively */
574f169c0eaSGlenn Lagasse if (zfs_snapshot(g_zfs, ss, B_TRUE, ss_props)
575f169c0eaSGlenn Lagasse != 0) {
576f169c0eaSGlenn Lagasse if (libzfs_errno(g_zfs) !=
577f169c0eaSGlenn Lagasse EZFS_EXISTS) {
578f169c0eaSGlenn Lagasse be_print_err(gettext(
579f169c0eaSGlenn Lagasse "be_create_snapshot: "
580f169c0eaSGlenn Lagasse "recursive snapshot of %s "
581f169c0eaSGlenn Lagasse "failed: %s\n"), ss,
582f169c0eaSGlenn Lagasse libzfs_error_description(
583f169c0eaSGlenn Lagasse g_zfs));
584f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
585f169c0eaSGlenn Lagasse goto done;
586f169c0eaSGlenn Lagasse }
587f169c0eaSGlenn Lagasse } else {
588f169c0eaSGlenn Lagasse break;
589f169c0eaSGlenn Lagasse }
590f169c0eaSGlenn Lagasse }
591f169c0eaSGlenn Lagasse
592f169c0eaSGlenn Lagasse /*
593f169c0eaSGlenn Lagasse * If we exhausted the maximum number of tries,
594f169c0eaSGlenn Lagasse * free the auto snap name and set error.
595f169c0eaSGlenn Lagasse */
596f169c0eaSGlenn Lagasse if (i == BE_AUTO_NAME_MAX_TRY) {
597f169c0eaSGlenn Lagasse be_print_err(gettext("be_create_snapshot: "
598f169c0eaSGlenn Lagasse "failed to create unique auto snapshot "
599f169c0eaSGlenn Lagasse "name\n"));
600f169c0eaSGlenn Lagasse free(bt.obe_snap_name);
601f169c0eaSGlenn Lagasse bt.obe_snap_name = NULL;
602f169c0eaSGlenn Lagasse ret = BE_ERR_AUTONAME;
603f169c0eaSGlenn Lagasse }
604f169c0eaSGlenn Lagasse }
605f169c0eaSGlenn Lagasse }
606f169c0eaSGlenn Lagasse
607f169c0eaSGlenn Lagasse /*
608f169c0eaSGlenn Lagasse * If we succeeded in creating an auto named snapshot, store
609f169c0eaSGlenn Lagasse * the name in the nvlist passed in by the caller.
610f169c0eaSGlenn Lagasse */
611f169c0eaSGlenn Lagasse if (autoname && bt.obe_snap_name) {
612f169c0eaSGlenn Lagasse *snap_name = bt.obe_snap_name;
613f169c0eaSGlenn Lagasse }
614f169c0eaSGlenn Lagasse
615f169c0eaSGlenn Lagasse done:
616f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
617f169c0eaSGlenn Lagasse
618aab83bb8SJosef 'Jeff' Sipek nvlist_free(ss_props);
619f169c0eaSGlenn Lagasse
620f169c0eaSGlenn Lagasse return (ret);
621f169c0eaSGlenn Lagasse }
622f169c0eaSGlenn Lagasse
623f169c0eaSGlenn Lagasse /*
624f169c0eaSGlenn Lagasse * Function: _be_destroy_snapshot
625f169c0eaSGlenn Lagasse * Description: see be_destroy_snapshot
626f169c0eaSGlenn Lagasse * Parameters:
627f169c0eaSGlenn Lagasse * be_name - The name of the BE that the snapshot belongs to.
628f169c0eaSGlenn Lagasse * snap_name - The name of the snapshot we're destroying.
629f169c0eaSGlenn Lagasse * Return:
630f169c0eaSGlenn Lagasse * BE_SUCCESS - Success
631f169c0eaSGlenn Lagasse * be_errno_t - Failure
632f169c0eaSGlenn Lagasse * Scope:
633f169c0eaSGlenn Lagasse * Semi-private (library wide use only)
634f169c0eaSGlenn Lagasse */
635f169c0eaSGlenn Lagasse int
_be_destroy_snapshot(char * be_name,char * snap_name)636f169c0eaSGlenn Lagasse _be_destroy_snapshot(char *be_name, char *snap_name)
637f169c0eaSGlenn Lagasse {
638f169c0eaSGlenn Lagasse be_transaction_data_t bt = { 0 };
639f169c0eaSGlenn Lagasse zfs_handle_t *zhp;
640f169c0eaSGlenn Lagasse char ss[MAXPATHLEN];
641f169c0eaSGlenn Lagasse char root_ds[MAXPATHLEN];
642f169c0eaSGlenn Lagasse int err = BE_SUCCESS, ret = BE_SUCCESS;
643f169c0eaSGlenn Lagasse
644f169c0eaSGlenn Lagasse /* Make sure we actaully have a snapshot name */
645f169c0eaSGlenn Lagasse if (snap_name == NULL) {
646f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
647f169c0eaSGlenn Lagasse "invalid snapshot name\n"));
648f169c0eaSGlenn Lagasse return (BE_ERR_INVAL);
649f169c0eaSGlenn Lagasse }
650f169c0eaSGlenn Lagasse
651f169c0eaSGlenn Lagasse /* Set parameters in bt structure */
652f169c0eaSGlenn Lagasse bt.obe_name = be_name;
653f169c0eaSGlenn Lagasse bt.obe_snap_name = snap_name;
654f169c0eaSGlenn Lagasse
655f169c0eaSGlenn Lagasse /* If original BE name not supplied, use current BE */
656f169c0eaSGlenn Lagasse if (bt.obe_name == NULL) {
657f169c0eaSGlenn Lagasse if ((err = be_find_current_be(&bt)) != BE_SUCCESS) {
658f169c0eaSGlenn Lagasse return (err);
659f169c0eaSGlenn Lagasse }
660f169c0eaSGlenn Lagasse }
661f169c0eaSGlenn Lagasse
662f169c0eaSGlenn Lagasse /* Find which zpool be_name lives in */
663f169c0eaSGlenn Lagasse if ((ret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
664f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
665f169c0eaSGlenn Lagasse "failed to find zpool for BE (%s)\n"), bt.obe_name);
666f169c0eaSGlenn Lagasse return (BE_ERR_BE_NOENT);
667f169c0eaSGlenn Lagasse } else if (ret < 0) {
668f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
669f169c0eaSGlenn Lagasse "zpool_iter failed: %s\n"),
670f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
671f169c0eaSGlenn Lagasse return (zfs_err_to_be_err(g_zfs));
672f169c0eaSGlenn Lagasse }
673f169c0eaSGlenn Lagasse
674*ec8422d0SAndy Fiddaman if ((ret = be_make_root_ds(bt.obe_zpool, bt.obe_name, root_ds,
675*ec8422d0SAndy Fiddaman sizeof (root_ds))) != BE_SUCCESS) {
676*ec8422d0SAndy Fiddaman be_print_err(gettext("%s: failed to get BE container dataset "
677*ec8422d0SAndy Fiddaman "for %s/%s\n"), __func__, bt.obe_zpool, bt.obe_name);
678*ec8422d0SAndy Fiddaman return (ret);
679*ec8422d0SAndy Fiddaman }
680f169c0eaSGlenn Lagasse bt.obe_root_ds = root_ds;
681f169c0eaSGlenn Lagasse
682f169c0eaSGlenn Lagasse zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET);
683f169c0eaSGlenn Lagasse if (zhp == NULL) {
684f169c0eaSGlenn Lagasse /*
685f169c0eaSGlenn Lagasse * The zfs_open failed, return an error.
686f169c0eaSGlenn Lagasse */
687f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
688f169c0eaSGlenn Lagasse "failed to open BE root dataset (%s): %s\n"),
689f169c0eaSGlenn Lagasse bt.obe_root_ds, libzfs_error_description(g_zfs));
690f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
691f169c0eaSGlenn Lagasse } else {
692f169c0eaSGlenn Lagasse /*
693f169c0eaSGlenn Lagasse * Generate the name of the snapshot to take.
694f169c0eaSGlenn Lagasse */
695f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s", bt.obe_name,
696f169c0eaSGlenn Lagasse bt.obe_snap_name);
697f169c0eaSGlenn Lagasse
698f169c0eaSGlenn Lagasse /*
699f169c0eaSGlenn Lagasse * destroy the snapshot.
700f169c0eaSGlenn Lagasse */
701f169c0eaSGlenn Lagasse /*
702f169c0eaSGlenn Lagasse * The boolean set to B_FALSE and passed to zfs_destroy_snaps()
703f169c0eaSGlenn Lagasse * tells zfs to process and destroy the snapshots now.
704f169c0eaSGlenn Lagasse * Otherwise the call will potentially return where the
705f169c0eaSGlenn Lagasse * snapshot isn't actually destroyed yet, and ZFS is waiting
706f169c0eaSGlenn Lagasse * until all the references to the snapshot have been
707f169c0eaSGlenn Lagasse * released before actually destroying the snapshot.
708f169c0eaSGlenn Lagasse */
709f169c0eaSGlenn Lagasse if (zfs_destroy_snaps(zhp, bt.obe_snap_name, B_FALSE) != 0) {
710f169c0eaSGlenn Lagasse err = zfs_err_to_be_err(g_zfs);
711f169c0eaSGlenn Lagasse be_print_err(gettext("be_destroy_snapshot: "
712f169c0eaSGlenn Lagasse "failed to destroy snapshot %s: %s\n"), ss,
713f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
714f169c0eaSGlenn Lagasse }
715f169c0eaSGlenn Lagasse }
716f169c0eaSGlenn Lagasse
717f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
718f169c0eaSGlenn Lagasse
719f169c0eaSGlenn Lagasse return (err);
720f169c0eaSGlenn Lagasse }
721f169c0eaSGlenn Lagasse
722f169c0eaSGlenn Lagasse /* ******************************************************************** */
723f169c0eaSGlenn Lagasse /* Private Functions */
724f169c0eaSGlenn Lagasse /* ******************************************************************** */
725f169c0eaSGlenn Lagasse
726f169c0eaSGlenn Lagasse /*
727f169c0eaSGlenn Lagasse * Function: be_rollback_check_callback
728f169c0eaSGlenn Lagasse * Description: Callback function used to iterate through a BE's filesystems
729f169c0eaSGlenn Lagasse * to check if a given snapshot name exists.
730f169c0eaSGlenn Lagasse * Parameters:
731f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to filesystem being processed.
732f169c0eaSGlenn Lagasse * data - name of the snapshot to check for.
733f169c0eaSGlenn Lagasse * Returns:
734f169c0eaSGlenn Lagasse * 0 - Success, snapshot name exists for all filesystems.
735f169c0eaSGlenn Lagasse * be_errno_t - Failure, snapshot name does not exist for all
736f169c0eaSGlenn Lagasse * filesystems.
737f169c0eaSGlenn Lagasse * Scope:
738f169c0eaSGlenn Lagasse * Private
739f169c0eaSGlenn Lagasse */
740f169c0eaSGlenn Lagasse static int
be_rollback_check_callback(zfs_handle_t * zhp,void * data)741f169c0eaSGlenn Lagasse be_rollback_check_callback(zfs_handle_t *zhp, void *data)
742f169c0eaSGlenn Lagasse {
743f169c0eaSGlenn Lagasse char *snap_name = data;
744f169c0eaSGlenn Lagasse char ss[MAXPATHLEN];
745f169c0eaSGlenn Lagasse int ret = BE_SUCCESS;
746f169c0eaSGlenn Lagasse
747f169c0eaSGlenn Lagasse /* Generate string for this filesystem's snapshot name */
748f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s", zfs_get_name(zhp), snap_name);
749f169c0eaSGlenn Lagasse
750f169c0eaSGlenn Lagasse /* Check if snapshot exists */
751f169c0eaSGlenn Lagasse if (!zfs_dataset_exists(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) {
752f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback_check_callback: "
753f169c0eaSGlenn Lagasse "snapshot does not exist %s\n"), ss);
754f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
755f169c0eaSGlenn Lagasse return (BE_ERR_SS_NOENT);
756f169c0eaSGlenn Lagasse }
757f169c0eaSGlenn Lagasse
758f169c0eaSGlenn Lagasse /* Iterate this dataset's children and check them */
759f169c0eaSGlenn Lagasse if ((ret = zfs_iter_filesystems(zhp, be_rollback_check_callback,
760f169c0eaSGlenn Lagasse snap_name)) != 0) {
761f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
762f169c0eaSGlenn Lagasse return (ret);
763f169c0eaSGlenn Lagasse }
764f169c0eaSGlenn Lagasse
765f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
766f169c0eaSGlenn Lagasse return (0);
767f169c0eaSGlenn Lagasse }
768f169c0eaSGlenn Lagasse
769f169c0eaSGlenn Lagasse /*
770f169c0eaSGlenn Lagasse * Function: be_rollback_callback
771f169c0eaSGlenn Lagasse * Description: Callback function used to iterate through a BE's filesystems
772f169c0eaSGlenn Lagasse * and roll them all back to the specified snapshot name.
773f169c0eaSGlenn Lagasse * Parameters:
774f169c0eaSGlenn Lagasse * zhp - zfs_handle_t pointer to filesystem being processed.
775f169c0eaSGlenn Lagasse * data - name of snapshot to rollback to.
776f169c0eaSGlenn Lagasse * Returns:
777f169c0eaSGlenn Lagasse * 0 - Success
778f169c0eaSGlenn Lagasse * be_errno_t - Failure
779f169c0eaSGlenn Lagasse * Scope:
780f169c0eaSGlenn Lagasse * Private
781f169c0eaSGlenn Lagasse */
782f169c0eaSGlenn Lagasse static int
be_rollback_callback(zfs_handle_t * zhp,void * data)783f169c0eaSGlenn Lagasse be_rollback_callback(zfs_handle_t *zhp, void *data)
784f169c0eaSGlenn Lagasse {
785f169c0eaSGlenn Lagasse zfs_handle_t *zhp_snap = NULL;
786f169c0eaSGlenn Lagasse char *snap_name = data;
787f169c0eaSGlenn Lagasse char ss[MAXPATHLEN];
788f169c0eaSGlenn Lagasse int ret = 0;
789f169c0eaSGlenn Lagasse
790f169c0eaSGlenn Lagasse /* Generate string for this filesystem's snapshot name */
791f169c0eaSGlenn Lagasse (void) snprintf(ss, sizeof (ss), "%s@%s", zfs_get_name(zhp), snap_name);
792f169c0eaSGlenn Lagasse
793f169c0eaSGlenn Lagasse /* Get handle to this filesystem's snapshot */
794f169c0eaSGlenn Lagasse if ((zhp_snap = zfs_open(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) == NULL) {
795f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback_callback: "
796f169c0eaSGlenn Lagasse "failed to open snapshot %s: %s\n"), zfs_get_name(zhp),
797f169c0eaSGlenn Lagasse libzfs_error_description(g_zfs));
798f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
799f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
800f169c0eaSGlenn Lagasse return (ret);
801f169c0eaSGlenn Lagasse }
802f169c0eaSGlenn Lagasse
803f169c0eaSGlenn Lagasse /* Rollback dataset */
804f169c0eaSGlenn Lagasse if (zfs_rollback(zhp, zhp_snap, B_FALSE) != 0) {
805f169c0eaSGlenn Lagasse be_print_err(gettext("be_rollback_callback: "
806f169c0eaSGlenn Lagasse "failed to rollback BE dataset %s to snapshot %s: %s\n"),
807f169c0eaSGlenn Lagasse zfs_get_name(zhp), ss, libzfs_error_description(g_zfs));
808f169c0eaSGlenn Lagasse ret = zfs_err_to_be_err(g_zfs);
809f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp_snap);
810f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
811f169c0eaSGlenn Lagasse return (ret);
812f169c0eaSGlenn Lagasse }
813f169c0eaSGlenn Lagasse
814f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp_snap);
815f169c0eaSGlenn Lagasse /* Iterate this dataset's children and roll them back */
816f169c0eaSGlenn Lagasse if ((ret = zfs_iter_filesystems(zhp, be_rollback_callback,
817f169c0eaSGlenn Lagasse snap_name)) != 0) {
818f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
819f169c0eaSGlenn Lagasse return (ret);
820f169c0eaSGlenn Lagasse }
821f169c0eaSGlenn Lagasse
822f169c0eaSGlenn Lagasse ZFS_CLOSE(zhp);
823f169c0eaSGlenn Lagasse return (0);
824f169c0eaSGlenn Lagasse }
825