kernel - Consolidate kern_utimes() into kern_utimensat()
[dragonfly.git] / sbin / hammer / blockmap.c
blobd64d9eb46b36d2d859792c0898d18912711f67bc
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
34 * $DragonFly: src/sbin/hammer/blockmap.c,v 1.2 2008/06/17 04:03:38 dillon Exp $
37 #include "hammer.h"
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)
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 *buffer1 = NULL;
51 struct buffer_info *buffer2 = NULL;
52 hammer_off_t layer1_offset;
53 hammer_off_t layer2_offset;
54 hammer_off_t result_offset;
55 int zone;
56 int i;
57 int error = 0;
59 if (save_layer1)
60 bzero(save_layer1, sizeof(*save_layer1));
61 if (save_layer2)
62 bzero(save_layer2, sizeof(*save_layer2));
64 zone = HAMMER_ZONE_DECODE(zone_offset);
66 if (AssertOnFailure) {
67 assert(zone > HAMMER_ZONE_RAW_VOLUME_INDEX);
68 assert(zone < HAMMER_MAX_ZONES);
69 assert(RootVolNo >= 0);
70 } else {
71 if (zone <= HAMMER_ZONE_RAW_VOLUME_INDEX)
72 error = -1;
73 if (zone >= HAMMER_MAX_ZONES)
74 error = -2;
75 if (RootVolNo < 0)
76 error = -3;
77 if (error) {
78 result_offset = HAMMER_OFF_BAD;
79 goto done;
83 root_volume = get_volume(RootVolNo);
84 blockmap = &root_volume->ondisk->vol0_blockmap[zone];
86 if (zone == HAMMER_ZONE_RAW_BUFFER_INDEX) {
87 result_offset = zone_offset;
88 } else if (zone == HAMMER_ZONE_UNDO_INDEX) {
89 i = (zone_offset & HAMMER_OFF_SHORT_MASK) /
90 HAMMER_BIGBLOCK_SIZE;
91 if (AssertOnFailure) {
92 assert(zone_offset < blockmap->alloc_offset);
93 } else {
94 if (zone_offset >= blockmap->alloc_offset) {
95 error = -4;
96 result_offset = HAMMER_OFF_BAD;
97 goto done;
100 result_offset = root_volume->ondisk->vol0_undo_array[i] +
101 (zone_offset & HAMMER_BIGBLOCK_MASK64);
102 } else {
103 result_offset = hammer_xlate_to_zone2(zone_offset);
107 * The blockmap should match the requested zone (else the volume
108 * header is mashed).
110 * Note that a valid offset can still be returned if AssertOnFailure
111 * is zero.
113 if (AssertOnFailure) {
114 assert(HAMMER_ZONE_DECODE(blockmap->alloc_offset) == zone);
115 } else {
116 if (HAMMER_ZONE_DECODE(blockmap->alloc_offset) != zone) {
117 error = -5;
118 goto done;
123 * Validate that the big-block is assigned to the zone. Also
124 * assign save_layer{1,2}.
127 freemap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
129 * Dive layer 1.
131 layer1_offset = freemap->phys_offset +
132 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
133 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
134 if (AssertOnFailure) {
135 assert(layer1);
136 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
137 } else {
138 if (layer1 == NULL) {
139 error = -6;
140 goto done;
142 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
143 error = -7;
144 goto done;
147 if (save_layer1)
148 *save_layer1 = *layer1;
151 * Dive layer 2, each entry represents a big-block.
153 layer2_offset = layer1->phys_offset +
154 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
155 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
157 if (AssertOnFailure) {
158 assert(layer2);
159 assert(layer2->zone == zone);
160 } else {
161 if (layer2 == NULL) {
162 error = -8;
163 goto done;
165 if (layer2->zone != zone) {
166 error = -9;
167 goto done;
170 if (save_layer2)
171 *save_layer2 = *layer2;
173 done:
174 rel_buffer(buffer1);
175 rel_buffer(buffer2);
176 rel_volume(root_volume);
178 if (errorp)
179 *errorp = error;
181 return(result_offset);