blob: 723b98b9069876d1656b74735dabcaf01484e26d [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
Vivek Goyal72e48482016-06-16 10:09:14 -0400141/* Common operations required to be done after creation of file on upper */
142static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200143 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400144{
145 ovl_dentry_version_inc(dentry->d_parent);
146 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200147 if (!hardlink) {
148 ovl_inode_update(inode, d_inode(newdentry));
149 ovl_copyattr(newdentry->d_inode, inode);
150 } else {
151 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
152 inc_nlink(inode);
153 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400154 d_instantiate(dentry, inode);
Amir Goldstein5b6c9052017-04-24 19:54:13 +0300155 /* Force lookup of new upper hardlink to find its lower */
156 if (hardlink)
157 d_drop(dentry);
Vivek Goyal72e48482016-06-16 10:09:14 -0400158}
159
Amir Goldstein97c684c2016-11-21 18:57:34 +0200160static bool ovl_type_merge(struct dentry *dentry)
161{
162 return OVL_TYPE_MERGE(ovl_path_type(dentry));
163}
164
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200165static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000166 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200167{
168 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
169 struct inode *udir = upperdir->d_inode;
170 struct dentry *newdentry;
171 int err;
172
Miklos Szeredi38b25692016-09-01 11:11:59 +0200173 if (!hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000174 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200175
Al Viro59551022016-01-22 15:40:57 -0500176 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200177 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
178 dentry->d_name.len);
179 err = PTR_ERR(newdentry);
180 if (IS_ERR(newdentry))
181 goto out_unlock;
Al Viro32a3d842016-12-04 17:33:17 +0000182 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200183 if (err)
184 goto out_dput;
185
Amir Goldstein4a99f3c2017-04-24 22:26:40 +0300186 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
Amir Goldstein97c684c2016-11-21 18:57:34 +0200187 /* Setting opaque here is just an optimization, allow to fail */
188 ovl_set_opaque(dentry, newdentry);
189 }
190
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200191 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200192 newdentry = NULL;
193out_dput:
194 dput(newdentry);
195out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500196 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197 return err;
198}
199
200static int ovl_lock_rename_workdir(struct dentry *workdir,
201 struct dentry *upperdir)
202{
203 /* Workdir should not be the same as upperdir */
204 if (workdir == upperdir)
205 goto err;
206
207 /* Workdir should not be subdir of upperdir and vice versa */
208 if (lock_rename(workdir, upperdir) != NULL)
209 goto err_unlock;
210
211 return 0;
212
213err_unlock:
214 unlock_rename(workdir, upperdir);
215err:
216 pr_err("overlayfs: failed to lock workdir+upperdir\n");
217 return -EIO;
218}
219
220static struct dentry *ovl_clear_empty(struct dentry *dentry,
221 struct list_head *list)
222{
223 struct dentry *workdir = ovl_workdir(dentry);
224 struct inode *wdir = workdir->d_inode;
225 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
226 struct inode *udir = upperdir->d_inode;
227 struct path upperpath;
228 struct dentry *upper;
229 struct dentry *opaquedir;
230 struct kstat stat;
231 int err;
232
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200233 if (WARN_ON(!workdir))
234 return ERR_PTR(-EROFS);
235
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236 err = ovl_lock_rename_workdir(workdir, upperdir);
237 if (err)
238 goto out;
239
240 ovl_path_upper(dentry, &upperpath);
David Howellsa528d352017-01-31 16:46:22 +0000241 err = vfs_getattr(&upperpath, &stat,
242 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200243 if (err)
244 goto out_unlock;
245
246 err = -ESTALE;
247 if (!S_ISDIR(stat.mode))
248 goto out_unlock;
249 upper = upperpath.dentry;
250 if (upper->d_parent->d_inode != udir)
251 goto out_unlock;
252
253 opaquedir = ovl_lookup_temp(workdir, dentry);
254 err = PTR_ERR(opaquedir);
255 if (IS_ERR(opaquedir))
256 goto out_unlock;
257
Al Viro32a3d842016-12-04 17:33:17 +0000258 err = ovl_create_real(wdir, opaquedir,
259 &(struct cattr){.mode = stat.mode}, NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200260 if (err)
261 goto out_dput;
262
263 err = ovl_copy_xattr(upper, opaquedir);
264 if (err)
265 goto out_cleanup;
266
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100267 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268 if (err)
269 goto out_cleanup;
270
Al Viro59551022016-01-22 15:40:57 -0500271 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500273 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200274 if (err)
275 goto out_cleanup;
276
277 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
278 if (err)
279 goto out_cleanup;
280
281 ovl_cleanup_whiteouts(upper, list);
282 ovl_cleanup(wdir, upper);
283 unlock_rename(workdir, upperdir);
284
285 /* dentry's upper doesn't match now, get rid of it */
286 d_drop(dentry);
287
288 return opaquedir;
289
290out_cleanup:
291 ovl_cleanup(wdir, opaquedir);
292out_dput:
293 dput(opaquedir);
294out_unlock:
295 unlock_rename(workdir, upperdir);
296out:
297 return ERR_PTR(err);
298}
299
Miklos Szeredia105d682014-11-20 16:39:59 +0100300static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301{
302 int err;
303 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200304 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 LIST_HEAD(list);
306
307 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200308 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200309 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200310 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100311 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200312
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200313 /*
314 * When removing an empty opaque directory, then it makes no sense to
315 * replace it with an exact replica of itself.
316 *
317 * If no upperdentry then skip clearing whiteouts.
318 *
319 * Can race with copy-up, since we don't hold the upperdir mutex.
320 * Doesn't matter, since copy-up can't create a non-empty directory
321 * from an empty one.
322 */
323 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
324 ret = ovl_clear_empty(dentry, &list);
325
326out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200327 ovl_cache_free(&list);
328
329 return ret;
330}
331
Miklos Szeredi38b25692016-09-01 11:11:59 +0200332static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
333 const struct posix_acl *acl)
334{
335 void *buffer;
336 size_t size;
337 int err;
338
339 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
340 return 0;
341
342 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
343 buffer = kmalloc(size, GFP_KERNEL);
344 if (!buffer)
345 return -ENOMEM;
346
347 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
348 err = size;
349 if (err < 0)
350 goto out_free;
351
352 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
353out_free:
354 kfree(buffer);
355 return err;
356}
357
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200358static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000359 struct cattr *cattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360 struct dentry *hardlink)
361{
362 struct dentry *workdir = ovl_workdir(dentry);
363 struct inode *wdir = workdir->d_inode;
364 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
365 struct inode *udir = upperdir->d_inode;
366 struct dentry *upper;
367 struct dentry *newdentry;
368 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200369 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200370
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200371 if (WARN_ON(!workdir))
372 return -EROFS;
373
Miklos Szeredi38b25692016-09-01 11:11:59 +0200374 if (!hardlink) {
375 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000376 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200377 if (err)
378 return err;
379 }
380
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381 err = ovl_lock_rename_workdir(workdir, upperdir);
382 if (err)
383 goto out;
384
385 newdentry = ovl_lookup_temp(workdir, dentry);
386 err = PTR_ERR(newdentry);
387 if (IS_ERR(newdentry))
388 goto out_unlock;
389
390 upper = lookup_one_len(dentry->d_name.name, upperdir,
391 dentry->d_name.len);
392 err = PTR_ERR(upper);
393 if (IS_ERR(upper))
394 goto out_dput;
395
Al Viro32a3d842016-12-04 17:33:17 +0000396 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397 if (err)
398 goto out_dput2;
399
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200400 /*
401 * mode could have been mutilated due to umask (e.g. sgid directory)
402 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200403 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000404 !S_ISLNK(cattr->mode) &&
405 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200406 struct iattr attr = {
407 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000408 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200409 };
410 inode_lock(newdentry->d_inode);
411 err = notify_change(newdentry, &attr, NULL);
412 inode_unlock(newdentry->d_inode);
413 if (err)
414 goto out_cleanup;
415 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200416 if (!hardlink) {
417 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
418 acl);
419 if (err)
420 goto out_cleanup;
421
422 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
423 default_acl);
424 if (err)
425 goto out_cleanup;
426 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200427
Al Viro32a3d842016-12-04 17:33:17 +0000428 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100429 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430 if (err)
431 goto out_cleanup;
432
433 err = ovl_do_rename(wdir, newdentry, udir, upper,
434 RENAME_EXCHANGE);
435 if (err)
436 goto out_cleanup;
437
438 ovl_cleanup(wdir, upper);
439 } else {
440 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
441 if (err)
442 goto out_cleanup;
443 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200444 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 newdentry = NULL;
446out_dput2:
447 dput(upper);
448out_dput:
449 dput(newdentry);
450out_unlock:
451 unlock_rename(workdir, upperdir);
452out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200453 if (!hardlink) {
454 posix_acl_release(acl);
455 posix_acl_release(default_acl);
456 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200457 return err;
458
459out_cleanup:
460 ovl_cleanup(wdir, newdentry);
461 goto out_dput2;
462}
463
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200464static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000465 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200466{
467 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400468 const struct cred *old_cred;
469 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200470
471 err = ovl_copy_up(dentry->d_parent);
472 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200473 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200474
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400475 old_cred = ovl_override_creds(dentry->d_sb);
476 err = -ENOMEM;
477 override_cred = prepare_creds();
478 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200479 override_cred->fsuid = inode->i_uid;
480 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400481 if (!hardlink) {
482 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000483 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400484 override_cred);
485 if (err) {
486 put_cred(override_cred);
487 goto out_revert_creds;
488 }
489 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400490 put_cred(override_creds(override_cred));
491 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200492
Miklos Szeredic412ce42016-12-16 11:02:55 +0100493 if (!ovl_dentry_is_whiteout(dentry))
Al Viro32a3d842016-12-04 17:33:17 +0000494 err = ovl_create_upper(dentry, inode, attr,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400495 hardlink);
496 else
Al Viro32a3d842016-12-04 17:33:17 +0000497 err = ovl_create_over_whiteout(dentry, inode, attr,
498 hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200499 }
Vivek Goyal26026252016-07-13 10:44:52 -0400500out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400501 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200502 if (!err) {
503 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
504
505 WARN_ON(inode->i_mode != realinode->i_mode);
506 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
507 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200508 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200509 return err;
510}
511
512static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
513 const char *link)
514{
515 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200516 struct inode *inode;
Al Viro32a3d842016-12-04 17:33:17 +0000517 struct cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200518 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000519 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200520 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200521
522 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200523 if (err)
524 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200525
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200526 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100527 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200528 if (!inode)
529 goto out_drop_write;
530
531 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000532 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200533
Al Viro32a3d842016-12-04 17:33:17 +0000534 err = ovl_create_or_link(dentry, inode, &attr, NULL);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200535 if (err)
536 iput(inode);
537
538out_drop_write:
539 ovl_drop_write(dentry);
540out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200541 return err;
542}
543
544static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
545 bool excl)
546{
547 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
548}
549
550static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
551{
552 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
553}
554
555static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
556 dev_t rdev)
557{
558 /* Don't allow creation of "whiteout" on overlay */
559 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
560 return -EPERM;
561
562 return ovl_create_object(dentry, mode, rdev, NULL);
563}
564
565static int ovl_symlink(struct inode *dir, struct dentry *dentry,
566 const char *link)
567{
568 return ovl_create_object(dentry, S_IFLNK, 0, link);
569}
570
571static int ovl_link(struct dentry *old, struct inode *newdir,
572 struct dentry *new)
573{
574 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200575 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200576
577 err = ovl_want_write(old);
578 if (err)
579 goto out;
580
581 err = ovl_copy_up(old);
582 if (err)
583 goto out_drop_write;
584
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200585 inode = d_inode(old);
586 ihold(inode);
587
Al Viro32a3d842016-12-04 17:33:17 +0000588 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200589 if (err)
590 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200591
592out_drop_write:
593 ovl_drop_write(old);
594out:
595 return err;
596}
597
Miklos Szeredia105d682014-11-20 16:39:59 +0100598static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200599{
600 struct dentry *workdir = ovl_workdir(dentry);
601 struct inode *wdir = workdir->d_inode;
602 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
603 struct inode *udir = upperdir->d_inode;
604 struct dentry *whiteout;
605 struct dentry *upper;
606 struct dentry *opaquedir = NULL;
607 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700608 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200609
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200610 if (WARN_ON(!workdir))
611 return -EROFS;
612
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200613 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200614 opaquedir = ovl_check_empty_and_clear(dentry);
615 err = PTR_ERR(opaquedir);
616 if (IS_ERR(opaquedir))
617 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200618 }
619
620 err = ovl_lock_rename_workdir(workdir, upperdir);
621 if (err)
622 goto out_dput;
623
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700624 upper = lookup_one_len(dentry->d_name.name, upperdir,
625 dentry->d_name.len);
626 err = PTR_ERR(upper);
627 if (IS_ERR(upper))
628 goto out_unlock;
629
630 err = -ESTALE;
631 if ((opaquedir && upper != opaquedir) ||
632 (!opaquedir && ovl_dentry_upper(dentry) &&
633 upper != ovl_dentry_upper(dentry))) {
634 goto out_dput_upper;
635 }
636
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200637 whiteout = ovl_whiteout(workdir, dentry);
638 err = PTR_ERR(whiteout);
639 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700640 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200641
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700642 if (d_is_dir(upper))
643 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200644
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700645 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
646 if (err)
647 goto kill_whiteout;
648 if (flags)
649 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200650
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200651 ovl_dentry_version_inc(dentry->d_parent);
652out_d_drop:
653 d_drop(dentry);
654 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700655out_dput_upper:
656 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200657out_unlock:
658 unlock_rename(workdir, upperdir);
659out_dput:
660 dput(opaquedir);
661out:
662 return err;
663
664kill_whiteout:
665 ovl_cleanup(wdir, whiteout);
666 goto out_d_drop;
667}
668
669static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
670{
671 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
672 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100673 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300674 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200675 int err;
676
Amir Goldsteind1595112016-10-26 12:34:06 +0300677 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
678 if (is_dir && ovl_dentry_get_redirect(dentry)) {
679 opaquedir = ovl_check_empty_and_clear(dentry);
680 err = PTR_ERR(opaquedir);
681 if (IS_ERR(opaquedir))
682 goto out;
683 }
684
Al Viro59551022016-01-22 15:40:57 -0500685 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100686 upper = lookup_one_len(dentry->d_name.name, upperdir,
687 dentry->d_name.len);
688 err = PTR_ERR(upper);
689 if (IS_ERR(upper))
690 goto out_unlock;
691
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200692 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300693 if ((opaquedir && upper != opaquedir) ||
694 (!opaquedir && upper != ovl_dentry_upper(dentry)))
695 goto out_dput_upper;
696
697 if (is_dir)
698 err = vfs_rmdir(dir, upper);
699 else
700 err = vfs_unlink(dir, upper, NULL);
701 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200702
703 /*
704 * Keeping this dentry hashed would mean having to release
705 * upperpath/lowerpath, which could only be done if we are the
706 * sole user of this dentry. Too tricky... Just unhash for
707 * now.
708 */
Rui Wangce9113b2016-01-08 23:09:59 +0800709 if (!err)
710 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300711out_dput_upper:
712 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100713out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500714 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300715 dput(opaquedir);
716out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200717 return err;
718}
719
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200720static int ovl_do_remove(struct dentry *dentry, bool is_dir)
721{
722 enum ovl_path_type type;
723 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400724 const struct cred *old_cred;
725
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200726 err = ovl_want_write(dentry);
727 if (err)
728 goto out;
729
730 err = ovl_copy_up(dentry->d_parent);
731 if (err)
732 goto out_drop_write;
733
734 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400735
736 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100737 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200738 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400739 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100740 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400741 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200742 if (!err) {
743 if (is_dir)
744 clear_nlink(dentry->d_inode);
745 else
746 drop_nlink(dentry->d_inode);
747 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200748out_drop_write:
749 ovl_drop_write(dentry);
750out:
751 return err;
752}
753
754static int ovl_unlink(struct inode *dir, struct dentry *dentry)
755{
756 return ovl_do_remove(dentry, false);
757}
758
759static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
760{
761 return ovl_do_remove(dentry, true);
762}
763
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100764static bool ovl_type_merge_or_lower(struct dentry *dentry)
765{
766 enum ovl_path_type type = ovl_path_type(dentry);
767
768 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
769}
770
Miklos Szeredia6c60652016-12-16 11:02:56 +0100771static bool ovl_can_move(struct dentry *dentry)
772{
773 return ovl_redirect_dir(dentry->d_sb) ||
774 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
775}
776
Miklos Szeredia6c60652016-12-16 11:02:56 +0100777static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
778{
779 char *buf, *ret;
780 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100781 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100782
783 if (samedir) {
784 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
785 GFP_KERNEL);
786 goto out;
787 }
788
789 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
790 if (!buf)
791 goto out;
792
793 buflen--;
794 buf[buflen] = '\0';
795 for (d = dget(dentry); !IS_ROOT(d);) {
796 const char *name;
797 int thislen;
798
799 spin_lock(&d->d_lock);
800 name = ovl_dentry_get_redirect(d);
801 if (name) {
802 thislen = strlen(name);
803 } else {
804 name = d->d_name.name;
805 thislen = d->d_name.len;
806 }
807
808 /* If path is too long, fall back to userspace move */
809 if (thislen + (name[0] != '/') > buflen) {
810 ret = ERR_PTR(-EXDEV);
811 spin_unlock(&d->d_lock);
812 goto out_put;
813 }
814
815 buflen -= thislen;
816 memcpy(&buf[buflen], name, thislen);
817 tmp = dget_dlock(d->d_parent);
818 spin_unlock(&d->d_lock);
819
820 dput(d);
821 d = tmp;
822
823 /* Absolute redirect: finished */
824 if (buf[buflen] == '/')
825 break;
826 buflen--;
827 buf[buflen] = '/';
828 }
829 ret = kstrdup(&buf[buflen], GFP_KERNEL);
830out_put:
831 dput(d);
832 kfree(buf);
833out:
834 return ret ? ret : ERR_PTR(-ENOMEM);
835}
836
837static int ovl_set_redirect(struct dentry *dentry, bool samedir)
838{
839 int err;
840 const char *redirect = ovl_dentry_get_redirect(dentry);
841
842 if (redirect && (samedir || redirect[0] == '/'))
843 return 0;
844
845 redirect = ovl_get_redirect(dentry, samedir);
846 if (IS_ERR(redirect))
847 return PTR_ERR(redirect);
848
849 err = ovl_do_setxattr(ovl_dentry_upper(dentry), OVL_XATTR_REDIRECT,
850 redirect, strlen(redirect), 0);
851 if (!err) {
852 spin_lock(&dentry->d_lock);
853 ovl_dentry_set_redirect(dentry, redirect);
854 spin_unlock(&dentry->d_lock);
855 } else {
856 kfree(redirect);
857 if (err == -EOPNOTSUPP)
858 ovl_clear_redirect_dir(dentry->d_sb);
859 else
860 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
861 /* Fall back to userspace copy-up */
862 err = -EXDEV;
863 }
864 return err;
865}
866
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100867static int ovl_rename(struct inode *olddir, struct dentry *old,
868 struct inode *newdir, struct dentry *new,
869 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200870{
871 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200872 struct dentry *old_upperdir;
873 struct dentry *new_upperdir;
874 struct dentry *olddentry;
875 struct dentry *newdentry;
876 struct dentry *trap;
877 bool old_opaque;
878 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200879 bool cleanup_whiteout = false;
880 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000881 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100882 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100883 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200884 struct dentry *opaquedir = NULL;
885 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200886
887 err = -EINVAL;
888 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
889 goto out;
890
891 flags &= ~RENAME_NOREPLACE;
892
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200893 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200894 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100895 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200896 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100897 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100898 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200899
900 err = ovl_want_write(old);
901 if (err)
902 goto out;
903
904 err = ovl_copy_up(old);
905 if (err)
906 goto out_drop_write;
907
908 err = ovl_copy_up(new->d_parent);
909 if (err)
910 goto out_drop_write;
911 if (!overwrite) {
912 err = ovl_copy_up(new);
913 if (err)
914 goto out_drop_write;
915 }
916
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400917 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200918
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100919 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100920 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200921 err = PTR_ERR(opaquedir);
922 if (IS_ERR(opaquedir)) {
923 opaquedir = NULL;
924 goto out_revert_creds;
925 }
926 }
927
928 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100929 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100930 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200931 /* Whiteout source */
932 flags |= RENAME_WHITEOUT;
933 } else {
934 /* Switch whiteouts */
935 flags |= RENAME_EXCHANGE;
936 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100937 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200938 flags |= RENAME_EXCHANGE;
939 cleanup_whiteout = true;
940 }
941 }
942
943 old_upperdir = ovl_dentry_upper(old->d_parent);
944 new_upperdir = ovl_dentry_upper(new->d_parent);
945
946 trap = lock_rename(new_upperdir, old_upperdir);
947
Miklos Szeredi11f37102016-03-21 17:31:44 +0100948 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
949 old->d_name.len);
950 err = PTR_ERR(olddentry);
951 if (IS_ERR(olddentry))
952 goto out_unlock;
953
954 err = -ESTALE;
955 if (olddentry != ovl_dentry_upper(old))
956 goto out_dput_old;
957
958 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
959 new->d_name.len);
960 err = PTR_ERR(newdentry);
961 if (IS_ERR(newdentry))
962 goto out_dput_old;
963
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100964 old_opaque = ovl_dentry_is_opaque(old);
965 new_opaque = ovl_dentry_is_opaque(new);
966
Miklos Szeredi11f37102016-03-21 17:31:44 +0100967 err = -ESTALE;
968 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200969 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100970 if (newdentry != opaquedir)
971 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200972 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100973 if (newdentry != ovl_dentry_upper(new))
974 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200975 }
976 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100977 if (!d_is_negative(newdentry) &&
978 (!new_opaque || !ovl_is_whiteout(newdentry)))
979 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200980 }
981
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200982 if (olddentry == trap)
983 goto out_dput;
984 if (newdentry == trap)
985 goto out_dput;
986
Miklos Szeredi804032f2016-12-16 11:02:55 +0100987 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
988 goto out_dput;
989
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100990 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100991 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100992 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +0100993 err = ovl_set_redirect(old, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +0200994 else if (!old_opaque && ovl_type_merge(new->d_parent))
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100995 err = ovl_set_opaque(old, olddentry);
996 if (err)
997 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200998 }
Miklos Szeredia6c60652016-12-16 11:02:56 +0100999 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001000 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001001 err = ovl_set_redirect(new, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001002 else if (!new_opaque && ovl_type_merge(old->d_parent))
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001003 err = ovl_set_opaque(new, newdentry);
1004 if (err)
1005 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001006 }
1007
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001008 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1009 new_upperdir->d_inode, newdentry, flags);
1010 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001011 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001012
1013 if (cleanup_whiteout)
1014 ovl_cleanup(old_upperdir->d_inode, newdentry);
1015
1016 ovl_dentry_version_inc(old->d_parent);
1017 ovl_dentry_version_inc(new->d_parent);
1018
1019out_dput:
1020 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001021out_dput_old:
1022 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023out_unlock:
1024 unlock_rename(new_upperdir, old_upperdir);
1025out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001026 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001027out_drop_write:
1028 ovl_drop_write(old);
1029out:
1030 dput(opaquedir);
1031 return err;
1032}
1033
1034const struct inode_operations ovl_dir_inode_operations = {
1035 .lookup = ovl_lookup,
1036 .mkdir = ovl_mkdir,
1037 .symlink = ovl_symlink,
1038 .unlink = ovl_unlink,
1039 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001040 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001041 .link = ovl_link,
1042 .setattr = ovl_setattr,
1043 .create = ovl_create,
1044 .mknod = ovl_mknod,
1045 .permission = ovl_permission,
Miklos Szeredi5b712092017-05-05 11:38:58 +02001046 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001047 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001048 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001049 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001050};