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> |
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 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 | ||
5571f1e6 | 1163 | static int fuse_readlink_page(struct inode *inode, struct page *page) |
e5e5558e | 1164 | { |
e5e5558e | 1165 | struct fuse_conn *fc = get_fuse_conn(inode); |
5571f1e6 DS |
1166 | struct fuse_req *req; |
1167 | int err; | |
e5e5558e | 1168 | |
5571f1e6 DS |
1169 | req = fuse_get_req(fc, 1); |
1170 | if (IS_ERR(req)) | |
1171 | return PTR_ERR(req); | |
1172 | ||
1173 | req->out.page_zeroing = 1; | |
1174 | req->out.argpages = 1; | |
1175 | req->num_pages = 1; | |
1176 | req->pages[0] = page; | |
1177 | req->page_descs[0].length = PAGE_SIZE - 1; | |
1178 | req->in.h.opcode = FUSE_READLINK; | |
1179 | req->in.h.nodeid = get_node_id(inode); | |
1180 | req->out.argvar = 1; | |
1181 | req->out.numargs = 1; | |
1182 | req->out.args[0].size = PAGE_SIZE - 1; | |
1183 | fuse_request_send(fc, req); | |
1184 | err = req->out.h.error; | |
6b255391 | 1185 | |
5571f1e6 DS |
1186 | if (!err) { |
1187 | char *link = page_address(page); | |
1188 | size_t len = req->out.args[0].size; | |
7078187a | 1189 | |
5571f1e6 DS |
1190 | BUG_ON(len >= PAGE_SIZE); |
1191 | link[len] = '\0'; | |
7078187a | 1192 | } |
5571f1e6 DS |
1193 | |
1194 | fuse_put_request(fc, req); | |
451418fc | 1195 | fuse_invalidate_atime(inode); |
5571f1e6 DS |
1196 | |
1197 | return err; | |
1198 | } | |
1199 | ||
1200 | static const char *fuse_get_link(struct dentry *dentry, struct inode *inode, | |
1201 | struct delayed_call *callback) | |
1202 | { | |
1203 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1204 | struct page *page; | |
1205 | int err; | |
1206 | ||
1207 | err = -EIO; | |
1208 | if (is_bad_inode(inode)) | |
1209 | goto out_err; | |
1210 | ||
1211 | if (fc->cache_symlinks) | |
1212 | return page_get_link(dentry, inode, callback); | |
1213 | ||
1214 | err = -ECHILD; | |
1215 | if (!dentry) | |
1216 | goto out_err; | |
1217 | ||
1218 | page = alloc_page(GFP_KERNEL); | |
1219 | err = -ENOMEM; | |
1220 | if (!page) | |
1221 | goto out_err; | |
1222 | ||
1223 | err = fuse_readlink_page(inode, page); | |
1224 | if (err) { | |
1225 | __free_page(page); | |
1226 | goto out_err; | |
1227 | } | |
1228 | ||
1229 | set_delayed_call(callback, page_put_link, page); | |
1230 | ||
1231 | return page_address(page); | |
1232 | ||
1233 | out_err: | |
1234 | return ERR_PTR(err); | |
e5e5558e MS |
1235 | } |
1236 | ||
e5e5558e MS |
1237 | static int fuse_dir_open(struct inode *inode, struct file *file) |
1238 | { | |
91fe96b4 | 1239 | return fuse_open_common(inode, file, true); |
e5e5558e MS |
1240 | } |
1241 | ||
1242 | static int fuse_dir_release(struct inode *inode, struct file *file) | |
1243 | { | |
8b0797a4 MS |
1244 | fuse_release_common(file, FUSE_RELEASEDIR); |
1245 | ||
1246 | return 0; | |
e5e5558e MS |
1247 | } |
1248 | ||
02c24a82 JB |
1249 | static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end, |
1250 | int datasync) | |
82547981 | 1251 | { |
02c24a82 | 1252 | return fuse_fsync_common(file, start, end, datasync, 1); |
82547981 MS |
1253 | } |
1254 | ||
b18da0c5 MS |
1255 | static long fuse_dir_ioctl(struct file *file, unsigned int cmd, |
1256 | unsigned long arg) | |
1257 | { | |
1258 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); | |
1259 | ||
1260 | /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */ | |
1261 | if (fc->minor < 18) | |
1262 | return -ENOTTY; | |
1263 | ||
1264 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR); | |
1265 | } | |
1266 | ||
1267 | static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd, | |
1268 | unsigned long arg) | |
1269 | { | |
1270 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); | |
1271 | ||
1272 | if (fc->minor < 18) | |
1273 | return -ENOTTY; | |
1274 | ||
1275 | return fuse_ioctl_common(file, cmd, arg, | |
1276 | FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR); | |
1277 | } | |
1278 | ||
b0aa7606 | 1279 | static bool update_mtime(unsigned ivalid, bool trust_local_mtime) |
17637cba MS |
1280 | { |
1281 | /* Always update if mtime is explicitly set */ | |
1282 | if (ivalid & ATTR_MTIME_SET) | |
1283 | return true; | |
1284 | ||
b0aa7606 MP |
1285 | /* Or if kernel i_mtime is the official one */ |
1286 | if (trust_local_mtime) | |
1287 | return true; | |
1288 | ||
17637cba MS |
1289 | /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ |
1290 | if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) | |
1291 | return false; | |
1292 | ||
1293 | /* In all other cases update */ | |
1294 | return true; | |
1295 | } | |
1296 | ||
8cb08329 EB |
1297 | static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr, |
1298 | struct fuse_setattr_in *arg, bool trust_local_cmtime) | |
9e6268db MS |
1299 | { |
1300 | unsigned ivalid = iattr->ia_valid; | |
9e6268db MS |
1301 | |
1302 | if (ivalid & ATTR_MODE) | |
befc649c | 1303 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
9e6268db | 1304 | if (ivalid & ATTR_UID) |
8cb08329 | 1305 | arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid); |
9e6268db | 1306 | if (ivalid & ATTR_GID) |
8cb08329 | 1307 | arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid); |
9e6268db | 1308 | if (ivalid & ATTR_SIZE) |
befc649c | 1309 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
17637cba MS |
1310 | if (ivalid & ATTR_ATIME) { |
1311 | arg->valid |= FATTR_ATIME; | |
befc649c | 1312 | arg->atime = iattr->ia_atime.tv_sec; |
17637cba MS |
1313 | arg->atimensec = iattr->ia_atime.tv_nsec; |
1314 | if (!(ivalid & ATTR_ATIME_SET)) | |
1315 | arg->valid |= FATTR_ATIME_NOW; | |
1316 | } | |
3ad22c62 | 1317 | if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) { |
17637cba | 1318 | arg->valid |= FATTR_MTIME; |
befc649c | 1319 | arg->mtime = iattr->ia_mtime.tv_sec; |
17637cba | 1320 | arg->mtimensec = iattr->ia_mtime.tv_nsec; |
3ad22c62 | 1321 | if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime) |
17637cba | 1322 | arg->valid |= FATTR_MTIME_NOW; |
befc649c | 1323 | } |
3ad22c62 MP |
1324 | if ((ivalid & ATTR_CTIME) && trust_local_cmtime) { |
1325 | arg->valid |= FATTR_CTIME; | |
1326 | arg->ctime = iattr->ia_ctime.tv_sec; | |
1327 | arg->ctimensec = iattr->ia_ctime.tv_nsec; | |
1328 | } | |
9e6268db MS |
1329 | } |
1330 | ||
3be5a52b MS |
1331 | /* |
1332 | * Prevent concurrent writepages on inode | |
1333 | * | |
1334 | * This is done by adding a negative bias to the inode write counter | |
1335 | * and waiting for all pending writes to finish. | |
1336 | */ | |
1337 | void fuse_set_nowrite(struct inode *inode) | |
1338 | { | |
1339 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1340 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1341 | ||
5955102c | 1342 | BUG_ON(!inode_is_locked(inode)); |
3be5a52b MS |
1343 | |
1344 | spin_lock(&fc->lock); | |
1345 | BUG_ON(fi->writectr < 0); | |
1346 | fi->writectr += FUSE_NOWRITE; | |
1347 | spin_unlock(&fc->lock); | |
1348 | wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE); | |
1349 | } | |
1350 | ||
1351 | /* | |
1352 | * Allow writepages on inode | |
1353 | * | |
1354 | * Remove the bias from the writecounter and send any queued | |
1355 | * writepages. | |
1356 | */ | |
1357 | static void __fuse_release_nowrite(struct inode *inode) | |
1358 | { | |
1359 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1360 | ||
1361 | BUG_ON(fi->writectr != FUSE_NOWRITE); | |
1362 | fi->writectr = 0; | |
1363 | fuse_flush_writepages(inode); | |
1364 | } | |
1365 | ||
1366 | void fuse_release_nowrite(struct inode *inode) | |
1367 | { | |
1368 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1369 | ||
1370 | spin_lock(&fc->lock); | |
1371 | __fuse_release_nowrite(inode); | |
1372 | spin_unlock(&fc->lock); | |
1373 | } | |
1374 | ||
7078187a | 1375 | static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args, |
b0aa7606 MP |
1376 | struct inode *inode, |
1377 | struct fuse_setattr_in *inarg_p, | |
1378 | struct fuse_attr_out *outarg_p) | |
1379 | { | |
7078187a MS |
1380 | args->in.h.opcode = FUSE_SETATTR; |
1381 | args->in.h.nodeid = get_node_id(inode); | |
1382 | args->in.numargs = 1; | |
1383 | args->in.args[0].size = sizeof(*inarg_p); | |
1384 | args->in.args[0].value = inarg_p; | |
1385 | args->out.numargs = 1; | |
21f62174 | 1386 | args->out.args[0].size = sizeof(*outarg_p); |
7078187a | 1387 | args->out.args[0].value = outarg_p; |
b0aa7606 MP |
1388 | } |
1389 | ||
1390 | /* | |
1391 | * Flush inode->i_mtime to the server | |
1392 | */ | |
ab9e13f7 | 1393 | int fuse_flush_times(struct inode *inode, struct fuse_file *ff) |
b0aa7606 | 1394 | { |
b0aa7606 | 1395 | struct fuse_conn *fc = get_fuse_conn(inode); |
7078187a | 1396 | FUSE_ARGS(args); |
b0aa7606 MP |
1397 | struct fuse_setattr_in inarg; |
1398 | struct fuse_attr_out outarg; | |
b0aa7606 MP |
1399 | |
1400 | memset(&inarg, 0, sizeof(inarg)); | |
1401 | memset(&outarg, 0, sizeof(outarg)); | |
1402 | ||
ab9e13f7 | 1403 | inarg.valid = FATTR_MTIME; |
b0aa7606 MP |
1404 | inarg.mtime = inode->i_mtime.tv_sec; |
1405 | inarg.mtimensec = inode->i_mtime.tv_nsec; | |
ab9e13f7 MP |
1406 | if (fc->minor >= 23) { |
1407 | inarg.valid |= FATTR_CTIME; | |
1408 | inarg.ctime = inode->i_ctime.tv_sec; | |
1409 | inarg.ctimensec = inode->i_ctime.tv_nsec; | |
1410 | } | |
1e18bda8 MS |
1411 | if (ff) { |
1412 | inarg.valid |= FATTR_FH; | |
1413 | inarg.fh = ff->fh; | |
1414 | } | |
7078187a | 1415 | fuse_setattr_fill(fc, &args, inode, &inarg, &outarg); |
b0aa7606 | 1416 | |
7078187a | 1417 | return fuse_simple_request(fc, &args); |
b0aa7606 MP |
1418 | } |
1419 | ||
6f9f1180 MS |
1420 | /* |
1421 | * Set attributes, and at the same time refresh them. | |
1422 | * | |
1423 | * Truncation is slightly complicated, because the 'truncate' request | |
1424 | * may fail, in which case we don't want to touch the mapping. | |
9ffbb916 MS |
1425 | * vmtruncate() doesn't allow for this case, so do the rlimit checking |
1426 | * and the actual truncation by hand. | |
6f9f1180 | 1427 | */ |
62490330 | 1428 | int fuse_do_setattr(struct dentry *dentry, struct iattr *attr, |
efb9fa9e | 1429 | struct file *file) |
9e6268db | 1430 | { |
62490330 | 1431 | struct inode *inode = d_inode(dentry); |
9e6268db | 1432 | struct fuse_conn *fc = get_fuse_conn(inode); |
06a7c3c2 | 1433 | struct fuse_inode *fi = get_fuse_inode(inode); |
7078187a | 1434 | FUSE_ARGS(args); |
9e6268db MS |
1435 | struct fuse_setattr_in inarg; |
1436 | struct fuse_attr_out outarg; | |
3be5a52b | 1437 | bool is_truncate = false; |
8373200b | 1438 | bool is_wb = fc->writeback_cache; |
3be5a52b | 1439 | loff_t oldsize; |
9e6268db | 1440 | int err; |
3ad22c62 | 1441 | bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode); |
9e6268db | 1442 | |
29433a29 | 1443 | if (!fc->default_permissions) |
db78b877 CH |
1444 | attr->ia_valid |= ATTR_FORCE; |
1445 | ||
31051c85 | 1446 | err = setattr_prepare(dentry, attr); |
db78b877 CH |
1447 | if (err) |
1448 | return err; | |
1e9a4ed9 | 1449 | |
8d56addd | 1450 | if (attr->ia_valid & ATTR_OPEN) { |
df0e91d4 MS |
1451 | /* This is coming from open(..., ... | O_TRUNC); */ |
1452 | WARN_ON(!(attr->ia_valid & ATTR_SIZE)); | |
1453 | WARN_ON(attr->ia_size != 0); | |
1454 | if (fc->atomic_o_trunc) { | |
1455 | /* | |
1456 | * No need to send request to userspace, since actual | |
1457 | * truncation has already been done by OPEN. But still | |
1458 | * need to truncate page cache. | |
1459 | */ | |
1460 | i_size_write(inode, 0); | |
1461 | truncate_pagecache(inode, 0); | |
8d56addd | 1462 | return 0; |
df0e91d4 | 1463 | } |
8d56addd MS |
1464 | file = NULL; |
1465 | } | |
6ff958ed | 1466 | |
ab2257e9 MS |
1467 | if (attr->ia_valid & ATTR_SIZE) { |
1468 | if (WARN_ON(!S_ISREG(inode->i_mode))) | |
1469 | return -EIO; | |
3be5a52b | 1470 | is_truncate = true; |
ab2257e9 | 1471 | } |
9e6268db | 1472 | |
06a7c3c2 | 1473 | if (is_truncate) { |
3be5a52b | 1474 | fuse_set_nowrite(inode); |
06a7c3c2 | 1475 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
3ad22c62 MP |
1476 | if (trust_local_cmtime && attr->ia_size != inode->i_size) |
1477 | attr->ia_valid |= ATTR_MTIME | ATTR_CTIME; | |
06a7c3c2 | 1478 | } |
3be5a52b | 1479 | |
9e6268db | 1480 | memset(&inarg, 0, sizeof(inarg)); |
0e9663ee | 1481 | memset(&outarg, 0, sizeof(outarg)); |
8cb08329 | 1482 | iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime); |
49d4914f MS |
1483 | if (file) { |
1484 | struct fuse_file *ff = file->private_data; | |
1485 | inarg.valid |= FATTR_FH; | |
1486 | inarg.fh = ff->fh; | |
1487 | } | |
f3332114 MS |
1488 | if (attr->ia_valid & ATTR_SIZE) { |
1489 | /* For mandatory locking in truncate */ | |
1490 | inarg.valid |= FATTR_LOCKOWNER; | |
1491 | inarg.lock_owner = fuse_lock_owner_id(fc, current->files); | |
1492 | } | |
7078187a MS |
1493 | fuse_setattr_fill(fc, &args, inode, &inarg, &outarg); |
1494 | err = fuse_simple_request(fc, &args); | |
e00d2c2d MS |
1495 | if (err) { |
1496 | if (err == -EINTR) | |
1497 | fuse_invalidate_attr(inode); | |
3be5a52b | 1498 | goto error; |
e00d2c2d | 1499 | } |
9e6268db | 1500 | |
e00d2c2d MS |
1501 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
1502 | make_bad_inode(inode); | |
3be5a52b MS |
1503 | err = -EIO; |
1504 | goto error; | |
1505 | } | |
1506 | ||
1507 | spin_lock(&fc->lock); | |
b0aa7606 | 1508 | /* the kernel maintains i_mtime locally */ |
3ad22c62 MP |
1509 | if (trust_local_cmtime) { |
1510 | if (attr->ia_valid & ATTR_MTIME) | |
1511 | inode->i_mtime = attr->ia_mtime; | |
1512 | if (attr->ia_valid & ATTR_CTIME) | |
1513 | inode->i_ctime = attr->ia_ctime; | |
1e18bda8 | 1514 | /* FIXME: clear I_DIRTY_SYNC? */ |
b0aa7606 MP |
1515 | } |
1516 | ||
3be5a52b MS |
1517 | fuse_change_attributes_common(inode, &outarg.attr, |
1518 | attr_timeout(&outarg)); | |
1519 | oldsize = inode->i_size; | |
8373200b PE |
1520 | /* see the comment in fuse_change_attributes() */ |
1521 | if (!is_wb || is_truncate || !S_ISREG(inode->i_mode)) | |
1522 | i_size_write(inode, outarg.attr.size); | |
3be5a52b MS |
1523 | |
1524 | if (is_truncate) { | |
1525 | /* NOTE: this may release/reacquire fc->lock */ | |
1526 | __fuse_release_nowrite(inode); | |
1527 | } | |
1528 | spin_unlock(&fc->lock); | |
1529 | ||
1530 | /* | |
1531 | * Only call invalidate_inode_pages2() after removing | |
1532 | * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock. | |
1533 | */ | |
8373200b PE |
1534 | if ((is_truncate || !is_wb) && |
1535 | S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) { | |
7caef267 | 1536 | truncate_pagecache(inode, outarg.attr.size); |
3be5a52b | 1537 | invalidate_inode_pages2(inode->i_mapping); |
e00d2c2d MS |
1538 | } |
1539 | ||
06a7c3c2 | 1540 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
e00d2c2d | 1541 | return 0; |
3be5a52b MS |
1542 | |
1543 | error: | |
1544 | if (is_truncate) | |
1545 | fuse_release_nowrite(inode); | |
1546 | ||
06a7c3c2 | 1547 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
3be5a52b | 1548 | return err; |
9e6268db MS |
1549 | } |
1550 | ||
49d4914f MS |
1551 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
1552 | { | |
2b0143b5 | 1553 | struct inode *inode = d_inode(entry); |
5e940c1d | 1554 | struct fuse_conn *fc = get_fuse_conn(inode); |
a09f99ed | 1555 | struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL; |
5e2b8828 | 1556 | int ret; |
efb9fa9e MP |
1557 | |
1558 | if (!fuse_allow_current_process(get_fuse_conn(inode))) | |
1559 | return -EACCES; | |
1560 | ||
a09f99ed | 1561 | if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) { |
a09f99ed MS |
1562 | attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | |
1563 | ATTR_MODE); | |
5e940c1d | 1564 | |
a09f99ed | 1565 | /* |
5e940c1d MS |
1566 | * The only sane way to reliably kill suid/sgid is to do it in |
1567 | * the userspace filesystem | |
1568 | * | |
1569 | * This should be done on write(), truncate() and chown(). | |
a09f99ed | 1570 | */ |
5e940c1d | 1571 | if (!fc->handle_killpriv) { |
5e940c1d MS |
1572 | /* |
1573 | * ia_mode calculation may have used stale i_mode. | |
1574 | * Refresh and recalculate. | |
1575 | */ | |
1576 | ret = fuse_do_getattr(inode, NULL, file); | |
1577 | if (ret) | |
1578 | return ret; | |
1579 | ||
1580 | attr->ia_mode = inode->i_mode; | |
c01638f5 | 1581 | if (inode->i_mode & S_ISUID) { |
5e940c1d MS |
1582 | attr->ia_valid |= ATTR_MODE; |
1583 | attr->ia_mode &= ~S_ISUID; | |
1584 | } | |
c01638f5 | 1585 | if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
5e940c1d MS |
1586 | attr->ia_valid |= ATTR_MODE; |
1587 | attr->ia_mode &= ~S_ISGID; | |
1588 | } | |
a09f99ed MS |
1589 | } |
1590 | } | |
1591 | if (!attr->ia_valid) | |
1592 | return 0; | |
5e2b8828 | 1593 | |
abb5a14f | 1594 | ret = fuse_do_setattr(entry, attr, file); |
5e2b8828 | 1595 | if (!ret) { |
60bcc88a SF |
1596 | /* |
1597 | * If filesystem supports acls it may have updated acl xattrs in | |
1598 | * the filesystem, so forget cached acls for the inode. | |
1599 | */ | |
1600 | if (fc->posix_acl) | |
1601 | forget_all_cached_acls(inode); | |
1602 | ||
5e2b8828 MS |
1603 | /* Directory mode changed, may need to revalidate access */ |
1604 | if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE)) | |
1605 | fuse_invalidate_entry_cache(entry); | |
1606 | } | |
1607 | return ret; | |
49d4914f MS |
1608 | } |
1609 | ||
a528d35e DH |
1610 | static int fuse_getattr(const struct path *path, struct kstat *stat, |
1611 | u32 request_mask, unsigned int flags) | |
e5e5558e | 1612 | { |
a528d35e | 1613 | struct inode *inode = d_inode(path->dentry); |
244f6385 | 1614 | struct fuse_conn *fc = get_fuse_conn(inode); |
244f6385 | 1615 | |
c2132c1b | 1616 | if (!fuse_allow_current_process(fc)) |
244f6385 MS |
1617 | return -EACCES; |
1618 | ||
2f1e8196 | 1619 | return fuse_update_get_attr(inode, NULL, stat, request_mask, flags); |
e5e5558e MS |
1620 | } |
1621 | ||
754661f1 | 1622 | static const struct inode_operations fuse_dir_inode_operations = { |
e5e5558e | 1623 | .lookup = fuse_lookup, |
9e6268db MS |
1624 | .mkdir = fuse_mkdir, |
1625 | .symlink = fuse_symlink, | |
1626 | .unlink = fuse_unlink, | |
1627 | .rmdir = fuse_rmdir, | |
2773bf00 | 1628 | .rename = fuse_rename2, |
9e6268db MS |
1629 | .link = fuse_link, |
1630 | .setattr = fuse_setattr, | |
1631 | .create = fuse_create, | |
c8ccbe03 | 1632 | .atomic_open = fuse_atomic_open, |
9e6268db | 1633 | .mknod = fuse_mknod, |
e5e5558e MS |
1634 | .permission = fuse_permission, |
1635 | .getattr = fuse_getattr, | |
92a8780e | 1636 | .listxattr = fuse_listxattr, |
60bcc88a SF |
1637 | .get_acl = fuse_get_acl, |
1638 | .set_acl = fuse_set_acl, | |
e5e5558e MS |
1639 | }; |
1640 | ||
4b6f5d20 | 1641 | static const struct file_operations fuse_dir_operations = { |
b6aeaded | 1642 | .llseek = generic_file_llseek, |
e5e5558e | 1643 | .read = generic_read_dir, |
d9b3dbdc | 1644 | .iterate_shared = fuse_readdir, |
e5e5558e MS |
1645 | .open = fuse_dir_open, |
1646 | .release = fuse_dir_release, | |
82547981 | 1647 | .fsync = fuse_dir_fsync, |
b18da0c5 MS |
1648 | .unlocked_ioctl = fuse_dir_ioctl, |
1649 | .compat_ioctl = fuse_dir_compat_ioctl, | |
e5e5558e MS |
1650 | }; |
1651 | ||
754661f1 | 1652 | static const struct inode_operations fuse_common_inode_operations = { |
9e6268db | 1653 | .setattr = fuse_setattr, |
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 | ||
754661f1 | 1661 | static const struct inode_operations fuse_symlink_inode_operations = { |
9e6268db | 1662 | .setattr = fuse_setattr, |
6b255391 | 1663 | .get_link = fuse_get_link, |
e5e5558e | 1664 | .getattr = fuse_getattr, |
92a8780e | 1665 | .listxattr = fuse_listxattr, |
e5e5558e MS |
1666 | }; |
1667 | ||
1668 | void fuse_init_common(struct inode *inode) | |
1669 | { | |
1670 | inode->i_op = &fuse_common_inode_operations; | |
1671 | } | |
1672 | ||
1673 | void fuse_init_dir(struct inode *inode) | |
1674 | { | |
ab2257e9 MS |
1675 | struct fuse_inode *fi = get_fuse_inode(inode); |
1676 | ||
e5e5558e MS |
1677 | inode->i_op = &fuse_dir_inode_operations; |
1678 | inode->i_fop = &fuse_dir_operations; | |
ab2257e9 MS |
1679 | |
1680 | spin_lock_init(&fi->rdc.lock); | |
1681 | fi->rdc.cached = false; | |
1682 | fi->rdc.size = 0; | |
1683 | fi->rdc.pos = 0; | |
1684 | fi->rdc.version = 0; | |
e5e5558e MS |
1685 | } |
1686 | ||
5571f1e6 DS |
1687 | static int fuse_symlink_readpage(struct file *null, struct page *page) |
1688 | { | |
1689 | int err = fuse_readlink_page(page->mapping->host, page); | |
1690 | ||
1691 | if (!err) | |
1692 | SetPageUptodate(page); | |
1693 | ||
1694 | unlock_page(page); | |
1695 | ||
1696 | return err; | |
1697 | } | |
1698 | ||
1699 | static const struct address_space_operations fuse_symlink_aops = { | |
1700 | .readpage = fuse_symlink_readpage, | |
1701 | }; | |
1702 | ||
e5e5558e MS |
1703 | void fuse_init_symlink(struct inode *inode) |
1704 | { | |
1705 | inode->i_op = &fuse_symlink_inode_operations; | |
5571f1e6 DS |
1706 | inode->i_data.a_ops = &fuse_symlink_aops; |
1707 | inode_nohighmem(inode); | |
e5e5558e | 1708 | } |