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.
34 #include <sys/types.h>
42 #include <sys/zfs_ioctl.h>
44 #include "libzfs_impl.h"
46 #define ZDIFF_SNAPDIR "/.zfs/snapshot/"
47 #define ZDIFF_SHARESDIR "/.zfs/shares/"
48 #define ZDIFF_PREFIX "zfs-diff-%d"
50 #define ZDIFF_ADDED '+'
51 #define ZDIFF_MODIFIED 'M'
52 #define ZDIFF_REMOVED '-'
53 #define ZDIFF_RENAMED 'R'
56 do_name_cmp(const char *fpath, const char *tpath)
59 fname = strrchr(fpath, '/') + 1;
60 tname = strrchr(tpath, '/') + 1;
61 return (strcmp(fname, tname) == 0);
64 typedef struct differ_info {
77 boolean_t timestamped;
86 * Given a {dsname, object id}, get the object path
89 get_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
90 char *pn, int maxlen, zfs_stat_t *sb)
95 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
99 error = ioctl(di->zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_STATS, &zc);
102 /* we can get stats even if we failed to get a path */
103 (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
105 ASSERT(di->zerr == 0);
106 (void) strlcpy(pn, zc.zc_value, maxlen);
110 if (di->zerr == EPERM) {
111 (void) snprintf(di->errbuf, sizeof (di->errbuf),
112 dgettext(TEXT_DOMAIN,
113 "The sys_config privilege or diff delegated permission "
114 "is needed\nto discover path names"));
117 (void) snprintf(di->errbuf, sizeof (di->errbuf),
118 dgettext(TEXT_DOMAIN,
119 "Unable to determine path or stats for "
120 "object %lld in %s"), obj, dsname);
128 * Prints a file name out a character at a time. If the character is
129 * not in the range of what we consider "printable" ASCII, display it
130 * as an escaped 3-digit octal value. ASCII values less than a space
131 * are all control characters and we declare the upper end as the
132 * DELete character. This also is the last 7-bit ASCII character.
133 * We choose to treat all 8-bit ASCII as not printable for this
137 stream_bytes(FILE *fp, const char *string)
140 if (*string > ' ' && *string != '\\' && *string < '\177')
141 (void) fprintf(fp, "%c", *string++);
143 (void) fprintf(fp, "\\%03hho",
144 (unsigned char)*string++);
150 print_what(FILE *fp, mode_t what)
154 switch (what & S_IFMT) {
190 (void) fprintf(fp, "%c", symbol);
194 print_cmn(FILE *fp, differ_info_t *di, const char *file)
196 stream_bytes(fp, di->dsmnt);
197 stream_bytes(fp, file);
201 print_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
205 (void) fprintf(fp, "%10lld.%09lld\t",
206 (longlong_t)isb->zs_ctime[0],
207 (longlong_t)isb->zs_ctime[1]);
208 (void) fprintf(fp, "%c\t", ZDIFF_RENAMED);
210 print_what(fp, isb->zs_mode);
211 (void) fprintf(fp, "\t");
213 print_cmn(fp, di, old);
215 (void) fprintf(fp, "\t");
217 (void) fprintf(fp, " -> ");
218 print_cmn(fp, di, new);
219 (void) fprintf(fp, "\n");
223 print_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
227 (void) fprintf(fp, "%10lld.%09lld\t",
228 (longlong_t)isb->zs_ctime[0],
229 (longlong_t)isb->zs_ctime[1]);
230 (void) fprintf(fp, "%c\t", ZDIFF_MODIFIED);
232 print_what(fp, isb->zs_mode);
233 (void) fprintf(fp, "\t");
235 print_cmn(fp, di, file);
236 (void) fprintf(fp, "\t(%+d)", delta);
237 (void) fprintf(fp, "\n");
241 print_file(FILE *fp, differ_info_t *di, char type, const char *file,
245 (void) fprintf(fp, "%10lld.%09lld\t",
246 (longlong_t)isb->zs_ctime[0],
247 (longlong_t)isb->zs_ctime[1]);
248 (void) fprintf(fp, "%c\t", type);
250 print_what(fp, isb->zs_mode);
251 (void) fprintf(fp, "\t");
253 print_cmn(fp, di, file);
254 (void) fprintf(fp, "\n");
258 write_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
260 struct zfs_stat fsb, tsb;
263 char fobjname[MAXPATHLEN], tobjname[MAXPATHLEN];
264 int fobjerr, tobjerr;
267 if (dobj == di->shares)
271 * Check the from and to snapshots for info on the object. If
272 * we get ENOENT, then the object just didn't exist in that
273 * snapshot. If we get ENOTSUP, then we tried to get
274 * info on a non-ZPL object, which we don't care about anyway.
276 fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
278 if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
281 tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
283 if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
287 * Unallocated object sharing the same meta dnode block
289 if (fobjerr && tobjerr) {
290 ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
295 di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
296 fmode = fsb.zs_mode & S_IFMT;
297 tmode = tsb.zs_mode & S_IFMT;
298 if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
302 change = tsb.zs_links - fsb.zs_links;
306 print_link_change(fp, di, change, tobjname, &tsb);
309 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
311 } else if (tobjerr) {
313 print_link_change(fp, di, change, fobjname, &fsb);
316 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
320 if (fmode != tmode && fsb.zs_gen == tsb.zs_gen)
321 tsb.zs_gen++; /* Force a generational difference */
322 same_name = do_name_cmp(fobjname, tobjname);
324 /* Simple modification or no change */
325 if (fsb.zs_gen == tsb.zs_gen) {
326 /* No apparent changes. Could we assert !this? */
327 if (fsb.zs_ctime[0] == tsb.zs_ctime[0] &&
328 fsb.zs_ctime[1] == tsb.zs_ctime[1])
331 print_link_change(fp, di, change,
332 change > 0 ? fobjname : tobjname, &tsb);
333 } else if (same_name) {
334 print_file(fp, di, ZDIFF_MODIFIED, fobjname, &tsb);
336 print_rename(fp, di, fobjname, tobjname, &tsb);
340 /* file re-created or object re-used */
341 print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
342 print_file(fp, di, ZDIFF_ADDED, tobjname, &tsb);
348 write_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
353 for (o = dr->ddr_first; o <= dr->ddr_last; o++) {
354 if (err = write_inuse_diffs_one(fp, di, o))
361 describe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
366 if (get_stats_for_obj(di, di->fromsnap, object, namebuf,
368 /* Let it slide, if in the delete queue on from side */
369 if (di->zerr == ENOENT && sb.zs_links == 0) {
376 print_file(fp, di, ZDIFF_REMOVED, namebuf, &sb);
381 write_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
383 zfs_cmd_t zc = { 0 };
384 libzfs_handle_t *lhdl = di->zhp->zfs_hdl;
385 char fobjname[MAXPATHLEN];
387 (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
388 zc.zc_obj = dr->ddr_first - 1;
390 ASSERT(di->zerr == 0);
392 while (zc.zc_obj < dr->ddr_last) {
395 err = ioctl(lhdl->libzfs_fd, ZFS_IOC_NEXT_OBJ, &zc);
397 if (zc.zc_obj == di->shares) {
401 if (zc.zc_obj > dr->ddr_last) {
404 err = describe_free(fp, di, zc.zc_obj, fobjname,
408 } else if (errno == ESRCH) {
411 (void) snprintf(di->errbuf, sizeof (di->errbuf),
412 dgettext(TEXT_DOMAIN,
413 "next allocated object (> %lld) find failure"),
427 differ_info_t *di = arg;
428 dmu_diff_record_t dr;
432 if ((ofp = fdopen(di->outputfd, "w")) == NULL) {
434 (void) strerror_r(errno, di->errbuf, sizeof (di->errbuf));
435 (void) close(di->datafd);
440 char *cp = (char *)&dr;
441 int len = sizeof (dr);
445 rv = read(di->datafd, cp, len);
448 } while (len > 0 && rv > 0);
450 if (rv < 0 || (rv == 0 && len != sizeof (dr))) {
453 } else if (rv == 0) {
454 /* end of file at a natural breaking point */
458 switch (dr.ddr_type) {
460 err = write_free_diffs(ofp, di, &dr);
463 err = write_inuse_diffs(ofp, di, &dr);
475 (void) close(di->datafd);
479 ASSERT(di->zerr == EINVAL);
480 (void) snprintf(di->errbuf, sizeof (di->errbuf),
481 dgettext(TEXT_DOMAIN,
482 "Internal error: bad data from diff IOCTL"));
489 find_shares_object(differ_info_t *di)
491 char fullpath[MAXPATHLEN];
492 struct stat64 sb = { 0 };
494 (void) strlcpy(fullpath, di->dsmnt, MAXPATHLEN);
495 (void) strlcat(fullpath, ZDIFF_SHARESDIR, MAXPATHLEN);
497 if (stat64(fullpath, &sb) != 0) {
499 (void) snprintf(di->errbuf, sizeof (di->errbuf),
500 dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath);
501 return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf));
507 di->shares = (uint64_t)sb.st_ino;
512 make_temp_snapshot(differ_info_t *di)
514 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
515 zfs_cmd_t zc = { 0 };
517 (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
518 ZDIFF_PREFIX, getpid());
519 (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
520 zc.zc_cleanup_fd = di->cleanupfd;
522 if (ioctl(hdl->libzfs_fd, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
525 (void) snprintf(di->errbuf, sizeof (di->errbuf),
526 dgettext(TEXT_DOMAIN, "The diff delegated "
527 "permission is needed in order\nto create a "
528 "just-in-time snapshot for diffing\n"));
529 return (zfs_error(hdl, EZFS_DIFF, di->errbuf));
531 (void) snprintf(di->errbuf, sizeof (di->errbuf),
532 dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
533 "snapshot of '%s'"), zc.zc_name);
534 return (zfs_standard_error(hdl, err, di->errbuf));
538 di->tmpsnap = zfs_strdup(hdl, zc.zc_value);
539 di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
544 teardown_differ_info(differ_info_t *di)
553 (void) close(di->cleanupfd);
557 get_snapshot_names(differ_info_t *di, const char *fromsnap,
560 libzfs_handle_t *hdl = di->zhp->zfs_hdl;
569 * dataset@snap1 dataset@snap2
570 * dataset@snap1 @snap2
571 * dataset@snap1 dataset
572 * @snap1 dataset@snap2
574 if (tosnap == NULL) {
575 /* only a from snapshot given, must be valid */
576 (void) snprintf(di->errbuf, sizeof (di->errbuf),
577 dgettext(TEXT_DOMAIN,
578 "Badly formed snapshot name %s"), fromsnap);
580 if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
582 return (zfs_error(hdl, EZFS_INVALIDNAME,
586 atptrf = strchr(fromsnap, '@');
587 ASSERT(atptrf != NULL);
588 fdslen = atptrf - fromsnap;
590 di->fromsnap = zfs_strdup(hdl, fromsnap);
591 di->ds = zfs_strdup(hdl, fromsnap);
592 di->ds[fdslen] = '\0';
594 /* the to snap will be a just-in-time snap of the head */
595 return (make_temp_snapshot(di));
598 (void) snprintf(di->errbuf, sizeof (di->errbuf),
599 dgettext(TEXT_DOMAIN,
600 "Unable to determine which snapshots to compare"));
602 atptrf = strchr(fromsnap, '@');
603 atptrt = strchr(tosnap, '@');
604 fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
605 tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
606 fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
607 tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
609 if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0) ||
610 (fsnlen == 0 && tsnlen == 0)) {
611 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
612 } else if ((fdslen > 0 && tdslen > 0) &&
613 ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
615 * not the same dataset name, might be okay if
616 * tosnap is a clone of a fromsnap descendant.
618 char origin[ZFS_MAXNAMELEN];
622 di->ds = zfs_alloc(di->zhp->zfs_hdl, tdslen + 1);
623 (void) strncpy(di->ds, tosnap, tdslen);
624 di->ds[tdslen] = '\0';
626 zhp = zfs_open(hdl, di->ds, ZFS_TYPE_FILESYSTEM);
627 while (zhp != NULL) {
628 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
629 sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
630 (void) zfs_close(zhp);
634 if (strncmp(origin, fromsnap, fsnlen) == 0)
637 (void) zfs_close(zhp);
638 zhp = zfs_open(hdl, origin, ZFS_TYPE_FILESYSTEM);
642 (void) snprintf(di->errbuf, sizeof (di->errbuf),
643 dgettext(TEXT_DOMAIN,
644 "Not an earlier snapshot from the same fs"));
645 return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
647 (void) zfs_close(zhp);
650 di->isclone = B_TRUE;
651 di->fromsnap = zfs_strdup(hdl, fromsnap);
653 di->tosnap = zfs_strdup(hdl, tosnap);
655 return (make_temp_snapshot(di));
658 int dslen = fdslen ? fdslen : tdslen;
660 di->ds = zfs_alloc(hdl, dslen + 1);
661 (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
662 di->ds[dslen] = '\0';
664 di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
666 di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
668 return (make_temp_snapshot(di));
675 get_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
679 mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
680 if (mounted == B_FALSE) {
681 (void) snprintf(di->errbuf, sizeof (di->errbuf),
682 dgettext(TEXT_DOMAIN,
683 "Cannot diff an unmounted snapshot"));
684 return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
687 /* Avoid a double slash at the beginning of root-mounted datasets */
688 if (**mntpt == '/' && *(*mntpt + 1) == '\0')
694 get_mountpoints(differ_info_t *di)
700 * first get the mountpoint for the parent dataset
702 if (get_mountpoint(di, di->ds, &di->dsmnt) != 0)
705 strptr = strchr(di->tosnap, '@');
706 ASSERT3P(strptr, !=, NULL);
707 di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
708 ZDIFF_SNAPDIR, ++strptr);
710 strptr = strchr(di->fromsnap, '@');
711 ASSERT3P(strptr, !=, NULL);
713 frommntpt = di->dsmnt;
719 err = get_mountpoint(di, di->fromsnap, &mntpt);
726 di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
727 ZDIFF_SNAPDIR, ++strptr);
736 setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
737 const char *tosnap, differ_info_t *di)
741 di->cleanupfd = open(ZFS_DEV, O_RDWR|O_EXCL);
742 VERIFY(di->cleanupfd >= 0);
744 if (get_snapshot_names(di, fromsnap, tosnap) != 0)
747 if (get_mountpoints(di) != 0)
750 if (find_shares_object(di) != 0)
757 zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
758 const char *tosnap, int flags)
760 zfs_cmd_t zc = { 0 };
762 differ_info_t di = { 0 };
767 (void) snprintf(errbuf, sizeof (errbuf),
768 dgettext(TEXT_DOMAIN, "zfs diff failed"));
770 if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
771 teardown_differ_info(&di);
776 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
777 teardown_differ_info(&di);
778 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
781 di.scripted = (flags & ZFS_DIFF_PARSEABLE);
782 di.classify = (flags & ZFS_DIFF_CLASSIFY);
783 di.timestamped = (flags & ZFS_DIFF_TIMESTAMP);
786 di.datafd = pipefd[0];
788 if (pthread_create(&tid, NULL, differ, &di)) {
789 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
790 (void) close(pipefd[0]);
791 (void) close(pipefd[1]);
792 teardown_differ_info(&di);
793 return (zfs_error(zhp->zfs_hdl,
794 EZFS_THREADCREATEFAILED, errbuf));
798 (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
799 (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
800 zc.zc_cookie = pipefd[1];
802 iocerr = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DIFF, &zc);
804 (void) snprintf(errbuf, sizeof (errbuf),
805 dgettext(TEXT_DOMAIN, "Unable to obtain diffs"));
806 if (errno == EPERM) {
807 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
808 "\n The sys_mount privilege or diff delegated "
809 "permission is needed\n to execute the "
811 } else if (errno == EXDEV) {
812 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
813 "\n Not an earlier snapshot from the same fs"));
814 } else if (errno != EPIPE || di.zerr == 0) {
815 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
817 (void) close(pipefd[1]);
818 (void) pthread_cancel(tid);
819 (void) pthread_join(tid, NULL);
820 teardown_differ_info(&di);
821 if (di.zerr != 0 && di.zerr != EPIPE) {
822 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
823 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
825 return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));
829 (void) close(pipefd[1]);
830 (void) pthread_join(tid, NULL);
833 zfs_error_aux(zhp->zfs_hdl, strerror(di.zerr));
834 return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
836 teardown_differ_info(&di);