^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) FUSE: Filesystem in Userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) This program can be distributed under the terms of the GNU GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) See the file COPYING.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "fuse_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fs_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/posix_acl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static void fuse_advise_use_readdirplus(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct fuse_inode *fi = get_fuse_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #if BITS_PER_LONG >= 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static inline void __fuse_dentry_settime(struct dentry *entry, u64 time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) entry->d_fsdata = (void *) time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static inline u64 fuse_dentry_time(const struct dentry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return (u64)entry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) union fuse_dentry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u64 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ((union fuse_dentry *) dentry->d_fsdata)->time = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static inline u64 fuse_dentry_time(const struct dentry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return ((union fuse_dentry *) entry->d_fsdata)->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void fuse_dentry_settime(struct dentry *dentry, u64 time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) bool delete = !time && fc->delete_stale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * Don't care about races, either way it's just an optimization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) spin_lock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!delete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) dentry->d_flags &= ~DCACHE_OP_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dentry->d_flags |= DCACHE_OP_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) __fuse_dentry_settime(dentry, time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * FUSE caches dentries and attributes with separate timeout. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * time in jiffies until the dentry/attributes are valid is stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * dentry->d_fsdata and fuse_inode->i_time respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Calculate the time in jiffies until a dentry/attributes are valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static u64 time_to_jiffies(u64 sec, u32 nsec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (sec || nsec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct timespec64 ts = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) min_t(u32, nsec, NSEC_PER_SEC - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return get_jiffies_64() + timespec64_to_jiffies(&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Set dentry and possibly attribute timeouts from the lookup/mk*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * replies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) fuse_dentry_settime(entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static u64 attr_timeout(struct fuse_attr_out *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) u64 entry_attr_timeout(struct fuse_entry_out *o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Mark the attributes as stale, so that at the next call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * ->getattr() they will be fetched from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void fuse_invalidate_attr(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void fuse_dir_changed(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) fuse_invalidate_attr(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) inode_maybe_inc_iversion(dir, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Mark the attributes as stale due to an atime change. Avoid the invalidate if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * atime is not used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void fuse_invalidate_atime(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (!IS_RDONLY(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) fuse_invalidate_attr_mask(inode, STATX_ATIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Just mark the entry as stale, so that a next attempt to look it up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * will result in a new lookup call to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * This is called when a dentry is about to become negative and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * timeout is unknown (unlink, rmdir, rename and in some cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) void fuse_invalidate_entry_cache(struct dentry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) fuse_dentry_settime(entry, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Same as fuse_invalidate_entry_cache(), but also try to remove the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * dentry from the hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void fuse_invalidate_entry(struct dentry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) d_invalidate(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) fuse_invalidate_entry_cache(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) u64 nodeid, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct fuse_entry_out *outarg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) memset(outarg, 0, sizeof(struct fuse_entry_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) args->opcode = FUSE_LOOKUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) args->nodeid = nodeid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) args->in_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) args->in_args[0].size = name->len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) args->in_args[0].value = name->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) args->out_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) args->out_args[0].size = sizeof(struct fuse_entry_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) args->out_args[0].value = outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Check whether the dentry is still valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * If the entry validity timeout has expired and the dentry is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * positive, try to redo the lookup. If the lookup results in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * different inode, then let the VFS invalidate the dentry and redo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * the lookup once more. If the lookup results in the same inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * then refresh the attributes, timeouts and mark the dentry valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct fuse_mount *fm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct fuse_inode *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) inode = d_inode_rcu(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (inode && fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) (flags & LOOKUP_REVAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct fuse_entry_out outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct fuse_forget_link *forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) u64 attr_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* For negative dentries, always do a fresh lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret = -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (flags & LOOKUP_RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) fm = get_fuse_mount(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) forget = fuse_alloc_forget();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!forget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) attr_version = fuse_get_attr_version(fm->fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) parent = dget_parent(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) fuse_lookup_init(fm->fc, &args, get_node_id(d_inode(parent)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) &entry->d_name, &outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* Zero nodeid is same as -ENOENT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (!ret && !outarg.nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (outarg.nodeid != get_node_id(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) fuse_queue_forget(fm->fc, forget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) outarg.nodeid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) spin_lock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) fi->nlookup++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) spin_unlock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) kfree(forget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (ret == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (ret || fuse_invalid_attr(&outarg.attr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) fuse_stale_inode(inode, outarg.generation, &outarg.attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) forget_all_cached_acls(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) fuse_change_attributes(inode, &outarg.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) entry_attr_timeout(&outarg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) attr_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) fuse_change_entry_timeout(entry, &outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) } else if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (flags & LOOKUP_RCU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) parent = dget_parent(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) fuse_advise_use_readdirplus(d_inode(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) #if BITS_PER_LONG < 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static int fuse_dentry_init(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return dentry->d_fsdata ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static void fuse_dentry_release(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) union fuse_dentry *fd = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) kfree_rcu(fd, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int fuse_dentry_delete(const struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return time_before64(fuse_dentry_time(dentry), get_jiffies_64());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * Create a fuse_mount object with a new superblock (with path->dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * as the root), and return that mount so it can be auto-mounted on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * @path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static struct vfsmount *fuse_dentry_automount(struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct fs_context *fsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct fuse_mount *parent_fm = get_fuse_mount_super(path->mnt->mnt_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct fuse_conn *fc = parent_fm->fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct fuse_mount *fm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct vfsmount *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (IS_ERR(fsc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) err = PTR_ERR(fsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (!fm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) goto out_put_fsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) refcount_set(&fm->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) fsc->s_fs_info = fm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) sb = sget_fc(fsc, NULL, set_anon_super_fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (IS_ERR(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) err = PTR_ERR(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) fuse_mount_put(fm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto out_put_fsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) fm->fc = fuse_conn_get(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /* Initialize superblock, making @mp_fi its root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) err = fuse_fill_super_submount(sb, mp_fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) fuse_conn_put(fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) kfree(fm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) sb->s_fs_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) goto out_put_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) down_write(&fc->killsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) list_add_tail(&fm->fc_entry, &fc->mounts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) up_write(&fc->killsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) sb->s_flags |= SB_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) fsc->root = dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * FIXME: setting SB_BORN requires a write barrier for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * super_cache_count(). We should actually come
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * up with a proper ->get_tree() implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * for submounts and call vfs_get_tree() to take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * care of the write barrier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) sb->s_flags |= SB_BORN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* We are done configuring the superblock, so unlock it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) up_write(&sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /* Create the submount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) mnt = vfs_create_mount(fsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (IS_ERR(mnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) err = PTR_ERR(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) goto out_put_fsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) mntget(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) put_fs_context(fsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) out_put_sb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * Only jump here when fsc->root is NULL and sb is still locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * (otherwise put_fs_context() will put the superblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) deactivate_locked_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) out_put_fsc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) put_fs_context(fsc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * Get the canonical path. Since we must translate to a path, this must be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * in the context of the userspace daemon, however, the userspace daemon cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * look up paths on its own. Instead, we handle the lookup as a special case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * inside of the write request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static void fuse_dentry_canonical_path(const struct path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct path *canonical_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) //struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct fuse_mount *fm = get_fuse_mount_super(path->mnt->mnt_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) char *path_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) path_name = (char *)get_zeroed_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (!path_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) goto default_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) args.opcode = FUSE_CANONICAL_PATH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) args.nodeid = get_node_id(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) args.in_numargs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) args.out_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) args.out_args[0].size = PATH_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) args.out_args[0].value = path_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) args.canonical_path = canonical_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) args.out_argvar = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) free_page((unsigned long)path_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) default_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) canonical_path->dentry = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) canonical_path->mnt = path->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) path_get(canonical_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) const struct dentry_operations fuse_dentry_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) .d_revalidate = fuse_dentry_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) .d_delete = fuse_dentry_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) #if BITS_PER_LONG < 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) .d_init = fuse_dentry_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) .d_release = fuse_dentry_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) .d_automount = fuse_dentry_automount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) .d_canonical_path = fuse_dentry_canonical_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) const struct dentry_operations fuse_root_dentry_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) #if BITS_PER_LONG < 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) .d_init = fuse_dentry_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) .d_release = fuse_dentry_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int fuse_valid_type(int m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) bool fuse_invalid_attr(struct fuse_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return !fuse_valid_type(attr->mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) attr->size > LLONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) struct fuse_entry_out *outarg, struct inode **inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct fuse_mount *fm = get_fuse_mount_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) struct fuse_forget_link *forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) u64 attr_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (name->len > FUSE_NAME_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) forget = fuse_alloc_forget();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (!forget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) attr_version = fuse_get_attr_version(fm->fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) fuse_lookup_init(fm->fc, &args, nodeid, name, outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /* Zero nodeid is same as -ENOENT, but with valid timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (err || !outarg->nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) goto out_put_forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (!outarg->nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) goto out_put_forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (fuse_invalid_attr(&outarg->attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) goto out_put_forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) &outarg->attr, entry_attr_timeout(outarg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) attr_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!*inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) out_put_forget:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) kfree(forget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct fuse_entry_out outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct dentry *newent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) bool outarg_valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) bool locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (fuse_is_bad(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) locked = fuse_lock_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) &outarg, &inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) fuse_unlock_inode(dir, locked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) outarg_valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (inode && get_node_id(inode) == FUSE_ROOT_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) goto out_iput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) newent = d_splice_alias(inode, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) err = PTR_ERR(newent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (IS_ERR(newent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) entry = newent ? newent : entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (outarg_valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) fuse_change_entry_timeout(entry, &outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) fuse_invalidate_entry_cache(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) fuse_advise_use_readdirplus(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return newent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) out_iput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * Atomic create+open operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) * If the filesystem doesn't support this, then fall back to separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * 'mknod' + 'open' requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) static int fuse_create_open(struct inode *dir, struct dentry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) struct file *file, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) struct fuse_conn *fc = get_fuse_conn(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct fuse_mount *fm = get_fuse_mount(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) struct fuse_forget_link *forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) struct fuse_create_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct fuse_open_out outopen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct fuse_entry_out outentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct fuse_inode *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct fuse_file *ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) /* Userspace expects S_IFREG in create mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) BUG_ON((mode & S_IFMT) != S_IFREG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) forget = fuse_alloc_forget();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!forget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) ff = fuse_file_alloc(fm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (!ff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) goto out_put_forget_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (!fm->fc->dont_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) mode &= ~current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) flags &= ~O_NOCTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) memset(&outentry, 0, sizeof(outentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) inarg.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) inarg.mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) inarg.umask = current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) args.opcode = FUSE_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) args.nodeid = get_node_id(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) args.in_numargs = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) args.in_args[0].size = sizeof(inarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) args.in_args[0].value = &inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) args.in_args[1].size = entry->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) args.in_args[1].value = entry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) args.out_numargs = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) args.out_args[0].size = sizeof(outentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) args.out_args[0].value = &outentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) args.out_args[1].size = sizeof(outopen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) args.out_args[1].value = &outopen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) goto out_free_ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) fuse_invalid_attr(&outentry.attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) goto out_free_ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) ff->fh = outopen.fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) ff->nodeid = outentry.nodeid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) ff->open_flags = outopen.open_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) fuse_passthrough_setup(fc, ff, &outopen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) &outentry.attr, entry_attr_timeout(&outentry), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) fuse_sync_release(NULL, ff, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) kfree(forget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) d_instantiate(entry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) fuse_change_entry_timeout(entry, &outentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) fuse_dir_changed(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) err = finish_open(file, entry, generic_file_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) fuse_sync_release(fi, ff, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) file->private_data = ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) fuse_finish_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) out_free_ff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) fuse_file_free(ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) out_put_forget_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) kfree(forget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) struct file *file, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct fuse_conn *fc = get_fuse_conn(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) struct dentry *res = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) if (fuse_is_bad(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (d_in_lookup(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) res = fuse_lookup(dir, entry, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (IS_ERR(res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return PTR_ERR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) entry = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (!(flags & O_CREAT) || d_really_is_positive(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) goto no_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /* Only creates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) file->f_mode |= FMODE_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (fc->no_create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) goto mknod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) err = fuse_create_open(dir, entry, file, flags, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (err == -ENOSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) fc->no_create = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) goto mknod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) dput(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) mknod:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) err = fuse_mknod(dir, entry, mode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) no_open:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) return finish_no_open(file, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) * Code shared between mknod, mkdir, symlink and link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) struct inode *dir, struct dentry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) struct fuse_entry_out outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) struct dentry *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) struct fuse_forget_link *forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (fuse_is_bad(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) forget = fuse_alloc_forget();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (!forget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) memset(&outarg, 0, sizeof(outarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) args->nodeid = get_node_id(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) args->out_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) args->out_args[0].size = sizeof(outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) args->out_args[0].value = &outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) err = fuse_simple_request(fm, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) goto out_put_forget_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) goto out_put_forget_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if ((outarg.attr.mode ^ mode) & S_IFMT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) goto out_put_forget_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) &outarg.attr, entry_attr_timeout(&outarg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) kfree(forget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) d_drop(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) d = d_splice_alias(inode, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (IS_ERR(d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) return PTR_ERR(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) fuse_change_entry_timeout(d, &outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) dput(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) fuse_change_entry_timeout(entry, &outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) fuse_dir_changed(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) out_put_forget_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) kfree(forget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct fuse_mknod_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct fuse_mount *fm = get_fuse_mount(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (!fm->fc->dont_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) mode &= ~current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) inarg.mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) inarg.rdev = new_encode_dev(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) inarg.umask = current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) args.opcode = FUSE_MKNOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) args.in_numargs = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) args.in_args[0].size = sizeof(inarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) args.in_args[0].value = &inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) args.in_args[1].size = entry->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) args.in_args[1].value = entry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) return create_new_entry(fm, &args, dir, entry, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) return fuse_mknod(dir, entry, mode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) struct fuse_mkdir_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) struct fuse_mount *fm = get_fuse_mount(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (!fm->fc->dont_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) mode &= ~current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) inarg.mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) inarg.umask = current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) args.opcode = FUSE_MKDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) args.in_numargs = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) args.in_args[0].size = sizeof(inarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) args.in_args[0].value = &inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) args.in_args[1].size = entry->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) args.in_args[1].value = entry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return create_new_entry(fm, &args, dir, entry, S_IFDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) static int fuse_symlink(struct inode *dir, struct dentry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) const char *link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) struct fuse_mount *fm = get_fuse_mount(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) unsigned len = strlen(link) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) args.opcode = FUSE_SYMLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) args.in_numargs = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) args.in_args[0].size = entry->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) args.in_args[0].value = entry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) args.in_args[1].size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) args.in_args[1].value = link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) return create_new_entry(fm, &args, dir, entry, S_IFLNK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) void fuse_flush_time_update(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) int err = sync_inode_metadata(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) mapping_set_error(inode->i_mapping, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) void fuse_update_ctime(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (!IS_NOCMTIME(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) mark_inode_dirty_sync(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) fuse_flush_time_update(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) static int fuse_unlink(struct inode *dir, struct dentry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) struct fuse_mount *fm = get_fuse_mount(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (fuse_is_bad(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) args.opcode = FUSE_UNLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) args.nodeid = get_node_id(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) args.in_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) args.in_args[0].size = entry->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) args.in_args[0].value = entry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) struct inode *inode = d_inode(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) spin_lock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) * If i_nlink == 0 then unlink doesn't make sense, yet this can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) * happen if userspace filesystem is careless. It would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * difficult to enforce correct nlink usage so just ignore this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * condition here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (inode->i_nlink > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) spin_unlock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) fuse_invalidate_attr(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) fuse_dir_changed(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) fuse_invalidate_entry_cache(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) fuse_update_ctime(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) } else if (err == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) fuse_invalidate_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) static int fuse_rmdir(struct inode *dir, struct dentry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) struct fuse_mount *fm = get_fuse_mount(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (fuse_is_bad(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) args.opcode = FUSE_RMDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) args.nodeid = get_node_id(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) args.in_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) args.in_args[0].size = entry->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) args.in_args[0].value = entry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) clear_nlink(d_inode(entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) fuse_dir_changed(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) fuse_invalidate_entry_cache(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) } else if (err == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) fuse_invalidate_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) struct inode *newdir, struct dentry *newent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) unsigned int flags, int opcode, size_t argsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) struct fuse_rename2_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) struct fuse_mount *fm = get_fuse_mount(olddir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) memset(&inarg, 0, argsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) inarg.newdir = get_node_id(newdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) inarg.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) args.opcode = opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) args.nodeid = get_node_id(olddir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) args.in_numargs = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) args.in_args[0].size = argsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) args.in_args[0].value = &inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) args.in_args[1].size = oldent->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) args.in_args[1].value = oldent->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) args.in_args[2].size = newent->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) args.in_args[2].value = newent->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) /* ctime changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) fuse_invalidate_attr(d_inode(oldent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) fuse_update_ctime(d_inode(oldent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (flags & RENAME_EXCHANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) fuse_invalidate_attr(d_inode(newent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) fuse_update_ctime(d_inode(newent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) fuse_dir_changed(olddir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (olddir != newdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) fuse_dir_changed(newdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) /* newent will end up negative */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) fuse_invalidate_attr(d_inode(newent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) fuse_invalidate_entry_cache(newent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) fuse_update_ctime(d_inode(newent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) } else if (err == -EINTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) /* If request was interrupted, DEITY only knows if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) rename actually took place. If the invalidation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) fails (e.g. some process has CWD under the renamed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) directory), then there can be inconsistency between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) the dcache and the real filesystem. Tough luck. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) fuse_invalidate_entry(oldent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) if (d_really_is_positive(newent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) fuse_invalidate_entry(newent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct inode *newdir, struct dentry *newent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) struct fuse_conn *fc = get_fuse_conn(olddir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (fuse_is_bad(olddir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) if (fc->no_rename2 || fc->minor < 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) FUSE_RENAME2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) sizeof(struct fuse_rename2_in));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (err == -ENOSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) fc->no_rename2 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) FUSE_RENAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) sizeof(struct fuse_rename_in));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) static int fuse_link(struct dentry *entry, struct inode *newdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) struct dentry *newent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) struct fuse_link_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) struct inode *inode = d_inode(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) struct fuse_mount *fm = get_fuse_mount(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) inarg.oldnodeid = get_node_id(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) args.opcode = FUSE_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) args.in_numargs = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) args.in_args[0].size = sizeof(inarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) args.in_args[0].value = &inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) args.in_args[1].size = newent->d_name.len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) args.in_args[1].value = newent->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) /* Contrary to "normal" filesystems it can happen that link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) makes two "logical" inodes point to the same "physical"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) inode. We invalidate the attributes of the old one, so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) will reflect changes in the backing inode (link count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) etc.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) spin_lock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (likely(inode->i_nlink < UINT_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) spin_unlock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) fuse_invalidate_attr(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) fuse_update_ctime(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) } else if (err == -EINTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) fuse_invalidate_attr(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) struct kstat *stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) unsigned int blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) /* see the comment in fuse_change_attributes() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) attr->size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) attr->mtime = inode->i_mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) attr->mtimensec = inode->i_mtime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) attr->ctime = inode->i_ctime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) attr->ctimensec = inode->i_ctime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) stat->dev = inode->i_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) stat->ino = attr->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) stat->nlink = attr->nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) stat->uid = make_kuid(fc->user_ns, attr->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) stat->gid = make_kgid(fc->user_ns, attr->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) stat->rdev = inode->i_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) stat->atime.tv_sec = attr->atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) stat->atime.tv_nsec = attr->atimensec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) stat->mtime.tv_sec = attr->mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) stat->mtime.tv_nsec = attr->mtimensec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) stat->ctime.tv_sec = attr->ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) stat->ctime.tv_nsec = attr->ctimensec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) stat->size = attr->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) stat->blocks = attr->blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (attr->blksize != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) blkbits = ilog2(attr->blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) blkbits = inode->i_sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) stat->blksize = 1 << blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) struct fuse_getattr_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) struct fuse_attr_out outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) struct fuse_mount *fm = get_fuse_mount(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) u64 attr_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) attr_version = fuse_get_attr_version(fm->fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) memset(&outarg, 0, sizeof(outarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) /* Directories have separate file-handle space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) if (file && S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) struct fuse_file *ff = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) inarg.getattr_flags |= FUSE_GETATTR_FH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) inarg.fh = ff->fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) args.opcode = FUSE_GETATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) args.nodeid = get_node_id(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) args.in_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) args.in_args[0].size = sizeof(inarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) args.in_args[0].value = &inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) args.out_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) args.out_args[0].size = sizeof(outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) args.out_args[0].value = &outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if (fuse_invalid_attr(&outarg.attr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) inode_wrong_type(inode, outarg.attr.mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) fuse_make_bad(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) fuse_change_attributes(inode, &outarg.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) attr_timeout(&outarg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) attr_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) if (stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) fuse_fillattr(inode, &outarg.attr, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) static int fuse_update_get_attr(struct inode *inode, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) struct kstat *stat, u32 request_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) bool sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (flags & AT_STATX_FORCE_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) sync = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) else if (flags & AT_STATX_DONT_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) sync = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) else if (request_mask & READ_ONCE(fi->inval_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) sync = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) sync = time_before64(fi->i_time, get_jiffies_64());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) forget_all_cached_acls(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) err = fuse_do_getattr(inode, stat, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) } else if (stat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) generic_fillattr(inode, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) stat->mode = fi->orig_i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) stat->ino = fi->orig_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) int fuse_update_attributes(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) /* Do *not* need to get atime for internal purposes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) return fuse_update_get_attr(inode, file, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) STATX_BASIC_STATS & ~STATX_ATIME, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) u64 child_nodeid, struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) int err = -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) struct inode *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) struct dentry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) parent = fuse_ilookup(fc, parent_nodeid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) inode_lock_nested(parent, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) if (!S_ISDIR(parent->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) dir = d_find_alias(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) name->hash = full_name_hash(dir, name->name, name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) entry = d_lookup(dir, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) fuse_dir_changed(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) fuse_invalidate_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (child_nodeid != 0 && d_really_is_positive(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) inode_lock(d_inode(entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) if (get_node_id(d_inode(entry)) != child_nodeid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) goto badentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (d_mountpoint(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) goto badentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) if (d_is_dir(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) shrink_dcache_parent(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) if (!simple_empty(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) err = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) goto badentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) d_inode(entry)->i_flags |= S_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) dont_mount(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) clear_nlink(d_inode(entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) badentry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) inode_unlock(d_inode(entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) d_delete(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) dput(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) inode_unlock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) iput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) * Calling into a user-controlled filesystem gives the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) * daemon ptrace-like capabilities over the current process. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) * means, that the filesystem daemon is able to record the exact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) * filesystem operations performed, and can also control the behavior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) * of the requester process in otherwise impossible ways. For example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) * it can delay the operation for arbitrary length of time allowing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) * DoS against the requester.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) * For this reason only those processes can call into the filesystem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * for which the owner of the mount has ptrace privilege. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * excludes processes started by other users, suid or sgid processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) int fuse_allow_current_process(struct fuse_conn *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) const struct cred *cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if (fc->allow_other)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) return current_in_userns(fc->user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) cred = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (uid_eq(cred->euid, fc->user_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) uid_eq(cred->suid, fc->user_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) uid_eq(cred->uid, fc->user_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) gid_eq(cred->egid, fc->group_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) gid_eq(cred->sgid, fc->group_id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) gid_eq(cred->gid, fc->group_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) static int fuse_access(struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) struct fuse_mount *fm = get_fuse_mount(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) struct fuse_access_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) BUG_ON(mask & MAY_NOT_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) if (fm->fc->no_access)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) args.opcode = FUSE_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) args.nodeid = get_node_id(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) args.in_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) args.in_args[0].size = sizeof(inarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) args.in_args[0].value = &inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if (err == -ENOSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) fm->fc->no_access = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) static int fuse_perm_getattr(struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) if (mask & MAY_NOT_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) forget_all_cached_acls(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) return fuse_do_getattr(inode, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) * Check permission. The two basic access models of FUSE are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) * 1) Local access checking ('default_permissions' mount option) based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) * on file mode. This is the plain old disk filesystem permission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) * modell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) * 2) "Remote" access checking, where server is responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) * checking permission in each inode operation. An exception to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * is if ->permission() was invoked from sys_access() in which case an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) * access request is sent. Execute permission is still checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) * locally based on file mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) static int fuse_permission(struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) bool refreshed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) if (fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) if (!fuse_allow_current_process(fc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) * If attributes are needed, refresh them before proceeding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) if (fc->default_permissions ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) if (perm_mask & READ_ONCE(fi->inval_mask) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) time_before64(fi->i_time, get_jiffies_64())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) refreshed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) err = fuse_perm_getattr(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) if (fc->default_permissions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) err = generic_permission(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) /* If permission is denied, try to refresh file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) attributes. This is also needed, because the root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) node will at first have no permissions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) if (err == -EACCES && !refreshed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) err = fuse_perm_getattr(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) err = generic_permission(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) /* Note: the opposite of the above test does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) exist. So if permissions are revoked this won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) noticed immediately, only after the attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) timeout has expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) err = fuse_access(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) if (!(inode->i_mode & S_IXUGO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) if (refreshed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) err = fuse_perm_getattr(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) if (!err && !(inode->i_mode & S_IXUGO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) static int fuse_readlink_page(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) struct fuse_mount *fm = get_fuse_mount(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) struct fuse_args_pages ap = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) .num_pages = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) .pages = &page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) .descs = &desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) char *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) ap.args.opcode = FUSE_READLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) ap.args.nodeid = get_node_id(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) ap.args.out_pages = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) ap.args.out_argvar = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) ap.args.page_zeroing = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) ap.args.out_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) ap.args.out_args[0].size = desc.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) res = fuse_simple_request(fm, &ap.args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) fuse_invalidate_atime(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (WARN_ON(res >= PAGE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) link = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) link[res] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) struct delayed_call *callback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) if (fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) if (fc->cache_symlinks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) return page_get_link(dentry, inode, callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) err = -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) page = alloc_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) err = fuse_readlink_page(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) __free_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) set_delayed_call(callback, page_put_link, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) return page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) static int fuse_dir_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) return fuse_open_common(inode, file, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) static int fuse_dir_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) fuse_release_common(file, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) if (fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) if (fc->no_fsyncdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) if (err == -ENOSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) fc->no_fsyncdir = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) if (fc->minor < 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) if (fc->minor < 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) return fuse_ioctl_common(file, cmd, arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) /* Always update if mtime is explicitly set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) if (ivalid & ATTR_MTIME_SET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) /* Or if kernel i_mtime is the official one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) if (trust_local_mtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) /* In all other cases update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) struct fuse_setattr_in *arg, bool trust_local_cmtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) unsigned ivalid = iattr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) if (ivalid & ATTR_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) if (ivalid & ATTR_UID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (ivalid & ATTR_GID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) if (ivalid & ATTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) if (ivalid & ATTR_ATIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) arg->valid |= FATTR_ATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) arg->atime = iattr->ia_atime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) arg->atimensec = iattr->ia_atime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) if (!(ivalid & ATTR_ATIME_SET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) arg->valid |= FATTR_ATIME_NOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) arg->valid |= FATTR_MTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) arg->mtime = iattr->ia_mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) arg->mtimensec = iattr->ia_mtime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) arg->valid |= FATTR_MTIME_NOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) arg->valid |= FATTR_CTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) arg->ctime = iattr->ia_ctime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) arg->ctimensec = iattr->ia_ctime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) * Prevent concurrent writepages on inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) * This is done by adding a negative bias to the inode write counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) * and waiting for all pending writes to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) void fuse_set_nowrite(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) BUG_ON(!inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) spin_lock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) BUG_ON(fi->writectr < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) fi->writectr += FUSE_NOWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) spin_unlock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) * Allow writepages on inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) * Remove the bias from the writecounter and send any queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) * writepages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) static void __fuse_release_nowrite(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) BUG_ON(fi->writectr != FUSE_NOWRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) fi->writectr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) fuse_flush_writepages(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) void fuse_release_nowrite(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) spin_lock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) __fuse_release_nowrite(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) spin_unlock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) struct fuse_setattr_in *inarg_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) struct fuse_attr_out *outarg_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) args->opcode = FUSE_SETATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) args->nodeid = get_node_id(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) args->in_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) args->in_args[0].size = sizeof(*inarg_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) args->in_args[0].value = inarg_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) args->out_numargs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) args->out_args[0].size = sizeof(*outarg_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) args->out_args[0].value = outarg_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) * Flush inode->i_mtime to the server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) struct fuse_mount *fm = get_fuse_mount(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) struct fuse_setattr_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) struct fuse_attr_out outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) memset(&outarg, 0, sizeof(outarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) inarg.valid = FATTR_MTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) inarg.mtime = inode->i_mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) inarg.mtimensec = inode->i_mtime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (fm->fc->minor >= 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) inarg.valid |= FATTR_CTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) inarg.ctime = inode->i_ctime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) inarg.ctimensec = inode->i_ctime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) if (ff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) inarg.valid |= FATTR_FH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) inarg.fh = ff->fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) return fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) * Set attributes, and at the same time refresh them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) * Truncation is slightly complicated, because the 'truncate' request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) * may fail, in which case we don't want to touch the mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) * vmtruncate() doesn't allow for this case, so do the rlimit checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) * and the actual truncation by hand.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) struct fuse_mount *fm = get_fuse_mount(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) struct fuse_conn *fc = fm->fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) FUSE_ARGS(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) struct fuse_setattr_in inarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) struct fuse_attr_out outarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) bool is_truncate = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) bool is_wb = fc->writeback_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) loff_t oldsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) bool fault_blocked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (!fc->default_permissions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) attr->ia_valid |= ATTR_FORCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) err = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) if (WARN_ON(!S_ISREG(inode->i_mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) is_truncate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) if (FUSE_IS_DAX(inode) && is_truncate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) down_write(&fi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) fault_blocked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) err = fuse_dax_break_layouts(inode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) up_write(&fi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) if (attr->ia_valid & ATTR_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) /* This is coming from open(..., ... | O_TRUNC); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) WARN_ON(!(attr->ia_valid & ATTR_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) WARN_ON(attr->ia_size != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) if (fc->atomic_o_trunc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) * No need to send request to userspace, since actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) * truncation has already been done by OPEN. But still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) * need to truncate page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) truncate_pagecache(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) /* Flush dirty data/metadata before non-truncate SETATTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) if (is_wb && S_ISREG(inode->i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) attr->ia_valid &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) ATTR_TIMES_SET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) err = write_inode_now(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) fuse_set_nowrite(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) fuse_release_nowrite(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) if (is_truncate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) fuse_set_nowrite(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) if (trust_local_cmtime && attr->ia_size != inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) memset(&inarg, 0, sizeof(inarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) memset(&outarg, 0, sizeof(outarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) struct fuse_file *ff = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) inarg.valid |= FATTR_FH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) inarg.fh = ff->fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) /* For mandatory locking in truncate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) inarg.valid |= FATTR_LOCKOWNER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) err = fuse_simple_request(fm, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) if (err == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) fuse_invalidate_attr(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) if (fuse_invalid_attr(&outarg.attr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) inode_wrong_type(inode, outarg.attr.mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) fuse_make_bad(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) spin_lock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) /* the kernel maintains i_mtime locally */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) if (trust_local_cmtime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) if (attr->ia_valid & ATTR_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) inode->i_mtime = attr->ia_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) if (attr->ia_valid & ATTR_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) inode->i_ctime = attr->ia_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) /* FIXME: clear I_DIRTY_SYNC? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) fuse_change_attributes_common(inode, &outarg.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) attr_timeout(&outarg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) oldsize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) /* see the comment in fuse_change_attributes() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) i_size_write(inode, outarg.attr.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) if (is_truncate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) /* NOTE: this may release/reacquire fi->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) __fuse_release_nowrite(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) spin_unlock(&fi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) * Only call invalidate_inode_pages2() after removing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if ((is_truncate || !is_wb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) truncate_pagecache(inode, outarg.attr.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) invalidate_inode_pages2(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) if (fault_blocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) up_write(&fi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) if (is_truncate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) fuse_release_nowrite(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) if (fault_blocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) up_write(&fi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) static int fuse_setattr(struct dentry *entry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) struct inode *inode = d_inode(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) if (fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) if (!fuse_allow_current_process(get_fuse_conn(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) ATTR_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) * The only sane way to reliably kill suid/sgid is to do it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) * the userspace filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) * This should be done on write(), truncate() and chown().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) if (!fc->handle_killpriv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) * ia_mode calculation may have used stale i_mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) * Refresh and recalculate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) ret = fuse_do_getattr(inode, NULL, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) attr->ia_mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) if (inode->i_mode & S_ISUID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) attr->ia_valid |= ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) attr->ia_mode &= ~S_ISUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) attr->ia_valid |= ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) attr->ia_mode &= ~S_ISGID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) if (!attr->ia_valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) ret = fuse_do_setattr(entry, attr, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) * If filesystem supports acls it may have updated acl xattrs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) * the filesystem, so forget cached acls for the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) if (fc->posix_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) forget_all_cached_acls(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) /* Directory mode changed, may need to revalidate access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) fuse_invalidate_entry_cache(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) static int fuse_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) if (fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) if (!fuse_allow_current_process(fc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (!request_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) * If user explicitly requested *nothing* then don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) * error out, but return st_dev only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) stat->result_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) stat->dev = inode->i_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) static const struct inode_operations fuse_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) .lookup = fuse_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) .mkdir = fuse_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) .symlink = fuse_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) .unlink = fuse_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) .rmdir = fuse_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) .rename = fuse_rename2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) .link = fuse_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) .setattr = fuse_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) .create = fuse_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) .atomic_open = fuse_atomic_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) .mknod = fuse_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) .permission = fuse_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) .getattr = fuse_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) .listxattr = fuse_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) .get_acl = fuse_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) .set_acl = fuse_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) static const struct file_operations fuse_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) .iterate_shared = fuse_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) .open = fuse_dir_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) .release = fuse_dir_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) .fsync = fuse_dir_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) .unlocked_ioctl = fuse_dir_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) .compat_ioctl = fuse_dir_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) static const struct inode_operations fuse_common_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) .setattr = fuse_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) .permission = fuse_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) .getattr = fuse_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) .listxattr = fuse_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) .get_acl = fuse_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) .set_acl = fuse_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) static const struct inode_operations fuse_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) .setattr = fuse_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) .get_link = fuse_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) .getattr = fuse_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) .listxattr = fuse_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) void fuse_init_common(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) inode->i_op = &fuse_common_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) void fuse_init_dir(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) struct fuse_inode *fi = get_fuse_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) inode->i_op = &fuse_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) inode->i_fop = &fuse_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) spin_lock_init(&fi->rdc.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) fi->rdc.cached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) fi->rdc.size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) fi->rdc.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) fi->rdc.version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) static int fuse_symlink_readpage(struct file *null, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) int err = fuse_readlink_page(page->mapping->host, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) static const struct address_space_operations fuse_symlink_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) .readpage = fuse_symlink_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) void fuse_init_symlink(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) inode->i_op = &fuse_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) inode->i_data.a_ops = &fuse_symlink_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) }