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