Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mst.c
Go to the documentation of this file.
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 
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 
123 int pre_write_mst_fixup(NTFS_RECORD *b, const u32 size)
124 {
125  le16 *usa_pos, *data_pos;
126  u16 usa_ofs, usa_count, usn;
127  le16 le_usn;
128 
129  /* Sanity check + only fixup if it makes sense. */
130  if (!b || ntfs_is_baad_record(b->magic) ||
131  ntfs_is_hole_record(b->magic))
132  return -EINVAL;
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_BLOCK_SIZE - 1) ||
139  usa_ofs & 1 ||
140  usa_ofs + (usa_count * 2) > size ||
141  (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
142  return -EINVAL;
143  /* Position of usn in update sequence array. */
144  usa_pos = (le16*)((u8*)b + usa_ofs);
145  /*
146  * Cyclically increment the update sequence number
147  * (skipping 0 and -1, i.e. 0xffff).
148  */
149  usn = le16_to_cpup(usa_pos) + 1;
150  if (usn == 0xffff || !usn)
151  usn = 1;
152  le_usn = cpu_to_le16(usn);
153  *usa_pos = le_usn;
154  /* Position in data of first u16 that needs fixing up. */
155  data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
156  /* Fixup all sectors. */
157  while (usa_count--) {
158  /*
159  * Increment the position in the usa and save the
160  * original data from the data buffer into the usa.
161  */
162  *(++usa_pos) = *data_pos;
163  /* Apply fixup to data. */
164  *data_pos = le_usn;
165  /* Increment position in data as well. */
166  data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
167  }
168  return 0;
169 }
170 
179 void post_write_mst_fixup(NTFS_RECORD *b)
180 {
181  le16 *usa_pos, *data_pos;
182 
183  u16 usa_ofs = le16_to_cpu(b->usa_ofs);
184  u16 usa_count = le16_to_cpu(b->usa_count) - 1;
185 
186  /* Position of usn in update sequence array. */
187  usa_pos = (le16*)b + usa_ofs/sizeof(le16);
188 
189  /* Position in protected data of first u16 that needs fixing up. */
190  data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
191 
192  /* Fixup all sectors. */
193  while (usa_count--) {
194  /*
195  * Increment position in usa and restore original data from
196  * the usa into the data buffer.
197  */
198  *data_pos = *(++usa_pos);
199 
200  /* Increment position in data as well. */
201  data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
202  }
203 }