partition.c 6.7 KB

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