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