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