AFS: Implement file locking
Subsystems (2)
David Howells
maintainerPATCH 1/1 AFS: Implement file locking
Implement file locking for AFS.
---
fs/afs/Makefile | 1 fs/afs/afs.h | 8 + fs/afs/afs_fs.h | 3 fs/afs/callback.c | 3 fs/afs/dir.c | 1 fs/afs/file.c | 2 fs/afs/flock.c | 558 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/afs/fsclient.c | 155 ++++++++++++++ fs/afs/internal.h | 30 +++ fs/afs/main.c | 1 fs/afs/misc.c | 1 fs/afs/super.c | 3 fs/afs/vnode.c | 130 +++++++++++- include/linux/fs.h | 4 14 files changed, 885 insertions(+), 15 deletions(-)
fs/afs/Makefile +1 -0
@@ -8,6 +8,7 @@ kafs-objs := \ cmservice.o \ dir.o \ file.o \ + flock.o \ fsclient.o \ inode.o \ main.o \
fs/afs/afs.h +8 -0
@@ -37,6 +37,13 @@ typedef enum {AFS_FTYPE_SYMLINK =3 , } afs_file_type_t; +typedef enum { +AFS_LOCK_READ =0 ,/* read lock request */ +AFS_LOCK_WRITE =1 ,/* write lock request */ +}afs_lock_type_t ; + +#define AFS_LOCKWAIT (5 * 60)/* time until a lock times out (seconds) */ +/* * AFS file identifier */
@@ -120,6 +127,7 @@ struct afs_file_status {struct afs_fid parent;/* parent dir ID for non-dirs only */ time_t mtime_client;/* last time client changed data */ time_t mtime_server;/* last time server changed data */ +s32 lock_count;/* file lock count (0=UNLK -1=WRLCK +ve=#RDLCK */ };/*
fs/afs/afs_fs.h +3 -0
@@ -31,6 +31,9 @@ enum AFS_FS_Operations {FSGETVOLUMEINFO =148 ,/* AFS Get information about a volume */ FSGETVOLUMESTATUS =149 ,/* AFS Get volume status information */ FSGETROOTVOLUME =151 ,/* AFS Get root volume name */ +FSSETLOCK =156 ,/* AFS Request a file lock */ +FSEXTENDLOCK =157 ,/* AFS Extend a file lock */ +FSRELEASELOCK =158 ,/* AFS Release a file lock */ FSLOOKUP =161 ,/* AFS lookup file in directory */ FSFETCHDATA64 =65537 ,/* AFS Fetch file data */ FSSTOREDATA64 = 65538, /* AFS Store file data */
fs/afs/callback.c +3 -0
@@ -125,6 +125,9 @@ static void afs_break_callback(struct afs_server *server,spin_unlock (&server->cb_lock);queue_work (afs_callback_update_worker, &vnode->cb_broken_work); +if (list_empty (&vnode->granted_locks) && + !list_empty (&vnode->pending_locks)) +afs_lock_may_be_available (vnode);spin_unlock (&vnode->lock); } }
fs/afs/dir.c +1 -0
@@ -44,6 +44,7 @@ const struct file_operations afs_dir_file_operations = { .open = afs_dir_open, .release = afs_release, .readdir = afs_readdir, + .lock = afs_lock, };const struct inode_operations afs_dir_inode_operations = {
fs/afs/file.c +2 -0
@@ -35,6 +35,8 @@ const struct file_operations afs_file_operations = { .mmap = afs_mmap, .sendfile = generic_file_sendfile, .fsync = afs_fsync, + .lock = afs_lock, + .flock = afs_flock, };const struct inode_operations afs_file_inode_operations = {
fs/afs/flock.c new file +558 -0
558 lines not on this page · raw message
fs/afs/fsclient.c +154 -1
164 lines not on this page · raw message
fs/afs/internal.h +30 -0
@@ -350,10 +350,18 @@ struct afs_vnode {#define AFS_VNODE_ZAP_DATA 3/* set if vnode's data should be invalidated */ #define AFS_VNODE_DELETED 4/* set if vnode deleted on server */ #define AFS_VNODE_MOUNTPOINT 5/* set if vnode is a mountpoint symlink */ +#define AFS_VNODE_LOCKING 6/* set if waiting for lock on vnode */ +#define AFS_VNODE_READLOCKED 7/* set if vnode is read-locked on the server */ +#define AFS_VNODE_WRITELOCKED 8/* set if vnode is write-locked on the server */ +#define AFS_VNODE_UNLOCKING 9/* set if vnode is being unlocked on the server */ long acl_order;/* ACL check count (callback break count) */ struct list_head writebacks;/* alterations in pagecache that need writing */ +struct list_head pending_locks;/* locks waiting to be granted */ +struct list_head granted_locks;/* locks granted on this file */ +struct delayed_work lock_work;/* work to be done in locking */ +struct key *unlock_key;/* key to be used in unlocking */ /* outstanding callback notification on this file */ struct rb_node server_rb;/* link in server->fs_vnodes */
@@ -473,6 +481,15 @@ extern int afs_open(struct inode *, struct file *);extern int afs_release (struct inode *,struct file *);/* +* flock.c +*/ +extern void __exit afs_kill_lock_manager(void); +extern void afs_lock_work (struct work_struct *); +extern void afs_lock_may_be_available (struct afs_vnode *); +extern int afs_lock (struct file *,int ,struct file_lock *); +extern int afs_flock (struct file *,int ,struct file_lock *); + +/* * fsclient.c */ extern int afs_fs_fetch_file_status(struct afs_server *, struct key *,
@@ -512,6 +529,15 @@ extern int afs_fs_get_volume_status(struct afs_server *, struct key *, struct afs_vnode *, struct afs_volume_status *, const struct afs_wait_mode *); +extern int afs_fs_set_lock (struct afs_server *,struct key *, +struct afs_vnode *,afs_lock_type_t , +const struct afs_wait_mode *); +extern int afs_fs_extend_lock (struct afs_server *,struct key *, +struct afs_vnode *, +const struct afs_wait_mode *); +extern int afs_fs_release_lock (struct afs_server *,struct key *, +struct afs_vnode *, +const struct afs_wait_mode *);/* * inode.c
@@ -680,6 +706,10 @@ extern int afs_vnode_store_data(struct afs_writeback *, pgoff_t, pgoff_t,extern int afs_vnode_setattr (struct afs_vnode *,struct key *,struct iattr *);extern int afs_vnode_get_volume_status (struct afs_vnode *,struct key *,struct afs_volume_status *); +extern int afs_vnode_set_lock (struct afs_vnode *,struct key *, +afs_lock_type_t ); +extern int afs_vnode_extend_lock (struct afs_vnode *,struct key *); +extern int afs_vnode_release_lock (struct afs_vnode *,struct key *);/* * volume.c
fs/afs/main.c +1 -0
@@ -168,6 +168,7 @@ static void __exit afs_exit(void)printk (KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n" );afs_fs_exit (); +afs_kill_lock_manager ();afs_close_socket ();afs_purge_servers ();afs_callback_update_kill ();
fs/afs/misc.c +1 -0
@@ -35,6 +35,7 @@ int afs_abort_to_error(u32 abort_code)case VOVERQUOTA :return -EDQUOT ;case VBUSY :return -EBUSY ;case VMOVED :return -ENXIO ; +case 0x2f6df0a :return -EWOULDBLOCK ;case 0x2f6df0c :return -EACCES ;case 0x2f6df0f :return -EBUSY ;case 0x2f6df10 :return -EEXIST ;
fs/afs/super.c +3 -0
@@ -460,6 +460,9 @@ static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,spin_lock_init (&vnode->writeback_lock);spin_lock_init (&vnode->lock);INIT_LIST_HEAD (&vnode->writebacks); +INIT_LIST_HEAD (&vnode->pending_locks); +INIT_LIST_HEAD (&vnode->granted_locks); +INIT_DELAYED_WORK (&vnode->lock_work, afs_lock_work);INIT_WORK (&vnode->cb_broken_work, afs_broken_callback_work); }
fs/afs/vnode.c +116 -14
147 lines not on this page · raw message
include/linux/fs.h +4 -0
@@ -820,6 +820,10 @@ struct file_lock {union {struct nfs_lock_info nfs_fl;struct nfs4_lock_info nfs4_fl; +struct { +struct list_head link;/* link in AFS vnode's pending_locks list */ +int state;/* state of grant or error if -ve */ + } afs; } fl_u; };
David Howells napsal(a):
Quoted 38 lines
Implement file locking for AFS.
Signed-off-by David Howells <[email protected]>---
fs/afs/Makefile | 1 fs/afs/afs.h | 8 + fs/afs/afs_fs.h | 3 fs/afs/callback.c | 3 fs/afs/dir.c | 1 fs/afs/file.c | 2 fs/afs/flock.c | 558 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/afs/fsclient.c | 155 ++++++++++++++ fs/afs/internal.h | 30 +++ fs/afs/main.c | 1 fs/afs/misc.c | 1 fs/afs/super.c | 3 fs/afs/vnode.c | 130 +++++++++++- include/linux/fs.h | 4 14 files changed, 885 insertions(+), 15 deletions(-)fs/afs/Makefile +1
-0@@ -8,6 +8,7 @@ kafs-objs := \ cmservice.o \ dir.o \ file.o \ + flock.o \ fsclient.o \ inode.o \ main.o \fs/afs/afs.h +0
-0
fs/afs/afs.h typedef enum { · line 43
Quoted 7 lines
fs/afs/afs.h +4
-0@@ -37,6 +37,13 @@ typedef enum {AFS_FTYPE_SYMLINK =3 , } afs_file_type_t; +typedef enum { +AFS_LOCK_READ =0 ,/* read lock request */ +AFS_LOCK_WRITE =1 ,/* write lock request */ +}afs_lock_type_t ;
Why typedef?
regards,
Signature · 4 lines
http://www.fi.muni.cz/~xslaby/ Jiri Slaby faculty of informatics, masaryk university, brno, cz e-mail: jirislaby gmail com, gpg pubkey fingerprint: B674 9967 0407 CE62 ACC8 22A0 32CC 55C3 39D4 7A7E
fs/afs/flock.c line 38
Quoted 11 lines · David Howells
fs/afs/flock.c new file +11
-0+/* +* initialise the lock manager thread if it isn't already running +*/ +static int afs_init_lock_manager (void ) +{ +if (!afs_lock_manager) { + afs_lock_manager =create_singlethread_workqueue ("kafs_lockd" ); +if (!afs_lock_manager) +return -ENOMEM ; + } +return 0 ;
Doesn't this need some locking?
Quoted 5 lines
fs/afs/flock.c new file +5
-0+/* +* request a lock on a file on the server +*/ +static int afs_do_setlk (struct file *file,struct file_lock *fl) +{
fs/afs/flock.c line 234
Quoted 10 lines
fs/afs/flock.c new file +10
-0+struct afs_vnode *vnode =AFS_FS_I (file->f_mapping->host); +afs_lock_type_t type; +struct key *key = file->private_data; +int ret; + +_enter ("{%x:%u},%u" , vnode->fid.vid, vnode->fid.vnode, fl->fl_type); + +/* only whole-file locks are supported */ +if (fl->fl_start !=0 || fl->fl_end !=OFFSET_MAX ) +return -EINVAL ;
Do you allow upgrades and downgrades? (Just curious.)
fs/afs/flock.c line 294
Quoted 2 lines
fs/afs/flock.c new file +2
-0+/* if we've already got a readlock on the server and no waiting +* writelocks, then we might be able to instantly grant another
Is that comment correct? (You don't really test for "waiting writelocks", do you?)
fs/afs/flock.c line 305
Quoted 11 lines
fs/afs/flock.c new file +11
-0+* readlock */ +if (type ==AFS_LOCK_READ && + vnode->flags & (1 <<AFS_VNODE_READLOCKED )) { +_debug ("instant readlock" ); +ASSERTCMP (vnode->flags & + ((1 <<AFS_VNODE_LOCKING ) | + (1 << AFS_VNODE_WRITELOCKED)), ==, 0); +ASSERT (!list_empty (&vnode->granted_locks)); + goto sharing_existing_lock; + } + }
--b.
Quoted 6 lines · J. Bruce Fields
fs/afs/flock.c line 234
Quoted 3 lines · David Howells
fs/afs/flock.c new file +3
-0+/* only whole-file locks are supported */ +if (fl->fl_start !=0 || fl->fl_end !=OFFSET_MAX ) +return -EINVAL ;Do you allow upgrades and downgrades? (Just curious.)
I was actually under the impression that OpenAFS had support for byte-range locking (as well as lock upgrade/downgrade); though IIRC there was some secondary protocol. That's probably why the support is so basic at the moment; David's getting the basics in first and the more complicated stuff can come later.
Cheers,
Kyle Moffett
David Howells
maintainerQuoted 2 lines · Kyle Moffett
I was actually under the impression that OpenAFS had support for byte- range locking (as well as lock upgrade/downgrade);
As far as I know, there is no support for byte-range locking at all in the AFS protocol itself. The client can try to emulate byte-range locking locally, but that's about it.
David
David Howells
maintainerQuoted 8 lines · J. Bruce Fields
fs/afs/flock.c line 38
Quoted 6 lines
fs/afs/flock.c new file +6
-0+if (!afs_lock_manager) { + afs_lock_manager =create_singlethread_workqueue ("kafs_lockd" ); +if (!afs_lock_manager) +return -ENOMEM ; + } +return 0 ;Doesn't this need some locking?
Oops. Yes. It used to be inside the lock_kernel() section, but has since escaped.
Quoted 1 line
Do you allow upgrades and downgrades? (Just curious.)
AFS does not, as far as I know. Upgrades are dangerous anyway as you can get deadlock quite easily.
Quoted 5 lines
fs/afs/flock.c line 294
Quoted 2 lines
fs/afs/flock.c new file +2
-0+/* if we've already got a readlock on the server and no waiting +* writelocks, then we might be able to instantly grant another Is that comment correct? (You don't really test for "waiting writelocks", do you?)
Locally, yes. 'if (list_empty(&vnode->pending_locks))' covers it quite handily. I can't do anything about checking the server.
David
Quoted 11 lines · David Howells
Quoted 8 lines · J. Bruce Fields
fs/afs/flock.c line 38
Quoted 6 lines
fs/afs/flock.c new file +6
-0+if (!afs_lock_manager) { + afs_lock_manager =create_singlethread_workqueue ("kafs_lockd" ); +if (!afs_lock_manager) +return -ENOMEM ; + } +return 0 ;Doesn't this need some locking?
Quoted 2 lines
Oops. Yes. It used to be inside the
lock_kernel()section, but has since escaped.
The BKL wouldn't help, since create_singlethread_workqueue() can sleep. Or am I missing something?
Quoted 3 lines
Quoted 1 line
Do you allow upgrades and downgrades? (Just curious.)
AFS does not, as far as I know.
So if I request a write lock while holding a read lock, my request will be denied?
Quoted 8 lines
Quoted 5 lines
fs/afs/flock.c line 294
Quoted 2 lines
fs/afs/flock.c new file +2
-0+/* if we've already got a readlock on the server and no waiting +* writelocks, then we might be able to instantly grant another Is that comment correct? (You don't really test for "waiting writelocks", do you?)
Locally, yes. 'if (
list_empty(&vnode->pending_locks))' covers it quite handily.
Oops, right, I was overlooking that check.
This is a little strange, though--if there's somebody waiting for a write lock on an inode (because somebody else already holds a read lock on it), that shouldn't block requests for read locks.
--b.
David Howells
maintainerQuoted 6 lines · J. Bruce Fields
Quoted 3 lines
Quoted 1 line
Do you allow upgrades and downgrades? (Just curious.)
AFS does not, as far as I know.
So if I request a write lock while holding a read lock, my request will be denied?
At the moment, yes. Don't the POSIX and flock lock-handling routines in the kernel normally do that anyway?
Quoted 3 lines
This is a little strange, though--if there's somebody waiting for a write lock on an inode (because somebody else already holds a read lock on it), that shouldn't block requests for read locks.
That depends on whether you want fairness or not. Allowing read locks to jump the queue like this can lead to starvation for your writers.
David
Quoted 6 lines · David Howells
Quoted 2 lines · J. Bruce Fields
So if I request a write lock while holding a read lock, my request will be denied?
At the moment, yes. Don't the POSIX and flock lock-handling routines in the kernel normally do that anyway?
No, they'd upgrade in that case.
Quoted 5 lines
Quoted 3 lines
This is a little strange, though--if there's somebody waiting for a write lock on an inode (because somebody else already holds a read lock on it), that shouldn't block requests for read locks.
That depends on whether you want fairness or not.
Neither posix nor flock locks delay a lock because of pending conflicting locks. SUS, as I read it, wouldn't allow that.
Quoted 2 lines
Allowing read locks to jump the queue like this can lead to starvation for your writers.
If you want fairness the best that you can do is to ensure that when more than one pending lock can be applied, the one that has been waiting longest will be chosen. But you can't make all such lock requests wait for a lock that hasn't even been applied yet.
You can contrive examples of applications that would be correct given the standard fcntl behavior, but that would deadlock on a system that didn't allow read locks to jump the queue in the above situation. I have no idea whether such applications actually exist in practice, but I'd be uneasy about changing the standard behavior without inventing some new kind of lock.
--b.
David Howells
maintainerQuoted 3 lines · J. Bruce Fields
You can contrive examples of applications that would be correct given the standard fcntl behavior, but that would deadlock on a system that didn't allow read locks to jump the queue in the above situation.
Yes, but you can also contrive starvation if you allow locks to jump the queue. Obviously, the Linux kernel behaviour is to allow readlocks to jump the queue if a readlock is currently in force, so I have to conform to that, whether I like it or not.
I'll need to test the upgrade/downgrade case. I don't know whether the AFS server supports that. If it doesn't, I can emulate downgrade, but not upgrade
- not unless I only ever ask it for exclusive locks.
Lock upgrading is really, really easy to contrive deadlock for.
David
Quoted 5 lines · David Howells
I'll need to test the upgrade/downgrade case. I don't know whether the AFS server supports that. If it doesn't, I can emulate downgrade, but not upgrade
- not unless I only ever ask it for exclusive locks.Lock upgrading is really, really easy to contrive deadlock for.
Any such deadlock is the user's fault.
But, right, I agree that upgrades are probably hard to use correctly. And that implementing them shouldn't be a priority in the case of AFS. Just as long as the implementation doesn't completely fall over when somebody attempts an upgrade.
--b.
David Howells
maintainerQuoted 4 lines · J. Bruce Fields
Quoted 2 lines
At the moment, yes. Don't the POSIX and flock lock-handling routines in the kernel normally do that anyway?
No, they'd upgrade in that case.
I just checked. The OpenAFS server supports neither lock upgrading nor lock downgrading. Attempts to do either incur an abort with code 0x02f6df0a (which I believe to be equivalent to EAGAIN).
This means that I can't practically support lock upgrading. Lock downgrading I can emulate by handing apparent readlocks to local processes whilst holding a writelock on the server.
David