]>
Commit | Line | Data |
---|---|---|
5ce34554 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * partition.c | |
4 | * | |
5 | * PURPOSE | |
6 | * Partition handling routines for the OSTA-UDF(tm) filesystem. | |
7 | * | |
1da177e4 | 8 | * COPYRIGHT |
1da177e4 LT |
9 | * (C) 1998-2001 Ben Fennema |
10 | * | |
11 | * HISTORY | |
12 | * | |
28de7948 | 13 | * 12/06/98 blf Created file. |
1da177e4 LT |
14 | * |
15 | */ | |
16 | ||
17 | #include "udfdecl.h" | |
18 | #include "udf_sb.h" | |
19 | #include "udf_i.h" | |
20 | ||
21 | #include <linux/fs.h> | |
22 | #include <linux/string.h> | |
7db09be6 | 23 | #include <linux/mutex.h> |
1da177e4 | 24 | |
22ba0317 AB |
25 | uint32_t udf_get_pblock(struct super_block *sb, uint32_t block, |
26 | uint16_t partition, uint32_t offset) | |
1da177e4 | 27 | { |
6c79e987 MS |
28 | struct udf_sb_info *sbi = UDF_SB(sb); |
29 | struct udf_part_map *map; | |
30 | if (partition >= sbi->s_partitions) { | |
fcbf7637 | 31 | udf_debug("block=%u, partition=%u, offset=%u: invalid partition\n", |
a983f368 | 32 | block, partition, offset); |
1da177e4 LT |
33 | return 0xFFFFFFFF; |
34 | } | |
6c79e987 MS |
35 | map = &sbi->s_partmaps[partition]; |
36 | if (map->s_partition_func) | |
37 | return map->s_partition_func(sb, block, partition, offset); | |
1da177e4 | 38 | else |
6c79e987 | 39 | return map->s_partition_root + block + offset; |
1da177e4 LT |
40 | } |
41 | ||
28de7948 | 42 | uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block, |
cb00ea35 | 43 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
44 | { |
45 | struct buffer_head *bh = NULL; | |
46 | uint32_t newblock; | |
47 | uint32_t index; | |
48 | uint32_t loc; | |
6c79e987 MS |
49 | struct udf_sb_info *sbi = UDF_SB(sb); |
50 | struct udf_part_map *map; | |
4b11111a | 51 | struct udf_virtual_data *vdata; |
fa5e0815 | 52 | struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode); |
4215db46 | 53 | int err; |
1da177e4 | 54 | |
6c79e987 | 55 | map = &sbi->s_partmaps[partition]; |
4b11111a | 56 | vdata = &map->s_type_specific.s_virtual; |
1da177e4 | 57 | |
4b11111a | 58 | if (block > vdata->s_num_entries) { |
fcbf7637 | 59 | udf_debug("Trying to access block beyond end of VAT (%u max %u)\n", |
a983f368 | 60 | block, vdata->s_num_entries); |
1da177e4 LT |
61 | return 0xFFFFFFFF; |
62 | } | |
63 | ||
fa5e0815 | 64 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { |
382a2287 | 65 | loc = le32_to_cpu(((__le32 *)(iinfo->i_data + |
47c9358a | 66 | vdata->s_start_offset))[block]); |
fa5e0815 JK |
67 | goto translate; |
68 | } | |
69 | index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t); | |
cb00ea35 | 70 | if (block >= index) { |
1da177e4 LT |
71 | block -= index; |
72 | newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t))); | |
73 | index = block % (sb->s_blocksize / sizeof(uint32_t)); | |
cb00ea35 | 74 | } else { |
1da177e4 | 75 | newblock = 0; |
4b11111a | 76 | index = vdata->s_start_offset / sizeof(uint32_t) + block; |
1da177e4 LT |
77 | } |
78 | ||
4215db46 | 79 | bh = udf_bread(sbi->s_vat_inode, newblock, 0, &err); |
4b11111a | 80 | if (!bh) { |
f8d0dd0b TR |
81 | udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%u,%u)\n", |
82 | sb, block, partition); | |
1da177e4 LT |
83 | return 0xFFFFFFFF; |
84 | } | |
85 | ||
28de7948 | 86 | loc = le32_to_cpu(((__le32 *)bh->b_data)[index]); |
1da177e4 | 87 | |
3bf25cb4 | 88 | brelse(bh); |
1da177e4 | 89 | |
fa5e0815 | 90 | translate: |
48d6d8ff | 91 | if (iinfo->i_location.partitionReferenceNum == partition) { |
1da177e4 LT |
92 | udf_debug("recursive call to udf_get_pblock!\n"); |
93 | return 0xFFFFFFFF; | |
94 | } | |
95 | ||
cb00ea35 | 96 | return udf_get_pblock(sb, loc, |
48d6d8ff | 97 | iinfo->i_location.partitionReferenceNum, |
28de7948 | 98 | offset); |
1da177e4 LT |
99 | } |
100 | ||
4b11111a | 101 | inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block, |
cb00ea35 | 102 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
103 | { |
104 | return udf_get_pblock_virt15(sb, block, partition, offset); | |
105 | } | |
106 | ||
6c79e987 | 107 | uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block, |
cb00ea35 | 108 | uint16_t partition, uint32_t offset) |
1da177e4 LT |
109 | { |
110 | int i; | |
111 | struct sparingTable *st = NULL; | |
6c79e987 MS |
112 | struct udf_sb_info *sbi = UDF_SB(sb); |
113 | struct udf_part_map *map; | |
114 | uint32_t packet; | |
4b11111a | 115 | struct udf_sparing_data *sdata; |
6c79e987 MS |
116 | |
117 | map = &sbi->s_partmaps[partition]; | |
4b11111a MS |
118 | sdata = &map->s_type_specific.s_sparing; |
119 | packet = (block + offset) & ~(sdata->s_packet_len - 1); | |
1da177e4 | 120 | |
cb00ea35 | 121 | for (i = 0; i < 4; i++) { |
4b11111a MS |
122 | if (sdata->s_spar_map[i] != NULL) { |
123 | st = (struct sparingTable *) | |
124 | sdata->s_spar_map[i]->b_data; | |
1da177e4 LT |
125 | break; |
126 | } | |
127 | } | |
128 | ||
cb00ea35 CG |
129 | if (st) { |
130 | for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) { | |
4b11111a MS |
131 | struct sparingEntry *entry = &st->mapEntry[i]; |
132 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
133 | if (origLoc >= 0xFFFFFFF0) | |
1da177e4 | 134 | break; |
4b11111a MS |
135 | else if (origLoc == packet) |
136 | return le32_to_cpu(entry->mappedLocation) + | |
137 | ((block + offset) & | |
138 | (sdata->s_packet_len - 1)); | |
139 | else if (origLoc > packet) | |
1da177e4 LT |
140 | break; |
141 | } | |
142 | } | |
28de7948 | 143 | |
6c79e987 | 144 | return map->s_partition_root + block + offset; |
1da177e4 LT |
145 | } |
146 | ||
147 | int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block) | |
148 | { | |
149 | struct udf_sparing_data *sdata; | |
150 | struct sparingTable *st = NULL; | |
151 | struct sparingEntry mapEntry; | |
152 | uint32_t packet; | |
153 | int i, j, k, l; | |
6c79e987 | 154 | struct udf_sb_info *sbi = UDF_SB(sb); |
4b11111a MS |
155 | u16 reallocationTableLen; |
156 | struct buffer_head *bh; | |
7db09be6 | 157 | int ret = 0; |
1da177e4 | 158 | |
7db09be6 | 159 | mutex_lock(&sbi->s_alloc_mutex); |
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 | 174 | |
7db09be6 AIB |
175 | if (!st) { |
176 | ret = 1; | |
177 | goto out; | |
178 | } | |
1da177e4 | 179 | |
4b11111a MS |
180 | reallocationTableLen = |
181 | le16_to_cpu(st->reallocationTableLen); | |
182 | for (k = 0; k < reallocationTableLen; k++) { | |
183 | struct sparingEntry *entry = &st->mapEntry[k]; | |
184 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
185 | ||
186 | if (origLoc == 0xFFFFFFFF) { | |
cb00ea35 | 187 | for (; j < 4; j++) { |
4b11111a MS |
188 | int len; |
189 | bh = sdata->s_spar_map[j]; | |
190 | if (!bh) | |
191 | continue; | |
192 | ||
193 | st = (struct sparingTable *) | |
194 | bh->b_data; | |
195 | entry->origLocation = | |
196 | cpu_to_le32(packet); | |
197 | len = | |
198 | sizeof(struct sparingTable) + | |
199 | reallocationTableLen * | |
200 | sizeof(struct sparingEntry); | |
201 | udf_update_tag((char *)st, len); | |
202 | mark_buffer_dirty(bh); | |
1da177e4 | 203 | } |
4b11111a MS |
204 | *new_block = le32_to_cpu( |
205 | entry->mappedLocation) + | |
206 | ((old_block - | |
207 | map->s_partition_root) & | |
208 | (sdata->s_packet_len - 1)); | |
7db09be6 AIB |
209 | ret = 0; |
210 | goto out; | |
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)); | |
7db09be6 AIB |
217 | ret = 0; |
218 | goto out; | |
4b11111a | 219 | } else if (origLoc > packet) |
1da177e4 LT |
220 | break; |
221 | } | |
28de7948 | 222 | |
4b11111a MS |
223 | for (l = k; l < reallocationTableLen; l++) { |
224 | struct sparingEntry *entry = &st->mapEntry[l]; | |
225 | u32 origLoc = le32_to_cpu(entry->origLocation); | |
226 | ||
227 | if (origLoc != 0xFFFFFFFF) | |
228 | continue; | |
229 | ||
230 | for (; j < 4; j++) { | |
231 | bh = sdata->s_spar_map[j]; | |
232 | if (!bh) | |
233 | continue; | |
234 | ||
235 | st = (struct sparingTable *)bh->b_data; | |
236 | mapEntry = st->mapEntry[l]; | |
237 | mapEntry.origLocation = | |
238 | cpu_to_le32(packet); | |
239 | memmove(&st->mapEntry[k + 1], | |
240 | &st->mapEntry[k], | |
241 | (l - k) * | |
242 | sizeof(struct sparingEntry)); | |
243 | st->mapEntry[k] = mapEntry; | |
244 | udf_update_tag((char *)st, | |
245 | sizeof(struct sparingTable) + | |
246 | reallocationTableLen * | |
247 | sizeof(struct sparingEntry)); | |
248 | mark_buffer_dirty(bh); | |
1da177e4 | 249 | } |
4b11111a MS |
250 | *new_block = |
251 | le32_to_cpu( | |
252 | st->mapEntry[k].mappedLocation) + | |
253 | ((old_block - map->s_partition_root) & | |
254 | (sdata->s_packet_len - 1)); | |
7db09be6 AIB |
255 | ret = 0; |
256 | goto out; | |
1da177e4 | 257 | } |
28de7948 | 258 | |
7db09be6 AIB |
259 | ret = 1; |
260 | goto out; | |
28de7948 | 261 | } /* if old_block */ |
1da177e4 | 262 | } |
28de7948 | 263 | |
6c79e987 | 264 | if (i == sbi->s_partitions) { |
1da177e4 LT |
265 | /* outside of partitions */ |
266 | /* for now, fail =) */ | |
7db09be6 | 267 | ret = 1; |
1da177e4 LT |
268 | } |
269 | ||
7db09be6 AIB |
270 | out: |
271 | mutex_unlock(&sbi->s_alloc_mutex); | |
272 | return ret; | |
1da177e4 | 273 | } |
bfb257a5 JK |
274 | |
275 | static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block, | |
276 | uint16_t partition, uint32_t offset) | |
277 | { | |
278 | struct super_block *sb = inode->i_sb; | |
279 | struct udf_part_map *map; | |
5ca4e4be | 280 | struct kernel_lb_addr eloc; |
bfb257a5 JK |
281 | uint32_t elen; |
282 | sector_t ext_offset; | |
283 | struct extent_position epos = {}; | |
284 | uint32_t phyblock; | |
285 | ||
286 | if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) != | |
287 | (EXT_RECORDED_ALLOCATED >> 30)) | |
288 | phyblock = 0xFFFFFFFF; | |
289 | else { | |
290 | map = &UDF_SB(sb)->s_partmaps[partition]; | |
291 | /* map to sparable/physical partition desc */ | |
292 | phyblock = udf_get_pblock(sb, eloc.logicalBlockNum, | |
7888824b AT |
293 | map->s_type_specific.s_metadata.s_phys_partition_ref, |
294 | ext_offset + offset); | |
bfb257a5 JK |
295 | } |
296 | ||
297 | brelse(epos.bh); | |
298 | return phyblock; | |
299 | } | |
300 | ||
301 | uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block, | |
302 | uint16_t partition, uint32_t offset) | |
303 | { | |
304 | struct udf_sb_info *sbi = UDF_SB(sb); | |
305 | struct udf_part_map *map; | |
306 | struct udf_meta_data *mdata; | |
307 | uint32_t retblk; | |
308 | struct inode *inode; | |
309 | ||
310 | udf_debug("READING from METADATA\n"); | |
311 | ||
312 | map = &sbi->s_partmaps[partition]; | |
313 | mdata = &map->s_type_specific.s_metadata; | |
314 | inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe; | |
315 | ||
585d7000 AT |
316 | if (!inode) |
317 | return 0xFFFFFFFF; | |
318 | ||
bfb257a5 | 319 | retblk = udf_try_read_meta(inode, block, partition, offset); |
3080a74e | 320 | if (retblk == 0xFFFFFFFF && mdata->s_metadata_fe) { |
a40ecd7b | 321 | udf_warn(sb, "error reading from METADATA, trying to read from MIRROR\n"); |
ed47a7d0 | 322 | if (!(mdata->s_flags & MF_MIRROR_FE_LOADED)) { |
3080a74e | 323 | mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb, |
7888824b AT |
324 | mdata->s_mirror_file_loc, |
325 | mdata->s_phys_partition_ref); | |
3743a03e AT |
326 | if (IS_ERR(mdata->s_mirror_fe)) |
327 | mdata->s_mirror_fe = NULL; | |
ed47a7d0 | 328 | mdata->s_flags |= MF_MIRROR_FE_LOADED; |
3080a74e NJ |
329 | } |
330 | ||
bfb257a5 JK |
331 | inode = mdata->s_mirror_fe; |
332 | if (!inode) | |
333 | return 0xFFFFFFFF; | |
334 | retblk = udf_try_read_meta(inode, block, partition, offset); | |
335 | } | |
336 | ||
337 | return retblk; | |
338 | } |