]>
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> | |
27 | #include <linux/udf_fs.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/buffer_head.h> | |
30 | ||
cb00ea35 CG |
31 | inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block, |
32 | uint16_t partition, uint32_t offset) | |
1da177e4 | 33 | { |
6c79e987 MS |
34 | struct udf_sb_info *sbi = UDF_SB(sb); |
35 | struct udf_part_map *map; | |
36 | if (partition >= sbi->s_partitions) { | |
4b11111a MS |
37 | udf_debug("block=%d, partition=%d, offset=%d: " |
38 | "invalid partition\n", block, partition, offset); | |
1da177e4 LT |
39 | return 0xFFFFFFFF; |
40 | } | |
6c79e987 MS |
41 | map = &sbi->s_partmaps[partition]; |
42 | if (map->s_partition_func) | |
43 | return map->s_partition_func(sb, block, partition, offset); | |
1da177e4 | 44 | else |
6c79e987 | 45 | return map->s_partition_root + block + offset; |
1da177e4 LT |
46 | } |
47 | ||
28de7948 | 48 | uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block, |
cb00ea35 | 49 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
50 | { |
51 | struct buffer_head *bh = NULL; | |
52 | uint32_t newblock; | |
53 | uint32_t index; | |
54 | uint32_t loc; | |
6c79e987 MS |
55 | struct udf_sb_info *sbi = UDF_SB(sb); |
56 | struct udf_part_map *map; | |
4b11111a | 57 | struct udf_virtual_data *vdata; |
1da177e4 | 58 | |
6c79e987 | 59 | map = &sbi->s_partmaps[partition]; |
4b11111a MS |
60 | vdata = &map->s_type_specific.s_virtual; |
61 | index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t); | |
1da177e4 | 62 | |
4b11111a MS |
63 | if (block > vdata->s_num_entries) { |
64 | udf_debug("Trying to access block beyond end of VAT " | |
65 | "(%d max %d)\n", block, vdata->s_num_entries); | |
1da177e4 LT |
66 | return 0xFFFFFFFF; |
67 | } | |
68 | ||
cb00ea35 | 69 | if (block >= index) { |
1da177e4 LT |
70 | block -= index; |
71 | newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t))); | |
72 | index = block % (sb->s_blocksize / sizeof(uint32_t)); | |
cb00ea35 | 73 | } else { |
1da177e4 | 74 | newblock = 0; |
4b11111a | 75 | index = vdata->s_start_offset / sizeof(uint32_t) + block; |
1da177e4 LT |
76 | } |
77 | ||
6c79e987 | 78 | loc = udf_block_map(sbi->s_vat_inode, newblock); |
1da177e4 | 79 | |
4b11111a MS |
80 | bh = sb_bread(sb, loc); |
81 | if (!bh) { | |
1da177e4 | 82 | udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n", |
cb00ea35 | 83 | sb, block, partition, loc, index); |
1da177e4 LT |
84 | return 0xFFFFFFFF; |
85 | } | |
86 | ||
28de7948 | 87 | loc = le32_to_cpu(((__le32 *)bh->b_data)[index]); |
1da177e4 | 88 | |
3bf25cb4 | 89 | brelse(bh); |
1da177e4 | 90 | |
c0b34438 | 91 | if (UDF_I(sbi->s_vat_inode)->i_location.partitionReferenceNum == |
4b11111a | 92 | partition) { |
1da177e4 LT |
93 | udf_debug("recursive call to udf_get_pblock!\n"); |
94 | return 0xFFFFFFFF; | |
95 | } | |
96 | ||
cb00ea35 | 97 | return udf_get_pblock(sb, loc, |
c0b34438 | 98 | UDF_I(sbi->s_vat_inode)->i_location. |
4b11111a | 99 | partitionReferenceNum, |
28de7948 | 100 | offset); |
1da177e4 LT |
101 | } |
102 | ||
4b11111a | 103 | inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block, |
cb00ea35 | 104 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
105 | { |
106 | return udf_get_pblock_virt15(sb, block, partition, offset); | |
107 | } | |
108 | ||
6c79e987 | 109 | uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block, |
cb00ea35 | 110 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
111 | { |
112 | int i; | |
113 | struct sparingTable *st = NULL; | |
6c79e987 MS |
114 | struct udf_sb_info *sbi = UDF_SB(sb); |
115 | struct udf_part_map *map; | |
116 | uint32_t packet; | |
4b11111a | 117 | struct udf_sparing_data *sdata; |
6c79e987 MS |
118 | |
119 | map = &sbi->s_partmaps[partition]; | |
4b11111a MS |
120 | sdata = &map->s_type_specific.s_sparing; |
121 | packet = (block + offset) & ~(sdata->s_packet_len - 1); | |
1da177e4 | 122 | |
cb00ea35 | 123 | for (i = 0; i < 4; i++) { |
4b11111a MS |
124 | if (sdata->s_spar_map[i] != NULL) { |
125 | st = (struct sparingTable *) | |
126 | sdata->s_spar_map[i]->b_data; | |
1da177e4 LT |
127 | break; |
128 | } | |
129 | } | |
130 | ||
cb00ea35 CG |
131 | if (st) { |
132 | for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) { | |
4b11111a MS |
133 | struct sparingEntry *entry = &st->mapEntry[i]; |
134 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
135 | if (origLoc >= 0xFFFFFFF0) | |
1da177e4 | 136 | break; |
4b11111a MS |
137 | else if (origLoc == packet) |
138 | return le32_to_cpu(entry->mappedLocation) + | |
139 | ((block + offset) & | |
140 | (sdata->s_packet_len - 1)); | |
141 | else if (origLoc > packet) | |
1da177e4 LT |
142 | break; |
143 | } | |
144 | } | |
28de7948 | 145 | |
6c79e987 | 146 | return map->s_partition_root + block + offset; |
1da177e4 LT |
147 | } |
148 | ||
149 | int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block) | |
150 | { | |
151 | struct udf_sparing_data *sdata; | |
152 | struct sparingTable *st = NULL; | |
153 | struct sparingEntry mapEntry; | |
154 | uint32_t packet; | |
155 | int i, j, k, l; | |
6c79e987 | 156 | struct udf_sb_info *sbi = UDF_SB(sb); |
4b11111a MS |
157 | u16 reallocationTableLen; |
158 | struct buffer_head *bh; | |
1da177e4 | 159 | |
6c79e987 MS |
160 | for (i = 0; i < sbi->s_partitions; i++) { |
161 | struct udf_part_map *map = &sbi->s_partmaps[i]; | |
162 | if (old_block > map->s_partition_root && | |
163 | old_block < map->s_partition_root + map->s_partition_len) { | |
164 | sdata = &map->s_type_specific.s_sparing; | |
4b11111a MS |
165 | packet = (old_block - map->s_partition_root) & |
166 | ~(sdata->s_packet_len - 1); | |
cb00ea35 | 167 | |
4b11111a MS |
168 | for (j = 0; j < 4; j++) |
169 | if (sdata->s_spar_map[j] != NULL) { | |
170 | st = (struct sparingTable *) | |
171 | sdata->s_spar_map[j]->b_data; | |
1da177e4 LT |
172 | break; |
173 | } | |
1da177e4 LT |
174 | |
175 | if (!st) | |
176 | return 1; | |
177 | ||
4b11111a MS |
178 | reallocationTableLen = |
179 | le16_to_cpu(st->reallocationTableLen); | |
180 | for (k = 0; k < reallocationTableLen; k++) { | |
181 | struct sparingEntry *entry = &st->mapEntry[k]; | |
182 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
183 | ||
184 | if (origLoc == 0xFFFFFFFF) { | |
cb00ea35 | 185 | for (; j < 4; j++) { |
4b11111a MS |
186 | int len; |
187 | bh = sdata->s_spar_map[j]; | |
188 | if (!bh) | |
189 | continue; | |
190 | ||
191 | st = (struct sparingTable *) | |
192 | bh->b_data; | |
193 | entry->origLocation = | |
194 | cpu_to_le32(packet); | |
195 | len = | |
196 | sizeof(struct sparingTable) + | |
197 | reallocationTableLen * | |
198 | sizeof(struct sparingEntry); | |
199 | udf_update_tag((char *)st, len); | |
200 | mark_buffer_dirty(bh); | |
1da177e4 | 201 | } |
4b11111a MS |
202 | *new_block = le32_to_cpu( |
203 | entry->mappedLocation) + | |
204 | ((old_block - | |
205 | map->s_partition_root) & | |
206 | (sdata->s_packet_len - 1)); | |
1da177e4 | 207 | return 0; |
4b11111a MS |
208 | } else if (origLoc == packet) { |
209 | *new_block = le32_to_cpu( | |
210 | entry->mappedLocation) + | |
211 | ((old_block - | |
212 | map->s_partition_root) & | |
213 | (sdata->s_packet_len - 1)); | |
1da177e4 | 214 | return 0; |
4b11111a | 215 | } else if (origLoc > packet) |
1da177e4 LT |
216 | break; |
217 | } | |
28de7948 | 218 | |
4b11111a MS |
219 | for (l = k; l < reallocationTableLen; l++) { |
220 | struct sparingEntry *entry = &st->mapEntry[l]; | |
221 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
222 | ||
223 | if (origLoc != 0xFFFFFFFF) | |
224 | continue; | |
225 | ||
226 | for (; j < 4; j++) { | |
227 | bh = sdata->s_spar_map[j]; | |
228 | if (!bh) | |
229 | continue; | |
230 | ||
231 | st = (struct sparingTable *)bh->b_data; | |
232 | mapEntry = st->mapEntry[l]; | |
233 | mapEntry.origLocation = | |
234 | cpu_to_le32(packet); | |
235 | memmove(&st->mapEntry[k + 1], | |
236 | &st->mapEntry[k], | |
237 | (l - k) * | |
238 | sizeof(struct sparingEntry)); | |
239 | st->mapEntry[k] = mapEntry; | |
240 | udf_update_tag((char *)st, | |
241 | sizeof(struct sparingTable) + | |
242 | reallocationTableLen * | |
243 | sizeof(struct sparingEntry)); | |
244 | mark_buffer_dirty(bh); | |
1da177e4 | 245 | } |
4b11111a MS |
246 | *new_block = |
247 | le32_to_cpu( | |
248 | st->mapEntry[k].mappedLocation) + | |
249 | ((old_block - map->s_partition_root) & | |
250 | (sdata->s_packet_len - 1)); | |
251 | return 0; | |
1da177e4 | 252 | } |
28de7948 | 253 | |
1da177e4 | 254 | return 1; |
28de7948 | 255 | } /* if old_block */ |
1da177e4 | 256 | } |
28de7948 | 257 | |
6c79e987 | 258 | if (i == sbi->s_partitions) { |
1da177e4 LT |
259 | /* outside of partitions */ |
260 | /* for now, fail =) */ | |
261 | return 1; | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } |