[PATCH] Fix bug in saa7146 analog tv i2c-handling
[linux-2.6/history.git] / fs / ntfs / mst.c
blob00f63c1f35247cd9b40d7b6ad826ca92e62ae0b4
1 /*
2 * mst.c - NTFS multi sector transfer protection handling code. Part of the
3 * Linux-NTFS project.
5 * Copyright (c) 2001 Anton Altaparmakov.
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 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
23 #include "ntfs.h"
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
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.
35 * Return 0 on success and -EINVAL on error ("BAAD" magic will be present).
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.
42 int post_read_mst_fixup(NTFS_RECORD *b, const u32 size)
44 u16 usa_ofs, usa_count, usn;
45 u16 *usa_pos, *data_pos;
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.
66 usn = *usa_pos;
68 * Position in protected data of first u16 that needs fixing up.
70 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
72 * Check for incomplete multi sector transfer(s).
74 while (usa_count--) {
75 if (*data_pos != usn) {
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.
81 b->magic = magic_BAAD;
82 return -EINVAL;
84 data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
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--) {
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_BLOCK_SIZE/sizeof(u16);
99 return 0;
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
107 * Perform the necessary pre write multi sector transfer fixup on the data
108 * pointer to by @b of @size.
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.
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).
123 int pre_write_mst_fixup(NTFS_RECORD *b, const u32 size)
125 u16 usa_ofs, usa_count, usn;
126 u16 *usa_pos, *data_pos;
128 /* Sanity check + only fixup if it makes sense. */
129 if (!b || is_baad_record(b->magic) || is_hole_record(b->magic))
130 return -EINVAL;
131 /* Setup the variables. */
132 usa_ofs = le16_to_cpu(b->usa_ofs);
133 /* Decrement usa_count to get number of fixups. */
134 usa_count = le16_to_cpu(b->usa_count) - 1;
135 /* Size and alignment checks. */
136 if ( size & (NTFS_BLOCK_SIZE - 1) ||
137 usa_ofs & 1 ||
138 usa_ofs + (usa_count * 2) > size ||
139 (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
140 return -EINVAL;
141 /* Position of usn in update sequence array. */
142 usa_pos = (u16*)((u8*)b + usa_ofs);
144 * Cyclically increment the update sequence number
145 * (skipping 0 and -1, i.e. 0xffff).
147 usn = le16_to_cpup(usa_pos) + 1;
148 if (usn == 0xffff || !usn)
149 usn = 1;
150 usn = cpu_to_le16(usn);
151 *usa_pos = usn;
152 /* Position in data of first u16 that needs fixing up. */
153 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
154 /* Fixup all sectors. */
155 while (usa_count--) {
157 * Increment the position in the usa and save the
158 * original data from the data buffer into the usa.
160 *(++usa_pos) = *data_pos;
161 /* Apply fixup to data. */
162 *data_pos = usn;
163 /* Increment position in data as well. */
164 data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
166 return 0;
170 * post_write_mst_fixup - fast deprotect multi sector transfer protected data
171 * @b: pointer to the data to deprotect
173 * Perform the necessary post write multi sector transfer fixup, not checking
174 * for any errors, because we assume we have just used pre_write_mst_fixup(),
175 * thus the data will be fine or we would never have gotten here.
177 void post_write_mst_fixup(NTFS_RECORD *b)
179 u16 *usa_pos, *data_pos;
181 u16 usa_ofs = le16_to_cpu(b->usa_ofs);
182 u16 usa_count = le16_to_cpu(b->usa_count) - 1;
184 /* Position of usn in update sequence array. */
185 usa_pos = (u16*)b + usa_ofs/sizeof(u16);
187 /* Position in protected data of first u16 that needs fixing up. */
188 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
190 /* Fixup all sectors. */
191 while (usa_count--) {
193 * Increment position in usa and restore original data from
194 * the usa into the data buffer.
196 *data_pos = *(++usa_pos);
198 /* Increment position in data as well. */
199 data_pos += NTFS_BLOCK_SIZE/sizeof(u16);