199d5e17Tim Haley/*
299d5e17Tim Haley * CDDL HEADER START
399d5e17Tim Haley *
499d5e17Tim Haley * The contents of this file are subject to the terms of the
599d5e17Tim Haley * Common Development and Distribution License (the "License").
699d5e17Tim Haley * You may not use this file except in compliance with the License.
799d5e17Tim Haley *
899d5e17Tim Haley * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
999d5e17Tim Haley * or http://www.opensolaris.org/os/licensing.
1099d5e17Tim Haley * See the License for the specific language governing permissions
1199d5e17Tim Haley * and limitations under the License.
1299d5e17Tim Haley *
1399d5e17Tim Haley * When distributing Covered Code, include this CDDL HEADER in each
1499d5e17Tim Haley * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1599d5e17Tim Haley * If applicable, add the following below this CDDL HEADER, with the
1699d5e17Tim Haley * fields enclosed by brackets "[]" replaced with your own identifying
1799d5e17Tim Haley * information: Portions Copyright [yyyy] [name of copyright owner]
1899d5e17Tim Haley *
1999d5e17Tim Haley * CDDL HEADER END
2099d5e17Tim Haley */
2199d5e17Tim Haley/*
2299d5e17Tim Haley * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23dcbf3bdGeorge Wilson * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
2499d5e17Tim Haley */
2599d5e17Tim Haley
2699d5e17Tim Haley#include <sys/dmu.h>
2799d5e17Tim Haley#include <sys/dmu_impl.h>
2899d5e17Tim Haley#include <sys/dmu_tx.h>
2999d5e17Tim Haley#include <sys/dbuf.h>
3099d5e17Tim Haley#include <sys/dnode.h>
3199d5e17Tim Haley#include <sys/zfs_context.h>
3299d5e17Tim Haley#include <sys/dmu_objset.h>
3399d5e17Tim Haley#include <sys/dmu_traverse.h>
3499d5e17Tim Haley#include <sys/dsl_dataset.h>
3599d5e17Tim Haley#include <sys/dsl_dir.h>
3699d5e17Tim Haley#include <sys/dsl_pool.h>
3799d5e17Tim Haley#include <sys/dsl_synctask.h>
3899d5e17Tim Haley#include <sys/zfs_ioctl.h>
3999d5e17Tim Haley#include <sys/zap.h>
4099d5e17Tim Haley#include <sys/zio_checksum.h>
4199d5e17Tim Haley#include <sys/zfs_znode.h>
4299d5e17Tim Haley
4399d5e17Tim Haleystruct diffarg {
4499d5e17Tim Haley	struct vnode *da_vp;		/* file to which we are reporting */
4599d5e17Tim Haley	offset_t *da_offp;
4699d5e17Tim Haley	int da_err;			/* error that stopped diff search */
4799d5e17Tim Haley	dmu_diff_record_t da_ddr;
4899d5e17Tim Haley};
4999d5e17Tim Haley
5099d5e17Tim Haleystatic int
5199d5e17Tim Haleywrite_record(struct diffarg *da)
5299d5e17Tim Haley{
5399d5e17Tim Haley	ssize_t resid; /* have to get resid to get detailed errno */
5499d5e17Tim Haley
5599d5e17Tim Haley	if (da->da_ddr.ddr_type == DDR_NONE) {
5699d5e17Tim Haley		da->da_err = 0;
5799d5e17Tim Haley		return (0);
5899d5e17Tim Haley	}
5999d5e17Tim Haley
6099d5e17Tim Haley	da->da_err = vn_rdwr(UIO_WRITE, da->da_vp, (caddr_t)&da->da_ddr,
6199d5e17Tim Haley	    sizeof (da->da_ddr), 0, UIO_SYSSPACE, FAPPEND,
6299d5e17Tim Haley	    RLIM64_INFINITY, CRED(), &resid);
6399d5e17Tim Haley	*da->da_offp += sizeof (da->da_ddr);
6499d5e17Tim Haley	return (da->da_err);
6599d5e17Tim Haley}
6699d5e17Tim Haley
6799d5e17Tim Haleystatic int
6899d5e17Tim Haleyreport_free_dnode_range(struct diffarg *da, uint64_t first, uint64_t last)
6999d5e17Tim Haley{
7099d5e17Tim Haley	ASSERT(first <= last);
7199d5e17Tim Haley	if (da->da_ddr.ddr_type != DDR_FREE ||
7299d5e17Tim Haley	    first != da->da_ddr.ddr_last + 1) {
7399d5e17Tim Haley		if (write_record(da) != 0)
7499d5e17Tim Haley			return (da->da_err);
7599d5e17Tim Haley		da->da_ddr.ddr_type = DDR_FREE;
7699d5e17Tim Haley		da->da_ddr.ddr_first = first;
7799d5e17Tim Haley		da->da_ddr.ddr_last = last;
7899d5e17Tim Haley		return (0);
7999d5e17Tim Haley	}
8099d5e17Tim Haley	da->da_ddr.ddr_last = last;
8199d5e17Tim Haley	return (0);
8299d5e17Tim Haley}
8399d5e17Tim Haley
8499d5e17Tim Haleystatic int
8599d5e17Tim Haleyreport_dnode(struct diffarg *da, uint64_t object, dnode_phys_t *dnp)
8699d5e17Tim Haley{
8799d5e17Tim Haley	ASSERT(dnp != NULL);
8899d5e17Tim Haley	if (dnp->dn_type == DMU_OT_NONE)
8999d5e17Tim Haley		return (report_free_dnode_range(da, object, object));
9099d5e17Tim Haley
9199d5e17Tim Haley	if (da->da_ddr.ddr_type != DDR_INUSE ||
9299d5e17Tim Haley	    object != da->da_ddr.ddr_last + 1) {
9399d5e17Tim Haley		if (write_record(da) != 0)
9499d5e17Tim Haley			return (da->da_err);
9599d5e17Tim Haley		da->da_ddr.ddr_type = DDR_INUSE;
9699d5e17Tim Haley		da->da_ddr.ddr_first = da->da_ddr.ddr_last = object;
9799d5e17Tim Haley		return (0);
9899d5e17Tim Haley	}
9999d5e17Tim Haley	da->da_ddr.ddr_last = object;
10099d5e17Tim Haley	return (0);
10199d5e17Tim Haley}
10299d5e17Tim Haley
10399d5e17Tim Haley#define	DBP_SPAN(dnp, level)				  \
10499d5e17Tim Haley	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
10599d5e17Tim Haley	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
10699d5e17Tim Haley
10799d5e17Tim Haley/* ARGSUSED */
10899d5e17Tim Haleystatic int
1091b912ecGeorge Wilsondiff_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1107802d7bMatthew Ahrens    const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
11199d5e17Tim Haley{
11299d5e17Tim Haley	struct diffarg *da = arg;
11399d5e17Tim Haley	int err = 0;
11499d5e17Tim Haley
11599d5e17Tim Haley	if (issig(JUSTLOOKING) && issig(FORREAL))
116be6fd75Matthew Ahrens		return (SET_ERROR(EINTR));
11799d5e17Tim Haley
118a2cdcddPaul Dagnelie	if (bp == NULL || zb->zb_object != DMU_META_DNODE_OBJECT)
11999d5e17Tim Haley		return (0);
12099d5e17Tim Haley
12143466aaMax Grossman	if (BP_IS_HOLE(bp)) {
12299d5e17Tim Haley		uint64_t span = DBP_SPAN(dnp, zb->zb_level);
12399d5e17Tim Haley		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
12499d5e17Tim Haley
12599d5e17Tim Haley		err = report_free_dnode_range(da, dnobj,
12699d5e17Tim Haley		    dnobj + (span >> DNODE_SHIFT) - 1);
12799d5e17Tim Haley		if (err)
12899d5e17Tim Haley			return (err);
12999d5e17Tim Haley	} else if (zb->zb_level == 0) {
13099d5e17Tim Haley		dnode_phys_t *blk;
13199d5e17Tim Haley		arc_buf_t *abuf;
1327adb730George Wilson		arc_flags_t aflags = ARC_FLAG_WAIT;
13399d5e17Tim Haley		int blksz = BP_GET_LSIZE(bp);
134eb63303Tom Caputi		int zio_flags = ZIO_FLAG_CANFAIL;
13599d5e17Tim Haley		int i;
13699d5e17Tim Haley
137eb63303Tom Caputi		if (BP_IS_PROTECTED(bp))
138eb63303Tom Caputi			zio_flags |= ZIO_FLAG_RAW;
139eb63303Tom Caputi
1401b912ecGeorge Wilson		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
141eb63303Tom Caputi		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &aflags, zb) != 0)
142be6fd75Matthew Ahrens			return (SET_ERROR(EIO));
14399d5e17Tim Haley
14499d5e17Tim Haley		blk = abuf->b_data;
14599d5e17Tim Haley		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
14699d5e17Tim Haley			uint64_t dnobj = (zb->zb_blkid <<
14799d5e17Tim Haley			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
14899d5e17Tim Haley			err = report_dnode(da, dnobj, blk+i);
14999d5e17Tim Haley			if (err)
15099d5e17Tim Haley				break;
15199d5e17Tim Haley		}
152dcbf3bdGeorge Wilson		arc_buf_destroy(abuf, &abuf);
15399d5e17Tim Haley		if (err)
15499d5e17Tim Haley			return (err);
15599d5e17Tim Haley		/* Don't care about the data blocks */
15699d5e17Tim Haley		return (TRAVERSE_VISIT_NO_CHILDREN);
15799d5e17Tim Haley	}
15899d5e17Tim Haley	return (0);
15999d5e17Tim Haley}
16099d5e17Tim Haley
16199d5e17Tim Haleyint
1623b2aab1Matthew Ahrensdmu_diff(const char *tosnap_name, const char *fromsnap_name,
1633b2aab1Matthew Ahrens    struct vnode *vp, offset_t *offp)
16499d5e17Tim Haley{
16599d5e17Tim Haley	struct diffarg da;
1663b2aab1Matthew Ahrens	dsl_dataset_t *fromsnap;
1673b2aab1Matthew Ahrens	dsl_dataset_t *tosnap;
1683b2aab1Matthew Ahrens	dsl_pool_t *dp;
1693b2aab1Matthew Ahrens	int error;
1703b2aab1Matthew Ahrens	uint64_t fromtxg;
1713b2aab1Matthew Ahrens
1723b2aab1Matthew Ahrens	if (strchr(tosnap_name, '@') == NULL ||
1733b2aab1Matthew Ahrens	    strchr(fromsnap_name, '@') == NULL)
174be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
17599d5e17Tim Haley
1763b2aab1Matthew Ahrens	error = dsl_pool_hold(tosnap_name, FTAG, &dp);
1773b2aab1Matthew Ahrens	if (error != 0)
1783b2aab1Matthew Ahrens		return (error);
17999d5e17Tim Haley
1803b2aab1Matthew Ahrens	error = dsl_dataset_hold(dp, tosnap_name, FTAG, &tosnap);
1813b2aab1Matthew Ahrens	if (error != 0) {
1823b2aab1Matthew Ahrens		dsl_pool_rele(dp, FTAG);
1833b2aab1Matthew Ahrens		return (error);
1843b2aab1Matthew Ahrens	}
18599d5e17Tim Haley
1863b2aab1Matthew Ahrens	error = dsl_dataset_hold(dp, fromsnap_name, FTAG, &fromsnap);
1873b2aab1Matthew Ahrens	if (error != 0) {
1883b2aab1Matthew Ahrens		dsl_dataset_rele(tosnap, FTAG);
1893b2aab1Matthew Ahrens		dsl_pool_rele(dp, FTAG);
1903b2aab1Matthew Ahrens		return (error);
1913b2aab1Matthew Ahrens	}
19299d5e17Tim Haley
19378f1710Matthew Ahrens	if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
1943b2aab1Matthew Ahrens		dsl_dataset_rele(fromsnap, FTAG);
1953b2aab1Matthew Ahrens		dsl_dataset_rele(tosnap, FTAG);
1963b2aab1Matthew Ahrens		dsl_pool_rele(dp, FTAG);
197be6fd75Matthew Ahrens		return (SET_ERROR(EXDEV));
19899d5e17Tim Haley	}
19999d5e17Tim Haley
200c137962Justin T. Gibbs	fromtxg = dsl_dataset_phys(fromsnap)->ds_creation_txg;
2013b2aab1Matthew Ahrens	dsl_dataset_rele(fromsnap, FTAG);
2023b2aab1Matthew Ahrens
2033b2aab1Matthew Ahrens	dsl_dataset_long_hold(tosnap, FTAG);
2043b2aab1Matthew Ahrens	dsl_pool_rele(dp, FTAG);
20599d5e17Tim Haley
20699d5e17Tim Haley	da.da_vp = vp;
20799d5e17Tim Haley	da.da_offp = offp;
20899d5e17Tim Haley	da.da_ddr.ddr_type = DDR_NONE;
20999d5e17Tim Haley	da.da_ddr.ddr_first = da.da_ddr.ddr_last = 0;
21099d5e17Tim Haley	da.da_err = 0;
21199d5e17Tim Haley
212eb63303Tom Caputi	/*
213eb63303Tom Caputi	 * Since zfs diff only looks at dnodes which are stored in plaintext
214eb63303Tom Caputi	 * (other than bonus buffers), we don't technically need to decrypt
215eb63303Tom Caputi	 * the dataset to perform this operation. However, the command line
216eb63303Tom Caputi	 * utility will still fail if the keys are not loaded because the
217eb63303Tom Caputi	 * dataset isn't mounted and because it will fail when it attempts to
218eb63303Tom Caputi	 * call the ZFS_IOC_OBJ_TO_STATS ioctl.
219eb63303Tom Caputi	 */
2203b2aab1Matthew Ahrens	error = traverse_dataset(tosnap, fromtxg,
221eb63303Tom Caputi	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_NO_DECRYPT,
222eb63303Tom Caputi	    diff_cb, &da);
22399d5e17Tim Haley
2243b2aab1Matthew Ahrens	if (error != 0) {
2253b2aab1Matthew Ahrens		da.da_err = error;
22699d5e17Tim Haley	} else {
22799d5e17Tim Haley		/* we set the da.da_err we return as side-effect */
22899d5e17Tim Haley		(void) write_record(&da);
22999d5e17Tim Haley	}
23099d5e17Tim Haley
2313b2aab1Matthew Ahrens	dsl_dataset_long_rele(tosnap, FTAG);
2323b2aab1Matthew Ahrens	dsl_dataset_rele(tosnap, FTAG);
2333b2aab1Matthew Ahrens
23499d5e17Tim Haley	return (da.da_err);
23599d5e17Tim Haley}
236