blob: 6b700734e5197356b0a3bb0dcff5d3eb66bfbcf0 [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/*
365 * Atomic create+open operation
366 *
367 * If the filesystem doesn't support this, then fall back to separate
368 * 'mknod' + 'open' requests.
369 */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800370static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
371 struct nameidata *nd)
372{
373 int err;
374 struct inode *inode;
375 struct fuse_conn *fc = get_fuse_conn(dir);
376 struct fuse_req *req;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700377 struct fuse_req *forget_req;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200378 struct fuse_create_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800379 struct fuse_open_out outopen;
380 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800381 struct fuse_file *ff;
382 struct file *file;
383 int flags = nd->intent.open.flags - 1;
384
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800385 if (fc->no_create)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700386 return -ENOSYS;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800387
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700388 forget_req = fuse_get_req(fc);
389 if (IS_ERR(forget_req))
390 return PTR_ERR(forget_req);
391
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700392 req = fuse_get_req(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700393 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700394 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700395 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800396
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700397 err = -ENOMEM;
Tejun Heoacf99432008-11-26 12:03:55 +0100398 ff = fuse_file_alloc(fc);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800399 if (!ff)
400 goto out_put_request;
401
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200402 if (!fc->dont_mask)
403 mode &= ~current_umask();
404
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800405 flags &= ~O_NOCTTY;
406 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700407 memset(&outentry, 0, sizeof(outentry));
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800408 inarg.flags = flags;
409 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200410 inarg.umask = current_umask();
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800411 req->in.h.opcode = FUSE_CREATE;
412 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800413 req->in.numargs = 2;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200414 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
415 sizeof(inarg);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800416 req->in.args[0].value = &inarg;
417 req->in.args[1].size = entry->d_name.len + 1;
418 req->in.args[1].value = entry->d_name.name;
419 req->out.numargs = 2;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700420 if (fc->minor < 9)
421 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
422 else
423 req->out.args[0].size = sizeof(outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800424 req->out.args[0].value = &outentry;
425 req->out.args[1].size = sizeof(outopen);
426 req->out.args[1].value = &outopen;
Tejun Heob93f8582008-11-26 12:03:55 +0100427 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800428 err = req->out.h.error;
429 if (err) {
430 if (err == -ENOSYS)
431 fc->no_create = 1;
432 goto out_free_ff;
433 }
434
435 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800436 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800437 goto out_free_ff;
438
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700439 fuse_put_request(fc, req);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200440 ff->fh = outopen.fh;
441 ff->nodeid = outentry.nodeid;
442 ff->open_flags = outopen.open_flags;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800443 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700444 &outentry.attr, entry_attr_timeout(&outentry), 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800445 if (!inode) {
446 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200447 fuse_sync_release(ff, flags);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700448 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
449 return -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800450 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700451 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800452 d_instantiate(entry, inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700453 fuse_change_entry_timeout(entry, &outentry);
Miklos Szeredi0952b2a2008-02-06 01:38:38 -0800454 fuse_invalidate_attr(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800455 file = lookup_instantiate_filp(nd, entry, generic_file_open);
456 if (IS_ERR(file)) {
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200457 fuse_sync_release(ff, flags);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800458 return PTR_ERR(file);
459 }
Miklos Szeredic7b71432009-04-28 16:56:37 +0200460 file->private_data = fuse_file_get(ff);
461 fuse_finish_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800462 return 0;
463
464 out_free_ff:
465 fuse_file_free(ff);
466 out_put_request:
467 fuse_put_request(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700468 out_put_forget_req:
469 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800470 return err;
471}
472
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800473/*
474 * Code shared between mknod, mkdir, symlink and link
475 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700476static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
477 struct inode *dir, struct dentry *entry,
478 int mode)
479{
480 struct fuse_entry_out outarg;
481 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700482 int err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800483 struct fuse_req *forget_req;
484
485 forget_req = fuse_get_req(fc);
486 if (IS_ERR(forget_req)) {
487 fuse_put_request(fc, req);
488 return PTR_ERR(forget_req);
489 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700490
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700491 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700492 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700493 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700494 if (fc->minor < 9)
495 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
496 else
497 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700498 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100499 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700500 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800501 fuse_put_request(fc, req);
502 if (err)
503 goto out_put_forget_req;
504
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800505 err = -EIO;
506 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800507 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800508
509 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800510 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800511
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700512 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700513 &outarg.attr, entry_attr_timeout(&outarg), 0);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700514 if (!inode) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800515 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700516 return -ENOMEM;
517 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800518 fuse_put_request(fc, forget_req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700519
Miklos Szeredid2a85162006-10-17 00:10:11 -0700520 if (S_ISDIR(inode->i_mode)) {
521 struct dentry *alias;
522 mutex_lock(&fc->inst_mutex);
523 alias = d_find_alias(inode);
524 if (alias) {
525 /* New directory must have moved since mkdir */
526 mutex_unlock(&fc->inst_mutex);
527 dput(alias);
528 iput(inode);
529 return -EBUSY;
530 }
531 d_instantiate(entry, inode);
532 mutex_unlock(&fc->inst_mutex);
533 } else
534 d_instantiate(entry, inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700535
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700536 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700537 fuse_invalidate_attr(dir);
538 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800539
Miklos Szeredi2d510132006-11-25 11:09:20 -0800540 out_put_forget_req:
541 fuse_put_request(fc, forget_req);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800542 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700543}
544
545static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
546 dev_t rdev)
547{
548 struct fuse_mknod_in inarg;
549 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700550 struct fuse_req *req = fuse_get_req(fc);
551 if (IS_ERR(req))
552 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700553
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200554 if (!fc->dont_mask)
555 mode &= ~current_umask();
556
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700557 memset(&inarg, 0, sizeof(inarg));
558 inarg.mode = mode;
559 inarg.rdev = new_encode_dev(rdev);
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200560 inarg.umask = current_umask();
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700561 req->in.h.opcode = FUSE_MKNOD;
562 req->in.numargs = 2;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200563 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
564 sizeof(inarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700565 req->in.args[0].value = &inarg;
566 req->in.args[1].size = entry->d_name.len + 1;
567 req->in.args[1].value = entry->d_name.name;
568 return create_new_entry(fc, req, dir, entry, mode);
569}
570
571static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
572 struct nameidata *nd)
573{
Miklos Szeredib9ba3472007-05-23 13:57:32 -0700574 if (nd && (nd->flags & LOOKUP_OPEN)) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800575 int err = fuse_create_open(dir, entry, mode, nd);
576 if (err != -ENOSYS)
577 return err;
578 /* Fall back on mknod */
579 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700580 return fuse_mknod(dir, entry, mode, 0);
581}
582
583static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
584{
585 struct fuse_mkdir_in inarg;
586 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700587 struct fuse_req *req = fuse_get_req(fc);
588 if (IS_ERR(req))
589 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700590
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200591 if (!fc->dont_mask)
592 mode &= ~current_umask();
593
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700594 memset(&inarg, 0, sizeof(inarg));
595 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200596 inarg.umask = current_umask();
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700597 req->in.h.opcode = FUSE_MKDIR;
598 req->in.numargs = 2;
599 req->in.args[0].size = sizeof(inarg);
600 req->in.args[0].value = &inarg;
601 req->in.args[1].size = entry->d_name.len + 1;
602 req->in.args[1].value = entry->d_name.name;
603 return create_new_entry(fc, req, dir, entry, S_IFDIR);
604}
605
606static int fuse_symlink(struct inode *dir, struct dentry *entry,
607 const char *link)
608{
609 struct fuse_conn *fc = get_fuse_conn(dir);
610 unsigned len = strlen(link) + 1;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700611 struct fuse_req *req = fuse_get_req(fc);
612 if (IS_ERR(req))
613 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700614
615 req->in.h.opcode = FUSE_SYMLINK;
616 req->in.numargs = 2;
617 req->in.args[0].size = entry->d_name.len + 1;
618 req->in.args[0].value = entry->d_name.name;
619 req->in.args[1].size = len;
620 req->in.args[1].value = link;
621 return create_new_entry(fc, req, dir, entry, S_IFLNK);
622}
623
624static int fuse_unlink(struct inode *dir, struct dentry *entry)
625{
626 int err;
627 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700628 struct fuse_req *req = fuse_get_req(fc);
629 if (IS_ERR(req))
630 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700631
632 req->in.h.opcode = FUSE_UNLINK;
633 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700634 req->in.numargs = 1;
635 req->in.args[0].size = entry->d_name.len + 1;
636 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100637 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700638 err = req->out.h.error;
639 fuse_put_request(fc, req);
640 if (!err) {
641 struct inode *inode = entry->d_inode;
642
Miklos Szeredi1729a162008-11-26 12:03:54 +0100643 /*
644 * Set nlink to zero so the inode can be cleared, if the inode
645 * does have more links this will be discovered at the next
646 * lookup/getattr.
647 */
Dave Hansence71ec32006-09-30 23:29:06 -0700648 clear_nlink(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700649 fuse_invalidate_attr(inode);
650 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800651 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700652 } else if (err == -EINTR)
653 fuse_invalidate_entry(entry);
654 return err;
655}
656
657static int fuse_rmdir(struct inode *dir, struct dentry *entry)
658{
659 int err;
660 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700661 struct fuse_req *req = fuse_get_req(fc);
662 if (IS_ERR(req))
663 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700664
665 req->in.h.opcode = FUSE_RMDIR;
666 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700667 req->in.numargs = 1;
668 req->in.args[0].size = entry->d_name.len + 1;
669 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100670 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700671 err = req->out.h.error;
672 fuse_put_request(fc, req);
673 if (!err) {
Dave Hansence71ec32006-09-30 23:29:06 -0700674 clear_nlink(entry->d_inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700675 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800676 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700677 } else if (err == -EINTR)
678 fuse_invalidate_entry(entry);
679 return err;
680}
681
682static int fuse_rename(struct inode *olddir, struct dentry *oldent,
683 struct inode *newdir, struct dentry *newent)
684{
685 int err;
686 struct fuse_rename_in inarg;
687 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700688 struct fuse_req *req = fuse_get_req(fc);
689 if (IS_ERR(req))
690 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700691
692 memset(&inarg, 0, sizeof(inarg));
693 inarg.newdir = get_node_id(newdir);
694 req->in.h.opcode = FUSE_RENAME;
695 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700696 req->in.numargs = 3;
697 req->in.args[0].size = sizeof(inarg);
698 req->in.args[0].value = &inarg;
699 req->in.args[1].size = oldent->d_name.len + 1;
700 req->in.args[1].value = oldent->d_name.name;
701 req->in.args[2].size = newent->d_name.len + 1;
702 req->in.args[2].value = newent->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100703 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700704 err = req->out.h.error;
705 fuse_put_request(fc, req);
706 if (!err) {
Miklos Szeredi08b63302007-11-28 16:22:03 -0800707 /* ctime changes */
708 fuse_invalidate_attr(oldent->d_inode);
709
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700710 fuse_invalidate_attr(olddir);
711 if (olddir != newdir)
712 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800713
714 /* newent will end up negative */
715 if (newent->d_inode)
716 fuse_invalidate_entry_cache(newent);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700717 } else if (err == -EINTR) {
718 /* If request was interrupted, DEITY only knows if the
719 rename actually took place. If the invalidation
720 fails (e.g. some process has CWD under the renamed
721 directory), then there can be inconsistency between
722 the dcache and the real filesystem. Tough luck. */
723 fuse_invalidate_entry(oldent);
724 if (newent->d_inode)
725 fuse_invalidate_entry(newent);
726 }
727
728 return err;
729}
730
731static int fuse_link(struct dentry *entry, struct inode *newdir,
732 struct dentry *newent)
733{
734 int err;
735 struct fuse_link_in inarg;
736 struct inode *inode = entry->d_inode;
737 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700738 struct fuse_req *req = fuse_get_req(fc);
739 if (IS_ERR(req))
740 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700741
742 memset(&inarg, 0, sizeof(inarg));
743 inarg.oldnodeid = get_node_id(inode);
744 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700745 req->in.numargs = 2;
746 req->in.args[0].size = sizeof(inarg);
747 req->in.args[0].value = &inarg;
748 req->in.args[1].size = newent->d_name.len + 1;
749 req->in.args[1].value = newent->d_name.name;
750 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
751 /* Contrary to "normal" filesystems it can happen that link
752 makes two "logical" inodes point to the same "physical"
753 inode. We invalidate the attributes of the old one, so it
754 will reflect changes in the backing inode (link count,
755 etc.)
756 */
757 if (!err || err == -EINTR)
758 fuse_invalidate_attr(inode);
759 return err;
760}
761
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700762static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
763 struct kstat *stat)
764{
765 stat->dev = inode->i_sb->s_dev;
766 stat->ino = attr->ino;
767 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
768 stat->nlink = attr->nlink;
769 stat->uid = attr->uid;
770 stat->gid = attr->gid;
771 stat->rdev = inode->i_rdev;
772 stat->atime.tv_sec = attr->atime;
773 stat->atime.tv_nsec = attr->atimensec;
774 stat->mtime.tv_sec = attr->mtime;
775 stat->mtime.tv_nsec = attr->mtimensec;
776 stat->ctime.tv_sec = attr->ctime;
777 stat->ctime.tv_nsec = attr->ctimensec;
778 stat->size = attr->size;
779 stat->blocks = attr->blocks;
780 stat->blksize = (1 << inode->i_blkbits);
781}
782
Miklos Szeredic79e3222007-10-18 03:06:59 -0700783static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
784 struct file *file)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700785{
786 int err;
Miklos Szeredic79e3222007-10-18 03:06:59 -0700787 struct fuse_getattr_in inarg;
788 struct fuse_attr_out outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700789 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700790 struct fuse_req *req;
791 u64 attr_version;
792
793 req = fuse_get_req(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700794 if (IS_ERR(req))
795 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700796
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800797 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700798
Miklos Szeredic79e3222007-10-18 03:06:59 -0700799 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700800 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredic79e3222007-10-18 03:06:59 -0700801 /* Directories have separate file-handle space */
802 if (file && S_ISREG(inode->i_mode)) {
803 struct fuse_file *ff = file->private_data;
804
805 inarg.getattr_flags |= FUSE_GETATTR_FH;
806 inarg.fh = ff->fh;
807 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700808 req->in.h.opcode = FUSE_GETATTR;
809 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700810 req->in.numargs = 1;
811 req->in.args[0].size = sizeof(inarg);
812 req->in.args[0].value = &inarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700813 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700814 if (fc->minor < 9)
815 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
816 else
817 req->out.args[0].size = sizeof(outarg);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700818 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100819 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700820 err = req->out.h.error;
821 fuse_put_request(fc, req);
822 if (!err) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700823 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700824 make_bad_inode(inode);
825 err = -EIO;
826 } else {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700827 fuse_change_attributes(inode, &outarg.attr,
828 attr_timeout(&outarg),
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700829 attr_version);
830 if (stat)
Miklos Szeredic79e3222007-10-18 03:06:59 -0700831 fuse_fillattr(inode, &outarg.attr, stat);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700832 }
833 }
834 return err;
835}
836
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800837int fuse_update_attributes(struct inode *inode, struct kstat *stat,
838 struct file *file, bool *refreshed)
839{
840 struct fuse_inode *fi = get_fuse_inode(inode);
841 int err;
842 bool r;
843
844 if (fi->i_time < get_jiffies_64()) {
845 r = true;
846 err = fuse_do_getattr(inode, stat, file);
847 } else {
848 r = false;
849 err = 0;
850 if (stat) {
851 generic_fillattr(inode, stat);
852 stat->mode = fi->orig_i_mode;
853 }
854 }
855
856 if (refreshed != NULL)
857 *refreshed = r;
858
859 return err;
860}
861
Miklos Szeredi87729a52005-09-09 13:10:34 -0700862/*
863 * Calling into a user-controlled filesystem gives the filesystem
864 * daemon ptrace-like capabilities over the requester process. This
865 * means, that the filesystem daemon is able to record the exact
866 * filesystem operations performed, and can also control the behavior
867 * of the requester process in otherwise impossible ways. For example
868 * it can delay the operation for arbitrary length of time allowing
869 * DoS against the requester.
870 *
871 * For this reason only those processes can call into the filesystem,
872 * for which the owner of the mount has ptrace privilege. This
873 * excludes processes started by other users, suid or sgid processes.
874 */
Miklos Szeredie57ac682007-10-18 03:06:58 -0700875int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
Miklos Szeredi87729a52005-09-09 13:10:34 -0700876{
David Howellsc69e8d92008-11-14 10:39:19 +1100877 const struct cred *cred;
878 int ret;
879
Miklos Szeredi87729a52005-09-09 13:10:34 -0700880 if (fc->flags & FUSE_ALLOW_OTHER)
881 return 1;
882
David Howellsc69e8d92008-11-14 10:39:19 +1100883 rcu_read_lock();
884 ret = 0;
885 cred = __task_cred(task);
886 if (cred->euid == fc->user_id &&
887 cred->suid == fc->user_id &&
888 cred->uid == fc->user_id &&
889 cred->egid == fc->group_id &&
890 cred->sgid == fc->group_id &&
891 cred->gid == fc->group_id)
892 ret = 1;
893 rcu_read_unlock();
Miklos Szeredi87729a52005-09-09 13:10:34 -0700894
David Howellsc69e8d92008-11-14 10:39:19 +1100895 return ret;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700896}
897
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800898static int fuse_access(struct inode *inode, int mask)
899{
900 struct fuse_conn *fc = get_fuse_conn(inode);
901 struct fuse_req *req;
902 struct fuse_access_in inarg;
903 int err;
904
905 if (fc->no_access)
906 return 0;
907
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700908 req = fuse_get_req(fc);
909 if (IS_ERR(req))
910 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800911
912 memset(&inarg, 0, sizeof(inarg));
Al Viroe6305c42008-07-15 21:03:57 -0400913 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800914 req->in.h.opcode = FUSE_ACCESS;
915 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800916 req->in.numargs = 1;
917 req->in.args[0].size = sizeof(inarg);
918 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100919 fuse_request_send(fc, req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800920 err = req->out.h.error;
921 fuse_put_request(fc, req);
922 if (err == -ENOSYS) {
923 fc->no_access = 1;
924 err = 0;
925 }
926 return err;
927}
928
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800929/*
930 * Check permission. The two basic access models of FUSE are:
931 *
932 * 1) Local access checking ('default_permissions' mount option) based
933 * on file mode. This is the plain old disk filesystem permission
934 * modell.
935 *
936 * 2) "Remote" access checking, where server is responsible for
937 * checking permission in each inode operation. An exception to this
938 * is if ->permission() was invoked from sys_access() in which case an
939 * access request is sent. Execute permission is still checked
940 * locally based on file mode.
941 */
Al Viroe6305c42008-07-15 21:03:57 -0400942static int fuse_permission(struct inode *inode, int mask)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700943{
944 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -0700945 bool refreshed = false;
946 int err = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700947
Miklos Szeredi87729a52005-09-09 13:10:34 -0700948 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700949 return -EACCES;
Miklos Szeredi244f6382007-10-16 23:31:02 -0700950
951 /*
Miklos Szeredie8e96152007-10-16 23:31:06 -0700952 * If attributes are needed, refresh them before proceeding
Miklos Szeredi244f6382007-10-16 23:31:02 -0700953 */
Miklos Szeredie8e96152007-10-16 23:31:06 -0700954 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
955 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800956 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
957 if (err)
958 return err;
Miklos Szeredi244f6382007-10-16 23:31:02 -0700959 }
960
961 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
Miklos Szeredi1a823ac2008-02-23 15:23:27 -0800962 err = generic_permission(inode, mask, NULL);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700963
964 /* If permission is denied, try to refresh file
965 attributes. This is also needed, because the root
966 node will at first have no permissions */
Miklos Szeredi244f6382007-10-16 23:31:02 -0700967 if (err == -EACCES && !refreshed) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700968 err = fuse_do_getattr(inode, NULL, NULL);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700969 if (!err)
970 err = generic_permission(inode, mask, NULL);
971 }
972
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800973 /* Note: the opposite of the above test does not
974 exist. So if permissions are revoked this won't be
975 noticed immediately, only after the attribute
976 timeout has expired */
Al Viroa1103432008-07-17 09:19:08 -0400977 } else if (mask & MAY_ACCESS) {
Miklos Szeredie8e96152007-10-16 23:31:06 -0700978 err = fuse_access(inode, mask);
979 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
980 if (!(inode->i_mode & S_IXUGO)) {
981 if (refreshed)
982 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700983
Miklos Szeredic79e3222007-10-18 03:06:59 -0700984 err = fuse_do_getattr(inode, NULL, NULL);
Miklos Szeredie8e96152007-10-16 23:31:06 -0700985 if (!err && !(inode->i_mode & S_IXUGO))
986 return -EACCES;
987 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700988 }
Miklos Szeredi244f6382007-10-16 23:31:02 -0700989 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700990}
991
992static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
993 void *dstbuf, filldir_t filldir)
994{
995 while (nbytes >= FUSE_NAME_OFFSET) {
996 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
997 size_t reclen = FUSE_DIRENT_SIZE(dirent);
998 int over;
999 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1000 return -EIO;
1001 if (reclen > nbytes)
1002 break;
1003
1004 over = filldir(dstbuf, dirent->name, dirent->namelen,
1005 file->f_pos, dirent->ino, dirent->type);
1006 if (over)
1007 break;
1008
1009 buf += reclen;
1010 nbytes -= reclen;
1011 file->f_pos = dirent->off;
1012 }
1013
1014 return 0;
1015}
1016
Miklos Szeredie5e55582005-09-09 13:10:28 -07001017static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1018{
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001019 int err;
1020 size_t nbytes;
1021 struct page *page;
Josef Sipek7706a9d2006-12-08 02:37:02 -08001022 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001023 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001024 struct fuse_req *req;
1025
1026 if (is_bad_inode(inode))
1027 return -EIO;
1028
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001029 req = fuse_get_req(fc);
1030 if (IS_ERR(req))
1031 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001032
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001033 page = alloc_page(GFP_KERNEL);
1034 if (!page) {
1035 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001036 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001037 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001038 req->out.argpages = 1;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001039 req->num_pages = 1;
1040 req->pages[0] = page;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001041 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
Tejun Heob93f8582008-11-26 12:03:55 +01001042 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -08001043 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001044 err = req->out.h.error;
1045 fuse_put_request(fc, req);
1046 if (!err)
1047 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1048 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001049
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001050 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001051 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001052 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001053}
1054
1055static char *read_link(struct dentry *dentry)
1056{
1057 struct inode *inode = dentry->d_inode;
1058 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001059 struct fuse_req *req = fuse_get_req(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001060 char *link;
1061
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001062 if (IS_ERR(req))
David Howellse231c2e2008-02-07 00:15:26 -08001063 return ERR_CAST(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001064
1065 link = (char *) __get_free_page(GFP_KERNEL);
1066 if (!link) {
1067 link = ERR_PTR(-ENOMEM);
1068 goto out;
1069 }
1070 req->in.h.opcode = FUSE_READLINK;
1071 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001072 req->out.argvar = 1;
1073 req->out.numargs = 1;
1074 req->out.args[0].size = PAGE_SIZE - 1;
1075 req->out.args[0].value = link;
Tejun Heob93f8582008-11-26 12:03:55 +01001076 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001077 if (req->out.h.error) {
1078 free_page((unsigned long) link);
1079 link = ERR_PTR(req->out.h.error);
1080 } else
1081 link[req->out.args[0].size] = '\0';
1082 out:
1083 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001084 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -07001085 return link;
1086}
1087
1088static void free_link(char *link)
1089{
1090 if (!IS_ERR(link))
1091 free_page((unsigned long) link);
1092}
1093
1094static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1095{
1096 nd_set_link(nd, read_link(dentry));
1097 return NULL;
1098}
1099
1100static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1101{
1102 free_link(nd_get_link(nd));
1103}
1104
1105static int fuse_dir_open(struct inode *inode, struct file *file)
1106{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +02001107 return fuse_open_common(inode, file, true);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001108}
1109
1110static int fuse_dir_release(struct inode *inode, struct file *file)
1111{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +02001112 fuse_release_common(file, FUSE_RELEASEDIR);
1113
1114 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001115}
1116
Miklos Szeredi82547982005-09-09 13:10:38 -07001117static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1118{
1119 /* nfsd can call this with no file */
1120 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1121}
1122
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001123static bool update_mtime(unsigned ivalid)
1124{
1125 /* Always update if mtime is explicitly set */
1126 if (ivalid & ATTR_MTIME_SET)
1127 return true;
1128
1129 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1130 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1131 return false;
1132
1133 /* In all other cases update */
1134 return true;
1135}
1136
Miklos Szeredibefc6492005-11-07 00:59:52 -08001137static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001138{
1139 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001140
1141 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001142 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001143 if (ivalid & ATTR_UID)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001144 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001145 if (ivalid & ATTR_GID)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001146 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001147 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001148 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001149 if (ivalid & ATTR_ATIME) {
1150 arg->valid |= FATTR_ATIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001151 arg->atime = iattr->ia_atime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001152 arg->atimensec = iattr->ia_atime.tv_nsec;
1153 if (!(ivalid & ATTR_ATIME_SET))
1154 arg->valid |= FATTR_ATIME_NOW;
1155 }
1156 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1157 arg->valid |= FATTR_MTIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001158 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001159 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1160 if (!(ivalid & ATTR_MTIME_SET))
1161 arg->valid |= FATTR_MTIME_NOW;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001162 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001163}
1164
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001165/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001166 * Prevent concurrent writepages on inode
1167 *
1168 * This is done by adding a negative bias to the inode write counter
1169 * and waiting for all pending writes to finish.
1170 */
1171void fuse_set_nowrite(struct inode *inode)
1172{
1173 struct fuse_conn *fc = get_fuse_conn(inode);
1174 struct fuse_inode *fi = get_fuse_inode(inode);
1175
1176 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1177
1178 spin_lock(&fc->lock);
1179 BUG_ON(fi->writectr < 0);
1180 fi->writectr += FUSE_NOWRITE;
1181 spin_unlock(&fc->lock);
1182 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1183}
1184
1185/*
1186 * Allow writepages on inode
1187 *
1188 * Remove the bias from the writecounter and send any queued
1189 * writepages.
1190 */
1191static void __fuse_release_nowrite(struct inode *inode)
1192{
1193 struct fuse_inode *fi = get_fuse_inode(inode);
1194
1195 BUG_ON(fi->writectr != FUSE_NOWRITE);
1196 fi->writectr = 0;
1197 fuse_flush_writepages(inode);
1198}
1199
1200void fuse_release_nowrite(struct inode *inode)
1201{
1202 struct fuse_conn *fc = get_fuse_conn(inode);
1203
1204 spin_lock(&fc->lock);
1205 __fuse_release_nowrite(inode);
1206 spin_unlock(&fc->lock);
1207}
1208
1209/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001210 * Set attributes, and at the same time refresh them.
1211 *
1212 * Truncation is slightly complicated, because the 'truncate' request
1213 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -07001214 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1215 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001216 */
Miklos Szeredi49d49142007-10-18 03:07:00 -07001217static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1218 struct file *file)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001219{
1220 struct inode *inode = entry->d_inode;
1221 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001222 struct fuse_req *req;
1223 struct fuse_setattr_in inarg;
1224 struct fuse_attr_out outarg;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001225 bool is_truncate = false;
1226 loff_t oldsize;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001227 int err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001228
Miklos Szeredie57ac682007-10-18 03:06:58 -07001229 if (!fuse_allow_task(fc, current))
1230 return -EACCES;
1231
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001232 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1233 err = inode_change_ok(inode, attr);
1234 if (err)
1235 return err;
1236 }
1237
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001238 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1239 return 0;
1240
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001241 if (attr->ia_valid & ATTR_SIZE) {
1242 unsigned long limit;
Miklos Szeredib2d22722006-12-06 20:35:51 -08001243 if (IS_SWAPFILE(inode))
1244 return -ETXTBSY;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001245 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1246 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1247 send_sig(SIGXFSZ, current, 0);
1248 return -EFBIG;
1249 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001250 is_truncate = true;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001251 }
1252
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001253 req = fuse_get_req(fc);
1254 if (IS_ERR(req))
1255 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001256
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001257 if (is_truncate)
1258 fuse_set_nowrite(inode);
1259
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001260 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001261 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -08001262 iattr_to_fattr(attr, &inarg);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001263 if (file) {
1264 struct fuse_file *ff = file->private_data;
1265 inarg.valid |= FATTR_FH;
1266 inarg.fh = ff->fh;
1267 }
Miklos Szeredif3332112007-10-18 03:07:04 -07001268 if (attr->ia_valid & ATTR_SIZE) {
1269 /* For mandatory locking in truncate */
1270 inarg.valid |= FATTR_LOCKOWNER;
1271 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1272 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001273 req->in.h.opcode = FUSE_SETATTR;
1274 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001275 req->in.numargs = 1;
1276 req->in.args[0].size = sizeof(inarg);
1277 req->in.args[0].value = &inarg;
1278 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001279 if (fc->minor < 9)
1280 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1281 else
1282 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001283 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001284 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001285 err = req->out.h.error;
1286 fuse_put_request(fc, req);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001287 if (err) {
1288 if (err == -EINTR)
1289 fuse_invalidate_attr(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001290 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001291 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001292
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001293 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1294 make_bad_inode(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001295 err = -EIO;
1296 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001297 }
1298
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001299 spin_lock(&fc->lock);
1300 fuse_change_attributes_common(inode, &outarg.attr,
1301 attr_timeout(&outarg));
1302 oldsize = inode->i_size;
1303 i_size_write(inode, outarg.attr.size);
1304
1305 if (is_truncate) {
1306 /* NOTE: this may release/reacquire fc->lock */
1307 __fuse_release_nowrite(inode);
1308 }
1309 spin_unlock(&fc->lock);
1310
1311 /*
1312 * Only call invalidate_inode_pages2() after removing
1313 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1314 */
1315 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1316 if (outarg.attr.size < oldsize)
1317 fuse_truncate(inode->i_mapping, outarg.attr.size);
1318 invalidate_inode_pages2(inode->i_mapping);
1319 }
1320
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001321 return 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001322
1323error:
1324 if (is_truncate)
1325 fuse_release_nowrite(inode);
1326
1327 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001328}
1329
Miklos Szeredi49d49142007-10-18 03:07:00 -07001330static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1331{
1332 if (attr->ia_valid & ATTR_FILE)
1333 return fuse_do_setattr(entry, attr, attr->ia_file);
1334 else
1335 return fuse_do_setattr(entry, attr, NULL);
1336}
1337
Miklos Szeredie5e55582005-09-09 13:10:28 -07001338static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1339 struct kstat *stat)
1340{
1341 struct inode *inode = entry->d_inode;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001342 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001343
1344 if (!fuse_allow_task(fc, current))
1345 return -EACCES;
1346
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001347 return fuse_update_attributes(inode, stat, NULL, NULL);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001348}
1349
Miklos Szeredi92a87802005-09-09 13:10:31 -07001350static int fuse_setxattr(struct dentry *entry, const char *name,
1351 const void *value, size_t size, int flags)
1352{
1353 struct inode *inode = entry->d_inode;
1354 struct fuse_conn *fc = get_fuse_conn(inode);
1355 struct fuse_req *req;
1356 struct fuse_setxattr_in inarg;
1357 int err;
1358
Miklos Szeredi92a87802005-09-09 13:10:31 -07001359 if (fc->no_setxattr)
1360 return -EOPNOTSUPP;
1361
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001362 req = fuse_get_req(fc);
1363 if (IS_ERR(req))
1364 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001365
1366 memset(&inarg, 0, sizeof(inarg));
1367 inarg.size = size;
1368 inarg.flags = flags;
1369 req->in.h.opcode = FUSE_SETXATTR;
1370 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001371 req->in.numargs = 3;
1372 req->in.args[0].size = sizeof(inarg);
1373 req->in.args[0].value = &inarg;
1374 req->in.args[1].size = strlen(name) + 1;
1375 req->in.args[1].value = name;
1376 req->in.args[2].size = size;
1377 req->in.args[2].value = value;
Tejun Heob93f8582008-11-26 12:03:55 +01001378 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001379 err = req->out.h.error;
1380 fuse_put_request(fc, req);
1381 if (err == -ENOSYS) {
1382 fc->no_setxattr = 1;
1383 err = -EOPNOTSUPP;
1384 }
1385 return err;
1386}
1387
1388static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1389 void *value, size_t size)
1390{
1391 struct inode *inode = entry->d_inode;
1392 struct fuse_conn *fc = get_fuse_conn(inode);
1393 struct fuse_req *req;
1394 struct fuse_getxattr_in inarg;
1395 struct fuse_getxattr_out outarg;
1396 ssize_t ret;
1397
1398 if (fc->no_getxattr)
1399 return -EOPNOTSUPP;
1400
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001401 req = fuse_get_req(fc);
1402 if (IS_ERR(req))
1403 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001404
1405 memset(&inarg, 0, sizeof(inarg));
1406 inarg.size = size;
1407 req->in.h.opcode = FUSE_GETXATTR;
1408 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001409 req->in.numargs = 2;
1410 req->in.args[0].size = sizeof(inarg);
1411 req->in.args[0].value = &inarg;
1412 req->in.args[1].size = strlen(name) + 1;
1413 req->in.args[1].value = name;
1414 /* This is really two different operations rolled into one */
1415 req->out.numargs = 1;
1416 if (size) {
1417 req->out.argvar = 1;
1418 req->out.args[0].size = size;
1419 req->out.args[0].value = value;
1420 } else {
1421 req->out.args[0].size = sizeof(outarg);
1422 req->out.args[0].value = &outarg;
1423 }
Tejun Heob93f8582008-11-26 12:03:55 +01001424 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001425 ret = req->out.h.error;
1426 if (!ret)
1427 ret = size ? req->out.args[0].size : outarg.size;
1428 else {
1429 if (ret == -ENOSYS) {
1430 fc->no_getxattr = 1;
1431 ret = -EOPNOTSUPP;
1432 }
1433 }
1434 fuse_put_request(fc, req);
1435 return ret;
1436}
1437
1438static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1439{
1440 struct inode *inode = entry->d_inode;
1441 struct fuse_conn *fc = get_fuse_conn(inode);
1442 struct fuse_req *req;
1443 struct fuse_getxattr_in inarg;
1444 struct fuse_getxattr_out outarg;
1445 ssize_t ret;
1446
Miklos Szeredie57ac682007-10-18 03:06:58 -07001447 if (!fuse_allow_task(fc, current))
1448 return -EACCES;
1449
Miklos Szeredi92a87802005-09-09 13:10:31 -07001450 if (fc->no_listxattr)
1451 return -EOPNOTSUPP;
1452
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001453 req = fuse_get_req(fc);
1454 if (IS_ERR(req))
1455 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001456
1457 memset(&inarg, 0, sizeof(inarg));
1458 inarg.size = size;
1459 req->in.h.opcode = FUSE_LISTXATTR;
1460 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001461 req->in.numargs = 1;
1462 req->in.args[0].size = sizeof(inarg);
1463 req->in.args[0].value = &inarg;
1464 /* This is really two different operations rolled into one */
1465 req->out.numargs = 1;
1466 if (size) {
1467 req->out.argvar = 1;
1468 req->out.args[0].size = size;
1469 req->out.args[0].value = list;
1470 } else {
1471 req->out.args[0].size = sizeof(outarg);
1472 req->out.args[0].value = &outarg;
1473 }
Tejun Heob93f8582008-11-26 12:03:55 +01001474 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001475 ret = req->out.h.error;
1476 if (!ret)
1477 ret = size ? req->out.args[0].size : outarg.size;
1478 else {
1479 if (ret == -ENOSYS) {
1480 fc->no_listxattr = 1;
1481 ret = -EOPNOTSUPP;
1482 }
1483 }
1484 fuse_put_request(fc, req);
1485 return ret;
1486}
1487
1488static int fuse_removexattr(struct dentry *entry, const char *name)
1489{
1490 struct inode *inode = entry->d_inode;
1491 struct fuse_conn *fc = get_fuse_conn(inode);
1492 struct fuse_req *req;
1493 int err;
1494
1495 if (fc->no_removexattr)
1496 return -EOPNOTSUPP;
1497
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001498 req = fuse_get_req(fc);
1499 if (IS_ERR(req))
1500 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001501
1502 req->in.h.opcode = FUSE_REMOVEXATTR;
1503 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001504 req->in.numargs = 1;
1505 req->in.args[0].size = strlen(name) + 1;
1506 req->in.args[0].value = name;
Tejun Heob93f8582008-11-26 12:03:55 +01001507 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001508 err = req->out.h.error;
1509 fuse_put_request(fc, req);
1510 if (err == -ENOSYS) {
1511 fc->no_removexattr = 1;
1512 err = -EOPNOTSUPP;
1513 }
1514 return err;
1515}
1516
Arjan van de Ven754661f2007-02-12 00:55:38 -08001517static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07001518 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001519 .mkdir = fuse_mkdir,
1520 .symlink = fuse_symlink,
1521 .unlink = fuse_unlink,
1522 .rmdir = fuse_rmdir,
1523 .rename = fuse_rename,
1524 .link = fuse_link,
1525 .setattr = fuse_setattr,
1526 .create = fuse_create,
1527 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001528 .permission = fuse_permission,
1529 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001530 .setxattr = fuse_setxattr,
1531 .getxattr = fuse_getxattr,
1532 .listxattr = fuse_listxattr,
1533 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001534};
1535
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001536static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001537 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001538 .read = generic_read_dir,
1539 .readdir = fuse_readdir,
1540 .open = fuse_dir_open,
1541 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001542 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001543};
1544
Arjan van de Ven754661f2007-02-12 00:55:38 -08001545static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001546 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001547 .permission = fuse_permission,
1548 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001549 .setxattr = fuse_setxattr,
1550 .getxattr = fuse_getxattr,
1551 .listxattr = fuse_listxattr,
1552 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001553};
1554
Arjan van de Ven754661f2007-02-12 00:55:38 -08001555static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001556 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001557 .follow_link = fuse_follow_link,
1558 .put_link = fuse_put_link,
1559 .readlink = generic_readlink,
1560 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001561 .setxattr = fuse_setxattr,
1562 .getxattr = fuse_getxattr,
1563 .listxattr = fuse_listxattr,
1564 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001565};
1566
1567void fuse_init_common(struct inode *inode)
1568{
1569 inode->i_op = &fuse_common_inode_operations;
1570}
1571
1572void fuse_init_dir(struct inode *inode)
1573{
1574 inode->i_op = &fuse_dir_inode_operations;
1575 inode->i_fop = &fuse_dir_operations;
1576}
1577
1578void fuse_init_symlink(struct inode *inode)
1579{
1580 inode->i_op = &fuse_symlink_inode_operations;
1581}