xref: /titanic_41/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision 54c529d43d4363891fd7381edde56d2cafbff593)
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 2008 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_mode & FWRITE))
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 	int err = 0;
138 	arc_buf_t *buf = NULL;
139 	struct prefetch_data *pd = td->td_pfd;
140 
141 	if (bp->blk_birth == 0) {
142 		err = td->td_func(td->td_spa, NULL, zb, dnp, td->td_arg);
143 		return (err);
144 	}
145 
146 	if (bp->blk_birth <= td->td_min_txg)
147 		return (0);
148 
149 	if (pd && !pd->pd_exited &&
150 	    ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
151 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) {
152 		mutex_enter(&pd->pd_mtx);
153 		ASSERT(pd->pd_blks_fetched >= 0);
154 		while (pd->pd_blks_fetched == 0 && !pd->pd_exited)
155 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
156 		pd->pd_blks_fetched--;
157 		cv_broadcast(&pd->pd_cv);
158 		mutex_exit(&pd->pd_mtx);
159 	}
160 
161 	if (td->td_flags & TRAVERSE_PRE) {
162 		err = td->td_func(td->td_spa, bp, zb, dnp, td->td_arg);
163 		if (err)
164 			return (err);
165 	}
166 
167 	if (BP_GET_LEVEL(bp) > 0) {
168 		uint32_t flags = ARC_WAIT;
169 		int i;
170 		blkptr_t *cbp;
171 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
172 
173 		err = arc_read(NULL, td->td_spa, bp, pbuf,
174 		    arc_getbuf_func, &buf,
175 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
176 		if (err)
177 			return (err);
178 
179 		/* recursively visitbp() blocks below this */
180 		cbp = buf->b_data;
181 		for (i = 0; i < epb; i++, cbp++) {
182 			zbookmark_t czb;
183 
184 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
185 			    zb->zb_level - 1,
186 			    zb->zb_blkid * epb + i);
187 			err = traverse_visitbp(td, dnp, buf, cbp, &czb);
188 			if (err)
189 				break;
190 		}
191 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
192 		uint32_t flags = ARC_WAIT;
193 		int i, j;
194 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
195 
196 		err = arc_read(NULL, td->td_spa, bp, pbuf,
197 		    arc_getbuf_func, &buf,
198 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
199 		if (err)
200 			return (err);
201 
202 		/* recursively visitbp() blocks below this */
203 		dnp = buf->b_data;
204 		for (i = 0; i < epb && err == 0; i++, dnp++) {
205 			for (j = 0; j < dnp->dn_nblkptr; j++) {
206 				zbookmark_t czb;
207 
208 				SET_BOOKMARK(&czb, zb->zb_objset,
209 				    zb->zb_blkid * epb + i,
210 				    dnp->dn_nlevels - 1, j);
211 				err = traverse_visitbp(td, dnp, buf,
212 				    (blkptr_t *)&dnp->dn_blkptr[j], &czb);
213 				if (err)
214 					break;
215 			}
216 		}
217 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
218 		uint32_t flags = ARC_WAIT;
219 		objset_phys_t *osp;
220 		int j;
221 
222 		err = arc_read_nolock(NULL, td->td_spa, bp,
223 		    arc_getbuf_func, &buf,
224 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
225 		if (err)
226 			return (err);
227 
228 		osp = buf->b_data;
229 		/*
230 		 * traverse_zil is just here for zdb's leak checking.
231 		 * For other consumers, there will be no ZIL blocks.
232 		 */
233 		traverse_zil(td, &osp->os_zil_header);
234 
235 		for (j = 0; j < osp->os_meta_dnode.dn_nblkptr; j++) {
236 			zbookmark_t czb;
237 
238 			SET_BOOKMARK(&czb, zb->zb_objset, 0,
239 			    osp->os_meta_dnode.dn_nlevels - 1, j);
240 			err = traverse_visitbp(td, &osp->os_meta_dnode, buf,
241 			    (blkptr_t *)&osp->os_meta_dnode.dn_blkptr[j],
242 			    &czb);
243 			if (err)
244 				break;
245 		}
246 	}
247 
248 	if (buf)
249 		(void) arc_buf_remove_ref(buf, &buf);
250 
251 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
252 		err = td->td_func(td->td_spa, bp, zb, dnp, td->td_arg);
253 
254 	return (err);
255 }
256 
257 /* ARGSUSED */
258 static int
259 traverse_prefetcher(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
260     const dnode_phys_t *dnp, void *arg)
261 {
262 	struct prefetch_data *pfd = arg;
263 	uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
264 
265 	ASSERT(pfd->pd_blks_fetched >= 0);
266 	if (pfd->pd_cancel)
267 		return (EINTR);
268 
269 	if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
270 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0))
271 		return (0);
272 
273 	mutex_enter(&pfd->pd_mtx);
274 	while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max)
275 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
276 	pfd->pd_blks_fetched++;
277 	cv_broadcast(&pfd->pd_cv);
278 	mutex_exit(&pfd->pd_mtx);
279 
280 	(void) arc_read_nolock(NULL, spa, bp, NULL, NULL,
281 	    ZIO_PRIORITY_ASYNC_READ,
282 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
283 	    &aflags, zb);
284 
285 	return (0);
286 }
287 
288 static void
289 traverse_prefetch_thread(void *arg)
290 {
291 	struct traverse_data *td_main = arg;
292 	struct traverse_data td = *td_main;
293 	zbookmark_t czb;
294 
295 	td.td_func = traverse_prefetcher;
296 	td.td_arg = td_main->td_pfd;
297 	td.td_pfd = NULL;
298 
299 	SET_BOOKMARK(&czb, td.td_objset, 0, -1, 0);
300 	(void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb);
301 
302 	mutex_enter(&td_main->td_pfd->pd_mtx);
303 	td_main->td_pfd->pd_exited = B_TRUE;
304 	cv_broadcast(&td_main->td_pfd->pd_cv);
305 	mutex_exit(&td_main->td_pfd->pd_mtx);
306 }
307 
308 /*
309  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
310  * in syncing context).
311  */
312 static int
313 traverse_impl(spa_t *spa, uint64_t objset, blkptr_t *rootbp,
314     uint64_t txg_start, int flags, blkptr_cb_t func, void *arg)
315 {
316 	struct traverse_data td;
317 	struct prefetch_data pd = { 0 };
318 	zbookmark_t czb;
319 	int err;
320 
321 	td.td_spa = spa;
322 	td.td_objset = objset;
323 	td.td_rootbp = rootbp;
324 	td.td_min_txg = txg_start;
325 	td.td_func = func;
326 	td.td_arg = arg;
327 	td.td_pfd = &pd;
328 	td.td_flags = flags;
329 
330 	pd.pd_blks_max = 100;
331 	pd.pd_flags = flags;
332 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
333 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
334 
335 	if (!(flags & TRAVERSE_PREFETCH) ||
336 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
337 	    &td, TQ_NOQUEUE))
338 		pd.pd_exited = B_TRUE;
339 
340 	SET_BOOKMARK(&czb, objset, 0, -1, 0);
341 	err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb);
342 
343 	mutex_enter(&pd.pd_mtx);
344 	pd.pd_cancel = B_TRUE;
345 	cv_broadcast(&pd.pd_cv);
346 	while (!pd.pd_exited)
347 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
348 	mutex_exit(&pd.pd_mtx);
349 
350 	mutex_destroy(&pd.pd_mtx);
351 	cv_destroy(&pd.pd_cv);
352 
353 	return (err);
354 }
355 
356 /*
357  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
358  * in syncing context).
359  */
360 int
361 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
362     blkptr_cb_t func, void *arg)
363 {
364 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds->ds_object,
365 	    &ds->ds_phys->ds_bp, txg_start, flags, func, arg));
366 }
367 
368 /*
369  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
370  */
371 int
372 traverse_pool(spa_t *spa, blkptr_cb_t func, void *arg)
373 {
374 	int err;
375 	uint64_t obj;
376 	dsl_pool_t *dp = spa_get_dsl(spa);
377 	objset_t *mos = dp->dp_meta_objset;
378 
379 	/* visit the MOS */
380 	err = traverse_impl(spa, 0, spa_get_rootblkptr(spa),
381 	    0, TRAVERSE_PRE, func, arg);
382 	if (err)
383 		return (err);
384 
385 	/* visit each dataset */
386 	for (obj = 1; err == 0; err = dmu_object_next(mos, &obj, FALSE, 0)) {
387 		dmu_object_info_t doi;
388 
389 		err = dmu_object_info(mos, obj, &doi);
390 		if (err)
391 			return (err);
392 
393 		if (doi.doi_type == DMU_OT_DSL_DATASET) {
394 			dsl_dataset_t *ds;
395 			rw_enter(&dp->dp_config_rwlock, RW_READER);
396 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
397 			rw_exit(&dp->dp_config_rwlock);
398 			if (err)
399 				return (err);
400 			err = traverse_dataset(ds,
401 			    ds->ds_phys->ds_prev_snap_txg, TRAVERSE_PRE,
402 			    func, arg);
403 			dsl_dataset_rele(ds, FTAG);
404 			if (err)
405 				return (err);
406 		}
407 	}
408 	if (err == ESRCH)
409 		err = 0;
410 	return (err);
411 }
412