blob: d94ba0df9f4d195cabf677fcdcd41cc01096c7e7 [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);
Greg Farnum40819f62010-08-02 15:34:23 -070048
Herb Shiu637ae8d2010-11-23 13:42:23 -080049 /* mds requires start and length rather than start and end */
50 if (LLONG_MAX == fl->fl_end)
51 length = 0;
52 else
53 length = fl->fl_end - fl->fl_start + 1;
54
Yan, Zhengeb13e832014-03-09 23:16:40 +080055 if (lock_type == CEPH_LOCK_FCNTL)
56 owner = secure_addr(fl->fl_owner);
57 else
58 owner = secure_addr(fl->fl_file);
59
60 dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
61 "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type,
62 (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length,
63 wait, fl->fl_type);
Herb Shiu637ae8d2010-11-23 13:42:23 -080064
Greg Farnum40819f62010-08-02 15:34:23 -070065 req->r_args.filelock_change.rule = lock_type;
66 req->r_args.filelock_change.type = cmd;
Yan, Zhengeb13e832014-03-09 23:16:40 +080067 req->r_args.filelock_change.owner = cpu_to_le64(owner);
Herb Shiu637ae8d2010-11-23 13:42:23 -080068 req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
Herb Shiu637ae8d2010-11-23 13:42:23 -080069 req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
Greg Farnum40819f62010-08-02 15:34:23 -070070 req->r_args.filelock_change.length = cpu_to_le64(length);
71 req->r_args.filelock_change.wait = wait;
72
73 err = ceph_mdsc_do_request(mdsc, inode, req);
Herb Shiua5b10622010-11-23 13:58:29 -080074
Yan, Zhengeb13e832014-03-09 23:16:40 +080075 if (operation == CEPH_MDS_OP_GETFILELOCK) {
Herb Shiua5b10622010-11-23 13:58:29 -080076 fl->fl_pid = le64_to_cpu(req->r_reply_info.filelock_reply->pid);
77 if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
78 fl->fl_type = F_RDLCK;
79 else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
80 fl->fl_type = F_WRLCK;
81 else
82 fl->fl_type = F_UNLCK;
83
84 fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
85 length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
86 le64_to_cpu(req->r_reply_info.filelock_reply->length);
87 if (length >= 1)
88 fl->fl_end = length -1;
89 else
90 fl->fl_end = 0;
91
92 }
Greg Farnum40819f62010-08-02 15:34:23 -070093 ceph_mdsc_put_request(req);
94 dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
Sage Weil0c1f91f2011-05-25 14:56:12 -070095 "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type,
Herb Shiu637ae8d2010-11-23 13:42:23 -080096 (int)operation, (u64)fl->fl_pid, fl->fl_start,
97 length, wait, fl->fl_type, err);
Greg Farnum40819f62010-08-02 15:34:23 -070098 return err;
99}
100
101/**
102 * Attempt to set an fcntl lock.
103 * For now, this just goes away to the server. Later it may be more awesome.
104 */
105int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
106{
Greg Farnum40819f62010-08-02 15:34:23 -0700107 u8 lock_cmd;
108 int err;
109 u8 wait = 0;
110 u16 op = CEPH_MDS_OP_SETFILELOCK;
111
Yan, Zhengeb70c0c2014-03-04 15:50:06 +0800112 if (!(fl->fl_flags & FL_POSIX))
113 return -ENOLCK;
114 /* No mandatory locks */
115 if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
116 return -ENOLCK;
117
Yan, Zhengeb13e832014-03-09 23:16:40 +0800118 dout("ceph_lock, fl_owner: %p", fl->fl_owner);
Greg Farnum40819f62010-08-02 15:34:23 -0700119
120 /* set wait bit as appropriate, then make command as Ceph expects it*/
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800121 if (IS_GETLK(cmd))
Greg Farnum40819f62010-08-02 15:34:23 -0700122 op = CEPH_MDS_OP_GETFILELOCK;
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800123 else if (IS_SETLKW(cmd))
124 wait = 1;
Greg Farnum40819f62010-08-02 15:34:23 -0700125
126 if (F_RDLCK == fl->fl_type)
127 lock_cmd = CEPH_LOCK_SHARED;
128 else if (F_WRLCK == fl->fl_type)
129 lock_cmd = CEPH_LOCK_EXCL;
130 else
131 lock_cmd = CEPH_LOCK_UNLOCK;
132
Herb Shiu637ae8d2010-11-23 13:42:23 -0800133 err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700134 if (!err) {
Yan, Zhengeb13e832014-03-09 23:16:40 +0800135 if (op != CEPH_MDS_OP_GETFILELOCK) {
Herb Shiua5b10622010-11-23 13:58:29 -0800136 dout("mds locked, locking locally");
137 err = posix_lock_file(file, fl, NULL);
138 if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
Sage Weil0c1f91f2011-05-25 14:56:12 -0700139 /* undo! This should only happen if
140 * the kernel detects local
141 * deadlock. */
Herb Shiua5b10622010-11-23 13:58:29 -0800142 ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
143 CEPH_LOCK_UNLOCK, 0, fl);
Sage Weil0c1f91f2011-05-25 14:56:12 -0700144 dout("got %d on posix_lock_file, undid lock",
145 err);
Herb Shiua5b10622010-11-23 13:58:29 -0800146 }
Greg Farnum40819f62010-08-02 15:34:23 -0700147 }
Herb Shiua5b10622010-11-23 13:58:29 -0800148
Sage Weil0c1f91f2011-05-25 14:56:12 -0700149 } else if (err == -ERESTARTSYS) {
150 dout("undoing lock\n");
151 ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
152 CEPH_LOCK_UNLOCK, 0, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700153 }
154 return err;
155}
156
157int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
158{
Greg Farnum40819f62010-08-02 15:34:23 -0700159 u8 lock_cmd;
160 int err;
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800161 u8 wait = 0;
Greg Farnum40819f62010-08-02 15:34:23 -0700162
Yan, Zhengeb70c0c2014-03-04 15:50:06 +0800163 if (!(fl->fl_flags & FL_FLOCK))
164 return -ENOLCK;
165 /* No mandatory locks */
166 if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
167 return -ENOLCK;
168
Yan, Zhengeb13e832014-03-09 23:16:40 +0800169 dout("ceph_flock, fl_file: %p", fl->fl_file);
Greg Farnum40819f62010-08-02 15:34:23 -0700170
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800171 if (IS_SETLKW(cmd))
172 wait = 1;
173
174 if (F_RDLCK == fl->fl_type)
Greg Farnum40819f62010-08-02 15:34:23 -0700175 lock_cmd = CEPH_LOCK_SHARED;
Yan, Zheng0e8e95d2014-03-04 15:42:24 +0800176 else if (F_WRLCK == fl->fl_type)
Greg Farnum40819f62010-08-02 15:34:23 -0700177 lock_cmd = CEPH_LOCK_EXCL;
178 else
179 lock_cmd = CEPH_LOCK_UNLOCK;
Greg Farnum40819f62010-08-02 15:34:23 -0700180
181 err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
Herb Shiu637ae8d2010-11-23 13:42:23 -0800182 file, lock_cmd, wait, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700183 if (!err) {
184 err = flock_lock_file_wait(file, fl);
185 if (err) {
186 ceph_lock_message(CEPH_LOCK_FLOCK,
187 CEPH_MDS_OP_SETFILELOCK,
Herb Shiu637ae8d2010-11-23 13:42:23 -0800188 file, CEPH_LOCK_UNLOCK, 0, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700189 dout("got %d on flock_lock_file_wait, undid lock", err);
190 }
Sage Weil0c1f91f2011-05-25 14:56:12 -0700191 } else if (err == -ERESTARTSYS) {
192 dout("undoing lock\n");
193 ceph_lock_message(CEPH_LOCK_FLOCK,
194 CEPH_MDS_OP_SETFILELOCK,
195 file, CEPH_LOCK_UNLOCK, 0, fl);
Greg Farnum40819f62010-08-02 15:34:23 -0700196 }
197 return err;
198}
199
200/**
Jim Schutt4d1bf792013-05-15 10:38:12 -0600201 * Must be called with lock_flocks() already held. Fills in the passed
Greg Farnum40819f62010-08-02 15:34:23 -0700202 * counter variables, so you can prepare pagelist metadata before calling
203 * ceph_encode_locks.
204 */
205void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
206{
207 struct file_lock *lock;
208
209 *fcntl_count = 0;
210 *flock_count = 0;
211
212 for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
213 if (lock->fl_flags & FL_POSIX)
214 ++(*fcntl_count);
215 else if (lock->fl_flags & FL_FLOCK)
216 ++(*flock_count);
217 }
218 dout("counted %d flock locks and %d fcntl locks",
219 *flock_count, *fcntl_count);
220}
221
222/**
Jim Schutt39be95e2013-05-15 13:03:35 -0500223 * Encode the flock and fcntl locks for the given inode into the ceph_filelock
Jeff Layton1c8c6012013-06-21 08:58:15 -0400224 * array. Must be called with inode->i_lock already held.
Jim Schutt39be95e2013-05-15 13:03:35 -0500225 * If we encounter more of a specific lock type than expected, return -ENOSPC.
Greg Farnum40819f62010-08-02 15:34:23 -0700226 */
Jim Schutt39be95e2013-05-15 13:03:35 -0500227int ceph_encode_locks_to_buffer(struct inode *inode,
228 struct ceph_filelock *flocks,
229 int num_fcntl_locks, int num_flock_locks)
Greg Farnum40819f62010-08-02 15:34:23 -0700230{
231 struct file_lock *lock;
Greg Farnum40819f62010-08-02 15:34:23 -0700232 int err = 0;
Greg Farnumfca44512010-09-17 10:24:02 -0700233 int seen_fcntl = 0;
234 int seen_flock = 0;
Jim Schutt39be95e2013-05-15 13:03:35 -0500235 int l = 0;
Greg Farnum40819f62010-08-02 15:34:23 -0700236
237 dout("encoding %d flock and %d fcntl locks", num_flock_locks,
238 num_fcntl_locks);
Jim Schutt39be95e2013-05-15 13:03:35 -0500239
Greg Farnum40819f62010-08-02 15:34:23 -0700240 for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
241 if (lock->fl_flags & FL_POSIX) {
Greg Farnumfca44512010-09-17 10:24:02 -0700242 ++seen_fcntl;
243 if (seen_fcntl > num_fcntl_locks) {
244 err = -ENOSPC;
245 goto fail;
246 }
Jim Schutt39be95e2013-05-15 13:03:35 -0500247 err = lock_to_ceph_filelock(lock, &flocks[l]);
Greg Farnum40819f62010-08-02 15:34:23 -0700248 if (err)
249 goto fail;
Jim Schutt39be95e2013-05-15 13:03:35 -0500250 ++l;
Greg Farnum40819f62010-08-02 15:34:23 -0700251 }
Greg Farnum40819f62010-08-02 15:34:23 -0700252 }
Greg Farnum40819f62010-08-02 15:34:23 -0700253 for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
254 if (lock->fl_flags & FL_FLOCK) {
Greg Farnumfca44512010-09-17 10:24:02 -0700255 ++seen_flock;
256 if (seen_flock > num_flock_locks) {
257 err = -ENOSPC;
258 goto fail;
259 }
Jim Schutt39be95e2013-05-15 13:03:35 -0500260 err = lock_to_ceph_filelock(lock, &flocks[l]);
Greg Farnum40819f62010-08-02 15:34:23 -0700261 if (err)
262 goto fail;
Jim Schutt39be95e2013-05-15 13:03:35 -0500263 ++l;
Greg Farnum40819f62010-08-02 15:34:23 -0700264 }
Greg Farnum40819f62010-08-02 15:34:23 -0700265 }
266fail:
267 return err;
268}
269
Jim Schutt39be95e2013-05-15 13:03:35 -0500270/**
271 * Copy the encoded flock and fcntl locks into the pagelist.
272 * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
273 * sequential flock locks.
274 * Returns zero on success.
275 */
276int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
277 struct ceph_pagelist *pagelist,
278 int num_fcntl_locks, int num_flock_locks)
279{
280 int err = 0;
281 __le32 nlocks;
282
283 nlocks = cpu_to_le32(num_fcntl_locks);
284 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
285 if (err)
286 goto out_fail;
287
288 err = ceph_pagelist_append(pagelist, flocks,
289 num_fcntl_locks * sizeof(*flocks));
290 if (err)
291 goto out_fail;
292
293 nlocks = cpu_to_le32(num_flock_locks);
294 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
295 if (err)
296 goto out_fail;
297
298 err = ceph_pagelist_append(pagelist,
299 &flocks[num_fcntl_locks],
300 num_flock_locks * sizeof(*flocks));
301out_fail:
302 return err;
303}
304
Greg Farnum40819f62010-08-02 15:34:23 -0700305/*
306 * Given a pointer to a lock, convert it to a ceph filelock
307 */
308int lock_to_ceph_filelock(struct file_lock *lock,
309 struct ceph_filelock *cephlock)
310{
311 int err = 0;
Greg Farnum40819f62010-08-02 15:34:23 -0700312 cephlock->start = cpu_to_le64(lock->fl_start);
313 cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
314 cephlock->client = cpu_to_le64(0);
Yan, Zhengeb13e832014-03-09 23:16:40 +0800315 cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
316 if (lock->fl_flags & FL_POSIX)
317 cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
318 else
319 cephlock->owner = cpu_to_le64(secure_addr(lock->fl_file));
Greg Farnum40819f62010-08-02 15:34:23 -0700320
321 switch (lock->fl_type) {
322 case F_RDLCK:
323 cephlock->type = CEPH_LOCK_SHARED;
324 break;
325 case F_WRLCK:
326 cephlock->type = CEPH_LOCK_EXCL;
327 break;
328 case F_UNLCK:
329 cephlock->type = CEPH_LOCK_UNLOCK;
330 break;
331 default:
332 dout("Have unknown lock type %d", lock->fl_type);
333 err = -EINVAL;
334 }
335
336 return err;
337}