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