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