Print this page
patch tsoome-feedback
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
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 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"),
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 326 if (getzoneid() != GLOBAL_ZONEID) {
327 327 if (!be_zone_compare_uuids(bt.obe_root_ds)) {
328 328 be_print_err(gettext("be_rollback: rolling back zone "
329 329 "root dataset from non-active global BE is not "
330 330 "supported\n"));
331 331 return (BE_ERR_NOTSUP);
332 332 }
333 333 }
334 334
335 335 /* Get handle to BE's root dataset */
336 336 if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET)) == NULL) {
337 337 be_print_err(gettext("be_rollback: "
338 338 "failed to open BE root dataset (%s): %s\n"),
339 339 bt.obe_root_ds, libzfs_error_description(g_zfs));
340 340 return (zfs_err_to_be_err(g_zfs));
341 341 }
342 342
343 343 /*
344 344 * Check that snapshot name exists for this BE and all of its
345 345 * children file systems. This call will end up closing the
346 346 * zfs handle passed in whether it succeeds or fails.
347 347 */
348 348 if ((ret = be_rollback_check_callback(zhp, bt.obe_snap_name)) != 0) {
349 349 zhp = NULL;
350 350 return (ret);
351 351 }
352 352
353 353 /* Get handle to BE's root dataset */
354 354 if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET)) == NULL) {
355 355 be_print_err(gettext("be_rollback: "
356 356 "failed to open BE root dataset (%s): %s\n"),
357 357 bt.obe_root_ds, libzfs_error_description(g_zfs));
358 358 return (zfs_err_to_be_err(g_zfs));
359 359 }
360 360
361 361 /*
362 362 * Iterate through a BE's datasets and roll them all back to
363 363 * the specified snapshot. This call will end up closing the
364 364 * zfs handle passed in whether it succeeds or fails.
365 365 */
366 366 if ((ret = be_rollback_callback(zhp, bt.obe_snap_name)) != 0) {
367 367 zhp = NULL;
368 368 be_print_err(gettext("be_rollback: "
369 369 "failed to rollback BE %s to %s\n"), bt.obe_name,
370 370 bt.obe_snap_name);
371 371 return (ret);
372 372 }
373 373 zhp = NULL;
374 374 be_zfs_fini();
375 375 return (BE_SUCCESS);
376 376 }
377 377
378 378
379 379 /* ******************************************************************** */
380 380 /* Semi-Private Functions */
381 381 /* ******************************************************************** */
382 382
383 383 /*
384 384 * Function: _be_create_snapshot
385 385 * Description: see be_create_snapshot
386 386 * Parameters:
387 387 * be_name - The name of the BE that we're taking a snapshot of.
388 388 * snap_name - The name of the snapshot we're creating. If
389 389 * snap_name is NULL an auto generated name will be used,
390 390 * and upon success, will return that name via this
391 391 * reference pointer. The caller is responsible for
392 392 * freeing the returned name.
393 393 * policy - The clean-up policy type. (library wide use only)
394 394 * Return:
395 395 * BE_SUCCESS - Success
396 396 * be_errno_t - Failure
397 397 * Scope:
398 398 * Semi-private (library wide use only)
399 399 */
400 400 int
401 401 _be_create_snapshot(char *be_name, char **snap_name, char *policy)
402 402 {
403 403 be_transaction_data_t bt = { 0 };
404 404 zfs_handle_t *zhp = NULL;
405 405 nvlist_t *ss_props = NULL;
406 406 char ss[MAXPATHLEN];
407 407 char root_ds[MAXPATHLEN];
408 408 int pool_version = 0;
409 409 int i = 0;
410 410 int zret = 0, ret = BE_SUCCESS;
411 411 boolean_t autoname = B_FALSE;
412 412
413 413 /* Set parameters in bt structure */
414 414 bt.obe_name = be_name;
415 415 bt.obe_snap_name = *snap_name;
416 416 bt.policy = policy;
417 417
418 418 /* If original BE name not supplied, use current BE */
419 419 if (bt.obe_name == NULL) {
420 420 if ((ret = be_find_current_be(&bt)) != BE_SUCCESS) {
421 421 return (ret);
422 422 }
423 423 }
424 424
425 425 /* Find which zpool obe_name lives in */
426 426 if ((zret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
427 427 be_print_err(gettext("be_create_snapshot: failed to "
428 428 "find zpool for BE (%s)\n"), bt.obe_name);
429 429 return (BE_ERR_BE_NOENT);
430 430 } else if (zret < 0) {
431 431 be_print_err(gettext("be_create_snapshot: "
432 432 "zpool_iter failed: %s\n"),
433 433 libzfs_error_description(g_zfs));
434 434 return (zfs_err_to_be_err(g_zfs));
435 435 }
436 436
437 437 be_make_root_ds(bt.obe_zpool, bt.obe_name, root_ds,
438 438 sizeof (root_ds));
439 439 bt.obe_root_ds = root_ds;
440 440
441 441 if (getzoneid() != GLOBAL_ZONEID) {
442 442 if (!be_zone_compare_uuids(bt.obe_root_ds)) {
443 443 be_print_err(gettext("be_create_snapshot: creating "
444 444 "snapshot for the zone root dataset from "
445 445 "non-active global BE is not "
446 446 "supported\n"));
447 447 return (BE_ERR_NOTSUP);
448 448 }
449 449 }
450 450
451 451 /* If BE policy not specified, use the default policy */
452 452 if (bt.policy == NULL) {
453 453 bt.policy = be_default_policy();
454 454 } else {
455 455 /* Validate policy type */
456 456 if (!valid_be_policy(bt.policy)) {
457 457 be_print_err(gettext("be_create_snapshot: "
458 458 "invalid BE policy type (%s)\n"), bt.policy);
459 459 return (BE_ERR_INVAL);
460 460 }
461 461 }
462 462
463 463 /*
464 464 * If snapshot name not specified, set auto name flag and
465 465 * generate auto snapshot name.
466 466 */
467 467 if (bt.obe_snap_name == NULL) {
468 468 autoname = B_TRUE;
469 469 if ((bt.obe_snap_name = be_auto_snap_name())
470 470 == NULL) {
471 471 be_print_err(gettext("be_create_snapshot: "
472 472 "failed to create auto snapshot name\n"));
473 473 ret = BE_ERR_AUTONAME;
474 474 goto done;
475 475 }
476 476 }
477 477
478 478 /* Generate the name of the snapshot to take. */
479 479 (void) snprintf(ss, sizeof (ss), "%s@%s", bt.obe_root_ds,
480 480 bt.obe_snap_name);
481 481
482 482 /* Get handle to BE's root dataset */
483 483 if ((zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET))
484 484 == NULL) {
485 485 be_print_err(gettext("be_create_snapshot: "
486 486 "failed to open BE root dataset (%s): %s\n"),
487 487 bt.obe_root_ds, libzfs_error_description(g_zfs));
488 488 ret = zfs_err_to_be_err(g_zfs);
489 489 goto done;
490 490 }
491 491
492 492 /* Get the ZFS pool version of the pool where this dataset resides */
493 493 if (zfs_spa_version(zhp, &pool_version) != 0) {
494 494 be_print_err(gettext("be_create_snapshot: failed to "
495 495 "get ZFS pool version for %s: %s\n"), zfs_get_name(zhp),
496 496 libzfs_error_description(g_zfs));
497 497 }
498 498
499 499 /*
500 500 * If ZFS pool version supports snapshot user properties, store
501 501 * cleanup policy there. Otherwise don't set one - this snapshot
502 502 * will always inherit the cleanup policy from its parent.
503 503 */
504 504 if (getzoneid() == GLOBAL_ZONEID) {
505 505 if (pool_version >= SPA_VERSION_SNAP_PROPS) {
506 506 if (nvlist_alloc(&ss_props, NV_UNIQUE_NAME, 0) != 0) {
507 507 be_print_err(gettext("be_create_snapshot: "
508 508 "internal error: out of memory\n"));
509 509 return (BE_ERR_NOMEM);
510 510 }
511 511 if (nvlist_add_string(ss_props, BE_POLICY_PROPERTY,
512 512 bt.policy) != 0) {
513 513 be_print_err(gettext("be_create_snapshot: "
514 514 "internal error: out of memory\n"));
515 515 nvlist_free(ss_props);
516 516 return (BE_ERR_NOMEM);
517 517 }
518 518 } else if (policy != NULL) {
519 519 /*
520 520 * If an explicit cleanup policy was requested
521 521 * by the caller and we don't support it, error out.
522 522 */
523 523 be_print_err(gettext("be_create_snapshot: cannot set "
524 524 "cleanup policy: ZFS pool version is %d\n"),
525 525 pool_version);
526 526 return (BE_ERR_NOTSUP);
527 527 }
528 528 }
529 529
530 530 /* Create the snapshots recursively */
531 531 if (zfs_snapshot(g_zfs, ss, B_TRUE, ss_props) != 0) {
532 532 if (!autoname || libzfs_errno(g_zfs) != EZFS_EXISTS) {
533 533 be_print_err(gettext("be_create_snapshot: "
534 534 "recursive snapshot of %s failed: %s\n"),
535 535 ss, libzfs_error_description(g_zfs));
536 536
537 537 if (libzfs_errno(g_zfs) == EZFS_EXISTS)
538 538 ret = BE_ERR_SS_EXISTS;
539 539 else
540 540 ret = zfs_err_to_be_err(g_zfs);
541 541
542 542 goto done;
543 543 } else {
544 544 for (i = 1; i < BE_AUTO_NAME_MAX_TRY; i++) {
545 545
546 546 /* Sleep 1 before retrying */
547 547 (void) sleep(1);
548 548
549 549 /* Generate new auto snapshot name. */
550 550 free(bt.obe_snap_name);
551 551 if ((bt.obe_snap_name =
552 552 be_auto_snap_name()) == NULL) {
553 553 be_print_err(gettext(
554 554 "be_create_snapshot: failed to "
555 555 "create auto snapshot name\n"));
556 556 ret = BE_ERR_AUTONAME;
557 557 goto done;
558 558 }
559 559
560 560 /* Generate string of the snapshot to take. */
561 561 (void) snprintf(ss, sizeof (ss), "%s@%s",
562 562 bt.obe_root_ds, bt.obe_snap_name);
563 563
564 564 /* Create the snapshots recursively */
565 565 if (zfs_snapshot(g_zfs, ss, B_TRUE, ss_props)
566 566 != 0) {
567 567 if (libzfs_errno(g_zfs) !=
568 568 EZFS_EXISTS) {
569 569 be_print_err(gettext(
570 570 "be_create_snapshot: "
571 571 "recursive snapshot of %s "
572 572 "failed: %s\n"), ss,
573 573 libzfs_error_description(
574 574 g_zfs));
575 575 ret = zfs_err_to_be_err(g_zfs);
576 576 goto done;
577 577 }
578 578 } else {
579 579 break;
580 580 }
581 581 }
582 582
583 583 /*
584 584 * If we exhausted the maximum number of tries,
585 585 * free the auto snap name and set error.
586 586 */
587 587 if (i == BE_AUTO_NAME_MAX_TRY) {
588 588 be_print_err(gettext("be_create_snapshot: "
589 589 "failed to create unique auto snapshot "
590 590 "name\n"));
591 591 free(bt.obe_snap_name);
592 592 bt.obe_snap_name = NULL;
593 593 ret = BE_ERR_AUTONAME;
594 594 }
595 595 }
596 596 }
597 597
598 598 /*
↓ open down ↓ |
598 lines elided |
↑ open up ↑ |
599 599 * If we succeeded in creating an auto named snapshot, store
600 600 * the name in the nvlist passed in by the caller.
601 601 */
602 602 if (autoname && bt.obe_snap_name) {
603 603 *snap_name = bt.obe_snap_name;
604 604 }
605 605
606 606 done:
607 607 ZFS_CLOSE(zhp);
608 608
609 - if (ss_props != NULL)
610 - nvlist_free(ss_props);
609 + nvlist_free(ss_props);
611 610
612 611 return (ret);
613 612 }
614 613
615 614 /*
616 615 * Function: _be_destroy_snapshot
617 616 * Description: see be_destroy_snapshot
618 617 * Parameters:
619 618 * be_name - The name of the BE that the snapshot belongs to.
620 619 * snap_name - The name of the snapshot we're destroying.
621 620 * Return:
622 621 * BE_SUCCESS - Success
623 622 * be_errno_t - Failure
624 623 * Scope:
625 624 * Semi-private (library wide use only)
626 625 */
627 626 int
628 627 _be_destroy_snapshot(char *be_name, char *snap_name)
629 628 {
630 629 be_transaction_data_t bt = { 0 };
631 630 zfs_handle_t *zhp;
632 631 char ss[MAXPATHLEN];
633 632 char root_ds[MAXPATHLEN];
634 633 int err = BE_SUCCESS, ret = BE_SUCCESS;
635 634
636 635 /* Make sure we actaully have a snapshot name */
637 636 if (snap_name == NULL) {
638 637 be_print_err(gettext("be_destroy_snapshot: "
639 638 "invalid snapshot name\n"));
640 639 return (BE_ERR_INVAL);
641 640 }
642 641
643 642 /* Set parameters in bt structure */
644 643 bt.obe_name = be_name;
645 644 bt.obe_snap_name = snap_name;
646 645
647 646 /* If original BE name not supplied, use current BE */
648 647 if (bt.obe_name == NULL) {
649 648 if ((err = be_find_current_be(&bt)) != BE_SUCCESS) {
650 649 return (err);
651 650 }
652 651 }
653 652
654 653 /* Find which zpool be_name lives in */
655 654 if ((ret = zpool_iter(g_zfs, be_find_zpool_callback, &bt)) == 0) {
656 655 be_print_err(gettext("be_destroy_snapshot: "
657 656 "failed to find zpool for BE (%s)\n"), bt.obe_name);
658 657 return (BE_ERR_BE_NOENT);
659 658 } else if (ret < 0) {
660 659 be_print_err(gettext("be_destroy_snapshot: "
661 660 "zpool_iter failed: %s\n"),
662 661 libzfs_error_description(g_zfs));
663 662 return (zfs_err_to_be_err(g_zfs));
664 663 }
665 664
666 665 be_make_root_ds(bt.obe_zpool, bt.obe_name, root_ds,
667 666 sizeof (root_ds));
668 667 bt.obe_root_ds = root_ds;
669 668
670 669 zhp = zfs_open(g_zfs, bt.obe_root_ds, ZFS_TYPE_DATASET);
671 670 if (zhp == NULL) {
672 671 /*
673 672 * The zfs_open failed, return an error.
674 673 */
675 674 be_print_err(gettext("be_destroy_snapshot: "
676 675 "failed to open BE root dataset (%s): %s\n"),
677 676 bt.obe_root_ds, libzfs_error_description(g_zfs));
678 677 err = zfs_err_to_be_err(g_zfs);
679 678 } else {
680 679 /*
681 680 * Generate the name of the snapshot to take.
682 681 */
683 682 (void) snprintf(ss, sizeof (ss), "%s@%s", bt.obe_name,
684 683 bt.obe_snap_name);
685 684
686 685 /*
687 686 * destroy the snapshot.
688 687 */
689 688 /*
690 689 * The boolean set to B_FALSE and passed to zfs_destroy_snaps()
691 690 * tells zfs to process and destroy the snapshots now.
692 691 * Otherwise the call will potentially return where the
693 692 * snapshot isn't actually destroyed yet, and ZFS is waiting
694 693 * until all the references to the snapshot have been
695 694 * released before actually destroying the snapshot.
696 695 */
697 696 if (zfs_destroy_snaps(zhp, bt.obe_snap_name, B_FALSE) != 0) {
698 697 err = zfs_err_to_be_err(g_zfs);
699 698 be_print_err(gettext("be_destroy_snapshot: "
700 699 "failed to destroy snapshot %s: %s\n"), ss,
701 700 libzfs_error_description(g_zfs));
702 701 }
703 702 }
704 703
705 704 ZFS_CLOSE(zhp);
706 705
707 706 return (err);
708 707 }
709 708
710 709 /* ******************************************************************** */
711 710 /* Private Functions */
712 711 /* ******************************************************************** */
713 712
714 713 /*
715 714 * Function: be_rollback_check_callback
716 715 * Description: Callback function used to iterate through a BE's filesystems
717 716 * to check if a given snapshot name exists.
718 717 * Parameters:
719 718 * zhp - zfs_handle_t pointer to filesystem being processed.
720 719 * data - name of the snapshot to check for.
721 720 * Returns:
722 721 * 0 - Success, snapshot name exists for all filesystems.
723 722 * be_errno_t - Failure, snapshot name does not exist for all
724 723 * filesystems.
725 724 * Scope:
726 725 * Private
727 726 */
728 727 static int
729 728 be_rollback_check_callback(zfs_handle_t *zhp, void *data)
730 729 {
731 730 char *snap_name = data;
732 731 char ss[MAXPATHLEN];
733 732 int ret = BE_SUCCESS;
734 733
735 734 /* Generate string for this filesystem's snapshot name */
736 735 (void) snprintf(ss, sizeof (ss), "%s@%s", zfs_get_name(zhp), snap_name);
737 736
738 737 /* Check if snapshot exists */
739 738 if (!zfs_dataset_exists(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) {
740 739 be_print_err(gettext("be_rollback_check_callback: "
741 740 "snapshot does not exist %s\n"), ss);
742 741 ZFS_CLOSE(zhp);
743 742 return (BE_ERR_SS_NOENT);
744 743 }
745 744
746 745 /* Iterate this dataset's children and check them */
747 746 if ((ret = zfs_iter_filesystems(zhp, be_rollback_check_callback,
748 747 snap_name)) != 0) {
749 748 ZFS_CLOSE(zhp);
750 749 return (ret);
751 750 }
752 751
753 752 ZFS_CLOSE(zhp);
754 753 return (0);
755 754 }
756 755
757 756 /*
758 757 * Function: be_rollback_callback
759 758 * Description: Callback function used to iterate through a BE's filesystems
760 759 * and roll them all back to the specified snapshot name.
761 760 * Parameters:
762 761 * zhp - zfs_handle_t pointer to filesystem being processed.
763 762 * data - name of snapshot to rollback to.
764 763 * Returns:
765 764 * 0 - Success
766 765 * be_errno_t - Failure
767 766 * Scope:
768 767 * Private
769 768 */
770 769 static int
771 770 be_rollback_callback(zfs_handle_t *zhp, void *data)
772 771 {
773 772 zfs_handle_t *zhp_snap = NULL;
774 773 char *snap_name = data;
775 774 char ss[MAXPATHLEN];
776 775 int ret = 0;
777 776
778 777 /* Generate string for this filesystem's snapshot name */
779 778 (void) snprintf(ss, sizeof (ss), "%s@%s", zfs_get_name(zhp), snap_name);
780 779
781 780 /* Get handle to this filesystem's snapshot */
782 781 if ((zhp_snap = zfs_open(g_zfs, ss, ZFS_TYPE_SNAPSHOT)) == NULL) {
783 782 be_print_err(gettext("be_rollback_callback: "
784 783 "failed to open snapshot %s: %s\n"), zfs_get_name(zhp),
785 784 libzfs_error_description(g_zfs));
786 785 ret = zfs_err_to_be_err(g_zfs);
787 786 ZFS_CLOSE(zhp);
788 787 return (ret);
789 788 }
790 789
791 790 /* Rollback dataset */
792 791 if (zfs_rollback(zhp, zhp_snap, B_FALSE) != 0) {
793 792 be_print_err(gettext("be_rollback_callback: "
794 793 "failed to rollback BE dataset %s to snapshot %s: %s\n"),
795 794 zfs_get_name(zhp), ss, libzfs_error_description(g_zfs));
796 795 ret = zfs_err_to_be_err(g_zfs);
797 796 ZFS_CLOSE(zhp_snap);
798 797 ZFS_CLOSE(zhp);
799 798 return (ret);
800 799 }
801 800
802 801 ZFS_CLOSE(zhp_snap);
803 802 /* Iterate this dataset's children and roll them back */
804 803 if ((ret = zfs_iter_filesystems(zhp, be_rollback_callback,
805 804 snap_name)) != 0) {
806 805 ZFS_CLOSE(zhp);
807 806 return (ret);
808 807 }
809 808
810 809 ZFS_CLOSE(zhp);
811 810 return (0);
812 811 }
↓ open down ↓ |
192 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX