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.
33 #include <sys/types.h>
41 #include <sys/zfs_ioctl.h>
43 #include "libzfs_impl.h"
45 #define ZDIFF_SNAPDIR "/.zfs/snapshot/"
46 #define ZDIFF_SHARESDIR "/.zfs/shares/"
47 #define ZDIFF_PREFIX "zfs-diff-%d"
49 #define ZDIFF_ADDED '+'
50 #define ZDIFF_MODIFIED 'M'
51 #define ZDIFF_REMOVED '-'
52 #define ZDIFF_RENAMED 'R'
55 do_name_cmp(const char *fpath, const char *tpath)
58 fname = strrchr(fpath, '/') + 1;
59 tname = strrchr(tpath, '/') + 1;
60 return (strcmp(fname, tname) == 0);
63 typedef struct differ_info {
76 boolean_t timestamped;
85 * Given a {dsname, object id}, get the object path
88 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
89 char *pn, int maxlen, zfs_stat_t *sb)
94 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
98 error = ioctl(di->zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_STATS, &zc);
101 /* we can get stats even if we failed to get a path */
102 (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
104 ASSERT(di->zerr == 0);
105 (void) strlcpy(pn, zc.zc_value, maxlen);
109 if (di->zerr == EPERM) {
110 (void) snprintf(di->errbuf, sizeof (di->errbuf),
111 dgettext(TEXT_DOMAIN,
112 "The sys_config privilege or diff delegated permission "
113 "is needed\nto discover path names"));
116 (void) snprintf(di->errbuf, sizeof (di->errbuf),
117 dgettext(TEXT_DOMAIN,
118 "Unable to determine path or stats for "
119 "object %lld in %s"), obj, dsname);
127 * Prints a file name out a character at a time. If the character is
128 * not in the range of what we consider "printable" ASCII, display it
129 * as an escaped 3-digit octal value. ASCII values less than a space
130 * are all control characters and we declare the upper end as the
131 * DELete character. This also is the last 7-bit ASCII character.
132 * We choose to treat all 8-bit ASCII as not printable for this
136 stream_bytes(FILE *fp, const char *string)
139 if (*string > ' ' && *string != '\\' && *string < '\177')
140 (void) fprintf(fp, "%c", *string++);
142 (void) fprintf(fp, "\\%03hho",
143 (unsigned char)*string++);
149 print_what(FILE *fp, mode_t what)
153 switch (what & S_IFMT) {
189 (void) fprintf(fp, "%c", symbol);
193 print_cmn(FILE *fp, differ_info_t *di, const char *file)
195 stream_bytes(fp, di->dsmnt);
196 stream_bytes(fp, file);
200 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
204 (void) fprintf(fp, "%10lld.%09lld\t",
205 (longlong_t)isb->zs_ctime[0],
206 (longlong_t)isb->zs_ctime[1]);
207 (void) fprintf(fp, "%c\t", ZDIFF_RENAMED);
209 print_what(fp, isb->zs_mode);
210 (void) fprintf(fp, "\t");
212 print_cmn(fp, di, old);
214 (void) fprintf(fp, "\t");
216 (void) fprintf(fp, " -> ");
217 print_cmn(fp, di, new);
218 (void) fprintf(fp, "\n");
222 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
226 (void) fprintf(fp, "%10lld.%09lld\t",
227 (longlong_t)isb->zs_ctime[0],
228 (longlong_t)isb->zs_ctime[1]);
229 (void) fprintf(fp, "%c\t", ZDIFF_MODIFIED);
231 print_what(fp, isb->zs_mode);
232 (void) fprintf(fp, "\t");
234 print_cmn(fp, di, file);
235 (void) fprintf(fp, "\t(%+d)", delta);
236 (void) fprintf(fp, "\n");
240 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
244 (void) fprintf(fp, "%10lld.%09lld\t",
245 (longlong_t)isb->zs_ctime[0],
246 (longlong_t)isb->zs_ctime[1]);
247 (void) fprintf(fp, "%c\t", type);
249 print_what(fp, isb->zs_mode);
250 (void) fprintf(fp, "\t");
252 print_cmn(fp, di, file);
253 (void) fprintf(fp, "\n");
257 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
259 struct zfs_stat fsb, tsb;
262 char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
263 int fobjerr, tobjerr;
266 if (dobj == di->shares)
270 * Check the from and to snapshots for info on the object. If
271 * we get ENOENT, then the object just didn't exist in that
272 * snapshot. If we get ENOTSUP, then we tried to get
273 * info on a non-ZPL object, which we don't care about anyway.
275 fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
277 if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
280 tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
282 if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
286 * Unallocated object sharing the same meta dnode block
288 if (fobjerr && tobjerr) {
289 ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
294 di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
295 fmode = fsb.zs_mode & S_IFMT;
296 tmode = tsb.zs_mode & S_IFMT;
297 if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
301 change = tsb.zs_links - fsb.zs_links;
305 print_link_change(fp, di, change, tobjname, &tsb);
308 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
310 } else if (tobjerr) {
312 print_link_change(fp, di, change, fobjname, &fsb);
315 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
319 if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
320 tsb.zs_gen++; /* Force a generational difference */
321 same_name = do_name_cmp(fobjname, tobjname);
323 /* Simple modification or no change */
324 if (fsb.zs_gen == tsb.zs_gen) {
325 /* No apparent changes. Could we assert !this? */
326 if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
327 fsb.zs_ctime[1] == tsb.zs_ctime[1])
330 print_link_change(fp, di, change,
331 change > 0 ? fobjname : tobjname, &tsb);
332 } else if (same_name) {
333 print_file(fp, di, ZDIFF_MODIFIED, fobjname, &tsb);
335 print_rename(fp, di, fobjname, tobjname, &tsb);
339 /* file re-created or object re-used */
340 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
341 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
347 write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
352 for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
353 if (err = write_inuse_diffs_one(fp, di, o))
360 describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
365 if (get_stats_for_obj(di, di->fromsnap, object, namebuf,
367 /* Let it slide, if in the delete queue on from side */
368 if (di->zerr == ENOENT && sb.zs_links == 0) {
375 print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
380 write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
382 zfs_cmd_t zc = { 0 };
383 libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
384 char fobjname[MAXPATHLEN];
386 (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
387 zc.zc_obj = dr->ddr_first - 1;
389 ASSERT(di->zerr == 0);
391 while (zc.zc_obj < dr->ddr_last) {
394 err = ioctl(lhdl->libzfs_fd, ZFS_IOC_NEXT_OBJ, &zc);
396 if (zc.zc_obj == di->shares) {
400 if (zc.zc_obj > dr->ddr_last) {
403 err = describe_free(fp, di, zc.zc_obj, fobjname,
407 } else if (errno == ESRCH) {
410 (void) snprintf(di->errbuf, sizeof (di->errbuf),
411 dgettext(TEXT_DOMAIN,
412 "next allocated object (> %lld) find failure"),
426 differ_info_t *di = arg;
427 dmu_diff_record_t dr;
431 if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
433 (void) strerror_r(errno, di->errbuf, sizeof (di->errbuf));
434 (void) close(di->datafd);
439 char *cp = (char *)&dr;
440 int len = sizeof (dr);
444 rv = read(di->datafd, cp, len);
447 } while (len > 0 && rv > 0);
449 if (rv < 0 || (rv == 0 && len != sizeof (dr))) {
452 } else if (rv == 0) {
453 /* end of file at a natural breaking point */
457 switch (dr.ddr_type) {
459 err = write_free_diffs(ofp, di, &dr);
462 err = write_inuse_diffs(ofp, di, &dr);
474 (void) close(di->datafd);
478 ASSERT(di->zerr == EINVAL);
479 (void) snprintf(di->errbuf, sizeof (di->errbuf),
480 dgettext(TEXT_DOMAIN,
481 "Internal error: bad data from diff IOCTL"));
488 find_shares_object(differ_info_t *di)
490 char fullpath[MAXPATHLEN];
491 struct stat64 sb = { 0 };
493 (void) strlcpy(fullpath, di->dsmnt, MAXPATHLEN);
494 (void) strlcat(fullpath, ZDIFF_SHARESDIR, MAXPATHLEN);
496 if (stat64(fullpath, &sb) != 0) {
498 (void) snprintf(di->errbuf, sizeof (di->errbuf),
499 dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath);
500 return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf));
506 di->shares = (uint64_t)sb.st_ino;
511 make_temp_snapshot(differ_info_t *di)
513 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
514 zfs_cmd_t zc = { 0 };
516 (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
517 ZDIFF_PREFIX, getpid());
518 (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
519 zc.zc_cleanup_fd = di->cleanupfd;
521 if (ioctl(hdl->libzfs_fd, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
524 (void) snprintf(di->errbuf, sizeof (di->errbuf),
525 dgettext(TEXT_DOMAIN, "The diff delegated "
526 "permission is needed in order\nto create a "
527 "just-in-time snapshot for diffing\n"));
528 return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
530 (void) snprintf(di->errbuf, sizeof (di->errbuf),
531 dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
532 "snapshot of '%s'"), zc.zc_name);
533 return (zfs_standard_error(hdl, err, di->errbuf));
537 di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
538 di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
543 teardown_differ_info(differ_info_t *di)
552 (void) close(di->cleanupfd);
556 get_snapshot_names(differ_info_t *di, const char *fromsnap,
559 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
568 * dataset@snap1 dataset@snap2
569 * dataset@snap1 @snap2
570 * dataset@snap1 dataset
571 * @snap1 dataset@snap2
573 if (tosnap == NULL) {
574 /* only a from snapshot given, must be valid */
575 (void) snprintf(di->errbuf, sizeof (di->errbuf),
576 dgettext(TEXT_DOMAIN,
577 "Badly formed snapshot name %s"), fromsnap);
579 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
581 return (zfs_error(hdl, EZFS_INVALIDNAME,
585 atptrf = strchr(fromsnap, '@');
586 ASSERT(atptrf != NULL);
587 fdslen = atptrf - fromsnap;
589 di->fromsnap = zfs_strdup(hdl, fromsnap);
590 di->ds = zfs_strdup(hdl, fromsnap);
591 di->ds[fdslen] = '\0';
593 /* the to snap will be a just-in-time snap of the head */
594 return (make_temp_snapshot(di));
597 (void) snprintf(di->errbuf, sizeof (di->errbuf),
598 dgettext(TEXT_DOMAIN,
599 "Unable to determine which snapshots to compare"));
601 atptrf = strchr(fromsnap, '@');
602 atptrt = strchr(tosnap, '@');
603 fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
604 tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
605 fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
606 tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
608 if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0) ||
609 (fsnlen == 0 && tsnlen == 0)) {
610 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
611 } else if ((fdslen > 0 && tdslen > 0) &&
612 ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
614 * not the same dataset name, might be okay if
615 * tosnap is a clone of a fromsnap descendant.
617 char origin[ZFS_MAXNAMELEN];
621 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
622 (void) strncpy(di->ds, tosnap, tdslen);
623 di->ds[tdslen] = '\0';
625 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
626 while (zhp != NULL) {
627 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
628 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
630 if (strncmp(origin, fromsnap, fsnlen) == 0)
633 (void) zfs_close(zhp);
634 zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
638 (void) snprintf(di->errbuf, sizeof (di->errbuf),
639 dgettext(TEXT_DOMAIN,
640 "Not an earlier snapshot from the same fs"));
641 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
643 (void) zfs_close(zhp);
646 di->isclone = B_TRUE;
647 di->fromsnap = zfs_strdup(hdl, fromsnap);
649 di->tosnap = zfs_strdup(hdl, tosnap);
651 return (make_temp_snapshot(di));
654 int dslen = fdslen ? fdslen : tdslen;
656 di->ds = zfs_alloc(hdl, dslen + 1);
657 (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
658 di->ds[dslen] = '\0';
660 di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
662 di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
664 return (make_temp_snapshot(di));
671 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
675 mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
676 if (mounted == B_FALSE) {
677 (void) snprintf(di->errbuf, sizeof (di->errbuf),
678 dgettext(TEXT_DOMAIN,
679 "Cannot diff an unmounted snapshot"));
680 return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
683 /* Avoid a double slash at the beginning of root-mounted datasets */
684 if (**mntpt == '/' && *(*mntpt + 1) == '\0')
690 get_mountpoints(differ_info_t *di)
696 * first get the mountpoint for the parent dataset
698 if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
701 strptr = strchr(di->tosnap, '@');
702 ASSERT3P(strptr, !=, NULL);
703 di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
704 ZDIFF_SNAPDIR, ++strptr);
706 strptr = strchr(di->fromsnap, '@');
707 ASSERT3P(strptr, !=, NULL);
709 frommntpt = di->dsmnt;
715 err = get_mountpoint(di, di->fromsnap, &mntpt);
722 di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
723 ZDIFF_SNAPDIR, ++strptr);
732 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
733 const char *tosnap, differ_info_t *di)
737 di->cleanupfd = open(ZFS_DEV, O_RDWR|O_EXCL);
738 VERIFY(di->cleanupfd >= 0);
740 if (get_snapshot_names(di, fromsnap, tosnap) != 0)
743 if (get_mountpoints(di) != 0)
746 if (find_shares_object(di) != 0)
753 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
754 const char *tosnap, int flags)
756 zfs_cmd_t zc = { 0 };
758 differ_info_t di = { 0 };
763 (void) snprintf(errbuf, sizeof (errbuf),
764 dgettext(TEXT_DOMAIN, "zfs diff failed"));
766 if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
767 teardown_differ_info(&di);
772 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
773 teardown_differ_info(&di);
774 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
777 di.scripted = (flags & ZFS_DIFF_PARSEABLE);
778 di.classify = (flags & ZFS_DIFF_CLASSIFY);
779 di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
782 di.datafd = pipefd[0];
784 if (pthread_create(&tid, NULL, differ, &di)) {
785 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
786 (void) close(pipefd[0]);
787 (void) close(pipefd[1]);
788 teardown_differ_info(&di);
789 return (zfs_error(zhp->zfs_hdl,
790 EZFS_THREADCREATEFAILED, errbuf));
794 (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
795 (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
796 zc.zc_cookie = pipefd[1];
798 iocerr = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DIFF, &zc);
800 (void) snprintf(errbuf, sizeof (errbuf),
801 dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
802 if (errno == EPERM) {
803 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
804 "\n The sys_mount privilege or diff delegated "
805 "permission is needed\n to execute the "
807 } else if (errno == EXDEV) {
808 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
809 "\n Not an earlier snapshot from the same fs"));
810 } else if (errno != EPIPE || di.zerr == 0) {
811 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
813 (void) close(pipefd[1]);
814 (void) pthread_cancel(tid);
815 (void) pthread_join(tid, NULL);
816 teardown_differ_info(&di);
817 if (di.zerr != 0 && di.zerr != EPIPE) {
818 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
819 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
821 return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
825 (void) close(pipefd[1]);
826 (void) pthread_join(tid, NULL);
829 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
830 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
832 teardown_differ_info(&di);