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> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/namei.h> |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 20 | #include <linux/fdtable.h> |
| 21 | #include <linux/ratelimit.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | #include "overlayfs.h" |
| 23 | |
| 24 | #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) |
| 25 | |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 26 | static bool __read_mostly ovl_check_copy_up; |
| 27 | module_param_named(check_copy_up, ovl_check_copy_up, bool, |
| 28 | S_IWUSR | S_IRUGO); |
| 29 | MODULE_PARM_DESC(ovl_check_copy_up, |
| 30 | "Warn on copy-up when causing process also has a R/O fd open"); |
| 31 | |
| 32 | static int ovl_check_fd(const void *data, struct file *f, unsigned int fd) |
| 33 | { |
| 34 | const struct dentry *dentry = data; |
| 35 | |
| 36 | if (f->f_inode == d_inode(dentry)) |
| 37 | 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", |
| 38 | f, fd, current->pid, current->comm); |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Check the fds open by this process and warn if something like the following |
| 44 | * scenario is about to occur: |
| 45 | * |
| 46 | * fd1 = open("foo", O_RDONLY); |
| 47 | * fd2 = open("foo", O_RDWR); |
| 48 | */ |
| 49 | static void ovl_do_check_copy_up(struct dentry *dentry) |
| 50 | { |
| 51 | if (ovl_check_copy_up) |
| 52 | iterate_fd(current->files, 0, ovl_check_fd, dentry); |
| 53 | } |
| 54 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 55 | int ovl_copy_xattr(struct dentry *old, struct dentry *new) |
| 56 | { |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 57 | ssize_t list_size, size, value_size = 0; |
| 58 | char *buf, *name, *value = NULL; |
| 59 | int uninitialized_var(error); |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 60 | size_t slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 61 | |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 62 | if (!(old->d_inode->i_opflags & IOP_XATTR) || |
| 63 | !(new->d_inode->i_opflags & IOP_XATTR)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 64 | return 0; |
| 65 | |
| 66 | list_size = vfs_listxattr(old, NULL, 0); |
| 67 | if (list_size <= 0) { |
| 68 | if (list_size == -EOPNOTSUPP) |
| 69 | return 0; |
| 70 | return list_size; |
| 71 | } |
| 72 | |
| 73 | buf = kzalloc(list_size, GFP_KERNEL); |
| 74 | if (!buf) |
| 75 | return -ENOMEM; |
| 76 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 77 | list_size = vfs_listxattr(old, buf, list_size); |
| 78 | if (list_size <= 0) { |
| 79 | error = list_size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 80 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Miklos Szeredi | 8b326c6 | 2016-09-16 14:12:11 +0200 | [diff] [blame] | 83 | for (name = buf; list_size; name += slen) { |
| 84 | slen = strnlen(name, list_size) + 1; |
| 85 | |
| 86 | /* underlying fs providing us with an broken xattr list? */ |
| 87 | if (WARN_ON(slen > list_size)) { |
| 88 | error = -EIO; |
| 89 | break; |
| 90 | } |
| 91 | list_size -= slen; |
| 92 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 93 | if (ovl_is_private_xattr(name)) |
| 94 | continue; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 95 | retry: |
| 96 | size = vfs_getxattr(old, name, value, value_size); |
| 97 | if (size == -ERANGE) |
| 98 | size = vfs_getxattr(old, name, NULL, 0); |
| 99 | |
Miklos Szeredi | 97daf8b | 2015-11-10 17:08:41 +0100 | [diff] [blame] | 100 | if (size < 0) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 101 | error = size; |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 102 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 103 | } |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 104 | |
| 105 | if (size > value_size) { |
| 106 | void *new; |
| 107 | |
| 108 | new = krealloc(value, size, GFP_KERNEL); |
| 109 | if (!new) { |
| 110 | error = -ENOMEM; |
| 111 | break; |
| 112 | } |
| 113 | value = new; |
| 114 | value_size = size; |
| 115 | goto retry; |
| 116 | } |
| 117 | |
Vivek Goyal | 121ab82 | 2016-07-13 10:44:49 -0400 | [diff] [blame] | 118 | error = security_inode_copy_up_xattr(name); |
| 119 | if (error < 0 && error != -EOPNOTSUPP) |
| 120 | break; |
| 121 | if (error == 1) { |
| 122 | error = 0; |
| 123 | continue; /* Discard */ |
| 124 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 125 | error = vfs_setxattr(new, name, value, size, 0); |
| 126 | if (error) |
Vito Caputo | e4ad29f | 2015-10-24 07:19:46 -0500 | [diff] [blame] | 127 | break; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 128 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 129 | kfree(value); |
| 130 | out: |
| 131 | kfree(buf); |
| 132 | return error; |
| 133 | } |
| 134 | |
| 135 | static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) |
| 136 | { |
| 137 | struct file *old_file; |
| 138 | struct file *new_file; |
| 139 | loff_t old_pos = 0; |
| 140 | loff_t new_pos = 0; |
| 141 | int error = 0; |
| 142 | |
| 143 | if (len == 0) |
| 144 | return 0; |
| 145 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 146 | old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 147 | if (IS_ERR(old_file)) |
| 148 | return PTR_ERR(old_file); |
| 149 | |
David Howells | 0480334 | 2015-09-18 11:45:12 +0100 | [diff] [blame] | 150 | new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 151 | if (IS_ERR(new_file)) { |
| 152 | error = PTR_ERR(new_file); |
| 153 | goto out_fput; |
| 154 | } |
| 155 | |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame^] | 156 | /* Try to use clone_file_range to clone up within the same fs */ |
| 157 | error = vfs_clone_file_range(old_file, 0, new_file, 0, len); |
| 158 | if (!error) |
| 159 | goto out; |
| 160 | /* Couldn't clone, so now we try to copy the data */ |
| 161 | error = 0; |
| 162 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 163 | /* FIXME: copy up sparse files efficiently */ |
| 164 | while (len) { |
| 165 | size_t this_len = OVL_COPY_UP_CHUNK_SIZE; |
| 166 | long bytes; |
| 167 | |
| 168 | if (len < this_len) |
| 169 | this_len = len; |
| 170 | |
| 171 | if (signal_pending_state(TASK_KILLABLE, current)) { |
| 172 | error = -EINTR; |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | bytes = do_splice_direct(old_file, &old_pos, |
| 177 | new_file, &new_pos, |
| 178 | this_len, SPLICE_F_MOVE); |
| 179 | if (bytes <= 0) { |
| 180 | error = bytes; |
| 181 | break; |
| 182 | } |
| 183 | WARN_ON(old_pos != new_pos); |
| 184 | |
| 185 | len -= bytes; |
| 186 | } |
Amir Goldstein | 2ea9846 | 2016-09-23 11:38:12 +0300 | [diff] [blame^] | 187 | out: |
Miklos Szeredi | 641089c | 2016-10-31 14:42:14 +0100 | [diff] [blame] | 188 | if (!error) |
| 189 | error = vfs_fsync(new_file, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 190 | fput(new_file); |
| 191 | out_fput: |
| 192 | fput(old_file); |
| 193 | return error; |
| 194 | } |
| 195 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 196 | static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
| 197 | { |
| 198 | struct iattr attr = { |
| 199 | .ia_valid = |
| 200 | ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, |
| 201 | .ia_atime = stat->atime, |
| 202 | .ia_mtime = stat->mtime, |
| 203 | }; |
| 204 | |
| 205 | return notify_change(upperdentry, &attr, NULL); |
| 206 | } |
| 207 | |
| 208 | int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) |
| 209 | { |
| 210 | int err = 0; |
| 211 | |
| 212 | if (!S_ISLNK(stat->mode)) { |
| 213 | struct iattr attr = { |
| 214 | .ia_valid = ATTR_MODE, |
| 215 | .ia_mode = stat->mode, |
| 216 | }; |
| 217 | err = notify_change(upperdentry, &attr, NULL); |
| 218 | } |
| 219 | if (!err) { |
| 220 | struct iattr attr = { |
| 221 | .ia_valid = ATTR_UID | ATTR_GID, |
| 222 | .ia_uid = stat->uid, |
| 223 | .ia_gid = stat->gid, |
| 224 | }; |
| 225 | err = notify_change(upperdentry, &attr, NULL); |
| 226 | } |
| 227 | if (!err) |
| 228 | ovl_set_timestamps(upperdentry, stat); |
| 229 | |
| 230 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, |
| 234 | struct dentry *dentry, struct path *lowerpath, |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 235 | struct kstat *stat, const char *link) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 236 | { |
| 237 | struct inode *wdir = workdir->d_inode; |
| 238 | struct inode *udir = upperdir->d_inode; |
| 239 | struct dentry *newdentry = NULL; |
| 240 | struct dentry *upper = NULL; |
| 241 | umode_t mode = stat->mode; |
| 242 | int err; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 243 | const struct cred *old_creds = NULL; |
| 244 | struct cred *new_creds = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 245 | |
| 246 | newdentry = ovl_lookup_temp(workdir, dentry); |
| 247 | err = PTR_ERR(newdentry); |
| 248 | if (IS_ERR(newdentry)) |
| 249 | goto out; |
| 250 | |
| 251 | upper = lookup_one_len(dentry->d_name.name, upperdir, |
| 252 | dentry->d_name.len); |
| 253 | err = PTR_ERR(upper); |
| 254 | if (IS_ERR(upper)) |
| 255 | goto out1; |
| 256 | |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 257 | err = security_inode_copy_up(dentry, &new_creds); |
| 258 | if (err < 0) |
| 259 | goto out2; |
| 260 | |
| 261 | if (new_creds) |
| 262 | old_creds = override_creds(new_creds); |
| 263 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 264 | /* Can't properly set mode on creation because of the umask */ |
| 265 | stat->mode &= S_IFMT; |
| 266 | err = ovl_create_real(wdir, newdentry, stat, link, NULL, true); |
| 267 | stat->mode = mode; |
Vivek Goyal | d8ad8b4 | 2016-07-13 11:13:56 -0400 | [diff] [blame] | 268 | |
| 269 | if (new_creds) { |
| 270 | revert_creds(old_creds); |
| 271 | put_cred(new_creds); |
| 272 | } |
| 273 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 274 | if (err) |
| 275 | goto out2; |
| 276 | |
| 277 | if (S_ISREG(stat->mode)) { |
| 278 | struct path upperpath; |
Sohom Bhattacharjee | f134f24 | 2016-03-15 20:57:59 +0530 | [diff] [blame] | 279 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 280 | ovl_path_upper(dentry, &upperpath); |
| 281 | BUG_ON(upperpath.dentry != NULL); |
| 282 | upperpath.dentry = newdentry; |
| 283 | |
| 284 | err = ovl_copy_up_data(lowerpath, &upperpath, stat->size); |
| 285 | if (err) |
| 286 | goto out_cleanup; |
| 287 | } |
| 288 | |
| 289 | err = ovl_copy_xattr(lowerpath->dentry, newdentry); |
| 290 | if (err) |
| 291 | goto out_cleanup; |
| 292 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 293 | inode_lock(newdentry->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 294 | err = ovl_set_attr(newdentry, stat); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 295 | inode_unlock(newdentry->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 296 | if (err) |
| 297 | goto out_cleanup; |
| 298 | |
| 299 | err = ovl_do_rename(wdir, newdentry, udir, upper, 0); |
| 300 | if (err) |
| 301 | goto out_cleanup; |
| 302 | |
| 303 | ovl_dentry_update(dentry, newdentry); |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 304 | ovl_inode_update(d_inode(dentry), d_inode(newdentry)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 305 | newdentry = NULL; |
| 306 | |
| 307 | /* |
| 308 | * Non-directores become opaque when copied up. |
| 309 | */ |
| 310 | if (!S_ISDIR(stat->mode)) |
| 311 | ovl_dentry_set_opaque(dentry, true); |
| 312 | out2: |
| 313 | dput(upper); |
| 314 | out1: |
| 315 | dput(newdentry); |
| 316 | out: |
| 317 | return err; |
| 318 | |
| 319 | out_cleanup: |
| 320 | ovl_cleanup(wdir, newdentry); |
David Howells | ab79efa | 2015-09-18 11:45:22 +0100 | [diff] [blame] | 321 | goto out2; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Copy up a single dentry |
| 326 | * |
| 327 | * Directory renames only allowed on "pure upper" (already created on |
| 328 | * upper filesystem, never copied up). Directories which are on lower or |
| 329 | * are merged may not be renamed. For these -EXDEV is returned and |
| 330 | * userspace has to deal with it. This means, when copying up a |
| 331 | * directory we can rely on it and ancestors being stable. |
| 332 | * |
| 333 | * Non-directory renames start with copy up of source if necessary. The |
| 334 | * actual rename will only proceed once the copy up was successful. Copy |
| 335 | * up uses upper parent i_mutex for exclusion. Since rename can change |
| 336 | * d_parent it is possible that the copy up will lock the old parent. At |
| 337 | * that point the file will have already been copied up anyway. |
| 338 | */ |
| 339 | int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 340 | struct path *lowerpath, struct kstat *stat) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 341 | { |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 342 | DEFINE_DELAYED_CALL(done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 343 | struct dentry *workdir = ovl_workdir(dentry); |
| 344 | int err; |
| 345 | struct kstat pstat; |
| 346 | struct path parentpath; |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 347 | struct dentry *lowerdentry = lowerpath->dentry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 348 | struct dentry *upperdir; |
| 349 | struct dentry *upperdentry; |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 350 | const char *link = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 351 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 352 | if (WARN_ON(!workdir)) |
| 353 | return -EROFS; |
| 354 | |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 355 | ovl_do_check_copy_up(lowerdentry); |
David Howells | fb5bb2c3 | 2015-07-07 15:04:44 +0100 | [diff] [blame] | 356 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 357 | ovl_path_upper(parent, &parentpath); |
| 358 | upperdir = parentpath.dentry; |
| 359 | |
| 360 | err = vfs_getattr(&parentpath, &pstat); |
| 361 | if (err) |
| 362 | return err; |
| 363 | |
| 364 | if (S_ISLNK(stat->mode)) { |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 365 | link = vfs_get_link(lowerdentry, &done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 366 | if (IS_ERR(link)) |
| 367 | return PTR_ERR(link); |
| 368 | } |
| 369 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 370 | err = -EIO; |
| 371 | if (lock_rename(workdir, upperdir) != NULL) { |
| 372 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); |
| 373 | goto out_unlock; |
| 374 | } |
| 375 | upperdentry = ovl_dentry_upper(dentry); |
| 376 | if (upperdentry) { |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 377 | /* Raced with another copy-up? Nothing to do, then... */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 378 | err = 0; |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 379 | goto out_unlock; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath, |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 383 | stat, link); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 384 | if (!err) { |
| 385 | /* Restore timestamps on parent (best effort) */ |
| 386 | ovl_set_timestamps(upperdir, &pstat); |
| 387 | } |
| 388 | out_unlock: |
| 389 | unlock_rename(workdir, upperdir); |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 390 | do_delayed_call(&done); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 391 | |
| 392 | return err; |
| 393 | } |
| 394 | |
| 395 | int ovl_copy_up(struct dentry *dentry) |
| 396 | { |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 397 | int err = 0; |
| 398 | const struct cred *old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 399 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 400 | while (!err) { |
| 401 | struct dentry *next; |
| 402 | struct dentry *parent; |
| 403 | struct path lowerpath; |
| 404 | struct kstat stat; |
| 405 | enum ovl_path_type type = ovl_path_type(dentry); |
| 406 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 407 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | next = dget(dentry); |
| 411 | /* find the topmost dentry not yet copied up */ |
| 412 | for (;;) { |
| 413 | parent = dget_parent(next); |
| 414 | |
| 415 | type = ovl_path_type(parent); |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 416 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 417 | break; |
| 418 | |
| 419 | dput(next); |
| 420 | next = parent; |
| 421 | } |
| 422 | |
| 423 | ovl_path_lower(next, &lowerpath); |
| 424 | err = vfs_getattr(&lowerpath, &stat); |
| 425 | if (!err) |
Al Viro | 0f7ff2d | 2015-12-06 12:31:07 -0500 | [diff] [blame] | 426 | err = ovl_copy_up_one(parent, next, &lowerpath, &stat); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 427 | |
| 428 | dput(parent); |
| 429 | dput(next); |
| 430 | } |
Vivek Goyal | 8eac98b | 2016-09-06 13:40:32 -0400 | [diff] [blame] | 431 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 432 | |
| 433 | return err; |
| 434 | } |