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