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> |
| 14 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 15 | static const struct file_operations fuse_direct_io_file_operations; |
Miklos Szeredi | 45323fb | 2005-09-09 13:10:37 -0700 | [diff] [blame] | 16 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 17 | static int fuse_send_open(struct inode *inode, struct file *file, int isdir, |
| 18 | struct fuse_open_out *outargp) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 19 | { |
| 20 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 21 | struct fuse_open_in inarg; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 22 | struct fuse_req *req; |
| 23 | int err; |
| 24 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 25 | req = fuse_get_req(fc); |
| 26 | if (IS_ERR(req)) |
| 27 | return PTR_ERR(req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 28 | |
| 29 | memset(&inarg, 0, sizeof(inarg)); |
| 30 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 31 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
| 32 | req->in.h.nodeid = get_node_id(inode); |
| 33 | req->inode = inode; |
| 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) { |
| 52 | ff->release_req = fuse_request_alloc(); |
| 53 | if (!ff->release_req) { |
| 54 | kfree(ff); |
| 55 | ff = NULL; |
| 56 | } |
| 57 | } |
| 58 | return ff; |
| 59 | } |
| 60 | |
| 61 | void fuse_file_free(struct fuse_file *ff) |
| 62 | { |
| 63 | fuse_request_free(ff->release_req); |
| 64 | kfree(ff); |
| 65 | } |
| 66 | |
| 67 | void fuse_finish_open(struct inode *inode, struct file *file, |
| 68 | struct fuse_file *ff, struct fuse_open_out *outarg) |
| 69 | { |
| 70 | if (outarg->open_flags & FOPEN_DIRECT_IO) |
| 71 | file->f_op = &fuse_direct_io_file_operations; |
| 72 | if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) |
| 73 | invalidate_inode_pages(inode->i_mapping); |
| 74 | ff->fh = outarg->fh; |
| 75 | file->private_data = ff; |
| 76 | } |
| 77 | |
| 78 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) |
| 79 | { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 80 | struct fuse_open_out outarg; |
| 81 | struct fuse_file *ff; |
| 82 | int err; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 83 | |
Miklos Szeredi | dd190d0 | 2005-09-30 11:59:02 -0700 | [diff] [blame] | 84 | /* VFS checks this, but only _after_ ->open() */ |
| 85 | if (file->f_flags & O_DIRECT) |
| 86 | return -EINVAL; |
| 87 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 88 | err = generic_file_open(inode, file); |
| 89 | if (err) |
| 90 | return err; |
| 91 | |
| 92 | /* If opening the root node, no lookup has been performed on |
| 93 | it, so the attributes must be refreshed */ |
| 94 | if (get_node_id(inode) == FUSE_ROOT_ID) { |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 95 | err = fuse_do_getattr(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 96 | if (err) |
| 97 | return err; |
| 98 | } |
| 99 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 100 | ff = fuse_file_alloc(); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 101 | if (!ff) |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 102 | return -ENOMEM; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 103 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 104 | err = fuse_send_open(inode, file, isdir, &outarg); |
| 105 | if (err) |
| 106 | fuse_file_free(ff); |
| 107 | else { |
| 108 | if (isdir) |
| 109 | outarg.open_flags &= ~FOPEN_DIRECT_IO; |
| 110 | fuse_finish_open(inode, file, ff, &outarg); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 113 | return err; |
| 114 | } |
| 115 | |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 116 | /* Special case for failed iget in CREATE */ |
| 117 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
| 118 | { |
Miklos Szeredi | 77e7f25 | 2006-02-17 13:52:52 -0800 | [diff] [blame] | 119 | /* If called from end_io_requests(), req has more than one |
| 120 | reference and fuse_reset_request() cannot work */ |
| 121 | if (fc->connected) { |
| 122 | u64 nodeid = req->in.h.nodeid; |
| 123 | fuse_reset_request(req); |
| 124 | fuse_send_forget(fc, req, nodeid, 1); |
| 125 | } else |
| 126 | fuse_put_request(fc, req); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 129 | void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff, |
| 130 | u64 nodeid, struct inode *inode, int flags, int isdir) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 131 | { |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 132 | struct fuse_req * req = ff->release_req; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 133 | struct fuse_release_in *inarg = &req->misc.release_in; |
| 134 | |
| 135 | inarg->fh = ff->fh; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 136 | inarg->flags = flags; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 137 | req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 138 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 139 | req->inode = inode; |
| 140 | req->in.numargs = 1; |
| 141 | req->in.args[0].size = sizeof(struct fuse_release_in); |
| 142 | req->in.args[0].value = inarg; |
| 143 | request_send_background(fc, req); |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 144 | if (!inode) |
| 145 | req->end = fuse_release_end; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 146 | kfree(ff); |
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) { |
| 153 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 154 | u64 nodeid = get_node_id(inode); |
| 155 | fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir); |
| 156 | } |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 157 | |
| 158 | /* Return value is ignored by VFS */ |
| 159 | return 0; |
| 160 | } |
| 161 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 162 | static int fuse_open(struct inode *inode, struct file *file) |
| 163 | { |
| 164 | return fuse_open_common(inode, file, 0); |
| 165 | } |
| 166 | |
| 167 | static int fuse_release(struct inode *inode, struct file *file) |
| 168 | { |
| 169 | return fuse_release_common(inode, file, 0); |
| 170 | } |
| 171 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 172 | static int fuse_flush(struct file *file) |
| 173 | { |
| 174 | struct inode *inode = file->f_dentry->d_inode; |
| 175 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 176 | struct fuse_file *ff = file->private_data; |
| 177 | struct fuse_req *req; |
| 178 | struct fuse_flush_in inarg; |
| 179 | int err; |
| 180 | |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 181 | if (is_bad_inode(inode)) |
| 182 | return -EIO; |
| 183 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 184 | if (fc->no_flush) |
| 185 | return 0; |
| 186 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 187 | req = fuse_get_req(fc); |
| 188 | if (IS_ERR(req)) |
| 189 | return PTR_ERR(req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 190 | |
| 191 | memset(&inarg, 0, sizeof(inarg)); |
| 192 | inarg.fh = ff->fh; |
| 193 | req->in.h.opcode = FUSE_FLUSH; |
| 194 | req->in.h.nodeid = get_node_id(inode); |
| 195 | req->inode = inode; |
| 196 | req->file = file; |
| 197 | req->in.numargs = 1; |
| 198 | req->in.args[0].size = sizeof(inarg); |
| 199 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 200 | request_send(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 201 | err = req->out.h.error; |
| 202 | fuse_put_request(fc, req); |
| 203 | if (err == -ENOSYS) { |
| 204 | fc->no_flush = 1; |
| 205 | err = 0; |
| 206 | } |
| 207 | return err; |
| 208 | } |
| 209 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 210 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
| 211 | int isdir) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 212 | { |
| 213 | struct inode *inode = de->d_inode; |
| 214 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 215 | struct fuse_file *ff = file->private_data; |
| 216 | struct fuse_req *req; |
| 217 | struct fuse_fsync_in inarg; |
| 218 | int err; |
| 219 | |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 220 | if (is_bad_inode(inode)) |
| 221 | return -EIO; |
| 222 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 223 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 226 | req = fuse_get_req(fc); |
| 227 | if (IS_ERR(req)) |
| 228 | return PTR_ERR(req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 229 | |
| 230 | memset(&inarg, 0, sizeof(inarg)); |
| 231 | inarg.fh = ff->fh; |
| 232 | inarg.fsync_flags = datasync ? 1 : 0; |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 233 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 234 | req->in.h.nodeid = get_node_id(inode); |
| 235 | req->inode = inode; |
| 236 | req->file = file; |
| 237 | req->in.numargs = 1; |
| 238 | req->in.args[0].size = sizeof(inarg); |
| 239 | req->in.args[0].value = &inarg; |
| 240 | request_send(fc, req); |
| 241 | err = req->out.h.error; |
| 242 | fuse_put_request(fc, req); |
| 243 | if (err == -ENOSYS) { |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 244 | if (isdir) |
| 245 | fc->no_fsyncdir = 1; |
| 246 | else |
| 247 | fc->no_fsync = 1; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 248 | err = 0; |
| 249 | } |
| 250 | return err; |
| 251 | } |
| 252 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 253 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
| 254 | { |
| 255 | return fuse_fsync_common(file, de, datasync, 0); |
| 256 | } |
| 257 | |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 258 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
| 259 | struct inode *inode, loff_t pos, size_t count, int opcode) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 260 | { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 261 | struct fuse_file *ff = file->private_data; |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 262 | struct fuse_read_in *inarg = &req->misc.read_in; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 263 | |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 264 | inarg->fh = ff->fh; |
| 265 | inarg->offset = pos; |
| 266 | inarg->size = count; |
| 267 | req->in.h.opcode = opcode; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 268 | req->in.h.nodeid = get_node_id(inode); |
| 269 | req->inode = inode; |
| 270 | req->file = file; |
| 271 | req->in.numargs = 1; |
| 272 | req->in.args[0].size = sizeof(struct fuse_read_in); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 273 | req->in.args[0].value = inarg; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 274 | req->out.argpages = 1; |
| 275 | req->out.argvar = 1; |
| 276 | req->out.numargs = 1; |
| 277 | req->out.args[0].size = count; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 280 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
| 281 | struct inode *inode, loff_t pos, size_t count) |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 282 | { |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 283 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 284 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
| 285 | request_send(fc, req); |
| 286 | return req->out.args[0].size; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 289 | static int fuse_readpage(struct file *file, struct page *page) |
| 290 | { |
| 291 | struct inode *inode = page->mapping->host; |
| 292 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 293 | struct fuse_req *req; |
| 294 | int err; |
| 295 | |
| 296 | err = -EIO; |
| 297 | if (is_bad_inode(inode)) |
| 298 | goto out; |
| 299 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 300 | req = fuse_get_req(fc); |
| 301 | err = PTR_ERR(req); |
| 302 | if (IS_ERR(req)) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 303 | goto out; |
| 304 | |
| 305 | req->out.page_zeroing = 1; |
| 306 | req->num_pages = 1; |
| 307 | req->pages[0] = page; |
Miklos Szeredi | 4633a22 | 2006-01-06 00:19:36 -0800 | [diff] [blame] | 308 | fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 309 | err = req->out.h.error; |
| 310 | fuse_put_request(fc, req); |
| 311 | if (!err) |
| 312 | SetPageUptodate(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 313 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 314 | out: |
| 315 | unlock_page(page); |
| 316 | return err; |
| 317 | } |
| 318 | |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 319 | 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] | 320 | { |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 321 | int i; |
| 322 | |
| 323 | fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */ |
| 324 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 325 | for (i = 0; i < req->num_pages; i++) { |
| 326 | struct page *page = req->pages[i]; |
| 327 | if (!req->out.h.error) |
| 328 | SetPageUptodate(page); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 329 | else |
| 330 | SetPageError(page); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 331 | unlock_page(page); |
| 332 | } |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 333 | fuse_put_request(fc, req); |
| 334 | } |
| 335 | |
| 336 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, |
| 337 | struct inode *inode) |
| 338 | { |
| 339 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 340 | loff_t pos = page_offset(req->pages[0]); |
| 341 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; |
| 342 | req->out.page_zeroing = 1; |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 343 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 344 | if (fc->async_read) { |
| 345 | req->end = fuse_readpages_end; |
| 346 | request_send_background(fc, req); |
| 347 | } else { |
| 348 | request_send(fc, req); |
| 349 | fuse_readpages_end(fc, req); |
| 350 | } |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | struct fuse_readpages_data { |
| 354 | struct fuse_req *req; |
| 355 | struct file *file; |
| 356 | struct inode *inode; |
| 357 | }; |
| 358 | |
| 359 | static int fuse_readpages_fill(void *_data, struct page *page) |
| 360 | { |
| 361 | struct fuse_readpages_data *data = _data; |
| 362 | struct fuse_req *req = data->req; |
| 363 | struct inode *inode = data->inode; |
| 364 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 365 | |
| 366 | if (req->num_pages && |
| 367 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || |
| 368 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || |
| 369 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 370 | fuse_send_readpages(req, data->file, inode); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 371 | data->req = req = fuse_get_req(fc); |
| 372 | if (IS_ERR(req)) { |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 373 | unlock_page(page); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 374 | return PTR_ERR(req); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 375 | } |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 376 | } |
| 377 | req->pages[req->num_pages] = page; |
| 378 | req->num_pages ++; |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static int fuse_readpages(struct file *file, struct address_space *mapping, |
| 383 | struct list_head *pages, unsigned nr_pages) |
| 384 | { |
| 385 | struct inode *inode = mapping->host; |
| 386 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 387 | struct fuse_readpages_data data; |
| 388 | int err; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 389 | |
| 390 | if (is_bad_inode(inode)) |
| 391 | return -EIO; |
| 392 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 393 | data.file = file; |
| 394 | data.inode = inode; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 395 | data.req = fuse_get_req(fc); |
| 396 | if (IS_ERR(data.req)) |
| 397 | return PTR_ERR(data.req); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 398 | |
| 399 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); |
Miklos Szeredi | d3406ff | 2006-04-10 22:54:49 -0700 | [diff] [blame] | 400 | if (!err) { |
| 401 | if (data.req->num_pages) |
| 402 | fuse_send_readpages(data.req, file, inode); |
| 403 | else |
| 404 | fuse_put_request(fc, data.req); |
| 405 | } |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 406 | return err; |
| 407 | } |
| 408 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 409 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, |
| 410 | struct inode *inode, loff_t pos, size_t count) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 411 | { |
| 412 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 413 | struct fuse_file *ff = file->private_data; |
| 414 | struct fuse_write_in inarg; |
| 415 | struct fuse_write_out outarg; |
| 416 | |
| 417 | memset(&inarg, 0, sizeof(struct fuse_write_in)); |
| 418 | inarg.fh = ff->fh; |
| 419 | inarg.offset = pos; |
| 420 | inarg.size = count; |
| 421 | req->in.h.opcode = FUSE_WRITE; |
| 422 | req->in.h.nodeid = get_node_id(inode); |
| 423 | req->inode = inode; |
| 424 | req->file = file; |
| 425 | req->in.argpages = 1; |
| 426 | req->in.numargs = 2; |
| 427 | req->in.args[0].size = sizeof(struct fuse_write_in); |
| 428 | req->in.args[0].value = &inarg; |
| 429 | req->in.args[1].size = count; |
| 430 | req->out.numargs = 1; |
| 431 | req->out.args[0].size = sizeof(struct fuse_write_out); |
| 432 | req->out.args[0].value = &outarg; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 433 | request_send(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 434 | return outarg.size; |
| 435 | } |
| 436 | |
| 437 | static int fuse_prepare_write(struct file *file, struct page *page, |
| 438 | unsigned offset, unsigned to) |
| 439 | { |
| 440 | /* No op */ |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int fuse_commit_write(struct file *file, struct page *page, |
| 445 | unsigned offset, unsigned to) |
| 446 | { |
| 447 | int err; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 448 | size_t nres; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 449 | unsigned count = to - offset; |
| 450 | struct inode *inode = page->mapping->host; |
| 451 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 4633a22 | 2006-01-06 00:19:36 -0800 | [diff] [blame] | 452 | loff_t pos = page_offset(page) + offset; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 453 | struct fuse_req *req; |
| 454 | |
| 455 | if (is_bad_inode(inode)) |
| 456 | return -EIO; |
| 457 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 458 | req = fuse_get_req(fc); |
| 459 | if (IS_ERR(req)) |
| 460 | return PTR_ERR(req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 461 | |
| 462 | req->num_pages = 1; |
| 463 | req->pages[0] = page; |
| 464 | req->page_offset = offset; |
| 465 | nres = fuse_send_write(req, file, inode, pos, count); |
| 466 | err = req->out.h.error; |
| 467 | fuse_put_request(fc, req); |
| 468 | if (!err && nres != count) |
| 469 | err = -EIO; |
| 470 | if (!err) { |
| 471 | pos += count; |
| 472 | if (pos > i_size_read(inode)) |
| 473 | i_size_write(inode, pos); |
| 474 | |
| 475 | if (offset == 0 && to == PAGE_CACHE_SIZE) { |
| 476 | clear_page_dirty(page); |
| 477 | SetPageUptodate(page); |
| 478 | } |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 479 | } |
| 480 | fuse_invalidate_attr(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 481 | return err; |
| 482 | } |
| 483 | |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 484 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
| 485 | { |
| 486 | unsigned i; |
| 487 | |
| 488 | for (i = 0; i < req->num_pages; i++) { |
| 489 | struct page *page = req->pages[i]; |
| 490 | if (write) |
| 491 | set_page_dirty_lock(page); |
| 492 | put_page(page); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, |
| 497 | unsigned nbytes, int write) |
| 498 | { |
| 499 | unsigned long user_addr = (unsigned long) buf; |
| 500 | unsigned offset = user_addr & ~PAGE_MASK; |
| 501 | int npages; |
| 502 | |
| 503 | /* This doesn't work with nfsd */ |
| 504 | if (!current->mm) |
| 505 | return -EPERM; |
| 506 | |
| 507 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); |
| 508 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Miklos Szeredi | 6ad84ac | 2006-01-06 00:19:42 -0800 | [diff] [blame] | 509 | npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 510 | down_read(¤t->mm->mmap_sem); |
| 511 | npages = get_user_pages(current, current->mm, user_addr, npages, write, |
| 512 | 0, req->pages, NULL); |
| 513 | up_read(¤t->mm->mmap_sem); |
| 514 | if (npages < 0) |
| 515 | return npages; |
| 516 | |
| 517 | req->num_pages = npages; |
| 518 | req->page_offset = offset; |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, |
| 523 | size_t count, loff_t *ppos, int write) |
| 524 | { |
| 525 | struct inode *inode = file->f_dentry->d_inode; |
| 526 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 527 | size_t nmax = write ? fc->max_write : fc->max_read; |
| 528 | loff_t pos = *ppos; |
| 529 | ssize_t res = 0; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 530 | struct fuse_req *req; |
| 531 | |
| 532 | if (is_bad_inode(inode)) |
| 533 | return -EIO; |
| 534 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 535 | req = fuse_get_req(fc); |
| 536 | if (IS_ERR(req)) |
| 537 | return PTR_ERR(req); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 538 | |
| 539 | while (count) { |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 540 | size_t nres; |
| 541 | size_t nbytes = min(count, nmax); |
| 542 | int err = fuse_get_user_pages(req, buf, nbytes, !write); |
| 543 | if (err) { |
| 544 | res = err; |
| 545 | break; |
| 546 | } |
Miklos Szeredi | 6ad84ac | 2006-01-06 00:19:42 -0800 | [diff] [blame] | 547 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
| 548 | nbytes = min(count, nbytes); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 549 | if (write) |
| 550 | nres = fuse_send_write(req, file, inode, pos, nbytes); |
| 551 | else |
| 552 | nres = fuse_send_read(req, file, inode, pos, nbytes); |
| 553 | fuse_release_user_pages(req, !write); |
| 554 | if (req->out.h.error) { |
| 555 | if (!res) |
| 556 | res = req->out.h.error; |
| 557 | break; |
| 558 | } else if (nres > nbytes) { |
| 559 | res = -EIO; |
| 560 | break; |
| 561 | } |
| 562 | count -= nres; |
| 563 | res += nres; |
| 564 | pos += nres; |
| 565 | buf += nres; |
| 566 | if (nres != nbytes) |
| 567 | break; |
Miklos Szeredi | 56cf34f | 2006-04-11 21:16:51 +0200 | [diff] [blame] | 568 | if (count) { |
| 569 | fuse_put_request(fc, req); |
| 570 | req = fuse_get_req(fc); |
| 571 | if (IS_ERR(req)) |
| 572 | break; |
| 573 | } |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 574 | } |
| 575 | fuse_put_request(fc, req); |
| 576 | if (res > 0) { |
| 577 | if (write && pos > i_size_read(inode)) |
| 578 | i_size_write(inode, pos); |
| 579 | *ppos = pos; |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 580 | } |
| 581 | fuse_invalidate_attr(inode); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 582 | |
| 583 | return res; |
| 584 | } |
| 585 | |
| 586 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
| 587 | size_t count, loff_t *ppos) |
| 588 | { |
| 589 | return fuse_direct_io(file, buf, count, ppos, 0); |
| 590 | } |
| 591 | |
| 592 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
| 593 | size_t count, loff_t *ppos) |
| 594 | { |
| 595 | struct inode *inode = file->f_dentry->d_inode; |
| 596 | ssize_t res; |
| 597 | /* Don't allow parallel writes to the same file */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 598 | mutex_lock(&inode->i_mutex); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 599 | res = fuse_direct_io(file, buf, count, ppos, 1); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 600 | mutex_unlock(&inode->i_mutex); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 601 | return res; |
| 602 | } |
| 603 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 604 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 605 | { |
| 606 | if ((vma->vm_flags & VM_SHARED)) { |
| 607 | if ((vma->vm_flags & VM_WRITE)) |
| 608 | return -ENODEV; |
| 609 | else |
| 610 | vma->vm_flags &= ~VM_MAYWRITE; |
| 611 | } |
| 612 | return generic_file_mmap(file, vma); |
| 613 | } |
| 614 | |
| 615 | static int fuse_set_page_dirty(struct page *page) |
| 616 | { |
| 617 | printk("fuse_set_page_dirty: should not happen\n"); |
| 618 | dump_stack(); |
| 619 | return 0; |
| 620 | } |
| 621 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 622 | static const struct file_operations fuse_file_operations = { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 623 | .llseek = generic_file_llseek, |
| 624 | .read = generic_file_read, |
| 625 | .write = generic_file_write, |
| 626 | .mmap = fuse_file_mmap, |
| 627 | .open = fuse_open, |
| 628 | .flush = fuse_flush, |
| 629 | .release = fuse_release, |
| 630 | .fsync = fuse_fsync, |
| 631 | .sendfile = generic_file_sendfile, |
| 632 | }; |
| 633 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 634 | static const struct file_operations fuse_direct_io_file_operations = { |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 635 | .llseek = generic_file_llseek, |
| 636 | .read = fuse_direct_read, |
| 637 | .write = fuse_direct_write, |
| 638 | .open = fuse_open, |
| 639 | .flush = fuse_flush, |
| 640 | .release = fuse_release, |
| 641 | .fsync = fuse_fsync, |
| 642 | /* no mmap and sendfile */ |
| 643 | }; |
| 644 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 645 | static struct address_space_operations fuse_file_aops = { |
| 646 | .readpage = fuse_readpage, |
| 647 | .prepare_write = fuse_prepare_write, |
| 648 | .commit_write = fuse_commit_write, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 649 | .readpages = fuse_readpages, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 650 | .set_page_dirty = fuse_set_page_dirty, |
| 651 | }; |
| 652 | |
| 653 | void fuse_init_file_inode(struct inode *inode) |
| 654 | { |
Miklos Szeredi | 45323fb | 2005-09-09 13:10:37 -0700 | [diff] [blame] | 655 | inode->i_fop = &fuse_file_operations; |
| 656 | inode->i_data.a_ops = &fuse_file_aops; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 657 | } |