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.
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.
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]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2015, 2018 by Delphix. All rights reserved.
26 * Copyright 2016 Joyent, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
37 #include <sys/types.h>
46 #include <sys/zfs_ioctl.h>
48 #include "libzfs_impl.h"
50 #define ZDIFF_SNAPDIR "/.zfs/snapshot/"
51 #define ZDIFF_PREFIX "zfs-diff-%d"
53 #define ZDIFF_ADDED '+'
54 #define ZDIFF_MODIFIED 'M'
55 #define ZDIFF_REMOVED '-'
56 #define ZDIFF_RENAMED 'R'
60 * Given a {dsname, object id}, get the object path
63 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
64 char *pn, int maxlen, zfs_stat_t *sb)
66 zfs_cmd_t zc = {"\0"};
69 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
73 error = zfs_ioctl(di->zhp->zfs_hdl, ZFS_IOC_OBJ_TO_STATS, &zc);
76 /* we can get stats even if we failed to get a path */
77 (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
79 ASSERT(di->zerr == 0);
80 (void) strlcpy(pn, zc.zc_value, maxlen);
84 if (di->zerr == ESTALE) {
85 (void) snprintf(pn, maxlen, "(on_delete_queue)");
87 } else if (di->zerr == EPERM) {
88 (void) snprintf(di->errbuf, sizeof (di->errbuf),
90 "The sys_config privilege or diff delegated permission "
91 "is needed\nto discover path names"));
93 } else if (di->zerr == EACCES) {
94 (void) snprintf(di->errbuf, sizeof (di->errbuf),
96 "Key must be loaded to discover path names"));
99 (void) snprintf(di->errbuf, sizeof (di->errbuf),
100 dgettext(TEXT_DOMAIN,
101 "Unable to determine path or stats for "
102 "object %lld in %s"), (longlong_t)obj, dsname);
110 * Prints a file name out a character at a time. If the character is
111 * not in the range of what we consider "printable" ASCII, display it
112 * as an escaped 4-digit octal value. ASCII values less than a space
113 * are all control characters and we declare the upper end as the
114 * DELete character. This also is the last 7-bit ASCII character.
115 * We choose to treat all 8-bit ASCII as not printable for this
119 stream_bytes(FILE *fp, const char *string)
123 while ((c = *string++) != '\0') {
124 if (c > ' ' && c != '\\' && c < '\177') {
125 (void) fprintf(fp, "%c", c);
127 (void) fprintf(fp, "\\%04o", (uint8_t)c);
133 print_what(FILE *fp, mode_t what)
137 switch (what & S_IFMT) {
173 (void) fprintf(fp, "%c", symbol);
177 print_cmn(FILE *fp, differ_info_t *di, const char *file)
179 stream_bytes(fp, di->dsmnt);
180 stream_bytes(fp, file);
184 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
188 (void) fprintf(fp, "%10lld.%09lld\t",
189 (longlong_t)isb->zs_ctime[0],
190 (longlong_t)isb->zs_ctime[1]);
191 (void) fprintf(fp, "%c\t", ZDIFF_RENAMED);
193 print_what(fp, isb->zs_mode);
194 (void) fprintf(fp, "\t");
196 print_cmn(fp, di, old);
198 (void) fprintf(fp, "\t");
200 (void) fprintf(fp, " -> ");
201 print_cmn(fp, di, new);
202 (void) fprintf(fp, "\n");
206 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
210 (void) fprintf(fp, "%10lld.%09lld\t",
211 (longlong_t)isb->zs_ctime[0],
212 (longlong_t)isb->zs_ctime[1]);
213 (void) fprintf(fp, "%c\t", ZDIFF_MODIFIED);
215 print_what(fp, isb->zs_mode);
216 (void) fprintf(fp, "\t");
218 print_cmn(fp, di, file);
219 (void) fprintf(fp, "\t(%+d)", delta);
220 (void) fprintf(fp, "\n");
224 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
228 (void) fprintf(fp, "%10lld.%09lld\t",
229 (longlong_t)isb->zs_ctime[0],
230 (longlong_t)isb->zs_ctime[1]);
231 (void) fprintf(fp, "%c\t", type);
233 print_what(fp, isb->zs_mode);
234 (void) fprintf(fp, "\t");
236 print_cmn(fp, di, file);
237 (void) fprintf(fp, "\n");
241 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
243 struct zfs_stat fsb, tsb;
245 char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
246 boolean_t already_logged = B_FALSE;
247 int fobjerr, tobjerr;
250 if (dobj == di->shares)
254 * Check the from and to snapshots for info on the object. If
255 * we get ENOENT, then the object just didn't exist in that
256 * snapshot. If we get ENOTSUP, then we tried to get
257 * info on a non-ZPL object, which we don't care about anyway.
258 * For any other error we print a warning which includes the
259 * errno and continue.
262 fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
264 if (fobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
265 zfs_error_aux(di->zhp->zfs_hdl, "%s", strerror(di->zerr));
266 zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
268 * Let's not print an error for the same object more than
269 * once if it happens in both snapshots
271 already_logged = B_TRUE;
274 tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
277 if (tobjerr && di->zerr != ENOTSUP && di->zerr != ENOENT) {
278 if (!already_logged) {
279 zfs_error_aux(di->zhp->zfs_hdl,
280 "%s", strerror(di->zerr));
281 zfs_error(di->zhp->zfs_hdl, di->zerr, di->errbuf);
285 * Unallocated object sharing the same meta dnode block
287 if (fobjerr && tobjerr) {
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 ||
299 change = tsb.zs_links - fsb.zs_links;
303 print_link_change(fp, di, change, tobjname, &tsb);
306 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
308 } else if (tobjerr) {
310 print_link_change(fp, di, change, fobjname, &fsb);
313 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
317 if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
318 tsb.zs_gen++; /* Force a generational difference */
320 /* Simple modification or no change */
321 if (fsb.zs_gen == tsb.zs_gen) {
322 /* No apparent changes. Could we assert !this? */
323 if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
324 fsb.zs_ctime[1] == tsb.zs_ctime[1])
327 print_link_change(fp, di, change,
328 change > 0 ? fobjname : tobjname, &tsb);
329 } else if (strcmp(fobjname, tobjname) == 0) {
330 print_file(fp, di, ZDIFF_MODIFIED, fobjname, &tsb);
332 print_rename(fp, di, fobjname, tobjname, &tsb);
336 /* file re-created or object re-used */
337 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
338 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
344 write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
349 for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
350 if ((err = write_inuse_diffs_one(fp, di, o)) != 0)
357 describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
362 (void) get_stats_for_obj(di, di->fromsnap, object, namebuf,
365 /* Don't print if in the delete queue on from side */
366 if (di->zerr == ESTALE || di->zerr == ENOENT) {
371 print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
376 write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
378 zfs_cmd_t zc = {"\0"};
379 libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
380 char fobjname[MAXPATHLEN];
382 (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
383 zc.zc_obj = dr->ddr_first - 1;
385 ASSERT(di->zerr == 0);
387 while (zc.zc_obj < dr->ddr_last) {
390 err = zfs_ioctl(lhdl, ZFS_IOC_NEXT_OBJ, &zc);
392 if (zc.zc_obj == di->shares) {
396 if (zc.zc_obj > dr->ddr_last) {
399 err = describe_free(fp, di, zc.zc_obj, fobjname,
401 } else if (errno == ESRCH) {
404 (void) snprintf(di->errbuf, sizeof (di->errbuf),
405 dgettext(TEXT_DOMAIN,
406 "next allocated object (> %lld) find failure"),
407 (longlong_t)zc.zc_obj);
420 differ_info_t *di = arg;
421 dmu_diff_record_t dr;
425 if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
427 strlcpy(di->errbuf, strerror(errno), sizeof (di->errbuf));
428 (void) close(di->datafd);
433 char *cp = (char *)&dr;
434 int len = sizeof (dr);
438 rv = read(di->datafd, cp, len);
441 } while (len > 0 && rv > 0);
443 if (rv < 0 || (rv == 0 && len != sizeof (dr))) {
446 } else if (rv == 0) {
447 /* end of file at a natural breaking point */
451 switch (dr.ddr_type) {
453 err = write_free_diffs(ofp, di, &dr);
456 err = write_inuse_diffs(ofp, di, &dr);
468 (void) close(di->datafd);
472 ASSERT(di->zerr == EPIPE);
473 (void) snprintf(di->errbuf, sizeof (di->errbuf),
474 dgettext(TEXT_DOMAIN,
475 "Internal error: bad data from diff IOCTL"));
482 make_temp_snapshot(differ_info_t *di)
484 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
485 zfs_cmd_t zc = {"\0"};
487 (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
488 ZDIFF_PREFIX, getpid());
489 (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
490 zc.zc_cleanup_fd = di->cleanupfd;
492 if (zfs_ioctl(hdl, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
495 (void) snprintf(di->errbuf, sizeof (di->errbuf),
496 dgettext(TEXT_DOMAIN, "The diff delegated "
497 "permission is needed in order\nto create a "
498 "just-in-time snapshot for diffing\n"));
499 return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
501 (void) snprintf(di->errbuf, sizeof (di->errbuf),
502 dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
503 "snapshot of '%s'"), zc.zc_name);
504 return (zfs_standard_error(hdl, err, di->errbuf));
508 di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
509 di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
514 teardown_differ_info(differ_info_t *di)
523 (void) close(di->cleanupfd);
527 get_snapshot_names(differ_info_t *di, const char *fromsnap,
530 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
538 * fdslen fsnlen tdslen tsnlen
540 * 0. dataset@snap1 dataset@snap2 >0 >1 >0 >1
541 * 1. dataset@snap1 @snap2 >0 >1 ==0 >1
542 * 2. dataset@snap1 dataset >0 >1 >0 ==0
543 * 3. @snap1 dataset@snap2 ==0 >1 >0 >1
544 * 4. @snap1 dataset ==0 >1 >0 ==0
546 if (tosnap == NULL) {
547 /* only a from snapshot given, must be valid */
548 (void) snprintf(di->errbuf, sizeof (di->errbuf),
549 dgettext(TEXT_DOMAIN,
550 "Badly formed snapshot name %s"), fromsnap);
552 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
554 return (zfs_error(hdl, EZFS_INVALIDNAME,
558 atptrf = strchr(fromsnap, '@');
559 ASSERT(atptrf != NULL);
560 fdslen = atptrf - fromsnap;
562 di->fromsnap = zfs_strdup(hdl, fromsnap);
563 di->ds = zfs_strdup(hdl, fromsnap);
564 di->ds[fdslen] = '\0';
566 /* the to snap will be a just-in-time snap of the head */
567 return (make_temp_snapshot(di));
570 (void) snprintf(di->errbuf, sizeof (di->errbuf),
571 dgettext(TEXT_DOMAIN,
572 "Unable to determine which snapshots to compare"));
574 atptrf = strchr(fromsnap, '@');
575 atptrt = strchr(tosnap, '@');
576 fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
577 tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
578 fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
579 tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
581 if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0)) {
582 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
583 } else if ((fdslen > 0 && tdslen > 0) &&
584 ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
586 * not the same dataset name, might be okay if
587 * tosnap is a clone of a fromsnap descendant.
589 char origin[ZFS_MAX_DATASET_NAME_LEN];
593 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
594 (void) strncpy(di->ds, tosnap, tdslen);
595 di->ds[tdslen] = '\0';
597 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
598 while (zhp != NULL) {
599 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
600 sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
601 (void) zfs_close(zhp);
605 if (strncmp(origin, fromsnap, fsnlen) == 0)
608 (void) zfs_close(zhp);
609 zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
613 (void) snprintf(di->errbuf, sizeof (di->errbuf),
614 dgettext(TEXT_DOMAIN,
615 "Not an earlier snapshot from the same fs"));
616 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
618 (void) zfs_close(zhp);
621 di->isclone = B_TRUE;
622 di->fromsnap = zfs_strdup(hdl, fromsnap);
624 di->tosnap = zfs_strdup(hdl, tosnap);
626 return (make_temp_snapshot(di));
629 int dslen = fdslen ? fdslen : tdslen;
631 di->ds = zfs_alloc(hdl, dslen + 1);
632 (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
633 di->ds[dslen] = '\0';
635 di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
637 di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
639 return (make_temp_snapshot(di));
646 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
650 mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
651 if (mounted == B_FALSE) {
652 (void) snprintf(di->errbuf, sizeof (di->errbuf),
653 dgettext(TEXT_DOMAIN,
654 "Cannot diff an unmounted snapshot"));
655 return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
658 /* Avoid a double slash at the beginning of root-mounted datasets */
659 if (**mntpt == '/' && *(*mntpt + 1) == '\0')
665 get_mountpoints(differ_info_t *di)
671 * first get the mountpoint for the parent dataset
673 if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
676 strptr = strchr(di->tosnap, '@');
677 ASSERT3P(strptr, !=, NULL);
678 di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
679 ZDIFF_SNAPDIR, ++strptr);
681 strptr = strchr(di->fromsnap, '@');
682 ASSERT3P(strptr, !=, NULL);
684 frommntpt = di->dsmnt;
690 err = get_mountpoint(di, di->fromsnap, &mntpt);
697 di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
698 ZDIFF_SNAPDIR, ++strptr);
707 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
708 const char *tosnap, differ_info_t *di)
712 di->cleanupfd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
713 VERIFY(di->cleanupfd >= 0);
715 if (get_snapshot_names(di, fromsnap, tosnap) != 0)
718 if (get_mountpoints(di) != 0)
721 if (find_shares_object(di) != 0)
728 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
729 const char *tosnap, int flags)
731 zfs_cmd_t zc = {"\0"};
733 differ_info_t di = { 0 };
738 (void) snprintf(errbuf, sizeof (errbuf),
739 dgettext(TEXT_DOMAIN, "zfs diff failed"));
741 if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
742 teardown_differ_info(&di);
746 if (pipe2(pipefd, O_CLOEXEC)) {
747 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
748 teardown_differ_info(&di);
749 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
752 di.scripted = (flags & ZFS_DIFF_PARSEABLE);
753 di.classify = (flags & ZFS_DIFF_CLASSIFY);
754 di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
757 di.datafd = pipefd[0];
759 if (pthread_create(&tid, NULL, differ, &di)) {
760 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
761 (void) close(pipefd[0]);
762 (void) close(pipefd[1]);
763 teardown_differ_info(&di);
764 return (zfs_error(zhp->zfs_hdl,
765 EZFS_THREADCREATEFAILED, errbuf));
769 (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
770 (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
771 zc.zc_cookie = pipefd[1];
773 iocerr = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DIFF, &zc);
775 (void) snprintf(errbuf, sizeof (errbuf),
776 dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
777 if (errno == EPERM) {
778 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
779 "\n The sys_mount privilege or diff delegated "
780 "permission is needed\n to execute the "
782 } else if (errno == EXDEV) {
783 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
784 "\n Not an earlier snapshot from the same fs"));
785 } else if (errno != EPIPE || di.zerr == 0) {
786 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
788 (void) close(pipefd[1]);
789 (void) pthread_cancel(tid);
790 (void) pthread_join(tid, NULL);
791 teardown_differ_info(&di);
792 if (di.zerr != 0 && di.zerr != EPIPE) {
793 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(di.zerr));
794 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
796 return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
800 (void) close(pipefd[1]);
801 (void) pthread_join(tid, NULL);
804 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(di.zerr));
805 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
807 teardown_differ_info(&di);