Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / read_write.c
index a30ab9d..c71dd10 100644 (file)
 #include <linux/file.h>
 #include <linux/uio.h>
 #include <linux/smp_lock.h>
-#include <linux/dnotify.h>
+#include <linux/fsnotify.h>
 #include <linux/security.h>
 #include <linux/module.h>
+#include <linux/syscalls.h>
+#include <linux/pagemap.h>
 
 #include <asm/uaccess.h>
+#include <asm/unistd.h>
 
-struct file_operations generic_ro_fops = {
+const struct file_operations generic_ro_fops = {
        .llseek         = generic_file_llseek,
        .read           = generic_file_read,
        .mmap           = generic_file_readonly_mmap,
@@ -30,7 +33,7 @@ loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
        long long retval;
        struct inode *inode = file->f_mapping->host;
 
-       down(&inode->i_sem);
+       mutex_lock(&inode->i_mutex);
        switch (origin) {
                case 2:
                        offset += inode->i_size;
@@ -46,7 +49,7 @@ loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
                }
                retval = offset;
        }
-       up(&inode->i_sem);
+       mutex_unlock(&inode->i_mutex);
        return retval;
 }
 
@@ -75,14 +78,12 @@ loff_t remote_llseek(struct file *file, loff_t offset, int origin)
        unlock_kernel();
        return retval;
 }
-
 EXPORT_SYMBOL(remote_llseek);
 
 loff_t no_llseek(struct file *file, loff_t offset, int origin)
 {
        return -ESPIPE;
 }
-
 EXPORT_SYMBOL(no_llseek);
 
 loff_t default_llseek(struct file *file, loff_t offset, int origin)
@@ -108,18 +109,21 @@ loff_t default_llseek(struct file *file, loff_t offset, int origin)
        unlock_kernel();
        return retval;
 }
-
 EXPORT_SYMBOL(default_llseek);
 
-static inline loff_t llseek(struct file *file, loff_t offset, int origin)
+loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
 {
        loff_t (*fn)(struct file *, loff_t, int);
 
-       fn = default_llseek;
-       if (file->f_op && file->f_op->llseek)
-               fn = file->f_op->llseek;
+       fn = no_llseek;
+       if (file->f_mode & FMODE_LSEEK) {
+               fn = default_llseek;
+               if (file->f_op && file->f_op->llseek)
+                       fn = file->f_op->llseek;
+       }
        return fn(file, offset, origin);
 }
+EXPORT_SYMBOL(vfs_llseek);
 
 asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
 {
@@ -134,7 +138,7 @@ asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
 
        retval = -EINVAL;
        if (origin <= 2) {
-               loff_t res = llseek(file, offset, origin);
+               loff_t res = vfs_llseek(file, offset, origin);
                retval = res;
                if (res != (loff_t)retval)
                        retval = -EOVERFLOW;    /* LFS: should only happen on 32 bit platforms */
@@ -143,9 +147,8 @@ asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
 bad:
        return retval;
 }
-EXPORT_SYMBOL_GPL(sys_lseek);
 
-#if !defined(__alpha__)
+#ifdef __ARCH_WANT_SYS_LLSEEK
 asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high,
                           unsigned long offset_low, loff_t __user * result,
                           unsigned int origin)
@@ -164,7 +167,7 @@ asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high,
        if (origin > 2)
                goto out_putf;
 
-       offset = llseek(file, ((loff_t) offset_high << 32) | offset_low,
+       offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
                        origin);
 
        retval = (int)offset;
@@ -180,6 +183,48 @@ bad:
 }
 #endif
 
