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