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