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