Commit | Line | Data |
---|---|---|
b6aeaded MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
1729a16c | 3 | Copyright (C) 2001-2008 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> | |
e8edc6e0 | 14 | #include <linux/sched.h> |
b6aeaded | 15 | |
4b6f5d20 | 16 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 17 | |
91fe96b4 MS |
18 | static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
19 | int opcode, struct fuse_open_out *outargp) | |
b6aeaded | 20 | { |
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)); | |
6ff958ed MS |
30 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
31 | if (!fc->atomic_o_trunc) | |
32 | inarg.flags &= ~O_TRUNC; | |
91fe96b4 MS |
33 | req->in.h.opcode = opcode; |
34 | req->in.h.nodeid = nodeid; | |
fd72faac MS |
35 | req->in.numargs = 1; |
36 | req->in.args[0].size = sizeof(inarg); | |
37 | req->in.args[0].value = &inarg; | |
38 | req->out.numargs = 1; | |
39 | req->out.args[0].size = sizeof(*outargp); | |
40 | req->out.args[0].value = outargp; | |
b93f858a | 41 | fuse_request_send(fc, req); |
fd72faac MS |
42 | err = req->out.h.error; |
43 | fuse_put_request(fc, req); | |
44 | ||
45 | return err; | |
46 | } | |
47 | ||
acf99433 | 48 | struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) |
fd72faac MS |
49 | { |
50 | struct fuse_file *ff; | |
6b2db28a | 51 | |
fd72faac | 52 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); |
6b2db28a TH |
53 | if (unlikely(!ff)) |
54 | return NULL; | |
55 | ||
da5e4714 | 56 | ff->fc = fc; |
6b2db28a TH |
57 | ff->reserved_req = fuse_request_alloc(); |
58 | if (unlikely(!ff->reserved_req)) { | |
59 | kfree(ff); | |
60 | return NULL; | |
fd72faac | 61 | } |
6b2db28a TH |
62 | |
63 | INIT_LIST_HEAD(&ff->write_entry); | |
64 | atomic_set(&ff->count, 0); | |
65 | RB_CLEAR_NODE(&ff->polled_node); | |
66 | init_waitqueue_head(&ff->poll_wait); | |
67 | ||
68 | spin_lock(&fc->lock); | |
69 | ff->kh = ++fc->khctr; | |
70 | spin_unlock(&fc->lock); | |
71 | ||
fd72faac MS |
72 | return ff; |
73 | } | |
74 | ||
75 | void fuse_file_free(struct fuse_file *ff) | |
76 | { | |
33649c91 | 77 | fuse_request_free(ff->reserved_req); |
fd72faac MS |
78 | kfree(ff); |
79 | } | |
80 | ||
c7b7143c | 81 | struct fuse_file *fuse_file_get(struct fuse_file *ff) |
c756e0a4 MS |
82 | { |
83 | atomic_inc(&ff->count); | |
84 | return ff; | |
85 | } | |
86 | ||
819c4b3b MS |
87 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
88 | { | |
b0be46eb | 89 | path_put(&req->misc.release.path); |
819c4b3b MS |
90 | } |
91 | ||
c756e0a4 MS |
92 | static void fuse_file_put(struct fuse_file *ff) |
93 | { | |
94 | if (atomic_dec_and_test(&ff->count)) { | |
95 | struct fuse_req *req = ff->reserved_req; | |
8b0797a4 | 96 | |
819c4b3b | 97 | req->end = fuse_release_end; |
8b0797a4 | 98 | fuse_request_send_background(ff->fc, req); |
c756e0a4 MS |
99 | kfree(ff); |
100 | } | |
101 | } | |
102 | ||
91fe96b4 MS |
103 | static int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
104 | bool isdir) | |
105 | { | |
106 | struct fuse_open_out outarg; | |
107 | struct fuse_file *ff; | |
108 | int err; | |
109 | int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; | |
110 | ||
111 | ff = fuse_file_alloc(fc); | |
112 | if (!ff) | |
113 | return -ENOMEM; | |
114 | ||
115 | err = fuse_send_open(fc, nodeid, file, opcode, &outarg); | |
116 | if (err) { | |
117 | fuse_file_free(ff); | |
118 | return err; | |
119 | } | |
120 | ||
121 | if (isdir) | |
122 | outarg.open_flags &= ~FOPEN_DIRECT_IO; | |
123 | ||
124 | ff->fh = outarg.fh; | |
125 | ff->nodeid = nodeid; | |
126 | ff->open_flags = outarg.open_flags; | |
127 | file->private_data = fuse_file_get(ff); | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
c7b7143c | 132 | void fuse_finish_open(struct inode *inode, struct file *file) |
fd72faac | 133 | { |
c7b7143c MS |
134 | struct fuse_file *ff = file->private_data; |
135 | ||
136 | if (ff->open_flags & FOPEN_DIRECT_IO) | |
fd72faac | 137 | file->f_op = &fuse_direct_io_file_operations; |
c7b7143c | 138 | if (!(ff->open_flags & FOPEN_KEEP_CACHE)) |
b1009979 | 139 | invalidate_inode_pages2(inode->i_mapping); |
c7b7143c | 140 | if (ff->open_flags & FOPEN_NONSEEKABLE) |
a7c1b990 | 141 | nonseekable_open(inode, file); |
fd72faac MS |
142 | } |
143 | ||
91fe96b4 | 144 | int fuse_open_common(struct inode *inode, struct file *file, bool isdir) |
fd72faac | 145 | { |
acf99433 | 146 | struct fuse_conn *fc = get_fuse_conn(inode); |
b6aeaded | 147 | int err; |
b6aeaded | 148 | |
dd190d06 MS |
149 | /* VFS checks this, but only _after_ ->open() */ |
150 | if (file->f_flags & O_DIRECT) | |
151 | return -EINVAL; | |
152 | ||
b6aeaded MS |
153 | err = generic_file_open(inode, file); |
154 | if (err) | |
155 | return err; | |
156 | ||
91fe96b4 | 157 | err = fuse_do_open(fc, get_node_id(inode), file, isdir); |
fd72faac | 158 | if (err) |
91fe96b4 | 159 | return err; |
b6aeaded | 160 | |
91fe96b4 MS |
161 | fuse_finish_open(inode, file); |
162 | ||
163 | return 0; | |
b6aeaded MS |
164 | } |
165 | ||
8b0797a4 | 166 | static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode) |
64c6d8ed | 167 | { |
8b0797a4 | 168 | struct fuse_conn *fc = ff->fc; |
33649c91 | 169 | struct fuse_req *req = ff->reserved_req; |
b57d4264 | 170 | struct fuse_release_in *inarg = &req->misc.release.in; |
b6aeaded | 171 | |
8b0797a4 MS |
172 | spin_lock(&fc->lock); |
173 | list_del(&ff->write_entry); | |
174 | if (!RB_EMPTY_NODE(&ff->polled_node)) | |
175 | rb_erase(&ff->polled_node, &fc->polled_files); | |
176 | spin_unlock(&fc->lock); | |
177 | ||
178 | wake_up_interruptible_sync(&ff->poll_wait); | |
179 | ||
b6aeaded | 180 | inarg->fh = ff->fh; |
fd72faac | 181 | inarg->flags = flags; |
51eb01e7 | 182 | req->in.h.opcode = opcode; |
c7b7143c | 183 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
184 | req->in.numargs = 1; |
185 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
186 | req->in.args[0].value = inarg; | |
fd72faac MS |
187 | } |
188 | ||
8b0797a4 | 189 | void fuse_release_common(struct file *file, int opcode) |
fd72faac | 190 | { |
6b2db28a TH |
191 | struct fuse_file *ff; |
192 | struct fuse_req *req; | |
b6aeaded | 193 | |
6b2db28a TH |
194 | ff = file->private_data; |
195 | if (unlikely(!ff)) | |
8b0797a4 | 196 | return; |
6b2db28a | 197 | |
6b2db28a | 198 | req = ff->reserved_req; |
8b0797a4 | 199 | fuse_prepare_release(ff, file->f_flags, opcode); |
6b2db28a TH |
200 | |
201 | /* Hold vfsmount and dentry until release is finished */ | |
b0be46eb MS |
202 | path_get(&file->f_path); |
203 | req->misc.release.path = file->f_path; | |
6b2db28a | 204 | |
6b2db28a TH |
205 | /* |
206 | * Normally this will send the RELEASE request, however if | |
207 | * some asynchronous READ or WRITE requests are outstanding, | |
208 | * the sending will be delayed. | |
209 | */ | |
210 | fuse_file_put(ff); | |
b6aeaded MS |
211 | } |
212 | ||
04730fef MS |
213 | static int fuse_open(struct inode *inode, struct file *file) |
214 | { | |
91fe96b4 | 215 | return fuse_open_common(inode, file, false); |
04730fef MS |
216 | } |
217 | ||
218 | static int fuse_release(struct inode *inode, struct file *file) | |
219 | { | |
8b0797a4 MS |
220 | fuse_release_common(file, FUSE_RELEASE); |
221 | ||
222 | /* return value is ignored by VFS */ | |
223 | return 0; | |
224 | } | |
225 | ||
226 | void fuse_sync_release(struct fuse_file *ff, int flags) | |
227 | { | |
228 | WARN_ON(atomic_read(&ff->count) > 1); | |
229 | fuse_prepare_release(ff, flags, FUSE_RELEASE); | |
230 | ff->reserved_req->force = 1; | |
231 | fuse_request_send(ff->fc, ff->reserved_req); | |
232 | fuse_put_request(ff->fc, ff->reserved_req); | |
233 | kfree(ff); | |
04730fef MS |
234 | } |
235 | ||
71421259 | 236 | /* |
9c8ef561 MS |
237 | * Scramble the ID space with XTEA, so that the value of the files_struct |
238 | * pointer is not exposed to userspace. | |
71421259 | 239 | */ |
f3332114 | 240 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 241 | { |
9c8ef561 MS |
242 | u32 *k = fc->scramble_key; |
243 | u64 v = (unsigned long) id; | |
244 | u32 v0 = v; | |
245 | u32 v1 = v >> 32; | |
246 | u32 sum = 0; | |
247 | int i; | |
248 | ||
249 | for (i = 0; i < 32; i++) { | |
250 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
251 | sum += 0x9E3779B9; | |
252 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
253 | } | |
254 | ||
255 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
256 | } |
257 | ||
3be5a52b MS |
258 | /* |
259 | * Check if page is under writeback | |
260 | * | |
261 | * This is currently done by walking the list of writepage requests | |
262 | * for the inode, which can be pretty inefficient. | |
263 | */ | |
264 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) | |
265 | { | |
266 | struct fuse_conn *fc = get_fuse_conn(inode); | |
267 | struct fuse_inode *fi = get_fuse_inode(inode); | |
268 | struct fuse_req *req; | |
269 | bool found = false; | |
270 | ||
271 | spin_lock(&fc->lock); | |
272 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | |
273 | pgoff_t curr_index; | |
274 | ||
275 | BUG_ON(req->inode != inode); | |
276 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; | |
277 | if (curr_index == index) { | |
278 | found = true; | |
279 | break; | |
280 | } | |
281 | } | |
282 | spin_unlock(&fc->lock); | |
283 | ||
284 | return found; | |
285 | } | |
286 | ||
287 | /* | |
288 | * Wait for page writeback to be completed. | |
289 | * | |
290 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
291 | * use some other means. | |
292 | */ | |
293 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) | |
294 | { | |
295 | struct fuse_inode *fi = get_fuse_inode(inode); | |
296 | ||
297 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
298 | return 0; | |
299 | } | |
300 | ||
75e1fcc0 | 301 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 302 | { |
7706a9d6 | 303 | struct inode *inode = file->f_path.dentry->d_inode; |
b6aeaded MS |
304 | struct fuse_conn *fc = get_fuse_conn(inode); |
305 | struct fuse_file *ff = file->private_data; | |
306 | struct fuse_req *req; | |
307 | struct fuse_flush_in inarg; | |
308 | int err; | |
309 | ||
248d86e8 MS |
310 | if (is_bad_inode(inode)) |
311 | return -EIO; | |
312 | ||
b6aeaded MS |
313 | if (fc->no_flush) |
314 | return 0; | |
315 | ||
33649c91 | 316 | req = fuse_get_req_nofail(fc, file); |
b6aeaded MS |
317 | memset(&inarg, 0, sizeof(inarg)); |
318 | inarg.fh = ff->fh; | |
9c8ef561 | 319 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
b6aeaded MS |
320 | req->in.h.opcode = FUSE_FLUSH; |
321 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
322 | req->in.numargs = 1; |
323 | req->in.args[0].size = sizeof(inarg); | |
324 | req->in.args[0].value = &inarg; | |
71421259 | 325 | req->force = 1; |
b93f858a | 326 | fuse_request_send(fc, req); |
b6aeaded MS |
327 | err = req->out.h.error; |
328 | fuse_put_request(fc, req); | |
329 | if (err == -ENOSYS) { | |
330 | fc->no_flush = 1; | |
331 | err = 0; | |
332 | } | |
333 | return err; | |
334 | } | |
335 | ||
3be5a52b MS |
336 | /* |
337 | * Wait for all pending writepages on the inode to finish. | |
338 | * | |
339 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
340 | * and waiting for all sent writes to complete. | |
341 | * | |
342 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
343 | * could conflict with truncation. | |
344 | */ | |
345 | static void fuse_sync_writes(struct inode *inode) | |
346 | { | |
347 | fuse_set_nowrite(inode); | |
348 | fuse_release_nowrite(inode); | |
349 | } | |
350 | ||
82547981 MS |
351 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
352 | int isdir) | |
b6aeaded MS |
353 | { |
354 | struct inode *inode = de->d_inode; | |
355 | struct fuse_conn *fc = get_fuse_conn(inode); | |
356 | struct fuse_file *ff = file->private_data; | |
357 | struct fuse_req *req; | |
358 | struct fuse_fsync_in inarg; | |
359 | int err; | |
360 | ||
248d86e8 MS |
361 | if (is_bad_inode(inode)) |
362 | return -EIO; | |
363 | ||
82547981 | 364 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
365 | return 0; |
366 | ||
3be5a52b MS |
367 | /* |
368 | * Start writeback against all dirty pages of the inode, then | |
369 | * wait for all outstanding writes, before sending the FSYNC | |
370 | * request. | |
371 | */ | |
372 | err = write_inode_now(inode, 0); | |
373 | if (err) | |
374 | return err; | |
375 | ||
376 | fuse_sync_writes(inode); | |
377 | ||
ce1d5a49 MS |
378 | req = fuse_get_req(fc); |
379 | if (IS_ERR(req)) | |
380 | return PTR_ERR(req); | |
b6aeaded MS |
381 | |
382 | memset(&inarg, 0, sizeof(inarg)); | |
383 | inarg.fh = ff->fh; | |
384 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 385 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 386 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
387 | req->in.numargs = 1; |
388 | req->in.args[0].size = sizeof(inarg); | |
389 | req->in.args[0].value = &inarg; | |
b93f858a | 390 | fuse_request_send(fc, req); |
b6aeaded MS |
391 | err = req->out.h.error; |
392 | fuse_put_request(fc, req); | |
393 | if (err == -ENOSYS) { | |
82547981 MS |
394 | if (isdir) |
395 | fc->no_fsyncdir = 1; | |
396 | else | |
397 | fc->no_fsync = 1; | |
b6aeaded MS |
398 | err = 0; |
399 | } | |
400 | return err; | |
401 | } | |
402 | ||
82547981 MS |
403 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
404 | { | |
405 | return fuse_fsync_common(file, de, datasync, 0); | |
406 | } | |
407 | ||
2106cb18 MS |
408 | void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos, |
409 | size_t count, int opcode) | |
b6aeaded | 410 | { |
5c5c5e51 | 411 | struct fuse_read_in *inarg = &req->misc.read.in; |
a6643094 | 412 | struct fuse_file *ff = file->private_data; |
b6aeaded | 413 | |
361b1eb5 MS |
414 | inarg->fh = ff->fh; |
415 | inarg->offset = pos; | |
416 | inarg->size = count; | |
a6643094 | 417 | inarg->flags = file->f_flags; |
361b1eb5 | 418 | req->in.h.opcode = opcode; |
2106cb18 | 419 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
420 | req->in.numargs = 1; |
421 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 422 | req->in.args[0].value = inarg; |
b6aeaded MS |
423 | req->out.argvar = 1; |
424 | req->out.numargs = 1; | |
425 | req->out.args[0].size = count; | |
b6aeaded MS |
426 | } |
427 | ||
8bfc016d | 428 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
2106cb18 | 429 | loff_t pos, size_t count, fl_owner_t owner) |
04730fef | 430 | { |
2106cb18 MS |
431 | struct fuse_file *ff = file->private_data; |
432 | struct fuse_conn *fc = ff->fc; | |
f3332114 | 433 | |
2106cb18 | 434 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
f3332114 | 435 | if (owner != NULL) { |
5c5c5e51 | 436 | struct fuse_read_in *inarg = &req->misc.read.in; |
f3332114 MS |
437 | |
438 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | |
439 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
440 | } | |
b93f858a | 441 | fuse_request_send(fc, req); |
361b1eb5 | 442 | return req->out.args[0].size; |
04730fef MS |
443 | } |
444 | ||
5c5c5e51 MS |
445 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
446 | u64 attr_ver) | |
447 | { | |
448 | struct fuse_conn *fc = get_fuse_conn(inode); | |
449 | struct fuse_inode *fi = get_fuse_inode(inode); | |
450 | ||
451 | spin_lock(&fc->lock); | |
452 | if (attr_ver == fi->attr_version && size < inode->i_size) { | |
453 | fi->attr_version = ++fc->attr_version; | |
454 | i_size_write(inode, size); | |
455 | } | |
456 | spin_unlock(&fc->lock); | |
457 | } | |
458 | ||
b6aeaded MS |
459 | static int fuse_readpage(struct file *file, struct page *page) |
460 | { | |
461 | struct inode *inode = page->mapping->host; | |
462 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 | 463 | struct fuse_req *req; |
5c5c5e51 MS |
464 | size_t num_read; |
465 | loff_t pos = page_offset(page); | |
466 | size_t count = PAGE_CACHE_SIZE; | |
467 | u64 attr_ver; | |
248d86e8 MS |
468 | int err; |
469 | ||
470 | err = -EIO; | |
471 | if (is_bad_inode(inode)) | |
472 | goto out; | |
473 | ||
3be5a52b MS |
474 | /* |
475 | * Page writeback can extend beyond the liftime of the | |
476 | * page-cache page, so make sure we read a properly synced | |
477 | * page. | |
478 | */ | |
479 | fuse_wait_on_page_writeback(inode, page->index); | |
480 | ||
ce1d5a49 MS |
481 | req = fuse_get_req(fc); |
482 | err = PTR_ERR(req); | |
483 | if (IS_ERR(req)) | |
b6aeaded MS |
484 | goto out; |
485 | ||
5c5c5e51 MS |
486 | attr_ver = fuse_get_attr_version(fc); |
487 | ||
b6aeaded | 488 | req->out.page_zeroing = 1; |
f4975c67 | 489 | req->out.argpages = 1; |
b6aeaded MS |
490 | req->num_pages = 1; |
491 | req->pages[0] = page; | |
2106cb18 | 492 | num_read = fuse_send_read(req, file, pos, count, NULL); |
b6aeaded MS |
493 | err = req->out.h.error; |
494 | fuse_put_request(fc, req); | |
5c5c5e51 MS |
495 | |
496 | if (!err) { | |
497 | /* | |
498 | * Short read means EOF. If file size is larger, truncate it | |
499 | */ | |
500 | if (num_read < count) | |
501 | fuse_read_update_size(inode, pos + num_read, attr_ver); | |
502 | ||
b6aeaded | 503 | SetPageUptodate(page); |
5c5c5e51 MS |
504 | } |
505 | ||
b36c31ba | 506 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
507 | out: |
508 | unlock_page(page); | |
509 | return err; | |
510 | } | |
511 | ||
c1aa96a5 | 512 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 513 | { |
c1aa96a5 | 514 | int i; |
5c5c5e51 MS |
515 | size_t count = req->misc.read.in.size; |
516 | size_t num_read = req->out.args[0].size; | |
517 | struct inode *inode = req->pages[0]->mapping->host; | |
c1aa96a5 | 518 | |
5c5c5e51 MS |
519 | /* |
520 | * Short read means EOF. If file size is larger, truncate it | |
521 | */ | |
522 | if (!req->out.h.error && num_read < count) { | |
523 | loff_t pos = page_offset(req->pages[0]) + num_read; | |
524 | fuse_read_update_size(inode, pos, req->misc.read.attr_ver); | |
525 | } | |
526 | ||
527 | fuse_invalidate_attr(inode); /* atime changed */ | |
c1aa96a5 | 528 | |
db50b96c MS |
529 | for (i = 0; i < req->num_pages; i++) { |
530 | struct page *page = req->pages[i]; | |
531 | if (!req->out.h.error) | |
532 | SetPageUptodate(page); | |
c1aa96a5 MS |
533 | else |
534 | SetPageError(page); | |
db50b96c MS |
535 | unlock_page(page); |
536 | } | |
c756e0a4 MS |
537 | if (req->ff) |
538 | fuse_file_put(req->ff); | |
c1aa96a5 MS |
539 | } |
540 | ||
2106cb18 | 541 | static void fuse_send_readpages(struct fuse_req *req, struct file *file) |
c1aa96a5 | 542 | { |
2106cb18 MS |
543 | struct fuse_file *ff = file->private_data; |
544 | struct fuse_conn *fc = ff->fc; | |
c1aa96a5 MS |
545 | loff_t pos = page_offset(req->pages[0]); |
546 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
f4975c67 MS |
547 | |
548 | req->out.argpages = 1; | |
c1aa96a5 | 549 | req->out.page_zeroing = 1; |
2106cb18 | 550 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
5c5c5e51 | 551 | req->misc.read.attr_ver = fuse_get_attr_version(fc); |
9cd68455 | 552 | if (fc->async_read) { |
c756e0a4 | 553 | req->ff = fuse_file_get(ff); |
9cd68455 | 554 | req->end = fuse_readpages_end; |
b93f858a | 555 | fuse_request_send_background(fc, req); |
9cd68455 | 556 | } else { |
b93f858a | 557 | fuse_request_send(fc, req); |
9cd68455 | 558 | fuse_readpages_end(fc, req); |
e9bb09dd | 559 | fuse_put_request(fc, req); |
9cd68455 | 560 | } |
db50b96c MS |
561 | } |
562 | ||
c756e0a4 | 563 | struct fuse_fill_data { |
db50b96c | 564 | struct fuse_req *req; |
a6643094 | 565 | struct file *file; |
db50b96c MS |
566 | struct inode *inode; |
567 | }; | |
568 | ||
569 | static int fuse_readpages_fill(void *_data, struct page *page) | |
570 | { | |
c756e0a4 | 571 | struct fuse_fill_data *data = _data; |
db50b96c MS |
572 | struct fuse_req *req = data->req; |
573 | struct inode *inode = data->inode; | |
574 | struct fuse_conn *fc = get_fuse_conn(inode); | |
575 | ||
3be5a52b MS |
576 | fuse_wait_on_page_writeback(inode, page->index); |
577 | ||
db50b96c MS |
578 | if (req->num_pages && |
579 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
580 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
581 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
2106cb18 | 582 | fuse_send_readpages(req, data->file); |
ce1d5a49 MS |
583 | data->req = req = fuse_get_req(fc); |
584 | if (IS_ERR(req)) { | |
db50b96c | 585 | unlock_page(page); |
ce1d5a49 | 586 | return PTR_ERR(req); |
db50b96c | 587 | } |
db50b96c MS |
588 | } |
589 | req->pages[req->num_pages] = page; | |
1729a16c | 590 | req->num_pages++; |
db50b96c MS |
591 | return 0; |
592 | } | |
593 | ||
594 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
595 | struct list_head *pages, unsigned nr_pages) | |
596 | { | |
597 | struct inode *inode = mapping->host; | |
598 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c756e0a4 | 599 | struct fuse_fill_data data; |
db50b96c | 600 | int err; |
248d86e8 | 601 | |
1d7ea732 | 602 | err = -EIO; |
248d86e8 | 603 | if (is_bad_inode(inode)) |
2e990021 | 604 | goto out; |
248d86e8 | 605 | |
a6643094 | 606 | data.file = file; |
db50b96c | 607 | data.inode = inode; |
ce1d5a49 | 608 | data.req = fuse_get_req(fc); |
1d7ea732 | 609 | err = PTR_ERR(data.req); |
ce1d5a49 | 610 | if (IS_ERR(data.req)) |
2e990021 | 611 | goto out; |
db50b96c MS |
612 | |
613 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
614 | if (!err) { |
615 | if (data.req->num_pages) | |
2106cb18 | 616 | fuse_send_readpages(data.req, file); |
d3406ffa MS |
617 | else |
618 | fuse_put_request(fc, data.req); | |
619 | } | |
2e990021 | 620 | out: |
1d7ea732 | 621 | return err; |
db50b96c MS |
622 | } |
623 | ||
bcb4be80 MS |
624 | static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
625 | unsigned long nr_segs, loff_t pos) | |
626 | { | |
627 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
628 | ||
629 | if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) { | |
630 | int err; | |
631 | /* | |
632 | * If trying to read past EOF, make sure the i_size | |
633 | * attribute is up-to-date. | |
634 | */ | |
635 | err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL); | |
636 | if (err) | |
637 | return err; | |
638 | } | |
639 | ||
640 | return generic_file_aio_read(iocb, iov, nr_segs, pos); | |
641 | } | |
642 | ||
2d698b07 | 643 | static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff, |
2106cb18 | 644 | loff_t pos, size_t count) |
b6aeaded | 645 | { |
b25e82e5 MS |
646 | struct fuse_write_in *inarg = &req->misc.write.in; |
647 | struct fuse_write_out *outarg = &req->misc.write.out; | |
b6aeaded | 648 | |
b25e82e5 MS |
649 | inarg->fh = ff->fh; |
650 | inarg->offset = pos; | |
651 | inarg->size = count; | |
b6aeaded | 652 | req->in.h.opcode = FUSE_WRITE; |
2106cb18 | 653 | req->in.h.nodeid = ff->nodeid; |
b6aeaded | 654 | req->in.numargs = 2; |
2106cb18 | 655 | if (ff->fc->minor < 9) |
f3332114 MS |
656 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
657 | else | |
658 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
b25e82e5 | 659 | req->in.args[0].value = inarg; |
b6aeaded MS |
660 | req->in.args[1].size = count; |
661 | req->out.numargs = 1; | |
662 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
b25e82e5 MS |
663 | req->out.args[0].value = outarg; |
664 | } | |
665 | ||
666 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, | |
2106cb18 | 667 | loff_t pos, size_t count, fl_owner_t owner) |
b25e82e5 | 668 | { |
2106cb18 MS |
669 | struct fuse_file *ff = file->private_data; |
670 | struct fuse_conn *fc = ff->fc; | |
2d698b07 MS |
671 | struct fuse_write_in *inarg = &req->misc.write.in; |
672 | ||
2106cb18 | 673 | fuse_write_fill(req, ff, pos, count); |
2d698b07 | 674 | inarg->flags = file->f_flags; |
f3332114 | 675 | if (owner != NULL) { |
f3332114 MS |
676 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
677 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
678 | } | |
b93f858a | 679 | fuse_request_send(fc, req); |
b25e82e5 | 680 | return req->misc.write.out.size; |
b6aeaded MS |
681 | } |
682 | ||
5e6f58a1 NP |
683 | static int fuse_write_begin(struct file *file, struct address_space *mapping, |
684 | loff_t pos, unsigned len, unsigned flags, | |
685 | struct page **pagep, void **fsdata) | |
b6aeaded | 686 | { |
5e6f58a1 NP |
687 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
688 | ||
54566b2c | 689 | *pagep = grab_cache_page_write_begin(mapping, index, flags); |
5e6f58a1 NP |
690 | if (!*pagep) |
691 | return -ENOMEM; | |
b6aeaded MS |
692 | return 0; |
693 | } | |
694 | ||
854512ec MS |
695 | static void fuse_write_update_size(struct inode *inode, loff_t pos) |
696 | { | |
697 | struct fuse_conn *fc = get_fuse_conn(inode); | |
698 | struct fuse_inode *fi = get_fuse_inode(inode); | |
699 | ||
700 | spin_lock(&fc->lock); | |
701 | fi->attr_version = ++fc->attr_version; | |
702 | if (pos > inode->i_size) | |
703 | i_size_write(inode, pos); | |
704 | spin_unlock(&fc->lock); | |
705 | } | |
706 | ||
5e6f58a1 NP |
707 | static int fuse_buffered_write(struct file *file, struct inode *inode, |
708 | loff_t pos, unsigned count, struct page *page) | |
b6aeaded MS |
709 | { |
710 | int err; | |
04730fef | 711 | size_t nres; |
b6aeaded | 712 | struct fuse_conn *fc = get_fuse_conn(inode); |
5e6f58a1 | 713 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
248d86e8 MS |
714 | struct fuse_req *req; |
715 | ||
716 | if (is_bad_inode(inode)) | |
717 | return -EIO; | |
718 | ||
3be5a52b MS |
719 | /* |
720 | * Make sure writepages on the same page are not mixed up with | |
721 | * plain writes. | |
722 | */ | |
723 | fuse_wait_on_page_writeback(inode, page->index); | |
724 | ||
ce1d5a49 MS |
725 | req = fuse_get_req(fc); |
726 | if (IS_ERR(req)) | |
727 | return PTR_ERR(req); | |
b6aeaded | 728 | |
f4975c67 | 729 | req->in.argpages = 1; |
b6aeaded MS |
730 | req->num_pages = 1; |
731 | req->pages[0] = page; | |
732 | req->page_offset = offset; | |
2106cb18 | 733 | nres = fuse_send_write(req, file, pos, count, NULL); |
b6aeaded MS |
734 | err = req->out.h.error; |
735 | fuse_put_request(fc, req); | |
5e6f58a1 | 736 | if (!err && !nres) |
b6aeaded MS |
737 | err = -EIO; |
738 | if (!err) { | |
5e6f58a1 | 739 | pos += nres; |
854512ec | 740 | fuse_write_update_size(inode, pos); |
5e6f58a1 | 741 | if (count == PAGE_CACHE_SIZE) |
b6aeaded | 742 | SetPageUptodate(page); |
b36c31ba MS |
743 | } |
744 | fuse_invalidate_attr(inode); | |
5e6f58a1 NP |
745 | return err ? err : nres; |
746 | } | |
747 | ||
748 | static int fuse_write_end(struct file *file, struct address_space *mapping, | |
749 | loff_t pos, unsigned len, unsigned copied, | |
750 | struct page *page, void *fsdata) | |
751 | { | |
752 | struct inode *inode = mapping->host; | |
753 | int res = 0; | |
754 | ||
755 | if (copied) | |
756 | res = fuse_buffered_write(file, inode, pos, copied, page); | |
757 | ||
758 | unlock_page(page); | |
759 | page_cache_release(page); | |
760 | return res; | |
b6aeaded MS |
761 | } |
762 | ||
ea9b9907 NP |
763 | static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file, |
764 | struct inode *inode, loff_t pos, | |
765 | size_t count) | |
766 | { | |
767 | size_t res; | |
768 | unsigned offset; | |
769 | unsigned i; | |
770 | ||
771 | for (i = 0; i < req->num_pages; i++) | |
772 | fuse_wait_on_page_writeback(inode, req->pages[i]->index); | |
773 | ||
2106cb18 | 774 | res = fuse_send_write(req, file, pos, count, NULL); |
ea9b9907 NP |
775 | |
776 | offset = req->page_offset; | |
777 | count = res; | |
778 | for (i = 0; i < req->num_pages; i++) { | |
779 | struct page *page = req->pages[i]; | |
780 | ||
781 | if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE) | |
782 | SetPageUptodate(page); | |
783 | ||
784 | if (count > PAGE_CACHE_SIZE - offset) | |
785 | count -= PAGE_CACHE_SIZE - offset; | |
786 | else | |
787 | count = 0; | |
788 | offset = 0; | |
789 | ||
790 | unlock_page(page); | |
791 | page_cache_release(page); | |
792 | } | |
793 | ||
794 | return res; | |
795 | } | |
796 | ||
797 | static ssize_t fuse_fill_write_pages(struct fuse_req *req, | |
798 | struct address_space *mapping, | |
799 | struct iov_iter *ii, loff_t pos) | |
800 | { | |
801 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
802 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | |
803 | size_t count = 0; | |
804 | int err; | |
805 | ||
f4975c67 | 806 | req->in.argpages = 1; |
ea9b9907 NP |
807 | req->page_offset = offset; |
808 | ||
809 | do { | |
810 | size_t tmp; | |
811 | struct page *page; | |
812 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
813 | size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset, | |
814 | iov_iter_count(ii)); | |
815 | ||
816 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
817 | ||
818 | again: | |
819 | err = -EFAULT; | |
820 | if (iov_iter_fault_in_readable(ii, bytes)) | |
821 | break; | |
822 | ||
823 | err = -ENOMEM; | |
54566b2c | 824 | page = grab_cache_page_write_begin(mapping, index, 0); |
ea9b9907 NP |
825 | if (!page) |
826 | break; | |
827 | ||
828 | pagefault_disable(); | |
829 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); | |
830 | pagefault_enable(); | |
831 | flush_dcache_page(page); | |
832 | ||
833 | if (!tmp) { | |
834 | unlock_page(page); | |
835 | page_cache_release(page); | |
836 | bytes = min(bytes, iov_iter_single_seg_count(ii)); | |
837 | goto again; | |
838 | } | |
839 | ||
840 | err = 0; | |
841 | req->pages[req->num_pages] = page; | |
842 | req->num_pages++; | |
843 | ||
844 | iov_iter_advance(ii, tmp); | |
845 | count += tmp; | |
846 | pos += tmp; | |
847 | offset += tmp; | |
848 | if (offset == PAGE_CACHE_SIZE) | |
849 | offset = 0; | |
850 | ||
78bb6cb9 MS |
851 | if (!fc->big_writes) |
852 | break; | |
ea9b9907 NP |
853 | } while (iov_iter_count(ii) && count < fc->max_write && |
854 | req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0); | |
855 | ||
856 | return count > 0 ? count : err; | |
857 | } | |
858 | ||
859 | static ssize_t fuse_perform_write(struct file *file, | |
860 | struct address_space *mapping, | |
861 | struct iov_iter *ii, loff_t pos) | |
862 | { | |
863 | struct inode *inode = mapping->host; | |
864 | struct fuse_conn *fc = get_fuse_conn(inode); | |
865 | int err = 0; | |
866 | ssize_t res = 0; | |
867 | ||
868 | if (is_bad_inode(inode)) | |
869 | return -EIO; | |
870 | ||
871 | do { | |
872 | struct fuse_req *req; | |
873 | ssize_t count; | |
874 | ||
875 | req = fuse_get_req(fc); | |
876 | if (IS_ERR(req)) { | |
877 | err = PTR_ERR(req); | |
878 | break; | |
879 | } | |
880 | ||
881 | count = fuse_fill_write_pages(req, mapping, ii, pos); | |
882 | if (count <= 0) { | |
883 | err = count; | |
884 | } else { | |
885 | size_t num_written; | |
886 | ||
887 | num_written = fuse_send_write_pages(req, file, inode, | |
888 | pos, count); | |
889 | err = req->out.h.error; | |
890 | if (!err) { | |
891 | res += num_written; | |
892 | pos += num_written; | |
893 | ||
894 | /* break out of the loop on short write */ | |
895 | if (num_written != count) | |
896 | err = -EIO; | |
897 | } | |
898 | } | |
899 | fuse_put_request(fc, req); | |
900 | } while (!err && iov_iter_count(ii)); | |
901 | ||
902 | if (res > 0) | |
903 | fuse_write_update_size(inode, pos); | |
904 | ||
905 | fuse_invalidate_attr(inode); | |
906 | ||
907 | return res > 0 ? res : err; | |
908 | } | |
909 | ||
910 | static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | |
911 | unsigned long nr_segs, loff_t pos) | |
912 | { | |
913 | struct file *file = iocb->ki_filp; | |
914 | struct address_space *mapping = file->f_mapping; | |
915 | size_t count = 0; | |
916 | ssize_t written = 0; | |
917 | struct inode *inode = mapping->host; | |
918 | ssize_t err; | |
919 | struct iov_iter i; | |
920 | ||
921 | WARN_ON(iocb->ki_pos != pos); | |
922 | ||
923 | err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ); | |
924 | if (err) | |
925 | return err; | |
926 | ||
927 | mutex_lock(&inode->i_mutex); | |
928 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
929 | ||
930 | /* We can write back this queue in page reclaim */ | |
931 | current->backing_dev_info = mapping->backing_dev_info; | |
932 | ||
933 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
934 | if (err) | |
935 | goto out; | |
936 | ||
937 | if (count == 0) | |
938 | goto out; | |
939 | ||
2f1936b8 | 940 | err = file_remove_suid(file); |
ea9b9907 NP |
941 | if (err) |
942 | goto out; | |
943 | ||
944 | file_update_time(file); | |
945 | ||
946 | iov_iter_init(&i, iov, nr_segs, count, 0); | |
947 | written = fuse_perform_write(file, mapping, &i, pos); | |
948 | if (written >= 0) | |
949 | iocb->ki_pos = pos + written; | |
950 | ||
951 | out: | |
952 | current->backing_dev_info = NULL; | |
953 | mutex_unlock(&inode->i_mutex); | |
954 | ||
955 | return written ? written : err; | |
956 | } | |
957 | ||
413ef8cb MS |
958 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
959 | { | |
960 | unsigned i; | |
961 | ||
962 | for (i = 0; i < req->num_pages; i++) { | |
963 | struct page *page = req->pages[i]; | |
964 | if (write) | |
965 | set_page_dirty_lock(page); | |
966 | put_page(page); | |
967 | } | |
968 | } | |
969 | ||
970 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | |
ce60a2f1 | 971 | size_t *nbytesp, int write) |
413ef8cb | 972 | { |
ce60a2f1 | 973 | size_t nbytes = *nbytesp; |
413ef8cb MS |
974 | unsigned long user_addr = (unsigned long) buf; |
975 | unsigned offset = user_addr & ~PAGE_MASK; | |
976 | int npages; | |
977 | ||
f4975c67 MS |
978 | /* Special case for kernel I/O: can copy directly into the buffer */ |
979 | if (segment_eq(get_fs(), KERNEL_DS)) { | |
980 | if (write) | |
981 | req->in.args[1].value = (void *) user_addr; | |
982 | else | |
983 | req->out.args[0].value = (void *) user_addr; | |
984 | ||
985 | return 0; | |
986 | } | |
413ef8cb | 987 | |
ce60a2f1 | 988 | nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); |
413ef8cb | 989 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
bd730967 | 990 | npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ); |
413ef8cb | 991 | down_read(¤t->mm->mmap_sem); |
f4975c67 | 992 | npages = get_user_pages(current, current->mm, user_addr, npages, !write, |
413ef8cb MS |
993 | 0, req->pages, NULL); |
994 | up_read(¤t->mm->mmap_sem); | |
995 | if (npages < 0) | |
996 | return npages; | |
997 | ||
998 | req->num_pages = npages; | |
999 | req->page_offset = offset; | |
f4975c67 MS |
1000 | |
1001 | if (write) | |
1002 | req->in.argpages = 1; | |
1003 | else | |
1004 | req->out.argpages = 1; | |
1005 | ||
1006 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; | |
1007 | *nbytesp = min(*nbytesp, nbytes); | |
1008 | ||
413ef8cb MS |
1009 | return 0; |
1010 | } | |
1011 | ||
1012 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |
1013 | size_t count, loff_t *ppos, int write) | |
1014 | { | |
2106cb18 MS |
1015 | struct fuse_file *ff = file->private_data; |
1016 | struct fuse_conn *fc = ff->fc; | |
413ef8cb MS |
1017 | size_t nmax = write ? fc->max_write : fc->max_read; |
1018 | loff_t pos = *ppos; | |
1019 | ssize_t res = 0; | |
248d86e8 MS |
1020 | struct fuse_req *req; |
1021 | ||
ce1d5a49 MS |
1022 | req = fuse_get_req(fc); |
1023 | if (IS_ERR(req)) | |
1024 | return PTR_ERR(req); | |
413ef8cb MS |
1025 | |
1026 | while (count) { | |
413ef8cb | 1027 | size_t nres; |
2106cb18 | 1028 | fl_owner_t owner = current->files; |
f4975c67 MS |
1029 | size_t nbytes = min(count, nmax); |
1030 | int err = fuse_get_user_pages(req, buf, &nbytes, write); | |
413ef8cb MS |
1031 | if (err) { |
1032 | res = err; | |
1033 | break; | |
1034 | } | |
f4975c67 | 1035 | |
413ef8cb | 1036 | if (write) |
2106cb18 | 1037 | nres = fuse_send_write(req, file, pos, nbytes, owner); |
413ef8cb | 1038 | else |
2106cb18 MS |
1039 | nres = fuse_send_read(req, file, pos, nbytes, owner); |
1040 | ||
413ef8cb MS |
1041 | fuse_release_user_pages(req, !write); |
1042 | if (req->out.h.error) { | |
1043 | if (!res) | |
1044 | res = req->out.h.error; | |
1045 | break; | |
1046 | } else if (nres > nbytes) { | |
1047 | res = -EIO; | |
1048 | break; | |
1049 | } | |
1050 | count -= nres; | |
1051 | res += nres; | |
1052 | pos += nres; | |
1053 | buf += nres; | |
1054 | if (nres != nbytes) | |
1055 | break; | |
56cf34ff MS |
1056 | if (count) { |
1057 | fuse_put_request(fc, req); | |
1058 | req = fuse_get_req(fc); | |
1059 | if (IS_ERR(req)) | |
1060 | break; | |
1061 | } | |
413ef8cb MS |
1062 | } |
1063 | fuse_put_request(fc, req); | |
d09cb9d7 | 1064 | if (res > 0) |
413ef8cb | 1065 | *ppos = pos; |
413ef8cb MS |
1066 | |
1067 | return res; | |
1068 | } | |
1069 | ||
1070 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, | |
1071 | size_t count, loff_t *ppos) | |
1072 | { | |
d09cb9d7 MS |
1073 | ssize_t res; |
1074 | struct inode *inode = file->f_path.dentry->d_inode; | |
1075 | ||
1076 | if (is_bad_inode(inode)) | |
1077 | return -EIO; | |
1078 | ||
1079 | res = fuse_direct_io(file, buf, count, ppos, 0); | |
1080 | ||
1081 | fuse_invalidate_attr(inode); | |
1082 | ||
1083 | return res; | |
413ef8cb MS |
1084 | } |
1085 | ||
1086 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | |
1087 | size_t count, loff_t *ppos) | |
1088 | { | |
7706a9d6 | 1089 | struct inode *inode = file->f_path.dentry->d_inode; |
413ef8cb | 1090 | ssize_t res; |
d09cb9d7 MS |
1091 | |
1092 | if (is_bad_inode(inode)) | |
1093 | return -EIO; | |
1094 | ||
413ef8cb | 1095 | /* Don't allow parallel writes to the same file */ |
1b1dcc1b | 1096 | mutex_lock(&inode->i_mutex); |
889f7848 | 1097 | res = generic_write_checks(file, ppos, &count, 0); |
d09cb9d7 | 1098 | if (!res) { |
889f7848 | 1099 | res = fuse_direct_io(file, buf, count, ppos, 1); |
d09cb9d7 MS |
1100 | if (res > 0) |
1101 | fuse_write_update_size(inode, *ppos); | |
1102 | } | |
1b1dcc1b | 1103 | mutex_unlock(&inode->i_mutex); |
d09cb9d7 MS |
1104 | |
1105 | fuse_invalidate_attr(inode); | |
1106 | ||
413ef8cb MS |
1107 | return res; |
1108 | } | |
1109 | ||
3be5a52b | 1110 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
b6aeaded | 1111 | { |
3be5a52b MS |
1112 | __free_page(req->pages[0]); |
1113 | fuse_file_put(req->ff); | |
3be5a52b MS |
1114 | } |
1115 | ||
1116 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | |
1117 | { | |
1118 | struct inode *inode = req->inode; | |
1119 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1120 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; | |
1121 | ||
1122 | list_del(&req->writepages_entry); | |
1123 | dec_bdi_stat(bdi, BDI_WRITEBACK); | |
1124 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); | |
1125 | bdi_writeout_inc(bdi); | |
1126 | wake_up(&fi->page_waitq); | |
1127 | } | |
1128 | ||
1129 | /* Called under fc->lock, may release and reacquire it */ | |
1130 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) | |
5d9ec854 HH |
1131 | __releases(&fc->lock) |
1132 | __acquires(&fc->lock) | |
3be5a52b MS |
1133 | { |
1134 | struct fuse_inode *fi = get_fuse_inode(req->inode); | |
1135 | loff_t size = i_size_read(req->inode); | |
1136 | struct fuse_write_in *inarg = &req->misc.write.in; | |
1137 | ||
1138 | if (!fc->connected) | |
1139 | goto out_free; | |
1140 | ||
1141 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { | |
1142 | inarg->size = PAGE_CACHE_SIZE; | |
1143 | } else if (inarg->offset < size) { | |
1144 | inarg->size = size & (PAGE_CACHE_SIZE - 1); | |
1145 | } else { | |
1146 | /* Got truncated off completely */ | |
1147 | goto out_free; | |
b6aeaded | 1148 | } |
3be5a52b MS |
1149 | |
1150 | req->in.args[1].size = inarg->size; | |
1151 | fi->writectr++; | |
b93f858a | 1152 | fuse_request_send_background_locked(fc, req); |
3be5a52b MS |
1153 | return; |
1154 | ||
1155 | out_free: | |
1156 | fuse_writepage_finish(fc, req); | |
1157 | spin_unlock(&fc->lock); | |
1158 | fuse_writepage_free(fc, req); | |
e9bb09dd | 1159 | fuse_put_request(fc, req); |
3be5a52b | 1160 | spin_lock(&fc->lock); |
b6aeaded MS |
1161 | } |
1162 | ||
3be5a52b MS |
1163 | /* |
1164 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1165 | * all queued writepage requests. | |
1166 | * | |
1167 | * Called with fc->lock | |
1168 | */ | |
1169 | void fuse_flush_writepages(struct inode *inode) | |
5d9ec854 HH |
1170 | __releases(&fc->lock) |
1171 | __acquires(&fc->lock) | |
b6aeaded | 1172 | { |
3be5a52b MS |
1173 | struct fuse_conn *fc = get_fuse_conn(inode); |
1174 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1175 | struct fuse_req *req; | |
1176 | ||
1177 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
1178 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | |
1179 | list_del_init(&req->list); | |
1180 | fuse_send_writepage(fc, req); | |
1181 | } | |
1182 | } | |
1183 | ||
1184 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | |
1185 | { | |
1186 | struct inode *inode = req->inode; | |
1187 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1188 | ||
1189 | mapping_set_error(inode->i_mapping, req->out.h.error); | |
1190 | spin_lock(&fc->lock); | |
1191 | fi->writectr--; | |
1192 | fuse_writepage_finish(fc, req); | |
1193 | spin_unlock(&fc->lock); | |
1194 | fuse_writepage_free(fc, req); | |
1195 | } | |
1196 | ||
1197 | static int fuse_writepage_locked(struct page *page) | |
1198 | { | |
1199 | struct address_space *mapping = page->mapping; | |
1200 | struct inode *inode = mapping->host; | |
1201 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1202 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1203 | struct fuse_req *req; | |
1204 | struct fuse_file *ff; | |
1205 | struct page *tmp_page; | |
1206 | ||
1207 | set_page_writeback(page); | |
1208 | ||
1209 | req = fuse_request_alloc_nofs(); | |
1210 | if (!req) | |
1211 | goto err; | |
1212 | ||
1213 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
1214 | if (!tmp_page) | |
1215 | goto err_free; | |
1216 | ||
1217 | spin_lock(&fc->lock); | |
1218 | BUG_ON(list_empty(&fi->write_files)); | |
1219 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); | |
1220 | req->ff = fuse_file_get(ff); | |
1221 | spin_unlock(&fc->lock); | |
1222 | ||
2106cb18 | 1223 | fuse_write_fill(req, ff, page_offset(page), 0); |
3be5a52b MS |
1224 | |
1225 | copy_highpage(tmp_page, page); | |
2d698b07 | 1226 | req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; |
f4975c67 | 1227 | req->in.argpages = 1; |
3be5a52b MS |
1228 | req->num_pages = 1; |
1229 | req->pages[0] = tmp_page; | |
1230 | req->page_offset = 0; | |
1231 | req->end = fuse_writepage_end; | |
1232 | req->inode = inode; | |
1233 | ||
1234 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); | |
1235 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); | |
1236 | end_page_writeback(page); | |
1237 | ||
1238 | spin_lock(&fc->lock); | |
1239 | list_add(&req->writepages_entry, &fi->writepages); | |
1240 | list_add_tail(&req->list, &fi->queued_writes); | |
1241 | fuse_flush_writepages(inode); | |
1242 | spin_unlock(&fc->lock); | |
1243 | ||
1244 | return 0; | |
1245 | ||
1246 | err_free: | |
1247 | fuse_request_free(req); | |
1248 | err: | |
1249 | end_page_writeback(page); | |
1250 | return -ENOMEM; | |
1251 | } | |
1252 | ||
1253 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1254 | { | |
1255 | int err; | |
1256 | ||
1257 | err = fuse_writepage_locked(page); | |
1258 | unlock_page(page); | |
1259 | ||
1260 | return err; | |
1261 | } | |
1262 | ||
1263 | static int fuse_launder_page(struct page *page) | |
1264 | { | |
1265 | int err = 0; | |
1266 | if (clear_page_dirty_for_io(page)) { | |
1267 | struct inode *inode = page->mapping->host; | |
1268 | err = fuse_writepage_locked(page); | |
1269 | if (!err) | |
1270 | fuse_wait_on_page_writeback(inode, page->index); | |
1271 | } | |
1272 | return err; | |
1273 | } | |
1274 | ||
1275 | /* | |
1276 | * Write back dirty pages now, because there may not be any suitable | |
1277 | * open files later | |
1278 | */ | |
1279 | static void fuse_vma_close(struct vm_area_struct *vma) | |
1280 | { | |
1281 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
1282 | } | |
1283 | ||
1284 | /* | |
1285 | * Wait for writeback against this page to complete before allowing it | |
1286 | * to be marked dirty again, and hence written back again, possibly | |
1287 | * before the previous writepage completed. | |
1288 | * | |
1289 | * Block here, instead of in ->writepage(), so that the userspace fs | |
1290 | * can only block processes actually operating on the filesystem. | |
1291 | * | |
1292 | * Otherwise unprivileged userspace fs would be able to block | |
1293 | * unrelated: | |
1294 | * | |
1295 | * - page migration | |
1296 | * - sync(2) | |
1297 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
1298 | */ | |
c2ec175c | 1299 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
3be5a52b | 1300 | { |
c2ec175c | 1301 | struct page *page = vmf->page; |
3be5a52b MS |
1302 | /* |
1303 | * Don't use page->mapping as it may become NULL from a | |
1304 | * concurrent truncate. | |
1305 | */ | |
1306 | struct inode *inode = vma->vm_file->f_mapping->host; | |
1307 | ||
1308 | fuse_wait_on_page_writeback(inode, page->index); | |
1309 | return 0; | |
1310 | } | |
1311 | ||
1312 | static struct vm_operations_struct fuse_file_vm_ops = { | |
1313 | .close = fuse_vma_close, | |
1314 | .fault = filemap_fault, | |
1315 | .page_mkwrite = fuse_page_mkwrite, | |
1316 | }; | |
1317 | ||
1318 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
1319 | { | |
1320 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { | |
1321 | struct inode *inode = file->f_dentry->d_inode; | |
1322 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1323 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1324 | struct fuse_file *ff = file->private_data; | |
1325 | /* | |
1326 | * file may be written through mmap, so chain it onto the | |
1327 | * inodes's write_file list | |
1328 | */ | |
1329 | spin_lock(&fc->lock); | |
1330 | if (list_empty(&ff->write_entry)) | |
1331 | list_add(&ff->write_entry, &fi->write_files); | |
1332 | spin_unlock(&fc->lock); | |
1333 | } | |
1334 | file_accessed(file); | |
1335 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
1336 | return 0; |
1337 | } | |
1338 | ||
fc280c96 MS |
1339 | static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma) |
1340 | { | |
1341 | /* Can't provide the coherency needed for MAP_SHARED */ | |
1342 | if (vma->vm_flags & VM_MAYSHARE) | |
1343 | return -ENODEV; | |
1344 | ||
3121bfe7 MS |
1345 | invalidate_inode_pages2(file->f_mapping); |
1346 | ||
fc280c96 MS |
1347 | return generic_file_mmap(file, vma); |
1348 | } | |
1349 | ||
71421259 MS |
1350 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
1351 | struct file_lock *fl) | |
1352 | { | |
1353 | switch (ffl->type) { | |
1354 | case F_UNLCK: | |
1355 | break; | |
1356 | ||
1357 | case F_RDLCK: | |
1358 | case F_WRLCK: | |
1359 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
1360 | ffl->end < ffl->start) | |
1361 | return -EIO; | |
1362 | ||
1363 | fl->fl_start = ffl->start; | |
1364 | fl->fl_end = ffl->end; | |
1365 | fl->fl_pid = ffl->pid; | |
1366 | break; | |
1367 | ||
1368 | default: | |
1369 | return -EIO; | |
1370 | } | |
1371 | fl->fl_type = ffl->type; | |
1372 | return 0; | |
1373 | } | |
1374 | ||
1375 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, | |
a9ff4f87 MS |
1376 | const struct file_lock *fl, int opcode, pid_t pid, |
1377 | int flock) | |
71421259 | 1378 | { |
7706a9d6 | 1379 | struct inode *inode = file->f_path.dentry->d_inode; |
9c8ef561 | 1380 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 MS |
1381 | struct fuse_file *ff = file->private_data; |
1382 | struct fuse_lk_in *arg = &req->misc.lk_in; | |
1383 | ||
1384 | arg->fh = ff->fh; | |
9c8ef561 | 1385 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
71421259 MS |
1386 | arg->lk.start = fl->fl_start; |
1387 | arg->lk.end = fl->fl_end; | |
1388 | arg->lk.type = fl->fl_type; | |
1389 | arg->lk.pid = pid; | |
a9ff4f87 MS |
1390 | if (flock) |
1391 | arg->lk_flags |= FUSE_LK_FLOCK; | |
71421259 MS |
1392 | req->in.h.opcode = opcode; |
1393 | req->in.h.nodeid = get_node_id(inode); | |
1394 | req->in.numargs = 1; | |
1395 | req->in.args[0].size = sizeof(*arg); | |
1396 | req->in.args[0].value = arg; | |
1397 | } | |
1398 | ||
1399 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
1400 | { | |
7706a9d6 | 1401 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1402 | struct fuse_conn *fc = get_fuse_conn(inode); |
1403 | struct fuse_req *req; | |
1404 | struct fuse_lk_out outarg; | |
1405 | int err; | |
1406 | ||
1407 | req = fuse_get_req(fc); | |
1408 | if (IS_ERR(req)) | |
1409 | return PTR_ERR(req); | |
1410 | ||
a9ff4f87 | 1411 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
71421259 MS |
1412 | req->out.numargs = 1; |
1413 | req->out.args[0].size = sizeof(outarg); | |
1414 | req->out.args[0].value = &outarg; | |
b93f858a | 1415 | fuse_request_send(fc, req); |
71421259 MS |
1416 | err = req->out.h.error; |
1417 | fuse_put_request(fc, req); | |
1418 | if (!err) | |
1419 | err = convert_fuse_file_lock(&outarg.lk, fl); | |
1420 | ||
1421 | return err; | |
1422 | } | |
1423 | ||
a9ff4f87 | 1424 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 1425 | { |
7706a9d6 | 1426 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1427 | struct fuse_conn *fc = get_fuse_conn(inode); |
1428 | struct fuse_req *req; | |
1429 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; | |
1430 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; | |
1431 | int err; | |
1432 | ||
48e90761 MS |
1433 | if (fl->fl_lmops && fl->fl_lmops->fl_grant) { |
1434 | /* NLM needs asynchronous locks, which we don't support yet */ | |
1435 | return -ENOLCK; | |
1436 | } | |
1437 | ||
71421259 MS |
1438 | /* Unlock on close is handled by the flush method */ |
1439 | if (fl->fl_flags & FL_CLOSE) | |
1440 | return 0; | |
1441 | ||
1442 | req = fuse_get_req(fc); | |
1443 | if (IS_ERR(req)) | |
1444 | return PTR_ERR(req); | |
1445 | ||
a9ff4f87 | 1446 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
b93f858a | 1447 | fuse_request_send(fc, req); |
71421259 | 1448 | err = req->out.h.error; |
a4d27e75 MS |
1449 | /* locking is restartable */ |
1450 | if (err == -EINTR) | |
1451 | err = -ERESTARTSYS; | |
71421259 MS |
1452 | fuse_put_request(fc, req); |
1453 | return err; | |
1454 | } | |
1455 | ||
1456 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
1457 | { | |
7706a9d6 | 1458 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1459 | struct fuse_conn *fc = get_fuse_conn(inode); |
1460 | int err; | |
1461 | ||
48e90761 MS |
1462 | if (cmd == F_CANCELLK) { |
1463 | err = 0; | |
1464 | } else if (cmd == F_GETLK) { | |
71421259 | 1465 | if (fc->no_lock) { |
9d6a8c5c | 1466 | posix_test_lock(file, fl); |
71421259 MS |
1467 | err = 0; |
1468 | } else | |
1469 | err = fuse_getlk(file, fl); | |
1470 | } else { | |
1471 | if (fc->no_lock) | |
48e90761 | 1472 | err = posix_lock_file(file, fl, NULL); |
71421259 | 1473 | else |
a9ff4f87 | 1474 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
1475 | } |
1476 | return err; | |
1477 | } | |
1478 | ||
a9ff4f87 MS |
1479 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
1480 | { | |
1481 | struct inode *inode = file->f_path.dentry->d_inode; | |
1482 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1483 | int err; | |
1484 | ||
1485 | if (fc->no_lock) { | |
1486 | err = flock_lock_file_wait(file, fl); | |
1487 | } else { | |
1488 | /* emulate flock with POSIX locks */ | |
1489 | fl->fl_owner = (fl_owner_t) file; | |
1490 | err = fuse_setlk(file, fl, 1); | |
1491 | } | |
1492 | ||
1493 | return err; | |
1494 | } | |
1495 | ||
b2d2272f MS |
1496 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
1497 | { | |
1498 | struct inode *inode = mapping->host; | |
1499 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1500 | struct fuse_req *req; | |
1501 | struct fuse_bmap_in inarg; | |
1502 | struct fuse_bmap_out outarg; | |
1503 | int err; | |
1504 | ||
1505 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
1506 | return 0; | |
1507 | ||
1508 | req = fuse_get_req(fc); | |
1509 | if (IS_ERR(req)) | |
1510 | return 0; | |
1511 | ||
1512 | memset(&inarg, 0, sizeof(inarg)); | |
1513 | inarg.block = block; | |
1514 | inarg.blocksize = inode->i_sb->s_blocksize; | |
1515 | req->in.h.opcode = FUSE_BMAP; | |
1516 | req->in.h.nodeid = get_node_id(inode); | |
1517 | req->in.numargs = 1; | |
1518 | req->in.args[0].size = sizeof(inarg); | |
1519 | req->in.args[0].value = &inarg; | |
1520 | req->out.numargs = 1; | |
1521 | req->out.args[0].size = sizeof(outarg); | |
1522 | req->out.args[0].value = &outarg; | |
b93f858a | 1523 | fuse_request_send(fc, req); |
b2d2272f MS |
1524 | err = req->out.h.error; |
1525 | fuse_put_request(fc, req); | |
1526 | if (err == -ENOSYS) | |
1527 | fc->no_bmap = 1; | |
1528 | ||
1529 | return err ? 0 : outarg.block; | |
1530 | } | |
1531 | ||
5559b8f4 MS |
1532 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin) |
1533 | { | |
1534 | loff_t retval; | |
1535 | struct inode *inode = file->f_path.dentry->d_inode; | |
1536 | ||
1537 | mutex_lock(&inode->i_mutex); | |
1538 | switch (origin) { | |
1539 | case SEEK_END: | |
769415c6 MS |
1540 | retval = fuse_update_attributes(inode, NULL, file, NULL); |
1541 | if (retval) | |
5291658d | 1542 | goto exit; |
5559b8f4 MS |
1543 | offset += i_size_read(inode); |
1544 | break; | |
1545 | case SEEK_CUR: | |
1546 | offset += file->f_pos; | |
1547 | } | |
1548 | retval = -EINVAL; | |
1549 | if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) { | |
1550 | if (offset != file->f_pos) { | |
1551 | file->f_pos = offset; | |
1552 | file->f_version = 0; | |
1553 | } | |
1554 | retval = offset; | |
1555 | } | |
5291658d | 1556 | exit: |
5559b8f4 MS |
1557 | mutex_unlock(&inode->i_mutex); |
1558 | return retval; | |
1559 | } | |
1560 | ||
59efec7b TH |
1561 | static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov, |
1562 | unsigned int nr_segs, size_t bytes, bool to_user) | |
1563 | { | |
1564 | struct iov_iter ii; | |
1565 | int page_idx = 0; | |
1566 | ||
1567 | if (!bytes) | |
1568 | return 0; | |
1569 | ||
1570 | iov_iter_init(&ii, iov, nr_segs, bytes, 0); | |
1571 | ||
1572 | while (iov_iter_count(&ii)) { | |
1573 | struct page *page = pages[page_idx++]; | |
1574 | size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii)); | |
1575 | void *kaddr, *map; | |
1576 | ||
1577 | kaddr = map = kmap(page); | |
1578 | ||
1579 | while (todo) { | |
1580 | char __user *uaddr = ii.iov->iov_base + ii.iov_offset; | |
1581 | size_t iov_len = ii.iov->iov_len - ii.iov_offset; | |
1582 | size_t copy = min(todo, iov_len); | |
1583 | size_t left; | |
1584 | ||
1585 | if (!to_user) | |
1586 | left = copy_from_user(kaddr, uaddr, copy); | |
1587 | else | |
1588 | left = copy_to_user(uaddr, kaddr, copy); | |
1589 | ||
1590 | if (unlikely(left)) | |
1591 | return -EFAULT; | |
1592 | ||
1593 | iov_iter_advance(&ii, copy); | |
1594 | todo -= copy; | |
1595 | kaddr += copy; | |
1596 | } | |
1597 | ||
1598 | kunmap(map); | |
1599 | } | |
1600 | ||
1601 | return 0; | |
1602 | } | |
1603 | ||
1604 | /* | |
1605 | * For ioctls, there is no generic way to determine how much memory | |
1606 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
1607 | * to dereference the passed pointer, so the parameter requires deep | |
1608 | * copying but FUSE has no idea whatsoever about what to copy in or | |
1609 | * out. | |
1610 | * | |
1611 | * This is solved by allowing FUSE server to retry ioctl with | |
1612 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
1613 | * needs to read in the following structure. | |
1614 | * | |
1615 | * struct a { | |
1616 | * char *buf; | |
1617 | * size_t buflen; | |
1618 | * } | |
1619 | * | |
1620 | * On the first callout to FUSE server, inarg->in_size and | |
1621 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
1622 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
1623 | * the actual iov array to | |
1624 | * | |
1625 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
1626 | * | |
1627 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
1628 | * On the second round, the server has access to the structure and | |
1629 | * from that it can tell what to look for next, so on the invocation, | |
1630 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
1631 | * | |
1632 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
1633 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
1634 | * | |
1635 | * FUSE will copy both struct a and the pointed buffer from the | |
1636 | * process doing the ioctl and retry ioctl with both struct a and the | |
1637 | * buffer. | |
1638 | * | |
1639 | * This time, FUSE server has everything it needs and completes ioctl | |
1640 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
1641 | * | |
1642 | * Copying data out works the same way. | |
1643 | * | |
1644 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
1645 | * automatically initializes in and out iovs by decoding @cmd with | |
1646 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
1647 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
1648 | * behavior for all FUSE servers. | |
1649 | */ | |
d36f2487 MS |
1650 | static long fuse_do_ioctl(struct file *file, unsigned int cmd, |
1651 | unsigned long arg, unsigned int flags) | |
59efec7b | 1652 | { |
59efec7b | 1653 | struct fuse_file *ff = file->private_data; |
d36f2487 | 1654 | struct fuse_conn *fc = ff->fc; |
59efec7b TH |
1655 | struct fuse_ioctl_in inarg = { |
1656 | .fh = ff->fh, | |
1657 | .cmd = cmd, | |
1658 | .arg = arg, | |
1659 | .flags = flags | |
1660 | }; | |
1661 | struct fuse_ioctl_out outarg; | |
1662 | struct fuse_req *req = NULL; | |
1663 | struct page **pages = NULL; | |
1664 | struct page *iov_page = NULL; | |
1665 | struct iovec *in_iov = NULL, *out_iov = NULL; | |
1666 | unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages; | |
1667 | size_t in_size, out_size, transferred; | |
1668 | int err; | |
1669 | ||
1670 | /* assume all the iovs returned by client always fits in a page */ | |
1671 | BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); | |
1672 | ||
59efec7b TH |
1673 | err = -ENOMEM; |
1674 | pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL); | |
1675 | iov_page = alloc_page(GFP_KERNEL); | |
1676 | if (!pages || !iov_page) | |
1677 | goto out; | |
1678 | ||
1679 | /* | |
1680 | * If restricted, initialize IO parameters as encoded in @cmd. | |
1681 | * RETRY from server is not allowed. | |
1682 | */ | |
1683 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
1684 | struct iovec *iov = page_address(iov_page); | |
1685 | ||
c9f0523d | 1686 | iov->iov_base = (void __user *)arg; |
59efec7b TH |
1687 | iov->iov_len = _IOC_SIZE(cmd); |
1688 | ||
1689 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
1690 | in_iov = iov; | |
1691 | in_iovs = 1; | |
1692 | } | |
1693 | ||
1694 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
1695 | out_iov = iov; | |
1696 | out_iovs = 1; | |
1697 | } | |
1698 | } | |
1699 | ||
1700 | retry: | |
1701 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
1702 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
1703 | ||
1704 | /* | |
1705 | * Out data can be used either for actual out data or iovs, | |
1706 | * make sure there always is at least one page. | |
1707 | */ | |
1708 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
1709 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
1710 | ||
1711 | /* make sure there are enough buffer pages and init request with them */ | |
1712 | err = -ENOMEM; | |
1713 | if (max_pages > FUSE_MAX_PAGES_PER_REQ) | |
1714 | goto out; | |
1715 | while (num_pages < max_pages) { | |
1716 | pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
1717 | if (!pages[num_pages]) | |
1718 | goto out; | |
1719 | num_pages++; | |
1720 | } | |
1721 | ||
1722 | req = fuse_get_req(fc); | |
1723 | if (IS_ERR(req)) { | |
1724 | err = PTR_ERR(req); | |
1725 | req = NULL; | |
1726 | goto out; | |
1727 | } | |
1728 | memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages); | |
1729 | req->num_pages = num_pages; | |
1730 | ||
1731 | /* okay, let's send it to the client */ | |
1732 | req->in.h.opcode = FUSE_IOCTL; | |
d36f2487 | 1733 | req->in.h.nodeid = ff->nodeid; |
59efec7b TH |
1734 | req->in.numargs = 1; |
1735 | req->in.args[0].size = sizeof(inarg); | |
1736 | req->in.args[0].value = &inarg; | |
1737 | if (in_size) { | |
1738 | req->in.numargs++; | |
1739 | req->in.args[1].size = in_size; | |
1740 | req->in.argpages = 1; | |
1741 | ||
1742 | err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size, | |
1743 | false); | |
1744 | if (err) | |
1745 | goto out; | |
1746 | } | |
1747 | ||
1748 | req->out.numargs = 2; | |
1749 | req->out.args[0].size = sizeof(outarg); | |
1750 | req->out.args[0].value = &outarg; | |
1751 | req->out.args[1].size = out_size; | |
1752 | req->out.argpages = 1; | |
1753 | req->out.argvar = 1; | |
1754 | ||
b93f858a | 1755 | fuse_request_send(fc, req); |
59efec7b TH |
1756 | err = req->out.h.error; |
1757 | transferred = req->out.args[1].size; | |
1758 | fuse_put_request(fc, req); | |
1759 | req = NULL; | |
1760 | if (err) | |
1761 | goto out; | |
1762 | ||
1763 | /* did it ask for retry? */ | |
1764 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
1765 | char *vaddr; | |
1766 | ||
1767 | /* no retry if in restricted mode */ | |
1768 | err = -EIO; | |
1769 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
1770 | goto out; | |
1771 | ||
1772 | in_iovs = outarg.in_iovs; | |
1773 | out_iovs = outarg.out_iovs; | |
1774 | ||
1775 | /* | |
1776 | * Make sure things are in boundary, separate checks | |
1777 | * are to protect against overflow. | |
1778 | */ | |
1779 | err = -ENOMEM; | |
1780 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
1781 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
1782 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
1783 | goto out; | |
1784 | ||
1785 | err = -EIO; | |
1786 | if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred) | |
1787 | goto out; | |
1788 | ||
1789 | /* okay, copy in iovs and retry */ | |
1790 | vaddr = kmap_atomic(pages[0], KM_USER0); | |
1791 | memcpy(page_address(iov_page), vaddr, transferred); | |
1792 | kunmap_atomic(vaddr, KM_USER0); | |
1793 | ||
1794 | in_iov = page_address(iov_page); | |
1795 | out_iov = in_iov + in_iovs; | |
1796 | ||
1797 | goto retry; | |
1798 | } | |
1799 | ||
1800 | err = -EIO; | |
1801 | if (transferred > inarg.out_size) | |
1802 | goto out; | |
1803 | ||
1804 | err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true); | |
1805 | out: | |
1806 | if (req) | |
1807 | fuse_put_request(fc, req); | |
1808 | if (iov_page) | |
1809 | __free_page(iov_page); | |
1810 | while (num_pages) | |
1811 | __free_page(pages[--num_pages]); | |
1812 | kfree(pages); | |
1813 | ||
1814 | return err ? err : outarg.result; | |
1815 | } | |
1816 | ||
d36f2487 MS |
1817 | static long fuse_file_ioctl_common(struct file *file, unsigned int cmd, |
1818 | unsigned long arg, unsigned int flags) | |
1819 | { | |
1820 | struct inode *inode = file->f_dentry->d_inode; | |
1821 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1822 | ||
1823 | if (!fuse_allow_task(fc, current)) | |
1824 | return -EACCES; | |
1825 | ||
1826 | if (is_bad_inode(inode)) | |
1827 | return -EIO; | |
1828 | ||
1829 | return fuse_do_ioctl(file, cmd, arg, flags); | |
1830 | } | |
1831 | ||
59efec7b TH |
1832 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, |
1833 | unsigned long arg) | |
1834 | { | |
d36f2487 | 1835 | return fuse_file_ioctl_common(file, cmd, arg, 0); |
59efec7b TH |
1836 | } |
1837 | ||
1838 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
1839 | unsigned long arg) | |
1840 | { | |
d36f2487 | 1841 | return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); |
59efec7b TH |
1842 | } |
1843 | ||
95668a69 TH |
1844 | /* |
1845 | * All files which have been polled are linked to RB tree | |
1846 | * fuse_conn->polled_files which is indexed by kh. Walk the tree and | |
1847 | * find the matching one. | |
1848 | */ | |
1849 | static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, | |
1850 | struct rb_node **parent_out) | |
1851 | { | |
1852 | struct rb_node **link = &fc->polled_files.rb_node; | |
1853 | struct rb_node *last = NULL; | |
1854 | ||
1855 | while (*link) { | |
1856 | struct fuse_file *ff; | |
1857 | ||
1858 | last = *link; | |
1859 | ff = rb_entry(last, struct fuse_file, polled_node); | |
1860 | ||
1861 | if (kh < ff->kh) | |
1862 | link = &last->rb_left; | |
1863 | else if (kh > ff->kh) | |
1864 | link = &last->rb_right; | |
1865 | else | |
1866 | return link; | |
1867 | } | |
1868 | ||
1869 | if (parent_out) | |
1870 | *parent_out = last; | |
1871 | return link; | |
1872 | } | |
1873 | ||
1874 | /* | |
1875 | * The file is about to be polled. Make sure it's on the polled_files | |
1876 | * RB tree. Note that files once added to the polled_files tree are | |
1877 | * not removed before the file is released. This is because a file | |
1878 | * polled once is likely to be polled again. | |
1879 | */ | |
1880 | static void fuse_register_polled_file(struct fuse_conn *fc, | |
1881 | struct fuse_file *ff) | |
1882 | { | |
1883 | spin_lock(&fc->lock); | |
1884 | if (RB_EMPTY_NODE(&ff->polled_node)) { | |
1885 | struct rb_node **link, *parent; | |
1886 | ||
1887 | link = fuse_find_polled_node(fc, ff->kh, &parent); | |
1888 | BUG_ON(*link); | |
1889 | rb_link_node(&ff->polled_node, parent, link); | |
1890 | rb_insert_color(&ff->polled_node, &fc->polled_files); | |
1891 | } | |
1892 | spin_unlock(&fc->lock); | |
1893 | } | |
1894 | ||
1895 | static unsigned fuse_file_poll(struct file *file, poll_table *wait) | |
1896 | { | |
95668a69 | 1897 | struct fuse_file *ff = file->private_data; |
797759aa | 1898 | struct fuse_conn *fc = ff->fc; |
95668a69 TH |
1899 | struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; |
1900 | struct fuse_poll_out outarg; | |
1901 | struct fuse_req *req; | |
1902 | int err; | |
1903 | ||
1904 | if (fc->no_poll) | |
1905 | return DEFAULT_POLLMASK; | |
1906 | ||
1907 | poll_wait(file, &ff->poll_wait, wait); | |
1908 | ||
1909 | /* | |
1910 | * Ask for notification iff there's someone waiting for it. | |
1911 | * The client may ignore the flag and always notify. | |
1912 | */ | |
1913 | if (waitqueue_active(&ff->poll_wait)) { | |
1914 | inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; | |
1915 | fuse_register_polled_file(fc, ff); | |
1916 | } | |
1917 | ||
1918 | req = fuse_get_req(fc); | |
1919 | if (IS_ERR(req)) | |
1920 | return PTR_ERR(req); | |
1921 | ||
1922 | req->in.h.opcode = FUSE_POLL; | |
797759aa | 1923 | req->in.h.nodeid = ff->nodeid; |
95668a69 TH |
1924 | req->in.numargs = 1; |
1925 | req->in.args[0].size = sizeof(inarg); | |
1926 | req->in.args[0].value = &inarg; | |
1927 | req->out.numargs = 1; | |
1928 | req->out.args[0].size = sizeof(outarg); | |
1929 | req->out.args[0].value = &outarg; | |
b93f858a | 1930 | fuse_request_send(fc, req); |
95668a69 TH |
1931 | err = req->out.h.error; |
1932 | fuse_put_request(fc, req); | |
1933 | ||
1934 | if (!err) | |
1935 | return outarg.revents; | |
1936 | if (err == -ENOSYS) { | |
1937 | fc->no_poll = 1; | |
1938 | return DEFAULT_POLLMASK; | |
1939 | } | |
1940 | return POLLERR; | |
1941 | } | |
1942 | ||
1943 | /* | |
1944 | * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and | |
1945 | * wakes up the poll waiters. | |
1946 | */ | |
1947 | int fuse_notify_poll_wakeup(struct fuse_conn *fc, | |
1948 | struct fuse_notify_poll_wakeup_out *outarg) | |
1949 | { | |
1950 | u64 kh = outarg->kh; | |
1951 | struct rb_node **link; | |
1952 | ||
1953 | spin_lock(&fc->lock); | |
1954 | ||
1955 | link = fuse_find_polled_node(fc, kh, NULL); | |
1956 | if (*link) { | |
1957 | struct fuse_file *ff; | |
1958 | ||
1959 | ff = rb_entry(*link, struct fuse_file, polled_node); | |
1960 | wake_up_interruptible_sync(&ff->poll_wait); | |
1961 | } | |
1962 | ||
1963 | spin_unlock(&fc->lock); | |
1964 | return 0; | |
1965 | } | |
1966 | ||
4b6f5d20 | 1967 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 1968 | .llseek = fuse_file_llseek, |
543ade1f | 1969 | .read = do_sync_read, |
bcb4be80 | 1970 | .aio_read = fuse_file_aio_read, |
543ade1f | 1971 | .write = do_sync_write, |
ea9b9907 | 1972 | .aio_write = fuse_file_aio_write, |
b6aeaded MS |
1973 | .mmap = fuse_file_mmap, |
1974 | .open = fuse_open, | |
1975 | .flush = fuse_flush, | |
1976 | .release = fuse_release, | |
1977 | .fsync = fuse_fsync, | |
71421259 | 1978 | .lock = fuse_file_lock, |
a9ff4f87 | 1979 | .flock = fuse_file_flock, |
5ffc4ef4 | 1980 | .splice_read = generic_file_splice_read, |
59efec7b TH |
1981 | .unlocked_ioctl = fuse_file_ioctl, |
1982 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 1983 | .poll = fuse_file_poll, |
b6aeaded MS |
1984 | }; |
1985 | ||
4b6f5d20 | 1986 | static const struct file_operations fuse_direct_io_file_operations = { |
5559b8f4 | 1987 | .llseek = fuse_file_llseek, |
413ef8cb MS |
1988 | .read = fuse_direct_read, |
1989 | .write = fuse_direct_write, | |
fc280c96 | 1990 | .mmap = fuse_direct_mmap, |
413ef8cb MS |
1991 | .open = fuse_open, |
1992 | .flush = fuse_flush, | |
1993 | .release = fuse_release, | |
1994 | .fsync = fuse_fsync, | |
71421259 | 1995 | .lock = fuse_file_lock, |
a9ff4f87 | 1996 | .flock = fuse_file_flock, |
59efec7b TH |
1997 | .unlocked_ioctl = fuse_file_ioctl, |
1998 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 1999 | .poll = fuse_file_poll, |
fc280c96 | 2000 | /* no splice_read */ |
413ef8cb MS |
2001 | }; |
2002 | ||
f5e54d6e | 2003 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 2004 | .readpage = fuse_readpage, |
3be5a52b MS |
2005 | .writepage = fuse_writepage, |
2006 | .launder_page = fuse_launder_page, | |
5e6f58a1 NP |
2007 | .write_begin = fuse_write_begin, |
2008 | .write_end = fuse_write_end, | |
db50b96c | 2009 | .readpages = fuse_readpages, |
3be5a52b | 2010 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 2011 | .bmap = fuse_bmap, |
b6aeaded MS |
2012 | }; |
2013 | ||
2014 | void fuse_init_file_inode(struct inode *inode) | |
2015 | { | |
45323fb7 MS |
2016 | inode->i_fop = &fuse_file_operations; |
2017 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 2018 | } |