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