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