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