Print this page
5847 libzfs_diff should check zfs_prop_get() return
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libzfs/common/libzfs_diff.c
+++ new/usr/src/lib/libzfs/common/libzfs_diff.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
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
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) 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 25 */
25 26
26 27 /*
27 28 * zfs diff support
28 29 */
29 30 #include <ctype.h>
30 31 #include <errno.h>
31 32 #include <libintl.h>
32 33 #include <string.h>
33 34 #include <sys/types.h>
34 35 #include <sys/stat.h>
35 36 #include <fcntl.h>
36 37 #include <attr.h>
37 38 #include <stddef.h>
38 39 #include <unistd.h>
39 40 #include <stdio.h>
40 41 #include <stdlib.h>
41 42 #include <stropts.h>
42 43 #include <pthread.h>
43 44 #include <sys/zfs_ioctl.h>
44 45 #include <libzfs.h>
45 46 #include "libzfs_impl.h"
46 47
47 48 #define ZDIFF_SNAPDIR "/.zfs/snapshot/"
48 49 #define ZDIFF_SHARESDIR "/.zfs/shares/"
49 50 #define ZDIFF_PREFIX "zfs-diff-%d"
50 51
51 52 #define ZDIFF_ADDED '+'
52 53 #define ZDIFF_MODIFIED 'M'
53 54 #define ZDIFF_REMOVED '-'
54 55 #define ZDIFF_RENAMED 'R'
55 56
56 57 static boolean_t
57 58 do_name_cmp(const char *fpath, const char *tpath)
58 59 {
59 60 char *fname, *tname;
60 61 fname = strrchr(fpath, '/') + 1;
61 62 tname = strrchr(tpath, '/') + 1;
62 63 return (strcmp(fname, tname) == 0);
63 64 }
64 65
65 66 typedef struct differ_info {
66 67 zfs_handle_t *zhp;
67 68 char *fromsnap;
68 69 char *frommnt;
69 70 char *tosnap;
70 71 char *tomnt;
71 72 char *ds;
72 73 char *dsmnt;
73 74 char *tmpsnap;
74 75 char errbuf[1024];
75 76 boolean_t isclone;
76 77 boolean_t scripted;
77 78 boolean_t classify;
78 79 boolean_t timestamped;
79 80 uint64_t shares;
80 81 int zerr;
81 82 int cleanupfd;
82 83 int outputfd;
83 84 int datafd;
84 85 } differ_info_t;
85 86
86 87 /*
87 88 * Given a {dsname, object id}, get the object path
88 89 */
89 90 static int
90 91 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
91 92 char *pn, int maxlen, zfs_stat_t *sb)
92 93 {
93 94 zfs_cmd_t zc = { 0 };
94 95 int error;
95 96
96 97 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
97 98 zc.zc_obj = obj;
98 99
99 100 errno = 0;
100 101 error = ioctl(di->zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_STATS, &zc);
101 102 di->zerr = errno;
102 103
103 104 /* we can get stats even if we failed to get a path */
104 105 (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
105 106 if (error == 0) {
106 107 ASSERT(di->zerr == 0);
107 108 (void) strlcpy(pn, zc.zc_value, maxlen);
108 109 return (0);
109 110 }
110 111
111 112 if (di->zerr == EPERM) {
112 113 (void) snprintf(di->errbuf, sizeof (di->errbuf),
113 114 dgettext(TEXT_DOMAIN,
114 115 "The sys_config privilege or diff delegated permission "
115 116 "is needed\nto discover path names"));
116 117 return (-1);
117 118 } else {
118 119 (void) snprintf(di->errbuf, sizeof (di->errbuf),
119 120 dgettext(TEXT_DOMAIN,
120 121 "Unable to determine path or stats for "
121 122 "object %lld in %s"), obj, dsname);
122 123 return (-1);
123 124 }
124 125 }
125 126
126 127 /*
127 128 * stream_bytes
128 129 *
129 130 * Prints a file name out a character at a time. If the character is
130 131 * not in the range of what we consider "printable" ASCII, display it
131 132 * as an escaped 3-digit octal value. ASCII values less than a space
132 133 * are all control characters and we declare the upper end as the
133 134 * DELete character. This also is the last 7-bit ASCII character.
134 135 * We choose to treat all 8-bit ASCII as not printable for this
135 136 * application.
136 137 */
137 138 static void
138 139 stream_bytes(FILE *fp, const char *string)
139 140 {
140 141 while (*string) {
141 142 if (*string > ' ' && *string != '\\' && *string < '\177')
142 143 (void) fprintf(fp, "%c", *string++);
143 144 else
144 145 (void) fprintf(fp, "\\%03o", *string++);
145 146 }
146 147 }
147 148
148 149 static void
149 150 print_what(FILE *fp, mode_t what)
150 151 {
151 152 char symbol;
152 153
153 154 switch (what & S_IFMT) {
154 155 case S_IFBLK:
155 156 symbol = 'B';
156 157 break;
157 158 case S_IFCHR:
158 159 symbol = 'C';
159 160 break;
160 161 case S_IFDIR:
161 162 symbol = '/';
162 163 break;
163 164 case S_IFDOOR:
164 165 symbol = '>';
165 166 break;
166 167 case S_IFIFO:
167 168 symbol = '|';
168 169 break;
169 170 case S_IFLNK:
170 171 symbol = '@';
171 172 break;
172 173 case S_IFPORT:
173 174 symbol = 'P';
174 175 break;
175 176 case S_IFSOCK:
176 177 symbol = '=';
177 178 break;
178 179 case S_IFREG:
179 180 symbol = 'F';
180 181 break;
181 182 default:
182 183 symbol = '?';
183 184 break;
184 185 }
185 186 (void) fprintf(fp, "%c", symbol);
186 187 }
187 188
188 189 static void
189 190 print_cmn(FILE *fp, differ_info_t *di, const char *file)
190 191 {
191 192 stream_bytes(fp, di->dsmnt);
192 193 stream_bytes(fp, file);
193 194 }
194 195
195 196 static void
196 197 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
197 198 zfs_stat_t *isb)
198 199 {
199 200 if (di->timestamped)
200 201 (void) fprintf(fp, "%10lld.%09lld\t",
201 202 (longlong_t)isb->zs_ctime[0],
202 203 (longlong_t)isb->zs_ctime[1]);
203 204 (void) fprintf(fp, "%c\t", ZDIFF_RENAMED);
204 205 if (di->classify) {
205 206 print_what(fp, isb->zs_mode);
206 207 (void) fprintf(fp, "\t");
207 208 }
208 209 print_cmn(fp, di, old);
209 210 if (di->scripted)
210 211 (void) fprintf(fp, "\t");
211 212 else
212 213 (void) fprintf(fp, " -> ");
213 214 print_cmn(fp, di, new);
214 215 (void) fprintf(fp, "\n");
215 216 }
216 217
217 218 static void
218 219 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
219 220 zfs_stat_t *isb)
220 221 {
221 222 if (di->timestamped)
222 223 (void) fprintf(fp, "%10lld.%09lld\t",
223 224 (longlong_t)isb->zs_ctime[0],
224 225 (longlong_t)isb->zs_ctime[1]);
225 226 (void) fprintf(fp, "%c\t", ZDIFF_MODIFIED);
226 227 if (di->classify) {
227 228 print_what(fp, isb->zs_mode);
228 229 (void) fprintf(fp, "\t");
229 230 }
230 231 print_cmn(fp, di, file);
231 232 (void) fprintf(fp, "\t(%+d)", delta);
232 233 (void) fprintf(fp, "\n");
233 234 }
234 235
235 236 static void
236 237 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
237 238 zfs_stat_t *isb)
238 239 {
239 240 if (di->timestamped)
240 241 (void) fprintf(fp, "%10lld.%09lld\t",
241 242 (longlong_t)isb->zs_ctime[0],
242 243 (longlong_t)isb->zs_ctime[1]);
243 244 (void) fprintf(fp, "%c\t", type);
244 245 if (di->classify) {
245 246 print_what(fp, isb->zs_mode);
246 247 (void) fprintf(fp, "\t");
247 248 }
248 249 print_cmn(fp, di, file);
249 250 (void) fprintf(fp, "\n");
250 251 }
251 252
252 253 static int
253 254 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
254 255 {
255 256 struct zfs_stat fsb, tsb;
256 257 boolean_t same_name;
257 258 mode_t fmode, tmode;
258 259 char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
259 260 int fobjerr, tobjerr;
260 261 int change;
261 262
262 263 if (dobj == di->shares)
263 264 return (0);
264 265
265 266 /*
266 267 * Check the from and to snapshots for info on the object. If
267 268 * we get ENOENT, then the object just didn't exist in that
268 269 * snapshot. If we get ENOTSUP, then we tried to get
269 270 * info on a non-ZPL object, which we don't care about anyway.
270 271 */
271 272 fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
272 273 MAXPATHLEN, &fsb);
273 274 if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
274 275 return (-1);
275 276
276 277 tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
277 278 MAXPATHLEN, &tsb);
278 279 if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
279 280 return (-1);
280 281
281 282 /*
282 283 * Unallocated object sharing the same meta dnode block
283 284 */
284 285 if (fobjerr && tobjerr) {
285 286 ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
286 287 di->zerr = 0;
287 288 return (0);
288 289 }
289 290
290 291 di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
291 292 fmode = fsb.zs_mode & S_IFMT;
292 293 tmode = tsb.zs_mode & S_IFMT;
293 294 if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
294 295 tsb.zs_links == 0)
295 296 change = 0;
296 297 else
297 298 change = tsb.zs_links - fsb.zs_links;
298 299
299 300 if (fobjerr) {
300 301 if (change) {
301 302 print_link_change(fp, di, change, tobjname, &tsb);
302 303 return (0);
303 304 }
304 305 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
305 306 return (0);
306 307 } else if (tobjerr) {
307 308 if (change) {
308 309 print_link_change(fp, di, change, fobjname, &fsb);
309 310 return (0);
310 311 }
311 312 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
312 313 return (0);
313 314 }
314 315
315 316 if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
316 317 tsb.zs_gen++; /* Force a generational difference */
317 318 same_name = do_name_cmp(fobjname, tobjname);
318 319
319 320 /* Simple modification or no change */
320 321 if (fsb.zs_gen == tsb.zs_gen) {
321 322 /* No apparent changes. Could we assert !this? */
322 323 if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
323 324 fsb.zs_ctime[1] == tsb.zs_ctime[1])
324 325 return (0);
325 326 if (change) {
326 327 print_link_change(fp, di, change,
327 328 change > 0 ? fobjname : tobjname, &tsb);
328 329 } else if (same_name) {
329 330 print_file(fp, di, ZDIFF_MODIFIED, fobjname, &tsb);
330 331 } else {
331 332 print_rename(fp, di, fobjname, tobjname, &tsb);
332 333 }
333 334 return (0);
334 335 } else {
335 336 /* file re-created or object re-used */
336 337 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
337 338 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
338 339 return (0);
339 340 }
340 341 }
341 342
342 343 static int
343 344 write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
344 345 {
345 346 uint64_t o;
346 347 int err;
347 348
348 349 for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
349 350 if (err = write_inuse_diffs_one(fp, di, o))
350 351 return (err);
351 352 }
352 353 return (0);
353 354 }
354 355
355 356 static int
356 357 describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
357 358 int maxlen)
358 359 {
359 360 struct zfs_stat sb;
360 361
361 362 if (get_stats_for_obj(di, di->fromsnap, object, namebuf,
362 363 maxlen, &sb) != 0) {
363 364 /* Let it slide, if in the delete queue on from side */
364 365 if (di->zerr == ENOENT && sb.zs_links == 0) {
365 366 di->zerr = 0;
366 367 return (0);
367 368 }
368 369 return (-1);
369 370 }
370 371
371 372 print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
372 373 return (0);
373 374 }
374 375
375 376 static int
376 377 write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
377 378 {
378 379 zfs_cmd_t zc = { 0 };
379 380 libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
380 381 char fobjname[MAXPATHLEN];
381 382
382 383 (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
383 384 zc.zc_obj = dr->ddr_first - 1;
384 385
385 386 ASSERT(di->zerr == 0);
386 387
387 388 while (zc.zc_obj < dr->ddr_last) {
388 389 int err;
389 390
390 391 err = ioctl(lhdl->libzfs_fd, ZFS_IOC_NEXT_OBJ, &zc);
391 392 if (err == 0) {
392 393 if (zc.zc_obj == di->shares) {
393 394 zc.zc_obj++;
394 395 continue;
395 396 }
396 397 if (zc.zc_obj > dr->ddr_last) {
397 398 break;
398 399 }
399 400 err = describe_free(fp, di, zc.zc_obj, fobjname,
400 401 MAXPATHLEN);
401 402 if (err)
402 403 break;
403 404 } else if (errno == ESRCH) {
404 405 break;
405 406 } else {
406 407 (void) snprintf(di->errbuf, sizeof (di->errbuf),
407 408 dgettext(TEXT_DOMAIN,
408 409 "next allocated object (> %lld) find failure"),
409 410 zc.zc_obj);
410 411 di->zerr = errno;
411 412 break;
412 413 }
413 414 }
414 415 if (di->zerr)
415 416 return (-1);
416 417 return (0);
417 418 }
418 419
419 420 static void *
420 421 differ(void *arg)
421 422 {
422 423 differ_info_t *di = arg;
423 424 dmu_diff_record_t dr;
424 425 FILE *ofp;
425 426 int err = 0;
426 427
427 428 if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
428 429 di->zerr = errno;
429 430 (void) strerror_r(errno, di->errbuf, sizeof (di->errbuf));
430 431 (void) close(di->datafd);
431 432 return ((void *)-1);
432 433 }
433 434
434 435 for (;;) {
435 436 char *cp = (char *)&dr;
436 437 int len = sizeof (dr);
437 438 int rv;
438 439
439 440 do {
440 441 rv = read(di->datafd, cp, len);
441 442 cp += rv;
442 443 len -= rv;
443 444 } while (len > 0 && rv > 0);
444 445
445 446 if (rv < 0 || (rv == 0 && len != sizeof (dr))) {
446 447 di->zerr = EPIPE;
447 448 break;
448 449 } else if (rv == 0) {
449 450 /* end of file at a natural breaking point */
450 451 break;
451 452 }
452 453
453 454 switch (dr.ddr_type) {
454 455 case DDR_FREE:
455 456 err = write_free_diffs(ofp, di, &dr);
456 457 break;
457 458 case DDR_INUSE:
458 459 err = write_inuse_diffs(ofp, di, &dr);
459 460 break;
460 461 default:
461 462 di->zerr = EPIPE;
462 463 break;
463 464 }
464 465
465 466 if (err || di->zerr)
466 467 break;
467 468 }
468 469
469 470 (void) fclose(ofp);
470 471 (void) close(di->datafd);
471 472 if (err)
472 473 return ((void *)-1);
473 474 if (di->zerr) {
474 475 ASSERT(di->zerr == EINVAL);
475 476 (void) snprintf(di->errbuf, sizeof (di->errbuf),
476 477 dgettext(TEXT_DOMAIN,
477 478 "Internal error: bad data from diff IOCTL"));
478 479 return ((void *)-1);
479 480 }
480 481 return ((void *)0);
481 482 }
482 483
483 484 static int
484 485 find_shares_object(differ_info_t *di)
485 486 {
486 487 char fullpath[MAXPATHLEN];
487 488 struct stat64 sb = { 0 };
488 489
489 490 (void) strlcpy(fullpath, di->dsmnt, MAXPATHLEN);
490 491 (void) strlcat(fullpath, ZDIFF_SHARESDIR, MAXPATHLEN);
491 492
492 493 if (stat64(fullpath, &sb) != 0) {
493 494 (void) snprintf(di->errbuf, sizeof (di->errbuf),
494 495 dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath);
495 496 return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf));
496 497 }
497 498
498 499 di->shares = (uint64_t)sb.st_ino;
499 500 return (0);
500 501 }
501 502
502 503 static int
503 504 make_temp_snapshot(differ_info_t *di)
504 505 {
505 506 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
506 507 zfs_cmd_t zc = { 0 };
507 508
508 509 (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
509 510 ZDIFF_PREFIX, getpid());
510 511 (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
511 512 zc.zc_cleanup_fd = di->cleanupfd;
512 513
513 514 if (ioctl(hdl->libzfs_fd, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
514 515 int err = errno;
515 516 if (err == EPERM) {
516 517 (void) snprintf(di->errbuf, sizeof (di->errbuf),
517 518 dgettext(TEXT_DOMAIN, "The diff delegated "
518 519 "permission is needed in order\nto create a "
519 520 "just-in-time snapshot for diffing\n"));
520 521 return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
521 522 } else {
522 523 (void) snprintf(di->errbuf, sizeof (di->errbuf),
523 524 dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
524 525 "snapshot of '%s'"), zc.zc_name);
525 526 return (zfs_standard_error(hdl, err, di->errbuf));
526 527 }
527 528 }
528 529
529 530 di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
530 531 di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
531 532 return (0);
532 533 }
533 534
534 535 static void
535 536 teardown_differ_info(differ_info_t *di)
536 537 {
537 538 free(di->ds);
538 539 free(di->dsmnt);
539 540 free(di->fromsnap);
540 541 free(di->frommnt);
541 542 free(di->tosnap);
542 543 free(di->tmpsnap);
543 544 free(di->tomnt);
544 545 (void) close(di->cleanupfd);
545 546 }
546 547
547 548 static int
548 549 get_snapshot_names(differ_info_t *di, const char *fromsnap,
549 550 const char *tosnap)
550 551 {
551 552 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
552 553 char *atptrf = NULL;
553 554 char *atptrt = NULL;
554 555 int fdslen, fsnlen;
555 556 int tdslen, tsnlen;
556 557
557 558 /*
558 559 * Can accept
559 560 * dataset@snap1
560 561 * dataset@snap1 dataset@snap2
561 562 * dataset@snap1 @snap2
562 563 * dataset@snap1 dataset
563 564 * @snap1 dataset@snap2
564 565 */
565 566 if (tosnap == NULL) {
566 567 /* only a from snapshot given, must be valid */
567 568 (void) snprintf(di->errbuf, sizeof (di->errbuf),
568 569 dgettext(TEXT_DOMAIN,
569 570 "Badly formed snapshot name %s"), fromsnap);
570 571
571 572 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
572 573 B_FALSE)) {
573 574 return (zfs_error(hdl, EZFS_INVALIDNAME,
574 575 di->errbuf));
575 576 }
576 577
577 578 atptrf = strchr(fromsnap, '@');
578 579 ASSERT(atptrf != NULL);
579 580 fdslen = atptrf - fromsnap;
580 581
581 582 di->fromsnap = zfs_strdup(hdl, fromsnap);
582 583 di->ds = zfs_strdup(hdl, fromsnap);
583 584 di->ds[fdslen] = '\0';
584 585
585 586 /* the to snap will be a just-in-time snap of the head */
586 587 return (make_temp_snapshot(di));
587 588 }
588 589
589 590 (void) snprintf(di->errbuf, sizeof (di->errbuf),
590 591 dgettext(TEXT_DOMAIN,
591 592 "Unable to determine which snapshots to compare"));
592 593
593 594 atptrf = strchr(fromsnap, '@');
594 595 atptrt = strchr(tosnap, '@');
595 596 fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
596 597 tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
597 598 fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
598 599 tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
599 600
600 601 if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0) ||
601 602 (fsnlen == 0 && tsnlen == 0)) {
602 603 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
603 604 } else if ((fdslen > 0 && tdslen > 0) &&
604 605 ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
605 606 /*
606 607 * not the same dataset name, might be okay if
607 608 * tosnap is a clone of a fromsnap descendant.
608 609 */
↓ open down ↓ |
575 lines elided |
↑ open up ↑ |
609 610 char origin[ZFS_MAXNAMELEN];
610 611 zprop_source_t src;
611 612 zfs_handle_t *zhp;
612 613
613 614 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
614 615 (void) strncpy(di->ds, tosnap, tdslen);
615 616 di->ds[tdslen] = '\0';
616 617
617 618 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
618 619 while (zhp != NULL) {
619 - (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
620 - origin, sizeof (origin), &src, NULL, 0, B_FALSE);
621 -
620 + if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
621 + sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
622 + (void) zfs_close(zhp);
623 + zhp = NULL;
624 + break;
625 + }
622 626 if (strncmp(origin, fromsnap, fsnlen) == 0)
623 627 break;
624 628
625 629 (void) zfs_close(zhp);
626 630 zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
627 631 }
628 632
629 633 if (zhp == NULL) {
630 634 (void) snprintf(di->errbuf, sizeof (di->errbuf),
631 635 dgettext(TEXT_DOMAIN,
632 636 "Not an earlier snapshot from the same fs"));
633 637 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
634 638 } else {
635 639 (void) zfs_close(zhp);
636 640 }
637 641
638 642 di->isclone = B_TRUE;
639 643 di->fromsnap = zfs_strdup(hdl, fromsnap);
640 644 if (tsnlen) {
641 645 di->tosnap = zfs_strdup(hdl, tosnap);
642 646 } else {
643 647 return (make_temp_snapshot(di));
644 648 }
645 649 } else {
646 650 int dslen = fdslen ? fdslen : tdslen;
647 651
648 652 di->ds = zfs_alloc(hdl, dslen + 1);
649 653 (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
650 654 di->ds[dslen] = '\0';
651 655
652 656 di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
653 657 if (tsnlen) {
654 658 di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
655 659 } else {
656 660 return (make_temp_snapshot(di));
657 661 }
658 662 }
659 663 return (0);
660 664 }
661 665
662 666 static int
663 667 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
664 668 {
665 669 boolean_t mounted;
666 670
667 671 mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
668 672 if (mounted == B_FALSE) {
669 673 (void) snprintf(di->errbuf, sizeof (di->errbuf),
670 674 dgettext(TEXT_DOMAIN,
671 675 "Cannot diff an unmounted snapshot"));
672 676 return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
673 677 }
674 678
675 679 /* Avoid a double slash at the beginning of root-mounted datasets */
676 680 if (**mntpt == '/' && *(*mntpt + 1) == '\0')
677 681 **mntpt = '\0';
678 682 return (0);
679 683 }
680 684
681 685 static int
682 686 get_mountpoints(differ_info_t *di)
683 687 {
684 688 char *strptr;
685 689 char *frommntpt;
686 690
687 691 /*
688 692 * first get the mountpoint for the parent dataset
689 693 */
690 694 if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
691 695 return (-1);
692 696
693 697 strptr = strchr(di->tosnap, '@');
694 698 ASSERT3P(strptr, !=, NULL);
695 699 di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
696 700 ZDIFF_SNAPDIR, ++strptr);
697 701
698 702 strptr = strchr(di->fromsnap, '@');
699 703 ASSERT3P(strptr, !=, NULL);
700 704
701 705 frommntpt = di->dsmnt;
702 706 if (di->isclone) {
703 707 char *mntpt;
704 708 int err;
705 709
706 710 *strptr = '\0';
707 711 err = get_mountpoint(di, di->fromsnap, &mntpt);
708 712 *strptr = '@';
709 713 if (err != 0)
710 714 return (-1);
711 715 frommntpt = mntpt;
712 716 }
713 717
714 718 di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
715 719 ZDIFF_SNAPDIR, ++strptr);
716 720
717 721 if (di->isclone)
718 722 free(frommntpt);
719 723
720 724 return (0);
721 725 }
722 726
723 727 static int
724 728 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
725 729 const char *tosnap, differ_info_t *di)
726 730 {
727 731 di->zhp = zhp;
728 732
729 733 di->cleanupfd = open(ZFS_DEV, O_RDWR|O_EXCL);
730 734 VERIFY(di->cleanupfd >= 0);
731 735
732 736 if (get_snapshot_names(di, fromsnap, tosnap) != 0)
733 737 return (-1);
734 738
735 739 if (get_mountpoints(di) != 0)
736 740 return (-1);
737 741
738 742 if (find_shares_object(di) != 0)
739 743 return (-1);
740 744
741 745 return (0);
742 746 }
743 747
744 748 int
745 749 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
746 750 const char *tosnap, int flags)
747 751 {
748 752 zfs_cmd_t zc = { 0 };
749 753 char errbuf[1024];
750 754 differ_info_t di = { 0 };
751 755 pthread_t tid;
752 756 int pipefd[2];
753 757 int iocerr;
754 758
755 759 (void) snprintf(errbuf, sizeof (errbuf),
756 760 dgettext(TEXT_DOMAIN, "zfs diff failed"));
757 761
758 762 if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
759 763 teardown_differ_info(&di);
760 764 return (-1);
761 765 }
762 766
763 767 if (pipe(pipefd)) {
764 768 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
765 769 teardown_differ_info(&di);
766 770 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
767 771 }
768 772
769 773 di.scripted = (flags & ZFS_DIFF_PARSEABLE);
770 774 di.classify = (flags & ZFS_DIFF_CLASSIFY);
771 775 di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
772 776
773 777 di.outputfd = outfd;
774 778 di.datafd = pipefd[0];
775 779
776 780 if (pthread_create(&tid, NULL, differ, &di)) {
777 781 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
778 782 (void) close(pipefd[0]);
779 783 (void) close(pipefd[1]);
780 784 teardown_differ_info(&di);
781 785 return (zfs_error(zhp->zfs_hdl,
782 786 EZFS_THREADCREATEFAILED, errbuf));
783 787 }
784 788
785 789 /* do the ioctl() */
786 790 (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
787 791 (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
788 792 zc.zc_cookie = pipefd[1];
789 793
790 794 iocerr = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DIFF, &zc);
791 795 if (iocerr != 0) {
792 796 (void) snprintf(errbuf, sizeof (errbuf),
793 797 dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
794 798 if (errno == EPERM) {
795 799 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
796 800 "\n The sys_mount privilege or diff delegated "
797 801 "permission is needed\n to execute the "
798 802 "diff ioctl"));
799 803 } else if (errno == EXDEV) {
800 804 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
801 805 "\n Not an earlier snapshot from the same fs"));
802 806 } else if (errno != EPIPE || di.zerr == 0) {
803 807 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
804 808 }
805 809 (void) close(pipefd[1]);
806 810 (void) pthread_cancel(tid);
807 811 (void) pthread_join(tid, NULL);
808 812 teardown_differ_info(&di);
809 813 if (di.zerr != 0 && di.zerr != EPIPE) {
810 814 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
811 815 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
812 816 } else {
813 817 return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
814 818 }
815 819 }
816 820
817 821 (void) close(pipefd[1]);
818 822 (void) pthread_join(tid, NULL);
819 823
820 824 if (di.zerr != 0) {
821 825 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
822 826 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
823 827 }
824 828 teardown_differ_info(&di);
825 829 return (0);
826 830 }
↓ open down ↓ |
195 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX