xref: /titanic_50/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision 7e16fca05dfbcfd32c2ebc9e4d1abdac1cd8657c)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/dmu_objset.h>
28 #include <sys/dmu_traverse.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/dnode.h>
33 #include <sys/spa.h>
34 #include <sys/zio.h>
35 #include <sys/dmu_impl.h>
36 #include <sys/callb.h>
37 
38 #define	SET_BOOKMARK(zb, objset, object, level, blkid)  \
39 {                                                       \
40 	(zb)->zb_objset = objset;                       \
41 	(zb)->zb_object = object;                       \
42 	(zb)->zb_level = level;                         \
43 	(zb)->zb_blkid = blkid;                         \
44 }
45 
46 struct prefetch_data {
47 	kmutex_t pd_mtx;
48 	kcondvar_t pd_cv;
49 	int pd_blks_max;
50 	int pd_blks_fetched;
51 	int pd_flags;
52 	boolean_t pd_cancel;
53 	boolean_t pd_exited;
54 };
55 
56 struct traverse_data {
57 	spa_t *td_spa;
58 	uint64_t td_objset;
59 	blkptr_t *td_rootbp;
60 	uint64_t td_min_txg;
61 	int td_flags;
62 	struct prefetch_data *td_pfd;
63 	blkptr_cb_t *td_func;
64 	void *td_arg;
65 };
66 
67 /* ARGSUSED */
68 static void
69 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
70 {
71 	struct traverse_data *td = arg;
72 	zbookmark_t zb;
73 
74 	if (bp->blk_birth == 0)
75 		return;
76 
77 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
78 		return;
79 
80 	zb.zb_objset = td->td_objset;
81 	zb.zb_object = 0;
82 	zb.zb_level = -1;
83 	zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
84 	VERIFY(0 == td->td_func(td->td_spa, bp, &zb, NULL, td->td_arg));
85 }
86 
87 /* ARGSUSED */
88 static void
89 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
90 {
91 	struct traverse_data *td = arg;
92 
93 	if (lrc->lrc_txtype == TX_WRITE) {
94 		lr_write_t *lr = (lr_write_t *)lrc;
95 		blkptr_t *bp = &lr->lr_blkptr;
96 		zbookmark_t zb;
97 
98 		if (bp->blk_birth == 0)
99 			return;
100 
101 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
102 			return;
103 
104 		zb.zb_objset = td->td_objset;
105 		zb.zb_object = lr->lr_foid;
106 		zb.zb_level = BP_GET_LEVEL(bp);
107 		zb.zb_blkid = lr->lr_offset / BP_GET_LSIZE(bp);
108 		VERIFY(0 == td->td_func(td->td_spa, bp, &zb, NULL, td->td_arg));
109 	}
110 }
111 
112 static void
113 traverse_zil(struct traverse_data *td, zil_header_t *zh)
114 {
115 	uint64_t claim_txg = zh->zh_claim_txg;
116 	zilog_t *zilog;
117 
118 	/*
119 	 * We only want to visit blocks that have been claimed but not yet
120 	 * replayed (or, in read-only mode, blocks that *would* be claimed).
121 	 */
122 	if (claim_txg == 0 && spa_writeable(td->td_spa))
123 		return;
124 
125 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
126 
127 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
128 	    claim_txg);
129 
130 	zil_free(zilog);
131 }
132 
133 static int
134 traverse_visitbp(struct traverse_data *td, const dnode_phys_t *dnp,
135     arc_buf_t *pbuf, blkptr_t *bp, const zbookmark_t *zb)
136 {
137 	zbookmark_t czb;
138 	int err = 0;
139 	arc_buf_t *buf = NULL;
140 	struct prefetch_data *pd = td->td_pfd;
141 
142 	if (bp->blk_birth == 0) {
143 		err = td->td_func(td->td_spa, NULL, zb, dnp, td->td_arg);
144 		return (err);
145 	}
146 
147 	if (bp->blk_birth <= td->td_min_txg)
148 		return (0);
149 
150 	if (pd && !pd->pd_exited &&
151 	    ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
152 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) {
153 		mutex_enter(&pd->pd_mtx);
154 		ASSERT(pd->pd_blks_fetched >= 0);
155 		while (pd->pd_blks_fetched == 0 && !pd->pd_exited)
156 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
157 		pd->pd_blks_fetched--;
158 		cv_broadcast(&pd->pd_cv);
159 		mutex_exit(&pd->pd_mtx);
160 	}
161 
162 	if (td->td_flags & TRAVERSE_PRE) {
163 		err = td->td_func(td->td_spa, bp, zb, dnp, td->td_arg);
164 		if (err)
165 			return (err);
166 	}
167 
168 	if (BP_GET_LEVEL(bp) > 0) {
169 		uint32_t flags = ARC_WAIT;
170 		int i;
171 		blkptr_t *cbp;
172 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
173 
174 		err = arc_read(NULL, td->td_spa, bp, pbuf,
175 		    arc_getbuf_func, &buf,
176 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
177 		if (err)
178 			return (err);
179 
180 		/* recursively visitbp() blocks below this */
181 		cbp = buf->b_data;
182 		for (i = 0; i < epb; i++, cbp++) {
183 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
184 			    zb->zb_level - 1,
185 			    zb->zb_blkid * epb + i);
186 			err = traverse_visitbp(td, dnp, buf, cbp, &czb);
187 			if (err)
188 				break;
189 		}
190 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
191 		uint32_t flags = ARC_WAIT;
192 		int i, j;
193 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
194 
195 		err = arc_read(NULL, td->td_spa, bp, pbuf,
196 		    arc_getbuf_func, &buf,
197 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
198 		if (err)
199 			return (err);
200 
201 		/* recursively visitbp() blocks below this */
202 		dnp = buf->b_data;
203 		for (i = 0; i < epb && err == 0; i++, dnp++) {
204 			for (j = 0; j < dnp->dn_nblkptr; j++) {
205 				SET_BOOKMARK(&czb, zb->zb_objset,
206 				    zb->zb_blkid * epb + i,
207 				    dnp->dn_nlevels - 1, j);
208 				err = traverse_visitbp(td, dnp, buf,
209 				    (blkptr_t *)&dnp->dn_blkptr[j], &czb);
210 				if (err)
211 					break;
212 			}
213 		}
214 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
215 		uint32_t flags = ARC_WAIT;
216 		objset_phys_t *osp;
217 		int j;
218 
219 		err = arc_read_nolock(NULL, td->td_spa, bp,
220 		    arc_getbuf_func, &buf,
221 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
222 		if (err)
223 			return (err);
224 
225 		osp = buf->b_data;
226 		traverse_zil(td, &osp->os_zil_header);
227 
228 		for (j = 0; j < osp->os_meta_dnode.dn_nblkptr; j++) {
229 			SET_BOOKMARK(&czb, zb->zb_objset, 0,
230 			    osp->os_meta_dnode.dn_nlevels - 1, j);
231 			err = traverse_visitbp(td, &osp->os_meta_dnode, buf,
232 			    (blkptr_t *)&osp->os_meta_dnode.dn_blkptr[j],
233 			    &czb);
234 			if (err)
235 				break;
236 		}
237 	}
238 
239 	if (buf)
240 		(void) arc_buf_remove_ref(buf, &buf);
241 
242 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
243 		err = td->td_func(td->td_spa, bp, zb, dnp, td->td_arg);
244 
245 	return (err);
246 }
247 
248 /* ARGSUSED */
249 static int
250 traverse_prefetcher(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
251     const dnode_phys_t *dnp, void *arg)
252 {
253 	struct prefetch_data *pfd = arg;
254 	uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
255 
256 	ASSERT(pfd->pd_blks_fetched >= 0);
257 	if (pfd->pd_cancel)
258 		return (EINTR);
259 
260 	if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
261 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0))
262 		return (0);
263 
264 	mutex_enter(&pfd->pd_mtx);
265 	while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max)
266 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
267 	pfd->pd_blks_fetched++;
268 	cv_broadcast(&pfd->pd_cv);
269 	mutex_exit(&pfd->pd_mtx);
270 
271 	(void) arc_read_nolock(NULL, spa, bp, NULL, NULL,
272 	    ZIO_PRIORITY_ASYNC_READ,
273 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
274 	    &aflags, zb);
275 
276 	return (0);
277 }
278 
279 static void
280 traverse_prefetch_thread(void *arg)
281 {
282 	struct traverse_data *td_main = arg;
283 	struct traverse_data td = *td_main;
284 	zbookmark_t czb;
285 
286 	td.td_func = traverse_prefetcher;
287 	td.td_arg = td_main->td_pfd;
288 	td.td_pfd = NULL;
289 
290 	SET_BOOKMARK(&czb, td.td_objset, 0, -1, 0);
291 	(void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb);
292 
293 	mutex_enter(&td_main->td_pfd->pd_mtx);
294 	td_main->td_pfd->pd_exited = B_TRUE;
295 	cv_broadcast(&td_main->td_pfd->pd_cv);
296 	mutex_exit(&td_main->td_pfd->pd_mtx);
297 }
298 
299 /*
300  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
301  * in syncing context).
302  */
303 static int
304 traverse_impl(spa_t *spa, uint64_t objset, blkptr_t *rootbp,
305     uint64_t txg_start, int flags, blkptr_cb_t func, void *arg)
306 {
307 	struct traverse_data td;
308 	struct prefetch_data pd = { 0 };
309 	zbookmark_t czb;
310 	int err;
311 
312 	td.td_spa = spa;
313 	td.td_objset = objset;
314 	td.td_rootbp = rootbp;
315 	td.td_min_txg = txg_start;
316 	td.td_func = func;
317 	td.td_arg = arg;
318 	td.td_pfd = &pd;
319 	td.td_flags = flags;
320 
321 	pd.pd_blks_max = 100;
322 	pd.pd_flags = flags;
323 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
324 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
325 
326 	if (!(flags & TRAVERSE_PREFETCH) ||
327 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
328 	    &td, TQ_NOQUEUE))
329 		pd.pd_exited = B_TRUE;
330 
331 	SET_BOOKMARK(&czb, objset, 0, -1, 0);
332 	err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb);
333 
334 	mutex_enter(&pd.pd_mtx);
335 	pd.pd_cancel = B_TRUE;
336 	cv_broadcast(&pd.pd_cv);
337 	while (!pd.pd_exited)
338 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
339 	mutex_exit(&pd.pd_mtx);
340 
341 	mutex_destroy(&pd.pd_mtx);
342 	cv_destroy(&pd.pd_cv);
343 
344 	return (err);
345 }
346 
347 /*
348  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
349  * in syncing context).
350  */
351 int
352 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
353     blkptr_cb_t func, void *arg)
354 {
355 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds->ds_object,
356 	    &ds->ds_phys->ds_bp, txg_start, flags, func, arg));
357 }
358 
359 /*
360  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
361  */
362 int
363 traverse_pool(spa_t *spa, blkptr_cb_t func, void *arg)
364 {
365 	int err;
366 	uint64_t obj;
367 	dsl_pool_t *dp = spa_get_dsl(spa);
368 	objset_t *mos = dp->dp_meta_objset;
369 
370 	/* visit the MOS */
371 	err = traverse_impl(spa, 0, spa_get_rootblkptr(spa),
372 	    0, TRAVERSE_PRE, func, arg);
373 	if (err)
374 		return (err);
375 
376 	/* visit each dataset */
377 	for (obj = 1; err == 0; err = dmu_object_next(mos, &obj, FALSE, 0)) {
378 		dmu_object_info_t doi;
379 
380 		err = dmu_object_info(mos, obj, &doi);
381 		if (err)
382 			return (err);
383 
384 		if (doi.doi_type == DMU_OT_DSL_DATASET) {
385 			dsl_dataset_t *ds;
386 			rw_enter(&dp->dp_config_rwlock, RW_READER);
387 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
388 			rw_exit(&dp->dp_config_rwlock);
389 			if (err)
390 				return (err);
391 			err = traverse_dataset(ds,
392 			    ds->ds_phys->ds_prev_snap_txg, TRAVERSE_PRE,
393 			    func, arg);
394 			dsl_dataset_rele(ds, FTAG);
395 			if (err)
396 				return (err);
397 		}
398 	}
399 	if (err == ESRCH)
400 		err = 0;
401 	return (err);
402 }
403