+/*
+ * rw_verify_area doesn't like huge counts. We limit
+ * them to something that fits in "int" so that others
+ * won't have to do range checks all the time.
+ */
+#define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
+
+int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
+{
+       struct inode *inode;
+       loff_t pos;
+
+       if (unlikely((ssize_t) count < 0))
+               goto Einval;
+       pos = *ppos;
+       if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
+               goto Einval;
+
+       inode = file->f_dentry->d_inode;
+       if (unlikely(inode->i_flock && MANDATORY_LOCK(inode))) {
+               int retval = locks_mandatory_area(
+                       read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
+                       inode, file, pos, count);
+               if (retval < 0)
+                       return retval;
+       }
+       return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
+
+Einval:
+       return -EINVAL;
+}
+
+static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
+{
+       set_current_state(TASK_UNINTERRUPTIBLE);
+       if (!kiocbIsKicked(iocb))
+               schedule();
+       else
+               kiocbClearKicked(iocb);
+       __set_current_state(TASK_RUNNING);
+}
+
 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
 {
        struct kiocb kiocb;
@@ -187,7 +232,10 @@ ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *pp
 
        init_sync_kiocb(&kiocb, filp);
        kiocb.ki_pos = *ppos;
-       ret = filp->f_op->aio_read(&kiocb, buf, len, kiocb.ki_pos);
+       while (-EIOCBRETRY ==
+               (ret = filp->f_op->aio_read(&kiocb, buf, len, kiocb.ki_pos)))
+               wait_on_retry_sync_kiocb(&kiocb);
+
        if (-EIOCBQUEUED == ret)
                ret = wait_on_sync_kiocb(&kiocb);
        *ppos = kiocb.ki_pos;
@@ -198,24 +246,29 @@ EXPORT_SYMBOL(do_sync_read);
 
 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
 {
-       struct inode *inode = file->f_dentry->d_inode;
        ssize_t ret;
 
        if (!(file->f_mode & FMODE_READ))
                return -EBADF;
        if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
                return -EINVAL;
+       if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
+               return -EFAULT;
 
-       ret = locks_verify_area(FLOCK_VERIFY_READ, inode, file, *pos, count);
-       if (!ret) {
+       ret = rw_verify_area(READ, file, pos, count);
+       if (ret >= 0) {
+               count = ret;
                ret = security_file_permission (file, MAY_READ);
                if (!ret) {
                        if (file->f_op->read)
                                ret = file->f_op->read(file, buf, count, pos);
                        else
                                ret = do_sync_read(file, buf, count, pos);
-                       if (ret > 0)
-                               dnotify_parent(file->f_dentry, DN_ACCESS);
+                       if (ret > 0) {
+                               fsnotify_access(file->f_dentry);
+                               current->rchar += ret;
+                       }
+                       current->syscr++;
                }
        }
 
@@ -231,7 +284,10 @@ ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, lof
 
        init_sync_kiocb(&kiocb, filp);
        kiocb.ki_pos = *ppos;
-       ret = filp->f_op->aio_write(&kiocb, buf, len, kiocb.ki_pos);
+       while (-EIOCBRETRY ==
+              (ret = filp->f_op->aio_write(&kiocb, buf, len, kiocb.ki_pos)))
+               wait_on_retry_sync_kiocb(&kiocb);
+
        if (-EIOCBQUEUED == ret)
                ret = wait_on_sync_kiocb(&kiocb);
        *ppos = kiocb.ki_pos;
@@ -242,24 +298,29 @@ EXPORT_SYMBOL(do_sync_write);
 
 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
 {
-       struct inode *inode = file->f_dentry->d_inode;
        ssize_t ret;
 
        if (!(file->f_mode & FMODE_WRITE))
                return -EBADF;
        if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
                return -EINVAL;
+       if (unlikely(!access_ok(VERIFY_READ, buf, count)))
+               return -EFAULT;
 
-       ret = locks_verify_area(FLOCK_VERIFY_WRITE, inode, file, *pos, count);
-       if (!ret) {
+       ret = rw_verify_area(WRITE, file, pos, count);
+       if (ret >= 0) {
+               count = ret;
                ret = security_file_permission (file, MAY_WRITE);
                if (!ret) {
                        if (file->f_op->write)
                                ret = file->f_op->write(file, buf, count, pos);
                        else
                                ret = do_sync_write(file, buf, count, pos);
-                       if (ret > 0)
-                               dnotify_parent(file->f_dentry, DN_MODIFY);
+                       if (ret > 0) {
+                               fsnotify_modify(file->f_dentry);
+                               current->wchar += ret;
+                       }
+                       current->syscw++;
                }
        }
 
@@ -268,6 +329,16 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
 
 EXPORT_SYMBOL(vfs_write);
 
+static inline loff_t file_pos_read(struct file *file)
+{
+       return file->f_pos;
+}
+
+static inline void file_pos_write(struct file *file, loff_t pos)
+{
+       file->f_pos = pos;
+}
+
 asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
 {
        struct file *file;
@@ -276,7 +347,9 @@ asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
 
        file = fget_light(fd, &fput_needed);
        if (file) {
-               ret = vfs_read(file, buf, count, &file->f_pos);
+               loff_t pos = file_pos_read(file);
+               ret = vfs_read(file, buf, count, &pos);
+               file_pos_write(file, pos);
                fput_light(file, fput_needed);
        }
 
@@ -292,13 +365,17 @@ asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t co
 
        file = fget_light(fd, &fput_needed);
        if (file) {
-               ret = vfs_write(file, buf, count, &file->f_pos);
+               loff_t pos = file_pos_read(file);
+               ret = vfs_write(file, buf, count, &pos);
+               file_pos_write(file, pos);
                fput_light(file, fput_needed);
        }
 
        return ret;
 }
 
+EXPORT_SYMBOL_GPL(sys_write);
+
 asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
                             size_t count, loff_t pos)
 {
@@ -311,7 +388,9 @@ asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
 
        file = fget_light(fd, &fput_needed);
        if (file) {
-               ret = vfs_read(file, buf, count, &pos);
+               ret = -ESPIPE;
+               if (file->f_mode & FMODE_PREAD)
+                       ret = vfs_read(file, buf, count, &pos);
                fput_light(file, fput_needed);
        }
 
@@ -330,7 +409,9 @@ asmlinkage ssize_t sys_pwrite64(unsigned int fd, const char __user *buf,
 
        file = fget_light(fd, &fput_needed);
        if (file) {
-               ret = vfs_write(file, buf, count, &pos);
+               ret = -ESPIPE;
+               if (file->f_mode & FMODE_PWRITE)  
+                       ret = vfs_write(file, buf, count, &pos);
                fput_light(file, fput_needed);
        }
 
@@ -359,6 +440,9 @@ unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
 
 EXPORT_SYMBOL(iov_shorten);
 
+/* A write operation does a read from user space and vice versa */
+#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
+
 static ssize_t do_readv_writev(int type, struct file *file,
                               const struct iovec __user * uvector,
                               unsigned long nr_segs, loff_t *pos)
@@ -373,7 +457,6 @@ static ssize_t do_readv_writev(int type, struct file *file,
        int seg;
        io_fn_t fn;
        iov_fn_t fnv;
-       struct inode *inode;
 
        /*
         * SuS says "The readv() function *may* fail if the iovcnt argument
@@ -389,7 +472,7 @@ static ssize_t do_readv_writev(int type, struct file *file,
         * verify all the pointers
         */
        ret = -EINVAL;
-       if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
+       if (nr_segs > UIO_MAXIOV)
                goto out;
        if (!file->f_op)
                goto out;
@@ -413,10 +496,13 @@ static ssize_t do_readv_writev(int type, struct file *file,
        tot_len = 0;
        ret = -EINVAL;
        for (seg = 0; seg < nr_segs; seg++) {
+               void __user *buf = iov[seg].iov_base;
                ssize_t len = (ssize_t)iov[seg].iov_len;
 
                if (len < 0)    /* size_t not fitting an ssize_t .. */
                        goto out;
+               if (unlikely(!access_ok(vrfy_dir(type), buf, len)))
+                       goto Efault;
                tot_len += len;
                if ((ssize_t)tot_len < 0) /* maths overflow on the ssize_t */
                        goto out;
@@ -426,11 +512,10 @@ static ssize_t do_readv_writev(int type, struct file *file,
                goto out;
        }
 
-       inode = file->f_dentry->d_inode;
-       /* VERIFY_WRITE actually means a read, as we write to user space */
-       ret = locks_verify_area((type == READ 
-                                ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE),
-                               inode, file, *pos, tot_len);
+       ret = rw_verify_area(type, file, pos, tot_len);
+       if (ret < 0)
+               goto out;
+       ret = security_file_permission(file, type == READ ? MAY_READ : MAY_WRITE);
        if (ret)
                goto out;
 
@@ -473,10 +558,16 @@ static ssize_t do_readv_writev(int type, struct file *file,
 out:
        if (iov != iovstack)
                kfree(iov);
-       if ((ret + (type == READ)) > 0)
-               dnotify_parent(file->f_dentry,
-                               (type == READ) ? DN_ACCESS : DN_MODIFY);
+       if ((ret + (type == READ)) > 0) {
+               if (type == READ)
+                       fsnotify_access(file->f_dentry);
+               else
+                       fsnotify_modify(file->f_dentry);
+       }
        return ret;
+Efault:
+       ret = -EFAULT;
+       goto out;
 }
 
 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
@@ -514,10 +605,15 @@ sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
 
        file = fget_light(fd, &fput_needed);
        if (file) {
-               ret = vfs_readv(file, vec, vlen, &file->f_pos);
+               loff_t pos = file_pos_read(file);
+               ret = vfs_readv(file, vec, vlen, &pos);
+               file_pos_write(file, pos);
                fput_light(file, fput_needed);
        }
 
+       if (ret > 0)
+               current->rchar += ret;
+       current->syscr++;
        return ret;
 }
 
@@ -530,10 +626,15 @@ sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
 
        file = fget_light(fd, &fput_needed);
        if (file) {
-               ret = vfs_writev(file, vec, vlen, &file->f_pos);
+               loff_t pos = file_pos_read(file);
+               ret = vfs_writev(file, vec, vlen, &pos);
+               file_pos_write(file, pos);
                fput_light(file, fput_needed);
        }
 
+       if (ret > 0)
+               current->wchar += ret;
+       current->syscw++;
        return ret;
 }
 
@@ -561,11 +662,16 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
                goto fput_in;
        if (!in_file->f_op || !in_file->f_op->sendfile)
                goto fput_in;
+       retval = -ESPIPE;
        if (!ppos)
                ppos = &in_file->f_pos;
-       retval = locks_verify_area(FLOCK_VERIFY_READ, in_inode, in_file, *ppos, count);
-       if (retval)
+       else
+               if (!(in_file->f_mode & FMODE_PREAD))
+                       goto fput_in;
+       retval = rw_verify_area(READ, in_file, ppos, count);
+       if (retval < 0)
                goto fput_in;
+       count = retval;
 
        retval = security_file_permission (in_file, MAY_READ);
        if (retval)
@@ -584,9 +690,10 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
        if (!out_file->f_op || !out_file->f_op->sendpage)
                goto fput_out;
        out_inode = out_file->f_dentry->d_inode;
-       retval = locks_verify_area(FLOCK_VERIFY_WRITE, out_inode, out_file, out_file->f_pos, count);
-       if (retval)
+       retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
+       if (retval < 0)
                goto fput_out;
+       count = retval;
 
        retval = security_file_permission (out_file, MAY_WRITE);
        if (retval)
@@ -608,6 +715,13 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
 
        retval = in_file->f_op->sendfile(in_file, ppos, count, file_send_actor, out_file);
 
+       if (retval > 0) {
+               current->rchar += retval;
+               current->wchar += retval;
+       }
+       current->syscr++;
+       current->syscw++;
+
        if (*ppos > max)
                retval = -EOVERFLOW;