blob: 6515796460dfe170fb33b109feb09c9ac0b17519 [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
44struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
45{
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
71 whiteout = ovl_lookup_temp(workdir, dentry);
72 if (IS_ERR(whiteout))
73 return whiteout;
74
75 err = ovl_do_whiteout(wdir, whiteout);
76 if (err) {
77 dput(whiteout);
78 whiteout = ERR_PTR(err);
79 }
80
81 return whiteout;
82}
83
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
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100130static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200131{
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100132 int err;
133
134 err = ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
135 if (!err)
136 ovl_dentry_set_opaque(dentry);
137
138 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200139}
140
David Howellsa528d352017-01-31 16:46:22 +0000141static int ovl_dir_getattr(const struct path *path, struct kstat *stat,
142 u32 request_mask, unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200143{
David Howellsa528d352017-01-31 16:46:22 +0000144 struct dentry *dentry = path->dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200145 int err;
146 enum ovl_path_type type;
147 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400148 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149
150 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400151 old_cred = ovl_override_creds(dentry->d_sb);
David Howellsa528d352017-01-31 16:46:22 +0000152 err = vfs_getattr(&realpath, stat, request_mask, flags);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400153 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200154 if (err)
155 return err;
156
157 stat->dev = dentry->d_sb->s_dev;
158 stat->ino = dentry->d_inode->i_ino;
159
160 /*
161 * It's probably not worth it to count subdirs to get the
162 * correct link count. nlink=1 seems to pacify 'find' and
163 * other utilities.
164 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100165 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200166 stat->nlink = 1;
167
168 return 0;
169}
170
Vivek Goyal72e48482016-06-16 10:09:14 -0400171/* Common operations required to be done after creation of file on upper */
172static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200173 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400174{
175 ovl_dentry_version_inc(dentry->d_parent);
176 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200177 if (!hardlink) {
178 ovl_inode_update(inode, d_inode(newdentry));
179 ovl_copyattr(newdentry->d_inode, inode);
180 } else {
181 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
182 inc_nlink(inode);
183 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400184 d_instantiate(dentry, inode);
185}
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
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200192static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000193 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200194{
195 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
196 struct inode *udir = upperdir->d_inode;
197 struct dentry *newdentry;
198 int err;
199
Miklos Szeredi38b25692016-09-01 11:11:59 +0200200 if (!hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000201 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200202
Al Viro59551022016-01-22 15:40:57 -0500203 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200204 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
205 dentry->d_name.len);
206 err = PTR_ERR(newdentry);
207 if (IS_ERR(newdentry))
208 goto out_unlock;
Al Viro32a3d842016-12-04 17:33:17 +0000209 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200210 if (err)
211 goto out_dput;
212
Amir Goldstein97c684c2016-11-21 18:57:34 +0200213 if (ovl_type_merge(dentry->d_parent)) {
214 /* Setting opaque here is just an optimization, allow to fail */
215 ovl_set_opaque(dentry, newdentry);
216 }
217
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200218 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200219 newdentry = NULL;
220out_dput:
221 dput(newdentry);
222out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500223 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200224 return err;
225}
226
227static int ovl_lock_rename_workdir(struct dentry *workdir,
228 struct dentry *upperdir)
229{
230 /* Workdir should not be the same as upperdir */
231 if (workdir == upperdir)
232 goto err;
233
234 /* Workdir should not be subdir of upperdir and vice versa */
235 if (lock_rename(workdir, upperdir) != NULL)
236 goto err_unlock;
237
238 return 0;
239
240err_unlock:
241 unlock_rename(workdir, upperdir);
242err:
243 pr_err("overlayfs: failed to lock workdir+upperdir\n");
244 return -EIO;
245}
246
247static struct dentry *ovl_clear_empty(struct dentry *dentry,
248 struct list_head *list)
249{
250 struct dentry *workdir = ovl_workdir(dentry);
251 struct inode *wdir = workdir->d_inode;
252 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
253 struct inode *udir = upperdir->d_inode;
254 struct path upperpath;
255 struct dentry *upper;
256 struct dentry *opaquedir;
257 struct kstat stat;
258 int err;
259
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200260 if (WARN_ON(!workdir))
261 return ERR_PTR(-EROFS);
262
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200263 err = ovl_lock_rename_workdir(workdir, upperdir);
264 if (err)
265 goto out;
266
267 ovl_path_upper(dentry, &upperpath);
David Howellsa528d352017-01-31 16:46:22 +0000268 err = vfs_getattr(&upperpath, &stat,
269 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270 if (err)
271 goto out_unlock;
272
273 err = -ESTALE;
274 if (!S_ISDIR(stat.mode))
275 goto out_unlock;
276 upper = upperpath.dentry;
277 if (upper->d_parent->d_inode != udir)
278 goto out_unlock;
279
280 opaquedir = ovl_lookup_temp(workdir, dentry);
281 err = PTR_ERR(opaquedir);
282 if (IS_ERR(opaquedir))
283 goto out_unlock;
284
Al Viro32a3d842016-12-04 17:33:17 +0000285 err = ovl_create_real(wdir, opaquedir,
286 &(struct cattr){.mode = stat.mode}, NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200287 if (err)
288 goto out_dput;
289
290 err = ovl_copy_xattr(upper, opaquedir);
291 if (err)
292 goto out_cleanup;
293
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100294 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200295 if (err)
296 goto out_cleanup;
297
Al Viro59551022016-01-22 15:40:57 -0500298 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200299 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500300 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301 if (err)
302 goto out_cleanup;
303
304 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
305 if (err)
306 goto out_cleanup;
307
308 ovl_cleanup_whiteouts(upper, list);
309 ovl_cleanup(wdir, upper);
310 unlock_rename(workdir, upperdir);
311
312 /* dentry's upper doesn't match now, get rid of it */
313 d_drop(dentry);
314
315 return opaquedir;
316
317out_cleanup:
318 ovl_cleanup(wdir, opaquedir);
319out_dput:
320 dput(opaquedir);
321out_unlock:
322 unlock_rename(workdir, upperdir);
323out:
324 return ERR_PTR(err);
325}
326
Miklos Szeredia105d682014-11-20 16:39:59 +0100327static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328{
329 int err;
330 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200331 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200332 LIST_HEAD(list);
333
334 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200335 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200336 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200337 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100338 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200340 /*
341 * When removing an empty opaque directory, then it makes no sense to
342 * replace it with an exact replica of itself.
343 *
344 * If no upperdentry then skip clearing whiteouts.
345 *
346 * Can race with copy-up, since we don't hold the upperdir mutex.
347 * Doesn't matter, since copy-up can't create a non-empty directory
348 * from an empty one.
349 */
350 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
351 ret = ovl_clear_empty(dentry, &list);
352
353out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200354 ovl_cache_free(&list);
355
356 return ret;
357}
358
Miklos Szeredi38b25692016-09-01 11:11:59 +0200359static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
360 const struct posix_acl *acl)
361{
362 void *buffer;
363 size_t size;
364 int err;
365
366 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
367 return 0;
368
369 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
370 buffer = kmalloc(size, GFP_KERNEL);
371 if (!buffer)
372 return -ENOMEM;
373
374 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
375 err = size;
376 if (err < 0)
377 goto out_free;
378
379 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
380out_free:
381 kfree(buffer);
382 return err;
383}
384
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000386 struct cattr *cattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200387 struct dentry *hardlink)
388{
389 struct dentry *workdir = ovl_workdir(dentry);
390 struct inode *wdir = workdir->d_inode;
391 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
392 struct inode *udir = upperdir->d_inode;
393 struct dentry *upper;
394 struct dentry *newdentry;
395 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200396 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200398 if (WARN_ON(!workdir))
399 return -EROFS;
400
Miklos Szeredi38b25692016-09-01 11:11:59 +0200401 if (!hardlink) {
402 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000403 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200404 if (err)
405 return err;
406 }
407
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 err = ovl_lock_rename_workdir(workdir, upperdir);
409 if (err)
410 goto out;
411
412 newdentry = ovl_lookup_temp(workdir, dentry);
413 err = PTR_ERR(newdentry);
414 if (IS_ERR(newdentry))
415 goto out_unlock;
416
417 upper = lookup_one_len(dentry->d_name.name, upperdir,
418 dentry->d_name.len);
419 err = PTR_ERR(upper);
420 if (IS_ERR(upper))
421 goto out_dput;
422
Al Viro32a3d842016-12-04 17:33:17 +0000423 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424 if (err)
425 goto out_dput2;
426
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200427 /*
428 * mode could have been mutilated due to umask (e.g. sgid directory)
429 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200430 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000431 !S_ISLNK(cattr->mode) &&
432 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200433 struct iattr attr = {
434 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000435 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200436 };
437 inode_lock(newdentry->d_inode);
438 err = notify_change(newdentry, &attr, NULL);
439 inode_unlock(newdentry->d_inode);
440 if (err)
441 goto out_cleanup;
442 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200443 if (!hardlink) {
444 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
445 acl);
446 if (err)
447 goto out_cleanup;
448
449 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
450 default_acl);
451 if (err)
452 goto out_cleanup;
453 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200454
Al Viro32a3d842016-12-04 17:33:17 +0000455 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100456 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200457 if (err)
458 goto out_cleanup;
459
460 err = ovl_do_rename(wdir, newdentry, udir, upper,
461 RENAME_EXCHANGE);
462 if (err)
463 goto out_cleanup;
464
465 ovl_cleanup(wdir, upper);
466 } else {
467 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
468 if (err)
469 goto out_cleanup;
470 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200471 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472 newdentry = NULL;
473out_dput2:
474 dput(upper);
475out_dput:
476 dput(newdentry);
477out_unlock:
478 unlock_rename(workdir, upperdir);
479out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200480 if (!hardlink) {
481 posix_acl_release(acl);
482 posix_acl_release(default_acl);
483 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200484 return err;
485
486out_cleanup:
487 ovl_cleanup(wdir, newdentry);
488 goto out_dput2;
489}
490
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200491static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000492 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200493{
494 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400495 const struct cred *old_cred;
496 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200497
498 err = ovl_copy_up(dentry->d_parent);
499 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200500 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200501
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400502 old_cred = ovl_override_creds(dentry->d_sb);
503 err = -ENOMEM;
504 override_cred = prepare_creds();
505 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200506 override_cred->fsuid = inode->i_uid;
507 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400508 if (!hardlink) {
509 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000510 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400511 override_cred);
512 if (err) {
513 put_cred(override_cred);
514 goto out_revert_creds;
515 }
516 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400517 put_cred(override_creds(override_cred));
518 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200519
Miklos Szeredic412ce42016-12-16 11:02:55 +0100520 if (!ovl_dentry_is_whiteout(dentry))
Al Viro32a3d842016-12-04 17:33:17 +0000521 err = ovl_create_upper(dentry, inode, attr,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400522 hardlink);
523 else
Al Viro32a3d842016-12-04 17:33:17 +0000524 err = ovl_create_over_whiteout(dentry, inode, attr,
525 hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200526 }
Vivek Goyal26026252016-07-13 10:44:52 -0400527out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400528 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200529 if (!err) {
530 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
531
532 WARN_ON(inode->i_mode != realinode->i_mode);
533 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
534 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200535 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536 return err;
537}
538
539static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
540 const char *link)
541{
542 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200543 struct inode *inode;
Al Viro32a3d842016-12-04 17:33:17 +0000544 struct cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200545 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000546 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200547 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200548
549 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200550 if (err)
551 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200552
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200553 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100554 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200555 if (!inode)
556 goto out_drop_write;
557
558 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000559 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200560
Al Viro32a3d842016-12-04 17:33:17 +0000561 err = ovl_create_or_link(dentry, inode, &attr, NULL);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200562 if (err)
563 iput(inode);
564
565out_drop_write:
566 ovl_drop_write(dentry);
567out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200568 return err;
569}
570
571static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
572 bool excl)
573{
574 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
575}
576
577static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
578{
579 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
580}
581
582static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
583 dev_t rdev)
584{
585 /* Don't allow creation of "whiteout" on overlay */
586 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
587 return -EPERM;
588
589 return ovl_create_object(dentry, mode, rdev, NULL);
590}
591
592static int ovl_symlink(struct inode *dir, struct dentry *dentry,
593 const char *link)
594{
595 return ovl_create_object(dentry, S_IFLNK, 0, link);
596}
597
598static int ovl_link(struct dentry *old, struct inode *newdir,
599 struct dentry *new)
600{
601 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200602 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200603
604 err = ovl_want_write(old);
605 if (err)
606 goto out;
607
608 err = ovl_copy_up(old);
609 if (err)
610 goto out_drop_write;
611
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200612 inode = d_inode(old);
613 ihold(inode);
614
Al Viro32a3d842016-12-04 17:33:17 +0000615 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200616 if (err)
617 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200618
619out_drop_write:
620 ovl_drop_write(old);
621out:
622 return err;
623}
624
Miklos Szeredia105d682014-11-20 16:39:59 +0100625static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200626{
627 struct dentry *workdir = ovl_workdir(dentry);
628 struct inode *wdir = workdir->d_inode;
629 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
630 struct inode *udir = upperdir->d_inode;
631 struct dentry *whiteout;
632 struct dentry *upper;
633 struct dentry *opaquedir = NULL;
634 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700635 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200636
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200637 if (WARN_ON(!workdir))
638 return -EROFS;
639
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200640 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200641 opaquedir = ovl_check_empty_and_clear(dentry);
642 err = PTR_ERR(opaquedir);
643 if (IS_ERR(opaquedir))
644 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200645 }
646
647 err = ovl_lock_rename_workdir(workdir, upperdir);
648 if (err)
649 goto out_dput;
650
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700651 upper = lookup_one_len(dentry->d_name.name, upperdir,
652 dentry->d_name.len);
653 err = PTR_ERR(upper);
654 if (IS_ERR(upper))
655 goto out_unlock;
656
657 err = -ESTALE;
658 if ((opaquedir && upper != opaquedir) ||
659 (!opaquedir && ovl_dentry_upper(dentry) &&
660 upper != ovl_dentry_upper(dentry))) {
661 goto out_dput_upper;
662 }
663
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200664 whiteout = ovl_whiteout(workdir, dentry);
665 err = PTR_ERR(whiteout);
666 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700667 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200668
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700669 if (d_is_dir(upper))
670 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200671
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700672 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
673 if (err)
674 goto kill_whiteout;
675 if (flags)
676 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200677
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200678 ovl_dentry_version_inc(dentry->d_parent);
679out_d_drop:
680 d_drop(dentry);
681 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700682out_dput_upper:
683 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200684out_unlock:
685 unlock_rename(workdir, upperdir);
686out_dput:
687 dput(opaquedir);
688out:
689 return err;
690
691kill_whiteout:
692 ovl_cleanup(wdir, whiteout);
693 goto out_d_drop;
694}
695
696static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
697{
698 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
699 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100700 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300701 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200702 int err;
703
Amir Goldsteind1595112016-10-26 12:34:06 +0300704 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
705 if (is_dir && ovl_dentry_get_redirect(dentry)) {
706 opaquedir = ovl_check_empty_and_clear(dentry);
707 err = PTR_ERR(opaquedir);
708 if (IS_ERR(opaquedir))
709 goto out;
710 }
711
Al Viro59551022016-01-22 15:40:57 -0500712 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100713 upper = lookup_one_len(dentry->d_name.name, upperdir,
714 dentry->d_name.len);
715 err = PTR_ERR(upper);
716 if (IS_ERR(upper))
717 goto out_unlock;
718
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200719 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300720 if ((opaquedir && upper != opaquedir) ||
721 (!opaquedir && upper != ovl_dentry_upper(dentry)))
722 goto out_dput_upper;
723
724 if (is_dir)
725 err = vfs_rmdir(dir, upper);
726 else
727 err = vfs_unlink(dir, upper, NULL);
728 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200729
730 /*
731 * Keeping this dentry hashed would mean having to release
732 * upperpath/lowerpath, which could only be done if we are the
733 * sole user of this dentry. Too tricky... Just unhash for
734 * now.
735 */
Rui Wangce9113b2016-01-08 23:09:59 +0800736 if (!err)
737 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300738out_dput_upper:
739 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100740out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500741 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300742 dput(opaquedir);
743out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200744 return err;
745}
746
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200747static int ovl_do_remove(struct dentry *dentry, bool is_dir)
748{
749 enum ovl_path_type type;
750 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400751 const struct cred *old_cred;
752
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200753 err = ovl_want_write(dentry);
754 if (err)
755 goto out;
756
757 err = ovl_copy_up(dentry->d_parent);
758 if (err)
759 goto out_drop_write;
760
761 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400762
763 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100764 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200765 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400766 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100767 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400768 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200769 if (!err) {
770 if (is_dir)
771 clear_nlink(dentry->d_inode);
772 else
773 drop_nlink(dentry->d_inode);
774 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200775out_drop_write:
776 ovl_drop_write(dentry);
777out:
778 return err;
779}
780
781static int ovl_unlink(struct inode *dir, struct dentry *dentry)
782{
783 return ovl_do_remove(dentry, false);
784}
785
786static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
787{
788 return ovl_do_remove(dentry, true);
789}
790
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100791static bool ovl_type_merge_or_lower(struct dentry *dentry)
792{
793 enum ovl_path_type type = ovl_path_type(dentry);
794
795 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
796}
797
Miklos Szeredia6c60652016-12-16 11:02:56 +0100798static bool ovl_can_move(struct dentry *dentry)
799{
800 return ovl_redirect_dir(dentry->d_sb) ||
801 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
802}
803
Miklos Szeredia6c60652016-12-16 11:02:56 +0100804static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
805{
806 char *buf, *ret;
807 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100808 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100809
810 if (samedir) {
811 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
812 GFP_KERNEL);
813 goto out;
814 }
815
816 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
817 if (!buf)
818 goto out;
819
820 buflen--;
821 buf[buflen] = '\0';
822 for (d = dget(dentry); !IS_ROOT(d);) {
823 const char *name;
824 int thislen;
825
826 spin_lock(&d->d_lock);
827 name = ovl_dentry_get_redirect(d);
828 if (name) {
829 thislen = strlen(name);
830 } else {
831 name = d->d_name.name;
832 thislen = d->d_name.len;
833 }
834
835 /* If path is too long, fall back to userspace move */
836 if (thislen + (name[0] != '/') > buflen) {
837 ret = ERR_PTR(-EXDEV);
838 spin_unlock(&d->d_lock);
839 goto out_put;
840 }
841
842 buflen -= thislen;
843 memcpy(&buf[buflen], name, thislen);
844 tmp = dget_dlock(d->d_parent);
845 spin_unlock(&d->d_lock);
846
847 dput(d);
848 d = tmp;
849
850 /* Absolute redirect: finished */
851 if (buf[buflen] == '/')
852 break;
853 buflen--;
854 buf[buflen] = '/';
855 }
856 ret = kstrdup(&buf[buflen], GFP_KERNEL);
857out_put:
858 dput(d);
859 kfree(buf);
860out:
861 return ret ? ret : ERR_PTR(-ENOMEM);
862}
863
864static int ovl_set_redirect(struct dentry *dentry, bool samedir)
865{
866 int err;
867 const char *redirect = ovl_dentry_get_redirect(dentry);
868
869 if (redirect && (samedir || redirect[0] == '/'))
870 return 0;
871
872 redirect = ovl_get_redirect(dentry, samedir);
873 if (IS_ERR(redirect))
874 return PTR_ERR(redirect);
875
876 err = ovl_do_setxattr(ovl_dentry_upper(dentry), OVL_XATTR_REDIRECT,
877 redirect, strlen(redirect), 0);
878 if (!err) {
879 spin_lock(&dentry->d_lock);
880 ovl_dentry_set_redirect(dentry, redirect);
881 spin_unlock(&dentry->d_lock);
882 } else {
883 kfree(redirect);
884 if (err == -EOPNOTSUPP)
885 ovl_clear_redirect_dir(dentry->d_sb);
886 else
887 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
888 /* Fall back to userspace copy-up */
889 err = -EXDEV;
890 }
891 return err;
892}
893
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100894static int ovl_rename(struct inode *olddir, struct dentry *old,
895 struct inode *newdir, struct dentry *new,
896 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200897{
898 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200899 struct dentry *old_upperdir;
900 struct dentry *new_upperdir;
901 struct dentry *olddentry;
902 struct dentry *newdentry;
903 struct dentry *trap;
904 bool old_opaque;
905 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200906 bool cleanup_whiteout = false;
907 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000908 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100909 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100910 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200911 struct dentry *opaquedir = NULL;
912 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200913
914 err = -EINVAL;
915 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
916 goto out;
917
918 flags &= ~RENAME_NOREPLACE;
919
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200920 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200921 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100922 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200923 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100924 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100925 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200926
927 err = ovl_want_write(old);
928 if (err)
929 goto out;
930
931 err = ovl_copy_up(old);
932 if (err)
933 goto out_drop_write;
934
935 err = ovl_copy_up(new->d_parent);
936 if (err)
937 goto out_drop_write;
938 if (!overwrite) {
939 err = ovl_copy_up(new);
940 if (err)
941 goto out_drop_write;
942 }
943
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400944 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200945
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100946 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100947 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200948 err = PTR_ERR(opaquedir);
949 if (IS_ERR(opaquedir)) {
950 opaquedir = NULL;
951 goto out_revert_creds;
952 }
953 }
954
955 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100956 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100957 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200958 /* Whiteout source */
959 flags |= RENAME_WHITEOUT;
960 } else {
961 /* Switch whiteouts */
962 flags |= RENAME_EXCHANGE;
963 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100964 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200965 flags |= RENAME_EXCHANGE;
966 cleanup_whiteout = true;
967 }
968 }
969
970 old_upperdir = ovl_dentry_upper(old->d_parent);
971 new_upperdir = ovl_dentry_upper(new->d_parent);
972
973 trap = lock_rename(new_upperdir, old_upperdir);
974
Miklos Szeredi11f37102016-03-21 17:31:44 +0100975 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
976 old->d_name.len);
977 err = PTR_ERR(olddentry);
978 if (IS_ERR(olddentry))
979 goto out_unlock;
980
981 err = -ESTALE;
982 if (olddentry != ovl_dentry_upper(old))
983 goto out_dput_old;
984
985 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
986 new->d_name.len);
987 err = PTR_ERR(newdentry);
988 if (IS_ERR(newdentry))
989 goto out_dput_old;
990
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100991 old_opaque = ovl_dentry_is_opaque(old);
992 new_opaque = ovl_dentry_is_opaque(new);
993
Miklos Szeredi11f37102016-03-21 17:31:44 +0100994 err = -ESTALE;
995 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200996 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100997 if (newdentry != opaquedir)
998 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200999 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001000 if (newdentry != ovl_dentry_upper(new))
1001 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001002 }
1003 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001004 if (!d_is_negative(newdentry) &&
1005 (!new_opaque || !ovl_is_whiteout(newdentry)))
1006 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001007 }
1008
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001009 if (olddentry == trap)
1010 goto out_dput;
1011 if (newdentry == trap)
1012 goto out_dput;
1013
Miklos Szeredi804032f2016-12-16 11:02:55 +01001014 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1015 goto out_dput;
1016
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001017 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001018 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001019 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001020 err = ovl_set_redirect(old, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001021 else if (!old_opaque && ovl_type_merge(new->d_parent))
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001022 err = ovl_set_opaque(old, olddentry);
1023 if (err)
1024 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001025 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001026 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001027 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001028 err = ovl_set_redirect(new, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001029 else if (!new_opaque && ovl_type_merge(old->d_parent))
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001030 err = ovl_set_opaque(new, newdentry);
1031 if (err)
1032 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001033 }
1034
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001035 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1036 new_upperdir->d_inode, newdentry, flags);
1037 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001038 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001039
1040 if (cleanup_whiteout)
1041 ovl_cleanup(old_upperdir->d_inode, newdentry);
1042
1043 ovl_dentry_version_inc(old->d_parent);
1044 ovl_dentry_version_inc(new->d_parent);
1045
1046out_dput:
1047 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001048out_dput_old:
1049 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001050out_unlock:
1051 unlock_rename(new_upperdir, old_upperdir);
1052out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001053 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001054out_drop_write:
1055 ovl_drop_write(old);
1056out:
1057 dput(opaquedir);
1058 return err;
1059}
1060
1061const struct inode_operations ovl_dir_inode_operations = {
1062 .lookup = ovl_lookup,
1063 .mkdir = ovl_mkdir,
1064 .symlink = ovl_symlink,
1065 .unlink = ovl_unlink,
1066 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001067 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001068 .link = ovl_link,
1069 .setattr = ovl_setattr,
1070 .create = ovl_create,
1071 .mknod = ovl_mknod,
1072 .permission = ovl_permission,
1073 .getattr = ovl_dir_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001074 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001075 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001076 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001077};