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