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/errno.h>
24#include <linux/fs.h>
25#include <linux/gfp.h>
26#include <linux/mm.h>
27#include <linux/pagemap.h>
28#include <linux/swap.h>
29#include <linux/buffer_head.h>
30#include <linux/writeback.h>
31#include <linux/bit_spinlock.h>
32
33#include "aops.h"
34#include "attrib.h"
35#include "debug.h"
36#include "inode.h"
37#include "mft.h"
38#include "runlist.h"
39#include "types.h"
40#include "ntfs.h"
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
59{
60 unsigned long flags;
61 struct buffer_head *first, *tmp;
62 struct page *page;
63 struct inode *vi;
64 ntfs_inode *ni;
65 int page_uptodate = 1;
66
67 page = bh->b_page;
68 vi = page->mapping->host;
69 ni = NTFS_I(vi);
70
71 if (likely(uptodate)) {
72 loff_t i_size;
73 s64 file_ofs, init_size;
74
75 set_buffer_uptodate(bh);
76
77 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
78 bh_offset(bh);
79 read_lock_irqsave(&ni->size_lock, flags);
80 init_size = ni->initialized_size;
81 i_size = i_size_read(vi);
82 read_unlock_irqrestore(&ni->size_lock, flags);
83 if (unlikely(init_size > i_size)) {
84
85 init_size = i_size;
86 }
87
88 if (unlikely(file_ofs + bh->b_size > init_size)) {
89 int ofs;
90 void *kaddr;
91
92 ofs = 0;
93 if (file_ofs < init_size)
94 ofs = init_size - file_ofs;
95 local_irq_save(flags);
96 kaddr = kmap_atomic(page);
97 memset(kaddr + bh_offset(bh) + ofs, 0,
98 bh->b_size - ofs);
99 flush_dcache_page(page);
100 kunmap_atomic(kaddr);
101 local_irq_restore(flags);
102 }
103 } else {
104 clear_buffer_uptodate(bh);
105 SetPageError(page);
106 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
107 "0x%llx.", (unsigned long long)bh->b_blocknr);
108 }
109 first = page_buffers(page);
110 local_irq_save(flags);
111 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
112 clear_buffer_async_read(bh);
113 unlock_buffer(bh);
114 tmp = bh;
115 do {
116 if (!buffer_uptodate(tmp))
117 page_uptodate = 0;
118 if (buffer_async_read(tmp)) {
119 if (likely(buffer_locked(tmp)))
120 goto still_busy;
121
122 BUG();
123 }
124 tmp = tmp->b_this_page;
125 } while (tmp != bh);
126 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
127 local_irq_restore(flags);
128
129
130
131
132
133
134
135
136 if (!NInoMstProtected(ni)) {
137 if (likely(page_uptodate && !PageError(page)))
138 SetPageUptodate(page);
139 } else {
140 u8 *kaddr;
141 unsigned int i, recs;
142 u32 rec_size;
143
144 rec_size = ni->itype.index.block_size;
145 recs = PAGE_CACHE_SIZE / rec_size;
146
147 BUG_ON(!recs);
148 local_irq_save(flags);
149 kaddr = kmap_atomic(page);
150 for (i = 0; i < recs; i++)
151 post_read_mst_fixup((NTFS_RECORD*)(kaddr +
152 i * rec_size), rec_size);
153 kunmap_atomic(kaddr);
154 local_irq_restore(flags);
155 flush_dcache_page(page);
156 if (likely(page_uptodate && !PageError(page)))
157 SetPageUptodate(page);
158 }
159 unlock_page(page);
160 return;
161still_busy:
162 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
163 local_irq_restore(flags);
164 return;
165}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184static int ntfs_read_block(struct page *page)
185{
186 loff_t i_size;
187 VCN vcn;
188 LCN lcn;
189 s64 init_size;
190 struct inode *vi;
191 ntfs_inode *ni;
192 ntfs_volume *vol;
193 runlist_element *rl;
194 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
195 sector_t iblock, lblock, zblock;
196 unsigned long flags;
197 unsigned int blocksize, vcn_ofs;
198 int i, nr;
199 unsigned char blocksize_bits;
200
201 vi = page->mapping->host;
202 ni = NTFS_I(vi);
203 vol = ni->vol;
204
205
206 BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
207
208 blocksize = vol->sb->s_blocksize;
209 blocksize_bits = vol->sb->s_blocksize_bits;
210
211 if (!page_has_buffers(page)) {
212 create_empty_buffers(page, blocksize, 0);
213 if (unlikely(!page_has_buffers(page))) {
214 unlock_page(page);
215 return -ENOMEM;
216 }
217 }
218 bh = head = page_buffers(page);
219 BUG_ON(!bh);
220
221
222
223
224
225
226
227
228
229
230
231
232 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
233 read_lock_irqsave(&ni->size_lock, flags);
234 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
235 init_size = ni->initialized_size;
236 i_size = i_size_read(vi);
237 read_unlock_irqrestore(&ni->size_lock, flags);
238 if (unlikely(init_size > i_size)) {
239
240 init_size = i_size;
241 }
242 zblock = (init_size + blocksize - 1) >> blocksize_bits;
243
244
245 rl = NULL;
246 nr = i = 0;
247 do {
248 int err = 0;
249
250 if (unlikely(buffer_uptodate(bh)))
251 continue;
252 if (unlikely(buffer_mapped(bh))) {
253 arr[nr++] = bh;
254 continue;
255 }
256 bh->b_bdev = vol->sb->s_bdev;
257
258 if (iblock < lblock) {
259 bool is_retry = false;
260
261
262 vcn = (VCN)iblock << blocksize_bits >>
263 vol->cluster_size_bits;
264 vcn_ofs = ((VCN)iblock << blocksize_bits) &
265 vol->cluster_size_mask;
266 if (!rl) {
267lock_retry_remap:
268 down_read(&ni->runlist.lock);
269 rl = ni->runlist.rl;
270 }
271 if (likely(rl != NULL)) {
272
273 while (rl->length && rl[1].vcn <= vcn)
274 rl++;
275 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
276 } else
277 lcn = LCN_RL_NOT_MAPPED;
278
279 if (lcn >= 0) {
280
281 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
282 + vcn_ofs) >> blocksize_bits;
283 set_buffer_mapped(bh);
284
285 if (iblock < zblock) {
286 arr[nr++] = bh;
287 continue;
288 }
289
290 goto handle_zblock;
291 }
292
293 if (lcn == LCN_HOLE)
294 goto handle_hole;
295
296 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
297 is_retry = true;
298
299
300
301
302 up_read(&ni->runlist.lock);
303 err = ntfs_map_runlist(ni, vcn);
304 if (likely(!err))
305 goto lock_retry_remap;
306 rl = NULL;
307 } else if (!rl)
308 up_read(&ni->runlist.lock);
309
310
311
312
313
314 if (err == -ENOENT || lcn == LCN_ENOENT) {
315 err = 0;
316 goto handle_hole;
317 }
318
319 if (!err)
320 err = -EIO;
321 bh->b_blocknr = -1;
322 SetPageError(page);
323 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
324 "attribute type 0x%x, vcn 0x%llx, "
325 "offset 0x%x because its location on "
326 "disk could not be determined%s "
327 "(error code %i).", ni->mft_no,
328 ni->type, (unsigned long long)vcn,
329 vcn_ofs, is_retry ? " even after "
330 "retrying" : "", err);
331 }
332
333
334
335
336
337handle_hole:
338 bh->b_blocknr = -1UL;
339 clear_buffer_mapped(bh);
340handle_zblock:
341 zero_user(page, i * blocksize, blocksize);
342 if (likely(!err))
343 set_buffer_uptodate(bh);
344 } while (i++, iblock++, (bh = bh->b_this_page) != head);
345
346
347 if (rl)
348 up_read(&ni->runlist.lock);
349
350
351 if (nr) {
352 struct buffer_head *tbh;
353
354
355 for (i = 0; i < nr; i++) {
356 tbh = arr[i];
357 lock_buffer(tbh);
358 tbh->b_end_io = ntfs_end_buffer_async_read;
359 set_buffer_async_read(tbh);
360 }
361
362 for (i = 0; i < nr; i++) {
363 tbh = arr[i];
364 if (likely(!buffer_uptodate(tbh)))
365 submit_bh(READ, tbh);
366 else
367 ntfs_end_buffer_async_read(tbh, 1);
368 }
369 return 0;
370 }
371
372 if (likely(!PageError(page)))
373 SetPageUptodate(page);
374 else
375 nr = -EIO;
376 unlock_page(page);
377 return nr;
378}
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398static int ntfs_readpage(struct file *file, struct page *page)
399{
400 loff_t i_size;
401 struct inode *vi;
402 ntfs_inode *ni, *base_ni;
403 u8 *addr;
404 ntfs_attr_search_ctx *ctx;
405 MFT_RECORD *mrec;
406 unsigned long flags;
407 u32 attr_len;
408 int err = 0;
409
410retry_readpage:
411 BUG_ON(!PageLocked(page));
412 vi = page->mapping->host;
413 i_size = i_size_read(vi);
414
415 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
416 PAGE_CACHE_SHIFT)) {
417 zero_user(page, 0, PAGE_CACHE_SIZE);
418 ntfs_debug("Read outside i_size - truncated?");
419 goto done;
420 }
421
422
423
424
425 if (PageUptodate(page)) {
426 unlock_page(page);
427 return 0;
428 }
429 ni = NTFS_I(vi);
430
431
432
433
434
435
436
437
438 if (ni->type != AT_INDEX_ALLOCATION) {
439
440 if (NInoEncrypted(ni)) {
441 BUG_ON(ni->type != AT_DATA);
442 err = -EACCES;
443 goto err_out;
444 }
445
446 if (NInoNonResident(ni) && NInoCompressed(ni)) {
447 BUG_ON(ni->type != AT_DATA);
448 BUG_ON(ni->name_len);
449 return ntfs_read_compressed_block(page);
450 }
451 }
452
453 if (NInoNonResident(ni)) {
454
455 return ntfs_read_block(page);
456 }
457
458
459
460
461
462
463
464
465 if (unlikely(page->index > 0)) {
466 zero_user(page, 0, PAGE_CACHE_SIZE);
467 goto done;
468 }
469 if (!NInoAttr(ni))
470 base_ni = ni;
471 else
472 base_ni = ni->ext.base_ntfs_ino;
473
474 mrec = map_mft_record(base_ni);
475 if (IS_ERR(mrec)) {
476 err = PTR_ERR(mrec);
477 goto err_out;
478 }
479
480
481
482
483 if (unlikely(NInoNonResident(ni))) {
484 unmap_mft_record(base_ni);
485 goto retry_readpage;
486 }
487 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
488 if (unlikely(!ctx)) {
489 err = -ENOMEM;
490 goto unm_err_out;
491 }
492 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
493 CASE_SENSITIVE, 0, NULL, 0, ctx);
494 if (unlikely(err))
495 goto put_unm_err_out;
496 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
497 read_lock_irqsave(&ni->size_lock, flags);
498 if (unlikely(attr_len > ni->initialized_size))
499 attr_len = ni->initialized_size;
500 i_size = i_size_read(vi);
501 read_unlock_irqrestore(&ni->size_lock, flags);
502 if (unlikely(attr_len > i_size)) {
503
504 attr_len = i_size;
505 }
506 addr = kmap_atomic(page);
507
508 memcpy(addr, (u8*)ctx->attr +
509 le16_to_cpu(ctx->attr->data.resident.value_offset),
510 attr_len);
511
512 memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
513 flush_dcache_page(page);
514 kunmap_atomic(addr);
515put_unm_err_out:
516 ntfs_attr_put_search_ctx(ctx);
517unm_err_out:
518 unmap_mft_record(base_ni);
519done:
520 SetPageUptodate(page);
521err_out:
522 unlock_page(page);
523 return err;
524}
525
526#ifdef NTFS_RW
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
551{
552 VCN vcn;
553 LCN lcn;
554 s64 initialized_size;
555 loff_t i_size;
556 sector_t block, dblock, iblock;
557 struct inode *vi;
558 ntfs_inode *ni;
559 ntfs_volume *vol;
560 runlist_element *rl;
561 struct buffer_head *bh, *head;
562 unsigned long flags;
563 unsigned int blocksize, vcn_ofs;
564 int err;
565 bool need_end_writeback;
566 unsigned char blocksize_bits;
567
568 vi = page->mapping->host;
569 ni = NTFS_I(vi);
570 vol = ni->vol;
571
572 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
573 "0x%lx.", ni->mft_no, ni->type, page->index);
574
575 BUG_ON(!NInoNonResident(ni));
576 BUG_ON(NInoMstProtected(ni));
577 blocksize = vol->sb->s_blocksize;
578 blocksize_bits = vol->sb->s_blocksize_bits;
579 if (!page_has_buffers(page)) {
580 BUG_ON(!PageUptodate(page));
581 create_empty_buffers(page, blocksize,
582 (1 << BH_Uptodate) | (1 << BH_Dirty));
583 if (unlikely(!page_has_buffers(page))) {
584 ntfs_warning(vol->sb, "Error allocating page "
585 "buffers. Redirtying page so we try "
586 "again later.");
587
588
589
590
591 redirty_page_for_writepage(wbc, page);
592 unlock_page(page);
593 return 0;
594 }
595 }
596 bh = head = page_buffers(page);
597 BUG_ON(!bh);
598
599
600
601
602 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
603
604 read_lock_irqsave(&ni->size_lock, flags);
605 i_size = i_size_read(vi);
606 initialized_size = ni->initialized_size;
607 read_unlock_irqrestore(&ni->size_lock, flags);
608
609
610 dblock = (i_size + blocksize - 1) >> blocksize_bits;
611
612
613 iblock = initialized_size >> blocksize_bits;
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630 rl = NULL;
631 err = 0;
632 do {
633 bool is_retry = false;
634
635 if (unlikely(block >= dblock)) {
636
637
638
639
640
641
642
643
644
645
646
647 clear_buffer_dirty(bh);
648 set_buffer_uptodate(bh);
649 continue;
650 }
651
652
653 if (!buffer_dirty(bh))
654 continue;
655
656
657 if (unlikely((block >= iblock) &&
658 (initialized_size < i_size))) {
659
660
661
662
663
664
665 if (block > iblock) {
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680 }
681
682
683
684
685
686
687
688
689
690 if (!PageUptodate(page)) {
691
692
693
694 }
695
696
697
698
699
700
701 ntfs_error(vol->sb, "Writing beyond initialized size "
702 "is not supported yet. Sorry.");
703 err = -EOPNOTSUPP;
704 break;
705
706
707
708
709 }
710
711
712 if (buffer_mapped(bh))
713 continue;
714
715
716 bh->b_bdev = vol->sb->s_bdev;
717
718
719 vcn = (VCN)block << blocksize_bits;
720 vcn_ofs = vcn & vol->cluster_size_mask;
721 vcn >>= vol->cluster_size_bits;
722 if (!rl) {
723lock_retry_remap:
724 down_read(&ni->runlist.lock);
725 rl = ni->runlist.rl;
726 }
727 if (likely(rl != NULL)) {
728
729 while (rl->length && rl[1].vcn <= vcn)
730 rl++;
731 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
732 } else
733 lcn = LCN_RL_NOT_MAPPED;
734
735 if (lcn >= 0) {
736
737 bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
738 vcn_ofs) >> blocksize_bits;
739 set_buffer_mapped(bh);
740 continue;
741 }
742
743 if (lcn == LCN_HOLE) {
744 u8 *kaddr;
745 unsigned long *bpos, *bend;
746
747
748 kaddr = kmap_atomic(page);
749 bpos = (unsigned long *)(kaddr + bh_offset(bh));
750 bend = (unsigned long *)((u8*)bpos + blocksize);
751 do {
752 if (unlikely(*bpos))
753 break;
754 } while (likely(++bpos < bend));
755 kunmap_atomic(kaddr);
756 if (bpos == bend) {
757
758
759
760
761 bh->b_blocknr = -1;
762 clear_buffer_dirty(bh);
763 continue;
764 }
765
766
767
768 ntfs_error(vol->sb, "Writing into sparse regions is "
769 "not supported yet. Sorry.");
770 err = -EOPNOTSUPP;
771 break;
772 }
773
774 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
775 is_retry = true;
776
777
778
779
780 up_read(&ni->runlist.lock);
781 err = ntfs_map_runlist(ni, vcn);
782 if (likely(!err))
783 goto lock_retry_remap;
784 rl = NULL;
785 } else if (!rl)
786 up_read(&ni->runlist.lock);
787
788
789
790
791
792 if (err == -ENOENT || lcn == LCN_ENOENT) {
793 bh->b_blocknr = -1;
794 clear_buffer_dirty(bh);
795 zero_user(page, bh_offset(bh), blocksize);
796 set_buffer_uptodate(bh);
797 err = 0;
798 continue;
799 }
800
801 if (!err)
802 err = -EIO;
803 bh->b_blocknr = -1;
804 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
805 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
806 "because its location on disk could not be "
807 "determined%s (error code %i).", ni->mft_no,
808 ni->type, (unsigned long long)vcn,
809 vcn_ofs, is_retry ? " even after "
810 "retrying" : "", err);
811 break;
812 } while (block++, (bh = bh->b_this_page) != head);
813
814
815 if (rl)
816 up_read(&ni->runlist.lock);
817
818
819 bh = head;
820
821
822 if (unlikely(!PageUptodate(page))) {
823 int uptodate = 1;
824 do {
825 if (!buffer_uptodate(bh)) {
826 uptodate = 0;
827 bh = head;
828 break;
829 }
830 } while ((bh = bh->b_this_page) != head);
831 if (uptodate)
832 SetPageUptodate(page);
833 }
834
835
836 do {
837 if (buffer_mapped(bh) && buffer_dirty(bh)) {
838 lock_buffer(bh);
839 if (test_clear_buffer_dirty(bh)) {
840 BUG_ON(!buffer_uptodate(bh));
841 mark_buffer_async_write(bh);
842 } else
843 unlock_buffer(bh);
844 } else if (unlikely(err)) {
845
846
847
848
849 if (err != -ENOMEM)
850 clear_buffer_dirty(bh);
851 }
852 } while ((bh = bh->b_this_page) != head);
853
854 if (unlikely(err)) {
855
856 if (unlikely(err == -EOPNOTSUPP))
857 err = 0;
858 else if (err == -ENOMEM) {
859 ntfs_warning(vol->sb, "Error allocating memory. "
860 "Redirtying page so we try again "
861 "later.");
862
863
864
865
866 redirty_page_for_writepage(wbc, page);
867 err = 0;
868 } else
869 SetPageError(page);
870 }
871
872 BUG_ON(PageWriteback(page));
873 set_page_writeback(page);
874
875
876 need_end_writeback = true;
877 do {
878 struct buffer_head *next = bh->b_this_page;
879 if (buffer_async_write(bh)) {
880 submit_bh(WRITE, bh);
881 need_end_writeback = false;
882 }
883 bh = next;
884 } while (bh != head);
885 unlock_page(page);
886
887
888 if (unlikely(need_end_writeback))
889 end_page_writeback(page);
890
891 ntfs_debug("Done.");
892 return err;
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
919static int ntfs_write_mst_block(struct page *page,
920 struct writeback_control *wbc)
921{
922 sector_t block, dblock, rec_block;
923 struct inode *vi = page->mapping->host;
924 ntfs_inode *ni = NTFS_I(vi);
925 ntfs_volume *vol = ni->vol;
926 u8 *kaddr;
927 unsigned int rec_size = ni->itype.index.block_size;
928 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
929 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
930 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
931 runlist_element *rl;
932 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
933 unsigned bh_size, rec_size_bits;
934 bool sync, is_mft, page_is_dirty, rec_is_dirty;
935 unsigned char bh_size_bits;
936
937 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
938 "0x%lx.", vi->i_ino, ni->type, page->index);
939 BUG_ON(!NInoNonResident(ni));
940 BUG_ON(!NInoMstProtected(ni));
941 is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
942
943
944
945
946
947
948 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
949 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
950 bh_size = vol->sb->s_blocksize;
951 bh_size_bits = vol->sb->s_blocksize_bits;
952 max_bhs = PAGE_CACHE_SIZE / bh_size;
953 BUG_ON(!max_bhs);
954 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
955
956
957 sync = (wbc->sync_mode == WB_SYNC_ALL);
958
959
960 bh = head = page_buffers(page);
961 BUG_ON(!bh);
962
963 rec_size_bits = ni->itype.index.block_size_bits;
964 BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
965 bhs_per_rec = rec_size >> bh_size_bits;
966 BUG_ON(!bhs_per_rec);
967
968
969 rec_block = block = (sector_t)page->index <<
970 (PAGE_CACHE_SHIFT - bh_size_bits);
971
972
973 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
974
975 rl = NULL;
976 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
977 page_is_dirty = rec_is_dirty = false;
978 rec_start_bh = NULL;
979 do {
980 bool is_retry = false;
981
982 if (likely(block < rec_block)) {
983 if (unlikely(block >= dblock)) {
984 clear_buffer_dirty(bh);
985 set_buffer_uptodate(bh);
986 continue;
987 }
988
989
990
991
992
993 if (!rec_is_dirty)
994 continue;
995 if (unlikely(err2)) {
996 if (err2 != -ENOMEM)
997 clear_buffer_dirty(bh);
998 continue;
999 }
1000 } else {
1001 BUG_ON(block > rec_block);
1002
1003 rec_block += bhs_per_rec;
1004 err2 = 0;
1005 if (unlikely(block >= dblock)) {
1006 clear_buffer_dirty(bh);
1007 continue;
1008 }
1009 if (!buffer_dirty(bh)) {
1010
1011 rec_is_dirty = false;
1012 continue;
1013 }
1014 rec_is_dirty = true;
1015 rec_start_bh = bh;
1016 }
1017
1018 if (unlikely(!buffer_mapped(bh))) {
1019 VCN vcn;
1020 LCN lcn;
1021 unsigned int vcn_ofs;
1022
1023 bh->b_bdev = vol->sb->s_bdev;
1024
1025 vcn = (VCN)block << bh_size_bits;
1026 vcn_ofs = vcn & vol->cluster_size_mask;
1027 vcn >>= vol->cluster_size_bits;
1028 if (!rl) {
1029lock_retry_remap:
1030 down_read(&ni->runlist.lock);
1031 rl = ni->runlist.rl;
1032 }
1033 if (likely(rl != NULL)) {
1034
1035 while (rl->length && rl[1].vcn <= vcn)
1036 rl++;
1037 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1038 } else
1039 lcn = LCN_RL_NOT_MAPPED;
1040
1041 if (likely(lcn >= 0)) {
1042
1043 bh->b_blocknr = ((lcn <<
1044 vol->cluster_size_bits) +
1045 vcn_ofs) >> bh_size_bits;
1046 set_buffer_mapped(bh);
1047 } else {
1048
1049
1050
1051
1052
1053 if (!is_mft && !is_retry &&
1054 lcn == LCN_RL_NOT_MAPPED) {
1055 is_retry = true;
1056
1057
1058
1059
1060 up_read(&ni->runlist.lock);
1061 err2 = ntfs_map_runlist(ni, vcn);
1062 if (likely(!err2))
1063 goto lock_retry_remap;
1064 if (err2 == -ENOMEM)
1065 page_is_dirty = true;
1066 lcn = err2;
1067 } else {
1068 err2 = -EIO;
1069 if (!rl)
1070 up_read(&ni->runlist.lock);
1071 }
1072
1073 if (!err || err == -ENOMEM)
1074 err = err2;
1075 bh->b_blocknr = -1;
1076 ntfs_error(vol->sb, "Cannot write ntfs record "
1077 "0x%llx (inode 0x%lx, "
1078 "attribute type 0x%x) because "
1079 "its location on disk could "
1080 "not be determined (error "
1081 "code %lli).",
1082 (long long)block <<
1083 bh_size_bits >>
1084 vol->mft_record_size_bits,
1085 ni->mft_no, ni->type,
1086 (long long)lcn);
1087
1088
1089
1090
1091
1092
1093 if (rec_start_bh != bh) {
1094 while (bhs[--nr_bhs] != rec_start_bh)
1095 ;
1096 if (err2 != -ENOMEM) {
1097 do {
1098 clear_buffer_dirty(
1099 rec_start_bh);
1100 } while ((rec_start_bh =
1101 rec_start_bh->
1102 b_this_page) !=
1103 bh);
1104 }
1105 }
1106 continue;
1107 }
1108 }
1109 BUG_ON(!buffer_uptodate(bh));
1110 BUG_ON(nr_bhs >= max_bhs);
1111 bhs[nr_bhs++] = bh;
1112 } while (block++, (bh = bh->b_this_page) != head);
1113 if (unlikely(rl))
1114 up_read(&ni->runlist.lock);
1115
1116 if (!nr_bhs)
1117 goto done;
1118
1119 kaddr = kmap(page);
1120
1121 BUG_ON(!PageUptodate(page));
1122 ClearPageUptodate(page);
1123 for (i = 0; i < nr_bhs; i++) {
1124 unsigned int ofs;
1125
1126
1127 if (i % bhs_per_rec)
1128 continue;
1129 tbh = bhs[i];
1130 ofs = bh_offset(tbh);
1131 if (is_mft) {
1132 ntfs_inode *tni;
1133 unsigned long mft_no;
1134
1135
1136 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1137 >> rec_size_bits;
1138
1139 tni = NULL;
1140 if (!ntfs_may_write_mft_record(vol, mft_no,
1141 (MFT_RECORD*)(kaddr + ofs), &tni)) {
1142
1143
1144
1145
1146
1147 page_is_dirty = true;
1148
1149
1150
1151
1152 do {
1153 bhs[i] = NULL;
1154 } while (++i % bhs_per_rec);
1155 continue;
1156 }
1157
1158
1159
1160
1161
1162 if (tni)
1163 locked_nis[nr_locked_nis++] = tni;
1164 }
1165
1166 err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
1167 rec_size);
1168 if (unlikely(err2)) {
1169 if (!err || err == -ENOMEM)
1170 err = -EIO;
1171 ntfs_error(vol->sb, "Failed to apply mst fixups "
1172 "(inode 0x%lx, attribute type 0x%x, "
1173 "page index 0x%lx, page offset 0x%x)!"
1174 " Unmount and run chkdsk.", vi->i_ino,
1175 ni->type, page->index, ofs);
1176
1177
1178
1179
1180 do {
1181 clear_buffer_dirty(bhs[i]);
1182 bhs[i] = NULL;
1183 } while (++i % bhs_per_rec);
1184 continue;
1185 }
1186 nr_recs++;
1187 }
1188
1189 if (!nr_recs)
1190 goto unm_done;
1191 flush_dcache_page(page);
1192
1193 for (i = 0; i < nr_bhs; i++) {
1194 tbh = bhs[i];
1195 if (!tbh)
1196 continue;
1197 if (!trylock_buffer(tbh))
1198 BUG();
1199
1200 clear_buffer_dirty(tbh);
1201 BUG_ON(!buffer_uptodate(tbh));
1202 BUG_ON(!buffer_mapped(tbh));
1203 get_bh(tbh);
1204 tbh->b_end_io = end_buffer_write_sync;
1205 submit_bh(WRITE, tbh);
1206 }
1207
1208 if (is_mft && !sync)
1209 goto do_mirror;
1210do_wait:
1211
1212 for (i = 0; i < nr_bhs; i++) {
1213 tbh = bhs[i];
1214 if (!tbh)
1215 continue;
1216 wait_on_buffer(tbh);
1217 if (unlikely(!buffer_uptodate(tbh))) {
1218 ntfs_error(vol->sb, "I/O error while writing ntfs "
1219 "record buffer (inode 0x%lx, "
1220 "attribute type 0x%x, page index "
1221 "0x%lx, page offset 0x%lx)! Unmount "
1222 "and run chkdsk.", vi->i_ino, ni->type,
1223 page->index, bh_offset(tbh));
1224 if (!err || err == -ENOMEM)
1225 err = -EIO;
1226
1227
1228
1229
1230 set_buffer_uptodate(tbh);
1231 }
1232 }
1233
1234 if (is_mft && sync) {
1235do_mirror:
1236 for (i = 0; i < nr_bhs; i++) {
1237 unsigned long mft_no;
1238 unsigned int ofs;
1239
1240
1241
1242
1243
1244 if (i % bhs_per_rec)
1245 continue;
1246 tbh = bhs[i];
1247
1248 if (!tbh)
1249 continue;
1250 ofs = bh_offset(tbh);
1251
1252 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1253 >> rec_size_bits;
1254 if (mft_no < vol->mftmirr_size)
1255 ntfs_sync_mft_mirror(vol, mft_no,
1256 (MFT_RECORD*)(kaddr + ofs),
1257 sync);
1258 }
1259 if (!sync)
1260 goto do_wait;
1261 }
1262
1263 for (i = 0; i < nr_bhs; i++) {
1264 if (!(i % bhs_per_rec)) {
1265 tbh = bhs[i];
1266 if (!tbh)
1267 continue;
1268 post_write_mst_fixup((NTFS_RECORD*)(kaddr +
1269 bh_offset(tbh)));
1270 }
1271 }
1272 flush_dcache_page(page);
1273unm_done:
1274
1275 while (nr_locked_nis-- > 0) {
1276 ntfs_inode *tni, *base_tni;
1277
1278 tni = locked_nis[nr_locked_nis];
1279
1280 mutex_lock(&tni->extent_lock);
1281 if (tni->nr_extents >= 0)
1282 base_tni = tni;
1283 else {
1284 base_tni = tni->ext.base_ntfs_ino;
1285 BUG_ON(!base_tni);
1286 }
1287 mutex_unlock(&tni->extent_lock);
1288 ntfs_debug("Unlocking %s inode 0x%lx.",
1289 tni == base_tni ? "base" : "extent",
1290 tni->mft_no);
1291 mutex_unlock(&tni->mrec_lock);
1292 atomic_dec(&tni->count);
1293 iput(VFS_I(base_tni));
1294 }
1295 SetPageUptodate(page);
1296 kunmap(page);
1297done:
1298 if (unlikely(err && err != -ENOMEM)) {
1299
1300
1301
1302
1303 if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
1304 SetPageError(page);
1305 NVolSetErrors(vol);
1306 }
1307 if (page_is_dirty) {
1308 ntfs_debug("Page still contains one or more dirty ntfs "
1309 "records. Redirtying the page starting at "
1310 "record 0x%lx.", page->index <<
1311 (PAGE_CACHE_SHIFT - rec_size_bits));
1312 redirty_page_for_writepage(wbc, page);
1313 unlock_page(page);
1314 } else {
1315
1316
1317
1318
1319
1320 BUG_ON(PageWriteback(page));
1321 set_page_writeback(page);
1322 unlock_page(page);
1323 end_page_writeback(page);
1324 }
1325 if (likely(!err))
1326 ntfs_debug("Done.");
1327 return err;
1328}
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1354{
1355 loff_t i_size;
1356 struct inode *vi = page->mapping->host;
1357 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1358 char *addr;
1359 ntfs_attr_search_ctx *ctx = NULL;
1360 MFT_RECORD *m = NULL;
1361 u32 attr_len;
1362 int err;
1363
1364retry_writepage:
1365 BUG_ON(!PageLocked(page));
1366 i_size = i_size_read(vi);
1367
1368 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1369 PAGE_CACHE_SHIFT)) {
1370
1371
1372
1373
1374 block_invalidatepage(page, 0, PAGE_CACHE_SIZE);
1375 unlock_page(page);
1376 ntfs_debug("Write outside i_size - truncated?");
1377 return 0;
1378 }
1379
1380
1381
1382
1383
1384
1385
1386
1387 if (ni->type != AT_INDEX_ALLOCATION) {
1388
1389 if (NInoEncrypted(ni)) {
1390 unlock_page(page);
1391 BUG_ON(ni->type != AT_DATA);
1392 ntfs_debug("Denying write access to encrypted file.");
1393 return -EACCES;
1394 }
1395
1396 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1397 BUG_ON(ni->type != AT_DATA);
1398 BUG_ON(ni->name_len);
1399
1400
1401 unlock_page(page);
1402 ntfs_error(vi->i_sb, "Writing to compressed files is "
1403 "not supported yet. Sorry.");
1404 return -EOPNOTSUPP;
1405 }
1406
1407 if (NInoNonResident(ni) && NInoSparse(ni)) {
1408 unlock_page(page);
1409 ntfs_error(vi->i_sb, "Writing to sparse files is not "
1410 "supported yet. Sorry.");
1411 return -EOPNOTSUPP;
1412 }
1413 }
1414
1415 if (NInoNonResident(ni)) {
1416
1417 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1418
1419 unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
1420 zero_user_segment(page, ofs, PAGE_CACHE_SIZE);
1421 }
1422
1423 if (NInoMstProtected(ni))
1424 return ntfs_write_mst_block(page, wbc);
1425
1426 return ntfs_write_block(page, wbc);
1427 }
1428
1429
1430
1431
1432
1433
1434
1435
1436 BUG_ON(page_has_buffers(page));
1437 BUG_ON(!PageUptodate(page));
1438 if (unlikely(page->index > 0)) {
1439 ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
1440 "Aborting write.", page->index);
1441 BUG_ON(PageWriteback(page));
1442 set_page_writeback(page);
1443 unlock_page(page);
1444 end_page_writeback(page);
1445 return -EIO;
1446 }
1447 if (!NInoAttr(ni))
1448 base_ni = ni;
1449 else
1450 base_ni = ni->ext.base_ntfs_ino;
1451
1452 m = map_mft_record(base_ni);
1453 if (IS_ERR(m)) {
1454 err = PTR_ERR(m);
1455 m = NULL;
1456 ctx = NULL;
1457 goto err_out;
1458 }
1459
1460
1461
1462
1463 if (unlikely(NInoNonResident(ni))) {
1464 unmap_mft_record(base_ni);
1465 goto retry_writepage;
1466 }
1467 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1468 if (unlikely(!ctx)) {
1469 err = -ENOMEM;
1470 goto err_out;
1471 }
1472 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1473 CASE_SENSITIVE, 0, NULL, 0, ctx);
1474 if (unlikely(err))
1475 goto err_out;
1476
1477
1478
1479
1480 BUG_ON(PageWriteback(page));
1481 set_page_writeback(page);
1482 unlock_page(page);
1483 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1484 i_size = i_size_read(vi);
1485 if (unlikely(attr_len > i_size)) {
1486
1487 attr_len = i_size;
1488
1489
1490
1491
1492 err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
1493 attr_len);
1494
1495 BUG_ON(err);
1496 }
1497 addr = kmap_atomic(page);
1498
1499 memcpy((u8*)ctx->attr +
1500 le16_to_cpu(ctx->attr->data.resident.value_offset),
1501 addr, attr_len);
1502
1503 memset(addr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
1504 kunmap_atomic(addr);
1505 flush_dcache_page(page);
1506 flush_dcache_mft_record_page(ctx->ntfs_ino);
1507
1508 end_page_writeback(page);
1509
1510 mark_mft_record_dirty(ctx->ntfs_ino);
1511 ntfs_attr_put_search_ctx(ctx);
1512 unmap_mft_record(base_ni);
1513 return 0;
1514err_out:
1515 if (err == -ENOMEM) {
1516 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
1517 "page so we try again later.");
1518
1519
1520
1521
1522 redirty_page_for_writepage(wbc, page);
1523 err = 0;
1524 } else {
1525 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1526 "error %i.", err);
1527 SetPageError(page);
1528 NVolSetErrors(ni->vol);
1529 }
1530 unlock_page(page);
1531 if (ctx)
1532 ntfs_attr_put_search_ctx(ctx);
1533 if (m)
1534 unmap_mft_record(base_ni);
1535 return err;
1536}
1537
1538#endif
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
1564{
1565 s64 ofs, size;
1566 loff_t i_size;
1567 LCN lcn;
1568 unsigned long blocksize, flags;
1569 ntfs_inode *ni = NTFS_I(mapping->host);
1570 ntfs_volume *vol = ni->vol;
1571 unsigned delta;
1572 unsigned char blocksize_bits, cluster_size_shift;
1573
1574 ntfs_debug("Entering for mft_no 0x%lx, logical block 0x%llx.",
1575 ni->mft_no, (unsigned long long)block);
1576 if (ni->type != AT_DATA || !NInoNonResident(ni) || NInoEncrypted(ni)) {
1577 ntfs_error(vol->sb, "BMAP does not make sense for %s "
1578 "attributes, returning 0.",
1579 (ni->type != AT_DATA) ? "non-data" :
1580 (!NInoNonResident(ni) ? "resident" :
1581 "encrypted"));
1582 return 0;
1583 }
1584
1585 BUG_ON(NInoCompressed(ni));
1586 BUG_ON(NInoMstProtected(ni));
1587 blocksize = vol->sb->s_blocksize;
1588 blocksize_bits = vol->sb->s_blocksize_bits;
1589 ofs = (s64)block << blocksize_bits;
1590 read_lock_irqsave(&ni->size_lock, flags);
1591 size = ni->initialized_size;
1592 i_size = i_size_read(VFS_I(ni));
1593 read_unlock_irqrestore(&ni->size_lock, flags);
1594
1595
1596
1597
1598
1599 if (unlikely(ofs >= size || (ofs + blocksize > size && size < i_size)))
1600 goto hole;
1601 cluster_size_shift = vol->cluster_size_bits;
1602 down_read(&ni->runlist.lock);
1603 lcn = ntfs_attr_vcn_to_lcn_nolock(ni, ofs >> cluster_size_shift, false);
1604 up_read(&ni->runlist.lock);
1605 if (unlikely(lcn < LCN_HOLE)) {
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615 switch ((int)lcn) {
1616 case LCN_ENOENT:
1617
1618
1619
1620
1621 goto hole;
1622 case LCN_ENOMEM:
1623 ntfs_error(vol->sb, "Not enough memory to complete "
1624 "mapping for inode 0x%lx. "
1625 "Returning 0.", ni->mft_no);
1626 break;
1627 default:
1628 ntfs_error(vol->sb, "Failed to complete mapping for "
1629 "inode 0x%lx. Run chkdsk. "
1630 "Returning 0.", ni->mft_no);
1631 break;
1632 }
1633 return 0;
1634 }
1635 if (lcn < 0) {
1636
1637hole:
1638 ntfs_debug("Done (returning hole).");
1639 return 0;
1640 }
1641
1642
1643
1644
1645 delta = ofs & vol->cluster_size_mask;
1646 if (unlikely(sizeof(block) < sizeof(lcn))) {
1647 block = lcn = ((lcn << cluster_size_shift) + delta) >>
1648 blocksize_bits;
1649
1650 if (unlikely(block != lcn)) {
1651 ntfs_error(vol->sb, "Physical block 0x%llx is too "
1652 "large to be returned, returning 0.",
1653 (long long)lcn);
1654 return 0;
1655 }
1656 } else
1657 block = ((lcn << cluster_size_shift) + delta) >>
1658 blocksize_bits;
1659 ntfs_debug("Done (returning block 0x%llx).", (unsigned long long)lcn);
1660 return block;
1661}
1662
1663
1664
1665
1666
1667
1668
1669const struct address_space_operations ntfs_normal_aops = {
1670 .readpage = ntfs_readpage,
1671#ifdef NTFS_RW
1672 .writepage = ntfs_writepage,
1673 .set_page_dirty = __set_page_dirty_buffers,
1674#endif
1675 .bmap = ntfs_bmap,
1676 .migratepage = buffer_migrate_page,
1677 .is_partially_uptodate = block_is_partially_uptodate,
1678 .error_remove_page = generic_error_remove_page,
1679};
1680
1681
1682
1683
1684const struct address_space_operations ntfs_compressed_aops = {
1685 .readpage = ntfs_readpage,
1686#ifdef NTFS_RW
1687 .writepage = ntfs_writepage,
1688 .set_page_dirty = __set_page_dirty_buffers,
1689#endif
1690 .migratepage = buffer_migrate_page,
1691 .is_partially_uptodate = block_is_partially_uptodate,
1692 .error_remove_page = generic_error_remove_page,
1693};
1694
1695
1696
1697
1698
1699const struct address_space_operations ntfs_mst_aops = {
1700 .readpage = ntfs_readpage,
1701#ifdef NTFS_RW
1702 .writepage = ntfs_writepage,
1703 .set_page_dirty = __set_page_dirty_nobuffers,
1704
1705
1706#endif
1707 .migratepage = buffer_migrate_page,
1708 .is_partially_uptodate = block_is_partially_uptodate,
1709 .error_remove_page = generic_error_remove_page,
1710};
1711
1712#ifdef NTFS_RW
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
1730 struct address_space *mapping = page->mapping;
1731 ntfs_inode *ni = NTFS_I(mapping->host);
1732 struct buffer_head *bh, *head, *buffers_to_free = NULL;
1733 unsigned int end, bh_size, bh_ofs;
1734
1735 BUG_ON(!PageUptodate(page));
1736 end = ofs + ni->itype.index.block_size;
1737 bh_size = VFS_I(ni)->i_sb->s_blocksize;
1738 spin_lock(&mapping->private_lock);
1739 if (unlikely(!page_has_buffers(page))) {
1740 spin_unlock(&mapping->private_lock);
1741 bh = head = alloc_page_buffers(page, bh_size, 1);
1742 spin_lock(&mapping->private_lock);
1743 if (likely(!page_has_buffers(page))) {
1744 struct buffer_head *tail;
1745
1746 do {
1747 set_buffer_uptodate(bh);
1748 tail = bh;
1749 bh = bh->b_this_page;
1750 } while (bh);
1751 tail->b_this_page = head;
1752 attach_page_buffers(page, head);
1753 } else
1754 buffers_to_free = bh;
1755 }
1756 bh = head = page_buffers(page);
1757 BUG_ON(!bh);
1758 do {
1759 bh_ofs = bh_offset(bh);
1760 if (bh_ofs + bh_size <= ofs)
1761 continue;
1762 if (unlikely(bh_ofs >= end))
1763 break;
1764 set_buffer_dirty(bh);
1765 } while ((bh = bh->b_this_page) != head);
1766 spin_unlock(&mapping->private_lock);
1767 __set_page_dirty_nobuffers(page);
1768 if (unlikely(buffers_to_free)) {
1769 do {
1770 bh = buffers_to_free->b_this_page;
1771 free_buffer_head(buffers_to_free);
1772 buffers_to_free = bh;
1773 } while (buffers_to_free);
1774 }
1775}
1776
1777#endif
1778