partition.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * partition.c
  3. *
  4. * PURPOSE
  5. * Partition handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * 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. *
  17. * 12/06/98 blf Created file.
  18. *
  19. */
  20. #include "udfdecl.h"
  21. #include "udf_sb.h"
  22. #include "udf_i.h"
  23. #include <linux/fs.h>
  24. #include <linux/string.h>
  25. #include <linux/mutex.h>
  26. uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
  27. uint16_t partition, uint32_t offset)
  28. {
  29. struct udf_sb_info *sbi = UDF_SB(sb);
  30. struct udf_part_map *map;
  31. if (partition >= sbi->s_partitions) {
  32. udf_debug("block=%u, partition=%u, offset=%u: invalid partition\n",
  33. block, partition, offset);
  34. return 0xFFFFFFFF;
  35. }
  36. map = &sbi->s_partmaps[partition];
  37. if (map->s_partition_func)
  38. return map->s_partition_func(sb, block, partition, offset);
  39. else
  40. return map->s_partition_root + block + offset;
  41. }
  42. uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
  43. uint16_t partition, uint32_t offset)
  44. {
  45. struct buffer_head *bh = NULL;
  46. uint32_t newblock;
  47. uint32_t index;
  48. uint32_t loc;
  49. struct udf_sb_info *sbi = UDF_SB(sb);
  50. struct udf_part_map *map;
  51. struct udf_virtual_data *vdata;
  52. struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode);
  53. map = &sbi->s_partmaps[partition];
  54. vdata = &map->s_type_specific.s_virtual;
  55. if (block > vdata->s_num_entries) {
  56. udf_debug("Trying to access block beyond end of VAT (%u max %u)\n",
  57. block, vdata->s_num_entries);
  58. return 0xFFFFFFFF;
  59. }
  60. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  61. loc = le32_to_cpu(((__le32 *)(iinfo->i_ext.i_data +
  62. vdata->s_start_offset))[block]);
  63. goto translate;
  64. }
  65. index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
  66. if (block >= index) {
  67. block -= index;
  68. newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
  69. index = block % (sb->s_blocksize / sizeof(uint32_t));
  70. } else {
  71. newblock = 0;
  72. index = vdata->s_start_offset / sizeof(uint32_t) + block;
  73. }
  74. loc = udf_block_map(sbi->s_vat_inode, newblock);
  75. bh = sb_bread(sb, loc);
  76. if (!bh) {
  77. udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%u,%u) VAT: %u[%u]\n",
  78. sb, block, partition, loc, index);
  79. return 0xFFFFFFFF;
  80. }
  81. loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
  82. brelse(bh);
  83. translate:
  84. if (iinfo->i_location.partitionReferenceNum == partition) {
  85. udf_debug("recursive call to udf_get_pblock!\n");
  86. return 0xFFFFFFFF;
  87. }
  88. return udf_get_pblock(sb, loc,
  89. iinfo->i_location.partitionReferenceNum,
  90. offset);
  91. }
  92. inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
  93. uint16_t partition, uint32_t offset)
  94. {
  95. return udf_get_pblock_virt15(sb, block, partition, offset);
  96. }
  97. uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
  98. uint16_t partition, uint32_t offset)
  99. {
  100. int i;
  101. struct sparingTable *st = NULL;
  102. struct udf_sb_info *sbi = UDF_SB(sb);
  103. struct udf_part_map *map;
  104. uint32_t packet;
  105. struct udf_sparing_data *sdata;
  106. map = &sbi->s_partmaps[partition];
  107. sdata = &map->s_type_specific.s_sparing;
  108. packet = (block + offset) & ~(sdata->s_packet_len - 1);
  109. for (i = 0; i < 4; i++) {
  110. if (sdata->s_spar_map[i] != NULL) {
  111. st = (struct sparingTable *)
  112. sdata->s_spar_map[i]->b_data;
  113. break;
  114. }
  115. }
  116. if (st) {
  117. for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
  118. struct sparingEntry *entry = &st->mapEntry[i];
  119. u32 origLoc = le32_to_cpu(entry->origLocation);
  120. if (origLoc >= 0xFFFFFFF0)
  121. break;
  122. else if (origLoc == packet)
  123. return le32_to_cpu(entry->mappedLocation) +
  124. ((block + offset) &
  125. (sdata->s_packet_len - 1));
  126. else if (origLoc > packet)
  127. break;
  128. }
  129. }
  130. return map->s_partition_root + block + offset;
  131. }
  132. int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
  133. {
  134. struct udf_sparing_data *sdata;
  135. struct sparingTable *st = NULL;
  136. struct sparingEntry mapEntry;
  137. uint32_t packet;
  138. int i, j, k, l;
  139. struct udf_sb_info *sbi = UDF_SB(sb);
  140. u16 reallocationTableLen;
  141. struct buffer_head *bh;
  142. int ret = 0;
  143. mutex_lock(&sbi->s_alloc_mutex);
  144. for (i = 0; i < sbi->s_partitions; i++) {
  145. struct udf_part_map *map = &sbi->s_partmaps[i];
  146. if (old_block > map->s_partition_root &&
  147. old_block < map->s_partition_root + map->s_partition_len) {
  148. sdata = &map->s_type_specific.s_sparing;
  149. packet = (old_block - map->s_partition_root) &
  150. ~(sdata->s_packet_len - 1);
  151. for (j = 0; j < 4; j++)
  152. if (sdata->s_spar_map[j] != NULL) {
  153. st = (struct sparingTable *)
  154. sdata->s_spar_map[j]->b_data;
  155. break;
  156. }
  157. if (!st) {
  158. ret = 1;
  159. goto out;
  160. }
  161. reallocationTableLen =
  162. le16_to_cpu(st->reallocationTableLen);
  163. for (k = 0; k < reallocationTableLen; k++) {
  164. struct sparingEntry *entry = &st->mapEntry[k];
  165. u32 origLoc = le32_to_cpu(entry->origLocation);
  166. if (origLoc == 0xFFFFFFFF) {
  167. for (; j < 4; j++) {
  168. int len;
  169. bh = sdata->s_spar_map[j];
  170. if (!bh)
  171. continue;
  172. st = (struct sparingTable *)
  173. bh->b_data;
  174. entry->origLocation =
  175. cpu_to_le32(packet);
  176. len =
  177. sizeof(struct sparingTable) +
  178. reallocationTableLen *
  179. sizeof(struct sparingEntry);
  180. udf_update_tag((char *)st, len);
  181. mark_buffer_dirty(bh);
  182. }
  183. *new_block = le32_to_cpu(
  184. entry->mappedLocation) +
  185. ((old_block -
  186. map->s_partition_root) &
  187. (sdata->s_packet_len - 1));
  188. ret = 0;
  189. goto out;
  190. } else if (origLoc == packet) {
  191. *new_block = le32_to_cpu(
  192. entry->mappedLocation) +
  193. ((old_block -
  194. map->s_partition_root) &
  195. (sdata->s_packet_len - 1));
  196. ret = 0;
  197. goto out;
  198. } else if (origLoc > packet)
  199. break;
  200. }
  201. for (l = k; l < reallocationTableLen; l++) {
  202. struct sparingEntry *entry = &st->mapEntry[l];
  203. u32 origLoc = le32_to_cpu(entry->origLocation);
  204. if (origLoc != 0xFFFFFFFF)
  205. continue;
  206. for (; j < 4; j++) {
  207. bh = sdata->s_spar_map[j];
  208. if (!bh)
  209. continue;
  210. st = (struct sparingTable *)bh->b_data;
  211. mapEntry = st->mapEntry[l];
  212. mapEntry.origLocation =
  213. cpu_to_le32(packet);
  214. memmove(&st->mapEntry[k + 1],
  215. &st->mapEntry[k],
  216. (l - k) *
  217. sizeof(struct sparingEntry));
  218. st->mapEntry[k] = mapEntry;
  219. udf_update_tag((char *)st,
  220. sizeof(struct sparingTable) +
  221. reallocationTableLen *
  222. sizeof(struct sparingEntry));
  223. mark_buffer_dirty(bh);
  224. }
  225. *new_block =
  226. le32_to_cpu(
  227. st->mapEntry[k].mappedLocation) +
  228. ((old_block - map->s_partition_root) &
  229. (sdata->s_packet_len - 1));
  230. ret = 0;
  231. goto out;
  232. }
  233. ret = 1;
  234. goto out;
  235. } /* if old_block */
  236. }
  237. if (i == sbi->s_partitions) {
  238. /* outside of partitions */
  239. /* for now, fail =) */
  240. ret = 1;
  241. }
  242. out:
  243. mutex_unlock(&sbi->s_alloc_mutex);
  244. return ret;
  245. }
  246. static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
  247. uint16_t partition, uint32_t offset)
  248. {
  249. struct super_block *sb = inode->i_sb;
  250. struct udf_part_map *map;
  251. struct kernel_lb_addr eloc;
  252. uint32_t elen;
  253. sector_t ext_offset;
  254. struct extent_position epos = {};
  255. uint32_t phyblock;
  256. if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
  257. (EXT_RECORDED_ALLOCATED >> 30))
  258. phyblock = 0xFFFFFFFF;
  259. else {
  260. map = &UDF_SB(sb)->s_partmaps[partition];
  261. /* map to sparable/physical partition desc */
  262. phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
  263. map->s_type_specific.s_metadata.s_phys_partition_ref,
  264. ext_offset + offset);
  265. }
  266. brelse(epos.bh);
  267. return phyblock;
  268. }
  269. uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
  270. uint16_t partition, uint32_t offset)
  271. {
  272. struct udf_sb_info *sbi = UDF_SB(sb);
  273. struct udf_part_map *map;
  274. struct udf_meta_data *mdata;
  275. uint32_t retblk;
  276. struct inode *inode;
  277. udf_debug("READING from METADATA\n");
  278. map = &sbi->s_partmaps[partition];
  279. mdata = &map->s_type_specific.s_metadata;
  280. inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
  281. if (!inode)
  282. return 0xFFFFFFFF;
  283. retblk = udf_try_read_meta(inode, block, partition, offset);
  284. if (retblk == 0xFFFFFFFF && mdata->s_metadata_fe) {
  285. udf_warn(sb, "error reading from METADATA, trying to read from MIRROR\n");
  286. if (!(mdata->s_flags & MF_MIRROR_FE_LOADED)) {
  287. mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
  288. mdata->s_mirror_file_loc,
  289. mdata->s_phys_partition_ref);
  290. if (IS_ERR(mdata->s_mirror_fe))
  291. mdata->s_mirror_fe = NULL;
  292. mdata->s_flags |= MF_MIRROR_FE_LOADED;
  293. }
  294. inode = mdata->s_mirror_fe;
  295. if (!inode)
  296. return 0xFFFFFFFF;
  297. retblk = udf_try_read_meta(inode, block, partition, offset);
  298. }
  299. return retblk;
  300. }