aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiklos Szeredi <mszeredi@suse.cz>2007-10-17 02:31:00 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-10-17 11:43:03 -0400
commitc756e0a4d79202535774806f148026e40466a5eb (patch)
treeaa769ecfe2204e2e1969108d2c391b88b95e983b
parentde5e3dec421c44c999071b8f7e0580ad2ade92ae (diff)
fuse: add reference counting to fuse_file
Make lifetime of 'struct fuse_file' independent from 'struct file' by adding a reference counter and destructor. This will enable asynchronous page writeback, where it cannot be guaranteed, that the file is not released while a request with this file handle is being served. The actual RELEASE request is only sent when there are no more references to the fuse_file. Signed-off-by: Miklos Szeredi <mszeredi@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--fs/fuse/dev.c2
-rw-r--r--fs/fuse/dir.c14
-rw-r--r--fs/fuse/file.c72
-rw-r--r--fs/fuse/fuse_i.h13
4 files changed, 59 insertions, 42 deletions
diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c
index 9c9e35e42bfd..de25bff31420 100644
--- a/fs/fuse/dev.c
+++ b/fs/fuse/dev.c
@@ -233,8 +233,6 @@ static void request_end(struct fuse_conn *fc, struct fuse_req *req)
233 spin_unlock(&fc->lock); 233 spin_unlock(&fc->lock);
234 dput(req->dentry); 234 dput(req->dentry);
235 mntput(req->vfsmount); 235 mntput(req->vfsmount);
236 if (req->file)
237 fput(req->file);
238 wake_up(&req->waitq); 236 wake_up(&req->waitq);
239 if (end) 237 if (end)
240 end(fc, req); 238 end(fc, req);
diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
index bd5a772d8ccf..35e5cabb3b8c 100644
--- a/fs/fuse/dir.c
+++ b/fs/fuse/dir.c
@@ -288,12 +288,11 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
288static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff, 288static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
289 u64 nodeid, int flags) 289 u64 nodeid, int flags)
290{ 290{
291 struct fuse_req *req; 291 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
292 292 ff->reserved_req->force = 1;
293 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE); 293 request_send(fc, ff->reserved_req);
294 req->force = 1; 294 fuse_put_request(fc, ff->reserved_req);
295 request_send(fc, req); 295 kfree(ff);
296 fuse_put_request(fc, req);
297} 296}
298 297
299/* 298/*
@@ -859,6 +858,7 @@ static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
859 struct page *page; 858 struct page *page;
860 struct inode *inode = file->f_path.dentry->d_inode; 859 struct inode *inode = file->f_path.dentry->d_inode;
861 struct fuse_conn *fc = get_fuse_conn(inode); 860 struct fuse_conn *fc = get_fuse_conn(inode);
861 struct fuse_file *ff = file->private_data;
862 struct fuse_req *req; 862 struct fuse_req *req;
863 863
864 if (is_bad_inode(inode)) 864 if (is_bad_inode(inode))
@@ -875,7 +875,7 @@ static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
875 } 875 }
876 req->num_pages = 1; 876 req->num_pages = 1;
877 req->pages[0] = page; 877 req->pages[0] = page;
878 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR); 878 fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
879 request_send(fc, req); 879 request_send(fc, req);
880 nbytes = req->out.args[0].size; 880 nbytes = req->out.args[0].size;
881 err = req->out.h.error; 881 err = req->out.h.error;
diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 11f22a3d728a..90ce7c5f7b59 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -54,6 +54,7 @@ struct fuse_file *fuse_file_alloc(void)
54 kfree(ff); 54 kfree(ff);
55 ff = NULL; 55 ff = NULL;
56 } 56 }
57 atomic_set(&ff->count, 0);
57 } 58 }
58 return ff; 59 return ff;
59} 60}
@@ -64,6 +65,22 @@ void fuse_file_free(struct fuse_file *ff)
64 kfree(ff); 65 kfree(ff);
65} 66}
66 67
68static struct fuse_file *fuse_file_get(struct fuse_file *ff)
69{
70 atomic_inc(&ff->count);
71 return ff;
72}
73
74static void fuse_file_put(struct fuse_file *ff)
75{
76 if (atomic_dec_and_test(&ff->count)) {
77 struct fuse_req *req = ff->reserved_req;
78 struct fuse_conn *fc = get_fuse_conn(req->dentry->d_inode);
79 request_send_background(fc, req);
80 kfree(ff);
81 }
82}
83
67void fuse_finish_open(struct inode *inode, struct file *file, 84void fuse_finish_open(struct inode *inode, struct file *file,
68 struct fuse_file *ff, struct fuse_open_out *outarg) 85 struct fuse_file *ff, struct fuse_open_out *outarg)
69{ 86{
@@ -72,7 +89,7 @@ void fuse_finish_open(struct inode *inode, struct file *file,
72 if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) 89 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
73 invalidate_mapping_pages(inode->i_mapping, 0, -1); 90 invalidate_mapping_pages(inode->i_mapping, 0, -1);
74 ff->fh = outarg->fh; 91 ff->fh = outarg->fh;
75 file->private_data = ff; 92 file->private_data = fuse_file_get(ff);
76} 93}
77 94
78int fuse_open_common(struct inode *inode, struct file *file, int isdir) 95int fuse_open_common(struct inode *inode, struct file *file, int isdir)
@@ -113,8 +130,7 @@ int fuse_open_common(struct inode *inode, struct file *file, int isdir)
113 return err; 130 return err;
114} 131}
115 132
116struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, 133void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
117 int opcode)
118{ 134{
119 struct fuse_req *req = ff->reserved_req; 135 struct fuse_req *req = ff->reserved_req;
120 struct fuse_release_in *inarg = &req->misc.release_in; 136 struct fuse_release_in *inarg = &req->misc.release_in;
@@ -126,25 +142,24 @@ struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
126 req->in.numargs = 1; 142 req->in.numargs = 1;
127 req->in.args[0].size = sizeof(struct fuse_release_in); 143 req->in.args[0].size = sizeof(struct fuse_release_in);
128 req->in.args[0].value = inarg; 144 req->in.args[0].value = inarg;
129 kfree(ff);
130
131 return req;
132} 145}
133 146
134int fuse_release_common(struct inode *inode, struct file *file, int isdir) 147int fuse_release_common(struct inode *inode, struct file *file, int isdir)
135{ 148{
136 struct fuse_file *ff = file->private_data; 149 struct fuse_file *ff = file->private_data;
137 if (ff) { 150 if (ff) {
138 struct fuse_conn *fc = get_fuse_conn(inode); 151 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
139 struct fuse_req *req; 152 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
140
141 req = fuse_release_fill(ff, get_node_id(inode), file->f_flags,
142 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
143 153
144 /* Hold vfsmount and dentry until release is finished */ 154 /* Hold vfsmount and dentry until release is finished */
145 req->vfsmount = mntget(file->f_path.mnt); 155 ff->reserved_req->vfsmount = mntget(file->f_path.mnt);
146 req->dentry = dget(file->f_path.dentry); 156 ff->reserved_req->dentry = dget(file->f_path.dentry);
147 request_send_background(fc, req); 157 /*
158 * Normally this will send the RELEASE request,
159 * however if some asynchronous READ or WRITE requests
160 * are outstanding, the sending will be delayed
161 */
162 fuse_file_put(ff);
148 } 163 }
149 164
150 /* Return value is ignored by VFS */ 165 /* Return value is ignored by VFS */
@@ -264,10 +279,9 @@ static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
264 return fuse_fsync_common(file, de, datasync, 0); 279 return fuse_fsync_common(file, de, datasync, 0);
265} 280}
266 281
267void fuse_read_fill(struct fuse_req *req, struct file *file, 282void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
268 struct inode *inode, loff_t pos, size_t count, int opcode) 283 struct inode *inode, loff_t pos, size_t count, int opcode)
269{ 284{
270 struct fuse_file *ff = file->private_data;
271 struct fuse_read_in *inarg = &req->misc.read_in; 285 struct fuse_read_in *inarg = &req->misc.read_in;
272 286
273 inarg->fh = ff->fh; 287 inarg->fh = ff->fh;
@@ -288,7 +302,8 @@ static size_t fuse_send_read(struct fuse_req *req, struct file *file,
288 struct inode *inode, loff_t pos, size_t count) 302 struct inode *inode, loff_t pos, size_t count)
289{ 303{
290 struct fuse_conn *fc = get_fuse_conn(inode); 304 struct fuse_conn *fc = get_fuse_conn(inode);
291 fuse_read_fill(req, file, inode, pos, count, FUSE_READ); 305 struct fuse_file *ff = file->private_data;
306 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
292 request_send(fc, req); 307 request_send(fc, req);
293 return req->out.args[0].size; 308 return req->out.args[0].size;
294} 309}
@@ -337,20 +352,21 @@ static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
337 SetPageError(page); 352 SetPageError(page);
338 unlock_page(page); 353 unlock_page(page);
339 } 354 }
355 if (req->ff)
356 fuse_file_put(req->ff);
340 fuse_put_request(fc, req); 357 fuse_put_request(fc, req);
341} 358}
342 359
343static void fuse_send_readpages(struct fuse_req *req, struct file *file, 360static void fuse_send_readpages(struct fuse_req *req, struct fuse_file *ff,
344 struct inode *inode) 361 struct inode *inode)
345{ 362{
346 struct fuse_conn *fc = get_fuse_conn(inode); 363 struct fuse_conn *fc = get_fuse_conn(inode);
347 loff_t pos = page_offset(req->pages[0]); 364 loff_t pos = page_offset(req->pages[0]);
348 size_t count = req->num_pages << PAGE_CACHE_SHIFT; 365 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
349 req->out.page_zeroing = 1; 366 req->out.page_zeroing = 1;
350 fuse_read_fill(req, file, inode, pos, count, FUSE_READ); 367 fuse_read_fill(req, ff, inode, pos, count, FUSE_READ);
351 if (fc->async_read) { 368 if (fc->async_read) {
352 get_file(file); 369 req->ff = fuse_file_get(ff);
353 req->file = file;
354 req->end = fuse_readpages_end; 370 req->end = fuse_readpages_end;
355 request_send_background(fc, req); 371 request_send_background(fc, req);
356 } else { 372 } else {
@@ -359,15 +375,15 @@ static void fuse_send_readpages(struct fuse_req *req, struct file *file,
359 } 375 }
360} 376}
361 377
362struct fuse_readpages_data { 378struct fuse_fill_data {
363 struct fuse_req *req; 379 struct fuse_req *req;
364 struct file *file; 380 struct fuse_file *ff;
365 struct inode *inode; 381 struct inode *inode;
366}; 382};
367 383
368static int fuse_readpages_fill(void *_data, struct page *page) 384static int fuse_readpages_fill(void *_data, struct page *page)
369{ 385{
370 struct fuse_readpages_data *data = _data; 386 struct fuse_fill_data *data = _data;
371 struct fuse_req *req = data->req; 387 struct fuse_req *req = data->req;
372 struct inode *inode = data->inode; 388 struct inode *inode = data->inode;
373 struct fuse_conn *fc = get_fuse_conn(inode); 389 struct fuse_conn *fc = get_fuse_conn(inode);
@@ -376,7 +392,7 @@ static int fuse_readpages_fill(void *_data, struct page *page)
376 (req->num_pages == FUSE_MAX_PAGES_PER_REQ || 392 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
377 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || 393 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
378 req->pages[req->num_pages - 1]->index + 1 != page->index)) { 394 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
379 fuse_send_readpages(req, data->file, inode); 395 fuse_send_readpages(req, data->ff, inode);
380 data->req = req = fuse_get_req(fc); 396 data->req = req = fuse_get_req(fc);
381 if (IS_ERR(req)) { 397 if (IS_ERR(req)) {
382 unlock_page(page); 398 unlock_page(page);
@@ -393,14 +409,14 @@ static int fuse_readpages(struct file *file, struct address_space *mapping,
393{ 409{
394 struct inode *inode = mapping->host; 410 struct inode *inode = mapping->host;
395 struct fuse_conn *fc = get_fuse_conn(inode); 411 struct fuse_conn *fc = get_fuse_conn(inode);
396 struct fuse_readpages_data data; 412 struct fuse_fill_data data;
397 int err; 413 int err;
398 414
399 err = -EIO; 415 err = -EIO;
400 if (is_bad_inode(inode)) 416 if (is_bad_inode(inode))
401 goto out; 417 goto out;
402 418
403 data.file = file; 419 data.ff = file->private_data;
404 data.inode = inode; 420 data.inode = inode;
405 data.req = fuse_get_req(fc); 421 data.req = fuse_get_req(fc);
406 err = PTR_ERR(data.req); 422 err = PTR_ERR(data.req);
@@ -410,7 +426,7 @@ static int fuse_readpages(struct file *file, struct address_space *mapping,
410 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); 426 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
411 if (!err) { 427 if (!err) {
412 if (data.req->num_pages) 428 if (data.req->num_pages)
413 fuse_send_readpages(data.req, file, inode); 429 fuse_send_readpages(data.req, data.ff, inode);
414 else 430 else
415 fuse_put_request(fc, data.req); 431 fuse_put_request(fc, data.req);
416 } 432 }
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index 95bcb433d1b4..60683b787250 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -72,6 +72,9 @@ struct fuse_file {
72 72
73 /** File handle used by userspace */ 73 /** File handle used by userspace */
74 u64 fh; 74 u64 fh;
75
76 /** Refcount */
77 atomic_t count;
75}; 78};
76 79
77/** One input argument of a request */ 80/** One input argument of a request */
@@ -216,7 +219,7 @@ struct fuse_req {
216 unsigned page_offset; 219 unsigned page_offset;
217 220
218 /** File used in the request (or NULL) */ 221 /** File used in the request (or NULL) */
219 struct file *file; 222 struct fuse_file *ff;
220 223
221 /** vfsmount used in release */ 224 /** vfsmount used in release */
222 struct vfsmount *vfsmount; 225 struct vfsmount *vfsmount;
@@ -420,7 +423,7 @@ void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
420/** 423/**
421 * Initialize READ or READDIR request 424 * Initialize READ or READDIR request
422 */ 425 */
423void fuse_read_fill(struct fuse_req *req, struct file *file, 426void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
424 struct inode *inode, loff_t pos, size_t count, int opcode); 427 struct inode *inode, loff_t pos, size_t count, int opcode);
425 428
426/** 429/**
@@ -433,9 +436,9 @@ void fuse_file_free(struct fuse_file *ff);
433void fuse_finish_open(struct inode *inode, struct file *file, 436void fuse_finish_open(struct inode *inode, struct file *file,
434 struct fuse_file *ff, struct fuse_open_out *outarg); 437 struct fuse_file *ff, struct fuse_open_out *outarg);
435 438
436/** */ 439/** Fill in ff->reserved_req with a RELEASE request */
437struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, 440void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode);
438 int opcode); 441
439/** 442/**
440 * Send RELEASE or RELEASEDIR request 443 * Send RELEASE or RELEASEDIR request
441 */ 444 */