mst.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * mst.c - NTFS multi sector transfer protection handling code. Part of the
  3. * Linux-NTFS project.
  4. *
  5. * Copyright (c) 2001-2004 Anton Altaparmakov
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "ntfs.h"
  23. /**
  24. * post_read_mst_fixup - deprotect multi sector transfer protected data
  25. * @b: pointer to the data to deprotect
  26. * @size: size in bytes of @b
  27. *
  28. * Perform the necessary post read multi sector transfer fixup and detect the
  29. * presence of incomplete multi sector transfers. - In that case, overwrite the
  30. * magic of the ntfs record header being processed with "BAAD" (in memory only!)
  31. * and abort processing.
  32. *
  33. * Return 0 on success and -EINVAL on error ("BAAD" magic will be present).
  34. *
  35. * NOTE: We consider the absence / invalidity of an update sequence array to
  36. * mean that the structure is not protected at all and hence doesn't need to
  37. * be fixed up. Thus, we return success and not failure in this case. This is
  38. * in contrast to pre_write_mst_fixup(), see below.
  39. */
  40. int post_read_mst_fixup(NTFS_RECORD *b, const u32 size)
  41. {
  42. u16 usa_ofs, usa_count, usn;
  43. u16 *usa_pos, *data_pos;
  44. /* Setup the variables. */
  45. usa_ofs = le16_to_cpu(b->usa_ofs);
  46. /* Decrement usa_count to get number of fixups. */
  47. usa_count = le16_to_cpu(b->usa_count) - 1;
  48. /* Size and alignment checks. */
  49. if ( size & (NTFS_BLOCK_SIZE - 1) ||
  50. usa_ofs & 1 ||
  51. usa_ofs + (usa_count * 2) > size ||
  52. (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
  53. return 0;
  54. /* Position of usn in update sequence array. */
  55. usa_pos = (u16*)b + usa_ofs/sizeof(u16);
  56. /*
  57. * The update sequence number which has to be equal to each of the
  58. * u16 values before they are fixed up. Note no need to care for
  59. * endianness since we are comparing and moving data for on disk
  60. * structures which means the data is consistent. - If it is
  61. * consistenty the wrong endianness it doesn't make any difference.
  62. */
  63. usn = *usa_pos;
  64. /*
  65. * Position in protected data of first u16 that needs fixing up.
  66. */
  67. data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
  68. /*
  69. * Check for incomplete multi sector transfer(s).
  70. */
  71. while (usa_count--) {
  72. if (*data_pos != usn) {
  73. /*
  74. * Incomplete multi sector transfer detected! )-:
  75. * Set the magic to "BAAD" and return failure.
  76. * Note that magic_BAAD is already converted to le32.
  77. */
  78. b->magic = magic_BAAD;
  79. return -EINVAL;
  80. }
  81. data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
  82. }
  83. /* Re-setup the variables. */
  84. usa_count = le16_to_cpu(b->usa_count) - 1;
  85. data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
  86. /* Fixup all sectors. */
  87. while (usa_count--) {
  88. /*
  89. * Increment position in usa and restore original data from
  90. * the usa into the data buffer.
  91. */
  92. *data_pos = *(++usa_pos);
  93. /* Increment position in data as well. */
  94. data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
  95. }
  96. return 0;
  97. }
  98. /**
  99. * pre_write_mst_fixup - apply multi sector transfer protection
  100. * @b: pointer to the data to protect
  101. * @size: size in bytes of @b
  102. *
  103. * Perform the necessary pre write multi sector transfer fixup on the data
  104. * pointer to by @b of @size.
  105. *
  106. * Return 0 if fixup applied (success) or -EINVAL if no fixup was performed
  107. * (assumed not needed). This is in contrast to post_read_mst_fixup() above.
  108. *
  109. * NOTE: We consider the absence / invalidity of an update sequence array to
  110. * mean that the structure is not subject to protection and hence doesn't need
  111. * to be fixed up. This means that you have to create a valid update sequence
  112. * array header in the ntfs record before calling this function, otherwise it
  113. * will fail (the header needs to contain the position of the update sequence
  114. * array together with the number of elements in the array). You also need to
  115. * initialise the update sequence number before calling this function
  116. * otherwise a random word will be used (whatever was in the record at that
  117. * position at that time).
  118. */
  119. int pre_write_mst_fixup(NTFS_RECORD *b, const u32 size)
  120. {
  121. le16 *usa_pos, *data_pos;
  122. u16 usa_ofs, usa_count, usn;
  123. le16 le_usn;
  124. /* Sanity check + only fixup if it makes sense. */
  125. if (!b || ntfs_is_baad_record(b->magic) ||
  126. ntfs_is_hole_record(b->magic))
  127. return -EINVAL;
  128. /* Setup the variables. */
  129. usa_ofs = le16_to_cpu(b->usa_ofs);
  130. /* Decrement usa_count to get number of fixups. */
  131. usa_count = le16_to_cpu(b->usa_count) - 1;
  132. /* Size and alignment checks. */
  133. if ( size & (NTFS_BLOCK_SIZE - 1) ||
  134. usa_ofs & 1 ||
  135. usa_ofs + (usa_count * 2) > size ||
  136. (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
  137. return -EINVAL;
  138. /* Position of usn in update sequence array. */
  139. usa_pos = (le16*)((u8*)b + usa_ofs);
  140. /*
  141. * Cyclically increment the update sequence number
  142. * (skipping 0 and -1, i.e. 0xffff).
  143. */
  144. usn = le16_to_cpup(usa_pos) + 1;
  145. if (usn == 0xffff || !usn)
  146. usn = 1;
  147. le_usn = cpu_to_le16(usn);
  148. *usa_pos = le_usn;
  149. /* Position in data of first u16 that needs fixing up. */
  150. data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
  151. /* Fixup all sectors. */
  152. while (usa_count--) {
  153. /*
  154. * Increment the position in the usa and save the
  155. * original data from the data buffer into the usa.
  156. */
  157. *(++usa_pos) = *data_pos;
  158. /* Apply fixup to data. */
  159. *data_pos = le_usn;
  160. /* Increment position in data as well. */
  161. data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
  162. }
  163. return 0;
  164. }
  165. /**
  166. * post_write_mst_fixup - fast deprotect multi sector transfer protected data
  167. * @b: pointer to the data to deprotect
  168. *
  169. * Perform the necessary post write multi sector transfer fixup, not checking
  170. * for any errors, because we assume we have just used pre_write_mst_fixup(),
  171. * thus the data will be fine or we would never have gotten here.
  172. */
  173. void post_write_mst_fixup(NTFS_RECORD *b)
  174. {
  175. le16 *usa_pos, *data_pos;
  176. u16 usa_ofs = le16_to_cpu(b->usa_ofs);
  177. u16 usa_count = le16_to_cpu(b->usa_count) - 1;
  178. /* Position of usn in update sequence array. */
  179. usa_pos = (le16*)b + usa_ofs/sizeof(le16);
  180. /* Position in protected data of first u16 that needs fixing up. */
  181. data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
  182. /* Fixup all sectors. */
  183. while (usa_count--) {
  184. /*
  185. * Increment position in usa and restore original data from
  186. * the usa into the data buffer.
  187. */
  188. *data_pos = *(++usa_pos);
  189. /* Increment position in data as well. */
  190. data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
  191. }
  192. }