Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project.
0004  *
0005  * Copyright (c) 2002-2007 Anton Altaparmakov
0006  */
0007 
0008 #ifdef NTFS_RW
0009 
0010 #include <linux/types.h>
0011 #include <linux/fs.h>
0012 #include <linux/highmem.h>
0013 #include <linux/buffer_head.h>
0014 #include <linux/bitops.h>
0015 #include <linux/log2.h>
0016 #include <linux/bio.h>
0017 
0018 #include "attrib.h"
0019 #include "aops.h"
0020 #include "debug.h"
0021 #include "logfile.h"
0022 #include "malloc.h"
0023 #include "volume.h"
0024 #include "ntfs.h"
0025 
0026 /**
0027  * ntfs_check_restart_page_header - check the page header for consistency
0028  * @vi:     $LogFile inode to which the restart page header belongs
0029  * @rp:     restart page header to check
0030  * @pos:    position in @vi at which the restart page header resides
0031  *
0032  * Check the restart page header @rp for consistency and return 'true' if it is
0033  * consistent and 'false' otherwise.
0034  *
0035  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
0036  * require the full restart page.
0037  */
0038 static bool ntfs_check_restart_page_header(struct inode *vi,
0039         RESTART_PAGE_HEADER *rp, s64 pos)
0040 {
0041     u32 logfile_system_page_size, logfile_log_page_size;
0042     u16 ra_ofs, usa_count, usa_ofs, usa_end = 0;
0043     bool have_usa = true;
0044 
0045     ntfs_debug("Entering.");
0046     /*
0047      * If the system or log page sizes are smaller than the ntfs block size
0048      * or either is not a power of 2 we cannot handle this log file.
0049      */
0050     logfile_system_page_size = le32_to_cpu(rp->system_page_size);
0051     logfile_log_page_size = le32_to_cpu(rp->log_page_size);
0052     if (logfile_system_page_size < NTFS_BLOCK_SIZE ||
0053             logfile_log_page_size < NTFS_BLOCK_SIZE ||
0054             logfile_system_page_size &
0055             (logfile_system_page_size - 1) ||
0056             !is_power_of_2(logfile_log_page_size)) {
0057         ntfs_error(vi->i_sb, "$LogFile uses unsupported page size.");
0058         return false;
0059     }
0060     /*
0061      * We must be either at !pos (1st restart page) or at pos = system page
0062      * size (2nd restart page).
0063      */
0064     if (pos && pos != logfile_system_page_size) {
0065         ntfs_error(vi->i_sb, "Found restart area in incorrect "
0066                 "position in $LogFile.");
0067         return false;
0068     }
0069     /* We only know how to handle version 1.1. */
0070     if (sle16_to_cpu(rp->major_ver) != 1 ||
0071             sle16_to_cpu(rp->minor_ver) != 1) {
0072         ntfs_error(vi->i_sb, "$LogFile version %i.%i is not "
0073                 "supported.  (This driver supports version "
0074                 "1.1 only.)", (int)sle16_to_cpu(rp->major_ver),
0075                 (int)sle16_to_cpu(rp->minor_ver));
0076         return false;
0077     }
0078     /*
0079      * If chkdsk has been run the restart page may not be protected by an
0080      * update sequence array.
0081      */
0082     if (ntfs_is_chkd_record(rp->magic) && !le16_to_cpu(rp->usa_count)) {
0083         have_usa = false;
0084         goto skip_usa_checks;
0085     }
0086     /* Verify the size of the update sequence array. */
0087     usa_count = 1 + (logfile_system_page_size >> NTFS_BLOCK_SIZE_BITS);
0088     if (usa_count != le16_to_cpu(rp->usa_count)) {
0089         ntfs_error(vi->i_sb, "$LogFile restart page specifies "
0090                 "inconsistent update sequence array count.");
0091         return false;
0092     }
0093     /* Verify the position of the update sequence array. */
0094     usa_ofs = le16_to_cpu(rp->usa_ofs);
0095     usa_end = usa_ofs + usa_count * sizeof(u16);
0096     if (usa_ofs < sizeof(RESTART_PAGE_HEADER) ||
0097             usa_end > NTFS_BLOCK_SIZE - sizeof(u16)) {
0098         ntfs_error(vi->i_sb, "$LogFile restart page specifies "
0099                 "inconsistent update sequence array offset.");
0100         return false;
0101     }
0102 skip_usa_checks:
0103     /*
0104      * Verify the position of the restart area.  It must be:
0105      *  - aligned to 8-byte boundary,
0106      *  - after the update sequence array, and
0107      *  - within the system page size.
0108      */
0109     ra_ofs = le16_to_cpu(rp->restart_area_offset);
0110     if (ra_ofs & 7 || (have_usa ? ra_ofs < usa_end :
0111             ra_ofs < sizeof(RESTART_PAGE_HEADER)) ||
0112             ra_ofs > logfile_system_page_size) {
0113         ntfs_error(vi->i_sb, "$LogFile restart page specifies "
0114                 "inconsistent restart area offset.");
0115         return false;
0116     }
0117     /*
0118      * Only restart pages modified by chkdsk are allowed to have chkdsk_lsn
0119      * set.
0120      */
0121     if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) {
0122         ntfs_error(vi->i_sb, "$LogFile restart page is not modified "
0123                 "by chkdsk but a chkdsk LSN is specified.");
0124         return false;
0125     }
0126     ntfs_debug("Done.");
0127     return true;
0128 }
0129 
0130 /**
0131  * ntfs_check_restart_area - check the restart area for consistency
0132  * @vi:     $LogFile inode to which the restart page belongs
0133  * @rp:     restart page whose restart area to check
0134  *
0135  * Check the restart area of the restart page @rp for consistency and return
0136  * 'true' if it is consistent and 'false' otherwise.
0137  *
0138  * This function assumes that the restart page header has already been
0139  * consistency checked.
0140  *
0141  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
0142  * require the full restart page.
0143  */
0144 static bool ntfs_check_restart_area(struct inode *vi, RESTART_PAGE_HEADER *rp)
0145 {
0146     u64 file_size;
0147     RESTART_AREA *ra;
0148     u16 ra_ofs, ra_len, ca_ofs;
0149     u8 fs_bits;
0150 
0151     ntfs_debug("Entering.");
0152     ra_ofs = le16_to_cpu(rp->restart_area_offset);
0153     ra = (RESTART_AREA*)((u8*)rp + ra_ofs);
0154     /*
0155      * Everything before ra->file_size must be before the first word
0156      * protected by an update sequence number.  This ensures that it is
0157      * safe to access ra->client_array_offset.
0158      */
0159     if (ra_ofs + offsetof(RESTART_AREA, file_size) >
0160             NTFS_BLOCK_SIZE - sizeof(u16)) {
0161         ntfs_error(vi->i_sb, "$LogFile restart area specifies "
0162                 "inconsistent file offset.");
0163         return false;
0164     }
0165     /*
0166      * Now that we can access ra->client_array_offset, make sure everything
0167      * up to the log client array is before the first word protected by an
0168      * update sequence number.  This ensures we can access all of the
0169      * restart area elements safely.  Also, the client array offset must be
0170      * aligned to an 8-byte boundary.
0171      */
0172     ca_ofs = le16_to_cpu(ra->client_array_offset);
0173     if (((ca_ofs + 7) & ~7) != ca_ofs ||
0174             ra_ofs + ca_ofs > NTFS_BLOCK_SIZE - sizeof(u16)) {
0175         ntfs_error(vi->i_sb, "$LogFile restart area specifies "
0176                 "inconsistent client array offset.");
0177         return false;
0178     }
0179     /*
0180      * The restart area must end within the system page size both when
0181      * calculated manually and as specified by ra->restart_area_length.
0182      * Also, the calculated length must not exceed the specified length.
0183      */
0184     ra_len = ca_ofs + le16_to_cpu(ra->log_clients) *
0185             sizeof(LOG_CLIENT_RECORD);
0186     if (ra_ofs + ra_len > le32_to_cpu(rp->system_page_size) ||
0187             ra_ofs + le16_to_cpu(ra->restart_area_length) >
0188             le32_to_cpu(rp->system_page_size) ||
0189             ra_len > le16_to_cpu(ra->restart_area_length)) {
0190         ntfs_error(vi->i_sb, "$LogFile restart area is out of bounds "
0191                 "of the system page size specified by the "
0192                 "restart page header and/or the specified "
0193                 "restart area length is inconsistent.");
0194         return false;
0195     }
0196     /*
0197      * The ra->client_free_list and ra->client_in_use_list must be either
0198      * LOGFILE_NO_CLIENT or less than ra->log_clients or they are
0199      * overflowing the client array.
0200      */
0201     if ((ra->client_free_list != LOGFILE_NO_CLIENT &&
0202             le16_to_cpu(ra->client_free_list) >=
0203             le16_to_cpu(ra->log_clients)) ||
0204             (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
0205             le16_to_cpu(ra->client_in_use_list) >=
0206             le16_to_cpu(ra->log_clients))) {
0207         ntfs_error(vi->i_sb, "$LogFile restart area specifies "
0208                 "overflowing client free and/or in use lists.");
0209         return false;
0210     }
0211     /*
0212      * Check ra->seq_number_bits against ra->file_size for consistency.
0213      * We cannot just use ffs() because the file size is not a power of 2.
0214      */
0215     file_size = (u64)sle64_to_cpu(ra->file_size);
0216     fs_bits = 0;
0217     while (file_size) {
0218         file_size >>= 1;
0219         fs_bits++;
0220     }
0221     if (le32_to_cpu(ra->seq_number_bits) != 67 - fs_bits) {
0222         ntfs_error(vi->i_sb, "$LogFile restart area specifies "
0223                 "inconsistent sequence number bits.");
0224         return false;
0225     }
0226     /* The log record header length must be a multiple of 8. */
0227     if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) !=
0228             le16_to_cpu(ra->log_record_header_length)) {
0229         ntfs_error(vi->i_sb, "$LogFile restart area specifies "
0230                 "inconsistent log record header length.");
0231         return false;
0232     }
0233     /* Dito for the log page data offset. */
0234     if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) !=
0235             le16_to_cpu(ra->log_page_data_offset)) {
0236         ntfs_error(vi->i_sb, "$LogFile restart area specifies "
0237                 "inconsistent log page data offset.");
0238         return false;
0239     }
0240     ntfs_debug("Done.");
0241     return true;
0242 }
0243 
0244 /**
0245  * ntfs_check_log_client_array - check the log client array for consistency
0246  * @vi:     $LogFile inode to which the restart page belongs
0247  * @rp:     restart page whose log client array to check
0248  *
0249  * Check the log client array of the restart page @rp for consistency and
0250  * return 'true' if it is consistent and 'false' otherwise.
0251  *
0252  * This function assumes that the restart page header and the restart area have
0253  * already been consistency checked.
0254  *
0255  * Unlike ntfs_check_restart_page_header() and ntfs_check_restart_area(), this
0256  * function needs @rp->system_page_size bytes in @rp, i.e. it requires the full
0257  * restart page and the page must be multi sector transfer deprotected.
0258  */
0259 static bool ntfs_check_log_client_array(struct inode *vi,
0260         RESTART_PAGE_HEADER *rp)
0261 {
0262     RESTART_AREA *ra;
0263     LOG_CLIENT_RECORD *ca, *cr;
0264     u16 nr_clients, idx;
0265     bool in_free_list, idx_is_first;
0266 
0267     ntfs_debug("Entering.");
0268     ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
0269     ca = (LOG_CLIENT_RECORD*)((u8*)ra +
0270             le16_to_cpu(ra->client_array_offset));
0271     /*
0272      * Check the ra->client_free_list first and then check the
0273      * ra->client_in_use_list.  Check each of the log client records in
0274      * each of the lists and check that the array does not overflow the
0275      * ra->log_clients value.  Also keep track of the number of records
0276      * visited as there cannot be more than ra->log_clients records and
0277      * that way we detect eventual loops in within a list.
0278      */
0279     nr_clients = le16_to_cpu(ra->log_clients);
0280     idx = le16_to_cpu(ra->client_free_list);
0281     in_free_list = true;
0282 check_list:
0283     for (idx_is_first = true; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--,
0284             idx = le16_to_cpu(cr->next_client)) {
0285         if (!nr_clients || idx >= le16_to_cpu(ra->log_clients))
0286             goto err_out;
0287         /* Set @cr to the current log client record. */
0288         cr = ca + idx;
0289         /* The first log client record must not have a prev_client. */
0290         if (idx_is_first) {
0291             if (cr->prev_client != LOGFILE_NO_CLIENT)
0292                 goto err_out;
0293             idx_is_first = false;
0294         }
0295     }
0296     /* Switch to and check the in use list if we just did the free list. */
0297     if (in_free_list) {
0298         in_free_list = false;
0299         idx = le16_to_cpu(ra->client_in_use_list);
0300         goto check_list;
0301     }
0302     ntfs_debug("Done.");
0303     return true;
0304 err_out:
0305     ntfs_error(vi->i_sb, "$LogFile log client array is corrupt.");
0306     return false;
0307 }
0308 
0309 /**
0310  * ntfs_check_and_load_restart_page - check the restart page for consistency
0311  * @vi:     $LogFile inode to which the restart page belongs
0312  * @rp:     restart page to check
0313  * @pos:    position in @vi at which the restart page resides
0314  * @wrp:    [OUT] copy of the multi sector transfer deprotected restart page
0315  * @lsn:    [OUT] set to the current logfile lsn on success
0316  *
0317  * Check the restart page @rp for consistency and return 0 if it is consistent
0318  * and -errno otherwise.  The restart page may have been modified by chkdsk in
0319  * which case its magic is CHKD instead of RSTR.
0320  *
0321  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
0322  * require the full restart page.
0323  *
0324  * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a
0325  * copy of the complete multi sector transfer deprotected page.  On failure,
0326  * *@wrp is undefined.
0327  *
0328  * Simillarly, if @lsn is not NULL, on success *@lsn will be set to the current
0329  * logfile lsn according to this restart page.  On failure, *@lsn is undefined.
0330  *
0331  * The following error codes are defined:
0332  *  -EINVAL - The restart page is inconsistent.
0333  *  -ENOMEM - Not enough memory to load the restart page.
0334  *  -EIO    - Failed to reading from $LogFile.
0335  */
0336 static int ntfs_check_and_load_restart_page(struct inode *vi,
0337         RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp,
0338         LSN *lsn)
0339 {
0340     RESTART_AREA *ra;
0341     RESTART_PAGE_HEADER *trp;
0342     int size, err;
0343 
0344     ntfs_debug("Entering.");
0345     /* Check the restart page header for consistency. */
0346     if (!ntfs_check_restart_page_header(vi, rp, pos)) {
0347         /* Error output already done inside the function. */
0348         return -EINVAL;
0349     }
0350     /* Check the restart area for consistency. */
0351     if (!ntfs_check_restart_area(vi, rp)) {
0352         /* Error output already done inside the function. */
0353         return -EINVAL;
0354     }
0355     ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
0356     /*
0357      * Allocate a buffer to store the whole restart page so we can multi
0358      * sector transfer deprotect it.
0359      */
0360     trp = ntfs_malloc_nofs(le32_to_cpu(rp->system_page_size));
0361     if (!trp) {
0362         ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile "
0363                 "restart page buffer.");
0364         return -ENOMEM;
0365     }
0366     /*
0367      * Read the whole of the restart page into the buffer.  If it fits
0368      * completely inside @rp, just copy it from there.  Otherwise map all
0369      * the required pages and copy the data from them.
0370      */
0371     size = PAGE_SIZE - (pos & ~PAGE_MASK);
0372     if (size >= le32_to_cpu(rp->system_page_size)) {
0373         memcpy(trp, rp, le32_to_cpu(rp->system_page_size));
0374     } else {
0375         pgoff_t idx;
0376         struct page *page;
0377         int have_read, to_read;
0378 
0379         /* First copy what we already have in @rp. */
0380         memcpy(trp, rp, size);
0381         /* Copy the remaining data one page at a time. */
0382         have_read = size;
0383         to_read = le32_to_cpu(rp->system_page_size) - size;
0384         idx = (pos + size) >> PAGE_SHIFT;
0385         BUG_ON((pos + size) & ~PAGE_MASK);
0386         do {
0387             page = ntfs_map_page(vi->i_mapping, idx);
0388             if (IS_ERR(page)) {
0389                 ntfs_error(vi->i_sb, "Error mapping $LogFile "
0390                         "page (index %lu).", idx);
0391                 err = PTR_ERR(page);
0392                 if (err != -EIO && err != -ENOMEM)
0393                     err = -EIO;
0394                 goto err_out;
0395             }
0396             size = min_t(int, to_read, PAGE_SIZE);
0397             memcpy((u8*)trp + have_read, page_address(page), size);
0398             ntfs_unmap_page(page);
0399             have_read += size;
0400             to_read -= size;
0401             idx++;
0402         } while (to_read > 0);
0403     }
0404     /*
0405      * Perform the multi sector transfer deprotection on the buffer if the
0406      * restart page is protected.
0407      */
0408     if ((!ntfs_is_chkd_record(trp->magic) || le16_to_cpu(trp->usa_count))
0409             && post_read_mst_fixup((NTFS_RECORD*)trp,
0410             le32_to_cpu(rp->system_page_size))) {
0411         /*
0412          * A multi sector tranfer error was detected.  We only need to
0413          * abort if the restart page contents exceed the multi sector
0414          * transfer fixup of the first sector.
0415          */
0416         if (le16_to_cpu(rp->restart_area_offset) +
0417                 le16_to_cpu(ra->restart_area_length) >
0418                 NTFS_BLOCK_SIZE - sizeof(u16)) {
0419             ntfs_error(vi->i_sb, "Multi sector transfer error "
0420                     "detected in $LogFile restart page.");
0421             err = -EINVAL;
0422             goto err_out;
0423         }
0424     }
0425     /*
0426      * If the restart page is modified by chkdsk or there are no active
0427      * logfile clients, the logfile is consistent.  Otherwise, need to
0428      * check the log client records for consistency, too.
0429      */
0430     err = 0;
0431     if (ntfs_is_rstr_record(rp->magic) &&
0432             ra->client_in_use_list != LOGFILE_NO_CLIENT) {
0433         if (!ntfs_check_log_client_array(vi, trp)) {
0434             err = -EINVAL;
0435             goto err_out;
0436         }
0437     }
0438     if (lsn) {
0439         if (ntfs_is_rstr_record(rp->magic))
0440             *lsn = sle64_to_cpu(ra->current_lsn);
0441         else /* if (ntfs_is_chkd_record(rp->magic)) */
0442             *lsn = sle64_to_cpu(rp->chkdsk_lsn);
0443     }
0444     ntfs_debug("Done.");
0445     if (wrp)
0446         *wrp = trp;
0447     else {
0448 err_out:
0449         ntfs_free(trp);
0450     }
0451     return err;
0452 }
0453 
0454 /**
0455  * ntfs_check_logfile - check the journal for consistency
0456  * @log_vi: struct inode of loaded journal $LogFile to check
0457  * @rp:     [OUT] on success this is a copy of the current restart page
0458  *
0459  * Check the $LogFile journal for consistency and return 'true' if it is
0460  * consistent and 'false' if not.  On success, the current restart page is
0461  * returned in *@rp.  Caller must call ntfs_free(*@rp) when finished with it.
0462  *
0463  * At present we only check the two restart pages and ignore the log record
0464  * pages.
0465  *
0466  * Note that the MstProtected flag is not set on the $LogFile inode and hence
0467  * when reading pages they are not deprotected.  This is because we do not know
0468  * if the $LogFile was created on a system with a different page size to ours
0469  * yet and mst deprotection would fail if our page size is smaller.
0470  */
0471 bool ntfs_check_logfile(struct inode *log_vi, RESTART_PAGE_HEADER **rp)
0472 {
0473     s64 size, pos;
0474     LSN rstr1_lsn, rstr2_lsn;
0475     ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
0476     struct address_space *mapping = log_vi->i_mapping;
0477     struct page *page = NULL;
0478     u8 *kaddr = NULL;
0479     RESTART_PAGE_HEADER *rstr1_ph = NULL;
0480     RESTART_PAGE_HEADER *rstr2_ph = NULL;
0481     int log_page_size, err;
0482     bool logfile_is_empty = true;
0483     u8 log_page_bits;
0484 
0485     ntfs_debug("Entering.");
0486     /* An empty $LogFile must have been clean before it got emptied. */
0487     if (NVolLogFileEmpty(vol))
0488         goto is_empty;
0489     size = i_size_read(log_vi);
0490     /* Make sure the file doesn't exceed the maximum allowed size. */
0491     if (size > MaxLogFileSize)
0492         size = MaxLogFileSize;
0493     /*
0494      * Truncate size to a multiple of the page cache size or the default
0495      * log page size if the page cache size is between the default log page
0496      * log page size if the page cache size is between the default log page
0497      * size and twice that.
0498      */
0499     if (PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <=
0500             DefaultLogPageSize * 2)
0501         log_page_size = DefaultLogPageSize;
0502     else
0503         log_page_size = PAGE_SIZE;
0504     /*
0505      * Use ntfs_ffs() instead of ffs() to enable the compiler to
0506      * optimize log_page_size and log_page_bits into constants.
0507      */
0508     log_page_bits = ntfs_ffs(log_page_size) - 1;
0509     size &= ~(s64)(log_page_size - 1);
0510     /*
0511      * Ensure the log file is big enough to store at least the two restart
0512      * pages and the minimum number of log record pages.
0513      */
0514     if (size < log_page_size * 2 || (size - log_page_size * 2) >>
0515             log_page_bits < MinLogRecordPages) {
0516         ntfs_error(vol->sb, "$LogFile is too small.");
0517         return false;
0518     }
0519     /*
0520      * Read through the file looking for a restart page.  Since the restart
0521      * page header is at the beginning of a page we only need to search at
0522      * what could be the beginning of a page (for each page size) rather
0523      * than scanning the whole file byte by byte.  If all potential places
0524      * contain empty and uninitialzed records, the log file can be assumed
0525      * to be empty.
0526      */
0527     for (pos = 0; pos < size; pos <<= 1) {
0528         pgoff_t idx = pos >> PAGE_SHIFT;
0529         if (!page || page->index != idx) {
0530             if (page)
0531                 ntfs_unmap_page(page);
0532             page = ntfs_map_page(mapping, idx);
0533             if (IS_ERR(page)) {
0534                 ntfs_error(vol->sb, "Error mapping $LogFile "
0535                         "page (index %lu).", idx);
0536                 goto err_out;
0537             }
0538         }
0539         kaddr = (u8*)page_address(page) + (pos & ~PAGE_MASK);
0540         /*
0541          * A non-empty block means the logfile is not empty while an
0542          * empty block after a non-empty block has been encountered
0543          * means we are done.
0544          */
0545         if (!ntfs_is_empty_recordp((le32*)kaddr))
0546             logfile_is_empty = false;
0547         else if (!logfile_is_empty)
0548             break;
0549         /*
0550          * A log record page means there cannot be a restart page after
0551          * this so no need to continue searching.
0552          */
0553         if (ntfs_is_rcrd_recordp((le32*)kaddr))
0554             break;
0555         /* If not a (modified by chkdsk) restart page, continue. */
0556         if (!ntfs_is_rstr_recordp((le32*)kaddr) &&
0557                 !ntfs_is_chkd_recordp((le32*)kaddr)) {
0558             if (!pos)
0559                 pos = NTFS_BLOCK_SIZE >> 1;
0560             continue;
0561         }
0562         /*
0563          * Check the (modified by chkdsk) restart page for consistency
0564          * and get a copy of the complete multi sector transfer
0565          * deprotected restart page.
0566          */
0567         err = ntfs_check_and_load_restart_page(log_vi,
0568                 (RESTART_PAGE_HEADER*)kaddr, pos,
0569                 !rstr1_ph ? &rstr1_ph : &rstr2_ph,
0570                 !rstr1_ph ? &rstr1_lsn : &rstr2_lsn);
0571         if (!err) {
0572             /*
0573              * If we have now found the first (modified by chkdsk)
0574              * restart page, continue looking for the second one.
0575              */
0576             if (!pos) {
0577                 pos = NTFS_BLOCK_SIZE >> 1;
0578                 continue;
0579             }
0580             /*
0581              * We have now found the second (modified by chkdsk)
0582              * restart page, so we can stop looking.
0583              */
0584             break;
0585         }
0586         /*
0587          * Error output already done inside the function.  Note, we do
0588          * not abort if the restart page was invalid as we might still
0589          * find a valid one further in the file.
0590          */
0591         if (err != -EINVAL) {
0592             ntfs_unmap_page(page);
0593             goto err_out;
0594         }
0595         /* Continue looking. */
0596         if (!pos)
0597             pos = NTFS_BLOCK_SIZE >> 1;
0598     }
0599     if (page)
0600         ntfs_unmap_page(page);
0601     if (logfile_is_empty) {
0602         NVolSetLogFileEmpty(vol);
0603 is_empty:
0604         ntfs_debug("Done.  ($LogFile is empty.)");
0605         return true;
0606     }
0607     if (!rstr1_ph) {
0608         BUG_ON(rstr2_ph);
0609         ntfs_error(vol->sb, "Did not find any restart pages in "
0610                 "$LogFile and it was not empty.");
0611         return false;
0612     }
0613     /* If both restart pages were found, use the more recent one. */
0614     if (rstr2_ph) {
0615         /*
0616          * If the second restart area is more recent, switch to it.
0617          * Otherwise just throw it away.
0618          */
0619         if (rstr2_lsn > rstr1_lsn) {
0620             ntfs_debug("Using second restart page as it is more "
0621                     "recent.");
0622             ntfs_free(rstr1_ph);
0623             rstr1_ph = rstr2_ph;
0624             /* rstr1_lsn = rstr2_lsn; */
0625         } else {
0626             ntfs_debug("Using first restart page as it is more "
0627                     "recent.");
0628             ntfs_free(rstr2_ph);
0629         }
0630         rstr2_ph = NULL;
0631     }
0632     /* All consistency checks passed. */
0633     if (rp)
0634         *rp = rstr1_ph;
0635     else
0636         ntfs_free(rstr1_ph);
0637     ntfs_debug("Done.");
0638     return true;
0639 err_out:
0640     if (rstr1_ph)
0641         ntfs_free(rstr1_ph);
0642     return false;
0643 }
0644 
0645 /**
0646  * ntfs_is_logfile_clean - check in the journal if the volume is clean
0647  * @log_vi: struct inode of loaded journal $LogFile to check
0648  * @rp:     copy of the current restart page
0649  *
0650  * Analyze the $LogFile journal and return 'true' if it indicates the volume was
0651  * shutdown cleanly and 'false' if not.
0652  *
0653  * At present we only look at the two restart pages and ignore the log record
0654  * pages.  This is a little bit crude in that there will be a very small number
0655  * of cases where we think that a volume is dirty when in fact it is clean.
0656  * This should only affect volumes that have not been shutdown cleanly but did
0657  * not have any pending, non-check-pointed i/o, i.e. they were completely idle
0658  * at least for the five seconds preceding the unclean shutdown.
0659  *
0660  * This function assumes that the $LogFile journal has already been consistency
0661  * checked by a call to ntfs_check_logfile() and in particular if the $LogFile
0662  * is empty this function requires that NVolLogFileEmpty() is true otherwise an
0663  * empty volume will be reported as dirty.
0664  */
0665 bool ntfs_is_logfile_clean(struct inode *log_vi, const RESTART_PAGE_HEADER *rp)
0666 {
0667     ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
0668     RESTART_AREA *ra;
0669 
0670     ntfs_debug("Entering.");
0671     /* An empty $LogFile must have been clean before it got emptied. */
0672     if (NVolLogFileEmpty(vol)) {
0673         ntfs_debug("Done.  ($LogFile is empty.)");
0674         return true;
0675     }
0676     BUG_ON(!rp);
0677     if (!ntfs_is_rstr_record(rp->magic) &&
0678             !ntfs_is_chkd_record(rp->magic)) {
0679         ntfs_error(vol->sb, "Restart page buffer is invalid.  This is "
0680                 "probably a bug in that the $LogFile should "
0681                 "have been consistency checked before calling "
0682                 "this function.");
0683         return false;
0684     }
0685     ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
0686     /*
0687      * If the $LogFile has active clients, i.e. it is open, and we do not
0688      * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags,
0689      * we assume there was an unclean shutdown.
0690      */
0691     if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
0692             !(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
0693         ntfs_debug("Done.  $LogFile indicates a dirty shutdown.");
0694         return false;
0695     }
0696     /* $LogFile indicates a clean shutdown. */
0697     ntfs_debug("Done.  $LogFile indicates a clean shutdown.");
0698     return true;
0699 }
0700 
0701 /**
0702  * ntfs_empty_logfile - empty the contents of the $LogFile journal
0703  * @log_vi: struct inode of loaded journal $LogFile to empty
0704  *
0705  * Empty the contents of the $LogFile journal @log_vi and return 'true' on
0706  * success and 'false' on error.
0707  *
0708  * This function assumes that the $LogFile journal has already been consistency
0709  * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean()
0710  * has been used to ensure that the $LogFile is clean.
0711  */
0712 bool ntfs_empty_logfile(struct inode *log_vi)
0713 {
0714     VCN vcn, end_vcn;
0715     ntfs_inode *log_ni = NTFS_I(log_vi);
0716     ntfs_volume *vol = log_ni->vol;
0717     struct super_block *sb = vol->sb;
0718     runlist_element *rl;
0719     unsigned long flags;
0720     unsigned block_size, block_size_bits;
0721     int err;
0722     bool should_wait = true;
0723 
0724     ntfs_debug("Entering.");
0725     if (NVolLogFileEmpty(vol)) {
0726         ntfs_debug("Done.");
0727         return true;
0728     }
0729     /*
0730      * We cannot use ntfs_attr_set() because we may be still in the middle
0731      * of a mount operation.  Thus we do the emptying by hand by first
0732      * zapping the page cache pages for the $LogFile/$DATA attribute and
0733      * then emptying each of the buffers in each of the clusters specified
0734      * by the runlist by hand.
0735      */
0736     block_size = sb->s_blocksize;
0737     block_size_bits = sb->s_blocksize_bits;
0738     vcn = 0;
0739     read_lock_irqsave(&log_ni->size_lock, flags);
0740     end_vcn = (log_ni->initialized_size + vol->cluster_size_mask) >>
0741             vol->cluster_size_bits;
0742     read_unlock_irqrestore(&log_ni->size_lock, flags);
0743     truncate_inode_pages(log_vi->i_mapping, 0);
0744     down_write(&log_ni->runlist.lock);
0745     rl = log_ni->runlist.rl;
0746     if (unlikely(!rl || vcn < rl->vcn || !rl->length)) {
0747 map_vcn:
0748         err = ntfs_map_runlist_nolock(log_ni, vcn, NULL);
0749         if (err) {
0750             ntfs_error(sb, "Failed to map runlist fragment (error "
0751                     "%d).", -err);
0752             goto err;
0753         }
0754         rl = log_ni->runlist.rl;
0755         BUG_ON(!rl || vcn < rl->vcn || !rl->length);
0756     }
0757     /* Seek to the runlist element containing @vcn. */
0758     while (rl->length && vcn >= rl[1].vcn)
0759         rl++;
0760     do {
0761         LCN lcn;
0762         sector_t block, end_block;
0763         s64 len;
0764 
0765         /*
0766          * If this run is not mapped map it now and start again as the
0767          * runlist will have been updated.
0768          */
0769         lcn = rl->lcn;
0770         if (unlikely(lcn == LCN_RL_NOT_MAPPED)) {
0771             vcn = rl->vcn;
0772             goto map_vcn;
0773         }
0774         /* If this run is not valid abort with an error. */
0775         if (unlikely(!rl->length || lcn < LCN_HOLE))
0776             goto rl_err;
0777         /* Skip holes. */
0778         if (lcn == LCN_HOLE)
0779             continue;
0780         block = lcn << vol->cluster_size_bits >> block_size_bits;
0781         len = rl->length;
0782         if (rl[1].vcn > end_vcn)
0783             len = end_vcn - rl->vcn;
0784         end_block = (lcn + len) << vol->cluster_size_bits >>
0785                 block_size_bits;
0786         /* Iterate over the blocks in the run and empty them. */
0787         do {
0788             struct buffer_head *bh;
0789 
0790             /* Obtain the buffer, possibly not uptodate. */
0791             bh = sb_getblk(sb, block);
0792             BUG_ON(!bh);
0793             /* Setup buffer i/o submission. */
0794             lock_buffer(bh);
0795             bh->b_end_io = end_buffer_write_sync;
0796             get_bh(bh);
0797             /* Set the entire contents of the buffer to 0xff. */
0798             memset(bh->b_data, -1, block_size);
0799             if (!buffer_uptodate(bh))
0800                 set_buffer_uptodate(bh);
0801             if (buffer_dirty(bh))
0802                 clear_buffer_dirty(bh);
0803             /*
0804              * Submit the buffer and wait for i/o to complete but
0805              * only for the first buffer so we do not miss really
0806              * serious i/o errors.  Once the first buffer has
0807              * completed ignore errors afterwards as we can assume
0808              * that if one buffer worked all of them will work.
0809              */
0810             submit_bh(REQ_OP_WRITE, bh);
0811             if (should_wait) {
0812                 should_wait = false;
0813                 wait_on_buffer(bh);
0814                 if (unlikely(!buffer_uptodate(bh)))
0815                     goto io_err;
0816             }
0817             brelse(bh);
0818         } while (++block < end_block);
0819     } while ((++rl)->vcn < end_vcn);
0820     up_write(&log_ni->runlist.lock);
0821     /*
0822      * Zap the pages again just in case any got instantiated whilst we were
0823      * emptying the blocks by hand.  FIXME: We may not have completed
0824      * writing to all the buffer heads yet so this may happen too early.
0825      * We really should use a kernel thread to do the emptying
0826      * asynchronously and then we can also set the volume dirty and output
0827      * an error message if emptying should fail.
0828      */
0829     truncate_inode_pages(log_vi->i_mapping, 0);
0830     /* Set the flag so we do not have to do it again on remount. */
0831     NVolSetLogFileEmpty(vol);
0832     ntfs_debug("Done.");
0833     return true;
0834 io_err:
0835     ntfs_error(sb, "Failed to write buffer.  Unmount and run chkdsk.");
0836     goto dirty_err;
0837 rl_err:
0838     ntfs_error(sb, "Runlist is corrupt.  Unmount and run chkdsk.");
0839 dirty_err:
0840     NVolSetErrors(vol);
0841     err = -EIO;
0842 err:
0843     up_write(&log_ni->runlist.lock);
0844     ntfs_error(sb, "Failed to fill $LogFile with 0xff bytes (error %d).",
0845             -err);
0846     return false;
0847 }
0848 
0849 #endif /* NTFS_RW */