99d5e173470cf967aa87653364ed614299e7b511Tim Haley * CDDL HEADER START
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * The contents of this file are subject to the terms of the
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * Common Development and Distribution License (the "License").
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * You may not use this file except in compliance with the License.
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * See the License for the specific language governing permissions
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * and limitations under the License.
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * When distributing Covered Code, include this CDDL HEADER in each
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * If applicable, add the following below this CDDL HEADER, with the
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * fields enclosed by brackets "[]" replaced with your own identifying
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * information: Portions Copyright [yyyy] [name of copyright owner]
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * CDDL HEADER END
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
8430278980a48338e04c7dd52b495b7f1551367aAlexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens * Copyright (c) 2015 by Delphix. All rights reserved.
b211eb9181f99c20acbf4c528f94cb44b4ca8c31Joshua M. Clulow * Copyright 2016 Joyent, Inc.
88f61dee20b358671b1b643e9d1dbf220a1d69beIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * zfs diff support
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * Given a {dsname, object id}, get the object path
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyget_stats_for_obj(differ_info_t *di, const char *dsname, uint64_t obj,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley error = ioctl(di->zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_STATS, &zc);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* we can get stats even if we failed to get a path */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) memcpy(sb, &zc.zc_stat, sizeof (zfs_stat_t));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "The sys_config privilege or diff delegated permission "
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "is needed\nto discover path names"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "Unable to determine path or stats for "
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * stream_bytes
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * Prints a file name out a character at a time. If the character is
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * not in the range of what we consider "printable" ASCII, display it
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * as an escaped 3-digit octal value. ASCII values less than a space
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * are all control characters and we declare the upper end as the
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * DELete character. This also is the last 7-bit ASCII character.
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * We choose to treat all 8-bit ASCII as not printable for this
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * application.
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyprint_cmn(FILE *fp, differ_info_t *di, const char *file)
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyprint_rename(FILE *fp, differ_info_t *di, const char *old, const char *new,
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyprint_link_change(FILE *fp, differ_info_t *di, int delta, const char *file,
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyprint_file(FILE *fp, differ_info_t *di, char type, const char *file,
99d5e173470cf967aa87653364ed614299e7b511Tim Haleywrite_inuse_diffs_one(FILE *fp, differ_info_t *di, uint64_t dobj)
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * Check the from and to snapshots for info on the object. If
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * we get ENOENT, then the object just didn't exist in that
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * snapshot. If we get ENOTSUP, then we tried to get
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * info on a non-ZPL object, which we don't care about anyway.
99d5e173470cf967aa87653364ed614299e7b511Tim Haley fobjerr = get_stats_for_obj(di, di->fromsnap, dobj, fobjname,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (fobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley tobjerr = get_stats_for_obj(di, di->tosnap, dobj, tobjname,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (tobjerr && di->zerr != ENOENT && di->zerr != ENOTSUP)
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * Unallocated object sharing the same meta dnode block
99d5e173470cf967aa87653364ed614299e7b511Tim Haley ASSERT(di->zerr == ENOENT || di->zerr == ENOTSUP);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley di->zerr = 0; /* negate get_stats_for_obj() from side that failed */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (fmode == S_IFDIR || tmode == S_IFDIR || fsb.zs_links == 0 ||
99d5e173470cf967aa87653364ed614299e7b511Tim Haley print_link_change(fp, di, change, tobjname, &tsb);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley } else if (tobjerr) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley print_link_change(fp, di, change, fobjname, &fsb);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley tsb.zs_gen++; /* Force a generational difference */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* Simple modification or no change */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* No apparent changes. Could we assert !this? */
aab04418a72c0a29040a5da7eec08efe19dbef04Joshua M. Clulow } else if (strcmp(fobjname, tobjname) == 0) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley print_file(fp, di, ZDIFF_MODIFIED, fobjname, &tsb);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* file re-created or object re-used */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley print_file(fp, di, ZDIFF_REMOVED, fobjname, &fsb);
99d5e173470cf967aa87653364ed614299e7b511Tim Haleywrite_inuse_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
88f61dee20b358671b1b643e9d1dbf220a1d69beIgor Kozhukhov if ((err = write_inuse_diffs_one(fp, di, o)) != 0)
99d5e173470cf967aa87653364ed614299e7b511Tim Haleydescribe_free(FILE *fp, differ_info_t *di, uint64_t object, char *namebuf,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (get_stats_for_obj(di, di->fromsnap, object, namebuf,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* Let it slide, if in the delete queue on from side */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haleywrite_free_diffs(FILE *fp, differ_info_t *di, dmu_diff_record_t *dr)
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strlcpy(zc.zc_name, di->fromsnap, sizeof (zc.zc_name));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley err = ioctl(lhdl->libzfs_fd, ZFS_IOC_NEXT_OBJ, &zc);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (err == 0) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "next allocated object (> %lld) find failure"),
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haleystatic void *
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strerror_r(errno, di->errbuf, sizeof (di->errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return ((void *)-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley } else if (rv == 0) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* end of file at a natural breaking point */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return ((void *)-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "Internal error: bad data from diff IOCTL"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return ((void *)-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return ((void *)0);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strlcat(fullpath, ZDIFF_SHARESDIR, MAXPATHLEN);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley dgettext(TEXT_DOMAIN, "Cannot stat %s"), fullpath);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_error(di->zhp->zfs_hdl, EZFS_DIFF, di->errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) snprintf(zc.zc_value, sizeof (zc.zc_value),
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strlcpy(zc.zc_name, di->ds, sizeof (zc.zc_name));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (ioctl(hdl->libzfs_fd, ZFS_IOC_TMP_SNAPSHOT, &zc) != 0) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "permission is needed in order\nto create a "
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "just-in-time snapshot for diffing\n"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley dgettext(TEXT_DOMAIN, "Cannot create just-in-time "
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_standard_error(hdl, err, di->errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley di->tosnap = zfs_asprintf(hdl, "%s@%s", di->ds, di->tmpsnap);
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyget_snapshot_names(differ_info_t *di, const char *fromsnap,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley const char *tosnap)
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * Can accept
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * dataset@snap1
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * dataset@snap1 dataset@snap2
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * dataset@snap1 @snap2
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * dataset@snap1 dataset
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * @snap1 dataset@snap2
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* only a from snapshot given, must be valid */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (!zfs_validate_name(hdl, fromsnap, ZFS_TYPE_SNAPSHOT,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* the to snap will be a just-in-time snap of the head */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "Unable to determine which snapshots to compare"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley fdslen = atptrf ? atptrf - fromsnap : strlen(fromsnap);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley tdslen = atptrt ? atptrt - tosnap : strlen(tosnap);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley fsnlen = strlen(fromsnap) - fdslen; /* includes @ sign */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley tsnlen = strlen(tosnap) - tdslen; /* includes @ sign */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (fsnlen <= 1 || tsnlen == 1 || (fdslen == 0 && tdslen == 0) ||
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley ((tdslen != fdslen || strncmp(fromsnap, tosnap, fdslen) != 0))) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * not the same dataset name, might be okay if
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * tosnap is a clone of a fromsnap descendant.
8430278980a48338e04c7dd52b495b7f1551367aAlexander Eremin if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin,
8430278980a48338e04c7dd52b495b7f1551367aAlexander Eremin sizeof (origin), &src, NULL, 0, B_FALSE) != 0) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "Not an earlier snapshot from the same fs"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_error(hdl, EZFS_INVALIDNAME, di->errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strncpy(di->ds, fdslen ? fromsnap : tosnap, dslen);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley di->fromsnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrf);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley di->tosnap = zfs_asprintf(hdl, "%s%s", di->ds, atptrt);
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyget_mountpoint(differ_info_t *di, char *dsnm, char **mntpt)
99d5e173470cf967aa87653364ed614299e7b511Tim Haley mounted = is_mounted(di->zhp->zfs_hdl, dsnm, mntpt);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "Cannot diff an unmounted snapshot"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_error(di->zhp->zfs_hdl, EZFS_BADTYPE, di->errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* Avoid a double slash at the beginning of root-mounted datasets */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley * first get the mountpoint for the parent dataset
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley di->tomnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", di->dsmnt,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley di->frommnt = zfs_asprintf(di->zhp->zfs_hdl, "%s%s%s", frommntpt,
99d5e173470cf967aa87653364ed614299e7b511Tim Haleysetup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (get_snapshot_names(di, fromsnap, tosnap) != 0)
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haleyzfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
99d5e173470cf967aa87653364ed614299e7b511Tim Haley if (setup_differ_info(zhp, fromsnap, tosnap, &di)) {
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (-1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley /* do the ioctl() */
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strlcpy(zc.zc_value, di.fromsnap, strlen(di.fromsnap) + 1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley (void) strlcpy(zc.zc_name, di.tosnap, strlen(di.tosnap) + 1);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley iocerr = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DIFF, &zc);
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "\n The sys_mount privilege or diff delegated "
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "permission is needed\n to execute the "
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "diff ioctl"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley "\n Not an earlier snapshot from the same fs"));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_error(zhp->zfs_hdl, EZFS_DIFF, di.errbuf));
99d5e173470cf967aa87653364ed614299e7b511Tim Haley return (zfs_error(zhp->zfs_hdl, EZFS_DIFFDATA, errbuf));