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