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