blob: de1d84af9f7c6baf27b64218fbff43604ab47c04 [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>
Miklos Szeredie5e55582005-09-09 13:10:28 -070016
Al Viro8d3af7f2013-05-18 03:03:58 -040017static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
Feng Shuo4582a4a2013-01-15 11:23:28 +080018{
19 struct fuse_conn *fc = get_fuse_conn(dir);
20 struct fuse_inode *fi = get_fuse_inode(dir);
21
22 if (!fc->do_readdirplus)
23 return false;
Eric Wong634734b2013-02-06 22:29:01 +000024 if (!fc->readdirplus_auto)
25 return true;
Feng Shuo4582a4a2013-01-15 11:23:28 +080026 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
27 return true;
Al Viro8d3af7f2013-05-18 03:03:58 -040028 if (ctx->pos == 0)
Feng Shuo4582a4a2013-01-15 11:23:28 +080029 return true;
30 return false;
31}
32
33static void fuse_advise_use_readdirplus(struct inode *dir)
34{
35 struct fuse_inode *fi = get_fuse_inode(dir);
36
37 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
38}
39
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070040#if BITS_PER_LONG >= 64
41static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
42{
43 entry->d_time = time;
44}
45
46static inline u64 fuse_dentry_time(struct dentry *entry)
47{
48 return entry->d_time;
49}
50#else
51/*
52 * On 32 bit archs store the high 32 bits of time in d_fsdata
53 */
54static void fuse_dentry_settime(struct dentry *entry, u64 time)
55{
56 entry->d_time = time;
57 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
58}
59
60static u64 fuse_dentry_time(struct dentry *entry)
61{
62 return (u64) entry->d_time +
63 ((u64) (unsigned long) entry->d_fsdata << 32);
64}
65#endif
66
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080067/*
68 * FUSE caches dentries and attributes with separate timeout. The
69 * time in jiffies until the dentry/attributes are valid is stored in
70 * dentry->d_time and fuse_inode->i_time respectively.
71 */
72
73/*
74 * Calculate the time in jiffies until a dentry/attributes are valid
75 */
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070076static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
Miklos Szeredie5e55582005-09-09 13:10:28 -070077{
Miklos Szeredi685d16d2006-07-30 03:04:08 -070078 if (sec || nsec) {
79 struct timespec ts = {sec, nsec};
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070080 return get_jiffies_64() + timespec_to_jiffies(&ts);
Miklos Szeredi685d16d2006-07-30 03:04:08 -070081 } else
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070082 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070083}
84
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080085/*
86 * Set dentry and possibly attribute timeouts from the lookup/mk*
87 * replies
88 */
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070089static void fuse_change_entry_timeout(struct dentry *entry,
90 struct fuse_entry_out *o)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080091{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070092 fuse_dentry_settime(entry,
93 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070094}
95
96static u64 attr_timeout(struct fuse_attr_out *o)
97{
98 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
99}
100
101static u64 entry_attr_timeout(struct fuse_entry_out *o)
102{
103 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800104}
105
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800106/*
107 * Mark the attributes as stale, so that at the next call to
108 * ->getattr() they will be fetched from userspace
109 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800110void fuse_invalidate_attr(struct inode *inode)
111{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700112 get_fuse_inode(inode)->i_time = 0;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800113}
114
Andrew Gallagher451418f2013-11-05 03:55:43 -0800115/**
116 * Mark the attributes as stale due to an atime change. Avoid the invalidate if
117 * atime is not used.
118 */
119void fuse_invalidate_atime(struct inode *inode)
120{
121 if (!IS_RDONLY(inode))
122 fuse_invalidate_attr(inode);
123}
124
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800125/*
126 * Just mark the entry as stale, so that a next attempt to look it up
127 * will result in a new lookup call to userspace
128 *
129 * This is called when a dentry is about to become negative and the
130 * timeout is unknown (unlink, rmdir, rename and in some cases
131 * lookup)
132 */
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700133void fuse_invalidate_entry_cache(struct dentry *entry)
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800134{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700135 fuse_dentry_settime(entry, 0);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800136}
137
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800138/*
139 * Same as fuse_invalidate_entry_cache(), but also try to remove the
140 * dentry from the hash
141 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800142static void fuse_invalidate_entry(struct dentry *entry)
143{
144 d_invalidate(entry);
145 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800146}
147
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700148static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
149 u64 nodeid, struct qstr *name,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700150 struct fuse_entry_out *outarg)
151{
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700152 memset(outarg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700153 req->in.h.opcode = FUSE_LOOKUP;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700154 req->in.h.nodeid = nodeid;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700155 req->in.numargs = 1;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700156 req->in.args[0].size = name->len + 1;
157 req->in.args[0].value = name->name;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700158 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700159 if (fc->minor < 9)
160 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
161 else
162 req->out.args[0].size = sizeof(struct fuse_entry_out);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700163 req->out.args[0].value = outarg;
164}
165
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700166u64 fuse_get_attr_version(struct fuse_conn *fc)
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800167{
168 u64 curr_version;
169
170 /*
171 * The spin lock isn't actually needed on 64bit archs, but we
172 * don't yet care too much about such optimizations.
173 */
174 spin_lock(&fc->lock);
175 curr_version = fc->attr_version;
176 spin_unlock(&fc->lock);
177
178 return curr_version;
179}
180
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800181/*
182 * Check whether the dentry is still valid
183 *
184 * If the entry validity timeout has expired and the dentry is
185 * positive, try to redo the lookup. If the lookup results in a
186 * different inode, then let the VFS invalidate the dentry and redo
187 * the lookup once more. If the lookup results in the same inode,
188 * then refresh the attributes, timeouts and mark the dentry valid.
189 */
Al Viro0b728e12012-06-10 16:03:43 -0400190static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700191{
Nick Piggin34286d62011-01-07 17:49:57 +1100192 struct inode *inode;
Miklos Szeredi28420da2013-06-03 14:40:22 +0200193 struct dentry *parent;
194 struct fuse_conn *fc;
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200195 struct fuse_inode *fi;
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200196 int ret;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800197
Miklos Szeredie7c0a162011-03-21 13:58:06 +0100198 inode = ACCESS_ONCE(entry->d_inode);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800199 if (inode && is_bad_inode(inode))
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200200 goto invalid;
Anand Avati154210c2014-06-26 20:21:57 -0400201 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
202 (flags & LOOKUP_REVAL)) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700203 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700204 struct fuse_entry_out outarg;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800205 struct fuse_req *req;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100206 struct fuse_forget_link *forget;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700207 u64 attr_version;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800208
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800209 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800210 if (!inode)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200211 goto invalid;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800212
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200213 ret = -ECHILD;
Al Viro0b728e12012-06-10 16:03:43 -0400214 if (flags & LOOKUP_RCU)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200215 goto out;
Miklos Szeredie7c0a162011-03-21 13:58:06 +0100216
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800217 fc = get_fuse_conn(inode);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400218 req = fuse_get_req_nopages(fc);
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200219 ret = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700220 if (IS_ERR(req))
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200221 goto out;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700222
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100223 forget = fuse_alloc_forget();
224 if (!forget) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800225 fuse_put_request(fc, req);
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200226 ret = -ENOMEM;
227 goto out;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800228 }
229
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800230 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700231
Miklos Szeredie956edd2006-10-17 00:10:12 -0700232 parent = dget_parent(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700233 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
234 &entry->d_name, &outarg);
Tejun Heob93f8582008-11-26 12:03:55 +0100235 fuse_request_send(fc, req);
Miklos Szeredie956edd2006-10-17 00:10:12 -0700236 dput(parent);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700237 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800238 fuse_put_request(fc, req);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800239 /* Zero nodeid is same as -ENOENT */
240 if (!err && !outarg.nodeid)
241 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700242 if (!err) {
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200243 fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700244 if (outarg.nodeid != get_node_id(inode)) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100245 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200246 goto invalid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700247 }
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700248 spin_lock(&fc->lock);
Miklos Szeredi1729a162008-11-26 12:03:54 +0100249 fi->nlookup++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700250 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700251 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100252 kfree(forget);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700253 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200254 goto invalid;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700255
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700256 fuse_change_attributes(inode, &outarg.attr,
257 entry_attr_timeout(&outarg),
258 attr_version);
259 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi28420da2013-06-03 14:40:22 +0200260 } else if (inode) {
Miklos Szeredi6314efe2013-10-01 16:41:22 +0200261 fi = get_fuse_inode(inode);
262 if (flags & LOOKUP_RCU) {
263 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
264 return -ECHILD;
265 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
Miklos Szeredi28420da2013-06-03 14:40:22 +0200266 parent = dget_parent(entry);
267 fuse_advise_use_readdirplus(parent->d_inode);
268 dput(parent);
269 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700270 }
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200271 ret = 1;
272out:
273 return ret;
274
275invalid:
276 ret = 0;
Miklos Szeredi3c70b8e2013-10-01 16:41:22 +0200277
278 if (!(flags & LOOKUP_RCU) && check_submounts_and_drop(entry) != 0)
Anand Avati46ea1562013-09-05 11:44:44 +0200279 ret = 1;
Miklos Szeredie2a6b952013-09-05 11:44:43 +0200280 goto out;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700281}
282
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800283static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800284{
285 return !nodeid || nodeid == FUSE_ROOT_ID;
286}
287
Al Viro42695902009-02-20 05:59:13 +0000288const struct dentry_operations fuse_dentry_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700289 .d_revalidate = fuse_dentry_revalidate,
290};
291
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700292int fuse_valid_type(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800293{
294 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
295 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
296}
297
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700298int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
299 struct fuse_entry_out *outarg, struct inode **inode)
300{
301 struct fuse_conn *fc = get_fuse_conn_super(sb);
302 struct fuse_req *req;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100303 struct fuse_forget_link *forget;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700304 u64 attr_version;
305 int err;
306
307 *inode = NULL;
308 err = -ENAMETOOLONG;
309 if (name->len > FUSE_NAME_MAX)
310 goto out;
311
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400312 req = fuse_get_req_nopages(fc);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700313 err = PTR_ERR(req);
314 if (IS_ERR(req))
315 goto out;
316
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100317 forget = fuse_alloc_forget();
318 err = -ENOMEM;
319 if (!forget) {
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700320 fuse_put_request(fc, req);
321 goto out;
322 }
323
324 attr_version = fuse_get_attr_version(fc);
325
326 fuse_lookup_init(fc, req, nodeid, name, outarg);
Tejun Heob93f8582008-11-26 12:03:55 +0100327 fuse_request_send(fc, req);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700328 err = req->out.h.error;
329 fuse_put_request(fc, req);
330 /* Zero nodeid is same as -ENOENT, but with valid timeout */
331 if (err || !outarg->nodeid)
332 goto out_put_forget;
333
334 err = -EIO;
335 if (!outarg->nodeid)
336 goto out_put_forget;
337 if (!fuse_valid_type(outarg->attr.mode))
338 goto out_put_forget;
339
340 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
341 &outarg->attr, entry_attr_timeout(outarg),
342 attr_version);
343 err = -ENOMEM;
344 if (!*inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100345 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700346 goto out;
347 }
348 err = 0;
349
350 out_put_forget:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100351 kfree(forget);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700352 out:
353 return err;
354}
355
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800356static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400357 unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700358{
359 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700360 struct fuse_entry_out outarg;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700361 struct inode *inode;
Miklos Szeredi0de62562008-07-25 01:48:59 -0700362 struct dentry *newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700363 bool outarg_valid = true;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700364
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700365 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
366 &outarg, &inode);
367 if (err == -ENOENT) {
368 outarg_valid = false;
369 err = 0;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800370 }
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700371 if (err)
372 goto out_err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800373
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700374 err = -EIO;
375 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
376 goto out_iput;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700377
Miklos Szeredib70a80e2013-10-01 16:44:54 +0200378 newent = d_materialise_unique(entry, inode);
Miklos Szeredi5835f332013-09-05 11:44:42 +0200379 err = PTR_ERR(newent);
380 if (IS_ERR(newent))
381 goto out_err;
Miklos Szeredid2a85162006-10-17 00:10:11 -0700382
Miklos Szeredi0de62562008-07-25 01:48:59 -0700383 entry = newent ? newent : entry;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700384 if (outarg_valid)
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700385 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800386 else
387 fuse_invalidate_entry_cache(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700388
Feng Shuo4582a4a2013-01-15 11:23:28 +0800389 fuse_advise_use_readdirplus(dir);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700390 return newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700391
392 out_iput:
393 iput(inode);
394 out_err:
395 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700396}
397
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800398/*
399 * Atomic create+open operation
400 *
401 * If the filesystem doesn't support this, then fall back to separate
402 * 'mknod' + 'open' requests.
403 */
Al Virod9585272012-06-22 12:39:14 +0400404static int fuse_create_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400405 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400406 umode_t mode, int *opened)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800407{
408 int err;
409 struct inode *inode;
410 struct fuse_conn *fc = get_fuse_conn(dir);
411 struct fuse_req *req;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100412 struct fuse_forget_link *forget;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200413 struct fuse_create_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800414 struct fuse_open_out outopen;
415 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800416 struct fuse_file *ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800417
Miklos Szerediaf109bc2012-08-15 13:01:24 +0200418 /* Userspace expects S_IFREG in create mode */
419 BUG_ON((mode & S_IFMT) != S_IFREG);
420
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100421 forget = fuse_alloc_forget();
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200422 err = -ENOMEM;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100423 if (!forget)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200424 goto out_err;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700425
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400426 req = fuse_get_req_nopages(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700427 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700428 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700429 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800430
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700431 err = -ENOMEM;
Tejun Heoacf99432008-11-26 12:03:55 +0100432 ff = fuse_file_alloc(fc);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800433 if (!ff)
434 goto out_put_request;
435
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200436 if (!fc->dont_mask)
437 mode &= ~current_umask();
438
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800439 flags &= ~O_NOCTTY;
440 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700441 memset(&outentry, 0, sizeof(outentry));
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800442 inarg.flags = flags;
443 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200444 inarg.umask = current_umask();
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800445 req->in.h.opcode = FUSE_CREATE;
446 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800447 req->in.numargs = 2;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200448 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
449 sizeof(inarg);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800450 req->in.args[0].value = &inarg;
451 req->in.args[1].size = entry->d_name.len + 1;
452 req->in.args[1].value = entry->d_name.name;
453 req->out.numargs = 2;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700454 if (fc->minor < 9)
455 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
456 else
457 req->out.args[0].size = sizeof(outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800458 req->out.args[0].value = &outentry;
459 req->out.args[1].size = sizeof(outopen);
460 req->out.args[1].value = &outopen;
Tejun Heob93f8582008-11-26 12:03:55 +0100461 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800462 err = req->out.h.error;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200463 if (err)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800464 goto out_free_ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800465
466 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800467 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800468 goto out_free_ff;
469
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700470 fuse_put_request(fc, req);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200471 ff->fh = outopen.fh;
472 ff->nodeid = outentry.nodeid;
473 ff->open_flags = outopen.open_flags;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800474 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700475 &outentry.attr, entry_attr_timeout(&outentry), 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800476 if (!inode) {
477 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200478 fuse_sync_release(ff, flags);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100479 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200480 err = -ENOMEM;
481 goto out_err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800482 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100483 kfree(forget);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800484 d_instantiate(entry, inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700485 fuse_change_entry_timeout(entry, &outentry);
Miklos Szeredi0952b2a2008-02-06 01:38:38 -0800486 fuse_invalidate_attr(dir);
Al Viro30d90492012-06-22 12:40:19 +0400487 err = finish_open(file, entry, generic_file_open, opened);
488 if (err) {
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200489 fuse_sync_release(ff, flags);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200490 } else {
491 file->private_data = fuse_file_get(ff);
492 fuse_finish_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800493 }
Al Virod9585272012-06-22 12:39:14 +0400494 return err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800495
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200496out_free_ff:
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800497 fuse_file_free(ff);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200498out_put_request:
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800499 fuse_put_request(fc, req);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200500out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100501 kfree(forget);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200502out_err:
Al Virod9585272012-06-22 12:39:14 +0400503 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200504}
505
506static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
Al Virod9585272012-06-22 12:39:14 +0400507static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400508 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400509 umode_t mode, int *opened)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200510{
511 int err;
512 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200513 struct dentry *res = NULL;
514
515 if (d_unhashed(entry)) {
Al Viro00cd8dd2012-06-10 17:13:09 -0400516 res = fuse_lookup(dir, entry, 0);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200517 if (IS_ERR(res))
Al Virod9585272012-06-22 12:39:14 +0400518 return PTR_ERR(res);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200519
520 if (res)
521 entry = res;
522 }
523
524 if (!(flags & O_CREAT) || entry->d_inode)
525 goto no_open;
526
527 /* Only creates */
Al Viro47237682012-06-10 05:01:45 -0400528 *opened |= FILE_CREATED;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200529
530 if (fc->no_create)
531 goto mknod;
532
Al Viro30d90492012-06-22 12:40:19 +0400533 err = fuse_create_open(dir, entry, file, flags, mode, opened);
Al Virod9585272012-06-22 12:39:14 +0400534 if (err == -ENOSYS) {
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200535 fc->no_create = 1;
536 goto mknod;
537 }
538out_dput:
539 dput(res);
Al Virod9585272012-06-22 12:39:14 +0400540 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200541
542mknod:
543 err = fuse_mknod(dir, entry, mode, 0);
Al Virod9585272012-06-22 12:39:14 +0400544 if (err)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200545 goto out_dput;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200546no_open:
Al Viroe45198a2012-06-10 06:48:09 -0400547 return finish_no_open(file, res);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800548}
549
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800550/*
551 * Code shared between mknod, mkdir, symlink and link
552 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700553static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
554 struct inode *dir, struct dentry *entry,
Al Viro541af6a2011-07-26 03:17:33 -0400555 umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700556{
557 struct fuse_entry_out outarg;
558 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700559 int err;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100560 struct fuse_forget_link *forget;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800561
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100562 forget = fuse_alloc_forget();
563 if (!forget) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800564 fuse_put_request(fc, req);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100565 return -ENOMEM;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800566 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700567
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700568 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700569 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700570 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700571 if (fc->minor < 9)
572 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
573 else
574 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700575 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100576 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700577 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800578 fuse_put_request(fc, req);
579 if (err)
580 goto out_put_forget_req;
581
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800582 err = -EIO;
583 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800584 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800585
586 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800587 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800588
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700589 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700590 &outarg.attr, entry_attr_timeout(&outarg), 0);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700591 if (!inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100592 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700593 return -ENOMEM;
594 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100595 kfree(forget);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700596
Miklos Szeredib70a80e2013-10-01 16:44:54 +0200597 err = d_instantiate_no_diralias(entry, inode);
598 if (err)
599 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700600
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700601 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700602 fuse_invalidate_attr(dir);
603 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800604
Miklos Szeredi2d510132006-11-25 11:09:20 -0800605 out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100606 kfree(forget);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800607 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700608}
609
Al Viro1a67aaf2011-07-26 01:52:52 -0400610static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700611 dev_t rdev)
612{
613 struct fuse_mknod_in inarg;
614 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400615 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700616 if (IS_ERR(req))
617 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700618
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200619 if (!fc->dont_mask)
620 mode &= ~current_umask();
621
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700622 memset(&inarg, 0, sizeof(inarg));
623 inarg.mode = mode;
624 inarg.rdev = new_encode_dev(rdev);
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200625 inarg.umask = current_umask();
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700626 req->in.h.opcode = FUSE_MKNOD;
627 req->in.numargs = 2;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200628 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
629 sizeof(inarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700630 req->in.args[0].value = &inarg;
631 req->in.args[1].size = entry->d_name.len + 1;
632 req->in.args[1].value = entry->d_name.name;
633 return create_new_entry(fc, req, dir, entry, mode);
634}
635
Al Viro4acdaf22011-07-26 01:42:34 -0400636static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400637 bool excl)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700638{
639 return fuse_mknod(dir, entry, mode, 0);
640}
641
Al Viro18bb1db2011-07-26 01:41:39 -0400642static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700643{
644 struct fuse_mkdir_in inarg;
645 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400646 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700647 if (IS_ERR(req))
648 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700649
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200650 if (!fc->dont_mask)
651 mode &= ~current_umask();
652
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700653 memset(&inarg, 0, sizeof(inarg));
654 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200655 inarg.umask = current_umask();
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700656 req->in.h.opcode = FUSE_MKDIR;
657 req->in.numargs = 2;
658 req->in.args[0].size = sizeof(inarg);
659 req->in.args[0].value = &inarg;
660 req->in.args[1].size = entry->d_name.len + 1;
661 req->in.args[1].value = entry->d_name.name;
662 return create_new_entry(fc, req, dir, entry, S_IFDIR);
663}
664
665static int fuse_symlink(struct inode *dir, struct dentry *entry,
666 const char *link)
667{
668 struct fuse_conn *fc = get_fuse_conn(dir);
669 unsigned len = strlen(link) + 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400670 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700671 if (IS_ERR(req))
672 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700673
674 req->in.h.opcode = FUSE_SYMLINK;
675 req->in.numargs = 2;
676 req->in.args[0].size = entry->d_name.len + 1;
677 req->in.args[0].value = entry->d_name.name;
678 req->in.args[1].size = len;
679 req->in.args[1].value = link;
680 return create_new_entry(fc, req, dir, entry, S_IFLNK);
681}
682
Maxim Patlasov31f32672014-04-28 14:19:24 +0200683static inline void fuse_update_ctime(struct inode *inode)
684{
685 if (!IS_NOCMTIME(inode)) {
686 inode->i_ctime = current_fs_time(inode->i_sb);
687 mark_inode_dirty_sync(inode);
688 }
689}
690
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700691static int fuse_unlink(struct inode *dir, struct dentry *entry)
692{
693 int err;
694 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400695 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700696 if (IS_ERR(req))
697 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700698
699 req->in.h.opcode = FUSE_UNLINK;
700 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700701 req->in.numargs = 1;
702 req->in.args[0].size = entry->d_name.len + 1;
703 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100704 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700705 err = req->out.h.error;
706 fuse_put_request(fc, req);
707 if (!err) {
708 struct inode *inode = entry->d_inode;
Miklos Szerediac45d612012-03-05 15:48:11 +0100709 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700710
Miklos Szerediac45d612012-03-05 15:48:11 +0100711 spin_lock(&fc->lock);
712 fi->attr_version = ++fc->attr_version;
Miklos Szeredidfca7ce2013-02-04 15:57:42 +0100713 /*
714 * If i_nlink == 0 then unlink doesn't make sense, yet this can
715 * happen if userspace filesystem is careless. It would be
716 * difficult to enforce correct nlink usage so just ignore this
717 * condition here
718 */
719 if (inode->i_nlink > 0)
720 drop_nlink(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100721 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700722 fuse_invalidate_attr(inode);
723 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800724 fuse_invalidate_entry_cache(entry);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200725 fuse_update_ctime(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700726 } else if (err == -EINTR)
727 fuse_invalidate_entry(entry);
728 return err;
729}
730
731static int fuse_rmdir(struct inode *dir, struct dentry *entry)
732{
733 int err;
734 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400735 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700736 if (IS_ERR(req))
737 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700738
739 req->in.h.opcode = FUSE_RMDIR;
740 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700741 req->in.numargs = 1;
742 req->in.args[0].size = entry->d_name.len + 1;
743 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100744 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700745 err = req->out.h.error;
746 fuse_put_request(fc, req);
747 if (!err) {
Dave Hansence71ec32006-09-30 23:29:06 -0700748 clear_nlink(entry->d_inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700749 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800750 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700751 } else if (err == -EINTR)
752 fuse_invalidate_entry(entry);
753 return err;
754}
755
Miklos Szeredi1560c972014-04-28 16:43:44 +0200756static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
757 struct inode *newdir, struct dentry *newent,
758 unsigned int flags, int opcode, size_t argsize)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700759{
760 int err;
Miklos Szeredi1560c972014-04-28 16:43:44 +0200761 struct fuse_rename2_in inarg;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700762 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredi1560c972014-04-28 16:43:44 +0200763 struct fuse_req *req;
Sage Weile4eaac02011-05-24 13:06:07 -0700764
Miklos Szeredi1560c972014-04-28 16:43:44 +0200765 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700766 if (IS_ERR(req))
767 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700768
Miklos Szeredi1560c972014-04-28 16:43:44 +0200769 memset(&inarg, 0, argsize);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700770 inarg.newdir = get_node_id(newdir);
Miklos Szeredi1560c972014-04-28 16:43:44 +0200771 inarg.flags = flags;
772 req->in.h.opcode = opcode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700773 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700774 req->in.numargs = 3;
Miklos Szeredi1560c972014-04-28 16:43:44 +0200775 req->in.args[0].size = argsize;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700776 req->in.args[0].value = &inarg;
777 req->in.args[1].size = oldent->d_name.len + 1;
778 req->in.args[1].value = oldent->d_name.name;
779 req->in.args[2].size = newent->d_name.len + 1;
780 req->in.args[2].value = newent->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100781 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700782 err = req->out.h.error;
783 fuse_put_request(fc, req);
784 if (!err) {
Miklos Szeredi08b63302007-11-28 16:22:03 -0800785 /* ctime changes */
786 fuse_invalidate_attr(oldent->d_inode);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200787 fuse_update_ctime(oldent->d_inode);
Miklos Szeredi08b63302007-11-28 16:22:03 -0800788
Miklos Szeredi1560c972014-04-28 16:43:44 +0200789 if (flags & RENAME_EXCHANGE) {
790 fuse_invalidate_attr(newent->d_inode);
791 fuse_update_ctime(newent->d_inode);
792 }
793
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700794 fuse_invalidate_attr(olddir);
795 if (olddir != newdir)
796 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800797
798 /* newent will end up negative */
Miklos Szeredi1560c972014-04-28 16:43:44 +0200799 if (!(flags & RENAME_EXCHANGE) && newent->d_inode) {
Miklos Szeredi5219f342009-11-04 10:24:52 +0100800 fuse_invalidate_attr(newent->d_inode);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800801 fuse_invalidate_entry_cache(newent);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200802 fuse_update_ctime(newent->d_inode);
Miklos Szeredi5219f342009-11-04 10:24:52 +0100803 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700804 } else if (err == -EINTR) {
805 /* If request was interrupted, DEITY only knows if the
806 rename actually took place. If the invalidation
807 fails (e.g. some process has CWD under the renamed
808 directory), then there can be inconsistency between
809 the dcache and the real filesystem. Tough luck. */
810 fuse_invalidate_entry(oldent);
811 if (newent->d_inode)
812 fuse_invalidate_entry(newent);
813 }
814
815 return err;
816}
817
Miklos Szeredi1560c972014-04-28 16:43:44 +0200818static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
819 struct inode *newdir, struct dentry *newent,
820 unsigned int flags)
821{
822 struct fuse_conn *fc = get_fuse_conn(olddir);
823 int err;
824
825 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
826 return -EINVAL;
827
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200828 if (flags) {
829 if (fc->no_rename2 || fc->minor < 23)
830 return -EINVAL;
Miklos Szeredi1560c972014-04-28 16:43:44 +0200831
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200832 err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
833 FUSE_RENAME2,
834 sizeof(struct fuse_rename2_in));
835 if (err == -ENOSYS) {
836 fc->no_rename2 = 1;
837 err = -EINVAL;
838 }
839 } else {
840 err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
841 FUSE_RENAME,
842 sizeof(struct fuse_rename_in));
Miklos Szeredi1560c972014-04-28 16:43:44 +0200843 }
Miklos Szeredi1560c972014-04-28 16:43:44 +0200844
Miklos Szeredi4237ba42014-07-10 10:50:19 +0200845 return err;
846}
847
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700848static int fuse_link(struct dentry *entry, struct inode *newdir,
849 struct dentry *newent)
850{
851 int err;
852 struct fuse_link_in inarg;
853 struct inode *inode = entry->d_inode;
854 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400855 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700856 if (IS_ERR(req))
857 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700858
859 memset(&inarg, 0, sizeof(inarg));
860 inarg.oldnodeid = get_node_id(inode);
861 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700862 req->in.numargs = 2;
863 req->in.args[0].size = sizeof(inarg);
864 req->in.args[0].value = &inarg;
865 req->in.args[1].size = newent->d_name.len + 1;
866 req->in.args[1].value = newent->d_name.name;
867 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
868 /* Contrary to "normal" filesystems it can happen that link
869 makes two "logical" inodes point to the same "physical"
870 inode. We invalidate the attributes of the old one, so it
871 will reflect changes in the backing inode (link count,
872 etc.)
873 */
Miklos Szerediac45d612012-03-05 15:48:11 +0100874 if (!err) {
875 struct fuse_inode *fi = get_fuse_inode(inode);
876
877 spin_lock(&fc->lock);
878 fi->attr_version = ++fc->attr_version;
879 inc_nlink(inode);
880 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700881 fuse_invalidate_attr(inode);
Maxim Patlasov31f32672014-04-28 14:19:24 +0200882 fuse_update_ctime(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100883 } else if (err == -EINTR) {
884 fuse_invalidate_attr(inode);
885 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700886 return err;
887}
888
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700889static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
890 struct kstat *stat)
891{
Miklos Szeredi203627b2012-05-10 19:49:38 +0400892 unsigned int blkbits;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400893 struct fuse_conn *fc = get_fuse_conn(inode);
894
895 /* see the comment in fuse_change_attributes() */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400896 if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
Pavel Emelyanov83732002013-10-10 17:10:46 +0400897 attr->size = i_size_read(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400898 attr->mtime = inode->i_mtime.tv_sec;
899 attr->mtimensec = inode->i_mtime.tv_nsec;
Maxim Patlasov31f32672014-04-28 14:19:24 +0200900 attr->ctime = inode->i_ctime.tv_sec;
901 attr->ctimensec = inode->i_ctime.tv_nsec;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400902 }
Miklos Szeredi203627b2012-05-10 19:49:38 +0400903
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700904 stat->dev = inode->i_sb->s_dev;
905 stat->ino = attr->ino;
906 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
907 stat->nlink = attr->nlink;
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800908 stat->uid = make_kuid(&init_user_ns, attr->uid);
909 stat->gid = make_kgid(&init_user_ns, attr->gid);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700910 stat->rdev = inode->i_rdev;
911 stat->atime.tv_sec = attr->atime;
912 stat->atime.tv_nsec = attr->atimensec;
913 stat->mtime.tv_sec = attr->mtime;
914 stat->mtime.tv_nsec = attr->mtimensec;
915 stat->ctime.tv_sec = attr->ctime;
916 stat->ctime.tv_nsec = attr->ctimensec;
917 stat->size = attr->size;
918 stat->blocks = attr->blocks;
Miklos Szeredi203627b2012-05-10 19:49:38 +0400919
920 if (attr->blksize != 0)
921 blkbits = ilog2(attr->blksize);
922 else
923 blkbits = inode->i_sb->s_blocksize_bits;
924
925 stat->blksize = 1 << blkbits;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700926}
927
Miklos Szeredic79e3222007-10-18 03:06:59 -0700928static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
929 struct file *file)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700930{
931 int err;
Miklos Szeredic79e3222007-10-18 03:06:59 -0700932 struct fuse_getattr_in inarg;
933 struct fuse_attr_out outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700934 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700935 struct fuse_req *req;
936 u64 attr_version;
937
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400938 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700939 if (IS_ERR(req))
940 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700941
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800942 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700943
Miklos Szeredic79e3222007-10-18 03:06:59 -0700944 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700945 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredic79e3222007-10-18 03:06:59 -0700946 /* Directories have separate file-handle space */
947 if (file && S_ISREG(inode->i_mode)) {
948 struct fuse_file *ff = file->private_data;
949
950 inarg.getattr_flags |= FUSE_GETATTR_FH;
951 inarg.fh = ff->fh;
952 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700953 req->in.h.opcode = FUSE_GETATTR;
954 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700955 req->in.numargs = 1;
956 req->in.args[0].size = sizeof(inarg);
957 req->in.args[0].value = &inarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700958 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700959 if (fc->minor < 9)
960 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
961 else
962 req->out.args[0].size = sizeof(outarg);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700963 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100964 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700965 err = req->out.h.error;
966 fuse_put_request(fc, req);
967 if (!err) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700968 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700969 make_bad_inode(inode);
970 err = -EIO;
971 } else {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700972 fuse_change_attributes(inode, &outarg.attr,
973 attr_timeout(&outarg),
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700974 attr_version);
975 if (stat)
Miklos Szeredic79e3222007-10-18 03:06:59 -0700976 fuse_fillattr(inode, &outarg.attr, stat);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700977 }
978 }
979 return err;
980}
981
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800982int fuse_update_attributes(struct inode *inode, struct kstat *stat,
983 struct file *file, bool *refreshed)
984{
985 struct fuse_inode *fi = get_fuse_inode(inode);
986 int err;
987 bool r;
988
Miklos Szeredi126b9d42014-07-07 15:28:50 +0200989 if (time_before64(fi->i_time, get_jiffies_64())) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800990 r = true;
991 err = fuse_do_getattr(inode, stat, file);
992 } else {
993 r = false;
994 err = 0;
995 if (stat) {
996 generic_fillattr(inode, stat);
997 stat->mode = fi->orig_i_mode;
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +0400998 stat->ino = fi->orig_ino;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800999 }
1000 }
1001
1002 if (refreshed != NULL)
1003 *refreshed = r;
1004
1005 return err;
1006}
1007
John Muir3b463ae2009-05-31 11:13:57 -04001008int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
John Muir451d0f52011-12-06 21:50:06 +01001009 u64 child_nodeid, struct qstr *name)
John Muir3b463ae2009-05-31 11:13:57 -04001010{
1011 int err = -ENOTDIR;
1012 struct inode *parent;
1013 struct dentry *dir;
1014 struct dentry *entry;
1015
1016 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
1017 if (!parent)
1018 return -ENOENT;
1019
1020 mutex_lock(&parent->i_mutex);
1021 if (!S_ISDIR(parent->i_mode))
1022 goto unlock;
1023
1024 err = -ENOENT;
1025 dir = d_find_alias(parent);
1026 if (!dir)
1027 goto unlock;
1028
1029 entry = d_lookup(dir, name);
1030 dput(dir);
1031 if (!entry)
1032 goto unlock;
1033
1034 fuse_invalidate_attr(parent);
1035 fuse_invalidate_entry(entry);
John Muir451d0f52011-12-06 21:50:06 +01001036
1037 if (child_nodeid != 0 && entry->d_inode) {
1038 mutex_lock(&entry->d_inode->i_mutex);
1039 if (get_node_id(entry->d_inode) != child_nodeid) {
1040 err = -ENOENT;
1041 goto badentry;
1042 }
1043 if (d_mountpoint(entry)) {
1044 err = -EBUSY;
1045 goto badentry;
1046 }
1047 if (S_ISDIR(entry->d_inode->i_mode)) {
1048 shrink_dcache_parent(entry);
1049 if (!simple_empty(entry)) {
1050 err = -ENOTEMPTY;
1051 goto badentry;
1052 }
1053 entry->d_inode->i_flags |= S_DEAD;
1054 }
1055 dont_mount(entry);
1056 clear_nlink(entry->d_inode);
1057 err = 0;
1058 badentry:
1059 mutex_unlock(&entry->d_inode->i_mutex);
1060 if (!err)
1061 d_delete(entry);
1062 } else {
1063 err = 0;
1064 }
John Muir3b463ae2009-05-31 11:13:57 -04001065 dput(entry);
John Muir3b463ae2009-05-31 11:13:57 -04001066
1067 unlock:
1068 mutex_unlock(&parent->i_mutex);
1069 iput(parent);
1070 return err;
1071}
1072
Miklos Szeredi87729a52005-09-09 13:10:34 -07001073/*
1074 * Calling into a user-controlled filesystem gives the filesystem
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001075 * daemon ptrace-like capabilities over the current process. This
Miklos Szeredi87729a52005-09-09 13:10:34 -07001076 * means, that the filesystem daemon is able to record the exact
1077 * filesystem operations performed, and can also control the behavior
1078 * of the requester process in otherwise impossible ways. For example
1079 * it can delay the operation for arbitrary length of time allowing
1080 * DoS against the requester.
1081 *
1082 * For this reason only those processes can call into the filesystem,
1083 * for which the owner of the mount has ptrace privilege. This
1084 * excludes processes started by other users, suid or sgid processes.
1085 */
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001086int fuse_allow_current_process(struct fuse_conn *fc)
Miklos Szeredi87729a52005-09-09 13:10:34 -07001087{
David Howellsc69e8d92008-11-14 10:39:19 +11001088 const struct cred *cred;
David Howellsc69e8d92008-11-14 10:39:19 +11001089
Miklos Szeredi87729a52005-09-09 13:10:34 -07001090 if (fc->flags & FUSE_ALLOW_OTHER)
1091 return 1;
1092
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001093 cred = current_cred();
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001094 if (uid_eq(cred->euid, fc->user_id) &&
1095 uid_eq(cred->suid, fc->user_id) &&
1096 uid_eq(cred->uid, fc->user_id) &&
1097 gid_eq(cred->egid, fc->group_id) &&
1098 gid_eq(cred->sgid, fc->group_id) &&
1099 gid_eq(cred->gid, fc->group_id))
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001100 return 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001101
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001102 return 0;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001103}
1104
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001105static int fuse_access(struct inode *inode, int mask)
1106{
1107 struct fuse_conn *fc = get_fuse_conn(inode);
1108 struct fuse_req *req;
1109 struct fuse_access_in inarg;
1110 int err;
1111
Miklos Szeredi698fa1d2013-10-01 16:41:23 +02001112 BUG_ON(mask & MAY_NOT_BLOCK);
1113
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001114 if (fc->no_access)
1115 return 0;
1116
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001117 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001118 if (IS_ERR(req))
1119 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001120
1121 memset(&inarg, 0, sizeof(inarg));
Al Viroe6305c42008-07-15 21:03:57 -04001122 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001123 req->in.h.opcode = FUSE_ACCESS;
1124 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001125 req->in.numargs = 1;
1126 req->in.args[0].size = sizeof(inarg);
1127 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001128 fuse_request_send(fc, req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001129 err = req->out.h.error;
1130 fuse_put_request(fc, req);
1131 if (err == -ENOSYS) {
1132 fc->no_access = 1;
1133 err = 0;
1134 }
1135 return err;
1136}
1137
Al Viro10556cb2011-06-20 19:28:19 -04001138static int fuse_perm_getattr(struct inode *inode, int mask)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001139{
Al Viro10556cb2011-06-20 19:28:19 -04001140 if (mask & MAY_NOT_BLOCK)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001141 return -ECHILD;
1142
1143 return fuse_do_getattr(inode, NULL, NULL);
1144}
1145
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001146/*
1147 * Check permission. The two basic access models of FUSE are:
1148 *
1149 * 1) Local access checking ('default_permissions' mount option) based
1150 * on file mode. This is the plain old disk filesystem permission
1151 * modell.
1152 *
1153 * 2) "Remote" access checking, where server is responsible for
1154 * checking permission in each inode operation. An exception to this
1155 * is if ->permission() was invoked from sys_access() in which case an
1156 * access request is sent. Execute permission is still checked
1157 * locally based on file mode.
1158 */
Al Viro10556cb2011-06-20 19:28:19 -04001159static int fuse_permission(struct inode *inode, int mask)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001160{
1161 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001162 bool refreshed = false;
1163 int err = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001164
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001165 if (!fuse_allow_current_process(fc))
Miklos Szeredie5e55582005-09-09 13:10:28 -07001166 return -EACCES;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001167
1168 /*
Miklos Szeredie8e96152007-10-16 23:31:06 -07001169 * If attributes are needed, refresh them before proceeding
Miklos Szeredi244f6382007-10-16 23:31:02 -07001170 */
Miklos Szeredie8e96152007-10-16 23:31:06 -07001171 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1172 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001173 struct fuse_inode *fi = get_fuse_inode(inode);
1174
Miklos Szeredi126b9d42014-07-07 15:28:50 +02001175 if (time_before64(fi->i_time, get_jiffies_64())) {
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001176 refreshed = true;
1177
Al Viro10556cb2011-06-20 19:28:19 -04001178 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001179 if (err)
1180 return err;
1181 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001182 }
1183
1184 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
Al Viro2830ba72011-06-20 19:16:29 -04001185 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001186
1187 /* If permission is denied, try to refresh file
1188 attributes. This is also needed, because the root
1189 node will at first have no permissions */
Miklos Szeredi244f6382007-10-16 23:31:02 -07001190 if (err == -EACCES && !refreshed) {
Al Viro10556cb2011-06-20 19:28:19 -04001191 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001192 if (!err)
Al Viro2830ba72011-06-20 19:16:29 -04001193 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001194 }
1195
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001196 /* Note: the opposite of the above test does not
1197 exist. So if permissions are revoked this won't be
1198 noticed immediately, only after the attribute
1199 timeout has expired */
Eric Paris9cfcac82010-07-23 11:43:51 -04001200 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
Miklos Szeredie8e96152007-10-16 23:31:06 -07001201 err = fuse_access(inode, mask);
1202 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1203 if (!(inode->i_mode & S_IXUGO)) {
1204 if (refreshed)
1205 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001206
Al Viro10556cb2011-06-20 19:28:19 -04001207 err = fuse_perm_getattr(inode, mask);
Miklos Szeredie8e96152007-10-16 23:31:06 -07001208 if (!err && !(inode->i_mode & S_IXUGO))
1209 return -EACCES;
1210 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001211 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001212 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001213}
1214
1215static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001216 struct dir_context *ctx)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001217{
1218 while (nbytes >= FUSE_NAME_OFFSET) {
1219 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1220 size_t reclen = FUSE_DIRENT_SIZE(dirent);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001221 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1222 return -EIO;
1223 if (reclen > nbytes)
1224 break;
Miklos Szerediefeb9e62013-09-03 14:28:38 +02001225 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1226 return -EIO;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001227
Al Viro8d3af7f2013-05-18 03:03:58 -04001228 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1229 dirent->ino, dirent->type))
Miklos Szeredie5e55582005-09-09 13:10:28 -07001230 break;
1231
1232 buf += reclen;
1233 nbytes -= reclen;
Al Viro8d3af7f2013-05-18 03:03:58 -04001234 ctx->pos = dirent->off;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001235 }
1236
1237 return 0;
1238}
1239
Anand V. Avati0b05b182012-08-19 08:53:23 -04001240static int fuse_direntplus_link(struct file *file,
1241 struct fuse_direntplus *direntplus,
1242 u64 attr_version)
1243{
1244 int err;
1245 struct fuse_entry_out *o = &direntplus->entry_out;
1246 struct fuse_dirent *dirent = &direntplus->dirent;
1247 struct dentry *parent = file->f_path.dentry;
1248 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1249 struct dentry *dentry;
1250 struct dentry *alias;
1251 struct inode *dir = parent->d_inode;
1252 struct fuse_conn *fc;
1253 struct inode *inode;
1254
1255 if (!o->nodeid) {
1256 /*
1257 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1258 * ENOENT. Instead, it only means the userspace filesystem did
1259 * not want to return attributes/handle for this entry.
1260 *
1261 * So do nothing.
1262 */
1263 return 0;
1264 }
1265
1266 if (name.name[0] == '.') {
1267 /*
1268 * We could potentially refresh the attributes of the directory
1269 * and its parent?
1270 */
1271 if (name.len == 1)
1272 return 0;
1273 if (name.name[1] == '.' && name.len == 2)
1274 return 0;
1275 }
Miklos Szeredia28ef452013-07-17 14:53:53 +02001276
1277 if (invalid_nodeid(o->nodeid))
1278 return -EIO;
1279 if (!fuse_valid_type(o->attr.mode))
1280 return -EIO;
1281
Anand V. Avati0b05b182012-08-19 08:53:23 -04001282 fc = get_fuse_conn(dir);
1283
1284 name.hash = full_name_hash(name.name, name.len);
1285 dentry = d_lookup(parent, &name);
Niels de Vos53ce9a32013-07-17 14:53:53 +02001286 if (dentry) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001287 inode = dentry->d_inode;
Niels de Vos53ce9a32013-07-17 14:53:53 +02001288 if (!inode) {
1289 d_drop(dentry);
Miklos Szeredia28ef452013-07-17 14:53:53 +02001290 } else if (get_node_id(inode) != o->nodeid ||
1291 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
Niels de Vos53ce9a32013-07-17 14:53:53 +02001292 err = d_invalidate(dentry);
1293 if (err)
1294 goto out;
Miklos Szeredia28ef452013-07-17 14:53:53 +02001295 } else if (is_bad_inode(inode)) {
1296 err = -EIO;
1297 goto out;
Niels de Vos53ce9a32013-07-17 14:53:53 +02001298 } else {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001299 struct fuse_inode *fi;
1300 fi = get_fuse_inode(inode);
1301 spin_lock(&fc->lock);
1302 fi->nlookup++;
1303 spin_unlock(&fc->lock);
1304
Miklos Szeredifa2b7212013-07-17 14:53:53 +02001305 fuse_change_attributes(inode, &o->attr,
1306 entry_attr_timeout(o),
1307 attr_version);
1308
Anand V. Avati0b05b182012-08-19 08:53:23 -04001309 /*
1310 * The other branch to 'found' comes via fuse_iget()
1311 * which bumps nlookup inside
1312 */
1313 goto found;
1314 }
Anand V. Avati0b05b182012-08-19 08:53:23 -04001315 dput(dentry);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001316 }
1317
1318 dentry = d_alloc(parent, &name);
1319 err = -ENOMEM;
1320 if (!dentry)
1321 goto out;
1322
1323 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1324 &o->attr, entry_attr_timeout(o), attr_version);
1325 if (!inode)
1326 goto out;
1327
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001328 alias = d_materialise_unique(dentry, inode);
Miklos Szeredi5835f332013-09-05 11:44:42 +02001329 err = PTR_ERR(alias);
1330 if (IS_ERR(alias))
1331 goto out;
Miklos Szeredi29149412013-07-17 14:53:53 +02001332
Anand V. Avati0b05b182012-08-19 08:53:23 -04001333 if (alias) {
1334 dput(dentry);
1335 dentry = alias;
1336 }
1337
1338found:
Miklos Szeredi6314efe2013-10-01 16:41:22 +02001339 if (fc->readdirplus_auto)
1340 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001341 fuse_change_entry_timeout(dentry, o);
1342
1343 err = 0;
1344out:
Miklos Szeredic7263bc2013-07-17 14:53:54 +02001345 dput(dentry);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001346 return err;
1347}
1348
1349static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001350 struct dir_context *ctx, u64 attr_version)
Anand V. Avati0b05b182012-08-19 08:53:23 -04001351{
1352 struct fuse_direntplus *direntplus;
1353 struct fuse_dirent *dirent;
1354 size_t reclen;
1355 int over = 0;
1356 int ret;
1357
1358 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1359 direntplus = (struct fuse_direntplus *) buf;
1360 dirent = &direntplus->dirent;
1361 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1362
1363 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1364 return -EIO;
1365 if (reclen > nbytes)
1366 break;
Miklos Szerediefeb9e62013-09-03 14:28:38 +02001367 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1368 return -EIO;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001369
1370 if (!over) {
1371 /* We fill entries into dstbuf only as much as
1372 it can hold. But we still continue iterating
1373 over remaining entries to link them. If not,
1374 we need to send a FORGET for each of those
1375 which we did not link.
1376 */
Al Viro8d3af7f2013-05-18 03:03:58 -04001377 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1378 dirent->ino, dirent->type);
1379 ctx->pos = dirent->off;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001380 }
1381
1382 buf += reclen;
1383 nbytes -= reclen;
1384
1385 ret = fuse_direntplus_link(file, direntplus, attr_version);
1386 if (ret)
1387 fuse_force_forget(file, direntplus->entry_out.nodeid);
1388 }
1389
1390 return 0;
1391}
1392
Al Viro8d3af7f2013-05-18 03:03:58 -04001393static int fuse_readdir(struct file *file, struct dir_context *ctx)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001394{
Feng Shuo4582a4a2013-01-15 11:23:28 +08001395 int plus, err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001396 size_t nbytes;
1397 struct page *page;
Al Viro496ad9a2013-01-23 17:07:38 -05001398 struct inode *inode = file_inode(file);
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001399 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001400 struct fuse_req *req;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001401 u64 attr_version = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001402
1403 if (is_bad_inode(inode))
1404 return -EIO;
1405
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001406 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001407 if (IS_ERR(req))
1408 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001409
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001410 page = alloc_page(GFP_KERNEL);
1411 if (!page) {
1412 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001413 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001414 }
Feng Shuo4582a4a2013-01-15 11:23:28 +08001415
Al Viro8d3af7f2013-05-18 03:03:58 -04001416 plus = fuse_use_readdirplus(inode, ctx);
Miklos Szeredif4975c62009-04-02 14:25:34 +02001417 req->out.argpages = 1;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001418 req->num_pages = 1;
1419 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001420 req->page_descs[0].length = PAGE_SIZE;
Feng Shuo4582a4a2013-01-15 11:23:28 +08001421 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001422 attr_version = fuse_get_attr_version(fc);
Al Viro8d3af7f2013-05-18 03:03:58 -04001423 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001424 FUSE_READDIRPLUS);
1425 } else {
Al Viro8d3af7f2013-05-18 03:03:58 -04001426 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001427 FUSE_READDIR);
1428 }
Tejun Heob93f8582008-11-26 12:03:55 +01001429 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -08001430 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001431 err = req->out.h.error;
1432 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001433 if (!err) {
Feng Shuo4582a4a2013-01-15 11:23:28 +08001434 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001435 err = parse_dirplusfile(page_address(page), nbytes,
Al Viro8d3af7f2013-05-18 03:03:58 -04001436 file, ctx,
Anand V. Avati0b05b182012-08-19 08:53:23 -04001437 attr_version);
1438 } else {
1439 err = parse_dirfile(page_address(page), nbytes, file,
Al Viro8d3af7f2013-05-18 03:03:58 -04001440 ctx);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001441 }
1442 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001443
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001444 __free_page(page);
Andrew Gallagher451418f2013-11-05 03:55:43 -08001445 fuse_invalidate_atime(inode);
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001446 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001447}
1448
1449static char *read_link(struct dentry *dentry)
1450{
1451 struct inode *inode = dentry->d_inode;
1452 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001453 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001454 char *link;
1455
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001456 if (IS_ERR(req))
David Howellse231c2e2008-02-07 00:15:26 -08001457 return ERR_CAST(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001458
1459 link = (char *) __get_free_page(GFP_KERNEL);
1460 if (!link) {
1461 link = ERR_PTR(-ENOMEM);
1462 goto out;
1463 }
1464 req->in.h.opcode = FUSE_READLINK;
1465 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001466 req->out.argvar = 1;
1467 req->out.numargs = 1;
1468 req->out.args[0].size = PAGE_SIZE - 1;
1469 req->out.args[0].value = link;
Tejun Heob93f8582008-11-26 12:03:55 +01001470 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001471 if (req->out.h.error) {
1472 free_page((unsigned long) link);
1473 link = ERR_PTR(req->out.h.error);
1474 } else
1475 link[req->out.args[0].size] = '\0';
1476 out:
1477 fuse_put_request(fc, req);
Andrew Gallagher451418f2013-11-05 03:55:43 -08001478 fuse_invalidate_atime(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001479 return link;
1480}
1481
1482static void free_link(char *link)
1483{
1484 if (!IS_ERR(link))
1485 free_page((unsigned long) link);
1486}
1487
1488static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1489{
1490 nd_set_link(nd, read_link(dentry));
1491 return NULL;
1492}
1493
1494static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1495{
1496 free_link(nd_get_link(nd));
1497}
1498
1499static int fuse_dir_open(struct inode *inode, struct file *file)
1500{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +02001501 return fuse_open_common(inode, file, true);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001502}
1503
1504static int fuse_dir_release(struct inode *inode, struct file *file)
1505{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +02001506 fuse_release_common(file, FUSE_RELEASEDIR);
1507
1508 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001509}
1510
Josef Bacik02c24a82011-07-16 20:44:56 -04001511static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1512 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -07001513{
Josef Bacik02c24a82011-07-16 20:44:56 -04001514 return fuse_fsync_common(file, start, end, datasync, 1);
Miklos Szeredi82547982005-09-09 13:10:38 -07001515}
1516
Miklos Szeredib18da0c2011-12-13 11:58:49 +01001517static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1518 unsigned long arg)
1519{
1520 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1521
1522 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1523 if (fc->minor < 18)
1524 return -ENOTTY;
1525
1526 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1527}
1528
1529static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1530 unsigned long arg)
1531{
1532 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1533
1534 if (fc->minor < 18)
1535 return -ENOTTY;
1536
1537 return fuse_ioctl_common(file, cmd, arg,
1538 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1539}
1540
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001541static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001542{
1543 /* Always update if mtime is explicitly set */
1544 if (ivalid & ATTR_MTIME_SET)
1545 return true;
1546
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001547 /* Or if kernel i_mtime is the official one */
1548 if (trust_local_mtime)
1549 return true;
1550
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001551 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1552 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1553 return false;
1554
1555 /* In all other cases update */
1556 return true;
1557}
1558
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001559static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001560 bool trust_local_cmtime)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001561{
1562 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001563
1564 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001565 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001566 if (ivalid & ATTR_UID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001567 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001568 if (ivalid & ATTR_GID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001569 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001570 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001571 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001572 if (ivalid & ATTR_ATIME) {
1573 arg->valid |= FATTR_ATIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001574 arg->atime = iattr->ia_atime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001575 arg->atimensec = iattr->ia_atime.tv_nsec;
1576 if (!(ivalid & ATTR_ATIME_SET))
1577 arg->valid |= FATTR_ATIME_NOW;
1578 }
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001579 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001580 arg->valid |= FATTR_MTIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001581 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001582 arg->mtimensec = iattr->ia_mtime.tv_nsec;
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001583 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001584 arg->valid |= FATTR_MTIME_NOW;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001585 }
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001586 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1587 arg->valid |= FATTR_CTIME;
1588 arg->ctime = iattr->ia_ctime.tv_sec;
1589 arg->ctimensec = iattr->ia_ctime.tv_nsec;
1590 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001591}
1592
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001593/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001594 * Prevent concurrent writepages on inode
1595 *
1596 * This is done by adding a negative bias to the inode write counter
1597 * and waiting for all pending writes to finish.
1598 */
1599void fuse_set_nowrite(struct inode *inode)
1600{
1601 struct fuse_conn *fc = get_fuse_conn(inode);
1602 struct fuse_inode *fi = get_fuse_inode(inode);
1603
1604 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1605
1606 spin_lock(&fc->lock);
1607 BUG_ON(fi->writectr < 0);
1608 fi->writectr += FUSE_NOWRITE;
1609 spin_unlock(&fc->lock);
1610 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1611}
1612
1613/*
1614 * Allow writepages on inode
1615 *
1616 * Remove the bias from the writecounter and send any queued
1617 * writepages.
1618 */
1619static void __fuse_release_nowrite(struct inode *inode)
1620{
1621 struct fuse_inode *fi = get_fuse_inode(inode);
1622
1623 BUG_ON(fi->writectr != FUSE_NOWRITE);
1624 fi->writectr = 0;
1625 fuse_flush_writepages(inode);
1626}
1627
1628void fuse_release_nowrite(struct inode *inode)
1629{
1630 struct fuse_conn *fc = get_fuse_conn(inode);
1631
1632 spin_lock(&fc->lock);
1633 __fuse_release_nowrite(inode);
1634 spin_unlock(&fc->lock);
1635}
1636
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001637static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_req *req,
1638 struct inode *inode,
1639 struct fuse_setattr_in *inarg_p,
1640 struct fuse_attr_out *outarg_p)
1641{
1642 req->in.h.opcode = FUSE_SETATTR;
1643 req->in.h.nodeid = get_node_id(inode);
1644 req->in.numargs = 1;
1645 req->in.args[0].size = sizeof(*inarg_p);
1646 req->in.args[0].value = inarg_p;
1647 req->out.numargs = 1;
1648 if (fc->minor < 9)
1649 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1650 else
1651 req->out.args[0].size = sizeof(*outarg_p);
1652 req->out.args[0].value = outarg_p;
1653}
1654
1655/*
1656 * Flush inode->i_mtime to the server
1657 */
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001658int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001659{
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001660 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001661 struct fuse_req *req;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001662 struct fuse_setattr_in inarg;
1663 struct fuse_attr_out outarg;
1664 int err;
1665
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001666 req = fuse_get_req_nopages(fc);
1667 if (IS_ERR(req))
1668 return PTR_ERR(req);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001669
1670 memset(&inarg, 0, sizeof(inarg));
1671 memset(&outarg, 0, sizeof(outarg));
1672
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001673 inarg.valid = FATTR_MTIME;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001674 inarg.mtime = inode->i_mtime.tv_sec;
1675 inarg.mtimensec = inode->i_mtime.tv_nsec;
Maxim Patlasovab9e13f2014-04-28 14:19:24 +02001676 if (fc->minor >= 23) {
1677 inarg.valid |= FATTR_CTIME;
1678 inarg.ctime = inode->i_ctime.tv_sec;
1679 inarg.ctimensec = inode->i_ctime.tv_nsec;
1680 }
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001681 if (ff) {
1682 inarg.valid |= FATTR_FH;
1683 inarg.fh = ff->fh;
1684 }
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001685 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1686 fuse_request_send(fc, req);
1687 err = req->out.h.error;
1688 fuse_put_request(fc, req);
1689
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001690 return err;
1691}
1692
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001693/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001694 * Set attributes, and at the same time refresh them.
1695 *
1696 * Truncation is slightly complicated, because the 'truncate' request
1697 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -07001698 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1699 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001700 */
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001701int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1702 struct file *file)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001703{
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001704 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001705 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001706 struct fuse_req *req;
1707 struct fuse_setattr_in inarg;
1708 struct fuse_attr_out outarg;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001709 bool is_truncate = false;
Pavel Emelyanov83732002013-10-10 17:10:46 +04001710 bool is_wb = fc->writeback_cache;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001711 loff_t oldsize;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001712 int err;
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001713 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001714
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001715 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1716 attr->ia_valid |= ATTR_FORCE;
1717
1718 err = inode_change_ok(inode, attr);
1719 if (err)
1720 return err;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001721
Miklos Szeredi8d56add2011-02-25 14:44:58 +01001722 if (attr->ia_valid & ATTR_OPEN) {
1723 if (fc->atomic_o_trunc)
1724 return 0;
1725 file = NULL;
1726 }
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001727
Christoph Hellwig2c27c652010-06-04 11:30:04 +02001728 if (attr->ia_valid & ATTR_SIZE)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001729 is_truncate = true;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001730
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001731 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001732 if (IS_ERR(req))
1733 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001734
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001735 if (is_truncate) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001736 fuse_set_nowrite(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001737 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001738 if (trust_local_cmtime && attr->ia_size != inode->i_size)
1739 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001740 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001741
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001742 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001743 memset(&outarg, 0, sizeof(outarg));
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001744 iattr_to_fattr(attr, &inarg, trust_local_cmtime);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001745 if (file) {
1746 struct fuse_file *ff = file->private_data;
1747 inarg.valid |= FATTR_FH;
1748 inarg.fh = ff->fh;
1749 }
Miklos Szeredif3332112007-10-18 03:07:04 -07001750 if (attr->ia_valid & ATTR_SIZE) {
1751 /* For mandatory locking in truncate */
1752 inarg.valid |= FATTR_LOCKOWNER;
1753 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1754 }
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001755 fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
Tejun Heob93f8582008-11-26 12:03:55 +01001756 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001757 err = req->out.h.error;
1758 fuse_put_request(fc, req);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001759 if (err) {
1760 if (err == -EINTR)
1761 fuse_invalidate_attr(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001762 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001763 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001764
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001765 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1766 make_bad_inode(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001767 err = -EIO;
1768 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001769 }
1770
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001771 spin_lock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001772 /* the kernel maintains i_mtime locally */
Maxim Patlasov3ad22c62014-04-28 14:19:25 +02001773 if (trust_local_cmtime) {
1774 if (attr->ia_valid & ATTR_MTIME)
1775 inode->i_mtime = attr->ia_mtime;
1776 if (attr->ia_valid & ATTR_CTIME)
1777 inode->i_ctime = attr->ia_ctime;
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001778 /* FIXME: clear I_DIRTY_SYNC? */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001779 }
1780
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001781 fuse_change_attributes_common(inode, &outarg.attr,
1782 attr_timeout(&outarg));
1783 oldsize = inode->i_size;
Pavel Emelyanov83732002013-10-10 17:10:46 +04001784 /* see the comment in fuse_change_attributes() */
1785 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1786 i_size_write(inode, outarg.attr.size);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001787
1788 if (is_truncate) {
1789 /* NOTE: this may release/reacquire fc->lock */
1790 __fuse_release_nowrite(inode);
1791 }
1792 spin_unlock(&fc->lock);
1793
1794 /*
1795 * Only call invalidate_inode_pages2() after removing
1796 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1797 */
Pavel Emelyanov83732002013-10-10 17:10:46 +04001798 if ((is_truncate || !is_wb) &&
1799 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001800 truncate_pagecache(inode, outarg.attr.size);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001801 invalidate_inode_pages2(inode->i_mapping);
1802 }
1803
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001804 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001805 return 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001806
1807error:
1808 if (is_truncate)
1809 fuse_release_nowrite(inode);
1810
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001811 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001812 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001813}
1814
Miklos Szeredi49d49142007-10-18 03:07:00 -07001815static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1816{
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001817 struct inode *inode = entry->d_inode;
1818
1819 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1820 return -EACCES;
1821
Miklos Szeredi49d49142007-10-18 03:07:00 -07001822 if (attr->ia_valid & ATTR_FILE)
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001823 return fuse_do_setattr(inode, attr, attr->ia_file);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001824 else
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04001825 return fuse_do_setattr(inode, attr, NULL);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001826}
1827
Miklos Szeredie5e55582005-09-09 13:10:28 -07001828static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1829 struct kstat *stat)
1830{
1831 struct inode *inode = entry->d_inode;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001832 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001833
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001834 if (!fuse_allow_current_process(fc))
Miklos Szeredi244f6382007-10-16 23:31:02 -07001835 return -EACCES;
1836
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001837 return fuse_update_attributes(inode, stat, NULL, NULL);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001838}
1839
Miklos Szeredi92a87802005-09-09 13:10:31 -07001840static int fuse_setxattr(struct dentry *entry, const char *name,
1841 const void *value, size_t size, int flags)
1842{
1843 struct inode *inode = entry->d_inode;
1844 struct fuse_conn *fc = get_fuse_conn(inode);
1845 struct fuse_req *req;
1846 struct fuse_setxattr_in inarg;
1847 int err;
1848
Miklos Szeredi92a87802005-09-09 13:10:31 -07001849 if (fc->no_setxattr)
1850 return -EOPNOTSUPP;
1851
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001852 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001853 if (IS_ERR(req))
1854 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001855
1856 memset(&inarg, 0, sizeof(inarg));
1857 inarg.size = size;
1858 inarg.flags = flags;
1859 req->in.h.opcode = FUSE_SETXATTR;
1860 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001861 req->in.numargs = 3;
1862 req->in.args[0].size = sizeof(inarg);
1863 req->in.args[0].value = &inarg;
1864 req->in.args[1].size = strlen(name) + 1;
1865 req->in.args[1].value = name;
1866 req->in.args[2].size = size;
1867 req->in.args[2].value = value;
Tejun Heob93f8582008-11-26 12:03:55 +01001868 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001869 err = req->out.h.error;
1870 fuse_put_request(fc, req);
1871 if (err == -ENOSYS) {
1872 fc->no_setxattr = 1;
1873 err = -EOPNOTSUPP;
1874 }
Maxim Patlasov31f32672014-04-28 14:19:24 +02001875 if (!err) {
Anand Avatid331a412013-08-20 02:21:07 -04001876 fuse_invalidate_attr(inode);
Maxim Patlasov31f32672014-04-28 14:19:24 +02001877 fuse_update_ctime(inode);
1878 }
Miklos Szeredi92a87802005-09-09 13:10:31 -07001879 return err;
1880}
1881
1882static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1883 void *value, size_t size)
1884{
1885 struct inode *inode = entry->d_inode;
1886 struct fuse_conn *fc = get_fuse_conn(inode);
1887 struct fuse_req *req;
1888 struct fuse_getxattr_in inarg;
1889 struct fuse_getxattr_out outarg;
1890 ssize_t ret;
1891
1892 if (fc->no_getxattr)
1893 return -EOPNOTSUPP;
1894
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001895 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001896 if (IS_ERR(req))
1897 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001898
1899 memset(&inarg, 0, sizeof(inarg));
1900 inarg.size = size;
1901 req->in.h.opcode = FUSE_GETXATTR;
1902 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001903 req->in.numargs = 2;
1904 req->in.args[0].size = sizeof(inarg);
1905 req->in.args[0].value = &inarg;
1906 req->in.args[1].size = strlen(name) + 1;
1907 req->in.args[1].value = name;
1908 /* This is really two different operations rolled into one */
1909 req->out.numargs = 1;
1910 if (size) {
1911 req->out.argvar = 1;
1912 req->out.args[0].size = size;
1913 req->out.args[0].value = value;
1914 } else {
1915 req->out.args[0].size = sizeof(outarg);
1916 req->out.args[0].value = &outarg;
1917 }
Tejun Heob93f8582008-11-26 12:03:55 +01001918 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001919 ret = req->out.h.error;
1920 if (!ret)
1921 ret = size ? req->out.args[0].size : outarg.size;
1922 else {
1923 if (ret == -ENOSYS) {
1924 fc->no_getxattr = 1;
1925 ret = -EOPNOTSUPP;
1926 }
1927 }
1928 fuse_put_request(fc, req);
1929 return ret;
1930}
1931
1932static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1933{
1934 struct inode *inode = entry->d_inode;
1935 struct fuse_conn *fc = get_fuse_conn(inode);
1936 struct fuse_req *req;
1937 struct fuse_getxattr_in inarg;
1938 struct fuse_getxattr_out outarg;
1939 ssize_t ret;
1940
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001941 if (!fuse_allow_current_process(fc))
Miklos Szeredie57ac682007-10-18 03:06:58 -07001942 return -EACCES;
1943
Miklos Szeredi92a87802005-09-09 13:10:31 -07001944 if (fc->no_listxattr)
1945 return -EOPNOTSUPP;
1946
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001947 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001948 if (IS_ERR(req))
1949 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001950
1951 memset(&inarg, 0, sizeof(inarg));
1952 inarg.size = size;
1953 req->in.h.opcode = FUSE_LISTXATTR;
1954 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001955 req->in.numargs = 1;
1956 req->in.args[0].size = sizeof(inarg);
1957 req->in.args[0].value = &inarg;
1958 /* This is really two different operations rolled into one */
1959 req->out.numargs = 1;
1960 if (size) {
1961 req->out.argvar = 1;
1962 req->out.args[0].size = size;
1963 req->out.args[0].value = list;
1964 } else {
1965 req->out.args[0].size = sizeof(outarg);
1966 req->out.args[0].value = &outarg;
1967 }
Tejun Heob93f8582008-11-26 12:03:55 +01001968 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001969 ret = req->out.h.error;
1970 if (!ret)
1971 ret = size ? req->out.args[0].size : outarg.size;
1972 else {
1973 if (ret == -ENOSYS) {
1974 fc->no_listxattr = 1;
1975 ret = -EOPNOTSUPP;
1976 }
1977 }
1978 fuse_put_request(fc, req);
1979 return ret;
1980}
1981
1982static int fuse_removexattr(struct dentry *entry, const char *name)
1983{
1984 struct inode *inode = entry->d_inode;
1985 struct fuse_conn *fc = get_fuse_conn(inode);
1986 struct fuse_req *req;
1987 int err;
1988
1989 if (fc->no_removexattr)
1990 return -EOPNOTSUPP;
1991
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001992 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001993 if (IS_ERR(req))
1994 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001995
1996 req->in.h.opcode = FUSE_REMOVEXATTR;
1997 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001998 req->in.numargs = 1;
1999 req->in.args[0].size = strlen(name) + 1;
2000 req->in.args[0].value = name;
Tejun Heob93f8582008-11-26 12:03:55 +01002001 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07002002 err = req->out.h.error;
2003 fuse_put_request(fc, req);
2004 if (err == -ENOSYS) {
2005 fc->no_removexattr = 1;
2006 err = -EOPNOTSUPP;
2007 }
Maxim Patlasov31f32672014-04-28 14:19:24 +02002008 if (!err) {
Anand Avatid331a412013-08-20 02:21:07 -04002009 fuse_invalidate_attr(inode);
Maxim Patlasov31f32672014-04-28 14:19:24 +02002010 fuse_update_ctime(inode);
2011 }
Miklos Szeredi92a87802005-09-09 13:10:31 -07002012 return err;
2013}
2014
Arjan van de Ven754661f2007-02-12 00:55:38 -08002015static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07002016 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07002017 .mkdir = fuse_mkdir,
2018 .symlink = fuse_symlink,
2019 .unlink = fuse_unlink,
2020 .rmdir = fuse_rmdir,
Miklos Szeredi1560c972014-04-28 16:43:44 +02002021 .rename2 = fuse_rename2,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07002022 .link = fuse_link,
2023 .setattr = fuse_setattr,
2024 .create = fuse_create,
Miklos Szeredic8ccbe02012-06-05 15:10:22 +02002025 .atomic_open = fuse_atomic_open,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07002026 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002027 .permission = fuse_permission,
2028 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07002029 .setxattr = fuse_setxattr,
2030 .getxattr = fuse_getxattr,
2031 .listxattr = fuse_listxattr,
2032 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002033};
2034
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002035static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002036 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002037 .read = generic_read_dir,
Al Viro8d3af7f2013-05-18 03:03:58 -04002038 .iterate = fuse_readdir,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002039 .open = fuse_dir_open,
2040 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07002041 .fsync = fuse_dir_fsync,
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002042 .unlocked_ioctl = fuse_dir_ioctl,
2043 .compat_ioctl = fuse_dir_compat_ioctl,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002044};
2045
Arjan van de Ven754661f2007-02-12 00:55:38 -08002046static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07002047 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002048 .permission = fuse_permission,
2049 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07002050 .setxattr = fuse_setxattr,
2051 .getxattr = fuse_getxattr,
2052 .listxattr = fuse_listxattr,
2053 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002054};
2055
Arjan van de Ven754661f2007-02-12 00:55:38 -08002056static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07002057 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002058 .follow_link = fuse_follow_link,
2059 .put_link = fuse_put_link,
2060 .readlink = generic_readlink,
2061 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07002062 .setxattr = fuse_setxattr,
2063 .getxattr = fuse_getxattr,
2064 .listxattr = fuse_listxattr,
2065 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07002066};
2067
2068void fuse_init_common(struct inode *inode)
2069{
2070 inode->i_op = &fuse_common_inode_operations;
2071}
2072
2073void fuse_init_dir(struct inode *inode)
2074{
2075 inode->i_op = &fuse_dir_inode_operations;
2076 inode->i_fop = &fuse_dir_operations;
2077}
2078
2079void fuse_init_symlink(struct inode *inode)
2080{
2081 inode->i_op = &fuse_symlink_inode_operations;
2082}