partition.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * partition.c
  3. *
  4. * PURPOSE
  5. * Partition handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * CONTACTS
  8. * E-mail regarding any portion of the Linux UDF file system should be
  9. * directed to the development team mailing list (run by majordomo):
  10. * linux_udf@hpesjro.fc.hp.com
  11. *
  12. * COPYRIGHT
  13. * This file is distributed under the terms of the GNU General Public
  14. * License (GPL). Copies of the GPL can be obtained from:
  15. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  16. * Each contributing author retains all rights to their own work.
  17. *
  18. * (C) 1998-2001 Ben Fennema
  19. *
  20. * HISTORY
  21. *
  22. * 12/06/98 blf Created file.
  23. *
  24. */
  25. #include "udfdecl.h"
  26. #include "udf_sb.h"
  27. #include "udf_i.h"
  28. #include <linux/fs.h>
  29. #include <linux/string.h>
  30. #include <linux/udf_fs.h>
  31. #include <linux/slab.h>
  32. #include <linux/buffer_head.h>
  33. inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block, uint16_t partition, uint32_t offset)
  34. {
  35. if (partition >= UDF_SB_NUMPARTS(sb))
  36. {
  37. udf_debug("block=%d, partition=%d, offset=%d: invalid partition\n",
  38. block, partition, offset);
  39. return 0xFFFFFFFF;
  40. }
  41. if (UDF_SB_PARTFUNC(sb, partition))
  42. return UDF_SB_PARTFUNC(sb, partition)(sb, block, partition, offset);
  43. else
  44. return UDF_SB_PARTROOT(sb, partition) + block + offset;
  45. }
  46. uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block, uint16_t partition, uint32_t offset)
  47. {
  48. struct buffer_head *bh = NULL;
  49. uint32_t newblock;
  50. uint32_t index;
  51. uint32_t loc;
  52. index = (sb->s_blocksize - UDF_SB_TYPEVIRT(sb,partition).s_start_offset) / sizeof(uint32_t);
  53. if (block > UDF_SB_TYPEVIRT(sb,partition).s_num_entries)
  54. {
  55. udf_debug("Trying to access block beyond end of VAT (%d max %d)\n",
  56. block, UDF_SB_TYPEVIRT(sb,partition).s_num_entries);
  57. return 0xFFFFFFFF;
  58. }
  59. if (block >= index)
  60. {
  61. block -= index;
  62. newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
  63. index = block % (sb->s_blocksize / sizeof(uint32_t));
  64. }
  65. else
  66. {
  67. newblock = 0;
  68. index = UDF_SB_TYPEVIRT(sb,partition).s_start_offset / sizeof(uint32_t) + block;
  69. }
  70. loc = udf_block_map(UDF_SB_VAT(sb), newblock);
  71. if (!(bh = sb_bread(sb, loc)))
  72. {
  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. udf_release_data(bh);
  79. if (UDF_I_LOCATION(UDF_SB_VAT(sb)).partitionReferenceNum == partition)
  80. {
  81. udf_debug("recursive call to udf_get_pblock!\n");
  82. return 0xFFFFFFFF;
  83. }
  84. return udf_get_pblock(sb, loc, UDF_I_LOCATION(UDF_SB_VAT(sb)).partitionReferenceNum, offset);
  85. }
  86. inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block, uint16_t partition, uint32_t offset)
  87. {
  88. return udf_get_pblock_virt15(sb, block, partition, offset);
  89. }
  90. uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block, uint16_t partition, uint32_t offset)
  91. {
  92. int i;
  93. struct sparingTable *st = NULL;
  94. uint32_t packet = (block + offset) & ~(UDF_SB_TYPESPAR(sb,partition).s_packet_len - 1);
  95. for (i=0; i<4; i++)
  96. {
  97. if (UDF_SB_TYPESPAR(sb,partition).s_spar_map[i] != NULL)
  98. {
  99. st = (struct sparingTable *)UDF_SB_TYPESPAR(sb,partition).s_spar_map[i]->b_data;
  100. break;
  101. }
  102. }
  103. if (st)
  104. {
  105. for (i=0; i<le16_to_cpu(st->reallocationTableLen); i++)
  106. {
  107. if (le32_to_cpu(st->mapEntry[i].origLocation) >= 0xFFFFFFF0)
  108. break;
  109. else if (le32_to_cpu(st->mapEntry[i].origLocation) == packet)
  110. {
  111. return le32_to_cpu(st->mapEntry[i].mappedLocation) +
  112. ((block + offset) & (UDF_SB_TYPESPAR(sb,partition).s_packet_len - 1));
  113. }
  114. else if (le32_to_cpu(st->mapEntry[i].origLocation) > packet)
  115. break;
  116. }
  117. }
  118. return UDF_SB_PARTROOT(sb,partition) + block + offset;
  119. }
  120. int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
  121. {
  122. struct udf_sparing_data *sdata;
  123. struct sparingTable *st = NULL;
  124. struct sparingEntry mapEntry;
  125. uint32_t packet;
  126. int i, j, k, l;
  127. for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
  128. {
  129. if (old_block > UDF_SB_PARTROOT(sb,i) &&
  130. old_block < UDF_SB_PARTROOT(sb,i) + UDF_SB_PARTLEN(sb,i))
  131. {
  132. sdata = &UDF_SB_TYPESPAR(sb,i);
  133. packet = (old_block - UDF_SB_PARTROOT(sb,i)) & ~(sdata->s_packet_len - 1);
  134. for (j=0; j<4; j++)
  135. {
  136. if (UDF_SB_TYPESPAR(sb,i).s_spar_map[j] != NULL)
  137. {
  138. st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
  139. break;
  140. }
  141. }
  142. if (!st)
  143. return 1;
  144. for (k=0; k<le16_to_cpu(st->reallocationTableLen); k++)
  145. {
  146. if (le32_to_cpu(st->mapEntry[k].origLocation) == 0xFFFFFFFF)
  147. {
  148. for (; j<4; j++)
  149. {
  150. if (sdata->s_spar_map[j])
  151. {
  152. st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
  153. st->mapEntry[k].origLocation = cpu_to_le32(packet);
  154. udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry));
  155. mark_buffer_dirty(sdata->s_spar_map[j]);
  156. }
  157. }
  158. *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
  159. ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1));
  160. return 0;
  161. }
  162. else if (le32_to_cpu(st->mapEntry[k].origLocation) == packet)
  163. {
  164. *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
  165. ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1));
  166. return 0;
  167. }
  168. else if (le32_to_cpu(st->mapEntry[k].origLocation) > packet)
  169. break;
  170. }
  171. for (l=k; l<le16_to_cpu(st->reallocationTableLen); l++)
  172. {
  173. if (le32_to_cpu(st->mapEntry[l].origLocation) == 0xFFFFFFFF)
  174. {
  175. for (; j<4; j++)
  176. {
  177. if (sdata->s_spar_map[j])
  178. {
  179. st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
  180. mapEntry = st->mapEntry[l];
  181. mapEntry.origLocation = cpu_to_le32(packet);
  182. memmove(&st->mapEntry[k+1], &st->mapEntry[k], (l-k)*sizeof(struct sparingEntry));
  183. st->mapEntry[k] = mapEntry;
  184. udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry));
  185. mark_buffer_dirty(sdata->s_spar_map[j]);
  186. }
  187. }
  188. *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
  189. ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1));
  190. return 0;
  191. }
  192. }
  193. return 1;
  194. }
  195. }
  196. if (i == UDF_SB_NUMPARTS(sb))
  197. {
  198. /* outside of partitions */
  199. /* for now, fail =) */
  200. return 1;
  201. }
  202. return 0;
  203. }