Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 2 | |
| 3 | #include <linux/file.h> |
| 4 | #include <linux/namei.h> |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 5 | #include <linux/random.h> |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 6 | |
| 7 | #include "super.h" |
| 8 | #include "mds_client.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include <linux/ceph/pagelist.h> |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 10 | |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 11 | static u64 lock_secret; |
| 12 | |
| 13 | static inline u64 secure_addr(void *addr) |
| 14 | { |
| 15 | u64 v = lock_secret ^ (u64)(unsigned long)addr; |
| 16 | /* |
| 17 | * Set the most significant bit, so that MDS knows the 'owner' |
| 18 | * is sufficient to identify the owner of lock. (old code uses |
| 19 | * both 'owner' and 'pid') |
| 20 | */ |
| 21 | v |= (1ULL << 63); |
| 22 | return v; |
| 23 | } |
| 24 | |
| 25 | void __init ceph_flock_init(void) |
| 26 | { |
| 27 | get_random_bytes(&lock_secret, sizeof(lock_secret)); |
| 28 | } |
| 29 | |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Implement fcntl and flock locking functions. |
| 32 | */ |
| 33 | static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 34 | int cmd, u8 wait, struct file_lock *fl) |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 35 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 36 | struct inode *inode = file_inode(file); |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 37 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 38 | struct ceph_mds_request *req; |
| 39 | int err; |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 40 | u64 length = 0; |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 41 | u64 owner; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 42 | |
| 43 | req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS); |
| 44 | if (IS_ERR(req)) |
| 45 | return PTR_ERR(req); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 46 | req->r_inode = inode; |
| 47 | ihold(inode); |
Yan, Zheng | 3bd5814 | 2014-04-27 09:17:45 +0800 | [diff] [blame] | 48 | req->r_num_caps = 1; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 49 | |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 50 | /* mds requires start and length rather than start and end */ |
| 51 | if (LLONG_MAX == fl->fl_end) |
| 52 | length = 0; |
| 53 | else |
| 54 | length = fl->fl_end - fl->fl_start + 1; |
| 55 | |
Jeff Layton | 130d1f9 | 2014-05-09 14:13:04 -0400 | [diff] [blame] | 56 | owner = secure_addr(fl->fl_owner); |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 57 | |
| 58 | dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, " |
| 59 | "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type, |
| 60 | (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length, |
| 61 | wait, fl->fl_type); |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 62 | |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 63 | req->r_args.filelock_change.rule = lock_type; |
| 64 | req->r_args.filelock_change.type = cmd; |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 65 | req->r_args.filelock_change.owner = cpu_to_le64(owner); |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 66 | req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid); |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 67 | req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 68 | req->r_args.filelock_change.length = cpu_to_le64(length); |
| 69 | req->r_args.filelock_change.wait = wait; |
| 70 | |
| 71 | err = ceph_mdsc_do_request(mdsc, inode, req); |
Herb Shiu | a5b1062 | 2010-11-23 13:58:29 -0800 | [diff] [blame] | 72 | |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 73 | if (operation == CEPH_MDS_OP_GETFILELOCK) { |
Herb Shiu | a5b1062 | 2010-11-23 13:58:29 -0800 | [diff] [blame] | 74 | fl->fl_pid = le64_to_cpu(req->r_reply_info.filelock_reply->pid); |
| 75 | if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type) |
| 76 | fl->fl_type = F_RDLCK; |
| 77 | else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type) |
| 78 | fl->fl_type = F_WRLCK; |
| 79 | else |
| 80 | fl->fl_type = F_UNLCK; |
| 81 | |
| 82 | fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start); |
| 83 | length = le64_to_cpu(req->r_reply_info.filelock_reply->start) + |
| 84 | le64_to_cpu(req->r_reply_info.filelock_reply->length); |
| 85 | if (length >= 1) |
| 86 | fl->fl_end = length -1; |
| 87 | else |
| 88 | fl->fl_end = 0; |
| 89 | |
| 90 | } |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 91 | ceph_mdsc_put_request(req); |
| 92 | dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, " |
Sage Weil | 0c1f91f | 2011-05-25 14:56:12 -0700 | [diff] [blame] | 93 | "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 94 | (int)operation, (u64)fl->fl_pid, fl->fl_start, |
| 95 | length, wait, fl->fl_type, err); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 96 | return err; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Attempt to set an fcntl lock. |
| 101 | * For now, this just goes away to the server. Later it may be more awesome. |
| 102 | */ |
| 103 | int ceph_lock(struct file *file, int cmd, struct file_lock *fl) |
| 104 | { |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 105 | u8 lock_cmd; |
| 106 | int err; |
| 107 | u8 wait = 0; |
| 108 | u16 op = CEPH_MDS_OP_SETFILELOCK; |
| 109 | |
Yan, Zheng | eb70c0c | 2014-03-04 15:50:06 +0800 | [diff] [blame] | 110 | if (!(fl->fl_flags & FL_POSIX)) |
| 111 | return -ENOLCK; |
| 112 | /* No mandatory locks */ |
| 113 | if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK) |
| 114 | return -ENOLCK; |
| 115 | |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 116 | dout("ceph_lock, fl_owner: %p", fl->fl_owner); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 117 | |
| 118 | /* set wait bit as appropriate, then make command as Ceph expects it*/ |
Yan, Zheng | 0e8e95d | 2014-03-04 15:42:24 +0800 | [diff] [blame] | 119 | if (IS_GETLK(cmd)) |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 120 | op = CEPH_MDS_OP_GETFILELOCK; |
Yan, Zheng | 0e8e95d | 2014-03-04 15:42:24 +0800 | [diff] [blame] | 121 | else if (IS_SETLKW(cmd)) |
| 122 | wait = 1; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 123 | |
| 124 | if (F_RDLCK == fl->fl_type) |
| 125 | lock_cmd = CEPH_LOCK_SHARED; |
| 126 | else if (F_WRLCK == fl->fl_type) |
| 127 | lock_cmd = CEPH_LOCK_EXCL; |
| 128 | else |
| 129 | lock_cmd = CEPH_LOCK_UNLOCK; |
| 130 | |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 131 | err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 132 | if (!err) { |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 133 | if (op != CEPH_MDS_OP_GETFILELOCK) { |
Herb Shiu | a5b1062 | 2010-11-23 13:58:29 -0800 | [diff] [blame] | 134 | dout("mds locked, locking locally"); |
| 135 | err = posix_lock_file(file, fl, NULL); |
| 136 | if (err && (CEPH_MDS_OP_SETFILELOCK == op)) { |
Sage Weil | 0c1f91f | 2011-05-25 14:56:12 -0700 | [diff] [blame] | 137 | /* undo! This should only happen if |
| 138 | * the kernel detects local |
| 139 | * deadlock. */ |
Herb Shiu | a5b1062 | 2010-11-23 13:58:29 -0800 | [diff] [blame] | 140 | ceph_lock_message(CEPH_LOCK_FCNTL, op, file, |
| 141 | CEPH_LOCK_UNLOCK, 0, fl); |
Sage Weil | 0c1f91f | 2011-05-25 14:56:12 -0700 | [diff] [blame] | 142 | dout("got %d on posix_lock_file, undid lock", |
| 143 | err); |
Herb Shiu | a5b1062 | 2010-11-23 13:58:29 -0800 | [diff] [blame] | 144 | } |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 145 | } |
Herb Shiu | a5b1062 | 2010-11-23 13:58:29 -0800 | [diff] [blame] | 146 | |
Sage Weil | 0c1f91f | 2011-05-25 14:56:12 -0700 | [diff] [blame] | 147 | } else if (err == -ERESTARTSYS) { |
| 148 | dout("undoing lock\n"); |
| 149 | ceph_lock_message(CEPH_LOCK_FCNTL, op, file, |
| 150 | CEPH_LOCK_UNLOCK, 0, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 151 | } |
| 152 | return err; |
| 153 | } |
| 154 | |
| 155 | int ceph_flock(struct file *file, int cmd, struct file_lock *fl) |
| 156 | { |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 157 | u8 lock_cmd; |
| 158 | int err; |
Yan, Zheng | 0e8e95d | 2014-03-04 15:42:24 +0800 | [diff] [blame] | 159 | u8 wait = 0; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 160 | |
Yan, Zheng | eb70c0c | 2014-03-04 15:50:06 +0800 | [diff] [blame] | 161 | if (!(fl->fl_flags & FL_FLOCK)) |
| 162 | return -ENOLCK; |
| 163 | /* No mandatory locks */ |
| 164 | if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK) |
| 165 | return -ENOLCK; |
| 166 | |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 167 | dout("ceph_flock, fl_file: %p", fl->fl_file); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 168 | |
Yan, Zheng | 0e8e95d | 2014-03-04 15:42:24 +0800 | [diff] [blame] | 169 | if (IS_SETLKW(cmd)) |
| 170 | wait = 1; |
| 171 | |
| 172 | if (F_RDLCK == fl->fl_type) |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 173 | lock_cmd = CEPH_LOCK_SHARED; |
Yan, Zheng | 0e8e95d | 2014-03-04 15:42:24 +0800 | [diff] [blame] | 174 | else if (F_WRLCK == fl->fl_type) |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 175 | lock_cmd = CEPH_LOCK_EXCL; |
| 176 | else |
| 177 | lock_cmd = CEPH_LOCK_UNLOCK; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 178 | |
| 179 | err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 180 | file, lock_cmd, wait, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 181 | if (!err) { |
| 182 | err = flock_lock_file_wait(file, fl); |
| 183 | if (err) { |
| 184 | ceph_lock_message(CEPH_LOCK_FLOCK, |
| 185 | CEPH_MDS_OP_SETFILELOCK, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame] | 186 | file, CEPH_LOCK_UNLOCK, 0, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 187 | dout("got %d on flock_lock_file_wait, undid lock", err); |
| 188 | } |
Sage Weil | 0c1f91f | 2011-05-25 14:56:12 -0700 | [diff] [blame] | 189 | } else if (err == -ERESTARTSYS) { |
| 190 | dout("undoing lock\n"); |
| 191 | ceph_lock_message(CEPH_LOCK_FLOCK, |
| 192 | CEPH_MDS_OP_SETFILELOCK, |
| 193 | file, CEPH_LOCK_UNLOCK, 0, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 194 | } |
| 195 | return err; |
| 196 | } |
| 197 | |
| 198 | /** |
Jim Schutt | 4d1bf79 | 2013-05-15 10:38:12 -0600 | [diff] [blame] | 199 | * Must be called with lock_flocks() already held. Fills in the passed |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 200 | * counter variables, so you can prepare pagelist metadata before calling |
| 201 | * ceph_encode_locks. |
| 202 | */ |
| 203 | void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count) |
| 204 | { |
| 205 | struct file_lock *lock; |
| 206 | |
| 207 | *fcntl_count = 0; |
| 208 | *flock_count = 0; |
| 209 | |
| 210 | for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { |
| 211 | if (lock->fl_flags & FL_POSIX) |
| 212 | ++(*fcntl_count); |
| 213 | else if (lock->fl_flags & FL_FLOCK) |
| 214 | ++(*flock_count); |
| 215 | } |
| 216 | dout("counted %d flock locks and %d fcntl locks", |
| 217 | *flock_count, *fcntl_count); |
| 218 | } |
| 219 | |
| 220 | /** |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 221 | * Encode the flock and fcntl locks for the given inode into the ceph_filelock |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 222 | * array. Must be called with inode->i_lock already held. |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 223 | * If we encounter more of a specific lock type than expected, return -ENOSPC. |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 224 | */ |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 225 | int ceph_encode_locks_to_buffer(struct inode *inode, |
| 226 | struct ceph_filelock *flocks, |
| 227 | int num_fcntl_locks, int num_flock_locks) |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 228 | { |
| 229 | struct file_lock *lock; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 230 | int err = 0; |
Greg Farnum | fca4451 | 2010-09-17 10:24:02 -0700 | [diff] [blame] | 231 | int seen_fcntl = 0; |
| 232 | int seen_flock = 0; |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 233 | int l = 0; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 234 | |
| 235 | dout("encoding %d flock and %d fcntl locks", num_flock_locks, |
| 236 | num_fcntl_locks); |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 237 | |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 238 | for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { |
| 239 | if (lock->fl_flags & FL_POSIX) { |
Greg Farnum | fca4451 | 2010-09-17 10:24:02 -0700 | [diff] [blame] | 240 | ++seen_fcntl; |
| 241 | if (seen_fcntl > num_fcntl_locks) { |
| 242 | err = -ENOSPC; |
| 243 | goto fail; |
| 244 | } |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 245 | err = lock_to_ceph_filelock(lock, &flocks[l]); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 246 | if (err) |
| 247 | goto fail; |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 248 | ++l; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 249 | } |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 250 | } |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 251 | for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { |
| 252 | if (lock->fl_flags & FL_FLOCK) { |
Greg Farnum | fca4451 | 2010-09-17 10:24:02 -0700 | [diff] [blame] | 253 | ++seen_flock; |
| 254 | if (seen_flock > num_flock_locks) { |
| 255 | err = -ENOSPC; |
| 256 | goto fail; |
| 257 | } |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 258 | err = lock_to_ceph_filelock(lock, &flocks[l]); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 259 | if (err) |
| 260 | goto fail; |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 261 | ++l; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 262 | } |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 263 | } |
| 264 | fail: |
| 265 | return err; |
| 266 | } |
| 267 | |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 268 | /** |
| 269 | * Copy the encoded flock and fcntl locks into the pagelist. |
| 270 | * Format is: #fcntl locks, sequential fcntl locks, #flock locks, |
| 271 | * sequential flock locks. |
| 272 | * Returns zero on success. |
| 273 | */ |
| 274 | int ceph_locks_to_pagelist(struct ceph_filelock *flocks, |
| 275 | struct ceph_pagelist *pagelist, |
| 276 | int num_fcntl_locks, int num_flock_locks) |
| 277 | { |
| 278 | int err = 0; |
| 279 | __le32 nlocks; |
| 280 | |
| 281 | nlocks = cpu_to_le32(num_fcntl_locks); |
| 282 | err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks)); |
| 283 | if (err) |
| 284 | goto out_fail; |
| 285 | |
| 286 | err = ceph_pagelist_append(pagelist, flocks, |
| 287 | num_fcntl_locks * sizeof(*flocks)); |
| 288 | if (err) |
| 289 | goto out_fail; |
| 290 | |
| 291 | nlocks = cpu_to_le32(num_flock_locks); |
| 292 | err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks)); |
| 293 | if (err) |
| 294 | goto out_fail; |
| 295 | |
| 296 | err = ceph_pagelist_append(pagelist, |
| 297 | &flocks[num_fcntl_locks], |
| 298 | num_flock_locks * sizeof(*flocks)); |
| 299 | out_fail: |
| 300 | return err; |
| 301 | } |
| 302 | |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 303 | /* |
| 304 | * Given a pointer to a lock, convert it to a ceph filelock |
| 305 | */ |
| 306 | int lock_to_ceph_filelock(struct file_lock *lock, |
| 307 | struct ceph_filelock *cephlock) |
| 308 | { |
| 309 | int err = 0; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 310 | cephlock->start = cpu_to_le64(lock->fl_start); |
| 311 | cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1); |
| 312 | cephlock->client = cpu_to_le64(0); |
Yan, Zheng | eb13e83 | 2014-03-09 23:16:40 +0800 | [diff] [blame] | 313 | cephlock->pid = cpu_to_le64((u64)lock->fl_pid); |
Jeff Layton | 130d1f9 | 2014-05-09 14:13:04 -0400 | [diff] [blame] | 314 | cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner)); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 315 | |
| 316 | switch (lock->fl_type) { |
| 317 | case F_RDLCK: |
| 318 | cephlock->type = CEPH_LOCK_SHARED; |
| 319 | break; |
| 320 | case F_WRLCK: |
| 321 | cephlock->type = CEPH_LOCK_EXCL; |
| 322 | break; |
| 323 | case F_UNLCK: |
| 324 | cephlock->type = CEPH_LOCK_UNLOCK; |
| 325 | break; |
| 326 | default: |
| 327 | dout("Have unknown lock type %d", lock->fl_type); |
| 328 | err = -EINVAL; |
| 329 | } |
| 330 | |
| 331 | return err; |
| 332 | } |