1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/file.h>
5 #include <linux/namei.h>
6 #include <linux/random.h>
9 #include "mds_client.h"
10 #include <linux/filelock.h>
11 #include <linux/ceph/pagelist.h>
13 static u64 lock_secret;
14 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
15 struct ceph_mds_request *req);
17 static inline u64 secure_addr(void *addr)
19 u64 v = lock_secret ^ (u64)(unsigned long)addr;
21 * Set the most significant bit, so that MDS knows the 'owner'
22 * is sufficient to identify the owner of lock. (old code uses
23 * both 'owner' and 'pid')
29 void __init ceph_flock_init(void)
31 get_random_bytes(&lock_secret, sizeof(lock_secret));
34 static void ceph_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
36 struct inode *inode = file_inode(dst->fl_file);
37 atomic_inc(&ceph_inode(inode)->i_filelock_ref);
38 dst->fl_u.ceph.inode = igrab(inode);
42 * Do not use the 'fl->fl_file' in release function, which
43 * is possibly already released by another thread.
45 static void ceph_fl_release_lock(struct file_lock *fl)
47 struct inode *inode = fl->fl_u.ceph.inode;
48 struct ceph_inode_info *ci;
51 * If inode is NULL it should be a request file_lock,
57 ci = ceph_inode(inode);
58 if (atomic_dec_and_test(&ci->i_filelock_ref)) {
59 /* clear error when all locks are released */
60 spin_lock(&ci->i_ceph_lock);
61 ci->i_ceph_flags &= ~CEPH_I_ERROR_FILELOCK;
62 spin_unlock(&ci->i_ceph_lock);
64 fl->fl_u.ceph.inode = NULL;
68 static const struct file_lock_operations ceph_fl_lock_ops = {
69 .fl_copy_lock = ceph_fl_copy_lock,
70 .fl_release_private = ceph_fl_release_lock,
74 * Implement fcntl and flock locking functions.
76 static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
77 int cmd, u8 wait, struct file_lock *fl)
79 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
80 struct ceph_client *cl = mdsc->fsc->client;
81 struct ceph_mds_request *req;
86 if (operation == CEPH_MDS_OP_SETFILELOCK) {
88 * increasing i_filelock_ref closes race window between
89 * handling request reply and adding file_lock struct to
90 * inode. Otherwise, auth caps may get trimmed in the
91 * window. Caller function will decrease the counter.
93 fl->fl_ops = &ceph_fl_lock_ops;
94 fl->fl_ops->fl_copy_lock(fl, NULL);
97 if (operation != CEPH_MDS_OP_SETFILELOCK || cmd == CEPH_LOCK_UNLOCK)
100 req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
103 req->r_inode = inode;
107 /* mds requires start and length rather than start and end */
108 if (LLONG_MAX == fl->fl_end)
111 length = fl->fl_end - fl->fl_start + 1;
113 owner = secure_addr(fl->fl_owner);
115 doutc(cl, "rule: %d, op: %d, owner: %llx, pid: %llu, "
116 "start: %llu, length: %llu, wait: %d, type: %d\n",
117 (int)lock_type, (int)operation, owner, (u64)fl->fl_pid,
118 fl->fl_start, length, wait, fl->fl_type);
120 req->r_args.filelock_change.rule = lock_type;
121 req->r_args.filelock_change.type = cmd;
122 req->r_args.filelock_change.owner = cpu_to_le64(owner);
123 req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
124 req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
125 req->r_args.filelock_change.length = cpu_to_le64(length);
126 req->r_args.filelock_change.wait = wait;
128 err = ceph_mdsc_submit_request(mdsc, inode, req);
130 err = ceph_mdsc_wait_request(mdsc, req, wait ?
131 ceph_lock_wait_for_completion : NULL);
132 if (!err && operation == CEPH_MDS_OP_GETFILELOCK) {
133 fl->fl_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
134 if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
135 fl->fl_type = F_RDLCK;
136 else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
137 fl->fl_type = F_WRLCK;
139 fl->fl_type = F_UNLCK;
141 fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
142 length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
143 le64_to_cpu(req->r_reply_info.filelock_reply->length);
145 fl->fl_end = length -1;
150 ceph_mdsc_put_request(req);
151 doutc(cl, "rule: %d, op: %d, pid: %llu, start: %llu, "
152 "length: %llu, wait: %d, type: %d, err code %d\n",
153 (int)lock_type, (int)operation, (u64)fl->fl_pid,
154 fl->fl_start, length, wait, fl->fl_type, err);
158 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
159 struct ceph_mds_request *req)
161 struct ceph_client *cl = mdsc->fsc->client;
162 struct ceph_mds_request *intr_req;
163 struct inode *inode = req->r_inode;
166 BUG_ON(req->r_op != CEPH_MDS_OP_SETFILELOCK);
167 if (req->r_args.filelock_change.rule == CEPH_LOCK_FCNTL)
168 lock_type = CEPH_LOCK_FCNTL_INTR;
169 else if (req->r_args.filelock_change.rule == CEPH_LOCK_FLOCK)
170 lock_type = CEPH_LOCK_FLOCK_INTR;
173 BUG_ON(req->r_args.filelock_change.type == CEPH_LOCK_UNLOCK);
175 err = wait_for_completion_interruptible(&req->r_completion);
179 doutc(cl, "request %llu was interrupted\n", req->r_tid);
181 mutex_lock(&mdsc->mutex);
182 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
186 * ensure we aren't running concurrently with
187 * ceph_fill_trace or ceph_readdir_prepopulate, which
188 * rely on locks (dir mutex) held by our caller.
190 mutex_lock(&req->r_fill_mutex);
192 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
193 mutex_unlock(&req->r_fill_mutex);
195 if (!req->r_session) {
196 // haven't sent the request
200 mutex_unlock(&mdsc->mutex);
204 intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
206 if (IS_ERR(intr_req))
207 return PTR_ERR(intr_req);
209 intr_req->r_inode = inode;
211 intr_req->r_num_caps = 1;
213 intr_req->r_args.filelock_change = req->r_args.filelock_change;
214 intr_req->r_args.filelock_change.rule = lock_type;
215 intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
217 err = ceph_mdsc_do_request(mdsc, inode, intr_req);
218 ceph_mdsc_put_request(intr_req);
220 if (err && err != -ERESTARTSYS)
223 wait_for_completion_killable(&req->r_safe_completion);
227 static int try_unlock_file(struct file *file, struct file_lock *fl)
230 unsigned int orig_flags = fl->fl_flags;
231 fl->fl_flags |= FL_EXISTS;
232 err = locks_lock_file_wait(file, fl);
233 fl->fl_flags = orig_flags;
234 if (err == -ENOENT) {
235 if (!(orig_flags & FL_EXISTS))
243 * Attempt to set an fcntl lock.
244 * For now, this just goes away to the server. Later it may be more awesome.
246 int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
248 struct inode *inode = file_inode(file);
249 struct ceph_inode_info *ci = ceph_inode(inode);
250 struct ceph_client *cl = ceph_inode_to_client(inode);
252 u16 op = CEPH_MDS_OP_SETFILELOCK;
256 if (!(fl->fl_flags & FL_POSIX))
259 if (ceph_inode_is_shutdown(inode))
262 doutc(cl, "fl_owner: %p\n", fl->fl_owner);
264 /* set wait bit as appropriate, then make command as Ceph expects it*/
266 op = CEPH_MDS_OP_GETFILELOCK;
267 else if (IS_SETLKW(cmd))
270 spin_lock(&ci->i_ceph_lock);
271 if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
274 spin_unlock(&ci->i_ceph_lock);
276 if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type)
277 posix_lock_file(file, fl, NULL);
281 if (F_RDLCK == fl->fl_type)
282 lock_cmd = CEPH_LOCK_SHARED;
283 else if (F_WRLCK == fl->fl_type)
284 lock_cmd = CEPH_LOCK_EXCL;
286 lock_cmd = CEPH_LOCK_UNLOCK;
288 if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type) {
289 err = try_unlock_file(file, fl);
294 err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
296 if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK != fl->fl_type) {
297 doutc(cl, "locking locally\n");
298 err = posix_lock_file(file, fl, NULL);
300 /* undo! This should only happen if
301 * the kernel detects local
303 ceph_lock_message(CEPH_LOCK_FCNTL, op, inode,
304 CEPH_LOCK_UNLOCK, 0, fl);
305 doutc(cl, "got %d on posix_lock_file, undid lock\n",
313 int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
315 struct inode *inode = file_inode(file);
316 struct ceph_inode_info *ci = ceph_inode(inode);
317 struct ceph_client *cl = ceph_inode_to_client(inode);
322 if (!(fl->fl_flags & FL_FLOCK))
325 if (ceph_inode_is_shutdown(inode))
328 doutc(cl, "fl_file: %p\n", fl->fl_file);
330 spin_lock(&ci->i_ceph_lock);
331 if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
334 spin_unlock(&ci->i_ceph_lock);
336 if (F_UNLCK == fl->fl_type)
337 locks_lock_file_wait(file, fl);
344 if (F_RDLCK == fl->fl_type)
345 lock_cmd = CEPH_LOCK_SHARED;
346 else if (F_WRLCK == fl->fl_type)
347 lock_cmd = CEPH_LOCK_EXCL;
349 lock_cmd = CEPH_LOCK_UNLOCK;
351 if (F_UNLCK == fl->fl_type) {
352 err = try_unlock_file(file, fl);
357 err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
358 inode, lock_cmd, wait, fl);
359 if (!err && F_UNLCK != fl->fl_type) {
360 err = locks_lock_file_wait(file, fl);
362 ceph_lock_message(CEPH_LOCK_FLOCK,
363 CEPH_MDS_OP_SETFILELOCK,
364 inode, CEPH_LOCK_UNLOCK, 0, fl);
365 doutc(cl, "got %d on locks_lock_file_wait, undid lock\n",
373 * Fills in the passed counter variables, so you can prepare pagelist metadata
374 * before calling ceph_encode_locks.
376 void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
378 struct ceph_client *cl = ceph_inode_to_client(inode);
379 struct file_lock *lock;
380 struct file_lock_context *ctx;
385 ctx = locks_inode_context(inode);
387 spin_lock(&ctx->flc_lock);
388 list_for_each_entry(lock, &ctx->flc_posix, fl_list)
390 list_for_each_entry(lock, &ctx->flc_flock, fl_list)
392 spin_unlock(&ctx->flc_lock);
394 doutc(cl, "counted %d flock locks and %d fcntl locks\n",
395 *flock_count, *fcntl_count);
399 * Given a pointer to a lock, convert it to a ceph filelock
401 static int lock_to_ceph_filelock(struct inode *inode,
402 struct file_lock *lock,
403 struct ceph_filelock *cephlock)
405 struct ceph_client *cl = ceph_inode_to_client(inode);
408 cephlock->start = cpu_to_le64(lock->fl_start);
409 cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
410 cephlock->client = cpu_to_le64(0);
411 cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
412 cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
414 switch (lock->fl_type) {
416 cephlock->type = CEPH_LOCK_SHARED;
419 cephlock->type = CEPH_LOCK_EXCL;
422 cephlock->type = CEPH_LOCK_UNLOCK;
425 doutc(cl, "Have unknown lock type %d\n", lock->fl_type);
433 * Encode the flock and fcntl locks for the given inode into the ceph_filelock
434 * array. Must be called with inode->i_lock already held.
435 * If we encounter more of a specific lock type than expected, return -ENOSPC.
437 int ceph_encode_locks_to_buffer(struct inode *inode,
438 struct ceph_filelock *flocks,
439 int num_fcntl_locks, int num_flock_locks)
441 struct file_lock *lock;
442 struct file_lock_context *ctx = locks_inode_context(inode);
443 struct ceph_client *cl = ceph_inode_to_client(inode);
449 doutc(cl, "encoding %d flock and %d fcntl locks\n", num_flock_locks,
455 spin_lock(&ctx->flc_lock);
456 list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
458 if (seen_fcntl > num_fcntl_locks) {
462 err = lock_to_ceph_filelock(inode, lock, &flocks[l]);
467 list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
469 if (seen_flock > num_flock_locks) {
473 err = lock_to_ceph_filelock(inode, lock, &flocks[l]);
479 spin_unlock(&ctx->flc_lock);
484 * Copy the encoded flock and fcntl locks into the pagelist.
485 * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
486 * sequential flock locks.
487 * Returns zero on success.
489 int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
490 struct ceph_pagelist *pagelist,
491 int num_fcntl_locks, int num_flock_locks)
496 nlocks = cpu_to_le32(num_fcntl_locks);
497 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
501 if (num_fcntl_locks > 0) {
502 err = ceph_pagelist_append(pagelist, flocks,
503 num_fcntl_locks * sizeof(*flocks));
508 nlocks = cpu_to_le32(num_flock_locks);
509 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
513 if (num_flock_locks > 0) {
514 err = ceph_pagelist_append(pagelist, &flocks[num_fcntl_locks],
515 num_flock_locks * sizeof(*flocks));