Print this page
2882 implement libzfs_core
2883 changing "canmount" property to "on" should not always remount dataset
2900 "zfs snapshot" should be able to create multiple, arbitrary snapshots at once
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Chris Siden <christopher.siden@delphix.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Bill Pijewski <wdp@joyent.com>
Reviewed by: Dan Kruchinin <dan.kruchinin@gmail.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libzfs/common/libzfs_iter.c
+++ new/usr/src/lib/libzfs/common/libzfs_iter.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.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
25 - * Copyright (c) 2011 by Delphix. All rights reserved.
25 + * Copyright (c) 2012 by Delphix. All rights reserved.
26 26 */
27 27
28 28 #include <stdio.h>
29 29 #include <stdlib.h>
30 30 #include <strings.h>
31 31 #include <unistd.h>
32 32 #include <stddef.h>
33 33 #include <libintl.h>
34 34 #include <libzfs.h>
35 35
36 36 #include "libzfs_impl.h"
37 37
38 38 int
39 39 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
40 40 {
41 41 nvlist_t *nvl = zfs_get_clones_nvl(zhp);
42 42 nvpair_t *pair;
43 43
44 44 if (nvl == NULL)
45 45 return (0);
46 46
47 47 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
48 48 pair = nvlist_next_nvpair(nvl, pair)) {
49 49 zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
50 50 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
51 51 if (clone != NULL) {
52 52 int err = func(clone, data);
53 53 if (err != 0)
54 54 return (err);
55 55 }
56 56 }
57 57 return (0);
58 58 }
59 59
60 60 static int
61 61 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
62 62 {
63 63 int rc;
64 64 uint64_t orig_cookie;
65 65
66 66 orig_cookie = zc->zc_cookie;
67 67 top:
68 68 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
69 69 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
70 70
71 71 if (rc == -1) {
72 72 switch (errno) {
73 73 case ENOMEM:
74 74 /* expand nvlist memory and try again */
75 75 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
76 76 zcmd_free_nvlists(zc);
77 77 return (-1);
78 78 }
79 79 zc->zc_cookie = orig_cookie;
80 80 goto top;
81 81 /*
82 82 * An errno value of ESRCH indicates normal completion.
83 83 * If ENOENT is returned, then the underlying dataset
84 84 * has been removed since we obtained the handle.
85 85 */
86 86 case ESRCH:
87 87 case ENOENT:
88 88 rc = 1;
89 89 break;
90 90 default:
91 91 rc = zfs_standard_error(zhp->zfs_hdl, errno,
92 92 dgettext(TEXT_DOMAIN,
93 93 "cannot iterate filesystems"));
94 94 break;
95 95 }
96 96 }
97 97 return (rc);
98 98 }
99 99
100 100 /*
101 101 * Iterate over all child filesystems
102 102 */
103 103 int
104 104 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
105 105 {
106 106 zfs_cmd_t zc = { 0 };
107 107 zfs_handle_t *nzhp;
108 108 int ret;
109 109
110 110 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
111 111 return (0);
112 112
113 113 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
114 114 return (-1);
115 115
116 116 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
117 117 &zc)) == 0) {
118 118 /*
119 119 * Silently ignore errors, as the only plausible explanation is
120 120 * that the pool has since been removed.
121 121 */
122 122 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
123 123 &zc)) == NULL) {
124 124 continue;
125 125 }
126 126
127 127 if ((ret = func(nzhp, data)) != 0) {
128 128 zcmd_free_nvlists(&zc);
129 129 return (ret);
130 130 }
131 131 }
132 132 zcmd_free_nvlists(&zc);
133 133 return ((ret < 0) ? ret : 0);
134 134 }
135 135
136 136 /*
137 137 * Iterate over all snapshots
138 138 */
139 139 int
140 140 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
141 141 {
142 142 zfs_cmd_t zc = { 0 };
143 143 zfs_handle_t *nzhp;
144 144 int ret;
145 145
146 146 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
147 147 return (0);
148 148
149 149 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
150 150 return (-1);
151 151 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
152 152 &zc)) == 0) {
153 153
154 154 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
155 155 &zc)) == NULL) {
156 156 continue;
157 157 }
158 158
159 159 if ((ret = func(nzhp, data)) != 0) {
160 160 zcmd_free_nvlists(&zc);
161 161 return (ret);
162 162 }
163 163 }
164 164 zcmd_free_nvlists(&zc);
165 165 return ((ret < 0) ? ret : 0);
166 166 }
167 167
168 168 /*
169 169 * Routines for dealing with the sorted snapshot functionality
170 170 */
171 171 typedef struct zfs_node {
172 172 zfs_handle_t *zn_handle;
173 173 avl_node_t zn_avlnode;
174 174 } zfs_node_t;
175 175
176 176 static int
177 177 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
178 178 {
179 179 avl_tree_t *avl = data;
180 180 zfs_node_t *node;
181 181 zfs_node_t search;
182 182
183 183 search.zn_handle = zhp;
184 184 node = avl_find(avl, &search, NULL);
185 185 if (node) {
186 186 /*
187 187 * If this snapshot was renamed while we were creating the
188 188 * AVL tree, it's possible that we already inserted it under
189 189 * its old name. Remove the old handle before adding the new
190 190 * one.
191 191 */
192 192 zfs_close(node->zn_handle);
193 193 avl_remove(avl, node);
194 194 free(node);
195 195 }
196 196
197 197 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
198 198 node->zn_handle = zhp;
199 199 avl_add(avl, node);
200 200
201 201 return (0);
202 202 }
203 203
204 204 static int
205 205 zfs_snapshot_compare(const void *larg, const void *rarg)
206 206 {
207 207 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
208 208 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
209 209 uint64_t lcreate, rcreate;
210 210
211 211 /*
212 212 * Sort them according to creation time. We use the hidden
213 213 * CREATETXG property to get an absolute ordering of snapshots.
214 214 */
215 215 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
216 216 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
217 217
218 218 if (lcreate < rcreate)
219 219 return (-1);
220 220 else if (lcreate > rcreate)
221 221 return (+1);
222 222 else
223 223 return (0);
224 224 }
225 225
226 226 int
227 227 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
228 228 {
229 229 int ret = 0;
230 230 zfs_node_t *node;
231 231 avl_tree_t avl;
232 232 void *cookie = NULL;
233 233
234 234 avl_create(&avl, zfs_snapshot_compare,
235 235 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
236 236
237 237 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
238 238
239 239 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
240 240 ret |= callback(node->zn_handle, data);
241 241
242 242 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
243 243 free(node);
244 244
245 245 avl_destroy(&avl);
246 246
247 247 return (ret);
248 248 }
249 249
250 250 typedef struct {
251 251 char *ssa_first;
252 252 char *ssa_last;
253 253 boolean_t ssa_seenfirst;
254 254 boolean_t ssa_seenlast;
255 255 zfs_iter_f ssa_func;
256 256 void *ssa_arg;
257 257 } snapspec_arg_t;
258 258
259 259 static int
260 260 snapspec_cb(zfs_handle_t *zhp, void *arg) {
261 261 snapspec_arg_t *ssa = arg;
262 262 char *shortsnapname;
263 263 int err = 0;
264 264
265 265 if (ssa->ssa_seenlast)
266 266 return (0);
267 267 shortsnapname = zfs_strdup(zhp->zfs_hdl,
268 268 strchr(zfs_get_name(zhp), '@') + 1);
269 269
270 270 if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
271 271 ssa->ssa_seenfirst = B_TRUE;
272 272
273 273 if (ssa->ssa_seenfirst) {
274 274 err = ssa->ssa_func(zhp, ssa->ssa_arg);
275 275 } else {
276 276 zfs_close(zhp);
277 277 }
278 278
279 279 if (strcmp(shortsnapname, ssa->ssa_last) == 0)
280 280 ssa->ssa_seenlast = B_TRUE;
281 281 free(shortsnapname);
282 282
283 283 return (err);
284 284 }
285 285
286 286 /*
287 287 * spec is a string like "A,B%C,D"
288 288 *
289 289 * <snaps>, where <snaps> can be:
290 290 * <snap> (single snapshot)
291 291 * <snap>%<snap> (range of snapshots, inclusive)
292 292 * %<snap> (range of snapshots, starting with earliest)
293 293 * <snap>% (range of snapshots, ending with last)
↓ open down ↓ |
258 lines elided |
↑ open up ↑ |
294 294 * % (all snapshots)
295 295 * <snaps>[,...] (comma separated list of the above)
296 296 *
297 297 * If a snapshot can not be opened, continue trying to open the others, but
298 298 * return ENOENT at the end.
299 299 */
300 300 int
301 301 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
302 302 zfs_iter_f func, void *arg)
303 303 {
304 - char buf[ZFS_MAXNAMELEN];
305 - char *comma_separated, *cp;
304 + char *buf, *comma_separated, *cp;
306 305 int err = 0;
307 306 int ret = 0;
308 307
309 - (void) strlcpy(buf, spec_orig, sizeof (buf));
308 + buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
310 309 cp = buf;
311 310
312 311 while ((comma_separated = strsep(&cp, ",")) != NULL) {
313 312 char *pct = strchr(comma_separated, '%');
314 313 if (pct != NULL) {
315 314 snapspec_arg_t ssa = { 0 };
316 315 ssa.ssa_func = func;
317 316 ssa.ssa_arg = arg;
318 317
319 318 if (pct == comma_separated)
320 319 ssa.ssa_seenfirst = B_TRUE;
321 320 else
322 321 ssa.ssa_first = comma_separated;
323 322 *pct = '\0';
324 323 ssa.ssa_last = pct + 1;
325 324
326 325 /*
327 326 * If there is a lastname specified, make sure it
328 327 * exists.
329 328 */
330 329 if (ssa.ssa_last[0] != '\0') {
331 330 char snapname[ZFS_MAXNAMELEN];
332 331 (void) snprintf(snapname, sizeof (snapname),
333 332 "%s@%s", zfs_get_name(fs_zhp),
334 333 ssa.ssa_last);
335 334 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
336 335 snapname, ZFS_TYPE_SNAPSHOT)) {
337 336 ret = ENOENT;
338 337 continue;
339 338 }
340 339 }
341 340
342 341 err = zfs_iter_snapshots_sorted(fs_zhp,
343 342 snapspec_cb, &ssa);
344 343 if (ret == 0)
345 344 ret = err;
346 345 if (ret == 0 && (!ssa.ssa_seenfirst ||
347 346 (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
348 347 ret = ENOENT;
349 348 }
350 349 } else {
351 350 char snapname[ZFS_MAXNAMELEN];
352 351 zfs_handle_t *snap_zhp;
353 352 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
354 353 zfs_get_name(fs_zhp), comma_separated);
355 354 snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
356 355 snapname);
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
357 356 if (snap_zhp == NULL) {
358 357 ret = ENOENT;
359 358 continue;
360 359 }
361 360 err = func(snap_zhp, arg);
362 361 if (ret == 0)
363 362 ret = err;
364 363 }
365 364 }
366 365
366 + free(buf);
367 367 return (ret);
368 368 }
369 369
370 370 /*
371 371 * Iterate over all children, snapshots and filesystems
372 372 */
373 373 int
374 374 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
375 375 {
376 376 int ret;
377 377
378 378 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
379 379 return (ret);
380 380
381 381 return (zfs_iter_snapshots(zhp, func, data));
382 382 }
383 383
384 384
385 385 typedef struct iter_stack_frame {
386 386 struct iter_stack_frame *next;
387 387 zfs_handle_t *zhp;
388 388 } iter_stack_frame_t;
389 389
390 390 typedef struct iter_dependents_arg {
391 391 boolean_t first;
392 392 boolean_t allowrecursion;
393 393 iter_stack_frame_t *stack;
394 394 zfs_iter_f func;
395 395 void *data;
396 396 } iter_dependents_arg_t;
397 397
398 398 static int
399 399 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
400 400 {
401 401 iter_dependents_arg_t *ida = arg;
402 402 int err;
403 403 boolean_t first = ida->first;
404 404 ida->first = B_FALSE;
405 405
406 406 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
407 407 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
408 408 } else {
409 409 iter_stack_frame_t isf;
410 410 iter_stack_frame_t *f;
411 411
412 412 /*
413 413 * check if there is a cycle by seeing if this fs is already
414 414 * on the stack.
415 415 */
416 416 for (f = ida->stack; f != NULL; f = f->next) {
417 417 if (f->zhp->zfs_dmustats.dds_guid ==
418 418 zhp->zfs_dmustats.dds_guid) {
419 419 if (ida->allowrecursion) {
420 420 zfs_close(zhp);
421 421 return (0);
422 422 } else {
423 423 zfs_error_aux(zhp->zfs_hdl,
424 424 dgettext(TEXT_DOMAIN,
425 425 "recursive dependency at '%s'"),
426 426 zfs_get_name(zhp));
427 427 err = zfs_error(zhp->zfs_hdl,
428 428 EZFS_RECURSIVE,
429 429 dgettext(TEXT_DOMAIN,
430 430 "cannot determine dependent "
431 431 "datasets"));
432 432 zfs_close(zhp);
433 433 return (err);
434 434 }
435 435 }
436 436 }
437 437
438 438 isf.zhp = zhp;
439 439 isf.next = ida->stack;
440 440 ida->stack = &isf;
441 441 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
442 442 if (err == 0)
443 443 err = zfs_iter_snapshots(zhp, iter_dependents_cb, ida);
444 444 ida->stack = isf.next;
445 445 }
446 446 if (!first && err == 0)
447 447 err = ida->func(zhp, ida->data);
448 448 return (err);
449 449 }
450 450
451 451 int
452 452 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
453 453 zfs_iter_f func, void *data)
454 454 {
455 455 iter_dependents_arg_t ida;
456 456 ida.allowrecursion = allowrecursion;
457 457 ida.stack = NULL;
458 458 ida.func = func;
459 459 ida.data = data;
460 460 ida.first = B_TRUE;
461 461 return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
462 462 }
↓ open down ↓ |
86 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX