xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_diff.c (revision 99d3b4e2)
199d5e173STim Haley /*
299d5e173STim Haley  * CDDL HEADER START
399d5e173STim Haley  *
499d5e173STim Haley  * The contents of this file are subject to the terms of the
599d5e173STim Haley  * Common Development and Distribution License (the "License").
699d5e173STim Haley  * You may not use this file except in compliance with the License.
799d5e173STim Haley  *
899d5e173STim Haley  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
999d5e173STim Haley  * or http://www.opensolaris.org/os/licensing.
1099d5e173STim Haley  * See the License for the specific language governing permissions
1199d5e173STim Haley  * and limitations under the License.
1299d5e173STim Haley  *
1399d5e173STim Haley  * When distributing Covered Code, include this CDDL HEADER in each
1499d5e173STim Haley  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1599d5e173STim Haley  * If applicable, add the following below this CDDL HEADER, with the
1699d5e173STim Haley  * fields enclosed by brackets "[]" replaced with your own identifying
1799d5e173STim Haley  * information: Portions Copyright [yyyy] [name of copyright owner]
1899d5e173STim Haley  *
1999d5e173STim Haley  * CDDL HEADER END
2099d5e173STim Haley  */
2199d5e173STim Haley /*
2299d5e173STim Haley  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23dcbf3bd6SGeorge Wilson  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24*99d3b4e2Sloli10K  * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
2599d5e173STim Haley  */
2699d5e173STim Haley 
2799d5e173STim Haley #include <sys/dmu.h>
2899d5e173STim Haley #include <sys/dmu_impl.h>
2999d5e173STim Haley #include <sys/dmu_tx.h>
3099d5e173STim Haley #include <sys/dbuf.h>
3199d5e173STim Haley #include <sys/dnode.h>
3299d5e173STim Haley #include <sys/zfs_context.h>
3399d5e173STim Haley #include <sys/dmu_objset.h>
3499d5e173STim Haley #include <sys/dmu_traverse.h>
3599d5e173STim Haley #include <sys/dsl_dataset.h>
3699d5e173STim Haley #include <sys/dsl_dir.h>
3799d5e173STim Haley #include <sys/dsl_pool.h>
3899d5e173STim Haley #include <sys/dsl_synctask.h>
3999d5e173STim Haley #include <sys/zfs_ioctl.h>
4099d5e173STim Haley #include <sys/zap.h>
4199d5e173STim Haley #include <sys/zio_checksum.h>
4299d5e173STim Haley #include <sys/zfs_znode.h>
4399d5e173STim Haley 
4499d5e173STim Haley struct diffarg {
4599d5e173STim Haley 	struct vnode *da_vp;		/* file to which we are reporting */
4699d5e173STim Haley 	offset_t *da_offp;
4799d5e173STim Haley 	int da_err;			/* error that stopped diff search */
4899d5e173STim Haley 	dmu_diff_record_t da_ddr;
4999d5e173STim Haley };
5099d5e173STim Haley 
5199d5e173STim Haley static int
write_record(struct diffarg * da)5299d5e173STim Haley write_record(struct diffarg *da)
5399d5e173STim Haley {
5499d5e173STim Haley 	ssize_t resid; /* have to get resid to get detailed errno */
5599d5e173STim Haley 
5699d5e173STim Haley 	if (da->da_ddr.ddr_type == DDR_NONE) {
5799d5e173STim Haley 		da->da_err = 0;
5899d5e173STim Haley 		return (0);
5999d5e173STim Haley 	}
6099d5e173STim Haley 
6199d5e173STim Haley 	da->da_err = vn_rdwr(UIO_WRITE, da->da_vp, (caddr_t)&da->da_ddr,
6299d5e173STim Haley 	    sizeof (da->da_ddr), 0, UIO_SYSSPACE, FAPPEND,
6399d5e173STim Haley 	    RLIM64_INFINITY, CRED(), &resid);
6499d5e173STim Haley 	*da->da_offp += sizeof (da->da_ddr);
6599d5e173STim Haley 	return (da->da_err);
6699d5e173STim Haley }
6799d5e173STim Haley 
6899d5e173STim Haley static int
report_free_dnode_range(struct diffarg * da,uint64_t first,uint64_t last)6999d5e173STim Haley report_free_dnode_range(struct diffarg *da, uint64_t first, uint64_t last)
7099d5e173STim Haley {
7199d5e173STim Haley 	ASSERT(first <= last);
7299d5e173STim Haley 	if (da->da_ddr.ddr_type != DDR_FREE ||
7399d5e173STim Haley 	    first != da->da_ddr.ddr_last + 1) {
7499d5e173STim Haley 		if (write_record(da) != 0)
7599d5e173STim Haley 			return (da->da_err);
7699d5e173STim Haley 		da->da_ddr.ddr_type = DDR_FREE;
7799d5e173STim Haley 		da->da_ddr.ddr_first = first;
7899d5e173STim Haley 		da->da_ddr.ddr_last = last;
7999d5e173STim Haley 		return (0);
8099d5e173STim Haley 	}
8199d5e173STim Haley 	da->da_ddr.ddr_last = last;
8299d5e173STim Haley 	return (0);
8399d5e173STim Haley }
8499d5e173STim Haley 
8599d5e173STim Haley static int
report_dnode(struct diffarg * da,uint64_t object,dnode_phys_t * dnp)8699d5e173STim Haley report_dnode(struct diffarg *da, uint64_t object, dnode_phys_t *dnp)
8799d5e173STim Haley {
8899d5e173STim Haley 	ASSERT(dnp != NULL);
8999d5e173STim Haley 	if (dnp->dn_type == DMU_OT_NONE)
9099d5e173STim Haley 		return (report_free_dnode_range(da, object, object));
9199d5e173STim Haley 
9299d5e173STim Haley 	if (da->da_ddr.ddr_type != DDR_INUSE ||
9399d5e173STim Haley 	    object != da->da_ddr.ddr_last + 1) {
9499d5e173STim Haley 		if (write_record(da) != 0)
9599d5e173STim Haley 			return (da->da_err);
9699d5e173STim Haley 		da->da_ddr.ddr_type = DDR_INUSE;
9799d5e173STim Haley 		da->da_ddr.ddr_first = da->da_ddr.ddr_last = object;
9899d5e173STim Haley 		return (0);
9999d5e173STim Haley 	}
10099d5e173STim Haley 	da->da_ddr.ddr_last = object;
10199d5e173STim Haley 	return (0);
10299d5e173STim Haley }
10399d5e173STim Haley 
10499d5e173STim Haley #define	DBP_SPAN(dnp, level)				  \
10599d5e173STim Haley 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
10699d5e173STim Haley 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
10799d5e173STim Haley 
10899d5e173STim Haley /* ARGSUSED */
10999d5e173STim Haley static int
diff_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)1101b912ec7SGeorge Wilson diff_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1117802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
11299d5e173STim Haley {
11399d5e173STim Haley 	struct diffarg *da = arg;
11499d5e173STim Haley 	int err = 0;
11599d5e173STim Haley 
11699d5e173STim Haley 	if (issig(JUSTLOOKING) && issig(FORREAL))
117be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
11899d5e173STim Haley 
119a2cdcdd2SPaul Dagnelie 	if (bp == NULL || zb->zb_object != DMU_META_DNODE_OBJECT)
12099d5e173STim Haley 		return (0);
12199d5e173STim Haley 
12243466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
12399d5e173STim Haley 		uint64_t span = DBP_SPAN(dnp, zb->zb_level);
12499d5e173STim Haley 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
12599d5e173STim Haley 
12699d5e173STim Haley 		err = report_free_dnode_range(da, dnobj,
12799d5e173STim Haley 		    dnobj + (span >> DNODE_SHIFT) - 1);
12899d5e173STim Haley 		if (err)
12999d5e173STim Haley 			return (err);
13099d5e173STim Haley 	} else if (zb->zb_level == 0) {
13199d5e173STim Haley 		dnode_phys_t *blk;
13299d5e173STim Haley 		arc_buf_t *abuf;
1337adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
134*99d3b4e2Sloli10K 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
135eb633035STom Caputi 		int zio_flags = ZIO_FLAG_CANFAIL;
13699d5e173STim Haley 		int i;
13799d5e173STim Haley 
138eb633035STom Caputi 		if (BP_IS_PROTECTED(bp))
139eb633035STom Caputi 			zio_flags |= ZIO_FLAG_RAW;
140eb633035STom Caputi 
1411b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
142eb633035STom Caputi 		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &aflags, zb) != 0)
143be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
14499d5e173STim Haley 
14599d5e173STim Haley 		blk = abuf->b_data;
146*99d3b4e2Sloli10K 		for (i = 0; i < epb; i += blk[i].dn_extra_slots + 1) {
14799d5e173STim Haley 			uint64_t dnobj = (zb->zb_blkid <<
14899d5e173STim Haley 			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
14999d5e173STim Haley 			err = report_dnode(da, dnobj, blk+i);
15099d5e173STim Haley 			if (err)
15199d5e173STim Haley 				break;
15299d5e173STim Haley 		}
153dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
15499d5e173STim Haley 		if (err)
15599d5e173STim Haley 			return (err);
15699d5e173STim Haley 		/* Don't care about the data blocks */
15799d5e173STim Haley 		return (TRAVERSE_VISIT_NO_CHILDREN);
15899d5e173STim Haley 	}
15999d5e173STim Haley 	return (0);
16099d5e173STim Haley }
16199d5e173STim Haley 
16299d5e173STim Haley int
dmu_diff(const char * tosnap_name,const char * fromsnap_name,struct vnode * vp,offset_t * offp)1633b2aab18SMatthew Ahrens dmu_diff(const char *tosnap_name, const char *fromsnap_name,
1643b2aab18SMatthew Ahrens     struct vnode *vp, offset_t *offp)
16599d5e173STim Haley {
16699d5e173STim Haley 	struct diffarg da;
1673b2aab18SMatthew Ahrens 	dsl_dataset_t *fromsnap;
1683b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
1693b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
1703b2aab18SMatthew Ahrens 	int error;
1713b2aab18SMatthew Ahrens 	uint64_t fromtxg;
17299d5e173STim Haley 
1733b2aab18SMatthew Ahrens 	if (strchr(tosnap_name, '@') == NULL ||
1743b2aab18SMatthew Ahrens 	    strchr(fromsnap_name, '@') == NULL)
175be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
17699d5e173STim Haley 
1773b2aab18SMatthew Ahrens 	error = dsl_pool_hold(tosnap_name, FTAG, &dp);
1783b2aab18SMatthew Ahrens 	if (error != 0)
1793b2aab18SMatthew Ahrens 		return (error);
18099d5e173STim Haley 
1813b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tosnap_name, FTAG, &tosnap);
1823b2aab18SMatthew Ahrens 	if (error != 0) {
1833b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
1843b2aab18SMatthew Ahrens 		return (error);
1853b2aab18SMatthew Ahrens 	}
18699d5e173STim Haley 
1873b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, fromsnap_name, FTAG, &fromsnap);
1883b2aab18SMatthew Ahrens 	if (error != 0) {
1893b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
1903b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
1913b2aab18SMatthew Ahrens 		return (error);
1923b2aab18SMatthew Ahrens 	}
19399d5e173STim Haley 
19478f17100SMatthew Ahrens 	if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
1953b2aab18SMatthew Ahrens 		dsl_dataset_rele(fromsnap, FTAG);
1963b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
1973b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
198be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
19999d5e173STim Haley 	}
20099d5e173STim Haley 
201c1379625SJustin T. Gibbs 	fromtxg = dsl_dataset_phys(fromsnap)->ds_creation_txg;
2023b2aab18SMatthew Ahrens 	dsl_dataset_rele(fromsnap, FTAG);
20399d5e173STim Haley 
2043b2aab18SMatthew Ahrens 	dsl_dataset_long_hold(tosnap, FTAG);
2053b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
20699d5e173STim Haley 
20799d5e173STim Haley 	da.da_vp = vp;
20899d5e173STim Haley 	da.da_offp = offp;
20999d5e173STim Haley 	da.da_ddr.ddr_type = DDR_NONE;
21099d5e173STim Haley 	da.da_ddr.ddr_first = da.da_ddr.ddr_last = 0;
21199d5e173STim Haley 	da.da_err = 0;
21299d5e173STim Haley 
213eb633035STom Caputi 	/*
214eb633035STom Caputi 	 * Since zfs diff only looks at dnodes which are stored in plaintext
215eb633035STom Caputi 	 * (other than bonus buffers), we don't technically need to decrypt
216eb633035STom Caputi 	 * the dataset to perform this operation. However, the command line
217eb633035STom Caputi 	 * utility will still fail if the keys are not loaded because the
218eb633035STom Caputi 	 * dataset isn't mounted and because it will fail when it attempts to
219eb633035STom Caputi 	 * call the ZFS_IOC_OBJ_TO_STATS ioctl.
220eb633035STom Caputi 	 */
2213b2aab18SMatthew Ahrens 	error = traverse_dataset(tosnap, fromtxg,
222eb633035STom Caputi 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_NO_DECRYPT,
223eb633035STom Caputi 	    diff_cb, &da);
22499d5e173STim Haley 
2253b2aab18SMatthew Ahrens 	if (error != 0) {
2263b2aab18SMatthew Ahrens 		da.da_err = error;
22799d5e173STim Haley 	} else {
22899d5e173STim Haley 		/* we set the da.da_err we return as side-effect */
22999d5e173STim Haley 		(void) write_record(&da);
23099d5e173STim Haley 	}
23199d5e173STim Haley 
2323b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(tosnap, FTAG);
2333b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
2343b2aab18SMatthew Ahrens 
23599d5e173STim Haley 	return (da.da_err);
23699d5e173STim Haley }
237