partition.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/buffer_head.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=%d, partition=%d, offset=%d: "
  33. "invalid partition\n", 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 "
  57. "(%d max %d)\n", 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,%d,%d) VAT: %d[%d]\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. for (i = 0; i < sbi->s_partitions; i++) {
  143. struct udf_part_map *map = &sbi->s_partmaps[i];
  144. if (old_block > map->s_partition_root &&
  145. old_block < map->s_partition_root + map->s_partition_len) {
  146. sdata = &map->s_type_specific.s_sparing;
  147. packet = (old_block - map->s_partition_root) &
  148. ~(sdata->s_packet_len - 1);
  149. for (j = 0; j < 4; j++)
  150. if (sdata->s_spar_map[j] != NULL) {
  151. st = (struct sparingTable *)
  152. sdata->s_spar_map[j]->b_data;
  153. break;
  154. }
  155. if (!st)
  156. return 1;
  157. reallocationTableLen =
  158. le16_to_cpu(st->reallocationTableLen);
  159. for (k = 0; k < reallocationTableLen; k++) {
  160. struct sparingEntry *entry = &st->mapEntry[k];
  161. u32 origLoc = le32_to_cpu(entry->origLocation);
  162. if (origLoc == 0xFFFFFFFF) {
  163. for (; j < 4; j++) {
  164. int len;
  165. bh = sdata->s_spar_map[j];
  166. if (!bh)
  167. continue;
  168. st = (struct sparingTable *)
  169. bh->b_data;
  170. entry->origLocation =
  171. cpu_to_le32(packet);
  172. len =
  173. sizeof(struct sparingTable) +
  174. reallocationTableLen *
  175. sizeof(struct sparingEntry);
  176. udf_update_tag((char *)st, len);
  177. mark_buffer_dirty(bh);
  178. }
  179. *new_block = le32_to_cpu(
  180. entry->mappedLocation) +
  181. ((old_block -
  182. map->s_partition_root) &
  183. (sdata->s_packet_len - 1));
  184. return 0;
  185. } else if (origLoc == packet) {
  186. *new_block = le32_to_cpu(
  187. entry->mappedLocation) +
  188. ((old_block -
  189. map->s_partition_root) &
  190. (sdata->s_packet_len - 1));
  191. return 0;
  192. } else if (origLoc > packet)
  193. break;
  194. }
  195. for (l = k; l < reallocationTableLen; l++) {
  196. struct sparingEntry *entry = &st->mapEntry[l];
  197. u32 origLoc = le32_to_cpu(entry->origLocation);
  198. if (origLoc != 0xFFFFFFFF)
  199. continue;
  200. for (; j < 4; j++) {
  201. bh = sdata->s_spar_map[j];
  202. if (!bh)
  203. continue;
  204. st = (struct sparingTable *)bh->b_data;
  205. mapEntry = st->mapEntry[l];
  206. mapEntry.origLocation =
  207. cpu_to_le32(packet);
  208. memmove(&st->mapEntry[k + 1],
  209. &st->mapEntry[k],
  210. (l - k) *
  211. sizeof(struct sparingEntry));
  212. st->mapEntry[k] = mapEntry;
  213. udf_update_tag((char *)st,
  214. sizeof(struct sparingTable) +
  215. reallocationTableLen *
  216. sizeof(struct sparingEntry));
  217. mark_buffer_dirty(bh);
  218. }
  219. *new_block =
  220. le32_to_cpu(
  221. st->mapEntry[k].mappedLocation) +
  222. ((old_block - map->s_partition_root) &
  223. (sdata->s_packet_len - 1));
  224. return 0;
  225. }
  226. return 1;
  227. } /* if old_block */
  228. }
  229. if (i == sbi->s_partitions) {
  230. /* outside of partitions */
  231. /* for now, fail =) */
  232. return 1;
  233. }
  234. return 0;
  235. }
  236. static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
  237. uint16_t partition, uint32_t offset)
  238. {
  239. struct super_block *sb = inode->i_sb;
  240. struct udf_part_map *map;
  241. struct kernel_lb_addr eloc;
  242. uint32_t elen;
  243. sector_t ext_offset;
  244. struct extent_position epos = {};
  245. uint32_t phyblock;
  246. if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
  247. (EXT_RECORDED_ALLOCATED >> 30))
  248. phyblock = 0xFFFFFFFF;
  249. else {
  250. map = &UDF_SB(sb)->s_partmaps[partition];
  251. /* map to sparable/physical partition desc */
  252. phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
  253. map->s_partition_num, ext_offset + offset);
  254. }
  255. brelse(epos.bh);
  256. return phyblock;
  257. }
  258. uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
  259. uint16_t partition, uint32_t offset)
  260. {
  261. struct udf_sb_info *sbi = UDF_SB(sb);
  262. struct udf_part_map *map;
  263. struct udf_meta_data *mdata;
  264. uint32_t retblk;
  265. struct inode *inode;
  266. udf_debug("READING from METADATA\n");
  267. map = &sbi->s_partmaps[partition];
  268. mdata = &map->s_type_specific.s_metadata;
  269. inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
  270. /* We shouldn't mount such media... */
  271. BUG_ON(!inode);
  272. retblk = udf_try_read_meta(inode, block, partition, offset);
  273. if (retblk == 0xFFFFFFFF) {
  274. udf_warning(sb, __func__, "error reading from METADATA, "
  275. "trying to read from MIRROR");
  276. inode = mdata->s_mirror_fe;
  277. if (!inode)
  278. return 0xFFFFFFFF;
  279. retblk = udf_try_read_meta(inode, block, partition, offset);
  280. }
  281. return retblk;
  282. }