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