blob: 16e06dd894575900f0043f417c5bf6227b690cf9 [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
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200141static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
142 struct kstat *stat)
143{
144 int err;
145 enum ovl_path_type type;
146 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400147 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148
149 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400150 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200151 err = vfs_getattr(&realpath, stat);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400152 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153 if (err)
154 return err;
155
156 stat->dev = dentry->d_sb->s_dev;
157 stat->ino = dentry->d_inode->i_ino;
158
159 /*
160 * It's probably not worth it to count subdirs to get the
161 * correct link count. nlink=1 seems to pacify 'find' and
162 * other utilities.
163 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100164 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200165 stat->nlink = 1;
166
167 return 0;
168}
169
Vivek Goyal72e48482016-06-16 10:09:14 -0400170/* Common operations required to be done after creation of file on upper */
171static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200172 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400173{
174 ovl_dentry_version_inc(dentry->d_parent);
175 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200176 if (!hardlink) {
177 ovl_inode_update(inode, d_inode(newdentry));
178 ovl_copyattr(newdentry->d_inode, inode);
179 } else {
180 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
181 inc_nlink(inode);
182 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400183 d_instantiate(dentry, inode);
184}
185
Amir Goldstein97c684c2016-11-21 18:57:34 +0200186static bool ovl_type_merge(struct dentry *dentry)
187{
188 return OVL_TYPE_MERGE(ovl_path_type(dentry));
189}
190
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200191static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000192 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193{
194 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
195 struct inode *udir = upperdir->d_inode;
196 struct dentry *newdentry;
197 int err;
198
Miklos Szeredi38b25692016-09-01 11:11:59 +0200199 if (!hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000200 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200201
Al Viro59551022016-01-22 15:40:57 -0500202 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
204 dentry->d_name.len);
205 err = PTR_ERR(newdentry);
206 if (IS_ERR(newdentry))
207 goto out_unlock;
Al Viro32a3d842016-12-04 17:33:17 +0000208 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209 if (err)
210 goto out_dput;
211
Amir Goldstein97c684c2016-11-21 18:57:34 +0200212 if (ovl_type_merge(dentry->d_parent)) {
213 /* Setting opaque here is just an optimization, allow to fail */
214 ovl_set_opaque(dentry, newdentry);
215 }
216
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200217 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200218 newdentry = NULL;
219out_dput:
220 dput(newdentry);
221out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500222 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223 return err;
224}
225
226static int ovl_lock_rename_workdir(struct dentry *workdir,
227 struct dentry *upperdir)
228{
229 /* Workdir should not be the same as upperdir */
230 if (workdir == upperdir)
231 goto err;
232
233 /* Workdir should not be subdir of upperdir and vice versa */
234 if (lock_rename(workdir, upperdir) != NULL)
235 goto err_unlock;
236
237 return 0;
238
239err_unlock:
240 unlock_rename(workdir, upperdir);
241err:
242 pr_err("overlayfs: failed to lock workdir+upperdir\n");
243 return -EIO;
244}
245
246static struct dentry *ovl_clear_empty(struct dentry *dentry,
247 struct list_head *list)
248{
249 struct dentry *workdir = ovl_workdir(dentry);
250 struct inode *wdir = workdir->d_inode;
251 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
252 struct inode *udir = upperdir->d_inode;
253 struct path upperpath;
254 struct dentry *upper;
255 struct dentry *opaquedir;
256 struct kstat stat;
257 int err;
258
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200259 if (WARN_ON(!workdir))
260 return ERR_PTR(-EROFS);
261
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200262 err = ovl_lock_rename_workdir(workdir, upperdir);
263 if (err)
264 goto out;
265
266 ovl_path_upper(dentry, &upperpath);
267 err = vfs_getattr(&upperpath, &stat);
268 if (err)
269 goto out_unlock;
270
271 err = -ESTALE;
272 if (!S_ISDIR(stat.mode))
273 goto out_unlock;
274 upper = upperpath.dentry;
275 if (upper->d_parent->d_inode != udir)
276 goto out_unlock;
277
278 opaquedir = ovl_lookup_temp(workdir, dentry);
279 err = PTR_ERR(opaquedir);
280 if (IS_ERR(opaquedir))
281 goto out_unlock;
282
Al Viro32a3d842016-12-04 17:33:17 +0000283 err = ovl_create_real(wdir, opaquedir,
284 &(struct cattr){.mode = stat.mode}, NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285 if (err)
286 goto out_dput;
287
288 err = ovl_copy_xattr(upper, opaquedir);
289 if (err)
290 goto out_cleanup;
291
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100292 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200293 if (err)
294 goto out_cleanup;
295
Al Viro59551022016-01-22 15:40:57 -0500296 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200297 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500298 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200299 if (err)
300 goto out_cleanup;
301
302 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
303 if (err)
304 goto out_cleanup;
305
306 ovl_cleanup_whiteouts(upper, list);
307 ovl_cleanup(wdir, upper);
308 unlock_rename(workdir, upperdir);
309
310 /* dentry's upper doesn't match now, get rid of it */
311 d_drop(dentry);
312
313 return opaquedir;
314
315out_cleanup:
316 ovl_cleanup(wdir, opaquedir);
317out_dput:
318 dput(opaquedir);
319out_unlock:
320 unlock_rename(workdir, upperdir);
321out:
322 return ERR_PTR(err);
323}
324
Miklos Szeredia105d682014-11-20 16:39:59 +0100325static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200326{
327 int err;
328 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200329 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200330 LIST_HEAD(list);
331
332 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200333 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200335 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100336 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200338 /*
339 * When removing an empty opaque directory, then it makes no sense to
340 * replace it with an exact replica of itself.
341 *
342 * If no upperdentry then skip clearing whiteouts.
343 *
344 * Can race with copy-up, since we don't hold the upperdir mutex.
345 * Doesn't matter, since copy-up can't create a non-empty directory
346 * from an empty one.
347 */
348 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
349 ret = ovl_clear_empty(dentry, &list);
350
351out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200352 ovl_cache_free(&list);
353
354 return ret;
355}
356
Miklos Szeredi38b25692016-09-01 11:11:59 +0200357static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
358 const struct posix_acl *acl)
359{
360 void *buffer;
361 size_t size;
362 int err;
363
364 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
365 return 0;
366
367 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
368 buffer = kmalloc(size, GFP_KERNEL);
369 if (!buffer)
370 return -ENOMEM;
371
372 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
373 err = size;
374 if (err < 0)
375 goto out_free;
376
377 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
378out_free:
379 kfree(buffer);
380 return err;
381}
382
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200383static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000384 struct cattr *cattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385 struct dentry *hardlink)
386{
387 struct dentry *workdir = ovl_workdir(dentry);
388 struct inode *wdir = workdir->d_inode;
389 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
390 struct inode *udir = upperdir->d_inode;
391 struct dentry *upper;
392 struct dentry *newdentry;
393 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200394 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200395
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200396 if (WARN_ON(!workdir))
397 return -EROFS;
398
Miklos Szeredi38b25692016-09-01 11:11:59 +0200399 if (!hardlink) {
400 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000401 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200402 if (err)
403 return err;
404 }
405
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406 err = ovl_lock_rename_workdir(workdir, upperdir);
407 if (err)
408 goto out;
409
410 newdentry = ovl_lookup_temp(workdir, dentry);
411 err = PTR_ERR(newdentry);
412 if (IS_ERR(newdentry))
413 goto out_unlock;
414
415 upper = lookup_one_len(dentry->d_name.name, upperdir,
416 dentry->d_name.len);
417 err = PTR_ERR(upper);
418 if (IS_ERR(upper))
419 goto out_dput;
420
Al Viro32a3d842016-12-04 17:33:17 +0000421 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200422 if (err)
423 goto out_dput2;
424
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200425 /*
426 * mode could have been mutilated due to umask (e.g. sgid directory)
427 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200428 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000429 !S_ISLNK(cattr->mode) &&
430 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200431 struct iattr attr = {
432 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000433 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200434 };
435 inode_lock(newdentry->d_inode);
436 err = notify_change(newdentry, &attr, NULL);
437 inode_unlock(newdentry->d_inode);
438 if (err)
439 goto out_cleanup;
440 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200441 if (!hardlink) {
442 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
443 acl);
444 if (err)
445 goto out_cleanup;
446
447 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
448 default_acl);
449 if (err)
450 goto out_cleanup;
451 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200452
Al Viro32a3d842016-12-04 17:33:17 +0000453 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100454 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200455 if (err)
456 goto out_cleanup;
457
458 err = ovl_do_rename(wdir, newdentry, udir, upper,
459 RENAME_EXCHANGE);
460 if (err)
461 goto out_cleanup;
462
463 ovl_cleanup(wdir, upper);
464 } else {
465 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
466 if (err)
467 goto out_cleanup;
468 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200469 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200470 newdentry = NULL;
471out_dput2:
472 dput(upper);
473out_dput:
474 dput(newdentry);
475out_unlock:
476 unlock_rename(workdir, upperdir);
477out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200478 if (!hardlink) {
479 posix_acl_release(acl);
480 posix_acl_release(default_acl);
481 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482 return err;
483
484out_cleanup:
485 ovl_cleanup(wdir, newdentry);
486 goto out_dput2;
487}
488
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200489static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000490 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200491{
492 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400493 const struct cred *old_cred;
494 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200495
496 err = ovl_copy_up(dentry->d_parent);
497 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200498 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200499
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400500 old_cred = ovl_override_creds(dentry->d_sb);
501 err = -ENOMEM;
502 override_cred = prepare_creds();
503 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200504 override_cred->fsuid = inode->i_uid;
505 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400506 if (!hardlink) {
507 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000508 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400509 override_cred);
510 if (err) {
511 put_cred(override_cred);
512 goto out_revert_creds;
513 }
514 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400515 put_cred(override_creds(override_cred));
516 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200517
Miklos Szeredic412ce42016-12-16 11:02:55 +0100518 if (!ovl_dentry_is_whiteout(dentry))
Al Viro32a3d842016-12-04 17:33:17 +0000519 err = ovl_create_upper(dentry, inode, attr,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400520 hardlink);
521 else
Al Viro32a3d842016-12-04 17:33:17 +0000522 err = ovl_create_over_whiteout(dentry, inode, attr,
523 hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200524 }
Vivek Goyal26026252016-07-13 10:44:52 -0400525out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400526 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200527 if (!err) {
528 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
529
530 WARN_ON(inode->i_mode != realinode->i_mode);
531 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
532 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200533 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200534 return err;
535}
536
537static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
538 const char *link)
539{
540 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200541 struct inode *inode;
Al Viro32a3d842016-12-04 17:33:17 +0000542 struct cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200543 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000544 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200545 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200546
547 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200548 if (err)
549 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200550
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200551 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100552 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200553 if (!inode)
554 goto out_drop_write;
555
556 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000557 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200558
Al Viro32a3d842016-12-04 17:33:17 +0000559 err = ovl_create_or_link(dentry, inode, &attr, NULL);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200560 if (err)
561 iput(inode);
562
563out_drop_write:
564 ovl_drop_write(dentry);
565out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200566 return err;
567}
568
569static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
570 bool excl)
571{
572 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
573}
574
575static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
576{
577 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
578}
579
580static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
581 dev_t rdev)
582{
583 /* Don't allow creation of "whiteout" on overlay */
584 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
585 return -EPERM;
586
587 return ovl_create_object(dentry, mode, rdev, NULL);
588}
589
590static int ovl_symlink(struct inode *dir, struct dentry *dentry,
591 const char *link)
592{
593 return ovl_create_object(dentry, S_IFLNK, 0, link);
594}
595
596static int ovl_link(struct dentry *old, struct inode *newdir,
597 struct dentry *new)
598{
599 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200600 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200601
602 err = ovl_want_write(old);
603 if (err)
604 goto out;
605
606 err = ovl_copy_up(old);
607 if (err)
608 goto out_drop_write;
609
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200610 inode = d_inode(old);
611 ihold(inode);
612
Al Viro32a3d842016-12-04 17:33:17 +0000613 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200614 if (err)
615 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200616
617out_drop_write:
618 ovl_drop_write(old);
619out:
620 return err;
621}
622
Miklos Szeredia105d682014-11-20 16:39:59 +0100623static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200624{
625 struct dentry *workdir = ovl_workdir(dentry);
626 struct inode *wdir = workdir->d_inode;
627 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
628 struct inode *udir = upperdir->d_inode;
629 struct dentry *whiteout;
630 struct dentry *upper;
631 struct dentry *opaquedir = NULL;
632 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700633 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200634
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200635 if (WARN_ON(!workdir))
636 return -EROFS;
637
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200638 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200639 opaquedir = ovl_check_empty_and_clear(dentry);
640 err = PTR_ERR(opaquedir);
641 if (IS_ERR(opaquedir))
642 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200643 }
644
645 err = ovl_lock_rename_workdir(workdir, upperdir);
646 if (err)
647 goto out_dput;
648
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700649 upper = lookup_one_len(dentry->d_name.name, upperdir,
650 dentry->d_name.len);
651 err = PTR_ERR(upper);
652 if (IS_ERR(upper))
653 goto out_unlock;
654
655 err = -ESTALE;
656 if ((opaquedir && upper != opaquedir) ||
657 (!opaquedir && ovl_dentry_upper(dentry) &&
658 upper != ovl_dentry_upper(dentry))) {
659 goto out_dput_upper;
660 }
661
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200662 whiteout = ovl_whiteout(workdir, dentry);
663 err = PTR_ERR(whiteout);
664 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700665 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200666
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700667 if (d_is_dir(upper))
668 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200669
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700670 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
671 if (err)
672 goto kill_whiteout;
673 if (flags)
674 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200675
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200676 ovl_dentry_version_inc(dentry->d_parent);
677out_d_drop:
678 d_drop(dentry);
679 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700680out_dput_upper:
681 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200682out_unlock:
683 unlock_rename(workdir, upperdir);
684out_dput:
685 dput(opaquedir);
686out:
687 return err;
688
689kill_whiteout:
690 ovl_cleanup(wdir, whiteout);
691 goto out_d_drop;
692}
693
694static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
695{
696 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
697 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100698 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300699 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200700 int err;
701
Amir Goldsteind1595112016-10-26 12:34:06 +0300702 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
703 if (is_dir && ovl_dentry_get_redirect(dentry)) {
704 opaquedir = ovl_check_empty_and_clear(dentry);
705 err = PTR_ERR(opaquedir);
706 if (IS_ERR(opaquedir))
707 goto out;
708 }
709
Al Viro59551022016-01-22 15:40:57 -0500710 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100711 upper = lookup_one_len(dentry->d_name.name, upperdir,
712 dentry->d_name.len);
713 err = PTR_ERR(upper);
714 if (IS_ERR(upper))
715 goto out_unlock;
716
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200717 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300718 if ((opaquedir && upper != opaquedir) ||
719 (!opaquedir && upper != ovl_dentry_upper(dentry)))
720 goto out_dput_upper;
721
722 if (is_dir)
723 err = vfs_rmdir(dir, upper);
724 else
725 err = vfs_unlink(dir, upper, NULL);
726 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200727
728 /*
729 * Keeping this dentry hashed would mean having to release
730 * upperpath/lowerpath, which could only be done if we are the
731 * sole user of this dentry. Too tricky... Just unhash for
732 * now.
733 */
Rui Wangce9113b2016-01-08 23:09:59 +0800734 if (!err)
735 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300736out_dput_upper:
737 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100738out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500739 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300740 dput(opaquedir);
741out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200742 return err;
743}
744
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200745static int ovl_do_remove(struct dentry *dentry, bool is_dir)
746{
747 enum ovl_path_type type;
748 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400749 const struct cred *old_cred;
750
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200751 err = ovl_want_write(dentry);
752 if (err)
753 goto out;
754
755 err = ovl_copy_up(dentry->d_parent);
756 if (err)
757 goto out_drop_write;
758
759 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400760
761 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100762 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200763 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400764 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100765 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400766 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200767 if (!err) {
768 if (is_dir)
769 clear_nlink(dentry->d_inode);
770 else
771 drop_nlink(dentry->d_inode);
772 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200773out_drop_write:
774 ovl_drop_write(dentry);
775out:
776 return err;
777}
778
779static int ovl_unlink(struct inode *dir, struct dentry *dentry)
780{
781 return ovl_do_remove(dentry, false);
782}
783
784static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
785{
786 return ovl_do_remove(dentry, true);
787}
788
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100789static bool ovl_type_merge_or_lower(struct dentry *dentry)
790{
791 enum ovl_path_type type = ovl_path_type(dentry);
792
793 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
794}
795
Miklos Szeredia6c60652016-12-16 11:02:56 +0100796static bool ovl_can_move(struct dentry *dentry)
797{
798 return ovl_redirect_dir(dentry->d_sb) ||
799 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
800}
801
Miklos Szeredia6c60652016-12-16 11:02:56 +0100802static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
803{
804 char *buf, *ret;
805 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100806 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100807
808 if (samedir) {
809 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
810 GFP_KERNEL);
811 goto out;
812 }
813
814 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
815 if (!buf)
816 goto out;
817
818 buflen--;
819 buf[buflen] = '\0';
820 for (d = dget(dentry); !IS_ROOT(d);) {
821 const char *name;
822 int thislen;
823
824 spin_lock(&d->d_lock);
825 name = ovl_dentry_get_redirect(d);
826 if (name) {
827 thislen = strlen(name);
828 } else {
829 name = d->d_name.name;
830 thislen = d->d_name.len;
831 }
832
833 /* If path is too long, fall back to userspace move */
834 if (thislen + (name[0] != '/') > buflen) {
835 ret = ERR_PTR(-EXDEV);
836 spin_unlock(&d->d_lock);
837 goto out_put;
838 }
839
840 buflen -= thislen;
841 memcpy(&buf[buflen], name, thislen);
842 tmp = dget_dlock(d->d_parent);
843 spin_unlock(&d->d_lock);
844
845 dput(d);
846 d = tmp;
847
848 /* Absolute redirect: finished */
849 if (buf[buflen] == '/')
850 break;
851 buflen--;
852 buf[buflen] = '/';
853 }
854 ret = kstrdup(&buf[buflen], GFP_KERNEL);
855out_put:
856 dput(d);
857 kfree(buf);
858out:
859 return ret ? ret : ERR_PTR(-ENOMEM);
860}
861
862static int ovl_set_redirect(struct dentry *dentry, bool samedir)
863{
864 int err;
865 const char *redirect = ovl_dentry_get_redirect(dentry);
866
867 if (redirect && (samedir || redirect[0] == '/'))
868 return 0;
869
870 redirect = ovl_get_redirect(dentry, samedir);
871 if (IS_ERR(redirect))
872 return PTR_ERR(redirect);
873
874 err = ovl_do_setxattr(ovl_dentry_upper(dentry), OVL_XATTR_REDIRECT,
875 redirect, strlen(redirect), 0);
876 if (!err) {
877 spin_lock(&dentry->d_lock);
878 ovl_dentry_set_redirect(dentry, redirect);
879 spin_unlock(&dentry->d_lock);
880 } else {
881 kfree(redirect);
882 if (err == -EOPNOTSUPP)
883 ovl_clear_redirect_dir(dentry->d_sb);
884 else
885 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
886 /* Fall back to userspace copy-up */
887 err = -EXDEV;
888 }
889 return err;
890}
891
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100892static int ovl_rename(struct inode *olddir, struct dentry *old,
893 struct inode *newdir, struct dentry *new,
894 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200895{
896 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200897 struct dentry *old_upperdir;
898 struct dentry *new_upperdir;
899 struct dentry *olddentry;
900 struct dentry *newdentry;
901 struct dentry *trap;
902 bool old_opaque;
903 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200904 bool cleanup_whiteout = false;
905 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000906 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100907 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100908 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200909 struct dentry *opaquedir = NULL;
910 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200911
912 err = -EINVAL;
913 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
914 goto out;
915
916 flags &= ~RENAME_NOREPLACE;
917
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200918 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200919 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100920 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200921 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100922 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100923 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200924
925 err = ovl_want_write(old);
926 if (err)
927 goto out;
928
929 err = ovl_copy_up(old);
930 if (err)
931 goto out_drop_write;
932
933 err = ovl_copy_up(new->d_parent);
934 if (err)
935 goto out_drop_write;
936 if (!overwrite) {
937 err = ovl_copy_up(new);
938 if (err)
939 goto out_drop_write;
940 }
941
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400942 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200943
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100944 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100945 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200946 err = PTR_ERR(opaquedir);
947 if (IS_ERR(opaquedir)) {
948 opaquedir = NULL;
949 goto out_revert_creds;
950 }
951 }
952
953 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100954 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100955 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200956 /* Whiteout source */
957 flags |= RENAME_WHITEOUT;
958 } else {
959 /* Switch whiteouts */
960 flags |= RENAME_EXCHANGE;
961 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100962 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200963 flags |= RENAME_EXCHANGE;
964 cleanup_whiteout = true;
965 }
966 }
967
968 old_upperdir = ovl_dentry_upper(old->d_parent);
969 new_upperdir = ovl_dentry_upper(new->d_parent);
970
971 trap = lock_rename(new_upperdir, old_upperdir);
972
Miklos Szeredi11f37102016-03-21 17:31:44 +0100973 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
974 old->d_name.len);
975 err = PTR_ERR(olddentry);
976 if (IS_ERR(olddentry))
977 goto out_unlock;
978
979 err = -ESTALE;
980 if (olddentry != ovl_dentry_upper(old))
981 goto out_dput_old;
982
983 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
984 new->d_name.len);
985 err = PTR_ERR(newdentry);
986 if (IS_ERR(newdentry))
987 goto out_dput_old;
988
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100989 old_opaque = ovl_dentry_is_opaque(old);
990 new_opaque = ovl_dentry_is_opaque(new);
991
Miklos Szeredi11f37102016-03-21 17:31:44 +0100992 err = -ESTALE;
993 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200994 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100995 if (newdentry != opaquedir)
996 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200997 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100998 if (newdentry != ovl_dentry_upper(new))
999 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001000 }
1001 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001002 if (!d_is_negative(newdentry) &&
1003 (!new_opaque || !ovl_is_whiteout(newdentry)))
1004 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001005 }
1006
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001007 if (olddentry == trap)
1008 goto out_dput;
1009 if (newdentry == trap)
1010 goto out_dput;
1011
Miklos Szeredi804032f2016-12-16 11:02:55 +01001012 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1013 goto out_dput;
1014
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001015 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001016 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001017 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001018 err = ovl_set_redirect(old, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001019 else if (!old_opaque && ovl_type_merge(new->d_parent))
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001020 err = ovl_set_opaque(old, olddentry);
1021 if (err)
1022 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001024 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001025 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001026 err = ovl_set_redirect(new, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001027 else if (!new_opaque && ovl_type_merge(old->d_parent))
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001028 err = ovl_set_opaque(new, newdentry);
1029 if (err)
1030 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001031 }
1032
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001033 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1034 new_upperdir->d_inode, newdentry, flags);
1035 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001036 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001037
1038 if (cleanup_whiteout)
1039 ovl_cleanup(old_upperdir->d_inode, newdentry);
1040
1041 ovl_dentry_version_inc(old->d_parent);
1042 ovl_dentry_version_inc(new->d_parent);
1043
1044out_dput:
1045 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001046out_dput_old:
1047 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001048out_unlock:
1049 unlock_rename(new_upperdir, old_upperdir);
1050out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001051 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001052out_drop_write:
1053 ovl_drop_write(old);
1054out:
1055 dput(opaquedir);
1056 return err;
1057}
1058
1059const struct inode_operations ovl_dir_inode_operations = {
1060 .lookup = ovl_lookup,
1061 .mkdir = ovl_mkdir,
1062 .symlink = ovl_symlink,
1063 .unlink = ovl_unlink,
1064 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001065 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001066 .link = ovl_link,
1067 .setattr = ovl_setattr,
1068 .create = ovl_create,
1069 .mknod = ovl_mknod,
1070 .permission = ovl_permission,
1071 .getattr = ovl_dir_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001072 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001073 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001074 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001075};