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