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