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