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