partition.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. inline 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;
  54. map = &sbi->s_partmaps[partition];
  55. vdata = &map->s_type_specific.s_virtual;
  56. index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
  57. if (block > vdata->s_num_entries) {
  58. udf_debug("Trying to access block beyond end of VAT "
  59. "(%d max %d)\n", block, vdata->s_num_entries);
  60. return 0xFFFFFFFF;
  61. }
  62. if (block >= index) {
  63. block -= index;
  64. newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
  65. index = block % (sb->s_blocksize / sizeof(uint32_t));
  66. } else {
  67. newblock = 0;
  68. index = vdata->s_start_offset / sizeof(uint32_t) + block;
  69. }
  70. loc = udf_block_map(sbi->s_vat_inode, newblock);
  71. bh = sb_bread(sb, loc);
  72. if (!bh) {
  73. udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
  74. sb, block, partition, loc, index);
  75. return 0xFFFFFFFF;
  76. }
  77. loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
  78. brelse(bh);
  79. iinfo = UDF_I(sbi->s_vat_inode);
  80. if (iinfo->i_location.partitionReferenceNum == partition) {
  81. udf_debug("recursive call to udf_get_pblock!\n");
  82. return 0xFFFFFFFF;
  83. }
  84. return udf_get_pblock(sb, loc,
  85. iinfo->i_location.partitionReferenceNum,
  86. offset);
  87. }
  88. inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
  89. uint16_t partition, uint32_t offset)
  90. {
  91. return udf_get_pblock_virt15(sb, block, partition, offset);
  92. }
  93. uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
  94. uint16_t partition, uint32_t offset)
  95. {
  96. int i;
  97. struct sparingTable *st = NULL;
  98. struct udf_sb_info *sbi = UDF_SB(sb);
  99. struct udf_part_map *map;
  100. uint32_t packet;
  101. struct udf_sparing_data *sdata;
  102. map = &sbi->s_partmaps[partition];
  103. sdata = &map->s_type_specific.s_sparing;
  104. packet = (block + offset) & ~(sdata->s_packet_len - 1);
  105. for (i = 0; i < 4; i++) {
  106. if (sdata->s_spar_map[i] != NULL) {
  107. st = (struct sparingTable *)
  108. sdata->s_spar_map[i]->b_data;
  109. break;
  110. }
  111. }
  112. if (st) {
  113. for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
  114. struct sparingEntry *entry = &st->mapEntry[i];
  115. u32 origLoc = le32_to_cpu(entry->origLocation);
  116. if (origLoc >= 0xFFFFFFF0)
  117. break;
  118. else if (origLoc == packet)
  119. return le32_to_cpu(entry->mappedLocation) +
  120. ((block + offset) &
  121. (sdata->s_packet_len - 1));
  122. else if (origLoc > packet)
  123. break;
  124. }
  125. }
  126. return map->s_partition_root + block + offset;
  127. }
  128. int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
  129. {
  130. struct udf_sparing_data *sdata;
  131. struct sparingTable *st = NULL;
  132. struct sparingEntry mapEntry;
  133. uint32_t packet;
  134. int i, j, k, l;
  135. struct udf_sb_info *sbi = UDF_SB(sb);
  136. u16 reallocationTableLen;
  137. struct buffer_head *bh;
  138. for (i = 0; i < sbi->s_partitions; i++) {
  139. struct udf_part_map *map = &sbi->s_partmaps[i];
  140. if (old_block > map->s_partition_root &&
  141. old_block < map->s_partition_root + map->s_partition_len) {
  142. sdata = &map->s_type_specific.s_sparing;
  143. packet = (old_block - map->s_partition_root) &
  144. ~(sdata->s_packet_len - 1);
  145. for (j = 0; j < 4; j++)
  146. if (sdata->s_spar_map[j] != NULL) {
  147. st = (struct sparingTable *)
  148. sdata->s_spar_map[j]->b_data;
  149. break;
  150. }
  151. if (!st)
  152. return 1;
  153. reallocationTableLen =
  154. le16_to_cpu(st->reallocationTableLen);
  155. for (k = 0; k < reallocationTableLen; k++) {
  156. struct sparingEntry *entry = &st->mapEntry[k];
  157. u32 origLoc = le32_to_cpu(entry->origLocation);
  158. if (origLoc == 0xFFFFFFFF) {
  159. for (; j < 4; j++) {
  160. int len;
  161. bh = sdata->s_spar_map[j];
  162. if (!bh)
  163. continue;
  164. st = (struct sparingTable *)
  165. bh->b_data;
  166. entry->origLocation =
  167. cpu_to_le32(packet);
  168. len =
  169. sizeof(struct sparingTable) +
  170. reallocationTableLen *
  171. sizeof(struct sparingEntry);
  172. udf_update_tag((char *)st, len);
  173. mark_buffer_dirty(bh);
  174. }
  175. *new_block = le32_to_cpu(
  176. entry->mappedLocation) +
  177. ((old_block -
  178. map->s_partition_root) &
  179. (sdata->s_packet_len - 1));
  180. return 0;
  181. } else if (origLoc == packet) {
  182. *new_block = le32_to_cpu(
  183. entry->mappedLocation) +
  184. ((old_block -
  185. map->s_partition_root) &
  186. (sdata->s_packet_len - 1));
  187. return 0;
  188. } else if (origLoc > packet)
  189. break;
  190. }
  191. for (l = k; l < reallocationTableLen; l++) {
  192. struct sparingEntry *entry = &st->mapEntry[l];
  193. u32 origLoc = le32_to_cpu(entry->origLocation);
  194. if (origLoc != 0xFFFFFFFF)
  195. continue;
  196. for (; j < 4; j++) {
  197. bh = sdata->s_spar_map[j];
  198. if (!bh)
  199. continue;
  200. st = (struct sparingTable *)bh->b_data;
  201. mapEntry = st->mapEntry[l];
  202. mapEntry.origLocation =
  203. cpu_to_le32(packet);
  204. memmove(&st->mapEntry[k + 1],
  205. &st->mapEntry[k],
  206. (l - k) *
  207. sizeof(struct sparingEntry));
  208. st->mapEntry[k] = mapEntry;
  209. udf_update_tag((char *)st,
  210. sizeof(struct sparingTable) +
  211. reallocationTableLen *
  212. sizeof(struct sparingEntry));
  213. mark_buffer_dirty(bh);
  214. }
  215. *new_block =
  216. le32_to_cpu(
  217. st->mapEntry[k].mappedLocation) +
  218. ((old_block - map->s_partition_root) &
  219. (sdata->s_packet_len - 1));
  220. return 0;
  221. }
  222. return 1;
  223. } /* if old_block */
  224. }
  225. if (i == sbi->s_partitions) {
  226. /* outside of partitions */
  227. /* for now, fail =) */
  228. return 1;
  229. }
  230. return 0;
  231. }