blob: dfc32dc97f7fd0d333e27c2b09b6e8fc4151f8ed [file] [log] [blame]
Miklos Szeredie5e55582005-09-09 13:10:28 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07003 Copyright (C) 2001-2006 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 Szeredi8cbdf1e2006-01-06 00:19:38 -0800100static void fuse_invalidate_entry_cache(struct dentry *entry)
101{
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 Szeredie5e55582005-09-09 13:10:28 -0700115static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
116 struct dentry *entry,
117 struct fuse_entry_out *outarg)
118{
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700119 struct fuse_conn *fc = get_fuse_conn(dir);
120
121 memset(outarg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700122 req->in.h.opcode = FUSE_LOOKUP;
123 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700124 req->in.numargs = 1;
125 req->in.args[0].size = entry->d_name.len + 1;
126 req->in.args[0].value = entry->d_name.name;
127 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700128 if (fc->minor < 9)
129 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
130 else
131 req->out.args[0].size = sizeof(struct fuse_entry_out);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700132 req->out.args[0].value = outarg;
133}
134
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800135static u64 fuse_get_attr_version(struct fuse_conn *fc)
136{
137 u64 curr_version;
138
139 /*
140 * The spin lock isn't actually needed on 64bit archs, but we
141 * don't yet care too much about such optimizations.
142 */
143 spin_lock(&fc->lock);
144 curr_version = fc->attr_version;
145 spin_unlock(&fc->lock);
146
147 return curr_version;
148}
149
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800150/*
151 * Check whether the dentry is still valid
152 *
153 * If the entry validity timeout has expired and the dentry is
154 * positive, try to redo the lookup. If the lookup results in a
155 * different inode, then let the VFS invalidate the dentry and redo
156 * the lookup once more. If the lookup results in the same inode,
157 * then refresh the attributes, timeouts and mark the dentry valid.
158 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700159static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
160{
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800161 struct inode *inode = entry->d_inode;
162
163 if (inode && is_bad_inode(inode))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700164 return 0;
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700165 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700166 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700167 struct fuse_entry_out outarg;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800168 struct fuse_conn *fc;
169 struct fuse_req *req;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800170 struct fuse_req *forget_req;
Miklos Szeredie956edd2006-10-17 00:10:12 -0700171 struct dentry *parent;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700172 u64 attr_version;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800173
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800174 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800175 if (!inode)
176 return 0;
177
178 fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700179 req = fuse_get_req(fc);
180 if (IS_ERR(req))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700181 return 0;
182
Miklos Szeredi2d510132006-11-25 11:09:20 -0800183 forget_req = fuse_get_req(fc);
184 if (IS_ERR(forget_req)) {
185 fuse_put_request(fc, req);
186 return 0;
187 }
188
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800189 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700190
Miklos Szeredie956edd2006-10-17 00:10:12 -0700191 parent = dget_parent(entry);
192 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700193 request_send(fc, req);
Miklos Szeredie956edd2006-10-17 00:10:12 -0700194 dput(parent);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700195 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800196 fuse_put_request(fc, req);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800197 /* Zero nodeid is same as -ENOENT */
198 if (!err && !outarg.nodeid)
199 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700200 if (!err) {
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800201 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700202 if (outarg.nodeid != get_node_id(inode)) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800203 fuse_send_forget(fc, forget_req,
204 outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700205 return 0;
206 }
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700207 spin_lock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700208 fi->nlookup ++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700209 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700210 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800211 fuse_put_request(fc, forget_req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700212 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700213 return 0;
214
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700215 fuse_change_attributes(inode, &outarg.attr,
216 entry_attr_timeout(&outarg),
217 attr_version);
218 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700219 }
220 return 1;
221}
222
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800223static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800224{
225 return !nodeid || nodeid == FUSE_ROOT_ID;
226}
227
Miklos Szeredie5e55582005-09-09 13:10:28 -0700228static struct dentry_operations fuse_dentry_operations = {
229 .d_revalidate = fuse_dentry_revalidate,
230};
231
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700232int fuse_valid_type(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800233{
234 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
235 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
236}
237
Miklos Szeredid2a85162006-10-17 00:10:11 -0700238/*
239 * Add a directory inode to a dentry, ensuring that no other dentry
240 * refers to this inode. Called with fc->inst_mutex.
241 */
242static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
243{
244 struct dentry *alias = d_find_alias(inode);
245 if (alias) {
246 /* This tries to shrink the subtree below alias */
247 fuse_invalidate_entry(alias);
248 dput(alias);
249 if (!list_empty(&inode->i_dentry))
250 return -EBUSY;
251 }
252 d_add(entry, inode);
253 return 0;
254}
255
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800256static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
257 struct nameidata *nd)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700258{
259 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700260 struct fuse_entry_out outarg;
261 struct inode *inode = NULL;
262 struct fuse_conn *fc = get_fuse_conn(dir);
263 struct fuse_req *req;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800264 struct fuse_req *forget_req;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700265 u64 attr_version;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700266
267 if (entry->d_name.len > FUSE_NAME_MAX)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800268 return ERR_PTR(-ENAMETOOLONG);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700269
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700270 req = fuse_get_req(fc);
271 if (IS_ERR(req))
272 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700273
Miklos Szeredi2d510132006-11-25 11:09:20 -0800274 forget_req = fuse_get_req(fc);
275 if (IS_ERR(forget_req)) {
276 fuse_put_request(fc, req);
277 return ERR_PTR(PTR_ERR(forget_req));
278 }
279
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800280 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700281
Miklos Szeredie5e55582005-09-09 13:10:28 -0700282 fuse_lookup_init(req, dir, entry, &outarg);
283 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700284 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800285 fuse_put_request(fc, req);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800286 /* Zero nodeid is same as -ENOENT, but with valid timeout */
287 if (!err && outarg.nodeid &&
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700288 (invalid_nodeid(outarg.nodeid) ||
289 !fuse_valid_type(outarg.attr.mode)))
Miklos Szerediee4e5272005-09-27 21:45:21 -0700290 err = -EIO;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800291 if (!err && outarg.nodeid) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700292 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700293 &outarg.attr, entry_attr_timeout(&outarg),
294 attr_version);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700295 if (!inode) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800296 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800297 return ERR_PTR(-ENOMEM);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700298 }
299 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800300 fuse_put_request(fc, forget_req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700301 if (err && err != -ENOENT)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800302 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700303
Miklos Szeredid2a85162006-10-17 00:10:11 -0700304 if (inode && S_ISDIR(inode->i_mode)) {
305 mutex_lock(&fc->inst_mutex);
306 err = fuse_d_add_directory(entry, inode);
307 mutex_unlock(&fc->inst_mutex);
308 if (err) {
309 iput(inode);
310 return ERR_PTR(err);
311 }
312 } else
313 d_add(entry, inode);
314
Miklos Szeredie5e55582005-09-09 13:10:28 -0700315 entry->d_op = &fuse_dentry_operations;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800316 if (!err)
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700317 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800318 else
319 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800320 return NULL;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700321}
322
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800323/*
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700324 * Synchronous release for the case when something goes wrong in CREATE_OPEN
325 */
326static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
327 u64 nodeid, int flags)
328{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700329 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
330 ff->reserved_req->force = 1;
331 request_send(fc, ff->reserved_req);
332 fuse_put_request(fc, ff->reserved_req);
333 kfree(ff);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700334}
335
336/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800337 * Atomic create+open operation
338 *
339 * If the filesystem doesn't support this, then fall back to separate
340 * 'mknod' + 'open' requests.
341 */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800342static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
343 struct nameidata *nd)
344{
345 int err;
346 struct inode *inode;
347 struct fuse_conn *fc = get_fuse_conn(dir);
348 struct fuse_req *req;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700349 struct fuse_req *forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800350 struct fuse_open_in inarg;
351 struct fuse_open_out outopen;
352 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800353 struct fuse_file *ff;
354 struct file *file;
355 int flags = nd->intent.open.flags - 1;
356
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800357 if (fc->no_create)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700358 return -ENOSYS;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800359
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700360 forget_req = fuse_get_req(fc);
361 if (IS_ERR(forget_req))
362 return PTR_ERR(forget_req);
363
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700364 req = fuse_get_req(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700365 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700366 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700367 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800368
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700369 err = -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800370 ff = fuse_file_alloc();
371 if (!ff)
372 goto out_put_request;
373
374 flags &= ~O_NOCTTY;
375 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700376 memset(&outentry, 0, sizeof(outentry));
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800377 inarg.flags = flags;
378 inarg.mode = mode;
379 req->in.h.opcode = FUSE_CREATE;
380 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800381 req->in.numargs = 2;
382 req->in.args[0].size = sizeof(inarg);
383 req->in.args[0].value = &inarg;
384 req->in.args[1].size = entry->d_name.len + 1;
385 req->in.args[1].value = entry->d_name.name;
386 req->out.numargs = 2;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700387 if (fc->minor < 9)
388 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
389 else
390 req->out.args[0].size = sizeof(outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800391 req->out.args[0].value = &outentry;
392 req->out.args[1].size = sizeof(outopen);
393 req->out.args[1].value = &outopen;
394 request_send(fc, req);
395 err = req->out.h.error;
396 if (err) {
397 if (err == -ENOSYS)
398 fc->no_create = 1;
399 goto out_free_ff;
400 }
401
402 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800403 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800404 goto out_free_ff;
405
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700406 fuse_put_request(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800407 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700408 &outentry.attr, entry_attr_timeout(&outentry), 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800409 if (!inode) {
410 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
411 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700412 fuse_sync_release(fc, ff, outentry.nodeid, flags);
413 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
414 return -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800415 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700416 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800417 d_instantiate(entry, inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700418 fuse_change_entry_timeout(entry, &outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800419 file = lookup_instantiate_filp(nd, entry, generic_file_open);
420 if (IS_ERR(file)) {
421 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700422 fuse_sync_release(fc, ff, outentry.nodeid, flags);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800423 return PTR_ERR(file);
424 }
425 fuse_finish_open(inode, file, ff, &outopen);
426 return 0;
427
428 out_free_ff:
429 fuse_file_free(ff);
430 out_put_request:
431 fuse_put_request(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700432 out_put_forget_req:
433 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800434 return err;
435}
436
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800437/*
438 * Code shared between mknod, mkdir, symlink and link
439 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700440static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
441 struct inode *dir, struct dentry *entry,
442 int mode)
443{
444 struct fuse_entry_out outarg;
445 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700446 int err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800447 struct fuse_req *forget_req;
448
449 forget_req = fuse_get_req(fc);
450 if (IS_ERR(forget_req)) {
451 fuse_put_request(fc, req);
452 return PTR_ERR(forget_req);
453 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700454
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700455 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700456 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700457 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700458 if (fc->minor < 9)
459 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
460 else
461 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700462 req->out.args[0].value = &outarg;
463 request_send(fc, req);
464 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800465 fuse_put_request(fc, req);
466 if (err)
467 goto out_put_forget_req;
468
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800469 err = -EIO;
470 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800471 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800472
473 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800474 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800475
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700476 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700477 &outarg.attr, entry_attr_timeout(&outarg), 0);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700478 if (!inode) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800479 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700480 return -ENOMEM;
481 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800482 fuse_put_request(fc, forget_req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700483
Miklos Szeredid2a85162006-10-17 00:10:11 -0700484 if (S_ISDIR(inode->i_mode)) {
485 struct dentry *alias;
486 mutex_lock(&fc->inst_mutex);
487 alias = d_find_alias(inode);
488 if (alias) {
489 /* New directory must have moved since mkdir */
490 mutex_unlock(&fc->inst_mutex);
491 dput(alias);
492 iput(inode);
493 return -EBUSY;
494 }
495 d_instantiate(entry, inode);
496 mutex_unlock(&fc->inst_mutex);
497 } else
498 d_instantiate(entry, inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700499
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700500 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700501 fuse_invalidate_attr(dir);
502 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800503
Miklos Szeredi2d510132006-11-25 11:09:20 -0800504 out_put_forget_req:
505 fuse_put_request(fc, forget_req);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800506 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700507}
508
509static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
510 dev_t rdev)
511{
512 struct fuse_mknod_in inarg;
513 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700514 struct fuse_req *req = fuse_get_req(fc);
515 if (IS_ERR(req))
516 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700517
518 memset(&inarg, 0, sizeof(inarg));
519 inarg.mode = mode;
520 inarg.rdev = new_encode_dev(rdev);
521 req->in.h.opcode = FUSE_MKNOD;
522 req->in.numargs = 2;
523 req->in.args[0].size = sizeof(inarg);
524 req->in.args[0].value = &inarg;
525 req->in.args[1].size = entry->d_name.len + 1;
526 req->in.args[1].value = entry->d_name.name;
527 return create_new_entry(fc, req, dir, entry, mode);
528}
529
530static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
531 struct nameidata *nd)
532{
Miklos Szeredib9ba3472007-05-23 13:57:32 -0700533 if (nd && (nd->flags & LOOKUP_OPEN)) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800534 int err = fuse_create_open(dir, entry, mode, nd);
535 if (err != -ENOSYS)
536 return err;
537 /* Fall back on mknod */
538 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700539 return fuse_mknod(dir, entry, mode, 0);
540}
541
542static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
543{
544 struct fuse_mkdir_in inarg;
545 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700546 struct fuse_req *req = fuse_get_req(fc);
547 if (IS_ERR(req))
548 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700549
550 memset(&inarg, 0, sizeof(inarg));
551 inarg.mode = mode;
552 req->in.h.opcode = FUSE_MKDIR;
553 req->in.numargs = 2;
554 req->in.args[0].size = sizeof(inarg);
555 req->in.args[0].value = &inarg;
556 req->in.args[1].size = entry->d_name.len + 1;
557 req->in.args[1].value = entry->d_name.name;
558 return create_new_entry(fc, req, dir, entry, S_IFDIR);
559}
560
561static int fuse_symlink(struct inode *dir, struct dentry *entry,
562 const char *link)
563{
564 struct fuse_conn *fc = get_fuse_conn(dir);
565 unsigned len = strlen(link) + 1;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700566 struct fuse_req *req = fuse_get_req(fc);
567 if (IS_ERR(req))
568 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700569
570 req->in.h.opcode = FUSE_SYMLINK;
571 req->in.numargs = 2;
572 req->in.args[0].size = entry->d_name.len + 1;
573 req->in.args[0].value = entry->d_name.name;
574 req->in.args[1].size = len;
575 req->in.args[1].value = link;
576 return create_new_entry(fc, req, dir, entry, S_IFLNK);
577}
578
579static int fuse_unlink(struct inode *dir, struct dentry *entry)
580{
581 int err;
582 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700583 struct fuse_req *req = fuse_get_req(fc);
584 if (IS_ERR(req))
585 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700586
587 req->in.h.opcode = FUSE_UNLINK;
588 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700589 req->in.numargs = 1;
590 req->in.args[0].size = entry->d_name.len + 1;
591 req->in.args[0].value = entry->d_name.name;
592 request_send(fc, req);
593 err = req->out.h.error;
594 fuse_put_request(fc, req);
595 if (!err) {
596 struct inode *inode = entry->d_inode;
597
598 /* Set nlink to zero so the inode can be cleared, if
599 the inode does have more links this will be
600 discovered at the next lookup/getattr */
Dave Hansence71ec32006-09-30 23:29:06 -0700601 clear_nlink(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700602 fuse_invalidate_attr(inode);
603 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800604 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700605 } else if (err == -EINTR)
606 fuse_invalidate_entry(entry);
607 return err;
608}
609
610static int fuse_rmdir(struct inode *dir, struct dentry *entry)
611{
612 int err;
613 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700614 struct fuse_req *req = fuse_get_req(fc);
615 if (IS_ERR(req))
616 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700617
618 req->in.h.opcode = FUSE_RMDIR;
619 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700620 req->in.numargs = 1;
621 req->in.args[0].size = entry->d_name.len + 1;
622 req->in.args[0].value = entry->d_name.name;
623 request_send(fc, req);
624 err = req->out.h.error;
625 fuse_put_request(fc, req);
626 if (!err) {
Dave Hansence71ec32006-09-30 23:29:06 -0700627 clear_nlink(entry->d_inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700628 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800629 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700630 } else if (err == -EINTR)
631 fuse_invalidate_entry(entry);
632 return err;
633}
634
635static int fuse_rename(struct inode *olddir, struct dentry *oldent,
636 struct inode *newdir, struct dentry *newent)
637{
638 int err;
639 struct fuse_rename_in inarg;
640 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700641 struct fuse_req *req = fuse_get_req(fc);
642 if (IS_ERR(req))
643 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700644
645 memset(&inarg, 0, sizeof(inarg));
646 inarg.newdir = get_node_id(newdir);
647 req->in.h.opcode = FUSE_RENAME;
648 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700649 req->in.numargs = 3;
650 req->in.args[0].size = sizeof(inarg);
651 req->in.args[0].value = &inarg;
652 req->in.args[1].size = oldent->d_name.len + 1;
653 req->in.args[1].value = oldent->d_name.name;
654 req->in.args[2].size = newent->d_name.len + 1;
655 req->in.args[2].value = newent->d_name.name;
656 request_send(fc, req);
657 err = req->out.h.error;
658 fuse_put_request(fc, req);
659 if (!err) {
660 fuse_invalidate_attr(olddir);
661 if (olddir != newdir)
662 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800663
664 /* newent will end up negative */
665 if (newent->d_inode)
666 fuse_invalidate_entry_cache(newent);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700667 } else if (err == -EINTR) {
668 /* If request was interrupted, DEITY only knows if the
669 rename actually took place. If the invalidation
670 fails (e.g. some process has CWD under the renamed
671 directory), then there can be inconsistency between
672 the dcache and the real filesystem. Tough luck. */
673 fuse_invalidate_entry(oldent);
674 if (newent->d_inode)
675 fuse_invalidate_entry(newent);
676 }
677
678 return err;
679}
680
681static int fuse_link(struct dentry *entry, struct inode *newdir,
682 struct dentry *newent)
683{
684 int err;
685 struct fuse_link_in inarg;
686 struct inode *inode = entry->d_inode;
687 struct fuse_conn *fc = get_fuse_conn(inode);
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.oldnodeid = get_node_id(inode);
694 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700695 req->in.numargs = 2;
696 req->in.args[0].size = sizeof(inarg);
697 req->in.args[0].value = &inarg;
698 req->in.args[1].size = newent->d_name.len + 1;
699 req->in.args[1].value = newent->d_name.name;
700 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
701 /* Contrary to "normal" filesystems it can happen that link
702 makes two "logical" inodes point to the same "physical"
703 inode. We invalidate the attributes of the old one, so it
704 will reflect changes in the backing inode (link count,
705 etc.)
706 */
707 if (!err || err == -EINTR)
708 fuse_invalidate_attr(inode);
709 return err;
710}
711
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700712static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
713 struct kstat *stat)
714{
715 stat->dev = inode->i_sb->s_dev;
716 stat->ino = attr->ino;
717 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
718 stat->nlink = attr->nlink;
719 stat->uid = attr->uid;
720 stat->gid = attr->gid;
721 stat->rdev = inode->i_rdev;
722 stat->atime.tv_sec = attr->atime;
723 stat->atime.tv_nsec = attr->atimensec;
724 stat->mtime.tv_sec = attr->mtime;
725 stat->mtime.tv_nsec = attr->mtimensec;
726 stat->ctime.tv_sec = attr->ctime;
727 stat->ctime.tv_nsec = attr->ctimensec;
728 stat->size = attr->size;
729 stat->blocks = attr->blocks;
730 stat->blksize = (1 << inode->i_blkbits);
731}
732
Miklos Szeredic79e3222007-10-18 03:06:59 -0700733static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
734 struct file *file)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700735{
736 int err;
Miklos Szeredic79e3222007-10-18 03:06:59 -0700737 struct fuse_getattr_in inarg;
738 struct fuse_attr_out outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700739 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700740 struct fuse_req *req;
741 u64 attr_version;
742
743 req = fuse_get_req(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700744 if (IS_ERR(req))
745 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700746
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800747 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700748
Miklos Szeredic79e3222007-10-18 03:06:59 -0700749 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700750 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredic79e3222007-10-18 03:06:59 -0700751 /* Directories have separate file-handle space */
752 if (file && S_ISREG(inode->i_mode)) {
753 struct fuse_file *ff = file->private_data;
754
755 inarg.getattr_flags |= FUSE_GETATTR_FH;
756 inarg.fh = ff->fh;
757 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700758 req->in.h.opcode = FUSE_GETATTR;
759 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700760 req->in.numargs = 1;
761 req->in.args[0].size = sizeof(inarg);
762 req->in.args[0].value = &inarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700763 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700764 if (fc->minor < 9)
765 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
766 else
767 req->out.args[0].size = sizeof(outarg);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700768 req->out.args[0].value = &outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700769 request_send(fc, req);
770 err = req->out.h.error;
771 fuse_put_request(fc, req);
772 if (!err) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700773 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700774 make_bad_inode(inode);
775 err = -EIO;
776 } else {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700777 fuse_change_attributes(inode, &outarg.attr,
778 attr_timeout(&outarg),
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700779 attr_version);
780 if (stat)
Miklos Szeredic79e3222007-10-18 03:06:59 -0700781 fuse_fillattr(inode, &outarg.attr, stat);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700782 }
783 }
784 return err;
785}
786
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800787int fuse_update_attributes(struct inode *inode, struct kstat *stat,
788 struct file *file, bool *refreshed)
789{
790 struct fuse_inode *fi = get_fuse_inode(inode);
791 int err;
792 bool r;
793
794 if (fi->i_time < get_jiffies_64()) {
795 r = true;
796 err = fuse_do_getattr(inode, stat, file);
797 } else {
798 r = false;
799 err = 0;
800 if (stat) {
801 generic_fillattr(inode, stat);
802 stat->mode = fi->orig_i_mode;
803 }
804 }
805
806 if (refreshed != NULL)
807 *refreshed = r;
808
809 return err;
810}
811
Miklos Szeredi87729a52005-09-09 13:10:34 -0700812/*
813 * Calling into a user-controlled filesystem gives the filesystem
814 * daemon ptrace-like capabilities over the requester process. This
815 * means, that the filesystem daemon is able to record the exact
816 * filesystem operations performed, and can also control the behavior
817 * of the requester process in otherwise impossible ways. For example
818 * it can delay the operation for arbitrary length of time allowing
819 * DoS against the requester.
820 *
821 * For this reason only those processes can call into the filesystem,
822 * for which the owner of the mount has ptrace privilege. This
823 * excludes processes started by other users, suid or sgid processes.
824 */
Miklos Szeredie57ac682007-10-18 03:06:58 -0700825int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
Miklos Szeredi87729a52005-09-09 13:10:34 -0700826{
827 if (fc->flags & FUSE_ALLOW_OTHER)
828 return 1;
829
830 if (task->euid == fc->user_id &&
831 task->suid == fc->user_id &&
832 task->uid == fc->user_id &&
833 task->egid == fc->group_id &&
834 task->sgid == fc->group_id &&
835 task->gid == fc->group_id)
836 return 1;
837
838 return 0;
839}
840
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800841static int fuse_access(struct inode *inode, int mask)
842{
843 struct fuse_conn *fc = get_fuse_conn(inode);
844 struct fuse_req *req;
845 struct fuse_access_in inarg;
846 int err;
847
848 if (fc->no_access)
849 return 0;
850
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700851 req = fuse_get_req(fc);
852 if (IS_ERR(req))
853 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800854
855 memset(&inarg, 0, sizeof(inarg));
856 inarg.mask = mask;
857 req->in.h.opcode = FUSE_ACCESS;
858 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800859 req->in.numargs = 1;
860 req->in.args[0].size = sizeof(inarg);
861 req->in.args[0].value = &inarg;
862 request_send(fc, req);
863 err = req->out.h.error;
864 fuse_put_request(fc, req);
865 if (err == -ENOSYS) {
866 fc->no_access = 1;
867 err = 0;
868 }
869 return err;
870}
871
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800872/*
873 * Check permission. The two basic access models of FUSE are:
874 *
875 * 1) Local access checking ('default_permissions' mount option) based
876 * on file mode. This is the plain old disk filesystem permission
877 * modell.
878 *
879 * 2) "Remote" access checking, where server is responsible for
880 * checking permission in each inode operation. An exception to this
881 * is if ->permission() was invoked from sys_access() in which case an
882 * access request is sent. Execute permission is still checked
883 * locally based on file mode.
884 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700885static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
886{
887 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -0700888 bool refreshed = false;
889 int err = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700890
Miklos Szeredi87729a52005-09-09 13:10:34 -0700891 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700892 return -EACCES;
Miklos Szeredi244f6382007-10-16 23:31:02 -0700893
894 /*
Miklos Szeredie8e96152007-10-16 23:31:06 -0700895 * If attributes are needed, refresh them before proceeding
Miklos Szeredi244f6382007-10-16 23:31:02 -0700896 */
Miklos Szeredie8e96152007-10-16 23:31:06 -0700897 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
898 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800899 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
900 if (err)
901 return err;
Miklos Szeredi244f6382007-10-16 23:31:02 -0700902 }
903
904 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700905 int err = generic_permission(inode, mask, NULL);
906
907 /* If permission is denied, try to refresh file
908 attributes. This is also needed, because the root
909 node will at first have no permissions */
Miklos Szeredi244f6382007-10-16 23:31:02 -0700910 if (err == -EACCES && !refreshed) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700911 err = fuse_do_getattr(inode, NULL, NULL);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700912 if (!err)
913 err = generic_permission(inode, mask, NULL);
914 }
915
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800916 /* Note: the opposite of the above test does not
917 exist. So if permissions are revoked this won't be
918 noticed immediately, only after the attribute
919 timeout has expired */
Miklos Szeredie8e96152007-10-16 23:31:06 -0700920 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
921 err = fuse_access(inode, mask);
922 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
923 if (!(inode->i_mode & S_IXUGO)) {
924 if (refreshed)
925 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700926
Miklos Szeredic79e3222007-10-18 03:06:59 -0700927 err = fuse_do_getattr(inode, NULL, NULL);
Miklos Szeredie8e96152007-10-16 23:31:06 -0700928 if (!err && !(inode->i_mode & S_IXUGO))
929 return -EACCES;
930 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700931 }
Miklos Szeredi244f6382007-10-16 23:31:02 -0700932 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700933}
934
935static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
936 void *dstbuf, filldir_t filldir)
937{
938 while (nbytes >= FUSE_NAME_OFFSET) {
939 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
940 size_t reclen = FUSE_DIRENT_SIZE(dirent);
941 int over;
942 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
943 return -EIO;
944 if (reclen > nbytes)
945 break;
946
947 over = filldir(dstbuf, dirent->name, dirent->namelen,
948 file->f_pos, dirent->ino, dirent->type);
949 if (over)
950 break;
951
952 buf += reclen;
953 nbytes -= reclen;
954 file->f_pos = dirent->off;
955 }
956
957 return 0;
958}
959
Miklos Szeredie5e55582005-09-09 13:10:28 -0700960static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
961{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700962 int err;
963 size_t nbytes;
964 struct page *page;
Josef Sipek7706a9d2006-12-08 02:37:02 -0800965 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700966 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800967 struct fuse_req *req;
968
969 if (is_bad_inode(inode))
970 return -EIO;
971
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700972 req = fuse_get_req(fc);
973 if (IS_ERR(req))
974 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700975
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700976 page = alloc_page(GFP_KERNEL);
977 if (!page) {
978 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700979 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700980 }
981 req->num_pages = 1;
982 req->pages[0] = page;
Miklos Szeredia6643092007-11-28 16:22:00 -0800983 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800984 request_send(fc, req);
985 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700986 err = req->out.h.error;
987 fuse_put_request(fc, req);
988 if (!err)
989 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
990 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700991
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700992 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700993 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700994 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700995}
996
997static char *read_link(struct dentry *dentry)
998{
999 struct inode *inode = dentry->d_inode;
1000 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001001 struct fuse_req *req = fuse_get_req(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001002 char *link;
1003
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001004 if (IS_ERR(req))
1005 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -07001006
1007 link = (char *) __get_free_page(GFP_KERNEL);
1008 if (!link) {
1009 link = ERR_PTR(-ENOMEM);
1010 goto out;
1011 }
1012 req->in.h.opcode = FUSE_READLINK;
1013 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001014 req->out.argvar = 1;
1015 req->out.numargs = 1;
1016 req->out.args[0].size = PAGE_SIZE - 1;
1017 req->out.args[0].value = link;
1018 request_send(fc, req);
1019 if (req->out.h.error) {
1020 free_page((unsigned long) link);
1021 link = ERR_PTR(req->out.h.error);
1022 } else
1023 link[req->out.args[0].size] = '\0';
1024 out:
1025 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001026 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -07001027 return link;
1028}
1029
1030static void free_link(char *link)
1031{
1032 if (!IS_ERR(link))
1033 free_page((unsigned long) link);
1034}
1035
1036static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1037{
1038 nd_set_link(nd, read_link(dentry));
1039 return NULL;
1040}
1041
1042static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1043{
1044 free_link(nd_get_link(nd));
1045}
1046
1047static int fuse_dir_open(struct inode *inode, struct file *file)
1048{
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001049 return fuse_open_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001050}
1051
1052static int fuse_dir_release(struct inode *inode, struct file *file)
1053{
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001054 return fuse_release_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001055}
1056
Miklos Szeredi82547982005-09-09 13:10:38 -07001057static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1058{
1059 /* nfsd can call this with no file */
1060 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1061}
1062
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001063static bool update_mtime(unsigned ivalid)
1064{
1065 /* Always update if mtime is explicitly set */
1066 if (ivalid & ATTR_MTIME_SET)
1067 return true;
1068
1069 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1070 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1071 return false;
1072
1073 /* In all other cases update */
1074 return true;
1075}
1076
Miklos Szeredibefc6492005-11-07 00:59:52 -08001077static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001078{
1079 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001080
1081 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001082 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001083 if (ivalid & ATTR_UID)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001084 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001085 if (ivalid & ATTR_GID)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001086 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001087 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001088 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001089 if (ivalid & ATTR_ATIME) {
1090 arg->valid |= FATTR_ATIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001091 arg->atime = iattr->ia_atime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001092 arg->atimensec = iattr->ia_atime.tv_nsec;
1093 if (!(ivalid & ATTR_ATIME_SET))
1094 arg->valid |= FATTR_ATIME_NOW;
1095 }
1096 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1097 arg->valid |= FATTR_MTIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001098 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001099 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1100 if (!(ivalid & ATTR_MTIME_SET))
1101 arg->valid |= FATTR_MTIME_NOW;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001102 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001103}
1104
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001105/*
1106 * Set attributes, and at the same time refresh them.
1107 *
1108 * Truncation is slightly complicated, because the 'truncate' request
1109 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -07001110 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1111 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001112 */
Miklos Szeredi49d49142007-10-18 03:07:00 -07001113static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1114 struct file *file)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001115{
1116 struct inode *inode = entry->d_inode;
1117 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001118 struct fuse_req *req;
1119 struct fuse_setattr_in inarg;
1120 struct fuse_attr_out outarg;
1121 int err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001122
Miklos Szeredie57ac682007-10-18 03:06:58 -07001123 if (!fuse_allow_task(fc, current))
1124 return -EACCES;
1125
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001126 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1127 err = inode_change_ok(inode, attr);
1128 if (err)
1129 return err;
1130 }
1131
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001132 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1133 return 0;
1134
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001135 if (attr->ia_valid & ATTR_SIZE) {
1136 unsigned long limit;
Miklos Szeredib2d22722006-12-06 20:35:51 -08001137 if (IS_SWAPFILE(inode))
1138 return -ETXTBSY;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001139 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1140 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1141 send_sig(SIGXFSZ, current, 0);
1142 return -EFBIG;
1143 }
1144 }
1145
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001146 req = fuse_get_req(fc);
1147 if (IS_ERR(req))
1148 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001149
1150 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001151 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -08001152 iattr_to_fattr(attr, &inarg);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001153 if (file) {
1154 struct fuse_file *ff = file->private_data;
1155 inarg.valid |= FATTR_FH;
1156 inarg.fh = ff->fh;
1157 }
Miklos Szeredif3332112007-10-18 03:07:04 -07001158 if (attr->ia_valid & ATTR_SIZE) {
1159 /* For mandatory locking in truncate */
1160 inarg.valid |= FATTR_LOCKOWNER;
1161 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1162 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001163 req->in.h.opcode = FUSE_SETATTR;
1164 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001165 req->in.numargs = 1;
1166 req->in.args[0].size = sizeof(inarg);
1167 req->in.args[0].value = &inarg;
1168 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001169 if (fc->minor < 9)
1170 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1171 else
1172 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001173 req->out.args[0].value = &outarg;
1174 request_send(fc, req);
1175 err = req->out.h.error;
1176 fuse_put_request(fc, req);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001177 if (err) {
1178 if (err == -EINTR)
1179 fuse_invalidate_attr(inode);
1180 return err;
1181 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001182
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001183 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1184 make_bad_inode(inode);
1185 return -EIO;
1186 }
1187
Miklos Szeredi1fb69e72007-10-18 03:06:58 -07001188 fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001189 return 0;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001190}
1191
Miklos Szeredi49d49142007-10-18 03:07:00 -07001192static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1193{
1194 if (attr->ia_valid & ATTR_FILE)
1195 return fuse_do_setattr(entry, attr, attr->ia_file);
1196 else
1197 return fuse_do_setattr(entry, attr, NULL);
1198}
1199
Miklos Szeredie5e55582005-09-09 13:10:28 -07001200static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1201 struct kstat *stat)
1202{
1203 struct inode *inode = entry->d_inode;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001204 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001205
1206 if (!fuse_allow_task(fc, current))
1207 return -EACCES;
1208
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001209 return fuse_update_attributes(inode, stat, NULL, NULL);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001210}
1211
Miklos Szeredi92a87802005-09-09 13:10:31 -07001212static int fuse_setxattr(struct dentry *entry, const char *name,
1213 const void *value, size_t size, int flags)
1214{
1215 struct inode *inode = entry->d_inode;
1216 struct fuse_conn *fc = get_fuse_conn(inode);
1217 struct fuse_req *req;
1218 struct fuse_setxattr_in inarg;
1219 int err;
1220
Miklos Szeredi92a87802005-09-09 13:10:31 -07001221 if (fc->no_setxattr)
1222 return -EOPNOTSUPP;
1223
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001224 req = fuse_get_req(fc);
1225 if (IS_ERR(req))
1226 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001227
1228 memset(&inarg, 0, sizeof(inarg));
1229 inarg.size = size;
1230 inarg.flags = flags;
1231 req->in.h.opcode = FUSE_SETXATTR;
1232 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001233 req->in.numargs = 3;
1234 req->in.args[0].size = sizeof(inarg);
1235 req->in.args[0].value = &inarg;
1236 req->in.args[1].size = strlen(name) + 1;
1237 req->in.args[1].value = name;
1238 req->in.args[2].size = size;
1239 req->in.args[2].value = value;
1240 request_send(fc, req);
1241 err = req->out.h.error;
1242 fuse_put_request(fc, req);
1243 if (err == -ENOSYS) {
1244 fc->no_setxattr = 1;
1245 err = -EOPNOTSUPP;
1246 }
1247 return err;
1248}
1249
1250static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1251 void *value, size_t size)
1252{
1253 struct inode *inode = entry->d_inode;
1254 struct fuse_conn *fc = get_fuse_conn(inode);
1255 struct fuse_req *req;
1256 struct fuse_getxattr_in inarg;
1257 struct fuse_getxattr_out outarg;
1258 ssize_t ret;
1259
1260 if (fc->no_getxattr)
1261 return -EOPNOTSUPP;
1262
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001263 req = fuse_get_req(fc);
1264 if (IS_ERR(req))
1265 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001266
1267 memset(&inarg, 0, sizeof(inarg));
1268 inarg.size = size;
1269 req->in.h.opcode = FUSE_GETXATTR;
1270 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001271 req->in.numargs = 2;
1272 req->in.args[0].size = sizeof(inarg);
1273 req->in.args[0].value = &inarg;
1274 req->in.args[1].size = strlen(name) + 1;
1275 req->in.args[1].value = name;
1276 /* This is really two different operations rolled into one */
1277 req->out.numargs = 1;
1278 if (size) {
1279 req->out.argvar = 1;
1280 req->out.args[0].size = size;
1281 req->out.args[0].value = value;
1282 } else {
1283 req->out.args[0].size = sizeof(outarg);
1284 req->out.args[0].value = &outarg;
1285 }
1286 request_send(fc, req);
1287 ret = req->out.h.error;
1288 if (!ret)
1289 ret = size ? req->out.args[0].size : outarg.size;
1290 else {
1291 if (ret == -ENOSYS) {
1292 fc->no_getxattr = 1;
1293 ret = -EOPNOTSUPP;
1294 }
1295 }
1296 fuse_put_request(fc, req);
1297 return ret;
1298}
1299
1300static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1301{
1302 struct inode *inode = entry->d_inode;
1303 struct fuse_conn *fc = get_fuse_conn(inode);
1304 struct fuse_req *req;
1305 struct fuse_getxattr_in inarg;
1306 struct fuse_getxattr_out outarg;
1307 ssize_t ret;
1308
Miklos Szeredie57ac682007-10-18 03:06:58 -07001309 if (!fuse_allow_task(fc, current))
1310 return -EACCES;
1311
Miklos Szeredi92a87802005-09-09 13:10:31 -07001312 if (fc->no_listxattr)
1313 return -EOPNOTSUPP;
1314
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001315 req = fuse_get_req(fc);
1316 if (IS_ERR(req))
1317 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001318
1319 memset(&inarg, 0, sizeof(inarg));
1320 inarg.size = size;
1321 req->in.h.opcode = FUSE_LISTXATTR;
1322 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001323 req->in.numargs = 1;
1324 req->in.args[0].size = sizeof(inarg);
1325 req->in.args[0].value = &inarg;
1326 /* This is really two different operations rolled into one */
1327 req->out.numargs = 1;
1328 if (size) {
1329 req->out.argvar = 1;
1330 req->out.args[0].size = size;
1331 req->out.args[0].value = list;
1332 } else {
1333 req->out.args[0].size = sizeof(outarg);
1334 req->out.args[0].value = &outarg;
1335 }
1336 request_send(fc, req);
1337 ret = req->out.h.error;
1338 if (!ret)
1339 ret = size ? req->out.args[0].size : outarg.size;
1340 else {
1341 if (ret == -ENOSYS) {
1342 fc->no_listxattr = 1;
1343 ret = -EOPNOTSUPP;
1344 }
1345 }
1346 fuse_put_request(fc, req);
1347 return ret;
1348}
1349
1350static int fuse_removexattr(struct dentry *entry, const char *name)
1351{
1352 struct inode *inode = entry->d_inode;
1353 struct fuse_conn *fc = get_fuse_conn(inode);
1354 struct fuse_req *req;
1355 int err;
1356
1357 if (fc->no_removexattr)
1358 return -EOPNOTSUPP;
1359
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001360 req = fuse_get_req(fc);
1361 if (IS_ERR(req))
1362 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001363
1364 req->in.h.opcode = FUSE_REMOVEXATTR;
1365 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001366 req->in.numargs = 1;
1367 req->in.args[0].size = strlen(name) + 1;
1368 req->in.args[0].value = name;
1369 request_send(fc, req);
1370 err = req->out.h.error;
1371 fuse_put_request(fc, req);
1372 if (err == -ENOSYS) {
1373 fc->no_removexattr = 1;
1374 err = -EOPNOTSUPP;
1375 }
1376 return err;
1377}
1378
Arjan van de Ven754661f2007-02-12 00:55:38 -08001379static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07001380 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001381 .mkdir = fuse_mkdir,
1382 .symlink = fuse_symlink,
1383 .unlink = fuse_unlink,
1384 .rmdir = fuse_rmdir,
1385 .rename = fuse_rename,
1386 .link = fuse_link,
1387 .setattr = fuse_setattr,
1388 .create = fuse_create,
1389 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001390 .permission = fuse_permission,
1391 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001392 .setxattr = fuse_setxattr,
1393 .getxattr = fuse_getxattr,
1394 .listxattr = fuse_listxattr,
1395 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001396};
1397
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001398static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001399 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001400 .read = generic_read_dir,
1401 .readdir = fuse_readdir,
1402 .open = fuse_dir_open,
1403 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001404 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001405};
1406
Arjan van de Ven754661f2007-02-12 00:55:38 -08001407static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001408 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001409 .permission = fuse_permission,
1410 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001411 .setxattr = fuse_setxattr,
1412 .getxattr = fuse_getxattr,
1413 .listxattr = fuse_listxattr,
1414 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001415};
1416
Arjan van de Ven754661f2007-02-12 00:55:38 -08001417static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001418 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001419 .follow_link = fuse_follow_link,
1420 .put_link = fuse_put_link,
1421 .readlink = generic_readlink,
1422 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001423 .setxattr = fuse_setxattr,
1424 .getxattr = fuse_getxattr,
1425 .listxattr = fuse_listxattr,
1426 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001427};
1428
1429void fuse_init_common(struct inode *inode)
1430{
1431 inode->i_op = &fuse_common_inode_operations;
1432}
1433
1434void fuse_init_dir(struct inode *inode)
1435{
1436 inode->i_op = &fuse_dir_inode_operations;
1437 inode->i_fop = &fuse_dir_operations;
1438}
1439
1440void fuse_init_symlink(struct inode *inode)
1441{
1442 inode->i_op = &fuse_symlink_inode_operations;
1443}