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