blob: 5f861ed287c3fde0fbf87de4c740e672213acd5d [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>
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +010027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Adrian Bunk22ba0312008-04-28 18:38:49 +030029uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
30 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Marcin Slusarz6c79e982008-02-08 04:20:30 -080032 struct udf_sb_info *sbi = UDF_SB(sb);
33 struct udf_part_map *map;
34 if (partition >= sbi->s_partitions) {
Joe Perchesa983f362011-10-10 01:08:07 -070035 udf_debug("block=%d, partition=%d, offset=%d: invalid partition\n",
36 block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return 0xFFFFFFFF;
38 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -080039 map = &sbi->s_partmaps[partition];
40 if (map->s_partition_func)
41 return map->s_partition_func(sb, block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -080043 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070046uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070047 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 struct buffer_head *bh = NULL;
50 uint32_t newblock;
51 uint32_t index;
52 uint32_t loc;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080053 struct udf_sb_info *sbi = UDF_SB(sb);
54 struct udf_part_map *map;
Marcin Slusarz4b111112008-02-08 04:20:36 -080055 struct udf_virtual_data *vdata;
Jan Karafa5e0812008-04-08 02:08:53 +020056 struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Marcin Slusarz6c79e982008-02-08 04:20:30 -080058 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -080059 vdata = &map->s_type_specific.s_virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Marcin Slusarz4b111112008-02-08 04:20:36 -080061 if (block > vdata->s_num_entries) {
Joe Perchesa983f362011-10-10 01:08:07 -070062 udf_debug("Trying to access block beyond end of VAT (%d max %d)\n",
63 block, vdata->s_num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0xFFFFFFFF;
65 }
66
Jan Karafa5e0812008-04-08 02:08:53 +020067 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +020068 loc = le32_to_cpu(((__le32 *)(iinfo->i_ext.i_data +
69 vdata->s_start_offset))[block]);
Jan Karafa5e0812008-04-08 02:08:53 +020070 goto translate;
71 }
72 index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070073 if (block >= index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 block -= index;
75 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
76 index = block % (sb->s_blocksize / sizeof(uint32_t));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070077 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 newblock = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -080079 index = vdata->s_start_offset / sizeof(uint32_t) + block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Marcin Slusarz6c79e982008-02-08 04:20:30 -080082 loc = udf_block_map(sbi->s_vat_inode, newblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Marcin Slusarz4b111112008-02-08 04:20:36 -080084 bh = sb_bread(sb, loc);
85 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070087 sb, block, partition, loc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0xFFFFFFFF;
89 }
90
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070091 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Jan Kara3bf25cb2007-05-08 00:35:16 -070093 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Jan Karafa5e0812008-04-08 02:08:53 +020095translate:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080096 if (iinfo->i_location.partitionReferenceNum == partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 udf_debug("recursive call to udf_get_pblock!\n");
98 return 0xFFFFFFFF;
99 }
100
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700101 return udf_get_pblock(sb, loc,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800102 iinfo->i_location.partitionReferenceNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700103 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Marcin Slusarz4b111112008-02-08 04:20:36 -0800106inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 return udf_get_pblock_virt15(sb, block, partition, offset);
110}
111
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800112uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 int i;
116 struct sparingTable *st = NULL;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800117 struct udf_sb_info *sbi = UDF_SB(sb);
118 struct udf_part_map *map;
119 uint32_t packet;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800120 struct udf_sparing_data *sdata;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800121
122 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -0800123 sdata = &map->s_type_specific.s_sparing;
124 packet = (block + offset) & ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700126 for (i = 0; i < 4; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800127 if (sdata->s_spar_map[i] != NULL) {
128 st = (struct sparingTable *)
129 sdata->s_spar_map[i]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 break;
131 }
132 }
133
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700134 if (st) {
135 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800136 struct sparingEntry *entry = &st->mapEntry[i];
137 u32 origLoc = le32_to_cpu(entry->origLocation);
138 if (origLoc >= 0xFFFFFFF0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800140 else if (origLoc == packet)
141 return le32_to_cpu(entry->mappedLocation) +
142 ((block + offset) &
143 (sdata->s_packet_len - 1));
144 else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 }
147 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700148
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800149 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
153{
154 struct udf_sparing_data *sdata;
155 struct sparingTable *st = NULL;
156 struct sparingEntry mapEntry;
157 uint32_t packet;
158 int i, j, k, l;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800159 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800160 u16 reallocationTableLen;
161 struct buffer_head *bh;
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100162 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100164 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800165 for (i = 0; i < sbi->s_partitions; i++) {
166 struct udf_part_map *map = &sbi->s_partmaps[i];
167 if (old_block > map->s_partition_root &&
168 old_block < map->s_partition_root + map->s_partition_len) {
169 sdata = &map->s_type_specific.s_sparing;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800170 packet = (old_block - map->s_partition_root) &
171 ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Marcin Slusarz4b111112008-02-08 04:20:36 -0800173 for (j = 0; j < 4; j++)
174 if (sdata->s_spar_map[j] != NULL) {
175 st = (struct sparingTable *)
176 sdata->s_spar_map[j]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100180 if (!st) {
181 ret = 1;
182 goto out;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Marcin Slusarz4b111112008-02-08 04:20:36 -0800185 reallocationTableLen =
186 le16_to_cpu(st->reallocationTableLen);
187 for (k = 0; k < reallocationTableLen; k++) {
188 struct sparingEntry *entry = &st->mapEntry[k];
189 u32 origLoc = le32_to_cpu(entry->origLocation);
190
191 if (origLoc == 0xFFFFFFFF) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700192 for (; j < 4; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800193 int len;
194 bh = sdata->s_spar_map[j];
195 if (!bh)
196 continue;
197
198 st = (struct sparingTable *)
199 bh->b_data;
200 entry->origLocation =
201 cpu_to_le32(packet);
202 len =
203 sizeof(struct sparingTable) +
204 reallocationTableLen *
205 sizeof(struct sparingEntry);
206 udf_update_tag((char *)st, len);
207 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800209 *new_block = le32_to_cpu(
210 entry->mappedLocation) +
211 ((old_block -
212 map->s_partition_root) &
213 (sdata->s_packet_len - 1));
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100214 ret = 0;
215 goto out;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800216 } else if (origLoc == packet) {
217 *new_block = le32_to_cpu(
218 entry->mappedLocation) +
219 ((old_block -
220 map->s_partition_root) &
221 (sdata->s_packet_len - 1));
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100222 ret = 0;
223 goto out;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800224 } else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
226 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227
Marcin Slusarz4b111112008-02-08 04:20:36 -0800228 for (l = k; l < reallocationTableLen; l++) {
229 struct sparingEntry *entry = &st->mapEntry[l];
230 u32 origLoc = le32_to_cpu(entry->origLocation);
231
232 if (origLoc != 0xFFFFFFFF)
233 continue;
234
235 for (; j < 4; j++) {
236 bh = sdata->s_spar_map[j];
237 if (!bh)
238 continue;
239
240 st = (struct sparingTable *)bh->b_data;
241 mapEntry = st->mapEntry[l];
242 mapEntry.origLocation =
243 cpu_to_le32(packet);
244 memmove(&st->mapEntry[k + 1],
245 &st->mapEntry[k],
246 (l - k) *
247 sizeof(struct sparingEntry));
248 st->mapEntry[k] = mapEntry;
249 udf_update_tag((char *)st,
250 sizeof(struct sparingTable) +
251 reallocationTableLen *
252 sizeof(struct sparingEntry));
253 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800255 *new_block =
256 le32_to_cpu(
257 st->mapEntry[k].mappedLocation) +
258 ((old_block - map->s_partition_root) &
259 (sdata->s_packet_len - 1));
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100260 ret = 0;
261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700263
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100264 ret = 1;
265 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700266 } /* if old_block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700268
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800269 if (i == sbi->s_partitions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* outside of partitions */
271 /* for now, fail =) */
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100272 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Alessio Igor Bogani7db09be2010-11-16 18:40:48 +0100275out:
276 mutex_unlock(&sbi->s_alloc_mutex);
277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Jan Karabfb257a2008-04-08 20:37:21 +0200279
280static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
281 uint16_t partition, uint32_t offset)
282{
283 struct super_block *sb = inode->i_sb;
284 struct udf_part_map *map;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200285 struct kernel_lb_addr eloc;
Jan Karabfb257a2008-04-08 20:37:21 +0200286 uint32_t elen;
287 sector_t ext_offset;
288 struct extent_position epos = {};
289 uint32_t phyblock;
290
291 if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
292 (EXT_RECORDED_ALLOCATED >> 30))
293 phyblock = 0xFFFFFFFF;
294 else {
295 map = &UDF_SB(sb)->s_partmaps[partition];
296 /* map to sparable/physical partition desc */
297 phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
298 map->s_partition_num, ext_offset + offset);
299 }
300
301 brelse(epos.bh);
302 return phyblock;
303}
304
305uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
306 uint16_t partition, uint32_t offset)
307{
308 struct udf_sb_info *sbi = UDF_SB(sb);
309 struct udf_part_map *map;
310 struct udf_meta_data *mdata;
311 uint32_t retblk;
312 struct inode *inode;
313
314 udf_debug("READING from METADATA\n");
315
316 map = &sbi->s_partmaps[partition];
317 mdata = &map->s_type_specific.s_metadata;
318 inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
319
320 /* We shouldn't mount such media... */
321 BUG_ON(!inode);
322 retblk = udf_try_read_meta(inode, block, partition, offset);
Namjae Jeon3080a742011-10-23 19:28:32 +0900323 if (retblk == 0xFFFFFFFF && mdata->s_metadata_fe) {
Joe Perchesa40ecd72011-10-10 01:08:04 -0700324 udf_warn(sb, "error reading from METADATA, trying to read from MIRROR\n");
Jan Karaed47a7d2011-10-24 16:47:48 +0200325 if (!(mdata->s_flags & MF_MIRROR_FE_LOADED)) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900326 mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
327 mdata->s_mirror_file_loc, map->s_partition_num);
Jan Karaed47a7d2011-10-24 16:47:48 +0200328 mdata->s_flags |= MF_MIRROR_FE_LOADED;
Namjae Jeon3080a742011-10-23 19:28:32 +0900329 }
330
Jan Karabfb257a2008-04-08 20:37:21 +0200331 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}