]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_diff.c
MFC r348005:
[FreeBSD/FreeBSD.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 (c) 2015, 2017 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 <pthread.h>
45 #include <sys/zfs_ioctl.h>
46 #include <libzfs.h>
47 #include "libzfs_impl.h"
48
49 #define ZDIFF_SNAPDIR           "/.zfs/snapshot/"
50 #define ZDIFF_SHARESDIR         "/.zfs/shares/"
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 typedef struct differ_info {
59         zfs_handle_t *zhp;
60         char *fromsnap;
61         char *frommnt;
62         char *tosnap;
63         char *tomnt;
64         char *ds;
65         char *dsmnt;
66         char *tmpsnap;
67         char errbuf[1024];
68         boolean_t isclone;
69         boolean_t scripted;
70         boolean_t classify;
71         boolean_t timestamped;
72         uint64_t shares;
73         int zerr;
74         int cleanupfd;
75         int outputfd;
76         int datafd;
77 } differ_info_t;
78
79 /*
80  * Given a {dsname, object id}, get the object path
81  */
82 static int
83 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
84     char *pn, int maxlen, zfs_stat_t *sb)
85 {
86         zfs_cmd_t zc = { 0 };
87         int error;
88
89         (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
90         zc.zc_obj = obj;
91
92         errno = 0;
93         error = ioctl(di->zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_STATS, &zc);
94         di->zerr = errno;
95
96         /* we can get stats even if we failed to get a path */
97         (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
98         if (error == 0) {
99                 ASSERT(di->zerr == 0);
100                 (void) strlcpy(pn, zc.zc_value, maxlen);
101                 return (0);
102         }
103
104         if (di->zerr == ESTALE) {
105                 (void) snprintf(pn, maxlen, "(on_delete_queue)");
106                 return (0);
107         } else if (di->zerr == EPERM) {
108                 (void) snprintf(di->errbuf, sizeof (di->errbuf),
109                     dgettext(TEXT_DOMAIN,
110                     "The sys_config privilege or diff delegated permission "
111                     "is needed\nto discover path names"));
112                 return (-1);
113         } else {
114                 (void) snprintf(di->errbuf, sizeof (di->errbuf),
115                     dgettext(TEXT_DOMAIN,
116                     "Unable to determine path or stats for "
117                     "object %jd in %s"), (uintmax_t)obj, dsname);
118                 return (-1);
119         }
120 }
121
122 /*
123  * stream_bytes
124  *
125  * Prints a file name out a character at a time.  If the character is
126  * not in the range of what we consider "printable" ASCII, display it
127  * as an escaped 3-digit octal value.  ASCII values less than a space
128  * are all control characters and we declare the upper end as the
129  * DELete character.  This also is the last 7-bit ASCII character.
130  * We choose to treat all 8-bit ASCII as not printable for this
131  * application.
132  */
133 static void
134 stream_bytes(FILE *fp, const char *string)
135 {
136         char c;
137
138         while ((c = *string++) != '\0') {
139                 if (c > ' ' && c != '\\' && c < '\177') {
140                         (void) fprintf(fp, "%c", c);
141                 } else {
142                         (void) fprintf(fp, "\\%03o", (uint8_t)c);
143                 }
144         }
145 }
146
147 static void
148 print_what(FILE *fp, mode_t what)
149 {
150         char symbol;
151
152         switch (what & S_IFMT) {
153         case S_IFBLK:
154                 symbol = 'B';
155                 break;
156         case S_IFCHR:
157                 symbol = 'C';
158                 break;
159         case S_IFDIR:
160                 symbol = '/';
161                 break;
162 #ifdef S_IFDOOR
163         case S_IFDOOR:
164                 symbol = '>';
165                 break;
166 #endif
167         case S_IFIFO:
168                 symbol = '|';
169                 break;
170         case S_IFLNK:
171                 symbol = '@';
172                 break;
173 #ifdef S_IFPORT
174         case S_IFPORT:
175                 symbol = 'P';
176                 break;
177 #endif
178         case S_IFSOCK:
179                 symbol = '=';
180                 break;
181         case S_IFREG:
182                 symbol = 'F';
183                 break;
184         default:
185                 symbol = '?';
186                 break;
187         }
188         (void) fprintf(fp, "%c", symbol);
189 }
190
191 static void
192 print_cmn(FILE *fp, differ_info_t *di, const char *file)
193 {
194         stream_bytes(fp, di->dsmnt);
195         stream_bytes(fp, file);
196 }
197
198 static void
199 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
200     zfs_stat_t *isb)
201 {
202         if (di->timestamped)
203                 (void) fprintf(fp, "%10lld.%09lld\t",
204                     (longlong_t)isb->zs_ctime[0],
205                     (longlong_t)isb->zs_ctime[1]);
206         (void) fprintf(fp, "%c\t", ZDIFF_RENAMED);
207         if (di->classify) {
208                 print_what(fp, isb->zs_mode);
209                 (void) fprintf(fp, "\t");
210         }
211         print_cmn(fp, di, old);
212         if (di->scripted)
213                 (void) fprintf(fp, "\t");
214         else
215                 (void) fprintf(fp, " -> ");
216         print_cmn(fp, di, new);
217         (void) fprintf(fp, "\n");
218 }
219
220 static void
221 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
222     zfs_stat_t *isb)
223 {
224         if (di->timestamped)
225                 (void) fprintf(fp, "%10lld.%09lld\t",
226                     (longlong_t)isb->zs_ctime[0],
227                     (longlong_t)isb->zs_ctime[1]);
228         (void) fprintf(fp, "%c\t", ZDIFF_MODIFIED);
229         if (di->classify) {
230                 print_what(fp, isb->zs_mode);
231                 (void) fprintf(fp, "\t");
232         }
233         print_cmn(fp, di, file);
234         (void) fprintf(fp, "\t(%+d)", delta);
235         (void) fprintf(fp, "\n");
236 }
237
238 static void
239 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
240     zfs_stat_t *isb)
241 {
242         if (di->timestamped)
243                 (void) fprintf(fp, "%10lld.%09lld\t",
244                     (longlong_t)isb->zs_ctime[0],
245                     (longlong_t)isb->zs_ctime[1]);
246         (void) fprintf(fp, "%c\t", type);
247         if (di->classify) {
248                 print_what(fp, isb->zs_mode);
249                 (void) fprintf(fp, "\t");
250         }
251         print_cmn(fp, di, file);
252         (void) fprintf(fp, "\n");
253 }
254
255 static int
256 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
257 {
258         struct zfs_stat fsb, tsb;
259         mode_t fmode, tmode;
260         char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
261         int fobjerr, tobjerr;
262         int change;
263
264         if (dobj == di->shares)
265                 return (0);
266
267         /*
268          * Check the from and to snapshots for info on the object. If
269          * we get ENOENT, then the object just didn't exist in that
270          * snapshot.  If we get ENOTSUP, then we tried to get
271          * info on a non-ZPL object, which we don't care about anyway.
272          */
273         fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
274             MAXPATHLEN, &fsb);
275         if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
276                 return (-1);
277
278         tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
279             MAXPATHLEN, &tsb);
280         if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
281                 return (-1);
282
283         /*
284          * Unallocated object sharing the same meta dnode block
285          */
286         if (fobjerr && tobjerr) {
287                 ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
288                 di->zerr = 0;
289                 return (0);
290         }
291
292         di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
293         fmode = fsb.zs_mode & S_IFMT;
294         tmode = tsb.zs_mode & S_IFMT;
295         if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
296             tsb.zs_links == 0)
297                 change = 0;
298         else
299                 change = tsb.zs_links - fsb.zs_links;
300
301         if (fobjerr) {
302                 if (change) {
303                         print_link_change(fp, di, change, tobjname, &tsb);
304                         return (0);
305                 }
306                 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
307                 return (0);
308         } else if (tobjerr) {
309                 if (change) {
310                         print_link_change(fp, di, change, fobjname, &fsb);
311                         return (0);
312                 }
313                 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
314                 return (0);
315         }
316
317         if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
318                 tsb.zs_gen++;   /* Force a generational difference */
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 (strcmp(fobjname, tobjname) == 0) {
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)) != 0)
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 (> %jd) find failure"),
410                             (uintmax_t)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 #ifdef illumos
495                 (void) snprintf(di->errbuf, sizeof (di->errbuf),
496                     dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath);
497                 return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf));
498 #else
499                 return (0);
500 #endif
501         }
502
503         di->shares = (uint64_t)sb.st_ino;
504         return (0);
505 }
506
507 static int
508 make_temp_snapshot(differ_info_t *di)
509 {
510         libzfs_handle_t *hdl = di->zhp->zfs_hdl;
511         zfs_cmd_t zc = { 0 };
512
513         (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
514             ZDIFF_PREFIX, getpid());
515         (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
516         zc.zc_cleanup_fd = di->cleanupfd;
517
518         if (ioctl(hdl->libzfs_fd, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
519                 int err = errno;
520                 if (err == EPERM) {
521                         (void) snprintf(di->errbuf, sizeof (di->errbuf),
522                             dgettext(TEXT_DOMAIN, "The diff delegated "
523                             "permission is needed in order\nto create a "
524                             "just-in-time snapshot for diffing\n"));
525                         return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
526                 } else {
527                         (void) snprintf(di->errbuf, sizeof (di->errbuf),
528                             dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
529                             "snapshot of '%s'"), zc.zc_name);
530                         return (zfs_standard_error(hdl, err, di->errbuf));
531                 }
532         }
533
534         di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
535         di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
536         return (0);
537 }
538
539 static void
540 teardown_differ_info(differ_info_t *di)
541 {
542         free(di->ds);
543         free(di->dsmnt);
544         free(di->fromsnap);
545         free(di->frommnt);
546         free(di->tosnap);
547         free(di->tmpsnap);
548         free(di->tomnt);
549         (void) close(di->cleanupfd);
550 }
551
552 static int
553 get_snapshot_names(differ_info_t *di, const char *fromsnap,
554     const char *tosnap)
555 {
556         libzfs_handle_t *hdl = di->zhp->zfs_hdl;
557         char *atptrf = NULL;
558         char *atptrt = NULL;
559         int fdslen, fsnlen;
560         int tdslen, tsnlen;
561
562         /*
563          * Can accept
564          *    dataset@snap1
565          *    dataset@snap1 dataset@snap2
566          *    dataset@snap1 @snap2
567          *    dataset@snap1 dataset
568          *    @snap1 dataset@snap2
569          */
570         if (tosnap == NULL) {
571                 /* only a from snapshot given, must be valid */
572                 (void) snprintf(di->errbuf, sizeof (di->errbuf),
573                     dgettext(TEXT_DOMAIN,
574                     "Badly formed snapshot name %s"), fromsnap);
575
576                 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
577                     B_FALSE)) {
578                         return (zfs_error(hdl, EZFS_INVALIDNAME,
579                             di->errbuf));
580                 }
581
582                 atptrf = strchr(fromsnap, '@');
583                 ASSERT(atptrf != NULL);
584                 fdslen = atptrf - fromsnap;
585
586                 di->fromsnap = zfs_strdup(hdl, fromsnap);
587                 di->ds = zfs_strdup(hdl, fromsnap);
588                 di->ds[fdslen] = '\0';
589
590                 /* the to snap will be a just-in-time snap of the head */
591                 return (make_temp_snapshot(di));
592         }
593
594         (void) snprintf(di->errbuf, sizeof (di->errbuf),
595             dgettext(TEXT_DOMAIN,
596             "Unable to determine which snapshots to compare"));
597
598         atptrf = strchr(fromsnap, '@');
599         atptrt = strchr(tosnap, '@');
600         fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
601         tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
602         fsnlen = strlen(fromsnap) - fdslen;     /* includes @ sign */
603         tsnlen = strlen(tosnap) - tdslen;       /* includes @ sign */
604
605         if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0) ||
606             (fsnlen == 0 && tsnlen == 0)) {
607                 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
608         } else if ((fdslen > 0 && tdslen > 0) &&
609             ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
610                 /*
611                  * not the same dataset name, might be okay if
612                  * tosnap is a clone of a fromsnap descendant.
613                  */
614                 char origin[ZFS_MAX_DATASET_NAME_LEN];
615                 zprop_source_t src;
616                 zfs_handle_t *zhp;
617
618                 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
619                 (void) strncpy(di->ds, tosnap, tdslen);
620                 di->ds[tdslen] = '\0';
621
622                 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
623                 while (zhp != NULL) {
624                         if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
625                             sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
626                                 (void) zfs_close(zhp);
627                                 zhp = NULL;
628                                 break;
629                         }
630                         if (strncmp(origin, fromsnap, fsnlen) == 0)
631                                 break;
632
633                         (void) zfs_close(zhp);
634                         zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
635                 }
636
637                 if (zhp == NULL) {
638                         (void) snprintf(di->errbuf, sizeof (di->errbuf),
639                             dgettext(TEXT_DOMAIN,
640                             "Not an earlier snapshot from the same fs"));
641                         return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
642                 } else {
643                         (void) zfs_close(zhp);
644                 }
645
646                 di->isclone = B_TRUE;
647                 di->fromsnap = zfs_strdup(hdl, fromsnap);
648                 if (tsnlen) {
649                         di->tosnap = zfs_strdup(hdl, tosnap);
650                 } else {
651                         return (make_temp_snapshot(di));
652                 }
653         } else {
654                 int dslen = fdslen ? fdslen : tdslen;
655
656                 di->ds = zfs_alloc(hdl, dslen + 1);
657                 (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
658                 di->ds[dslen] = '\0';
659
660                 di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
661                 if (tsnlen) {
662                         di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
663                 } else {
664                         return (make_temp_snapshot(di));
665                 }
666         }
667         return (0);
668 }
669
670 static int
671 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
672 {
673         boolean_t mounted;
674
675         mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
676         if (mounted == B_FALSE) {
677                 (void) snprintf(di->errbuf, sizeof (di->errbuf),
678                     dgettext(TEXT_DOMAIN,
679                     "Cannot diff an unmounted snapshot"));
680                 return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
681         }
682
683         /* Avoid a double slash at the beginning of root-mounted datasets */
684         if (**mntpt == '/' && *(*mntpt + 1) == '\0')
685                 **mntpt = '\0';
686         return (0);
687 }
688
689 static int
690 get_mountpoints(differ_info_t *di)
691 {
692         char *strptr;
693         char *frommntpt;
694
695         /*
696          * first get the mountpoint for the parent dataset
697          */
698         if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
699                 return (-1);
700
701         strptr = strchr(di->tosnap, '@');
702         ASSERT3P(strptr, !=, NULL);
703         di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
704             ZDIFF_SNAPDIR, ++strptr);
705
706         strptr = strchr(di->fromsnap, '@');
707         ASSERT3P(strptr, !=, NULL);
708
709         frommntpt = di->dsmnt;
710         if (di->isclone) {
711                 char *mntpt;
712                 int err;
713
714                 *strptr = '\0';
715                 err = get_mountpoint(di, di->fromsnap, &mntpt);
716                 *strptr = '@';
717                 if (err != 0)
718                         return (-1);
719                 frommntpt = mntpt;
720         }
721
722         di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
723             ZDIFF_SNAPDIR, ++strptr);
724
725         if (di->isclone)
726                 free(frommntpt);
727
728         return (0);
729 }
730
731 static int
732 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
733     const char *tosnap, differ_info_t *di)
734 {
735         di->zhp = zhp;
736
737         di->cleanupfd = open(ZFS_DEV, O_RDWR|O_EXCL);
738         VERIFY(di->cleanupfd >= 0);
739
740         if (get_snapshot_names(di, fromsnap, tosnap) != 0)
741                 return (-1);
742
743         if (get_mountpoints(di) != 0)
744                 return (-1);
745
746         if (find_shares_object(di) != 0)
747                 return (-1);
748
749         return (0);
750 }
751
752 int
753 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
754     const char *tosnap, int flags)
755 {
756         zfs_cmd_t zc = { 0 };
757         char errbuf[1024];
758         differ_info_t di = { 0 };
759         pthread_t tid;
760         int pipefd[2];
761         int iocerr;
762
763         (void) snprintf(errbuf, sizeof (errbuf),
764             dgettext(TEXT_DOMAIN, "zfs diff failed"));
765
766         if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
767                 teardown_differ_info(&di);
768                 return (-1);
769         }
770
771         if (pipe(pipefd)) {
772                 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
773                 teardown_differ_info(&di);
774                 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
775         }
776
777         di.scripted = (flags & ZFS_DIFF_PARSEABLE);
778         di.classify = (flags & ZFS_DIFF_CLASSIFY);
779         di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
780
781         di.outputfd = outfd;
782         di.datafd = pipefd[0];
783
784         if (pthread_create(&tid, NULL, differ, &di)) {
785                 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
786                 (void) close(pipefd[0]);
787                 (void) close(pipefd[1]);
788                 teardown_differ_info(&di);
789                 return (zfs_error(zhp->zfs_hdl,
790                     EZFS_THREADCREATEFAILED, errbuf));
791         }
792
793         /* do the ioctl() */
794         (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
795         (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
796         zc.zc_cookie = pipefd[1];
797
798         iocerr = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DIFF, &zc);
799         if (iocerr != 0) {
800                 (void) snprintf(errbuf, sizeof (errbuf),
801                     dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
802                 if (errno == EPERM) {
803                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
804                             "\n   The sys_mount privilege or diff delegated "
805                             "permission is needed\n   to execute the "
806                             "diff ioctl"));
807                 } else if (errno == EXDEV) {
808                         zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
809                             "\n   Not an earlier snapshot from the same fs"));
810                 } else if (errno != EPIPE || di.zerr == 0) {
811                         zfs_error_aux(zhp->zfs_hdl, strerror(errno));
812                 }
813                 (void) close(pipefd[1]);
814                 (void) pthread_cancel(tid);
815                 (void) pthread_join(tid, NULL);
816                 teardown_differ_info(&di);
817                 if (di.zerr != 0 && di.zerr != EPIPE) {
818                         zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
819                         return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
820                 } else {
821                         return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
822                 }
823         }
824
825         (void) close(pipefd[1]);
826         (void) pthread_join(tid, NULL);
827
828         if (di.zerr != 0) {
829                 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
830                 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
831         }
832         teardown_differ_info(&di);
833         return (0);
834 }