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