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