blob: fc533345ab89198a12fb472765f7c9073a356970 [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>
27#include <linux/udf_fs.h>
28#include <linux/slab.h>
29#include <linux/buffer_head.h>
30
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070031inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
32 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Marcin Slusarz6c79e982008-02-08 04:20:30 -080034 struct udf_sb_info *sbi = UDF_SB(sb);
35 struct udf_part_map *map;
36 if (partition >= sbi->s_partitions) {
Marcin Slusarz4b111112008-02-08 04:20:36 -080037 udf_debug("block=%d, partition=%d, offset=%d: "
38 "invalid partition\n", block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return 0xFFFFFFFF;
40 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -080041 map = &sbi->s_partmaps[partition];
42 if (map->s_partition_func)
43 return map->s_partition_func(sb, block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -080045 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070048uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070049 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct buffer_head *bh = NULL;
52 uint32_t newblock;
53 uint32_t index;
54 uint32_t loc;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080055 struct udf_sb_info *sbi = UDF_SB(sb);
56 struct udf_part_map *map;
Marcin Slusarz4b111112008-02-08 04:20:36 -080057 struct udf_virtual_data *vdata;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080058 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Marcin Slusarz6c79e982008-02-08 04:20:30 -080060 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -080061 vdata = &map->s_type_specific.s_virtual;
62 index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Marcin Slusarz4b111112008-02-08 04:20:36 -080064 if (block > vdata->s_num_entries) {
65 udf_debug("Trying to access block beyond end of VAT "
66 "(%d max %d)\n", block, vdata->s_num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return 0xFFFFFFFF;
68 }
69
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070070 if (block >= index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 block -= index;
72 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
73 index = block % (sb->s_blocksize / sizeof(uint32_t));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070074 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 newblock = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -080076 index = vdata->s_start_offset / sizeof(uint32_t) + block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
Marcin Slusarz6c79e982008-02-08 04:20:30 -080079 loc = udf_block_map(sbi->s_vat_inode, newblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Marcin Slusarz4b111112008-02-08 04:20:36 -080081 bh = sb_bread(sb, loc);
82 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070084 sb, block, partition, loc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0xFFFFFFFF;
86 }
87
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070088 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Jan Kara3bf25cb2007-05-08 00:35:16 -070090 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080092 iinfo = UDF_I(sbi->s_vat_inode);
93 if (iinfo->i_location.partitionReferenceNum == partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 udf_debug("recursive call to udf_get_pblock!\n");
95 return 0xFFFFFFFF;
96 }
97
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070098 return udf_get_pblock(sb, loc,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080099 iinfo->i_location.partitionReferenceNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700100 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Marcin Slusarz4b111112008-02-08 04:20:36 -0800103inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700104 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 return udf_get_pblock_virt15(sb, block, partition, offset);
107}
108
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800109uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 int i;
113 struct sparingTable *st = NULL;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800114 struct udf_sb_info *sbi = UDF_SB(sb);
115 struct udf_part_map *map;
116 uint32_t packet;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800117 struct udf_sparing_data *sdata;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800118
119 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -0800120 sdata = &map->s_type_specific.s_sparing;
121 packet = (block + offset) & ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700123 for (i = 0; i < 4; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800124 if (sdata->s_spar_map[i] != NULL) {
125 st = (struct sparingTable *)
126 sdata->s_spar_map[i]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
128 }
129 }
130
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700131 if (st) {
132 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800133 struct sparingEntry *entry = &st->mapEntry[i];
134 u32 origLoc = le32_to_cpu(entry->origLocation);
135 if (origLoc >= 0xFFFFFFF0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 break;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800137 else if (origLoc == packet)
138 return le32_to_cpu(entry->mappedLocation) +
139 ((block + offset) &
140 (sdata->s_packet_len - 1));
141 else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 break;
143 }
144 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700145
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800146 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
150{
151 struct udf_sparing_data *sdata;
152 struct sparingTable *st = NULL;
153 struct sparingEntry mapEntry;
154 uint32_t packet;
155 int i, j, k, l;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800156 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800157 u16 reallocationTableLen;
158 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800160 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;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800165 packet = (old_block - map->s_partition_root) &
166 ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Marcin Slusarz4b111112008-02-08 04:20:36 -0800168 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!st)
176 return 1;
177
Marcin Slusarz4b111112008-02-08 04:20:36 -0800178 reallocationTableLen =
179 le16_to_cpu(st->reallocationTableLen);
180 for (k = 0; k < reallocationTableLen; k++) {
181 struct sparingEntry *entry = &st->mapEntry[k];
182 u32 origLoc = le32_to_cpu(entry->origLocation);
183
184 if (origLoc == 0xFFFFFFFF) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700185 for (; j < 4; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800186 int len;
187 bh = sdata->s_spar_map[j];
188 if (!bh)
189 continue;
190
191 st = (struct sparingTable *)
192 bh->b_data;
193 entry->origLocation =
194 cpu_to_le32(packet);
195 len =
196 sizeof(struct sparingTable) +
197 reallocationTableLen *
198 sizeof(struct sparingEntry);
199 udf_update_tag((char *)st, len);
200 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800202 *new_block = le32_to_cpu(
203 entry->mappedLocation) +
204 ((old_block -
205 map->s_partition_root) &
206 (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800208 } else if (origLoc == packet) {
209 *new_block = le32_to_cpu(
210 entry->mappedLocation) +
211 ((old_block -
212 map->s_partition_root) &
213 (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800215 } else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
Marcin Slusarz4b111112008-02-08 04:20:36 -0800219 for (l = k; l < reallocationTableLen; l++) {
220 struct sparingEntry *entry = &st->mapEntry[l];
221 u32 origLoc = le32_to_cpu(entry->origLocation);
222
223 if (origLoc != 0xFFFFFFFF)
224 continue;
225
226 for (; j < 4; j++) {
227 bh = sdata->s_spar_map[j];
228 if (!bh)
229 continue;
230
231 st = (struct sparingTable *)bh->b_data;
232 mapEntry = st->mapEntry[l];
233 mapEntry.origLocation =
234 cpu_to_le32(packet);
235 memmove(&st->mapEntry[k + 1],
236 &st->mapEntry[k],
237 (l - k) *
238 sizeof(struct sparingEntry));
239 st->mapEntry[k] = mapEntry;
240 udf_update_tag((char *)st,
241 sizeof(struct sparingTable) +
242 reallocationTableLen *
243 sizeof(struct sparingEntry));
244 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800246 *new_block =
247 le32_to_cpu(
248 st->mapEntry[k].mappedLocation) +
249 ((old_block - map->s_partition_root) &
250 (sdata->s_packet_len - 1));
251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700255 } /* if old_block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700257
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800258 if (i == sbi->s_partitions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* outside of partitions */
260 /* for now, fail =) */
261 return 1;
262 }
263
264 return 0;
265}