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