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