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