blob: 8b8eebc5614bb1e1f6dcba5ecd0ad1966d831f07 [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>
13#include <linux/gfp.h>
14#include <linux/sched.h>
15#include <linux/namei.h>
16
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070017#if BITS_PER_LONG >= 64
18static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
19{
20 entry->d_time = time;
21}
22
23static inline u64 fuse_dentry_time(struct dentry *entry)
24{
25 return entry->d_time;
26}
27#else
28/*
29 * On 32 bit archs store the high 32 bits of time in d_fsdata
30 */
31static void fuse_dentry_settime(struct dentry *entry, u64 time)
32{
33 entry->d_time = time;
34 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
35}
36
37static u64 fuse_dentry_time(struct dentry *entry)
38{
39 return (u64) entry->d_time +
40 ((u64) (unsigned long) entry->d_fsdata << 32);
41}
42#endif
43
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080044/*
45 * FUSE caches dentries and attributes with separate timeout. The
46 * time in jiffies until the dentry/attributes are valid is stored in
47 * dentry->d_time and fuse_inode->i_time respectively.
48 */
49
50/*
51 * Calculate the time in jiffies until a dentry/attributes are valid
52 */
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070053static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
Miklos Szeredie5e55582005-09-09 13:10:28 -070054{
Miklos Szeredi685d16d2006-07-30 03:04:08 -070055 if (sec || nsec) {
56 struct timespec ts = {sec, nsec};
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070057 return get_jiffies_64() + timespec_to_jiffies(&ts);
Miklos Szeredi685d16d2006-07-30 03:04:08 -070058 } else
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070059 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070060}
61
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080062/*
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
64 * replies
65 */
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070066static void fuse_change_entry_timeout(struct dentry *entry,
67 struct fuse_entry_out *o)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080068{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070069 fuse_dentry_settime(entry,
70 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070071}
72
73static u64 attr_timeout(struct fuse_attr_out *o)
74{
75 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
76}
77
78static u64 entry_attr_timeout(struct fuse_entry_out *o)
79{
80 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080081}
82
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080083/*
84 * Mark the attributes as stale, so that at the next call to
85 * ->getattr() they will be fetched from userspace
86 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080087void fuse_invalidate_attr(struct inode *inode)
88{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070089 get_fuse_inode(inode)->i_time = 0;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080090}
91
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080092/*
93 * Just mark the entry as stale, so that a next attempt to look it up
94 * will result in a new lookup call to userspace
95 *
96 * This is called when a dentry is about to become negative and the
97 * timeout is unknown (unlink, rmdir, rename and in some cases
98 * lookup)
99 */
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700100void fuse_invalidate_entry_cache(struct dentry *entry)
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800101{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700102 fuse_dentry_settime(entry, 0);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800103}
104
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800105/*
106 * Same as fuse_invalidate_entry_cache(), but also try to remove the
107 * dentry from the hash
108 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800109static void fuse_invalidate_entry(struct dentry *entry)
110{
111 d_invalidate(entry);
112 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800113}
114
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700115static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
116 u64 nodeid, struct qstr *name,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700117 struct fuse_entry_out *outarg)
118{
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700119 memset(outarg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700120 req->in.h.opcode = FUSE_LOOKUP;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700121 req->in.h.nodeid = nodeid;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700122 req->in.numargs = 1;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700123 req->in.args[0].size = name->len + 1;
124 req->in.args[0].value = name->name;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700125 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700126 if (fc->minor < 9)
127 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
128 else
129 req->out.args[0].size = sizeof(struct fuse_entry_out);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700130 req->out.args[0].value = outarg;
131}
132
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700133u64 fuse_get_attr_version(struct fuse_conn *fc)
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800134{
135 u64 curr_version;
136
137 /*
138 * The spin lock isn't actually needed on 64bit archs, but we
139 * don't yet care too much about such optimizations.
140 */
141 spin_lock(&fc->lock);
142 curr_version = fc->attr_version;
143 spin_unlock(&fc->lock);
144
145 return curr_version;
146}
147
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800148/*
149 * Check whether the dentry is still valid
150 *
151 * If the entry validity timeout has expired and the dentry is
152 * positive, try to redo the lookup. If the lookup results in a
153 * different inode, then let the VFS invalidate the dentry and redo
154 * the lookup once more. If the lookup results in the same inode,
155 * then refresh the attributes, timeouts and mark the dentry valid.
156 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700157static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
158{
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800159 struct inode *inode = entry->d_inode;
160
161 if (inode && is_bad_inode(inode))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700162 return 0;
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700163 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700164 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700165 struct fuse_entry_out outarg;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800166 struct fuse_conn *fc;
167 struct fuse_req *req;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800168 struct fuse_req *forget_req;
Miklos Szeredie956edd2006-10-17 00:10:12 -0700169 struct dentry *parent;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700170 u64 attr_version;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800171
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800172 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800173 if (!inode)
174 return 0;
175
176 fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700177 req = fuse_get_req(fc);
178 if (IS_ERR(req))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700179 return 0;
180
Miklos Szeredi2d510132006-11-25 11:09:20 -0800181 forget_req = fuse_get_req(fc);
182 if (IS_ERR(forget_req)) {
183 fuse_put_request(fc, req);
184 return 0;
185 }
186
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800187 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700188
Miklos Szeredie956edd2006-10-17 00:10:12 -0700189 parent = dget_parent(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700190 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
191 &entry->d_name, &outarg);
Tejun Heob93f8582008-11-26 12:03:55 +0100192 fuse_request_send(fc, req);
Miklos Szeredie956edd2006-10-17 00:10:12 -0700193 dput(parent);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700194 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800195 fuse_put_request(fc, req);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800196 /* Zero nodeid is same as -ENOENT */
197 if (!err && !outarg.nodeid)
198 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700199 if (!err) {
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800200 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700201 if (outarg.nodeid != get_node_id(inode)) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800202 fuse_send_forget(fc, forget_req,
203 outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700204 return 0;
205 }
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700206 spin_lock(&fc->lock);
Miklos Szeredi1729a162008-11-26 12:03:54 +0100207 fi->nlookup++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700208 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700209 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800210 fuse_put_request(fc, forget_req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700211 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700212 return 0;
213
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700214 fuse_change_attributes(inode, &outarg.attr,
215 entry_attr_timeout(&outarg),
216 attr_version);
217 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700218 }
219 return 1;
220}
221
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800222static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800223{
224 return !nodeid || nodeid == FUSE_ROOT_ID;
225}
226
Al Viro42695902009-02-20 05:59:13 +0000227const struct dentry_operations fuse_dentry_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700228 .d_revalidate = fuse_dentry_revalidate,
229};
230
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700231int fuse_valid_type(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800232{
233 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
234 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
235}
236
Miklos Szeredid2a85162006-10-17 00:10:11 -0700237/*
238 * Add a directory inode to a dentry, ensuring that no other dentry
239 * refers to this inode. Called with fc->inst_mutex.
240 */
Miklos Szeredi0de62562008-07-25 01:48:59 -0700241static struct dentry *fuse_d_add_directory(struct dentry *entry,
242 struct inode *inode)
Miklos Szeredid2a85162006-10-17 00:10:11 -0700243{
244 struct dentry *alias = d_find_alias(inode);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700245 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
Miklos Szeredid2a85162006-10-17 00:10:11 -0700246 /* This tries to shrink the subtree below alias */
247 fuse_invalidate_entry(alias);
248 dput(alias);
249 if (!list_empty(&inode->i_dentry))
Miklos Szeredi0de62562008-07-25 01:48:59 -0700250 return ERR_PTR(-EBUSY);
251 } else {
252 dput(alias);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700253 }
Miklos Szeredi0de62562008-07-25 01:48:59 -0700254 return d_splice_alias(inode, entry);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700255}
256
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700257int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
258 struct fuse_entry_out *outarg, struct inode **inode)
259{
260 struct fuse_conn *fc = get_fuse_conn_super(sb);
261 struct fuse_req *req;
262 struct fuse_req *forget_req;
263 u64 attr_version;
264 int err;
265
266 *inode = NULL;
267 err = -ENAMETOOLONG;
268 if (name->len > FUSE_NAME_MAX)
269 goto out;
270
271 req = fuse_get_req(fc);
272 err = PTR_ERR(req);
273 if (IS_ERR(req))
274 goto out;
275
276 forget_req = fuse_get_req(fc);
277 err = PTR_ERR(forget_req);
278 if (IS_ERR(forget_req)) {
279 fuse_put_request(fc, req);
280 goto out;
281 }
282
283 attr_version = fuse_get_attr_version(fc);
284
285 fuse_lookup_init(fc, req, nodeid, name, outarg);
Tejun Heob93f8582008-11-26 12:03:55 +0100286 fuse_request_send(fc, req);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700287 err = req->out.h.error;
288 fuse_put_request(fc, req);
289 /* Zero nodeid is same as -ENOENT, but with valid timeout */
290 if (err || !outarg->nodeid)
291 goto out_put_forget;
292
293 err = -EIO;
294 if (!outarg->nodeid)
295 goto out_put_forget;
296 if (!fuse_valid_type(outarg->attr.mode))
297 goto out_put_forget;
298
299 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
300 &outarg->attr, entry_attr_timeout(outarg),
301 attr_version);
302 err = -ENOMEM;
303 if (!*inode) {
304 fuse_send_forget(fc, forget_req, outarg->nodeid, 1);
305 goto out;
306 }
307 err = 0;
308
309 out_put_forget:
310 fuse_put_request(fc, forget_req);
311 out:
312 return err;
313}
314
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800315static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
316 struct nameidata *nd)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700317{
318 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700319 struct fuse_entry_out outarg;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700320 struct inode *inode;
Miklos Szeredi0de62562008-07-25 01:48:59 -0700321 struct dentry *newent;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700322 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700323 bool outarg_valid = true;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700324
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700325 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
326 &outarg, &inode);
327 if (err == -ENOENT) {
328 outarg_valid = false;
329 err = 0;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800330 }
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700331 if (err)
332 goto out_err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800333
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700334 err = -EIO;
335 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
336 goto out_iput;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700337
Miklos Szeredid2a85162006-10-17 00:10:11 -0700338 if (inode && S_ISDIR(inode->i_mode)) {
339 mutex_lock(&fc->inst_mutex);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700340 newent = fuse_d_add_directory(entry, inode);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700341 mutex_unlock(&fc->inst_mutex);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700342 err = PTR_ERR(newent);
343 if (IS_ERR(newent))
344 goto out_iput;
345 } else {
Miklos Szeredi0de62562008-07-25 01:48:59 -0700346 newent = d_splice_alias(inode, entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700347 }
Miklos Szeredid2a85162006-10-17 00:10:11 -0700348
Miklos Szeredi0de62562008-07-25 01:48:59 -0700349 entry = newent ? newent : entry;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700350 entry->d_op = &fuse_dentry_operations;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700351 if (outarg_valid)
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700352 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800353 else
354 fuse_invalidate_entry_cache(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700355
Miklos Szeredi0de62562008-07-25 01:48:59 -0700356 return newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700357
358 out_iput:
359 iput(inode);
360 out_err:
361 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700362}
363
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800364/*
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700365 * Synchronous release for the case when something goes wrong in CREATE_OPEN
366 */
367static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
368 u64 nodeid, int flags)
369{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700370 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
371 ff->reserved_req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100372 fuse_request_send(fc, ff->reserved_req);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700373 fuse_put_request(fc, ff->reserved_req);
374 kfree(ff);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700375}
376
377/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800378 * Atomic create+open operation
379 *
380 * If the filesystem doesn't support this, then fall back to separate
381 * 'mknod' + 'open' requests.
382 */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800383static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
384 struct nameidata *nd)
385{
386 int err;
387 struct inode *inode;
388 struct fuse_conn *fc = get_fuse_conn(dir);
389 struct fuse_req *req;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700390 struct fuse_req *forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800391 struct fuse_open_in inarg;
392 struct fuse_open_out outopen;
393 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800394 struct fuse_file *ff;
395 struct file *file;
396 int flags = nd->intent.open.flags - 1;
397
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800398 if (fc->no_create)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700399 return -ENOSYS;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800400
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700401 forget_req = fuse_get_req(fc);
402 if (IS_ERR(forget_req))
403 return PTR_ERR(forget_req);
404
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700405 req = fuse_get_req(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700406 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700407 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700408 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800409
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700410 err = -ENOMEM;
Tejun Heoacf99432008-11-26 12:03:55 +0100411 ff = fuse_file_alloc(fc);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800412 if (!ff)
413 goto out_put_request;
414
415 flags &= ~O_NOCTTY;
416 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700417 memset(&outentry, 0, sizeof(outentry));
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800418 inarg.flags = flags;
419 inarg.mode = mode;
420 req->in.h.opcode = FUSE_CREATE;
421 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800422 req->in.numargs = 2;
423 req->in.args[0].size = sizeof(inarg);
424 req->in.args[0].value = &inarg;
425 req->in.args[1].size = entry->d_name.len + 1;
426 req->in.args[1].value = entry->d_name.name;
427 req->out.numargs = 2;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700428 if (fc->minor < 9)
429 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
430 else
431 req->out.args[0].size = sizeof(outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800432 req->out.args[0].value = &outentry;
433 req->out.args[1].size = sizeof(outopen);
434 req->out.args[1].value = &outopen;
Tejun Heob93f8582008-11-26 12:03:55 +0100435 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800436 err = req->out.h.error;
437 if (err) {
438 if (err == -ENOSYS)
439 fc->no_create = 1;
440 goto out_free_ff;
441 }
442
443 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800444 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800445 goto out_free_ff;
446
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700447 fuse_put_request(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800448 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700449 &outentry.attr, entry_attr_timeout(&outentry), 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800450 if (!inode) {
451 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
452 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700453 fuse_sync_release(fc, ff, outentry.nodeid, flags);
454 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
455 return -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800456 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700457 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800458 d_instantiate(entry, inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700459 fuse_change_entry_timeout(entry, &outentry);
Miklos Szeredi0952b2a2008-02-06 01:38:38 -0800460 fuse_invalidate_attr(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800461 file = lookup_instantiate_filp(nd, entry, generic_file_open);
462 if (IS_ERR(file)) {
463 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700464 fuse_sync_release(fc, ff, outentry.nodeid, flags);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800465 return PTR_ERR(file);
466 }
467 fuse_finish_open(inode, file, ff, &outopen);
468 return 0;
469
470 out_free_ff:
471 fuse_file_free(ff);
472 out_put_request:
473 fuse_put_request(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700474 out_put_forget_req:
475 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800476 return err;
477}
478
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800479/*
480 * Code shared between mknod, mkdir, symlink and link
481 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700482static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
483 struct inode *dir, struct dentry *entry,
484 int mode)
485{
486 struct fuse_entry_out outarg;
487 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700488 int err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800489 struct fuse_req *forget_req;
490
491 forget_req = fuse_get_req(fc);
492 if (IS_ERR(forget_req)) {
493 fuse_put_request(fc, req);
494 return PTR_ERR(forget_req);
495 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700496
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700497 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700498 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700499 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700500 if (fc->minor < 9)
501 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
502 else
503 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700504 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100505 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700506 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800507 fuse_put_request(fc, req);
508 if (err)
509 goto out_put_forget_req;
510
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800511 err = -EIO;
512 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800513 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800514
515 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800516 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800517
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700518 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700519 &outarg.attr, entry_attr_timeout(&outarg), 0);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700520 if (!inode) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800521 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700522 return -ENOMEM;
523 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800524 fuse_put_request(fc, forget_req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700525
Miklos Szeredid2a85162006-10-17 00:10:11 -0700526 if (S_ISDIR(inode->i_mode)) {
527 struct dentry *alias;
528 mutex_lock(&fc->inst_mutex);
529 alias = d_find_alias(inode);
530 if (alias) {
531 /* New directory must have moved since mkdir */
532 mutex_unlock(&fc->inst_mutex);
533 dput(alias);
534 iput(inode);
535 return -EBUSY;
536 }
537 d_instantiate(entry, inode);
538 mutex_unlock(&fc->inst_mutex);
539 } else
540 d_instantiate(entry, inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700541
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700542 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700543 fuse_invalidate_attr(dir);
544 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800545
Miklos Szeredi2d510132006-11-25 11:09:20 -0800546 out_put_forget_req:
547 fuse_put_request(fc, forget_req);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800548 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700549}
550
551static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
552 dev_t rdev)
553{
554 struct fuse_mknod_in inarg;
555 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700556 struct fuse_req *req = fuse_get_req(fc);
557 if (IS_ERR(req))
558 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700559
560 memset(&inarg, 0, sizeof(inarg));
561 inarg.mode = mode;
562 inarg.rdev = new_encode_dev(rdev);
563 req->in.h.opcode = FUSE_MKNOD;
564 req->in.numargs = 2;
565 req->in.args[0].size = sizeof(inarg);
566 req->in.args[0].value = &inarg;
567 req->in.args[1].size = entry->d_name.len + 1;
568 req->in.args[1].value = entry->d_name.name;
569 return create_new_entry(fc, req, dir, entry, mode);
570}
571
572static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
573 struct nameidata *nd)
574{
Miklos Szeredib9ba3472007-05-23 13:57:32 -0700575 if (nd && (nd->flags & LOOKUP_OPEN)) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800576 int err = fuse_create_open(dir, entry, mode, nd);
577 if (err != -ENOSYS)
578 return err;
579 /* Fall back on mknod */
580 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700581 return fuse_mknod(dir, entry, mode, 0);
582}
583
584static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
585{
586 struct fuse_mkdir_in inarg;
587 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700588 struct fuse_req *req = fuse_get_req(fc);
589 if (IS_ERR(req))
590 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700591
592 memset(&inarg, 0, sizeof(inarg));
593 inarg.mode = mode;
594 req->in.h.opcode = FUSE_MKDIR;
595 req->in.numargs = 2;
596 req->in.args[0].size = sizeof(inarg);
597 req->in.args[0].value = &inarg;
598 req->in.args[1].size = entry->d_name.len + 1;
599 req->in.args[1].value = entry->d_name.name;
600 return create_new_entry(fc, req, dir, entry, S_IFDIR);
601}
602
603static int fuse_symlink(struct inode *dir, struct dentry *entry,
604 const char *link)
605{
606 struct fuse_conn *fc = get_fuse_conn(dir);
607 unsigned len = strlen(link) + 1;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700608 struct fuse_req *req = fuse_get_req(fc);
609 if (IS_ERR(req))
610 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700611
612 req->in.h.opcode = FUSE_SYMLINK;
613 req->in.numargs = 2;
614 req->in.args[0].size = entry->d_name.len + 1;
615 req->in.args[0].value = entry->d_name.name;
616 req->in.args[1].size = len;
617 req->in.args[1].value = link;
618 return create_new_entry(fc, req, dir, entry, S_IFLNK);
619}
620
621static int fuse_unlink(struct inode *dir, struct dentry *entry)
622{
623 int err;
624 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700625 struct fuse_req *req = fuse_get_req(fc);
626 if (IS_ERR(req))
627 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700628
629 req->in.h.opcode = FUSE_UNLINK;
630 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700631 req->in.numargs = 1;
632 req->in.args[0].size = entry->d_name.len + 1;
633 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100634 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700635 err = req->out.h.error;
636 fuse_put_request(fc, req);
637 if (!err) {
638 struct inode *inode = entry->d_inode;
639
Miklos Szeredi1729a162008-11-26 12:03:54 +0100640 /*
641 * Set nlink to zero so the inode can be cleared, if the inode
642 * does have more links this will be discovered at the next
643 * lookup/getattr.
644 */
Dave Hansence71ec32006-09-30 23:29:06 -0700645 clear_nlink(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700646 fuse_invalidate_attr(inode);
647 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800648 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700649 } else if (err == -EINTR)
650 fuse_invalidate_entry(entry);
651 return err;
652}
653
654static int fuse_rmdir(struct inode *dir, struct dentry *entry)
655{
656 int err;
657 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700658 struct fuse_req *req = fuse_get_req(fc);
659 if (IS_ERR(req))
660 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700661
662 req->in.h.opcode = FUSE_RMDIR;
663 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700664 req->in.numargs = 1;
665 req->in.args[0].size = entry->d_name.len + 1;
666 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100667 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700668 err = req->out.h.error;
669 fuse_put_request(fc, req);
670 if (!err) {
Dave Hansence71ec32006-09-30 23:29:06 -0700671 clear_nlink(entry->d_inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700672 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800673 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700674 } else if (err == -EINTR)
675 fuse_invalidate_entry(entry);
676 return err;
677}
678
679static int fuse_rename(struct inode *olddir, struct dentry *oldent,
680 struct inode *newdir, struct dentry *newent)
681{
682 int err;
683 struct fuse_rename_in inarg;
684 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700685 struct fuse_req *req = fuse_get_req(fc);
686 if (IS_ERR(req))
687 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700688
689 memset(&inarg, 0, sizeof(inarg));
690 inarg.newdir = get_node_id(newdir);
691 req->in.h.opcode = FUSE_RENAME;
692 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700693 req->in.numargs = 3;
694 req->in.args[0].size = sizeof(inarg);
695 req->in.args[0].value = &inarg;
696 req->in.args[1].size = oldent->d_name.len + 1;
697 req->in.args[1].value = oldent->d_name.name;
698 req->in.args[2].size = newent->d_name.len + 1;
699 req->in.args[2].value = newent->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100700 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700701 err = req->out.h.error;
702 fuse_put_request(fc, req);
703 if (!err) {
Miklos Szeredi08b63302007-11-28 16:22:03 -0800704 /* ctime changes */
705 fuse_invalidate_attr(oldent->d_inode);
706
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700707 fuse_invalidate_attr(olddir);
708 if (olddir != newdir)
709 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800710
711 /* newent will end up negative */
712 if (newent->d_inode)
713 fuse_invalidate_entry_cache(newent);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700714 } else if (err == -EINTR) {
715 /* If request was interrupted, DEITY only knows if the
716 rename actually took place. If the invalidation
717 fails (e.g. some process has CWD under the renamed
718 directory), then there can be inconsistency between
719 the dcache and the real filesystem. Tough luck. */
720 fuse_invalidate_entry(oldent);
721 if (newent->d_inode)
722 fuse_invalidate_entry(newent);
723 }
724
725 return err;
726}
727
728static int fuse_link(struct dentry *entry, struct inode *newdir,
729 struct dentry *newent)
730{
731 int err;
732 struct fuse_link_in inarg;
733 struct inode *inode = entry->d_inode;
734 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700735 struct fuse_req *req = fuse_get_req(fc);
736 if (IS_ERR(req))
737 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700738
739 memset(&inarg, 0, sizeof(inarg));
740 inarg.oldnodeid = get_node_id(inode);
741 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700742 req->in.numargs = 2;
743 req->in.args[0].size = sizeof(inarg);
744 req->in.args[0].value = &inarg;
745 req->in.args[1].size = newent->d_name.len + 1;
746 req->in.args[1].value = newent->d_name.name;
747 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
748 /* Contrary to "normal" filesystems it can happen that link
749 makes two "logical" inodes point to the same "physical"
750 inode. We invalidate the attributes of the old one, so it
751 will reflect changes in the backing inode (link count,
752 etc.)
753 */
754 if (!err || err == -EINTR)
755 fuse_invalidate_attr(inode);
756 return err;
757}
758
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700759static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
760 struct kstat *stat)
761{
762 stat->dev = inode->i_sb->s_dev;
763 stat->ino = attr->ino;
764 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
765 stat->nlink = attr->nlink;
766 stat->uid = attr->uid;
767 stat->gid = attr->gid;
768 stat->rdev = inode->i_rdev;
769 stat->atime.tv_sec = attr->atime;
770 stat->atime.tv_nsec = attr->atimensec;
771 stat->mtime.tv_sec = attr->mtime;
772 stat->mtime.tv_nsec = attr->mtimensec;
773 stat->ctime.tv_sec = attr->ctime;
774 stat->ctime.tv_nsec = attr->ctimensec;
775 stat->size = attr->size;
776 stat->blocks = attr->blocks;
777 stat->blksize = (1 << inode->i_blkbits);
778}
779
Miklos Szeredic79e3222007-10-18 03:06:59 -0700780static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
781 struct file *file)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700782{
783 int err;
Miklos Szeredic79e3222007-10-18 03:06:59 -0700784 struct fuse_getattr_in inarg;
785 struct fuse_attr_out outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700786 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700787 struct fuse_req *req;
788 u64 attr_version;
789
790 req = fuse_get_req(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700791 if (IS_ERR(req))
792 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700793
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800794 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700795
Miklos Szeredic79e3222007-10-18 03:06:59 -0700796 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700797 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredic79e3222007-10-18 03:06:59 -0700798 /* Directories have separate file-handle space */
799 if (file && S_ISREG(inode->i_mode)) {
800 struct fuse_file *ff = file->private_data;
801
802 inarg.getattr_flags |= FUSE_GETATTR_FH;
803 inarg.fh = ff->fh;
804 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700805 req->in.h.opcode = FUSE_GETATTR;
806 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700807 req->in.numargs = 1;
808 req->in.args[0].size = sizeof(inarg);
809 req->in.args[0].value = &inarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700810 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700811 if (fc->minor < 9)
812 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
813 else
814 req->out.args[0].size = sizeof(outarg);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700815 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100816 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700817 err = req->out.h.error;
818 fuse_put_request(fc, req);
819 if (!err) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700820 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700821 make_bad_inode(inode);
822 err = -EIO;
823 } else {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700824 fuse_change_attributes(inode, &outarg.attr,
825 attr_timeout(&outarg),
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700826 attr_version);
827 if (stat)
Miklos Szeredic79e3222007-10-18 03:06:59 -0700828 fuse_fillattr(inode, &outarg.attr, stat);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700829 }
830 }
831 return err;
832}
833
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800834int fuse_update_attributes(struct inode *inode, struct kstat *stat,
835 struct file *file, bool *refreshed)
836{
837 struct fuse_inode *fi = get_fuse_inode(inode);
838 int err;
839 bool r;
840
841 if (fi->i_time < get_jiffies_64()) {
842 r = true;
843 err = fuse_do_getattr(inode, stat, file);
844 } else {
845 r = false;
846 err = 0;
847 if (stat) {
848 generic_fillattr(inode, stat);
849 stat->mode = fi->orig_i_mode;
850 }
851 }
852
853 if (refreshed != NULL)
854 *refreshed = r;
855
856 return err;
857}
858
Miklos Szeredi87729a52005-09-09 13:10:34 -0700859/*
860 * Calling into a user-controlled filesystem gives the filesystem
861 * daemon ptrace-like capabilities over the requester process. This
862 * means, that the filesystem daemon is able to record the exact
863 * filesystem operations performed, and can also control the behavior
864 * of the requester process in otherwise impossible ways. For example
865 * it can delay the operation for arbitrary length of time allowing
866 * DoS against the requester.
867 *
868 * For this reason only those processes can call into the filesystem,
869 * for which the owner of the mount has ptrace privilege. This
870 * excludes processes started by other users, suid or sgid processes.
871 */
Miklos Szeredie57ac682007-10-18 03:06:58 -0700872int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
Miklos Szeredi87729a52005-09-09 13:10:34 -0700873{
David Howellsc69e8d92008-11-14 10:39:19 +1100874 const struct cred *cred;
875 int ret;
876
Miklos Szeredi87729a52005-09-09 13:10:34 -0700877 if (fc->flags & FUSE_ALLOW_OTHER)
878 return 1;
879
David Howellsc69e8d92008-11-14 10:39:19 +1100880 rcu_read_lock();
881 ret = 0;
882 cred = __task_cred(task);
883 if (cred->euid == fc->user_id &&
884 cred->suid == fc->user_id &&
885 cred->uid == fc->user_id &&
886 cred->egid == fc->group_id &&
887 cred->sgid == fc->group_id &&
888 cred->gid == fc->group_id)
889 ret = 1;
890 rcu_read_unlock();
Miklos Szeredi87729a52005-09-09 13:10:34 -0700891
David Howellsc69e8d92008-11-14 10:39:19 +1100892 return ret;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700893}
894
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800895static int fuse_access(struct inode *inode, int mask)
896{
897 struct fuse_conn *fc = get_fuse_conn(inode);
898 struct fuse_req *req;
899 struct fuse_access_in inarg;
900 int err;
901
902 if (fc->no_access)
903 return 0;
904
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700905 req = fuse_get_req(fc);
906 if (IS_ERR(req))
907 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800908
909 memset(&inarg, 0, sizeof(inarg));
Al Viroe6305c42008-07-15 21:03:57 -0400910 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800911 req->in.h.opcode = FUSE_ACCESS;
912 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800913 req->in.numargs = 1;
914 req->in.args[0].size = sizeof(inarg);
915 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100916 fuse_request_send(fc, req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800917 err = req->out.h.error;
918 fuse_put_request(fc, req);
919 if (err == -ENOSYS) {
920 fc->no_access = 1;
921 err = 0;
922 }
923 return err;
924}
925
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800926/*
927 * Check permission. The two basic access models of FUSE are:
928 *
929 * 1) Local access checking ('default_permissions' mount option) based
930 * on file mode. This is the plain old disk filesystem permission
931 * modell.
932 *
933 * 2) "Remote" access checking, where server is responsible for
934 * checking permission in each inode operation. An exception to this
935 * is if ->permission() was invoked from sys_access() in which case an
936 * access request is sent. Execute permission is still checked
937 * locally based on file mode.
938 */
Al Viroe6305c42008-07-15 21:03:57 -0400939static int fuse_permission(struct inode *inode, int mask)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700940{
941 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -0700942 bool refreshed = false;
943 int err = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700944
Miklos Szeredi87729a52005-09-09 13:10:34 -0700945 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700946 return -EACCES;
Miklos Szeredi244f6382007-10-16 23:31:02 -0700947
948 /*
Miklos Szeredie8e96152007-10-16 23:31:06 -0700949 * If attributes are needed, refresh them before proceeding
Miklos Szeredi244f6382007-10-16 23:31:02 -0700950 */
Miklos Szeredie8e96152007-10-16 23:31:06 -0700951 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
952 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800953 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
954 if (err)
955 return err;
Miklos Szeredi244f6382007-10-16 23:31:02 -0700956 }
957
958 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
Miklos Szeredi1a823ac2008-02-23 15:23:27 -0800959 err = generic_permission(inode, mask, NULL);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700960
961 /* If permission is denied, try to refresh file
962 attributes. This is also needed, because the root
963 node will at first have no permissions */
Miklos Szeredi244f6382007-10-16 23:31:02 -0700964 if (err == -EACCES && !refreshed) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700965 err = fuse_do_getattr(inode, NULL, NULL);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700966 if (!err)
967 err = generic_permission(inode, mask, NULL);
968 }
969
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800970 /* Note: the opposite of the above test does not
971 exist. So if permissions are revoked this won't be
972 noticed immediately, only after the attribute
973 timeout has expired */
Al Viroa1103432008-07-17 09:19:08 -0400974 } else if (mask & MAY_ACCESS) {
Miklos Szeredie8e96152007-10-16 23:31:06 -0700975 err = fuse_access(inode, mask);
976 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
977 if (!(inode->i_mode & S_IXUGO)) {
978 if (refreshed)
979 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700980
Miklos Szeredic79e3222007-10-18 03:06:59 -0700981 err = fuse_do_getattr(inode, NULL, NULL);
Miklos Szeredie8e96152007-10-16 23:31:06 -0700982 if (!err && !(inode->i_mode & S_IXUGO))
983 return -EACCES;
984 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700985 }
Miklos Szeredi244f6382007-10-16 23:31:02 -0700986 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700987}
988
989static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
990 void *dstbuf, filldir_t filldir)
991{
992 while (nbytes >= FUSE_NAME_OFFSET) {
993 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
994 size_t reclen = FUSE_DIRENT_SIZE(dirent);
995 int over;
996 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
997 return -EIO;
998 if (reclen > nbytes)
999 break;
1000
1001 over = filldir(dstbuf, dirent->name, dirent->namelen,
1002 file->f_pos, dirent->ino, dirent->type);
1003 if (over)
1004 break;
1005
1006 buf += reclen;
1007 nbytes -= reclen;
1008 file->f_pos = dirent->off;
1009 }
1010
1011 return 0;
1012}
1013
Miklos Szeredie5e55582005-09-09 13:10:28 -07001014static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1015{
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001016 int err;
1017 size_t nbytes;
1018 struct page *page;
Josef Sipek7706a9d2006-12-08 02:37:02 -08001019 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001020 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001021 struct fuse_req *req;
1022
1023 if (is_bad_inode(inode))
1024 return -EIO;
1025
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001026 req = fuse_get_req(fc);
1027 if (IS_ERR(req))
1028 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001029
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001030 page = alloc_page(GFP_KERNEL);
1031 if (!page) {
1032 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001033 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001034 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001035 req->out.argpages = 1;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001036 req->num_pages = 1;
1037 req->pages[0] = page;
Miklos Szeredia6643092007-11-28 16:22:00 -08001038 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
Tejun Heob93f8582008-11-26 12:03:55 +01001039 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -08001040 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001041 err = req->out.h.error;
1042 fuse_put_request(fc, req);
1043 if (!err)
1044 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1045 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001046
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001047 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001048 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001049 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001050}
1051
1052static char *read_link(struct dentry *dentry)
1053{
1054 struct inode *inode = dentry->d_inode;
1055 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001056 struct fuse_req *req = fuse_get_req(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001057 char *link;
1058
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001059 if (IS_ERR(req))
David Howellse231c2e2008-02-07 00:15:26 -08001060 return ERR_CAST(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001061
1062 link = (char *) __get_free_page(GFP_KERNEL);
1063 if (!link) {
1064 link = ERR_PTR(-ENOMEM);
1065 goto out;
1066 }
1067 req->in.h.opcode = FUSE_READLINK;
1068 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001069 req->out.argvar = 1;
1070 req->out.numargs = 1;
1071 req->out.args[0].size = PAGE_SIZE - 1;
1072 req->out.args[0].value = link;
Tejun Heob93f8582008-11-26 12:03:55 +01001073 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001074 if (req->out.h.error) {
1075 free_page((unsigned long) link);
1076 link = ERR_PTR(req->out.h.error);
1077 } else
1078 link[req->out.args[0].size] = '\0';
1079 out:
1080 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001081 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -07001082 return link;
1083}
1084
1085static void free_link(char *link)
1086{
1087 if (!IS_ERR(link))
1088 free_page((unsigned long) link);
1089}
1090
1091static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1092{
1093 nd_set_link(nd, read_link(dentry));
1094 return NULL;
1095}
1096
1097static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1098{
1099 free_link(nd_get_link(nd));
1100}
1101
1102static int fuse_dir_open(struct inode *inode, struct file *file)
1103{
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001104 return fuse_open_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001105}
1106
1107static int fuse_dir_release(struct inode *inode, struct file *file)
1108{
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001109 return fuse_release_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001110}
1111
Miklos Szeredi82547982005-09-09 13:10:38 -07001112static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1113{
1114 /* nfsd can call this with no file */
1115 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1116}
1117
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001118static bool update_mtime(unsigned ivalid)
1119{
1120 /* Always update if mtime is explicitly set */
1121 if (ivalid & ATTR_MTIME_SET)
1122 return true;
1123
1124 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1125 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1126 return false;
1127
1128 /* In all other cases update */
1129 return true;
1130}
1131
Miklos Szeredibefc6492005-11-07 00:59:52 -08001132static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001133{
1134 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001135
1136 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001137 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001138 if (ivalid & ATTR_UID)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001139 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001140 if (ivalid & ATTR_GID)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001141 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001142 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001143 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001144 if (ivalid & ATTR_ATIME) {
1145 arg->valid |= FATTR_ATIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001146 arg->atime = iattr->ia_atime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001147 arg->atimensec = iattr->ia_atime.tv_nsec;
1148 if (!(ivalid & ATTR_ATIME_SET))
1149 arg->valid |= FATTR_ATIME_NOW;
1150 }
1151 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1152 arg->valid |= FATTR_MTIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001153 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001154 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1155 if (!(ivalid & ATTR_MTIME_SET))
1156 arg->valid |= FATTR_MTIME_NOW;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001157 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001158}
1159
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001160/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001161 * Prevent concurrent writepages on inode
1162 *
1163 * This is done by adding a negative bias to the inode write counter
1164 * and waiting for all pending writes to finish.
1165 */
1166void fuse_set_nowrite(struct inode *inode)
1167{
1168 struct fuse_conn *fc = get_fuse_conn(inode);
1169 struct fuse_inode *fi = get_fuse_inode(inode);
1170
1171 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1172
1173 spin_lock(&fc->lock);
1174 BUG_ON(fi->writectr < 0);
1175 fi->writectr += FUSE_NOWRITE;
1176 spin_unlock(&fc->lock);
1177 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1178}
1179
1180/*
1181 * Allow writepages on inode
1182 *
1183 * Remove the bias from the writecounter and send any queued
1184 * writepages.
1185 */
1186static void __fuse_release_nowrite(struct inode *inode)
1187{
1188 struct fuse_inode *fi = get_fuse_inode(inode);
1189
1190 BUG_ON(fi->writectr != FUSE_NOWRITE);
1191 fi->writectr = 0;
1192 fuse_flush_writepages(inode);
1193}
1194
1195void fuse_release_nowrite(struct inode *inode)
1196{
1197 struct fuse_conn *fc = get_fuse_conn(inode);
1198
1199 spin_lock(&fc->lock);
1200 __fuse_release_nowrite(inode);
1201 spin_unlock(&fc->lock);
1202}
1203
1204/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001205 * Set attributes, and at the same time refresh them.
1206 *
1207 * Truncation is slightly complicated, because the 'truncate' request
1208 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -07001209 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1210 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001211 */
Miklos Szeredi49d49142007-10-18 03:07:00 -07001212static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1213 struct file *file)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001214{
1215 struct inode *inode = entry->d_inode;
1216 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001217 struct fuse_req *req;
1218 struct fuse_setattr_in inarg;
1219 struct fuse_attr_out outarg;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001220 bool is_truncate = false;
1221 loff_t oldsize;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001222 int err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001223
Miklos Szeredie57ac682007-10-18 03:06:58 -07001224 if (!fuse_allow_task(fc, current))
1225 return -EACCES;
1226
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001227 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1228 err = inode_change_ok(inode, attr);
1229 if (err)
1230 return err;
1231 }
1232
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001233 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1234 return 0;
1235
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001236 if (attr->ia_valid & ATTR_SIZE) {
1237 unsigned long limit;
Miklos Szeredib2d22722006-12-06 20:35:51 -08001238 if (IS_SWAPFILE(inode))
1239 return -ETXTBSY;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001240 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1241 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1242 send_sig(SIGXFSZ, current, 0);
1243 return -EFBIG;
1244 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001245 is_truncate = true;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001246 }
1247
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001248 req = fuse_get_req(fc);
1249 if (IS_ERR(req))
1250 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001251
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001252 if (is_truncate)
1253 fuse_set_nowrite(inode);
1254
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001255 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001256 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -08001257 iattr_to_fattr(attr, &inarg);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001258 if (file) {
1259 struct fuse_file *ff = file->private_data;
1260 inarg.valid |= FATTR_FH;
1261 inarg.fh = ff->fh;
1262 }
Miklos Szeredif3332112007-10-18 03:07:04 -07001263 if (attr->ia_valid & ATTR_SIZE) {
1264 /* For mandatory locking in truncate */
1265 inarg.valid |= FATTR_LOCKOWNER;
1266 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1267 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001268 req->in.h.opcode = FUSE_SETATTR;
1269 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001270 req->in.numargs = 1;
1271 req->in.args[0].size = sizeof(inarg);
1272 req->in.args[0].value = &inarg;
1273 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001274 if (fc->minor < 9)
1275 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1276 else
1277 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001278 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001279 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001280 err = req->out.h.error;
1281 fuse_put_request(fc, req);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001282 if (err) {
1283 if (err == -EINTR)
1284 fuse_invalidate_attr(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001285 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001286 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001287
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001288 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1289 make_bad_inode(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001290 err = -EIO;
1291 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001292 }
1293
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001294 spin_lock(&fc->lock);
1295 fuse_change_attributes_common(inode, &outarg.attr,
1296 attr_timeout(&outarg));
1297 oldsize = inode->i_size;
1298 i_size_write(inode, outarg.attr.size);
1299
1300 if (is_truncate) {
1301 /* NOTE: this may release/reacquire fc->lock */
1302 __fuse_release_nowrite(inode);
1303 }
1304 spin_unlock(&fc->lock);
1305
1306 /*
1307 * Only call invalidate_inode_pages2() after removing
1308 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1309 */
1310 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1311 if (outarg.attr.size < oldsize)
1312 fuse_truncate(inode->i_mapping, outarg.attr.size);
1313 invalidate_inode_pages2(inode->i_mapping);
1314 }
1315
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001316 return 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001317
1318error:
1319 if (is_truncate)
1320 fuse_release_nowrite(inode);
1321
1322 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001323}
1324
Miklos Szeredi49d49142007-10-18 03:07:00 -07001325static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1326{
1327 if (attr->ia_valid & ATTR_FILE)
1328 return fuse_do_setattr(entry, attr, attr->ia_file);
1329 else
1330 return fuse_do_setattr(entry, attr, NULL);
1331}
1332
Miklos Szeredie5e55582005-09-09 13:10:28 -07001333static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1334 struct kstat *stat)
1335{
1336 struct inode *inode = entry->d_inode;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001337 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001338
1339 if (!fuse_allow_task(fc, current))
1340 return -EACCES;
1341
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001342 return fuse_update_attributes(inode, stat, NULL, NULL);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001343}
1344
Miklos Szeredi92a87802005-09-09 13:10:31 -07001345static int fuse_setxattr(struct dentry *entry, const char *name,
1346 const void *value, size_t size, int flags)
1347{
1348 struct inode *inode = entry->d_inode;
1349 struct fuse_conn *fc = get_fuse_conn(inode);
1350 struct fuse_req *req;
1351 struct fuse_setxattr_in inarg;
1352 int err;
1353
Miklos Szeredi92a87802005-09-09 13:10:31 -07001354 if (fc->no_setxattr)
1355 return -EOPNOTSUPP;
1356
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001357 req = fuse_get_req(fc);
1358 if (IS_ERR(req))
1359 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001360
1361 memset(&inarg, 0, sizeof(inarg));
1362 inarg.size = size;
1363 inarg.flags = flags;
1364 req->in.h.opcode = FUSE_SETXATTR;
1365 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001366 req->in.numargs = 3;
1367 req->in.args[0].size = sizeof(inarg);
1368 req->in.args[0].value = &inarg;
1369 req->in.args[1].size = strlen(name) + 1;
1370 req->in.args[1].value = name;
1371 req->in.args[2].size = size;
1372 req->in.args[2].value = value;
Tejun Heob93f8582008-11-26 12:03:55 +01001373 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001374 err = req->out.h.error;
1375 fuse_put_request(fc, req);
1376 if (err == -ENOSYS) {
1377 fc->no_setxattr = 1;
1378 err = -EOPNOTSUPP;
1379 }
1380 return err;
1381}
1382
1383static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1384 void *value, size_t size)
1385{
1386 struct inode *inode = entry->d_inode;
1387 struct fuse_conn *fc = get_fuse_conn(inode);
1388 struct fuse_req *req;
1389 struct fuse_getxattr_in inarg;
1390 struct fuse_getxattr_out outarg;
1391 ssize_t ret;
1392
1393 if (fc->no_getxattr)
1394 return -EOPNOTSUPP;
1395
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001396 req = fuse_get_req(fc);
1397 if (IS_ERR(req))
1398 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001399
1400 memset(&inarg, 0, sizeof(inarg));
1401 inarg.size = size;
1402 req->in.h.opcode = FUSE_GETXATTR;
1403 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001404 req->in.numargs = 2;
1405 req->in.args[0].size = sizeof(inarg);
1406 req->in.args[0].value = &inarg;
1407 req->in.args[1].size = strlen(name) + 1;
1408 req->in.args[1].value = name;
1409 /* This is really two different operations rolled into one */
1410 req->out.numargs = 1;
1411 if (size) {
1412 req->out.argvar = 1;
1413 req->out.args[0].size = size;
1414 req->out.args[0].value = value;
1415 } else {
1416 req->out.args[0].size = sizeof(outarg);
1417 req->out.args[0].value = &outarg;
1418 }
Tejun Heob93f8582008-11-26 12:03:55 +01001419 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001420 ret = req->out.h.error;
1421 if (!ret)
1422 ret = size ? req->out.args[0].size : outarg.size;
1423 else {
1424 if (ret == -ENOSYS) {
1425 fc->no_getxattr = 1;
1426 ret = -EOPNOTSUPP;
1427 }
1428 }
1429 fuse_put_request(fc, req);
1430 return ret;
1431}
1432
1433static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1434{
1435 struct inode *inode = entry->d_inode;
1436 struct fuse_conn *fc = get_fuse_conn(inode);
1437 struct fuse_req *req;
1438 struct fuse_getxattr_in inarg;
1439 struct fuse_getxattr_out outarg;
1440 ssize_t ret;
1441
Miklos Szeredie57ac682007-10-18 03:06:58 -07001442 if (!fuse_allow_task(fc, current))
1443 return -EACCES;
1444
Miklos Szeredi92a87802005-09-09 13:10:31 -07001445 if (fc->no_listxattr)
1446 return -EOPNOTSUPP;
1447
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001448 req = fuse_get_req(fc);
1449 if (IS_ERR(req))
1450 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001451
1452 memset(&inarg, 0, sizeof(inarg));
1453 inarg.size = size;
1454 req->in.h.opcode = FUSE_LISTXATTR;
1455 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001456 req->in.numargs = 1;
1457 req->in.args[0].size = sizeof(inarg);
1458 req->in.args[0].value = &inarg;
1459 /* This is really two different operations rolled into one */
1460 req->out.numargs = 1;
1461 if (size) {
1462 req->out.argvar = 1;
1463 req->out.args[0].size = size;
1464 req->out.args[0].value = list;
1465 } else {
1466 req->out.args[0].size = sizeof(outarg);
1467 req->out.args[0].value = &outarg;
1468 }
Tejun Heob93f8582008-11-26 12:03:55 +01001469 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001470 ret = req->out.h.error;
1471 if (!ret)
1472 ret = size ? req->out.args[0].size : outarg.size;
1473 else {
1474 if (ret == -ENOSYS) {
1475 fc->no_listxattr = 1;
1476 ret = -EOPNOTSUPP;
1477 }
1478 }
1479 fuse_put_request(fc, req);
1480 return ret;
1481}
1482
1483static int fuse_removexattr(struct dentry *entry, const char *name)
1484{
1485 struct inode *inode = entry->d_inode;
1486 struct fuse_conn *fc = get_fuse_conn(inode);
1487 struct fuse_req *req;
1488 int err;
1489
1490 if (fc->no_removexattr)
1491 return -EOPNOTSUPP;
1492
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001493 req = fuse_get_req(fc);
1494 if (IS_ERR(req))
1495 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001496
1497 req->in.h.opcode = FUSE_REMOVEXATTR;
1498 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001499 req->in.numargs = 1;
1500 req->in.args[0].size = strlen(name) + 1;
1501 req->in.args[0].value = name;
Tejun Heob93f8582008-11-26 12:03:55 +01001502 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001503 err = req->out.h.error;
1504 fuse_put_request(fc, req);
1505 if (err == -ENOSYS) {
1506 fc->no_removexattr = 1;
1507 err = -EOPNOTSUPP;
1508 }
1509 return err;
1510}
1511
Arjan van de Ven754661f2007-02-12 00:55:38 -08001512static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07001513 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001514 .mkdir = fuse_mkdir,
1515 .symlink = fuse_symlink,
1516 .unlink = fuse_unlink,
1517 .rmdir = fuse_rmdir,
1518 .rename = fuse_rename,
1519 .link = fuse_link,
1520 .setattr = fuse_setattr,
1521 .create = fuse_create,
1522 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001523 .permission = fuse_permission,
1524 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001525 .setxattr = fuse_setxattr,
1526 .getxattr = fuse_getxattr,
1527 .listxattr = fuse_listxattr,
1528 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001529};
1530
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001531static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001532 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001533 .read = generic_read_dir,
1534 .readdir = fuse_readdir,
1535 .open = fuse_dir_open,
1536 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001537 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001538};
1539
Arjan van de Ven754661f2007-02-12 00:55:38 -08001540static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001541 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001542 .permission = fuse_permission,
1543 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001544 .setxattr = fuse_setxattr,
1545 .getxattr = fuse_getxattr,
1546 .listxattr = fuse_listxattr,
1547 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001548};
1549
Arjan van de Ven754661f2007-02-12 00:55:38 -08001550static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001551 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001552 .follow_link = fuse_follow_link,
1553 .put_link = fuse_put_link,
1554 .readlink = generic_readlink,
1555 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001556 .setxattr = fuse_setxattr,
1557 .getxattr = fuse_getxattr,
1558 .listxattr = fuse_listxattr,
1559 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001560};
1561
1562void fuse_init_common(struct inode *inode)
1563{
1564 inode->i_op = &fuse_common_inode_operations;
1565}
1566
1567void fuse_init_dir(struct inode *inode)
1568{
1569 inode->i_op = &fuse_dir_inode_operations;
1570 inode->i_fop = &fuse_dir_operations;
1571}
1572
1573void fuse_init_symlink(struct inode *inode)
1574{
1575 inode->i_op = &fuse_symlink_inode_operations;
1576}