Merge git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux-tile
[linux-2.6-block.git] / mm / filemap.c
index a794a7f987435c07db26148ec076c35116b3d498..6bf5e42d560a46eea8e4916bd017d855033b8d65 100644 (file)
@@ -202,16 +202,15 @@ void __delete_from_page_cache(struct page *page, void *shadow)
        BUG_ON(page_mapped(page));
 
        /*
-        * Some filesystems seem to re-dirty the page even after
-        * the VM has canceled the dirty bit (eg ext3 journaling).
+        * At this point page must be either written or cleaned by truncate.
+        * Dirty page here signals a bug and loss of unwritten data.
         *
-        * Fix it up by doing a final dirty accounting check after
-        * having removed the page entirely.
+        * This fixes dirty accounting after removing the page entirely but
+        * leaves PageDirty set: it has no effect for truncated page and
+        * anyway will be cleared before returning page into buddy allocator.
         */
-       if (PageDirty(page) && mapping_cap_account_dirty(mapping)) {
-               dec_zone_page_state(page, NR_FILE_DIRTY);
-               dec_bdi_stat(inode_to_bdi(mapping->host), BDI_RECLAIMABLE);
-       }
+       if (WARN_ON_ONCE(PageDirty(page)))
+               account_page_cleaned(page, mapping);
 }
 
 /**
@@ -1694,7 +1693,7 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
        loff_t *ppos = &iocb->ki_pos;
        loff_t pos = *ppos;
 
-       if (io_is_direct(file)) {
+       if (iocb->ki_flags & IOCB_DIRECT) {
                struct address_space *mapping = file->f_mapping;
                struct inode *inode = mapping->host;
                size_t count = iov_iter_count(iter);
@@ -2260,41 +2259,38 @@ EXPORT_SYMBOL(read_cache_page_gfp);
  * Returns appropriate error code that caller should return or
  * zero in case that write should be allowed.
  */
-inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
+inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
 {
+       struct file *file = iocb->ki_filp;
        struct inode *inode = file->f_mapping->host;
        unsigned long limit = rlimit(RLIMIT_FSIZE);
+       loff_t pos;
+
+       if (!iov_iter_count(from))
+               return 0;
 
-        if (unlikely(*pos < 0))
-                return -EINVAL;
+       /* FIXME: this is for backwards compatibility with 2.4 */
+       if (iocb->ki_flags & IOCB_APPEND)
+               iocb->ki_pos = i_size_read(inode);
 
-       if (!isblk) {
-               /* FIXME: this is for backwards compatibility with 2.4 */
-               if (file->f_flags & O_APPEND)
-                        *pos = i_size_read(inode);
+       pos = iocb->ki_pos;
 
-               if (limit != RLIM_INFINITY) {
-                       if (*pos >= limit) {
-                               send_sig(SIGXFSZ, current, 0);
-                               return -EFBIG;
-                       }
-                       if (*count > limit - (typeof(limit))*pos) {
-                               *count = limit - (typeof(limit))*pos;
-                       }
+       if (limit != RLIM_INFINITY) {
+               if (iocb->ki_pos >= limit) {
+                       send_sig(SIGXFSZ, current, 0);
+                       return -EFBIG;
                }
+               iov_iter_truncate(from, limit - (unsigned long)pos);
        }
 
        /*
         * LFS rule
         */
-       if (unlikely(*pos + *count > MAX_NON_LFS &&
+       if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
                                !(file->f_flags & O_LARGEFILE))) {
-               if (*pos >= MAX_NON_LFS) {
+               if (pos >= MAX_NON_LFS)
                        return -EFBIG;
-               }
-               if (*count > MAX_NON_LFS - (unsigned long)*pos) {
-                       *count = MAX_NON_LFS - (unsigned long)*pos;
-               }
+               iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
        }
 
        /*
@@ -2304,34 +2300,11 @@ inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, i
         * exceeded without writing data we send a signal and return EFBIG.
         * Linus frestrict idea will clean these up nicely..
         */
-       if (likely(!isblk)) {
-               if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
-                       if (*count || *pos > inode->i_sb->s_maxbytes) {
-                               return -EFBIG;
-                       }
-                       /* zero-length writes at ->s_maxbytes are OK */
-               }
+       if (unlikely(pos >= inode->i_sb->s_maxbytes))
+               return -EFBIG;
 
-               if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
-                       *count = inode->i_sb->s_maxbytes - *pos;
-       } else {
-#ifdef CONFIG_BLOCK
-               loff_t isize;
-               if (bdev_read_only(I_BDEV(inode)))
-                       return -EPERM;
-               isize = i_size_read(inode);
-               if (*pos >= isize) {
-                       if (*count || *pos > isize)
-                               return -ENOSPC;
-               }
-
-               if (*pos + *count > isize)
-                       *count = isize - *pos;
-#else
-               return -EPERM;
-#endif
-       }
-       return 0;
+       iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
+       return iov_iter_count(from);
 }
 EXPORT_SYMBOL(generic_write_checks);
 
@@ -2571,7 +2544,7 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
        if (err)
                goto out;
 
-       if (io_is_direct(file)) {
+       if (iocb->ki_flags & IOCB_DIRECT) {
                loff_t pos, endbyte;
 
                written = generic_file_direct_write(iocb, from, iocb->ki_pos);
@@ -2641,14 +2614,11 @@ ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
        struct file *file = iocb->ki_filp;
        struct inode *inode = file->f_mapping->host;
        ssize_t ret;
-       size_t count = iov_iter_count(from);
 
        mutex_lock(&inode->i_mutex);
-       ret = generic_write_checks(file, &iocb->ki_pos, &count, 0);
-       if (!ret && count) {
-               iov_iter_truncate(from, count);
+       ret = generic_write_checks(iocb, from);
+       if (ret > 0)
                ret = __generic_file_write_iter(iocb, from);
-       }
        mutex_unlock(&inode->i_mutex);
 
        if (ret > 0) {