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