180bca3d3SAxel Dörfler /**
280bca3d3SAxel Dörfler * mst.c - Multi sector fixup handling code. Originated from the Linux-NTFS project.
380bca3d3SAxel Dörfler *
480bca3d3SAxel Dörfler * Copyright (c) 2000-2004 Anton Altaparmakov
530bc84e9SGerasim Troeglazov * Copyright (c) 2006-2009 Szabolcs Szakacsits
680bca3d3SAxel Dörfler *
780bca3d3SAxel Dörfler * This program/include file is free software; you can redistribute it and/or
880bca3d3SAxel Dörfler * modify it under the terms of the GNU General Public License as published
980bca3d3SAxel Dörfler * by the Free Software Foundation; either version 2 of the License, or
1080bca3d3SAxel Dörfler * (at your option) any later version.
1180bca3d3SAxel Dörfler *
1280bca3d3SAxel Dörfler * This program/include file is distributed in the hope that it will be
1380bca3d3SAxel Dörfler * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1480bca3d3SAxel Dörfler * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1580bca3d3SAxel Dörfler * GNU General Public License for more details.
1680bca3d3SAxel Dörfler *
1780bca3d3SAxel Dörfler * You should have received a copy of the GNU General Public License
1880bca3d3SAxel Dörfler * along with this program (in the main directory of the NTFS-3G
1980bca3d3SAxel Dörfler * distribution in the file COPYING); if not, write to the Free Software
2080bca3d3SAxel Dörfler * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2180bca3d3SAxel Dörfler */
2280bca3d3SAxel Dörfler
2380bca3d3SAxel Dörfler #ifdef HAVE_CONFIG_H
2480bca3d3SAxel Dörfler #include "config.h"
2580bca3d3SAxel Dörfler #endif
2680bca3d3SAxel Dörfler
2780bca3d3SAxel Dörfler #ifdef HAVE_ERRNO_H
2880bca3d3SAxel Dörfler #include <errno.h>
2980bca3d3SAxel Dörfler #endif
3080bca3d3SAxel Dörfler
3180bca3d3SAxel Dörfler #include "mst.h"
3280bca3d3SAxel Dörfler #include "logging.h"
3380bca3d3SAxel Dörfler
34*0490778eSAugustin Cavalier /*
35*0490778eSAugustin Cavalier * Basic validation of a NTFS multi-sector record. The record size must be a
36*0490778eSAugustin Cavalier * multiple of the logical sector size; and the update sequence array must be
37*0490778eSAugustin Cavalier * properly aligned, of the expected length, and must end before the last le16
38*0490778eSAugustin Cavalier * in the first logical sector.
39*0490778eSAugustin Cavalier */
40*0490778eSAugustin Cavalier static BOOL
is_valid_record(u32 size,u16 usa_ofs,u16 usa_count)41*0490778eSAugustin Cavalier is_valid_record(u32 size, u16 usa_ofs, u16 usa_count)
42*0490778eSAugustin Cavalier {
43*0490778eSAugustin Cavalier return size % NTFS_BLOCK_SIZE == 0 &&
44*0490778eSAugustin Cavalier usa_ofs % 2 == 0 &&
45*0490778eSAugustin Cavalier usa_count == 1 + (size / NTFS_BLOCK_SIZE) &&
46*0490778eSAugustin Cavalier usa_ofs + ((u32)usa_count * 2) <= NTFS_BLOCK_SIZE - 2;
47*0490778eSAugustin Cavalier }
48*0490778eSAugustin Cavalier
4980bca3d3SAxel Dörfler /**
5080bca3d3SAxel Dörfler * ntfs_mst_post_read_fixup - deprotect multi sector transfer protected data
5180bca3d3SAxel Dörfler * @b: pointer to the data to deprotect
5280bca3d3SAxel Dörfler * @size: size in bytes of @b
5380bca3d3SAxel Dörfler *
5480bca3d3SAxel Dörfler * Perform the necessary post read multi sector transfer fixups and detect the
5580bca3d3SAxel Dörfler * presence of incomplete multi sector transfers. - In that case, overwrite the
5680bca3d3SAxel Dörfler * magic of the ntfs record header being processed with "BAAD" (in memory only!)
5780bca3d3SAxel Dörfler * and abort processing.
5880bca3d3SAxel Dörfler *
5980bca3d3SAxel Dörfler * Return 0 on success and -1 on error, with errno set to the error code. The
6080bca3d3SAxel Dörfler * following error codes are defined:
6180bca3d3SAxel Dörfler * EINVAL Invalid arguments or invalid NTFS record in buffer @b.
6280bca3d3SAxel Dörfler * EIO Multi sector transfer error was detected. Magic of the NTFS
6380bca3d3SAxel Dörfler * record in @b will have been set to "BAAD".
6480bca3d3SAxel Dörfler */
ntfs_mst_post_read_fixup_warn(NTFS_RECORD * b,const u32 size,BOOL warn)65a814d850Sthreedeyes int ntfs_mst_post_read_fixup_warn(NTFS_RECORD *b, const u32 size,
66a814d850Sthreedeyes BOOL warn)
6780bca3d3SAxel Dörfler {
6880bca3d3SAxel Dörfler u16 usa_ofs, usa_count, usn;
6980bca3d3SAxel Dörfler u16 *usa_pos, *data_pos;
7080bca3d3SAxel Dörfler
7180bca3d3SAxel Dörfler ntfs_log_trace("Entering\n");
7280bca3d3SAxel Dörfler
7380bca3d3SAxel Dörfler /* Setup the variables. */
7480bca3d3SAxel Dörfler usa_ofs = le16_to_cpu(b->usa_ofs);
75*0490778eSAugustin Cavalier usa_count = le16_to_cpu(b->usa_count);
76*0490778eSAugustin Cavalier
77*0490778eSAugustin Cavalier if (!is_valid_record(size, usa_ofs, usa_count)) {
7880bca3d3SAxel Dörfler errno = EINVAL;
79a814d850Sthreedeyes if (warn) {
80a814d850Sthreedeyes ntfs_log_perror("%s: magic: 0x%08lx size: %ld "
81a814d850Sthreedeyes " usa_ofs: %d usa_count: %u",
82a814d850Sthreedeyes __FUNCTION__,
83a814d850Sthreedeyes (long)le32_to_cpu(*(le32 *)b),
84a814d850Sthreedeyes (long)size, (int)usa_ofs,
85a814d850Sthreedeyes (unsigned int)usa_count);
86a814d850Sthreedeyes }
8780bca3d3SAxel Dörfler return -1;
8880bca3d3SAxel Dörfler }
8980bca3d3SAxel Dörfler /* Position of usn in update sequence array. */
9080bca3d3SAxel Dörfler usa_pos = (u16*)b + usa_ofs/sizeof(u16);
9180bca3d3SAxel Dörfler /*
9280bca3d3SAxel Dörfler * The update sequence number which has to be equal to each of the
9380bca3d3SAxel Dörfler * u16 values before they are fixed up. Note no need to care for
9480bca3d3SAxel Dörfler * endianness since we are comparing and moving data for on disk
9580bca3d3SAxel Dörfler * structures which means the data is consistent. - If it is
9680bca3d3SAxel Dörfler * consistency the wrong endianness it doesn't make any difference.
9780bca3d3SAxel Dörfler */
9880bca3d3SAxel Dörfler usn = *usa_pos;
9980bca3d3SAxel Dörfler /*
10080bca3d3SAxel Dörfler * Position in protected data of first u16 that needs fixing up.
10180bca3d3SAxel Dörfler */
10280bca3d3SAxel Dörfler data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
10380bca3d3SAxel Dörfler /*
10480bca3d3SAxel Dörfler * Check for incomplete multi sector transfer(s).
10580bca3d3SAxel Dörfler */
106*0490778eSAugustin Cavalier while (--usa_count) {
10780bca3d3SAxel Dörfler if (*data_pos != usn) {
10880bca3d3SAxel Dörfler /*
10980bca3d3SAxel Dörfler * Incomplete multi sector transfer detected! )-:
11080bca3d3SAxel Dörfler * Set the magic to "BAAD" and return failure.
11180bca3d3SAxel Dörfler * Note that magic_BAAD is already converted to le32.
11280bca3d3SAxel Dörfler */
11380bca3d3SAxel Dörfler errno = EIO;
11430bc84e9SGerasim Troeglazov ntfs_log_perror("Incomplete multi-sector transfer: "
11530bc84e9SGerasim Troeglazov "magic: 0x%08x size: %d usa_ofs: %d usa_count:"
116*0490778eSAugustin Cavalier " %d data: %d usn: %d", le32_to_cpu(*(le32 *)b), size,
11730bc84e9SGerasim Troeglazov usa_ofs, usa_count, *data_pos, usn);
11830bc84e9SGerasim Troeglazov b->magic = magic_BAAD;
11980bca3d3SAxel Dörfler return -1;
12080bca3d3SAxel Dörfler }
12180bca3d3SAxel Dörfler data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
12280bca3d3SAxel Dörfler }
12380bca3d3SAxel Dörfler /* Re-setup the variables. */
124*0490778eSAugustin Cavalier usa_count = le16_to_cpu(b->usa_count);
12580bca3d3SAxel Dörfler data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
12680bca3d3SAxel Dörfler /* Fixup all sectors. */
127*0490778eSAugustin Cavalier while (--usa_count) {
12880bca3d3SAxel Dörfler /*
12980bca3d3SAxel Dörfler * Increment position in usa and restore original data from
13080bca3d3SAxel Dörfler * the usa into the data buffer.
13180bca3d3SAxel Dörfler */
13280bca3d3SAxel Dörfler *data_pos = *(++usa_pos);
13380bca3d3SAxel Dörfler /* Increment position in data as well. */
13480bca3d3SAxel Dörfler data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
13580bca3d3SAxel Dörfler }
13680bca3d3SAxel Dörfler return 0;
13780bca3d3SAxel Dörfler }
13880bca3d3SAxel Dörfler
139a814d850Sthreedeyes /*
140a814d850Sthreedeyes * Deprotect multi sector transfer protected data
141a814d850Sthreedeyes * with a warning if an error is found.
142a814d850Sthreedeyes */
143a814d850Sthreedeyes
ntfs_mst_post_read_fixup(NTFS_RECORD * b,const u32 size)144a814d850Sthreedeyes int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size)
145a814d850Sthreedeyes {
146a814d850Sthreedeyes return (ntfs_mst_post_read_fixup_warn(b,size,TRUE));
147a814d850Sthreedeyes }
148a814d850Sthreedeyes
14980bca3d3SAxel Dörfler /**
15080bca3d3SAxel Dörfler * ntfs_mst_pre_write_fixup - apply multi sector transfer protection
15180bca3d3SAxel Dörfler * @b: pointer to the data to protect
15280bca3d3SAxel Dörfler * @size: size in bytes of @b
15380bca3d3SAxel Dörfler *
15480bca3d3SAxel Dörfler * Perform the necessary pre write multi sector transfer fixup on the data
15580bca3d3SAxel Dörfler * pointer to by @b of @size.
15680bca3d3SAxel Dörfler *
15780bca3d3SAxel Dörfler * Return 0 if fixups applied successfully or -1 if no fixups were performed
15880bca3d3SAxel Dörfler * due to errors. In that case errno i set to the error code (EINVAL).
15980bca3d3SAxel Dörfler *
16080bca3d3SAxel Dörfler * NOTE: We consider the absence / invalidity of an update sequence array to
16180bca3d3SAxel Dörfler * mean error. This means that you have to create a valid update sequence
16280bca3d3SAxel Dörfler * array header in the ntfs record before calling this function, otherwise it
16380bca3d3SAxel Dörfler * will fail (the header needs to contain the position of the update sequence
16480bca3d3SAxel Dörfler * array together with the number of elements in the array). You also need to
16580bca3d3SAxel Dörfler * initialise the update sequence number before calling this function
16680bca3d3SAxel Dörfler * otherwise a random word will be used (whatever was in the record at that
16780bca3d3SAxel Dörfler * position at that time).
16880bca3d3SAxel Dörfler */
ntfs_mst_pre_write_fixup(NTFS_RECORD * b,const u32 size)16980bca3d3SAxel Dörfler int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size)
17080bca3d3SAxel Dörfler {
17180bca3d3SAxel Dörfler u16 usa_ofs, usa_count, usn;
172*0490778eSAugustin Cavalier le16 le_usn;
173*0490778eSAugustin Cavalier le16 *usa_pos, *data_pos;
17480bca3d3SAxel Dörfler
17580bca3d3SAxel Dörfler ntfs_log_trace("Entering\n");
17680bca3d3SAxel Dörfler
17780bca3d3SAxel Dörfler /* Sanity check + only fixup if it makes sense. */
17880bca3d3SAxel Dörfler if (!b || ntfs_is_baad_record(b->magic) ||
17980bca3d3SAxel Dörfler ntfs_is_hole_record(b->magic)) {
18080bca3d3SAxel Dörfler errno = EINVAL;
18130bc84e9SGerasim Troeglazov ntfs_log_perror("%s: bad argument", __FUNCTION__);
18280bca3d3SAxel Dörfler return -1;
18380bca3d3SAxel Dörfler }
18480bca3d3SAxel Dörfler /* Setup the variables. */
18580bca3d3SAxel Dörfler usa_ofs = le16_to_cpu(b->usa_ofs);
186*0490778eSAugustin Cavalier usa_count = le16_to_cpu(b->usa_count);
187*0490778eSAugustin Cavalier
188*0490778eSAugustin Cavalier if (!is_valid_record(size, usa_ofs, usa_count)) {
18980bca3d3SAxel Dörfler errno = EINVAL;
19030bc84e9SGerasim Troeglazov ntfs_log_perror("%s", __FUNCTION__);
19180bca3d3SAxel Dörfler return -1;
19280bca3d3SAxel Dörfler }
19380bca3d3SAxel Dörfler /* Position of usn in update sequence array. */
194*0490778eSAugustin Cavalier usa_pos = (le16*)((u8*)b + usa_ofs);
19580bca3d3SAxel Dörfler /*
19680bca3d3SAxel Dörfler * Cyclically increment the update sequence number
19780bca3d3SAxel Dörfler * (skipping 0 and -1, i.e. 0xffff).
19880bca3d3SAxel Dörfler */
19980bca3d3SAxel Dörfler usn = le16_to_cpup(usa_pos) + 1;
20080bca3d3SAxel Dörfler if (usn == 0xffff || !usn)
20180bca3d3SAxel Dörfler usn = 1;
202*0490778eSAugustin Cavalier le_usn = cpu_to_le16(usn);
203*0490778eSAugustin Cavalier *usa_pos = le_usn;
204*0490778eSAugustin Cavalier /* Position in data of first le16 that needs fixing up. */
205*0490778eSAugustin Cavalier data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
20680bca3d3SAxel Dörfler /* Fixup all sectors. */
207*0490778eSAugustin Cavalier while (--usa_count) {
20880bca3d3SAxel Dörfler /*
20980bca3d3SAxel Dörfler * Increment the position in the usa and save the
21080bca3d3SAxel Dörfler * original data from the data buffer into the usa.
21180bca3d3SAxel Dörfler */
21280bca3d3SAxel Dörfler *(++usa_pos) = *data_pos;
21380bca3d3SAxel Dörfler /* Apply fixup to data. */
214*0490778eSAugustin Cavalier *data_pos = le_usn;
21580bca3d3SAxel Dörfler /* Increment position in data as well. */
216*0490778eSAugustin Cavalier data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
21780bca3d3SAxel Dörfler }
21880bca3d3SAxel Dörfler return 0;
21980bca3d3SAxel Dörfler }
22080bca3d3SAxel Dörfler
22180bca3d3SAxel Dörfler /**
22280bca3d3SAxel Dörfler * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data
22380bca3d3SAxel Dörfler * @b: pointer to the data to deprotect
22480bca3d3SAxel Dörfler *
22580bca3d3SAxel Dörfler * Perform the necessary post write multi sector transfer fixup, not checking
22680bca3d3SAxel Dörfler * for any errors, because we assume we have just used
22780bca3d3SAxel Dörfler * ntfs_mst_pre_write_fixup(), thus the data will be fine or we would never
22880bca3d3SAxel Dörfler * have gotten here.
22980bca3d3SAxel Dörfler */
ntfs_mst_post_write_fixup(NTFS_RECORD * b)23080bca3d3SAxel Dörfler void ntfs_mst_post_write_fixup(NTFS_RECORD *b)
23180bca3d3SAxel Dörfler {
23280bca3d3SAxel Dörfler u16 *usa_pos, *data_pos;
23380bca3d3SAxel Dörfler
23480bca3d3SAxel Dörfler u16 usa_ofs = le16_to_cpu(b->usa_ofs);
235*0490778eSAugustin Cavalier u16 usa_count = le16_to_cpu(b->usa_count);
23680bca3d3SAxel Dörfler
23780bca3d3SAxel Dörfler ntfs_log_trace("Entering\n");
23880bca3d3SAxel Dörfler
23980bca3d3SAxel Dörfler /* Position of usn in update sequence array. */
24080bca3d3SAxel Dörfler usa_pos = (u16*)b + usa_ofs/sizeof(u16);
24180bca3d3SAxel Dörfler
24280bca3d3SAxel Dörfler /* Position in protected data of first u16 that needs fixing up. */
24380bca3d3SAxel Dörfler data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
24480bca3d3SAxel Dörfler
24580bca3d3SAxel Dörfler /* Fixup all sectors. */
246*0490778eSAugustin Cavalier while (--usa_count) {
24780bca3d3SAxel Dörfler /*
24880bca3d3SAxel Dörfler * Increment position in usa and restore original data from
24980bca3d3SAxel Dörfler * the usa into the data buffer.
25080bca3d3SAxel Dörfler */
25180bca3d3SAxel Dörfler *data_pos = *(++usa_pos);
25280bca3d3SAxel Dörfler
25380bca3d3SAxel Dörfler /* Increment position in data as well. */
25480bca3d3SAxel Dörfler data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
25580bca3d3SAxel Dörfler }
25680bca3d3SAxel Dörfler }
25780bca3d3SAxel Dörfler
258