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