blob: 369ee7c4cdc0bf0f7cf061a15dea476b3bc760c5 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
11#include <linux/namei.h>
12#include <linux/xattr.h>
13#include <linux/security.h>
14#include <linux/cred.h>
Miklos Szeredi3ea22a72016-12-16 11:02:57 +010015#include <linux/module.h>
Miklos Szeredi38b25692016-09-01 11:11:59 +020016#include <linux/posix_acl.h>
17#include <linux/posix_acl_xattr.h>
Richard Weinberger6a45b362016-09-16 11:45:24 +020018#include <linux/atomic.h>
Miklos Szeredia6c60652016-12-16 11:02:56 +010019#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include "overlayfs.h"
21
Miklos Szeredi3ea22a72016-12-16 11:02:57 +010022static unsigned short ovl_redirect_max = 256;
23module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
24MODULE_PARM_DESC(ovl_redirect_max,
25 "Maximum length of absolute redirect xattr value");
26
Miklos Szeredie9be9d52014-10-24 00:14:38 +020027void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
28{
29 int err;
30
31 dget(wdentry);
David Howellse36cb0b2015-01-29 12:02:35 +000032 if (d_is_dir(wdentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020033 err = ovl_do_rmdir(wdir, wdentry);
34 else
35 err = ovl_do_unlink(wdir, wdentry);
36 dput(wdentry);
37
38 if (err) {
39 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
40 wdentry, err);
41 }
42}
43
Miklos Szeredi3d275732017-05-19 09:33:49 +020044struct dentry *ovl_lookup_temp(struct dentry *workdir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020045{
46 struct dentry *temp;
47 char name[20];
Richard Weinberger6a45b362016-09-16 11:45:24 +020048 static atomic_t temp_id = ATOMIC_INIT(0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049
Richard Weinberger6a45b362016-09-16 11:45:24 +020050 /* counter is allowed to wrap, since temp dentries are ephemeral */
51 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
Miklos Szeredie9be9d52014-10-24 00:14:38 +020052
53 temp = lookup_one_len(name, workdir, strlen(name));
54 if (!IS_ERR(temp) && temp->d_inode) {
55 pr_err("overlayfs: workdir/%s already exists\n", name);
56 dput(temp);
57 temp = ERR_PTR(-EIO);
58 }
59
60 return temp;
61}
62
63/* caller holds i_mutex on workdir */
64static struct dentry *ovl_whiteout(struct dentry *workdir,
65 struct dentry *dentry)
66{
67 int err;
68 struct dentry *whiteout;
69 struct inode *wdir = workdir->d_inode;
70
Miklos Szeredi3d275732017-05-19 09:33:49 +020071 whiteout = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020072 if (IS_ERR(whiteout))
73 return whiteout;
74
75 err = ovl_do_whiteout(wdir, whiteout);
76 if (err) {
77 dput(whiteout);
78 whiteout = ERR_PTR(err);
79 }
80
81 return whiteout;
82}
83
84int ovl_create_real(struct inode *dir, struct dentry *newdentry,
Al Viro32a3d842016-12-04 17:33:17 +000085 struct cattr *attr, struct dentry *hardlink, bool debug)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020086{
87 int err;
88
89 if (newdentry->d_inode)
90 return -ESTALE;
91
92 if (hardlink) {
93 err = ovl_do_link(hardlink, dir, newdentry, debug);
94 } else {
Al Viro32a3d842016-12-04 17:33:17 +000095 switch (attr->mode & S_IFMT) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020096 case S_IFREG:
Al Viro32a3d842016-12-04 17:33:17 +000097 err = ovl_do_create(dir, newdentry, attr->mode, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020098 break;
99
100 case S_IFDIR:
Al Viro32a3d842016-12-04 17:33:17 +0000101 err = ovl_do_mkdir(dir, newdentry, attr->mode, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200102 break;
103
104 case S_IFCHR:
105 case S_IFBLK:
106 case S_IFIFO:
107 case S_IFSOCK:
108 err = ovl_do_mknod(dir, newdentry,
Al Viro32a3d842016-12-04 17:33:17 +0000109 attr->mode, attr->rdev, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110 break;
111
112 case S_IFLNK:
Al Viro32a3d842016-12-04 17:33:17 +0000113 err = ovl_do_symlink(dir, newdentry, attr->link, debug);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200114 break;
115
116 default:
117 err = -EPERM;
118 }
119 }
120 if (!err && WARN_ON(!newdentry->d_inode)) {
121 /*
122 * Not quite sure if non-instantiated dentry is legal or not.
123 * VFS doesn't seem to care so check and warn here.
124 */
125 err = -ENOENT;
126 }
127 return err;
128}
129
Amir Goldstein21a22872017-05-17 00:12:41 +0300130static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
131 int xerr)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132{
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100133 int err;
134
Amir Goldstein21a22872017-05-17 00:12:41 +0300135 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100136 if (!err)
137 ovl_dentry_set_opaque(dentry);
138
139 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140}
141
Amir Goldstein21a22872017-05-17 00:12:41 +0300142static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
143{
144 /*
145 * Fail with -EIO when trying to create opaque dir and upper doesn't
146 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
147 * return a specific error for noxattr case.
148 */
149 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
150}
151
Vivek Goyal72e48482016-06-16 10:09:14 -0400152/* Common operations required to be done after creation of file on upper */
153static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200154 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400155{
156 ovl_dentry_version_inc(dentry->d_parent);
157 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200158 if (!hardlink) {
159 ovl_inode_update(inode, d_inode(newdentry));
160 ovl_copyattr(newdentry->d_inode, inode);
161 } else {
162 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
163 inc_nlink(inode);
164 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400165 d_instantiate(dentry, inode);
Amir Goldstein5b6c9052017-04-24 19:54:13 +0300166 /* Force lookup of new upper hardlink to find its lower */
167 if (hardlink)
168 d_drop(dentry);
Vivek Goyal72e48482016-06-16 10:09:14 -0400169}
170
Amir Goldstein97c684c2016-11-21 18:57:34 +0200171static bool ovl_type_merge(struct dentry *dentry)
172{
173 return OVL_TYPE_MERGE(ovl_path_type(dentry));
174}
175
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200176static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000177 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200178{
179 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
180 struct inode *udir = upperdir->d_inode;
181 struct dentry *newdentry;
182 int err;
183
Miklos Szeredi38b25692016-09-01 11:11:59 +0200184 if (!hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000185 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200186
Al Viro59551022016-01-22 15:40:57 -0500187 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200188 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
189 dentry->d_name.len);
190 err = PTR_ERR(newdentry);
191 if (IS_ERR(newdentry))
192 goto out_unlock;
Al Viro32a3d842016-12-04 17:33:17 +0000193 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200194 if (err)
195 goto out_dput;
196
Amir Goldstein4a99f3c2017-04-24 22:26:40 +0300197 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
Amir Goldstein97c684c2016-11-21 18:57:34 +0200198 /* Setting opaque here is just an optimization, allow to fail */
199 ovl_set_opaque(dentry, newdentry);
200 }
201
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200202 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203 newdentry = NULL;
204out_dput:
205 dput(newdentry);
206out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500207 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200208 return err;
209}
210
211static int ovl_lock_rename_workdir(struct dentry *workdir,
212 struct dentry *upperdir)
213{
214 /* Workdir should not be the same as upperdir */
215 if (workdir == upperdir)
216 goto err;
217
218 /* Workdir should not be subdir of upperdir and vice versa */
219 if (lock_rename(workdir, upperdir) != NULL)
220 goto err_unlock;
221
222 return 0;
223
224err_unlock:
225 unlock_rename(workdir, upperdir);
226err:
227 pr_err("overlayfs: failed to lock workdir+upperdir\n");
228 return -EIO;
229}
230
231static struct dentry *ovl_clear_empty(struct dentry *dentry,
232 struct list_head *list)
233{
234 struct dentry *workdir = ovl_workdir(dentry);
235 struct inode *wdir = workdir->d_inode;
236 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
237 struct inode *udir = upperdir->d_inode;
238 struct path upperpath;
239 struct dentry *upper;
240 struct dentry *opaquedir;
241 struct kstat stat;
242 int err;
243
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200244 if (WARN_ON(!workdir))
245 return ERR_PTR(-EROFS);
246
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200247 err = ovl_lock_rename_workdir(workdir, upperdir);
248 if (err)
249 goto out;
250
251 ovl_path_upper(dentry, &upperpath);
David Howellsa528d352017-01-31 16:46:22 +0000252 err = vfs_getattr(&upperpath, &stat,
253 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200254 if (err)
255 goto out_unlock;
256
257 err = -ESTALE;
258 if (!S_ISDIR(stat.mode))
259 goto out_unlock;
260 upper = upperpath.dentry;
261 if (upper->d_parent->d_inode != udir)
262 goto out_unlock;
263
Miklos Szeredi3d275732017-05-19 09:33:49 +0200264 opaquedir = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265 err = PTR_ERR(opaquedir);
266 if (IS_ERR(opaquedir))
267 goto out_unlock;
268
Al Viro32a3d842016-12-04 17:33:17 +0000269 err = ovl_create_real(wdir, opaquedir,
270 &(struct cattr){.mode = stat.mode}, NULL, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200271 if (err)
272 goto out_dput;
273
274 err = ovl_copy_xattr(upper, opaquedir);
275 if (err)
276 goto out_cleanup;
277
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100278 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279 if (err)
280 goto out_cleanup;
281
Al Viro59551022016-01-22 15:40:57 -0500282 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200283 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500284 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200285 if (err)
286 goto out_cleanup;
287
288 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
289 if (err)
290 goto out_cleanup;
291
292 ovl_cleanup_whiteouts(upper, list);
293 ovl_cleanup(wdir, upper);
294 unlock_rename(workdir, upperdir);
295
296 /* dentry's upper doesn't match now, get rid of it */
297 d_drop(dentry);
298
299 return opaquedir;
300
301out_cleanup:
302 ovl_cleanup(wdir, opaquedir);
303out_dput:
304 dput(opaquedir);
305out_unlock:
306 unlock_rename(workdir, upperdir);
307out:
308 return ERR_PTR(err);
309}
310
Miklos Szeredia105d682014-11-20 16:39:59 +0100311static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200312{
313 int err;
314 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200315 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316 LIST_HEAD(list);
317
318 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200319 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200321 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100322 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200324 /*
325 * When removing an empty opaque directory, then it makes no sense to
326 * replace it with an exact replica of itself.
327 *
328 * If no upperdentry then skip clearing whiteouts.
329 *
330 * Can race with copy-up, since we don't hold the upperdir mutex.
331 * Doesn't matter, since copy-up can't create a non-empty directory
332 * from an empty one.
333 */
334 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
335 ret = ovl_clear_empty(dentry, &list);
336
337out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 ovl_cache_free(&list);
339
340 return ret;
341}
342
Miklos Szeredi38b25692016-09-01 11:11:59 +0200343static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
344 const struct posix_acl *acl)
345{
346 void *buffer;
347 size_t size;
348 int err;
349
350 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
351 return 0;
352
353 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
354 buffer = kmalloc(size, GFP_KERNEL);
355 if (!buffer)
356 return -ENOMEM;
357
358 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
359 err = size;
360 if (err < 0)
361 goto out_free;
362
363 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
364out_free:
365 kfree(buffer);
366 return err;
367}
368
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000370 struct cattr *cattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371 struct dentry *hardlink)
372{
373 struct dentry *workdir = ovl_workdir(dentry);
374 struct inode *wdir = workdir->d_inode;
375 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
376 struct inode *udir = upperdir->d_inode;
377 struct dentry *upper;
378 struct dentry *newdentry;
379 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200380 struct posix_acl *acl, *default_acl;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200381
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200382 if (WARN_ON(!workdir))
383 return -EROFS;
384
Miklos Szeredi38b25692016-09-01 11:11:59 +0200385 if (!hardlink) {
386 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000387 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200388 if (err)
389 return err;
390 }
391
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392 err = ovl_lock_rename_workdir(workdir, upperdir);
393 if (err)
394 goto out;
395
Miklos Szeredi3d275732017-05-19 09:33:49 +0200396 newdentry = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397 err = PTR_ERR(newdentry);
398 if (IS_ERR(newdentry))
399 goto out_unlock;
400
401 upper = lookup_one_len(dentry->d_name.name, upperdir,
402 dentry->d_name.len);
403 err = PTR_ERR(upper);
404 if (IS_ERR(upper))
405 goto out_dput;
406
Al Viro32a3d842016-12-04 17:33:17 +0000407 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 if (err)
409 goto out_dput2;
410
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200411 /*
412 * mode could have been mutilated due to umask (e.g. sgid directory)
413 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200414 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000415 !S_ISLNK(cattr->mode) &&
416 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200417 struct iattr attr = {
418 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000419 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200420 };
421 inode_lock(newdentry->d_inode);
422 err = notify_change(newdentry, &attr, NULL);
423 inode_unlock(newdentry->d_inode);
424 if (err)
425 goto out_cleanup;
426 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200427 if (!hardlink) {
428 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
429 acl);
430 if (err)
431 goto out_cleanup;
432
433 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
434 default_acl);
435 if (err)
436 goto out_cleanup;
437 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200438
Al Viro32a3d842016-12-04 17:33:17 +0000439 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100440 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200441 if (err)
442 goto out_cleanup;
443
444 err = ovl_do_rename(wdir, newdentry, udir, upper,
445 RENAME_EXCHANGE);
446 if (err)
447 goto out_cleanup;
448
449 ovl_cleanup(wdir, upper);
450 } else {
451 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
452 if (err)
453 goto out_cleanup;
454 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200455 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200456 newdentry = NULL;
457out_dput2:
458 dput(upper);
459out_dput:
460 dput(newdentry);
461out_unlock:
462 unlock_rename(workdir, upperdir);
463out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200464 if (!hardlink) {
465 posix_acl_release(acl);
466 posix_acl_release(default_acl);
467 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200468 return err;
469
470out_cleanup:
471 ovl_cleanup(wdir, newdentry);
472 goto out_dput2;
473}
474
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200475static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Al Viro32a3d842016-12-04 17:33:17 +0000476 struct cattr *attr, struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200477{
478 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400479 const struct cred *old_cred;
480 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200481
482 err = ovl_copy_up(dentry->d_parent);
483 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200484 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200485
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400486 old_cred = ovl_override_creds(dentry->d_sb);
487 err = -ENOMEM;
488 override_cred = prepare_creds();
489 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200490 override_cred->fsuid = inode->i_uid;
491 override_cred->fsgid = inode->i_gid;
Vivek Goyal26026252016-07-13 10:44:52 -0400492 if (!hardlink) {
493 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000494 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400495 override_cred);
496 if (err) {
497 put_cred(override_cred);
498 goto out_revert_creds;
499 }
500 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400501 put_cred(override_creds(override_cred));
502 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503
Miklos Szeredic412ce42016-12-16 11:02:55 +0100504 if (!ovl_dentry_is_whiteout(dentry))
Al Viro32a3d842016-12-04 17:33:17 +0000505 err = ovl_create_upper(dentry, inode, attr,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400506 hardlink);
507 else
Al Viro32a3d842016-12-04 17:33:17 +0000508 err = ovl_create_over_whiteout(dentry, inode, attr,
509 hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200510 }
Vivek Goyal26026252016-07-13 10:44:52 -0400511out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400512 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200513 if (!err) {
514 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
515
516 WARN_ON(inode->i_mode != realinode->i_mode);
517 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
518 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200519 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200520 return err;
521}
522
523static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
524 const char *link)
525{
526 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200527 struct inode *inode;
Al Viro32a3d842016-12-04 17:33:17 +0000528 struct cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200529 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000530 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200531 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200532
533 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200534 if (err)
535 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200537 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100538 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200539 if (!inode)
540 goto out_drop_write;
541
542 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000543 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200544
Al Viro32a3d842016-12-04 17:33:17 +0000545 err = ovl_create_or_link(dentry, inode, &attr, NULL);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200546 if (err)
547 iput(inode);
548
549out_drop_write:
550 ovl_drop_write(dentry);
551out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200552 return err;
553}
554
555static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
556 bool excl)
557{
558 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
559}
560
561static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
562{
563 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
564}
565
566static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
567 dev_t rdev)
568{
569 /* Don't allow creation of "whiteout" on overlay */
570 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
571 return -EPERM;
572
573 return ovl_create_object(dentry, mode, rdev, NULL);
574}
575
576static int ovl_symlink(struct inode *dir, struct dentry *dentry,
577 const char *link)
578{
579 return ovl_create_object(dentry, S_IFLNK, 0, link);
580}
581
582static int ovl_link(struct dentry *old, struct inode *newdir,
583 struct dentry *new)
584{
585 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200586 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200587
588 err = ovl_want_write(old);
589 if (err)
590 goto out;
591
592 err = ovl_copy_up(old);
593 if (err)
594 goto out_drop_write;
595
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200596 inode = d_inode(old);
597 ihold(inode);
598
Al Viro32a3d842016-12-04 17:33:17 +0000599 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200600 if (err)
601 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200602
603out_drop_write:
604 ovl_drop_write(old);
605out:
606 return err;
607}
608
Miklos Szeredia105d682014-11-20 16:39:59 +0100609static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200610{
611 struct dentry *workdir = ovl_workdir(dentry);
612 struct inode *wdir = workdir->d_inode;
613 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
614 struct inode *udir = upperdir->d_inode;
615 struct dentry *whiteout;
616 struct dentry *upper;
617 struct dentry *opaquedir = NULL;
618 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700619 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200620
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200621 if (WARN_ON(!workdir))
622 return -EROFS;
623
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200624 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200625 opaquedir = ovl_check_empty_and_clear(dentry);
626 err = PTR_ERR(opaquedir);
627 if (IS_ERR(opaquedir))
628 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200629 }
630
631 err = ovl_lock_rename_workdir(workdir, upperdir);
632 if (err)
633 goto out_dput;
634
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700635 upper = lookup_one_len(dentry->d_name.name, upperdir,
636 dentry->d_name.len);
637 err = PTR_ERR(upper);
638 if (IS_ERR(upper))
639 goto out_unlock;
640
641 err = -ESTALE;
642 if ((opaquedir && upper != opaquedir) ||
643 (!opaquedir && ovl_dentry_upper(dentry) &&
644 upper != ovl_dentry_upper(dentry))) {
645 goto out_dput_upper;
646 }
647
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200648 whiteout = ovl_whiteout(workdir, dentry);
649 err = PTR_ERR(whiteout);
650 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700651 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200652
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700653 if (d_is_dir(upper))
654 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200655
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700656 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
657 if (err)
658 goto kill_whiteout;
659 if (flags)
660 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200661
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200662 ovl_dentry_version_inc(dentry->d_parent);
663out_d_drop:
664 d_drop(dentry);
665 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700666out_dput_upper:
667 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200668out_unlock:
669 unlock_rename(workdir, upperdir);
670out_dput:
671 dput(opaquedir);
672out:
673 return err;
674
675kill_whiteout:
676 ovl_cleanup(wdir, whiteout);
677 goto out_d_drop;
678}
679
680static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
681{
682 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
683 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100684 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300685 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200686 int err;
687
Amir Goldsteind1595112016-10-26 12:34:06 +0300688 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
689 if (is_dir && ovl_dentry_get_redirect(dentry)) {
690 opaquedir = ovl_check_empty_and_clear(dentry);
691 err = PTR_ERR(opaquedir);
692 if (IS_ERR(opaquedir))
693 goto out;
694 }
695
Al Viro59551022016-01-22 15:40:57 -0500696 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100697 upper = lookup_one_len(dentry->d_name.name, upperdir,
698 dentry->d_name.len);
699 err = PTR_ERR(upper);
700 if (IS_ERR(upper))
701 goto out_unlock;
702
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200703 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300704 if ((opaquedir && upper != opaquedir) ||
705 (!opaquedir && upper != ovl_dentry_upper(dentry)))
706 goto out_dput_upper;
707
708 if (is_dir)
709 err = vfs_rmdir(dir, upper);
710 else
711 err = vfs_unlink(dir, upper, NULL);
712 ovl_dentry_version_inc(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200713
714 /*
715 * Keeping this dentry hashed would mean having to release
716 * upperpath/lowerpath, which could only be done if we are the
717 * sole user of this dentry. Too tricky... Just unhash for
718 * now.
719 */
Rui Wangce9113b2016-01-08 23:09:59 +0800720 if (!err)
721 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300722out_dput_upper:
723 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100724out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500725 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300726 dput(opaquedir);
727out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200728 return err;
729}
730
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200731static int ovl_do_remove(struct dentry *dentry, bool is_dir)
732{
733 enum ovl_path_type type;
734 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400735 const struct cred *old_cred;
736
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200737 err = ovl_want_write(dentry);
738 if (err)
739 goto out;
740
741 err = ovl_copy_up(dentry->d_parent);
742 if (err)
743 goto out_drop_write;
744
745 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400746
747 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100748 if (!ovl_lower_positive(dentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200749 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400750 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100751 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400752 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200753 if (!err) {
754 if (is_dir)
755 clear_nlink(dentry->d_inode);
756 else
757 drop_nlink(dentry->d_inode);
758 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200759out_drop_write:
760 ovl_drop_write(dentry);
761out:
762 return err;
763}
764
765static int ovl_unlink(struct inode *dir, struct dentry *dentry)
766{
767 return ovl_do_remove(dentry, false);
768}
769
770static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
771{
772 return ovl_do_remove(dentry, true);
773}
774
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100775static bool ovl_type_merge_or_lower(struct dentry *dentry)
776{
777 enum ovl_path_type type = ovl_path_type(dentry);
778
779 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
780}
781
Miklos Szeredia6c60652016-12-16 11:02:56 +0100782static bool ovl_can_move(struct dentry *dentry)
783{
784 return ovl_redirect_dir(dentry->d_sb) ||
785 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
786}
787
Miklos Szeredia6c60652016-12-16 11:02:56 +0100788static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
789{
790 char *buf, *ret;
791 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100792 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100793
794 if (samedir) {
795 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
796 GFP_KERNEL);
797 goto out;
798 }
799
800 buf = ret = kmalloc(buflen, GFP_TEMPORARY);
801 if (!buf)
802 goto out;
803
804 buflen--;
805 buf[buflen] = '\0';
806 for (d = dget(dentry); !IS_ROOT(d);) {
807 const char *name;
808 int thislen;
809
810 spin_lock(&d->d_lock);
811 name = ovl_dentry_get_redirect(d);
812 if (name) {
813 thislen = strlen(name);
814 } else {
815 name = d->d_name.name;
816 thislen = d->d_name.len;
817 }
818
819 /* If path is too long, fall back to userspace move */
820 if (thislen + (name[0] != '/') > buflen) {
821 ret = ERR_PTR(-EXDEV);
822 spin_unlock(&d->d_lock);
823 goto out_put;
824 }
825
826 buflen -= thislen;
827 memcpy(&buf[buflen], name, thislen);
828 tmp = dget_dlock(d->d_parent);
829 spin_unlock(&d->d_lock);
830
831 dput(d);
832 d = tmp;
833
834 /* Absolute redirect: finished */
835 if (buf[buflen] == '/')
836 break;
837 buflen--;
838 buf[buflen] = '/';
839 }
840 ret = kstrdup(&buf[buflen], GFP_KERNEL);
841out_put:
842 dput(d);
843 kfree(buf);
844out:
845 return ret ? ret : ERR_PTR(-ENOMEM);
846}
847
848static int ovl_set_redirect(struct dentry *dentry, bool samedir)
849{
850 int err;
851 const char *redirect = ovl_dentry_get_redirect(dentry);
852
853 if (redirect && (samedir || redirect[0] == '/'))
854 return 0;
855
856 redirect = ovl_get_redirect(dentry, samedir);
857 if (IS_ERR(redirect))
858 return PTR_ERR(redirect);
859
Amir Goldstein21a22872017-05-17 00:12:41 +0300860 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
861 OVL_XATTR_REDIRECT,
862 redirect, strlen(redirect), -EXDEV);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100863 if (!err) {
864 spin_lock(&dentry->d_lock);
865 ovl_dentry_set_redirect(dentry, redirect);
866 spin_unlock(&dentry->d_lock);
867 } else {
868 kfree(redirect);
Amir Goldstein21a22872017-05-17 00:12:41 +0300869 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100870 /* Fall back to userspace copy-up */
871 err = -EXDEV;
872 }
873 return err;
874}
875
Miklos Szeredi6c02cb52016-12-16 11:02:55 +0100876static int ovl_rename(struct inode *olddir, struct dentry *old,
877 struct inode *newdir, struct dentry *new,
878 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200879{
880 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200881 struct dentry *old_upperdir;
882 struct dentry *new_upperdir;
883 struct dentry *olddentry;
884 struct dentry *newdentry;
885 struct dentry *trap;
886 bool old_opaque;
887 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200888 bool cleanup_whiteout = false;
889 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000890 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100891 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100892 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200893 struct dentry *opaquedir = NULL;
894 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200895
896 err = -EINVAL;
897 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
898 goto out;
899
900 flags &= ~RENAME_NOREPLACE;
901
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200902 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200903 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100904 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200905 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100906 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100907 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200908
909 err = ovl_want_write(old);
910 if (err)
911 goto out;
912
913 err = ovl_copy_up(old);
914 if (err)
915 goto out_drop_write;
916
917 err = ovl_copy_up(new->d_parent);
918 if (err)
919 goto out_drop_write;
920 if (!overwrite) {
921 err = ovl_copy_up(new);
922 if (err)
923 goto out_drop_write;
924 }
925
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400926 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200927
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100928 if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100929 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200930 err = PTR_ERR(opaquedir);
931 if (IS_ERR(opaquedir)) {
932 opaquedir = NULL;
933 goto out_revert_creds;
934 }
935 }
936
937 if (overwrite) {
Miklos Szeredi2aff4532016-12-16 11:02:55 +0100938 if (ovl_lower_positive(old)) {
Miklos Szeredic412ce42016-12-16 11:02:55 +0100939 if (!ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200940 /* Whiteout source */
941 flags |= RENAME_WHITEOUT;
942 } else {
943 /* Switch whiteouts */
944 flags |= RENAME_EXCHANGE;
945 }
Miklos Szeredic412ce42016-12-16 11:02:55 +0100946 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200947 flags |= RENAME_EXCHANGE;
948 cleanup_whiteout = true;
949 }
950 }
951
952 old_upperdir = ovl_dentry_upper(old->d_parent);
953 new_upperdir = ovl_dentry_upper(new->d_parent);
954
955 trap = lock_rename(new_upperdir, old_upperdir);
956
Miklos Szeredi11f37102016-03-21 17:31:44 +0100957 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
958 old->d_name.len);
959 err = PTR_ERR(olddentry);
960 if (IS_ERR(olddentry))
961 goto out_unlock;
962
963 err = -ESTALE;
964 if (olddentry != ovl_dentry_upper(old))
965 goto out_dput_old;
966
967 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
968 new->d_name.len);
969 err = PTR_ERR(newdentry);
970 if (IS_ERR(newdentry))
971 goto out_dput_old;
972
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +0100973 old_opaque = ovl_dentry_is_opaque(old);
974 new_opaque = ovl_dentry_is_opaque(new);
975
Miklos Szeredi11f37102016-03-21 17:31:44 +0100976 err = -ESTALE;
977 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200978 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100979 if (newdentry != opaquedir)
980 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200981 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100982 if (newdentry != ovl_dentry_upper(new))
983 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200984 }
985 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100986 if (!d_is_negative(newdentry) &&
987 (!new_opaque || !ovl_is_whiteout(newdentry)))
988 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200989 }
990
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200991 if (olddentry == trap)
992 goto out_dput;
993 if (newdentry == trap)
994 goto out_dput;
995
Miklos Szeredi804032f2016-12-16 11:02:55 +0100996 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
997 goto out_dput;
998
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100999 err = 0;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001000 if (is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001001 if (ovl_type_merge_or_lower(old))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001002 err = ovl_set_redirect(old, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001003 else if (!old_opaque && ovl_type_merge(new->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001004 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001005 if (err)
1006 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001007 }
Miklos Szeredia6c60652016-12-16 11:02:56 +01001008 if (!overwrite && new_is_dir) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001009 if (ovl_type_merge_or_lower(new))
Miklos Szeredia6c60652016-12-16 11:02:56 +01001010 err = ovl_set_redirect(new, samedir);
Amir Goldstein97c684c2016-11-21 18:57:34 +02001011 else if (!new_opaque && ovl_type_merge(old->d_parent))
Amir Goldstein21a22872017-05-17 00:12:41 +03001012 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001013 if (err)
1014 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001015 }
1016
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001017 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1018 new_upperdir->d_inode, newdentry, flags);
1019 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001020 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001021
1022 if (cleanup_whiteout)
1023 ovl_cleanup(old_upperdir->d_inode, newdentry);
1024
1025 ovl_dentry_version_inc(old->d_parent);
1026 ovl_dentry_version_inc(new->d_parent);
1027
1028out_dput:
1029 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001030out_dput_old:
1031 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001032out_unlock:
1033 unlock_rename(new_upperdir, old_upperdir);
1034out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001035 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001036out_drop_write:
1037 ovl_drop_write(old);
1038out:
1039 dput(opaquedir);
1040 return err;
1041}
1042
1043const struct inode_operations ovl_dir_inode_operations = {
1044 .lookup = ovl_lookup,
1045 .mkdir = ovl_mkdir,
1046 .symlink = ovl_symlink,
1047 .unlink = ovl_unlink,
1048 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001049 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001050 .link = ovl_link,
1051 .setattr = ovl_setattr,
1052 .create = ovl_create,
1053 .mknod = ovl_mknod,
1054 .permission = ovl_permission,
Miklos Szeredi5b712092017-05-05 11:38:58 +02001055 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001056 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001057 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001058 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001059};