blob: 96dfd207c3d6339f5a07377a217e5f15c01140e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * partition.c
3 *
4 * PURPOSE
5 * Partition handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070017 * 12/06/98 blf Created file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/buffer_head.h>
29
Adrian Bunk22ba0312008-04-28 18:38:49 +030030uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
31 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Marcin Slusarz6c79e982008-02-08 04:20:30 -080033 struct udf_sb_info *sbi = UDF_SB(sb);
34 struct udf_part_map *map;
35 if (partition >= sbi->s_partitions) {
Marcin Slusarz4b111112008-02-08 04:20:36 -080036 udf_debug("block=%d, partition=%d, offset=%d: "
37 "invalid partition\n", block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return 0xFFFFFFFF;
39 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -080040 map = &sbi->s_partmaps[partition];
41 if (map->s_partition_func)
42 return map->s_partition_func(sb, block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -080044 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070047uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070048 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct buffer_head *bh = NULL;
51 uint32_t newblock;
52 uint32_t index;
53 uint32_t loc;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080054 struct udf_sb_info *sbi = UDF_SB(sb);
55 struct udf_part_map *map;
Marcin Slusarz4b111112008-02-08 04:20:36 -080056 struct udf_virtual_data *vdata;
Jan Karafa5e0812008-04-08 02:08:53 +020057 struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Marcin Slusarz6c79e982008-02-08 04:20:30 -080059 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -080060 vdata = &map->s_type_specific.s_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Marcin Slusarz4b111112008-02-08 04:20:36 -080062 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0xFFFFFFFF;
66 }
67
Jan Karafa5e0812008-04-08 02:08:53 +020068 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +020069 loc = le32_to_cpu(((__le32 *)(iinfo->i_ext.i_data +
70 vdata->s_start_offset))[block]);
Jan Karafa5e0812008-04-08 02:08:53 +020071 goto translate;
72 }
73 index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070074 if (block >= index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 block -= index;
76 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
77 index = block % (sb->s_blocksize / sizeof(uint32_t));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070078 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 newblock = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -080080 index = vdata->s_start_offset / sizeof(uint32_t) + block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
Marcin Slusarz6c79e982008-02-08 04:20:30 -080083 loc = udf_block_map(sbi->s_vat_inode, newblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Marcin Slusarz4b111112008-02-08 04:20:36 -080085 bh = sb_bread(sb, loc);
86 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070088 sb, block, partition, loc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return 0xFFFFFFFF;
90 }
91
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070092 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Jan Kara3bf25cb2007-05-08 00:35:16 -070094 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Jan Karafa5e0812008-04-08 02:08:53 +020096translate:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080097 if (iinfo->i_location.partitionReferenceNum == partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 udf_debug("recursive call to udf_get_pblock!\n");
99 return 0xFFFFFFFF;
100 }
101
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700102 return udf_get_pblock(sb, loc,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800103 iinfo->i_location.partitionReferenceNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700104 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 return udf_get_pblock_virt15(sb, block, partition, offset);
111}
112
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800113uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 int i;
117 struct sparingTable *st = NULL;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800118 struct udf_sb_info *sbi = UDF_SB(sb);
119 struct udf_part_map *map;
120 uint32_t packet;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800121 struct udf_sparing_data *sdata;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800122
123 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -0800124 sdata = &map->s_type_specific.s_sparing;
125 packet = (block + offset) & ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700127 for (i = 0; i < 4; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800128 if (sdata->s_spar_map[i] != NULL) {
129 st = (struct sparingTable *)
130 sdata->s_spar_map[i]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
132 }
133 }
134
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700135 if (st) {
136 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800137 struct sparingEntry *entry = &st->mapEntry[i];
138 u32 origLoc = le32_to_cpu(entry->origLocation);
139 if (origLoc >= 0xFFFFFFF0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800141 else if (origLoc == packet)
142 return le32_to_cpu(entry->mappedLocation) +
143 ((block + offset) &
144 (sdata->s_packet_len - 1));
145 else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 break;
147 }
148 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700149
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800150 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
154{
155 struct udf_sparing_data *sdata;
156 struct sparingTable *st = NULL;
157 struct sparingEntry mapEntry;
158 uint32_t packet;
159 int i, j, k, l;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800160 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800161 u16 reallocationTableLen;
162 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800164 for (i = 0; i < sbi->s_partitions; i++) {
165 struct udf_part_map *map = &sbi->s_partmaps[i];
166 if (old_block > map->s_partition_root &&
167 old_block < map->s_partition_root + map->s_partition_len) {
168 sdata = &map->s_type_specific.s_sparing;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800169 packet = (old_block - map->s_partition_root) &
170 ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Marcin Slusarz4b111112008-02-08 04:20:36 -0800172 for (j = 0; j < 4; j++)
173 if (sdata->s_spar_map[j] != NULL) {
174 st = (struct sparingTable *)
175 sdata->s_spar_map[j]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (!st)
180 return 1;
181
Marcin Slusarz4b111112008-02-08 04:20:36 -0800182 reallocationTableLen =
183 le16_to_cpu(st->reallocationTableLen);
184 for (k = 0; k < reallocationTableLen; k++) {
185 struct sparingEntry *entry = &st->mapEntry[k];
186 u32 origLoc = le32_to_cpu(entry->origLocation);
187
188 if (origLoc == 0xFFFFFFFF) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189 for (; j < 4; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800190 int len;
191 bh = sdata->s_spar_map[j];
192 if (!bh)
193 continue;
194
195 st = (struct sparingTable *)
196 bh->b_data;
197 entry->origLocation =
198 cpu_to_le32(packet);
199 len =
200 sizeof(struct sparingTable) +
201 reallocationTableLen *
202 sizeof(struct sparingEntry);
203 udf_update_tag((char *)st, len);
204 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800206 *new_block = le32_to_cpu(
207 entry->mappedLocation) +
208 ((old_block -
209 map->s_partition_root) &
210 (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800212 } else if (origLoc == packet) {
213 *new_block = le32_to_cpu(
214 entry->mappedLocation) +
215 ((old_block -
216 map->s_partition_root) &
217 (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800219 } else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 break;
221 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700222
Marcin Slusarz4b111112008-02-08 04:20:36 -0800223 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800250 *new_block =
251 le32_to_cpu(
252 st->mapEntry[k].mappedLocation) +
253 ((old_block - map->s_partition_root) &
254 (sdata->s_packet_len - 1));
255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700259 } /* if old_block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700261
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800262 if (i == sbi->s_partitions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* outside of partitions */
264 /* for now, fail =) */
265 return 1;
266 }
267
268 return 0;
269}
Jan Karabfb257a2008-04-08 20:37:21 +0200270
271static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
272 uint16_t partition, uint32_t offset)
273{
274 struct super_block *sb = inode->i_sb;
275 struct udf_part_map *map;
276 kernel_lb_addr eloc;
277 uint32_t elen;
278 sector_t ext_offset;
279 struct extent_position epos = {};
280 uint32_t phyblock;
281
282 if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
283 (EXT_RECORDED_ALLOCATED >> 30))
284 phyblock = 0xFFFFFFFF;
285 else {
286 map = &UDF_SB(sb)->s_partmaps[partition];
287 /* map to sparable/physical partition desc */
288 phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
289 map->s_partition_num, ext_offset + offset);
290 }
291
292 brelse(epos.bh);
293 return phyblock;
294}
295
296uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
297 uint16_t partition, uint32_t offset)
298{
299 struct udf_sb_info *sbi = UDF_SB(sb);
300 struct udf_part_map *map;
301 struct udf_meta_data *mdata;
302 uint32_t retblk;
303 struct inode *inode;
304
305 udf_debug("READING from METADATA\n");
306
307 map = &sbi->s_partmaps[partition];
308 mdata = &map->s_type_specific.s_metadata;
309 inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
310
311 /* We shouldn't mount such media... */
312 BUG_ON(!inode);
313 retblk = udf_try_read_meta(inode, block, partition, offset);
314 if (retblk == 0xFFFFFFFF) {
315 udf_warning(sb, __func__, "error reading from METADATA, "
316 "trying to read from MIRROR");
317 inode = mdata->s_mirror_fe;
318 if (!inode)
319 return 0xFFFFFFFF;
320 retblk = udf_try_read_meta(inode, block, partition, offset);
321 }
322
323 return retblk;
324}