]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu_diff.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dmu_diff.c
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  */
24
25 #include <sys/dmu.h>
26 #include <sys/dmu_impl.h>
27 #include <sys/dmu_tx.h>
28 #include <sys/dbuf.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>
38 #include <sys/zap.h>
39 #include <sys/zio_checksum.h>
40 #include <sys/zfs_znode.h>
41
42 struct diffarg {
43         struct file *da_fp;             /* file to which we are reporting */
44         offset_t *da_offp;
45         int da_err;                     /* error that stopped diff search */
46         dmu_diff_record_t da_ddr;
47         kthread_t *da_td;
48 };
49
50 static int
51 write_bytes(struct diffarg *da)
52 {
53         struct uio auio;
54         struct iovec aiov;
55
56         aiov.iov_base = (caddr_t)&da->da_ddr;
57         aiov.iov_len = sizeof (da->da_ddr);
58         auio.uio_iov = &aiov;
59         auio.uio_iovcnt = 1;
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;
65 #ifdef _KERNEL
66         if (da->da_fp->f_type == DTYPE_VNODE)
67                 bwillwrite();
68         return (fo_write(da->da_fp, &auio, da->da_td->td_ucred, 0, da->da_td));
69 #else
70         fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
71         return (EOPNOTSUPP);
72 #endif
73 }
74
75 static int
76 write_record(struct diffarg *da)
77 {
78
79         if (da->da_ddr.ddr_type == DDR_NONE) {
80                 da->da_err = 0;
81                 return (0);
82         }
83
84         da->da_err = write_bytes(da);
85         *da->da_offp += sizeof (da->da_ddr);
86         return (da->da_err);
87 }
88
89 static int
90 report_free_dnode_range(struct diffarg *da, uint64_t first, uint64_t last)
91 {
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)
96                         return (da->da_err);
97                 da->da_ddr.ddr_type = DDR_FREE;
98                 da->da_ddr.ddr_first = first;
99                 da->da_ddr.ddr_last = last;
100                 return (0);
101         }
102         da->da_ddr.ddr_last = last;
103         return (0);
104 }
105
106 static int
107 report_dnode(struct diffarg *da, uint64_t object, dnode_phys_t *dnp)
108 {
109         ASSERT(dnp != NULL);
110         if (dnp->dn_type == DMU_OT_NONE)
111                 return (report_free_dnode_range(da, object, object));
112
113         if (da->da_ddr.ddr_type != DDR_INUSE ||
114             object != da->da_ddr.ddr_last + 1) {
115                 if (write_record(da) != 0)
116                         return (da->da_err);
117                 da->da_ddr.ddr_type = DDR_INUSE;
118                 da->da_ddr.ddr_first = da->da_ddr.ddr_last = object;
119                 return (0);
120         }
121         da->da_ddr.ddr_last = object;
122         return (0);
123 }
124
125 #define DBP_SPAN(dnp, level)                              \
126         (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
127         (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
128
129 /* ARGSUSED */
130 static int
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)
133 {
134         struct diffarg *da = arg;
135         int err = 0;
136
137         if (issig(JUSTLOOKING) && issig(FORREAL))
138                 return (EINTR);
139
140         if (zb->zb_object != DMU_META_DNODE_OBJECT)
141                 return (0);
142
143         if (bp == NULL) {
144                 uint64_t span = DBP_SPAN(dnp, zb->zb_level);
145                 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
146
147                 err = report_free_dnode_range(da, dnobj,
148                     dnobj + (span >> DNODE_SHIFT) - 1);
149                 if (err)
150                         return (err);
151         } else if (zb->zb_level == 0) {
152                 dnode_phys_t *blk;
153                 arc_buf_t *abuf;
154                 uint32_t aflags = ARC_WAIT;
155                 int blksz = BP_GET_LSIZE(bp);
156                 int i;
157
158                 if (dsl_read(NULL, spa, bp, pbuf,
159                     arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
160                     ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
161                         return (EIO);
162
163                 blk = abuf->b_data;
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);
168                         if (err)
169                                 break;
170                 }
171                 (void) arc_buf_remove_ref(abuf, &abuf);
172                 if (err)
173                         return (err);
174                 /* Don't care about the data blocks */
175                 return (TRAVERSE_VISIT_NO_CHILDREN);
176         }
177         return (0);
178 }
179
180 int
181 dmu_diff(objset_t *tosnap, objset_t *fromsnap, struct file *fp, offset_t *offp)
182 {
183         struct diffarg da;
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;
188         int err = 0;
189
190         /* make certain we are looking at snapshots */
191         if (!dsl_dataset_is_snapshot(ds) || !dsl_dataset_is_snapshot(fromds))
192                 return (EINVAL);
193
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)
196                 return (EXDEV);
197
198         relds = NULL;
199         findds = ds;
200
201         while (fromds->ds_dir != findds->ds_dir) {
202                 dsl_pool_t *dp = ds->ds_dir->dd_pool;
203
204                 if (!dsl_dir_is_clone(findds->ds_dir)) {
205                         if (relds)
206                                 dsl_dataset_rele(relds, FTAG);
207                         return (EXDEV);
208                 }
209
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);
214
215                 if (relds)
216                         dsl_dataset_rele(relds, FTAG);
217
218                 if (err)
219                         return (EXDEV);
220
221                 relds = findds;
222         }
223
224         if (relds)
225                 dsl_dataset_rele(relds, FTAG);
226
227         da.da_fp = fp;
228         da.da_offp = offp;
229         da.da_ddr.ddr_type = DDR_NONE;
230         da.da_ddr.ddr_first = da.da_ddr.ddr_last = 0;
231         da.da_err = 0;
232         da.da_td = curthread;
233
234         err = traverse_dataset(ds, fromds->ds_phys->ds_creation_txg,
235             TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, diff_cb, &da);
236
237         if (err) {
238                 da.da_err = err;
239         } else {
240                 /* we set the da.da_err we return as side-effect */
241                 (void) write_record(&da);
242         }
243
244         return (da.da_err);
245 }