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