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