ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / ntfs / logfile.c
1 /*
2  * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2002-2004 Anton Altaparmakov.
5  *
6  * This program/include file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program/include file is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the Linux-NTFS
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #ifdef NTFS_RW
23
24 #include <linux/types.h>
25 #include <linux/fs.h>
26 #include <linux/highmem.h>
27 #include <linux/buffer_head.h>
28
29 #include "logfile.h"
30 #include "volume.h"
31 #include "ntfs.h"
32 #include "debug.h"
33
34 /**
35  * ntfs_check_restart_page_header - check the page header for consistency
36  * @vi:         $LogFile inode to which the restart page header belongs
37  * @rp:         restart page header to check
38  * @pos:        position in @vi at which the restart page header resides
39  *
40  * Check the restart page header @rp for consistency and return TRUE if it is
41  * consistent and FALSE otherwise.
42  *
43  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
44  * require the full restart page.
45  */
46 static BOOL ntfs_check_restart_page_header(struct inode *vi,
47                 RESTART_PAGE_HEADER *rp, s64 pos)
48 {
49         u32 logfile_system_page_size, logfile_log_page_size;
50         u16 usa_count, usa_ofs, usa_end, ra_ofs;
51
52         ntfs_debug("Entering.");
53         /*
54          * If the system or log page sizes are smaller than the ntfs block size
55          * or either is not a power of 2 we cannot handle this log file.
56          */
57         logfile_system_page_size = le32_to_cpu(rp->system_page_size);
58         logfile_log_page_size = le32_to_cpu(rp->log_page_size);
59         if (logfile_system_page_size < NTFS_BLOCK_SIZE ||
60                         logfile_log_page_size < NTFS_BLOCK_SIZE ||
61                         logfile_system_page_size &
62                         (logfile_system_page_size - 1) ||
63                         logfile_log_page_size & (logfile_log_page_size - 1)) {
64                 ntfs_error(vi->i_sb, "$LogFile uses unsupported page size.");
65                 return FALSE;
66         }
67         /*
68          * We must be either at !pos (1st restart page) or at pos = system page
69          * size (2nd restart page).
70          */
71         if (pos && pos != logfile_system_page_size) {
72                 ntfs_error(vi->i_sb, "Found restart area in incorrect "
73                                 "position in $LogFile.");
74                 return FALSE;
75         }
76         /* We only know how to handle version 1.1. */
77         if (sle16_to_cpu(rp->major_ver) != 1 ||
78                         sle16_to_cpu(rp->minor_ver) != 1) {
79                 ntfs_error(vi->i_sb, "$LogFile version %i.%i is not "
80                                 "supported.  (This driver supports version "
81                                 "1.1 only.)", (int)sle16_to_cpu(rp->major_ver),
82                                 (int)sle16_to_cpu(rp->minor_ver));
83                 return FALSE;
84         }
85         /* Verify the size of the update sequence array. */
86         usa_count = 1 + (logfile_system_page_size >> NTFS_BLOCK_SIZE_BITS);
87         if (usa_count != le16_to_cpu(rp->usa_count)) {
88                 ntfs_error(vi->i_sb, "$LogFile restart page specifies "
89                                 "inconsistent update sequence array count.");
90                 return FALSE;
91         }
92         /* Verify the position of the update sequence array. */
93         usa_ofs = le16_to_cpu(rp->usa_ofs);
94         usa_end = usa_ofs + usa_count * sizeof(u16);
95         if (usa_ofs < sizeof(RESTART_PAGE_HEADER) ||
96                         usa_end > NTFS_BLOCK_SIZE - sizeof(u16)) {
97                 ntfs_error(vi->i_sb, "$LogFile restart page specifies "
98                                 "inconsistent update sequence array offset.");
99                 return FALSE;
100         }
101         /*
102          * Verify the position of the restart area.  It must be:
103          *      - aligned to 8-byte boundary,
104          *      - after the update sequence array, and
105          *      - within the system page size.
106          */
107         ra_ofs = le16_to_cpu(rp->restart_area_offset);
108         if (ra_ofs & 7 || ra_ofs < usa_end ||
109                         ra_ofs > logfile_system_page_size) {
110                 ntfs_error(vi->i_sb, "$LogFile restart page specifies "
111                                 "inconsistent restart area offset.");
112                 return FALSE;
113         }
114         /*
115          * Only restart pages modified by chkdsk are allowed to have chkdsk_lsn
116          * set.
117          */
118         if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) {
119                 ntfs_error(vi->i_sb, "$LogFile restart page is not modified "
120                                 "chkdsk but a chkdsk LSN is specified.");
121                 return FALSE;
122         }
123         ntfs_debug("Done.");
124         return TRUE;
125 }
126
127 /**
128  * ntfs_check_restart_area - check the restart area for consistency
129  * @vi:         $LogFile inode to which the restart page belongs
130  * @rp:         restart page whose restart area to check
131  *
132  * Check the restart area of the restart page @rp for consistency and return
133  * TRUE if it is consistent and FALSE otherwise.
134  *
135  * This function assumes that the restart page header has already been
136  * consistency checked.
137  *
138  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
139  * require the full restart page.
140  */
141 static BOOL ntfs_check_restart_area(struct inode *vi, RESTART_PAGE_HEADER *rp)
142 {
143         u64 file_size;
144         RESTART_AREA *ra;
145         u16 ra_ofs, ra_len, ca_ofs;
146         u8 fs_bits;
147
148         ntfs_debug("Entering.");
149         ra_ofs = le16_to_cpu(rp->restart_area_offset);
150         ra = (RESTART_AREA*)((u8*)rp + ra_ofs);
151         /*
152          * Everything before ra->file_size must be before the first word
153          * protected by an update sequence number.  This ensures that it is
154          * safe to access ra->client_array_offset.
155          */
156         if (ra_ofs + offsetof(RESTART_AREA, file_size) >
157                         NTFS_BLOCK_SIZE - sizeof(u16)) {
158                 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
159                                 "inconsistent file offset.");
160                 return FALSE;
161         }
162         /*
163          * Now that we can access ra->client_array_offset, make sure everything
164          * up to the log client array is before the first word protected by an
165          * update sequence number.  This ensures we can access all of the
166          * restart area elements safely.  Also, the client array offset must be
167          * aligned to an 8-byte boundary.
168          */
169         ca_ofs = le16_to_cpu(ra->client_array_offset);
170         if (((ca_ofs + 7) & ~7) != ca_ofs ||
171                         ra_ofs + ca_ofs > NTFS_BLOCK_SIZE - sizeof(u16)) {
172                 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
173                                 "inconsistent client array offset.");
174                 return FALSE;
175         }
176         /*
177          * The restart area must end within the system page size both when
178          * calculated manually and as specified by ra->restart_area_length.
179          * Also, the calculated length must not exceed the specified length.
180          */
181         ra_len = ca_ofs + le16_to_cpu(ra->log_clients) *
182                         sizeof(LOG_CLIENT_RECORD);
183         if (ra_ofs + ra_len > le32_to_cpu(rp->system_page_size) ||
184                         ra_ofs + le16_to_cpu(ra->restart_area_length) >
185                         le32_to_cpu(rp->system_page_size) ||
186                         ra_len > le16_to_cpu(ra->restart_area_length)) {
187                 ntfs_error(vi->i_sb, "$LogFile restart area is out of bounds "
188                                 "of the system page size specified by the "
189                                 "restart page header and/or the specified "
190                                 "restart area length is inconsistent.");
191                 return FALSE;
192         }
193         /*
194          * The ra->client_free_list and ra->client_in_use_list must be either
195          * LOGFILE_NO_CLIENT or less than ra->log_clients or they are
196          * overflowing the client array.
197          */
198         if ((ra->client_free_list != LOGFILE_NO_CLIENT &&
199                         le16_to_cpu(ra->client_free_list) >=
200                         le16_to_cpu(ra->log_clients)) ||
201                         (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
202                         le16_to_cpu(ra->client_in_use_list) >=
203                         le16_to_cpu(ra->log_clients))) {
204                 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
205                                 "overflowing client free and/or in use lists.");
206                 return FALSE;
207         }
208         /*
209          * Check ra->seq_number_bits against ra->file_size for consistency.
210          * We cannot just use ffs() because the file size is not a power of 2.
211          */
212         file_size = (u64)sle64_to_cpu(ra->file_size);
213         fs_bits = 0;
214         while (file_size) {
215                 file_size >>= 1;
216                 fs_bits++;
217         }
218         if (le32_to_cpu(ra->seq_number_bits) != 67 - fs_bits) {
219                 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
220                                 "inconsistent sequence number bits.");
221                 return FALSE;
222         }
223         /* The log record header length must be a multiple of 8. */
224         if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) !=
225                         le16_to_cpu(ra->log_record_header_length)) {
226                 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
227                                 "inconsistent log record header length.");
228                 return FALSE;
229         }
230         /* Dito for the log page data offset. */
231         if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) !=
232                         le16_to_cpu(ra->log_page_data_offset)) {
233                 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
234                                 "inconsistent log page data offset.");
235                 return FALSE;
236         }
237         ntfs_debug("Done.");
238         return TRUE;
239 }
240
241 /**
242  * ntfs_check_log_client_array - check the log client array for consistency
243  * @vi:         $LogFile inode to which the restart page belongs
244  * @rp:         restart page whose log client array to check
245  *
246  * Check the log client array of the restart page @rp for consistency and
247  * return TRUE if it is consistent and FALSE otherwise.
248  *
249  * This function assumes that the restart page header and the restart area have
250  * already been consistency checked.
251  *
252  * Unlike ntfs_check_restart_page_header() and ntfs_check_restart_area(), this
253  * function needs @rp->system_page_size bytes in @rp, i.e. it requires the full
254  * restart page and the page must be multi sector transfer deprotected.
255  */
256 static BOOL ntfs_check_log_client_array(struct inode *vi,
257                 RESTART_PAGE_HEADER *rp)
258 {
259         RESTART_AREA *ra;
260         LOG_CLIENT_RECORD *ca, *cr;
261         u16 nr_clients, idx;
262         BOOL in_free_list, idx_is_first;
263
264         ntfs_debug("Entering.");
265         ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
266         ca = (LOG_CLIENT_RECORD*)((u8*)ra +
267                         le16_to_cpu(ra->client_array_offset));
268         /*
269          * Check the ra->client_free_list first and then check the
270          * ra->client_in_use_list.  Check each of the log client records in
271          * each of the lists and check that the array does not overflow the
272          * ra->log_clients value.  Also keep track of the number of records
273          * visited as there cannot be more than ra->log_clients records and
274          * that way we detect eventual loops in within a list.
275          */
276         nr_clients = le16_to_cpu(ra->log_clients);
277         idx = le16_to_cpu(ra->client_free_list);
278         in_free_list = TRUE;
279 check_list:
280         for (idx_is_first = TRUE; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--,
281                         idx = le16_to_cpu(cr->next_client)) {
282                 if (!nr_clients || idx >= le16_to_cpu(ra->log_clients))
283                         goto err_out;
284                 /* Set @cr to the current log client record. */
285                 cr = ca + idx;
286                 /* The first log client record must not have a prev_client. */
287                 if (idx_is_first) {
288                         if (cr->prev_client != LOGFILE_NO_CLIENT)
289                                 goto err_out;
290                         idx_is_first = FALSE;
291                 }
292         }
293         /* Switch to and check the in use list if we just did the free list. */
294         if (in_free_list) {
295                 in_free_list = FALSE;
296                 idx = le16_to_cpu(ra->client_in_use_list);
297                 goto check_list;
298         }
299         ntfs_debug("Done.");
300         return TRUE;
301 err_out:
302         ntfs_error(vi->i_sb, "$LogFile log client array is corrupt.");
303         return FALSE;
304 }
305
306 /**
307  * ntfs_check_and_load_restart_page - check the restart page for consistency
308  * @vi:         $LogFile inode to which the restart page belongs
309  * @rp:         restart page to check
310  * @pos:        position in @vi at which the restart page resides
311  * @wrp:        copy of the multi sector transfer deprotected restart page
312  *
313  * Check the restart page @rp for consistency and return TRUE if it is
314  * consistent and FALSE otherwise.
315  *
316  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
317  * require the full restart page.
318  *
319  * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a
320  * copy of the complete multi sector transfer deprotected page.  On failure,
321  * *@wrp is undefined.
322  */
323 static BOOL ntfs_check_and_load_restart_page(struct inode *vi,
324                 RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp)
325 {
326         RESTART_AREA *ra;
327         RESTART_PAGE_HEADER *trp;
328         int size;
329         BOOL ret;
330
331         ntfs_debug("Entering.");
332         /* Check the restart page header for consistency. */
333         if (!ntfs_check_restart_page_header(vi, rp, pos)) {
334                 /* Error output already done inside the function. */
335                 return FALSE;
336         }
337         /* Check the restart area for consistency. */
338         if (!ntfs_check_restart_area(vi, rp)) {
339                 /* Error output already done inside the function. */
340                 return FALSE;
341         }
342         ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
343         /*
344          * Allocate a buffer to store the whole restart page so we can multi
345          * sector transfer deprotect it.
346          */
347         trp = ntfs_malloc_nofs(le32_to_cpu(rp->system_page_size));
348         if (!trp) {
349                 ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile "
350                                 "restart page buffer.");
351                 return FALSE;
352         }
353         /*
354          * Read the whole of the restart page into the buffer.  If it fits
355          * completely inside @rp, just copy it from there.  Otherwise map all
356          * the required pages and copy the data from them.
357          */
358         size = PAGE_CACHE_SIZE - (pos & ~PAGE_CACHE_MASK);
359         if (size >= le32_to_cpu(rp->system_page_size)) {
360                 memcpy(trp, rp, le32_to_cpu(rp->system_page_size));
361         } else {
362                 pgoff_t idx;
363                 struct page *page;
364                 int have_read, to_read;
365
366                 /* First copy what we already have in @rp. */
367                 memcpy(trp, rp, size);
368                 /* Copy the remaining data one page at a time. */
369                 have_read = size;
370                 to_read = le32_to_cpu(rp->system_page_size) - size;
371                 idx = (pos + size) >> PAGE_CACHE_SHIFT;
372                 BUG_ON((pos + size) & ~PAGE_CACHE_MASK);
373                 do {
374                         page = ntfs_map_page(vi->i_mapping, idx);
375                         if (IS_ERR(page)) {
376                                 ntfs_error(vi->i_sb, "Error mapping $LogFile "
377                                                 "page (index %lu).", idx);
378                                 goto err_out;
379                         }
380                         size = min_t(int, to_read, PAGE_CACHE_SIZE);
381                         memcpy((u8*)trp + have_read, page_address(page), size);
382                         ntfs_unmap_page(page);
383                         have_read += size;
384                         to_read -= size;
385                         idx++;
386                 } while (to_read > 0);
387         }
388         /* Perform the multi sector transfer deprotection on the buffer. */
389         if (post_read_mst_fixup((NTFS_RECORD*)trp,
390                         le32_to_cpu(rp->system_page_size))) {
391                 ntfs_error(vi->i_sb, "Multi sector transfer error detected in "
392                                 "$LogFile restart page.");
393                 goto err_out;
394         }
395         /* Check the log client records for consistency. */
396         ret = ntfs_check_log_client_array(vi, trp);
397         if (ret && wrp)
398                 *wrp = trp;
399         else
400                 ntfs_free(trp);
401         ntfs_debug("Done.");
402         return ret;
403 err_out:
404         ntfs_free(trp);
405         return FALSE;
406 }
407
408 /**
409  * ntfs_ckeck_logfile - check in the journal if the volume is consistent
410  * @log_vi:     struct inode of loaded journal $LogFile to check
411  *
412  * Check the $LogFile journal for consistency and return TRUE if it is
413  * consistent and FALSE if not.
414  *
415  * At present we only check the two restart pages and ignore the log record
416  * pages.
417  *
418  * Note that the MstProtected flag is not set on the $LogFile inode and hence
419  * when reading pages they are not deprotected.  This is because we do not know
420  * if the $LogFile was created on a system with a different page size to ours
421  * yet and mst deprotection would fail if our page size is smaller.
422  */
423 BOOL ntfs_check_logfile(struct inode *log_vi)
424 {
425         s64 size, pos, rstr1_pos, rstr2_pos;
426         ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
427         struct address_space *mapping = log_vi->i_mapping;
428         struct page *page = NULL;
429         u8 *kaddr = NULL;
430         RESTART_PAGE_HEADER *rstr1_ph = NULL;
431         RESTART_PAGE_HEADER *rstr2_ph = NULL;
432         int log_page_size, log_page_mask, ofs;
433         BOOL logfile_is_empty = TRUE;
434         BOOL rstr1_found = FALSE;
435         BOOL rstr2_found = FALSE;
436         u8 log_page_bits;
437
438         ntfs_debug("Entering.");
439         /* An empty $LogFile must have been clean before it got emptied. */
440         if (NVolLogFileEmpty(vol))
441                 goto is_empty;
442         size = log_vi->i_size;
443         /* Make sure the file doesn't exceed the maximum allowed size. */
444         if (size > MaxLogFileSize)
445                 size = MaxLogFileSize;
446         /*
447          * Truncate size to a multiple of the page cache size or the default
448          * log page size if the page cache size is between the default log page
449          * log page size if the page cache size is between the default log page
450          * size and twice that.
451          */
452         if (PAGE_CACHE_SIZE >= DefaultLogPageSize && PAGE_CACHE_SIZE <=
453                         DefaultLogPageSize * 2)
454                 log_page_size = DefaultLogPageSize;
455         else
456                 log_page_size = PAGE_CACHE_SIZE;
457         log_page_mask = log_page_size - 1;
458         log_page_bits = ffs(log_page_size) - 1;
459         size &= ~(log_page_size - 1);
460         /*
461          * Ensure the log file is big enough to store at least the two restart
462          * pages and the minimum number of log record pages.
463          */
464         if (size < log_page_size * 2 || (size - log_page_size * 2) >>
465                         log_page_bits < MinLogRecordPages) {
466                 ntfs_error(vol->sb, "$LogFile is too small.");
467                 return FALSE;
468         }
469         /*
470          * Read through the file looking for a restart page.  Since the restart
471          * page header is at the beginning of a page we only need to search at
472          * what could be the beginning of a page (for each page size) rather
473          * than scanning the whole file byte by byte.  If all potential places
474          * contain empty and uninitialzed records, the log file can be assumed
475          * to be empty.
476          */
477         for (pos = 0; pos < size; pos <<= 1) {
478                 pgoff_t idx = pos >> PAGE_CACHE_SHIFT;
479                 if (!page || page->index != idx) {
480                         if (page)
481                                 ntfs_unmap_page(page);
482                         page = ntfs_map_page(mapping, idx);
483                         if (IS_ERR(page)) {
484                                 ntfs_error(vol->sb, "Error mapping $LogFile "
485                                                 "page (index %lu).", idx);
486                                 return FALSE;
487                         }
488                 }
489                 kaddr = (u8*)page_address(page) + (pos & ~PAGE_CACHE_MASK);
490                 /*
491                  * A non-empty block means the logfile is not empty while an
492                  * empty block after a non-empty block has been encountered
493                  * means we are done.
494                  */
495                 if (!ntfs_is_empty_recordp(kaddr))
496                         logfile_is_empty = FALSE;
497                 else if (!logfile_is_empty)
498                         break;
499                 /*
500                  * A log record page means there cannot be a restart page after
501                  * this so no need to continue searching.
502                  */
503                 if (ntfs_is_rcrd_recordp(kaddr))
504                         break;
505                 /*
506                  * A modified by chkdsk restart page means we cannot handle
507                  * this log file.
508                  */
509                 if (ntfs_is_chkd_recordp(kaddr)) {
510                         ntfs_error(vol->sb, "$LogFile has been modified by "
511                                         "chkdsk.  Mount this volume in "
512                                         "Windows.");
513                         goto err_out;
514                 }
515                 /* If not a restart page, continue. */
516                 if (!ntfs_is_rstr_recordp(kaddr)) {
517                         /* Skip to the minimum page size for the next one. */
518                         if (!pos)
519                                 pos = NTFS_BLOCK_SIZE >> 1;
520                         continue;
521                 }
522                 /* We now know we have a restart page. */
523                 if (!pos) {
524                         rstr1_found = TRUE;
525                         rstr1_pos = pos;
526                 } else {
527                         if (rstr2_found) {
528                                 ntfs_error(vol->sb, "Found more than two "
529                                                 "restart pages in $LogFile.");
530                                 goto err_out;
531                         }
532                         rstr2_found = TRUE;
533                         rstr2_pos = pos;
534                 }
535                 /*
536                  * Check the restart page for consistency and get a copy of the
537                  * complete multi sector transfer deprotected restart page.
538                  */
539                 if (!ntfs_check_and_load_restart_page(log_vi,
540                                 (RESTART_PAGE_HEADER*)kaddr, pos,
541                                 !pos ? &rstr1_ph : &rstr2_ph)) {
542                         /* Error output already done inside the function. */
543                         goto err_out;
544                 }
545                 /*
546                  * We have a valid restart page.  The next one must be after
547                  * a whole system page size as specified by the valid restart
548                  * page.
549                  */
550                 if (!pos)
551                         pos = le32_to_cpu(rstr1_ph->system_page_size) >> 1;
552         }
553         if (page) {
554                 ntfs_unmap_page(page);
555                 page = NULL;
556         }
557         if (logfile_is_empty) {
558                 NVolSetLogFileEmpty(vol);
559 is_empty:
560                 ntfs_debug("Done.  ($LogFile is empty.)");
561                 return TRUE;
562         }
563         if (!rstr1_found || !rstr2_found) {
564                 ntfs_error(vol->sb, "Did not find two restart pages in "
565                                 "$LogFile.");
566                 goto err_out;
567         }
568         /*
569          * The two restart areas must be identical except for the update
570          * sequence number.
571          */
572         ofs = le16_to_cpu(rstr1_ph->usa_ofs);
573         if (memcmp(rstr1_ph, rstr2_ph, ofs) || (ofs += sizeof(u16),
574                         memcmp((u8*)rstr1_ph + ofs, (u8*)rstr2_ph + ofs,
575                         le32_to_cpu(rstr1_ph->system_page_size) - ofs))) {
576                 ntfs_error(vol->sb, "The two restart pages in $LogFile do not "
577                                 "match.");
578                 goto err_out;
579         }
580         ntfs_free(rstr1_ph);
581         ntfs_free(rstr2_ph);
582         /* All consistency checks passed. */
583         ntfs_debug("Done.");
584         return TRUE;
585 err_out:
586         if (page)
587                 ntfs_unmap_page(page);
588         if (rstr1_ph)
589                 ntfs_free(rstr1_ph);
590         if (rstr2_ph)
591                 ntfs_free(rstr2_ph);
592         return FALSE;
593 }
594
595 /**
596  * ntfs_is_logfile_clean - check in the journal if the volume is clean
597  * @log_vi:     struct inode of loaded journal $LogFile to check
598  *
599  * Analyze the $LogFile journal and return TRUE if it indicates the volume was
600  * shutdown cleanly and FALSE if not.
601  *
602  * At present we only look at the two restart pages and ignore the log record
603  * pages.  This is a little bit crude in that there will be a very small number
604  * of cases where we think that a volume is dirty when in fact it is clean.
605  * This should only affect volumes that have not been shutdown cleanly but did
606  * not have any pending, non-check-pointed i/o, i.e. they were completely idle
607  * at least for the five seconds preceeding the unclean shutdown.
608  *
609  * This function assumes that the $LogFile journal has already been consistency
610  * checked by a call to ntfs_check_logfile() and in particular if the $LogFile
611  * is empty this function requires that NVolLogFileEmpty() is true otherwise an
612  * empty volume will be reported as dirty.
613  */
614 BOOL ntfs_is_logfile_clean(struct inode *log_vi)
615 {
616         ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
617         struct page *page;
618         RESTART_PAGE_HEADER *rp;
619         RESTART_AREA *ra;
620
621         ntfs_debug("Entering.");
622         /* An empty $LogFile must have been clean before it got emptied. */
623         if (NVolLogFileEmpty(vol)) {
624                 ntfs_debug("Done.  ($LogFile is empty.)");
625                 return TRUE;
626         }
627         /*
628          * Read the first restart page.  It will be possibly incomplete and
629          * will not be multi sector transfer deprotected but we only need the
630          * first NTFS_BLOCK_SIZE bytes so it does not matter.
631          */
632         page = ntfs_map_page(log_vi->i_mapping, 0);
633         if (IS_ERR(page)) {
634                 ntfs_error(vol->sb, "Error mapping $LogFile page (index 0).");
635                 return FALSE;
636         }
637         rp = (RESTART_PAGE_HEADER*)page_address(page);
638         if (!ntfs_is_rstr_record(rp->magic)) {
639                 ntfs_error(vol->sb, "No restart page found at offset zero in "
640                                 "$LogFile.  This is probably a bug in that "
641                                 "the $LogFile should have been consistency "
642                                 "checked before calling this function.");
643                 goto err_out;
644         }
645         ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
646         /*
647          * If the $LogFile has active clients, i.e. it is open, and we do not
648          * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags,
649          * we assume there was an unclean shutdown.
650          */
651         if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
652                         !(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
653                 ntfs_debug("Done.  $LogFile indicates a dirty shutdown.");
654                 goto err_out;
655         }
656         ntfs_unmap_page(page);
657         /* $LogFile indicates a clean shutdown. */
658         ntfs_debug("Done.  $LogFile indicates a clean shutdown.");
659         return TRUE;
660 err_out:
661         ntfs_unmap_page(page);
662         return FALSE;
663 }
664
665 /**
666  * ntfs_empty_logfile - empty the contents of the $LogFile journal
667  * @log_vi:     struct inode of loaded journal $LogFile to empty
668  *
669  * Empty the contents of the $LogFile journal @log_vi and return TRUE on
670  * success FALSE on error.
671  *
672  * This function assumes that the $LogFile journal has already been consistency
673  * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean()
674  * has been used to ensure that the $LogFile is clean.
675  */
676 BOOL ntfs_empty_logfile(struct inode *log_vi)
677 {
678         ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
679         struct address_space *mapping;
680         pgoff_t idx, end;
681
682         ntfs_debug("Entering.");
683         if (NVolLogFileEmpty(vol))
684                 goto done;
685         mapping = log_vi->i_mapping;
686         end = (log_vi->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
687         for (idx = 0; idx < end; ++idx) {
688                 struct page *page;
689                 u8 *kaddr;
690
691                 /* Find or create the current page.  (The page is locked.) */
692                 page = grab_cache_page(mapping, idx);
693                 if (unlikely(!page)) {
694                         ntfs_error(vol->sb, "Insufficient memory to grab "
695                                         "$LogFile page (index %lu).", idx);
696                         return FALSE;
697                 }
698                 /*
699                  * Set all bytes in the page to 0xff.  It doesn't matter if we
700                  * go beyond i_size, because ntfs_writepage() will take care of
701                  * that for us.
702                  */
703                 kaddr = (u8*)kmap_atomic(page, KM_USER0);
704                 memset(kaddr, 0xff, PAGE_CACHE_SIZE);
705                 flush_dcache_page(page);
706                 kunmap_atomic(kaddr, KM_USER0);
707                 /*
708                  * If the page has buffers, mark them uptodate since buffer
709                  * state and not page state is definitive in 2.6 kernels.
710                  */
711                 if (page_has_buffers(page)) {
712                         struct buffer_head *bh, *head;
713
714                         bh = head = page_buffers(page);
715                         do {
716                                 set_buffer_uptodate(bh);
717                         } while ((bh = bh->b_this_page) != head);
718                 }
719                 /* Now that buffers are uptodate, set the page uptodate, too. */
720                 SetPageUptodate(page);
721                 /*
722                  * Set the page and all its buffers dirty and mark the inode
723                  * dirty, too. The VM will write the page later on.
724                  */
725                 set_page_dirty(page);
726                 /* Finally unlock and release the page. */
727                 unlock_page(page);
728                 page_cache_release(page);
729         }
730         /* We set the flag so we do not clear the log file again on remount. */
731         NVolSetLogFileEmpty(vol);
732 done:
733         ntfs_debug("Done.");
734         return TRUE;
735 }
736
737 #endif /* NTFS_RW */