1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/buffer_head.h>
24#include <linux/swap.h>
25
26#include "attrib.h"
27#include "aops.h"
28#include "bitmap.h"
29#include "debug.h"
30#include "dir.h"
31#include "lcnalloc.h"
32#include "malloc.h"
33#include "mft.h"
34#include "ntfs.h"
35
36
37
38
39
40
41
42
43
44
45
46static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni)
47{
48 loff_t i_size;
49 ntfs_volume *vol = ni->vol;
50 struct inode *mft_vi = vol->mft_ino;
51 struct page *page;
52 unsigned long index, end_index;
53 unsigned ofs;
54
55 BUG_ON(ni->page);
56
57
58
59
60
61
62 index = (u64)ni->mft_no << vol->mft_record_size_bits >>
63 PAGE_CACHE_SHIFT;
64 ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
65
66 i_size = i_size_read(mft_vi);
67
68 end_index = i_size >> PAGE_CACHE_SHIFT;
69
70
71 if (unlikely(index >= end_index)) {
72 if (index > end_index || (i_size & ~PAGE_CACHE_MASK) < ofs +
73 vol->mft_record_size) {
74 page = ERR_PTR(-ENOENT);
75 ntfs_error(vol->sb, "Attemt to read mft record 0x%lx, "
76 "which is beyond the end of the mft. "
77 "This is probably a bug in the ntfs "
78 "driver.", ni->mft_no);
79 goto err_out;
80 }
81 }
82
83 page = ntfs_map_page(mft_vi->i_mapping, index);
84 if (likely(!IS_ERR(page))) {
85
86 if (likely(ntfs_is_mft_recordp((le32*)(page_address(page) +
87 ofs)))) {
88 ni->page = page;
89 ni->page_ofs = ofs;
90 return page_address(page) + ofs;
91 }
92 ntfs_error(vol->sb, "Mft record 0x%lx is corrupt. "
93 "Run chkdsk.", ni->mft_no);
94 ntfs_unmap_page(page);
95 page = ERR_PTR(-EIO);
96 NVolSetErrors(vol);
97 }
98err_out:
99 ni->page = NULL;
100 ni->page_ofs = 0;
101 return (void*)page;
102}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154MFT_RECORD *map_mft_record(ntfs_inode *ni)
155{
156 MFT_RECORD *m;
157
158 ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
159
160
161 atomic_inc(&ni->count);
162
163
164 mutex_lock(&ni->mrec_lock);
165
166 m = map_mft_record_page(ni);
167 if (likely(!IS_ERR(m)))
168 return m;
169
170 mutex_unlock(&ni->mrec_lock);
171 atomic_dec(&ni->count);
172 ntfs_error(ni->vol->sb, "Failed with error code %lu.", -PTR_ERR(m));
173 return m;
174}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190static inline void unmap_mft_record_page(ntfs_inode *ni)
191{
192 BUG_ON(!ni->page);
193
194
195 ntfs_unmap_page(ni->page);
196 ni->page = NULL;
197 ni->page_ofs = 0;
198 return;
199}
200
201
202
203
204
205
206
207
208
209
210
211
212void unmap_mft_record(ntfs_inode *ni)
213{
214 struct page *page = ni->page;
215
216 BUG_ON(!page);
217
218 ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
219
220 unmap_mft_record_page(ni);
221 mutex_unlock(&ni->mrec_lock);
222 atomic_dec(&ni->count);
223
224
225
226
227
228
229 return;
230}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245MFT_RECORD *map_extent_mft_record(ntfs_inode *base_ni, MFT_REF mref,
246 ntfs_inode **ntfs_ino)
247{
248 MFT_RECORD *m;
249 ntfs_inode *ni = NULL;
250 ntfs_inode **extent_nis = NULL;
251 int i;
252 unsigned long mft_no = MREF(mref);
253 u16 seq_no = MSEQNO(mref);
254 bool destroy_ni = false;
255
256 ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).",
257 mft_no, base_ni->mft_no);
258
259 atomic_inc(&base_ni->count);
260
261
262
263
264
265 mutex_lock(&base_ni->extent_lock);
266 if (base_ni->nr_extents > 0) {
267 extent_nis = base_ni->ext.extent_ntfs_inos;
268 for (i = 0; i < base_ni->nr_extents; i++) {
269 if (mft_no != extent_nis[i]->mft_no)
270 continue;
271 ni = extent_nis[i];
272
273 atomic_inc(&ni->count);
274 break;
275 }
276 }
277 if (likely(ni != NULL)) {
278 mutex_unlock(&base_ni->extent_lock);
279 atomic_dec(&base_ni->count);
280
281 m = map_mft_record(ni);
282
283 atomic_dec(&ni->count);
284 if (likely(!IS_ERR(m))) {
285
286 if (likely(le16_to_cpu(m->sequence_number) == seq_no)) {
287 ntfs_debug("Done 1.");
288 *ntfs_ino = ni;
289 return m;
290 }
291 unmap_mft_record(ni);
292 ntfs_error(base_ni->vol->sb, "Found stale extent mft "
293 "reference! Corrupt filesystem. "
294 "Run chkdsk.");
295 return ERR_PTR(-EIO);
296 }
297map_err_out:
298 ntfs_error(base_ni->vol->sb, "Failed to map extent "
299 "mft record, error code %ld.", -PTR_ERR(m));
300 return m;
301 }
302
303 ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no);
304 if (unlikely(!ni)) {
305 mutex_unlock(&base_ni->extent_lock);
306 atomic_dec(&base_ni->count);
307 return ERR_PTR(-ENOMEM);
308 }
309 ni->vol = base_ni->vol;
310 ni->seq_no = seq_no;
311 ni->nr_extents = -1;
312 ni->ext.base_ntfs_ino = base_ni;
313
314 m = map_mft_record(ni);
315 if (IS_ERR(m)) {
316 mutex_unlock(&base_ni->extent_lock);
317 atomic_dec(&base_ni->count);
318 ntfs_clear_extent_inode(ni);
319 goto map_err_out;
320 }
321
322 if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) {
323 ntfs_error(base_ni->vol->sb, "Found stale extent mft "
324 "reference! Corrupt filesystem. Run chkdsk.");
325 destroy_ni = true;
326 m = ERR_PTR(-EIO);
327 goto unm_err_out;
328 }
329
330 if (!(base_ni->nr_extents & 3)) {
331 ntfs_inode **tmp;
332 int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
333
334 tmp = kmalloc(new_size, GFP_NOFS);
335 if (unlikely(!tmp)) {
336 ntfs_error(base_ni->vol->sb, "Failed to allocate "
337 "internal buffer.");
338 destroy_ni = true;
339 m = ERR_PTR(-ENOMEM);
340 goto unm_err_out;
341 }
342 if (base_ni->nr_extents) {
343 BUG_ON(!base_ni->ext.extent_ntfs_inos);
344 memcpy(tmp, base_ni->ext.extent_ntfs_inos, new_size -
345 4 * sizeof(ntfs_inode *));
346 kfree(base_ni->ext.extent_ntfs_inos);
347 }
348 base_ni->ext.extent_ntfs_inos = tmp;
349 }
350 base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni;
351 mutex_unlock(&base_ni->extent_lock);
352 atomic_dec(&base_ni->count);
353 ntfs_debug("Done 2.");
354 *ntfs_ino = ni;
355 return m;
356unm_err_out:
357 unmap_mft_record(ni);
358 mutex_unlock(&base_ni->extent_lock);
359 atomic_dec(&base_ni->count);
360
361
362
363
364 if (destroy_ni)
365 ntfs_clear_extent_inode(ni);
366 return m;
367}
368
369#ifdef NTFS_RW
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395void __mark_mft_record_dirty(ntfs_inode *ni)
396{
397 ntfs_inode *base_ni;
398
399 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
400 BUG_ON(NInoAttr(ni));
401 mark_ntfs_record_dirty(ni->page, ni->page_ofs);
402
403 mutex_lock(&ni->extent_lock);
404 if (likely(ni->nr_extents >= 0))
405 base_ni = ni;
406 else
407 base_ni = ni->ext.base_ntfs_ino;
408 mutex_unlock(&ni->extent_lock);
409 __mark_inode_dirty(VFS_I(base_ni), I_DIRTY_SYNC | I_DIRTY_DATASYNC);
410}
411
412static const char *ntfs_please_email = "Please email "
413 "linux-ntfs-dev@lists.sourceforge.net and say that you saw "
414 "this message. Thank you.";
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438static int ntfs_sync_mft_mirror_umount(ntfs_volume *vol,
439 const unsigned long mft_no, MFT_RECORD *m)
440{
441 BUG_ON(vol->mftmirr_ino);
442 ntfs_error(vol->sb, "Umount time mft mirror syncing is not "
443 "implemented yet. %s", ntfs_please_email);
444 return -EOPNOTSUPP;
445}
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
466 MFT_RECORD *m, int sync)
467{
468 struct page *page;
469 unsigned int blocksize = vol->sb->s_blocksize;
470 int max_bhs = vol->mft_record_size / blocksize;
471 struct buffer_head *bhs[max_bhs];
472 struct buffer_head *bh, *head;
473 u8 *kmirr;
474 runlist_element *rl;
475 unsigned int block_start, block_end, m_start, m_end, page_ofs;
476 int i_bhs, nr_bhs, err = 0;
477 unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
478
479 ntfs_debug("Entering for inode 0x%lx.", mft_no);
480 BUG_ON(!max_bhs);
481 if (unlikely(!vol->mftmirr_ino)) {
482
483 err = ntfs_sync_mft_mirror_umount(vol, mft_no, m);
484 if (likely(!err))
485 return err;
486 goto err_out;
487 }
488
489 page = ntfs_map_page(vol->mftmirr_ino->i_mapping, mft_no >>
490 (PAGE_CACHE_SHIFT - vol->mft_record_size_bits));
491 if (IS_ERR(page)) {
492 ntfs_error(vol->sb, "Failed to map mft mirror page.");
493 err = PTR_ERR(page);
494 goto err_out;
495 }
496 lock_page(page);
497 BUG_ON(!PageUptodate(page));
498 ClearPageUptodate(page);
499
500 page_ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
501
502 kmirr = page_address(page) + page_ofs;
503
504 memcpy(kmirr, m, vol->mft_record_size);
505
506 if (unlikely(!page_has_buffers(page))) {
507 struct buffer_head *tail;
508
509 bh = head = alloc_page_buffers(page, blocksize, 1);
510 do {
511 set_buffer_uptodate(bh);
512 tail = bh;
513 bh = bh->b_this_page;
514 } while (bh);
515 tail->b_this_page = head;
516 attach_page_buffers(page, head);
517 }
518 bh = head = page_buffers(page);
519 BUG_ON(!bh);
520 rl = NULL;
521 nr_bhs = 0;
522 block_start = 0;
523 m_start = kmirr - (u8*)page_address(page);
524 m_end = m_start + vol->mft_record_size;
525 do {
526 block_end = block_start + blocksize;
527
528 if (block_end <= m_start)
529 continue;
530 if (unlikely(block_start >= m_end))
531 break;
532
533 if (unlikely(!buffer_mapped(bh))) {
534 VCN vcn;
535 LCN lcn;
536 unsigned int vcn_ofs;
537
538 bh->b_bdev = vol->sb->s_bdev;
539
540 vcn = ((VCN)mft_no << vol->mft_record_size_bits) +
541 (block_start - m_start);
542 vcn_ofs = vcn & vol->cluster_size_mask;
543 vcn >>= vol->cluster_size_bits;
544 if (!rl) {
545 down_read(&NTFS_I(vol->mftmirr_ino)->
546 runlist.lock);
547 rl = NTFS_I(vol->mftmirr_ino)->runlist.rl;
548
549
550
551
552 BUG_ON(!rl);
553 }
554
555 while (rl->length && rl[1].vcn <= vcn)
556 rl++;
557 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
558
559 if (likely(lcn >= 0)) {
560
561 bh->b_blocknr = ((lcn <<
562 vol->cluster_size_bits) +
563 vcn_ofs) >> blocksize_bits;
564 set_buffer_mapped(bh);
565 } else {
566 bh->b_blocknr = -1;
567 ntfs_error(vol->sb, "Cannot write mft mirror "
568 "record 0x%lx because its "
569 "location on disk could not "
570 "be determined (error code "
571 "%lli).", mft_no,
572 (long long)lcn);
573 err = -EIO;
574 }
575 }
576 BUG_ON(!buffer_uptodate(bh));
577 BUG_ON(!nr_bhs && (m_start != block_start));
578 BUG_ON(nr_bhs >= max_bhs);
579 bhs[nr_bhs++] = bh;
580 BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
581 } while (block_start = block_end, (bh = bh->b_this_page) != head);
582 if (unlikely(rl))
583 up_read(&NTFS_I(vol->mftmirr_ino)->runlist.lock);
584 if (likely(!err)) {
585
586 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
587 struct buffer_head *tbh = bhs[i_bhs];
588
589 if (!trylock_buffer(tbh))
590 BUG();
591 BUG_ON(!buffer_uptodate(tbh));
592 clear_buffer_dirty(tbh);
593 get_bh(tbh);
594 tbh->b_end_io = end_buffer_write_sync;
595 submit_bh(WRITE, tbh);
596 }
597
598 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
599 struct buffer_head *tbh = bhs[i_bhs];
600
601 wait_on_buffer(tbh);
602 if (unlikely(!buffer_uptodate(tbh))) {
603 err = -EIO;
604
605
606
607
608 set_buffer_uptodate(tbh);
609 }
610 }
611 } else {
612
613 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
614 clear_buffer_dirty(bhs[i_bhs]);
615 }
616
617
618 post_write_mst_fixup((NTFS_RECORD*)kmirr);
619 flush_dcache_page(page);
620 SetPageUptodate(page);
621 unlock_page(page);
622 ntfs_unmap_page(page);
623 if (likely(!err)) {
624 ntfs_debug("Done.");
625 } else {
626 ntfs_error(vol->sb, "I/O error while writing mft mirror "
627 "record 0x%lx!", mft_no);
628err_out:
629 ntfs_error(vol->sb, "Failed to synchronize $MFTMirr (error "
630 "code %i). Volume will be left marked dirty "
631 "on umount. Run ntfsfix on the partition "
632 "after umounting to correct this.", -err);
633 NVolSetErrors(vol);
634 }
635 return err;
636}
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync)
670{
671 ntfs_volume *vol = ni->vol;
672 struct page *page = ni->page;
673 unsigned int blocksize = vol->sb->s_blocksize;
674 unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
675 int max_bhs = vol->mft_record_size / blocksize;
676 struct buffer_head *bhs[max_bhs];
677 struct buffer_head *bh, *head;
678 runlist_element *rl;
679 unsigned int block_start, block_end, m_start, m_end;
680 int i_bhs, nr_bhs, err = 0;
681
682 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
683 BUG_ON(NInoAttr(ni));
684 BUG_ON(!max_bhs);
685 BUG_ON(!PageLocked(page));
686
687
688
689
690
691
692 if (!NInoTestClearDirty(ni))
693 goto done;
694 bh = head = page_buffers(page);
695 BUG_ON(!bh);
696 rl = NULL;
697 nr_bhs = 0;
698 block_start = 0;
699 m_start = ni->page_ofs;
700 m_end = m_start + vol->mft_record_size;
701 do {
702 block_end = block_start + blocksize;
703
704 if (block_end <= m_start)
705 continue;
706 if (unlikely(block_start >= m_end))
707 break;
708
709
710
711
712
713 if (block_start == m_start) {
714
715 if (!buffer_dirty(bh)) {
716 BUG_ON(nr_bhs);
717
718 break;
719 }
720 }
721
722 if (unlikely(!buffer_mapped(bh))) {
723 VCN vcn;
724 LCN lcn;
725 unsigned int vcn_ofs;
726
727 bh->b_bdev = vol->sb->s_bdev;
728
729 vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) +
730 (block_start - m_start);
731 vcn_ofs = vcn & vol->cluster_size_mask;
732 vcn >>= vol->cluster_size_bits;
733 if (!rl) {
734 down_read(&NTFS_I(vol->mft_ino)->runlist.lock);
735 rl = NTFS_I(vol->mft_ino)->runlist.rl;
736 BUG_ON(!rl);
737 }
738
739 while (rl->length && rl[1].vcn <= vcn)
740 rl++;
741 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
742
743 if (likely(lcn >= 0)) {
744
745 bh->b_blocknr = ((lcn <<
746 vol->cluster_size_bits) +
747 vcn_ofs) >> blocksize_bits;
748 set_buffer_mapped(bh);
749 } else {
750 bh->b_blocknr = -1;
751 ntfs_error(vol->sb, "Cannot write mft record "
752 "0x%lx because its location "
753 "on disk could not be "
754 "determined (error code %lli).",
755 ni->mft_no, (long long)lcn);
756 err = -EIO;
757 }
758 }
759 BUG_ON(!buffer_uptodate(bh));
760 BUG_ON(!nr_bhs && (m_start != block_start));
761 BUG_ON(nr_bhs >= max_bhs);
762 bhs[nr_bhs++] = bh;
763 BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
764 } while (block_start = block_end, (bh = bh->b_this_page) != head);
765 if (unlikely(rl))
766 up_read(&NTFS_I(vol->mft_ino)->runlist.lock);
767 if (!nr_bhs)
768 goto done;
769 if (unlikely(err))
770 goto cleanup_out;
771
772 err = pre_write_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size);
773 if (err) {
774 ntfs_error(vol->sb, "Failed to apply mst fixups!");
775 goto cleanup_out;
776 }
777 flush_dcache_mft_record_page(ni);
778
779 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
780 struct buffer_head *tbh = bhs[i_bhs];
781
782 if (!trylock_buffer(tbh))
783 BUG();
784 BUG_ON(!buffer_uptodate(tbh));
785 clear_buffer_dirty(tbh);
786 get_bh(tbh);
787 tbh->b_end_io = end_buffer_write_sync;
788 submit_bh(WRITE, tbh);
789 }
790
791 if (!sync && ni->mft_no < vol->mftmirr_size)
792 ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
793
794 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
795 struct buffer_head *tbh = bhs[i_bhs];
796
797 wait_on_buffer(tbh);
798 if (unlikely(!buffer_uptodate(tbh))) {
799 err = -EIO;
800
801
802
803
804 if (PageUptodate(page))
805 set_buffer_uptodate(tbh);
806 }
807 }
808
809 if (sync && ni->mft_no < vol->mftmirr_size)
810 ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
811
812 post_write_mst_fixup((NTFS_RECORD*)m);
813 flush_dcache_mft_record_page(ni);
814 if (unlikely(err)) {
815
816 ntfs_error(vol->sb, "I/O error while writing mft record "
817 "0x%lx! Marking base inode as bad. You "
818 "should unmount the volume and run chkdsk.",
819 ni->mft_no);
820 goto err_out;
821 }
822done:
823 ntfs_debug("Done.");
824 return 0;
825cleanup_out:
826
827 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
828 clear_buffer_dirty(bhs[i_bhs]);
829err_out:
830
831
832
833
834
835
836 if (err == -ENOMEM) {
837 ntfs_error(vol->sb, "Not enough memory to write mft record. "
838 "Redirtying so the write is retried later.");
839 mark_mft_record_dirty(ni);
840 err = 0;
841 } else
842 NVolSetErrors(vol);
843 return err;
844}
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925bool ntfs_may_write_mft_record(ntfs_volume *vol, const unsigned long mft_no,
926 const MFT_RECORD *m, ntfs_inode **locked_ni)
927{
928 struct super_block *sb = vol->sb;
929 struct inode *mft_vi = vol->mft_ino;
930 struct inode *vi;
931 ntfs_inode *ni, *eni, **extent_nis;
932 int i;
933 ntfs_attr na;
934
935 ntfs_debug("Entering for inode 0x%lx.", mft_no);
936
937
938
939 BUG_ON(!locked_ni);
940 *locked_ni = NULL;
941
942
943
944
945 ntfs_debug("Looking for inode 0x%lx in icache.", mft_no);
946 na.mft_no = mft_no;
947 na.name = NULL;
948 na.name_len = 0;
949 na.type = AT_UNUSED;
950
951
952
953
954 if (!mft_no) {
955
956 vi = igrab(mft_vi);
957 BUG_ON(vi != mft_vi);
958 } else {
959
960
961
962
963
964
965
966 vi = ilookup5_nowait(sb, mft_no, (test_t)ntfs_test_inode, &na);
967 }
968 if (vi) {
969 ntfs_debug("Base inode 0x%lx is in icache.", mft_no);
970
971 ni = NTFS_I(vi);
972
973 atomic_inc(&ni->count);
974
975 if (NInoDirty(ni)) {
976 ntfs_debug("Inode 0x%lx is dirty, do not write it.",
977 mft_no);
978 atomic_dec(&ni->count);
979 iput(vi);
980 return false;
981 }
982 ntfs_debug("Inode 0x%lx is not dirty.", mft_no);
983
984 if (unlikely(!mutex_trylock(&ni->mrec_lock))) {
985 ntfs_debug("Mft record 0x%lx is already locked, do "
986 "not write it.", mft_no);
987 atomic_dec(&ni->count);
988 iput(vi);
989 return false;
990 }
991 ntfs_debug("Managed to lock mft record 0x%lx, write it.",
992 mft_no);
993
994
995
996
997 *locked_ni = ni;
998 return true;
999 }
1000 ntfs_debug("Inode 0x%lx is not in icache.", mft_no);
1001
1002
1003 if (!ntfs_is_mft_record(m->magic)) {
1004 ntfs_debug("Mft record 0x%lx is not a FILE record, write it.",
1005 mft_no);
1006 return true;
1007 }
1008
1009 if (!m->base_mft_record) {
1010 ntfs_debug("Mft record 0x%lx is a base record, write it.",
1011 mft_no);
1012 return true;
1013 }
1014
1015
1016
1017
1018
1019 na.mft_no = MREF_LE(m->base_mft_record);
1020 ntfs_debug("Mft record 0x%lx is an extent record. Looking for base "
1021 "inode 0x%lx in icache.", mft_no, na.mft_no);
1022 if (!na.mft_no) {
1023
1024 vi = igrab(mft_vi);
1025 BUG_ON(vi != mft_vi);
1026 } else
1027 vi = ilookup5_nowait(sb, na.mft_no, (test_t)ntfs_test_inode,
1028 &na);
1029 if (!vi) {
1030
1031
1032
1033
1034 ntfs_debug("Base inode 0x%lx is not in icache, write the "
1035 "extent record.", na.mft_no);
1036 return true;
1037 }
1038 ntfs_debug("Base inode 0x%lx is in icache.", na.mft_no);
1039
1040
1041
1042
1043 ni = NTFS_I(vi);
1044 mutex_lock(&ni->extent_lock);
1045 if (ni->nr_extents <= 0) {
1046
1047
1048
1049
1050 mutex_unlock(&ni->extent_lock);
1051 iput(vi);
1052 ntfs_debug("Base inode 0x%lx has no attached extent inodes, "
1053 "write the extent record.", na.mft_no);
1054 return true;
1055 }
1056
1057 extent_nis = ni->ext.extent_ntfs_inos;
1058 for (eni = NULL, i = 0; i < ni->nr_extents; ++i) {
1059 if (mft_no == extent_nis[i]->mft_no) {
1060
1061
1062
1063
1064 eni = extent_nis[i];
1065 break;
1066 }
1067 }
1068
1069
1070
1071
1072 if (!eni) {
1073 mutex_unlock(&ni->extent_lock);
1074 iput(vi);
1075 ntfs_debug("Extent inode 0x%lx is not attached to its base "
1076 "inode 0x%lx, write the extent record.",
1077 mft_no, na.mft_no);
1078 return true;
1079 }
1080 ntfs_debug("Extent inode 0x%lx is attached to its base inode 0x%lx.",
1081 mft_no, na.mft_no);
1082
1083 atomic_inc(&eni->count);
1084 mutex_unlock(&ni->extent_lock);
1085
1086
1087
1088
1089 if (unlikely(!mutex_trylock(&eni->mrec_lock))) {
1090 atomic_dec(&eni->count);
1091 iput(vi);
1092 ntfs_debug("Extent mft record 0x%lx is already locked, do "
1093 "not write it.", mft_no);
1094 return false;
1095 }
1096 ntfs_debug("Managed to lock extent mft record 0x%lx, write it.",
1097 mft_no);
1098 if (NInoTestClearDirty(eni))
1099 ntfs_debug("Extent inode 0x%lx is dirty, marking it clean.",
1100 mft_no);
1101
1102
1103
1104
1105 *locked_ni = eni;
1106 return true;
1107}
1108
1109static const char *es = " Leaving inconsistent metadata. Unmount and run "
1110 "chkdsk.";
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
1132 ntfs_inode *base_ni)
1133{
1134 s64 pass_end, ll, data_pos, pass_start, ofs, bit;
1135 unsigned long flags;
1136 struct address_space *mftbmp_mapping;
1137 u8 *buf, *byte;
1138 struct page *page;
1139 unsigned int page_ofs, size;
1140 u8 pass, b;
1141
1142 ntfs_debug("Searching for free mft record in the currently "
1143 "initialized mft bitmap.");
1144 mftbmp_mapping = vol->mftbmp_ino->i_mapping;
1145
1146
1147
1148
1149 read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags);
1150 pass_end = NTFS_I(vol->mft_ino)->allocated_size >>
1151 vol->mft_record_size_bits;
1152 read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags);
1153 read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1154 ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3;
1155 read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1156 if (pass_end > ll)
1157 pass_end = ll;
1158 pass = 1;
1159 if (!base_ni)
1160 data_pos = vol->mft_data_pos;
1161 else
1162 data_pos = base_ni->mft_no + 1;
1163 if (data_pos < 24)
1164 data_pos = 24;
1165 if (data_pos >= pass_end) {
1166 data_pos = 24;
1167 pass = 2;
1168
1169 if (data_pos >= pass_end)
1170 return -ENOSPC;
1171 }
1172 pass_start = data_pos;
1173 ntfs_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
1174 "pass_end 0x%llx, data_pos 0x%llx.", pass,
1175 (long long)pass_start, (long long)pass_end,
1176 (long long)data_pos);
1177
1178 for (; pass <= 2;) {
1179
1180 ofs = data_pos >> 3;
1181 page_ofs = ofs & ~PAGE_CACHE_MASK;
1182 size = PAGE_CACHE_SIZE - page_ofs;
1183 ll = ((pass_end + 7) >> 3) - ofs;
1184 if (size > ll)
1185 size = ll;
1186 size <<= 3;
1187
1188
1189
1190
1191 if (size) {
1192 page = ntfs_map_page(mftbmp_mapping,
1193 ofs >> PAGE_CACHE_SHIFT);
1194 if (IS_ERR(page)) {
1195 ntfs_error(vol->sb, "Failed to read mft "
1196 "bitmap, aborting.");
1197 return PTR_ERR(page);
1198 }
1199 buf = (u8*)page_address(page) + page_ofs;
1200 bit = data_pos & 7;
1201 data_pos &= ~7ull;
1202 ntfs_debug("Before inner for loop: size 0x%x, "
1203 "data_pos 0x%llx, bit 0x%llx", size,
1204 (long long)data_pos, (long long)bit);
1205 for (; bit < size && data_pos + bit < pass_end;
1206 bit &= ~7ull, bit += 8) {
1207 byte = buf + (bit >> 3);
1208 if (*byte == 0xff)
1209 continue;
1210 b = ffz((unsigned long)*byte);
1211 if (b < 8 && b >= (bit & 7)) {
1212 ll = data_pos + (bit & ~7ull) + b;
1213 if (unlikely(ll > (1ll << 32))) {
1214 ntfs_unmap_page(page);
1215 return -ENOSPC;
1216 }
1217 *byte |= 1 << b;
1218 flush_dcache_page(page);
1219 set_page_dirty(page);
1220 ntfs_unmap_page(page);
1221 ntfs_debug("Done. (Found and "
1222 "allocated mft record "
1223 "0x%llx.)",
1224 (long long)ll);
1225 return ll;
1226 }
1227 }
1228 ntfs_debug("After inner for loop: size 0x%x, "
1229 "data_pos 0x%llx, bit 0x%llx", size,
1230 (long long)data_pos, (long long)bit);
1231 data_pos += size;
1232 ntfs_unmap_page(page);
1233
1234
1235
1236
1237 if (data_pos < pass_end)
1238 continue;
1239 }
1240
1241 if (++pass == 2) {
1242
1243
1244
1245
1246 pass_end = pass_start;
1247 data_pos = pass_start = 24;
1248 ntfs_debug("pass %i, pass_start 0x%llx, pass_end "
1249 "0x%llx.", pass, (long long)pass_start,
1250 (long long)pass_end);
1251 if (data_pos >= pass_end)
1252 break;
1253 }
1254 }
1255
1256 ntfs_debug("Done. (No free mft records left in currently initialized "
1257 "mft bitmap.)");
1258 return -ENOSPC;
1259}
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1279{
1280 LCN lcn;
1281 s64 ll;
1282 unsigned long flags;
1283 struct page *page;
1284 ntfs_inode *mft_ni, *mftbmp_ni;
1285 runlist_element *rl, *rl2 = NULL;
1286 ntfs_attr_search_ctx *ctx = NULL;
1287 MFT_RECORD *mrec;
1288 ATTR_RECORD *a = NULL;
1289 int ret, mp_size;
1290 u32 old_alen = 0;
1291 u8 *b, tb;
1292 struct {
1293 u8 added_cluster:1;
1294 u8 added_run:1;
1295 u8 mp_rebuilt:1;
1296 } status = { 0, 0, 0 };
1297
1298 ntfs_debug("Extending mft bitmap allocation.");
1299 mft_ni = NTFS_I(vol->mft_ino);
1300 mftbmp_ni = NTFS_I(vol->mftbmp_ino);
1301
1302
1303
1304
1305 down_write(&mftbmp_ni->runlist.lock);
1306 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1307 ll = mftbmp_ni->allocated_size;
1308 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1309 rl = ntfs_attr_find_vcn_nolock(mftbmp_ni,
1310 (ll - 1) >> vol->cluster_size_bits, NULL);
1311 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
1312 up_write(&mftbmp_ni->runlist.lock);
1313 ntfs_error(vol->sb, "Failed to determine last allocated "
1314 "cluster of mft bitmap attribute.");
1315 if (!IS_ERR(rl))
1316 ret = -EIO;
1317 else
1318 ret = PTR_ERR(rl);
1319 return ret;
1320 }
1321 lcn = rl->lcn + rl->length;
1322 ntfs_debug("Last lcn of mft bitmap attribute is 0x%llx.",
1323 (long long)lcn);
1324
1325
1326
1327
1328
1329 ll = lcn >> 3;
1330 page = ntfs_map_page(vol->lcnbmp_ino->i_mapping,
1331 ll >> PAGE_CACHE_SHIFT);
1332 if (IS_ERR(page)) {
1333 up_write(&mftbmp_ni->runlist.lock);
1334 ntfs_error(vol->sb, "Failed to read from lcn bitmap.");
1335 return PTR_ERR(page);
1336 }
1337 b = (u8*)page_address(page) + (ll & ~PAGE_CACHE_MASK);
1338 tb = 1 << (lcn & 7ull);
1339 down_write(&vol->lcnbmp_lock);
1340 if (*b != 0xff && !(*b & tb)) {
1341
1342 *b |= tb;
1343 flush_dcache_page(page);
1344 set_page_dirty(page);
1345 up_write(&vol->lcnbmp_lock);
1346 ntfs_unmap_page(page);
1347
1348 rl->length++;
1349 rl[1].vcn++;
1350 status.added_cluster = 1;
1351 ntfs_debug("Appending one cluster to mft bitmap.");
1352 } else {
1353 up_write(&vol->lcnbmp_lock);
1354 ntfs_unmap_page(page);
1355
1356 rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE,
1357 true);
1358 if (IS_ERR(rl2)) {
1359 up_write(&mftbmp_ni->runlist.lock);
1360 ntfs_error(vol->sb, "Failed to allocate a cluster for "
1361 "the mft bitmap.");
1362 return PTR_ERR(rl2);
1363 }
1364 rl = ntfs_runlists_merge(mftbmp_ni->runlist.rl, rl2);
1365 if (IS_ERR(rl)) {
1366 up_write(&mftbmp_ni->runlist.lock);
1367 ntfs_error(vol->sb, "Failed to merge runlists for mft "
1368 "bitmap.");
1369 if (ntfs_cluster_free_from_rl(vol, rl2)) {
1370 ntfs_error(vol->sb, "Failed to dealocate "
1371 "allocated cluster.%s", es);
1372 NVolSetErrors(vol);
1373 }
1374 ntfs_free(rl2);
1375 return PTR_ERR(rl);
1376 }
1377 mftbmp_ni->runlist.rl = rl;
1378 status.added_run = 1;
1379 ntfs_debug("Adding one run to mft bitmap.");
1380
1381 for (; rl[1].length; rl++)
1382 ;
1383 }
1384
1385
1386
1387
1388 mrec = map_mft_record(mft_ni);
1389 if (IS_ERR(mrec)) {
1390 ntfs_error(vol->sb, "Failed to map mft record.");
1391 ret = PTR_ERR(mrec);
1392 goto undo_alloc;
1393 }
1394 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1395 if (unlikely(!ctx)) {
1396 ntfs_error(vol->sb, "Failed to get search context.");
1397 ret = -ENOMEM;
1398 goto undo_alloc;
1399 }
1400 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1401 mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
1402 0, ctx);
1403 if (unlikely(ret)) {
1404 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1405 "mft bitmap attribute.");
1406 if (ret == -ENOENT)
1407 ret = -EIO;
1408 goto undo_alloc;
1409 }
1410 a = ctx->attr;
1411 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
1412
1413 for (rl2 = rl; rl2 > mftbmp_ni->runlist.rl; rl2--) {
1414 if (ll >= rl2->vcn)
1415 break;
1416 }
1417 BUG_ON(ll < rl2->vcn);
1418 BUG_ON(ll >= rl2->vcn + rl2->length);
1419
1420 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1421 if (unlikely(mp_size <= 0)) {
1422 ntfs_error(vol->sb, "Get size for mapping pairs failed for "
1423 "mft bitmap attribute extent.");
1424 ret = mp_size;
1425 if (!ret)
1426 ret = -EIO;
1427 goto undo_alloc;
1428 }
1429
1430 old_alen = le32_to_cpu(a->length);
1431 ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
1432 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
1433 if (unlikely(ret)) {
1434 if (ret != -ENOSPC) {
1435 ntfs_error(vol->sb, "Failed to resize attribute "
1436 "record for mft bitmap attribute.");
1437 goto undo_alloc;
1438 }
1439
1440
1441
1442
1443
1444 ntfs_error(vol->sb, "Not enough space in this mft record to "
1445 "accomodate extended mft bitmap attribute "
1446 "extent. Cannot handle this yet.");
1447 ret = -EOPNOTSUPP;
1448 goto undo_alloc;
1449 }
1450 status.mp_rebuilt = 1;
1451
1452 ret = ntfs_mapping_pairs_build(vol, (u8*)a +
1453 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1454 mp_size, rl2, ll, -1, NULL);
1455 if (unlikely(ret)) {
1456 ntfs_error(vol->sb, "Failed to build mapping pairs array for "
1457 "mft bitmap attribute.");
1458 goto undo_alloc;
1459 }
1460
1461 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
1462
1463
1464
1465
1466 if (a->data.non_resident.lowest_vcn) {
1467
1468
1469
1470
1471 flush_dcache_mft_record_page(ctx->ntfs_ino);
1472 mark_mft_record_dirty(ctx->ntfs_ino);
1473 ntfs_attr_reinit_search_ctx(ctx);
1474 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1475 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL,
1476 0, ctx);
1477 if (unlikely(ret)) {
1478 ntfs_error(vol->sb, "Failed to find first attribute "
1479 "extent of mft bitmap attribute.");
1480 goto restore_undo_alloc;
1481 }
1482 a = ctx->attr;
1483 }
1484 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1485 mftbmp_ni->allocated_size += vol->cluster_size;
1486 a->data.non_resident.allocated_size =
1487 cpu_to_sle64(mftbmp_ni->allocated_size);
1488 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1489
1490 flush_dcache_mft_record_page(ctx->ntfs_ino);
1491 mark_mft_record_dirty(ctx->ntfs_ino);
1492 ntfs_attr_put_search_ctx(ctx);
1493 unmap_mft_record(mft_ni);
1494 up_write(&mftbmp_ni->runlist.lock);
1495 ntfs_debug("Done.");
1496 return 0;
1497restore_undo_alloc:
1498 ntfs_attr_reinit_search_ctx(ctx);
1499 if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1500 mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
1501 0, ctx)) {
1502 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1503 "mft bitmap attribute.%s", es);
1504 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1505 mftbmp_ni->allocated_size += vol->cluster_size;
1506 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1507 ntfs_attr_put_search_ctx(ctx);
1508 unmap_mft_record(mft_ni);
1509 up_write(&mftbmp_ni->runlist.lock);
1510
1511
1512
1513
1514 NVolSetErrors(vol);
1515 return ret;
1516 }
1517 a = ctx->attr;
1518 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 2);
1519undo_alloc:
1520 if (status.added_cluster) {
1521
1522 rl->length--;
1523 rl[1].vcn--;
1524 } else if (status.added_run) {
1525 lcn = rl->lcn;
1526
1527 rl->lcn = rl[1].lcn;
1528 rl->length = 0;
1529 }
1530
1531 down_write(&vol->lcnbmp_lock);
1532 if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) {
1533 ntfs_error(vol->sb, "Failed to free allocated cluster.%s", es);
1534 NVolSetErrors(vol);
1535 }
1536 up_write(&vol->lcnbmp_lock);
1537 if (status.mp_rebuilt) {
1538 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
1539 a->data.non_resident.mapping_pairs_offset),
1540 old_alen - le16_to_cpu(
1541 a->data.non_resident.mapping_pairs_offset),
1542 rl2, ll, -1, NULL)) {
1543 ntfs_error(vol->sb, "Failed to restore mapping pairs "
1544 "array.%s", es);
1545 NVolSetErrors(vol);
1546 }
1547 if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
1548 ntfs_error(vol->sb, "Failed to restore attribute "
1549 "record.%s", es);
1550 NVolSetErrors(vol);
1551 }
1552 flush_dcache_mft_record_page(ctx->ntfs_ino);
1553 mark_mft_record_dirty(ctx->ntfs_ino);
1554 }
1555 if (ctx)
1556 ntfs_attr_put_search_ctx(ctx);
1557 if (!IS_ERR(mrec))
1558 unmap_mft_record(mft_ni);
1559 up_write(&mftbmp_ni->runlist.lock);
1560 return ret;
1561}
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1578{
1579 s64 old_data_size, old_initialized_size;
1580 unsigned long flags;
1581 struct inode *mftbmp_vi;
1582 ntfs_inode *mft_ni, *mftbmp_ni;
1583 ntfs_attr_search_ctx *ctx;
1584 MFT_RECORD *mrec;
1585 ATTR_RECORD *a;
1586 int ret;
1587
1588 ntfs_debug("Extending mft bitmap initiailized (and data) size.");
1589 mft_ni = NTFS_I(vol->mft_ino);
1590 mftbmp_vi = vol->mftbmp_ino;
1591 mftbmp_ni = NTFS_I(mftbmp_vi);
1592
1593 mrec = map_mft_record(mft_ni);
1594 if (IS_ERR(mrec)) {
1595 ntfs_error(vol->sb, "Failed to map mft record.");
1596 return PTR_ERR(mrec);
1597 }
1598 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1599 if (unlikely(!ctx)) {
1600 ntfs_error(vol->sb, "Failed to get search context.");
1601 ret = -ENOMEM;
1602 goto unm_err_out;
1603 }
1604 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1605 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx);
1606 if (unlikely(ret)) {
1607 ntfs_error(vol->sb, "Failed to find first attribute extent of "
1608 "mft bitmap attribute.");
1609 if (ret == -ENOENT)
1610 ret = -EIO;
1611 goto put_err_out;
1612 }
1613 a = ctx->attr;
1614 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1615 old_data_size = i_size_read(mftbmp_vi);
1616 old_initialized_size = mftbmp_ni->initialized_size;
1617
1618
1619
1620
1621
1622 mftbmp_ni->initialized_size += 8;
1623 a->data.non_resident.initialized_size =
1624 cpu_to_sle64(mftbmp_ni->initialized_size);
1625 if (mftbmp_ni->initialized_size > old_data_size) {
1626 i_size_write(mftbmp_vi, mftbmp_ni->initialized_size);
1627 a->data.non_resident.data_size =
1628 cpu_to_sle64(mftbmp_ni->initialized_size);
1629 }
1630 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1631
1632 flush_dcache_mft_record_page(ctx->ntfs_ino);
1633 mark_mft_record_dirty(ctx->ntfs_ino);
1634 ntfs_attr_put_search_ctx(ctx);
1635 unmap_mft_record(mft_ni);
1636
1637 ret = ntfs_attr_set(mftbmp_ni, old_initialized_size, 8, 0);
1638 if (likely(!ret)) {
1639 ntfs_debug("Done. (Wrote eight initialized bytes to mft "
1640 "bitmap.");
1641 return 0;
1642 }
1643 ntfs_error(vol->sb, "Failed to write to mft bitmap.");
1644
1645 mrec = map_mft_record(mft_ni);
1646 if (IS_ERR(mrec)) {
1647 ntfs_error(vol->sb, "Failed to map mft record.%s", es);
1648 NVolSetErrors(vol);
1649 return ret;
1650 }
1651 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1652 if (unlikely(!ctx)) {
1653 ntfs_error(vol->sb, "Failed to get search context.%s", es);
1654 NVolSetErrors(vol);
1655 goto unm_err_out;
1656 }
1657 if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1658 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
1659 ntfs_error(vol->sb, "Failed to find first attribute extent of "
1660 "mft bitmap attribute.%s", es);
1661 NVolSetErrors(vol);
1662put_err_out:
1663 ntfs_attr_put_search_ctx(ctx);
1664unm_err_out:
1665 unmap_mft_record(mft_ni);
1666 goto err_out;
1667 }
1668 a = ctx->attr;
1669 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1670 mftbmp_ni->initialized_size = old_initialized_size;
1671 a->data.non_resident.initialized_size =
1672 cpu_to_sle64(old_initialized_size);
1673 if (i_size_read(mftbmp_vi) != old_data_size) {
1674 i_size_write(mftbmp_vi, old_data_size);
1675 a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
1676 }
1677 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1678 flush_dcache_mft_record_page(ctx->ntfs_ino);
1679 mark_mft_record_dirty(ctx->ntfs_ino);
1680 ntfs_attr_put_search_ctx(ctx);
1681 unmap_mft_record(mft_ni);
1682#ifdef DEBUG
1683 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1684 ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
1685 "data_size 0x%llx, initialized_size 0x%llx.",
1686 (long long)mftbmp_ni->allocated_size,
1687 (long long)i_size_read(mftbmp_vi),
1688 (long long)mftbmp_ni->initialized_size);
1689 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1690#endif
1691err_out:
1692 return ret;
1693}
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1715{
1716 LCN lcn;
1717 VCN old_last_vcn;
1718 s64 min_nr, nr, ll;
1719 unsigned long flags;
1720 ntfs_inode *mft_ni;
1721 runlist_element *rl, *rl2;
1722 ntfs_attr_search_ctx *ctx = NULL;
1723 MFT_RECORD *mrec;
1724 ATTR_RECORD *a = NULL;
1725 int ret, mp_size;
1726 u32 old_alen = 0;
1727 bool mp_rebuilt = false;
1728
1729 ntfs_debug("Extending mft data allocation.");
1730 mft_ni = NTFS_I(vol->mft_ino);
1731
1732
1733
1734
1735
1736 down_write(&mft_ni->runlist.lock);
1737 read_lock_irqsave(&mft_ni->size_lock, flags);
1738 ll = mft_ni->allocated_size;
1739 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1740 rl = ntfs_attr_find_vcn_nolock(mft_ni,
1741 (ll - 1) >> vol->cluster_size_bits, NULL);
1742 if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
1743 up_write(&mft_ni->runlist.lock);
1744 ntfs_error(vol->sb, "Failed to determine last allocated "
1745 "cluster of mft data attribute.");
1746 if (!IS_ERR(rl))
1747 ret = -EIO;
1748 else
1749 ret = PTR_ERR(rl);
1750 return ret;
1751 }
1752 lcn = rl->lcn + rl->length;
1753 ntfs_debug("Last lcn of mft data attribute is 0x%llx.", (long long)lcn);
1754
1755 min_nr = vol->mft_record_size >> vol->cluster_size_bits;
1756 if (!min_nr)
1757 min_nr = 1;
1758
1759 nr = vol->mft_record_size << 4 >> vol->cluster_size_bits;
1760 if (!nr)
1761 nr = min_nr;
1762
1763 read_lock_irqsave(&mft_ni->size_lock, flags);
1764 ll = mft_ni->allocated_size;
1765 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1766 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1767 vol->mft_record_size_bits >= (1ll << 32))) {
1768 nr = min_nr;
1769 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1770 vol->mft_record_size_bits >= (1ll << 32))) {
1771 ntfs_warning(vol->sb, "Cannot allocate mft record "
1772 "because the maximum number of inodes "
1773 "(2^32) has already been reached.");
1774 up_write(&mft_ni->runlist.lock);
1775 return -ENOSPC;
1776 }
1777 }
1778 ntfs_debug("Trying mft data allocation with %s cluster count %lli.",
1779 nr > min_nr ? "default" : "minimal", (long long)nr);
1780 old_last_vcn = rl[1].vcn;
1781 do {
1782 rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE,
1783 true);
1784 if (likely(!IS_ERR(rl2)))
1785 break;
1786 if (PTR_ERR(rl2) != -ENOSPC || nr == min_nr) {
1787 ntfs_error(vol->sb, "Failed to allocate the minimal "
1788 "number of clusters (%lli) for the "
1789 "mft data attribute.", (long long)nr);
1790 up_write(&mft_ni->runlist.lock);
1791 return PTR_ERR(rl2);
1792 }
1793
1794
1795
1796
1797
1798 nr = min_nr;
1799 ntfs_debug("Retrying mft data allocation with minimal cluster "
1800 "count %lli.", (long long)nr);
1801 } while (1);
1802 rl = ntfs_runlists_merge(mft_ni->runlist.rl, rl2);
1803 if (IS_ERR(rl)) {
1804 up_write(&mft_ni->runlist.lock);
1805 ntfs_error(vol->sb, "Failed to merge runlists for mft data "
1806 "attribute.");
1807 if (ntfs_cluster_free_from_rl(vol, rl2)) {
1808 ntfs_error(vol->sb, "Failed to dealocate clusters "
1809 "from the mft data attribute.%s", es);
1810 NVolSetErrors(vol);
1811 }
1812 ntfs_free(rl2);
1813 return PTR_ERR(rl);
1814 }
1815 mft_ni->runlist.rl = rl;
1816 ntfs_debug("Allocated %lli clusters.", (long long)nr);
1817
1818 for (; rl[1].length; rl++)
1819 ;
1820
1821 mrec = map_mft_record(mft_ni);
1822 if (IS_ERR(mrec)) {
1823 ntfs_error(vol->sb, "Failed to map mft record.");
1824 ret = PTR_ERR(mrec);
1825 goto undo_alloc;
1826 }
1827 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1828 if (unlikely(!ctx)) {
1829 ntfs_error(vol->sb, "Failed to get search context.");
1830 ret = -ENOMEM;
1831 goto undo_alloc;
1832 }
1833 ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
1834 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx);
1835 if (unlikely(ret)) {
1836 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1837 "mft data attribute.");
1838 if (ret == -ENOENT)
1839 ret = -EIO;
1840 goto undo_alloc;
1841 }
1842 a = ctx->attr;
1843 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
1844
1845 for (rl2 = rl; rl2 > mft_ni->runlist.rl; rl2--) {
1846 if (ll >= rl2->vcn)
1847 break;
1848 }
1849 BUG_ON(ll < rl2->vcn);
1850 BUG_ON(ll >= rl2->vcn + rl2->length);
1851
1852 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1853 if (unlikely(mp_size <= 0)) {
1854 ntfs_error(vol->sb, "Get size for mapping pairs failed for "
1855 "mft data attribute extent.");
1856 ret = mp_size;
1857 if (!ret)
1858 ret = -EIO;
1859 goto undo_alloc;
1860 }
1861
1862 old_alen = le32_to_cpu(a->length);
1863 ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
1864 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
1865 if (unlikely(ret)) {
1866 if (ret != -ENOSPC) {
1867 ntfs_error(vol->sb, "Failed to resize attribute "
1868 "record for mft data attribute.");
1869 goto undo_alloc;
1870 }
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881 ntfs_error(vol->sb, "Not enough space in this mft record to "
1882 "accomodate extended mft data attribute "
1883 "extent. Cannot handle this yet.");
1884 ret = -EOPNOTSUPP;
1885 goto undo_alloc;
1886 }
1887 mp_rebuilt = true;
1888
1889 ret = ntfs_mapping_pairs_build(vol, (u8*)a +
1890 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1891 mp_size, rl2, ll, -1, NULL);
1892 if (unlikely(ret)) {
1893 ntfs_error(vol->sb, "Failed to build mapping pairs array of "
1894 "mft data attribute.");
1895 goto undo_alloc;
1896 }
1897
1898 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
1899
1900
1901
1902
1903
1904
1905 if (a->data.non_resident.lowest_vcn) {
1906
1907
1908
1909
1910 flush_dcache_mft_record_page(ctx->ntfs_ino);
1911 mark_mft_record_dirty(ctx->ntfs_ino);
1912 ntfs_attr_reinit_search_ctx(ctx);
1913 ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name,
1914 mft_ni->name_len, CASE_SENSITIVE, 0, NULL, 0,
1915 ctx);
1916 if (unlikely(ret)) {
1917 ntfs_error(vol->sb, "Failed to find first attribute "
1918 "extent of mft data attribute.");
1919 goto restore_undo_alloc;
1920 }
1921 a = ctx->attr;
1922 }
1923 write_lock_irqsave(&mft_ni->size_lock, flags);
1924 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1925 a->data.non_resident.allocated_size =
1926 cpu_to_sle64(mft_ni->allocated_size);
1927 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1928
1929 flush_dcache_mft_record_page(ctx->ntfs_ino);
1930 mark_mft_record_dirty(ctx->ntfs_ino);
1931 ntfs_attr_put_search_ctx(ctx);
1932 unmap_mft_record(mft_ni);
1933 up_write(&mft_ni->runlist.lock);
1934 ntfs_debug("Done.");
1935 return 0;
1936restore_undo_alloc:
1937 ntfs_attr_reinit_search_ctx(ctx);
1938 if (ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
1939 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) {
1940 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1941 "mft data attribute.%s", es);
1942 write_lock_irqsave(&mft_ni->size_lock, flags);
1943 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1944 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1945 ntfs_attr_put_search_ctx(ctx);
1946 unmap_mft_record(mft_ni);
1947 up_write(&mft_ni->runlist.lock);
1948
1949
1950
1951
1952 NVolSetErrors(vol);
1953 return ret;
1954 }
1955 ctx->attr->data.non_resident.highest_vcn =
1956 cpu_to_sle64(old_last_vcn - 1);
1957undo_alloc:
1958 if (ntfs_cluster_free(mft_ni, old_last_vcn, -1, ctx) < 0) {
1959 ntfs_error(vol->sb, "Failed to free clusters from mft data "
1960 "attribute.%s", es);
1961 NVolSetErrors(vol);
1962 }
1963 a = ctx->attr;
1964 if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
1965 ntfs_error(vol->sb, "Failed to truncate mft data attribute "
1966 "runlist.%s", es);
1967 NVolSetErrors(vol);
1968 }
1969 if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
1970 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
1971 a->data.non_resident.mapping_pairs_offset),
1972 old_alen - le16_to_cpu(
1973 a->data.non_resident.mapping_pairs_offset),
1974 rl2, ll, -1, NULL)) {
1975 ntfs_error(vol->sb, "Failed to restore mapping pairs "
1976 "array.%s", es);
1977 NVolSetErrors(vol);
1978 }
1979 if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
1980 ntfs_error(vol->sb, "Failed to restore attribute "
1981 "record.%s", es);
1982 NVolSetErrors(vol);
1983 }
1984 flush_dcache_mft_record_page(ctx->ntfs_ino);
1985 mark_mft_record_dirty(ctx->ntfs_ino);
1986 } else if (IS_ERR(ctx->mrec)) {
1987 ntfs_error(vol->sb, "Failed to restore attribute search "
1988 "context.%s", es);
1989 NVolSetErrors(vol);
1990 }
1991 if (ctx)
1992 ntfs_attr_put_search_ctx(ctx);
1993 if (!IS_ERR(mrec))
1994 unmap_mft_record(mft_ni);
1995 up_write(&mft_ni->runlist.lock);
1996 return ret;
1997}
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
2013 MFT_RECORD *m)
2014{
2015 ATTR_RECORD *a;
2016
2017 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
2018 if (mft_no >= (1ll << 32)) {
2019 ntfs_error(vol->sb, "Mft record number 0x%llx exceeds "
2020 "maximum of 2^32.", (long long)mft_no);
2021 return -ERANGE;
2022 }
2023
2024 memset(m, 0, vol->mft_record_size);
2025
2026 if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver))
2027 m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1);
2028 else {
2029 m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1);
2030
2031
2032
2033
2034 m->reserved = 0;
2035 m->mft_record_number = cpu_to_le32((u32)mft_no);
2036 }
2037 m->magic = magic_FILE;
2038 if (vol->mft_record_size >= NTFS_BLOCK_SIZE)
2039 m->usa_count = cpu_to_le16(vol->mft_record_size /
2040 NTFS_BLOCK_SIZE + 1);
2041 else {
2042 m->usa_count = cpu_to_le16(1);
2043 ntfs_warning(vol->sb, "Sector size is bigger than mft record "
2044 "size. Setting usa_count to 1. If chkdsk "
2045 "reports this as corruption, please email "
2046 "linux-ntfs-dev@lists.sourceforge.net stating "
2047 "that you saw this message and that the "
2048 "modified filesystem created was corrupt. "
2049 "Thank you.");
2050 }
2051
2052 *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = cpu_to_le16(1);
2053 m->lsn = 0;
2054 m->sequence_number = cpu_to_le16(1);
2055 m->link_count = 0;
2056
2057
2058
2059
2060 m->attrs_offset = cpu_to_le16((le16_to_cpu(m->usa_ofs) +
2061 (le16_to_cpu(m->usa_count) << 1) + 7) & ~7);
2062 m->flags = 0;
2063
2064
2065
2066
2067
2068 m->bytes_in_use = cpu_to_le32(le16_to_cpu(m->attrs_offset) + 8);
2069 m->bytes_allocated = cpu_to_le32(vol->mft_record_size);
2070 m->base_mft_record = 0;
2071 m->next_attr_instance = 0;
2072
2073 a = (ATTR_RECORD*)((u8*)m + le16_to_cpu(m->attrs_offset));
2074 a->type = AT_END;
2075 a->length = 0;
2076 ntfs_debug("Done.");
2077 return 0;
2078}
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
2092{
2093 loff_t i_size;
2094 struct inode *mft_vi = vol->mft_ino;
2095 struct page *page;
2096 MFT_RECORD *m;
2097 pgoff_t index, end_index;
2098 unsigned int ofs;
2099 int err;
2100
2101 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
2102
2103
2104
2105
2106 index = mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
2107 ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
2108
2109 i_size = i_size_read(mft_vi);
2110 end_index = i_size >> PAGE_CACHE_SHIFT;
2111 if (unlikely(index >= end_index)) {
2112 if (unlikely(index > end_index || ofs + vol->mft_record_size >=
2113 (i_size & ~PAGE_CACHE_MASK))) {
2114 ntfs_error(vol->sb, "Tried to format non-existing mft "
2115 "record 0x%llx.", (long long)mft_no);
2116 return -ENOENT;
2117 }
2118 }
2119
2120 page = ntfs_map_page(mft_vi->i_mapping, index);
2121 if (IS_ERR(page)) {
2122 ntfs_error(vol->sb, "Failed to map page containing mft record "
2123 "to format 0x%llx.", (long long)mft_no);
2124 return PTR_ERR(page);
2125 }
2126 lock_page(page);
2127 BUG_ON(!PageUptodate(page));
2128 ClearPageUptodate(page);
2129 m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
2130 err = ntfs_mft_record_layout(vol, mft_no, m);
2131 if (unlikely(err)) {
2132 ntfs_error(vol->sb, "Failed to layout mft record 0x%llx.",
2133 (long long)mft_no);
2134 SetPageUptodate(page);
2135 unlock_page(page);
2136 ntfs_unmap_page(page);
2137 return err;
2138 }
2139 flush_dcache_page(page);
2140 SetPageUptodate(page);
2141 unlock_page(page);
2142
2143
2144
2145
2146
2147 mark_ntfs_record_dirty(page, ofs);
2148 ntfs_unmap_page(page);
2149 ntfs_debug("Done.");
2150 return 0;
2151}
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2244 ntfs_inode *base_ni, MFT_RECORD **mrec)
2245{
2246 s64 ll, bit, old_data_initialized, old_data_size;
2247 unsigned long flags;
2248 struct inode *vi;
2249 struct page *page;
2250 ntfs_inode *mft_ni, *mftbmp_ni, *ni;
2251 ntfs_attr_search_ctx *ctx;
2252 MFT_RECORD *m;
2253 ATTR_RECORD *a;
2254 pgoff_t index;
2255 unsigned int ofs;
2256 int err;
2257 le16 seq_no, usn;
2258 bool record_formatted = false;
2259
2260 if (base_ni) {
2261 ntfs_debug("Entering (allocating an extent mft record for "
2262 "base mft record 0x%llx).",
2263 (long long)base_ni->mft_no);
2264
2265 BUG_ON(mode);
2266 } else
2267 ntfs_debug("Entering (allocating a base mft record).");
2268 if (mode) {
2269
2270 BUG_ON(base_ni);
2271
2272 if (!S_ISREG(mode) && !S_ISDIR(mode))
2273 return ERR_PTR(-EOPNOTSUPP);
2274 }
2275 BUG_ON(!mrec);
2276 mft_ni = NTFS_I(vol->mft_ino);
2277 mftbmp_ni = NTFS_I(vol->mftbmp_ino);
2278 down_write(&vol->mftbmp_lock);
2279 bit = ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(vol, base_ni);
2280 if (bit >= 0) {
2281 ntfs_debug("Found and allocated free record (#1), bit 0x%llx.",
2282 (long long)bit);
2283 goto have_alloc_rec;
2284 }
2285 if (bit != -ENOSPC) {
2286 up_write(&vol->mftbmp_lock);
2287 return ERR_PTR(bit);
2288 }
2289
2290
2291
2292
2293
2294
2295
2296
2297 read_lock_irqsave(&mft_ni->size_lock, flags);
2298 ll = mft_ni->initialized_size >> vol->mft_record_size_bits;
2299 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2300 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2301 old_data_initialized = mftbmp_ni->initialized_size;
2302 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2303 if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
2304 bit = ll;
2305 if (bit < 24)
2306 bit = 24;
2307 if (unlikely(bit >= (1ll << 32)))
2308 goto max_err_out;
2309 ntfs_debug("Found free record (#2), bit 0x%llx.",
2310 (long long)bit);
2311 goto found_free_rec;
2312 }
2313
2314
2315
2316
2317
2318 bit = old_data_initialized << 3;
2319 if (unlikely(bit >= (1ll << 32)))
2320 goto max_err_out;
2321 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2322 old_data_size = mftbmp_ni->allocated_size;
2323 ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
2324 "data_size 0x%llx, initialized_size 0x%llx.",
2325 (long long)old_data_size,
2326 (long long)i_size_read(vol->mftbmp_ino),
2327 (long long)old_data_initialized);
2328 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2329 if (old_data_initialized + 8 > old_data_size) {
2330
2331 ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
2332 err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
2333 if (unlikely(err)) {
2334 up_write(&vol->mftbmp_lock);
2335 goto err_out;
2336 }
2337#ifdef DEBUG
2338 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2339 ntfs_debug("Status of mftbmp after allocation extension: "
2340 "allocated_size 0x%llx, data_size 0x%llx, "
2341 "initialized_size 0x%llx.",
2342 (long long)mftbmp_ni->allocated_size,
2343 (long long)i_size_read(vol->mftbmp_ino),
2344 (long long)mftbmp_ni->initialized_size);
2345 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2346#endif
2347 }
2348
2349
2350
2351
2352
2353 err = ntfs_mft_bitmap_extend_initialized_nolock(vol);
2354 if (unlikely(err)) {
2355 up_write(&vol->mftbmp_lock);
2356 goto err_out;
2357 }
2358#ifdef DEBUG
2359 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2360 ntfs_debug("Status of mftbmp after initialized extention: "
2361 "allocated_size 0x%llx, data_size 0x%llx, "
2362 "initialized_size 0x%llx.",
2363 (long long)mftbmp_ni->allocated_size,
2364 (long long)i_size_read(vol->mftbmp_ino),
2365 (long long)mftbmp_ni->initialized_size);
2366 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2367#endif
2368 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit);
2369found_free_rec:
2370
2371 ntfs_debug("At found_free_rec.");
2372 err = ntfs_bitmap_set_bit(vol->mftbmp_ino, bit);
2373 if (unlikely(err)) {
2374 ntfs_error(vol->sb, "Failed to allocate bit in mft bitmap.");
2375 up_write(&vol->mftbmp_lock);
2376 goto err_out;
2377 }
2378 ntfs_debug("Set bit 0x%llx in mft bitmap.", (long long)bit);
2379have_alloc_rec:
2380
2381
2382
2383
2384
2385
2386
2387
2388 ll = (bit + 1) << vol->mft_record_size_bits;
2389 read_lock_irqsave(&mft_ni->size_lock, flags);
2390 old_data_initialized = mft_ni->initialized_size;
2391 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2392 if (ll <= old_data_initialized) {
2393 ntfs_debug("Allocated mft record already initialized.");
2394 goto mft_rec_already_initialized;
2395 }
2396 ntfs_debug("Initializing allocated mft record.");
2397
2398
2399
2400
2401
2402
2403 read_lock_irqsave(&mft_ni->size_lock, flags);
2404 ntfs_debug("Status of mft data before extension: "
2405 "allocated_size 0x%llx, data_size 0x%llx, "
2406 "initialized_size 0x%llx.",
2407 (long long)mft_ni->allocated_size,
2408 (long long)i_size_read(vol->mft_ino),
2409 (long long)mft_ni->initialized_size);
2410 while (ll > mft_ni->allocated_size) {
2411 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2412 err = ntfs_mft_data_extend_allocation_nolock(vol);
2413 if (unlikely(err)) {
2414 ntfs_error(vol->sb, "Failed to extend mft data "
2415 "allocation.");
2416 goto undo_mftbmp_alloc_nolock;
2417 }
2418 read_lock_irqsave(&mft_ni->size_lock, flags);
2419 ntfs_debug("Status of mft data after allocation extension: "
2420 "allocated_size 0x%llx, data_size 0x%llx, "
2421 "initialized_size 0x%llx.",
2422 (long long)mft_ni->allocated_size,
2423 (long long)i_size_read(vol->mft_ino),
2424 (long long)mft_ni->initialized_size);
2425 }
2426 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2427
2428
2429
2430
2431
2432
2433
2434 write_lock_irqsave(&mft_ni->size_lock, flags);
2435 old_data_initialized = mft_ni->initialized_size;
2436 old_data_size = vol->mft_ino->i_size;
2437 while (ll > mft_ni->initialized_size) {
2438 s64 new_initialized_size, mft_no;
2439
2440 new_initialized_size = mft_ni->initialized_size +
2441 vol->mft_record_size;
2442 mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits;
2443 if (new_initialized_size > i_size_read(vol->mft_ino))
2444 i_size_write(vol->mft_ino, new_initialized_size);
2445 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2446 ntfs_debug("Initializing mft record 0x%llx.",
2447 (long long)mft_no);
2448 err = ntfs_mft_record_format(vol, mft_no);
2449 if (unlikely(err)) {
2450 ntfs_error(vol->sb, "Failed to format mft record.");
2451 goto undo_data_init;
2452 }
2453 write_lock_irqsave(&mft_ni->size_lock, flags);
2454 mft_ni->initialized_size = new_initialized_size;
2455 }
2456 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2457 record_formatted = true;
2458
2459 m = map_mft_record(mft_ni);
2460 if (IS_ERR(m)) {
2461 ntfs_error(vol->sb, "Failed to map mft record.");
2462 err = PTR_ERR(m);
2463 goto undo_data_init;
2464 }
2465 ctx = ntfs_attr_get_search_ctx(mft_ni, m);
2466 if (unlikely(!ctx)) {
2467 ntfs_error(vol->sb, "Failed to get search context.");
2468 err = -ENOMEM;
2469 unmap_mft_record(mft_ni);
2470 goto undo_data_init;
2471 }
2472 err = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
2473 CASE_SENSITIVE, 0, NULL, 0, ctx);
2474 if (unlikely(err)) {
2475 ntfs_error(vol->sb, "Failed to find first attribute extent of "
2476 "mft data attribute.");
2477 ntfs_attr_put_search_ctx(ctx);
2478 unmap_mft_record(mft_ni);
2479 goto undo_data_init;
2480 }
2481 a = ctx->attr;
2482 read_lock_irqsave(&mft_ni->size_lock, flags);
2483 a->data.non_resident.initialized_size =
2484 cpu_to_sle64(mft_ni->initialized_size);
2485 a->data.non_resident.data_size =
2486 cpu_to_sle64(i_size_read(vol->mft_ino));
2487 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2488
2489 flush_dcache_mft_record_page(ctx->ntfs_ino);
2490 mark_mft_record_dirty(ctx->ntfs_ino);
2491 ntfs_attr_put_search_ctx(ctx);
2492 unmap_mft_record(mft_ni);
2493 read_lock_irqsave(&mft_ni->size_lock, flags);
2494 ntfs_debug("Status of mft data after mft record initialization: "
2495 "allocated_size 0x%llx, data_size 0x%llx, "
2496 "initialized_size 0x%llx.",
2497 (long long)mft_ni->allocated_size,
2498 (long long)i_size_read(vol->mft_ino),
2499 (long long)mft_ni->initialized_size);
2500 BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size);
2501 BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino));
2502 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2503mft_rec_already_initialized:
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513 up_write(&vol->mftbmp_lock);
2514
2515
2516
2517
2518 index = bit << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
2519 ofs = (bit << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
2520
2521 page = ntfs_map_page(vol->mft_ino->i_mapping, index);
2522 if (IS_ERR(page)) {
2523 ntfs_error(vol->sb, "Failed to map page containing allocated "
2524 "mft record 0x%llx.", (long long)bit);
2525 err = PTR_ERR(page);
2526 goto undo_mftbmp_alloc;
2527 }
2528 lock_page(page);
2529 BUG_ON(!PageUptodate(page));
2530 ClearPageUptodate(page);
2531 m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
2532
2533 if (!record_formatted) {
2534
2535 if (ntfs_is_file_record(m->magic) &&
2536 (m->flags & MFT_RECORD_IN_USE)) {
2537 ntfs_error(vol->sb, "Mft record 0x%llx was marked "
2538 "free in mft bitmap but is marked "
2539 "used itself. Corrupt filesystem. "
2540 "Unmount and run chkdsk.",
2541 (long long)bit);
2542 err = -EIO;
2543 SetPageUptodate(page);
2544 unlock_page(page);
2545 ntfs_unmap_page(page);
2546 NVolSetErrors(vol);
2547 goto undo_mftbmp_alloc;
2548 }
2549
2550
2551
2552
2553
2554
2555
2556 seq_no = m->sequence_number;
2557 usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs));
2558 err = ntfs_mft_record_layout(vol, bit, m);
2559 if (unlikely(err)) {
2560 ntfs_error(vol->sb, "Failed to layout allocated mft "
2561 "record 0x%llx.", (long long)bit);
2562 SetPageUptodate(page);
2563 unlock_page(page);
2564 ntfs_unmap_page(page);
2565 goto undo_mftbmp_alloc;
2566 }
2567 if (seq_no)
2568 m->sequence_number = seq_no;
2569 if (usn && le16_to_cpu(usn) != 0xffff)
2570 *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn;
2571 }
2572
2573 m->flags |= MFT_RECORD_IN_USE;
2574 if (S_ISDIR(mode))
2575 m->flags |= MFT_RECORD_IS_DIRECTORY;
2576 flush_dcache_page(page);
2577 SetPageUptodate(page);
2578 if (base_ni) {
2579
2580
2581
2582
2583
2584 m->base_mft_record = MK_LE_MREF(base_ni->mft_no,
2585 base_ni->seq_no);
2586
2587
2588
2589
2590
2591 m = map_extent_mft_record(base_ni, bit, &ni);
2592 if (IS_ERR(m)) {
2593 ntfs_error(vol->sb, "Failed to map allocated extent "
2594 "mft record 0x%llx.", (long long)bit);
2595 err = PTR_ERR(m);
2596
2597 m->flags &= cpu_to_le16(
2598 ~le16_to_cpu(MFT_RECORD_IN_USE));
2599 flush_dcache_page(page);
2600
2601 mark_ntfs_record_dirty(page, ofs);
2602 unlock_page(page);
2603 ntfs_unmap_page(page);
2604 goto undo_mftbmp_alloc;
2605 }
2606
2607
2608
2609
2610
2611
2612
2613 mark_ntfs_record_dirty(page, ofs);
2614 unlock_page(page);
2615
2616
2617
2618
2619 ntfs_unmap_page(page);
2620 } else {
2621
2622
2623
2624
2625
2626 vi = new_inode(vol->sb);
2627 if (unlikely(!vi)) {
2628 err = -ENOMEM;
2629
2630 m->flags &= cpu_to_le16(
2631 ~le16_to_cpu(MFT_RECORD_IN_USE));
2632 flush_dcache_page(page);
2633
2634 mark_ntfs_record_dirty(page, ofs);
2635 unlock_page(page);
2636 ntfs_unmap_page(page);
2637 goto undo_mftbmp_alloc;
2638 }
2639 vi->i_ino = bit;
2640
2641
2642
2643
2644
2645 vi->i_version = 1;
2646
2647
2648 vi->i_uid = vol->uid;
2649 vi->i_gid = vol->gid;
2650
2651
2652 ntfs_init_big_inode(vi);
2653 ni = NTFS_I(vi);
2654
2655
2656
2657
2658 if (S_ISDIR(mode)) {
2659 vi->i_mode = S_IFDIR | S_IRWXUGO;
2660 vi->i_mode &= ~vol->dmask;
2661
2662 NInoSetMstProtected(ni);
2663 ni->type = AT_INDEX_ALLOCATION;
2664 ni->name = I30;
2665 ni->name_len = 4;
2666
2667 ni->itype.index.block_size = 4096;
2668 ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1;
2669 ni->itype.index.collation_rule = COLLATION_FILE_NAME;
2670 if (vol->cluster_size <= ni->itype.index.block_size) {
2671 ni->itype.index.vcn_size = vol->cluster_size;
2672 ni->itype.index.vcn_size_bits =
2673 vol->cluster_size_bits;
2674 } else {
2675 ni->itype.index.vcn_size = vol->sector_size;
2676 ni->itype.index.vcn_size_bits =
2677 vol->sector_size_bits;
2678 }
2679 } else {
2680 vi->i_mode = S_IFREG | S_IRWXUGO;
2681 vi->i_mode &= ~vol->fmask;
2682
2683 ni->type = AT_DATA;
2684 ni->name = NULL;
2685 ni->name_len = 0;
2686 }
2687 if (IS_RDONLY(vi))
2688 vi->i_mode &= ~S_IWUGO;
2689
2690
2691 vi->i_atime = vi->i_mtime = vi->i_ctime =
2692 current_fs_time(vi->i_sb);
2693
2694
2695
2696
2697 vi->i_size = 0;
2698 vi->i_blocks = 0;
2699
2700
2701 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
2702
2703
2704
2705
2706 atomic_inc(&ni->count);
2707 mutex_lock(&ni->mrec_lock);
2708 ni->page = page;
2709 ni->page_ofs = ofs;
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720 mark_ntfs_record_dirty(page, ofs);
2721 unlock_page(page);
2722
2723
2724 insert_inode_hash(vi);
2725
2726
2727 vol->mft_data_pos = bit + 1;
2728 }
2729
2730
2731
2732
2733 ntfs_debug("Returning opened, allocated %sinode 0x%llx.",
2734 base_ni ? "extent " : "", (long long)bit);
2735 *mrec = m;
2736 return ni;
2737undo_data_init:
2738 write_lock_irqsave(&mft_ni->size_lock, flags);
2739 mft_ni->initialized_size = old_data_initialized;
2740 i_size_write(vol->mft_ino, old_data_size);
2741 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2742 goto undo_mftbmp_alloc_nolock;
2743undo_mftbmp_alloc:
2744 down_write(&vol->mftbmp_lock);
2745undo_mftbmp_alloc_nolock:
2746 if (ntfs_bitmap_clear_bit(vol->mftbmp_ino, bit)) {
2747 ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
2748 NVolSetErrors(vol);
2749 }
2750 up_write(&vol->mftbmp_lock);
2751err_out:
2752 return ERR_PTR(err);
2753max_err_out:
2754 ntfs_warning(vol->sb, "Cannot allocate mft record because the maximum "
2755 "number of inodes (2^32) has already been reached.");
2756 up_write(&vol->mftbmp_lock);
2757 return ERR_PTR(-ENOSPC);
2758}
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781int ntfs_extent_mft_record_free(ntfs_inode *ni, MFT_RECORD *m)
2782{
2783 unsigned long mft_no = ni->mft_no;
2784 ntfs_volume *vol = ni->vol;
2785 ntfs_inode *base_ni;
2786 ntfs_inode **extent_nis;
2787 int i, err;
2788 le16 old_seq_no;
2789 u16 seq_no;
2790
2791 BUG_ON(NInoAttr(ni));
2792 BUG_ON(ni->nr_extents != -1);
2793
2794 mutex_lock(&ni->extent_lock);
2795 base_ni = ni->ext.base_ntfs_ino;
2796 mutex_unlock(&ni->extent_lock);
2797
2798 BUG_ON(base_ni->nr_extents <= 0);
2799
2800 ntfs_debug("Entering for extent inode 0x%lx, base inode 0x%lx.\n",
2801 mft_no, base_ni->mft_no);
2802
2803 mutex_lock(&base_ni->extent_lock);
2804
2805
2806 if (atomic_read(&ni->count) > 2) {
2807 ntfs_error(vol->sb, "Tried to free busy extent inode 0x%lx, "
2808 "not freeing.", base_ni->mft_no);
2809 mutex_unlock(&base_ni->extent_lock);
2810 return -EBUSY;
2811 }
2812
2813
2814 extent_nis = base_ni->ext.extent_ntfs_inos;
2815 err = -ENOENT;
2816 for (i = 0; i < base_ni->nr_extents; i++) {
2817 if (ni != extent_nis[i])
2818 continue;
2819 extent_nis += i;
2820 base_ni->nr_extents--;
2821 memmove(extent_nis, extent_nis + 1, (base_ni->nr_extents - i) *
2822 sizeof(ntfs_inode*));
2823 err = 0;
2824 break;
2825 }
2826
2827 mutex_unlock(&base_ni->extent_lock);
2828
2829 if (unlikely(err)) {
2830 ntfs_error(vol->sb, "Extent inode 0x%lx is not attached to "
2831 "its base inode 0x%lx.", mft_no,
2832 base_ni->mft_no);
2833 BUG();
2834 }
2835
2836
2837
2838
2839
2840
2841
2842 m->flags &= const_cpu_to_le16(~const_le16_to_cpu(MFT_RECORD_IN_USE));
2843
2844
2845 old_seq_no = m->sequence_number;
2846 seq_no = le16_to_cpu(old_seq_no);
2847 if (seq_no == 0xffff)
2848 seq_no = 1;
2849 else if (seq_no)
2850 seq_no++;
2851 m->sequence_number = cpu_to_le16(seq_no);
2852
2853
2854
2855
2856
2857
2858 NInoSetDirty(ni);
2859 err = write_mft_record(ni, m, 0);
2860 if (unlikely(err)) {
2861 ntfs_error(vol->sb, "Failed to write mft record 0x%lx, not "
2862 "freeing.", mft_no);
2863 goto rollback;
2864 }
2865rollback_error:
2866
2867 unmap_extent_mft_record(ni);
2868 ntfs_clear_extent_inode(ni);
2869
2870
2871 down_write(&vol->mftbmp_lock);
2872 err = ntfs_bitmap_clear_bit(vol->mftbmp_ino, mft_no);
2873 up_write(&vol->mftbmp_lock);
2874 if (unlikely(err)) {
2875
2876
2877
2878
2879
2880 ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
2881 NVolSetErrors(vol);
2882 }
2883 return 0;
2884rollback:
2885
2886 mutex_lock(&base_ni->extent_lock);
2887 extent_nis = base_ni->ext.extent_ntfs_inos;
2888 if (!(base_ni->nr_extents & 3)) {
2889 int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode*);
2890
2891 extent_nis = kmalloc(new_size, GFP_NOFS);
2892 if (unlikely(!extent_nis)) {
2893 ntfs_error(vol->sb, "Failed to allocate internal "
2894 "buffer during rollback.%s", es);
2895 mutex_unlock(&base_ni->extent_lock);
2896 NVolSetErrors(vol);
2897 goto rollback_error;
2898 }
2899 if (base_ni->nr_extents) {
2900 BUG_ON(!base_ni->ext.extent_ntfs_inos);
2901 memcpy(extent_nis, base_ni->ext.extent_ntfs_inos,
2902 new_size - 4 * sizeof(ntfs_inode*));
2903 kfree(base_ni->ext.extent_ntfs_inos);
2904 }
2905 base_ni->ext.extent_ntfs_inos = extent_nis;
2906 }
2907 m->flags |= MFT_RECORD_IN_USE;
2908 m->sequence_number = old_seq_no;
2909 extent_nis[base_ni->nr_extents++] = ni;
2910 mutex_unlock(&base_ni->extent_lock);
2911 mark_mft_record_dirty(ni);
2912 return err;
2913}
2914#endif