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" |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 25 | #include "ovl_entry.h" |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 26 | |
| 27 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) |
| 28 | |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 29 | static bool __read_mostly ovl_check_copy_up; |
| 30 | module_param_named(check_copy_up, ovl_check_copy_up, bool, |
| 31 | S_IWUSR | S_IRUGO); |
| 32 | MODULE_PARM_DESC(ovl_check_copy_up, |
| 33 | "Warn on copy-up when causing process also has a R/O fd open"); |
| 34 | |
| 35 | static int ovl_check_fd(const void *data, struct file *f, unsigned int fd) |
| 36 | { |
| 37 | const struct dentry *dentry = data; |
| 38 | |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 39 | if (file_inode(f) == d_inode(dentry)) |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 40 | 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", |
| 41 | f, fd, current->pid, current->comm); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Check the fds open by this process and warn if something like the following |
| 47 | * scenario is about to occur: |
| 48 | * |
| 49 | * fd1 = open("foo", O_RDONLY); |
| 50 | * fd2 = open("foo", O_RDWR); |
| 51 | */ |
| 52 | static void ovl_do_check_copy_up(struct dentry *dentry) |
| 53 | { |
| 54 | if (ovl_check_copy_up) |
| 55 | iterate_fd(current->files, 0, ovl_check_fd, dentry); |
| 56 | } |
| 57 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 58 | int ovl_copy_xattr(struct dentry *old, struct dentry *new) |
| 59 | { |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 60 | ssize_t list_size, size, value_size = 0; |
| 61 | char *buf, *name, *value = NULL; |
| 62 | int uninitialized_var(error); |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 63 | size_t slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 64 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 65 | if (!(old->d_inode->i_opflags & IOP_XATTR) || |
| 66 | !(new->d_inode->i_opflags & IOP_XATTR)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 67 | return 0; |
| 68 | |
| 69 | list_size = vfs_listxattr(old, NULL, 0); |
| 70 | if (list_size <= 0) { |
| 71 | if (list_size == -EOPNOTSUPP) |
| 72 | return 0; |
| 73 | return list_size; |
| 74 | } |
| 75 | |
| 76 | buf = kzalloc(list_size, GFP_KERNEL); |
| 77 | if (!buf) |
| 78 | return -ENOMEM; |
| 79 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 80 | list_size = vfs_listxattr(old, buf, list_size); |
| 81 | if (list_size <= 0) { |
| 82 | error = list_size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 83 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 86 | for (name = buf; list_size; name += slen) { |
| 87 | slen = strnlen(name, list_size) + 1; |
| 88 | |
| 89 | /* underlying fs providing us with an broken xattr list? */ |
| 90 | if (WARN_ON(slen > list_size)) { |
| 91 | error = -EIO; |
| 92 | break; |
| 93 | } |
| 94 | list_size -= slen; |
| 95 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 96 | if (ovl_is_private_xattr(name)) |
| 97 | continue; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 98 | retry: |
| 99 | size = vfs_getxattr(old, name, value, value_size); |
| 100 | if (size == -ERANGE) |
| 101 | size = vfs_getxattr(old, name, NULL, 0); |
| 102 | |
Miklos Szeredi | 97daf8b | 2015-11-10 17:08:41 +0100 | [diff] [blame] | 103 | if (size < 0) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 104 | error = size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 105 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 106 | } |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 107 | |
| 108 | if (size > value_size) { |
| 109 | void *new; |
| 110 | |
| 111 | new = krealloc(value, size, GFP_KERNEL); |
| 112 | if (!new) { |
| 113 | error = -ENOMEM; |
| 114 | break; |
| 115 | } |
| 116 | value = new; |
| 117 | value_size = size; |
| 118 | goto retry; |
| 119 | } |
| 120 | |
Vivek Goyal | 121ab82 | 2016-07-13 10:44:49 -0400 | [diff] [blame] | 121 | error = security_inode_copy_up_xattr(name); |
| 122 | if (error < 0 && error != -EOPNOTSUPP) |
| 123 | break; |
| 124 | if (error == 1) { |
| 125 | error = 0; |
| 126 | continue; /* Discard */ |
| 127 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 128 | error = vfs_setxattr(new, name, value, size, 0); |
| 129 | if (error) |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 130 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 131 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 132 | kfree(value); |
| 133 | out: |
| 134 | kfree(buf); |
| 135 | return error; |
| 136 | } |
| 137 | |
| 138 | static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) |
| 139 | { |
| 140 | struct file *old_file; |
| 141 | struct file *new_file; |
| 142 | loff_t old_pos = 0; |
| 143 | loff_t new_pos = 0; |
| 144 | int error = 0; |
| 145 | |
| 146 | if (len == 0) |
| 147 | return 0; |
| 148 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 149 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 150 | if (IS_ERR(old_file)) |
| 151 | return PTR_ERR(old_file); |
| 152 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 153 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 154 | if (IS_ERR(new_file)) { |
| 155 | error = PTR_ERR(new_file); |
| 156 | goto out_fput; |
| 157 | } |
| 158 | |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 159 | /* Try to use clone_file_range to clone up within the same fs */ |
| 160 | error = vfs_clone_file_range(old_file, 0, new_file, 0, len); |
| 161 | if (!error) |
| 162 | goto out; |
| 163 | /* Couldn't clone, so now we try to copy the data */ |
| 164 | error = 0; |
| 165 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 166 | /* FIXME: copy up sparse files efficiently */ |
| 167 | while (len) { |
| 168 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; |
| 169 | long bytes; |
| 170 | |
| 171 | if (len < this_len) |
| 172 | this_len = len; |
| 173 | |
| 174 | if (signal_pending_state(TASK_KILLABLE, current)) { |
| 175 | error = -EINTR; |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | bytes = do_splice_direct(old_file, &old_pos, |
| 180 | new_file, &new_pos, |
| 181 | this_len, SPLICE_F_MOVE); |
| 182 | if (bytes <= 0) { |
| 183 | error = bytes; |
| 184 | break; |
| 185 | } |
| 186 | WARN_ON(old_pos != new_pos); |
| 187 | |
| 188 | len -= bytes; |
| 189 | } |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame] | 190 | out: |
Miklos Szeredi | 641089c | 2016-10-31 14:42:14 +0100 | [diff] [blame] | 191 | if (!error) |
| 192 | error = vfs_fsync(new_file, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 193 | fput(new_file); |
| 194 | out_fput: |
| 195 | fput(old_file); |
| 196 | return error; |
| 197 | } |
| 198 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 199 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
| 200 | { |
| 201 | struct iattr attr = { |
| 202 | .ia_valid = |
| 203 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, |
| 204 | .ia_atime = stat->atime, |
| 205 | .ia_mtime = stat->mtime, |
| 206 | }; |
| 207 | |
| 208 | return notify_change(upperdentry, &attr, NULL); |
| 209 | } |
| 210 | |
| 211 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) |
| 212 | { |
| 213 | int err = 0; |
| 214 | |
| 215 | if (!S_ISLNK(stat->mode)) { |
| 216 | struct iattr attr = { |
| 217 | .ia_valid = ATTR_MODE, |
| 218 | .ia_mode = stat->mode, |
| 219 | }; |
| 220 | err = notify_change(upperdentry, &attr, NULL); |
| 221 | } |
| 222 | if (!err) { |
| 223 | struct iattr attr = { |
| 224 | .ia_valid = ATTR_UID | ATTR_GID, |
| 225 | .ia_uid = stat->uid, |
| 226 | .ia_gid = stat->gid, |
| 227 | }; |
| 228 | err = notify_change(upperdentry, &attr, NULL); |
| 229 | } |
| 230 | if (!err) |
| 231 | ovl_set_timestamps(upperdentry, stat); |
| 232 | |
| 233 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 236 | static struct ovl_fh *ovl_encode_fh(struct dentry *lower, uuid_be *uuid) |
| 237 | { |
| 238 | struct ovl_fh *fh; |
| 239 | int fh_type, fh_len, dwords; |
| 240 | void *buf; |
| 241 | int buflen = MAX_HANDLE_SZ; |
| 242 | |
| 243 | buf = kmalloc(buflen, GFP_TEMPORARY); |
| 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; |
| 253 | fh_type = exportfs_encode_fh(lower, buf, &dwords, 0); |
| 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; |
| 274 | fh->len = fh_len; |
| 275 | fh->uuid = *uuid; |
| 276 | memcpy(fh->fid, buf, buflen); |
| 277 | |
| 278 | out: |
| 279 | kfree(buf); |
| 280 | return fh; |
| 281 | } |
| 282 | |
| 283 | static int ovl_set_origin(struct dentry *dentry, struct dentry *lower, |
| 284 | struct dentry *upper) |
| 285 | { |
| 286 | struct super_block *sb = lower->d_sb; |
| 287 | uuid_be *uuid = (uuid_be *) &sb->s_uuid; |
| 288 | const struct ovl_fh *fh = NULL; |
| 289 | int err; |
| 290 | |
| 291 | /* |
| 292 | * When lower layer doesn't support export operations store a 'null' fh, |
| 293 | * so we can use the overlay.origin xattr to distignuish between a copy |
| 294 | * up and a pure upper inode. |
| 295 | */ |
| 296 | if (sb->s_export_op && sb->s_export_op->fh_to_dentry && |
| 297 | uuid_be_cmp(*uuid, NULL_UUID_BE)) { |
| 298 | fh = ovl_encode_fh(lower, uuid); |
| 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 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 313 | static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, |
| 314 | struct dentry *dentry, struct path *lowerpath, |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 315 | struct kstat *stat, const char *link, |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 316 | struct kstat *pstat, bool tmpfile) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 317 | { |
| 318 | struct inode *wdir = workdir->d_inode; |
| 319 | struct inode *udir = upperdir->d_inode; |
| 320 | struct dentry *newdentry = NULL; |
| 321 | struct dentry *upper = NULL; |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 322 | struct dentry *temp = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 323 | int err; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 324 | const struct cred *old_creds = NULL; |
| 325 | struct cred *new_creds = NULL; |
Al Viro | 32a3d84 | 2016-12-04 17:33:17 +0000 | [diff] [blame] | 326 | struct cattr cattr = { |
| 327 | /* Can't properly set mode on creation because of the umask */ |
| 328 | .mode = stat->mode & S_IFMT, |
| 329 | .rdev = stat->rdev, |
| 330 | .link = link |
| 331 | }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 332 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 333 | upper = lookup_one_len(dentry->d_name.name, upperdir, |
| 334 | dentry->d_name.len); |
| 335 | err = PTR_ERR(upper); |
| 336 | if (IS_ERR(upper)) |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 337 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 338 | |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 339 | err = security_inode_copy_up(dentry, &new_creds); |
| 340 | if (err < 0) |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 341 | goto out1; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 342 | |
| 343 | if (new_creds) |
| 344 | old_creds = override_creds(new_creds); |
| 345 | |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 346 | if (tmpfile) |
| 347 | temp = ovl_do_tmpfile(upperdir, stat->mode); |
| 348 | else |
Miklos Szeredi | 3d27573 | 2017-05-19 09:33:49 +0200 | [diff] [blame] | 349 | temp = ovl_lookup_temp(workdir); |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 350 | err = 0; |
Amir Goldstein | 8137ae2 | 2017-05-16 08:45:46 +0300 | [diff] [blame] | 351 | if (IS_ERR(temp)) { |
| 352 | err = PTR_ERR(temp); |
| 353 | temp = NULL; |
| 354 | } |
| 355 | |
| 356 | if (!err && !tmpfile) |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 357 | err = ovl_create_real(wdir, temp, &cattr, NULL, true); |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 358 | |
| 359 | if (new_creds) { |
| 360 | revert_creds(old_creds); |
| 361 | put_cred(new_creds); |
| 362 | } |
| 363 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 364 | if (err) |
| 365 | goto out2; |
| 366 | |
| 367 | if (S_ISREG(stat->mode)) { |
| 368 | struct path upperpath; |
Sohom Bhattacharjee | f134f24 | 2016-03-15 20:57:59 +0530 | [diff] [blame] | 369 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 370 | ovl_path_upper(dentry, &upperpath); |
| 371 | BUG_ON(upperpath.dentry != NULL); |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 372 | upperpath.dentry = temp; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 373 | |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 374 | if (tmpfile) { |
| 375 | inode_unlock(udir); |
| 376 | err = ovl_copy_up_data(lowerpath, &upperpath, |
| 377 | stat->size); |
| 378 | inode_lock_nested(udir, I_MUTEX_PARENT); |
| 379 | } else { |
| 380 | err = ovl_copy_up_data(lowerpath, &upperpath, |
| 381 | stat->size); |
| 382 | } |
| 383 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 384 | if (err) |
| 385 | goto out_cleanup; |
| 386 | } |
| 387 | |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 388 | err = ovl_copy_xattr(lowerpath->dentry, temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 389 | if (err) |
| 390 | goto out_cleanup; |
| 391 | |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 392 | inode_lock(temp->d_inode); |
| 393 | err = ovl_set_attr(temp, stat); |
| 394 | inode_unlock(temp->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 395 | if (err) |
| 396 | goto out_cleanup; |
| 397 | |
Amir Goldstein | 3a1e819 | 2017-03-30 15:22:16 +0300 | [diff] [blame] | 398 | /* |
| 399 | * Store identifier of lower inode in upper inode xattr to |
| 400 | * allow lookup of the copy up origin inode. |
| 401 | */ |
| 402 | err = ovl_set_origin(dentry, lowerpath->dentry, temp); |
| 403 | if (err) |
| 404 | goto out_cleanup; |
| 405 | |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 406 | if (tmpfile) |
| 407 | err = ovl_do_link(temp, udir, upper, true); |
| 408 | else |
| 409 | err = ovl_do_rename(wdir, temp, udir, upper, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 410 | if (err) |
| 411 | goto out_cleanup; |
| 412 | |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 413 | newdentry = dget(tmpfile ? upper : temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 414 | ovl_dentry_update(dentry, newdentry); |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 415 | ovl_inode_update(d_inode(dentry), d_inode(newdentry)); |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 416 | |
| 417 | /* Restore timestamps on parent (best effort) */ |
| 418 | ovl_set_timestamps(upperdir, pstat); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 419 | out2: |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 420 | dput(temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 421 | out1: |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 422 | dput(upper); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 423 | out: |
| 424 | return err; |
| 425 | |
| 426 | out_cleanup: |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 427 | if (!tmpfile) |
| 428 | ovl_cleanup(wdir, temp); |
David Howells | ab79efa | 2015-09-18 11:45:22 +0100 | [diff] [blame] | 429 | goto out2; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Copy up a single dentry |
| 434 | * |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 435 | * All renames start with copy up of source if necessary. The actual |
| 436 | * rename will only proceed once the copy up was successful. Copy up uses |
| 437 | * upper parent i_mutex for exclusion. Since rename can change d_parent it |
| 438 | * is possible that the copy up will lock the old parent. At that point |
| 439 | * the file will have already been copied up anyway. |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 440 | */ |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 441 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
| 442 | struct path *lowerpath, struct kstat *stat) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 443 | { |
Miklos Szeredi | 7764235 | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 444 | DEFINE_DELAYED_CALL(done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 445 | struct dentry *workdir = ovl_workdir(dentry); |
| 446 | int err; |
| 447 | struct kstat pstat; |
| 448 | struct path parentpath; |
Miklos Szeredi | 7764235 | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 449 | struct dentry *lowerdentry = lowerpath->dentry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 450 | struct dentry *upperdir; |
Miklos Szeredi | 7764235 | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 451 | const char *link = NULL; |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 452 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 453 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 454 | if (WARN_ON(!workdir)) |
| 455 | return -EROFS; |
| 456 | |
Miklos Szeredi | 7764235 | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 457 | ovl_do_check_copy_up(lowerdentry); |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 458 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 459 | ovl_path_upper(parent, &parentpath); |
| 460 | upperdir = parentpath.dentry; |
| 461 | |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 462 | /* Mark parent "impure" because it may now contain non-pure upper */ |
| 463 | err = ovl_set_impure(parent, upperdir); |
| 464 | if (err) |
| 465 | return err; |
| 466 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 467 | err = vfs_getattr(&parentpath, &pstat, |
| 468 | STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 469 | if (err) |
| 470 | return err; |
| 471 | |
| 472 | if (S_ISLNK(stat->mode)) { |
Miklos Szeredi | 7764235 | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 473 | link = vfs_get_link(lowerdentry, &done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 474 | if (IS_ERR(link)) |
| 475 | return PTR_ERR(link); |
| 476 | } |
| 477 | |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 478 | /* Should we copyup with O_TMPFILE or with workdir? */ |
| 479 | if (S_ISREG(stat->mode) && ofs->tmpfile) { |
| 480 | err = ovl_copy_up_start(dentry); |
| 481 | /* err < 0: interrupted, err > 0: raced with another copy-up */ |
| 482 | if (unlikely(err)) { |
| 483 | pr_debug("ovl_copy_up_start(%pd2) = %i\n", dentry, err); |
| 484 | if (err > 0) |
| 485 | err = 0; |
| 486 | goto out_done; |
| 487 | } |
| 488 | |
| 489 | inode_lock_nested(upperdir->d_inode, I_MUTEX_PARENT); |
| 490 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, |
| 491 | stat, link, &pstat, true); |
| 492 | inode_unlock(upperdir->d_inode); |
| 493 | ovl_copy_up_end(dentry); |
| 494 | goto out_done; |
| 495 | } |
| 496 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 497 | err = -EIO; |
| 498 | if (lock_rename(workdir, upperdir) != NULL) { |
| 499 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); |
| 500 | goto out_unlock; |
| 501 | } |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 502 | if (ovl_dentry_upper(dentry)) { |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 503 | /* Raced with another copy-up? Nothing to do, then... */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 504 | err = 0; |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 505 | goto out_unlock; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 509 | stat, link, &pstat, false); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 510 | out_unlock: |
| 511 | unlock_rename(workdir, upperdir); |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 512 | out_done: |
Miklos Szeredi | 7764235 | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 513 | do_delayed_call(&done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 514 | |
| 515 | return err; |
| 516 | } |
| 517 | |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 518 | int ovl_copy_up_flags(struct dentry *dentry, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 519 | { |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 520 | int err = 0; |
| 521 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 522 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 523 | while (!err) { |
| 524 | struct dentry *next; |
| 525 | struct dentry *parent; |
| 526 | struct path lowerpath; |
| 527 | struct kstat stat; |
| 528 | enum ovl_path_type type = ovl_path_type(dentry); |
| 529 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 530 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 531 | break; |
| 532 | |
| 533 | next = dget(dentry); |
| 534 | /* find the topmost dentry not yet copied up */ |
| 535 | for (;;) { |
| 536 | parent = dget_parent(next); |
| 537 | |
| 538 | type = ovl_path_type(parent); |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 539 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 540 | break; |
| 541 | |
| 542 | dput(next); |
| 543 | next = parent; |
| 544 | } |
| 545 | |
| 546 | ovl_path_lower(next, &lowerpath); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 547 | err = vfs_getattr(&lowerpath, &stat, |
| 548 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 549 | /* maybe truncate regular file. this has no effect on dirs */ |
| 550 | if (flags & O_TRUNC) |
| 551 | stat.size = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 552 | if (!err) |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 553 | err = ovl_copy_up_one(parent, next, &lowerpath, &stat); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 554 | |
| 555 | dput(parent); |
| 556 | dput(next); |
| 557 | } |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 558 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 559 | |
| 560 | return err; |
| 561 | } |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 562 | |
| 563 | int ovl_copy_up(struct dentry *dentry) |
| 564 | { |
| 565 | return ovl_copy_up_flags(dentry, 0); |
| 566 | } |