mst.c 6.2 KB

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