blob: 3076f48d86a6fbd41435195c3f6cf6a232bcf212 [file] [log] [blame]
Miklos Szeredie5e55582005-09-09 13:10:28 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredie5e55582005-09-09 13:10:28 -07004
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 Szeredie5e55582005-09-09 13:10:28 -070013#include <linux/sched.h>
14#include <linux/namei.h>
Miklos Szeredi07e77dc2010-12-07 20:16:56 +010015#include <linux/slab.h>
Seth Forshee703c7362016-08-29 08:46:36 -050016#include <linux/xattr.h>
Seth Forshee60bcc882016-08-29 08:46:37 -050017#include <linux/posix_acl.h>
Miklos Szeredie5e55582005-09-09 13:10:28 -070018
Al Viro8d3af7f2013-05-18 03:03:58 -040019static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
Feng Shuo4582a4a2013-01-15 11:23:28 +080020{
21 struct fuse_conn *fc = get_fuse_conn(dir);
22 struct fuse_inode *fi = get_fuse_inode(dir);
23
24 if (!fc->do_readdirplus)
25 return false;
Eric Wong634734b2013-02-06 22:29:01 +000026 if (!fc->readdirplus_auto)
27 return true;
Feng Shuo4582a4a2013-01-15 11:23:28 +080028 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
29 return true;
Al Viro8d3af7f2013-05-18 03:03:58 -040030 if (ctx->pos == 0)
Feng Shuo4582a4a2013-01-15 11:23:28 +080031 return true;
32 return false;
33}
34
35static void fuse_advise_use_readdirplus(struct inode *dir)
36{
37 struct fuse_inode *fi = get_fuse_inode(dir);
38
39 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
40}
41
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070042#if BITS_PER_LONG >= 64
43static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
44{
45 entry->d_time = time;
46}
47
48static inline u64 fuse_dentry_time(struct dentry *entry)
49{
50 return entry->d_time;
51}
52#else
53/*
54 * On 32 bit archs store the high 32 bits of time in d_fsdata
55 */
56static void fuse_dentry_settime(struct dentry *entry, u64 time)
57{
58 entry->d_time = time;
59 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
60}
61
62static u64 fuse_dentry_time(struct dentry *entry)
63{
64 return (u64) entry->d_time +
65 ((u64) (unsigned long) entry->d_fsdata << 32);
66}
67#endif
68
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080069/*
70 * FUSE caches dentries and attributes with separate timeout. The
71 * time in jiffies until the dentry/attributes are valid is stored in
72 * dentry->d_time and fuse_inode->i_time respectively.
73 */
74
75/*
76 * Calculate the time in jiffies until a dentry/attributes are valid
77 */
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070078static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
Miklos Szeredie5e55582005-09-09 13:10:28 -070079{
Miklos Szeredi685d16d2006-07-30 03:04:08 -070080 if (sec || nsec) {
81 struct timespec ts = {sec, nsec};
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070082 return get_jiffies_64() + timespec_to_jiffies(&ts);
Miklos Szeredi685d16d2006-07-30 03:04:08 -070083 } else
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070084 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070085}
86
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080087/*
88 * Set dentry and possibly attribute timeouts from the lookup/mk*
89 * replies
90 */
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070091static void fuse_change_entry_timeout(struct dentry *entry,
92 struct fuse_entry_out *o)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080093{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070094 fuse_dentry_settime(entry,
95 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070096}
97
98static u64 attr_timeout(struct fuse_attr_out *o)
99{
100 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
101}
102
103static u64 entry_attr_timeout(struct fuse_entry_out *o)
104{
105 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800106}
107
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800108/*
109 * Mark the attributes as stale, so that at the next call to
110 * ->getattr() they will be fetched from userspace
111 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800112void fuse_invalidate_attr(struct inode *inode)
113{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700114 get_fuse_inode(inode)->i_time = 0;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800115}
116
Andrew Gallagher451418f2013-11-05 03:55:43 -0800117/**
118 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
119 * atime is not used.
120 */
121void fuse_invalidate_atime(struct inode *inode)
122{
123 if (!IS_RDONLY(inode))
124 fuse_invalidate_attr(inode);
125}
126
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800127/*
128 * Just mark the entry as stale, so that a next attempt to look it up
129 * will result in a new lookup call to userspace
130 *
131 * This is called when a dentry is about to become negative and the
132 * timeout is unknown (unlink, rmdir, rename and in some cases
133 * lookup)
134 */
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700135void fuse_invalidate_entry_cache(struct dentry *entry)
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800136{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700137 fuse_dentry_settime(entry, 0);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800138}
139
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800140/*
141 * Same as fuse_invalidate_entry_cache(), but also try to remove the
142 * dentry from the hash
143 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800144static void fuse_invalidate_entry(struct dentry *entry)
145{
146 d_invalidate(entry);
147 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800148}
149
Miklos Szeredi70781872014-12-12 09:49:05 +0100150static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
Al Viro13983d02016-07-20 22:34:44 -0400151 u64 nodeid, const struct qstr *name,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700152 struct fuse_entry_out *outarg)
153{
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700154 memset(outarg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi70781872014-12-12 09:49:05 +0100155 args->in.h.opcode = FUSE_LOOKUP;
156 args->in.h.nodeid = nodeid;
157 args->in.numargs = 1;
158 args->in.args[0].size = name->len + 1;
159 args->in.args[0].value = name->name;
160 args->out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100161 args->out.args[0].size = sizeof(struct fuse_entry_out);
Miklos Szeredi70781872014-12-12 09:49:05 +0100162 args->out.args[0].value = outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700163}
164
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700165u64 fuse_get_attr_version(struct fuse_conn *fc)
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800166{
167 u64 curr_version;
168
169 /*
170 * The spin lock isn't actually needed on 64bit archs, but we
171 * don't yet care too much about such optimizations.
172 */
173 spin_lock(&fc->lock);
174 curr_version = fc->attr_version;
175 spin_unlock(&fc->lock);
176
177 return curr_version;
178}
179
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800180/*
181 * Check whether the dentry is still valid
182 *
183 * If the entry validity timeout has expired and the dentry is
184 * positive, try to redo the lookup. If the lookup results in a
185 * different inode, then let the VFS invalidate the dentry and redo
186 * the lookup once more. If the lookup results in the same inode,
187 * then refresh the attributes, timeouts and mark the dentry valid.
188 */
Al Viro0b728e12012-06-10 16:03:43 -0400189static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700190{
Nick Piggin34286d62011-01-07 17:49:57 +1100191 struct inode *inode;
Miklos Szeredi28420da2013-06-03 14:40:22 +0200192 struct dentry *parent;
193 struct fuse_conn *fc;
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200194 struct fuse_inode *fi;
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200195 int ret;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800196
David Howells2b0143b2015-03-17 22:25:59 +0000197 inode = d_inode_rcu(entry);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800198 if (inode && is_bad_inode(inode))
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200199 goto invalid;
Anand Avati154210c2014-06-26 20:21:57 -0400200 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
201 (flags & LOOKUP_REVAL)) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700202 struct fuse_entry_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +0100203 FUSE_ARGS(args);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100204 struct fuse_forget_link *forget;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700205 u64 attr_version;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800206
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800207 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800208 if (!inode)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200209 goto invalid;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800210
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200211 ret = -ECHILD;
Al Viro0b728e12012-06-10 16:03:43 -0400212 if (flags & LOOKUP_RCU)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200213 goto out;
Miklos Szeredie7c0a162011-03-21 13:58:06 +0100214
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800215 fc = get_fuse_conn(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700216
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100217 forget = fuse_alloc_forget();
Miklos Szeredi70781872014-12-12 09:49:05 +0100218 ret = -ENOMEM;
219 if (!forget)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200220 goto out;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800221
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800222 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700223
Miklos Szeredie956edd2006-10-17 00:10:12 -0700224 parent = dget_parent(entry);
David Howells2b0143b2015-03-17 22:25:59 +0000225 fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700226 &entry->d_name, &outarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100227 ret = fuse_simple_request(fc, &args);
Miklos Szeredie956edd2006-10-17 00:10:12 -0700228 dput(parent);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800229 /* Zero nodeid is same as -ENOENT */
Miklos Szeredi70781872014-12-12 09:49:05 +0100230 if (!ret && !outarg.nodeid)
231 ret = -ENOENT;
232 if (!ret) {
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200233 fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700234 if (outarg.nodeid != get_node_id(inode)) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100235 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200236 goto invalid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700237 }
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700238 spin_lock(&fc->lock);
Miklos Szeredi1729a162008-11-26 12:03:54 +0100239 fi->nlookup++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700240 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700241 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100242 kfree(forget);
Miklos Szeredi70781872014-12-12 09:49:05 +0100243 if (ret == -ENOMEM)
244 goto out;
245 if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200246 goto invalid;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700247
Seth Forshee60bcc882016-08-29 08:46:37 -0500248 forget_all_cached_acls(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700249 fuse_change_attributes(inode, &outarg.attr,
250 entry_attr_timeout(&outarg),
251 attr_version);
252 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi28420da2013-06-03 14:40:22 +0200253 } else if (inode) {
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200254 fi = get_fuse_inode(inode);
255 if (flags & LOOKUP_RCU) {
256 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
257 return -ECHILD;
258 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
Miklos Szeredi28420da2013-06-03 14:40:22 +0200259 parent = dget_parent(entry);
David Howells2b0143b2015-03-17 22:25:59 +0000260 fuse_advise_use_readdirplus(d_inode(parent));
Miklos Szeredi28420da2013-06-03 14:40:22 +0200261 dput(parent);
262 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700263 }
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200264 ret = 1;
265out:
266 return ret;
267
268invalid:
269 ret = 0;
270 goto out;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700271}
272
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800273static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800274{
275 return !nodeid || nodeid == FUSE_ROOT_ID;
276}
277
Al Viro42695902009-02-20 05:59:13 +0000278const struct dentry_operations fuse_dentry_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700279 .d_revalidate = fuse_dentry_revalidate,
280};
281
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700282int fuse_valid_type(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800283{
284 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
285 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
286}
287
Al Viro13983d02016-07-20 22:34:44 -0400288int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700289 struct fuse_entry_out *outarg, struct inode **inode)
290{
291 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredi70781872014-12-12 09:49:05 +0100292 FUSE_ARGS(args);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100293 struct fuse_forget_link *forget;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700294 u64 attr_version;
295 int err;
296
297 *inode = NULL;
298 err = -ENAMETOOLONG;
299 if (name->len > FUSE_NAME_MAX)
300 goto out;
301
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700302
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100303 forget = fuse_alloc_forget();
304 err = -ENOMEM;
Miklos Szeredi70781872014-12-12 09:49:05 +0100305 if (!forget)
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700306 goto out;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700307
308 attr_version = fuse_get_attr_version(fc);
309
Miklos Szeredi70781872014-12-12 09:49:05 +0100310 fuse_lookup_init(fc, &args, nodeid, name, outarg);
311 err = fuse_simple_request(fc, &args);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700312 /* Zero nodeid is same as -ENOENT, but with valid timeout */
313 if (err || !outarg->nodeid)
314 goto out_put_forget;
315
316 err = -EIO;
317 if (!outarg->nodeid)
318 goto out_put_forget;
319 if (!fuse_valid_type(outarg->attr.mode))
320 goto out_put_forget;
321
322 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
323 &outarg->attr, entry_attr_timeout(outarg),
324 attr_version);
325 err = -ENOMEM;
326 if (!*inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100327 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700328 goto out;
329 }
330 err = 0;
331
332 out_put_forget:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100333 kfree(forget);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700334 out:
335 return err;
336}
337
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800338static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400339 unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700340{
341 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700342 struct fuse_entry_out outarg;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700343 struct inode *inode;
Miklos Szeredi0de62562008-07-25 01:48:59 -0700344 struct dentry *newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700345 bool outarg_valid = true;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700346
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200347 fuse_lock_inode(dir);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700348 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
349 &outarg, &inode);
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200350 fuse_unlock_inode(dir);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700351 if (err == -ENOENT) {
352 outarg_valid = false;
353 err = 0;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800354 }
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700355 if (err)
356 goto out_err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800357
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700358 err = -EIO;
359 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
360 goto out_iput;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700361
Al Viro41d28bc2014-10-12 22:24:21 -0400362 newent = d_splice_alias(inode, entry);
Miklos Szeredi5835f332013-09-05 11:44:42 +0200363 err = PTR_ERR(newent);
364 if (IS_ERR(newent))
365 goto out_err;
Miklos Szeredid2a85162006-10-17 00:10:11 -0700366
Miklos Szeredi0de62562008-07-25 01:48:59 -0700367 entry = newent ? newent : entry;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700368 if (outarg_valid)
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700369 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800370 else
371 fuse_invalidate_entry_cache(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700372
Feng Shuo4582a4a2013-01-15 11:23:28 +0800373 fuse_advise_use_readdirplus(dir);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700374 return newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700375
376 out_iput:
377 iput(inode);
378 out_err:
379 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700380}
381
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800382/*
383 * Atomic create+open operation
384 *
385 * If the filesystem doesn't support this, then fall back to separate
386 * 'mknod' + 'open' requests.
387 */
Al Virod9585272012-06-22 12:39:14 +0400388static int fuse_create_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400389 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400390 umode_t mode, int *opened)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800391{
392 int err;
393 struct inode *inode;
394 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100395 FUSE_ARGS(args);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100396 struct fuse_forget_link *forget;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200397 struct fuse_create_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800398 struct fuse_open_out outopen;
399 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800400 struct fuse_file *ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800401
Miklos Szerediaf109bc2012-08-15 13:01:24 +0200402 /* Userspace expects S_IFREG in create mode */
403 BUG_ON((mode & S_IFMT) != S_IFREG);
404
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100405 forget = fuse_alloc_forget();
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200406 err = -ENOMEM;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100407 if (!forget)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200408 goto out_err;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700409
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700410 err = -ENOMEM;
Tejun Heoacf99432008-11-26 12:03:55 +0100411 ff = fuse_file_alloc(fc);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800412 if (!ff)
Miklos Szeredi70781872014-12-12 09:49:05 +0100413 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800414
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200415 if (!fc->dont_mask)
416 mode &= ~current_umask();
417
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800418 flags &= ~O_NOCTTY;
419 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700420 memset(&outentry, 0, sizeof(outentry));
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800421 inarg.flags = flags;
422 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200423 inarg.umask = current_umask();
Miklos Szeredi70781872014-12-12 09:49:05 +0100424 args.in.h.opcode = FUSE_CREATE;
425 args.in.h.nodeid = get_node_id(dir);
426 args.in.numargs = 2;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100427 args.in.args[0].size = sizeof(inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100428 args.in.args[0].value = &inarg;
429 args.in.args[1].size = entry->d_name.len + 1;
430 args.in.args[1].value = entry->d_name.name;
431 args.out.numargs = 2;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100432 args.out.args[0].size = sizeof(outentry);
Miklos Szeredi70781872014-12-12 09:49:05 +0100433 args.out.args[0].value = &outentry;
434 args.out.args[1].size = sizeof(outopen);
435 args.out.args[1].value = &outopen;
436 err = fuse_simple_request(fc, &args);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200437 if (err)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800438 goto out_free_ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800439
440 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800441 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800442 goto out_free_ff;
443
Miklos Szeredic7b71432009-04-28 16:56:37 +0200444 ff->fh = outopen.fh;
445 ff->nodeid = outentry.nodeid;
446 ff->open_flags = outopen.open_flags;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800447 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700448 &outentry.attr, entry_attr_timeout(&outentry), 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800449 if (!inode) {
450 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200451 fuse_sync_release(ff, flags);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100452 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200453 err = -ENOMEM;
454 goto out_err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800455 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100456 kfree(forget);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800457 d_instantiate(entry, inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700458 fuse_change_entry_timeout(entry, &outentry);
Miklos Szeredi0952b2a2008-02-06 01:38:38 -0800459 fuse_invalidate_attr(dir);
Al Viro30d90492012-06-22 12:40:19 +0400460 err = finish_open(file, entry, generic_file_open, opened);
461 if (err) {
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200462 fuse_sync_release(ff, flags);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200463 } else {
464 file->private_data = fuse_file_get(ff);
465 fuse_finish_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800466 }
Al Virod9585272012-06-22 12:39:14 +0400467 return err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800468
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200469out_free_ff:
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800470 fuse_file_free(ff);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200471out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100472 kfree(forget);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200473out_err:
Al Virod9585272012-06-22 12:39:14 +0400474 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200475}
476
477static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
Al Virod9585272012-06-22 12:39:14 +0400478static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400479 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400480 umode_t mode, int *opened)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200481{
482 int err;
483 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200484 struct dentry *res = NULL;
485
Al Viro00699ad2016-07-05 09:44:53 -0400486 if (d_in_lookup(entry)) {
Al Viro00cd8dd2012-06-10 17:13:09 -0400487 res = fuse_lookup(dir, entry, 0);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200488 if (IS_ERR(res))
Al Virod9585272012-06-22 12:39:14 +0400489 return PTR_ERR(res);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200490
491 if (res)
492 entry = res;
493 }
494
David Howells2b0143b2015-03-17 22:25:59 +0000495 if (!(flags & O_CREAT) || d_really_is_positive(entry))
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200496 goto no_open;
497
498 /* Only creates */
Al Viro47237682012-06-10 05:01:45 -0400499 *opened |= FILE_CREATED;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200500
501 if (fc->no_create)
502 goto mknod;
503
Al Viro30d90492012-06-22 12:40:19 +0400504 err = fuse_create_open(dir, entry, file, flags, mode, opened);
Al Virod9585272012-06-22 12:39:14 +0400505 if (err == -ENOSYS) {
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200506 fc->no_create = 1;
507 goto mknod;
508 }
509out_dput:
510 dput(res);
Al Virod9585272012-06-22 12:39:14 +0400511 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200512
513mknod:
514 err = fuse_mknod(dir, entry, mode, 0);
Al Virod9585272012-06-22 12:39:14 +0400515 if (err)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200516 goto out_dput;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200517no_open:
Al Viroe45198a2012-06-10 06:48:09 -0400518 return finish_no_open(file, res);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800519}
520
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800521/*
522 * Code shared between mknod, mkdir, symlink and link
523 */
Miklos Szeredi70781872014-12-12 09:49:05 +0100524static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700525 struct inode *dir, struct dentry *entry,
Al Viro541af6a2011-07-26 03:17:33 -0400526 umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700527{
528 struct fuse_entry_out outarg;
529 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700530 int err;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100531 struct fuse_forget_link *forget;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800532
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100533 forget = fuse_alloc_forget();
Miklos Szeredi70781872014-12-12 09:49:05 +0100534 if (!forget)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100535 return -ENOMEM;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700536
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700537 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredi70781872014-12-12 09:49:05 +0100538 args->in.h.nodeid = get_node_id(dir);
539 args->out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100540 args->out.args[0].size = sizeof(outarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100541 args->out.args[0].value = &outarg;
542 err = fuse_simple_request(fc, args);
Miklos Szeredi2d510132006-11-25 11:09:20 -0800543 if (err)
544 goto out_put_forget_req;
545
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800546 err = -EIO;
547 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800548 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800549
550 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800551 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800552
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700553 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700554 &outarg.attr, entry_attr_timeout(&outarg), 0);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700555 if (!inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100556 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700557 return -ENOMEM;
558 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100559 kfree(forget);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700560
Miklos Szeredib70a80e2013-10-01 16:44:54 +0200561 err = d_instantiate_no_diralias(entry, inode);
562 if (err)
563 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700564
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700565 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700566 fuse_invalidate_attr(dir);
567 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800568
Miklos Szeredi2d510132006-11-25 11:09:20 -0800569 out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100570 kfree(forget);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800571 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700572}
573
Al Viro1a67aaf2011-07-26 01:52:52 -0400574static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700575 dev_t rdev)
576{
577 struct fuse_mknod_in inarg;
578 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100579 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700580
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200581 if (!fc->dont_mask)
582 mode &= ~current_umask();
583
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700584 memset(&inarg, 0, sizeof(inarg));
585 inarg.mode = mode;
586 inarg.rdev = new_encode_dev(rdev);
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200587 inarg.umask = current_umask();
Miklos Szeredi70781872014-12-12 09:49:05 +0100588 args.in.h.opcode = FUSE_MKNOD;
589 args.in.numargs = 2;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100590 args.in.args[0].size = sizeof(inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100591 args.in.args[0].value = &inarg;
592 args.in.args[1].size = entry->d_name.len + 1;
593 args.in.args[1].value = entry->d_name.name;
594 return create_new_entry(fc, &args, dir, entry, mode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700595}
596
Al Viro4acdaf22011-07-26 01:42:34 -0400597static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400598 bool excl)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700599{
600 return fuse_mknod(dir, entry, mode, 0);
601}
602
Al Viro18bb1db2011-07-26 01:41:39 -0400603static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700604{
605 struct fuse_mkdir_in inarg;
606 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100607 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700608
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200609 if (!fc->dont_mask)
610 mode &= ~current_umask();
611
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700612 memset(&inarg, 0, sizeof(inarg));
613 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200614 inarg.umask = current_umask();
Miklos Szeredi70781872014-12-12 09:49:05 +0100615 args.in.h.opcode = FUSE_MKDIR;
616 args.in.numargs = 2;
617 args.in.args[0].size = sizeof(inarg);
618 args.in.args[0].value = &inarg;
619 args.in.args[1].size = entry->d_name.len + 1;
620 args.in.args[1].value = entry->d_name.name;
621 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700622}
623
624static int fuse_symlink(struct inode *dir, struct dentry *entry,
625 const char *link)
626{
627 struct fuse_conn *fc = get_fuse_conn(dir);
628 unsigned len = strlen(link) + 1;
Miklos Szeredi70781872014-12-12 09:49:05 +0100629 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700630
Miklos Szeredi70781872014-12-12 09:49:05 +0100631 args.in.h.opcode = FUSE_SYMLINK;
632 args.in.numargs = 2;
633 args.in.args[0].size = entry->d_name.len + 1;
634 args.in.args[0].value = entry->d_name.name;
635 args.in.args[1].size = len;
636 args.in.args[1].value = link;
637 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700638}
639
Seth Forshee703c7362016-08-29 08:46:36 -0500640void fuse_update_ctime(struct inode *inode)
Maxim Patlasov31f32672014-04-28 14:19:24 +0200641{
642 if (!IS_NOCMTIME(inode)) {
643 inode->i_ctime = current_fs_time(inode->i_sb);
644 mark_inode_dirty_sync(inode);
645 }
646}
647
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700648static int fuse_unlink(struct inode *dir, struct dentry *entry)
649{
650 int err;
651 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100652 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700653
Miklos Szeredi70781872014-12-12 09:49:05 +0100654 args.in.h.opcode = FUSE_UNLINK;
655 args.in.h.nodeid = get_node_id(dir);
656 args.in.numargs = 1;
657 args.in.args[0].size = entry->d_name.len + 1;
658 args.in.args[0].value = entry->d_name.name;
659 err = fuse_simple_request(fc, &args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700660 if (!err) {
David Howells2b0143b2015-03-17 22:25:59 +0000661 struct inode *inode = d_inode(entry);
Miklos Szerediac45d612012-03-05 15:48:11 +0100662 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700663
Miklos Szerediac45d612012-03-05 15:48:11 +0100664 spin_lock(&fc->lock);
665 fi->attr_version = ++fc->attr_version;
Miklos Szeredidfca7ce2013-02-04 15:57:42 +0100666 /*
667 * If i_nlink == 0 then unlink doesn't make sense, yet this can
668 * happen if userspace filesystem is careless. It would be
669 * difficult to enforce correct nlink usage so just ignore this
670 * condition here
671 */
672 if (inode->i_nlink > 0)
673 drop_nlink(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100674 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700675 fuse_invalidate_attr(inode);
676 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800677 fuse_invalidate_entry_cache(entry);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200678 fuse_update_ctime(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700679 } else if (err == -EINTR)
680 fuse_invalidate_entry(entry);
681 return err;
682}
683
684static int fuse_rmdir(struct inode *dir, struct dentry *entry)
685{
686 int err;
687 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100688 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700689
Miklos Szeredi70781872014-12-12 09:49:05 +0100690 args.in.h.opcode = FUSE_RMDIR;
691 args.in.h.nodeid = get_node_id(dir);
692 args.in.numargs = 1;
693 args.in.args[0].size = entry->d_name.len + 1;
694 args.in.args[0].value = entry->d_name.name;
695 err = fuse_simple_request(fc, &args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700696 if (!err) {
David Howells2b0143b2015-03-17 22:25:59 +0000697 clear_nlink(d_inode(entry));
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700698 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800699 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700700 } else if (err == -EINTR)
701 fuse_invalidate_entry(entry);
702 return err;
703}
704
Miklos Szeredi1560c972014-04-28 16:43:44 +0200705static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
706 struct inode *newdir, struct dentry *newent,
707 unsigned int flags, int opcode, size_t argsize)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700708{
709 int err;
Miklos Szeredi1560c972014-04-28 16:43:44 +0200710 struct fuse_rename2_in inarg;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700711 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100712 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700713
Miklos Szeredi1560c972014-04-28 16:43:44 +0200714 memset(&inarg, 0, argsize);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700715 inarg.newdir = get_node_id(newdir);
Miklos Szeredi1560c972014-04-28 16:43:44 +0200716 inarg.flags = flags;
Miklos Szeredi70781872014-12-12 09:49:05 +0100717 args.in.h.opcode = opcode;
718 args.in.h.nodeid = get_node_id(olddir);
719 args.in.numargs = 3;
720 args.in.args[0].size = argsize;
721 args.in.args[0].value = &inarg;
722 args.in.args[1].size = oldent->d_name.len + 1;
723 args.in.args[1].value = oldent->d_name.name;
724 args.in.args[2].size = newent->d_name.len + 1;
725 args.in.args[2].value = newent->d_name.name;
726 err = fuse_simple_request(fc, &args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700727 if (!err) {
Miklos Szeredi08b63302007-11-28 16:22:03 -0800728 /* ctime changes */
David Howells2b0143b2015-03-17 22:25:59 +0000729 fuse_invalidate_attr(d_inode(oldent));
730 fuse_update_ctime(d_inode(oldent));
Miklos Szeredi08b63302007-11-28 16:22:03 -0800731
Miklos Szeredi1560c972014-04-28 16:43:44 +0200732 if (flags & RENAME_EXCHANGE) {
David Howells2b0143b2015-03-17 22:25:59 +0000733 fuse_invalidate_attr(d_inode(newent));
734 fuse_update_ctime(d_inode(newent));
Miklos Szeredi1560c972014-04-28 16:43:44 +0200735 }
736
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700737 fuse_invalidate_attr(olddir);
738 if (olddir != newdir)
739 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800740
741 /* newent will end up negative */
David Howells2b0143b2015-03-17 22:25:59 +0000742 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
743 fuse_invalidate_attr(d_inode(newent));
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800744 fuse_invalidate_entry_cache(newent);
David Howells2b0143b2015-03-17 22:25:59 +0000745 fuse_update_ctime(d_inode(newent));
Miklos Szeredi5219f342009-11-04 10:24:52 +0100746 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700747 } else if (err == -EINTR) {
748 /* If request was interrupted, DEITY only knows if the
749 rename actually took place. If the invalidation
750 fails (e.g. some process has CWD under the renamed
751 directory), then there can be inconsistency between
752 the dcache and the real filesystem. Tough luck. */
753 fuse_invalidate_entry(oldent);
David Howells2b0143b2015-03-17 22:25:59 +0000754 if (d_really_is_positive(newent))
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700755 fuse_invalidate_entry(newent);
756 }
757
758 return err;
759}
760
Miklos Szeredi1560c972014-04-28 16:43:44 +0200761static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
762 struct inode *newdir, struct dentry *newent,
763 unsigned int flags)
764{
765 struct fuse_conn *fc = get_fuse_conn(olddir);
766 int err;
767
768 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
769 return -EINVAL;
770
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200771 if (flags) {
772 if (fc->no_rename2 || fc->minor < 23)
773 return -EINVAL;
Miklos Szeredi1560c972014-04-28 16:43:44 +0200774
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200775 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
776 FUSE_RENAME2,
777 sizeof(struct fuse_rename2_in));
778 if (err == -ENOSYS) {
779 fc->no_rename2 = 1;
780 err = -EINVAL;
781 }
782 } else {
783 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
784 FUSE_RENAME,
785 sizeof(struct fuse_rename_in));
Miklos Szeredi1560c972014-04-28 16:43:44 +0200786 }
Miklos Szeredi1560c972014-04-28 16:43:44 +0200787
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200788 return err;
789}
790
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700791static int fuse_link(struct dentry *entry, struct inode *newdir,
792 struct dentry *newent)
793{
794 int err;
795 struct fuse_link_in inarg;
David Howells2b0143b2015-03-17 22:25:59 +0000796 struct inode *inode = d_inode(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700797 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +0100798 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700799
800 memset(&inarg, 0, sizeof(inarg));
801 inarg.oldnodeid = get_node_id(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +0100802 args.in.h.opcode = FUSE_LINK;
803 args.in.numargs = 2;
804 args.in.args[0].size = sizeof(inarg);
805 args.in.args[0].value = &inarg;
806 args.in.args[1].size = newent->d_name.len + 1;
807 args.in.args[1].value = newent->d_name.name;
808 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700809 /* Contrary to "normal" filesystems it can happen that link
810 makes two "logical" inodes point to the same "physical"
811 inode. We invalidate the attributes of the old one, so it
812 will reflect changes in the backing inode (link count,
813 etc.)
814 */
Miklos Szerediac45d612012-03-05 15:48:11 +0100815 if (!err) {
816 struct fuse_inode *fi = get_fuse_inode(inode);
817
818 spin_lock(&fc->lock);
819 fi->attr_version = ++fc->attr_version;
820 inc_nlink(inode);
821 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700822 fuse_invalidate_attr(inode);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200823 fuse_update_ctime(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100824 } else if (err == -EINTR) {
825 fuse_invalidate_attr(inode);
826 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700827 return err;
828}
829
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700830static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
831 struct kstat *stat)
832{
Miklos Szeredi203627b2012-05-10 19:49:38 +0400833 unsigned int blkbits;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400834 struct fuse_conn *fc = get_fuse_conn(inode);
835
836 /* see the comment in fuse_change_attributes() */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400837 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
Pavel Emelyanov83732002013-10-10 17:10:46 +0400838 attr->size = i_size_read(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400839 attr->mtime = inode->i_mtime.tv_sec;
840 attr->mtimensec = inode->i_mtime.tv_nsec;
Maxim Patlasov31f32672014-04-28 14:19:24 +0200841 attr->ctime = inode->i_ctime.tv_sec;
842 attr->ctimensec = inode->i_ctime.tv_nsec;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400843 }
Miklos Szeredi203627b2012-05-10 19:49:38 +0400844
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700845 stat->dev = inode->i_sb->s_dev;
846 stat->ino = attr->ino;
847 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
848 stat->nlink = attr->nlink;
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800849 stat->uid = make_kuid(&init_user_ns, attr->uid);
850 stat->gid = make_kgid(&init_user_ns, attr->gid);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700851 stat->rdev = inode->i_rdev;
852 stat->atime.tv_sec = attr->atime;
853 stat->atime.tv_nsec = attr->atimensec;
854 stat->mtime.tv_sec = attr->mtime;
855 stat->mtime.tv_nsec = attr->mtimensec;
856 stat->ctime.tv_sec = attr->ctime;
857 stat->ctime.tv_nsec = attr->ctimensec;
858 stat->size = attr->size;
859 stat->blocks = attr->blocks;
Miklos Szeredi203627b2012-05-10 19:49:38 +0400860
861 if (attr->blksize != 0)
862 blkbits = ilog2(attr->blksize);
863 else
864 blkbits = inode->i_sb->s_blocksize_bits;
865
866 stat->blksize = 1 << blkbits;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700867}
868
Miklos Szeredic79e3222007-10-18 03:06:59 -0700869static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
870 struct file *file)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700871{
872 int err;
Miklos Szeredic79e3222007-10-18 03:06:59 -0700873 struct fuse_getattr_in inarg;
874 struct fuse_attr_out outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700875 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +0100876 FUSE_ARGS(args);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700877 u64 attr_version;
878
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800879 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700880
Miklos Szeredic79e3222007-10-18 03:06:59 -0700881 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700882 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredic79e3222007-10-18 03:06:59 -0700883 /* Directories have separate file-handle space */
884 if (file && S_ISREG(inode->i_mode)) {
885 struct fuse_file *ff = file->private_data;
886
887 inarg.getattr_flags |= FUSE_GETATTR_FH;
888 inarg.fh = ff->fh;
889 }
Miklos Szeredi70781872014-12-12 09:49:05 +0100890 args.in.h.opcode = FUSE_GETATTR;
891 args.in.h.nodeid = get_node_id(inode);
892 args.in.numargs = 1;
893 args.in.args[0].size = sizeof(inarg);
894 args.in.args[0].value = &inarg;
895 args.out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100896 args.out.args[0].size = sizeof(outarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100897 args.out.args[0].value = &outarg;
898 err = fuse_simple_request(fc, &args);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700899 if (!err) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700900 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700901 make_bad_inode(inode);
902 err = -EIO;
903 } else {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700904 fuse_change_attributes(inode, &outarg.attr,
905 attr_timeout(&outarg),
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700906 attr_version);
907 if (stat)
Miklos Szeredic79e3222007-10-18 03:06:59 -0700908 fuse_fillattr(inode, &outarg.attr, stat);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700909 }
910 }
911 return err;
912}
913
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800914int fuse_update_attributes(struct inode *inode, struct kstat *stat,
915 struct file *file, bool *refreshed)
916{
917 struct fuse_inode *fi = get_fuse_inode(inode);
918 int err;
919 bool r;
920
Miklos Szeredi126b9d42014-07-07 15:28:50 +0200921 if (time_before64(fi->i_time, get_jiffies_64())) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800922 r = true;
Seth Forshee60bcc882016-08-29 08:46:37 -0500923 forget_all_cached_acls(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800924 err = fuse_do_getattr(inode, stat, file);
925 } else {
926 r = false;
927 err = 0;
928 if (stat) {
929 generic_fillattr(inode, stat);
930 stat->mode = fi->orig_i_mode;
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +0400931 stat->ino = fi->orig_ino;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800932 }
933 }
934
935 if (refreshed != NULL)
936 *refreshed = r;
937
938 return err;
939}
940
John Muir3b463ae2009-05-31 11:13:57 -0400941int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
John Muir451d0f52011-12-06 21:50:06 +0100942 u64 child_nodeid, struct qstr *name)
John Muir3b463ae2009-05-31 11:13:57 -0400943{
944 int err = -ENOTDIR;
945 struct inode *parent;
946 struct dentry *dir;
947 struct dentry *entry;
948
949 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
950 if (!parent)
951 return -ENOENT;
952
Al Viro59551022016-01-22 15:40:57 -0500953 inode_lock(parent);
John Muir3b463ae2009-05-31 11:13:57 -0400954 if (!S_ISDIR(parent->i_mode))
955 goto unlock;
956
957 err = -ENOENT;
958 dir = d_find_alias(parent);
959 if (!dir)
960 goto unlock;
961
Linus Torvalds8387ff22016-06-10 07:51:30 -0700962 name->hash = full_name_hash(dir, name->name, name->len);
John Muir3b463ae2009-05-31 11:13:57 -0400963 entry = d_lookup(dir, name);
964 dput(dir);
965 if (!entry)
966 goto unlock;
967
968 fuse_invalidate_attr(parent);
969 fuse_invalidate_entry(entry);
John Muir451d0f52011-12-06 21:50:06 +0100970
David Howells2b0143b2015-03-17 22:25:59 +0000971 if (child_nodeid != 0 && d_really_is_positive(entry)) {
Al Viro59551022016-01-22 15:40:57 -0500972 inode_lock(d_inode(entry));
David Howells2b0143b2015-03-17 22:25:59 +0000973 if (get_node_id(d_inode(entry)) != child_nodeid) {
John Muir451d0f52011-12-06 21:50:06 +0100974 err = -ENOENT;
975 goto badentry;
976 }
977 if (d_mountpoint(entry)) {
978 err = -EBUSY;
979 goto badentry;
980 }
David Howellse36cb0b2015-01-29 12:02:35 +0000981 if (d_is_dir(entry)) {
John Muir451d0f52011-12-06 21:50:06 +0100982 shrink_dcache_parent(entry);
983 if (!simple_empty(entry)) {
984 err = -ENOTEMPTY;
985 goto badentry;
986 }
David Howells2b0143b2015-03-17 22:25:59 +0000987 d_inode(entry)->i_flags |= S_DEAD;
John Muir451d0f52011-12-06 21:50:06 +0100988 }
989 dont_mount(entry);
David Howells2b0143b2015-03-17 22:25:59 +0000990 clear_nlink(d_inode(entry));
John Muir451d0f52011-12-06 21:50:06 +0100991 err = 0;
992 badentry:
Al Viro59551022016-01-22 15:40:57 -0500993 inode_unlock(d_inode(entry));
John Muir451d0f52011-12-06 21:50:06 +0100994 if (!err)
995 d_delete(entry);
996 } else {
997 err = 0;
998 }
John Muir3b463ae2009-05-31 11:13:57 -0400999 dput(entry);
John Muir3b463ae2009-05-31 11:13:57 -04001000
1001 unlock:
Al Viro59551022016-01-22 15:40:57 -05001002 inode_unlock(parent);
John Muir3b463ae2009-05-31 11:13:57 -04001003 iput(parent);
1004 return err;
1005}
1006
Miklos Szeredi87729a52005-09-09 13:10:34 -07001007/*
1008 * Calling into a user-controlled filesystem gives the filesystem
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001009 * daemon ptrace-like capabilities over the current process. This
Miklos Szeredi87729a52005-09-09 13:10:34 -07001010 * means, that the filesystem daemon is able to record the exact
1011 * filesystem operations performed, and can also control the behavior
1012 * of the requester process in otherwise impossible ways. For example
1013 * it can delay the operation for arbitrary length of time allowing
1014 * DoS against the requester.
1015 *
1016 * For this reason only those processes can call into the filesystem,
1017 * for which the owner of the mount has ptrace privilege. This
1018 * excludes processes started by other users, suid or sgid processes.
1019 */
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001020int fuse_allow_current_process(struct fuse_conn *fc)
Miklos Szeredi87729a52005-09-09 13:10:34 -07001021{
David Howellsc69e8d92008-11-14 10:39:19 +11001022 const struct cred *cred;
David Howellsc69e8d92008-11-14 10:39:19 +11001023
Miklos Szeredi87729a52005-09-09 13:10:34 -07001024 if (fc->flags & FUSE_ALLOW_OTHER)
1025 return 1;
1026
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001027 cred = current_cred();
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001028 if (uid_eq(cred->euid, fc->user_id) &&
1029 uid_eq(cred->suid, fc->user_id) &&
1030 uid_eq(cred->uid, fc->user_id) &&
1031 gid_eq(cred->egid, fc->group_id) &&
1032 gid_eq(cred->sgid, fc->group_id) &&
1033 gid_eq(cred->gid, fc->group_id))
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001034 return 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001035
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001036 return 0;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001037}
1038
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001039static int fuse_access(struct inode *inode, int mask)
1040{
1041 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001042 FUSE_ARGS(args);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001043 struct fuse_access_in inarg;
1044 int err;
1045
Miklos Szeredi698fa1d2013-10-01 16:41:23 +02001046 BUG_ON(mask & MAY_NOT_BLOCK);
1047
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001048 if (fc->no_access)
1049 return 0;
1050
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001051 memset(&inarg, 0, sizeof(inarg));
Al Viroe6305c42008-07-15 21:03:57 -04001052 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
Miklos Szeredi70781872014-12-12 09:49:05 +01001053 args.in.h.opcode = FUSE_ACCESS;
1054 args.in.h.nodeid = get_node_id(inode);
1055 args.in.numargs = 1;
1056 args.in.args[0].size = sizeof(inarg);
1057 args.in.args[0].value = &inarg;
1058 err = fuse_simple_request(fc, &args);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001059 if (err == -ENOSYS) {
1060 fc->no_access = 1;
1061 err = 0;
1062 }
1063 return err;
1064}
1065
Al Viro10556cb2011-06-20 19:28:19 -04001066static int fuse_perm_getattr(struct inode *inode, int mask)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001067{
Al Viro10556cb2011-06-20 19:28:19 -04001068 if (mask & MAY_NOT_BLOCK)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001069 return -ECHILD;
1070
Seth Forshee60bcc882016-08-29 08:46:37 -05001071 forget_all_cached_acls(inode);
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001072 return fuse_do_getattr(inode, NULL, NULL);
1073}
1074
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001075/*
1076 * Check permission. The two basic access models of FUSE are:
1077 *
1078 * 1) Local access checking ('default_permissions' mount option) based
1079 * on file mode. This is the plain old disk filesystem permission
1080 * modell.
1081 *
1082 * 2) "Remote" access checking, where server is responsible for
1083 * checking permission in each inode operation. An exception to this
1084 * is if ->permission() was invoked from sys_access() in which case an
1085 * access request is sent. Execute permission is still checked
1086 * locally based on file mode.
1087 */
Al Viro10556cb2011-06-20 19:28:19 -04001088static int fuse_permission(struct inode *inode, int mask)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001089{
1090 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001091 bool refreshed = false;
1092 int err = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001093
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001094 if (!fuse_allow_current_process(fc))
Miklos Szeredie5e55582005-09-09 13:10:28 -07001095 return -EACCES;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001096
1097 /*
Miklos Szeredie8e96152007-10-16 23:31:06 -07001098 * If attributes are needed, refresh them before proceeding
Miklos Szeredi244f6382007-10-16 23:31:02 -07001099 */
Miklos Szeredie8e96152007-10-16 23:31:06 -07001100 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1101 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001102 struct fuse_inode *fi = get_fuse_inode(inode);
1103
Miklos Szeredi126b9d42014-07-07 15:28:50 +02001104 if (time_before64(fi->i_time, get_jiffies_64())) {
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001105 refreshed = true;
1106
Al Viro10556cb2011-06-20 19:28:19 -04001107 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001108 if (err)
1109 return err;
1110 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001111 }
1112
1113 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
Al Viro2830ba72011-06-20 19:16:29 -04001114 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001115
1116 /* If permission is denied, try to refresh file
1117 attributes. This is also needed, because the root
1118 node will at first have no permissions */
Miklos Szeredi244f6382007-10-16 23:31:02 -07001119 if (err == -EACCES && !refreshed) {
Al Viro10556cb2011-06-20 19:28:19 -04001120 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001121 if (!err)
Al Viro2830ba72011-06-20 19:16:29 -04001122 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001123 }
1124
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001125 /* Note: the opposite of the above test does not
1126 exist. So if permissions are revoked this won't be
1127 noticed immediately, only after the attribute
1128 timeout has expired */
Eric Paris9cfcac82010-07-23 11:43:51 -04001129 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
Miklos Szeredie8e96152007-10-16 23:31:06 -07001130 err = fuse_access(inode, mask);
1131 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1132 if (!(inode->i_mode & S_IXUGO)) {
1133 if (refreshed)
1134 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001135
Al Viro10556cb2011-06-20 19:28:19 -04001136 err = fuse_perm_getattr(inode, mask);
Miklos Szeredie8e96152007-10-16 23:31:06 -07001137 if (!err && !(inode->i_mode & S_IXUGO))
1138 return -EACCES;
1139 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001140 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001141 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001142}
1143
1144static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001145 struct dir_context *ctx)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001146{
1147 while (nbytes >= FUSE_NAME_OFFSET) {
1148 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1149 size_t reclen = FUSE_DIRENT_SIZE(dirent);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001150 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1151 return -EIO;
1152 if (reclen > nbytes)
1153 break;
Miklos Szerediefeb9e62013-09-03 14:28:38 +02001154 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1155 return -EIO;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001156
Al Viro8d3af7f2013-05-18 03:03:58 -04001157 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1158 dirent->ino, dirent->type))
Miklos Szeredie5e55582005-09-09 13:10:28 -07001159 break;
1160
1161 buf += reclen;
1162 nbytes -= reclen;
Al Viro8d3af7f2013-05-18 03:03:58 -04001163 ctx->pos = dirent->off;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001164 }
1165
1166 return 0;
1167}
1168
Anand V. Avati0b05b182012-08-19 08:53:23 -04001169static int fuse_direntplus_link(struct file *file,
1170 struct fuse_direntplus *direntplus,
1171 u64 attr_version)
1172{
Anand V. Avati0b05b182012-08-19 08:53:23 -04001173 struct fuse_entry_out *o = &direntplus->entry_out;
1174 struct fuse_dirent *dirent = &direntplus->dirent;
1175 struct dentry *parent = file->f_path.dentry;
1176 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1177 struct dentry *dentry;
1178 struct dentry *alias;
David Howells2b0143b2015-03-17 22:25:59 +00001179 struct inode *dir = d_inode(parent);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001180 struct fuse_conn *fc;
1181 struct inode *inode;
Al Virod9b3dbd2016-04-20 17:30:32 -04001182 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001183
1184 if (!o->nodeid) {
1185 /*
1186 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1187 * ENOENT. Instead, it only means the userspace filesystem did
1188 * not want to return attributes/handle for this entry.
1189 *
1190 * So do nothing.
1191 */
1192 return 0;
1193 }
1194
1195 if (name.name[0] == '.') {
1196 /*
1197 * We could potentially refresh the attributes of the directory
1198 * and its parent?
1199 */
1200 if (name.len == 1)
1201 return 0;
1202 if (name.name[1] == '.' && name.len == 2)
1203 return 0;
1204 }
Miklos Szeredia28ef452013-07-17 14:53:53 +02001205
1206 if (invalid_nodeid(o->nodeid))
1207 return -EIO;
1208 if (!fuse_valid_type(o->attr.mode))
1209 return -EIO;
1210
Anand V. Avati0b05b182012-08-19 08:53:23 -04001211 fc = get_fuse_conn(dir);
1212
Linus Torvalds8387ff22016-06-10 07:51:30 -07001213 name.hash = full_name_hash(parent, name.name, name.len);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001214 dentry = d_lookup(parent, &name);
Al Virod9b3dbd2016-04-20 17:30:32 -04001215 if (!dentry) {
1216retry:
1217 dentry = d_alloc_parallel(parent, &name, &wq);
1218 if (IS_ERR(dentry))
1219 return PTR_ERR(dentry);
1220 }
1221 if (!d_in_lookup(dentry)) {
1222 struct fuse_inode *fi;
David Howells2b0143b2015-03-17 22:25:59 +00001223 inode = d_inode(dentry);
Al Virod9b3dbd2016-04-20 17:30:32 -04001224 if (!inode ||
1225 get_node_id(inode) != o->nodeid ||
1226 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001227 d_invalidate(dentry);
Al Virod9b3dbd2016-04-20 17:30:32 -04001228 dput(dentry);
1229 goto retry;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001230 }
Al Virod9b3dbd2016-04-20 17:30:32 -04001231 if (is_bad_inode(inode)) {
1232 dput(dentry);
1233 return -EIO;
1234 }
1235
1236 fi = get_fuse_inode(inode);
1237 spin_lock(&fc->lock);
1238 fi->nlookup++;
1239 spin_unlock(&fc->lock);
1240
Seth Forshee60bcc882016-08-29 08:46:37 -05001241 forget_all_cached_acls(inode);
Al Virod9b3dbd2016-04-20 17:30:32 -04001242 fuse_change_attributes(inode, &o->attr,
1243 entry_attr_timeout(o),
1244 attr_version);
1245 /*
1246 * The other branch comes via fuse_iget()
1247 * which bumps nlookup inside
1248 */
1249 } else {
1250 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1251 &o->attr, entry_attr_timeout(o),
1252 attr_version);
1253 if (!inode)
1254 inode = ERR_PTR(-ENOMEM);
1255
1256 alias = d_splice_alias(inode, dentry);
1257 d_lookup_done(dentry);
1258 if (alias) {
1259 dput(dentry);
1260 dentry = alias;
1261 }
1262 if (IS_ERR(dentry))
1263 return PTR_ERR(dentry);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001264 }
Miklos Szeredi6314efe2013-10-01 16:41:22 +02001265 if (fc->readdirplus_auto)
1266 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001267 fuse_change_entry_timeout(dentry, o);
1268
Miklos Szeredic7263bc2013-07-17 14:53:54 +02001269 dput(dentry);
Al Virod9b3dbd2016-04-20 17:30:32 -04001270 return 0;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001271}
1272
1273static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001274 struct dir_context *ctx, u64 attr_version)
Anand V. Avati0b05b182012-08-19 08:53:23 -04001275{
1276 struct fuse_direntplus *direntplus;
1277 struct fuse_dirent *dirent;
1278 size_t reclen;
1279 int over = 0;
1280 int ret;
1281
1282 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1283 direntplus = (struct fuse_direntplus *) buf;
1284 dirent = &direntplus->dirent;
1285 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1286
1287 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1288 return -EIO;
1289 if (reclen > nbytes)
1290 break;
Miklos Szerediefeb9e62013-09-03 14:28:38 +02001291 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1292 return -EIO;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001293
1294 if (!over) {
1295 /* We fill entries into dstbuf only as much as
1296 it can hold. But we still continue iterating
1297 over remaining entries to link them. If not,
1298 we need to send a FORGET for each of those
1299 which we did not link.
1300 */
Al Viro8d3af7f2013-05-18 03:03:58 -04001301 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1302 dirent->ino, dirent->type);
1303 ctx->pos = dirent->off;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001304 }
1305
1306 buf += reclen;
1307 nbytes -= reclen;
1308
1309 ret = fuse_direntplus_link(file, direntplus, attr_version);
1310 if (ret)
1311 fuse_force_forget(file, direntplus->entry_out.nodeid);
1312 }
1313
1314 return 0;
1315}
1316
Al Viro8d3af7f2013-05-18 03:03:58 -04001317static int fuse_readdir(struct file *file, struct dir_context *ctx)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001318{
Feng Shuo4582a4a2013-01-15 11:23:28 +08001319 int plus, err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001320 size_t nbytes;
1321 struct page *page;
Al Viro496ad9a2013-01-23 17:07:38 -05001322 struct inode *inode = file_inode(file);
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001323 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001324 struct fuse_req *req;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001325 u64 attr_version = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001326
1327 if (is_bad_inode(inode))
1328 return -EIO;
1329
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001330 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001331 if (IS_ERR(req))
1332 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001333
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001334 page = alloc_page(GFP_KERNEL);
1335 if (!page) {
1336 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001337 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001338 }
Feng Shuo4582a4a2013-01-15 11:23:28 +08001339
Al Viro8d3af7f2013-05-18 03:03:58 -04001340 plus = fuse_use_readdirplus(inode, ctx);
Miklos Szeredif4975c62009-04-02 14:25:34 +02001341 req->out.argpages = 1;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001342 req->num_pages = 1;
1343 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001344 req->page_descs[0].length = PAGE_SIZE;
Feng Shuo4582a4a2013-01-15 11:23:28 +08001345 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001346 attr_version = fuse_get_attr_version(fc);
Al Viro8d3af7f2013-05-18 03:03:58 -04001347 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001348 FUSE_READDIRPLUS);
1349 } else {
Al Viro8d3af7f2013-05-18 03:03:58 -04001350 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001351 FUSE_READDIR);
1352 }
Miklos Szeredi5c672ab2016-06-30 13:10:49 +02001353 fuse_lock_inode(inode);
Tejun Heob93f8582008-11-26 12:03:55 +01001354 fuse_request_send(fc, req);
Miklos Szeredi5c672ab2016-06-30 13:10:49 +02001355 fuse_unlock_inode(inode);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -08001356 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001357 err = req->out.h.error;
1358 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001359 if (!err) {
Feng Shuo4582a4a2013-01-15 11:23:28 +08001360 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001361 err = parse_dirplusfile(page_address(page), nbytes,
Al Viro8d3af7f2013-05-18 03:03:58 -04001362 file, ctx,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001363 attr_version);
1364 } else {
1365 err = parse_dirfile(page_address(page), nbytes, file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001366 ctx);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001367 }
1368 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001369
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001370 __free_page(page);
Andrew Gallagher451418f2013-11-05 03:55:43 -08001371 fuse_invalidate_atime(inode);
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001372 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001373}
1374
Al Viro6b255392015-11-17 10:20:54 -05001375static const char *fuse_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001376 struct inode *inode,
1377 struct delayed_call *done)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001378{
Miklos Szeredie5e55582005-09-09 13:10:28 -07001379 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001380 FUSE_ARGS(args);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001381 char *link;
Miklos Szeredi70781872014-12-12 09:49:05 +01001382 ssize_t ret;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001383
Al Viro6b255392015-11-17 10:20:54 -05001384 if (!dentry)
1385 return ERR_PTR(-ECHILD);
1386
Al Virocd3417c2015-12-29 16:03:53 -05001387 link = kmalloc(PAGE_SIZE, GFP_KERNEL);
Miklos Szeredi70781872014-12-12 09:49:05 +01001388 if (!link)
1389 return ERR_PTR(-ENOMEM);
1390
1391 args.in.h.opcode = FUSE_READLINK;
1392 args.in.h.nodeid = get_node_id(inode);
1393 args.out.argvar = 1;
1394 args.out.numargs = 1;
1395 args.out.args[0].size = PAGE_SIZE - 1;
1396 args.out.args[0].value = link;
1397 ret = fuse_simple_request(fc, &args);
1398 if (ret < 0) {
Al Virocd3417c2015-12-29 16:03:53 -05001399 kfree(link);
Miklos Szeredi70781872014-12-12 09:49:05 +01001400 link = ERR_PTR(ret);
1401 } else {
1402 link[ret] = '\0';
Al Virofceef392015-12-29 15:58:39 -05001403 set_delayed_call(done, kfree_link, link);
Miklos Szeredi70781872014-12-12 09:49:05 +01001404 }
Andrew Gallagher451418f2013-11-05 03:55:43 -08001405 fuse_invalidate_atime(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001406 return link;
1407}
1408
Miklos Szeredie5e55582005-09-09 13:10:28 -07001409static int fuse_dir_open(struct inode *inode, struct file *file)
1410{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +02001411 return fuse_open_common(inode, file, true);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001412}
1413
1414static int fuse_dir_release(struct inode *inode, struct file *file)
1415{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +02001416 fuse_release_common(file, FUSE_RELEASEDIR);
1417
1418 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001419}
1420
Josef Bacik02c24a82011-07-16 20:44:56 -04001421static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1422 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -07001423{
Josef Bacik02c24a82011-07-16 20:44:56 -04001424 return fuse_fsync_common(file, start, end, datasync, 1);
Miklos Szeredi82547982005-09-09 13:10:38 -07001425}
1426
Miklos Szeredib18da0c2011-12-13 11:58:49 +01001427static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1428 unsigned long arg)
1429{
1430 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1431
1432 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1433 if (fc->minor < 18)
1434 return -ENOTTY;
1435
1436 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1437}
1438
1439static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1440 unsigned long arg)
1441{
1442 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1443
1444 if (fc->minor < 18)
1445 return -ENOTTY;
1446
1447 return fuse_ioctl_common(file, cmd, arg,
1448 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1449}
1450
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001451static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001452{
1453 /* Always update if mtime is explicitly set */
1454 if (ivalid & ATTR_MTIME_SET)
1455 return true;
1456
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001457 /* Or if kernel i_mtime is the official one */
1458 if (trust_local_mtime)
1459 return true;
1460
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001461 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1462 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1463 return false;
1464
1465 /* In all other cases update */
1466 return true;
1467}
1468
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001469static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001470 bool trust_local_cmtime)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001471{
1472 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001473
1474 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001475 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001476 if (ivalid & ATTR_UID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001477 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001478 if (ivalid & ATTR_GID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001479 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001480 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001481 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001482 if (ivalid & ATTR_ATIME) {
1483 arg->valid |= FATTR_ATIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001484 arg->atime = iattr->ia_atime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001485 arg->atimensec = iattr->ia_atime.tv_nsec;
1486 if (!(ivalid & ATTR_ATIME_SET))
1487 arg->valid |= FATTR_ATIME_NOW;
1488 }
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001489 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001490 arg->valid |= FATTR_MTIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001491 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001492 arg->mtimensec = iattr->ia_mtime.tv_nsec;
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001493 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001494 arg->valid |= FATTR_MTIME_NOW;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001495 }
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001496 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1497 arg->valid |= FATTR_CTIME;
1498 arg->ctime = iattr->ia_ctime.tv_sec;
1499 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1500 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001501}
1502
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001503/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001504 * Prevent concurrent writepages on inode
1505 *
1506 * This is done by adding a negative bias to the inode write counter
1507 * and waiting for all pending writes to finish.
1508 */
1509void fuse_set_nowrite(struct inode *inode)
1510{
1511 struct fuse_conn *fc = get_fuse_conn(inode);
1512 struct fuse_inode *fi = get_fuse_inode(inode);
1513
Al Viro59551022016-01-22 15:40:57 -05001514 BUG_ON(!inode_is_locked(inode));
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001515
1516 spin_lock(&fc->lock);
1517 BUG_ON(fi->writectr < 0);
1518 fi->writectr += FUSE_NOWRITE;
1519 spin_unlock(&fc->lock);
1520 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1521}
1522
1523/*
1524 * Allow writepages on inode
1525 *
1526 * Remove the bias from the writecounter and send any queued
1527 * writepages.
1528 */
1529static void __fuse_release_nowrite(struct inode *inode)
1530{
1531 struct fuse_inode *fi = get_fuse_inode(inode);
1532
1533 BUG_ON(fi->writectr != FUSE_NOWRITE);
1534 fi->writectr = 0;
1535 fuse_flush_writepages(inode);
1536}
1537
1538void fuse_release_nowrite(struct inode *inode)
1539{
1540 struct fuse_conn *fc = get_fuse_conn(inode);
1541
1542 spin_lock(&fc->lock);
1543 __fuse_release_nowrite(inode);
1544 spin_unlock(&fc->lock);
1545}
1546
Miklos Szeredi70781872014-12-12 09:49:05 +01001547static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001548 struct inode *inode,
1549 struct fuse_setattr_in *inarg_p,
1550 struct fuse_attr_out *outarg_p)
1551{
Miklos Szeredi70781872014-12-12 09:49:05 +01001552 args->in.h.opcode = FUSE_SETATTR;
1553 args->in.h.nodeid = get_node_id(inode);
1554 args->in.numargs = 1;
1555 args->in.args[0].size = sizeof(*inarg_p);
1556 args->in.args[0].value = inarg_p;
1557 args->out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +01001558 args->out.args[0].size = sizeof(*outarg_p);
Miklos Szeredi70781872014-12-12 09:49:05 +01001559 args->out.args[0].value = outarg_p;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001560}
1561
1562/*
1563 * Flush inode->i_mtime to the server
1564 */
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001565int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001566{
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001567 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001568 FUSE_ARGS(args);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001569 struct fuse_setattr_in inarg;
1570 struct fuse_attr_out outarg;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001571
1572 memset(&inarg, 0, sizeof(inarg));
1573 memset(&outarg, 0, sizeof(outarg));
1574
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001575 inarg.valid = FATTR_MTIME;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001576 inarg.mtime = inode->i_mtime.tv_sec;
1577 inarg.mtimensec = inode->i_mtime.tv_nsec;
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001578 if (fc->minor >= 23) {
1579 inarg.valid |= FATTR_CTIME;
1580 inarg.ctime = inode->i_ctime.tv_sec;
1581 inarg.ctimensec = inode->i_ctime.tv_nsec;
1582 }
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001583 if (ff) {
1584 inarg.valid |= FATTR_FH;
1585 inarg.fh = ff->fh;
1586 }
Miklos Szeredi70781872014-12-12 09:49:05 +01001587 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001588
Miklos Szeredi70781872014-12-12 09:49:05 +01001589 return fuse_simple_request(fc, &args);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001590}
1591
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001592/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001593 * Set attributes, and at the same time refresh them.
1594 *
1595 * Truncation is slightly complicated, because the 'truncate' request
1596 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -07001597 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1598 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001599 */
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001600int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1601 struct file *file)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001602{
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001603 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001604 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001605 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001606 struct fuse_setattr_in inarg;
1607 struct fuse_attr_out outarg;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001608 bool is_truncate = false;
Pavel Emelyanov83732002013-10-10 17:10:46 +04001609 bool is_wb = fc->writeback_cache;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001610 loff_t oldsize;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001611 int err;
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001612 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001613
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001614 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1615 attr->ia_valid |= ATTR_FORCE;
1616
1617 err = inode_change_ok(inode, attr);
1618 if (err)
1619 return err;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001620
Miklos Szeredi8d56add2011-02-25 14:44:58 +01001621 if (attr->ia_valid & ATTR_OPEN) {
1622 if (fc->atomic_o_trunc)
1623 return 0;
1624 file = NULL;
1625 }
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001626
Christoph Hellwig2c27c652010-06-04 11:30:04 +02001627 if (attr->ia_valid & ATTR_SIZE)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001628 is_truncate = true;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001629
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001630 if (is_truncate) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001631 fuse_set_nowrite(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001632 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001633 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1634 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001635 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001637 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001638 memset(&outarg, 0, sizeof(outarg));
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001639 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001640 if (file) {
1641 struct fuse_file *ff = file->private_data;
1642 inarg.valid |= FATTR_FH;
1643 inarg.fh = ff->fh;
1644 }
Miklos Szeredif3332112007-10-18 03:07:04 -07001645 if (attr->ia_valid & ATTR_SIZE) {
1646 /* For mandatory locking in truncate */
1647 inarg.valid |= FATTR_LOCKOWNER;
1648 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1649 }
Miklos Szeredi70781872014-12-12 09:49:05 +01001650 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1651 err = fuse_simple_request(fc, &args);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001652 if (err) {
1653 if (err == -EINTR)
1654 fuse_invalidate_attr(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001655 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001656 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001657
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001658 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1659 make_bad_inode(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660 err = -EIO;
1661 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001662 }
1663
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001664 spin_lock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001665 /* the kernel maintains i_mtime locally */
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001666 if (trust_local_cmtime) {
1667 if (attr->ia_valid & ATTR_MTIME)
1668 inode->i_mtime = attr->ia_mtime;
1669 if (attr->ia_valid & ATTR_CTIME)
1670 inode->i_ctime = attr->ia_ctime;
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001671 /* FIXME: clear I_DIRTY_SYNC? */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001672 }
1673
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001674 fuse_change_attributes_common(inode, &outarg.attr,
1675 attr_timeout(&outarg));
1676 oldsize = inode->i_size;
Pavel Emelyanov83732002013-10-10 17:10:46 +04001677 /* see the comment in fuse_change_attributes() */
1678 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1679 i_size_write(inode, outarg.attr.size);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001680
1681 if (is_truncate) {
1682 /* NOTE: this may release/reacquire fc->lock */
1683 __fuse_release_nowrite(inode);
1684 }
1685 spin_unlock(&fc->lock);
1686
1687 /*
1688 * Only call invalidate_inode_pages2() after removing
1689 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1690 */
Pavel Emelyanov83732002013-10-10 17:10:46 +04001691 if ((is_truncate || !is_wb) &&
1692 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001693 truncate_pagecache(inode, outarg.attr.size);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001694 invalidate_inode_pages2(inode->i_mapping);
1695 }
1696
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001697 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001698 return 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001699
1700error:
1701 if (is_truncate)
1702 fuse_release_nowrite(inode);
1703
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001704 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001705 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001706}
1707
Miklos Szeredi49d49142007-10-18 03:07:00 -07001708static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1709{
David Howells2b0143b2015-03-17 22:25:59 +00001710 struct inode *inode = d_inode(entry);
Miklos Szeredi5e940c12016-10-01 07:32:32 +02001711 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredia09f99e2016-10-01 07:32:32 +02001712 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
Miklos Szeredi5e2b8822016-10-01 07:32:32 +02001713 int ret;
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001714
1715 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1716 return -EACCES;
1717
Miklos Szeredia09f99e2016-10-01 07:32:32 +02001718 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
Miklos Szeredia09f99e2016-10-01 07:32:32 +02001719 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1720 ATTR_MODE);
Miklos Szeredia09f99e2016-10-01 07:32:32 +02001721
Miklos Szeredi5e940c12016-10-01 07:32:32 +02001722 /*
1723 * The only sane way to reliably kill suid/sgid is to do it in
1724 * the userspace filesystem
1725 *
1726 * This should be done on write(), truncate() and chown().
1727 */
1728 if (!fc->handle_killpriv) {
1729 int kill;
1730
1731 /*
1732 * ia_mode calculation may have used stale i_mode.
1733 * Refresh and recalculate.
1734 */
1735 ret = fuse_do_getattr(inode, NULL, file);
1736 if (ret)
1737 return ret;
1738
1739 attr->ia_mode = inode->i_mode;
1740 kill = should_remove_suid(entry);
1741 if (kill & ATTR_KILL_SUID) {
1742 attr->ia_valid |= ATTR_MODE;
1743 attr->ia_mode &= ~S_ISUID;
1744 }
1745 if (kill & ATTR_KILL_SGID) {
1746 attr->ia_valid |= ATTR_MODE;
1747 attr->ia_mode &= ~S_ISGID;
1748 }
Miklos Szeredia09f99e2016-10-01 07:32:32 +02001749 }
1750 }
1751 if (!attr->ia_valid)
1752 return 0;
1753
1754 ret = fuse_do_setattr(inode, attr, file);
Miklos Szeredi5e2b8822016-10-01 07:32:32 +02001755 if (!ret) {
Seth Forshee60bcc882016-08-29 08:46:37 -05001756 /*
1757 * If filesystem supports acls it may have updated acl xattrs in
1758 * the filesystem, so forget cached acls for the inode.
1759 */
1760 if (fc->posix_acl)
1761 forget_all_cached_acls(inode);
1762
Miklos Szeredi5e2b8822016-10-01 07:32:32 +02001763 /* Directory mode changed, may need to revalidate access */
1764 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1765 fuse_invalidate_entry_cache(entry);
1766 }
1767 return ret;
Miklos Szeredi49d49142007-10-18 03:07:00 -07001768}
1769
Miklos Szeredie5e55582005-09-09 13:10:28 -07001770static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1771 struct kstat *stat)
1772{
David Howells2b0143b2015-03-17 22:25:59 +00001773 struct inode *inode = d_inode(entry);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001774 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001775
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001776 if (!fuse_allow_current_process(fc))
Miklos Szeredi244f6382007-10-16 23:31:02 -07001777 return -EACCES;
1778
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001779 return fuse_update_attributes(inode, stat, NULL, NULL);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001780}
1781
Arjan van de Ven754661f2007-02-12 00:55:38 -08001782static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07001783 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001784 .mkdir = fuse_mkdir,
1785 .symlink = fuse_symlink,
1786 .unlink = fuse_unlink,
1787 .rmdir = fuse_rmdir,
Miklos Szeredi1560c972014-04-28 16:43:44 +02001788 .rename2 = fuse_rename2,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001789 .link = fuse_link,
1790 .setattr = fuse_setattr,
1791 .create = fuse_create,
Miklos Szeredic8ccbe02012-06-05 15:10:22 +02001792 .atomic_open = fuse_atomic_open,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001793 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001794 .permission = fuse_permission,
1795 .getattr = fuse_getattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001796 .setxattr = generic_setxattr,
1797 .getxattr = generic_getxattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001798 .listxattr = fuse_listxattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001799 .removexattr = generic_removexattr,
Seth Forshee60bcc882016-08-29 08:46:37 -05001800 .get_acl = fuse_get_acl,
1801 .set_acl = fuse_set_acl,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001802};
1803
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001804static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001805 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001806 .read = generic_read_dir,
Al Virod9b3dbd2016-04-20 17:30:32 -04001807 .iterate_shared = fuse_readdir,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001808 .open = fuse_dir_open,
1809 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001810 .fsync = fuse_dir_fsync,
Miklos Szeredib18da0c2011-12-13 11:58:49 +01001811 .unlocked_ioctl = fuse_dir_ioctl,
1812 .compat_ioctl = fuse_dir_compat_ioctl,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001813};
1814
Arjan van de Ven754661f2007-02-12 00:55:38 -08001815static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001816 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001817 .permission = fuse_permission,
1818 .getattr = fuse_getattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001819 .setxattr = generic_setxattr,
1820 .getxattr = generic_getxattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001821 .listxattr = fuse_listxattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001822 .removexattr = generic_removexattr,
Seth Forshee60bcc882016-08-29 08:46:37 -05001823 .get_acl = fuse_get_acl,
1824 .set_acl = fuse_set_acl,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001825};
1826
Arjan van de Ven754661f2007-02-12 00:55:38 -08001827static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001828 .setattr = fuse_setattr,
Al Viro6b255392015-11-17 10:20:54 -05001829 .get_link = fuse_get_link,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001830 .readlink = generic_readlink,
1831 .getattr = fuse_getattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001832 .setxattr = generic_setxattr,
1833 .getxattr = generic_getxattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001834 .listxattr = fuse_listxattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001835 .removexattr = generic_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001836};
1837
1838void fuse_init_common(struct inode *inode)
1839{
1840 inode->i_op = &fuse_common_inode_operations;
1841}
1842
1843void fuse_init_dir(struct inode *inode)
1844{
1845 inode->i_op = &fuse_dir_inode_operations;
1846 inode->i_fop = &fuse_dir_operations;
1847}
1848
1849void fuse_init_symlink(struct inode *inode)
1850{
1851 inode->i_op = &fuse_symlink_inode_operations;
1852}