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