1
2
3
4
5
6
7
8
9
10
11
12#include <linux/export.h>
13#include <linux/compiler.h>
14#include <linux/fs.h>
15#include <linux/uaccess.h>
16#include <linux/aio.h>
17#include <linux/capability.h>
18#include <linux/kernel_stat.h>
19#include <linux/gfp.h>
20#include <linux/mm.h>
21#include <linux/swap.h>
22#include <linux/mman.h>
23#include <linux/pagemap.h>
24#include <linux/file.h>
25#include <linux/uio.h>
26#include <linux/hash.h>
27#include <linux/writeback.h>
28#include <linux/backing-dev.h>
29#include <linux/pagevec.h>
30#include <linux/blkdev.h>
31#include <linux/security.h>
32#include <linux/syscalls.h>
33#include <linux/cpuset.h>
34#include <linux/hardirq.h>
35#include <linux/memcontrol.h>
36#include <linux/cleancache.h>
37#include "internal.h"
38
39
40
41
42#include <linux/buffer_head.h>
43
44#include <asm/mman.h>
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114void __delete_from_page_cache(struct page *page)
115{
116 struct address_space *mapping = page->mapping;
117
118
119
120
121
122
123 if (PageUptodate(page) && PageMappedToDisk(page))
124 cleancache_put_page(page);
125 else
126 cleancache_flush_page(mapping, page);
127
128 radix_tree_delete(&mapping->page_tree, page->index);
129 page->mapping = NULL;
130
131 mapping->nrpages--;
132 __dec_zone_page_state(page, NR_FILE_PAGES);
133 if (PageSwapBacked(page))
134 __dec_zone_page_state(page, NR_SHMEM);
135 BUG_ON(page_mapped(page));
136
137
138
139
140
141
142
143
144 if (PageDirty(page) && mapping_cap_account_dirty(mapping)) {
145 dec_zone_page_state(page, NR_FILE_DIRTY);
146 dec_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
147 }
148}
149
150
151
152
153
154
155
156
157
158void delete_from_page_cache(struct page *page)
159{
160 struct address_space *mapping = page->mapping;
161 void (*freepage)(struct page *);
162
163 BUG_ON(!PageLocked(page));
164
165 freepage = mapping->a_ops->freepage;
166 spin_lock_irq(&mapping->tree_lock);
167 __delete_from_page_cache(page);
168 spin_unlock_irq(&mapping->tree_lock);
169 mem_cgroup_uncharge_cache_page(page);
170
171 if (freepage)
172 freepage(page);
173 page_cache_release(page);
174}
175EXPORT_SYMBOL(delete_from_page_cache);
176
177static int sleep_on_page(void *word)
178{
179 io_schedule();
180 return 0;
181}
182
183static int sleep_on_page_killable(void *word)
184{
185 sleep_on_page(word);
186 return fatal_signal_pending(current) ? -EINTR : 0;
187}
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
205 loff_t end, int sync_mode)
206{
207 int ret;
208 struct writeback_control wbc = {
209 .sync_mode = sync_mode,
210 .nr_to_write = LONG_MAX,
211 .range_start = start,
212 .range_end = end,
213 };
214
215 if (!mapping_cap_writeback_dirty(mapping))
216 return 0;
217
218 ret = do_writepages(mapping, &wbc);
219 return ret;
220}
221
222static inline int __filemap_fdatawrite(struct address_space *mapping,
223 int sync_mode)
224{
225 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
226}
227
228int filemap_fdatawrite(struct address_space *mapping)
229{
230 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
231}
232EXPORT_SYMBOL(filemap_fdatawrite);
233
234int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
235 loff_t end)
236{
237 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
238}
239EXPORT_SYMBOL(filemap_fdatawrite_range);
240
241
242
243
244
245
246
247
248int filemap_flush(struct address_space *mapping)
249{
250 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
251}
252EXPORT_SYMBOL(filemap_flush);
253
254
255
256
257
258
259
260
261
262
263int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
264 loff_t end_byte)
265{
266 pgoff_t index = start_byte >> PAGE_CACHE_SHIFT;
267 pgoff_t end = end_byte >> PAGE_CACHE_SHIFT;
268 struct pagevec pvec;
269 int nr_pages;
270 int ret = 0;
271
272 if (end_byte < start_byte)
273 return 0;
274
275 pagevec_init(&pvec, 0);
276 while ((index <= end) &&
277 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
278 PAGECACHE_TAG_WRITEBACK,
279 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
280 unsigned i;
281
282 for (i = 0; i < nr_pages; i++) {
283 struct page *page = pvec.pages[i];
284
285
286 if (page->index > end)
287 continue;
288
289 wait_on_page_writeback(page);
290 if (TestClearPageError(page))
291 ret = -EIO;
292 }
293 pagevec_release(&pvec);
294 cond_resched();
295 }
296
297
298 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
299 ret = -ENOSPC;
300 if (test_and_clear_bit(AS_EIO, &mapping->flags))
301 ret = -EIO;
302
303 return ret;
304}
305EXPORT_SYMBOL(filemap_fdatawait_range);
306
307
308
309
310
311
312
313
314int filemap_fdatawait(struct address_space *mapping)
315{
316 loff_t i_size = i_size_read(mapping->host);
317
318 if (i_size == 0)
319 return 0;
320
321 return filemap_fdatawait_range(mapping, 0, i_size - 1);
322}
323EXPORT_SYMBOL(filemap_fdatawait);
324
325int filemap_write_and_wait(struct address_space *mapping)
326{
327 int err = 0;
328
329 if (mapping->nrpages) {
330 err = filemap_fdatawrite(mapping);
331
332
333
334
335
336
337 if (err != -EIO) {
338 int err2 = filemap_fdatawait(mapping);
339 if (!err)
340 err = err2;
341 }
342 }
343 return err;
344}
345EXPORT_SYMBOL(filemap_write_and_wait);
346
347
348
349
350
351
352
353
354
355
356
357
358int filemap_write_and_wait_range(struct address_space *mapping,
359 loff_t lstart, loff_t lend)
360{
361 int err = 0;
362
363 if (mapping->nrpages) {
364 err = __filemap_fdatawrite_range(mapping, lstart, lend,
365 WB_SYNC_ALL);
366
367 if (err != -EIO) {
368 int err2 = filemap_fdatawait_range(mapping,
369 lstart, lend);
370 if (!err)
371 err = err2;
372 }
373 }
374 return err;
375}
376EXPORT_SYMBOL(filemap_write_and_wait_range);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
394{
395 int error;
396
397 VM_BUG_ON(!PageLocked(old));
398 VM_BUG_ON(!PageLocked(new));
399 VM_BUG_ON(new->mapping);
400
401 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
402 if (!error) {
403 struct address_space *mapping = old->mapping;
404 void (*freepage)(struct page *);
405
406 pgoff_t offset = old->index;
407 freepage = mapping->a_ops->freepage;
408
409 page_cache_get(new);
410 new->mapping = mapping;
411 new->index = offset;
412
413 spin_lock_irq(&mapping->tree_lock);
414 __delete_from_page_cache(old);
415 error = radix_tree_insert(&mapping->page_tree, offset, new);
416 BUG_ON(error);
417 mapping->nrpages++;
418 __inc_zone_page_state(new, NR_FILE_PAGES);
419 if (PageSwapBacked(new))
420 __inc_zone_page_state(new, NR_SHMEM);
421 spin_unlock_irq(&mapping->tree_lock);
422
423 mem_cgroup_replace_page_cache(old, new);
424 radix_tree_preload_end();
425 if (freepage)
426 freepage(old);
427 page_cache_release(old);
428 }
429
430 return error;
431}
432EXPORT_SYMBOL_GPL(replace_page_cache_page);
433
434
435
436
437
438
439
440
441
442
443
444int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
445 pgoff_t offset, gfp_t gfp_mask)
446{
447 int error;
448
449 VM_BUG_ON(!PageLocked(page));
450 VM_BUG_ON(PageSwapBacked(page));
451
452 error = mem_cgroup_cache_charge(page, current->mm,
453 gfp_mask & GFP_RECLAIM_MASK);
454 if (error)
455 goto out;
456
457 error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
458 if (error == 0) {
459 page_cache_get(page);
460 page->mapping = mapping;
461 page->index = offset;
462
463 spin_lock_irq(&mapping->tree_lock);
464 error = radix_tree_insert(&mapping->page_tree, offset, page);
465 if (likely(!error)) {
466 mapping->nrpages++;
467 __inc_zone_page_state(page, NR_FILE_PAGES);
468 spin_unlock_irq(&mapping->tree_lock);
469 } else {
470 page->mapping = NULL;
471
472 spin_unlock_irq(&mapping->tree_lock);
473 mem_cgroup_uncharge_cache_page(page);
474 page_cache_release(page);
475 }
476 radix_tree_preload_end();
477 } else
478 mem_cgroup_uncharge_cache_page(page);
479out:
480 return error;
481}
482EXPORT_SYMBOL(add_to_page_cache_locked);
483
484int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
485 pgoff_t offset, gfp_t gfp_mask)
486{
487 int ret;
488
489 ret = add_to_page_cache(page, mapping, offset, gfp_mask);
490 if (ret == 0)
491 lru_cache_add_file(page);
492 return ret;
493}
494EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
495
496#ifdef CONFIG_NUMA
497struct page *__page_cache_alloc(gfp_t gfp)
498{
499 int n;
500 struct page *page;
501
502 if (cpuset_do_page_mem_spread()) {
503 get_mems_allowed();
504 n = cpuset_mem_spread_node();
505 page = alloc_pages_exact_node(n, gfp, 0);
506 put_mems_allowed();
507 return page;
508 }
509 return alloc_pages(gfp, 0);
510}
511EXPORT_SYMBOL(__page_cache_alloc);
512#endif
513
514
515
516
517
518
519
520
521
522
523
524static wait_queue_head_t *page_waitqueue(struct page *page)
525{
526 const struct zone *zone = page_zone(page);
527
528 return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
529}
530
531static inline void wake_up_page(struct page *page, int bit)
532{
533 __wake_up_bit(page_waitqueue(page), &page->flags, bit);
534}
535
536void wait_on_page_bit(struct page *page, int bit_nr)
537{
538 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
539
540 if (test_bit(bit_nr, &page->flags))
541 __wait_on_bit(page_waitqueue(page), &wait, sleep_on_page,
542 TASK_UNINTERRUPTIBLE);
543}
544EXPORT_SYMBOL(wait_on_page_bit);
545
546int wait_on_page_bit_killable(struct page *page, int bit_nr)
547{
548 DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
549
550 if (!test_bit(bit_nr, &page->flags))
551 return 0;
552
553 return __wait_on_bit(page_waitqueue(page), &wait,
554 sleep_on_page_killable, TASK_KILLABLE);
555}
556
557
558
559
560
561
562
563
564void add_page_wait_queue(struct page *page, wait_queue_t *waiter)
565{
566 wait_queue_head_t *q = page_waitqueue(page);
567 unsigned long flags;
568
569 spin_lock_irqsave(&q->lock, flags);
570 __add_wait_queue(q, waiter);
571 spin_unlock_irqrestore(&q->lock, flags);
572}
573EXPORT_SYMBOL_GPL(add_page_wait_queue);
574
575
576
577
578
579
580
581
582
583
584
585
586
587void unlock_page(struct page *page)
588{
589 VM_BUG_ON(!PageLocked(page));
590 clear_bit_unlock(PG_locked, &page->flags);
591 smp_mb__after_clear_bit();
592 wake_up_page(page, PG_locked);
593}
594EXPORT_SYMBOL(unlock_page);
595
596
597
598
599
600void end_page_writeback(struct page *page)
601{
602 if (TestClearPageReclaim(page))
603 rotate_reclaimable_page(page);
604
605 if (!test_clear_page_writeback(page))
606 BUG();
607
608 smp_mb__after_clear_bit();
609 wake_up_page(page, PG_writeback);
610}
611EXPORT_SYMBOL(end_page_writeback);
612
613
614
615
616
617void __lock_page(struct page *page)
618{
619 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
620
621 __wait_on_bit_lock(page_waitqueue(page), &wait, sleep_on_page,
622 TASK_UNINTERRUPTIBLE);
623}
624EXPORT_SYMBOL(__lock_page);
625
626int __lock_page_killable(struct page *page)
627{
628 DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
629
630 return __wait_on_bit_lock(page_waitqueue(page), &wait,
631 sleep_on_page_killable, TASK_KILLABLE);
632}
633EXPORT_SYMBOL_GPL(__lock_page_killable);
634
635int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
636 unsigned int flags)
637{
638 if (flags & FAULT_FLAG_ALLOW_RETRY) {
639
640
641
642
643 if (flags & FAULT_FLAG_RETRY_NOWAIT)
644 return 0;
645
646 up_read(&mm->mmap_sem);
647 if (flags & FAULT_FLAG_KILLABLE)
648 wait_on_page_locked_killable(page);
649 else
650 wait_on_page_locked(page);
651 return 0;
652 } else {
653 if (flags & FAULT_FLAG_KILLABLE) {
654 int ret;
655
656 ret = __lock_page_killable(page);
657 if (ret) {
658 up_read(&mm->mmap_sem);
659 return 0;
660 }
661 } else
662 __lock_page(page);
663 return 1;
664 }
665}
666
667
668
669
670
671
672
673
674
675struct page *find_get_page(struct address_space *mapping, pgoff_t offset)
676{
677 void **pagep;
678 struct page *page;
679
680 rcu_read_lock();
681repeat:
682 page = NULL;
683 pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
684 if (pagep) {
685 page = radix_tree_deref_slot(pagep);
686 if (unlikely(!page))
687 goto out;
688 if (radix_tree_exception(page)) {
689 if (radix_tree_deref_retry(page))
690 goto repeat;
691
692
693
694
695
696 goto out;
697 }
698 if (!page_cache_get_speculative(page))
699 goto repeat;
700
701
702
703
704
705
706 if (unlikely(page != *pagep)) {
707 page_cache_release(page);
708 goto repeat;
709 }
710 }
711out:
712 rcu_read_unlock();
713
714 return page;
715}
716EXPORT_SYMBOL(find_get_page);
717
718
719
720
721
722
723
724
725
726
727
728struct page *find_lock_page(struct address_space *mapping, pgoff_t offset)
729{
730 struct page *page;
731
732repeat:
733 page = find_get_page(mapping, offset);
734 if (page && !radix_tree_exception(page)) {
735 lock_page(page);
736
737 if (unlikely(page->mapping != mapping)) {
738 unlock_page(page);
739 page_cache_release(page);
740 goto repeat;
741 }
742 VM_BUG_ON(page->index != offset);
743 }
744 return page;
745}
746EXPORT_SYMBOL(find_lock_page);
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765struct page *find_or_create_page(struct address_space *mapping,
766 pgoff_t index, gfp_t gfp_mask)
767{
768 struct page *page;
769 int err;
770repeat:
771 page = find_lock_page(mapping, index);
772 if (!page) {
773 page = __page_cache_alloc(gfp_mask);
774 if (!page)
775 return NULL;
776
777
778
779
780
781
782 err = add_to_page_cache_lru(page, mapping, index,
783 (gfp_mask & GFP_RECLAIM_MASK));
784 if (unlikely(err)) {
785 page_cache_release(page);
786 page = NULL;
787 if (err == -EEXIST)
788 goto repeat;
789 }
790 }
791 return page;
792}
793EXPORT_SYMBOL(find_or_create_page);
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
812 unsigned int nr_pages, struct page **pages)
813{
814 unsigned int i;
815 unsigned int ret;
816 unsigned int nr_found, nr_skip;
817
818 rcu_read_lock();
819restart:
820 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
821 (void ***)pages, NULL, start, nr_pages);
822 ret = 0;
823 nr_skip = 0;
824 for (i = 0; i < nr_found; i++) {
825 struct page *page;
826repeat:
827 page = radix_tree_deref_slot((void **)pages[i]);
828 if (unlikely(!page))
829 continue;
830
831 if (radix_tree_exception(page)) {
832 if (radix_tree_deref_retry(page)) {
833
834
835
836
837
838 WARN_ON(start | i);
839 goto restart;
840 }
841
842
843
844
845
846 nr_skip++;
847 continue;
848 }
849
850 if (!page_cache_get_speculative(page))
851 goto repeat;
852
853
854 if (unlikely(page != *((void **)pages[i]))) {
855 page_cache_release(page);
856 goto repeat;
857 }
858
859 pages[ret] = page;
860 ret++;
861 }
862
863
864
865
866
867 if (unlikely(!ret && nr_found > nr_skip))
868 goto restart;
869 rcu_read_unlock();
870 return ret;
871}
872
873
874
875
876
877
878
879
880
881
882
883
884
885unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
886 unsigned int nr_pages, struct page **pages)
887{
888 unsigned int i;
889 unsigned int ret;
890 unsigned int nr_found;
891
892 rcu_read_lock();
893restart:
894 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
895 (void ***)pages, NULL, index, nr_pages);
896 ret = 0;
897 for (i = 0; i < nr_found; i++) {
898 struct page *page;
899repeat:
900 page = radix_tree_deref_slot((void **)pages[i]);
901 if (unlikely(!page))
902 continue;
903
904 if (radix_tree_exception(page)) {
905 if (radix_tree_deref_retry(page)) {
906
907
908
909
910
911 goto restart;
912 }
913
914
915
916
917
918 break;
919 }
920
921 if (!page_cache_get_speculative(page))
922 goto repeat;
923
924
925 if (unlikely(page != *((void **)pages[i]))) {
926 page_cache_release(page);
927 goto repeat;
928 }
929
930
931
932
933
934
935 if (page->mapping == NULL || page->index != index) {
936 page_cache_release(page);
937 break;
938 }
939
940 pages[ret] = page;
941 ret++;
942 index++;
943 }
944 rcu_read_unlock();
945 return ret;
946}
947EXPORT_SYMBOL(find_get_pages_contig);
948
949
950
951
952
953
954
955
956
957
958
959
960unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
961 int tag, unsigned int nr_pages, struct page **pages)
962{
963 unsigned int i;
964 unsigned int ret;
965 unsigned int nr_found;
966
967 rcu_read_lock();
968restart:
969 nr_found = radix_tree_gang_lookup_tag_slot(&mapping->page_tree,
970 (void ***)pages, *index, nr_pages, tag);
971 ret = 0;
972 for (i = 0; i < nr_found; i++) {
973 struct page *page;
974repeat:
975 page = radix_tree_deref_slot((void **)pages[i]);
976 if (unlikely(!page))
977 continue;
978
979 if (radix_tree_exception(page)) {
980 if (radix_tree_deref_retry(page)) {
981
982
983
984
985
986 goto restart;
987 }
988
989
990
991
992 BUG();
993 }
994
995 if (!page_cache_get_speculative(page))
996 goto repeat;
997
998
999 if (unlikely(page != *((void **)pages[i]))) {
1000 page_cache_release(page);
1001 goto repeat;
1002 }
1003
1004 pages[ret] = page;
1005 ret++;
1006 }
1007
1008
1009
1010
1011
1012 if (unlikely(!ret && nr_found))
1013 goto restart;
1014 rcu_read_unlock();
1015
1016 if (ret)
1017 *index = pages[ret - 1]->index + 1;
1018
1019 return ret;
1020}
1021EXPORT_SYMBOL(find_get_pages_tag);
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036struct page *
1037grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
1038{
1039 struct page *page = find_get_page(mapping, index);
1040
1041 if (page) {
1042 if (trylock_page(page))
1043 return page;
1044 page_cache_release(page);
1045 return NULL;
1046 }
1047 page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
1048 if (page && add_to_page_cache_lru(page, mapping, index, GFP_NOFS)) {
1049 page_cache_release(page);
1050 page = NULL;
1051 }
1052 return page;
1053}
1054EXPORT_SYMBOL(grab_cache_page_nowait);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071static void shrink_readahead_size_eio(struct file *filp,
1072 struct file_ra_state *ra)
1073{
1074 ra->ra_pages /= 4;
1075}
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090static void do_generic_file_read(struct file *filp, loff_t *ppos,
1091 read_descriptor_t *desc, read_actor_t actor)
1092{
1093 struct address_space *mapping = filp->f_mapping;
1094 struct inode *inode = mapping->host;
1095 struct file_ra_state *ra = &filp->f_ra;
1096 pgoff_t index;
1097 pgoff_t last_index;
1098 pgoff_t prev_index;
1099 unsigned long offset;
1100 unsigned int prev_offset;
1101 int error;
1102
1103 index = *ppos >> PAGE_CACHE_SHIFT;
1104 prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
1105 prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
1106 last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
1107 offset = *ppos & ~PAGE_CACHE_MASK;
1108
1109 for (;;) {
1110 struct page *page;
1111 pgoff_t end_index;
1112 loff_t isize;
1113 unsigned long nr, ret;
1114
1115 cond_resched();
1116find_page:
1117 page = find_get_page(mapping, index);
1118 if (!page) {
1119 page_cache_sync_readahead(mapping,
1120 ra, filp,
1121 index, last_index - index);
1122 page = find_get_page(mapping, index);
1123 if (unlikely(page == NULL))
1124 goto no_cached_page;
1125 }
1126 if (PageReadahead(page)) {
1127 page_cache_async_readahead(mapping,
1128 ra, filp, page,
1129 index, last_index - index);
1130 }
1131 if (!PageUptodate(page)) {
1132 if (inode->i_blkbits == PAGE_CACHE_SHIFT ||
1133 !mapping->a_ops->is_partially_uptodate)
1134 goto page_not_up_to_date;
1135 if (!trylock_page(page))
1136 goto page_not_up_to_date;
1137
1138 if (!page->mapping)
1139 goto page_not_up_to_date_locked;
1140 if (!mapping->a_ops->is_partially_uptodate(page,
1141 desc, offset))
1142 goto page_not_up_to_date_locked;
1143 unlock_page(page);
1144 }
1145page_ok:
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155 isize = i_size_read(inode);
1156 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1157 if (unlikely(!isize || index > end_index)) {
1158 page_cache_release(page);
1159 goto out;
1160 }
1161
1162
1163 nr = PAGE_CACHE_SIZE;
1164 if (index == end_index) {
1165 nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1166 if (nr <= offset) {
1167 page_cache_release(page);
1168 goto out;
1169 }
1170 }
1171 nr = nr - offset;
1172
1173
1174
1175
1176
1177 if (mapping_writably_mapped(mapping))
1178 flush_dcache_page(page);
1179
1180
1181
1182
1183
1184 if (prev_index != index || offset != prev_offset)
1185 mark_page_accessed(page);
1186 prev_index = index;
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198 ret = actor(desc, page, offset, nr);
1199 offset += ret;
1200 index += offset >> PAGE_CACHE_SHIFT;
1201 offset &= ~PAGE_CACHE_MASK;
1202 prev_offset = offset;
1203
1204 page_cache_release(page);
1205 if (ret == nr && desc->count)
1206 continue;
1207 goto out;
1208
1209page_not_up_to_date:
1210
1211 error = lock_page_killable(page);
1212 if (unlikely(error))
1213 goto readpage_error;
1214
1215page_not_up_to_date_locked:
1216
1217 if (!page->mapping) {
1218 unlock_page(page);
1219 page_cache_release(page);
1220 continue;
1221 }
1222
1223
1224 if (PageUptodate(page)) {
1225 unlock_page(page);
1226 goto page_ok;
1227 }
1228
1229readpage:
1230
1231
1232
1233
1234
1235 ClearPageError(page);
1236
1237 error = mapping->a_ops->readpage(filp, page);
1238
1239 if (unlikely(error)) {
1240 if (error == AOP_TRUNCATED_PAGE) {
1241 page_cache_release(page);
1242 goto find_page;
1243 }
1244 goto readpage_error;
1245 }
1246
1247 if (!PageUptodate(page)) {
1248 error = lock_page_killable(page);
1249 if (unlikely(error))
1250 goto readpage_error;
1251 if (!PageUptodate(page)) {
1252 if (page->mapping == NULL) {
1253
1254
1255
1256 unlock_page(page);
1257 page_cache_release(page);
1258 goto find_page;
1259 }
1260 unlock_page(page);
1261 shrink_readahead_size_eio(filp, ra);
1262 error = -EIO;
1263 goto readpage_error;
1264 }
1265 unlock_page(page);
1266 }
1267
1268 goto page_ok;
1269
1270readpage_error:
1271
1272 desc->error = error;
1273 page_cache_release(page);
1274 goto out;
1275
1276no_cached_page:
1277
1278
1279
1280
1281 page = page_cache_alloc_cold(mapping);
1282 if (!page) {
1283 desc->error = -ENOMEM;
1284 goto out;
1285 }
1286 error = add_to_page_cache_lru(page, mapping,
1287 index, GFP_KERNEL);
1288 if (error) {
1289 page_cache_release(page);
1290 if (error == -EEXIST)
1291 goto find_page;
1292 desc->error = error;
1293 goto out;
1294 }
1295 goto readpage;
1296 }
1297
1298out:
1299 ra->prev_pos = prev_index;
1300 ra->prev_pos <<= PAGE_CACHE_SHIFT;
1301 ra->prev_pos |= prev_offset;
1302
1303 *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
1304 file_accessed(filp);
1305}
1306
1307int file_read_actor(read_descriptor_t *desc, struct page *page,
1308 unsigned long offset, unsigned long size)
1309{
1310 char *kaddr;
1311 unsigned long left, count = desc->count;
1312
1313 if (size > count)
1314 size = count;
1315
1316
1317
1318
1319
1320 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1321 kaddr = kmap_atomic(page, KM_USER0);
1322 left = __copy_to_user_inatomic(desc->arg.buf,
1323 kaddr + offset, size);
1324 kunmap_atomic(kaddr, KM_USER0);
1325 if (left == 0)
1326 goto success;
1327 }
1328
1329
1330 kaddr = kmap(page);
1331 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1332 kunmap(page);
1333
1334 if (left) {
1335 size -= left;
1336 desc->error = -EFAULT;
1337 }
1338success:
1339 desc->count = count - size;
1340 desc->written += size;
1341 desc->arg.buf += size;
1342 return size;
1343}
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356int generic_segment_checks(const struct iovec *iov,
1357 unsigned long *nr_segs, size_t *count, int access_flags)
1358{
1359 unsigned long seg;
1360 size_t cnt = 0;
1361 for (seg = 0; seg < *nr_segs; seg++) {
1362 const struct iovec *iv = &iov[seg];
1363
1364
1365
1366
1367
1368 cnt += iv->iov_len;
1369 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1370 return -EINVAL;
1371 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
1372 continue;
1373 if (seg == 0)
1374 return -EFAULT;
1375 *nr_segs = seg;
1376 cnt -= iv->iov_len;
1377 break;
1378 }
1379 *count = cnt;
1380 return 0;
1381}
1382EXPORT_SYMBOL(generic_segment_checks);
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394ssize_t
1395generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1396 unsigned long nr_segs, loff_t pos)
1397{
1398 struct file *filp = iocb->ki_filp;
1399 ssize_t retval;
1400 unsigned long seg = 0;
1401 size_t count;
1402 loff_t *ppos = &iocb->ki_pos;
1403
1404 count = 0;
1405 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1406 if (retval)
1407 return retval;
1408
1409
1410 if (filp->f_flags & O_DIRECT) {
1411 loff_t size;
1412 struct address_space *mapping;
1413 struct inode *inode;
1414
1415 mapping = filp->f_mapping;
1416 inode = mapping->host;
1417 if (!count)
1418 goto out;
1419 size = i_size_read(inode);
1420 if (pos < size) {
1421 retval = filemap_write_and_wait_range(mapping, pos,
1422 pos + iov_length(iov, nr_segs) - 1);
1423 if (!retval) {
1424 struct blk_plug plug;
1425
1426 blk_start_plug(&plug);
1427 retval = mapping->a_ops->direct_IO(READ, iocb,
1428 iov, pos, nr_segs);
1429 blk_finish_plug(&plug);
1430 }
1431 if (retval > 0) {
1432 *ppos = pos + retval;
1433 count -= retval;
1434 }
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444 if (retval < 0 || !count || *ppos >= size) {
1445 file_accessed(filp);
1446 goto out;
1447 }
1448 }
1449 }
1450
1451 count = retval;
1452 for (seg = 0; seg < nr_segs; seg++) {
1453 read_descriptor_t desc;
1454 loff_t offset = 0;
1455
1456
1457
1458
1459
1460 if (count) {
1461 if (count > iov[seg].iov_len) {
1462 count -= iov[seg].iov_len;
1463 continue;
1464 }
1465 offset = count;
1466 count = 0;
1467 }
1468
1469 desc.written = 0;
1470 desc.arg.buf = iov[seg].iov_base + offset;
1471 desc.count = iov[seg].iov_len - offset;
1472 if (desc.count == 0)
1473 continue;
1474 desc.error = 0;
1475 do_generic_file_read(filp, ppos, &desc, file_read_actor);
1476 retval += desc.written;
1477 if (desc.error) {
1478 retval = retval ?: desc.error;
1479 break;
1480 }
1481 if (desc.count > 0)
1482 break;
1483 }
1484out:
1485 return retval;
1486}
1487EXPORT_SYMBOL(generic_file_aio_read);
1488
1489static ssize_t
1490do_readahead(struct address_space *mapping, struct file *filp,
1491 pgoff_t index, unsigned long nr)
1492{
1493 if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
1494 return -EINVAL;
1495
1496 force_page_cache_readahead(mapping, filp, index, nr);
1497 return 0;
1498}
1499
1500SYSCALL_DEFINE(readahead)(int fd, loff_t offset, size_t count)
1501{
1502 ssize_t ret;
1503 struct file *file;
1504
1505 ret = -EBADF;
1506 file = fget(fd);
1507 if (file) {
1508 if (file->f_mode & FMODE_READ) {
1509 struct address_space *mapping = file->f_mapping;
1510 pgoff_t start = offset >> PAGE_CACHE_SHIFT;
1511 pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
1512 unsigned long len = end - start + 1;
1513 ret = do_readahead(mapping, file, start, len);
1514 }
1515 fput(file);
1516 }
1517 return ret;
1518}
1519#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
1520asmlinkage long SyS_readahead(long fd, loff_t offset, long count)
1521{
1522 return SYSC_readahead((int) fd, offset, (size_t) count);
1523}
1524SYSCALL_ALIAS(sys_readahead, SyS_readahead);
1525#endif
1526
1527#ifdef CONFIG_MMU
1528
1529
1530
1531
1532
1533
1534
1535
1536static int page_cache_read(struct file *file, pgoff_t offset)
1537{
1538 struct address_space *mapping = file->f_mapping;
1539 struct page *page;
1540 int ret;
1541
1542 do {
1543 page = page_cache_alloc_cold(mapping);
1544 if (!page)
1545 return -ENOMEM;
1546
1547 ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
1548 if (ret == 0)
1549 ret = mapping->a_ops->readpage(file, page);
1550 else if (ret == -EEXIST)
1551 ret = 0;
1552
1553 page_cache_release(page);
1554
1555 } while (ret == AOP_TRUNCATED_PAGE);
1556
1557 return ret;
1558}
1559
1560#define MMAP_LOTSAMISS (100)
1561
1562
1563
1564
1565
1566static void do_sync_mmap_readahead(struct vm_area_struct *vma,
1567 struct file_ra_state *ra,
1568 struct file *file,
1569 pgoff_t offset)
1570{
1571 unsigned long ra_pages;
1572 struct address_space *mapping = file->f_mapping;
1573
1574
1575 if (VM_RandomReadHint(vma))
1576 return;
1577 if (!ra->ra_pages)
1578 return;
1579
1580 if (VM_SequentialReadHint(vma)) {
1581 page_cache_sync_readahead(mapping, ra, file, offset,
1582 ra->ra_pages);
1583 return;
1584 }
1585
1586
1587 if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
1588 ra->mmap_miss++;
1589
1590
1591
1592
1593
1594 if (ra->mmap_miss > MMAP_LOTSAMISS)
1595 return;
1596
1597
1598
1599
1600 ra_pages = max_sane_readahead(ra->ra_pages);
1601 ra->start = max_t(long, 0, offset - ra_pages / 2);
1602 ra->size = ra_pages;
1603 ra->async_size = ra_pages / 4;
1604 ra_submit(ra, mapping, file);
1605}
1606
1607
1608
1609
1610
1611static void do_async_mmap_readahead(struct vm_area_struct *vma,
1612 struct file_ra_state *ra,
1613 struct file *file,
1614 struct page *page,
1615 pgoff_t offset)
1616{
1617 struct address_space *mapping = file->f_mapping;
1618
1619
1620 if (VM_RandomReadHint(vma))
1621 return;
1622 if (ra->mmap_miss > 0)
1623 ra->mmap_miss--;
1624 if (PageReadahead(page))
1625 page_cache_async_readahead(mapping, ra, file,
1626 page, offset, ra->ra_pages);
1627}
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1642{
1643 int error;
1644 struct file *file = vma->vm_file;
1645 struct address_space *mapping = file->f_mapping;
1646 struct file_ra_state *ra = &file->f_ra;
1647 struct inode *inode = mapping->host;
1648 pgoff_t offset = vmf->pgoff;
1649 struct page *page;
1650 pgoff_t size;
1651 int ret = 0;
1652
1653 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1654 if (offset >= size)
1655 return VM_FAULT_SIGBUS;
1656
1657
1658
1659
1660 page = find_get_page(mapping, offset);
1661 if (likely(page)) {
1662
1663
1664
1665
1666 do_async_mmap_readahead(vma, ra, file, page, offset);
1667 } else {
1668
1669 do_sync_mmap_readahead(vma, ra, file, offset);
1670 count_vm_event(PGMAJFAULT);
1671 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1672 ret = VM_FAULT_MAJOR;
1673retry_find:
1674 page = find_get_page(mapping, offset);
1675 if (!page)
1676 goto no_cached_page;
1677 }
1678
1679 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
1680 page_cache_release(page);
1681 return ret | VM_FAULT_RETRY;
1682 }
1683
1684
1685 if (unlikely(page->mapping != mapping)) {
1686 unlock_page(page);
1687 put_page(page);
1688 goto retry_find;
1689 }
1690 VM_BUG_ON(page->index != offset);
1691
1692
1693
1694
1695
1696 if (unlikely(!PageUptodate(page)))
1697 goto page_not_uptodate;
1698
1699
1700
1701
1702
1703 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1704 if (unlikely(offset >= size)) {
1705 unlock_page(page);
1706 page_cache_release(page);
1707 return VM_FAULT_SIGBUS;
1708 }
1709
1710 vmf->page = page;
1711 return ret | VM_FAULT_LOCKED;
1712
1713no_cached_page:
1714
1715
1716
1717
1718 error = page_cache_read(file, offset);
1719
1720
1721
1722
1723
1724
1725 if (error >= 0)
1726 goto retry_find;
1727
1728
1729
1730
1731
1732
1733 if (error == -ENOMEM)
1734 return VM_FAULT_OOM;
1735 return VM_FAULT_SIGBUS;
1736
1737page_not_uptodate:
1738
1739
1740
1741
1742
1743
1744 ClearPageError(page);
1745 error = mapping->a_ops->readpage(file, page);
1746 if (!error) {
1747 wait_on_page_locked(page);
1748 if (!PageUptodate(page))
1749 error = -EIO;
1750 }
1751 page_cache_release(page);
1752
1753 if (!error || error == AOP_TRUNCATED_PAGE)
1754 goto retry_find;
1755
1756
1757 shrink_readahead_size_eio(file, ra);
1758 return VM_FAULT_SIGBUS;
1759}
1760EXPORT_SYMBOL(filemap_fault);
1761
1762const struct vm_operations_struct generic_file_vm_ops = {
1763 .fault = filemap_fault,
1764};
1765
1766
1767
1768int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1769{
1770 struct address_space *mapping = file->f_mapping;
1771
1772 if (!mapping->a_ops->readpage)
1773 return -ENOEXEC;
1774 file_accessed(file);
1775 vma->vm_ops = &generic_file_vm_ops;
1776 vma->vm_flags |= VM_CAN_NONLINEAR;
1777 return 0;
1778}
1779
1780
1781
1782
1783int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
1784{
1785 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1786 return -EINVAL;
1787 return generic_file_mmap(file, vma);
1788}
1789#else
1790int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
1791{
1792 return -ENOSYS;
1793}
1794int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
1795{
1796 return -ENOSYS;
1797}
1798#endif
1799
1800EXPORT_SYMBOL(generic_file_mmap);
1801EXPORT_SYMBOL(generic_file_readonly_mmap);
1802
1803static struct page *__read_cache_page(struct address_space *mapping,
1804 pgoff_t index,
1805 int (*filler)(void *, struct page *),
1806 void *data,
1807 gfp_t gfp)
1808{
1809 struct page *page;
1810 int err;
1811repeat:
1812 page = find_get_page(mapping, index);
1813 if (!page) {
1814 page = __page_cache_alloc(gfp | __GFP_COLD);
1815 if (!page)
1816 return ERR_PTR(-ENOMEM);
1817 err = add_to_page_cache_lru(page, mapping, index, gfp);
1818 if (unlikely(err)) {
1819 page_cache_release(page);
1820 if (err == -EEXIST)
1821 goto repeat;
1822
1823 return ERR_PTR(err);
1824 }
1825 err = filler(data, page);
1826 if (err < 0) {
1827 page_cache_release(page);
1828 page = ERR_PTR(err);
1829 }
1830 }
1831 return page;
1832}
1833
1834static struct page *do_read_cache_page(struct address_space *mapping,
1835 pgoff_t index,
1836 int (*filler)(void *, struct page *),
1837 void *data,
1838 gfp_t gfp)
1839
1840{
1841 struct page *page;
1842 int err;
1843
1844retry:
1845 page = __read_cache_page(mapping, index, filler, data, gfp);
1846 if (IS_ERR(page))
1847 return page;
1848 if (PageUptodate(page))
1849 goto out;
1850
1851 lock_page(page);
1852 if (!page->mapping) {
1853 unlock_page(page);
1854 page_cache_release(page);
1855 goto retry;
1856 }
1857 if (PageUptodate(page)) {
1858 unlock_page(page);
1859 goto out;
1860 }
1861 err = filler(data, page);
1862 if (err < 0) {
1863 page_cache_release(page);
1864 return ERR_PTR(err);
1865 }
1866out:
1867 mark_page_accessed(page);
1868 return page;
1869}
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886struct page *read_cache_page_async(struct address_space *mapping,
1887 pgoff_t index,
1888 int (*filler)(void *, struct page *),
1889 void *data)
1890{
1891 return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
1892}
1893EXPORT_SYMBOL(read_cache_page_async);
1894
1895static struct page *wait_on_page_read(struct page *page)
1896{
1897 if (!IS_ERR(page)) {
1898 wait_on_page_locked(page);
1899 if (!PageUptodate(page)) {
1900 page_cache_release(page);
1901 page = ERR_PTR(-EIO);
1902 }
1903 }
1904 return page;
1905}
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918struct page *read_cache_page_gfp(struct address_space *mapping,
1919 pgoff_t index,
1920 gfp_t gfp)
1921{
1922 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
1923
1924 return wait_on_page_read(do_read_cache_page(mapping, index, filler, NULL, gfp));
1925}
1926EXPORT_SYMBOL(read_cache_page_gfp);
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940struct page *read_cache_page(struct address_space *mapping,
1941 pgoff_t index,
1942 int (*filler)(void *, struct page *),
1943 void *data)
1944{
1945 return wait_on_page_read(read_cache_page_async(mapping, index, filler, data));
1946}
1947EXPORT_SYMBOL(read_cache_page);
1948
1949
1950
1951
1952
1953
1954
1955int should_remove_suid(struct dentry *dentry)
1956{
1957 umode_t mode = dentry->d_inode->i_mode;
1958 int kill = 0;
1959
1960
1961 if (unlikely(mode & S_ISUID))
1962 kill = ATTR_KILL_SUID;
1963
1964
1965
1966
1967
1968 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1969 kill |= ATTR_KILL_SGID;
1970
1971 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
1972 return kill;
1973
1974 return 0;
1975}
1976EXPORT_SYMBOL(should_remove_suid);
1977
1978static int __remove_suid(struct dentry *dentry, int kill)
1979{
1980 struct iattr newattrs;
1981
1982 newattrs.ia_valid = ATTR_FORCE | kill;
1983 return notify_change(dentry, &newattrs);
1984}
1985
1986int file_remove_suid(struct file *file)
1987{
1988 struct dentry *dentry = file->f_path.dentry;
1989 struct inode *inode = dentry->d_inode;
1990 int killsuid;
1991 int killpriv;
1992 int error = 0;
1993
1994
1995 if (IS_NOSEC(inode))
1996 return 0;
1997
1998 killsuid = should_remove_suid(dentry);
1999 killpriv = security_inode_need_killpriv(dentry);
2000
2001 if (killpriv < 0)
2002 return killpriv;
2003 if (killpriv)
2004 error = security_inode_killpriv(dentry);
2005 if (!error && killsuid)
2006 error = __remove_suid(dentry, killsuid);
2007 if (!error && (inode->i_sb->s_flags & MS_NOSEC))
2008 inode->i_flags |= S_NOSEC;
2009
2010 return error;
2011}
2012EXPORT_SYMBOL(file_remove_suid);
2013
2014static size_t __iovec_copy_from_user_inatomic(char *vaddr,
2015 const struct iovec *iov, size_t base, size_t bytes)
2016{
2017 size_t copied = 0, left = 0;
2018
2019 while (bytes) {
2020 char __user *buf = iov->iov_base + base;
2021 int copy = min(bytes, iov->iov_len - base);
2022
2023 base = 0;
2024 left = __copy_from_user_inatomic(vaddr, buf, copy);
2025 copied += copy;
2026 bytes -= copy;
2027 vaddr += copy;
2028 iov++;
2029
2030 if (unlikely(left))
2031 break;
2032 }
2033 return copied - left;
2034}
2035
2036
2037
2038
2039
2040
2041size_t iov_iter_copy_from_user_atomic(struct page *page,
2042 struct iov_iter *i, unsigned long offset, size_t bytes)
2043{
2044 char *kaddr;
2045 size_t copied;
2046
2047 BUG_ON(!in_atomic());
2048 kaddr = kmap_atomic(page, KM_USER0);
2049 if (likely(i->nr_segs == 1)) {
2050 int left;
2051 char __user *buf = i->iov->iov_base + i->iov_offset;
2052 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
2053 copied = bytes - left;
2054 } else {
2055 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2056 i->iov, i->iov_offset, bytes);
2057 }
2058 kunmap_atomic(kaddr, KM_USER0);
2059
2060 return copied;
2061}
2062EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
2063
2064
2065
2066
2067
2068
2069
2070size_t iov_iter_copy_from_user(struct page *page,
2071 struct iov_iter *i, unsigned long offset, size_t bytes)
2072{
2073 char *kaddr;
2074 size_t copied;
2075
2076 kaddr = kmap(page);
2077 if (likely(i->nr_segs == 1)) {
2078 int left;
2079 char __user *buf = i->iov->iov_base + i->iov_offset;
2080 left = __copy_from_user(kaddr + offset, buf, bytes);
2081 copied = bytes - left;
2082 } else {
2083 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2084 i->iov, i->iov_offset, bytes);
2085 }
2086 kunmap(page);
2087 return copied;
2088}
2089EXPORT_SYMBOL(iov_iter_copy_from_user);
2090
2091void iov_iter_advance(struct iov_iter *i, size_t bytes)
2092{
2093 BUG_ON(i->count < bytes);
2094
2095 if (likely(i->nr_segs == 1)) {
2096 i->iov_offset += bytes;
2097 i->count -= bytes;
2098 } else {
2099 const struct iovec *iov = i->iov;
2100 size_t base = i->iov_offset;
2101 unsigned long nr_segs = i->nr_segs;
2102
2103
2104
2105
2106
2107 while (bytes || unlikely(i->count && !iov->iov_len)) {
2108 int copy;
2109
2110 copy = min(bytes, iov->iov_len - base);
2111 BUG_ON(!i->count || i->count < copy);
2112 i->count -= copy;
2113 bytes -= copy;
2114 base += copy;
2115 if (iov->iov_len == base) {
2116 iov++;
2117 nr_segs--;
2118 base = 0;
2119 }
2120 }
2121 i->iov = iov;
2122 i->iov_offset = base;
2123 i->nr_segs = nr_segs;
2124 }
2125}
2126EXPORT_SYMBOL(iov_iter_advance);
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
2138{
2139 char __user *buf = i->iov->iov_base + i->iov_offset;
2140 bytes = min(bytes, i->iov->iov_len - i->iov_offset);
2141 return fault_in_pages_readable(buf, bytes);
2142}
2143EXPORT_SYMBOL(iov_iter_fault_in_readable);
2144
2145
2146
2147
2148size_t iov_iter_single_seg_count(struct iov_iter *i)
2149{
2150 const struct iovec *iov = i->iov;
2151 if (i->nr_segs == 1)
2152 return i->count;
2153 else
2154 return min(i->count, iov->iov_len - i->iov_offset);
2155}
2156EXPORT_SYMBOL(iov_iter_single_seg_count);
2157
2158
2159
2160
2161
2162
2163
2164
2165inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
2166{
2167 struct inode *inode = file->f_mapping->host;
2168 unsigned long limit = rlimit(RLIMIT_FSIZE);
2169
2170 if (unlikely(*pos < 0))
2171 return -EINVAL;
2172
2173 if (!isblk) {
2174
2175 if (file->f_flags & O_APPEND)
2176 *pos = i_size_read(inode);
2177
2178 if (limit != RLIM_INFINITY) {
2179 if (*pos >= limit) {
2180 send_sig(SIGXFSZ, current, 0);
2181 return -EFBIG;
2182 }
2183 if (*count > limit - (typeof(limit))*pos) {
2184 *count = limit - (typeof(limit))*pos;
2185 }
2186 }
2187 }
2188
2189
2190
2191
2192 if (unlikely(*pos + *count > MAX_NON_LFS &&
2193 !(file->f_flags & O_LARGEFILE))) {
2194 if (*pos >= MAX_NON_LFS) {
2195 return -EFBIG;
2196 }
2197 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
2198 *count = MAX_NON_LFS - (unsigned long)*pos;
2199 }
2200 }
2201
2202
2203
2204
2205
2206
2207
2208
2209 if (likely(!isblk)) {
2210 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
2211 if (*count || *pos > inode->i_sb->s_maxbytes) {
2212 return -EFBIG;
2213 }
2214
2215 }
2216
2217 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
2218 *count = inode->i_sb->s_maxbytes - *pos;
2219 } else {
2220#ifdef CONFIG_BLOCK
2221 loff_t isize;
2222 if (bdev_read_only(I_BDEV(inode)))
2223 return -EPERM;
2224 isize = i_size_read(inode);
2225 if (*pos >= isize) {
2226 if (*count || *pos > isize)
2227 return -ENOSPC;
2228 }
2229
2230 if (*pos + *count > isize)
2231 *count = isize - *pos;
2232#else
2233 return -EPERM;
2234#endif
2235 }
2236 return 0;
2237}
2238EXPORT_SYMBOL(generic_write_checks);
2239
2240int pagecache_write_begin(struct file *file, struct address_space *mapping,
2241 loff_t pos, unsigned len, unsigned flags,
2242 struct page **pagep, void **fsdata)
2243{
2244 const struct address_space_operations *aops = mapping->a_ops;
2245
2246 return aops->write_begin(file, mapping, pos, len, flags,
2247 pagep, fsdata);
2248}
2249EXPORT_SYMBOL(pagecache_write_begin);
2250
2251int pagecache_write_end(struct file *file, struct address_space *mapping,
2252 loff_t pos, unsigned len, unsigned copied,
2253 struct page *page, void *fsdata)
2254{
2255 const struct address_space_operations *aops = mapping->a_ops;
2256
2257 mark_page_accessed(page);
2258 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
2259}
2260EXPORT_SYMBOL(pagecache_write_end);
2261
2262ssize_t
2263generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
2264 unsigned long *nr_segs, loff_t pos, loff_t *ppos,
2265 size_t count, size_t ocount)
2266{
2267 struct file *file = iocb->ki_filp;
2268 struct address_space *mapping = file->f_mapping;
2269 struct inode *inode = mapping->host;
2270 ssize_t written;
2271 size_t write_len;
2272 pgoff_t end;
2273
2274 if (count != ocount)
2275 *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
2276
2277 write_len = iov_length(iov, *nr_segs);
2278 end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT;
2279
2280 written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
2281 if (written)
2282 goto out;
2283
2284
2285
2286
2287
2288
2289
2290 if (mapping->nrpages) {
2291 written = invalidate_inode_pages2_range(mapping,
2292 pos >> PAGE_CACHE_SHIFT, end);
2293
2294
2295
2296
2297 if (written) {
2298 if (written == -EBUSY)
2299 return 0;
2300 goto out;
2301 }
2302 }
2303
2304 written = mapping->a_ops->direct_IO(WRITE, iocb, iov, pos, *nr_segs);
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314 if (mapping->nrpages) {
2315 invalidate_inode_pages2_range(mapping,
2316 pos >> PAGE_CACHE_SHIFT, end);
2317 }
2318
2319 if (written > 0) {
2320 pos += written;
2321 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
2322 i_size_write(inode, pos);
2323 mark_inode_dirty(inode);
2324 }
2325 *ppos = pos;
2326 }
2327out:
2328 return written;
2329}
2330EXPORT_SYMBOL(generic_file_direct_write);
2331
2332
2333
2334
2335
2336struct page *grab_cache_page_write_begin(struct address_space *mapping,
2337 pgoff_t index, unsigned flags)
2338{
2339 int status;
2340 gfp_t gfp_mask;
2341 struct page *page;
2342 gfp_t gfp_notmask = 0;
2343
2344 gfp_mask = mapping_gfp_mask(mapping) | __GFP_WRITE;
2345 if (flags & AOP_FLAG_NOFS)
2346 gfp_notmask = __GFP_FS;
2347repeat:
2348 page = find_lock_page(mapping, index);
2349 if (page)
2350 goto found;
2351
2352 page = __page_cache_alloc(gfp_mask & ~gfp_notmask);
2353 if (!page)
2354 return NULL;
2355 status = add_to_page_cache_lru(page, mapping, index,
2356 GFP_KERNEL & ~gfp_notmask);
2357 if (unlikely(status)) {
2358 page_cache_release(page);
2359 if (status == -EEXIST)
2360 goto repeat;
2361 return NULL;
2362 }
2363found:
2364 wait_on_page_writeback(page);
2365 return page;
2366}
2367EXPORT_SYMBOL(grab_cache_page_write_begin);
2368
2369static ssize_t generic_perform_write(struct file *file,
2370 struct iov_iter *i, loff_t pos)
2371{
2372 struct address_space *mapping = file->f_mapping;
2373 const struct address_space_operations *a_ops = mapping->a_ops;
2374 long status = 0;
2375 ssize_t written = 0;
2376 unsigned int flags = 0;
2377
2378
2379
2380
2381 if (segment_eq(get_fs(), KERNEL_DS))
2382 flags |= AOP_FLAG_UNINTERRUPTIBLE;
2383
2384 do {
2385 struct page *page;
2386 unsigned long offset;
2387 unsigned long bytes;
2388 size_t copied;
2389 void *fsdata;
2390
2391 offset = (pos & (PAGE_CACHE_SIZE - 1));
2392 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2393 iov_iter_count(i));
2394
2395again:
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
2407 status = -EFAULT;
2408 break;
2409 }
2410
2411 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
2412 &page, &fsdata);
2413 if (unlikely(status))
2414 break;
2415
2416 if (mapping_writably_mapped(mapping))
2417 flush_dcache_page(page);
2418
2419 pagefault_disable();
2420 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
2421 pagefault_enable();
2422 flush_dcache_page(page);
2423
2424 mark_page_accessed(page);
2425 status = a_ops->write_end(file, mapping, pos, bytes, copied,
2426 page, fsdata);
2427 if (unlikely(status < 0))
2428 break;
2429 copied = status;
2430
2431 cond_resched();
2432
2433 iov_iter_advance(i, copied);
2434 if (unlikely(copied == 0)) {
2435
2436
2437
2438
2439
2440
2441
2442
2443 bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
2444 iov_iter_single_seg_count(i));
2445 goto again;
2446 }
2447 pos += copied;
2448 written += copied;
2449
2450 balance_dirty_pages_ratelimited(mapping);
2451 if (fatal_signal_pending(current)) {
2452 status = -EINTR;
2453 break;
2454 }
2455 } while (iov_iter_count(i));
2456
2457 return written ? written : status;
2458}
2459
2460ssize_t
2461generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
2462 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2463 size_t count, ssize_t written)
2464{
2465 struct file *file = iocb->ki_filp;
2466 ssize_t status;
2467 struct iov_iter i;
2468
2469 iov_iter_init(&i, iov, nr_segs, count, written);
2470 status = generic_perform_write(file, &i, pos);
2471
2472 if (likely(status >= 0)) {
2473 written += status;
2474 *ppos = pos + status;
2475 }
2476
2477 return written ? written : status;
2478}
2479EXPORT_SYMBOL(generic_file_buffered_write);
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2501 unsigned long nr_segs, loff_t *ppos)
2502{
2503 struct file *file = iocb->ki_filp;
2504 struct address_space * mapping = file->f_mapping;
2505 size_t ocount;
2506 size_t count;
2507 struct inode *inode = mapping->host;
2508 loff_t pos;
2509 ssize_t written;
2510 ssize_t err;
2511
2512 ocount = 0;
2513 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
2514 if (err)
2515 return err;
2516
2517 count = ocount;
2518 pos = *ppos;
2519
2520 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2521
2522
2523 current->backing_dev_info = mapping->backing_dev_info;
2524 written = 0;
2525
2526 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2527 if (err)
2528 goto out;
2529
2530 if (count == 0)
2531 goto out;
2532
2533 err = file_remove_suid(file);
2534 if (err)
2535 goto out;
2536
2537 file_update_time(file);
2538
2539
2540 if (unlikely(file->f_flags & O_DIRECT)) {
2541 loff_t endbyte;
2542 ssize_t written_buffered;
2543
2544 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
2545 ppos, count, ocount);
2546 if (written < 0 || written == count)
2547 goto out;
2548
2549
2550
2551
2552 pos += written;
2553 count -= written;
2554 written_buffered = generic_file_buffered_write(iocb, iov,
2555 nr_segs, pos, ppos, count,
2556 written);
2557
2558
2559
2560
2561
2562
2563
2564 if (written_buffered < 0) {
2565 err = written_buffered;
2566 goto out;
2567 }
2568
2569
2570
2571
2572
2573
2574 endbyte = pos + written_buffered - written - 1;
2575 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
2576 if (err == 0) {
2577 written = written_buffered;
2578 invalidate_mapping_pages(mapping,
2579 pos >> PAGE_CACHE_SHIFT,
2580 endbyte >> PAGE_CACHE_SHIFT);
2581 } else {
2582
2583
2584
2585
2586 }
2587 } else {
2588 written = generic_file_buffered_write(iocb, iov, nr_segs,
2589 pos, ppos, count, written);
2590 }
2591out:
2592 current->backing_dev_info = NULL;
2593 return written ? written : err;
2594}
2595EXPORT_SYMBOL(__generic_file_aio_write);
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2609 unsigned long nr_segs, loff_t pos)
2610{
2611 struct file *file = iocb->ki_filp;
2612 struct inode *inode = file->f_mapping->host;
2613 struct blk_plug plug;
2614 ssize_t ret;
2615
2616 BUG_ON(iocb->ki_pos != pos);
2617
2618 mutex_lock(&inode->i_mutex);
2619 blk_start_plug(&plug);
2620 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
2621 mutex_unlock(&inode->i_mutex);
2622
2623 if (ret > 0 || ret == -EIOCBQUEUED) {
2624 ssize_t err;
2625
2626 err = generic_write_sync(file, pos, ret);
2627 if (err < 0 && ret > 0)
2628 ret = err;
2629 }
2630 blk_finish_plug(&plug);
2631 return ret;
2632}
2633EXPORT_SYMBOL(generic_file_aio_write);
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652int try_to_release_page(struct page *page, gfp_t gfp_mask)
2653{
2654 struct address_space * const mapping = page->mapping;
2655
2656 BUG_ON(!PageLocked(page));
2657 if (PageWriteback(page))
2658 return 0;
2659
2660 if (mapping && mapping->a_ops->releasepage)
2661 return mapping->a_ops->releasepage(page, gfp_mask);
2662 return try_to_free_buffers(page);
2663}
2664
2665EXPORT_SYMBOL(try_to_release_page);
2666