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