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