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