]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * partition.c | |
3 | * | |
4 | * PURPOSE | |
5 | * Partition handling routines for the OSTA-UDF(tm) filesystem. | |
6 | * | |
1da177e4 LT |
7 | * COPYRIGHT |
8 | * This file is distributed under the terms of the GNU General Public | |
9 | * License (GPL). Copies of the GPL can be obtained from: | |
10 | * ftp://prep.ai.mit.edu/pub/gnu/GPL | |
11 | * Each contributing author retains all rights to their own work. | |
12 | * | |
13 | * (C) 1998-2001 Ben Fennema | |
14 | * | |
15 | * HISTORY | |
16 | * | |
28de7948 | 17 | * 12/06/98 blf Created file. |
1da177e4 LT |
18 | * |
19 | */ | |
20 | ||
21 | #include "udfdecl.h" | |
22 | #include "udf_sb.h" | |
23 | #include "udf_i.h" | |
24 | ||
25 | #include <linux/fs.h> | |
26 | #include <linux/string.h> | |
1da177e4 LT |
27 | #include <linux/slab.h> |
28 | #include <linux/buffer_head.h> | |
29 | ||
cb00ea35 CG |
30 | inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block, |
31 | uint16_t partition, uint32_t offset) | |
1da177e4 | 32 | { |
6c79e987 MS |
33 | struct udf_sb_info *sbi = UDF_SB(sb); |
34 | struct udf_part_map *map; | |
35 | if (partition >= sbi->s_partitions) { | |
4b11111a MS |
36 | udf_debug("block=%d, partition=%d, offset=%d: " |
37 | "invalid partition\n", block, partition, offset); | |
1da177e4 LT |
38 | return 0xFFFFFFFF; |
39 | } | |
6c79e987 MS |
40 | map = &sbi->s_partmaps[partition]; |
41 | if (map->s_partition_func) | |
42 | return map->s_partition_func(sb, block, partition, offset); | |
1da177e4 | 43 | else |
6c79e987 | 44 | return map->s_partition_root + block + offset; |
1da177e4 LT |
45 | } |
46 | ||
28de7948 | 47 | uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block, |
cb00ea35 | 48 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
49 | { |
50 | struct buffer_head *bh = NULL; | |
51 | uint32_t newblock; | |
52 | uint32_t index; | |
53 | uint32_t loc; | |
6c79e987 MS |
54 | struct udf_sb_info *sbi = UDF_SB(sb); |
55 | struct udf_part_map *map; | |
4b11111a | 56 | struct udf_virtual_data *vdata; |
fa5e0815 | 57 | struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode); |
1da177e4 | 58 | |
6c79e987 | 59 | map = &sbi->s_partmaps[partition]; |
4b11111a | 60 | vdata = &map->s_type_specific.s_virtual; |
1da177e4 | 61 | |
4b11111a MS |
62 | if (block > vdata->s_num_entries) { |
63 | udf_debug("Trying to access block beyond end of VAT " | |
64 | "(%d max %d)\n", block, vdata->s_num_entries); | |
1da177e4 LT |
65 | return 0xFFFFFFFF; |
66 | } | |
67 | ||
fa5e0815 JK |
68 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { |
69 | loc = le32_to_cpu(((__le32 *)iinfo->i_ext.i_data)[block]); | |
70 | goto translate; | |
71 | } | |
72 | index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t); | |
cb00ea35 | 73 | if (block >= index) { |
1da177e4 LT |
74 | block -= index; |
75 | newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t))); | |
76 | index = block % (sb->s_blocksize / sizeof(uint32_t)); | |
cb00ea35 | 77 | } else { |
1da177e4 | 78 | newblock = 0; |
4b11111a | 79 | index = vdata->s_start_offset / sizeof(uint32_t) + block; |
1da177e4 LT |
80 | } |
81 | ||
6c79e987 | 82 | loc = udf_block_map(sbi->s_vat_inode, newblock); |
1da177e4 | 83 | |
4b11111a MS |
84 | bh = sb_bread(sb, loc); |
85 | if (!bh) { | |
1da177e4 | 86 | udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n", |
cb00ea35 | 87 | sb, block, partition, loc, index); |
1da177e4 LT |
88 | return 0xFFFFFFFF; |
89 | } | |
90 | ||
28de7948 | 91 | loc = le32_to_cpu(((__le32 *)bh->b_data)[index]); |
1da177e4 | 92 | |
3bf25cb4 | 93 | brelse(bh); |
1da177e4 | 94 | |
fa5e0815 | 95 | translate: |
48d6d8ff | 96 | if (iinfo->i_location.partitionReferenceNum == partition) { |
1da177e4 LT |
97 | udf_debug("recursive call to udf_get_pblock!\n"); |
98 | return 0xFFFFFFFF; | |
99 | } | |
100 | ||
cb00ea35 | 101 | return udf_get_pblock(sb, loc, |
48d6d8ff | 102 | iinfo->i_location.partitionReferenceNum, |
28de7948 | 103 | offset); |
1da177e4 LT |
104 | } |
105 | ||
4b11111a | 106 | inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block, |
cb00ea35 | 107 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
108 | { |
109 | return udf_get_pblock_virt15(sb, block, partition, offset); | |
110 | } | |
111 | ||
6c79e987 | 112 | uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block, |
cb00ea35 | 113 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
114 | { |
115 | int i; | |
116 | struct sparingTable *st = NULL; | |
6c79e987 MS |
117 | struct udf_sb_info *sbi = UDF_SB(sb); |
118 | struct udf_part_map *map; | |
119 | uint32_t packet; | |
4b11111a | 120 | struct udf_sparing_data *sdata; |
6c79e987 MS |
121 | |
122 | map = &sbi->s_partmaps[partition]; | |
4b11111a MS |
123 | sdata = &map->s_type_specific.s_sparing; |
124 | packet = (block + offset) & ~(sdata->s_packet_len - 1); | |
1da177e4 | 125 | |
cb00ea35 | 126 | for (i = 0; i < 4; i++) { |
4b11111a MS |
127 | if (sdata->s_spar_map[i] != NULL) { |
128 | st = (struct sparingTable *) | |
129 | sdata->s_spar_map[i]->b_data; | |
1da177e4 LT |
130 | break; |
131 | } | |
132 | } | |
133 | ||
cb00ea35 CG |
134 | if (st) { |
135 | for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) { | |
4b11111a MS |
136 | struct sparingEntry *entry = &st->mapEntry[i]; |
137 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
138 | if (origLoc >= 0xFFFFFFF0) | |
1da177e4 | 139 | break; |
4b11111a MS |
140 | else if (origLoc == packet) |
141 | return le32_to_cpu(entry->mappedLocation) + | |
142 | ((block + offset) & | |
143 | (sdata->s_packet_len - 1)); | |
144 | else if (origLoc > packet) | |
1da177e4 LT |
145 | break; |
146 | } | |
147 | } | |
28de7948 | 148 | |
6c79e987 | 149 | return map->s_partition_root + block + offset; |
1da177e4 LT |
150 | } |
151 | ||
152 | int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block) | |
153 | { | |
154 | struct udf_sparing_data *sdata; | |
155 | struct sparingTable *st = NULL; | |
156 | struct sparingEntry mapEntry; | |
157 | uint32_t packet; | |
158 | int i, j, k, l; | |
6c79e987 | 159 | struct udf_sb_info *sbi = UDF_SB(sb); |
4b11111a MS |
160 | u16 reallocationTableLen; |
161 | struct buffer_head *bh; | |
1da177e4 | 162 | |
6c79e987 MS |
163 | for (i = 0; i < sbi->s_partitions; i++) { |
164 | struct udf_part_map *map = &sbi->s_partmaps[i]; | |
165 | if (old_block > map->s_partition_root && | |
166 | old_block < map->s_partition_root + map->s_partition_len) { | |
167 | sdata = &map->s_type_specific.s_sparing; | |
4b11111a MS |
168 | packet = (old_block - map->s_partition_root) & |
169 | ~(sdata->s_packet_len - 1); | |
cb00ea35 | 170 | |
4b11111a MS |
171 | for (j = 0; j < 4; j++) |
172 | if (sdata->s_spar_map[j] != NULL) { | |
173 | st = (struct sparingTable *) | |
174 | sdata->s_spar_map[j]->b_data; | |
1da177e4 LT |
175 | break; |
176 | } | |
1da177e4 LT |
177 | |
178 | if (!st) | |
179 | return 1; | |
180 | ||
4b11111a MS |
181 | reallocationTableLen = |
182 | le16_to_cpu(st->reallocationTableLen); | |
183 | for (k = 0; k < reallocationTableLen; k++) { | |
184 | struct sparingEntry *entry = &st->mapEntry[k]; | |
185 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
186 | ||
187 | if (origLoc == 0xFFFFFFFF) { | |
cb00ea35 | 188 | for (; j < 4; j++) { |
4b11111a MS |
189 | int len; |
190 | bh = sdata->s_spar_map[j]; | |
191 | if (!bh) | |
192 | continue; | |
193 | ||
194 | st = (struct sparingTable *) | |
195 | bh->b_data; | |
196 | entry->origLocation = | |
197 | cpu_to_le32(packet); | |
198 | len = | |
199 | sizeof(struct sparingTable) + | |
200 | reallocationTableLen * | |
201 | sizeof(struct sparingEntry); | |
202 | udf_update_tag((char *)st, len); | |
203 | mark_buffer_dirty(bh); | |
1da177e4 | 204 | } |
4b11111a MS |
205 | *new_block = le32_to_cpu( |
206 | entry->mappedLocation) + | |
207 | ((old_block - | |
208 | map->s_partition_root) & | |
209 | (sdata->s_packet_len - 1)); | |
1da177e4 | 210 | return 0; |
4b11111a MS |
211 | } else if (origLoc == packet) { |
212 | *new_block = le32_to_cpu( | |
213 | entry->mappedLocation) + | |
214 | ((old_block - | |
215 | map->s_partition_root) & | |
216 | (sdata->s_packet_len - 1)); | |
1da177e4 | 217 | return 0; |
4b11111a | 218 | } else if (origLoc > packet) |
1da177e4 LT |
219 | break; |
220 | } | |
28de7948 | 221 | |
4b11111a MS |
222 | for (l = k; l < reallocationTableLen; l++) { |
223 | struct sparingEntry *entry = &st->mapEntry[l]; | |
224 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
225 | ||
226 | if (origLoc != 0xFFFFFFFF) | |
227 | continue; | |
228 | ||
229 | for (; j < 4; j++) { | |
230 | bh = sdata->s_spar_map[j]; | |
231 | if (!bh) | |
232 | continue; | |
233 | ||
234 | st = (struct sparingTable *)bh->b_data; | |
235 | mapEntry = st->mapEntry[l]; | |
236 | mapEntry.origLocation = | |
237 | cpu_to_le32(packet); | |
238 | memmove(&st->mapEntry[k + 1], | |
239 | &st->mapEntry[k], | |
240 | (l - k) * | |
241 | sizeof(struct sparingEntry)); | |
242 | st->mapEntry[k] = mapEntry; | |
243 | udf_update_tag((char *)st, | |
244 | sizeof(struct sparingTable) + | |
245 | reallocationTableLen * | |
246 | sizeof(struct sparingEntry)); | |
247 | mark_buffer_dirty(bh); | |
1da177e4 | 248 | } |
4b11111a MS |
249 | *new_block = |
250 | le32_to_cpu( | |
251 | st->mapEntry[k].mappedLocation) + | |
252 | ((old_block - map->s_partition_root) & | |
253 | (sdata->s_packet_len - 1)); | |
254 | return 0; | |
1da177e4 | 255 | } |
28de7948 | 256 | |
1da177e4 | 257 | return 1; |
28de7948 | 258 | } /* if old_block */ |
1da177e4 | 259 | } |
28de7948 | 260 | |
6c79e987 | 261 | if (i == sbi->s_partitions) { |
1da177e4 LT |
262 | /* outside of partitions */ |
263 | /* for now, fail =) */ | |
264 | return 1; | |
265 | } | |
266 | ||
267 | return 0; | |
268 | } |