diff options
author | Miklos Szeredi <mszeredi@suse.cz> | 2007-10-17 02:31:00 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-10-17 11:43:03 -0400 |
commit | c756e0a4d79202535774806f148026e40466a5eb (patch) | |
tree | aa769ecfe2204e2e1969108d2c391b88b95e983b /fs/fuse/file.c | |
parent | de5e3dec421c44c999071b8f7e0580ad2ade92ae (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>
Diffstat (limited to 'fs/fuse/file.c')
-rw-r--r-- | fs/fuse/file.c | 72 |
1 files changed, 44 insertions, 28 deletions
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 | ||
68 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) | ||
69 | { | ||
70 | atomic_inc(&ff->count); | ||
71 | return ff; | ||
72 | } | ||
73 | |||
74 | static 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 | |||
67 | void fuse_finish_open(struct inode *inode, struct file *file, | 84 | void 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 | ||
78 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) | 95 | int 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 | ||
116 | struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, | 133 | void 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 | ||
134 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) | 147 | int 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 | ||
267 | void fuse_read_fill(struct fuse_req *req, struct file *file, | 282 | void 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 | ||
343 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, | 360 | static 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 | ||
362 | struct fuse_readpages_data { | 378 | struct 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 | ||
368 | static int fuse_readpages_fill(void *_data, struct page *page) | 384 | static 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 | } |