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> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 23 | #include "overlayfs.h" |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 24 | #include "ovl_entry.h" |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 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 | |
| 235 | static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, |
| 236 | struct dentry *dentry, struct path *lowerpath, |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 237 | struct kstat *stat, const char *link, |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 238 | struct kstat *pstat, bool tmpfile) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 239 | { |
| 240 | struct inode *wdir = workdir->d_inode; |
| 241 | struct inode *udir = upperdir->d_inode; |
| 242 | struct dentry *newdentry = NULL; |
| 243 | struct dentry *upper = NULL; |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 244 | struct dentry *temp = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 245 | int err; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 246 | const struct cred *old_creds = NULL; |
| 247 | struct cred *new_creds = NULL; |
Al Viro | 32a3d84 | 2016-12-04 17:33:17 +0000 | [diff] [blame] | 248 | struct cattr cattr = { |
| 249 | /* Can't properly set mode on creation because of the umask */ |
| 250 | .mode = stat->mode & S_IFMT, |
| 251 | .rdev = stat->rdev, |
| 252 | .link = link |
| 253 | }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 254 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 255 | upper = lookup_one_len(dentry->d_name.name, upperdir, |
| 256 | dentry->d_name.len); |
| 257 | err = PTR_ERR(upper); |
| 258 | if (IS_ERR(upper)) |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 259 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 260 | |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 261 | err = security_inode_copy_up(dentry, &new_creds); |
| 262 | if (err < 0) |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 263 | goto out1; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 264 | |
| 265 | if (new_creds) |
| 266 | old_creds = override_creds(new_creds); |
| 267 | |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 268 | if (tmpfile) |
| 269 | temp = ovl_do_tmpfile(upperdir, stat->mode); |
| 270 | else |
| 271 | temp = ovl_lookup_temp(workdir, dentry); |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 272 | err = PTR_ERR(temp); |
| 273 | if (IS_ERR(temp)) |
| 274 | goto out1; |
| 275 | |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 276 | err = 0; |
| 277 | if (!tmpfile) |
| 278 | err = ovl_create_real(wdir, temp, &cattr, NULL, true); |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 279 | |
| 280 | if (new_creds) { |
| 281 | revert_creds(old_creds); |
| 282 | put_cred(new_creds); |
| 283 | } |
| 284 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 285 | if (err) |
| 286 | goto out2; |
| 287 | |
| 288 | if (S_ISREG(stat->mode)) { |
| 289 | struct path upperpath; |
Sohom Bhattacharjee | f134f24 | 2016-03-15 20:57:59 +0530 | [diff] [blame] | 290 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 291 | ovl_path_upper(dentry, &upperpath); |
| 292 | BUG_ON(upperpath.dentry != NULL); |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 293 | upperpath.dentry = temp; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 294 | |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 295 | if (tmpfile) { |
| 296 | inode_unlock(udir); |
| 297 | err = ovl_copy_up_data(lowerpath, &upperpath, |
| 298 | stat->size); |
| 299 | inode_lock_nested(udir, I_MUTEX_PARENT); |
| 300 | } else { |
| 301 | err = ovl_copy_up_data(lowerpath, &upperpath, |
| 302 | stat->size); |
| 303 | } |
| 304 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 305 | if (err) |
| 306 | goto out_cleanup; |
| 307 | } |
| 308 | |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 309 | err = ovl_copy_xattr(lowerpath->dentry, temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 310 | if (err) |
| 311 | goto out_cleanup; |
| 312 | |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 313 | inode_lock(temp->d_inode); |
| 314 | err = ovl_set_attr(temp, stat); |
| 315 | inode_unlock(temp->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 316 | if (err) |
| 317 | goto out_cleanup; |
| 318 | |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 319 | if (tmpfile) |
| 320 | err = ovl_do_link(temp, udir, upper, true); |
| 321 | else |
| 322 | err = ovl_do_rename(wdir, temp, udir, upper, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 323 | if (err) |
| 324 | goto out_cleanup; |
| 325 | |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 326 | newdentry = dget(tmpfile ? upper : temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 327 | ovl_dentry_update(dentry, newdentry); |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 328 | ovl_inode_update(d_inode(dentry), d_inode(newdentry)); |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 329 | |
| 330 | /* Restore timestamps on parent (best effort) */ |
| 331 | ovl_set_timestamps(upperdir, pstat); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 332 | out2: |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 333 | dput(temp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 334 | out1: |
Amir Goldstein | 42f269b | 2017-01-17 06:34:54 +0200 | [diff] [blame] | 335 | dput(upper); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 336 | out: |
| 337 | return err; |
| 338 | |
| 339 | out_cleanup: |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 340 | if (!tmpfile) |
| 341 | ovl_cleanup(wdir, temp); |
David Howells | ab79efa | 2015-09-18 11:45:22 +0100 | [diff] [blame] | 342 | goto out2; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Copy up a single dentry |
| 347 | * |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 348 | * All renames start with copy up of source if necessary. The actual |
| 349 | * rename will only proceed once the copy up was successful. Copy up uses |
| 350 | * upper parent i_mutex for exclusion. Since rename can change d_parent it |
| 351 | * is possible that the copy up will lock the old parent. At that point |
| 352 | * the file will have already been copied up anyway. |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 353 | */ |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 354 | static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
| 355 | struct path *lowerpath, struct kstat *stat) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 356 | { |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 357 | DEFINE_DELAYED_CALL(done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 358 | struct dentry *workdir = ovl_workdir(dentry); |
| 359 | int err; |
| 360 | struct kstat pstat; |
| 361 | struct path parentpath; |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 362 | struct dentry *lowerdentry = lowerpath->dentry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 363 | struct dentry *upperdir; |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 364 | const char *link = NULL; |
Amir Goldstein | d8514d8 | 2017-01-17 06:34:55 +0200 | [diff] [blame] | 365 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 366 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 367 | if (WARN_ON(!workdir)) |
| 368 | return -EROFS; |
| 369 | |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 370 | ovl_do_check_copy_up(lowerdentry); |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 371 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 372 | ovl_path_upper(parent, &parentpath); |
| 373 | upperdir = parentpath.dentry; |
| 374 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 375 | err = vfs_getattr(&parentpath, &pstat, |
| 376 | STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 377 | if (err) |
| 378 | return err; |
| 379 | |
| 380 | if (S_ISLNK(stat->mode)) { |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 381 | link = vfs_get_link(lowerdentry, &done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 382 | if (IS_ERR(link)) |
| 383 | return PTR_ERR(link); |
| 384 | } |
| 385 | |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 386 | /* Should we copyup with O_TMPFILE or with workdir? */ |
| 387 | if (S_ISREG(stat->mode) && ofs->tmpfile) { |
| 388 | err = ovl_copy_up_start(dentry); |
| 389 | /* err < 0: interrupted, err > 0: raced with another copy-up */ |
| 390 | if (unlikely(err)) { |
| 391 | pr_debug("ovl_copy_up_start(%pd2) = %i\n", dentry, err); |
| 392 | if (err > 0) |
| 393 | err = 0; |
| 394 | goto out_done; |
| 395 | } |
| 396 | |
| 397 | inode_lock_nested(upperdir->d_inode, I_MUTEX_PARENT); |
| 398 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, |
| 399 | stat, link, &pstat, true); |
| 400 | inode_unlock(upperdir->d_inode); |
| 401 | ovl_copy_up_end(dentry); |
| 402 | goto out_done; |
| 403 | } |
| 404 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 405 | err = -EIO; |
| 406 | if (lock_rename(workdir, upperdir) != NULL) { |
| 407 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); |
| 408 | goto out_unlock; |
| 409 | } |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 410 | if (ovl_dentry_upper(dentry)) { |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 411 | /* Raced with another copy-up? Nothing to do, then... */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 412 | err = 0; |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 413 | goto out_unlock; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 417 | stat, link, &pstat, false); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 418 | out_unlock: |
| 419 | unlock_rename(workdir, upperdir); |
Amir Goldstein | 01ad3eb | 2017-01-17 06:34:57 +0200 | [diff] [blame] | 420 | out_done: |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 421 | do_delayed_call(&done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 422 | |
| 423 | return err; |
| 424 | } |
| 425 | |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 426 | int ovl_copy_up_flags(struct dentry *dentry, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 427 | { |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 428 | int err = 0; |
| 429 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 430 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 431 | while (!err) { |
| 432 | struct dentry *next; |
| 433 | struct dentry *parent; |
| 434 | struct path lowerpath; |
| 435 | struct kstat stat; |
| 436 | enum ovl_path_type type = ovl_path_type(dentry); |
| 437 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 438 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 439 | break; |
| 440 | |
| 441 | next = dget(dentry); |
| 442 | /* find the topmost dentry not yet copied up */ |
| 443 | for (;;) { |
| 444 | parent = dget_parent(next); |
| 445 | |
| 446 | type = ovl_path_type(parent); |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 447 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | dput(next); |
| 451 | next = parent; |
| 452 | } |
| 453 | |
| 454 | ovl_path_lower(next, &lowerpath); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 455 | err = vfs_getattr(&lowerpath, &stat, |
| 456 | STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 457 | /* maybe truncate regular file. this has no effect on dirs */ |
| 458 | if (flags & O_TRUNC) |
| 459 | stat.size = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 460 | if (!err) |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 461 | err = ovl_copy_up_one(parent, next, &lowerpath, &stat); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 462 | |
| 463 | dput(parent); |
| 464 | dput(next); |
| 465 | } |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 466 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 467 | |
| 468 | return err; |
| 469 | } |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 470 | |
| 471 | int ovl_copy_up(struct dentry *dentry) |
| 472 | { |
| 473 | return ovl_copy_up_flags(dentry, 0); |
| 474 | } |