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