blob: f2a118ba00e4b890e83ac36b5bb7c0aebba042ef [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
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
28{
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 }
42}
43
Miklos Szeredi3d275732017-05-19 09:33:49 +020044struct dentry *ovl_lookup_temp(struct dentry *workdir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020045{
46 struct dentry *temp;
47 char name[20];
Richard Weinberger6a45b362016-09-16 11:45:24 +020048 static atomic_t temp_id = ATOMIC_INIT(0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049
Richard Weinberger6a45b362016-09-16 11:45:24 +020050 /* counter is allowed to wrap, since temp dentries are ephemeral */
51 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
Miklos Szeredie9be9d52014-10-24 00:14:38 +020052
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 */
64static 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
Miklos Szeredi3d275732017-05-19 09:33:49 +020071 whiteout = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020072 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
84int ovl_create_real(struct inode *dir, struct dentry *newdentry,
Al Viro32a3d842016-12-04 17:33:17 +000085 struct cattr *attr, struct dentry *hardlink, bool debug)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020086{
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 Viro32a3d842016-12-04 17:33:17 +000095 switch (attr->mode & S_IFMT) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020096 case S_IFREG:
Al Viro32a3d842016-12-04 17:33:17 +000097 err = ovl_do_create(dir, newdentry, attr->mode, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020098 break;
99
100 case S_IFDIR:
Al Viro32a3d842016-12-04 17:33:17 +0000101 err = ovl_do_mkdir(dir, newdentry, attr->mode, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200102 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 Viro32a3d842016-12-04 17:33:17 +0000109 attr->mode, attr->rdev, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110 break;
111
112 case S_IFLNK:
Al Viro32a3d842016-12-04 17:33:17 +0000113 err = ovl_do_symlink(dir, newdentry, attr->link, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200114 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
Amir Goldstein21a22872017-05-17 00:12:41 +0300130static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
131 int xerr)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132{
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100133 int err;
134
Amir Goldstein21a22872017-05-17 00:12:41 +0300135 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100136 if (!err)
137 ovl_dentry_set_opaque(dentry);
138
139 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140}
141
Amir Goldstein21a22872017-05-17 00:12:41 +0300142static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
143{
144 /*
145 * Fail with -EIO when trying to create opaque dir and upper doesn't
146 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
147 * return a specific error for noxattr case.
148 */
149 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
150}
151
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300152static int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
153{
154 int err;
155
156 /*
157 * Do not fail when upper doesn't support xattrs.
158 * Upper inodes won't have origin nor redirect xattr anyway.
159 */
160 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
161 "y", 1, 0);
162 if (!err)
163 ovl_dentry_set_impure(dentry);
164
165 return err;
166}
167
Vivek Goyal72e48482016-06-16 10:09:14 -0400168/* Common operations required to be done after creation of file on upper */
169static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200170 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400171{
172 ovl_dentry_version_inc(dentry->d_parent);
173 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200174 if (!hardlink) {
175 ovl_inode_update(inode, d_inode(newdentry));
176 ovl_copyattr(newdentry->d_inode, inode);
177 } else {
178 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
179 inc_nlink(inode);
180 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400181 d_instantiate(dentry, inode);
Amir Goldstein5b6c9052017-04-24 19:54:13 +0300182 /* Force lookup of new upper hardlink to find its lower */
183 if (hardlink)
184 d_drop(dentry);
Vivek Goyal72e48482016-06-16 10:09:14 -0400185}
186
Amir Goldstein97c684c2016-11-21 18:57:34 +0200187static bool ovl_type_merge(struct dentry *dentry)
188{
189 return OVL_TYPE_MERGE(ovl_path_type(dentry));
190}
191
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300192static bool ovl_type_origin(struct dentry *dentry)
193{
194 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
195}
196
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000198 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199{
200 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
201 struct inode *udir = upperdir->d_inode;
202 struct dentry *newdentry;
203 int err;
204
Miklos Szeredi38b25692016-09-01 11:11:59 +0200205 if (!hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000206 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200207
Al Viro59551022016-01-22 15:40:57 -0500208 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
210 dentry->d_name.len);
211 err = PTR_ERR(newdentry);
212 if (IS_ERR(newdentry))
213 goto out_unlock;
Al Viro32a3d842016-12-04 17:33:17 +0000214 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215 if (err)
216 goto out_dput;
217
Amir Goldstein4a99f3c2017-04-24 22:26:40 +0300218 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
Amir Goldstein97c684c2016-11-21 18:57:34 +0200219 /* Setting opaque here is just an optimization, allow to fail */
220 ovl_set_opaque(dentry, newdentry);
221 }
222
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200223 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200224 newdentry = NULL;
225out_dput:
226 dput(newdentry);
227out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500228 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200229 return err;
230}
231
232static int ovl_lock_rename_workdir(struct dentry *workdir,
233 struct dentry *upperdir)
234{
235 /* Workdir should not be the same as upperdir */
236 if (workdir == upperdir)
237 goto err;
238
239 /* Workdir should not be subdir of upperdir and vice versa */
240 if (lock_rename(workdir, upperdir) != NULL)
241 goto err_unlock;
242
243 return 0;
244
245err_unlock:
246 unlock_rename(workdir, upperdir);
247err:
248 pr_err("overlayfs: failed to lock workdir+upperdir\n");
249 return -EIO;
250}
251
252static struct dentry *ovl_clear_empty(struct dentry *dentry,
253 struct list_head *list)
254{
255 struct dentry *workdir = ovl_workdir(dentry);
256 struct inode *wdir = workdir->d_inode;
257 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
258 struct inode *udir = upperdir->d_inode;
259 struct path upperpath;
260 struct dentry *upper;
261 struct dentry *opaquedir;
262 struct kstat stat;
263 int err;
264
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200265 if (WARN_ON(!workdir))
266 return ERR_PTR(-EROFS);
267
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268 err = ovl_lock_rename_workdir(workdir, upperdir);
269 if (err)
270 goto out;
271
272 ovl_path_upper(dentry, &upperpath);
David Howellsa528d352017-01-31 16:46:22 +0000273 err = vfs_getattr(&upperpath, &stat,
274 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200275 if (err)
276 goto out_unlock;
277
278 err = -ESTALE;
279 if (!S_ISDIR(stat.mode))
280 goto out_unlock;
281 upper = upperpath.dentry;
282 if (upper->d_parent->d_inode != udir)
283 goto out_unlock;
284
Miklos Szeredi3d275732017-05-19 09:33:49 +0200285 opaquedir = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200286 err = PTR_ERR(opaquedir);
287 if (IS_ERR(opaquedir))
288 goto out_unlock;
289
Al Viro32a3d842016-12-04 17:33:17 +0000290 err = ovl_create_real(wdir, opaquedir,
291 &(struct cattr){.mode = stat.mode}, NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292 if (err)
293 goto out_dput;
294
295 err = ovl_copy_xattr(upper, opaquedir);
296 if (err)
297 goto out_cleanup;
298
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100299 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300 if (err)
301 goto out_cleanup;
302
Al Viro59551022016-01-22 15:40:57 -0500303 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200304 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500305 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200306 if (err)
307 goto out_cleanup;
308
309 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
310 if (err)
311 goto out_cleanup;
312
313 ovl_cleanup_whiteouts(upper, list);
314 ovl_cleanup(wdir, upper);
315 unlock_rename(workdir, upperdir);
316
317 /* dentry's upper doesn't match now, get rid of it */
318 d_drop(dentry);
319
320 return opaquedir;
321
322out_cleanup:
323 ovl_cleanup(wdir, opaquedir);
324out_dput:
325 dput(opaquedir);
326out_unlock:
327 unlock_rename(workdir, upperdir);
328out:
329 return ERR_PTR(err);
330}
331
Miklos Szeredia105d682014-11-20 16:39:59 +0100332static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200333{
334 int err;
335 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200336 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337 LIST_HEAD(list);
338
339 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200340 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200341 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200342 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100343 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200344
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200345 /*
346 * When removing an empty opaque directory, then it makes no sense to
347 * replace it with an exact replica of itself.
348 *
349 * If no upperdentry then skip clearing whiteouts.
350 *
351 * Can race with copy-up, since we don't hold the upperdir mutex.
352 * Doesn't matter, since copy-up can't create a non-empty directory
353 * from an empty one.
354 */
355 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
356 ret = ovl_clear_empty(dentry, &list);
357
358out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200359 ovl_cache_free(&list);
360
361 return ret;
362}
363
Miklos Szeredi38b25692016-09-01 11:11:59 +0200364static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
365 const struct posix_acl *acl)
366{
367 void *buffer;
368 size_t size;
369 int err;
370
371 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
372 return 0;
373
374 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
375 buffer = kmalloc(size, GFP_KERNEL);
376 if (!buffer)
377 return -ENOMEM;
378
379 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
380 err = size;
381 if (err < 0)
382 goto out_free;
383
384 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
385out_free:
386 kfree(buffer);
387 return err;
388}
389
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200390static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000391 struct cattr *cattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392 struct dentry *hardlink)
393{
394 struct dentry *workdir = ovl_workdir(dentry);
395 struct inode *wdir = workdir->d_inode;
396 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
397 struct inode *udir = upperdir->d_inode;
398 struct dentry *upper;
399 struct dentry *newdentry;
400 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200401 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200402
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200403 if (WARN_ON(!workdir))
404 return -EROFS;
405
Miklos Szeredi38b25692016-09-01 11:11:59 +0200406 if (!hardlink) {
407 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000408 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200409 if (err)
410 return err;
411 }
412
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413 err = ovl_lock_rename_workdir(workdir, upperdir);
414 if (err)
415 goto out;
416
Miklos Szeredi3d275732017-05-19 09:33:49 +0200417 newdentry = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200418 err = PTR_ERR(newdentry);
419 if (IS_ERR(newdentry))
420 goto out_unlock;
421
422 upper = lookup_one_len(dentry->d_name.name, upperdir,
423 dentry->d_name.len);
424 err = PTR_ERR(upper);
425 if (IS_ERR(upper))
426 goto out_dput;
427
Al Viro32a3d842016-12-04 17:33:17 +0000428 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200429 if (err)
430 goto out_dput2;
431
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200432 /*
433 * mode could have been mutilated due to umask (e.g. sgid directory)
434 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200435 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000436 !S_ISLNK(cattr->mode) &&
437 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200438 struct iattr attr = {
439 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000440 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200441 };
442 inode_lock(newdentry->d_inode);
443 err = notify_change(newdentry, &attr, NULL);
444 inode_unlock(newdentry->d_inode);
445 if (err)
446 goto out_cleanup;
447 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200448 if (!hardlink) {
449 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
450 acl);
451 if (err)
452 goto out_cleanup;
453
454 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
455 default_acl);
456 if (err)
457 goto out_cleanup;
458 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200459
Al Viro32a3d842016-12-04 17:33:17 +0000460 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100461 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200462 if (err)
463 goto out_cleanup;
464
465 err = ovl_do_rename(wdir, newdentry, udir, upper,
466 RENAME_EXCHANGE);
467 if (err)
468 goto out_cleanup;
469
470 ovl_cleanup(wdir, upper);
471 } else {
472 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
473 if (err)
474 goto out_cleanup;
475 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200476 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200477 newdentry = NULL;
478out_dput2:
479 dput(upper);
480out_dput:
481 dput(newdentry);
482out_unlock:
483 unlock_rename(workdir, upperdir);
484out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200485 if (!hardlink) {
486 posix_acl_release(acl);
487 posix_acl_release(default_acl);
488 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200489 return err;
490
491out_cleanup:
492 ovl_cleanup(wdir, newdentry);
493 goto out_dput2;
494}
495
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200496static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000497 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200498{
499 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400500 const struct cred *old_cred;
501 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200502
503 err = ovl_copy_up(dentry->d_parent);
504 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200505 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200506
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400507 old_cred = ovl_override_creds(dentry->d_sb);
508 err = -ENOMEM;
509 override_cred = prepare_creds();
510 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200511 override_cred->fsuid = inode->i_uid;
512 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400513 if (!hardlink) {
514 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000515 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400516 override_cred);
517 if (err) {
518 put_cred(override_cred);
519 goto out_revert_creds;
520 }
521 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400522 put_cred(override_creds(override_cred));
523 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200524
Miklos Szeredic412ce42016-12-16 11:02:55 +0100525 if (!ovl_dentry_is_whiteout(dentry))
Al Viro32a3d842016-12-04 17:33:17 +0000526 err = ovl_create_upper(dentry, inode, attr,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400527 hardlink);
528 else
Al Viro32a3d842016-12-04 17:33:17 +0000529 err = ovl_create_over_whiteout(dentry, inode, attr,
530 hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200531 }
Vivek Goyal26026252016-07-13 10:44:52 -0400532out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400533 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200534 if (!err) {
535 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
536
537 WARN_ON(inode->i_mode != realinode->i_mode);
538 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
539 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200540 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200541 return err;
542}
543
544static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
545 const char *link)
546{
547 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200548 struct inode *inode;
Al Viro32a3d842016-12-04 17:33:17 +0000549 struct cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200550 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000551 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200552 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200553
554 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200555 if (err)
556 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200557
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200558 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100559 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200560 if (!inode)
561 goto out_drop_write;
562
563 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000564 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200565
Al Viro32a3d842016-12-04 17:33:17 +0000566 err = ovl_create_or_link(dentry, inode, &attr, NULL);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200567 if (err)
568 iput(inode);
569
570out_drop_write:
571 ovl_drop_write(dentry);
572out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200573 return err;
574}
575
576static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
577 bool excl)
578{
579 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
580}
581
582static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
583{
584 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
585}
586
587static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
588 dev_t rdev)
589{
590 /* Don't allow creation of "whiteout" on overlay */
591 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
592 return -EPERM;
593
594 return ovl_create_object(dentry, mode, rdev, NULL);
595}
596
597static int ovl_symlink(struct inode *dir, struct dentry *dentry,
598 const char *link)
599{
600 return ovl_create_object(dentry, S_IFLNK, 0, link);
601}
602
603static int ovl_link(struct dentry *old, struct inode *newdir,
604 struct dentry *new)
605{
606 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200607 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200608
609 err = ovl_want_write(old);
610 if (err)
611 goto out;
612
613 err = ovl_copy_up(old);
614 if (err)
615 goto out_drop_write;
616
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200617 inode = d_inode(old);
618 ihold(inode);
619
Al Viro32a3d842016-12-04 17:33:17 +0000620 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200621 if (err)
622 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200623
624out_drop_write:
625 ovl_drop_write(old);
626out:
627 return err;
628}
629
Miklos Szeredia105d682014-11-20 16:39:59 +0100630static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200631{
632 struct dentry *workdir = ovl_workdir(dentry);
633 struct inode *wdir = workdir->d_inode;
634 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
635 struct inode *udir = upperdir->d_inode;
636 struct dentry *whiteout;
637 struct dentry *upper;
638 struct dentry *opaquedir = NULL;
639 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700640 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200641
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200642 if (WARN_ON(!workdir))
643 return -EROFS;
644
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200645 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200646 opaquedir = ovl_check_empty_and_clear(dentry);
647 err = PTR_ERR(opaquedir);
648 if (IS_ERR(opaquedir))
649 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200650 }
651
652 err = ovl_lock_rename_workdir(workdir, upperdir);
653 if (err)
654 goto out_dput;
655
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700656 upper = lookup_one_len(dentry->d_name.name, upperdir,
657 dentry->d_name.len);
658 err = PTR_ERR(upper);
659 if (IS_ERR(upper))
660 goto out_unlock;
661
662 err = -ESTALE;
663 if ((opaquedir && upper != opaquedir) ||
664 (!opaquedir && ovl_dentry_upper(dentry) &&
665 upper != ovl_dentry_upper(dentry))) {
666 goto out_dput_upper;
667 }
668
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200669 whiteout = ovl_whiteout(workdir, dentry);
670 err = PTR_ERR(whiteout);
671 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700672 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200673
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700674 if (d_is_dir(upper))
675 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200676
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700677 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
678 if (err)
679 goto kill_whiteout;
680 if (flags)
681 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200682
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200683 ovl_dentry_version_inc(dentry->d_parent);
684out_d_drop:
685 d_drop(dentry);
686 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700687out_dput_upper:
688 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200689out_unlock:
690 unlock_rename(workdir, upperdir);
691out_dput:
692 dput(opaquedir);
693out:
694 return err;
695
696kill_whiteout:
697 ovl_cleanup(wdir, whiteout);
698 goto out_d_drop;
699}
700
701static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
702{
703 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
704 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100705 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300706 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200707 int err;
708
Amir Goldsteind1595112016-10-26 12:34:06 +0300709 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
710 if (is_dir && ovl_dentry_get_redirect(dentry)) {
711 opaquedir = ovl_check_empty_and_clear(dentry);
712 err = PTR_ERR(opaquedir);
713 if (IS_ERR(opaquedir))
714 goto out;
715 }
716
Al Viro59551022016-01-22 15:40:57 -0500717 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100718 upper = lookup_one_len(dentry->d_name.name, upperdir,
719 dentry->d_name.len);
720 err = PTR_ERR(upper);
721 if (IS_ERR(upper))
722 goto out_unlock;
723
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200724 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300725 if ((opaquedir && upper != opaquedir) ||
726 (!opaquedir && upper != ovl_dentry_upper(dentry)))
727 goto out_dput_upper;
728
729 if (is_dir)
730 err = vfs_rmdir(dir, upper);
731 else
732 err = vfs_unlink(dir, upper, NULL);
733 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200734
735 /*
736 * Keeping this dentry hashed would mean having to release
737 * upperpath/lowerpath, which could only be done if we are the
738 * sole user of this dentry. Too tricky... Just unhash for
739 * now.
740 */
Rui Wangce9113b2016-01-08 23:09:59 +0800741 if (!err)
742 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300743out_dput_upper:
744 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100745out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500746 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300747 dput(opaquedir);
748out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200749 return err;
750}
751
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200752static int ovl_do_remove(struct dentry *dentry, bool is_dir)
753{
754 enum ovl_path_type type;
755 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400756 const struct cred *old_cred;
757
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200758 err = ovl_want_write(dentry);
759 if (err)
760 goto out;
761
762 err = ovl_copy_up(dentry->d_parent);
763 if (err)
764 goto out_drop_write;
765
766 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400767
768 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100769 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200770 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400771 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100772 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400773 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200774 if (!err) {
775 if (is_dir)
776 clear_nlink(dentry->d_inode);
777 else
778 drop_nlink(dentry->d_inode);
779 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200780out_drop_write:
781 ovl_drop_write(dentry);
782out:
783 return err;
784}
785
786static int ovl_unlink(struct inode *dir, struct dentry *dentry)
787{
788 return ovl_do_remove(dentry, false);
789}
790
791static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
792{
793 return ovl_do_remove(dentry, true);
794}
795
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100796static bool ovl_type_merge_or_lower(struct dentry *dentry)
797{
798 enum ovl_path_type type = ovl_path_type(dentry);
799
800 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
801}
802
Miklos Szeredia6c60652016-12-16 11:02:56 +0100803static bool ovl_can_move(struct dentry *dentry)
804{
805 return ovl_redirect_dir(dentry->d_sb) ||
806 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
807}
808
Miklos Szeredia6c60652016-12-16 11:02:56 +0100809static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
810{
811 char *buf, *ret;
812 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100813 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100814
815 if (samedir) {
816 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
817 GFP_KERNEL);
818 goto out;
819 }
820
821 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
822 if (!buf)
823 goto out;
824
825 buflen--;
826 buf[buflen] = '\0';
827 for (d = dget(dentry); !IS_ROOT(d);) {
828 const char *name;
829 int thislen;
830
831 spin_lock(&d->d_lock);
832 name = ovl_dentry_get_redirect(d);
833 if (name) {
834 thislen = strlen(name);
835 } else {
836 name = d->d_name.name;
837 thislen = d->d_name.len;
838 }
839
840 /* If path is too long, fall back to userspace move */
841 if (thislen + (name[0] != '/') > buflen) {
842 ret = ERR_PTR(-EXDEV);
843 spin_unlock(&d->d_lock);
844 goto out_put;
845 }
846
847 buflen -= thislen;
848 memcpy(&buf[buflen], name, thislen);
849 tmp = dget_dlock(d->d_parent);
850 spin_unlock(&d->d_lock);
851
852 dput(d);
853 d = tmp;
854
855 /* Absolute redirect: finished */
856 if (buf[buflen] == '/')
857 break;
858 buflen--;
859 buf[buflen] = '/';
860 }
861 ret = kstrdup(&buf[buflen], GFP_KERNEL);
862out_put:
863 dput(d);
864 kfree(buf);
865out:
866 return ret ? ret : ERR_PTR(-ENOMEM);
867}
868
869static int ovl_set_redirect(struct dentry *dentry, bool samedir)
870{
871 int err;
872 const char *redirect = ovl_dentry_get_redirect(dentry);
873
874 if (redirect && (samedir || redirect[0] == '/'))
875 return 0;
876
877 redirect = ovl_get_redirect(dentry, samedir);
878 if (IS_ERR(redirect))
879 return PTR_ERR(redirect);
880
Amir Goldstein21a22872017-05-17 00:12:41 +0300881 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
882 OVL_XATTR_REDIRECT,
883 redirect, strlen(redirect), -EXDEV);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100884 if (!err) {
885 spin_lock(&dentry->d_lock);
886 ovl_dentry_set_redirect(dentry, redirect);
887 spin_unlock(&dentry->d_lock);
888 } else {
889 kfree(redirect);
Amir Goldstein21a22872017-05-17 00:12:41 +0300890 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100891 /* Fall back to userspace copy-up */
892 err = -EXDEV;
893 }
894 return err;
895}
896
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100897static int ovl_rename(struct inode *olddir, struct dentry *old,
898 struct inode *newdir, struct dentry *new,
899 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200900{
901 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200902 struct dentry *old_upperdir;
903 struct dentry *new_upperdir;
904 struct dentry *olddentry;
905 struct dentry *newdentry;
906 struct dentry *trap;
907 bool old_opaque;
908 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200909 bool cleanup_whiteout = false;
910 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000911 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100912 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100913 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200914 struct dentry *opaquedir = NULL;
915 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200916
917 err = -EINVAL;
918 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
919 goto out;
920
921 flags &= ~RENAME_NOREPLACE;
922
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200923 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200924 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100925 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200926 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100927 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100928 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200929
930 err = ovl_want_write(old);
931 if (err)
932 goto out;
933
934 err = ovl_copy_up(old);
935 if (err)
936 goto out_drop_write;
937
938 err = ovl_copy_up(new->d_parent);
939 if (err)
940 goto out_drop_write;
941 if (!overwrite) {
942 err = ovl_copy_up(new);
943 if (err)
944 goto out_drop_write;
945 }
946
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400947 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200948
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100949 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100950 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200951 err = PTR_ERR(opaquedir);
952 if (IS_ERR(opaquedir)) {
953 opaquedir = NULL;
954 goto out_revert_creds;
955 }
956 }
957
958 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100959 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100960 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200961 /* Whiteout source */
962 flags |= RENAME_WHITEOUT;
963 } else {
964 /* Switch whiteouts */
965 flags |= RENAME_EXCHANGE;
966 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100967 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200968 flags |= RENAME_EXCHANGE;
969 cleanup_whiteout = true;
970 }
971 }
972
973 old_upperdir = ovl_dentry_upper(old->d_parent);
974 new_upperdir = ovl_dentry_upper(new->d_parent);
975
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300976 if (!samedir) {
977 /*
978 * When moving a merge dir or non-dir with copy up origin into
979 * a non-merge upper dir (a.k.a pure upper dir), we are making
980 * the target parent dir "impure". ovl_iterate() iterates pure
981 * upper dirs directly, because there is no need to filter out
982 * whiteouts and merge dir content with lower dir. But for the
983 * case of an "impure" upper dir, ovl_iterate() cannot iterate
984 * the real directory directly, because it looks for the inode
985 * numbers to fill d_ino in the entries origin inode.
986 */
987 if (ovl_type_origin(old) && !ovl_type_merge(new->d_parent)) {
988 err = ovl_set_impure(new->d_parent, new_upperdir);
989 if (err)
990 goto out_revert_creds;
991 }
992 if (!overwrite && ovl_type_origin(new) &&
993 !ovl_type_merge(old->d_parent)) {
994 err = ovl_set_impure(old->d_parent, old_upperdir);
995 if (err)
996 goto out_revert_creds;
997 }
998 }
999
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001000 trap = lock_rename(new_upperdir, old_upperdir);
1001
Miklos Szeredi11f37102016-03-21 17:31:44 +01001002 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1003 old->d_name.len);
1004 err = PTR_ERR(olddentry);
1005 if (IS_ERR(olddentry))
1006 goto out_unlock;
1007
1008 err = -ESTALE;
1009 if (olddentry != ovl_dentry_upper(old))
1010 goto out_dput_old;
1011
1012 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1013 new->d_name.len);
1014 err = PTR_ERR(newdentry);
1015 if (IS_ERR(newdentry))
1016 goto out_dput_old;
1017
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001018 old_opaque = ovl_dentry_is_opaque(old);
1019 new_opaque = ovl_dentry_is_opaque(new);
1020
Miklos Szeredi11f37102016-03-21 17:31:44 +01001021 err = -ESTALE;
1022 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001024 if (newdentry != opaquedir)
1025 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001026 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001027 if (newdentry != ovl_dentry_upper(new))
1028 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001029 }
1030 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001031 if (!d_is_negative(newdentry) &&
1032 (!new_opaque || !ovl_is_whiteout(newdentry)))
1033 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001034 }
1035
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001036 if (olddentry == trap)
1037 goto out_dput;
1038 if (newdentry == trap)
1039 goto out_dput;
1040
Miklos Szeredi804032f2016-12-16 11:02:55 +01001041 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1042 goto out_dput;
1043
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001044 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001045 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001046 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001047 err = ovl_set_redirect(old, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001048 else if (!old_opaque && ovl_type_merge(new->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001049 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001050 if (err)
1051 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001052 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001053 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001054 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001055 err = ovl_set_redirect(new, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001056 else if (!new_opaque && ovl_type_merge(old->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001057 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001058 if (err)
1059 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001060 }
1061
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001062 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1063 new_upperdir->d_inode, newdentry, flags);
1064 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001065 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001066
1067 if (cleanup_whiteout)
1068 ovl_cleanup(old_upperdir->d_inode, newdentry);
1069
1070 ovl_dentry_version_inc(old->d_parent);
1071 ovl_dentry_version_inc(new->d_parent);
1072
1073out_dput:
1074 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001075out_dput_old:
1076 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001077out_unlock:
1078 unlock_rename(new_upperdir, old_upperdir);
1079out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001080 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001081out_drop_write:
1082 ovl_drop_write(old);
1083out:
1084 dput(opaquedir);
1085 return err;
1086}
1087
1088const struct inode_operations ovl_dir_inode_operations = {
1089 .lookup = ovl_lookup,
1090 .mkdir = ovl_mkdir,
1091 .symlink = ovl_symlink,
1092 .unlink = ovl_unlink,
1093 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001094 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001095 .link = ovl_link,
1096 .setattr = ovl_setattr,
1097 .create = ovl_create,
1098 .mknod = ovl_mknod,
1099 .permission = ovl_permission,
Miklos Szeredi5b712092017-05-05 11:38:58 +02001100 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001101 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001102 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001103 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001104};