1
2
3
4
5
6
7
8#ifdef NTFS_RW
9
10#include <linux/types.h>
11#include <linux/fs.h>
12#include <linux/highmem.h>
13#include <linux/buffer_head.h>
14#include <linux/bitops.h>
15#include <linux/log2.h>
16#include <linux/bio.h>
17
18#include "attrib.h"
19#include "aops.h"
20#include "debug.h"
21#include "logfile.h"
22#include "malloc.h"
23#include "volume.h"
24#include "ntfs.h"
25
26
27
28
29
30
31
32
33
34
35
36
37
38static bool ntfs_check_restart_page_header(struct inode *vi,
39 RESTART_PAGE_HEADER *rp, s64 pos)
40{
41 u32 logfile_system_page_size, logfile_log_page_size;
42 u16 ra_ofs, usa_count, usa_ofs, usa_end = 0;
43 bool have_usa = true;
44
45 ntfs_debug("Entering.");
46
47
48
49
50 logfile_system_page_size = le32_to_cpu(rp->system_page_size);
51 logfile_log_page_size = le32_to_cpu(rp->log_page_size);
52 if (logfile_system_page_size < NTFS_BLOCK_SIZE ||
53 logfile_log_page_size < NTFS_BLOCK_SIZE ||
54 logfile_system_page_size &
55 (logfile_system_page_size - 1) ||
56 !is_power_of_2(logfile_log_page_size)) {
57 ntfs_error(vi->i_sb, "$LogFile uses unsupported page size.");
58 return false;
59 }
60
61
62
63
64 if (pos && pos != logfile_system_page_size) {
65 ntfs_error(vi->i_sb, "Found restart area in incorrect "
66 "position in $LogFile.");
67 return false;
68 }
69
70 if (sle16_to_cpu(rp->major_ver) != 1 ||
71 sle16_to_cpu(rp->minor_ver) != 1) {
72 ntfs_error(vi->i_sb, "$LogFile version %i.%i is not "
73 "supported. (This driver supports version "
74 "1.1 only.)", (int)sle16_to_cpu(rp->major_ver),
75 (int)sle16_to_cpu(rp->minor_ver));
76 return false;
77 }
78
79
80
81
82 if (ntfs_is_chkd_record(rp->magic) && !le16_to_cpu(rp->usa_count)) {
83 have_usa = false;
84 goto skip_usa_checks;
85 }
86
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
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 }
102skip_usa_checks:
103
104
105
106
107
108
109 ra_ofs = le16_to_cpu(rp->restart_area_offset);
110 if (ra_ofs & 7 || (have_usa ? ra_ofs < usa_end :
111 ra_ofs < sizeof(RESTART_PAGE_HEADER)) ||
112 ra_ofs > logfile_system_page_size) {
113 ntfs_error(vi->i_sb, "$LogFile restart page specifies "
114 "inconsistent restart area offset.");
115 return false;
116 }
117
118
119
120
121 if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) {
122 ntfs_error(vi->i_sb, "$LogFile restart page is not modified "
123 "by chkdsk but a chkdsk LSN is specified.");
124 return false;
125 }
126 ntfs_debug("Done.");
127 return true;
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144static bool ntfs_check_restart_area(struct inode *vi, RESTART_PAGE_HEADER *rp)
145{
146 u64 file_size;
147 RESTART_AREA *ra;
148 u16 ra_ofs, ra_len, ca_ofs;
149 u8 fs_bits;
150
151 ntfs_debug("Entering.");
152 ra_ofs = le16_to_cpu(rp->restart_area_offset);
153 ra = (RESTART_AREA*)((u8*)rp + ra_ofs);
154
155
156
157
158
159 if (ra_ofs + offsetof(RESTART_AREA, file_size) >
160 NTFS_BLOCK_SIZE - sizeof(u16)) {
161 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
162 "inconsistent file offset.");
163 return false;
164 }
165
166
167
168
169
170
171
172 ca_ofs = le16_to_cpu(ra->client_array_offset);
173 if (((ca_ofs + 7) & ~7) != ca_ofs ||
174 ra_ofs + ca_ofs > NTFS_BLOCK_SIZE - sizeof(u16)) {
175 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
176 "inconsistent client array offset.");
177 return false;
178 }
179
180
181
182
183
184 ra_len = ca_ofs + le16_to_cpu(ra->log_clients) *
185 sizeof(LOG_CLIENT_RECORD);
186 if (ra_ofs + ra_len > le32_to_cpu(rp->system_page_size) ||
187 ra_ofs + le16_to_cpu(ra->restart_area_length) >
188 le32_to_cpu(rp->system_page_size) ||
189 ra_len > le16_to_cpu(ra->restart_area_length)) {
190 ntfs_error(vi->i_sb, "$LogFile restart area is out of bounds "
191 "of the system page size specified by the "
192 "restart page header and/or the specified "
193 "restart area length is inconsistent.");
194 return false;
195 }
196
197
198
199
200
201 if ((ra->client_free_list != LOGFILE_NO_CLIENT &&
202 le16_to_cpu(ra->client_free_list) >=
203 le16_to_cpu(ra->log_clients)) ||
204 (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
205 le16_to_cpu(ra->client_in_use_list) >=
206 le16_to_cpu(ra->log_clients))) {
207 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
208 "overflowing client free and/or in use lists.");
209 return false;
210 }
211
212
213
214
215 file_size = (u64)sle64_to_cpu(ra->file_size);
216 fs_bits = 0;
217 while (file_size) {
218 file_size >>= 1;
219 fs_bits++;
220 }
221 if (le32_to_cpu(ra->seq_number_bits) != 67 - fs_bits) {
222 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
223 "inconsistent sequence number bits.");
224 return false;
225 }
226
227 if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) !=
228 le16_to_cpu(ra->log_record_header_length)) {
229 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
230 "inconsistent log record header length.");
231 return false;
232 }
233
234 if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) !=
235 le16_to_cpu(ra->log_page_data_offset)) {
236 ntfs_error(vi->i_sb, "$LogFile restart area specifies "
237 "inconsistent log page data offset.");
238 return false;
239 }
240 ntfs_debug("Done.");
241 return true;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259static bool ntfs_check_log_client_array(struct inode *vi,
260 RESTART_PAGE_HEADER *rp)
261{
262 RESTART_AREA *ra;
263 LOG_CLIENT_RECORD *ca, *cr;
264 u16 nr_clients, idx;
265 bool in_free_list, idx_is_first;
266
267 ntfs_debug("Entering.");
268 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
269 ca = (LOG_CLIENT_RECORD*)((u8*)ra +
270 le16_to_cpu(ra->client_array_offset));
271
272
273
274
275
276
277
278
279 nr_clients = le16_to_cpu(ra->log_clients);
280 idx = le16_to_cpu(ra->client_free_list);
281 in_free_list = true;
282check_list:
283 for (idx_is_first = true; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--,
284 idx = le16_to_cpu(cr->next_client)) {
285 if (!nr_clients || idx >= le16_to_cpu(ra->log_clients))
286 goto err_out;
287
288 cr = ca + idx;
289
290 if (idx_is_first) {
291 if (cr->prev_client != LOGFILE_NO_CLIENT)
292 goto err_out;
293 idx_is_first = false;
294 }
295 }
296
297 if (in_free_list) {
298 in_free_list = false;
299 idx = le16_to_cpu(ra->client_in_use_list);
300 goto check_list;
301 }
302 ntfs_debug("Done.");
303 return true;
304err_out:
305 ntfs_error(vi->i_sb, "$LogFile log client array is corrupt.");
306 return false;
307}
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336static int ntfs_check_and_load_restart_page(struct inode *vi,
337 RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp,
338 LSN *lsn)
339{
340 RESTART_AREA *ra;
341 RESTART_PAGE_HEADER *trp;
342 int size, err;
343
344 ntfs_debug("Entering.");
345
346 if (!ntfs_check_restart_page_header(vi, rp, pos)) {
347
348 return -EINVAL;
349 }
350
351 if (!ntfs_check_restart_area(vi, rp)) {
352
353 return -EINVAL;
354 }
355 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
356
357
358
359
360 trp = ntfs_malloc_nofs(le32_to_cpu(rp->system_page_size));
361 if (!trp) {
362 ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile "
363 "restart page buffer.");
364 return -ENOMEM;
365 }
366
367
368
369
370
371 size = PAGE_SIZE - (pos & ~PAGE_MASK);
372 if (size >= le32_to_cpu(rp->system_page_size)) {
373 memcpy(trp, rp, le32_to_cpu(rp->system_page_size));
374 } else {
375 pgoff_t idx;
376 struct page *page;
377 int have_read, to_read;
378
379
380 memcpy(trp, rp, size);
381
382 have_read = size;
383 to_read = le32_to_cpu(rp->system_page_size) - size;
384 idx = (pos + size) >> PAGE_SHIFT;
385 BUG_ON((pos + size) & ~PAGE_MASK);
386 do {
387 page = ntfs_map_page(vi->i_mapping, idx);
388 if (IS_ERR(page)) {
389 ntfs_error(vi->i_sb, "Error mapping $LogFile "
390 "page (index %lu).", idx);
391 err = PTR_ERR(page);
392 if (err != -EIO && err != -ENOMEM)
393 err = -EIO;
394 goto err_out;
395 }
396 size = min_t(int, to_read, PAGE_SIZE);
397 memcpy((u8*)trp + have_read, page_address(page), size);
398 ntfs_unmap_page(page);
399 have_read += size;
400 to_read -= size;
401 idx++;
402 } while (to_read > 0);
403 }
404
405
406
407
408 if ((!ntfs_is_chkd_record(trp->magic) || le16_to_cpu(trp->usa_count))
409 && post_read_mst_fixup((NTFS_RECORD*)trp,
410 le32_to_cpu(rp->system_page_size))) {
411
412
413
414
415
416 if (le16_to_cpu(rp->restart_area_offset) +
417 le16_to_cpu(ra->restart_area_length) >
418 NTFS_BLOCK_SIZE - sizeof(u16)) {
419 ntfs_error(vi->i_sb, "Multi sector transfer error "
420 "detected in $LogFile restart page.");
421 err = -EINVAL;
422 goto err_out;
423 }
424 }
425
426
427
428
429
430 err = 0;
431 if (ntfs_is_rstr_record(rp->magic) &&
432 ra->client_in_use_list != LOGFILE_NO_CLIENT) {
433 if (!ntfs_check_log_client_array(vi, trp)) {
434 err = -EINVAL;
435 goto err_out;
436 }
437 }
438 if (lsn) {
439 if (ntfs_is_rstr_record(rp->magic))
440 *lsn = sle64_to_cpu(ra->current_lsn);
441 else
442 *lsn = sle64_to_cpu(rp->chkdsk_lsn);
443 }
444 ntfs_debug("Done.");
445 if (wrp)
446 *wrp = trp;
447 else {
448err_out:
449 ntfs_free(trp);
450 }
451 return err;
452}
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471bool ntfs_check_logfile(struct inode *log_vi, RESTART_PAGE_HEADER **rp)
472{
473 s64 size, pos;
474 LSN rstr1_lsn, rstr2_lsn;
475 ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
476 struct address_space *mapping = log_vi->i_mapping;
477 struct page *page = NULL;
478 u8 *kaddr = NULL;
479 RESTART_PAGE_HEADER *rstr1_ph = NULL;
480 RESTART_PAGE_HEADER *rstr2_ph = NULL;
481 int log_page_size, log_page_mask, err;
482 bool logfile_is_empty = true;
483 u8 log_page_bits;
484
485 ntfs_debug("Entering.");
486
487 if (NVolLogFileEmpty(vol))
488 goto is_empty;
489 size = i_size_read(log_vi);
490
491 if (size > MaxLogFileSize)
492 size = MaxLogFileSize;
493
494
495
496
497
498
499 if (PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <=
500 DefaultLogPageSize * 2)
501 log_page_size = DefaultLogPageSize;
502 else
503 log_page_size = PAGE_SIZE;
504 log_page_mask = log_page_size - 1;
505
506
507
508
509 log_page_bits = ntfs_ffs(log_page_size) - 1;
510 size &= ~(s64)(log_page_size - 1);
511
512
513
514
515 if (size < log_page_size * 2 || (size - log_page_size * 2) >>
516 log_page_bits < MinLogRecordPages) {
517 ntfs_error(vol->sb, "$LogFile is too small.");
518 return false;
519 }
520
521
522
523
524
525
526
527
528 for (pos = 0; pos < size; pos <<= 1) {
529 pgoff_t idx = pos >> PAGE_SHIFT;
530 if (!page || page->index != idx) {
531 if (page)
532 ntfs_unmap_page(page);
533 page = ntfs_map_page(mapping, idx);
534 if (IS_ERR(page)) {
535 ntfs_error(vol->sb, "Error mapping $LogFile "
536 "page (index %lu).", idx);
537 goto err_out;
538 }
539 }
540 kaddr = (u8*)page_address(page) + (pos & ~PAGE_MASK);
541
542
543
544
545
546 if (!ntfs_is_empty_recordp((le32*)kaddr))
547 logfile_is_empty = false;
548 else if (!logfile_is_empty)
549 break;
550
551
552
553
554 if (ntfs_is_rcrd_recordp((le32*)kaddr))
555 break;
556
557 if (!ntfs_is_rstr_recordp((le32*)kaddr) &&
558 !ntfs_is_chkd_recordp((le32*)kaddr)) {
559 if (!pos)
560 pos = NTFS_BLOCK_SIZE >> 1;
561 continue;
562 }
563
564
565
566
567
568 err = ntfs_check_and_load_restart_page(log_vi,
569 (RESTART_PAGE_HEADER*)kaddr, pos,
570 !rstr1_ph ? &rstr1_ph : &rstr2_ph,
571 !rstr1_ph ? &rstr1_lsn : &rstr2_lsn);
572 if (!err) {
573
574
575
576
577 if (!pos) {
578 pos = NTFS_BLOCK_SIZE >> 1;
579 continue;
580 }
581
582
583
584
585 break;
586 }
587
588
589
590
591
592 if (err != -EINVAL) {
593 ntfs_unmap_page(page);
594 goto err_out;
595 }
596
597 if (!pos)
598 pos = NTFS_BLOCK_SIZE >> 1;
599 }
600 if (page)
601 ntfs_unmap_page(page);
602 if (logfile_is_empty) {
603 NVolSetLogFileEmpty(vol);
604is_empty:
605 ntfs_debug("Done. ($LogFile is empty.)");
606 return true;
607 }
608 if (!rstr1_ph) {
609 BUG_ON(rstr2_ph);
610 ntfs_error(vol->sb, "Did not find any restart pages in "
611 "$LogFile and it was not empty.");
612 return false;
613 }
614
615 if (rstr2_ph) {
616
617
618
619
620 if (rstr2_lsn > rstr1_lsn) {
621 ntfs_debug("Using second restart page as it is more "
622 "recent.");
623 ntfs_free(rstr1_ph);
624 rstr1_ph = rstr2_ph;
625
626 } else {
627 ntfs_debug("Using first restart page as it is more "
628 "recent.");
629 ntfs_free(rstr2_ph);
630 }
631 rstr2_ph = NULL;
632 }
633
634 if (rp)
635 *rp = rstr1_ph;
636 else
637 ntfs_free(rstr1_ph);
638 ntfs_debug("Done.");
639 return true;
640err_out:
641 if (rstr1_ph)
642 ntfs_free(rstr1_ph);
643 return false;
644}
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666bool ntfs_is_logfile_clean(struct inode *log_vi, const RESTART_PAGE_HEADER *rp)
667{
668 ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
669 RESTART_AREA *ra;
670
671 ntfs_debug("Entering.");
672
673 if (NVolLogFileEmpty(vol)) {
674 ntfs_debug("Done. ($LogFile is empty.)");
675 return true;
676 }
677 BUG_ON(!rp);
678 if (!ntfs_is_rstr_record(rp->magic) &&
679 !ntfs_is_chkd_record(rp->magic)) {
680 ntfs_error(vol->sb, "Restart page buffer is invalid. This is "
681 "probably a bug in that the $LogFile should "
682 "have been consistency checked before calling "
683 "this function.");
684 return false;
685 }
686 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
687
688
689
690
691
692 if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
693 !(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
694 ntfs_debug("Done. $LogFile indicates a dirty shutdown.");
695 return false;
696 }
697
698 ntfs_debug("Done. $LogFile indicates a clean shutdown.");
699 return true;
700}
701
702
703
704
705
706
707
708
709
710
711
712
713bool ntfs_empty_logfile(struct inode *log_vi)
714{
715 VCN vcn, end_vcn;
716 ntfs_inode *log_ni = NTFS_I(log_vi);
717 ntfs_volume *vol = log_ni->vol;
718 struct super_block *sb = vol->sb;
719 runlist_element *rl;
720 unsigned long flags;
721 unsigned block_size, block_size_bits;
722 int err;
723 bool should_wait = true;
724
725 ntfs_debug("Entering.");
726 if (NVolLogFileEmpty(vol)) {
727 ntfs_debug("Done.");
728 return true;
729 }
730
731
732
733
734
735
736
737 block_size = sb->s_blocksize;
738 block_size_bits = sb->s_blocksize_bits;
739 vcn = 0;
740 read_lock_irqsave(&log_ni->size_lock, flags);
741 end_vcn = (log_ni->initialized_size + vol->cluster_size_mask) >>
742 vol->cluster_size_bits;
743 read_unlock_irqrestore(&log_ni->size_lock, flags);
744 truncate_inode_pages(log_vi->i_mapping, 0);
745 down_write(&log_ni->runlist.lock);
746 rl = log_ni->runlist.rl;
747 if (unlikely(!rl || vcn < rl->vcn || !rl->length)) {
748map_vcn:
749 err = ntfs_map_runlist_nolock(log_ni, vcn, NULL);
750 if (err) {
751 ntfs_error(sb, "Failed to map runlist fragment (error "
752 "%d).", -err);
753 goto err;
754 }
755 rl = log_ni->runlist.rl;
756 BUG_ON(!rl || vcn < rl->vcn || !rl->length);
757 }
758
759 while (rl->length && vcn >= rl[1].vcn)
760 rl++;
761 do {
762 LCN lcn;
763 sector_t block, end_block;
764 s64 len;
765
766
767
768
769
770 lcn = rl->lcn;
771 if (unlikely(lcn == LCN_RL_NOT_MAPPED)) {
772 vcn = rl->vcn;
773 goto map_vcn;
774 }
775
776 if (unlikely(!rl->length || lcn < LCN_HOLE))
777 goto rl_err;
778
779 if (lcn == LCN_HOLE)
780 continue;
781 block = lcn << vol->cluster_size_bits >> block_size_bits;
782 len = rl->length;
783 if (rl[1].vcn > end_vcn)
784 len = end_vcn - rl->vcn;
785 end_block = (lcn + len) << vol->cluster_size_bits >>
786 block_size_bits;
787
788 do {
789 struct buffer_head *bh;
790
791
792 bh = sb_getblk(sb, block);
793 BUG_ON(!bh);
794
795 lock_buffer(bh);
796 bh->b_end_io = end_buffer_write_sync;
797 get_bh(bh);
798
799 memset(bh->b_data, -1, block_size);
800 if (!buffer_uptodate(bh))
801 set_buffer_uptodate(bh);
802 if (buffer_dirty(bh))
803 clear_buffer_dirty(bh);
804
805
806
807
808
809
810
811 submit_bh(REQ_OP_WRITE, 0, bh);
812 if (should_wait) {
813 should_wait = false;
814 wait_on_buffer(bh);
815 if (unlikely(!buffer_uptodate(bh)))
816 goto io_err;
817 }
818 brelse(bh);
819 } while (++block < end_block);
820 } while ((++rl)->vcn < end_vcn);
821 up_write(&log_ni->runlist.lock);
822
823
824
825
826
827
828
829
830 truncate_inode_pages(log_vi->i_mapping, 0);
831
832 NVolSetLogFileEmpty(vol);
833 ntfs_debug("Done.");
834 return true;
835io_err:
836 ntfs_error(sb, "Failed to write buffer. Unmount and run chkdsk.");
837 goto dirty_err;
838rl_err:
839 ntfs_error(sb, "Runlist is corrupt. Unmount and run chkdsk.");
840dirty_err:
841 NVolSetErrors(vol);
842 err = -EIO;
843err:
844 up_write(&log_ni->runlist.lock);
845 ntfs_error(sb, "Failed to fill $LogFile with 0xff bytes (error %d).",
846 -err);
847 return false;
848}
849
850#endif
851