blob: fbc39c47bacd17150581e2741531c610e5e89f56 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Greg Farnum40819f62010-08-02 15:34:23 -07002
3#include <linux/file.h>
4#include <linux/namei.h>
Yan, Zhengeb13e832014-03-09 23:16:40 +08005#include <linux/random.h>
Greg Farnum40819f62010-08-02 15:34:23 -07006
7#include "super.h"
8#include "mds_client.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/ceph/pagelist.h>
Greg Farnum40819f62010-08-02 15:34:23 -070010
Yan, Zhengeb13e832014-03-09 23:16:40 +080011static u64 lock_secret;
12
13static 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
25void __init ceph_flock_init(void)
26{
27 get_random_bytes(&lock_secret, sizeof(lock_secret));
28}
29
Greg Farnum40819f62010-08-02 15:34:23 -070030/**
31 * Implement fcntl and flock locking functions.
32 */
33static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
Herb Shiu637ae8d2010-11-23 13:42:23 -080034 int cmd, u8 wait, struct file_lock *fl)
Greg Farnum40819f62010-08-02 15:34:23 -070035{
Al Viro496ad9a2013-01-23 17:07:38 -050036 struct inode *inode = file_inode(file);
Yan, Zhengeb13e832014-03-09 23:16:40 +080037 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Greg Farnum40819f62010-08-02 15:34:23 -070038 struct ceph_mds_request *req;
39 int err;
Herb Shiu637ae8d2010-11-23 13:42:23 -080040 u64 length = 0;
Yan, Zhengeb13e832014-03-09 23:16:40 +080041 u64 owner;
Greg Farnum40819f62010-08-02 15:34:23 -070042
43 req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
44 if (IS_ERR(req))
45 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -070046 req->r_inode = inode;
47 ihold(inode);
Yan, Zheng3bd58142014-04-27 09:17:45 +080048 req->r_num_caps = 1;
Greg Farnum40819f62010-08-02 15:34:23 -070049
Herb Shiu637ae8d2010-11-23 13:42:23 -080050 /* 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 Layton130d1f92014-05-09 14:13:04 -040056 owner = secure_addr(fl->fl_owner);
Yan, Zhengeb13e832014-03-09 23:16:40 +080057
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 Shiu637ae8d2010-11-23 13:42:23 -080062
Greg Farnum40819f62010-08-02 15:34:23 -070063 req->r_args.filelock_change.rule = lock_type;
64 req->r_args.filelock_change.type = cmd;
Yan, Zhengeb13e832014-03-09 23:16:40 +080065 req->r_args.filelock_change.owner = cpu_to_le64(owner);
Herb Shiu637ae8d2010-11-23 13:42:23 -080066 req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
Herb Shiu637ae8d2010-11-23 13:42:23 -080067 req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
Greg Farnum40819f62010-08-02 15:34:23 -070068 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 Shiua5b10622010-11-23 13:58:29 -080072
Yan, Zhengeb13e832014-03-09 23:16:40 +080073 if (operation == CEPH_MDS_OP_GETFILELOCK) {
Herb Shiua5b10622010-11-23 13:58:29 -080074 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 Farnum40819f62010-08-02 15:34:23 -070091 ceph_mdsc_put_request(req);
92 dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
Sage Weil0c1f91f2011-05-25 14:56:12 -070093 "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type,
Herb Shiu637ae8d2010-11-23 13:42:23 -080094 (int)operation, (u64)fl->fl_pid, fl->fl_start,
95 length, wait, fl->fl_type, err);
Greg Farnum40819f62010-08-02 15:34:23 -070096 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 */
103int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
104{
Greg Farnum40819f62010-08-02 15:34:23 -0700105 u8 lock_cmd;
106 int err;
107 u8 wait = 0;
108 u16 op = CEPH_MDS_OP_SETFILELOCK;
109
Yan, Zhengeb70c0c2014-03-04 15:50:06 +0800110 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, Zhengeb13e832014-03-09 23:16:40 +0800116 dout("ceph_lock, fl_owner: %p", fl->fl_owner);
Greg Farnum40819f62010-08-02 15:34:23 -0700117
118 /* set wait bit as appropriate, then make command as Ceph expects it*/
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800119 if (IS_GETLK(cmd))
Greg Farnum40819f62010-08-02 15:34:23 -0700120 op = CEPH_MDS_OP_GETFILELOCK;
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800121 else if (IS_SETLKW(cmd))
122 wait = 1;
Greg Farnum40819f62010-08-02 15:34:23 -0700123
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 Shiu637ae8d2010-11-23 13:42:23 -0800131 err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700132 if (!err) {
Yan, Zhengeb13e832014-03-09 23:16:40 +0800133 if (op != CEPH_MDS_OP_GETFILELOCK) {
Herb Shiua5b10622010-11-23 13:58:29 -0800134 dout("mds locked, locking locally");
135 err = posix_lock_file(file, fl, NULL);
136 if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
Sage Weil0c1f91f2011-05-25 14:56:12 -0700137 /* undo! This should only happen if
138 * the kernel detects local
139 * deadlock. */
Herb Shiua5b10622010-11-23 13:58:29 -0800140 ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
141 CEPH_LOCK_UNLOCK, 0, fl);
Sage Weil0c1f91f2011-05-25 14:56:12 -0700142 dout("got %d on posix_lock_file, undid lock",
143 err);
Herb Shiua5b10622010-11-23 13:58:29 -0800144 }
Greg Farnum40819f62010-08-02 15:34:23 -0700145 }
Herb Shiua5b10622010-11-23 13:58:29 -0800146
Sage Weil0c1f91f2011-05-25 14:56:12 -0700147 } 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 Farnum40819f62010-08-02 15:34:23 -0700151 }
152 return err;
153}
154
155int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
156{
Greg Farnum40819f62010-08-02 15:34:23 -0700157 u8 lock_cmd;
158 int err;
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800159 u8 wait = 0;
Greg Farnum40819f62010-08-02 15:34:23 -0700160
Yan, Zhengeb70c0c2014-03-04 15:50:06 +0800161 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, Zhengeb13e832014-03-09 23:16:40 +0800167 dout("ceph_flock, fl_file: %p", fl->fl_file);
Greg Farnum40819f62010-08-02 15:34:23 -0700168
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800169 if (IS_SETLKW(cmd))
170 wait = 1;
171
172 if (F_RDLCK == fl->fl_type)
Greg Farnum40819f62010-08-02 15:34:23 -0700173 lock_cmd = CEPH_LOCK_SHARED;
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800174 else if (F_WRLCK == fl->fl_type)
Greg Farnum40819f62010-08-02 15:34:23 -0700175 lock_cmd = CEPH_LOCK_EXCL;
176 else
177 lock_cmd = CEPH_LOCK_UNLOCK;
Greg Farnum40819f62010-08-02 15:34:23 -0700178
179 err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
Herb Shiu637ae8d2010-11-23 13:42:23 -0800180 file, lock_cmd, wait, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700181 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 Shiu637ae8d2010-11-23 13:42:23 -0800186 file, CEPH_LOCK_UNLOCK, 0, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700187 dout("got %d on flock_lock_file_wait, undid lock", err);
188 }
Sage Weil0c1f91f2011-05-25 14:56:12 -0700189 } 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 Farnum40819f62010-08-02 15:34:23 -0700194 }
195 return err;
196}
197
198/**
Jim Schutt4d1bf792013-05-15 10:38:12 -0600199 * Must be called with lock_flocks() already held. Fills in the passed
Greg Farnum40819f62010-08-02 15:34:23 -0700200 * counter variables, so you can prepare pagelist metadata before calling
201 * ceph_encode_locks.
202 */
203void 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 Schutt39be95e2013-05-15 13:03:35 -0500221 * Encode the flock and fcntl locks for the given inode into the ceph_filelock
Jeff Layton1c8c6012013-06-21 08:58:15 -0400222 * array. Must be called with inode->i_lock already held.
Jim Schutt39be95e2013-05-15 13:03:35 -0500223 * If we encounter more of a specific lock type than expected, return -ENOSPC.
Greg Farnum40819f62010-08-02 15:34:23 -0700224 */
Jim Schutt39be95e2013-05-15 13:03:35 -0500225int ceph_encode_locks_to_buffer(struct inode *inode,
226 struct ceph_filelock *flocks,
227 int num_fcntl_locks, int num_flock_locks)
Greg Farnum40819f62010-08-02 15:34:23 -0700228{
229 struct file_lock *lock;
Greg Farnum40819f62010-08-02 15:34:23 -0700230 int err = 0;
Greg Farnumfca44512010-09-17 10:24:02 -0700231 int seen_fcntl = 0;
232 int seen_flock = 0;
Jim Schutt39be95e2013-05-15 13:03:35 -0500233 int l = 0;
Greg Farnum40819f62010-08-02 15:34:23 -0700234
235 dout("encoding %d flock and %d fcntl locks", num_flock_locks,
236 num_fcntl_locks);
Jim Schutt39be95e2013-05-15 13:03:35 -0500237
Greg Farnum40819f62010-08-02 15:34:23 -0700238 for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
239 if (lock->fl_flags & FL_POSIX) {
Greg Farnumfca44512010-09-17 10:24:02 -0700240 ++seen_fcntl;
241 if (seen_fcntl > num_fcntl_locks) {
242 err = -ENOSPC;
243 goto fail;
244 }
Jim Schutt39be95e2013-05-15 13:03:35 -0500245 err = lock_to_ceph_filelock(lock, &flocks[l]);
Greg Farnum40819f62010-08-02 15:34:23 -0700246 if (err)
247 goto fail;
Jim Schutt39be95e2013-05-15 13:03:35 -0500248 ++l;
Greg Farnum40819f62010-08-02 15:34:23 -0700249 }
Greg Farnum40819f62010-08-02 15:34:23 -0700250 }
Greg Farnum40819f62010-08-02 15:34:23 -0700251 for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
252 if (lock->fl_flags & FL_FLOCK) {
Greg Farnumfca44512010-09-17 10:24:02 -0700253 ++seen_flock;
254 if (seen_flock > num_flock_locks) {
255 err = -ENOSPC;
256 goto fail;
257 }
Jim Schutt39be95e2013-05-15 13:03:35 -0500258 err = lock_to_ceph_filelock(lock, &flocks[l]);
Greg Farnum40819f62010-08-02 15:34:23 -0700259 if (err)
260 goto fail;
Jim Schutt39be95e2013-05-15 13:03:35 -0500261 ++l;
Greg Farnum40819f62010-08-02 15:34:23 -0700262 }
Greg Farnum40819f62010-08-02 15:34:23 -0700263 }
264fail:
265 return err;
266}
267
Jim Schutt39be95e2013-05-15 13:03:35 -0500268/**
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 */
274int 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));
299out_fail:
300 return err;
301}
302
Greg Farnum40819f62010-08-02 15:34:23 -0700303/*
304 * Given a pointer to a lock, convert it to a ceph filelock
305 */
306int lock_to_ceph_filelock(struct file_lock *lock,
307 struct ceph_filelock *cephlock)
308{
309 int err = 0;
Greg Farnum40819f62010-08-02 15:34:23 -0700310 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, Zhengeb13e832014-03-09 23:16:40 +0800313 cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
Jeff Layton130d1f92014-05-09 14:13:04 -0400314 cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
Greg Farnum40819f62010-08-02 15:34:23 -0700315
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}