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