Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 56cf34f | 2006-04-11 21:16:51 +0200 | [diff] [blame] | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/kernel.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 14 | #include <linux/sched.h> |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 15 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 16 | static const struct file_operations fuse_direct_io_file_operations; |
Miklos Szeredi | 45323fb | 2005-09-09 13:10:37 -0700 | [diff] [blame] | 17 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 18 | static int fuse_send_open(struct inode *inode, struct file *file, int isdir, |
| 19 | struct fuse_open_out *outargp) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 20 | { |
| 21 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 22 | struct fuse_open_in inarg; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 23 | struct fuse_req *req; |
| 24 | int err; |
| 25 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 26 | req = fuse_get_req(fc); |
| 27 | if (IS_ERR(req)) |
| 28 | return PTR_ERR(req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 29 | |
| 30 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 6ff958e | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 31 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
| 32 | if (!fc->atomic_o_trunc) |
| 33 | inarg.flags &= ~O_TRUNC; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 34 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
| 35 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 36 | req->in.numargs = 1; |
| 37 | req->in.args[0].size = sizeof(inarg); |
| 38 | req->in.args[0].value = &inarg; |
| 39 | req->out.numargs = 1; |
| 40 | req->out.args[0].size = sizeof(*outargp); |
| 41 | req->out.args[0].value = outargp; |
| 42 | request_send(fc, req); |
| 43 | err = req->out.h.error; |
| 44 | fuse_put_request(fc, req); |
| 45 | |
| 46 | return err; |
| 47 | } |
| 48 | |
| 49 | struct fuse_file *fuse_file_alloc(void) |
| 50 | { |
| 51 | struct fuse_file *ff; |
| 52 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); |
| 53 | if (ff) { |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 54 | ff->reserved_req = fuse_request_alloc(); |
| 55 | if (!ff->reserved_req) { |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 56 | kfree(ff); |
| 57 | ff = NULL; |
Adrian Bunk | 8744969 | 2007-11-14 17:00:02 -0800 | [diff] [blame] | 58 | } else { |
| 59 | INIT_LIST_HEAD(&ff->write_entry); |
| 60 | atomic_set(&ff->count, 0); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | return ff; |
| 64 | } |
| 65 | |
| 66 | void fuse_file_free(struct fuse_file *ff) |
| 67 | { |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 68 | fuse_request_free(ff->reserved_req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 69 | kfree(ff); |
| 70 | } |
| 71 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 72 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) |
| 73 | { |
| 74 | atomic_inc(&ff->count); |
| 75 | return ff; |
| 76 | } |
| 77 | |
Miklos Szeredi | 819c4b3 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 78 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
| 79 | { |
Miklos Szeredi | b57d426 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 80 | dput(req->misc.release.dentry); |
| 81 | mntput(req->misc.release.vfsmount); |
Miklos Szeredi | 819c4b3 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 82 | fuse_put_request(fc, req); |
| 83 | } |
| 84 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 85 | static void fuse_file_put(struct fuse_file *ff) |
| 86 | { |
| 87 | if (atomic_dec_and_test(&ff->count)) { |
| 88 | struct fuse_req *req = ff->reserved_req; |
Miklos Szeredi | b57d426 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 89 | struct inode *inode = req->misc.release.dentry->d_inode; |
| 90 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 819c4b3 | 2007-10-16 23:31:04 -0700 | [diff] [blame] | 91 | req->end = fuse_release_end; |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 92 | request_send_background(fc, req); |
| 93 | kfree(ff); |
| 94 | } |
| 95 | } |
| 96 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 97 | void fuse_finish_open(struct inode *inode, struct file *file, |
| 98 | struct fuse_file *ff, struct fuse_open_out *outarg) |
| 99 | { |
| 100 | if (outarg->open_flags & FOPEN_DIRECT_IO) |
| 101 | file->f_op = &fuse_direct_io_file_operations; |
| 102 | if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) |
Miklos Szeredi | b100997 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 103 | invalidate_inode_pages2(inode->i_mapping); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 104 | ff->fh = outarg->fh; |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 105 | file->private_data = fuse_file_get(ff); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) |
| 109 | { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 110 | struct fuse_open_out outarg; |
| 111 | struct fuse_file *ff; |
| 112 | int err; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 113 | |
Miklos Szeredi | dd190d0 | 2005-09-30 11:59:02 -0700 | [diff] [blame] | 114 | /* VFS checks this, but only _after_ ->open() */ |
| 115 | if (file->f_flags & O_DIRECT) |
| 116 | return -EINVAL; |
| 117 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 118 | err = generic_file_open(inode, file); |
| 119 | if (err) |
| 120 | return err; |
| 121 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 122 | ff = fuse_file_alloc(); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 123 | if (!ff) |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 124 | return -ENOMEM; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 125 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 126 | err = fuse_send_open(inode, file, isdir, &outarg); |
| 127 | if (err) |
| 128 | fuse_file_free(ff); |
| 129 | else { |
| 130 | if (isdir) |
| 131 | outarg.open_flags &= ~FOPEN_DIRECT_IO; |
| 132 | fuse_finish_open(inode, file, ff, &outarg); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 135 | return err; |
| 136 | } |
| 137 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 138 | void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode) |
Miklos Szeredi | 64c6d8e | 2006-01-16 22:14:42 -0800 | [diff] [blame] | 139 | { |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 140 | struct fuse_req *req = ff->reserved_req; |
Miklos Szeredi | b57d426 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 141 | struct fuse_release_in *inarg = &req->misc.release.in; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 142 | |
| 143 | inarg->fh = ff->fh; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 144 | inarg->flags = flags; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 145 | req->in.h.opcode = opcode; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 146 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 147 | req->in.numargs = 1; |
| 148 | req->in.args[0].size = sizeof(struct fuse_release_in); |
| 149 | req->in.args[0].value = inarg; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) |
| 153 | { |
| 154 | struct fuse_file *ff = file->private_data; |
| 155 | if (ff) { |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 156 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | b57d426 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 157 | struct fuse_req *req = ff->reserved_req; |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 158 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 159 | fuse_release_fill(ff, get_node_id(inode), file->f_flags, |
| 160 | isdir ? FUSE_RELEASEDIR : FUSE_RELEASE); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 161 | |
| 162 | /* Hold vfsmount and dentry until release is finished */ |
Miklos Szeredi | b57d426 | 2008-02-06 01:38:39 -0800 | [diff] [blame] | 163 | req->misc.release.vfsmount = mntget(file->f_path.mnt); |
| 164 | req->misc.release.dentry = dget(file->f_path.dentry); |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 165 | |
| 166 | spin_lock(&fc->lock); |
| 167 | list_del(&ff->write_entry); |
| 168 | spin_unlock(&fc->lock); |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 169 | /* |
| 170 | * Normally this will send the RELEASE request, |
| 171 | * however if some asynchronous READ or WRITE requests |
| 172 | * are outstanding, the sending will be delayed |
| 173 | */ |
| 174 | fuse_file_put(ff); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 175 | } |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 176 | |
| 177 | /* Return value is ignored by VFS */ |
| 178 | return 0; |
| 179 | } |
| 180 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 181 | static int fuse_open(struct inode *inode, struct file *file) |
| 182 | { |
| 183 | return fuse_open_common(inode, file, 0); |
| 184 | } |
| 185 | |
| 186 | static int fuse_release(struct inode *inode, struct file *file) |
| 187 | { |
| 188 | return fuse_release_common(inode, file, 0); |
| 189 | } |
| 190 | |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 191 | /* |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 192 | * Scramble the ID space with XTEA, so that the value of the files_struct |
| 193 | * pointer is not exposed to userspace. |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 194 | */ |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 195 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 196 | { |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 197 | u32 *k = fc->scramble_key; |
| 198 | u64 v = (unsigned long) id; |
| 199 | u32 v0 = v; |
| 200 | u32 v1 = v >> 32; |
| 201 | u32 sum = 0; |
| 202 | int i; |
| 203 | |
| 204 | for (i = 0; i < 32; i++) { |
| 205 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); |
| 206 | sum += 0x9E3779B9; |
| 207 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); |
| 208 | } |
| 209 | |
| 210 | return (u64) v0 + ((u64) v1 << 32); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 213 | /* |
| 214 | * Check if page is under writeback |
| 215 | * |
| 216 | * This is currently done by walking the list of writepage requests |
| 217 | * for the inode, which can be pretty inefficient. |
| 218 | */ |
| 219 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) |
| 220 | { |
| 221 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 222 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 223 | struct fuse_req *req; |
| 224 | bool found = false; |
| 225 | |
| 226 | spin_lock(&fc->lock); |
| 227 | list_for_each_entry(req, &fi->writepages, writepages_entry) { |
| 228 | pgoff_t curr_index; |
| 229 | |
| 230 | BUG_ON(req->inode != inode); |
| 231 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; |
| 232 | if (curr_index == index) { |
| 233 | found = true; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | spin_unlock(&fc->lock); |
| 238 | |
| 239 | return found; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Wait for page writeback to be completed. |
| 244 | * |
| 245 | * Since fuse doesn't rely on the VM writeback tracking, this has to |
| 246 | * use some other means. |
| 247 | */ |
| 248 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) |
| 249 | { |
| 250 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 251 | |
| 252 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 256 | static int fuse_flush(struct file *file, fl_owner_t id) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 257 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 258 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 259 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 260 | struct fuse_file *ff = file->private_data; |
| 261 | struct fuse_req *req; |
| 262 | struct fuse_flush_in inarg; |
| 263 | int err; |
| 264 | |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 265 | if (is_bad_inode(inode)) |
| 266 | return -EIO; |
| 267 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 268 | if (fc->no_flush) |
| 269 | return 0; |
| 270 | |
Miklos Szeredi | 33649c9 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 271 | req = fuse_get_req_nofail(fc, file); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 272 | memset(&inarg, 0, sizeof(inarg)); |
| 273 | inarg.fh = ff->fh; |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 274 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 275 | req->in.h.opcode = FUSE_FLUSH; |
| 276 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 277 | req->in.numargs = 1; |
| 278 | req->in.args[0].size = sizeof(inarg); |
| 279 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 280 | req->force = 1; |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 281 | request_send(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 282 | err = req->out.h.error; |
| 283 | fuse_put_request(fc, req); |
| 284 | if (err == -ENOSYS) { |
| 285 | fc->no_flush = 1; |
| 286 | err = 0; |
| 287 | } |
| 288 | return err; |
| 289 | } |
| 290 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 291 | /* |
| 292 | * Wait for all pending writepages on the inode to finish. |
| 293 | * |
| 294 | * This is currently done by blocking further writes with FUSE_NOWRITE |
| 295 | * and waiting for all sent writes to complete. |
| 296 | * |
| 297 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage |
| 298 | * could conflict with truncation. |
| 299 | */ |
| 300 | static void fuse_sync_writes(struct inode *inode) |
| 301 | { |
| 302 | fuse_set_nowrite(inode); |
| 303 | fuse_release_nowrite(inode); |
| 304 | } |
| 305 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 306 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
| 307 | int isdir) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 308 | { |
| 309 | struct inode *inode = de->d_inode; |
| 310 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 311 | struct fuse_file *ff = file->private_data; |
| 312 | struct fuse_req *req; |
| 313 | struct fuse_fsync_in inarg; |
| 314 | int err; |
| 315 | |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 316 | if (is_bad_inode(inode)) |
| 317 | return -EIO; |
| 318 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 319 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 320 | return 0; |
| 321 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 322 | /* |
| 323 | * Start writeback against all dirty pages of the inode, then |
| 324 | * wait for all outstanding writes, before sending the FSYNC |
| 325 | * request. |
| 326 | */ |
| 327 | err = write_inode_now(inode, 0); |
| 328 | if (err) |
| 329 | return err; |
| 330 | |
| 331 | fuse_sync_writes(inode); |
| 332 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 333 | req = fuse_get_req(fc); |
| 334 | if (IS_ERR(req)) |
| 335 | return PTR_ERR(req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 336 | |
| 337 | memset(&inarg, 0, sizeof(inarg)); |
| 338 | inarg.fh = ff->fh; |
| 339 | inarg.fsync_flags = datasync ? 1 : 0; |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 340 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 341 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 342 | req->in.numargs = 1; |
| 343 | req->in.args[0].size = sizeof(inarg); |
| 344 | req->in.args[0].value = &inarg; |
| 345 | request_send(fc, req); |
| 346 | err = req->out.h.error; |
| 347 | fuse_put_request(fc, req); |
| 348 | if (err == -ENOSYS) { |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 349 | if (isdir) |
| 350 | fc->no_fsyncdir = 1; |
| 351 | else |
| 352 | fc->no_fsync = 1; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 353 | err = 0; |
| 354 | } |
| 355 | return err; |
| 356 | } |
| 357 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 358 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
| 359 | { |
| 360 | return fuse_fsync_common(file, de, datasync, 0); |
| 361 | } |
| 362 | |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 363 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 364 | struct inode *inode, loff_t pos, size_t count, int opcode) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 365 | { |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 366 | struct fuse_read_in *inarg = &req->misc.read_in; |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 367 | struct fuse_file *ff = file->private_data; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 368 | |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 369 | inarg->fh = ff->fh; |
| 370 | inarg->offset = pos; |
| 371 | inarg->size = count; |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 372 | inarg->flags = file->f_flags; |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 373 | req->in.h.opcode = opcode; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 374 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 375 | req->in.numargs = 1; |
| 376 | req->in.args[0].size = sizeof(struct fuse_read_in); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 377 | req->in.args[0].value = inarg; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 378 | req->out.argpages = 1; |
| 379 | req->out.argvar = 1; |
| 380 | req->out.numargs = 1; |
| 381 | req->out.args[0].size = count; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 384 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 385 | struct inode *inode, loff_t pos, size_t count, |
| 386 | fl_owner_t owner) |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 387 | { |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 388 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 389 | |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 390 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 391 | if (owner != NULL) { |
| 392 | struct fuse_read_in *inarg = &req->misc.read_in; |
| 393 | |
| 394 | inarg->read_flags |= FUSE_READ_LOCKOWNER; |
| 395 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); |
| 396 | } |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 397 | request_send(fc, req); |
| 398 | return req->out.args[0].size; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 401 | static int fuse_readpage(struct file *file, struct page *page) |
| 402 | { |
| 403 | struct inode *inode = page->mapping->host; |
| 404 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 405 | struct fuse_req *req; |
| 406 | int err; |
| 407 | |
| 408 | err = -EIO; |
| 409 | if (is_bad_inode(inode)) |
| 410 | goto out; |
| 411 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 412 | /* |
| 413 | * Page writeback can extend beyond the liftime of the |
| 414 | * page-cache page, so make sure we read a properly synced |
| 415 | * page. |
| 416 | */ |
| 417 | fuse_wait_on_page_writeback(inode, page->index); |
| 418 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 419 | req = fuse_get_req(fc); |
| 420 | err = PTR_ERR(req); |
| 421 | if (IS_ERR(req)) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 422 | goto out; |
| 423 | |
| 424 | req->out.page_zeroing = 1; |
| 425 | req->num_pages = 1; |
| 426 | req->pages[0] = page; |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 427 | fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE, |
| 428 | NULL); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 429 | err = req->out.h.error; |
| 430 | fuse_put_request(fc, req); |
| 431 | if (!err) |
| 432 | SetPageUptodate(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 433 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 434 | out: |
| 435 | unlock_page(page); |
| 436 | return err; |
| 437 | } |
| 438 | |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 439 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 440 | { |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 441 | int i; |
| 442 | |
| 443 | fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */ |
| 444 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 445 | for (i = 0; i < req->num_pages; i++) { |
| 446 | struct page *page = req->pages[i]; |
| 447 | if (!req->out.h.error) |
| 448 | SetPageUptodate(page); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 449 | else |
| 450 | SetPageError(page); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 451 | unlock_page(page); |
| 452 | } |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 453 | if (req->ff) |
| 454 | fuse_file_put(req->ff); |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 455 | fuse_put_request(fc, req); |
| 456 | } |
| 457 | |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 458 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, |
Miklos Szeredi | c1aa96a | 2006-01-16 22:14:46 -0800 | [diff] [blame] | 459 | struct inode *inode) |
| 460 | { |
| 461 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 462 | loff_t pos = page_offset(req->pages[0]); |
| 463 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; |
| 464 | req->out.page_zeroing = 1; |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 465 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 466 | if (fc->async_read) { |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 467 | struct fuse_file *ff = file->private_data; |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 468 | req->ff = fuse_file_get(ff); |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 469 | req->end = fuse_readpages_end; |
| 470 | request_send_background(fc, req); |
| 471 | } else { |
| 472 | request_send(fc, req); |
| 473 | fuse_readpages_end(fc, req); |
| 474 | } |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 477 | struct fuse_fill_data { |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 478 | struct fuse_req *req; |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 479 | struct file *file; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 480 | struct inode *inode; |
| 481 | }; |
| 482 | |
| 483 | static int fuse_readpages_fill(void *_data, struct page *page) |
| 484 | { |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 485 | struct fuse_fill_data *data = _data; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 486 | struct fuse_req *req = data->req; |
| 487 | struct inode *inode = data->inode; |
| 488 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 489 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 490 | fuse_wait_on_page_writeback(inode, page->index); |
| 491 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 492 | if (req->num_pages && |
| 493 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || |
| 494 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || |
| 495 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 496 | fuse_send_readpages(req, data->file, inode); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 497 | data->req = req = fuse_get_req(fc); |
| 498 | if (IS_ERR(req)) { |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 499 | unlock_page(page); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 500 | return PTR_ERR(req); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 501 | } |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 502 | } |
| 503 | req->pages[req->num_pages] = page; |
| 504 | req->num_pages ++; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int fuse_readpages(struct file *file, struct address_space *mapping, |
| 509 | struct list_head *pages, unsigned nr_pages) |
| 510 | { |
| 511 | struct inode *inode = mapping->host; |
| 512 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 513 | struct fuse_fill_data data; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 514 | int err; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 515 | |
Alexander Zarochentsev | 1d7ea73 | 2006-08-13 23:24:27 -0700 | [diff] [blame] | 516 | err = -EIO; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 517 | if (is_bad_inode(inode)) |
OGAWA Hirofumi | 2e99002 | 2006-11-02 22:07:09 -0800 | [diff] [blame] | 518 | goto out; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 519 | |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 520 | data.file = file; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 521 | data.inode = inode; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 522 | data.req = fuse_get_req(fc); |
Alexander Zarochentsev | 1d7ea73 | 2006-08-13 23:24:27 -0700 | [diff] [blame] | 523 | err = PTR_ERR(data.req); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 524 | if (IS_ERR(data.req)) |
OGAWA Hirofumi | 2e99002 | 2006-11-02 22:07:09 -0800 | [diff] [blame] | 525 | goto out; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 526 | |
| 527 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); |
Miklos Szeredi | d3406ff | 2006-04-10 22:54:49 -0700 | [diff] [blame] | 528 | if (!err) { |
| 529 | if (data.req->num_pages) |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 530 | fuse_send_readpages(data.req, file, inode); |
Miklos Szeredi | d3406ff | 2006-04-10 22:54:49 -0700 | [diff] [blame] | 531 | else |
| 532 | fuse_put_request(fc, data.req); |
| 533 | } |
OGAWA Hirofumi | 2e99002 | 2006-11-02 22:07:09 -0800 | [diff] [blame] | 534 | out: |
Alexander Zarochentsev | 1d7ea73 | 2006-08-13 23:24:27 -0700 | [diff] [blame] | 535 | return err; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 538 | static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 539 | unsigned long nr_segs, loff_t pos) |
| 540 | { |
| 541 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 542 | |
| 543 | if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) { |
| 544 | int err; |
| 545 | /* |
| 546 | * If trying to read past EOF, make sure the i_size |
| 547 | * attribute is up-to-date. |
| 548 | */ |
| 549 | err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL); |
| 550 | if (err) |
| 551 | return err; |
| 552 | } |
| 553 | |
| 554 | return generic_file_aio_read(iocb, iov, nr_segs, pos); |
| 555 | } |
| 556 | |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 557 | static void fuse_write_fill(struct fuse_req *req, struct file *file, |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 558 | struct fuse_file *ff, struct inode *inode, |
| 559 | loff_t pos, size_t count, int writepage) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 560 | { |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 561 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 562 | struct fuse_write_in *inarg = &req->misc.write.in; |
| 563 | struct fuse_write_out *outarg = &req->misc.write.out; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 564 | |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 565 | memset(inarg, 0, sizeof(struct fuse_write_in)); |
| 566 | inarg->fh = ff->fh; |
| 567 | inarg->offset = pos; |
| 568 | inarg->size = count; |
| 569 | inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0; |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 570 | inarg->flags = file ? file->f_flags : 0; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 571 | req->in.h.opcode = FUSE_WRITE; |
| 572 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 573 | req->in.argpages = 1; |
| 574 | req->in.numargs = 2; |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 575 | if (fc->minor < 9) |
| 576 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
| 577 | else |
| 578 | req->in.args[0].size = sizeof(struct fuse_write_in); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 579 | req->in.args[0].value = inarg; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 580 | req->in.args[1].size = count; |
| 581 | req->out.numargs = 1; |
| 582 | req->out.args[0].size = sizeof(struct fuse_write_out); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 583 | req->out.args[0].value = outarg; |
| 584 | } |
| 585 | |
| 586 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 587 | struct inode *inode, loff_t pos, size_t count, |
| 588 | fl_owner_t owner) |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 589 | { |
| 590 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 591 | fuse_write_fill(req, file, file->private_data, inode, pos, count, 0); |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 592 | if (owner != NULL) { |
| 593 | struct fuse_write_in *inarg = &req->misc.write.in; |
| 594 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
| 595 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); |
| 596 | } |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 597 | request_send(fc, req); |
Miklos Szeredi | b25e82e | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 598 | return req->misc.write.out.size; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 601 | static int fuse_write_begin(struct file *file, struct address_space *mapping, |
| 602 | loff_t pos, unsigned len, unsigned flags, |
| 603 | struct page **pagep, void **fsdata) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 604 | { |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 605 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
| 606 | |
| 607 | *pagep = __grab_cache_page(mapping, index); |
| 608 | if (!*pagep) |
| 609 | return -ENOMEM; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 610 | return 0; |
| 611 | } |
| 612 | |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 613 | static int fuse_buffered_write(struct file *file, struct inode *inode, |
| 614 | loff_t pos, unsigned count, struct page *page) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 615 | { |
| 616 | int err; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 617 | size_t nres; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 618 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 619 | struct fuse_inode *fi = get_fuse_inode(inode); |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 620 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 621 | struct fuse_req *req; |
| 622 | |
| 623 | if (is_bad_inode(inode)) |
| 624 | return -EIO; |
| 625 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 626 | /* |
| 627 | * Make sure writepages on the same page are not mixed up with |
| 628 | * plain writes. |
| 629 | */ |
| 630 | fuse_wait_on_page_writeback(inode, page->index); |
| 631 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 632 | req = fuse_get_req(fc); |
| 633 | if (IS_ERR(req)) |
| 634 | return PTR_ERR(req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 635 | |
| 636 | req->num_pages = 1; |
| 637 | req->pages[0] = page; |
| 638 | req->page_offset = offset; |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 639 | nres = fuse_send_write(req, file, inode, pos, count, NULL); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 640 | err = req->out.h.error; |
| 641 | fuse_put_request(fc, req); |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 642 | if (!err && !nres) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 643 | err = -EIO; |
| 644 | if (!err) { |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 645 | pos += nres; |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 646 | spin_lock(&fc->lock); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 647 | fi->attr_version = ++fc->attr_version; |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 648 | if (pos > inode->i_size) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 649 | i_size_write(inode, pos); |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 650 | spin_unlock(&fc->lock); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 651 | |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 652 | if (count == PAGE_CACHE_SIZE) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 653 | SetPageUptodate(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 654 | } |
| 655 | fuse_invalidate_attr(inode); |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 656 | return err ? err : nres; |
| 657 | } |
| 658 | |
| 659 | static int fuse_write_end(struct file *file, struct address_space *mapping, |
| 660 | loff_t pos, unsigned len, unsigned copied, |
| 661 | struct page *page, void *fsdata) |
| 662 | { |
| 663 | struct inode *inode = mapping->host; |
| 664 | int res = 0; |
| 665 | |
| 666 | if (copied) |
| 667 | res = fuse_buffered_write(file, inode, pos, copied, page); |
| 668 | |
| 669 | unlock_page(page); |
| 670 | page_cache_release(page); |
| 671 | return res; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 674 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
| 675 | { |
| 676 | unsigned i; |
| 677 | |
| 678 | for (i = 0; i < req->num_pages; i++) { |
| 679 | struct page *page = req->pages[i]; |
| 680 | if (write) |
| 681 | set_page_dirty_lock(page); |
| 682 | put_page(page); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, |
| 687 | unsigned nbytes, int write) |
| 688 | { |
| 689 | unsigned long user_addr = (unsigned long) buf; |
| 690 | unsigned offset = user_addr & ~PAGE_MASK; |
| 691 | int npages; |
| 692 | |
| 693 | /* This doesn't work with nfsd */ |
| 694 | if (!current->mm) |
| 695 | return -EPERM; |
| 696 | |
| 697 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); |
| 698 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Miklos Szeredi | 6ad84ac | 2006-01-06 00:19:42 -0800 | [diff] [blame] | 699 | npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 700 | down_read(¤t->mm->mmap_sem); |
| 701 | npages = get_user_pages(current, current->mm, user_addr, npages, write, |
| 702 | 0, req->pages, NULL); |
| 703 | up_read(¤t->mm->mmap_sem); |
| 704 | if (npages < 0) |
| 705 | return npages; |
| 706 | |
| 707 | req->num_pages = npages; |
| 708 | req->page_offset = offset; |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, |
| 713 | size_t count, loff_t *ppos, int write) |
| 714 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 715 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 716 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 717 | size_t nmax = write ? fc->max_write : fc->max_read; |
| 718 | loff_t pos = *ppos; |
| 719 | ssize_t res = 0; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 720 | struct fuse_req *req; |
| 721 | |
| 722 | if (is_bad_inode(inode)) |
| 723 | return -EIO; |
| 724 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 725 | req = fuse_get_req(fc); |
| 726 | if (IS_ERR(req)) |
| 727 | return PTR_ERR(req); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 728 | |
| 729 | while (count) { |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 730 | size_t nres; |
| 731 | size_t nbytes = min(count, nmax); |
| 732 | int err = fuse_get_user_pages(req, buf, nbytes, !write); |
| 733 | if (err) { |
| 734 | res = err; |
| 735 | break; |
| 736 | } |
Miklos Szeredi | 6ad84ac | 2006-01-06 00:19:42 -0800 | [diff] [blame] | 737 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
| 738 | nbytes = min(count, nbytes); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 739 | if (write) |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 740 | nres = fuse_send_write(req, file, inode, pos, nbytes, |
| 741 | current->files); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 742 | else |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 743 | nres = fuse_send_read(req, file, inode, pos, nbytes, |
| 744 | current->files); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 745 | fuse_release_user_pages(req, !write); |
| 746 | if (req->out.h.error) { |
| 747 | if (!res) |
| 748 | res = req->out.h.error; |
| 749 | break; |
| 750 | } else if (nres > nbytes) { |
| 751 | res = -EIO; |
| 752 | break; |
| 753 | } |
| 754 | count -= nres; |
| 755 | res += nres; |
| 756 | pos += nres; |
| 757 | buf += nres; |
| 758 | if (nres != nbytes) |
| 759 | break; |
Miklos Szeredi | 56cf34f | 2006-04-11 21:16:51 +0200 | [diff] [blame] | 760 | if (count) { |
| 761 | fuse_put_request(fc, req); |
| 762 | req = fuse_get_req(fc); |
| 763 | if (IS_ERR(req)) |
| 764 | break; |
| 765 | } |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 766 | } |
| 767 | fuse_put_request(fc, req); |
| 768 | if (res > 0) { |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 769 | if (write) { |
| 770 | spin_lock(&fc->lock); |
| 771 | if (pos > inode->i_size) |
| 772 | i_size_write(inode, pos); |
| 773 | spin_unlock(&fc->lock); |
| 774 | } |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 775 | *ppos = pos; |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 776 | } |
| 777 | fuse_invalidate_attr(inode); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 778 | |
| 779 | return res; |
| 780 | } |
| 781 | |
| 782 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
| 783 | size_t count, loff_t *ppos) |
| 784 | { |
| 785 | return fuse_direct_io(file, buf, count, ppos, 0); |
| 786 | } |
| 787 | |
| 788 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
| 789 | size_t count, loff_t *ppos) |
| 790 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 791 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 792 | ssize_t res; |
| 793 | /* Don't allow parallel writes to the same file */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 794 | mutex_lock(&inode->i_mutex); |
Miklos Szeredi | 889f784 | 2007-05-23 13:57:54 -0700 | [diff] [blame] | 795 | res = generic_write_checks(file, ppos, &count, 0); |
| 796 | if (!res) |
| 797 | res = fuse_direct_io(file, buf, count, ppos, 1); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 798 | mutex_unlock(&inode->i_mutex); |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 799 | return res; |
| 800 | } |
| 801 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 802 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 803 | { |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 804 | __free_page(req->pages[0]); |
| 805 | fuse_file_put(req->ff); |
| 806 | fuse_put_request(fc, req); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 809 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 810 | { |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 811 | struct inode *inode = req->inode; |
| 812 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 813 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; |
| 814 | |
| 815 | list_del(&req->writepages_entry); |
| 816 | dec_bdi_stat(bdi, BDI_WRITEBACK); |
| 817 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); |
| 818 | bdi_writeout_inc(bdi); |
| 819 | wake_up(&fi->page_waitq); |
| 820 | } |
| 821 | |
| 822 | /* Called under fc->lock, may release and reacquire it */ |
| 823 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) |
| 824 | { |
| 825 | struct fuse_inode *fi = get_fuse_inode(req->inode); |
| 826 | loff_t size = i_size_read(req->inode); |
| 827 | struct fuse_write_in *inarg = &req->misc.write.in; |
| 828 | |
| 829 | if (!fc->connected) |
| 830 | goto out_free; |
| 831 | |
| 832 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { |
| 833 | inarg->size = PAGE_CACHE_SIZE; |
| 834 | } else if (inarg->offset < size) { |
| 835 | inarg->size = size & (PAGE_CACHE_SIZE - 1); |
| 836 | } else { |
| 837 | /* Got truncated off completely */ |
| 838 | goto out_free; |
| 839 | } |
| 840 | |
| 841 | req->in.args[1].size = inarg->size; |
| 842 | fi->writectr++; |
| 843 | request_send_background_locked(fc, req); |
| 844 | return; |
| 845 | |
| 846 | out_free: |
| 847 | fuse_writepage_finish(fc, req); |
| 848 | spin_unlock(&fc->lock); |
| 849 | fuse_writepage_free(fc, req); |
| 850 | spin_lock(&fc->lock); |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * If fi->writectr is positive (no truncate or fsync going on) send |
| 855 | * all queued writepage requests. |
| 856 | * |
| 857 | * Called with fc->lock |
| 858 | */ |
| 859 | void fuse_flush_writepages(struct inode *inode) |
| 860 | { |
| 861 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 862 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 863 | struct fuse_req *req; |
| 864 | |
| 865 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { |
| 866 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); |
| 867 | list_del_init(&req->list); |
| 868 | fuse_send_writepage(fc, req); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) |
| 873 | { |
| 874 | struct inode *inode = req->inode; |
| 875 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 876 | |
| 877 | mapping_set_error(inode->i_mapping, req->out.h.error); |
| 878 | spin_lock(&fc->lock); |
| 879 | fi->writectr--; |
| 880 | fuse_writepage_finish(fc, req); |
| 881 | spin_unlock(&fc->lock); |
| 882 | fuse_writepage_free(fc, req); |
| 883 | } |
| 884 | |
| 885 | static int fuse_writepage_locked(struct page *page) |
| 886 | { |
| 887 | struct address_space *mapping = page->mapping; |
| 888 | struct inode *inode = mapping->host; |
| 889 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 890 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 891 | struct fuse_req *req; |
| 892 | struct fuse_file *ff; |
| 893 | struct page *tmp_page; |
| 894 | |
| 895 | set_page_writeback(page); |
| 896 | |
| 897 | req = fuse_request_alloc_nofs(); |
| 898 | if (!req) |
| 899 | goto err; |
| 900 | |
| 901 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
| 902 | if (!tmp_page) |
| 903 | goto err_free; |
| 904 | |
| 905 | spin_lock(&fc->lock); |
| 906 | BUG_ON(list_empty(&fi->write_files)); |
| 907 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); |
| 908 | req->ff = fuse_file_get(ff); |
| 909 | spin_unlock(&fc->lock); |
| 910 | |
| 911 | fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1); |
| 912 | |
| 913 | copy_highpage(tmp_page, page); |
| 914 | req->num_pages = 1; |
| 915 | req->pages[0] = tmp_page; |
| 916 | req->page_offset = 0; |
| 917 | req->end = fuse_writepage_end; |
| 918 | req->inode = inode; |
| 919 | |
| 920 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); |
| 921 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); |
| 922 | end_page_writeback(page); |
| 923 | |
| 924 | spin_lock(&fc->lock); |
| 925 | list_add(&req->writepages_entry, &fi->writepages); |
| 926 | list_add_tail(&req->list, &fi->queued_writes); |
| 927 | fuse_flush_writepages(inode); |
| 928 | spin_unlock(&fc->lock); |
| 929 | |
| 930 | return 0; |
| 931 | |
| 932 | err_free: |
| 933 | fuse_request_free(req); |
| 934 | err: |
| 935 | end_page_writeback(page); |
| 936 | return -ENOMEM; |
| 937 | } |
| 938 | |
| 939 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) |
| 940 | { |
| 941 | int err; |
| 942 | |
| 943 | err = fuse_writepage_locked(page); |
| 944 | unlock_page(page); |
| 945 | |
| 946 | return err; |
| 947 | } |
| 948 | |
| 949 | static int fuse_launder_page(struct page *page) |
| 950 | { |
| 951 | int err = 0; |
| 952 | if (clear_page_dirty_for_io(page)) { |
| 953 | struct inode *inode = page->mapping->host; |
| 954 | err = fuse_writepage_locked(page); |
| 955 | if (!err) |
| 956 | fuse_wait_on_page_writeback(inode, page->index); |
| 957 | } |
| 958 | return err; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Write back dirty pages now, because there may not be any suitable |
| 963 | * open files later |
| 964 | */ |
| 965 | static void fuse_vma_close(struct vm_area_struct *vma) |
| 966 | { |
| 967 | filemap_write_and_wait(vma->vm_file->f_mapping); |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Wait for writeback against this page to complete before allowing it |
| 972 | * to be marked dirty again, and hence written back again, possibly |
| 973 | * before the previous writepage completed. |
| 974 | * |
| 975 | * Block here, instead of in ->writepage(), so that the userspace fs |
| 976 | * can only block processes actually operating on the filesystem. |
| 977 | * |
| 978 | * Otherwise unprivileged userspace fs would be able to block |
| 979 | * unrelated: |
| 980 | * |
| 981 | * - page migration |
| 982 | * - sync(2) |
| 983 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER |
| 984 | */ |
| 985 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 986 | { |
| 987 | /* |
| 988 | * Don't use page->mapping as it may become NULL from a |
| 989 | * concurrent truncate. |
| 990 | */ |
| 991 | struct inode *inode = vma->vm_file->f_mapping->host; |
| 992 | |
| 993 | fuse_wait_on_page_writeback(inode, page->index); |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | static struct vm_operations_struct fuse_file_vm_ops = { |
| 998 | .close = fuse_vma_close, |
| 999 | .fault = filemap_fault, |
| 1000 | .page_mkwrite = fuse_page_mkwrite, |
| 1001 | }; |
| 1002 | |
| 1003 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 1004 | { |
| 1005 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { |
| 1006 | struct inode *inode = file->f_dentry->d_inode; |
| 1007 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1008 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1009 | struct fuse_file *ff = file->private_data; |
| 1010 | /* |
| 1011 | * file may be written through mmap, so chain it onto the |
| 1012 | * inodes's write_file list |
| 1013 | */ |
| 1014 | spin_lock(&fc->lock); |
| 1015 | if (list_empty(&ff->write_entry)) |
| 1016 | list_add(&ff->write_entry, &fi->write_files); |
| 1017 | spin_unlock(&fc->lock); |
| 1018 | } |
| 1019 | file_accessed(file); |
| 1020 | vma->vm_ops = &fuse_file_vm_ops; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1024 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
| 1025 | struct file_lock *fl) |
| 1026 | { |
| 1027 | switch (ffl->type) { |
| 1028 | case F_UNLCK: |
| 1029 | break; |
| 1030 | |
| 1031 | case F_RDLCK: |
| 1032 | case F_WRLCK: |
| 1033 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || |
| 1034 | ffl->end < ffl->start) |
| 1035 | return -EIO; |
| 1036 | |
| 1037 | fl->fl_start = ffl->start; |
| 1038 | fl->fl_end = ffl->end; |
| 1039 | fl->fl_pid = ffl->pid; |
| 1040 | break; |
| 1041 | |
| 1042 | default: |
| 1043 | return -EIO; |
| 1044 | } |
| 1045 | fl->fl_type = ffl->type; |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1050 | const struct file_lock *fl, int opcode, pid_t pid, |
| 1051 | int flock) |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1052 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 1053 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 1054 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1055 | struct fuse_file *ff = file->private_data; |
| 1056 | struct fuse_lk_in *arg = &req->misc.lk_in; |
| 1057 | |
| 1058 | arg->fh = ff->fh; |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 1059 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1060 | arg->lk.start = fl->fl_start; |
| 1061 | arg->lk.end = fl->fl_end; |
| 1062 | arg->lk.type = fl->fl_type; |
| 1063 | arg->lk.pid = pid; |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1064 | if (flock) |
| 1065 | arg->lk_flags |= FUSE_LK_FLOCK; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1066 | req->in.h.opcode = opcode; |
| 1067 | req->in.h.nodeid = get_node_id(inode); |
| 1068 | req->in.numargs = 1; |
| 1069 | req->in.args[0].size = sizeof(*arg); |
| 1070 | req->in.args[0].value = arg; |
| 1071 | } |
| 1072 | |
| 1073 | static int fuse_getlk(struct file *file, struct file_lock *fl) |
| 1074 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 1075 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1076 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1077 | struct fuse_req *req; |
| 1078 | struct fuse_lk_out outarg; |
| 1079 | int err; |
| 1080 | |
| 1081 | req = fuse_get_req(fc); |
| 1082 | if (IS_ERR(req)) |
| 1083 | return PTR_ERR(req); |
| 1084 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1085 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1086 | req->out.numargs = 1; |
| 1087 | req->out.args[0].size = sizeof(outarg); |
| 1088 | req->out.args[0].value = &outarg; |
| 1089 | request_send(fc, req); |
| 1090 | err = req->out.h.error; |
| 1091 | fuse_put_request(fc, req); |
| 1092 | if (!err) |
| 1093 | err = convert_fuse_file_lock(&outarg.lk, fl); |
| 1094 | |
| 1095 | return err; |
| 1096 | } |
| 1097 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1098 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1099 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 1100 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1101 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1102 | struct fuse_req *req; |
| 1103 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; |
| 1104 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; |
| 1105 | int err; |
| 1106 | |
| 1107 | /* Unlock on close is handled by the flush method */ |
| 1108 | if (fl->fl_flags & FL_CLOSE) |
| 1109 | return 0; |
| 1110 | |
| 1111 | req = fuse_get_req(fc); |
| 1112 | if (IS_ERR(req)) |
| 1113 | return PTR_ERR(req); |
| 1114 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1115 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1116 | request_send(fc, req); |
| 1117 | err = req->out.h.error; |
Miklos Szeredi | a4d27e7 | 2006-06-25 05:48:54 -0700 | [diff] [blame] | 1118 | /* locking is restartable */ |
| 1119 | if (err == -EINTR) |
| 1120 | err = -ERESTARTSYS; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1121 | fuse_put_request(fc, req); |
| 1122 | return err; |
| 1123 | } |
| 1124 | |
| 1125 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) |
| 1126 | { |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 1127 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1128 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1129 | int err; |
| 1130 | |
| 1131 | if (cmd == F_GETLK) { |
| 1132 | if (fc->no_lock) { |
Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 1133 | posix_test_lock(file, fl); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1134 | err = 0; |
| 1135 | } else |
| 1136 | err = fuse_getlk(file, fl); |
| 1137 | } else { |
| 1138 | if (fc->no_lock) |
| 1139 | err = posix_lock_file_wait(file, fl); |
| 1140 | else |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1141 | err = fuse_setlk(file, fl, 0); |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1142 | } |
| 1143 | return err; |
| 1144 | } |
| 1145 | |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1146 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
| 1147 | { |
| 1148 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1149 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1150 | int err; |
| 1151 | |
| 1152 | if (fc->no_lock) { |
| 1153 | err = flock_lock_file_wait(file, fl); |
| 1154 | } else { |
| 1155 | /* emulate flock with POSIX locks */ |
| 1156 | fl->fl_owner = (fl_owner_t) file; |
| 1157 | err = fuse_setlk(file, fl, 1); |
| 1158 | } |
| 1159 | |
| 1160 | return err; |
| 1161 | } |
| 1162 | |
Miklos Szeredi | b2d2272 | 2006-12-06 20:35:51 -0800 | [diff] [blame] | 1163 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
| 1164 | { |
| 1165 | struct inode *inode = mapping->host; |
| 1166 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1167 | struct fuse_req *req; |
| 1168 | struct fuse_bmap_in inarg; |
| 1169 | struct fuse_bmap_out outarg; |
| 1170 | int err; |
| 1171 | |
| 1172 | if (!inode->i_sb->s_bdev || fc->no_bmap) |
| 1173 | return 0; |
| 1174 | |
| 1175 | req = fuse_get_req(fc); |
| 1176 | if (IS_ERR(req)) |
| 1177 | return 0; |
| 1178 | |
| 1179 | memset(&inarg, 0, sizeof(inarg)); |
| 1180 | inarg.block = block; |
| 1181 | inarg.blocksize = inode->i_sb->s_blocksize; |
| 1182 | req->in.h.opcode = FUSE_BMAP; |
| 1183 | req->in.h.nodeid = get_node_id(inode); |
| 1184 | req->in.numargs = 1; |
| 1185 | req->in.args[0].size = sizeof(inarg); |
| 1186 | req->in.args[0].value = &inarg; |
| 1187 | req->out.numargs = 1; |
| 1188 | req->out.args[0].size = sizeof(outarg); |
| 1189 | req->out.args[0].value = &outarg; |
| 1190 | request_send(fc, req); |
| 1191 | err = req->out.h.error; |
| 1192 | fuse_put_request(fc, req); |
| 1193 | if (err == -ENOSYS) |
| 1194 | fc->no_bmap = 1; |
| 1195 | |
| 1196 | return err ? 0 : outarg.block; |
| 1197 | } |
| 1198 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1199 | static const struct file_operations fuse_file_operations = { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1200 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1201 | .read = do_sync_read, |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 1202 | .aio_read = fuse_file_aio_read, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1203 | .write = do_sync_write, |
| 1204 | .aio_write = generic_file_aio_write, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1205 | .mmap = fuse_file_mmap, |
| 1206 | .open = fuse_open, |
| 1207 | .flush = fuse_flush, |
| 1208 | .release = fuse_release, |
| 1209 | .fsync = fuse_fsync, |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1210 | .lock = fuse_file_lock, |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1211 | .flock = fuse_file_flock, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 1212 | .splice_read = generic_file_splice_read, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1213 | }; |
| 1214 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1215 | static const struct file_operations fuse_direct_io_file_operations = { |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 1216 | .llseek = generic_file_llseek, |
| 1217 | .read = fuse_direct_read, |
| 1218 | .write = fuse_direct_write, |
| 1219 | .open = fuse_open, |
| 1220 | .flush = fuse_flush, |
| 1221 | .release = fuse_release, |
| 1222 | .fsync = fuse_fsync, |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 1223 | .lock = fuse_file_lock, |
Miklos Szeredi | a9ff4f8 | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1224 | .flock = fuse_file_flock, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 1225 | /* no mmap and splice_read */ |
Miklos Szeredi | 413ef8c | 2005-09-09 13:10:35 -0700 | [diff] [blame] | 1226 | }; |
| 1227 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 1228 | static const struct address_space_operations fuse_file_aops = { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1229 | .readpage = fuse_readpage, |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 1230 | .writepage = fuse_writepage, |
| 1231 | .launder_page = fuse_launder_page, |
Nick Piggin | 5e6f58a | 2007-10-16 01:25:17 -0700 | [diff] [blame] | 1232 | .write_begin = fuse_write_begin, |
| 1233 | .write_end = fuse_write_end, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 1234 | .readpages = fuse_readpages, |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame^] | 1235 | .set_page_dirty = __set_page_dirty_nobuffers, |
Miklos Szeredi | b2d2272 | 2006-12-06 20:35:51 -0800 | [diff] [blame] | 1236 | .bmap = fuse_bmap, |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1237 | }; |
| 1238 | |
| 1239 | void fuse_init_file_inode(struct inode *inode) |
| 1240 | { |
Miklos Szeredi | 45323fb | 2005-09-09 13:10:37 -0700 | [diff] [blame] | 1241 | inode->i_fop = &fuse_file_operations; |
| 1242 | inode->i_data.a_ops = &fuse_file_aops; |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1243 | } |