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