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