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