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