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> |
b6aeaded | 15 | |
4b6f5d20 | 16 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 17 | |
fd72faac MS |
18 | static int fuse_send_open(struct inode *inode, struct file *file, int isdir, |
19 | struct fuse_open_out *outargp) | |
b6aeaded MS |
20 | { |
21 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b6aeaded | 22 | struct fuse_open_in inarg; |
fd72faac MS |
23 | struct fuse_req *req; |
24 | int err; | |
25 | ||
ce1d5a49 MS |
26 | req = fuse_get_req(fc); |
27 | if (IS_ERR(req)) | |
28 | return PTR_ERR(req); | |
fd72faac MS |
29 | |
30 | memset(&inarg, 0, sizeof(inarg)); | |
6ff958ed MS |
31 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
32 | if (!fc->atomic_o_trunc) | |
33 | inarg.flags &= ~O_TRUNC; | |
fd72faac MS |
34 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
35 | req->in.h.nodeid = get_node_id(inode); | |
fd72faac MS |
36 | req->in.numargs = 1; |
37 | req->in.args[0].size = sizeof(inarg); | |
38 | req->in.args[0].value = &inarg; | |
39 | req->out.numargs = 1; | |
40 | req->out.args[0].size = sizeof(*outargp); | |
41 | req->out.args[0].value = outargp; | |
42 | request_send(fc, req); | |
43 | err = req->out.h.error; | |
44 | fuse_put_request(fc, req); | |
45 | ||
46 | return err; | |
47 | } | |
48 | ||
acf99433 | 49 | struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) |
fd72faac MS |
50 | { |
51 | struct fuse_file *ff; | |
52 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); | |
53 | if (ff) { | |
33649c91 MS |
54 | ff->reserved_req = fuse_request_alloc(); |
55 | if (!ff->reserved_req) { | |
fd72faac MS |
56 | kfree(ff); |
57 | ff = NULL; | |
8744969a AB |
58 | } else { |
59 | INIT_LIST_HEAD(&ff->write_entry); | |
60 | atomic_set(&ff->count, 0); | |
acf99433 TH |
61 | spin_lock(&fc->lock); |
62 | ff->kh = ++fc->khctr; | |
63 | spin_unlock(&fc->lock); | |
fd72faac MS |
64 | } |
65 | } | |
66 | return ff; | |
67 | } | |
68 | ||
69 | void fuse_file_free(struct fuse_file *ff) | |
70 | { | |
33649c91 | 71 | fuse_request_free(ff->reserved_req); |
fd72faac MS |
72 | kfree(ff); |
73 | } | |
74 | ||
c756e0a4 MS |
75 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) |
76 | { | |
77 | atomic_inc(&ff->count); | |
78 | return ff; | |
79 | } | |
80 | ||
819c4b3b MS |
81 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
82 | { | |
b57d4264 MS |
83 | dput(req->misc.release.dentry); |
84 | mntput(req->misc.release.vfsmount); | |
819c4b3b MS |
85 | } |
86 | ||
c756e0a4 MS |
87 | static void fuse_file_put(struct fuse_file *ff) |
88 | { | |
89 | if (atomic_dec_and_test(&ff->count)) { | |
90 | struct fuse_req *req = ff->reserved_req; | |
b57d4264 MS |
91 | struct inode *inode = req->misc.release.dentry->d_inode; |
92 | struct fuse_conn *fc = get_fuse_conn(inode); | |
819c4b3b | 93 | req->end = fuse_release_end; |
c756e0a4 MS |
94 | request_send_background(fc, req); |
95 | kfree(ff); | |
96 | } | |
97 | } | |
98 | ||
fd72faac MS |
99 | void fuse_finish_open(struct inode *inode, struct file *file, |
100 | struct fuse_file *ff, struct fuse_open_out *outarg) | |
101 | { | |
102 | if (outarg->open_flags & FOPEN_DIRECT_IO) | |
103 | file->f_op = &fuse_direct_io_file_operations; | |
104 | if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) | |
b1009979 | 105 | invalidate_inode_pages2(inode->i_mapping); |
a7c1b990 TH |
106 | if (outarg->open_flags & FOPEN_NONSEEKABLE) |
107 | nonseekable_open(inode, file); | |
fd72faac | 108 | ff->fh = outarg->fh; |
c756e0a4 | 109 | file->private_data = fuse_file_get(ff); |
fd72faac MS |
110 | } |
111 | ||
112 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) | |
113 | { | |
acf99433 | 114 | struct fuse_conn *fc = get_fuse_conn(inode); |
b6aeaded MS |
115 | struct fuse_open_out outarg; |
116 | struct fuse_file *ff; | |
117 | int err; | |
b6aeaded | 118 | |
dd190d06 MS |
119 | /* VFS checks this, but only _after_ ->open() */ |
120 | if (file->f_flags & O_DIRECT) | |
121 | return -EINVAL; | |
122 | ||
b6aeaded MS |
123 | err = generic_file_open(inode, file); |
124 | if (err) | |
125 | return err; | |
126 | ||
acf99433 | 127 | ff = fuse_file_alloc(fc); |
b6aeaded | 128 | if (!ff) |
fd72faac | 129 | return -ENOMEM; |
b6aeaded | 130 | |
fd72faac MS |
131 | err = fuse_send_open(inode, file, isdir, &outarg); |
132 | if (err) | |
133 | fuse_file_free(ff); | |
134 | else { | |
135 | if (isdir) | |
136 | outarg.open_flags &= ~FOPEN_DIRECT_IO; | |
137 | fuse_finish_open(inode, file, ff, &outarg); | |
b6aeaded MS |
138 | } |
139 | ||
b6aeaded MS |
140 | return err; |
141 | } | |
142 | ||
c756e0a4 | 143 | void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode) |
64c6d8ed | 144 | { |
33649c91 | 145 | struct fuse_req *req = ff->reserved_req; |
b57d4264 | 146 | struct fuse_release_in *inarg = &req->misc.release.in; |
b6aeaded MS |
147 | |
148 | inarg->fh = ff->fh; | |
fd72faac | 149 | inarg->flags = flags; |
51eb01e7 | 150 | req->in.h.opcode = opcode; |
fd72faac | 151 | req->in.h.nodeid = nodeid; |
b6aeaded MS |
152 | req->in.numargs = 1; |
153 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
154 | req->in.args[0].value = inarg; | |
fd72faac MS |
155 | } |
156 | ||
157 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) | |
158 | { | |
159 | struct fuse_file *ff = file->private_data; | |
160 | if (ff) { | |
93a8c3cd | 161 | struct fuse_conn *fc = get_fuse_conn(inode); |
b57d4264 | 162 | struct fuse_req *req = ff->reserved_req; |
93a8c3cd | 163 | |
c756e0a4 MS |
164 | fuse_release_fill(ff, get_node_id(inode), file->f_flags, |
165 | isdir ? FUSE_RELEASEDIR : FUSE_RELEASE); | |
51eb01e7 MS |
166 | |
167 | /* Hold vfsmount and dentry until release is finished */ | |
b57d4264 MS |
168 | req->misc.release.vfsmount = mntget(file->f_path.mnt); |
169 | req->misc.release.dentry = dget(file->f_path.dentry); | |
93a8c3cd MS |
170 | |
171 | spin_lock(&fc->lock); | |
172 | list_del(&ff->write_entry); | |
173 | spin_unlock(&fc->lock); | |
c756e0a4 MS |
174 | /* |
175 | * Normally this will send the RELEASE request, | |
176 | * however if some asynchronous READ or WRITE requests | |
177 | * are outstanding, the sending will be delayed | |
178 | */ | |
179 | fuse_file_put(ff); | |
fd72faac | 180 | } |
b6aeaded MS |
181 | |
182 | /* Return value is ignored by VFS */ | |
183 | return 0; | |
184 | } | |
185 | ||
04730fef MS |
186 | static int fuse_open(struct inode *inode, struct file *file) |
187 | { | |
188 | return fuse_open_common(inode, file, 0); | |
189 | } | |
190 | ||
191 | static int fuse_release(struct inode *inode, struct file *file) | |
192 | { | |
193 | return fuse_release_common(inode, file, 0); | |
194 | } | |
195 | ||
71421259 | 196 | /* |
9c8ef561 MS |
197 | * Scramble the ID space with XTEA, so that the value of the files_struct |
198 | * pointer is not exposed to userspace. | |
71421259 | 199 | */ |
f3332114 | 200 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 201 | { |
9c8ef561 MS |
202 | u32 *k = fc->scramble_key; |
203 | u64 v = (unsigned long) id; | |
204 | u32 v0 = v; | |
205 | u32 v1 = v >> 32; | |
206 | u32 sum = 0; | |
207 | int i; | |
208 | ||
209 | for (i = 0; i < 32; i++) { | |
210 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
211 | sum += 0x9E3779B9; | |
212 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
213 | } | |
214 | ||
215 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
216 | } |
217 | ||
3be5a52b MS |
218 | /* |
219 | * Check if page is under writeback | |
220 | * | |
221 | * This is currently done by walking the list of writepage requests | |
222 | * for the inode, which can be pretty inefficient. | |
223 | */ | |
224 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) | |
225 | { | |
226 | struct fuse_conn *fc = get_fuse_conn(inode); | |
227 | struct fuse_inode *fi = get_fuse_inode(inode); | |
228 | struct fuse_req *req; | |
229 | bool found = false; | |
230 | ||
231 | spin_lock(&fc->lock); | |
232 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | |
233 | pgoff_t curr_index; | |
234 | ||
235 | BUG_ON(req->inode != inode); | |
236 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; | |
237 | if (curr_index == index) { | |
238 | found = true; | |
239 | break; | |
240 | } | |
241 | } | |
242 | spin_unlock(&fc->lock); | |
243 | ||
244 | return found; | |
245 | } | |
246 | ||
247 | /* | |
248 | * Wait for page writeback to be completed. | |
249 | * | |
250 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
251 | * use some other means. | |
252 | */ | |
253 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) | |
254 | { | |
255 | struct fuse_inode *fi = get_fuse_inode(inode); | |
256 | ||
257 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
258 | return 0; | |
259 | } | |
260 | ||
75e1fcc0 | 261 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 262 | { |
7706a9d6 | 263 | struct inode *inode = file->f_path.dentry->d_inode; |
b6aeaded MS |
264 | struct fuse_conn *fc = get_fuse_conn(inode); |
265 | struct fuse_file *ff = file->private_data; | |
266 | struct fuse_req *req; | |
267 | struct fuse_flush_in inarg; | |
268 | int err; | |
269 | ||
248d86e8 MS |
270 | if (is_bad_inode(inode)) |
271 | return -EIO; | |
272 | ||
b6aeaded MS |
273 | if (fc->no_flush) |
274 | return 0; | |
275 | ||
33649c91 | 276 | req = fuse_get_req_nofail(fc, file); |
b6aeaded MS |
277 | memset(&inarg, 0, sizeof(inarg)); |
278 | inarg.fh = ff->fh; | |
9c8ef561 | 279 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
b6aeaded MS |
280 | req->in.h.opcode = FUSE_FLUSH; |
281 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
282 | req->in.numargs = 1; |
283 | req->in.args[0].size = sizeof(inarg); | |
284 | req->in.args[0].value = &inarg; | |
71421259 | 285 | req->force = 1; |
7c352bdf | 286 | request_send(fc, req); |
b6aeaded MS |
287 | err = req->out.h.error; |
288 | fuse_put_request(fc, req); | |
289 | if (err == -ENOSYS) { | |
290 | fc->no_flush = 1; | |
291 | err = 0; | |
292 | } | |
293 | return err; | |
294 | } | |
295 | ||
3be5a52b MS |
296 | /* |
297 | * Wait for all pending writepages on the inode to finish. | |
298 | * | |
299 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
300 | * and waiting for all sent writes to complete. | |
301 | * | |
302 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
303 | * could conflict with truncation. | |
304 | */ | |
305 | static void fuse_sync_writes(struct inode *inode) | |
306 | { | |
307 | fuse_set_nowrite(inode); | |
308 | fuse_release_nowrite(inode); | |
309 | } | |
310 | ||
82547981 MS |
311 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
312 | int isdir) | |
b6aeaded MS |
313 | { |
314 | struct inode *inode = de->d_inode; | |
315 | struct fuse_conn *fc = get_fuse_conn(inode); | |
316 | struct fuse_file *ff = file->private_data; | |
317 | struct fuse_req *req; | |
318 | struct fuse_fsync_in inarg; | |
319 | int err; | |
320 | ||
248d86e8 MS |
321 | if (is_bad_inode(inode)) |
322 | return -EIO; | |
323 | ||
82547981 | 324 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
325 | return 0; |
326 | ||
3be5a52b MS |
327 | /* |
328 | * Start writeback against all dirty pages of the inode, then | |
329 | * wait for all outstanding writes, before sending the FSYNC | |
330 | * request. | |
331 | */ | |
332 | err = write_inode_now(inode, 0); | |
333 | if (err) | |
334 | return err; | |
335 | ||
336 | fuse_sync_writes(inode); | |
337 | ||
ce1d5a49 MS |
338 | req = fuse_get_req(fc); |
339 | if (IS_ERR(req)) | |
340 | return PTR_ERR(req); | |
b6aeaded MS |
341 | |
342 | memset(&inarg, 0, sizeof(inarg)); | |
343 | inarg.fh = ff->fh; | |
344 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 345 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 346 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
347 | req->in.numargs = 1; |
348 | req->in.args[0].size = sizeof(inarg); | |
349 | req->in.args[0].value = &inarg; | |
350 | request_send(fc, req); | |
351 | err = req->out.h.error; | |
352 | fuse_put_request(fc, req); | |
353 | if (err == -ENOSYS) { | |
82547981 MS |
354 | if (isdir) |
355 | fc->no_fsyncdir = 1; | |
356 | else | |
357 | fc->no_fsync = 1; | |
b6aeaded MS |
358 | err = 0; |
359 | } | |
360 | return err; | |
361 | } | |
362 | ||
82547981 MS |
363 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
364 | { | |
365 | return fuse_fsync_common(file, de, datasync, 0); | |
366 | } | |
367 | ||
a6643094 | 368 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
361b1eb5 | 369 | struct inode *inode, loff_t pos, size_t count, int opcode) |
b6aeaded | 370 | { |
5c5c5e51 | 371 | struct fuse_read_in *inarg = &req->misc.read.in; |
a6643094 | 372 | struct fuse_file *ff = file->private_data; |
b6aeaded | 373 | |
361b1eb5 MS |
374 | inarg->fh = ff->fh; |
375 | inarg->offset = pos; | |
376 | inarg->size = count; | |
a6643094 | 377 | inarg->flags = file->f_flags; |
361b1eb5 | 378 | req->in.h.opcode = opcode; |
b6aeaded | 379 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
380 | req->in.numargs = 1; |
381 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 382 | req->in.args[0].value = inarg; |
b6aeaded MS |
383 | req->out.argpages = 1; |
384 | req->out.argvar = 1; | |
385 | req->out.numargs = 1; | |
386 | req->out.args[0].size = count; | |
b6aeaded MS |
387 | } |
388 | ||
8bfc016d | 389 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
f3332114 MS |
390 | struct inode *inode, loff_t pos, size_t count, |
391 | fl_owner_t owner) | |
04730fef | 392 | { |
361b1eb5 | 393 | struct fuse_conn *fc = get_fuse_conn(inode); |
f3332114 | 394 | |
a6643094 | 395 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
f3332114 | 396 | if (owner != NULL) { |
5c5c5e51 | 397 | struct fuse_read_in *inarg = &req->misc.read.in; |
f3332114 MS |
398 | |
399 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | |
400 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
401 | } | |
361b1eb5 MS |
402 | request_send(fc, req); |
403 | return req->out.args[0].size; | |
04730fef MS |
404 | } |
405 | ||
5c5c5e51 MS |
406 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
407 | u64 attr_ver) | |
408 | { | |
409 | struct fuse_conn *fc = get_fuse_conn(inode); | |
410 | struct fuse_inode *fi = get_fuse_inode(inode); | |
411 | ||
412 | spin_lock(&fc->lock); | |
413 | if (attr_ver == fi->attr_version && size < inode->i_size) { | |
414 | fi->attr_version = ++fc->attr_version; | |
415 | i_size_write(inode, size); | |
416 | } | |
417 | spin_unlock(&fc->lock); | |
418 | } | |
419 | ||
b6aeaded MS |
420 | static int fuse_readpage(struct file *file, struct page *page) |
421 | { | |
422 | struct inode *inode = page->mapping->host; | |
423 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 | 424 | struct fuse_req *req; |
5c5c5e51 MS |
425 | size_t num_read; |
426 | loff_t pos = page_offset(page); | |
427 | size_t count = PAGE_CACHE_SIZE; | |
428 | u64 attr_ver; | |
248d86e8 MS |
429 | int err; |
430 | ||
431 | err = -EIO; | |
432 | if (is_bad_inode(inode)) | |
433 | goto out; | |
434 | ||
3be5a52b MS |
435 | /* |
436 | * Page writeback can extend beyond the liftime of the | |
437 | * page-cache page, so make sure we read a properly synced | |
438 | * page. | |
439 | */ | |
440 | fuse_wait_on_page_writeback(inode, page->index); | |
441 | ||
ce1d5a49 MS |
442 | req = fuse_get_req(fc); |
443 | err = PTR_ERR(req); | |
444 | if (IS_ERR(req)) | |
b6aeaded MS |
445 | goto out; |
446 | ||
5c5c5e51 MS |
447 | attr_ver = fuse_get_attr_version(fc); |
448 | ||
b6aeaded MS |
449 | req->out.page_zeroing = 1; |
450 | req->num_pages = 1; | |
451 | req->pages[0] = page; | |
5c5c5e51 | 452 | num_read = fuse_send_read(req, file, inode, pos, count, NULL); |
b6aeaded MS |
453 | err = req->out.h.error; |
454 | fuse_put_request(fc, req); | |
5c5c5e51 MS |
455 | |
456 | if (!err) { | |
457 | /* | |
458 | * Short read means EOF. If file size is larger, truncate it | |
459 | */ | |
460 | if (num_read < count) | |
461 | fuse_read_update_size(inode, pos + num_read, attr_ver); | |
462 | ||
b6aeaded | 463 | SetPageUptodate(page); |
5c5c5e51 MS |
464 | } |
465 | ||
b36c31ba | 466 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
467 | out: |
468 | unlock_page(page); | |
469 | return err; | |
470 | } | |
471 | ||
c1aa96a5 | 472 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 473 | { |
c1aa96a5 | 474 | int i; |
5c5c5e51 MS |
475 | size_t count = req->misc.read.in.size; |
476 | size_t num_read = req->out.args[0].size; | |
477 | struct inode *inode = req->pages[0]->mapping->host; | |
c1aa96a5 | 478 | |
5c5c5e51 MS |
479 | /* |
480 | * Short read means EOF. If file size is larger, truncate it | |
481 | */ | |
482 | if (!req->out.h.error && num_read < count) { | |
483 | loff_t pos = page_offset(req->pages[0]) + num_read; | |
484 | fuse_read_update_size(inode, pos, req->misc.read.attr_ver); | |
485 | } | |
486 | ||
487 | fuse_invalidate_attr(inode); /* atime changed */ | |
c1aa96a5 | 488 | |
db50b96c MS |
489 | for (i = 0; i < req->num_pages; i++) { |
490 | struct page *page = req->pages[i]; | |
491 | if (!req->out.h.error) | |
492 | SetPageUptodate(page); | |
c1aa96a5 MS |
493 | else |
494 | SetPageError(page); | |
db50b96c MS |
495 | unlock_page(page); |
496 | } | |
c756e0a4 MS |
497 | if (req->ff) |
498 | fuse_file_put(req->ff); | |
c1aa96a5 MS |
499 | } |
500 | ||
a6643094 | 501 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, |
c1aa96a5 MS |
502 | struct inode *inode) |
503 | { | |
504 | struct fuse_conn *fc = get_fuse_conn(inode); | |
505 | loff_t pos = page_offset(req->pages[0]); | |
506 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
507 | req->out.page_zeroing = 1; | |
a6643094 | 508 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
5c5c5e51 | 509 | req->misc.read.attr_ver = fuse_get_attr_version(fc); |
9cd68455 | 510 | if (fc->async_read) { |
a6643094 | 511 | struct fuse_file *ff = file->private_data; |
c756e0a4 | 512 | req->ff = fuse_file_get(ff); |
9cd68455 MS |
513 | req->end = fuse_readpages_end; |
514 | request_send_background(fc, req); | |
515 | } else { | |
516 | request_send(fc, req); | |
517 | fuse_readpages_end(fc, req); | |
e9bb09dd | 518 | fuse_put_request(fc, req); |
9cd68455 | 519 | } |
db50b96c MS |
520 | } |
521 | ||
c756e0a4 | 522 | struct fuse_fill_data { |
db50b96c | 523 | struct fuse_req *req; |
a6643094 | 524 | struct file *file; |
db50b96c MS |
525 | struct inode *inode; |
526 | }; | |
527 | ||
528 | static int fuse_readpages_fill(void *_data, struct page *page) | |
529 | { | |
c756e0a4 | 530 | struct fuse_fill_data *data = _data; |
db50b96c MS |
531 | struct fuse_req *req = data->req; |
532 | struct inode *inode = data->inode; | |
533 | struct fuse_conn *fc = get_fuse_conn(inode); | |
534 | ||
3be5a52b MS |
535 | fuse_wait_on_page_writeback(inode, page->index); |
536 | ||
db50b96c MS |
537 | if (req->num_pages && |
538 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
539 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
540 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
a6643094 | 541 | fuse_send_readpages(req, data->file, inode); |
ce1d5a49 MS |
542 | data->req = req = fuse_get_req(fc); |
543 | if (IS_ERR(req)) { | |
db50b96c | 544 | unlock_page(page); |
ce1d5a49 | 545 | return PTR_ERR(req); |
db50b96c | 546 | } |
db50b96c MS |
547 | } |
548 | req->pages[req->num_pages] = page; | |
1729a16c | 549 | req->num_pages++; |
db50b96c MS |
550 | return 0; |
551 | } | |
552 | ||
553 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
554 | struct list_head *pages, unsigned nr_pages) | |
555 | { | |
556 | struct inode *inode = mapping->host; | |
557 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c756e0a4 | 558 | struct fuse_fill_data data; |
db50b96c | 559 | int err; |
248d86e8 | 560 | |
1d7ea732 | 561 | err = -EIO; |
248d86e8 | 562 | if (is_bad_inode(inode)) |
2e990021 | 563 | goto out; |
248d86e8 | 564 | |
a6643094 | 565 | data.file = file; |
db50b96c | 566 | data.inode = inode; |
ce1d5a49 | 567 | data.req = fuse_get_req(fc); |
1d7ea732 | 568 | err = PTR_ERR(data.req); |
ce1d5a49 | 569 | if (IS_ERR(data.req)) |
2e990021 | 570 | goto out; |
db50b96c MS |
571 | |
572 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
573 | if (!err) { |
574 | if (data.req->num_pages) | |
a6643094 | 575 | fuse_send_readpages(data.req, file, inode); |
d3406ffa MS |
576 | else |
577 | fuse_put_request(fc, data.req); | |
578 | } | |
2e990021 | 579 | out: |
1d7ea732 | 580 | return err; |
db50b96c MS |
581 | } |
582 | ||
bcb4be80 MS |
583 | static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
584 | unsigned long nr_segs, loff_t pos) | |
585 | { | |
586 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
587 | ||
588 | if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) { | |
589 | int err; | |
590 | /* | |
591 | * If trying to read past EOF, make sure the i_size | |
592 | * attribute is up-to-date. | |
593 | */ | |
594 | err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL); | |
595 | if (err) | |
596 | return err; | |
597 | } | |
598 | ||
599 | return generic_file_aio_read(iocb, iov, nr_segs, pos); | |
600 | } | |
601 | ||
a6643094 | 602 | static void fuse_write_fill(struct fuse_req *req, struct file *file, |
3be5a52b MS |
603 | struct fuse_file *ff, struct inode *inode, |
604 | loff_t pos, size_t count, int writepage) | |
b6aeaded | 605 | { |
f3332114 | 606 | struct fuse_conn *fc = get_fuse_conn(inode); |
b25e82e5 MS |
607 | struct fuse_write_in *inarg = &req->misc.write.in; |
608 | struct fuse_write_out *outarg = &req->misc.write.out; | |
b6aeaded | 609 | |
b25e82e5 MS |
610 | memset(inarg, 0, sizeof(struct fuse_write_in)); |
611 | inarg->fh = ff->fh; | |
612 | inarg->offset = pos; | |
613 | inarg->size = count; | |
614 | inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0; | |
3be5a52b | 615 | inarg->flags = file ? file->f_flags : 0; |
b6aeaded MS |
616 | req->in.h.opcode = FUSE_WRITE; |
617 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
618 | req->in.argpages = 1; |
619 | req->in.numargs = 2; | |
f3332114 MS |
620 | if (fc->minor < 9) |
621 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; | |
622 | else | |
623 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
b25e82e5 | 624 | req->in.args[0].value = inarg; |
b6aeaded MS |
625 | req->in.args[1].size = count; |
626 | req->out.numargs = 1; | |
627 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
b25e82e5 MS |
628 | req->out.args[0].value = outarg; |
629 | } | |
630 | ||
631 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, | |
f3332114 MS |
632 | struct inode *inode, loff_t pos, size_t count, |
633 | fl_owner_t owner) | |
b25e82e5 MS |
634 | { |
635 | struct fuse_conn *fc = get_fuse_conn(inode); | |
3be5a52b | 636 | fuse_write_fill(req, file, file->private_data, inode, pos, count, 0); |
f3332114 MS |
637 | if (owner != NULL) { |
638 | struct fuse_write_in *inarg = &req->misc.write.in; | |
639 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; | |
640 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
641 | } | |
7c352bdf | 642 | request_send(fc, req); |
b25e82e5 | 643 | return req->misc.write.out.size; |
b6aeaded MS |
644 | } |
645 | ||
5e6f58a1 NP |
646 | static int fuse_write_begin(struct file *file, struct address_space *mapping, |
647 | loff_t pos, unsigned len, unsigned flags, | |
648 | struct page **pagep, void **fsdata) | |
b6aeaded | 649 | { |
5e6f58a1 NP |
650 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
651 | ||
652 | *pagep = __grab_cache_page(mapping, index); | |
653 | if (!*pagep) | |
654 | return -ENOMEM; | |
b6aeaded MS |
655 | return 0; |
656 | } | |
657 | ||
854512ec MS |
658 | static void fuse_write_update_size(struct inode *inode, loff_t pos) |
659 | { | |
660 | struct fuse_conn *fc = get_fuse_conn(inode); | |
661 | struct fuse_inode *fi = get_fuse_inode(inode); | |
662 | ||
663 | spin_lock(&fc->lock); | |
664 | fi->attr_version = ++fc->attr_version; | |
665 | if (pos > inode->i_size) | |
666 | i_size_write(inode, pos); | |
667 | spin_unlock(&fc->lock); | |
668 | } | |
669 | ||
5e6f58a1 NP |
670 | static int fuse_buffered_write(struct file *file, struct inode *inode, |
671 | loff_t pos, unsigned count, struct page *page) | |
b6aeaded MS |
672 | { |
673 | int err; | |
04730fef | 674 | size_t nres; |
b6aeaded | 675 | struct fuse_conn *fc = get_fuse_conn(inode); |
5e6f58a1 | 676 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
248d86e8 MS |
677 | struct fuse_req *req; |
678 | ||
679 | if (is_bad_inode(inode)) | |
680 | return -EIO; | |
681 | ||
3be5a52b MS |
682 | /* |
683 | * Make sure writepages on the same page are not mixed up with | |
684 | * plain writes. | |
685 | */ | |
686 | fuse_wait_on_page_writeback(inode, page->index); | |
687 | ||
ce1d5a49 MS |
688 | req = fuse_get_req(fc); |
689 | if (IS_ERR(req)) | |
690 | return PTR_ERR(req); | |
b6aeaded MS |
691 | |
692 | req->num_pages = 1; | |
693 | req->pages[0] = page; | |
694 | req->page_offset = offset; | |
f3332114 | 695 | nres = fuse_send_write(req, file, inode, pos, count, NULL); |
b6aeaded MS |
696 | err = req->out.h.error; |
697 | fuse_put_request(fc, req); | |
5e6f58a1 | 698 | if (!err && !nres) |
b6aeaded MS |
699 | err = -EIO; |
700 | if (!err) { | |
5e6f58a1 | 701 | pos += nres; |
854512ec | 702 | fuse_write_update_size(inode, pos); |
5e6f58a1 | 703 | if (count == PAGE_CACHE_SIZE) |
b6aeaded | 704 | SetPageUptodate(page); |
b36c31ba MS |
705 | } |
706 | fuse_invalidate_attr(inode); | |
5e6f58a1 NP |
707 | return err ? err : nres; |
708 | } | |
709 | ||
710 | static int fuse_write_end(struct file *file, struct address_space *mapping, | |
711 | loff_t pos, unsigned len, unsigned copied, | |
712 | struct page *page, void *fsdata) | |
713 | { | |
714 | struct inode *inode = mapping->host; | |
715 | int res = 0; | |
716 | ||
717 | if (copied) | |
718 | res = fuse_buffered_write(file, inode, pos, copied, page); | |
719 | ||
720 | unlock_page(page); | |
721 | page_cache_release(page); | |
722 | return res; | |
b6aeaded MS |
723 | } |
724 | ||
ea9b9907 NP |
725 | static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file, |
726 | struct inode *inode, loff_t pos, | |
727 | size_t count) | |
728 | { | |
729 | size_t res; | |
730 | unsigned offset; | |
731 | unsigned i; | |
732 | ||
733 | for (i = 0; i < req->num_pages; i++) | |
734 | fuse_wait_on_page_writeback(inode, req->pages[i]->index); | |
735 | ||
736 | res = fuse_send_write(req, file, inode, pos, count, NULL); | |
737 | ||
738 | offset = req->page_offset; | |
739 | count = res; | |
740 | for (i = 0; i < req->num_pages; i++) { | |
741 | struct page *page = req->pages[i]; | |
742 | ||
743 | if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE) | |
744 | SetPageUptodate(page); | |
745 | ||
746 | if (count > PAGE_CACHE_SIZE - offset) | |
747 | count -= PAGE_CACHE_SIZE - offset; | |
748 | else | |
749 | count = 0; | |
750 | offset = 0; | |
751 | ||
752 | unlock_page(page); | |
753 | page_cache_release(page); | |
754 | } | |
755 | ||
756 | return res; | |
757 | } | |
758 | ||
759 | static ssize_t fuse_fill_write_pages(struct fuse_req *req, | |
760 | struct address_space *mapping, | |
761 | struct iov_iter *ii, loff_t pos) | |
762 | { | |
763 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
764 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | |
765 | size_t count = 0; | |
766 | int err; | |
767 | ||
768 | req->page_offset = offset; | |
769 | ||
770 | do { | |
771 | size_t tmp; | |
772 | struct page *page; | |
773 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
774 | size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset, | |
775 | iov_iter_count(ii)); | |
776 | ||
777 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
778 | ||
779 | again: | |
780 | err = -EFAULT; | |
781 | if (iov_iter_fault_in_readable(ii, bytes)) | |
782 | break; | |
783 | ||
784 | err = -ENOMEM; | |
785 | page = __grab_cache_page(mapping, index); | |
786 | if (!page) | |
787 | break; | |
788 | ||
789 | pagefault_disable(); | |
790 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); | |
791 | pagefault_enable(); | |
792 | flush_dcache_page(page); | |
793 | ||
794 | if (!tmp) { | |
795 | unlock_page(page); | |
796 | page_cache_release(page); | |
797 | bytes = min(bytes, iov_iter_single_seg_count(ii)); | |
798 | goto again; | |
799 | } | |
800 | ||
801 | err = 0; | |
802 | req->pages[req->num_pages] = page; | |
803 | req->num_pages++; | |
804 | ||
805 | iov_iter_advance(ii, tmp); | |
806 | count += tmp; | |
807 | pos += tmp; | |
808 | offset += tmp; | |
809 | if (offset == PAGE_CACHE_SIZE) | |
810 | offset = 0; | |
811 | ||
78bb6cb9 MS |
812 | if (!fc->big_writes) |
813 | break; | |
ea9b9907 NP |
814 | } while (iov_iter_count(ii) && count < fc->max_write && |
815 | req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0); | |
816 | ||
817 | return count > 0 ? count : err; | |
818 | } | |
819 | ||
820 | static ssize_t fuse_perform_write(struct file *file, | |
821 | struct address_space *mapping, | |
822 | struct iov_iter *ii, loff_t pos) | |
823 | { | |
824 | struct inode *inode = mapping->host; | |
825 | struct fuse_conn *fc = get_fuse_conn(inode); | |
826 | int err = 0; | |
827 | ssize_t res = 0; | |
828 | ||
829 | if (is_bad_inode(inode)) | |
830 | return -EIO; | |
831 | ||
832 | do { | |
833 | struct fuse_req *req; | |
834 | ssize_t count; | |
835 | ||
836 | req = fuse_get_req(fc); | |
837 | if (IS_ERR(req)) { | |
838 | err = PTR_ERR(req); | |
839 | break; | |
840 | } | |
841 | ||
842 | count = fuse_fill_write_pages(req, mapping, ii, pos); | |
843 | if (count <= 0) { | |
844 | err = count; | |
845 | } else { | |
846 | size_t num_written; | |
847 | ||
848 | num_written = fuse_send_write_pages(req, file, inode, | |
849 | pos, count); | |
850 | err = req->out.h.error; | |
851 | if (!err) { | |
852 | res += num_written; | |
853 | pos += num_written; | |
854 | ||
855 | /* break out of the loop on short write */ | |
856 | if (num_written != count) | |
857 | err = -EIO; | |
858 | } | |
859 | } | |
860 | fuse_put_request(fc, req); | |
861 | } while (!err && iov_iter_count(ii)); | |
862 | ||
863 | if (res > 0) | |
864 | fuse_write_update_size(inode, pos); | |
865 | ||
866 | fuse_invalidate_attr(inode); | |
867 | ||
868 | return res > 0 ? res : err; | |
869 | } | |
870 | ||
871 | static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | |
872 | unsigned long nr_segs, loff_t pos) | |
873 | { | |
874 | struct file *file = iocb->ki_filp; | |
875 | struct address_space *mapping = file->f_mapping; | |
876 | size_t count = 0; | |
877 | ssize_t written = 0; | |
878 | struct inode *inode = mapping->host; | |
879 | ssize_t err; | |
880 | struct iov_iter i; | |
881 | ||
882 | WARN_ON(iocb->ki_pos != pos); | |
883 | ||
884 | err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ); | |
885 | if (err) | |
886 | return err; | |
887 | ||
888 | mutex_lock(&inode->i_mutex); | |
889 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
890 | ||
891 | /* We can write back this queue in page reclaim */ | |
892 | current->backing_dev_info = mapping->backing_dev_info; | |
893 | ||
894 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
895 | if (err) | |
896 | goto out; | |
897 | ||
898 | if (count == 0) | |
899 | goto out; | |
900 | ||
2f1936b8 | 901 | err = file_remove_suid(file); |
ea9b9907 NP |
902 | if (err) |
903 | goto out; | |
904 | ||
905 | file_update_time(file); | |
906 | ||
907 | iov_iter_init(&i, iov, nr_segs, count, 0); | |
908 | written = fuse_perform_write(file, mapping, &i, pos); | |
909 | if (written >= 0) | |
910 | iocb->ki_pos = pos + written; | |
911 | ||
912 | out: | |
913 | current->backing_dev_info = NULL; | |
914 | mutex_unlock(&inode->i_mutex); | |
915 | ||
916 | return written ? written : err; | |
917 | } | |
918 | ||
413ef8cb MS |
919 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
920 | { | |
921 | unsigned i; | |
922 | ||
923 | for (i = 0; i < req->num_pages; i++) { | |
924 | struct page *page = req->pages[i]; | |
925 | if (write) | |
926 | set_page_dirty_lock(page); | |
927 | put_page(page); | |
928 | } | |
929 | } | |
930 | ||
931 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | |
932 | unsigned nbytes, int write) | |
933 | { | |
934 | unsigned long user_addr = (unsigned long) buf; | |
935 | unsigned offset = user_addr & ~PAGE_MASK; | |
936 | int npages; | |
937 | ||
938 | /* This doesn't work with nfsd */ | |
939 | if (!current->mm) | |
940 | return -EPERM; | |
941 | ||
942 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | |
943 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
bd730967 | 944 | npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ); |
413ef8cb MS |
945 | down_read(¤t->mm->mmap_sem); |
946 | npages = get_user_pages(current, current->mm, user_addr, npages, write, | |
947 | 0, req->pages, NULL); | |
948 | up_read(¤t->mm->mmap_sem); | |
949 | if (npages < 0) | |
950 | return npages; | |
951 | ||
952 | req->num_pages = npages; | |
953 | req->page_offset = offset; | |
954 | return 0; | |
955 | } | |
956 | ||
957 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |
958 | size_t count, loff_t *ppos, int write) | |
959 | { | |
7706a9d6 | 960 | struct inode *inode = file->f_path.dentry->d_inode; |
413ef8cb MS |
961 | struct fuse_conn *fc = get_fuse_conn(inode); |
962 | size_t nmax = write ? fc->max_write : fc->max_read; | |
963 | loff_t pos = *ppos; | |
964 | ssize_t res = 0; | |
248d86e8 MS |
965 | struct fuse_req *req; |
966 | ||
967 | if (is_bad_inode(inode)) | |
968 | return -EIO; | |
969 | ||
ce1d5a49 MS |
970 | req = fuse_get_req(fc); |
971 | if (IS_ERR(req)) | |
972 | return PTR_ERR(req); | |
413ef8cb MS |
973 | |
974 | while (count) { | |
413ef8cb | 975 | size_t nres; |
e5d9a0df MS |
976 | size_t nbytes_limit = min(count, nmax); |
977 | size_t nbytes; | |
978 | int err = fuse_get_user_pages(req, buf, nbytes_limit, !write); | |
413ef8cb MS |
979 | if (err) { |
980 | res = err; | |
981 | break; | |
982 | } | |
6ad84aca | 983 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
e5d9a0df | 984 | nbytes = min(nbytes_limit, nbytes); |
413ef8cb | 985 | if (write) |
f3332114 MS |
986 | nres = fuse_send_write(req, file, inode, pos, nbytes, |
987 | current->files); | |
413ef8cb | 988 | else |
f3332114 MS |
989 | nres = fuse_send_read(req, file, inode, pos, nbytes, |
990 | current->files); | |
413ef8cb MS |
991 | fuse_release_user_pages(req, !write); |
992 | if (req->out.h.error) { | |
993 | if (!res) | |
994 | res = req->out.h.error; | |
995 | break; | |
996 | } else if (nres > nbytes) { | |
997 | res = -EIO; | |
998 | break; | |
999 | } | |
1000 | count -= nres; | |
1001 | res += nres; | |
1002 | pos += nres; | |
1003 | buf += nres; | |
1004 | if (nres != nbytes) | |
1005 | break; | |
56cf34ff MS |
1006 | if (count) { |
1007 | fuse_put_request(fc, req); | |
1008 | req = fuse_get_req(fc); | |
1009 | if (IS_ERR(req)) | |
1010 | break; | |
1011 | } | |
413ef8cb MS |
1012 | } |
1013 | fuse_put_request(fc, req); | |
1014 | if (res > 0) { | |
854512ec MS |
1015 | if (write) |
1016 | fuse_write_update_size(inode, pos); | |
413ef8cb | 1017 | *ppos = pos; |
b36c31ba MS |
1018 | } |
1019 | fuse_invalidate_attr(inode); | |
413ef8cb MS |
1020 | |
1021 | return res; | |
1022 | } | |
1023 | ||
1024 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, | |
1025 | size_t count, loff_t *ppos) | |
1026 | { | |
1027 | return fuse_direct_io(file, buf, count, ppos, 0); | |
1028 | } | |
1029 | ||
1030 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | |
1031 | size_t count, loff_t *ppos) | |
1032 | { | |
7706a9d6 | 1033 | struct inode *inode = file->f_path.dentry->d_inode; |
413ef8cb MS |
1034 | ssize_t res; |
1035 | /* Don't allow parallel writes to the same file */ | |
1b1dcc1b | 1036 | mutex_lock(&inode->i_mutex); |
889f7848 MS |
1037 | res = generic_write_checks(file, ppos, &count, 0); |
1038 | if (!res) | |
1039 | res = fuse_direct_io(file, buf, count, ppos, 1); | |
1b1dcc1b | 1040 | mutex_unlock(&inode->i_mutex); |
413ef8cb MS |
1041 | return res; |
1042 | } | |
1043 | ||
3be5a52b | 1044 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
b6aeaded | 1045 | { |
3be5a52b MS |
1046 | __free_page(req->pages[0]); |
1047 | fuse_file_put(req->ff); | |
3be5a52b MS |
1048 | } |
1049 | ||
1050 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | |
1051 | { | |
1052 | struct inode *inode = req->inode; | |
1053 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1054 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; | |
1055 | ||
1056 | list_del(&req->writepages_entry); | |
1057 | dec_bdi_stat(bdi, BDI_WRITEBACK); | |
1058 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); | |
1059 | bdi_writeout_inc(bdi); | |
1060 | wake_up(&fi->page_waitq); | |
1061 | } | |
1062 | ||
1063 | /* Called under fc->lock, may release and reacquire it */ | |
1064 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) | |
1065 | { | |
1066 | struct fuse_inode *fi = get_fuse_inode(req->inode); | |
1067 | loff_t size = i_size_read(req->inode); | |
1068 | struct fuse_write_in *inarg = &req->misc.write.in; | |
1069 | ||
1070 | if (!fc->connected) | |
1071 | goto out_free; | |
1072 | ||
1073 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { | |
1074 | inarg->size = PAGE_CACHE_SIZE; | |
1075 | } else if (inarg->offset < size) { | |
1076 | inarg->size = size & (PAGE_CACHE_SIZE - 1); | |
1077 | } else { | |
1078 | /* Got truncated off completely */ | |
1079 | goto out_free; | |
b6aeaded | 1080 | } |
3be5a52b MS |
1081 | |
1082 | req->in.args[1].size = inarg->size; | |
1083 | fi->writectr++; | |
1084 | request_send_background_locked(fc, req); | |
1085 | return; | |
1086 | ||
1087 | out_free: | |
1088 | fuse_writepage_finish(fc, req); | |
1089 | spin_unlock(&fc->lock); | |
1090 | fuse_writepage_free(fc, req); | |
e9bb09dd | 1091 | fuse_put_request(fc, req); |
3be5a52b | 1092 | spin_lock(&fc->lock); |
b6aeaded MS |
1093 | } |
1094 | ||
3be5a52b MS |
1095 | /* |
1096 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1097 | * all queued writepage requests. | |
1098 | * | |
1099 | * Called with fc->lock | |
1100 | */ | |
1101 | void fuse_flush_writepages(struct inode *inode) | |
b6aeaded | 1102 | { |
3be5a52b MS |
1103 | struct fuse_conn *fc = get_fuse_conn(inode); |
1104 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1105 | struct fuse_req *req; | |
1106 | ||
1107 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
1108 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | |
1109 | list_del_init(&req->list); | |
1110 | fuse_send_writepage(fc, req); | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | |
1115 | { | |
1116 | struct inode *inode = req->inode; | |
1117 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1118 | ||
1119 | mapping_set_error(inode->i_mapping, req->out.h.error); | |
1120 | spin_lock(&fc->lock); | |
1121 | fi->writectr--; | |
1122 | fuse_writepage_finish(fc, req); | |
1123 | spin_unlock(&fc->lock); | |
1124 | fuse_writepage_free(fc, req); | |
1125 | } | |
1126 | ||
1127 | static int fuse_writepage_locked(struct page *page) | |
1128 | { | |
1129 | struct address_space *mapping = page->mapping; | |
1130 | struct inode *inode = mapping->host; | |
1131 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1132 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1133 | struct fuse_req *req; | |
1134 | struct fuse_file *ff; | |
1135 | struct page *tmp_page; | |
1136 | ||
1137 | set_page_writeback(page); | |
1138 | ||
1139 | req = fuse_request_alloc_nofs(); | |
1140 | if (!req) | |
1141 | goto err; | |
1142 | ||
1143 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
1144 | if (!tmp_page) | |
1145 | goto err_free; | |
1146 | ||
1147 | spin_lock(&fc->lock); | |
1148 | BUG_ON(list_empty(&fi->write_files)); | |
1149 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); | |
1150 | req->ff = fuse_file_get(ff); | |
1151 | spin_unlock(&fc->lock); | |
1152 | ||
1153 | fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1); | |
1154 | ||
1155 | copy_highpage(tmp_page, page); | |
1156 | req->num_pages = 1; | |
1157 | req->pages[0] = tmp_page; | |
1158 | req->page_offset = 0; | |
1159 | req->end = fuse_writepage_end; | |
1160 | req->inode = inode; | |
1161 | ||
1162 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); | |
1163 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); | |
1164 | end_page_writeback(page); | |
1165 | ||
1166 | spin_lock(&fc->lock); | |
1167 | list_add(&req->writepages_entry, &fi->writepages); | |
1168 | list_add_tail(&req->list, &fi->queued_writes); | |
1169 | fuse_flush_writepages(inode); | |
1170 | spin_unlock(&fc->lock); | |
1171 | ||
1172 | return 0; | |
1173 | ||
1174 | err_free: | |
1175 | fuse_request_free(req); | |
1176 | err: | |
1177 | end_page_writeback(page); | |
1178 | return -ENOMEM; | |
1179 | } | |
1180 | ||
1181 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1182 | { | |
1183 | int err; | |
1184 | ||
1185 | err = fuse_writepage_locked(page); | |
1186 | unlock_page(page); | |
1187 | ||
1188 | return err; | |
1189 | } | |
1190 | ||
1191 | static int fuse_launder_page(struct page *page) | |
1192 | { | |
1193 | int err = 0; | |
1194 | if (clear_page_dirty_for_io(page)) { | |
1195 | struct inode *inode = page->mapping->host; | |
1196 | err = fuse_writepage_locked(page); | |
1197 | if (!err) | |
1198 | fuse_wait_on_page_writeback(inode, page->index); | |
1199 | } | |
1200 | return err; | |
1201 | } | |
1202 | ||
1203 | /* | |
1204 | * Write back dirty pages now, because there may not be any suitable | |
1205 | * open files later | |
1206 | */ | |
1207 | static void fuse_vma_close(struct vm_area_struct *vma) | |
1208 | { | |
1209 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
1210 | } | |
1211 | ||
1212 | /* | |
1213 | * Wait for writeback against this page to complete before allowing it | |
1214 | * to be marked dirty again, and hence written back again, possibly | |
1215 | * before the previous writepage completed. | |
1216 | * | |
1217 | * Block here, instead of in ->writepage(), so that the userspace fs | |
1218 | * can only block processes actually operating on the filesystem. | |
1219 | * | |
1220 | * Otherwise unprivileged userspace fs would be able to block | |
1221 | * unrelated: | |
1222 | * | |
1223 | * - page migration | |
1224 | * - sync(2) | |
1225 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
1226 | */ | |
1227 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page) | |
1228 | { | |
1229 | /* | |
1230 | * Don't use page->mapping as it may become NULL from a | |
1231 | * concurrent truncate. | |
1232 | */ | |
1233 | struct inode *inode = vma->vm_file->f_mapping->host; | |
1234 | ||
1235 | fuse_wait_on_page_writeback(inode, page->index); | |
1236 | return 0; | |
1237 | } | |
1238 | ||
1239 | static struct vm_operations_struct fuse_file_vm_ops = { | |
1240 | .close = fuse_vma_close, | |
1241 | .fault = filemap_fault, | |
1242 | .page_mkwrite = fuse_page_mkwrite, | |
1243 | }; | |
1244 | ||
1245 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
1246 | { | |
1247 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { | |
1248 | struct inode *inode = file->f_dentry->d_inode; | |
1249 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1250 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1251 | struct fuse_file *ff = file->private_data; | |
1252 | /* | |
1253 | * file may be written through mmap, so chain it onto the | |
1254 | * inodes's write_file list | |
1255 | */ | |
1256 | spin_lock(&fc->lock); | |
1257 | if (list_empty(&ff->write_entry)) | |
1258 | list_add(&ff->write_entry, &fi->write_files); | |
1259 | spin_unlock(&fc->lock); | |
1260 | } | |
1261 | file_accessed(file); | |
1262 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
1263 | return 0; |
1264 | } | |
1265 | ||
71421259 MS |
1266 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
1267 | struct file_lock *fl) | |
1268 | { | |
1269 | switch (ffl->type) { | |
1270 | case F_UNLCK: | |
1271 | break; | |
1272 | ||
1273 | case F_RDLCK: | |
1274 | case F_WRLCK: | |
1275 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
1276 | ffl->end < ffl->start) | |
1277 | return -EIO; | |
1278 | ||
1279 | fl->fl_start = ffl->start; | |
1280 | fl->fl_end = ffl->end; | |
1281 | fl->fl_pid = ffl->pid; | |
1282 | break; | |
1283 | ||
1284 | default: | |
1285 | return -EIO; | |
1286 | } | |
1287 | fl->fl_type = ffl->type; | |
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, | |
a9ff4f87 MS |
1292 | const struct file_lock *fl, int opcode, pid_t pid, |
1293 | int flock) | |
71421259 | 1294 | { |
7706a9d6 | 1295 | struct inode *inode = file->f_path.dentry->d_inode; |
9c8ef561 | 1296 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 MS |
1297 | struct fuse_file *ff = file->private_data; |
1298 | struct fuse_lk_in *arg = &req->misc.lk_in; | |
1299 | ||
1300 | arg->fh = ff->fh; | |
9c8ef561 | 1301 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
71421259 MS |
1302 | arg->lk.start = fl->fl_start; |
1303 | arg->lk.end = fl->fl_end; | |
1304 | arg->lk.type = fl->fl_type; | |
1305 | arg->lk.pid = pid; | |
a9ff4f87 MS |
1306 | if (flock) |
1307 | arg->lk_flags |= FUSE_LK_FLOCK; | |
71421259 MS |
1308 | req->in.h.opcode = opcode; |
1309 | req->in.h.nodeid = get_node_id(inode); | |
1310 | req->in.numargs = 1; | |
1311 | req->in.args[0].size = sizeof(*arg); | |
1312 | req->in.args[0].value = arg; | |
1313 | } | |
1314 | ||
1315 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
1316 | { | |
7706a9d6 | 1317 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1318 | struct fuse_conn *fc = get_fuse_conn(inode); |
1319 | struct fuse_req *req; | |
1320 | struct fuse_lk_out outarg; | |
1321 | int err; | |
1322 | ||
1323 | req = fuse_get_req(fc); | |
1324 | if (IS_ERR(req)) | |
1325 | return PTR_ERR(req); | |
1326 | ||
a9ff4f87 | 1327 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
71421259 MS |
1328 | req->out.numargs = 1; |
1329 | req->out.args[0].size = sizeof(outarg); | |
1330 | req->out.args[0].value = &outarg; | |
1331 | request_send(fc, req); | |
1332 | err = req->out.h.error; | |
1333 | fuse_put_request(fc, req); | |
1334 | if (!err) | |
1335 | err = convert_fuse_file_lock(&outarg.lk, fl); | |
1336 | ||
1337 | return err; | |
1338 | } | |
1339 | ||
a9ff4f87 | 1340 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 1341 | { |
7706a9d6 | 1342 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1343 | struct fuse_conn *fc = get_fuse_conn(inode); |
1344 | struct fuse_req *req; | |
1345 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; | |
1346 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; | |
1347 | int err; | |
1348 | ||
48e90761 MS |
1349 | if (fl->fl_lmops && fl->fl_lmops->fl_grant) { |
1350 | /* NLM needs asynchronous locks, which we don't support yet */ | |
1351 | return -ENOLCK; | |
1352 | } | |
1353 | ||
71421259 MS |
1354 | /* Unlock on close is handled by the flush method */ |
1355 | if (fl->fl_flags & FL_CLOSE) | |
1356 | return 0; | |
1357 | ||
1358 | req = fuse_get_req(fc); | |
1359 | if (IS_ERR(req)) | |
1360 | return PTR_ERR(req); | |
1361 | ||
a9ff4f87 | 1362 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
71421259 MS |
1363 | request_send(fc, req); |
1364 | err = req->out.h.error; | |
a4d27e75 MS |
1365 | /* locking is restartable */ |
1366 | if (err == -EINTR) | |
1367 | err = -ERESTARTSYS; | |
71421259 MS |
1368 | fuse_put_request(fc, req); |
1369 | return err; | |
1370 | } | |
1371 | ||
1372 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
1373 | { | |
7706a9d6 | 1374 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1375 | struct fuse_conn *fc = get_fuse_conn(inode); |
1376 | int err; | |
1377 | ||
48e90761 MS |
1378 | if (cmd == F_CANCELLK) { |
1379 | err = 0; | |
1380 | } else if (cmd == F_GETLK) { | |
71421259 | 1381 | if (fc->no_lock) { |
9d6a8c5c | 1382 | posix_test_lock(file, fl); |
71421259 MS |
1383 | err = 0; |
1384 | } else | |
1385 | err = fuse_getlk(file, fl); | |
1386 | } else { | |
1387 | if (fc->no_lock) | |
48e90761 | 1388 | err = posix_lock_file(file, fl, NULL); |
71421259 | 1389 | else |
a9ff4f87 | 1390 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
1391 | } |
1392 | return err; | |
1393 | } | |
1394 | ||
a9ff4f87 MS |
1395 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
1396 | { | |
1397 | struct inode *inode = file->f_path.dentry->d_inode; | |
1398 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1399 | int err; | |
1400 | ||
1401 | if (fc->no_lock) { | |
1402 | err = flock_lock_file_wait(file, fl); | |
1403 | } else { | |
1404 | /* emulate flock with POSIX locks */ | |
1405 | fl->fl_owner = (fl_owner_t) file; | |
1406 | err = fuse_setlk(file, fl, 1); | |
1407 | } | |
1408 | ||
1409 | return err; | |
1410 | } | |
1411 | ||
b2d2272f MS |
1412 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
1413 | { | |
1414 | struct inode *inode = mapping->host; | |
1415 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1416 | struct fuse_req *req; | |
1417 | struct fuse_bmap_in inarg; | |
1418 | struct fuse_bmap_out outarg; | |
1419 | int err; | |
1420 | ||
1421 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
1422 | return 0; | |
1423 | ||
1424 | req = fuse_get_req(fc); | |
1425 | if (IS_ERR(req)) | |
1426 | return 0; | |
1427 | ||
1428 | memset(&inarg, 0, sizeof(inarg)); | |
1429 | inarg.block = block; | |
1430 | inarg.blocksize = inode->i_sb->s_blocksize; | |
1431 | req->in.h.opcode = FUSE_BMAP; | |
1432 | req->in.h.nodeid = get_node_id(inode); | |
1433 | req->in.numargs = 1; | |
1434 | req->in.args[0].size = sizeof(inarg); | |
1435 | req->in.args[0].value = &inarg; | |
1436 | req->out.numargs = 1; | |
1437 | req->out.args[0].size = sizeof(outarg); | |
1438 | req->out.args[0].value = &outarg; | |
1439 | request_send(fc, req); | |
1440 | err = req->out.h.error; | |
1441 | fuse_put_request(fc, req); | |
1442 | if (err == -ENOSYS) | |
1443 | fc->no_bmap = 1; | |
1444 | ||
1445 | return err ? 0 : outarg.block; | |
1446 | } | |
1447 | ||
5559b8f4 MS |
1448 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin) |
1449 | { | |
1450 | loff_t retval; | |
1451 | struct inode *inode = file->f_path.dentry->d_inode; | |
1452 | ||
1453 | mutex_lock(&inode->i_mutex); | |
1454 | switch (origin) { | |
1455 | case SEEK_END: | |
769415c6 MS |
1456 | retval = fuse_update_attributes(inode, NULL, file, NULL); |
1457 | if (retval) | |
1458 | return retval; | |
5559b8f4 MS |
1459 | offset += i_size_read(inode); |
1460 | break; | |
1461 | case SEEK_CUR: | |
1462 | offset += file->f_pos; | |
1463 | } | |
1464 | retval = -EINVAL; | |
1465 | if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) { | |
1466 | if (offset != file->f_pos) { | |
1467 | file->f_pos = offset; | |
1468 | file->f_version = 0; | |
1469 | } | |
1470 | retval = offset; | |
1471 | } | |
1472 | mutex_unlock(&inode->i_mutex); | |
1473 | return retval; | |
1474 | } | |
1475 | ||
59efec7b TH |
1476 | static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov, |
1477 | unsigned int nr_segs, size_t bytes, bool to_user) | |
1478 | { | |
1479 | struct iov_iter ii; | |
1480 | int page_idx = 0; | |
1481 | ||
1482 | if (!bytes) | |
1483 | return 0; | |
1484 | ||
1485 | iov_iter_init(&ii, iov, nr_segs, bytes, 0); | |
1486 | ||
1487 | while (iov_iter_count(&ii)) { | |
1488 | struct page *page = pages[page_idx++]; | |
1489 | size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii)); | |
1490 | void *kaddr, *map; | |
1491 | ||
1492 | kaddr = map = kmap(page); | |
1493 | ||
1494 | while (todo) { | |
1495 | char __user *uaddr = ii.iov->iov_base + ii.iov_offset; | |
1496 | size_t iov_len = ii.iov->iov_len - ii.iov_offset; | |
1497 | size_t copy = min(todo, iov_len); | |
1498 | size_t left; | |
1499 | ||
1500 | if (!to_user) | |
1501 | left = copy_from_user(kaddr, uaddr, copy); | |
1502 | else | |
1503 | left = copy_to_user(uaddr, kaddr, copy); | |
1504 | ||
1505 | if (unlikely(left)) | |
1506 | return -EFAULT; | |
1507 | ||
1508 | iov_iter_advance(&ii, copy); | |
1509 | todo -= copy; | |
1510 | kaddr += copy; | |
1511 | } | |
1512 | ||
1513 | kunmap(map); | |
1514 | } | |
1515 | ||
1516 | return 0; | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * For ioctls, there is no generic way to determine how much memory | |
1521 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
1522 | * to dereference the passed pointer, so the parameter requires deep | |
1523 | * copying but FUSE has no idea whatsoever about what to copy in or | |
1524 | * out. | |
1525 | * | |
1526 | * This is solved by allowing FUSE server to retry ioctl with | |
1527 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
1528 | * needs to read in the following structure. | |
1529 | * | |
1530 | * struct a { | |
1531 | * char *buf; | |
1532 | * size_t buflen; | |
1533 | * } | |
1534 | * | |
1535 | * On the first callout to FUSE server, inarg->in_size and | |
1536 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
1537 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
1538 | * the actual iov array to | |
1539 | * | |
1540 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
1541 | * | |
1542 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
1543 | * On the second round, the server has access to the structure and | |
1544 | * from that it can tell what to look for next, so on the invocation, | |
1545 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
1546 | * | |
1547 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
1548 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
1549 | * | |
1550 | * FUSE will copy both struct a and the pointed buffer from the | |
1551 | * process doing the ioctl and retry ioctl with both struct a and the | |
1552 | * buffer. | |
1553 | * | |
1554 | * This time, FUSE server has everything it needs and completes ioctl | |
1555 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
1556 | * | |
1557 | * Copying data out works the same way. | |
1558 | * | |
1559 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
1560 | * automatically initializes in and out iovs by decoding @cmd with | |
1561 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
1562 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
1563 | * behavior for all FUSE servers. | |
1564 | */ | |
1565 | static long fuse_file_do_ioctl(struct file *file, unsigned int cmd, | |
1566 | unsigned long arg, unsigned int flags) | |
1567 | { | |
1568 | struct inode *inode = file->f_dentry->d_inode; | |
1569 | struct fuse_file *ff = file->private_data; | |
1570 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1571 | struct fuse_ioctl_in inarg = { | |
1572 | .fh = ff->fh, | |
1573 | .cmd = cmd, | |
1574 | .arg = arg, | |
1575 | .flags = flags | |
1576 | }; | |
1577 | struct fuse_ioctl_out outarg; | |
1578 | struct fuse_req *req = NULL; | |
1579 | struct page **pages = NULL; | |
1580 | struct page *iov_page = NULL; | |
1581 | struct iovec *in_iov = NULL, *out_iov = NULL; | |
1582 | unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages; | |
1583 | size_t in_size, out_size, transferred; | |
1584 | int err; | |
1585 | ||
1586 | /* assume all the iovs returned by client always fits in a page */ | |
1587 | BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); | |
1588 | ||
1589 | if (!fuse_allow_task(fc, current)) | |
1590 | return -EACCES; | |
1591 | ||
1592 | err = -EIO; | |
1593 | if (is_bad_inode(inode)) | |
1594 | goto out; | |
1595 | ||
1596 | err = -ENOMEM; | |
1597 | pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL); | |
1598 | iov_page = alloc_page(GFP_KERNEL); | |
1599 | if (!pages || !iov_page) | |
1600 | goto out; | |
1601 | ||
1602 | /* | |
1603 | * If restricted, initialize IO parameters as encoded in @cmd. | |
1604 | * RETRY from server is not allowed. | |
1605 | */ | |
1606 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
1607 | struct iovec *iov = page_address(iov_page); | |
1608 | ||
1609 | iov->iov_base = (void *)arg; | |
1610 | iov->iov_len = _IOC_SIZE(cmd); | |
1611 | ||
1612 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
1613 | in_iov = iov; | |
1614 | in_iovs = 1; | |
1615 | } | |
1616 | ||
1617 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
1618 | out_iov = iov; | |
1619 | out_iovs = 1; | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | retry: | |
1624 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
1625 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
1626 | ||
1627 | /* | |
1628 | * Out data can be used either for actual out data or iovs, | |
1629 | * make sure there always is at least one page. | |
1630 | */ | |
1631 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
1632 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
1633 | ||
1634 | /* make sure there are enough buffer pages and init request with them */ | |
1635 | err = -ENOMEM; | |
1636 | if (max_pages > FUSE_MAX_PAGES_PER_REQ) | |
1637 | goto out; | |
1638 | while (num_pages < max_pages) { | |
1639 | pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
1640 | if (!pages[num_pages]) | |
1641 | goto out; | |
1642 | num_pages++; | |
1643 | } | |
1644 | ||
1645 | req = fuse_get_req(fc); | |
1646 | if (IS_ERR(req)) { | |
1647 | err = PTR_ERR(req); | |
1648 | req = NULL; | |
1649 | goto out; | |
1650 | } | |
1651 | memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages); | |
1652 | req->num_pages = num_pages; | |
1653 | ||
1654 | /* okay, let's send it to the client */ | |
1655 | req->in.h.opcode = FUSE_IOCTL; | |
1656 | req->in.h.nodeid = get_node_id(inode); | |
1657 | req->in.numargs = 1; | |
1658 | req->in.args[0].size = sizeof(inarg); | |
1659 | req->in.args[0].value = &inarg; | |
1660 | if (in_size) { | |
1661 | req->in.numargs++; | |
1662 | req->in.args[1].size = in_size; | |
1663 | req->in.argpages = 1; | |
1664 | ||
1665 | err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size, | |
1666 | false); | |
1667 | if (err) | |
1668 | goto out; | |
1669 | } | |
1670 | ||
1671 | req->out.numargs = 2; | |
1672 | req->out.args[0].size = sizeof(outarg); | |
1673 | req->out.args[0].value = &outarg; | |
1674 | req->out.args[1].size = out_size; | |
1675 | req->out.argpages = 1; | |
1676 | req->out.argvar = 1; | |
1677 | ||
1678 | request_send(fc, req); | |
1679 | err = req->out.h.error; | |
1680 | transferred = req->out.args[1].size; | |
1681 | fuse_put_request(fc, req); | |
1682 | req = NULL; | |
1683 | if (err) | |
1684 | goto out; | |
1685 | ||
1686 | /* did it ask for retry? */ | |
1687 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
1688 | char *vaddr; | |
1689 | ||
1690 | /* no retry if in restricted mode */ | |
1691 | err = -EIO; | |
1692 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
1693 | goto out; | |
1694 | ||
1695 | in_iovs = outarg.in_iovs; | |
1696 | out_iovs = outarg.out_iovs; | |
1697 | ||
1698 | /* | |
1699 | * Make sure things are in boundary, separate checks | |
1700 | * are to protect against overflow. | |
1701 | */ | |
1702 | err = -ENOMEM; | |
1703 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
1704 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
1705 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
1706 | goto out; | |
1707 | ||
1708 | err = -EIO; | |
1709 | if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred) | |
1710 | goto out; | |
1711 | ||
1712 | /* okay, copy in iovs and retry */ | |
1713 | vaddr = kmap_atomic(pages[0], KM_USER0); | |
1714 | memcpy(page_address(iov_page), vaddr, transferred); | |
1715 | kunmap_atomic(vaddr, KM_USER0); | |
1716 | ||
1717 | in_iov = page_address(iov_page); | |
1718 | out_iov = in_iov + in_iovs; | |
1719 | ||
1720 | goto retry; | |
1721 | } | |
1722 | ||
1723 | err = -EIO; | |
1724 | if (transferred > inarg.out_size) | |
1725 | goto out; | |
1726 | ||
1727 | err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true); | |
1728 | out: | |
1729 | if (req) | |
1730 | fuse_put_request(fc, req); | |
1731 | if (iov_page) | |
1732 | __free_page(iov_page); | |
1733 | while (num_pages) | |
1734 | __free_page(pages[--num_pages]); | |
1735 | kfree(pages); | |
1736 | ||
1737 | return err ? err : outarg.result; | |
1738 | } | |
1739 | ||
1740 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, | |
1741 | unsigned long arg) | |
1742 | { | |
1743 | return fuse_file_do_ioctl(file, cmd, arg, 0); | |
1744 | } | |
1745 | ||
1746 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
1747 | unsigned long arg) | |
1748 | { | |
1749 | return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT); | |
1750 | } | |
1751 | ||
4b6f5d20 | 1752 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 1753 | .llseek = fuse_file_llseek, |
543ade1f | 1754 | .read = do_sync_read, |
bcb4be80 | 1755 | .aio_read = fuse_file_aio_read, |
543ade1f | 1756 | .write = do_sync_write, |
ea9b9907 | 1757 | .aio_write = fuse_file_aio_write, |
b6aeaded MS |
1758 | .mmap = fuse_file_mmap, |
1759 | .open = fuse_open, | |
1760 | .flush = fuse_flush, | |
1761 | .release = fuse_release, | |
1762 | .fsync = fuse_fsync, | |
71421259 | 1763 | .lock = fuse_file_lock, |
a9ff4f87 | 1764 | .flock = fuse_file_flock, |
5ffc4ef4 | 1765 | .splice_read = generic_file_splice_read, |
59efec7b TH |
1766 | .unlocked_ioctl = fuse_file_ioctl, |
1767 | .compat_ioctl = fuse_file_compat_ioctl, | |
b6aeaded MS |
1768 | }; |
1769 | ||
4b6f5d20 | 1770 | static const struct file_operations fuse_direct_io_file_operations = { |
5559b8f4 | 1771 | .llseek = fuse_file_llseek, |
413ef8cb MS |
1772 | .read = fuse_direct_read, |
1773 | .write = fuse_direct_write, | |
1774 | .open = fuse_open, | |
1775 | .flush = fuse_flush, | |
1776 | .release = fuse_release, | |
1777 | .fsync = fuse_fsync, | |
71421259 | 1778 | .lock = fuse_file_lock, |
a9ff4f87 | 1779 | .flock = fuse_file_flock, |
59efec7b TH |
1780 | .unlocked_ioctl = fuse_file_ioctl, |
1781 | .compat_ioctl = fuse_file_compat_ioctl, | |
5ffc4ef4 | 1782 | /* no mmap and splice_read */ |
413ef8cb MS |
1783 | }; |
1784 | ||
f5e54d6e | 1785 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 1786 | .readpage = fuse_readpage, |
3be5a52b MS |
1787 | .writepage = fuse_writepage, |
1788 | .launder_page = fuse_launder_page, | |
5e6f58a1 NP |
1789 | .write_begin = fuse_write_begin, |
1790 | .write_end = fuse_write_end, | |
db50b96c | 1791 | .readpages = fuse_readpages, |
3be5a52b | 1792 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 1793 | .bmap = fuse_bmap, |
b6aeaded MS |
1794 | }; |
1795 | ||
1796 | void fuse_init_file_inode(struct inode *inode) | |
1797 | { | |
45323fb7 MS |
1798 | inode->i_fop = &fuse_file_operations; |
1799 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 1800 | } |