Skip to contentSkip to conversation

AFS: Implement file locking [try #2]

PATCH In review David Howells 2 participants 5 messages May 29, 2007 – May 31, 2007

Subsystems (2)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH 1/1 AFS: Implement file locking [try #2]

Implement file locking for AFS.

[try #2]:

(*) Start the lock manager thread under a mutex to avoid a race.

(*) Made the locking non-fair: New readlocks will jump pending writelocks if
there's a readlock currently granted on a file. This makes the behaviour similar to Linux's VFS locking.

Regrading of locks is not currently supported as this is not supported by the server. Byte-range locking is also not currently supported for the same reason.

---

 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     |  590 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 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, 917 insertions(+), 15 deletions(-)
fs/afs/Makefile +1 -0
index 73ce561..a666710 100644
@@ -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
index 2452579..c548aa3 100644
@@ -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
index a18c374..eb64732 100644
@@ -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
index bacf518..b824394 100644
@@ -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
index 546c595..33fe39a 100644
@@ -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
index 1547500..8aaa233 100644
@@ -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 +590 -0

590 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
index bc39eba..f8a41bd 100644
@@ -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
index cd21195..0f60f6b 100644
@@ -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
index d1a889c..2d33a5f 100644
@@ -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
index 2e8496b..993cdf1 100644
@@ -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
index 7cf0c54..c81fb3b 100644
@@ -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;
 };
 
#1 lore raw

One more vague question I had while skimming the previous version--

Quoted 7 lines · David Howells
fs/afs/flock.c new file +7 -0
+static void afs_grant_locks(struct afs_vnode *vnode, struct file_lock *fl)
+{
+	struct file_lock *p, *_p;
+
+	list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
+	if (fl->fl_type == F_RDLCK) {
+		list_for_each_entry_safe(p, _p, &vnode->pending_locks,

fs/afs/flock.c line 100

Quoted 10 lines
fs/afs/flock.c new file +10 -0
+					 fl_u.afs.link) {
+			if (p->fl_type == F_RDLCK) {
+				p->fl_u.afs.state = AFS_LOCK_GRANTED;
+				list_move_tail(&p->fl_u.afs.link,
+					       &vnode->granted_locks);
+				wake_up(&p->fl_wait);
+			}
+		}
+	}
+}

--without having tried to understand how they're actually used, these data structures (like the pending_locks and granted_locks lists) seem to duplicate stuff that's already kept in fs/locks.c. Is there a reason they're required?

--b.

author maintainer
#2 lore raw
Quoted 4 lines · J. Bruce Fields

--without having tried to understand how they're actually used, these data structures (like the pending_locks and granted_locks lists) seem to duplicate stuff that's already kept in fs/locks.c. Is there a reason they're required?

Yes. I need to get the server lock first, before going to the VFS locking routines. The VFS routines as they stand aren't particularly useful, and they permit things that AFS doesn't support (such as lock upgrading).

David

#3 lore raw
Quoted 9 lines · David Howells
Quoted 4 lines · J. Bruce Fields

--without having tried to understand how they're actually used, these data structures (like the pending_locks and granted_locks lists) seem to duplicate stuff that's already kept in fs/locks.c. Is there a reason they're required?

Yes. I need to get the server lock first, before going to the VFS locking routines.

That doesn't really answer the question. The NFS client has similar requirements, but it doesn't have to duplicate the per-inode lists of granted locks, for example.

Quoted 2 lines

The VFS routines as they stand aren't particularly useful, and they permit things that AFS doesn't support (such as lock upgrading).

Sure. But handling those things is just a matter of checking for a few special cases and making sure they're rejected before calling the posix routines.

--b.

author maintainer
#4 lore raw
Quoted 6 lines · J. Bruce Fields
Quoted 2 lines

Yes. I need to get the server lock first, before going to the VFS locking routines.

That doesn't really answer the question. The NFS client has similar requirements, but it doesn't have to duplicate the per-inode lists of granted locks, for example.

Actually, it might... It's just that they're in the lock manager server, not in the NFS client.

As far as I can tell, NFS passes each lock request individually to the lock manager server, which grants them individually. AFS doesn't do that.

David