blob: 3d3afa7ea57c9b55ececf52f1fbadbac9c65a99a [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>
Miklos Szeredie5e55582005-09-09 13:10:28 -070017
Al Viro8d3af7f2013-05-18 03:03:58 -040018static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
Feng Shuo4582a4a2013-01-15 11:23:28 +080019{
20 struct fuse_conn *fc = get_fuse_conn(dir);
21 struct fuse_inode *fi = get_fuse_inode(dir);
22
23 if (!fc->do_readdirplus)
24 return false;
Eric Wong634734b2013-02-06 22:29:01 +000025 if (!fc->readdirplus_auto)
26 return true;
Feng Shuo4582a4a2013-01-15 11:23:28 +080027 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
28 return true;
Al Viro8d3af7f2013-05-18 03:03:58 -040029 if (ctx->pos == 0)
Feng Shuo4582a4a2013-01-15 11:23:28 +080030 return true;
31 return false;
32}
33
34static void fuse_advise_use_readdirplus(struct inode *dir)
35{
36 struct fuse_inode *fi = get_fuse_inode(dir);
37
38 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
39}
40
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070041#if BITS_PER_LONG >= 64
42static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
43{
44 entry->d_time = time;
45}
46
47static inline u64 fuse_dentry_time(struct dentry *entry)
48{
49 return entry->d_time;
50}
51#else
52/*
53 * On 32 bit archs store the high 32 bits of time in d_fsdata
54 */
55static void fuse_dentry_settime(struct dentry *entry, u64 time)
56{
57 entry->d_time = time;
58 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
59}
60
61static u64 fuse_dentry_time(struct dentry *entry)
62{
63 return (u64) entry->d_time +
64 ((u64) (unsigned long) entry->d_fsdata << 32);
65}
66#endif
67
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080068/*
69 * FUSE caches dentries and attributes with separate timeout. The
70 * time in jiffies until the dentry/attributes are valid is stored in
71 * dentry->d_time and fuse_inode->i_time respectively.
72 */
73
74/*
75 * Calculate the time in jiffies until a dentry/attributes are valid
76 */
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070077static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
Miklos Szeredie5e55582005-09-09 13:10:28 -070078{
Miklos Szeredi685d16d2006-07-30 03:04:08 -070079 if (sec || nsec) {
80 struct timespec ts = {sec, nsec};
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070081 return get_jiffies_64() + timespec_to_jiffies(&ts);
Miklos Szeredi685d16d2006-07-30 03:04:08 -070082 } else
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070083 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070084}
85
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080086/*
87 * Set dentry and possibly attribute timeouts from the lookup/mk*
88 * replies
89 */
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070090static void fuse_change_entry_timeout(struct dentry *entry,
91 struct fuse_entry_out *o)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080092{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070093 fuse_dentry_settime(entry,
94 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070095}
96
97static u64 attr_timeout(struct fuse_attr_out *o)
98{
99 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
100}
101
102static u64 entry_attr_timeout(struct fuse_entry_out *o)
103{
104 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800105}
106
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800107/*
108 * Mark the attributes as stale, so that at the next call to
109 * ->getattr() they will be fetched from userspace
110 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800111void fuse_invalidate_attr(struct inode *inode)
112{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700113 get_fuse_inode(inode)->i_time = 0;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800114}
115
Andrew Gallagher451418f2013-11-05 03:55:43 -0800116/**
117 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
118 * atime is not used.
119 */
120void fuse_invalidate_atime(struct inode *inode)
121{
122 if (!IS_RDONLY(inode))
123 fuse_invalidate_attr(inode);
124}
125
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800126/*
127 * Just mark the entry as stale, so that a next attempt to look it up
128 * will result in a new lookup call to userspace
129 *
130 * This is called when a dentry is about to become negative and the
131 * timeout is unknown (unlink, rmdir, rename and in some cases
132 * lookup)
133 */
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700134void fuse_invalidate_entry_cache(struct dentry *entry)
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800135{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700136 fuse_dentry_settime(entry, 0);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800137}
138
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800139/*
140 * Same as fuse_invalidate_entry_cache(), but also try to remove the
141 * dentry from the hash
142 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800143static void fuse_invalidate_entry(struct dentry *entry)
144{
145 d_invalidate(entry);
146 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800147}
148
Miklos Szeredi70781872014-12-12 09:49:05 +0100149static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
Al Viro13983d02016-07-20 22:34:44 -0400150 u64 nodeid, const struct qstr *name,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700151 struct fuse_entry_out *outarg)
152{
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700153 memset(outarg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredi70781872014-12-12 09:49:05 +0100154 args->in.h.opcode = FUSE_LOOKUP;
155 args->in.h.nodeid = nodeid;
156 args->in.numargs = 1;
157 args->in.args[0].size = name->len + 1;
158 args->in.args[0].value = name->name;
159 args->out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100160 args->out.args[0].size = sizeof(struct fuse_entry_out);
Miklos Szeredi70781872014-12-12 09:49:05 +0100161 args->out.args[0].value = outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700162}
163
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700164u64 fuse_get_attr_version(struct fuse_conn *fc)
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800165{
166 u64 curr_version;
167
168 /*
169 * The spin lock isn't actually needed on 64bit archs, but we
170 * don't yet care too much about such optimizations.
171 */
172 spin_lock(&fc->lock);
173 curr_version = fc->attr_version;
174 spin_unlock(&fc->lock);
175
176 return curr_version;
177}
178
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800179/*
180 * Check whether the dentry is still valid
181 *
182 * If the entry validity timeout has expired and the dentry is
183 * positive, try to redo the lookup. If the lookup results in a
184 * different inode, then let the VFS invalidate the dentry and redo
185 * the lookup once more. If the lookup results in the same inode,
186 * then refresh the attributes, timeouts and mark the dentry valid.
187 */
Al Viro0b728e12012-06-10 16:03:43 -0400188static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700189{
Nick Piggin34286d62011-01-07 17:49:57 +1100190 struct inode *inode;
Miklos Szeredi28420da2013-06-03 14:40:22 +0200191 struct dentry *parent;
192 struct fuse_conn *fc;
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200193 struct fuse_inode *fi;
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200194 int ret;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800195
David Howells2b0143b2015-03-17 22:25:59 +0000196 inode = d_inode_rcu(entry);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800197 if (inode && is_bad_inode(inode))
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200198 goto invalid;
Anand Avati154210c2014-06-26 20:21:57 -0400199 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
200 (flags & LOOKUP_REVAL)) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700201 struct fuse_entry_out outarg;
Miklos Szeredi70781872014-12-12 09:49:05 +0100202 FUSE_ARGS(args);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100203 struct fuse_forget_link *forget;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700204 u64 attr_version;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800205
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800206 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800207 if (!inode)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200208 goto invalid;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800209
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200210 ret = -ECHILD;
Al Viro0b728e12012-06-10 16:03:43 -0400211 if (flags & LOOKUP_RCU)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200212 goto out;
Miklos Szeredie7c0a162011-03-21 13:58:06 +0100213
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800214 fc = get_fuse_conn(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700215
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100216 forget = fuse_alloc_forget();
Miklos Szeredi70781872014-12-12 09:49:05 +0100217 ret = -ENOMEM;
218 if (!forget)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200219 goto out;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800220
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800221 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700222
Miklos Szeredie956edd2006-10-17 00:10:12 -0700223 parent = dget_parent(entry);
David Howells2b0143b2015-03-17 22:25:59 +0000224 fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700225 &entry->d_name, &outarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100226 ret = fuse_simple_request(fc, &args);
Miklos Szeredie956edd2006-10-17 00:10:12 -0700227 dput(parent);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800228 /* Zero nodeid is same as -ENOENT */
Miklos Szeredi70781872014-12-12 09:49:05 +0100229 if (!ret && !outarg.nodeid)
230 ret = -ENOENT;
231 if (!ret) {
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200232 fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700233 if (outarg.nodeid != get_node_id(inode)) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100234 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200235 goto invalid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700236 }
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700237 spin_lock(&fc->lock);
Miklos Szeredi1729a162008-11-26 12:03:54 +0100238 fi->nlookup++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700239 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700240 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100241 kfree(forget);
Miklos Szeredi70781872014-12-12 09:49:05 +0100242 if (ret == -ENOMEM)
243 goto out;
244 if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200245 goto invalid;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700246
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700247 fuse_change_attributes(inode, &outarg.attr,
248 entry_attr_timeout(&outarg),
249 attr_version);
250 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi28420da2013-06-03 14:40:22 +0200251 } else if (inode) {
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200252 fi = get_fuse_inode(inode);
253 if (flags & LOOKUP_RCU) {
254 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
255 return -ECHILD;
256 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
Miklos Szeredi28420da2013-06-03 14:40:22 +0200257 parent = dget_parent(entry);
David Howells2b0143b2015-03-17 22:25:59 +0000258 fuse_advise_use_readdirplus(d_inode(parent));
Miklos Szeredi28420da2013-06-03 14:40:22 +0200259 dput(parent);
260 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700261 }
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200262 ret = 1;
263out:
264 return ret;
265
266invalid:
267 ret = 0;
268 goto out;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700269}
270
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800271static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800272{
273 return !nodeid || nodeid == FUSE_ROOT_ID;
274}
275
Al Viro42695902009-02-20 05:59:13 +0000276const struct dentry_operations fuse_dentry_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700277 .d_revalidate = fuse_dentry_revalidate,
278};
279
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700280int fuse_valid_type(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800281{
282 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
283 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
284}
285
Al Viro13983d02016-07-20 22:34:44 -0400286int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700287 struct fuse_entry_out *outarg, struct inode **inode)
288{
289 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredi70781872014-12-12 09:49:05 +0100290 FUSE_ARGS(args);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100291 struct fuse_forget_link *forget;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700292 u64 attr_version;
293 int err;
294
295 *inode = NULL;
296 err = -ENAMETOOLONG;
297 if (name->len > FUSE_NAME_MAX)
298 goto out;
299
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700300
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100301 forget = fuse_alloc_forget();
302 err = -ENOMEM;
Miklos Szeredi70781872014-12-12 09:49:05 +0100303 if (!forget)
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700304 goto out;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700305
306 attr_version = fuse_get_attr_version(fc);
307
Miklos Szeredi70781872014-12-12 09:49:05 +0100308 fuse_lookup_init(fc, &args, nodeid, name, outarg);
309 err = fuse_simple_request(fc, &args);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700310 /* Zero nodeid is same as -ENOENT, but with valid timeout */
311 if (err || !outarg->nodeid)
312 goto out_put_forget;
313
314 err = -EIO;
315 if (!outarg->nodeid)
316 goto out_put_forget;
317 if (!fuse_valid_type(outarg->attr.mode))
318 goto out_put_forget;
319
320 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
321 &outarg->attr, entry_attr_timeout(outarg),
322 attr_version);
323 err = -ENOMEM;
324 if (!*inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100325 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700326 goto out;
327 }
328 err = 0;
329
330 out_put_forget:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100331 kfree(forget);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700332 out:
333 return err;
334}
335
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800336static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400337 unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700338{
339 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700340 struct fuse_entry_out outarg;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700341 struct inode *inode;
Miklos Szeredi0de62562008-07-25 01:48:59 -0700342 struct dentry *newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700343 bool outarg_valid = true;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700344
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200345 fuse_lock_inode(dir);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700346 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
347 &outarg, &inode);
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200348 fuse_unlock_inode(dir);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700349 if (err == -ENOENT) {
350 outarg_valid = false;
351 err = 0;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800352 }
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700353 if (err)
354 goto out_err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800355
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700356 err = -EIO;
357 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
358 goto out_iput;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700359
Al Viro41d28bc2014-10-12 22:24:21 -0400360 newent = d_splice_alias(inode, entry);
Miklos Szeredi5835f332013-09-05 11:44:42 +0200361 err = PTR_ERR(newent);
362 if (IS_ERR(newent))
363 goto out_err;
Miklos Szeredid2a85162006-10-17 00:10:11 -0700364
Miklos Szeredi0de62562008-07-25 01:48:59 -0700365 entry = newent ? newent : entry;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700366 if (outarg_valid)
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700367 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800368 else
369 fuse_invalidate_entry_cache(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700370
Feng Shuo4582a4a2013-01-15 11:23:28 +0800371 fuse_advise_use_readdirplus(dir);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700372 return newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700373
374 out_iput:
375 iput(inode);
376 out_err:
377 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700378}
379
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800380/*
381 * Atomic create+open operation
382 *
383 * If the filesystem doesn't support this, then fall back to separate
384 * 'mknod' + 'open' requests.
385 */
Al Virod9585272012-06-22 12:39:14 +0400386static int fuse_create_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400387 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400388 umode_t mode, int *opened)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800389{
390 int err;
391 struct inode *inode;
392 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100393 FUSE_ARGS(args);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100394 struct fuse_forget_link *forget;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200395 struct fuse_create_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800396 struct fuse_open_out outopen;
397 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800398 struct fuse_file *ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800399
Miklos Szerediaf109bc2012-08-15 13:01:24 +0200400 /* Userspace expects S_IFREG in create mode */
401 BUG_ON((mode & S_IFMT) != S_IFREG);
402
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100403 forget = fuse_alloc_forget();
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200404 err = -ENOMEM;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100405 if (!forget)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200406 goto out_err;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700407
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700408 err = -ENOMEM;
Tejun Heoacf99432008-11-26 12:03:55 +0100409 ff = fuse_file_alloc(fc);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800410 if (!ff)
Miklos Szeredi70781872014-12-12 09:49:05 +0100411 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800412
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200413 if (!fc->dont_mask)
414 mode &= ~current_umask();
415
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800416 flags &= ~O_NOCTTY;
417 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700418 memset(&outentry, 0, sizeof(outentry));
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800419 inarg.flags = flags;
420 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200421 inarg.umask = current_umask();
Miklos Szeredi70781872014-12-12 09:49:05 +0100422 args.in.h.opcode = FUSE_CREATE;
423 args.in.h.nodeid = get_node_id(dir);
424 args.in.numargs = 2;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100425 args.in.args[0].size = sizeof(inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100426 args.in.args[0].value = &inarg;
427 args.in.args[1].size = entry->d_name.len + 1;
428 args.in.args[1].value = entry->d_name.name;
429 args.out.numargs = 2;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100430 args.out.args[0].size = sizeof(outentry);
Miklos Szeredi70781872014-12-12 09:49:05 +0100431 args.out.args[0].value = &outentry;
432 args.out.args[1].size = sizeof(outopen);
433 args.out.args[1].value = &outopen;
434 err = fuse_simple_request(fc, &args);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200435 if (err)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800436 goto out_free_ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800437
438 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800439 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800440 goto out_free_ff;
441
Miklos Szeredic7b71432009-04-28 16:56:37 +0200442 ff->fh = outopen.fh;
443 ff->nodeid = outentry.nodeid;
444 ff->open_flags = outopen.open_flags;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800445 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700446 &outentry.attr, entry_attr_timeout(&outentry), 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800447 if (!inode) {
448 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200449 fuse_sync_release(ff, flags);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100450 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200451 err = -ENOMEM;
452 goto out_err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800453 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100454 kfree(forget);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800455 d_instantiate(entry, inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700456 fuse_change_entry_timeout(entry, &outentry);
Miklos Szeredi0952b2a2008-02-06 01:38:38 -0800457 fuse_invalidate_attr(dir);
Al Viro30d90492012-06-22 12:40:19 +0400458 err = finish_open(file, entry, generic_file_open, opened);
459 if (err) {
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200460 fuse_sync_release(ff, flags);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200461 } else {
462 file->private_data = fuse_file_get(ff);
463 fuse_finish_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800464 }
Al Virod9585272012-06-22 12:39:14 +0400465 return err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800466
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200467out_free_ff:
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800468 fuse_file_free(ff);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200469out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100470 kfree(forget);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200471out_err:
Al Virod9585272012-06-22 12:39:14 +0400472 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200473}
474
475static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
Al Virod9585272012-06-22 12:39:14 +0400476static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400477 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400478 umode_t mode, int *opened)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200479{
480 int err;
481 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200482 struct dentry *res = NULL;
483
Al Viro00699ad2016-07-05 09:44:53 -0400484 if (d_in_lookup(entry)) {
Al Viro00cd8dd2012-06-10 17:13:09 -0400485 res = fuse_lookup(dir, entry, 0);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200486 if (IS_ERR(res))
Al Virod9585272012-06-22 12:39:14 +0400487 return PTR_ERR(res);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200488
489 if (res)
490 entry = res;
491 }
492
David Howells2b0143b2015-03-17 22:25:59 +0000493 if (!(flags & O_CREAT) || d_really_is_positive(entry))
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200494 goto no_open;
495
496 /* Only creates */
Al Viro47237682012-06-10 05:01:45 -0400497 *opened |= FILE_CREATED;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200498
499 if (fc->no_create)
500 goto mknod;
501
Al Viro30d90492012-06-22 12:40:19 +0400502 err = fuse_create_open(dir, entry, file, flags, mode, opened);
Al Virod9585272012-06-22 12:39:14 +0400503 if (err == -ENOSYS) {
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200504 fc->no_create = 1;
505 goto mknod;
506 }
507out_dput:
508 dput(res);
Al Virod9585272012-06-22 12:39:14 +0400509 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200510
511mknod:
512 err = fuse_mknod(dir, entry, mode, 0);
Al Virod9585272012-06-22 12:39:14 +0400513 if (err)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200514 goto out_dput;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200515no_open:
Al Viroe45198a2012-06-10 06:48:09 -0400516 return finish_no_open(file, res);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800517}
518
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800519/*
520 * Code shared between mknod, mkdir, symlink and link
521 */
Miklos Szeredi70781872014-12-12 09:49:05 +0100522static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700523 struct inode *dir, struct dentry *entry,
Al Viro541af6a2011-07-26 03:17:33 -0400524 umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700525{
526 struct fuse_entry_out outarg;
527 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700528 int err;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100529 struct fuse_forget_link *forget;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800530
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100531 forget = fuse_alloc_forget();
Miklos Szeredi70781872014-12-12 09:49:05 +0100532 if (!forget)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100533 return -ENOMEM;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700534
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700535 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredi70781872014-12-12 09:49:05 +0100536 args->in.h.nodeid = get_node_id(dir);
537 args->out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100538 args->out.args[0].size = sizeof(outarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100539 args->out.args[0].value = &outarg;
540 err = fuse_simple_request(fc, args);
Miklos Szeredi2d510132006-11-25 11:09:20 -0800541 if (err)
542 goto out_put_forget_req;
543
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800544 err = -EIO;
545 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800546 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800547
548 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800549 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800550
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700551 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700552 &outarg.attr, entry_attr_timeout(&outarg), 0);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700553 if (!inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100554 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700555 return -ENOMEM;
556 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100557 kfree(forget);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700558
Miklos Szeredib70a80e2013-10-01 16:44:54 +0200559 err = d_instantiate_no_diralias(entry, inode);
560 if (err)
561 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700562
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700563 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700564 fuse_invalidate_attr(dir);
565 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800566
Miklos Szeredi2d510132006-11-25 11:09:20 -0800567 out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100568 kfree(forget);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800569 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700570}
571
Al Viro1a67aaf2011-07-26 01:52:52 -0400572static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700573 dev_t rdev)
574{
575 struct fuse_mknod_in inarg;
576 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100577 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700578
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200579 if (!fc->dont_mask)
580 mode &= ~current_umask();
581
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700582 memset(&inarg, 0, sizeof(inarg));
583 inarg.mode = mode;
584 inarg.rdev = new_encode_dev(rdev);
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200585 inarg.umask = current_umask();
Miklos Szeredi70781872014-12-12 09:49:05 +0100586 args.in.h.opcode = FUSE_MKNOD;
587 args.in.numargs = 2;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100588 args.in.args[0].size = sizeof(inarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100589 args.in.args[0].value = &inarg;
590 args.in.args[1].size = entry->d_name.len + 1;
591 args.in.args[1].value = entry->d_name.name;
592 return create_new_entry(fc, &args, dir, entry, mode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700593}
594
Al Viro4acdaf22011-07-26 01:42:34 -0400595static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400596 bool excl)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700597{
598 return fuse_mknod(dir, entry, mode, 0);
599}
600
Al Viro18bb1db2011-07-26 01:41:39 -0400601static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700602{
603 struct fuse_mkdir_in inarg;
604 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100605 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700606
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200607 if (!fc->dont_mask)
608 mode &= ~current_umask();
609
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700610 memset(&inarg, 0, sizeof(inarg));
611 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200612 inarg.umask = current_umask();
Miklos Szeredi70781872014-12-12 09:49:05 +0100613 args.in.h.opcode = FUSE_MKDIR;
614 args.in.numargs = 2;
615 args.in.args[0].size = sizeof(inarg);
616 args.in.args[0].value = &inarg;
617 args.in.args[1].size = entry->d_name.len + 1;
618 args.in.args[1].value = entry->d_name.name;
619 return create_new_entry(fc, &args, dir, entry, S_IFDIR);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700620}
621
622static int fuse_symlink(struct inode *dir, struct dentry *entry,
623 const char *link)
624{
625 struct fuse_conn *fc = get_fuse_conn(dir);
626 unsigned len = strlen(link) + 1;
Miklos Szeredi70781872014-12-12 09:49:05 +0100627 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700628
Miklos Szeredi70781872014-12-12 09:49:05 +0100629 args.in.h.opcode = FUSE_SYMLINK;
630 args.in.numargs = 2;
631 args.in.args[0].size = entry->d_name.len + 1;
632 args.in.args[0].value = entry->d_name.name;
633 args.in.args[1].size = len;
634 args.in.args[1].value = link;
635 return create_new_entry(fc, &args, dir, entry, S_IFLNK);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700636}
637
Seth Forshee703c7362016-08-29 08:46:36 -0500638void fuse_update_ctime(struct inode *inode)
Maxim Patlasov31f32672014-04-28 14:19:24 +0200639{
640 if (!IS_NOCMTIME(inode)) {
641 inode->i_ctime = current_fs_time(inode->i_sb);
642 mark_inode_dirty_sync(inode);
643 }
644}
645
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700646static int fuse_unlink(struct inode *dir, struct dentry *entry)
647{
648 int err;
649 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100650 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700651
Miklos Szeredi70781872014-12-12 09:49:05 +0100652 args.in.h.opcode = FUSE_UNLINK;
653 args.in.h.nodeid = get_node_id(dir);
654 args.in.numargs = 1;
655 args.in.args[0].size = entry->d_name.len + 1;
656 args.in.args[0].value = entry->d_name.name;
657 err = fuse_simple_request(fc, &args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700658 if (!err) {
David Howells2b0143b2015-03-17 22:25:59 +0000659 struct inode *inode = d_inode(entry);
Miklos Szerediac45d612012-03-05 15:48:11 +0100660 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700661
Miklos Szerediac45d612012-03-05 15:48:11 +0100662 spin_lock(&fc->lock);
663 fi->attr_version = ++fc->attr_version;
Miklos Szeredidfca7ce2013-02-04 15:57:42 +0100664 /*
665 * If i_nlink == 0 then unlink doesn't make sense, yet this can
666 * happen if userspace filesystem is careless. It would be
667 * difficult to enforce correct nlink usage so just ignore this
668 * condition here
669 */
670 if (inode->i_nlink > 0)
671 drop_nlink(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100672 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700673 fuse_invalidate_attr(inode);
674 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800675 fuse_invalidate_entry_cache(entry);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200676 fuse_update_ctime(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700677 } else if (err == -EINTR)
678 fuse_invalidate_entry(entry);
679 return err;
680}
681
682static int fuse_rmdir(struct inode *dir, struct dentry *entry)
683{
684 int err;
685 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100686 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700687
Miklos Szeredi70781872014-12-12 09:49:05 +0100688 args.in.h.opcode = FUSE_RMDIR;
689 args.in.h.nodeid = get_node_id(dir);
690 args.in.numargs = 1;
691 args.in.args[0].size = entry->d_name.len + 1;
692 args.in.args[0].value = entry->d_name.name;
693 err = fuse_simple_request(fc, &args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700694 if (!err) {
David Howells2b0143b2015-03-17 22:25:59 +0000695 clear_nlink(d_inode(entry));
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700696 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800697 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700698 } else if (err == -EINTR)
699 fuse_invalidate_entry(entry);
700 return err;
701}
702
Miklos Szeredi1560c972014-04-28 16:43:44 +0200703static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
704 struct inode *newdir, struct dentry *newent,
705 unsigned int flags, int opcode, size_t argsize)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700706{
707 int err;
Miklos Szeredi1560c972014-04-28 16:43:44 +0200708 struct fuse_rename2_in inarg;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700709 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredi70781872014-12-12 09:49:05 +0100710 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700711
Miklos Szeredi1560c972014-04-28 16:43:44 +0200712 memset(&inarg, 0, argsize);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700713 inarg.newdir = get_node_id(newdir);
Miklos Szeredi1560c972014-04-28 16:43:44 +0200714 inarg.flags = flags;
Miklos Szeredi70781872014-12-12 09:49:05 +0100715 args.in.h.opcode = opcode;
716 args.in.h.nodeid = get_node_id(olddir);
717 args.in.numargs = 3;
718 args.in.args[0].size = argsize;
719 args.in.args[0].value = &inarg;
720 args.in.args[1].size = oldent->d_name.len + 1;
721 args.in.args[1].value = oldent->d_name.name;
722 args.in.args[2].size = newent->d_name.len + 1;
723 args.in.args[2].value = newent->d_name.name;
724 err = fuse_simple_request(fc, &args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700725 if (!err) {
Miklos Szeredi08b63302007-11-28 16:22:03 -0800726 /* ctime changes */
David Howells2b0143b2015-03-17 22:25:59 +0000727 fuse_invalidate_attr(d_inode(oldent));
728 fuse_update_ctime(d_inode(oldent));
Miklos Szeredi08b63302007-11-28 16:22:03 -0800729
Miklos Szeredi1560c972014-04-28 16:43:44 +0200730 if (flags & RENAME_EXCHANGE) {
David Howells2b0143b2015-03-17 22:25:59 +0000731 fuse_invalidate_attr(d_inode(newent));
732 fuse_update_ctime(d_inode(newent));
Miklos Szeredi1560c972014-04-28 16:43:44 +0200733 }
734
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700735 fuse_invalidate_attr(olddir);
736 if (olddir != newdir)
737 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800738
739 /* newent will end up negative */
David Howells2b0143b2015-03-17 22:25:59 +0000740 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
741 fuse_invalidate_attr(d_inode(newent));
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800742 fuse_invalidate_entry_cache(newent);
David Howells2b0143b2015-03-17 22:25:59 +0000743 fuse_update_ctime(d_inode(newent));
Miklos Szeredi5219f342009-11-04 10:24:52 +0100744 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700745 } else if (err == -EINTR) {
746 /* If request was interrupted, DEITY only knows if the
747 rename actually took place. If the invalidation
748 fails (e.g. some process has CWD under the renamed
749 directory), then there can be inconsistency between
750 the dcache and the real filesystem. Tough luck. */
751 fuse_invalidate_entry(oldent);
David Howells2b0143b2015-03-17 22:25:59 +0000752 if (d_really_is_positive(newent))
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700753 fuse_invalidate_entry(newent);
754 }
755
756 return err;
757}
758
Miklos Szeredi1560c972014-04-28 16:43:44 +0200759static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
760 struct inode *newdir, struct dentry *newent,
761 unsigned int flags)
762{
763 struct fuse_conn *fc = get_fuse_conn(olddir);
764 int err;
765
766 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
767 return -EINVAL;
768
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200769 if (flags) {
770 if (fc->no_rename2 || fc->minor < 23)
771 return -EINVAL;
Miklos Szeredi1560c972014-04-28 16:43:44 +0200772
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200773 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
774 FUSE_RENAME2,
775 sizeof(struct fuse_rename2_in));
776 if (err == -ENOSYS) {
777 fc->no_rename2 = 1;
778 err = -EINVAL;
779 }
780 } else {
781 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
782 FUSE_RENAME,
783 sizeof(struct fuse_rename_in));
Miklos Szeredi1560c972014-04-28 16:43:44 +0200784 }
Miklos Szeredi1560c972014-04-28 16:43:44 +0200785
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200786 return err;
787}
788
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700789static int fuse_link(struct dentry *entry, struct inode *newdir,
790 struct dentry *newent)
791{
792 int err;
793 struct fuse_link_in inarg;
David Howells2b0143b2015-03-17 22:25:59 +0000794 struct inode *inode = d_inode(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700795 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +0100796 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700797
798 memset(&inarg, 0, sizeof(inarg));
799 inarg.oldnodeid = get_node_id(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +0100800 args.in.h.opcode = FUSE_LINK;
801 args.in.numargs = 2;
802 args.in.args[0].size = sizeof(inarg);
803 args.in.args[0].value = &inarg;
804 args.in.args[1].size = newent->d_name.len + 1;
805 args.in.args[1].value = newent->d_name.name;
806 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700807 /* Contrary to "normal" filesystems it can happen that link
808 makes two "logical" inodes point to the same "physical"
809 inode. We invalidate the attributes of the old one, so it
810 will reflect changes in the backing inode (link count,
811 etc.)
812 */
Miklos Szerediac45d612012-03-05 15:48:11 +0100813 if (!err) {
814 struct fuse_inode *fi = get_fuse_inode(inode);
815
816 spin_lock(&fc->lock);
817 fi->attr_version = ++fc->attr_version;
818 inc_nlink(inode);
819 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700820 fuse_invalidate_attr(inode);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200821 fuse_update_ctime(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100822 } else if (err == -EINTR) {
823 fuse_invalidate_attr(inode);
824 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700825 return err;
826}
827
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700828static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
829 struct kstat *stat)
830{
Miklos Szeredi203627b2012-05-10 19:49:38 +0400831 unsigned int blkbits;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400832 struct fuse_conn *fc = get_fuse_conn(inode);
833
834 /* see the comment in fuse_change_attributes() */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400835 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
Pavel Emelyanov83732002013-10-10 17:10:46 +0400836 attr->size = i_size_read(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400837 attr->mtime = inode->i_mtime.tv_sec;
838 attr->mtimensec = inode->i_mtime.tv_nsec;
Maxim Patlasov31f32672014-04-28 14:19:24 +0200839 attr->ctime = inode->i_ctime.tv_sec;
840 attr->ctimensec = inode->i_ctime.tv_nsec;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400841 }
Miklos Szeredi203627b2012-05-10 19:49:38 +0400842
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700843 stat->dev = inode->i_sb->s_dev;
844 stat->ino = attr->ino;
845 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
846 stat->nlink = attr->nlink;
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800847 stat->uid = make_kuid(&init_user_ns, attr->uid);
848 stat->gid = make_kgid(&init_user_ns, attr->gid);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700849 stat->rdev = inode->i_rdev;
850 stat->atime.tv_sec = attr->atime;
851 stat->atime.tv_nsec = attr->atimensec;
852 stat->mtime.tv_sec = attr->mtime;
853 stat->mtime.tv_nsec = attr->mtimensec;
854 stat->ctime.tv_sec = attr->ctime;
855 stat->ctime.tv_nsec = attr->ctimensec;
856 stat->size = attr->size;
857 stat->blocks = attr->blocks;
Miklos Szeredi203627b2012-05-10 19:49:38 +0400858
859 if (attr->blksize != 0)
860 blkbits = ilog2(attr->blksize);
861 else
862 blkbits = inode->i_sb->s_blocksize_bits;
863
864 stat->blksize = 1 << blkbits;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700865}
866
Miklos Szeredic79e3222007-10-18 03:06:59 -0700867static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
868 struct file *file)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700869{
870 int err;
Miklos Szeredic79e3222007-10-18 03:06:59 -0700871 struct fuse_getattr_in inarg;
872 struct fuse_attr_out outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700873 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +0100874 FUSE_ARGS(args);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700875 u64 attr_version;
876
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800877 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700878
Miklos Szeredic79e3222007-10-18 03:06:59 -0700879 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700880 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredic79e3222007-10-18 03:06:59 -0700881 /* Directories have separate file-handle space */
882 if (file && S_ISREG(inode->i_mode)) {
883 struct fuse_file *ff = file->private_data;
884
885 inarg.getattr_flags |= FUSE_GETATTR_FH;
886 inarg.fh = ff->fh;
887 }
Miklos Szeredi70781872014-12-12 09:49:05 +0100888 args.in.h.opcode = FUSE_GETATTR;
889 args.in.h.nodeid = get_node_id(inode);
890 args.in.numargs = 1;
891 args.in.args[0].size = sizeof(inarg);
892 args.in.args[0].value = &inarg;
893 args.out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +0100894 args.out.args[0].size = sizeof(outarg);
Miklos Szeredi70781872014-12-12 09:49:05 +0100895 args.out.args[0].value = &outarg;
896 err = fuse_simple_request(fc, &args);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700897 if (!err) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700898 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700899 make_bad_inode(inode);
900 err = -EIO;
901 } else {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700902 fuse_change_attributes(inode, &outarg.attr,
903 attr_timeout(&outarg),
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700904 attr_version);
905 if (stat)
Miklos Szeredic79e3222007-10-18 03:06:59 -0700906 fuse_fillattr(inode, &outarg.attr, stat);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700907 }
908 }
909 return err;
910}
911
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800912int fuse_update_attributes(struct inode *inode, struct kstat *stat,
913 struct file *file, bool *refreshed)
914{
915 struct fuse_inode *fi = get_fuse_inode(inode);
916 int err;
917 bool r;
918
Miklos Szeredi126b9d42014-07-07 15:28:50 +0200919 if (time_before64(fi->i_time, get_jiffies_64())) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800920 r = true;
921 err = fuse_do_getattr(inode, stat, file);
922 } else {
923 r = false;
924 err = 0;
925 if (stat) {
926 generic_fillattr(inode, stat);
927 stat->mode = fi->orig_i_mode;
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +0400928 stat->ino = fi->orig_ino;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800929 }
930 }
931
932 if (refreshed != NULL)
933 *refreshed = r;
934
935 return err;
936}
937
John Muir3b463ae2009-05-31 11:13:57 -0400938int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
John Muir451d0f52011-12-06 21:50:06 +0100939 u64 child_nodeid, struct qstr *name)
John Muir3b463ae2009-05-31 11:13:57 -0400940{
941 int err = -ENOTDIR;
942 struct inode *parent;
943 struct dentry *dir;
944 struct dentry *entry;
945
946 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
947 if (!parent)
948 return -ENOENT;
949
Al Viro59551022016-01-22 15:40:57 -0500950 inode_lock(parent);
John Muir3b463ae2009-05-31 11:13:57 -0400951 if (!S_ISDIR(parent->i_mode))
952 goto unlock;
953
954 err = -ENOENT;
955 dir = d_find_alias(parent);
956 if (!dir)
957 goto unlock;
958
Linus Torvalds8387ff22016-06-10 07:51:30 -0700959 name->hash = full_name_hash(dir, name->name, name->len);
John Muir3b463ae2009-05-31 11:13:57 -0400960 entry = d_lookup(dir, name);
961 dput(dir);
962 if (!entry)
963 goto unlock;
964
965 fuse_invalidate_attr(parent);
966 fuse_invalidate_entry(entry);
John Muir451d0f52011-12-06 21:50:06 +0100967
David Howells2b0143b2015-03-17 22:25:59 +0000968 if (child_nodeid != 0 && d_really_is_positive(entry)) {
Al Viro59551022016-01-22 15:40:57 -0500969 inode_lock(d_inode(entry));
David Howells2b0143b2015-03-17 22:25:59 +0000970 if (get_node_id(d_inode(entry)) != child_nodeid) {
John Muir451d0f52011-12-06 21:50:06 +0100971 err = -ENOENT;
972 goto badentry;
973 }
974 if (d_mountpoint(entry)) {
975 err = -EBUSY;
976 goto badentry;
977 }
David Howellse36cb0b2015-01-29 12:02:35 +0000978 if (d_is_dir(entry)) {
John Muir451d0f52011-12-06 21:50:06 +0100979 shrink_dcache_parent(entry);
980 if (!simple_empty(entry)) {
981 err = -ENOTEMPTY;
982 goto badentry;
983 }
David Howells2b0143b2015-03-17 22:25:59 +0000984 d_inode(entry)->i_flags |= S_DEAD;
John Muir451d0f52011-12-06 21:50:06 +0100985 }
986 dont_mount(entry);
David Howells2b0143b2015-03-17 22:25:59 +0000987 clear_nlink(d_inode(entry));
John Muir451d0f52011-12-06 21:50:06 +0100988 err = 0;
989 badentry:
Al Viro59551022016-01-22 15:40:57 -0500990 inode_unlock(d_inode(entry));
John Muir451d0f52011-12-06 21:50:06 +0100991 if (!err)
992 d_delete(entry);
993 } else {
994 err = 0;
995 }
John Muir3b463ae2009-05-31 11:13:57 -0400996 dput(entry);
John Muir3b463ae2009-05-31 11:13:57 -0400997
998 unlock:
Al Viro59551022016-01-22 15:40:57 -0500999 inode_unlock(parent);
John Muir3b463ae2009-05-31 11:13:57 -04001000 iput(parent);
1001 return err;
1002}
1003
Miklos Szeredi87729a52005-09-09 13:10:34 -07001004/*
1005 * Calling into a user-controlled filesystem gives the filesystem
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001006 * daemon ptrace-like capabilities over the current process. This
Miklos Szeredi87729a52005-09-09 13:10:34 -07001007 * means, that the filesystem daemon is able to record the exact
1008 * filesystem operations performed, and can also control the behavior
1009 * of the requester process in otherwise impossible ways. For example
1010 * it can delay the operation for arbitrary length of time allowing
1011 * DoS against the requester.
1012 *
1013 * For this reason only those processes can call into the filesystem,
1014 * for which the owner of the mount has ptrace privilege. This
1015 * excludes processes started by other users, suid or sgid processes.
1016 */
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001017int fuse_allow_current_process(struct fuse_conn *fc)
Miklos Szeredi87729a52005-09-09 13:10:34 -07001018{
David Howellsc69e8d92008-11-14 10:39:19 +11001019 const struct cred *cred;
David Howellsc69e8d92008-11-14 10:39:19 +11001020
Miklos Szeredi87729a52005-09-09 13:10:34 -07001021 if (fc->flags & FUSE_ALLOW_OTHER)
1022 return 1;
1023
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001024 cred = current_cred();
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001025 if (uid_eq(cred->euid, fc->user_id) &&
1026 uid_eq(cred->suid, fc->user_id) &&
1027 uid_eq(cred->uid, fc->user_id) &&
1028 gid_eq(cred->egid, fc->group_id) &&
1029 gid_eq(cred->sgid, fc->group_id) &&
1030 gid_eq(cred->gid, fc->group_id))
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001031 return 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001032
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001033 return 0;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001034}
1035
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001036static int fuse_access(struct inode *inode, int mask)
1037{
1038 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001039 FUSE_ARGS(args);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001040 struct fuse_access_in inarg;
1041 int err;
1042
Miklos Szeredi698fa1d2013-10-01 16:41:23 +02001043 BUG_ON(mask & MAY_NOT_BLOCK);
1044
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001045 if (fc->no_access)
1046 return 0;
1047
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001048 memset(&inarg, 0, sizeof(inarg));
Al Viroe6305c42008-07-15 21:03:57 -04001049 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
Miklos Szeredi70781872014-12-12 09:49:05 +01001050 args.in.h.opcode = FUSE_ACCESS;
1051 args.in.h.nodeid = get_node_id(inode);
1052 args.in.numargs = 1;
1053 args.in.args[0].size = sizeof(inarg);
1054 args.in.args[0].value = &inarg;
1055 err = fuse_simple_request(fc, &args);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001056 if (err == -ENOSYS) {
1057 fc->no_access = 1;
1058 err = 0;
1059 }
1060 return err;
1061}
1062
Al Viro10556cb2011-06-20 19:28:19 -04001063static int fuse_perm_getattr(struct inode *inode, int mask)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001064{
Al Viro10556cb2011-06-20 19:28:19 -04001065 if (mask & MAY_NOT_BLOCK)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001066 return -ECHILD;
1067
1068 return fuse_do_getattr(inode, NULL, NULL);
1069}
1070
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001071/*
1072 * Check permission. The two basic access models of FUSE are:
1073 *
1074 * 1) Local access checking ('default_permissions' mount option) based
1075 * on file mode. This is the plain old disk filesystem permission
1076 * modell.
1077 *
1078 * 2) "Remote" access checking, where server is responsible for
1079 * checking permission in each inode operation. An exception to this
1080 * is if ->permission() was invoked from sys_access() in which case an
1081 * access request is sent. Execute permission is still checked
1082 * locally based on file mode.
1083 */
Al Viro10556cb2011-06-20 19:28:19 -04001084static int fuse_permission(struct inode *inode, int mask)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001085{
1086 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001087 bool refreshed = false;
1088 int err = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001089
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001090 if (!fuse_allow_current_process(fc))
Miklos Szeredie5e55582005-09-09 13:10:28 -07001091 return -EACCES;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001092
1093 /*
Miklos Szeredie8e96152007-10-16 23:31:06 -07001094 * If attributes are needed, refresh them before proceeding
Miklos Szeredi244f6382007-10-16 23:31:02 -07001095 */
Miklos Szeredie8e96152007-10-16 23:31:06 -07001096 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1097 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001098 struct fuse_inode *fi = get_fuse_inode(inode);
1099
Miklos Szeredi126b9d42014-07-07 15:28:50 +02001100 if (time_before64(fi->i_time, get_jiffies_64())) {
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001101 refreshed = true;
1102
Al Viro10556cb2011-06-20 19:28:19 -04001103 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001104 if (err)
1105 return err;
1106 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001107 }
1108
1109 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
Al Viro2830ba72011-06-20 19:16:29 -04001110 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001111
1112 /* If permission is denied, try to refresh file
1113 attributes. This is also needed, because the root
1114 node will at first have no permissions */
Miklos Szeredi244f6382007-10-16 23:31:02 -07001115 if (err == -EACCES && !refreshed) {
Al Viro10556cb2011-06-20 19:28:19 -04001116 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001117 if (!err)
Al Viro2830ba72011-06-20 19:16:29 -04001118 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001119 }
1120
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001121 /* Note: the opposite of the above test does not
1122 exist. So if permissions are revoked this won't be
1123 noticed immediately, only after the attribute
1124 timeout has expired */
Eric Paris9cfcac82010-07-23 11:43:51 -04001125 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
Miklos Szeredie8e96152007-10-16 23:31:06 -07001126 err = fuse_access(inode, mask);
1127 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1128 if (!(inode->i_mode & S_IXUGO)) {
1129 if (refreshed)
1130 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001131
Al Viro10556cb2011-06-20 19:28:19 -04001132 err = fuse_perm_getattr(inode, mask);
Miklos Szeredie8e96152007-10-16 23:31:06 -07001133 if (!err && !(inode->i_mode & S_IXUGO))
1134 return -EACCES;
1135 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001136 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001137 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001138}
1139
1140static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001141 struct dir_context *ctx)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001142{
1143 while (nbytes >= FUSE_NAME_OFFSET) {
1144 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1145 size_t reclen = FUSE_DIRENT_SIZE(dirent);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001146 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1147 return -EIO;
1148 if (reclen > nbytes)
1149 break;
Miklos Szerediefeb9e62013-09-03 14:28:38 +02001150 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1151 return -EIO;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001152
Al Viro8d3af7f2013-05-18 03:03:58 -04001153 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1154 dirent->ino, dirent->type))
Miklos Szeredie5e55582005-09-09 13:10:28 -07001155 break;
1156
1157 buf += reclen;
1158 nbytes -= reclen;
Al Viro8d3af7f2013-05-18 03:03:58 -04001159 ctx->pos = dirent->off;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001160 }
1161
1162 return 0;
1163}
1164
Anand V. Avati0b05b182012-08-19 08:53:23 -04001165static int fuse_direntplus_link(struct file *file,
1166 struct fuse_direntplus *direntplus,
1167 u64 attr_version)
1168{
Anand V. Avati0b05b182012-08-19 08:53:23 -04001169 struct fuse_entry_out *o = &direntplus->entry_out;
1170 struct fuse_dirent *dirent = &direntplus->dirent;
1171 struct dentry *parent = file->f_path.dentry;
1172 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1173 struct dentry *dentry;
1174 struct dentry *alias;
David Howells2b0143b2015-03-17 22:25:59 +00001175 struct inode *dir = d_inode(parent);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001176 struct fuse_conn *fc;
1177 struct inode *inode;
Al Virod9b3dbd2016-04-20 17:30:32 -04001178 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001179
1180 if (!o->nodeid) {
1181 /*
1182 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1183 * ENOENT. Instead, it only means the userspace filesystem did
1184 * not want to return attributes/handle for this entry.
1185 *
1186 * So do nothing.
1187 */
1188 return 0;
1189 }
1190
1191 if (name.name[0] == '.') {
1192 /*
1193 * We could potentially refresh the attributes of the directory
1194 * and its parent?
1195 */
1196 if (name.len == 1)
1197 return 0;
1198 if (name.name[1] == '.' && name.len == 2)
1199 return 0;
1200 }
Miklos Szeredia28ef452013-07-17 14:53:53 +02001201
1202 if (invalid_nodeid(o->nodeid))
1203 return -EIO;
1204 if (!fuse_valid_type(o->attr.mode))
1205 return -EIO;
1206
Anand V. Avati0b05b182012-08-19 08:53:23 -04001207 fc = get_fuse_conn(dir);
1208
Linus Torvalds8387ff22016-06-10 07:51:30 -07001209 name.hash = full_name_hash(parent, name.name, name.len);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001210 dentry = d_lookup(parent, &name);
Al Virod9b3dbd2016-04-20 17:30:32 -04001211 if (!dentry) {
1212retry:
1213 dentry = d_alloc_parallel(parent, &name, &wq);
1214 if (IS_ERR(dentry))
1215 return PTR_ERR(dentry);
1216 }
1217 if (!d_in_lookup(dentry)) {
1218 struct fuse_inode *fi;
David Howells2b0143b2015-03-17 22:25:59 +00001219 inode = d_inode(dentry);
Al Virod9b3dbd2016-04-20 17:30:32 -04001220 if (!inode ||
1221 get_node_id(inode) != o->nodeid ||
1222 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001223 d_invalidate(dentry);
Al Virod9b3dbd2016-04-20 17:30:32 -04001224 dput(dentry);
1225 goto retry;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001226 }
Al Virod9b3dbd2016-04-20 17:30:32 -04001227 if (is_bad_inode(inode)) {
1228 dput(dentry);
1229 return -EIO;
1230 }
1231
1232 fi = get_fuse_inode(inode);
1233 spin_lock(&fc->lock);
1234 fi->nlookup++;
1235 spin_unlock(&fc->lock);
1236
1237 fuse_change_attributes(inode, &o->attr,
1238 entry_attr_timeout(o),
1239 attr_version);
1240 /*
1241 * The other branch comes via fuse_iget()
1242 * which bumps nlookup inside
1243 */
1244 } else {
1245 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1246 &o->attr, entry_attr_timeout(o),
1247 attr_version);
1248 if (!inode)
1249 inode = ERR_PTR(-ENOMEM);
1250
1251 alias = d_splice_alias(inode, dentry);
1252 d_lookup_done(dentry);
1253 if (alias) {
1254 dput(dentry);
1255 dentry = alias;
1256 }
1257 if (IS_ERR(dentry))
1258 return PTR_ERR(dentry);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001259 }
Miklos Szeredi6314efe2013-10-01 16:41:22 +02001260 if (fc->readdirplus_auto)
1261 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001262 fuse_change_entry_timeout(dentry, o);
1263
Miklos Szeredic7263bc2013-07-17 14:53:54 +02001264 dput(dentry);
Al Virod9b3dbd2016-04-20 17:30:32 -04001265 return 0;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001266}
1267
1268static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001269 struct dir_context *ctx, u64 attr_version)
Anand V. Avati0b05b182012-08-19 08:53:23 -04001270{
1271 struct fuse_direntplus *direntplus;
1272 struct fuse_dirent *dirent;
1273 size_t reclen;
1274 int over = 0;
1275 int ret;
1276
1277 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1278 direntplus = (struct fuse_direntplus *) buf;
1279 dirent = &direntplus->dirent;
1280 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1281
1282 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1283 return -EIO;
1284 if (reclen > nbytes)
1285 break;
Miklos Szerediefeb9e62013-09-03 14:28:38 +02001286 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1287 return -EIO;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001288
1289 if (!over) {
1290 /* We fill entries into dstbuf only as much as
1291 it can hold. But we still continue iterating
1292 over remaining entries to link them. If not,
1293 we need to send a FORGET for each of those
1294 which we did not link.
1295 */
Al Viro8d3af7f2013-05-18 03:03:58 -04001296 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1297 dirent->ino, dirent->type);
1298 ctx->pos = dirent->off;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001299 }
1300
1301 buf += reclen;
1302 nbytes -= reclen;
1303
1304 ret = fuse_direntplus_link(file, direntplus, attr_version);
1305 if (ret)
1306 fuse_force_forget(file, direntplus->entry_out.nodeid);
1307 }
1308
1309 return 0;
1310}
1311
Al Viro8d3af7f2013-05-18 03:03:58 -04001312static int fuse_readdir(struct file *file, struct dir_context *ctx)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001313{
Feng Shuo4582a4a2013-01-15 11:23:28 +08001314 int plus, err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001315 size_t nbytes;
1316 struct page *page;
Al Viro496ad9a2013-01-23 17:07:38 -05001317 struct inode *inode = file_inode(file);
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001318 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001319 struct fuse_req *req;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001320 u64 attr_version = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001321
1322 if (is_bad_inode(inode))
1323 return -EIO;
1324
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001325 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001326 if (IS_ERR(req))
1327 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001328
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001329 page = alloc_page(GFP_KERNEL);
1330 if (!page) {
1331 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001332 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001333 }
Feng Shuo4582a4a2013-01-15 11:23:28 +08001334
Al Viro8d3af7f2013-05-18 03:03:58 -04001335 plus = fuse_use_readdirplus(inode, ctx);
Miklos Szeredif4975c62009-04-02 14:25:34 +02001336 req->out.argpages = 1;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001337 req->num_pages = 1;
1338 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001339 req->page_descs[0].length = PAGE_SIZE;
Feng Shuo4582a4a2013-01-15 11:23:28 +08001340 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001341 attr_version = fuse_get_attr_version(fc);
Al Viro8d3af7f2013-05-18 03:03:58 -04001342 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001343 FUSE_READDIRPLUS);
1344 } else {
Al Viro8d3af7f2013-05-18 03:03:58 -04001345 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001346 FUSE_READDIR);
1347 }
Miklos Szeredi5c672ab2016-06-30 13:10:49 +02001348 fuse_lock_inode(inode);
Tejun Heob93f8582008-11-26 12:03:55 +01001349 fuse_request_send(fc, req);
Miklos Szeredi5c672ab2016-06-30 13:10:49 +02001350 fuse_unlock_inode(inode);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -08001351 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001352 err = req->out.h.error;
1353 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001354 if (!err) {
Feng Shuo4582a4a2013-01-15 11:23:28 +08001355 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001356 err = parse_dirplusfile(page_address(page), nbytes,
Al Viro8d3af7f2013-05-18 03:03:58 -04001357 file, ctx,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001358 attr_version);
1359 } else {
1360 err = parse_dirfile(page_address(page), nbytes, file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001361 ctx);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001362 }
1363 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001364
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001365 __free_page(page);
Andrew Gallagher451418f2013-11-05 03:55:43 -08001366 fuse_invalidate_atime(inode);
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001367 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001368}
1369
Al Viro6b255392015-11-17 10:20:54 -05001370static const char *fuse_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001371 struct inode *inode,
1372 struct delayed_call *done)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001373{
Miklos Szeredie5e55582005-09-09 13:10:28 -07001374 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001375 FUSE_ARGS(args);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001376 char *link;
Miklos Szeredi70781872014-12-12 09:49:05 +01001377 ssize_t ret;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001378
Al Viro6b255392015-11-17 10:20:54 -05001379 if (!dentry)
1380 return ERR_PTR(-ECHILD);
1381
Al Virocd3417c2015-12-29 16:03:53 -05001382 link = kmalloc(PAGE_SIZE, GFP_KERNEL);
Miklos Szeredi70781872014-12-12 09:49:05 +01001383 if (!link)
1384 return ERR_PTR(-ENOMEM);
1385
1386 args.in.h.opcode = FUSE_READLINK;
1387 args.in.h.nodeid = get_node_id(inode);
1388 args.out.argvar = 1;
1389 args.out.numargs = 1;
1390 args.out.args[0].size = PAGE_SIZE - 1;
1391 args.out.args[0].value = link;
1392 ret = fuse_simple_request(fc, &args);
1393 if (ret < 0) {
Al Virocd3417c2015-12-29 16:03:53 -05001394 kfree(link);
Miklos Szeredi70781872014-12-12 09:49:05 +01001395 link = ERR_PTR(ret);
1396 } else {
1397 link[ret] = '\0';
Al Virofceef392015-12-29 15:58:39 -05001398 set_delayed_call(done, kfree_link, link);
Miklos Szeredi70781872014-12-12 09:49:05 +01001399 }
Andrew Gallagher451418f2013-11-05 03:55:43 -08001400 fuse_invalidate_atime(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001401 return link;
1402}
1403
Miklos Szeredie5e55582005-09-09 13:10:28 -07001404static int fuse_dir_open(struct inode *inode, struct file *file)
1405{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +02001406 return fuse_open_common(inode, file, true);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001407}
1408
1409static int fuse_dir_release(struct inode *inode, struct file *file)
1410{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +02001411 fuse_release_common(file, FUSE_RELEASEDIR);
1412
1413 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001414}
1415
Josef Bacik02c24a82011-07-16 20:44:56 -04001416static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1417 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -07001418{
Josef Bacik02c24a82011-07-16 20:44:56 -04001419 return fuse_fsync_common(file, start, end, datasync, 1);
Miklos Szeredi82547982005-09-09 13:10:38 -07001420}
1421
Miklos Szeredib18da0c2011-12-13 11:58:49 +01001422static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1423 unsigned long arg)
1424{
1425 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1426
1427 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1428 if (fc->minor < 18)
1429 return -ENOTTY;
1430
1431 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1432}
1433
1434static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1435 unsigned long arg)
1436{
1437 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1438
1439 if (fc->minor < 18)
1440 return -ENOTTY;
1441
1442 return fuse_ioctl_common(file, cmd, arg,
1443 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1444}
1445
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001446static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001447{
1448 /* Always update if mtime is explicitly set */
1449 if (ivalid & ATTR_MTIME_SET)
1450 return true;
1451
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001452 /* Or if kernel i_mtime is the official one */
1453 if (trust_local_mtime)
1454 return true;
1455
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001456 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1457 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1458 return false;
1459
1460 /* In all other cases update */
1461 return true;
1462}
1463
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001464static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001465 bool trust_local_cmtime)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001466{
1467 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001468
1469 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001470 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001471 if (ivalid & ATTR_UID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001472 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001473 if (ivalid & ATTR_GID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001474 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001475 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001476 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001477 if (ivalid & ATTR_ATIME) {
1478 arg->valid |= FATTR_ATIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001479 arg->atime = iattr->ia_atime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001480 arg->atimensec = iattr->ia_atime.tv_nsec;
1481 if (!(ivalid & ATTR_ATIME_SET))
1482 arg->valid |= FATTR_ATIME_NOW;
1483 }
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001484 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001485 arg->valid |= FATTR_MTIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001486 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001487 arg->mtimensec = iattr->ia_mtime.tv_nsec;
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001488 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001489 arg->valid |= FATTR_MTIME_NOW;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001490 }
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001491 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1492 arg->valid |= FATTR_CTIME;
1493 arg->ctime = iattr->ia_ctime.tv_sec;
1494 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1495 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001496}
1497
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001498/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001499 * Prevent concurrent writepages on inode
1500 *
1501 * This is done by adding a negative bias to the inode write counter
1502 * and waiting for all pending writes to finish.
1503 */
1504void fuse_set_nowrite(struct inode *inode)
1505{
1506 struct fuse_conn *fc = get_fuse_conn(inode);
1507 struct fuse_inode *fi = get_fuse_inode(inode);
1508
Al Viro59551022016-01-22 15:40:57 -05001509 BUG_ON(!inode_is_locked(inode));
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001510
1511 spin_lock(&fc->lock);
1512 BUG_ON(fi->writectr < 0);
1513 fi->writectr += FUSE_NOWRITE;
1514 spin_unlock(&fc->lock);
1515 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1516}
1517
1518/*
1519 * Allow writepages on inode
1520 *
1521 * Remove the bias from the writecounter and send any queued
1522 * writepages.
1523 */
1524static void __fuse_release_nowrite(struct inode *inode)
1525{
1526 struct fuse_inode *fi = get_fuse_inode(inode);
1527
1528 BUG_ON(fi->writectr != FUSE_NOWRITE);
1529 fi->writectr = 0;
1530 fuse_flush_writepages(inode);
1531}
1532
1533void fuse_release_nowrite(struct inode *inode)
1534{
1535 struct fuse_conn *fc = get_fuse_conn(inode);
1536
1537 spin_lock(&fc->lock);
1538 __fuse_release_nowrite(inode);
1539 spin_unlock(&fc->lock);
1540}
1541
Miklos Szeredi70781872014-12-12 09:49:05 +01001542static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001543 struct inode *inode,
1544 struct fuse_setattr_in *inarg_p,
1545 struct fuse_attr_out *outarg_p)
1546{
Miklos Szeredi70781872014-12-12 09:49:05 +01001547 args->in.h.opcode = FUSE_SETATTR;
1548 args->in.h.nodeid = get_node_id(inode);
1549 args->in.numargs = 1;
1550 args->in.args[0].size = sizeof(*inarg_p);
1551 args->in.args[0].value = inarg_p;
1552 args->out.numargs = 1;
Miklos Szeredi21f62172015-01-06 10:45:35 +01001553 args->out.args[0].size = sizeof(*outarg_p);
Miklos Szeredi70781872014-12-12 09:49:05 +01001554 args->out.args[0].value = outarg_p;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001555}
1556
1557/*
1558 * Flush inode->i_mtime to the server
1559 */
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001560int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001561{
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001562 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001563 FUSE_ARGS(args);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001564 struct fuse_setattr_in inarg;
1565 struct fuse_attr_out outarg;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001566
1567 memset(&inarg, 0, sizeof(inarg));
1568 memset(&outarg, 0, sizeof(outarg));
1569
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001570 inarg.valid = FATTR_MTIME;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001571 inarg.mtime = inode->i_mtime.tv_sec;
1572 inarg.mtimensec = inode->i_mtime.tv_nsec;
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001573 if (fc->minor >= 23) {
1574 inarg.valid |= FATTR_CTIME;
1575 inarg.ctime = inode->i_ctime.tv_sec;
1576 inarg.ctimensec = inode->i_ctime.tv_nsec;
1577 }
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001578 if (ff) {
1579 inarg.valid |= FATTR_FH;
1580 inarg.fh = ff->fh;
1581 }
Miklos Szeredi70781872014-12-12 09:49:05 +01001582 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001583
Miklos Szeredi70781872014-12-12 09:49:05 +01001584 return fuse_simple_request(fc, &args);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001585}
1586
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001587/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001588 * Set attributes, and at the same time refresh them.
1589 *
1590 * Truncation is slightly complicated, because the 'truncate' request
1591 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -07001592 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1593 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001594 */
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001595int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1596 struct file *file)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001597{
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001598 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001599 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi70781872014-12-12 09:49:05 +01001600 FUSE_ARGS(args);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001601 struct fuse_setattr_in inarg;
1602 struct fuse_attr_out outarg;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001603 bool is_truncate = false;
Pavel Emelyanov83732002013-10-10 17:10:46 +04001604 bool is_wb = fc->writeback_cache;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001605 loff_t oldsize;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001606 int err;
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001607 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001608
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001609 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1610 attr->ia_valid |= ATTR_FORCE;
1611
1612 err = inode_change_ok(inode, attr);
1613 if (err)
1614 return err;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001615
Miklos Szeredi8d56add2011-02-25 14:44:58 +01001616 if (attr->ia_valid & ATTR_OPEN) {
1617 if (fc->atomic_o_trunc)
1618 return 0;
1619 file = NULL;
1620 }
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001621
Christoph Hellwig2c27c652010-06-04 11:30:04 +02001622 if (attr->ia_valid & ATTR_SIZE)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001623 is_truncate = true;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001624
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001625 if (is_truncate) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001626 fuse_set_nowrite(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001627 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001628 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1629 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001630 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001631
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001632 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001633 memset(&outarg, 0, sizeof(outarg));
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001634 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001635 if (file) {
1636 struct fuse_file *ff = file->private_data;
1637 inarg.valid |= FATTR_FH;
1638 inarg.fh = ff->fh;
1639 }
Miklos Szeredif3332112007-10-18 03:07:04 -07001640 if (attr->ia_valid & ATTR_SIZE) {
1641 /* For mandatory locking in truncate */
1642 inarg.valid |= FATTR_LOCKOWNER;
1643 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1644 }
Miklos Szeredi70781872014-12-12 09:49:05 +01001645 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1646 err = fuse_simple_request(fc, &args);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001647 if (err) {
1648 if (err == -EINTR)
1649 fuse_invalidate_attr(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001650 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001651 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001652
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001653 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1654 make_bad_inode(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001655 err = -EIO;
1656 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001657 }
1658
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001659 spin_lock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001660 /* the kernel maintains i_mtime locally */
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001661 if (trust_local_cmtime) {
1662 if (attr->ia_valid & ATTR_MTIME)
1663 inode->i_mtime = attr->ia_mtime;
1664 if (attr->ia_valid & ATTR_CTIME)
1665 inode->i_ctime = attr->ia_ctime;
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001666 /* FIXME: clear I_DIRTY_SYNC? */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001667 }
1668
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001669 fuse_change_attributes_common(inode, &outarg.attr,
1670 attr_timeout(&outarg));
1671 oldsize = inode->i_size;
Pavel Emelyanov83732002013-10-10 17:10:46 +04001672 /* see the comment in fuse_change_attributes() */
1673 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1674 i_size_write(inode, outarg.attr.size);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001675
1676 if (is_truncate) {
1677 /* NOTE: this may release/reacquire fc->lock */
1678 __fuse_release_nowrite(inode);
1679 }
1680 spin_unlock(&fc->lock);
1681
1682 /*
1683 * Only call invalidate_inode_pages2() after removing
1684 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1685 */
Pavel Emelyanov83732002013-10-10 17:10:46 +04001686 if ((is_truncate || !is_wb) &&
1687 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001688 truncate_pagecache(inode, outarg.attr.size);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001689 invalidate_inode_pages2(inode->i_mapping);
1690 }
1691
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001692 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001693 return 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001694
1695error:
1696 if (is_truncate)
1697 fuse_release_nowrite(inode);
1698
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001699 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001700 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001701}
1702
Miklos Szeredi49d49142007-10-18 03:07:00 -07001703static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1704{
David Howells2b0143b2015-03-17 22:25:59 +00001705 struct inode *inode = d_inode(entry);
Miklos Szeredi5e2b8822016-10-01 07:32:32 +02001706 int ret;
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001707
1708 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1709 return -EACCES;
1710
Miklos Szeredi49d49142007-10-18 03:07:00 -07001711 if (attr->ia_valid & ATTR_FILE)
Miklos Szeredi5e2b8822016-10-01 07:32:32 +02001712 ret = fuse_do_setattr(inode, attr, attr->ia_file);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001713 else
Miklos Szeredi5e2b8822016-10-01 07:32:32 +02001714 ret = fuse_do_setattr(inode, attr, NULL);
1715
1716 if (!ret) {
1717 /* Directory mode changed, may need to revalidate access */
1718 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1719 fuse_invalidate_entry_cache(entry);
1720 }
1721 return ret;
Miklos Szeredi49d49142007-10-18 03:07:00 -07001722}
1723
Miklos Szeredie5e55582005-09-09 13:10:28 -07001724static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1725 struct kstat *stat)
1726{
David Howells2b0143b2015-03-17 22:25:59 +00001727 struct inode *inode = d_inode(entry);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001728 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001729
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001730 if (!fuse_allow_current_process(fc))
Miklos Szeredi244f6382007-10-16 23:31:02 -07001731 return -EACCES;
1732
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001733 return fuse_update_attributes(inode, stat, NULL, NULL);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001734}
1735
Arjan van de Ven754661f2007-02-12 00:55:38 -08001736static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07001737 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001738 .mkdir = fuse_mkdir,
1739 .symlink = fuse_symlink,
1740 .unlink = fuse_unlink,
1741 .rmdir = fuse_rmdir,
Miklos Szeredi1560c972014-04-28 16:43:44 +02001742 .rename2 = fuse_rename2,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001743 .link = fuse_link,
1744 .setattr = fuse_setattr,
1745 .create = fuse_create,
Miklos Szeredic8ccbe02012-06-05 15:10:22 +02001746 .atomic_open = fuse_atomic_open,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001747 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001748 .permission = fuse_permission,
1749 .getattr = fuse_getattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001750 .setxattr = generic_setxattr,
1751 .getxattr = generic_getxattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001752 .listxattr = fuse_listxattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001753 .removexattr = generic_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001754};
1755
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001756static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001757 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001758 .read = generic_read_dir,
Al Virod9b3dbd2016-04-20 17:30:32 -04001759 .iterate_shared = fuse_readdir,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001760 .open = fuse_dir_open,
1761 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001762 .fsync = fuse_dir_fsync,
Miklos Szeredib18da0c2011-12-13 11:58:49 +01001763 .unlocked_ioctl = fuse_dir_ioctl,
1764 .compat_ioctl = fuse_dir_compat_ioctl,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001765};
1766
Arjan van de Ven754661f2007-02-12 00:55:38 -08001767static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001768 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001769 .permission = fuse_permission,
1770 .getattr = fuse_getattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001771 .setxattr = generic_setxattr,
1772 .getxattr = generic_getxattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001773 .listxattr = fuse_listxattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001774 .removexattr = generic_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001775};
1776
Arjan van de Ven754661f2007-02-12 00:55:38 -08001777static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001778 .setattr = fuse_setattr,
Al Viro6b255392015-11-17 10:20:54 -05001779 .get_link = fuse_get_link,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001780 .readlink = generic_readlink,
1781 .getattr = fuse_getattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001782 .setxattr = generic_setxattr,
1783 .getxattr = generic_getxattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001784 .listxattr = fuse_listxattr,
Seth Forshee703c7362016-08-29 08:46:36 -05001785 .removexattr = generic_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001786};
1787
1788void fuse_init_common(struct inode *inode)
1789{
1790 inode->i_op = &fuse_common_inode_operations;
1791}
1792
1793void fuse_init_dir(struct inode *inode)
1794{
1795 inode->i_op = &fuse_dir_inode_operations;
1796 inode->i_fop = &fuse_dir_operations;
1797}
1798
1799void fuse_init_symlink(struct inode *inode)
1800{
1801 inode->i_op = &fuse_symlink_inode_operations;
1802}