xref: /titanic_52/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision f6e214c7418f43af38bd8c3a557e3d0a1d311cfa)
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 int zfs_pd_blks_max = 100;
40 
41 typedef struct prefetch_data {
42 	kmutex_t pd_mtx;
43 	kcondvar_t pd_cv;
44 	int pd_blks_max;
45 	int pd_blks_fetched;
46 	int pd_flags;
47 	boolean_t pd_cancel;
48 	boolean_t pd_exited;
49 } prefetch_data_t;
50 
51 typedef struct traverse_data {
52 	spa_t *td_spa;
53 	uint64_t td_objset;
54 	blkptr_t *td_rootbp;
55 	uint64_t td_min_txg;
56 	int td_flags;
57 	prefetch_data_t *td_pfd;
58 	blkptr_cb_t *td_func;
59 	void *td_arg;
60 } traverse_data_t;
61 
62 static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
63     arc_buf_t *buf, uint64_t objset, uint64_t object);
64 
65 static int
66 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
67 {
68 	traverse_data_t *td = arg;
69 	zbookmark_t zb;
70 
71 	if (bp->blk_birth == 0)
72 		return (0);
73 
74 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
75 		return (0);
76 
77 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
78 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
79 
80 	(void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, td->td_arg);
81 
82 	return (0);
83 }
84 
85 static int
86 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
87 {
88 	traverse_data_t *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,
102 		    ZB_ZIL_LEVEL, 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(traverse_data_t *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(traverse_data_t *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 	prefetch_data_t *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 		dnp = &osp->os_meta_dnode;
229 		err = traverse_dnode(td, dnp, buf, zb->zb_objset,
230 		    DMU_META_DNODE_OBJECT);
231 		if (err && hard) {
232 			lasterr = err;
233 			err = 0;
234 		}
235 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
236 			dnp = &osp->os_userused_dnode;
237 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
238 			    DMU_USERUSED_OBJECT);
239 		}
240 		if (err && hard) {
241 			lasterr = err;
242 			err = 0;
243 		}
244 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
245 			dnp = &osp->os_groupused_dnode;
246 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
247 			    DMU_GROUPUSED_OBJECT);
248 		}
249 	}
250 
251 	if (buf)
252 		(void) arc_buf_remove_ref(buf, &buf);
253 
254 	if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) {
255 		err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp,
256 		    td->td_arg);
257 	}
258 
259 	return (err != 0 ? err : lasterr);
260 }
261 
262 static int
263 traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
264     arc_buf_t *buf, uint64_t objset, uint64_t object)
265 {
266 	int j, err = 0, lasterr = 0;
267 	zbookmark_t czb;
268 	boolean_t hard = (td->td_flags & TRAVERSE_HARD);
269 
270 	for (j = 0; j < dnp->dn_nblkptr; j++) {
271 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
272 		err = traverse_visitbp(td, dnp, buf,
273 		    (blkptr_t *)&dnp->dn_blkptr[j], &czb);
274 		if (err) {
275 			if (!hard)
276 				break;
277 			lasterr = err;
278 		}
279 	}
280 
281 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
282 		SET_BOOKMARK(&czb, objset,
283 		    object, 0, DMU_SPILL_BLKID);
284 		err = traverse_visitbp(td, dnp, buf,
285 		    (blkptr_t *)&dnp->dn_spill, &czb);
286 		if (err) {
287 			if (!hard)
288 				return (err);
289 			lasterr = err;
290 		}
291 	}
292 	return (err != 0 ? err : lasterr);
293 }
294 
295 /* ARGSUSED */
296 static int
297 traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
298     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp,
299     void *arg)
300 {
301 	prefetch_data_t *pfd = arg;
302 	uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
303 
304 	ASSERT(pfd->pd_blks_fetched >= 0);
305 	if (pfd->pd_cancel)
306 		return (EINTR);
307 
308 	if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
309 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) ||
310 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
311 		return (0);
312 
313 	mutex_enter(&pfd->pd_mtx);
314 	while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max)
315 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
316 	pfd->pd_blks_fetched++;
317 	cv_broadcast(&pfd->pd_cv);
318 	mutex_exit(&pfd->pd_mtx);
319 
320 	(void) dsl_read(NULL, spa, bp, pbuf, NULL, NULL,
321 	    ZIO_PRIORITY_ASYNC_READ,
322 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
323 	    &aflags, zb);
324 
325 	return (0);
326 }
327 
328 static void
329 traverse_prefetch_thread(void *arg)
330 {
331 	traverse_data_t *td_main = arg;
332 	traverse_data_t td = *td_main;
333 	zbookmark_t czb;
334 
335 	td.td_func = traverse_prefetcher;
336 	td.td_arg = td_main->td_pfd;
337 	td.td_pfd = NULL;
338 
339 	SET_BOOKMARK(&czb, td.td_objset,
340 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
341 	(void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb);
342 
343 	mutex_enter(&td_main->td_pfd->pd_mtx);
344 	td_main->td_pfd->pd_exited = B_TRUE;
345 	cv_broadcast(&td_main->td_pfd->pd_cv);
346 	mutex_exit(&td_main->td_pfd->pd_mtx);
347 }
348 
349 /*
350  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
351  * in syncing context).
352  */
353 static int
354 traverse_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *rootbp,
355     uint64_t txg_start, int flags, blkptr_cb_t func, void *arg)
356 {
357 	traverse_data_t td;
358 	prefetch_data_t pd = { 0 };
359 	zbookmark_t czb;
360 	int err;
361 
362 	td.td_spa = spa;
363 	td.td_objset = ds ? ds->ds_object : 0;
364 	td.td_rootbp = rootbp;
365 	td.td_min_txg = txg_start;
366 	td.td_func = func;
367 	td.td_arg = arg;
368 	td.td_pfd = &pd;
369 	td.td_flags = flags;
370 
371 	pd.pd_blks_max = zfs_pd_blks_max;
372 	pd.pd_flags = flags;
373 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
374 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
375 
376 	/* See comment on ZIL traversal in dsl_scan_visitds. */
377 	if (ds != NULL && !dsl_dataset_is_snapshot(ds)) {
378 		objset_t *os;
379 
380 		err = dmu_objset_from_ds(ds, &os);
381 		if (err)
382 			return (err);
383 
384 		traverse_zil(&td, &os->os_zil_header);
385 	}
386 
387 	if (!(flags & TRAVERSE_PREFETCH) ||
388 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
389 	    &td, TQ_NOQUEUE))
390 		pd.pd_exited = B_TRUE;
391 
392 	SET_BOOKMARK(&czb, td.td_objset,
393 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
394 	err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb);
395 
396 	mutex_enter(&pd.pd_mtx);
397 	pd.pd_cancel = B_TRUE;
398 	cv_broadcast(&pd.pd_cv);
399 	while (!pd.pd_exited)
400 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
401 	mutex_exit(&pd.pd_mtx);
402 
403 	mutex_destroy(&pd.pd_mtx);
404 	cv_destroy(&pd.pd_cv);
405 
406 	return (err);
407 }
408 
409 /*
410  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
411  * in syncing context).
412  */
413 int
414 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
415     blkptr_cb_t func, void *arg)
416 {
417 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds,
418 	    &ds->ds_phys->ds_bp, txg_start, flags, func, arg));
419 }
420 
421 /*
422  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
423  */
424 int
425 traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
426     blkptr_cb_t func, void *arg)
427 {
428 	int err, lasterr = 0;
429 	uint64_t obj;
430 	dsl_pool_t *dp = spa_get_dsl(spa);
431 	objset_t *mos = dp->dp_meta_objset;
432 	boolean_t hard = (flags & TRAVERSE_HARD);
433 
434 	/* visit the MOS */
435 	err = traverse_impl(spa, NULL, spa_get_rootblkptr(spa),
436 	    txg_start, flags, func, arg);
437 	if (err)
438 		return (err);
439 
440 	/* visit each dataset */
441 	for (obj = 1; err == 0 || (err != ESRCH && hard);
442 	    err = dmu_object_next(mos, &obj, FALSE, txg_start)) {
443 		dmu_object_info_t doi;
444 
445 		err = dmu_object_info(mos, obj, &doi);
446 		if (err) {
447 			if (!hard)
448 				return (err);
449 			lasterr = err;
450 			continue;
451 		}
452 
453 		if (doi.doi_type == DMU_OT_DSL_DATASET) {
454 			dsl_dataset_t *ds;
455 			uint64_t txg = txg_start;
456 
457 			rw_enter(&dp->dp_config_rwlock, RW_READER);
458 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
459 			rw_exit(&dp->dp_config_rwlock);
460 			if (err) {
461 				if (!hard)
462 					return (err);
463 				lasterr = err;
464 				continue;
465 			}
466 			if (ds->ds_phys->ds_prev_snap_txg > txg)
467 				txg = ds->ds_phys->ds_prev_snap_txg;
468 			err = traverse_dataset(ds, txg, flags, func, arg);
469 			dsl_dataset_rele(ds, FTAG);
470 			if (err) {
471 				if (!hard)
472 					return (err);
473 				lasterr = err;
474 			}
475 		}
476 	}
477 	if (err == ESRCH)
478 		err = 0;
479 	return (err != 0 ? err : lasterr);
480 }
481