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