xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_diff.c (revision ce17336ed725d3b7fdff67bf0a0ee2b55018fec6)
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 (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 <attr.h>
41 #include <stddef.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stropts.h>
46 #include <pthread.h>
47 #include <sys/zfs_ioctl.h>
48 #include <libzfs.h>
49 #include "libzfs_impl.h"
50 
51 #define	ZDIFF_SNAPDIR		"/.zfs/snapshot/"
52 #define	ZDIFF_SHARESDIR		"/.zfs/shares/"
53 #define	ZDIFF_PREFIX		"zfs-diff-%d"
54 
55 #define	ZDIFF_ADDED	'+'
56 #define	ZDIFF_MODIFIED	'M'
57 #define	ZDIFF_REMOVED	'-'
58 #define	ZDIFF_RENAMED	'R'
59 
60 typedef struct differ_info {
61 	zfs_handle_t *zhp;
62 	char *fromsnap;
63 	char *frommnt;
64 	char *tosnap;
65 	char *tomnt;
66 	char *ds;
67 	char *dsmnt;
68 	char *tmpsnap;
69 	char errbuf[1024];
70 	boolean_t isclone;
71 	boolean_t scripted;
72 	boolean_t classify;
73 	boolean_t timestamped;
74 	uint64_t shares;
75 	int zerr;
76 	int cleanupfd;
77 	int outputfd;
78 	int datafd;
79 } differ_info_t;
80 
81 /*
82  * Given a {dsname, object id}, get the object path
83  */
84 static int
85 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
86     char *pn, int maxlen, zfs_stat_t *sb)
87 {
88 	zfs_cmd_t zc = { 0 };
89 	int error;
90 
91 	(void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
92 	zc.zc_obj = obj;
93 
94 	errno = 0;
95 	error = ioctl(di->zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_STATS, &zc);
96 	di->zerr = errno;
97 
98 	/* we can get stats even if we failed to get a path */
99 	(void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
100 	if (error == 0) {
101 		ASSERT(di->zerr == 0);
102 		(void) strlcpy(pn, zc.zc_value, maxlen);
103 		return (0);
104 	}
105 
106 	if (di->zerr == ESTALE) {
107 		(void) snprintf(pn, maxlen, "(on_delete_queue)");
108 		return (0);
109 	} else if (di->zerr == EPERM) {
110 		(void) snprintf(di->errbuf, sizeof (di->errbuf),
111 		    dgettext(TEXT_DOMAIN,
112 		    "The sys_config privilege or diff delegated permission "
113 		    "is needed\nto discover path names"));
114 		return (-1);
115 	} else if (di->zerr == EACCES) {
116 		(void) snprintf(di->errbuf, sizeof (di->errbuf),
117 		    dgettext(TEXT_DOMAIN,
118 		    "Key must be loaded to discover path names"));
119 		return (-1);
120 	} else {
121 		(void) snprintf(di->errbuf, sizeof (di->errbuf),
122 		    dgettext(TEXT_DOMAIN,
123 		    "Unable to determine path or stats for "
124 		    "object %lld in %s"), obj, dsname);
125 		return (-1);
126 	}
127 }
128 
129 /*
130  * stream_bytes
131  *
132  * Prints a file name out a character at a time.  If the character is
133  * not in the range of what we consider "printable" ASCII, display it
134  * as an escaped 3-digit octal value.  ASCII values less than a space
135  * are all control characters and we declare the upper end as the
136  * DELete character.  This also is the last 7-bit ASCII character.
137  * We choose to treat all 8-bit ASCII as not printable for this
138  * application.
139  */
140 static void
141 stream_bytes(FILE *fp, const char *string)
142 {
143 	char c;
144 
145 	while ((c = *string++) != '\0') {
146 		if (c > ' ' && c != '\\' && c < '\177') {
147 			(void) fprintf(fp, "%c", c);
148 		} else {
149 			(void) fprintf(fp, "\\%03o", (uint8_t)c);
150 		}
151 	}
152 }
153 
154 static void
155 print_what(FILE *fp, mode_t what)
156 {
157 	char symbol;
158 
159 	switch (what & S_IFMT) {
160 	case S_IFBLK:
161 		symbol = 'B';
162 		break;
163 	case S_IFCHR:
164 		symbol = 'C';
165 		break;
166 	case S_IFDIR:
167 		symbol = '/';
168 		break;
169 	case S_IFDOOR:
170 		symbol = '>';
171 		break;
172 	case S_IFIFO:
173 		symbol = '|';
174 		break;
175 	case S_IFLNK:
176 		symbol = '@';
177 		break;
178 	case S_IFPORT:
179 		symbol = 'P';
180 		break;
181 	case S_IFSOCK:
182 		symbol = '=';
183 		break;
184 	case S_IFREG:
185 		symbol = 'F';
186 		break;
187 	default:
188 		symbol = '?';
189 		break;
190 	}
191 	(void) fprintf(fp, "%c", symbol);
192 }
193 
194 static void
195 print_cmn(FILE *fp, differ_info_t *di, const char *file)
196 {
197 	stream_bytes(fp, di->dsmnt);
198 	stream_bytes(fp, file);
199 }
200 
201 static void
202 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
203     zfs_stat_t *isb)
204 {
205 	if (di->timestamped)
206 		(void) fprintf(fp, "%10lld.%09lld\t",
207 		    (longlong_t)isb->zs_ctime[0],
208 		    (longlong_t)isb->zs_ctime[1]);
209 	(void) fprintf(fp, "%c\t", ZDIFF_RENAMED);
210 	if (di->classify) {
211 		print_what(fp, isb->zs_mode);
212 		(void) fprintf(fp, "\t");
213 	}
214 	print_cmn(fp, di, old);
215 	if (di->scripted)
216 		(void) fprintf(fp, "\t");
217 	else
218 		(void) fprintf(fp, " -> ");
219 	print_cmn(fp, di, new);
220 	(void) fprintf(fp, "\n");
221 }
222 
223 static void
224 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
225     zfs_stat_t *isb)
226 {
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) fprintf(fp, "%c\t", ZDIFF_MODIFIED);
232 	if (di->classify) {
233 		print_what(fp, isb->zs_mode);
234 		(void) fprintf(fp, "\t");
235 	}
236 	print_cmn(fp, di, file);
237 	(void) fprintf(fp, "\t(%+d)", delta);
238 	(void) fprintf(fp, "\n");
239 }
240 
241 static void
242 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
243     zfs_stat_t *isb)
244 {
245 	if (di->timestamped)
246 		(void) fprintf(fp, "%10lld.%09lld\t",
247 		    (longlong_t)isb->zs_ctime[0],
248 		    (longlong_t)isb->zs_ctime[1]);
249 	(void) fprintf(fp, "%c\t", type);
250 	if (di->classify) {
251 		print_what(fp, isb->zs_mode);
252 		(void) fprintf(fp, "\t");
253 	}
254 	print_cmn(fp, di, file);
255 	(void) fprintf(fp, "\n");
256 }
257 
258 static int
259 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
260 {
261 	struct zfs_stat fsb, tsb;
262 	mode_t fmode, tmode;
263 	char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
264 	int fobjerr, tobjerr;
265 	int change;
266 
267 	if (dobj == di->shares)
268 		return (0);
269 
270 	/*
271 	 * Check the from and to snapshots for info on the object. If
272 	 * we get ENOENT, then the object just didn't exist in that
273 	 * snapshot.  If we get ENOTSUP, then we tried to get
274 	 * info on a non-ZPL object, which we don't care about anyway.
275 	 */
276 	fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
277 	    MAXPATHLEN, &fsb);
278 	if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
279 		return (-1);
280 
281 	tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
282 	    MAXPATHLEN, &tsb);
283 	if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
284 		return (-1);
285 
286 	/*
287 	 * Unallocated object sharing the same meta dnode block
288 	 */
289 	if (fobjerr && tobjerr) {
290 		ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
291 		di->zerr = 0;
292 		return (0);
293 	}
294 
295 	di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
296 	fmode = fsb.zs_mode & S_IFMT;
297 	tmode = tsb.zs_mode & S_IFMT;
298 	if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
299 	    tsb.zs_links == 0)
300 		change = 0;
301 	else
302 		change = tsb.zs_links - fsb.zs_links;
303 
304 	if (fobjerr) {
305 		if (change) {
306 			print_link_change(fp, di, change, tobjname, &tsb);
307 			return (0);
308 		}
309 		print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
310 		return (0);
311 	} else if (tobjerr) {
312 		if (change) {
313 			print_link_change(fp, di, change, fobjname, &fsb);
314 			return (0);
315 		}
316 		print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
317 		return (0);
318 	}
319 
320 	if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
321 		tsb.zs_gen++;	/* Force a generational difference */
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 (strcmp(fobjname, tobjname) == 0) {
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)) != 0)
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 		return (-1);
368 	}
369 	/* Don't print if in the delete queue on from side */
370 	if (di->zerr == ESTALE) {
371 		di->zerr = 0;
372 		return (0);
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 };
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 			    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 		(void) strerror_r(errno, di->errbuf, 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 };
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_MAX_DATASET_NAME_LEN];
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 			if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
624 			    sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
625 				(void) zfs_close(zhp);
626 				zhp = NULL;
627 				break;
628 			}
629 			if (strncmp(origin, fromsnap, fsnlen) == 0)
630 				break;
631 
632 			(void) zfs_close(zhp);
633 			zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
634 		}
635 
636 		if (zhp == NULL) {
637 			(void) snprintf(di->errbuf, sizeof (di->errbuf),
638 			    dgettext(TEXT_DOMAIN,
639 			    "Not an earlier snapshot from the same fs"));
640 			return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
641 		} else {
642 			(void) zfs_close(zhp);
643 		}
644 
645 		di->isclone = B_TRUE;
646 		di->fromsnap = zfs_strdup(hdl, fromsnap);
647 		if (tsnlen) {
648 			di->tosnap = zfs_strdup(hdl, tosnap);
649 		} else {
650 			return (make_temp_snapshot(di));
651 		}
652 	} else {
653 		int dslen = fdslen ? fdslen : tdslen;
654 
655 		di->ds = zfs_alloc(hdl, dslen + 1);
656 		(void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
657 		di->ds[dslen] = '\0';
658 
659 		di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
660 		if (tsnlen) {
661 			di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
662 		} else {
663 			return (make_temp_snapshot(di));
664 		}
665 	}
666 	return (0);
667 }
668 
669 static int
670 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
671 {
672 	boolean_t mounted;
673 
674 	mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
675 	if (mounted == B_FALSE) {
676 		(void) snprintf(di->errbuf, sizeof (di->errbuf),
677 		    dgettext(TEXT_DOMAIN,
678 		    "Cannot diff an unmounted snapshot"));
679 		return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
680 	}
681 
682 	/* Avoid a double slash at the beginning of root-mounted datasets */
683 	if (**mntpt == '/' && *(*mntpt + 1) == '\0')
684 		**mntpt = '\0';
685 	return (0);
686 }
687 
688 static int
689 get_mountpoints(differ_info_t *di)
690 {
691 	char *strptr;
692 	char *frommntpt;
693 
694 	/*
695 	 * first get the mountpoint for the parent dataset
696 	 */
697 	if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
698 		return (-1);
699 
700 	strptr = strchr(di->tosnap, '@');
701 	ASSERT3P(strptr, !=, NULL);
702 	di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
703 	    ZDIFF_SNAPDIR, ++strptr);
704 
705 	strptr = strchr(di->fromsnap, '@');
706 	ASSERT3P(strptr, !=, NULL);
707 
708 	frommntpt = di->dsmnt;
709 	if (di->isclone) {
710 		char *mntpt;
711 		int err;
712 
713 		*strptr = '\0';
714 		err = get_mountpoint(di, di->fromsnap, &mntpt);
715 		*strptr = '@';
716 		if (err != 0)
717 			return (-1);
718 		frommntpt = mntpt;
719 	}
720 
721 	di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
722 	    ZDIFF_SNAPDIR, ++strptr);
723 
724 	if (di->isclone)
725 		free(frommntpt);
726 
727 	return (0);
728 }
729 
730 static int
731 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
732     const char *tosnap, differ_info_t *di)
733 {
734 	di->zhp = zhp;
735 
736 	di->cleanupfd = open(ZFS_DEV, O_RDWR|O_EXCL);
737 	VERIFY(di->cleanupfd >= 0);
738 
739 	if (get_snapshot_names(di, fromsnap, tosnap) != 0)
740 		return (-1);
741 
742 	if (get_mountpoints(di) != 0)
743 		return (-1);
744 
745 	if (find_shares_object(di) != 0)
746 		return (-1);
747 
748 	return (0);
749 }
750 
751 int
752 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
753     const char *tosnap, int flags)
754 {
755 	zfs_cmd_t zc = { 0 };
756 	char errbuf[1024];
757 	differ_info_t di = { 0 };
758 	pthread_t tid;
759 	int pipefd[2];
760 	int iocerr;
761 
762 	(void) snprintf(errbuf, sizeof (errbuf),
763 	    dgettext(TEXT_DOMAIN, "zfs diff failed"));
764 
765 	if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
766 		teardown_differ_info(&di);
767 		return (-1);
768 	}
769 
770 	if (pipe(pipefd)) {
771 		zfs_error_aux(zhp->zfs_hdl, strerror(errno));
772 		teardown_differ_info(&di);
773 		return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
774 	}
775 
776 	di.scripted = (flags & ZFS_DIFF_PARSEABLE);
777 	di.classify = (flags & ZFS_DIFF_CLASSIFY);
778 	di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
779 
780 	di.outputfd = outfd;
781 	di.datafd = pipefd[0];
782 
783 	if (pthread_create(&tid, NULL, differ, &di)) {
784 		zfs_error_aux(zhp->zfs_hdl, strerror(errno));
785 		(void) close(pipefd[0]);
786 		(void) close(pipefd[1]);
787 		teardown_differ_info(&di);
788 		return (zfs_error(zhp->zfs_hdl,
789 		    EZFS_THREADCREATEFAILED, errbuf));
790 	}
791 
792 	/* do the ioctl() */
793 	(void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
794 	(void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
795 	zc.zc_cookie = pipefd[1];
796 
797 	iocerr = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DIFF, &zc);
798 	if (iocerr != 0) {
799 		(void) snprintf(errbuf, sizeof (errbuf),
800 		    dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
801 		if (errno == EPERM) {
802 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
803 			    "\n   The sys_mount privilege or diff delegated "
804 			    "permission is needed\n   to execute the "
805 			    "diff ioctl"));
806 		} else if (errno == EXDEV) {
807 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
808 			    "\n   Not an earlier snapshot from the same fs"));
809 		} else if (errno != EPIPE || di.zerr == 0) {
810 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
811 		}
812 		(void) close(pipefd[1]);
813 		(void) pthread_cancel(tid);
814 		(void) pthread_join(tid, NULL);
815 		teardown_differ_info(&di);
816 		if (di.zerr != 0 && di.zerr != EPIPE) {
817 			zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
818 			return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
819 		} else {
820 			return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
821 		}
822 	}
823 
824 	(void) close(pipefd[1]);
825 	(void) pthread_join(tid, NULL);
826 
827 	if (di.zerr != 0) {
828 		zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
829 		return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
830 	}
831 	teardown_differ_info(&di);
832 	return (0);
833 }
834