Merge tag 'sirf-dts-for-3.16' of git://git.kernel.org/pub/scm/linux/kernel/git/baohua...
[firefly-linux-kernel-4.4.55.git] / fs / ceph / locks.c
index ae6d14e82b0f439153b5e62adfd4d0c2ebed73a1..d94ba0df9f4d195cabf677fcdcd41cc01096c7e7 100644 (file)
@@ -2,11 +2,31 @@
 
 #include <linux/file.h>
 #include <linux/namei.h>
+#include <linux/random.h>
 
 #include "super.h"
 #include "mds_client.h"
 #include <linux/ceph/pagelist.h>
 
+static u64 lock_secret;
+
+static inline u64 secure_addr(void *addr)
+{
+       u64 v = lock_secret ^ (u64)(unsigned long)addr;
+       /*
+        * Set the most significant bit, so that MDS knows the 'owner'
+        * is sufficient to identify the owner of lock. (old code uses
+        * both 'owner' and 'pid')
+        */
+       v |= (1ULL << 63);
+       return v;
+}
+
+void __init ceph_flock_init(void)
+{
+       get_random_bytes(&lock_secret, sizeof(lock_secret));
+}
+
 /**
  * Implement fcntl and flock locking functions.
  */
@@ -14,11 +34,11 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
                             int cmd, u8 wait, struct file_lock *fl)
 {
        struct inode *inode = file_inode(file);
-       struct ceph_mds_client *mdsc =
-               ceph_sb_to_client(inode->i_sb)->mdsc;
+       struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
        struct ceph_mds_request *req;
        int err;
        u64 length = 0;
+       u64 owner;
 
        req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
        if (IS_ERR(req))
@@ -32,25 +52,27 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
        else
                length = fl->fl_end - fl->fl_start + 1;
 
-       dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
-            "length: %llu, wait: %d, type: %d", (int)lock_type,
-            (int)operation, (u64)fl->fl_pid, fl->fl_start,
-            length, wait, fl->fl_type);
+       if (lock_type == CEPH_LOCK_FCNTL)
+               owner = secure_addr(fl->fl_owner);
+       else
+               owner = secure_addr(fl->fl_file);
+
+       dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
+            "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type,
+            (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length,
+            wait, fl->fl_type);
 
        req->r_args.filelock_change.rule = lock_type;
        req->r_args.filelock_change.type = cmd;
+       req->r_args.filelock_change.owner = cpu_to_le64(owner);
        req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
-       /* This should be adjusted, but I'm not sure if
-          namespaces actually get id numbers*/
-       req->r_args.filelock_change.pid_namespace =
-               cpu_to_le64((u64)(unsigned long)fl->fl_nspid);
        req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
        req->r_args.filelock_change.length = cpu_to_le64(length);
        req->r_args.filelock_change.wait = wait;
 
        err = ceph_mdsc_do_request(mdsc, inode, req);
 
-       if ( operation == CEPH_MDS_OP_GETFILELOCK){
+       if (operation == CEPH_MDS_OP_GETFILELOCK) {
                fl->fl_pid = le64_to_cpu(req->r_reply_info.filelock_reply->pid);
                if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
                        fl->fl_type = F_RDLCK;
@@ -87,14 +109,19 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
        u8 wait = 0;
        u16 op = CEPH_MDS_OP_SETFILELOCK;
 
-       fl->fl_nspid = get_pid(task_tgid(current));
-       dout("ceph_lock, fl_pid:%d", fl->fl_pid);
+       if (!(fl->fl_flags & FL_POSIX))
+               return -ENOLCK;
+       /* No mandatory locks */
+       if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
+               return -ENOLCK;
+
+       dout("ceph_lock, fl_owner: %p", fl->fl_owner);
 
        /* set wait bit as appropriate, then make command as Ceph expects it*/
-       if (F_SETLKW == cmd)
-               wait = 1;
-       if (F_GETLK == cmd)
+       if (IS_GETLK(cmd))
                op = CEPH_MDS_OP_GETFILELOCK;
+       else if (IS_SETLKW(cmd))
+               wait = 1;
 
        if (F_RDLCK == fl->fl_type)
                lock_cmd = CEPH_LOCK_SHARED;
@@ -105,7 +132,7 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
 
        err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl);
        if (!err) {
-               if ( op != CEPH_MDS_OP_GETFILELOCK ){
+               if (op != CEPH_MDS_OP_GETFILELOCK) {
                        dout("mds locked, locking locally");
                        err = posix_lock_file(file, fl, NULL);
                        if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
@@ -131,20 +158,22 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
 {
        u8 lock_cmd;
        int err;
-       u8 wait = 1;
-
-       fl->fl_nspid = get_pid(task_tgid(current));
-       dout("ceph_flock, fl_pid:%d", fl->fl_pid);
-
-       /* set wait bit, then clear it out of cmd*/
-       if (cmd & LOCK_NB)
-               wait = 0;
-       cmd = cmd & (LOCK_SH | LOCK_EX | LOCK_UN);
-       /* set command sequence that Ceph wants to see:
-          shared lock, exclusive lock, or unlock */
-       if (LOCK_SH == cmd)
+       u8 wait = 0;
+
+       if (!(fl->fl_flags & FL_FLOCK))
+               return -ENOLCK;
+       /* No mandatory locks */
+       if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
+               return -ENOLCK;
+
+       dout("ceph_flock, fl_file: %p", fl->fl_file);
+
+       if (IS_SETLKW(cmd))
+               wait = 1;
+
+       if (F_RDLCK == fl->fl_type)
                lock_cmd = CEPH_LOCK_SHARED;
-       else if (LOCK_EX == cmd)
+       else if (F_WRLCK == fl->fl_type)
                lock_cmd = CEPH_LOCK_EXCL;
        else
                lock_cmd = CEPH_LOCK_UNLOCK;
@@ -280,13 +309,14 @@ int lock_to_ceph_filelock(struct file_lock *lock,
                          struct ceph_filelock *cephlock)
 {
        int err = 0;
-
        cephlock->start = cpu_to_le64(lock->fl_start);
        cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
        cephlock->client = cpu_to_le64(0);
-       cephlock->pid = cpu_to_le64(lock->fl_pid);
-       cephlock->pid_namespace =
-               cpu_to_le64((u64)(unsigned long)lock->fl_nspid);
+       cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
+       if (lock->fl_flags & FL_POSIX)
+               cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
+       else
+               cephlock->owner = cpu_to_le64(secure_addr(lock->fl_file));
 
        switch (lock->fl_type) {
        case F_RDLCK: