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