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