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