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