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