]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - fs/ceph/locks.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-kernels.git] / fs / ceph / locks.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
3d14c5d2 2#include <linux/ceph/ceph_debug.h>
40819f6f
GF
3
4#include <linux/file.h>
5#include <linux/namei.h>
eb13e832 6#include <linux/random.h>
40819f6f
GF
7
8#include "super.h"
9#include "mds_client.h"
3d14c5d2 10#include <linux/ceph/pagelist.h>
40819f6f 11
eb13e832 12static u64 lock_secret;
9280be24
YZ
13static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
14 struct ceph_mds_request *req);
eb13e832
YZ
15
16static inline u64 secure_addr(void *addr)
17{
18 u64 v = lock_secret ^ (u64)(unsigned long)addr;
19 /*
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')
23 */
24 v |= (1ULL << 63);
25 return v;
26}
27
28void __init ceph_flock_init(void)
29{
30 get_random_bytes(&lock_secret, sizeof(lock_secret));
31}
32
40819f6f
GF
33/**
34 * Implement fcntl and flock locking functions.
35 */
36static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
637ae8d5 37 int cmd, u8 wait, struct file_lock *fl)
40819f6f 38{
496ad9aa 39 struct inode *inode = file_inode(file);
eb13e832 40 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
40819f6f
GF
41 struct ceph_mds_request *req;
42 int err;
637ae8d5 43 u64 length = 0;
eb13e832 44 u64 owner;
40819f6f 45
9280be24
YZ
46 if (operation != CEPH_MDS_OP_SETFILELOCK || cmd == CEPH_LOCK_UNLOCK)
47 wait = 0;
48
40819f6f
GF
49 req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
50 if (IS_ERR(req))
51 return PTR_ERR(req);
70b666c3
SW
52 req->r_inode = inode;
53 ihold(inode);
3bd58143 54 req->r_num_caps = 1;
40819f6f 55
637ae8d5
HS
56 /* mds requires start and length rather than start and end */
57 if (LLONG_MAX == fl->fl_end)
58 length = 0;
59 else
60 length = fl->fl_end - fl->fl_start + 1;
61
130d1f95 62 owner = secure_addr(fl->fl_owner);
eb13e832
YZ
63
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,
67 wait, fl->fl_type);
637ae8d5 68
40819f6f
GF
69 req->r_args.filelock_change.rule = lock_type;
70 req->r_args.filelock_change.type = cmd;
eb13e832 71 req->r_args.filelock_change.owner = cpu_to_le64(owner);
637ae8d5 72 req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
637ae8d5 73 req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
40819f6f
GF
74 req->r_args.filelock_change.length = cpu_to_le64(length);
75 req->r_args.filelock_change.wait = wait;
76
9280be24
YZ
77 if (wait)
78 req->r_wait_for_completion = ceph_lock_wait_for_completion;
79
40819f6f 80 err = ceph_mdsc_do_request(mdsc, inode, req);
a5b10629 81
eb13e832 82 if (operation == CEPH_MDS_OP_GETFILELOCK) {
9d5b86ac 83 fl->fl_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
a5b10629
HS
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;
88 else
89 fl->fl_type = F_UNLCK;
90
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);
94 if (length >= 1)
95 fl->fl_end = length -1;
96 else
97 fl->fl_end = 0;
98
99 }
40819f6f
GF
100 ceph_mdsc_put_request(req);
101 dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
0c1f91f2 102 "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type,
637ae8d5
HS
103 (int)operation, (u64)fl->fl_pid, fl->fl_start,
104 length, wait, fl->fl_type, err);
40819f6f
GF
105 return err;
106}
107
9280be24
YZ
108static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
109 struct ceph_mds_request *req)
110{
111 struct ceph_mds_request *intr_req;
112 struct inode *inode = req->r_inode;
113 int err, lock_type;
114
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;
120 else
121 BUG_ON(1);
122 BUG_ON(req->r_args.filelock_change.type == CEPH_LOCK_UNLOCK);
123
124 err = wait_for_completion_interruptible(&req->r_completion);
125 if (!err)
126 return 0;
127
128 dout("ceph_lock_wait_for_completion: request %llu was interrupted\n",
129 req->r_tid);
130
92e57e62
YZ
131 mutex_lock(&mdsc->mutex);
132 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
133 err = 0;
134 } else {
135 /*
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.
139 */
140 mutex_lock(&req->r_fill_mutex);
141 req->r_err = err;
142 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
143 mutex_unlock(&req->r_fill_mutex);
144
145 if (!req->r_session) {
146 // haven't sent the request
147 err = 0;
148 }
149 }
150 mutex_unlock(&mdsc->mutex);
151 if (!err)
152 return 0;
153
9280be24
YZ
154 intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
155 USE_AUTH_MDS);
156 if (IS_ERR(intr_req))
157 return PTR_ERR(intr_req);
158
159 intr_req->r_inode = inode;
160 ihold(inode);
161 intr_req->r_num_caps = 1;
162
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;
166
167 err = ceph_mdsc_do_request(mdsc, inode, intr_req);
168 ceph_mdsc_put_request(intr_req);
169
170 if (err && err != -ERESTARTSYS)
171 return err;
172
92e57e62 173 wait_for_completion_killable(&req->r_safe_completion);
9280be24
YZ
174 return 0;
175}
176
40819f6f
GF
177/**
178 * Attempt to set an fcntl lock.
179 * For now, this just goes away to the server. Later it may be more awesome.
180 */
181int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
182{
40819f6f
GF
183 u8 lock_cmd;
184 int err;
185 u8 wait = 0;
186 u16 op = CEPH_MDS_OP_SETFILELOCK;
187
eb70c0ce
YZ
188 if (!(fl->fl_flags & FL_POSIX))
189 return -ENOLCK;
190 /* No mandatory locks */
191 if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
192 return -ENOLCK;
193
eb13e832 194 dout("ceph_lock, fl_owner: %p", fl->fl_owner);
40819f6f
GF
195
196 /* set wait bit as appropriate, then make command as Ceph expects it*/
0e8e95d6 197 if (IS_GETLK(cmd))
40819f6f 198 op = CEPH_MDS_OP_GETFILELOCK;
0e8e95d6
YZ
199 else if (IS_SETLKW(cmd))
200 wait = 1;
40819f6f
GF
201
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;
206 else
207 lock_cmd = CEPH_LOCK_UNLOCK;
208
637ae8d5 209 err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl);
40819f6f 210 if (!err) {
eb13e832 211 if (op != CEPH_MDS_OP_GETFILELOCK) {
a5b10629
HS
212 dout("mds locked, locking locally");
213 err = posix_lock_file(file, fl, NULL);
214 if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
0c1f91f2
SW
215 /* undo! This should only happen if
216 * the kernel detects local
217 * deadlock. */
a5b10629
HS
218 ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
219 CEPH_LOCK_UNLOCK, 0, fl);
0c1f91f2
SW
220 dout("got %d on posix_lock_file, undid lock",
221 err);
a5b10629 222 }
40819f6f 223 }
40819f6f
GF
224 }
225 return err;
226}
227
228int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
229{
40819f6f
GF
230 u8 lock_cmd;
231 int err;
0e8e95d6 232 u8 wait = 0;
40819f6f 233
eb70c0ce
YZ
234 if (!(fl->fl_flags & FL_FLOCK))
235 return -ENOLCK;
236 /* No mandatory locks */
db4a63aa
YZ
237 if (fl->fl_type & LOCK_MAND)
238 return -EOPNOTSUPP;
eb70c0ce 239
eb13e832 240 dout("ceph_flock, fl_file: %p", fl->fl_file);
40819f6f 241
0e8e95d6
YZ
242 if (IS_SETLKW(cmd))
243 wait = 1;
244
245 if (F_RDLCK == fl->fl_type)
40819f6f 246 lock_cmd = CEPH_LOCK_SHARED;
0e8e95d6 247 else if (F_WRLCK == fl->fl_type)
40819f6f
GF
248 lock_cmd = CEPH_LOCK_EXCL;
249 else
250 lock_cmd = CEPH_LOCK_UNLOCK;
40819f6f
GF
251
252 err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
637ae8d5 253 file, lock_cmd, wait, fl);
40819f6f 254 if (!err) {
4f656367 255 err = locks_lock_file_wait(file, fl);
40819f6f
GF
256 if (err) {
257 ceph_lock_message(CEPH_LOCK_FLOCK,
258 CEPH_MDS_OP_SETFILELOCK,
637ae8d5 259 file, CEPH_LOCK_UNLOCK, 0, fl);
4f656367 260 dout("got %d on locks_lock_file_wait, undid lock", err);
40819f6f 261 }
40819f6f
GF
262 }
263 return err;
264}
265
5263e31e
JL
266/*
267 * Fills in the passed counter variables, so you can prepare pagelist metadata
268 * before calling ceph_encode_locks.
40819f6f
GF
269 */
270void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
271{
e084c1bd 272 struct file_lock *lock;
5263e31e 273 struct file_lock_context *ctx;
40819f6f
GF
274
275 *fcntl_count = 0;
276 *flock_count = 0;
277
5263e31e
JL
278 ctx = inode->i_flctx;
279 if (ctx) {
e084c1bd
JL
280 spin_lock(&ctx->flc_lock);
281 list_for_each_entry(lock, &ctx->flc_posix, fl_list)
282 ++(*fcntl_count);
283 list_for_each_entry(lock, &ctx->flc_flock, fl_list)
284 ++(*flock_count);
285 spin_unlock(&ctx->flc_lock);
40819f6f
GF
286 }
287 dout("counted %d flock locks and %d fcntl locks",
288 *flock_count, *fcntl_count);
289}
290
291/**
39be95e9 292 * Encode the flock and fcntl locks for the given inode into the ceph_filelock
1c8c601a 293 * array. Must be called with inode->i_lock already held.
39be95e9 294 * If we encounter more of a specific lock type than expected, return -ENOSPC.
40819f6f 295 */
39be95e9
JS
296int ceph_encode_locks_to_buffer(struct inode *inode,
297 struct ceph_filelock *flocks,
298 int num_fcntl_locks, int num_flock_locks)
40819f6f
GF
299{
300 struct file_lock *lock;
bd61e0a9 301 struct file_lock_context *ctx = inode->i_flctx;
40819f6f 302 int err = 0;
fca4451a
GF
303 int seen_fcntl = 0;
304 int seen_flock = 0;
39be95e9 305 int l = 0;
40819f6f
GF
306
307 dout("encoding %d flock and %d fcntl locks", num_flock_locks,
308 num_fcntl_locks);
39be95e9 309
bd61e0a9
JL
310 if (!ctx)
311 return 0;
312
6109c850 313 spin_lock(&ctx->flc_lock);
f6762cb2 314 list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
bd61e0a9
JL
315 ++seen_fcntl;
316 if (seen_fcntl > num_fcntl_locks) {
317 err = -ENOSPC;
318 goto fail;
40819f6f 319 }
bd61e0a9
JL
320 err = lock_to_ceph_filelock(lock, &flocks[l]);
321 if (err)
322 goto fail;
323 ++l;
40819f6f 324 }
bd61e0a9
JL
325 list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
326 ++seen_flock;
327 if (seen_flock > num_flock_locks) {
328 err = -ENOSPC;
329 goto fail;
40819f6f 330 }
bd61e0a9
JL
331 err = lock_to_ceph_filelock(lock, &flocks[l]);
332 if (err)
333 goto fail;
334 ++l;
40819f6f
GF
335 }
336fail:
6109c850 337 spin_unlock(&ctx->flc_lock);
40819f6f
GF
338 return err;
339}
340
39be95e9
JS
341/**
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.
346 */
347int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
348 struct ceph_pagelist *pagelist,
349 int num_fcntl_locks, int num_flock_locks)
350{
351 int err = 0;
352 __le32 nlocks;
353
354 nlocks = cpu_to_le32(num_fcntl_locks);
355 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
356 if (err)
357 goto out_fail;
358
359 err = ceph_pagelist_append(pagelist, flocks,
360 num_fcntl_locks * sizeof(*flocks));
361 if (err)
362 goto out_fail;
363
364 nlocks = cpu_to_le32(num_flock_locks);
365 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
366 if (err)
367 goto out_fail;
368
369 err = ceph_pagelist_append(pagelist,
370 &flocks[num_fcntl_locks],
371 num_flock_locks * sizeof(*flocks));
372out_fail:
373 return err;
374}
375
40819f6f
GF
376/*
377 * Given a pointer to a lock, convert it to a ceph filelock
378 */
379int lock_to_ceph_filelock(struct file_lock *lock,
380 struct ceph_filelock *cephlock)
381{
382 int err = 0;
40819f6f
GF
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);
eb13e832 386 cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
130d1f95 387 cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
40819f6f
GF
388
389 switch (lock->fl_type) {
390 case F_RDLCK:
391 cephlock->type = CEPH_LOCK_SHARED;
392 break;
393 case F_WRLCK:
394 cephlock->type = CEPH_LOCK_EXCL;
395 break;
396 case F_UNLCK:
397 cephlock->type = CEPH_LOCK_UNLOCK;
398 break;
399 default:
400 dout("Have unknown lock type %d", lock->fl_type);
401 err = -EINVAL;
402 }
403
404 return err;
405}