Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 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_warn(NTFS_RECORD *b, const u32 size, |
| 51 | BOOL warn) |
| 52 | { |
| 53 | u16 usa_ofs, usa_count, usn; |
| 54 | u16 *usa_pos, *data_pos; |
| 55 | |
| 56 | ntfs_log_trace("Entering\n"); |
| 57 | |
| 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); |
| 74 | } |
| 75 | return -1; |
| 76 | } |
| 77 | /* Position of usn in update sequence array. */ |
| 78 | usa_pos = (u16*)b + usa_ofs/sizeof(u16); |
| 79 | /* |
| 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. |
| 85 | */ |
| 86 | usn = *usa_pos; |
| 87 | /* |
| 88 | * Position in protected data of first u16 that needs fixing up. |
| 89 | */ |
| 90 | data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; |
| 91 | /* |
| 92 | * Check for incomplete multi sector transfer(s). |
| 93 | */ |
| 94 | while (usa_count--) { |
| 95 | if (*data_pos != usn) { |
| 96 | /* |
| 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. |
| 100 | */ |
| 101 | errno = EIO; |
| 102 | ntfs_log_perror("Incomplete multi-sector transfer: " |
| 103 | "magic: 0x%08x size: %d usa_ofs: %d usa_count:" |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 104 | " %d data: %d usn: %d", le32_to_cpu(*(le32 *)b), size, |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 105 | usa_ofs, usa_count, *data_pos, usn); |
| 106 | b->magic = magic_BAAD; |
| 107 | return -1; |
| 108 | } |
| 109 | data_pos += NTFS_BLOCK_SIZE/sizeof(u16); |
| 110 | } |
| 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--) { |
| 116 | /* |
| 117 | * Increment position in usa and restore original data from |
| 118 | * the usa into the data buffer. |
| 119 | */ |
| 120 | *data_pos = *(++usa_pos); |
| 121 | /* Increment position in data as well. */ |
| 122 | data_pos += NTFS_BLOCK_SIZE/sizeof(u16); |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Deprotect multi sector transfer protected data |
| 129 | * with a warning if an error is found. |
| 130 | */ |
| 131 | |
| 132 | int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size) |
| 133 | { |
| 134 | return (ntfs_mst_post_read_fixup_warn(b,size,TRUE)); |
| 135 | } |
| 136 | |
| 137 | /** |
| 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 |
| 141 | * |
| 142 | * Perform the necessary pre write multi sector transfer fixup on the data |
| 143 | * pointer to by @b of @size. |
| 144 | * |
| 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). |
| 147 | * |
| 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). |
| 156 | */ |
| 157 | int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size) |
| 158 | { |
| 159 | u16 usa_ofs, usa_count, usn; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 160 | le16 le_usn; |
| 161 | le16 *usa_pos, *data_pos; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 162 | |
| 163 | ntfs_log_trace("Entering\n"); |
| 164 | |
| 165 | /* Sanity check + only fixup if it makes sense. */ |
| 166 | if (!b || ntfs_is_baad_record(b->magic) || |
| 167 | ntfs_is_hole_record(b->magic)) { |
| 168 | errno = EINVAL; |
| 169 | ntfs_log_perror("%s: bad argument", __FUNCTION__); |
| 170 | return -1; |
| 171 | } |
| 172 | /* Setup the variables. */ |
| 173 | usa_ofs = le16_to_cpu(b->usa_ofs); |
| 174 | /* Decrement usa_count to get number of fixups. */ |
| 175 | usa_count = le16_to_cpu(b->usa_count) - 1; |
| 176 | /* Size and alignment checks. */ |
| 177 | if (size & (NTFS_BLOCK_SIZE - 1) || usa_ofs & 1 || |
| 178 | (u32)(usa_ofs + (usa_count * 2)) > size || |
| 179 | (size >> NTFS_BLOCK_SIZE_BITS) != usa_count) { |
| 180 | errno = EINVAL; |
| 181 | ntfs_log_perror("%s", __FUNCTION__); |
| 182 | return -1; |
| 183 | } |
| 184 | /* Position of usn in update sequence array. */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 185 | usa_pos = (le16*)((u8*)b + usa_ofs); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 186 | /* |
| 187 | * Cyclically increment the update sequence number |
| 188 | * (skipping 0 and -1, i.e. 0xffff). |
| 189 | */ |
| 190 | usn = le16_to_cpup(usa_pos) + 1; |
| 191 | if (usn == 0xffff || !usn) |
| 192 | usn = 1; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 193 | le_usn = cpu_to_le16(usn); |
| 194 | *usa_pos = le_usn; |
| 195 | /* Position in data of first le16 that needs fixing up. */ |
| 196 | data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 197 | /* Fixup all sectors. */ |
| 198 | while (usa_count--) { |
| 199 | /* |
| 200 | * Increment the position in the usa and save the |
| 201 | * original data from the data buffer into the usa. |
| 202 | */ |
| 203 | *(++usa_pos) = *data_pos; |
| 204 | /* Apply fixup to data. */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 205 | *data_pos = le_usn; |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 206 | /* Increment position in data as well. */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 207 | data_pos += NTFS_BLOCK_SIZE/sizeof(le16); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data |
| 214 | * @b: pointer to the data to deprotect |
| 215 | * |
| 216 | * Perform the necessary post write multi sector transfer fixup, not checking |
| 217 | * for any errors, because we assume we have just used |
| 218 | * ntfs_mst_pre_write_fixup(), thus the data will be fine or we would never |
| 219 | * have gotten here. |
| 220 | */ |
| 221 | void ntfs_mst_post_write_fixup(NTFS_RECORD *b) |
| 222 | { |
| 223 | u16 *usa_pos, *data_pos; |
| 224 | |
| 225 | u16 usa_ofs = le16_to_cpu(b->usa_ofs); |
| 226 | u16 usa_count = le16_to_cpu(b->usa_count) - 1; |
| 227 | |
| 228 | ntfs_log_trace("Entering\n"); |
| 229 | |
| 230 | /* Position of usn in update sequence array. */ |
| 231 | usa_pos = (u16*)b + usa_ofs/sizeof(u16); |
| 232 | |
| 233 | /* Position in protected data of first u16 that needs fixing up. */ |
| 234 | data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; |
| 235 | |
| 236 | /* Fixup all sectors. */ |
| 237 | while (usa_count--) { |
| 238 | /* |
| 239 | * Increment position in usa and restore original data from |
| 240 | * the usa into the data buffer. |
| 241 | */ |
| 242 | *data_pos = *(++usa_pos); |
| 243 | |
| 244 | /* Increment position in data as well. */ |
| 245 | data_pos += NTFS_BLOCK_SIZE/sizeof(u16); |
| 246 | } |
| 247 | } |
| 248 | |