2 * mst.c - Multi sector fixup handling code. Part of the Linux-NTFS project.
4 * Copyright (c) 2000-2002 Anton Altaparmakov
6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program/include file is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (in the main directory of the Linux-NTFS
18 * distribution in the file COPYING); if not, write to the Free Software
19 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * ntfs_mst_post_read_fixup - deprotect multi sector transfer protected data
27 * @b: pointer to the data to deprotect
28 * @size: size in bytes of @b
30 * Perform the necessary post read multi sector transfer fixups and detect the
31 * presence of incomplete multi sector transfers. - In that case, overwrite the
32 * magic of the ntfs record header being processed with "BAAD" (in memory only!) * and abort processing.
34 * Return 0 on success and -1 on error, with errno set to the error code. The
35 * following error codes are defined:
36 * EINVAL Invalid arguments or invalid NTFS record in buffer @b.
37 * EIO Mulit sector transfer error was detected. Magic of the NTFS
38 * record in @b will have been set to "BAAD".
40 int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size)
42 u16 usa_ofs, usa_count, usn;
43 u16 *usa_pos, *data_pos;
45 /* Setup the variables. */
46 usa_ofs = le16_to_cpu(b->usa_ofs);
47 /* Decrement usa_count to get number of fixups. */
48 usa_count = le16_to_cpu(b->usa_count) - 1;
49 /* Size and alignment checks. */
50 if (size & (NTFS_SECTOR_SIZE - 1) || usa_ofs & 1 ||
51 usa_ofs + (usa_count * 2) > size ||
52 (size >> NTFS_SECTOR_SIZE_BITS) != usa_count) {
56 /* Position of usn in update sequence array. */
57 usa_pos = (u16*)b + usa_ofs/sizeof(u16);
59 * The update sequence number which has to be equal to each of the
60 * u16 values before they are fixed up. Note no need to care for
61 * endianness since we are comparing and moving data for on disk
62 * structures which means the data is consistent. - If it is
63 * consistenty the wrong endianness it doesn't make any difference.
67 * Position in protected data of first u16 that needs fixing up.
69 data_pos = (u16*)b + NTFS_SECTOR_SIZE/sizeof(u16) - 1;
71 * Check for incomplete multi sector transfer(s).
74 if (*data_pos != usn) {
76 * Incomplete multi sector transfer detected! )-:
77 * Set the magic to "BAAD" and return failure.
78 * Note that magic_BAAD is already converted to le32.
80 b->magic = magic_BAAD;
84 data_pos += NTFS_SECTOR_SIZE/sizeof(u16);
86 /* Re-setup the variables. */
87 usa_count = le16_to_cpu(b->usa_count) - 1;
88 data_pos = (u16*)b + NTFS_SECTOR_SIZE/sizeof(u16) - 1;
89 /* Fixup all sectors. */
92 * Increment position in usa and restore original data from
93 * the usa into the data buffer.
95 *data_pos = *(++usa_pos);
96 /* Increment position in data as well. */
97 data_pos += NTFS_SECTOR_SIZE/sizeof(u16);
103 * ntfs_mst_pre_write_fixup - apply multi sector transfer protection
104 * @b: pointer to the data to protect
105 * @size: size in bytes of @b
107 * Perform the necessary pre write multi sector transfer fixup on the data
108 * pointer to by @b of @size.
110 * Return 0 if fixups applied successfully or -1 if no fixups were performed
111 * due to errors. In that case errno i set to the error code (EINVAL).
113 * NOTE: We consider the absence / invalidity of an update sequence array to
114 * mean error. This means that you have to create a valid update sequence
115 * array header in the ntfs record before calling this function, otherwise it
116 * will fail (the header needs to contain the position of the update seqeuence
117 * array together with the number of elements in the array). You also need to
118 * initialise the update sequence number before calling this function
119 * otherwise a random word will be used (whatever was in the record at that
120 * position at that time).
122 int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size)
124 u16 usa_ofs, usa_count, usn;
125 u16 *usa_pos, *data_pos;
127 /* Sanity check + only fixup if it makes sense. */
128 if (!b || ntfs_is_baad_record(b->magic) ||
129 ntfs_is_hole_record(b->magic)) {
133 /* Setup the variables. */
134 usa_ofs = le16_to_cpu(b->usa_ofs);
135 /* Decrement usa_count to get number of fixups. */
136 usa_count = le16_to_cpu(b->usa_count) - 1;
137 /* Size and alignment checks. */
138 if (size & (NTFS_SECTOR_SIZE - 1) || usa_ofs & 1 ||
139 usa_ofs + (usa_count * 2) > size ||
140 (size >> NTFS_SECTOR_SIZE_BITS) != usa_count) {
144 /* Position of usn in update sequence array. */
145 usa_pos = (u16*)((u8*)b + usa_ofs);
147 * Cyclically increment the update sequence number
148 * (skipping 0 and -1, i.e. 0xffff).
150 usn = le16_to_cpup(usa_pos) + 1;
151 if (usn == 0xffff || !usn)
153 usn = cpu_to_le16(usn);
155 /* Position in data of first u16 that needs fixing up. */
156 data_pos = (u16*)b + NTFS_SECTOR_SIZE/sizeof(u16) - 1;
157 /* Fixup all sectors. */
158 while (usa_count--) {
160 * Increment the position in the usa and save the
161 * original data from the data buffer into the usa.
163 *(++usa_pos) = *data_pos;
164 /* Apply fixup to data. */
166 /* Increment position in data as well. */
167 data_pos += NTFS_SECTOR_SIZE/sizeof(u16);
173 * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data
174 * @b: pointer to the data to deprotect
176 * Perform the necessary post write multi sector transfer fixup, not checking
177 * for any errors, because we assume we have just used
178 * ntfs_mst_pre_write_fixup(), thus the data will be fine or we would never
181 void ntfs_mst_post_write_fixup(NTFS_RECORD *b)
183 u16 *usa_pos, *data_pos;
185 u16 usa_ofs = le16_to_cpu(b->usa_ofs);
186 u16 usa_count = le16_to_cpu(b->usa_count) - 1;
188 /* Position of usn in update sequence array. */
189 usa_pos = (u16*)b + usa_ofs/sizeof(u16);
191 /* Position in protected data of first u16 that needs fixing up. */
192 data_pos = (u16*)b + NTFS_SECTOR_SIZE/sizeof(u16) - 1;
194 /* Fixup all sectors. */
195 while (usa_count--) {
197 * Increment position in usa and restore original data from
198 * the usa into the data buffer.
200 *data_pos = *(++usa_pos);
202 /* Increment position in data as well. */
203 data_pos += NTFS_SECTOR_SIZE/sizeof(u16);