1 /* AFS file locking support
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
14 #define AFS_LOCK_GRANTED 0
15 #define AFS_LOCK_PENDING 1
17 struct workqueue_struct
*afs_lock_manager
;
19 static void afs_fl_copy_lock(struct file_lock
*new, struct file_lock
*fl
);
20 static void afs_fl_release_private(struct file_lock
*fl
);
22 static const struct file_lock_operations afs_lock_ops
= {
23 .fl_copy_lock
= afs_fl_copy_lock
,
24 .fl_release_private
= afs_fl_release_private
,
28 * if the callback is broken on this vnode, then the lock may now be available
30 void afs_lock_may_be_available(struct afs_vnode
*vnode
)
32 _enter("{%x:%u}", vnode
->fid
.vid
, vnode
->fid
.vnode
);
34 queue_delayed_work(afs_lock_manager
, &vnode
->lock_work
, 0);
38 * the lock will time out in 5 minutes unless we extend it, so schedule
39 * extension in a bit less than that time
41 static void afs_schedule_lock_extension(struct afs_vnode
*vnode
)
43 queue_delayed_work(afs_lock_manager
, &vnode
->lock_work
,
44 AFS_LOCKWAIT
* HZ
/ 2);
48 * grant one or more locks (readlocks are allowed to jump the queue if the
49 * first lock in the queue is itself a readlock)
50 * - the caller must hold the vnode lock
52 static void afs_grant_locks(struct afs_vnode
*vnode
, struct file_lock
*fl
)
54 struct file_lock
*p
, *_p
;
56 list_move_tail(&fl
->fl_u
.afs
.link
, &vnode
->granted_locks
);
57 if (fl
->fl_type
== F_RDLCK
) {
58 list_for_each_entry_safe(p
, _p
, &vnode
->pending_locks
,
60 if (p
->fl_type
== F_RDLCK
) {
61 p
->fl_u
.afs
.state
= AFS_LOCK_GRANTED
;
62 list_move_tail(&p
->fl_u
.afs
.link
,
63 &vnode
->granted_locks
);
71 * Get a lock on a file
73 static int afs_set_lock(struct afs_vnode
*vnode
, struct key
*key
,
76 struct afs_fs_cursor fc
;
79 _enter("%s{%x:%u.%u},%x,%u",
84 key_serial(key
), type
);
87 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
88 while (afs_select_fileserver(&fc
)) {
89 fc
.cb_break
= vnode
->cb_break
+ vnode
->cb_s_break
;
90 afs_fs_set_lock(&fc
, type
);
93 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
94 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
95 ret
= afs_end_vnode_operation(&fc
);
103 * Extend a lock on a file
105 static int afs_extend_lock(struct afs_vnode
*vnode
, struct key
*key
)
107 struct afs_fs_cursor fc
;
110 _enter("%s{%x:%u.%u},%x",
118 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
119 while (afs_select_current_fileserver(&fc
)) {
120 fc
.cb_break
= vnode
->cb_break
+ vnode
->cb_s_break
;
121 afs_fs_extend_lock(&fc
);
124 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
125 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
126 ret
= afs_end_vnode_operation(&fc
);
129 _leave(" = %d", ret
);
134 * Release a lock on a file
136 static int afs_release_lock(struct afs_vnode
*vnode
, struct key
*key
)
138 struct afs_fs_cursor fc
;
141 _enter("%s{%x:%u.%u},%x",
149 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
150 while (afs_select_current_fileserver(&fc
)) {
151 fc
.cb_break
= vnode
->cb_break
+ vnode
->cb_s_break
;
152 afs_fs_release_lock(&fc
);
155 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
156 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
157 ret
= afs_end_vnode_operation(&fc
);
160 _leave(" = %d", ret
);
165 * do work for a lock, including:
166 * - probing for a lock we're waiting on but didn't get immediately
167 * - extending a lock that's close to timing out
169 void afs_lock_work(struct work_struct
*work
)
171 struct afs_vnode
*vnode
=
172 container_of(work
, struct afs_vnode
, lock_work
.work
);
173 struct file_lock
*fl
, *next
;
174 afs_lock_type_t type
;
178 _enter("{%x:%u}", vnode
->fid
.vid
, vnode
->fid
.vnode
);
180 spin_lock(&vnode
->lock
);
183 _debug("wstate %u for %p", vnode
->lock_state
, vnode
);
184 switch (vnode
->lock_state
) {
185 case AFS_VNODE_LOCK_NEED_UNLOCK
:
187 vnode
->lock_state
= AFS_VNODE_LOCK_UNLOCKING
;
188 spin_unlock(&vnode
->lock
);
190 /* attempt to release the server lock; if it fails, we just
191 * wait 5 minutes and it'll expire anyway */
192 ret
= afs_release_lock(vnode
, vnode
->lock_key
);
194 printk(KERN_WARNING
"AFS:"
195 " Failed to release lock on {%x:%x} error %d\n",
196 vnode
->fid
.vid
, vnode
->fid
.vnode
, ret
);
198 spin_lock(&vnode
->lock
);
199 key_put(vnode
->lock_key
);
200 vnode
->lock_key
= NULL
;
201 vnode
->lock_state
= AFS_VNODE_LOCK_NONE
;
203 if (list_empty(&vnode
->pending_locks
)) {
204 spin_unlock(&vnode
->lock
);
208 /* The new front of the queue now owns the state variables. */
209 next
= list_entry(vnode
->pending_locks
.next
,
210 struct file_lock
, fl_u
.afs
.link
);
211 vnode
->lock_key
= afs_file_key(next
->fl_file
);
212 vnode
->lock_type
= (next
->fl_type
== F_RDLCK
) ? AFS_LOCK_READ
: AFS_LOCK_WRITE
;
213 vnode
->lock_state
= AFS_VNODE_LOCK_WAITING_FOR_CB
;
216 /* If we've already got a lock, then it must be time to extend that
217 * lock as AFS locks time out after 5 minutes.
219 case AFS_VNODE_LOCK_GRANTED
:
222 ASSERT(!list_empty(&vnode
->granted_locks
));
224 key
= key_get(vnode
->lock_key
);
225 vnode
->lock_state
= AFS_VNODE_LOCK_EXTENDING
;
226 spin_unlock(&vnode
->lock
);
228 ret
= afs_extend_lock(vnode
, key
); /* RPC */
232 pr_warning("AFS: Failed to extend lock on {%x:%x} error %d\n",
233 vnode
->fid
.vid
, vnode
->fid
.vnode
, ret
);
235 spin_lock(&vnode
->lock
);
237 if (vnode
->lock_state
!= AFS_VNODE_LOCK_EXTENDING
)
239 vnode
->lock_state
= AFS_VNODE_LOCK_GRANTED
;
242 afs_schedule_lock_extension(vnode
);
244 queue_delayed_work(afs_lock_manager
, &vnode
->lock_work
,
246 spin_unlock(&vnode
->lock
);
250 /* If we don't have a granted lock, then we must've been called
251 * back by the server, and so if might be possible to get a
252 * lock we're currently waiting for.
254 case AFS_VNODE_LOCK_WAITING_FOR_CB
:
257 key
= key_get(vnode
->lock_key
);
258 type
= vnode
->lock_type
;
259 vnode
->lock_state
= AFS_VNODE_LOCK_SETTING
;
260 spin_unlock(&vnode
->lock
);
262 ret
= afs_set_lock(vnode
, key
, type
); /* RPC */
265 spin_lock(&vnode
->lock
);
272 vnode
->lock_state
= AFS_VNODE_LOCK_GRANTED
;
275 /* Pass the lock or the error onto the first locker in
276 * the list - if they're looking for this type of lock.
277 * If they're not, we assume that whoever asked for it
280 if (list_empty(&vnode
->pending_locks
)) {
282 vnode
->lock_state
= AFS_VNODE_LOCK_NEED_UNLOCK
;
286 fl
= list_entry(vnode
->pending_locks
.next
,
287 struct file_lock
, fl_u
.afs
.link
);
288 type
= (fl
->fl_type
== F_RDLCK
) ? AFS_LOCK_READ
: AFS_LOCK_WRITE
;
289 if (vnode
->lock_type
!= type
) {
291 vnode
->lock_state
= AFS_VNODE_LOCK_NEED_UNLOCK
;
295 fl
->fl_u
.afs
.state
= ret
;
297 afs_grant_locks(vnode
, fl
);
299 list_del_init(&fl
->fl_u
.afs
.link
);
300 wake_up(&fl
->fl_wait
);
301 spin_unlock(&vnode
->lock
);
302 _leave(" [granted]");
307 /* Looks like a lock request was withdrawn. */
308 spin_unlock(&vnode
->lock
);
315 * pass responsibility for the unlocking of a vnode on the server to the
316 * manager thread, lest a pending signal in the calling thread interrupt
318 * - the caller must hold the vnode lock
320 static void afs_defer_unlock(struct afs_vnode
*vnode
)
324 if (vnode
->lock_state
== AFS_VNODE_LOCK_GRANTED
||
325 vnode
->lock_state
== AFS_VNODE_LOCK_EXTENDING
) {
326 cancel_delayed_work(&vnode
->lock_work
);
328 vnode
->lock_state
= AFS_VNODE_LOCK_NEED_UNLOCK
;
329 afs_lock_may_be_available(vnode
);
334 * Check that our view of the file metadata is up to date and check to see
335 * whether we think that we have a locking permit.
337 static int afs_do_setlk_check(struct afs_vnode
*vnode
, struct key
*key
,
338 afs_lock_type_t type
, bool can_sleep
)
343 /* Make sure we've got a callback on this file and that our view of the
344 * data version is up to date.
346 ret
= afs_validate(vnode
, key
);
350 /* Check the permission set to see if we're actually going to be
351 * allowed to get a lock on this file.
353 ret
= afs_check_permit(vnode
, key
, &access
);
357 /* At a rough estimation, you need LOCK, WRITE or INSERT perm to
358 * read-lock a file and WRITE or INSERT perm to write-lock a file.
360 * We can't rely on the server to do this for us since if we want to
361 * share a read lock that we already have, we won't go the server.
363 if (type
== AFS_LOCK_READ
) {
364 if (!(access
& (AFS_ACE_INSERT
| AFS_ACE_WRITE
| AFS_ACE_LOCK
)))
366 if (vnode
->status
.lock_count
== -1 && !can_sleep
)
367 return -EAGAIN
; /* Write locked */
369 if (!(access
& (AFS_ACE_INSERT
| AFS_ACE_WRITE
)))
371 if (vnode
->status
.lock_count
!= 0 && !can_sleep
)
372 return -EAGAIN
; /* Locked */
379 * Remove the front runner from the pending queue.
380 * - The caller must hold vnode->lock.
382 static void afs_dequeue_lock(struct afs_vnode
*vnode
, struct file_lock
*fl
)
384 struct file_lock
*next
;
388 /* ->lock_type, ->lock_key and ->lock_state only belong to this
389 * file_lock if we're at the front of the pending queue or if we have
390 * the lock granted or if the lock_state is NEED_UNLOCK or UNLOCKING.
392 if (vnode
->granted_locks
.next
== &fl
->fl_u
.afs
.link
&&
393 vnode
->granted_locks
.prev
== &fl
->fl_u
.afs
.link
) {
394 list_del_init(&fl
->fl_u
.afs
.link
);
395 afs_defer_unlock(vnode
);
399 if (!list_empty(&vnode
->granted_locks
) ||
400 vnode
->pending_locks
.next
!= &fl
->fl_u
.afs
.link
) {
401 list_del_init(&fl
->fl_u
.afs
.link
);
405 list_del_init(&fl
->fl_u
.afs
.link
);
406 key_put(vnode
->lock_key
);
407 vnode
->lock_key
= NULL
;
408 vnode
->lock_state
= AFS_VNODE_LOCK_NONE
;
410 if (list_empty(&vnode
->pending_locks
))
413 /* The new front of the queue now owns the state variables. */
414 next
= list_entry(vnode
->pending_locks
.next
,
415 struct file_lock
, fl_u
.afs
.link
);
416 vnode
->lock_key
= afs_file_key(next
->fl_file
);
417 vnode
->lock_type
= (next
->fl_type
== F_RDLCK
) ? AFS_LOCK_READ
: AFS_LOCK_WRITE
;
418 vnode
->lock_state
= AFS_VNODE_LOCK_WAITING_FOR_CB
;
419 afs_lock_may_be_available(vnode
);
423 * request a lock on a file on the server
425 static int afs_do_setlk(struct file
*file
, struct file_lock
*fl
)
427 struct inode
*inode
= locks_inode(file
);
428 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
429 afs_lock_type_t type
;
430 struct key
*key
= afs_file_key(file
);
433 _enter("{%x:%u},%u", vnode
->fid
.vid
, vnode
->fid
.vnode
, fl
->fl_type
);
435 /* only whole-file locks are supported */
436 if (fl
->fl_start
!= 0 || fl
->fl_end
!= OFFSET_MAX
)
439 fl
->fl_ops
= &afs_lock_ops
;
440 INIT_LIST_HEAD(&fl
->fl_u
.afs
.link
);
441 fl
->fl_u
.afs
.state
= AFS_LOCK_PENDING
;
443 type
= (fl
->fl_type
== F_RDLCK
) ? AFS_LOCK_READ
: AFS_LOCK_WRITE
;
445 ret
= afs_do_setlk_check(vnode
, key
, type
, fl
->fl_flags
& FL_SLEEP
);
449 spin_lock(&vnode
->lock
);
451 /* If we've already got a readlock on the server then we instantly
452 * grant another readlock, irrespective of whether there are any
453 * pending writelocks.
455 if (type
== AFS_LOCK_READ
&&
456 vnode
->lock_state
== AFS_VNODE_LOCK_GRANTED
&&
457 vnode
->lock_type
== AFS_LOCK_READ
) {
458 _debug("instant readlock");
459 ASSERT(!list_empty(&vnode
->granted_locks
));
460 goto share_existing_lock
;
463 list_add_tail(&fl
->fl_u
.afs
.link
, &vnode
->pending_locks
);
465 if (vnode
->lock_state
!= AFS_VNODE_LOCK_NONE
)
468 /* We don't have a lock on this vnode and we aren't currently waiting
469 * for one either, so ask the server for a lock.
471 * Note that we need to be careful if we get interrupted by a signal
472 * after dispatching the request as we may still get the lock, even
473 * though we don't wait for the reply (it's not too bad a problem - the
474 * lock will expire in 10 mins anyway).
476 _debug("not locked");
477 vnode
->lock_key
= key_get(key
);
478 vnode
->lock_type
= type
;
479 vnode
->lock_state
= AFS_VNODE_LOCK_SETTING
;
480 spin_unlock(&vnode
->lock
);
482 ret
= afs_set_lock(vnode
, key
, type
); /* RPC */
484 spin_lock(&vnode
->lock
);
490 /* The server doesn't have a lock-waiting queue, so the client
491 * will have to retry. The server will break the outstanding
492 * callbacks on a file when a lock is released.
494 _debug("would block");
495 ASSERT(list_empty(&vnode
->granted_locks
));
496 ASSERTCMP(vnode
->pending_locks
.next
, ==, &fl
->fl_u
.afs
.link
);
497 vnode
->lock_state
= AFS_VNODE_LOCK_WAITING_FOR_CB
;
505 /* we've acquired a server lock, but it needs to be renewed after 5
507 vnode
->lock_state
= AFS_VNODE_LOCK_GRANTED
;
508 afs_schedule_lock_extension(vnode
);
511 /* the lock has been granted as far as we're concerned... */
512 fl
->fl_u
.afs
.state
= AFS_LOCK_GRANTED
;
513 list_move_tail(&fl
->fl_u
.afs
.link
, &vnode
->granted_locks
);
516 /* ... but we do still need to get the VFS's blessing */
517 spin_unlock(&vnode
->lock
);
519 ret
= posix_lock_file(file
, fl
, NULL
);
521 goto vfs_rejected_lock
;
523 /* Again, make sure we've got a callback on this file and, again, make
524 * sure that our view of the data version is up to date (we ignore
525 * errors incurred here and deal with the consequences elsewhere).
527 afs_validate(vnode
, key
);
532 /* We're going to have to wait. Either this client doesn't have a lock
533 * on the server yet and we need to wait for a callback to occur, or
534 * the client does have a lock on the server, but it belongs to some
535 * other process(es) and is incompatible with the lock we want.
538 if (fl
->fl_flags
& FL_SLEEP
) {
539 spin_unlock(&vnode
->lock
);
542 ret
= wait_event_interruptible(fl
->fl_wait
,
543 fl
->fl_u
.afs
.state
!= AFS_LOCK_PENDING
);
545 spin_lock(&vnode
->lock
);
548 if (fl
->fl_u
.afs
.state
== AFS_LOCK_GRANTED
)
550 if (fl
->fl_u
.afs
.state
< 0)
551 ret
= fl
->fl_u
.afs
.state
;
554 /* we aren't going to get the lock, either because we're unwilling to
555 * wait, or because some signal happened */
557 afs_dequeue_lock(vnode
, fl
);
560 spin_unlock(&vnode
->lock
);
561 _leave(" = %d", ret
);
565 /* The VFS rejected the lock we just obtained, so we have to discard
566 * what we just got. We defer this to the lock manager work item to
569 _debug("vfs refused %d", ret
);
570 spin_lock(&vnode
->lock
);
571 list_del_init(&fl
->fl_u
.afs
.link
);
572 if (list_empty(&vnode
->granted_locks
))
573 afs_defer_unlock(vnode
);
578 * unlock on a file on the server
580 static int afs_do_unlk(struct file
*file
, struct file_lock
*fl
)
582 struct afs_vnode
*vnode
= AFS_FS_I(locks_inode(file
));
585 _enter("{%x:%u},%u", vnode
->fid
.vid
, vnode
->fid
.vnode
, fl
->fl_type
);
587 /* Flush all pending writes before doing anything with locks. */
590 /* only whole-file unlocks are supported */
591 if (fl
->fl_start
!= 0 || fl
->fl_end
!= OFFSET_MAX
)
594 ret
= posix_lock_file(file
, fl
, NULL
);
595 _leave(" = %d [%u]", ret
, vnode
->lock_state
);
600 * return information about a lock we currently hold, if indeed we hold one
602 static int afs_do_getlk(struct file
*file
, struct file_lock
*fl
)
604 struct afs_vnode
*vnode
= AFS_FS_I(locks_inode(file
));
605 struct key
*key
= afs_file_key(file
);
610 fl
->fl_type
= F_UNLCK
;
612 /* check local lock records first */
613 posix_test_lock(file
, fl
);
614 if (fl
->fl_type
== F_UNLCK
) {
615 /* no local locks; consult the server */
616 ret
= afs_fetch_status(vnode
, key
);
620 lock_count
= READ_ONCE(vnode
->status
.lock_count
);
622 fl
->fl_type
= F_RDLCK
;
624 fl
->fl_type
= F_WRLCK
;
626 fl
->fl_end
= OFFSET_MAX
;
631 _leave(" = %d [%hd]", ret
, fl
->fl_type
);
636 * manage POSIX locks on a file
638 int afs_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
640 struct afs_vnode
*vnode
= AFS_FS_I(locks_inode(file
));
642 _enter("{%x:%u},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
643 vnode
->fid
.vid
, vnode
->fid
.vnode
, cmd
,
644 fl
->fl_type
, fl
->fl_flags
,
645 (long long) fl
->fl_start
, (long long) fl
->fl_end
);
647 /* AFS doesn't support mandatory locks */
648 if (__mandatory_lock(&vnode
->vfs_inode
) && fl
->fl_type
!= F_UNLCK
)
652 return afs_do_getlk(file
, fl
);
653 if (fl
->fl_type
== F_UNLCK
)
654 return afs_do_unlk(file
, fl
);
655 return afs_do_setlk(file
, fl
);
659 * manage FLOCK locks on a file
661 int afs_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
663 struct afs_vnode
*vnode
= AFS_FS_I(locks_inode(file
));
665 _enter("{%x:%u},%d,{t=%x,fl=%x}",
666 vnode
->fid
.vid
, vnode
->fid
.vnode
, cmd
,
667 fl
->fl_type
, fl
->fl_flags
);
670 * No BSD flocks over NFS allowed.
671 * Note: we could try to fake a POSIX lock request here by
672 * using ((u32) filp | 0x80000000) or some such as the pid.
673 * Not sure whether that would be unique, though, or whether
674 * that would break in other places.
676 if (!(fl
->fl_flags
& FL_FLOCK
))
679 /* we're simulating flock() locks using posix locks on the server */
680 if (fl
->fl_type
== F_UNLCK
)
681 return afs_do_unlk(file
, fl
);
682 return afs_do_setlk(file
, fl
);
686 * the POSIX lock management core VFS code copies the lock record and adds the
687 * copy into its own list, so we need to add that copy to the vnode's lock
688 * queue in the same place as the original (which will be deleted shortly
691 static void afs_fl_copy_lock(struct file_lock
*new, struct file_lock
*fl
)
693 struct afs_vnode
*vnode
= AFS_FS_I(locks_inode(fl
->fl_file
));
697 spin_lock(&vnode
->lock
);
698 list_add(&new->fl_u
.afs
.link
, &fl
->fl_u
.afs
.link
);
699 spin_unlock(&vnode
->lock
);
703 * need to remove this lock from the vnode queue when it's removed from the
706 static void afs_fl_release_private(struct file_lock
*fl
)
708 struct afs_vnode
*vnode
= AFS_FS_I(locks_inode(fl
->fl_file
));
712 spin_lock(&vnode
->lock
);
713 afs_dequeue_lock(vnode
, fl
);
714 _debug("state %u for %p", vnode
->lock_state
, vnode
);
715 spin_unlock(&vnode
->lock
);