blob: a63a71656e9bdaef6ed5cadf8acdb6d8002fe1b6 [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
Vivek Goyal72e48482016-06-16 10:09:14 -0400152/* Common operations required to be done after creation of file on upper */
153static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200154 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400155{
156 ovl_dentry_version_inc(dentry->d_parent);
157 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200158 if (!hardlink) {
159 ovl_inode_update(inode, d_inode(newdentry));
160 ovl_copyattr(newdentry->d_inode, inode);
161 } else {
162 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
163 inc_nlink(inode);
164 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400165 d_instantiate(dentry, inode);
Amir Goldstein5b6c9052017-04-24 19:54:13 +0300166 /* Force lookup of new upper hardlink to find its lower */
167 if (hardlink)
168 d_drop(dentry);
Vivek Goyal72e48482016-06-16 10:09:14 -0400169}
170
Amir Goldstein97c684c2016-11-21 18:57:34 +0200171static bool ovl_type_merge(struct dentry *dentry)
172{
173 return OVL_TYPE_MERGE(ovl_path_type(dentry));
174}
175
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300176static bool ovl_type_origin(struct dentry *dentry)
177{
178 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
179}
180
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000182 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200183{
184 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
185 struct inode *udir = upperdir->d_inode;
186 struct dentry *newdentry;
187 int err;
188
Miklos Szeredi38b25692016-09-01 11:11:59 +0200189 if (!hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000190 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200191
Al Viro59551022016-01-22 15:40:57 -0500192 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
194 dentry->d_name.len);
195 err = PTR_ERR(newdentry);
196 if (IS_ERR(newdentry))
197 goto out_unlock;
Al Viro32a3d842016-12-04 17:33:17 +0000198 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199 if (err)
200 goto out_dput;
201
Amir Goldstein4a99f3c2017-04-24 22:26:40 +0300202 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
Amir Goldstein97c684c2016-11-21 18:57:34 +0200203 /* Setting opaque here is just an optimization, allow to fail */
204 ovl_set_opaque(dentry, newdentry);
205 }
206
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200207 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200208 newdentry = NULL;
209out_dput:
210 dput(newdentry);
211out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500212 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200213 return err;
214}
215
216static int ovl_lock_rename_workdir(struct dentry *workdir,
217 struct dentry *upperdir)
218{
219 /* Workdir should not be the same as upperdir */
220 if (workdir == upperdir)
221 goto err;
222
223 /* Workdir should not be subdir of upperdir and vice versa */
224 if (lock_rename(workdir, upperdir) != NULL)
225 goto err_unlock;
226
227 return 0;
228
229err_unlock:
230 unlock_rename(workdir, upperdir);
231err:
232 pr_err("overlayfs: failed to lock workdir+upperdir\n");
233 return -EIO;
234}
235
236static struct dentry *ovl_clear_empty(struct dentry *dentry,
237 struct list_head *list)
238{
239 struct dentry *workdir = ovl_workdir(dentry);
240 struct inode *wdir = workdir->d_inode;
241 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
242 struct inode *udir = upperdir->d_inode;
243 struct path upperpath;
244 struct dentry *upper;
245 struct dentry *opaquedir;
246 struct kstat stat;
247 int err;
248
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200249 if (WARN_ON(!workdir))
250 return ERR_PTR(-EROFS);
251
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200252 err = ovl_lock_rename_workdir(workdir, upperdir);
253 if (err)
254 goto out;
255
256 ovl_path_upper(dentry, &upperpath);
David Howellsa528d352017-01-31 16:46:22 +0000257 err = vfs_getattr(&upperpath, &stat,
258 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200259 if (err)
260 goto out_unlock;
261
262 err = -ESTALE;
263 if (!S_ISDIR(stat.mode))
264 goto out_unlock;
265 upper = upperpath.dentry;
266 if (upper->d_parent->d_inode != udir)
267 goto out_unlock;
268
Miklos Szeredi3d275732017-05-19 09:33:49 +0200269 opaquedir = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270 err = PTR_ERR(opaquedir);
271 if (IS_ERR(opaquedir))
272 goto out_unlock;
273
Al Viro32a3d842016-12-04 17:33:17 +0000274 err = ovl_create_real(wdir, opaquedir,
275 &(struct cattr){.mode = stat.mode}, NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200276 if (err)
277 goto out_dput;
278
279 err = ovl_copy_xattr(upper, opaquedir);
280 if (err)
281 goto out_cleanup;
282
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100283 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200284 if (err)
285 goto out_cleanup;
286
Al Viro59551022016-01-22 15:40:57 -0500287 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500289 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200290 if (err)
291 goto out_cleanup;
292
293 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
294 if (err)
295 goto out_cleanup;
296
297 ovl_cleanup_whiteouts(upper, list);
298 ovl_cleanup(wdir, upper);
299 unlock_rename(workdir, upperdir);
300
301 /* dentry's upper doesn't match now, get rid of it */
302 d_drop(dentry);
303
304 return opaquedir;
305
306out_cleanup:
307 ovl_cleanup(wdir, opaquedir);
308out_dput:
309 dput(opaquedir);
310out_unlock:
311 unlock_rename(workdir, upperdir);
312out:
313 return ERR_PTR(err);
314}
315
Miklos Szeredia105d682014-11-20 16:39:59 +0100316static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317{
318 int err;
319 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200320 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321 LIST_HEAD(list);
322
323 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200324 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200326 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100327 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200329 /*
330 * When removing an empty opaque directory, then it makes no sense to
331 * replace it with an exact replica of itself.
332 *
333 * If no upperdentry then skip clearing whiteouts.
334 *
335 * Can race with copy-up, since we don't hold the upperdir mutex.
336 * Doesn't matter, since copy-up can't create a non-empty directory
337 * from an empty one.
338 */
339 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
340 ret = ovl_clear_empty(dentry, &list);
341
342out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 ovl_cache_free(&list);
344
345 return ret;
346}
347
Miklos Szeredi38b25692016-09-01 11:11:59 +0200348static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
349 const struct posix_acl *acl)
350{
351 void *buffer;
352 size_t size;
353 int err;
354
355 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
356 return 0;
357
358 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
359 buffer = kmalloc(size, GFP_KERNEL);
360 if (!buffer)
361 return -ENOMEM;
362
363 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
364 err = size;
365 if (err < 0)
366 goto out_free;
367
368 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
369out_free:
370 kfree(buffer);
371 return err;
372}
373
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200374static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000375 struct cattr *cattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376 struct dentry *hardlink)
377{
378 struct dentry *workdir = ovl_workdir(dentry);
379 struct inode *wdir = workdir->d_inode;
380 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
381 struct inode *udir = upperdir->d_inode;
382 struct dentry *upper;
383 struct dentry *newdentry;
384 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200385 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200387 if (WARN_ON(!workdir))
388 return -EROFS;
389
Miklos Szeredi38b25692016-09-01 11:11:59 +0200390 if (!hardlink) {
391 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000392 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200393 if (err)
394 return err;
395 }
396
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397 err = ovl_lock_rename_workdir(workdir, upperdir);
398 if (err)
399 goto out;
400
Miklos Szeredi3d275732017-05-19 09:33:49 +0200401 newdentry = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200402 err = PTR_ERR(newdentry);
403 if (IS_ERR(newdentry))
404 goto out_unlock;
405
406 upper = lookup_one_len(dentry->d_name.name, upperdir,
407 dentry->d_name.len);
408 err = PTR_ERR(upper);
409 if (IS_ERR(upper))
410 goto out_dput;
411
Al Viro32a3d842016-12-04 17:33:17 +0000412 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413 if (err)
414 goto out_dput2;
415
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200416 /*
417 * mode could have been mutilated due to umask (e.g. sgid directory)
418 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200419 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000420 !S_ISLNK(cattr->mode) &&
421 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200422 struct iattr attr = {
423 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000424 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200425 };
426 inode_lock(newdentry->d_inode);
427 err = notify_change(newdentry, &attr, NULL);
428 inode_unlock(newdentry->d_inode);
429 if (err)
430 goto out_cleanup;
431 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200432 if (!hardlink) {
433 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
434 acl);
435 if (err)
436 goto out_cleanup;
437
438 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
439 default_acl);
440 if (err)
441 goto out_cleanup;
442 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200443
Al Viro32a3d842016-12-04 17:33:17 +0000444 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100445 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200446 if (err)
447 goto out_cleanup;
448
449 err = ovl_do_rename(wdir, newdentry, udir, upper,
450 RENAME_EXCHANGE);
451 if (err)
452 goto out_cleanup;
453
454 ovl_cleanup(wdir, upper);
455 } else {
456 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
457 if (err)
458 goto out_cleanup;
459 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200460 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200461 newdentry = NULL;
462out_dput2:
463 dput(upper);
464out_dput:
465 dput(newdentry);
466out_unlock:
467 unlock_rename(workdir, upperdir);
468out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200469 if (!hardlink) {
470 posix_acl_release(acl);
471 posix_acl_release(default_acl);
472 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200473 return err;
474
475out_cleanup:
476 ovl_cleanup(wdir, newdentry);
477 goto out_dput2;
478}
479
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200480static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000481 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482{
483 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400484 const struct cred *old_cred;
485 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200486
487 err = ovl_copy_up(dentry->d_parent);
488 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200489 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200490
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400491 old_cred = ovl_override_creds(dentry->d_sb);
492 err = -ENOMEM;
493 override_cred = prepare_creds();
494 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200495 override_cred->fsuid = inode->i_uid;
496 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400497 if (!hardlink) {
498 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000499 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400500 override_cred);
501 if (err) {
502 put_cred(override_cred);
503 goto out_revert_creds;
504 }
505 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400506 put_cred(override_creds(override_cred));
507 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200508
Miklos Szeredic412ce42016-12-16 11:02:55 +0100509 if (!ovl_dentry_is_whiteout(dentry))
Al Viro32a3d842016-12-04 17:33:17 +0000510 err = ovl_create_upper(dentry, inode, attr,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400511 hardlink);
512 else
Al Viro32a3d842016-12-04 17:33:17 +0000513 err = ovl_create_over_whiteout(dentry, inode, attr,
514 hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200515 }
Vivek Goyal26026252016-07-13 10:44:52 -0400516out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400517 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200518 if (!err) {
519 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
520
521 WARN_ON(inode->i_mode != realinode->i_mode);
522 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
523 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200524 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200525 return err;
526}
527
528static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
529 const char *link)
530{
531 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200532 struct inode *inode;
Al Viro32a3d842016-12-04 17:33:17 +0000533 struct cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200534 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000535 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200536 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200537
538 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200539 if (err)
540 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200541
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200542 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100543 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200544 if (!inode)
545 goto out_drop_write;
546
547 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000548 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200549
Al Viro32a3d842016-12-04 17:33:17 +0000550 err = ovl_create_or_link(dentry, inode, &attr, NULL);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200551 if (err)
552 iput(inode);
553
554out_drop_write:
555 ovl_drop_write(dentry);
556out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200557 return err;
558}
559
560static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
561 bool excl)
562{
563 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
564}
565
566static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
567{
568 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
569}
570
571static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
572 dev_t rdev)
573{
574 /* Don't allow creation of "whiteout" on overlay */
575 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
576 return -EPERM;
577
578 return ovl_create_object(dentry, mode, rdev, NULL);
579}
580
581static int ovl_symlink(struct inode *dir, struct dentry *dentry,
582 const char *link)
583{
584 return ovl_create_object(dentry, S_IFLNK, 0, link);
585}
586
587static int ovl_link(struct dentry *old, struct inode *newdir,
588 struct dentry *new)
589{
590 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200591 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200592
593 err = ovl_want_write(old);
594 if (err)
595 goto out;
596
597 err = ovl_copy_up(old);
598 if (err)
599 goto out_drop_write;
600
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200601 inode = d_inode(old);
602 ihold(inode);
603
Al Viro32a3d842016-12-04 17:33:17 +0000604 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200605 if (err)
606 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200607
608out_drop_write:
609 ovl_drop_write(old);
610out:
611 return err;
612}
613
Miklos Szeredia105d682014-11-20 16:39:59 +0100614static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200615{
616 struct dentry *workdir = ovl_workdir(dentry);
617 struct inode *wdir = workdir->d_inode;
618 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
619 struct inode *udir = upperdir->d_inode;
620 struct dentry *whiteout;
621 struct dentry *upper;
622 struct dentry *opaquedir = NULL;
623 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700624 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200625
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200626 if (WARN_ON(!workdir))
627 return -EROFS;
628
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200629 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200630 opaquedir = ovl_check_empty_and_clear(dentry);
631 err = PTR_ERR(opaquedir);
632 if (IS_ERR(opaquedir))
633 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200634 }
635
636 err = ovl_lock_rename_workdir(workdir, upperdir);
637 if (err)
638 goto out_dput;
639
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700640 upper = lookup_one_len(dentry->d_name.name, upperdir,
641 dentry->d_name.len);
642 err = PTR_ERR(upper);
643 if (IS_ERR(upper))
644 goto out_unlock;
645
646 err = -ESTALE;
647 if ((opaquedir && upper != opaquedir) ||
648 (!opaquedir && ovl_dentry_upper(dentry) &&
649 upper != ovl_dentry_upper(dentry))) {
650 goto out_dput_upper;
651 }
652
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200653 whiteout = ovl_whiteout(workdir, dentry);
654 err = PTR_ERR(whiteout);
655 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700656 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200657
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700658 if (d_is_dir(upper))
659 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200660
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700661 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
662 if (err)
663 goto kill_whiteout;
664 if (flags)
665 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200666
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200667 ovl_dentry_version_inc(dentry->d_parent);
668out_d_drop:
669 d_drop(dentry);
670 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700671out_dput_upper:
672 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200673out_unlock:
674 unlock_rename(workdir, upperdir);
675out_dput:
676 dput(opaquedir);
677out:
678 return err;
679
680kill_whiteout:
681 ovl_cleanup(wdir, whiteout);
682 goto out_d_drop;
683}
684
685static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
686{
687 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
688 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100689 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300690 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200691 int err;
692
Amir Goldsteind1595112016-10-26 12:34:06 +0300693 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
694 if (is_dir && ovl_dentry_get_redirect(dentry)) {
695 opaquedir = ovl_check_empty_and_clear(dentry);
696 err = PTR_ERR(opaquedir);
697 if (IS_ERR(opaquedir))
698 goto out;
699 }
700
Al Viro59551022016-01-22 15:40:57 -0500701 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100702 upper = lookup_one_len(dentry->d_name.name, upperdir,
703 dentry->d_name.len);
704 err = PTR_ERR(upper);
705 if (IS_ERR(upper))
706 goto out_unlock;
707
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200708 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300709 if ((opaquedir && upper != opaquedir) ||
710 (!opaquedir && upper != ovl_dentry_upper(dentry)))
711 goto out_dput_upper;
712
713 if (is_dir)
714 err = vfs_rmdir(dir, upper);
715 else
716 err = vfs_unlink(dir, upper, NULL);
717 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200718
719 /*
720 * Keeping this dentry hashed would mean having to release
721 * upperpath/lowerpath, which could only be done if we are the
722 * sole user of this dentry. Too tricky... Just unhash for
723 * now.
724 */
Rui Wangce9113b2016-01-08 23:09:59 +0800725 if (!err)
726 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300727out_dput_upper:
728 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100729out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500730 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300731 dput(opaquedir);
732out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200733 return err;
734}
735
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200736static int ovl_do_remove(struct dentry *dentry, bool is_dir)
737{
738 enum ovl_path_type type;
739 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400740 const struct cred *old_cred;
741
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200742 err = ovl_want_write(dentry);
743 if (err)
744 goto out;
745
746 err = ovl_copy_up(dentry->d_parent);
747 if (err)
748 goto out_drop_write;
749
750 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400751
752 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100753 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200754 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400755 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100756 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400757 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200758 if (!err) {
759 if (is_dir)
760 clear_nlink(dentry->d_inode);
761 else
762 drop_nlink(dentry->d_inode);
763 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200764out_drop_write:
765 ovl_drop_write(dentry);
766out:
767 return err;
768}
769
770static int ovl_unlink(struct inode *dir, struct dentry *dentry)
771{
772 return ovl_do_remove(dentry, false);
773}
774
775static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
776{
777 return ovl_do_remove(dentry, true);
778}
779
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100780static bool ovl_type_merge_or_lower(struct dentry *dentry)
781{
782 enum ovl_path_type type = ovl_path_type(dentry);
783
784 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
785}
786
Miklos Szeredia6c60652016-12-16 11:02:56 +0100787static bool ovl_can_move(struct dentry *dentry)
788{
789 return ovl_redirect_dir(dentry->d_sb) ||
790 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
791}
792
Miklos Szeredia6c60652016-12-16 11:02:56 +0100793static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
794{
795 char *buf, *ret;
796 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100797 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100798
799 if (samedir) {
800 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
801 GFP_KERNEL);
802 goto out;
803 }
804
805 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
806 if (!buf)
807 goto out;
808
809 buflen--;
810 buf[buflen] = '\0';
811 for (d = dget(dentry); !IS_ROOT(d);) {
812 const char *name;
813 int thislen;
814
815 spin_lock(&d->d_lock);
816 name = ovl_dentry_get_redirect(d);
817 if (name) {
818 thislen = strlen(name);
819 } else {
820 name = d->d_name.name;
821 thislen = d->d_name.len;
822 }
823
824 /* If path is too long, fall back to userspace move */
825 if (thislen + (name[0] != '/') > buflen) {
826 ret = ERR_PTR(-EXDEV);
827 spin_unlock(&d->d_lock);
828 goto out_put;
829 }
830
831 buflen -= thislen;
832 memcpy(&buf[buflen], name, thislen);
833 tmp = dget_dlock(d->d_parent);
834 spin_unlock(&d->d_lock);
835
836 dput(d);
837 d = tmp;
838
839 /* Absolute redirect: finished */
840 if (buf[buflen] == '/')
841 break;
842 buflen--;
843 buf[buflen] = '/';
844 }
845 ret = kstrdup(&buf[buflen], GFP_KERNEL);
846out_put:
847 dput(d);
848 kfree(buf);
849out:
850 return ret ? ret : ERR_PTR(-ENOMEM);
851}
852
853static int ovl_set_redirect(struct dentry *dentry, bool samedir)
854{
855 int err;
856 const char *redirect = ovl_dentry_get_redirect(dentry);
857
858 if (redirect && (samedir || redirect[0] == '/'))
859 return 0;
860
861 redirect = ovl_get_redirect(dentry, samedir);
862 if (IS_ERR(redirect))
863 return PTR_ERR(redirect);
864
Amir Goldstein21a22872017-05-17 00:12:41 +0300865 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
866 OVL_XATTR_REDIRECT,
867 redirect, strlen(redirect), -EXDEV);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100868 if (!err) {
869 spin_lock(&dentry->d_lock);
870 ovl_dentry_set_redirect(dentry, redirect);
871 spin_unlock(&dentry->d_lock);
872 } else {
873 kfree(redirect);
Amir Goldstein21a22872017-05-17 00:12:41 +0300874 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100875 /* Fall back to userspace copy-up */
876 err = -EXDEV;
877 }
878 return err;
879}
880
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100881static int ovl_rename(struct inode *olddir, struct dentry *old,
882 struct inode *newdir, struct dentry *new,
883 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200884{
885 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200886 struct dentry *old_upperdir;
887 struct dentry *new_upperdir;
888 struct dentry *olddentry;
889 struct dentry *newdentry;
890 struct dentry *trap;
891 bool old_opaque;
892 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200893 bool cleanup_whiteout = false;
894 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000895 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100896 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100897 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200898 struct dentry *opaquedir = NULL;
899 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200900
901 err = -EINVAL;
902 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
903 goto out;
904
905 flags &= ~RENAME_NOREPLACE;
906
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200907 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200908 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100909 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200910 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100911 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100912 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200913
914 err = ovl_want_write(old);
915 if (err)
916 goto out;
917
918 err = ovl_copy_up(old);
919 if (err)
920 goto out_drop_write;
921
922 err = ovl_copy_up(new->d_parent);
923 if (err)
924 goto out_drop_write;
925 if (!overwrite) {
926 err = ovl_copy_up(new);
927 if (err)
928 goto out_drop_write;
929 }
930
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400931 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200932
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100933 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100934 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200935 err = PTR_ERR(opaquedir);
936 if (IS_ERR(opaquedir)) {
937 opaquedir = NULL;
938 goto out_revert_creds;
939 }
940 }
941
942 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100943 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100944 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200945 /* Whiteout source */
946 flags |= RENAME_WHITEOUT;
947 } else {
948 /* Switch whiteouts */
949 flags |= RENAME_EXCHANGE;
950 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100951 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200952 flags |= RENAME_EXCHANGE;
953 cleanup_whiteout = true;
954 }
955 }
956
957 old_upperdir = ovl_dentry_upper(old->d_parent);
958 new_upperdir = ovl_dentry_upper(new->d_parent);
959
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300960 if (!samedir) {
961 /*
962 * When moving a merge dir or non-dir with copy up origin into
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300963 * a new parent, we are marking the new parent dir "impure".
964 * When ovl_iterate() iterates an "impure" upper dir, it will
965 * lookup the origin inodes of the entries to fill d_ino.
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300966 */
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300967 if (ovl_type_origin(old)) {
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300968 err = ovl_set_impure(new->d_parent, new_upperdir);
969 if (err)
970 goto out_revert_creds;
971 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300972 if (!overwrite && ovl_type_origin(new)) {
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300973 err = ovl_set_impure(old->d_parent, old_upperdir);
974 if (err)
975 goto out_revert_creds;
976 }
977 }
978
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200979 trap = lock_rename(new_upperdir, old_upperdir);
980
Miklos Szeredi11f37102016-03-21 17:31:44 +0100981 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
982 old->d_name.len);
983 err = PTR_ERR(olddentry);
984 if (IS_ERR(olddentry))
985 goto out_unlock;
986
987 err = -ESTALE;
988 if (olddentry != ovl_dentry_upper(old))
989 goto out_dput_old;
990
991 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
992 new->d_name.len);
993 err = PTR_ERR(newdentry);
994 if (IS_ERR(newdentry))
995 goto out_dput_old;
996
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100997 old_opaque = ovl_dentry_is_opaque(old);
998 new_opaque = ovl_dentry_is_opaque(new);
999
Miklos Szeredi11f37102016-03-21 17:31:44 +01001000 err = -ESTALE;
1001 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001002 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001003 if (newdentry != opaquedir)
1004 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001005 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001006 if (newdentry != ovl_dentry_upper(new))
1007 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001008 }
1009 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001010 if (!d_is_negative(newdentry) &&
1011 (!new_opaque || !ovl_is_whiteout(newdentry)))
1012 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001013 }
1014
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001015 if (olddentry == trap)
1016 goto out_dput;
1017 if (newdentry == trap)
1018 goto out_dput;
1019
Miklos Szeredi804032f2016-12-16 11:02:55 +01001020 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1021 goto out_dput;
1022
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001023 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001024 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001025 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001026 err = ovl_set_redirect(old, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001027 else if (!old_opaque && ovl_type_merge(new->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001028 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001029 if (err)
1030 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001031 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001032 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001033 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001034 err = ovl_set_redirect(new, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001035 else if (!new_opaque && ovl_type_merge(old->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001036 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001037 if (err)
1038 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001039 }
1040
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001041 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1042 new_upperdir->d_inode, newdentry, flags);
1043 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001044 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001045
1046 if (cleanup_whiteout)
1047 ovl_cleanup(old_upperdir->d_inode, newdentry);
1048
1049 ovl_dentry_version_inc(old->d_parent);
1050 ovl_dentry_version_inc(new->d_parent);
1051
1052out_dput:
1053 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001054out_dput_old:
1055 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001056out_unlock:
1057 unlock_rename(new_upperdir, old_upperdir);
1058out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001059 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001060out_drop_write:
1061 ovl_drop_write(old);
1062out:
1063 dput(opaquedir);
1064 return err;
1065}
1066
1067const struct inode_operations ovl_dir_inode_operations = {
1068 .lookup = ovl_lookup,
1069 .mkdir = ovl_mkdir,
1070 .symlink = ovl_symlink,
1071 .unlink = ovl_unlink,
1072 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001073 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001074 .link = ovl_link,
1075 .setattr = ovl_setattr,
1076 .create = ovl_create,
1077 .mknod = ovl_mknod,
1078 .permission = ovl_permission,
Miklos Szeredi5b712092017-05-05 11:38:58 +02001079 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001080 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001081 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001082 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001083};