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.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
27 #include <sys/dmu_impl.h>
28 #include <sys/dmu_tx.h>
30 #include <sys/dnode.h>
31 #include <sys/zfs_context.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/zfs_ioctl.h>
40 #include <sys/zio_checksum.h>
41 #include <sys/zfs_znode.h>
44 struct vnode
*da_vp
; /* file to which we are reporting */
46 int da_err
; /* error that stopped diff search */
47 dmu_diff_record_t da_ddr
;
51 write_record(struct diffarg
*da
)
53 ssize_t resid
; /* have to get resid to get detailed errno */
55 if (da
->da_ddr
.ddr_type
== DDR_NONE
) {
60 da
->da_err
= vn_rdwr(UIO_WRITE
, da
->da_vp
, (caddr_t
)&da
->da_ddr
,
61 sizeof (da
->da_ddr
), 0, UIO_SYSSPACE
, FAPPEND
,
62 RLIM64_INFINITY
, CRED(), &resid
);
63 *da
->da_offp
+= sizeof (da
->da_ddr
);
68 report_free_dnode_range(struct diffarg
*da
, uint64_t first
, uint64_t last
)
70 ASSERT(first
<= last
);
71 if (da
->da_ddr
.ddr_type
!= DDR_FREE
||
72 first
!= da
->da_ddr
.ddr_last
+ 1) {
73 if (write_record(da
) != 0)
75 da
->da_ddr
.ddr_type
= DDR_FREE
;
76 da
->da_ddr
.ddr_first
= first
;
77 da
->da_ddr
.ddr_last
= last
;
80 da
->da_ddr
.ddr_last
= last
;
85 report_dnode(struct diffarg
*da
, uint64_t object
, dnode_phys_t
*dnp
)
88 if (dnp
->dn_type
== DMU_OT_NONE
)
89 return (report_free_dnode_range(da
, object
, object
));
91 if (da
->da_ddr
.ddr_type
!= DDR_INUSE
||
92 object
!= da
->da_ddr
.ddr_last
+ 1) {
93 if (write_record(da
) != 0)
95 da
->da_ddr
.ddr_type
= DDR_INUSE
;
96 da
->da_ddr
.ddr_first
= da
->da_ddr
.ddr_last
= object
;
99 da
->da_ddr
.ddr_last
= object
;
103 #define DBP_SPAN(dnp, level) \
104 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
105 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
109 diff_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
110 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
112 struct diffarg
*da
= arg
;
115 if (issig(JUSTLOOKING
) && issig(FORREAL
))
116 return (SET_ERROR(EINTR
));
118 if (bp
== NULL
|| zb
->zb_object
!= DMU_META_DNODE_OBJECT
)
121 if (BP_IS_HOLE(bp
)) {
122 uint64_t span
= DBP_SPAN(dnp
, zb
->zb_level
);
123 uint64_t dnobj
= (zb
->zb_blkid
* span
) >> DNODE_SHIFT
;
125 err
= report_free_dnode_range(da
, dnobj
,
126 dnobj
+ (span
>> DNODE_SHIFT
) - 1);
129 } else if (zb
->zb_level
== 0) {
132 arc_flags_t aflags
= ARC_FLAG_WAIT
;
133 int blksz
= BP_GET_LSIZE(bp
);
134 int zio_flags
= ZIO_FLAG_CANFAIL
;
137 if (BP_IS_PROTECTED(bp
))
138 zio_flags
|= ZIO_FLAG_RAW
;
140 if (arc_read(NULL
, spa
, bp
, arc_getbuf_func
, &abuf
,
141 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &aflags
, zb
) != 0)
142 return (SET_ERROR(EIO
));
145 for (i
= 0; i
< blksz
>> DNODE_SHIFT
; i
++) {
146 uint64_t dnobj
= (zb
->zb_blkid
<<
147 (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
)) + i
;
148 err
= report_dnode(da
, dnobj
, blk
+i
);
152 arc_buf_destroy(abuf
, &abuf
);
155 /* Don't care about the data blocks */
156 return (TRAVERSE_VISIT_NO_CHILDREN
);
162 dmu_diff(const char *tosnap_name
, const char *fromsnap_name
,
163 struct vnode
*vp
, offset_t
*offp
)
166 dsl_dataset_t
*fromsnap
;
167 dsl_dataset_t
*tosnap
;
172 if (strchr(tosnap_name
, '@') == NULL
||
173 strchr(fromsnap_name
, '@') == NULL
)
174 return (SET_ERROR(EINVAL
));
176 error
= dsl_pool_hold(tosnap_name
, FTAG
, &dp
);
180 error
= dsl_dataset_hold(dp
, tosnap_name
, FTAG
, &tosnap
);
182 dsl_pool_rele(dp
, FTAG
);
186 error
= dsl_dataset_hold(dp
, fromsnap_name
, FTAG
, &fromsnap
);
188 dsl_dataset_rele(tosnap
, FTAG
);
189 dsl_pool_rele(dp
, FTAG
);
193 if (!dsl_dataset_is_before(tosnap
, fromsnap
, 0)) {
194 dsl_dataset_rele(fromsnap
, FTAG
);
195 dsl_dataset_rele(tosnap
, FTAG
);
196 dsl_pool_rele(dp
, FTAG
);
197 return (SET_ERROR(EXDEV
));
200 fromtxg
= dsl_dataset_phys(fromsnap
)->ds_creation_txg
;
201 dsl_dataset_rele(fromsnap
, FTAG
);
203 dsl_dataset_long_hold(tosnap
, FTAG
);
204 dsl_pool_rele(dp
, FTAG
);
208 da
.da_ddr
.ddr_type
= DDR_NONE
;
209 da
.da_ddr
.ddr_first
= da
.da_ddr
.ddr_last
= 0;
213 * Since zfs diff only looks at dnodes which are stored in plaintext
214 * (other than bonus buffers), we don't technically need to decrypt
215 * the dataset to perform this operation. However, the command line
216 * utility will still fail if the keys are not loaded because the
217 * dataset isn't mounted and because it will fail when it attempts to
218 * call the ZFS_IOC_OBJ_TO_STATS ioctl.
220 error
= traverse_dataset(tosnap
, fromtxg
,
221 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
| TRAVERSE_NO_DECRYPT
,
227 /* we set the da.da_err we return as side-effect */
228 (void) write_record(&da
);
231 dsl_dataset_long_rele(tosnap
, FTAG
);
232 dsl_dataset_rele(tosnap
, FTAG
);