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