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