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