Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / fs / 9p / vfs_file.c
index e97c92b..240c306 100644 (file)
@@ -33,6 +33,7 @@
 #include <linux/inet.h>
 #include <linux/list.h>
 #include <linux/pagemap.h>
+#include <linux/utsname.h>
 #include <asm/uaccess.h>
 #include <linux/idr.h>
 #include <net/9p/9p.h>
@@ -44,6 +45,7 @@
 #include "cache.h"
 
 static const struct file_operations v9fs_cached_file_operations;
+static const struct file_operations v9fs_cached_file_operations_dotl;
 
 /**
  * v9fs_file_open - open a file (or directory)
@@ -92,6 +94,8 @@ int v9fs_file_open(struct inode *inode, struct file *file)
                /* enable cached file options */
                if(file->f_op == &v9fs_file_operations)
                        file->f_op = &v9fs_cached_file_operations;
+               else if (file->f_op == &v9fs_file_operations_dotl)
+                       file->f_op = &v9fs_cached_file_operations_dotl;
 
 #ifdef CONFIG_9P_FSCACHE
                v9fs_cache_inode_set_cookie(inode, file);
@@ -130,6 +134,206 @@ static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
        return res;
 }
 
+static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
+{
+       struct p9_flock flock;
+       struct p9_fid *fid;
+       uint8_t status;
+       int res = 0;
+       unsigned char fl_type;
+
+       fid = filp->private_data;
+       BUG_ON(fid == NULL);
+
+       if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
+               BUG();
+
+       res = posix_lock_file_wait(filp, fl);
+       if (res < 0)
+               goto out;
+
+       /* convert posix lock to p9 tlock args */
+       memset(&flock, 0, sizeof(flock));
+       flock.type = fl->fl_type;
+       flock.start = fl->fl_start;
+       if (fl->fl_end == OFFSET_MAX)
+               flock.length = 0;
+       else
+               flock.length = fl->fl_end - fl->fl_start + 1;
+       flock.proc_id = fl->fl_pid;
+       flock.client_id = utsname()->nodename;
+       if (IS_SETLKW(cmd))
+               flock.flags = P9_LOCK_FLAGS_BLOCK;
+
+       /*
+        * if its a blocked request and we get P9_LOCK_BLOCKED as the status
+        * for lock request, keep on trying
+        */
+       for (;;) {
+               res = p9_client_lock_dotl(fid, &flock, &status);
+               if (res < 0)
+                       break;
+
+               if (status != P9_LOCK_BLOCKED)
+                       break;
+               if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
+                       break;
+               schedule_timeout_interruptible(P9_LOCK_TIMEOUT);
+       }
+
+       /* map 9p status to VFS status */
+       switch (status) {
+       case P9_LOCK_SUCCESS:
+               res = 0;
+               break;
+       case P9_LOCK_BLOCKED:
+               res = -EAGAIN;
+               break;
+       case P9_LOCK_ERROR:
+       case P9_LOCK_GRACE:
+               res = -ENOLCK;
+               break;
+       default:
+               BUG();
+       }
+
+       /*
+        * incase server returned error for lock request, revert
+        * it locally
+        */
+       if (res < 0 && fl->fl_type != F_UNLCK) {
+               fl_type = fl->fl_type;
+               fl->fl_type = F_UNLCK;
+               res = posix_lock_file_wait(filp, fl);
+               fl->fl_type = fl_type;
+       }
+out:
+       return res;
+}
+
+static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
+{
+       struct p9_getlock glock;
+       struct p9_fid *fid;
+       int res = 0;
+
+       fid = filp->private_data;
+       BUG_ON(fid == NULL);
+
+       posix_test_lock(filp, fl);
+       /*
+        * if we have a conflicting lock locally, no need to validate
+        * with server
+        */
+       if (fl->fl_type != F_UNLCK)
+               return res;
+
+       /* convert posix lock to p9 tgetlock args */
+       memset(&glock, 0, sizeof(glock));
+       glock.type = fl->fl_type;
+       glock.start = fl->fl_start;
+       if (fl->fl_end == OFFSET_MAX)
+               glock.length = 0;
+       else
+               glock.length = fl->fl_end - fl->fl_start + 1;
+       glock.proc_id = fl->fl_pid;
+       glock.client_id = utsname()->nodename;
+
+       res = p9_client_getlock_dotl(fid, &glock);
+       if (res < 0)
+               return res;
+       if (glock.type != F_UNLCK) {
+               fl->fl_type = glock.type;
+               fl->fl_start = glock.start;
+               if (glock.length == 0)
+                       fl->fl_end = OFFSET_MAX;
+               else
+                       fl->fl_end = glock.start + glock.length - 1;
+               fl->fl_pid = glock.proc_id;
+       } else
+               fl->fl_type = F_UNLCK;
+
+       return res;
+}
+
+/**
+ * v9fs_file_lock_dotl - lock a file (or directory)
+ * @filp: file to be locked
+ * @cmd: lock command
+ * @fl: file lock structure
+ *
+ */
+
+static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
+{
+       struct inode *inode = filp->f_path.dentry->d_inode;
+       int ret = -ENOLCK;
+
+       P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
+                               cmd, fl, filp->f_path.dentry->d_name.name);
+
+       /* No mandatory locks */
+       if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
+               goto out_err;
+
+       if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
+               filemap_write_and_wait(inode->i_mapping);
+               invalidate_mapping_pages(&inode->i_data, 0, -1);
+       }
+
+       if (IS_SETLK(cmd) || IS_SETLKW(cmd))
+               ret = v9fs_file_do_lock(filp, cmd, fl);
+       else if (IS_GETLK(cmd))
+               ret = v9fs_file_getlock(filp, fl);
+       else
+               ret = -EINVAL;
+out_err:
+       return ret;
+}
+
+/**
+ * v9fs_file_flock_dotl - lock a file
+ * @filp: file to be locked
+ * @cmd: lock command
+ * @fl: file lock structure
+ *
+ */
+
+static int v9fs_file_flock_dotl(struct file *filp, int cmd,
+       struct file_lock *fl)
+{
+       struct inode *inode = filp->f_path.dentry->d_inode;
+       int ret = -ENOLCK;
+
+       P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
+                               cmd, fl, filp->f_path.dentry->d_name.name);
+
+       /* No mandatory locks */
+       if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
+               goto out_err;
+
+       if (!(fl->fl_flags & FL_FLOCK))
+               goto out_err;
+
+       if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
+               filemap_write_and_wait(inode->i_mapping);
+               invalidate_mapping_pages(&inode->i_data, 0, -1);
+       }
+       /* Convert flock to posix lock */
+       fl->fl_owner = (fl_owner_t)filp;
+       fl->fl_start = 0;
+       fl->fl_end = OFFSET_MAX;
+       fl->fl_flags |= FL_POSIX;
+       fl->fl_flags ^= FL_FLOCK;
+
+       if (IS_SETLK(cmd) | IS_SETLKW(cmd))
+               ret = v9fs_file_do_lock(filp, cmd, fl);
+       else
+               ret = -EINVAL;
+out_err:
+       return ret;
+}
+
 /**
  * v9fs_file_readn - read from a file
  * @filp: file pointer to read
@@ -219,7 +423,9 @@ static ssize_t
 v9fs_file_write(struct file *filp, const char __user * data,
                size_t count, loff_t * offset)
 {
-       int n, rsize, total = 0;
+       ssize_t retval;
+       size_t total = 0;
+       int n;
        struct p9_fid *fid;
        struct p9_client *clnt;
        struct inode *inode = filp->f_path.dentry->d_inode;
@@ -232,14 +438,19 @@ v9fs_file_write(struct file *filp, const char __user * data,
        fid = filp->private_data;
        clnt = fid->clnt;
 
-       rsize = fid->iounit ? fid->iounit : clnt->msize - P9_IOHDRSZ;
+       retval = generic_write_checks(filp, &origin, &count, 0);
+       if (retval)
+               goto out;
 
-       do {
-               if (count < rsize)
-                       rsize = count;
+       retval = -EINVAL;
+       if ((ssize_t) count < 0)
+               goto out;
+       retval = 0;
+       if (!count)
+               goto out;
 
-               n = p9_client_write(fid, NULL, data+total, origin+total,
-                                                                       rsize);
+       do {
+               n = p9_client_write(fid, NULL, data+total, origin+total, count);
                if (n <= 0)
                        break;
                count -= n;
@@ -258,9 +469,11 @@ v9fs_file_write(struct file *filp, const char __user * data,
        }
 
        if (n < 0)
-               return n;
-
-       return total;
+               retval = n;
+       else
+               retval = total;
+out:
+       return retval;
 }
 
 static int v9fs_file_fsync(struct file *filp, int datasync)
@@ -278,6 +491,20 @@ static int v9fs_file_fsync(struct file *filp, int datasync)
        return retval;
 }
 
+int v9fs_file_fsync_dotl(struct file *filp, int datasync)
+{
+       struct p9_fid *fid;
+       int retval;
+
+       P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
+                       filp, datasync);
+
+       fid = filp->private_data;
+
+       retval = p9_client_fsync(fid, datasync);
+       return retval;
+}
+
 static const struct file_operations v9fs_cached_file_operations = {
        .llseek = generic_file_llseek,
        .read = do_sync_read,
@@ -290,6 +517,19 @@ static const struct file_operations v9fs_cached_file_operations = {
        .fsync = v9fs_file_fsync,
 };
 
+static const struct file_operations v9fs_cached_file_operations_dotl = {
+       .llseek = generic_file_llseek,
+       .read = do_sync_read,
+       .aio_read = generic_file_aio_read,
+       .write = v9fs_file_write,
+       .open = v9fs_file_open,
+       .release = v9fs_dir_release,
+       .lock = v9fs_file_lock_dotl,
+       .flock = v9fs_file_flock_dotl,
+       .mmap = generic_file_readonly_mmap,
+       .fsync = v9fs_file_fsync_dotl,
+};
+
 const struct file_operations v9fs_file_operations = {
        .llseek = generic_file_llseek,
        .read = v9fs_file_read,
@@ -307,7 +547,8 @@ const struct file_operations v9fs_file_operations_dotl = {
        .write = v9fs_file_write,
        .open = v9fs_file_open,
        .release = v9fs_dir_release,
-       .lock = v9fs_file_lock,
+       .lock = v9fs_file_lock_dotl,
+       .flock = v9fs_file_flock_dotl,
        .mmap = generic_file_readonly_mmap,
-       .fsync = v9fs_file_fsync,
+       .fsync = v9fs_file_fsync_dotl,
 };