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