Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/file.h> |
| 13 | #include <linux/gfp.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/namei.h> |
| 16 | |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 17 | #if BITS_PER_LONG >= 64 |
| 18 | static inline void fuse_dentry_settime(struct dentry *entry, u64 time) |
| 19 | { |
| 20 | entry->d_time = time; |
| 21 | } |
| 22 | |
| 23 | static inline u64 fuse_dentry_time(struct dentry *entry) |
| 24 | { |
| 25 | return entry->d_time; |
| 26 | } |
| 27 | #else |
| 28 | /* |
| 29 | * On 32 bit archs store the high 32 bits of time in d_fsdata |
| 30 | */ |
| 31 | static void fuse_dentry_settime(struct dentry *entry, u64 time) |
| 32 | { |
| 33 | entry->d_time = time; |
| 34 | entry->d_fsdata = (void *) (unsigned long) (time >> 32); |
| 35 | } |
| 36 | |
| 37 | static u64 fuse_dentry_time(struct dentry *entry) |
| 38 | { |
| 39 | return (u64) entry->d_time + |
| 40 | ((u64) (unsigned long) entry->d_fsdata << 32); |
| 41 | } |
| 42 | #endif |
| 43 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 44 | /* |
| 45 | * FUSE caches dentries and attributes with separate timeout. The |
| 46 | * time in jiffies until the dentry/attributes are valid is stored in |
| 47 | * dentry->d_time and fuse_inode->i_time respectively. |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * Calculate the time in jiffies until a dentry/attributes are valid |
| 52 | */ |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 53 | static u64 time_to_jiffies(unsigned long sec, unsigned long nsec) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 54 | { |
Miklos Szeredi | 685d16d | 2006-07-30 03:04:08 -0700 | [diff] [blame] | 55 | if (sec || nsec) { |
| 56 | struct timespec ts = {sec, nsec}; |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 57 | return get_jiffies_64() + timespec_to_jiffies(&ts); |
Miklos Szeredi | 685d16d | 2006-07-30 03:04:08 -0700 | [diff] [blame] | 58 | } else |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 59 | return 0; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 62 | /* |
| 63 | * Set dentry and possibly attribute timeouts from the lookup/mk* |
| 64 | * replies |
| 65 | */ |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 66 | static void fuse_change_entry_timeout(struct dentry *entry, |
| 67 | struct fuse_entry_out *o) |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 68 | { |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 69 | fuse_dentry_settime(entry, |
| 70 | time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static u64 attr_timeout(struct fuse_attr_out *o) |
| 74 | { |
| 75 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); |
| 76 | } |
| 77 | |
| 78 | static u64 entry_attr_timeout(struct fuse_entry_out *o) |
| 79 | { |
| 80 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 83 | /* |
| 84 | * Mark the attributes as stale, so that at the next call to |
| 85 | * ->getattr() they will be fetched from userspace |
| 86 | */ |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 87 | void fuse_invalidate_attr(struct inode *inode) |
| 88 | { |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 89 | get_fuse_inode(inode)->i_time = 0; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 92 | /* |
| 93 | * Just mark the entry as stale, so that a next attempt to look it up |
| 94 | * will result in a new lookup call to userspace |
| 95 | * |
| 96 | * This is called when a dentry is about to become negative and the |
| 97 | * timeout is unknown (unlink, rmdir, rename and in some cases |
| 98 | * lookup) |
| 99 | */ |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 100 | static void fuse_invalidate_entry_cache(struct dentry *entry) |
| 101 | { |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 102 | fuse_dentry_settime(entry, 0); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 105 | /* |
| 106 | * Same as fuse_invalidate_entry_cache(), but also try to remove the |
| 107 | * dentry from the hash |
| 108 | */ |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 109 | static void fuse_invalidate_entry(struct dentry *entry) |
| 110 | { |
| 111 | d_invalidate(entry); |
| 112 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 115 | static void fuse_lookup_init(struct fuse_req *req, struct inode *dir, |
| 116 | struct dentry *entry, |
| 117 | struct fuse_entry_out *outarg) |
| 118 | { |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 119 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 120 | |
| 121 | memset(outarg, 0, sizeof(struct fuse_entry_out)); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 122 | req->in.h.opcode = FUSE_LOOKUP; |
| 123 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 124 | req->in.numargs = 1; |
| 125 | req->in.args[0].size = entry->d_name.len + 1; |
| 126 | req->in.args[0].value = entry->d_name.name; |
| 127 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 128 | if (fc->minor < 9) |
| 129 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
| 130 | else |
| 131 | req->out.args[0].size = sizeof(struct fuse_entry_out); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 132 | req->out.args[0].value = outarg; |
| 133 | } |
| 134 | |
Miklos Szeredi | 7dca9fd | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 135 | static u64 fuse_get_attr_version(struct fuse_conn *fc) |
| 136 | { |
| 137 | u64 curr_version; |
| 138 | |
| 139 | /* |
| 140 | * The spin lock isn't actually needed on 64bit archs, but we |
| 141 | * don't yet care too much about such optimizations. |
| 142 | */ |
| 143 | spin_lock(&fc->lock); |
| 144 | curr_version = fc->attr_version; |
| 145 | spin_unlock(&fc->lock); |
| 146 | |
| 147 | return curr_version; |
| 148 | } |
| 149 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 150 | /* |
| 151 | * Check whether the dentry is still valid |
| 152 | * |
| 153 | * If the entry validity timeout has expired and the dentry is |
| 154 | * positive, try to redo the lookup. If the lookup results in a |
| 155 | * different inode, then let the VFS invalidate the dentry and redo |
| 156 | * the lookup once more. If the lookup results in the same inode, |
| 157 | * then refresh the attributes, timeouts and mark the dentry valid. |
| 158 | */ |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 159 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) |
| 160 | { |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 161 | struct inode *inode = entry->d_inode; |
| 162 | |
| 163 | if (inode && is_bad_inode(inode)) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 164 | return 0; |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 165 | else if (fuse_dentry_time(entry) < get_jiffies_64()) { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 166 | int err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 167 | struct fuse_entry_out outarg; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 168 | struct fuse_conn *fc; |
| 169 | struct fuse_req *req; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 170 | struct fuse_req *forget_req; |
Miklos Szeredi | e956edd | 2006-10-17 00:10:12 -0700 | [diff] [blame] | 171 | struct dentry *parent; |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 172 | u64 attr_version; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 173 | |
Miklos Szeredi | 50322fe | 2006-02-28 16:59:03 -0800 | [diff] [blame] | 174 | /* For negative dentries, always do a fresh lookup */ |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 175 | if (!inode) |
| 176 | return 0; |
| 177 | |
| 178 | fc = get_fuse_conn(inode); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 179 | req = fuse_get_req(fc); |
| 180 | if (IS_ERR(req)) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 181 | return 0; |
| 182 | |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 183 | forget_req = fuse_get_req(fc); |
| 184 | if (IS_ERR(forget_req)) { |
| 185 | fuse_put_request(fc, req); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Miklos Szeredi | 7dca9fd | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 189 | attr_version = fuse_get_attr_version(fc); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 190 | |
Miklos Szeredi | e956edd | 2006-10-17 00:10:12 -0700 | [diff] [blame] | 191 | parent = dget_parent(entry); |
| 192 | fuse_lookup_init(req, parent->d_inode, entry, &outarg); |
Miklos Szeredi | 7c352bd | 2005-09-09 13:10:39 -0700 | [diff] [blame] | 193 | request_send(fc, req); |
Miklos Szeredi | e956edd | 2006-10-17 00:10:12 -0700 | [diff] [blame] | 194 | dput(parent); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 195 | err = req->out.h.error; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 196 | fuse_put_request(fc, req); |
Miklos Szeredi | 50322fe | 2006-02-28 16:59:03 -0800 | [diff] [blame] | 197 | /* Zero nodeid is same as -ENOENT */ |
| 198 | if (!err && !outarg.nodeid) |
| 199 | err = -ENOENT; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 200 | if (!err) { |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 201 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 202 | if (outarg.nodeid != get_node_id(inode)) { |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 203 | fuse_send_forget(fc, forget_req, |
| 204 | outarg.nodeid, 1); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 205 | return 0; |
| 206 | } |
Miklos Szeredi | 8da5ff2 | 2006-10-17 00:10:08 -0700 | [diff] [blame] | 207 | spin_lock(&fc->lock); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 208 | fi->nlookup ++; |
Miklos Szeredi | 8da5ff2 | 2006-10-17 00:10:08 -0700 | [diff] [blame] | 209 | spin_unlock(&fc->lock); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 210 | } |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 211 | fuse_put_request(fc, forget_req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 212 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 213 | return 0; |
| 214 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 215 | fuse_change_attributes(inode, &outarg.attr, |
| 216 | entry_attr_timeout(&outarg), |
| 217 | attr_version); |
| 218 | fuse_change_entry_timeout(entry, &outarg); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 219 | } |
| 220 | return 1; |
| 221 | } |
| 222 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 223 | static int invalid_nodeid(u64 nodeid) |
Miklos Szeredi | 2827d0b2 | 2005-11-28 13:44:16 -0800 | [diff] [blame] | 224 | { |
| 225 | return !nodeid || nodeid == FUSE_ROOT_ID; |
| 226 | } |
| 227 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 228 | static struct dentry_operations fuse_dentry_operations = { |
| 229 | .d_revalidate = fuse_dentry_revalidate, |
| 230 | }; |
| 231 | |
Timo Savola | a5bfffac | 2007-04-08 16:04:00 -0700 | [diff] [blame] | 232 | int fuse_valid_type(int m) |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 233 | { |
| 234 | return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || |
| 235 | S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); |
| 236 | } |
| 237 | |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Add a directory inode to a dentry, ensuring that no other dentry |
| 240 | * refers to this inode. Called with fc->inst_mutex. |
| 241 | */ |
| 242 | static int fuse_d_add_directory(struct dentry *entry, struct inode *inode) |
| 243 | { |
| 244 | struct dentry *alias = d_find_alias(inode); |
| 245 | if (alias) { |
| 246 | /* This tries to shrink the subtree below alias */ |
| 247 | fuse_invalidate_entry(alias); |
| 248 | dput(alias); |
| 249 | if (!list_empty(&inode->i_dentry)) |
| 250 | return -EBUSY; |
| 251 | } |
| 252 | d_add(entry, inode); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 256 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
| 257 | struct nameidata *nd) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 258 | { |
| 259 | int err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 260 | struct fuse_entry_out outarg; |
| 261 | struct inode *inode = NULL; |
| 262 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 263 | struct fuse_req *req; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 264 | struct fuse_req *forget_req; |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 265 | u64 attr_version; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 266 | |
| 267 | if (entry->d_name.len > FUSE_NAME_MAX) |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 268 | return ERR_PTR(-ENAMETOOLONG); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 269 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 270 | req = fuse_get_req(fc); |
| 271 | if (IS_ERR(req)) |
| 272 | return ERR_PTR(PTR_ERR(req)); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 273 | |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 274 | forget_req = fuse_get_req(fc); |
| 275 | if (IS_ERR(forget_req)) { |
| 276 | fuse_put_request(fc, req); |
| 277 | return ERR_PTR(PTR_ERR(forget_req)); |
| 278 | } |
| 279 | |
Miklos Szeredi | 7dca9fd | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 280 | attr_version = fuse_get_attr_version(fc); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 281 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 282 | fuse_lookup_init(req, dir, entry, &outarg); |
| 283 | request_send(fc, req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 284 | err = req->out.h.error; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 285 | fuse_put_request(fc, req); |
Miklos Szeredi | 50322fe | 2006-02-28 16:59:03 -0800 | [diff] [blame] | 286 | /* Zero nodeid is same as -ENOENT, but with valid timeout */ |
| 287 | if (!err && outarg.nodeid && |
Timo Savola | a5bfffac | 2007-04-08 16:04:00 -0700 | [diff] [blame] | 288 | (invalid_nodeid(outarg.nodeid) || |
| 289 | !fuse_valid_type(outarg.attr.mode))) |
Miklos Szeredi | ee4e527 | 2005-09-27 21:45:21 -0700 | [diff] [blame] | 290 | err = -EIO; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 291 | if (!err && outarg.nodeid) { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 292 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 293 | &outarg.attr, entry_attr_timeout(&outarg), |
| 294 | attr_version); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 295 | if (!inode) { |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 296 | fuse_send_forget(fc, forget_req, outarg.nodeid, 1); |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 297 | return ERR_PTR(-ENOMEM); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 300 | fuse_put_request(fc, forget_req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 301 | if (err && err != -ENOENT) |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 302 | return ERR_PTR(err); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 303 | |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 304 | if (inode && S_ISDIR(inode->i_mode)) { |
| 305 | mutex_lock(&fc->inst_mutex); |
| 306 | err = fuse_d_add_directory(entry, inode); |
| 307 | mutex_unlock(&fc->inst_mutex); |
| 308 | if (err) { |
| 309 | iput(inode); |
| 310 | return ERR_PTR(err); |
| 311 | } |
| 312 | } else |
| 313 | d_add(entry, inode); |
| 314 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 315 | entry->d_op = &fuse_dentry_operations; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 316 | if (!err) |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 317 | fuse_change_entry_timeout(entry, &outarg); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 318 | else |
| 319 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 320 | return NULL; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 323 | /* |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 324 | * Synchronous release for the case when something goes wrong in CREATE_OPEN |
| 325 | */ |
| 326 | static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff, |
| 327 | u64 nodeid, int flags) |
| 328 | { |
Miklos Szeredi | c756e0a | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 329 | fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE); |
| 330 | ff->reserved_req->force = 1; |
| 331 | request_send(fc, ff->reserved_req); |
| 332 | fuse_put_request(fc, ff->reserved_req); |
| 333 | kfree(ff); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 337 | * Atomic create+open operation |
| 338 | * |
| 339 | * If the filesystem doesn't support this, then fall back to separate |
| 340 | * 'mknod' + 'open' requests. |
| 341 | */ |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 342 | static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode, |
| 343 | struct nameidata *nd) |
| 344 | { |
| 345 | int err; |
| 346 | struct inode *inode; |
| 347 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 348 | struct fuse_req *req; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 349 | struct fuse_req *forget_req; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 350 | struct fuse_open_in inarg; |
| 351 | struct fuse_open_out outopen; |
| 352 | struct fuse_entry_out outentry; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 353 | struct fuse_file *ff; |
| 354 | struct file *file; |
| 355 | int flags = nd->intent.open.flags - 1; |
| 356 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 357 | if (fc->no_create) |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 358 | return -ENOSYS; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 359 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 360 | forget_req = fuse_get_req(fc); |
| 361 | if (IS_ERR(forget_req)) |
| 362 | return PTR_ERR(forget_req); |
| 363 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 364 | req = fuse_get_req(fc); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 365 | err = PTR_ERR(req); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 366 | if (IS_ERR(req)) |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 367 | goto out_put_forget_req; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 368 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 369 | err = -ENOMEM; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 370 | ff = fuse_file_alloc(); |
| 371 | if (!ff) |
| 372 | goto out_put_request; |
| 373 | |
| 374 | flags &= ~O_NOCTTY; |
| 375 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 376 | memset(&outentry, 0, sizeof(outentry)); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 377 | inarg.flags = flags; |
| 378 | inarg.mode = mode; |
| 379 | req->in.h.opcode = FUSE_CREATE; |
| 380 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 381 | req->in.numargs = 2; |
| 382 | req->in.args[0].size = sizeof(inarg); |
| 383 | req->in.args[0].value = &inarg; |
| 384 | req->in.args[1].size = entry->d_name.len + 1; |
| 385 | req->in.args[1].value = entry->d_name.name; |
| 386 | req->out.numargs = 2; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 387 | if (fc->minor < 9) |
| 388 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
| 389 | else |
| 390 | req->out.args[0].size = sizeof(outentry); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 391 | req->out.args[0].value = &outentry; |
| 392 | req->out.args[1].size = sizeof(outopen); |
| 393 | req->out.args[1].value = &outopen; |
| 394 | request_send(fc, req); |
| 395 | err = req->out.h.error; |
| 396 | if (err) { |
| 397 | if (err == -ENOSYS) |
| 398 | fc->no_create = 1; |
| 399 | goto out_free_ff; |
| 400 | } |
| 401 | |
| 402 | err = -EIO; |
Miklos Szeredi | 2827d0b2 | 2005-11-28 13:44:16 -0800 | [diff] [blame] | 403 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 404 | goto out_free_ff; |
| 405 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 406 | fuse_put_request(fc, req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 407 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 408 | &outentry.attr, entry_attr_timeout(&outentry), 0); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 409 | if (!inode) { |
| 410 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); |
| 411 | ff->fh = outopen.fh; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 412 | fuse_sync_release(fc, ff, outentry.nodeid, flags); |
| 413 | fuse_send_forget(fc, forget_req, outentry.nodeid, 1); |
| 414 | return -ENOMEM; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 415 | } |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 416 | fuse_put_request(fc, forget_req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 417 | d_instantiate(entry, inode); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 418 | fuse_change_entry_timeout(entry, &outentry); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 419 | file = lookup_instantiate_filp(nd, entry, generic_file_open); |
| 420 | if (IS_ERR(file)) { |
| 421 | ff->fh = outopen.fh; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 422 | fuse_sync_release(fc, ff, outentry.nodeid, flags); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 423 | return PTR_ERR(file); |
| 424 | } |
| 425 | fuse_finish_open(inode, file, ff, &outopen); |
| 426 | return 0; |
| 427 | |
| 428 | out_free_ff: |
| 429 | fuse_file_free(ff); |
| 430 | out_put_request: |
| 431 | fuse_put_request(fc, req); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 432 | out_put_forget_req: |
| 433 | fuse_put_request(fc, forget_req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 434 | return err; |
| 435 | } |
| 436 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 437 | /* |
| 438 | * Code shared between mknod, mkdir, symlink and link |
| 439 | */ |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 440 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
| 441 | struct inode *dir, struct dentry *entry, |
| 442 | int mode) |
| 443 | { |
| 444 | struct fuse_entry_out outarg; |
| 445 | struct inode *inode; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 446 | int err; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 447 | struct fuse_req *forget_req; |
| 448 | |
| 449 | forget_req = fuse_get_req(fc); |
| 450 | if (IS_ERR(forget_req)) { |
| 451 | fuse_put_request(fc, req); |
| 452 | return PTR_ERR(forget_req); |
| 453 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 454 | |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 455 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 456 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 457 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 458 | if (fc->minor < 9) |
| 459 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
| 460 | else |
| 461 | req->out.args[0].size = sizeof(outarg); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 462 | req->out.args[0].value = &outarg; |
| 463 | request_send(fc, req); |
| 464 | err = req->out.h.error; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 465 | fuse_put_request(fc, req); |
| 466 | if (err) |
| 467 | goto out_put_forget_req; |
| 468 | |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 469 | err = -EIO; |
| 470 | if (invalid_nodeid(outarg.nodeid)) |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 471 | goto out_put_forget_req; |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 472 | |
| 473 | if ((outarg.attr.mode ^ mode) & S_IFMT) |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 474 | goto out_put_forget_req; |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 475 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 476 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 477 | &outarg.attr, entry_attr_timeout(&outarg), 0); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 478 | if (!inode) { |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 479 | fuse_send_forget(fc, forget_req, outarg.nodeid, 1); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 480 | return -ENOMEM; |
| 481 | } |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 482 | fuse_put_request(fc, forget_req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 483 | |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 484 | if (S_ISDIR(inode->i_mode)) { |
| 485 | struct dentry *alias; |
| 486 | mutex_lock(&fc->inst_mutex); |
| 487 | alias = d_find_alias(inode); |
| 488 | if (alias) { |
| 489 | /* New directory must have moved since mkdir */ |
| 490 | mutex_unlock(&fc->inst_mutex); |
| 491 | dput(alias); |
| 492 | iput(inode); |
| 493 | return -EBUSY; |
| 494 | } |
| 495 | d_instantiate(entry, inode); |
| 496 | mutex_unlock(&fc->inst_mutex); |
| 497 | } else |
| 498 | d_instantiate(entry, inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 499 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 500 | fuse_change_entry_timeout(entry, &outarg); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 501 | fuse_invalidate_attr(dir); |
| 502 | return 0; |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 503 | |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 504 | out_put_forget_req: |
| 505 | fuse_put_request(fc, forget_req); |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 506 | return err; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, |
| 510 | dev_t rdev) |
| 511 | { |
| 512 | struct fuse_mknod_in inarg; |
| 513 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 514 | struct fuse_req *req = fuse_get_req(fc); |
| 515 | if (IS_ERR(req)) |
| 516 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 517 | |
| 518 | memset(&inarg, 0, sizeof(inarg)); |
| 519 | inarg.mode = mode; |
| 520 | inarg.rdev = new_encode_dev(rdev); |
| 521 | req->in.h.opcode = FUSE_MKNOD; |
| 522 | req->in.numargs = 2; |
| 523 | req->in.args[0].size = sizeof(inarg); |
| 524 | req->in.args[0].value = &inarg; |
| 525 | req->in.args[1].size = entry->d_name.len + 1; |
| 526 | req->in.args[1].value = entry->d_name.name; |
| 527 | return create_new_entry(fc, req, dir, entry, mode); |
| 528 | } |
| 529 | |
| 530 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, |
| 531 | struct nameidata *nd) |
| 532 | { |
Miklos Szeredi | b9ba347 | 2007-05-23 13:57:32 -0700 | [diff] [blame] | 533 | if (nd && (nd->flags & LOOKUP_OPEN)) { |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 534 | int err = fuse_create_open(dir, entry, mode, nd); |
| 535 | if (err != -ENOSYS) |
| 536 | return err; |
| 537 | /* Fall back on mknod */ |
| 538 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 539 | return fuse_mknod(dir, entry, mode, 0); |
| 540 | } |
| 541 | |
| 542 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) |
| 543 | { |
| 544 | struct fuse_mkdir_in inarg; |
| 545 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 546 | struct fuse_req *req = fuse_get_req(fc); |
| 547 | if (IS_ERR(req)) |
| 548 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 549 | |
| 550 | memset(&inarg, 0, sizeof(inarg)); |
| 551 | inarg.mode = mode; |
| 552 | req->in.h.opcode = FUSE_MKDIR; |
| 553 | req->in.numargs = 2; |
| 554 | req->in.args[0].size = sizeof(inarg); |
| 555 | req->in.args[0].value = &inarg; |
| 556 | req->in.args[1].size = entry->d_name.len + 1; |
| 557 | req->in.args[1].value = entry->d_name.name; |
| 558 | return create_new_entry(fc, req, dir, entry, S_IFDIR); |
| 559 | } |
| 560 | |
| 561 | static int fuse_symlink(struct inode *dir, struct dentry *entry, |
| 562 | const char *link) |
| 563 | { |
| 564 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 565 | unsigned len = strlen(link) + 1; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 566 | struct fuse_req *req = fuse_get_req(fc); |
| 567 | if (IS_ERR(req)) |
| 568 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 569 | |
| 570 | req->in.h.opcode = FUSE_SYMLINK; |
| 571 | req->in.numargs = 2; |
| 572 | req->in.args[0].size = entry->d_name.len + 1; |
| 573 | req->in.args[0].value = entry->d_name.name; |
| 574 | req->in.args[1].size = len; |
| 575 | req->in.args[1].value = link; |
| 576 | return create_new_entry(fc, req, dir, entry, S_IFLNK); |
| 577 | } |
| 578 | |
| 579 | static int fuse_unlink(struct inode *dir, struct dentry *entry) |
| 580 | { |
| 581 | int err; |
| 582 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 583 | struct fuse_req *req = fuse_get_req(fc); |
| 584 | if (IS_ERR(req)) |
| 585 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 586 | |
| 587 | req->in.h.opcode = FUSE_UNLINK; |
| 588 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 589 | req->in.numargs = 1; |
| 590 | req->in.args[0].size = entry->d_name.len + 1; |
| 591 | req->in.args[0].value = entry->d_name.name; |
| 592 | request_send(fc, req); |
| 593 | err = req->out.h.error; |
| 594 | fuse_put_request(fc, req); |
| 595 | if (!err) { |
| 596 | struct inode *inode = entry->d_inode; |
| 597 | |
| 598 | /* Set nlink to zero so the inode can be cleared, if |
| 599 | the inode does have more links this will be |
| 600 | discovered at the next lookup/getattr */ |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 601 | clear_nlink(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 602 | fuse_invalidate_attr(inode); |
| 603 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 604 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 605 | } else if (err == -EINTR) |
| 606 | fuse_invalidate_entry(entry); |
| 607 | return err; |
| 608 | } |
| 609 | |
| 610 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) |
| 611 | { |
| 612 | int err; |
| 613 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 614 | struct fuse_req *req = fuse_get_req(fc); |
| 615 | if (IS_ERR(req)) |
| 616 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 617 | |
| 618 | req->in.h.opcode = FUSE_RMDIR; |
| 619 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 620 | req->in.numargs = 1; |
| 621 | req->in.args[0].size = entry->d_name.len + 1; |
| 622 | req->in.args[0].value = entry->d_name.name; |
| 623 | request_send(fc, req); |
| 624 | err = req->out.h.error; |
| 625 | fuse_put_request(fc, req); |
| 626 | if (!err) { |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 627 | clear_nlink(entry->d_inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 628 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 629 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 630 | } else if (err == -EINTR) |
| 631 | fuse_invalidate_entry(entry); |
| 632 | return err; |
| 633 | } |
| 634 | |
| 635 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, |
| 636 | struct inode *newdir, struct dentry *newent) |
| 637 | { |
| 638 | int err; |
| 639 | struct fuse_rename_in inarg; |
| 640 | struct fuse_conn *fc = get_fuse_conn(olddir); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 641 | struct fuse_req *req = fuse_get_req(fc); |
| 642 | if (IS_ERR(req)) |
| 643 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 644 | |
| 645 | memset(&inarg, 0, sizeof(inarg)); |
| 646 | inarg.newdir = get_node_id(newdir); |
| 647 | req->in.h.opcode = FUSE_RENAME; |
| 648 | req->in.h.nodeid = get_node_id(olddir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 649 | req->in.numargs = 3; |
| 650 | req->in.args[0].size = sizeof(inarg); |
| 651 | req->in.args[0].value = &inarg; |
| 652 | req->in.args[1].size = oldent->d_name.len + 1; |
| 653 | req->in.args[1].value = oldent->d_name.name; |
| 654 | req->in.args[2].size = newent->d_name.len + 1; |
| 655 | req->in.args[2].value = newent->d_name.name; |
| 656 | request_send(fc, req); |
| 657 | err = req->out.h.error; |
| 658 | fuse_put_request(fc, req); |
| 659 | if (!err) { |
Miklos Szeredi | 08b6330 | 2007-11-28 16:22:03 -0800 | [diff] [blame] | 660 | /* ctime changes */ |
| 661 | fuse_invalidate_attr(oldent->d_inode); |
| 662 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 663 | fuse_invalidate_attr(olddir); |
| 664 | if (olddir != newdir) |
| 665 | fuse_invalidate_attr(newdir); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 666 | |
| 667 | /* newent will end up negative */ |
| 668 | if (newent->d_inode) |
| 669 | fuse_invalidate_entry_cache(newent); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 670 | } else if (err == -EINTR) { |
| 671 | /* If request was interrupted, DEITY only knows if the |
| 672 | rename actually took place. If the invalidation |
| 673 | fails (e.g. some process has CWD under the renamed |
| 674 | directory), then there can be inconsistency between |
| 675 | the dcache and the real filesystem. Tough luck. */ |
| 676 | fuse_invalidate_entry(oldent); |
| 677 | if (newent->d_inode) |
| 678 | fuse_invalidate_entry(newent); |
| 679 | } |
| 680 | |
| 681 | return err; |
| 682 | } |
| 683 | |
| 684 | static int fuse_link(struct dentry *entry, struct inode *newdir, |
| 685 | struct dentry *newent) |
| 686 | { |
| 687 | int err; |
| 688 | struct fuse_link_in inarg; |
| 689 | struct inode *inode = entry->d_inode; |
| 690 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 691 | struct fuse_req *req = fuse_get_req(fc); |
| 692 | if (IS_ERR(req)) |
| 693 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 694 | |
| 695 | memset(&inarg, 0, sizeof(inarg)); |
| 696 | inarg.oldnodeid = get_node_id(inode); |
| 697 | req->in.h.opcode = FUSE_LINK; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 698 | req->in.numargs = 2; |
| 699 | req->in.args[0].size = sizeof(inarg); |
| 700 | req->in.args[0].value = &inarg; |
| 701 | req->in.args[1].size = newent->d_name.len + 1; |
| 702 | req->in.args[1].value = newent->d_name.name; |
| 703 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); |
| 704 | /* Contrary to "normal" filesystems it can happen that link |
| 705 | makes two "logical" inodes point to the same "physical" |
| 706 | inode. We invalidate the attributes of the old one, so it |
| 707 | will reflect changes in the backing inode (link count, |
| 708 | etc.) |
| 709 | */ |
| 710 | if (!err || err == -EINTR) |
| 711 | fuse_invalidate_attr(inode); |
| 712 | return err; |
| 713 | } |
| 714 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 715 | static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr, |
| 716 | struct kstat *stat) |
| 717 | { |
| 718 | stat->dev = inode->i_sb->s_dev; |
| 719 | stat->ino = attr->ino; |
| 720 | stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); |
| 721 | stat->nlink = attr->nlink; |
| 722 | stat->uid = attr->uid; |
| 723 | stat->gid = attr->gid; |
| 724 | stat->rdev = inode->i_rdev; |
| 725 | stat->atime.tv_sec = attr->atime; |
| 726 | stat->atime.tv_nsec = attr->atimensec; |
| 727 | stat->mtime.tv_sec = attr->mtime; |
| 728 | stat->mtime.tv_nsec = attr->mtimensec; |
| 729 | stat->ctime.tv_sec = attr->ctime; |
| 730 | stat->ctime.tv_nsec = attr->ctimensec; |
| 731 | stat->size = attr->size; |
| 732 | stat->blocks = attr->blocks; |
| 733 | stat->blksize = (1 << inode->i_blkbits); |
| 734 | } |
| 735 | |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 736 | static int fuse_do_getattr(struct inode *inode, struct kstat *stat, |
| 737 | struct file *file) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 738 | { |
| 739 | int err; |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 740 | struct fuse_getattr_in inarg; |
| 741 | struct fuse_attr_out outarg; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 742 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 743 | struct fuse_req *req; |
| 744 | u64 attr_version; |
| 745 | |
| 746 | req = fuse_get_req(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 747 | if (IS_ERR(req)) |
| 748 | return PTR_ERR(req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 749 | |
Miklos Szeredi | 7dca9fd | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 750 | attr_version = fuse_get_attr_version(fc); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 751 | |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 752 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 753 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 754 | /* Directories have separate file-handle space */ |
| 755 | if (file && S_ISREG(inode->i_mode)) { |
| 756 | struct fuse_file *ff = file->private_data; |
| 757 | |
| 758 | inarg.getattr_flags |= FUSE_GETATTR_FH; |
| 759 | inarg.fh = ff->fh; |
| 760 | } |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 761 | req->in.h.opcode = FUSE_GETATTR; |
| 762 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 763 | req->in.numargs = 1; |
| 764 | req->in.args[0].size = sizeof(inarg); |
| 765 | req->in.args[0].value = &inarg; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 766 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 767 | if (fc->minor < 9) |
| 768 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; |
| 769 | else |
| 770 | req->out.args[0].size = sizeof(outarg); |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 771 | req->out.args[0].value = &outarg; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 772 | request_send(fc, req); |
| 773 | err = req->out.h.error; |
| 774 | fuse_put_request(fc, req); |
| 775 | if (!err) { |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 776 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 777 | make_bad_inode(inode); |
| 778 | err = -EIO; |
| 779 | } else { |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 780 | fuse_change_attributes(inode, &outarg.attr, |
| 781 | attr_timeout(&outarg), |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 782 | attr_version); |
| 783 | if (stat) |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 784 | fuse_fillattr(inode, &outarg.attr, stat); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | return err; |
| 788 | } |
| 789 | |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 790 | int fuse_update_attributes(struct inode *inode, struct kstat *stat, |
| 791 | struct file *file, bool *refreshed) |
| 792 | { |
| 793 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 794 | int err; |
| 795 | bool r; |
| 796 | |
| 797 | if (fi->i_time < get_jiffies_64()) { |
| 798 | r = true; |
| 799 | err = fuse_do_getattr(inode, stat, file); |
| 800 | } else { |
| 801 | r = false; |
| 802 | err = 0; |
| 803 | if (stat) { |
| 804 | generic_fillattr(inode, stat); |
| 805 | stat->mode = fi->orig_i_mode; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (refreshed != NULL) |
| 810 | *refreshed = r; |
| 811 | |
| 812 | return err; |
| 813 | } |
| 814 | |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 815 | /* |
| 816 | * Calling into a user-controlled filesystem gives the filesystem |
| 817 | * daemon ptrace-like capabilities over the requester process. This |
| 818 | * means, that the filesystem daemon is able to record the exact |
| 819 | * filesystem operations performed, and can also control the behavior |
| 820 | * of the requester process in otherwise impossible ways. For example |
| 821 | * it can delay the operation for arbitrary length of time allowing |
| 822 | * DoS against the requester. |
| 823 | * |
| 824 | * For this reason only those processes can call into the filesystem, |
| 825 | * for which the owner of the mount has ptrace privilege. This |
| 826 | * excludes processes started by other users, suid or sgid processes. |
| 827 | */ |
Miklos Szeredi | e57ac68 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 828 | int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task) |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 829 | { |
| 830 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 831 | return 1; |
| 832 | |
| 833 | if (task->euid == fc->user_id && |
| 834 | task->suid == fc->user_id && |
| 835 | task->uid == fc->user_id && |
| 836 | task->egid == fc->group_id && |
| 837 | task->sgid == fc->group_id && |
| 838 | task->gid == fc->group_id) |
| 839 | return 1; |
| 840 | |
| 841 | return 0; |
| 842 | } |
| 843 | |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 844 | static int fuse_access(struct inode *inode, int mask) |
| 845 | { |
| 846 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 847 | struct fuse_req *req; |
| 848 | struct fuse_access_in inarg; |
| 849 | int err; |
| 850 | |
| 851 | if (fc->no_access) |
| 852 | return 0; |
| 853 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 854 | req = fuse_get_req(fc); |
| 855 | if (IS_ERR(req)) |
| 856 | return PTR_ERR(req); |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 857 | |
| 858 | memset(&inarg, 0, sizeof(inarg)); |
| 859 | inarg.mask = mask; |
| 860 | req->in.h.opcode = FUSE_ACCESS; |
| 861 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 862 | req->in.numargs = 1; |
| 863 | req->in.args[0].size = sizeof(inarg); |
| 864 | req->in.args[0].value = &inarg; |
| 865 | request_send(fc, req); |
| 866 | err = req->out.h.error; |
| 867 | fuse_put_request(fc, req); |
| 868 | if (err == -ENOSYS) { |
| 869 | fc->no_access = 1; |
| 870 | err = 0; |
| 871 | } |
| 872 | return err; |
| 873 | } |
| 874 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 875 | /* |
| 876 | * Check permission. The two basic access models of FUSE are: |
| 877 | * |
| 878 | * 1) Local access checking ('default_permissions' mount option) based |
| 879 | * on file mode. This is the plain old disk filesystem permission |
| 880 | * modell. |
| 881 | * |
| 882 | * 2) "Remote" access checking, where server is responsible for |
| 883 | * checking permission in each inode operation. An exception to this |
| 884 | * is if ->permission() was invoked from sys_access() in which case an |
| 885 | * access request is sent. Execute permission is still checked |
| 886 | * locally based on file mode. |
| 887 | */ |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 888 | static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd) |
| 889 | { |
| 890 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 891 | bool refreshed = false; |
| 892 | int err = 0; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 893 | |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 894 | if (!fuse_allow_task(fc, current)) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 895 | return -EACCES; |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 896 | |
| 897 | /* |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 898 | * If attributes are needed, refresh them before proceeding |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 899 | */ |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 900 | if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) || |
| 901 | ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 902 | err = fuse_update_attributes(inode, NULL, NULL, &refreshed); |
| 903 | if (err) |
| 904 | return err; |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 908 | int err = generic_permission(inode, mask, NULL); |
| 909 | |
| 910 | /* If permission is denied, try to refresh file |
| 911 | attributes. This is also needed, because the root |
| 912 | node will at first have no permissions */ |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 913 | if (err == -EACCES && !refreshed) { |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 914 | err = fuse_do_getattr(inode, NULL, NULL); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 915 | if (!err) |
| 916 | err = generic_permission(inode, mask, NULL); |
| 917 | } |
| 918 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 919 | /* Note: the opposite of the above test does not |
| 920 | exist. So if permissions are revoked this won't be |
| 921 | noticed immediately, only after the attribute |
| 922 | timeout has expired */ |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 923 | } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) { |
| 924 | err = fuse_access(inode, mask); |
| 925 | } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { |
| 926 | if (!(inode->i_mode & S_IXUGO)) { |
| 927 | if (refreshed) |
| 928 | return -EACCES; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 929 | |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 930 | err = fuse_do_getattr(inode, NULL, NULL); |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 931 | if (!err && !(inode->i_mode & S_IXUGO)) |
| 932 | return -EACCES; |
| 933 | } |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 934 | } |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 935 | return err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, |
| 939 | void *dstbuf, filldir_t filldir) |
| 940 | { |
| 941 | while (nbytes >= FUSE_NAME_OFFSET) { |
| 942 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; |
| 943 | size_t reclen = FUSE_DIRENT_SIZE(dirent); |
| 944 | int over; |
| 945 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) |
| 946 | return -EIO; |
| 947 | if (reclen > nbytes) |
| 948 | break; |
| 949 | |
| 950 | over = filldir(dstbuf, dirent->name, dirent->namelen, |
| 951 | file->f_pos, dirent->ino, dirent->type); |
| 952 | if (over) |
| 953 | break; |
| 954 | |
| 955 | buf += reclen; |
| 956 | nbytes -= reclen; |
| 957 | file->f_pos = dirent->off; |
| 958 | } |
| 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 963 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
| 964 | { |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 965 | int err; |
| 966 | size_t nbytes; |
| 967 | struct page *page; |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 968 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 969 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 970 | struct fuse_req *req; |
| 971 | |
| 972 | if (is_bad_inode(inode)) |
| 973 | return -EIO; |
| 974 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 975 | req = fuse_get_req(fc); |
| 976 | if (IS_ERR(req)) |
| 977 | return PTR_ERR(req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 978 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 979 | page = alloc_page(GFP_KERNEL); |
| 980 | if (!page) { |
| 981 | fuse_put_request(fc, req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 982 | return -ENOMEM; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 983 | } |
| 984 | req->num_pages = 1; |
| 985 | req->pages[0] = page; |
Miklos Szeredi | a664309 | 2007-11-28 16:22:00 -0800 | [diff] [blame] | 986 | fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR); |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 987 | request_send(fc, req); |
| 988 | nbytes = req->out.args[0].size; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 989 | err = req->out.h.error; |
| 990 | fuse_put_request(fc, req); |
| 991 | if (!err) |
| 992 | err = parse_dirfile(page_address(page), nbytes, file, dstbuf, |
| 993 | filldir); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 994 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 995 | __free_page(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 996 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 997 | return err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static char *read_link(struct dentry *dentry) |
| 1001 | { |
| 1002 | struct inode *inode = dentry->d_inode; |
| 1003 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1004 | struct fuse_req *req = fuse_get_req(fc); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1005 | char *link; |
| 1006 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1007 | if (IS_ERR(req)) |
| 1008 | return ERR_PTR(PTR_ERR(req)); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1009 | |
| 1010 | link = (char *) __get_free_page(GFP_KERNEL); |
| 1011 | if (!link) { |
| 1012 | link = ERR_PTR(-ENOMEM); |
| 1013 | goto out; |
| 1014 | } |
| 1015 | req->in.h.opcode = FUSE_READLINK; |
| 1016 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1017 | req->out.argvar = 1; |
| 1018 | req->out.numargs = 1; |
| 1019 | req->out.args[0].size = PAGE_SIZE - 1; |
| 1020 | req->out.args[0].value = link; |
| 1021 | request_send(fc, req); |
| 1022 | if (req->out.h.error) { |
| 1023 | free_page((unsigned long) link); |
| 1024 | link = ERR_PTR(req->out.h.error); |
| 1025 | } else |
| 1026 | link[req->out.args[0].size] = '\0'; |
| 1027 | out: |
| 1028 | fuse_put_request(fc, req); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1029 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1030 | return link; |
| 1031 | } |
| 1032 | |
| 1033 | static void free_link(char *link) |
| 1034 | { |
| 1035 | if (!IS_ERR(link)) |
| 1036 | free_page((unsigned long) link); |
| 1037 | } |
| 1038 | |
| 1039 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 1040 | { |
| 1041 | nd_set_link(nd, read_link(dentry)); |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | |
| 1045 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) |
| 1046 | { |
| 1047 | free_link(nd_get_link(nd)); |
| 1048 | } |
| 1049 | |
| 1050 | static int fuse_dir_open(struct inode *inode, struct file *file) |
| 1051 | { |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1052 | return fuse_open_common(inode, file, 1); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static int fuse_dir_release(struct inode *inode, struct file *file) |
| 1056 | { |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1057 | return fuse_release_common(inode, file, 1); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1060 | static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync) |
| 1061 | { |
| 1062 | /* nfsd can call this with no file */ |
| 1063 | return file ? fuse_fsync_common(file, de, datasync, 1) : 0; |
| 1064 | } |
| 1065 | |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1066 | static bool update_mtime(unsigned ivalid) |
| 1067 | { |
| 1068 | /* Always update if mtime is explicitly set */ |
| 1069 | if (ivalid & ATTR_MTIME_SET) |
| 1070 | return true; |
| 1071 | |
| 1072 | /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ |
| 1073 | if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) |
| 1074 | return false; |
| 1075 | |
| 1076 | /* In all other cases update */ |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1080 | static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg) |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1081 | { |
| 1082 | unsigned ivalid = iattr->ia_valid; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1083 | |
| 1084 | if (ivalid & ATTR_MODE) |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1085 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1086 | if (ivalid & ATTR_UID) |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1087 | arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1088 | if (ivalid & ATTR_GID) |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1089 | arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1090 | if (ivalid & ATTR_SIZE) |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1091 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1092 | if (ivalid & ATTR_ATIME) { |
| 1093 | arg->valid |= FATTR_ATIME; |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1094 | arg->atime = iattr->ia_atime.tv_sec; |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1095 | arg->atimensec = iattr->ia_atime.tv_nsec; |
| 1096 | if (!(ivalid & ATTR_ATIME_SET)) |
| 1097 | arg->valid |= FATTR_ATIME_NOW; |
| 1098 | } |
| 1099 | if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) { |
| 1100 | arg->valid |= FATTR_MTIME; |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1101 | arg->mtime = iattr->ia_mtime.tv_sec; |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1102 | arg->mtimensec = iattr->ia_mtime.tv_nsec; |
| 1103 | if (!(ivalid & ATTR_MTIME_SET)) |
| 1104 | arg->valid |= FATTR_MTIME_NOW; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1105 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1108 | /* |
| 1109 | * Set attributes, and at the same time refresh them. |
| 1110 | * |
| 1111 | * Truncation is slightly complicated, because the 'truncate' request |
| 1112 | * may fail, in which case we don't want to touch the mapping. |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 1113 | * vmtruncate() doesn't allow for this case, so do the rlimit checking |
| 1114 | * and the actual truncation by hand. |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1115 | */ |
Miklos Szeredi | 49d4914 | 2007-10-18 03:07:00 -0700 | [diff] [blame] | 1116 | static int fuse_do_setattr(struct dentry *entry, struct iattr *attr, |
| 1117 | struct file *file) |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1118 | { |
| 1119 | struct inode *inode = entry->d_inode; |
| 1120 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1121 | struct fuse_req *req; |
| 1122 | struct fuse_setattr_in inarg; |
| 1123 | struct fuse_attr_out outarg; |
| 1124 | int err; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1125 | |
Miklos Szeredi | e57ac68 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 1126 | if (!fuse_allow_task(fc, current)) |
| 1127 | return -EACCES; |
| 1128 | |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1129 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
| 1130 | err = inode_change_ok(inode, attr); |
| 1131 | if (err) |
| 1132 | return err; |
| 1133 | } |
| 1134 | |
Miklos Szeredi | 6ff958e | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1135 | if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc) |
| 1136 | return 0; |
| 1137 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1138 | if (attr->ia_valid & ATTR_SIZE) { |
| 1139 | unsigned long limit; |
Miklos Szeredi | b2d2272 | 2006-12-06 20:35:51 -0800 | [diff] [blame] | 1140 | if (IS_SWAPFILE(inode)) |
| 1141 | return -ETXTBSY; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1142 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
| 1143 | if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) { |
| 1144 | send_sig(SIGXFSZ, current, 0); |
| 1145 | return -EFBIG; |
| 1146 | } |
| 1147 | } |
| 1148 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1149 | req = fuse_get_req(fc); |
| 1150 | if (IS_ERR(req)) |
| 1151 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1152 | |
| 1153 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 1154 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1155 | iattr_to_fattr(attr, &inarg); |
Miklos Szeredi | 49d4914 | 2007-10-18 03:07:00 -0700 | [diff] [blame] | 1156 | if (file) { |
| 1157 | struct fuse_file *ff = file->private_data; |
| 1158 | inarg.valid |= FATTR_FH; |
| 1159 | inarg.fh = ff->fh; |
| 1160 | } |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 1161 | if (attr->ia_valid & ATTR_SIZE) { |
| 1162 | /* For mandatory locking in truncate */ |
| 1163 | inarg.valid |= FATTR_LOCKOWNER; |
| 1164 | inarg.lock_owner = fuse_lock_owner_id(fc, current->files); |
| 1165 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1166 | req->in.h.opcode = FUSE_SETATTR; |
| 1167 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1168 | req->in.numargs = 1; |
| 1169 | req->in.args[0].size = sizeof(inarg); |
| 1170 | req->in.args[0].value = &inarg; |
| 1171 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 1172 | if (fc->minor < 9) |
| 1173 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; |
| 1174 | else |
| 1175 | req->out.args[0].size = sizeof(outarg); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1176 | req->out.args[0].value = &outarg; |
| 1177 | request_send(fc, req); |
| 1178 | err = req->out.h.error; |
| 1179 | fuse_put_request(fc, req); |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1180 | if (err) { |
| 1181 | if (err == -EINTR) |
| 1182 | fuse_invalidate_attr(inode); |
| 1183 | return err; |
| 1184 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1185 | |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1186 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
| 1187 | make_bad_inode(inode); |
| 1188 | return -EIO; |
| 1189 | } |
| 1190 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 1191 | fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0); |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1192 | return 0; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Miklos Szeredi | 49d4914 | 2007-10-18 03:07:00 -0700 | [diff] [blame] | 1195 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
| 1196 | { |
| 1197 | if (attr->ia_valid & ATTR_FILE) |
| 1198 | return fuse_do_setattr(entry, attr, attr->ia_file); |
| 1199 | else |
| 1200 | return fuse_do_setattr(entry, attr, NULL); |
| 1201 | } |
| 1202 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1203 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
| 1204 | struct kstat *stat) |
| 1205 | { |
| 1206 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1207 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1208 | |
| 1209 | if (!fuse_allow_task(fc, current)) |
| 1210 | return -EACCES; |
| 1211 | |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 1212 | return fuse_update_attributes(inode, stat, NULL, NULL); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1215 | static int fuse_setxattr(struct dentry *entry, const char *name, |
| 1216 | const void *value, size_t size, int flags) |
| 1217 | { |
| 1218 | struct inode *inode = entry->d_inode; |
| 1219 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1220 | struct fuse_req *req; |
| 1221 | struct fuse_setxattr_in inarg; |
| 1222 | int err; |
| 1223 | |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1224 | if (fc->no_setxattr) |
| 1225 | return -EOPNOTSUPP; |
| 1226 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1227 | req = fuse_get_req(fc); |
| 1228 | if (IS_ERR(req)) |
| 1229 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1230 | |
| 1231 | memset(&inarg, 0, sizeof(inarg)); |
| 1232 | inarg.size = size; |
| 1233 | inarg.flags = flags; |
| 1234 | req->in.h.opcode = FUSE_SETXATTR; |
| 1235 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1236 | req->in.numargs = 3; |
| 1237 | req->in.args[0].size = sizeof(inarg); |
| 1238 | req->in.args[0].value = &inarg; |
| 1239 | req->in.args[1].size = strlen(name) + 1; |
| 1240 | req->in.args[1].value = name; |
| 1241 | req->in.args[2].size = size; |
| 1242 | req->in.args[2].value = value; |
| 1243 | request_send(fc, req); |
| 1244 | err = req->out.h.error; |
| 1245 | fuse_put_request(fc, req); |
| 1246 | if (err == -ENOSYS) { |
| 1247 | fc->no_setxattr = 1; |
| 1248 | err = -EOPNOTSUPP; |
| 1249 | } |
| 1250 | return err; |
| 1251 | } |
| 1252 | |
| 1253 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, |
| 1254 | void *value, size_t size) |
| 1255 | { |
| 1256 | struct inode *inode = entry->d_inode; |
| 1257 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1258 | struct fuse_req *req; |
| 1259 | struct fuse_getxattr_in inarg; |
| 1260 | struct fuse_getxattr_out outarg; |
| 1261 | ssize_t ret; |
| 1262 | |
| 1263 | if (fc->no_getxattr) |
| 1264 | return -EOPNOTSUPP; |
| 1265 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1266 | req = fuse_get_req(fc); |
| 1267 | if (IS_ERR(req)) |
| 1268 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1269 | |
| 1270 | memset(&inarg, 0, sizeof(inarg)); |
| 1271 | inarg.size = size; |
| 1272 | req->in.h.opcode = FUSE_GETXATTR; |
| 1273 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1274 | req->in.numargs = 2; |
| 1275 | req->in.args[0].size = sizeof(inarg); |
| 1276 | req->in.args[0].value = &inarg; |
| 1277 | req->in.args[1].size = strlen(name) + 1; |
| 1278 | req->in.args[1].value = name; |
| 1279 | /* This is really two different operations rolled into one */ |
| 1280 | req->out.numargs = 1; |
| 1281 | if (size) { |
| 1282 | req->out.argvar = 1; |
| 1283 | req->out.args[0].size = size; |
| 1284 | req->out.args[0].value = value; |
| 1285 | } else { |
| 1286 | req->out.args[0].size = sizeof(outarg); |
| 1287 | req->out.args[0].value = &outarg; |
| 1288 | } |
| 1289 | request_send(fc, req); |
| 1290 | ret = req->out.h.error; |
| 1291 | if (!ret) |
| 1292 | ret = size ? req->out.args[0].size : outarg.size; |
| 1293 | else { |
| 1294 | if (ret == -ENOSYS) { |
| 1295 | fc->no_getxattr = 1; |
| 1296 | ret = -EOPNOTSUPP; |
| 1297 | } |
| 1298 | } |
| 1299 | fuse_put_request(fc, req); |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
| 1303 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) |
| 1304 | { |
| 1305 | struct inode *inode = entry->d_inode; |
| 1306 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1307 | struct fuse_req *req; |
| 1308 | struct fuse_getxattr_in inarg; |
| 1309 | struct fuse_getxattr_out outarg; |
| 1310 | ssize_t ret; |
| 1311 | |
Miklos Szeredi | e57ac68 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 1312 | if (!fuse_allow_task(fc, current)) |
| 1313 | return -EACCES; |
| 1314 | |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1315 | if (fc->no_listxattr) |
| 1316 | return -EOPNOTSUPP; |
| 1317 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1318 | req = fuse_get_req(fc); |
| 1319 | if (IS_ERR(req)) |
| 1320 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1321 | |
| 1322 | memset(&inarg, 0, sizeof(inarg)); |
| 1323 | inarg.size = size; |
| 1324 | req->in.h.opcode = FUSE_LISTXATTR; |
| 1325 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1326 | req->in.numargs = 1; |
| 1327 | req->in.args[0].size = sizeof(inarg); |
| 1328 | req->in.args[0].value = &inarg; |
| 1329 | /* This is really two different operations rolled into one */ |
| 1330 | req->out.numargs = 1; |
| 1331 | if (size) { |
| 1332 | req->out.argvar = 1; |
| 1333 | req->out.args[0].size = size; |
| 1334 | req->out.args[0].value = list; |
| 1335 | } else { |
| 1336 | req->out.args[0].size = sizeof(outarg); |
| 1337 | req->out.args[0].value = &outarg; |
| 1338 | } |
| 1339 | request_send(fc, req); |
| 1340 | ret = req->out.h.error; |
| 1341 | if (!ret) |
| 1342 | ret = size ? req->out.args[0].size : outarg.size; |
| 1343 | else { |
| 1344 | if (ret == -ENOSYS) { |
| 1345 | fc->no_listxattr = 1; |
| 1346 | ret = -EOPNOTSUPP; |
| 1347 | } |
| 1348 | } |
| 1349 | fuse_put_request(fc, req); |
| 1350 | return ret; |
| 1351 | } |
| 1352 | |
| 1353 | static int fuse_removexattr(struct dentry *entry, const char *name) |
| 1354 | { |
| 1355 | struct inode *inode = entry->d_inode; |
| 1356 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1357 | struct fuse_req *req; |
| 1358 | int err; |
| 1359 | |
| 1360 | if (fc->no_removexattr) |
| 1361 | return -EOPNOTSUPP; |
| 1362 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1363 | req = fuse_get_req(fc); |
| 1364 | if (IS_ERR(req)) |
| 1365 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1366 | |
| 1367 | req->in.h.opcode = FUSE_REMOVEXATTR; |
| 1368 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1369 | req->in.numargs = 1; |
| 1370 | req->in.args[0].size = strlen(name) + 1; |
| 1371 | req->in.args[0].value = name; |
| 1372 | request_send(fc, req); |
| 1373 | err = req->out.h.error; |
| 1374 | fuse_put_request(fc, req); |
| 1375 | if (err == -ENOSYS) { |
| 1376 | fc->no_removexattr = 1; |
| 1377 | err = -EOPNOTSUPP; |
| 1378 | } |
| 1379 | return err; |
| 1380 | } |
| 1381 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1382 | static const struct inode_operations fuse_dir_inode_operations = { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1383 | .lookup = fuse_lookup, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1384 | .mkdir = fuse_mkdir, |
| 1385 | .symlink = fuse_symlink, |
| 1386 | .unlink = fuse_unlink, |
| 1387 | .rmdir = fuse_rmdir, |
| 1388 | .rename = fuse_rename, |
| 1389 | .link = fuse_link, |
| 1390 | .setattr = fuse_setattr, |
| 1391 | .create = fuse_create, |
| 1392 | .mknod = fuse_mknod, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1393 | .permission = fuse_permission, |
| 1394 | .getattr = fuse_getattr, |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1395 | .setxattr = fuse_setxattr, |
| 1396 | .getxattr = fuse_getxattr, |
| 1397 | .listxattr = fuse_listxattr, |
| 1398 | .removexattr = fuse_removexattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1399 | }; |
| 1400 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1401 | static const struct file_operations fuse_dir_operations = { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1402 | .llseek = generic_file_llseek, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1403 | .read = generic_read_dir, |
| 1404 | .readdir = fuse_readdir, |
| 1405 | .open = fuse_dir_open, |
| 1406 | .release = fuse_dir_release, |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1407 | .fsync = fuse_dir_fsync, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1408 | }; |
| 1409 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1410 | static const struct inode_operations fuse_common_inode_operations = { |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1411 | .setattr = fuse_setattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1412 | .permission = fuse_permission, |
| 1413 | .getattr = fuse_getattr, |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1414 | .setxattr = fuse_setxattr, |
| 1415 | .getxattr = fuse_getxattr, |
| 1416 | .listxattr = fuse_listxattr, |
| 1417 | .removexattr = fuse_removexattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1418 | }; |
| 1419 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1420 | static const struct inode_operations fuse_symlink_inode_operations = { |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1421 | .setattr = fuse_setattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1422 | .follow_link = fuse_follow_link, |
| 1423 | .put_link = fuse_put_link, |
| 1424 | .readlink = generic_readlink, |
| 1425 | .getattr = fuse_getattr, |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1426 | .setxattr = fuse_setxattr, |
| 1427 | .getxattr = fuse_getxattr, |
| 1428 | .listxattr = fuse_listxattr, |
| 1429 | .removexattr = fuse_removexattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1430 | }; |
| 1431 | |
| 1432 | void fuse_init_common(struct inode *inode) |
| 1433 | { |
| 1434 | inode->i_op = &fuse_common_inode_operations; |
| 1435 | } |
| 1436 | |
| 1437 | void fuse_init_dir(struct inode *inode) |
| 1438 | { |
| 1439 | inode->i_op = &fuse_dir_inode_operations; |
| 1440 | inode->i_fop = &fuse_dir_operations; |
| 1441 | } |
| 1442 | |
| 1443 | void fuse_init_symlink(struct inode *inode) |
| 1444 | { |
| 1445 | inode->i_op = &fuse_symlink_inode_operations; |
| 1446 | } |