vserver 2.0 rc7
[linux-2.6.git] / fs / nfs / file.c
index 57ccbd2..55c9075 100644 (file)
@@ -37,6 +37,7 @@
 
 static int nfs_file_open(struct inode *, struct file *);
 static int nfs_file_release(struct inode *, struct file *);
+static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
 static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
 static ssize_t nfs_file_sendfile(struct file *, loff_t *, size_t, read_actor_t, void *);
 static ssize_t nfs_file_read(struct kiocb *, char __user *, size_t, loff_t);
@@ -44,9 +45,11 @@ static ssize_t nfs_file_write(struct kiocb *, const char __user *, size_t, loff_
 static int  nfs_file_flush(struct file *);
 static int  nfs_fsync(struct file *, struct dentry *dentry, int datasync);
 static int nfs_check_flags(int flags);
+static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
+static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
 
 struct file_operations nfs_file_operations = {
-       .llseek         = remote_llseek,
+       .llseek         = nfs_file_llseek,
        .read           = do_sync_read,
        .write          = do_sync_write,
        .aio_read               = nfs_file_read,
@@ -57,6 +60,7 @@ struct file_operations nfs_file_operations = {
        .release        = nfs_file_release,
        .fsync          = nfs_fsync,
        .lock           = nfs_lock,
+       .flock          = nfs_flock,
        .sendfile       = nfs_file_sendfile,
        .check_flags    = nfs_check_flags,
 };
@@ -105,9 +109,51 @@ nfs_file_open(struct inode *inode, struct file *filp)
 static int
 nfs_file_release(struct inode *inode, struct file *filp)
 {
+       /* Ensure that dirty pages are flushed out with the right creds */
+       if (filp->f_mode & FMODE_WRITE)
+               filemap_fdatawrite(filp->f_mapping);
        return NFS_PROTO(inode)->file_release(inode, filp);
 }
 
+/**
+ * nfs_revalidate_size - Revalidate the file size
+ * @inode - pointer to inode struct
+ * @file - pointer to struct file
+ *
+ * Revalidates the file length. This is basically a wrapper around
+ * nfs_revalidate_inode() that takes into account the fact that we may
+ * have cached writes (in which case we don't care about the server's
+ * idea of what the file length is), or O_DIRECT (in which case we
+ * shouldn't trust the cache).
+ */
+static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
+{
+       struct nfs_server *server = NFS_SERVER(inode);
+       struct nfs_inode *nfsi = NFS_I(inode);
+
+       if (server->flags & NFS_MOUNT_NOAC)
+               goto force_reval;
+       if (filp->f_flags & O_DIRECT)
+               goto force_reval;
+       if (nfsi->npages != 0)
+               return 0;
+       return nfs_revalidate_inode(server, inode);
+force_reval:
+       return __nfs_revalidate_inode(server, inode);
+}
+
+static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
+{
+       /* origin == SEEK_END => we must revalidate the cached file length */
+       if (origin == 2) {
+               struct inode *inode = filp->f_mapping->host;
+               int retval = nfs_revalidate_file_size(inode, filp);
+               if (retval < 0)
+                       return (loff_t)retval;
+       }
+       return remote_llseek(filp, offset, origin);
+}
+
 /*
  * Flush all dirty pages, and check for write errors.
  *
@@ -312,6 +358,25 @@ static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
        return status;
 }
 
+static int do_vfs_lock(struct file *file, struct file_lock *fl)
+{
+       int res = 0;
+       switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
+               case FL_POSIX:
+                       res = posix_lock_file_wait(file, fl);
+                       break;
+               case FL_FLOCK:
+                       res = flock_lock_file_wait(file, fl);
+                       break;
+               default:
+                       BUG();
+       }
+       if (res < 0)
+               printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
+                               __FUNCTION__);
+       return res;
+}
+
 static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
 {
        struct inode *inode = filp->f_mapping->host;
@@ -338,7 +403,7 @@ static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
        if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
                status = NFS_PROTO(inode)->lock(filp, cmd, fl);
        else
-               status = posix_lock_file_wait(filp, fl);
+               status = do_vfs_lock(filp, fl);
        unlock_kernel();
        rpc_clnt_sigunmask(NFS_CLIENT(inode), &oldset);
        return status;
@@ -377,9 +442,9 @@ static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
                 * the process exits.
                 */
                if (status == -EINTR || status == -ERESTARTSYS)
-                       posix_lock_file_wait(filp, fl);
+                       do_vfs_lock(filp, fl);
        } else
-               status = posix_lock_file_wait(filp, fl);
+               status = do_vfs_lock(filp, fl);
        unlock_kernel();
        if (status < 0)
                goto out;
@@ -401,8 +466,7 @@ out:
 /*
  * Lock a (portion of) a file
  */
-int
-nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
+static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
 {
        struct inode * inode = filp->f_mapping->host;
 
@@ -418,6 +482,27 @@ nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
        if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
                return -ENOLCK;
 
+       if (IS_GETLK(cmd))
+               return do_getlk(filp, cmd, fl);
+       if (fl->fl_type == F_UNLCK)
+               return do_unlk(filp, cmd, fl);
+       return do_setlk(filp, cmd, fl);
+}
+
+/*
+ * Lock a (portion of) a file
+ */
+static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
+{
+       struct inode * inode = filp->f_mapping->host;
+
+       dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n",
+                       inode->i_sb->s_id, inode->i_ino,
+                       fl->fl_type, fl->fl_flags);
+
+       if (!inode)
+               return -EINVAL;
+
        /*
         * No BSD flocks over NFS allowed.
         * Note: we could try to fake a POSIX lock request here by
@@ -425,11 +510,14 @@ nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
         * Not sure whether that would be unique, though, or whether
         * that would break in other places.
         */
-       if (!fl->fl_owner || !(fl->fl_flags & FL_POSIX))
+       if (!(fl->fl_flags & FL_FLOCK))
                return -ENOLCK;
 
-       if (IS_GETLK(cmd))
-               return do_getlk(filp, cmd, fl);
+       /* We're simulating flock() locks using posix locks on the server */
+       fl->fl_owner = (fl_owner_t)filp;
+       fl->fl_start = 0;
+       fl->fl_end = OFFSET_MAX;
+
        if (fl->fl_type == F_UNLCK)
                return do_unlk(filp, cmd, fl);
        return do_setlk(filp, cmd, fl);