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