5 * Partition handling routines for the OSTA-UDF(tm) filesystem.
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.
13 * (C) 1998-2001 Ben Fennema
17 * 12/06/98 blf Created file.
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <linux/buffer_head.h>
30 inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
31 uint16_t partition, uint32_t offset)
33 struct udf_sb_info *sbi = UDF_SB(sb);
34 struct udf_part_map *map;
35 if (partition >= sbi->s_partitions) {
36 udf_debug("block=%d, partition=%d, offset=%d: "
37 "invalid partition\n", block, partition, offset);
40 map = &sbi->s_partmaps[partition];
41 if (map->s_partition_func)
42 return map->s_partition_func(sb, block, partition, offset);
44 return map->s_partition_root + block + offset;
47 uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
48 uint16_t partition, uint32_t offset)
50 struct buffer_head *bh = NULL;
54 struct udf_sb_info *sbi = UDF_SB(sb);
55 struct udf_part_map *map;
56 struct udf_virtual_data *vdata;
57 struct udf_inode_info *iinfo;
59 map = &sbi->s_partmaps[partition];
60 vdata = &map->s_type_specific.s_virtual;
61 index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
63 if (block > vdata->s_num_entries) {
64 udf_debug("Trying to access block beyond end of VAT "
65 "(%d max %d)\n", block, vdata->s_num_entries);
71 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
72 index = block % (sb->s_blocksize / sizeof(uint32_t));
75 index = vdata->s_start_offset / sizeof(uint32_t) + block;
78 loc = udf_block_map(sbi->s_vat_inode, newblock);
80 bh = sb_bread(sb, loc);
82 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
83 sb, block, partition, loc, index);
87 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
91 iinfo = UDF_I(sbi->s_vat_inode);
92 if (iinfo->i_location.partitionReferenceNum == partition) {
93 udf_debug("recursive call to udf_get_pblock!\n");
97 return udf_get_pblock(sb, loc,
98 iinfo->i_location.partitionReferenceNum,
102 inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
103 uint16_t partition, uint32_t offset)
105 return udf_get_pblock_virt15(sb, block, partition, offset);
108 uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
109 uint16_t partition, uint32_t offset)
112 struct sparingTable *st = NULL;
113 struct udf_sb_info *sbi = UDF_SB(sb);
114 struct udf_part_map *map;
116 struct udf_sparing_data *sdata;
118 map = &sbi->s_partmaps[partition];
119 sdata = &map->s_type_specific.s_sparing;
120 packet = (block + offset) & ~(sdata->s_packet_len - 1);
122 for (i = 0; i < 4; i++) {
123 if (sdata->s_spar_map[i] != NULL) {
124 st = (struct sparingTable *)
125 sdata->s_spar_map[i]->b_data;
131 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
132 struct sparingEntry *entry = &st->mapEntry[i];
133 u32 origLoc = le32_to_cpu(entry->origLocation);
134 if (origLoc >= 0xFFFFFFF0)
136 else if (origLoc == packet)
137 return le32_to_cpu(entry->mappedLocation) +
139 (sdata->s_packet_len - 1));
140 else if (origLoc > packet)
145 return map->s_partition_root + block + offset;
148 int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
150 struct udf_sparing_data *sdata;
151 struct sparingTable *st = NULL;
152 struct sparingEntry mapEntry;
155 struct udf_sb_info *sbi = UDF_SB(sb);
156 u16 reallocationTableLen;
157 struct buffer_head *bh;
159 for (i = 0; i < sbi->s_partitions; i++) {
160 struct udf_part_map *map = &sbi->s_partmaps[i];
161 if (old_block > map->s_partition_root &&
162 old_block < map->s_partition_root + map->s_partition_len) {
163 sdata = &map->s_type_specific.s_sparing;
164 packet = (old_block - map->s_partition_root) &
165 ~(sdata->s_packet_len - 1);
167 for (j = 0; j < 4; j++)
168 if (sdata->s_spar_map[j] != NULL) {
169 st = (struct sparingTable *)
170 sdata->s_spar_map[j]->b_data;
177 reallocationTableLen =
178 le16_to_cpu(st->reallocationTableLen);
179 for (k = 0; k < reallocationTableLen; k++) {
180 struct sparingEntry *entry = &st->mapEntry[k];
181 u32 origLoc = le32_to_cpu(entry->origLocation);
183 if (origLoc == 0xFFFFFFFF) {
186 bh = sdata->s_spar_map[j];
190 st = (struct sparingTable *)
192 entry->origLocation =
195 sizeof(struct sparingTable) +
196 reallocationTableLen *
197 sizeof(struct sparingEntry);
198 udf_update_tag((char *)st, len);
199 mark_buffer_dirty(bh);
201 *new_block = le32_to_cpu(
202 entry->mappedLocation) +
204 map->s_partition_root) &
205 (sdata->s_packet_len - 1));
207 } else if (origLoc == packet) {
208 *new_block = le32_to_cpu(
209 entry->mappedLocation) +
211 map->s_partition_root) &
212 (sdata->s_packet_len - 1));
214 } else if (origLoc > packet)
218 for (l = k; l < reallocationTableLen; l++) {
219 struct sparingEntry *entry = &st->mapEntry[l];
220 u32 origLoc = le32_to_cpu(entry->origLocation);
222 if (origLoc != 0xFFFFFFFF)
226 bh = sdata->s_spar_map[j];
230 st = (struct sparingTable *)bh->b_data;
231 mapEntry = st->mapEntry[l];
232 mapEntry.origLocation =
234 memmove(&st->mapEntry[k + 1],
237 sizeof(struct sparingEntry));
238 st->mapEntry[k] = mapEntry;
239 udf_update_tag((char *)st,
240 sizeof(struct sparingTable) +
241 reallocationTableLen *
242 sizeof(struct sparingEntry));
243 mark_buffer_dirty(bh);
247 st->mapEntry[k].mappedLocation) +
248 ((old_block - map->s_partition_root) &
249 (sdata->s_packet_len - 1));
257 if (i == sbi->s_partitions) {
258 /* outside of partitions */
259 /* for now, fail =) */