Merge branch 'hugepage-fallbacks' (hugepatch patches from David Rientjes)
[linux-2.6-block.git] / fs / afs / flock.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
e8d6c554
DH
2/* AFS file locking support
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
e8d6c554
DH
6 */
7
e8d6c554
DH
8#include "internal.h"
9
10#define AFS_LOCK_GRANTED 0
11#define AFS_LOCK_PENDING 1
4be5975a 12#define AFS_LOCK_YOUR_TRY 2
e8d6c554 13
f044c884
DH
14struct workqueue_struct *afs_lock_manager;
15
4be5975a 16static void afs_next_locker(struct afs_vnode *vnode, int error);
e8d6c554
DH
17static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
18static void afs_fl_release_private(struct file_lock *fl);
19
6aed6285 20static const struct file_lock_operations afs_lock_ops = {
e8d6c554
DH
21 .fl_copy_lock = afs_fl_copy_lock,
22 .fl_release_private = afs_fl_release_private,
23};
24
4be5975a
DH
25static inline void afs_set_lock_state(struct afs_vnode *vnode, enum afs_lock_state state)
26{
27 _debug("STATE %u -> %u", vnode->lock_state, state);
28 vnode->lock_state = state;
29}
30
d4696601
DH
31static atomic_t afs_file_lock_debug_id;
32
e8d6c554
DH
33/*
34 * if the callback is broken on this vnode, then the lock may now be available
35 */
36void afs_lock_may_be_available(struct afs_vnode *vnode)
37{
3b6492df 38 _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
e8d6c554 39
4be5975a
DH
40 spin_lock(&vnode->lock);
41 if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
42 afs_next_locker(vnode, 0);
d4696601 43 trace_afs_flock_ev(vnode, NULL, afs_flock_callback_break, 0);
4be5975a 44 spin_unlock(&vnode->lock);
e8d6c554
DH
45}
46
47/*
48 * the lock will time out in 5 minutes unless we extend it, so schedule
49 * extension in a bit less than that time
50 */
51static void afs_schedule_lock_extension(struct afs_vnode *vnode)
52{
a690f60a
DH
53 ktime_t expires_at, now, duration;
54 u64 duration_j;
55
56 expires_at = ktime_add_ms(vnode->locked_at, AFS_LOCKWAIT * 1000 / 2);
57 now = ktime_get_real();
58 duration = ktime_sub(expires_at, now);
59 if (duration <= 0)
60 duration_j = 0;
61 else
62 duration_j = nsecs_to_jiffies(ktime_to_ns(duration));
63
64 queue_delayed_work(afs_lock_manager, &vnode->lock_work, duration_j);
65}
66
67/*
68 * In the case of successful completion of a lock operation, record the time
69 * the reply appeared and start the lock extension timer.
70 */
71void afs_lock_op_done(struct afs_call *call)
72{
a58823ac 73 struct afs_vnode *vnode = call->lvnode;
a690f60a
DH
74
75 if (call->error == 0) {
76 spin_lock(&vnode->lock);
d4696601 77 trace_afs_flock_ev(vnode, NULL, afs_flock_timestamp, 0);
a690f60a
DH
78 vnode->locked_at = call->reply_time;
79 afs_schedule_lock_extension(vnode);
80 spin_unlock(&vnode->lock);
81 }
e8d6c554
DH
82}
83
ff8e210a
DH
84/*
85 * grant one or more locks (readlocks are allowed to jump the queue if the
86 * first lock in the queue is itself a readlock)
87 * - the caller must hold the vnode lock
88 */
4be5975a 89static void afs_grant_locks(struct afs_vnode *vnode)
ff8e210a
DH
90{
91 struct file_lock *p, *_p;
4be5975a 92 bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE);
ff8e210a 93
4be5975a
DH
94 list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
95 if (!exclusive && p->fl_type == F_WRLCK)
96 continue;
97
98 list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks);
99 p->fl_u.afs.state = AFS_LOCK_GRANTED;
d4696601 100 trace_afs_flock_op(vnode, p, afs_flock_op_grant);
4be5975a
DH
101 wake_up(&p->fl_wait);
102 }
103}
104
105/*
106 * If an error is specified, reject every pending lock that matches the
107 * authentication and type of the lock we failed to get. If there are any
108 * remaining lockers, try to wake up one of them to have a go.
109 */
110static void afs_next_locker(struct afs_vnode *vnode, int error)
111{
112 struct file_lock *p, *_p, *next = NULL;
113 struct key *key = vnode->lock_key;
114 unsigned int fl_type = F_RDLCK;
115
116 _enter("");
117
118 if (vnode->lock_type == AFS_LOCK_WRITE)
119 fl_type = F_WRLCK;
120
121 list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
122 if (error &&
123 p->fl_type == fl_type &&
124 afs_file_key(p->fl_file) == key) {
125 list_del_init(&p->fl_u.afs.link);
126 p->fl_u.afs.state = error;
127 wake_up(&p->fl_wait);
ff8e210a 128 }
4be5975a
DH
129
130 /* Select the next locker to hand off to. */
131 if (next &&
132 (next->fl_type == F_WRLCK || p->fl_type == F_RDLCK))
133 continue;
134 next = p;
ff8e210a 135 }
4be5975a
DH
136
137 vnode->lock_key = NULL;
138 key_put(key);
139
140 if (next) {
141 afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
142 next->fl_u.afs.state = AFS_LOCK_YOUR_TRY;
d4696601 143 trace_afs_flock_op(vnode, next, afs_flock_op_wake);
4be5975a
DH
144 wake_up(&next->fl_wait);
145 } else {
146 afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE);
d4696601 147 trace_afs_flock_ev(vnode, NULL, afs_flock_no_lockers, 0);
4be5975a
DH
148 }
149
150 _leave("");
ff8e210a
DH
151}
152
cdfb26b4
DH
153/*
154 * Kill off all waiters in the the pending lock queue due to the vnode being
155 * deleted.
156 */
157static void afs_kill_lockers_enoent(struct afs_vnode *vnode)
158{
159 struct file_lock *p;
160
161 afs_set_lock_state(vnode, AFS_VNODE_LOCK_DELETED);
162
163 while (!list_empty(&vnode->pending_locks)) {
164 p = list_entry(vnode->pending_locks.next,
165 struct file_lock, fl_u.afs.link);
166 list_del_init(&p->fl_u.afs.link);
167 p->fl_u.afs.state = -ENOENT;
168 wake_up(&p->fl_wait);
169 }
170
171 key_put(vnode->lock_key);
172 vnode->lock_key = NULL;
ff8e210a
DH
173}
174
d2ddc776
DH
175/*
176 * Get a lock on a file
177 */
178static int afs_set_lock(struct afs_vnode *vnode, struct key *key,
179 afs_lock_type_t type)
180{
a58823ac 181 struct afs_status_cb *scb;
d2ddc776
DH
182 struct afs_fs_cursor fc;
183 int ret;
184
3b6492df 185 _enter("%s{%llx:%llu.%u},%x,%u",
d2ddc776
DH
186 vnode->volume->name,
187 vnode->fid.vid,
188 vnode->fid.vnode,
189 vnode->fid.unique,
190 key_serial(key), type);
191
a58823ac
DH
192 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
193 if (!scb)
194 return -ENOMEM;
195
d2ddc776 196 ret = -ERESTARTSYS;
20b8391f 197 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
d2ddc776 198 while (afs_select_fileserver(&fc)) {
68251f0a 199 fc.cb_break = afs_calc_vnode_cb_break(vnode);
a58823ac 200 afs_fs_set_lock(&fc, type, scb);
d2ddc776
DH
201 }
202
a58823ac
DH
203 afs_check_for_remote_deletion(&fc, vnode);
204 afs_vnode_commit_status(&fc, vnode, fc.cb_break, NULL, scb);
d2ddc776
DH
205 ret = afs_end_vnode_operation(&fc);
206 }
207
a58823ac 208 kfree(scb);
d2ddc776
DH
209 _leave(" = %d", ret);
210 return ret;
211}
212
213/*
214 * Extend a lock on a file
215 */
216static int afs_extend_lock(struct afs_vnode *vnode, struct key *key)
217{
a58823ac 218 struct afs_status_cb *scb;
d2ddc776
DH
219 struct afs_fs_cursor fc;
220 int ret;
221
3b6492df 222 _enter("%s{%llx:%llu.%u},%x",
d2ddc776
DH
223 vnode->volume->name,
224 vnode->fid.vid,
225 vnode->fid.vnode,
226 vnode->fid.unique,
227 key_serial(key));
228
a58823ac
DH
229 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
230 if (!scb)
231 return -ENOMEM;
232
d2ddc776 233 ret = -ERESTARTSYS;
20b8391f 234 if (afs_begin_vnode_operation(&fc, vnode, key, false)) {
d2ddc776 235 while (afs_select_current_fileserver(&fc)) {
68251f0a 236 fc.cb_break = afs_calc_vnode_cb_break(vnode);
a58823ac 237 afs_fs_extend_lock(&fc, scb);
d2ddc776
DH
238 }
239
a58823ac
DH
240 afs_check_for_remote_deletion(&fc, vnode);
241 afs_vnode_commit_status(&fc, vnode, fc.cb_break, NULL, scb);
d2ddc776
DH
242 ret = afs_end_vnode_operation(&fc);
243 }
244
a58823ac 245 kfree(scb);
d2ddc776
DH
246 _leave(" = %d", ret);
247 return ret;
248}
249
250/*
251 * Release a lock on a file
252 */
253static int afs_release_lock(struct afs_vnode *vnode, struct key *key)
254{
a58823ac 255 struct afs_status_cb *scb;
d2ddc776
DH
256 struct afs_fs_cursor fc;
257 int ret;
258
3b6492df 259 _enter("%s{%llx:%llu.%u},%x",
d2ddc776
DH
260 vnode->volume->name,
261 vnode->fid.vid,
262 vnode->fid.vnode,
263 vnode->fid.unique,
264 key_serial(key));
265
a58823ac
DH
266 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
267 if (!scb)
268 return -ENOMEM;
269
d2ddc776 270 ret = -ERESTARTSYS;
20b8391f 271 if (afs_begin_vnode_operation(&fc, vnode, key, false)) {
d2ddc776 272 while (afs_select_current_fileserver(&fc)) {
68251f0a 273 fc.cb_break = afs_calc_vnode_cb_break(vnode);
a58823ac 274 afs_fs_release_lock(&fc, scb);
d2ddc776
DH
275 }
276
a58823ac
DH
277 afs_check_for_remote_deletion(&fc, vnode);
278 afs_vnode_commit_status(&fc, vnode, fc.cb_break, NULL, scb);
d2ddc776
DH
279 ret = afs_end_vnode_operation(&fc);
280 }
281
a58823ac 282 kfree(scb);
d2ddc776
DH
283 _leave(" = %d", ret);
284 return ret;
285}
286
e8d6c554
DH
287/*
288 * do work for a lock, including:
289 * - probing for a lock we're waiting on but didn't get immediately
290 * - extending a lock that's close to timing out
291 */
292void afs_lock_work(struct work_struct *work)
293{
294 struct afs_vnode *vnode =
295 container_of(work, struct afs_vnode, lock_work.work);
e8d6c554
DH
296 struct key *key;
297 int ret;
298
3b6492df 299 _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
e8d6c554
DH
300
301 spin_lock(&vnode->lock);
302
0fafdc9f
DH
303again:
304 _debug("wstate %u for %p", vnode->lock_state, vnode);
305 switch (vnode->lock_state) {
306 case AFS_VNODE_LOCK_NEED_UNLOCK:
4be5975a 307 afs_set_lock_state(vnode, AFS_VNODE_LOCK_UNLOCKING);
d4696601 308 trace_afs_flock_ev(vnode, NULL, afs_flock_work_unlocking, 0);
e8d6c554
DH
309 spin_unlock(&vnode->lock);
310
311 /* attempt to release the server lock; if it fails, we just
0fafdc9f
DH
312 * wait 5 minutes and it'll expire anyway */
313 ret = afs_release_lock(vnode, vnode->lock_key);
79ddbfa5 314 if (ret < 0 && vnode->lock_state != AFS_VNODE_LOCK_DELETED) {
cdfb26b4
DH
315 trace_afs_flock_ev(vnode, NULL, afs_flock_release_fail,
316 ret);
e8d6c554 317 printk(KERN_WARNING "AFS:"
3b6492df 318 " Failed to release lock on {%llx:%llx} error %d\n",
e8d6c554 319 vnode->fid.vid, vnode->fid.vnode, ret);
0fafdc9f
DH
320 }
321
e8d6c554 322 spin_lock(&vnode->lock);
cdfb26b4
DH
323 if (ret == -ENOENT)
324 afs_kill_lockers_enoent(vnode);
325 else
326 afs_next_locker(vnode, 0);
4be5975a
DH
327 spin_unlock(&vnode->lock);
328 return;
e8d6c554 329
0fafdc9f
DH
330 /* If we've already got a lock, then it must be time to extend that
331 * lock as AFS locks time out after 5 minutes.
332 */
333 case AFS_VNODE_LOCK_GRANTED:
e8d6c554
DH
334 _debug("extend");
335
0fafdc9f
DH
336 ASSERT(!list_empty(&vnode->granted_locks));
337
338 key = key_get(vnode->lock_key);
4be5975a 339 afs_set_lock_state(vnode, AFS_VNODE_LOCK_EXTENDING);
d4696601 340 trace_afs_flock_ev(vnode, NULL, afs_flock_work_extending, 0);
e8d6c554
DH
341 spin_unlock(&vnode->lock);
342
0fafdc9f 343 ret = afs_extend_lock(vnode, key); /* RPC */
e8d6c554 344 key_put(key);
0fafdc9f 345
cdfb26b4
DH
346 if (ret < 0) {
347 trace_afs_flock_ev(vnode, NULL, afs_flock_extend_fail,
348 ret);
3b6492df 349 pr_warning("AFS: Failed to extend lock on {%llx:%llx} error %d\n",
0fafdc9f 350 vnode->fid.vid, vnode->fid.vnode, ret);
cdfb26b4 351 }
0fafdc9f
DH
352
353 spin_lock(&vnode->lock);
354
cdfb26b4
DH
355 if (ret == -ENOENT) {
356 afs_kill_lockers_enoent(vnode);
357 spin_unlock(&vnode->lock);
358 return;
359 }
360
0fafdc9f
DH
361 if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING)
362 goto again;
4be5975a 363 afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
0fafdc9f 364
4be5975a 365 if (ret != 0)
e8d6c554
DH
366 queue_delayed_work(afs_lock_manager, &vnode->lock_work,
367 HZ * 10);
0fafdc9f
DH
368 spin_unlock(&vnode->lock);
369 _leave(" [ext]");
e8d6c554 370 return;
e8d6c554 371
4be5975a
DH
372 /* If we're waiting for a callback to indicate lock release, we can't
373 * actually rely on this, so need to recheck at regular intervals. The
374 * problem is that the server might not notify us if the lock just
375 * expires (say because a client died) rather than being explicitly
376 * released.
377 */
0fafdc9f 378 case AFS_VNODE_LOCK_WAITING_FOR_CB:
4be5975a
DH
379 _debug("retry");
380 afs_next_locker(vnode, 0);
e8d6c554 381 spin_unlock(&vnode->lock);
4be5975a 382 return;
e8d6c554 383
cdfb26b4
DH
384 case AFS_VNODE_LOCK_DELETED:
385 afs_kill_lockers_enoent(vnode);
386 spin_unlock(&vnode->lock);
387 return;
0fafdc9f 388
e690c9e3 389 /* Fall through */
0fafdc9f
DH
390 default:
391 /* Looks like a lock request was withdrawn. */
392 spin_unlock(&vnode->lock);
393 _leave(" [no]");
e8d6c554
DH
394 return;
395 }
e8d6c554
DH
396}
397
398/*
399 * pass responsibility for the unlocking of a vnode on the server to the
400 * manager thread, lest a pending signal in the calling thread interrupt
401 * AF_RXRPC
402 * - the caller must hold the vnode lock
403 */
0fafdc9f 404static void afs_defer_unlock(struct afs_vnode *vnode)
e8d6c554 405{
4be5975a 406 _enter("%u", vnode->lock_state);
0fafdc9f 407
4be5975a
DH
408 if (list_empty(&vnode->granted_locks) &&
409 (vnode->lock_state == AFS_VNODE_LOCK_GRANTED ||
410 vnode->lock_state == AFS_VNODE_LOCK_EXTENDING)) {
0fafdc9f
DH
411 cancel_delayed_work(&vnode->lock_work);
412
4be5975a 413 afs_set_lock_state(vnode, AFS_VNODE_LOCK_NEED_UNLOCK);
d4696601 414 trace_afs_flock_ev(vnode, NULL, afs_flock_defer_unlock, 0);
4be5975a 415 queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
0fafdc9f
DH
416 }
417}
418
419/*
420 * Check that our view of the file metadata is up to date and check to see
421 * whether we think that we have a locking permit.
422 */
423static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key,
6c6c1d63 424 enum afs_flock_mode mode, afs_lock_type_t type)
0fafdc9f
DH
425{
426 afs_access_t access;
427 int ret;
428
429 /* Make sure we've got a callback on this file and that our view of the
430 * data version is up to date.
431 */
432 ret = afs_validate(vnode, key);
433 if (ret < 0)
434 return ret;
435
436 /* Check the permission set to see if we're actually going to be
437 * allowed to get a lock on this file.
438 */
439 ret = afs_check_permit(vnode, key, &access);
440 if (ret < 0)
441 return ret;
442
443 /* At a rough estimation, you need LOCK, WRITE or INSERT perm to
444 * read-lock a file and WRITE or INSERT perm to write-lock a file.
445 *
446 * We can't rely on the server to do this for us since if we want to
447 * share a read lock that we already have, we won't go the server.
448 */
449 if (type == AFS_LOCK_READ) {
450 if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK)))
451 return -EACCES;
0fafdc9f
DH
452 } else {
453 if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE)))
454 return -EACCES;
0fafdc9f
DH
455 }
456
457 return 0;
458}
459
e8d6c554
DH
460/*
461 * request a lock on a file on the server
462 */
463static int afs_do_setlk(struct file *file, struct file_lock *fl)
464{
0fafdc9f 465 struct inode *inode = locks_inode(file);
1c8c601a 466 struct afs_vnode *vnode = AFS_FS_I(inode);
6c6c1d63 467 enum afs_flock_mode mode = AFS_FS_S(inode->i_sb)->flock_mode;
e8d6c554 468 afs_lock_type_t type;
215804a9 469 struct key *key = afs_file_key(file);
6c6c1d63 470 bool partial, no_server_lock = false;
e8d6c554
DH
471 int ret;
472
6c6c1d63
DH
473 if (mode == afs_flock_mode_unset)
474 mode = afs_flock_mode_openafs;
e8d6c554 475
6c6c1d63
DH
476 _enter("{%llx:%llu},%llu-%llu,%u,%u",
477 vnode->fid.vid, vnode->fid.vnode,
478 fl->fl_start, fl->fl_end, fl->fl_type, mode);
e8d6c554 479
e8d6c554
DH
480 fl->fl_ops = &afs_lock_ops;
481 INIT_LIST_HEAD(&fl->fl_u.afs.link);
482 fl->fl_u.afs.state = AFS_LOCK_PENDING;
483
6c6c1d63 484 partial = (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX);
e8d6c554 485 type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
6c6c1d63
DH
486 if (mode == afs_flock_mode_write && partial)
487 type = AFS_LOCK_WRITE;
e8d6c554 488
6c6c1d63 489 ret = afs_do_setlk_check(vnode, key, mode, type);
e8d6c554 490 if (ret < 0)
0fafdc9f 491 return ret;
e8d6c554 492
d4696601 493 trace_afs_flock_op(vnode, fl, afs_flock_op_set_lock);
e8d6c554 494
6c6c1d63
DH
495 /* AFS3 protocol only supports full-file locks and doesn't provide any
496 * method of upgrade/downgrade, so we need to emulate for partial-file
497 * locks.
498 *
499 * The OpenAFS client only gets a server lock for a full-file lock and
500 * keeps partial-file locks local. Allow this behaviour to be emulated
501 * (as the default).
0fafdc9f 502 */
6c6c1d63
DH
503 if (mode == afs_flock_mode_local ||
504 (partial && mode == afs_flock_mode_openafs)) {
505 no_server_lock = true;
506 goto skip_server_lock;
ff8e210a 507 }
e8d6c554 508
e8d6c554 509 spin_lock(&vnode->lock);
0fafdc9f 510 list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
e8d6c554 511
cdfb26b4
DH
512 ret = -ENOENT;
513 if (vnode->lock_state == AFS_VNODE_LOCK_DELETED)
514 goto error_unlock;
515
4be5975a
DH
516 /* If we've already got a lock on the server then try to move to having
517 * the VFS grant the requested lock. Note that this means that other
518 * clients may get starved out.
0fafdc9f 519 */
4be5975a
DH
520 _debug("try %u", vnode->lock_state);
521 if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED) {
522 if (type == AFS_LOCK_READ) {
523 _debug("instant readlock");
524 list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
525 fl->fl_u.afs.state = AFS_LOCK_GRANTED;
526 goto vnode_is_locked_u;
527 }
e8d6c554 528
4be5975a
DH
529 if (vnode->lock_type == AFS_LOCK_WRITE) {
530 _debug("instant writelock");
531 list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
532 fl->fl_u.afs.state = AFS_LOCK_GRANTED;
533 goto vnode_is_locked_u;
534 }
535 }
e8d6c554 536
6c6c1d63
DH
537 if (vnode->lock_state == AFS_VNODE_LOCK_NONE &&
538 !(fl->fl_flags & FL_SLEEP)) {
539 ret = -EAGAIN;
540 if (type == AFS_LOCK_READ) {
541 if (vnode->status.lock_count == -1)
542 goto lock_is_contended; /* Write locked */
543 } else {
544 if (vnode->status.lock_count != 0)
545 goto lock_is_contended; /* Locked */
546 }
547 }
548
0fafdc9f
DH
549 if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
550 goto need_to_wait;
e8d6c554 551
4be5975a 552try_to_lock:
0fafdc9f
DH
553 /* We don't have a lock on this vnode and we aren't currently waiting
554 * for one either, so ask the server for a lock.
555 *
556 * Note that we need to be careful if we get interrupted by a signal
557 * after dispatching the request as we may still get the lock, even
558 * though we don't wait for the reply (it's not too bad a problem - the
4be5975a 559 * lock will expire in 5 mins anyway).
0fafdc9f 560 */
d4696601 561 trace_afs_flock_ev(vnode, fl, afs_flock_try_to_lock, 0);
0fafdc9f
DH
562 vnode->lock_key = key_get(key);
563 vnode->lock_type = type;
4be5975a 564 afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
e8d6c554
DH
565 spin_unlock(&vnode->lock);
566
0fafdc9f 567 ret = afs_set_lock(vnode, key, type); /* RPC */
e8d6c554
DH
568
569 spin_lock(&vnode->lock);
0fafdc9f 570 switch (ret) {
4be5975a
DH
571 case -EKEYREJECTED:
572 case -EKEYEXPIRED:
573 case -EKEYREVOKED:
574 case -EPERM:
575 case -EACCES:
576 fl->fl_u.afs.state = ret;
d4696601 577 trace_afs_flock_ev(vnode, fl, afs_flock_fail_perm, ret);
4be5975a
DH
578 list_del_init(&fl->fl_u.afs.link);
579 afs_next_locker(vnode, ret);
580 goto error_unlock;
581
cdfb26b4
DH
582 case -ENOENT:
583 fl->fl_u.afs.state = ret;
584 trace_afs_flock_ev(vnode, fl, afs_flock_fail_other, ret);
585 list_del_init(&fl->fl_u.afs.link);
586 afs_kill_lockers_enoent(vnode);
587 goto error_unlock;
588
0fafdc9f 589 default:
4be5975a 590 fl->fl_u.afs.state = ret;
d4696601 591 trace_afs_flock_ev(vnode, fl, afs_flock_fail_other, ret);
4be5975a
DH
592 list_del_init(&fl->fl_u.afs.link);
593 afs_next_locker(vnode, 0);
594 goto error_unlock;
e8d6c554 595
0fafdc9f
DH
596 case -EWOULDBLOCK:
597 /* The server doesn't have a lock-waiting queue, so the client
598 * will have to retry. The server will break the outstanding
599 * callbacks on a file when a lock is released.
600 */
0fafdc9f
DH
601 ASSERT(list_empty(&vnode->granted_locks));
602 ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link);
4be5975a 603 goto lock_is_contended;
0fafdc9f
DH
604
605 case 0:
4be5975a 606 afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
d4696601 607 trace_afs_flock_ev(vnode, fl, afs_flock_acquired, type);
4be5975a
DH
608 afs_grant_locks(vnode);
609 goto vnode_is_locked_u;
e8d6c554 610 }
e8d6c554 611
4be5975a 612vnode_is_locked_u:
0fafdc9f 613 spin_unlock(&vnode->lock);
4be5975a
DH
614vnode_is_locked:
615 /* the lock has been granted by the server... */
616 ASSERTCMP(fl->fl_u.afs.state, ==, AFS_LOCK_GRANTED);
0fafdc9f 617
6c6c1d63 618skip_server_lock:
4be5975a 619 /* ... but the VFS still needs to distribute access on this client. */
d4696601 620 trace_afs_flock_ev(vnode, fl, afs_flock_vfs_locking, 0);
4be5975a 621 ret = locks_lock_file_wait(file, fl);
d4696601 622 trace_afs_flock_ev(vnode, fl, afs_flock_vfs_lock, ret);
e8d6c554
DH
623 if (ret < 0)
624 goto vfs_rejected_lock;
e8d6c554 625
0fafdc9f 626 /* Again, make sure we've got a callback on this file and, again, make
e8d6c554 627 * sure that our view of the data version is up to date (we ignore
0fafdc9f
DH
628 * errors incurred here and deal with the consequences elsewhere).
629 */
d2ddc776 630 afs_validate(vnode, key);
0fafdc9f
DH
631 _leave(" = 0");
632 return 0;
e8d6c554 633
4be5975a
DH
634lock_is_contended:
635 if (!(fl->fl_flags & FL_SLEEP)) {
636 list_del_init(&fl->fl_u.afs.link);
637 afs_next_locker(vnode, 0);
638 ret = -EAGAIN;
639 goto error_unlock;
640 }
641
642 afs_set_lock_state(vnode, AFS_VNODE_LOCK_WAITING_FOR_CB);
d4696601 643 trace_afs_flock_ev(vnode, fl, afs_flock_would_block, ret);
4be5975a
DH
644 queue_delayed_work(afs_lock_manager, &vnode->lock_work, HZ * 5);
645
0fafdc9f
DH
646need_to_wait:
647 /* We're going to have to wait. Either this client doesn't have a lock
648 * on the server yet and we need to wait for a callback to occur, or
4be5975a
DH
649 * the client does have a lock on the server, but it's shared and we
650 * need an exclusive lock.
0fafdc9f 651 */
4be5975a 652 spin_unlock(&vnode->lock);
0fafdc9f 653
d4696601 654 trace_afs_flock_ev(vnode, fl, afs_flock_waiting, 0);
4be5975a
DH
655 ret = wait_event_interruptible(fl->fl_wait,
656 fl->fl_u.afs.state != AFS_LOCK_PENDING);
d4696601 657 trace_afs_flock_ev(vnode, fl, afs_flock_waited, ret);
0fafdc9f 658
4be5975a 659 if (fl->fl_u.afs.state >= 0 && fl->fl_u.afs.state != AFS_LOCK_GRANTED) {
0fafdc9f 660 spin_lock(&vnode->lock);
0fafdc9f 661
4be5975a
DH
662 switch (fl->fl_u.afs.state) {
663 case AFS_LOCK_YOUR_TRY:
664 fl->fl_u.afs.state = AFS_LOCK_PENDING;
665 goto try_to_lock;
666 case AFS_LOCK_PENDING:
667 if (ret > 0) {
668 /* We need to retry the lock. We may not be
669 * notified by the server if it just expired
670 * rather than being released.
671 */
672 ASSERTCMP(vnode->lock_state, ==, AFS_VNODE_LOCK_WAITING_FOR_CB);
673 afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
674 fl->fl_u.afs.state = AFS_LOCK_PENDING;
675 goto try_to_lock;
676 }
677 goto error_unlock;
678 case AFS_LOCK_GRANTED:
679 default:
680 break;
681 }
0fafdc9f 682
4be5975a
DH
683 spin_unlock(&vnode->lock);
684 }
0fafdc9f 685
4be5975a
DH
686 if (fl->fl_u.afs.state == AFS_LOCK_GRANTED)
687 goto vnode_is_locked;
688 ret = fl->fl_u.afs.state;
689 goto error;
e8d6c554
DH
690
691vfs_rejected_lock:
0fafdc9f
DH
692 /* The VFS rejected the lock we just obtained, so we have to discard
693 * what we just got. We defer this to the lock manager work item to
694 * deal with.
695 */
e8d6c554 696 _debug("vfs refused %d", ret);
6c6c1d63
DH
697 if (no_server_lock)
698 goto error;
0fafdc9f 699 spin_lock(&vnode->lock);
e8d6c554 700 list_del_init(&fl->fl_u.afs.link);
4be5975a
DH
701 afs_defer_unlock(vnode);
702
703error_unlock:
704 spin_unlock(&vnode->lock);
705error:
706 _leave(" = %d", ret);
707 return ret;
e8d6c554
DH
708}
709
710/*
711 * unlock on a file on the server
712 */
713static int afs_do_unlk(struct file *file, struct file_lock *fl)
714{
0fafdc9f 715 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
e8d6c554
DH
716 int ret;
717
3b6492df 718 _enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
e8d6c554 719
d4696601
DH
720 trace_afs_flock_op(vnode, fl, afs_flock_op_unlock);
721
0fafdc9f
DH
722 /* Flush all pending writes before doing anything with locks. */
723 vfs_fsync(file, 0);
724
4be5975a 725 ret = locks_lock_file_wait(file, fl);
0fafdc9f
DH
726 _leave(" = %d [%u]", ret, vnode->lock_state);
727 return ret;
e8d6c554
DH
728}
729
730/*
731 * return information about a lock we currently hold, if indeed we hold one
732 */
733static int afs_do_getlk(struct file *file, struct file_lock *fl)
734{
0fafdc9f 735 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
215804a9 736 struct key *key = afs_file_key(file);
e8d6c554
DH
737 int ret, lock_count;
738
739 _enter("");
740
cdfb26b4
DH
741 if (vnode->lock_state == AFS_VNODE_LOCK_DELETED)
742 return -ENOENT;
743
e8d6c554
DH
744 fl->fl_type = F_UNLCK;
745
e8d6c554 746 /* check local lock records first */
275afcac
AM
747 posix_test_lock(file, fl);
748 if (fl->fl_type == F_UNLCK) {
e8d6c554 749 /* no local locks; consult the server */
a58823ac 750 ret = afs_fetch_status(vnode, key, false, NULL);
e8d6c554
DH
751 if (ret < 0)
752 goto error;
0fafdc9f
DH
753
754 lock_count = READ_ONCE(vnode->status.lock_count);
68ce801f
DH
755 if (lock_count != 0) {
756 if (lock_count > 0)
757 fl->fl_type = F_RDLCK;
758 else
759 fl->fl_type = F_WRLCK;
760 fl->fl_start = 0;
761 fl->fl_end = OFFSET_MAX;
762 fl->fl_pid = 0;
763 }
e8d6c554
DH
764 }
765
0fafdc9f 766 ret = 0;
e8d6c554 767error:
e8d6c554
DH
768 _leave(" = %d [%hd]", ret, fl->fl_type);
769 return ret;
770}
771
772/*
773 * manage POSIX locks on a file
774 */
775int afs_lock(struct file *file, int cmd, struct file_lock *fl)
776{
0fafdc9f 777 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
d4696601
DH
778 enum afs_flock_operation op;
779 int ret;
e8d6c554 780
3b6492df 781 _enter("{%llx:%llu},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
e8d6c554
DH
782 vnode->fid.vid, vnode->fid.vnode, cmd,
783 fl->fl_type, fl->fl_flags,
784 (long long) fl->fl_start, (long long) fl->fl_end);
785
786 /* AFS doesn't support mandatory locks */
fc5846e5 787 if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK)
e8d6c554
DH
788 return -ENOLCK;
789
790 if (IS_GETLK(cmd))
791 return afs_do_getlk(file, fl);
d4696601
DH
792
793 fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
794 trace_afs_flock_op(vnode, fl, afs_flock_op_lock);
795
e8d6c554 796 if (fl->fl_type == F_UNLCK)
d4696601
DH
797 ret = afs_do_unlk(file, fl);
798 else
799 ret = afs_do_setlk(file, fl);
800
801 switch (ret) {
802 case 0: op = afs_flock_op_return_ok; break;
803 case -EAGAIN: op = afs_flock_op_return_eagain; break;
804 case -EDEADLK: op = afs_flock_op_return_edeadlk; break;
805 default: op = afs_flock_op_return_error; break;
806 }
807 trace_afs_flock_op(vnode, fl, op);
808 return ret;
e8d6c554
DH
809}
810
811/*
812 * manage FLOCK locks on a file
813 */
814int afs_flock(struct file *file, int cmd, struct file_lock *fl)
815{
0fafdc9f 816 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
d4696601
DH
817 enum afs_flock_operation op;
818 int ret;
e8d6c554 819
3b6492df 820 _enter("{%llx:%llu},%d,{t=%x,fl=%x}",
e8d6c554
DH
821 vnode->fid.vid, vnode->fid.vnode, cmd,
822 fl->fl_type, fl->fl_flags);
823
824 /*
825 * No BSD flocks over NFS allowed.
826 * Note: we could try to fake a POSIX lock request here by
827 * using ((u32) filp | 0x80000000) or some such as the pid.
828 * Not sure whether that would be unique, though, or whether
829 * that would break in other places.
830 */
831 if (!(fl->fl_flags & FL_FLOCK))
832 return -ENOLCK;
833
d4696601
DH
834 fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
835 trace_afs_flock_op(vnode, fl, afs_flock_op_flock);
836
e8d6c554 837 /* we're simulating flock() locks using posix locks on the server */
e8d6c554 838 if (fl->fl_type == F_UNLCK)
d4696601
DH
839 ret = afs_do_unlk(file, fl);
840 else
841 ret = afs_do_setlk(file, fl);
842
843 switch (ret) {
844 case 0: op = afs_flock_op_return_ok; break;
845 case -EAGAIN: op = afs_flock_op_return_eagain; break;
846 case -EDEADLK: op = afs_flock_op_return_edeadlk; break;
847 default: op = afs_flock_op_return_error; break;
848 }
849 trace_afs_flock_op(vnode, fl, op);
850 return ret;
e8d6c554
DH
851}
852
853/*
854 * the POSIX lock management core VFS code copies the lock record and adds the
855 * copy into its own list, so we need to add that copy to the vnode's lock
856 * queue in the same place as the original (which will be deleted shortly
857 * after)
858 */
859static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
860{
0fafdc9f
DH
861 struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
862
e8d6c554
DH
863 _enter("");
864
d4696601
DH
865 new->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
866
0fafdc9f 867 spin_lock(&vnode->lock);
d4696601 868 trace_afs_flock_op(vnode, new, afs_flock_op_copy_lock);
e8d6c554 869 list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
0fafdc9f 870 spin_unlock(&vnode->lock);
e8d6c554
DH
871}
872
873/*
874 * need to remove this lock from the vnode queue when it's removed from the
875 * VFS's list
876 */
877static void afs_fl_release_private(struct file_lock *fl)
878{
0fafdc9f
DH
879 struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
880
e8d6c554
DH
881 _enter("");
882
0fafdc9f 883 spin_lock(&vnode->lock);
4be5975a 884
d4696601 885 trace_afs_flock_op(vnode, fl, afs_flock_op_release_lock);
4be5975a
DH
886 list_del_init(&fl->fl_u.afs.link);
887 if (list_empty(&vnode->granted_locks))
888 afs_defer_unlock(vnode);
889
0fafdc9f
DH
890 _debug("state %u for %p", vnode->lock_state, vnode);
891 spin_unlock(&vnode->lock);
e8d6c554 892}