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