Add missing AC_MSG_RESULT(no) to configure
[zfs.git] / lib / libzfs / libzfs_diff.c
blobe8b8a1dfc398d54af882a03be83940bb12a9f2a9
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 http://www.opensolaris.org/os/licensing.
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 <stropts.h>
45 #include <pthread.h>
46 #include <sys/zfs_ioctl.h>
47 #include <libzfs.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"
60 * Given a {dsname, object id}, get the object path
62 static int
63 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
64 char *pn, int maxlen, zfs_stat_t *sb)
66 zfs_cmd_t zc = {"\0"};
67 int error;
69 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
70 zc.zc_obj = obj;
72 errno = 0;
73 error = zfs_ioctl(di->zhp->zfs_hdl, ZFS_IOC_OBJ_TO_STATS, &zc);
74 di->zerr = errno;
76 /* we can get stats even if we failed to get a path */
77 (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
78 if (error == 0) {
79 ASSERT(di->zerr == 0);
80 (void) strlcpy(pn, zc.zc_value, maxlen);
81 return (0);
84 if (di->zerr == ESTALE) {
85 (void) snprintf(pn, maxlen, "(on_delete_queue)");
86 return (0);
87 } else if (di->zerr == EPERM) {
88 (void) snprintf(di->errbuf, sizeof (di->errbuf),
89 dgettext(TEXT_DOMAIN,
90 "The sys_config privilege or diff delegated permission "
91 "is needed\nto discover path names"));
92 return (-1);
93 } else if (di->zerr == EACCES) {
94 (void) snprintf(di->errbuf, sizeof (di->errbuf),
95 dgettext(TEXT_DOMAIN,
96 "Key must be loaded to discover path names"));
97 return (-1);
98 } else {
99 (void) snprintf(di->errbuf, sizeof (di->errbuf),
100 dgettext(TEXT_DOMAIN,
101 "Unable to determine path or stats for "
102 "object %lld in %s"), (longlong_t)obj, dsname);
103 return (-1);
108 * stream_bytes
110 * Prints a file name out a character at a time. If the character is
111 * not in the range of what we consider "printable" ASCII, display it
112 * as an escaped 4-digit octal value. ASCII values less than a space
113 * are all control characters and we declare the upper end as the
114 * DELete character. This also is the last 7-bit ASCII character.
115 * We choose to treat all 8-bit ASCII as not printable for this
116 * application.
118 static void
119 stream_bytes(FILE *fp, const char *string)
121 char c;
123 while ((c = *string++) != '\0') {
124 if (c > ' ' && c != '\\' && c < '\177') {
125 (void) fputc(c, fp);
126 } else {
127 (void) fprintf(fp, "\\%04hho", (uint8_t)c);
132 static char
133 get_what(mode_t what)
135 switch (what & S_IFMT) {
136 case S_IFBLK:
137 return ('B');
138 case S_IFCHR:
139 return ('C');
140 case S_IFDIR:
141 return ('/');
142 #ifdef S_IFDOOR
143 case S_IFDOOR:
144 return ('>');
145 #endif
146 case S_IFIFO:
147 return ('|');
148 case S_IFLNK:
149 return ('@');
150 #ifdef S_IFPORT
151 case S_IFPORT:
152 return ('P');
153 #endif
154 case S_IFSOCK:
155 return ('=');
156 case S_IFREG:
157 return ('F');
158 default:
159 return ('?');
163 static void
164 print_cmn(FILE *fp, differ_info_t *di, const char *file)
166 if (!di->no_mangle) {
167 stream_bytes(fp, di->dsmnt);
168 stream_bytes(fp, file);
169 } else {
170 (void) fputs(di->dsmnt, fp);
171 (void) fputs(file, fp);
175 static void
176 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
177 zfs_stat_t *isb)
179 if (di->timestamped)
180 (void) fprintf(fp, "%10lld.%09lld\t",
181 (longlong_t)isb->zs_ctime[0],
182 (longlong_t)isb->zs_ctime[1]);
183 (void) fputs(ZDIFF_RENAMED "\t", fp);
184 if (di->classify)
185 (void) fprintf(fp, "%c\t", get_what(isb->zs_mode));
186 print_cmn(fp, di, old);
187 (void) fputs(di->scripted ? "\t" : " -> ", fp);
188 print_cmn(fp, di, new);
189 (void) fputc('\n', fp);
192 static void
193 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
194 zfs_stat_t *isb)
196 if (di->timestamped)
197 (void) fprintf(fp, "%10lld.%09lld\t",
198 (longlong_t)isb->zs_ctime[0],
199 (longlong_t)isb->zs_ctime[1]);
200 (void) fputs(ZDIFF_MODIFIED "\t", fp);
201 if (di->classify)
202 (void) fprintf(fp, "%c\t", get_what(isb->zs_mode));
203 print_cmn(fp, di, file);
204 (void) fprintf(fp, "\t(%+d)\n", delta);
207 static void
208 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
209 zfs_stat_t *isb)
211 if (di->timestamped)
212 (void) fprintf(fp, "%10lld.%09lld\t",
213 (longlong_t)isb->zs_ctime[0],
214 (longlong_t)isb->zs_ctime[1]);
215 (void) fprintf(fp, "%c\t", type);
216 if (di->classify)
217 (void) fprintf(fp, "%c\t", get_what(isb->zs_mode));
218 print_cmn(fp, di, file);
219 (void) fputc('\n', fp);
222 static int
223 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
225 struct zfs_stat fsb, tsb;
226 mode_t fmode, tmode;
227 char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
228 boolean_t already_logged = B_FALSE;
229 int fobjerr, tobjerr;
230 int change;
232 if (dobj == di->shares)
233 return (0);
236 * Check the from and to snapshots for info on the object. If
237 * we get ENOENT, then the object just didn't exist in that
238 * snapshot. If we get ENOTSUP, then we tried to get
239 * info on a non-ZPL object, which we don't care about anyway.
240 * For any other error we print a warning which includes the
241 * errno and continue.
244 fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
245 MAXPATHLEN, &fsb);
246 if (fobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
247 zfs_error_aux(di->zhp->zfs_hdl, "%s", strerror(di->zerr));
248 zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
250 * Let's not print an error for the same object more than
251 * once if it happens in both snapshots
253 already_logged = B_TRUE;
256 tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
257 MAXPATHLEN, &tsb);
259 if (tobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
260 if (!already_logged) {
261 zfs_error_aux(di->zhp->zfs_hdl,
262 "%s", strerror(di->zerr));
263 zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
267 * Unallocated object sharing the same meta dnode block
269 if (fobjerr && tobjerr) {
270 di->zerr = 0;
271 return (0);
274 di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
275 fmode = fsb.zs_mode & S_IFMT;
276 tmode = tsb.zs_mode & S_IFMT;
277 if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
278 tsb.zs_links == 0)
279 change = 0;
280 else
281 change = tsb.zs_links - fsb.zs_links;
283 if (fobjerr) {
284 if (change) {
285 print_link_change(fp, di, change, tobjname, &tsb);
286 return (0);
288 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
289 return (0);
290 } else if (tobjerr) {
291 if (change) {
292 print_link_change(fp, di, change, fobjname, &fsb);
293 return (0);
295 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
296 return (0);
299 if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
300 tsb.zs_gen++; /* Force a generational difference */
302 /* Simple modification or no change */
303 if (fsb.zs_gen == tsb.zs_gen) {
304 /* No apparent changes. Could we assert !this? */
305 if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
306 fsb.zs_ctime[1] == tsb.zs_ctime[1])
307 return (0);
308 if (change) {
309 print_link_change(fp, di, change,
310 change > 0 ? fobjname : tobjname, &tsb);
311 } else if (strcmp(fobjname, tobjname) == 0) {
312 print_file(fp, di, *ZDIFF_MODIFIED, fobjname, &tsb);
313 } else {
314 print_rename(fp, di, fobjname, tobjname, &tsb);
316 return (0);
317 } else {
318 /* file re-created or object re-used */
319 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
320 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
321 return (0);
325 static int
326 write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
328 uint64_t o;
329 int err;
331 for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
332 if ((err = write_inuse_diffs_one(fp, di, o)) != 0)
333 return (err);
335 return (0);
338 static int
339 describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
340 int maxlen)
342 struct zfs_stat sb;
344 (void) get_stats_for_obj(di, di->fromsnap, object, namebuf,
345 maxlen, &sb);
347 /* Don't print if in the delete queue on from side */
348 if (di->zerr == ESTALE || di->zerr == ENOENT) {
349 di->zerr = 0;
350 return (0);
353 print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
354 return (0);
357 static int
358 write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
360 zfs_cmd_t zc = {"\0"};
361 libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
362 char fobjname[MAXPATHLEN];
364 (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
365 zc.zc_obj = dr->ddr_first - 1;
367 ASSERT(di->zerr == 0);
369 while (zc.zc_obj < dr->ddr_last) {
370 int err;
372 err = zfs_ioctl(lhdl, ZFS_IOC_NEXT_OBJ, &zc);
373 if (err == 0) {
374 if (zc.zc_obj == di->shares) {
375 zc.zc_obj++;
376 continue;
378 if (zc.zc_obj > dr->ddr_last) {
379 break;
381 err = describe_free(fp, di, zc.zc_obj, fobjname,
382 MAXPATHLEN);
383 } else if (errno == ESRCH) {
384 break;
385 } else {
386 (void) snprintf(di->errbuf, sizeof (di->errbuf),
387 dgettext(TEXT_DOMAIN,
388 "next allocated object (> %lld) find failure"),
389 (longlong_t)zc.zc_obj);
390 di->zerr = errno;
391 break;
394 if (di->zerr)
395 return (-1);
396 return (0);
399 static void *
400 differ(void *arg)
402 differ_info_t *di = arg;
403 dmu_diff_record_t dr;
404 FILE *ofp;
405 int err = 0;
407 if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
408 di->zerr = errno;
409 strlcpy(di->errbuf, strerror(errno), sizeof (di->errbuf));
410 (void) close(di->datafd);
411 return ((void *)-1);
414 for (;;) {
415 char *cp = (char *)&dr;
416 int len = sizeof (dr);
417 int rv;
419 do {
420 rv = read(di->datafd, cp, len);
421 cp += rv;
422 len -= rv;
423 } while (len > 0 && rv > 0);
425 if (rv < 0 || (rv == 0 && len != sizeof (dr))) {
426 di->zerr = EPIPE;
427 break;
428 } else if (rv == 0) {
429 /* end of file at a natural breaking point */
430 break;
433 switch (dr.ddr_type) {
434 case DDR_FREE:
435 err = write_free_diffs(ofp, di, &dr);
436 break;
437 case DDR_INUSE:
438 err = write_inuse_diffs(ofp, di, &dr);
439 break;
440 default:
441 di->zerr = EPIPE;
442 break;
445 if (err || di->zerr)
446 break;
449 (void) fclose(ofp);
450 (void) close(di->datafd);
451 if (err)
452 return ((void *)-1);
453 if (di->zerr) {
454 ASSERT(di->zerr == EPIPE);
455 (void) snprintf(di->errbuf, sizeof (di->errbuf),
456 dgettext(TEXT_DOMAIN,
457 "Internal error: bad data from diff IOCTL"));
458 return ((void *)-1);
460 return ((void *)0);
463 static int
464 make_temp_snapshot(differ_info_t *di)
466 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
467 zfs_cmd_t zc = {"\0"};
469 (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
470 ZDIFF_PREFIX, getpid());
471 (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
472 zc.zc_cleanup_fd = di->cleanupfd;
474 if (zfs_ioctl(hdl, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
475 int err = errno;
476 if (err == EPERM) {
477 (void) snprintf(di->errbuf, sizeof (di->errbuf),
478 dgettext(TEXT_DOMAIN, "The diff delegated "
479 "permission is needed in order\nto create a "
480 "just-in-time snapshot for diffing\n"));
481 return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
482 } else {
483 (void) snprintf(di->errbuf, sizeof (di->errbuf),
484 dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
485 "snapshot of '%s'"), zc.zc_name);
486 return (zfs_standard_error(hdl, err, di->errbuf));
490 di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
491 di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
492 return (0);
495 static void
496 teardown_differ_info(differ_info_t *di)
498 free(di->ds);
499 free(di->dsmnt);
500 free(di->fromsnap);
501 free(di->frommnt);
502 free(di->tosnap);
503 free(di->tmpsnap);
504 free(di->tomnt);
505 (void) close(di->cleanupfd);
508 static int
509 get_snapshot_names(differ_info_t *di, const char *fromsnap,
510 const char *tosnap)
512 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
513 char *atptrf = NULL;
514 char *atptrt = NULL;
515 int fdslen, fsnlen;
516 int tdslen, tsnlen;
519 * Can accept
520 * fdslen fsnlen tdslen tsnlen
521 * dataset@snap1
522 * 0. dataset@snap1 dataset@snap2 >0 >1 >0 >1
523 * 1. dataset@snap1 @snap2 >0 >1 ==0 >1
524 * 2. dataset@snap1 dataset >0 >1 >0 ==0
525 * 3. @snap1 dataset@snap2 ==0 >1 >0 >1
526 * 4. @snap1 dataset ==0 >1 >0 ==0
528 if (tosnap == NULL) {
529 /* only a from snapshot given, must be valid */
530 (void) snprintf(di->errbuf, sizeof (di->errbuf),
531 dgettext(TEXT_DOMAIN,
532 "Badly formed snapshot name %s"), fromsnap);
534 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
535 B_FALSE)) {
536 return (zfs_error(hdl, EZFS_INVALIDNAME,
537 di->errbuf));
540 atptrf = strchr(fromsnap, '@');
541 ASSERT(atptrf != NULL);
542 fdslen = atptrf - fromsnap;
544 di->fromsnap = zfs_strdup(hdl, fromsnap);
545 di->ds = zfs_strdup(hdl, fromsnap);
546 di->ds[fdslen] = '\0';
548 /* the to snap will be a just-in-time snap of the head */
549 return (make_temp_snapshot(di));
552 (void) snprintf(di->errbuf, sizeof (di->errbuf),
553 dgettext(TEXT_DOMAIN,
554 "Unable to determine which snapshots to compare"));
556 atptrf = strchr(fromsnap, '@');
557 atptrt = strchr(tosnap, '@');
558 fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
559 tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
560 fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
561 tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
563 if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0)) {
564 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
565 } else if ((fdslen > 0 && tdslen > 0) &&
566 ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
568 * not the same dataset name, might be okay if
569 * tosnap is a clone of a fromsnap descendant.
571 char origin[ZFS_MAX_DATASET_NAME_LEN];
572 zprop_source_t src;
573 zfs_handle_t *zhp;
575 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
576 (void) strncpy(di->ds, tosnap, tdslen);
577 di->ds[tdslen] = '\0';
579 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
580 while (zhp != NULL) {
581 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
582 sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
583 (void) zfs_close(zhp);
584 zhp = NULL;
585 break;
587 if (strncmp(origin, fromsnap, fsnlen) == 0)
588 break;
590 (void) zfs_close(zhp);
591 zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
594 if (zhp == NULL) {
595 (void) snprintf(di->errbuf, sizeof (di->errbuf),
596 dgettext(TEXT_DOMAIN,
597 "Not an earlier snapshot from the same fs"));
598 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
599 } else {
600 (void) zfs_close(zhp);
603 di->isclone = B_TRUE;
604 di->fromsnap = zfs_strdup(hdl, fromsnap);
605 if (tsnlen)
606 di->tosnap = zfs_strdup(hdl, tosnap);
607 else
608 return (make_temp_snapshot(di));
609 } else {
610 int dslen = fdslen ? fdslen : tdslen;
612 di->ds = zfs_alloc(hdl, dslen + 1);
613 (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
614 di->ds[dslen] = '\0';
616 di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
617 if (tsnlen) {
618 di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
619 } else {
620 return (make_temp_snapshot(di));
623 return (0);
626 static int
627 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
629 boolean_t mounted;
631 mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
632 if (mounted == B_FALSE) {
633 (void) snprintf(di->errbuf, sizeof (di->errbuf),
634 dgettext(TEXT_DOMAIN,
635 "Cannot diff an unmounted snapshot"));
636 return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
639 /* Avoid a double slash at the beginning of root-mounted datasets */
640 if (**mntpt == '/' && *(*mntpt + 1) == '\0')
641 **mntpt = '\0';
642 return (0);
645 static int
646 get_mountpoints(differ_info_t *di)
648 char *strptr;
649 char *frommntpt;
652 * first get the mountpoint for the parent dataset
654 if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
655 return (-1);
657 strptr = strchr(di->tosnap, '@');
658 ASSERT3P(strptr, !=, NULL);
659 di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
660 ZDIFF_SNAPDIR, ++strptr);
662 strptr = strchr(di->fromsnap, '@');
663 ASSERT3P(strptr, !=, NULL);
665 frommntpt = di->dsmnt;
666 if (di->isclone) {
667 char *mntpt;
668 int err;
670 *strptr = '\0';
671 err = get_mountpoint(di, di->fromsnap, &mntpt);
672 *strptr = '@';
673 if (err != 0)
674 return (-1);
675 frommntpt = mntpt;
678 di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
679 ZDIFF_SNAPDIR, ++strptr);
681 if (di->isclone)
682 free(frommntpt);
684 return (0);
687 static int
688 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
689 const char *tosnap, differ_info_t *di)
691 di->zhp = zhp;
693 di->cleanupfd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
694 VERIFY(di->cleanupfd >= 0);
696 if (get_snapshot_names(di, fromsnap, tosnap) != 0)
697 return (-1);
699 if (get_mountpoints(di) != 0)
700 return (-1);
702 if (find_shares_object(di) != 0)
703 return (-1);
705 return (0);
709 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
710 const char *tosnap, int flags)
712 zfs_cmd_t zc = {"\0"};
713 char errbuf[1024];
714 differ_info_t di = { 0 };
715 pthread_t tid;
716 int pipefd[2];
717 int iocerr;
719 (void) snprintf(errbuf, sizeof (errbuf),
720 dgettext(TEXT_DOMAIN, "zfs diff failed"));
722 if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
723 teardown_differ_info(&di);
724 return (-1);
727 if (pipe2(pipefd, O_CLOEXEC)) {
728 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
729 teardown_differ_info(&di);
730 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
733 di.scripted = (flags & ZFS_DIFF_PARSEABLE);
734 di.classify = (flags & ZFS_DIFF_CLASSIFY);
735 di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
736 di.no_mangle = (flags & ZFS_DIFF_NO_MANGLE);
738 di.outputfd = outfd;
739 di.datafd = pipefd[0];
741 if (pthread_create(&tid, NULL, differ, &di)) {
742 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
743 (void) close(pipefd[0]);
744 (void) close(pipefd[1]);
745 teardown_differ_info(&di);
746 return (zfs_error(zhp->zfs_hdl,
747 EZFS_THREADCREATEFAILED, errbuf));
750 /* do the ioctl() */
751 (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
752 (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
753 zc.zc_cookie = pipefd[1];
755 iocerr = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DIFF, &zc);
756 if (iocerr != 0) {
757 (void) snprintf(errbuf, sizeof (errbuf),
758 dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
759 if (errno == EPERM) {
760 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
761 "\n The sys_mount privilege or diff delegated "
762 "permission is needed\n to execute the "
763 "diff ioctl"));
764 } else if (errno == EXDEV) {
765 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
766 "\n Not an earlier snapshot from the same fs"));
767 } else if (errno != EPIPE || di.zerr == 0) {
768 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
770 (void) close(pipefd[1]);
771 (void) pthread_cancel(tid);
772 (void) pthread_join(tid, NULL);
773 teardown_differ_info(&di);
774 if (di.zerr != 0 && di.zerr != EPIPE) {
775 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(di.zerr));
776 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
777 } else {
778 return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
782 (void) close(pipefd[1]);
783 (void) pthread_join(tid, NULL);
785 if (di.zerr != 0) {
786 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(di.zerr));
787 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
789 teardown_differ_info(&di);
790 return (0);