Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Copyright (C) 2011 Novell Inc. |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 7 | #include <linux/module.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 8 | #include <linux/fs.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/file.h> |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 11 | #include <linux/fileattr.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 12 | #include <linux/splice.h> |
| 13 | #include <linux/xattr.h> |
| 14 | #include <linux/security.h> |
| 15 | #include <linux/uaccess.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 17 | #include <linux/cred.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 18 | #include <linux/namei.h> |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 19 | #include <linux/fdtable.h> |
| 20 | #include <linux/ratelimit.h> |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 21 | #include <linux/exportfs.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | #include "overlayfs.h" |
| 23 | |
| 24 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) |
| 25 | |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 26 | static int ovl_ccup_set(const char *buf, const struct kernel_param *param) |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 27 | { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 28 | pr_warn("\"check_copy_up\" module option is obsolete\n"); |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 29 | return 0; |
| 30 | } |
| 31 | |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 32 | static int ovl_ccup_get(char *buf, const struct kernel_param *param) |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 33 | { |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 34 | return sprintf(buf, "N\n"); |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 37 | module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644); |
Nicolas Schier | 253e748 | 2019-06-17 09:39:00 +0200 | [diff] [blame] | 38 | MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing"); |
Miklos Szeredi | 670c232 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 39 | |
Miklos Szeredi | c61ca55 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 40 | static bool ovl_must_copy_xattr(const char *name) |
| 41 | { |
| 42 | return !strcmp(name, XATTR_POSIX_ACL_ACCESS) || |
| 43 | !strcmp(name, XATTR_POSIX_ACL_DEFAULT) || |
| 44 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN); |
| 45 | } |
| 46 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 47 | int ovl_copy_xattr(struct super_block *sb, struct dentry *old, |
| 48 | struct dentry *new) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 49 | { |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 50 | ssize_t list_size, size, value_size = 0; |
| 51 | char *buf, *name, *value = NULL; |
Yuxuan Shui | 520da69 | 2020-05-27 04:08:02 +0100 | [diff] [blame] | 52 | int error = 0; |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 53 | size_t slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 54 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 55 | if (!(old->d_inode->i_opflags & IOP_XATTR) || |
| 56 | !(new->d_inode->i_opflags & IOP_XATTR)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | |
| 59 | list_size = vfs_listxattr(old, NULL, 0); |
| 60 | if (list_size <= 0) { |
| 61 | if (list_size == -EOPNOTSUPP) |
| 62 | return 0; |
| 63 | return list_size; |
| 64 | } |
| 65 | |
Miklos Szeredi | f945ca1 | 2021-07-22 14:18:14 +0200 | [diff] [blame] | 66 | buf = kvzalloc(list_size, GFP_KERNEL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 67 | if (!buf) |
| 68 | return -ENOMEM; |
| 69 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 70 | list_size = vfs_listxattr(old, buf, list_size); |
| 71 | if (list_size <= 0) { |
| 72 | error = list_size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 73 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 76 | for (name = buf; list_size; name += slen) { |
| 77 | slen = strnlen(name, list_size) + 1; |
| 78 | |
| 79 | /* underlying fs providing us with an broken xattr list? */ |
| 80 | if (WARN_ON(slen > list_size)) { |
| 81 | error = -EIO; |
| 82 | break; |
| 83 | } |
| 84 | list_size -= slen; |
| 85 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 86 | if (ovl_is_private_xattr(sb, name)) |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 87 | continue; |
Amir Goldstein | 03fedf9 | 2020-12-19 12:16:08 +0200 | [diff] [blame] | 88 | |
| 89 | error = security_inode_copy_up_xattr(name); |
| 90 | if (error < 0 && error != -EOPNOTSUPP) |
| 91 | break; |
| 92 | if (error == 1) { |
| 93 | error = 0; |
| 94 | continue; /* Discard */ |
| 95 | } |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 96 | retry: |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 97 | size = vfs_getxattr(&init_user_ns, old, name, value, value_size); |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 98 | if (size == -ERANGE) |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 99 | size = vfs_getxattr(&init_user_ns, old, name, NULL, 0); |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 100 | |
Miklos Szeredi | 97daf8b | 2015-11-10 17:08:41 +0100 | [diff] [blame] | 101 | if (size < 0) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 102 | error = size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 103 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 104 | } |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 105 | |
| 106 | if (size > value_size) { |
| 107 | void *new; |
| 108 | |
Miklos Szeredi | f945ca1 | 2021-07-22 14:18:14 +0200 | [diff] [blame] | 109 | new = kvmalloc(size, GFP_KERNEL); |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 110 | if (!new) { |
| 111 | error = -ENOMEM; |
| 112 | break; |
| 113 | } |
Miklos Szeredi | f945ca1 | 2021-07-22 14:18:14 +0200 | [diff] [blame] | 114 | kvfree(value); |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 115 | value = new; |
| 116 | value_size = size; |
| 117 | goto retry; |
| 118 | } |
| 119 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 120 | error = vfs_setxattr(&init_user_ns, new, name, value, size, 0); |
Miklos Szeredi | c61ca55 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 121 | if (error) { |
| 122 | if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name)) |
| 123 | break; |
| 124 | |
| 125 | /* Ignore failure to copy unknown xattrs */ |
| 126 | error = 0; |
| 127 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 128 | } |
Miklos Szeredi | f945ca1 | 2021-07-22 14:18:14 +0200 | [diff] [blame] | 129 | kvfree(value); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 130 | out: |
Miklos Szeredi | f945ca1 | 2021-07-22 14:18:14 +0200 | [diff] [blame] | 131 | kvfree(buf); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 132 | return error; |
| 133 | } |
| 134 | |
Amir Goldstein | 096a218 | 2021-06-19 12:26:19 +0300 | [diff] [blame] | 135 | static int ovl_copy_fileattr(struct inode *inode, struct path *old, |
| 136 | struct path *new) |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 137 | { |
| 138 | struct fileattr oldfa = { .flags_valid = true }; |
| 139 | struct fileattr newfa = { .flags_valid = true }; |
| 140 | int err; |
| 141 | |
| 142 | err = ovl_real_fileattr_get(old, &oldfa); |
Miklos Szeredi | 5b0a414 | 2021-11-04 14:04:52 +0100 | [diff] [blame] | 143 | if (err) { |
| 144 | /* Ntfs-3g returns -EINVAL for "no fileattr support" */ |
| 145 | if (err == -ENOTTY || err == -EINVAL) |
| 146 | return 0; |
| 147 | pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n", |
Christoph Fritz | 4ee7e4a | 2022-01-12 19:33:21 +0100 | [diff] [blame] | 148 | old->dentry, err); |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 149 | return err; |
Miklos Szeredi | 5b0a414 | 2021-11-04 14:04:52 +0100 | [diff] [blame] | 150 | } |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 151 | |
Amir Goldstein | 096a218 | 2021-06-19 12:26:19 +0300 | [diff] [blame] | 152 | /* |
| 153 | * We cannot set immutable and append-only flags on upper inode, |
| 154 | * because we would not be able to link upper inode to upper dir |
| 155 | * not set overlay private xattr on upper inode. |
| 156 | * Store these flags in overlay.protattr xattr instead. |
| 157 | */ |
| 158 | if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) { |
| 159 | err = ovl_set_protattr(inode, new->dentry, &oldfa); |
Miklos Szeredi | 94fd197 | 2022-01-14 16:57:56 +0100 | [diff] [blame] | 160 | if (err == -EPERM) |
| 161 | pr_warn_once("copying fileattr: no xattr on upper\n"); |
| 162 | else if (err) |
Amir Goldstein | 096a218 | 2021-06-19 12:26:19 +0300 | [diff] [blame] | 163 | return err; |
| 164 | } |
| 165 | |
Miklos Szeredi | 5b0a414 | 2021-11-04 14:04:52 +0100 | [diff] [blame] | 166 | /* Don't bother copying flags if none are set */ |
| 167 | if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK)) |
| 168 | return 0; |
| 169 | |
| 170 | err = ovl_real_fileattr_get(new, &newfa); |
| 171 | if (err) { |
Miklos Szeredi | 94fd197 | 2022-01-14 16:57:56 +0100 | [diff] [blame] | 172 | /* |
| 173 | * Returning an error if upper doesn't support fileattr will |
| 174 | * result in a regression, so revert to the old behavior. |
| 175 | */ |
| 176 | if (err == -ENOTTY || err == -EINVAL) { |
| 177 | pr_warn_once("copying fileattr: no support on upper\n"); |
| 178 | return 0; |
| 179 | } |
Miklos Szeredi | 5b0a414 | 2021-11-04 14:04:52 +0100 | [diff] [blame] | 180 | pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n", |
Christoph Fritz | 4ee7e4a | 2022-01-12 19:33:21 +0100 | [diff] [blame] | 181 | new->dentry, err); |
Miklos Szeredi | 5b0a414 | 2021-11-04 14:04:52 +0100 | [diff] [blame] | 182 | return err; |
| 183 | } |
| 184 | |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 185 | BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL); |
| 186 | newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK; |
| 187 | newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK); |
| 188 | |
| 189 | BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON); |
| 190 | newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK; |
| 191 | newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK); |
| 192 | |
| 193 | return ovl_real_fileattr_set(new, &newfa); |
| 194 | } |
| 195 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 196 | static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old, |
| 197 | struct path *new, loff_t len) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 198 | { |
| 199 | struct file *old_file; |
| 200 | struct file *new_file; |
| 201 | loff_t old_pos = 0; |
| 202 | loff_t new_pos = 0; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 203 | loff_t cloned; |
Chengguang Xu | b504c65 | 2019-11-01 20:35:51 +0800 | [diff] [blame] | 204 | loff_t data_pos = -1; |
| 205 | loff_t hole_len; |
| 206 | bool skip_hole = false; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 207 | int error = 0; |
| 208 | |
| 209 | if (len == 0) |
| 210 | return 0; |
| 211 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 212 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 213 | if (IS_ERR(old_file)) |
| 214 | return PTR_ERR(old_file); |
| 215 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 216 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 217 | if (IS_ERR(new_file)) { |
| 218 | error = PTR_ERR(new_file); |
| 219 | goto out_fput; |
| 220 | } |
| 221 | |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 222 | /* Try to use clone_file_range to clone up within the same fs */ |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 223 | cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0); |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 224 | if (cloned == len) |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 225 | goto out; |
| 226 | /* Couldn't clone, so now we try to copy the data */ |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 227 | |
Chengguang Xu | b504c65 | 2019-11-01 20:35:51 +0800 | [diff] [blame] | 228 | /* Check if lower fs supports seek operation */ |
| 229 | if (old_file->f_mode & FMODE_LSEEK && |
| 230 | old_file->f_op->llseek) |
| 231 | skip_hole = true; |
| 232 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 233 | while (len) { |
| 234 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; |
| 235 | long bytes; |
| 236 | |
| 237 | if (len < this_len) |
| 238 | this_len = len; |
| 239 | |
| 240 | if (signal_pending_state(TASK_KILLABLE, current)) { |
| 241 | error = -EINTR; |
| 242 | break; |
| 243 | } |
| 244 | |
Chengguang Xu | b504c65 | 2019-11-01 20:35:51 +0800 | [diff] [blame] | 245 | /* |
| 246 | * Fill zero for hole will cost unnecessary disk space |
| 247 | * and meanwhile slow down the copy-up speed, so we do |
| 248 | * an optimization for hole during copy-up, it relies |
| 249 | * on SEEK_DATA implementation in lower fs so if lower |
| 250 | * fs does not support it, copy-up will behave as before. |
| 251 | * |
| 252 | * Detail logic of hole detection as below: |
| 253 | * When we detect next data position is larger than current |
| 254 | * position we will skip that hole, otherwise we copy |
| 255 | * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually, |
| 256 | * it may not recognize all kind of holes and sometimes |
| 257 | * only skips partial of hole area. However, it will be |
| 258 | * enough for most of the use cases. |
| 259 | */ |
| 260 | |
| 261 | if (skip_hole && data_pos < old_pos) { |
| 262 | data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA); |
| 263 | if (data_pos > old_pos) { |
| 264 | hole_len = data_pos - old_pos; |
| 265 | len -= hole_len; |
| 266 | old_pos = new_pos = data_pos; |
| 267 | continue; |
| 268 | } else if (data_pos == -ENXIO) { |
| 269 | break; |
| 270 | } else if (data_pos < 0) { |
| 271 | skip_hole = false; |
| 272 | } |
| 273 | } |
| 274 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 275 | bytes = do_splice_direct(old_file, &old_pos, |
| 276 | new_file, &new_pos, |
| 277 | this_len, SPLICE_F_MOVE); |
| 278 | if (bytes <= 0) { |
| 279 | error = bytes; |
| 280 | break; |
| 281 | } |
| 282 | WARN_ON(old_pos != new_pos); |
| 283 | |
| 284 | len -= bytes; |
| 285 | } |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 286 | out: |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 287 | if (!error && ovl_should_sync(ofs)) |
Miklos Szeredi | 641089c | 2016-10-31 14:42:14 +0100 | [diff] [blame] | 288 | error = vfs_fsync(new_file, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 289 | fput(new_file); |
| 290 | out_fput: |
| 291 | fput(old_file); |
| 292 | return error; |
| 293 | } |
| 294 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 295 | static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat) |
| 296 | { |
| 297 | struct iattr attr = { |
| 298 | .ia_valid = ATTR_SIZE, |
| 299 | .ia_size = stat->size, |
| 300 | }; |
| 301 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 302 | return notify_change(&init_user_ns, upperdentry, &attr, NULL); |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 305 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
| 306 | { |
| 307 | struct iattr attr = { |
| 308 | .ia_valid = |
| 309 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, |
| 310 | .ia_atime = stat->atime, |
| 311 | .ia_mtime = stat->mtime, |
| 312 | }; |
| 313 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 314 | return notify_change(&init_user_ns, upperdentry, &attr, NULL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) |
| 318 | { |
| 319 | int err = 0; |
| 320 | |
| 321 | if (!S_ISLNK(stat->mode)) { |
| 322 | struct iattr attr = { |
| 323 | .ia_valid = ATTR_MODE, |
| 324 | .ia_mode = stat->mode, |
| 325 | }; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 326 | err = notify_change(&init_user_ns, upperdentry, &attr, NULL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 327 | } |
| 328 | if (!err) { |
| 329 | struct iattr attr = { |
| 330 | .ia_valid = ATTR_UID | ATTR_GID, |
| 331 | .ia_uid = stat->uid, |
| 332 | .ia_gid = stat->gid, |
| 333 | }; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 334 | err = notify_change(&init_user_ns, upperdentry, &attr, NULL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 335 | } |
| 336 | if (!err) |
| 337 | ovl_set_timestamps(upperdentry, stat); |
| 338 | |
| 339 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 342 | struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real, |
| 343 | bool is_upper) |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 344 | { |
| 345 | struct ovl_fh *fh; |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 346 | int fh_type, dwords; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 347 | int buflen = MAX_HANDLE_SZ; |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 348 | uuid_t *uuid = &real->d_sb->s_uuid; |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 349 | int err; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 350 | |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 351 | /* Make sure the real fid stays 32bit aligned */ |
| 352 | BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4); |
| 353 | BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255); |
| 354 | |
| 355 | fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL); |
| 356 | if (!fh) |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 357 | return ERR_PTR(-ENOMEM); |
| 358 | |
| 359 | /* |
| 360 | * We encode a non-connectable file handle for non-dir, because we |
| 361 | * only need to find the lower inode number and we don't want to pay |
| 362 | * the price or reconnecting the dentry. |
| 363 | */ |
| 364 | dwords = buflen >> 2; |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 365 | fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 366 | buflen = (dwords << 2); |
| 367 | |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 368 | err = -EIO; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 369 | if (WARN_ON(fh_type < 0) || |
| 370 | WARN_ON(buflen > MAX_HANDLE_SZ) || |
| 371 | WARN_ON(fh_type == FILEID_INVALID)) |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 372 | goto out_err; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 373 | |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 374 | fh->fb.version = OVL_FH_VERSION; |
| 375 | fh->fb.magic = OVL_FH_MAGIC; |
| 376 | fh->fb.type = fh_type; |
| 377 | fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN; |
Amir Goldstein | 54fb347 | 2017-06-21 15:28:38 +0300 | [diff] [blame] | 378 | /* |
| 379 | * When we will want to decode an overlay dentry from this handle |
| 380 | * and all layers are on the same fs, if we get a disconncted real |
| 381 | * dentry when we decode fid, the only way to tell if we should assign |
| 382 | * it to upperdentry or to lowerstack is by checking this flag. |
| 383 | */ |
| 384 | if (is_upper) |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 385 | fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER; |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 386 | fh->fb.len = sizeof(fh->fb) + buflen; |
Pavel Tikhomirov | 5830fb6 | 2020-10-13 17:59:54 +0300 | [diff] [blame] | 387 | if (ofs->config.uuid) |
| 388 | fh->fb.uuid = *uuid; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 389 | |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 390 | return fh; |
Amir Goldstein | ec7bbb53 | 2019-11-15 13:33:04 +0200 | [diff] [blame] | 391 | |
| 392 | out_err: |
| 393 | kfree(fh); |
| 394 | return ERR_PTR(err); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 395 | } |
| 396 | |
Amir Goldstein | a0c236b | 2021-06-19 12:26:17 +0300 | [diff] [blame] | 397 | int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower, |
| 398 | struct dentry *upper) |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 399 | { |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 400 | const struct ovl_fh *fh = NULL; |
| 401 | int err; |
| 402 | |
| 403 | /* |
| 404 | * When lower layer doesn't support export operations store a 'null' fh, |
| 405 | * so we can use the overlay.origin xattr to distignuish between a copy |
| 406 | * up and a pure upper inode. |
| 407 | */ |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 408 | if (ovl_can_decode_fh(lower->d_sb)) { |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 409 | fh = ovl_encode_real_fh(ofs, lower, false); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 410 | if (IS_ERR(fh)) |
| 411 | return PTR_ERR(fh); |
| 412 | } |
| 413 | |
Miklos Szeredi | 6266d46 | 2017-05-18 16:11:24 +0200 | [diff] [blame] | 414 | /* |
| 415 | * Do not fail when upper doesn't support xattrs. |
| 416 | */ |
Amir Goldstein | a0c236b | 2021-06-19 12:26:17 +0300 | [diff] [blame] | 417 | err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf, |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 418 | fh ? fh->fb.len : 0, 0); |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 419 | kfree(fh); |
| 420 | |
Miklos Szeredi | 6939f97 | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 421 | /* Ignore -EPERM from setting "user.*" on symlink/special */ |
| 422 | return err == -EPERM ? 0 : err; |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 423 | } |
| 424 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 425 | /* Store file handle of @upper dir in @index dir entry */ |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 426 | static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper, |
| 427 | struct dentry *index) |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 428 | { |
| 429 | const struct ovl_fh *fh; |
| 430 | int err; |
| 431 | |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 432 | fh = ovl_encode_real_fh(ofs, upper, true); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 433 | if (IS_ERR(fh)) |
| 434 | return PTR_ERR(fh); |
| 435 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 436 | err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 437 | |
| 438 | kfree(fh); |
| 439 | return err; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Create and install index entry. |
| 444 | * |
| 445 | * Caller must hold i_mutex on indexdir. |
| 446 | */ |
| 447 | static int ovl_create_index(struct dentry *dentry, struct dentry *origin, |
| 448 | struct dentry *upper) |
| 449 | { |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 450 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 451 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
| 452 | struct inode *dir = d_inode(indexdir); |
| 453 | struct dentry *index = NULL; |
| 454 | struct dentry *temp = NULL; |
| 455 | struct qstr name = { }; |
| 456 | int err; |
| 457 | |
| 458 | /* |
| 459 | * For now this is only used for creating index entry for directories, |
| 460 | * because non-dir are copied up directly to index and then hardlinked |
| 461 | * to upper dir. |
| 462 | * |
| 463 | * TODO: implement create index for non-dir, so we can call it when |
| 464 | * encoding file handle for non-dir in case index does not exist. |
| 465 | */ |
| 466 | if (WARN_ON(!d_is_dir(dentry))) |
| 467 | return -EIO; |
| 468 | |
| 469 | /* Directory not expected to be indexed before copy up */ |
| 470 | if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) |
| 471 | return -EIO; |
| 472 | |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 473 | err = ovl_get_index_name(ofs, origin, &name); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 474 | if (err) |
| 475 | return err; |
| 476 | |
Amir Goldstein | 137ec52 | 2018-05-16 17:51:25 +0300 | [diff] [blame] | 477 | temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0)); |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 478 | err = PTR_ERR(temp); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 479 | if (IS_ERR(temp)) |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 480 | goto free_name; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 481 | |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 482 | err = ovl_set_upper_fh(ofs, upper, temp); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 483 | if (err) |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 484 | goto out; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 485 | |
| 486 | index = lookup_one_len(name.name, indexdir, name.len); |
| 487 | if (IS_ERR(index)) { |
| 488 | err = PTR_ERR(index); |
| 489 | } else { |
| 490 | err = ovl_do_rename(dir, temp, dir, index, 0); |
| 491 | dput(index); |
| 492 | } |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 493 | out: |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 494 | if (err) |
| 495 | ovl_cleanup(dir, temp); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 496 | dput(temp); |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 497 | free_name: |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 498 | kfree(name.name); |
| 499 | return err; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 502 | struct ovl_copy_up_ctx { |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 503 | struct dentry *parent; |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 504 | struct dentry *dentry; |
| 505 | struct path lowerpath; |
| 506 | struct kstat stat; |
| 507 | struct kstat pstat; |
| 508 | const char *link; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 509 | struct dentry *destdir; |
| 510 | struct qstr destname; |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 511 | struct dentry *workdir; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 512 | bool origin; |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 513 | bool indexed; |
Vivek Goyal | 44d5bf1 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 514 | bool metacopy; |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 515 | }; |
| 516 | |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 517 | static int ovl_link_up(struct ovl_copy_up_ctx *c) |
| 518 | { |
| 519 | int err; |
| 520 | struct dentry *upper; |
| 521 | struct dentry *upperdir = ovl_dentry_upper(c->parent); |
| 522 | struct inode *udir = d_inode(upperdir); |
| 523 | |
| 524 | /* Mark parent "impure" because it may now contain non-pure upper */ |
| 525 | err = ovl_set_impure(c->parent, upperdir); |
| 526 | if (err) |
| 527 | return err; |
| 528 | |
| 529 | err = ovl_set_nlink_lower(c->dentry); |
| 530 | if (err) |
| 531 | return err; |
| 532 | |
| 533 | inode_lock_nested(udir, I_MUTEX_PARENT); |
| 534 | upper = lookup_one_len(c->dentry->d_name.name, upperdir, |
| 535 | c->dentry->d_name.len); |
| 536 | err = PTR_ERR(upper); |
| 537 | if (!IS_ERR(upper)) { |
Amir Goldstein | 6cf0076 | 2018-05-16 17:04:00 +0300 | [diff] [blame] | 538 | err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper); |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 539 | dput(upper); |
| 540 | |
| 541 | if (!err) { |
| 542 | /* Restore timestamps on parent (best effort) */ |
| 543 | ovl_set_timestamps(upperdir, &c->pstat); |
| 544 | ovl_dentry_set_upper_alias(c->dentry); |
| 545 | } |
| 546 | } |
| 547 | inode_unlock(udir); |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 548 | if (err) |
| 549 | return err; |
| 550 | |
| 551 | err = ovl_set_nlink_upper(c->dentry); |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 552 | |
| 553 | return err; |
| 554 | } |
| 555 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 556 | static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp) |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 557 | { |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 558 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 559 | struct inode *inode = d_inode(c->dentry); |
| 560 | struct path upperpath, datapath; |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 561 | int err; |
| 562 | |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 563 | ovl_path_upper(c->dentry, &upperpath); |
| 564 | if (WARN_ON(upperpath.dentry != NULL)) |
| 565 | return -EIO; |
| 566 | |
| 567 | upperpath.dentry = temp; |
| 568 | |
Vivek Goyal | 5f32879 | 2019-01-11 19:37:00 +0100 | [diff] [blame] | 569 | /* |
| 570 | * Copy up data first and then xattrs. Writing data after |
| 571 | * xattrs will remove security.capability xattr automatically. |
| 572 | */ |
| 573 | if (S_ISREG(c->stat.mode) && !c->metacopy) { |
Vivek Goyal | 5f32879 | 2019-01-11 19:37:00 +0100 | [diff] [blame] | 574 | ovl_path_lowerdata(c->dentry, &datapath); |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 575 | err = ovl_copy_up_data(ofs, &datapath, &upperpath, |
| 576 | c->stat.size); |
Vivek Goyal | 5f32879 | 2019-01-11 19:37:00 +0100 | [diff] [blame] | 577 | if (err) |
| 578 | return err; |
| 579 | } |
| 580 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 581 | err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp); |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 582 | if (err) |
| 583 | return err; |
| 584 | |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 585 | if (inode->i_flags & OVL_COPY_I_FLAGS_MASK) { |
| 586 | /* |
| 587 | * Copy the fileattr inode flags that are the source of already |
| 588 | * copied i_flags |
| 589 | */ |
Amir Goldstein | 096a218 | 2021-06-19 12:26:19 +0300 | [diff] [blame] | 590 | err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath); |
Amir Goldstein | 72db821 | 2021-06-19 12:26:18 +0300 | [diff] [blame] | 591 | if (err) |
| 592 | return err; |
| 593 | } |
| 594 | |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 595 | /* |
| 596 | * Store identifier of lower inode in upper inode xattr to |
| 597 | * allow lookup of the copy up origin inode. |
| 598 | * |
| 599 | * Don't set origin when we are breaking the association with a lower |
| 600 | * hard link. |
| 601 | */ |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 602 | if (c->origin) { |
Amir Goldstein | a0c236b | 2021-06-19 12:26:17 +0300 | [diff] [blame] | 603 | err = ovl_set_origin(ofs, c->lowerpath.dentry, temp); |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 604 | if (err) |
| 605 | return err; |
| 606 | } |
| 607 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 608 | if (c->metacopy) { |
Amir Goldstein | a0c236b | 2021-06-19 12:26:17 +0300 | [diff] [blame] | 609 | err = ovl_check_setxattr(ofs, temp, OVL_XATTR_METACOPY, |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 610 | NULL, 0, -EOPNOTSUPP); |
| 611 | if (err) |
| 612 | return err; |
| 613 | } |
| 614 | |
Vivek Goyal | bd64e57 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 615 | inode_lock(temp->d_inode); |
Chengguang Xu | b504c65 | 2019-11-01 20:35:51 +0800 | [diff] [blame] | 616 | if (S_ISREG(c->stat.mode)) |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 617 | err = ovl_set_size(temp, &c->stat); |
| 618 | if (!err) |
| 619 | err = ovl_set_attr(temp, &c->stat); |
Vivek Goyal | bd64e57 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 620 | inode_unlock(temp->d_inode); |
| 621 | |
| 622 | return err; |
Amir Goldstein | 02209d1 | 2017-05-19 15:16:21 +0300 | [diff] [blame] | 623 | } |
| 624 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 625 | struct ovl_cu_creds { |
| 626 | const struct cred *old; |
| 627 | struct cred *new; |
| 628 | }; |
| 629 | |
| 630 | static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc) |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 631 | { |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 632 | int err; |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 633 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 634 | cc->old = cc->new = NULL; |
| 635 | err = security_inode_copy_up(dentry, &cc->new); |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 636 | if (err < 0) |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 637 | return err; |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 638 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 639 | if (cc->new) |
| 640 | cc->old = override_creds(cc->new); |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 641 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 642 | return 0; |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 643 | } |
| 644 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 645 | static void ovl_revert_cu_creds(struct ovl_cu_creds *cc) |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 646 | { |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 647 | if (cc->new) { |
| 648 | revert_creds(cc->old); |
| 649 | put_cred(cc->new); |
| 650 | } |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Copyup using workdir to prepare temp file. Used when copying up directories, |
| 655 | * special files or when upper fs doesn't support O_TMPFILE. |
| 656 | */ |
| 657 | static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) |
| 658 | { |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 659 | struct inode *inode; |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 660 | struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir); |
| 661 | struct dentry *temp, *upper; |
| 662 | struct ovl_cu_creds cc; |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 663 | int err; |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 664 | struct ovl_cattr cattr = { |
| 665 | /* Can't properly set mode on creation because of the umask */ |
| 666 | .mode = c->stat.mode & S_IFMT, |
| 667 | .rdev = c->stat.rdev, |
| 668 | .link = c->link |
| 669 | }; |
Miklos Szeredi | 7d90b85 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 670 | |
Amir Goldstein | 773cb4c | 2020-04-03 08:43:12 +0300 | [diff] [blame] | 671 | /* workdir and destdir could be the same when copying up to indexdir */ |
| 672 | err = -EIO; |
| 673 | if (lock_rename(c->workdir, c->destdir) != NULL) |
| 674 | goto unlock; |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 675 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 676 | err = ovl_prep_cu_creds(c->dentry, &cc); |
| 677 | if (err) |
| 678 | goto unlock; |
| 679 | |
| 680 | temp = ovl_create_temp(c->workdir, &cattr); |
| 681 | ovl_revert_cu_creds(&cc); |
| 682 | |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 683 | err = PTR_ERR(temp); |
| 684 | if (IS_ERR(temp)) |
| 685 | goto unlock; |
| 686 | |
| 687 | err = ovl_copy_up_inode(c, temp); |
| 688 | if (err) |
| 689 | goto cleanup; |
| 690 | |
| 691 | if (S_ISDIR(c->stat.mode) && c->indexed) { |
| 692 | err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp); |
| 693 | if (err) |
| 694 | goto cleanup; |
| 695 | } |
| 696 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 697 | upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); |
| 698 | err = PTR_ERR(upper); |
| 699 | if (IS_ERR(upper)) |
| 700 | goto cleanup; |
| 701 | |
| 702 | err = ovl_do_rename(wdir, temp, udir, upper, 0); |
| 703 | dput(upper); |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 704 | if (err) |
| 705 | goto cleanup; |
| 706 | |
| 707 | if (!c->metacopy) |
| 708 | ovl_set_upperdata(d_inode(c->dentry)); |
| 709 | inode = d_inode(c->dentry); |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 710 | ovl_inode_update(inode, temp); |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 711 | if (S_ISDIR(inode->i_mode)) |
| 712 | ovl_set_flag(OVL_WHITEOUTS, inode); |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 713 | unlock: |
| 714 | unlock_rename(c->workdir, c->destdir); |
| 715 | |
| 716 | return err; |
| 717 | |
| 718 | cleanup: |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 719 | ovl_cleanup(wdir, temp); |
| 720 | dput(temp); |
| 721 | goto unlock; |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /* Copyup using O_TMPFILE which does not require cross dir locking */ |
| 725 | static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) |
| 726 | { |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 727 | struct inode *udir = d_inode(c->destdir); |
| 728 | struct dentry *temp, *upper; |
| 729 | struct ovl_cu_creds cc; |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 730 | int err; |
| 731 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 732 | err = ovl_prep_cu_creds(c->dentry, &cc); |
| 733 | if (err) |
| 734 | return err; |
| 735 | |
| 736 | temp = ovl_do_tmpfile(c->workdir, c->stat.mode); |
| 737 | ovl_revert_cu_creds(&cc); |
| 738 | |
Miklos Szeredi | b148cba | 2018-05-31 11:06:11 +0200 | [diff] [blame] | 739 | if (IS_ERR(temp)) |
| 740 | return PTR_ERR(temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 741 | |
Miklos Szeredi | 23f0ab1 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 742 | err = ovl_copy_up_inode(c, temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 743 | if (err) |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 744 | goto out_dput; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 745 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 746 | inode_lock_nested(udir, I_MUTEX_PARENT); |
| 747 | |
| 748 | upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len); |
| 749 | err = PTR_ERR(upper); |
| 750 | if (!IS_ERR(upper)) { |
| 751 | err = ovl_do_link(temp, udir, upper); |
| 752 | dput(upper); |
| 753 | } |
| 754 | inode_unlock(udir); |
| 755 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 756 | if (err) |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 757 | goto out_dput; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 758 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 759 | if (!c->metacopy) |
| 760 | ovl_set_upperdata(d_inode(c->dentry)); |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 761 | ovl_inode_update(d_inode(c->dentry), temp); |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 762 | |
Miklos Szeredi | 6b52243 | 2018-10-26 23:34:39 +0200 | [diff] [blame] | 763 | return 0; |
| 764 | |
| 765 | out_dput: |
Miklos Szeredi | e85f82f | 2017-06-28 13:41:22 +0200 | [diff] [blame] | 766 | dput(temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 767 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Copy up a single dentry |
| 772 | * |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 773 | * All renames start with copy up of source if necessary. The actual |
| 774 | * rename will only proceed once the copy up was successful. Copy up uses |
| 775 | * upper parent i_mutex for exclusion. Since rename can change d_parent it |
| 776 | * is possible that the copy up will lock the old parent. At that point |
| 777 | * the file will have already been copied up anyway. |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 778 | */ |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 779 | static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 780 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 781 | int err; |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 782 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 783 | bool to_index = false; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 784 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 785 | /* |
| 786 | * Indexed non-dir is copied up directly to the index entry and then |
| 787 | * hardlinked to upper dir. Indexed dir is copied up to indexdir, |
| 788 | * then index entry is created and then copied up dir installed. |
| 789 | * Copying dir up to indexdir instead of workdir simplifies locking. |
| 790 | */ |
| 791 | if (ovl_need_index(c->dentry)) { |
| 792 | c->indexed = true; |
| 793 | if (S_ISDIR(c->stat.mode)) |
| 794 | c->workdir = ovl_indexdir(c->dentry->d_sb); |
| 795 | else |
| 796 | to_index = true; |
| 797 | } |
| 798 | |
| 799 | if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 800 | c->origin = true; |
| 801 | |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 802 | if (to_index) { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 803 | c->destdir = ovl_indexdir(c->dentry->d_sb); |
Pavel Tikhomirov | 1cdb0cb | 2020-10-13 17:59:53 +0300 | [diff] [blame] | 804 | err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname); |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 805 | if (err) |
| 806 | return err; |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 807 | } else if (WARN_ON(!c->parent)) { |
| 808 | /* Disconnected dentry must be copied up to index dir */ |
| 809 | return -EIO; |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 810 | } else { |
| 811 | /* |
| 812 | * Mark parent "impure" because it may now contain non-pure |
| 813 | * upper |
| 814 | */ |
| 815 | err = ovl_set_impure(c->parent, c->destdir); |
| 816 | if (err) |
| 817 | return err; |
| 818 | } |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 819 | |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 820 | /* Should we copyup with O_TMPFILE or with workdir? */ |
Amir Goldstein | b10cdcd | 2018-10-08 07:25:23 +0300 | [diff] [blame] | 821 | if (S_ISREG(c->stat.mode) && ofs->tmpfile) |
| 822 | err = ovl_copy_up_tmpfile(c); |
| 823 | else |
| 824 | err = ovl_copy_up_workdir(c); |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 825 | if (err) |
| 826 | goto out; |
| 827 | |
| 828 | if (c->indexed) |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 829 | ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); |
| 830 | |
| 831 | if (to_index) { |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 832 | /* Initialize nlink for copy up of disconnected dentry */ |
| 833 | err = ovl_set_nlink_upper(c->dentry); |
| 834 | } else { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 835 | struct inode *udir = d_inode(c->destdir); |
| 836 | |
| 837 | /* Restore timestamps on parent (best effort) */ |
| 838 | inode_lock(udir); |
| 839 | ovl_set_timestamps(c->destdir, &c->pstat); |
| 840 | inode_unlock(udir); |
| 841 | |
| 842 | ovl_dentry_set_upper_alias(c->dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 843 | } |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 844 | |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 845 | out: |
| 846 | if (to_index) |
| 847 | kfree(c->destname.name); |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 848 | return err; |
| 849 | } |
| 850 | |
Vivek Goyal | 44d5bf1 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 851 | static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode, |
| 852 | int flags) |
| 853 | { |
| 854 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 855 | |
Vivek Goyal | 44d5bf1 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 856 | if (!ofs->config.metacopy) |
| 857 | return false; |
| 858 | |
| 859 | if (!S_ISREG(mode)) |
| 860 | return false; |
| 861 | |
| 862 | if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC))) |
| 863 | return false; |
| 864 | |
| 865 | return true; |
| 866 | } |
| 867 | |
Miklos Szeredi | de7a52c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 868 | static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value) |
Miklos Szeredi | fee0f29 | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 869 | { |
| 870 | ssize_t res; |
Miklos Szeredi | de7a52c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 871 | char *buf; |
Miklos Szeredi | fee0f29 | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 872 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 873 | res = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0); |
Miklos Szeredi | de7a52c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 874 | if (res == -ENODATA || res == -EOPNOTSUPP) |
| 875 | res = 0; |
Miklos Szeredi | fee0f29 | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 876 | |
Miklos Szeredi | de7a52c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 877 | if (res > 0) { |
| 878 | buf = kzalloc(res, GFP_KERNEL); |
Miklos Szeredi | fee0f29 | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 879 | if (!buf) |
| 880 | return -ENOMEM; |
| 881 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 882 | res = vfs_getxattr(&init_user_ns, dentry, name, buf, res); |
Miklos Szeredi | fee0f29 | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 883 | if (res < 0) |
Miklos Szeredi | de7a52c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 884 | kfree(buf); |
| 885 | else |
| 886 | *value = buf; |
Miklos Szeredi | fee0f29 | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 887 | } |
Miklos Szeredi | fee0f29 | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 888 | return res; |
| 889 | } |
| 890 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 891 | /* Copy up data of an inode which was copied up metadata only in the past. */ |
| 892 | static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) |
| 893 | { |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 894 | struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); |
Vivek Goyal | 4f93b42 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 895 | struct path upperpath, datapath; |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 896 | int err; |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 897 | char *capability = NULL; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 898 | ssize_t cap_size; |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 899 | |
| 900 | ovl_path_upper(c->dentry, &upperpath); |
| 901 | if (WARN_ON(upperpath.dentry == NULL)) |
| 902 | return -EIO; |
| 903 | |
Vivek Goyal | 4f93b42 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 904 | ovl_path_lowerdata(c->dentry, &datapath); |
| 905 | if (WARN_ON(datapath.dentry == NULL)) |
| 906 | return -EIO; |
| 907 | |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 908 | if (c->stat.size) { |
| 909 | err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS, |
Miklos Szeredi | de7a52c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 910 | &capability); |
| 911 | if (cap_size < 0) |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 912 | goto out; |
| 913 | } |
| 914 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 915 | err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size); |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 916 | if (err) |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 917 | goto out_free; |
| 918 | |
| 919 | /* |
| 920 | * Writing to upper file will clear security.capability xattr. We |
| 921 | * don't want that to happen for normal copy-up operation. |
| 922 | */ |
| 923 | if (capability) { |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 924 | err = vfs_setxattr(&init_user_ns, upperpath.dentry, |
| 925 | XATTR_NAME_CAPS, capability, cap_size, 0); |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 926 | if (err) |
| 927 | goto out_free; |
| 928 | } |
| 929 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 930 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 931 | err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY); |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 932 | if (err) |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 933 | goto out_free; |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 934 | |
| 935 | ovl_set_upperdata(d_inode(c->dentry)); |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 936 | out_free: |
| 937 | kfree(capability); |
| 938 | out: |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 939 | return err; |
| 940 | } |
| 941 | |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 942 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
| 943 | int flags) |
| 944 | { |
| 945 | int err; |
| 946 | DEFINE_DELAYED_CALL(done); |
| 947 | struct path parentpath; |
| 948 | struct ovl_copy_up_ctx ctx = { |
| 949 | .parent = parent, |
| 950 | .dentry = dentry, |
| 951 | .workdir = ovl_workdir(dentry), |
| 952 | }; |
| 953 | |
| 954 | if (WARN_ON(!ctx.workdir)) |
| 955 | return -EROFS; |
| 956 | |
| 957 | ovl_path_lower(dentry, &ctx.lowerpath); |
| 958 | err = vfs_getattr(&ctx.lowerpath, &ctx.stat, |
| 959 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); |
| 960 | if (err) |
| 961 | return err; |
| 962 | |
Vivek Goyal | 44d5bf1 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 963 | ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags); |
| 964 | |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 965 | if (parent) { |
| 966 | ovl_path_upper(parent, &parentpath); |
| 967 | ctx.destdir = parentpath.dentry; |
| 968 | ctx.destname = dentry->d_name; |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 969 | |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 970 | err = vfs_getattr(&parentpath, &ctx.pstat, |
| 971 | STATX_ATIME | STATX_MTIME, |
| 972 | AT_STATX_SYNC_AS_STAT); |
| 973 | if (err) |
| 974 | return err; |
| 975 | } |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 976 | |
| 977 | /* maybe truncate regular file. this has no effect on dirs */ |
| 978 | if (flags & O_TRUNC) |
| 979 | ctx.stat.size = 0; |
| 980 | |
| 981 | if (S_ISLNK(ctx.stat.mode)) { |
| 982 | ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); |
| 983 | if (IS_ERR(ctx.link)) |
| 984 | return PTR_ERR(ctx.link); |
| 985 | } |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 986 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 987 | err = ovl_copy_up_start(dentry, flags); |
Miklos Szeredi | fd210b7 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 988 | /* err < 0: interrupted, err > 0: raced with another copy-up */ |
| 989 | if (unlikely(err)) { |
| 990 | if (err > 0) |
| 991 | err = 0; |
| 992 | } else { |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 993 | if (!ovl_dentry_upper(dentry)) |
| 994 | err = ovl_do_copy_up(&ctx); |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 995 | if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) |
Amir Goldstein | f4439de | 2017-07-04 22:04:06 +0300 | [diff] [blame] | 996 | err = ovl_link_up(&ctx); |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 997 | if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags)) |
| 998 | err = ovl_copy_up_meta_inode_data(&ctx); |
Miklos Szeredi | fd210b7 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 999 | ovl_copy_up_end(dentry); |
| 1000 | } |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 1001 | do_delayed_call(&done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1002 | |
| 1003 | return err; |
| 1004 | } |
| 1005 | |
youngjun | 5ac8e80 | 2020-06-21 07:30:59 -0700 | [diff] [blame] | 1006 | static int ovl_copy_up_flags(struct dentry *dentry, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1007 | { |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 1008 | int err = 0; |
Dan Carpenter | 7b279bb | 2021-03-23 16:19:35 +0300 | [diff] [blame] | 1009 | const struct cred *old_cred; |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 1010 | bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); |
| 1011 | |
| 1012 | /* |
| 1013 | * With NFS export, copy up can get called for a disconnected non-dir. |
| 1014 | * In this case, we will copy up lower inode to index dir without |
| 1015 | * linking it to upper dir. |
| 1016 | */ |
| 1017 | if (WARN_ON(disconnected && d_is_dir(dentry))) |
| 1018 | return -EIO; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1019 | |
Dan Carpenter | 7b279bb | 2021-03-23 16:19:35 +0300 | [diff] [blame] | 1020 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1021 | while (!err) { |
| 1022 | struct dentry *next; |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 1023 | struct dentry *parent = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1024 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1025 | if (ovl_already_copied_up(dentry, flags)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1026 | break; |
| 1027 | |
| 1028 | next = dget(dentry); |
| 1029 | /* find the topmost dentry not yet copied up */ |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 1030 | for (; !disconnected;) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1031 | parent = dget_parent(next); |
| 1032 | |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 1033 | if (ovl_dentry_upper(parent)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1034 | break; |
| 1035 | |
| 1036 | dput(next); |
| 1037 | next = parent; |
| 1038 | } |
| 1039 | |
Miklos Szeredi | a6fb235 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 1040 | err = ovl_copy_up_one(parent, next, flags); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1041 | |
| 1042 | dput(parent); |
| 1043 | dput(next); |
| 1044 | } |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 1045 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1046 | |
| 1047 | return err; |
| 1048 | } |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 1049 | |
Vivek Goyal | d6eac03 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1050 | static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) |
| 1051 | { |
| 1052 | /* Copy up of disconnected dentry does not set upper alias */ |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1053 | if (ovl_already_copied_up(dentry, flags)) |
Vivek Goyal | d6eac03 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1054 | return false; |
| 1055 | |
| 1056 | if (special_file(d_inode(dentry)->i_mode)) |
| 1057 | return false; |
| 1058 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1059 | if (!ovl_open_flags_need_copy_up(flags)) |
Vivek Goyal | d6eac03 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1060 | return false; |
| 1061 | |
| 1062 | return true; |
| 1063 | } |
| 1064 | |
Amir Goldstein | 3428030 | 2019-01-22 07:01:39 +0200 | [diff] [blame] | 1065 | int ovl_maybe_copy_up(struct dentry *dentry, int flags) |
Vivek Goyal | d6eac03 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1066 | { |
| 1067 | int err = 0; |
| 1068 | |
Amir Goldstein | 3428030 | 2019-01-22 07:01:39 +0200 | [diff] [blame] | 1069 | if (ovl_open_need_copy_up(dentry, flags)) { |
Vivek Goyal | d6eac03 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1070 | err = ovl_want_write(dentry); |
| 1071 | if (!err) { |
Amir Goldstein | 3428030 | 2019-01-22 07:01:39 +0200 | [diff] [blame] | 1072 | err = ovl_copy_up_flags(dentry, flags); |
Vivek Goyal | d6eac03 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1073 | ovl_drop_write(dentry); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | return err; |
| 1078 | } |
| 1079 | |
Vivek Goyal | d1e6f6a | 2018-05-11 11:49:33 -0400 | [diff] [blame] | 1080 | int ovl_copy_up_with_data(struct dentry *dentry) |
| 1081 | { |
| 1082 | return ovl_copy_up_flags(dentry, O_WRONLY); |
| 1083 | } |
| 1084 | |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 1085 | int ovl_copy_up(struct dentry *dentry) |
| 1086 | { |
| 1087 | return ovl_copy_up_flags(dentry, 0); |
| 1088 | } |