Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 56cf34f | 2006-04-11 21:16:51 +0200 | [diff] [blame] | 3 | Copyright (C) 2001-2006 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; |
| 42 | request_send(fc, req); |
| 43 | err = req->out.h.error; |
| 44 | fuse_put_request(fc, req); |
| 45 | |
| 46 | return err; |
| 47 | } |
| 48 | |
| 49 | struct fuse_file *fuse_file_alloc(void) |
| 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); |
| 57 | ff = NULL; |
| 58 | } |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 59 | INIT_LIST_HEAD(&ff->write_entry); |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 60 | atomic_set(&ff->count, 0); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 61 | } |
| 62 | return ff; |
| 63 | } |
| 64 | |
| 65 | void fuse_file_free(struct fuse_file *ff) |
| 66 | { |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 67 | fuse_request_free(ff->reserved_req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 68 | kfree(ff); |
| 69 | } |
| 70 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 71 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) |
| 72 | { |
| 73 | atomic_inc(&ff->count); |
| 74 | return ff; |
| 75 | } |
| 76 | |
Miklos Szeredi | 819c4b3 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 77 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
| 78 | { |
| 79 | dput(req->dentry); |
| 80 | mntput(req->vfsmount); |
| 81 | fuse_put_request(fc, req); |
| 82 | } |
| 83 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 84 | static void fuse_file_put(struct fuse_file *ff) |
| 85 | { |
| 86 | if (atomic_dec_and_test(&ff->count)) { |
| 87 | struct fuse_req *req = ff->reserved_req; |
| 88 | struct fuse_conn *fc = get_fuse_conn(req->dentry->d_inode); |
Miklos Szeredi | 819c4b3 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 89 | req->end = fuse_release_end; |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 90 | request_send_background(fc, req); |
| 91 | kfree(ff); |
| 92 | } |
| 93 | } |
| 94 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 95 | void fuse_finish_open(struct inode *inode, struct file *file, |
| 96 | struct fuse_file *ff, struct fuse_open_out *outarg) |
| 97 | { |
| 98 | if (outarg->open_flags & FOPEN_DIRECT_IO) |
| 99 | file->f_op = &fuse_direct_io_file_operations; |
| 100 | if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) |
Miklos Szeredi | b100997 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 101 | invalidate_inode_pages2(inode->i_mapping); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 102 | ff->fh = outarg->fh; |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 103 | file->private_data = fuse_file_get(ff); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) |
| 107 | { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 108 | struct fuse_open_out outarg; |
| 109 | struct fuse_file *ff; |
| 110 | int err; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 111 | |
Miklos Szeredi | dd190d0 | 2005-09-30 11:59:02 -0700 | [diff] [blame] | 112 | /* VFS checks this, but only _after_ ->open() */ |
| 113 | if (file->f_flags & O_DIRECT) |
| 114 | return -EINVAL; |
| 115 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 116 | err = generic_file_open(inode, file); |
| 117 | if (err) |
| 118 | return err; |
| 119 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 120 | ff = fuse_file_alloc(); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 121 | if (!ff) |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 122 | return -ENOMEM; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 123 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 124 | err = fuse_send_open(inode, file, isdir, &outarg); |
| 125 | if (err) |
| 126 | fuse_file_free(ff); |
| 127 | else { |
| 128 | if (isdir) |
| 129 | outarg.open_flags &= ~FOPEN_DIRECT_IO; |
| 130 | fuse_finish_open(inode, file, ff, &outarg); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 133 | return err; |
| 134 | } |
| 135 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 136 | 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] | 137 | { |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 138 | struct fuse_req *req = ff->reserved_req; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 139 | struct fuse_release_in *inarg = &req->misc.release_in; |
| 140 | |
| 141 | inarg->fh = ff->fh; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 142 | inarg->flags = flags; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 143 | req->in.h.opcode = opcode; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 144 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 145 | req->in.numargs = 1; |
| 146 | req->in.args[0].size = sizeof(struct fuse_release_in); |
| 147 | req->in.args[0].value = inarg; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) |
| 151 | { |
| 152 | struct fuse_file *ff = file->private_data; |
| 153 | if (ff) { |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 154 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 155 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 156 | fuse_release_fill(ff, get_node_id(inode), file->f_flags, |
| 157 | isdir ? FUSE_RELEASEDIR : FUSE_RELEASE); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 158 | |
| 159 | /* Hold vfsmount and dentry until release is finished */ |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 160 | ff->reserved_req->vfsmount = mntget(file->f_path.mnt); |
| 161 | ff->reserved_req->dentry = dget(file->f_path.dentry); |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 162 | |
| 163 | spin_lock(&fc->lock); |
| 164 | list_del(&ff->write_entry); |
| 165 | spin_unlock(&fc->lock); |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 166 | /* |
| 167 | * Normally this will send the RELEASE request, |
| 168 | * however if some asynchronous READ or WRITE requests |
| 169 | * are outstanding, the sending will be delayed |
| 170 | */ |
| 171 | fuse_file_put(ff); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 172 | } |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 173 | |
| 174 | /* Return value is ignored by VFS */ |
| 175 | return 0; |
| 176 | } |
| 177 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 178 | static int fuse_open(struct inode *inode, struct file *file) |
| 179 | { |
| 180 | return fuse_open_common(inode, file, 0); |
| 181 | } |
| 182 | |
| 183 | static int fuse_release(struct inode *inode, struct file *file) |
| 184 | { |
| 185 | return fuse_release_common(inode, file, 0); |
| 186 | } |
| 187 | |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 188 | /* |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 189 | * Scramble the ID space with XTEA, so that the value of the files_struct |
| 190 | * pointer is not exposed to userspace. |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 191 | */ |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 192 | 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] | 193 | { |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 194 | u32 *k = fc->scramble_key; |
| 195 | u64 v = (unsigned long) id; |
| 196 | u32 v0 = v; |
| 197 | u32 v1 = v >> 32; |
| 198 | u32 sum = 0; |
| 199 | int i; |
| 200 | |
| 201 | for (i = 0; i < 32; i++) { |
| 202 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); |
| 203 | sum += 0x9E3779B9; |
| 204 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); |
| 205 | } |
| 206 | |
| 207 | return (u64) v0 + ((u64) v1 << 32); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 210 | static int fuse_flush(struct file *file, fl_owner_t id) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 211 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 212 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 213 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 214 | struct fuse_file *ff = file->private_data; |
| 215 | struct fuse_req *req; |
| 216 | struct fuse_flush_in inarg; |
| 217 | int err; |
| 218 | |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 219 | if (is_bad_inode(inode)) |
| 220 | return -EIO; |
| 221 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 222 | if (fc->no_flush) |
| 223 | return 0; |
| 224 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 225 | req = fuse_get_req_nofail(fc, file); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 226 | memset(&inarg, 0, sizeof(inarg)); |
| 227 | inarg.fh = ff->fh; |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 228 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 229 | req->in.h.opcode = FUSE_FLUSH; |
| 230 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 231 | req->in.numargs = 1; |
| 232 | req->in.args[0].size = sizeof(inarg); |
| 233 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 234 | req->force = 1; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 235 | request_send(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 236 | err = req->out.h.error; |
| 237 | fuse_put_request(fc, req); |
| 238 | if (err == -ENOSYS) { |
| 239 | fc->no_flush = 1; |
| 240 | err = 0; |
| 241 | } |
| 242 | return err; |
| 243 | } |
| 244 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 245 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
| 246 | int isdir) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 247 | { |
| 248 | struct inode *inode = de->d_inode; |
| 249 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 250 | struct fuse_file *ff = file->private_data; |
| 251 | struct fuse_req *req; |
| 252 | struct fuse_fsync_in inarg; |
| 253 | int err; |
| 254 | |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 255 | if (is_bad_inode(inode)) |
| 256 | return -EIO; |
| 257 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 258 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 261 | req = fuse_get_req(fc); |
| 262 | if (IS_ERR(req)) |
| 263 | return PTR_ERR(req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 264 | |
| 265 | memset(&inarg, 0, sizeof(inarg)); |
| 266 | inarg.fh = ff->fh; |
| 267 | inarg.fsync_flags = datasync ? 1 : 0; |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 268 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 269 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 270 | req->in.numargs = 1; |
| 271 | req->in.args[0].size = sizeof(inarg); |
| 272 | req->in.args[0].value = &inarg; |
| 273 | request_send(fc, req); |
| 274 | err = req->out.h.error; |
| 275 | fuse_put_request(fc, req); |
| 276 | if (err == -ENOSYS) { |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 277 | if (isdir) |
| 278 | fc->no_fsyncdir = 1; |
| 279 | else |
| 280 | fc->no_fsync = 1; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 281 | err = 0; |
| 282 | } |
| 283 | return err; |
| 284 | } |
| 285 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 286 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
| 287 | { |
| 288 | return fuse_fsync_common(file, de, datasync, 0); |
| 289 | } |
| 290 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 291 | void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff, |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 292 | struct inode *inode, loff_t pos, size_t count, int opcode) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 293 | { |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 294 | struct fuse_read_in *inarg = &req->misc.read_in; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 295 | |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 296 | inarg->fh = ff->fh; |
| 297 | inarg->offset = pos; |
| 298 | inarg->size = count; |
| 299 | req->in.h.opcode = opcode; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 300 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 301 | req->in.numargs = 1; |
| 302 | req->in.args[0].size = sizeof(struct fuse_read_in); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 303 | req->in.args[0].value = inarg; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 304 | req->out.argpages = 1; |
| 305 | req->out.argvar = 1; |
| 306 | req->out.numargs = 1; |
| 307 | req->out.args[0].size = count; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 310 | 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^] | 311 | struct inode *inode, loff_t pos, size_t count, |
| 312 | fl_owner_t owner) |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 313 | { |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 314 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 315 | struct fuse_file *ff = file->private_data; |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 316 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 317 | fuse_read_fill(req, ff, inode, pos, count, FUSE_READ); |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 318 | if (owner != NULL) { |
| 319 | struct fuse_read_in *inarg = &req->misc.read_in; |
| 320 | |
| 321 | inarg->read_flags |= FUSE_READ_LOCKOWNER; |
| 322 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); |
| 323 | } |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 324 | request_send(fc, req); |
| 325 | return req->out.args[0].size; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 328 | static int fuse_readpage(struct file *file, struct page *page) |
| 329 | { |
| 330 | struct inode *inode = page->mapping->host; |
| 331 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 332 | struct fuse_req *req; |
| 333 | int err; |
| 334 | |
| 335 | err = -EIO; |
| 336 | if (is_bad_inode(inode)) |
| 337 | goto out; |
| 338 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 339 | req = fuse_get_req(fc); |
| 340 | err = PTR_ERR(req); |
| 341 | if (IS_ERR(req)) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 342 | goto out; |
| 343 | |
| 344 | req->out.page_zeroing = 1; |
| 345 | req->num_pages = 1; |
| 346 | req->pages[0] = page; |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 347 | fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE, |
| 348 | NULL); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 349 | err = req->out.h.error; |
| 350 | fuse_put_request(fc, req); |
| 351 | if (!err) |
| 352 | SetPageUptodate(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 353 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 354 | out: |
| 355 | unlock_page(page); |
| 356 | return err; |
| 357 | } |
| 358 | |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 359 | 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] | 360 | { |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 361 | int i; |
| 362 | |
| 363 | fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */ |
| 364 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 365 | for (i = 0; i < req->num_pages; i++) { |
| 366 | struct page *page = req->pages[i]; |
| 367 | if (!req->out.h.error) |
| 368 | SetPageUptodate(page); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 369 | else |
| 370 | SetPageError(page); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 371 | unlock_page(page); |
| 372 | } |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 373 | if (req->ff) |
| 374 | fuse_file_put(req->ff); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 375 | fuse_put_request(fc, req); |
| 376 | } |
| 377 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 378 | static void fuse_send_readpages(struct fuse_req *req, struct fuse_file *ff, |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 379 | struct inode *inode) |
| 380 | { |
| 381 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 382 | loff_t pos = page_offset(req->pages[0]); |
| 383 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; |
| 384 | req->out.page_zeroing = 1; |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 385 | fuse_read_fill(req, ff, inode, pos, count, FUSE_READ); |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 386 | if (fc->async_read) { |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 387 | req->ff = fuse_file_get(ff); |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 388 | req->end = fuse_readpages_end; |
| 389 | request_send_background(fc, req); |
| 390 | } else { |
| 391 | request_send(fc, req); |
| 392 | fuse_readpages_end(fc, req); |
| 393 | } |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 396 | struct fuse_fill_data { |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 397 | struct fuse_req *req; |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 398 | struct fuse_file *ff; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 399 | struct inode *inode; |
| 400 | }; |
| 401 | |
| 402 | static int fuse_readpages_fill(void *_data, struct page *page) |
| 403 | { |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 404 | struct fuse_fill_data *data = _data; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 405 | struct fuse_req *req = data->req; |
| 406 | struct inode *inode = data->inode; |
| 407 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 408 | |
| 409 | if (req->num_pages && |
| 410 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || |
| 411 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || |
| 412 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 413 | fuse_send_readpages(req, data->ff, inode); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 414 | data->req = req = fuse_get_req(fc); |
| 415 | if (IS_ERR(req)) { |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 416 | unlock_page(page); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 417 | return PTR_ERR(req); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 418 | } |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 419 | } |
| 420 | req->pages[req->num_pages] = page; |
| 421 | req->num_pages ++; |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static int fuse_readpages(struct file *file, struct address_space *mapping, |
| 426 | struct list_head *pages, unsigned nr_pages) |
| 427 | { |
| 428 | struct inode *inode = mapping->host; |
| 429 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 430 | struct fuse_fill_data data; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 431 | int err; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 432 | |
Alexander Zarochentsev | 1d7ea73 | 2006-08-13 23:24:27 -0700 | [diff] [blame] | 433 | err = -EIO; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 434 | if (is_bad_inode(inode)) |
OGAWA Hirofumi | 2e99002 | 2006-11-02 22:07:09 -0800 | [diff] [blame] | 435 | goto out; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 436 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 437 | data.ff = file->private_data; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 438 | data.inode = inode; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 439 | data.req = fuse_get_req(fc); |
Alexander Zarochentsev | 1d7ea73 | 2006-08-13 23:24:27 -0700 | [diff] [blame] | 440 | err = PTR_ERR(data.req); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 441 | if (IS_ERR(data.req)) |
OGAWA Hirofumi | 2e99002 | 2006-11-02 22:07:09 -0800 | [diff] [blame] | 442 | goto out; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 443 | |
| 444 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); |
Miklos Szeredi | d3406ff | 2006-04-10 22:54:49 -0700 | [diff] [blame] | 445 | if (!err) { |
| 446 | if (data.req->num_pages) |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 447 | fuse_send_readpages(data.req, data.ff, inode); |
Miklos Szeredi | d3406ff | 2006-04-10 22:54:49 -0700 | [diff] [blame] | 448 | else |
| 449 | fuse_put_request(fc, data.req); |
| 450 | } |
OGAWA Hirofumi | 2e99002 | 2006-11-02 22:07:09 -0800 | [diff] [blame] | 451 | out: |
Alexander Zarochentsev | 1d7ea73 | 2006-08-13 23:24:27 -0700 | [diff] [blame] | 452 | return err; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 455 | static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff, |
| 456 | struct inode *inode, loff_t pos, size_t count, |
| 457 | int writepage) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 458 | { |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 459 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 460 | struct fuse_write_in *inarg = &req->misc.write.in; |
| 461 | struct fuse_write_out *outarg = &req->misc.write.out; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 462 | |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 463 | memset(inarg, 0, sizeof(struct fuse_write_in)); |
| 464 | inarg->fh = ff->fh; |
| 465 | inarg->offset = pos; |
| 466 | inarg->size = count; |
| 467 | inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 468 | req->in.h.opcode = FUSE_WRITE; |
| 469 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 470 | req->in.argpages = 1; |
| 471 | req->in.numargs = 2; |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 472 | if (fc->minor < 9) |
| 473 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
| 474 | else |
| 475 | req->in.args[0].size = sizeof(struct fuse_write_in); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 476 | req->in.args[0].value = inarg; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 477 | req->in.args[1].size = count; |
| 478 | req->out.numargs = 1; |
| 479 | req->out.args[0].size = sizeof(struct fuse_write_out); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 480 | req->out.args[0].value = outarg; |
| 481 | } |
| 482 | |
| 483 | 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^] | 484 | struct inode *inode, loff_t pos, size_t count, |
| 485 | fl_owner_t owner) |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 486 | { |
| 487 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 488 | fuse_write_fill(req, file->private_data, inode, pos, count, 0); |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 489 | if (owner != NULL) { |
| 490 | struct fuse_write_in *inarg = &req->misc.write.in; |
| 491 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
| 492 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); |
| 493 | } |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 494 | request_send(fc, req); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 495 | return req->misc.write.out.size; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 498 | static int fuse_write_begin(struct file *file, struct address_space *mapping, |
| 499 | loff_t pos, unsigned len, unsigned flags, |
| 500 | struct page **pagep, void **fsdata) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 501 | { |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 502 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
| 503 | |
| 504 | *pagep = __grab_cache_page(mapping, index); |
| 505 | if (!*pagep) |
| 506 | return -ENOMEM; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 510 | static int fuse_buffered_write(struct file *file, struct inode *inode, |
| 511 | loff_t pos, unsigned count, struct page *page) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 512 | { |
| 513 | int err; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 514 | size_t nres; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 515 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 516 | struct fuse_inode *fi = get_fuse_inode(inode); |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 517 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 518 | struct fuse_req *req; |
| 519 | |
| 520 | if (is_bad_inode(inode)) |
| 521 | return -EIO; |
| 522 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 523 | req = fuse_get_req(fc); |
| 524 | if (IS_ERR(req)) |
| 525 | return PTR_ERR(req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 526 | |
| 527 | req->num_pages = 1; |
| 528 | req->pages[0] = page; |
| 529 | req->page_offset = offset; |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 530 | nres = fuse_send_write(req, file, inode, pos, count, NULL); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 531 | err = req->out.h.error; |
| 532 | fuse_put_request(fc, req); |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 533 | if (!err && !nres) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 534 | err = -EIO; |
| 535 | if (!err) { |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 536 | pos += nres; |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 537 | spin_lock(&fc->lock); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 538 | fi->attr_version = ++fc->attr_version; |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 539 | if (pos > inode->i_size) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 540 | i_size_write(inode, pos); |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 541 | spin_unlock(&fc->lock); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 542 | |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 543 | if (count == PAGE_CACHE_SIZE) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 544 | SetPageUptodate(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 545 | } |
| 546 | fuse_invalidate_attr(inode); |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 547 | return err ? err : nres; |
| 548 | } |
| 549 | |
| 550 | static int fuse_write_end(struct file *file, struct address_space *mapping, |
| 551 | loff_t pos, unsigned len, unsigned copied, |
| 552 | struct page *page, void *fsdata) |
| 553 | { |
| 554 | struct inode *inode = mapping->host; |
| 555 | int res = 0; |
| 556 | |
| 557 | if (copied) |
| 558 | res = fuse_buffered_write(file, inode, pos, copied, page); |
| 559 | |
| 560 | unlock_page(page); |
| 561 | page_cache_release(page); |
| 562 | return res; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 565 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
| 566 | { |
| 567 | unsigned i; |
| 568 | |
| 569 | for (i = 0; i < req->num_pages; i++) { |
| 570 | struct page *page = req->pages[i]; |
| 571 | if (write) |
| 572 | set_page_dirty_lock(page); |
| 573 | put_page(page); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, |
| 578 | unsigned nbytes, int write) |
| 579 | { |
| 580 | unsigned long user_addr = (unsigned long) buf; |
| 581 | unsigned offset = user_addr & ~PAGE_MASK; |
| 582 | int npages; |
| 583 | |
| 584 | /* This doesn't work with nfsd */ |
| 585 | if (!current->mm) |
| 586 | return -EPERM; |
| 587 | |
| 588 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); |
| 589 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Miklos Szeredi | 6ad84ac | 2006-01-06 00:19:42 -0800 | [diff] [blame] | 590 | npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 591 | down_read(¤t->mm->mmap_sem); |
| 592 | npages = get_user_pages(current, current->mm, user_addr, npages, write, |
| 593 | 0, req->pages, NULL); |
| 594 | up_read(¤t->mm->mmap_sem); |
| 595 | if (npages < 0) |
| 596 | return npages; |
| 597 | |
| 598 | req->num_pages = npages; |
| 599 | req->page_offset = offset; |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, |
| 604 | size_t count, loff_t *ppos, int write) |
| 605 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 606 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 607 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 608 | size_t nmax = write ? fc->max_write : fc->max_read; |
| 609 | loff_t pos = *ppos; |
| 610 | ssize_t res = 0; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 611 | struct fuse_req *req; |
| 612 | |
| 613 | if (is_bad_inode(inode)) |
| 614 | return -EIO; |
| 615 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 616 | req = fuse_get_req(fc); |
| 617 | if (IS_ERR(req)) |
| 618 | return PTR_ERR(req); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 619 | |
| 620 | while (count) { |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 621 | size_t nres; |
| 622 | size_t nbytes = min(count, nmax); |
| 623 | int err = fuse_get_user_pages(req, buf, nbytes, !write); |
| 624 | if (err) { |
| 625 | res = err; |
| 626 | break; |
| 627 | } |
Miklos Szeredi | 6ad84ac | 2006-01-06 00:19:42 -0800 | [diff] [blame] | 628 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
| 629 | nbytes = min(count, nbytes); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 630 | if (write) |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 631 | nres = fuse_send_write(req, file, inode, pos, nbytes, |
| 632 | current->files); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 633 | else |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame^] | 634 | nres = fuse_send_read(req, file, inode, pos, nbytes, |
| 635 | current->files); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 636 | fuse_release_user_pages(req, !write); |
| 637 | if (req->out.h.error) { |
| 638 | if (!res) |
| 639 | res = req->out.h.error; |
| 640 | break; |
| 641 | } else if (nres > nbytes) { |
| 642 | res = -EIO; |
| 643 | break; |
| 644 | } |
| 645 | count -= nres; |
| 646 | res += nres; |
| 647 | pos += nres; |
| 648 | buf += nres; |
| 649 | if (nres != nbytes) |
| 650 | break; |
Miklos Szeredi | 56cf34f | 2006-04-11 21:16:51 +0200 | [diff] [blame] | 651 | if (count) { |
| 652 | fuse_put_request(fc, req); |
| 653 | req = fuse_get_req(fc); |
| 654 | if (IS_ERR(req)) |
| 655 | break; |
| 656 | } |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 657 | } |
| 658 | fuse_put_request(fc, req); |
| 659 | if (res > 0) { |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 660 | if (write) { |
| 661 | spin_lock(&fc->lock); |
| 662 | if (pos > inode->i_size) |
| 663 | i_size_write(inode, pos); |
| 664 | spin_unlock(&fc->lock); |
| 665 | } |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 666 | *ppos = pos; |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 667 | } |
| 668 | fuse_invalidate_attr(inode); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 669 | |
| 670 | return res; |
| 671 | } |
| 672 | |
| 673 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
| 674 | size_t count, loff_t *ppos) |
| 675 | { |
| 676 | return fuse_direct_io(file, buf, count, ppos, 0); |
| 677 | } |
| 678 | |
| 679 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
| 680 | size_t count, loff_t *ppos) |
| 681 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 682 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 683 | ssize_t res; |
| 684 | /* Don't allow parallel writes to the same file */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 685 | mutex_lock(&inode->i_mutex); |
Miklos Szeredi | 889f784 | 2007-05-23 13:57:54 -0700 | [diff] [blame] | 686 | res = generic_write_checks(file, ppos, &count, 0); |
| 687 | if (!res) |
| 688 | res = fuse_direct_io(file, buf, count, ppos, 1); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 689 | mutex_unlock(&inode->i_mutex); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 690 | return res; |
| 691 | } |
| 692 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 693 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 694 | { |
| 695 | if ((vma->vm_flags & VM_SHARED)) { |
| 696 | if ((vma->vm_flags & VM_WRITE)) |
| 697 | return -ENODEV; |
| 698 | else |
| 699 | vma->vm_flags &= ~VM_MAYWRITE; |
| 700 | } |
| 701 | return generic_file_mmap(file, vma); |
| 702 | } |
| 703 | |
| 704 | static int fuse_set_page_dirty(struct page *page) |
| 705 | { |
| 706 | printk("fuse_set_page_dirty: should not happen\n"); |
| 707 | dump_stack(); |
| 708 | return 0; |
| 709 | } |
| 710 | |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 711 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
| 712 | struct file_lock *fl) |
| 713 | { |
| 714 | switch (ffl->type) { |
| 715 | case F_UNLCK: |
| 716 | break; |
| 717 | |
| 718 | case F_RDLCK: |
| 719 | case F_WRLCK: |
| 720 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || |
| 721 | ffl->end < ffl->start) |
| 722 | return -EIO; |
| 723 | |
| 724 | fl->fl_start = ffl->start; |
| 725 | fl->fl_end = ffl->end; |
| 726 | fl->fl_pid = ffl->pid; |
| 727 | break; |
| 728 | |
| 729 | default: |
| 730 | return -EIO; |
| 731 | } |
| 732 | fl->fl_type = ffl->type; |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 737 | const struct file_lock *fl, int opcode, pid_t pid, |
| 738 | int flock) |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 739 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 740 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 741 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 742 | struct fuse_file *ff = file->private_data; |
| 743 | struct fuse_lk_in *arg = &req->misc.lk_in; |
| 744 | |
| 745 | arg->fh = ff->fh; |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 746 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 747 | arg->lk.start = fl->fl_start; |
| 748 | arg->lk.end = fl->fl_end; |
| 749 | arg->lk.type = fl->fl_type; |
| 750 | arg->lk.pid = pid; |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 751 | if (flock) |
| 752 | arg->lk_flags |= FUSE_LK_FLOCK; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 753 | req->in.h.opcode = opcode; |
| 754 | req->in.h.nodeid = get_node_id(inode); |
| 755 | req->in.numargs = 1; |
| 756 | req->in.args[0].size = sizeof(*arg); |
| 757 | req->in.args[0].value = arg; |
| 758 | } |
| 759 | |
| 760 | static int fuse_getlk(struct file *file, struct file_lock *fl) |
| 761 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 762 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 763 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 764 | struct fuse_req *req; |
| 765 | struct fuse_lk_out outarg; |
| 766 | int err; |
| 767 | |
| 768 | req = fuse_get_req(fc); |
| 769 | if (IS_ERR(req)) |
| 770 | return PTR_ERR(req); |
| 771 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 772 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 773 | req->out.numargs = 1; |
| 774 | req->out.args[0].size = sizeof(outarg); |
| 775 | req->out.args[0].value = &outarg; |
| 776 | request_send(fc, req); |
| 777 | err = req->out.h.error; |
| 778 | fuse_put_request(fc, req); |
| 779 | if (!err) |
| 780 | err = convert_fuse_file_lock(&outarg.lk, fl); |
| 781 | |
| 782 | return err; |
| 783 | } |
| 784 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 785 | 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] | 786 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 787 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 788 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 789 | struct fuse_req *req; |
| 790 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; |
| 791 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; |
| 792 | int err; |
| 793 | |
| 794 | /* Unlock on close is handled by the flush method */ |
| 795 | if (fl->fl_flags & FL_CLOSE) |
| 796 | return 0; |
| 797 | |
| 798 | req = fuse_get_req(fc); |
| 799 | if (IS_ERR(req)) |
| 800 | return PTR_ERR(req); |
| 801 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 802 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 803 | request_send(fc, req); |
| 804 | err = req->out.h.error; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 805 | /* locking is restartable */ |
| 806 | if (err == -EINTR) |
| 807 | err = -ERESTARTSYS; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 808 | fuse_put_request(fc, req); |
| 809 | return err; |
| 810 | } |
| 811 | |
| 812 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) |
| 813 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 814 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 815 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 816 | int err; |
| 817 | |
| 818 | if (cmd == F_GETLK) { |
| 819 | if (fc->no_lock) { |
Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 820 | posix_test_lock(file, fl); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 821 | err = 0; |
| 822 | } else |
| 823 | err = fuse_getlk(file, fl); |
| 824 | } else { |
| 825 | if (fc->no_lock) |
| 826 | err = posix_lock_file_wait(file, fl); |
| 827 | else |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 828 | err = fuse_setlk(file, fl, 0); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 829 | } |
| 830 | return err; |
| 831 | } |
| 832 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 833 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
| 834 | { |
| 835 | struct inode *inode = file->f_path.dentry->d_inode; |
| 836 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 837 | int err; |
| 838 | |
| 839 | if (fc->no_lock) { |
| 840 | err = flock_lock_file_wait(file, fl); |
| 841 | } else { |
| 842 | /* emulate flock with POSIX locks */ |
| 843 | fl->fl_owner = (fl_owner_t) file; |
| 844 | err = fuse_setlk(file, fl, 1); |
| 845 | } |
| 846 | |
| 847 | return err; |
| 848 | } |
| 849 | |
Miklos Szeredi | b2d2272 | 2006-12-06 20:35:51 -0800 | [diff] [blame] | 850 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
| 851 | { |
| 852 | struct inode *inode = mapping->host; |
| 853 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 854 | struct fuse_req *req; |
| 855 | struct fuse_bmap_in inarg; |
| 856 | struct fuse_bmap_out outarg; |
| 857 | int err; |
| 858 | |
| 859 | if (!inode->i_sb->s_bdev || fc->no_bmap) |
| 860 | return 0; |
| 861 | |
| 862 | req = fuse_get_req(fc); |
| 863 | if (IS_ERR(req)) |
| 864 | return 0; |
| 865 | |
| 866 | memset(&inarg, 0, sizeof(inarg)); |
| 867 | inarg.block = block; |
| 868 | inarg.blocksize = inode->i_sb->s_blocksize; |
| 869 | req->in.h.opcode = FUSE_BMAP; |
| 870 | req->in.h.nodeid = get_node_id(inode); |
| 871 | req->in.numargs = 1; |
| 872 | req->in.args[0].size = sizeof(inarg); |
| 873 | req->in.args[0].value = &inarg; |
| 874 | req->out.numargs = 1; |
| 875 | req->out.args[0].size = sizeof(outarg); |
| 876 | req->out.args[0].value = &outarg; |
| 877 | request_send(fc, req); |
| 878 | err = req->out.h.error; |
| 879 | fuse_put_request(fc, req); |
| 880 | if (err == -ENOSYS) |
| 881 | fc->no_bmap = 1; |
| 882 | |
| 883 | return err ? 0 : outarg.block; |
| 884 | } |
| 885 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 886 | static const struct file_operations fuse_file_operations = { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 887 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 888 | .read = do_sync_read, |
| 889 | .aio_read = generic_file_aio_read, |
| 890 | .write = do_sync_write, |
| 891 | .aio_write = generic_file_aio_write, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 892 | .mmap = fuse_file_mmap, |
| 893 | .open = fuse_open, |
| 894 | .flush = fuse_flush, |
| 895 | .release = fuse_release, |
| 896 | .fsync = fuse_fsync, |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 897 | .lock = fuse_file_lock, |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 898 | .flock = fuse_file_flock, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 899 | .splice_read = generic_file_splice_read, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 900 | }; |
| 901 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 902 | static const struct file_operations fuse_direct_io_file_operations = { |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 903 | .llseek = generic_file_llseek, |
| 904 | .read = fuse_direct_read, |
| 905 | .write = fuse_direct_write, |
| 906 | .open = fuse_open, |
| 907 | .flush = fuse_flush, |
| 908 | .release = fuse_release, |
| 909 | .fsync = fuse_fsync, |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 910 | .lock = fuse_file_lock, |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 911 | .flock = fuse_file_flock, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 912 | /* no mmap and splice_read */ |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 913 | }; |
| 914 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 915 | static const struct address_space_operations fuse_file_aops = { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 916 | .readpage = fuse_readpage, |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 917 | .write_begin = fuse_write_begin, |
| 918 | .write_end = fuse_write_end, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 919 | .readpages = fuse_readpages, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 920 | .set_page_dirty = fuse_set_page_dirty, |
Miklos Szeredi | b2d2272 | 2006-12-06 20:35:51 -0800 | [diff] [blame] | 921 | .bmap = fuse_bmap, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 922 | }; |
| 923 | |
| 924 | void fuse_init_file_inode(struct inode *inode) |
| 925 | { |
Miklos Szeredi | 45323fb | 2005-09-09 13:10:37 -0700 | [diff] [blame] | 926 | inode->i_fop = &fuse_file_operations; |
| 927 | inode->i_data.a_ops = &fuse_file_aops; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 928 | } |