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