zpool/zfs: restore -V & --version options
[zfs.git] / lib / libzfs / libzfs_diff.c
blobedf290c4d040329c5e40f2b52992ef3d7a3b65dc
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2015, 2018 by Delphix. All rights reserved.
26 * Copyright 2016 Joyent, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31 * zfs diff support
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 <libzutil.h>
48 #include "libzfs_impl.h"
50 #define ZDIFF_SNAPDIR "/.zfs/snapshot/"
51 #define ZDIFF_PREFIX "zfs-diff-%d"
53 #define ZDIFF_ADDED '+'
54 #define ZDIFF_MODIFIED "M"
55 #define ZDIFF_REMOVED '-'
56 #define ZDIFF_RENAMED "R"
58 #define ZDIFF_ADDED_COLOR ANSI_GREEN
59 #define ZDIFF_MODIFIED_COLOR ANSI_YELLOW
60 #define ZDIFF_REMOVED_COLOR ANSI_RED
61 #define ZDIFF_RENAMED_COLOR ANSI_BOLD_BLUE
64 * Given a {dsname, object id}, get the object path
66 static int
67 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
68 char *pn, int maxlen, zfs_stat_t *sb)
70 zfs_cmd_t zc = {"\0"};
71 int error;
73 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
74 zc.zc_obj = obj;
76 errno = 0;
77 error = zfs_ioctl(di->zhp->zfs_hdl, ZFS_IOC_OBJ_TO_STATS, &zc);
78 di->zerr = errno;
80 /* we can get stats even if we failed to get a path */
81 (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
82 if (error == 0) {
83 ASSERT(di->zerr == 0);
84 (void) strlcpy(pn, zc.zc_value, maxlen);
85 return (0);
88 if (di->zerr == ESTALE) {
89 (void) snprintf(pn, maxlen, "(on_delete_queue)");
90 return (0);
91 } else if (di->zerr == EPERM) {
92 (void) snprintf(di->errbuf, sizeof (di->errbuf),
93 dgettext(TEXT_DOMAIN,
94 "The sys_config privilege or diff delegated permission "
95 "is needed\nto discover path names"));
96 return (-1);
97 } else if (di->zerr == EACCES) {
98 (void) snprintf(di->errbuf, sizeof (di->errbuf),
99 dgettext(TEXT_DOMAIN,
100 "Key must be loaded to discover path names"));
101 return (-1);
102 } else {
103 (void) snprintf(di->errbuf, sizeof (di->errbuf),
104 dgettext(TEXT_DOMAIN,
105 "Unable to determine path or stats for "
106 "object %lld in %s"), (longlong_t)obj, dsname);
107 return (-1);
112 * stream_bytes
114 * Prints a file name out a character at a time. If the character is
115 * not in the range of what we consider "printable" ASCII, display it
116 * as an escaped 4-digit octal value. ASCII values less than a space
117 * are all control characters and we declare the upper end as the
118 * DELete character. This also is the last 7-bit ASCII character.
119 * We choose to treat all 8-bit ASCII as not printable for this
120 * application.
122 static void
123 stream_bytes(FILE *fp, const char *string)
125 char c;
127 while ((c = *string++) != '\0') {
128 if (c > ' ' && c != '\\' && c < '\177') {
129 (void) fputc(c, fp);
130 } else {
131 (void) fprintf(fp, "\\%04hho", (uint8_t)c);
137 * Takes the type of change (like `print_file`), outputs the appropriate color
139 static const char *
140 type_to_color(char type)
142 if (type == '+')
143 return (ZDIFF_ADDED_COLOR);
144 else if (type == '-')
145 return (ZDIFF_REMOVED_COLOR);
146 else if (type == 'M')
147 return (ZDIFF_MODIFIED_COLOR);
148 else if (type == 'R')
149 return (ZDIFF_RENAMED_COLOR);
150 else
151 return (NULL);
155 static char
156 get_what(mode_t what)
158 switch (what & S_IFMT) {
159 case S_IFBLK:
160 return ('B');
161 case S_IFCHR:
162 return ('C');
163 case S_IFDIR:
164 return ('/');
165 #ifdef S_IFDOOR
166 case S_IFDOOR:
167 return ('>');
168 #endif
169 case S_IFIFO:
170 return ('|');
171 case S_IFLNK:
172 return ('@');
173 #ifdef S_IFPORT
174 case S_IFPORT:
175 return ('P');
176 #endif
177 case S_IFSOCK:
178 return ('=');
179 case S_IFREG:
180 return ('F');
181 default:
182 return ('?');
186 static void
187 print_cmn(FILE *fp, differ_info_t *di, const char *file)
189 if (!di->no_mangle) {
190 stream_bytes(fp, di->dsmnt);
191 stream_bytes(fp, file);
192 } else {
193 (void) fputs(di->dsmnt, fp);
194 (void) fputs(file, fp);
198 static void
199 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
200 zfs_stat_t *isb)
202 if (isatty(fileno(fp)))
203 color_start(ZDIFF_RENAMED_COLOR);
204 if (di->timestamped)
205 (void) fprintf(fp, "%10lld.%09lld\t",
206 (longlong_t)isb->zs_ctime[0],
207 (longlong_t)isb->zs_ctime[1]);
208 (void) fputs(ZDIFF_RENAMED "\t", fp);
209 if (di->classify)
210 (void) fprintf(fp, "%c\t", get_what(isb->zs_mode));
211 print_cmn(fp, di, old);
212 (void) fputs(di->scripted ? "\t" : " -> ", fp);
213 print_cmn(fp, di, new);
214 (void) fputc('\n', fp);
216 if (isatty(fileno(fp)))
217 color_end();
220 static void
221 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
222 zfs_stat_t *isb)
224 if (isatty(fileno(fp)))
225 color_start(ZDIFF_MODIFIED_COLOR);
227 if (di->timestamped)
228 (void) fprintf(fp, "%10lld.%09lld\t",
229 (longlong_t)isb->zs_ctime[0],
230 (longlong_t)isb->zs_ctime[1]);
231 (void) fputs(ZDIFF_MODIFIED "\t", fp);
232 if (di->classify)
233 (void) fprintf(fp, "%c\t", get_what(isb->zs_mode));
234 print_cmn(fp, di, file);
235 (void) fprintf(fp, "\t(%+d)\n", delta);
236 if (isatty(fileno(fp)))
237 color_end();
240 static void
241 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
242 zfs_stat_t *isb)
244 if (isatty(fileno(fp)))
245 color_start(type_to_color(type));
247 if (di->timestamped)
248 (void) fprintf(fp, "%10lld.%09lld\t",
249 (longlong_t)isb->zs_ctime[0],
250 (longlong_t)isb->zs_ctime[1]);
251 (void) fprintf(fp, "%c\t", type);
252 if (di->classify)
253 (void) fprintf(fp, "%c\t", get_what(isb->zs_mode));
254 print_cmn(fp, di, file);
255 (void) fputc('\n', fp);
257 if (isatty(fileno(fp)))
258 color_end();
261 static int
262 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
264 struct zfs_stat fsb, tsb;
265 mode_t fmode, tmode;
266 char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
267 boolean_t already_logged = B_FALSE;
268 int fobjerr, tobjerr;
269 int change;
271 if (dobj == di->shares)
272 return (0);
275 * Check the from and to snapshots for info on the object. If
276 * we get ENOENT, then the object just didn't exist in that
277 * snapshot. If we get ENOTSUP, then we tried to get
278 * info on a non-ZPL object, which we don't care about anyway.
279 * For any other error we print a warning which includes the
280 * errno and continue.
283 fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
284 MAXPATHLEN, &fsb);
285 if (fobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
286 zfs_error_aux(di->zhp->zfs_hdl, "%s", zfs_strerror(di->zerr));
287 zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
289 * Let's not print an error for the same object more than
290 * once if it happens in both snapshots
292 already_logged = B_TRUE;
295 tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
296 MAXPATHLEN, &tsb);
298 if (tobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
299 if (!already_logged) {
300 zfs_error_aux(di->zhp->zfs_hdl,
301 "%s", zfs_strerror(di->zerr));
302 zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
306 * Unallocated object sharing the same meta dnode block
308 if (fobjerr && tobjerr) {
309 di->zerr = 0;
310 return (0);
313 di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
314 fmode = fsb.zs_mode & S_IFMT;
315 tmode = tsb.zs_mode & S_IFMT;
316 if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
317 tsb.zs_links == 0)
318 change = 0;
319 else
320 change = tsb.zs_links - fsb.zs_links;
322 if (fobjerr) {
323 if (change) {
324 print_link_change(fp, di, change, tobjname, &tsb);
325 return (0);
327 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
328 return (0);
329 } else if (tobjerr) {
330 if (change) {
331 print_link_change(fp, di, change, fobjname, &fsb);
332 return (0);
334 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
335 return (0);
338 if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
339 tsb.zs_gen++; /* Force a generational difference */
341 /* Simple modification or no change */
342 if (fsb.zs_gen == tsb.zs_gen) {
343 /* No apparent changes. Could we assert !this? */
344 if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
345 fsb.zs_ctime[1] == tsb.zs_ctime[1])
346 return (0);
347 if (change) {
348 print_link_change(fp, di, change,
349 change > 0 ? fobjname : tobjname, &tsb);
350 } else if (strcmp(fobjname, tobjname) == 0) {
351 print_file(fp, di, *ZDIFF_MODIFIED, fobjname, &tsb);
352 } else {
353 print_rename(fp, di, fobjname, tobjname, &tsb);
355 return (0);
356 } else {
357 /* file re-created or object re-used */
358 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
359 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
360 return (0);
364 static int
365 write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
367 uint64_t o;
368 int err;
370 for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
371 if ((err = write_inuse_diffs_one(fp, di, o)) != 0)
372 return (err);
374 return (0);
377 static int
378 describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
379 int maxlen)
381 struct zfs_stat sb;
383 (void) get_stats_for_obj(di, di->fromsnap, object, namebuf,
384 maxlen, &sb);
386 /* Don't print if in the delete queue on from side */
387 if (di->zerr == ESTALE || di->zerr == ENOENT) {
388 di->zerr = 0;
389 return (0);
392 print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
393 return (0);
396 static int
397 write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
399 zfs_cmd_t zc = {"\0"};
400 libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
401 char fobjname[MAXPATHLEN];
403 (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
404 zc.zc_obj = dr->ddr_first - 1;
406 ASSERT(di->zerr == 0);
408 while (zc.zc_obj < dr->ddr_last) {
409 int err;
411 err = zfs_ioctl(lhdl, ZFS_IOC_NEXT_OBJ, &zc);
412 if (err == 0) {
413 if (zc.zc_obj == di->shares) {
414 zc.zc_obj++;
415 continue;
417 if (zc.zc_obj > dr->ddr_last) {
418 break;
420 (void) describe_free(fp, di, zc.zc_obj, fobjname,
421 MAXPATHLEN);
422 } else if (errno == ESRCH) {
423 break;
424 } else {
425 (void) snprintf(di->errbuf, sizeof (di->errbuf),
426 dgettext(TEXT_DOMAIN,
427 "next allocated object (> %lld) find failure"),
428 (longlong_t)zc.zc_obj);
429 di->zerr = errno;
430 break;
433 if (di->zerr)
434 return (-1);
435 return (0);
438 static void *
439 differ(void *arg)
441 differ_info_t *di = arg;
442 dmu_diff_record_t dr;
443 FILE *ofp;
444 int err = 0;
446 if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
447 di->zerr = errno;
448 strlcpy(di->errbuf, zfs_strerror(errno), sizeof (di->errbuf));
449 (void) close(di->datafd);
450 return ((void *)-1);
453 for (;;) {
454 char *cp = (char *)&dr;
455 int len = sizeof (dr);
456 int rv;
458 do {
459 rv = read(di->datafd, cp, len);
460 cp += rv;
461 len -= rv;
462 } while (len > 0 && rv > 0);
464 if (rv < 0 || (rv == 0 && len != sizeof (dr))) {
465 di->zerr = EPIPE;
466 break;
467 } else if (rv == 0) {
468 /* end of file at a natural breaking point */
469 break;
472 switch (dr.ddr_type) {
473 case DDR_FREE:
474 err = write_free_diffs(ofp, di, &dr);
475 break;
476 case DDR_INUSE:
477 err = write_inuse_diffs(ofp, di, &dr);
478 break;
479 default:
480 di->zerr = EPIPE;
481 break;
484 if (err || di->zerr)
485 break;
488 (void) fclose(ofp);
489 (void) close(di->datafd);
490 if (err)
491 return ((void *)-1);
492 if (di->zerr) {
493 ASSERT(di->zerr == EPIPE);
494 (void) snprintf(di->errbuf, sizeof (di->errbuf),
495 dgettext(TEXT_DOMAIN,
496 "Internal error: bad data from diff IOCTL"));
497 return ((void *)-1);
499 return ((void *)0);
502 static int
503 make_temp_snapshot(differ_info_t *di)
505 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
506 zfs_cmd_t zc = {"\0"};
508 (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
509 ZDIFF_PREFIX, getpid());
510 (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
511 zc.zc_cleanup_fd = di->cleanupfd;
513 if (zfs_ioctl(hdl, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
514 int err = errno;
515 if (err == EPERM) {
516 (void) snprintf(di->errbuf, sizeof (di->errbuf),
517 dgettext(TEXT_DOMAIN, "The diff delegated "
518 "permission is needed in order\nto create a "
519 "just-in-time snapshot for diffing\n"));
520 return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
521 } else {
522 (void) snprintf(di->errbuf, sizeof (di->errbuf),
523 dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
524 "snapshot of '%s'"), zc.zc_name);
525 return (zfs_standard_error(hdl, err, di->errbuf));
529 di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
530 di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
531 return (0);
534 static void
535 teardown_differ_info(differ_info_t *di)
537 free(di->ds);
538 free(di->dsmnt);
539 free(di->fromsnap);
540 free(di->frommnt);
541 free(di->tosnap);
542 free(di->tmpsnap);
543 free(di->tomnt);
544 (void) close(di->cleanupfd);
547 static int
548 get_snapshot_names(differ_info_t *di, const char *fromsnap,
549 const char *tosnap)
551 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
552 char *atptrf = NULL;
553 char *atptrt = NULL;
554 int fdslen, fsnlen;
555 int tdslen, tsnlen;
558 * Can accept
559 * fdslen fsnlen tdslen tsnlen
560 * dataset@snap1
561 * 0. dataset@snap1 dataset@snap2 >0 >1 >0 >1
562 * 1. dataset@snap1 @snap2 >0 >1 ==0 >1
563 * 2. dataset@snap1 dataset >0 >1 >0 ==0
564 * 3. @snap1 dataset@snap2 ==0 >1 >0 >1
565 * 4. @snap1 dataset ==0 >1 >0 ==0
567 if (tosnap == NULL) {
568 /* only a from snapshot given, must be valid */
569 (void) snprintf(di->errbuf, sizeof (di->errbuf),
570 dgettext(TEXT_DOMAIN,
571 "Badly formed snapshot name %s"), fromsnap);
573 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
574 B_FALSE)) {
575 return (zfs_error(hdl, EZFS_INVALIDNAME,
576 di->errbuf));
579 atptrf = strchr(fromsnap, '@');
580 ASSERT(atptrf != NULL);
581 fdslen = atptrf - fromsnap;
583 di->fromsnap = zfs_strdup(hdl, fromsnap);
584 di->ds = zfs_strdup(hdl, fromsnap);
585 di->ds[fdslen] = '\0';
587 /* the to snap will be a just-in-time snap of the head */
588 return (make_temp_snapshot(di));
591 (void) snprintf(di->errbuf, sizeof (di->errbuf),
592 dgettext(TEXT_DOMAIN,
593 "Unable to determine which snapshots to compare"));
595 atptrf = strchr(fromsnap, '@');
596 atptrt = strchr(tosnap, '@');
597 fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
598 tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
599 fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
600 tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
602 if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 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))) {
607 * not the same dataset name, might be okay if
608 * tosnap is a clone of a fromsnap descendant.
610 char origin[ZFS_MAX_DATASET_NAME_LEN];
611 zprop_source_t src;
612 zfs_handle_t *zhp;
614 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
615 (void) strlcpy(di->ds, tosnap, tdslen + 1);
617 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
618 while (zhp != NULL) {
619 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
620 sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
621 (void) zfs_close(zhp);
622 zhp = NULL;
623 break;
625 if (strncmp(origin, fromsnap, fsnlen) == 0)
626 break;
628 (void) zfs_close(zhp);
629 zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
632 if (zhp == NULL) {
633 (void) snprintf(di->errbuf, sizeof (di->errbuf),
634 dgettext(TEXT_DOMAIN,
635 "Not an earlier snapshot from the same fs"));
636 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
637 } else {
638 (void) zfs_close(zhp);
641 di->isclone = B_TRUE;
642 di->fromsnap = zfs_strdup(hdl, fromsnap);
643 if (tsnlen)
644 di->tosnap = zfs_strdup(hdl, tosnap);
645 else
646 return (make_temp_snapshot(di));
647 } else {
648 int dslen = fdslen ? fdslen : tdslen;
650 di->ds = zfs_alloc(hdl, dslen + 1);
651 (void) strlcpy(di->ds, fdslen ? fromsnap : tosnap, dslen + 1);
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));
660 return (0);
663 static int
664 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
666 boolean_t mounted;
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));
676 /* Avoid a double slash at the beginning of root-mounted datasets */
677 if (**mntpt == '/' && *(*mntpt + 1) == '\0')
678 **mntpt = '\0';
679 return (0);
682 static int
683 get_mountpoints(differ_info_t *di)
685 char *strptr;
686 char *frommntpt;
689 * first get the mountpoint for the parent dataset
691 if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
692 return (-1);
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);
699 strptr = strchr(di->fromsnap, '@');
700 ASSERT3P(strptr, !=, NULL);
702 frommntpt = di->dsmnt;
703 if (di->isclone) {
704 char *mntpt;
705 int err;
707 *strptr = '\0';
708 err = get_mountpoint(di, di->fromsnap, &mntpt);
709 *strptr = '@';
710 if (err != 0)
711 return (-1);
712 frommntpt = mntpt;
715 di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
716 ZDIFF_SNAPDIR, ++strptr);
718 if (di->isclone)
719 free(frommntpt);
721 return (0);
724 static int
725 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
726 const char *tosnap, differ_info_t *di)
728 di->zhp = zhp;
730 di->cleanupfd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
731 VERIFY(di->cleanupfd >= 0);
733 if (get_snapshot_names(di, fromsnap, tosnap) != 0)
734 return (-1);
736 if (get_mountpoints(di) != 0)
737 return (-1);
739 if (find_shares_object(di) != 0)
740 return (-1);
742 return (0);
746 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
747 const char *tosnap, int flags)
749 zfs_cmd_t zc = {"\0"};
750 char errbuf[ERRBUFLEN];
751 differ_info_t di = { 0 };
752 pthread_t tid;
753 int pipefd[2];
754 int iocerr;
756 (void) snprintf(errbuf, sizeof (errbuf),
757 dgettext(TEXT_DOMAIN, "zfs diff failed"));
759 if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
760 teardown_differ_info(&di);
761 return (-1);
764 if (pipe2(pipefd, O_CLOEXEC)) {
765 zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
766 teardown_differ_info(&di);
767 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
770 di.scripted = (flags & ZFS_DIFF_PARSEABLE);
771 di.classify = (flags & ZFS_DIFF_CLASSIFY);
772 di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
773 di.no_mangle = (flags & ZFS_DIFF_NO_MANGLE);
775 di.outputfd = outfd;
776 di.datafd = pipefd[0];
778 if (pthread_create(&tid, NULL, differ, &di)) {
779 zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
780 (void) close(pipefd[0]);
781 (void) close(pipefd[1]);
782 teardown_differ_info(&di);
783 return (zfs_error(zhp->zfs_hdl,
784 EZFS_THREADCREATEFAILED, errbuf));
787 /* do the ioctl() */
788 (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
789 (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
790 zc.zc_cookie = pipefd[1];
792 iocerr = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DIFF, &zc);
793 if (iocerr != 0) {
794 (void) snprintf(errbuf, sizeof (errbuf),
795 dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
796 if (errno == EPERM) {
797 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
798 "\n The sys_mount privilege or diff delegated "
799 "permission is needed\n to execute the "
800 "diff ioctl"));
801 } else if (errno == EXDEV) {
802 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
803 "\n Not an earlier snapshot from the same fs"));
804 } else if (errno != EPIPE || di.zerr == 0) {
805 zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
807 (void) close(pipefd[1]);
808 (void) pthread_cancel(tid);
809 (void) pthread_join(tid, NULL);
810 teardown_differ_info(&di);
811 if (di.zerr != 0 && di.zerr != EPIPE) {
812 zfs_error_aux(zhp->zfs_hdl, "%s",
813 zfs_strerror(di.zerr));
814 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
815 } else {
816 return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
820 (void) close(pipefd[1]);
821 (void) pthread_join(tid, NULL);
823 if (di.zerr != 0) {
824 zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(di.zerr));
825 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
827 teardown_differ_info(&di);
828 return (0);