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