Release: captive1 -> captive2cvs
[ntfsprogs.git] / libntfs / mst.c
1 /*
2  * mst.c - Multi sector fixup handling code. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2000-2002 Anton Altaparmakov
5  *
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.
10  *
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.
15  *
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
20  */
21
22 #include "mst.h"
23 #include <errno.h>
24
25 /**
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
29  *
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.
33  *
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".
39  */
40 int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size)
41 {
42         u16 usa_ofs, usa_count, usn;
43         u16 *usa_pos, *data_pos;
44
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) {
53                 errno = EINVAL;
54                 return -1;
55         }
56         /* Position of usn in update sequence array. */
57         usa_pos = (u16*)b + usa_ofs/sizeof(u16);
58         /*
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.
64          */
65         usn = *usa_pos;
66         /*
67          * Position in protected data of first u16 that needs fixing up.
68          */
69         data_pos = (u16*)b + NTFS_SECTOR_SIZE/sizeof(u16) - 1;
70         /*
71          * Check for incomplete multi sector transfer(s).
72          */
73         while (usa_count--) {
74                 if (*data_pos != usn) {
75                         /*
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.
79                          */
80                         b->magic = magic_BAAD;
81                         errno = EIO;
82                         return -1;
83                 }
84                 data_pos += NTFS_SECTOR_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_SECTOR_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_SECTOR_SIZE/sizeof(u16);
98         }
99         return 0;
100 }
101
102 /**
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
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 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).
112  *
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).
121  */
122 int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size)
123 {
124         u16 usa_ofs, usa_count, usn;
125         u16 *usa_pos, *data_pos;
126
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)) {
130                 errno = EINVAL;
131                 return -1;
132         }
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) {
141                 errno = EINVAL;
142                 return -1;
143         }
144         /* Position of usn in update sequence array. */
145         usa_pos = (u16*)((u8*)b + usa_ofs);
146         /*
147          * Cyclically increment the update sequence number
148          * (skipping 0 and -1, i.e. 0xffff).
149          */
150         usn = le16_to_cpup(usa_pos) + 1;
151         if (usn == 0xffff || !usn)
152                 usn = 1;
153         usn = cpu_to_le16(usn);
154         *usa_pos = 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--) {
159                 /*
160                  * Increment the position in the usa and save the
161                  * original data from the data buffer into the usa.
162                  */
163                 *(++usa_pos) = *data_pos;
164                 /* Apply fixup to data. */
165                 *data_pos = usn;
166                 /* Increment position in data as well. */
167                 data_pos += NTFS_SECTOR_SIZE/sizeof(u16);
168         }
169         return 0;
170 }
171
172 /**
173  * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data
174  * @b:          pointer to the data to deprotect
175  *
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
179  * have gotten here.
180  */
181 void ntfs_mst_post_write_fixup(NTFS_RECORD *b)
182 {
183         u16 *usa_pos, *data_pos;
184
185         u16 usa_ofs = le16_to_cpu(b->usa_ofs);
186         u16 usa_count = le16_to_cpu(b->usa_count) - 1;
187
188         /* Position of usn in update sequence array. */
189         usa_pos = (u16*)b + usa_ofs/sizeof(u16);
190
191         /* Position in protected data of first u16 that needs fixing up. */
192         data_pos = (u16*)b + NTFS_SECTOR_SIZE/sizeof(u16) - 1;
193
194         /* Fixup all sectors. */
195         while (usa_count--) {
196                 /*
197                  * Increment position in usa and restore original data from
198                  * the usa into the data buffer.
199                  */
200                 *data_pos = *(++usa_pos);
201
202                 /* Increment position in data as well. */
203                 data_pos += NTFS_SECTOR_SIZE/sizeof(u16);
204         }
205 }
206