Thomas Gleixner | a1d312d | 2019-05-22 09:51:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project. |
| 4 | * |
Anton Altaparmakov | bfab36e | 2007-10-12 09:37:15 +0100 | [diff] [blame] | 5 | * Copyright (c) 2002-2007 Anton Altaparmakov |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifdef NTFS_RW |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/highmem.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/bitops.h> |
Robert P. J. Day | 02d5341 | 2009-06-16 15:33:35 -0700 | [diff] [blame] | 15 | #include <linux/log2.h> |
Christoph Hellwig | 2f8b544 | 2016-11-01 07:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include "attrib.h" |
| 19 | #include "aops.h" |
| 20 | #include "debug.h" |
| 21 | #include "logfile.h" |
| 22 | #include "malloc.h" |
| 23 | #include "volume.h" |
| 24 | #include "ntfs.h" |
| 25 | |
| 26 | /** |
| 27 | * ntfs_check_restart_page_header - check the page header for consistency |
| 28 | * @vi: $LogFile inode to which the restart page header belongs |
| 29 | * @rp: restart page header to check |
| 30 | * @pos: position in @vi at which the restart page header resides |
| 31 | * |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 32 | * Check the restart page header @rp for consistency and return 'true' if it is |
| 33 | * consistent and 'false' otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * |
| 35 | * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not |
| 36 | * require the full restart page. |
| 37 | */ |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 38 | static bool ntfs_check_restart_page_header(struct inode *vi, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | RESTART_PAGE_HEADER *rp, s64 pos) |
| 40 | { |
| 41 | u32 logfile_system_page_size, logfile_log_page_size; |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 42 | u16 ra_ofs, usa_count, usa_ofs, usa_end = 0; |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 43 | bool have_usa = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | ntfs_debug("Entering."); |
| 46 | /* |
| 47 | * If the system or log page sizes are smaller than the ntfs block size |
| 48 | * or either is not a power of 2 we cannot handle this log file. |
| 49 | */ |
| 50 | logfile_system_page_size = le32_to_cpu(rp->system_page_size); |
| 51 | logfile_log_page_size = le32_to_cpu(rp->log_page_size); |
| 52 | if (logfile_system_page_size < NTFS_BLOCK_SIZE || |
| 53 | logfile_log_page_size < NTFS_BLOCK_SIZE || |
| 54 | logfile_system_page_size & |
| 55 | (logfile_system_page_size - 1) || |
Robert P. J. Day | 02d5341 | 2009-06-16 15:33:35 -0700 | [diff] [blame] | 56 | !is_power_of_2(logfile_log_page_size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | ntfs_error(vi->i_sb, "$LogFile uses unsupported page size."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 58 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | /* |
| 61 | * We must be either at !pos (1st restart page) or at pos = system page |
| 62 | * size (2nd restart page). |
| 63 | */ |
| 64 | if (pos && pos != logfile_system_page_size) { |
| 65 | ntfs_error(vi->i_sb, "Found restart area in incorrect " |
| 66 | "position in $LogFile."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 67 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | /* We only know how to handle version 1.1. */ |
| 70 | if (sle16_to_cpu(rp->major_ver) != 1 || |
| 71 | sle16_to_cpu(rp->minor_ver) != 1) { |
| 72 | ntfs_error(vi->i_sb, "$LogFile version %i.%i is not " |
| 73 | "supported. (This driver supports version " |
| 74 | "1.1 only.)", (int)sle16_to_cpu(rp->major_ver), |
| 75 | (int)sle16_to_cpu(rp->minor_ver)); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 76 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 78 | /* |
| 79 | * If chkdsk has been run the restart page may not be protected by an |
| 80 | * update sequence array. |
| 81 | */ |
| 82 | if (ntfs_is_chkd_record(rp->magic) && !le16_to_cpu(rp->usa_count)) { |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 83 | have_usa = false; |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 84 | goto skip_usa_checks; |
| 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* Verify the size of the update sequence array. */ |
| 87 | usa_count = 1 + (logfile_system_page_size >> NTFS_BLOCK_SIZE_BITS); |
| 88 | if (usa_count != le16_to_cpu(rp->usa_count)) { |
| 89 | ntfs_error(vi->i_sb, "$LogFile restart page specifies " |
| 90 | "inconsistent update sequence array count."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 91 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | /* Verify the position of the update sequence array. */ |
| 94 | usa_ofs = le16_to_cpu(rp->usa_ofs); |
| 95 | usa_end = usa_ofs + usa_count * sizeof(u16); |
| 96 | if (usa_ofs < sizeof(RESTART_PAGE_HEADER) || |
| 97 | usa_end > NTFS_BLOCK_SIZE - sizeof(u16)) { |
| 98 | ntfs_error(vi->i_sb, "$LogFile restart page specifies " |
| 99 | "inconsistent update sequence array offset."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 100 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 102 | skip_usa_checks: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* |
| 104 | * Verify the position of the restart area. It must be: |
| 105 | * - aligned to 8-byte boundary, |
| 106 | * - after the update sequence array, and |
| 107 | * - within the system page size. |
| 108 | */ |
| 109 | ra_ofs = le16_to_cpu(rp->restart_area_offset); |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 110 | if (ra_ofs & 7 || (have_usa ? ra_ofs < usa_end : |
| 111 | ra_ofs < sizeof(RESTART_PAGE_HEADER)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | ra_ofs > logfile_system_page_size) { |
| 113 | ntfs_error(vi->i_sb, "$LogFile restart page specifies " |
| 114 | "inconsistent restart area offset."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 115 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | /* |
| 118 | * Only restart pages modified by chkdsk are allowed to have chkdsk_lsn |
| 119 | * set. |
| 120 | */ |
| 121 | if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) { |
| 122 | ntfs_error(vi->i_sb, "$LogFile restart page is not modified " |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 123 | "by chkdsk but a chkdsk LSN is specified."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 124 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | ntfs_debug("Done."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 127 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * ntfs_check_restart_area - check the restart area for consistency |
| 132 | * @vi: $LogFile inode to which the restart page belongs |
| 133 | * @rp: restart page whose restart area to check |
| 134 | * |
| 135 | * Check the restart area of the restart page @rp for consistency and return |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 136 | * 'true' if it is consistent and 'false' otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * |
| 138 | * This function assumes that the restart page header has already been |
| 139 | * consistency checked. |
| 140 | * |
| 141 | * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not |
| 142 | * require the full restart page. |
| 143 | */ |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 144 | static bool ntfs_check_restart_area(struct inode *vi, RESTART_PAGE_HEADER *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | u64 file_size; |
| 147 | RESTART_AREA *ra; |
| 148 | u16 ra_ofs, ra_len, ca_ofs; |
| 149 | u8 fs_bits; |
| 150 | |
| 151 | ntfs_debug("Entering."); |
| 152 | ra_ofs = le16_to_cpu(rp->restart_area_offset); |
| 153 | ra = (RESTART_AREA*)((u8*)rp + ra_ofs); |
| 154 | /* |
| 155 | * Everything before ra->file_size must be before the first word |
| 156 | * protected by an update sequence number. This ensures that it is |
| 157 | * safe to access ra->client_array_offset. |
| 158 | */ |
| 159 | if (ra_ofs + offsetof(RESTART_AREA, file_size) > |
| 160 | NTFS_BLOCK_SIZE - sizeof(u16)) { |
| 161 | ntfs_error(vi->i_sb, "$LogFile restart area specifies " |
| 162 | "inconsistent file offset."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 163 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | /* |
| 166 | * Now that we can access ra->client_array_offset, make sure everything |
| 167 | * up to the log client array is before the first word protected by an |
| 168 | * update sequence number. This ensures we can access all of the |
| 169 | * restart area elements safely. Also, the client array offset must be |
| 170 | * aligned to an 8-byte boundary. |
| 171 | */ |
| 172 | ca_ofs = le16_to_cpu(ra->client_array_offset); |
| 173 | if (((ca_ofs + 7) & ~7) != ca_ofs || |
| 174 | ra_ofs + ca_ofs > NTFS_BLOCK_SIZE - sizeof(u16)) { |
| 175 | ntfs_error(vi->i_sb, "$LogFile restart area specifies " |
| 176 | "inconsistent client array offset."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 177 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | /* |
| 180 | * The restart area must end within the system page size both when |
| 181 | * calculated manually and as specified by ra->restart_area_length. |
| 182 | * Also, the calculated length must not exceed the specified length. |
| 183 | */ |
| 184 | ra_len = ca_ofs + le16_to_cpu(ra->log_clients) * |
| 185 | sizeof(LOG_CLIENT_RECORD); |
| 186 | if (ra_ofs + ra_len > le32_to_cpu(rp->system_page_size) || |
| 187 | ra_ofs + le16_to_cpu(ra->restart_area_length) > |
| 188 | le32_to_cpu(rp->system_page_size) || |
| 189 | ra_len > le16_to_cpu(ra->restart_area_length)) { |
| 190 | ntfs_error(vi->i_sb, "$LogFile restart area is out of bounds " |
| 191 | "of the system page size specified by the " |
| 192 | "restart page header and/or the specified " |
| 193 | "restart area length is inconsistent."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 194 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | /* |
| 197 | * The ra->client_free_list and ra->client_in_use_list must be either |
| 198 | * LOGFILE_NO_CLIENT or less than ra->log_clients or they are |
| 199 | * overflowing the client array. |
| 200 | */ |
| 201 | if ((ra->client_free_list != LOGFILE_NO_CLIENT && |
| 202 | le16_to_cpu(ra->client_free_list) >= |
| 203 | le16_to_cpu(ra->log_clients)) || |
| 204 | (ra->client_in_use_list != LOGFILE_NO_CLIENT && |
| 205 | le16_to_cpu(ra->client_in_use_list) >= |
| 206 | le16_to_cpu(ra->log_clients))) { |
| 207 | ntfs_error(vi->i_sb, "$LogFile restart area specifies " |
| 208 | "overflowing client free and/or in use lists."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 209 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | /* |
| 212 | * Check ra->seq_number_bits against ra->file_size for consistency. |
| 213 | * We cannot just use ffs() because the file size is not a power of 2. |
| 214 | */ |
| 215 | file_size = (u64)sle64_to_cpu(ra->file_size); |
| 216 | fs_bits = 0; |
| 217 | while (file_size) { |
| 218 | file_size >>= 1; |
| 219 | fs_bits++; |
| 220 | } |
| 221 | if (le32_to_cpu(ra->seq_number_bits) != 67 - fs_bits) { |
| 222 | ntfs_error(vi->i_sb, "$LogFile restart area specifies " |
| 223 | "inconsistent sequence number bits."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 224 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | /* The log record header length must be a multiple of 8. */ |
| 227 | if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) != |
| 228 | le16_to_cpu(ra->log_record_header_length)) { |
| 229 | ntfs_error(vi->i_sb, "$LogFile restart area specifies " |
| 230 | "inconsistent log record header length."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 231 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | /* Dito for the log page data offset. */ |
| 234 | if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) != |
| 235 | le16_to_cpu(ra->log_page_data_offset)) { |
| 236 | ntfs_error(vi->i_sb, "$LogFile restart area specifies " |
| 237 | "inconsistent log page data offset."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 238 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | ntfs_debug("Done."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 241 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * ntfs_check_log_client_array - check the log client array for consistency |
| 246 | * @vi: $LogFile inode to which the restart page belongs |
| 247 | * @rp: restart page whose log client array to check |
| 248 | * |
| 249 | * Check the log client array of the restart page @rp for consistency and |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 250 | * return 'true' if it is consistent and 'false' otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | * |
| 252 | * This function assumes that the restart page header and the restart area have |
| 253 | * already been consistency checked. |
| 254 | * |
| 255 | * Unlike ntfs_check_restart_page_header() and ntfs_check_restart_area(), this |
| 256 | * function needs @rp->system_page_size bytes in @rp, i.e. it requires the full |
| 257 | * restart page and the page must be multi sector transfer deprotected. |
| 258 | */ |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 259 | static bool ntfs_check_log_client_array(struct inode *vi, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | RESTART_PAGE_HEADER *rp) |
| 261 | { |
| 262 | RESTART_AREA *ra; |
| 263 | LOG_CLIENT_RECORD *ca, *cr; |
| 264 | u16 nr_clients, idx; |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 265 | bool in_free_list, idx_is_first; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | ntfs_debug("Entering."); |
| 268 | ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); |
| 269 | ca = (LOG_CLIENT_RECORD*)((u8*)ra + |
| 270 | le16_to_cpu(ra->client_array_offset)); |
| 271 | /* |
| 272 | * Check the ra->client_free_list first and then check the |
| 273 | * ra->client_in_use_list. Check each of the log client records in |
| 274 | * each of the lists and check that the array does not overflow the |
| 275 | * ra->log_clients value. Also keep track of the number of records |
| 276 | * visited as there cannot be more than ra->log_clients records and |
| 277 | * that way we detect eventual loops in within a list. |
| 278 | */ |
| 279 | nr_clients = le16_to_cpu(ra->log_clients); |
| 280 | idx = le16_to_cpu(ra->client_free_list); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 281 | in_free_list = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | check_list: |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 283 | for (idx_is_first = true; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | idx = le16_to_cpu(cr->next_client)) { |
| 285 | if (!nr_clients || idx >= le16_to_cpu(ra->log_clients)) |
| 286 | goto err_out; |
| 287 | /* Set @cr to the current log client record. */ |
| 288 | cr = ca + idx; |
| 289 | /* The first log client record must not have a prev_client. */ |
| 290 | if (idx_is_first) { |
| 291 | if (cr->prev_client != LOGFILE_NO_CLIENT) |
| 292 | goto err_out; |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 293 | idx_is_first = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | /* Switch to and check the in use list if we just did the free list. */ |
| 297 | if (in_free_list) { |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 298 | in_free_list = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | idx = le16_to_cpu(ra->client_in_use_list); |
| 300 | goto check_list; |
| 301 | } |
| 302 | ntfs_debug("Done."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 303 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | err_out: |
| 305 | ntfs_error(vi->i_sb, "$LogFile log client array is corrupt."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 306 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * ntfs_check_and_load_restart_page - check the restart page for consistency |
| 311 | * @vi: $LogFile inode to which the restart page belongs |
| 312 | * @rp: restart page to check |
| 313 | * @pos: position in @vi at which the restart page resides |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 314 | * @wrp: [OUT] copy of the multi sector transfer deprotected restart page |
| 315 | * @lsn: [OUT] set to the current logfile lsn on success |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | * |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 317 | * Check the restart page @rp for consistency and return 0 if it is consistent |
| 318 | * and -errno otherwise. The restart page may have been modified by chkdsk in |
| 319 | * which case its magic is CHKD instead of RSTR. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | * |
| 321 | * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not |
| 322 | * require the full restart page. |
| 323 | * |
| 324 | * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a |
| 325 | * copy of the complete multi sector transfer deprotected page. On failure, |
| 326 | * *@wrp is undefined. |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 327 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 328 | * Simillarly, if @lsn is not NULL, on success *@lsn will be set to the current |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 329 | * logfile lsn according to this restart page. On failure, *@lsn is undefined. |
| 330 | * |
| 331 | * The following error codes are defined: |
| 332 | * -EINVAL - The restart page is inconsistent. |
| 333 | * -ENOMEM - Not enough memory to load the restart page. |
| 334 | * -EIO - Failed to reading from $LogFile. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | */ |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 336 | static int ntfs_check_and_load_restart_page(struct inode *vi, |
| 337 | RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp, |
| 338 | LSN *lsn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
| 340 | RESTART_AREA *ra; |
| 341 | RESTART_PAGE_HEADER *trp; |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 342 | int size, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | ntfs_debug("Entering."); |
| 345 | /* Check the restart page header for consistency. */ |
| 346 | if (!ntfs_check_restart_page_header(vi, rp, pos)) { |
| 347 | /* Error output already done inside the function. */ |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 348 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | /* Check the restart area for consistency. */ |
| 351 | if (!ntfs_check_restart_area(vi, rp)) { |
| 352 | /* Error output already done inside the function. */ |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 353 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); |
| 356 | /* |
| 357 | * Allocate a buffer to store the whole restart page so we can multi |
| 358 | * sector transfer deprotect it. |
| 359 | */ |
| 360 | trp = ntfs_malloc_nofs(le32_to_cpu(rp->system_page_size)); |
| 361 | if (!trp) { |
| 362 | ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile " |
| 363 | "restart page buffer."); |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 364 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | /* |
| 367 | * Read the whole of the restart page into the buffer. If it fits |
| 368 | * completely inside @rp, just copy it from there. Otherwise map all |
| 369 | * the required pages and copy the data from them. |
| 370 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 371 | size = PAGE_SIZE - (pos & ~PAGE_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (size >= le32_to_cpu(rp->system_page_size)) { |
| 373 | memcpy(trp, rp, le32_to_cpu(rp->system_page_size)); |
| 374 | } else { |
| 375 | pgoff_t idx; |
| 376 | struct page *page; |
| 377 | int have_read, to_read; |
| 378 | |
| 379 | /* First copy what we already have in @rp. */ |
| 380 | memcpy(trp, rp, size); |
| 381 | /* Copy the remaining data one page at a time. */ |
| 382 | have_read = size; |
| 383 | to_read = le32_to_cpu(rp->system_page_size) - size; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 384 | idx = (pos + size) >> PAGE_SHIFT; |
| 385 | BUG_ON((pos + size) & ~PAGE_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | do { |
| 387 | page = ntfs_map_page(vi->i_mapping, idx); |
| 388 | if (IS_ERR(page)) { |
| 389 | ntfs_error(vi->i_sb, "Error mapping $LogFile " |
| 390 | "page (index %lu).", idx); |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 391 | err = PTR_ERR(page); |
| 392 | if (err != -EIO && err != -ENOMEM) |
| 393 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | goto err_out; |
| 395 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 396 | size = min_t(int, to_read, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | memcpy((u8*)trp + have_read, page_address(page), size); |
| 398 | ntfs_unmap_page(page); |
| 399 | have_read += size; |
| 400 | to_read -= size; |
| 401 | idx++; |
| 402 | } while (to_read > 0); |
| 403 | } |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 404 | /* |
| 405 | * Perform the multi sector transfer deprotection on the buffer if the |
| 406 | * restart page is protected. |
| 407 | */ |
| 408 | if ((!ntfs_is_chkd_record(trp->magic) || le16_to_cpu(trp->usa_count)) |
| 409 | && post_read_mst_fixup((NTFS_RECORD*)trp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | le32_to_cpu(rp->system_page_size))) { |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 411 | /* |
| 412 | * A multi sector tranfer error was detected. We only need to |
| 413 | * abort if the restart page contents exceed the multi sector |
| 414 | * transfer fixup of the first sector. |
| 415 | */ |
| 416 | if (le16_to_cpu(rp->restart_area_offset) + |
| 417 | le16_to_cpu(ra->restart_area_length) > |
| 418 | NTFS_BLOCK_SIZE - sizeof(u16)) { |
| 419 | ntfs_error(vi->i_sb, "Multi sector transfer error " |
| 420 | "detected in $LogFile restart page."); |
| 421 | err = -EINVAL; |
| 422 | goto err_out; |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 425 | /* |
| 426 | * If the restart page is modified by chkdsk or there are no active |
| 427 | * logfile clients, the logfile is consistent. Otherwise, need to |
| 428 | * check the log client records for consistency, too. |
| 429 | */ |
| 430 | err = 0; |
| 431 | if (ntfs_is_rstr_record(rp->magic) && |
| 432 | ra->client_in_use_list != LOGFILE_NO_CLIENT) { |
| 433 | if (!ntfs_check_log_client_array(vi, trp)) { |
| 434 | err = -EINVAL; |
| 435 | goto err_out; |
| 436 | } |
| 437 | } |
| 438 | if (lsn) { |
| 439 | if (ntfs_is_rstr_record(rp->magic)) |
| 440 | *lsn = sle64_to_cpu(ra->current_lsn); |
| 441 | else /* if (ntfs_is_chkd_record(rp->magic)) */ |
| 442 | *lsn = sle64_to_cpu(rp->chkdsk_lsn); |
| 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | ntfs_debug("Done."); |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 445 | if (wrp) |
| 446 | *wrp = trp; |
| 447 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | err_out: |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 449 | ntfs_free(trp); |
| 450 | } |
| 451 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
Anton Altaparmakov | 3bd1f4a | 2005-06-25 16:51:58 +0100 | [diff] [blame] | 455 | * ntfs_check_logfile - check the journal for consistency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | * @log_vi: struct inode of loaded journal $LogFile to check |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 457 | * @rp: [OUT] on success this is a copy of the current restart page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | * |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 459 | * Check the $LogFile journal for consistency and return 'true' if it is |
| 460 | * consistent and 'false' if not. On success, the current restart page is |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 461 | * returned in *@rp. Caller must call ntfs_free(*@rp) when finished with it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | * |
| 463 | * At present we only check the two restart pages and ignore the log record |
| 464 | * pages. |
| 465 | * |
| 466 | * Note that the MstProtected flag is not set on the $LogFile inode and hence |
| 467 | * when reading pages they are not deprotected. This is because we do not know |
| 468 | * if the $LogFile was created on a system with a different page size to ours |
| 469 | * yet and mst deprotection would fail if our page size is smaller. |
| 470 | */ |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 471 | bool ntfs_check_logfile(struct inode *log_vi, RESTART_PAGE_HEADER **rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 473 | s64 size, pos; |
| 474 | LSN rstr1_lsn, rstr2_lsn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | ntfs_volume *vol = NTFS_SB(log_vi->i_sb); |
| 476 | struct address_space *mapping = log_vi->i_mapping; |
| 477 | struct page *page = NULL; |
| 478 | u8 *kaddr = NULL; |
| 479 | RESTART_PAGE_HEADER *rstr1_ph = NULL; |
| 480 | RESTART_PAGE_HEADER *rstr2_ph = NULL; |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 481 | int log_page_size, log_page_mask, err; |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 482 | bool logfile_is_empty = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | u8 log_page_bits; |
| 484 | |
| 485 | ntfs_debug("Entering."); |
| 486 | /* An empty $LogFile must have been clean before it got emptied. */ |
| 487 | if (NVolLogFileEmpty(vol)) |
| 488 | goto is_empty; |
Anton Altaparmakov | 66129f8 | 2004-11-11 12:34:00 +0000 | [diff] [blame] | 489 | size = i_size_read(log_vi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | /* Make sure the file doesn't exceed the maximum allowed size. */ |
| 491 | if (size > MaxLogFileSize) |
| 492 | size = MaxLogFileSize; |
| 493 | /* |
| 494 | * Truncate size to a multiple of the page cache size or the default |
| 495 | * log page size if the page cache size is between the default log page |
| 496 | * log page size if the page cache size is between the default log page |
| 497 | * size and twice that. |
| 498 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 499 | if (PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | DefaultLogPageSize * 2) |
| 501 | log_page_size = DefaultLogPageSize; |
| 502 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 503 | log_page_size = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | log_page_mask = log_page_size - 1; |
| 505 | /* |
Akinobu Mita | b9a2838 | 2006-03-26 01:39:53 -0800 | [diff] [blame] | 506 | * Use ntfs_ffs() instead of ffs() to enable the compiler to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | * optimize log_page_size and log_page_bits into constants. |
| 508 | */ |
Akinobu Mita | b9a2838 | 2006-03-26 01:39:53 -0800 | [diff] [blame] | 509 | log_page_bits = ntfs_ffs(log_page_size) - 1; |
Anton Altaparmakov | 3bd1f4a | 2005-06-25 16:51:58 +0100 | [diff] [blame] | 510 | size &= ~(s64)(log_page_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | /* |
| 512 | * Ensure the log file is big enough to store at least the two restart |
| 513 | * pages and the minimum number of log record pages. |
| 514 | */ |
| 515 | if (size < log_page_size * 2 || (size - log_page_size * 2) >> |
| 516 | log_page_bits < MinLogRecordPages) { |
| 517 | ntfs_error(vol->sb, "$LogFile is too small."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 518 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | /* |
| 521 | * Read through the file looking for a restart page. Since the restart |
| 522 | * page header is at the beginning of a page we only need to search at |
| 523 | * what could be the beginning of a page (for each page size) rather |
| 524 | * than scanning the whole file byte by byte. If all potential places |
| 525 | * contain empty and uninitialzed records, the log file can be assumed |
| 526 | * to be empty. |
| 527 | */ |
| 528 | for (pos = 0; pos < size; pos <<= 1) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 529 | pgoff_t idx = pos >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | if (!page || page->index != idx) { |
| 531 | if (page) |
| 532 | ntfs_unmap_page(page); |
| 533 | page = ntfs_map_page(mapping, idx); |
| 534 | if (IS_ERR(page)) { |
| 535 | ntfs_error(vol->sb, "Error mapping $LogFile " |
| 536 | "page (index %lu).", idx); |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 537 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 540 | kaddr = (u8*)page_address(page) + (pos & ~PAGE_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /* |
| 542 | * A non-empty block means the logfile is not empty while an |
| 543 | * empty block after a non-empty block has been encountered |
| 544 | * means we are done. |
| 545 | */ |
| 546 | if (!ntfs_is_empty_recordp((le32*)kaddr)) |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 547 | logfile_is_empty = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | else if (!logfile_is_empty) |
| 549 | break; |
| 550 | /* |
| 551 | * A log record page means there cannot be a restart page after |
| 552 | * this so no need to continue searching. |
| 553 | */ |
| 554 | if (ntfs_is_rcrd_recordp((le32*)kaddr)) |
| 555 | break; |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 556 | /* If not a (modified by chkdsk) restart page, continue. */ |
| 557 | if (!ntfs_is_rstr_recordp((le32*)kaddr) && |
| 558 | !ntfs_is_chkd_recordp((le32*)kaddr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | if (!pos) |
| 560 | pos = NTFS_BLOCK_SIZE >> 1; |
| 561 | continue; |
| 562 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 563 | /* |
| 564 | * Check the (modified by chkdsk) restart page for consistency |
| 565 | * and get a copy of the complete multi sector transfer |
| 566 | * deprotected restart page. |
| 567 | */ |
| 568 | err = ntfs_check_and_load_restart_page(log_vi, |
| 569 | (RESTART_PAGE_HEADER*)kaddr, pos, |
| 570 | !rstr1_ph ? &rstr1_ph : &rstr2_ph, |
| 571 | !rstr1_ph ? &rstr1_lsn : &rstr2_lsn); |
| 572 | if (!err) { |
| 573 | /* |
| 574 | * If we have now found the first (modified by chkdsk) |
| 575 | * restart page, continue looking for the second one. |
| 576 | */ |
| 577 | if (!pos) { |
| 578 | pos = NTFS_BLOCK_SIZE >> 1; |
| 579 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 581 | /* |
| 582 | * We have now found the second (modified by chkdsk) |
| 583 | * restart page, so we can stop looking. |
| 584 | */ |
| 585 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | /* |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 588 | * Error output already done inside the function. Note, we do |
| 589 | * not abort if the restart page was invalid as we might still |
| 590 | * find a valid one further in the file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | */ |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 592 | if (err != -EINVAL) { |
| 593 | ntfs_unmap_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | goto err_out; |
| 595 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 596 | /* Continue looking. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | if (!pos) |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 598 | pos = NTFS_BLOCK_SIZE >> 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 600 | if (page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | ntfs_unmap_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if (logfile_is_empty) { |
| 603 | NVolSetLogFileEmpty(vol); |
| 604 | is_empty: |
| 605 | ntfs_debug("Done. ($LogFile is empty.)"); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 606 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 608 | if (!rstr1_ph) { |
| 609 | BUG_ON(rstr2_ph); |
| 610 | ntfs_error(vol->sb, "Did not find any restart pages in " |
| 611 | "$LogFile and it was not empty."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 612 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 614 | /* If both restart pages were found, use the more recent one. */ |
| 615 | if (rstr2_ph) { |
| 616 | /* |
| 617 | * If the second restart area is more recent, switch to it. |
| 618 | * Otherwise just throw it away. |
| 619 | */ |
| 620 | if (rstr2_lsn > rstr1_lsn) { |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 621 | ntfs_debug("Using second restart page as it is more " |
| 622 | "recent."); |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 623 | ntfs_free(rstr1_ph); |
| 624 | rstr1_ph = rstr2_ph; |
| 625 | /* rstr1_lsn = rstr2_lsn; */ |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 626 | } else { |
| 627 | ntfs_debug("Using first restart page as it is more " |
| 628 | "recent."); |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 629 | ntfs_free(rstr2_ph); |
Anton Altaparmakov | 5a8c0cc | 2005-09-26 10:48:54 +0100 | [diff] [blame] | 630 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 631 | rstr2_ph = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | /* All consistency checks passed. */ |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 634 | if (rp) |
| 635 | *rp = rstr1_ph; |
| 636 | else |
| 637 | ntfs_free(rstr1_ph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | ntfs_debug("Done."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 639 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | err_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | if (rstr1_ph) |
| 642 | ntfs_free(rstr1_ph); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 643 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /** |
| 647 | * ntfs_is_logfile_clean - check in the journal if the volume is clean |
| 648 | * @log_vi: struct inode of loaded journal $LogFile to check |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 649 | * @rp: copy of the current restart page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | * |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 651 | * Analyze the $LogFile journal and return 'true' if it indicates the volume was |
| 652 | * shutdown cleanly and 'false' if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | * |
| 654 | * At present we only look at the two restart pages and ignore the log record |
| 655 | * pages. This is a little bit crude in that there will be a very small number |
| 656 | * of cases where we think that a volume is dirty when in fact it is clean. |
| 657 | * This should only affect volumes that have not been shutdown cleanly but did |
| 658 | * not have any pending, non-check-pointed i/o, i.e. they were completely idle |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 659 | * at least for the five seconds preceding the unclean shutdown. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * |
| 661 | * This function assumes that the $LogFile journal has already been consistency |
| 662 | * checked by a call to ntfs_check_logfile() and in particular if the $LogFile |
| 663 | * is empty this function requires that NVolLogFileEmpty() is true otherwise an |
| 664 | * empty volume will be reported as dirty. |
| 665 | */ |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 666 | bool ntfs_is_logfile_clean(struct inode *log_vi, const RESTART_PAGE_HEADER *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | ntfs_volume *vol = NTFS_SB(log_vi->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | RESTART_AREA *ra; |
| 670 | |
| 671 | ntfs_debug("Entering."); |
| 672 | /* An empty $LogFile must have been clean before it got emptied. */ |
| 673 | if (NVolLogFileEmpty(vol)) { |
| 674 | ntfs_debug("Done. ($LogFile is empty.)"); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 675 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
Anton Altaparmakov | e7a1033 | 2005-09-08 16:12:28 +0100 | [diff] [blame] | 677 | BUG_ON(!rp); |
| 678 | if (!ntfs_is_rstr_record(rp->magic) && |
| 679 | !ntfs_is_chkd_record(rp->magic)) { |
| 680 | ntfs_error(vol->sb, "Restart page buffer is invalid. This is " |
| 681 | "probably a bug in that the $LogFile should " |
| 682 | "have been consistency checked before calling " |
| 683 | "this function."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 684 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); |
| 687 | /* |
| 688 | * If the $LogFile has active clients, i.e. it is open, and we do not |
| 689 | * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags, |
| 690 | * we assume there was an unclean shutdown. |
| 691 | */ |
| 692 | if (ra->client_in_use_list != LOGFILE_NO_CLIENT && |
| 693 | !(ra->flags & RESTART_VOLUME_IS_CLEAN)) { |
| 694 | ntfs_debug("Done. $LogFile indicates a dirty shutdown."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 695 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | /* $LogFile indicates a clean shutdown. */ |
| 698 | ntfs_debug("Done. $LogFile indicates a clean shutdown."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 699 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
| 703 | * ntfs_empty_logfile - empty the contents of the $LogFile journal |
| 704 | * @log_vi: struct inode of loaded journal $LogFile to empty |
| 705 | * |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 706 | * Empty the contents of the $LogFile journal @log_vi and return 'true' on |
| 707 | * success and 'false' on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | * |
| 709 | * This function assumes that the $LogFile journal has already been consistency |
| 710 | * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean() |
| 711 | * has been used to ensure that the $LogFile is clean. |
| 712 | */ |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 713 | bool ntfs_empty_logfile(struct inode *log_vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
Anton Altaparmakov | bfab36e | 2007-10-12 09:37:15 +0100 | [diff] [blame] | 715 | VCN vcn, end_vcn; |
| 716 | ntfs_inode *log_ni = NTFS_I(log_vi); |
| 717 | ntfs_volume *vol = log_ni->vol; |
| 718 | struct super_block *sb = vol->sb; |
| 719 | runlist_element *rl; |
| 720 | unsigned long flags; |
| 721 | unsigned block_size, block_size_bits; |
| 722 | int err; |
| 723 | bool should_wait = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | ntfs_debug("Entering."); |
Anton Altaparmakov | bfab36e | 2007-10-12 09:37:15 +0100 | [diff] [blame] | 726 | if (NVolLogFileEmpty(vol)) { |
| 727 | ntfs_debug("Done."); |
| 728 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
Anton Altaparmakov | bfab36e | 2007-10-12 09:37:15 +0100 | [diff] [blame] | 730 | /* |
| 731 | * We cannot use ntfs_attr_set() because we may be still in the middle |
| 732 | * of a mount operation. Thus we do the emptying by hand by first |
| 733 | * zapping the page cache pages for the $LogFile/$DATA attribute and |
| 734 | * then emptying each of the buffers in each of the clusters specified |
| 735 | * by the runlist by hand. |
| 736 | */ |
| 737 | block_size = sb->s_blocksize; |
| 738 | block_size_bits = sb->s_blocksize_bits; |
| 739 | vcn = 0; |
| 740 | read_lock_irqsave(&log_ni->size_lock, flags); |
| 741 | end_vcn = (log_ni->initialized_size + vol->cluster_size_mask) >> |
| 742 | vol->cluster_size_bits; |
| 743 | read_unlock_irqrestore(&log_ni->size_lock, flags); |
| 744 | truncate_inode_pages(log_vi->i_mapping, 0); |
| 745 | down_write(&log_ni->runlist.lock); |
| 746 | rl = log_ni->runlist.rl; |
| 747 | if (unlikely(!rl || vcn < rl->vcn || !rl->length)) { |
| 748 | map_vcn: |
| 749 | err = ntfs_map_runlist_nolock(log_ni, vcn, NULL); |
| 750 | if (err) { |
| 751 | ntfs_error(sb, "Failed to map runlist fragment (error " |
| 752 | "%d).", -err); |
| 753 | goto err; |
| 754 | } |
| 755 | rl = log_ni->runlist.rl; |
| 756 | BUG_ON(!rl || vcn < rl->vcn || !rl->length); |
| 757 | } |
| 758 | /* Seek to the runlist element containing @vcn. */ |
| 759 | while (rl->length && vcn >= rl[1].vcn) |
| 760 | rl++; |
| 761 | do { |
| 762 | LCN lcn; |
| 763 | sector_t block, end_block; |
| 764 | s64 len; |
| 765 | |
| 766 | /* |
| 767 | * If this run is not mapped map it now and start again as the |
| 768 | * runlist will have been updated. |
| 769 | */ |
| 770 | lcn = rl->lcn; |
| 771 | if (unlikely(lcn == LCN_RL_NOT_MAPPED)) { |
| 772 | vcn = rl->vcn; |
| 773 | goto map_vcn; |
| 774 | } |
| 775 | /* If this run is not valid abort with an error. */ |
| 776 | if (unlikely(!rl->length || lcn < LCN_HOLE)) |
| 777 | goto rl_err; |
| 778 | /* Skip holes. */ |
| 779 | if (lcn == LCN_HOLE) |
| 780 | continue; |
| 781 | block = lcn << vol->cluster_size_bits >> block_size_bits; |
| 782 | len = rl->length; |
| 783 | if (rl[1].vcn > end_vcn) |
| 784 | len = end_vcn - rl->vcn; |
| 785 | end_block = (lcn + len) << vol->cluster_size_bits >> |
| 786 | block_size_bits; |
| 787 | /* Iterate over the blocks in the run and empty them. */ |
| 788 | do { |
| 789 | struct buffer_head *bh; |
| 790 | |
| 791 | /* Obtain the buffer, possibly not uptodate. */ |
| 792 | bh = sb_getblk(sb, block); |
| 793 | BUG_ON(!bh); |
| 794 | /* Setup buffer i/o submission. */ |
| 795 | lock_buffer(bh); |
| 796 | bh->b_end_io = end_buffer_write_sync; |
| 797 | get_bh(bh); |
| 798 | /* Set the entire contents of the buffer to 0xff. */ |
| 799 | memset(bh->b_data, -1, block_size); |
| 800 | if (!buffer_uptodate(bh)) |
| 801 | set_buffer_uptodate(bh); |
| 802 | if (buffer_dirty(bh)) |
| 803 | clear_buffer_dirty(bh); |
| 804 | /* |
| 805 | * Submit the buffer and wait for i/o to complete but |
| 806 | * only for the first buffer so we do not miss really |
| 807 | * serious i/o errors. Once the first buffer has |
| 808 | * completed ignore errors afterwards as we can assume |
| 809 | * that if one buffer worked all of them will work. |
| 810 | */ |
Mike Christie | 2a222ca | 2016-06-05 14:31:43 -0500 | [diff] [blame] | 811 | submit_bh(REQ_OP_WRITE, 0, bh); |
Anton Altaparmakov | bfab36e | 2007-10-12 09:37:15 +0100 | [diff] [blame] | 812 | if (should_wait) { |
| 813 | should_wait = false; |
| 814 | wait_on_buffer(bh); |
| 815 | if (unlikely(!buffer_uptodate(bh))) |
| 816 | goto io_err; |
| 817 | } |
| 818 | brelse(bh); |
| 819 | } while (++block < end_block); |
| 820 | } while ((++rl)->vcn < end_vcn); |
| 821 | up_write(&log_ni->runlist.lock); |
| 822 | /* |
| 823 | * Zap the pages again just in case any got instantiated whilst we were |
| 824 | * emptying the blocks by hand. FIXME: We may not have completed |
| 825 | * writing to all the buffer heads yet so this may happen too early. |
| 826 | * We really should use a kernel thread to do the emptying |
| 827 | * asynchronously and then we can also set the volume dirty and output |
| 828 | * an error message if emptying should fail. |
| 829 | */ |
| 830 | truncate_inode_pages(log_vi->i_mapping, 0); |
| 831 | /* Set the flag so we do not have to do it again on remount. */ |
| 832 | NVolSetLogFileEmpty(vol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | ntfs_debug("Done."); |
Richard Knutsson | c49c311 | 2006-09-30 23:27:12 -0700 | [diff] [blame] | 834 | return true; |
Anton Altaparmakov | bfab36e | 2007-10-12 09:37:15 +0100 | [diff] [blame] | 835 | io_err: |
| 836 | ntfs_error(sb, "Failed to write buffer. Unmount and run chkdsk."); |
| 837 | goto dirty_err; |
| 838 | rl_err: |
| 839 | ntfs_error(sb, "Runlist is corrupt. Unmount and run chkdsk."); |
| 840 | dirty_err: |
| 841 | NVolSetErrors(vol); |
| 842 | err = -EIO; |
| 843 | err: |
| 844 | up_write(&log_ni->runlist.lock); |
| 845 | ntfs_error(sb, "Failed to fill $LogFile with 0xff bytes (error %d).", |
| 846 | -err); |
| 847 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | #endif /* NTFS_RW */ |