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