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