partition.c 8.3 KB

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