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