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