1 /** 2 * mst.c - Multi sector fixup handling code. Originated from the Linux-NTFS project. 3 * 4 * Copyright (c) 2000-2004 Anton Altaparmakov 5 * Copyright (c) 2006-2009 Szabolcs Szakacsits 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 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 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include "config.h" 25 #endif 26 27 #ifdef HAVE_ERRNO_H 28 #include <errno.h> 29 #endif 30 31 #include "mst.h" 32 #include "logging.h" 33 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 38 * 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. 43 * 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". 49 */ 50 int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size) 51 { 52 u16 usa_ofs, usa_count, usn; 53 u16 *usa_pos, *data_pos; 54 55 ntfs_log_trace("Entering\n"); 56 57 /* Setup the variables. */ 58 usa_ofs = le16_to_cpu(b->usa_ofs); 59 /* Decrement usa_count to get number of fixups. */ 60 usa_count = le16_to_cpu(b->usa_count) - 1; 61 /* Size and alignment checks. */ 62 if (size & (NTFS_BLOCK_SIZE - 1) || usa_ofs & 1 || 63 (u32)(usa_ofs + (usa_count * 2)) > size || 64 (size >> NTFS_BLOCK_SIZE_BITS) != usa_count) { 65 errno = EINVAL; 66 ntfs_log_perror("%s: magic: 0x%08x size: %d usa_ofs: %d " 67 "usa_count: %d", __FUNCTION__, *(le32 *)b, 68 size, usa_ofs, usa_count); 69 return -1; 70 } 71 /* Position of usn in update sequence array. */ 72 usa_pos = (u16*)b + usa_ofs/sizeof(u16); 73 /* 74 * The update sequence number which has to be equal to each of the 75 * u16 values before they are fixed up. Note no need to care for 76 * endianness since we are comparing and moving data for on disk 77 * structures which means the data is consistent. - If it is 78 * consistency the wrong endianness it doesn't make any difference. 79 */ 80 usn = *usa_pos; 81 /* 82 * Position in protected data of first u16 that needs fixing up. 83 */ 84 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; 85 /* 86 * Check for incomplete multi sector transfer(s). 87 */ 88 while (usa_count--) { 89 if (*data_pos != usn) { 90 /* 91 * Incomplete multi sector transfer detected! )-: 92 * Set the magic to "BAAD" and return failure. 93 * Note that magic_BAAD is already converted to le32. 94 */ 95 errno = EIO; 96 ntfs_log_perror("Incomplete multi-sector transfer: " 97 "magic: 0x%08x size: %d usa_ofs: %d usa_count:" 98 " %d data: %d usn: %d", *(le32 *)b, size, 99 usa_ofs, usa_count, *data_pos, usn); 100 b->magic = magic_BAAD; 101 return -1; 102 } 103 data_pos += NTFS_BLOCK_SIZE/sizeof(u16); 104 } 105 /* Re-setup the variables. */ 106 usa_count = le16_to_cpu(b->usa_count) - 1; 107 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; 108 /* Fixup all sectors. */ 109 while (usa_count--) { 110 /* 111 * Increment position in usa and restore original data from 112 * the usa into the data buffer. 113 */ 114 *data_pos = *(++usa_pos); 115 /* Increment position in data as well. */ 116 data_pos += NTFS_BLOCK_SIZE/sizeof(u16); 117 } 118 return 0; 119 } 120 121 /** 122 * ntfs_mst_pre_write_fixup - apply multi sector transfer protection 123 * @b: pointer to the data to protect 124 * @size: size in bytes of @b 125 * 126 * Perform the necessary pre write multi sector transfer fixup on the data 127 * pointer to by @b of @size. 128 * 129 * Return 0 if fixups applied successfully or -1 if no fixups were performed 130 * due to errors. In that case errno i set to the error code (EINVAL). 131 * 132 * NOTE: We consider the absence / invalidity of an update sequence array to 133 * mean error. This means that you have to create a valid update sequence 134 * array header in the ntfs record before calling this function, otherwise it 135 * will fail (the header needs to contain the position of the update sequence 136 * array together with the number of elements in the array). You also need to 137 * initialise the update sequence number before calling this function 138 * otherwise a random word will be used (whatever was in the record at that 139 * position at that time). 140 */ 141 int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size) 142 { 143 u16 usa_ofs, usa_count, usn; 144 u16 *usa_pos, *data_pos; 145 146 ntfs_log_trace("Entering\n"); 147 148 /* Sanity check + only fixup if it makes sense. */ 149 if (!b || ntfs_is_baad_record(b->magic) || 150 ntfs_is_hole_record(b->magic)) { 151 errno = EINVAL; 152 ntfs_log_perror("%s: bad argument", __FUNCTION__); 153 return -1; 154 } 155 /* Setup the variables. */ 156 usa_ofs = le16_to_cpu(b->usa_ofs); 157 /* Decrement usa_count to get number of fixups. */ 158 usa_count = le16_to_cpu(b->usa_count) - 1; 159 /* Size and alignment checks. */ 160 if (size & (NTFS_BLOCK_SIZE - 1) || usa_ofs & 1 || 161 (u32)(usa_ofs + (usa_count * 2)) > size || 162 (size >> NTFS_BLOCK_SIZE_BITS) != usa_count) { 163 errno = EINVAL; 164 ntfs_log_perror("%s", __FUNCTION__); 165 return -1; 166 } 167 /* Position of usn in update sequence array. */ 168 usa_pos = (u16*)((u8*)b + usa_ofs); 169 /* 170 * Cyclically increment the update sequence number 171 * (skipping 0 and -1, i.e. 0xffff). 172 */ 173 usn = le16_to_cpup(usa_pos) + 1; 174 if (usn == 0xffff || !usn) 175 usn = 1; 176 usn = cpu_to_le16(usn); 177 *usa_pos = usn; 178 /* Position in data of first u16 that needs fixing up. */ 179 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; 180 /* Fixup all sectors. */ 181 while (usa_count--) { 182 /* 183 * Increment the position in the usa and save the 184 * original data from the data buffer into the usa. 185 */ 186 *(++usa_pos) = *data_pos; 187 /* Apply fixup to data. */ 188 *data_pos = usn; 189 /* Increment position in data as well. */ 190 data_pos += NTFS_BLOCK_SIZE/sizeof(u16); 191 } 192 return 0; 193 } 194 195 /** 196 * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data 197 * @b: pointer to the data to deprotect 198 * 199 * Perform the necessary post write multi sector transfer fixup, not checking 200 * for any errors, because we assume we have just used 201 * ntfs_mst_pre_write_fixup(), thus the data will be fine or we would never 202 * have gotten here. 203 */ 204 void ntfs_mst_post_write_fixup(NTFS_RECORD *b) 205 { 206 u16 *usa_pos, *data_pos; 207 208 u16 usa_ofs = le16_to_cpu(b->usa_ofs); 209 u16 usa_count = le16_to_cpu(b->usa_count) - 1; 210 211 ntfs_log_trace("Entering\n"); 212 213 /* Position of usn in update sequence array. */ 214 usa_pos = (u16*)b + usa_ofs/sizeof(u16); 215 216 /* Position in protected data of first u16 that needs fixing up. */ 217 data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; 218 219 /* Fixup all sectors. */ 220 while (usa_count--) { 221 /* 222 * Increment position in usa and restore original data from 223 * the usa into the data buffer. 224 */ 225 *data_pos = *(++usa_pos); 226 227 /* Increment position in data as well. */ 228 data_pos += NTFS_BLOCK_SIZE/sizeof(u16); 229 } 230 } 231 232