]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/fuse/dir.c
fuse: fix race between getattr and write
[mirror_ubuntu-zesty-kernel.git] / fs / fuse / dir.c
CommitLineData
e5e5558e
MS
1/*
2 FUSE: Filesystem in Userspace
51eb01e7 3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
e5e5558e
MS
4
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
0a0898cf
MS
17#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
6f9f1180
MS
44/*
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 */
0a0898cf 53static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
e5e5558e 54{
685d16dd
MS
55 if (sec || nsec) {
56 struct timespec ts = {sec, nsec};
0a0898cf 57 return get_jiffies_64() + timespec_to_jiffies(&ts);
685d16dd 58 } else
0a0898cf 59 return 0;
e5e5558e
MS
60}
61
6f9f1180
MS
62/*
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
64 * replies
65 */
1fb69e78
MS
66static void fuse_change_entry_timeout(struct dentry *entry,
67 struct fuse_entry_out *o)
0aa7c699 68{
0a0898cf
MS
69 fuse_dentry_settime(entry,
70 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
1fb69e78
MS
71}
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);
8cbdf1e6
MS
81}
82
6f9f1180
MS
83/*
84 * Mark the attributes as stale, so that at the next call to
85 * ->getattr() they will be fetched from userspace
86 */
8cbdf1e6
MS
87void fuse_invalidate_attr(struct inode *inode)
88{
0a0898cf 89 get_fuse_inode(inode)->i_time = 0;
8cbdf1e6
MS
90}
91
6f9f1180
MS
92/*
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 */
8cbdf1e6
MS
100static void fuse_invalidate_entry_cache(struct dentry *entry)
101{
0a0898cf 102 fuse_dentry_settime(entry, 0);
8cbdf1e6
MS
103}
104
6f9f1180
MS
105/*
106 * Same as fuse_invalidate_entry_cache(), but also try to remove the
107 * dentry from the hash
108 */
8cbdf1e6
MS
109static void fuse_invalidate_entry(struct dentry *entry)
110{
111 d_invalidate(entry);
112 fuse_invalidate_entry_cache(entry);
0aa7c699
MS
113}
114
e5e5558e
MS
115static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
116 struct dentry *entry,
117 struct fuse_entry_out *outarg)
118{
119 req->in.h.opcode = FUSE_LOOKUP;
120 req->in.h.nodeid = get_node_id(dir);
e5e5558e
MS
121 req->in.numargs = 1;
122 req->in.args[0].size = entry->d_name.len + 1;
123 req->in.args[0].value = entry->d_name.name;
124 req->out.numargs = 1;
125 req->out.args[0].size = sizeof(struct fuse_entry_out);
126 req->out.args[0].value = outarg;
127}
128
6f9f1180
MS
129/*
130 * Check whether the dentry is still valid
131 *
132 * If the entry validity timeout has expired and the dentry is
133 * positive, try to redo the lookup. If the lookup results in a
134 * different inode, then let the VFS invalidate the dentry and redo
135 * the lookup once more. If the lookup results in the same inode,
136 * then refresh the attributes, timeouts and mark the dentry valid.
137 */
e5e5558e
MS
138static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
139{
8cbdf1e6
MS
140 struct inode *inode = entry->d_inode;
141
142 if (inode && is_bad_inode(inode))
e5e5558e 143 return 0;
0a0898cf 144 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
e5e5558e 145 int err;
e5e5558e 146 struct fuse_entry_out outarg;
8cbdf1e6
MS
147 struct fuse_conn *fc;
148 struct fuse_req *req;
2d51013e 149 struct fuse_req *forget_req;
e956edd0 150 struct dentry *parent;
1fb69e78 151 u64 attr_version;
8cbdf1e6 152
50322fe7 153 /* For negative dentries, always do a fresh lookup */
8cbdf1e6
MS
154 if (!inode)
155 return 0;
156
157 fc = get_fuse_conn(inode);
ce1d5a49
MS
158 req = fuse_get_req(fc);
159 if (IS_ERR(req))
e5e5558e
MS
160 return 0;
161
2d51013e
MS
162 forget_req = fuse_get_req(fc);
163 if (IS_ERR(forget_req)) {
164 fuse_put_request(fc, req);
165 return 0;
166 }
167
1fb69e78
MS
168 spin_lock(&fc->lock);
169 attr_version = fc->attr_version;
170 spin_unlock(&fc->lock);
171
e956edd0
MS
172 parent = dget_parent(entry);
173 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
7c352bdf 174 request_send(fc, req);
e956edd0 175 dput(parent);
e5e5558e 176 err = req->out.h.error;
2d51013e 177 fuse_put_request(fc, req);
50322fe7
MS
178 /* Zero nodeid is same as -ENOENT */
179 if (!err && !outarg.nodeid)
180 err = -ENOENT;
9e6268db 181 if (!err) {
8cbdf1e6 182 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 183 if (outarg.nodeid != get_node_id(inode)) {
2d51013e
MS
184 fuse_send_forget(fc, forget_req,
185 outarg.nodeid, 1);
9e6268db
MS
186 return 0;
187 }
8da5ff23 188 spin_lock(&fc->lock);
9e6268db 189 fi->nlookup ++;
8da5ff23 190 spin_unlock(&fc->lock);
9e6268db 191 }
2d51013e 192 fuse_put_request(fc, forget_req);
9e6268db 193 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
e5e5558e
MS
194 return 0;
195
1fb69e78
MS
196 fuse_change_attributes(inode, &outarg.attr,
197 entry_attr_timeout(&outarg),
198 attr_version);
199 fuse_change_entry_timeout(entry, &outarg);
e5e5558e
MS
200 }
201 return 1;
202}
203
8bfc016d 204static int invalid_nodeid(u64 nodeid)
2827d0b2
MS
205{
206 return !nodeid || nodeid == FUSE_ROOT_ID;
207}
208
e5e5558e
MS
209static struct dentry_operations fuse_dentry_operations = {
210 .d_revalidate = fuse_dentry_revalidate,
211};
212
a5bfffac 213int fuse_valid_type(int m)
39ee059a
MS
214{
215 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
216 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
217}
218
d2a85164
MS
219/*
220 * Add a directory inode to a dentry, ensuring that no other dentry
221 * refers to this inode. Called with fc->inst_mutex.
222 */
223static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
224{
225 struct dentry *alias = d_find_alias(inode);
226 if (alias) {
227 /* This tries to shrink the subtree below alias */
228 fuse_invalidate_entry(alias);
229 dput(alias);
230 if (!list_empty(&inode->i_dentry))
231 return -EBUSY;
232 }
233 d_add(entry, inode);
234 return 0;
235}
236
0aa7c699
MS
237static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
238 struct nameidata *nd)
e5e5558e
MS
239{
240 int err;
e5e5558e
MS
241 struct fuse_entry_out outarg;
242 struct inode *inode = NULL;
243 struct fuse_conn *fc = get_fuse_conn(dir);
244 struct fuse_req *req;
2d51013e 245 struct fuse_req *forget_req;
1fb69e78 246 u64 attr_version;
e5e5558e
MS
247
248 if (entry->d_name.len > FUSE_NAME_MAX)
0aa7c699 249 return ERR_PTR(-ENAMETOOLONG);
e5e5558e 250
ce1d5a49
MS
251 req = fuse_get_req(fc);
252 if (IS_ERR(req))
253 return ERR_PTR(PTR_ERR(req));
e5e5558e 254
2d51013e
MS
255 forget_req = fuse_get_req(fc);
256 if (IS_ERR(forget_req)) {
257 fuse_put_request(fc, req);
258 return ERR_PTR(PTR_ERR(forget_req));
259 }
260
1fb69e78
MS
261 spin_lock(&fc->lock);
262 attr_version = fc->attr_version;
263 spin_unlock(&fc->lock);
264
e5e5558e
MS
265 fuse_lookup_init(req, dir, entry, &outarg);
266 request_send(fc, req);
e5e5558e 267 err = req->out.h.error;
2d51013e 268 fuse_put_request(fc, req);
50322fe7
MS
269 /* Zero nodeid is same as -ENOENT, but with valid timeout */
270 if (!err && outarg.nodeid &&
a5bfffac
TS
271 (invalid_nodeid(outarg.nodeid) ||
272 !fuse_valid_type(outarg.attr.mode)))
ee4e5271 273 err = -EIO;
8cbdf1e6 274 if (!err && outarg.nodeid) {
e5e5558e 275 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
1fb69e78
MS
276 &outarg.attr, entry_attr_timeout(&outarg),
277 attr_version);
e5e5558e 278 if (!inode) {
2d51013e 279 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
0aa7c699 280 return ERR_PTR(-ENOMEM);
e5e5558e
MS
281 }
282 }
2d51013e 283 fuse_put_request(fc, forget_req);
e5e5558e 284 if (err && err != -ENOENT)
0aa7c699 285 return ERR_PTR(err);
e5e5558e 286
d2a85164
MS
287 if (inode && S_ISDIR(inode->i_mode)) {
288 mutex_lock(&fc->inst_mutex);
289 err = fuse_d_add_directory(entry, inode);
290 mutex_unlock(&fc->inst_mutex);
291 if (err) {
292 iput(inode);
293 return ERR_PTR(err);
294 }
295 } else
296 d_add(entry, inode);
297
e5e5558e 298 entry->d_op = &fuse_dentry_operations;
8cbdf1e6 299 if (!err)
1fb69e78 300 fuse_change_entry_timeout(entry, &outarg);
8cbdf1e6
MS
301 else
302 fuse_invalidate_entry_cache(entry);
0aa7c699 303 return NULL;
e5e5558e
MS
304}
305
51eb01e7
MS
306/*
307 * Synchronous release for the case when something goes wrong in CREATE_OPEN
308 */
309static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
310 u64 nodeid, int flags)
311{
c756e0a4
MS
312 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
313 ff->reserved_req->force = 1;
314 request_send(fc, ff->reserved_req);
315 fuse_put_request(fc, ff->reserved_req);
316 kfree(ff);
51eb01e7
MS
317}
318
6f9f1180
MS
319/*
320 * Atomic create+open operation
321 *
322 * If the filesystem doesn't support this, then fall back to separate
323 * 'mknod' + 'open' requests.
324 */
fd72faac
MS
325static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
326 struct nameidata *nd)
327{
328 int err;
329 struct inode *inode;
330 struct fuse_conn *fc = get_fuse_conn(dir);
331 struct fuse_req *req;
51eb01e7 332 struct fuse_req *forget_req;
fd72faac
MS
333 struct fuse_open_in inarg;
334 struct fuse_open_out outopen;
335 struct fuse_entry_out outentry;
fd72faac
MS
336 struct fuse_file *ff;
337 struct file *file;
338 int flags = nd->intent.open.flags - 1;
339
fd72faac 340 if (fc->no_create)
ce1d5a49 341 return -ENOSYS;
fd72faac 342
51eb01e7
MS
343 forget_req = fuse_get_req(fc);
344 if (IS_ERR(forget_req))
345 return PTR_ERR(forget_req);
346
ce1d5a49 347 req = fuse_get_req(fc);
51eb01e7 348 err = PTR_ERR(req);
ce1d5a49 349 if (IS_ERR(req))
51eb01e7 350 goto out_put_forget_req;
fd72faac 351
ce1d5a49 352 err = -ENOMEM;
fd72faac
MS
353 ff = fuse_file_alloc();
354 if (!ff)
355 goto out_put_request;
356
357 flags &= ~O_NOCTTY;
358 memset(&inarg, 0, sizeof(inarg));
359 inarg.flags = flags;
360 inarg.mode = mode;
361 req->in.h.opcode = FUSE_CREATE;
362 req->in.h.nodeid = get_node_id(dir);
fd72faac
MS
363 req->in.numargs = 2;
364 req->in.args[0].size = sizeof(inarg);
365 req->in.args[0].value = &inarg;
366 req->in.args[1].size = entry->d_name.len + 1;
367 req->in.args[1].value = entry->d_name.name;
368 req->out.numargs = 2;
369 req->out.args[0].size = sizeof(outentry);
370 req->out.args[0].value = &outentry;
371 req->out.args[1].size = sizeof(outopen);
372 req->out.args[1].value = &outopen;
373 request_send(fc, req);
374 err = req->out.h.error;
375 if (err) {
376 if (err == -ENOSYS)
377 fc->no_create = 1;
378 goto out_free_ff;
379 }
380
381 err = -EIO;
2827d0b2 382 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
fd72faac
MS
383 goto out_free_ff;
384
51eb01e7 385 fuse_put_request(fc, req);
fd72faac 386 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
1fb69e78 387 &outentry.attr, entry_attr_timeout(&outentry), 0);
fd72faac
MS
388 if (!inode) {
389 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
390 ff->fh = outopen.fh;
51eb01e7
MS
391 fuse_sync_release(fc, ff, outentry.nodeid, flags);
392 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
393 return -ENOMEM;
fd72faac 394 }
51eb01e7 395 fuse_put_request(fc, forget_req);
fd72faac 396 d_instantiate(entry, inode);
1fb69e78 397 fuse_change_entry_timeout(entry, &outentry);
fd72faac
MS
398 file = lookup_instantiate_filp(nd, entry, generic_file_open);
399 if (IS_ERR(file)) {
400 ff->fh = outopen.fh;
51eb01e7 401 fuse_sync_release(fc, ff, outentry.nodeid, flags);
fd72faac
MS
402 return PTR_ERR(file);
403 }
404 fuse_finish_open(inode, file, ff, &outopen);
405 return 0;
406
407 out_free_ff:
408 fuse_file_free(ff);
409 out_put_request:
410 fuse_put_request(fc, req);
51eb01e7
MS
411 out_put_forget_req:
412 fuse_put_request(fc, forget_req);
fd72faac
MS
413 return err;
414}
415
6f9f1180
MS
416/*
417 * Code shared between mknod, mkdir, symlink and link
418 */
9e6268db
MS
419static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
420 struct inode *dir, struct dentry *entry,
421 int mode)
422{
423 struct fuse_entry_out outarg;
424 struct inode *inode;
9e6268db 425 int err;
2d51013e
MS
426 struct fuse_req *forget_req;
427
428 forget_req = fuse_get_req(fc);
429 if (IS_ERR(forget_req)) {
430 fuse_put_request(fc, req);
431 return PTR_ERR(forget_req);
432 }
9e6268db
MS
433
434 req->in.h.nodeid = get_node_id(dir);
9e6268db
MS
435 req->out.numargs = 1;
436 req->out.args[0].size = sizeof(outarg);
437 req->out.args[0].value = &outarg;
438 request_send(fc, req);
439 err = req->out.h.error;
2d51013e
MS
440 fuse_put_request(fc, req);
441 if (err)
442 goto out_put_forget_req;
443
39ee059a
MS
444 err = -EIO;
445 if (invalid_nodeid(outarg.nodeid))
2d51013e 446 goto out_put_forget_req;
39ee059a
MS
447
448 if ((outarg.attr.mode ^ mode) & S_IFMT)
2d51013e 449 goto out_put_forget_req;
39ee059a 450
9e6268db 451 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
1fb69e78 452 &outarg.attr, entry_attr_timeout(&outarg), 0);
9e6268db 453 if (!inode) {
2d51013e 454 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
9e6268db
MS
455 return -ENOMEM;
456 }
2d51013e 457 fuse_put_request(fc, forget_req);
9e6268db 458
d2a85164
MS
459 if (S_ISDIR(inode->i_mode)) {
460 struct dentry *alias;
461 mutex_lock(&fc->inst_mutex);
462 alias = d_find_alias(inode);
463 if (alias) {
464 /* New directory must have moved since mkdir */
465 mutex_unlock(&fc->inst_mutex);
466 dput(alias);
467 iput(inode);
468 return -EBUSY;
469 }
470 d_instantiate(entry, inode);
471 mutex_unlock(&fc->inst_mutex);
472 } else
473 d_instantiate(entry, inode);
9e6268db 474
1fb69e78 475 fuse_change_entry_timeout(entry, &outarg);
9e6268db
MS
476 fuse_invalidate_attr(dir);
477 return 0;
39ee059a 478
2d51013e
MS
479 out_put_forget_req:
480 fuse_put_request(fc, forget_req);
39ee059a 481 return err;
9e6268db
MS
482}
483
484static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
485 dev_t rdev)
486{
487 struct fuse_mknod_in inarg;
488 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
489 struct fuse_req *req = fuse_get_req(fc);
490 if (IS_ERR(req))
491 return PTR_ERR(req);
9e6268db
MS
492
493 memset(&inarg, 0, sizeof(inarg));
494 inarg.mode = mode;
495 inarg.rdev = new_encode_dev(rdev);
496 req->in.h.opcode = FUSE_MKNOD;
497 req->in.numargs = 2;
498 req->in.args[0].size = sizeof(inarg);
499 req->in.args[0].value = &inarg;
500 req->in.args[1].size = entry->d_name.len + 1;
501 req->in.args[1].value = entry->d_name.name;
502 return create_new_entry(fc, req, dir, entry, mode);
503}
504
505static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
506 struct nameidata *nd)
507{
b9ba347f 508 if (nd && (nd->flags & LOOKUP_OPEN)) {
fd72faac
MS
509 int err = fuse_create_open(dir, entry, mode, nd);
510 if (err != -ENOSYS)
511 return err;
512 /* Fall back on mknod */
513 }
9e6268db
MS
514 return fuse_mknod(dir, entry, mode, 0);
515}
516
517static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
518{
519 struct fuse_mkdir_in inarg;
520 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
521 struct fuse_req *req = fuse_get_req(fc);
522 if (IS_ERR(req))
523 return PTR_ERR(req);
9e6268db
MS
524
525 memset(&inarg, 0, sizeof(inarg));
526 inarg.mode = mode;
527 req->in.h.opcode = FUSE_MKDIR;
528 req->in.numargs = 2;
529 req->in.args[0].size = sizeof(inarg);
530 req->in.args[0].value = &inarg;
531 req->in.args[1].size = entry->d_name.len + 1;
532 req->in.args[1].value = entry->d_name.name;
533 return create_new_entry(fc, req, dir, entry, S_IFDIR);
534}
535
536static int fuse_symlink(struct inode *dir, struct dentry *entry,
537 const char *link)
538{
539 struct fuse_conn *fc = get_fuse_conn(dir);
540 unsigned len = strlen(link) + 1;
ce1d5a49
MS
541 struct fuse_req *req = fuse_get_req(fc);
542 if (IS_ERR(req))
543 return PTR_ERR(req);
9e6268db
MS
544
545 req->in.h.opcode = FUSE_SYMLINK;
546 req->in.numargs = 2;
547 req->in.args[0].size = entry->d_name.len + 1;
548 req->in.args[0].value = entry->d_name.name;
549 req->in.args[1].size = len;
550 req->in.args[1].value = link;
551 return create_new_entry(fc, req, dir, entry, S_IFLNK);
552}
553
554static int fuse_unlink(struct inode *dir, struct dentry *entry)
555{
556 int err;
557 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
558 struct fuse_req *req = fuse_get_req(fc);
559 if (IS_ERR(req))
560 return PTR_ERR(req);
9e6268db
MS
561
562 req->in.h.opcode = FUSE_UNLINK;
563 req->in.h.nodeid = get_node_id(dir);
9e6268db
MS
564 req->in.numargs = 1;
565 req->in.args[0].size = entry->d_name.len + 1;
566 req->in.args[0].value = entry->d_name.name;
567 request_send(fc, req);
568 err = req->out.h.error;
569 fuse_put_request(fc, req);
570 if (!err) {
571 struct inode *inode = entry->d_inode;
572
573 /* Set nlink to zero so the inode can be cleared, if
574 the inode does have more links this will be
575 discovered at the next lookup/getattr */
ce71ec36 576 clear_nlink(inode);
9e6268db
MS
577 fuse_invalidate_attr(inode);
578 fuse_invalidate_attr(dir);
8cbdf1e6 579 fuse_invalidate_entry_cache(entry);
9e6268db
MS
580 } else if (err == -EINTR)
581 fuse_invalidate_entry(entry);
582 return err;
583}
584
585static int fuse_rmdir(struct inode *dir, struct dentry *entry)
586{
587 int err;
588 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
589 struct fuse_req *req = fuse_get_req(fc);
590 if (IS_ERR(req))
591 return PTR_ERR(req);
9e6268db
MS
592
593 req->in.h.opcode = FUSE_RMDIR;
594 req->in.h.nodeid = get_node_id(dir);
9e6268db
MS
595 req->in.numargs = 1;
596 req->in.args[0].size = entry->d_name.len + 1;
597 req->in.args[0].value = entry->d_name.name;
598 request_send(fc, req);
599 err = req->out.h.error;
600 fuse_put_request(fc, req);
601 if (!err) {
ce71ec36 602 clear_nlink(entry->d_inode);
9e6268db 603 fuse_invalidate_attr(dir);
8cbdf1e6 604 fuse_invalidate_entry_cache(entry);
9e6268db
MS
605 } else if (err == -EINTR)
606 fuse_invalidate_entry(entry);
607 return err;
608}
609
610static int fuse_rename(struct inode *olddir, struct dentry *oldent,
611 struct inode *newdir, struct dentry *newent)
612{
613 int err;
614 struct fuse_rename_in inarg;
615 struct fuse_conn *fc = get_fuse_conn(olddir);
ce1d5a49
MS
616 struct fuse_req *req = fuse_get_req(fc);
617 if (IS_ERR(req))
618 return PTR_ERR(req);
9e6268db
MS
619
620 memset(&inarg, 0, sizeof(inarg));
621 inarg.newdir = get_node_id(newdir);
622 req->in.h.opcode = FUSE_RENAME;
623 req->in.h.nodeid = get_node_id(olddir);
9e6268db
MS
624 req->in.numargs = 3;
625 req->in.args[0].size = sizeof(inarg);
626 req->in.args[0].value = &inarg;
627 req->in.args[1].size = oldent->d_name.len + 1;
628 req->in.args[1].value = oldent->d_name.name;
629 req->in.args[2].size = newent->d_name.len + 1;
630 req->in.args[2].value = newent->d_name.name;
631 request_send(fc, req);
632 err = req->out.h.error;
633 fuse_put_request(fc, req);
634 if (!err) {
635 fuse_invalidate_attr(olddir);
636 if (olddir != newdir)
637 fuse_invalidate_attr(newdir);
8cbdf1e6
MS
638
639 /* newent will end up negative */
640 if (newent->d_inode)
641 fuse_invalidate_entry_cache(newent);
9e6268db
MS
642 } else if (err == -EINTR) {
643 /* If request was interrupted, DEITY only knows if the
644 rename actually took place. If the invalidation
645 fails (e.g. some process has CWD under the renamed
646 directory), then there can be inconsistency between
647 the dcache and the real filesystem. Tough luck. */
648 fuse_invalidate_entry(oldent);
649 if (newent->d_inode)
650 fuse_invalidate_entry(newent);
651 }
652
653 return err;
654}
655
656static int fuse_link(struct dentry *entry, struct inode *newdir,
657 struct dentry *newent)
658{
659 int err;
660 struct fuse_link_in inarg;
661 struct inode *inode = entry->d_inode;
662 struct fuse_conn *fc = get_fuse_conn(inode);
ce1d5a49
MS
663 struct fuse_req *req = fuse_get_req(fc);
664 if (IS_ERR(req))
665 return PTR_ERR(req);
9e6268db
MS
666
667 memset(&inarg, 0, sizeof(inarg));
668 inarg.oldnodeid = get_node_id(inode);
669 req->in.h.opcode = FUSE_LINK;
9e6268db
MS
670 req->in.numargs = 2;
671 req->in.args[0].size = sizeof(inarg);
672 req->in.args[0].value = &inarg;
673 req->in.args[1].size = newent->d_name.len + 1;
674 req->in.args[1].value = newent->d_name.name;
675 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
676 /* Contrary to "normal" filesystems it can happen that link
677 makes two "logical" inodes point to the same "physical"
678 inode. We invalidate the attributes of the old one, so it
679 will reflect changes in the backing inode (link count,
680 etc.)
681 */
682 if (!err || err == -EINTR)
683 fuse_invalidate_attr(inode);
684 return err;
685}
686
1fb69e78
MS
687static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
688 struct kstat *stat)
689{
690 stat->dev = inode->i_sb->s_dev;
691 stat->ino = attr->ino;
692 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
693 stat->nlink = attr->nlink;
694 stat->uid = attr->uid;
695 stat->gid = attr->gid;
696 stat->rdev = inode->i_rdev;
697 stat->atime.tv_sec = attr->atime;
698 stat->atime.tv_nsec = attr->atimensec;
699 stat->mtime.tv_sec = attr->mtime;
700 stat->mtime.tv_nsec = attr->mtimensec;
701 stat->ctime.tv_sec = attr->ctime;
702 stat->ctime.tv_nsec = attr->ctimensec;
703 stat->size = attr->size;
704 stat->blocks = attr->blocks;
705 stat->blksize = (1 << inode->i_blkbits);
706}
707
708static int fuse_do_getattr(struct inode *inode, struct kstat *stat)
e5e5558e
MS
709{
710 int err;
711 struct fuse_attr_out arg;
712 struct fuse_conn *fc = get_fuse_conn(inode);
1fb69e78
MS
713 struct fuse_req *req;
714 u64 attr_version;
715
716 req = fuse_get_req(fc);
ce1d5a49
MS
717 if (IS_ERR(req))
718 return PTR_ERR(req);
e5e5558e 719
1fb69e78
MS
720 spin_lock(&fc->lock);
721 attr_version = fc->attr_version;
722 spin_unlock(&fc->lock);
723
e5e5558e
MS
724 req->in.h.opcode = FUSE_GETATTR;
725 req->in.h.nodeid = get_node_id(inode);
e5e5558e
MS
726 req->out.numargs = 1;
727 req->out.args[0].size = sizeof(arg);
728 req->out.args[0].value = &arg;
729 request_send(fc, req);
730 err = req->out.h.error;
731 fuse_put_request(fc, req);
732 if (!err) {
733 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
734 make_bad_inode(inode);
735 err = -EIO;
736 } else {
1fb69e78
MS
737 fuse_change_attributes(inode, &arg.attr,
738 attr_timeout(&arg),
739 attr_version);
740 if (stat)
741 fuse_fillattr(inode, &arg.attr, stat);
e5e5558e
MS
742 }
743 }
744 return err;
745}
746
87729a55
MS
747/*
748 * Calling into a user-controlled filesystem gives the filesystem
749 * daemon ptrace-like capabilities over the requester process. This
750 * means, that the filesystem daemon is able to record the exact
751 * filesystem operations performed, and can also control the behavior
752 * of the requester process in otherwise impossible ways. For example
753 * it can delay the operation for arbitrary length of time allowing
754 * DoS against the requester.
755 *
756 * For this reason only those processes can call into the filesystem,
757 * for which the owner of the mount has ptrace privilege. This
758 * excludes processes started by other users, suid or sgid processes.
759 */
e57ac683 760int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
87729a55
MS
761{
762 if (fc->flags & FUSE_ALLOW_OTHER)
763 return 1;
764
765 if (task->euid == fc->user_id &&
766 task->suid == fc->user_id &&
767 task->uid == fc->user_id &&
768 task->egid == fc->group_id &&
769 task->sgid == fc->group_id &&
770 task->gid == fc->group_id)
771 return 1;
772
773 return 0;
774}
775
31d40d74
MS
776static int fuse_access(struct inode *inode, int mask)
777{
778 struct fuse_conn *fc = get_fuse_conn(inode);
779 struct fuse_req *req;
780 struct fuse_access_in inarg;
781 int err;
782
783 if (fc->no_access)
784 return 0;
785
ce1d5a49
MS
786 req = fuse_get_req(fc);
787 if (IS_ERR(req))
788 return PTR_ERR(req);
31d40d74
MS
789
790 memset(&inarg, 0, sizeof(inarg));
791 inarg.mask = mask;
792 req->in.h.opcode = FUSE_ACCESS;
793 req->in.h.nodeid = get_node_id(inode);
31d40d74
MS
794 req->in.numargs = 1;
795 req->in.args[0].size = sizeof(inarg);
796 req->in.args[0].value = &inarg;
797 request_send(fc, req);
798 err = req->out.h.error;
799 fuse_put_request(fc, req);
800 if (err == -ENOSYS) {
801 fc->no_access = 1;
802 err = 0;
803 }
804 return err;
805}
806
6f9f1180
MS
807/*
808 * Check permission. The two basic access models of FUSE are:
809 *
810 * 1) Local access checking ('default_permissions' mount option) based
811 * on file mode. This is the plain old disk filesystem permission
812 * modell.
813 *
814 * 2) "Remote" access checking, where server is responsible for
815 * checking permission in each inode operation. An exception to this
816 * is if ->permission() was invoked from sys_access() in which case an
817 * access request is sent. Execute permission is still checked
818 * locally based on file mode.
819 */
e5e5558e
MS
820static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
821{
822 struct fuse_conn *fc = get_fuse_conn(inode);
244f6385
MS
823 bool refreshed = false;
824 int err = 0;
e5e5558e 825
87729a55 826 if (!fuse_allow_task(fc, current))
e5e5558e 827 return -EACCES;
244f6385
MS
828
829 /*
e8e96157 830 * If attributes are needed, refresh them before proceeding
244f6385 831 */
e8e96157
MS
832 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
833 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1fb69e78
MS
834 struct fuse_inode *fi = get_fuse_inode(inode);
835 if (fi->i_time < get_jiffies_64()) {
836 err = fuse_do_getattr(inode, NULL);
837 if (err)
838 return err;
244f6385 839
1fb69e78
MS
840 refreshed = true;
841 }
244f6385
MS
842 }
843
844 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1e9a4ed9
MS
845 int err = generic_permission(inode, mask, NULL);
846
847 /* If permission is denied, try to refresh file
848 attributes. This is also needed, because the root
849 node will at first have no permissions */
244f6385 850 if (err == -EACCES && !refreshed) {
1fb69e78 851 err = fuse_do_getattr(inode, NULL);
1e9a4ed9
MS
852 if (!err)
853 err = generic_permission(inode, mask, NULL);
854 }
855
6f9f1180
MS
856 /* Note: the opposite of the above test does not
857 exist. So if permissions are revoked this won't be
858 noticed immediately, only after the attribute
859 timeout has expired */
e8e96157
MS
860 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
861 err = fuse_access(inode, mask);
862 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
863 if (!(inode->i_mode & S_IXUGO)) {
864 if (refreshed)
865 return -EACCES;
866
1fb69e78 867 err = fuse_do_getattr(inode, NULL);
e8e96157
MS
868 if (!err && !(inode->i_mode & S_IXUGO))
869 return -EACCES;
870 }
e5e5558e 871 }
244f6385 872 return err;
e5e5558e
MS
873}
874
875static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
876 void *dstbuf, filldir_t filldir)
877{
878 while (nbytes >= FUSE_NAME_OFFSET) {
879 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
880 size_t reclen = FUSE_DIRENT_SIZE(dirent);
881 int over;
882 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
883 return -EIO;
884 if (reclen > nbytes)
885 break;
886
887 over = filldir(dstbuf, dirent->name, dirent->namelen,
888 file->f_pos, dirent->ino, dirent->type);
889 if (over)
890 break;
891
892 buf += reclen;
893 nbytes -= reclen;
894 file->f_pos = dirent->off;
895 }
896
897 return 0;
898}
899
04730fef 900static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
e5e5558e 901{
04730fef
MS
902 int err;
903 size_t nbytes;
904 struct page *page;
7706a9d6 905 struct inode *inode = file->f_path.dentry->d_inode;
e5e5558e 906 struct fuse_conn *fc = get_fuse_conn(inode);
c756e0a4 907 struct fuse_file *ff = file->private_data;
248d86e8
MS
908 struct fuse_req *req;
909
910 if (is_bad_inode(inode))
911 return -EIO;
912
ce1d5a49
MS
913 req = fuse_get_req(fc);
914 if (IS_ERR(req))
915 return PTR_ERR(req);
e5e5558e 916
04730fef
MS
917 page = alloc_page(GFP_KERNEL);
918 if (!page) {
919 fuse_put_request(fc, req);
920 return -ENOMEM;
921 }
922 req->num_pages = 1;
923 req->pages[0] = page;
c756e0a4 924 fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
361b1eb5
MS
925 request_send(fc, req);
926 nbytes = req->out.args[0].size;
e5e5558e
MS
927 err = req->out.h.error;
928 fuse_put_request(fc, req);
929 if (!err)
04730fef
MS
930 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
931 filldir);
e5e5558e 932
04730fef 933 __free_page(page);
b36c31ba 934 fuse_invalidate_attr(inode); /* atime changed */
04730fef 935 return err;
e5e5558e
MS
936}
937
938static char *read_link(struct dentry *dentry)
939{
940 struct inode *inode = dentry->d_inode;
941 struct fuse_conn *fc = get_fuse_conn(inode);
ce1d5a49 942 struct fuse_req *req = fuse_get_req(fc);
e5e5558e
MS
943 char *link;
944
ce1d5a49
MS
945 if (IS_ERR(req))
946 return ERR_PTR(PTR_ERR(req));
e5e5558e
MS
947
948 link = (char *) __get_free_page(GFP_KERNEL);
949 if (!link) {
950 link = ERR_PTR(-ENOMEM);
951 goto out;
952 }
953 req->in.h.opcode = FUSE_READLINK;
954 req->in.h.nodeid = get_node_id(inode);
e5e5558e
MS
955 req->out.argvar = 1;
956 req->out.numargs = 1;
957 req->out.args[0].size = PAGE_SIZE - 1;
958 req->out.args[0].value = link;
959 request_send(fc, req);
960 if (req->out.h.error) {
961 free_page((unsigned long) link);
962 link = ERR_PTR(req->out.h.error);
963 } else
964 link[req->out.args[0].size] = '\0';
965 out:
966 fuse_put_request(fc, req);
b36c31ba 967 fuse_invalidate_attr(inode); /* atime changed */
e5e5558e
MS
968 return link;
969}
970
971static void free_link(char *link)
972{
973 if (!IS_ERR(link))
974 free_page((unsigned long) link);
975}
976
977static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
978{
979 nd_set_link(nd, read_link(dentry));
980 return NULL;
981}
982
983static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
984{
985 free_link(nd_get_link(nd));
986}
987
988static int fuse_dir_open(struct inode *inode, struct file *file)
989{
04730fef 990 return fuse_open_common(inode, file, 1);
e5e5558e
MS
991}
992
993static int fuse_dir_release(struct inode *inode, struct file *file)
994{
04730fef 995 return fuse_release_common(inode, file, 1);
e5e5558e
MS
996}
997
82547981
MS
998static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
999{
1000 /* nfsd can call this with no file */
1001 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1002}
1003
befc649c 1004static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
9e6268db
MS
1005{
1006 unsigned ivalid = iattr->ia_valid;
9e6268db
MS
1007
1008 if (ivalid & ATTR_MODE)
befc649c 1009 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
9e6268db 1010 if (ivalid & ATTR_UID)
befc649c 1011 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
9e6268db 1012 if (ivalid & ATTR_GID)
befc649c 1013 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
9e6268db 1014 if (ivalid & ATTR_SIZE)
befc649c 1015 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
9e6268db
MS
1016 /* You can only _set_ these together (they may change by themselves) */
1017 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
befc649c
MS
1018 arg->valid |= FATTR_ATIME | FATTR_MTIME;
1019 arg->atime = iattr->ia_atime.tv_sec;
1020 arg->mtime = iattr->ia_mtime.tv_sec;
1021 }
1022 if (ivalid & ATTR_FILE) {
1023 struct fuse_file *ff = iattr->ia_file->private_data;
1024 arg->valid |= FATTR_FH;
1025 arg->fh = ff->fh;
9e6268db 1026 }
9e6268db
MS
1027}
1028
6f9f1180
MS
1029/*
1030 * Set attributes, and at the same time refresh them.
1031 *
1032 * Truncation is slightly complicated, because the 'truncate' request
1033 * may fail, in which case we don't want to touch the mapping.
9ffbb916
MS
1034 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1035 * and the actual truncation by hand.
6f9f1180 1036 */
9e6268db
MS
1037static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1038{
1039 struct inode *inode = entry->d_inode;
1040 struct fuse_conn *fc = get_fuse_conn(inode);
9e6268db
MS
1041 struct fuse_req *req;
1042 struct fuse_setattr_in inarg;
1043 struct fuse_attr_out outarg;
1044 int err;
9e6268db 1045
e57ac683
MS
1046 if (!fuse_allow_task(fc, current))
1047 return -EACCES;
1048
1e9a4ed9
MS
1049 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1050 err = inode_change_ok(inode, attr);
1051 if (err)
1052 return err;
1053 }
1054
9e6268db
MS
1055 if (attr->ia_valid & ATTR_SIZE) {
1056 unsigned long limit;
b2d2272f
MS
1057 if (IS_SWAPFILE(inode))
1058 return -ETXTBSY;
9e6268db
MS
1059 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1060 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1061 send_sig(SIGXFSZ, current, 0);
1062 return -EFBIG;
1063 }
1064 }
1065
ce1d5a49
MS
1066 req = fuse_get_req(fc);
1067 if (IS_ERR(req))
1068 return PTR_ERR(req);
9e6268db
MS
1069
1070 memset(&inarg, 0, sizeof(inarg));
befc649c 1071 iattr_to_fattr(attr, &inarg);
9e6268db
MS
1072 req->in.h.opcode = FUSE_SETATTR;
1073 req->in.h.nodeid = get_node_id(inode);
9e6268db
MS
1074 req->in.numargs = 1;
1075 req->in.args[0].size = sizeof(inarg);
1076 req->in.args[0].value = &inarg;
1077 req->out.numargs = 1;
1078 req->out.args[0].size = sizeof(outarg);
1079 req->out.args[0].value = &outarg;
1080 request_send(fc, req);
1081 err = req->out.h.error;
1082 fuse_put_request(fc, req);
e00d2c2d
MS
1083 if (err) {
1084 if (err == -EINTR)
1085 fuse_invalidate_attr(inode);
1086 return err;
1087 }
9e6268db 1088
e00d2c2d
MS
1089 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1090 make_bad_inode(inode);
1091 return -EIO;
1092 }
1093
1fb69e78 1094 fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0);
e00d2c2d 1095 return 0;
9e6268db
MS
1096}
1097
e5e5558e
MS
1098static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1099 struct kstat *stat)
1100{
1101 struct inode *inode = entry->d_inode;
244f6385
MS
1102 struct fuse_inode *fi = get_fuse_inode(inode);
1103 struct fuse_conn *fc = get_fuse_conn(inode);
e8e96157 1104 int err;
244f6385
MS
1105
1106 if (!fuse_allow_task(fc, current))
1107 return -EACCES;
1108
1fb69e78
MS
1109 if (fi->i_time < get_jiffies_64())
1110 err = fuse_do_getattr(inode, stat);
1111 else {
1112 err = 0;
e5e5558e 1113 generic_fillattr(inode, stat);
ebc14c4d
MS
1114 stat->mode = fi->orig_i_mode;
1115 }
e5e5558e
MS
1116
1117 return err;
1118}
1119
92a8780e
MS
1120static int fuse_setxattr(struct dentry *entry, const char *name,
1121 const void *value, size_t size, int flags)
1122{
1123 struct inode *inode = entry->d_inode;
1124 struct fuse_conn *fc = get_fuse_conn(inode);
1125 struct fuse_req *req;
1126 struct fuse_setxattr_in inarg;
1127 int err;
1128
92a8780e
MS
1129 if (fc->no_setxattr)
1130 return -EOPNOTSUPP;
1131
ce1d5a49
MS
1132 req = fuse_get_req(fc);
1133 if (IS_ERR(req))
1134 return PTR_ERR(req);
92a8780e
MS
1135
1136 memset(&inarg, 0, sizeof(inarg));
1137 inarg.size = size;
1138 inarg.flags = flags;
1139 req->in.h.opcode = FUSE_SETXATTR;
1140 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1141 req->in.numargs = 3;
1142 req->in.args[0].size = sizeof(inarg);
1143 req->in.args[0].value = &inarg;
1144 req->in.args[1].size = strlen(name) + 1;
1145 req->in.args[1].value = name;
1146 req->in.args[2].size = size;
1147 req->in.args[2].value = value;
1148 request_send(fc, req);
1149 err = req->out.h.error;
1150 fuse_put_request(fc, req);
1151 if (err == -ENOSYS) {
1152 fc->no_setxattr = 1;
1153 err = -EOPNOTSUPP;
1154 }
1155 return err;
1156}
1157
1158static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1159 void *value, size_t size)
1160{
1161 struct inode *inode = entry->d_inode;
1162 struct fuse_conn *fc = get_fuse_conn(inode);
1163 struct fuse_req *req;
1164 struct fuse_getxattr_in inarg;
1165 struct fuse_getxattr_out outarg;
1166 ssize_t ret;
1167
1168 if (fc->no_getxattr)
1169 return -EOPNOTSUPP;
1170
ce1d5a49
MS
1171 req = fuse_get_req(fc);
1172 if (IS_ERR(req))
1173 return PTR_ERR(req);
92a8780e
MS
1174
1175 memset(&inarg, 0, sizeof(inarg));
1176 inarg.size = size;
1177 req->in.h.opcode = FUSE_GETXATTR;
1178 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1179 req->in.numargs = 2;
1180 req->in.args[0].size = sizeof(inarg);
1181 req->in.args[0].value = &inarg;
1182 req->in.args[1].size = strlen(name) + 1;
1183 req->in.args[1].value = name;
1184 /* This is really two different operations rolled into one */
1185 req->out.numargs = 1;
1186 if (size) {
1187 req->out.argvar = 1;
1188 req->out.args[0].size = size;
1189 req->out.args[0].value = value;
1190 } else {
1191 req->out.args[0].size = sizeof(outarg);
1192 req->out.args[0].value = &outarg;
1193 }
1194 request_send(fc, req);
1195 ret = req->out.h.error;
1196 if (!ret)
1197 ret = size ? req->out.args[0].size : outarg.size;
1198 else {
1199 if (ret == -ENOSYS) {
1200 fc->no_getxattr = 1;
1201 ret = -EOPNOTSUPP;
1202 }
1203 }
1204 fuse_put_request(fc, req);
1205 return ret;
1206}
1207
1208static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1209{
1210 struct inode *inode = entry->d_inode;
1211 struct fuse_conn *fc = get_fuse_conn(inode);
1212 struct fuse_req *req;
1213 struct fuse_getxattr_in inarg;
1214 struct fuse_getxattr_out outarg;
1215 ssize_t ret;
1216
e57ac683
MS
1217 if (!fuse_allow_task(fc, current))
1218 return -EACCES;
1219
92a8780e
MS
1220 if (fc->no_listxattr)
1221 return -EOPNOTSUPP;
1222
ce1d5a49
MS
1223 req = fuse_get_req(fc);
1224 if (IS_ERR(req))
1225 return PTR_ERR(req);
92a8780e
MS
1226
1227 memset(&inarg, 0, sizeof(inarg));
1228 inarg.size = size;
1229 req->in.h.opcode = FUSE_LISTXATTR;
1230 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1231 req->in.numargs = 1;
1232 req->in.args[0].size = sizeof(inarg);
1233 req->in.args[0].value = &inarg;
1234 /* This is really two different operations rolled into one */
1235 req->out.numargs = 1;
1236 if (size) {
1237 req->out.argvar = 1;
1238 req->out.args[0].size = size;
1239 req->out.args[0].value = list;
1240 } else {
1241 req->out.args[0].size = sizeof(outarg);
1242 req->out.args[0].value = &outarg;
1243 }
1244 request_send(fc, req);
1245 ret = req->out.h.error;
1246 if (!ret)
1247 ret = size ? req->out.args[0].size : outarg.size;
1248 else {
1249 if (ret == -ENOSYS) {
1250 fc->no_listxattr = 1;
1251 ret = -EOPNOTSUPP;
1252 }
1253 }
1254 fuse_put_request(fc, req);
1255 return ret;
1256}
1257
1258static int fuse_removexattr(struct dentry *entry, const char *name)
1259{
1260 struct inode *inode = entry->d_inode;
1261 struct fuse_conn *fc = get_fuse_conn(inode);
1262 struct fuse_req *req;
1263 int err;
1264
1265 if (fc->no_removexattr)
1266 return -EOPNOTSUPP;
1267
ce1d5a49
MS
1268 req = fuse_get_req(fc);
1269 if (IS_ERR(req))
1270 return PTR_ERR(req);
92a8780e
MS
1271
1272 req->in.h.opcode = FUSE_REMOVEXATTR;
1273 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1274 req->in.numargs = 1;
1275 req->in.args[0].size = strlen(name) + 1;
1276 req->in.args[0].value = name;
1277 request_send(fc, req);
1278 err = req->out.h.error;
1279 fuse_put_request(fc, req);
1280 if (err == -ENOSYS) {
1281 fc->no_removexattr = 1;
1282 err = -EOPNOTSUPP;
1283 }
1284 return err;
1285}
1286
754661f1 1287static const struct inode_operations fuse_dir_inode_operations = {
e5e5558e 1288 .lookup = fuse_lookup,
9e6268db
MS
1289 .mkdir = fuse_mkdir,
1290 .symlink = fuse_symlink,
1291 .unlink = fuse_unlink,
1292 .rmdir = fuse_rmdir,
1293 .rename = fuse_rename,
1294 .link = fuse_link,
1295 .setattr = fuse_setattr,
1296 .create = fuse_create,
1297 .mknod = fuse_mknod,
e5e5558e
MS
1298 .permission = fuse_permission,
1299 .getattr = fuse_getattr,
92a8780e
MS
1300 .setxattr = fuse_setxattr,
1301 .getxattr = fuse_getxattr,
1302 .listxattr = fuse_listxattr,
1303 .removexattr = fuse_removexattr,
e5e5558e
MS
1304};
1305
4b6f5d20 1306static const struct file_operations fuse_dir_operations = {
b6aeaded 1307 .llseek = generic_file_llseek,
e5e5558e
MS
1308 .read = generic_read_dir,
1309 .readdir = fuse_readdir,
1310 .open = fuse_dir_open,
1311 .release = fuse_dir_release,
82547981 1312 .fsync = fuse_dir_fsync,
e5e5558e
MS
1313};
1314
754661f1 1315static const struct inode_operations fuse_common_inode_operations = {
9e6268db 1316 .setattr = fuse_setattr,
e5e5558e
MS
1317 .permission = fuse_permission,
1318 .getattr = fuse_getattr,
92a8780e
MS
1319 .setxattr = fuse_setxattr,
1320 .getxattr = fuse_getxattr,
1321 .listxattr = fuse_listxattr,
1322 .removexattr = fuse_removexattr,
e5e5558e
MS
1323};
1324
754661f1 1325static const struct inode_operations fuse_symlink_inode_operations = {
9e6268db 1326 .setattr = fuse_setattr,
e5e5558e
MS
1327 .follow_link = fuse_follow_link,
1328 .put_link = fuse_put_link,
1329 .readlink = generic_readlink,
1330 .getattr = fuse_getattr,
92a8780e
MS
1331 .setxattr = fuse_setxattr,
1332 .getxattr = fuse_getxattr,
1333 .listxattr = fuse_listxattr,
1334 .removexattr = fuse_removexattr,
e5e5558e
MS
1335};
1336
1337void fuse_init_common(struct inode *inode)
1338{
1339 inode->i_op = &fuse_common_inode_operations;
1340}
1341
1342void fuse_init_dir(struct inode *inode)
1343{
1344 inode->i_op = &fuse_dir_inode_operations;
1345 inode->i_fop = &fuse_dir_operations;
1346}
1347
1348void fuse_init_symlink(struct inode *inode)
1349{
1350 inode->i_op = &fuse_symlink_inode_operations;
1351}