d61bfa535934428a5f4731d84fdcffcf043e7f7b
[dragonfly.git] / sbin / hammer / blockmap.c
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.2 2008/06/17 04:03:38 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 hammer_off_t
40 blockmap_lookup(hammer_off_t zone_offset,
41                 struct hammer_blockmap_layer1 *save_layer1,
42                 struct hammer_blockmap_layer2 *save_layer2,
43                 int *errorp)
44 {
45         struct volume_info *root_volume = NULL;
46         hammer_blockmap_t blockmap;
47         hammer_blockmap_t freemap;
48         struct hammer_blockmap_layer1 *layer1;
49         struct hammer_blockmap_layer2 *layer2;
50         struct buffer_info *buffer = NULL;
51         hammer_off_t layer1_offset;
52         hammer_off_t layer2_offset;
53         hammer_off_t result_offset;
54         int zone;
55         int i;
56         int error = 0;
57
58         zone = HAMMER_ZONE_DECODE(zone_offset);
59
60         if (AssertOnFailure) {
61                 assert(zone > HAMMER_ZONE_RAW_VOLUME_INDEX);
62                 assert(zone < HAMMER_MAX_ZONES);
63                 assert(RootVolNo >= 0);
64         } else {
65                 if (zone <= HAMMER_ZONE_RAW_VOLUME_INDEX)
66                         error = EDOM;
67                 if (zone >= HAMMER_MAX_ZONES)
68                         error = EDOM;
69                 if (RootVolNo < 0)
70                         error = EDOM;
71                 if (error) {
72                         result_offset = HAMMER_OFF_BAD;
73                         goto done;
74                 }
75         }
76
77         root_volume = get_volume(RootVolNo);
78         blockmap = &root_volume->ondisk->vol0_blockmap[zone];
79
80         if (zone == HAMMER_ZONE_RAW_BUFFER_INDEX) {
81                 result_offset = zone_offset;
82         } else if (zone == HAMMER_ZONE_UNDO_INDEX) {
83                 i = (zone_offset & HAMMER_OFF_SHORT_MASK) /
84                     HAMMER_LARGEBLOCK_SIZE;
85                 if (AssertOnFailure) {
86                         assert(zone_offset < blockmap->alloc_offset);
87                 } else {
88                         if (zone_offset >= blockmap->alloc_offset) {
89                                 error = EDOM;
90                                 result_offset = HAMMER_OFF_BAD;
91                                 goto done;
92                         }
93                 }
94                 result_offset = root_volume->ondisk->vol0_undo_array[i] +
95                                 (zone_offset & HAMMER_LARGEBLOCK_MASK64);
96         } else {
97                 result_offset = (zone_offset & ~HAMMER_OFF_ZONE_MASK) |
98                                 HAMMER_ZONE_RAW_BUFFER;
99         }
100
101         /*
102          * The blockmap should match the requested zone (else the volume
103          * header is mashed).
104          */
105         if (AssertOnFailure) {
106                 assert(HAMMER_ZONE_DECODE(blockmap->alloc_offset) == zone);
107         } else {
108                 if (HAMMER_ZONE_DECODE(blockmap->alloc_offset) != zone) {
109                         error = EDOM;
110                         goto done;
111                 }
112         }
113
114         /*
115          * Validate that the big-block is assigned to the zone.  Also
116          * assign save_layer{1,2}.
117          */
118
119         freemap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
120         /*
121          * Dive layer 1.
122          */
123         layer1_offset = freemap->phys_offset +
124                         HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
125         layer1 = get_buffer_data(layer1_offset, &buffer, 0);
126         if (AssertOnFailure) {
127                 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
128         } else {
129                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
130                         error = EDOM;
131                         goto done;
132                 }
133         }
134         if (save_layer1)
135                 *save_layer1 = *layer1;
136
137         /*
138          * Dive layer 2, each entry represents a large-block.
139          */
140         layer2_offset = layer1->phys_offset +
141                         HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
142         layer2 = get_buffer_data(layer2_offset, &buffer, 0);
143         if (save_layer2)
144                 *save_layer2 = *layer2;
145
146         if (AssertOnFailure) {
147                 assert(layer2->zone == zone);
148         } else {
149                 if (layer2->zone != zone)
150                         error = EDOM;
151         }
152
153 done:
154         if (buffer)
155                 rel_buffer(buffer);
156         if (root_volume)
157                 rel_volume(root_volume);
158
159         if (errorp)
160                 *errorp = error;
161
162         return(result_offset);
163 }
164