partition.c 8.4 KB

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