Commit 33649c91 authored by Miklos Szeredi's avatar Miklos Szeredi Committed by Linus Torvalds

[PATCH] fuse: ensure FLUSH reaches userspace

All POSIX locks owned by the current task are removed on close().  If the
FLUSH request resulting initiated by close() fails to reach userspace, there
might be locks remaining, which cannot be removed.

The only reason it could fail, is if allocating the request fails.  In this
case use the request reserved for RELEASE, or if that is currently used by
another FLUSH, wait for it to become available.
Signed-off-by: default avatarMiklos Szeredi <miklos@szeredi.hu>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 71421259
...@@ -76,6 +76,13 @@ static void __fuse_put_request(struct fuse_req *req) ...@@ -76,6 +76,13 @@ static void __fuse_put_request(struct fuse_req *req)
atomic_dec(&req->count); atomic_dec(&req->count);
} }
static void fuse_req_init_context(struct fuse_req *req)
{
req->in.h.uid = current->fsuid;
req->in.h.gid = current->fsgid;
req->in.h.pid = current->pid;
}
struct fuse_req *fuse_get_req(struct fuse_conn *fc) struct fuse_req *fuse_get_req(struct fuse_conn *fc)
{ {
struct fuse_req *req; struct fuse_req *req;
...@@ -100,9 +107,7 @@ struct fuse_req *fuse_get_req(struct fuse_conn *fc) ...@@ -100,9 +107,7 @@ struct fuse_req *fuse_get_req(struct fuse_conn *fc)
if (!req) if (!req)
goto out; goto out;
req->in.h.uid = current->fsuid; fuse_req_init_context(req);
req->in.h.gid = current->fsgid;
req->in.h.pid = current->pid;
req->waiting = 1; req->waiting = 1;
return req; return req;
...@@ -111,11 +116,86 @@ struct fuse_req *fuse_get_req(struct fuse_conn *fc) ...@@ -111,11 +116,86 @@ struct fuse_req *fuse_get_req(struct fuse_conn *fc)
return ERR_PTR(err); return ERR_PTR(err);
} }
/*
* Return request in fuse_file->reserved_req. However that may
* currently be in use. If that is the case, wait for it to become
* available.
*/
static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
struct file *file)
{
struct fuse_req *req = NULL;
struct fuse_file *ff = file->private_data;
do {
wait_event(fc->blocked_waitq, ff->reserved_req);
spin_lock(&fc->lock);
if (ff->reserved_req) {
req = ff->reserved_req;
ff->reserved_req = NULL;
get_file(file);
req->stolen_file = file;
}
spin_unlock(&fc->lock);
} while (!req);
return req;
}
/*
* Put stolen request back into fuse_file->reserved_req
*/
static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
{
struct file *file = req->stolen_file;
struct fuse_file *ff = file->private_data;
spin_lock(&fc->lock);
fuse_request_init(req);
BUG_ON(ff->reserved_req);
ff->reserved_req = req;
wake_up(&fc->blocked_waitq);
spin_unlock(&fc->lock);
fput(file);
}
/*
* Gets a requests for a file operation, always succeeds
*
* This is used for sending the FLUSH request, which must get to
* userspace, due to POSIX locks which may need to be unlocked.
*
* If allocation fails due to OOM, use the reserved request in
* fuse_file.
*
* This is very unlikely to deadlock accidentally, since the
* filesystem should not have it's own file open. If deadlock is
* intentional, it can still be broken by "aborting" the filesystem.
*/
struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
{
struct fuse_req *req;
atomic_inc(&fc->num_waiting);
wait_event(fc->blocked_waitq, !fc->blocked);
req = fuse_request_alloc();
if (!req)
req = get_reserved_req(fc, file);
fuse_req_init_context(req);
req->waiting = 1;
return req;
}
void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
{ {
if (atomic_dec_and_test(&req->count)) { if (atomic_dec_and_test(&req->count)) {
if (req->waiting) if (req->waiting)
atomic_dec(&fc->num_waiting); atomic_dec(&fc->num_waiting);
if (req->stolen_file)
put_reserved_req(fc, req);
else
fuse_request_free(req); fuse_request_free(req);
} }
} }
......
...@@ -48,8 +48,8 @@ struct fuse_file *fuse_file_alloc(void) ...@@ -48,8 +48,8 @@ struct fuse_file *fuse_file_alloc(void)
struct fuse_file *ff; struct fuse_file *ff;
ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
if (ff) { if (ff) {
ff->release_req = fuse_request_alloc(); ff->reserved_req = fuse_request_alloc();
if (!ff->release_req) { if (!ff->reserved_req) {
kfree(ff); kfree(ff);
ff = NULL; ff = NULL;
} }
...@@ -59,7 +59,7 @@ struct fuse_file *fuse_file_alloc(void) ...@@ -59,7 +59,7 @@ struct fuse_file *fuse_file_alloc(void)
void fuse_file_free(struct fuse_file *ff) void fuse_file_free(struct fuse_file *ff)
{ {
fuse_request_free(ff->release_req); fuse_request_free(ff->reserved_req);
kfree(ff); kfree(ff);
} }
...@@ -115,7 +115,7 @@ int fuse_open_common(struct inode *inode, struct file *file, int isdir) ...@@ -115,7 +115,7 @@ int fuse_open_common(struct inode *inode, struct file *file, int isdir)
struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
int opcode) int opcode)
{ {
struct fuse_req *req = ff->release_req; struct fuse_req *req = ff->reserved_req;
struct fuse_release_in *inarg = &req->misc.release_in; struct fuse_release_in *inarg = &req->misc.release_in;
inarg->fh = ff->fh; inarg->fh = ff->fh;
...@@ -187,10 +187,7 @@ static int fuse_flush(struct file *file, fl_owner_t id) ...@@ -187,10 +187,7 @@ static int fuse_flush(struct file *file, fl_owner_t id)
if (fc->no_flush) if (fc->no_flush)
return 0; return 0;
req = fuse_get_req(fc); req = fuse_get_req_nofail(fc, file);
if (IS_ERR(req))
return PTR_ERR(req);
memset(&inarg, 0, sizeof(inarg)); memset(&inarg, 0, sizeof(inarg));
inarg.fh = ff->fh; inarg.fh = ff->fh;
inarg.lock_owner = fuse_lock_owner_id(id); inarg.lock_owner = fuse_lock_owner_id(id);
......
...@@ -65,7 +65,7 @@ struct fuse_inode { ...@@ -65,7 +65,7 @@ struct fuse_inode {
/** FUSE specific file data */ /** FUSE specific file data */
struct fuse_file { struct fuse_file {
/** Request reserved for flush and release */ /** Request reserved for flush and release */
struct fuse_req *release_req; struct fuse_req *reserved_req;
/** File handle used by userspace */ /** File handle used by userspace */
u64 fh; u64 fh;
...@@ -213,6 +213,9 @@ struct fuse_req { ...@@ -213,6 +213,9 @@ struct fuse_req {
/** Request completion callback */ /** Request completion callback */
void (*end)(struct fuse_conn *, struct fuse_req *); void (*end)(struct fuse_conn *, struct fuse_req *);
/** Request is stolen from fuse_file->reserved_req */
struct file *stolen_file;
}; };
/** /**
...@@ -456,10 +459,15 @@ struct fuse_req *fuse_request_alloc(void); ...@@ -456,10 +459,15 @@ struct fuse_req *fuse_request_alloc(void);
void fuse_request_free(struct fuse_req *req); void fuse_request_free(struct fuse_req *req);
/** /**
* Reserve a preallocated request * Get a request, may fail with -ENOMEM
*/ */
struct fuse_req *fuse_get_req(struct fuse_conn *fc); struct fuse_req *fuse_get_req(struct fuse_conn *fc);
/**
* Gets a requests for a file operation, always succeeds
*/
struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
/** /**
* Decrement reference count of a request. If count goes to zero free * Decrement reference count of a request. If count goes to zero free
* the request. * the request.
......
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