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