2 * mst.c - Multi sector fixup handling code. Originated from the Linux-NTFS project.
4 * Copyright (c) 2000-2004 Anton Altaparmakov
5 * Copyright (c) 2006-2009 Szabolcs Szakacsits
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the NTFS-3G
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
35 * Basic validation of a NTFS multi-sector record. The record size must be a
36 * multiple of the logical sector size; and the update sequence array must be
37 * properly aligned, of the expected length, and must end before the last le16
38 * in the first logical sector.
41 is_valid_record(u32 size
, u16 usa_ofs
, u16 usa_count
)
43 return size
% NTFS_BLOCK_SIZE
== 0 &&
45 usa_count
== 1 + (size
/ NTFS_BLOCK_SIZE
) &&
46 usa_ofs
+ ((u32
)usa_count
* 2) <= NTFS_BLOCK_SIZE
- 2;
50 * ntfs_mst_post_read_fixup - deprotect multi sector transfer protected data
51 * @b: pointer to the data to deprotect
52 * @size: size in bytes of @b
54 * Perform the necessary post read multi sector transfer fixups and detect the
55 * presence of incomplete multi sector transfers. - In that case, overwrite the
56 * magic of the ntfs record header being processed with "BAAD" (in memory only!)
57 * and abort processing.
59 * Return 0 on success and -1 on error, with errno set to the error code. The
60 * following error codes are defined:
61 * EINVAL Invalid arguments or invalid NTFS record in buffer @b.
62 * EIO Multi sector transfer error was detected. Magic of the NTFS
63 * record in @b will have been set to "BAAD".
65 int ntfs_mst_post_read_fixup_warn(NTFS_RECORD
*b
, const u32 size
,
68 u16 usa_ofs
, usa_count
, usn
;
69 u16
*usa_pos
, *data_pos
;
71 ntfs_log_trace("Entering\n");
73 /* Setup the variables. */
74 usa_ofs
= le16_to_cpu(b
->usa_ofs
);
75 usa_count
= le16_to_cpu(b
->usa_count
);
77 if (!is_valid_record(size
, usa_ofs
, usa_count
)) {
80 ntfs_log_perror("%s: magic: 0x%08lx size: %ld "
81 " usa_ofs: %d usa_count: %u",
83 (long)le32_to_cpu(*(le32
*)b
),
84 (long)size
, (int)usa_ofs
,
85 (unsigned int)usa_count
);
89 /* Position of usn in update sequence array. */
90 usa_pos
= (u16
*)b
+ usa_ofs
/sizeof(u16
);
92 * The update sequence number which has to be equal to each of the
93 * u16 values before they are fixed up. Note no need to care for
94 * endianness since we are comparing and moving data for on disk
95 * structures which means the data is consistent. - If it is
96 * consistency the wrong endianness it doesn't make any difference.
100 * Position in protected data of first u16 that needs fixing up.
102 data_pos
= (u16
*)b
+ NTFS_BLOCK_SIZE
/sizeof(u16
) - 1;
104 * Check for incomplete multi sector transfer(s).
106 while (--usa_count
) {
107 if (*data_pos
!= usn
) {
109 * Incomplete multi sector transfer detected! )-:
110 * Set the magic to "BAAD" and return failure.
111 * Note that magic_BAAD is already converted to le32.
114 ntfs_log_perror("Incomplete multi-sector transfer: "
115 "magic: 0x%08x size: %d usa_ofs: %d usa_count:"
116 " %d data: %d usn: %d", le32_to_cpu(*(le32
*)b
), size
,
117 usa_ofs
, usa_count
, *data_pos
, usn
);
118 b
->magic
= magic_BAAD
;
121 data_pos
+= NTFS_BLOCK_SIZE
/sizeof(u16
);
123 /* Re-setup the variables. */
124 usa_count
= le16_to_cpu(b
->usa_count
);
125 data_pos
= (u16
*)b
+ NTFS_BLOCK_SIZE
/sizeof(u16
) - 1;
126 /* Fixup all sectors. */
127 while (--usa_count
) {
129 * Increment position in usa and restore original data from
130 * the usa into the data buffer.
132 *data_pos
= *(++usa_pos
);
133 /* Increment position in data as well. */
134 data_pos
+= NTFS_BLOCK_SIZE
/sizeof(u16
);
140 * Deprotect multi sector transfer protected data
141 * with a warning if an error is found.
144 int ntfs_mst_post_read_fixup(NTFS_RECORD
*b
, const u32 size
)
146 return (ntfs_mst_post_read_fixup_warn(b
,size
,TRUE
));
150 * ntfs_mst_pre_write_fixup - apply multi sector transfer protection
151 * @b: pointer to the data to protect
152 * @size: size in bytes of @b
154 * Perform the necessary pre write multi sector transfer fixup on the data
155 * pointer to by @b of @size.
157 * Return 0 if fixups applied successfully or -1 if no fixups were performed
158 * due to errors. In that case errno i set to the error code (EINVAL).
160 * NOTE: We consider the absence / invalidity of an update sequence array to
161 * mean error. This means that you have to create a valid update sequence
162 * array header in the ntfs record before calling this function, otherwise it
163 * will fail (the header needs to contain the position of the update sequence
164 * array together with the number of elements in the array). You also need to
165 * initialise the update sequence number before calling this function
166 * otherwise a random word will be used (whatever was in the record at that
167 * position at that time).
169 int ntfs_mst_pre_write_fixup(NTFS_RECORD
*b
, const u32 size
)
171 u16 usa_ofs
, usa_count
, usn
;
173 le16
*usa_pos
, *data_pos
;
175 ntfs_log_trace("Entering\n");
177 /* Sanity check + only fixup if it makes sense. */
178 if (!b
|| ntfs_is_baad_record(b
->magic
) ||
179 ntfs_is_hole_record(b
->magic
)) {
181 ntfs_log_perror("%s: bad argument", __FUNCTION__
);
184 /* Setup the variables. */
185 usa_ofs
= le16_to_cpu(b
->usa_ofs
);
186 usa_count
= le16_to_cpu(b
->usa_count
);
188 if (!is_valid_record(size
, usa_ofs
, usa_count
)) {
190 ntfs_log_perror("%s", __FUNCTION__
);
193 /* Position of usn in update sequence array. */
194 usa_pos
= (le16
*)((u8
*)b
+ usa_ofs
);
196 * Cyclically increment the update sequence number
197 * (skipping 0 and -1, i.e. 0xffff).
199 usn
= le16_to_cpup(usa_pos
) + 1;
200 if (usn
== 0xffff || !usn
)
202 le_usn
= cpu_to_le16(usn
);
204 /* Position in data of first le16 that needs fixing up. */
205 data_pos
= (le16
*)b
+ NTFS_BLOCK_SIZE
/sizeof(le16
) - 1;
206 /* Fixup all sectors. */
207 while (--usa_count
) {
209 * Increment the position in the usa and save the
210 * original data from the data buffer into the usa.
212 *(++usa_pos
) = *data_pos
;
213 /* Apply fixup to data. */
215 /* Increment position in data as well. */
216 data_pos
+= NTFS_BLOCK_SIZE
/sizeof(le16
);
222 * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data
223 * @b: pointer to the data to deprotect
225 * Perform the necessary post write multi sector transfer fixup, not checking
226 * for any errors, because we assume we have just used
227 * ntfs_mst_pre_write_fixup(), thus the data will be fine or we would never
230 void ntfs_mst_post_write_fixup(NTFS_RECORD
*b
)
232 u16
*usa_pos
, *data_pos
;
234 u16 usa_ofs
= le16_to_cpu(b
->usa_ofs
);
235 u16 usa_count
= le16_to_cpu(b
->usa_count
);
237 ntfs_log_trace("Entering\n");
239 /* Position of usn in update sequence array. */
240 usa_pos
= (u16
*)b
+ usa_ofs
/sizeof(u16
);
242 /* Position in protected data of first u16 that needs fixing up. */
243 data_pos
= (u16
*)b
+ NTFS_BLOCK_SIZE
/sizeof(u16
) - 1;
245 /* Fixup all sectors. */
246 while (--usa_count
) {
248 * Increment position in usa and restore original data from
249 * the usa into the data buffer.
251 *data_pos
= *(++usa_pos
);
253 /* Increment position in data as well. */
254 data_pos
+= NTFS_BLOCK_SIZE
/sizeof(u16
);