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 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/namei.h> |
| 12 | #include <linux/xattr.h> |
| 13 | #include <linux/security.h> |
| 14 | #include <linux/cred.h> |
Miklos Szeredi | 38b2569 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 15 | #include <linux/posix_acl.h> |
| 16 | #include <linux/posix_acl_xattr.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 17 | #include "overlayfs.h" |
| 18 | |
| 19 | void ovl_cleanup(struct inode *wdir, struct dentry *wdentry) |
| 20 | { |
| 21 | int err; |
| 22 | |
| 23 | dget(wdentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 24 | if (d_is_dir(wdentry)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 25 | err = ovl_do_rmdir(wdir, wdentry); |
| 26 | else |
| 27 | err = ovl_do_unlink(wdir, wdentry); |
| 28 | dput(wdentry); |
| 29 | |
| 30 | if (err) { |
| 31 | pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n", |
| 32 | wdentry, err); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry) |
| 37 | { |
| 38 | struct dentry *temp; |
| 39 | char name[20]; |
| 40 | |
| 41 | snprintf(name, sizeof(name), "#%lx", (unsigned long) dentry); |
| 42 | |
| 43 | temp = lookup_one_len(name, workdir, strlen(name)); |
| 44 | if (!IS_ERR(temp) && temp->d_inode) { |
| 45 | pr_err("overlayfs: workdir/%s already exists\n", name); |
| 46 | dput(temp); |
| 47 | temp = ERR_PTR(-EIO); |
| 48 | } |
| 49 | |
| 50 | return temp; |
| 51 | } |
| 52 | |
| 53 | /* caller holds i_mutex on workdir */ |
| 54 | static struct dentry *ovl_whiteout(struct dentry *workdir, |
| 55 | struct dentry *dentry) |
| 56 | { |
| 57 | int err; |
| 58 | struct dentry *whiteout; |
| 59 | struct inode *wdir = workdir->d_inode; |
| 60 | |
| 61 | whiteout = ovl_lookup_temp(workdir, dentry); |
| 62 | if (IS_ERR(whiteout)) |
| 63 | return whiteout; |
| 64 | |
| 65 | err = ovl_do_whiteout(wdir, whiteout); |
| 66 | if (err) { |
| 67 | dput(whiteout); |
| 68 | whiteout = ERR_PTR(err); |
| 69 | } |
| 70 | |
| 71 | return whiteout; |
| 72 | } |
| 73 | |
| 74 | int ovl_create_real(struct inode *dir, struct dentry *newdentry, |
| 75 | struct kstat *stat, const char *link, |
| 76 | struct dentry *hardlink, bool debug) |
| 77 | { |
| 78 | int err; |
| 79 | |
| 80 | if (newdentry->d_inode) |
| 81 | return -ESTALE; |
| 82 | |
| 83 | if (hardlink) { |
| 84 | err = ovl_do_link(hardlink, dir, newdentry, debug); |
| 85 | } else { |
| 86 | switch (stat->mode & S_IFMT) { |
| 87 | case S_IFREG: |
| 88 | err = ovl_do_create(dir, newdentry, stat->mode, debug); |
| 89 | break; |
| 90 | |
| 91 | case S_IFDIR: |
| 92 | err = ovl_do_mkdir(dir, newdentry, stat->mode, debug); |
| 93 | break; |
| 94 | |
| 95 | case S_IFCHR: |
| 96 | case S_IFBLK: |
| 97 | case S_IFIFO: |
| 98 | case S_IFSOCK: |
| 99 | err = ovl_do_mknod(dir, newdentry, |
| 100 | stat->mode, stat->rdev, debug); |
| 101 | break; |
| 102 | |
| 103 | case S_IFLNK: |
| 104 | err = ovl_do_symlink(dir, newdentry, link, debug); |
| 105 | break; |
| 106 | |
| 107 | default: |
| 108 | err = -EPERM; |
| 109 | } |
| 110 | } |
| 111 | if (!err && WARN_ON(!newdentry->d_inode)) { |
| 112 | /* |
| 113 | * Not quite sure if non-instantiated dentry is legal or not. |
| 114 | * VFS doesn't seem to care so check and warn here. |
| 115 | */ |
| 116 | err = -ENOENT; |
| 117 | } |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | static int ovl_set_opaque(struct dentry *upperdentry) |
| 122 | { |
hujianyang | cead89b | 2014-11-24 18:25:21 +0800 | [diff] [blame] | 123 | return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void ovl_remove_opaque(struct dentry *upperdentry) |
| 127 | { |
| 128 | int err; |
| 129 | |
hujianyang | cead89b | 2014-11-24 18:25:21 +0800 | [diff] [blame] | 130 | err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 131 | if (err) { |
| 132 | pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n", |
| 133 | upperdentry->d_name.name, err); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 138 | struct kstat *stat) |
| 139 | { |
| 140 | int err; |
| 141 | enum ovl_path_type type; |
| 142 | struct path realpath; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 143 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 144 | |
| 145 | type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 146 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 147 | err = vfs_getattr(&realpath, stat); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 148 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 149 | if (err) |
| 150 | return err; |
| 151 | |
| 152 | stat->dev = dentry->d_sb->s_dev; |
| 153 | stat->ino = dentry->d_inode->i_ino; |
| 154 | |
| 155 | /* |
| 156 | * It's probably not worth it to count subdirs to get the |
| 157 | * correct link count. nlink=1 seems to pacify 'find' and |
| 158 | * other utilities. |
| 159 | */ |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 160 | if (OVL_TYPE_MERGE(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 161 | stat->nlink = 1; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Vivek Goyal | 72e4848 | 2016-06-16 10:09:14 -0400 | [diff] [blame] | 166 | /* Common operations required to be done after creation of file on upper */ |
| 167 | static void ovl_instantiate(struct dentry *dentry, struct inode *inode, |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 168 | struct dentry *newdentry, bool hardlink) |
Vivek Goyal | 72e4848 | 2016-06-16 10:09:14 -0400 | [diff] [blame] | 169 | { |
| 170 | ovl_dentry_version_inc(dentry->d_parent); |
| 171 | ovl_dentry_update(dentry, newdentry); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 172 | if (!hardlink) { |
| 173 | ovl_inode_update(inode, d_inode(newdentry)); |
| 174 | ovl_copyattr(newdentry->d_inode, inode); |
| 175 | } else { |
| 176 | WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry)); |
| 177 | inc_nlink(inode); |
| 178 | } |
Vivek Goyal | 72e4848 | 2016-06-16 10:09:14 -0400 | [diff] [blame] | 179 | d_instantiate(dentry, inode); |
| 180 | } |
| 181 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 182 | static int ovl_create_upper(struct dentry *dentry, struct inode *inode, |
| 183 | struct kstat *stat, const char *link, |
| 184 | struct dentry *hardlink) |
| 185 | { |
| 186 | struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); |
| 187 | struct inode *udir = upperdir->d_inode; |
| 188 | struct dentry *newdentry; |
| 189 | int err; |
| 190 | |
Miklos Szeredi | 38b2569 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 191 | if (!hardlink && !IS_POSIXACL(udir)) |
| 192 | stat->mode &= ~current_umask(); |
| 193 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 194 | inode_lock_nested(udir, I_MUTEX_PARENT); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 195 | newdentry = lookup_one_len(dentry->d_name.name, upperdir, |
| 196 | dentry->d_name.len); |
| 197 | err = PTR_ERR(newdentry); |
| 198 | if (IS_ERR(newdentry)) |
| 199 | goto out_unlock; |
| 200 | err = ovl_create_real(udir, newdentry, stat, link, hardlink, false); |
| 201 | if (err) |
| 202 | goto out_dput; |
| 203 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 204 | ovl_instantiate(dentry, inode, newdentry, !!hardlink); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 205 | newdentry = NULL; |
| 206 | out_dput: |
| 207 | dput(newdentry); |
| 208 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 209 | inode_unlock(udir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 210 | return err; |
| 211 | } |
| 212 | |
| 213 | static int ovl_lock_rename_workdir(struct dentry *workdir, |
| 214 | struct dentry *upperdir) |
| 215 | { |
| 216 | /* Workdir should not be the same as upperdir */ |
| 217 | if (workdir == upperdir) |
| 218 | goto err; |
| 219 | |
| 220 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 221 | if (lock_rename(workdir, upperdir) != NULL) |
| 222 | goto err_unlock; |
| 223 | |
| 224 | return 0; |
| 225 | |
| 226 | err_unlock: |
| 227 | unlock_rename(workdir, upperdir); |
| 228 | err: |
| 229 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); |
| 230 | return -EIO; |
| 231 | } |
| 232 | |
| 233 | static struct dentry *ovl_clear_empty(struct dentry *dentry, |
| 234 | struct list_head *list) |
| 235 | { |
| 236 | struct dentry *workdir = ovl_workdir(dentry); |
| 237 | struct inode *wdir = workdir->d_inode; |
| 238 | struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); |
| 239 | struct inode *udir = upperdir->d_inode; |
| 240 | struct path upperpath; |
| 241 | struct dentry *upper; |
| 242 | struct dentry *opaquedir; |
| 243 | struct kstat stat; |
| 244 | int err; |
| 245 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 246 | if (WARN_ON(!workdir)) |
| 247 | return ERR_PTR(-EROFS); |
| 248 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 249 | err = ovl_lock_rename_workdir(workdir, upperdir); |
| 250 | if (err) |
| 251 | goto out; |
| 252 | |
| 253 | ovl_path_upper(dentry, &upperpath); |
| 254 | err = vfs_getattr(&upperpath, &stat); |
| 255 | if (err) |
| 256 | goto out_unlock; |
| 257 | |
| 258 | err = -ESTALE; |
| 259 | if (!S_ISDIR(stat.mode)) |
| 260 | goto out_unlock; |
| 261 | upper = upperpath.dentry; |
| 262 | if (upper->d_parent->d_inode != udir) |
| 263 | goto out_unlock; |
| 264 | |
| 265 | opaquedir = ovl_lookup_temp(workdir, dentry); |
| 266 | err = PTR_ERR(opaquedir); |
| 267 | if (IS_ERR(opaquedir)) |
| 268 | goto out_unlock; |
| 269 | |
| 270 | err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true); |
| 271 | if (err) |
| 272 | goto out_dput; |
| 273 | |
| 274 | err = ovl_copy_xattr(upper, opaquedir); |
| 275 | if (err) |
| 276 | goto out_cleanup; |
| 277 | |
| 278 | err = ovl_set_opaque(opaquedir); |
| 279 | if (err) |
| 280 | goto out_cleanup; |
| 281 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 282 | inode_lock(opaquedir->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 283 | err = ovl_set_attr(opaquedir, &stat); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 284 | inode_unlock(opaquedir->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 285 | if (err) |
| 286 | goto out_cleanup; |
| 287 | |
| 288 | err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE); |
| 289 | if (err) |
| 290 | goto out_cleanup; |
| 291 | |
| 292 | ovl_cleanup_whiteouts(upper, list); |
| 293 | ovl_cleanup(wdir, upper); |
| 294 | unlock_rename(workdir, upperdir); |
| 295 | |
| 296 | /* dentry's upper doesn't match now, get rid of it */ |
| 297 | d_drop(dentry); |
| 298 | |
| 299 | return opaquedir; |
| 300 | |
| 301 | out_cleanup: |
| 302 | ovl_cleanup(wdir, opaquedir); |
| 303 | out_dput: |
| 304 | dput(opaquedir); |
| 305 | out_unlock: |
| 306 | unlock_rename(workdir, upperdir); |
| 307 | out: |
| 308 | return ERR_PTR(err); |
| 309 | } |
| 310 | |
Miklos Szeredi | a105d68 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 311 | static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 312 | { |
| 313 | int err; |
| 314 | struct dentry *ret = NULL; |
Miklos Szeredi | 30c17eb | 2016-07-29 12:05:25 +0200 | [diff] [blame] | 315 | enum ovl_path_type type = ovl_path_type(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 316 | LIST_HEAD(list); |
| 317 | |
| 318 | err = ovl_check_empty_dir(dentry, &list); |
Miklos Szeredi | 30c17eb | 2016-07-29 12:05:25 +0200 | [diff] [blame] | 319 | if (err) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 320 | ret = ERR_PTR(err); |
Miklos Szeredi | 30c17eb | 2016-07-29 12:05:25 +0200 | [diff] [blame] | 321 | goto out_free; |
Miklos Szeredi | a105d68 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 322 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 323 | |
Miklos Szeredi | 30c17eb | 2016-07-29 12:05:25 +0200 | [diff] [blame] | 324 | /* |
| 325 | * When removing an empty opaque directory, then it makes no sense to |
| 326 | * replace it with an exact replica of itself. |
| 327 | * |
| 328 | * If no upperdentry then skip clearing whiteouts. |
| 329 | * |
| 330 | * Can race with copy-up, since we don't hold the upperdir mutex. |
| 331 | * Doesn't matter, since copy-up can't create a non-empty directory |
| 332 | * from an empty one. |
| 333 | */ |
| 334 | if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type)) |
| 335 | ret = ovl_clear_empty(dentry, &list); |
| 336 | |
| 337 | out_free: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 338 | ovl_cache_free(&list); |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | |
Miklos Szeredi | 38b2569 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 343 | static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name, |
| 344 | const struct posix_acl *acl) |
| 345 | { |
| 346 | void *buffer; |
| 347 | size_t size; |
| 348 | int err; |
| 349 | |
| 350 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl) |
| 351 | return 0; |
| 352 | |
| 353 | size = posix_acl_to_xattr(NULL, acl, NULL, 0); |
| 354 | buffer = kmalloc(size, GFP_KERNEL); |
| 355 | if (!buffer) |
| 356 | return -ENOMEM; |
| 357 | |
| 358 | size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); |
| 359 | err = size; |
| 360 | if (err < 0) |
| 361 | goto out_free; |
| 362 | |
| 363 | err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE); |
| 364 | out_free: |
| 365 | kfree(buffer); |
| 366 | return err; |
| 367 | } |
| 368 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 369 | static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode, |
| 370 | struct kstat *stat, const char *link, |
| 371 | struct dentry *hardlink) |
| 372 | { |
| 373 | struct dentry *workdir = ovl_workdir(dentry); |
| 374 | struct inode *wdir = workdir->d_inode; |
| 375 | struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); |
| 376 | struct inode *udir = upperdir->d_inode; |
| 377 | struct dentry *upper; |
| 378 | struct dentry *newdentry; |
| 379 | int err; |
Miklos Szeredi | 38b2569 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 380 | struct posix_acl *acl, *default_acl; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 381 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 382 | if (WARN_ON(!workdir)) |
| 383 | return -EROFS; |
| 384 | |
Miklos Szeredi | 38b2569 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 385 | if (!hardlink) { |
| 386 | err = posix_acl_create(dentry->d_parent->d_inode, |
| 387 | &stat->mode, &default_acl, &acl); |
| 388 | if (err) |
| 389 | return err; |
| 390 | } |
| 391 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 392 | err = ovl_lock_rename_workdir(workdir, upperdir); |
| 393 | if (err) |
| 394 | goto out; |
| 395 | |
| 396 | newdentry = ovl_lookup_temp(workdir, dentry); |
| 397 | err = PTR_ERR(newdentry); |
| 398 | if (IS_ERR(newdentry)) |
| 399 | goto out_unlock; |
| 400 | |
| 401 | upper = lookup_one_len(dentry->d_name.name, upperdir, |
| 402 | dentry->d_name.len); |
| 403 | err = PTR_ERR(upper); |
| 404 | if (IS_ERR(upper)) |
| 405 | goto out_dput; |
| 406 | |
| 407 | err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true); |
| 408 | if (err) |
| 409 | goto out_dput2; |
| 410 | |
Miklos Szeredi | bb0d2b8 | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 411 | /* |
| 412 | * mode could have been mutilated due to umask (e.g. sgid directory) |
| 413 | */ |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 414 | if (!hardlink && |
| 415 | !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) { |
Miklos Szeredi | bb0d2b8 | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 416 | struct iattr attr = { |
| 417 | .ia_valid = ATTR_MODE, |
| 418 | .ia_mode = stat->mode, |
| 419 | }; |
| 420 | inode_lock(newdentry->d_inode); |
| 421 | err = notify_change(newdentry, &attr, NULL); |
| 422 | inode_unlock(newdentry->d_inode); |
| 423 | if (err) |
| 424 | goto out_cleanup; |
| 425 | } |
Miklos Szeredi | 38b2569 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 426 | if (!hardlink) { |
| 427 | err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS, |
| 428 | acl); |
| 429 | if (err) |
| 430 | goto out_cleanup; |
| 431 | |
| 432 | err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT, |
| 433 | default_acl); |
| 434 | if (err) |
| 435 | goto out_cleanup; |
| 436 | } |
Miklos Szeredi | bb0d2b8 | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 437 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 438 | if (!hardlink && S_ISDIR(stat->mode)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 439 | err = ovl_set_opaque(newdentry); |
| 440 | if (err) |
| 441 | goto out_cleanup; |
| 442 | |
| 443 | err = ovl_do_rename(wdir, newdentry, udir, upper, |
| 444 | RENAME_EXCHANGE); |
| 445 | if (err) |
| 446 | goto out_cleanup; |
| 447 | |
| 448 | ovl_cleanup(wdir, upper); |
| 449 | } else { |
| 450 | err = ovl_do_rename(wdir, newdentry, udir, upper, 0); |
| 451 | if (err) |
| 452 | goto out_cleanup; |
| 453 | } |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 454 | ovl_instantiate(dentry, inode, newdentry, !!hardlink); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 455 | newdentry = NULL; |
| 456 | out_dput2: |
| 457 | dput(upper); |
| 458 | out_dput: |
| 459 | dput(newdentry); |
| 460 | out_unlock: |
| 461 | unlock_rename(workdir, upperdir); |
| 462 | out: |
Miklos Szeredi | 38b2569 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 463 | if (!hardlink) { |
| 464 | posix_acl_release(acl); |
| 465 | posix_acl_release(default_acl); |
| 466 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 467 | return err; |
| 468 | |
| 469 | out_cleanup: |
| 470 | ovl_cleanup(wdir, newdentry); |
| 471 | goto out_dput2; |
| 472 | } |
| 473 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 474 | static int ovl_create_or_link(struct dentry *dentry, struct inode *inode, |
| 475 | struct kstat *stat, const char *link, |
| 476 | struct dentry *hardlink) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 477 | { |
| 478 | int err; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 479 | const struct cred *old_cred; |
| 480 | struct cred *override_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 481 | |
| 482 | err = ovl_copy_up(dentry->d_parent); |
| 483 | if (err) |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 484 | return err; |
Miklos Szeredi | bb0d2b8 | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 485 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 486 | old_cred = ovl_override_creds(dentry->d_sb); |
| 487 | err = -ENOMEM; |
| 488 | override_cred = prepare_creds(); |
| 489 | if (override_cred) { |
Miklos Szeredi | bb0d2b8 | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 490 | override_cred->fsuid = inode->i_uid; |
| 491 | override_cred->fsgid = inode->i_gid; |
Vivek Goyal | 2602625 | 2016-07-13 10:44:52 -0400 | [diff] [blame] | 492 | if (!hardlink) { |
| 493 | err = security_dentry_create_files_as(dentry, |
| 494 | stat->mode, &dentry->d_name, old_cred, |
| 495 | override_cred); |
| 496 | if (err) { |
| 497 | put_cred(override_cred); |
| 498 | goto out_revert_creds; |
| 499 | } |
| 500 | } |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 501 | put_cred(override_creds(override_cred)); |
| 502 | put_cred(override_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 503 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 504 | if (!ovl_dentry_is_opaque(dentry)) |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 505 | err = ovl_create_upper(dentry, inode, stat, link, |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 506 | hardlink); |
| 507 | else |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 508 | err = ovl_create_over_whiteout(dentry, inode, stat, |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 509 | link, hardlink); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 510 | } |
Vivek Goyal | 2602625 | 2016-07-13 10:44:52 -0400 | [diff] [blame] | 511 | out_revert_creds: |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 512 | revert_creds(old_cred); |
Miklos Szeredi | bb0d2b8 | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 513 | if (!err) { |
| 514 | struct inode *realinode = d_inode(ovl_dentry_upper(dentry)); |
| 515 | |
| 516 | WARN_ON(inode->i_mode != realinode->i_mode); |
| 517 | WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid)); |
| 518 | WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid)); |
Miklos Szeredi | bb0d2b8 | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 519 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 520 | return err; |
| 521 | } |
| 522 | |
| 523 | static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev, |
| 524 | const char *link) |
| 525 | { |
| 526 | int err; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 527 | struct inode *inode; |
| 528 | struct kstat stat = { |
| 529 | .rdev = rdev, |
| 530 | }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 531 | |
| 532 | err = ovl_want_write(dentry); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 533 | if (err) |
| 534 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 535 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 536 | err = -ENOMEM; |
| 537 | inode = ovl_new_inode(dentry->d_sb, mode); |
| 538 | if (!inode) |
| 539 | goto out_drop_write; |
| 540 | |
| 541 | inode_init_owner(inode, dentry->d_parent->d_inode, mode); |
| 542 | stat.mode = inode->i_mode; |
| 543 | |
| 544 | err = ovl_create_or_link(dentry, inode, &stat, link, NULL); |
| 545 | if (err) |
| 546 | iput(inode); |
| 547 | |
| 548 | out_drop_write: |
| 549 | ovl_drop_write(dentry); |
| 550 | out: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 551 | return err; |
| 552 | } |
| 553 | |
| 554 | static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 555 | bool excl) |
| 556 | { |
| 557 | return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL); |
| 558 | } |
| 559 | |
| 560 | static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 561 | { |
| 562 | return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL); |
| 563 | } |
| 564 | |
| 565 | static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, |
| 566 | dev_t rdev) |
| 567 | { |
| 568 | /* Don't allow creation of "whiteout" on overlay */ |
| 569 | if (S_ISCHR(mode) && rdev == WHITEOUT_DEV) |
| 570 | return -EPERM; |
| 571 | |
| 572 | return ovl_create_object(dentry, mode, rdev, NULL); |
| 573 | } |
| 574 | |
| 575 | static int ovl_symlink(struct inode *dir, struct dentry *dentry, |
| 576 | const char *link) |
| 577 | { |
| 578 | return ovl_create_object(dentry, S_IFLNK, 0, link); |
| 579 | } |
| 580 | |
| 581 | static int ovl_link(struct dentry *old, struct inode *newdir, |
| 582 | struct dentry *new) |
| 583 | { |
| 584 | int err; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 585 | struct inode *inode; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 586 | |
| 587 | err = ovl_want_write(old); |
| 588 | if (err) |
| 589 | goto out; |
| 590 | |
| 591 | err = ovl_copy_up(old); |
| 592 | if (err) |
| 593 | goto out_drop_write; |
| 594 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 595 | inode = d_inode(old); |
| 596 | ihold(inode); |
| 597 | |
| 598 | err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old)); |
| 599 | if (err) |
| 600 | iput(inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 601 | |
| 602 | out_drop_write: |
| 603 | ovl_drop_write(old); |
| 604 | out: |
| 605 | return err; |
| 606 | } |
| 607 | |
Miklos Szeredi | a105d68 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 608 | static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 609 | { |
| 610 | struct dentry *workdir = ovl_workdir(dentry); |
| 611 | struct inode *wdir = workdir->d_inode; |
| 612 | struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); |
| 613 | struct inode *udir = upperdir->d_inode; |
| 614 | struct dentry *whiteout; |
| 615 | struct dentry *upper; |
| 616 | struct dentry *opaquedir = NULL; |
| 617 | int err; |
Maxim Patlasov | cfc9fde | 2016-07-21 18:24:26 -0700 | [diff] [blame] | 618 | int flags = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 619 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 620 | if (WARN_ON(!workdir)) |
| 621 | return -EROFS; |
| 622 | |
Miklos Szeredi | d377c5e | 2015-05-14 10:04:44 +0200 | [diff] [blame] | 623 | if (is_dir) { |
Miklos Szeredi | 30c17eb | 2016-07-29 12:05:25 +0200 | [diff] [blame] | 624 | opaquedir = ovl_check_empty_and_clear(dentry); |
| 625 | err = PTR_ERR(opaquedir); |
| 626 | if (IS_ERR(opaquedir)) |
| 627 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | err = ovl_lock_rename_workdir(workdir, upperdir); |
| 631 | if (err) |
| 632 | goto out_dput; |
| 633 | |
Maxim Patlasov | cfc9fde | 2016-07-21 18:24:26 -0700 | [diff] [blame] | 634 | upper = lookup_one_len(dentry->d_name.name, upperdir, |
| 635 | dentry->d_name.len); |
| 636 | err = PTR_ERR(upper); |
| 637 | if (IS_ERR(upper)) |
| 638 | goto out_unlock; |
| 639 | |
| 640 | err = -ESTALE; |
| 641 | if ((opaquedir && upper != opaquedir) || |
| 642 | (!opaquedir && ovl_dentry_upper(dentry) && |
| 643 | upper != ovl_dentry_upper(dentry))) { |
| 644 | goto out_dput_upper; |
| 645 | } |
| 646 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 647 | whiteout = ovl_whiteout(workdir, dentry); |
| 648 | err = PTR_ERR(whiteout); |
| 649 | if (IS_ERR(whiteout)) |
Maxim Patlasov | cfc9fde | 2016-07-21 18:24:26 -0700 | [diff] [blame] | 650 | goto out_dput_upper; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 651 | |
Maxim Patlasov | cfc9fde | 2016-07-21 18:24:26 -0700 | [diff] [blame] | 652 | if (d_is_dir(upper)) |
| 653 | flags = RENAME_EXCHANGE; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 654 | |
Maxim Patlasov | cfc9fde | 2016-07-21 18:24:26 -0700 | [diff] [blame] | 655 | err = ovl_do_rename(wdir, whiteout, udir, upper, flags); |
| 656 | if (err) |
| 657 | goto kill_whiteout; |
| 658 | if (flags) |
| 659 | ovl_cleanup(wdir, upper); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 660 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 661 | ovl_dentry_version_inc(dentry->d_parent); |
| 662 | out_d_drop: |
| 663 | d_drop(dentry); |
| 664 | dput(whiteout); |
Maxim Patlasov | cfc9fde | 2016-07-21 18:24:26 -0700 | [diff] [blame] | 665 | out_dput_upper: |
| 666 | dput(upper); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 667 | out_unlock: |
| 668 | unlock_rename(workdir, upperdir); |
| 669 | out_dput: |
| 670 | dput(opaquedir); |
| 671 | out: |
| 672 | return err; |
| 673 | |
| 674 | kill_whiteout: |
| 675 | ovl_cleanup(wdir, whiteout); |
| 676 | goto out_d_drop; |
| 677 | } |
| 678 | |
| 679 | static int ovl_remove_upper(struct dentry *dentry, bool is_dir) |
| 680 | { |
| 681 | struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); |
| 682 | struct inode *dir = upperdir->d_inode; |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 683 | struct dentry *upper; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 684 | int err; |
| 685 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 686 | inode_lock_nested(dir, I_MUTEX_PARENT); |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 687 | upper = lookup_one_len(dentry->d_name.name, upperdir, |
| 688 | dentry->d_name.len); |
| 689 | err = PTR_ERR(upper); |
| 690 | if (IS_ERR(upper)) |
| 691 | goto out_unlock; |
| 692 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 693 | err = -ESTALE; |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 694 | if (upper == ovl_dentry_upper(dentry)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 695 | if (is_dir) |
| 696 | err = vfs_rmdir(dir, upper); |
| 697 | else |
| 698 | err = vfs_unlink(dir, upper, NULL); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 699 | ovl_dentry_version_inc(dentry->d_parent); |
| 700 | } |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 701 | dput(upper); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * Keeping this dentry hashed would mean having to release |
| 705 | * upperpath/lowerpath, which could only be done if we are the |
| 706 | * sole user of this dentry. Too tricky... Just unhash for |
| 707 | * now. |
| 708 | */ |
Rui Wang | ce9113b | 2016-01-08 23:09:59 +0800 | [diff] [blame] | 709 | if (!err) |
| 710 | d_drop(dentry); |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 711 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 712 | inode_unlock(dir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 713 | |
| 714 | return err; |
| 715 | } |
| 716 | |
| 717 | static inline int ovl_check_sticky(struct dentry *dentry) |
| 718 | { |
| 719 | struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode; |
| 720 | struct inode *inode = ovl_dentry_real(dentry)->d_inode; |
| 721 | |
| 722 | if (check_sticky(dir, inode)) |
| 723 | return -EPERM; |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int ovl_do_remove(struct dentry *dentry, bool is_dir) |
| 729 | { |
| 730 | enum ovl_path_type type; |
| 731 | int err; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 732 | const struct cred *old_cred; |
| 733 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 734 | |
| 735 | err = ovl_check_sticky(dentry); |
| 736 | if (err) |
| 737 | goto out; |
| 738 | |
| 739 | err = ovl_want_write(dentry); |
| 740 | if (err) |
| 741 | goto out; |
| 742 | |
| 743 | err = ovl_copy_up(dentry->d_parent); |
| 744 | if (err) |
| 745 | goto out_drop_write; |
| 746 | |
| 747 | type = ovl_path_type(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 748 | |
| 749 | old_cred = ovl_override_creds(dentry->d_sb); |
| 750 | if (OVL_TYPE_PURE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 751 | err = ovl_remove_upper(dentry, is_dir); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 752 | else |
Miklos Szeredi | a105d68 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 753 | err = ovl_remove_and_whiteout(dentry, is_dir); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 754 | revert_creds(old_cred); |
Miklos Szeredi | dbc816d | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 755 | if (!err) { |
| 756 | if (is_dir) |
| 757 | clear_nlink(dentry->d_inode); |
| 758 | else |
| 759 | drop_nlink(dentry->d_inode); |
| 760 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 761 | out_drop_write: |
| 762 | ovl_drop_write(dentry); |
| 763 | out: |
| 764 | return err; |
| 765 | } |
| 766 | |
| 767 | static int ovl_unlink(struct inode *dir, struct dentry *dentry) |
| 768 | { |
| 769 | return ovl_do_remove(dentry, false); |
| 770 | } |
| 771 | |
| 772 | static int ovl_rmdir(struct inode *dir, struct dentry *dentry) |
| 773 | { |
| 774 | return ovl_do_remove(dentry, true); |
| 775 | } |
| 776 | |
| 777 | static int ovl_rename2(struct inode *olddir, struct dentry *old, |
| 778 | struct inode *newdir, struct dentry *new, |
| 779 | unsigned int flags) |
| 780 | { |
| 781 | int err; |
| 782 | enum ovl_path_type old_type; |
| 783 | enum ovl_path_type new_type; |
| 784 | struct dentry *old_upperdir; |
| 785 | struct dentry *new_upperdir; |
| 786 | struct dentry *olddentry; |
| 787 | struct dentry *newdentry; |
| 788 | struct dentry *trap; |
| 789 | bool old_opaque; |
| 790 | bool new_opaque; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 791 | bool cleanup_whiteout = false; |
| 792 | bool overwrite = !(flags & RENAME_EXCHANGE); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 793 | bool is_dir = d_is_dir(old); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 794 | bool new_is_dir = false; |
| 795 | struct dentry *opaquedir = NULL; |
| 796 | const struct cred *old_cred = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 797 | |
| 798 | err = -EINVAL; |
| 799 | if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE)) |
| 800 | goto out; |
| 801 | |
| 802 | flags &= ~RENAME_NOREPLACE; |
| 803 | |
| 804 | err = ovl_check_sticky(old); |
| 805 | if (err) |
| 806 | goto out; |
| 807 | |
| 808 | /* Don't copy up directory trees */ |
| 809 | old_type = ovl_path_type(old); |
| 810 | err = -EXDEV; |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 811 | if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 812 | goto out; |
| 813 | |
| 814 | if (new->d_inode) { |
| 815 | err = ovl_check_sticky(new); |
| 816 | if (err) |
| 817 | goto out; |
| 818 | |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 819 | if (d_is_dir(new)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 820 | new_is_dir = true; |
| 821 | |
| 822 | new_type = ovl_path_type(new); |
| 823 | err = -EXDEV; |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 824 | if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 825 | goto out; |
| 826 | |
| 827 | err = 0; |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 828 | if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 829 | if (ovl_dentry_lower(old)->d_inode == |
| 830 | ovl_dentry_lower(new)->d_inode) |
| 831 | goto out; |
| 832 | } |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 833 | if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 834 | if (ovl_dentry_upper(old)->d_inode == |
| 835 | ovl_dentry_upper(new)->d_inode) |
| 836 | goto out; |
| 837 | } |
| 838 | } else { |
| 839 | if (ovl_dentry_is_opaque(new)) |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 840 | new_type = __OVL_PATH_UPPER; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 841 | else |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 842 | new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | err = ovl_want_write(old); |
| 846 | if (err) |
| 847 | goto out; |
| 848 | |
| 849 | err = ovl_copy_up(old); |
| 850 | if (err) |
| 851 | goto out_drop_write; |
| 852 | |
| 853 | err = ovl_copy_up(new->d_parent); |
| 854 | if (err) |
| 855 | goto out_drop_write; |
| 856 | if (!overwrite) { |
| 857 | err = ovl_copy_up(new); |
| 858 | if (err) |
| 859 | goto out_drop_write; |
| 860 | } |
| 861 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 862 | old_opaque = !OVL_TYPE_PURE_UPPER(old_type); |
| 863 | new_opaque = !OVL_TYPE_PURE_UPPER(new_type); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 864 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 865 | old_cred = ovl_override_creds(old->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 866 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 867 | if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) { |
Miklos Szeredi | a105d68 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 868 | opaquedir = ovl_check_empty_and_clear(new); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 869 | err = PTR_ERR(opaquedir); |
| 870 | if (IS_ERR(opaquedir)) { |
| 871 | opaquedir = NULL; |
| 872 | goto out_revert_creds; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (overwrite) { |
| 877 | if (old_opaque) { |
| 878 | if (new->d_inode || !new_opaque) { |
| 879 | /* Whiteout source */ |
| 880 | flags |= RENAME_WHITEOUT; |
| 881 | } else { |
| 882 | /* Switch whiteouts */ |
| 883 | flags |= RENAME_EXCHANGE; |
| 884 | } |
| 885 | } else if (is_dir && !new->d_inode && new_opaque) { |
| 886 | flags |= RENAME_EXCHANGE; |
| 887 | cleanup_whiteout = true; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | old_upperdir = ovl_dentry_upper(old->d_parent); |
| 892 | new_upperdir = ovl_dentry_upper(new->d_parent); |
| 893 | |
| 894 | trap = lock_rename(new_upperdir, old_upperdir); |
| 895 | |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 896 | |
| 897 | olddentry = lookup_one_len(old->d_name.name, old_upperdir, |
| 898 | old->d_name.len); |
| 899 | err = PTR_ERR(olddentry); |
| 900 | if (IS_ERR(olddentry)) |
| 901 | goto out_unlock; |
| 902 | |
| 903 | err = -ESTALE; |
| 904 | if (olddentry != ovl_dentry_upper(old)) |
| 905 | goto out_dput_old; |
| 906 | |
| 907 | newdentry = lookup_one_len(new->d_name.name, new_upperdir, |
| 908 | new->d_name.len); |
| 909 | err = PTR_ERR(newdentry); |
| 910 | if (IS_ERR(newdentry)) |
| 911 | goto out_dput_old; |
| 912 | |
| 913 | err = -ESTALE; |
| 914 | if (ovl_dentry_upper(new)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 915 | if (opaquedir) { |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 916 | if (newdentry != opaquedir) |
| 917 | goto out_dput; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 918 | } else { |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 919 | if (newdentry != ovl_dentry_upper(new)) |
| 920 | goto out_dput; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 921 | } |
| 922 | } else { |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 923 | if (!d_is_negative(newdentry) && |
| 924 | (!new_opaque || !ovl_is_whiteout(newdentry))) |
| 925 | goto out_dput; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 928 | if (olddentry == trap) |
| 929 | goto out_dput; |
| 930 | if (newdentry == trap) |
| 931 | goto out_dput; |
| 932 | |
| 933 | if (is_dir && !old_opaque && new_opaque) { |
| 934 | err = ovl_set_opaque(olddentry); |
| 935 | if (err) |
| 936 | goto out_dput; |
| 937 | } |
| 938 | if (!overwrite && new_is_dir && old_opaque && !new_opaque) { |
| 939 | err = ovl_set_opaque(newdentry); |
| 940 | if (err) |
| 941 | goto out_dput; |
| 942 | } |
| 943 | |
| 944 | if (old_opaque || new_opaque) { |
| 945 | err = ovl_do_rename(old_upperdir->d_inode, olddentry, |
| 946 | new_upperdir->d_inode, newdentry, |
| 947 | flags); |
| 948 | } else { |
| 949 | /* No debug for the plain case */ |
| 950 | BUG_ON(flags & ~RENAME_EXCHANGE); |
| 951 | err = vfs_rename(old_upperdir->d_inode, olddentry, |
| 952 | new_upperdir->d_inode, newdentry, |
| 953 | NULL, flags); |
| 954 | } |
| 955 | |
| 956 | if (err) { |
| 957 | if (is_dir && !old_opaque && new_opaque) |
| 958 | ovl_remove_opaque(olddentry); |
| 959 | if (!overwrite && new_is_dir && old_opaque && !new_opaque) |
| 960 | ovl_remove_opaque(newdentry); |
| 961 | goto out_dput; |
| 962 | } |
| 963 | |
| 964 | if (is_dir && old_opaque && !new_opaque) |
| 965 | ovl_remove_opaque(olddentry); |
| 966 | if (!overwrite && new_is_dir && !old_opaque && new_opaque) |
| 967 | ovl_remove_opaque(newdentry); |
| 968 | |
Konstantin Khlebnikov | 45d1173 | 2016-01-31 16:17:53 +0300 | [diff] [blame] | 969 | /* |
| 970 | * Old dentry now lives in different location. Dentries in |
| 971 | * lowerstack are stale. We cannot drop them here because |
| 972 | * access to them is lockless. This could be only pure upper |
| 973 | * or opaque directory - numlower is zero. Or upper non-dir |
| 974 | * entry - its pureness is tracked by flag opaque. |
| 975 | */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 976 | if (old_opaque != new_opaque) { |
| 977 | ovl_dentry_set_opaque(old, new_opaque); |
| 978 | if (!overwrite) |
| 979 | ovl_dentry_set_opaque(new, old_opaque); |
| 980 | } |
| 981 | |
| 982 | if (cleanup_whiteout) |
| 983 | ovl_cleanup(old_upperdir->d_inode, newdentry); |
| 984 | |
| 985 | ovl_dentry_version_inc(old->d_parent); |
| 986 | ovl_dentry_version_inc(new->d_parent); |
| 987 | |
| 988 | out_dput: |
| 989 | dput(newdentry); |
Miklos Szeredi | 11f3710 | 2016-03-21 17:31:44 +0100 | [diff] [blame] | 990 | out_dput_old: |
| 991 | dput(olddentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 992 | out_unlock: |
| 993 | unlock_rename(new_upperdir, old_upperdir); |
| 994 | out_revert_creds: |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 995 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 996 | out_drop_write: |
| 997 | ovl_drop_write(old); |
| 998 | out: |
| 999 | dput(opaquedir); |
| 1000 | return err; |
| 1001 | } |
| 1002 | |
| 1003 | const struct inode_operations ovl_dir_inode_operations = { |
| 1004 | .lookup = ovl_lookup, |
| 1005 | .mkdir = ovl_mkdir, |
| 1006 | .symlink = ovl_symlink, |
| 1007 | .unlink = ovl_unlink, |
| 1008 | .rmdir = ovl_rmdir, |
| 1009 | .rename2 = ovl_rename2, |
| 1010 | .link = ovl_link, |
| 1011 | .setattr = ovl_setattr, |
| 1012 | .create = ovl_create, |
| 1013 | .mknod = ovl_mknod, |
| 1014 | .permission = ovl_permission, |
| 1015 | .getattr = ovl_dir_getattr, |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1016 | .setxattr = generic_setxattr, |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1017 | .getxattr = generic_getxattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1018 | .listxattr = ovl_listxattr, |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 1019 | .removexattr = generic_removexattr, |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 1020 | .get_acl = ovl_get_acl, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 1021 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1022 | }; |