]>
Commit | Line | Data |
---|---|---|
e5e5558e MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
1729a16c | 3 | Copyright (C) 2001-2008 Miklos Szeredi <[email protected]> |
e5e5558e MS |
4 | |
5 | This program can be distributed under the terms of the GNU GPL. | |
6 | See the file COPYING. | |
7 | */ | |
8 | ||
9 | #include "fuse_i.h" | |
10 | ||
11 | #include <linux/pagemap.h> | |
12 | #include <linux/file.h> | |
bf109c64 | 13 | #include <linux/fs_context.h> |
9ccf47b2 | 14 | #include <linux/moduleparam.h> |
e5e5558e MS |
15 | #include <linux/sched.h> |
16 | #include <linux/namei.h> | |
07e77dca | 17 | #include <linux/slab.h> |
703c7362 | 18 | #include <linux/xattr.h> |
261aaba7 | 19 | #include <linux/iversion.h> |
60bcc88a | 20 | #include <linux/posix_acl.h> |
3e2b6fdb VG |
21 | #include <linux/security.h> |
22 | #include <linux/types.h> | |
23 | #include <linux/kernel.h> | |
e5e5558e | 24 | |
9ccf47b2 DM |
25 | static bool __read_mostly allow_sys_admin_access; |
26 | module_param(allow_sys_admin_access, bool, 0644); | |
27 | MODULE_PARM_DESC(allow_sys_admin_access, | |
28 | "Allow users with CAP_SYS_ADMIN in initial userns to bypass allow_other access check"); | |
29 | ||
4582a4ab FS |
30 | static void fuse_advise_use_readdirplus(struct inode *dir) |
31 | { | |
32 | struct fuse_inode *fi = get_fuse_inode(dir); | |
33 | ||
34 | set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state); | |
35 | } | |
36 | ||
30c6a23d KK |
37 | #if BITS_PER_LONG >= 64 |
38 | static inline void __fuse_dentry_settime(struct dentry *entry, u64 time) | |
39 | { | |
40 | entry->d_fsdata = (void *) time; | |
41 | } | |
42 | ||
43 | static inline u64 fuse_dentry_time(const struct dentry *entry) | |
44 | { | |
45 | return (u64)entry->d_fsdata; | |
46 | } | |
47 | ||
48 | #else | |
f75fdf22 MS |
49 | union fuse_dentry { |
50 | u64 time; | |
51 | struct rcu_head rcu; | |
52 | }; | |
53 | ||
30c6a23d KK |
54 | static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time) |
55 | { | |
56 | ((union fuse_dentry *) dentry->d_fsdata)->time = time; | |
57 | } | |
58 | ||
59 | static inline u64 fuse_dentry_time(const struct dentry *entry) | |
60 | { | |
61 | return ((union fuse_dentry *) entry->d_fsdata)->time; | |
62 | } | |
63 | #endif | |
64 | ||
8fab0106 | 65 | static void fuse_dentry_settime(struct dentry *dentry, u64 time) |
0a0898cf | 66 | { |
8fab0106 MS |
67 | struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb); |
68 | bool delete = !time && fc->delete_stale; | |
69 | /* | |
70 | * Mess with DCACHE_OP_DELETE because dput() will be faster without it. | |
71 | * Don't care about races, either way it's just an optimization | |
72 | */ | |
73 | if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) || | |
74 | (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) { | |
75 | spin_lock(&dentry->d_lock); | |
76 | if (!delete) | |
77 | dentry->d_flags &= ~DCACHE_OP_DELETE; | |
78 | else | |
79 | dentry->d_flags |= DCACHE_OP_DELETE; | |
80 | spin_unlock(&dentry->d_lock); | |
81 | } | |
82 | ||
30c6a23d | 83 | __fuse_dentry_settime(dentry, time); |
0a0898cf | 84 | } |
0a0898cf | 85 | |
6f9f1180 MS |
86 | /* |
87 | * FUSE caches dentries and attributes with separate timeout. The | |
88 | * time in jiffies until the dentry/attributes are valid is stored in | |
f75fdf22 | 89 | * dentry->d_fsdata and fuse_inode->i_time respectively. |
6f9f1180 MS |
90 | */ |
91 | ||
92 | /* | |
93 | * Calculate the time in jiffies until a dentry/attributes are valid | |
94 | */ | |
9dc10a54 | 95 | u64 fuse_time_to_jiffies(u64 sec, u32 nsec) |
e5e5558e | 96 | { |
685d16dd | 97 | if (sec || nsec) { |
bcb6f6d2 MS |
98 | struct timespec64 ts = { |
99 | sec, | |
21067527 | 100 | min_t(u32, nsec, NSEC_PER_SEC - 1) |
bcb6f6d2 MS |
101 | }; |
102 | ||
103 | return get_jiffies_64() + timespec64_to_jiffies(&ts); | |
685d16dd | 104 | } else |
0a0898cf | 105 | return 0; |
e5e5558e MS |
106 | } |
107 | ||
6f9f1180 MS |
108 | /* |
109 | * Set dentry and possibly attribute timeouts from the lookup/mk* | |
110 | * replies | |
111 | */ | |
d123d8e1 | 112 | void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o) |
0aa7c699 | 113 | { |
0a0898cf | 114 | fuse_dentry_settime(entry, |
9dc10a54 | 115 | fuse_time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); |
8cbdf1e6 MS |
116 | } |
117 | ||
fa5eee57 | 118 | void fuse_invalidate_attr_mask(struct inode *inode, u32 mask) |
2f1e8196 MS |
119 | { |
120 | set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask); | |
121 | } | |
122 | ||
6f9f1180 MS |
123 | /* |
124 | * Mark the attributes as stale, so that at the next call to | |
125 | * ->getattr() they will be fetched from userspace | |
126 | */ | |
8cbdf1e6 MS |
127 | void fuse_invalidate_attr(struct inode *inode) |
128 | { | |
2f1e8196 | 129 | fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS); |
8cbdf1e6 MS |
130 | } |
131 | ||
261aaba7 MS |
132 | static void fuse_dir_changed(struct inode *dir) |
133 | { | |
134 | fuse_invalidate_attr(dir); | |
135 | inode_maybe_inc_iversion(dir, false); | |
136 | } | |
137 | ||
06bbb761 | 138 | /* |
451418fc AG |
139 | * Mark the attributes as stale due to an atime change. Avoid the invalidate if |
140 | * atime is not used. | |
141 | */ | |
142 | void fuse_invalidate_atime(struct inode *inode) | |
143 | { | |
144 | if (!IS_RDONLY(inode)) | |
2f1e8196 | 145 | fuse_invalidate_attr_mask(inode, STATX_ATIME); |
451418fc AG |
146 | } |
147 | ||
6f9f1180 MS |
148 | /* |
149 | * Just mark the entry as stale, so that a next attempt to look it up | |
150 | * will result in a new lookup call to userspace | |
151 | * | |
152 | * This is called when a dentry is about to become negative and the | |
153 | * timeout is unknown (unlink, rmdir, rename and in some cases | |
154 | * lookup) | |
155 | */ | |
dbd561d2 | 156 | void fuse_invalidate_entry_cache(struct dentry *entry) |
8cbdf1e6 | 157 | { |
0a0898cf | 158 | fuse_dentry_settime(entry, 0); |
8cbdf1e6 MS |
159 | } |
160 | ||
6f9f1180 MS |
161 | /* |
162 | * Same as fuse_invalidate_entry_cache(), but also try to remove the | |
163 | * dentry from the hash | |
164 | */ | |
8cbdf1e6 MS |
165 | static void fuse_invalidate_entry(struct dentry *entry) |
166 | { | |
167 | d_invalidate(entry); | |
168 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 MS |
169 | } |
170 | ||
7078187a | 171 | static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args, |
13983d06 | 172 | u64 nodeid, const struct qstr *name, |
e5e5558e MS |
173 | struct fuse_entry_out *outarg) |
174 | { | |
0e9663ee | 175 | memset(outarg, 0, sizeof(struct fuse_entry_out)); |
d5b48543 MS |
176 | args->opcode = FUSE_LOOKUP; |
177 | args->nodeid = nodeid; | |
d3d90cc2 | 178 | args->in_numargs = 3; |
7ccd86ba | 179 | fuse_set_zero_arg0(args); |
d3d90cc2 | 180 | args->in_args[1].size = name->len; |
7ccd86ba | 181 | args->in_args[1].value = name->name; |
d3d90cc2 LT |
182 | args->in_args[2].size = 1; |
183 | args->in_args[2].value = ""; | |
d5b48543 MS |
184 | args->out_numargs = 1; |
185 | args->out_args[0].size = sizeof(struct fuse_entry_out); | |
186 | args->out_args[0].value = outarg; | |
e5e5558e MS |
187 | } |
188 | ||
6f9f1180 MS |
189 | /* |
190 | * Check whether the dentry is still valid | |
191 | * | |
192 | * If the entry validity timeout has expired and the dentry is | |
193 | * positive, try to redo the lookup. If the lookup results in a | |
194 | * different inode, then let the VFS invalidate the dentry and redo | |
195 | * the lookup once more. If the lookup results in the same inode, | |
196 | * then refresh the attributes, timeouts and mark the dentry valid. | |
197 | */ | |
5be1fa8a AV |
198 | static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name, |
199 | struct dentry *entry, unsigned int flags) | |
e5e5558e | 200 | { |
34286d66 | 201 | struct inode *inode; |
fcee216b | 202 | struct fuse_mount *fm; |
6314efee | 203 | struct fuse_inode *fi; |
e2a6b952 | 204 | int ret; |
8cbdf1e6 | 205 | |
2b0143b5 | 206 | inode = d_inode_rcu(entry); |
5d069dbe | 207 | if (inode && fuse_is_bad(inode)) |
e2a6b952 | 208 | goto invalid; |
154210cc | 209 | else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) || |
ccc031e2 | 210 | (flags & (LOOKUP_EXCL | LOOKUP_REVAL | LOOKUP_RENAME_TARGET))) { |
e5e5558e | 211 | struct fuse_entry_out outarg; |
7078187a | 212 | FUSE_ARGS(args); |
07e77dca | 213 | struct fuse_forget_link *forget; |
1fb69e78 | 214 | u64 attr_version; |
8cbdf1e6 | 215 | |
50322fe7 | 216 | /* For negative dentries, always do a fresh lookup */ |
8cbdf1e6 | 217 | if (!inode) |
e2a6b952 | 218 | goto invalid; |
8cbdf1e6 | 219 | |
e2a6b952 | 220 | ret = -ECHILD; |
0b728e19 | 221 | if (flags & LOOKUP_RCU) |
e2a6b952 | 222 | goto out; |
e7c0a167 | 223 | |
fcee216b | 224 | fm = get_fuse_mount(inode); |
e5e5558e | 225 | |
07e77dca | 226 | forget = fuse_alloc_forget(); |
7078187a MS |
227 | ret = -ENOMEM; |
228 | if (!forget) | |
e2a6b952 | 229 | goto out; |
2d51013e | 230 | |
fcee216b | 231 | attr_version = fuse_get_attr_version(fm->fc); |
1fb69e78 | 232 | |
19e1dbdc AV |
233 | fuse_lookup_init(fm->fc, &args, get_node_id(dir), |
234 | name, &outarg); | |
0c679382 | 235 | ret = fuse_simple_request(fm, &args); |
50322fe7 | 236 | /* Zero nodeid is same as -ENOENT */ |
7078187a MS |
237 | if (!ret && !outarg.nodeid) |
238 | ret = -ENOENT; | |
239 | if (!ret) { | |
6314efee | 240 | fi = get_fuse_inode(inode); |
bf109c64 MR |
241 | if (outarg.nodeid != get_node_id(inode) || |
242 | (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) { | |
fcee216b MR |
243 | fuse_queue_forget(fm->fc, forget, |
244 | outarg.nodeid, 1); | |
e2a6b952 | 245 | goto invalid; |
9e6268db | 246 | } |
c9d8f5f0 | 247 | spin_lock(&fi->lock); |
1729a16c | 248 | fi->nlookup++; |
c9d8f5f0 | 249 | spin_unlock(&fi->lock); |
9e6268db | 250 | } |
07e77dca | 251 | kfree(forget); |
a9d1c4c6 | 252 | if (ret == -ENOMEM || ret == -EINTR) |
7078187a | 253 | goto out; |
eb59bd17 | 254 | if (ret || fuse_invalid_attr(&outarg.attr) || |
15db1683 | 255 | fuse_stale_inode(inode, outarg.generation, &outarg.attr)) |
e2a6b952 | 256 | goto invalid; |
e5e5558e | 257 | |
60bcc88a | 258 | forget_all_cached_acls(inode); |
972f4c46 | 259 | fuse_change_attributes(inode, &outarg.attr, NULL, |
9dc10a54 | 260 | ATTR_TIMEOUT(&outarg), |
1fb69e78 MS |
261 | attr_version); |
262 | fuse_change_entry_timeout(entry, &outarg); | |
28420dad | 263 | } else if (inode) { |
6314efee MS |
264 | fi = get_fuse_inode(inode); |
265 | if (flags & LOOKUP_RCU) { | |
266 | if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state)) | |
267 | return -ECHILD; | |
268 | } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) { | |
19e1dbdc | 269 | fuse_advise_use_readdirplus(dir); |
28420dad | 270 | } |
e5e5558e | 271 | } |
e2a6b952 MS |
272 | ret = 1; |
273 | out: | |
274 | return ret; | |
275 | ||
276 | invalid: | |
277 | ret = 0; | |
278 | goto out; | |
e5e5558e MS |
279 | } |
280 | ||
30c6a23d | 281 | #if BITS_PER_LONG < 64 |
f75fdf22 MS |
282 | static int fuse_dentry_init(struct dentry *dentry) |
283 | { | |
dc69e98c KK |
284 | dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry), |
285 | GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE); | |
f75fdf22 MS |
286 | |
287 | return dentry->d_fsdata ? 0 : -ENOMEM; | |
288 | } | |
289 | static void fuse_dentry_release(struct dentry *dentry) | |
290 | { | |
291 | union fuse_dentry *fd = dentry->d_fsdata; | |
292 | ||
293 | kfree_rcu(fd, rcu); | |
294 | } | |
30c6a23d | 295 | #endif |
f75fdf22 | 296 | |
8fab0106 MS |
297 | static int fuse_dentry_delete(const struct dentry *dentry) |
298 | { | |
299 | return time_before64(fuse_dentry_time(dentry), get_jiffies_64()); | |
300 | } | |
301 | ||
bf109c64 MR |
302 | /* |
303 | * Create a fuse_mount object with a new superblock (with path->dentry | |
304 | * as the root), and return that mount so it can be auto-mounted on | |
305 | * @path. | |
306 | */ | |
307 | static struct vfsmount *fuse_dentry_automount(struct path *path) | |
308 | { | |
309 | struct fs_context *fsc; | |
bf109c64 MR |
310 | struct vfsmount *mnt; |
311 | struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry)); | |
bf109c64 MR |
312 | |
313 | fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry); | |
29e0e4df GK |
314 | if (IS_ERR(fsc)) |
315 | return ERR_CAST(fsc); | |
bf109c64 | 316 | |
266eb3f2 GK |
317 | /* Pass the FUSE inode of the mount for fuse_get_tree_submount() */ |
318 | fsc->fs_private = mp_fi; | |
bf109c64 | 319 | |
bf109c64 | 320 | /* Create the submount */ |
29e0e4df GK |
321 | mnt = fc_mount(fsc); |
322 | if (!IS_ERR(mnt)) | |
323 | mntget(mnt); | |
bf109c64 | 324 | |
bf109c64 | 325 | put_fs_context(fsc); |
29e0e4df | 326 | return mnt; |
bf109c64 MR |
327 | } |
328 | ||
4269590a | 329 | const struct dentry_operations fuse_dentry_operations = { |
e5e5558e | 330 | .d_revalidate = fuse_dentry_revalidate, |
8fab0106 | 331 | .d_delete = fuse_dentry_delete, |
30c6a23d | 332 | #if BITS_PER_LONG < 64 |
f75fdf22 MS |
333 | .d_init = fuse_dentry_init, |
334 | .d_release = fuse_dentry_release, | |
30c6a23d | 335 | #endif |
bf109c64 | 336 | .d_automount = fuse_dentry_automount, |
e5e5558e MS |
337 | }; |
338 | ||
0ce267ff | 339 | const struct dentry_operations fuse_root_dentry_operations = { |
30c6a23d | 340 | #if BITS_PER_LONG < 64 |
0ce267ff MS |
341 | .d_init = fuse_dentry_init, |
342 | .d_release = fuse_dentry_release, | |
30c6a23d | 343 | #endif |
0ce267ff MS |
344 | }; |
345 | ||
a5bfffac | 346 | int fuse_valid_type(int m) |
39ee059a MS |
347 | { |
348 | return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || | |
349 | S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); | |
350 | } | |
351 | ||
d3045530 MS |
352 | static bool fuse_valid_size(u64 size) |
353 | { | |
354 | return size <= LLONG_MAX; | |
355 | } | |
356 | ||
eb59bd17 MS |
357 | bool fuse_invalid_attr(struct fuse_attr *attr) |
358 | { | |
d3045530 | 359 | return !fuse_valid_type(attr->mode) || !fuse_valid_size(attr->size); |
eb59bd17 MS |
360 | } |
361 | ||
13983d06 | 362 | int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name, |
c180eebe | 363 | struct fuse_entry_out *outarg, struct inode **inode) |
e5e5558e | 364 | { |
fcee216b | 365 | struct fuse_mount *fm = get_fuse_mount_super(sb); |
7078187a | 366 | FUSE_ARGS(args); |
07e77dca | 367 | struct fuse_forget_link *forget; |
69eb56f6 | 368 | u64 attr_version, evict_ctr; |
c180eebe | 369 | int err; |
e5e5558e | 370 | |
c180eebe MS |
371 | *inode = NULL; |
372 | err = -ENAMETOOLONG; | |
373 | if (name->len > FUSE_NAME_MAX) | |
374 | goto out; | |
e5e5558e | 375 | |
e5e5558e | 376 | |
07e77dca MS |
377 | forget = fuse_alloc_forget(); |
378 | err = -ENOMEM; | |
7078187a | 379 | if (!forget) |
c180eebe | 380 | goto out; |
2d51013e | 381 | |
fcee216b | 382 | attr_version = fuse_get_attr_version(fm->fc); |
69eb56f6 | 383 | evict_ctr = fuse_get_evict_ctr(fm->fc); |
1fb69e78 | 384 | |
fcee216b | 385 | fuse_lookup_init(fm->fc, &args, nodeid, name, outarg); |
0c679382 | 386 | err = fuse_simple_request(fm, &args); |
50322fe7 | 387 | /* Zero nodeid is same as -ENOENT, but with valid timeout */ |
c180eebe MS |
388 | if (err || !outarg->nodeid) |
389 | goto out_put_forget; | |
390 | ||
391 | err = -EIO; | |
eb59bd17 | 392 | if (fuse_invalid_attr(&outarg->attr)) |
c180eebe | 393 | goto out_put_forget; |
68ca1b49 MS |
394 | if (outarg->nodeid == FUSE_ROOT_ID && outarg->generation != 0) { |
395 | pr_warn_once("root generation should be zero\n"); | |
396 | outarg->generation = 0; | |
397 | } | |
c180eebe MS |
398 | |
399 | *inode = fuse_iget(sb, outarg->nodeid, outarg->generation, | |
9dc10a54 | 400 | &outarg->attr, ATTR_TIMEOUT(outarg), |
69eb56f6 | 401 | attr_version, evict_ctr); |
c180eebe MS |
402 | err = -ENOMEM; |
403 | if (!*inode) { | |
fcee216b | 404 | fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1); |
c180eebe | 405 | goto out; |
e5e5558e | 406 | } |
c180eebe MS |
407 | err = 0; |
408 | ||
409 | out_put_forget: | |
07e77dca | 410 | kfree(forget); |
c180eebe MS |
411 | out: |
412 | return err; | |
413 | } | |
414 | ||
415 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, | |
00cd8dd3 | 416 | unsigned int flags) |
c180eebe MS |
417 | { |
418 | int err; | |
419 | struct fuse_entry_out outarg; | |
420 | struct inode *inode; | |
421 | struct dentry *newent; | |
c180eebe | 422 | bool outarg_valid = true; |
63576c13 | 423 | bool locked; |
c180eebe | 424 | |
5d069dbe MS |
425 | if (fuse_is_bad(dir)) |
426 | return ERR_PTR(-EIO); | |
427 | ||
63576c13 | 428 | locked = fuse_lock_inode(dir); |
c180eebe MS |
429 | err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name, |
430 | &outarg, &inode); | |
63576c13 | 431 | fuse_unlock_inode(dir, locked); |
c180eebe MS |
432 | if (err == -ENOENT) { |
433 | outarg_valid = false; | |
434 | err = 0; | |
435 | } | |
436 | if (err) | |
437 | goto out_err; | |
438 | ||
439 | err = -EIO; | |
440 | if (inode && get_node_id(inode) == FUSE_ROOT_ID) | |
441 | goto out_iput; | |
e5e5558e | 442 | |
41d28bca | 443 | newent = d_splice_alias(inode, entry); |
5835f339 MS |
444 | err = PTR_ERR(newent); |
445 | if (IS_ERR(newent)) | |
446 | goto out_err; | |
d2a85164 | 447 | |
0de6256d | 448 | entry = newent ? newent : entry; |
c180eebe | 449 | if (outarg_valid) |
1fb69e78 | 450 | fuse_change_entry_timeout(entry, &outarg); |
8cbdf1e6 MS |
451 | else |
452 | fuse_invalidate_entry_cache(entry); | |
c180eebe | 453 | |
6c26f717 MS |
454 | if (inode) |
455 | fuse_advise_use_readdirplus(dir); | |
0de6256d | 456 | return newent; |
c180eebe MS |
457 | |
458 | out_iput: | |
459 | iput(inode); | |
460 | out_err: | |
461 | return ERR_PTR(err); | |
e5e5558e MS |
462 | } |
463 | ||
3e2b6fdb | 464 | static int get_security_context(struct dentry *entry, umode_t mode, |
15d937d7 | 465 | struct fuse_in_arg *ext) |
3e2b6fdb VG |
466 | { |
467 | struct fuse_secctx *fctx; | |
468 | struct fuse_secctx_header *header; | |
b530104f CS |
469 | struct lsm_context lsmctx = { }; |
470 | void *ptr; | |
471 | u32 total_len = sizeof(*header); | |
3e2b6fdb | 472 | int err, nr_ctx = 0; |
b530104f | 473 | const char *name = NULL; |
3e2b6fdb VG |
474 | size_t namelen; |
475 | ||
476 | err = security_dentry_init_security(entry, mode, &entry->d_name, | |
b530104f CS |
477 | &name, &lsmctx); |
478 | ||
479 | /* If no LSM is supporting this security hook ignore error */ | |
480 | if (err && err != -EOPNOTSUPP) | |
481 | goto out_err; | |
3e2b6fdb | 482 | |
b530104f | 483 | if (lsmctx.len) { |
3e2b6fdb VG |
484 | nr_ctx = 1; |
485 | namelen = strlen(name) + 1; | |
486 | err = -EIO; | |
b530104f CS |
487 | if (WARN_ON(namelen > XATTR_NAME_MAX + 1 || |
488 | lsmctx.len > S32_MAX)) | |
3e2b6fdb | 489 | goto out_err; |
b530104f CS |
490 | total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen + |
491 | lsmctx.len); | |
3e2b6fdb VG |
492 | } |
493 | ||
494 | err = -ENOMEM; | |
495 | header = ptr = kzalloc(total_len, GFP_KERNEL); | |
496 | if (!ptr) | |
497 | goto out_err; | |
498 | ||
499 | header->nr_secctx = nr_ctx; | |
500 | header->size = total_len; | |
501 | ptr += sizeof(*header); | |
502 | if (nr_ctx) { | |
503 | fctx = ptr; | |
b530104f | 504 | fctx->size = lsmctx.len; |
3e2b6fdb VG |
505 | ptr += sizeof(*fctx); |
506 | ||
507 | strcpy(ptr, name); | |
508 | ptr += namelen; | |
509 | ||
b530104f | 510 | memcpy(ptr, lsmctx.context, lsmctx.len); |
3e2b6fdb | 511 | } |
15d937d7 MS |
512 | ext->size = total_len; |
513 | ext->value = header; | |
3e2b6fdb VG |
514 | err = 0; |
515 | out_err: | |
b530104f CS |
516 | if (nr_ctx) |
517 | security_release_secctx(&lsmctx); | |
3e2b6fdb VG |
518 | return err; |
519 | } | |
520 | ||
8ed7cb3f MS |
521 | static void *extend_arg(struct fuse_in_arg *buf, u32 bytes) |
522 | { | |
523 | void *p; | |
524 | u32 newlen = buf->size + bytes; | |
525 | ||
526 | p = krealloc(buf->value, newlen, GFP_KERNEL); | |
527 | if (!p) { | |
528 | kfree(buf->value); | |
529 | buf->size = 0; | |
530 | buf->value = NULL; | |
531 | return NULL; | |
532 | } | |
533 | ||
534 | memset(p + buf->size, 0, bytes); | |
535 | buf->value = p; | |
536 | buf->size = newlen; | |
537 | ||
538 | return p + newlen - bytes; | |
539 | } | |
540 | ||
541 | static u32 fuse_ext_size(size_t size) | |
542 | { | |
543 | return FUSE_REC_ALIGN(sizeof(struct fuse_ext_header) + size); | |
544 | } | |
545 | ||
546 | /* | |
547 | * This adds just a single supplementary group that matches the parent's group. | |
548 | */ | |
d561254f AM |
549 | static int get_create_supp_group(struct mnt_idmap *idmap, |
550 | struct inode *dir, | |
551 | struct fuse_in_arg *ext) | |
8ed7cb3f MS |
552 | { |
553 | struct fuse_conn *fc = get_fuse_conn(dir); | |
554 | struct fuse_ext_header *xh; | |
555 | struct fuse_supp_groups *sg; | |
556 | kgid_t kgid = dir->i_gid; | |
d561254f | 557 | vfsgid_t vfsgid = make_vfsgid(idmap, fc->user_ns, kgid); |
8ed7cb3f | 558 | gid_t parent_gid = from_kgid(fc->user_ns, kgid); |
d561254f | 559 | |
8ed7cb3f MS |
560 | u32 sg_len = fuse_ext_size(sizeof(*sg) + sizeof(sg->groups[0])); |
561 | ||
d561254f AM |
562 | if (parent_gid == (gid_t) -1 || vfsgid_eq_kgid(vfsgid, current_fsgid()) || |
563 | !vfsgid_in_group_p(vfsgid)) | |
8ed7cb3f MS |
564 | return 0; |
565 | ||
566 | xh = extend_arg(ext, sg_len); | |
567 | if (!xh) | |
568 | return -ENOMEM; | |
569 | ||
570 | xh->size = sg_len; | |
571 | xh->type = FUSE_EXT_GROUPS; | |
572 | ||
573 | sg = (struct fuse_supp_groups *) &xh[1]; | |
574 | sg->nr_groups = 1; | |
575 | sg->groups[0] = parent_gid; | |
576 | ||
577 | return 0; | |
578 | } | |
579 | ||
d561254f AM |
580 | static int get_create_ext(struct mnt_idmap *idmap, |
581 | struct fuse_args *args, | |
8ed7cb3f | 582 | struct inode *dir, struct dentry *dentry, |
15d937d7 MS |
583 | umode_t mode) |
584 | { | |
585 | struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb); | |
586 | struct fuse_in_arg ext = { .size = 0, .value = NULL }; | |
587 | int err = 0; | |
588 | ||
589 | if (fc->init_security) | |
590 | err = get_security_context(dentry, mode, &ext); | |
8ed7cb3f | 591 | if (!err && fc->create_supp_group) |
d561254f | 592 | err = get_create_supp_group(idmap, dir, &ext); |
15d937d7 MS |
593 | |
594 | if (!err && ext.size) { | |
595 | WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args)); | |
596 | args->is_ext = true; | |
597 | args->ext_idx = args->in_numargs++; | |
598 | args->in_args[args->ext_idx] = ext; | |
599 | } else { | |
600 | kfree(ext.value); | |
601 | } | |
602 | ||
603 | return err; | |
604 | } | |
605 | ||
606 | static void free_ext_value(struct fuse_args *args) | |
607 | { | |
608 | if (args->is_ext) | |
609 | kfree(args->in_args[args->ext_idx].value); | |
610 | } | |
611 | ||
6f9f1180 MS |
612 | /* |
613 | * Atomic create+open operation | |
614 | * | |
615 | * If the filesystem doesn't support this, then fall back to separate | |
616 | * 'mknod' + 'open' requests. | |
617 | */ | |
556208e1 AM |
618 | static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, |
619 | struct dentry *entry, struct file *file, | |
620 | unsigned int flags, umode_t mode, u32 opcode) | |
fd72faac MS |
621 | { |
622 | int err; | |
623 | struct inode *inode; | |
fcee216b | 624 | struct fuse_mount *fm = get_fuse_mount(dir); |
7078187a | 625 | FUSE_ARGS(args); |
07e77dca | 626 | struct fuse_forget_link *forget; |
e0a43ddc | 627 | struct fuse_create_in inarg; |
fc8ff397 | 628 | struct fuse_open_out *outopenp; |
fd72faac | 629 | struct fuse_entry_out outentry; |
ebf84d0c | 630 | struct fuse_inode *fi; |
fd72faac | 631 | struct fuse_file *ff; |
2fdbb8dd | 632 | bool trunc = flags & O_TRUNC; |
fd72faac | 633 | |
af109bca MS |
634 | /* Userspace expects S_IFREG in create mode */ |
635 | BUG_ON((mode & S_IFMT) != S_IFREG); | |
636 | ||
07e77dca | 637 | forget = fuse_alloc_forget(); |
c8ccbe03 | 638 | err = -ENOMEM; |
07e77dca | 639 | if (!forget) |
c8ccbe03 | 640 | goto out_err; |
51eb01e7 | 641 | |
ce1d5a49 | 642 | err = -ENOMEM; |
e26ee4ef | 643 | ff = fuse_file_alloc(fm, true); |
fd72faac | 644 | if (!ff) |
7078187a | 645 | goto out_put_forget_req; |
fd72faac | 646 | |
fcee216b | 647 | if (!fm->fc->dont_mask) |
e0a43ddc MS |
648 | mode &= ~current_umask(); |
649 | ||
fd72faac MS |
650 | flags &= ~O_NOCTTY; |
651 | memset(&inarg, 0, sizeof(inarg)); | |
0e9663ee | 652 | memset(&outentry, 0, sizeof(outentry)); |
fd72faac MS |
653 | inarg.flags = flags; |
654 | inarg.mode = mode; | |
e0a43ddc | 655 | inarg.umask = current_umask(); |
643a666a | 656 | |
2fdbb8dd | 657 | if (fm->fc->handle_killpriv_v2 && trunc && |
643a666a VG |
658 | !(flags & O_EXCL) && !capable(CAP_FSETID)) { |
659 | inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID; | |
660 | } | |
661 | ||
7d375390 | 662 | args.opcode = opcode; |
d5b48543 MS |
663 | args.nodeid = get_node_id(dir); |
664 | args.in_numargs = 2; | |
665 | args.in_args[0].size = sizeof(inarg); | |
666 | args.in_args[0].value = &inarg; | |
667 | args.in_args[1].size = entry->d_name.len + 1; | |
668 | args.in_args[1].value = entry->d_name.name; | |
669 | args.out_numargs = 2; | |
670 | args.out_args[0].size = sizeof(outentry); | |
671 | args.out_args[0].value = &outentry; | |
fc8ff397 AG |
672 | /* Store outarg for fuse_finish_open() */ |
673 | outopenp = &ff->args->open_outarg; | |
674 | args.out_args[1].size = sizeof(*outopenp); | |
675 | args.out_args[1].value = outopenp; | |
3e2b6fdb | 676 | |
556208e1 | 677 | err = get_create_ext(idmap, &args, dir, entry, mode); |
15d937d7 | 678 | if (err) |
3002240d | 679 | goto out_free_ff; |
3e2b6fdb | 680 | |
0c679382 | 681 | err = fuse_simple_idmap_request(idmap, fm, &args); |
15d937d7 | 682 | free_ext_value(&args); |
c8ccbe03 | 683 | if (err) |
fd72faac | 684 | goto out_free_ff; |
fd72faac MS |
685 | |
686 | err = -EIO; | |
eb59bd17 MS |
687 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) || |
688 | fuse_invalid_attr(&outentry.attr)) | |
fd72faac MS |
689 | goto out_free_ff; |
690 | ||
fc8ff397 | 691 | ff->fh = outopenp->fh; |
c7b7143c | 692 | ff->nodeid = outentry.nodeid; |
fc8ff397 | 693 | ff->open_flags = outopenp->open_flags; |
fd72faac | 694 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, |
69eb56f6 | 695 | &outentry.attr, ATTR_TIMEOUT(&outentry), 0, 0); |
fd72faac MS |
696 | if (!inode) { |
697 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); | |
ebf84d0c | 698 | fuse_sync_release(NULL, ff, flags); |
fcee216b | 699 | fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1); |
c8ccbe03 MS |
700 | err = -ENOMEM; |
701 | goto out_err; | |
fd72faac | 702 | } |
07e77dca | 703 | kfree(forget); |
fd72faac | 704 | d_instantiate(entry, inode); |
1fb69e78 | 705 | fuse_change_entry_timeout(entry, &outentry); |
261aaba7 | 706 | fuse_dir_changed(dir); |
d2c487f1 AG |
707 | err = generic_file_open(inode, file); |
708 | if (!err) { | |
709 | file->private_data = ff; | |
710 | err = finish_open(file, entry, fuse_finish_open); | |
711 | } | |
30d90494 | 712 | if (err) { |
ebf84d0c KT |
713 | fi = get_fuse_inode(inode); |
714 | fuse_sync_release(fi, ff, flags); | |
c8ccbe03 | 715 | } else { |
2fdbb8dd MS |
716 | if (fm->fc->atomic_o_trunc && trunc) |
717 | truncate_pagecache(inode, 0); | |
718 | else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) | |
719 | invalidate_inode_pages2(inode->i_mapping); | |
fd72faac | 720 | } |
d9585277 | 721 | return err; |
fd72faac | 722 | |
c8ccbe03 | 723 | out_free_ff: |
fd72faac | 724 | fuse_file_free(ff); |
c8ccbe03 | 725 | out_put_forget_req: |
07e77dca | 726 | kfree(forget); |
c8ccbe03 | 727 | out_err: |
d9585277 | 728 | return err; |
c8ccbe03 MS |
729 | } |
730 | ||
5ebb29be | 731 | static int fuse_mknod(struct mnt_idmap *, struct inode *, struct dentry *, |
549c7297 | 732 | umode_t, dev_t); |
d9585277 | 733 | static int fuse_atomic_open(struct inode *dir, struct dentry *entry, |
30d90494 | 734 | struct file *file, unsigned flags, |
44907d79 | 735 | umode_t mode) |
c8ccbe03 MS |
736 | { |
737 | int err; | |
556208e1 | 738 | struct mnt_idmap *idmap = file_mnt_idmap(file); |
c8ccbe03 | 739 | struct fuse_conn *fc = get_fuse_conn(dir); |
c8ccbe03 MS |
740 | struct dentry *res = NULL; |
741 | ||
5d069dbe MS |
742 | if (fuse_is_bad(dir)) |
743 | return -EIO; | |
744 | ||
00699ad8 | 745 | if (d_in_lookup(entry)) { |
00cd8dd3 | 746 | res = fuse_lookup(dir, entry, 0); |
c8ccbe03 | 747 | if (IS_ERR(res)) |
d9585277 | 748 | return PTR_ERR(res); |
c8ccbe03 MS |
749 | |
750 | if (res) | |
751 | entry = res; | |
752 | } | |
753 | ||
2b0143b5 | 754 | if (!(flags & O_CREAT) || d_really_is_positive(entry)) |
c8ccbe03 MS |
755 | goto no_open; |
756 | ||
757 | /* Only creates */ | |
73a09dd9 | 758 | file->f_mode |= FMODE_CREATED; |
c8ccbe03 MS |
759 | |
760 | if (fc->no_create) | |
761 | goto mknod; | |
762 | ||
556208e1 | 763 | err = fuse_create_open(idmap, dir, entry, file, flags, mode, FUSE_CREATE); |
d9585277 | 764 | if (err == -ENOSYS) { |
c8ccbe03 MS |
765 | fc->no_create = 1; |
766 | goto mknod; | |
7d875e66 JZ |
767 | } else if (err == -EEXIST) |
768 | fuse_invalidate_entry(entry); | |
c8ccbe03 MS |
769 | out_dput: |
770 | dput(res); | |
d9585277 | 771 | return err; |
c8ccbe03 MS |
772 | |
773 | mknod: | |
556208e1 | 774 | err = fuse_mknod(idmap, dir, entry, mode, 0); |
d9585277 | 775 | if (err) |
c8ccbe03 | 776 | goto out_dput; |
c8ccbe03 | 777 | no_open: |
e45198a6 | 778 | return finish_no_open(file, res); |
fd72faac MS |
779 | } |
780 | ||
6f9f1180 MS |
781 | /* |
782 | * Code shared between mknod, mkdir, symlink and link | |
783 | */ | |
556208e1 AM |
784 | static int create_new_entry(struct mnt_idmap *idmap, struct fuse_mount *fm, |
785 | struct fuse_args *args, struct inode *dir, | |
786 | struct dentry *entry, umode_t mode) | |
9e6268db MS |
787 | { |
788 | struct fuse_entry_out outarg; | |
789 | struct inode *inode; | |
c971e6a0 | 790 | struct dentry *d; |
9e6268db | 791 | int err; |
07e77dca | 792 | struct fuse_forget_link *forget; |
2d51013e | 793 | |
5d069dbe MS |
794 | if (fuse_is_bad(dir)) |
795 | return -EIO; | |
796 | ||
07e77dca | 797 | forget = fuse_alloc_forget(); |
7078187a | 798 | if (!forget) |
07e77dca | 799 | return -ENOMEM; |
9e6268db | 800 | |
0e9663ee | 801 | memset(&outarg, 0, sizeof(outarg)); |
d5b48543 MS |
802 | args->nodeid = get_node_id(dir); |
803 | args->out_numargs = 1; | |
804 | args->out_args[0].size = sizeof(outarg); | |
805 | args->out_args[0].value = &outarg; | |
3e2b6fdb | 806 | |
15d937d7 | 807 | if (args->opcode != FUSE_LINK) { |
556208e1 | 808 | err = get_create_ext(idmap, args, dir, entry, mode); |
3e2b6fdb VG |
809 | if (err) |
810 | goto out_put_forget_req; | |
3e2b6fdb VG |
811 | } |
812 | ||
0c679382 | 813 | err = fuse_simple_idmap_request(idmap, fm, args); |
15d937d7 | 814 | free_ext_value(args); |
2d51013e MS |
815 | if (err) |
816 | goto out_put_forget_req; | |
817 | ||
39ee059a | 818 | err = -EIO; |
eb59bd17 | 819 | if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr)) |
2d51013e | 820 | goto out_put_forget_req; |
39ee059a MS |
821 | |
822 | if ((outarg.attr.mode ^ mode) & S_IFMT) | |
2d51013e | 823 | goto out_put_forget_req; |
39ee059a | 824 | |
9e6268db | 825 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
69eb56f6 | 826 | &outarg.attr, ATTR_TIMEOUT(&outarg), 0, 0); |
9e6268db | 827 | if (!inode) { |
fcee216b | 828 | fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1); |
9e6268db MS |
829 | return -ENOMEM; |
830 | } | |
07e77dca | 831 | kfree(forget); |
9e6268db | 832 | |
c971e6a0 AV |
833 | d_drop(entry); |
834 | d = d_splice_alias(inode, entry); | |
835 | if (IS_ERR(d)) | |
836 | return PTR_ERR(d); | |
9e6268db | 837 | |
c971e6a0 AV |
838 | if (d) { |
839 | fuse_change_entry_timeout(d, &outarg); | |
840 | dput(d); | |
841 | } else { | |
842 | fuse_change_entry_timeout(entry, &outarg); | |
843 | } | |
261aaba7 | 844 | fuse_dir_changed(dir); |
9e6268db | 845 | return 0; |
39ee059a | 846 | |
2d51013e | 847 | out_put_forget_req: |
7d875e66 JZ |
848 | if (err == -EEXIST) |
849 | fuse_invalidate_entry(entry); | |
07e77dca | 850 | kfree(forget); |
39ee059a | 851 | return err; |
9e6268db MS |
852 | } |
853 | ||
5ebb29be | 854 | static int fuse_mknod(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 855 | struct dentry *entry, umode_t mode, dev_t rdev) |
9e6268db MS |
856 | { |
857 | struct fuse_mknod_in inarg; | |
fcee216b | 858 | struct fuse_mount *fm = get_fuse_mount(dir); |
7078187a | 859 | FUSE_ARGS(args); |
9e6268db | 860 | |
fcee216b | 861 | if (!fm->fc->dont_mask) |
e0a43ddc MS |
862 | mode &= ~current_umask(); |
863 | ||
9e6268db MS |
864 | memset(&inarg, 0, sizeof(inarg)); |
865 | inarg.mode = mode; | |
866 | inarg.rdev = new_encode_dev(rdev); | |
e0a43ddc | 867 | inarg.umask = current_umask(); |
d5b48543 MS |
868 | args.opcode = FUSE_MKNOD; |
869 | args.in_numargs = 2; | |
870 | args.in_args[0].size = sizeof(inarg); | |
871 | args.in_args[0].value = &inarg; | |
872 | args.in_args[1].size = entry->d_name.len + 1; | |
873 | args.in_args[1].value = entry->d_name.name; | |
556208e1 | 874 | return create_new_entry(idmap, fm, &args, dir, entry, mode); |
9e6268db MS |
875 | } |
876 | ||
6c960e68 | 877 | static int fuse_create(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 878 | struct dentry *entry, umode_t mode, bool excl) |
9e6268db | 879 | { |
556208e1 | 880 | return fuse_mknod(idmap, dir, entry, mode, 0); |
9e6268db MS |
881 | } |
882 | ||
011e2b71 | 883 | static int fuse_tmpfile(struct mnt_idmap *idmap, struct inode *dir, |
7d375390 MS |
884 | struct file *file, umode_t mode) |
885 | { | |
886 | struct fuse_conn *fc = get_fuse_conn(dir); | |
887 | int err; | |
888 | ||
889 | if (fc->no_tmpfile) | |
890 | return -EOPNOTSUPP; | |
891 | ||
556208e1 AM |
892 | err = fuse_create_open(idmap, dir, file->f_path.dentry, file, |
893 | file->f_flags, mode, FUSE_TMPFILE); | |
7d375390 MS |
894 | if (err == -ENOSYS) { |
895 | fc->no_tmpfile = 1; | |
896 | err = -EOPNOTSUPP; | |
897 | } | |
898 | return err; | |
899 | } | |
900 | ||
c54bd91e | 901 | static int fuse_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 902 | struct dentry *entry, umode_t mode) |
9e6268db MS |
903 | { |
904 | struct fuse_mkdir_in inarg; | |
fcee216b | 905 | struct fuse_mount *fm = get_fuse_mount(dir); |
7078187a | 906 | FUSE_ARGS(args); |
9e6268db | 907 | |
fcee216b | 908 | if (!fm->fc->dont_mask) |
e0a43ddc MS |
909 | mode &= ~current_umask(); |
910 | ||
9e6268db MS |
911 | memset(&inarg, 0, sizeof(inarg)); |
912 | inarg.mode = mode; | |
e0a43ddc | 913 | inarg.umask = current_umask(); |
d5b48543 MS |
914 | args.opcode = FUSE_MKDIR; |
915 | args.in_numargs = 2; | |
916 | args.in_args[0].size = sizeof(inarg); | |
917 | args.in_args[0].value = &inarg; | |
918 | args.in_args[1].size = entry->d_name.len + 1; | |
919 | args.in_args[1].value = entry->d_name.name; | |
556208e1 | 920 | return create_new_entry(idmap, fm, &args, dir, entry, S_IFDIR); |
9e6268db MS |
921 | } |
922 | ||
7a77db95 | 923 | static int fuse_symlink(struct mnt_idmap *idmap, struct inode *dir, |
549c7297 | 924 | struct dentry *entry, const char *link) |
9e6268db | 925 | { |
fcee216b | 926 | struct fuse_mount *fm = get_fuse_mount(dir); |
9e6268db | 927 | unsigned len = strlen(link) + 1; |
7078187a | 928 | FUSE_ARGS(args); |
9e6268db | 929 | |
d5b48543 | 930 | args.opcode = FUSE_SYMLINK; |
7ccd86ba BS |
931 | args.in_numargs = 3; |
932 | fuse_set_zero_arg0(&args); | |
933 | args.in_args[1].size = entry->d_name.len + 1; | |
934 | args.in_args[1].value = entry->d_name.name; | |
935 | args.in_args[2].size = len; | |
936 | args.in_args[2].value = link; | |
556208e1 | 937 | return create_new_entry(idmap, fm, &args, dir, entry, S_IFLNK); |
9e6268db MS |
938 | } |
939 | ||
5c791fe1 MS |
940 | void fuse_flush_time_update(struct inode *inode) |
941 | { | |
942 | int err = sync_inode_metadata(inode, 1); | |
943 | ||
944 | mapping_set_error(inode->i_mapping, err); | |
945 | } | |
946 | ||
97f044f6 | 947 | static void fuse_update_ctime_in_cache(struct inode *inode) |
31f3267b MP |
948 | { |
949 | if (!IS_NOCMTIME(inode)) { | |
ceb2d5e9 | 950 | inode_set_ctime_current(inode); |
31f3267b | 951 | mark_inode_dirty_sync(inode); |
5c791fe1 | 952 | fuse_flush_time_update(inode); |
31f3267b MP |
953 | } |
954 | } | |
955 | ||
97f044f6 MS |
956 | void fuse_update_ctime(struct inode *inode) |
957 | { | |
fa5eee57 | 958 | fuse_invalidate_attr_mask(inode, STATX_CTIME); |
97f044f6 MS |
959 | fuse_update_ctime_in_cache(inode); |
960 | } | |
961 | ||
cefd1b83 MS |
962 | static void fuse_entry_unlinked(struct dentry *entry) |
963 | { | |
964 | struct inode *inode = d_inode(entry); | |
965 | struct fuse_conn *fc = get_fuse_conn(inode); | |
966 | struct fuse_inode *fi = get_fuse_inode(inode); | |
967 | ||
968 | spin_lock(&fi->lock); | |
969 | fi->attr_version = atomic64_inc_return(&fc->attr_version); | |
970 | /* | |
971 | * If i_nlink == 0 then unlink doesn't make sense, yet this can | |
972 | * happen if userspace filesystem is careless. It would be | |
973 | * difficult to enforce correct nlink usage so just ignore this | |
974 | * condition here | |
975 | */ | |
976 | if (S_ISDIR(inode->i_mode)) | |
977 | clear_nlink(inode); | |
978 | else if (inode->i_nlink > 0) | |
979 | drop_nlink(inode); | |
980 | spin_unlock(&fi->lock); | |
981 | fuse_invalidate_entry_cache(entry); | |
982 | fuse_update_ctime(inode); | |
983 | } | |
984 | ||
9e6268db MS |
985 | static int fuse_unlink(struct inode *dir, struct dentry *entry) |
986 | { | |
987 | int err; | |
fcee216b | 988 | struct fuse_mount *fm = get_fuse_mount(dir); |
7078187a MS |
989 | FUSE_ARGS(args); |
990 | ||
5d069dbe MS |
991 | if (fuse_is_bad(dir)) |
992 | return -EIO; | |
993 | ||
d5b48543 MS |
994 | args.opcode = FUSE_UNLINK; |
995 | args.nodeid = get_node_id(dir); | |
7ccd86ba BS |
996 | args.in_numargs = 2; |
997 | fuse_set_zero_arg0(&args); | |
998 | args.in_args[1].size = entry->d_name.len + 1; | |
999 | args.in_args[1].value = entry->d_name.name; | |
0c679382 | 1000 | err = fuse_simple_request(fm, &args); |
9e6268db | 1001 | if (!err) { |
261aaba7 | 1002 | fuse_dir_changed(dir); |
cefd1b83 | 1003 | fuse_entry_unlinked(entry); |
7d875e66 | 1004 | } else if (err == -EINTR || err == -ENOENT) |
9e6268db MS |
1005 | fuse_invalidate_entry(entry); |
1006 | return err; | |
1007 | } | |
1008 | ||
1009 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) | |
1010 | { | |
1011 | int err; | |
fcee216b | 1012 | struct fuse_mount *fm = get_fuse_mount(dir); |
7078187a MS |
1013 | FUSE_ARGS(args); |
1014 | ||
5d069dbe MS |
1015 | if (fuse_is_bad(dir)) |
1016 | return -EIO; | |
1017 | ||
d5b48543 MS |
1018 | args.opcode = FUSE_RMDIR; |
1019 | args.nodeid = get_node_id(dir); | |
7ccd86ba BS |
1020 | args.in_numargs = 2; |
1021 | fuse_set_zero_arg0(&args); | |
1022 | args.in_args[1].size = entry->d_name.len + 1; | |
1023 | args.in_args[1].value = entry->d_name.name; | |
0c679382 | 1024 | err = fuse_simple_request(fm, &args); |
9e6268db | 1025 | if (!err) { |
261aaba7 | 1026 | fuse_dir_changed(dir); |
cefd1b83 | 1027 | fuse_entry_unlinked(entry); |
7d875e66 | 1028 | } else if (err == -EINTR || err == -ENOENT) |
9e6268db MS |
1029 | fuse_invalidate_entry(entry); |
1030 | return err; | |
1031 | } | |
1032 | ||
4be75ffe | 1033 | static int fuse_rename_common(struct mnt_idmap *idmap, struct inode *olddir, struct dentry *oldent, |
1560c974 MS |
1034 | struct inode *newdir, struct dentry *newent, |
1035 | unsigned int flags, int opcode, size_t argsize) | |
9e6268db MS |
1036 | { |
1037 | int err; | |
1560c974 | 1038 | struct fuse_rename2_in inarg; |
fcee216b | 1039 | struct fuse_mount *fm = get_fuse_mount(olddir); |
7078187a | 1040 | FUSE_ARGS(args); |
9e6268db | 1041 | |
1560c974 | 1042 | memset(&inarg, 0, argsize); |
9e6268db | 1043 | inarg.newdir = get_node_id(newdir); |
1560c974 | 1044 | inarg.flags = flags; |
d5b48543 MS |
1045 | args.opcode = opcode; |
1046 | args.nodeid = get_node_id(olddir); | |
1047 | args.in_numargs = 3; | |
1048 | args.in_args[0].size = argsize; | |
1049 | args.in_args[0].value = &inarg; | |
1050 | args.in_args[1].size = oldent->d_name.len + 1; | |
1051 | args.in_args[1].value = oldent->d_name.name; | |
1052 | args.in_args[2].size = newent->d_name.len + 1; | |
1053 | args.in_args[2].value = newent->d_name.name; | |
0c679382 | 1054 | err = fuse_simple_idmap_request(idmap, fm, &args); |
9e6268db | 1055 | if (!err) { |
08b63307 | 1056 | /* ctime changes */ |
2b0143b5 | 1057 | fuse_update_ctime(d_inode(oldent)); |
08b63307 | 1058 | |
371e8fd0 | 1059 | if (flags & RENAME_EXCHANGE) |
2b0143b5 | 1060 | fuse_update_ctime(d_inode(newent)); |
1560c974 | 1061 | |
261aaba7 | 1062 | fuse_dir_changed(olddir); |
9e6268db | 1063 | if (olddir != newdir) |
261aaba7 | 1064 | fuse_dir_changed(newdir); |
8cbdf1e6 MS |
1065 | |
1066 | /* newent will end up negative */ | |
cefd1b83 MS |
1067 | if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) |
1068 | fuse_entry_unlinked(newent); | |
7d875e66 | 1069 | } else if (err == -EINTR || err == -ENOENT) { |
9e6268db MS |
1070 | /* If request was interrupted, DEITY only knows if the |
1071 | rename actually took place. If the invalidation | |
1072 | fails (e.g. some process has CWD under the renamed | |
1073 | directory), then there can be inconsistency between | |
1074 | the dcache and the real filesystem. Tough luck. */ | |
1075 | fuse_invalidate_entry(oldent); | |
2b0143b5 | 1076 | if (d_really_is_positive(newent)) |
9e6268db MS |
1077 | fuse_invalidate_entry(newent); |
1078 | } | |
1079 | ||
1080 | return err; | |
1081 | } | |
1082 | ||
e18275ae | 1083 | static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir, |
549c7297 CB |
1084 | struct dentry *oldent, struct inode *newdir, |
1085 | struct dentry *newent, unsigned int flags) | |
1560c974 MS |
1086 | { |
1087 | struct fuse_conn *fc = get_fuse_conn(olddir); | |
1088 | int err; | |
1089 | ||
5d069dbe MS |
1090 | if (fuse_is_bad(olddir)) |
1091 | return -EIO; | |
1092 | ||
519525fa | 1093 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
1560c974 MS |
1094 | return -EINVAL; |
1095 | ||
4237ba43 MS |
1096 | if (flags) { |
1097 | if (fc->no_rename2 || fc->minor < 23) | |
1098 | return -EINVAL; | |
1560c974 | 1099 | |
106e4593 | 1100 | err = fuse_rename_common((flags & RENAME_WHITEOUT) ? idmap : &invalid_mnt_idmap, |
4be75ffe | 1101 | olddir, oldent, newdir, newent, flags, |
4237ba43 MS |
1102 | FUSE_RENAME2, |
1103 | sizeof(struct fuse_rename2_in)); | |
1104 | if (err == -ENOSYS) { | |
1105 | fc->no_rename2 = 1; | |
1106 | err = -EINVAL; | |
1107 | } | |
1108 | } else { | |
106e4593 | 1109 | err = fuse_rename_common(&invalid_mnt_idmap, olddir, oldent, newdir, newent, 0, |
4237ba43 MS |
1110 | FUSE_RENAME, |
1111 | sizeof(struct fuse_rename_in)); | |
1560c974 | 1112 | } |
4237ba43 | 1113 | |
1560c974 | 1114 | return err; |
4237ba43 | 1115 | } |
1560c974 | 1116 | |
9e6268db MS |
1117 | static int fuse_link(struct dentry *entry, struct inode *newdir, |
1118 | struct dentry *newent) | |
1119 | { | |
1120 | int err; | |
1121 | struct fuse_link_in inarg; | |
2b0143b5 | 1122 | struct inode *inode = d_inode(entry); |
fcee216b | 1123 | struct fuse_mount *fm = get_fuse_mount(inode); |
7078187a | 1124 | FUSE_ARGS(args); |
9e6268db MS |
1125 | |
1126 | memset(&inarg, 0, sizeof(inarg)); | |
1127 | inarg.oldnodeid = get_node_id(inode); | |
d5b48543 MS |
1128 | args.opcode = FUSE_LINK; |
1129 | args.in_numargs = 2; | |
1130 | args.in_args[0].size = sizeof(inarg); | |
1131 | args.in_args[0].value = &inarg; | |
1132 | args.in_args[1].size = newent->d_name.len + 1; | |
1133 | args.in_args[1].value = newent->d_name.name; | |
106e4593 | 1134 | err = create_new_entry(&invalid_mnt_idmap, fm, &args, newdir, newent, inode->i_mode); |
97f044f6 MS |
1135 | if (!err) |
1136 | fuse_update_ctime_in_cache(inode); | |
1137 | else if (err == -EINTR) | |
ac45d613 | 1138 | fuse_invalidate_attr(inode); |
97f044f6 | 1139 | |
9e6268db MS |
1140 | return err; |
1141 | } | |
1142 | ||
2a8c810d AM |
1143 | static void fuse_fillattr(struct mnt_idmap *idmap, struct inode *inode, |
1144 | struct fuse_attr *attr, struct kstat *stat) | |
1fb69e78 | 1145 | { |
203627bb | 1146 | unsigned int blkbits; |
8373200b | 1147 | struct fuse_conn *fc = get_fuse_conn(inode); |
2a8c810d AM |
1148 | vfsuid_t vfsuid = make_vfsuid(idmap, fc->user_ns, |
1149 | make_kuid(fc->user_ns, attr->uid)); | |
1150 | vfsgid_t vfsgid = make_vfsgid(idmap, fc->user_ns, | |
1151 | make_kgid(fc->user_ns, attr->gid)); | |
8373200b | 1152 | |
1fb69e78 MS |
1153 | stat->dev = inode->i_sb->s_dev; |
1154 | stat->ino = attr->ino; | |
1155 | stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); | |
1156 | stat->nlink = attr->nlink; | |
2a8c810d AM |
1157 | stat->uid = vfsuid_into_kuid(vfsuid); |
1158 | stat->gid = vfsgid_into_kgid(vfsgid); | |
1fb69e78 MS |
1159 | stat->rdev = inode->i_rdev; |
1160 | stat->atime.tv_sec = attr->atime; | |
1161 | stat->atime.tv_nsec = attr->atimensec; | |
1162 | stat->mtime.tv_sec = attr->mtime; | |
1163 | stat->mtime.tv_nsec = attr->mtimensec; | |
1164 | stat->ctime.tv_sec = attr->ctime; | |
1165 | stat->ctime.tv_nsec = attr->ctimensec; | |
1166 | stat->size = attr->size; | |
1167 | stat->blocks = attr->blocks; | |
203627bb MS |
1168 | |
1169 | if (attr->blksize != 0) | |
1170 | blkbits = ilog2(attr->blksize); | |
1171 | else | |
1172 | blkbits = inode->i_sb->s_blocksize_bits; | |
1173 | ||
1174 | stat->blksize = 1 << blkbits; | |
1fb69e78 MS |
1175 | } |
1176 | ||
d3045530 MS |
1177 | static void fuse_statx_to_attr(struct fuse_statx *sx, struct fuse_attr *attr) |
1178 | { | |
1179 | memset(attr, 0, sizeof(*attr)); | |
1180 | attr->ino = sx->ino; | |
1181 | attr->size = sx->size; | |
1182 | attr->blocks = sx->blocks; | |
1183 | attr->atime = sx->atime.tv_sec; | |
1184 | attr->mtime = sx->mtime.tv_sec; | |
1185 | attr->ctime = sx->ctime.tv_sec; | |
1186 | attr->atimensec = sx->atime.tv_nsec; | |
1187 | attr->mtimensec = sx->mtime.tv_nsec; | |
1188 | attr->ctimensec = sx->ctime.tv_nsec; | |
1189 | attr->mode = sx->mode; | |
1190 | attr->nlink = sx->nlink; | |
1191 | attr->uid = sx->uid; | |
1192 | attr->gid = sx->gid; | |
1193 | attr->rdev = new_encode_dev(MKDEV(sx->rdev_major, sx->rdev_minor)); | |
1194 | attr->blksize = sx->blksize; | |
1195 | } | |
1196 | ||
2a8c810d AM |
1197 | static int fuse_do_statx(struct mnt_idmap *idmap, struct inode *inode, |
1198 | struct file *file, struct kstat *stat) | |
d3045530 MS |
1199 | { |
1200 | int err; | |
1201 | struct fuse_attr attr; | |
1202 | struct fuse_statx *sx; | |
1203 | struct fuse_statx_in inarg; | |
1204 | struct fuse_statx_out outarg; | |
1205 | struct fuse_mount *fm = get_fuse_mount(inode); | |
1206 | u64 attr_version = fuse_get_attr_version(fm->fc); | |
1207 | FUSE_ARGS(args); | |
1208 | ||
1209 | memset(&inarg, 0, sizeof(inarg)); | |
1210 | memset(&outarg, 0, sizeof(outarg)); | |
1211 | /* Directories have separate file-handle space */ | |
1212 | if (file && S_ISREG(inode->i_mode)) { | |
1213 | struct fuse_file *ff = file->private_data; | |
1214 | ||
1215 | inarg.getattr_flags |= FUSE_GETATTR_FH; | |
1216 | inarg.fh = ff->fh; | |
1217 | } | |
1218 | /* For now leave sync hints as the default, request all stats. */ | |
1219 | inarg.sx_flags = 0; | |
1220 | inarg.sx_mask = STATX_BASIC_STATS | STATX_BTIME; | |
1221 | args.opcode = FUSE_STATX; | |
1222 | args.nodeid = get_node_id(inode); | |
1223 | args.in_numargs = 1; | |
1224 | args.in_args[0].size = sizeof(inarg); | |
1225 | args.in_args[0].value = &inarg; | |
1226 | args.out_numargs = 1; | |
1227 | args.out_args[0].size = sizeof(outarg); | |
1228 | args.out_args[0].value = &outarg; | |
0c679382 | 1229 | err = fuse_simple_request(fm, &args); |
d3045530 MS |
1230 | if (err) |
1231 | return err; | |
1232 | ||
1233 | sx = &outarg.stat; | |
1234 | if (((sx->mask & STATX_SIZE) && !fuse_valid_size(sx->size)) || | |
1235 | ((sx->mask & STATX_TYPE) && (!fuse_valid_type(sx->mode) || | |
1236 | inode_wrong_type(inode, sx->mode)))) { | |
82e081ae | 1237 | fuse_make_bad(inode); |
d3045530 MS |
1238 | return -EIO; |
1239 | } | |
1240 | ||
1241 | fuse_statx_to_attr(&outarg.stat, &attr); | |
1242 | if ((sx->mask & STATX_BASIC_STATS) == STATX_BASIC_STATS) { | |
972f4c46 MS |
1243 | fuse_change_attributes(inode, &attr, &outarg.stat, |
1244 | ATTR_TIMEOUT(&outarg), attr_version); | |
d3045530 | 1245 | } |
f73016b6 BS |
1246 | |
1247 | if (stat) { | |
1248 | stat->result_mask = sx->mask & (STATX_BASIC_STATS | STATX_BTIME); | |
1249 | stat->btime.tv_sec = sx->btime.tv_sec; | |
1250 | stat->btime.tv_nsec = min_t(u32, sx->btime.tv_nsec, NSEC_PER_SEC - 1); | |
2a8c810d | 1251 | fuse_fillattr(idmap, inode, &attr, stat); |
f73016b6 BS |
1252 | stat->result_mask |= STATX_TYPE; |
1253 | } | |
d3045530 MS |
1254 | |
1255 | return 0; | |
1256 | } | |
1257 | ||
2a8c810d AM |
1258 | static int fuse_do_getattr(struct mnt_idmap *idmap, struct inode *inode, |
1259 | struct kstat *stat, struct file *file) | |
e5e5558e MS |
1260 | { |
1261 | int err; | |
c79e322f MS |
1262 | struct fuse_getattr_in inarg; |
1263 | struct fuse_attr_out outarg; | |
fcee216b | 1264 | struct fuse_mount *fm = get_fuse_mount(inode); |
7078187a | 1265 | FUSE_ARGS(args); |
1fb69e78 MS |
1266 | u64 attr_version; |
1267 | ||
fcee216b | 1268 | attr_version = fuse_get_attr_version(fm->fc); |
1fb69e78 | 1269 | |
c79e322f | 1270 | memset(&inarg, 0, sizeof(inarg)); |
0e9663ee | 1271 | memset(&outarg, 0, sizeof(outarg)); |
c79e322f MS |
1272 | /* Directories have separate file-handle space */ |
1273 | if (file && S_ISREG(inode->i_mode)) { | |
1274 | struct fuse_file *ff = file->private_data; | |
1275 | ||
1276 | inarg.getattr_flags |= FUSE_GETATTR_FH; | |
1277 | inarg.fh = ff->fh; | |
1278 | } | |
d5b48543 MS |
1279 | args.opcode = FUSE_GETATTR; |
1280 | args.nodeid = get_node_id(inode); | |
1281 | args.in_numargs = 1; | |
1282 | args.in_args[0].size = sizeof(inarg); | |
1283 | args.in_args[0].value = &inarg; | |
1284 | args.out_numargs = 1; | |
1285 | args.out_args[0].size = sizeof(outarg); | |
1286 | args.out_args[0].value = &outarg; | |
0c679382 | 1287 | err = fuse_simple_request(fm, &args); |
e5e5558e | 1288 | if (!err) { |
eb59bd17 | 1289 | if (fuse_invalid_attr(&outarg.attr) || |
6e3e2c43 | 1290 | inode_wrong_type(inode, outarg.attr.mode)) { |
5d069dbe | 1291 | fuse_make_bad(inode); |
e5e5558e MS |
1292 | err = -EIO; |
1293 | } else { | |
972f4c46 | 1294 | fuse_change_attributes(inode, &outarg.attr, NULL, |
9dc10a54 | 1295 | ATTR_TIMEOUT(&outarg), |
1fb69e78 MS |
1296 | attr_version); |
1297 | if (stat) | |
2a8c810d | 1298 | fuse_fillattr(idmap, inode, &outarg.attr, stat); |
e5e5558e MS |
1299 | } |
1300 | } | |
1301 | return err; | |
1302 | } | |
1303 | ||
2a8c810d AM |
1304 | static int fuse_update_get_attr(struct mnt_idmap *idmap, struct inode *inode, |
1305 | struct file *file, struct kstat *stat, | |
1306 | u32 request_mask, unsigned int flags) | |
bcb4be80 MS |
1307 | { |
1308 | struct fuse_inode *fi = get_fuse_inode(inode); | |
d3045530 | 1309 | struct fuse_conn *fc = get_fuse_conn(inode); |
5b97eeac | 1310 | int err = 0; |
bf5c1898 | 1311 | bool sync; |
ec855375 MS |
1312 | u32 inval_mask = READ_ONCE(fi->inval_mask); |
1313 | u32 cache_mask = fuse_get_cache_mask(inode); | |
bcb4be80 | 1314 | |
d3045530 MS |
1315 | |
1316 | /* FUSE only supports basic stats and possibly btime */ | |
1317 | request_mask &= STATX_BASIC_STATS | STATX_BTIME; | |
1318 | retry: | |
1319 | if (fc->no_statx) | |
1320 | request_mask &= STATX_BASIC_STATS; | |
8d8f9c4b MS |
1321 | |
1322 | if (!request_mask) | |
1323 | sync = false; | |
1324 | else if (flags & AT_STATX_FORCE_SYNC) | |
bf5c1898 MS |
1325 | sync = true; |
1326 | else if (flags & AT_STATX_DONT_SYNC) | |
1327 | sync = false; | |
ec855375 | 1328 | else if (request_mask & inval_mask & ~cache_mask) |
2f1e8196 | 1329 | sync = true; |
bf5c1898 MS |
1330 | else |
1331 | sync = time_before64(fi->i_time, get_jiffies_64()); | |
1332 | ||
1333 | if (sync) { | |
60bcc88a | 1334 | forget_all_cached_acls(inode); |
d3045530 MS |
1335 | /* Try statx if BTIME is requested */ |
1336 | if (!fc->no_statx && (request_mask & ~STATX_BASIC_STATS)) { | |
2a8c810d | 1337 | err = fuse_do_statx(idmap, inode, file, stat); |
d3045530 MS |
1338 | if (err == -ENOSYS) { |
1339 | fc->no_statx = 1; | |
eb4b691b | 1340 | err = 0; |
d3045530 MS |
1341 | goto retry; |
1342 | } | |
1343 | } else { | |
2a8c810d | 1344 | err = fuse_do_getattr(idmap, inode, stat, file); |
d3045530 | 1345 | } |
5b97eeac | 1346 | } else if (stat) { |
2a8c810d | 1347 | generic_fillattr(idmap, request_mask, inode, stat); |
5b97eeac MS |
1348 | stat->mode = fi->orig_i_mode; |
1349 | stat->ino = fi->orig_ino; | |
972f4c46 MS |
1350 | if (test_bit(FUSE_I_BTIME, &fi->state)) { |
1351 | stat->btime = fi->i_btime; | |
1352 | stat->result_mask |= STATX_BTIME; | |
1353 | } | |
bcb4be80 MS |
1354 | } |
1355 | ||
bcb4be80 MS |
1356 | return err; |
1357 | } | |
1358 | ||
c6c745b8 | 1359 | int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask) |
5b97eeac | 1360 | { |
2a8c810d | 1361 | return fuse_update_get_attr(&nop_mnt_idmap, inode, file, NULL, mask, 0); |
5b97eeac MS |
1362 | } |
1363 | ||
fcee216b | 1364 | int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid, |
4f8d3702 | 1365 | u64 child_nodeid, struct qstr *name, u32 flags) |
3b463ae0 JM |
1366 | { |
1367 | int err = -ENOTDIR; | |
1368 | struct inode *parent; | |
1369 | struct dentry *dir; | |
1370 | struct dentry *entry; | |
1371 | ||
fcee216b | 1372 | parent = fuse_ilookup(fc, parent_nodeid, NULL); |
3b463ae0 JM |
1373 | if (!parent) |
1374 | return -ENOENT; | |
1375 | ||
bda9a719 | 1376 | inode_lock_nested(parent, I_MUTEX_PARENT); |
3b463ae0 JM |
1377 | if (!S_ISDIR(parent->i_mode)) |
1378 | goto unlock; | |
1379 | ||
1380 | err = -ENOENT; | |
1381 | dir = d_find_alias(parent); | |
1382 | if (!dir) | |
1383 | goto unlock; | |
1384 | ||
8387ff25 | 1385 | name->hash = full_name_hash(dir, name->name, name->len); |
3b463ae0 JM |
1386 | entry = d_lookup(dir, name); |
1387 | dput(dir); | |
1388 | if (!entry) | |
1389 | goto unlock; | |
1390 | ||
261aaba7 | 1391 | fuse_dir_changed(parent); |
4f8d3702 MS |
1392 | if (!(flags & FUSE_EXPIRE_ONLY)) |
1393 | d_invalidate(entry); | |
1394 | fuse_invalidate_entry_cache(entry); | |
451d0f59 | 1395 | |
2b0143b5 | 1396 | if (child_nodeid != 0 && d_really_is_positive(entry)) { |
5955102c | 1397 | inode_lock(d_inode(entry)); |
2b0143b5 | 1398 | if (get_node_id(d_inode(entry)) != child_nodeid) { |
451d0f59 JM |
1399 | err = -ENOENT; |
1400 | goto badentry; | |
1401 | } | |
1402 | if (d_mountpoint(entry)) { | |
1403 | err = -EBUSY; | |
1404 | goto badentry; | |
1405 | } | |
e36cb0b8 | 1406 | if (d_is_dir(entry)) { |
451d0f59 JM |
1407 | shrink_dcache_parent(entry); |
1408 | if (!simple_empty(entry)) { | |
1409 | err = -ENOTEMPTY; | |
1410 | goto badentry; | |
1411 | } | |
2b0143b5 | 1412 | d_inode(entry)->i_flags |= S_DEAD; |
451d0f59 JM |
1413 | } |
1414 | dont_mount(entry); | |
2b0143b5 | 1415 | clear_nlink(d_inode(entry)); |
451d0f59 JM |
1416 | err = 0; |
1417 | badentry: | |
5955102c | 1418 | inode_unlock(d_inode(entry)); |
451d0f59 JM |
1419 | if (!err) |
1420 | d_delete(entry); | |
1421 | } else { | |
1422 | err = 0; | |
1423 | } | |
3b463ae0 | 1424 | dput(entry); |
3b463ae0 JM |
1425 | |
1426 | unlock: | |
5955102c | 1427 | inode_unlock(parent); |
3b463ae0 JM |
1428 | iput(parent); |
1429 | return err; | |
1430 | } | |
1431 | ||
b1387777 DM |
1432 | static inline bool fuse_permissible_uidgid(struct fuse_conn *fc) |
1433 | { | |
1434 | const struct cred *cred = current_cred(); | |
1435 | ||
1436 | return (uid_eq(cred->euid, fc->user_id) && | |
1437 | uid_eq(cred->suid, fc->user_id) && | |
1438 | uid_eq(cred->uid, fc->user_id) && | |
1439 | gid_eq(cred->egid, fc->group_id) && | |
1440 | gid_eq(cred->sgid, fc->group_id) && | |
1441 | gid_eq(cred->gid, fc->group_id)); | |
1442 | } | |
1443 | ||
87729a55 MS |
1444 | /* |
1445 | * Calling into a user-controlled filesystem gives the filesystem | |
c2132c1b | 1446 | * daemon ptrace-like capabilities over the current process. This |
87729a55 MS |
1447 | * means, that the filesystem daemon is able to record the exact |
1448 | * filesystem operations performed, and can also control the behavior | |
1449 | * of the requester process in otherwise impossible ways. For example | |
1450 | * it can delay the operation for arbitrary length of time allowing | |
1451 | * DoS against the requester. | |
1452 | * | |
1453 | * For this reason only those processes can call into the filesystem, | |
1454 | * for which the owner of the mount has ptrace privilege. This | |
1455 | * excludes processes started by other users, suid or sgid processes. | |
1456 | */ | |
b1387777 | 1457 | bool fuse_allow_current_process(struct fuse_conn *fc) |
87729a55 | 1458 | { |
b1387777 | 1459 | bool allow; |
9ccf47b2 | 1460 | |
29433a29 | 1461 | if (fc->allow_other) |
b1387777 DM |
1462 | allow = current_in_userns(fc->user_ns); |
1463 | else | |
1464 | allow = fuse_permissible_uidgid(fc); | |
87729a55 | 1465 | |
b1387777 DM |
1466 | if (!allow && allow_sys_admin_access && capable(CAP_SYS_ADMIN)) |
1467 | allow = true; | |
c69e8d9c | 1468 | |
b1387777 | 1469 | return allow; |
87729a55 MS |
1470 | } |
1471 | ||
31d40d74 MS |
1472 | static int fuse_access(struct inode *inode, int mask) |
1473 | { | |
fcee216b | 1474 | struct fuse_mount *fm = get_fuse_mount(inode); |
7078187a | 1475 | FUSE_ARGS(args); |
31d40d74 MS |
1476 | struct fuse_access_in inarg; |
1477 | int err; | |
1478 | ||
698fa1d1 MS |
1479 | BUG_ON(mask & MAY_NOT_BLOCK); |
1480 | ||
6d14b185 AM |
1481 | /* |
1482 | * We should not send FUSE_ACCESS to the userspace | |
1483 | * when idmapped mounts are enabled as for this case | |
1484 | * we have fc->default_permissions = 1 and access | |
1485 | * permission checks are done on the kernel side. | |
1486 | */ | |
1487 | WARN_ON_ONCE(!(fm->sb->s_iflags & SB_I_NOIDMAP)); | |
1488 | ||
fcee216b | 1489 | if (fm->fc->no_access) |
31d40d74 MS |
1490 | return 0; |
1491 | ||
31d40d74 | 1492 | memset(&inarg, 0, sizeof(inarg)); |
e6305c43 | 1493 | inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC); |
d5b48543 MS |
1494 | args.opcode = FUSE_ACCESS; |
1495 | args.nodeid = get_node_id(inode); | |
1496 | args.in_numargs = 1; | |
1497 | args.in_args[0].size = sizeof(inarg); | |
1498 | args.in_args[0].value = &inarg; | |
0c679382 | 1499 | err = fuse_simple_request(fm, &args); |
31d40d74 | 1500 | if (err == -ENOSYS) { |
fcee216b | 1501 | fm->fc->no_access = 1; |
31d40d74 MS |
1502 | err = 0; |
1503 | } | |
1504 | return err; | |
1505 | } | |
1506 | ||
10556cb2 | 1507 | static int fuse_perm_getattr(struct inode *inode, int mask) |
19690ddb | 1508 | { |
10556cb2 | 1509 | if (mask & MAY_NOT_BLOCK) |
19690ddb MS |
1510 | return -ECHILD; |
1511 | ||
60bcc88a | 1512 | forget_all_cached_acls(inode); |
2a8c810d | 1513 | return fuse_do_getattr(&nop_mnt_idmap, inode, NULL, NULL); |
19690ddb MS |
1514 | } |
1515 | ||
6f9f1180 MS |
1516 | /* |
1517 | * Check permission. The two basic access models of FUSE are: | |
1518 | * | |
1519 | * 1) Local access checking ('default_permissions' mount option) based | |
1520 | * on file mode. This is the plain old disk filesystem permission | |
2d09ab22 | 1521 | * model. |
6f9f1180 MS |
1522 | * |
1523 | * 2) "Remote" access checking, where server is responsible for | |
1524 | * checking permission in each inode operation. An exception to this | |
1525 | * is if ->permission() was invoked from sys_access() in which case an | |
1526 | * access request is sent. Execute permission is still checked | |
1527 | * locally based on file mode. | |
1528 | */ | |
4609e1f1 | 1529 | static int fuse_permission(struct mnt_idmap *idmap, |
549c7297 | 1530 | struct inode *inode, int mask) |
e5e5558e MS |
1531 | { |
1532 | struct fuse_conn *fc = get_fuse_conn(inode); | |
244f6385 MS |
1533 | bool refreshed = false; |
1534 | int err = 0; | |
e5e5558e | 1535 | |
5d069dbe MS |
1536 | if (fuse_is_bad(inode)) |
1537 | return -EIO; | |
1538 | ||
c2132c1b | 1539 | if (!fuse_allow_current_process(fc)) |
e5e5558e | 1540 | return -EACCES; |
244f6385 MS |
1541 | |
1542 | /* | |
e8e96157 | 1543 | * If attributes are needed, refresh them before proceeding |
244f6385 | 1544 | */ |
29433a29 | 1545 | if (fc->default_permissions || |
e8e96157 | 1546 | ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { |
19690ddb | 1547 | struct fuse_inode *fi = get_fuse_inode(inode); |
d233c7dd | 1548 | u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID; |
19690ddb | 1549 | |
d233c7dd MS |
1550 | if (perm_mask & READ_ONCE(fi->inval_mask) || |
1551 | time_before64(fi->i_time, get_jiffies_64())) { | |
19690ddb MS |
1552 | refreshed = true; |
1553 | ||
10556cb2 | 1554 | err = fuse_perm_getattr(inode, mask); |
19690ddb MS |
1555 | if (err) |
1556 | return err; | |
1557 | } | |
244f6385 MS |
1558 | } |
1559 | ||
29433a29 | 1560 | if (fc->default_permissions) { |
c1d82215 | 1561 | err = generic_permission(idmap, inode, mask); |
1e9a4ed9 MS |
1562 | |
1563 | /* If permission is denied, try to refresh file | |
1564 | attributes. This is also needed, because the root | |
1565 | node will at first have no permissions */ | |
244f6385 | 1566 | if (err == -EACCES && !refreshed) { |
10556cb2 | 1567 | err = fuse_perm_getattr(inode, mask); |
1e9a4ed9 | 1568 | if (!err) |
c1d82215 | 1569 | err = generic_permission(idmap, |
47291baa | 1570 | inode, mask); |
1e9a4ed9 MS |
1571 | } |
1572 | ||
6f9f1180 MS |
1573 | /* Note: the opposite of the above test does not |
1574 | exist. So if permissions are revoked this won't be | |
1575 | noticed immediately, only after the attribute | |
1576 | timeout has expired */ | |
9cfcac81 | 1577 | } else if (mask & (MAY_ACCESS | MAY_CHDIR)) { |
e8e96157 MS |
1578 | err = fuse_access(inode, mask); |
1579 | } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { | |
1580 | if (!(inode->i_mode & S_IXUGO)) { | |
1581 | if (refreshed) | |
1582 | return -EACCES; | |
1583 | ||
10556cb2 | 1584 | err = fuse_perm_getattr(inode, mask); |
e8e96157 MS |
1585 | if (!err && !(inode->i_mode & S_IXUGO)) |
1586 | return -EACCES; | |
1587 | } | |
e5e5558e | 1588 | } |
244f6385 | 1589 | return err; |
e5e5558e MS |
1590 | } |
1591 | ||
c1e4862b | 1592 | static int fuse_readlink_page(struct inode *inode, struct folio *folio) |
e5e5558e | 1593 | { |
fcee216b | 1594 | struct fuse_mount *fm = get_fuse_mount(inode); |
c1e4862b | 1595 | struct fuse_folio_desc desc = { .length = PAGE_SIZE - 1 }; |
4c29afec | 1596 | struct fuse_args_pages ap = { |
c1e4862b JK |
1597 | .num_folios = 1, |
1598 | .folios = &folio, | |
68bfb7eb | 1599 | .descs = &desc, |
4c29afec MS |
1600 | }; |
1601 | char *link; | |
1602 | ssize_t res; | |
1603 | ||
1604 | ap.args.opcode = FUSE_READLINK; | |
1605 | ap.args.nodeid = get_node_id(inode); | |
1606 | ap.args.out_pages = true; | |
1607 | ap.args.out_argvar = true; | |
1608 | ap.args.page_zeroing = true; | |
1609 | ap.args.out_numargs = 1; | |
1610 | ap.args.out_args[0].size = desc.length; | |
0c679382 | 1611 | res = fuse_simple_request(fm, &ap.args); |
e5e5558e | 1612 | |
4c29afec | 1613 | fuse_invalidate_atime(inode); |
6b255391 | 1614 | |
4c29afec MS |
1615 | if (res < 0) |
1616 | return res; | |
7078187a | 1617 | |
4c29afec MS |
1618 | if (WARN_ON(res >= PAGE_SIZE)) |
1619 | return -EIO; | |
5571f1e6 | 1620 | |
c1e4862b | 1621 | link = folio_address(folio); |
4c29afec | 1622 | link[res] = '\0'; |
5571f1e6 | 1623 | |
4c29afec | 1624 | return 0; |
5571f1e6 DS |
1625 | } |
1626 | ||
1627 | static const char *fuse_get_link(struct dentry *dentry, struct inode *inode, | |
1628 | struct delayed_call *callback) | |
1629 | { | |
1630 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c1e4862b | 1631 | struct folio *folio; |
5571f1e6 DS |
1632 | int err; |
1633 | ||
1634 | err = -EIO; | |
5d069dbe | 1635 | if (fuse_is_bad(inode)) |
5571f1e6 DS |
1636 | goto out_err; |
1637 | ||
1638 | if (fc->cache_symlinks) | |
1639 | return page_get_link(dentry, inode, callback); | |
1640 | ||
1641 | err = -ECHILD; | |
1642 | if (!dentry) | |
1643 | goto out_err; | |
1644 | ||
c1e4862b | 1645 | folio = folio_alloc(GFP_KERNEL, 0); |
5571f1e6 | 1646 | err = -ENOMEM; |
c1e4862b | 1647 | if (!folio) |
5571f1e6 DS |
1648 | goto out_err; |
1649 | ||
c1e4862b | 1650 | err = fuse_readlink_page(inode, folio); |
5571f1e6 | 1651 | if (err) { |
c1e4862b | 1652 | folio_put(folio); |
5571f1e6 DS |
1653 | goto out_err; |
1654 | } | |
1655 | ||
c1e4862b | 1656 | set_delayed_call(callback, page_put_link, &folio->page); |
5571f1e6 | 1657 | |
c1e4862b | 1658 | return folio_address(folio); |
5571f1e6 DS |
1659 | |
1660 | out_err: | |
1661 | return ERR_PTR(err); | |
e5e5558e MS |
1662 | } |
1663 | ||
e5e5558e MS |
1664 | static int fuse_dir_open(struct inode *inode, struct file *file) |
1665 | { | |
7de64d52 AG |
1666 | struct fuse_mount *fm = get_fuse_mount(inode); |
1667 | int err; | |
1668 | ||
1669 | if (fuse_is_bad(inode)) | |
1670 | return -EIO; | |
1671 | ||
1672 | err = generic_file_open(inode, file); | |
1673 | if (err) | |
1674 | return err; | |
1675 | ||
1676 | err = fuse_do_open(fm, get_node_id(inode), file, true); | |
1677 | if (!err) { | |
1678 | struct fuse_file *ff = file->private_data; | |
1679 | ||
1680 | /* | |
1681 | * Keep handling FOPEN_STREAM and FOPEN_NONSEEKABLE for | |
1682 | * directories for backward compatibility, though it's unlikely | |
1683 | * to be useful. | |
1684 | */ | |
1685 | if (ff->open_flags & (FOPEN_STREAM | FOPEN_NONSEEKABLE)) | |
1686 | nonseekable_open(inode, file); | |
03f275ad AG |
1687 | if (!(ff->open_flags & FOPEN_KEEP_CACHE)) |
1688 | invalidate_inode_pages2(inode->i_mapping); | |
7de64d52 AG |
1689 | } |
1690 | ||
1691 | return err; | |
e5e5558e MS |
1692 | } |
1693 | ||
1694 | static int fuse_dir_release(struct inode *inode, struct file *file) | |
1695 | { | |
2e64ff15 | 1696 | fuse_release_common(file, true); |
8b0797a4 MS |
1697 | |
1698 | return 0; | |
e5e5558e MS |
1699 | } |
1700 | ||
02c24a82 JB |
1701 | static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end, |
1702 | int datasync) | |
82547981 | 1703 | { |
a9c2d1e8 MS |
1704 | struct inode *inode = file->f_mapping->host; |
1705 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1706 | int err; | |
1707 | ||
5d069dbe | 1708 | if (fuse_is_bad(inode)) |
a9c2d1e8 MS |
1709 | return -EIO; |
1710 | ||
1711 | if (fc->no_fsyncdir) | |
1712 | return 0; | |
1713 | ||
1714 | inode_lock(inode); | |
1715 | err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR); | |
1716 | if (err == -ENOSYS) { | |
1717 | fc->no_fsyncdir = 1; | |
1718 | err = 0; | |
1719 | } | |
1720 | inode_unlock(inode); | |
1721 | ||
1722 | return err; | |
82547981 MS |
1723 | } |
1724 | ||
b18da0c5 MS |
1725 | static long fuse_dir_ioctl(struct file *file, unsigned int cmd, |
1726 | unsigned long arg) | |
1727 | { | |
1728 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); | |
1729 | ||
1730 | /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */ | |
1731 | if (fc->minor < 18) | |
1732 | return -ENOTTY; | |
1733 | ||
1734 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR); | |
1735 | } | |
1736 | ||
1737 | static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd, | |
1738 | unsigned long arg) | |
1739 | { | |
1740 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); | |
1741 | ||
1742 | if (fc->minor < 18) | |
1743 | return -ENOTTY; | |
1744 | ||
1745 | return fuse_ioctl_common(file, cmd, arg, | |
1746 | FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR); | |
1747 | } | |
1748 | ||
b0aa7606 | 1749 | static bool update_mtime(unsigned ivalid, bool trust_local_mtime) |
17637cba MS |
1750 | { |
1751 | /* Always update if mtime is explicitly set */ | |
1752 | if (ivalid & ATTR_MTIME_SET) | |
1753 | return true; | |
1754 | ||
b0aa7606 MP |
1755 | /* Or if kernel i_mtime is the official one */ |
1756 | if (trust_local_mtime) | |
1757 | return true; | |
1758 | ||
17637cba MS |
1759 | /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ |
1760 | if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) | |
1761 | return false; | |
1762 | ||
1763 | /* In all other cases update */ | |
1764 | return true; | |
1765 | } | |
1766 | ||
276a0256 AM |
1767 | static void iattr_to_fattr(struct mnt_idmap *idmap, struct fuse_conn *fc, |
1768 | struct iattr *iattr, struct fuse_setattr_in *arg, | |
1769 | bool trust_local_cmtime) | |
9e6268db MS |
1770 | { |
1771 | unsigned ivalid = iattr->ia_valid; | |
9e6268db MS |
1772 | |
1773 | if (ivalid & ATTR_MODE) | |
befc649c | 1774 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
276a0256 AM |
1775 | |
1776 | if (ivalid & ATTR_UID) { | |
1777 | kuid_t fsuid = from_vfsuid(idmap, fc->user_ns, iattr->ia_vfsuid); | |
1778 | ||
1779 | arg->valid |= FATTR_UID; | |
1780 | arg->uid = from_kuid(fc->user_ns, fsuid); | |
1781 | } | |
1782 | ||
1783 | if (ivalid & ATTR_GID) { | |
1784 | kgid_t fsgid = from_vfsgid(idmap, fc->user_ns, iattr->ia_vfsgid); | |
1785 | ||
1786 | arg->valid |= FATTR_GID; | |
1787 | arg->gid = from_kgid(fc->user_ns, fsgid); | |
1788 | } | |
1789 | ||
9e6268db | 1790 | if (ivalid & ATTR_SIZE) |
befc649c | 1791 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
17637cba MS |
1792 | if (ivalid & ATTR_ATIME) { |
1793 | arg->valid |= FATTR_ATIME; | |
befc649c | 1794 | arg->atime = iattr->ia_atime.tv_sec; |
17637cba MS |
1795 | arg->atimensec = iattr->ia_atime.tv_nsec; |
1796 | if (!(ivalid & ATTR_ATIME_SET)) | |
1797 | arg->valid |= FATTR_ATIME_NOW; | |
1798 | } | |
3ad22c62 | 1799 | if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) { |
17637cba | 1800 | arg->valid |= FATTR_MTIME; |
befc649c | 1801 | arg->mtime = iattr->ia_mtime.tv_sec; |
17637cba | 1802 | arg->mtimensec = iattr->ia_mtime.tv_nsec; |
3ad22c62 | 1803 | if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime) |
17637cba | 1804 | arg->valid |= FATTR_MTIME_NOW; |
befc649c | 1805 | } |
3ad22c62 MP |
1806 | if ((ivalid & ATTR_CTIME) && trust_local_cmtime) { |
1807 | arg->valid |= FATTR_CTIME; | |
1808 | arg->ctime = iattr->ia_ctime.tv_sec; | |
1809 | arg->ctimensec = iattr->ia_ctime.tv_nsec; | |
1810 | } | |
9e6268db MS |
1811 | } |
1812 | ||
3be5a52b MS |
1813 | /* |
1814 | * Prevent concurrent writepages on inode | |
1815 | * | |
1816 | * This is done by adding a negative bias to the inode write counter | |
1817 | * and waiting for all pending writes to finish. | |
1818 | */ | |
1819 | void fuse_set_nowrite(struct inode *inode) | |
1820 | { | |
3be5a52b MS |
1821 | struct fuse_inode *fi = get_fuse_inode(inode); |
1822 | ||
5955102c | 1823 | BUG_ON(!inode_is_locked(inode)); |
3be5a52b | 1824 | |
f15ecfef | 1825 | spin_lock(&fi->lock); |
3be5a52b MS |
1826 | BUG_ON(fi->writectr < 0); |
1827 | fi->writectr += FUSE_NOWRITE; | |
f15ecfef | 1828 | spin_unlock(&fi->lock); |
3be5a52b MS |
1829 | wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE); |
1830 | } | |
1831 | ||
1832 | /* | |
1833 | * Allow writepages on inode | |
1834 | * | |
1835 | * Remove the bias from the writecounter and send any queued | |
1836 | * writepages. | |
1837 | */ | |
1838 | static void __fuse_release_nowrite(struct inode *inode) | |
1839 | { | |
1840 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1841 | ||
1842 | BUG_ON(fi->writectr != FUSE_NOWRITE); | |
1843 | fi->writectr = 0; | |
1844 | fuse_flush_writepages(inode); | |
1845 | } | |
1846 | ||
1847 | void fuse_release_nowrite(struct inode *inode) | |
1848 | { | |
f15ecfef | 1849 | struct fuse_inode *fi = get_fuse_inode(inode); |
3be5a52b | 1850 | |
f15ecfef | 1851 | spin_lock(&fi->lock); |
3be5a52b | 1852 | __fuse_release_nowrite(inode); |
f15ecfef | 1853 | spin_unlock(&fi->lock); |
3be5a52b MS |
1854 | } |
1855 | ||
7078187a | 1856 | static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args, |
b0aa7606 MP |
1857 | struct inode *inode, |
1858 | struct fuse_setattr_in *inarg_p, | |
1859 | struct fuse_attr_out *outarg_p) | |
1860 | { | |
d5b48543 MS |
1861 | args->opcode = FUSE_SETATTR; |
1862 | args->nodeid = get_node_id(inode); | |
1863 | args->in_numargs = 1; | |
1864 | args->in_args[0].size = sizeof(*inarg_p); | |
1865 | args->in_args[0].value = inarg_p; | |
1866 | args->out_numargs = 1; | |
1867 | args->out_args[0].size = sizeof(*outarg_p); | |
1868 | args->out_args[0].value = outarg_p; | |
b0aa7606 MP |
1869 | } |
1870 | ||
1871 | /* | |
1872 | * Flush inode->i_mtime to the server | |
1873 | */ | |
ab9e13f7 | 1874 | int fuse_flush_times(struct inode *inode, struct fuse_file *ff) |
b0aa7606 | 1875 | { |
fcee216b | 1876 | struct fuse_mount *fm = get_fuse_mount(inode); |
7078187a | 1877 | FUSE_ARGS(args); |
b0aa7606 MP |
1878 | struct fuse_setattr_in inarg; |
1879 | struct fuse_attr_out outarg; | |
b0aa7606 MP |
1880 | |
1881 | memset(&inarg, 0, sizeof(inarg)); | |
1882 | memset(&outarg, 0, sizeof(outarg)); | |
1883 | ||
ab9e13f7 | 1884 | inarg.valid = FATTR_MTIME; |
3c0d5df2 JL |
1885 | inarg.mtime = inode_get_mtime_sec(inode); |
1886 | inarg.mtimensec = inode_get_mtime_nsec(inode); | |
fcee216b | 1887 | if (fm->fc->minor >= 23) { |
ab9e13f7 | 1888 | inarg.valid |= FATTR_CTIME; |
3c0d5df2 JL |
1889 | inarg.ctime = inode_get_ctime_sec(inode); |
1890 | inarg.ctimensec = inode_get_ctime_nsec(inode); | |
ab9e13f7 | 1891 | } |
1e18bda8 MS |
1892 | if (ff) { |
1893 | inarg.valid |= FATTR_FH; | |
1894 | inarg.fh = ff->fh; | |
1895 | } | |
fcee216b | 1896 | fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg); |
b0aa7606 | 1897 | |
0c679382 | 1898 | return fuse_simple_request(fm, &args); |
b0aa7606 MP |
1899 | } |
1900 | ||
6f9f1180 MS |
1901 | /* |
1902 | * Set attributes, and at the same time refresh them. | |
1903 | * | |
1904 | * Truncation is slightly complicated, because the 'truncate' request | |
1905 | * may fail, in which case we don't want to touch the mapping. | |
9ffbb916 MS |
1906 | * vmtruncate() doesn't allow for this case, so do the rlimit checking |
1907 | * and the actual truncation by hand. | |
6f9f1180 | 1908 | */ |
276a0256 AM |
1909 | int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
1910 | struct iattr *attr, struct file *file) | |
9e6268db | 1911 | { |
62490330 | 1912 | struct inode *inode = d_inode(dentry); |
fcee216b MR |
1913 | struct fuse_mount *fm = get_fuse_mount(inode); |
1914 | struct fuse_conn *fc = fm->fc; | |
06a7c3c2 | 1915 | struct fuse_inode *fi = get_fuse_inode(inode); |
8bcbbe9c | 1916 | struct address_space *mapping = inode->i_mapping; |
7078187a | 1917 | FUSE_ARGS(args); |
9e6268db MS |
1918 | struct fuse_setattr_in inarg; |
1919 | struct fuse_attr_out outarg; | |
3be5a52b | 1920 | bool is_truncate = false; |
c15016b7 | 1921 | bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode); |
3be5a52b | 1922 | loff_t oldsize; |
9e6268db | 1923 | int err; |
c15016b7 | 1924 | bool trust_local_cmtime = is_wb; |
6ae330ca | 1925 | bool fault_blocked = false; |
9e6268db | 1926 | |
29433a29 | 1927 | if (!fc->default_permissions) |
db78b877 CH |
1928 | attr->ia_valid |= ATTR_FORCE; |
1929 | ||
276a0256 | 1930 | err = setattr_prepare(idmap, dentry, attr); |
db78b877 CH |
1931 | if (err) |
1932 | return err; | |
1e9a4ed9 | 1933 | |
6ae330ca VG |
1934 | if (attr->ia_valid & ATTR_SIZE) { |
1935 | if (WARN_ON(!S_ISREG(inode->i_mode))) | |
1936 | return -EIO; | |
1937 | is_truncate = true; | |
1938 | } | |
1939 | ||
1940 | if (FUSE_IS_DAX(inode) && is_truncate) { | |
8bcbbe9c | 1941 | filemap_invalidate_lock(mapping); |
6ae330ca VG |
1942 | fault_blocked = true; |
1943 | err = fuse_dax_break_layouts(inode, 0, 0); | |
1944 | if (err) { | |
8bcbbe9c | 1945 | filemap_invalidate_unlock(mapping); |
6ae330ca VG |
1946 | return err; |
1947 | } | |
1948 | } | |
1949 | ||
8d56addd | 1950 | if (attr->ia_valid & ATTR_OPEN) { |
df0e91d4 MS |
1951 | /* This is coming from open(..., ... | O_TRUNC); */ |
1952 | WARN_ON(!(attr->ia_valid & ATTR_SIZE)); | |
1953 | WARN_ON(attr->ia_size != 0); | |
1954 | if (fc->atomic_o_trunc) { | |
1955 | /* | |
1956 | * No need to send request to userspace, since actual | |
1957 | * truncation has already been done by OPEN. But still | |
1958 | * need to truncate page cache. | |
1959 | */ | |
1960 | i_size_write(inode, 0); | |
1961 | truncate_pagecache(inode, 0); | |
6ae330ca | 1962 | goto out; |
df0e91d4 | 1963 | } |
8d56addd MS |
1964 | file = NULL; |
1965 | } | |
6ff958ed | 1966 | |
b24e7598 | 1967 | /* Flush dirty data/metadata before non-truncate SETATTR */ |
c15016b7 | 1968 | if (is_wb && |
b24e7598 MS |
1969 | attr->ia_valid & |
1970 | (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET | | |
1971 | ATTR_TIMES_SET)) { | |
1972 | err = write_inode_now(inode, true); | |
1973 | if (err) | |
1974 | return err; | |
1975 | ||
1976 | fuse_set_nowrite(inode); | |
1977 | fuse_release_nowrite(inode); | |
1978 | } | |
1979 | ||
06a7c3c2 | 1980 | if (is_truncate) { |
3be5a52b | 1981 | fuse_set_nowrite(inode); |
06a7c3c2 | 1982 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
3ad22c62 MP |
1983 | if (trust_local_cmtime && attr->ia_size != inode->i_size) |
1984 | attr->ia_valid |= ATTR_MTIME | ATTR_CTIME; | |
06a7c3c2 | 1985 | } |
3be5a52b | 1986 | |
9e6268db | 1987 | memset(&inarg, 0, sizeof(inarg)); |
0e9663ee | 1988 | memset(&outarg, 0, sizeof(outarg)); |
276a0256 | 1989 | iattr_to_fattr(idmap, fc, attr, &inarg, trust_local_cmtime); |
49d4914f MS |
1990 | if (file) { |
1991 | struct fuse_file *ff = file->private_data; | |
1992 | inarg.valid |= FATTR_FH; | |
1993 | inarg.fh = ff->fh; | |
1994 | } | |
31792161 VG |
1995 | |
1996 | /* Kill suid/sgid for non-directory chown unconditionally */ | |
1997 | if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) && | |
1998 | attr->ia_valid & (ATTR_UID | ATTR_GID)) | |
1999 | inarg.valid |= FATTR_KILL_SUIDGID; | |
2000 | ||
f3332114 MS |
2001 | if (attr->ia_valid & ATTR_SIZE) { |
2002 | /* For mandatory locking in truncate */ | |
2003 | inarg.valid |= FATTR_LOCKOWNER; | |
2004 | inarg.lock_owner = fuse_lock_owner_id(fc, current->files); | |
31792161 VG |
2005 | |
2006 | /* Kill suid/sgid for truncate only if no CAP_FSETID */ | |
2007 | if (fc->handle_killpriv_v2 && !capable(CAP_FSETID)) | |
2008 | inarg.valid |= FATTR_KILL_SUIDGID; | |
f3332114 | 2009 | } |
7078187a | 2010 | fuse_setattr_fill(fc, &args, inode, &inarg, &outarg); |
0c679382 | 2011 | err = fuse_simple_request(fm, &args); |
e00d2c2d MS |
2012 | if (err) { |
2013 | if (err == -EINTR) | |
2014 | fuse_invalidate_attr(inode); | |
3be5a52b | 2015 | goto error; |
e00d2c2d | 2016 | } |
9e6268db | 2017 | |
eb59bd17 | 2018 | if (fuse_invalid_attr(&outarg.attr) || |
6e3e2c43 | 2019 | inode_wrong_type(inode, outarg.attr.mode)) { |
5d069dbe | 2020 | fuse_make_bad(inode); |
3be5a52b MS |
2021 | err = -EIO; |
2022 | goto error; | |
2023 | } | |
2024 | ||
f15ecfef | 2025 | spin_lock(&fi->lock); |
b0aa7606 | 2026 | /* the kernel maintains i_mtime locally */ |
3ad22c62 MP |
2027 | if (trust_local_cmtime) { |
2028 | if (attr->ia_valid & ATTR_MTIME) | |
3c0d5df2 | 2029 | inode_set_mtime_to_ts(inode, attr->ia_mtime); |
3ad22c62 | 2030 | if (attr->ia_valid & ATTR_CTIME) |
ceb2d5e9 | 2031 | inode_set_ctime_to_ts(inode, attr->ia_ctime); |
1e18bda8 | 2032 | /* FIXME: clear I_DIRTY_SYNC? */ |
b0aa7606 MP |
2033 | } |
2034 | ||
972f4c46 | 2035 | fuse_change_attributes_common(inode, &outarg.attr, NULL, |
9dc10a54 | 2036 | ATTR_TIMEOUT(&outarg), |
69eb56f6 | 2037 | fuse_get_cache_mask(inode), 0); |
3be5a52b | 2038 | oldsize = inode->i_size; |
8373200b | 2039 | /* see the comment in fuse_change_attributes() */ |
c15016b7 | 2040 | if (!is_wb || is_truncate) |
8373200b | 2041 | i_size_write(inode, outarg.attr.size); |
3be5a52b MS |
2042 | |
2043 | if (is_truncate) { | |
f15ecfef | 2044 | /* NOTE: this may release/reacquire fi->lock */ |
3be5a52b MS |
2045 | __fuse_release_nowrite(inode); |
2046 | } | |
f15ecfef | 2047 | spin_unlock(&fi->lock); |
3be5a52b MS |
2048 | |
2049 | /* | |
2050 | * Only call invalidate_inode_pages2() after removing | |
2bf06b8e | 2051 | * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock. |
3be5a52b | 2052 | */ |
8373200b PE |
2053 | if ((is_truncate || !is_wb) && |
2054 | S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) { | |
7caef267 | 2055 | truncate_pagecache(inode, outarg.attr.size); |
8bcbbe9c | 2056 | invalidate_inode_pages2(mapping); |
e00d2c2d MS |
2057 | } |
2058 | ||
06a7c3c2 | 2059 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
6ae330ca VG |
2060 | out: |
2061 | if (fault_blocked) | |
8bcbbe9c | 2062 | filemap_invalidate_unlock(mapping); |
6ae330ca | 2063 | |
e00d2c2d | 2064 | return 0; |
3be5a52b MS |
2065 | |
2066 | error: | |
2067 | if (is_truncate) | |
2068 | fuse_release_nowrite(inode); | |
2069 | ||
06a7c3c2 | 2070 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
6ae330ca VG |
2071 | |
2072 | if (fault_blocked) | |
8bcbbe9c | 2073 | filemap_invalidate_unlock(mapping); |
3be5a52b | 2074 | return err; |
9e6268db MS |
2075 | } |
2076 | ||
c1632a0f | 2077 | static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry, |
549c7297 | 2078 | struct iattr *attr) |
49d4914f | 2079 | { |
2b0143b5 | 2080 | struct inode *inode = d_inode(entry); |
5e940c1d | 2081 | struct fuse_conn *fc = get_fuse_conn(inode); |
a09f99ed | 2082 | struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL; |
5e2b8828 | 2083 | int ret; |
efb9fa9e | 2084 | |
5d069dbe MS |
2085 | if (fuse_is_bad(inode)) |
2086 | return -EIO; | |
2087 | ||
efb9fa9e MP |
2088 | if (!fuse_allow_current_process(get_fuse_conn(inode))) |
2089 | return -EACCES; | |
2090 | ||
a09f99ed | 2091 | if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) { |
a09f99ed MS |
2092 | attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | |
2093 | ATTR_MODE); | |
5e940c1d | 2094 | |
a09f99ed | 2095 | /* |
5e940c1d MS |
2096 | * The only sane way to reliably kill suid/sgid is to do it in |
2097 | * the userspace filesystem | |
2098 | * | |
2099 | * This should be done on write(), truncate() and chown(). | |
a09f99ed | 2100 | */ |
8981bdfd | 2101 | if (!fc->handle_killpriv && !fc->handle_killpriv_v2) { |
5e940c1d MS |
2102 | /* |
2103 | * ia_mode calculation may have used stale i_mode. | |
2104 | * Refresh and recalculate. | |
2105 | */ | |
2a8c810d | 2106 | ret = fuse_do_getattr(idmap, inode, NULL, file); |
5e940c1d MS |
2107 | if (ret) |
2108 | return ret; | |
2109 | ||
2110 | attr->ia_mode = inode->i_mode; | |
c01638f5 | 2111 | if (inode->i_mode & S_ISUID) { |
5e940c1d MS |
2112 | attr->ia_valid |= ATTR_MODE; |
2113 | attr->ia_mode &= ~S_ISUID; | |
2114 | } | |
c01638f5 | 2115 | if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
5e940c1d MS |
2116 | attr->ia_valid |= ATTR_MODE; |
2117 | attr->ia_mode &= ~S_ISGID; | |
2118 | } | |
a09f99ed MS |
2119 | } |
2120 | } | |
2121 | if (!attr->ia_valid) | |
2122 | return 0; | |
5e2b8828 | 2123 | |
276a0256 | 2124 | ret = fuse_do_setattr(idmap, entry, attr, file); |
5e2b8828 | 2125 | if (!ret) { |
60bcc88a SF |
2126 | /* |
2127 | * If filesystem supports acls it may have updated acl xattrs in | |
2128 | * the filesystem, so forget cached acls for the inode. | |
2129 | */ | |
2130 | if (fc->posix_acl) | |
2131 | forget_all_cached_acls(inode); | |
2132 | ||
5e2b8828 MS |
2133 | /* Directory mode changed, may need to revalidate access */ |
2134 | if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE)) | |
2135 | fuse_invalidate_entry_cache(entry); | |
2136 | } | |
2137 | return ret; | |
49d4914f MS |
2138 | } |
2139 | ||
b74d24f7 | 2140 | static int fuse_getattr(struct mnt_idmap *idmap, |
549c7297 | 2141 | const struct path *path, struct kstat *stat, |
a528d35e | 2142 | u32 request_mask, unsigned int flags) |
e5e5558e | 2143 | { |
a528d35e | 2144 | struct inode *inode = d_inode(path->dentry); |
244f6385 | 2145 | struct fuse_conn *fc = get_fuse_conn(inode); |
244f6385 | 2146 | |
5d069dbe MS |
2147 | if (fuse_is_bad(inode)) |
2148 | return -EIO; | |
2149 | ||
5157da2c MS |
2150 | if (!fuse_allow_current_process(fc)) { |
2151 | if (!request_mask) { | |
2152 | /* | |
2153 | * If user explicitly requested *nothing* then don't | |
2154 | * error out, but return st_dev only. | |
2155 | */ | |
2156 | stat->result_mask = 0; | |
2157 | stat->dev = inode->i_sb->s_dev; | |
2158 | return 0; | |
2159 | } | |
244f6385 | 2160 | return -EACCES; |
5157da2c | 2161 | } |
244f6385 | 2162 | |
2a8c810d | 2163 | return fuse_update_get_attr(idmap, inode, NULL, stat, request_mask, flags); |
e5e5558e MS |
2164 | } |
2165 | ||
754661f1 | 2166 | static const struct inode_operations fuse_dir_inode_operations = { |
e5e5558e | 2167 | .lookup = fuse_lookup, |
9e6268db MS |
2168 | .mkdir = fuse_mkdir, |
2169 | .symlink = fuse_symlink, | |
2170 | .unlink = fuse_unlink, | |
2171 | .rmdir = fuse_rmdir, | |
2773bf00 | 2172 | .rename = fuse_rename2, |
9e6268db MS |
2173 | .link = fuse_link, |
2174 | .setattr = fuse_setattr, | |
2175 | .create = fuse_create, | |
c8ccbe03 | 2176 | .atomic_open = fuse_atomic_open, |
7d375390 | 2177 | .tmpfile = fuse_tmpfile, |
9e6268db | 2178 | .mknod = fuse_mknod, |
e5e5558e MS |
2179 | .permission = fuse_permission, |
2180 | .getattr = fuse_getattr, | |
92a8780e | 2181 | .listxattr = fuse_listxattr, |
facd6105 CB |
2182 | .get_inode_acl = fuse_get_inode_acl, |
2183 | .get_acl = fuse_get_acl, | |
60bcc88a | 2184 | .set_acl = fuse_set_acl, |
72227eac MS |
2185 | .fileattr_get = fuse_fileattr_get, |
2186 | .fileattr_set = fuse_fileattr_set, | |
e5e5558e MS |
2187 | }; |
2188 | ||
4b6f5d20 | 2189 | static const struct file_operations fuse_dir_operations = { |
b6aeaded | 2190 | .llseek = generic_file_llseek, |
e5e5558e | 2191 | .read = generic_read_dir, |
d9b3dbdc | 2192 | .iterate_shared = fuse_readdir, |
e5e5558e MS |
2193 | .open = fuse_dir_open, |
2194 | .release = fuse_dir_release, | |
82547981 | 2195 | .fsync = fuse_dir_fsync, |
b18da0c5 MS |
2196 | .unlocked_ioctl = fuse_dir_ioctl, |
2197 | .compat_ioctl = fuse_dir_compat_ioctl, | |
e5e5558e MS |
2198 | }; |
2199 | ||
754661f1 | 2200 | static const struct inode_operations fuse_common_inode_operations = { |
9e6268db | 2201 | .setattr = fuse_setattr, |
e5e5558e MS |
2202 | .permission = fuse_permission, |
2203 | .getattr = fuse_getattr, | |
92a8780e | 2204 | .listxattr = fuse_listxattr, |
facd6105 CB |
2205 | .get_inode_acl = fuse_get_inode_acl, |
2206 | .get_acl = fuse_get_acl, | |
60bcc88a | 2207 | .set_acl = fuse_set_acl, |
72227eac MS |
2208 | .fileattr_get = fuse_fileattr_get, |
2209 | .fileattr_set = fuse_fileattr_set, | |
e5e5558e MS |
2210 | }; |
2211 | ||
754661f1 | 2212 | static const struct inode_operations fuse_symlink_inode_operations = { |
9e6268db | 2213 | .setattr = fuse_setattr, |
6b255391 | 2214 | .get_link = fuse_get_link, |
e5e5558e | 2215 | .getattr = fuse_getattr, |
92a8780e | 2216 | .listxattr = fuse_listxattr, |
e5e5558e MS |
2217 | }; |
2218 | ||
2219 | void fuse_init_common(struct inode *inode) | |
2220 | { | |
2221 | inode->i_op = &fuse_common_inode_operations; | |
2222 | } | |
2223 | ||
2224 | void fuse_init_dir(struct inode *inode) | |
2225 | { | |
ab2257e9 MS |
2226 | struct fuse_inode *fi = get_fuse_inode(inode); |
2227 | ||
e5e5558e MS |
2228 | inode->i_op = &fuse_dir_inode_operations; |
2229 | inode->i_fop = &fuse_dir_operations; | |
ab2257e9 MS |
2230 | |
2231 | spin_lock_init(&fi->rdc.lock); | |
2232 | fi->rdc.cached = false; | |
2233 | fi->rdc.size = 0; | |
2234 | fi->rdc.pos = 0; | |
2235 | fi->rdc.version = 0; | |
e5e5558e MS |
2236 | } |
2237 | ||
5efd00e4 | 2238 | static int fuse_symlink_read_folio(struct file *null, struct folio *folio) |
5571f1e6 | 2239 | { |
c1e4862b | 2240 | int err = fuse_readlink_page(folio->mapping->host, folio); |
5571f1e6 DS |
2241 | |
2242 | if (!err) | |
5efd00e4 | 2243 | folio_mark_uptodate(folio); |
5571f1e6 | 2244 | |
5efd00e4 | 2245 | folio_unlock(folio); |
5571f1e6 DS |
2246 | |
2247 | return err; | |
2248 | } | |
2249 | ||
2250 | static const struct address_space_operations fuse_symlink_aops = { | |
5efd00e4 | 2251 | .read_folio = fuse_symlink_read_folio, |
5571f1e6 DS |
2252 | }; |
2253 | ||
e5e5558e MS |
2254 | void fuse_init_symlink(struct inode *inode) |
2255 | { | |
2256 | inode->i_op = &fuse_symlink_inode_operations; | |
5571f1e6 DS |
2257 | inode->i_data.a_ops = &fuse_symlink_aops; |
2258 | inode_nohighmem(inode); | |
e5e5558e | 2259 | } |