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