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