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