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