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/mutex.h>
29 uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
30 uint16_t partition, uint32_t offset)
32 struct udf_sb_info *sbi = UDF_SB(sb);
33 struct udf_part_map *map;
34 if (partition >= sbi->s_partitions) {
35 udf_debug("block=%u, partition=%u, offset=%u: invalid partition\n",
36 block, partition, offset);
39 map = &sbi->s_partmaps[partition];
40 if (map->s_partition_func)
41 return map->s_partition_func(sb, block, partition, offset);
43 return map->s_partition_root + block + offset;
46 uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
47 uint16_t partition, uint32_t offset)
49 struct buffer_head *bh = NULL;
53 struct udf_sb_info *sbi = UDF_SB(sb);
54 struct udf_part_map *map;
55 struct udf_virtual_data *vdata;
56 struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode);
59 map = &sbi->s_partmaps[partition];
60 vdata = &map->s_type_specific.s_virtual;
62 if (block > vdata->s_num_entries) {
63 udf_debug("Trying to access block beyond end of VAT (%u max %u)\n",
64 block, vdata->s_num_entries);
68 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
69 loc = le32_to_cpu(((__le32 *)(iinfo->i_data +
70 vdata->s_start_offset))[block]);
73 index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
76 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
77 index = block % (sb->s_blocksize / sizeof(uint32_t));
80 index = vdata->s_start_offset / sizeof(uint32_t) + block;
83 bh = udf_bread(sbi->s_vat_inode, newblock, 0, &err);
85 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%u,%u)\n",
86 sb, block, partition);
90 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
95 if (iinfo->i_location.partitionReferenceNum == partition) {
96 udf_debug("recursive call to udf_get_pblock!\n");
100 return udf_get_pblock(sb, loc,
101 iinfo->i_location.partitionReferenceNum,
105 inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
106 uint16_t partition, uint32_t offset)
108 return udf_get_pblock_virt15(sb, block, partition, offset);
111 uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
112 uint16_t partition, uint32_t offset)
115 struct sparingTable *st = NULL;
116 struct udf_sb_info *sbi = UDF_SB(sb);
117 struct udf_part_map *map;
119 struct udf_sparing_data *sdata;
121 map = &sbi->s_partmaps[partition];
122 sdata = &map->s_type_specific.s_sparing;
123 packet = (block + offset) & ~(sdata->s_packet_len - 1);
125 for (i = 0; i < 4; i++) {
126 if (sdata->s_spar_map[i] != NULL) {
127 st = (struct sparingTable *)
128 sdata->s_spar_map[i]->b_data;
134 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
135 struct sparingEntry *entry = &st->mapEntry[i];
136 u32 origLoc = le32_to_cpu(entry->origLocation);
137 if (origLoc >= 0xFFFFFFF0)
139 else if (origLoc == packet)
140 return le32_to_cpu(entry->mappedLocation) +
142 (sdata->s_packet_len - 1));
143 else if (origLoc > packet)
148 return map->s_partition_root + block + offset;
151 int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
153 struct udf_sparing_data *sdata;
154 struct sparingTable *st = NULL;
155 struct sparingEntry mapEntry;
158 struct udf_sb_info *sbi = UDF_SB(sb);
159 u16 reallocationTableLen;
160 struct buffer_head *bh;
163 mutex_lock(&sbi->s_alloc_mutex);
164 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;
169 packet = (old_block - map->s_partition_root) &
170 ~(sdata->s_packet_len - 1);
172 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;
184 reallocationTableLen =
185 le16_to_cpu(st->reallocationTableLen);
186 for (k = 0; k < reallocationTableLen; k++) {
187 struct sparingEntry *entry = &st->mapEntry[k];
188 u32 origLoc = le32_to_cpu(entry->origLocation);
190 if (origLoc == 0xFFFFFFFF) {
193 bh = sdata->s_spar_map[j];
197 st = (struct sparingTable *)
199 entry->origLocation =
202 sizeof(struct sparingTable) +
203 reallocationTableLen *
204 sizeof(struct sparingEntry);
205 udf_update_tag((char *)st, len);
206 mark_buffer_dirty(bh);
208 *new_block = le32_to_cpu(
209 entry->mappedLocation) +
211 map->s_partition_root) &
212 (sdata->s_packet_len - 1));
215 } else if (origLoc == packet) {
216 *new_block = le32_to_cpu(
217 entry->mappedLocation) +
219 map->s_partition_root) &
220 (sdata->s_packet_len - 1));
223 } else if (origLoc > packet)
227 for (l = k; l < reallocationTableLen; l++) {
228 struct sparingEntry *entry = &st->mapEntry[l];
229 u32 origLoc = le32_to_cpu(entry->origLocation);
231 if (origLoc != 0xFFFFFFFF)
235 bh = sdata->s_spar_map[j];
239 st = (struct sparingTable *)bh->b_data;
240 mapEntry = st->mapEntry[l];
241 mapEntry.origLocation =
243 memmove(&st->mapEntry[k + 1],
246 sizeof(struct sparingEntry));
247 st->mapEntry[k] = mapEntry;
248 udf_update_tag((char *)st,
249 sizeof(struct sparingTable) +
250 reallocationTableLen *
251 sizeof(struct sparingEntry));
252 mark_buffer_dirty(bh);
256 st->mapEntry[k].mappedLocation) +
257 ((old_block - map->s_partition_root) &
258 (sdata->s_packet_len - 1));
268 if (i == sbi->s_partitions) {
269 /* outside of partitions */
270 /* for now, fail =) */
275 mutex_unlock(&sbi->s_alloc_mutex);
279 static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
280 uint16_t partition, uint32_t offset)
282 struct super_block *sb = inode->i_sb;
283 struct udf_part_map *map;
284 struct kernel_lb_addr eloc;
287 struct extent_position epos = {};
290 if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
291 (EXT_RECORDED_ALLOCATED >> 30))
292 phyblock = 0xFFFFFFFF;
294 map = &UDF_SB(sb)->s_partmaps[partition];
295 /* map to sparable/physical partition desc */
296 phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
297 map->s_type_specific.s_metadata.s_phys_partition_ref,
298 ext_offset + offset);
305 uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
306 uint16_t partition, uint32_t offset)
308 struct udf_sb_info *sbi = UDF_SB(sb);
309 struct udf_part_map *map;
310 struct udf_meta_data *mdata;
314 udf_debug("READING from METADATA\n");
316 map = &sbi->s_partmaps[partition];
317 mdata = &map->s_type_specific.s_metadata;
318 inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
323 retblk = udf_try_read_meta(inode, block, partition, offset);
324 if (retblk == 0xFFFFFFFF && mdata->s_metadata_fe) {
325 udf_warn(sb, "error reading from METADATA, trying to read from MIRROR\n");
326 if (!(mdata->s_flags & MF_MIRROR_FE_LOADED)) {
327 mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
328 mdata->s_mirror_file_loc,
329 mdata->s_phys_partition_ref);
330 if (IS_ERR(mdata->s_mirror_fe))
331 mdata->s_mirror_fe = NULL;
332 mdata->s_flags |= MF_MIRROR_FE_LOADED;
335 inode = mdata->s_mirror_fe;
338 retblk = udf_try_read_meta(inode, block, partition, offset);