Commit e8d6c554 authored by David Howells's avatar David Howells Committed by Linus Torvalds

AFS: implement file locking

Implement file locking for AFS.
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent b0fed314
...@@ -8,6 +8,7 @@ kafs-objs := \ ...@@ -8,6 +8,7 @@ kafs-objs := \
cmservice.o \ cmservice.o \
dir.o \ dir.o \
file.o \ file.o \
flock.o \
fsclient.o \ fsclient.o \
inode.o \ inode.o \
main.o \ main.o \
......
...@@ -37,6 +37,13 @@ typedef enum { ...@@ -37,6 +37,13 @@ typedef enum {
AFS_FTYPE_SYMLINK = 3, AFS_FTYPE_SYMLINK = 3,
} afs_file_type_t; } 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 * AFS file identifier
*/ */
...@@ -120,6 +127,7 @@ struct afs_file_status { ...@@ -120,6 +127,7 @@ struct afs_file_status {
struct afs_fid parent; /* parent dir ID for non-dirs only */ struct afs_fid parent; /* parent dir ID for non-dirs only */
time_t mtime_client; /* last time client changed data */ time_t mtime_client; /* last time client changed data */
time_t mtime_server; /* last time server changed data */ time_t mtime_server; /* last time server changed data */
s32 lock_count; /* file lock count (0=UNLK -1=WRLCK +ve=#RDLCK */
}; };
/* /*
......
...@@ -31,6 +31,9 @@ enum AFS_FS_Operations { ...@@ -31,6 +31,9 @@ enum AFS_FS_Operations {
FSGETVOLUMEINFO = 148, /* AFS Get information about a volume */ FSGETVOLUMEINFO = 148, /* AFS Get information about a volume */
FSGETVOLUMESTATUS = 149, /* AFS Get volume status information */ FSGETVOLUMESTATUS = 149, /* AFS Get volume status information */
FSGETROOTVOLUME = 151, /* AFS Get root volume name */ 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 */ FSLOOKUP = 161, /* AFS lookup file in directory */
FSFETCHDATA64 = 65537, /* AFS Fetch file data */ FSFETCHDATA64 = 65537, /* AFS Fetch file data */
FSSTOREDATA64 = 65538, /* AFS Store file data */ FSSTOREDATA64 = 65538, /* AFS Store file data */
......
...@@ -125,6 +125,9 @@ static void afs_break_callback(struct afs_server *server, ...@@ -125,6 +125,9 @@ static void afs_break_callback(struct afs_server *server,
spin_unlock(&server->cb_lock); spin_unlock(&server->cb_lock);
queue_work(afs_callback_update_worker, &vnode->cb_broken_work); 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); spin_unlock(&vnode->lock);
} }
} }
......
...@@ -44,6 +44,7 @@ const struct file_operations afs_dir_file_operations = { ...@@ -44,6 +44,7 @@ const struct file_operations afs_dir_file_operations = {
.open = afs_dir_open, .open = afs_dir_open,
.release = afs_release, .release = afs_release,
.readdir = afs_readdir, .readdir = afs_readdir,
.lock = afs_lock,
}; };
const struct inode_operations afs_dir_inode_operations = { const struct inode_operations afs_dir_inode_operations = {
......
...@@ -34,6 +34,8 @@ const struct file_operations afs_file_operations = { ...@@ -34,6 +34,8 @@ const struct file_operations afs_file_operations = {
.mmap = generic_file_readonly_mmap, .mmap = generic_file_readonly_mmap,
.splice_read = generic_file_splice_read, .splice_read = generic_file_splice_read,
.fsync = afs_fsync, .fsync = afs_fsync,
.lock = afs_lock,
.flock = afs_flock,
}; };
const struct inode_operations afs_file_inode_operations = { const struct inode_operations afs_file_inode_operations = {
......
This diff is collapsed.
...@@ -67,7 +67,7 @@ static void xdr_decode_AFSFetchStatus(const __be32 **_bp, ...@@ -67,7 +67,7 @@ static void xdr_decode_AFSFetchStatus(const __be32 **_bp,
EXTRACT(status->group); EXTRACT(status->group);
bp++; /* sync counter */ bp++; /* sync counter */
data_version |= (u64) ntohl(*bp++) << 32; data_version |= (u64) ntohl(*bp++) << 32;
bp++; /* lock count */ EXTRACT(status->lock_count);
size |= (u64) ntohl(*bp++) << 32; size |= (u64) ntohl(*bp++) << 32;
bp++; /* spare 4 */ bp++; /* spare 4 */
*_bp = bp; *_bp = bp;
...@@ -1748,3 +1748,156 @@ int afs_fs_get_volume_status(struct afs_server *server, ...@@ -1748,3 +1748,156 @@ int afs_fs_get_volume_status(struct afs_server *server,
return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode); return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
} }
/*
* deliver reply data to an FS.SetLock, FS.ExtendLock or FS.ReleaseLock
*/
static int afs_deliver_fs_xxxx_lock(struct afs_call *call,
struct sk_buff *skb, bool last)
{
const __be32 *bp;
_enter("{%u},{%u},%d", call->unmarshall, skb->len, last);
afs_transfer_reply(call, skb);
if (!last)
return 0;
if (call->reply_size != call->reply_max)
return -EBADMSG;
/* unmarshall the reply once we've received all of it */
bp = call->buffer;
/* xdr_decode_AFSVolSync(&bp, call->replyX); */
_leave(" = 0 [done]");
return 0;
}
/*
* FS.SetLock operation type
*/
static const struct afs_call_type afs_RXFSSetLock = {
.name = "FS.SetLock",
.deliver = afs_deliver_fs_xxxx_lock,
.abort_to_error = afs_abort_to_error,
.destructor = afs_flat_call_destructor,
};
/*
* FS.ExtendLock operation type
*/
static const struct afs_call_type afs_RXFSExtendLock = {
.name = "FS.ExtendLock",
.deliver = afs_deliver_fs_xxxx_lock,
.abort_to_error = afs_abort_to_error,
.destructor = afs_flat_call_destructor,
};
/*
* FS.ReleaseLock operation type
*/
static const struct afs_call_type afs_RXFSReleaseLock = {
.name = "FS.ReleaseLock",
.deliver = afs_deliver_fs_xxxx_lock,
.abort_to_error = afs_abort_to_error,
.destructor = afs_flat_call_destructor,
};
/*
* get a lock on a file
*/
int afs_fs_set_lock(struct afs_server *server,
struct key *key,
struct afs_vnode *vnode,
afs_lock_type_t type,
const struct afs_wait_mode *wait_mode)
{
struct afs_call *call;
__be32 *bp;
_enter("");
call = afs_alloc_flat_call(&afs_RXFSSetLock, 5 * 4, 6 * 4);
if (!call)
return -ENOMEM;
call->key = key;
call->reply = vnode;
call->service_id = FS_SERVICE;
call->port = htons(AFS_FS_PORT);
/* marshall the parameters */
bp = call->request;
*bp++ = htonl(FSSETLOCK);
*bp++ = htonl(vnode->fid.vid);
*bp++ = htonl(vnode->fid.vnode);
*bp++ = htonl(vnode->fid.unique);
*bp++ = htonl(type);
return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
}
/*
* extend a lock on a file
*/
int afs_fs_extend_lock(struct afs_server *server,
struct key *key,
struct afs_vnode *vnode,
const struct afs_wait_mode *wait_mode)
{
struct afs_call *call;
__be32 *bp;
_enter("");
call = afs_alloc_flat_call(&afs_RXFSExtendLock, 4 * 4, 6 * 4);
if (!call)
return -ENOMEM;
call->key = key;
call->reply = vnode;
call->service_id = FS_SERVICE;
call->port = htons(AFS_FS_PORT);
/* marshall the parameters */
bp = call->request;
*bp++ = htonl(FSEXTENDLOCK);
*bp++ = htonl(vnode->fid.vid);
*bp++ = htonl(vnode->fid.vnode);
*bp++ = htonl(vnode->fid.unique);
return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
}
/*
* release a lock on a file
*/
int afs_fs_release_lock(struct afs_server *server,
struct key *key,
struct afs_vnode *vnode,
const struct afs_wait_mode *wait_mode)
{
struct afs_call *call;
__be32 *bp;
_enter("");
call = afs_alloc_flat_call(&afs_RXFSReleaseLock, 4 * 4, 6 * 4);
if (!call)
return -ENOMEM;
call->key = key;
call->reply = vnode;
call->service_id = FS_SERVICE;
call->port = htons(AFS_FS_PORT);
/* marshall the parameters */
bp = call->request;
*bp++ = htonl(FSRELEASELOCK);
*bp++ = htonl(vnode->fid.vid);
*bp++ = htonl(vnode->fid.vnode);
*bp++ = htonl(vnode->fid.unique);
return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
}
...@@ -351,10 +351,18 @@ struct afs_vnode { ...@@ -351,10 +351,18 @@ struct afs_vnode {
#define AFS_VNODE_ZAP_DATA 3 /* set if vnode's data should be invalidated */ #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_DELETED 4 /* set if vnode deleted on server */
#define AFS_VNODE_MOUNTPOINT 5 /* set if vnode is a mountpoint symlink */ #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) */ long acl_order; /* ACL check count (callback break count) */
struct list_head writebacks; /* alterations in pagecache that need writing */ 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 */ /* outstanding callback notification on this file */
struct rb_node server_rb; /* link in server->fs_vnodes */ struct rb_node server_rb; /* link in server->fs_vnodes */
...@@ -473,6 +481,15 @@ extern const struct file_operations afs_file_operations; ...@@ -473,6 +481,15 @@ extern const struct file_operations afs_file_operations;
extern int afs_open(struct inode *, struct file *); extern int afs_open(struct inode *, struct file *);
extern int afs_release(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 * fsclient.c
*/ */
...@@ -513,6 +530,15 @@ extern int afs_fs_get_volume_status(struct afs_server *, struct key *, ...@@ -513,6 +530,15 @@ extern int afs_fs_get_volume_status(struct afs_server *, struct key *,
struct afs_vnode *, struct afs_vnode *,
struct afs_volume_status *, struct afs_volume_status *,
const struct afs_wait_mode *); 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 * inode.c
...@@ -681,6 +707,10 @@ extern int afs_vnode_store_data(struct afs_writeback *, pgoff_t, pgoff_t, ...@@ -681,6 +707,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_setattr(struct afs_vnode *, struct key *, struct iattr *);
extern int afs_vnode_get_volume_status(struct afs_vnode *, struct key *, extern int afs_vnode_get_volume_status(struct afs_vnode *, struct key *,
struct afs_volume_status *); 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 * volume.c
......
...@@ -168,6 +168,7 @@ static void __exit afs_exit(void) ...@@ -168,6 +168,7 @@ static void __exit afs_exit(void)
printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n"); printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
afs_fs_exit(); afs_fs_exit();
afs_kill_lock_manager();
afs_close_socket(); afs_close_socket();
afs_purge_servers(); afs_purge_servers();
afs_callback_update_kill(); afs_callback_update_kill();
......
...@@ -35,6 +35,7 @@ int afs_abort_to_error(u32 abort_code) ...@@ -35,6 +35,7 @@ int afs_abort_to_error(u32 abort_code)
case VOVERQUOTA: return -EDQUOT; case VOVERQUOTA: return -EDQUOT;
case VBUSY: return -EBUSY; case VBUSY: return -EBUSY;
case VMOVED: return -ENXIO; case VMOVED: return -ENXIO;
case 0x2f6df0a: return -EWOULDBLOCK;
case 0x2f6df0c: return -EACCES; case 0x2f6df0c: return -EACCES;
case 0x2f6df0f: return -EBUSY; case 0x2f6df0f: return -EBUSY;
case 0x2f6df10: return -EEXIST; case 0x2f6df10: return -EEXIST;
......
...@@ -460,6 +460,9 @@ static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep, ...@@ -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->writeback_lock);
spin_lock_init(&vnode->lock); spin_lock_init(&vnode->lock);
INIT_LIST_HEAD(&vnode->writebacks); 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); INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
} }
......
...@@ -887,11 +887,6 @@ int afs_vnode_get_volume_status(struct afs_vnode *vnode, struct key *key, ...@@ -887,11 +887,6 @@ int afs_vnode_get_volume_status(struct afs_vnode *vnode, struct key *key,
vnode->fid.unique, vnode->fid.unique,
key_serial(key)); key_serial(key));
/* this op will fetch the status */
spin_lock(&vnode->lock);
vnode->update_cnt++;
spin_unlock(&vnode->lock);
do { do {
/* pick a server to query */ /* pick a server to query */
server = afs_volume_pick_fileserver(vnode); server = afs_volume_pick_fileserver(vnode);
...@@ -905,20 +900,127 @@ int afs_vnode_get_volume_status(struct afs_vnode *vnode, struct key *key, ...@@ -905,20 +900,127 @@ int afs_vnode_get_volume_status(struct afs_vnode *vnode, struct key *key,
} while (!afs_volume_release_fileserver(vnode, server, ret)); } while (!afs_volume_release_fileserver(vnode, server, ret));
/* adjust the flags */ /* adjust the flags */
if (ret == 0) { if (ret == 0)
afs_vnode_finalise_status_update(vnode, server); afs_put_server(server);
_leave(" = %d", ret);
return ret;
no_server:
return PTR_ERR(server);
}
/*
* get a lock on a file
*/
int afs_vnode_set_lock(struct afs_vnode *vnode, struct key *key,
afs_lock_type_t type)
{
struct afs_server *server;
int ret;
_enter("%s{%x:%u.%u},%x,%u",
vnode->volume->vlocation->vldb.name,
vnode->fid.vid,
vnode->fid.vnode,
vnode->fid.unique,
key_serial(key), type);
do {
/* pick a server to query */
server = afs_volume_pick_fileserver(vnode);
if (IS_ERR(server))
goto no_server;
_debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
ret = afs_fs_set_lock(server, key, vnode, type, &afs_sync_call);
} while (!afs_volume_release_fileserver(vnode, server, ret));
/* adjust the flags */
if (ret == 0)
afs_put_server(server);
_leave(" = %d", ret);
return ret;
no_server:
return PTR_ERR(server);
}
/*
* extend a lock on a file
*/
int afs_vnode_extend_lock(struct afs_vnode *vnode, struct key *key)
{
struct afs_server *server;
int ret;
_enter("%s{%x:%u.%u},%x",
vnode->volume->vlocation->vldb.name,
vnode->fid.vid,
vnode->fid.vnode,
vnode->fid.unique,
key_serial(key));
do {
/* pick a server to query */
server = afs_volume_pick_fileserver(vnode);
if (IS_ERR(server))
goto no_server;
_debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
ret = afs_fs_extend_lock(server, key, vnode, &afs_sync_call);
} while (!afs_volume_release_fileserver(vnode, server, ret));
/* adjust the flags */
if (ret == 0)
afs_put_server(server);
_leave(" = %d", ret);
return ret;
no_server:
return PTR_ERR(server);
}
/*
* release a lock on a file
*/
int afs_vnode_release_lock(struct afs_vnode *vnode, struct key *key)
{
struct afs_server *server;
int ret;
_enter("%s{%x:%u.%u},%x",
vnode->volume->vlocation->vldb.name,
vnode->fid.vid,
vnode->fid.vnode,
vnode->fid.unique,
key_serial(key));
do {
/* pick a server to query */
server = afs_volume_pick_fileserver(vnode);
if (IS_ERR(server))
goto no_server;
_debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
ret = afs_fs_release_lock(server, key, vnode, &afs_sync_call);
} while (!afs_volume_release_fileserver(vnode, server, ret));
/* adjust the flags */
if (ret == 0)
afs_put_server(server); afs_put_server(server);
} else {
afs_vnode_status_update_failed(vnode, ret);
}
_leave(" = %d", ret); _leave(" = %d", ret);
return ret; return ret;
no_server: no_server:
spin_lock(&vnode->lock);
vnode->update_cnt--;
ASSERTCMP(vnode->update_cnt, >=, 0);
spin_unlock(&vnode->lock);
return PTR_ERR(server); return PTR_ERR(server);
} }
...@@ -820,6 +820,10 @@ struct file_lock { ...@@ -820,6 +820,10 @@ struct file_lock {
union { union {
struct nfs_lock_info nfs_fl; struct nfs_lock_info nfs_fl;
struct nfs4_lock_info nfs4_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; } fl_u;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment