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; |
48d6d8ff | 57 | struct udf_inode_info *iinfo; |
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 | |
48d6d8ff MS |
91 | iinfo = UDF_I(sbi->s_vat_inode); |
92 | if (iinfo->i_location.partitionReferenceNum == 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, |
48d6d8ff | 98 | iinfo->i_location.partitionReferenceNum, |
28de7948 | 99 | offset); |
1da177e4 LT |
100 | } |
101 | ||
4b11111a | 102 | inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block, |
cb00ea35 | 103 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
104 | { |
105 | return udf_get_pblock_virt15(sb, block, partition, offset); | |
106 | } | |
107 | ||
6c79e987 | 108 | uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block, |
cb00ea35 | 109 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
110 | { |
111 | int i; | |
112 | struct sparingTable *st = NULL; | |
6c79e987 MS |
113 | struct udf_sb_info *sbi = UDF_SB(sb); |
114 | struct udf_part_map *map; | |
115 | uint32_t packet; | |
4b11111a | 116 | struct udf_sparing_data *sdata; |
6c79e987 MS |
117 | |
118 | map = &sbi->s_partmaps[partition]; | |
4b11111a MS |
119 | sdata = &map->s_type_specific.s_sparing; |
120 | packet = (block + offset) & ~(sdata->s_packet_len - 1); | |
1da177e4 | 121 | |
cb00ea35 | 122 | for (i = 0; i < 4; i++) { |
4b11111a MS |
123 | if (sdata->s_spar_map[i] != NULL) { |
124 | st = (struct sparingTable *) | |
125 | sdata->s_spar_map[i]->b_data; | |
1da177e4 LT |
126 | break; |
127 | } | |
128 | } | |
129 | ||
cb00ea35 CG |
130 | if (st) { |
131 | for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) { | |
4b11111a MS |
132 | struct sparingEntry *entry = &st->mapEntry[i]; |
133 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
134 | if (origLoc >= 0xFFFFFFF0) | |
1da177e4 | 135 | break; |
4b11111a MS |
136 | else if (origLoc == packet) |
137 | return le32_to_cpu(entry->mappedLocation) + | |
138 | ((block + offset) & | |
139 | (sdata->s_packet_len - 1)); | |
140 | else if (origLoc > packet) | |
1da177e4 LT |
141 | break; |
142 | } | |
143 | } | |
28de7948 | 144 | |
6c79e987 | 145 | return map->s_partition_root + block + offset; |
1da177e4 LT |
146 | } |
147 | ||
148 | int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block) | |
149 | { | |
150 | struct udf_sparing_data *sdata; | |
151 | struct sparingTable *st = NULL; | |
152 | struct sparingEntry mapEntry; | |
153 | uint32_t packet; | |
154 | int i, j, k, l; | |
6c79e987 | 155 | struct udf_sb_info *sbi = UDF_SB(sb); |
4b11111a MS |
156 | u16 reallocationTableLen; |
157 | struct buffer_head *bh; | |
1da177e4 | 158 | |
6c79e987 MS |
159 | for (i = 0; i < sbi->s_partitions; i++) { |
160 | struct udf_part_map *map = &sbi->s_partmaps[i]; | |
161 | if (old_block > map->s_partition_root && | |
162 | old_block < map->s_partition_root + map->s_partition_len) { | |
163 | sdata = &map->s_type_specific.s_sparing; | |
4b11111a MS |
164 | packet = (old_block - map->s_partition_root) & |
165 | ~(sdata->s_packet_len - 1); | |
cb00ea35 | 166 | |
4b11111a MS |
167 | for (j = 0; j < 4; j++) |
168 | if (sdata->s_spar_map[j] != NULL) { | |
169 | st = (struct sparingTable *) | |
170 | sdata->s_spar_map[j]->b_data; | |
1da177e4 LT |
171 | break; |
172 | } | |
1da177e4 LT |
173 | |
174 | if (!st) | |
175 | return 1; | |
176 | ||
4b11111a MS |
177 | reallocationTableLen = |
178 | le16_to_cpu(st->reallocationTableLen); | |
179 | for (k = 0; k < reallocationTableLen; k++) { | |
180 | struct sparingEntry *entry = &st->mapEntry[k]; | |
181 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
182 | ||
183 | if (origLoc == 0xFFFFFFFF) { | |
cb00ea35 | 184 | for (; j < 4; j++) { |
4b11111a MS |
185 | int len; |
186 | bh = sdata->s_spar_map[j]; | |
187 | if (!bh) | |
188 | continue; | |
189 | ||
190 | st = (struct sparingTable *) | |
191 | bh->b_data; | |
192 | entry->origLocation = | |
193 | cpu_to_le32(packet); | |
194 | len = | |
195 | sizeof(struct sparingTable) + | |
196 | reallocationTableLen * | |
197 | sizeof(struct sparingEntry); | |
198 | udf_update_tag((char *)st, len); | |
199 | mark_buffer_dirty(bh); | |
1da177e4 | 200 | } |
4b11111a MS |
201 | *new_block = le32_to_cpu( |
202 | entry->mappedLocation) + | |
203 | ((old_block - | |
204 | map->s_partition_root) & | |
205 | (sdata->s_packet_len - 1)); | |
1da177e4 | 206 | return 0; |
4b11111a MS |
207 | } else if (origLoc == packet) { |
208 | *new_block = le32_to_cpu( | |
209 | entry->mappedLocation) + | |
210 | ((old_block - | |
211 | map->s_partition_root) & | |
212 | (sdata->s_packet_len - 1)); | |
1da177e4 | 213 | return 0; |
4b11111a | 214 | } else if (origLoc > packet) |
1da177e4 LT |
215 | break; |
216 | } | |
28de7948 | 217 | |
4b11111a MS |
218 | for (l = k; l < reallocationTableLen; l++) { |
219 | struct sparingEntry *entry = &st->mapEntry[l]; | |
220 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
221 | ||
222 | if (origLoc != 0xFFFFFFFF) | |
223 | continue; | |
224 | ||
225 | for (; j < 4; j++) { | |
226 | bh = sdata->s_spar_map[j]; | |
227 | if (!bh) | |
228 | continue; | |
229 | ||
230 | st = (struct sparingTable *)bh->b_data; | |
231 | mapEntry = st->mapEntry[l]; | |
232 | mapEntry.origLocation = | |
233 | cpu_to_le32(packet); | |
234 | memmove(&st->mapEntry[k + 1], | |
235 | &st->mapEntry[k], | |
236 | (l - k) * | |
237 | sizeof(struct sparingEntry)); | |
238 | st->mapEntry[k] = mapEntry; | |
239 | udf_update_tag((char *)st, | |
240 | sizeof(struct sparingTable) + | |
241 | reallocationTableLen * | |
242 | sizeof(struct sparingEntry)); | |
243 | mark_buffer_dirty(bh); | |
1da177e4 | 244 | } |
4b11111a MS |
245 | *new_block = |
246 | le32_to_cpu( | |
247 | st->mapEntry[k].mappedLocation) + | |
248 | ((old_block - map->s_partition_root) & | |
249 | (sdata->s_packet_len - 1)); | |
250 | return 0; | |
1da177e4 | 251 | } |
28de7948 | 252 | |
1da177e4 | 253 | return 1; |
28de7948 | 254 | } /* if old_block */ |
1da177e4 | 255 | } |
28de7948 | 256 | |
6c79e987 | 257 | if (i == sbi->s_partitions) { |
1da177e4 LT |
258 | /* outside of partitions */ |
259 | /* for now, fail =) */ | |
260 | return 1; | |
261 | } | |
262 | ||
263 | return 0; | |
264 | } |