Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
| 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 | |
Miklos Szeredi | 45323fb | 2005-09-09 13:10:37 -0700 | [diff] [blame] | 15 | static struct file_operations fuse_direct_io_file_operations; |
| 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 | |
| 25 | req = fuse_get_request(fc); |
| 26 | if (!req) |
| 27 | return -EINTR; |
| 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 | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 116 | void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff, |
| 117 | u64 nodeid, struct inode *inode, int flags, int isdir) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 118 | { |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 119 | struct fuse_req * req = ff->release_req; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 120 | struct fuse_release_in *inarg = &req->misc.release_in; |
| 121 | |
| 122 | inarg->fh = ff->fh; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 123 | inarg->flags = flags; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 124 | req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 125 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 126 | req->inode = inode; |
| 127 | req->in.numargs = 1; |
| 128 | req->in.args[0].size = sizeof(struct fuse_release_in); |
| 129 | req->in.args[0].value = inarg; |
| 130 | request_send_background(fc, req); |
| 131 | kfree(ff); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) |
| 135 | { |
| 136 | struct fuse_file *ff = file->private_data; |
| 137 | if (ff) { |
| 138 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 139 | u64 nodeid = get_node_id(inode); |
| 140 | fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir); |
| 141 | } |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 142 | |
| 143 | /* Return value is ignored by VFS */ |
| 144 | return 0; |
| 145 | } |
| 146 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 147 | static int fuse_open(struct inode *inode, struct file *file) |
| 148 | { |
| 149 | return fuse_open_common(inode, file, 0); |
| 150 | } |
| 151 | |
| 152 | static int fuse_release(struct inode *inode, struct file *file) |
| 153 | { |
| 154 | return fuse_release_common(inode, file, 0); |
| 155 | } |
| 156 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 157 | static int fuse_flush(struct file *file) |
| 158 | { |
| 159 | struct inode *inode = file->f_dentry->d_inode; |
| 160 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 161 | struct fuse_file *ff = file->private_data; |
| 162 | struct fuse_req *req; |
| 163 | struct fuse_flush_in inarg; |
| 164 | int err; |
| 165 | |
| 166 | if (fc->no_flush) |
| 167 | return 0; |
| 168 | |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 169 | req = fuse_get_request(fc); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 170 | if (!req) |
| 171 | return -EINTR; |
| 172 | |
| 173 | memset(&inarg, 0, sizeof(inarg)); |
| 174 | inarg.fh = ff->fh; |
| 175 | req->in.h.opcode = FUSE_FLUSH; |
| 176 | req->in.h.nodeid = get_node_id(inode); |
| 177 | req->inode = inode; |
| 178 | req->file = file; |
| 179 | req->in.numargs = 1; |
| 180 | req->in.args[0].size = sizeof(inarg); |
| 181 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 182 | request_send(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 183 | err = req->out.h.error; |
| 184 | fuse_put_request(fc, req); |
| 185 | if (err == -ENOSYS) { |
| 186 | fc->no_flush = 1; |
| 187 | err = 0; |
| 188 | } |
| 189 | return err; |
| 190 | } |
| 191 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 192 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
| 193 | int isdir) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 194 | { |
| 195 | struct inode *inode = de->d_inode; |
| 196 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 197 | struct fuse_file *ff = file->private_data; |
| 198 | struct fuse_req *req; |
| 199 | struct fuse_fsync_in inarg; |
| 200 | int err; |
| 201 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 202 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | |
| 205 | req = fuse_get_request(fc); |
| 206 | if (!req) |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 207 | return -EINTR; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 208 | |
| 209 | memset(&inarg, 0, sizeof(inarg)); |
| 210 | inarg.fh = ff->fh; |
| 211 | inarg.fsync_flags = datasync ? 1 : 0; |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 212 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 213 | req->in.h.nodeid = get_node_id(inode); |
| 214 | req->inode = inode; |
| 215 | req->file = file; |
| 216 | req->in.numargs = 1; |
| 217 | req->in.args[0].size = sizeof(inarg); |
| 218 | req->in.args[0].value = &inarg; |
| 219 | request_send(fc, req); |
| 220 | err = req->out.h.error; |
| 221 | fuse_put_request(fc, req); |
| 222 | if (err == -ENOSYS) { |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 223 | if (isdir) |
| 224 | fc->no_fsyncdir = 1; |
| 225 | else |
| 226 | fc->no_fsync = 1; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 227 | err = 0; |
| 228 | } |
| 229 | return err; |
| 230 | } |
| 231 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 232 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
| 233 | { |
| 234 | return fuse_fsync_common(file, de, datasync, 0); |
| 235 | } |
| 236 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 237 | size_t fuse_send_read_common(struct fuse_req *req, struct file *file, |
| 238 | struct inode *inode, loff_t pos, size_t count, |
| 239 | int isdir) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 240 | { |
| 241 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 242 | struct fuse_file *ff = file->private_data; |
| 243 | struct fuse_read_in inarg; |
| 244 | |
| 245 | memset(&inarg, 0, sizeof(struct fuse_read_in)); |
| 246 | inarg.fh = ff->fh; |
| 247 | inarg.offset = pos; |
| 248 | inarg.size = count; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 249 | req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 250 | req->in.h.nodeid = get_node_id(inode); |
| 251 | req->inode = inode; |
| 252 | req->file = file; |
| 253 | req->in.numargs = 1; |
| 254 | req->in.args[0].size = sizeof(struct fuse_read_in); |
| 255 | req->in.args[0].value = &inarg; |
| 256 | req->out.argpages = 1; |
| 257 | req->out.argvar = 1; |
| 258 | req->out.numargs = 1; |
| 259 | req->out.args[0].size = count; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 260 | request_send(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 261 | return req->out.args[0].size; |
| 262 | } |
| 263 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 264 | static inline size_t fuse_send_read(struct fuse_req *req, struct file *file, |
| 265 | struct inode *inode, loff_t pos, |
| 266 | size_t count) |
| 267 | { |
| 268 | return fuse_send_read_common(req, file, inode, pos, count, 0); |
| 269 | } |
| 270 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 271 | static int fuse_readpage(struct file *file, struct page *page) |
| 272 | { |
| 273 | struct inode *inode = page->mapping->host; |
| 274 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 275 | loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 276 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 277 | int err = -EINTR; |
| 278 | if (!req) |
| 279 | goto out; |
| 280 | |
| 281 | req->out.page_zeroing = 1; |
| 282 | req->num_pages = 1; |
| 283 | req->pages[0] = page; |
| 284 | fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE); |
| 285 | err = req->out.h.error; |
| 286 | fuse_put_request(fc, req); |
| 287 | if (!err) |
| 288 | SetPageUptodate(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 289 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 290 | out: |
| 291 | unlock_page(page); |
| 292 | return err; |
| 293 | } |
| 294 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 295 | static int fuse_send_readpages(struct fuse_req *req, struct file *file, |
| 296 | struct inode *inode) |
| 297 | { |
| 298 | loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT; |
| 299 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; |
| 300 | unsigned i; |
| 301 | req->out.page_zeroing = 1; |
| 302 | fuse_send_read(req, file, inode, pos, count); |
| 303 | for (i = 0; i < req->num_pages; i++) { |
| 304 | struct page *page = req->pages[i]; |
| 305 | if (!req->out.h.error) |
| 306 | SetPageUptodate(page); |
| 307 | unlock_page(page); |
| 308 | } |
| 309 | return req->out.h.error; |
| 310 | } |
| 311 | |
| 312 | struct fuse_readpages_data { |
| 313 | struct fuse_req *req; |
| 314 | struct file *file; |
| 315 | struct inode *inode; |
| 316 | }; |
| 317 | |
| 318 | static int fuse_readpages_fill(void *_data, struct page *page) |
| 319 | { |
| 320 | struct fuse_readpages_data *data = _data; |
| 321 | struct fuse_req *req = data->req; |
| 322 | struct inode *inode = data->inode; |
| 323 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 324 | |
| 325 | if (req->num_pages && |
| 326 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || |
| 327 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || |
| 328 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { |
| 329 | int err = fuse_send_readpages(req, data->file, inode); |
| 330 | if (err) { |
| 331 | unlock_page(page); |
| 332 | return err; |
| 333 | } |
| 334 | fuse_reset_request(req); |
| 335 | } |
| 336 | req->pages[req->num_pages] = page; |
| 337 | req->num_pages ++; |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int fuse_readpages(struct file *file, struct address_space *mapping, |
| 342 | struct list_head *pages, unsigned nr_pages) |
| 343 | { |
| 344 | struct inode *inode = mapping->host; |
| 345 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 346 | struct fuse_readpages_data data; |
| 347 | int err; |
| 348 | data.file = file; |
| 349 | data.inode = inode; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 350 | data.req = fuse_get_request(fc); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 351 | if (!data.req) |
| 352 | return -EINTR; |
| 353 | |
| 354 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); |
| 355 | if (!err && data.req->num_pages) |
| 356 | err = fuse_send_readpages(data.req, file, inode); |
| 357 | fuse_put_request(fc, data.req); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 358 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 359 | return err; |
| 360 | } |
| 361 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 362 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, |
| 363 | struct inode *inode, loff_t pos, size_t count) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 364 | { |
| 365 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 366 | struct fuse_file *ff = file->private_data; |
| 367 | struct fuse_write_in inarg; |
| 368 | struct fuse_write_out outarg; |
| 369 | |
| 370 | memset(&inarg, 0, sizeof(struct fuse_write_in)); |
| 371 | inarg.fh = ff->fh; |
| 372 | inarg.offset = pos; |
| 373 | inarg.size = count; |
| 374 | req->in.h.opcode = FUSE_WRITE; |
| 375 | req->in.h.nodeid = get_node_id(inode); |
| 376 | req->inode = inode; |
| 377 | req->file = file; |
| 378 | req->in.argpages = 1; |
| 379 | req->in.numargs = 2; |
| 380 | req->in.args[0].size = sizeof(struct fuse_write_in); |
| 381 | req->in.args[0].value = &inarg; |
| 382 | req->in.args[1].size = count; |
| 383 | req->out.numargs = 1; |
| 384 | req->out.args[0].size = sizeof(struct fuse_write_out); |
| 385 | req->out.args[0].value = &outarg; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 386 | request_send(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 387 | return outarg.size; |
| 388 | } |
| 389 | |
| 390 | static int fuse_prepare_write(struct file *file, struct page *page, |
| 391 | unsigned offset, unsigned to) |
| 392 | { |
| 393 | /* No op */ |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int fuse_commit_write(struct file *file, struct page *page, |
| 398 | unsigned offset, unsigned to) |
| 399 | { |
| 400 | int err; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 401 | size_t nres; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 402 | unsigned count = to - offset; |
| 403 | struct inode *inode = page->mapping->host; |
| 404 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 405 | loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 406 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 407 | if (!req) |
| 408 | return -EINTR; |
| 409 | |
| 410 | req->num_pages = 1; |
| 411 | req->pages[0] = page; |
| 412 | req->page_offset = offset; |
| 413 | nres = fuse_send_write(req, file, inode, pos, count); |
| 414 | err = req->out.h.error; |
| 415 | fuse_put_request(fc, req); |
| 416 | if (!err && nres != count) |
| 417 | err = -EIO; |
| 418 | if (!err) { |
| 419 | pos += count; |
| 420 | if (pos > i_size_read(inode)) |
| 421 | i_size_write(inode, pos); |
| 422 | |
| 423 | if (offset == 0 && to == PAGE_CACHE_SIZE) { |
| 424 | clear_page_dirty(page); |
| 425 | SetPageUptodate(page); |
| 426 | } |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 427 | } |
| 428 | fuse_invalidate_attr(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 429 | return err; |
| 430 | } |
| 431 | |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 432 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
| 433 | { |
| 434 | unsigned i; |
| 435 | |
| 436 | for (i = 0; i < req->num_pages; i++) { |
| 437 | struct page *page = req->pages[i]; |
| 438 | if (write) |
| 439 | set_page_dirty_lock(page); |
| 440 | put_page(page); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, |
| 445 | unsigned nbytes, int write) |
| 446 | { |
| 447 | unsigned long user_addr = (unsigned long) buf; |
| 448 | unsigned offset = user_addr & ~PAGE_MASK; |
| 449 | int npages; |
| 450 | |
| 451 | /* This doesn't work with nfsd */ |
| 452 | if (!current->mm) |
| 453 | return -EPERM; |
| 454 | |
| 455 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); |
| 456 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 457 | npages = min(npages, FUSE_MAX_PAGES_PER_REQ); |
| 458 | down_read(¤t->mm->mmap_sem); |
| 459 | npages = get_user_pages(current, current->mm, user_addr, npages, write, |
| 460 | 0, req->pages, NULL); |
| 461 | up_read(¤t->mm->mmap_sem); |
| 462 | if (npages < 0) |
| 463 | return npages; |
| 464 | |
| 465 | req->num_pages = npages; |
| 466 | req->page_offset = offset; |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, |
| 471 | size_t count, loff_t *ppos, int write) |
| 472 | { |
| 473 | struct inode *inode = file->f_dentry->d_inode; |
| 474 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 475 | size_t nmax = write ? fc->max_write : fc->max_read; |
| 476 | loff_t pos = *ppos; |
| 477 | ssize_t res = 0; |
| 478 | struct fuse_req *req = fuse_get_request(fc); |
| 479 | if (!req) |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 480 | return -EINTR; |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 481 | |
| 482 | while (count) { |
| 483 | size_t tmp; |
| 484 | size_t nres; |
| 485 | size_t nbytes = min(count, nmax); |
| 486 | int err = fuse_get_user_pages(req, buf, nbytes, !write); |
| 487 | if (err) { |
| 488 | res = err; |
| 489 | break; |
| 490 | } |
| 491 | tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
| 492 | nbytes = min(nbytes, tmp); |
| 493 | if (write) |
| 494 | nres = fuse_send_write(req, file, inode, pos, nbytes); |
| 495 | else |
| 496 | nres = fuse_send_read(req, file, inode, pos, nbytes); |
| 497 | fuse_release_user_pages(req, !write); |
| 498 | if (req->out.h.error) { |
| 499 | if (!res) |
| 500 | res = req->out.h.error; |
| 501 | break; |
| 502 | } else if (nres > nbytes) { |
| 503 | res = -EIO; |
| 504 | break; |
| 505 | } |
| 506 | count -= nres; |
| 507 | res += nres; |
| 508 | pos += nres; |
| 509 | buf += nres; |
| 510 | if (nres != nbytes) |
| 511 | break; |
| 512 | if (count) |
| 513 | fuse_reset_request(req); |
| 514 | } |
| 515 | fuse_put_request(fc, req); |
| 516 | if (res > 0) { |
| 517 | if (write && pos > i_size_read(inode)) |
| 518 | i_size_write(inode, pos); |
| 519 | *ppos = pos; |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 520 | } |
| 521 | fuse_invalidate_attr(inode); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 522 | |
| 523 | return res; |
| 524 | } |
| 525 | |
| 526 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
| 527 | size_t count, loff_t *ppos) |
| 528 | { |
| 529 | return fuse_direct_io(file, buf, count, ppos, 0); |
| 530 | } |
| 531 | |
| 532 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
| 533 | size_t count, loff_t *ppos) |
| 534 | { |
| 535 | struct inode *inode = file->f_dentry->d_inode; |
| 536 | ssize_t res; |
| 537 | /* Don't allow parallel writes to the same file */ |
| 538 | down(&inode->i_sem); |
| 539 | res = fuse_direct_io(file, buf, count, ppos, 1); |
| 540 | up(&inode->i_sem); |
| 541 | return res; |
| 542 | } |
| 543 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 544 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 545 | { |
| 546 | if ((vma->vm_flags & VM_SHARED)) { |
| 547 | if ((vma->vm_flags & VM_WRITE)) |
| 548 | return -ENODEV; |
| 549 | else |
| 550 | vma->vm_flags &= ~VM_MAYWRITE; |
| 551 | } |
| 552 | return generic_file_mmap(file, vma); |
| 553 | } |
| 554 | |
| 555 | static int fuse_set_page_dirty(struct page *page) |
| 556 | { |
| 557 | printk("fuse_set_page_dirty: should not happen\n"); |
| 558 | dump_stack(); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static struct file_operations fuse_file_operations = { |
| 563 | .llseek = generic_file_llseek, |
| 564 | .read = generic_file_read, |
| 565 | .write = generic_file_write, |
| 566 | .mmap = fuse_file_mmap, |
| 567 | .open = fuse_open, |
| 568 | .flush = fuse_flush, |
| 569 | .release = fuse_release, |
| 570 | .fsync = fuse_fsync, |
| 571 | .sendfile = generic_file_sendfile, |
| 572 | }; |
| 573 | |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 574 | static struct file_operations fuse_direct_io_file_operations = { |
| 575 | .llseek = generic_file_llseek, |
| 576 | .read = fuse_direct_read, |
| 577 | .write = fuse_direct_write, |
| 578 | .open = fuse_open, |
| 579 | .flush = fuse_flush, |
| 580 | .release = fuse_release, |
| 581 | .fsync = fuse_fsync, |
| 582 | /* no mmap and sendfile */ |
| 583 | }; |
| 584 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 585 | static struct address_space_operations fuse_file_aops = { |
| 586 | .readpage = fuse_readpage, |
| 587 | .prepare_write = fuse_prepare_write, |
| 588 | .commit_write = fuse_commit_write, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 589 | .readpages = fuse_readpages, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 590 | .set_page_dirty = fuse_set_page_dirty, |
| 591 | }; |
| 592 | |
| 593 | void fuse_init_file_inode(struct inode *inode) |
| 594 | { |
Miklos Szeredi | 45323fb | 2005-09-09 13:10:37 -0700 | [diff] [blame] | 595 | inode->i_fop = &fuse_file_operations; |
| 596 | inode->i_data.a_ops = &fuse_file_aops; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 597 | } |