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