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