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