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