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.
2499d3b4eloli * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
2599d5e17Tim Haley */
2699d5e17Tim Haley
2799d5e17Tim Haley#include <sys/dmu.h>
2899d5e17Tim Haley#include <sys/dmu_impl.h>
2999d5e17Tim Haley#include <sys/dmu_tx.h>
3099d5e17Tim Haley#include <sys/dbuf.h>
3199d5e17Tim Haley#include <sys/dnode.h>
3299d5e17Tim Haley#include <sys/zfs_context.h>
3399d5e17Tim Haley#include <sys/dmu_objset.h>
3499d5e17Tim Haley#include <sys/dmu_traverse.h>
3599d5e17Tim Haley#include <sys/dsl_dataset.h>
3699d5e17Tim Haley#include <sys/dsl_dir.h>
3799d5e17Tim Haley#include <sys/dsl_pool.h>
3899d5e17Tim Haley#include <sys/dsl_synctask.h>
3999d5e17Tim Haley#include <sys/zfs_ioctl.h>
4099d5e17Tim Haley#include <sys/zap.h>
4199d5e17Tim Haley#include <sys/zio_checksum.h>
4299d5e17Tim Haley#include <sys/zfs_znode.h>
4399d5e17Tim Haley
4499d5e17Tim Haleystruct diffarg {
4599d5e17Tim Haley	struct vnode *da_vp;		/* file to which we are reporting */
4699d5e17Tim Haley	offset_t *da_offp;
4799d5e17Tim Haley	int da_err;			/* error that stopped diff search */
4899d5e17Tim Haley	dmu_diff_record_t da_ddr;
4999d5e17Tim Haley};
5099d5e17Tim Haley
5199d5e17Tim Haleystatic int
5299d5e17Tim Haleywrite_record(struct diffarg *da)
5399d5e17Tim Haley{
5499d5e17Tim Haley	ssize_t resid; /* have to get resid to get detailed errno */
5599d5e17Tim Haley
5699d5e17Tim Haley	if (da->da_ddr.ddr_type == DDR_NONE) {
5799d5e17Tim Haley		da->da_err = 0;
5899d5e17Tim Haley		return (0);
5999d5e17Tim Haley	}
6099d5e17Tim Haley
6199d5e17Tim Haley	da->da_err = vn_rdwr(UIO_WRITE, da->da_vp, (caddr_t)&da->da_ddr,
6299d5e17Tim Haley	    sizeof (da->da_ddr), 0, UIO_SYSSPACE, FAPPEND,
6399d5e17Tim Haley	    RLIM64_INFINITY, CRED(), &resid);
6499d5e17Tim Haley	*da->da_offp += sizeof (da->da_ddr);
6599d5e17Tim Haley	return (da->da_err);
6699d5e17Tim Haley}
6799d5e17Tim Haley
6899d5e17Tim Haleystatic int
6999d5e17Tim Haleyreport_free_dnode_range(struct diffarg *da, uint64_t first, uint64_t last)
7099d5e17Tim Haley{
7199d5e17Tim Haley	ASSERT(first <= last);
7299d5e17Tim Haley	if (da->da_ddr.ddr_type != DDR_FREE ||
7399d5e17Tim Haley	    first != da->da_ddr.ddr_last + 1) {
7499d5e17Tim Haley		if (write_record(da) != 0)
7599d5e17Tim Haley			return (da->da_err);
7699d5e17Tim Haley		da->da_ddr.ddr_type = DDR_FREE;
7799d5e17Tim Haley		da->da_ddr.ddr_first = first;
7899d5e17Tim Haley		da->da_ddr.ddr_last = last;
7999d5e17Tim Haley		return (0);
8099d5e17Tim Haley	}
8199d5e17Tim Haley	da->da_ddr.ddr_last = last;
8299d5e17Tim Haley	return (0);
8399d5e17Tim Haley}
8499d5e17Tim Haley
8599d5e17Tim Haleystatic int
8699d5e17Tim Haleyreport_dnode(struct diffarg *da, uint64_t object, dnode_phys_t *dnp)
8799d5e17Tim Haley{
8899d5e17Tim Haley	ASSERT(dnp != NULL);
8999d5e17Tim Haley	if (dnp->dn_type == DMU_OT_NONE)
9099d5e17Tim Haley		return (report_free_dnode_range(da, object, object));
9199d5e17Tim Haley
9299d5e17Tim Haley	if (da->da_ddr.ddr_type != DDR_INUSE ||
9399d5e17Tim Haley	    object != da->da_ddr.ddr_last + 1) {
9499d5e17Tim Haley		if (write_record(da) != 0)
9599d5e17Tim Haley			return (da->da_err);
9699d5e17Tim Haley		da->da_ddr.ddr_type = DDR_INUSE;
9799d5e17Tim Haley		da->da_ddr.ddr_first = da->da_ddr.ddr_last = object;
9899d5e17Tim Haley		return (0);
9999d5e17Tim Haley	}
10099d5e17Tim Haley	da->da_ddr.ddr_last = object;
10199d5e17Tim Haley	return (0);
10299d5e17Tim Haley}
10399d5e17Tim Haley
10499d5e17Tim Haley#define	DBP_SPAN(dnp, level)				  \
10599d5e17Tim Haley	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
10699d5e17Tim Haley	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
10799d5e17Tim Haley
10899d5e17Tim Haley/* ARGSUSED */
10999d5e17Tim Haleystatic int
1101b912ecGeorge Wilsondiff_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1117802d7bMatthew Ahrens    const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
11299d5e17Tim Haley{
11399d5e17Tim Haley	struct diffarg *da = arg;
11499d5e17Tim Haley	int err = 0;
11599d5e17Tim Haley
11699d5e17Tim Haley	if (issig(JUSTLOOKING) && issig(FORREAL))
117be6fd75Matthew Ahrens		return (SET_ERROR(EINTR));
11899d5e17Tim Haley
119a2cdcddPaul Dagnelie	if (bp == NULL || zb->zb_object != DMU_META_DNODE_OBJECT)
12099d5e17Tim Haley		return (0);
12199d5e17Tim Haley
12243466aaMax Grossman	if (BP_IS_HOLE(bp)) {
12399d5e17Tim Haley		uint64_t span = DBP_SPAN(dnp, zb->zb_level);
12499d5e17Tim Haley		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
12599d5e17Tim Haley
12699d5e17Tim Haley		err = report_free_dnode_range(da, dnobj,
12799d5e17Tim Haley		    dnobj + (span >> DNODE_SHIFT) - 1);
12899d5e17Tim Haley		if (err)
12999d5e17Tim Haley			return (err);
13099d5e17Tim Haley	} else if (zb->zb_level == 0) {
13199d5e17Tim Haley		dnode_phys_t *blk;
13299d5e17Tim Haley		arc_buf_t *abuf;
1337adb730George Wilson		arc_flags_t aflags = ARC_FLAG_WAIT;
13499d3b4eloli		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
135eb63303Tom Caputi		int zio_flags = ZIO_FLAG_CANFAIL;
13699d5e17Tim Haley		int i;
13799d5e17Tim Haley
138eb63303Tom Caputi		if (BP_IS_PROTECTED(bp))
139eb63303Tom Caputi			zio_flags |= ZIO_FLAG_RAW;
140eb63303Tom Caputi
1411b912ecGeorge Wilson		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
142eb63303Tom Caputi		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &aflags, zb) != 0)
143be6fd75Matthew Ahrens			return (SET_ERROR(EIO));
14499d5e17Tim Haley
14599d5e17Tim Haley		blk = abuf->b_data;
14699d3b4eloli		for (i = 0; i < epb; i += blk[i].dn_extra_slots + 1) {
14799d5e17Tim Haley			uint64_t dnobj = (zb->zb_blkid <<
14899d5e17Tim Haley			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
14999d5e17Tim Haley			err = report_dnode(da, dnobj, blk+i);
15099d5e17Tim Haley			if (err)
15199d5e17Tim Haley				break;
15299d5e17Tim Haley		}
153dcbf3bdGeorge Wilson		arc_buf_destroy(abuf, &abuf);
15499d5e17Tim Haley		if (err)
15599d5e17Tim Haley			return (err);
15699d5e17Tim Haley		/* Don't care about the data blocks */
15799d5e17Tim Haley		return (TRAVERSE_VISIT_NO_CHILDREN);
15899d5e17Tim Haley	}
15999d5e17Tim Haley	return (0);
16099d5e17Tim Haley}
16199d5e17Tim Haley
16299d5e17Tim Haleyint
1633b2aab1Matthew Ahrensdmu_diff(const char *tosnap_name, const char *fromsnap_name,
1643b2aab1Matthew Ahrens    struct vnode *vp, offset_t *offp)
16599d5e17Tim Haley{
16699d5e17Tim Haley	struct diffarg da;
1673b2aab1Matthew Ahrens	dsl_dataset_t *fromsnap;
1683b2aab1Matthew Ahrens	dsl_dataset_t *tosnap;
1693b2aab1Matthew Ahrens	dsl_pool_t *dp;
1703b2aab1Matthew Ahrens	int error;
1713b2aab1Matthew Ahrens	uint64_t fromtxg;
1723b2aab1Matthew Ahrens
1733b2aab1Matthew Ahrens	if (strchr(tosnap_name, '@') == NULL ||
1743b2aab1Matthew Ahrens	    strchr(fromsnap_name, '@') == NULL)
175be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
17699d5e17Tim Haley
1773b2aab1Matthew Ahrens	error = dsl_pool_hold(tosnap_name, FTAG, &dp);
1783b2aab1Matthew Ahrens	if (error != 0)
1793b2aab1Matthew Ahrens		return (error);
18099d5e17Tim Haley
1813b2aab1Matthew Ahrens	error = dsl_dataset_hold(dp, tosnap_name, FTAG, &tosnap);
1823b2aab1Matthew Ahrens	if (error != 0) {
1833b2aab1Matthew Ahrens		dsl_pool_rele(dp, FTAG);
1843b2aab1Matthew Ahrens		return (error);
1853b2aab1Matthew Ahrens	}
18699d5e17Tim Haley
1873b2aab1Matthew Ahrens	error = dsl_dataset_hold(dp, fromsnap_name, FTAG, &fromsnap);
1883b2aab1Matthew Ahrens	if (error != 0) {
1893b2aab1Matthew Ahrens		dsl_dataset_rele(tosnap, FTAG);
1903b2aab1Matthew Ahrens		dsl_pool_rele(dp, FTAG);
1913b2aab1Matthew Ahrens		return (error);
1923b2aab1Matthew Ahrens	}
19399d5e17Tim Haley
19478f1710Matthew Ahrens	if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
1953b2aab1Matthew Ahrens		dsl_dataset_rele(fromsnap, FTAG);
1963b2aab1Matthew Ahrens		dsl_dataset_rele(tosnap, FTAG);
1973b2aab1Matthew Ahrens		dsl_pool_rele(dp, FTAG);
198be6fd75Matthew Ahrens		return (SET_ERROR(EXDEV));
19999d5e17Tim Haley	}
20099d5e17Tim Haley
201c137962Justin T. Gibbs	fromtxg = dsl_dataset_phys(fromsnap)->ds_creation_txg;
2023b2aab1Matthew Ahrens	dsl_dataset_rele(fromsnap, FTAG);
2033b2aab1Matthew Ahrens
2043b2aab1Matthew Ahrens	dsl_dataset_long_hold(tosnap, FTAG);
2053b2aab1Matthew Ahrens	dsl_pool_rele(dp, FTAG);
20699d5e17Tim Haley
20799d5e17Tim Haley	da.da_vp = vp;
20899d5e17Tim Haley	da.da_offp = offp;
20999d5e17Tim Haley	da.da_ddr.ddr_type = DDR_NONE;
21099d5e17Tim Haley	da.da_ddr.ddr_first = da.da_ddr.ddr_last = 0;
21199d5e17Tim Haley	da.da_err = 0;
21299d5e17Tim Haley
213eb63303Tom Caputi	/*
214eb63303Tom Caputi	 * Since zfs diff only looks at dnodes which are stored in plaintext
215eb63303Tom Caputi	 * (other than bonus buffers), we don't technically need to decrypt
216eb63303Tom Caputi	 * the dataset to perform this operation. However, the command line
217eb63303Tom Caputi	 * utility will still fail if the keys are not loaded because the
218eb63303Tom Caputi	 * dataset isn't mounted and because it will fail when it attempts to
219eb63303Tom Caputi	 * call the ZFS_IOC_OBJ_TO_STATS ioctl.
220eb63303Tom Caputi	 */
2213b2aab1Matthew Ahrens	error = traverse_dataset(tosnap, fromtxg,
222eb63303Tom Caputi	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_NO_DECRYPT,
223eb63303Tom Caputi	    diff_cb, &da);
22499d5e17Tim Haley
2253b2aab1Matthew Ahrens	if (error != 0) {
2263b2aab1Matthew Ahrens		da.da_err = error;
22799d5e17Tim Haley	} else {
22899d5e17Tim Haley		/* we set the da.da_err we return as side-effect */
22999d5e17Tim Haley		(void) write_record(&da);
23099d5e17Tim Haley	}
23199d5e17Tim Haley
2323b2aab1Matthew Ahrens	dsl_dataset_long_rele(tosnap, FTAG);
2333b2aab1Matthew Ahrens	dsl_dataset_rele(tosnap, FTAG);
2343b2aab1Matthew Ahrens
23599d5e17Tim Haley	return (da.da_err);
23699d5e17Tim Haley}
237