partition.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, uint16_t partition, uint32_t offset)
  29. {
  30. if (partition >= UDF_SB_NUMPARTS(sb))
  31. {
  32. udf_debug("block=%d, partition=%d, offset=%d: invalid partition\n",
  33. block, partition, offset);
  34. return 0xFFFFFFFF;
  35. }
  36. if (UDF_SB_PARTFUNC(sb, partition))
  37. return UDF_SB_PARTFUNC(sb, partition)(sb, block, partition, offset);
  38. else
  39. return UDF_SB_PARTROOT(sb, partition) + block + offset;
  40. }
  41. uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block, uint16_t partition, uint32_t offset)
  42. {
  43. struct buffer_head *bh = NULL;
  44. uint32_t newblock;
  45. uint32_t index;
  46. uint32_t loc;
  47. index = (sb->s_blocksize - UDF_SB_TYPEVIRT(sb,partition).s_start_offset) / sizeof(uint32_t);
  48. if (block > UDF_SB_TYPEVIRT(sb,partition).s_num_entries)
  49. {
  50. udf_debug("Trying to access block beyond end of VAT (%d max %d)\n",
  51. block, UDF_SB_TYPEVIRT(sb,partition).s_num_entries);
  52. return 0xFFFFFFFF;
  53. }
  54. if (block >= index)
  55. {
  56. block -= index;
  57. newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
  58. index = block % (sb->s_blocksize / sizeof(uint32_t));
  59. }
  60. else
  61. {
  62. newblock = 0;
  63. index = UDF_SB_TYPEVIRT(sb,partition).s_start_offset / sizeof(uint32_t) + block;
  64. }
  65. loc = udf_block_map(UDF_SB_VAT(sb), newblock);
  66. if (!(bh = sb_bread(sb, loc)))
  67. {
  68. udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
  69. sb, block, partition, loc, index);
  70. return 0xFFFFFFFF;
  71. }
  72. loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
  73. udf_release_data(bh);
  74. if (UDF_I_LOCATION(UDF_SB_VAT(sb)).partitionReferenceNum == partition)
  75. {
  76. udf_debug("recursive call to udf_get_pblock!\n");
  77. return 0xFFFFFFFF;
  78. }
  79. return udf_get_pblock(sb, loc, UDF_I_LOCATION(UDF_SB_VAT(sb)).partitionReferenceNum, offset);
  80. }
  81. inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block, uint16_t partition, uint32_t offset)
  82. {
  83. return udf_get_pblock_virt15(sb, block, partition, offset);
  84. }
  85. uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block, uint16_t partition, uint32_t offset)
  86. {
  87. int i;
  88. struct sparingTable *st = NULL;
  89. uint32_t packet = (block + offset) & ~(UDF_SB_TYPESPAR(sb,partition).s_packet_len - 1);
  90. for (i=0; i<4; i++)
  91. {
  92. if (UDF_SB_TYPESPAR(sb,partition).s_spar_map[i] != NULL)
  93. {
  94. st = (struct sparingTable *)UDF_SB_TYPESPAR(sb,partition).s_spar_map[i]->b_data;
  95. break;
  96. }
  97. }
  98. if (st)
  99. {
  100. for (i=0; i<le16_to_cpu(st->reallocationTableLen); i++)
  101. {
  102. if (le32_to_cpu(st->mapEntry[i].origLocation) >= 0xFFFFFFF0)
  103. break;
  104. else if (le32_to_cpu(st->mapEntry[i].origLocation) == packet)
  105. {
  106. return le32_to_cpu(st->mapEntry[i].mappedLocation) +
  107. ((block + offset) & (UDF_SB_TYPESPAR(sb,partition).s_packet_len - 1));
  108. }
  109. else if (le32_to_cpu(st->mapEntry[i].origLocation) > packet)
  110. break;
  111. }
  112. }
  113. return UDF_SB_PARTROOT(sb,partition) + block + offset;
  114. }
  115. int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
  116. {
  117. struct udf_sparing_data *sdata;
  118. struct sparingTable *st = NULL;
  119. struct sparingEntry mapEntry;
  120. uint32_t packet;
  121. int i, j, k, l;
  122. for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
  123. {
  124. if (old_block > UDF_SB_PARTROOT(sb,i) &&
  125. old_block < UDF_SB_PARTROOT(sb,i) + UDF_SB_PARTLEN(sb,i))
  126. {
  127. sdata = &UDF_SB_TYPESPAR(sb,i);
  128. packet = (old_block - UDF_SB_PARTROOT(sb,i)) & ~(sdata->s_packet_len - 1);
  129. for (j=0; j<4; j++)
  130. {
  131. if (UDF_SB_TYPESPAR(sb,i).s_spar_map[j] != NULL)
  132. {
  133. st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
  134. break;
  135. }
  136. }
  137. if (!st)
  138. return 1;
  139. for (k=0; k<le16_to_cpu(st->reallocationTableLen); k++)
  140. {
  141. if (le32_to_cpu(st->mapEntry[k].origLocation) == 0xFFFFFFFF)
  142. {
  143. for (; j<4; j++)
  144. {
  145. if (sdata->s_spar_map[j])
  146. {
  147. st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
  148. st->mapEntry[k].origLocation = cpu_to_le32(packet);
  149. udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry));
  150. mark_buffer_dirty(sdata->s_spar_map[j]);
  151. }
  152. }
  153. *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
  154. ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1));
  155. return 0;
  156. }
  157. else if (le32_to_cpu(st->mapEntry[k].origLocation) == packet)
  158. {
  159. *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
  160. ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1));
  161. return 0;
  162. }
  163. else if (le32_to_cpu(st->mapEntry[k].origLocation) > packet)
  164. break;
  165. }
  166. for (l=k; l<le16_to_cpu(st->reallocationTableLen); l++)
  167. {
  168. if (le32_to_cpu(st->mapEntry[l].origLocation) == 0xFFFFFFFF)
  169. {
  170. for (; j<4; j++)
  171. {
  172. if (sdata->s_spar_map[j])
  173. {
  174. st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
  175. mapEntry = st->mapEntry[l];
  176. mapEntry.origLocation = cpu_to_le32(packet);
  177. memmove(&st->mapEntry[k+1], &st->mapEntry[k], (l-k)*sizeof(struct sparingEntry));
  178. st->mapEntry[k] = mapEntry;
  179. udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry));
  180. mark_buffer_dirty(sdata->s_spar_map[j]);
  181. }
  182. }
  183. *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
  184. ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1));
  185. return 0;
  186. }
  187. }
  188. return 1;
  189. }
  190. }
  191. if (i == UDF_SB_NUMPARTS(sb))
  192. {
  193. /* outside of partitions */
  194. /* for now, fail =) */
  195. return 1;
  196. }
  197. return 0;
  198. }