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