aboutsummaryrefslogtreecommitdiffstats
path: root/mm/filemap.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/filemap.c')
-rw-r--r--mm/filemap.c114
1 files changed, 38 insertions, 76 deletions
diff --git a/mm/filemap.c b/mm/filemap.c
index 12548d03c11d..6bf5e42d560a 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1693,7 +1693,7 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1693 loff_t *ppos = &iocb->ki_pos; 1693 loff_t *ppos = &iocb->ki_pos;
1694 loff_t pos = *ppos; 1694 loff_t pos = *ppos;
1695 1695
1696 if (io_is_direct(file)) { 1696 if (iocb->ki_flags & IOCB_DIRECT) {
1697 struct address_space *mapping = file->f_mapping; 1697 struct address_space *mapping = file->f_mapping;
1698 struct inode *inode = mapping->host; 1698 struct inode *inode = mapping->host;
1699 size_t count = iov_iter_count(iter); 1699 size_t count = iov_iter_count(iter);
@@ -1706,7 +1706,7 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1706 pos + count - 1); 1706 pos + count - 1);
1707 if (!retval) { 1707 if (!retval) {
1708 struct iov_iter data = *iter; 1708 struct iov_iter data = *iter;
1709 retval = mapping->a_ops->direct_IO(READ, iocb, &data, pos); 1709 retval = mapping->a_ops->direct_IO(iocb, &data, pos);
1710 } 1710 }
1711 1711
1712 if (retval > 0) { 1712 if (retval > 0) {
@@ -2259,41 +2259,38 @@ EXPORT_SYMBOL(read_cache_page_gfp);
2259 * Returns appropriate error code that caller should return or 2259 * Returns appropriate error code that caller should return or
2260 * zero in case that write should be allowed. 2260 * zero in case that write should be allowed.
2261 */ 2261 */
2262inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk) 2262inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
2263{ 2263{
2264 struct file *file = iocb->ki_filp;
2264 struct inode *inode = file->f_mapping->host; 2265 struct inode *inode = file->f_mapping->host;
2265 unsigned long limit = rlimit(RLIMIT_FSIZE); 2266 unsigned long limit = rlimit(RLIMIT_FSIZE);
2267 loff_t pos;
2266 2268
2267 if (unlikely(*pos < 0)) 2269 if (!iov_iter_count(from))
2268 return -EINVAL; 2270 return 0;
2269 2271
2270 if (!isblk) { 2272 /* FIXME: this is for backwards compatibility with 2.4 */
2271 /* FIXME: this is for backwards compatibility with 2.4 */ 2273 if (iocb->ki_flags & IOCB_APPEND)
2272 if (file->f_flags & O_APPEND) 2274 iocb->ki_pos = i_size_read(inode);
2273 *pos = i_size_read(inode);
2274 2275
2275 if (limit != RLIM_INFINITY) { 2276 pos = iocb->ki_pos;
2276 if (*pos >= limit) { 2277
2277 send_sig(SIGXFSZ, current, 0); 2278 if (limit != RLIM_INFINITY) {
2278 return -EFBIG; 2279 if (iocb->ki_pos >= limit) {
2279 } 2280 send_sig(SIGXFSZ, current, 0);
2280 if (*count > limit - (typeof(limit))*pos) { 2281 return -EFBIG;
2281 *count = limit - (typeof(limit))*pos;
2282 }
2283 } 2282 }
2283 iov_iter_truncate(from, limit - (unsigned long)pos);
2284 } 2284 }
2285 2285
2286 /* 2286 /*
2287 * LFS rule 2287 * LFS rule
2288 */ 2288 */
2289 if (unlikely(*pos + *count > MAX_NON_LFS && 2289 if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
2290 !(file->f_flags & O_LARGEFILE))) { 2290 !(file->f_flags & O_LARGEFILE))) {
2291 if (*pos >= MAX_NON_LFS) { 2291 if (pos >= MAX_NON_LFS)
2292 return -EFBIG; 2292 return -EFBIG;
2293 } 2293 iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
2294 if (*count > MAX_NON_LFS - (unsigned long)*pos) {
2295 *count = MAX_NON_LFS - (unsigned long)*pos;
2296 }
2297 } 2294 }
2298 2295
2299 /* 2296 /*
@@ -2303,34 +2300,11 @@ inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, i
2303 * exceeded without writing data we send a signal and return EFBIG. 2300 * exceeded without writing data we send a signal and return EFBIG.
2304 * Linus frestrict idea will clean these up nicely.. 2301 * Linus frestrict idea will clean these up nicely..
2305 */ 2302 */
2306 if (likely(!isblk)) { 2303 if (unlikely(pos >= inode->i_sb->s_maxbytes))
2307 if (unlikely(*pos >= inode->i_sb->s_maxbytes)) { 2304 return -EFBIG;
2308 if (*count || *pos > inode->i_sb->s_maxbytes) {
2309 return -EFBIG;
2310 }
2311 /* zero-length writes at ->s_maxbytes are OK */
2312 }
2313 2305
2314 if (unlikely(*pos + *count > inode->i_sb->s_maxbytes)) 2306 iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2315 *count = inode->i_sb->s_maxbytes - *pos; 2307 return iov_iter_count(from);
2316 } else {
2317#ifdef CONFIG_BLOCK
2318 loff_t isize;
2319 if (bdev_read_only(I_BDEV(inode)))
2320 return -EPERM;
2321 isize = i_size_read(inode);
2322 if (*pos >= isize) {
2323 if (*count || *pos > isize)
2324 return -ENOSPC;
2325 }
2326
2327 if (*pos + *count > isize)
2328 *count = isize - *pos;
2329#else
2330 return -EPERM;
2331#endif
2332 }
2333 return 0;
2334} 2308}
2335EXPORT_SYMBOL(generic_write_checks); 2309EXPORT_SYMBOL(generic_write_checks);
2336 2310
@@ -2394,7 +2368,7 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos)
2394 } 2368 }
2395 2369
2396 data = *from; 2370 data = *from;
2397 written = mapping->a_ops->direct_IO(WRITE, iocb, &data, pos); 2371 written = mapping->a_ops->direct_IO(iocb, &data, pos);
2398 2372
2399 /* 2373 /*
2400 * Finally, try again to invalidate clean pages which might have been 2374 * Finally, try again to invalidate clean pages which might have been
@@ -2556,23 +2530,12 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2556 struct file *file = iocb->ki_filp; 2530 struct file *file = iocb->ki_filp;
2557 struct address_space * mapping = file->f_mapping; 2531 struct address_space * mapping = file->f_mapping;
2558 struct inode *inode = mapping->host; 2532 struct inode *inode = mapping->host;
2559 loff_t pos = iocb->ki_pos;
2560 ssize_t written = 0; 2533 ssize_t written = 0;
2561 ssize_t err; 2534 ssize_t err;
2562 ssize_t status; 2535 ssize_t status;
2563 size_t count = iov_iter_count(from);
2564 2536
2565 /* We can write back this queue in page reclaim */ 2537 /* We can write back this queue in page reclaim */
2566 current->backing_dev_info = inode_to_bdi(inode); 2538 current->backing_dev_info = inode_to_bdi(inode);
2567 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2568 if (err)
2569 goto out;
2570
2571 if (count == 0)
2572 goto out;
2573
2574 iov_iter_truncate(from, count);
2575
2576 err = file_remove_suid(file); 2539 err = file_remove_suid(file);
2577 if (err) 2540 if (err)
2578 goto out; 2541 goto out;
@@ -2581,10 +2544,10 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2581 if (err) 2544 if (err)
2582 goto out; 2545 goto out;
2583 2546
2584 if (io_is_direct(file)) { 2547 if (iocb->ki_flags & IOCB_DIRECT) {
2585 loff_t endbyte; 2548 loff_t pos, endbyte;
2586 2549
2587 written = generic_file_direct_write(iocb, from, pos); 2550 written = generic_file_direct_write(iocb, from, iocb->ki_pos);
2588 /* 2551 /*
2589 * If the write stopped short of completing, fall back to 2552 * If the write stopped short of completing, fall back to
2590 * buffered writes. Some filesystems do this for writes to 2553 * buffered writes. Some filesystems do this for writes to
@@ -2592,13 +2555,10 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2592 * not succeed (even if it did, DAX does not handle dirty 2555 * not succeed (even if it did, DAX does not handle dirty
2593 * page-cache pages correctly). 2556 * page-cache pages correctly).
2594 */ 2557 */
2595 if (written < 0 || written == count || IS_DAX(inode)) 2558 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
2596 goto out; 2559 goto out;
2597 2560
2598 pos += written; 2561 status = generic_perform_write(file, from, pos = iocb->ki_pos);
2599 count -= written;
2600
2601 status = generic_perform_write(file, from, pos);
2602 /* 2562 /*
2603 * If generic_perform_write() returned a synchronous error 2563 * If generic_perform_write() returned a synchronous error
2604 * then we want to return the number of bytes which were 2564 * then we want to return the number of bytes which were
@@ -2610,15 +2570,15 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2610 err = status; 2570 err = status;
2611 goto out; 2571 goto out;
2612 } 2572 }
2613 iocb->ki_pos = pos + status;
2614 /* 2573 /*
2615 * We need to ensure that the page cache pages are written to 2574 * We need to ensure that the page cache pages are written to
2616 * disk and invalidated to preserve the expected O_DIRECT 2575 * disk and invalidated to preserve the expected O_DIRECT
2617 * semantics. 2576 * semantics.
2618 */ 2577 */
2619 endbyte = pos + status - 1; 2578 endbyte = pos + status - 1;
2620 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte); 2579 err = filemap_write_and_wait_range(mapping, pos, endbyte);
2621 if (err == 0) { 2580 if (err == 0) {
2581 iocb->ki_pos = endbyte + 1;
2622 written += status; 2582 written += status;
2623 invalidate_mapping_pages(mapping, 2583 invalidate_mapping_pages(mapping,
2624 pos >> PAGE_CACHE_SHIFT, 2584 pos >> PAGE_CACHE_SHIFT,
@@ -2630,9 +2590,9 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2630 */ 2590 */
2631 } 2591 }
2632 } else { 2592 } else {
2633 written = generic_perform_write(file, from, pos); 2593 written = generic_perform_write(file, from, iocb->ki_pos);
2634 if (likely(written >= 0)) 2594 if (likely(written > 0))
2635 iocb->ki_pos = pos + written; 2595 iocb->ki_pos += written;
2636 } 2596 }
2637out: 2597out:
2638 current->backing_dev_info = NULL; 2598 current->backing_dev_info = NULL;
@@ -2656,7 +2616,9 @@ ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2656 ssize_t ret; 2616 ssize_t ret;
2657 2617
2658 mutex_lock(&inode->i_mutex); 2618 mutex_lock(&inode->i_mutex);
2659 ret = __generic_file_write_iter(iocb, from); 2619 ret = generic_write_checks(iocb, from);
2620 if (ret > 0)
2621 ret = __generic_file_write_iter(iocb, from);
2660 mutex_unlock(&inode->i_mutex); 2622 mutex_unlock(&inode->i_mutex);
2661 2623
2662 if (ret > 0) { 2624 if (ret > 0) {