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/ceph/pagelist.h>
12 static u64 lock_secret;
13 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
14 struct ceph_mds_request *req);
16 static inline u64 secure_addr(void *addr)
18 u64 v = lock_secret ^ (u64)(unsigned long)addr;
20 * Set the most significant bit, so that MDS knows the 'owner'
21 * is sufficient to identify the owner of lock. (old code uses
22 * both 'owner' and 'pid')
28 void __init ceph_flock_init(void)
30 get_random_bytes(&lock_secret, sizeof(lock_secret));
34 * Implement fcntl and flock locking functions.
36 static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
37 int cmd, u8 wait, struct file_lock *fl)
39 struct inode *inode = file_inode(file);
40 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
41 struct ceph_mds_request *req;
46 if (operation != CEPH_MDS_OP_SETFILELOCK || cmd == CEPH_LOCK_UNLOCK)
49 req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
56 /* mds requires start and length rather than start and end */
57 if (LLONG_MAX == fl->fl_end)
60 length = fl->fl_end - fl->fl_start + 1;
62 owner = secure_addr(fl->fl_owner);
64 dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
65 "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type,
66 (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length,
69 req->r_args.filelock_change.rule = lock_type;
70 req->r_args.filelock_change.type = cmd;
71 req->r_args.filelock_change.owner = cpu_to_le64(owner);
72 req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
73 req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
74 req->r_args.filelock_change.length = cpu_to_le64(length);
75 req->r_args.filelock_change.wait = wait;
78 req->r_wait_for_completion = ceph_lock_wait_for_completion;
80 err = ceph_mdsc_do_request(mdsc, inode, req);
82 if (operation == CEPH_MDS_OP_GETFILELOCK) {
83 fl->fl_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
84 if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
85 fl->fl_type = F_RDLCK;
86 else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
87 fl->fl_type = F_WRLCK;
89 fl->fl_type = F_UNLCK;
91 fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
92 length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
93 le64_to_cpu(req->r_reply_info.filelock_reply->length);
95 fl->fl_end = length -1;
100 ceph_mdsc_put_request(req);
101 dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
102 "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type,
103 (int)operation, (u64)fl->fl_pid, fl->fl_start,
104 length, wait, fl->fl_type, err);
108 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
109 struct ceph_mds_request *req)
111 struct ceph_mds_request *intr_req;
112 struct inode *inode = req->r_inode;
115 BUG_ON(req->r_op != CEPH_MDS_OP_SETFILELOCK);
116 if (req->r_args.filelock_change.rule == CEPH_LOCK_FCNTL)
117 lock_type = CEPH_LOCK_FCNTL_INTR;
118 else if (req->r_args.filelock_change.rule == CEPH_LOCK_FLOCK)
119 lock_type = CEPH_LOCK_FLOCK_INTR;
122 BUG_ON(req->r_args.filelock_change.type == CEPH_LOCK_UNLOCK);
124 err = wait_for_completion_interruptible(&req->r_completion);
128 dout("ceph_lock_wait_for_completion: request %llu was interrupted\n",
131 mutex_lock(&mdsc->mutex);
132 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
136 * ensure we aren't running concurrently with
137 * ceph_fill_trace or ceph_readdir_prepopulate, which
138 * rely on locks (dir mutex) held by our caller.
140 mutex_lock(&req->r_fill_mutex);
142 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
143 mutex_unlock(&req->r_fill_mutex);
145 if (!req->r_session) {
146 // haven't sent the request
150 mutex_unlock(&mdsc->mutex);
154 intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
156 if (IS_ERR(intr_req))
157 return PTR_ERR(intr_req);
159 intr_req->r_inode = inode;
161 intr_req->r_num_caps = 1;
163 intr_req->r_args.filelock_change = req->r_args.filelock_change;
164 intr_req->r_args.filelock_change.rule = lock_type;
165 intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
167 err = ceph_mdsc_do_request(mdsc, inode, intr_req);
168 ceph_mdsc_put_request(intr_req);
170 if (err && err != -ERESTARTSYS)
173 wait_for_completion_killable(&req->r_safe_completion);
178 * Attempt to set an fcntl lock.
179 * For now, this just goes away to the server. Later it may be more awesome.
181 int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
186 u16 op = CEPH_MDS_OP_SETFILELOCK;
188 if (!(fl->fl_flags & FL_POSIX))
190 /* No mandatory locks */
191 if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
194 dout("ceph_lock, fl_owner: %p", fl->fl_owner);
196 /* set wait bit as appropriate, then make command as Ceph expects it*/
198 op = CEPH_MDS_OP_GETFILELOCK;
199 else if (IS_SETLKW(cmd))
202 if (F_RDLCK == fl->fl_type)
203 lock_cmd = CEPH_LOCK_SHARED;
204 else if (F_WRLCK == fl->fl_type)
205 lock_cmd = CEPH_LOCK_EXCL;
207 lock_cmd = CEPH_LOCK_UNLOCK;
209 err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl);
211 if (op != CEPH_MDS_OP_GETFILELOCK) {
212 dout("mds locked, locking locally");
213 err = posix_lock_file(file, fl, NULL);
214 if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
215 /* undo! This should only happen if
216 * the kernel detects local
218 ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
219 CEPH_LOCK_UNLOCK, 0, fl);
220 dout("got %d on posix_lock_file, undid lock",
228 int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
234 if (!(fl->fl_flags & FL_FLOCK))
236 /* No mandatory locks */
237 if (fl->fl_type & LOCK_MAND)
240 dout("ceph_flock, fl_file: %p", fl->fl_file);
245 if (F_RDLCK == fl->fl_type)
246 lock_cmd = CEPH_LOCK_SHARED;
247 else if (F_WRLCK == fl->fl_type)
248 lock_cmd = CEPH_LOCK_EXCL;
250 lock_cmd = CEPH_LOCK_UNLOCK;
252 err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
253 file, lock_cmd, wait, fl);
255 err = locks_lock_file_wait(file, fl);
257 ceph_lock_message(CEPH_LOCK_FLOCK,
258 CEPH_MDS_OP_SETFILELOCK,
259 file, CEPH_LOCK_UNLOCK, 0, fl);
260 dout("got %d on locks_lock_file_wait, undid lock", err);
267 * Fills in the passed counter variables, so you can prepare pagelist metadata
268 * before calling ceph_encode_locks.
270 void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
272 struct file_lock *lock;
273 struct file_lock_context *ctx;
278 ctx = inode->i_flctx;
280 spin_lock(&ctx->flc_lock);
281 list_for_each_entry(lock, &ctx->flc_posix, fl_list)
283 list_for_each_entry(lock, &ctx->flc_flock, fl_list)
285 spin_unlock(&ctx->flc_lock);
287 dout("counted %d flock locks and %d fcntl locks",
288 *flock_count, *fcntl_count);
292 * Encode the flock and fcntl locks for the given inode into the ceph_filelock
293 * array. Must be called with inode->i_lock already held.
294 * If we encounter more of a specific lock type than expected, return -ENOSPC.
296 int ceph_encode_locks_to_buffer(struct inode *inode,
297 struct ceph_filelock *flocks,
298 int num_fcntl_locks, int num_flock_locks)
300 struct file_lock *lock;
301 struct file_lock_context *ctx = inode->i_flctx;
307 dout("encoding %d flock and %d fcntl locks", num_flock_locks,
313 spin_lock(&ctx->flc_lock);
314 list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
316 if (seen_fcntl > num_fcntl_locks) {
320 err = lock_to_ceph_filelock(lock, &flocks[l]);
325 list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
327 if (seen_flock > num_flock_locks) {
331 err = lock_to_ceph_filelock(lock, &flocks[l]);
337 spin_unlock(&ctx->flc_lock);
342 * Copy the encoded flock and fcntl locks into the pagelist.
343 * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
344 * sequential flock locks.
345 * Returns zero on success.
347 int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
348 struct ceph_pagelist *pagelist,
349 int num_fcntl_locks, int num_flock_locks)
354 nlocks = cpu_to_le32(num_fcntl_locks);
355 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
359 err = ceph_pagelist_append(pagelist, flocks,
360 num_fcntl_locks * sizeof(*flocks));
364 nlocks = cpu_to_le32(num_flock_locks);
365 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
369 err = ceph_pagelist_append(pagelist,
370 &flocks[num_fcntl_locks],
371 num_flock_locks * sizeof(*flocks));
377 * Given a pointer to a lock, convert it to a ceph filelock
379 int lock_to_ceph_filelock(struct file_lock *lock,
380 struct ceph_filelock *cephlock)
383 cephlock->start = cpu_to_le64(lock->fl_start);
384 cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
385 cephlock->client = cpu_to_le64(0);
386 cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
387 cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
389 switch (lock->fl_type) {
391 cephlock->type = CEPH_LOCK_SHARED;
394 cephlock->type = CEPH_LOCK_EXCL;
397 cephlock->type = CEPH_LOCK_UNLOCK;
400 dout("Have unknown lock type %d", lock->fl_type);