Merge remote-tracking branch 'spi/topic/build' into spi-next
[deliverable/linux.git] / fs / fuse / dir.c
CommitLineData
e5e5558e
MS
1/*
2 FUSE: Filesystem in Userspace
1729a16c 3 Copyright (C) 2001-2008 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>
e5e5558e
MS
13#include <linux/sched.h>
14#include <linux/namei.h>
07e77dca 15#include <linux/slab.h>
e5e5558e 16
8d3af7f3 17static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
4582a4ab
FS
18{
19 struct fuse_conn *fc = get_fuse_conn(dir);
20 struct fuse_inode *fi = get_fuse_inode(dir);
21
22 if (!fc->do_readdirplus)
23 return false;
634734b6
EW
24 if (!fc->readdirplus_auto)
25 return true;
4582a4ab
FS
26 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
27 return true;
8d3af7f3 28 if (ctx->pos == 0)
4582a4ab
FS
29 return true;
30 return false;
31}
32
33static void fuse_advise_use_readdirplus(struct inode *dir)
34{
35 struct fuse_inode *fi = get_fuse_inode(dir);
36
37 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
38}
39
0a0898cf
MS
40#if BITS_PER_LONG >= 64
41static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
42{
43 entry->d_time = time;
44}
45
46static inline u64 fuse_dentry_time(struct dentry *entry)
47{
48 return entry->d_time;
49}
50#else
51/*
52 * On 32 bit archs store the high 32 bits of time in d_fsdata
53 */
54static void fuse_dentry_settime(struct dentry *entry, u64 time)
55{
56 entry->d_time = time;
57 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
58}
59
60static u64 fuse_dentry_time(struct dentry *entry)
61{
62 return (u64) entry->d_time +
63 ((u64) (unsigned long) entry->d_fsdata << 32);
64}
65#endif
66
6f9f1180
MS
67/*
68 * FUSE caches dentries and attributes with separate timeout. The
69 * time in jiffies until the dentry/attributes are valid is stored in
70 * dentry->d_time and fuse_inode->i_time respectively.
71 */
72
73/*
74 * Calculate the time in jiffies until a dentry/attributes are valid
75 */
0a0898cf 76static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
e5e5558e 77{
685d16dd
MS
78 if (sec || nsec) {
79 struct timespec ts = {sec, nsec};
0a0898cf 80 return get_jiffies_64() + timespec_to_jiffies(&ts);
685d16dd 81 } else
0a0898cf 82 return 0;
e5e5558e
MS
83}
84
6f9f1180
MS
85/*
86 * Set dentry and possibly attribute timeouts from the lookup/mk*
87 * replies
88 */
1fb69e78
MS
89static void fuse_change_entry_timeout(struct dentry *entry,
90 struct fuse_entry_out *o)
0aa7c699 91{
0a0898cf
MS
92 fuse_dentry_settime(entry,
93 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
1fb69e78
MS
94}
95
96static u64 attr_timeout(struct fuse_attr_out *o)
97{
98 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
99}
100
101static u64 entry_attr_timeout(struct fuse_entry_out *o)
102{
103 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
8cbdf1e6
MS
104}
105
6f9f1180
MS
106/*
107 * Mark the attributes as stale, so that at the next call to
108 * ->getattr() they will be fetched from userspace
109 */
8cbdf1e6
MS
110void fuse_invalidate_attr(struct inode *inode)
111{
0a0898cf 112 get_fuse_inode(inode)->i_time = 0;
8cbdf1e6
MS
113}
114
6f9f1180
MS
115/*
116 * Just mark the entry as stale, so that a next attempt to look it up
117 * will result in a new lookup call to userspace
118 *
119 * This is called when a dentry is about to become negative and the
120 * timeout is unknown (unlink, rmdir, rename and in some cases
121 * lookup)
122 */
dbd561d2 123void fuse_invalidate_entry_cache(struct dentry *entry)
8cbdf1e6 124{
0a0898cf 125 fuse_dentry_settime(entry, 0);
8cbdf1e6
MS
126}
127
6f9f1180
MS
128/*
129 * Same as fuse_invalidate_entry_cache(), but also try to remove the
130 * dentry from the hash
131 */
8cbdf1e6
MS
132static void fuse_invalidate_entry(struct dentry *entry)
133{
134 d_invalidate(entry);
135 fuse_invalidate_entry_cache(entry);
0aa7c699
MS
136}
137
c180eebe
MS
138static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
139 u64 nodeid, struct qstr *name,
e5e5558e
MS
140 struct fuse_entry_out *outarg)
141{
0e9663ee 142 memset(outarg, 0, sizeof(struct fuse_entry_out));
e5e5558e 143 req->in.h.opcode = FUSE_LOOKUP;
c180eebe 144 req->in.h.nodeid = nodeid;
e5e5558e 145 req->in.numargs = 1;
c180eebe
MS
146 req->in.args[0].size = name->len + 1;
147 req->in.args[0].value = name->name;
e5e5558e 148 req->out.numargs = 1;
0e9663ee
MS
149 if (fc->minor < 9)
150 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
151 else
152 req->out.args[0].size = sizeof(struct fuse_entry_out);
e5e5558e
MS
153 req->out.args[0].value = outarg;
154}
155
5c5c5e51 156u64 fuse_get_attr_version(struct fuse_conn *fc)
7dca9fd3
MS
157{
158 u64 curr_version;
159
160 /*
161 * The spin lock isn't actually needed on 64bit archs, but we
162 * don't yet care too much about such optimizations.
163 */
164 spin_lock(&fc->lock);
165 curr_version = fc->attr_version;
166 spin_unlock(&fc->lock);
167
168 return curr_version;
169}
170
6f9f1180
MS
171/*
172 * Check whether the dentry is still valid
173 *
174 * If the entry validity timeout has expired and the dentry is
175 * positive, try to redo the lookup. If the lookup results in a
176 * different inode, then let the VFS invalidate the dentry and redo
177 * the lookup once more. If the lookup results in the same inode,
178 * then refresh the attributes, timeouts and mark the dentry valid.
179 */
0b728e19 180static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
e5e5558e 181{
34286d66 182 struct inode *inode;
28420dad
MS
183 struct dentry *parent;
184 struct fuse_conn *fc;
8cbdf1e6 185
e7c0a167 186 inode = ACCESS_ONCE(entry->d_inode);
8cbdf1e6 187 if (inode && is_bad_inode(inode))
e5e5558e 188 return 0;
0a0898cf 189 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
e5e5558e 190 int err;
e5e5558e 191 struct fuse_entry_out outarg;
8cbdf1e6 192 struct fuse_req *req;
07e77dca 193 struct fuse_forget_link *forget;
1fb69e78 194 u64 attr_version;
8cbdf1e6 195
50322fe7 196 /* For negative dentries, always do a fresh lookup */
8cbdf1e6
MS
197 if (!inode)
198 return 0;
199
0b728e19 200 if (flags & LOOKUP_RCU)
e7c0a167
MS
201 return -ECHILD;
202
8cbdf1e6 203 fc = get_fuse_conn(inode);
b111c8c0 204 req = fuse_get_req_nopages(fc);
ce1d5a49 205 if (IS_ERR(req))
e5e5558e
MS
206 return 0;
207
07e77dca
MS
208 forget = fuse_alloc_forget();
209 if (!forget) {
2d51013e
MS
210 fuse_put_request(fc, req);
211 return 0;
212 }
213
7dca9fd3 214 attr_version = fuse_get_attr_version(fc);
1fb69e78 215
e956edd0 216 parent = dget_parent(entry);
c180eebe
MS
217 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
218 &entry->d_name, &outarg);
b93f858a 219 fuse_request_send(fc, req);
e956edd0 220 dput(parent);
e5e5558e 221 err = req->out.h.error;
2d51013e 222 fuse_put_request(fc, req);
50322fe7
MS
223 /* Zero nodeid is same as -ENOENT */
224 if (!err && !outarg.nodeid)
225 err = -ENOENT;
9e6268db 226 if (!err) {
8cbdf1e6 227 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 228 if (outarg.nodeid != get_node_id(inode)) {
07e77dca 229 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
9e6268db
MS
230 return 0;
231 }
8da5ff23 232 spin_lock(&fc->lock);
1729a16c 233 fi->nlookup++;
8da5ff23 234 spin_unlock(&fc->lock);
9e6268db 235 }
07e77dca 236 kfree(forget);
9e6268db 237 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
e5e5558e
MS
238 return 0;
239
1fb69e78
MS
240 fuse_change_attributes(inode, &outarg.attr,
241 entry_attr_timeout(&outarg),
242 attr_version);
243 fuse_change_entry_timeout(entry, &outarg);
28420dad
MS
244 } else if (inode) {
245 fc = get_fuse_conn(inode);
246 if (fc->readdirplus_auto) {
247 parent = dget_parent(entry);
248 fuse_advise_use_readdirplus(parent->d_inode);
249 dput(parent);
250 }
e5e5558e
MS
251 }
252 return 1;
253}
254
8bfc016d 255static int invalid_nodeid(u64 nodeid)
2827d0b2
MS
256{
257 return !nodeid || nodeid == FUSE_ROOT_ID;
258}
259
4269590a 260const struct dentry_operations fuse_dentry_operations = {
e5e5558e
MS
261 .d_revalidate = fuse_dentry_revalidate,
262};
263
a5bfffac 264int fuse_valid_type(int m)
39ee059a
MS
265{
266 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
267 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
268}
269
d2a85164
MS
270/*
271 * Add a directory inode to a dentry, ensuring that no other dentry
272 * refers to this inode. Called with fc->inst_mutex.
273 */
0de6256d
MS
274static struct dentry *fuse_d_add_directory(struct dentry *entry,
275 struct inode *inode)
d2a85164
MS
276{
277 struct dentry *alias = d_find_alias(inode);
0de6256d 278 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
d2a85164
MS
279 /* This tries to shrink the subtree below alias */
280 fuse_invalidate_entry(alias);
281 dput(alias);
b3d9b7a3 282 if (!hlist_empty(&inode->i_dentry))
0de6256d
MS
283 return ERR_PTR(-EBUSY);
284 } else {
285 dput(alias);
d2a85164 286 }
0de6256d 287 return d_splice_alias(inode, entry);
d2a85164
MS
288}
289
c180eebe
MS
290int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
291 struct fuse_entry_out *outarg, struct inode **inode)
e5e5558e 292{
c180eebe 293 struct fuse_conn *fc = get_fuse_conn_super(sb);
e5e5558e 294 struct fuse_req *req;
07e77dca 295 struct fuse_forget_link *forget;
1fb69e78 296 u64 attr_version;
c180eebe 297 int err;
e5e5558e 298
c180eebe
MS
299 *inode = NULL;
300 err = -ENAMETOOLONG;
301 if (name->len > FUSE_NAME_MAX)
302 goto out;
e5e5558e 303
b111c8c0 304 req = fuse_get_req_nopages(fc);
c180eebe 305 err = PTR_ERR(req);
ce1d5a49 306 if (IS_ERR(req))
c180eebe 307 goto out;
e5e5558e 308
07e77dca
MS
309 forget = fuse_alloc_forget();
310 err = -ENOMEM;
311 if (!forget) {
2d51013e 312 fuse_put_request(fc, req);
c180eebe 313 goto out;
2d51013e
MS
314 }
315
7dca9fd3 316 attr_version = fuse_get_attr_version(fc);
1fb69e78 317
c180eebe 318 fuse_lookup_init(fc, req, nodeid, name, outarg);
b93f858a 319 fuse_request_send(fc, req);
e5e5558e 320 err = req->out.h.error;
2d51013e 321 fuse_put_request(fc, req);
50322fe7 322 /* Zero nodeid is same as -ENOENT, but with valid timeout */
c180eebe
MS
323 if (err || !outarg->nodeid)
324 goto out_put_forget;
325
326 err = -EIO;
327 if (!outarg->nodeid)
328 goto out_put_forget;
329 if (!fuse_valid_type(outarg->attr.mode))
330 goto out_put_forget;
331
332 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
333 &outarg->attr, entry_attr_timeout(outarg),
334 attr_version);
335 err = -ENOMEM;
336 if (!*inode) {
07e77dca 337 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
c180eebe 338 goto out;
e5e5558e 339 }
c180eebe
MS
340 err = 0;
341
342 out_put_forget:
07e77dca 343 kfree(forget);
c180eebe
MS
344 out:
345 return err;
346}
347
348static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
00cd8dd3 349 unsigned int flags)
c180eebe
MS
350{
351 int err;
352 struct fuse_entry_out outarg;
353 struct inode *inode;
354 struct dentry *newent;
355 struct fuse_conn *fc = get_fuse_conn(dir);
356 bool outarg_valid = true;
357
358 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
359 &outarg, &inode);
360 if (err == -ENOENT) {
361 outarg_valid = false;
362 err = 0;
363 }
364 if (err)
365 goto out_err;
366
367 err = -EIO;
368 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
369 goto out_iput;
e5e5558e 370
d2a85164
MS
371 if (inode && S_ISDIR(inode->i_mode)) {
372 mutex_lock(&fc->inst_mutex);
0de6256d 373 newent = fuse_d_add_directory(entry, inode);
d2a85164 374 mutex_unlock(&fc->inst_mutex);
c180eebe
MS
375 err = PTR_ERR(newent);
376 if (IS_ERR(newent))
377 goto out_iput;
378 } else {
0de6256d 379 newent = d_splice_alias(inode, entry);
c180eebe 380 }
d2a85164 381
0de6256d 382 entry = newent ? newent : entry;
c180eebe 383 if (outarg_valid)
1fb69e78 384 fuse_change_entry_timeout(entry, &outarg);
8cbdf1e6
MS
385 else
386 fuse_invalidate_entry_cache(entry);
c180eebe 387
4582a4ab 388 fuse_advise_use_readdirplus(dir);
0de6256d 389 return newent;
c180eebe
MS
390
391 out_iput:
392 iput(inode);
393 out_err:
394 return ERR_PTR(err);
e5e5558e
MS
395}
396
6f9f1180
MS
397/*
398 * Atomic create+open operation
399 *
400 * If the filesystem doesn't support this, then fall back to separate
401 * 'mknod' + 'open' requests.
402 */
d9585277 403static int fuse_create_open(struct inode *dir, struct dentry *entry,
30d90494 404 struct file *file, unsigned flags,
d9585277 405 umode_t mode, int *opened)
fd72faac
MS
406{
407 int err;
408 struct inode *inode;
409 struct fuse_conn *fc = get_fuse_conn(dir);
410 struct fuse_req *req;
07e77dca 411 struct fuse_forget_link *forget;
e0a43ddc 412 struct fuse_create_in inarg;
fd72faac
MS
413 struct fuse_open_out outopen;
414 struct fuse_entry_out outentry;
fd72faac 415 struct fuse_file *ff;
fd72faac 416
af109bca
MS
417 /* Userspace expects S_IFREG in create mode */
418 BUG_ON((mode & S_IFMT) != S_IFREG);
419
07e77dca 420 forget = fuse_alloc_forget();
c8ccbe03 421 err = -ENOMEM;
07e77dca 422 if (!forget)
c8ccbe03 423 goto out_err;
51eb01e7 424
b111c8c0 425 req = fuse_get_req_nopages(fc);
51eb01e7 426 err = PTR_ERR(req);
ce1d5a49 427 if (IS_ERR(req))
51eb01e7 428 goto out_put_forget_req;
fd72faac 429
ce1d5a49 430 err = -ENOMEM;
acf99433 431 ff = fuse_file_alloc(fc);
fd72faac
MS
432 if (!ff)
433 goto out_put_request;
434
e0a43ddc
MS
435 if (!fc->dont_mask)
436 mode &= ~current_umask();
437
fd72faac
MS
438 flags &= ~O_NOCTTY;
439 memset(&inarg, 0, sizeof(inarg));
0e9663ee 440 memset(&outentry, 0, sizeof(outentry));
fd72faac
MS
441 inarg.flags = flags;
442 inarg.mode = mode;
e0a43ddc 443 inarg.umask = current_umask();
fd72faac
MS
444 req->in.h.opcode = FUSE_CREATE;
445 req->in.h.nodeid = get_node_id(dir);
fd72faac 446 req->in.numargs = 2;
e0a43ddc
MS
447 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
448 sizeof(inarg);
fd72faac
MS
449 req->in.args[0].value = &inarg;
450 req->in.args[1].size = entry->d_name.len + 1;
451 req->in.args[1].value = entry->d_name.name;
452 req->out.numargs = 2;
0e9663ee
MS
453 if (fc->minor < 9)
454 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
455 else
456 req->out.args[0].size = sizeof(outentry);
fd72faac
MS
457 req->out.args[0].value = &outentry;
458 req->out.args[1].size = sizeof(outopen);
459 req->out.args[1].value = &outopen;
b93f858a 460 fuse_request_send(fc, req);
fd72faac 461 err = req->out.h.error;
c8ccbe03 462 if (err)
fd72faac 463 goto out_free_ff;
fd72faac
MS
464
465 err = -EIO;
2827d0b2 466 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
fd72faac
MS
467 goto out_free_ff;
468
51eb01e7 469 fuse_put_request(fc, req);
c7b7143c
MS
470 ff->fh = outopen.fh;
471 ff->nodeid = outentry.nodeid;
472 ff->open_flags = outopen.open_flags;
fd72faac 473 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
1fb69e78 474 &outentry.attr, entry_attr_timeout(&outentry), 0);
fd72faac
MS
475 if (!inode) {
476 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
8b0797a4 477 fuse_sync_release(ff, flags);
07e77dca 478 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
c8ccbe03
MS
479 err = -ENOMEM;
480 goto out_err;
fd72faac 481 }
07e77dca 482 kfree(forget);
fd72faac 483 d_instantiate(entry, inode);
1fb69e78 484 fuse_change_entry_timeout(entry, &outentry);
0952b2a4 485 fuse_invalidate_attr(dir);
30d90494
AV
486 err = finish_open(file, entry, generic_file_open, opened);
487 if (err) {
8b0797a4 488 fuse_sync_release(ff, flags);
c8ccbe03
MS
489 } else {
490 file->private_data = fuse_file_get(ff);
491 fuse_finish_open(inode, file);
fd72faac 492 }
d9585277 493 return err;
fd72faac 494
c8ccbe03 495out_free_ff:
fd72faac 496 fuse_file_free(ff);
c8ccbe03 497out_put_request:
fd72faac 498 fuse_put_request(fc, req);
c8ccbe03 499out_put_forget_req:
07e77dca 500 kfree(forget);
c8ccbe03 501out_err:
d9585277 502 return err;
c8ccbe03
MS
503}
504
505static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
d9585277 506static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
30d90494 507 struct file *file, unsigned flags,
d9585277 508 umode_t mode, int *opened)
c8ccbe03
MS
509{
510 int err;
511 struct fuse_conn *fc = get_fuse_conn(dir);
c8ccbe03
MS
512 struct dentry *res = NULL;
513
514 if (d_unhashed(entry)) {
00cd8dd3 515 res = fuse_lookup(dir, entry, 0);
c8ccbe03 516 if (IS_ERR(res))
d9585277 517 return PTR_ERR(res);
c8ccbe03
MS
518
519 if (res)
520 entry = res;
521 }
522
523 if (!(flags & O_CREAT) || entry->d_inode)
524 goto no_open;
525
526 /* Only creates */
47237687 527 *opened |= FILE_CREATED;
c8ccbe03
MS
528
529 if (fc->no_create)
530 goto mknod;
531
30d90494 532 err = fuse_create_open(dir, entry, file, flags, mode, opened);
d9585277 533 if (err == -ENOSYS) {
c8ccbe03
MS
534 fc->no_create = 1;
535 goto mknod;
536 }
537out_dput:
538 dput(res);
d9585277 539 return err;
c8ccbe03
MS
540
541mknod:
542 err = fuse_mknod(dir, entry, mode, 0);
d9585277 543 if (err)
c8ccbe03 544 goto out_dput;
c8ccbe03 545no_open:
e45198a6 546 return finish_no_open(file, res);
fd72faac
MS
547}
548
6f9f1180
MS
549/*
550 * Code shared between mknod, mkdir, symlink and link
551 */
9e6268db
MS
552static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
553 struct inode *dir, struct dentry *entry,
541af6a0 554 umode_t mode)
9e6268db
MS
555{
556 struct fuse_entry_out outarg;
557 struct inode *inode;
9e6268db 558 int err;
07e77dca 559 struct fuse_forget_link *forget;
2d51013e 560
07e77dca
MS
561 forget = fuse_alloc_forget();
562 if (!forget) {
2d51013e 563 fuse_put_request(fc, req);
07e77dca 564 return -ENOMEM;
2d51013e 565 }
9e6268db 566
0e9663ee 567 memset(&outarg, 0, sizeof(outarg));
9e6268db 568 req->in.h.nodeid = get_node_id(dir);
9e6268db 569 req->out.numargs = 1;
0e9663ee
MS
570 if (fc->minor < 9)
571 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
572 else
573 req->out.args[0].size = sizeof(outarg);
9e6268db 574 req->out.args[0].value = &outarg;
b93f858a 575 fuse_request_send(fc, req);
9e6268db 576 err = req->out.h.error;
2d51013e
MS
577 fuse_put_request(fc, req);
578 if (err)
579 goto out_put_forget_req;
580
39ee059a
MS
581 err = -EIO;
582 if (invalid_nodeid(outarg.nodeid))
2d51013e 583 goto out_put_forget_req;
39ee059a
MS
584
585 if ((outarg.attr.mode ^ mode) & S_IFMT)
2d51013e 586 goto out_put_forget_req;
39ee059a 587
9e6268db 588 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
1fb69e78 589 &outarg.attr, entry_attr_timeout(&outarg), 0);
9e6268db 590 if (!inode) {
07e77dca 591 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
9e6268db
MS
592 return -ENOMEM;
593 }
07e77dca 594 kfree(forget);
9e6268db 595
d2a85164
MS
596 if (S_ISDIR(inode->i_mode)) {
597 struct dentry *alias;
598 mutex_lock(&fc->inst_mutex);
599 alias = d_find_alias(inode);
600 if (alias) {
601 /* New directory must have moved since mkdir */
602 mutex_unlock(&fc->inst_mutex);
603 dput(alias);
604 iput(inode);
605 return -EBUSY;
606 }
607 d_instantiate(entry, inode);
608 mutex_unlock(&fc->inst_mutex);
609 } else
610 d_instantiate(entry, inode);
9e6268db 611
1fb69e78 612 fuse_change_entry_timeout(entry, &outarg);
9e6268db
MS
613 fuse_invalidate_attr(dir);
614 return 0;
39ee059a 615
2d51013e 616 out_put_forget_req:
07e77dca 617 kfree(forget);
39ee059a 618 return err;
9e6268db
MS
619}
620
1a67aafb 621static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
9e6268db
MS
622 dev_t rdev)
623{
624 struct fuse_mknod_in inarg;
625 struct fuse_conn *fc = get_fuse_conn(dir);
b111c8c0 626 struct fuse_req *req = fuse_get_req_nopages(fc);
ce1d5a49
MS
627 if (IS_ERR(req))
628 return PTR_ERR(req);
9e6268db 629
e0a43ddc
MS
630 if (!fc->dont_mask)
631 mode &= ~current_umask();
632
9e6268db
MS
633 memset(&inarg, 0, sizeof(inarg));
634 inarg.mode = mode;
635 inarg.rdev = new_encode_dev(rdev);
e0a43ddc 636 inarg.umask = current_umask();
9e6268db
MS
637 req->in.h.opcode = FUSE_MKNOD;
638 req->in.numargs = 2;
e0a43ddc
MS
639 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
640 sizeof(inarg);
9e6268db
MS
641 req->in.args[0].value = &inarg;
642 req->in.args[1].size = entry->d_name.len + 1;
643 req->in.args[1].value = entry->d_name.name;
644 return create_new_entry(fc, req, dir, entry, mode);
645}
646
4acdaf27 647static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
ebfc3b49 648 bool excl)
9e6268db
MS
649{
650 return fuse_mknod(dir, entry, mode, 0);
651}
652
18bb1db3 653static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
9e6268db
MS
654{
655 struct fuse_mkdir_in inarg;
656 struct fuse_conn *fc = get_fuse_conn(dir);
b111c8c0 657 struct fuse_req *req = fuse_get_req_nopages(fc);
ce1d5a49
MS
658 if (IS_ERR(req))
659 return PTR_ERR(req);
9e6268db 660
e0a43ddc
MS
661 if (!fc->dont_mask)
662 mode &= ~current_umask();
663
9e6268db
MS
664 memset(&inarg, 0, sizeof(inarg));
665 inarg.mode = mode;
e0a43ddc 666 inarg.umask = current_umask();
9e6268db
MS
667 req->in.h.opcode = FUSE_MKDIR;
668 req->in.numargs = 2;
669 req->in.args[0].size = sizeof(inarg);
670 req->in.args[0].value = &inarg;
671 req->in.args[1].size = entry->d_name.len + 1;
672 req->in.args[1].value = entry->d_name.name;
673 return create_new_entry(fc, req, dir, entry, S_IFDIR);
674}
675
676static int fuse_symlink(struct inode *dir, struct dentry *entry,
677 const char *link)
678{
679 struct fuse_conn *fc = get_fuse_conn(dir);
680 unsigned len = strlen(link) + 1;
b111c8c0 681 struct fuse_req *req = fuse_get_req_nopages(fc);
ce1d5a49
MS
682 if (IS_ERR(req))
683 return PTR_ERR(req);
9e6268db
MS
684
685 req->in.h.opcode = FUSE_SYMLINK;
686 req->in.numargs = 2;
687 req->in.args[0].size = entry->d_name.len + 1;
688 req->in.args[0].value = entry->d_name.name;
689 req->in.args[1].size = len;
690 req->in.args[1].value = link;
691 return create_new_entry(fc, req, dir, entry, S_IFLNK);
692}
693
694static int fuse_unlink(struct inode *dir, struct dentry *entry)
695{
696 int err;
697 struct fuse_conn *fc = get_fuse_conn(dir);
b111c8c0 698 struct fuse_req *req = fuse_get_req_nopages(fc);
ce1d5a49
MS
699 if (IS_ERR(req))
700 return PTR_ERR(req);
9e6268db
MS
701
702 req->in.h.opcode = FUSE_UNLINK;
703 req->in.h.nodeid = get_node_id(dir);
9e6268db
MS
704 req->in.numargs = 1;
705 req->in.args[0].size = entry->d_name.len + 1;
706 req->in.args[0].value = entry->d_name.name;
b93f858a 707 fuse_request_send(fc, req);
9e6268db
MS
708 err = req->out.h.error;
709 fuse_put_request(fc, req);
710 if (!err) {
711 struct inode *inode = entry->d_inode;
ac45d613 712 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 713
ac45d613
MS
714 spin_lock(&fc->lock);
715 fi->attr_version = ++fc->attr_version;
dfca7ceb
MS
716 /*
717 * If i_nlink == 0 then unlink doesn't make sense, yet this can
718 * happen if userspace filesystem is careless. It would be
719 * difficult to enforce correct nlink usage so just ignore this
720 * condition here
721 */
722 if (inode->i_nlink > 0)
723 drop_nlink(inode);
ac45d613 724 spin_unlock(&fc->lock);
9e6268db
MS
725 fuse_invalidate_attr(inode);
726 fuse_invalidate_attr(dir);
8cbdf1e6 727 fuse_invalidate_entry_cache(entry);
9e6268db
MS
728 } else if (err == -EINTR)
729 fuse_invalidate_entry(entry);
730 return err;
731}
732
733static int fuse_rmdir(struct inode *dir, struct dentry *entry)
734{
735 int err;
736 struct fuse_conn *fc = get_fuse_conn(dir);
b111c8c0 737 struct fuse_req *req = fuse_get_req_nopages(fc);
ce1d5a49
MS
738 if (IS_ERR(req))
739 return PTR_ERR(req);
9e6268db
MS
740
741 req->in.h.opcode = FUSE_RMDIR;
742 req->in.h.nodeid = get_node_id(dir);
9e6268db
MS
743 req->in.numargs = 1;
744 req->in.args[0].size = entry->d_name.len + 1;
745 req->in.args[0].value = entry->d_name.name;
b93f858a 746 fuse_request_send(fc, req);
9e6268db
MS
747 err = req->out.h.error;
748 fuse_put_request(fc, req);
749 if (!err) {
ce71ec36 750 clear_nlink(entry->d_inode);
9e6268db 751 fuse_invalidate_attr(dir);
8cbdf1e6 752 fuse_invalidate_entry_cache(entry);
9e6268db
MS
753 } else if (err == -EINTR)
754 fuse_invalidate_entry(entry);
755 return err;
756}
757
758static int fuse_rename(struct inode *olddir, struct dentry *oldent,
759 struct inode *newdir, struct dentry *newent)
760{
761 int err;
762 struct fuse_rename_in inarg;
763 struct fuse_conn *fc = get_fuse_conn(olddir);
b111c8c0 764 struct fuse_req *req = fuse_get_req_nopages(fc);
e4eaac06 765
ce1d5a49
MS
766 if (IS_ERR(req))
767 return PTR_ERR(req);
9e6268db
MS
768
769 memset(&inarg, 0, sizeof(inarg));
770 inarg.newdir = get_node_id(newdir);
771 req->in.h.opcode = FUSE_RENAME;
772 req->in.h.nodeid = get_node_id(olddir);
9e6268db
MS
773 req->in.numargs = 3;
774 req->in.args[0].size = sizeof(inarg);
775 req->in.args[0].value = &inarg;
776 req->in.args[1].size = oldent->d_name.len + 1;
777 req->in.args[1].value = oldent->d_name.name;
778 req->in.args[2].size = newent->d_name.len + 1;
779 req->in.args[2].value = newent->d_name.name;
b93f858a 780 fuse_request_send(fc, req);
9e6268db
MS
781 err = req->out.h.error;
782 fuse_put_request(fc, req);
783 if (!err) {
08b63307
MS
784 /* ctime changes */
785 fuse_invalidate_attr(oldent->d_inode);
786
9e6268db
MS
787 fuse_invalidate_attr(olddir);
788 if (olddir != newdir)
789 fuse_invalidate_attr(newdir);
8cbdf1e6
MS
790
791 /* newent will end up negative */
5219f346
MS
792 if (newent->d_inode) {
793 fuse_invalidate_attr(newent->d_inode);
8cbdf1e6 794 fuse_invalidate_entry_cache(newent);
5219f346 795 }
9e6268db
MS
796 } else if (err == -EINTR) {
797 /* If request was interrupted, DEITY only knows if the
798 rename actually took place. If the invalidation
799 fails (e.g. some process has CWD under the renamed
800 directory), then there can be inconsistency between
801 the dcache and the real filesystem. Tough luck. */
802 fuse_invalidate_entry(oldent);
803 if (newent->d_inode)
804 fuse_invalidate_entry(newent);
805 }
806
807 return err;
808}
809
810static int fuse_link(struct dentry *entry, struct inode *newdir,
811 struct dentry *newent)
812{
813 int err;
814 struct fuse_link_in inarg;
815 struct inode *inode = entry->d_inode;
816 struct fuse_conn *fc = get_fuse_conn(inode);
b111c8c0 817 struct fuse_req *req = fuse_get_req_nopages(fc);
ce1d5a49
MS
818 if (IS_ERR(req))
819 return PTR_ERR(req);
9e6268db
MS
820
821 memset(&inarg, 0, sizeof(inarg));
822 inarg.oldnodeid = get_node_id(inode);
823 req->in.h.opcode = FUSE_LINK;
9e6268db
MS
824 req->in.numargs = 2;
825 req->in.args[0].size = sizeof(inarg);
826 req->in.args[0].value = &inarg;
827 req->in.args[1].size = newent->d_name.len + 1;
828 req->in.args[1].value = newent->d_name.name;
829 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
830 /* Contrary to "normal" filesystems it can happen that link
831 makes two "logical" inodes point to the same "physical"
832 inode. We invalidate the attributes of the old one, so it
833 will reflect changes in the backing inode (link count,
834 etc.)
835 */
ac45d613
MS
836 if (!err) {
837 struct fuse_inode *fi = get_fuse_inode(inode);
838
839 spin_lock(&fc->lock);
840 fi->attr_version = ++fc->attr_version;
841 inc_nlink(inode);
842 spin_unlock(&fc->lock);
9e6268db 843 fuse_invalidate_attr(inode);
ac45d613
MS
844 } else if (err == -EINTR) {
845 fuse_invalidate_attr(inode);
846 }
9e6268db
MS
847 return err;
848}
849
1fb69e78
MS
850static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
851 struct kstat *stat)
852{
203627bb
MS
853 unsigned int blkbits;
854
1fb69e78
MS
855 stat->dev = inode->i_sb->s_dev;
856 stat->ino = attr->ino;
857 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
858 stat->nlink = attr->nlink;
499dcf20
EB
859 stat->uid = make_kuid(&init_user_ns, attr->uid);
860 stat->gid = make_kgid(&init_user_ns, attr->gid);
1fb69e78
MS
861 stat->rdev = inode->i_rdev;
862 stat->atime.tv_sec = attr->atime;
863 stat->atime.tv_nsec = attr->atimensec;
864 stat->mtime.tv_sec = attr->mtime;
865 stat->mtime.tv_nsec = attr->mtimensec;
866 stat->ctime.tv_sec = attr->ctime;
867 stat->ctime.tv_nsec = attr->ctimensec;
868 stat->size = attr->size;
869 stat->blocks = attr->blocks;
203627bb
MS
870
871 if (attr->blksize != 0)
872 blkbits = ilog2(attr->blksize);
873 else
874 blkbits = inode->i_sb->s_blocksize_bits;
875
876 stat->blksize = 1 << blkbits;
1fb69e78
MS
877}
878
c79e322f
MS
879static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
880 struct file *file)
e5e5558e
MS
881{
882 int err;
c79e322f
MS
883 struct fuse_getattr_in inarg;
884 struct fuse_attr_out outarg;
e5e5558e 885 struct fuse_conn *fc = get_fuse_conn(inode);
1fb69e78
MS
886 struct fuse_req *req;
887 u64 attr_version;
888
b111c8c0 889 req = fuse_get_req_nopages(fc);
ce1d5a49
MS
890 if (IS_ERR(req))
891 return PTR_ERR(req);
e5e5558e 892
7dca9fd3 893 attr_version = fuse_get_attr_version(fc);
1fb69e78 894
c79e322f 895 memset(&inarg, 0, sizeof(inarg));
0e9663ee 896 memset(&outarg, 0, sizeof(outarg));
c79e322f
MS
897 /* Directories have separate file-handle space */
898 if (file && S_ISREG(inode->i_mode)) {
899 struct fuse_file *ff = file->private_data;
900
901 inarg.getattr_flags |= FUSE_GETATTR_FH;
902 inarg.fh = ff->fh;
903 }
e5e5558e
MS
904 req->in.h.opcode = FUSE_GETATTR;
905 req->in.h.nodeid = get_node_id(inode);
c79e322f
MS
906 req->in.numargs = 1;
907 req->in.args[0].size = sizeof(inarg);
908 req->in.args[0].value = &inarg;
e5e5558e 909 req->out.numargs = 1;
0e9663ee
MS
910 if (fc->minor < 9)
911 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
912 else
913 req->out.args[0].size = sizeof(outarg);
c79e322f 914 req->out.args[0].value = &outarg;
b93f858a 915 fuse_request_send(fc, req);
e5e5558e
MS
916 err = req->out.h.error;
917 fuse_put_request(fc, req);
918 if (!err) {
c79e322f 919 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
e5e5558e
MS
920 make_bad_inode(inode);
921 err = -EIO;
922 } else {
c79e322f
MS
923 fuse_change_attributes(inode, &outarg.attr,
924 attr_timeout(&outarg),
1fb69e78
MS
925 attr_version);
926 if (stat)
c79e322f 927 fuse_fillattr(inode, &outarg.attr, stat);
e5e5558e
MS
928 }
929 }
930 return err;
931}
932
bcb4be80
MS
933int fuse_update_attributes(struct inode *inode, struct kstat *stat,
934 struct file *file, bool *refreshed)
935{
936 struct fuse_inode *fi = get_fuse_inode(inode);
937 int err;
938 bool r;
939
940 if (fi->i_time < get_jiffies_64()) {
941 r = true;
942 err = fuse_do_getattr(inode, stat, file);
943 } else {
944 r = false;
945 err = 0;
946 if (stat) {
947 generic_fillattr(inode, stat);
948 stat->mode = fi->orig_i_mode;
45c72cd7 949 stat->ino = fi->orig_ino;
bcb4be80
MS
950 }
951 }
952
953 if (refreshed != NULL)
954 *refreshed = r;
955
956 return err;
957}
958
3b463ae0 959int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
451d0f59 960 u64 child_nodeid, struct qstr *name)
3b463ae0
JM
961{
962 int err = -ENOTDIR;
963 struct inode *parent;
964 struct dentry *dir;
965 struct dentry *entry;
966
967 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
968 if (!parent)
969 return -ENOENT;
970
971 mutex_lock(&parent->i_mutex);
972 if (!S_ISDIR(parent->i_mode))
973 goto unlock;
974
975 err = -ENOENT;
976 dir = d_find_alias(parent);
977 if (!dir)
978 goto unlock;
979
980 entry = d_lookup(dir, name);
981 dput(dir);
982 if (!entry)
983 goto unlock;
984
985 fuse_invalidate_attr(parent);
986 fuse_invalidate_entry(entry);
451d0f59
JM
987
988 if (child_nodeid != 0 && entry->d_inode) {
989 mutex_lock(&entry->d_inode->i_mutex);
990 if (get_node_id(entry->d_inode) != child_nodeid) {
991 err = -ENOENT;
992 goto badentry;
993 }
994 if (d_mountpoint(entry)) {
995 err = -EBUSY;
996 goto badentry;
997 }
998 if (S_ISDIR(entry->d_inode->i_mode)) {
999 shrink_dcache_parent(entry);
1000 if (!simple_empty(entry)) {
1001 err = -ENOTEMPTY;
1002 goto badentry;
1003 }
1004 entry->d_inode->i_flags |= S_DEAD;
1005 }
1006 dont_mount(entry);
1007 clear_nlink(entry->d_inode);
1008 err = 0;
1009 badentry:
1010 mutex_unlock(&entry->d_inode->i_mutex);
1011 if (!err)
1012 d_delete(entry);
1013 } else {
1014 err = 0;
1015 }
3b463ae0 1016 dput(entry);
3b463ae0
JM
1017
1018 unlock:
1019 mutex_unlock(&parent->i_mutex);
1020 iput(parent);
1021 return err;
1022}
1023
87729a55
MS
1024/*
1025 * Calling into a user-controlled filesystem gives the filesystem
c2132c1b 1026 * daemon ptrace-like capabilities over the current process. This
87729a55
MS
1027 * means, that the filesystem daemon is able to record the exact
1028 * filesystem operations performed, and can also control the behavior
1029 * of the requester process in otherwise impossible ways. For example
1030 * it can delay the operation for arbitrary length of time allowing
1031 * DoS against the requester.
1032 *
1033 * For this reason only those processes can call into the filesystem,
1034 * for which the owner of the mount has ptrace privilege. This
1035 * excludes processes started by other users, suid or sgid processes.
1036 */
c2132c1b 1037int fuse_allow_current_process(struct fuse_conn *fc)
87729a55 1038{
c69e8d9c 1039 const struct cred *cred;
87729a55 1040
c69e8d9c 1041 if (fc->flags & FUSE_ALLOW_OTHER)
87729a55
MS
1042 return 1;
1043
c2132c1b 1044 cred = current_cred();
499dcf20
EB
1045 if (uid_eq(cred->euid, fc->user_id) &&
1046 uid_eq(cred->suid, fc->user_id) &&
1047 uid_eq(cred->uid, fc->user_id) &&
1048 gid_eq(cred->egid, fc->group_id) &&
1049 gid_eq(cred->sgid, fc->group_id) &&
1050 gid_eq(cred->gid, fc->group_id))
c2132c1b 1051 return 1;
c69e8d9c 1052
c2132c1b 1053 return 0;
87729a55
MS
1054}
1055
31d40d74
MS
1056static int fuse_access(struct inode *inode, int mask)
1057{
1058 struct fuse_conn *fc = get_fuse_conn(inode);
1059 struct fuse_req *req;
1060 struct fuse_access_in inarg;
1061 int err;
1062
1063 if (fc->no_access)
1064 return 0;
1065
b111c8c0 1066 req = fuse_get_req_nopages(fc);
ce1d5a49
MS
1067 if (IS_ERR(req))
1068 return PTR_ERR(req);
31d40d74
MS
1069
1070 memset(&inarg, 0, sizeof(inarg));
e6305c43 1071 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
31d40d74
MS
1072 req->in.h.opcode = FUSE_ACCESS;
1073 req->in.h.nodeid = get_node_id(inode);
31d40d74
MS
1074 req->in.numargs = 1;
1075 req->in.args[0].size = sizeof(inarg);
1076 req->in.args[0].value = &inarg;
b93f858a 1077 fuse_request_send(fc, req);
31d40d74
MS
1078 err = req->out.h.error;
1079 fuse_put_request(fc, req);
1080 if (err == -ENOSYS) {
1081 fc->no_access = 1;
1082 err = 0;
1083 }
1084 return err;
1085}
1086
10556cb2 1087static int fuse_perm_getattr(struct inode *inode, int mask)
19690ddb 1088{
10556cb2 1089 if (mask & MAY_NOT_BLOCK)
19690ddb
MS
1090 return -ECHILD;
1091
1092 return fuse_do_getattr(inode, NULL, NULL);
1093}
1094
6f9f1180
MS
1095/*
1096 * Check permission. The two basic access models of FUSE are:
1097 *
1098 * 1) Local access checking ('default_permissions' mount option) based
1099 * on file mode. This is the plain old disk filesystem permission
1100 * modell.
1101 *
1102 * 2) "Remote" access checking, where server is responsible for
1103 * checking permission in each inode operation. An exception to this
1104 * is if ->permission() was invoked from sys_access() in which case an
1105 * access request is sent. Execute permission is still checked
1106 * locally based on file mode.
1107 */
10556cb2 1108static int fuse_permission(struct inode *inode, int mask)
e5e5558e
MS
1109{
1110 struct fuse_conn *fc = get_fuse_conn(inode);
244f6385
MS
1111 bool refreshed = false;
1112 int err = 0;
e5e5558e 1113
c2132c1b 1114 if (!fuse_allow_current_process(fc))
e5e5558e 1115 return -EACCES;
244f6385
MS
1116
1117 /*
e8e96157 1118 * If attributes are needed, refresh them before proceeding
244f6385 1119 */
e8e96157
MS
1120 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1121 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
19690ddb
MS
1122 struct fuse_inode *fi = get_fuse_inode(inode);
1123
1124 if (fi->i_time < get_jiffies_64()) {
1125 refreshed = true;
1126
10556cb2 1127 err = fuse_perm_getattr(inode, mask);
19690ddb
MS
1128 if (err)
1129 return err;
1130 }
244f6385
MS
1131 }
1132
1133 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
2830ba7f 1134 err = generic_permission(inode, mask);
1e9a4ed9
MS
1135
1136 /* If permission is denied, try to refresh file
1137 attributes. This is also needed, because the root
1138 node will at first have no permissions */
244f6385 1139 if (err == -EACCES && !refreshed) {
10556cb2 1140 err = fuse_perm_getattr(inode, mask);
1e9a4ed9 1141 if (!err)
2830ba7f 1142 err = generic_permission(inode, mask);
1e9a4ed9
MS
1143 }
1144
6f9f1180
MS
1145 /* Note: the opposite of the above test does not
1146 exist. So if permissions are revoked this won't be
1147 noticed immediately, only after the attribute
1148 timeout has expired */
9cfcac81 1149 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
10556cb2 1150 if (mask & MAY_NOT_BLOCK)
19690ddb
MS
1151 return -ECHILD;
1152
e8e96157
MS
1153 err = fuse_access(inode, mask);
1154 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1155 if (!(inode->i_mode & S_IXUGO)) {
1156 if (refreshed)
1157 return -EACCES;
1158
10556cb2 1159 err = fuse_perm_getattr(inode, mask);
e8e96157
MS
1160 if (!err && !(inode->i_mode & S_IXUGO))
1161 return -EACCES;
1162 }
e5e5558e 1163 }
244f6385 1164 return err;
e5e5558e
MS
1165}
1166
1167static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
8d3af7f3 1168 struct dir_context *ctx)
e5e5558e
MS
1169{
1170 while (nbytes >= FUSE_NAME_OFFSET) {
1171 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1172 size_t reclen = FUSE_DIRENT_SIZE(dirent);
e5e5558e
MS
1173 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1174 return -EIO;
1175 if (reclen > nbytes)
1176 break;
1177
8d3af7f3
AV
1178 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1179 dirent->ino, dirent->type))
e5e5558e
MS
1180 break;
1181
1182 buf += reclen;
1183 nbytes -= reclen;
8d3af7f3 1184 ctx->pos = dirent->off;
e5e5558e
MS
1185 }
1186
1187 return 0;
1188}
1189
0b05b183
AA
1190static int fuse_direntplus_link(struct file *file,
1191 struct fuse_direntplus *direntplus,
1192 u64 attr_version)
1193{
1194 int err;
1195 struct fuse_entry_out *o = &direntplus->entry_out;
1196 struct fuse_dirent *dirent = &direntplus->dirent;
1197 struct dentry *parent = file->f_path.dentry;
1198 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1199 struct dentry *dentry;
1200 struct dentry *alias;
1201 struct inode *dir = parent->d_inode;
1202 struct fuse_conn *fc;
1203 struct inode *inode;
1204
1205 if (!o->nodeid) {
1206 /*
1207 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1208 * ENOENT. Instead, it only means the userspace filesystem did
1209 * not want to return attributes/handle for this entry.
1210 *
1211 * So do nothing.
1212 */
1213 return 0;
1214 }
1215
1216 if (name.name[0] == '.') {
1217 /*
1218 * We could potentially refresh the attributes of the directory
1219 * and its parent?
1220 */
1221 if (name.len == 1)
1222 return 0;
1223 if (name.name[1] == '.' && name.len == 2)
1224 return 0;
1225 }
a28ef45c
MS
1226
1227 if (invalid_nodeid(o->nodeid))
1228 return -EIO;
1229 if (!fuse_valid_type(o->attr.mode))
1230 return -EIO;
1231
0b05b183
AA
1232 fc = get_fuse_conn(dir);
1233
1234 name.hash = full_name_hash(name.name, name.len);
1235 dentry = d_lookup(parent, &name);
53ce9a33 1236 if (dentry) {
0b05b183 1237 inode = dentry->d_inode;
53ce9a33
NV
1238 if (!inode) {
1239 d_drop(dentry);
a28ef45c
MS
1240 } else if (get_node_id(inode) != o->nodeid ||
1241 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
53ce9a33
NV
1242 err = d_invalidate(dentry);
1243 if (err)
1244 goto out;
a28ef45c
MS
1245 } else if (is_bad_inode(inode)) {
1246 err = -EIO;
1247 goto out;
53ce9a33 1248 } else {
0b05b183
AA
1249 struct fuse_inode *fi;
1250 fi = get_fuse_inode(inode);
1251 spin_lock(&fc->lock);
1252 fi->nlookup++;
1253 spin_unlock(&fc->lock);
1254
fa2b7213
MS
1255 fuse_change_attributes(inode, &o->attr,
1256 entry_attr_timeout(o),
1257 attr_version);
1258
0b05b183
AA
1259 /*
1260 * The other branch to 'found' comes via fuse_iget()
1261 * which bumps nlookup inside
1262 */
1263 goto found;
1264 }
0b05b183 1265 dput(dentry);
0b05b183
AA
1266 }
1267
1268 dentry = d_alloc(parent, &name);
1269 err = -ENOMEM;
1270 if (!dentry)
1271 goto out;
1272
1273 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1274 &o->attr, entry_attr_timeout(o), attr_version);
1275 if (!inode)
1276 goto out;
1277
2914941e
MS
1278 if (S_ISDIR(inode->i_mode)) {
1279 mutex_lock(&fc->inst_mutex);
1280 alias = fuse_d_add_directory(dentry, inode);
1281 mutex_unlock(&fc->inst_mutex);
1282 err = PTR_ERR(alias);
1283 if (IS_ERR(alias)) {
1284 iput(inode);
1285 goto out;
1286 }
1287 } else {
1288 alias = d_splice_alias(inode, dentry);
1289 }
1290
0b05b183
AA
1291 if (alias) {
1292 dput(dentry);
1293 dentry = alias;
1294 }
1295
1296found:
0b05b183
AA
1297 fuse_change_entry_timeout(dentry, o);
1298
1299 err = 0;
1300out:
c7263bcd 1301 dput(dentry);
0b05b183
AA
1302 return err;
1303}
1304
1305static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
8d3af7f3 1306 struct dir_context *ctx, u64 attr_version)
0b05b183
AA
1307{
1308 struct fuse_direntplus *direntplus;
1309 struct fuse_dirent *dirent;
1310 size_t reclen;
1311 int over = 0;
1312 int ret;
1313
1314 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1315 direntplus = (struct fuse_direntplus *) buf;
1316 dirent = &direntplus->dirent;
1317 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1318
1319 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1320 return -EIO;
1321 if (reclen > nbytes)
1322 break;
1323
1324 if (!over) {
1325 /* We fill entries into dstbuf only as much as
1326 it can hold. But we still continue iterating
1327 over remaining entries to link them. If not,
1328 we need to send a FORGET for each of those
1329 which we did not link.
1330 */
8d3af7f3
AV
1331 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1332 dirent->ino, dirent->type);
1333 ctx->pos = dirent->off;
0b05b183
AA
1334 }
1335
1336 buf += reclen;
1337 nbytes -= reclen;
1338
1339 ret = fuse_direntplus_link(file, direntplus, attr_version);
1340 if (ret)
1341 fuse_force_forget(file, direntplus->entry_out.nodeid);
1342 }
1343
1344 return 0;
1345}
1346
8d3af7f3 1347static int fuse_readdir(struct file *file, struct dir_context *ctx)
e5e5558e 1348{
4582a4ab 1349 int plus, err;
04730fef
MS
1350 size_t nbytes;
1351 struct page *page;
496ad9aa 1352 struct inode *inode = file_inode(file);
e5e5558e 1353 struct fuse_conn *fc = get_fuse_conn(inode);
248d86e8 1354 struct fuse_req *req;
0b05b183 1355 u64 attr_version = 0;
248d86e8
MS
1356
1357 if (is_bad_inode(inode))
1358 return -EIO;
1359
b111c8c0 1360 req = fuse_get_req(fc, 1);
ce1d5a49
MS
1361 if (IS_ERR(req))
1362 return PTR_ERR(req);
e5e5558e 1363
04730fef
MS
1364 page = alloc_page(GFP_KERNEL);
1365 if (!page) {
1366 fuse_put_request(fc, req);
1367 return -ENOMEM;
1368 }
4582a4ab 1369
8d3af7f3 1370 plus = fuse_use_readdirplus(inode, ctx);
f4975c67 1371 req->out.argpages = 1;
04730fef
MS
1372 req->num_pages = 1;
1373 req->pages[0] = page;
85f40aec 1374 req->page_descs[0].length = PAGE_SIZE;
4582a4ab 1375 if (plus) {
0b05b183 1376 attr_version = fuse_get_attr_version(fc);
8d3af7f3 1377 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
0b05b183
AA
1378 FUSE_READDIRPLUS);
1379 } else {
8d3af7f3 1380 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
0b05b183
AA
1381 FUSE_READDIR);
1382 }
b93f858a 1383 fuse_request_send(fc, req);
361b1eb5 1384 nbytes = req->out.args[0].size;
e5e5558e
MS
1385 err = req->out.h.error;
1386 fuse_put_request(fc, req);
0b05b183 1387 if (!err) {
4582a4ab 1388 if (plus) {
0b05b183 1389 err = parse_dirplusfile(page_address(page), nbytes,
8d3af7f3 1390 file, ctx,
0b05b183
AA
1391 attr_version);
1392 } else {
1393 err = parse_dirfile(page_address(page), nbytes, file,
8d3af7f3 1394 ctx);
0b05b183
AA
1395 }
1396 }
e5e5558e 1397
04730fef 1398 __free_page(page);
b36c31ba 1399 fuse_invalidate_attr(inode); /* atime changed */
04730fef 1400 return err;
e5e5558e
MS
1401}
1402
1403static char *read_link(struct dentry *dentry)
1404{
1405 struct inode *inode = dentry->d_inode;
1406 struct fuse_conn *fc = get_fuse_conn(inode);
b111c8c0 1407 struct fuse_req *req = fuse_get_req_nopages(fc);
e5e5558e
MS
1408 char *link;
1409
ce1d5a49 1410 if (IS_ERR(req))
e231c2ee 1411 return ERR_CAST(req);
e5e5558e
MS
1412
1413 link = (char *) __get_free_page(GFP_KERNEL);
1414 if (!link) {
1415 link = ERR_PTR(-ENOMEM);
1416 goto out;
1417 }
1418 req->in.h.opcode = FUSE_READLINK;
1419 req->in.h.nodeid = get_node_id(inode);
e5e5558e
MS
1420 req->out.argvar = 1;
1421 req->out.numargs = 1;
1422 req->out.args[0].size = PAGE_SIZE - 1;
1423 req->out.args[0].value = link;
b93f858a 1424 fuse_request_send(fc, req);
e5e5558e
MS
1425 if (req->out.h.error) {
1426 free_page((unsigned long) link);
1427 link = ERR_PTR(req->out.h.error);
1428 } else
1429 link[req->out.args[0].size] = '\0';
1430 out:
1431 fuse_put_request(fc, req);
b36c31ba 1432 fuse_invalidate_attr(inode); /* atime changed */
e5e5558e
MS
1433 return link;
1434}
1435
1436static void free_link(char *link)
1437{
1438 if (!IS_ERR(link))
1439 free_page((unsigned long) link);
1440}
1441
1442static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1443{
1444 nd_set_link(nd, read_link(dentry));
1445 return NULL;
1446}
1447
1448static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1449{
1450 free_link(nd_get_link(nd));
1451}
1452
1453static int fuse_dir_open(struct inode *inode, struct file *file)
1454{
91fe96b4 1455 return fuse_open_common(inode, file, true);
e5e5558e
MS
1456}
1457
1458static int fuse_dir_release(struct inode *inode, struct file *file)
1459{
8b0797a4
MS
1460 fuse_release_common(file, FUSE_RELEASEDIR);
1461
1462 return 0;
e5e5558e
MS
1463}
1464
02c24a82
JB
1465static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1466 int datasync)
82547981 1467{
02c24a82 1468 return fuse_fsync_common(file, start, end, datasync, 1);
82547981
MS
1469}
1470
b18da0c5
MS
1471static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1472 unsigned long arg)
1473{
1474 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1475
1476 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1477 if (fc->minor < 18)
1478 return -ENOTTY;
1479
1480 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1481}
1482
1483static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1484 unsigned long arg)
1485{
1486 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1487
1488 if (fc->minor < 18)
1489 return -ENOTTY;
1490
1491 return fuse_ioctl_common(file, cmd, arg,
1492 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1493}
1494
17637cba
MS
1495static bool update_mtime(unsigned ivalid)
1496{
1497 /* Always update if mtime is explicitly set */
1498 if (ivalid & ATTR_MTIME_SET)
1499 return true;
1500
1501 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1502 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1503 return false;
1504
1505 /* In all other cases update */
1506 return true;
1507}
1508
befc649c 1509static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
9e6268db
MS
1510{
1511 unsigned ivalid = iattr->ia_valid;
9e6268db
MS
1512
1513 if (ivalid & ATTR_MODE)
befc649c 1514 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
9e6268db 1515 if (ivalid & ATTR_UID)
499dcf20 1516 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
9e6268db 1517 if (ivalid & ATTR_GID)
499dcf20 1518 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
9e6268db 1519 if (ivalid & ATTR_SIZE)
befc649c 1520 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
17637cba
MS
1521 if (ivalid & ATTR_ATIME) {
1522 arg->valid |= FATTR_ATIME;
befc649c 1523 arg->atime = iattr->ia_atime.tv_sec;
17637cba
MS
1524 arg->atimensec = iattr->ia_atime.tv_nsec;
1525 if (!(ivalid & ATTR_ATIME_SET))
1526 arg->valid |= FATTR_ATIME_NOW;
1527 }
1528 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1529 arg->valid |= FATTR_MTIME;
befc649c 1530 arg->mtime = iattr->ia_mtime.tv_sec;
17637cba
MS
1531 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1532 if (!(ivalid & ATTR_MTIME_SET))
1533 arg->valid |= FATTR_MTIME_NOW;
befc649c 1534 }
9e6268db
MS
1535}
1536
3be5a52b
MS
1537/*
1538 * Prevent concurrent writepages on inode
1539 *
1540 * This is done by adding a negative bias to the inode write counter
1541 * and waiting for all pending writes to finish.
1542 */
1543void fuse_set_nowrite(struct inode *inode)
1544{
1545 struct fuse_conn *fc = get_fuse_conn(inode);
1546 struct fuse_inode *fi = get_fuse_inode(inode);
1547
1548 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1549
1550 spin_lock(&fc->lock);
1551 BUG_ON(fi->writectr < 0);
1552 fi->writectr += FUSE_NOWRITE;
1553 spin_unlock(&fc->lock);
1554 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1555}
1556
1557/*
1558 * Allow writepages on inode
1559 *
1560 * Remove the bias from the writecounter and send any queued
1561 * writepages.
1562 */
1563static void __fuse_release_nowrite(struct inode *inode)
1564{
1565 struct fuse_inode *fi = get_fuse_inode(inode);
1566
1567 BUG_ON(fi->writectr != FUSE_NOWRITE);
1568 fi->writectr = 0;
1569 fuse_flush_writepages(inode);
1570}
1571
1572void fuse_release_nowrite(struct inode *inode)
1573{
1574 struct fuse_conn *fc = get_fuse_conn(inode);
1575
1576 spin_lock(&fc->lock);
1577 __fuse_release_nowrite(inode);
1578 spin_unlock(&fc->lock);
1579}
1580
6f9f1180
MS
1581/*
1582 * Set attributes, and at the same time refresh them.
1583 *
1584 * Truncation is slightly complicated, because the 'truncate' request
1585 * may fail, in which case we don't want to touch the mapping.
9ffbb916
MS
1586 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1587 * and the actual truncation by hand.
6f9f1180 1588 */
efb9fa9e
MP
1589int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1590 struct file *file)
9e6268db 1591{
9e6268db 1592 struct fuse_conn *fc = get_fuse_conn(inode);
9e6268db
MS
1593 struct fuse_req *req;
1594 struct fuse_setattr_in inarg;
1595 struct fuse_attr_out outarg;
3be5a52b
MS
1596 bool is_truncate = false;
1597 loff_t oldsize;
9e6268db 1598 int err;
9e6268db 1599
db78b877
CH
1600 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1601 attr->ia_valid |= ATTR_FORCE;
1602
1603 err = inode_change_ok(inode, attr);
1604 if (err)
1605 return err;
1e9a4ed9 1606
8d56addd
MS
1607 if (attr->ia_valid & ATTR_OPEN) {
1608 if (fc->atomic_o_trunc)
1609 return 0;
1610 file = NULL;
1611 }
6ff958ed 1612
2c27c65e 1613 if (attr->ia_valid & ATTR_SIZE)
3be5a52b 1614 is_truncate = true;
9e6268db 1615
b111c8c0 1616 req = fuse_get_req_nopages(fc);
ce1d5a49
MS
1617 if (IS_ERR(req))
1618 return PTR_ERR(req);
9e6268db 1619
3be5a52b
MS
1620 if (is_truncate)
1621 fuse_set_nowrite(inode);
1622
9e6268db 1623 memset(&inarg, 0, sizeof(inarg));
0e9663ee 1624 memset(&outarg, 0, sizeof(outarg));
befc649c 1625 iattr_to_fattr(attr, &inarg);
49d4914f
MS
1626 if (file) {
1627 struct fuse_file *ff = file->private_data;
1628 inarg.valid |= FATTR_FH;
1629 inarg.fh = ff->fh;
1630 }
f3332114
MS
1631 if (attr->ia_valid & ATTR_SIZE) {
1632 /* For mandatory locking in truncate */
1633 inarg.valid |= FATTR_LOCKOWNER;
1634 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1635 }
9e6268db
MS
1636 req->in.h.opcode = FUSE_SETATTR;
1637 req->in.h.nodeid = get_node_id(inode);
9e6268db
MS
1638 req->in.numargs = 1;
1639 req->in.args[0].size = sizeof(inarg);
1640 req->in.args[0].value = &inarg;
1641 req->out.numargs = 1;
0e9663ee
MS
1642 if (fc->minor < 9)
1643 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1644 else
1645 req->out.args[0].size = sizeof(outarg);
9e6268db 1646 req->out.args[0].value = &outarg;
b93f858a 1647 fuse_request_send(fc, req);
9e6268db
MS
1648 err = req->out.h.error;
1649 fuse_put_request(fc, req);
e00d2c2d
MS
1650 if (err) {
1651 if (err == -EINTR)
1652 fuse_invalidate_attr(inode);
3be5a52b 1653 goto error;
e00d2c2d 1654 }
9e6268db 1655
e00d2c2d
MS
1656 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1657 make_bad_inode(inode);
3be5a52b
MS
1658 err = -EIO;
1659 goto error;
1660 }
1661
1662 spin_lock(&fc->lock);
1663 fuse_change_attributes_common(inode, &outarg.attr,
1664 attr_timeout(&outarg));
1665 oldsize = inode->i_size;
1666 i_size_write(inode, outarg.attr.size);
1667
1668 if (is_truncate) {
1669 /* NOTE: this may release/reacquire fc->lock */
1670 __fuse_release_nowrite(inode);
1671 }
1672 spin_unlock(&fc->lock);
1673
1674 /*
1675 * Only call invalidate_inode_pages2() after removing
1676 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1677 */
1678 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
c08d3b0e 1679 truncate_pagecache(inode, oldsize, outarg.attr.size);
3be5a52b 1680 invalidate_inode_pages2(inode->i_mapping);
e00d2c2d
MS
1681 }
1682
e00d2c2d 1683 return 0;
3be5a52b
MS
1684
1685error:
1686 if (is_truncate)
1687 fuse_release_nowrite(inode);
1688
1689 return err;
9e6268db
MS
1690}
1691
49d4914f
MS
1692static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1693{
efb9fa9e
MP
1694 struct inode *inode = entry->d_inode;
1695
1696 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1697 return -EACCES;
1698
49d4914f 1699 if (attr->ia_valid & ATTR_FILE)
efb9fa9e 1700 return fuse_do_setattr(inode, attr, attr->ia_file);
49d4914f 1701 else
efb9fa9e 1702 return fuse_do_setattr(inode, attr, NULL);
49d4914f
MS
1703}
1704
e5e5558e
MS
1705static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1706 struct kstat *stat)
1707{
1708 struct inode *inode = entry->d_inode;
244f6385 1709 struct fuse_conn *fc = get_fuse_conn(inode);
244f6385 1710
c2132c1b 1711 if (!fuse_allow_current_process(fc))
244f6385
MS
1712 return -EACCES;
1713
bcb4be80 1714 return fuse_update_attributes(inode, stat, NULL, NULL);
e5e5558e
MS
1715}
1716
92a8780e
MS
1717static int fuse_setxattr(struct dentry *entry, const char *name,
1718 const void *value, size_t size, int flags)
1719{
1720 struct inode *inode = entry->d_inode;
1721 struct fuse_conn *fc = get_fuse_conn(inode);
1722 struct fuse_req *req;
1723 struct fuse_setxattr_in inarg;
1724 int err;
1725
92a8780e
MS
1726 if (fc->no_setxattr)
1727 return -EOPNOTSUPP;
1728
b111c8c0 1729 req = fuse_get_req_nopages(fc);
ce1d5a49
MS
1730 if (IS_ERR(req))
1731 return PTR_ERR(req);
92a8780e
MS
1732
1733 memset(&inarg, 0, sizeof(inarg));
1734 inarg.size = size;
1735 inarg.flags = flags;
1736 req->in.h.opcode = FUSE_SETXATTR;
1737 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1738 req->in.numargs = 3;
1739 req->in.args[0].size = sizeof(inarg);
1740 req->in.args[0].value = &inarg;
1741 req->in.args[1].size = strlen(name) + 1;
1742 req->in.args[1].value = name;
1743 req->in.args[2].size = size;
1744 req->in.args[2].value = value;
b93f858a 1745 fuse_request_send(fc, req);
92a8780e
MS
1746 err = req->out.h.error;
1747 fuse_put_request(fc, req);
1748 if (err == -ENOSYS) {
1749 fc->no_setxattr = 1;
1750 err = -EOPNOTSUPP;
1751 }
1752 return err;
1753}
1754
1755static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1756 void *value, size_t size)
1757{
1758 struct inode *inode = entry->d_inode;
1759 struct fuse_conn *fc = get_fuse_conn(inode);
1760 struct fuse_req *req;
1761 struct fuse_getxattr_in inarg;
1762 struct fuse_getxattr_out outarg;
1763 ssize_t ret;
1764
1765 if (fc->no_getxattr)
1766 return -EOPNOTSUPP;
1767
b111c8c0 1768 req = fuse_get_req_nopages(fc);
ce1d5a49
MS
1769 if (IS_ERR(req))
1770 return PTR_ERR(req);
92a8780e
MS
1771
1772 memset(&inarg, 0, sizeof(inarg));
1773 inarg.size = size;
1774 req->in.h.opcode = FUSE_GETXATTR;
1775 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1776 req->in.numargs = 2;
1777 req->in.args[0].size = sizeof(inarg);
1778 req->in.args[0].value = &inarg;
1779 req->in.args[1].size = strlen(name) + 1;
1780 req->in.args[1].value = name;
1781 /* This is really two different operations rolled into one */
1782 req->out.numargs = 1;
1783 if (size) {
1784 req->out.argvar = 1;
1785 req->out.args[0].size = size;
1786 req->out.args[0].value = value;
1787 } else {
1788 req->out.args[0].size = sizeof(outarg);
1789 req->out.args[0].value = &outarg;
1790 }
b93f858a 1791 fuse_request_send(fc, req);
92a8780e
MS
1792 ret = req->out.h.error;
1793 if (!ret)
1794 ret = size ? req->out.args[0].size : outarg.size;
1795 else {
1796 if (ret == -ENOSYS) {
1797 fc->no_getxattr = 1;
1798 ret = -EOPNOTSUPP;
1799 }
1800 }
1801 fuse_put_request(fc, req);
1802 return ret;
1803}
1804
1805static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1806{
1807 struct inode *inode = entry->d_inode;
1808 struct fuse_conn *fc = get_fuse_conn(inode);
1809 struct fuse_req *req;
1810 struct fuse_getxattr_in inarg;
1811 struct fuse_getxattr_out outarg;
1812 ssize_t ret;
1813
c2132c1b 1814 if (!fuse_allow_current_process(fc))
e57ac683
MS
1815 return -EACCES;
1816
92a8780e
MS
1817 if (fc->no_listxattr)
1818 return -EOPNOTSUPP;
1819
b111c8c0 1820 req = fuse_get_req_nopages(fc);
ce1d5a49
MS
1821 if (IS_ERR(req))
1822 return PTR_ERR(req);
92a8780e
MS
1823
1824 memset(&inarg, 0, sizeof(inarg));
1825 inarg.size = size;
1826 req->in.h.opcode = FUSE_LISTXATTR;
1827 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1828 req->in.numargs = 1;
1829 req->in.args[0].size = sizeof(inarg);
1830 req->in.args[0].value = &inarg;
1831 /* This is really two different operations rolled into one */
1832 req->out.numargs = 1;
1833 if (size) {
1834 req->out.argvar = 1;
1835 req->out.args[0].size = size;
1836 req->out.args[0].value = list;
1837 } else {
1838 req->out.args[0].size = sizeof(outarg);
1839 req->out.args[0].value = &outarg;
1840 }
b93f858a 1841 fuse_request_send(fc, req);
92a8780e
MS
1842 ret = req->out.h.error;
1843 if (!ret)
1844 ret = size ? req->out.args[0].size : outarg.size;
1845 else {
1846 if (ret == -ENOSYS) {
1847 fc->no_listxattr = 1;
1848 ret = -EOPNOTSUPP;
1849 }
1850 }
1851 fuse_put_request(fc, req);
1852 return ret;
1853}
1854
1855static int fuse_removexattr(struct dentry *entry, const char *name)
1856{
1857 struct inode *inode = entry->d_inode;
1858 struct fuse_conn *fc = get_fuse_conn(inode);
1859 struct fuse_req *req;
1860 int err;
1861
1862 if (fc->no_removexattr)
1863 return -EOPNOTSUPP;
1864
b111c8c0 1865 req = fuse_get_req_nopages(fc);
ce1d5a49
MS
1866 if (IS_ERR(req))
1867 return PTR_ERR(req);
92a8780e
MS
1868
1869 req->in.h.opcode = FUSE_REMOVEXATTR;
1870 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1871 req->in.numargs = 1;
1872 req->in.args[0].size = strlen(name) + 1;
1873 req->in.args[0].value = name;
b93f858a 1874 fuse_request_send(fc, req);
92a8780e
MS
1875 err = req->out.h.error;
1876 fuse_put_request(fc, req);
1877 if (err == -ENOSYS) {
1878 fc->no_removexattr = 1;
1879 err = -EOPNOTSUPP;
1880 }
1881 return err;
1882}
1883
754661f1 1884static const struct inode_operations fuse_dir_inode_operations = {
e5e5558e 1885 .lookup = fuse_lookup,
9e6268db
MS
1886 .mkdir = fuse_mkdir,
1887 .symlink = fuse_symlink,
1888 .unlink = fuse_unlink,
1889 .rmdir = fuse_rmdir,
1890 .rename = fuse_rename,
1891 .link = fuse_link,
1892 .setattr = fuse_setattr,
1893 .create = fuse_create,
c8ccbe03 1894 .atomic_open = fuse_atomic_open,
9e6268db 1895 .mknod = fuse_mknod,
e5e5558e
MS
1896 .permission = fuse_permission,
1897 .getattr = fuse_getattr,
92a8780e
MS
1898 .setxattr = fuse_setxattr,
1899 .getxattr = fuse_getxattr,
1900 .listxattr = fuse_listxattr,
1901 .removexattr = fuse_removexattr,
e5e5558e
MS
1902};
1903
4b6f5d20 1904static const struct file_operations fuse_dir_operations = {
b6aeaded 1905 .llseek = generic_file_llseek,
e5e5558e 1906 .read = generic_read_dir,
8d3af7f3 1907 .iterate = fuse_readdir,
e5e5558e
MS
1908 .open = fuse_dir_open,
1909 .release = fuse_dir_release,
82547981 1910 .fsync = fuse_dir_fsync,
b18da0c5
MS
1911 .unlocked_ioctl = fuse_dir_ioctl,
1912 .compat_ioctl = fuse_dir_compat_ioctl,
e5e5558e
MS
1913};
1914
754661f1 1915static const struct inode_operations fuse_common_inode_operations = {
9e6268db 1916 .setattr = fuse_setattr,
e5e5558e
MS
1917 .permission = fuse_permission,
1918 .getattr = fuse_getattr,
92a8780e
MS
1919 .setxattr = fuse_setxattr,
1920 .getxattr = fuse_getxattr,
1921 .listxattr = fuse_listxattr,
1922 .removexattr = fuse_removexattr,
e5e5558e
MS
1923};
1924
754661f1 1925static const struct inode_operations fuse_symlink_inode_operations = {
9e6268db 1926 .setattr = fuse_setattr,
e5e5558e
MS
1927 .follow_link = fuse_follow_link,
1928 .put_link = fuse_put_link,
1929 .readlink = generic_readlink,
1930 .getattr = fuse_getattr,
92a8780e
MS
1931 .setxattr = fuse_setxattr,
1932 .getxattr = fuse_getxattr,
1933 .listxattr = fuse_listxattr,
1934 .removexattr = fuse_removexattr,
e5e5558e
MS
1935};
1936
1937void fuse_init_common(struct inode *inode)
1938{
1939 inode->i_op = &fuse_common_inode_operations;
1940}
1941
1942void fuse_init_dir(struct inode *inode)
1943{
1944 inode->i_op = &fuse_dir_inode_operations;
1945 inode->i_fop = &fuse_dir_operations;
1946}
1947
1948void fuse_init_symlink(struct inode *inode)
1949{
1950 inode->i_op = &fuse_symlink_inode_operations;
1951}
This page took 1.052253 seconds and 5 git commands to generate.