blob: b526f25c04c9d5d4ba75f5261456e2557ea4f162 [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/buffer_head.h>
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +010028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
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) {
Joe Perchesa983f362011-10-10 01:08:07 -070036 udf_debug("block=%d, partition=%d, offset=%d: invalid partition\n",
37 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) {
Joe Perchesa983f362011-10-10 01:08:07 -070063 udf_debug("Trying to access block beyond end of VAT (%d max %d)\n",
64 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;
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100163 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100165 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800166 for (i = 0; i < sbi->s_partitions; i++) {
167 struct udf_part_map *map = &sbi->s_partmaps[i];
168 if (old_block > map->s_partition_root &&
169 old_block < map->s_partition_root + map->s_partition_len) {
170 sdata = &map->s_type_specific.s_sparing;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800171 packet = (old_block - map->s_partition_root) &
172 ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Marcin Slusarz4b111112008-02-08 04:20:36 -0800174 for (j = 0; j < 4; j++)
175 if (sdata->s_spar_map[j] != NULL) {
176 st = (struct sparingTable *)
177 sdata->s_spar_map[j]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100181 if (!st) {
182 ret = 1;
183 goto out;
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Marcin Slusarz4b111112008-02-08 04:20:36 -0800186 reallocationTableLen =
187 le16_to_cpu(st->reallocationTableLen);
188 for (k = 0; k < reallocationTableLen; k++) {
189 struct sparingEntry *entry = &st->mapEntry[k];
190 u32 origLoc = le32_to_cpu(entry->origLocation);
191
192 if (origLoc == 0xFFFFFFFF) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700193 for (; j < 4; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800194 int len;
195 bh = sdata->s_spar_map[j];
196 if (!bh)
197 continue;
198
199 st = (struct sparingTable *)
200 bh->b_data;
201 entry->origLocation =
202 cpu_to_le32(packet);
203 len =
204 sizeof(struct sparingTable) +
205 reallocationTableLen *
206 sizeof(struct sparingEntry);
207 udf_update_tag((char *)st, len);
208 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800210 *new_block = le32_to_cpu(
211 entry->mappedLocation) +
212 ((old_block -
213 map->s_partition_root) &
214 (sdata->s_packet_len - 1));
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100215 ret = 0;
216 goto out;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800217 } else if (origLoc == packet) {
218 *new_block = le32_to_cpu(
219 entry->mappedLocation) +
220 ((old_block -
221 map->s_partition_root) &
222 (sdata->s_packet_len - 1));
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100223 ret = 0;
224 goto out;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800225 } else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
227 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700228
Marcin Slusarz4b111112008-02-08 04:20:36 -0800229 for (l = k; l < reallocationTableLen; l++) {
230 struct sparingEntry *entry = &st->mapEntry[l];
231 u32 origLoc = le32_to_cpu(entry->origLocation);
232
233 if (origLoc != 0xFFFFFFFF)
234 continue;
235
236 for (; j < 4; j++) {
237 bh = sdata->s_spar_map[j];
238 if (!bh)
239 continue;
240
241 st = (struct sparingTable *)bh->b_data;
242 mapEntry = st->mapEntry[l];
243 mapEntry.origLocation =
244 cpu_to_le32(packet);
245 memmove(&st->mapEntry[k + 1],
246 &st->mapEntry[k],
247 (l - k) *
248 sizeof(struct sparingEntry));
249 st->mapEntry[k] = mapEntry;
250 udf_update_tag((char *)st,
251 sizeof(struct sparingTable) +
252 reallocationTableLen *
253 sizeof(struct sparingEntry));
254 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800256 *new_block =
257 le32_to_cpu(
258 st->mapEntry[k].mappedLocation) +
259 ((old_block - map->s_partition_root) &
260 (sdata->s_packet_len - 1));
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100261 ret = 0;
262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700264
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100265 ret = 1;
266 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700267 } /* if old_block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700269
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800270 if (i == sbi->s_partitions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* outside of partitions */
272 /* for now, fail =) */
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100273 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100276out:
277 mutex_unlock(&sbi->s_alloc_mutex);
278 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
Jan Karabfb257a2008-04-08 20:37:21 +0200280
281static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
282 uint16_t partition, uint32_t offset)
283{
284 struct super_block *sb = inode->i_sb;
285 struct udf_part_map *map;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200286 struct kernel_lb_addr eloc;
Jan Karabfb257a2008-04-08 20:37:21 +0200287 uint32_t elen;
288 sector_t ext_offset;
289 struct extent_position epos = {};
290 uint32_t phyblock;
291
292 if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
293 (EXT_RECORDED_ALLOCATED >> 30))
294 phyblock = 0xFFFFFFFF;
295 else {
296 map = &UDF_SB(sb)->s_partmaps[partition];
297 /* map to sparable/physical partition desc */
298 phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
299 map->s_partition_num, ext_offset + offset);
300 }
301
302 brelse(epos.bh);
303 return phyblock;
304}
305
306uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
307 uint16_t partition, uint32_t offset)
308{
309 struct udf_sb_info *sbi = UDF_SB(sb);
310 struct udf_part_map *map;
311 struct udf_meta_data *mdata;
312 uint32_t retblk;
313 struct inode *inode;
314
315 udf_debug("READING from METADATA\n");
316
317 map = &sbi->s_partmaps[partition];
318 mdata = &map->s_type_specific.s_metadata;
319 inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
320
321 /* We shouldn't mount such media... */
322 BUG_ON(!inode);
323 retblk = udf_try_read_meta(inode, block, partition, offset);
Namjae Jeon3080a742011-10-23 19:28:32 +0900324 if (retblk == 0xFFFFFFFF && mdata->s_metadata_fe) {
Joe Perchesa40ecd72011-10-10 01:08:04 -0700325 udf_warn(sb, "error reading from METADATA, trying to read from MIRROR\n");
Namjae Jeon3080a742011-10-23 19:28:32 +0900326 if (!mdata->s_mirror_loaded_flag) {
327 mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
328 mdata->s_mirror_file_loc, map->s_partition_num);
329 mdata->s_mirror_loaded_flag = 1;
330 }
331
Jan Karabfb257a2008-04-08 20:37:21 +0200332 inode = mdata->s_mirror_fe;
333 if (!inode)
334 return 0xFFFFFFFF;
335 retblk = udf_try_read_meta(inode, block, partition, offset);
336 }
337
338 return retblk;
339}