12 #include <linux/export.h>
13 #include <linux/compiler.h>
17 #include <linux/capability.h>
22 #include <linux/mman.h>
25 #include <linux/uio.h>
26 #include <linux/hash.h>
121 if (PageUptodate(page) && PageMappedToDisk(page))
122 cleancache_put_page(page);
124 cleancache_invalidate_page(mapping, page);
131 if (PageSwapBacked(page))
132 __dec_zone_page_state(page,
NR_SHMEM);
133 BUG_ON(page_mapped(page));
142 if (PageDirty(page) && mapping_cap_account_dirty(mapping)) {
159 void (*freepage)(
struct page *);
161 BUG_ON(!PageLocked(page));
163 freepage = mapping->
a_ops->freepage;
175 static int sleep_on_page(
void *
word)
181 static int sleep_on_page_killable(
void *
word)
203 loff_t
end,
int sync_mode)
209 .range_start =
start,
213 if (!mapping_cap_writeback_dirty(mapping))
270 if (end_byte < start_byte)
273 pagevec_init(&pvec, 0);
274 while ((index <= end) &&
280 for (i = 0; i < nr_pages; i++) {
284 if (page->
index > end)
287 wait_on_page_writeback(page);
288 if (TestClearPageError(page))
291 pagevec_release(&pvec);
314 loff_t i_size = i_size_read(mapping->
host);
357 loff_t lstart, loff_t lend)
405 freepage = mapping->
a_ops->freepage;
417 if (PageSwapBacked(
new))
418 __inc_zone_page_state(
new,
NR_SHMEM);
422 radix_tree_preload_end();
474 radix_tree_preload_end();
487 ret = add_to_page_cache(page, mapping, offset, gfp_mask);
489 lru_cache_add_file(page);
495 struct page *__page_cache_alloc(
gfp_t gfp)
500 if (cpuset_do_page_mem_spread()) {
501 unsigned int cpuset_mems_cookie;
503 cpuset_mems_cookie = get_mems_allowed();
505 page = alloc_pages_exact_node(n, gfp, 0);
506 }
while (!put_mems_allowed(cpuset_mems_cookie) && !page);
527 const struct zone *
zone = page_zone(page);
532 static inline void wake_up_page(
struct page *page,
int bit)
571 __add_wait_queue(q, waiter);
572 spin_unlock_irqrestore(&q->
lock, flags);
603 if (TestClearPageReclaim(page))
639 if (flags & FAULT_FLAG_ALLOW_RETRY) {
644 if (flags & FAULT_FLAG_RETRY_NOWAIT)
648 if (flags & FAULT_FLAG_KILLABLE)
649 wait_on_page_locked_killable(page);
651 wait_on_page_locked(page);
654 if (flags & FAULT_FLAG_KILLABLE) {
686 page = radix_tree_deref_slot(pagep);
689 if (radix_tree_exception(page)) {
690 if (radix_tree_deref_retry(page))
699 if (!page_cache_get_speculative(page))
735 if (page && !radix_tree_exception(page)) {
774 page = __page_cache_alloc(gfp_mask);
813 unsigned int nr_pages,
struct page **
pages)
827 page = radix_tree_deref_slot(slot);
831 if (radix_tree_exception(page)) {
832 if (radix_tree_deref_retry(page)) {
849 if (!page_cache_get_speculative(page))
859 if (++ret == nr_pages)
880 unsigned int nr_pages,
struct page **
pages)
884 unsigned int ret = 0;
894 page = radix_tree_deref_slot(slot);
899 if (radix_tree_exception(page)) {
900 if (radix_tree_deref_retry(page)) {
916 if (!page_cache_get_speculative(page))
936 if (++ret == nr_pages)
956 int tag,
unsigned int nr_pages,
struct page **
pages)
968 &iter, *index, tag) {
971 page = radix_tree_deref_slot(slot);
975 if (radix_tree_exception(page)) {
976 if (radix_tree_deref_retry(page)) {
991 if (!page_cache_get_speculative(page))
1001 if (++ret == nr_pages)
1008 *index = pages[ret - 1]->
index + 1;
1033 if (trylock_page(page))
1038 page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~
__GFP_FS);
1062 static void shrink_readahead_size_eio(
struct file *filp,
1081 static void do_generic_file_read(
struct file *filp, loff_t *ppos,
1091 unsigned int prev_offset;
1104 unsigned long nr,
ret;
1112 index, last_index - index);
1115 goto no_cached_page;
1117 if (PageReadahead(page)) {
1120 index, last_index - index);
1122 if (!PageUptodate(page)) {
1123 if (inode->
i_blkbits == PAGE_CACHE_SHIFT ||
1124 !mapping->
a_ops->is_partially_uptodate)
1125 goto page_not_up_to_date;
1126 if (!trylock_page(page))
1127 goto page_not_up_to_date;
1130 goto page_not_up_to_date_locked;
1131 if (!mapping->
a_ops->is_partially_uptodate(page,
1133 goto page_not_up_to_date_locked;
1146 isize = i_size_read(inode);
1147 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1148 if (
unlikely(!isize || index > end_index)) {
1155 if (index == end_index) {
1168 if (mapping_writably_mapped(mapping))
1175 if (prev_index != index || offset != prev_offset)
1189 ret = actor(desc, page, offset, nr);
1196 if (ret == nr && desc->
count)
1200 page_not_up_to_date:
1202 error = lock_page_killable(page);
1204 goto readpage_error;
1206 page_not_up_to_date_locked:
1215 if (PageUptodate(page)) {
1226 ClearPageError(page);
1228 error = mapping->
a_ops->readpage(filp, page);
1235 goto readpage_error;
1238 if (!PageUptodate(page)) {
1239 error = lock_page_killable(page);
1241 goto readpage_error;
1242 if (!PageUptodate(page)) {
1252 shrink_readahead_size_eio(filp, ra);
1254 goto readpage_error;
1272 page = page_cache_alloc_cold(mapping);
1294 *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) +
offset;
1295 file_accessed(filp);
1299 unsigned long offset,
unsigned long size)
1311 if (!fault_in_pages_writeable(desc->
arg.
buf, size)) {
1314 kaddr + offset, size);
1348 unsigned long *nr_segs,
size_t *
count,
int access_flags)
1352 for (seg = 0; seg < *nr_segs; seg++) {
1387 unsigned long nr_segs, loff_t
pos)
1391 unsigned long seg = 0;
1393 loff_t *ppos = &iocb->
ki_pos;
1404 struct inode *
inode;
1407 inode = mapping->
host;
1410 size = i_size_read(inode);
1413 pos + iov_length(iov, nr_segs) - 1);
1415 retval = mapping->
a_ops->direct_IO(
READ, iocb,
1431 if (retval < 0 || !count || *ppos >= size) {
1432 file_accessed(filp);
1439 for (seg = 0; seg < nr_segs; seg++) {
1448 if (count > iov[seg].iov_len) {
1459 if (desc.
count == 0)
1465 retval = retval ?: desc.
error;
1492 page = page_cache_alloc_cold(mapping);
1498 ret = mapping->
a_ops->readpage(file, page);
1509 #define MMAP_LOTSAMISS (100)
1520 unsigned long ra_pages;
1524 if (VM_RandomReadHint(vma))
1529 if (VM_SequentialReadHint(vma)) {
1536 if (ra->
mmap_miss < MMAP_LOTSAMISS * 10)
1550 ra->
start =
max_t(
long, 0, offset - ra_pages / 2);
1551 ra->
size = ra_pages;
1569 if (VM_RandomReadHint(vma))
1573 if (PageReadahead(page))
1593 struct file *file = vma->
vm_file;
1596 struct inode *inode = mapping->
host;
1602 size = (i_size_read(inode) +
PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1604 return VM_FAULT_SIGBUS;
1610 if (
likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
1615 do_async_mmap_readahead(vma, ra, file, page, offset);
1618 do_sync_mmap_readahead(vma, ra, file, offset);
1621 ret = VM_FAULT_MAJOR;
1625 goto no_cached_page;
1628 if (!lock_page_or_retry(page, vma->
vm_mm, vmf->flags)) {
1630 return ret | VM_FAULT_RETRY;
1646 goto page_not_uptodate;
1652 size = (i_size_read(inode) +
PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1656 return VM_FAULT_SIGBUS;
1660 return ret | VM_FAULT_LOCKED;
1667 error = page_cache_read(file, offset);
1683 return VM_FAULT_OOM;
1684 return VM_FAULT_SIGBUS;
1693 ClearPageError(page);
1694 error = mapping->
a_ops->readpage(file, page);
1696 wait_on_page_locked(page);
1697 if (!PageUptodate(page))
1706 shrink_readahead_size_eio(file, ra);
1707 return VM_FAULT_SIGBUS;
1711 int filemap_page_mkwrite(
struct vm_area_struct *vma,
struct vm_fault *vmf)
1713 struct page *page = vmf->page;
1714 struct inode *inode = vma->
vm_file->f_path.dentry->d_inode;
1715 int ret = VM_FAULT_LOCKED;
1717 sb_start_pagefault(inode->
i_sb);
1722 ret = VM_FAULT_NOPAGE;
1732 sb_end_pagefault(inode->
i_sb);
1739 .page_mkwrite = filemap_page_mkwrite,
1749 if (!mapping->
a_ops->readpage)
1751 file_accessed(file);
1779 static struct page *__read_cache_page(
struct address_space *mapping,
1781 int (*
filler)(
void *,
struct page *),
1799 return ERR_PTR(err);
1801 err =
filler(data, page);
1804 page = ERR_PTR(err);
1810 static struct page *do_read_cache_page(
struct address_space *mapping,
1812 int (*
filler)(
void *,
struct page *),
1821 page = __read_cache_page(mapping, index,
filler, data, gfp);
1824 if (PageUptodate(page))
1833 if (PageUptodate(page)) {
1837 err =
filler(data, page);
1840 return ERR_PTR(err);
1864 int (*
filler)(
void *,
struct page *),
1867 return do_read_cache_page(mapping, index,
filler, data, mapping_gfp_mask(mapping));
1871 static struct page *wait_on_page_read(
struct page *page)
1873 if (!IS_ERR(page)) {
1874 wait_on_page_locked(page);
1875 if (!PageUptodate(page)) {
1877 page = ERR_PTR(-
EIO);
1900 return wait_on_page_read(do_read_cache_page(mapping, index, filler,
NULL, gfp));
1918 int (*
filler)(
void *,
struct page *),
1925 static size_t __iovec_copy_from_user_inatomic(
char *
vaddr,
1926 const struct iovec *iov,
size_t base,
size_t bytes)
1928 size_t copied = 0,
left = 0;
1944 return copied -
left;
1953 struct iov_iter *
i,
unsigned long offset,
size_t bytes)
1964 copied = bytes -
left;
1966 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
1982 struct iov_iter *
i,
unsigned long offset,
size_t bytes)
1992 copied = bytes -
left;
1994 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
2012 unsigned long nr_segs = i->
nr_segs;
2052 return fault_in_pages_readable(buf, bytes);
2078 struct inode *inode = file->
f_mapping->host;
2087 *pos = i_size_read(inode);
2090 if (*pos >= limit) {
2094 if (*count > limit - (
typeof(limit))*pos) {
2095 *count = limit - (
typeof(limit))*pos;
2122 if (*count || *pos > inode->
i_sb->s_maxbytes) {
2129 *count = inode->
i_sb->s_maxbytes - *pos;
2135 isize = i_size_read(inode);
2136 if (*pos >= isize) {
2137 if (*count || *pos > isize)
2141 if (*pos + *count > isize)
2142 *count = isize - *
pos;
2152 loff_t
pos,
unsigned len,
unsigned flags,
2153 struct page **pagep,
void **fsdata)
2157 return aops->
write_begin(file, mapping, pos, len, flags,
2163 loff_t
pos,
unsigned len,
unsigned copied,
2164 struct page *page,
void *fsdata)
2169 return aops->
write_end(file, mapping, pos, len, copied, page, fsdata);
2175 unsigned long *nr_segs, loff_t
pos, loff_t *ppos,
2176 size_t count,
size_t ocount)
2178 struct file *file = iocb->
ki_filp;
2180 struct inode *inode = mapping->
host;
2185 if (count != ocount)
2188 write_len = iov_length(iov, *nr_segs);
2189 end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT;
2203 pos >> PAGE_CACHE_SHIFT, end);
2209 if (written == -
EBUSY)
2215 written = mapping->
a_ops->direct_IO(
WRITE, iocb, iov, pos, *nr_segs);
2227 pos >> PAGE_CACHE_SHIFT, end);
2233 i_size_write(inode, pos);
2234 mark_inode_dirty(inode);
2253 gfp_t gfp_notmask = 0;
2255 gfp_mask = mapping_gfp_mask(mapping);
2256 if (mapping_cap_account_dirty(mapping))
2265 page = __page_cache_alloc(gfp_mask & ~gfp_notmask);
2277 wait_on_page_writeback(page);
2282 static ssize_t generic_perform_write(
struct file *file,
2289 unsigned int flags = 0;
2300 unsigned long bytes;
2324 status = a_ops->
write_begin(file, mapping, pos, bytes, flags,
2329 if (mapping_writably_mapped(mapping))
2332 pagefault_disable();
2338 status = a_ops->
write_end(file, mapping, pos, bytes, copied,
2363 balance_dirty_pages_ratelimited(mapping);
2364 if (fatal_signal_pending(
current)) {
2368 }
while (iov_iter_count(i));
2370 return written ? written :
status;
2375 unsigned long nr_segs, loff_t pos, loff_t *ppos,
2378 struct file *file = iocb->
ki_filp;
2382 iov_iter_init(&i, iov, nr_segs, count, written);
2383 status = generic_perform_write(file, &i, pos);
2385 if (
likely(status >= 0)) {
2390 return written ? written :
status;
2414 unsigned long nr_segs, loff_t *ppos)
2416 struct file *file = iocb->
ki_filp;
2420 struct inode *inode = mapping->
host;
2458 ppos, count, ocount);
2459 if (written < 0 || written == count)
2468 nr_segs, pos, ppos, count,
2477 if (written_buffered < 0) {
2478 err = written_buffered;
2487 endbyte = pos + written_buffered - written - 1;
2490 written = written_buffered;
2492 pos >> PAGE_CACHE_SHIFT,
2493 endbyte >> PAGE_CACHE_SHIFT);
2502 pos, ppos, count, written);
2506 return written ? written :
err;
2522 unsigned long nr_segs, loff_t pos)
2524 struct file *file = iocb->
ki_filp;
2525 struct inode *inode = file->
f_mapping->host;
2530 sb_start_write(inode->
i_sb);
2539 if (err < 0 && ret > 0)
2542 sb_end_write(inode->
i_sb);
2568 BUG_ON(!PageLocked(page));
2569 if (PageWriteback(page))
2572 if (mapping && mapping->
a_ops->releasepage)
2573 return mapping->
a_ops->releasepage(page, gfp_mask);