Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1 | /* |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 7 | */ |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 8 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 9 | #include "fuse_i.h" |
| 10 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 12 | #include <linux/slab.h> |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 14 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 15 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 16 | #define PageUptodate(page) Page_Uptodate(page) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 17 | #define clear_page_dirty(page) ClearPageDirty(page) |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 18 | #endif |
Miklos Szeredi | bb4b974 | 2005-11-29 11:29:44 +0000 | [diff] [blame] | 19 | #ifndef KERNEL_2_6_11_PLUS |
| 20 | static inline loff_t page_offset(struct page *page) |
| 21 | { |
| 22 | return ((loff_t)page->index) << PAGE_CACHE_SHIFT; |
| 23 | } |
| 24 | #endif |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 25 | static struct file_operations fuse_direct_io_file_operations; |
| 26 | |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 27 | static int fuse_send_open(struct inode *inode, struct file *file, int isdir, |
| 28 | struct fuse_open_out *outargp) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 29 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 30 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 31 | struct fuse_open_in inarg; |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 32 | struct fuse_req *req; |
| 33 | int err; |
| 34 | |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 35 | req = fuse_get_req(fc); |
| 36 | if (IS_ERR(req)) |
| 37 | return PTR_ERR(req); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 38 | |
| 39 | memset(&inarg, 0, sizeof(inarg)); |
| 40 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 41 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
| 42 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 43 | req->in.numargs = 1; |
| 44 | req->in.args[0].size = sizeof(inarg); |
| 45 | req->in.args[0].value = &inarg; |
| 46 | req->out.numargs = 1; |
| 47 | req->out.args[0].size = sizeof(*outargp); |
| 48 | req->out.args[0].value = outargp; |
| 49 | request_send(fc, req); |
| 50 | err = req->out.h.error; |
| 51 | fuse_put_request(fc, req); |
| 52 | |
| 53 | return err; |
| 54 | } |
| 55 | |
| 56 | struct fuse_file *fuse_file_alloc(void) |
| 57 | { |
| 58 | struct fuse_file *ff; |
| 59 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); |
| 60 | if (ff) { |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 61 | ff->reserved_req = fuse_request_alloc(); |
| 62 | if (!ff->reserved_req) { |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 63 | kfree(ff); |
| 64 | ff = NULL; |
| 65 | } |
| 66 | } |
| 67 | return ff; |
| 68 | } |
| 69 | |
| 70 | void fuse_file_free(struct fuse_file *ff) |
| 71 | { |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 72 | fuse_request_free(ff->reserved_req); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 73 | kfree(ff); |
| 74 | } |
| 75 | |
| 76 | void fuse_finish_open(struct inode *inode, struct file *file, |
| 77 | struct fuse_file *ff, struct fuse_open_out *outarg) |
| 78 | { |
| 79 | if (outarg->open_flags & FOPEN_DIRECT_IO) |
| 80 | file->f_op = &fuse_direct_io_file_operations; |
| 81 | if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) |
| 82 | #ifdef KERNEL_2_6 |
| 83 | invalidate_inode_pages(inode->i_mapping); |
| 84 | #else |
| 85 | invalidate_inode_pages(inode); |
| 86 | #endif |
| 87 | ff->fh = outarg->fh; |
| 88 | file->private_data = ff; |
| 89 | } |
| 90 | |
| 91 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) |
| 92 | { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 93 | struct fuse_open_out outarg; |
| 94 | struct fuse_file *ff; |
Miklos Szeredi | f58cc61 | 2004-02-06 13:52:00 +0000 | [diff] [blame] | 95 | int err; |
| 96 | |
Miklos Szeredi | f45f3c1 | 2005-09-22 15:14:27 +0000 | [diff] [blame] | 97 | /* VFS checks this, but only _after_ ->open() */ |
| 98 | if (file->f_flags & O_DIRECT) |
| 99 | return -EINVAL; |
| 100 | |
Miklos Szeredi | f58cc61 | 2004-02-06 13:52:00 +0000 | [diff] [blame] | 101 | err = generic_file_open(inode, file); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 102 | if (err) |
Miklos Szeredi | f58cc61 | 2004-02-06 13:52:00 +0000 | [diff] [blame] | 103 | return err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 104 | |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 105 | /* If opening the root node, no lookup has been performed on |
| 106 | it, so the attributes must be refreshed */ |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 107 | if (get_node_id(inode) == FUSE_ROOT_ID) { |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 108 | err = fuse_do_getattr(inode); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 109 | if (err) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 110 | return err; |
| 111 | } |
| 112 | |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 113 | ff = fuse_file_alloc(); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 114 | if (!ff) |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 115 | return -ENOMEM; |
Miklos Szeredi | d3dd2d5 | 2004-06-22 18:46:02 +0000 | [diff] [blame] | 116 | |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 117 | err = fuse_send_open(inode, file, isdir, &outarg); |
| 118 | if (err) |
| 119 | fuse_file_free(ff); |
| 120 | else { |
| 121 | if (isdir) |
| 122 | outarg.open_flags &= ~FOPEN_DIRECT_IO; |
| 123 | fuse_finish_open(inode, file, ff, &outarg); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 126 | return err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 129 | struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, |
| 130 | int opcode) |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 131 | { |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 132 | struct fuse_req *req = ff->reserved_req; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 133 | struct fuse_release_in *inarg = &req->misc.release_in; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 134 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 135 | inarg->fh = ff->fh; |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 136 | inarg->flags = flags; |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 137 | req->in.h.opcode = opcode; |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 138 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 139 | req->in.numargs = 1; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 140 | req->in.args[0].size = sizeof(struct fuse_release_in); |
| 141 | req->in.args[0].value = inarg; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 142 | kfree(ff); |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 143 | |
| 144 | return req; |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) |
| 148 | { |
| 149 | struct fuse_file *ff = file->private_data; |
| 150 | if (ff) { |
| 151 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 152 | struct fuse_req *req; |
| 153 | |
| 154 | req = fuse_release_fill(ff, get_node_id(inode), file->f_flags, |
| 155 | isdir ? FUSE_RELEASEDIR : FUSE_RELEASE); |
| 156 | |
| 157 | /* Hold vfsmount and dentry until release is finished */ |
| 158 | req->vfsmount = mntget(file->f_vfsmnt); |
| 159 | req->dentry = dget(file->f_dentry); |
| 160 | request_send_background(fc, req); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 161 | } |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame] | 162 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 163 | /* Return value is ignored by VFS */ |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame] | 164 | return 0; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 167 | static int fuse_open(struct inode *inode, struct file *file) |
| 168 | { |
| 169 | return fuse_open_common(inode, file, 0); |
| 170 | } |
| 171 | |
| 172 | static int fuse_release(struct inode *inode, struct file *file) |
| 173 | { |
| 174 | return fuse_release_common(inode, file, 0); |
| 175 | } |
| 176 | |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 177 | /* |
| 178 | * Scramble the ID space with XTEA, so that the value of the files_struct |
| 179 | * pointer is not exposed to userspace. |
| 180 | */ |
| 181 | static u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
| 182 | { |
| 183 | u32 *k = fc->scramble_key; |
| 184 | u64 v = (unsigned long) id; |
| 185 | u32 v0 = v; |
| 186 | u32 v1 = v >> 32; |
| 187 | u32 sum = 0; |
| 188 | int i; |
| 189 | |
| 190 | for (i = 0; i < 32; i++) { |
| 191 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); |
| 192 | sum += 0x9E3779B9; |
| 193 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); |
| 194 | } |
| 195 | |
| 196 | return (u64) v0 + ((u64) v1 << 32); |
| 197 | } |
| 198 | |
| 199 | #ifdef KERNEL_2_6_18_PLUS |
| 200 | static int fuse_flush(struct file *file, fl_owner_t id) |
| 201 | #else |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 202 | static int fuse_flush(struct file *file) |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 203 | #endif |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 204 | { |
| 205 | struct inode *inode = file->f_dentry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 206 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 207 | struct fuse_file *ff = file->private_data; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 208 | struct fuse_req *req; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 209 | struct fuse_flush_in inarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 210 | int err; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 211 | |
Miklos Szeredi | c53cddd | 2005-12-07 12:57:59 +0000 | [diff] [blame] | 212 | if (is_bad_inode(inode)) |
| 213 | return -EIO; |
| 214 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 215 | if (fc->no_flush) |
| 216 | return 0; |
| 217 | |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 218 | req = fuse_get_req_nofail(fc, file); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 219 | memset(&inarg, 0, sizeof(inarg)); |
| 220 | inarg.fh = ff->fh; |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 221 | #ifdef KERNEL_2_6_18_PLUS |
| 222 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
| 223 | #else |
| 224 | inarg.lock_owner = fuse_lock_owner_id(fc, NULL); |
| 225 | #endif |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 226 | req->in.h.opcode = FUSE_FLUSH; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 227 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 228 | req->in.numargs = 1; |
| 229 | req->in.args[0].size = sizeof(inarg); |
| 230 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 231 | req->force = 1; |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 232 | request_send(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 233 | err = req->out.h.error; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 234 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 235 | if (err == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 236 | fc->no_flush = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 237 | err = 0; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 238 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 239 | return err; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 242 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
| 243 | int isdir) |
Miklos Szeredi | e4cf733 | 2003-12-12 11:53:31 +0000 | [diff] [blame] | 244 | { |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 245 | struct inode *inode = de->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 246 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 247 | struct fuse_file *ff = file->private_data; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 248 | struct fuse_req *req; |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 249 | struct fuse_fsync_in inarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 250 | int err; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 251 | |
Miklos Szeredi | c53cddd | 2005-12-07 12:57:59 +0000 | [diff] [blame] | 252 | if (is_bad_inode(inode)) |
| 253 | return -EIO; |
| 254 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 255 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 256 | return 0; |
| 257 | |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 258 | req = fuse_get_req(fc); |
| 259 | if (IS_ERR(req)) |
| 260 | return PTR_ERR(req); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 261 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 262 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 263 | inarg.fh = ff->fh; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 264 | inarg.fsync_flags = datasync ? 1 : 0; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 265 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 266 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 267 | req->in.numargs = 1; |
| 268 | req->in.args[0].size = sizeof(inarg); |
| 269 | req->in.args[0].value = &inarg; |
| 270 | request_send(fc, req); |
| 271 | err = req->out.h.error; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 272 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 273 | if (err == -ENOSYS) { |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 274 | if (isdir) |
| 275 | fc->no_fsyncdir = 1; |
| 276 | else |
| 277 | fc->no_fsync = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 278 | err = 0; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 279 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 280 | return err; |
Miklos Szeredi | e4cf733 | 2003-12-12 11:53:31 +0000 | [diff] [blame] | 281 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 282 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 283 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
| 284 | { |
| 285 | return fuse_fsync_common(file, de, datasync, 0); |
| 286 | } |
| 287 | |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 288 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
| 289 | struct inode *inode, loff_t pos, size_t count, int opcode) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 290 | { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 291 | struct fuse_file *ff = file->private_data; |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 292 | struct fuse_read_in *inarg = &req->misc.read_in; |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 293 | |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 294 | inarg->fh = ff->fh; |
| 295 | inarg->offset = pos; |
| 296 | inarg->size = count; |
| 297 | req->in.h.opcode = opcode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 298 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 299 | req->in.numargs = 1; |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 300 | req->in.args[0].size = sizeof(struct fuse_read_in); |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 301 | req->in.args[0].value = inarg; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 302 | req->out.argpages = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 303 | req->out.argvar = 1; |
| 304 | req->out.numargs = 1; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 305 | req->out.args[0].size = count; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 308 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
| 309 | struct inode *inode, loff_t pos, size_t count) |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 310 | { |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 311 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 312 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
| 313 | request_send(fc, req); |
| 314 | return req->out.args[0].size; |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 317 | static int fuse_readpage(struct file *file, struct page *page) |
| 318 | { |
| 319 | struct inode *inode = page->mapping->host; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 320 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | c53cddd | 2005-12-07 12:57:59 +0000 | [diff] [blame] | 321 | struct fuse_req *req; |
| 322 | int err; |
| 323 | |
| 324 | err = -EIO; |
| 325 | if (is_bad_inode(inode)) |
| 326 | goto out; |
| 327 | |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 328 | req = fuse_get_req(fc); |
| 329 | err = PTR_ERR(req); |
| 330 | if (IS_ERR(req)) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 331 | goto out; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 332 | |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 333 | req->out.page_zeroing = 1; |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 334 | req->num_pages = 1; |
| 335 | req->pages[0] = page; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 336 | fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 337 | err = req->out.h.error; |
| 338 | fuse_put_request(fc, req); |
| 339 | if (!err) |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 340 | SetPageUptodate(page); |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 341 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 342 | out: |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 343 | unlock_page(page); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 344 | return err; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 347 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 348 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 349 | { |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 350 | int i; |
| 351 | |
| 352 | fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */ |
| 353 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 354 | for (i = 0; i < req->num_pages; i++) { |
| 355 | struct page *page = req->pages[i]; |
| 356 | if (!req->out.h.error) |
| 357 | SetPageUptodate(page); |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 358 | else |
| 359 | SetPageError(page); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 360 | unlock_page(page); |
| 361 | } |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 362 | fuse_put_request(fc, req); |
| 363 | } |
| 364 | |
| 365 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, |
| 366 | struct inode *inode) |
| 367 | { |
| 368 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 369 | loff_t pos = page_offset(req->pages[0]); |
| 370 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; |
| 371 | req->out.page_zeroing = 1; |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 372 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 373 | if (fc->async_read) { |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 374 | get_file(file); |
| 375 | req->file = file; |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 376 | req->end = fuse_readpages_end; |
| 377 | request_send_background(fc, req); |
| 378 | } else { |
| 379 | request_send(fc, req); |
| 380 | fuse_readpages_end(fc, req); |
| 381 | } |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 384 | struct fuse_readpages_data { |
| 385 | struct fuse_req *req; |
| 386 | struct file *file; |
| 387 | struct inode *inode; |
| 388 | }; |
| 389 | |
| 390 | static int fuse_readpages_fill(void *_data, struct page *page) |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 391 | { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 392 | struct fuse_readpages_data *data = _data; |
| 393 | struct fuse_req *req = data->req; |
| 394 | struct inode *inode = data->inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 395 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 63d3c40 | 2006-08-18 16:38:08 +0000 | [diff] [blame^] | 396 | int err; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 397 | |
Miklos Szeredi | 63d3c40 | 2006-08-18 16:38:08 +0000 | [diff] [blame^] | 398 | if (req && req->num_pages && |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 399 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 400 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 401 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 402 | fuse_send_readpages(req, data->file, inode); |
Miklos Szeredi | 63d3c40 | 2006-08-18 16:38:08 +0000 | [diff] [blame^] | 403 | req = NULL; |
| 404 | } |
| 405 | if (!req) { |
| 406 | err = -EIO; |
| 407 | if (is_bad_inode(inode)) |
| 408 | goto out_unlock_page; |
| 409 | |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 410 | data->req = req = fuse_get_req(fc); |
Miklos Szeredi | 63d3c40 | 2006-08-18 16:38:08 +0000 | [diff] [blame^] | 411 | err = PTR_ERR(req); |
| 412 | if (IS_ERR(req)) |
| 413 | goto out_unlock_page; |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 414 | } |
| 415 | req->pages[req->num_pages] = page; |
| 416 | req->num_pages ++; |
| 417 | return 0; |
Miklos Szeredi | 63d3c40 | 2006-08-18 16:38:08 +0000 | [diff] [blame^] | 418 | |
| 419 | out_unlock_page: |
| 420 | unlock_page(page); |
| 421 | return err; |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static int fuse_readpages(struct file *file, struct address_space *mapping, |
| 425 | struct list_head *pages, unsigned nr_pages) |
| 426 | { |
| 427 | struct inode *inode = mapping->host; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 428 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 429 | struct fuse_readpages_data data; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 430 | int err; |
Miklos Szeredi | c53cddd | 2005-12-07 12:57:59 +0000 | [diff] [blame] | 431 | |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 432 | data.file = file; |
| 433 | data.inode = inode; |
Miklos Szeredi | 63d3c40 | 2006-08-18 16:38:08 +0000 | [diff] [blame^] | 434 | data.req = NULL; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 435 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 436 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 437 | if (!err) { |
| 438 | if (data.req->num_pages) |
| 439 | fuse_send_readpages(data.req, file, inode); |
| 440 | else |
| 441 | fuse_put_request(fc, data.req); |
| 442 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 443 | return err; |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 444 | } |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 445 | #else /* KERNEL_2_6 */ |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 446 | #define FUSE_BLOCK_SHIFT 16 |
| 447 | #define FUSE_BLOCK_SIZE (1UL << FUSE_BLOCK_SHIFT) |
| 448 | #define FUSE_BLOCK_MASK (~(FUSE_BLOCK_SIZE-1)) |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 449 | #if (1UL << (FUSE_BLOCK_SHIFT - PAGE_CACHE_SHIFT)) > FUSE_MAX_PAGES_PER_REQ |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 450 | #error FUSE_BLOCK_SHIFT too large |
| 451 | #endif |
| 452 | |
| 453 | static int fuse_is_block_uptodate(struct inode *inode, unsigned start, |
| 454 | unsigned end) |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 455 | { |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 456 | int index; |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 457 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 458 | for (index = start; index < end; index++) { |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 459 | struct page *page = find_get_page(inode->i_mapping, index); |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 460 | if (!page) |
| 461 | return 0; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 462 | if (!PageUptodate(page)) { |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 463 | page_cache_release(page); |
| 464 | return 0; |
| 465 | } |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 466 | page_cache_release(page); |
| 467 | } |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 468 | return 1; |
| 469 | } |
| 470 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 471 | static int fuse_file_read_block(struct fuse_req *req, struct file *file, |
| 472 | struct inode *inode, unsigned start, |
| 473 | unsigned end) |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 474 | { |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 475 | loff_t pos; |
| 476 | size_t count; |
| 477 | int index; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 478 | int err = -EBUSY; |
Michael Grigoriev | 37b3f3b | 2003-11-09 15:33:24 +0000 | [diff] [blame] | 479 | int i; |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 480 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 481 | for (index = start; index < end; index++) { |
| 482 | struct page *page = grab_cache_page(inode->i_mapping, index); |
Michael Grigoriev | 37b3f3b | 2003-11-09 15:33:24 +0000 | [diff] [blame] | 483 | if (!page) |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 484 | goto out; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 485 | if (PageUptodate(page)) { |
| 486 | unlock_page(page); |
| 487 | page_cache_release(page); |
| 488 | page = NULL; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 489 | } |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 490 | req->pages[req->num_pages++] = page; |
| 491 | } |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 492 | pos = (loff_t) start << PAGE_CACHE_SHIFT; |
| 493 | count = req->num_pages << PAGE_CACHE_SHIFT; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 494 | fuse_send_read(req, file, inode, pos, count); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 495 | err = req->out.h.error; |
| 496 | out: |
| 497 | for (i = 0; i < req->num_pages; i++) { |
| 498 | struct page *page = req->pages[i]; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 499 | if (page) { |
| 500 | if (!err) |
| 501 | SetPageUptodate(page); |
| 502 | unlock_page(page); |
| 503 | page_cache_release(page); |
| 504 | } |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 505 | } |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 506 | return err; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 507 | } |
Miklos Szeredi | 36ca556 | 2003-11-03 19:32:14 +0000 | [diff] [blame] | 508 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 509 | static int fuse_file_bigread(struct file *file, struct inode *inode, |
| 510 | loff_t pos, size_t count) |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 511 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 512 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 513 | unsigned starti; |
| 514 | unsigned endi; |
| 515 | unsigned nexti; |
| 516 | struct fuse_req *req; |
| 517 | loff_t size = i_size_read(inode); |
| 518 | loff_t end = (pos + count + FUSE_BLOCK_SIZE - 1) & FUSE_BLOCK_MASK; |
| 519 | end = min(end, size); |
| 520 | if (end <= pos) |
| 521 | return 0; |
| 522 | |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 523 | starti = (pos & FUSE_BLOCK_MASK) >> PAGE_CACHE_SHIFT; |
| 524 | endi = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 525 | |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 526 | req = fuse_get_req(fc); |
| 527 | if (IS_ERR(req)) |
| 528 | return PTR_ERR(req); |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 529 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 530 | for (; starti < endi; starti = nexti) { |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 531 | nexti = starti + (FUSE_BLOCK_SIZE >> PAGE_CACHE_SHIFT); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 532 | nexti = min(nexti, endi); |
| 533 | if (!fuse_is_block_uptodate(inode, starti, nexti)) { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 534 | if (fuse_file_read_block(req, file, inode, starti, nexti)) |
| 535 | break; |
| 536 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 537 | fuse_reset_request(req); |
| 538 | } |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 539 | } |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 540 | fuse_put_request(fc, req); |
| 541 | return 0; |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 542 | } |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 543 | #endif /* KERNEL_2_6 */ |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 544 | |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 545 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 546 | struct inode *inode, loff_t pos, size_t count) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 547 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 548 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 549 | struct fuse_file *ff = file->private_data; |
| 550 | struct fuse_write_in inarg; |
| 551 | struct fuse_write_out outarg; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 552 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 553 | memset(&inarg, 0, sizeof(struct fuse_write_in)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 554 | inarg.fh = ff->fh; |
| 555 | inarg.offset = pos; |
| 556 | inarg.size = count; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 557 | req->in.h.opcode = FUSE_WRITE; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 558 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 559 | req->in.argpages = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 560 | req->in.numargs = 2; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 561 | req->in.args[0].size = sizeof(struct fuse_write_in); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 562 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 563 | req->in.args[1].size = count; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 564 | req->out.numargs = 1; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 565 | req->out.args[0].size = sizeof(struct fuse_write_out); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 566 | req->out.args[0].value = &outarg; |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 567 | request_send(fc, req); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 568 | return outarg.size; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 571 | static int fuse_prepare_write(struct file *file, struct page *page, |
| 572 | unsigned offset, unsigned to) |
| 573 | { |
| 574 | /* No op */ |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int fuse_commit_write(struct file *file, struct page *page, |
| 579 | unsigned offset, unsigned to) |
| 580 | { |
| 581 | int err; |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 582 | size_t nres; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 583 | unsigned count = to - offset; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 584 | struct inode *inode = page->mapping->host; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 585 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 586 | loff_t pos = page_offset(page) + offset; |
Miklos Szeredi | 044da2e | 2005-12-06 17:59:55 +0000 | [diff] [blame] | 587 | struct fuse_req *req; |
| 588 | |
Miklos Szeredi | c53cddd | 2005-12-07 12:57:59 +0000 | [diff] [blame] | 589 | if (is_bad_inode(inode)) |
| 590 | return -EIO; |
| 591 | |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 592 | req = fuse_get_req(fc); |
| 593 | if (IS_ERR(req)) |
| 594 | return PTR_ERR(req); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 595 | |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 596 | req->num_pages = 1; |
| 597 | req->pages[0] = page; |
| 598 | req->page_offset = offset; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 599 | nres = fuse_send_write(req, file, inode, pos, count); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 600 | err = req->out.h.error; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 601 | fuse_put_request(fc, req); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 602 | if (!err && nres != count) |
| 603 | err = -EIO; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 604 | if (!err) { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 605 | pos += count; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 606 | if (pos > i_size_read(inode)) |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 607 | i_size_write(inode, pos); |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 608 | |
| 609 | if (offset == 0 && to == PAGE_CACHE_SIZE) { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 610 | clear_page_dirty(page); |
| 611 | SetPageUptodate(page); |
| 612 | } |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 613 | } |
| 614 | fuse_invalidate_attr(inode); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 615 | return err; |
| 616 | } |
| 617 | |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 618 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
| 619 | { |
| 620 | unsigned i; |
| 621 | |
| 622 | for (i = 0; i < req->num_pages; i++) { |
| 623 | struct page *page = req->pages[i]; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 624 | if (write) |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 625 | set_page_dirty_lock(page); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 626 | put_page(page); |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
| 630 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, |
| 631 | unsigned nbytes, int write) |
| 632 | { |
| 633 | unsigned long user_addr = (unsigned long) buf; |
| 634 | unsigned offset = user_addr & ~PAGE_MASK; |
| 635 | int npages; |
| 636 | |
Miklos Szeredi | 89814a1 | 2005-04-08 16:39:09 +0000 | [diff] [blame] | 637 | /* This doesn't work with nfsd */ |
| 638 | if (!current->mm) |
| 639 | return -EPERM; |
| 640 | |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 641 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); |
| 642 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Miklos Szeredi | 1f6a534 | 2006-01-06 13:40:07 +0000 | [diff] [blame] | 643 | npages = max(npages, 1); |
| 644 | npages = min(npages, FUSE_MAX_PAGES_PER_REQ); |
Miklos Szeredi | c45d1a6 | 2004-12-07 20:13:47 +0000 | [diff] [blame] | 645 | down_read(¤t->mm->mmap_sem); |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 646 | npages = get_user_pages(current, current->mm, user_addr, npages, write, |
| 647 | 0, req->pages, NULL); |
Miklos Szeredi | c45d1a6 | 2004-12-07 20:13:47 +0000 | [diff] [blame] | 648 | up_read(¤t->mm->mmap_sem); |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 649 | if (npages < 0) |
| 650 | return npages; |
| 651 | |
| 652 | req->num_pages = npages; |
| 653 | req->page_offset = offset; |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, |
| 658 | size_t count, loff_t *ppos, int write) |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 659 | { |
| 660 | struct inode *inode = file->f_dentry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 661 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | eab72ef | 2005-03-31 19:59:12 +0000 | [diff] [blame] | 662 | size_t nmax = write ? fc->max_write : fc->max_read; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 663 | loff_t pos = *ppos; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 664 | ssize_t res = 0; |
Miklos Szeredi | c53cddd | 2005-12-07 12:57:59 +0000 | [diff] [blame] | 665 | struct fuse_req *req; |
| 666 | |
| 667 | if (is_bad_inode(inode)) |
| 668 | return -EIO; |
| 669 | |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 670 | req = fuse_get_req(fc); |
| 671 | if (IS_ERR(req)) |
| 672 | return PTR_ERR(req); |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 673 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 674 | while (count) { |
Miklos Szeredi | eab72ef | 2005-03-31 19:59:12 +0000 | [diff] [blame] | 675 | size_t nres; |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 676 | size_t nbytes = min(count, nmax); |
| 677 | int err = fuse_get_user_pages(req, buf, nbytes, !write); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 678 | if (err) { |
| 679 | res = err; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 680 | break; |
| 681 | } |
Miklos Szeredi | 154ffe2 | 2005-12-15 16:41:20 +0000 | [diff] [blame] | 682 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
| 683 | nbytes = min(count, nbytes); |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 684 | if (write) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 685 | nres = fuse_send_write(req, file, inode, pos, nbytes); |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 686 | else |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 687 | nres = fuse_send_read(req, file, inode, pos, nbytes); |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 688 | fuse_release_user_pages(req, !write); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 689 | if (req->out.h.error) { |
| 690 | if (!res) |
| 691 | res = req->out.h.error; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 692 | break; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 693 | } else if (nres > nbytes) { |
| 694 | res = -EIO; |
| 695 | break; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 696 | } |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 697 | count -= nres; |
| 698 | res += nres; |
| 699 | pos += nres; |
| 700 | buf += nres; |
| 701 | if (nres != nbytes) |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 702 | break; |
Miklos Szeredi | 7a2814c | 2006-04-12 10:41:50 +0000 | [diff] [blame] | 703 | if (count) { |
| 704 | fuse_put_request(fc, req); |
| 705 | req = fuse_get_req(fc); |
| 706 | if (IS_ERR(req)) |
| 707 | break; |
| 708 | } |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 709 | } |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 710 | fuse_put_request(fc, req); |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 711 | if (res > 0) { |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 712 | if (write && pos > i_size_read(inode)) |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 713 | i_size_write(inode, pos); |
| 714 | *ppos = pos; |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 715 | } |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 716 | fuse_invalidate_attr(inode); |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 717 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 718 | return res; |
| 719 | } |
| 720 | |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 721 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
| 722 | size_t count, loff_t *ppos) |
| 723 | { |
| 724 | return fuse_direct_io(file, buf, count, ppos, 0); |
| 725 | } |
| 726 | |
| 727 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
| 728 | size_t count, loff_t *ppos) |
| 729 | { |
| 730 | struct inode *inode = file->f_dentry->d_inode; |
| 731 | ssize_t res; |
| 732 | /* Don't allow parallel writes to the same file */ |
Miklos Szeredi | e089b71 | 2006-01-17 12:42:39 +0000 | [diff] [blame] | 733 | #ifdef KERNEL_2_6_16_PLUS |
| 734 | mutex_lock(&inode->i_mutex); |
| 735 | res = fuse_direct_io(file, buf, count, ppos, 1); |
| 736 | mutex_unlock(&inode->i_mutex); |
| 737 | #else |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 738 | down(&inode->i_sem); |
| 739 | res = fuse_direct_io(file, buf, count, ppos, 1); |
| 740 | up(&inode->i_sem); |
Miklos Szeredi | e089b71 | 2006-01-17 12:42:39 +0000 | [diff] [blame] | 741 | #endif |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 742 | return res; |
| 743 | } |
| 744 | |
| 745 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 746 | static ssize_t fuse_file_read(struct file *file, char __user *buf, |
| 747 | size_t count, loff_t *ppos) |
| 748 | { |
| 749 | struct inode *inode = file->f_dentry->d_inode; |
| 750 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 5c7e63e | 2004-12-01 23:31:03 +0000 | [diff] [blame] | 751 | |
Miklos Szeredi | 23f49a0 | 2006-01-06 13:04:46 +0000 | [diff] [blame] | 752 | if (is_bad_inode(inode)) |
Miklos Szeredi | c53cddd | 2005-12-07 12:57:59 +0000 | [diff] [blame] | 753 | return -EIO; |
| 754 | |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 755 | if (fc->flags & FUSE_LARGE_READ) { |
| 756 | int res; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 757 | down(&inode->i_sem); |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 758 | res = fuse_file_bigread(file, inode, *ppos, count); |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 759 | up(&inode->i_sem); |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 760 | if (res) |
| 761 | return res; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 762 | } |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 763 | return generic_file_read(file, buf, count, ppos); |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 764 | } |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 765 | #endif |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 766 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 767 | { |
Miklos Szeredi | 2a92727 | 2005-01-07 11:14:15 +0000 | [diff] [blame] | 768 | if ((vma->vm_flags & VM_SHARED)) { |
| 769 | if ((vma->vm_flags & VM_WRITE)) |
| 770 | return -ENODEV; |
| 771 | else |
| 772 | vma->vm_flags &= ~VM_MAYWRITE; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 773 | } |
Miklos Szeredi | 2a92727 | 2005-01-07 11:14:15 +0000 | [diff] [blame] | 774 | return generic_file_mmap(file, vma); |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Miklos Szeredi | 2a92727 | 2005-01-07 11:14:15 +0000 | [diff] [blame] | 777 | #ifdef KERNEL_2_6 |
| 778 | static int fuse_set_page_dirty(struct page *page) |
| 779 | { |
| 780 | printk("fuse_set_page_dirty: should not happen\n"); |
| 781 | dump_stack(); |
| 782 | return 0; |
| 783 | } |
| 784 | #endif |
| 785 | |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 786 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
| 787 | struct file_lock *fl) |
| 788 | { |
| 789 | switch (ffl->type) { |
| 790 | case F_UNLCK: |
| 791 | break; |
| 792 | |
| 793 | case F_RDLCK: |
| 794 | case F_WRLCK: |
| 795 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || |
| 796 | ffl->end < ffl->start) |
| 797 | return -EIO; |
| 798 | |
| 799 | fl->fl_start = ffl->start; |
| 800 | fl->fl_end = ffl->end; |
| 801 | fl->fl_pid = ffl->pid; |
| 802 | break; |
| 803 | |
| 804 | default: |
| 805 | return -EIO; |
| 806 | } |
| 807 | fl->fl_type = ffl->type; |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, |
| 812 | const struct file_lock *fl, int opcode, pid_t pid) |
| 813 | { |
| 814 | struct inode *inode = file->f_dentry->d_inode; |
| 815 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 816 | struct fuse_file *ff = file->private_data; |
| 817 | struct fuse_lk_in *arg = &req->misc.lk_in; |
| 818 | |
| 819 | arg->fh = ff->fh; |
| 820 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
| 821 | arg->lk.start = fl->fl_start; |
| 822 | arg->lk.end = fl->fl_end; |
| 823 | arg->lk.type = fl->fl_type; |
| 824 | arg->lk.pid = pid; |
| 825 | req->in.h.opcode = opcode; |
| 826 | req->in.h.nodeid = get_node_id(inode); |
| 827 | req->in.numargs = 1; |
| 828 | req->in.args[0].size = sizeof(*arg); |
| 829 | req->in.args[0].value = arg; |
| 830 | } |
| 831 | |
| 832 | static int fuse_getlk(struct file *file, struct file_lock *fl) |
| 833 | { |
| 834 | struct inode *inode = file->f_dentry->d_inode; |
| 835 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 836 | struct fuse_req *req; |
| 837 | struct fuse_lk_out outarg; |
| 838 | int err; |
| 839 | |
| 840 | req = fuse_get_req(fc); |
| 841 | if (IS_ERR(req)) |
| 842 | return PTR_ERR(req); |
| 843 | |
| 844 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0); |
| 845 | req->out.numargs = 1; |
| 846 | req->out.args[0].size = sizeof(outarg); |
| 847 | req->out.args[0].value = &outarg; |
| 848 | request_send(fc, req); |
| 849 | err = req->out.h.error; |
| 850 | fuse_put_request(fc, req); |
| 851 | if (!err) |
| 852 | err = convert_fuse_file_lock(&outarg.lk, fl); |
| 853 | |
| 854 | return err; |
| 855 | } |
| 856 | |
| 857 | static int fuse_setlk(struct file *file, struct file_lock *fl) |
| 858 | { |
| 859 | struct inode *inode = file->f_dentry->d_inode; |
| 860 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 861 | struct fuse_req *req; |
| 862 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; |
| 863 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; |
| 864 | int err; |
| 865 | |
| 866 | #ifdef KERNEL_2_6_18_PLUS |
| 867 | /* Unlock on close is handled by the flush method */ |
| 868 | if (fl->fl_flags & FL_CLOSE) |
| 869 | return 0; |
| 870 | #endif |
| 871 | |
| 872 | req = fuse_get_req(fc); |
| 873 | if (IS_ERR(req)) |
| 874 | return PTR_ERR(req); |
| 875 | |
| 876 | fuse_lk_fill(req, file, fl, opcode, pid); |
| 877 | request_send(fc, req); |
| 878 | err = req->out.h.error; |
| 879 | /* locking is restartable */ |
| 880 | if (err == -EINTR) |
| 881 | err = -ERESTARTSYS; |
| 882 | fuse_put_request(fc, req); |
| 883 | return err; |
| 884 | } |
| 885 | |
| 886 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) |
| 887 | { |
| 888 | struct inode *inode = file->f_dentry->d_inode; |
| 889 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 890 | int err; |
| 891 | |
| 892 | if (cmd == F_GETLK) { |
| 893 | if (fc->no_lock) { |
| 894 | #ifdef KERNEL_2_6_17_PLUS |
| 895 | if (!posix_test_lock(file, fl, fl)) |
| 896 | fl->fl_type = F_UNLCK; |
| 897 | #else |
| 898 | struct file_lock *cfl = posix_test_lock(file, fl); |
| 899 | if (!cfl) |
| 900 | fl->fl_type = F_UNLCK; |
| 901 | else |
| 902 | *fl = *cfl; |
| 903 | #endif |
| 904 | err = 0; |
| 905 | } else |
| 906 | err = fuse_getlk(file, fl); |
| 907 | } else { |
| 908 | if (fc->no_lock) |
| 909 | err = posix_lock_file_wait(file, fl); |
| 910 | else |
| 911 | err = fuse_setlk(file, fl); |
| 912 | } |
| 913 | return err; |
| 914 | } |
| 915 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 916 | static struct file_operations fuse_file_operations = { |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 917 | .llseek = generic_file_llseek, |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 918 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 15af1b8 | 2006-01-16 17:52:24 +0000 | [diff] [blame] | 919 | .read = generic_file_read, |
| 920 | .write = generic_file_write, |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 921 | #else |
Miklos Szeredi | 89b86af | 2004-02-06 17:02:08 +0000 | [diff] [blame] | 922 | .read = fuse_file_read, |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 923 | .write = generic_file_write, |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 924 | #endif |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 925 | .mmap = fuse_file_mmap, |
Miklos Szeredi | 89b86af | 2004-02-06 17:02:08 +0000 | [diff] [blame] | 926 | .open = fuse_open, |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 927 | .flush = fuse_flush, |
Miklos Szeredi | 89b86af | 2004-02-06 17:02:08 +0000 | [diff] [blame] | 928 | .release = fuse_release, |
| 929 | .fsync = fuse_fsync, |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 930 | .lock = fuse_file_lock, |
Miklos Szeredi | 89b86af | 2004-02-06 17:02:08 +0000 | [diff] [blame] | 931 | #ifdef KERNEL_2_6 |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 932 | .sendfile = generic_file_sendfile, |
Miklos Szeredi | 89b86af | 2004-02-06 17:02:08 +0000 | [diff] [blame] | 933 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 934 | }; |
| 935 | |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 936 | static struct file_operations fuse_direct_io_file_operations = { |
| 937 | .llseek = generic_file_llseek, |
| 938 | .read = fuse_direct_read, |
| 939 | .write = fuse_direct_write, |
| 940 | .open = fuse_open, |
| 941 | .flush = fuse_flush, |
| 942 | .release = fuse_release, |
| 943 | .fsync = fuse_fsync, |
Miklos Szeredi | 91762cd | 2006-06-29 14:38:35 +0000 | [diff] [blame] | 944 | .lock = fuse_file_lock, |
Miklos Szeredi | bb9f517 | 2005-07-03 18:03:11 +0000 | [diff] [blame] | 945 | /* no mmap and sendfile */ |
| 946 | }; |
| 947 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 948 | static struct address_space_operations fuse_file_aops = { |
Miklos Szeredi | 84ba0f4 | 2004-07-18 11:32:59 +0000 | [diff] [blame] | 949 | .readpage = fuse_readpage, |
Miklos Szeredi | 84ba0f4 | 2004-07-18 11:32:59 +0000 | [diff] [blame] | 950 | .prepare_write = fuse_prepare_write, |
| 951 | .commit_write = fuse_commit_write, |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 952 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 5886537 | 2004-07-20 14:22:26 +0000 | [diff] [blame] | 953 | .readpages = fuse_readpages, |
Miklos Szeredi | 2a92727 | 2005-01-07 11:14:15 +0000 | [diff] [blame] | 954 | .set_page_dirty = fuse_set_page_dirty, |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 955 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 956 | }; |
| 957 | |
| 958 | void fuse_init_file_inode(struct inode *inode) |
| 959 | { |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 960 | inode->i_fop = &fuse_file_operations; |
| 961 | inode->i_data.a_ops = &fuse_file_aops; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 962 | } |