usr/src/uts/common/fs/zfs/dmu_traverse.c
author ahrens
Mon, 31 Oct 2005 11:33:35 -0800
changeset 789 b348f31ed315
child 928 36d72fe4da29
permissions -rw-r--r--
PSARC 2002/240 ZFS 6338653 Integrate ZFS PSARC 2004/652 - DKIOCFLUSH 5096886 Write caching disks need mechanism to flush cache to physical media
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     5
 * Common Development and Distribution License, Version 1.0 only
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     6
 * (the "License").  You may not use this file except in compliance
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 * with the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <sys/zfs_context.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <sys/dmu_objset.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/dmu_traverse.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/dsl_dataset.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/dsl_dir.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <sys/dsl_pool.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <sys/dnode.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <sys/zio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <sys/dmu_impl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#define	BP_SPAN_SHIFT(level, width)	((level) * (width))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#define	BP_EQUAL(b1, b2)				\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
	(DVA_EQUAL(BP_IDENTITY(b1), BP_IDENTITY(b2)) &&	\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
	(b1)->blk_birth == (b2)->blk_birth)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
 * Compare two bookmarks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
 * For ADVANCE_PRE, the visitation order is:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
 *	objset 0, 1, 2, ..., ZB_MAXOBJSET.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
 *	object 0, 1, 2, ..., ZB_MAXOBJECT.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
 *	blkoff 0, 1, 2, ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
 *	level ZB_MAXLEVEL, ..., 2, 1, 0.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
 * where blkoff = blkid << BP_SPAN_SHIFT(level, width), and thus a valid
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
 * ordering vector is:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
 *	< objset, object, blkoff, -level >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
 * For ADVANCE_POST, the starting offsets aren't sequential but ending
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
 * offsets [blkoff = (blkid + 1) << BP_SPAN_SHIFT(level, width)] are.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
 * The visitation order is:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
 *	objset 1, 2, ..., ZB_MAXOBJSET, 0.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
 *	object 1, 2, ..., ZB_MAXOBJECT, 0.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
 *	blkoff 1, 2, ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
 *	level 0, 1, 2, ..., ZB_MAXLEVEL.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
 * and thus a valid ordering vector is:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
 *	< objset - 1, object - 1, blkoff, level >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
 * Both orderings can be expressed as:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
 *	< objset + bias, object + bias, blkoff, level ^ bias >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
 * where 'bias' is either 0 or -1 (for ADVANCE_PRE or ADVANCE_POST)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
 * and 'blkoff' is (blkid - bias) << BP_SPAN_SHIFT(level, wshift).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
 * Special case: an objset's osphys is represented as level -1 of object 0.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
 * It is always either the very first or very last block we visit in an objset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
 * Therefore, if either bookmark's level is -1, level alone determines order.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
