xref: /dragonfly/sbin/hammer/blockmap.c (revision 0cfebe3d)
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sbin/hammer/blockmap.c,v 1.1 2008/02/23 03:01:06 dillon Exp $
35  */
36 
37 #include "hammer.h"
38 
39 hammer_off_t
40 blockmap_lookup(hammer_off_t bmap_off,
41 		struct hammer_blockmap_layer1 *save_layer1,
42 		struct hammer_blockmap_layer2 *save_layer2)
43 {
44 	struct volume_info *root_volume;
45 	hammer_blockmap_t rootmap;
46 	struct hammer_blockmap_layer1 *layer1;
47 	struct hammer_blockmap_layer2 *layer2;
48 	struct buffer_info *buffer = NULL;
49 	hammer_off_t layer1_offset;
50 	hammer_off_t layer2_offset;
51 	hammer_off_t result_offset;
52 	int zone;
53 
54 	zone = HAMMER_ZONE_DECODE(bmap_off);
55 	assert(zone >= HAMMER_ZONE_BTREE_INDEX && zone < HAMMER_MAX_ZONES);
56 	assert(RootVolNo >= 0);
57 	root_volume = get_volume(RootVolNo);
58 	rootmap = &root_volume->ondisk->vol0_blockmap[zone];
59 	assert(rootmap->phys_offset != 0);
60 	assert(HAMMER_ZONE_DECODE(rootmap->phys_offset) ==
61 		 HAMMER_ZONE_RAW_BUFFER_INDEX);
62 	assert(HAMMER_ZONE_DECODE(rootmap->alloc_offset) == zone);
63 
64 	if (bmap_off >= rootmap->alloc_offset) {
65 		panic("hammer_blockmap_lookup: %016llx beyond EOF %016llx",
66 		      bmap_off, rootmap->alloc_offset);
67 		result_offset = 0;
68 		goto done;
69 	}
70 
71 	/*
72 	 * Dive layer 1.
73 	 */
74 	layer1_offset = rootmap->phys_offset +
75 			HAMMER_BLOCKMAP_LAYER1_OFFSET(bmap_off);
76 	layer1 = get_buffer_data(layer1_offset, &buffer, 0);
77 	assert(layer1->phys_offset);
78 	if (save_layer1)
79 		*save_layer1 = *layer1;
80 
81 	/*
82 	 * Dive layer 2, each entry represents a large-block.
83 	 */
84 	layer2_offset = layer1->phys_offset +
85 			HAMMER_BLOCKMAP_LAYER2_OFFSET(bmap_off);
86 	layer2 = get_buffer_data(layer2_offset, &buffer, 0);
87 	if (save_layer2)
88 		*save_layer2 = *layer2;
89 
90 	assert(layer2->u.phys_offset);
91 
92 	result_offset = layer2->u.phys_offset +
93 			(bmap_off & HAMMER_LARGEBLOCK_MASK64);
94 done:
95 	if (buffer)
96 		rel_buffer(buffer);
97 	rel_volume(root_volume);
98 	return(result_offset);
99 }
100 
101