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