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  * 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 <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
get_stats_for_obj(differ_info_t * di,const char * dsname,uint64_t obj,char * pn,int maxlen,zfs_stat_t * sb)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
stream_bytes(FILE * fp,const char * string)131 stream_bytes(FILE *fp, const char *string)
132 {
133 	char c;
134 
135 	while ((c = *string++) != '\0') {
136 		if (c > ' ' && c != '\\' && c < '\177') {
137 			(void) fprintf(fp, "%c", c);
138 		} else {
139 			(void) fprintf(fp, "\\%03o", (uint8_t)c);
140 		}
141 	}
142 }
143 
144 static void
print_what(FILE * fp,mode_t what)145 print_what(FILE *fp, mode_t what)
146 {
147 	char symbol;
148 
149 	switch (what & S_IFMT) {
150 	case S_IFBLK:
151 		symbol = 'B';
152 		break;
153 	case S_IFCHR:
154 		symbol = 'C';
155 		break;
156 	case S_IFDIR:
157 		symbol = '/';
158 		break;
159 #ifdef S_IFDOOR
160 	case S_IFDOOR:
161 		symbol = '>';
162 		break;
163 #endif
164 	case S_IFIFO:
165 		symbol = '|';
166 		break;
167 	case S_IFLNK:
168 		symbol = '@';
169 		break;
170 #ifdef S_IFPORT
171 	case S_IFPORT:
172 		symbol = 'P';
173 		break;
174 #endif
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
print_cmn(FILE * fp,differ_info_t * di,const char * file)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
print_rename(FILE * fp,differ_info_t * di,const char * old,const char * new,zfs_stat_t * isb)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
print_link_change(FILE * fp,differ_info_t * di,int delta,const char * file,zfs_stat_t * isb)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
print_file(FILE * fp,differ_info_t * di,char type,const char * file,zfs_stat_t * isb)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
write_inuse_diffs_one(FILE * fp,differ_info_t * di,uint64_t dobj)253 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
254 {
255 	struct zfs_stat fsb, tsb;
256 	mode_t fmode, tmode;
257 	char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
258 	int fobjerr, tobjerr;
259 	int change;
260 
261 	if (dobj == di->shares)
262 		return (0);
263 
264 	/*
265 	 * Check the from and to snapshots for info on the object. If
266 	 * we get ENOENT, then the object just didn't exist in that
267 	 * snapshot.  If we get ENOTSUP, then we tried to get
268 	 * info on a non-ZPL object, which we don't care about anyway.
269 	 */
270 	fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
271 	    MAXPATHLEN, &fsb);
272 	if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
273 		return (-1);
274 
275 	tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
276 	    MAXPATHLEN, &tsb);
277 	if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
278 		return (-1);
279 
280 	/*
281 	 * Unallocated object sharing the same meta dnode block
282 	 */
283 	if (fobjerr && tobjerr) {
284 		ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
285 		di->zerr = 0;
286 		return (0);
287 	}
288 
289 	di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
290 	fmode = fsb.zs_mode & S_IFMT;
291 	tmode = tsb.zs_mode & S_IFMT;
292 	if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
293 	    tsb.zs_links == 0)
294 		change = 0;
295 	else
296 		change = tsb.zs_links - fsb.zs_links;
297 
298 	if (fobjerr) {
299 		if (change) {
300 			print_link_change(fp, di, change, tobjname, &tsb);
301 			return (0);
302 		}
303 		print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
304 		return (0);
305 	} else if (tobjerr) {
306 		if (change) {
307 			print_link_change(fp, di, change, fobjname, &fsb);
308 			return (0);
309 		}
310 		print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
311 		return (0);
312 	}
313 
314 	if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
315 		tsb.zs_gen++;	/* Force a generational difference */
316 
317 	/* Simple modification or no change */
318 	if (fsb.zs_gen == tsb.zs_gen) {
319 		/* No apparent changes.  Could we assert !this?  */
320 		if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
321 		    fsb.zs_ctime[1] == tsb.zs_ctime[1])
322 			return (0);
323 		if (change) {
324 			print_link_change(fp, di, change,
325 			    change > 0 ? fobjname : tobjname, &tsb);
326 		} else if (strcmp(fobjname, tobjname) == 0) {
327 			print_file(fp, di, ZDIFF_MODIFIED, fobjname, &tsb);
328 		} else {
329 			print_rename(fp, di, fobjname, tobjname, &tsb);
330 		}
331 		return (0);
332 	} else {
333 		/* file re-created or object re-used */
334 		print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
335 		print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
336 		return (0);
337 	}
338 }
339 
340 static int
write_inuse_diffs(FILE * fp,differ_info_t * di,dmu_diff_record_t * dr)341 write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
342 {
343 	uint64_t o;
344 	int err;
345 
346 	for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
347 		if ((err = write_inuse_diffs_one(fp, di, o)) != 0)
348 			return (err);
349 	}
350 	return (0);
351 }
352 
353 static int
describe_free(FILE * fp,differ_info_t * di,uint64_t object,char * namebuf,int maxlen)354 describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
355     int maxlen)
356 {
357 	struct zfs_stat sb;
358 
359 	if (get_stats_for_obj(di, di->fromsnap, object, namebuf,
360 	    maxlen, &sb) != 0) {
361 		/* Let it slide, if in the delete queue on from side */
362 		if (di->zerr == ENOENT && sb.zs_links == 0) {
363 			di->zerr = 0;
364 			return (0);
365 		}
366 		return (-1);
367 	}
368 
369 	print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
370 	return (0);
371 }
372 
373 static int
write_free_diffs(FILE * fp,differ_info_t * di,dmu_diff_record_t * dr)374 write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
375 {
376 	zfs_cmd_t zc = { 0 };
377 	libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
378 	char fobjname[MAXPATHLEN];
379 
380 	(void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
381 	zc.zc_obj = dr->ddr_first - 1;
382 
383 	ASSERT(di->zerr == 0);
384 
385 	while (zc.zc_obj < dr->ddr_last) {
386 		int err;
387 
388 		err = ioctl(lhdl->libzfs_fd, ZFS_IOC_NEXT_OBJ, &zc);
389 		if (err == 0) {
390 			if (zc.zc_obj == di->shares) {
391 				zc.zc_obj++;
392 				continue;
393 			}
394 			if (zc.zc_obj > dr->ddr_last) {
395 				break;
396 			}
397 			err = describe_free(fp, di, zc.zc_obj, fobjname,
398 			    MAXPATHLEN);
399 			if (err)
400 				break;
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 			    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 *
differ(void * arg)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 		(void) strerror_r(errno, di->errbuf, 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 == EINVAL);
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
find_shares_object(differ_info_t * di)482 find_shares_object(differ_info_t *di)
483 {
484 	char fullpath[MAXPATHLEN];
485 	struct stat64 sb = { 0 };
486 
487 	(void) strlcpy(fullpath, di->dsmnt, MAXPATHLEN);
488 	(void) strlcat(fullpath, ZDIFF_SHARESDIR, MAXPATHLEN);
489 
490 	if (stat64(fullpath, &sb) != 0) {
491 #ifdef illumos
492 		(void) snprintf(di->errbuf, sizeof (di->errbuf),
493 		    dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath);
494 		return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf));
495 #else
496 		return (0);
497 #endif
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