blob: 8b2b23181b19cfe26b692159ffac6f6fa230335c [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
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 Szeredi3ea22a72016-12-16 11:02:57 +010015#include <linux/module.h>
Miklos Szeredi38b25692016-09-01 11:11:59 +020016#include <linux/posix_acl.h>
17#include <linux/posix_acl_xattr.h>
Richard Weinberger6a45b362016-09-16 11:45:24 +020018#include <linux/atomic.h>
Miklos Szeredia6c60652016-12-16 11:02:56 +010019#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include "overlayfs.h"
21
Miklos Szeredi3ea22a72016-12-16 11:02:57 +010022static unsigned short ovl_redirect_max = 256;
23module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
24MODULE_PARM_DESC(ovl_redirect_max,
25 "Maximum length of absolute redirect xattr value");
26
Amir Goldstein415543d2017-06-21 15:28:42 +030027int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020028{
29 int err;
30
31 dget(wdentry);
David Howellse36cb0b2015-01-29 12:02:35 +000032 if (d_is_dir(wdentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020033 err = ovl_do_rmdir(wdir, wdentry);
34 else
35 err = ovl_do_unlink(wdir, wdentry);
36 dput(wdentry);
37
38 if (err) {
39 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
40 wdentry, err);
41 }
Amir Goldstein415543d2017-06-21 15:28:42 +030042
43 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020044}
45
Miklos Szeredi3d275732017-05-19 09:33:49 +020046struct dentry *ovl_lookup_temp(struct dentry *workdir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020047{
48 struct dentry *temp;
49 char name[20];
Richard Weinberger6a45b362016-09-16 11:45:24 +020050 static atomic_t temp_id = ATOMIC_INIT(0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051
Richard Weinberger6a45b362016-09-16 11:45:24 +020052 /* counter is allowed to wrap, since temp dentries are ephemeral */
53 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
Miklos Szeredie9be9d52014-10-24 00:14:38 +020054
55 temp = lookup_one_len(name, workdir, strlen(name));
56 if (!IS_ERR(temp) && temp->d_inode) {
57 pr_err("overlayfs: workdir/%s already exists\n", name);
58 dput(temp);
59 temp = ERR_PTR(-EIO);
60 }
61
62 return temp;
63}
64
65/* caller holds i_mutex on workdir */
66static struct dentry *ovl_whiteout(struct dentry *workdir,
67 struct dentry *dentry)
68{
69 int err;
70 struct dentry *whiteout;
71 struct inode *wdir = workdir->d_inode;
72
Miklos Szeredi3d275732017-05-19 09:33:49 +020073 whiteout = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020074 if (IS_ERR(whiteout))
75 return whiteout;
76
77 err = ovl_do_whiteout(wdir, whiteout);
78 if (err) {
79 dput(whiteout);
80 whiteout = ERR_PTR(err);
81 }
82
83 return whiteout;
84}
85
86int ovl_create_real(struct inode *dir, struct dentry *newdentry,
Al Viro32a3d842016-12-04 17:33:17 +000087 struct cattr *attr, struct dentry *hardlink, bool debug)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020088{
89 int err;
90
91 if (newdentry->d_inode)
92 return -ESTALE;
93
94 if (hardlink) {
95 err = ovl_do_link(hardlink, dir, newdentry, debug);
96 } else {
Al Viro32a3d842016-12-04 17:33:17 +000097 switch (attr->mode & S_IFMT) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020098 case S_IFREG:
Al Viro32a3d842016-12-04 17:33:17 +000099 err = ovl_do_create(dir, newdentry, attr->mode, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200100 break;
101
102 case S_IFDIR:
Al Viro32a3d842016-12-04 17:33:17 +0000103 err = ovl_do_mkdir(dir, newdentry, attr->mode, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200104 break;
105
106 case S_IFCHR:
107 case S_IFBLK:
108 case S_IFIFO:
109 case S_IFSOCK:
110 err = ovl_do_mknod(dir, newdentry,
Al Viro32a3d842016-12-04 17:33:17 +0000111 attr->mode, attr->rdev, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200112 break;
113
114 case S_IFLNK:
Al Viro32a3d842016-12-04 17:33:17 +0000115 err = ovl_do_symlink(dir, newdentry, attr->link, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200116 break;
117
118 default:
119 err = -EPERM;
120 }
121 }
122 if (!err && WARN_ON(!newdentry->d_inode)) {
123 /*
124 * Not quite sure if non-instantiated dentry is legal or not.
125 * VFS doesn't seem to care so check and warn here.
126 */
127 err = -ENOENT;
128 }
129 return err;
130}
131
Amir Goldstein21a22872017-05-17 00:12:41 +0300132static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
133 int xerr)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134{
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100135 int err;
136
Amir Goldstein21a22872017-05-17 00:12:41 +0300137 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100138 if (!err)
139 ovl_dentry_set_opaque(dentry);
140
141 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142}
143
Amir Goldstein21a22872017-05-17 00:12:41 +0300144static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
145{
146 /*
147 * Fail with -EIO when trying to create opaque dir and upper doesn't
148 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
149 * return a specific error for noxattr case.
150 */
151 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
152}
153
Vivek Goyal72e48482016-06-16 10:09:14 -0400154/* Common operations required to be done after creation of file on upper */
155static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200156 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400157{
158 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200159 ovl_dentry_set_upper_alias(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200160 if (!hardlink) {
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200161 ovl_inode_update(inode, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200162 ovl_copyattr(newdentry->d_inode, inode);
163 } else {
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200164 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
165 dput(newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200166 inc_nlink(inode);
167 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400168 d_instantiate(dentry, inode);
Amir Goldstein5b6c9052017-04-24 19:54:13 +0300169 /* Force lookup of new upper hardlink to find its lower */
170 if (hardlink)
171 d_drop(dentry);
Vivek Goyal72e48482016-06-16 10:09:14 -0400172}
173
Amir Goldstein97c684c2016-11-21 18:57:34 +0200174static bool ovl_type_merge(struct dentry *dentry)
175{
176 return OVL_TYPE_MERGE(ovl_path_type(dentry));
177}
178
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300179static bool ovl_type_origin(struct dentry *dentry)
180{
181 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
182}
183
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200184static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000185 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186{
187 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
188 struct inode *udir = upperdir->d_inode;
189 struct dentry *newdentry;
190 int err;
191
Miklos Szeredi38b25692016-09-01 11:11:59 +0200192 if (!hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000193 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200194
Al Viro59551022016-01-22 15:40:57 -0500195 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
197 dentry->d_name.len);
198 err = PTR_ERR(newdentry);
199 if (IS_ERR(newdentry))
200 goto out_unlock;
Al Viro32a3d842016-12-04 17:33:17 +0000201 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200202 if (err)
203 goto out_dput;
204
Amir Goldstein4a99f3c2017-04-24 22:26:40 +0300205 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
Amir Goldstein97c684c2016-11-21 18:57:34 +0200206 /* Setting opaque here is just an optimization, allow to fail */
207 ovl_set_opaque(dentry, newdentry);
208 }
209
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200210 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211 newdentry = NULL;
212out_dput:
213 dput(newdentry);
214out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500215 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200216 return err;
217}
218
219static int ovl_lock_rename_workdir(struct dentry *workdir,
220 struct dentry *upperdir)
221{
222 /* Workdir should not be the same as upperdir */
223 if (workdir == upperdir)
224 goto err;
225
226 /* Workdir should not be subdir of upperdir and vice versa */
227 if (lock_rename(workdir, upperdir) != NULL)
228 goto err_unlock;
229
230 return 0;
231
232err_unlock:
233 unlock_rename(workdir, upperdir);
234err:
235 pr_err("overlayfs: failed to lock workdir+upperdir\n");
236 return -EIO;
237}
238
239static struct dentry *ovl_clear_empty(struct dentry *dentry,
240 struct list_head *list)
241{
242 struct dentry *workdir = ovl_workdir(dentry);
243 struct inode *wdir = workdir->d_inode;
244 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
245 struct inode *udir = upperdir->d_inode;
246 struct path upperpath;
247 struct dentry *upper;
248 struct dentry *opaquedir;
249 struct kstat stat;
250 int err;
251
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200252 if (WARN_ON(!workdir))
253 return ERR_PTR(-EROFS);
254
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200255 err = ovl_lock_rename_workdir(workdir, upperdir);
256 if (err)
257 goto out;
258
259 ovl_path_upper(dentry, &upperpath);
David Howellsa528d352017-01-31 16:46:22 +0000260 err = vfs_getattr(&upperpath, &stat,
261 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200262 if (err)
263 goto out_unlock;
264
265 err = -ESTALE;
266 if (!S_ISDIR(stat.mode))
267 goto out_unlock;
268 upper = upperpath.dentry;
269 if (upper->d_parent->d_inode != udir)
270 goto out_unlock;
271
Miklos Szeredi3d275732017-05-19 09:33:49 +0200272 opaquedir = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273 err = PTR_ERR(opaquedir);
274 if (IS_ERR(opaquedir))
275 goto out_unlock;
276
Al Viro32a3d842016-12-04 17:33:17 +0000277 err = ovl_create_real(wdir, opaquedir,
278 &(struct cattr){.mode = stat.mode}, NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279 if (err)
280 goto out_dput;
281
282 err = ovl_copy_xattr(upper, opaquedir);
283 if (err)
284 goto out_cleanup;
285
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100286 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200287 if (err)
288 goto out_cleanup;
289
Al Viro59551022016-01-22 15:40:57 -0500290 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200291 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500292 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200293 if (err)
294 goto out_cleanup;
295
296 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
297 if (err)
298 goto out_cleanup;
299
300 ovl_cleanup_whiteouts(upper, list);
301 ovl_cleanup(wdir, upper);
302 unlock_rename(workdir, upperdir);
303
304 /* dentry's upper doesn't match now, get rid of it */
305 d_drop(dentry);
306
307 return opaquedir;
308
309out_cleanup:
310 ovl_cleanup(wdir, opaquedir);
311out_dput:
312 dput(opaquedir);
313out_unlock:
314 unlock_rename(workdir, upperdir);
315out:
316 return ERR_PTR(err);
317}
318
Miklos Szeredia105d682014-11-20 16:39:59 +0100319static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320{
321 int err;
322 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200323 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200324 LIST_HEAD(list);
325
326 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200327 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200329 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100330 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200332 /*
333 * When removing an empty opaque directory, then it makes no sense to
334 * replace it with an exact replica of itself.
335 *
336 * If no upperdentry then skip clearing whiteouts.
337 *
338 * Can race with copy-up, since we don't hold the upperdir mutex.
339 * Doesn't matter, since copy-up can't create a non-empty directory
340 * from an empty one.
341 */
342 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
343 ret = ovl_clear_empty(dentry, &list);
344
345out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346 ovl_cache_free(&list);
347
348 return ret;
349}
350
Miklos Szeredi38b25692016-09-01 11:11:59 +0200351static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
352 const struct posix_acl *acl)
353{
354 void *buffer;
355 size_t size;
356 int err;
357
358 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
359 return 0;
360
361 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
362 buffer = kmalloc(size, GFP_KERNEL);
363 if (!buffer)
364 return -ENOMEM;
365
366 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
367 err = size;
368 if (err < 0)
369 goto out_free;
370
371 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
372out_free:
373 kfree(buffer);
374 return err;
375}
376
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000378 struct cattr *cattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200379 struct dentry *hardlink)
380{
381 struct dentry *workdir = ovl_workdir(dentry);
382 struct inode *wdir = workdir->d_inode;
383 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
384 struct inode *udir = upperdir->d_inode;
385 struct dentry *upper;
386 struct dentry *newdentry;
387 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200388 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200390 if (WARN_ON(!workdir))
391 return -EROFS;
392
Miklos Szeredi38b25692016-09-01 11:11:59 +0200393 if (!hardlink) {
394 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000395 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200396 if (err)
397 return err;
398 }
399
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200400 err = ovl_lock_rename_workdir(workdir, upperdir);
401 if (err)
402 goto out;
403
Miklos Szeredi3d275732017-05-19 09:33:49 +0200404 newdentry = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405 err = PTR_ERR(newdentry);
406 if (IS_ERR(newdentry))
407 goto out_unlock;
408
409 upper = lookup_one_len(dentry->d_name.name, upperdir,
410 dentry->d_name.len);
411 err = PTR_ERR(upper);
412 if (IS_ERR(upper))
413 goto out_dput;
414
Al Viro32a3d842016-12-04 17:33:17 +0000415 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200416 if (err)
417 goto out_dput2;
418
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200419 /*
420 * mode could have been mutilated due to umask (e.g. sgid directory)
421 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200422 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000423 !S_ISLNK(cattr->mode) &&
424 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200425 struct iattr attr = {
426 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000427 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200428 };
429 inode_lock(newdentry->d_inode);
430 err = notify_change(newdentry, &attr, NULL);
431 inode_unlock(newdentry->d_inode);
432 if (err)
433 goto out_cleanup;
434 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200435 if (!hardlink) {
436 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
437 acl);
438 if (err)
439 goto out_cleanup;
440
441 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
442 default_acl);
443 if (err)
444 goto out_cleanup;
445 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200446
Al Viro32a3d842016-12-04 17:33:17 +0000447 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100448 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200449 if (err)
450 goto out_cleanup;
451
452 err = ovl_do_rename(wdir, newdentry, udir, upper,
453 RENAME_EXCHANGE);
454 if (err)
455 goto out_cleanup;
456
457 ovl_cleanup(wdir, upper);
458 } else {
459 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
460 if (err)
461 goto out_cleanup;
462 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200463 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200464 newdentry = NULL;
465out_dput2:
466 dput(upper);
467out_dput:
468 dput(newdentry);
469out_unlock:
470 unlock_rename(workdir, upperdir);
471out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200472 if (!hardlink) {
473 posix_acl_release(acl);
474 posix_acl_release(default_acl);
475 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476 return err;
477
478out_cleanup:
479 ovl_cleanup(wdir, newdentry);
480 goto out_dput2;
481}
482
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200483static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000484 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200485{
486 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400487 const struct cred *old_cred;
488 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200489
490 err = ovl_copy_up(dentry->d_parent);
491 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200492 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200493
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400494 old_cred = ovl_override_creds(dentry->d_sb);
495 err = -ENOMEM;
496 override_cred = prepare_creds();
497 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200498 override_cred->fsuid = inode->i_uid;
499 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400500 if (!hardlink) {
501 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000502 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400503 override_cred);
504 if (err) {
505 put_cred(override_cred);
506 goto out_revert_creds;
507 }
508 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400509 put_cred(override_creds(override_cred));
510 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200511
Miklos Szeredic412ce42016-12-16 11:02:55 +0100512 if (!ovl_dentry_is_whiteout(dentry))
Al Viro32a3d842016-12-04 17:33:17 +0000513 err = ovl_create_upper(dentry, inode, attr,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400514 hardlink);
515 else
Al Viro32a3d842016-12-04 17:33:17 +0000516 err = ovl_create_over_whiteout(dentry, inode, attr,
517 hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200518 }
Vivek Goyal26026252016-07-13 10:44:52 -0400519out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400520 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200521 if (!err) {
522 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
523
524 WARN_ON(inode->i_mode != realinode->i_mode);
525 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
526 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200527 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200528 return err;
529}
530
531static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
532 const char *link)
533{
534 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200535 struct inode *inode;
Al Viro32a3d842016-12-04 17:33:17 +0000536 struct cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200537 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000538 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200539 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200540
541 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200542 if (err)
543 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200545 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100546 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200547 if (!inode)
548 goto out_drop_write;
549
550 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000551 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200552
Al Viro32a3d842016-12-04 17:33:17 +0000553 err = ovl_create_or_link(dentry, inode, &attr, NULL);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200554 if (err)
555 iput(inode);
556
557out_drop_write:
558 ovl_drop_write(dentry);
559out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200560 return err;
561}
562
563static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
564 bool excl)
565{
566 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
567}
568
569static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
570{
571 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
572}
573
574static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
575 dev_t rdev)
576{
577 /* Don't allow creation of "whiteout" on overlay */
578 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
579 return -EPERM;
580
581 return ovl_create_object(dentry, mode, rdev, NULL);
582}
583
584static int ovl_symlink(struct inode *dir, struct dentry *dentry,
585 const char *link)
586{
587 return ovl_create_object(dentry, S_IFLNK, 0, link);
588}
589
590static int ovl_link(struct dentry *old, struct inode *newdir,
591 struct dentry *new)
592{
593 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200594 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200595
596 err = ovl_want_write(old);
597 if (err)
598 goto out;
599
600 err = ovl_copy_up(old);
601 if (err)
602 goto out_drop_write;
603
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200604 inode = d_inode(old);
605 ihold(inode);
606
Al Viro32a3d842016-12-04 17:33:17 +0000607 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200608 if (err)
609 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200610
611out_drop_write:
612 ovl_drop_write(old);
613out:
614 return err;
615}
616
Miklos Szeredi9020df32017-07-04 22:03:16 +0200617static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
618{
619 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
620}
621
Miklos Szeredia105d682014-11-20 16:39:59 +0100622static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200623{
624 struct dentry *workdir = ovl_workdir(dentry);
625 struct inode *wdir = workdir->d_inode;
626 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
627 struct inode *udir = upperdir->d_inode;
628 struct dentry *whiteout;
629 struct dentry *upper;
630 struct dentry *opaquedir = NULL;
631 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700632 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200633
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200634 if (WARN_ON(!workdir))
635 return -EROFS;
636
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200637 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200638 opaquedir = ovl_check_empty_and_clear(dentry);
639 err = PTR_ERR(opaquedir);
640 if (IS_ERR(opaquedir))
641 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200642 }
643
644 err = ovl_lock_rename_workdir(workdir, upperdir);
645 if (err)
646 goto out_dput;
647
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700648 upper = lookup_one_len(dentry->d_name.name, upperdir,
649 dentry->d_name.len);
650 err = PTR_ERR(upper);
651 if (IS_ERR(upper))
652 goto out_unlock;
653
654 err = -ESTALE;
655 if ((opaquedir && upper != opaquedir) ||
656 (!opaquedir && ovl_dentry_upper(dentry) &&
Miklos Szeredi9020df32017-07-04 22:03:16 +0200657 !ovl_matches_upper(dentry, upper))) {
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700658 goto out_dput_upper;
659 }
660
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200661 whiteout = ovl_whiteout(workdir, dentry);
662 err = PTR_ERR(whiteout);
663 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700664 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200665
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700666 if (d_is_dir(upper))
667 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200668
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700669 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
670 if (err)
671 goto kill_whiteout;
672 if (flags)
673 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200674
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200675 ovl_dentry_version_inc(dentry->d_parent);
676out_d_drop:
677 d_drop(dentry);
678 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700679out_dput_upper:
680 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200681out_unlock:
682 unlock_rename(workdir, upperdir);
683out_dput:
684 dput(opaquedir);
685out:
686 return err;
687
688kill_whiteout:
689 ovl_cleanup(wdir, whiteout);
690 goto out_d_drop;
691}
692
693static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
694{
695 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
696 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100697 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300698 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200699 int err;
700
Amir Goldsteind1595112016-10-26 12:34:06 +0300701 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
702 if (is_dir && ovl_dentry_get_redirect(dentry)) {
703 opaquedir = ovl_check_empty_and_clear(dentry);
704 err = PTR_ERR(opaquedir);
705 if (IS_ERR(opaquedir))
706 goto out;
707 }
708
Al Viro59551022016-01-22 15:40:57 -0500709 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100710 upper = lookup_one_len(dentry->d_name.name, upperdir,
711 dentry->d_name.len);
712 err = PTR_ERR(upper);
713 if (IS_ERR(upper))
714 goto out_unlock;
715
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200716 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300717 if ((opaquedir && upper != opaquedir) ||
Miklos Szeredi9020df32017-07-04 22:03:16 +0200718 (!opaquedir && !ovl_matches_upper(dentry, upper)))
Amir Goldsteind1595112016-10-26 12:34:06 +0300719 goto out_dput_upper;
720
721 if (is_dir)
722 err = vfs_rmdir(dir, upper);
723 else
724 err = vfs_unlink(dir, upper, NULL);
725 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200726
727 /*
728 * Keeping this dentry hashed would mean having to release
729 * upperpath/lowerpath, which could only be done if we are the
730 * sole user of this dentry. Too tricky... Just unhash for
731 * now.
732 */
Rui Wangce9113b2016-01-08 23:09:59 +0800733 if (!err)
734 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300735out_dput_upper:
736 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100737out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500738 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300739 dput(opaquedir);
740out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200741 return err;
742}
743
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200744static int ovl_do_remove(struct dentry *dentry, bool is_dir)
745{
746 enum ovl_path_type type;
747 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400748 const struct cred *old_cred;
749
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200750 err = ovl_want_write(dentry);
751 if (err)
752 goto out;
753
754 err = ovl_copy_up(dentry->d_parent);
755 if (err)
756 goto out_drop_write;
757
758 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400759
760 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100761 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200762 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400763 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100764 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400765 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200766 if (!err) {
767 if (is_dir)
768 clear_nlink(dentry->d_inode);
769 else
770 drop_nlink(dentry->d_inode);
771 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200772out_drop_write:
773 ovl_drop_write(dentry);
774out:
775 return err;
776}
777
778static int ovl_unlink(struct inode *dir, struct dentry *dentry)
779{
780 return ovl_do_remove(dentry, false);
781}
782
783static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
784{
785 return ovl_do_remove(dentry, true);
786}
787
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100788static bool ovl_type_merge_or_lower(struct dentry *dentry)
789{
790 enum ovl_path_type type = ovl_path_type(dentry);
791
792 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
793}
794
Miklos Szeredia6c60652016-12-16 11:02:56 +0100795static bool ovl_can_move(struct dentry *dentry)
796{
797 return ovl_redirect_dir(dentry->d_sb) ||
798 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
799}
800
Miklos Szeredia6c60652016-12-16 11:02:56 +0100801static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
802{
803 char *buf, *ret;
804 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100805 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100806
807 if (samedir) {
808 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
809 GFP_KERNEL);
810 goto out;
811 }
812
813 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
814 if (!buf)
815 goto out;
816
817 buflen--;
818 buf[buflen] = '\0';
819 for (d = dget(dentry); !IS_ROOT(d);) {
820 const char *name;
821 int thislen;
822
823 spin_lock(&d->d_lock);
824 name = ovl_dentry_get_redirect(d);
825 if (name) {
826 thislen = strlen(name);
827 } else {
828 name = d->d_name.name;
829 thislen = d->d_name.len;
830 }
831
832 /* If path is too long, fall back to userspace move */
833 if (thislen + (name[0] != '/') > buflen) {
834 ret = ERR_PTR(-EXDEV);
835 spin_unlock(&d->d_lock);
836 goto out_put;
837 }
838
839 buflen -= thislen;
840 memcpy(&buf[buflen], name, thislen);
841 tmp = dget_dlock(d->d_parent);
842 spin_unlock(&d->d_lock);
843
844 dput(d);
845 d = tmp;
846
847 /* Absolute redirect: finished */
848 if (buf[buflen] == '/')
849 break;
850 buflen--;
851 buf[buflen] = '/';
852 }
853 ret = kstrdup(&buf[buflen], GFP_KERNEL);
854out_put:
855 dput(d);
856 kfree(buf);
857out:
858 return ret ? ret : ERR_PTR(-ENOMEM);
859}
860
861static int ovl_set_redirect(struct dentry *dentry, bool samedir)
862{
863 int err;
864 const char *redirect = ovl_dentry_get_redirect(dentry);
865
866 if (redirect && (samedir || redirect[0] == '/'))
867 return 0;
868
869 redirect = ovl_get_redirect(dentry, samedir);
870 if (IS_ERR(redirect))
871 return PTR_ERR(redirect);
872
Amir Goldstein21a22872017-05-17 00:12:41 +0300873 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
874 OVL_XATTR_REDIRECT,
875 redirect, strlen(redirect), -EXDEV);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100876 if (!err) {
877 spin_lock(&dentry->d_lock);
878 ovl_dentry_set_redirect(dentry, redirect);
879 spin_unlock(&dentry->d_lock);
880 } else {
881 kfree(redirect);
Amir Goldstein21a22872017-05-17 00:12:41 +0300882 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100883 /* Fall back to userspace copy-up */
884 err = -EXDEV;
885 }
886 return err;
887}
888
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100889static int ovl_rename(struct inode *olddir, struct dentry *old,
890 struct inode *newdir, struct dentry *new,
891 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200892{
893 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200894 struct dentry *old_upperdir;
895 struct dentry *new_upperdir;
896 struct dentry *olddentry;
897 struct dentry *newdentry;
898 struct dentry *trap;
899 bool old_opaque;
900 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200901 bool cleanup_whiteout = false;
902 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000903 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100904 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100905 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200906 struct dentry *opaquedir = NULL;
907 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200908
909 err = -EINVAL;
910 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
911 goto out;
912
913 flags &= ~RENAME_NOREPLACE;
914
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200915 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200916 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100917 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200918 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100919 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100920 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200921
922 err = ovl_want_write(old);
923 if (err)
924 goto out;
925
926 err = ovl_copy_up(old);
927 if (err)
928 goto out_drop_write;
929
930 err = ovl_copy_up(new->d_parent);
931 if (err)
932 goto out_drop_write;
933 if (!overwrite) {
934 err = ovl_copy_up(new);
935 if (err)
936 goto out_drop_write;
937 }
938
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400939 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200940
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100941 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100942 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200943 err = PTR_ERR(opaquedir);
944 if (IS_ERR(opaquedir)) {
945 opaquedir = NULL;
946 goto out_revert_creds;
947 }
948 }
949
950 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100951 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100952 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200953 /* Whiteout source */
954 flags |= RENAME_WHITEOUT;
955 } else {
956 /* Switch whiteouts */
957 flags |= RENAME_EXCHANGE;
958 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100959 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200960 flags |= RENAME_EXCHANGE;
961 cleanup_whiteout = true;
962 }
963 }
964
965 old_upperdir = ovl_dentry_upper(old->d_parent);
966 new_upperdir = ovl_dentry_upper(new->d_parent);
967
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300968 if (!samedir) {
969 /*
970 * When moving a merge dir or non-dir with copy up origin into
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300971 * a new parent, we are marking the new parent dir "impure".
972 * When ovl_iterate() iterates an "impure" upper dir, it will
973 * lookup the origin inodes of the entries to fill d_ino.
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300974 */
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300975 if (ovl_type_origin(old)) {
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300976 err = ovl_set_impure(new->d_parent, new_upperdir);
977 if (err)
978 goto out_revert_creds;
979 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300980 if (!overwrite && ovl_type_origin(new)) {
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300981 err = ovl_set_impure(old->d_parent, old_upperdir);
982 if (err)
983 goto out_revert_creds;
984 }
985 }
986
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200987 trap = lock_rename(new_upperdir, old_upperdir);
988
Miklos Szeredi11f37102016-03-21 17:31:44 +0100989 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
990 old->d_name.len);
991 err = PTR_ERR(olddentry);
992 if (IS_ERR(olddentry))
993 goto out_unlock;
994
995 err = -ESTALE;
Miklos Szeredi9020df32017-07-04 22:03:16 +0200996 if (!ovl_matches_upper(old, olddentry))
Miklos Szeredi11f37102016-03-21 17:31:44 +0100997 goto out_dput_old;
998
999 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1000 new->d_name.len);
1001 err = PTR_ERR(newdentry);
1002 if (IS_ERR(newdentry))
1003 goto out_dput_old;
1004
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001005 old_opaque = ovl_dentry_is_opaque(old);
1006 new_opaque = ovl_dentry_is_opaque(new);
1007
Miklos Szeredi11f37102016-03-21 17:31:44 +01001008 err = -ESTALE;
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001009 if (d_inode(new) && ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001010 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001011 if (newdentry != opaquedir)
1012 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001013 } else {
Miklos Szeredi9020df32017-07-04 22:03:16 +02001014 if (!ovl_matches_upper(new, newdentry))
Miklos Szeredi11f37102016-03-21 17:31:44 +01001015 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001016 }
1017 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001018 if (!d_is_negative(newdentry) &&
1019 (!new_opaque || !ovl_is_whiteout(newdentry)))
1020 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001021 }
1022
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023 if (olddentry == trap)
1024 goto out_dput;
1025 if (newdentry == trap)
1026 goto out_dput;
1027
Miklos Szeredi804032f2016-12-16 11:02:55 +01001028 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1029 goto out_dput;
1030
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001031 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001032 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001033 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001034 err = ovl_set_redirect(old, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001035 else if (!old_opaque && ovl_type_merge(new->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001036 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001037 if (err)
1038 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001039 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001040 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001041 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001042 err = ovl_set_redirect(new, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001043 else if (!new_opaque && ovl_type_merge(old->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001044 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001045 if (err)
1046 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001047 }
1048
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001049 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1050 new_upperdir->d_inode, newdentry, flags);
1051 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001052 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001053
1054 if (cleanup_whiteout)
1055 ovl_cleanup(old_upperdir->d_inode, newdentry);
1056
Amir Goldsteinf681eb12017-06-05 22:44:49 +03001057 if (overwrite && d_inode(new)) {
1058 if (new_is_dir)
1059 clear_nlink(d_inode(new));
1060 else
1061 drop_nlink(d_inode(new));
1062 }
1063
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001064 ovl_dentry_version_inc(old->d_parent);
1065 ovl_dentry_version_inc(new->d_parent);
1066
1067out_dput:
1068 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001069out_dput_old:
1070 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001071out_unlock:
1072 unlock_rename(new_upperdir, old_upperdir);
1073out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001074 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001075out_drop_write:
1076 ovl_drop_write(old);
1077out:
1078 dput(opaquedir);
1079 return err;
1080}
1081
1082const struct inode_operations ovl_dir_inode_operations = {
1083 .lookup = ovl_lookup,
1084 .mkdir = ovl_mkdir,
1085 .symlink = ovl_symlink,
1086 .unlink = ovl_unlink,
1087 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001088 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001089 .link = ovl_link,
1090 .setattr = ovl_setattr,
1091 .create = ovl_create,
1092 .mknod = ovl_mknod,
1093 .permission = ovl_permission,
Miklos Szeredi5b712092017-05-05 11:38:58 +02001094 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001095 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001096 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001097 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001098};