Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / ntfs-3g / libntfs-3g / mst.c
blobb7937b7a411c04366906210b81e582f57f3056fd
1 /**
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
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #ifdef HAVE_ERRNO_H
28 #include <errno.h>
29 #endif
31 #include "mst.h"
32 #include "logging.h"
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
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.
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".
50 int ntfs_mst_post_read_fixup_warn(NTFS_RECORD *b, const u32 size,
51 BOOL warn)
53 u16 usa_ofs, usa_count, usn;
54 u16 *usa_pos, *data_pos;
56 ntfs_log_trace("Entering\n");
58 /* Setup the variables. */
59 usa_ofs = le16_to_cpu(b->usa_ofs);
60 /* Decrement usa_count to get number of fixups. */
61 usa_count = le16_to_cpu(b->usa_count) - 1;
62 /* Size and alignment checks. */
63 if (size & (NTFS_BLOCK_SIZE - 1) || usa_ofs & 1 ||
64 (u32)(usa_ofs + (usa_count * 2)) > size ||
65 (size >> NTFS_BLOCK_SIZE_BITS) != usa_count) {
66 errno = EINVAL;
67 if (warn) {
68 ntfs_log_perror("%s: magic: 0x%08lx size: %ld "
69 " usa_ofs: %d usa_count: %u",
70 __FUNCTION__,
71 (long)le32_to_cpu(*(le32 *)b),
72 (long)size, (int)usa_ofs,
73 (unsigned int)usa_count);
75 return -1;
77 /* Position of usn in update sequence array. */
78 usa_pos = (u16*)b + usa_ofs/sizeof(u16);
80 * The update sequence number which has to be equal to each of the
81 * u16 values before they are fixed up. Note no need to care for
82 * endianness since we are comparing and moving data for on disk
83 * structures which means the data is consistent. - If it is
84 * consistency the wrong endianness it doesn't make any difference.
86 usn = *usa_pos;
88 * Position in protected data of first u16 that needs fixing up.
90 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
92 * Check for incomplete multi sector transfer(s).
94 while (usa_count--) {
95 if (*data_pos != usn) {
97 * Incomplete multi sector transfer detected! )-:
98 * Set the magic to "BAAD" and return failure.
99 * Note that magic_BAAD is already converted to le32.
101 errno = EIO;
102 ntfs_log_perror("Incomplete multi-sector transfer: "
103 "magic: 0x%08x size: %d usa_ofs: %d usa_count:"
104 " %d data: %d usn: %d", *(le32 *)b, size,
105 usa_ofs, usa_count, *data_pos, usn);
106 b->magic = magic_BAAD;
107 return -1;
109 data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
111 /* Re-setup the variables. */
112 usa_count = le16_to_cpu(b->usa_count) - 1;
113 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
114 /* Fixup all sectors. */
115 while (usa_count--) {
117 * Increment position in usa and restore original data from
118 * the usa into the data buffer.
120 *data_pos = *(++usa_pos);
121 /* Increment position in data as well. */
122 data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
124 return 0;
128 * Deprotect multi sector transfer protected data
129 * with a warning if an error is found.
132 int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size)
134 return (ntfs_mst_post_read_fixup_warn(b,size,TRUE));
138 * ntfs_mst_pre_write_fixup - apply multi sector transfer protection
139 * @b: pointer to the data to protect
140 * @size: size in bytes of @b
142 * Perform the necessary pre write multi sector transfer fixup on the data
143 * pointer to by @b of @size.
145 * Return 0 if fixups applied successfully or -1 if no fixups were performed
146 * due to errors. In that case errno i set to the error code (EINVAL).
148 * NOTE: We consider the absence / invalidity of an update sequence array to
149 * mean error. This means that you have to create a valid update sequence
150 * array header in the ntfs record before calling this function, otherwise it
151 * will fail (the header needs to contain the position of the update sequence
152 * array together with the number of elements in the array). You also need to
153 * initialise the update sequence number before calling this function
154 * otherwise a random word will be used (whatever was in the record at that
155 * position at that time).
157 int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size)
159 u16 usa_ofs, usa_count, usn;
160 u16 *usa_pos, *data_pos;
162 ntfs_log_trace("Entering\n");
164 /* Sanity check + only fixup if it makes sense. */
165 if (!b || ntfs_is_baad_record(b->magic) ||
166 ntfs_is_hole_record(b->magic)) {
167 errno = EINVAL;
168 ntfs_log_perror("%s: bad argument", __FUNCTION__);
169 return -1;
171 /* Setup the variables. */
172 usa_ofs = le16_to_cpu(b->usa_ofs);
173 /* Decrement usa_count to get number of fixups. */
174 usa_count = le16_to_cpu(b->usa_count) - 1;
175 /* Size and alignment checks. */
176 if (size & (NTFS_BLOCK_SIZE - 1) || usa_ofs & 1 ||
177 (u32)(usa_ofs + (usa_count * 2)) > size ||
178 (size >> NTFS_BLOCK_SIZE_BITS) != usa_count) {
179 errno = EINVAL;
180 ntfs_log_perror("%s", __FUNCTION__);
181 return -1;
183 /* Position of usn in update sequence array. */
184 usa_pos = (u16*)((u8*)b + usa_ofs);
186 * Cyclically increment the update sequence number
187 * (skipping 0 and -1, i.e. 0xffff).
189 usn = le16_to_cpup(usa_pos) + 1;
190 if (usn == 0xffff || !usn)
191 usn = 1;
192 usn = cpu_to_le16(usn);
193 *usa_pos = usn;
194 /* Position in data of first u16 that needs fixing up. */
195 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
196 /* Fixup all sectors. */
197 while (usa_count--) {
199 * Increment the position in the usa and save the
200 * original data from the data buffer into the usa.
202 *(++usa_pos) = *data_pos;
203 /* Apply fixup to data. */
204 *data_pos = usn;
205 /* Increment position in data as well. */
206 data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
208 return 0;
212 * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data
213 * @b: pointer to the data to deprotect
215 * Perform the necessary post write multi sector transfer fixup, not checking
216 * for any errors, because we assume we have just used
217 * ntfs_mst_pre_write_fixup(), thus the data will be fine or we would never
218 * have gotten here.
220 void ntfs_mst_post_write_fixup(NTFS_RECORD *b)
222 u16 *usa_pos, *data_pos;
224 u16 usa_ofs = le16_to_cpu(b->usa_ofs);
225 u16 usa_count = le16_to_cpu(b->usa_count) - 1;
227 ntfs_log_trace("Entering\n");
229 /* Position of usn in update sequence array. */
230 usa_pos = (u16*)b + usa_ofs/sizeof(u16);
232 /* Position in protected data of first u16 that needs fixing up. */
233 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
235 /* Fixup all sectors. */
236 while (usa_count--) {
238 * Increment position in usa and restore original data from
239 * the usa into the data buffer.
241 *data_pos = *(++usa_pos);
243 /* Increment position in data as well. */
244 data_pos += NTFS_BLOCK_SIZE/sizeof(u16);