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