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