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