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