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> |
08cbf542 | 15 | #include <linux/module.h> |
d9d318d3 | 16 | #include <linux/compat.h> |
478e0841 | 17 | #include <linux/swap.h> |
a27bb332 | 18 | #include <linux/aio.h> |
3634a632 | 19 | #include <linux/falloc.h> |
b6aeaded | 20 | |
4b6f5d20 | 21 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 22 | |
91fe96b4 MS |
23 | static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
24 | int opcode, struct fuse_open_out *outargp) | |
b6aeaded | 25 | { |
b6aeaded | 26 | struct fuse_open_in inarg; |
fd72faac MS |
27 | struct fuse_req *req; |
28 | int err; | |
29 | ||
b111c8c0 | 30 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
31 | if (IS_ERR(req)) |
32 | return PTR_ERR(req); | |
fd72faac MS |
33 | |
34 | memset(&inarg, 0, sizeof(inarg)); | |
6ff958ed MS |
35 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
36 | if (!fc->atomic_o_trunc) | |
37 | inarg.flags &= ~O_TRUNC; | |
91fe96b4 MS |
38 | req->in.h.opcode = opcode; |
39 | req->in.h.nodeid = nodeid; | |
fd72faac MS |
40 | req->in.numargs = 1; |
41 | req->in.args[0].size = sizeof(inarg); | |
42 | req->in.args[0].value = &inarg; | |
43 | req->out.numargs = 1; | |
44 | req->out.args[0].size = sizeof(*outargp); | |
45 | req->out.args[0].value = outargp; | |
b93f858a | 46 | fuse_request_send(fc, req); |
fd72faac MS |
47 | err = req->out.h.error; |
48 | fuse_put_request(fc, req); | |
49 | ||
50 | return err; | |
51 | } | |
52 | ||
acf99433 | 53 | struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) |
fd72faac MS |
54 | { |
55 | struct fuse_file *ff; | |
6b2db28a | 56 | |
c2ace304 | 57 | ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL); |
6b2db28a TH |
58 | if (unlikely(!ff)) |
59 | return NULL; | |
60 | ||
da5e4714 | 61 | ff->fc = fc; |
4250c066 | 62 | ff->reserved_req = fuse_request_alloc(0); |
6b2db28a TH |
63 | if (unlikely(!ff->reserved_req)) { |
64 | kfree(ff); | |
65 | return NULL; | |
fd72faac | 66 | } |
6b2db28a TH |
67 | |
68 | INIT_LIST_HEAD(&ff->write_entry); | |
69 | atomic_set(&ff->count, 0); | |
70 | RB_CLEAR_NODE(&ff->polled_node); | |
71 | init_waitqueue_head(&ff->poll_wait); | |
72 | ||
73 | spin_lock(&fc->lock); | |
74 | ff->kh = ++fc->khctr; | |
75 | spin_unlock(&fc->lock); | |
76 | ||
fd72faac MS |
77 | return ff; |
78 | } | |
79 | ||
80 | void fuse_file_free(struct fuse_file *ff) | |
81 | { | |
33649c91 | 82 | fuse_request_free(ff->reserved_req); |
fd72faac MS |
83 | kfree(ff); |
84 | } | |
85 | ||
c7b7143c | 86 | struct fuse_file *fuse_file_get(struct fuse_file *ff) |
c756e0a4 MS |
87 | { |
88 | atomic_inc(&ff->count); | |
89 | return ff; | |
90 | } | |
91 | ||
5a18ec17 MS |
92 | static void fuse_release_async(struct work_struct *work) |
93 | { | |
94 | struct fuse_req *req; | |
95 | struct fuse_conn *fc; | |
96 | struct path path; | |
97 | ||
98 | req = container_of(work, struct fuse_req, misc.release.work); | |
99 | path = req->misc.release.path; | |
100 | fc = get_fuse_conn(path.dentry->d_inode); | |
101 | ||
102 | fuse_put_request(fc, req); | |
103 | path_put(&path); | |
104 | } | |
105 | ||
819c4b3b MS |
106 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
107 | { | |
5a18ec17 MS |
108 | if (fc->destroy_req) { |
109 | /* | |
110 | * If this is a fuseblk mount, then it's possible that | |
111 | * releasing the path will result in releasing the | |
112 | * super block and sending the DESTROY request. If | |
113 | * the server is single threaded, this would hang. | |
114 | * For this reason do the path_put() in a separate | |
115 | * thread. | |
116 | */ | |
117 | atomic_inc(&req->count); | |
118 | INIT_WORK(&req->misc.release.work, fuse_release_async); | |
119 | schedule_work(&req->misc.release.work); | |
120 | } else { | |
121 | path_put(&req->misc.release.path); | |
122 | } | |
819c4b3b MS |
123 | } |
124 | ||
5a18ec17 | 125 | static void fuse_file_put(struct fuse_file *ff, bool sync) |
c756e0a4 MS |
126 | { |
127 | if (atomic_dec_and_test(&ff->count)) { | |
128 | struct fuse_req *req = ff->reserved_req; | |
8b0797a4 | 129 | |
5a18ec17 | 130 | if (sync) { |
5beea857 | 131 | req->force = 1; |
8b41e671 | 132 | req->background = 0; |
5a18ec17 MS |
133 | fuse_request_send(ff->fc, req); |
134 | path_put(&req->misc.release.path); | |
135 | fuse_put_request(ff->fc, req); | |
136 | } else { | |
137 | req->end = fuse_release_end; | |
8b41e671 | 138 | req->background = 1; |
5a18ec17 MS |
139 | fuse_request_send_background(ff->fc, req); |
140 | } | |
c756e0a4 MS |
141 | kfree(ff); |
142 | } | |
143 | } | |
144 | ||
08cbf542 TH |
145 | int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
146 | bool isdir) | |
91fe96b4 MS |
147 | { |
148 | struct fuse_open_out outarg; | |
149 | struct fuse_file *ff; | |
150 | int err; | |
151 | int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; | |
152 | ||
153 | ff = fuse_file_alloc(fc); | |
154 | if (!ff) | |
155 | return -ENOMEM; | |
156 | ||
157 | err = fuse_send_open(fc, nodeid, file, opcode, &outarg); | |
158 | if (err) { | |
159 | fuse_file_free(ff); | |
160 | return err; | |
161 | } | |
162 | ||
163 | if (isdir) | |
164 | outarg.open_flags &= ~FOPEN_DIRECT_IO; | |
165 | ||
166 | ff->fh = outarg.fh; | |
167 | ff->nodeid = nodeid; | |
168 | ff->open_flags = outarg.open_flags; | |
169 | file->private_data = fuse_file_get(ff); | |
170 | ||
171 | return 0; | |
172 | } | |
08cbf542 | 173 | EXPORT_SYMBOL_GPL(fuse_do_open); |
91fe96b4 | 174 | |
c7b7143c | 175 | void fuse_finish_open(struct inode *inode, struct file *file) |
fd72faac | 176 | { |
c7b7143c | 177 | struct fuse_file *ff = file->private_data; |
a0822c55 | 178 | struct fuse_conn *fc = get_fuse_conn(inode); |
c7b7143c MS |
179 | |
180 | if (ff->open_flags & FOPEN_DIRECT_IO) | |
fd72faac | 181 | file->f_op = &fuse_direct_io_file_operations; |
c7b7143c | 182 | if (!(ff->open_flags & FOPEN_KEEP_CACHE)) |
b1009979 | 183 | invalidate_inode_pages2(inode->i_mapping); |
c7b7143c | 184 | if (ff->open_flags & FOPEN_NONSEEKABLE) |
a7c1b990 | 185 | nonseekable_open(inode, file); |
a0822c55 KS |
186 | if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) { |
187 | struct fuse_inode *fi = get_fuse_inode(inode); | |
188 | ||
189 | spin_lock(&fc->lock); | |
190 | fi->attr_version = ++fc->attr_version; | |
191 | i_size_write(inode, 0); | |
192 | spin_unlock(&fc->lock); | |
193 | fuse_invalidate_attr(inode); | |
194 | } | |
fd72faac MS |
195 | } |
196 | ||
91fe96b4 | 197 | int fuse_open_common(struct inode *inode, struct file *file, bool isdir) |
fd72faac | 198 | { |
acf99433 | 199 | struct fuse_conn *fc = get_fuse_conn(inode); |
b6aeaded | 200 | int err; |
b6aeaded MS |
201 | |
202 | err = generic_file_open(inode, file); | |
203 | if (err) | |
204 | return err; | |
205 | ||
91fe96b4 | 206 | err = fuse_do_open(fc, get_node_id(inode), file, isdir); |
fd72faac | 207 | if (err) |
91fe96b4 | 208 | return err; |
b6aeaded | 209 | |
91fe96b4 MS |
210 | fuse_finish_open(inode, file); |
211 | ||
212 | return 0; | |
b6aeaded MS |
213 | } |
214 | ||
8b0797a4 | 215 | static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode) |
64c6d8ed | 216 | { |
8b0797a4 | 217 | struct fuse_conn *fc = ff->fc; |
33649c91 | 218 | struct fuse_req *req = ff->reserved_req; |
b57d4264 | 219 | struct fuse_release_in *inarg = &req->misc.release.in; |
b6aeaded | 220 | |
8b0797a4 MS |
221 | spin_lock(&fc->lock); |
222 | list_del(&ff->write_entry); | |
223 | if (!RB_EMPTY_NODE(&ff->polled_node)) | |
224 | rb_erase(&ff->polled_node, &fc->polled_files); | |
225 | spin_unlock(&fc->lock); | |
226 | ||
357ccf2b | 227 | wake_up_interruptible_all(&ff->poll_wait); |
8b0797a4 | 228 | |
b6aeaded | 229 | inarg->fh = ff->fh; |
fd72faac | 230 | inarg->flags = flags; |
51eb01e7 | 231 | req->in.h.opcode = opcode; |
c7b7143c | 232 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
233 | req->in.numargs = 1; |
234 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
235 | req->in.args[0].value = inarg; | |
fd72faac MS |
236 | } |
237 | ||
8b0797a4 | 238 | void fuse_release_common(struct file *file, int opcode) |
fd72faac | 239 | { |
6b2db28a TH |
240 | struct fuse_file *ff; |
241 | struct fuse_req *req; | |
b6aeaded | 242 | |
6b2db28a TH |
243 | ff = file->private_data; |
244 | if (unlikely(!ff)) | |
8b0797a4 | 245 | return; |
6b2db28a | 246 | |
6b2db28a | 247 | req = ff->reserved_req; |
8b0797a4 | 248 | fuse_prepare_release(ff, file->f_flags, opcode); |
6b2db28a | 249 | |
37fb3a30 MS |
250 | if (ff->flock) { |
251 | struct fuse_release_in *inarg = &req->misc.release.in; | |
252 | inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; | |
253 | inarg->lock_owner = fuse_lock_owner_id(ff->fc, | |
254 | (fl_owner_t) file); | |
255 | } | |
6b2db28a | 256 | /* Hold vfsmount and dentry until release is finished */ |
b0be46eb MS |
257 | path_get(&file->f_path); |
258 | req->misc.release.path = file->f_path; | |
6b2db28a | 259 | |
6b2db28a TH |
260 | /* |
261 | * Normally this will send the RELEASE request, however if | |
262 | * some asynchronous READ or WRITE requests are outstanding, | |
263 | * the sending will be delayed. | |
5a18ec17 MS |
264 | * |
265 | * Make the release synchronous if this is a fuseblk mount, | |
266 | * synchronous RELEASE is allowed (and desirable) in this case | |
267 | * because the server can be trusted not to screw up. | |
6b2db28a | 268 | */ |
5a18ec17 | 269 | fuse_file_put(ff, ff->fc->destroy_req != NULL); |
b6aeaded MS |
270 | } |
271 | ||
04730fef MS |
272 | static int fuse_open(struct inode *inode, struct file *file) |
273 | { | |
91fe96b4 | 274 | return fuse_open_common(inode, file, false); |
04730fef MS |
275 | } |
276 | ||
277 | static int fuse_release(struct inode *inode, struct file *file) | |
278 | { | |
8b0797a4 MS |
279 | fuse_release_common(file, FUSE_RELEASE); |
280 | ||
281 | /* return value is ignored by VFS */ | |
282 | return 0; | |
283 | } | |
284 | ||
285 | void fuse_sync_release(struct fuse_file *ff, int flags) | |
286 | { | |
287 | WARN_ON(atomic_read(&ff->count) > 1); | |
288 | fuse_prepare_release(ff, flags, FUSE_RELEASE); | |
289 | ff->reserved_req->force = 1; | |
8b41e671 | 290 | ff->reserved_req->background = 0; |
8b0797a4 MS |
291 | fuse_request_send(ff->fc, ff->reserved_req); |
292 | fuse_put_request(ff->fc, ff->reserved_req); | |
293 | kfree(ff); | |
04730fef | 294 | } |
08cbf542 | 295 | EXPORT_SYMBOL_GPL(fuse_sync_release); |
04730fef | 296 | |
71421259 | 297 | /* |
9c8ef561 MS |
298 | * Scramble the ID space with XTEA, so that the value of the files_struct |
299 | * pointer is not exposed to userspace. | |
71421259 | 300 | */ |
f3332114 | 301 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 302 | { |
9c8ef561 MS |
303 | u32 *k = fc->scramble_key; |
304 | u64 v = (unsigned long) id; | |
305 | u32 v0 = v; | |
306 | u32 v1 = v >> 32; | |
307 | u32 sum = 0; | |
308 | int i; | |
309 | ||
310 | for (i = 0; i < 32; i++) { | |
311 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
312 | sum += 0x9E3779B9; | |
313 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
314 | } | |
315 | ||
316 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
317 | } |
318 | ||
3be5a52b MS |
319 | /* |
320 | * Check if page is under writeback | |
321 | * | |
322 | * This is currently done by walking the list of writepage requests | |
323 | * for the inode, which can be pretty inefficient. | |
324 | */ | |
325 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) | |
326 | { | |
327 | struct fuse_conn *fc = get_fuse_conn(inode); | |
328 | struct fuse_inode *fi = get_fuse_inode(inode); | |
329 | struct fuse_req *req; | |
330 | bool found = false; | |
331 | ||
332 | spin_lock(&fc->lock); | |
333 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | |
334 | pgoff_t curr_index; | |
335 | ||
336 | BUG_ON(req->inode != inode); | |
337 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; | |
338 | if (curr_index == index) { | |
339 | found = true; | |
340 | break; | |
341 | } | |
342 | } | |
343 | spin_unlock(&fc->lock); | |
344 | ||
345 | return found; | |
346 | } | |
347 | ||
348 | /* | |
349 | * Wait for page writeback to be completed. | |
350 | * | |
351 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
352 | * use some other means. | |
353 | */ | |
354 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) | |
355 | { | |
356 | struct fuse_inode *fi = get_fuse_inode(inode); | |
357 | ||
358 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
359 | return 0; | |
360 | } | |
361 | ||
75e1fcc0 | 362 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 363 | { |
6131ffaa | 364 | struct inode *inode = file_inode(file); |
b6aeaded MS |
365 | struct fuse_conn *fc = get_fuse_conn(inode); |
366 | struct fuse_file *ff = file->private_data; | |
367 | struct fuse_req *req; | |
368 | struct fuse_flush_in inarg; | |
369 | int err; | |
370 | ||
248d86e8 MS |
371 | if (is_bad_inode(inode)) |
372 | return -EIO; | |
373 | ||
b6aeaded MS |
374 | if (fc->no_flush) |
375 | return 0; | |
376 | ||
b111c8c0 | 377 | req = fuse_get_req_nofail_nopages(fc, file); |
b6aeaded MS |
378 | memset(&inarg, 0, sizeof(inarg)); |
379 | inarg.fh = ff->fh; | |
9c8ef561 | 380 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
b6aeaded MS |
381 | req->in.h.opcode = FUSE_FLUSH; |
382 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
383 | req->in.numargs = 1; |
384 | req->in.args[0].size = sizeof(inarg); | |
385 | req->in.args[0].value = &inarg; | |
71421259 | 386 | req->force = 1; |
b93f858a | 387 | fuse_request_send(fc, req); |
b6aeaded MS |
388 | err = req->out.h.error; |
389 | fuse_put_request(fc, req); | |
390 | if (err == -ENOSYS) { | |
391 | fc->no_flush = 1; | |
392 | err = 0; | |
393 | } | |
394 | return err; | |
395 | } | |
396 | ||
3be5a52b MS |
397 | /* |
398 | * Wait for all pending writepages on the inode to finish. | |
399 | * | |
400 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
401 | * and waiting for all sent writes to complete. | |
402 | * | |
403 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
404 | * could conflict with truncation. | |
405 | */ | |
406 | static void fuse_sync_writes(struct inode *inode) | |
407 | { | |
408 | fuse_set_nowrite(inode); | |
409 | fuse_release_nowrite(inode); | |
410 | } | |
411 | ||
02c24a82 JB |
412 | int fuse_fsync_common(struct file *file, loff_t start, loff_t end, |
413 | int datasync, int isdir) | |
b6aeaded | 414 | { |
7ea80859 | 415 | struct inode *inode = file->f_mapping->host; |
b6aeaded MS |
416 | struct fuse_conn *fc = get_fuse_conn(inode); |
417 | struct fuse_file *ff = file->private_data; | |
418 | struct fuse_req *req; | |
419 | struct fuse_fsync_in inarg; | |
420 | int err; | |
421 | ||
248d86e8 MS |
422 | if (is_bad_inode(inode)) |
423 | return -EIO; | |
424 | ||
02c24a82 JB |
425 | err = filemap_write_and_wait_range(inode->i_mapping, start, end); |
426 | if (err) | |
427 | return err; | |
428 | ||
82547981 | 429 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
430 | return 0; |
431 | ||
02c24a82 JB |
432 | mutex_lock(&inode->i_mutex); |
433 | ||
3be5a52b MS |
434 | /* |
435 | * Start writeback against all dirty pages of the inode, then | |
436 | * wait for all outstanding writes, before sending the FSYNC | |
437 | * request. | |
438 | */ | |
439 | err = write_inode_now(inode, 0); | |
440 | if (err) | |
02c24a82 | 441 | goto out; |
3be5a52b MS |
442 | |
443 | fuse_sync_writes(inode); | |
444 | ||
b111c8c0 | 445 | req = fuse_get_req_nopages(fc); |
02c24a82 JB |
446 | if (IS_ERR(req)) { |
447 | err = PTR_ERR(req); | |
448 | goto out; | |
449 | } | |
b6aeaded MS |
450 | |
451 | memset(&inarg, 0, sizeof(inarg)); | |
452 | inarg.fh = ff->fh; | |
453 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 454 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 455 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
456 | req->in.numargs = 1; |
457 | req->in.args[0].size = sizeof(inarg); | |
458 | req->in.args[0].value = &inarg; | |
b93f858a | 459 | fuse_request_send(fc, req); |
b6aeaded MS |
460 | err = req->out.h.error; |
461 | fuse_put_request(fc, req); | |
462 | if (err == -ENOSYS) { | |
82547981 MS |
463 | if (isdir) |
464 | fc->no_fsyncdir = 1; | |
465 | else | |
466 | fc->no_fsync = 1; | |
b6aeaded MS |
467 | err = 0; |
468 | } | |
02c24a82 JB |
469 | out: |
470 | mutex_unlock(&inode->i_mutex); | |
b6aeaded MS |
471 | return err; |
472 | } | |
473 | ||
02c24a82 JB |
474 | static int fuse_fsync(struct file *file, loff_t start, loff_t end, |
475 | int datasync) | |
82547981 | 476 | { |
02c24a82 | 477 | return fuse_fsync_common(file, start, end, datasync, 0); |
82547981 MS |
478 | } |
479 | ||
2106cb18 MS |
480 | void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos, |
481 | size_t count, int opcode) | |
b6aeaded | 482 | { |
5c5c5e51 | 483 | struct fuse_read_in *inarg = &req->misc.read.in; |
a6643094 | 484 | struct fuse_file *ff = file->private_data; |
b6aeaded | 485 | |
361b1eb5 MS |
486 | inarg->fh = ff->fh; |
487 | inarg->offset = pos; | |
488 | inarg->size = count; | |
a6643094 | 489 | inarg->flags = file->f_flags; |
361b1eb5 | 490 | req->in.h.opcode = opcode; |
2106cb18 | 491 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
492 | req->in.numargs = 1; |
493 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 494 | req->in.args[0].value = inarg; |
b6aeaded MS |
495 | req->out.argvar = 1; |
496 | req->out.numargs = 1; | |
497 | req->out.args[0].size = count; | |
b6aeaded MS |
498 | } |
499 | ||
187c5c36 MP |
500 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
501 | { | |
502 | unsigned i; | |
503 | ||
504 | for (i = 0; i < req->num_pages; i++) { | |
505 | struct page *page = req->pages[i]; | |
506 | if (write) | |
507 | set_page_dirty_lock(page); | |
508 | put_page(page); | |
509 | } | |
510 | } | |
511 | ||
01e9d11a MP |
512 | /** |
513 | * In case of short read, the caller sets 'pos' to the position of | |
514 | * actual end of fuse request in IO request. Otherwise, if bytes_requested | |
515 | * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1. | |
516 | * | |
517 | * An example: | |
518 | * User requested DIO read of 64K. It was splitted into two 32K fuse requests, | |
519 | * both submitted asynchronously. The first of them was ACKed by userspace as | |
520 | * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The | |
521 | * second request was ACKed as short, e.g. only 1K was read, resulting in | |
522 | * pos == 33K. | |
523 | * | |
524 | * Thus, when all fuse requests are completed, the minimal non-negative 'pos' | |
525 | * will be equal to the length of the longest contiguous fragment of | |
526 | * transferred data starting from the beginning of IO request. | |
527 | */ | |
528 | static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos) | |
529 | { | |
530 | int left; | |
531 | ||
532 | spin_lock(&io->lock); | |
533 | if (err) | |
534 | io->err = io->err ? : err; | |
535 | else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes)) | |
536 | io->bytes = pos; | |
537 | ||
538 | left = --io->reqs; | |
539 | spin_unlock(&io->lock); | |
540 | ||
541 | if (!left) { | |
542 | long res; | |
543 | ||
544 | if (io->err) | |
545 | res = io->err; | |
546 | else if (io->bytes >= 0 && io->write) | |
547 | res = -EIO; | |
548 | else { | |
549 | res = io->bytes < 0 ? io->size : io->bytes; | |
550 | ||
551 | if (!is_sync_kiocb(io->iocb)) { | |
552 | struct path *path = &io->iocb->ki_filp->f_path; | |
553 | struct inode *inode = path->dentry->d_inode; | |
554 | struct fuse_conn *fc = get_fuse_conn(inode); | |
555 | struct fuse_inode *fi = get_fuse_inode(inode); | |
556 | ||
557 | spin_lock(&fc->lock); | |
558 | fi->attr_version = ++fc->attr_version; | |
559 | spin_unlock(&fc->lock); | |
560 | } | |
561 | } | |
562 | ||
563 | aio_complete(io->iocb, res, 0); | |
564 | kfree(io); | |
565 | } | |
566 | } | |
567 | ||
568 | static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req) | |
569 | { | |
570 | struct fuse_io_priv *io = req->io; | |
571 | ssize_t pos = -1; | |
572 | ||
573 | fuse_release_user_pages(req, !io->write); | |
574 | ||
575 | if (io->write) { | |
576 | if (req->misc.write.in.size != req->misc.write.out.size) | |
577 | pos = req->misc.write.in.offset - io->offset + | |
578 | req->misc.write.out.size; | |
579 | } else { | |
580 | if (req->misc.read.in.size != req->out.args[0].size) | |
581 | pos = req->misc.read.in.offset - io->offset + | |
582 | req->out.args[0].size; | |
583 | } | |
584 | ||
585 | fuse_aio_complete(io, req->out.h.error, pos); | |
586 | } | |
587 | ||
588 | static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req, | |
589 | size_t num_bytes, struct fuse_io_priv *io) | |
590 | { | |
591 | spin_lock(&io->lock); | |
592 | io->size += num_bytes; | |
593 | io->reqs++; | |
594 | spin_unlock(&io->lock); | |
595 | ||
596 | req->io = io; | |
597 | req->end = fuse_aio_complete_req; | |
598 | ||
36cf66ed | 599 | __fuse_get_request(req); |
01e9d11a MP |
600 | fuse_request_send_background(fc, req); |
601 | ||
602 | return num_bytes; | |
603 | } | |
604 | ||
36cf66ed | 605 | static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io, |
2106cb18 | 606 | loff_t pos, size_t count, fl_owner_t owner) |
04730fef | 607 | { |
36cf66ed | 608 | struct file *file = io->file; |
2106cb18 MS |
609 | struct fuse_file *ff = file->private_data; |
610 | struct fuse_conn *fc = ff->fc; | |
f3332114 | 611 | |
2106cb18 | 612 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
f3332114 | 613 | if (owner != NULL) { |
5c5c5e51 | 614 | struct fuse_read_in *inarg = &req->misc.read.in; |
f3332114 MS |
615 | |
616 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | |
617 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
618 | } | |
36cf66ed MP |
619 | |
620 | if (io->async) | |
621 | return fuse_async_req_send(fc, req, count, io); | |
622 | ||
b93f858a | 623 | fuse_request_send(fc, req); |
361b1eb5 | 624 | return req->out.args[0].size; |
04730fef MS |
625 | } |
626 | ||
5c5c5e51 MS |
627 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
628 | u64 attr_ver) | |
629 | { | |
630 | struct fuse_conn *fc = get_fuse_conn(inode); | |
631 | struct fuse_inode *fi = get_fuse_inode(inode); | |
632 | ||
633 | spin_lock(&fc->lock); | |
4e208303 MP |
634 | if (attr_ver == fi->attr_version && size < inode->i_size && |
635 | !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { | |
5c5c5e51 MS |
636 | fi->attr_version = ++fc->attr_version; |
637 | i_size_write(inode, size); | |
638 | } | |
639 | spin_unlock(&fc->lock); | |
640 | } | |
641 | ||
b6aeaded MS |
642 | static int fuse_readpage(struct file *file, struct page *page) |
643 | { | |
36cf66ed | 644 | struct fuse_io_priv io = { .async = 0, .file = file }; |
b6aeaded MS |
645 | struct inode *inode = page->mapping->host; |
646 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 | 647 | struct fuse_req *req; |
5c5c5e51 MS |
648 | size_t num_read; |
649 | loff_t pos = page_offset(page); | |
650 | size_t count = PAGE_CACHE_SIZE; | |
651 | u64 attr_ver; | |
248d86e8 MS |
652 | int err; |
653 | ||
654 | err = -EIO; | |
655 | if (is_bad_inode(inode)) | |
656 | goto out; | |
657 | ||
3be5a52b | 658 | /* |
25985edc | 659 | * Page writeback can extend beyond the lifetime of the |
3be5a52b MS |
660 | * page-cache page, so make sure we read a properly synced |
661 | * page. | |
662 | */ | |
663 | fuse_wait_on_page_writeback(inode, page->index); | |
664 | ||
b111c8c0 | 665 | req = fuse_get_req(fc, 1); |
ce1d5a49 MS |
666 | err = PTR_ERR(req); |
667 | if (IS_ERR(req)) | |
b6aeaded MS |
668 | goto out; |
669 | ||
5c5c5e51 MS |
670 | attr_ver = fuse_get_attr_version(fc); |
671 | ||
b6aeaded | 672 | req->out.page_zeroing = 1; |
f4975c67 | 673 | req->out.argpages = 1; |
b6aeaded MS |
674 | req->num_pages = 1; |
675 | req->pages[0] = page; | |
85f40aec | 676 | req->page_descs[0].length = count; |
36cf66ed | 677 | num_read = fuse_send_read(req, &io, pos, count, NULL); |
b6aeaded MS |
678 | err = req->out.h.error; |
679 | fuse_put_request(fc, req); | |
5c5c5e51 MS |
680 | |
681 | if (!err) { | |
682 | /* | |
683 | * Short read means EOF. If file size is larger, truncate it | |
684 | */ | |
685 | if (num_read < count) | |
686 | fuse_read_update_size(inode, pos + num_read, attr_ver); | |
687 | ||
b6aeaded | 688 | SetPageUptodate(page); |
5c5c5e51 MS |
689 | } |
690 | ||
b36c31ba | 691 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
692 | out: |
693 | unlock_page(page); | |
694 | return err; | |
695 | } | |
696 | ||
c1aa96a5 | 697 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 698 | { |
c1aa96a5 | 699 | int i; |
5c5c5e51 MS |
700 | size_t count = req->misc.read.in.size; |
701 | size_t num_read = req->out.args[0].size; | |
ce534fb0 | 702 | struct address_space *mapping = NULL; |
c1aa96a5 | 703 | |
ce534fb0 MS |
704 | for (i = 0; mapping == NULL && i < req->num_pages; i++) |
705 | mapping = req->pages[i]->mapping; | |
5c5c5e51 | 706 | |
ce534fb0 MS |
707 | if (mapping) { |
708 | struct inode *inode = mapping->host; | |
709 | ||
710 | /* | |
711 | * Short read means EOF. If file size is larger, truncate it | |
712 | */ | |
713 | if (!req->out.h.error && num_read < count) { | |
714 | loff_t pos; | |
715 | ||
716 | pos = page_offset(req->pages[0]) + num_read; | |
717 | fuse_read_update_size(inode, pos, | |
718 | req->misc.read.attr_ver); | |
719 | } | |
720 | fuse_invalidate_attr(inode); /* atime changed */ | |
721 | } | |
c1aa96a5 | 722 | |
db50b96c MS |
723 | for (i = 0; i < req->num_pages; i++) { |
724 | struct page *page = req->pages[i]; | |
725 | if (!req->out.h.error) | |
726 | SetPageUptodate(page); | |
c1aa96a5 MS |
727 | else |
728 | SetPageError(page); | |
db50b96c | 729 | unlock_page(page); |
b5dd3285 | 730 | page_cache_release(page); |
db50b96c | 731 | } |
c756e0a4 | 732 | if (req->ff) |
5a18ec17 | 733 | fuse_file_put(req->ff, false); |
c1aa96a5 MS |
734 | } |
735 | ||
2106cb18 | 736 | static void fuse_send_readpages(struct fuse_req *req, struct file *file) |
c1aa96a5 | 737 | { |
2106cb18 MS |
738 | struct fuse_file *ff = file->private_data; |
739 | struct fuse_conn *fc = ff->fc; | |
c1aa96a5 MS |
740 | loff_t pos = page_offset(req->pages[0]); |
741 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
f4975c67 MS |
742 | |
743 | req->out.argpages = 1; | |
c1aa96a5 | 744 | req->out.page_zeroing = 1; |
ce534fb0 | 745 | req->out.page_replace = 1; |
2106cb18 | 746 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
5c5c5e51 | 747 | req->misc.read.attr_ver = fuse_get_attr_version(fc); |
9cd68455 | 748 | if (fc->async_read) { |
c756e0a4 | 749 | req->ff = fuse_file_get(ff); |
9cd68455 | 750 | req->end = fuse_readpages_end; |
b93f858a | 751 | fuse_request_send_background(fc, req); |
9cd68455 | 752 | } else { |
b93f858a | 753 | fuse_request_send(fc, req); |
9cd68455 | 754 | fuse_readpages_end(fc, req); |
e9bb09dd | 755 | fuse_put_request(fc, req); |
9cd68455 | 756 | } |
db50b96c MS |
757 | } |
758 | ||
c756e0a4 | 759 | struct fuse_fill_data { |
db50b96c | 760 | struct fuse_req *req; |
a6643094 | 761 | struct file *file; |
db50b96c | 762 | struct inode *inode; |
f8dbdf81 | 763 | unsigned nr_pages; |
db50b96c MS |
764 | }; |
765 | ||
766 | static int fuse_readpages_fill(void *_data, struct page *page) | |
767 | { | |
c756e0a4 | 768 | struct fuse_fill_data *data = _data; |
db50b96c MS |
769 | struct fuse_req *req = data->req; |
770 | struct inode *inode = data->inode; | |
771 | struct fuse_conn *fc = get_fuse_conn(inode); | |
772 | ||
3be5a52b MS |
773 | fuse_wait_on_page_writeback(inode, page->index); |
774 | ||
db50b96c MS |
775 | if (req->num_pages && |
776 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
777 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
778 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
f8dbdf81 MP |
779 | int nr_alloc = min_t(unsigned, data->nr_pages, |
780 | FUSE_MAX_PAGES_PER_REQ); | |
2106cb18 | 781 | fuse_send_readpages(req, data->file); |
8b41e671 MP |
782 | if (fc->async_read) |
783 | req = fuse_get_req_for_background(fc, nr_alloc); | |
784 | else | |
785 | req = fuse_get_req(fc, nr_alloc); | |
786 | ||
787 | data->req = req; | |
ce1d5a49 | 788 | if (IS_ERR(req)) { |
db50b96c | 789 | unlock_page(page); |
ce1d5a49 | 790 | return PTR_ERR(req); |
db50b96c | 791 | } |
db50b96c | 792 | } |
f8dbdf81 MP |
793 | |
794 | if (WARN_ON(req->num_pages >= req->max_pages)) { | |
795 | fuse_put_request(fc, req); | |
796 | return -EIO; | |
797 | } | |
798 | ||
b5dd3285 | 799 | page_cache_get(page); |
db50b96c | 800 | req->pages[req->num_pages] = page; |
85f40aec | 801 | req->page_descs[req->num_pages].length = PAGE_SIZE; |
1729a16c | 802 | req->num_pages++; |
f8dbdf81 | 803 | data->nr_pages--; |
db50b96c MS |
804 | return 0; |
805 | } | |
806 | ||
807 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
808 | struct list_head *pages, unsigned nr_pages) | |
809 | { | |
810 | struct inode *inode = mapping->host; | |
811 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c756e0a4 | 812 | struct fuse_fill_data data; |
db50b96c | 813 | int err; |
f8dbdf81 | 814 | int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ); |
248d86e8 | 815 | |
1d7ea732 | 816 | err = -EIO; |
248d86e8 | 817 | if (is_bad_inode(inode)) |
2e990021 | 818 | goto out; |
248d86e8 | 819 | |
a6643094 | 820 | data.file = file; |
db50b96c | 821 | data.inode = inode; |
8b41e671 MP |
822 | if (fc->async_read) |
823 | data.req = fuse_get_req_for_background(fc, nr_alloc); | |
824 | else | |
825 | data.req = fuse_get_req(fc, nr_alloc); | |
f8dbdf81 | 826 | data.nr_pages = nr_pages; |
1d7ea732 | 827 | err = PTR_ERR(data.req); |
ce1d5a49 | 828 | if (IS_ERR(data.req)) |
2e990021 | 829 | goto out; |
db50b96c MS |
830 | |
831 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
832 | if (!err) { |
833 | if (data.req->num_pages) | |
2106cb18 | 834 | fuse_send_readpages(data.req, file); |
d3406ffa MS |
835 | else |
836 | fuse_put_request(fc, data.req); | |
837 | } | |
2e990021 | 838 | out: |
1d7ea732 | 839 | return err; |
db50b96c MS |
840 | } |
841 | ||
bcb4be80 MS |
842 | static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
843 | unsigned long nr_segs, loff_t pos) | |
844 | { | |
845 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
a8894274 | 846 | struct fuse_conn *fc = get_fuse_conn(inode); |
bcb4be80 | 847 | |
a8894274 BF |
848 | /* |
849 | * In auto invalidate mode, always update attributes on read. | |
850 | * Otherwise, only update if we attempt to read past EOF (to ensure | |
851 | * i_size is up to date). | |
852 | */ | |
853 | if (fc->auto_inval_data || | |
854 | (pos + iov_length(iov, nr_segs) > i_size_read(inode))) { | |
bcb4be80 | 855 | int err; |
bcb4be80 MS |
856 | err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL); |
857 | if (err) | |
858 | return err; | |
859 | } | |
860 | ||
861 | return generic_file_aio_read(iocb, iov, nr_segs, pos); | |
862 | } | |
863 | ||
2d698b07 | 864 | static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff, |
2106cb18 | 865 | loff_t pos, size_t count) |
b6aeaded | 866 | { |
b25e82e5 MS |
867 | struct fuse_write_in *inarg = &req->misc.write.in; |
868 | struct fuse_write_out *outarg = &req->misc.write.out; | |
b6aeaded | 869 | |
b25e82e5 MS |
870 | inarg->fh = ff->fh; |
871 | inarg->offset = pos; | |
872 | inarg->size = count; | |
b6aeaded | 873 | req->in.h.opcode = FUSE_WRITE; |
2106cb18 | 874 | req->in.h.nodeid = ff->nodeid; |
b6aeaded | 875 | req->in.numargs = 2; |
2106cb18 | 876 | if (ff->fc->minor < 9) |
f3332114 MS |
877 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
878 | else | |
879 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
b25e82e5 | 880 | req->in.args[0].value = inarg; |
b6aeaded MS |
881 | req->in.args[1].size = count; |
882 | req->out.numargs = 1; | |
883 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
b25e82e5 MS |
884 | req->out.args[0].value = outarg; |
885 | } | |
886 | ||
36cf66ed | 887 | static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io, |
2106cb18 | 888 | loff_t pos, size_t count, fl_owner_t owner) |
b25e82e5 | 889 | { |
36cf66ed | 890 | struct file *file = io->file; |
2106cb18 MS |
891 | struct fuse_file *ff = file->private_data; |
892 | struct fuse_conn *fc = ff->fc; | |
2d698b07 MS |
893 | struct fuse_write_in *inarg = &req->misc.write.in; |
894 | ||
2106cb18 | 895 | fuse_write_fill(req, ff, pos, count); |
2d698b07 | 896 | inarg->flags = file->f_flags; |
f3332114 | 897 | if (owner != NULL) { |
f3332114 MS |
898 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
899 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
900 | } | |
36cf66ed MP |
901 | |
902 | if (io->async) | |
903 | return fuse_async_req_send(fc, req, count, io); | |
904 | ||
b93f858a | 905 | fuse_request_send(fc, req); |
b25e82e5 | 906 | return req->misc.write.out.size; |
b6aeaded MS |
907 | } |
908 | ||
a1d75f25 | 909 | void fuse_write_update_size(struct inode *inode, loff_t pos) |
854512ec MS |
910 | { |
911 | struct fuse_conn *fc = get_fuse_conn(inode); | |
912 | struct fuse_inode *fi = get_fuse_inode(inode); | |
913 | ||
914 | spin_lock(&fc->lock); | |
915 | fi->attr_version = ++fc->attr_version; | |
916 | if (pos > inode->i_size) | |
917 | i_size_write(inode, pos); | |
918 | spin_unlock(&fc->lock); | |
919 | } | |
920 | ||
ea9b9907 NP |
921 | static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file, |
922 | struct inode *inode, loff_t pos, | |
923 | size_t count) | |
924 | { | |
925 | size_t res; | |
926 | unsigned offset; | |
927 | unsigned i; | |
36cf66ed | 928 | struct fuse_io_priv io = { .async = 0, .file = file }; |
ea9b9907 NP |
929 | |
930 | for (i = 0; i < req->num_pages; i++) | |
931 | fuse_wait_on_page_writeback(inode, req->pages[i]->index); | |
932 | ||
36cf66ed | 933 | res = fuse_send_write(req, &io, pos, count, NULL); |
ea9b9907 | 934 | |
b2430d75 | 935 | offset = req->page_descs[0].offset; |
ea9b9907 NP |
936 | count = res; |
937 | for (i = 0; i < req->num_pages; i++) { | |
938 | struct page *page = req->pages[i]; | |
939 | ||
940 | if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE) | |
941 | SetPageUptodate(page); | |
942 | ||
943 | if (count > PAGE_CACHE_SIZE - offset) | |
944 | count -= PAGE_CACHE_SIZE - offset; | |
945 | else | |
946 | count = 0; | |
947 | offset = 0; | |
948 | ||
949 | unlock_page(page); | |
950 | page_cache_release(page); | |
951 | } | |
952 | ||
953 | return res; | |
954 | } | |
955 | ||
956 | static ssize_t fuse_fill_write_pages(struct fuse_req *req, | |
957 | struct address_space *mapping, | |
958 | struct iov_iter *ii, loff_t pos) | |
959 | { | |
960 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
961 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | |
962 | size_t count = 0; | |
963 | int err; | |
964 | ||
f4975c67 | 965 | req->in.argpages = 1; |
b2430d75 | 966 | req->page_descs[0].offset = offset; |
ea9b9907 NP |
967 | |
968 | do { | |
969 | size_t tmp; | |
970 | struct page *page; | |
971 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
972 | size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset, | |
973 | iov_iter_count(ii)); | |
974 | ||
975 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
976 | ||
977 | again: | |
978 | err = -EFAULT; | |
979 | if (iov_iter_fault_in_readable(ii, bytes)) | |
980 | break; | |
981 | ||
982 | err = -ENOMEM; | |
54566b2c | 983 | page = grab_cache_page_write_begin(mapping, index, 0); |
ea9b9907 NP |
984 | if (!page) |
985 | break; | |
986 | ||
931e80e4 | 987 | if (mapping_writably_mapped(mapping)) |
988 | flush_dcache_page(page); | |
989 | ||
ea9b9907 NP |
990 | pagefault_disable(); |
991 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); | |
992 | pagefault_enable(); | |
993 | flush_dcache_page(page); | |
994 | ||
478e0841 JW |
995 | mark_page_accessed(page); |
996 | ||
020ef191 | 997 | iov_iter_advance(ii, tmp); |
ea9b9907 NP |
998 | if (!tmp) { |
999 | unlock_page(page); | |
1000 | page_cache_release(page); | |
1001 | bytes = min(bytes, iov_iter_single_seg_count(ii)); | |
1002 | goto again; | |
1003 | } | |
1004 | ||
1005 | err = 0; | |
1006 | req->pages[req->num_pages] = page; | |
85f40aec | 1007 | req->page_descs[req->num_pages].length = tmp; |
ea9b9907 NP |
1008 | req->num_pages++; |
1009 | ||
ea9b9907 NP |
1010 | count += tmp; |
1011 | pos += tmp; | |
1012 | offset += tmp; | |
1013 | if (offset == PAGE_CACHE_SIZE) | |
1014 | offset = 0; | |
1015 | ||
78bb6cb9 MS |
1016 | if (!fc->big_writes) |
1017 | break; | |
ea9b9907 | 1018 | } while (iov_iter_count(ii) && count < fc->max_write && |
d07f09f5 | 1019 | req->num_pages < req->max_pages && offset == 0); |
ea9b9907 NP |
1020 | |
1021 | return count > 0 ? count : err; | |
1022 | } | |
1023 | ||
d07f09f5 MP |
1024 | static inline unsigned fuse_wr_pages(loff_t pos, size_t len) |
1025 | { | |
1026 | return min_t(unsigned, | |
1027 | ((pos + len - 1) >> PAGE_CACHE_SHIFT) - | |
1028 | (pos >> PAGE_CACHE_SHIFT) + 1, | |
1029 | FUSE_MAX_PAGES_PER_REQ); | |
1030 | } | |
1031 | ||
ea9b9907 NP |
1032 | static ssize_t fuse_perform_write(struct file *file, |
1033 | struct address_space *mapping, | |
1034 | struct iov_iter *ii, loff_t pos) | |
1035 | { | |
1036 | struct inode *inode = mapping->host; | |
1037 | struct fuse_conn *fc = get_fuse_conn(inode); | |
4e208303 | 1038 | struct fuse_inode *fi = get_fuse_inode(inode); |
ea9b9907 NP |
1039 | int err = 0; |
1040 | ssize_t res = 0; | |
1041 | ||
1042 | if (is_bad_inode(inode)) | |
1043 | return -EIO; | |
1044 | ||
4e208303 MP |
1045 | if (inode->i_size < pos + iov_iter_count(ii)) |
1046 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
1047 | ||
ea9b9907 NP |
1048 | do { |
1049 | struct fuse_req *req; | |
1050 | ssize_t count; | |
d07f09f5 | 1051 | unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii)); |
ea9b9907 | 1052 | |
d07f09f5 | 1053 | req = fuse_get_req(fc, nr_pages); |
ea9b9907 NP |
1054 | if (IS_ERR(req)) { |
1055 | err = PTR_ERR(req); | |
1056 | break; | |
1057 | } | |
1058 | ||
1059 | count = fuse_fill_write_pages(req, mapping, ii, pos); | |
1060 | if (count <= 0) { | |
1061 | err = count; | |
1062 | } else { | |
1063 | size_t num_written; | |
1064 | ||
1065 | num_written = fuse_send_write_pages(req, file, inode, | |
1066 | pos, count); | |
1067 | err = req->out.h.error; | |
1068 | if (!err) { | |
1069 | res += num_written; | |
1070 | pos += num_written; | |
1071 | ||
1072 | /* break out of the loop on short write */ | |
1073 | if (num_written != count) | |
1074 | err = -EIO; | |
1075 | } | |
1076 | } | |
1077 | fuse_put_request(fc, req); | |
1078 | } while (!err && iov_iter_count(ii)); | |
1079 | ||
1080 | if (res > 0) | |
1081 | fuse_write_update_size(inode, pos); | |
1082 | ||
4e208303 | 1083 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
ea9b9907 NP |
1084 | fuse_invalidate_attr(inode); |
1085 | ||
1086 | return res > 0 ? res : err; | |
1087 | } | |
1088 | ||
1089 | static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | |
1090 | unsigned long nr_segs, loff_t pos) | |
1091 | { | |
1092 | struct file *file = iocb->ki_filp; | |
1093 | struct address_space *mapping = file->f_mapping; | |
1094 | size_t count = 0; | |
4273b793 | 1095 | size_t ocount = 0; |
ea9b9907 | 1096 | ssize_t written = 0; |
4273b793 | 1097 | ssize_t written_buffered = 0; |
ea9b9907 NP |
1098 | struct inode *inode = mapping->host; |
1099 | ssize_t err; | |
1100 | struct iov_iter i; | |
4273b793 | 1101 | loff_t endbyte = 0; |
ea9b9907 NP |
1102 | |
1103 | WARN_ON(iocb->ki_pos != pos); | |
1104 | ||
4273b793 AA |
1105 | ocount = 0; |
1106 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); | |
ea9b9907 NP |
1107 | if (err) |
1108 | return err; | |
1109 | ||
4273b793 | 1110 | count = ocount; |
ea9b9907 | 1111 | mutex_lock(&inode->i_mutex); |
ea9b9907 NP |
1112 | |
1113 | /* We can write back this queue in page reclaim */ | |
1114 | current->backing_dev_info = mapping->backing_dev_info; | |
1115 | ||
1116 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
1117 | if (err) | |
1118 | goto out; | |
1119 | ||
1120 | if (count == 0) | |
1121 | goto out; | |
1122 | ||
2f1936b8 | 1123 | err = file_remove_suid(file); |
ea9b9907 NP |
1124 | if (err) |
1125 | goto out; | |
1126 | ||
c3b2da31 JB |
1127 | err = file_update_time(file); |
1128 | if (err) | |
1129 | goto out; | |
ea9b9907 | 1130 | |
4273b793 AA |
1131 | if (file->f_flags & O_DIRECT) { |
1132 | written = generic_file_direct_write(iocb, iov, &nr_segs, | |
1133 | pos, &iocb->ki_pos, | |
1134 | count, ocount); | |
1135 | if (written < 0 || written == count) | |
1136 | goto out; | |
1137 | ||
1138 | pos += written; | |
1139 | count -= written; | |
ea9b9907 | 1140 | |
4273b793 AA |
1141 | iov_iter_init(&i, iov, nr_segs, count, written); |
1142 | written_buffered = fuse_perform_write(file, mapping, &i, pos); | |
1143 | if (written_buffered < 0) { | |
1144 | err = written_buffered; | |
1145 | goto out; | |
1146 | } | |
1147 | endbyte = pos + written_buffered - 1; | |
1148 | ||
1149 | err = filemap_write_and_wait_range(file->f_mapping, pos, | |
1150 | endbyte); | |
1151 | if (err) | |
1152 | goto out; | |
1153 | ||
1154 | invalidate_mapping_pages(file->f_mapping, | |
1155 | pos >> PAGE_CACHE_SHIFT, | |
1156 | endbyte >> PAGE_CACHE_SHIFT); | |
1157 | ||
1158 | written += written_buffered; | |
1159 | iocb->ki_pos = pos + written_buffered; | |
1160 | } else { | |
1161 | iov_iter_init(&i, iov, nr_segs, count, 0); | |
1162 | written = fuse_perform_write(file, mapping, &i, pos); | |
1163 | if (written >= 0) | |
1164 | iocb->ki_pos = pos + written; | |
1165 | } | |
ea9b9907 NP |
1166 | out: |
1167 | current->backing_dev_info = NULL; | |
1168 | mutex_unlock(&inode->i_mutex); | |
1169 | ||
1170 | return written ? written : err; | |
1171 | } | |
1172 | ||
7c190c8b MP |
1173 | static inline void fuse_page_descs_length_init(struct fuse_req *req, |
1174 | unsigned index, unsigned nr_pages) | |
85f40aec MP |
1175 | { |
1176 | int i; | |
1177 | ||
7c190c8b | 1178 | for (i = index; i < index + nr_pages; i++) |
85f40aec MP |
1179 | req->page_descs[i].length = PAGE_SIZE - |
1180 | req->page_descs[i].offset; | |
1181 | } | |
1182 | ||
7c190c8b MP |
1183 | static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) |
1184 | { | |
1185 | return (unsigned long)ii->iov->iov_base + ii->iov_offset; | |
1186 | } | |
1187 | ||
1188 | static inline size_t fuse_get_frag_size(const struct iov_iter *ii, | |
1189 | size_t max_size) | |
1190 | { | |
1191 | return min(iov_iter_single_seg_count(ii), max_size); | |
1192 | } | |
1193 | ||
b98d023a | 1194 | static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii, |
ce60a2f1 | 1195 | size_t *nbytesp, int write) |
413ef8cb | 1196 | { |
7c190c8b | 1197 | size_t nbytes = 0; /* # bytes already packed in req */ |
b98d023a | 1198 | |
f4975c67 MS |
1199 | /* Special case for kernel I/O: can copy directly into the buffer */ |
1200 | if (segment_eq(get_fs(), KERNEL_DS)) { | |
7c190c8b MP |
1201 | unsigned long user_addr = fuse_get_user_addr(ii); |
1202 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp); | |
1203 | ||
f4975c67 MS |
1204 | if (write) |
1205 | req->in.args[1].value = (void *) user_addr; | |
1206 | else | |
1207 | req->out.args[0].value = (void *) user_addr; | |
1208 | ||
b98d023a MP |
1209 | iov_iter_advance(ii, frag_size); |
1210 | *nbytesp = frag_size; | |
f4975c67 MS |
1211 | return 0; |
1212 | } | |
413ef8cb | 1213 | |
5565a9d8 | 1214 | while (nbytes < *nbytesp && req->num_pages < req->max_pages) { |
7c190c8b MP |
1215 | unsigned npages; |
1216 | unsigned long user_addr = fuse_get_user_addr(ii); | |
1217 | unsigned offset = user_addr & ~PAGE_MASK; | |
1218 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes); | |
1219 | int ret; | |
413ef8cb | 1220 | |
5565a9d8 | 1221 | unsigned n = req->max_pages - req->num_pages; |
7c190c8b MP |
1222 | frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT); |
1223 | ||
1224 | npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1225 | npages = clamp(npages, 1U, n); | |
1226 | ||
1227 | ret = get_user_pages_fast(user_addr, npages, !write, | |
1228 | &req->pages[req->num_pages]); | |
1229 | if (ret < 0) | |
1230 | return ret; | |
1231 | ||
1232 | npages = ret; | |
1233 | frag_size = min_t(size_t, frag_size, | |
1234 | (npages << PAGE_SHIFT) - offset); | |
1235 | iov_iter_advance(ii, frag_size); | |
1236 | ||
1237 | req->page_descs[req->num_pages].offset = offset; | |
1238 | fuse_page_descs_length_init(req, req->num_pages, npages); | |
1239 | ||
1240 | req->num_pages += npages; | |
1241 | req->page_descs[req->num_pages - 1].length -= | |
1242 | (npages << PAGE_SHIFT) - offset - frag_size; | |
1243 | ||
1244 | nbytes += frag_size; | |
1245 | } | |
f4975c67 MS |
1246 | |
1247 | if (write) | |
1248 | req->in.argpages = 1; | |
1249 | else | |
1250 | req->out.argpages = 1; | |
1251 | ||
7c190c8b | 1252 | *nbytesp = nbytes; |
f4975c67 | 1253 | |
413ef8cb MS |
1254 | return 0; |
1255 | } | |
1256 | ||
5565a9d8 MP |
1257 | static inline int fuse_iter_npages(const struct iov_iter *ii_p) |
1258 | { | |
1259 | struct iov_iter ii = *ii_p; | |
1260 | int npages = 0; | |
1261 | ||
1262 | while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) { | |
1263 | unsigned long user_addr = fuse_get_user_addr(&ii); | |
1264 | unsigned offset = user_addr & ~PAGE_MASK; | |
1265 | size_t frag_size = iov_iter_single_seg_count(&ii); | |
1266 | ||
1267 | npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1268 | iov_iter_advance(&ii, frag_size); | |
1269 | } | |
1270 | ||
1271 | return min(npages, FUSE_MAX_PAGES_PER_REQ); | |
1272 | } | |
1273 | ||
36cf66ed | 1274 | ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov, |
fb05f41f MS |
1275 | unsigned long nr_segs, size_t count, loff_t *ppos, |
1276 | int write) | |
413ef8cb | 1277 | { |
36cf66ed | 1278 | struct file *file = io->file; |
2106cb18 MS |
1279 | struct fuse_file *ff = file->private_data; |
1280 | struct fuse_conn *fc = ff->fc; | |
413ef8cb MS |
1281 | size_t nmax = write ? fc->max_write : fc->max_read; |
1282 | loff_t pos = *ppos; | |
1283 | ssize_t res = 0; | |
248d86e8 | 1284 | struct fuse_req *req; |
b98d023a MP |
1285 | struct iov_iter ii; |
1286 | ||
1287 | iov_iter_init(&ii, iov, nr_segs, count, 0); | |
248d86e8 | 1288 | |
de82b923 BF |
1289 | if (io->async) |
1290 | req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii)); | |
1291 | else | |
1292 | req = fuse_get_req(fc, fuse_iter_npages(&ii)); | |
ce1d5a49 MS |
1293 | if (IS_ERR(req)) |
1294 | return PTR_ERR(req); | |
413ef8cb MS |
1295 | |
1296 | while (count) { | |
413ef8cb | 1297 | size_t nres; |
2106cb18 | 1298 | fl_owner_t owner = current->files; |
f4975c67 | 1299 | size_t nbytes = min(count, nmax); |
b98d023a | 1300 | int err = fuse_get_user_pages(req, &ii, &nbytes, write); |
413ef8cb MS |
1301 | if (err) { |
1302 | res = err; | |
1303 | break; | |
1304 | } | |
f4975c67 | 1305 | |
413ef8cb | 1306 | if (write) |
36cf66ed | 1307 | nres = fuse_send_write(req, io, pos, nbytes, owner); |
413ef8cb | 1308 | else |
36cf66ed | 1309 | nres = fuse_send_read(req, io, pos, nbytes, owner); |
2106cb18 | 1310 | |
36cf66ed MP |
1311 | if (!io->async) |
1312 | fuse_release_user_pages(req, !write); | |
413ef8cb MS |
1313 | if (req->out.h.error) { |
1314 | if (!res) | |
1315 | res = req->out.h.error; | |
1316 | break; | |
1317 | } else if (nres > nbytes) { | |
1318 | res = -EIO; | |
1319 | break; | |
1320 | } | |
1321 | count -= nres; | |
1322 | res += nres; | |
1323 | pos += nres; | |
413ef8cb MS |
1324 | if (nres != nbytes) |
1325 | break; | |
56cf34ff MS |
1326 | if (count) { |
1327 | fuse_put_request(fc, req); | |
de82b923 BF |
1328 | if (io->async) |
1329 | req = fuse_get_req_for_background(fc, | |
1330 | fuse_iter_npages(&ii)); | |
1331 | else | |
1332 | req = fuse_get_req(fc, fuse_iter_npages(&ii)); | |
56cf34ff MS |
1333 | if (IS_ERR(req)) |
1334 | break; | |
1335 | } | |
413ef8cb | 1336 | } |
f60311d5 AA |
1337 | if (!IS_ERR(req)) |
1338 | fuse_put_request(fc, req); | |
d09cb9d7 | 1339 | if (res > 0) |
413ef8cb | 1340 | *ppos = pos; |
413ef8cb MS |
1341 | |
1342 | return res; | |
1343 | } | |
08cbf542 | 1344 | EXPORT_SYMBOL_GPL(fuse_direct_io); |
413ef8cb | 1345 | |
36cf66ed MP |
1346 | static ssize_t __fuse_direct_read(struct fuse_io_priv *io, |
1347 | const struct iovec *iov, | |
439ee5f0 MP |
1348 | unsigned long nr_segs, loff_t *ppos, |
1349 | size_t count) | |
413ef8cb | 1350 | { |
d09cb9d7 | 1351 | ssize_t res; |
36cf66ed | 1352 | struct file *file = io->file; |
6131ffaa | 1353 | struct inode *inode = file_inode(file); |
d09cb9d7 MS |
1354 | |
1355 | if (is_bad_inode(inode)) | |
1356 | return -EIO; | |
1357 | ||
439ee5f0 | 1358 | res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0); |
d09cb9d7 MS |
1359 | |
1360 | fuse_invalidate_attr(inode); | |
1361 | ||
1362 | return res; | |
413ef8cb MS |
1363 | } |
1364 | ||
b98d023a MP |
1365 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
1366 | size_t count, loff_t *ppos) | |
1367 | { | |
36cf66ed | 1368 | struct fuse_io_priv io = { .async = 0, .file = file }; |
fb05f41f | 1369 | struct iovec iov = { .iov_base = buf, .iov_len = count }; |
439ee5f0 | 1370 | return __fuse_direct_read(&io, &iov, 1, ppos, count); |
b98d023a MP |
1371 | } |
1372 | ||
36cf66ed MP |
1373 | static ssize_t __fuse_direct_write(struct fuse_io_priv *io, |
1374 | const struct iovec *iov, | |
b98d023a | 1375 | unsigned long nr_segs, loff_t *ppos) |
413ef8cb | 1376 | { |
36cf66ed | 1377 | struct file *file = io->file; |
6131ffaa | 1378 | struct inode *inode = file_inode(file); |
b98d023a | 1379 | size_t count = iov_length(iov, nr_segs); |
413ef8cb | 1380 | ssize_t res; |
d09cb9d7 | 1381 | |
889f7848 | 1382 | res = generic_write_checks(file, ppos, &count, 0); |
bcba24cc | 1383 | if (!res) |
36cf66ed | 1384 | res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1); |
d09cb9d7 MS |
1385 | |
1386 | fuse_invalidate_attr(inode); | |
1387 | ||
413ef8cb MS |
1388 | return res; |
1389 | } | |
1390 | ||
4273b793 AA |
1391 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
1392 | size_t count, loff_t *ppos) | |
1393 | { | |
fb05f41f | 1394 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
6131ffaa | 1395 | struct inode *inode = file_inode(file); |
4273b793 | 1396 | ssize_t res; |
36cf66ed | 1397 | struct fuse_io_priv io = { .async = 0, .file = file }; |
4273b793 AA |
1398 | |
1399 | if (is_bad_inode(inode)) | |
1400 | return -EIO; | |
1401 | ||
1402 | /* Don't allow parallel writes to the same file */ | |
1403 | mutex_lock(&inode->i_mutex); | |
36cf66ed | 1404 | res = __fuse_direct_write(&io, &iov, 1, ppos); |
bcba24cc MP |
1405 | if (res > 0) |
1406 | fuse_write_update_size(inode, *ppos); | |
4273b793 AA |
1407 | mutex_unlock(&inode->i_mutex); |
1408 | ||
1409 | return res; | |
1410 | } | |
1411 | ||
3be5a52b | 1412 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
b6aeaded | 1413 | { |
3be5a52b | 1414 | __free_page(req->pages[0]); |
5a18ec17 | 1415 | fuse_file_put(req->ff, false); |
3be5a52b MS |
1416 | } |
1417 | ||
1418 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | |
1419 | { | |
1420 | struct inode *inode = req->inode; | |
1421 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1422 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; | |
1423 | ||
1424 | list_del(&req->writepages_entry); | |
1425 | dec_bdi_stat(bdi, BDI_WRITEBACK); | |
1426 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); | |
1427 | bdi_writeout_inc(bdi); | |
1428 | wake_up(&fi->page_waitq); | |
1429 | } | |
1430 | ||
1431 | /* Called under fc->lock, may release and reacquire it */ | |
1432 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) | |
b9ca67b2 MS |
1433 | __releases(fc->lock) |
1434 | __acquires(fc->lock) | |
3be5a52b MS |
1435 | { |
1436 | struct fuse_inode *fi = get_fuse_inode(req->inode); | |
1437 | loff_t size = i_size_read(req->inode); | |
1438 | struct fuse_write_in *inarg = &req->misc.write.in; | |
1439 | ||
1440 | if (!fc->connected) | |
1441 | goto out_free; | |
1442 | ||
1443 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { | |
1444 | inarg->size = PAGE_CACHE_SIZE; | |
1445 | } else if (inarg->offset < size) { | |
1446 | inarg->size = size & (PAGE_CACHE_SIZE - 1); | |
1447 | } else { | |
1448 | /* Got truncated off completely */ | |
1449 | goto out_free; | |
b6aeaded | 1450 | } |
3be5a52b MS |
1451 | |
1452 | req->in.args[1].size = inarg->size; | |
1453 | fi->writectr++; | |
b93f858a | 1454 | fuse_request_send_background_locked(fc, req); |
3be5a52b MS |
1455 | return; |
1456 | ||
1457 | out_free: | |
1458 | fuse_writepage_finish(fc, req); | |
1459 | spin_unlock(&fc->lock); | |
1460 | fuse_writepage_free(fc, req); | |
e9bb09dd | 1461 | fuse_put_request(fc, req); |
3be5a52b | 1462 | spin_lock(&fc->lock); |
b6aeaded MS |
1463 | } |
1464 | ||
3be5a52b MS |
1465 | /* |
1466 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1467 | * all queued writepage requests. | |
1468 | * | |
1469 | * Called with fc->lock | |
1470 | */ | |
1471 | void fuse_flush_writepages(struct inode *inode) | |
b9ca67b2 MS |
1472 | __releases(fc->lock) |
1473 | __acquires(fc->lock) | |
b6aeaded | 1474 | { |
3be5a52b MS |
1475 | struct fuse_conn *fc = get_fuse_conn(inode); |
1476 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1477 | struct fuse_req *req; | |
1478 | ||
1479 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
1480 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | |
1481 | list_del_init(&req->list); | |
1482 | fuse_send_writepage(fc, req); | |
1483 | } | |
1484 | } | |
1485 | ||
1486 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | |
1487 | { | |
1488 | struct inode *inode = req->inode; | |
1489 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1490 | ||
1491 | mapping_set_error(inode->i_mapping, req->out.h.error); | |
1492 | spin_lock(&fc->lock); | |
1493 | fi->writectr--; | |
1494 | fuse_writepage_finish(fc, req); | |
1495 | spin_unlock(&fc->lock); | |
1496 | fuse_writepage_free(fc, req); | |
1497 | } | |
1498 | ||
1499 | static int fuse_writepage_locked(struct page *page) | |
1500 | { | |
1501 | struct address_space *mapping = page->mapping; | |
1502 | struct inode *inode = mapping->host; | |
1503 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1504 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1505 | struct fuse_req *req; | |
1506 | struct fuse_file *ff; | |
1507 | struct page *tmp_page; | |
1508 | ||
1509 | set_page_writeback(page); | |
1510 | ||
4250c066 | 1511 | req = fuse_request_alloc_nofs(1); |
3be5a52b MS |
1512 | if (!req) |
1513 | goto err; | |
1514 | ||
8b41e671 | 1515 | req->background = 1; /* writeback always goes to bg_queue */ |
3be5a52b MS |
1516 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
1517 | if (!tmp_page) | |
1518 | goto err_free; | |
1519 | ||
1520 | spin_lock(&fc->lock); | |
1521 | BUG_ON(list_empty(&fi->write_files)); | |
1522 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); | |
1523 | req->ff = fuse_file_get(ff); | |
1524 | spin_unlock(&fc->lock); | |
1525 | ||
2106cb18 | 1526 | fuse_write_fill(req, ff, page_offset(page), 0); |
3be5a52b MS |
1527 | |
1528 | copy_highpage(tmp_page, page); | |
2d698b07 | 1529 | req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; |
f4975c67 | 1530 | req->in.argpages = 1; |
3be5a52b MS |
1531 | req->num_pages = 1; |
1532 | req->pages[0] = tmp_page; | |
b2430d75 | 1533 | req->page_descs[0].offset = 0; |
85f40aec | 1534 | req->page_descs[0].length = PAGE_SIZE; |
3be5a52b MS |
1535 | req->end = fuse_writepage_end; |
1536 | req->inode = inode; | |
1537 | ||
1538 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); | |
1539 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); | |
3be5a52b MS |
1540 | |
1541 | spin_lock(&fc->lock); | |
1542 | list_add(&req->writepages_entry, &fi->writepages); | |
1543 | list_add_tail(&req->list, &fi->queued_writes); | |
1544 | fuse_flush_writepages(inode); | |
1545 | spin_unlock(&fc->lock); | |
1546 | ||
3002e63b MP |
1547 | end_page_writeback(page); |
1548 | ||
3be5a52b MS |
1549 | return 0; |
1550 | ||
1551 | err_free: | |
1552 | fuse_request_free(req); | |
1553 | err: | |
1554 | end_page_writeback(page); | |
1555 | return -ENOMEM; | |
1556 | } | |
1557 | ||
1558 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1559 | { | |
1560 | int err; | |
1561 | ||
1562 | err = fuse_writepage_locked(page); | |
1563 | unlock_page(page); | |
1564 | ||
1565 | return err; | |
1566 | } | |
1567 | ||
1568 | static int fuse_launder_page(struct page *page) | |
1569 | { | |
1570 | int err = 0; | |
1571 | if (clear_page_dirty_for_io(page)) { | |
1572 | struct inode *inode = page->mapping->host; | |
1573 | err = fuse_writepage_locked(page); | |
1574 | if (!err) | |
1575 | fuse_wait_on_page_writeback(inode, page->index); | |
1576 | } | |
1577 | return err; | |
1578 | } | |
1579 | ||
1580 | /* | |
1581 | * Write back dirty pages now, because there may not be any suitable | |
1582 | * open files later | |
1583 | */ | |
1584 | static void fuse_vma_close(struct vm_area_struct *vma) | |
1585 | { | |
1586 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
1587 | } | |
1588 | ||
1589 | /* | |
1590 | * Wait for writeback against this page to complete before allowing it | |
1591 | * to be marked dirty again, and hence written back again, possibly | |
1592 | * before the previous writepage completed. | |
1593 | * | |
1594 | * Block here, instead of in ->writepage(), so that the userspace fs | |
1595 | * can only block processes actually operating on the filesystem. | |
1596 | * | |
1597 | * Otherwise unprivileged userspace fs would be able to block | |
1598 | * unrelated: | |
1599 | * | |
1600 | * - page migration | |
1601 | * - sync(2) | |
1602 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
1603 | */ | |
c2ec175c | 1604 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
3be5a52b | 1605 | { |
c2ec175c | 1606 | struct page *page = vmf->page; |
3be5a52b MS |
1607 | /* |
1608 | * Don't use page->mapping as it may become NULL from a | |
1609 | * concurrent truncate. | |
1610 | */ | |
1611 | struct inode *inode = vma->vm_file->f_mapping->host; | |
1612 | ||
1613 | fuse_wait_on_page_writeback(inode, page->index); | |
1614 | return 0; | |
1615 | } | |
1616 | ||
f0f37e2f | 1617 | static const struct vm_operations_struct fuse_file_vm_ops = { |
3be5a52b MS |
1618 | .close = fuse_vma_close, |
1619 | .fault = filemap_fault, | |
1620 | .page_mkwrite = fuse_page_mkwrite, | |
0b173bc4 | 1621 | .remap_pages = generic_file_remap_pages, |
3be5a52b MS |
1622 | }; |
1623 | ||
1624 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
1625 | { | |
1626 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { | |
6131ffaa | 1627 | struct inode *inode = file_inode(file); |
3be5a52b MS |
1628 | struct fuse_conn *fc = get_fuse_conn(inode); |
1629 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1630 | struct fuse_file *ff = file->private_data; | |
1631 | /* | |
1632 | * file may be written through mmap, so chain it onto the | |
1633 | * inodes's write_file list | |
1634 | */ | |
1635 | spin_lock(&fc->lock); | |
1636 | if (list_empty(&ff->write_entry)) | |
1637 | list_add(&ff->write_entry, &fi->write_files); | |
1638 | spin_unlock(&fc->lock); | |
1639 | } | |
1640 | file_accessed(file); | |
1641 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
1642 | return 0; |
1643 | } | |
1644 | ||
fc280c96 MS |
1645 | static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma) |
1646 | { | |
1647 | /* Can't provide the coherency needed for MAP_SHARED */ | |
1648 | if (vma->vm_flags & VM_MAYSHARE) | |
1649 | return -ENODEV; | |
1650 | ||
3121bfe7 MS |
1651 | invalidate_inode_pages2(file->f_mapping); |
1652 | ||
fc280c96 MS |
1653 | return generic_file_mmap(file, vma); |
1654 | } | |
1655 | ||
71421259 MS |
1656 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
1657 | struct file_lock *fl) | |
1658 | { | |
1659 | switch (ffl->type) { | |
1660 | case F_UNLCK: | |
1661 | break; | |
1662 | ||
1663 | case F_RDLCK: | |
1664 | case F_WRLCK: | |
1665 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
1666 | ffl->end < ffl->start) | |
1667 | return -EIO; | |
1668 | ||
1669 | fl->fl_start = ffl->start; | |
1670 | fl->fl_end = ffl->end; | |
1671 | fl->fl_pid = ffl->pid; | |
1672 | break; | |
1673 | ||
1674 | default: | |
1675 | return -EIO; | |
1676 | } | |
1677 | fl->fl_type = ffl->type; | |
1678 | return 0; | |
1679 | } | |
1680 | ||
1681 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, | |
a9ff4f87 MS |
1682 | const struct file_lock *fl, int opcode, pid_t pid, |
1683 | int flock) | |
71421259 | 1684 | { |
6131ffaa | 1685 | struct inode *inode = file_inode(file); |
9c8ef561 | 1686 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 MS |
1687 | struct fuse_file *ff = file->private_data; |
1688 | struct fuse_lk_in *arg = &req->misc.lk_in; | |
1689 | ||
1690 | arg->fh = ff->fh; | |
9c8ef561 | 1691 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
71421259 MS |
1692 | arg->lk.start = fl->fl_start; |
1693 | arg->lk.end = fl->fl_end; | |
1694 | arg->lk.type = fl->fl_type; | |
1695 | arg->lk.pid = pid; | |
a9ff4f87 MS |
1696 | if (flock) |
1697 | arg->lk_flags |= FUSE_LK_FLOCK; | |
71421259 MS |
1698 | req->in.h.opcode = opcode; |
1699 | req->in.h.nodeid = get_node_id(inode); | |
1700 | req->in.numargs = 1; | |
1701 | req->in.args[0].size = sizeof(*arg); | |
1702 | req->in.args[0].value = arg; | |
1703 | } | |
1704 | ||
1705 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
1706 | { | |
6131ffaa | 1707 | struct inode *inode = file_inode(file); |
71421259 MS |
1708 | struct fuse_conn *fc = get_fuse_conn(inode); |
1709 | struct fuse_req *req; | |
1710 | struct fuse_lk_out outarg; | |
1711 | int err; | |
1712 | ||
b111c8c0 | 1713 | req = fuse_get_req_nopages(fc); |
71421259 MS |
1714 | if (IS_ERR(req)) |
1715 | return PTR_ERR(req); | |
1716 | ||
a9ff4f87 | 1717 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
71421259 MS |
1718 | req->out.numargs = 1; |
1719 | req->out.args[0].size = sizeof(outarg); | |
1720 | req->out.args[0].value = &outarg; | |
b93f858a | 1721 | fuse_request_send(fc, req); |
71421259 MS |
1722 | err = req->out.h.error; |
1723 | fuse_put_request(fc, req); | |
1724 | if (!err) | |
1725 | err = convert_fuse_file_lock(&outarg.lk, fl); | |
1726 | ||
1727 | return err; | |
1728 | } | |
1729 | ||
a9ff4f87 | 1730 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 1731 | { |
6131ffaa | 1732 | struct inode *inode = file_inode(file); |
71421259 MS |
1733 | struct fuse_conn *fc = get_fuse_conn(inode); |
1734 | struct fuse_req *req; | |
1735 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; | |
1736 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; | |
1737 | int err; | |
1738 | ||
8fb47a4f | 1739 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) { |
48e90761 MS |
1740 | /* NLM needs asynchronous locks, which we don't support yet */ |
1741 | return -ENOLCK; | |
1742 | } | |
1743 | ||
71421259 MS |
1744 | /* Unlock on close is handled by the flush method */ |
1745 | if (fl->fl_flags & FL_CLOSE) | |
1746 | return 0; | |
1747 | ||
b111c8c0 | 1748 | req = fuse_get_req_nopages(fc); |
71421259 MS |
1749 | if (IS_ERR(req)) |
1750 | return PTR_ERR(req); | |
1751 | ||
a9ff4f87 | 1752 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
b93f858a | 1753 | fuse_request_send(fc, req); |
71421259 | 1754 | err = req->out.h.error; |
a4d27e75 MS |
1755 | /* locking is restartable */ |
1756 | if (err == -EINTR) | |
1757 | err = -ERESTARTSYS; | |
71421259 MS |
1758 | fuse_put_request(fc, req); |
1759 | return err; | |
1760 | } | |
1761 | ||
1762 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
1763 | { | |
6131ffaa | 1764 | struct inode *inode = file_inode(file); |
71421259 MS |
1765 | struct fuse_conn *fc = get_fuse_conn(inode); |
1766 | int err; | |
1767 | ||
48e90761 MS |
1768 | if (cmd == F_CANCELLK) { |
1769 | err = 0; | |
1770 | } else if (cmd == F_GETLK) { | |
71421259 | 1771 | if (fc->no_lock) { |
9d6a8c5c | 1772 | posix_test_lock(file, fl); |
71421259 MS |
1773 | err = 0; |
1774 | } else | |
1775 | err = fuse_getlk(file, fl); | |
1776 | } else { | |
1777 | if (fc->no_lock) | |
48e90761 | 1778 | err = posix_lock_file(file, fl, NULL); |
71421259 | 1779 | else |
a9ff4f87 | 1780 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
1781 | } |
1782 | return err; | |
1783 | } | |
1784 | ||
a9ff4f87 MS |
1785 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
1786 | { | |
6131ffaa | 1787 | struct inode *inode = file_inode(file); |
a9ff4f87 MS |
1788 | struct fuse_conn *fc = get_fuse_conn(inode); |
1789 | int err; | |
1790 | ||
37fb3a30 | 1791 | if (fc->no_flock) { |
a9ff4f87 MS |
1792 | err = flock_lock_file_wait(file, fl); |
1793 | } else { | |
37fb3a30 MS |
1794 | struct fuse_file *ff = file->private_data; |
1795 | ||
a9ff4f87 MS |
1796 | /* emulate flock with POSIX locks */ |
1797 | fl->fl_owner = (fl_owner_t) file; | |
37fb3a30 | 1798 | ff->flock = true; |
a9ff4f87 MS |
1799 | err = fuse_setlk(file, fl, 1); |
1800 | } | |
1801 | ||
1802 | return err; | |
1803 | } | |
1804 | ||
b2d2272f MS |
1805 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
1806 | { | |
1807 | struct inode *inode = mapping->host; | |
1808 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1809 | struct fuse_req *req; | |
1810 | struct fuse_bmap_in inarg; | |
1811 | struct fuse_bmap_out outarg; | |
1812 | int err; | |
1813 | ||
1814 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
1815 | return 0; | |
1816 | ||
b111c8c0 | 1817 | req = fuse_get_req_nopages(fc); |
b2d2272f MS |
1818 | if (IS_ERR(req)) |
1819 | return 0; | |
1820 | ||
1821 | memset(&inarg, 0, sizeof(inarg)); | |
1822 | inarg.block = block; | |
1823 | inarg.blocksize = inode->i_sb->s_blocksize; | |
1824 | req->in.h.opcode = FUSE_BMAP; | |
1825 | req->in.h.nodeid = get_node_id(inode); | |
1826 | req->in.numargs = 1; | |
1827 | req->in.args[0].size = sizeof(inarg); | |
1828 | req->in.args[0].value = &inarg; | |
1829 | req->out.numargs = 1; | |
1830 | req->out.args[0].size = sizeof(outarg); | |
1831 | req->out.args[0].value = &outarg; | |
b93f858a | 1832 | fuse_request_send(fc, req); |
b2d2272f MS |
1833 | err = req->out.h.error; |
1834 | fuse_put_request(fc, req); | |
1835 | if (err == -ENOSYS) | |
1836 | fc->no_bmap = 1; | |
1837 | ||
1838 | return err ? 0 : outarg.block; | |
1839 | } | |
1840 | ||
965c8e59 | 1841 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) |
5559b8f4 MS |
1842 | { |
1843 | loff_t retval; | |
6131ffaa | 1844 | struct inode *inode = file_inode(file); |
5559b8f4 | 1845 | |
c07c3d19 | 1846 | /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ |
965c8e59 AM |
1847 | if (whence == SEEK_CUR || whence == SEEK_SET) |
1848 | return generic_file_llseek(file, offset, whence); | |
06222e49 | 1849 | |
c07c3d19 MS |
1850 | mutex_lock(&inode->i_mutex); |
1851 | retval = fuse_update_attributes(inode, NULL, file, NULL); | |
1852 | if (!retval) | |
965c8e59 | 1853 | retval = generic_file_llseek(file, offset, whence); |
5559b8f4 | 1854 | mutex_unlock(&inode->i_mutex); |
c07c3d19 | 1855 | |
5559b8f4 MS |
1856 | return retval; |
1857 | } | |
1858 | ||
59efec7b TH |
1859 | static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov, |
1860 | unsigned int nr_segs, size_t bytes, bool to_user) | |
1861 | { | |
1862 | struct iov_iter ii; | |
1863 | int page_idx = 0; | |
1864 | ||
1865 | if (!bytes) | |
1866 | return 0; | |
1867 | ||
1868 | iov_iter_init(&ii, iov, nr_segs, bytes, 0); | |
1869 | ||
1870 | while (iov_iter_count(&ii)) { | |
1871 | struct page *page = pages[page_idx++]; | |
1872 | size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii)); | |
4aa0edd2 | 1873 | void *kaddr; |
59efec7b | 1874 | |
4aa0edd2 | 1875 | kaddr = kmap(page); |
59efec7b TH |
1876 | |
1877 | while (todo) { | |
1878 | char __user *uaddr = ii.iov->iov_base + ii.iov_offset; | |
1879 | size_t iov_len = ii.iov->iov_len - ii.iov_offset; | |
1880 | size_t copy = min(todo, iov_len); | |
1881 | size_t left; | |
1882 | ||
1883 | if (!to_user) | |
1884 | left = copy_from_user(kaddr, uaddr, copy); | |
1885 | else | |
1886 | left = copy_to_user(uaddr, kaddr, copy); | |
1887 | ||
1888 | if (unlikely(left)) | |
1889 | return -EFAULT; | |
1890 | ||
1891 | iov_iter_advance(&ii, copy); | |
1892 | todo -= copy; | |
1893 | kaddr += copy; | |
1894 | } | |
1895 | ||
0bd87182 | 1896 | kunmap(page); |
59efec7b TH |
1897 | } |
1898 | ||
1899 | return 0; | |
1900 | } | |
1901 | ||
d9d318d3 MS |
1902 | /* |
1903 | * CUSE servers compiled on 32bit broke on 64bit kernels because the | |
1904 | * ABI was defined to be 'struct iovec' which is different on 32bit | |
1905 | * and 64bit. Fortunately we can determine which structure the server | |
1906 | * used from the size of the reply. | |
1907 | */ | |
1baa26b2 MS |
1908 | static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, |
1909 | size_t transferred, unsigned count, | |
1910 | bool is_compat) | |
d9d318d3 MS |
1911 | { |
1912 | #ifdef CONFIG_COMPAT | |
1913 | if (count * sizeof(struct compat_iovec) == transferred) { | |
1914 | struct compat_iovec *ciov = src; | |
1915 | unsigned i; | |
1916 | ||
1917 | /* | |
1918 | * With this interface a 32bit server cannot support | |
1919 | * non-compat (i.e. ones coming from 64bit apps) ioctl | |
1920 | * requests | |
1921 | */ | |
1922 | if (!is_compat) | |
1923 | return -EINVAL; | |
1924 | ||
1925 | for (i = 0; i < count; i++) { | |
1926 | dst[i].iov_base = compat_ptr(ciov[i].iov_base); | |
1927 | dst[i].iov_len = ciov[i].iov_len; | |
1928 | } | |
1929 | return 0; | |
1930 | } | |
1931 | #endif | |
1932 | ||
1933 | if (count * sizeof(struct iovec) != transferred) | |
1934 | return -EIO; | |
1935 | ||
1936 | memcpy(dst, src, transferred); | |
1937 | return 0; | |
1938 | } | |
1939 | ||
7572777e MS |
1940 | /* Make sure iov_length() won't overflow */ |
1941 | static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count) | |
1942 | { | |
1943 | size_t n; | |
1944 | u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT; | |
1945 | ||
fb6ccff6 | 1946 | for (n = 0; n < count; n++, iov++) { |
7572777e MS |
1947 | if (iov->iov_len > (size_t) max) |
1948 | return -ENOMEM; | |
1949 | max -= iov->iov_len; | |
1950 | } | |
1951 | return 0; | |
1952 | } | |
1953 | ||
1baa26b2 MS |
1954 | static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, |
1955 | void *src, size_t transferred, unsigned count, | |
1956 | bool is_compat) | |
1957 | { | |
1958 | unsigned i; | |
1959 | struct fuse_ioctl_iovec *fiov = src; | |
1960 | ||
1961 | if (fc->minor < 16) { | |
1962 | return fuse_copy_ioctl_iovec_old(dst, src, transferred, | |
1963 | count, is_compat); | |
1964 | } | |
1965 | ||
1966 | if (count * sizeof(struct fuse_ioctl_iovec) != transferred) | |
1967 | return -EIO; | |
1968 | ||
1969 | for (i = 0; i < count; i++) { | |
1970 | /* Did the server supply an inappropriate value? */ | |
1971 | if (fiov[i].base != (unsigned long) fiov[i].base || | |
1972 | fiov[i].len != (unsigned long) fiov[i].len) | |
1973 | return -EIO; | |
1974 | ||
1975 | dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; | |
1976 | dst[i].iov_len = (size_t) fiov[i].len; | |
1977 | ||
1978 | #ifdef CONFIG_COMPAT | |
1979 | if (is_compat && | |
1980 | (ptr_to_compat(dst[i].iov_base) != fiov[i].base || | |
1981 | (compat_size_t) dst[i].iov_len != fiov[i].len)) | |
1982 | return -EIO; | |
1983 | #endif | |
1984 | } | |
1985 | ||
1986 | return 0; | |
1987 | } | |
1988 | ||
1989 | ||
59efec7b TH |
1990 | /* |
1991 | * For ioctls, there is no generic way to determine how much memory | |
1992 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
1993 | * to dereference the passed pointer, so the parameter requires deep | |
1994 | * copying but FUSE has no idea whatsoever about what to copy in or | |
1995 | * out. | |
1996 | * | |
1997 | * This is solved by allowing FUSE server to retry ioctl with | |
1998 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
1999 | * needs to read in the following structure. | |
2000 | * | |
2001 | * struct a { | |
2002 | * char *buf; | |
2003 | * size_t buflen; | |
2004 | * } | |
2005 | * | |
2006 | * On the first callout to FUSE server, inarg->in_size and | |
2007 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
2008 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
2009 | * the actual iov array to | |
2010 | * | |
2011 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
2012 | * | |
2013 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
2014 | * On the second round, the server has access to the structure and | |
2015 | * from that it can tell what to look for next, so on the invocation, | |
2016 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
2017 | * | |
2018 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
2019 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
2020 | * | |
2021 | * FUSE will copy both struct a and the pointed buffer from the | |
2022 | * process doing the ioctl and retry ioctl with both struct a and the | |
2023 | * buffer. | |
2024 | * | |
2025 | * This time, FUSE server has everything it needs and completes ioctl | |
2026 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
2027 | * | |
2028 | * Copying data out works the same way. | |
2029 | * | |
2030 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
2031 | * automatically initializes in and out iovs by decoding @cmd with | |
2032 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
2033 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
2034 | * behavior for all FUSE servers. | |
2035 | */ | |
08cbf542 TH |
2036 | long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, |
2037 | unsigned int flags) | |
59efec7b | 2038 | { |
59efec7b | 2039 | struct fuse_file *ff = file->private_data; |
d36f2487 | 2040 | struct fuse_conn *fc = ff->fc; |
59efec7b TH |
2041 | struct fuse_ioctl_in inarg = { |
2042 | .fh = ff->fh, | |
2043 | .cmd = cmd, | |
2044 | .arg = arg, | |
2045 | .flags = flags | |
2046 | }; | |
2047 | struct fuse_ioctl_out outarg; | |
2048 | struct fuse_req *req = NULL; | |
2049 | struct page **pages = NULL; | |
8ac83505 | 2050 | struct iovec *iov_page = NULL; |
59efec7b TH |
2051 | struct iovec *in_iov = NULL, *out_iov = NULL; |
2052 | unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages; | |
2053 | size_t in_size, out_size, transferred; | |
2054 | int err; | |
2055 | ||
1baa26b2 MS |
2056 | #if BITS_PER_LONG == 32 |
2057 | inarg.flags |= FUSE_IOCTL_32BIT; | |
2058 | #else | |
2059 | if (flags & FUSE_IOCTL_COMPAT) | |
2060 | inarg.flags |= FUSE_IOCTL_32BIT; | |
2061 | #endif | |
2062 | ||
59efec7b | 2063 | /* assume all the iovs returned by client always fits in a page */ |
1baa26b2 | 2064 | BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); |
59efec7b | 2065 | |
59efec7b | 2066 | err = -ENOMEM; |
c411cc88 | 2067 | pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL); |
8ac83505 | 2068 | iov_page = (struct iovec *) __get_free_page(GFP_KERNEL); |
59efec7b TH |
2069 | if (!pages || !iov_page) |
2070 | goto out; | |
2071 | ||
2072 | /* | |
2073 | * If restricted, initialize IO parameters as encoded in @cmd. | |
2074 | * RETRY from server is not allowed. | |
2075 | */ | |
2076 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
8ac83505 | 2077 | struct iovec *iov = iov_page; |
59efec7b | 2078 | |
c9f0523d | 2079 | iov->iov_base = (void __user *)arg; |
59efec7b TH |
2080 | iov->iov_len = _IOC_SIZE(cmd); |
2081 | ||
2082 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
2083 | in_iov = iov; | |
2084 | in_iovs = 1; | |
2085 | } | |
2086 | ||
2087 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
2088 | out_iov = iov; | |
2089 | out_iovs = 1; | |
2090 | } | |
2091 | } | |
2092 | ||
2093 | retry: | |
2094 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
2095 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
2096 | ||
2097 | /* | |
2098 | * Out data can be used either for actual out data or iovs, | |
2099 | * make sure there always is at least one page. | |
2100 | */ | |
2101 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
2102 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
2103 | ||
2104 | /* make sure there are enough buffer pages and init request with them */ | |
2105 | err = -ENOMEM; | |
2106 | if (max_pages > FUSE_MAX_PAGES_PER_REQ) | |
2107 | goto out; | |
2108 | while (num_pages < max_pages) { | |
2109 | pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
2110 | if (!pages[num_pages]) | |
2111 | goto out; | |
2112 | num_pages++; | |
2113 | } | |
2114 | ||
54b96670 | 2115 | req = fuse_get_req(fc, num_pages); |
59efec7b TH |
2116 | if (IS_ERR(req)) { |
2117 | err = PTR_ERR(req); | |
2118 | req = NULL; | |
2119 | goto out; | |
2120 | } | |
2121 | memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages); | |
2122 | req->num_pages = num_pages; | |
7c190c8b | 2123 | fuse_page_descs_length_init(req, 0, req->num_pages); |
59efec7b TH |
2124 | |
2125 | /* okay, let's send it to the client */ | |
2126 | req->in.h.opcode = FUSE_IOCTL; | |
d36f2487 | 2127 | req->in.h.nodeid = ff->nodeid; |
59efec7b TH |
2128 | req->in.numargs = 1; |
2129 | req->in.args[0].size = sizeof(inarg); | |
2130 | req->in.args[0].value = &inarg; | |
2131 | if (in_size) { | |
2132 | req->in.numargs++; | |
2133 | req->in.args[1].size = in_size; | |
2134 | req->in.argpages = 1; | |
2135 | ||
2136 | err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size, | |
2137 | false); | |
2138 | if (err) | |
2139 | goto out; | |
2140 | } | |
2141 | ||
2142 | req->out.numargs = 2; | |
2143 | req->out.args[0].size = sizeof(outarg); | |
2144 | req->out.args[0].value = &outarg; | |
2145 | req->out.args[1].size = out_size; | |
2146 | req->out.argpages = 1; | |
2147 | req->out.argvar = 1; | |
2148 | ||
b93f858a | 2149 | fuse_request_send(fc, req); |
59efec7b TH |
2150 | err = req->out.h.error; |
2151 | transferred = req->out.args[1].size; | |
2152 | fuse_put_request(fc, req); | |
2153 | req = NULL; | |
2154 | if (err) | |
2155 | goto out; | |
2156 | ||
2157 | /* did it ask for retry? */ | |
2158 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
8ac83505 | 2159 | void *vaddr; |
59efec7b TH |
2160 | |
2161 | /* no retry if in restricted mode */ | |
2162 | err = -EIO; | |
2163 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
2164 | goto out; | |
2165 | ||
2166 | in_iovs = outarg.in_iovs; | |
2167 | out_iovs = outarg.out_iovs; | |
2168 | ||
2169 | /* | |
2170 | * Make sure things are in boundary, separate checks | |
2171 | * are to protect against overflow. | |
2172 | */ | |
2173 | err = -ENOMEM; | |
2174 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
2175 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
2176 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
2177 | goto out; | |
2178 | ||
2408f6ef | 2179 | vaddr = kmap_atomic(pages[0]); |
1baa26b2 | 2180 | err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr, |
d9d318d3 MS |
2181 | transferred, in_iovs + out_iovs, |
2182 | (flags & FUSE_IOCTL_COMPAT) != 0); | |
2408f6ef | 2183 | kunmap_atomic(vaddr); |
d9d318d3 MS |
2184 | if (err) |
2185 | goto out; | |
59efec7b | 2186 | |
8ac83505 | 2187 | in_iov = iov_page; |
59efec7b TH |
2188 | out_iov = in_iov + in_iovs; |
2189 | ||
7572777e MS |
2190 | err = fuse_verify_ioctl_iov(in_iov, in_iovs); |
2191 | if (err) | |
2192 | goto out; | |
2193 | ||
2194 | err = fuse_verify_ioctl_iov(out_iov, out_iovs); | |
2195 | if (err) | |
2196 | goto out; | |
2197 | ||
59efec7b TH |
2198 | goto retry; |
2199 | } | |
2200 | ||
2201 | err = -EIO; | |
2202 | if (transferred > inarg.out_size) | |
2203 | goto out; | |
2204 | ||
2205 | err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true); | |
2206 | out: | |
2207 | if (req) | |
2208 | fuse_put_request(fc, req); | |
8ac83505 | 2209 | free_page((unsigned long) iov_page); |
59efec7b TH |
2210 | while (num_pages) |
2211 | __free_page(pages[--num_pages]); | |
2212 | kfree(pages); | |
2213 | ||
2214 | return err ? err : outarg.result; | |
2215 | } | |
08cbf542 | 2216 | EXPORT_SYMBOL_GPL(fuse_do_ioctl); |
59efec7b | 2217 | |
b18da0c5 MS |
2218 | long fuse_ioctl_common(struct file *file, unsigned int cmd, |
2219 | unsigned long arg, unsigned int flags) | |
d36f2487 | 2220 | { |
6131ffaa | 2221 | struct inode *inode = file_inode(file); |
d36f2487 MS |
2222 | struct fuse_conn *fc = get_fuse_conn(inode); |
2223 | ||
c2132c1b | 2224 | if (!fuse_allow_current_process(fc)) |
d36f2487 MS |
2225 | return -EACCES; |
2226 | ||
2227 | if (is_bad_inode(inode)) | |
2228 | return -EIO; | |
2229 | ||
2230 | return fuse_do_ioctl(file, cmd, arg, flags); | |
2231 | } | |
2232 | ||
59efec7b TH |
2233 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, |
2234 | unsigned long arg) | |
2235 | { | |
b18da0c5 | 2236 | return fuse_ioctl_common(file, cmd, arg, 0); |
59efec7b TH |
2237 | } |
2238 | ||
2239 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
2240 | unsigned long arg) | |
2241 | { | |
b18da0c5 | 2242 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); |
59efec7b TH |
2243 | } |
2244 | ||
95668a69 TH |
2245 | /* |
2246 | * All files which have been polled are linked to RB tree | |
2247 | * fuse_conn->polled_files which is indexed by kh. Walk the tree and | |
2248 | * find the matching one. | |
2249 | */ | |
2250 | static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, | |
2251 | struct rb_node **parent_out) | |
2252 | { | |
2253 | struct rb_node **link = &fc->polled_files.rb_node; | |
2254 | struct rb_node *last = NULL; | |
2255 | ||
2256 | while (*link) { | |
2257 | struct fuse_file *ff; | |
2258 | ||
2259 | last = *link; | |
2260 | ff = rb_entry(last, struct fuse_file, polled_node); | |
2261 | ||
2262 | if (kh < ff->kh) | |
2263 | link = &last->rb_left; | |
2264 | else if (kh > ff->kh) | |
2265 | link = &last->rb_right; | |
2266 | else | |
2267 | return link; | |
2268 | } | |
2269 | ||
2270 | if (parent_out) | |
2271 | *parent_out = last; | |
2272 | return link; | |
2273 | } | |
2274 | ||
2275 | /* | |
2276 | * The file is about to be polled. Make sure it's on the polled_files | |
2277 | * RB tree. Note that files once added to the polled_files tree are | |
2278 | * not removed before the file is released. This is because a file | |
2279 | * polled once is likely to be polled again. | |
2280 | */ | |
2281 | static void fuse_register_polled_file(struct fuse_conn *fc, | |
2282 | struct fuse_file *ff) | |
2283 | { | |
2284 | spin_lock(&fc->lock); | |
2285 | if (RB_EMPTY_NODE(&ff->polled_node)) { | |
2286 | struct rb_node **link, *parent; | |
2287 | ||
2288 | link = fuse_find_polled_node(fc, ff->kh, &parent); | |
2289 | BUG_ON(*link); | |
2290 | rb_link_node(&ff->polled_node, parent, link); | |
2291 | rb_insert_color(&ff->polled_node, &fc->polled_files); | |
2292 | } | |
2293 | spin_unlock(&fc->lock); | |
2294 | } | |
2295 | ||
08cbf542 | 2296 | unsigned fuse_file_poll(struct file *file, poll_table *wait) |
95668a69 | 2297 | { |
95668a69 | 2298 | struct fuse_file *ff = file->private_data; |
797759aa | 2299 | struct fuse_conn *fc = ff->fc; |
95668a69 TH |
2300 | struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; |
2301 | struct fuse_poll_out outarg; | |
2302 | struct fuse_req *req; | |
2303 | int err; | |
2304 | ||
2305 | if (fc->no_poll) | |
2306 | return DEFAULT_POLLMASK; | |
2307 | ||
2308 | poll_wait(file, &ff->poll_wait, wait); | |
0415d291 | 2309 | inarg.events = (__u32)poll_requested_events(wait); |
95668a69 TH |
2310 | |
2311 | /* | |
2312 | * Ask for notification iff there's someone waiting for it. | |
2313 | * The client may ignore the flag and always notify. | |
2314 | */ | |
2315 | if (waitqueue_active(&ff->poll_wait)) { | |
2316 | inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; | |
2317 | fuse_register_polled_file(fc, ff); | |
2318 | } | |
2319 | ||
b111c8c0 | 2320 | req = fuse_get_req_nopages(fc); |
95668a69 | 2321 | if (IS_ERR(req)) |
201fa69a | 2322 | return POLLERR; |
95668a69 TH |
2323 | |
2324 | req->in.h.opcode = FUSE_POLL; | |
797759aa | 2325 | req->in.h.nodeid = ff->nodeid; |
95668a69 TH |
2326 | req->in.numargs = 1; |
2327 | req->in.args[0].size = sizeof(inarg); | |
2328 | req->in.args[0].value = &inarg; | |
2329 | req->out.numargs = 1; | |
2330 | req->out.args[0].size = sizeof(outarg); | |
2331 | req->out.args[0].value = &outarg; | |
b93f858a | 2332 | fuse_request_send(fc, req); |
95668a69 TH |
2333 | err = req->out.h.error; |
2334 | fuse_put_request(fc, req); | |
2335 | ||
2336 | if (!err) | |
2337 | return outarg.revents; | |
2338 | if (err == -ENOSYS) { | |
2339 | fc->no_poll = 1; | |
2340 | return DEFAULT_POLLMASK; | |
2341 | } | |
2342 | return POLLERR; | |
2343 | } | |
08cbf542 | 2344 | EXPORT_SYMBOL_GPL(fuse_file_poll); |
95668a69 TH |
2345 | |
2346 | /* | |
2347 | * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and | |
2348 | * wakes up the poll waiters. | |
2349 | */ | |
2350 | int fuse_notify_poll_wakeup(struct fuse_conn *fc, | |
2351 | struct fuse_notify_poll_wakeup_out *outarg) | |
2352 | { | |
2353 | u64 kh = outarg->kh; | |
2354 | struct rb_node **link; | |
2355 | ||
2356 | spin_lock(&fc->lock); | |
2357 | ||
2358 | link = fuse_find_polled_node(fc, kh, NULL); | |
2359 | if (*link) { | |
2360 | struct fuse_file *ff; | |
2361 | ||
2362 | ff = rb_entry(*link, struct fuse_file, polled_node); | |
2363 | wake_up_interruptible_sync(&ff->poll_wait); | |
2364 | } | |
2365 | ||
2366 | spin_unlock(&fc->lock); | |
2367 | return 0; | |
2368 | } | |
2369 | ||
efb9fa9e MP |
2370 | static void fuse_do_truncate(struct file *file) |
2371 | { | |
2372 | struct inode *inode = file->f_mapping->host; | |
2373 | struct iattr attr; | |
2374 | ||
2375 | attr.ia_valid = ATTR_SIZE; | |
2376 | attr.ia_size = i_size_read(inode); | |
2377 | ||
2378 | attr.ia_file = file; | |
2379 | attr.ia_valid |= ATTR_FILE; | |
2380 | ||
2381 | fuse_do_setattr(inode, &attr, file); | |
2382 | } | |
2383 | ||
e5c5f05d MP |
2384 | static inline loff_t fuse_round_up(loff_t off) |
2385 | { | |
2386 | return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | |
2387 | } | |
2388 | ||
4273b793 AA |
2389 | static ssize_t |
2390 | fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | |
2391 | loff_t offset, unsigned long nr_segs) | |
2392 | { | |
2393 | ssize_t ret = 0; | |
60b9df7a MS |
2394 | struct file *file = iocb->ki_filp; |
2395 | struct fuse_file *ff = file->private_data; | |
e5c5f05d | 2396 | bool async_dio = ff->fc->async_dio; |
4273b793 | 2397 | loff_t pos = 0; |
bcba24cc MP |
2398 | struct inode *inode; |
2399 | loff_t i_size; | |
2400 | size_t count = iov_length(iov, nr_segs); | |
36cf66ed | 2401 | struct fuse_io_priv *io; |
d3005b09 | 2402 | bool is_sync = is_sync_kiocb(iocb); |
4273b793 | 2403 | |
4273b793 | 2404 | pos = offset; |
bcba24cc MP |
2405 | inode = file->f_mapping->host; |
2406 | i_size = i_size_read(inode); | |
4273b793 | 2407 | |
439ee5f0 | 2408 | /* optimization for short read */ |
e5c5f05d | 2409 | if (async_dio && rw != WRITE && offset + count > i_size) { |
439ee5f0 MP |
2410 | if (offset >= i_size) |
2411 | return 0; | |
e5c5f05d | 2412 | count = min_t(loff_t, count, fuse_round_up(i_size - offset)); |
439ee5f0 MP |
2413 | } |
2414 | ||
bcba24cc | 2415 | io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL); |
36cf66ed MP |
2416 | if (!io) |
2417 | return -ENOMEM; | |
bcba24cc MP |
2418 | spin_lock_init(&io->lock); |
2419 | io->reqs = 1; | |
2420 | io->bytes = -1; | |
2421 | io->size = 0; | |
2422 | io->offset = offset; | |
2423 | io->write = (rw == WRITE); | |
2424 | io->err = 0; | |
36cf66ed | 2425 | io->file = file; |
bcba24cc MP |
2426 | /* |
2427 | * By default, we want to optimize all I/Os with async request | |
60b9df7a | 2428 | * submission to the client filesystem if supported. |
bcba24cc | 2429 | */ |
e5c5f05d | 2430 | io->async = async_dio; |
bcba24cc MP |
2431 | io->iocb = iocb; |
2432 | ||
2433 | /* | |
2434 | * We cannot asynchronously extend the size of a file. We have no method | |
2435 | * to wait on real async I/O requests, so we must submit this request | |
2436 | * synchronously. | |
2437 | */ | |
d3005b09 | 2438 | if (!is_sync && (offset + count > i_size) && rw == WRITE) |
bcba24cc | 2439 | io->async = false; |
4273b793 | 2440 | |
b98d023a | 2441 | if (rw == WRITE) |
36cf66ed | 2442 | ret = __fuse_direct_write(io, iov, nr_segs, &pos); |
b98d023a | 2443 | else |
439ee5f0 | 2444 | ret = __fuse_direct_read(io, iov, nr_segs, &pos, count); |
36cf66ed | 2445 | |
bcba24cc MP |
2446 | if (io->async) { |
2447 | fuse_aio_complete(io, ret < 0 ? ret : 0, -1); | |
2448 | ||
2449 | /* we have a non-extending, async request, so return */ | |
d3005b09 | 2450 | if (!is_sync) |
bcba24cc MP |
2451 | return -EIOCBQUEUED; |
2452 | ||
2453 | ret = wait_on_sync_kiocb(iocb); | |
2454 | } else { | |
2455 | kfree(io); | |
2456 | } | |
2457 | ||
efb9fa9e MP |
2458 | if (rw == WRITE) { |
2459 | if (ret > 0) | |
2460 | fuse_write_update_size(inode, pos); | |
2461 | else if (ret < 0 && offset + count > i_size) | |
2462 | fuse_do_truncate(file); | |
2463 | } | |
4273b793 AA |
2464 | |
2465 | return ret; | |
2466 | } | |
2467 | ||
cdadb11c MS |
2468 | static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, |
2469 | loff_t length) | |
05ba1f08 AP |
2470 | { |
2471 | struct fuse_file *ff = file->private_data; | |
3634a632 | 2472 | struct inode *inode = file->f_inode; |
9abd30b4 | 2473 | struct fuse_inode *fi = get_fuse_inode(inode); |
05ba1f08 AP |
2474 | struct fuse_conn *fc = ff->fc; |
2475 | struct fuse_req *req; | |
2476 | struct fuse_fallocate_in inarg = { | |
2477 | .fh = ff->fh, | |
2478 | .offset = offset, | |
2479 | .length = length, | |
2480 | .mode = mode | |
2481 | }; | |
2482 | int err; | |
14c14414 MP |
2483 | bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) || |
2484 | (mode & FALLOC_FL_PUNCH_HOLE); | |
05ba1f08 | 2485 | |
519c6040 MS |
2486 | if (fc->no_fallocate) |
2487 | return -EOPNOTSUPP; | |
2488 | ||
14c14414 | 2489 | if (lock_inode) { |
3634a632 | 2490 | mutex_lock(&inode->i_mutex); |
eb12ca30 MP |
2491 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
2492 | loff_t endbyte = offset + length - 1; | |
2493 | err = filemap_write_and_wait_range(inode->i_mapping, | |
2494 | offset, endbyte); | |
2495 | if (err) | |
2496 | goto out; | |
2497 | ||
2498 | fuse_sync_writes(inode); | |
2499 | } | |
3634a632 BF |
2500 | } |
2501 | ||
9abd30b4 MP |
2502 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
2503 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
2504 | ||
b111c8c0 | 2505 | req = fuse_get_req_nopages(fc); |
3634a632 BF |
2506 | if (IS_ERR(req)) { |
2507 | err = PTR_ERR(req); | |
2508 | goto out; | |
2509 | } | |
05ba1f08 AP |
2510 | |
2511 | req->in.h.opcode = FUSE_FALLOCATE; | |
2512 | req->in.h.nodeid = ff->nodeid; | |
2513 | req->in.numargs = 1; | |
2514 | req->in.args[0].size = sizeof(inarg); | |
2515 | req->in.args[0].value = &inarg; | |
2516 | fuse_request_send(fc, req); | |
2517 | err = req->out.h.error; | |
519c6040 MS |
2518 | if (err == -ENOSYS) { |
2519 | fc->no_fallocate = 1; | |
2520 | err = -EOPNOTSUPP; | |
2521 | } | |
05ba1f08 AP |
2522 | fuse_put_request(fc, req); |
2523 | ||
bee6c307 BF |
2524 | if (err) |
2525 | goto out; | |
2526 | ||
2527 | /* we could have extended the file */ | |
2528 | if (!(mode & FALLOC_FL_KEEP_SIZE)) | |
2529 | fuse_write_update_size(inode, offset + length); | |
2530 | ||
2531 | if (mode & FALLOC_FL_PUNCH_HOLE) | |
2532 | truncate_pagecache_range(inode, offset, offset + length - 1); | |
2533 | ||
2534 | fuse_invalidate_attr(inode); | |
2535 | ||
3634a632 | 2536 | out: |
9abd30b4 MP |
2537 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
2538 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
2539 | ||
eb12ca30 | 2540 | if (lock_inode) |
3634a632 | 2541 | mutex_unlock(&inode->i_mutex); |
3634a632 | 2542 | |
05ba1f08 AP |
2543 | return err; |
2544 | } | |
05ba1f08 | 2545 | |
4b6f5d20 | 2546 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 2547 | .llseek = fuse_file_llseek, |
543ade1f | 2548 | .read = do_sync_read, |
bcb4be80 | 2549 | .aio_read = fuse_file_aio_read, |
543ade1f | 2550 | .write = do_sync_write, |
ea9b9907 | 2551 | .aio_write = fuse_file_aio_write, |
b6aeaded MS |
2552 | .mmap = fuse_file_mmap, |
2553 | .open = fuse_open, | |
2554 | .flush = fuse_flush, | |
2555 | .release = fuse_release, | |
2556 | .fsync = fuse_fsync, | |
71421259 | 2557 | .lock = fuse_file_lock, |
a9ff4f87 | 2558 | .flock = fuse_file_flock, |
5ffc4ef4 | 2559 | .splice_read = generic_file_splice_read, |
59efec7b TH |
2560 | .unlocked_ioctl = fuse_file_ioctl, |
2561 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 2562 | .poll = fuse_file_poll, |
05ba1f08 | 2563 | .fallocate = fuse_file_fallocate, |
b6aeaded MS |
2564 | }; |
2565 | ||
4b6f5d20 | 2566 | static const struct file_operations fuse_direct_io_file_operations = { |
5559b8f4 | 2567 | .llseek = fuse_file_llseek, |
413ef8cb MS |
2568 | .read = fuse_direct_read, |
2569 | .write = fuse_direct_write, | |
fc280c96 | 2570 | .mmap = fuse_direct_mmap, |
413ef8cb MS |
2571 | .open = fuse_open, |
2572 | .flush = fuse_flush, | |
2573 | .release = fuse_release, | |
2574 | .fsync = fuse_fsync, | |
71421259 | 2575 | .lock = fuse_file_lock, |
a9ff4f87 | 2576 | .flock = fuse_file_flock, |
59efec7b TH |
2577 | .unlocked_ioctl = fuse_file_ioctl, |
2578 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 2579 | .poll = fuse_file_poll, |
05ba1f08 | 2580 | .fallocate = fuse_file_fallocate, |
fc280c96 | 2581 | /* no splice_read */ |
413ef8cb MS |
2582 | }; |
2583 | ||
f5e54d6e | 2584 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 2585 | .readpage = fuse_readpage, |
3be5a52b MS |
2586 | .writepage = fuse_writepage, |
2587 | .launder_page = fuse_launder_page, | |
db50b96c | 2588 | .readpages = fuse_readpages, |
3be5a52b | 2589 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 2590 | .bmap = fuse_bmap, |
4273b793 | 2591 | .direct_IO = fuse_direct_IO, |
b6aeaded MS |
2592 | }; |
2593 | ||
2594 | void fuse_init_file_inode(struct inode *inode) | |
2595 | { | |
45323fb7 MS |
2596 | inode->i_fop = &fuse_file_operations; |
2597 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 2598 | } |