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