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