1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
25 */
26
27#include <sys/dmu.h>
28#include <sys/dmu_impl.h>
29#include <sys/dmu_tx.h>
30#include <sys/dbuf.h>
31#include <sys/dnode.h>
32#include <sys/zfs_context.h>
33#include <sys/dmu_objset.h>
34#include <sys/dmu_traverse.h>
35#include <sys/dsl_dataset.h>
36#include <sys/dsl_dir.h>
37#include <sys/dsl_pool.h>
38#include <sys/dsl_synctask.h>
39#include <sys/zfs_ioctl.h>
40#include <sys/zap.h>
41#include <sys/zio_checksum.h>
42#include <sys/zfs_znode.h>
43
44struct diffarg {
45	struct vnode *da_vp;		/* file to which we are reporting */
46	offset_t *da_offp;
47	int da_err;			/* error that stopped diff search */
48	dmu_diff_record_t da_ddr;
49};
50
51static int
52write_record(struct diffarg *da)
53{
54	ssize_t resid; /* have to get resid to get detailed errno */
55
56	if (da->da_ddr.ddr_type == DDR_NONE) {
57		da->da_err = 0;
58		return (0);
59	}
60
61	da->da_err = vn_rdwr(UIO_WRITE, da->da_vp, (caddr_t)&da->da_ddr,
62	    sizeof (da->da_ddr), 0, UIO_SYSSPACE, FAPPEND,
63	    RLIM64_INFINITY, CRED(), &resid);
64	*da->da_offp += sizeof (da->da_ddr);
65	return (da->da_err);
66}
67
68static int
69report_free_dnode_range(struct diffarg *da, uint64_t first, uint64_t last)
70{
71	ASSERT(first <= last);
72	if (da->da_ddr.ddr_type != DDR_FREE ||
73	    first != da->da_ddr.ddr_last + 1) {
74		if (write_record(da) != 0)
75			return (da->da_err);
76		da->da_ddr.ddr_type = DDR_FREE;
77		da->da_ddr.ddr_first = first;
78		da->da_ddr.ddr_last = last;
79		return (0);
80	}
81	da->da_ddr.ddr_last = last;
82	return (0);
83}
84
85static int
86report_dnode(struct diffarg *da, uint64_t object, dnode_phys_t *dnp)
87{
88	ASSERT(dnp != NULL);
89	if (dnp->dn_type == DMU_OT_NONE)
90		return (report_free_dnode_range(da, object, object));
91
92	if (da->da_ddr.ddr_type != DDR_INUSE ||
93	    object != da->da_ddr.ddr_last + 1) {
94		if (write_record(da) != 0)
95			return (da->da_err);
96		da->da_ddr.ddr_type = DDR_INUSE;
97		da->da_ddr.ddr_first = da->da_ddr.ddr_last = object;
98		return (0);
99	}
100	da->da_ddr.ddr_last = object;
101	return (0);
102}
103
104#define	DBP_SPAN(dnp, level)				  \
105	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
106	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
107
108/* ARGSUSED */
109static int
110diff_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
111    const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
112{
113	struct diffarg *da = arg;
114	int err = 0;
115
116	if (issig(JUSTLOOKING) && issig(FORREAL))
117		return (SET_ERROR(EINTR));
118
119	if (bp == NULL || zb->zb_object != DMU_META_DNODE_OBJECT)
120		return (0);
121
122	if (BP_IS_HOLE(bp)) {
123		uint64_t span = DBP_SPAN(dnp, zb->zb_level);
124		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
125
126		err = report_free_dnode_range(da, dnobj,
127		    dnobj + (span >> DNODE_SHIFT) - 1);
128		if (err)
129			return (err);
130	} else if (zb->zb_level == 0) {
131		dnode_phys_t *blk;
132		arc_buf_t *abuf;
133		arc_flags_t aflags = ARC_FLAG_WAIT;
134		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
135		int zio_flags = ZIO_FLAG_CANFAIL;
136		int i;
137
138		if (BP_IS_PROTECTED(bp))
139			zio_flags |= ZIO_FLAG_RAW;
140
141		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
142		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &aflags, zb) != 0)
143			return (SET_ERROR(EIO));
144
145		blk = abuf->b_data;
146		for (i = 0; i < epb; i += blk[i].dn_extra_slots + 1) {
147			uint64_t dnobj = (zb->zb_blkid <<
148			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
149			err = report_dnode(da, dnobj, blk+i);
150			if (err)
151				break;
152		}
153		arc_buf_destroy(abuf, &abuf);
154		if (err)
155			return (err);
156		/* Don't care about the data blocks */
157		return (TRAVERSE_VISIT_NO_CHILDREN);
158	}
159	return (0);
160}
161
162int
163dmu_diff(const char *tosnap_name, const char *fromsnap_name,
164    struct vnode *vp, offset_t *offp)
165{
166	struct diffarg da;
167	dsl_dataset_t *fromsnap;
168	dsl_dataset_t *tosnap;
169	dsl_pool_t *dp;
170	int error;
171	uint64_t fromtxg;
172
173	if (strchr(tosnap_name, '@') == NULL ||
174	    strchr(fromsnap_name, '@') == NULL)
175		return (SET_ERROR(EINVAL));
176
177	error = dsl_pool_hold(tosnap_name, FTAG, &dp);
178	if (error != 0)
179		return (error);
180
181	error = dsl_dataset_hold(dp, tosnap_name, FTAG, &tosnap);
182	if (error != 0) {
183		dsl_pool_rele(dp, FTAG);
184		return (error);
185	}
186
187	error = dsl_dataset_hold(dp, fromsnap_name, FTAG, &fromsnap);
188	if (error != 0) {
189		dsl_dataset_rele(tosnap, FTAG);
190		dsl_pool_rele(dp, FTAG);
191		return (error);
192	}
193
194	if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
195		dsl_dataset_rele(fromsnap, FTAG);
196		dsl_dataset_rele(tosnap, FTAG);
197		dsl_pool_rele(dp, FTAG);
198		return (SET_ERROR(EXDEV));
199	}
200
201	fromtxg = dsl_dataset_phys(fromsnap)->ds_creation_txg;
202	dsl_dataset_rele(fromsnap, FTAG);
203
204	dsl_dataset_long_hold(tosnap, FTAG);
205	dsl_pool_rele(dp, FTAG);
206
207	da.da_vp = vp;
208	da.da_offp = offp;
209	da.da_ddr.ddr_type = DDR_NONE;
210	da.da_ddr.ddr_first = da.da_ddr.ddr_last = 0;
211	da.da_err = 0;
212
213	/*
214	 * Since zfs diff only looks at dnodes which are stored in plaintext
215	 * (other than bonus buffers), we don't technically need to decrypt
216	 * the dataset to perform this operation. However, the command line
217	 * utility will still fail if the keys are not loaded because the
218	 * dataset isn't mounted and because it will fail when it attempts to
219	 * call the ZFS_IOC_OBJ_TO_STATS ioctl.
220	 */
221	error = traverse_dataset(tosnap, fromtxg,
222	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_NO_DECRYPT,
223	    diff_cb, &da);
224
225	if (error != 0) {
226		da.da_err = error;
227	} else {
228		/* we set the da.da_err we return as side-effect */
229		(void) write_record(&da);
230	}
231
232	dsl_dataset_long_rele(tosnap, FTAG);
233	dsl_dataset_rele(tosnap, FTAG);
234
235	return (da.da_err);
236}
237