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