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