]>
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> |
7a36094d | 15 | #include <linux/sched/signal.h> |
08cbf542 | 16 | #include <linux/module.h> |
d9d318d3 | 17 | #include <linux/compat.h> |
478e0841 | 18 | #include <linux/swap.h> |
3634a632 | 19 | #include <linux/falloc.h> |
e2e40f2c | 20 | #include <linux/uio.h> |
31070f6c | 21 | #include <linux/fs.h> |
b6aeaded | 22 | |
7213394c MS |
23 | static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags, |
24 | struct fuse_page_desc **desc) | |
4c4f03f7 MS |
25 | { |
26 | struct page **pages; | |
27 | ||
28 | pages = kzalloc(npages * (sizeof(struct page *) + | |
29 | sizeof(struct fuse_page_desc)), flags); | |
30 | *desc = (void *) (pages + npages); | |
31 | ||
32 | return pages; | |
33 | } | |
34 | ||
91fe96b4 MS |
35 | static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
36 | int opcode, struct fuse_open_out *outargp) | |
b6aeaded | 37 | { |
b6aeaded | 38 | struct fuse_open_in inarg; |
7078187a | 39 | FUSE_ARGS(args); |
fd72faac MS |
40 | |
41 | memset(&inarg, 0, sizeof(inarg)); | |
6ff958ed MS |
42 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
43 | if (!fc->atomic_o_trunc) | |
44 | inarg.flags &= ~O_TRUNC; | |
d5b48543 MS |
45 | args.opcode = opcode; |
46 | args.nodeid = nodeid; | |
47 | args.in_numargs = 1; | |
48 | args.in_args[0].size = sizeof(inarg); | |
49 | args.in_args[0].value = &inarg; | |
50 | args.out_numargs = 1; | |
51 | args.out_args[0].size = sizeof(*outargp); | |
52 | args.out_args[0].value = outargp; | |
fd72faac | 53 | |
7078187a | 54 | return fuse_simple_request(fc, &args); |
fd72faac MS |
55 | } |
56 | ||
4cb54866 MS |
57 | struct fuse_release_args { |
58 | struct fuse_args args; | |
59 | struct fuse_release_in inarg; | |
60 | struct inode *inode; | |
61 | }; | |
62 | ||
acf99433 | 63 | struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) |
fd72faac MS |
64 | { |
65 | struct fuse_file *ff; | |
6b2db28a | 66 | |
dc69e98c | 67 | ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT); |
6b2db28a TH |
68 | if (unlikely(!ff)) |
69 | return NULL; | |
70 | ||
da5e4714 | 71 | ff->fc = fc; |
dc69e98c KK |
72 | ff->release_args = kzalloc(sizeof(*ff->release_args), |
73 | GFP_KERNEL_ACCOUNT); | |
4cb54866 | 74 | if (!ff->release_args) { |
6b2db28a TH |
75 | kfree(ff); |
76 | return NULL; | |
fd72faac | 77 | } |
6b2db28a TH |
78 | |
79 | INIT_LIST_HEAD(&ff->write_entry); | |
5d7bc7e8 | 80 | mutex_init(&ff->readdir.lock); |
4e8c2eb5 | 81 | refcount_set(&ff->count, 1); |
6b2db28a TH |
82 | RB_CLEAR_NODE(&ff->polled_node); |
83 | init_waitqueue_head(&ff->poll_wait); | |
84 | ||
75126f55 | 85 | ff->kh = atomic64_inc_return(&fc->khctr); |
6b2db28a | 86 | |
fd72faac MS |
87 | return ff; |
88 | } | |
89 | ||
90 | void fuse_file_free(struct fuse_file *ff) | |
91 | { | |
4cb54866 | 92 | kfree(ff->release_args); |
5d7bc7e8 | 93 | mutex_destroy(&ff->readdir.lock); |
fd72faac MS |
94 | kfree(ff); |
95 | } | |
96 | ||
267d8444 | 97 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) |
c756e0a4 | 98 | { |
4e8c2eb5 | 99 | refcount_inc(&ff->count); |
c756e0a4 MS |
100 | return ff; |
101 | } | |
102 | ||
4cb54866 MS |
103 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_args *args, |
104 | int error) | |
819c4b3b | 105 | { |
4cb54866 MS |
106 | struct fuse_release_args *ra = container_of(args, typeof(*ra), args); |
107 | ||
108 | iput(ra->inode); | |
109 | kfree(ra); | |
819c4b3b MS |
110 | } |
111 | ||
2e64ff15 | 112 | static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir) |
c756e0a4 | 113 | { |
4e8c2eb5 | 114 | if (refcount_dec_and_test(&ff->count)) { |
4cb54866 | 115 | struct fuse_args *args = &ff->release_args->args; |
8b0797a4 | 116 | |
d9a9ea94 | 117 | if (isdir ? ff->fc->no_opendir : ff->fc->no_open) { |
4cb54866 MS |
118 | /* Do nothing when client does not implement 'open' */ |
119 | fuse_release_end(ff->fc, args, 0); | |
7678ac50 | 120 | } else if (sync) { |
4cb54866 MS |
121 | fuse_simple_request(ff->fc, args); |
122 | fuse_release_end(ff->fc, args, 0); | |
5a18ec17 | 123 | } else { |
4cb54866 MS |
124 | args->end = fuse_release_end; |
125 | if (fuse_simple_background(ff->fc, args, | |
126 | GFP_KERNEL | __GFP_NOFAIL)) | |
127 | fuse_release_end(ff->fc, args, -ENOTCONN); | |
5a18ec17 | 128 | } |
c756e0a4 MS |
129 | kfree(ff); |
130 | } | |
131 | } | |
132 | ||
08cbf542 TH |
133 | int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
134 | bool isdir) | |
91fe96b4 | 135 | { |
91fe96b4 | 136 | struct fuse_file *ff; |
91fe96b4 MS |
137 | int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
138 | ||
139 | ff = fuse_file_alloc(fc); | |
140 | if (!ff) | |
141 | return -ENOMEM; | |
142 | ||
7678ac50 | 143 | ff->fh = 0; |
fabf7e02 CA |
144 | /* Default for no-open */ |
145 | ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0); | |
d9a9ea94 | 146 | if (isdir ? !fc->no_opendir : !fc->no_open) { |
7678ac50 AG |
147 | struct fuse_open_out outarg; |
148 | int err; | |
149 | ||
150 | err = fuse_send_open(fc, nodeid, file, opcode, &outarg); | |
151 | if (!err) { | |
152 | ff->fh = outarg.fh; | |
153 | ff->open_flags = outarg.open_flags; | |
154 | ||
d9a9ea94 | 155 | } else if (err != -ENOSYS) { |
7678ac50 AG |
156 | fuse_file_free(ff); |
157 | return err; | |
158 | } else { | |
d9a9ea94 CA |
159 | if (isdir) |
160 | fc->no_opendir = 1; | |
161 | else | |
162 | fc->no_open = 1; | |
7678ac50 | 163 | } |
91fe96b4 MS |
164 | } |
165 | ||
166 | if (isdir) | |
7678ac50 | 167 | ff->open_flags &= ~FOPEN_DIRECT_IO; |
91fe96b4 | 168 | |
91fe96b4 | 169 | ff->nodeid = nodeid; |
267d8444 | 170 | file->private_data = ff; |
91fe96b4 MS |
171 | |
172 | return 0; | |
173 | } | |
08cbf542 | 174 | EXPORT_SYMBOL_GPL(fuse_do_open); |
91fe96b4 | 175 | |
650b22b9 PE |
176 | static void fuse_link_write_file(struct file *file) |
177 | { | |
178 | struct inode *inode = file_inode(file); | |
650b22b9 PE |
179 | struct fuse_inode *fi = get_fuse_inode(inode); |
180 | struct fuse_file *ff = file->private_data; | |
181 | /* | |
182 | * file may be written through mmap, so chain it onto the | |
183 | * inodes's write_file list | |
184 | */ | |
f15ecfef | 185 | spin_lock(&fi->lock); |
650b22b9 PE |
186 | if (list_empty(&ff->write_entry)) |
187 | list_add(&ff->write_entry, &fi->write_files); | |
f15ecfef | 188 | spin_unlock(&fi->lock); |
650b22b9 PE |
189 | } |
190 | ||
c7b7143c | 191 | void fuse_finish_open(struct inode *inode, struct file *file) |
fd72faac | 192 | { |
c7b7143c | 193 | struct fuse_file *ff = file->private_data; |
a0822c55 | 194 | struct fuse_conn *fc = get_fuse_conn(inode); |
c7b7143c | 195 | |
c7b7143c | 196 | if (!(ff->open_flags & FOPEN_KEEP_CACHE)) |
b1009979 | 197 | invalidate_inode_pages2(inode->i_mapping); |
bbd84f33 KS |
198 | if (ff->open_flags & FOPEN_STREAM) |
199 | stream_open(inode, file); | |
200 | else if (ff->open_flags & FOPEN_NONSEEKABLE) | |
a7c1b990 | 201 | nonseekable_open(inode, file); |
a0822c55 KS |
202 | if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) { |
203 | struct fuse_inode *fi = get_fuse_inode(inode); | |
204 | ||
f15ecfef | 205 | spin_lock(&fi->lock); |
4510d86f | 206 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
a0822c55 | 207 | i_size_write(inode, 0); |
f15ecfef | 208 | spin_unlock(&fi->lock); |
a0822c55 | 209 | fuse_invalidate_attr(inode); |
75caeecd MP |
210 | if (fc->writeback_cache) |
211 | file_update_time(file); | |
a0822c55 | 212 | } |
4d99ff8f PE |
213 | if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache) |
214 | fuse_link_write_file(file); | |
fd72faac MS |
215 | } |
216 | ||
91fe96b4 | 217 | int fuse_open_common(struct inode *inode, struct file *file, bool isdir) |
fd72faac | 218 | { |
acf99433 | 219 | struct fuse_conn *fc = get_fuse_conn(inode); |
b6aeaded | 220 | int err; |
e4648309 | 221 | bool is_wb_truncate = (file->f_flags & O_TRUNC) && |
75caeecd MP |
222 | fc->atomic_o_trunc && |
223 | fc->writeback_cache; | |
b6aeaded MS |
224 | |
225 | err = generic_file_open(inode, file); | |
226 | if (err) | |
227 | return err; | |
228 | ||
e4648309 | 229 | if (is_wb_truncate) { |
5955102c | 230 | inode_lock(inode); |
e4648309 MS |
231 | fuse_set_nowrite(inode); |
232 | } | |
75caeecd | 233 | |
91fe96b4 | 234 | err = fuse_do_open(fc, get_node_id(inode), file, isdir); |
b6aeaded | 235 | |
75caeecd MP |
236 | if (!err) |
237 | fuse_finish_open(inode, file); | |
91fe96b4 | 238 | |
e4648309 MS |
239 | if (is_wb_truncate) { |
240 | fuse_release_nowrite(inode); | |
5955102c | 241 | inode_unlock(inode); |
e4648309 | 242 | } |
75caeecd MP |
243 | |
244 | return err; | |
b6aeaded MS |
245 | } |
246 | ||
ebf84d0c KT |
247 | static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff, |
248 | int flags, int opcode) | |
64c6d8ed | 249 | { |
8b0797a4 | 250 | struct fuse_conn *fc = ff->fc; |
4cb54866 | 251 | struct fuse_release_args *ra = ff->release_args; |
b6aeaded | 252 | |
f15ecfef KT |
253 | /* Inode is NULL on error path of fuse_create_open() */ |
254 | if (likely(fi)) { | |
255 | spin_lock(&fi->lock); | |
256 | list_del(&ff->write_entry); | |
257 | spin_unlock(&fi->lock); | |
258 | } | |
8b0797a4 | 259 | spin_lock(&fc->lock); |
8b0797a4 MS |
260 | if (!RB_EMPTY_NODE(&ff->polled_node)) |
261 | rb_erase(&ff->polled_node, &fc->polled_files); | |
262 | spin_unlock(&fc->lock); | |
263 | ||
357ccf2b | 264 | wake_up_interruptible_all(&ff->poll_wait); |
8b0797a4 | 265 | |
4cb54866 MS |
266 | ra->inarg.fh = ff->fh; |
267 | ra->inarg.flags = flags; | |
268 | ra->args.in_numargs = 1; | |
269 | ra->args.in_args[0].size = sizeof(struct fuse_release_in); | |
270 | ra->args.in_args[0].value = &ra->inarg; | |
271 | ra->args.opcode = opcode; | |
272 | ra->args.nodeid = ff->nodeid; | |
273 | ra->args.force = true; | |
274 | ra->args.nocreds = true; | |
fd72faac MS |
275 | } |
276 | ||
2e64ff15 | 277 | void fuse_release_common(struct file *file, bool isdir) |
fd72faac | 278 | { |
ebf84d0c | 279 | struct fuse_inode *fi = get_fuse_inode(file_inode(file)); |
9a87ad3d | 280 | struct fuse_file *ff = file->private_data; |
4cb54866 | 281 | struct fuse_release_args *ra = ff->release_args; |
2e64ff15 | 282 | int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; |
6b2db28a | 283 | |
ebf84d0c | 284 | fuse_prepare_release(fi, ff, file->f_flags, opcode); |
6b2db28a | 285 | |
37fb3a30 | 286 | if (ff->flock) { |
4cb54866 MS |
287 | ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; |
288 | ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc, | |
289 | (fl_owner_t) file); | |
37fb3a30 | 290 | } |
baebccbe | 291 | /* Hold inode until release is finished */ |
4cb54866 | 292 | ra->inode = igrab(file_inode(file)); |
6b2db28a | 293 | |
6b2db28a TH |
294 | /* |
295 | * Normally this will send the RELEASE request, however if | |
296 | * some asynchronous READ or WRITE requests are outstanding, | |
297 | * the sending will be delayed. | |
5a18ec17 MS |
298 | * |
299 | * Make the release synchronous if this is a fuseblk mount, | |
300 | * synchronous RELEASE is allowed (and desirable) in this case | |
301 | * because the server can be trusted not to screw up. | |
6b2db28a | 302 | */ |
1ccd1ea2 | 303 | fuse_file_put(ff, ff->fc->destroy, isdir); |
b6aeaded MS |
304 | } |
305 | ||
04730fef MS |
306 | static int fuse_open(struct inode *inode, struct file *file) |
307 | { | |
91fe96b4 | 308 | return fuse_open_common(inode, file, false); |
04730fef MS |
309 | } |
310 | ||
311 | static int fuse_release(struct inode *inode, struct file *file) | |
312 | { | |
e7cc133c PE |
313 | struct fuse_conn *fc = get_fuse_conn(inode); |
314 | ||
315 | /* see fuse_vma_close() for !writeback_cache case */ | |
316 | if (fc->writeback_cache) | |
1e18bda8 | 317 | write_inode_now(inode, 1); |
b0aa7606 | 318 | |
2e64ff15 | 319 | fuse_release_common(file, false); |
8b0797a4 MS |
320 | |
321 | /* return value is ignored by VFS */ | |
322 | return 0; | |
323 | } | |
324 | ||
ebf84d0c | 325 | void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags) |
8b0797a4 | 326 | { |
4e8c2eb5 | 327 | WARN_ON(refcount_read(&ff->count) > 1); |
ebf84d0c | 328 | fuse_prepare_release(fi, ff, flags, FUSE_RELEASE); |
267d8444 MS |
329 | /* |
330 | * iput(NULL) is a no-op and since the refcount is 1 and everything's | |
331 | * synchronous, we are fine with not doing igrab() here" | |
332 | */ | |
2e64ff15 | 333 | fuse_file_put(ff, true, false); |
04730fef | 334 | } |
08cbf542 | 335 | EXPORT_SYMBOL_GPL(fuse_sync_release); |
04730fef | 336 | |
71421259 | 337 | /* |
9c8ef561 MS |
338 | * Scramble the ID space with XTEA, so that the value of the files_struct |
339 | * pointer is not exposed to userspace. | |
71421259 | 340 | */ |
f3332114 | 341 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 342 | { |
9c8ef561 MS |
343 | u32 *k = fc->scramble_key; |
344 | u64 v = (unsigned long) id; | |
345 | u32 v0 = v; | |
346 | u32 v1 = v >> 32; | |
347 | u32 sum = 0; | |
348 | int i; | |
349 | ||
350 | for (i = 0; i < 32; i++) { | |
351 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
352 | sum += 0x9E3779B9; | |
353 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
354 | } | |
355 | ||
356 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
357 | } |
358 | ||
33826ebb MS |
359 | struct fuse_writepage_args { |
360 | struct fuse_io_args ia; | |
6b2fb799 | 361 | struct rb_node writepages_entry; |
33826ebb MS |
362 | struct list_head queue_entry; |
363 | struct fuse_writepage_args *next; | |
364 | struct inode *inode; | |
365 | }; | |
366 | ||
367 | static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi, | |
2fe93bd4 MS |
368 | pgoff_t idx_from, pgoff_t idx_to) |
369 | { | |
6b2fb799 MP |
370 | struct rb_node *n; |
371 | ||
372 | n = fi->writepages.rb_node; | |
2fe93bd4 | 373 | |
6b2fb799 MP |
374 | while (n) { |
375 | struct fuse_writepage_args *wpa; | |
2fe93bd4 MS |
376 | pgoff_t curr_index; |
377 | ||
6b2fb799 | 378 | wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry); |
33826ebb MS |
379 | WARN_ON(get_fuse_inode(wpa->inode) != fi); |
380 | curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT; | |
6b2fb799 MP |
381 | if (idx_from >= curr_index + wpa->ia.ap.num_pages) |
382 | n = n->rb_right; | |
383 | else if (idx_to < curr_index) | |
384 | n = n->rb_left; | |
385 | else | |
33826ebb | 386 | return wpa; |
2fe93bd4 MS |
387 | } |
388 | return NULL; | |
389 | } | |
390 | ||
3be5a52b | 391 | /* |
ea8cd333 | 392 | * Check if any page in a range is under writeback |
3be5a52b MS |
393 | * |
394 | * This is currently done by walking the list of writepage requests | |
395 | * for the inode, which can be pretty inefficient. | |
396 | */ | |
ea8cd333 PE |
397 | static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from, |
398 | pgoff_t idx_to) | |
3be5a52b | 399 | { |
3be5a52b | 400 | struct fuse_inode *fi = get_fuse_inode(inode); |
2fe93bd4 | 401 | bool found; |
3be5a52b | 402 | |
f15ecfef | 403 | spin_lock(&fi->lock); |
2fe93bd4 | 404 | found = fuse_find_writeback(fi, idx_from, idx_to); |
f15ecfef | 405 | spin_unlock(&fi->lock); |
3be5a52b MS |
406 | |
407 | return found; | |
408 | } | |
409 | ||
ea8cd333 PE |
410 | static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) |
411 | { | |
412 | return fuse_range_is_writeback(inode, index, index); | |
413 | } | |
414 | ||
3be5a52b MS |
415 | /* |
416 | * Wait for page writeback to be completed. | |
417 | * | |
418 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
419 | * use some other means. | |
420 | */ | |
17b2cbe2 | 421 | static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) |
3be5a52b MS |
422 | { |
423 | struct fuse_inode *fi = get_fuse_inode(inode); | |
424 | ||
425 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
3be5a52b MS |
426 | } |
427 | ||
fe38d7df MP |
428 | /* |
429 | * Wait for all pending writepages on the inode to finish. | |
430 | * | |
431 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
432 | * and waiting for all sent writes to complete. | |
433 | * | |
434 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
435 | * could conflict with truncation. | |
436 | */ | |
437 | static void fuse_sync_writes(struct inode *inode) | |
438 | { | |
439 | fuse_set_nowrite(inode); | |
440 | fuse_release_nowrite(inode); | |
441 | } | |
442 | ||
75e1fcc0 | 443 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 444 | { |
6131ffaa | 445 | struct inode *inode = file_inode(file); |
b6aeaded MS |
446 | struct fuse_conn *fc = get_fuse_conn(inode); |
447 | struct fuse_file *ff = file->private_data; | |
b6aeaded | 448 | struct fuse_flush_in inarg; |
c500ebaa | 449 | FUSE_ARGS(args); |
b6aeaded MS |
450 | int err; |
451 | ||
248d86e8 MS |
452 | if (is_bad_inode(inode)) |
453 | return -EIO; | |
454 | ||
1e18bda8 | 455 | err = write_inode_now(inode, 1); |
fe38d7df MP |
456 | if (err) |
457 | return err; | |
458 | ||
5955102c | 459 | inode_lock(inode); |
fe38d7df | 460 | fuse_sync_writes(inode); |
5955102c | 461 | inode_unlock(inode); |
fe38d7df | 462 | |
4a7f4e88 | 463 | err = filemap_check_errors(file->f_mapping); |
9ebce595 MP |
464 | if (err) |
465 | return err; | |
466 | ||
614c026e MS |
467 | err = 0; |
468 | if (fc->no_flush) | |
469 | goto inval_attr_out; | |
470 | ||
b6aeaded MS |
471 | memset(&inarg, 0, sizeof(inarg)); |
472 | inarg.fh = ff->fh; | |
9c8ef561 | 473 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
c500ebaa MS |
474 | args.opcode = FUSE_FLUSH; |
475 | args.nodeid = get_node_id(inode); | |
476 | args.in_numargs = 1; | |
477 | args.in_args[0].size = sizeof(inarg); | |
478 | args.in_args[0].value = &inarg; | |
479 | args.force = true; | |
480 | ||
481 | err = fuse_simple_request(fc, &args); | |
b6aeaded MS |
482 | if (err == -ENOSYS) { |
483 | fc->no_flush = 1; | |
484 | err = 0; | |
485 | } | |
cf576c58 EG |
486 | |
487 | inval_attr_out: | |
488 | /* | |
489 | * In memory i_blocks is not maintained by fuse, if writeback cache is | |
490 | * enabled, i_blocks from cached attr may not be accurate. | |
491 | */ | |
492 | if (!err && fc->writeback_cache) | |
493 | fuse_invalidate_attr(inode); | |
b6aeaded MS |
494 | return err; |
495 | } | |
496 | ||
02c24a82 | 497 | int fuse_fsync_common(struct file *file, loff_t start, loff_t end, |
a9c2d1e8 | 498 | int datasync, int opcode) |
b6aeaded | 499 | { |
7ea80859 | 500 | struct inode *inode = file->f_mapping->host; |
b6aeaded MS |
501 | struct fuse_conn *fc = get_fuse_conn(inode); |
502 | struct fuse_file *ff = file->private_data; | |
7078187a | 503 | FUSE_ARGS(args); |
b6aeaded | 504 | struct fuse_fsync_in inarg; |
a9c2d1e8 MS |
505 | |
506 | memset(&inarg, 0, sizeof(inarg)); | |
507 | inarg.fh = ff->fh; | |
154603fe | 508 | inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0; |
d5b48543 MS |
509 | args.opcode = opcode; |
510 | args.nodeid = get_node_id(inode); | |
511 | args.in_numargs = 1; | |
512 | args.in_args[0].size = sizeof(inarg); | |
513 | args.in_args[0].value = &inarg; | |
a9c2d1e8 MS |
514 | return fuse_simple_request(fc, &args); |
515 | } | |
516 | ||
517 | static int fuse_fsync(struct file *file, loff_t start, loff_t end, | |
518 | int datasync) | |
519 | { | |
520 | struct inode *inode = file->f_mapping->host; | |
521 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b6aeaded MS |
522 | int err; |
523 | ||
248d86e8 MS |
524 | if (is_bad_inode(inode)) |
525 | return -EIO; | |
526 | ||
5955102c | 527 | inode_lock(inode); |
02c24a82 | 528 | |
3be5a52b MS |
529 | /* |
530 | * Start writeback against all dirty pages of the inode, then | |
531 | * wait for all outstanding writes, before sending the FSYNC | |
532 | * request. | |
533 | */ | |
7e51fe1d | 534 | err = file_write_and_wait_range(file, start, end); |
3be5a52b | 535 | if (err) |
02c24a82 | 536 | goto out; |
3be5a52b MS |
537 | |
538 | fuse_sync_writes(inode); | |
ac7f052b AK |
539 | |
540 | /* | |
541 | * Due to implementation of fuse writeback | |
7e51fe1d | 542 | * file_write_and_wait_range() does not catch errors. |
ac7f052b AK |
543 | * We have to do this directly after fuse_sync_writes() |
544 | */ | |
7e51fe1d | 545 | err = file_check_and_advance_wb_err(file); |
ac7f052b AK |
546 | if (err) |
547 | goto out; | |
548 | ||
1e18bda8 MS |
549 | err = sync_inode_metadata(inode, 1); |
550 | if (err) | |
551 | goto out; | |
3be5a52b | 552 | |
a9c2d1e8 | 553 | if (fc->no_fsync) |
22401e7b | 554 | goto out; |
b0aa7606 | 555 | |
a9c2d1e8 | 556 | err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC); |
b6aeaded | 557 | if (err == -ENOSYS) { |
a9c2d1e8 | 558 | fc->no_fsync = 1; |
b6aeaded MS |
559 | err = 0; |
560 | } | |
02c24a82 | 561 | out: |
5955102c | 562 | inode_unlock(inode); |
b6aeaded | 563 | |
a9c2d1e8 | 564 | return err; |
82547981 MS |
565 | } |
566 | ||
00793ca5 MS |
567 | void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos, |
568 | size_t count, int opcode) | |
569 | { | |
570 | struct fuse_file *ff = file->private_data; | |
571 | struct fuse_args *args = &ia->ap.args; | |
572 | ||
573 | ia->read.in.fh = ff->fh; | |
574 | ia->read.in.offset = pos; | |
575 | ia->read.in.size = count; | |
576 | ia->read.in.flags = file->f_flags; | |
577 | args->opcode = opcode; | |
578 | args->nodeid = ff->nodeid; | |
579 | args->in_numargs = 1; | |
580 | args->in_args[0].size = sizeof(ia->read.in); | |
581 | args->in_args[0].value = &ia->read.in; | |
582 | args->out_argvar = true; | |
583 | args->out_numargs = 1; | |
584 | args->out_args[0].size = count; | |
585 | } | |
586 | ||
45ac96ed MS |
587 | static void fuse_release_user_pages(struct fuse_args_pages *ap, |
588 | bool should_dirty) | |
187c5c36 | 589 | { |
45ac96ed | 590 | unsigned int i; |
187c5c36 | 591 | |
45ac96ed | 592 | for (i = 0; i < ap->num_pages; i++) { |
8fba54ae | 593 | if (should_dirty) |
45ac96ed MS |
594 | set_page_dirty_lock(ap->pages[i]); |
595 | put_page(ap->pages[i]); | |
187c5c36 MP |
596 | } |
597 | } | |
598 | ||
744742d6 SF |
599 | static void fuse_io_release(struct kref *kref) |
600 | { | |
601 | kfree(container_of(kref, struct fuse_io_priv, refcnt)); | |
602 | } | |
603 | ||
9d5722b7 CH |
604 | static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io) |
605 | { | |
606 | if (io->err) | |
607 | return io->err; | |
608 | ||
609 | if (io->bytes >= 0 && io->write) | |
610 | return -EIO; | |
611 | ||
612 | return io->bytes < 0 ? io->size : io->bytes; | |
613 | } | |
614 | ||
01e9d11a MP |
615 | /** |
616 | * In case of short read, the caller sets 'pos' to the position of | |
617 | * actual end of fuse request in IO request. Otherwise, if bytes_requested | |
618 | * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1. | |
619 | * | |
620 | * An example: | |
621 | * User requested DIO read of 64K. It was splitted into two 32K fuse requests, | |
622 | * both submitted asynchronously. The first of them was ACKed by userspace as | |
623 | * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The | |
624 | * second request was ACKed as short, e.g. only 1K was read, resulting in | |
625 | * pos == 33K. | |
626 | * | |
627 | * Thus, when all fuse requests are completed, the minimal non-negative 'pos' | |
628 | * will be equal to the length of the longest contiguous fragment of | |
629 | * transferred data starting from the beginning of IO request. | |
630 | */ | |
631 | static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos) | |
632 | { | |
633 | int left; | |
634 | ||
635 | spin_lock(&io->lock); | |
636 | if (err) | |
637 | io->err = io->err ? : err; | |
638 | else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes)) | |
639 | io->bytes = pos; | |
640 | ||
641 | left = --io->reqs; | |
7879c4e5 | 642 | if (!left && io->blocking) |
9d5722b7 | 643 | complete(io->done); |
01e9d11a MP |
644 | spin_unlock(&io->lock); |
645 | ||
7879c4e5 | 646 | if (!left && !io->blocking) { |
9d5722b7 | 647 | ssize_t res = fuse_get_res_by_io(io); |
01e9d11a | 648 | |
9d5722b7 CH |
649 | if (res >= 0) { |
650 | struct inode *inode = file_inode(io->iocb->ki_filp); | |
651 | struct fuse_conn *fc = get_fuse_conn(inode); | |
652 | struct fuse_inode *fi = get_fuse_inode(inode); | |
01e9d11a | 653 | |
f15ecfef | 654 | spin_lock(&fi->lock); |
4510d86f | 655 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
f15ecfef | 656 | spin_unlock(&fi->lock); |
01e9d11a MP |
657 | } |
658 | ||
04b2fa9f | 659 | io->iocb->ki_complete(io->iocb, res, 0); |
01e9d11a | 660 | } |
744742d6 SF |
661 | |
662 | kref_put(&io->refcnt, fuse_io_release); | |
01e9d11a MP |
663 | } |
664 | ||
45ac96ed MS |
665 | static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io, |
666 | unsigned int npages) | |
667 | { | |
668 | struct fuse_io_args *ia; | |
669 | ||
670 | ia = kzalloc(sizeof(*ia), GFP_KERNEL); | |
671 | if (ia) { | |
672 | ia->io = io; | |
673 | ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL, | |
674 | &ia->ap.descs); | |
675 | if (!ia->ap.pages) { | |
676 | kfree(ia); | |
677 | ia = NULL; | |
678 | } | |
679 | } | |
680 | return ia; | |
681 | } | |
682 | ||
683 | static void fuse_io_free(struct fuse_io_args *ia) | |
01e9d11a | 684 | { |
45ac96ed MS |
685 | kfree(ia->ap.pages); |
686 | kfree(ia); | |
687 | } | |
688 | ||
689 | static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args, | |
690 | int err) | |
691 | { | |
692 | struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args); | |
693 | struct fuse_io_priv *io = ia->io; | |
01e9d11a MP |
694 | ssize_t pos = -1; |
695 | ||
45ac96ed | 696 | fuse_release_user_pages(&ia->ap, io->should_dirty); |
01e9d11a | 697 | |
45ac96ed MS |
698 | if (err) { |
699 | /* Nothing */ | |
700 | } else if (io->write) { | |
701 | if (ia->write.out.size > ia->write.in.size) { | |
702 | err = -EIO; | |
703 | } else if (ia->write.in.size != ia->write.out.size) { | |
704 | pos = ia->write.in.offset - io->offset + | |
705 | ia->write.out.size; | |
706 | } | |
01e9d11a | 707 | } else { |
45ac96ed MS |
708 | u32 outsize = args->out_args[0].size; |
709 | ||
710 | if (ia->read.in.size != outsize) | |
711 | pos = ia->read.in.offset - io->offset + outsize; | |
01e9d11a MP |
712 | } |
713 | ||
45ac96ed MS |
714 | fuse_aio_complete(io, err, pos); |
715 | fuse_io_free(ia); | |
01e9d11a MP |
716 | } |
717 | ||
45ac96ed MS |
718 | static ssize_t fuse_async_req_send(struct fuse_conn *fc, |
719 | struct fuse_io_args *ia, size_t num_bytes) | |
01e9d11a | 720 | { |
45ac96ed MS |
721 | ssize_t err; |
722 | struct fuse_io_priv *io = ia->io; | |
723 | ||
01e9d11a | 724 | spin_lock(&io->lock); |
744742d6 | 725 | kref_get(&io->refcnt); |
01e9d11a MP |
726 | io->size += num_bytes; |
727 | io->reqs++; | |
728 | spin_unlock(&io->lock); | |
729 | ||
45ac96ed | 730 | ia->ap.args.end = fuse_aio_complete_req; |
bb737bbe | 731 | ia->ap.args.may_block = io->should_dirty; |
45ac96ed | 732 | err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL); |
f1ebdeff MS |
733 | if (err) |
734 | fuse_aio_complete_req(fc, &ia->ap.args, err); | |
01e9d11a | 735 | |
f1ebdeff | 736 | return num_bytes; |
01e9d11a MP |
737 | } |
738 | ||
45ac96ed MS |
739 | static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count, |
740 | fl_owner_t owner) | |
04730fef | 741 | { |
45ac96ed | 742 | struct file *file = ia->io->iocb->ki_filp; |
2106cb18 MS |
743 | struct fuse_file *ff = file->private_data; |
744 | struct fuse_conn *fc = ff->fc; | |
f3332114 | 745 | |
45ac96ed | 746 | fuse_read_args_fill(ia, file, pos, count, FUSE_READ); |
f3332114 | 747 | if (owner != NULL) { |
45ac96ed MS |
748 | ia->read.in.read_flags |= FUSE_READ_LOCKOWNER; |
749 | ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner); | |
f3332114 | 750 | } |
36cf66ed | 751 | |
45ac96ed MS |
752 | if (ia->io->async) |
753 | return fuse_async_req_send(fc, ia, count); | |
36cf66ed | 754 | |
45ac96ed | 755 | return fuse_simple_request(fc, &ia->ap.args); |
04730fef MS |
756 | } |
757 | ||
5c5c5e51 MS |
758 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
759 | u64 attr_ver) | |
760 | { | |
761 | struct fuse_conn *fc = get_fuse_conn(inode); | |
762 | struct fuse_inode *fi = get_fuse_inode(inode); | |
763 | ||
f15ecfef | 764 | spin_lock(&fi->lock); |
06a7c3c2 MP |
765 | if (attr_ver == fi->attr_version && size < inode->i_size && |
766 | !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { | |
4510d86f | 767 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
5c5c5e51 MS |
768 | i_size_write(inode, size); |
769 | } | |
f15ecfef | 770 | spin_unlock(&fi->lock); |
5c5c5e51 MS |
771 | } |
772 | ||
a0d45d84 | 773 | static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read, |
134831e3 | 774 | struct fuse_args_pages *ap) |
a92adc82 | 775 | { |
8373200b PE |
776 | struct fuse_conn *fc = get_fuse_conn(inode); |
777 | ||
778 | if (fc->writeback_cache) { | |
779 | /* | |
780 | * A hole in a file. Some data after the hole are in page cache, | |
781 | * but have not reached the client fs yet. So, the hole is not | |
782 | * present there. | |
783 | */ | |
784 | int i; | |
09cbfeaf KS |
785 | int start_idx = num_read >> PAGE_SHIFT; |
786 | size_t off = num_read & (PAGE_SIZE - 1); | |
a92adc82 | 787 | |
134831e3 MS |
788 | for (i = start_idx; i < ap->num_pages; i++) { |
789 | zero_user_segment(ap->pages[i], off, PAGE_SIZE); | |
8373200b PE |
790 | off = 0; |
791 | } | |
792 | } else { | |
134831e3 | 793 | loff_t pos = page_offset(ap->pages[0]) + num_read; |
8373200b PE |
794 | fuse_read_update_size(inode, pos, attr_ver); |
795 | } | |
a92adc82 PE |
796 | } |
797 | ||
482fce55 | 798 | static int fuse_do_readpage(struct file *file, struct page *page) |
b6aeaded MS |
799 | { |
800 | struct inode *inode = page->mapping->host; | |
801 | struct fuse_conn *fc = get_fuse_conn(inode); | |
5c5c5e51 | 802 | loff_t pos = page_offset(page); |
00793ca5 MS |
803 | struct fuse_page_desc desc = { .length = PAGE_SIZE }; |
804 | struct fuse_io_args ia = { | |
805 | .ap.args.page_zeroing = true, | |
806 | .ap.args.out_pages = true, | |
807 | .ap.num_pages = 1, | |
808 | .ap.pages = &page, | |
809 | .ap.descs = &desc, | |
810 | }; | |
811 | ssize_t res; | |
5c5c5e51 | 812 | u64 attr_ver; |
248d86e8 | 813 | |
3be5a52b | 814 | /* |
25985edc | 815 | * Page writeback can extend beyond the lifetime of the |
3be5a52b MS |
816 | * page-cache page, so make sure we read a properly synced |
817 | * page. | |
818 | */ | |
819 | fuse_wait_on_page_writeback(inode, page->index); | |
820 | ||
5c5c5e51 MS |
821 | attr_ver = fuse_get_attr_version(fc); |
822 | ||
2f139829 MS |
823 | /* Don't overflow end offset */ |
824 | if (pos + (desc.length - 1) == LLONG_MAX) | |
825 | desc.length--; | |
826 | ||
00793ca5 MS |
827 | fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ); |
828 | res = fuse_simple_request(fc, &ia.ap.args); | |
829 | if (res < 0) | |
830 | return res; | |
831 | /* | |
832 | * Short read means EOF. If file size is larger, truncate it | |
833 | */ | |
834 | if (res < desc.length) | |
134831e3 | 835 | fuse_short_read(inode, attr_ver, res, &ia.ap); |
5c5c5e51 | 836 | |
00793ca5 | 837 | SetPageUptodate(page); |
482fce55 | 838 | |
00793ca5 | 839 | return 0; |
482fce55 MP |
840 | } |
841 | ||
842 | static int fuse_readpage(struct file *file, struct page *page) | |
843 | { | |
844 | struct inode *inode = page->mapping->host; | |
845 | int err; | |
846 | ||
847 | err = -EIO; | |
848 | if (is_bad_inode(inode)) | |
849 | goto out; | |
850 | ||
851 | err = fuse_do_readpage(file, page); | |
451418fc | 852 | fuse_invalidate_atime(inode); |
b6aeaded MS |
853 | out: |
854 | unlock_page(page); | |
855 | return err; | |
856 | } | |
857 | ||
134831e3 MS |
858 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args, |
859 | int err) | |
db50b96c | 860 | { |
c1aa96a5 | 861 | int i; |
134831e3 MS |
862 | struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args); |
863 | struct fuse_args_pages *ap = &ia->ap; | |
864 | size_t count = ia->read.in.size; | |
865 | size_t num_read = args->out_args[0].size; | |
ce534fb0 | 866 | struct address_space *mapping = NULL; |
c1aa96a5 | 867 | |
134831e3 MS |
868 | for (i = 0; mapping == NULL && i < ap->num_pages; i++) |
869 | mapping = ap->pages[i]->mapping; | |
5c5c5e51 | 870 | |
ce534fb0 MS |
871 | if (mapping) { |
872 | struct inode *inode = mapping->host; | |
873 | ||
874 | /* | |
875 | * Short read means EOF. If file size is larger, truncate it | |
876 | */ | |
134831e3 MS |
877 | if (!err && num_read < count) |
878 | fuse_short_read(inode, ia->read.attr_ver, num_read, ap); | |
ce534fb0 | 879 | |
451418fc | 880 | fuse_invalidate_atime(inode); |
ce534fb0 | 881 | } |
c1aa96a5 | 882 | |
134831e3 MS |
883 | for (i = 0; i < ap->num_pages; i++) { |
884 | struct page *page = ap->pages[i]; | |
885 | ||
886 | if (!err) | |
db50b96c | 887 | SetPageUptodate(page); |
c1aa96a5 MS |
888 | else |
889 | SetPageError(page); | |
db50b96c | 890 | unlock_page(page); |
09cbfeaf | 891 | put_page(page); |
db50b96c | 892 | } |
134831e3 MS |
893 | if (ia->ff) |
894 | fuse_file_put(ia->ff, false, false); | |
895 | ||
896 | fuse_io_free(ia); | |
c1aa96a5 MS |
897 | } |
898 | ||
134831e3 | 899 | static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file) |
c1aa96a5 | 900 | { |
2106cb18 MS |
901 | struct fuse_file *ff = file->private_data; |
902 | struct fuse_conn *fc = ff->fc; | |
134831e3 MS |
903 | struct fuse_args_pages *ap = &ia->ap; |
904 | loff_t pos = page_offset(ap->pages[0]); | |
905 | size_t count = ap->num_pages << PAGE_SHIFT; | |
7df1e988 | 906 | ssize_t res; |
134831e3 MS |
907 | int err; |
908 | ||
909 | ap->args.out_pages = true; | |
910 | ap->args.page_zeroing = true; | |
911 | ap->args.page_replace = true; | |
2f139829 MS |
912 | |
913 | /* Don't overflow end offset */ | |
914 | if (pos + (count - 1) == LLONG_MAX) { | |
915 | count--; | |
916 | ap->descs[ap->num_pages - 1].length--; | |
917 | } | |
918 | WARN_ON((loff_t) (pos + count) < 0); | |
919 | ||
134831e3 MS |
920 | fuse_read_args_fill(ia, file, pos, count, FUSE_READ); |
921 | ia->read.attr_ver = fuse_get_attr_version(fc); | |
9cd68455 | 922 | if (fc->async_read) { |
134831e3 MS |
923 | ia->ff = fuse_file_get(ff); |
924 | ap->args.end = fuse_readpages_end; | |
925 | err = fuse_simple_background(fc, &ap->args, GFP_KERNEL); | |
926 | if (!err) | |
927 | return; | |
9cd68455 | 928 | } else { |
7df1e988 MS |
929 | res = fuse_simple_request(fc, &ap->args); |
930 | err = res < 0 ? res : 0; | |
9cd68455 | 931 | } |
134831e3 | 932 | fuse_readpages_end(fc, &ap->args, err); |
db50b96c MS |
933 | } |
934 | ||
76a0294e | 935 | static void fuse_readahead(struct readahead_control *rac) |
db50b96c | 936 | { |
76a0294e | 937 | struct inode *inode = rac->mapping->host; |
db50b96c | 938 | struct fuse_conn *fc = get_fuse_conn(inode); |
76a0294e | 939 | unsigned int i, max_pages, nr_pages = 0; |
db50b96c | 940 | |
248d86e8 | 941 | if (is_bad_inode(inode)) |
76a0294e | 942 | return; |
248d86e8 | 943 | |
76a0294e MWO |
944 | max_pages = min_t(unsigned int, fc->max_pages, |
945 | fc->max_read / PAGE_SIZE); | |
db50b96c | 946 | |
76a0294e MWO |
947 | for (;;) { |
948 | struct fuse_io_args *ia; | |
949 | struct fuse_args_pages *ap; | |
950 | ||
951 | nr_pages = readahead_count(rac) - nr_pages; | |
952 | if (nr_pages > max_pages) | |
953 | nr_pages = max_pages; | |
954 | if (nr_pages == 0) | |
955 | break; | |
956 | ia = fuse_io_alloc(NULL, nr_pages); | |
957 | if (!ia) | |
958 | return; | |
959 | ap = &ia->ap; | |
960 | nr_pages = __readahead_batch(rac, ap->pages, nr_pages); | |
961 | for (i = 0; i < nr_pages; i++) { | |
962 | fuse_wait_on_page_writeback(inode, | |
963 | readahead_index(rac) + i); | |
964 | ap->descs[i].length = PAGE_SIZE; | |
965 | } | |
966 | ap->num_pages = nr_pages; | |
967 | fuse_send_readpages(ia, rac->file); | |
d3406ffa | 968 | } |
db50b96c MS |
969 | } |
970 | ||
55752a3a | 971 | static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to) |
bcb4be80 MS |
972 | { |
973 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
a8894274 | 974 | struct fuse_conn *fc = get_fuse_conn(inode); |
bcb4be80 | 975 | |
a8894274 BF |
976 | /* |
977 | * In auto invalidate mode, always update attributes on read. | |
978 | * Otherwise, only update if we attempt to read past EOF (to ensure | |
979 | * i_size is up to date). | |
980 | */ | |
981 | if (fc->auto_inval_data || | |
37c20f16 | 982 | (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) { |
bcb4be80 | 983 | int err; |
5b97eeac | 984 | err = fuse_update_attributes(inode, iocb->ki_filp); |
bcb4be80 MS |
985 | if (err) |
986 | return err; | |
987 | } | |
988 | ||
37c20f16 | 989 | return generic_file_read_iter(iocb, to); |
bcb4be80 MS |
990 | } |
991 | ||
338f2e3f MS |
992 | static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff, |
993 | loff_t pos, size_t count) | |
994 | { | |
995 | struct fuse_args *args = &ia->ap.args; | |
996 | ||
997 | ia->write.in.fh = ff->fh; | |
998 | ia->write.in.offset = pos; | |
999 | ia->write.in.size = count; | |
1000 | args->opcode = FUSE_WRITE; | |
1001 | args->nodeid = ff->nodeid; | |
1002 | args->in_numargs = 2; | |
1003 | if (ff->fc->minor < 9) | |
1004 | args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; | |
1005 | else | |
1006 | args->in_args[0].size = sizeof(ia->write.in); | |
1007 | args->in_args[0].value = &ia->write.in; | |
1008 | args->in_args[1].size = count; | |
1009 | args->out_numargs = 1; | |
1010 | args->out_args[0].size = sizeof(ia->write.out); | |
1011 | args->out_args[0].value = &ia->write.out; | |
1012 | } | |
1013 | ||
1014 | static unsigned int fuse_write_flags(struct kiocb *iocb) | |
1015 | { | |
1016 | unsigned int flags = iocb->ki_filp->f_flags; | |
1017 | ||
1018 | if (iocb->ki_flags & IOCB_DSYNC) | |
1019 | flags |= O_DSYNC; | |
1020 | if (iocb->ki_flags & IOCB_SYNC) | |
1021 | flags |= O_SYNC; | |
1022 | ||
1023 | return flags; | |
1024 | } | |
1025 | ||
45ac96ed MS |
1026 | static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos, |
1027 | size_t count, fl_owner_t owner) | |
b25e82e5 | 1028 | { |
45ac96ed | 1029 | struct kiocb *iocb = ia->io->iocb; |
e1c0eecb | 1030 | struct file *file = iocb->ki_filp; |
2106cb18 MS |
1031 | struct fuse_file *ff = file->private_data; |
1032 | struct fuse_conn *fc = ff->fc; | |
45ac96ed MS |
1033 | struct fuse_write_in *inarg = &ia->write.in; |
1034 | ssize_t err; | |
2d698b07 | 1035 | |
45ac96ed | 1036 | fuse_write_args_fill(ia, ff, pos, count); |
338f2e3f | 1037 | inarg->flags = fuse_write_flags(iocb); |
f3332114 | 1038 | if (owner != NULL) { |
f3332114 MS |
1039 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
1040 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
1041 | } | |
36cf66ed | 1042 | |
45ac96ed MS |
1043 | if (ia->io->async) |
1044 | return fuse_async_req_send(fc, ia, count); | |
1045 | ||
1046 | err = fuse_simple_request(fc, &ia->ap.args); | |
1047 | if (!err && ia->write.out.size > count) | |
1048 | err = -EIO; | |
36cf66ed | 1049 | |
45ac96ed | 1050 | return err ?: ia->write.out.size; |
b6aeaded MS |
1051 | } |
1052 | ||
b0aa7606 | 1053 | bool fuse_write_update_size(struct inode *inode, loff_t pos) |
854512ec MS |
1054 | { |
1055 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1056 | struct fuse_inode *fi = get_fuse_inode(inode); | |
b0aa7606 | 1057 | bool ret = false; |
854512ec | 1058 | |
f15ecfef | 1059 | spin_lock(&fi->lock); |
4510d86f | 1060 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
b0aa7606 | 1061 | if (pos > inode->i_size) { |
854512ec | 1062 | i_size_write(inode, pos); |
b0aa7606 MP |
1063 | ret = true; |
1064 | } | |
f15ecfef | 1065 | spin_unlock(&fi->lock); |
b0aa7606 MP |
1066 | |
1067 | return ret; | |
854512ec MS |
1068 | } |
1069 | ||
338f2e3f MS |
1070 | static ssize_t fuse_send_write_pages(struct fuse_io_args *ia, |
1071 | struct kiocb *iocb, struct inode *inode, | |
1072 | loff_t pos, size_t count) | |
ea9b9907 | 1073 | { |
338f2e3f MS |
1074 | struct fuse_args_pages *ap = &ia->ap; |
1075 | struct file *file = iocb->ki_filp; | |
1076 | struct fuse_file *ff = file->private_data; | |
1077 | struct fuse_conn *fc = ff->fc; | |
1078 | unsigned int offset, i; | |
1079 | int err; | |
ea9b9907 | 1080 | |
338f2e3f MS |
1081 | for (i = 0; i < ap->num_pages; i++) |
1082 | fuse_wait_on_page_writeback(inode, ap->pages[i]->index); | |
ea9b9907 | 1083 | |
338f2e3f MS |
1084 | fuse_write_args_fill(ia, ff, pos, count); |
1085 | ia->write.in.flags = fuse_write_flags(iocb); | |
ea9b9907 | 1086 | |
338f2e3f | 1087 | err = fuse_simple_request(fc, &ap->args); |
8aab336b MS |
1088 | if (!err && ia->write.out.size > count) |
1089 | err = -EIO; | |
338f2e3f MS |
1090 | |
1091 | offset = ap->descs[0].offset; | |
1092 | count = ia->write.out.size; | |
1093 | for (i = 0; i < ap->num_pages; i++) { | |
1094 | struct page *page = ap->pages[i]; | |
ea9b9907 | 1095 | |
338f2e3f | 1096 | if (!err && !offset && count >= PAGE_SIZE) |
ea9b9907 NP |
1097 | SetPageUptodate(page); |
1098 | ||
09cbfeaf KS |
1099 | if (count > PAGE_SIZE - offset) |
1100 | count -= PAGE_SIZE - offset; | |
ea9b9907 NP |
1101 | else |
1102 | count = 0; | |
1103 | offset = 0; | |
1104 | ||
1105 | unlock_page(page); | |
09cbfeaf | 1106 | put_page(page); |
ea9b9907 NP |
1107 | } |
1108 | ||
338f2e3f | 1109 | return err; |
ea9b9907 NP |
1110 | } |
1111 | ||
338f2e3f MS |
1112 | static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap, |
1113 | struct address_space *mapping, | |
1114 | struct iov_iter *ii, loff_t pos, | |
1115 | unsigned int max_pages) | |
ea9b9907 NP |
1116 | { |
1117 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
09cbfeaf | 1118 | unsigned offset = pos & (PAGE_SIZE - 1); |
ea9b9907 NP |
1119 | size_t count = 0; |
1120 | int err; | |
1121 | ||
338f2e3f MS |
1122 | ap->args.in_pages = true; |
1123 | ap->descs[0].offset = offset; | |
ea9b9907 NP |
1124 | |
1125 | do { | |
1126 | size_t tmp; | |
1127 | struct page *page; | |
09cbfeaf KS |
1128 | pgoff_t index = pos >> PAGE_SHIFT; |
1129 | size_t bytes = min_t(size_t, PAGE_SIZE - offset, | |
ea9b9907 NP |
1130 | iov_iter_count(ii)); |
1131 | ||
1132 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
1133 | ||
1134 | again: | |
1135 | err = -EFAULT; | |
1136 | if (iov_iter_fault_in_readable(ii, bytes)) | |
1137 | break; | |
1138 | ||
1139 | err = -ENOMEM; | |
54566b2c | 1140 | page = grab_cache_page_write_begin(mapping, index, 0); |
ea9b9907 NP |
1141 | if (!page) |
1142 | break; | |
1143 | ||
931e80e4 | 1144 | if (mapping_writably_mapped(mapping)) |
1145 | flush_dcache_page(page); | |
1146 | ||
ea9b9907 | 1147 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); |
ea9b9907 NP |
1148 | flush_dcache_page(page); |
1149 | ||
3ca8138f | 1150 | iov_iter_advance(ii, tmp); |
ea9b9907 NP |
1151 | if (!tmp) { |
1152 | unlock_page(page); | |
09cbfeaf | 1153 | put_page(page); |
ea9b9907 NP |
1154 | bytes = min(bytes, iov_iter_single_seg_count(ii)); |
1155 | goto again; | |
1156 | } | |
1157 | ||
1158 | err = 0; | |
338f2e3f MS |
1159 | ap->pages[ap->num_pages] = page; |
1160 | ap->descs[ap->num_pages].length = tmp; | |
1161 | ap->num_pages++; | |
ea9b9907 | 1162 | |
ea9b9907 NP |
1163 | count += tmp; |
1164 | pos += tmp; | |
1165 | offset += tmp; | |
09cbfeaf | 1166 | if (offset == PAGE_SIZE) |
ea9b9907 NP |
1167 | offset = 0; |
1168 | ||
78bb6cb9 MS |
1169 | if (!fc->big_writes) |
1170 | break; | |
ea9b9907 | 1171 | } while (iov_iter_count(ii) && count < fc->max_write && |
338f2e3f | 1172 | ap->num_pages < max_pages && offset == 0); |
ea9b9907 NP |
1173 | |
1174 | return count > 0 ? count : err; | |
1175 | } | |
1176 | ||
5da784cc CS |
1177 | static inline unsigned int fuse_wr_pages(loff_t pos, size_t len, |
1178 | unsigned int max_pages) | |
d07f09f5 | 1179 | { |
5da784cc | 1180 | return min_t(unsigned int, |
09cbfeaf KS |
1181 | ((pos + len - 1) >> PAGE_SHIFT) - |
1182 | (pos >> PAGE_SHIFT) + 1, | |
5da784cc | 1183 | max_pages); |
d07f09f5 MP |
1184 | } |
1185 | ||
e1c0eecb | 1186 | static ssize_t fuse_perform_write(struct kiocb *iocb, |
ea9b9907 NP |
1187 | struct address_space *mapping, |
1188 | struct iov_iter *ii, loff_t pos) | |
1189 | { | |
1190 | struct inode *inode = mapping->host; | |
1191 | struct fuse_conn *fc = get_fuse_conn(inode); | |
06a7c3c2 | 1192 | struct fuse_inode *fi = get_fuse_inode(inode); |
ea9b9907 NP |
1193 | int err = 0; |
1194 | ssize_t res = 0; | |
1195 | ||
06a7c3c2 MP |
1196 | if (inode->i_size < pos + iov_iter_count(ii)) |
1197 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
1198 | ||
ea9b9907 | 1199 | do { |
ea9b9907 | 1200 | ssize_t count; |
338f2e3f MS |
1201 | struct fuse_io_args ia = {}; |
1202 | struct fuse_args_pages *ap = &ia.ap; | |
5da784cc CS |
1203 | unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii), |
1204 | fc->max_pages); | |
ea9b9907 | 1205 | |
338f2e3f MS |
1206 | ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs); |
1207 | if (!ap->pages) { | |
1208 | err = -ENOMEM; | |
ea9b9907 NP |
1209 | break; |
1210 | } | |
1211 | ||
338f2e3f | 1212 | count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages); |
ea9b9907 NP |
1213 | if (count <= 0) { |
1214 | err = count; | |
1215 | } else { | |
338f2e3f MS |
1216 | err = fuse_send_write_pages(&ia, iocb, inode, |
1217 | pos, count); | |
ea9b9907 | 1218 | if (!err) { |
338f2e3f MS |
1219 | size_t num_written = ia.write.out.size; |
1220 | ||
ea9b9907 NP |
1221 | res += num_written; |
1222 | pos += num_written; | |
1223 | ||
1224 | /* break out of the loop on short write */ | |
1225 | if (num_written != count) | |
1226 | err = -EIO; | |
1227 | } | |
1228 | } | |
338f2e3f | 1229 | kfree(ap->pages); |
ea9b9907 NP |
1230 | } while (!err && iov_iter_count(ii)); |
1231 | ||
1232 | if (res > 0) | |
1233 | fuse_write_update_size(inode, pos); | |
1234 | ||
06a7c3c2 | 1235 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
ea9b9907 NP |
1236 | fuse_invalidate_attr(inode); |
1237 | ||
1238 | return res > 0 ? res : err; | |
1239 | } | |
1240 | ||
55752a3a | 1241 | static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) |
ea9b9907 NP |
1242 | { |
1243 | struct file *file = iocb->ki_filp; | |
1244 | struct address_space *mapping = file->f_mapping; | |
ea9b9907 | 1245 | ssize_t written = 0; |
4273b793 | 1246 | ssize_t written_buffered = 0; |
ea9b9907 NP |
1247 | struct inode *inode = mapping->host; |
1248 | ssize_t err; | |
4273b793 | 1249 | loff_t endbyte = 0; |
ea9b9907 | 1250 | |
4d99ff8f PE |
1251 | if (get_fuse_conn(inode)->writeback_cache) { |
1252 | /* Update size (EOF optimization) and mode (SUID clearing) */ | |
5b97eeac | 1253 | err = fuse_update_attributes(mapping->host, file); |
4d99ff8f PE |
1254 | if (err) |
1255 | return err; | |
1256 | ||
84c3d55c | 1257 | return generic_file_write_iter(iocb, from); |
4d99ff8f PE |
1258 | } |
1259 | ||
5955102c | 1260 | inode_lock(inode); |
ea9b9907 NP |
1261 | |
1262 | /* We can write back this queue in page reclaim */ | |
de1414a6 | 1263 | current->backing_dev_info = inode_to_bdi(inode); |
ea9b9907 | 1264 | |
3309dd04 AV |
1265 | err = generic_write_checks(iocb, from); |
1266 | if (err <= 0) | |
ea9b9907 NP |
1267 | goto out; |
1268 | ||
5fa8e0a1 | 1269 | err = file_remove_privs(file); |
ea9b9907 NP |
1270 | if (err) |
1271 | goto out; | |
1272 | ||
c3b2da31 JB |
1273 | err = file_update_time(file); |
1274 | if (err) | |
1275 | goto out; | |
ea9b9907 | 1276 | |
2ba48ce5 | 1277 | if (iocb->ki_flags & IOCB_DIRECT) { |
3309dd04 | 1278 | loff_t pos = iocb->ki_pos; |
1af5bb49 | 1279 | written = generic_file_direct_write(iocb, from); |
84c3d55c | 1280 | if (written < 0 || !iov_iter_count(from)) |
4273b793 AA |
1281 | goto out; |
1282 | ||
1283 | pos += written; | |
ea9b9907 | 1284 | |
e1c0eecb | 1285 | written_buffered = fuse_perform_write(iocb, mapping, from, pos); |
4273b793 AA |
1286 | if (written_buffered < 0) { |
1287 | err = written_buffered; | |
1288 | goto out; | |
1289 | } | |
1290 | endbyte = pos + written_buffered - 1; | |
1291 | ||
1292 | err = filemap_write_and_wait_range(file->f_mapping, pos, | |
1293 | endbyte); | |
1294 | if (err) | |
1295 | goto out; | |
1296 | ||
1297 | invalidate_mapping_pages(file->f_mapping, | |
09cbfeaf KS |
1298 | pos >> PAGE_SHIFT, |
1299 | endbyte >> PAGE_SHIFT); | |
4273b793 AA |
1300 | |
1301 | written += written_buffered; | |
1302 | iocb->ki_pos = pos + written_buffered; | |
1303 | } else { | |
e1c0eecb | 1304 | written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos); |
4273b793 | 1305 | if (written >= 0) |
3309dd04 | 1306 | iocb->ki_pos += written; |
4273b793 | 1307 | } |
ea9b9907 NP |
1308 | out: |
1309 | current->backing_dev_info = NULL; | |
5955102c | 1310 | inode_unlock(inode); |
e1c0eecb MS |
1311 | if (written > 0) |
1312 | written = generic_write_sync(iocb, written); | |
ea9b9907 NP |
1313 | |
1314 | return written ? written : err; | |
1315 | } | |
1316 | ||
093f38a2 MS |
1317 | static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs, |
1318 | unsigned int index, | |
1319 | unsigned int nr_pages) | |
85f40aec MP |
1320 | { |
1321 | int i; | |
1322 | ||
7c190c8b | 1323 | for (i = index; i < index + nr_pages; i++) |
093f38a2 | 1324 | descs[i].length = PAGE_SIZE - descs[i].offset; |
85f40aec MP |
1325 | } |
1326 | ||
7c190c8b MP |
1327 | static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) |
1328 | { | |
1329 | return (unsigned long)ii->iov->iov_base + ii->iov_offset; | |
1330 | } | |
1331 | ||
1332 | static inline size_t fuse_get_frag_size(const struct iov_iter *ii, | |
1333 | size_t max_size) | |
1334 | { | |
1335 | return min(iov_iter_single_seg_count(ii), max_size); | |
1336 | } | |
1337 | ||
45ac96ed MS |
1338 | static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, |
1339 | size_t *nbytesp, int write, | |
1340 | unsigned int max_pages) | |
413ef8cb | 1341 | { |
7c190c8b | 1342 | size_t nbytes = 0; /* # bytes already packed in req */ |
742f9927 | 1343 | ssize_t ret = 0; |
b98d023a | 1344 | |
f4975c67 | 1345 | /* Special case for kernel I/O: can copy directly into the buffer */ |
00e23707 | 1346 | if (iov_iter_is_kvec(ii)) { |
7c190c8b MP |
1347 | unsigned long user_addr = fuse_get_user_addr(ii); |
1348 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp); | |
1349 | ||
f4975c67 | 1350 | if (write) |
45ac96ed | 1351 | ap->args.in_args[1].value = (void *) user_addr; |
f4975c67 | 1352 | else |
45ac96ed | 1353 | ap->args.out_args[0].value = (void *) user_addr; |
f4975c67 | 1354 | |
b98d023a MP |
1355 | iov_iter_advance(ii, frag_size); |
1356 | *nbytesp = frag_size; | |
f4975c67 MS |
1357 | return 0; |
1358 | } | |
413ef8cb | 1359 | |
45ac96ed | 1360 | while (nbytes < *nbytesp && ap->num_pages < max_pages) { |
7c190c8b | 1361 | unsigned npages; |
f67da30c | 1362 | size_t start; |
45ac96ed | 1363 | ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages], |
2c80929c | 1364 | *nbytesp - nbytes, |
45ac96ed | 1365 | max_pages - ap->num_pages, |
c7f3888a | 1366 | &start); |
7c190c8b | 1367 | if (ret < 0) |
742f9927 | 1368 | break; |
7c190c8b | 1369 | |
c9c37e2e AV |
1370 | iov_iter_advance(ii, ret); |
1371 | nbytes += ret; | |
7c190c8b | 1372 | |
c9c37e2e AV |
1373 | ret += start; |
1374 | npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE; | |
7c190c8b | 1375 | |
45ac96ed MS |
1376 | ap->descs[ap->num_pages].offset = start; |
1377 | fuse_page_descs_length_init(ap->descs, ap->num_pages, npages); | |
7c190c8b | 1378 | |
45ac96ed MS |
1379 | ap->num_pages += npages; |
1380 | ap->descs[ap->num_pages - 1].length -= | |
c9c37e2e | 1381 | (PAGE_SIZE - ret) & (PAGE_SIZE - 1); |
7c190c8b | 1382 | } |
f4975c67 MS |
1383 | |
1384 | if (write) | |
cabdb4fa | 1385 | ap->args.in_pages = true; |
f4975c67 | 1386 | else |
cabdb4fa | 1387 | ap->args.out_pages = true; |
f4975c67 | 1388 | |
7c190c8b | 1389 | *nbytesp = nbytes; |
f4975c67 | 1390 | |
2c932d4c | 1391 | return ret < 0 ? ret : 0; |
413ef8cb MS |
1392 | } |
1393 | ||
d22a943f AV |
1394 | ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, |
1395 | loff_t *ppos, int flags) | |
413ef8cb | 1396 | { |
ea8cd333 PE |
1397 | int write = flags & FUSE_DIO_WRITE; |
1398 | int cuse = flags & FUSE_DIO_CUSE; | |
e1c0eecb | 1399 | struct file *file = io->iocb->ki_filp; |
ea8cd333 | 1400 | struct inode *inode = file->f_mapping->host; |
2106cb18 MS |
1401 | struct fuse_file *ff = file->private_data; |
1402 | struct fuse_conn *fc = ff->fc; | |
413ef8cb MS |
1403 | size_t nmax = write ? fc->max_write : fc->max_read; |
1404 | loff_t pos = *ppos; | |
d22a943f | 1405 | size_t count = iov_iter_count(iter); |
09cbfeaf KS |
1406 | pgoff_t idx_from = pos >> PAGE_SHIFT; |
1407 | pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT; | |
413ef8cb | 1408 | ssize_t res = 0; |
742f9927 | 1409 | int err = 0; |
45ac96ed MS |
1410 | struct fuse_io_args *ia; |
1411 | unsigned int max_pages; | |
248d86e8 | 1412 | |
45ac96ed MS |
1413 | max_pages = iov_iter_npages(iter, fc->max_pages); |
1414 | ia = fuse_io_alloc(io, max_pages); | |
1415 | if (!ia) | |
1416 | return -ENOMEM; | |
413ef8cb | 1417 | |
45ac96ed | 1418 | ia->io = io; |
ea8cd333 PE |
1419 | if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) { |
1420 | if (!write) | |
5955102c | 1421 | inode_lock(inode); |
ea8cd333 PE |
1422 | fuse_sync_writes(inode); |
1423 | if (!write) | |
5955102c | 1424 | inode_unlock(inode); |
ea8cd333 PE |
1425 | } |
1426 | ||
61c12b49 | 1427 | io->should_dirty = !write && iter_is_iovec(iter); |
413ef8cb | 1428 | while (count) { |
45ac96ed | 1429 | ssize_t nres; |
2106cb18 | 1430 | fl_owner_t owner = current->files; |
f4975c67 | 1431 | size_t nbytes = min(count, nmax); |
45ac96ed MS |
1432 | |
1433 | err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write, | |
1434 | max_pages); | |
742f9927 | 1435 | if (err && !nbytes) |
413ef8cb | 1436 | break; |
f4975c67 | 1437 | |
4a2abf99 | 1438 | if (write) { |
45ac96ed MS |
1439 | if (!capable(CAP_FSETID)) |
1440 | ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV; | |
4a2abf99 | 1441 | |
45ac96ed | 1442 | nres = fuse_send_write(ia, pos, nbytes, owner); |
4a2abf99 | 1443 | } else { |
45ac96ed | 1444 | nres = fuse_send_read(ia, pos, nbytes, owner); |
4a2abf99 | 1445 | } |
2106cb18 | 1446 | |
45ac96ed MS |
1447 | if (!io->async || nres < 0) { |
1448 | fuse_release_user_pages(&ia->ap, io->should_dirty); | |
1449 | fuse_io_free(ia); | |
1450 | } | |
1451 | ia = NULL; | |
1452 | if (nres < 0) { | |
f658adee | 1453 | iov_iter_revert(iter, nbytes); |
45ac96ed | 1454 | err = nres; |
413ef8cb MS |
1455 | break; |
1456 | } | |
45ac96ed MS |
1457 | WARN_ON(nres > nbytes); |
1458 | ||
413ef8cb MS |
1459 | count -= nres; |
1460 | res += nres; | |
1461 | pos += nres; | |
f658adee MS |
1462 | if (nres != nbytes) { |
1463 | iov_iter_revert(iter, nbytes - nres); | |
413ef8cb | 1464 | break; |
f658adee | 1465 | } |
56cf34ff | 1466 | if (count) { |
45ac96ed MS |
1467 | max_pages = iov_iter_npages(iter, fc->max_pages); |
1468 | ia = fuse_io_alloc(io, max_pages); | |
1469 | if (!ia) | |
56cf34ff MS |
1470 | break; |
1471 | } | |
413ef8cb | 1472 | } |
45ac96ed MS |
1473 | if (ia) |
1474 | fuse_io_free(ia); | |
d09cb9d7 | 1475 | if (res > 0) |
413ef8cb | 1476 | *ppos = pos; |
413ef8cb | 1477 | |
742f9927 | 1478 | return res > 0 ? res : err; |
413ef8cb | 1479 | } |
08cbf542 | 1480 | EXPORT_SYMBOL_GPL(fuse_direct_io); |
413ef8cb | 1481 | |
36cf66ed | 1482 | static ssize_t __fuse_direct_read(struct fuse_io_priv *io, |
d22a943f AV |
1483 | struct iov_iter *iter, |
1484 | loff_t *ppos) | |
413ef8cb | 1485 | { |
d09cb9d7 | 1486 | ssize_t res; |
e1c0eecb | 1487 | struct inode *inode = file_inode(io->iocb->ki_filp); |
d09cb9d7 | 1488 | |
d22a943f | 1489 | res = fuse_direct_io(io, iter, ppos, 0); |
d09cb9d7 | 1490 | |
9a2eb24d | 1491 | fuse_invalidate_atime(inode); |
d09cb9d7 MS |
1492 | |
1493 | return res; | |
413ef8cb MS |
1494 | } |
1495 | ||
23c94e1c MR |
1496 | static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter); |
1497 | ||
15316263 | 1498 | static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) |
b98d023a | 1499 | { |
23c94e1c MR |
1500 | ssize_t res; |
1501 | ||
1502 | if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { | |
23c94e1c MR |
1503 | res = fuse_direct_IO(iocb, to); |
1504 | } else { | |
1505 | struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); | |
1506 | ||
1507 | res = __fuse_direct_read(&io, to, &iocb->ki_pos); | |
1508 | } | |
1509 | ||
1510 | return res; | |
b98d023a MP |
1511 | } |
1512 | ||
15316263 | 1513 | static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) |
4273b793 | 1514 | { |
e1c0eecb MS |
1515 | struct inode *inode = file_inode(iocb->ki_filp); |
1516 | struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); | |
15316263 | 1517 | ssize_t res; |
4273b793 | 1518 | |
4273b793 | 1519 | /* Don't allow parallel writes to the same file */ |
5955102c | 1520 | inode_lock(inode); |
3309dd04 | 1521 | res = generic_write_checks(iocb, from); |
23c94e1c MR |
1522 | if (res > 0) { |
1523 | if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { | |
1524 | res = fuse_direct_IO(iocb, from); | |
1525 | } else { | |
1526 | res = fuse_direct_io(&io, from, &iocb->ki_pos, | |
1527 | FUSE_DIO_WRITE); | |
1528 | } | |
1529 | } | |
812408fb | 1530 | fuse_invalidate_attr(inode); |
bcba24cc | 1531 | if (res > 0) |
15316263 | 1532 | fuse_write_update_size(inode, iocb->ki_pos); |
5955102c | 1533 | inode_unlock(inode); |
4273b793 AA |
1534 | |
1535 | return res; | |
1536 | } | |
1537 | ||
55752a3a MS |
1538 | static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
1539 | { | |
2f7b6f5b MS |
1540 | struct file *file = iocb->ki_filp; |
1541 | struct fuse_file *ff = file->private_data; | |
1542 | ||
1543 | if (is_bad_inode(file_inode(file))) | |
1544 | return -EIO; | |
55752a3a MS |
1545 | |
1546 | if (!(ff->open_flags & FOPEN_DIRECT_IO)) | |
1547 | return fuse_cache_read_iter(iocb, to); | |
1548 | else | |
1549 | return fuse_direct_read_iter(iocb, to); | |
1550 | } | |
1551 | ||
1552 | static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
1553 | { | |
2f7b6f5b MS |
1554 | struct file *file = iocb->ki_filp; |
1555 | struct fuse_file *ff = file->private_data; | |
1556 | ||
1557 | if (is_bad_inode(file_inode(file))) | |
1558 | return -EIO; | |
55752a3a MS |
1559 | |
1560 | if (!(ff->open_flags & FOPEN_DIRECT_IO)) | |
1561 | return fuse_cache_write_iter(iocb, from); | |
1562 | else | |
1563 | return fuse_direct_write_iter(iocb, from); | |
1564 | } | |
1565 | ||
33826ebb | 1566 | static void fuse_writepage_free(struct fuse_writepage_args *wpa) |
b6aeaded | 1567 | { |
33826ebb | 1568 | struct fuse_args_pages *ap = &wpa->ia.ap; |
385b1268 PE |
1569 | int i; |
1570 | ||
33826ebb MS |
1571 | for (i = 0; i < ap->num_pages; i++) |
1572 | __free_page(ap->pages[i]); | |
1573 | ||
1574 | if (wpa->ia.ff) | |
1575 | fuse_file_put(wpa->ia.ff, false, false); | |
8b284dc4 | 1576 | |
33826ebb MS |
1577 | kfree(ap->pages); |
1578 | kfree(wpa); | |
3be5a52b MS |
1579 | } |
1580 | ||
33826ebb MS |
1581 | static void fuse_writepage_finish(struct fuse_conn *fc, |
1582 | struct fuse_writepage_args *wpa) | |
3be5a52b | 1583 | { |
33826ebb MS |
1584 | struct fuse_args_pages *ap = &wpa->ia.ap; |
1585 | struct inode *inode = wpa->inode; | |
3be5a52b | 1586 | struct fuse_inode *fi = get_fuse_inode(inode); |
de1414a6 | 1587 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
385b1268 | 1588 | int i; |
3be5a52b | 1589 | |
33826ebb | 1590 | for (i = 0; i < ap->num_pages; i++) { |
93f78d88 | 1591 | dec_wb_stat(&bdi->wb, WB_WRITEBACK); |
33826ebb | 1592 | dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP); |
93f78d88 | 1593 | wb_writeout_inc(&bdi->wb); |
385b1268 | 1594 | } |
3be5a52b MS |
1595 | wake_up(&fi->page_waitq); |
1596 | } | |
1597 | ||
f15ecfef | 1598 | /* Called under fi->lock, may release and reacquire it */ |
33826ebb MS |
1599 | static void fuse_send_writepage(struct fuse_conn *fc, |
1600 | struct fuse_writepage_args *wpa, loff_t size) | |
f15ecfef KT |
1601 | __releases(fi->lock) |
1602 | __acquires(fi->lock) | |
3be5a52b | 1603 | { |
33826ebb MS |
1604 | struct fuse_writepage_args *aux, *next; |
1605 | struct fuse_inode *fi = get_fuse_inode(wpa->inode); | |
1606 | struct fuse_write_in *inarg = &wpa->ia.write.in; | |
1607 | struct fuse_args *args = &wpa->ia.ap.args; | |
1608 | __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE; | |
1609 | int err; | |
3be5a52b | 1610 | |
33826ebb | 1611 | fi->writectr++; |
385b1268 PE |
1612 | if (inarg->offset + data_size <= size) { |
1613 | inarg->size = data_size; | |
3be5a52b | 1614 | } else if (inarg->offset < size) { |
385b1268 | 1615 | inarg->size = size - inarg->offset; |
3be5a52b MS |
1616 | } else { |
1617 | /* Got truncated off completely */ | |
1618 | goto out_free; | |
b6aeaded | 1619 | } |
3be5a52b | 1620 | |
33826ebb MS |
1621 | args->in_args[1].size = inarg->size; |
1622 | args->force = true; | |
1623 | args->nocreds = true; | |
1624 | ||
1625 | err = fuse_simple_background(fc, args, GFP_ATOMIC); | |
1626 | if (err == -ENOMEM) { | |
1627 | spin_unlock(&fi->lock); | |
1628 | err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL); | |
1629 | spin_lock(&fi->lock); | |
1630 | } | |
1631 | ||
f15ecfef | 1632 | /* Fails on broken connection only */ |
33826ebb | 1633 | if (unlikely(err)) |
f15ecfef KT |
1634 | goto out_free; |
1635 | ||
3be5a52b MS |
1636 | return; |
1637 | ||
1638 | out_free: | |
33826ebb | 1639 | fi->writectr--; |
69a6487a | 1640 | rb_erase(&wpa->writepages_entry, &fi->writepages); |
33826ebb | 1641 | fuse_writepage_finish(fc, wpa); |
f15ecfef | 1642 | spin_unlock(&fi->lock); |
e2653bd5 MS |
1643 | |
1644 | /* After fuse_writepage_finish() aux request list is private */ | |
33826ebb MS |
1645 | for (aux = wpa->next; aux; aux = next) { |
1646 | next = aux->next; | |
1647 | aux->next = NULL; | |
1648 | fuse_writepage_free(aux); | |
e2653bd5 MS |
1649 | } |
1650 | ||
33826ebb | 1651 | fuse_writepage_free(wpa); |
f15ecfef | 1652 | spin_lock(&fi->lock); |
b6aeaded MS |
1653 | } |
1654 | ||
3be5a52b MS |
1655 | /* |
1656 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1657 | * all queued writepage requests. | |
1658 | * | |
f15ecfef | 1659 | * Called with fi->lock |
3be5a52b MS |
1660 | */ |
1661 | void fuse_flush_writepages(struct inode *inode) | |
f15ecfef KT |
1662 | __releases(fi->lock) |
1663 | __acquires(fi->lock) | |
b6aeaded | 1664 | { |
3be5a52b MS |
1665 | struct fuse_conn *fc = get_fuse_conn(inode); |
1666 | struct fuse_inode *fi = get_fuse_inode(inode); | |
9de5be06 | 1667 | loff_t crop = i_size_read(inode); |
33826ebb | 1668 | struct fuse_writepage_args *wpa; |
3be5a52b MS |
1669 | |
1670 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
33826ebb MS |
1671 | wpa = list_entry(fi->queued_writes.next, |
1672 | struct fuse_writepage_args, queue_entry); | |
1673 | list_del_init(&wpa->queue_entry); | |
1674 | fuse_send_writepage(fc, wpa, crop); | |
3be5a52b MS |
1675 | } |
1676 | } | |
1677 | ||
c146024e MS |
1678 | static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root, |
1679 | struct fuse_writepage_args *wpa) | |
6b2fb799 MP |
1680 | { |
1681 | pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT; | |
1682 | pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1; | |
1683 | struct rb_node **p = &root->rb_node; | |
1684 | struct rb_node *parent = NULL; | |
1685 | ||
1686 | WARN_ON(!wpa->ia.ap.num_pages); | |
1687 | while (*p) { | |
1688 | struct fuse_writepage_args *curr; | |
1689 | pgoff_t curr_index; | |
1690 | ||
1691 | parent = *p; | |
1692 | curr = rb_entry(parent, struct fuse_writepage_args, | |
1693 | writepages_entry); | |
1694 | WARN_ON(curr->inode != wpa->inode); | |
1695 | curr_index = curr->ia.write.in.offset >> PAGE_SHIFT; | |
1696 | ||
1697 | if (idx_from >= curr_index + curr->ia.ap.num_pages) | |
1698 | p = &(*p)->rb_right; | |
1699 | else if (idx_to < curr_index) | |
1700 | p = &(*p)->rb_left; | |
1701 | else | |
c146024e | 1702 | return curr; |
6b2fb799 MP |
1703 | } |
1704 | ||
1705 | rb_link_node(&wpa->writepages_entry, parent, p); | |
1706 | rb_insert_color(&wpa->writepages_entry, root); | |
c146024e MS |
1707 | return NULL; |
1708 | } | |
1709 | ||
1710 | static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa) | |
1711 | { | |
1712 | WARN_ON(fuse_insert_writeback(root, wpa)); | |
6b2fb799 MP |
1713 | } |
1714 | ||
33826ebb MS |
1715 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args, |
1716 | int error) | |
3be5a52b | 1717 | { |
33826ebb MS |
1718 | struct fuse_writepage_args *wpa = |
1719 | container_of(args, typeof(*wpa), ia.ap.args); | |
1720 | struct inode *inode = wpa->inode; | |
3be5a52b MS |
1721 | struct fuse_inode *fi = get_fuse_inode(inode); |
1722 | ||
33826ebb | 1723 | mapping_set_error(inode->i_mapping, error); |
f15ecfef | 1724 | spin_lock(&fi->lock); |
69a6487a | 1725 | rb_erase(&wpa->writepages_entry, &fi->writepages); |
33826ebb | 1726 | while (wpa->next) { |
6eaf4782 | 1727 | struct fuse_conn *fc = get_fuse_conn(inode); |
33826ebb MS |
1728 | struct fuse_write_in *inarg = &wpa->ia.write.in; |
1729 | struct fuse_writepage_args *next = wpa->next; | |
1730 | ||
1731 | wpa->next = next->next; | |
1732 | next->next = NULL; | |
1733 | next->ia.ff = fuse_file_get(wpa->ia.ff); | |
6b2fb799 | 1734 | tree_insert(&fi->writepages, next); |
6eaf4782 MP |
1735 | |
1736 | /* | |
1737 | * Skip fuse_flush_writepages() to make it easy to crop requests | |
1738 | * based on primary request size. | |
1739 | * | |
1740 | * 1st case (trivial): there are no concurrent activities using | |
1741 | * fuse_set/release_nowrite. Then we're on safe side because | |
1742 | * fuse_flush_writepages() would call fuse_send_writepage() | |
1743 | * anyway. | |
1744 | * | |
1745 | * 2nd case: someone called fuse_set_nowrite and it is waiting | |
1746 | * now for completion of all in-flight requests. This happens | |
1747 | * rarely and no more than once per page, so this should be | |
1748 | * okay. | |
1749 | * | |
1750 | * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle | |
1751 | * of fuse_set_nowrite..fuse_release_nowrite section. The fact | |
1752 | * that fuse_set_nowrite returned implies that all in-flight | |
1753 | * requests were completed along with all of their secondary | |
1754 | * requests. Further primary requests are blocked by negative | |
1755 | * writectr. Hence there cannot be any in-flight requests and | |
1756 | * no invocations of fuse_writepage_end() while we're in | |
1757 | * fuse_set_nowrite..fuse_release_nowrite section. | |
1758 | */ | |
1759 | fuse_send_writepage(fc, next, inarg->offset + inarg->size); | |
8b284dc4 | 1760 | } |
3be5a52b | 1761 | fi->writectr--; |
33826ebb | 1762 | fuse_writepage_finish(fc, wpa); |
f15ecfef | 1763 | spin_unlock(&fi->lock); |
33826ebb | 1764 | fuse_writepage_free(wpa); |
3be5a52b MS |
1765 | } |
1766 | ||
1e18bda8 MS |
1767 | static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc, |
1768 | struct fuse_inode *fi) | |
adcadfa8 | 1769 | { |
72523425 | 1770 | struct fuse_file *ff = NULL; |
adcadfa8 | 1771 | |
f15ecfef | 1772 | spin_lock(&fi->lock); |
1e18bda8 | 1773 | if (!list_empty(&fi->write_files)) { |
72523425 MS |
1774 | ff = list_entry(fi->write_files.next, struct fuse_file, |
1775 | write_entry); | |
1776 | fuse_file_get(ff); | |
1777 | } | |
f15ecfef | 1778 | spin_unlock(&fi->lock); |
adcadfa8 PE |
1779 | |
1780 | return ff; | |
1781 | } | |
1782 | ||
1e18bda8 MS |
1783 | static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc, |
1784 | struct fuse_inode *fi) | |
1785 | { | |
1786 | struct fuse_file *ff = __fuse_write_file_get(fc, fi); | |
1787 | WARN_ON(!ff); | |
1788 | return ff; | |
1789 | } | |
1790 | ||
1791 | int fuse_write_inode(struct inode *inode, struct writeback_control *wbc) | |
1792 | { | |
1793 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1794 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1795 | struct fuse_file *ff; | |
1796 | int err; | |
1797 | ||
1798 | ff = __fuse_write_file_get(fc, fi); | |
ab9e13f7 | 1799 | err = fuse_flush_times(inode, ff); |
1e18bda8 | 1800 | if (ff) |
2e64ff15 | 1801 | fuse_file_put(ff, false, false); |
1e18bda8 MS |
1802 | |
1803 | return err; | |
1804 | } | |
1805 | ||
33826ebb MS |
1806 | static struct fuse_writepage_args *fuse_writepage_args_alloc(void) |
1807 | { | |
1808 | struct fuse_writepage_args *wpa; | |
1809 | struct fuse_args_pages *ap; | |
1810 | ||
1811 | wpa = kzalloc(sizeof(*wpa), GFP_NOFS); | |
1812 | if (wpa) { | |
1813 | ap = &wpa->ia.ap; | |
1814 | ap->num_pages = 0; | |
1815 | ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs); | |
1816 | if (!ap->pages) { | |
1817 | kfree(wpa); | |
1818 | wpa = NULL; | |
1819 | } | |
1820 | } | |
1821 | return wpa; | |
1822 | ||
1823 | } | |
1824 | ||
3be5a52b MS |
1825 | static int fuse_writepage_locked(struct page *page) |
1826 | { | |
1827 | struct address_space *mapping = page->mapping; | |
1828 | struct inode *inode = mapping->host; | |
1829 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1830 | struct fuse_inode *fi = get_fuse_inode(inode); | |
33826ebb MS |
1831 | struct fuse_writepage_args *wpa; |
1832 | struct fuse_args_pages *ap; | |
3be5a52b | 1833 | struct page *tmp_page; |
72523425 | 1834 | int error = -ENOMEM; |
3be5a52b MS |
1835 | |
1836 | set_page_writeback(page); | |
1837 | ||
33826ebb MS |
1838 | wpa = fuse_writepage_args_alloc(); |
1839 | if (!wpa) | |
3be5a52b | 1840 | goto err; |
33826ebb | 1841 | ap = &wpa->ia.ap; |
3be5a52b MS |
1842 | |
1843 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
1844 | if (!tmp_page) | |
1845 | goto err_free; | |
1846 | ||
72523425 | 1847 | error = -EIO; |
33826ebb MS |
1848 | wpa->ia.ff = fuse_write_file_get(fc, fi); |
1849 | if (!wpa->ia.ff) | |
27f1b363 | 1850 | goto err_nofile; |
72523425 | 1851 | |
33826ebb | 1852 | fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0); |
3be5a52b MS |
1853 | |
1854 | copy_highpage(tmp_page, page); | |
33826ebb MS |
1855 | wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE; |
1856 | wpa->next = NULL; | |
1857 | ap->args.in_pages = true; | |
1858 | ap->num_pages = 1; | |
1859 | ap->pages[0] = tmp_page; | |
1860 | ap->descs[0].offset = 0; | |
1861 | ap->descs[0].length = PAGE_SIZE; | |
1862 | ap->args.end = fuse_writepage_end; | |
1863 | wpa->inode = inode; | |
3be5a52b | 1864 | |
93f78d88 | 1865 | inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); |
11fb9989 | 1866 | inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); |
3be5a52b | 1867 | |
f15ecfef | 1868 | spin_lock(&fi->lock); |
6b2fb799 | 1869 | tree_insert(&fi->writepages, wpa); |
33826ebb | 1870 | list_add_tail(&wpa->queue_entry, &fi->queued_writes); |
3be5a52b | 1871 | fuse_flush_writepages(inode); |
f15ecfef | 1872 | spin_unlock(&fi->lock); |
3be5a52b | 1873 | |
4a4ac4eb MP |
1874 | end_page_writeback(page); |
1875 | ||
3be5a52b MS |
1876 | return 0; |
1877 | ||
27f1b363 MP |
1878 | err_nofile: |
1879 | __free_page(tmp_page); | |
3be5a52b | 1880 | err_free: |
33826ebb | 1881 | kfree(wpa); |
3be5a52b | 1882 | err: |
9183976e | 1883 | mapping_set_error(page->mapping, error); |
3be5a52b | 1884 | end_page_writeback(page); |
72523425 | 1885 | return error; |
3be5a52b MS |
1886 | } |
1887 | ||
1888 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1889 | { | |
1890 | int err; | |
1891 | ||
ff17be08 MS |
1892 | if (fuse_page_is_writeback(page->mapping->host, page->index)) { |
1893 | /* | |
1894 | * ->writepages() should be called for sync() and friends. We | |
1895 | * should only get here on direct reclaim and then we are | |
1896 | * allowed to skip a page which is already in flight | |
1897 | */ | |
1898 | WARN_ON(wbc->sync_mode == WB_SYNC_ALL); | |
1899 | ||
1900 | redirty_page_for_writepage(wbc, page); | |
d5880c7a | 1901 | unlock_page(page); |
ff17be08 MS |
1902 | return 0; |
1903 | } | |
1904 | ||
3be5a52b MS |
1905 | err = fuse_writepage_locked(page); |
1906 | unlock_page(page); | |
1907 | ||
1908 | return err; | |
1909 | } | |
1910 | ||
26d614df | 1911 | struct fuse_fill_wb_data { |
33826ebb | 1912 | struct fuse_writepage_args *wpa; |
26d614df PE |
1913 | struct fuse_file *ff; |
1914 | struct inode *inode; | |
2d033eaa | 1915 | struct page **orig_pages; |
33826ebb | 1916 | unsigned int max_pages; |
26d614df PE |
1917 | }; |
1918 | ||
33826ebb MS |
1919 | static bool fuse_pages_realloc(struct fuse_fill_wb_data *data) |
1920 | { | |
1921 | struct fuse_args_pages *ap = &data->wpa->ia.ap; | |
1922 | struct fuse_conn *fc = get_fuse_conn(data->inode); | |
1923 | struct page **pages; | |
1924 | struct fuse_page_desc *descs; | |
1925 | unsigned int npages = min_t(unsigned int, | |
1926 | max_t(unsigned int, data->max_pages * 2, | |
1927 | FUSE_DEFAULT_MAX_PAGES_PER_REQ), | |
1928 | fc->max_pages); | |
1929 | WARN_ON(npages <= data->max_pages); | |
1930 | ||
1931 | pages = fuse_pages_alloc(npages, GFP_NOFS, &descs); | |
1932 | if (!pages) | |
1933 | return false; | |
1934 | ||
1935 | memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages); | |
1936 | memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages); | |
1937 | kfree(ap->pages); | |
1938 | ap->pages = pages; | |
1939 | ap->descs = descs; | |
1940 | data->max_pages = npages; | |
1941 | ||
1942 | return true; | |
1943 | } | |
1944 | ||
26d614df PE |
1945 | static void fuse_writepages_send(struct fuse_fill_wb_data *data) |
1946 | { | |
33826ebb | 1947 | struct fuse_writepage_args *wpa = data->wpa; |
26d614df | 1948 | struct inode *inode = data->inode; |
26d614df | 1949 | struct fuse_inode *fi = get_fuse_inode(inode); |
33826ebb | 1950 | int num_pages = wpa->ia.ap.num_pages; |
2d033eaa | 1951 | int i; |
26d614df | 1952 | |
33826ebb | 1953 | wpa->ia.ff = fuse_file_get(data->ff); |
f15ecfef | 1954 | spin_lock(&fi->lock); |
33826ebb | 1955 | list_add_tail(&wpa->queue_entry, &fi->queued_writes); |
26d614df | 1956 | fuse_flush_writepages(inode); |
f15ecfef | 1957 | spin_unlock(&fi->lock); |
2d033eaa MP |
1958 | |
1959 | for (i = 0; i < num_pages; i++) | |
1960 | end_page_writeback(data->orig_pages[i]); | |
26d614df PE |
1961 | } |
1962 | ||
7f305ca1 | 1963 | /* |
c146024e MS |
1964 | * Check under fi->lock if the page is under writeback, and insert it onto the |
1965 | * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's | |
419234d5 MS |
1966 | * one already added for a page at this offset. If there's none, then insert |
1967 | * this new request onto the auxiliary list, otherwise reuse the existing one by | |
c146024e | 1968 | * swapping the new temp page with the old one. |
7f305ca1 | 1969 | */ |
c146024e MS |
1970 | static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa, |
1971 | struct page *page) | |
8b284dc4 | 1972 | { |
33826ebb MS |
1973 | struct fuse_inode *fi = get_fuse_inode(new_wpa->inode); |
1974 | struct fuse_writepage_args *tmp; | |
1975 | struct fuse_writepage_args *old_wpa; | |
1976 | struct fuse_args_pages *new_ap = &new_wpa->ia.ap; | |
8b284dc4 | 1977 | |
33826ebb | 1978 | WARN_ON(new_ap->num_pages != 0); |
c146024e | 1979 | new_ap->num_pages = 1; |
8b284dc4 | 1980 | |
f15ecfef | 1981 | spin_lock(&fi->lock); |
c146024e | 1982 | old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa); |
33826ebb | 1983 | if (!old_wpa) { |
f15ecfef | 1984 | spin_unlock(&fi->lock); |
c146024e | 1985 | return true; |
f6011081 | 1986 | } |
8b284dc4 | 1987 | |
33826ebb | 1988 | for (tmp = old_wpa->next; tmp; tmp = tmp->next) { |
7f305ca1 MS |
1989 | pgoff_t curr_index; |
1990 | ||
33826ebb MS |
1991 | WARN_ON(tmp->inode != new_wpa->inode); |
1992 | curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT; | |
419234d5 | 1993 | if (curr_index == page->index) { |
33826ebb MS |
1994 | WARN_ON(tmp->ia.ap.num_pages != 1); |
1995 | swap(tmp->ia.ap.pages[0], new_ap->pages[0]); | |
7f305ca1 | 1996 | break; |
8b284dc4 MS |
1997 | } |
1998 | } | |
1999 | ||
7f305ca1 | 2000 | if (!tmp) { |
33826ebb MS |
2001 | new_wpa->next = old_wpa->next; |
2002 | old_wpa->next = new_wpa; | |
7f305ca1 | 2003 | } |
41b6e41f | 2004 | |
f15ecfef | 2005 | spin_unlock(&fi->lock); |
7f305ca1 MS |
2006 | |
2007 | if (tmp) { | |
33826ebb | 2008 | struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode); |
8b284dc4 | 2009 | |
93f78d88 | 2010 | dec_wb_stat(&bdi->wb, WB_WRITEBACK); |
33826ebb | 2011 | dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP); |
93f78d88 | 2012 | wb_writeout_inc(&bdi->wb); |
33826ebb | 2013 | fuse_writepage_free(new_wpa); |
8b284dc4 | 2014 | } |
7f305ca1 | 2015 | |
c146024e | 2016 | return false; |
8b284dc4 MS |
2017 | } |
2018 | ||
6ddf3af9 MS |
2019 | static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page, |
2020 | struct fuse_args_pages *ap, | |
2021 | struct fuse_fill_wb_data *data) | |
2022 | { | |
2023 | WARN_ON(!ap->num_pages); | |
2024 | ||
2025 | /* | |
2026 | * Being under writeback is unlikely but possible. For example direct | |
2027 | * read to an mmaped fuse file will set the page dirty twice; once when | |
2028 | * the pages are faulted with get_user_pages(), and then after the read | |
2029 | * completed. | |
2030 | */ | |
2031 | if (fuse_page_is_writeback(data->inode, page->index)) | |
2032 | return true; | |
2033 | ||
2034 | /* Reached max pages */ | |
2035 | if (ap->num_pages == fc->max_pages) | |
2036 | return true; | |
2037 | ||
2038 | /* Reached max write bytes */ | |
2039 | if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write) | |
2040 | return true; | |
2041 | ||
2042 | /* Discontinuity */ | |
2043 | if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index) | |
2044 | return true; | |
2045 | ||
2046 | /* Need to grow the pages array? If so, did the expansion fail? */ | |
2047 | if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data)) | |
2048 | return true; | |
2049 | ||
2050 | return false; | |
2051 | } | |
2052 | ||
26d614df PE |
2053 | static int fuse_writepages_fill(struct page *page, |
2054 | struct writeback_control *wbc, void *_data) | |
2055 | { | |
2056 | struct fuse_fill_wb_data *data = _data; | |
33826ebb MS |
2057 | struct fuse_writepage_args *wpa = data->wpa; |
2058 | struct fuse_args_pages *ap = &wpa->ia.ap; | |
26d614df | 2059 | struct inode *inode = data->inode; |
f15ecfef | 2060 | struct fuse_inode *fi = get_fuse_inode(inode); |
26d614df PE |
2061 | struct fuse_conn *fc = get_fuse_conn(inode); |
2062 | struct page *tmp_page; | |
2063 | int err; | |
2064 | ||
2065 | if (!data->ff) { | |
2066 | err = -EIO; | |
091d1a72 | 2067 | data->ff = fuse_write_file_get(fc, fi); |
26d614df PE |
2068 | if (!data->ff) |
2069 | goto out_unlock; | |
2070 | } | |
2071 | ||
6ddf3af9 | 2072 | if (wpa && fuse_writepage_need_send(fc, page, ap, data)) { |
8b284dc4 | 2073 | fuse_writepages_send(data); |
33826ebb | 2074 | data->wpa = NULL; |
26d614df | 2075 | } |
e52a8250 | 2076 | |
26d614df PE |
2077 | err = -ENOMEM; |
2078 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
2079 | if (!tmp_page) | |
2080 | goto out_unlock; | |
2081 | ||
2082 | /* | |
2083 | * The page must not be redirtied until the writeout is completed | |
2084 | * (i.e. userspace has sent a reply to the write request). Otherwise | |
2085 | * there could be more than one temporary page instance for each real | |
2086 | * page. | |
2087 | * | |
2088 | * This is ensured by holding the page lock in page_mkwrite() while | |
2089 | * checking fuse_page_is_writeback(). We already hold the page lock | |
2090 | * since clear_page_dirty_for_io() and keep it held until we add the | |
33826ebb | 2091 | * request to the fi->writepages list and increment ap->num_pages. |
26d614df PE |
2092 | * After this fuse_page_is_writeback() will indicate that the page is |
2093 | * under writeback, so we can release the page lock. | |
2094 | */ | |
33826ebb | 2095 | if (data->wpa == NULL) { |
26d614df | 2096 | err = -ENOMEM; |
33826ebb MS |
2097 | wpa = fuse_writepage_args_alloc(); |
2098 | if (!wpa) { | |
26d614df PE |
2099 | __free_page(tmp_page); |
2100 | goto out_unlock; | |
2101 | } | |
33826ebb | 2102 | data->max_pages = 1; |
26d614df | 2103 | |
33826ebb MS |
2104 | ap = &wpa->ia.ap; |
2105 | fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0); | |
2106 | wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE; | |
2107 | wpa->next = NULL; | |
2108 | ap->args.in_pages = true; | |
2109 | ap->args.end = fuse_writepage_end; | |
2110 | ap->num_pages = 0; | |
2111 | wpa->inode = inode; | |
26d614df PE |
2112 | } |
2113 | set_page_writeback(page); | |
2114 | ||
2115 | copy_highpage(tmp_page, page); | |
33826ebb MS |
2116 | ap->pages[ap->num_pages] = tmp_page; |
2117 | ap->descs[ap->num_pages].offset = 0; | |
2118 | ap->descs[ap->num_pages].length = PAGE_SIZE; | |
c146024e | 2119 | data->orig_pages[ap->num_pages] = page; |
26d614df | 2120 | |
93f78d88 | 2121 | inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); |
11fb9989 | 2122 | inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); |
8b284dc4 MS |
2123 | |
2124 | err = 0; | |
c146024e MS |
2125 | if (data->wpa) { |
2126 | /* | |
2127 | * Protected by fi->lock against concurrent access by | |
2128 | * fuse_page_is_writeback(). | |
2129 | */ | |
2130 | spin_lock(&fi->lock); | |
2131 | ap->num_pages++; | |
2132 | spin_unlock(&fi->lock); | |
2133 | } else if (fuse_writepage_add(wpa, page)) { | |
2134 | data->wpa = wpa; | |
2135 | } else { | |
8b284dc4 | 2136 | end_page_writeback(page); |
8b284dc4 | 2137 | } |
26d614df PE |
2138 | out_unlock: |
2139 | unlock_page(page); | |
2140 | ||
2141 | return err; | |
2142 | } | |
2143 | ||
2144 | static int fuse_writepages(struct address_space *mapping, | |
2145 | struct writeback_control *wbc) | |
2146 | { | |
2147 | struct inode *inode = mapping->host; | |
5da784cc | 2148 | struct fuse_conn *fc = get_fuse_conn(inode); |
26d614df PE |
2149 | struct fuse_fill_wb_data data; |
2150 | int err; | |
2151 | ||
2152 | err = -EIO; | |
2153 | if (is_bad_inode(inode)) | |
2154 | goto out; | |
2155 | ||
2156 | data.inode = inode; | |
33826ebb | 2157 | data.wpa = NULL; |
26d614df PE |
2158 | data.ff = NULL; |
2159 | ||
2d033eaa | 2160 | err = -ENOMEM; |
5da784cc | 2161 | data.orig_pages = kcalloc(fc->max_pages, |
f2b3455e | 2162 | sizeof(struct page *), |
2d033eaa MP |
2163 | GFP_NOFS); |
2164 | if (!data.orig_pages) | |
2165 | goto out; | |
2166 | ||
26d614df | 2167 | err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data); |
33826ebb | 2168 | if (data.wpa) { |
33826ebb | 2169 | WARN_ON(!data.wpa->ia.ap.num_pages); |
26d614df | 2170 | fuse_writepages_send(&data); |
26d614df PE |
2171 | } |
2172 | if (data.ff) | |
2e64ff15 | 2173 | fuse_file_put(data.ff, false, false); |
2d033eaa MP |
2174 | |
2175 | kfree(data.orig_pages); | |
26d614df PE |
2176 | out: |
2177 | return err; | |
2178 | } | |
2179 | ||
6b12c1b3 PE |
2180 | /* |
2181 | * It's worthy to make sure that space is reserved on disk for the write, | |
2182 | * but how to implement it without killing performance need more thinking. | |
2183 | */ | |
2184 | static int fuse_write_begin(struct file *file, struct address_space *mapping, | |
2185 | loff_t pos, unsigned len, unsigned flags, | |
2186 | struct page **pagep, void **fsdata) | |
2187 | { | |
09cbfeaf | 2188 | pgoff_t index = pos >> PAGE_SHIFT; |
a455589f | 2189 | struct fuse_conn *fc = get_fuse_conn(file_inode(file)); |
6b12c1b3 PE |
2190 | struct page *page; |
2191 | loff_t fsize; | |
2192 | int err = -ENOMEM; | |
2193 | ||
2194 | WARN_ON(!fc->writeback_cache); | |
2195 | ||
2196 | page = grab_cache_page_write_begin(mapping, index, flags); | |
2197 | if (!page) | |
2198 | goto error; | |
2199 | ||
2200 | fuse_wait_on_page_writeback(mapping->host, page->index); | |
2201 | ||
09cbfeaf | 2202 | if (PageUptodate(page) || len == PAGE_SIZE) |
6b12c1b3 PE |
2203 | goto success; |
2204 | /* | |
2205 | * Check if the start this page comes after the end of file, in which | |
2206 | * case the readpage can be optimized away. | |
2207 | */ | |
2208 | fsize = i_size_read(mapping->host); | |
09cbfeaf KS |
2209 | if (fsize <= (pos & PAGE_MASK)) { |
2210 | size_t off = pos & ~PAGE_MASK; | |
6b12c1b3 PE |
2211 | if (off) |
2212 | zero_user_segment(page, 0, off); | |
2213 | goto success; | |
2214 | } | |
2215 | err = fuse_do_readpage(file, page); | |
2216 | if (err) | |
2217 | goto cleanup; | |
2218 | success: | |
2219 | *pagep = page; | |
2220 | return 0; | |
2221 | ||
2222 | cleanup: | |
2223 | unlock_page(page); | |
09cbfeaf | 2224 | put_page(page); |
6b12c1b3 PE |
2225 | error: |
2226 | return err; | |
2227 | } | |
2228 | ||
2229 | static int fuse_write_end(struct file *file, struct address_space *mapping, | |
2230 | loff_t pos, unsigned len, unsigned copied, | |
2231 | struct page *page, void *fsdata) | |
2232 | { | |
2233 | struct inode *inode = page->mapping->host; | |
2234 | ||
59c3b76c MS |
2235 | /* Haven't copied anything? Skip zeroing, size extending, dirtying. */ |
2236 | if (!copied) | |
2237 | goto unlock; | |
2238 | ||
6b12c1b3 PE |
2239 | if (!PageUptodate(page)) { |
2240 | /* Zero any unwritten bytes at the end of the page */ | |
09cbfeaf | 2241 | size_t endoff = (pos + copied) & ~PAGE_MASK; |
6b12c1b3 | 2242 | if (endoff) |
09cbfeaf | 2243 | zero_user_segment(page, endoff, PAGE_SIZE); |
6b12c1b3 PE |
2244 | SetPageUptodate(page); |
2245 | } | |
2246 | ||
2247 | fuse_write_update_size(inode, pos + copied); | |
2248 | set_page_dirty(page); | |
59c3b76c MS |
2249 | |
2250 | unlock: | |
6b12c1b3 | 2251 | unlock_page(page); |
09cbfeaf | 2252 | put_page(page); |
6b12c1b3 PE |
2253 | |
2254 | return copied; | |
2255 | } | |
2256 | ||
3be5a52b MS |
2257 | static int fuse_launder_page(struct page *page) |
2258 | { | |
2259 | int err = 0; | |
2260 | if (clear_page_dirty_for_io(page)) { | |
2261 | struct inode *inode = page->mapping->host; | |
2262 | err = fuse_writepage_locked(page); | |
2263 | if (!err) | |
2264 | fuse_wait_on_page_writeback(inode, page->index); | |
2265 | } | |
2266 | return err; | |
2267 | } | |
2268 | ||
2269 | /* | |
2270 | * Write back dirty pages now, because there may not be any suitable | |
2271 | * open files later | |
2272 | */ | |
2273 | static void fuse_vma_close(struct vm_area_struct *vma) | |
2274 | { | |
2275 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
2276 | } | |
2277 | ||
2278 | /* | |
2279 | * Wait for writeback against this page to complete before allowing it | |
2280 | * to be marked dirty again, and hence written back again, possibly | |
2281 | * before the previous writepage completed. | |
2282 | * | |
2283 | * Block here, instead of in ->writepage(), so that the userspace fs | |
2284 | * can only block processes actually operating on the filesystem. | |
2285 | * | |
2286 | * Otherwise unprivileged userspace fs would be able to block | |
2287 | * unrelated: | |
2288 | * | |
2289 | * - page migration | |
2290 | * - sync(2) | |
2291 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
2292 | */ | |
46fb504a | 2293 | static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf) |
3be5a52b | 2294 | { |
c2ec175c | 2295 | struct page *page = vmf->page; |
11bac800 | 2296 | struct inode *inode = file_inode(vmf->vma->vm_file); |
cca24370 | 2297 | |
11bac800 | 2298 | file_update_time(vmf->vma->vm_file); |
cca24370 MS |
2299 | lock_page(page); |
2300 | if (page->mapping != inode->i_mapping) { | |
2301 | unlock_page(page); | |
2302 | return VM_FAULT_NOPAGE; | |
2303 | } | |
3be5a52b MS |
2304 | |
2305 | fuse_wait_on_page_writeback(inode, page->index); | |
cca24370 | 2306 | return VM_FAULT_LOCKED; |
3be5a52b MS |
2307 | } |
2308 | ||
f0f37e2f | 2309 | static const struct vm_operations_struct fuse_file_vm_ops = { |
3be5a52b MS |
2310 | .close = fuse_vma_close, |
2311 | .fault = filemap_fault, | |
f1820361 | 2312 | .map_pages = filemap_map_pages, |
3be5a52b MS |
2313 | .page_mkwrite = fuse_page_mkwrite, |
2314 | }; | |
2315 | ||
2316 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
2317 | { | |
55752a3a MS |
2318 | struct fuse_file *ff = file->private_data; |
2319 | ||
2320 | if (ff->open_flags & FOPEN_DIRECT_IO) { | |
2321 | /* Can't provide the coherency needed for MAP_SHARED */ | |
2322 | if (vma->vm_flags & VM_MAYSHARE) | |
2323 | return -ENODEV; | |
2324 | ||
2325 | invalidate_inode_pages2(file->f_mapping); | |
2326 | ||
2327 | return generic_file_mmap(file, vma); | |
2328 | } | |
2329 | ||
650b22b9 PE |
2330 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
2331 | fuse_link_write_file(file); | |
2332 | ||
3be5a52b MS |
2333 | file_accessed(file); |
2334 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
2335 | return 0; |
2336 | } | |
2337 | ||
0b6e9ea0 SF |
2338 | static int convert_fuse_file_lock(struct fuse_conn *fc, |
2339 | const struct fuse_file_lock *ffl, | |
71421259 MS |
2340 | struct file_lock *fl) |
2341 | { | |
2342 | switch (ffl->type) { | |
2343 | case F_UNLCK: | |
2344 | break; | |
2345 | ||
2346 | case F_RDLCK: | |
2347 | case F_WRLCK: | |
2348 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
2349 | ffl->end < ffl->start) | |
2350 | return -EIO; | |
2351 | ||
2352 | fl->fl_start = ffl->start; | |
2353 | fl->fl_end = ffl->end; | |
0b6e9ea0 SF |
2354 | |
2355 | /* | |
9d5b86ac BC |
2356 | * Convert pid into init's pid namespace. The locks API will |
2357 | * translate it into the caller's pid namespace. | |
0b6e9ea0 SF |
2358 | */ |
2359 | rcu_read_lock(); | |
9d5b86ac | 2360 | fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns); |
0b6e9ea0 | 2361 | rcu_read_unlock(); |
71421259 MS |
2362 | break; |
2363 | ||
2364 | default: | |
2365 | return -EIO; | |
2366 | } | |
2367 | fl->fl_type = ffl->type; | |
2368 | return 0; | |
2369 | } | |
2370 | ||
7078187a | 2371 | static void fuse_lk_fill(struct fuse_args *args, struct file *file, |
a9ff4f87 | 2372 | const struct file_lock *fl, int opcode, pid_t pid, |
7078187a | 2373 | int flock, struct fuse_lk_in *inarg) |
71421259 | 2374 | { |
6131ffaa | 2375 | struct inode *inode = file_inode(file); |
9c8ef561 | 2376 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 | 2377 | struct fuse_file *ff = file->private_data; |
7078187a MS |
2378 | |
2379 | memset(inarg, 0, sizeof(*inarg)); | |
2380 | inarg->fh = ff->fh; | |
2381 | inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner); | |
2382 | inarg->lk.start = fl->fl_start; | |
2383 | inarg->lk.end = fl->fl_end; | |
2384 | inarg->lk.type = fl->fl_type; | |
2385 | inarg->lk.pid = pid; | |
a9ff4f87 | 2386 | if (flock) |
7078187a | 2387 | inarg->lk_flags |= FUSE_LK_FLOCK; |
d5b48543 MS |
2388 | args->opcode = opcode; |
2389 | args->nodeid = get_node_id(inode); | |
2390 | args->in_numargs = 1; | |
2391 | args->in_args[0].size = sizeof(*inarg); | |
2392 | args->in_args[0].value = inarg; | |
71421259 MS |
2393 | } |
2394 | ||
2395 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
2396 | { | |
6131ffaa | 2397 | struct inode *inode = file_inode(file); |
71421259 | 2398 | struct fuse_conn *fc = get_fuse_conn(inode); |
7078187a MS |
2399 | FUSE_ARGS(args); |
2400 | struct fuse_lk_in inarg; | |
71421259 MS |
2401 | struct fuse_lk_out outarg; |
2402 | int err; | |
2403 | ||
7078187a | 2404 | fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg); |
d5b48543 MS |
2405 | args.out_numargs = 1; |
2406 | args.out_args[0].size = sizeof(outarg); | |
2407 | args.out_args[0].value = &outarg; | |
7078187a | 2408 | err = fuse_simple_request(fc, &args); |
71421259 | 2409 | if (!err) |
0b6e9ea0 | 2410 | err = convert_fuse_file_lock(fc, &outarg.lk, fl); |
71421259 MS |
2411 | |
2412 | return err; | |
2413 | } | |
2414 | ||
a9ff4f87 | 2415 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 2416 | { |
6131ffaa | 2417 | struct inode *inode = file_inode(file); |
71421259 | 2418 | struct fuse_conn *fc = get_fuse_conn(inode); |
7078187a MS |
2419 | FUSE_ARGS(args); |
2420 | struct fuse_lk_in inarg; | |
71421259 | 2421 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; |
0b6e9ea0 SF |
2422 | struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL; |
2423 | pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns); | |
71421259 MS |
2424 | int err; |
2425 | ||
8fb47a4f | 2426 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) { |
48e90761 MS |
2427 | /* NLM needs asynchronous locks, which we don't support yet */ |
2428 | return -ENOLCK; | |
2429 | } | |
2430 | ||
71421259 | 2431 | /* Unlock on close is handled by the flush method */ |
50f2112c | 2432 | if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX) |
71421259 MS |
2433 | return 0; |
2434 | ||
0b6e9ea0 | 2435 | fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg); |
7078187a | 2436 | err = fuse_simple_request(fc, &args); |
71421259 | 2437 | |
a4d27e75 MS |
2438 | /* locking is restartable */ |
2439 | if (err == -EINTR) | |
2440 | err = -ERESTARTSYS; | |
7078187a | 2441 | |
71421259 MS |
2442 | return err; |
2443 | } | |
2444 | ||
2445 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
2446 | { | |
6131ffaa | 2447 | struct inode *inode = file_inode(file); |
71421259 MS |
2448 | struct fuse_conn *fc = get_fuse_conn(inode); |
2449 | int err; | |
2450 | ||
48e90761 MS |
2451 | if (cmd == F_CANCELLK) { |
2452 | err = 0; | |
2453 | } else if (cmd == F_GETLK) { | |
71421259 | 2454 | if (fc->no_lock) { |
9d6a8c5c | 2455 | posix_test_lock(file, fl); |
71421259 MS |
2456 | err = 0; |
2457 | } else | |
2458 | err = fuse_getlk(file, fl); | |
2459 | } else { | |
2460 | if (fc->no_lock) | |
48e90761 | 2461 | err = posix_lock_file(file, fl, NULL); |
71421259 | 2462 | else |
a9ff4f87 | 2463 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
2464 | } |
2465 | return err; | |
2466 | } | |
2467 | ||
a9ff4f87 MS |
2468 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
2469 | { | |
6131ffaa | 2470 | struct inode *inode = file_inode(file); |
a9ff4f87 MS |
2471 | struct fuse_conn *fc = get_fuse_conn(inode); |
2472 | int err; | |
2473 | ||
37fb3a30 | 2474 | if (fc->no_flock) { |
4f656367 | 2475 | err = locks_lock_file_wait(file, fl); |
a9ff4f87 | 2476 | } else { |
37fb3a30 MS |
2477 | struct fuse_file *ff = file->private_data; |
2478 | ||
a9ff4f87 | 2479 | /* emulate flock with POSIX locks */ |
37fb3a30 | 2480 | ff->flock = true; |
a9ff4f87 MS |
2481 | err = fuse_setlk(file, fl, 1); |
2482 | } | |
2483 | ||
2484 | return err; | |
2485 | } | |
2486 | ||
b2d2272f MS |
2487 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
2488 | { | |
2489 | struct inode *inode = mapping->host; | |
2490 | struct fuse_conn *fc = get_fuse_conn(inode); | |
7078187a | 2491 | FUSE_ARGS(args); |
b2d2272f MS |
2492 | struct fuse_bmap_in inarg; |
2493 | struct fuse_bmap_out outarg; | |
2494 | int err; | |
2495 | ||
2496 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
2497 | return 0; | |
2498 | ||
b2d2272f MS |
2499 | memset(&inarg, 0, sizeof(inarg)); |
2500 | inarg.block = block; | |
2501 | inarg.blocksize = inode->i_sb->s_blocksize; | |
d5b48543 MS |
2502 | args.opcode = FUSE_BMAP; |
2503 | args.nodeid = get_node_id(inode); | |
2504 | args.in_numargs = 1; | |
2505 | args.in_args[0].size = sizeof(inarg); | |
2506 | args.in_args[0].value = &inarg; | |
2507 | args.out_numargs = 1; | |
2508 | args.out_args[0].size = sizeof(outarg); | |
2509 | args.out_args[0].value = &outarg; | |
7078187a | 2510 | err = fuse_simple_request(fc, &args); |
b2d2272f MS |
2511 | if (err == -ENOSYS) |
2512 | fc->no_bmap = 1; | |
2513 | ||
2514 | return err ? 0 : outarg.block; | |
2515 | } | |
2516 | ||
0b5da8db R |
2517 | static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) |
2518 | { | |
2519 | struct inode *inode = file->f_mapping->host; | |
2520 | struct fuse_conn *fc = get_fuse_conn(inode); | |
2521 | struct fuse_file *ff = file->private_data; | |
2522 | FUSE_ARGS(args); | |
2523 | struct fuse_lseek_in inarg = { | |
2524 | .fh = ff->fh, | |
2525 | .offset = offset, | |
2526 | .whence = whence | |
2527 | }; | |
2528 | struct fuse_lseek_out outarg; | |
2529 | int err; | |
2530 | ||
2531 | if (fc->no_lseek) | |
2532 | goto fallback; | |
2533 | ||
d5b48543 MS |
2534 | args.opcode = FUSE_LSEEK; |
2535 | args.nodeid = ff->nodeid; | |
2536 | args.in_numargs = 1; | |
2537 | args.in_args[0].size = sizeof(inarg); | |
2538 | args.in_args[0].value = &inarg; | |
2539 | args.out_numargs = 1; | |
2540 | args.out_args[0].size = sizeof(outarg); | |
2541 | args.out_args[0].value = &outarg; | |
0b5da8db R |
2542 | err = fuse_simple_request(fc, &args); |
2543 | if (err) { | |
2544 | if (err == -ENOSYS) { | |
2545 | fc->no_lseek = 1; | |
2546 | goto fallback; | |
2547 | } | |
2548 | return err; | |
2549 | } | |
2550 | ||
2551 | return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes); | |
2552 | ||
2553 | fallback: | |
5b97eeac | 2554 | err = fuse_update_attributes(inode, file); |
0b5da8db R |
2555 | if (!err) |
2556 | return generic_file_llseek(file, offset, whence); | |
2557 | else | |
2558 | return err; | |
2559 | } | |
2560 | ||
965c8e59 | 2561 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) |
5559b8f4 MS |
2562 | { |
2563 | loff_t retval; | |
6131ffaa | 2564 | struct inode *inode = file_inode(file); |
5559b8f4 | 2565 | |
0b5da8db R |
2566 | switch (whence) { |
2567 | case SEEK_SET: | |
2568 | case SEEK_CUR: | |
2569 | /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ | |
965c8e59 | 2570 | retval = generic_file_llseek(file, offset, whence); |
0b5da8db R |
2571 | break; |
2572 | case SEEK_END: | |
5955102c | 2573 | inode_lock(inode); |
5b97eeac | 2574 | retval = fuse_update_attributes(inode, file); |
0b5da8db R |
2575 | if (!retval) |
2576 | retval = generic_file_llseek(file, offset, whence); | |
5955102c | 2577 | inode_unlock(inode); |
0b5da8db R |
2578 | break; |
2579 | case SEEK_HOLE: | |
2580 | case SEEK_DATA: | |
5955102c | 2581 | inode_lock(inode); |
0b5da8db | 2582 | retval = fuse_lseek(file, offset, whence); |
5955102c | 2583 | inode_unlock(inode); |
0b5da8db R |
2584 | break; |
2585 | default: | |
2586 | retval = -EINVAL; | |
2587 | } | |
c07c3d19 | 2588 | |
5559b8f4 MS |
2589 | return retval; |
2590 | } | |
2591 | ||
d9d318d3 MS |
2592 | /* |
2593 | * CUSE servers compiled on 32bit broke on 64bit kernels because the | |
2594 | * ABI was defined to be 'struct iovec' which is different on 32bit | |
2595 | * and 64bit. Fortunately we can determine which structure the server | |
2596 | * used from the size of the reply. | |
2597 | */ | |
1baa26b2 MS |
2598 | static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, |
2599 | size_t transferred, unsigned count, | |
2600 | bool is_compat) | |
d9d318d3 MS |
2601 | { |
2602 | #ifdef CONFIG_COMPAT | |
2603 | if (count * sizeof(struct compat_iovec) == transferred) { | |
2604 | struct compat_iovec *ciov = src; | |
2605 | unsigned i; | |
2606 | ||
2607 | /* | |
2608 | * With this interface a 32bit server cannot support | |
2609 | * non-compat (i.e. ones coming from 64bit apps) ioctl | |
2610 | * requests | |
2611 | */ | |
2612 | if (!is_compat) | |
2613 | return -EINVAL; | |
2614 | ||
2615 | for (i = 0; i < count; i++) { | |
2616 | dst[i].iov_base = compat_ptr(ciov[i].iov_base); | |
2617 | dst[i].iov_len = ciov[i].iov_len; | |
2618 | } | |
2619 | return 0; | |
2620 | } | |
2621 | #endif | |
2622 | ||
2623 | if (count * sizeof(struct iovec) != transferred) | |
2624 | return -EIO; | |
2625 | ||
2626 | memcpy(dst, src, transferred); | |
2627 | return 0; | |
2628 | } | |
2629 | ||
7572777e | 2630 | /* Make sure iov_length() won't overflow */ |
5da784cc CS |
2631 | static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov, |
2632 | size_t count) | |
7572777e MS |
2633 | { |
2634 | size_t n; | |
5da784cc | 2635 | u32 max = fc->max_pages << PAGE_SHIFT; |
7572777e | 2636 | |
fb6ccff6 | 2637 | for (n = 0; n < count; n++, iov++) { |
7572777e MS |
2638 | if (iov->iov_len > (size_t) max) |
2639 | return -ENOMEM; | |
2640 | max -= iov->iov_len; | |
2641 | } | |
2642 | return 0; | |
2643 | } | |
2644 | ||
1baa26b2 MS |
2645 | static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, |
2646 | void *src, size_t transferred, unsigned count, | |
2647 | bool is_compat) | |
2648 | { | |
2649 | unsigned i; | |
2650 | struct fuse_ioctl_iovec *fiov = src; | |
2651 | ||
2652 | if (fc->minor < 16) { | |
2653 | return fuse_copy_ioctl_iovec_old(dst, src, transferred, | |
2654 | count, is_compat); | |
2655 | } | |
2656 | ||
2657 | if (count * sizeof(struct fuse_ioctl_iovec) != transferred) | |
2658 | return -EIO; | |
2659 | ||
2660 | for (i = 0; i < count; i++) { | |
2661 | /* Did the server supply an inappropriate value? */ | |
2662 | if (fiov[i].base != (unsigned long) fiov[i].base || | |
2663 | fiov[i].len != (unsigned long) fiov[i].len) | |
2664 | return -EIO; | |
2665 | ||
2666 | dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; | |
2667 | dst[i].iov_len = (size_t) fiov[i].len; | |
2668 | ||
2669 | #ifdef CONFIG_COMPAT | |
2670 | if (is_compat && | |
2671 | (ptr_to_compat(dst[i].iov_base) != fiov[i].base || | |
2672 | (compat_size_t) dst[i].iov_len != fiov[i].len)) | |
2673 | return -EIO; | |
2674 | #endif | |
2675 | } | |
2676 | ||
2677 | return 0; | |
2678 | } | |
2679 | ||
2680 | ||
59efec7b TH |
2681 | /* |
2682 | * For ioctls, there is no generic way to determine how much memory | |
2683 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
2684 | * to dereference the passed pointer, so the parameter requires deep | |
2685 | * copying but FUSE has no idea whatsoever about what to copy in or | |
2686 | * out. | |
2687 | * | |
2688 | * This is solved by allowing FUSE server to retry ioctl with | |
2689 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
2690 | * needs to read in the following structure. | |
2691 | * | |
2692 | * struct a { | |
2693 | * char *buf; | |
2694 | * size_t buflen; | |
2695 | * } | |
2696 | * | |
2697 | * On the first callout to FUSE server, inarg->in_size and | |
2698 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
2699 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
2700 | * the actual iov array to | |
2701 | * | |
2702 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
2703 | * | |
2704 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
2705 | * On the second round, the server has access to the structure and | |
2706 | * from that it can tell what to look for next, so on the invocation, | |
2707 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
2708 | * | |
2709 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
2710 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
2711 | * | |
2712 | * FUSE will copy both struct a and the pointed buffer from the | |
2713 | * process doing the ioctl and retry ioctl with both struct a and the | |
2714 | * buffer. | |
2715 | * | |
2716 | * This time, FUSE server has everything it needs and completes ioctl | |
2717 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
2718 | * | |
2719 | * Copying data out works the same way. | |
2720 | * | |
2721 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
2722 | * automatically initializes in and out iovs by decoding @cmd with | |
2723 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
2724 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
2725 | * behavior for all FUSE servers. | |
2726 | */ | |
08cbf542 TH |
2727 | long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, |
2728 | unsigned int flags) | |
59efec7b | 2729 | { |
59efec7b | 2730 | struct fuse_file *ff = file->private_data; |
d36f2487 | 2731 | struct fuse_conn *fc = ff->fc; |
59efec7b TH |
2732 | struct fuse_ioctl_in inarg = { |
2733 | .fh = ff->fh, | |
2734 | .cmd = cmd, | |
2735 | .arg = arg, | |
2736 | .flags = flags | |
2737 | }; | |
2738 | struct fuse_ioctl_out outarg; | |
8ac83505 | 2739 | struct iovec *iov_page = NULL; |
59efec7b | 2740 | struct iovec *in_iov = NULL, *out_iov = NULL; |
093f38a2 MS |
2741 | unsigned int in_iovs = 0, out_iovs = 0, max_pages; |
2742 | size_t in_size, out_size, c; | |
2743 | ssize_t transferred; | |
acbe5fda MS |
2744 | int err, i; |
2745 | struct iov_iter ii; | |
093f38a2 | 2746 | struct fuse_args_pages ap = {}; |
59efec7b | 2747 | |
1baa26b2 MS |
2748 | #if BITS_PER_LONG == 32 |
2749 | inarg.flags |= FUSE_IOCTL_32BIT; | |
2750 | #else | |
6407f44a | 2751 | if (flags & FUSE_IOCTL_COMPAT) { |
1baa26b2 | 2752 | inarg.flags |= FUSE_IOCTL_32BIT; |
6407f44a IA |
2753 | #ifdef CONFIG_X86_X32 |
2754 | if (in_x32_syscall()) | |
2755 | inarg.flags |= FUSE_IOCTL_COMPAT_X32; | |
2756 | #endif | |
2757 | } | |
1baa26b2 MS |
2758 | #endif |
2759 | ||
59efec7b | 2760 | /* assume all the iovs returned by client always fits in a page */ |
1baa26b2 | 2761 | BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); |
59efec7b | 2762 | |
59efec7b | 2763 | err = -ENOMEM; |
093f38a2 | 2764 | ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs); |
8ac83505 | 2765 | iov_page = (struct iovec *) __get_free_page(GFP_KERNEL); |
093f38a2 | 2766 | if (!ap.pages || !iov_page) |
59efec7b TH |
2767 | goto out; |
2768 | ||
093f38a2 MS |
2769 | fuse_page_descs_length_init(ap.descs, 0, fc->max_pages); |
2770 | ||
59efec7b TH |
2771 | /* |
2772 | * If restricted, initialize IO parameters as encoded in @cmd. | |
2773 | * RETRY from server is not allowed. | |
2774 | */ | |
2775 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
8ac83505 | 2776 | struct iovec *iov = iov_page; |
59efec7b | 2777 | |
c9f0523d | 2778 | iov->iov_base = (void __user *)arg; |
31070f6c CE |
2779 | |
2780 | switch (cmd) { | |
2781 | case FS_IOC_GETFLAGS: | |
2782 | case FS_IOC_SETFLAGS: | |
2783 | iov->iov_len = sizeof(int); | |
2784 | break; | |
2785 | default: | |
2786 | iov->iov_len = _IOC_SIZE(cmd); | |
2787 | break; | |
2788 | } | |
59efec7b TH |
2789 | |
2790 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
2791 | in_iov = iov; | |
2792 | in_iovs = 1; | |
2793 | } | |
2794 | ||
2795 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
2796 | out_iov = iov; | |
2797 | out_iovs = 1; | |
2798 | } | |
2799 | } | |
2800 | ||
2801 | retry: | |
2802 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
2803 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
2804 | ||
2805 | /* | |
2806 | * Out data can be used either for actual out data or iovs, | |
2807 | * make sure there always is at least one page. | |
2808 | */ | |
2809 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
2810 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
2811 | ||
2812 | /* make sure there are enough buffer pages and init request with them */ | |
2813 | err = -ENOMEM; | |
5da784cc | 2814 | if (max_pages > fc->max_pages) |
59efec7b | 2815 | goto out; |
093f38a2 MS |
2816 | while (ap.num_pages < max_pages) { |
2817 | ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
2818 | if (!ap.pages[ap.num_pages]) | |
59efec7b | 2819 | goto out; |
093f38a2 | 2820 | ap.num_pages++; |
59efec7b TH |
2821 | } |
2822 | ||
59efec7b TH |
2823 | |
2824 | /* okay, let's send it to the client */ | |
093f38a2 MS |
2825 | ap.args.opcode = FUSE_IOCTL; |
2826 | ap.args.nodeid = ff->nodeid; | |
2827 | ap.args.in_numargs = 1; | |
2828 | ap.args.in_args[0].size = sizeof(inarg); | |
2829 | ap.args.in_args[0].value = &inarg; | |
59efec7b | 2830 | if (in_size) { |
093f38a2 MS |
2831 | ap.args.in_numargs++; |
2832 | ap.args.in_args[1].size = in_size; | |
2833 | ap.args.in_pages = true; | |
59efec7b | 2834 | |
acbe5fda MS |
2835 | err = -EFAULT; |
2836 | iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size); | |
093f38a2 MS |
2837 | for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) { |
2838 | c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii); | |
acbe5fda MS |
2839 | if (c != PAGE_SIZE && iov_iter_count(&ii)) |
2840 | goto out; | |
2841 | } | |
59efec7b TH |
2842 | } |
2843 | ||
093f38a2 MS |
2844 | ap.args.out_numargs = 2; |
2845 | ap.args.out_args[0].size = sizeof(outarg); | |
2846 | ap.args.out_args[0].value = &outarg; | |
2847 | ap.args.out_args[1].size = out_size; | |
2848 | ap.args.out_pages = true; | |
2849 | ap.args.out_argvar = true; | |
59efec7b | 2850 | |
093f38a2 MS |
2851 | transferred = fuse_simple_request(fc, &ap.args); |
2852 | err = transferred; | |
2853 | if (transferred < 0) | |
59efec7b TH |
2854 | goto out; |
2855 | ||
2856 | /* did it ask for retry? */ | |
2857 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
8ac83505 | 2858 | void *vaddr; |
59efec7b TH |
2859 | |
2860 | /* no retry if in restricted mode */ | |
2861 | err = -EIO; | |
2862 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
2863 | goto out; | |
2864 | ||
2865 | in_iovs = outarg.in_iovs; | |
2866 | out_iovs = outarg.out_iovs; | |
2867 | ||
2868 | /* | |
2869 | * Make sure things are in boundary, separate checks | |
2870 | * are to protect against overflow. | |
2871 | */ | |
2872 | err = -ENOMEM; | |
2873 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
2874 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
2875 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
2876 | goto out; | |
2877 | ||
093f38a2 | 2878 | vaddr = kmap_atomic(ap.pages[0]); |
1baa26b2 | 2879 | err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr, |
d9d318d3 MS |
2880 | transferred, in_iovs + out_iovs, |
2881 | (flags & FUSE_IOCTL_COMPAT) != 0); | |
2408f6ef | 2882 | kunmap_atomic(vaddr); |
d9d318d3 MS |
2883 | if (err) |
2884 | goto out; | |
59efec7b | 2885 | |
8ac83505 | 2886 | in_iov = iov_page; |
59efec7b TH |
2887 | out_iov = in_iov + in_iovs; |
2888 | ||
5da784cc | 2889 | err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs); |
7572777e MS |
2890 | if (err) |
2891 | goto out; | |
2892 | ||
5da784cc | 2893 | err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs); |
7572777e MS |
2894 | if (err) |
2895 | goto out; | |
2896 | ||
59efec7b TH |
2897 | goto retry; |
2898 | } | |
2899 | ||
2900 | err = -EIO; | |
2901 | if (transferred > inarg.out_size) | |
2902 | goto out; | |
2903 | ||
acbe5fda MS |
2904 | err = -EFAULT; |
2905 | iov_iter_init(&ii, READ, out_iov, out_iovs, transferred); | |
093f38a2 MS |
2906 | for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) { |
2907 | c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii); | |
acbe5fda MS |
2908 | if (c != PAGE_SIZE && iov_iter_count(&ii)) |
2909 | goto out; | |
2910 | } | |
2911 | err = 0; | |
59efec7b | 2912 | out: |
8ac83505 | 2913 | free_page((unsigned long) iov_page); |
093f38a2 MS |
2914 | while (ap.num_pages) |
2915 | __free_page(ap.pages[--ap.num_pages]); | |
2916 | kfree(ap.pages); | |
59efec7b TH |
2917 | |
2918 | return err ? err : outarg.result; | |
2919 | } | |
08cbf542 | 2920 | EXPORT_SYMBOL_GPL(fuse_do_ioctl); |
59efec7b | 2921 | |
b18da0c5 MS |
2922 | long fuse_ioctl_common(struct file *file, unsigned int cmd, |
2923 | unsigned long arg, unsigned int flags) | |
d36f2487 | 2924 | { |
6131ffaa | 2925 | struct inode *inode = file_inode(file); |
d36f2487 MS |
2926 | struct fuse_conn *fc = get_fuse_conn(inode); |
2927 | ||
c2132c1b | 2928 | if (!fuse_allow_current_process(fc)) |
d36f2487 MS |
2929 | return -EACCES; |
2930 | ||
2931 | if (is_bad_inode(inode)) | |
2932 | return -EIO; | |
2933 | ||
2934 | return fuse_do_ioctl(file, cmd, arg, flags); | |
2935 | } | |
2936 | ||
59efec7b TH |
2937 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, |
2938 | unsigned long arg) | |
2939 | { | |
b18da0c5 | 2940 | return fuse_ioctl_common(file, cmd, arg, 0); |
59efec7b TH |
2941 | } |
2942 | ||
2943 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
2944 | unsigned long arg) | |
2945 | { | |
b18da0c5 | 2946 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); |
59efec7b TH |
2947 | } |
2948 | ||
95668a69 TH |
2949 | /* |
2950 | * All files which have been polled are linked to RB tree | |
2951 | * fuse_conn->polled_files which is indexed by kh. Walk the tree and | |
2952 | * find the matching one. | |
2953 | */ | |
2954 | static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, | |
2955 | struct rb_node **parent_out) | |
2956 | { | |
2957 | struct rb_node **link = &fc->polled_files.rb_node; | |
2958 | struct rb_node *last = NULL; | |
2959 | ||
2960 | while (*link) { | |
2961 | struct fuse_file *ff; | |
2962 | ||
2963 | last = *link; | |
2964 | ff = rb_entry(last, struct fuse_file, polled_node); | |
2965 | ||
2966 | if (kh < ff->kh) | |
2967 | link = &last->rb_left; | |
2968 | else if (kh > ff->kh) | |
2969 | link = &last->rb_right; | |
2970 | else | |
2971 | return link; | |
2972 | } | |
2973 | ||
2974 | if (parent_out) | |
2975 | *parent_out = last; | |
2976 | return link; | |
2977 | } | |
2978 | ||
2979 | /* | |
2980 | * The file is about to be polled. Make sure it's on the polled_files | |
2981 | * RB tree. Note that files once added to the polled_files tree are | |
2982 | * not removed before the file is released. This is because a file | |
2983 | * polled once is likely to be polled again. | |
2984 | */ | |
2985 | static void fuse_register_polled_file(struct fuse_conn *fc, | |
2986 | struct fuse_file *ff) | |
2987 | { | |
2988 | spin_lock(&fc->lock); | |
2989 | if (RB_EMPTY_NODE(&ff->polled_node)) { | |
3f649ab7 | 2990 | struct rb_node **link, *parent; |
95668a69 TH |
2991 | |
2992 | link = fuse_find_polled_node(fc, ff->kh, &parent); | |
2993 | BUG_ON(*link); | |
2994 | rb_link_node(&ff->polled_node, parent, link); | |
2995 | rb_insert_color(&ff->polled_node, &fc->polled_files); | |
2996 | } | |
2997 | spin_unlock(&fc->lock); | |
2998 | } | |
2999 | ||
076ccb76 | 3000 | __poll_t fuse_file_poll(struct file *file, poll_table *wait) |
95668a69 | 3001 | { |
95668a69 | 3002 | struct fuse_file *ff = file->private_data; |
797759aa | 3003 | struct fuse_conn *fc = ff->fc; |
95668a69 TH |
3004 | struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; |
3005 | struct fuse_poll_out outarg; | |
7078187a | 3006 | FUSE_ARGS(args); |
95668a69 TH |
3007 | int err; |
3008 | ||
3009 | if (fc->no_poll) | |
3010 | return DEFAULT_POLLMASK; | |
3011 | ||
3012 | poll_wait(file, &ff->poll_wait, wait); | |
c71d227f | 3013 | inarg.events = mangle_poll(poll_requested_events(wait)); |
95668a69 TH |
3014 | |
3015 | /* | |
3016 | * Ask for notification iff there's someone waiting for it. | |
3017 | * The client may ignore the flag and always notify. | |
3018 | */ | |
3019 | if (waitqueue_active(&ff->poll_wait)) { | |
3020 | inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; | |
3021 | fuse_register_polled_file(fc, ff); | |
3022 | } | |
3023 | ||
d5b48543 MS |
3024 | args.opcode = FUSE_POLL; |
3025 | args.nodeid = ff->nodeid; | |
3026 | args.in_numargs = 1; | |
3027 | args.in_args[0].size = sizeof(inarg); | |
3028 | args.in_args[0].value = &inarg; | |
3029 | args.out_numargs = 1; | |
3030 | args.out_args[0].size = sizeof(outarg); | |
3031 | args.out_args[0].value = &outarg; | |
7078187a | 3032 | err = fuse_simple_request(fc, &args); |
95668a69 TH |
3033 | |
3034 | if (!err) | |
c71d227f | 3035 | return demangle_poll(outarg.revents); |
95668a69 TH |
3036 | if (err == -ENOSYS) { |
3037 | fc->no_poll = 1; | |
3038 | return DEFAULT_POLLMASK; | |
3039 | } | |
a9a08845 | 3040 | return EPOLLERR; |
95668a69 | 3041 | } |
08cbf542 | 3042 | EXPORT_SYMBOL_GPL(fuse_file_poll); |
95668a69 TH |
3043 | |
3044 | /* | |
3045 | * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and | |
3046 | * wakes up the poll waiters. | |
3047 | */ | |
3048 | int fuse_notify_poll_wakeup(struct fuse_conn *fc, | |
3049 | struct fuse_notify_poll_wakeup_out *outarg) | |
3050 | { | |
3051 | u64 kh = outarg->kh; | |
3052 | struct rb_node **link; | |
3053 | ||
3054 | spin_lock(&fc->lock); | |
3055 | ||
3056 | link = fuse_find_polled_node(fc, kh, NULL); | |
3057 | if (*link) { | |
3058 | struct fuse_file *ff; | |
3059 | ||
3060 | ff = rb_entry(*link, struct fuse_file, polled_node); | |
3061 | wake_up_interruptible_sync(&ff->poll_wait); | |
3062 | } | |
3063 | ||
3064 | spin_unlock(&fc->lock); | |
3065 | return 0; | |
3066 | } | |
3067 | ||
efb9fa9e MP |
3068 | static void fuse_do_truncate(struct file *file) |
3069 | { | |
3070 | struct inode *inode = file->f_mapping->host; | |
3071 | struct iattr attr; | |
3072 | ||
3073 | attr.ia_valid = ATTR_SIZE; | |
3074 | attr.ia_size = i_size_read(inode); | |
3075 | ||
3076 | attr.ia_file = file; | |
3077 | attr.ia_valid |= ATTR_FILE; | |
3078 | ||
62490330 | 3079 | fuse_do_setattr(file_dentry(file), &attr, file); |
efb9fa9e MP |
3080 | } |
3081 | ||
5da784cc | 3082 | static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off) |
e5c5f05d | 3083 | { |
5da784cc | 3084 | return round_up(off, fc->max_pages << PAGE_SHIFT); |
e5c5f05d MP |
3085 | } |
3086 | ||
4273b793 | 3087 | static ssize_t |
c8b8e32d | 3088 | fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
4273b793 | 3089 | { |
9d5722b7 | 3090 | DECLARE_COMPLETION_ONSTACK(wait); |
4273b793 | 3091 | ssize_t ret = 0; |
60b9df7a MS |
3092 | struct file *file = iocb->ki_filp; |
3093 | struct fuse_file *ff = file->private_data; | |
e5c5f05d | 3094 | bool async_dio = ff->fc->async_dio; |
4273b793 | 3095 | loff_t pos = 0; |
bcba24cc MP |
3096 | struct inode *inode; |
3097 | loff_t i_size; | |
a6cbcd4a | 3098 | size_t count = iov_iter_count(iter); |
c8b8e32d | 3099 | loff_t offset = iocb->ki_pos; |
36cf66ed | 3100 | struct fuse_io_priv *io; |
4273b793 | 3101 | |
4273b793 | 3102 | pos = offset; |
bcba24cc MP |
3103 | inode = file->f_mapping->host; |
3104 | i_size = i_size_read(inode); | |
4273b793 | 3105 | |
6f673763 | 3106 | if ((iov_iter_rw(iter) == READ) && (offset > i_size)) |
9fe55eea SW |
3107 | return 0; |
3108 | ||
439ee5f0 | 3109 | /* optimization for short read */ |
6f673763 | 3110 | if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) { |
439ee5f0 MP |
3111 | if (offset >= i_size) |
3112 | return 0; | |
5da784cc | 3113 | iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset)); |
6b775b18 | 3114 | count = iov_iter_count(iter); |
439ee5f0 MP |
3115 | } |
3116 | ||
bcba24cc | 3117 | io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL); |
36cf66ed MP |
3118 | if (!io) |
3119 | return -ENOMEM; | |
bcba24cc | 3120 | spin_lock_init(&io->lock); |
744742d6 | 3121 | kref_init(&io->refcnt); |
bcba24cc MP |
3122 | io->reqs = 1; |
3123 | io->bytes = -1; | |
3124 | io->size = 0; | |
3125 | io->offset = offset; | |
6f673763 | 3126 | io->write = (iov_iter_rw(iter) == WRITE); |
bcba24cc | 3127 | io->err = 0; |
bcba24cc MP |
3128 | /* |
3129 | * By default, we want to optimize all I/Os with async request | |
60b9df7a | 3130 | * submission to the client filesystem if supported. |
bcba24cc | 3131 | */ |
e5c5f05d | 3132 | io->async = async_dio; |
bcba24cc | 3133 | io->iocb = iocb; |
7879c4e5 | 3134 | io->blocking = is_sync_kiocb(iocb); |
bcba24cc MP |
3135 | |
3136 | /* | |
7879c4e5 AS |
3137 | * We cannot asynchronously extend the size of a file. |
3138 | * In such case the aio will behave exactly like sync io. | |
bcba24cc | 3139 | */ |
7879c4e5 AS |
3140 | if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE) |
3141 | io->blocking = true; | |
4273b793 | 3142 | |
7879c4e5 | 3143 | if (io->async && io->blocking) { |
744742d6 SF |
3144 | /* |
3145 | * Additional reference to keep io around after | |
3146 | * calling fuse_aio_complete() | |
3147 | */ | |
3148 | kref_get(&io->refcnt); | |
9d5722b7 | 3149 | io->done = &wait; |
744742d6 | 3150 | } |
9d5722b7 | 3151 | |
6f673763 | 3152 | if (iov_iter_rw(iter) == WRITE) { |
6b775b18 | 3153 | ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE); |
812408fb AV |
3154 | fuse_invalidate_attr(inode); |
3155 | } else { | |
d22a943f | 3156 | ret = __fuse_direct_read(io, iter, &pos); |
812408fb | 3157 | } |
36cf66ed | 3158 | |
bcba24cc | 3159 | if (io->async) { |
ebacb812 LC |
3160 | bool blocking = io->blocking; |
3161 | ||
bcba24cc MP |
3162 | fuse_aio_complete(io, ret < 0 ? ret : 0, -1); |
3163 | ||
3164 | /* we have a non-extending, async request, so return */ | |
ebacb812 | 3165 | if (!blocking) |
bcba24cc MP |
3166 | return -EIOCBQUEUED; |
3167 | ||
9d5722b7 CH |
3168 | wait_for_completion(&wait); |
3169 | ret = fuse_get_res_by_io(io); | |
bcba24cc MP |
3170 | } |
3171 | ||
744742d6 | 3172 | kref_put(&io->refcnt, fuse_io_release); |
9d5722b7 | 3173 | |
6f673763 | 3174 | if (iov_iter_rw(iter) == WRITE) { |
efb9fa9e MP |
3175 | if (ret > 0) |
3176 | fuse_write_update_size(inode, pos); | |
3177 | else if (ret < 0 && offset + count > i_size) | |
3178 | fuse_do_truncate(file); | |
3179 | } | |
4273b793 AA |
3180 | |
3181 | return ret; | |
3182 | } | |
3183 | ||
26eb3bae MS |
3184 | static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end) |
3185 | { | |
3186 | int err = filemap_write_and_wait_range(inode->i_mapping, start, end); | |
3187 | ||
3188 | if (!err) | |
3189 | fuse_sync_writes(inode); | |
3190 | ||
3191 | return err; | |
3192 | } | |
3193 | ||
cdadb11c MS |
3194 | static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, |
3195 | loff_t length) | |
05ba1f08 AP |
3196 | { |
3197 | struct fuse_file *ff = file->private_data; | |
1c68271c | 3198 | struct inode *inode = file_inode(file); |
0ab08f57 | 3199 | struct fuse_inode *fi = get_fuse_inode(inode); |
05ba1f08 | 3200 | struct fuse_conn *fc = ff->fc; |
7078187a | 3201 | FUSE_ARGS(args); |
05ba1f08 AP |
3202 | struct fuse_fallocate_in inarg = { |
3203 | .fh = ff->fh, | |
3204 | .offset = offset, | |
3205 | .length = length, | |
3206 | .mode = mode | |
3207 | }; | |
3208 | int err; | |
14c14414 MP |
3209 | bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) || |
3210 | (mode & FALLOC_FL_PUNCH_HOLE); | |
05ba1f08 | 3211 | |
4adb8302 MS |
3212 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
3213 | return -EOPNOTSUPP; | |
3214 | ||
519c6040 MS |
3215 | if (fc->no_fallocate) |
3216 | return -EOPNOTSUPP; | |
3217 | ||
14c14414 | 3218 | if (lock_inode) { |
5955102c | 3219 | inode_lock(inode); |
bde52788 MP |
3220 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
3221 | loff_t endbyte = offset + length - 1; | |
26eb3bae MS |
3222 | |
3223 | err = fuse_writeback_range(inode, offset, endbyte); | |
bde52788 MP |
3224 | if (err) |
3225 | goto out; | |
bde52788 | 3226 | } |
3634a632 BF |
3227 | } |
3228 | ||
0cbade02 LB |
3229 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
3230 | offset + length > i_size_read(inode)) { | |
3231 | err = inode_newsize_ok(inode, offset + length); | |
3232 | if (err) | |
35d6fcbb | 3233 | goto out; |
0cbade02 LB |
3234 | } |
3235 | ||
0ab08f57 MP |
3236 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
3237 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
3238 | ||
d5b48543 MS |
3239 | args.opcode = FUSE_FALLOCATE; |
3240 | args.nodeid = ff->nodeid; | |
3241 | args.in_numargs = 1; | |
3242 | args.in_args[0].size = sizeof(inarg); | |
3243 | args.in_args[0].value = &inarg; | |
7078187a | 3244 | err = fuse_simple_request(fc, &args); |
519c6040 MS |
3245 | if (err == -ENOSYS) { |
3246 | fc->no_fallocate = 1; | |
3247 | err = -EOPNOTSUPP; | |
3248 | } | |
bee6c307 BF |
3249 | if (err) |
3250 | goto out; | |
3251 | ||
3252 | /* we could have extended the file */ | |
b0aa7606 MP |
3253 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
3254 | bool changed = fuse_write_update_size(inode, offset + length); | |
3255 | ||
93d2269d MS |
3256 | if (changed && fc->writeback_cache) |
3257 | file_update_time(file); | |
b0aa7606 | 3258 | } |
bee6c307 BF |
3259 | |
3260 | if (mode & FALLOC_FL_PUNCH_HOLE) | |
3261 | truncate_pagecache_range(inode, offset, offset + length - 1); | |
3262 | ||
3263 | fuse_invalidate_attr(inode); | |
3264 | ||
3634a632 | 3265 | out: |
0ab08f57 MP |
3266 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
3267 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
3268 | ||
bde52788 | 3269 | if (lock_inode) |
5955102c | 3270 | inode_unlock(inode); |
3634a632 | 3271 | |
05ba1f08 AP |
3272 | return err; |
3273 | } | |
05ba1f08 | 3274 | |
64bf5ff5 DC |
3275 | static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in, |
3276 | struct file *file_out, loff_t pos_out, | |
3277 | size_t len, unsigned int flags) | |
88bc7d50 NV |
3278 | { |
3279 | struct fuse_file *ff_in = file_in->private_data; | |
3280 | struct fuse_file *ff_out = file_out->private_data; | |
a2bc9236 | 3281 | struct inode *inode_in = file_inode(file_in); |
88bc7d50 NV |
3282 | struct inode *inode_out = file_inode(file_out); |
3283 | struct fuse_inode *fi_out = get_fuse_inode(inode_out); | |
3284 | struct fuse_conn *fc = ff_in->fc; | |
3285 | FUSE_ARGS(args); | |
3286 | struct fuse_copy_file_range_in inarg = { | |
3287 | .fh_in = ff_in->fh, | |
3288 | .off_in = pos_in, | |
3289 | .nodeid_out = ff_out->nodeid, | |
3290 | .fh_out = ff_out->fh, | |
3291 | .off_out = pos_out, | |
3292 | .len = len, | |
3293 | .flags = flags | |
3294 | }; | |
3295 | struct fuse_write_out outarg; | |
3296 | ssize_t err; | |
3297 | /* mark unstable when write-back is not used, and file_out gets | |
3298 | * extended */ | |
3299 | bool is_unstable = (!fc->writeback_cache) && | |
3300 | ((pos_out + len) > inode_out->i_size); | |
3301 | ||
3302 | if (fc->no_copy_file_range) | |
3303 | return -EOPNOTSUPP; | |
3304 | ||
5dae222a AG |
3305 | if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) |
3306 | return -EXDEV; | |
3307 | ||
2c4656df MS |
3308 | inode_lock(inode_in); |
3309 | err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1); | |
3310 | inode_unlock(inode_in); | |
3311 | if (err) | |
3312 | return err; | |
a2bc9236 | 3313 | |
88bc7d50 NV |
3314 | inode_lock(inode_out); |
3315 | ||
fe0da9c0 AG |
3316 | err = file_modified(file_out); |
3317 | if (err) | |
3318 | goto out; | |
3319 | ||
9b46418c MS |
3320 | /* |
3321 | * Write out dirty pages in the destination file before sending the COPY | |
3322 | * request to userspace. After the request is completed, truncate off | |
3323 | * pages (including partial ones) from the cache that have been copied, | |
3324 | * since these contain stale data at that point. | |
3325 | * | |
3326 | * This should be mostly correct, but if the COPY writes to partial | |
3327 | * pages (at the start or end) and the parts not covered by the COPY are | |
3328 | * written through a memory map after calling fuse_writeback_range(), | |
3329 | * then these partial page modifications will be lost on truncation. | |
3330 | * | |
3331 | * It is unlikely that someone would rely on such mixed style | |
3332 | * modifications. Yet this does give less guarantees than if the | |
3333 | * copying was performed with write(2). | |
3334 | * | |
3335 | * To fix this a i_mmap_sem style lock could be used to prevent new | |
3336 | * faults while the copy is ongoing. | |
3337 | */ | |
2c4656df MS |
3338 | err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1); |
3339 | if (err) | |
3340 | goto out; | |
88bc7d50 NV |
3341 | |
3342 | if (is_unstable) | |
3343 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); | |
3344 | ||
d5b48543 MS |
3345 | args.opcode = FUSE_COPY_FILE_RANGE; |
3346 | args.nodeid = ff_in->nodeid; | |
3347 | args.in_numargs = 1; | |
3348 | args.in_args[0].size = sizeof(inarg); | |
3349 | args.in_args[0].value = &inarg; | |
3350 | args.out_numargs = 1; | |
3351 | args.out_args[0].size = sizeof(outarg); | |
3352 | args.out_args[0].value = &outarg; | |
88bc7d50 NV |
3353 | err = fuse_simple_request(fc, &args); |
3354 | if (err == -ENOSYS) { | |
3355 | fc->no_copy_file_range = 1; | |
3356 | err = -EOPNOTSUPP; | |
3357 | } | |
3358 | if (err) | |
3359 | goto out; | |
3360 | ||
9b46418c MS |
3361 | truncate_inode_pages_range(inode_out->i_mapping, |
3362 | ALIGN_DOWN(pos_out, PAGE_SIZE), | |
3363 | ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1); | |
3364 | ||
88bc7d50 NV |
3365 | if (fc->writeback_cache) { |
3366 | fuse_write_update_size(inode_out, pos_out + outarg.size); | |
3367 | file_update_time(file_out); | |
3368 | } | |
3369 | ||
3370 | fuse_invalidate_attr(inode_out); | |
3371 | ||
3372 | err = outarg.size; | |
3373 | out: | |
3374 | if (is_unstable) | |
3375 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); | |
3376 | ||
3377 | inode_unlock(inode_out); | |
fe0da9c0 | 3378 | file_accessed(file_in); |
88bc7d50 NV |
3379 | |
3380 | return err; | |
3381 | } | |
3382 | ||
64bf5ff5 DC |
3383 | static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off, |
3384 | struct file *dst_file, loff_t dst_off, | |
3385 | size_t len, unsigned int flags) | |
3386 | { | |
3387 | ssize_t ret; | |
3388 | ||
3389 | ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off, | |
3390 | len, flags); | |
3391 | ||
5dae222a | 3392 | if (ret == -EOPNOTSUPP || ret == -EXDEV) |
64bf5ff5 DC |
3393 | ret = generic_copy_file_range(src_file, src_off, dst_file, |
3394 | dst_off, len, flags); | |
3395 | return ret; | |
3396 | } | |
3397 | ||
4b6f5d20 | 3398 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 3399 | .llseek = fuse_file_llseek, |
37c20f16 | 3400 | .read_iter = fuse_file_read_iter, |
84c3d55c | 3401 | .write_iter = fuse_file_write_iter, |
b6aeaded MS |
3402 | .mmap = fuse_file_mmap, |
3403 | .open = fuse_open, | |
3404 | .flush = fuse_flush, | |
3405 | .release = fuse_release, | |
3406 | .fsync = fuse_fsync, | |
71421259 | 3407 | .lock = fuse_file_lock, |
a9ff4f87 | 3408 | .flock = fuse_file_flock, |
3c3db095 MS |
3409 | .splice_read = generic_file_splice_read, |
3410 | .splice_write = iter_file_splice_write, | |
59efec7b TH |
3411 | .unlocked_ioctl = fuse_file_ioctl, |
3412 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 3413 | .poll = fuse_file_poll, |
05ba1f08 | 3414 | .fallocate = fuse_file_fallocate, |
d4136d60 | 3415 | .copy_file_range = fuse_copy_file_range, |
413ef8cb MS |
3416 | }; |
3417 | ||
f5e54d6e | 3418 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 3419 | .readpage = fuse_readpage, |
76a0294e | 3420 | .readahead = fuse_readahead, |
3be5a52b | 3421 | .writepage = fuse_writepage, |
26d614df | 3422 | .writepages = fuse_writepages, |
3be5a52b | 3423 | .launder_page = fuse_launder_page, |
3be5a52b | 3424 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 3425 | .bmap = fuse_bmap, |
4273b793 | 3426 | .direct_IO = fuse_direct_IO, |
6b12c1b3 PE |
3427 | .write_begin = fuse_write_begin, |
3428 | .write_end = fuse_write_end, | |
b6aeaded MS |
3429 | }; |
3430 | ||
3431 | void fuse_init_file_inode(struct inode *inode) | |
3432 | { | |
ab2257e9 MS |
3433 | struct fuse_inode *fi = get_fuse_inode(inode); |
3434 | ||
45323fb7 MS |
3435 | inode->i_fop = &fuse_file_operations; |
3436 | inode->i_data.a_ops = &fuse_file_aops; | |
ab2257e9 MS |
3437 | |
3438 | INIT_LIST_HEAD(&fi->write_files); | |
3439 | INIT_LIST_HEAD(&fi->queued_writes); | |
3440 | fi->writectr = 0; | |
3441 | init_waitqueue_head(&fi->page_waitq); | |
6b2fb799 | 3442 | fi->writepages = RB_ROOT; |
b6aeaded | 3443 | } |