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