Commit | Line | Data |
---|---|---|
b6aeaded MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
56cf34ff | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
b6aeaded MS |
4 | |
5 | This program can be distributed under the terms of the GNU GPL. | |
6 | See the file COPYING. | |
7 | */ | |
8 | ||
9 | #include "fuse_i.h" | |
10 | ||
11 | #include <linux/pagemap.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/kernel.h> | |
14 | ||
4b6f5d20 | 15 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 16 | |
fd72faac MS |
17 | static int fuse_send_open(struct inode *inode, struct file *file, int isdir, |
18 | struct fuse_open_out *outargp) | |
b6aeaded MS |
19 | { |
20 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b6aeaded | 21 | struct fuse_open_in inarg; |
fd72faac MS |
22 | struct fuse_req *req; |
23 | int err; | |
24 | ||
ce1d5a49 MS |
25 | req = fuse_get_req(fc); |
26 | if (IS_ERR(req)) | |
27 | return PTR_ERR(req); | |
fd72faac MS |
28 | |
29 | memset(&inarg, 0, sizeof(inarg)); | |
30 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); | |
31 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; | |
32 | req->in.h.nodeid = get_node_id(inode); | |
fd72faac MS |
33 | req->in.numargs = 1; |
34 | req->in.args[0].size = sizeof(inarg); | |
35 | req->in.args[0].value = &inarg; | |
36 | req->out.numargs = 1; | |
37 | req->out.args[0].size = sizeof(*outargp); | |
38 | req->out.args[0].value = outargp; | |
39 | request_send(fc, req); | |
40 | err = req->out.h.error; | |
41 | fuse_put_request(fc, req); | |
42 | ||
43 | return err; | |
44 | } | |
45 | ||
46 | struct fuse_file *fuse_file_alloc(void) | |
47 | { | |
48 | struct fuse_file *ff; | |
49 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); | |
50 | if (ff) { | |
51 | ff->release_req = fuse_request_alloc(); | |
52 | if (!ff->release_req) { | |
53 | kfree(ff); | |
54 | ff = NULL; | |
55 | } | |
56 | } | |
57 | return ff; | |
58 | } | |
59 | ||
60 | void fuse_file_free(struct fuse_file *ff) | |
61 | { | |
62 | fuse_request_free(ff->release_req); | |
63 | kfree(ff); | |
64 | } | |
65 | ||
66 | void fuse_finish_open(struct inode *inode, struct file *file, | |
67 | struct fuse_file *ff, struct fuse_open_out *outarg) | |
68 | { | |
69 | if (outarg->open_flags & FOPEN_DIRECT_IO) | |
70 | file->f_op = &fuse_direct_io_file_operations; | |
71 | if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) | |
72 | invalidate_inode_pages(inode->i_mapping); | |
73 | ff->fh = outarg->fh; | |
74 | file->private_data = ff; | |
75 | } | |
76 | ||
77 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) | |
78 | { | |
b6aeaded MS |
79 | struct fuse_open_out outarg; |
80 | struct fuse_file *ff; | |
81 | int err; | |
b6aeaded | 82 | |
dd190d06 MS |
83 | /* VFS checks this, but only _after_ ->open() */ |
84 | if (file->f_flags & O_DIRECT) | |
85 | return -EINVAL; | |
86 | ||
b6aeaded MS |
87 | err = generic_file_open(inode, file); |
88 | if (err) | |
89 | return err; | |
90 | ||
91 | /* If opening the root node, no lookup has been performed on | |
92 | it, so the attributes must be refreshed */ | |
93 | if (get_node_id(inode) == FUSE_ROOT_ID) { | |
fd72faac | 94 | err = fuse_do_getattr(inode); |
b6aeaded MS |
95 | if (err) |
96 | return err; | |
97 | } | |
98 | ||
fd72faac | 99 | ff = fuse_file_alloc(); |
b6aeaded | 100 | if (!ff) |
fd72faac | 101 | return -ENOMEM; |
b6aeaded | 102 | |
fd72faac MS |
103 | err = fuse_send_open(inode, file, isdir, &outarg); |
104 | if (err) | |
105 | fuse_file_free(ff); | |
106 | else { | |
107 | if (isdir) | |
108 | outarg.open_flags &= ~FOPEN_DIRECT_IO; | |
109 | fuse_finish_open(inode, file, ff, &outarg); | |
b6aeaded MS |
110 | } |
111 | ||
b6aeaded MS |
112 | return err; |
113 | } | |
114 | ||
51eb01e7 MS |
115 | struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, |
116 | int opcode) | |
64c6d8ed | 117 | { |
51eb01e7 | 118 | struct fuse_req *req = ff->release_req; |
b6aeaded MS |
119 | struct fuse_release_in *inarg = &req->misc.release_in; |
120 | ||
121 | inarg->fh = ff->fh; | |
fd72faac | 122 | inarg->flags = flags; |
51eb01e7 | 123 | req->in.h.opcode = opcode; |
fd72faac | 124 | req->in.h.nodeid = nodeid; |
b6aeaded MS |
125 | req->in.numargs = 1; |
126 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
127 | req->in.args[0].value = inarg; | |
b6aeaded | 128 | kfree(ff); |
51eb01e7 MS |
129 | |
130 | return req; | |
fd72faac MS |
131 | } |
132 | ||
133 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) | |
134 | { | |
135 | struct fuse_file *ff = file->private_data; | |
136 | if (ff) { | |
137 | struct fuse_conn *fc = get_fuse_conn(inode); | |
51eb01e7 MS |
138 | struct fuse_req *req; |
139 | ||
140 | req = fuse_release_fill(ff, get_node_id(inode), file->f_flags, | |
141 | isdir ? FUSE_RELEASEDIR : FUSE_RELEASE); | |
142 | ||
143 | /* Hold vfsmount and dentry until release is finished */ | |
144 | req->vfsmount = mntget(file->f_vfsmnt); | |
145 | req->dentry = dget(file->f_dentry); | |
146 | request_send_background(fc, req); | |
fd72faac | 147 | } |
b6aeaded MS |
148 | |
149 | /* Return value is ignored by VFS */ | |
150 | return 0; | |
151 | } | |
152 | ||
04730fef MS |
153 | static int fuse_open(struct inode *inode, struct file *file) |
154 | { | |
155 | return fuse_open_common(inode, file, 0); | |
156 | } | |
157 | ||
158 | static int fuse_release(struct inode *inode, struct file *file) | |
159 | { | |
160 | return fuse_release_common(inode, file, 0); | |
161 | } | |
162 | ||
75e1fcc0 | 163 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded MS |
164 | { |
165 | struct inode *inode = file->f_dentry->d_inode; | |
166 | struct fuse_conn *fc = get_fuse_conn(inode); | |
167 | struct fuse_file *ff = file->private_data; | |
168 | struct fuse_req *req; | |
169 | struct fuse_flush_in inarg; | |
170 | int err; | |
171 | ||
248d86e8 MS |
172 | if (is_bad_inode(inode)) |
173 | return -EIO; | |
174 | ||
b6aeaded MS |
175 | if (fc->no_flush) |
176 | return 0; | |
177 | ||
ce1d5a49 MS |
178 | req = fuse_get_req(fc); |
179 | if (IS_ERR(req)) | |
180 | return PTR_ERR(req); | |
b6aeaded MS |
181 | |
182 | memset(&inarg, 0, sizeof(inarg)); | |
183 | inarg.fh = ff->fh; | |
184 | req->in.h.opcode = FUSE_FLUSH; | |
185 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
186 | req->in.numargs = 1; |
187 | req->in.args[0].size = sizeof(inarg); | |
188 | req->in.args[0].value = &inarg; | |
7c352bdf | 189 | request_send(fc, req); |
b6aeaded MS |
190 | err = req->out.h.error; |
191 | fuse_put_request(fc, req); | |
192 | if (err == -ENOSYS) { | |
193 | fc->no_flush = 1; | |
194 | err = 0; | |
195 | } | |
196 | return err; | |
197 | } | |
198 | ||
82547981 MS |
199 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
200 | int isdir) | |
b6aeaded MS |
201 | { |
202 | struct inode *inode = de->d_inode; | |
203 | struct fuse_conn *fc = get_fuse_conn(inode); | |
204 | struct fuse_file *ff = file->private_data; | |
205 | struct fuse_req *req; | |
206 | struct fuse_fsync_in inarg; | |
207 | int err; | |
208 | ||
248d86e8 MS |
209 | if (is_bad_inode(inode)) |
210 | return -EIO; | |
211 | ||
82547981 | 212 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
213 | return 0; |
214 | ||
ce1d5a49 MS |
215 | req = fuse_get_req(fc); |
216 | if (IS_ERR(req)) | |
217 | return PTR_ERR(req); | |
b6aeaded MS |
218 | |
219 | memset(&inarg, 0, sizeof(inarg)); | |
220 | inarg.fh = ff->fh; | |
221 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 222 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 223 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
224 | req->in.numargs = 1; |
225 | req->in.args[0].size = sizeof(inarg); | |
226 | req->in.args[0].value = &inarg; | |
227 | request_send(fc, req); | |
228 | err = req->out.h.error; | |
229 | fuse_put_request(fc, req); | |
230 | if (err == -ENOSYS) { | |
82547981 MS |
231 | if (isdir) |
232 | fc->no_fsyncdir = 1; | |
233 | else | |
234 | fc->no_fsync = 1; | |
b6aeaded MS |
235 | err = 0; |
236 | } | |
237 | return err; | |
238 | } | |
239 | ||
82547981 MS |
240 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
241 | { | |
242 | return fuse_fsync_common(file, de, datasync, 0); | |
243 | } | |
244 | ||
361b1eb5 MS |
245 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
246 | struct inode *inode, loff_t pos, size_t count, int opcode) | |
b6aeaded | 247 | { |
b6aeaded | 248 | struct fuse_file *ff = file->private_data; |
361b1eb5 | 249 | struct fuse_read_in *inarg = &req->misc.read_in; |
b6aeaded | 250 | |
361b1eb5 MS |
251 | inarg->fh = ff->fh; |
252 | inarg->offset = pos; | |
253 | inarg->size = count; | |
254 | req->in.h.opcode = opcode; | |
b6aeaded | 255 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
256 | req->in.numargs = 1; |
257 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 258 | req->in.args[0].value = inarg; |
b6aeaded MS |
259 | req->out.argpages = 1; |
260 | req->out.argvar = 1; | |
261 | req->out.numargs = 1; | |
262 | req->out.args[0].size = count; | |
b6aeaded MS |
263 | } |
264 | ||
8bfc016d MS |
265 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
266 | struct inode *inode, loff_t pos, size_t count) | |
04730fef | 267 | { |
361b1eb5 MS |
268 | struct fuse_conn *fc = get_fuse_conn(inode); |
269 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); | |
270 | request_send(fc, req); | |
271 | return req->out.args[0].size; | |
04730fef MS |
272 | } |
273 | ||
b6aeaded MS |
274 | static int fuse_readpage(struct file *file, struct page *page) |
275 | { | |
276 | struct inode *inode = page->mapping->host; | |
277 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 MS |
278 | struct fuse_req *req; |
279 | int err; | |
280 | ||
281 | err = -EIO; | |
282 | if (is_bad_inode(inode)) | |
283 | goto out; | |
284 | ||
ce1d5a49 MS |
285 | req = fuse_get_req(fc); |
286 | err = PTR_ERR(req); | |
287 | if (IS_ERR(req)) | |
b6aeaded MS |
288 | goto out; |
289 | ||
290 | req->out.page_zeroing = 1; | |
291 | req->num_pages = 1; | |
292 | req->pages[0] = page; | |
4633a22e | 293 | fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE); |
b6aeaded MS |
294 | err = req->out.h.error; |
295 | fuse_put_request(fc, req); | |
296 | if (!err) | |
297 | SetPageUptodate(page); | |
b36c31ba | 298 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
299 | out: |
300 | unlock_page(page); | |
301 | return err; | |
302 | } | |
303 | ||
c1aa96a5 | 304 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 305 | { |
c1aa96a5 MS |
306 | int i; |
307 | ||
308 | fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */ | |
309 | ||
db50b96c MS |
310 | for (i = 0; i < req->num_pages; i++) { |
311 | struct page *page = req->pages[i]; | |
312 | if (!req->out.h.error) | |
313 | SetPageUptodate(page); | |
c1aa96a5 MS |
314 | else |
315 | SetPageError(page); | |
db50b96c MS |
316 | unlock_page(page); |
317 | } | |
c1aa96a5 MS |
318 | fuse_put_request(fc, req); |
319 | } | |
320 | ||
321 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, | |
322 | struct inode *inode) | |
323 | { | |
324 | struct fuse_conn *fc = get_fuse_conn(inode); | |
325 | loff_t pos = page_offset(req->pages[0]); | |
326 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
327 | req->out.page_zeroing = 1; | |
c1aa96a5 | 328 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
9cd68455 | 329 | if (fc->async_read) { |
51eb01e7 MS |
330 | get_file(file); |
331 | req->file = file; | |
9cd68455 MS |
332 | req->end = fuse_readpages_end; |
333 | request_send_background(fc, req); | |
334 | } else { | |
335 | request_send(fc, req); | |
336 | fuse_readpages_end(fc, req); | |
337 | } | |
db50b96c MS |
338 | } |
339 | ||
340 | struct fuse_readpages_data { | |
341 | struct fuse_req *req; | |
342 | struct file *file; | |
343 | struct inode *inode; | |
344 | }; | |
345 | ||
346 | static int fuse_readpages_fill(void *_data, struct page *page) | |
347 | { | |
348 | struct fuse_readpages_data *data = _data; | |
349 | struct fuse_req *req = data->req; | |
350 | struct inode *inode = data->inode; | |
351 | struct fuse_conn *fc = get_fuse_conn(inode); | |
352 | ||
353 | if (req->num_pages && | |
354 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
355 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
356 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
c1aa96a5 | 357 | fuse_send_readpages(req, data->file, inode); |
ce1d5a49 MS |
358 | data->req = req = fuse_get_req(fc); |
359 | if (IS_ERR(req)) { | |
db50b96c | 360 | unlock_page(page); |
ce1d5a49 | 361 | return PTR_ERR(req); |
db50b96c | 362 | } |
db50b96c MS |
363 | } |
364 | req->pages[req->num_pages] = page; | |
365 | req->num_pages ++; | |
366 | return 0; | |
367 | } | |
368 | ||
369 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
370 | struct list_head *pages, unsigned nr_pages) | |
371 | { | |
372 | struct inode *inode = mapping->host; | |
373 | struct fuse_conn *fc = get_fuse_conn(inode); | |
374 | struct fuse_readpages_data data; | |
375 | int err; | |
248d86e8 MS |
376 | |
377 | if (is_bad_inode(inode)) | |
378 | return -EIO; | |
379 | ||
db50b96c MS |
380 | data.file = file; |
381 | data.inode = inode; | |
ce1d5a49 MS |
382 | data.req = fuse_get_req(fc); |
383 | if (IS_ERR(data.req)) | |
384 | return PTR_ERR(data.req); | |
db50b96c MS |
385 | |
386 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
387 | if (!err) { |
388 | if (data.req->num_pages) | |
389 | fuse_send_readpages(data.req, file, inode); | |
390 | else | |
391 | fuse_put_request(fc, data.req); | |
392 | } | |
db50b96c MS |
393 | return err; |
394 | } | |
395 | ||
04730fef MS |
396 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, |
397 | struct inode *inode, loff_t pos, size_t count) | |
b6aeaded MS |
398 | { |
399 | struct fuse_conn *fc = get_fuse_conn(inode); | |
400 | struct fuse_file *ff = file->private_data; | |
401 | struct fuse_write_in inarg; | |
402 | struct fuse_write_out outarg; | |
403 | ||
404 | memset(&inarg, 0, sizeof(struct fuse_write_in)); | |
405 | inarg.fh = ff->fh; | |
406 | inarg.offset = pos; | |
407 | inarg.size = count; | |
408 | req->in.h.opcode = FUSE_WRITE; | |
409 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
410 | req->in.argpages = 1; |
411 | req->in.numargs = 2; | |
412 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
413 | req->in.args[0].value = &inarg; | |
414 | req->in.args[1].size = count; | |
415 | req->out.numargs = 1; | |
416 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
417 | req->out.args[0].value = &outarg; | |
7c352bdf | 418 | request_send(fc, req); |
b6aeaded MS |
419 | return outarg.size; |
420 | } | |
421 | ||
422 | static int fuse_prepare_write(struct file *file, struct page *page, | |
423 | unsigned offset, unsigned to) | |
424 | { | |
425 | /* No op */ | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static int fuse_commit_write(struct file *file, struct page *page, | |
430 | unsigned offset, unsigned to) | |
431 | { | |
432 | int err; | |
04730fef | 433 | size_t nres; |
b6aeaded MS |
434 | unsigned count = to - offset; |
435 | struct inode *inode = page->mapping->host; | |
436 | struct fuse_conn *fc = get_fuse_conn(inode); | |
4633a22e | 437 | loff_t pos = page_offset(page) + offset; |
248d86e8 MS |
438 | struct fuse_req *req; |
439 | ||
440 | if (is_bad_inode(inode)) | |
441 | return -EIO; | |
442 | ||
ce1d5a49 MS |
443 | req = fuse_get_req(fc); |
444 | if (IS_ERR(req)) | |
445 | return PTR_ERR(req); | |
b6aeaded MS |
446 | |
447 | req->num_pages = 1; | |
448 | req->pages[0] = page; | |
449 | req->page_offset = offset; | |
450 | nres = fuse_send_write(req, file, inode, pos, count); | |
451 | err = req->out.h.error; | |
452 | fuse_put_request(fc, req); | |
453 | if (!err && nres != count) | |
454 | err = -EIO; | |
455 | if (!err) { | |
456 | pos += count; | |
457 | if (pos > i_size_read(inode)) | |
458 | i_size_write(inode, pos); | |
459 | ||
460 | if (offset == 0 && to == PAGE_CACHE_SIZE) { | |
461 | clear_page_dirty(page); | |
462 | SetPageUptodate(page); | |
463 | } | |
b36c31ba MS |
464 | } |
465 | fuse_invalidate_attr(inode); | |
b6aeaded MS |
466 | return err; |
467 | } | |
468 | ||
413ef8cb MS |
469 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
470 | { | |
471 | unsigned i; | |
472 | ||
473 | for (i = 0; i < req->num_pages; i++) { | |
474 | struct page *page = req->pages[i]; | |
475 | if (write) | |
476 | set_page_dirty_lock(page); | |
477 | put_page(page); | |
478 | } | |
479 | } | |
480 | ||
481 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | |
482 | unsigned nbytes, int write) | |
483 | { | |
484 | unsigned long user_addr = (unsigned long) buf; | |
485 | unsigned offset = user_addr & ~PAGE_MASK; | |
486 | int npages; | |
487 | ||
488 | /* This doesn't work with nfsd */ | |
489 | if (!current->mm) | |
490 | return -EPERM; | |
491 | ||
492 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | |
493 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
6ad84aca | 494 | npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ); |
413ef8cb MS |
495 | down_read(¤t->mm->mmap_sem); |
496 | npages = get_user_pages(current, current->mm, user_addr, npages, write, | |
497 | 0, req->pages, NULL); | |
498 | up_read(¤t->mm->mmap_sem); | |
499 | if (npages < 0) | |
500 | return npages; | |
501 | ||
502 | req->num_pages = npages; | |
503 | req->page_offset = offset; | |
504 | return 0; | |
505 | } | |
506 | ||
507 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |
508 | size_t count, loff_t *ppos, int write) | |
509 | { | |
510 | struct inode *inode = file->f_dentry->d_inode; | |
511 | struct fuse_conn *fc = get_fuse_conn(inode); | |
512 | size_t nmax = write ? fc->max_write : fc->max_read; | |
513 | loff_t pos = *ppos; | |
514 | ssize_t res = 0; | |
248d86e8 MS |
515 | struct fuse_req *req; |
516 | ||
517 | if (is_bad_inode(inode)) | |
518 | return -EIO; | |
519 | ||
ce1d5a49 MS |
520 | req = fuse_get_req(fc); |
521 | if (IS_ERR(req)) | |
522 | return PTR_ERR(req); | |
413ef8cb MS |
523 | |
524 | while (count) { | |
413ef8cb MS |
525 | size_t nres; |
526 | size_t nbytes = min(count, nmax); | |
527 | int err = fuse_get_user_pages(req, buf, nbytes, !write); | |
528 | if (err) { | |
529 | res = err; | |
530 | break; | |
531 | } | |
6ad84aca MS |
532 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
533 | nbytes = min(count, nbytes); | |
413ef8cb MS |
534 | if (write) |
535 | nres = fuse_send_write(req, file, inode, pos, nbytes); | |
536 | else | |
537 | nres = fuse_send_read(req, file, inode, pos, nbytes); | |
538 | fuse_release_user_pages(req, !write); | |
539 | if (req->out.h.error) { | |
540 | if (!res) | |
541 | res = req->out.h.error; | |
542 | break; | |
543 | } else if (nres > nbytes) { | |
544 | res = -EIO; | |
545 | break; | |
546 | } | |
547 | count -= nres; | |
548 | res += nres; | |
549 | pos += nres; | |
550 | buf += nres; | |
551 | if (nres != nbytes) | |
552 | break; | |
56cf34ff MS |
553 | if (count) { |
554 | fuse_put_request(fc, req); | |
555 | req = fuse_get_req(fc); | |
556 | if (IS_ERR(req)) | |
557 | break; | |
558 | } | |
413ef8cb MS |
559 | } |
560 | fuse_put_request(fc, req); | |
561 | if (res > 0) { | |
562 | if (write && pos > i_size_read(inode)) | |
563 | i_size_write(inode, pos); | |
564 | *ppos = pos; | |
b36c31ba MS |
565 | } |
566 | fuse_invalidate_attr(inode); | |
413ef8cb MS |
567 | |
568 | return res; | |
569 | } | |
570 | ||
571 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, | |
572 | size_t count, loff_t *ppos) | |
573 | { | |
574 | return fuse_direct_io(file, buf, count, ppos, 0); | |
575 | } | |
576 | ||
577 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | |
578 | size_t count, loff_t *ppos) | |
579 | { | |
580 | struct inode *inode = file->f_dentry->d_inode; | |
581 | ssize_t res; | |
582 | /* Don't allow parallel writes to the same file */ | |
1b1dcc1b | 583 | mutex_lock(&inode->i_mutex); |
413ef8cb | 584 | res = fuse_direct_io(file, buf, count, ppos, 1); |
1b1dcc1b | 585 | mutex_unlock(&inode->i_mutex); |
413ef8cb MS |
586 | return res; |
587 | } | |
588 | ||
b6aeaded MS |
589 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
590 | { | |
591 | if ((vma->vm_flags & VM_SHARED)) { | |
592 | if ((vma->vm_flags & VM_WRITE)) | |
593 | return -ENODEV; | |
594 | else | |
595 | vma->vm_flags &= ~VM_MAYWRITE; | |
596 | } | |
597 | return generic_file_mmap(file, vma); | |
598 | } | |
599 | ||
600 | static int fuse_set_page_dirty(struct page *page) | |
601 | { | |
602 | printk("fuse_set_page_dirty: should not happen\n"); | |
603 | dump_stack(); | |
604 | return 0; | |
605 | } | |
606 | ||
4b6f5d20 | 607 | static const struct file_operations fuse_file_operations = { |
b6aeaded MS |
608 | .llseek = generic_file_llseek, |
609 | .read = generic_file_read, | |
610 | .write = generic_file_write, | |
611 | .mmap = fuse_file_mmap, | |
612 | .open = fuse_open, | |
613 | .flush = fuse_flush, | |
614 | .release = fuse_release, | |
615 | .fsync = fuse_fsync, | |
616 | .sendfile = generic_file_sendfile, | |
617 | }; | |
618 | ||
4b6f5d20 | 619 | static const struct file_operations fuse_direct_io_file_operations = { |
413ef8cb MS |
620 | .llseek = generic_file_llseek, |
621 | .read = fuse_direct_read, | |
622 | .write = fuse_direct_write, | |
623 | .open = fuse_open, | |
624 | .flush = fuse_flush, | |
625 | .release = fuse_release, | |
626 | .fsync = fuse_fsync, | |
627 | /* no mmap and sendfile */ | |
628 | }; | |
629 | ||
b6aeaded MS |
630 | static struct address_space_operations fuse_file_aops = { |
631 | .readpage = fuse_readpage, | |
632 | .prepare_write = fuse_prepare_write, | |
633 | .commit_write = fuse_commit_write, | |
db50b96c | 634 | .readpages = fuse_readpages, |
b6aeaded MS |
635 | .set_page_dirty = fuse_set_page_dirty, |
636 | }; | |
637 | ||
638 | void fuse_init_file_inode(struct inode *inode) | |
639 | { | |
45323fb7 MS |
640 | inode->i_fop = &fuse_file_operations; |
641 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 642 | } |