📄 logfile.c
字号:
/* * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project. * * Copyright (c) 2002-2007 Anton Altaparmakov * * This program/include file is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program/include file is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (in the main directory of the Linux-NTFS * distribution in the file COPYING); if not, write to the Free Software * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#ifdef NTFS_RW#include <linux/types.h>#include <linux/fs.h>#include <linux/highmem.h>#include <linux/buffer_head.h>#include <linux/bitops.h>#include "attrib.h"#include "aops.h"#include "debug.h"#include "logfile.h"#include "malloc.h"#include "volume.h"#include "ntfs.h"/** * ntfs_check_restart_page_header - check the page header for consistency * @vi: $LogFile inode to which the restart page header belongs * @rp: restart page header to check * @pos: position in @vi at which the restart page header resides * * Check the restart page header @rp for consistency and return 'true' if it is * consistent and 'false' otherwise. * * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not * require the full restart page. */static bool ntfs_check_restart_page_header(struct inode *vi, RESTART_PAGE_HEADER *rp, s64 pos){ u32 logfile_system_page_size, logfile_log_page_size; u16 ra_ofs, usa_count, usa_ofs, usa_end = 0; bool have_usa = true; ntfs_debug("Entering."); /* * If the system or log page sizes are smaller than the ntfs block size * or either is not a power of 2 we cannot handle this log file. */ logfile_system_page_size = le32_to_cpu(rp->system_page_size); logfile_log_page_size = le32_to_cpu(rp->log_page_size); if (logfile_system_page_size < NTFS_BLOCK_SIZE || logfile_log_page_size < NTFS_BLOCK_SIZE || logfile_system_page_size & (logfile_system_page_size - 1) || logfile_log_page_size & (logfile_log_page_size - 1)) { ntfs_error(vi->i_sb, "$LogFile uses unsupported page size."); return false; } /* * We must be either at !pos (1st restart page) or at pos = system page * size (2nd restart page). */ if (pos && pos != logfile_system_page_size) { ntfs_error(vi->i_sb, "Found restart area in incorrect " "position in $LogFile."); return false; } /* We only know how to handle version 1.1. */ if (sle16_to_cpu(rp->major_ver) != 1 || sle16_to_cpu(rp->minor_ver) != 1) { ntfs_error(vi->i_sb, "$LogFile version %i.%i is not " "supported. (This driver supports version " "1.1 only.)", (int)sle16_to_cpu(rp->major_ver), (int)sle16_to_cpu(rp->minor_ver)); return false; } /* * If chkdsk has been run the restart page may not be protected by an * update sequence array. */ if (ntfs_is_chkd_record(rp->magic) && !le16_to_cpu(rp->usa_count)) { have_usa = false; goto skip_usa_checks; } /* Verify the size of the update sequence array. */ usa_count = 1 + (logfile_system_page_size >> NTFS_BLOCK_SIZE_BITS); if (usa_count != le16_to_cpu(rp->usa_count)) { ntfs_error(vi->i_sb, "$LogFile restart page specifies " "inconsistent update sequence array count."); return false; } /* Verify the position of the update sequence array. */ usa_ofs = le16_to_cpu(rp->usa_ofs); usa_end = usa_ofs + usa_count * sizeof(u16); if (usa_ofs < sizeof(RESTART_PAGE_HEADER) || usa_end > NTFS_BLOCK_SIZE - sizeof(u16)) { ntfs_error(vi->i_sb, "$LogFile restart page specifies " "inconsistent update sequence array offset."); return false; }skip_usa_checks: /* * Verify the position of the restart area. It must be: * - aligned to 8-byte boundary, * - after the update sequence array, and * - within the system page size. */ ra_ofs = le16_to_cpu(rp->restart_area_offset); if (ra_ofs & 7 || (have_usa ? ra_ofs < usa_end : ra_ofs < sizeof(RESTART_PAGE_HEADER)) || ra_ofs > logfile_system_page_size) { ntfs_error(vi->i_sb, "$LogFile restart page specifies " "inconsistent restart area offset."); return false; } /* * Only restart pages modified by chkdsk are allowed to have chkdsk_lsn * set. */ if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) { ntfs_error(vi->i_sb, "$LogFile restart page is not modified " "by chkdsk but a chkdsk LSN is specified."); return false; } ntfs_debug("Done."); return true;}/** * ntfs_check_restart_area - check the restart area for consistency * @vi: $LogFile inode to which the restart page belongs * @rp: restart page whose restart area to check * * Check the restart area of the restart page @rp for consistency and return * 'true' if it is consistent and 'false' otherwise. * * This function assumes that the restart page header has already been * consistency checked. * * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not * require the full restart page. */static bool ntfs_check_restart_area(struct inode *vi, RESTART_PAGE_HEADER *rp){ u64 file_size; RESTART_AREA *ra; u16 ra_ofs, ra_len, ca_ofs; u8 fs_bits; ntfs_debug("Entering."); ra_ofs = le16_to_cpu(rp->restart_area_offset); ra = (RESTART_AREA*)((u8*)rp + ra_ofs); /* * Everything before ra->file_size must be before the first word * protected by an update sequence number. This ensures that it is * safe to access ra->client_array_offset. */ if (ra_ofs + offsetof(RESTART_AREA, file_size) > NTFS_BLOCK_SIZE - sizeof(u16)) { ntfs_error(vi->i_sb, "$LogFile restart area specifies " "inconsistent file offset."); return false; } /* * Now that we can access ra->client_array_offset, make sure everything * up to the log client array is before the first word protected by an * update sequence number. This ensures we can access all of the * restart area elements safely. Also, the client array offset must be * aligned to an 8-byte boundary. */ ca_ofs = le16_to_cpu(ra->client_array_offset); if (((ca_ofs + 7) & ~7) != ca_ofs || ra_ofs + ca_ofs > NTFS_BLOCK_SIZE - sizeof(u16)) { ntfs_error(vi->i_sb, "$LogFile restart area specifies " "inconsistent client array offset."); return false; } /* * The restart area must end within the system page size both when * calculated manually and as specified by ra->restart_area_length. * Also, the calculated length must not exceed the specified length. */ ra_len = ca_ofs + le16_to_cpu(ra->log_clients) * sizeof(LOG_CLIENT_RECORD); if (ra_ofs + ra_len > le32_to_cpu(rp->system_page_size) || ra_ofs + le16_to_cpu(ra->restart_area_length) > le32_to_cpu(rp->system_page_size) || ra_len > le16_to_cpu(ra->restart_area_length)) { ntfs_error(vi->i_sb, "$LogFile restart area is out of bounds " "of the system page size specified by the " "restart page header and/or the specified " "restart area length is inconsistent."); return false; } /* * The ra->client_free_list and ra->client_in_use_list must be either * LOGFILE_NO_CLIENT or less than ra->log_clients or they are * overflowing the client array. */ if ((ra->client_free_list != LOGFILE_NO_CLIENT && le16_to_cpu(ra->client_free_list) >= le16_to_cpu(ra->log_clients)) || (ra->client_in_use_list != LOGFILE_NO_CLIENT && le16_to_cpu(ra->client_in_use_list) >= le16_to_cpu(ra->log_clients))) { ntfs_error(vi->i_sb, "$LogFile restart area specifies " "overflowing client free and/or in use lists."); return false; } /* * Check ra->seq_number_bits against ra->file_size for consistency. * We cannot just use ffs() because the file size is not a power of 2. */ file_size = (u64)sle64_to_cpu(ra->file_size); fs_bits = 0; while (file_size) { file_size >>= 1; fs_bits++; } if (le32_to_cpu(ra->seq_number_bits) != 67 - fs_bits) { ntfs_error(vi->i_sb, "$LogFile restart area specifies " "inconsistent sequence number bits."); return false; } /* The log record header length must be a multiple of 8. */ if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) != le16_to_cpu(ra->log_record_header_length)) { ntfs_error(vi->i_sb, "$LogFile restart area specifies " "inconsistent log record header length."); return false; } /* Dito for the log page data offset. */ if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) != le16_to_cpu(ra->log_page_data_offset)) { ntfs_error(vi->i_sb, "$LogFile restart area specifies " "inconsistent log page data offset."); return false; } ntfs_debug("Done."); return true;}/** * ntfs_check_log_client_array - check the log client array for consistency * @vi: $LogFile inode to which the restart page belongs * @rp: restart page whose log client array to check * * Check the log client array of the restart page @rp for consistency and * return 'true' if it is consistent and 'false' otherwise. * * This function assumes that the restart page header and the restart area have * already been consistency checked. * * Unlike ntfs_check_restart_page_header() and ntfs_check_restart_area(), this * function needs @rp->system_page_size bytes in @rp, i.e. it requires the full * restart page and the page must be multi sector transfer deprotected. */static bool ntfs_check_log_client_array(struct inode *vi, RESTART_PAGE_HEADER *rp){ RESTART_AREA *ra; LOG_CLIENT_RECORD *ca, *cr; u16 nr_clients, idx; bool in_free_list, idx_is_first; ntfs_debug("Entering."); ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); ca = (LOG_CLIENT_RECORD*)((u8*)ra + le16_to_cpu(ra->client_array_offset)); /* * Check the ra->client_free_list first and then check the * ra->client_in_use_list. Check each of the log client records in * each of the lists and check that the array does not overflow the * ra->log_clients value. Also keep track of the number of records * visited as there cannot be more than ra->log_clients records and * that way we detect eventual loops in within a list. */ nr_clients = le16_to_cpu(ra->log_clients); idx = le16_to_cpu(ra->client_free_list); in_free_list = true;check_list: for (idx_is_first = true; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--, idx = le16_to_cpu(cr->next_client)) { if (!nr_clients || idx >= le16_to_cpu(ra->log_clients)) goto err_out; /* Set @cr to the current log client record. */ cr = ca + idx; /* The first log client record must not have a prev_client. */ if (idx_is_first) { if (cr->prev_client != LOGFILE_NO_CLIENT) goto err_out; idx_is_first = false; } } /* Switch to and check the in use list if we just did the free list. */ if (in_free_list) { in_free_list = false; idx = le16_to_cpu(ra->client_in_use_list); goto check_list; } ntfs_debug("Done."); return true;err_out: ntfs_error(vi->i_sb, "$LogFile log client array is corrupt."); return false;}/** * ntfs_check_and_load_restart_page - check the restart page for consistency * @vi: $LogFile inode to which the restart page belongs * @rp: restart page to check * @pos: position in @vi at which the restart page resides * @wrp: [OUT] copy of the multi sector transfer deprotected restart page * @lsn: [OUT] set to the current logfile lsn on success * * Check the restart page @rp for consistency and return 0 if it is consistent * and -errno otherwise. The restart page may have been modified by chkdsk in * which case its magic is CHKD instead of RSTR. * * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not * require the full restart page. * * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a * copy of the complete multi sector transfer deprotected page. On failure, * *@wrp is undefined. * * Simillarly, if @lsn is not NULL, on succes *@lsn will be set to the current * logfile lsn according to this restart page. On failure, *@lsn is undefined. * * The following error codes are defined: * -EINVAL - The restart page is inconsistent. * -ENOMEM - Not enough memory to load the restart page. * -EIO - Failed to reading from $LogFile. */static int ntfs_check_and_load_restart_page(struct inode *vi, RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp, LSN *lsn){ RESTART_AREA *ra; RESTART_PAGE_HEADER *trp; int size, err; ntfs_debug("Entering."); /* Check the restart page header for consistency. */ if (!ntfs_check_restart_page_header(vi, rp, pos)) { /* Error output already done inside the function. */ return -EINVAL; } /* Check the restart area for consistency. */ if (!ntfs_check_restart_area(vi, rp)) { /* Error output already done inside the function. */ return -EINVAL; } ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); /* * Allocate a buffer to store the whole restart page so we can multi * sector transfer deprotect it. */ trp = ntfs_malloc_nofs(le32_to_cpu(rp->system_page_size)); if (!trp) { ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile " "restart page buffer."); return -ENOMEM; } /* * Read the whole of the restart page into the buffer. If it fits * completely inside @rp, just copy it from there. Otherwise map all * the required pages and copy the data from them. */ size = PAGE_CACHE_SIZE - (pos & ~PAGE_CACHE_MASK); if (size >= le32_to_cpu(rp->system_page_size)) { memcpy(trp, rp, le32_to_cpu(rp->system_page_size)); } else { pgoff_t idx; struct page *page; int have_read, to_read; /* First copy what we already have in @rp. */ memcpy(trp, rp, size); /* Copy the remaining data one page at a time. */ have_read = size; to_read = le32_to_cpu(rp->system_page_size) - size; idx = (pos + size) >> PAGE_CACHE_SHIFT; BUG_ON((pos + size) & ~PAGE_CACHE_MASK); do { page = ntfs_map_page(vi->i_mapping, idx); if (IS_ERR(page)) { ntfs_error(vi->i_sb, "Error mapping $LogFile " "page (index %lu).", idx); err = PTR_ERR(page); if (err != -EIO && err != -ENOMEM) err = -EIO; goto err_out; } size = min_t(int, to_read, PAGE_CACHE_SIZE); memcpy((u8*)trp + have_read, page_address(page), size); ntfs_unmap_page(page); have_read += size; to_read -= size; idx++; } while (to_read > 0); } /* * Perform the multi sector transfer deprotection on the buffer if the * restart page is protected. */ if ((!ntfs_is_chkd_record(trp->magic) || le16_to_cpu(trp->usa_count)) && post_read_mst_fixup((NTFS_RECORD*)trp, le32_to_cpu(rp->system_page_size))) { /* * A multi sector tranfer error was detected. We only need to * abort if the restart page contents exceed the multi sector * transfer fixup of the first sector. */ if (le16_to_cpu(rp->restart_area_offset) + le16_to_cpu(ra->restart_area_length) > NTFS_BLOCK_SIZE - sizeof(u16)) { ntfs_error(vi->i_sb, "Multi sector transfer error " "detected in $LogFile restart page.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -