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.
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.
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]
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/dmu_impl.h>
27 #include <sys/dmu_tx.h>
29 #include <sys/dnode.h>
30 #include <sys/zfs_context.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_traverse.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/zfs_ioctl.h>
39 #include <sys/zio_checksum.h>
40 #include <sys/zfs_znode.h>
43 struct file *da_fp; /* file to which we are reporting */
45 int da_err; /* error that stopped diff search */
46 dmu_diff_record_t da_ddr;
51 write_bytes(struct diffarg *da)
56 aiov.iov_base = (caddr_t)&da->da_ddr;
57 aiov.iov_len = sizeof (da->da_ddr);
60 auio.uio_resid = aiov.iov_len;
61 auio.uio_segflg = UIO_SYSSPACE;
62 auio.uio_rw = UIO_WRITE;
63 auio.uio_offset = (off_t)-1;
64 auio.uio_td = da->da_td;
66 if (da->da_fp->f_type == DTYPE_VNODE)
68 return (fo_write(da->da_fp, &auio, da->da_td->td_ucred, 0, da->da_td));
70 fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
76 write_record(struct diffarg *da)
79 if (da->da_ddr.ddr_type == DDR_NONE) {
84 da->da_err = write_bytes(da);
85 *da->da_offp += sizeof (da->da_ddr);
90 report_free_dnode_range(struct diffarg *da, uint64_t first, uint64_t last)
92 ASSERT(first <= last);
93 if (da->da_ddr.ddr_type != DDR_FREE ||
94 first != da->da_ddr.ddr_last + 1) {
95 if (write_record(da) != 0)
97 da->da_ddr.ddr_type = DDR_FREE;
98 da->da_ddr.ddr_first = first;
99 da->da_ddr.ddr_last = last;
102 da->da_ddr.ddr_last = last;
107 report_dnode(struct diffarg *da, uint64_t object, dnode_phys_t *dnp)
110 if (dnp->dn_type == DMU_OT_NONE)
111 return (report_free_dnode_range(da, object, object));
113 if (da->da_ddr.ddr_type != DDR_INUSE ||
114 object != da->da_ddr.ddr_last + 1) {
115 if (write_record(da) != 0)
117 da->da_ddr.ddr_type = DDR_INUSE;
118 da->da_ddr.ddr_first = da->da_ddr.ddr_last = object;
121 da->da_ddr.ddr_last = object;
125 #define DBP_SPAN(dnp, level) \
126 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
127 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
131 diff_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
132 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
134 struct diffarg *da = arg;
137 if (issig(JUSTLOOKING) && issig(FORREAL))
140 if (zb->zb_object != DMU_META_DNODE_OBJECT)
144 uint64_t span = DBP_SPAN(dnp, zb->zb_level);
145 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
147 err = report_free_dnode_range(da, dnobj,
148 dnobj + (span >> DNODE_SHIFT) - 1);
151 } else if (zb->zb_level == 0) {
154 uint32_t aflags = ARC_WAIT;
155 int blksz = BP_GET_LSIZE(bp);
158 if (dsl_read(NULL, spa, bp, pbuf,
159 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
160 ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
164 for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
165 uint64_t dnobj = (zb->zb_blkid <<
166 (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
167 err = report_dnode(da, dnobj, blk+i);
171 (void) arc_buf_remove_ref(abuf, &abuf);
174 /* Don't care about the data blocks */
175 return (TRAVERSE_VISIT_NO_CHILDREN);
181 dmu_diff(objset_t *tosnap, objset_t *fromsnap, struct file *fp, offset_t *offp)
184 dsl_dataset_t *ds = tosnap->os_dsl_dataset;
185 dsl_dataset_t *fromds = fromsnap->os_dsl_dataset;
186 dsl_dataset_t *findds;
187 dsl_dataset_t *relds;
190 /* make certain we are looking at snapshots */
191 if (!dsl_dataset_is_snapshot(ds) || !dsl_dataset_is_snapshot(fromds))
194 /* fromsnap must be earlier and from the same lineage as tosnap */
195 if (fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg)
201 while (fromds->ds_dir != findds->ds_dir) {
202 dsl_pool_t *dp = ds->ds_dir->dd_pool;
204 if (!dsl_dir_is_clone(findds->ds_dir)) {
206 dsl_dataset_rele(relds, FTAG);
210 rw_enter(&dp->dp_config_rwlock, RW_READER);
211 err = dsl_dataset_hold_obj(dp,
212 findds->ds_dir->dd_phys->dd_origin_obj, FTAG, &findds);
213 rw_exit(&dp->dp_config_rwlock);
216 dsl_dataset_rele(relds, FTAG);
225 dsl_dataset_rele(relds, FTAG);
229 da.da_ddr.ddr_type = DDR_NONE;
230 da.da_ddr.ddr_first = da.da_ddr.ddr_last = 0;
232 da.da_td = curthread;
234 err = traverse_dataset(ds, fromds->ds_phys->ds_creation_txg,
235 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, diff_cb, &da);
240 /* we set the da.da_err we return as side-effect */
241 (void) write_record(&da);