Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 3 | Copyright (C) 2001-2008 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> |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 13 | #include <linux/sched.h> |
| 14 | #include <linux/namei.h> |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 15 | #include <linux/slab.h> |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 16 | |
Feng Shuo | 4582a4a | 2013-01-15 11:23:28 +0800 | [diff] [blame] | 17 | static bool fuse_use_readdirplus(struct inode *dir, struct file *filp) |
| 18 | { |
| 19 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 20 | struct fuse_inode *fi = get_fuse_inode(dir); |
| 21 | |
| 22 | if (!fc->do_readdirplus) |
| 23 | return false; |
| 24 | if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state)) |
| 25 | return true; |
| 26 | if (filp->f_pos == 0) |
| 27 | return true; |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | static void fuse_advise_use_readdirplus(struct inode *dir) |
| 32 | { |
| 33 | struct fuse_inode *fi = get_fuse_inode(dir); |
| 34 | |
| 35 | set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state); |
| 36 | } |
| 37 | |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 38 | #if BITS_PER_LONG >= 64 |
| 39 | static inline void fuse_dentry_settime(struct dentry *entry, u64 time) |
| 40 | { |
| 41 | entry->d_time = time; |
| 42 | } |
| 43 | |
| 44 | static inline u64 fuse_dentry_time(struct dentry *entry) |
| 45 | { |
| 46 | return entry->d_time; |
| 47 | } |
| 48 | #else |
| 49 | /* |
| 50 | * On 32 bit archs store the high 32 bits of time in d_fsdata |
| 51 | */ |
| 52 | static void fuse_dentry_settime(struct dentry *entry, u64 time) |
| 53 | { |
| 54 | entry->d_time = time; |
| 55 | entry->d_fsdata = (void *) (unsigned long) (time >> 32); |
| 56 | } |
| 57 | |
| 58 | static u64 fuse_dentry_time(struct dentry *entry) |
| 59 | { |
| 60 | return (u64) entry->d_time + |
| 61 | ((u64) (unsigned long) entry->d_fsdata << 32); |
| 62 | } |
| 63 | #endif |
| 64 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 65 | /* |
| 66 | * FUSE caches dentries and attributes with separate timeout. The |
| 67 | * time in jiffies until the dentry/attributes are valid is stored in |
| 68 | * dentry->d_time and fuse_inode->i_time respectively. |
| 69 | */ |
| 70 | |
| 71 | /* |
| 72 | * Calculate the time in jiffies until a dentry/attributes are valid |
| 73 | */ |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 74 | static u64 time_to_jiffies(unsigned long sec, unsigned long nsec) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 75 | { |
Miklos Szeredi | 685d16d | 2006-07-30 03:04:08 -0700 | [diff] [blame] | 76 | if (sec || nsec) { |
| 77 | struct timespec ts = {sec, nsec}; |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 78 | return get_jiffies_64() + timespec_to_jiffies(&ts); |
Miklos Szeredi | 685d16d | 2006-07-30 03:04:08 -0700 | [diff] [blame] | 79 | } else |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 80 | return 0; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 83 | /* |
| 84 | * Set dentry and possibly attribute timeouts from the lookup/mk* |
| 85 | * replies |
| 86 | */ |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 87 | static void fuse_change_entry_timeout(struct dentry *entry, |
| 88 | struct fuse_entry_out *o) |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 89 | { |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 90 | fuse_dentry_settime(entry, |
| 91 | time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static u64 attr_timeout(struct fuse_attr_out *o) |
| 95 | { |
| 96 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); |
| 97 | } |
| 98 | |
| 99 | static u64 entry_attr_timeout(struct fuse_entry_out *o) |
| 100 | { |
| 101 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 104 | /* |
| 105 | * Mark the attributes as stale, so that at the next call to |
| 106 | * ->getattr() they will be fetched from userspace |
| 107 | */ |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 108 | void fuse_invalidate_attr(struct inode *inode) |
| 109 | { |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 110 | get_fuse_inode(inode)->i_time = 0; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 113 | /* |
| 114 | * Just mark the entry as stale, so that a next attempt to look it up |
| 115 | * will result in a new lookup call to userspace |
| 116 | * |
| 117 | * This is called when a dentry is about to become negative and the |
| 118 | * timeout is unknown (unlink, rmdir, rename and in some cases |
| 119 | * lookup) |
| 120 | */ |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 121 | void fuse_invalidate_entry_cache(struct dentry *entry) |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 122 | { |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 123 | fuse_dentry_settime(entry, 0); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 126 | /* |
| 127 | * Same as fuse_invalidate_entry_cache(), but also try to remove the |
| 128 | * dentry from the hash |
| 129 | */ |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 130 | static void fuse_invalidate_entry(struct dentry *entry) |
| 131 | { |
| 132 | d_invalidate(entry); |
| 133 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 136 | static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req, |
| 137 | u64 nodeid, struct qstr *name, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 138 | struct fuse_entry_out *outarg) |
| 139 | { |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 140 | memset(outarg, 0, sizeof(struct fuse_entry_out)); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 141 | req->in.h.opcode = FUSE_LOOKUP; |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 142 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 143 | req->in.numargs = 1; |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 144 | req->in.args[0].size = name->len + 1; |
| 145 | req->in.args[0].value = name->name; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 146 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 147 | if (fc->minor < 9) |
| 148 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
| 149 | else |
| 150 | req->out.args[0].size = sizeof(struct fuse_entry_out); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 151 | req->out.args[0].value = outarg; |
| 152 | } |
| 153 | |
Miklos Szeredi | 5c5c5e5 | 2008-04-30 00:54:43 -0700 | [diff] [blame] | 154 | u64 fuse_get_attr_version(struct fuse_conn *fc) |
Miklos Szeredi | 7dca9fd | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 155 | { |
| 156 | u64 curr_version; |
| 157 | |
| 158 | /* |
| 159 | * The spin lock isn't actually needed on 64bit archs, but we |
| 160 | * don't yet care too much about such optimizations. |
| 161 | */ |
| 162 | spin_lock(&fc->lock); |
| 163 | curr_version = fc->attr_version; |
| 164 | spin_unlock(&fc->lock); |
| 165 | |
| 166 | return curr_version; |
| 167 | } |
| 168 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 169 | /* |
| 170 | * Check whether the dentry is still valid |
| 171 | * |
| 172 | * If the entry validity timeout has expired and the dentry is |
| 173 | * positive, try to redo the lookup. If the lookup results in a |
| 174 | * different inode, then let the VFS invalidate the dentry and redo |
| 175 | * the lookup once more. If the lookup results in the same inode, |
| 176 | * then refresh the attributes, timeouts and mark the dentry valid. |
| 177 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 178 | static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 179 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 180 | struct inode *inode; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 181 | |
Miklos Szeredi | e7c0a16 | 2011-03-21 13:58:06 +0100 | [diff] [blame] | 182 | inode = ACCESS_ONCE(entry->d_inode); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 183 | if (inode && is_bad_inode(inode)) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 184 | return 0; |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 185 | else if (fuse_dentry_time(entry) < get_jiffies_64()) { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 186 | int err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 187 | struct fuse_entry_out outarg; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 188 | struct fuse_conn *fc; |
| 189 | struct fuse_req *req; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 190 | struct fuse_forget_link *forget; |
Miklos Szeredi | e956edd | 2006-10-17 00:10:12 -0700 | [diff] [blame] | 191 | struct dentry *parent; |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 192 | u64 attr_version; |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 193 | |
Miklos Szeredi | 50322fe | 2006-02-28 16:59:03 -0800 | [diff] [blame] | 194 | /* For negative dentries, always do a fresh lookup */ |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 195 | if (!inode) |
| 196 | return 0; |
| 197 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 198 | if (flags & LOOKUP_RCU) |
Miklos Szeredi | e7c0a16 | 2011-03-21 13:58:06 +0100 | [diff] [blame] | 199 | return -ECHILD; |
| 200 | |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 201 | fc = get_fuse_conn(inode); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 202 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 203 | if (IS_ERR(req)) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 206 | forget = fuse_alloc_forget(); |
| 207 | if (!forget) { |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 208 | fuse_put_request(fc, req); |
| 209 | return 0; |
| 210 | } |
| 211 | |
Miklos Szeredi | 7dca9fd | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 212 | attr_version = fuse_get_attr_version(fc); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 213 | |
Miklos Szeredi | e956edd | 2006-10-17 00:10:12 -0700 | [diff] [blame] | 214 | parent = dget_parent(entry); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 215 | fuse_lookup_init(fc, req, get_node_id(parent->d_inode), |
| 216 | &entry->d_name, &outarg); |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 217 | fuse_request_send(fc, req); |
Miklos Szeredi | e956edd | 2006-10-17 00:10:12 -0700 | [diff] [blame] | 218 | dput(parent); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 219 | err = req->out.h.error; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 220 | fuse_put_request(fc, req); |
Miklos Szeredi | 50322fe | 2006-02-28 16:59:03 -0800 | [diff] [blame] | 221 | /* Zero nodeid is same as -ENOENT */ |
| 222 | if (!err && !outarg.nodeid) |
| 223 | err = -ENOENT; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 224 | if (!err) { |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 225 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 226 | if (outarg.nodeid != get_node_id(inode)) { |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 227 | fuse_queue_forget(fc, forget, outarg.nodeid, 1); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 228 | return 0; |
| 229 | } |
Miklos Szeredi | 8da5ff2 | 2006-10-17 00:10:08 -0700 | [diff] [blame] | 230 | spin_lock(&fc->lock); |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 231 | fi->nlookup++; |
Miklos Szeredi | 8da5ff2 | 2006-10-17 00:10:08 -0700 | [diff] [blame] | 232 | spin_unlock(&fc->lock); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 233 | } |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 234 | kfree(forget); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 235 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 238 | fuse_change_attributes(inode, &outarg.attr, |
| 239 | entry_attr_timeout(&outarg), |
| 240 | attr_version); |
| 241 | fuse_change_entry_timeout(entry, &outarg); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 242 | } |
Feng Shuo | 4582a4a | 2013-01-15 11:23:28 +0800 | [diff] [blame] | 243 | fuse_advise_use_readdirplus(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 244 | return 1; |
| 245 | } |
| 246 | |
Miklos Szeredi | 8bfc016 | 2006-01-16 22:14:28 -0800 | [diff] [blame] | 247 | static int invalid_nodeid(u64 nodeid) |
Miklos Szeredi | 2827d0b2 | 2005-11-28 13:44:16 -0800 | [diff] [blame] | 248 | { |
| 249 | return !nodeid || nodeid == FUSE_ROOT_ID; |
| 250 | } |
| 251 | |
Al Viro | 4269590 | 2009-02-20 05:59:13 +0000 | [diff] [blame] | 252 | const struct dentry_operations fuse_dentry_operations = { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 253 | .d_revalidate = fuse_dentry_revalidate, |
| 254 | }; |
| 255 | |
Timo Savola | a5bfffac | 2007-04-08 16:04:00 -0700 | [diff] [blame] | 256 | int fuse_valid_type(int m) |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 257 | { |
| 258 | return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || |
| 259 | S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); |
| 260 | } |
| 261 | |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 262 | /* |
| 263 | * Add a directory inode to a dentry, ensuring that no other dentry |
| 264 | * refers to this inode. Called with fc->inst_mutex. |
| 265 | */ |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 266 | static struct dentry *fuse_d_add_directory(struct dentry *entry, |
| 267 | struct inode *inode) |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 268 | { |
| 269 | struct dentry *alias = d_find_alias(inode); |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 270 | if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) { |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 271 | /* This tries to shrink the subtree below alias */ |
| 272 | fuse_invalidate_entry(alias); |
| 273 | dput(alias); |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 274 | if (!hlist_empty(&inode->i_dentry)) |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 275 | return ERR_PTR(-EBUSY); |
| 276 | } else { |
| 277 | dput(alias); |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 278 | } |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 279 | return d_splice_alias(inode, entry); |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 282 | int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name, |
| 283 | struct fuse_entry_out *outarg, struct inode **inode) |
| 284 | { |
| 285 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 286 | struct fuse_req *req; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 287 | struct fuse_forget_link *forget; |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 288 | u64 attr_version; |
| 289 | int err; |
| 290 | |
| 291 | *inode = NULL; |
| 292 | err = -ENAMETOOLONG; |
| 293 | if (name->len > FUSE_NAME_MAX) |
| 294 | goto out; |
| 295 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 296 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 297 | err = PTR_ERR(req); |
| 298 | if (IS_ERR(req)) |
| 299 | goto out; |
| 300 | |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 301 | forget = fuse_alloc_forget(); |
| 302 | err = -ENOMEM; |
| 303 | if (!forget) { |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 304 | fuse_put_request(fc, req); |
| 305 | goto out; |
| 306 | } |
| 307 | |
| 308 | attr_version = fuse_get_attr_version(fc); |
| 309 | |
| 310 | fuse_lookup_init(fc, req, nodeid, name, outarg); |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 311 | fuse_request_send(fc, req); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 312 | err = req->out.h.error; |
| 313 | fuse_put_request(fc, req); |
| 314 | /* Zero nodeid is same as -ENOENT, but with valid timeout */ |
| 315 | if (err || !outarg->nodeid) |
| 316 | goto out_put_forget; |
| 317 | |
| 318 | err = -EIO; |
| 319 | if (!outarg->nodeid) |
| 320 | goto out_put_forget; |
| 321 | if (!fuse_valid_type(outarg->attr.mode)) |
| 322 | goto out_put_forget; |
| 323 | |
| 324 | *inode = fuse_iget(sb, outarg->nodeid, outarg->generation, |
| 325 | &outarg->attr, entry_attr_timeout(outarg), |
| 326 | attr_version); |
| 327 | err = -ENOMEM; |
| 328 | if (!*inode) { |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 329 | fuse_queue_forget(fc, forget, outarg->nodeid, 1); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 330 | goto out; |
| 331 | } |
| 332 | err = 0; |
| 333 | |
| 334 | out_put_forget: |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 335 | kfree(forget); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 336 | out: |
| 337 | return err; |
| 338 | } |
| 339 | |
Miklos Szeredi | 0aa7c69 | 2006-01-06 00:19:34 -0800 | [diff] [blame] | 340 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 341 | unsigned int flags) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 342 | { |
| 343 | int err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 344 | struct fuse_entry_out outarg; |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 345 | struct inode *inode; |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 346 | struct dentry *newent; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 347 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 348 | bool outarg_valid = true; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 349 | |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 350 | err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name, |
| 351 | &outarg, &inode); |
| 352 | if (err == -ENOENT) { |
| 353 | outarg_valid = false; |
| 354 | err = 0; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 355 | } |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 356 | if (err) |
| 357 | goto out_err; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 358 | |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 359 | err = -EIO; |
| 360 | if (inode && get_node_id(inode) == FUSE_ROOT_ID) |
| 361 | goto out_iput; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 362 | |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 363 | if (inode && S_ISDIR(inode->i_mode)) { |
| 364 | mutex_lock(&fc->inst_mutex); |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 365 | newent = fuse_d_add_directory(entry, inode); |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 366 | mutex_unlock(&fc->inst_mutex); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 367 | err = PTR_ERR(newent); |
| 368 | if (IS_ERR(newent)) |
| 369 | goto out_iput; |
| 370 | } else { |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 371 | newent = d_splice_alias(inode, entry); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 372 | } |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 373 | |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 374 | entry = newent ? newent : entry; |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 375 | if (outarg_valid) |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 376 | fuse_change_entry_timeout(entry, &outarg); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 377 | else |
| 378 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 379 | |
Feng Shuo | 4582a4a | 2013-01-15 11:23:28 +0800 | [diff] [blame] | 380 | fuse_advise_use_readdirplus(dir); |
Miklos Szeredi | 0de6256 | 2008-07-25 01:48:59 -0700 | [diff] [blame] | 381 | return newent; |
Miklos Szeredi | c180eeb | 2008-07-25 01:49:01 -0700 | [diff] [blame] | 382 | |
| 383 | out_iput: |
| 384 | iput(inode); |
| 385 | out_err: |
| 386 | return ERR_PTR(err); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 389 | /* |
| 390 | * Atomic create+open operation |
| 391 | * |
| 392 | * If the filesystem doesn't support this, then fall back to separate |
| 393 | * 'mknod' + 'open' requests. |
| 394 | */ |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 395 | static int fuse_create_open(struct inode *dir, struct dentry *entry, |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 396 | struct file *file, unsigned flags, |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 397 | umode_t mode, int *opened) |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 398 | { |
| 399 | int err; |
| 400 | struct inode *inode; |
| 401 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 402 | struct fuse_req *req; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 403 | struct fuse_forget_link *forget; |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 404 | struct fuse_create_in inarg; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 405 | struct fuse_open_out outopen; |
| 406 | struct fuse_entry_out outentry; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 407 | struct fuse_file *ff; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 408 | |
Miklos Szeredi | af109bc | 2012-08-15 13:01:24 +0200 | [diff] [blame] | 409 | /* Userspace expects S_IFREG in create mode */ |
| 410 | BUG_ON((mode & S_IFMT) != S_IFREG); |
| 411 | |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 412 | forget = fuse_alloc_forget(); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 413 | err = -ENOMEM; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 414 | if (!forget) |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 415 | goto out_err; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 416 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 417 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 418 | err = PTR_ERR(req); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 419 | if (IS_ERR(req)) |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 420 | goto out_put_forget_req; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 421 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 422 | err = -ENOMEM; |
Tejun Heo | acf9943 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 423 | ff = fuse_file_alloc(fc); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 424 | if (!ff) |
| 425 | goto out_put_request; |
| 426 | |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 427 | if (!fc->dont_mask) |
| 428 | mode &= ~current_umask(); |
| 429 | |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 430 | flags &= ~O_NOCTTY; |
| 431 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 432 | memset(&outentry, 0, sizeof(outentry)); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 433 | inarg.flags = flags; |
| 434 | inarg.mode = mode; |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 435 | inarg.umask = current_umask(); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 436 | req->in.h.opcode = FUSE_CREATE; |
| 437 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 438 | req->in.numargs = 2; |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 439 | req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) : |
| 440 | sizeof(inarg); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 441 | req->in.args[0].value = &inarg; |
| 442 | req->in.args[1].size = entry->d_name.len + 1; |
| 443 | req->in.args[1].value = entry->d_name.name; |
| 444 | req->out.numargs = 2; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 445 | if (fc->minor < 9) |
| 446 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
| 447 | else |
| 448 | req->out.args[0].size = sizeof(outentry); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 449 | req->out.args[0].value = &outentry; |
| 450 | req->out.args[1].size = sizeof(outopen); |
| 451 | req->out.args[1].value = &outopen; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 452 | fuse_request_send(fc, req); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 453 | err = req->out.h.error; |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 454 | if (err) |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 455 | goto out_free_ff; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 456 | |
| 457 | err = -EIO; |
Miklos Szeredi | 2827d0b2 | 2005-11-28 13:44:16 -0800 | [diff] [blame] | 458 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 459 | goto out_free_ff; |
| 460 | |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 461 | fuse_put_request(fc, req); |
Miklos Szeredi | c7b7143 | 2009-04-28 16:56:37 +0200 | [diff] [blame] | 462 | ff->fh = outopen.fh; |
| 463 | ff->nodeid = outentry.nodeid; |
| 464 | ff->open_flags = outopen.open_flags; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 465 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 466 | &outentry.attr, entry_attr_timeout(&outentry), 0); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 467 | if (!inode) { |
| 468 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); |
Miklos Szeredi | 8b0797a | 2009-04-28 16:56:39 +0200 | [diff] [blame] | 469 | fuse_sync_release(ff, flags); |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 470 | fuse_queue_forget(fc, forget, outentry.nodeid, 1); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 471 | err = -ENOMEM; |
| 472 | goto out_err; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 473 | } |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 474 | kfree(forget); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 475 | d_instantiate(entry, inode); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 476 | fuse_change_entry_timeout(entry, &outentry); |
Miklos Szeredi | 0952b2a | 2008-02-06 01:38:38 -0800 | [diff] [blame] | 477 | fuse_invalidate_attr(dir); |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 478 | err = finish_open(file, entry, generic_file_open, opened); |
| 479 | if (err) { |
Miklos Szeredi | 8b0797a | 2009-04-28 16:56:39 +0200 | [diff] [blame] | 480 | fuse_sync_release(ff, flags); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 481 | } else { |
| 482 | file->private_data = fuse_file_get(ff); |
| 483 | fuse_finish_open(inode, file); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 484 | } |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 485 | return err; |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 486 | |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 487 | out_free_ff: |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 488 | fuse_file_free(ff); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 489 | out_put_request: |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 490 | fuse_put_request(fc, req); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 491 | out_put_forget_req: |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 492 | kfree(forget); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 493 | out_err: |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 494 | return err; |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 498 | static int fuse_atomic_open(struct inode *dir, struct dentry *entry, |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 499 | struct file *file, unsigned flags, |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 500 | umode_t mode, int *opened) |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 501 | { |
| 502 | int err; |
| 503 | struct fuse_conn *fc = get_fuse_conn(dir); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 504 | struct dentry *res = NULL; |
| 505 | |
| 506 | if (d_unhashed(entry)) { |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 507 | res = fuse_lookup(dir, entry, 0); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 508 | if (IS_ERR(res)) |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 509 | return PTR_ERR(res); |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 510 | |
| 511 | if (res) |
| 512 | entry = res; |
| 513 | } |
| 514 | |
| 515 | if (!(flags & O_CREAT) || entry->d_inode) |
| 516 | goto no_open; |
| 517 | |
| 518 | /* Only creates */ |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 519 | *opened |= FILE_CREATED; |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 520 | |
| 521 | if (fc->no_create) |
| 522 | goto mknod; |
| 523 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 524 | err = fuse_create_open(dir, entry, file, flags, mode, opened); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 525 | if (err == -ENOSYS) { |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 526 | fc->no_create = 1; |
| 527 | goto mknod; |
| 528 | } |
| 529 | out_dput: |
| 530 | dput(res); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 531 | return err; |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 532 | |
| 533 | mknod: |
| 534 | err = fuse_mknod(dir, entry, mode, 0); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 535 | if (err) |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 536 | goto out_dput; |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 537 | no_open: |
Al Viro | e45198a | 2012-06-10 06:48:09 -0400 | [diff] [blame] | 538 | return finish_no_open(file, res); |
Miklos Szeredi | fd72faa | 2005-11-07 00:59:51 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 541 | /* |
| 542 | * Code shared between mknod, mkdir, symlink and link |
| 543 | */ |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 544 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
| 545 | struct inode *dir, struct dentry *entry, |
Al Viro | 541af6a | 2011-07-26 03:17:33 -0400 | [diff] [blame] | 546 | umode_t mode) |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 547 | { |
| 548 | struct fuse_entry_out outarg; |
| 549 | struct inode *inode; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 550 | int err; |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 551 | struct fuse_forget_link *forget; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 552 | |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 553 | forget = fuse_alloc_forget(); |
| 554 | if (!forget) { |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 555 | fuse_put_request(fc, req); |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 556 | return -ENOMEM; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 557 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 558 | |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 559 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 560 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 561 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 562 | if (fc->minor < 9) |
| 563 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; |
| 564 | else |
| 565 | req->out.args[0].size = sizeof(outarg); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 566 | req->out.args[0].value = &outarg; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 567 | fuse_request_send(fc, req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 568 | err = req->out.h.error; |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 569 | fuse_put_request(fc, req); |
| 570 | if (err) |
| 571 | goto out_put_forget_req; |
| 572 | |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 573 | err = -EIO; |
| 574 | if (invalid_nodeid(outarg.nodeid)) |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 575 | goto out_put_forget_req; |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 576 | |
| 577 | if ((outarg.attr.mode ^ mode) & S_IFMT) |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 578 | goto out_put_forget_req; |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 579 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 580 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 581 | &outarg.attr, entry_attr_timeout(&outarg), 0); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 582 | if (!inode) { |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 583 | fuse_queue_forget(fc, forget, outarg.nodeid, 1); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 584 | return -ENOMEM; |
| 585 | } |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 586 | kfree(forget); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 587 | |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 588 | if (S_ISDIR(inode->i_mode)) { |
| 589 | struct dentry *alias; |
| 590 | mutex_lock(&fc->inst_mutex); |
| 591 | alias = d_find_alias(inode); |
| 592 | if (alias) { |
| 593 | /* New directory must have moved since mkdir */ |
| 594 | mutex_unlock(&fc->inst_mutex); |
| 595 | dput(alias); |
| 596 | iput(inode); |
| 597 | return -EBUSY; |
| 598 | } |
| 599 | d_instantiate(entry, inode); |
| 600 | mutex_unlock(&fc->inst_mutex); |
| 601 | } else |
| 602 | d_instantiate(entry, inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 603 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 604 | fuse_change_entry_timeout(entry, &outarg); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 605 | fuse_invalidate_attr(dir); |
| 606 | return 0; |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 607 | |
Miklos Szeredi | 2d51013 | 2006-11-25 11:09:20 -0800 | [diff] [blame] | 608 | out_put_forget_req: |
Miklos Szeredi | 07e77dc | 2010-12-07 20:16:56 +0100 | [diff] [blame] | 609 | kfree(forget); |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 610 | return err; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 613 | static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 614 | dev_t rdev) |
| 615 | { |
| 616 | struct fuse_mknod_in inarg; |
| 617 | struct fuse_conn *fc = get_fuse_conn(dir); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 618 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 619 | if (IS_ERR(req)) |
| 620 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 621 | |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 622 | if (!fc->dont_mask) |
| 623 | mode &= ~current_umask(); |
| 624 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 625 | memset(&inarg, 0, sizeof(inarg)); |
| 626 | inarg.mode = mode; |
| 627 | inarg.rdev = new_encode_dev(rdev); |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 628 | inarg.umask = current_umask(); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 629 | req->in.h.opcode = FUSE_MKNOD; |
| 630 | req->in.numargs = 2; |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 631 | req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE : |
| 632 | sizeof(inarg); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 633 | req->in.args[0].value = &inarg; |
| 634 | req->in.args[1].size = entry->d_name.len + 1; |
| 635 | req->in.args[1].value = entry->d_name.name; |
| 636 | return create_new_entry(fc, req, dir, entry, mode); |
| 637 | } |
| 638 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 639 | static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 640 | bool excl) |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 641 | { |
| 642 | return fuse_mknod(dir, entry, mode, 0); |
| 643 | } |
| 644 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 645 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode) |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 646 | { |
| 647 | struct fuse_mkdir_in inarg; |
| 648 | struct fuse_conn *fc = get_fuse_conn(dir); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 649 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 650 | if (IS_ERR(req)) |
| 651 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 652 | |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 653 | if (!fc->dont_mask) |
| 654 | mode &= ~current_umask(); |
| 655 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 656 | memset(&inarg, 0, sizeof(inarg)); |
| 657 | inarg.mode = mode; |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 658 | inarg.umask = current_umask(); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 659 | req->in.h.opcode = FUSE_MKDIR; |
| 660 | req->in.numargs = 2; |
| 661 | req->in.args[0].size = sizeof(inarg); |
| 662 | req->in.args[0].value = &inarg; |
| 663 | req->in.args[1].size = entry->d_name.len + 1; |
| 664 | req->in.args[1].value = entry->d_name.name; |
| 665 | return create_new_entry(fc, req, dir, entry, S_IFDIR); |
| 666 | } |
| 667 | |
| 668 | static int fuse_symlink(struct inode *dir, struct dentry *entry, |
| 669 | const char *link) |
| 670 | { |
| 671 | struct fuse_conn *fc = get_fuse_conn(dir); |
| 672 | unsigned len = strlen(link) + 1; |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 673 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 674 | if (IS_ERR(req)) |
| 675 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 676 | |
| 677 | req->in.h.opcode = FUSE_SYMLINK; |
| 678 | req->in.numargs = 2; |
| 679 | req->in.args[0].size = entry->d_name.len + 1; |
| 680 | req->in.args[0].value = entry->d_name.name; |
| 681 | req->in.args[1].size = len; |
| 682 | req->in.args[1].value = link; |
| 683 | return create_new_entry(fc, req, dir, entry, S_IFLNK); |
| 684 | } |
| 685 | |
| 686 | static int fuse_unlink(struct inode *dir, struct dentry *entry) |
| 687 | { |
| 688 | int err; |
| 689 | struct fuse_conn *fc = get_fuse_conn(dir); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 690 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 691 | if (IS_ERR(req)) |
| 692 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 693 | |
| 694 | req->in.h.opcode = FUSE_UNLINK; |
| 695 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 696 | req->in.numargs = 1; |
| 697 | req->in.args[0].size = entry->d_name.len + 1; |
| 698 | req->in.args[0].value = entry->d_name.name; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 699 | fuse_request_send(fc, req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 700 | err = req->out.h.error; |
| 701 | fuse_put_request(fc, req); |
| 702 | if (!err) { |
| 703 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | ac45d61 | 2012-03-05 15:48:11 +0100 | [diff] [blame] | 704 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 705 | |
Miklos Szeredi | ac45d61 | 2012-03-05 15:48:11 +0100 | [diff] [blame] | 706 | spin_lock(&fc->lock); |
| 707 | fi->attr_version = ++fc->attr_version; |
Miklos Szeredi | dfca7ce | 2013-02-04 15:57:42 +0100 | [diff] [blame^] | 708 | /* |
| 709 | * If i_nlink == 0 then unlink doesn't make sense, yet this can |
| 710 | * happen if userspace filesystem is careless. It would be |
| 711 | * difficult to enforce correct nlink usage so just ignore this |
| 712 | * condition here |
| 713 | */ |
| 714 | if (inode->i_nlink > 0) |
| 715 | drop_nlink(inode); |
Miklos Szeredi | ac45d61 | 2012-03-05 15:48:11 +0100 | [diff] [blame] | 716 | spin_unlock(&fc->lock); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 717 | fuse_invalidate_attr(inode); |
| 718 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 719 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 720 | } else if (err == -EINTR) |
| 721 | fuse_invalidate_entry(entry); |
| 722 | return err; |
| 723 | } |
| 724 | |
| 725 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) |
| 726 | { |
| 727 | int err; |
| 728 | struct fuse_conn *fc = get_fuse_conn(dir); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 729 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 730 | if (IS_ERR(req)) |
| 731 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 732 | |
| 733 | req->in.h.opcode = FUSE_RMDIR; |
| 734 | req->in.h.nodeid = get_node_id(dir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 735 | req->in.numargs = 1; |
| 736 | req->in.args[0].size = entry->d_name.len + 1; |
| 737 | req->in.args[0].value = entry->d_name.name; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 738 | fuse_request_send(fc, req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 739 | err = req->out.h.error; |
| 740 | fuse_put_request(fc, req); |
| 741 | if (!err) { |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 742 | clear_nlink(entry->d_inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 743 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 744 | fuse_invalidate_entry_cache(entry); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 745 | } else if (err == -EINTR) |
| 746 | fuse_invalidate_entry(entry); |
| 747 | return err; |
| 748 | } |
| 749 | |
| 750 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, |
| 751 | struct inode *newdir, struct dentry *newent) |
| 752 | { |
| 753 | int err; |
| 754 | struct fuse_rename_in inarg; |
| 755 | struct fuse_conn *fc = get_fuse_conn(olddir); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 756 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Sage Weil | e4eaac0 | 2011-05-24 13:06:07 -0700 | [diff] [blame] | 757 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 758 | if (IS_ERR(req)) |
| 759 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 760 | |
| 761 | memset(&inarg, 0, sizeof(inarg)); |
| 762 | inarg.newdir = get_node_id(newdir); |
| 763 | req->in.h.opcode = FUSE_RENAME; |
| 764 | req->in.h.nodeid = get_node_id(olddir); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 765 | req->in.numargs = 3; |
| 766 | req->in.args[0].size = sizeof(inarg); |
| 767 | req->in.args[0].value = &inarg; |
| 768 | req->in.args[1].size = oldent->d_name.len + 1; |
| 769 | req->in.args[1].value = oldent->d_name.name; |
| 770 | req->in.args[2].size = newent->d_name.len + 1; |
| 771 | req->in.args[2].value = newent->d_name.name; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 772 | fuse_request_send(fc, req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 773 | err = req->out.h.error; |
| 774 | fuse_put_request(fc, req); |
| 775 | if (!err) { |
Miklos Szeredi | 08b6330 | 2007-11-28 16:22:03 -0800 | [diff] [blame] | 776 | /* ctime changes */ |
| 777 | fuse_invalidate_attr(oldent->d_inode); |
| 778 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 779 | fuse_invalidate_attr(olddir); |
| 780 | if (olddir != newdir) |
| 781 | fuse_invalidate_attr(newdir); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 782 | |
| 783 | /* newent will end up negative */ |
Miklos Szeredi | 5219f34 | 2009-11-04 10:24:52 +0100 | [diff] [blame] | 784 | if (newent->d_inode) { |
| 785 | fuse_invalidate_attr(newent->d_inode); |
Miklos Szeredi | 8cbdf1e | 2006-01-06 00:19:38 -0800 | [diff] [blame] | 786 | fuse_invalidate_entry_cache(newent); |
Miklos Szeredi | 5219f34 | 2009-11-04 10:24:52 +0100 | [diff] [blame] | 787 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 788 | } else if (err == -EINTR) { |
| 789 | /* If request was interrupted, DEITY only knows if the |
| 790 | rename actually took place. If the invalidation |
| 791 | fails (e.g. some process has CWD under the renamed |
| 792 | directory), then there can be inconsistency between |
| 793 | the dcache and the real filesystem. Tough luck. */ |
| 794 | fuse_invalidate_entry(oldent); |
| 795 | if (newent->d_inode) |
| 796 | fuse_invalidate_entry(newent); |
| 797 | } |
| 798 | |
| 799 | return err; |
| 800 | } |
| 801 | |
| 802 | static int fuse_link(struct dentry *entry, struct inode *newdir, |
| 803 | struct dentry *newent) |
| 804 | { |
| 805 | int err; |
| 806 | struct fuse_link_in inarg; |
| 807 | struct inode *inode = entry->d_inode; |
| 808 | struct fuse_conn *fc = get_fuse_conn(inode); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 809 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 810 | if (IS_ERR(req)) |
| 811 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 812 | |
| 813 | memset(&inarg, 0, sizeof(inarg)); |
| 814 | inarg.oldnodeid = get_node_id(inode); |
| 815 | req->in.h.opcode = FUSE_LINK; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 816 | req->in.numargs = 2; |
| 817 | req->in.args[0].size = sizeof(inarg); |
| 818 | req->in.args[0].value = &inarg; |
| 819 | req->in.args[1].size = newent->d_name.len + 1; |
| 820 | req->in.args[1].value = newent->d_name.name; |
| 821 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); |
| 822 | /* Contrary to "normal" filesystems it can happen that link |
| 823 | makes two "logical" inodes point to the same "physical" |
| 824 | inode. We invalidate the attributes of the old one, so it |
| 825 | will reflect changes in the backing inode (link count, |
| 826 | etc.) |
| 827 | */ |
Miklos Szeredi | ac45d61 | 2012-03-05 15:48:11 +0100 | [diff] [blame] | 828 | if (!err) { |
| 829 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 830 | |
| 831 | spin_lock(&fc->lock); |
| 832 | fi->attr_version = ++fc->attr_version; |
| 833 | inc_nlink(inode); |
| 834 | spin_unlock(&fc->lock); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 835 | fuse_invalidate_attr(inode); |
Miklos Szeredi | ac45d61 | 2012-03-05 15:48:11 +0100 | [diff] [blame] | 836 | } else if (err == -EINTR) { |
| 837 | fuse_invalidate_attr(inode); |
| 838 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 839 | return err; |
| 840 | } |
| 841 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 842 | static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr, |
| 843 | struct kstat *stat) |
| 844 | { |
Miklos Szeredi | 203627b | 2012-05-10 19:49:38 +0400 | [diff] [blame] | 845 | unsigned int blkbits; |
| 846 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 847 | stat->dev = inode->i_sb->s_dev; |
| 848 | stat->ino = attr->ino; |
| 849 | stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); |
| 850 | stat->nlink = attr->nlink; |
Eric W. Biederman | 499dcf2 | 2012-02-07 16:26:03 -0800 | [diff] [blame] | 851 | stat->uid = make_kuid(&init_user_ns, attr->uid); |
| 852 | stat->gid = make_kgid(&init_user_ns, attr->gid); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 853 | stat->rdev = inode->i_rdev; |
| 854 | stat->atime.tv_sec = attr->atime; |
| 855 | stat->atime.tv_nsec = attr->atimensec; |
| 856 | stat->mtime.tv_sec = attr->mtime; |
| 857 | stat->mtime.tv_nsec = attr->mtimensec; |
| 858 | stat->ctime.tv_sec = attr->ctime; |
| 859 | stat->ctime.tv_nsec = attr->ctimensec; |
| 860 | stat->size = attr->size; |
| 861 | stat->blocks = attr->blocks; |
Miklos Szeredi | 203627b | 2012-05-10 19:49:38 +0400 | [diff] [blame] | 862 | |
| 863 | if (attr->blksize != 0) |
| 864 | blkbits = ilog2(attr->blksize); |
| 865 | else |
| 866 | blkbits = inode->i_sb->s_blocksize_bits; |
| 867 | |
| 868 | stat->blksize = 1 << blkbits; |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 871 | static int fuse_do_getattr(struct inode *inode, struct kstat *stat, |
| 872 | struct file *file) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 873 | { |
| 874 | int err; |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 875 | struct fuse_getattr_in inarg; |
| 876 | struct fuse_attr_out outarg; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 877 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 878 | struct fuse_req *req; |
| 879 | u64 attr_version; |
| 880 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 881 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 882 | if (IS_ERR(req)) |
| 883 | return PTR_ERR(req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 884 | |
Miklos Szeredi | 7dca9fd | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 885 | attr_version = fuse_get_attr_version(fc); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 886 | |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 887 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 888 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 889 | /* Directories have separate file-handle space */ |
| 890 | if (file && S_ISREG(inode->i_mode)) { |
| 891 | struct fuse_file *ff = file->private_data; |
| 892 | |
| 893 | inarg.getattr_flags |= FUSE_GETATTR_FH; |
| 894 | inarg.fh = ff->fh; |
| 895 | } |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 896 | req->in.h.opcode = FUSE_GETATTR; |
| 897 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 898 | req->in.numargs = 1; |
| 899 | req->in.args[0].size = sizeof(inarg); |
| 900 | req->in.args[0].value = &inarg; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 901 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 902 | if (fc->minor < 9) |
| 903 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; |
| 904 | else |
| 905 | req->out.args[0].size = sizeof(outarg); |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 906 | req->out.args[0].value = &outarg; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 907 | fuse_request_send(fc, req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 908 | err = req->out.h.error; |
| 909 | fuse_put_request(fc, req); |
| 910 | if (!err) { |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 911 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 912 | make_bad_inode(inode); |
| 913 | err = -EIO; |
| 914 | } else { |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 915 | fuse_change_attributes(inode, &outarg.attr, |
| 916 | attr_timeout(&outarg), |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 917 | attr_version); |
| 918 | if (stat) |
Miklos Szeredi | c79e322 | 2007-10-18 03:06:59 -0700 | [diff] [blame] | 919 | fuse_fillattr(inode, &outarg.attr, stat); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | return err; |
| 923 | } |
| 924 | |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 925 | int fuse_update_attributes(struct inode *inode, struct kstat *stat, |
| 926 | struct file *file, bool *refreshed) |
| 927 | { |
| 928 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 929 | int err; |
| 930 | bool r; |
| 931 | |
| 932 | if (fi->i_time < get_jiffies_64()) { |
| 933 | r = true; |
| 934 | err = fuse_do_getattr(inode, stat, file); |
| 935 | } else { |
| 936 | r = false; |
| 937 | err = 0; |
| 938 | if (stat) { |
| 939 | generic_fillattr(inode, stat); |
| 940 | stat->mode = fi->orig_i_mode; |
Pavel Shilovsky | 45c72cd | 2012-05-10 19:49:38 +0400 | [diff] [blame] | 941 | stat->ino = fi->orig_ino; |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | if (refreshed != NULL) |
| 946 | *refreshed = r; |
| 947 | |
| 948 | return err; |
| 949 | } |
| 950 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 951 | int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid, |
John Muir | 451d0f5 | 2011-12-06 21:50:06 +0100 | [diff] [blame] | 952 | u64 child_nodeid, struct qstr *name) |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 953 | { |
| 954 | int err = -ENOTDIR; |
| 955 | struct inode *parent; |
| 956 | struct dentry *dir; |
| 957 | struct dentry *entry; |
| 958 | |
| 959 | parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid); |
| 960 | if (!parent) |
| 961 | return -ENOENT; |
| 962 | |
| 963 | mutex_lock(&parent->i_mutex); |
| 964 | if (!S_ISDIR(parent->i_mode)) |
| 965 | goto unlock; |
| 966 | |
| 967 | err = -ENOENT; |
| 968 | dir = d_find_alias(parent); |
| 969 | if (!dir) |
| 970 | goto unlock; |
| 971 | |
| 972 | entry = d_lookup(dir, name); |
| 973 | dput(dir); |
| 974 | if (!entry) |
| 975 | goto unlock; |
| 976 | |
| 977 | fuse_invalidate_attr(parent); |
| 978 | fuse_invalidate_entry(entry); |
John Muir | 451d0f5 | 2011-12-06 21:50:06 +0100 | [diff] [blame] | 979 | |
| 980 | if (child_nodeid != 0 && entry->d_inode) { |
| 981 | mutex_lock(&entry->d_inode->i_mutex); |
| 982 | if (get_node_id(entry->d_inode) != child_nodeid) { |
| 983 | err = -ENOENT; |
| 984 | goto badentry; |
| 985 | } |
| 986 | if (d_mountpoint(entry)) { |
| 987 | err = -EBUSY; |
| 988 | goto badentry; |
| 989 | } |
| 990 | if (S_ISDIR(entry->d_inode->i_mode)) { |
| 991 | shrink_dcache_parent(entry); |
| 992 | if (!simple_empty(entry)) { |
| 993 | err = -ENOTEMPTY; |
| 994 | goto badentry; |
| 995 | } |
| 996 | entry->d_inode->i_flags |= S_DEAD; |
| 997 | } |
| 998 | dont_mount(entry); |
| 999 | clear_nlink(entry->d_inode); |
| 1000 | err = 0; |
| 1001 | badentry: |
| 1002 | mutex_unlock(&entry->d_inode->i_mutex); |
| 1003 | if (!err) |
| 1004 | d_delete(entry); |
| 1005 | } else { |
| 1006 | err = 0; |
| 1007 | } |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1008 | dput(entry); |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1009 | |
| 1010 | unlock: |
| 1011 | mutex_unlock(&parent->i_mutex); |
| 1012 | iput(parent); |
| 1013 | return err; |
| 1014 | } |
| 1015 | |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 1016 | /* |
| 1017 | * Calling into a user-controlled filesystem gives the filesystem |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1018 | * daemon ptrace-like capabilities over the current process. This |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 1019 | * means, that the filesystem daemon is able to record the exact |
| 1020 | * filesystem operations performed, and can also control the behavior |
| 1021 | * of the requester process in otherwise impossible ways. For example |
| 1022 | * it can delay the operation for arbitrary length of time allowing |
| 1023 | * DoS against the requester. |
| 1024 | * |
| 1025 | * For this reason only those processes can call into the filesystem, |
| 1026 | * for which the owner of the mount has ptrace privilege. This |
| 1027 | * excludes processes started by other users, suid or sgid processes. |
| 1028 | */ |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1029 | int fuse_allow_current_process(struct fuse_conn *fc) |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 1030 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1031 | const struct cred *cred; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1032 | |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 1033 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 1034 | return 1; |
| 1035 | |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1036 | cred = current_cred(); |
Eric W. Biederman | 499dcf2 | 2012-02-07 16:26:03 -0800 | [diff] [blame] | 1037 | if (uid_eq(cred->euid, fc->user_id) && |
| 1038 | uid_eq(cred->suid, fc->user_id) && |
| 1039 | uid_eq(cred->uid, fc->user_id) && |
| 1040 | gid_eq(cred->egid, fc->group_id) && |
| 1041 | gid_eq(cred->sgid, fc->group_id) && |
| 1042 | gid_eq(cred->gid, fc->group_id)) |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1043 | return 1; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 1044 | |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1045 | return 0; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 1048 | static int fuse_access(struct inode *inode, int mask) |
| 1049 | { |
| 1050 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1051 | struct fuse_req *req; |
| 1052 | struct fuse_access_in inarg; |
| 1053 | int err; |
| 1054 | |
| 1055 | if (fc->no_access) |
| 1056 | return 0; |
| 1057 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1058 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1059 | if (IS_ERR(req)) |
| 1060 | return PTR_ERR(req); |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 1061 | |
| 1062 | memset(&inarg, 0, sizeof(inarg)); |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 1063 | inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC); |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 1064 | req->in.h.opcode = FUSE_ACCESS; |
| 1065 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 1066 | req->in.numargs = 1; |
| 1067 | req->in.args[0].size = sizeof(inarg); |
| 1068 | req->in.args[0].value = &inarg; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1069 | fuse_request_send(fc, req); |
Miklos Szeredi | 31d40d7 | 2005-11-07 00:59:50 -0800 | [diff] [blame] | 1070 | err = req->out.h.error; |
| 1071 | fuse_put_request(fc, req); |
| 1072 | if (err == -ENOSYS) { |
| 1073 | fc->no_access = 1; |
| 1074 | err = 0; |
| 1075 | } |
| 1076 | return err; |
| 1077 | } |
| 1078 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 1079 | static int fuse_perm_getattr(struct inode *inode, int mask) |
Miklos Szeredi | 19690dd | 2011-03-21 13:58:06 +0100 | [diff] [blame] | 1080 | { |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 1081 | if (mask & MAY_NOT_BLOCK) |
Miklos Szeredi | 19690dd | 2011-03-21 13:58:06 +0100 | [diff] [blame] | 1082 | return -ECHILD; |
| 1083 | |
| 1084 | return fuse_do_getattr(inode, NULL, NULL); |
| 1085 | } |
| 1086 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1087 | /* |
| 1088 | * Check permission. The two basic access models of FUSE are: |
| 1089 | * |
| 1090 | * 1) Local access checking ('default_permissions' mount option) based |
| 1091 | * on file mode. This is the plain old disk filesystem permission |
| 1092 | * modell. |
| 1093 | * |
| 1094 | * 2) "Remote" access checking, where server is responsible for |
| 1095 | * checking permission in each inode operation. An exception to this |
| 1096 | * is if ->permission() was invoked from sys_access() in which case an |
| 1097 | * access request is sent. Execute permission is still checked |
| 1098 | * locally based on file mode. |
| 1099 | */ |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 1100 | static int fuse_permission(struct inode *inode, int mask) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1101 | { |
| 1102 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1103 | bool refreshed = false; |
| 1104 | int err = 0; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1105 | |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1106 | if (!fuse_allow_current_process(fc)) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1107 | return -EACCES; |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1108 | |
| 1109 | /* |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 1110 | * If attributes are needed, refresh them before proceeding |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1111 | */ |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 1112 | if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) || |
| 1113 | ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { |
Miklos Szeredi | 19690dd | 2011-03-21 13:58:06 +0100 | [diff] [blame] | 1114 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1115 | |
| 1116 | if (fi->i_time < get_jiffies_64()) { |
| 1117 | refreshed = true; |
| 1118 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 1119 | err = fuse_perm_getattr(inode, mask); |
Miklos Szeredi | 19690dd | 2011-03-21 13:58:06 +0100 | [diff] [blame] | 1120 | if (err) |
| 1121 | return err; |
| 1122 | } |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 1126 | err = generic_permission(inode, mask); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1127 | |
| 1128 | /* If permission is denied, try to refresh file |
| 1129 | attributes. This is also needed, because the root |
| 1130 | node will at first have no permissions */ |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1131 | if (err == -EACCES && !refreshed) { |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 1132 | err = fuse_perm_getattr(inode, mask); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1133 | if (!err) |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 1134 | err = generic_permission(inode, mask); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1137 | /* Note: the opposite of the above test does not |
| 1138 | exist. So if permissions are revoked this won't be |
| 1139 | noticed immediately, only after the attribute |
| 1140 | timeout has expired */ |
Eric Paris | 9cfcac8 | 2010-07-23 11:43:51 -0400 | [diff] [blame] | 1141 | } else if (mask & (MAY_ACCESS | MAY_CHDIR)) { |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 1142 | if (mask & MAY_NOT_BLOCK) |
Miklos Szeredi | 19690dd | 2011-03-21 13:58:06 +0100 | [diff] [blame] | 1143 | return -ECHILD; |
| 1144 | |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 1145 | err = fuse_access(inode, mask); |
| 1146 | } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { |
| 1147 | if (!(inode->i_mode & S_IXUGO)) { |
| 1148 | if (refreshed) |
| 1149 | return -EACCES; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1150 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 1151 | err = fuse_perm_getattr(inode, mask); |
Miklos Szeredi | e8e9615 | 2007-10-16 23:31:06 -0700 | [diff] [blame] | 1152 | if (!err && !(inode->i_mode & S_IXUGO)) |
| 1153 | return -EACCES; |
| 1154 | } |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1155 | } |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1156 | return err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, |
| 1160 | void *dstbuf, filldir_t filldir) |
| 1161 | { |
| 1162 | while (nbytes >= FUSE_NAME_OFFSET) { |
| 1163 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; |
| 1164 | size_t reclen = FUSE_DIRENT_SIZE(dirent); |
| 1165 | int over; |
| 1166 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) |
| 1167 | return -EIO; |
| 1168 | if (reclen > nbytes) |
| 1169 | break; |
| 1170 | |
| 1171 | over = filldir(dstbuf, dirent->name, dirent->namelen, |
| 1172 | file->f_pos, dirent->ino, dirent->type); |
| 1173 | if (over) |
| 1174 | break; |
| 1175 | |
| 1176 | buf += reclen; |
| 1177 | nbytes -= reclen; |
| 1178 | file->f_pos = dirent->off; |
| 1179 | } |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Anand V. Avati | 0b05b18 | 2012-08-19 08:53:23 -0400 | [diff] [blame] | 1184 | static int fuse_direntplus_link(struct file *file, |
| 1185 | struct fuse_direntplus *direntplus, |
| 1186 | u64 attr_version) |
| 1187 | { |
| 1188 | int err; |
| 1189 | struct fuse_entry_out *o = &direntplus->entry_out; |
| 1190 | struct fuse_dirent *dirent = &direntplus->dirent; |
| 1191 | struct dentry *parent = file->f_path.dentry; |
| 1192 | struct qstr name = QSTR_INIT(dirent->name, dirent->namelen); |
| 1193 | struct dentry *dentry; |
| 1194 | struct dentry *alias; |
| 1195 | struct inode *dir = parent->d_inode; |
| 1196 | struct fuse_conn *fc; |
| 1197 | struct inode *inode; |
| 1198 | |
| 1199 | if (!o->nodeid) { |
| 1200 | /* |
| 1201 | * Unlike in the case of fuse_lookup, zero nodeid does not mean |
| 1202 | * ENOENT. Instead, it only means the userspace filesystem did |
| 1203 | * not want to return attributes/handle for this entry. |
| 1204 | * |
| 1205 | * So do nothing. |
| 1206 | */ |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | if (name.name[0] == '.') { |
| 1211 | /* |
| 1212 | * We could potentially refresh the attributes of the directory |
| 1213 | * and its parent? |
| 1214 | */ |
| 1215 | if (name.len == 1) |
| 1216 | return 0; |
| 1217 | if (name.name[1] == '.' && name.len == 2) |
| 1218 | return 0; |
| 1219 | } |
| 1220 | fc = get_fuse_conn(dir); |
| 1221 | |
| 1222 | name.hash = full_name_hash(name.name, name.len); |
| 1223 | dentry = d_lookup(parent, &name); |
| 1224 | if (dentry && dentry->d_inode) { |
| 1225 | inode = dentry->d_inode; |
| 1226 | if (get_node_id(inode) == o->nodeid) { |
| 1227 | struct fuse_inode *fi; |
| 1228 | fi = get_fuse_inode(inode); |
| 1229 | spin_lock(&fc->lock); |
| 1230 | fi->nlookup++; |
| 1231 | spin_unlock(&fc->lock); |
| 1232 | |
| 1233 | /* |
| 1234 | * The other branch to 'found' comes via fuse_iget() |
| 1235 | * which bumps nlookup inside |
| 1236 | */ |
| 1237 | goto found; |
| 1238 | } |
| 1239 | err = d_invalidate(dentry); |
| 1240 | if (err) |
| 1241 | goto out; |
| 1242 | dput(dentry); |
| 1243 | dentry = NULL; |
| 1244 | } |
| 1245 | |
| 1246 | dentry = d_alloc(parent, &name); |
| 1247 | err = -ENOMEM; |
| 1248 | if (!dentry) |
| 1249 | goto out; |
| 1250 | |
| 1251 | inode = fuse_iget(dir->i_sb, o->nodeid, o->generation, |
| 1252 | &o->attr, entry_attr_timeout(o), attr_version); |
| 1253 | if (!inode) |
| 1254 | goto out; |
| 1255 | |
| 1256 | alias = d_materialise_unique(dentry, inode); |
| 1257 | err = PTR_ERR(alias); |
| 1258 | if (IS_ERR(alias)) |
| 1259 | goto out; |
| 1260 | if (alias) { |
| 1261 | dput(dentry); |
| 1262 | dentry = alias; |
| 1263 | } |
| 1264 | |
| 1265 | found: |
| 1266 | fuse_change_attributes(inode, &o->attr, entry_attr_timeout(o), |
| 1267 | attr_version); |
| 1268 | |
| 1269 | fuse_change_entry_timeout(dentry, o); |
| 1270 | |
| 1271 | err = 0; |
| 1272 | out: |
| 1273 | if (dentry) |
| 1274 | dput(dentry); |
| 1275 | return err; |
| 1276 | } |
| 1277 | |
| 1278 | static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file, |
| 1279 | void *dstbuf, filldir_t filldir, u64 attr_version) |
| 1280 | { |
| 1281 | struct fuse_direntplus *direntplus; |
| 1282 | struct fuse_dirent *dirent; |
| 1283 | size_t reclen; |
| 1284 | int over = 0; |
| 1285 | int ret; |
| 1286 | |
| 1287 | while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) { |
| 1288 | direntplus = (struct fuse_direntplus *) buf; |
| 1289 | dirent = &direntplus->dirent; |
| 1290 | reclen = FUSE_DIRENTPLUS_SIZE(direntplus); |
| 1291 | |
| 1292 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) |
| 1293 | return -EIO; |
| 1294 | if (reclen > nbytes) |
| 1295 | break; |
| 1296 | |
| 1297 | if (!over) { |
| 1298 | /* We fill entries into dstbuf only as much as |
| 1299 | it can hold. But we still continue iterating |
| 1300 | over remaining entries to link them. If not, |
| 1301 | we need to send a FORGET for each of those |
| 1302 | which we did not link. |
| 1303 | */ |
| 1304 | over = filldir(dstbuf, dirent->name, dirent->namelen, |
| 1305 | file->f_pos, dirent->ino, |
| 1306 | dirent->type); |
| 1307 | file->f_pos = dirent->off; |
| 1308 | } |
| 1309 | |
| 1310 | buf += reclen; |
| 1311 | nbytes -= reclen; |
| 1312 | |
| 1313 | ret = fuse_direntplus_link(file, direntplus, attr_version); |
| 1314 | if (ret) |
| 1315 | fuse_force_forget(file, direntplus->entry_out.nodeid); |
| 1316 | } |
| 1317 | |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1321 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
| 1322 | { |
Feng Shuo | 4582a4a | 2013-01-15 11:23:28 +0800 | [diff] [blame] | 1323 | int plus, err; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1324 | size_t nbytes; |
| 1325 | struct page *page; |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 1326 | struct inode *inode = file->f_path.dentry->d_inode; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1327 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1328 | struct fuse_req *req; |
Anand V. Avati | 0b05b18 | 2012-08-19 08:53:23 -0400 | [diff] [blame] | 1329 | u64 attr_version = 0; |
Miklos Szeredi | 248d86e | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1330 | |
| 1331 | if (is_bad_inode(inode)) |
| 1332 | return -EIO; |
| 1333 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1334 | req = fuse_get_req(fc, 1); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1335 | if (IS_ERR(req)) |
| 1336 | return PTR_ERR(req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1337 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1338 | page = alloc_page(GFP_KERNEL); |
| 1339 | if (!page) { |
| 1340 | fuse_put_request(fc, req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1341 | return -ENOMEM; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1342 | } |
Feng Shuo | 4582a4a | 2013-01-15 11:23:28 +0800 | [diff] [blame] | 1343 | |
| 1344 | plus = fuse_use_readdirplus(inode, file); |
Miklos Szeredi | f4975c6 | 2009-04-02 14:25:34 +0200 | [diff] [blame] | 1345 | req->out.argpages = 1; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1346 | req->num_pages = 1; |
| 1347 | req->pages[0] = page; |
Maxim Patlasov | 85f40ae | 2012-10-26 19:49:33 +0400 | [diff] [blame] | 1348 | req->page_descs[0].length = PAGE_SIZE; |
Feng Shuo | 4582a4a | 2013-01-15 11:23:28 +0800 | [diff] [blame] | 1349 | if (plus) { |
Anand V. Avati | 0b05b18 | 2012-08-19 08:53:23 -0400 | [diff] [blame] | 1350 | attr_version = fuse_get_attr_version(fc); |
| 1351 | fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, |
| 1352 | FUSE_READDIRPLUS); |
| 1353 | } else { |
| 1354 | fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, |
| 1355 | FUSE_READDIR); |
| 1356 | } |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1357 | fuse_request_send(fc, req); |
Miklos Szeredi | 361b1eb5 | 2006-01-16 22:14:45 -0800 | [diff] [blame] | 1358 | nbytes = req->out.args[0].size; |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1359 | err = req->out.h.error; |
| 1360 | fuse_put_request(fc, req); |
Anand V. Avati | 0b05b18 | 2012-08-19 08:53:23 -0400 | [diff] [blame] | 1361 | if (!err) { |
Feng Shuo | 4582a4a | 2013-01-15 11:23:28 +0800 | [diff] [blame] | 1362 | if (plus) { |
Anand V. Avati | 0b05b18 | 2012-08-19 08:53:23 -0400 | [diff] [blame] | 1363 | err = parse_dirplusfile(page_address(page), nbytes, |
| 1364 | file, dstbuf, filldir, |
| 1365 | attr_version); |
| 1366 | } else { |
| 1367 | err = parse_dirfile(page_address(page), nbytes, file, |
| 1368 | dstbuf, filldir); |
| 1369 | } |
| 1370 | } |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1371 | |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1372 | __free_page(page); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1373 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | 04730fe | 2005-09-09 13:10:36 -0700 | [diff] [blame] | 1374 | return err; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | static char *read_link(struct dentry *dentry) |
| 1378 | { |
| 1379 | struct inode *inode = dentry->d_inode; |
| 1380 | struct fuse_conn *fc = get_fuse_conn(inode); |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1381 | struct fuse_req *req = fuse_get_req_nopages(fc); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1382 | char *link; |
| 1383 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1384 | if (IS_ERR(req)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 1385 | return ERR_CAST(req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1386 | |
| 1387 | link = (char *) __get_free_page(GFP_KERNEL); |
| 1388 | if (!link) { |
| 1389 | link = ERR_PTR(-ENOMEM); |
| 1390 | goto out; |
| 1391 | } |
| 1392 | req->in.h.opcode = FUSE_READLINK; |
| 1393 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1394 | req->out.argvar = 1; |
| 1395 | req->out.numargs = 1; |
| 1396 | req->out.args[0].size = PAGE_SIZE - 1; |
| 1397 | req->out.args[0].value = link; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1398 | fuse_request_send(fc, req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1399 | if (req->out.h.error) { |
| 1400 | free_page((unsigned long) link); |
| 1401 | link = ERR_PTR(req->out.h.error); |
| 1402 | } else |
| 1403 | link[req->out.args[0].size] = '\0'; |
| 1404 | out: |
| 1405 | fuse_put_request(fc, req); |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1406 | fuse_invalidate_attr(inode); /* atime changed */ |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1407 | return link; |
| 1408 | } |
| 1409 | |
| 1410 | static void free_link(char *link) |
| 1411 | { |
| 1412 | if (!IS_ERR(link)) |
| 1413 | free_page((unsigned long) link); |
| 1414 | } |
| 1415 | |
| 1416 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 1417 | { |
| 1418 | nd_set_link(nd, read_link(dentry)); |
| 1419 | return NULL; |
| 1420 | } |
| 1421 | |
| 1422 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) |
| 1423 | { |
| 1424 | free_link(nd_get_link(nd)); |
| 1425 | } |
| 1426 | |
| 1427 | static int fuse_dir_open(struct inode *inode, struct file *file) |
| 1428 | { |
Miklos Szeredi | 91fe96b | 2009-04-28 16:56:37 +0200 | [diff] [blame] | 1429 | return fuse_open_common(inode, file, true); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | static int fuse_dir_release(struct inode *inode, struct file *file) |
| 1433 | { |
Miklos Szeredi | 8b0797a | 2009-04-28 16:56:39 +0200 | [diff] [blame] | 1434 | fuse_release_common(file, FUSE_RELEASEDIR); |
| 1435 | |
| 1436 | return 0; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1439 | static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end, |
| 1440 | int datasync) |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1441 | { |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1442 | return fuse_fsync_common(file, start, end, datasync, 1); |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Miklos Szeredi | b18da0c | 2011-12-13 11:58:49 +0100 | [diff] [blame] | 1445 | static long fuse_dir_ioctl(struct file *file, unsigned int cmd, |
| 1446 | unsigned long arg) |
| 1447 | { |
| 1448 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); |
| 1449 | |
| 1450 | /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */ |
| 1451 | if (fc->minor < 18) |
| 1452 | return -ENOTTY; |
| 1453 | |
| 1454 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR); |
| 1455 | } |
| 1456 | |
| 1457 | static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd, |
| 1458 | unsigned long arg) |
| 1459 | { |
| 1460 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); |
| 1461 | |
| 1462 | if (fc->minor < 18) |
| 1463 | return -ENOTTY; |
| 1464 | |
| 1465 | return fuse_ioctl_common(file, cmd, arg, |
| 1466 | FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR); |
| 1467 | } |
| 1468 | |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1469 | static bool update_mtime(unsigned ivalid) |
| 1470 | { |
| 1471 | /* Always update if mtime is explicitly set */ |
| 1472 | if (ivalid & ATTR_MTIME_SET) |
| 1473 | return true; |
| 1474 | |
| 1475 | /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ |
| 1476 | if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) |
| 1477 | return false; |
| 1478 | |
| 1479 | /* In all other cases update */ |
| 1480 | return true; |
| 1481 | } |
| 1482 | |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1483 | 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] | 1484 | { |
| 1485 | unsigned ivalid = iattr->ia_valid; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1486 | |
| 1487 | if (ivalid & ATTR_MODE) |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1488 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1489 | if (ivalid & ATTR_UID) |
Eric W. Biederman | 499dcf2 | 2012-02-07 16:26:03 -0800 | [diff] [blame] | 1490 | arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1491 | if (ivalid & ATTR_GID) |
Eric W. Biederman | 499dcf2 | 2012-02-07 16:26:03 -0800 | [diff] [blame] | 1492 | arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1493 | if (ivalid & ATTR_SIZE) |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1494 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1495 | if (ivalid & ATTR_ATIME) { |
| 1496 | arg->valid |= FATTR_ATIME; |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1497 | arg->atime = iattr->ia_atime.tv_sec; |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1498 | arg->atimensec = iattr->ia_atime.tv_nsec; |
| 1499 | if (!(ivalid & ATTR_ATIME_SET)) |
| 1500 | arg->valid |= FATTR_ATIME_NOW; |
| 1501 | } |
| 1502 | if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) { |
| 1503 | arg->valid |= FATTR_MTIME; |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1504 | arg->mtime = iattr->ia_mtime.tv_sec; |
Miklos Szeredi | 17637cb | 2007-10-18 03:07:01 -0700 | [diff] [blame] | 1505 | arg->mtimensec = iattr->ia_mtime.tv_nsec; |
| 1506 | if (!(ivalid & ATTR_MTIME_SET)) |
| 1507 | arg->valid |= FATTR_MTIME_NOW; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1508 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1511 | /* |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1512 | * Prevent concurrent writepages on inode |
| 1513 | * |
| 1514 | * This is done by adding a negative bias to the inode write counter |
| 1515 | * and waiting for all pending writes to finish. |
| 1516 | */ |
| 1517 | void fuse_set_nowrite(struct inode *inode) |
| 1518 | { |
| 1519 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1520 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1521 | |
| 1522 | BUG_ON(!mutex_is_locked(&inode->i_mutex)); |
| 1523 | |
| 1524 | spin_lock(&fc->lock); |
| 1525 | BUG_ON(fi->writectr < 0); |
| 1526 | fi->writectr += FUSE_NOWRITE; |
| 1527 | spin_unlock(&fc->lock); |
| 1528 | wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE); |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Allow writepages on inode |
| 1533 | * |
| 1534 | * Remove the bias from the writecounter and send any queued |
| 1535 | * writepages. |
| 1536 | */ |
| 1537 | static void __fuse_release_nowrite(struct inode *inode) |
| 1538 | { |
| 1539 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1540 | |
| 1541 | BUG_ON(fi->writectr != FUSE_NOWRITE); |
| 1542 | fi->writectr = 0; |
| 1543 | fuse_flush_writepages(inode); |
| 1544 | } |
| 1545 | |
| 1546 | void fuse_release_nowrite(struct inode *inode) |
| 1547 | { |
| 1548 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1549 | |
| 1550 | spin_lock(&fc->lock); |
| 1551 | __fuse_release_nowrite(inode); |
| 1552 | spin_unlock(&fc->lock); |
| 1553 | } |
| 1554 | |
| 1555 | /* |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1556 | * Set attributes, and at the same time refresh them. |
| 1557 | * |
| 1558 | * Truncation is slightly complicated, because the 'truncate' request |
| 1559 | * 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] | 1560 | * vmtruncate() doesn't allow for this case, so do the rlimit checking |
| 1561 | * and the actual truncation by hand. |
Miklos Szeredi | 6f9f118 | 2006-01-06 00:19:39 -0800 | [diff] [blame] | 1562 | */ |
Miklos Szeredi | 49d4914 | 2007-10-18 03:07:00 -0700 | [diff] [blame] | 1563 | static int fuse_do_setattr(struct dentry *entry, struct iattr *attr, |
| 1564 | struct file *file) |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1565 | { |
| 1566 | struct inode *inode = entry->d_inode; |
| 1567 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1568 | struct fuse_req *req; |
| 1569 | struct fuse_setattr_in inarg; |
| 1570 | struct fuse_attr_out outarg; |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1571 | bool is_truncate = false; |
| 1572 | loff_t oldsize; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1573 | int err; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1574 | |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1575 | if (!fuse_allow_current_process(fc)) |
Miklos Szeredi | e57ac68 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 1576 | return -EACCES; |
| 1577 | |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 1578 | if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS)) |
| 1579 | attr->ia_valid |= ATTR_FORCE; |
| 1580 | |
| 1581 | err = inode_change_ok(inode, attr); |
| 1582 | if (err) |
| 1583 | return err; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1584 | |
Miklos Szeredi | 8d56add | 2011-02-25 14:44:58 +0100 | [diff] [blame] | 1585 | if (attr->ia_valid & ATTR_OPEN) { |
| 1586 | if (fc->atomic_o_trunc) |
| 1587 | return 0; |
| 1588 | file = NULL; |
| 1589 | } |
Miklos Szeredi | 6ff958e | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 1590 | |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 1591 | if (attr->ia_valid & ATTR_SIZE) |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1592 | is_truncate = true; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1593 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1594 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1595 | if (IS_ERR(req)) |
| 1596 | return PTR_ERR(req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1597 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1598 | if (is_truncate) |
| 1599 | fuse_set_nowrite(inode); |
| 1600 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1601 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 1602 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | befc649 | 2005-11-07 00:59:52 -0800 | [diff] [blame] | 1603 | iattr_to_fattr(attr, &inarg); |
Miklos Szeredi | 49d4914 | 2007-10-18 03:07:00 -0700 | [diff] [blame] | 1604 | if (file) { |
| 1605 | struct fuse_file *ff = file->private_data; |
| 1606 | inarg.valid |= FATTR_FH; |
| 1607 | inarg.fh = ff->fh; |
| 1608 | } |
Miklos Szeredi | f333211 | 2007-10-18 03:07:04 -0700 | [diff] [blame] | 1609 | if (attr->ia_valid & ATTR_SIZE) { |
| 1610 | /* For mandatory locking in truncate */ |
| 1611 | inarg.valid |= FATTR_LOCKOWNER; |
| 1612 | inarg.lock_owner = fuse_lock_owner_id(fc, current->files); |
| 1613 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1614 | req->in.h.opcode = FUSE_SETATTR; |
| 1615 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1616 | req->in.numargs = 1; |
| 1617 | req->in.args[0].size = sizeof(inarg); |
| 1618 | req->in.args[0].value = &inarg; |
| 1619 | req->out.numargs = 1; |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 1620 | if (fc->minor < 9) |
| 1621 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; |
| 1622 | else |
| 1623 | req->out.args[0].size = sizeof(outarg); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1624 | req->out.args[0].value = &outarg; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1625 | fuse_request_send(fc, req); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1626 | err = req->out.h.error; |
| 1627 | fuse_put_request(fc, req); |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1628 | if (err) { |
| 1629 | if (err == -EINTR) |
| 1630 | fuse_invalidate_attr(inode); |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1631 | goto error; |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1632 | } |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1633 | |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1634 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
| 1635 | make_bad_inode(inode); |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1636 | err = -EIO; |
| 1637 | goto error; |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1640 | spin_lock(&fc->lock); |
| 1641 | fuse_change_attributes_common(inode, &outarg.attr, |
| 1642 | attr_timeout(&outarg)); |
| 1643 | oldsize = inode->i_size; |
| 1644 | i_size_write(inode, outarg.attr.size); |
| 1645 | |
| 1646 | if (is_truncate) { |
| 1647 | /* NOTE: this may release/reacquire fc->lock */ |
| 1648 | __fuse_release_nowrite(inode); |
| 1649 | } |
| 1650 | spin_unlock(&fc->lock); |
| 1651 | |
| 1652 | /* |
| 1653 | * Only call invalidate_inode_pages2() after removing |
| 1654 | * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock. |
| 1655 | */ |
| 1656 | if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) { |
npiggin@suse.de | c08d3b0 | 2009-08-21 02:35:06 +1000 | [diff] [blame] | 1657 | truncate_pagecache(inode, oldsize, outarg.attr.size); |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1658 | invalidate_inode_pages2(inode->i_mapping); |
| 1659 | } |
| 1660 | |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 1661 | return 0; |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 1662 | |
| 1663 | error: |
| 1664 | if (is_truncate) |
| 1665 | fuse_release_nowrite(inode); |
| 1666 | |
| 1667 | return err; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
Miklos Szeredi | 49d4914 | 2007-10-18 03:07:00 -0700 | [diff] [blame] | 1670 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
| 1671 | { |
| 1672 | if (attr->ia_valid & ATTR_FILE) |
| 1673 | return fuse_do_setattr(entry, attr, attr->ia_file); |
| 1674 | else |
| 1675 | return fuse_do_setattr(entry, attr, NULL); |
| 1676 | } |
| 1677 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1678 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
| 1679 | struct kstat *stat) |
| 1680 | { |
| 1681 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1682 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1683 | |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1684 | if (!fuse_allow_current_process(fc)) |
Miklos Szeredi | 244f638 | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 1685 | return -EACCES; |
| 1686 | |
Miklos Szeredi | bcb4be8 | 2007-11-28 16:21:59 -0800 | [diff] [blame] | 1687 | return fuse_update_attributes(inode, stat, NULL, NULL); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1690 | static int fuse_setxattr(struct dentry *entry, const char *name, |
| 1691 | const void *value, size_t size, int flags) |
| 1692 | { |
| 1693 | struct inode *inode = entry->d_inode; |
| 1694 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1695 | struct fuse_req *req; |
| 1696 | struct fuse_setxattr_in inarg; |
| 1697 | int err; |
| 1698 | |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1699 | if (fc->no_setxattr) |
| 1700 | return -EOPNOTSUPP; |
| 1701 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1702 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1703 | if (IS_ERR(req)) |
| 1704 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1705 | |
| 1706 | memset(&inarg, 0, sizeof(inarg)); |
| 1707 | inarg.size = size; |
| 1708 | inarg.flags = flags; |
| 1709 | req->in.h.opcode = FUSE_SETXATTR; |
| 1710 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1711 | req->in.numargs = 3; |
| 1712 | req->in.args[0].size = sizeof(inarg); |
| 1713 | req->in.args[0].value = &inarg; |
| 1714 | req->in.args[1].size = strlen(name) + 1; |
| 1715 | req->in.args[1].value = name; |
| 1716 | req->in.args[2].size = size; |
| 1717 | req->in.args[2].value = value; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1718 | fuse_request_send(fc, req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1719 | err = req->out.h.error; |
| 1720 | fuse_put_request(fc, req); |
| 1721 | if (err == -ENOSYS) { |
| 1722 | fc->no_setxattr = 1; |
| 1723 | err = -EOPNOTSUPP; |
| 1724 | } |
| 1725 | return err; |
| 1726 | } |
| 1727 | |
| 1728 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, |
| 1729 | void *value, size_t size) |
| 1730 | { |
| 1731 | struct inode *inode = entry->d_inode; |
| 1732 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1733 | struct fuse_req *req; |
| 1734 | struct fuse_getxattr_in inarg; |
| 1735 | struct fuse_getxattr_out outarg; |
| 1736 | ssize_t ret; |
| 1737 | |
| 1738 | if (fc->no_getxattr) |
| 1739 | return -EOPNOTSUPP; |
| 1740 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1741 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1742 | if (IS_ERR(req)) |
| 1743 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1744 | |
| 1745 | memset(&inarg, 0, sizeof(inarg)); |
| 1746 | inarg.size = size; |
| 1747 | req->in.h.opcode = FUSE_GETXATTR; |
| 1748 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1749 | req->in.numargs = 2; |
| 1750 | req->in.args[0].size = sizeof(inarg); |
| 1751 | req->in.args[0].value = &inarg; |
| 1752 | req->in.args[1].size = strlen(name) + 1; |
| 1753 | req->in.args[1].value = name; |
| 1754 | /* This is really two different operations rolled into one */ |
| 1755 | req->out.numargs = 1; |
| 1756 | if (size) { |
| 1757 | req->out.argvar = 1; |
| 1758 | req->out.args[0].size = size; |
| 1759 | req->out.args[0].value = value; |
| 1760 | } else { |
| 1761 | req->out.args[0].size = sizeof(outarg); |
| 1762 | req->out.args[0].value = &outarg; |
| 1763 | } |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1764 | fuse_request_send(fc, req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1765 | ret = req->out.h.error; |
| 1766 | if (!ret) |
| 1767 | ret = size ? req->out.args[0].size : outarg.size; |
| 1768 | else { |
| 1769 | if (ret == -ENOSYS) { |
| 1770 | fc->no_getxattr = 1; |
| 1771 | ret = -EOPNOTSUPP; |
| 1772 | } |
| 1773 | } |
| 1774 | fuse_put_request(fc, req); |
| 1775 | return ret; |
| 1776 | } |
| 1777 | |
| 1778 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) |
| 1779 | { |
| 1780 | struct inode *inode = entry->d_inode; |
| 1781 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1782 | struct fuse_req *req; |
| 1783 | struct fuse_getxattr_in inarg; |
| 1784 | struct fuse_getxattr_out outarg; |
| 1785 | ssize_t ret; |
| 1786 | |
Anatol Pomozov | c2132c1 | 2013-01-14 22:30:00 -0800 | [diff] [blame] | 1787 | if (!fuse_allow_current_process(fc)) |
Miklos Szeredi | e57ac68 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 1788 | return -EACCES; |
| 1789 | |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1790 | if (fc->no_listxattr) |
| 1791 | return -EOPNOTSUPP; |
| 1792 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1793 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1794 | if (IS_ERR(req)) |
| 1795 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1796 | |
| 1797 | memset(&inarg, 0, sizeof(inarg)); |
| 1798 | inarg.size = size; |
| 1799 | req->in.h.opcode = FUSE_LISTXATTR; |
| 1800 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1801 | req->in.numargs = 1; |
| 1802 | req->in.args[0].size = sizeof(inarg); |
| 1803 | req->in.args[0].value = &inarg; |
| 1804 | /* This is really two different operations rolled into one */ |
| 1805 | req->out.numargs = 1; |
| 1806 | if (size) { |
| 1807 | req->out.argvar = 1; |
| 1808 | req->out.args[0].size = size; |
| 1809 | req->out.args[0].value = list; |
| 1810 | } else { |
| 1811 | req->out.args[0].size = sizeof(outarg); |
| 1812 | req->out.args[0].value = &outarg; |
| 1813 | } |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1814 | fuse_request_send(fc, req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1815 | ret = req->out.h.error; |
| 1816 | if (!ret) |
| 1817 | ret = size ? req->out.args[0].size : outarg.size; |
| 1818 | else { |
| 1819 | if (ret == -ENOSYS) { |
| 1820 | fc->no_listxattr = 1; |
| 1821 | ret = -EOPNOTSUPP; |
| 1822 | } |
| 1823 | } |
| 1824 | fuse_put_request(fc, req); |
| 1825 | return ret; |
| 1826 | } |
| 1827 | |
| 1828 | static int fuse_removexattr(struct dentry *entry, const char *name) |
| 1829 | { |
| 1830 | struct inode *inode = entry->d_inode; |
| 1831 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1832 | struct fuse_req *req; |
| 1833 | int err; |
| 1834 | |
| 1835 | if (fc->no_removexattr) |
| 1836 | return -EOPNOTSUPP; |
| 1837 | |
Maxim Patlasov | b111c8c | 2012-10-26 19:48:30 +0400 | [diff] [blame] | 1838 | req = fuse_get_req_nopages(fc); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 1839 | if (IS_ERR(req)) |
| 1840 | return PTR_ERR(req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1841 | |
| 1842 | req->in.h.opcode = FUSE_REMOVEXATTR; |
| 1843 | req->in.h.nodeid = get_node_id(inode); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1844 | req->in.numargs = 1; |
| 1845 | req->in.args[0].size = strlen(name) + 1; |
| 1846 | req->in.args[0].value = name; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 1847 | fuse_request_send(fc, req); |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1848 | err = req->out.h.error; |
| 1849 | fuse_put_request(fc, req); |
| 1850 | if (err == -ENOSYS) { |
| 1851 | fc->no_removexattr = 1; |
| 1852 | err = -EOPNOTSUPP; |
| 1853 | } |
| 1854 | return err; |
| 1855 | } |
| 1856 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1857 | static const struct inode_operations fuse_dir_inode_operations = { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1858 | .lookup = fuse_lookup, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1859 | .mkdir = fuse_mkdir, |
| 1860 | .symlink = fuse_symlink, |
| 1861 | .unlink = fuse_unlink, |
| 1862 | .rmdir = fuse_rmdir, |
| 1863 | .rename = fuse_rename, |
| 1864 | .link = fuse_link, |
| 1865 | .setattr = fuse_setattr, |
| 1866 | .create = fuse_create, |
Miklos Szeredi | c8ccbe0 | 2012-06-05 15:10:22 +0200 | [diff] [blame] | 1867 | .atomic_open = fuse_atomic_open, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1868 | .mknod = fuse_mknod, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1869 | .permission = fuse_permission, |
| 1870 | .getattr = fuse_getattr, |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1871 | .setxattr = fuse_setxattr, |
| 1872 | .getxattr = fuse_getxattr, |
| 1873 | .listxattr = fuse_listxattr, |
| 1874 | .removexattr = fuse_removexattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1875 | }; |
| 1876 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1877 | static const struct file_operations fuse_dir_operations = { |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 1878 | .llseek = generic_file_llseek, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1879 | .read = generic_read_dir, |
| 1880 | .readdir = fuse_readdir, |
| 1881 | .open = fuse_dir_open, |
| 1882 | .release = fuse_dir_release, |
Miklos Szeredi | 8254798 | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 1883 | .fsync = fuse_dir_fsync, |
Miklos Szeredi | b18da0c | 2011-12-13 11:58:49 +0100 | [diff] [blame] | 1884 | .unlocked_ioctl = fuse_dir_ioctl, |
| 1885 | .compat_ioctl = fuse_dir_compat_ioctl, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1886 | }; |
| 1887 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1888 | static const struct inode_operations fuse_common_inode_operations = { |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1889 | .setattr = fuse_setattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1890 | .permission = fuse_permission, |
| 1891 | .getattr = fuse_getattr, |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1892 | .setxattr = fuse_setxattr, |
| 1893 | .getxattr = fuse_getxattr, |
| 1894 | .listxattr = fuse_listxattr, |
| 1895 | .removexattr = fuse_removexattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1896 | }; |
| 1897 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1898 | static const struct inode_operations fuse_symlink_inode_operations = { |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 1899 | .setattr = fuse_setattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1900 | .follow_link = fuse_follow_link, |
| 1901 | .put_link = fuse_put_link, |
| 1902 | .readlink = generic_readlink, |
| 1903 | .getattr = fuse_getattr, |
Miklos Szeredi | 92a8780 | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 1904 | .setxattr = fuse_setxattr, |
| 1905 | .getxattr = fuse_getxattr, |
| 1906 | .listxattr = fuse_listxattr, |
| 1907 | .removexattr = fuse_removexattr, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 1908 | }; |
| 1909 | |
| 1910 | void fuse_init_common(struct inode *inode) |
| 1911 | { |
| 1912 | inode->i_op = &fuse_common_inode_operations; |
| 1913 | } |
| 1914 | |
| 1915 | void fuse_init_dir(struct inode *inode) |
| 1916 | { |
| 1917 | inode->i_op = &fuse_dir_inode_operations; |
| 1918 | inode->i_fop = &fuse_dir_operations; |
| 1919 | } |
| 1920 | |
| 1921 | void fuse_init_symlink(struct inode *inode) |
| 1922 | { |
| 1923 | inode->i_op = &fuse_symlink_inode_operations; |
| 1924 | } |