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