compare_bookmark(zbookmark_t *szb, zbookmark_t *ezb, dnode_phys_t *dnp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
    int advance)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
	int bias = (advance & ADVANCE_PRE) ? 0 : -1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
	uint64_t sblkoff, eblkoff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
	int slevel, elevel, wshift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
	if (szb->zb_objset + bias < ezb->zb_objset + bias)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
	if (szb->zb_objset + bias > ezb->zb_objset + bias)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
	slevel = szb->zb_level;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
	elevel = ezb->zb_level;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
	if ((slevel | elevel) < 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
		return ((slevel ^ bias) - (elevel ^ bias));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
	if (szb->zb_object + bias < ezb->zb_object + bias)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
	if (szb->zb_object + bias > ezb->zb_object + bias)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
	if (dnp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
	wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
	sblkoff = (szb->zb_blkid - bias) << BP_SPAN_SHIFT(slevel, wshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
	eblkoff = (ezb->zb_blkid - bias) << BP_SPAN_SHIFT(elevel, wshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
	if (sblkoff < eblkoff)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
	if (sblkoff > eblkoff)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
	return ((elevel ^ bias) - (slevel ^ bias));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
#define	SET_BOOKMARK(zb, objset, object, level, blkid)	\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
{							\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
	(zb)->zb_objset = objset;			\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
	(zb)->zb_object = object;			\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
	(zb)->zb_level = level;				\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
	(zb)->zb_blkid = blkid;				\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
#define	SET_BOOKMARK_LB(zb, level, blkid)		\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
{							\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
	(zb)->zb_level = level;				\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
	(zb)->zb_blkid = blkid;				\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   143
advance_objset(zseg_t *zseg, uint64_t objset, int advance)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
	zbookmark_t *zb = &zseg->seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   147
	if (advance & ADVANCE_PRE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
		if (objset >= ZB_MAXOBJSET)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
			return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
		SET_BOOKMARK(zb, objset, 0, -1, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   152
		if (objset >= ZB_MAXOBJSET)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
			objset = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
		SET_BOOKMARK(zb, objset, 1, 0, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
	if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
		return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
	return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   162
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   163
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
advance_object(zseg_t *zseg, uint64_t object, int advance)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
	zbookmark_t *zb = &zseg->seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
	if (advance & ADVANCE_PRE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   169
		if (object >= ZB_MAXOBJECT) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
			SET_BOOKMARK(zb, zb->zb_objset + 1, 0, -1, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   172
			SET_BOOKMARK(zb, zb->zb_objset, object, ZB_MAXLEVEL, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   173
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   174
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   175
		if (zb->zb_object == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   176
			SET_BOOKMARK(zb, zb->zb_objset, 0, -1, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   177
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   178
			if (object >= ZB_MAXOBJECT)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   179
				object = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   180
			SET_BOOKMARK(zb, zb->zb_objset, object, 0, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   181
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   182
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   183
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   184
	if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   185
		return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
	return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   190
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   191
advance_from_osphys(zseg_t *zseg, int advance)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   192
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   193
	zbookmark_t *zb = &zseg->seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   194
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   195
	ASSERT(zb->zb_object == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   196
	ASSERT(zb->zb_level == -1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   197
	ASSERT(zb->zb_blkid == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
	if (advance & ADVANCE_PRE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
		SET_BOOKMARK_LB(zb, ZB_MAXLEVEL, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
		if (zb->zb_objset == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   203
			return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   204
		SET_BOOKMARK(zb, zb->zb_objset + 1, 1, 0, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   205
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   207
	if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   208
		return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   209
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   210
	return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   211
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   212
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   213
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
advance_block(zseg_t *zseg, dnode_phys_t *dnp, int rc, int advance)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   216
	zbookmark_t *zb = &zseg->seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
	int wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
	int maxlevel = dnp->dn_nlevels - 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
	int level = zb->zb_level;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
	uint64_t blkid = zb->zb_blkid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
	if (advance & ADVANCE_PRE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   223
		if (level > 0 && rc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
			level--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   225
			blkid <<= wshift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   226
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   227
			blkid++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   228
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   229
			if ((blkid << BP_SPAN_SHIFT(level, wshift)) >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
			    dnp->dn_maxblkid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
				return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
			while (level < maxlevel) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
				if (P2PHASE(blkid, 1ULL << wshift))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
				blkid >>= wshift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
				level++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
		if (level >= maxlevel || P2PHASE(blkid + 1, 1ULL << wshift)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
			blkid = (blkid + 1) << BP_SPAN_SHIFT(level, wshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
			level = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
			blkid >>= wshift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
			level++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
		while ((blkid << BP_SPAN_SHIFT(level, wshift)) >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
		    dnp->dn_maxblkid) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
			if (level == maxlevel)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
				return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
			blkid >>= wshift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
			level++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   255
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   256
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
	SET_BOOKMARK_LB(zb, level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
	if (compare_bookmark(zb, &zseg->seg_end, dnp, advance) > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
		return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
	return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
traverse_callback(traverse_handle_t *th, zseg_t *zseg, traverse_blk_cache_t *bc)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
	 * Before we issue the callback, prune against maxtxg.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
	 * We prune against mintxg before we get here because it's a big win.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	 * If a given block was born in txg 37, then we know that the entire
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
	 * subtree below that block must have been born in txg 37 or earlier.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
	 * We can therefore lop off huge branches of the tree as we go.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
	 * There's no corresponding optimization for maxtxg because knowing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
	 * that bp->blk_birth >= maxtxg doesn't imply anything about the bp's
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
	 * children.  In fact, the copy-on-write design of ZFS ensures that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
	 * top-level blocks will pretty much always be new.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
	 * Therefore, in the name of simplicity we don't prune against
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
	 * maxtxg until the last possible moment -- that being right now.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   284
	if (bc->bc_errno == 0 && bc->bc_blkptr.blk_birth >= zseg->seg_maxtxg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
	if (bc->bc_errno == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
		zbookmark_t *zb = &bc->bc_bookmark;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   289
		zbookmark_t *szb = &zseg->seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   290
		zbookmark_t *ezb = &zseg->seg_end;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   291
		zbookmark_t *lzb = &th->th_lastcb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   292
		dnode_phys_t *dnp = bc->bc_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   293
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
		 * Debugging: verify that the order we visit things
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
		 * agrees with the order defined by compare_bookmark().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
		ASSERT(compare_bookmark(zb, ezb, dnp, th->th_advance) <= 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
		ASSERT(compare_bookmark(zb, szb, dnp, th->th_advance) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
		ASSERT(compare_bookmark(lzb, zb, dnp, th->th_advance) < 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
		    lzb->zb_level == ZB_NO_LEVEL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
		*lzb = *zb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
	th->th_callbacks++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
	return (th->th_func(bc, th->th_spa, th->th_arg));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
traverse_read(traverse_handle_t *th, traverse_blk_cache_t *bc, blkptr_t *bp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
	dnode_phys_t *dnp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
	zbookmark_t *zb = &bc->bc_bookmark;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
	th->th_hits++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
	bc->bc_dnode = dnp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
	bc->bc_errno = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
	if (BP_EQUAL(&bc->bc_blkptr, bp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
	bc->bc_blkptr = *bp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
	if (bc->bc_data == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
	if (BP_IS_HOLE(bp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
		ASSERT(th->th_advance & ADVANCE_HOLES);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
	if (compare_bookmark(zb, &th->th_noread, dnp, 0) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
		error = EIO;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
	} else if (arc_tryread(th->th_spa, bp, bc->bc_data) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
		th->th_arc_hits++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   340
		error = zio_wait(zio_read(NULL, th->th_spa, bp, bc->bc_data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   341
		    BP_GET_LSIZE(bp), NULL, NULL, ZIO_PRIORITY_SYNC_READ,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   342
		    th->th_zio_flags | ZIO_FLAG_DONT_CACHE));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
		if (BP_SHOULD_BYTESWAP(bp) && error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
			(zb->zb_level > 0 ? byteswap_uint64_array :
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
			    dmu_ot[BP_GET_TYPE(bp)].ot_byteswap)(bc->bc_data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
			    BP_GET_LSIZE(bp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
		th->th_reads++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
		bc->bc_errno = error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
		error = traverse_callback(th, NULL, bc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
		ASSERT(error == EAGAIN || error == EINTR || error == ERESTART);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
		bc->bc_blkptr.blk_birth = -1ULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
	dprintf("cache %02x error %d <%llu, %llu, %d, %llx>\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
	    bc - &th->th_cache[0][0], error,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
	    zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   366
find_block(traverse_handle_t *th, zseg_t *zseg, dnode_phys_t *dnp, int depth)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
	zbookmark_t *zb = &zseg->seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   369
	traverse_blk_cache_t *bc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
	blkptr_t *bp = dnp->dn_blkptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
	int i, first, level;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
	int nbp = dnp->dn_nblkptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   373
	int minlevel = zb->zb_level;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   374
	int maxlevel = dnp->dn_nlevels - 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
	int wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   376
	int bp_shift = BP_SPAN_SHIFT(maxlevel - minlevel, wshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   377
	uint64_t blkid = zb->zb_blkid >> bp_shift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
	int do_holes = (th->th_advance & ADVANCE_HOLES) && depth == ZB_DN_CACHE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   379
	int rc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
	if (minlevel > maxlevel || blkid >= nbp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
		return (ERANGE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
	for (level = maxlevel; level >= minlevel; level--) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
		first = P2PHASE(blkid, 1ULL << wshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
		for (i = first; i < nbp; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   388
			if (bp[i].blk_birth > zseg->seg_mintxg ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
			    BP_IS_HOLE(&bp[i]) && do_holes)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   392
		if (i != first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
			i--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
			SET_BOOKMARK_LB(zb, level, blkid + (i - first));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
			return (ENOTBLK);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
		bc = &th->th_cache[depth][level];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
		SET_BOOKMARK(&bc->bc_bookmark, zb->zb_objset, zb->zb_object,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
		    level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
		if (rc = traverse_read(th, bc, bp + i, dnp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
			if (rc != EAGAIN) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
				SET_BOOKMARK_LB(zb, level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
			return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
		if (BP_IS_HOLE(&bp[i])) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
			SET_BOOKMARK_LB(zb, level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
			th->th_lastcb.zb_level = ZB_NO_LEVEL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
		nbp = 1 << wshift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
		bp = bc->bc_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
		bp_shift -= wshift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
		blkid = zb->zb_blkid >> bp_shift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   424
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   425
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   426
get_dnode(traverse_handle_t *th, uint64_t objset, dnode_phys_t *mdn,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
    uint64_t *objectp, dnode_phys_t **dnpp, uint64_t txg, int type, int depth)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
	zseg_t zseg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
	zbookmark_t *zb = &zseg.seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
	uint64_t object = *objectp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
	int i, rc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
	SET_BOOKMARK(zb, objset, 0, 0, object / DNODES_PER_BLOCK);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
	SET_BOOKMARK(&zseg.seg_end, objset, 0, 0, ZB_MAXBLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
	zseg.seg_mintxg = txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
	zseg.seg_maxtxg = -1ULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
	for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
		rc = find_block(th, &zseg, mdn, depth);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
		if (rc == EAGAIN || rc == EINTR || rc == ERANGE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
		if (rc == 0 && zb->zb_level == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
			dnode_phys_t *dnp = th->th_cache[depth][0].bc_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
			for (i = 0; i < DNODES_PER_BLOCK; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
				object = (zb->zb_blkid * DNODES_PER_BLOCK) + i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
				if (object >= *objectp &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
				    dnp[i].dn_type != DMU_OT_NONE &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
				    (type == -1 || dnp[i].dn_type == type)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
					*objectp = object;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
					*dnpp = &dnp[i];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
					return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   456
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   457
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
		rc = advance_block(&zseg, mdn, rc, ADVANCE_PRE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
		if (rc == ERANGE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
	if (rc == ERANGE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
		*objectp = ZB_MAXOBJECT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   469
	return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   472
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   473
traverse_segment(traverse_handle_t *th, zseg_t *zseg, blkptr_t *mosbp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   475
	zbookmark_t *zb = &zseg->seg_start;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
	traverse_blk_cache_t *bc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   477
	dnode_phys_t *dn, *dn_tmp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   478
	int worklimit = 1000;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   479
	int rc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   481
	dprintf("<%llu, %llu, %d, %llx>\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   482
	    zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
	bc = &th->th_cache[ZB_MOS_CACHE][ZB_MAXLEVEL - 1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
	dn = &((objset_phys_t *)bc->bc_data)->os_meta_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	SET_BOOKMARK(&bc->bc_bookmark, 0, 0, -1, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
	rc = traverse_read(th, bc, mosbp, dn);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
	if (rc)		/* If we get ERESTART, we've got nowhere left to go */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
		return (rc == ERESTART ? EINTR : rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
	ASSERT(dn->dn_nlevels < ZB_MAXLEVEL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
	if (zb->zb_objset != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
		uint64_t objset = zb->zb_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
		dsl_dataset_phys_t *dsp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
		rc = get_dnode(th, 0, dn, &objset, &dn_tmp, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
		    DMU_OT_DSL_OBJSET, ZB_MOS_CACHE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
		if (objset != zb->zb_objset)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
			rc = advance_objset(zseg, objset, th->th_advance);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
		if (rc != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
			return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
		dsp = DN_BONUS(dn_tmp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
		bc = &th->th_cache[ZB_MDN_CACHE][ZB_MAXLEVEL - 1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
		dn = &((objset_phys_t *)bc->bc_data)->os_meta_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
		SET_BOOKMARK(&bc->bc_bookmark, objset, 0, -1, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
		rc = traverse_read(th, bc, &dsp->ds_bp, dn);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
		if (rc != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
			if (rc == ERESTART)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
				rc = advance_objset(zseg, zb->zb_objset + 1,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
				    th->th_advance);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
			return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
		if (th->th_advance & ADVANCE_PRUNE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
			zseg->seg_mintxg =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
			    MAX(zseg->seg_mintxg, dsp->ds_prev_snap_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
	if (zb->zb_level == -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
		ASSERT(zb->zb_object == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
		if (bc->bc_blkptr.blk_birth > zseg->seg_mintxg) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
			rc = traverse_callback(th, zseg, bc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
			if (rc) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
				ASSERT(rc == EINTR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
				return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
		return (advance_from_osphys(zseg, th->th_advance));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
	if (zb->zb_object != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
		uint64_t object = zb->zb_object;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
		rc = get_dnode(th, zb->zb_objset, dn, &object, &dn_tmp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
		    zseg->seg_mintxg, -1, ZB_MDN_CACHE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
		if (object != zb->zb_object)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
			rc = advance_object(zseg, object, th->th_advance);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   553
		if (rc != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
			return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
		dn = dn_tmp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
	if (zb->zb_level == ZB_MAXLEVEL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
		zb->zb_level = dn->dn_nlevels - 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
	for (;;) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
		rc = find_block(th, zseg, dn, ZB_DN_CACHE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
		if (rc == EAGAIN || rc == EINTR || rc == ERANGE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
		if (rc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
			bc = &th->th_cache[ZB_DN_CACHE][zb->zb_level];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
			ASSERT(bc->bc_dnode == dn);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
			ASSERT(bc->bc_blkptr.blk_birth <= mosbp->blk_birth);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
			rc = traverse_callback(th, zseg, bc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
			if (rc) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
				ASSERT(rc == EINTR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
				return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
			if (BP_IS_HOLE(&bc->bc_blkptr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
				ASSERT(th->th_advance & ADVANCE_HOLES);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
				rc = ENOTBLK;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   580
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
		rc = advance_block(zseg, dn, rc, th->th_advance);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
		if (rc == ERANGE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
		 * Give spa_sync() a chance to run.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
		if (spa_traverse_wanted(th->th_spa)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
			th->th_syncs++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
			return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
		if (--worklimit == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
			return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
	if (rc == ERANGE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
		rc = advance_object(zseg, zb->zb_object + 1, th->th_advance);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
	return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   605
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
 * It is the caller's responsibility to ensure that the dsl_dataset_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
 * doesn't go away during traversal.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   609
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
traverse_dsl_dataset(dsl_dataset_t *ds, uint64_t txg_start, int advance,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
    blkptr_cb_t func, void *arg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
	traverse_handle_t *th;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   616
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
	th = traverse_init(spa, func, arg, advance, ZIO_FLAG_MUSTSUCCEED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
	traverse_add_objset(th, txg_start, -1ULL, ds->ds_object);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
	while ((err = traverse_more(th)) == EAGAIN)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
		continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
	traverse_fini(th);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
	return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
traverse_more(traverse_handle_t *th)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
	zseg_t *zseg = list_head(&th->th_seglist);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
	uint64_t save_txg;	/* XXX won't be necessary with real itinerary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
	krwlock_t *rw = spa_traverse_rwlock(th->th_spa);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
	blkptr_t *mosbp = spa_get_rootblkptr(th->th_spa);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
	int rc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
	if (zseg == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
	th->th_restarts++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
	save_txg = zseg->seg_mintxg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
	if (!(th->th_advance & ADVANCE_NOLOCK))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   646
		rw_enter(rw, RW_READER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   647
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   648
	rc = traverse_segment(th, zseg, mosbp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   649
	ASSERT(rc == ERANGE || rc == EAGAIN || rc == EINTR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   651
	if (!(th->th_advance & ADVANCE_NOLOCK))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   652
		rw_exit(rw);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   653
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   654
	zseg->seg_mintxg = save_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   655
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   656
	if (rc == ERANGE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
		list_remove(&th->th_seglist, zseg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
		kmem_free(zseg, sizeof (*zseg));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
		return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
	return (rc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
 * Note: (mintxg, maxtxg) is an open interval; mintxg and maxtxg themselves
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
 * are not included.  The blocks covered by this segment will all have
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
 * mintxg < birth < maxtxg.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
traverse_add_segment(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
    uint64_t sobjset, uint64_t sobject, int slevel, uint64_t sblkid,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
    uint64_t eobjset, uint64_t eobject, int elevel, uint64_t eblkid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
	zseg_t *zseg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	zseg = kmem_alloc(sizeof (zseg_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
	zseg->seg_mintxg = mintxg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
	zseg->seg_maxtxg = maxtxg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
	zseg->seg_start.zb_objset = sobjset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
	zseg->seg_start.zb_object = sobject;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
	zseg->seg_start.zb_level = slevel;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
	zseg->seg_start.zb_blkid = sblkid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
	zseg->seg_end.zb_objset = eobjset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
	zseg->seg_end.zb_object = eobject;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
	zseg->seg_end.zb_level = elevel;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
	zseg->seg_end.zb_blkid = eblkid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
	list_insert_tail(&th->th_seglist, zseg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
traverse_add_dnode(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
    uint64_t objset, uint64_t object)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   699
	if (th->th_advance & ADVANCE_PRE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
		traverse_add_segment(th, mintxg, maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
		    objset, object, ZB_MAXLEVEL, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
		    objset, object, 0, ZB_MAXBLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   704
		traverse_add_segment(th, mintxg, maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
		    objset, object, 0, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
		    objset, object, 0, ZB_MAXBLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
traverse_add_objset(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
    uint64_t objset)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
	if (th->th_advance & ADVANCE_PRE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
		traverse_add_segment(th, mintxg, maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
		    objset, 0, -1, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
		    objset, ZB_MAXOBJECT, 0, ZB_MAXBLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
		traverse_add_segment(th, mintxg, maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
		    objset, 1, 0, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
		    objset, 0, -1, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   722
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   723
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
traverse_add_pool(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
	if (th->th_advance & ADVANCE_PRE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
		traverse_add_segment(th, mintxg, maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
		    0, 0, -1, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
		    ZB_MAXOBJSET, ZB_MAXOBJECT, 0, ZB_MAXBLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
		traverse_add_segment(th, mintxg, maxtxg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
		    1, 1, 0, 0,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
		    0, 0, -1, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
traverse_handle_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
traverse_init(spa_t *spa, blkptr_cb_t func, void *arg, int advance,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
    int zio_flags)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
	traverse_handle_t *th;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
	int d, l;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   742
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
	th = kmem_zalloc(sizeof (*th), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   744
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
	th->th_spa = spa;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
	th->th_func = func;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
	th->th_arg = arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
	th->th_advance = advance;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
	th->th_lastcb.zb_level = ZB_NO_LEVEL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
	th->th_noread.zb_level = ZB_NO_LEVEL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
	th->th_zio_flags = zio_flags;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
	list_create(&th->th_seglist, sizeof (zseg_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
	    offsetof(zseg_t, seg_node));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
	for (d = 0; d < ZB_DEPTH; d++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
		for (l = 0; l < ZB_MAXLEVEL; l++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
			if ((advance & ADVANCE_DATA) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
			    l != 0 || d != ZB_DN_CACHE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
				th->th_cache[d][l].bc_data =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
				    zio_buf_alloc(SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
	return (th);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
traverse_fini(traverse_handle_t *th)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
	int d, l;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
	zseg_t *zseg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
	for (d = 0; d < ZB_DEPTH; d++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   775
		for (l = 0; l < ZB_MAXLEVEL; l++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
			if (th->th_cache[d][l].bc_data != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
				zio_buf_free(th->th_cache[d][l].bc_data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
				    SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
	while ((zseg = list_head(&th->th_seglist)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
		list_remove(&th->th_seglist, zseg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
		kmem_free(zseg, sizeof (*zseg));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
	list_destroy(&th->th_seglist);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
	dprintf("%llu hit, %llu ARC, %llu IO, %llu cb, %llu sync, %llu again\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
	    th->th_hits, th->th_arc_hits, th->th_reads, th->th_callbacks,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
	    th->th_syncs, th->th_restarts);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
	kmem_free(th, sizeof (*th));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
}