Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 149f607 | 2005-01-10 12:29:28 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +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 | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 12 | #include <linux/file.h> |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 13 | #ifdef KERNEL_2_6 |
| 14 | #include <linux/gfp.h> |
| 15 | #else |
| 16 | #include <linux/mm.h> |
| 17 | #endif |
| 18 | #include <linux/sched.h> |
Miklos Szeredi | 3b9e53f | 2005-09-02 16:04:48 +0000 | [diff] [blame] | 19 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 8139452 | 2005-01-11 14:24:18 +0000 | [diff] [blame] | 20 | #include <linux/namei.h> |
| 21 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 22 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 23 | static inline unsigned long time_to_jiffies(unsigned long sec, |
| 24 | unsigned long nsec) |
| 25 | { |
Miklos Szeredi | 8139452 | 2005-01-11 14:24:18 +0000 | [diff] [blame] | 26 | struct timespec ts = {sec, nsec}; |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 27 | return jiffies + timespec_to_jiffies(&ts); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 30 | static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o) |
| 31 | { |
| 32 | entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec); |
| 33 | if (entry->d_inode) |
| 34 | get_fuse_inode(entry->d_inode)->i_time = |
| 35 | time_to_jiffies(o->attr_valid, o->attr_valid_nsec); |
| 36 | } |
| 37 | |
| 38 | void fuse_invalidate_attr(struct inode *inode) |
| 39 | { |
| 40 | get_fuse_inode(inode)->i_time = jiffies - 1; |
| 41 | } |
| 42 | |
| 43 | static void fuse_invalidate_entry_cache(struct dentry *entry) |
| 44 | { |
| 45 | entry->d_time = jiffies - 1; |
| 46 | } |
| 47 | |
| 48 | static void fuse_invalidate_entry(struct dentry *entry) |
| 49 | { |
| 50 | d_invalidate(entry); |
| 51 | fuse_invalidate_entry_cache(entry); |
| 52 | } |
| 53 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 54 | static void fuse_lookup_init(struct fuse_req *req, struct inode *dir, |
| 55 | struct dentry *entry, |
| 56 | struct fuse_entry_out *outarg) |
| 57 | { |
| 58 | req->in.h.opcode = FUSE_LOOKUP; |
| 59 | req->in.h.nodeid = get_node_id(dir); |
| 60 | req->inode = dir; |
| 61 | req->in.numargs = 1; |
| 62 | req->in.args[0].size = entry->d_name.len + 1; |
| 63 | req->in.args[0].value = entry->d_name.name; |
| 64 | req->out.numargs = 1; |
| 65 | req->out.args[0].size = sizeof(struct fuse_entry_out); |
| 66 | req->out.args[0].value = outarg; |
| 67 | } |
| 68 | |
| 69 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) |
| 70 | { |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 71 | struct inode *inode = entry->d_inode; |
| 72 | |
| 73 | if (inode && is_bad_inode(inode)) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 74 | return 0; |
Miklos Szeredi | 8139452 | 2005-01-11 14:24:18 +0000 | [diff] [blame] | 75 | else if (time_after(jiffies, entry->d_time)) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 76 | int err; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 77 | struct fuse_entry_out outarg; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 78 | struct fuse_conn *fc; |
| 79 | struct fuse_req *req; |
| 80 | |
| 81 | fuse_invalidate_entry_cache(entry); |
| 82 | if (!inode) |
| 83 | return 0; |
| 84 | |
| 85 | fc = get_fuse_conn(inode); |
| 86 | req = fuse_get_request(fc); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 87 | if (!req) |
| 88 | return 0; |
| 89 | |
| 90 | fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg); |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 91 | request_send(fc, req); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 92 | err = req->out.h.error; |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 93 | if (!err) { |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 94 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 95 | if (outarg.nodeid != get_node_id(inode)) { |
| 96 | fuse_send_forget(fc, req, outarg.nodeid, 1); |
| 97 | return 0; |
| 98 | } |
| 99 | fi->nlookup ++; |
| 100 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 101 | fuse_put_request(fc, req); |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 102 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 103 | return 0; |
| 104 | |
| 105 | fuse_change_attributes(inode, &outarg.attr); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 106 | fuse_change_timeout(entry, &outarg); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 107 | } |
| 108 | return 1; |
| 109 | } |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 110 | |
| 111 | static int dir_alias(struct inode *inode) |
| 112 | { |
| 113 | if (S_ISDIR(inode->i_mode)) { |
| 114 | /* Don't allow creating an alias to a directory */ |
| 115 | struct dentry *alias = d_find_alias(inode); |
| 116 | #if defined(FUSE_MAINLINE) || !defined(KERNEL_2_6) |
| 117 | if (alias) { |
| 118 | dput(alias); |
| 119 | return 1; |
| 120 | } |
| 121 | #else |
| 122 | if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) { |
| 123 | dput(alias); |
| 124 | return 1; |
| 125 | } |
| 126 | dput(alias); |
| 127 | #endif |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static inline int invalid_nodeid(u64 nodeid) |
| 133 | { |
| 134 | return !nodeid || nodeid == FUSE_ROOT_ID; |
| 135 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 136 | #ifndef KERNEL_2_6 |
| 137 | static int fuse_dentry_revalidate_2_4(struct dentry *entry, int flags) |
| 138 | { |
| 139 | return fuse_dentry_revalidate(entry, NULL); |
| 140 | } |
| 141 | #endif |
| 142 | |
| 143 | static struct dentry_operations fuse_dentry_operations = { |
| 144 | #ifdef KERNEL_2_6 |
| 145 | .d_revalidate = fuse_dentry_revalidate, |
| 146 | #else |
| 147 | .d_revalidate = fuse_dentry_revalidate_2_4, |
| 148 | #endif |
| 149 | }; |
| 150 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 151 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
| 152 | struct nameidata *nd) |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 153 | { |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 154 | int err; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 155 | struct fuse_entry_out outarg; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 156 | struct inode *inode = NULL; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 157 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 158 | struct fuse_req *req; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 159 | #if !defined(FUSE_MAINLINE) && defined(KERNEL_2_6) |
| 160 | struct dentry *newent; |
| 161 | #endif |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 162 | |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 163 | if (entry->d_name.len > FUSE_NAME_MAX) |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 164 | return ERR_PTR(-ENAMETOOLONG); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 165 | |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 166 | req = fuse_get_request(fc); |
| 167 | if (!req) |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 168 | return ERR_PTR(-EINTR); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 169 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 170 | fuse_lookup_init(req, dir, entry, &outarg); |
| 171 | request_send(fc, req); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 172 | err = req->out.h.error; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 173 | if (!err && outarg.nodeid && invalid_nodeid(outarg.nodeid)) |
Miklos Szeredi | 6becf0b | 2005-09-23 11:25:28 +0000 | [diff] [blame] | 174 | err = -EIO; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 175 | if (!err && outarg.nodeid) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 176 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 177 | &outarg.attr); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 178 | if (!inode) { |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 179 | fuse_send_forget(fc, req, outarg.nodeid, 1); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 180 | return ERR_PTR(-ENOMEM); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 181 | } |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 182 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 183 | fuse_put_request(fc, req); |
| 184 | if (err && err != -ENOENT) |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 185 | return ERR_PTR(err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 186 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 187 | if (inode && dir_alias(inode)) { |
| 188 | iput(inode); |
| 189 | return ERR_PTR(-EIO); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 190 | } |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 191 | #if defined(FUSE_MAINLINE) || !defined(KERNEL_2_6) |
| 192 | d_add(entry, inode); |
| 193 | #else |
| 194 | newent = d_splice_alias(inode, entry); |
| 195 | entry = newent ? newent : entry; |
| 196 | #endif |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 197 | entry->d_op = &fuse_dentry_operations; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 198 | if (!err) |
| 199 | fuse_change_timeout(entry, &outarg); |
| 200 | else |
| 201 | fuse_invalidate_entry_cache(entry); |
| 202 | #if defined(FUSE_MAINLINE) || !defined(KERNEL_2_6) |
| 203 | return NULL; |
| 204 | #else |
| 205 | return newent; |
| 206 | #endif |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 209 | #ifdef HAVE_LOOKUP_INSTANTIATE_FILP |
| 210 | static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode, |
| 211 | struct nameidata *nd) |
| 212 | { |
| 213 | int err; |
| 214 | struct inode *inode; |
| 215 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 216 | struct fuse_req *req; |
| 217 | struct fuse_open_in inarg; |
| 218 | struct fuse_open_out outopen; |
| 219 | struct fuse_entry_out outentry; |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 220 | struct fuse_file *ff; |
| 221 | struct file *file; |
| 222 | int flags = nd->intent.open.flags - 1; |
| 223 | |
| 224 | err = -ENOSYS; |
| 225 | if (fc->no_create) |
| 226 | goto out; |
| 227 | |
| 228 | err = -ENAMETOOLONG; |
| 229 | if (entry->d_name.len > FUSE_NAME_MAX) |
| 230 | goto out; |
| 231 | |
| 232 | err = -EINTR; |
| 233 | req = fuse_get_request(fc); |
| 234 | if (!req) |
| 235 | goto out; |
| 236 | |
| 237 | ff = fuse_file_alloc(); |
| 238 | if (!ff) |
| 239 | goto out_put_request; |
| 240 | |
| 241 | flags &= ~O_NOCTTY; |
| 242 | memset(&inarg, 0, sizeof(inarg)); |
| 243 | inarg.flags = flags; |
| 244 | inarg.mode = mode; |
| 245 | req->in.h.opcode = FUSE_CREATE; |
| 246 | req->in.h.nodeid = get_node_id(dir); |
| 247 | req->inode = dir; |
| 248 | req->in.numargs = 2; |
| 249 | req->in.args[0].size = sizeof(inarg); |
| 250 | req->in.args[0].value = &inarg; |
| 251 | req->in.args[1].size = entry->d_name.len + 1; |
| 252 | req->in.args[1].value = entry->d_name.name; |
| 253 | req->out.numargs = 2; |
| 254 | req->out.args[0].size = sizeof(outentry); |
| 255 | req->out.args[0].value = &outentry; |
| 256 | req->out.args[1].size = sizeof(outopen); |
| 257 | req->out.args[1].value = &outopen; |
| 258 | request_send(fc, req); |
| 259 | err = req->out.h.error; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 260 | ff->fh = outopen.fh; |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 261 | if (err) { |
| 262 | if (err == -ENOSYS) |
| 263 | fc->no_create = 1; |
| 264 | goto out_free_ff; |
| 265 | } |
| 266 | |
| 267 | err = -EIO; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 268 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 269 | goto out_free_ff; |
| 270 | |
| 271 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, |
| 272 | &outentry.attr); |
| 273 | err = -ENOMEM; |
| 274 | if (!inode) { |
| 275 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 276 | fuse_send_release(fc, ff, outentry.nodeid, NULL, flags, 0); |
| 277 | goto out_put_request; |
| 278 | } |
| 279 | fuse_put_request(fc, req); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 280 | |
| 281 | d_instantiate(entry, inode); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 282 | fuse_change_timeout(entry, &outentry); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 283 | file = lookup_instantiate_filp(nd, entry, generic_file_open); |
| 284 | if (IS_ERR(file)) { |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 285 | fuse_send_release(fc, ff, outentry.nodeid, inode, flags, 0); |
| 286 | return PTR_ERR(file); |
| 287 | } |
| 288 | fuse_finish_open(inode, file, ff, &outopen); |
| 289 | return 0; |
| 290 | |
| 291 | out_free_ff: |
| 292 | fuse_file_free(ff); |
| 293 | out_put_request: |
| 294 | fuse_put_request(fc, req); |
| 295 | out: |
| 296 | return err; |
| 297 | } |
| 298 | #endif |
| 299 | |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 300 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 301 | struct inode *dir, struct dentry *entry, |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 302 | int mode) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 303 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 304 | struct fuse_entry_out outarg; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 305 | struct inode *inode; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 306 | int err; |
| 307 | |
| 308 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 309 | req->inode = dir; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 310 | req->out.numargs = 1; |
| 311 | req->out.args[0].size = sizeof(outarg); |
| 312 | req->out.args[0].value = &outarg; |
| 313 | request_send(fc, req); |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 314 | err = req->out.h.error; |
| 315 | if (err) { |
| 316 | fuse_put_request(fc, req); |
| 317 | return err; |
| 318 | } |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 319 | if (invalid_nodeid(outarg.nodeid)) { |
Miklos Szeredi | 6becf0b | 2005-09-23 11:25:28 +0000 | [diff] [blame] | 320 | fuse_put_request(fc, req); |
| 321 | return -EIO; |
| 322 | } |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 323 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 324 | &outarg.attr); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 325 | if (!inode) { |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 326 | fuse_send_forget(fc, req, outarg.nodeid, 1); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 327 | return -ENOMEM; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 328 | } |
| 329 | fuse_put_request(fc, req); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 330 | |
| 331 | /* Don't allow userspace to do really stupid things... */ |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 332 | if (((inode->i_mode ^ mode) & S_IFMT) || dir_alias(inode)) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 333 | iput(inode); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 334 | return -EIO; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | d_instantiate(entry, inode); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 338 | fuse_change_timeout(entry, &outarg); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 339 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 343 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 344 | dev_t rdev) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 345 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 346 | struct fuse_mknod_in inarg; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 347 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 348 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 349 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 350 | return -EINTR; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 351 | |
| 352 | memset(&inarg, 0, sizeof(inarg)); |
| 353 | inarg.mode = mode; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 354 | inarg.rdev = new_encode_dev(rdev); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 355 | req->in.h.opcode = FUSE_MKNOD; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 356 | req->in.numargs = 2; |
| 357 | req->in.args[0].size = sizeof(inarg); |
| 358 | req->in.args[0].value = &inarg; |
| 359 | req->in.args[1].size = entry->d_name.len + 1; |
| 360 | req->in.args[1].value = entry->d_name.name; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 361 | return create_new_entry(fc, req, dir, entry, mode); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 364 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, |
| 365 | struct nameidata *nd) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 366 | { |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 367 | #ifdef HAVE_LOOKUP_INSTANTIATE_FILP |
| 368 | if (nd && (nd->flags & LOOKUP_CREATE)) { |
| 369 | int err = fuse_create_open(dir, entry, mode, nd); |
| 370 | if (err != -ENOSYS) |
| 371 | return err; |
| 372 | /* Fall back on mknod */ |
| 373 | } |
| 374 | #endif |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 375 | return fuse_mknod(dir, entry, mode, 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 378 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) |
| 379 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 380 | struct fuse_mkdir_in inarg; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 381 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 382 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 383 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 384 | return -EINTR; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 385 | |
| 386 | memset(&inarg, 0, sizeof(inarg)); |
| 387 | inarg.mode = mode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 388 | req->in.h.opcode = FUSE_MKDIR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 389 | req->in.numargs = 2; |
| 390 | req->in.args[0].size = sizeof(inarg); |
| 391 | req->in.args[0].value = &inarg; |
| 392 | req->in.args[1].size = entry->d_name.len + 1; |
| 393 | req->in.args[1].value = entry->d_name.name; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 394 | return create_new_entry(fc, req, dir, entry, S_IFDIR); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int fuse_symlink(struct inode *dir, struct dentry *entry, |
| 398 | const char *link) |
| 399 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 400 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 401 | unsigned len = strlen(link) + 1; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 402 | struct fuse_req *req; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 403 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 404 | if (len > FUSE_SYMLINK_MAX) |
| 405 | return -ENAMETOOLONG; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 406 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 407 | req = fuse_get_request(fc); |
| 408 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 409 | return -EINTR; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 410 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 411 | req->in.h.opcode = FUSE_SYMLINK; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 412 | req->in.numargs = 2; |
| 413 | req->in.args[0].size = entry->d_name.len + 1; |
| 414 | req->in.args[0].value = entry->d_name.name; |
| 415 | req->in.args[1].size = len; |
| 416 | req->in.args[1].value = link; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 417 | return create_new_entry(fc, req, dir, entry, S_IFLNK); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 420 | static int fuse_unlink(struct inode *dir, struct dentry *entry) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 421 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 422 | int err; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 423 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 424 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 425 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 426 | return -EINTR; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 427 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 428 | req->in.h.opcode = FUSE_UNLINK; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 429 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 430 | req->inode = dir; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 431 | req->in.numargs = 1; |
| 432 | req->in.args[0].size = entry->d_name.len + 1; |
| 433 | req->in.args[0].value = entry->d_name.name; |
| 434 | request_send(fc, req); |
| 435 | err = req->out.h.error; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 436 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 437 | if (!err) { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 438 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 439 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 440 | /* Set nlink to zero so the inode can be cleared, if |
| 441 | the inode does have more links this will be |
| 442 | discovered at the next lookup/getattr */ |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 443 | inode->i_nlink = 0; |
| 444 | fuse_invalidate_attr(inode); |
| 445 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 446 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 447 | } else if (err == -EINTR) |
| 448 | fuse_invalidate_entry(entry); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 449 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) |
| 453 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 454 | int err; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 455 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 456 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 457 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 458 | return -EINTR; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 459 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 460 | req->in.h.opcode = FUSE_RMDIR; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 461 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 462 | req->inode = dir; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 463 | req->in.numargs = 1; |
| 464 | req->in.args[0].size = entry->d_name.len + 1; |
| 465 | req->in.args[0].value = entry->d_name.name; |
| 466 | request_send(fc, req); |
| 467 | err = req->out.h.error; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 468 | fuse_put_request(fc, req); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 469 | if (!err) { |
Miklos Szeredi | f4f8b89 | 2004-01-27 17:04:59 +0000 | [diff] [blame] | 470 | entry->d_inode->i_nlink = 0; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 471 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 472 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 473 | } else if (err == -EINTR) |
| 474 | fuse_invalidate_entry(entry); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 475 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, |
| 479 | struct inode *newdir, struct dentry *newent) |
| 480 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 481 | int err; |
| 482 | struct fuse_rename_in inarg; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 483 | struct fuse_conn *fc = get_fuse_conn(olddir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 484 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 485 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 486 | return -EINTR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 487 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 488 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 489 | inarg.newdir = get_node_id(newdir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 490 | req->in.h.opcode = FUSE_RENAME; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 491 | req->in.h.nodeid = get_node_id(olddir); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 492 | req->inode = olddir; |
| 493 | req->inode2 = newdir; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 494 | req->in.numargs = 3; |
| 495 | req->in.args[0].size = sizeof(inarg); |
| 496 | req->in.args[0].value = &inarg; |
| 497 | req->in.args[1].size = oldent->d_name.len + 1; |
| 498 | req->in.args[1].value = oldent->d_name.name; |
| 499 | req->in.args[2].size = newent->d_name.len + 1; |
| 500 | req->in.args[2].value = newent->d_name.name; |
| 501 | request_send(fc, req); |
| 502 | err = req->out.h.error; |
| 503 | fuse_put_request(fc, req); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 504 | if (!err) { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 505 | fuse_invalidate_attr(olddir); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 506 | if (olddir != newdir) |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 507 | fuse_invalidate_attr(newdir); |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 508 | |
| 509 | /* newent will end up negative */ |
| 510 | fuse_invalidate_entry_cache(newent); |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 511 | } else if (err == -EINTR) { |
| 512 | /* If request was interrupted, DEITY only knows if the |
| 513 | rename actually took place. If the invalidation |
| 514 | fails (e.g. some process has CWD under the renamed |
| 515 | directory), then there can be inconsistency between |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 516 | the dcache and the real filesystem. Tough luck. */ |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 517 | fuse_invalidate_entry(oldent); |
| 518 | if (newent->d_inode) |
| 519 | fuse_invalidate_entry(newent); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 520 | } |
Miklos Szeredi | cdae16e | 2005-01-14 11:43:22 +0000 | [diff] [blame] | 521 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 522 | return err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static int fuse_link(struct dentry *entry, struct inode *newdir, |
| 526 | struct dentry *newent) |
| 527 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 528 | int err; |
| 529 | struct fuse_link_in inarg; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 530 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 531 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 532 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 533 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 534 | return -EINTR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 535 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 536 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | ed6b5dd | 2005-01-26 17:07:59 +0000 | [diff] [blame] | 537 | inarg.oldnodeid = get_node_id(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 538 | req->in.h.opcode = FUSE_LINK; |
Miklos Szeredi | ed6b5dd | 2005-01-26 17:07:59 +0000 | [diff] [blame] | 539 | req->inode2 = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 540 | req->in.numargs = 2; |
| 541 | req->in.args[0].size = sizeof(inarg); |
| 542 | req->in.args[0].value = &inarg; |
| 543 | req->in.args[1].size = newent->d_name.len + 1; |
| 544 | req->in.args[1].value = newent->d_name.name; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 545 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); |
| 546 | /* Contrary to "normal" filesystems it can happen that link |
| 547 | makes two "logical" inodes point to the same "physical" |
| 548 | inode. We invalidate the attributes of the old one, so it |
| 549 | will reflect changes in the backing inode (link count, |
| 550 | etc.) |
| 551 | */ |
| 552 | if (!err || err == -EINTR) |
| 553 | fuse_invalidate_attr(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 554 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 557 | int fuse_do_getattr(struct inode *inode) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 558 | { |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 559 | int err; |
| 560 | struct fuse_attr_out arg; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 561 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 562 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 563 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 564 | return -EINTR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 565 | |
| 566 | req->in.h.opcode = FUSE_GETATTR; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 567 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 568 | req->inode = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 569 | req->out.numargs = 1; |
| 570 | req->out.args[0].size = sizeof(arg); |
| 571 | req->out.args[0].value = &arg; |
| 572 | request_send(fc, req); |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 573 | err = req->out.h.error; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 574 | fuse_put_request(fc, req); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 575 | if (!err) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 576 | if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) { |
Miklos Szeredi | 9f60c49 | 2005-09-08 14:34:48 +0000 | [diff] [blame] | 577 | #ifndef KERNEL_2_6_12_PLUS |
Miklos Szeredi | dbe0f65 | 2005-01-15 14:32:56 +0000 | [diff] [blame] | 578 | if (get_node_id(inode) != FUSE_ROOT_ID) |
| 579 | make_bad_inode(inode); |
Miklos Szeredi | e22acbe | 2005-01-20 08:37:30 +0000 | [diff] [blame] | 580 | #else |
| 581 | make_bad_inode(inode); |
| 582 | #endif |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 583 | err = -EIO; |
| 584 | } else { |
| 585 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 586 | fuse_change_attributes(inode, &arg.attr); |
| 587 | fi->i_time = time_to_jiffies(arg.attr_valid, |
| 588 | arg.attr_valid_nsec); |
| 589 | } |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 590 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 591 | return err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Miklos Szeredi | 61139db | 2005-04-29 07:38:25 +0000 | [diff] [blame] | 594 | /* |
| 595 | * Calling into a user-controlled filesystem gives the filesystem |
| 596 | * daemon ptrace-like capabilities over the requester process. This |
| 597 | * means, that the filesystem daemon is able to record the exact |
| 598 | * filesystem operations performed, and can also control the behavior |
| 599 | * of the requester process in otherwise impossible ways. For example |
| 600 | * it can delay the operation for arbitrary length of time allowing |
| 601 | * DoS against the requester. |
| 602 | * |
| 603 | * For this reason only those processes can call into the filesystem, |
| 604 | * for which the owner of the mount has ptrace privilege. This |
| 605 | * excludes processes started by other users, suid or sgid processes. |
| 606 | */ |
Miklos Szeredi | 9c1b68d | 2005-04-28 09:55:09 +0000 | [diff] [blame] | 607 | static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task) |
| 608 | { |
| 609 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 610 | return 1; |
| 611 | |
Miklos Szeredi | 9c1b68d | 2005-04-28 09:55:09 +0000 | [diff] [blame] | 612 | if (task->euid == fc->user_id && |
| 613 | task->suid == fc->user_id && |
| 614 | task->uid == fc->user_id && |
| 615 | task->egid == fc->group_id && |
| 616 | task->sgid == fc->group_id && |
| 617 | task->gid == fc->group_id) |
| 618 | return 1; |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 623 | static int fuse_revalidate(struct dentry *entry) |
| 624 | { |
| 625 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 626 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 627 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 628 | |
Miklos Szeredi | 9c1b68d | 2005-04-28 09:55:09 +0000 | [diff] [blame] | 629 | if (!fuse_allow_task(fc, current)) |
| 630 | return -EACCES; |
| 631 | if (get_node_id(inode) != FUSE_ROOT_ID && |
| 632 | time_before_eq(jiffies, fi->i_time)) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 633 | return 0; |
| 634 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 635 | return fuse_do_getattr(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 638 | #ifdef KERNEL_2_6 |
| 639 | static int fuse_access(struct inode *inode, int mask) |
| 640 | { |
| 641 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 642 | struct fuse_req *req; |
| 643 | struct fuse_access_in inarg; |
| 644 | int err; |
| 645 | |
| 646 | if (fc->no_access) |
| 647 | return 0; |
| 648 | |
| 649 | req = fuse_get_request(fc); |
| 650 | if (!req) |
| 651 | return -EINTR; |
| 652 | |
| 653 | memset(&inarg, 0, sizeof(inarg)); |
| 654 | inarg.mask = mask; |
| 655 | req->in.h.opcode = FUSE_ACCESS; |
| 656 | req->in.h.nodeid = get_node_id(inode); |
| 657 | req->inode = inode; |
| 658 | req->in.numargs = 1; |
| 659 | req->in.args[0].size = sizeof(inarg); |
| 660 | req->in.args[0].value = &inarg; |
| 661 | request_send(fc, req); |
| 662 | err = req->out.h.error; |
| 663 | fuse_put_request(fc, req); |
| 664 | if (err == -ENOSYS) { |
| 665 | fc->no_access = 1; |
| 666 | err = 0; |
| 667 | } |
| 668 | return err; |
| 669 | } |
| 670 | #endif |
| 671 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 672 | static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 673 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 674 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 675 | |
Miklos Szeredi | 9c1b68d | 2005-04-28 09:55:09 +0000 | [diff] [blame] | 676 | if (!fuse_allow_task(fc, current)) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 677 | return -EACCES; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 678 | else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 679 | #ifdef KERNEL_2_6_10_PLUS |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 680 | int err = generic_permission(inode, mask, NULL); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 681 | #else |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 682 | int err = vfs_permission(inode, mask); |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 683 | #endif |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 684 | |
| 685 | /* If permission is denied, try to refresh file |
| 686 | attributes. This is also needed, because the root |
| 687 | node will at first have no permissions */ |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 688 | if (err == -EACCES) { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 689 | err = fuse_do_getattr(inode); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 690 | if (!err) |
| 691 | #ifdef KERNEL_2_6_10_PLUS |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 692 | err = generic_permission(inode, mask, NULL); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 693 | #else |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 694 | err = vfs_permission(inode, mask); |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 695 | #endif |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* FIXME: Need some mechanism to revoke permissions: |
| 699 | currently if the filesystem suddenly changes the |
Miklos Szeredi | 064efb0 | 2004-11-09 17:30:29 +0000 | [diff] [blame] | 700 | file mode, we will not be informed about it, and |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 701 | continue to allow access to the file/directory. |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 702 | |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 703 | This is actually not so grave, since the user can |
| 704 | simply keep access to the file/directory anyway by |
| 705 | keeping it open... */ |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 706 | |
| 707 | return err; |
Miklos Szeredi | 96dfad7 | 2004-11-30 00:00:02 +0000 | [diff] [blame] | 708 | } else { |
| 709 | int mode = inode->i_mode; |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 710 | #ifndef KERNEL_2_6_11_PLUS |
Miklos Szeredi | 96dfad7 | 2004-11-30 00:00:02 +0000 | [diff] [blame] | 711 | if ((mask & MAY_WRITE) && IS_RDONLY(inode) && |
| 712 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
| 713 | return -EROFS; |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 714 | #endif |
Miklos Szeredi | 96dfad7 | 2004-11-30 00:00:02 +0000 | [diff] [blame] | 715 | if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO)) |
| 716 | return -EACCES; |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 717 | |
| 718 | #ifdef KERNEL_2_6 |
| 719 | if (nd && (nd->flags & LOOKUP_ACCESS)) |
| 720 | return fuse_access(inode, mask); |
| 721 | #endif |
Miklos Szeredi | fcf9f8d | 2005-09-08 14:28:54 +0000 | [diff] [blame] | 722 | return 0; |
Miklos Szeredi | 96dfad7 | 2004-11-30 00:00:02 +0000 | [diff] [blame] | 723 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 726 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, |
| 727 | void *dstbuf, filldir_t filldir) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 728 | { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 729 | while (nbytes >= FUSE_NAME_OFFSET) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 730 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 731 | size_t reclen = FUSE_DIRENT_SIZE(dirent); |
| 732 | int over; |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame] | 733 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 734 | return -EIO; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 735 | if (reclen > nbytes) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 736 | break; |
| 737 | |
| 738 | over = filldir(dstbuf, dirent->name, dirent->namelen, |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame] | 739 | file->f_pos, dirent->ino, dirent->type); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 740 | if (over) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 741 | break; |
| 742 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 743 | buf += reclen; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 744 | nbytes -= reclen; |
Miklos Szeredi | 18fce98 | 2005-04-01 21:07:35 +0000 | [diff] [blame] | 745 | file->f_pos = dirent->off; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 751 | static inline size_t fuse_send_readdir(struct fuse_req *req, struct file *file, |
| 752 | struct inode *inode, loff_t pos, |
| 753 | size_t count) |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 754 | { |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 755 | return fuse_send_read_common(req, file, inode, pos, count, 1); |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 758 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
| 759 | { |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 760 | int err; |
| 761 | size_t nbytes; |
| 762 | struct page *page; |
Miklos Szeredi | e22acbe | 2005-01-20 08:37:30 +0000 | [diff] [blame] | 763 | struct inode *inode = file->f_dentry->d_inode; |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 764 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 765 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 766 | if (!req) |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 767 | return -EINTR; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 768 | |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 769 | page = alloc_page(GFP_KERNEL); |
| 770 | if (!page) { |
| 771 | fuse_put_request(fc, req); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 772 | return -ENOMEM; |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 773 | } |
| 774 | req->num_pages = 1; |
| 775 | req->pages[0] = page; |
| 776 | nbytes = fuse_send_readdir(req, file, inode, file->f_pos, PAGE_SIZE); |
| 777 | err = req->out.h.error; |
| 778 | fuse_put_request(fc, req); |
| 779 | if (!err) |
| 780 | err = parse_dirfile(page_address(page), nbytes, file, dstbuf, |
| 781 | filldir); |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 782 | |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 783 | __free_page(page); |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 784 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 785 | return err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 788 | static char *read_link(struct dentry *dentry) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 789 | { |
| 790 | struct inode *inode = dentry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 791 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 792 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 793 | char *link; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 794 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 795 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 796 | return ERR_PTR(-EINTR); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 797 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 798 | link = (char *) __get_free_page(GFP_KERNEL); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 799 | if (!link) { |
| 800 | link = ERR_PTR(-ENOMEM); |
| 801 | goto out; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 802 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 803 | req->in.h.opcode = FUSE_READLINK; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 804 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 805 | req->inode = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 806 | req->out.argvar = 1; |
| 807 | req->out.numargs = 1; |
| 808 | req->out.args[0].size = PAGE_SIZE - 1; |
| 809 | req->out.args[0].value = link; |
| 810 | request_send(fc, req); |
| 811 | if (req->out.h.error) { |
| 812 | free_page((unsigned long) link); |
| 813 | link = ERR_PTR(req->out.h.error); |
| 814 | } else |
| 815 | link[req->out.args[0].size] = '\0'; |
| 816 | out: |
| 817 | fuse_put_request(fc, req); |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 818 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 819 | return link; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static void free_link(char *link) |
| 823 | { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 824 | if (!IS_ERR(link)) |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 825 | free_page((unsigned long) link); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Miklos Szeredi | 5648781 | 2005-09-02 13:05:06 +0000 | [diff] [blame] | 828 | #ifdef KERNEL_2_6_13_PLUS |
| 829 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 830 | { |
| 831 | nd_set_link(nd, read_link(dentry)); |
| 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) |
| 836 | { |
| 837 | free_link(nd_get_link(nd)); |
| 838 | } |
| 839 | #elif defined(KERNEL_2_6_8_PLUS) |
Miklos Szeredi | 8139452 | 2005-01-11 14:24:18 +0000 | [diff] [blame] | 840 | static int fuse_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 841 | { |
| 842 | nd_set_link(nd, read_link(dentry)); |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd) |
| 847 | { |
| 848 | free_link(nd_get_link(nd)); |
| 849 | } |
| 850 | #else |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 851 | static int fuse_readlink(struct dentry *dentry, char __user *buffer, |
| 852 | int buflen) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 853 | { |
| 854 | int ret; |
| 855 | char *link; |
| 856 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 857 | link = read_link(dentry); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 858 | ret = vfs_readlink(dentry, buffer, buflen, link); |
| 859 | free_link(link); |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | static int fuse_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 864 | { |
| 865 | int ret; |
| 866 | char *link; |
| 867 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 868 | link = read_link(dentry); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 869 | ret = vfs_follow_link(nd, link); |
| 870 | free_link(link); |
| 871 | return ret; |
| 872 | } |
Miklos Szeredi | 8139452 | 2005-01-11 14:24:18 +0000 | [diff] [blame] | 873 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 874 | |
| 875 | static int fuse_dir_open(struct inode *inode, struct file *file) |
| 876 | { |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 877 | return fuse_open_common(inode, file, 1); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static int fuse_dir_release(struct inode *inode, struct file *file) |
| 881 | { |
Miklos Szeredi | 1adb227 | 2005-01-18 21:19:58 +0000 | [diff] [blame] | 882 | return fuse_release_common(inode, file, 1); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 885 | static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync) |
| 886 | { |
Miklos Szeredi | b7640d2 | 2005-04-08 15:15:28 +0000 | [diff] [blame] | 887 | /* nfsd can call this with no file */ |
| 888 | return file ? fuse_fsync_common(file, de, datasync, 1) : 0; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 891 | static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 892 | { |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 893 | unsigned ivalid = iattr->ia_valid; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 894 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 895 | if (ivalid & ATTR_MODE) |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 896 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 897 | if (ivalid & ATTR_UID) |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 898 | arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 899 | if (ivalid & ATTR_GID) |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 900 | arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 901 | if (ivalid & ATTR_SIZE) |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 902 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 903 | /* You can only _set_ these together (they may change by themselves) */ |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 904 | if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) { |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 905 | arg->valid |= FATTR_ATIME | FATTR_MTIME; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 906 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 907 | arg->atime = iattr->ia_atime.tv_sec; |
| 908 | arg->mtime = iattr->ia_mtime.tv_sec; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 909 | #else |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 910 | arg->atime = iattr->ia_atime; |
| 911 | arg->mtime = iattr->ia_mtime; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 912 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 913 | } |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 914 | #ifdef ATTR_FILE |
| 915 | if (ivalid & ATTR_FILE) { |
| 916 | struct fuse_file *ff = iattr->ia_file->private_data; |
| 917 | arg->valid |= FATTR_FH; |
| 918 | arg->fh = ff->fh; |
| 919 | } |
| 920 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
| 924 | { |
| 925 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 926 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 927 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 58615e0 | 2004-07-04 21:21:08 +0000 | [diff] [blame] | 928 | struct fuse_req *req; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 929 | struct fuse_setattr_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 930 | struct fuse_attr_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 931 | int err; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 932 | int is_truncate = 0; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 933 | |
Miklos Szeredi | 94ed76a | 2004-07-26 19:38:45 +0000 | [diff] [blame] | 934 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
| 935 | err = inode_change_ok(inode, attr); |
| 936 | if (err) |
| 937 | return err; |
| 938 | } |
Miklos Szeredi | 58615e0 | 2004-07-04 21:21:08 +0000 | [diff] [blame] | 939 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 940 | if (attr->ia_valid & ATTR_SIZE) { |
| 941 | unsigned long limit; |
| 942 | is_truncate = 1; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 943 | #ifdef KERNEL_2_6_10_PLUS |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 944 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 945 | #else |
| 946 | limit = current->rlim[RLIMIT_FSIZE].rlim_cur; |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 947 | #endif |
| 948 | if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 949 | send_sig(SIGXFSZ, current, 0); |
| 950 | return -EFBIG; |
| 951 | } |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 952 | } |
Miklos Szeredi | 58615e0 | 2004-07-04 21:21:08 +0000 | [diff] [blame] | 953 | |
| 954 | req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 955 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 956 | return -EINTR; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 957 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 958 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 959 | iattr_to_fattr(attr, &inarg); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 960 | req->in.h.opcode = FUSE_SETATTR; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 961 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 962 | req->inode = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 963 | req->in.numargs = 1; |
| 964 | req->in.args[0].size = sizeof(inarg); |
| 965 | req->in.args[0].value = &inarg; |
| 966 | req->out.numargs = 1; |
| 967 | req->out.args[0].size = sizeof(outarg); |
| 968 | req->out.args[0].value = &outarg; |
| 969 | request_send(fc, req); |
| 970 | err = req->out.h.error; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 971 | fuse_put_request(fc, req); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 972 | if (!err) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 973 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
Miklos Szeredi | 9f60c49 | 2005-09-08 14:34:48 +0000 | [diff] [blame] | 974 | #ifndef KERNEL_2_6_12_PLUS |
Miklos Szeredi | dbe0f65 | 2005-01-15 14:32:56 +0000 | [diff] [blame] | 975 | if (get_node_id(inode) != FUSE_ROOT_ID) |
| 976 | make_bad_inode(inode); |
Miklos Szeredi | e22acbe | 2005-01-20 08:37:30 +0000 | [diff] [blame] | 977 | #else |
| 978 | make_bad_inode(inode); |
| 979 | #endif |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 980 | err = -EIO; |
| 981 | } else { |
| 982 | if (is_truncate) { |
| 983 | loff_t origsize = i_size_read(inode); |
| 984 | i_size_write(inode, outarg.attr.size); |
| 985 | if (origsize > outarg.attr.size) |
| 986 | vmtruncate(inode, outarg.attr.size); |
| 987 | } |
| 988 | fuse_change_attributes(inode, &outarg.attr); |
| 989 | fi->i_time = time_to_jiffies(outarg.attr_valid, |
| 990 | outarg.attr_valid_nsec); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 991 | } |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 992 | } else if (err == -EINTR) |
| 993 | fuse_invalidate_attr(inode); |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 994 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 995 | return err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 998 | #ifdef KERNEL_2_6 |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 999 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
| 1000 | struct kstat *stat) |
| 1001 | { |
| 1002 | struct inode *inode = entry->d_inode; |
| 1003 | int err = fuse_revalidate(entry); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 1004 | if (!err) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1005 | generic_fillattr(inode, stat); |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 1006 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1007 | return err; |
| 1008 | } |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1009 | #else /* KERNEL_2_6 */ |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 1010 | static struct dentry *fuse_lookup_2_4(struct inode *dir, struct dentry *entry) |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1011 | { |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 1012 | return fuse_lookup(dir, entry, NULL); |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1015 | static int fuse_mknod_2_4(struct inode *dir, struct dentry *entry, int mode, |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 1016 | int rdev) |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1017 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1018 | return fuse_mknod(dir, entry, mode, rdev); |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1021 | static int fuse_create_2_4(struct inode *dir, struct dentry *entry, int mode) |
| 1022 | { |
| 1023 | return fuse_create(dir, entry, mode, NULL); |
| 1024 | } |
| 1025 | |
| 1026 | static int fuse_permission_2_4(struct inode *inode, int mask) |
| 1027 | { |
| 1028 | return fuse_permission(inode, mask, NULL); |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1029 | } |
| 1030 | #endif /* KERNEL_2_6 */ |
| 1031 | |
| 1032 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1033 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1034 | static int fuse_setxattr(struct dentry *entry, const char *name, |
| 1035 | const void *value, size_t size, int flags) |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1036 | #else |
| 1037 | static int fuse_setxattr(struct dentry *entry, const char *name, |
| 1038 | void *value, size_t size, int flags) |
| 1039 | #endif |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1040 | { |
| 1041 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1042 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1043 | struct fuse_req *req; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1044 | struct fuse_setxattr_in inarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1045 | int err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1046 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 1047 | if (size > FUSE_XATTR_SIZE_MAX) |
| 1048 | return -E2BIG; |
| 1049 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1050 | if (fc->no_setxattr) |
| 1051 | return -EOPNOTSUPP; |
| 1052 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1053 | req = fuse_get_request(fc); |
| 1054 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 1055 | return -EINTR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1056 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1057 | memset(&inarg, 0, sizeof(inarg)); |
| 1058 | inarg.size = size; |
| 1059 | inarg.flags = flags; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1060 | req->in.h.opcode = FUSE_SETXATTR; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1061 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1062 | req->inode = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1063 | req->in.numargs = 3; |
| 1064 | req->in.args[0].size = sizeof(inarg); |
| 1065 | req->in.args[0].value = &inarg; |
| 1066 | req->in.args[1].size = strlen(name) + 1; |
| 1067 | req->in.args[1].value = name; |
| 1068 | req->in.args[2].size = size; |
| 1069 | req->in.args[2].value = value; |
| 1070 | request_send(fc, req); |
| 1071 | err = req->out.h.error; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 1072 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1073 | if (err == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1074 | fc->no_setxattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1075 | err = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1076 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1077 | return err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, |
| 1081 | void *value, size_t size) |
| 1082 | { |
| 1083 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1084 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1085 | struct fuse_req *req; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1086 | struct fuse_getxattr_in inarg; |
| 1087 | struct fuse_getxattr_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1088 | ssize_t ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1089 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1090 | if (fc->no_getxattr) |
| 1091 | return -EOPNOTSUPP; |
| 1092 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1093 | req = fuse_get_request(fc); |
| 1094 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 1095 | return -EINTR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1096 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1097 | memset(&inarg, 0, sizeof(inarg)); |
| 1098 | inarg.size = size; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1099 | req->in.h.opcode = FUSE_GETXATTR; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1100 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1101 | req->inode = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1102 | req->in.numargs = 2; |
| 1103 | req->in.args[0].size = sizeof(inarg); |
| 1104 | req->in.args[0].value = &inarg; |
| 1105 | req->in.args[1].size = strlen(name) + 1; |
| 1106 | req->in.args[1].value = name; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1107 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1108 | req->out.numargs = 1; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 1109 | if (size) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1110 | req->out.argvar = 1; |
| 1111 | req->out.args[0].size = size; |
| 1112 | req->out.args[0].value = value; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1113 | } else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1114 | req->out.args[0].size = sizeof(outarg); |
| 1115 | req->out.args[0].value = &outarg; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1116 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1117 | request_send(fc, req); |
| 1118 | ret = req->out.h.error; |
| 1119 | if (!ret) |
| 1120 | ret = size ? req->out.args[0].size : outarg.size; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1121 | else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1122 | if (ret == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1123 | fc->no_getxattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1124 | ret = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1125 | } |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1126 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1127 | fuse_put_request(fc, req); |
| 1128 | return ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) |
| 1132 | { |
| 1133 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1134 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1135 | struct fuse_req *req; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1136 | struct fuse_getxattr_in inarg; |
| 1137 | struct fuse_getxattr_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1138 | ssize_t ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1139 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1140 | if (fc->no_listxattr) |
| 1141 | return -EOPNOTSUPP; |
| 1142 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1143 | req = fuse_get_request(fc); |
| 1144 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 1145 | return -EINTR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1146 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1147 | memset(&inarg, 0, sizeof(inarg)); |
| 1148 | inarg.size = size; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1149 | req->in.h.opcode = FUSE_LISTXATTR; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1150 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1151 | req->inode = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1152 | req->in.numargs = 1; |
| 1153 | req->in.args[0].size = sizeof(inarg); |
| 1154 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1155 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1156 | req->out.numargs = 1; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 1157 | if (size) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1158 | req->out.argvar = 1; |
| 1159 | req->out.args[0].size = size; |
| 1160 | req->out.args[0].value = list; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1161 | } else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1162 | req->out.args[0].size = sizeof(outarg); |
| 1163 | req->out.args[0].value = &outarg; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1164 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1165 | request_send(fc, req); |
| 1166 | ret = req->out.h.error; |
| 1167 | if (!ret) |
| 1168 | ret = size ? req->out.args[0].size : outarg.size; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1169 | else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1170 | if (ret == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1171 | fc->no_listxattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1172 | ret = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1173 | } |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1174 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1175 | fuse_put_request(fc, req); |
| 1176 | return ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static int fuse_removexattr(struct dentry *entry, const char *name) |
| 1180 | { |
| 1181 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1182 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1183 | struct fuse_req *req; |
| 1184 | int err; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 1185 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1186 | if (fc->no_removexattr) |
| 1187 | return -EOPNOTSUPP; |
| 1188 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1189 | req = fuse_get_request(fc); |
| 1190 | if (!req) |
Miklos Szeredi | f94e010 | 2005-05-12 14:56:34 +0000 | [diff] [blame] | 1191 | return -EINTR; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1192 | |
| 1193 | req->in.h.opcode = FUSE_REMOVEXATTR; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 1194 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1195 | req->inode = inode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1196 | req->in.numargs = 1; |
| 1197 | req->in.args[0].size = strlen(name) + 1; |
| 1198 | req->in.args[0].value = name; |
| 1199 | request_send(fc, req); |
| 1200 | err = req->out.h.error; |
Miklos Szeredi | 0fcfa03 | 2004-12-13 15:22:28 +0000 | [diff] [blame] | 1201 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1202 | if (err == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1203 | fc->no_removexattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1204 | err = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1205 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1206 | return err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1207 | } |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1208 | #endif |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1209 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1210 | static struct inode_operations fuse_dir_inode_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1211 | .lookup = fuse_lookup, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1212 | .mkdir = fuse_mkdir, |
| 1213 | .symlink = fuse_symlink, |
| 1214 | .unlink = fuse_unlink, |
| 1215 | .rmdir = fuse_rmdir, |
| 1216 | .rename = fuse_rename, |
| 1217 | .link = fuse_link, |
| 1218 | .setattr = fuse_setattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1219 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1220 | .create = fuse_create, |
| 1221 | .mknod = fuse_mknod, |
| 1222 | .permission = fuse_permission, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1223 | .getattr = fuse_getattr, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1224 | #else |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1225 | .create = fuse_create_2_4, |
| 1226 | .mknod = fuse_mknod_2_4, |
| 1227 | .permission = fuse_permission_2_4, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1228 | .revalidate = fuse_revalidate, |
| 1229 | #endif |
| 1230 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1231 | .setxattr = fuse_setxattr, |
| 1232 | .getxattr = fuse_getxattr, |
| 1233 | .listxattr = fuse_listxattr, |
| 1234 | .removexattr = fuse_removexattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1235 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1236 | }; |
| 1237 | |
| 1238 | static struct file_operations fuse_dir_operations = { |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1239 | .llseek = generic_file_llseek, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1240 | .read = generic_read_dir, |
| 1241 | .readdir = fuse_readdir, |
| 1242 | .open = fuse_dir_open, |
| 1243 | .release = fuse_dir_release, |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1244 | .fsync = fuse_dir_fsync, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1245 | }; |
| 1246 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1247 | static struct inode_operations fuse_common_inode_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1248 | .setattr = fuse_setattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1249 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1250 | .permission = fuse_permission, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1251 | .getattr = fuse_getattr, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1252 | #else |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1253 | .permission = fuse_permission_2_4, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1254 | .revalidate = fuse_revalidate, |
| 1255 | #endif |
| 1256 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1257 | .setxattr = fuse_setxattr, |
| 1258 | .getxattr = fuse_getxattr, |
| 1259 | .listxattr = fuse_listxattr, |
| 1260 | .removexattr = fuse_removexattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1261 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1262 | }; |
| 1263 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1264 | static struct inode_operations fuse_symlink_inode_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1265 | .setattr = fuse_setattr, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1266 | .follow_link = fuse_follow_link, |
Miklos Szeredi | 7a98395 | 2005-01-28 09:58:19 +0000 | [diff] [blame] | 1267 | #ifdef KERNEL_2_6_8_PLUS |
Miklos Szeredi | 8139452 | 2005-01-11 14:24:18 +0000 | [diff] [blame] | 1268 | .put_link = fuse_put_link, |
| 1269 | .readlink = generic_readlink, |
| 1270 | #else |
| 1271 | .readlink = fuse_readlink, |
| 1272 | #endif |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1273 | #ifdef KERNEL_2_6 |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1274 | .getattr = fuse_getattr, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1275 | #else |
| 1276 | .revalidate = fuse_revalidate, |
| 1277 | #endif |
| 1278 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1279 | .setxattr = fuse_setxattr, |
| 1280 | .getxattr = fuse_getxattr, |
| 1281 | .listxattr = fuse_listxattr, |
| 1282 | .removexattr = fuse_removexattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1283 | #endif |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1284 | }; |
| 1285 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1286 | void fuse_init_common(struct inode *inode) |
| 1287 | { |
| 1288 | inode->i_op = &fuse_common_inode_operations; |
| 1289 | } |
| 1290 | |
| 1291 | void fuse_init_dir(struct inode *inode) |
| 1292 | { |
| 1293 | inode->i_op = &fuse_dir_inode_operations; |
| 1294 | inode->i_fop = &fuse_dir_operations; |
| 1295 | } |
| 1296 | |
| 1297 | void fuse_init_symlink(struct inode *inode) |
| 1298 | { |
| 1299 | inode->i_op = &fuse_symlink_inode_operations; |
| 1300 | } |