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