Btrfs: fix space leak when we fail to reserve metadata space
[linux-2.6.git] / fs / read_write.c
index 5520f8a..ffc99d2 100644 (file)
@@ -11,7 +11,7 @@
 #include <linux/uio.h>
 #include <linux/fsnotify.h>
 #include <linux/security.h>
-#include <linux/module.h>
+#include <linux/export.h>
 #include <linux/syscalls.h>
 #include <linux/pagemap.h>
 #include <linux/splice.h>
@@ -35,23 +35,45 @@ static inline int unsigned_offsets(struct file *file)
        return file->f_mode & FMODE_UNSIGNED_OFFSET;
 }
 
+static loff_t lseek_execute(struct file *file, struct inode *inode,
+               loff_t offset, loff_t maxsize)
+{
+       if (offset < 0 && !unsigned_offsets(file))
+               return -EINVAL;
+       if (offset > maxsize)
+               return -EINVAL;
+
+       if (offset != file->f_pos) {
+               file->f_pos = offset;
+               file->f_version = 0;
+       }
+       return offset;
+}
+
 /**
- * generic_file_llseek_unlocked - lockless generic llseek implementation
+ * generic_file_llseek_size - generic llseek implementation for regular files
  * @file:      file structure to seek on
  * @offset:    file offset to seek to
  * @origin:    type of seek
+ * @size:      max size of file system
+ *
+ * This is a variant of generic_file_llseek that allows passing in a custom
+ * file size.
  *
- * Updates the file offset to the value specified by @offset and @origin.
- * Locking must be provided by the caller.
+ * Synchronization:
+ * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
+ * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
+ * read/writes behave like SEEK_SET against seeks.
  */
 loff_t
-generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
+generic_file_llseek_size(struct file *file, loff_t offset, int origin,
+               loff_t maxsize)
 {
        struct inode *inode = file->f_mapping->host;
 
        switch (origin) {
        case SEEK_END:
-               offset += inode->i_size;
+               offset += i_size_read(inode);
                break;
        case SEEK_CUR:
                /*
@@ -62,24 +84,38 @@ generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
                 */
                if (offset == 0)
                        return file->f_pos;
-               offset += file->f_pos;
+               /*
+                * f_lock protects against read/modify/write race with other
+                * SEEK_CURs. Note that parallel writes and reads behave
+                * like SEEK_SET.
+                */
+               spin_lock(&file->f_lock);
+               offset = lseek_execute(file, inode, file->f_pos + offset,
+                                      maxsize);
+               spin_unlock(&file->f_lock);
+               return offset;
+       case SEEK_DATA:
+               /*
+                * In the generic case the entire file is data, so as long as
+                * offset isn't at the end of the file then the offset is data.
+                */
+               if (offset >= i_size_read(inode))
+                       return -ENXIO;
+               break;
+       case SEEK_HOLE:
+               /*
+                * There is a virtual hole at the end of the file, so as long as
+                * offset isn't i_size or larger, return i_size.
+                */
+               if (offset >= i_size_read(inode))
+                       return -ENXIO;
+               offset = i_size_read(inode);
                break;
        }
 
-       if (offset < 0 && !unsigned_offsets(file))
-               return -EINVAL;
-       if (offset > inode->i_sb->s_maxbytes)
-               return -EINVAL;
-
-       /* Special lock needed here? */
-       if (offset != file->f_pos) {
-               file->f_pos = offset;
-               file->f_version = 0;
-       }
-
-       return offset;
+       return lseek_execute(file, inode, offset, maxsize);
 }
-EXPORT_SYMBOL(generic_file_llseek_unlocked);
+EXPORT_SYMBOL(generic_file_llseek_size);
 
 /**
  * generic_file_llseek - generic llseek implementation for regular files
@@ -93,13 +129,10 @@ EXPORT_SYMBOL(generic_file_llseek_unlocked);
  */
 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
 {
-       loff_t rval;
-
-       mutex_lock(&file->f_dentry->d_inode->i_mutex);
-       rval = generic_file_llseek_unlocked(file, offset, origin);
-       mutex_unlock(&file->f_dentry->d_inode->i_mutex);
+       struct inode *inode = file->f_mapping->host;
 
-       return rval;
+       return generic_file_llseek_size(file, offset, origin,
+                                       inode->i_sb->s_maxbytes);
 }
 EXPORT_SYMBOL(generic_file_llseek);
 
@@ -128,12 +161,13 @@ EXPORT_SYMBOL(no_llseek);
 
 loff_t default_llseek(struct file *file, loff_t offset, int origin)
 {
+       struct inode *inode = file->f_path.dentry->d_inode;
        loff_t retval;
 
-       mutex_lock(&file->f_dentry->d_inode->i_mutex);
+       mutex_lock(&inode->i_mutex);
        switch (origin) {
                case SEEK_END:
-                       offset += i_size_read(file->f_path.dentry->d_inode);
+                       offset += i_size_read(inode);
                        break;
                case SEEK_CUR:
                        if (offset == 0) {
@@ -141,6 +175,30 @@ loff_t default_llseek(struct file *file, loff_t offset, int origin)
                                goto out;
                        }
                        offset += file->f_pos;
+                       break;
+               case SEEK_DATA:
+                       /*
+                        * In the generic case the entire file is data, so as
+                        * long as offset isn't at the end of the file then the
+                        * offset is data.
+                        */
+                       if (offset >= inode->i_size) {
+                               retval = -ENXIO;
+                               goto out;
+                       }
+                       break;
+               case SEEK_HOLE:
+                       /*
+                        * There is a virtual hole at the end of the file, so
+                        * as long as offset isn't i_size or larger, return
+                        * i_size.
+                        */
+                       if (offset >= inode->i_size) {
+                               retval = -ENXIO;
+                               goto out;
+                       }
+                       offset = inode->i_size;
+                       break;
        }
        retval = -EINVAL;
        if (offset >= 0 || unsigned_offsets(file)) {
@@ -151,7 +209,7 @@ loff_t default_llseek(struct file *file, loff_t offset, int origin)
                retval = offset;
        }
 out:
-       mutex_unlock(&file->f_dentry->d_inode->i_mutex);
+       mutex_unlock(&inode->i_mutex);
        return retval;
 }
 EXPORT_SYMBOL(default_llseek);
@@ -575,7 +633,8 @@ ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
                              unsigned long nr_segs, unsigned long fast_segs,
                              struct iovec *fast_pointer,
-                             struct iovec **ret_pointer)
+                             struct iovec **ret_pointer,
+                             int check_access)
 {
        unsigned long seg;
        ssize_t ret;
@@ -631,7 +690,8 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
                        ret = -EINVAL;
                        goto out;
                }
-               if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
+               if (check_access
+                   && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
                        ret = -EFAULT;
                        goto out;
                }
@@ -663,7 +723,7 @@ static ssize_t do_readv_writev(int type, struct file *file,
        }
 
        ret = rw_copy_check_uvector(type, uvector, nr_segs,
-                       ARRAY_SIZE(iovstack), iovstack, &iov);
+                                   ARRAY_SIZE(iovstack), iovstack, &iov, 1);
        if (ret <= 0)
                goto out;