0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include "udfdecl.h"
0022 #include "udf_sb.h"
0023 #include "udf_i.h"
0024
0025 #include <linux/fs.h>
0026 #include <linux/string.h>
0027 #include <linux/mutex.h>
0028
0029 uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
0030 uint16_t partition, uint32_t offset)
0031 {
0032 struct udf_sb_info *sbi = UDF_SB(sb);
0033 struct udf_part_map *map;
0034 if (partition >= sbi->s_partitions) {
0035 udf_debug("block=%u, partition=%u, offset=%u: invalid partition\n",
0036 block, partition, offset);
0037 return 0xFFFFFFFF;
0038 }
0039 map = &sbi->s_partmaps[partition];
0040 if (map->s_partition_func)
0041 return map->s_partition_func(sb, block, partition, offset);
0042 else
0043 return map->s_partition_root + block + offset;
0044 }
0045
0046 uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
0047 uint16_t partition, uint32_t offset)
0048 {
0049 struct buffer_head *bh = NULL;
0050 uint32_t newblock;
0051 uint32_t index;
0052 uint32_t loc;
0053 struct udf_sb_info *sbi = UDF_SB(sb);
0054 struct udf_part_map *map;
0055 struct udf_virtual_data *vdata;
0056 struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode);
0057
0058 map = &sbi->s_partmaps[partition];
0059 vdata = &map->s_type_specific.s_virtual;
0060
0061 if (block > vdata->s_num_entries) {
0062 udf_debug("Trying to access block beyond end of VAT (%u max %u)\n",
0063 block, vdata->s_num_entries);
0064 return 0xFFFFFFFF;
0065 }
0066
0067 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
0068 loc = le32_to_cpu(((__le32 *)(iinfo->i_data +
0069 vdata->s_start_offset))[block]);
0070 goto translate;
0071 }
0072 index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
0073 if (block >= index) {
0074 block -= index;
0075 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
0076 index = block % (sb->s_blocksize / sizeof(uint32_t));
0077 } else {
0078 newblock = 0;
0079 index = vdata->s_start_offset / sizeof(uint32_t) + block;
0080 }
0081
0082 loc = udf_block_map(sbi->s_vat_inode, newblock);
0083
0084 bh = sb_bread(sb, loc);
0085 if (!bh) {
0086 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%u,%u) VAT: %u[%u]\n",
0087 sb, block, partition, loc, index);
0088 return 0xFFFFFFFF;
0089 }
0090
0091 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
0092
0093 brelse(bh);
0094
0095 translate:
0096 if (iinfo->i_location.partitionReferenceNum == partition) {
0097 udf_debug("recursive call to udf_get_pblock!\n");
0098 return 0xFFFFFFFF;
0099 }
0100
0101 return udf_get_pblock(sb, loc,
0102 iinfo->i_location.partitionReferenceNum,
0103 offset);
0104 }
0105
0106 inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
0107 uint16_t partition, uint32_t offset)
0108 {
0109 return udf_get_pblock_virt15(sb, block, partition, offset);
0110 }
0111
0112 uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
0113 uint16_t partition, uint32_t offset)
0114 {
0115 int i;
0116 struct sparingTable *st = NULL;
0117 struct udf_sb_info *sbi = UDF_SB(sb);
0118 struct udf_part_map *map;
0119 uint32_t packet;
0120 struct udf_sparing_data *sdata;
0121
0122 map = &sbi->s_partmaps[partition];
0123 sdata = &map->s_type_specific.s_sparing;
0124 packet = (block + offset) & ~(sdata->s_packet_len - 1);
0125
0126 for (i = 0; i < 4; i++) {
0127 if (sdata->s_spar_map[i] != NULL) {
0128 st = (struct sparingTable *)
0129 sdata->s_spar_map[i]->b_data;
0130 break;
0131 }
0132 }
0133
0134 if (st) {
0135 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
0136 struct sparingEntry *entry = &st->mapEntry[i];
0137 u32 origLoc = le32_to_cpu(entry->origLocation);
0138 if (origLoc >= 0xFFFFFFF0)
0139 break;
0140 else if (origLoc == packet)
0141 return le32_to_cpu(entry->mappedLocation) +
0142 ((block + offset) &
0143 (sdata->s_packet_len - 1));
0144 else if (origLoc > packet)
0145 break;
0146 }
0147 }
0148
0149 return map->s_partition_root + block + offset;
0150 }
0151
0152 int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
0153 {
0154 struct udf_sparing_data *sdata;
0155 struct sparingTable *st = NULL;
0156 struct sparingEntry mapEntry;
0157 uint32_t packet;
0158 int i, j, k, l;
0159 struct udf_sb_info *sbi = UDF_SB(sb);
0160 u16 reallocationTableLen;
0161 struct buffer_head *bh;
0162 int ret = 0;
0163
0164 mutex_lock(&sbi->s_alloc_mutex);
0165 for (i = 0; i < sbi->s_partitions; i++) {
0166 struct udf_part_map *map = &sbi->s_partmaps[i];
0167 if (old_block > map->s_partition_root &&
0168 old_block < map->s_partition_root + map->s_partition_len) {
0169 sdata = &map->s_type_specific.s_sparing;
0170 packet = (old_block - map->s_partition_root) &
0171 ~(sdata->s_packet_len - 1);
0172
0173 for (j = 0; j < 4; j++)
0174 if (sdata->s_spar_map[j] != NULL) {
0175 st = (struct sparingTable *)
0176 sdata->s_spar_map[j]->b_data;
0177 break;
0178 }
0179
0180 if (!st) {
0181 ret = 1;
0182 goto out;
0183 }
0184
0185 reallocationTableLen =
0186 le16_to_cpu(st->reallocationTableLen);
0187 for (k = 0; k < reallocationTableLen; k++) {
0188 struct sparingEntry *entry = &st->mapEntry[k];
0189 u32 origLoc = le32_to_cpu(entry->origLocation);
0190
0191 if (origLoc == 0xFFFFFFFF) {
0192 for (; j < 4; j++) {
0193 int len;
0194 bh = sdata->s_spar_map[j];
0195 if (!bh)
0196 continue;
0197
0198 st = (struct sparingTable *)
0199 bh->b_data;
0200 entry->origLocation =
0201 cpu_to_le32(packet);
0202 len =
0203 sizeof(struct sparingTable) +
0204 reallocationTableLen *
0205 sizeof(struct sparingEntry);
0206 udf_update_tag((char *)st, len);
0207 mark_buffer_dirty(bh);
0208 }
0209 *new_block = le32_to_cpu(
0210 entry->mappedLocation) +
0211 ((old_block -
0212 map->s_partition_root) &
0213 (sdata->s_packet_len - 1));
0214 ret = 0;
0215 goto out;
0216 } else if (origLoc == packet) {
0217 *new_block = le32_to_cpu(
0218 entry->mappedLocation) +
0219 ((old_block -
0220 map->s_partition_root) &
0221 (sdata->s_packet_len - 1));
0222 ret = 0;
0223 goto out;
0224 } else if (origLoc > packet)
0225 break;
0226 }
0227
0228 for (l = k; l < reallocationTableLen; l++) {
0229 struct sparingEntry *entry = &st->mapEntry[l];
0230 u32 origLoc = le32_to_cpu(entry->origLocation);
0231
0232 if (origLoc != 0xFFFFFFFF)
0233 continue;
0234
0235 for (; j < 4; j++) {
0236 bh = sdata->s_spar_map[j];
0237 if (!bh)
0238 continue;
0239
0240 st = (struct sparingTable *)bh->b_data;
0241 mapEntry = st->mapEntry[l];
0242 mapEntry.origLocation =
0243 cpu_to_le32(packet);
0244 memmove(&st->mapEntry[k + 1],
0245 &st->mapEntry[k],
0246 (l - k) *
0247 sizeof(struct sparingEntry));
0248 st->mapEntry[k] = mapEntry;
0249 udf_update_tag((char *)st,
0250 sizeof(struct sparingTable) +
0251 reallocationTableLen *
0252 sizeof(struct sparingEntry));
0253 mark_buffer_dirty(bh);
0254 }
0255 *new_block =
0256 le32_to_cpu(
0257 st->mapEntry[k].mappedLocation) +
0258 ((old_block - map->s_partition_root) &
0259 (sdata->s_packet_len - 1));
0260 ret = 0;
0261 goto out;
0262 }
0263
0264 ret = 1;
0265 goto out;
0266 }
0267 }
0268
0269 if (i == sbi->s_partitions) {
0270
0271
0272 ret = 1;
0273 }
0274
0275 out:
0276 mutex_unlock(&sbi->s_alloc_mutex);
0277 return ret;
0278 }
0279
0280 static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
0281 uint16_t partition, uint32_t offset)
0282 {
0283 struct super_block *sb = inode->i_sb;
0284 struct udf_part_map *map;
0285 struct kernel_lb_addr eloc;
0286 uint32_t elen;
0287 sector_t ext_offset;
0288 struct extent_position epos = {};
0289 uint32_t phyblock;
0290
0291 if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
0292 (EXT_RECORDED_ALLOCATED >> 30))
0293 phyblock = 0xFFFFFFFF;
0294 else {
0295 map = &UDF_SB(sb)->s_partmaps[partition];
0296
0297 phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
0298 map->s_type_specific.s_metadata.s_phys_partition_ref,
0299 ext_offset + offset);
0300 }
0301
0302 brelse(epos.bh);
0303 return phyblock;
0304 }
0305
0306 uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
0307 uint16_t partition, uint32_t offset)
0308 {
0309 struct udf_sb_info *sbi = UDF_SB(sb);
0310 struct udf_part_map *map;
0311 struct udf_meta_data *mdata;
0312 uint32_t retblk;
0313 struct inode *inode;
0314
0315 udf_debug("READING from METADATA\n");
0316
0317 map = &sbi->s_partmaps[partition];
0318 mdata = &map->s_type_specific.s_metadata;
0319 inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
0320
0321 if (!inode)
0322 return 0xFFFFFFFF;
0323
0324 retblk = udf_try_read_meta(inode, block, partition, offset);
0325 if (retblk == 0xFFFFFFFF && mdata->s_metadata_fe) {
0326 udf_warn(sb, "error reading from METADATA, trying to read from MIRROR\n");
0327 if (!(mdata->s_flags & MF_MIRROR_FE_LOADED)) {
0328 mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
0329 mdata->s_mirror_file_loc,
0330 mdata->s_phys_partition_ref);
0331 if (IS_ERR(mdata->s_mirror_fe))
0332 mdata->s_mirror_fe = NULL;
0333 mdata->s_flags |= MF_MIRROR_FE_LOADED;
0334 }
0335
0336 inode = mdata->s_mirror_fe;
0337 if (!inode)
0338 return 0xFFFFFFFF;
0339 retblk = udf_try_read_meta(inode, block, partition, offset);
0340 }
0341
0342 return retblk;
0343 }