Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[linux-block.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>
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 */
dbd561d2 100void fuse_invalidate_entry_cache(struct dentry *entry)
8cbdf1e6 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
c180eebe
MS
115static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
116 u64 nodeid, struct qstr *name,
e5e5558e
MS
117 struct fuse_entry_out *outarg)
118{
0e9663ee 119 memset(outarg, 0, sizeof(struct fuse_entry_out));
e5e5558e 120 req->in.h.opcode = FUSE_LOOKUP;
c180eebe 121 req->in.h.nodeid = nodeid;
e5e5558e 122 req->in.numargs = 1;
c180eebe
MS
123 req->in.args[0].size = name->len + 1;
124 req->in.args[0].value = name->name;
e5e5558e 125 req->out.numargs = 1;
0e9663ee
MS
126 if (fc->minor < 9)
127 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
128 else
129 req->out.args[0].size = sizeof(struct fuse_entry_out);
e5e5558e
MS
130 req->out.args[0].value = outarg;
131}
132
5c5c5e51 133u64 fuse_get_attr_version(struct fuse_conn *fc)
7dca9fd3
MS
134{
135 u64 curr_version;
136
137 /*
138 * The spin lock isn't actually needed on 64bit archs, but we
139 * don't yet care too much about such optimizations.
140 */
141 spin_lock(&fc->lock);
142 curr_version = fc->attr_version;
143 spin_unlock(&fc->lock);
144
145 return curr_version;
146}
147
6f9f1180
MS
148/*
149 * Check whether the dentry is still valid
150 *
151 * If the entry validity timeout has expired and the dentry is
152 * positive, try to redo the lookup. If the lookup results in a
153 * different inode, then let the VFS invalidate the dentry and redo
154 * the lookup once more. If the lookup results in the same inode,
155 * then refresh the attributes, timeouts and mark the dentry valid.
156 */
e5e5558e
MS
157static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
158{
8cbdf1e6
MS
159 struct inode *inode = entry->d_inode;
160
161 if (inode && is_bad_inode(inode))
e5e5558e 162 return 0;
0a0898cf 163 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
e5e5558e 164 int err;
e5e5558e 165 struct fuse_entry_out outarg;
8cbdf1e6
MS
166 struct fuse_conn *fc;
167 struct fuse_req *req;
2d51013e 168 struct fuse_req *forget_req;
e956edd0 169 struct dentry *parent;
1fb69e78 170 u64 attr_version;
8cbdf1e6 171
50322fe7 172 /* For negative dentries, always do a fresh lookup */
8cbdf1e6
MS
173 if (!inode)
174 return 0;
175
176 fc = get_fuse_conn(inode);
ce1d5a49
MS
177 req = fuse_get_req(fc);
178 if (IS_ERR(req))
e5e5558e
MS
179 return 0;
180
2d51013e
MS
181 forget_req = fuse_get_req(fc);
182 if (IS_ERR(forget_req)) {
183 fuse_put_request(fc, req);
184 return 0;
185 }
186
7dca9fd3 187 attr_version = fuse_get_attr_version(fc);
1fb69e78 188
e956edd0 189 parent = dget_parent(entry);
c180eebe
MS
190 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
191 &entry->d_name, &outarg);
b93f858a 192 fuse_request_send(fc, req);
e956edd0 193 dput(parent);
e5e5558e 194 err = req->out.h.error;
2d51013e 195 fuse_put_request(fc, req);
50322fe7
MS
196 /* Zero nodeid is same as -ENOENT */
197 if (!err && !outarg.nodeid)
198 err = -ENOENT;
9e6268db 199 if (!err) {
8cbdf1e6 200 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 201 if (outarg.nodeid != get_node_id(inode)) {
2d51013e
MS
202 fuse_send_forget(fc, forget_req,
203 outarg.nodeid, 1);
9e6268db
MS
204 return 0;
205 }
8da5ff23 206 spin_lock(&fc->lock);
1729a16c 207 fi->nlookup++;
8da5ff23 208 spin_unlock(&fc->lock);
9e6268db 209 }
2d51013e 210 fuse_put_request(fc, forget_req);
9e6268db 211 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
e5e5558e
MS
212 return 0;
213
1fb69e78
MS
214 fuse_change_attributes(inode, &outarg.attr,
215 entry_attr_timeout(&outarg),
216 attr_version);
217 fuse_change_entry_timeout(entry, &outarg);
e5e5558e
MS
218 }
219 return 1;
220}
221
8bfc016d 222static int invalid_nodeid(u64 nodeid)
2827d0b2
MS
223{
224 return !nodeid || nodeid == FUSE_ROOT_ID;
225}
226
4269590a 227const struct dentry_operations fuse_dentry_operations = {
e5e5558e
MS
228 .d_revalidate = fuse_dentry_revalidate,
229};
230
a5bfffac 231int fuse_valid_type(int m)
39ee059a
MS
232{
233 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
234 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
235}
236
d2a85164
MS
237/*
238 * Add a directory inode to a dentry, ensuring that no other dentry
239 * refers to this inode. Called with fc->inst_mutex.
240 */
0de6256d
MS
241static struct dentry *fuse_d_add_directory(struct dentry *entry,
242 struct inode *inode)
d2a85164
MS
243{
244 struct dentry *alias = d_find_alias(inode);
0de6256d 245 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
d2a85164
MS
246 /* This tries to shrink the subtree below alias */
247 fuse_invalidate_entry(alias);
248 dput(alias);
249 if (!list_empty(&inode->i_dentry))
0de6256d
MS
250 return ERR_PTR(-EBUSY);
251 } else {
252 dput(alias);
d2a85164 253 }
0de6256d 254 return d_splice_alias(inode, entry);
d2a85164
MS
255}
256
c180eebe
MS
257int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
258 struct fuse_entry_out *outarg, struct inode **inode)
e5e5558e 259{
c180eebe 260 struct fuse_conn *fc = get_fuse_conn_super(sb);
e5e5558e 261 struct fuse_req *req;
2d51013e 262 struct fuse_req *forget_req;
1fb69e78 263 u64 attr_version;
c180eebe 264 int err;
e5e5558e 265
c180eebe
MS
266 *inode = NULL;
267 err = -ENAMETOOLONG;
268 if (name->len > FUSE_NAME_MAX)
269 goto out;
e5e5558e 270
ce1d5a49 271 req = fuse_get_req(fc);
c180eebe 272 err = PTR_ERR(req);
ce1d5a49 273 if (IS_ERR(req))
c180eebe 274 goto out;
e5e5558e 275
2d51013e 276 forget_req = fuse_get_req(fc);
c180eebe 277 err = PTR_ERR(forget_req);
2d51013e
MS
278 if (IS_ERR(forget_req)) {
279 fuse_put_request(fc, req);
c180eebe 280 goto out;
2d51013e
MS
281 }
282
7dca9fd3 283 attr_version = fuse_get_attr_version(fc);
1fb69e78 284
c180eebe 285 fuse_lookup_init(fc, req, nodeid, name, outarg);
b93f858a 286 fuse_request_send(fc, req);
e5e5558e 287 err = req->out.h.error;
2d51013e 288 fuse_put_request(fc, req);
50322fe7 289 /* Zero nodeid is same as -ENOENT, but with valid timeout */
c180eebe
MS
290 if (err || !outarg->nodeid)
291 goto out_put_forget;
292
293 err = -EIO;
294 if (!outarg->nodeid)
295 goto out_put_forget;
296 if (!fuse_valid_type(outarg->attr.mode))
297 goto out_put_forget;
298
299 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
300 &outarg->attr, entry_attr_timeout(outarg),
301 attr_version);
302 err = -ENOMEM;
303 if (!*inode) {
304 fuse_send_forget(fc, forget_req, outarg->nodeid, 1);
305 goto out;
e5e5558e 306 }
c180eebe
MS
307 err = 0;
308
309 out_put_forget:
2d51013e 310 fuse_put_request(fc, forget_req);
c180eebe
MS
311 out:
312 return err;
313}
314
315static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
316 struct nameidata *nd)
317{
318 int err;
319 struct fuse_entry_out outarg;
320 struct inode *inode;
321 struct dentry *newent;
322 struct fuse_conn *fc = get_fuse_conn(dir);
323 bool outarg_valid = true;
324
325 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
326 &outarg, &inode);
327 if (err == -ENOENT) {
328 outarg_valid = false;
329 err = 0;
330 }
331 if (err)
332 goto out_err;
333
334 err = -EIO;
335 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
336 goto out_iput;
e5e5558e 337
d2a85164
MS
338 if (inode && S_ISDIR(inode->i_mode)) {
339 mutex_lock(&fc->inst_mutex);
0de6256d 340 newent = fuse_d_add_directory(entry, inode);
d2a85164 341 mutex_unlock(&fc->inst_mutex);
c180eebe
MS
342 err = PTR_ERR(newent);
343 if (IS_ERR(newent))
344 goto out_iput;
345 } else {
0de6256d 346 newent = d_splice_alias(inode, entry);
c180eebe 347 }
d2a85164 348
0de6256d 349 entry = newent ? newent : entry;
e5e5558e 350 entry->d_op = &fuse_dentry_operations;
c180eebe 351 if (outarg_valid)
1fb69e78 352 fuse_change_entry_timeout(entry, &outarg);
8cbdf1e6
MS
353 else
354 fuse_invalidate_entry_cache(entry);
c180eebe 355
0de6256d 356 return newent;
c180eebe
MS
357
358 out_iput:
359 iput(inode);
360 out_err:
361 return ERR_PTR(err);
e5e5558e
MS
362}
363
6f9f1180
MS
364/*
365 * Atomic create+open operation
366 *
367 * If the filesystem doesn't support this, then fall back to separate
368 * 'mknod' + 'open' requests.
369 */
fd72faac
MS
370static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
371 struct nameidata *nd)
372{
373 int err;
374 struct inode *inode;
375 struct fuse_conn *fc = get_fuse_conn(dir);
376 struct fuse_req *req;
51eb01e7 377 struct fuse_req *forget_req;
e0a43ddc 378 struct fuse_create_in inarg;
fd72faac
MS
379 struct fuse_open_out outopen;
380 struct fuse_entry_out outentry;
fd72faac
MS
381 struct fuse_file *ff;
382 struct file *file;
383 int flags = nd->intent.open.flags - 1;
384
fd72faac 385 if (fc->no_create)
ce1d5a49 386 return -ENOSYS;
fd72faac 387
51eb01e7
MS
388 forget_req = fuse_get_req(fc);
389 if (IS_ERR(forget_req))
390 return PTR_ERR(forget_req);
391
ce1d5a49 392 req = fuse_get_req(fc);
51eb01e7 393 err = PTR_ERR(req);
ce1d5a49 394 if (IS_ERR(req))
51eb01e7 395 goto out_put_forget_req;
fd72faac 396
ce1d5a49 397 err = -ENOMEM;
acf99433 398 ff = fuse_file_alloc(fc);
fd72faac
MS
399 if (!ff)
400 goto out_put_request;
401
e0a43ddc
MS
402 if (!fc->dont_mask)
403 mode &= ~current_umask();
404
fd72faac
MS
405 flags &= ~O_NOCTTY;
406 memset(&inarg, 0, sizeof(inarg));
0e9663ee 407 memset(&outentry, 0, sizeof(outentry));
fd72faac
MS
408 inarg.flags = flags;
409 inarg.mode = mode;
e0a43ddc 410 inarg.umask = current_umask();
fd72faac
MS
411 req->in.h.opcode = FUSE_CREATE;
412 req->in.h.nodeid = get_node_id(dir);
fd72faac 413 req->in.numargs = 2;
e0a43ddc
MS
414 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
415 sizeof(inarg);
fd72faac
MS
416 req->in.args[0].value = &inarg;
417 req->in.args[1].size = entry->d_name.len + 1;
418 req->in.args[1].value = entry->d_name.name;
419 req->out.numargs = 2;
0e9663ee
MS
420 if (fc->minor < 9)
421 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
422 else
423 req->out.args[0].size = sizeof(outentry);
fd72faac
MS
424 req->out.args[0].value = &outentry;
425 req->out.args[1].size = sizeof(outopen);
426 req->out.args[1].value = &outopen;
b93f858a 427 fuse_request_send(fc, req);
fd72faac
MS
428 err = req->out.h.error;
429 if (err) {
430 if (err == -ENOSYS)
431 fc->no_create = 1;
432 goto out_free_ff;
433 }
434
435 err = -EIO;
2827d0b2 436 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
fd72faac
MS
437 goto out_free_ff;
438
51eb01e7 439 fuse_put_request(fc, req);
c7b7143c
MS
440 ff->fh = outopen.fh;
441 ff->nodeid = outentry.nodeid;
442 ff->open_flags = outopen.open_flags;
fd72faac 443 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
1fb69e78 444 &outentry.attr, entry_attr_timeout(&outentry), 0);
fd72faac
MS
445 if (!inode) {
446 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
8b0797a4 447 fuse_sync_release(ff, flags);
51eb01e7
MS
448 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
449 return -ENOMEM;
fd72faac 450 }
51eb01e7 451 fuse_put_request(fc, forget_req);
fd72faac 452 d_instantiate(entry, inode);
1fb69e78 453 fuse_change_entry_timeout(entry, &outentry);
0952b2a4 454 fuse_invalidate_attr(dir);
fd72faac
MS
455 file = lookup_instantiate_filp(nd, entry, generic_file_open);
456 if (IS_ERR(file)) {
8b0797a4 457 fuse_sync_release(ff, flags);
fd72faac
MS
458 return PTR_ERR(file);
459 }
c7b7143c
MS
460 file->private_data = fuse_file_get(ff);
461 fuse_finish_open(inode, file);
fd72faac
MS
462 return 0;
463
464 out_free_ff:
465 fuse_file_free(ff);
466 out_put_request:
467 fuse_put_request(fc, req);
51eb01e7
MS
468 out_put_forget_req:
469 fuse_put_request(fc, forget_req);
fd72faac
MS
470 return err;
471}
472
6f9f1180
MS
473/*
474 * Code shared between mknod, mkdir, symlink and link
475 */
9e6268db
MS
476static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
477 struct inode *dir, struct dentry *entry,
478 int mode)
479{
480 struct fuse_entry_out outarg;
481 struct inode *inode;
9e6268db 482 int err;
2d51013e
MS
483 struct fuse_req *forget_req;
484
485 forget_req = fuse_get_req(fc);
486 if (IS_ERR(forget_req)) {
487 fuse_put_request(fc, req);
488 return PTR_ERR(forget_req);
489 }
9e6268db 490
0e9663ee 491 memset(&outarg, 0, sizeof(outarg));
9e6268db 492 req->in.h.nodeid = get_node_id(dir);
9e6268db 493 req->out.numargs = 1;
0e9663ee
MS
494 if (fc->minor < 9)
495 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
496 else
497 req->out.args[0].size = sizeof(outarg);
9e6268db 498 req->out.args[0].value = &outarg;
b93f858a 499 fuse_request_send(fc, req);
9e6268db 500 err = req->out.h.error;
2d51013e
MS
501 fuse_put_request(fc, req);
502 if (err)
503 goto out_put_forget_req;
504
39ee059a
MS
505 err = -EIO;
506 if (invalid_nodeid(outarg.nodeid))
2d51013e 507 goto out_put_forget_req;
39ee059a
MS
508
509 if ((outarg.attr.mode ^ mode) & S_IFMT)
2d51013e 510 goto out_put_forget_req;
39ee059a 511
9e6268db 512 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
1fb69e78 513 &outarg.attr, entry_attr_timeout(&outarg), 0);
9e6268db 514 if (!inode) {
2d51013e 515 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
9e6268db
MS
516 return -ENOMEM;
517 }
2d51013e 518 fuse_put_request(fc, forget_req);
9e6268db 519
d2a85164
MS
520 if (S_ISDIR(inode->i_mode)) {
521 struct dentry *alias;
522 mutex_lock(&fc->inst_mutex);
523 alias = d_find_alias(inode);
524 if (alias) {
525 /* New directory must have moved since mkdir */
526 mutex_unlock(&fc->inst_mutex);
527 dput(alias);
528 iput(inode);
529 return -EBUSY;
530 }
531 d_instantiate(entry, inode);
532 mutex_unlock(&fc->inst_mutex);
533 } else
534 d_instantiate(entry, inode);
9e6268db 535
1fb69e78 536 fuse_change_entry_timeout(entry, &outarg);
9e6268db
MS
537 fuse_invalidate_attr(dir);
538 return 0;
39ee059a 539
2d51013e
MS
540 out_put_forget_req:
541 fuse_put_request(fc, forget_req);
39ee059a 542 return err;
9e6268db
MS
543}
544
545static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
546 dev_t rdev)
547{
548 struct fuse_mknod_in inarg;
549 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
550 struct fuse_req *req = fuse_get_req(fc);
551 if (IS_ERR(req))
552 return PTR_ERR(req);
9e6268db 553
e0a43ddc
MS
554 if (!fc->dont_mask)
555 mode &= ~current_umask();
556
9e6268db
MS
557 memset(&inarg, 0, sizeof(inarg));
558 inarg.mode = mode;
559 inarg.rdev = new_encode_dev(rdev);
e0a43ddc 560 inarg.umask = current_umask();
9e6268db
MS
561 req->in.h.opcode = FUSE_MKNOD;
562 req->in.numargs = 2;
e0a43ddc
MS
563 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
564 sizeof(inarg);
9e6268db
MS
565 req->in.args[0].value = &inarg;
566 req->in.args[1].size = entry->d_name.len + 1;
567 req->in.args[1].value = entry->d_name.name;
568 return create_new_entry(fc, req, dir, entry, mode);
569}
570
571static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
572 struct nameidata *nd)
573{
b9ba347f 574 if (nd && (nd->flags & LOOKUP_OPEN)) {
fd72faac
MS
575 int err = fuse_create_open(dir, entry, mode, nd);
576 if (err != -ENOSYS)
577 return err;
578 /* Fall back on mknod */
579 }
9e6268db
MS
580 return fuse_mknod(dir, entry, mode, 0);
581}
582
583static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
584{
585 struct fuse_mkdir_in inarg;
586 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
587 struct fuse_req *req = fuse_get_req(fc);
588 if (IS_ERR(req))
589 return PTR_ERR(req);
9e6268db 590
e0a43ddc
MS
591 if (!fc->dont_mask)
592 mode &= ~current_umask();
593
9e6268db
MS
594 memset(&inarg, 0, sizeof(inarg));
595 inarg.mode = mode;
e0a43ddc 596 inarg.umask = current_umask();
9e6268db
MS
597 req->in.h.opcode = FUSE_MKDIR;
598 req->in.numargs = 2;
599 req->in.args[0].size = sizeof(inarg);
600 req->in.args[0].value = &inarg;
601 req->in.args[1].size = entry->d_name.len + 1;
602 req->in.args[1].value = entry->d_name.name;
603 return create_new_entry(fc, req, dir, entry, S_IFDIR);
604}
605
606static int fuse_symlink(struct inode *dir, struct dentry *entry,
607 const char *link)
608{
609 struct fuse_conn *fc = get_fuse_conn(dir);
610 unsigned len = strlen(link) + 1;
ce1d5a49
MS
611 struct fuse_req *req = fuse_get_req(fc);
612 if (IS_ERR(req))
613 return PTR_ERR(req);
9e6268db
MS
614
615 req->in.h.opcode = FUSE_SYMLINK;
616 req->in.numargs = 2;
617 req->in.args[0].size = entry->d_name.len + 1;
618 req->in.args[0].value = entry->d_name.name;
619 req->in.args[1].size = len;
620 req->in.args[1].value = link;
621 return create_new_entry(fc, req, dir, entry, S_IFLNK);
622}
623
624static int fuse_unlink(struct inode *dir, struct dentry *entry)
625{
626 int err;
627 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
628 struct fuse_req *req = fuse_get_req(fc);
629 if (IS_ERR(req))
630 return PTR_ERR(req);
9e6268db
MS
631
632 req->in.h.opcode = FUSE_UNLINK;
633 req->in.h.nodeid = get_node_id(dir);
9e6268db
MS
634 req->in.numargs = 1;
635 req->in.args[0].size = entry->d_name.len + 1;
636 req->in.args[0].value = entry->d_name.name;
b93f858a 637 fuse_request_send(fc, req);
9e6268db
MS
638 err = req->out.h.error;
639 fuse_put_request(fc, req);
640 if (!err) {
641 struct inode *inode = entry->d_inode;
642
1729a16c
MS
643 /*
644 * Set nlink to zero so the inode can be cleared, if the inode
645 * does have more links this will be discovered at the next
646 * lookup/getattr.
647 */
ce71ec36 648 clear_nlink(inode);
9e6268db
MS
649 fuse_invalidate_attr(inode);
650 fuse_invalidate_attr(dir);
8cbdf1e6 651 fuse_invalidate_entry_cache(entry);
9e6268db
MS
652 } else if (err == -EINTR)
653 fuse_invalidate_entry(entry);
654 return err;
655}
656
657static int fuse_rmdir(struct inode *dir, struct dentry *entry)
658{
659 int err;
660 struct fuse_conn *fc = get_fuse_conn(dir);
ce1d5a49
MS
661 struct fuse_req *req = fuse_get_req(fc);
662 if (IS_ERR(req))
663 return PTR_ERR(req);
9e6268db
MS
664
665 req->in.h.opcode = FUSE_RMDIR;
666 req->in.h.nodeid = get_node_id(dir);
9e6268db
MS
667 req->in.numargs = 1;
668 req->in.args[0].size = entry->d_name.len + 1;
669 req->in.args[0].value = entry->d_name.name;
b93f858a 670 fuse_request_send(fc, req);
9e6268db
MS
671 err = req->out.h.error;
672 fuse_put_request(fc, req);
673 if (!err) {
ce71ec36 674 clear_nlink(entry->d_inode);
9e6268db 675 fuse_invalidate_attr(dir);
8cbdf1e6 676 fuse_invalidate_entry_cache(entry);
9e6268db
MS
677 } else if (err == -EINTR)
678 fuse_invalidate_entry(entry);
679 return err;
680}
681
682static int fuse_rename(struct inode *olddir, struct dentry *oldent,
683 struct inode *newdir, struct dentry *newent)
684{
685 int err;
686 struct fuse_rename_in inarg;
687 struct fuse_conn *fc = get_fuse_conn(olddir);
ce1d5a49
MS
688 struct fuse_req *req = fuse_get_req(fc);
689 if (IS_ERR(req))
690 return PTR_ERR(req);
9e6268db
MS
691
692 memset(&inarg, 0, sizeof(inarg));
693 inarg.newdir = get_node_id(newdir);
694 req->in.h.opcode = FUSE_RENAME;
695 req->in.h.nodeid = get_node_id(olddir);
9e6268db
MS
696 req->in.numargs = 3;
697 req->in.args[0].size = sizeof(inarg);
698 req->in.args[0].value = &inarg;
699 req->in.args[1].size = oldent->d_name.len + 1;
700 req->in.args[1].value = oldent->d_name.name;
701 req->in.args[2].size = newent->d_name.len + 1;
702 req->in.args[2].value = newent->d_name.name;
b93f858a 703 fuse_request_send(fc, req);
9e6268db
MS
704 err = req->out.h.error;
705 fuse_put_request(fc, req);
706 if (!err) {
08b63307
MS
707 /* ctime changes */
708 fuse_invalidate_attr(oldent->d_inode);
709
9e6268db
MS
710 fuse_invalidate_attr(olddir);
711 if (olddir != newdir)
712 fuse_invalidate_attr(newdir);
8cbdf1e6
MS
713
714 /* newent will end up negative */
715 if (newent->d_inode)
716 fuse_invalidate_entry_cache(newent);
9e6268db
MS
717 } else if (err == -EINTR) {
718 /* If request was interrupted, DEITY only knows if the
719 rename actually took place. If the invalidation
720 fails (e.g. some process has CWD under the renamed
721 directory), then there can be inconsistency between
722 the dcache and the real filesystem. Tough luck. */
723 fuse_invalidate_entry(oldent);
724 if (newent->d_inode)
725 fuse_invalidate_entry(newent);
726 }
727
728 return err;
729}
730
731static int fuse_link(struct dentry *entry, struct inode *newdir,
732 struct dentry *newent)
733{
734 int err;
735 struct fuse_link_in inarg;
736 struct inode *inode = entry->d_inode;
737 struct fuse_conn *fc = get_fuse_conn(inode);
ce1d5a49
MS
738 struct fuse_req *req = fuse_get_req(fc);
739 if (IS_ERR(req))
740 return PTR_ERR(req);
9e6268db
MS
741
742 memset(&inarg, 0, sizeof(inarg));
743 inarg.oldnodeid = get_node_id(inode);
744 req->in.h.opcode = FUSE_LINK;
9e6268db
MS
745 req->in.numargs = 2;
746 req->in.args[0].size = sizeof(inarg);
747 req->in.args[0].value = &inarg;
748 req->in.args[1].size = newent->d_name.len + 1;
749 req->in.args[1].value = newent->d_name.name;
750 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
751 /* Contrary to "normal" filesystems it can happen that link
752 makes two "logical" inodes point to the same "physical"
753 inode. We invalidate the attributes of the old one, so it
754 will reflect changes in the backing inode (link count,
755 etc.)
756 */
757 if (!err || err == -EINTR)
758 fuse_invalidate_attr(inode);
759 return err;
760}
761
1fb69e78
MS
762static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
763 struct kstat *stat)
764{
765 stat->dev = inode->i_sb->s_dev;
766 stat->ino = attr->ino;
767 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
768 stat->nlink = attr->nlink;
769 stat->uid = attr->uid;
770 stat->gid = attr->gid;
771 stat->rdev = inode->i_rdev;
772 stat->atime.tv_sec = attr->atime;
773 stat->atime.tv_nsec = attr->atimensec;
774 stat->mtime.tv_sec = attr->mtime;
775 stat->mtime.tv_nsec = attr->mtimensec;
776 stat->ctime.tv_sec = attr->ctime;
777 stat->ctime.tv_nsec = attr->ctimensec;
778 stat->size = attr->size;
779 stat->blocks = attr->blocks;
780 stat->blksize = (1 << inode->i_blkbits);
781}
782
c79e322f
MS
783static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
784 struct file *file)
e5e5558e
MS
785{
786 int err;
c79e322f
MS
787 struct fuse_getattr_in inarg;
788 struct fuse_attr_out outarg;
e5e5558e 789 struct fuse_conn *fc = get_fuse_conn(inode);
1fb69e78
MS
790 struct fuse_req *req;
791 u64 attr_version;
792
793 req = fuse_get_req(fc);
ce1d5a49
MS
794 if (IS_ERR(req))
795 return PTR_ERR(req);
e5e5558e 796
7dca9fd3 797 attr_version = fuse_get_attr_version(fc);
1fb69e78 798
c79e322f 799 memset(&inarg, 0, sizeof(inarg));
0e9663ee 800 memset(&outarg, 0, sizeof(outarg));
c79e322f
MS
801 /* Directories have separate file-handle space */
802 if (file && S_ISREG(inode->i_mode)) {
803 struct fuse_file *ff = file->private_data;
804
805 inarg.getattr_flags |= FUSE_GETATTR_FH;
806 inarg.fh = ff->fh;
807 }
e5e5558e
MS
808 req->in.h.opcode = FUSE_GETATTR;
809 req->in.h.nodeid = get_node_id(inode);
c79e322f
MS
810 req->in.numargs = 1;
811 req->in.args[0].size = sizeof(inarg);
812 req->in.args[0].value = &inarg;
e5e5558e 813 req->out.numargs = 1;
0e9663ee
MS
814 if (fc->minor < 9)
815 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
816 else
817 req->out.args[0].size = sizeof(outarg);
c79e322f 818 req->out.args[0].value = &outarg;
b93f858a 819 fuse_request_send(fc, req);
e5e5558e
MS
820 err = req->out.h.error;
821 fuse_put_request(fc, req);
822 if (!err) {
c79e322f 823 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
e5e5558e
MS
824 make_bad_inode(inode);
825 err = -EIO;
826 } else {
c79e322f
MS
827 fuse_change_attributes(inode, &outarg.attr,
828 attr_timeout(&outarg),
1fb69e78
MS
829 attr_version);
830 if (stat)
c79e322f 831 fuse_fillattr(inode, &outarg.attr, stat);
e5e5558e
MS
832 }
833 }
834 return err;
835}
836
bcb4be80
MS
837int fuse_update_attributes(struct inode *inode, struct kstat *stat,
838 struct file *file, bool *refreshed)
839{
840 struct fuse_inode *fi = get_fuse_inode(inode);
841 int err;
842 bool r;
843
844 if (fi->i_time < get_jiffies_64()) {
845 r = true;
846 err = fuse_do_getattr(inode, stat, file);
847 } else {
848 r = false;
849 err = 0;
850 if (stat) {
851 generic_fillattr(inode, stat);
852 stat->mode = fi->orig_i_mode;
853 }
854 }
855
856 if (refreshed != NULL)
857 *refreshed = r;
858
859 return err;
860}
861
3b463ae0
JM
862int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
863 struct qstr *name)
864{
865 int err = -ENOTDIR;
866 struct inode *parent;
867 struct dentry *dir;
868 struct dentry *entry;
869
870 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
871 if (!parent)
872 return -ENOENT;
873
874 mutex_lock(&parent->i_mutex);
875 if (!S_ISDIR(parent->i_mode))
876 goto unlock;
877
878 err = -ENOENT;
879 dir = d_find_alias(parent);
880 if (!dir)
881 goto unlock;
882
883 entry = d_lookup(dir, name);
884 dput(dir);
885 if (!entry)
886 goto unlock;
887
888 fuse_invalidate_attr(parent);
889 fuse_invalidate_entry(entry);
890 dput(entry);
891 err = 0;
892
893 unlock:
894 mutex_unlock(&parent->i_mutex);
895 iput(parent);
896 return err;
897}
898
87729a55
MS
899/*
900 * Calling into a user-controlled filesystem gives the filesystem
901 * daemon ptrace-like capabilities over the requester process. This
902 * means, that the filesystem daemon is able to record the exact
903 * filesystem operations performed, and can also control the behavior
904 * of the requester process in otherwise impossible ways. For example
905 * it can delay the operation for arbitrary length of time allowing
906 * DoS against the requester.
907 *
908 * For this reason only those processes can call into the filesystem,
909 * for which the owner of the mount has ptrace privilege. This
910 * excludes processes started by other users, suid or sgid processes.
911 */
e57ac683 912int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
87729a55 913{
c69e8d9c
DH
914 const struct cred *cred;
915 int ret;
87729a55 916
c69e8d9c 917 if (fc->flags & FUSE_ALLOW_OTHER)
87729a55
MS
918 return 1;
919
c69e8d9c
DH
920 rcu_read_lock();
921 ret = 0;
922 cred = __task_cred(task);
923 if (cred->euid == fc->user_id &&
924 cred->suid == fc->user_id &&
925 cred->uid == fc->user_id &&
926 cred->egid == fc->group_id &&
927 cred->sgid == fc->group_id &&
928 cred->gid == fc->group_id)
929 ret = 1;
930 rcu_read_unlock();
931
932 return ret;
87729a55
MS
933}
934
31d40d74
MS
935static int fuse_access(struct inode *inode, int mask)
936{
937 struct fuse_conn *fc = get_fuse_conn(inode);
938 struct fuse_req *req;
939 struct fuse_access_in inarg;
940 int err;
941
942 if (fc->no_access)
943 return 0;
944
ce1d5a49
MS
945 req = fuse_get_req(fc);
946 if (IS_ERR(req))
947 return PTR_ERR(req);
31d40d74
MS
948
949 memset(&inarg, 0, sizeof(inarg));
e6305c43 950 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
31d40d74
MS
951 req->in.h.opcode = FUSE_ACCESS;
952 req->in.h.nodeid = get_node_id(inode);
31d40d74
MS
953 req->in.numargs = 1;
954 req->in.args[0].size = sizeof(inarg);
955 req->in.args[0].value = &inarg;
b93f858a 956 fuse_request_send(fc, req);
31d40d74
MS
957 err = req->out.h.error;
958 fuse_put_request(fc, req);
959 if (err == -ENOSYS) {
960 fc->no_access = 1;
961 err = 0;
962 }
963 return err;
964}
965
6f9f1180
MS
966/*
967 * Check permission. The two basic access models of FUSE are:
968 *
969 * 1) Local access checking ('default_permissions' mount option) based
970 * on file mode. This is the plain old disk filesystem permission
971 * modell.
972 *
973 * 2) "Remote" access checking, where server is responsible for
974 * checking permission in each inode operation. An exception to this
975 * is if ->permission() was invoked from sys_access() in which case an
976 * access request is sent. Execute permission is still checked
977 * locally based on file mode.
978 */
e6305c43 979static int fuse_permission(struct inode *inode, int mask)
e5e5558e
MS
980{
981 struct fuse_conn *fc = get_fuse_conn(inode);
244f6385
MS
982 bool refreshed = false;
983 int err = 0;
e5e5558e 984
87729a55 985 if (!fuse_allow_task(fc, current))
e5e5558e 986 return -EACCES;
244f6385
MS
987
988 /*
e8e96157 989 * If attributes are needed, refresh them before proceeding
244f6385 990 */
e8e96157
MS
991 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
992 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
bcb4be80
MS
993 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
994 if (err)
995 return err;
244f6385
MS
996 }
997
998 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1a823ac9 999 err = generic_permission(inode, mask, NULL);
1e9a4ed9
MS
1000
1001 /* If permission is denied, try to refresh file
1002 attributes. This is also needed, because the root
1003 node will at first have no permissions */
244f6385 1004 if (err == -EACCES && !refreshed) {
c79e322f 1005 err = fuse_do_getattr(inode, NULL, NULL);
1e9a4ed9
MS
1006 if (!err)
1007 err = generic_permission(inode, mask, NULL);
1008 }
1009
6f9f1180
MS
1010 /* Note: the opposite of the above test does not
1011 exist. So if permissions are revoked this won't be
1012 noticed immediately, only after the attribute
1013 timeout has expired */
a110343f 1014 } else if (mask & MAY_ACCESS) {
e8e96157
MS
1015 err = fuse_access(inode, mask);
1016 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1017 if (!(inode->i_mode & S_IXUGO)) {
1018 if (refreshed)
1019 return -EACCES;
1020
c79e322f 1021 err = fuse_do_getattr(inode, NULL, NULL);
e8e96157
MS
1022 if (!err && !(inode->i_mode & S_IXUGO))
1023 return -EACCES;
1024 }
e5e5558e 1025 }
244f6385 1026 return err;
e5e5558e
MS
1027}
1028
1029static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1030 void *dstbuf, filldir_t filldir)
1031{
1032 while (nbytes >= FUSE_NAME_OFFSET) {
1033 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1034 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1035 int over;
1036 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1037 return -EIO;
1038 if (reclen > nbytes)
1039 break;
1040
1041 over = filldir(dstbuf, dirent->name, dirent->namelen,
1042 file->f_pos, dirent->ino, dirent->type);
1043 if (over)
1044 break;
1045
1046 buf += reclen;
1047 nbytes -= reclen;
1048 file->f_pos = dirent->off;
1049 }
1050
1051 return 0;
1052}
1053
04730fef 1054static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
e5e5558e 1055{
04730fef
MS
1056 int err;
1057 size_t nbytes;
1058 struct page *page;
7706a9d6 1059 struct inode *inode = file->f_path.dentry->d_inode;
e5e5558e 1060 struct fuse_conn *fc = get_fuse_conn(inode);
248d86e8
MS
1061 struct fuse_req *req;
1062
1063 if (is_bad_inode(inode))
1064 return -EIO;
1065
ce1d5a49
MS
1066 req = fuse_get_req(fc);
1067 if (IS_ERR(req))
1068 return PTR_ERR(req);
e5e5558e 1069
04730fef
MS
1070 page = alloc_page(GFP_KERNEL);
1071 if (!page) {
1072 fuse_put_request(fc, req);
1073 return -ENOMEM;
1074 }
f4975c67 1075 req->out.argpages = 1;
04730fef
MS
1076 req->num_pages = 1;
1077 req->pages[0] = page;
2106cb18 1078 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
b93f858a 1079 fuse_request_send(fc, req);
361b1eb5 1080 nbytes = req->out.args[0].size;
e5e5558e
MS
1081 err = req->out.h.error;
1082 fuse_put_request(fc, req);
1083 if (!err)
04730fef
MS
1084 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1085 filldir);
e5e5558e 1086
04730fef 1087 __free_page(page);
b36c31ba 1088 fuse_invalidate_attr(inode); /* atime changed */
04730fef 1089 return err;
e5e5558e
MS
1090}
1091
1092static char *read_link(struct dentry *dentry)
1093{
1094 struct inode *inode = dentry->d_inode;
1095 struct fuse_conn *fc = get_fuse_conn(inode);
ce1d5a49 1096 struct fuse_req *req = fuse_get_req(fc);
e5e5558e
MS
1097 char *link;
1098
ce1d5a49 1099 if (IS_ERR(req))
e231c2ee 1100 return ERR_CAST(req);
e5e5558e
MS
1101
1102 link = (char *) __get_free_page(GFP_KERNEL);
1103 if (!link) {
1104 link = ERR_PTR(-ENOMEM);
1105 goto out;
1106 }
1107 req->in.h.opcode = FUSE_READLINK;
1108 req->in.h.nodeid = get_node_id(inode);
e5e5558e
MS
1109 req->out.argvar = 1;
1110 req->out.numargs = 1;
1111 req->out.args[0].size = PAGE_SIZE - 1;
1112 req->out.args[0].value = link;
b93f858a 1113 fuse_request_send(fc, req);
e5e5558e
MS
1114 if (req->out.h.error) {
1115 free_page((unsigned long) link);
1116 link = ERR_PTR(req->out.h.error);
1117 } else
1118 link[req->out.args[0].size] = '\0';
1119 out:
1120 fuse_put_request(fc, req);
b36c31ba 1121 fuse_invalidate_attr(inode); /* atime changed */
e5e5558e
MS
1122 return link;
1123}
1124
1125static void free_link(char *link)
1126{
1127 if (!IS_ERR(link))
1128 free_page((unsigned long) link);
1129}
1130
1131static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1132{
1133 nd_set_link(nd, read_link(dentry));
1134 return NULL;
1135}
1136
1137static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1138{
1139 free_link(nd_get_link(nd));
1140}
1141
1142static int fuse_dir_open(struct inode *inode, struct file *file)
1143{
91fe96b4 1144 return fuse_open_common(inode, file, true);
e5e5558e
MS
1145}
1146
1147static int fuse_dir_release(struct inode *inode, struct file *file)
1148{
8b0797a4
MS
1149 fuse_release_common(file, FUSE_RELEASEDIR);
1150
1151 return 0;
e5e5558e
MS
1152}
1153
82547981
MS
1154static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1155{
1156 /* nfsd can call this with no file */
1157 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1158}
1159
17637cba
MS
1160static bool update_mtime(unsigned ivalid)
1161{
1162 /* Always update if mtime is explicitly set */
1163 if (ivalid & ATTR_MTIME_SET)
1164 return true;
1165
1166 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1167 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1168 return false;
1169
1170 /* In all other cases update */
1171 return true;
1172}
1173
befc649c 1174static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
9e6268db
MS
1175{
1176 unsigned ivalid = iattr->ia_valid;
9e6268db
MS
1177
1178 if (ivalid & ATTR_MODE)
befc649c 1179 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
9e6268db 1180 if (ivalid & ATTR_UID)
befc649c 1181 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
9e6268db 1182 if (ivalid & ATTR_GID)
befc649c 1183 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
9e6268db 1184 if (ivalid & ATTR_SIZE)
befc649c 1185 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
17637cba
MS
1186 if (ivalid & ATTR_ATIME) {
1187 arg->valid |= FATTR_ATIME;
befc649c 1188 arg->atime = iattr->ia_atime.tv_sec;
17637cba
MS
1189 arg->atimensec = iattr->ia_atime.tv_nsec;
1190 if (!(ivalid & ATTR_ATIME_SET))
1191 arg->valid |= FATTR_ATIME_NOW;
1192 }
1193 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1194 arg->valid |= FATTR_MTIME;
befc649c 1195 arg->mtime = iattr->ia_mtime.tv_sec;
17637cba
MS
1196 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1197 if (!(ivalid & ATTR_MTIME_SET))
1198 arg->valid |= FATTR_MTIME_NOW;
befc649c 1199 }
9e6268db
MS
1200}
1201
3be5a52b
MS
1202/*
1203 * Prevent concurrent writepages on inode
1204 *
1205 * This is done by adding a negative bias to the inode write counter
1206 * and waiting for all pending writes to finish.
1207 */
1208void fuse_set_nowrite(struct inode *inode)
1209{
1210 struct fuse_conn *fc = get_fuse_conn(inode);
1211 struct fuse_inode *fi = get_fuse_inode(inode);
1212
1213 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1214
1215 spin_lock(&fc->lock);
1216 BUG_ON(fi->writectr < 0);
1217 fi->writectr += FUSE_NOWRITE;
1218 spin_unlock(&fc->lock);
1219 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1220}
1221
1222/*
1223 * Allow writepages on inode
1224 *
1225 * Remove the bias from the writecounter and send any queued
1226 * writepages.
1227 */
1228static void __fuse_release_nowrite(struct inode *inode)
1229{
1230 struct fuse_inode *fi = get_fuse_inode(inode);
1231
1232 BUG_ON(fi->writectr != FUSE_NOWRITE);
1233 fi->writectr = 0;
1234 fuse_flush_writepages(inode);
1235}
1236
1237void fuse_release_nowrite(struct inode *inode)
1238{
1239 struct fuse_conn *fc = get_fuse_conn(inode);
1240
1241 spin_lock(&fc->lock);
1242 __fuse_release_nowrite(inode);
1243 spin_unlock(&fc->lock);
1244}
1245
6f9f1180
MS
1246/*
1247 * Set attributes, and at the same time refresh them.
1248 *
1249 * Truncation is slightly complicated, because the 'truncate' request
1250 * may fail, in which case we don't want to touch the mapping.
9ffbb916
MS
1251 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1252 * and the actual truncation by hand.
6f9f1180 1253 */
49d4914f
MS
1254static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1255 struct file *file)
9e6268db
MS
1256{
1257 struct inode *inode = entry->d_inode;
1258 struct fuse_conn *fc = get_fuse_conn(inode);
9e6268db
MS
1259 struct fuse_req *req;
1260 struct fuse_setattr_in inarg;
1261 struct fuse_attr_out outarg;
3be5a52b
MS
1262 bool is_truncate = false;
1263 loff_t oldsize;
9e6268db 1264 int err;
9e6268db 1265
e57ac683
MS
1266 if (!fuse_allow_task(fc, current))
1267 return -EACCES;
1268
1e9a4ed9
MS
1269 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1270 err = inode_change_ok(inode, attr);
1271 if (err)
1272 return err;
1273 }
1274
6ff958ed
MS
1275 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1276 return 0;
1277
9e6268db 1278 if (attr->ia_valid & ATTR_SIZE) {
c08d3b0e 1279 err = inode_newsize_ok(inode, attr->ia_size);
1280 if (err)
1281 return err;
3be5a52b 1282 is_truncate = true;
9e6268db
MS
1283 }
1284
ce1d5a49
MS
1285 req = fuse_get_req(fc);
1286 if (IS_ERR(req))
1287 return PTR_ERR(req);
9e6268db 1288
3be5a52b
MS
1289 if (is_truncate)
1290 fuse_set_nowrite(inode);
1291
9e6268db 1292 memset(&inarg, 0, sizeof(inarg));
0e9663ee 1293 memset(&outarg, 0, sizeof(outarg));
befc649c 1294 iattr_to_fattr(attr, &inarg);
49d4914f
MS
1295 if (file) {
1296 struct fuse_file *ff = file->private_data;
1297 inarg.valid |= FATTR_FH;
1298 inarg.fh = ff->fh;
1299 }
f3332114
MS
1300 if (attr->ia_valid & ATTR_SIZE) {
1301 /* For mandatory locking in truncate */
1302 inarg.valid |= FATTR_LOCKOWNER;
1303 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1304 }
9e6268db
MS
1305 req->in.h.opcode = FUSE_SETATTR;
1306 req->in.h.nodeid = get_node_id(inode);
9e6268db
MS
1307 req->in.numargs = 1;
1308 req->in.args[0].size = sizeof(inarg);
1309 req->in.args[0].value = &inarg;
1310 req->out.numargs = 1;
0e9663ee
MS
1311 if (fc->minor < 9)
1312 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1313 else
1314 req->out.args[0].size = sizeof(outarg);
9e6268db 1315 req->out.args[0].value = &outarg;
b93f858a 1316 fuse_request_send(fc, req);
9e6268db
MS
1317 err = req->out.h.error;
1318 fuse_put_request(fc, req);
e00d2c2d
MS
1319 if (err) {
1320 if (err == -EINTR)
1321 fuse_invalidate_attr(inode);
3be5a52b 1322 goto error;
e00d2c2d 1323 }
9e6268db 1324
e00d2c2d
MS
1325 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1326 make_bad_inode(inode);
3be5a52b
MS
1327 err = -EIO;
1328 goto error;
1329 }
1330
1331 spin_lock(&fc->lock);
1332 fuse_change_attributes_common(inode, &outarg.attr,
1333 attr_timeout(&outarg));
1334 oldsize = inode->i_size;
1335 i_size_write(inode, outarg.attr.size);
1336
1337 if (is_truncate) {
1338 /* NOTE: this may release/reacquire fc->lock */
1339 __fuse_release_nowrite(inode);
1340 }
1341 spin_unlock(&fc->lock);
1342
1343 /*
1344 * Only call invalidate_inode_pages2() after removing
1345 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1346 */
1347 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
c08d3b0e 1348 truncate_pagecache(inode, oldsize, outarg.attr.size);
3be5a52b 1349 invalidate_inode_pages2(inode->i_mapping);
e00d2c2d
MS
1350 }
1351
e00d2c2d 1352 return 0;
3be5a52b
MS
1353
1354error:
1355 if (is_truncate)
1356 fuse_release_nowrite(inode);
1357
1358 return err;
9e6268db
MS
1359}
1360
49d4914f
MS
1361static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1362{
1363 if (attr->ia_valid & ATTR_FILE)
1364 return fuse_do_setattr(entry, attr, attr->ia_file);
1365 else
1366 return fuse_do_setattr(entry, attr, NULL);
1367}
1368
e5e5558e
MS
1369static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1370 struct kstat *stat)
1371{
1372 struct inode *inode = entry->d_inode;
244f6385 1373 struct fuse_conn *fc = get_fuse_conn(inode);
244f6385
MS
1374
1375 if (!fuse_allow_task(fc, current))
1376 return -EACCES;
1377
bcb4be80 1378 return fuse_update_attributes(inode, stat, NULL, NULL);
e5e5558e
MS
1379}
1380
92a8780e
MS
1381static int fuse_setxattr(struct dentry *entry, const char *name,
1382 const void *value, size_t size, int flags)
1383{
1384 struct inode *inode = entry->d_inode;
1385 struct fuse_conn *fc = get_fuse_conn(inode);
1386 struct fuse_req *req;
1387 struct fuse_setxattr_in inarg;
1388 int err;
1389
92a8780e
MS
1390 if (fc->no_setxattr)
1391 return -EOPNOTSUPP;
1392
ce1d5a49
MS
1393 req = fuse_get_req(fc);
1394 if (IS_ERR(req))
1395 return PTR_ERR(req);
92a8780e
MS
1396
1397 memset(&inarg, 0, sizeof(inarg));
1398 inarg.size = size;
1399 inarg.flags = flags;
1400 req->in.h.opcode = FUSE_SETXATTR;
1401 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1402 req->in.numargs = 3;
1403 req->in.args[0].size = sizeof(inarg);
1404 req->in.args[0].value = &inarg;
1405 req->in.args[1].size = strlen(name) + 1;
1406 req->in.args[1].value = name;
1407 req->in.args[2].size = size;
1408 req->in.args[2].value = value;
b93f858a 1409 fuse_request_send(fc, req);
92a8780e
MS
1410 err = req->out.h.error;
1411 fuse_put_request(fc, req);
1412 if (err == -ENOSYS) {
1413 fc->no_setxattr = 1;
1414 err = -EOPNOTSUPP;
1415 }
1416 return err;
1417}
1418
1419static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1420 void *value, size_t size)
1421{
1422 struct inode *inode = entry->d_inode;
1423 struct fuse_conn *fc = get_fuse_conn(inode);
1424 struct fuse_req *req;
1425 struct fuse_getxattr_in inarg;
1426 struct fuse_getxattr_out outarg;
1427 ssize_t ret;
1428
1429 if (fc->no_getxattr)
1430 return -EOPNOTSUPP;
1431
ce1d5a49
MS
1432 req = fuse_get_req(fc);
1433 if (IS_ERR(req))
1434 return PTR_ERR(req);
92a8780e
MS
1435
1436 memset(&inarg, 0, sizeof(inarg));
1437 inarg.size = size;
1438 req->in.h.opcode = FUSE_GETXATTR;
1439 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1440 req->in.numargs = 2;
1441 req->in.args[0].size = sizeof(inarg);
1442 req->in.args[0].value = &inarg;
1443 req->in.args[1].size = strlen(name) + 1;
1444 req->in.args[1].value = name;
1445 /* This is really two different operations rolled into one */
1446 req->out.numargs = 1;
1447 if (size) {
1448 req->out.argvar = 1;
1449 req->out.args[0].size = size;
1450 req->out.args[0].value = value;
1451 } else {
1452 req->out.args[0].size = sizeof(outarg);
1453 req->out.args[0].value = &outarg;
1454 }
b93f858a 1455 fuse_request_send(fc, req);
92a8780e
MS
1456 ret = req->out.h.error;
1457 if (!ret)
1458 ret = size ? req->out.args[0].size : outarg.size;
1459 else {
1460 if (ret == -ENOSYS) {
1461 fc->no_getxattr = 1;
1462 ret = -EOPNOTSUPP;
1463 }
1464 }
1465 fuse_put_request(fc, req);
1466 return ret;
1467}
1468
1469static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1470{
1471 struct inode *inode = entry->d_inode;
1472 struct fuse_conn *fc = get_fuse_conn(inode);
1473 struct fuse_req *req;
1474 struct fuse_getxattr_in inarg;
1475 struct fuse_getxattr_out outarg;
1476 ssize_t ret;
1477
e57ac683
MS
1478 if (!fuse_allow_task(fc, current))
1479 return -EACCES;
1480
92a8780e
MS
1481 if (fc->no_listxattr)
1482 return -EOPNOTSUPP;
1483
ce1d5a49
MS
1484 req = fuse_get_req(fc);
1485 if (IS_ERR(req))
1486 return PTR_ERR(req);
92a8780e
MS
1487
1488 memset(&inarg, 0, sizeof(inarg));
1489 inarg.size = size;
1490 req->in.h.opcode = FUSE_LISTXATTR;
1491 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1492 req->in.numargs = 1;
1493 req->in.args[0].size = sizeof(inarg);
1494 req->in.args[0].value = &inarg;
1495 /* This is really two different operations rolled into one */
1496 req->out.numargs = 1;
1497 if (size) {
1498 req->out.argvar = 1;
1499 req->out.args[0].size = size;
1500 req->out.args[0].value = list;
1501 } else {
1502 req->out.args[0].size = sizeof(outarg);
1503 req->out.args[0].value = &outarg;
1504 }
b93f858a 1505 fuse_request_send(fc, req);
92a8780e
MS
1506 ret = req->out.h.error;
1507 if (!ret)
1508 ret = size ? req->out.args[0].size : outarg.size;
1509 else {
1510 if (ret == -ENOSYS) {
1511 fc->no_listxattr = 1;
1512 ret = -EOPNOTSUPP;
1513 }
1514 }
1515 fuse_put_request(fc, req);
1516 return ret;
1517}
1518
1519static int fuse_removexattr(struct dentry *entry, const char *name)
1520{
1521 struct inode *inode = entry->d_inode;
1522 struct fuse_conn *fc = get_fuse_conn(inode);
1523 struct fuse_req *req;
1524 int err;
1525
1526 if (fc->no_removexattr)
1527 return -EOPNOTSUPP;
1528
ce1d5a49
MS
1529 req = fuse_get_req(fc);
1530 if (IS_ERR(req))
1531 return PTR_ERR(req);
92a8780e
MS
1532
1533 req->in.h.opcode = FUSE_REMOVEXATTR;
1534 req->in.h.nodeid = get_node_id(inode);
92a8780e
MS
1535 req->in.numargs = 1;
1536 req->in.args[0].size = strlen(name) + 1;
1537 req->in.args[0].value = name;
b93f858a 1538 fuse_request_send(fc, req);
92a8780e
MS
1539 err = req->out.h.error;
1540 fuse_put_request(fc, req);
1541 if (err == -ENOSYS) {
1542 fc->no_removexattr = 1;
1543 err = -EOPNOTSUPP;
1544 }
1545 return err;
1546}
1547
754661f1 1548static const struct inode_operations fuse_dir_inode_operations = {
e5e5558e 1549 .lookup = fuse_lookup,
9e6268db
MS
1550 .mkdir = fuse_mkdir,
1551 .symlink = fuse_symlink,
1552 .unlink = fuse_unlink,
1553 .rmdir = fuse_rmdir,
1554 .rename = fuse_rename,
1555 .link = fuse_link,
1556 .setattr = fuse_setattr,
1557 .create = fuse_create,
1558 .mknod = fuse_mknod,
e5e5558e
MS
1559 .permission = fuse_permission,
1560 .getattr = fuse_getattr,
92a8780e
MS
1561 .setxattr = fuse_setxattr,
1562 .getxattr = fuse_getxattr,
1563 .listxattr = fuse_listxattr,
1564 .removexattr = fuse_removexattr,
e5e5558e
MS
1565};
1566
4b6f5d20 1567static const struct file_operations fuse_dir_operations = {
b6aeaded 1568 .llseek = generic_file_llseek,
e5e5558e
MS
1569 .read = generic_read_dir,
1570 .readdir = fuse_readdir,
1571 .open = fuse_dir_open,
1572 .release = fuse_dir_release,
82547981 1573 .fsync = fuse_dir_fsync,
e5e5558e
MS
1574};
1575
754661f1 1576static const struct inode_operations fuse_common_inode_operations = {
9e6268db 1577 .setattr = fuse_setattr,
e5e5558e
MS
1578 .permission = fuse_permission,
1579 .getattr = fuse_getattr,
92a8780e
MS
1580 .setxattr = fuse_setxattr,
1581 .getxattr = fuse_getxattr,
1582 .listxattr = fuse_listxattr,
1583 .removexattr = fuse_removexattr,
e5e5558e
MS
1584};
1585
754661f1 1586static const struct inode_operations fuse_symlink_inode_operations = {
9e6268db 1587 .setattr = fuse_setattr,
e5e5558e
MS
1588 .follow_link = fuse_follow_link,
1589 .put_link = fuse_put_link,
1590 .readlink = generic_readlink,
1591 .getattr = fuse_getattr,
92a8780e
MS
1592 .setxattr = fuse_setxattr,
1593 .getxattr = fuse_getxattr,
1594 .listxattr = fuse_listxattr,
1595 .removexattr = fuse_removexattr,
e5e5558e
MS
1596};
1597
1598void fuse_init_common(struct inode *inode)
1599{
1600 inode->i_op = &fuse_common_inode_operations;
1601}
1602
1603void fuse_init_dir(struct inode *inode)
1604{
1605 inode->i_op = &fuse_dir_inode_operations;
1606 inode->i_fop = &fuse_dir_operations;
1607}
1608
1609void fuse_init_symlink(struct inode *inode)
1610{
1611 inode->i_op = &fuse_symlink_inode_operations;
1612}