blob: 276914ae3c60aaf297747f03b58d4582df335ec9 [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
Vivek Goyal4120fe62018-05-11 11:49:32 -040027static int ovl_set_redirect(struct dentry *dentry, bool samedir);
28
Amir Goldstein415543d2017-06-21 15:28:42 +030029int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020030{
31 int err;
32
33 dget(wdentry);
David Howellse36cb0b2015-01-29 12:02:35 +000034 if (d_is_dir(wdentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020035 err = ovl_do_rmdir(wdir, wdentry);
36 else
37 err = ovl_do_unlink(wdir, wdentry);
38 dput(wdentry);
39
40 if (err) {
41 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
42 wdentry, err);
43 }
Amir Goldstein415543d2017-06-21 15:28:42 +030044
45 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020046}
47
Amir Goldstein137ec522018-05-16 17:51:25 +030048static struct dentry *ovl_lookup_temp(struct dentry *workdir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049{
50 struct dentry *temp;
51 char name[20];
Richard Weinberger6a45b362016-09-16 11:45:24 +020052 static atomic_t temp_id = ATOMIC_INIT(0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020053
Richard Weinberger6a45b362016-09-16 11:45:24 +020054 /* counter is allowed to wrap, since temp dentries are ephemeral */
55 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
Miklos Szeredie9be9d52014-10-24 00:14:38 +020056
57 temp = lookup_one_len(name, workdir, strlen(name));
58 if (!IS_ERR(temp) && temp->d_inode) {
59 pr_err("overlayfs: workdir/%s already exists\n", name);
60 dput(temp);
61 temp = ERR_PTR(-EIO);
62 }
63
64 return temp;
65}
66
67/* caller holds i_mutex on workdir */
Amir Goldsteine7dd0e72017-10-24 17:38:33 +030068static struct dentry *ovl_whiteout(struct dentry *workdir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069{
70 int err;
71 struct dentry *whiteout;
72 struct inode *wdir = workdir->d_inode;
73
Miklos Szeredi3d275732017-05-19 09:33:49 +020074 whiteout = ovl_lookup_temp(workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020075 if (IS_ERR(whiteout))
76 return whiteout;
77
78 err = ovl_do_whiteout(wdir, whiteout);
79 if (err) {
80 dput(whiteout);
81 whiteout = ERR_PTR(err);
82 }
83
84 return whiteout;
85}
86
Amir Goldsteine7dd0e72017-10-24 17:38:33 +030087/* Caller must hold i_mutex on both workdir and dir */
88int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
89 struct dentry *dentry)
90{
91 struct inode *wdir = workdir->d_inode;
92 struct dentry *whiteout;
93 int err;
94 int flags = 0;
95
96 whiteout = ovl_whiteout(workdir);
97 err = PTR_ERR(whiteout);
98 if (IS_ERR(whiteout))
99 return err;
100
101 if (d_is_dir(dentry))
102 flags = RENAME_EXCHANGE;
103
104 err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
105 if (err)
106 goto kill_whiteout;
107 if (flags)
108 ovl_cleanup(wdir, dentry);
109
110out:
111 dput(whiteout);
112 return err;
113
114kill_whiteout:
115 ovl_cleanup(wdir, whiteout);
116 goto out;
117}
118
Al Virof73cc772018-05-16 18:19:53 +0300119static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
120 umode_t mode)
121{
122 int err;
123 struct dentry *d, *dentry = *newdentry;
124
125 err = ovl_do_mkdir(dir, dentry, mode);
126 if (err)
127 return err;
128
129 if (likely(!d_unhashed(dentry)))
130 return 0;
131
132 /*
133 * vfs_mkdir() may succeed and leave the dentry passed
134 * to it unhashed and negative. If that happens, try to
135 * lookup a new hashed and positive dentry.
136 */
137 d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
138 dentry->d_name.len);
139 if (IS_ERR(d)) {
140 pr_warn("overlayfs: failed lookup after mkdir (%pd2, err=%i).\n",
141 dentry, err);
142 return PTR_ERR(d);
143 }
144 dput(dentry);
145 *newdentry = d;
146
147 return 0;
148}
149
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300150struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
151 struct ovl_cattr *attr)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200152{
153 int err;
154
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300155 if (IS_ERR(newdentry))
156 return newdentry;
157
158 err = -ESTALE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200159 if (newdentry->d_inode)
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300160 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200161
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300162 if (attr->hardlink) {
163 err = ovl_do_link(attr->hardlink, dir, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200164 } else {
Al Viro32a3d842016-12-04 17:33:17 +0000165 switch (attr->mode & S_IFMT) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200166 case S_IFREG:
Amir Goldstein6cf00762018-05-16 17:04:00 +0300167 err = ovl_do_create(dir, newdentry, attr->mode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200168 break;
169
170 case S_IFDIR:
Al Virof73cc772018-05-16 18:19:53 +0300171 /* mkdir is special... */
172 err = ovl_mkdir_real(dir, &newdentry, attr->mode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200173 break;
174
175 case S_IFCHR:
176 case S_IFBLK:
177 case S_IFIFO:
178 case S_IFSOCK:
Amir Goldstein6cf00762018-05-16 17:04:00 +0300179 err = ovl_do_mknod(dir, newdentry, attr->mode,
180 attr->rdev);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181 break;
182
183 case S_IFLNK:
Amir Goldstein6cf00762018-05-16 17:04:00 +0300184 err = ovl_do_symlink(dir, newdentry, attr->link);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200185 break;
186
187 default:
188 err = -EPERM;
189 }
190 }
191 if (!err && WARN_ON(!newdentry->d_inode)) {
192 /*
193 * Not quite sure if non-instantiated dentry is legal or not.
194 * VFS doesn't seem to care so check and warn here.
195 */
Miklos Szeredidd8ac6992018-05-31 11:06:11 +0200196 err = -EIO;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197 }
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300198out:
199 if (err) {
200 dput(newdentry);
201 return ERR_PTR(err);
202 }
203 return newdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200204}
205
Amir Goldstein137ec522018-05-16 17:51:25 +0300206struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
207{
208 return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
209 attr);
210}
211
Amir Goldstein21a22872017-05-17 00:12:41 +0300212static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
213 int xerr)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200214{
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100215 int err;
216
Amir Goldstein21a22872017-05-17 00:12:41 +0300217 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100218 if (!err)
219 ovl_dentry_set_opaque(dentry);
220
221 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200222}
223
Amir Goldstein21a22872017-05-17 00:12:41 +0300224static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
225{
226 /*
227 * Fail with -EIO when trying to create opaque dir and upper doesn't
228 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
229 * return a specific error for noxattr case.
230 */
231 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
232}
233
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300234/*
235 * Common operations required to be done after creation of file on upper.
236 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
237 * may not use to instantiate the new dentry.
238 */
239static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
240 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400241{
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300242 struct ovl_inode_params oip = {
243 .upperdentry = newdentry,
244 .newinode = inode,
245 };
246
Miklos Szeredid9854c82018-07-18 15:44:40 +0200247 ovl_dir_modified(dentry->d_parent, false);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200248 ovl_dentry_set_upper_alias(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200249 if (!hardlink) {
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300250 /*
251 * ovl_obtain_alias() can be called after ovl_create_real()
252 * and before we get here, so we may get an inode from cache
253 * with the same real upperdentry that is not the inode we
254 * pre-allocated. In this case we will use the cached inode
255 * to instantiate the new dentry.
256 *
257 * XXX: if we ever use ovl_obtain_alias() to decode directory
258 * file handles, need to use ovl_get_inode_locked() and
259 * d_instantiate_new() here to prevent from creating two
260 * hashed directory inode aliases.
261 */
262 inode = ovl_get_inode(dentry->d_sb, &oip);
263 if (WARN_ON(IS_ERR(inode)))
264 return PTR_ERR(inode);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200265 } else {
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200266 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
267 dput(newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200268 inc_nlink(inode);
269 }
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300270
Vivek Goyal72e48482016-06-16 10:09:14 -0400271 d_instantiate(dentry, inode);
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300272 if (inode != oip.newinode) {
273 pr_warn_ratelimited("overlayfs: newly created inode found in cache (%pd2)\n",
274 dentry);
275 }
276
Amir Goldstein5b6c9052017-04-24 19:54:13 +0300277 /* Force lookup of new upper hardlink to find its lower */
278 if (hardlink)
279 d_drop(dentry);
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300280
281 return 0;
Vivek Goyal72e48482016-06-16 10:09:14 -0400282}
283
Amir Goldstein97c684c2016-11-21 18:57:34 +0200284static bool ovl_type_merge(struct dentry *dentry)
285{
286 return OVL_TYPE_MERGE(ovl_path_type(dentry));
287}
288
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300289static bool ovl_type_origin(struct dentry *dentry)
290{
291 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
292}
293
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300295 struct ovl_cattr *attr)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296{
297 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
298 struct inode *udir = upperdir->d_inode;
299 struct dentry *newdentry;
300 int err;
301
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300302 if (!attr->hardlink && !IS_POSIXACL(udir))
Al Viro32a3d842016-12-04 17:33:17 +0000303 attr->mode &= ~current_umask();
Miklos Szeredi38b25692016-09-01 11:11:59 +0200304
Al Viro59551022016-01-22 15:40:57 -0500305 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300306 newdentry = ovl_create_real(udir,
307 lookup_one_len(dentry->d_name.name,
308 upperdir,
309 dentry->d_name.len),
310 attr);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311 err = PTR_ERR(newdentry);
312 if (IS_ERR(newdentry))
313 goto out_unlock;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200314
Amir Goldstein4a99f3c2017-04-24 22:26:40 +0300315 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
Amir Goldstein97c684c2016-11-21 18:57:34 +0200316 /* Setting opaque here is just an optimization, allow to fail */
317 ovl_set_opaque(dentry, newdentry);
318 }
319
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300320 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
321 if (err)
322 goto out_cleanup;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200323out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500324 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200325 return err;
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300326
327out_cleanup:
328 ovl_cleanup(udir, newdentry);
329 dput(newdentry);
330 goto out_unlock;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331}
332
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200333static struct dentry *ovl_clear_empty(struct dentry *dentry,
334 struct list_head *list)
335{
336 struct dentry *workdir = ovl_workdir(dentry);
337 struct inode *wdir = workdir->d_inode;
338 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
339 struct inode *udir = upperdir->d_inode;
340 struct path upperpath;
341 struct dentry *upper;
342 struct dentry *opaquedir;
343 struct kstat stat;
344 int err;
345
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200346 if (WARN_ON(!workdir))
347 return ERR_PTR(-EROFS);
348
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200349 err = ovl_lock_rename_workdir(workdir, upperdir);
350 if (err)
351 goto out;
352
353 ovl_path_upper(dentry, &upperpath);
David Howellsa528d352017-01-31 16:46:22 +0000354 err = vfs_getattr(&upperpath, &stat,
355 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200356 if (err)
357 goto out_unlock;
358
359 err = -ESTALE;
360 if (!S_ISDIR(stat.mode))
361 goto out_unlock;
362 upper = upperpath.dentry;
363 if (upper->d_parent->d_inode != udir)
364 goto out_unlock;
365
Amir Goldstein137ec522018-05-16 17:51:25 +0300366 opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200367 err = PTR_ERR(opaquedir);
368 if (IS_ERR(opaquedir))
369 goto out_unlock;
370
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371 err = ovl_copy_xattr(upper, opaquedir);
372 if (err)
373 goto out_cleanup;
374
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100375 err = ovl_set_opaque(dentry, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376 if (err)
377 goto out_cleanup;
378
Al Viro59551022016-01-22 15:40:57 -0500379 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500381 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200382 if (err)
383 goto out_cleanup;
384
385 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
386 if (err)
387 goto out_cleanup;
388
389 ovl_cleanup_whiteouts(upper, list);
390 ovl_cleanup(wdir, upper);
391 unlock_rename(workdir, upperdir);
392
393 /* dentry's upper doesn't match now, get rid of it */
394 d_drop(dentry);
395
396 return opaquedir;
397
398out_cleanup:
399 ovl_cleanup(wdir, opaquedir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200400 dput(opaquedir);
401out_unlock:
402 unlock_rename(workdir, upperdir);
403out:
404 return ERR_PTR(err);
405}
406
Miklos Szeredi38b25692016-09-01 11:11:59 +0200407static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
408 const struct posix_acl *acl)
409{
410 void *buffer;
411 size_t size;
412 int err;
413
414 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
415 return 0;
416
417 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
418 buffer = kmalloc(size, GFP_KERNEL);
419 if (!buffer)
420 return -ENOMEM;
421
422 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
423 err = size;
424 if (err < 0)
425 goto out_free;
426
427 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
428out_free:
429 kfree(buffer);
430 return err;
431}
432
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300434 struct ovl_cattr *cattr)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200435{
436 struct dentry *workdir = ovl_workdir(dentry);
437 struct inode *wdir = workdir->d_inode;
438 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
439 struct inode *udir = upperdir->d_inode;
440 struct dentry *upper;
441 struct dentry *newdentry;
442 int err;
Miklos Szeredi38b25692016-09-01 11:11:59 +0200443 struct posix_acl *acl, *default_acl;
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300444 bool hardlink = !!cattr->hardlink;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200446 if (WARN_ON(!workdir))
447 return -EROFS;
448
Miklos Szeredi38b25692016-09-01 11:11:59 +0200449 if (!hardlink) {
450 err = posix_acl_create(dentry->d_parent->d_inode,
Al Viro32a3d842016-12-04 17:33:17 +0000451 &cattr->mode, &default_acl, &acl);
Miklos Szeredi38b25692016-09-01 11:11:59 +0200452 if (err)
453 return err;
454 }
455
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200456 err = ovl_lock_rename_workdir(workdir, upperdir);
457 if (err)
458 goto out;
459
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460 upper = lookup_one_len(dentry->d_name.name, upperdir,
461 dentry->d_name.len);
462 err = PTR_ERR(upper);
463 if (IS_ERR(upper))
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300464 goto out_unlock;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200465
Amir Goldstein137ec522018-05-16 17:51:25 +0300466 newdentry = ovl_create_temp(workdir, cattr);
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300467 err = PTR_ERR(newdentry);
468 if (IS_ERR(newdentry))
469 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200470
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200471 /*
472 * mode could have been mutilated due to umask (e.g. sgid directory)
473 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200474 if (!hardlink &&
Al Viro32a3d842016-12-04 17:33:17 +0000475 !S_ISLNK(cattr->mode) &&
476 newdentry->d_inode->i_mode != cattr->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200477 struct iattr attr = {
478 .ia_valid = ATTR_MODE,
Al Viro32a3d842016-12-04 17:33:17 +0000479 .ia_mode = cattr->mode,
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200480 };
481 inode_lock(newdentry->d_inode);
482 err = notify_change(newdentry, &attr, NULL);
483 inode_unlock(newdentry->d_inode);
484 if (err)
485 goto out_cleanup;
486 }
Miklos Szeredi38b25692016-09-01 11:11:59 +0200487 if (!hardlink) {
488 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
489 acl);
490 if (err)
491 goto out_cleanup;
492
493 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
494 default_acl);
495 if (err)
496 goto out_cleanup;
497 }
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200498
Al Viro32a3d842016-12-04 17:33:17 +0000499 if (!hardlink && S_ISDIR(cattr->mode)) {
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100500 err = ovl_set_opaque(dentry, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200501 if (err)
502 goto out_cleanup;
503
504 err = ovl_do_rename(wdir, newdentry, udir, upper,
505 RENAME_EXCHANGE);
506 if (err)
507 goto out_cleanup;
508
509 ovl_cleanup(wdir, upper);
510 } else {
511 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
512 if (err)
513 goto out_cleanup;
514 }
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300515 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
516 if (err)
517 goto out_cleanup;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200518out_dput:
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300519 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200520out_unlock:
521 unlock_rename(workdir, upperdir);
522out:
Miklos Szeredi38b25692016-09-01 11:11:59 +0200523 if (!hardlink) {
524 posix_acl_release(acl);
525 posix_acl_release(default_acl);
526 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200527 return err;
528
529out_cleanup:
530 ovl_cleanup(wdir, newdentry);
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300531 dput(newdentry);
532 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200533}
534
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200535static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300536 struct ovl_cattr *attr, bool origin)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200537{
538 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400539 const struct cred *old_cred;
540 struct cred *override_cred;
Amir Goldsteinea3dad12017-07-11 15:58:34 +0300541 struct dentry *parent = dentry->d_parent;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200542
Amir Goldsteinea3dad12017-07-11 15:58:34 +0300543 err = ovl_copy_up(parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200545 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200546
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400547 old_cred = ovl_override_creds(dentry->d_sb);
Amir Goldsteinea3dad12017-07-11 15:58:34 +0300548
549 /*
550 * When linking a file with copy up origin into a new parent, mark the
551 * new parent dir "impure".
552 */
553 if (origin) {
554 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
555 if (err)
556 goto out_revert_creds;
557 }
558
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400559 err = -ENOMEM;
560 override_cred = prepare_creds();
561 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200562 override_cred->fsuid = inode->i_uid;
563 override_cred->fsgid = inode->i_gid;
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300564 if (!attr->hardlink) {
Vivek Goyal26026252016-07-13 10:44:52 -0400565 err = security_dentry_create_files_as(dentry,
Al Viro32a3d842016-12-04 17:33:17 +0000566 attr->mode, &dentry->d_name, old_cred,
Vivek Goyal26026252016-07-13 10:44:52 -0400567 override_cred);
568 if (err) {
569 put_cred(override_cred);
570 goto out_revert_creds;
571 }
572 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400573 put_cred(override_creds(override_cred));
574 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200575
Miklos Szeredic412ce42016-12-16 11:02:55 +0100576 if (!ovl_dentry_is_whiteout(dentry))
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300577 err = ovl_create_upper(dentry, inode, attr);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400578 else
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300579 err = ovl_create_over_whiteout(dentry, inode, attr);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200580 }
Vivek Goyal26026252016-07-13 10:44:52 -0400581out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400582 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200583 return err;
584}
585
586static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
587 const char *link)
588{
589 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200590 struct inode *inode;
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300591 struct ovl_cattr attr = {
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200592 .rdev = rdev,
Al Viro32a3d842016-12-04 17:33:17 +0000593 .link = link,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200594 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200595
596 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200597 if (err)
598 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200599
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300600 /* Preallocate inode to be used by ovl_get_inode() */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200601 err = -ENOMEM;
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100602 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200603 if (!inode)
604 goto out_drop_write;
605
Miklos Szeredi6faf05c2018-08-22 10:55:22 +0200606 spin_lock(&inode->i_lock);
607 inode->i_state |= I_CREATING;
608 spin_unlock(&inode->i_lock);
609
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200610 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
Al Viro32a3d842016-12-04 17:33:17 +0000611 attr.mode = inode->i_mode;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200612
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300613 err = ovl_create_or_link(dentry, inode, &attr, false);
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300614 /* Did we end up using the preallocated inode? */
615 if (inode != d_inode(dentry))
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200616 iput(inode);
617
618out_drop_write:
619 ovl_drop_write(dentry);
620out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200621 return err;
622}
623
624static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
625 bool excl)
626{
627 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
628}
629
630static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
631{
632 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
633}
634
635static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
636 dev_t rdev)
637{
638 /* Don't allow creation of "whiteout" on overlay */
639 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
640 return -EPERM;
641
642 return ovl_create_object(dentry, mode, rdev, NULL);
643}
644
645static int ovl_symlink(struct inode *dir, struct dentry *dentry,
646 const char *link)
647{
648 return ovl_create_object(dentry, S_IFLNK, 0, link);
649}
650
651static int ovl_link(struct dentry *old, struct inode *newdir,
652 struct dentry *new)
653{
654 int err;
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300655 bool locked = false;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200656 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200657
658 err = ovl_want_write(old);
659 if (err)
660 goto out;
661
662 err = ovl_copy_up(old);
663 if (err)
664 goto out_drop_write;
665
Vivek Goyal4120fe62018-05-11 11:49:32 -0400666 if (ovl_is_metacopy_dentry(old)) {
667 err = ovl_set_redirect(old, false);
668 if (err)
669 goto out_drop_write;
670 }
671
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300672 err = ovl_nlink_start(old, &locked);
673 if (err)
674 goto out_drop_write;
675
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200676 inode = d_inode(old);
677 ihold(inode);
678
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300679 err = ovl_create_or_link(new, inode,
680 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
681 ovl_type_origin(old));
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200682 if (err)
683 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200684
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300685 ovl_nlink_end(old, locked);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200686out_drop_write:
687 ovl_drop_write(old);
688out:
689 return err;
690}
691
Miklos Szeredi9020df32017-07-04 22:03:16 +0200692static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
693{
694 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
695}
696
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200697static int ovl_remove_and_whiteout(struct dentry *dentry,
698 struct list_head *list)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200699{
700 struct dentry *workdir = ovl_workdir(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200701 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200702 struct dentry *upper;
703 struct dentry *opaquedir = NULL;
704 int err;
705
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200706 if (WARN_ON(!workdir))
707 return -EROFS;
708
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200709 if (!list_empty(list)) {
710 opaquedir = ovl_clear_empty(dentry, list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200711 err = PTR_ERR(opaquedir);
712 if (IS_ERR(opaquedir))
713 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200714 }
715
716 err = ovl_lock_rename_workdir(workdir, upperdir);
717 if (err)
718 goto out_dput;
719
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700720 upper = lookup_one_len(dentry->d_name.name, upperdir,
721 dentry->d_name.len);
722 err = PTR_ERR(upper);
723 if (IS_ERR(upper))
724 goto out_unlock;
725
726 err = -ESTALE;
727 if ((opaquedir && upper != opaquedir) ||
728 (!opaquedir && ovl_dentry_upper(dentry) &&
Miklos Szeredi9020df32017-07-04 22:03:16 +0200729 !ovl_matches_upper(dentry, upper))) {
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700730 goto out_dput_upper;
731 }
732
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300733 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700734 if (err)
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300735 goto out_d_drop;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200736
Miklos Szeredid9854c82018-07-18 15:44:40 +0200737 ovl_dir_modified(dentry->d_parent, true);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200738out_d_drop:
739 d_drop(dentry);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700740out_dput_upper:
741 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200742out_unlock:
743 unlock_rename(workdir, upperdir);
744out_dput:
745 dput(opaquedir);
746out:
747 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200748}
749
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200750static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
751 struct list_head *list)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200752{
753 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
754 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100755 struct dentry *upper;
Amir Goldsteind1595112016-10-26 12:34:06 +0300756 struct dentry *opaquedir = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200757 int err;
758
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200759 if (!list_empty(list)) {
760 opaquedir = ovl_clear_empty(dentry, list);
Amir Goldsteind1595112016-10-26 12:34:06 +0300761 err = PTR_ERR(opaquedir);
762 if (IS_ERR(opaquedir))
763 goto out;
764 }
765
Al Viro59551022016-01-22 15:40:57 -0500766 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100767 upper = lookup_one_len(dentry->d_name.name, upperdir,
768 dentry->d_name.len);
769 err = PTR_ERR(upper);
770 if (IS_ERR(upper))
771 goto out_unlock;
772
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200773 err = -ESTALE;
Amir Goldsteind1595112016-10-26 12:34:06 +0300774 if ((opaquedir && upper != opaquedir) ||
Miklos Szeredi9020df32017-07-04 22:03:16 +0200775 (!opaquedir && !ovl_matches_upper(dentry, upper)))
Amir Goldsteind1595112016-10-26 12:34:06 +0300776 goto out_dput_upper;
777
778 if (is_dir)
779 err = vfs_rmdir(dir, upper);
780 else
781 err = vfs_unlink(dir, upper, NULL);
Miklos Szeredid9854c82018-07-18 15:44:40 +0200782 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200783
784 /*
785 * Keeping this dentry hashed would mean having to release
786 * upperpath/lowerpath, which could only be done if we are the
787 * sole user of this dentry. Too tricky... Just unhash for
788 * now.
789 */
Rui Wangce9113b2016-01-08 23:09:59 +0800790 if (!err)
791 d_drop(dentry);
Amir Goldsteind1595112016-10-26 12:34:06 +0300792out_dput_upper:
793 dput(upper);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100794out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500795 inode_unlock(dir);
Amir Goldsteind1595112016-10-26 12:34:06 +0300796 dput(opaquedir);
797out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200798 return err;
799}
800
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200801static bool ovl_pure_upper(struct dentry *dentry)
802{
803 return !ovl_dentry_lower(dentry) &&
804 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
805}
806
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200807static int ovl_do_remove(struct dentry *dentry, bool is_dir)
808{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200809 int err;
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300810 bool locked = false;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400811 const struct cred *old_cred;
Miklos Szeredid9854c82018-07-18 15:44:40 +0200812 struct dentry *upperdentry;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200813 bool lower_positive = ovl_lower_positive(dentry);
814 LIST_HEAD(list);
815
816 /* No need to clean pure upper removed by vfs_rmdir() */
817 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
818 err = ovl_check_empty_dir(dentry, &list);
819 if (err)
820 goto out;
821 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400822
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200823 err = ovl_want_write(dentry);
824 if (err)
825 goto out;
826
827 err = ovl_copy_up(dentry->d_parent);
828 if (err)
829 goto out_drop_write;
830
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300831 err = ovl_nlink_start(dentry, &locked);
832 if (err)
833 goto out_drop_write;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400834
835 old_cred = ovl_override_creds(dentry->d_sb);
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200836 if (!lower_positive)
837 err = ovl_remove_upper(dentry, is_dir, &list);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400838 else
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200839 err = ovl_remove_and_whiteout(dentry, &list);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400840 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200841 if (!err) {
842 if (is_dir)
843 clear_nlink(dentry->d_inode);
844 else
845 drop_nlink(dentry->d_inode);
846 }
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300847 ovl_nlink_end(dentry, locked);
Miklos Szeredid9854c82018-07-18 15:44:40 +0200848
849 /*
850 * Copy ctime
851 *
852 * Note: we fail to update ctime if there was no copy-up, only a
853 * whiteout
854 */
855 upperdentry = ovl_dentry_upper(dentry);
856 if (upperdentry)
857 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
858
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200859out_drop_write:
860 ovl_drop_write(dentry);
861out:
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200862 ovl_cache_free(&list);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200863 return err;
864}
865
866static int ovl_unlink(struct inode *dir, struct dentry *dentry)
867{
868 return ovl_do_remove(dentry, false);
869}
870
871static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
872{
873 return ovl_do_remove(dentry, true);
874}
875
Miklos Szeredi370e55a2016-12-16 11:02:55 +0100876static bool ovl_type_merge_or_lower(struct dentry *dentry)
877{
878 enum ovl_path_type type = ovl_path_type(dentry);
879
880 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
881}
882
Miklos Szeredia6c60652016-12-16 11:02:56 +0100883static bool ovl_can_move(struct dentry *dentry)
884{
885 return ovl_redirect_dir(dentry->d_sb) ||
886 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
887}
888
Vivek Goyal7bb08382018-05-11 11:49:32 -0400889static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
Miklos Szeredia6c60652016-12-16 11:02:56 +0100890{
891 char *buf, *ret;
892 struct dentry *d, *tmp;
Miklos Szeredi3ea22a72016-12-16 11:02:57 +0100893 int buflen = ovl_redirect_max + 1;
Miklos Szeredia6c60652016-12-16 11:02:56 +0100894
Vivek Goyal7bb08382018-05-11 11:49:32 -0400895 if (!abs_redirect) {
Miklos Szeredia6c60652016-12-16 11:02:56 +0100896 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
897 GFP_KERNEL);
898 goto out;
899 }
900
Michal Hocko0ee931c2017-09-13 16:28:29 -0700901 buf = ret = kmalloc(buflen, GFP_KERNEL);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100902 if (!buf)
903 goto out;
904
905 buflen--;
906 buf[buflen] = '\0';
907 for (d = dget(dentry); !IS_ROOT(d);) {
908 const char *name;
909 int thislen;
910
911 spin_lock(&d->d_lock);
912 name = ovl_dentry_get_redirect(d);
913 if (name) {
914 thislen = strlen(name);
915 } else {
916 name = d->d_name.name;
917 thislen = d->d_name.len;
918 }
919
920 /* If path is too long, fall back to userspace move */
921 if (thislen + (name[0] != '/') > buflen) {
922 ret = ERR_PTR(-EXDEV);
923 spin_unlock(&d->d_lock);
924 goto out_put;
925 }
926
927 buflen -= thislen;
928 memcpy(&buf[buflen], name, thislen);
929 tmp = dget_dlock(d->d_parent);
930 spin_unlock(&d->d_lock);
931
932 dput(d);
933 d = tmp;
934
935 /* Absolute redirect: finished */
936 if (buf[buflen] == '/')
937 break;
938 buflen--;
939 buf[buflen] = '/';
940 }
941 ret = kstrdup(&buf[buflen], GFP_KERNEL);
942out_put:
943 dput(d);
944 kfree(buf);
945out:
946 return ret ? ret : ERR_PTR(-ENOMEM);
947}
948
Vivek Goyal7bb08382018-05-11 11:49:32 -0400949static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
950{
951 struct dentry *lowerdentry;
952
953 if (!samedir)
954 return true;
955
956 if (d_is_dir(dentry))
957 return false;
958
959 /*
960 * For non-dir hardlinked files, we need absolute redirects
961 * in general as two upper hardlinks could be in different
962 * dirs. We could put a relative redirect now and convert
963 * it to absolute redirect later. But when nlink > 1 and
964 * indexing is on, that means relative redirect needs to be
965 * converted to absolute during copy up of another lower
966 * hardllink as well.
967 *
968 * So without optimizing too much, just check if lower is
969 * a hard link or not. If lower is hard link, put absolute
970 * redirect.
971 */
972 lowerdentry = ovl_dentry_lower(dentry);
973 return (d_inode(lowerdentry)->i_nlink > 1);
974}
975
Miklos Szeredia6c60652016-12-16 11:02:56 +0100976static int ovl_set_redirect(struct dentry *dentry, bool samedir)
977{
978 int err;
979 const char *redirect = ovl_dentry_get_redirect(dentry);
Vivek Goyal7bb08382018-05-11 11:49:32 -0400980 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100981
Vivek Goyal7bb08382018-05-11 11:49:32 -0400982 if (redirect && (!absolute_redirect || redirect[0] == '/'))
Miklos Szeredia6c60652016-12-16 11:02:56 +0100983 return 0;
984
Vivek Goyal7bb08382018-05-11 11:49:32 -0400985 redirect = ovl_get_redirect(dentry, absolute_redirect);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100986 if (IS_ERR(redirect))
987 return PTR_ERR(redirect);
988
Amir Goldstein21a22872017-05-17 00:12:41 +0300989 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
990 OVL_XATTR_REDIRECT,
991 redirect, strlen(redirect), -EXDEV);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100992 if (!err) {
993 spin_lock(&dentry->d_lock);
994 ovl_dentry_set_redirect(dentry, redirect);
995 spin_unlock(&dentry->d_lock);
996 } else {
997 kfree(redirect);
Amir Goldsteinda2e6b72017-11-22 20:27:34 +0200998 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
999 err);
Miklos Szeredia6c60652016-12-16 11:02:56 +01001000 /* Fall back to userspace copy-up */
1001 err = -EXDEV;
1002 }
1003 return err;
1004}
1005
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001006static int ovl_rename(struct inode *olddir, struct dentry *old,
1007 struct inode *newdir, struct dentry *new,
1008 unsigned int flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001009{
1010 int err;
Amir Goldstein5f8415d2017-06-20 15:35:14 +03001011 bool locked = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001012 struct dentry *old_upperdir;
1013 struct dentry *new_upperdir;
1014 struct dentry *olddentry;
1015 struct dentry *newdentry;
1016 struct dentry *trap;
1017 bool old_opaque;
1018 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001019 bool cleanup_whiteout = false;
1020 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +00001021 bool is_dir = d_is_dir(old);
Miklos Szeredi370e55a2016-12-16 11:02:55 +01001022 bool new_is_dir = d_is_dir(new);
Miklos Szeredia6c60652016-12-16 11:02:56 +01001023 bool samedir = olddir == newdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001024 struct dentry *opaquedir = NULL;
1025 const struct cred *old_cred = NULL;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001026 LIST_HEAD(list);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001027
1028 err = -EINVAL;
1029 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1030 goto out;
1031
1032 flags &= ~RENAME_NOREPLACE;
1033
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001034 /* Don't copy up directory trees */
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001035 err = -EXDEV;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001036 if (!ovl_can_move(old))
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001037 goto out;
Miklos Szeredia6c60652016-12-16 11:02:56 +01001038 if (!overwrite && !ovl_can_move(new))
Miklos Szeredi370e55a2016-12-16 11:02:55 +01001039 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001040
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001041 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1042 err = ovl_check_empty_dir(new, &list);
1043 if (err)
1044 goto out;
1045 }
1046
1047 if (overwrite) {
1048 if (ovl_lower_positive(old)) {
1049 if (!ovl_dentry_is_whiteout(new)) {
1050 /* Whiteout source */
1051 flags |= RENAME_WHITEOUT;
1052 } else {
1053 /* Switch whiteouts */
1054 flags |= RENAME_EXCHANGE;
1055 }
1056 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1057 flags |= RENAME_EXCHANGE;
1058 cleanup_whiteout = true;
1059 }
1060 }
1061
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001062 err = ovl_want_write(old);
1063 if (err)
1064 goto out;
1065
1066 err = ovl_copy_up(old);
1067 if (err)
1068 goto out_drop_write;
1069
1070 err = ovl_copy_up(new->d_parent);
1071 if (err)
1072 goto out_drop_write;
1073 if (!overwrite) {
1074 err = ovl_copy_up(new);
1075 if (err)
1076 goto out_drop_write;
Amir Goldstein5f8415d2017-06-20 15:35:14 +03001077 } else {
1078 err = ovl_nlink_start(new, &locked);
1079 if (err)
1080 goto out_drop_write;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001081 }
1082
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001083 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001084
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001085 if (!list_empty(&list)) {
1086 opaquedir = ovl_clear_empty(new, &list);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001087 err = PTR_ERR(opaquedir);
1088 if (IS_ERR(opaquedir)) {
1089 opaquedir = NULL;
1090 goto out_revert_creds;
1091 }
1092 }
1093
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001094 old_upperdir = ovl_dentry_upper(old->d_parent);
1095 new_upperdir = ovl_dentry_upper(new->d_parent);
1096
Amir Goldsteinee1d6d372017-05-11 16:42:26 +03001097 if (!samedir) {
1098 /*
1099 * When moving a merge dir or non-dir with copy up origin into
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001100 * a new parent, we are marking the new parent dir "impure".
1101 * When ovl_iterate() iterates an "impure" upper dir, it will
1102 * lookup the origin inodes of the entries to fill d_ino.
Amir Goldsteinee1d6d372017-05-11 16:42:26 +03001103 */
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001104 if (ovl_type_origin(old)) {
Amir Goldsteinee1d6d372017-05-11 16:42:26 +03001105 err = ovl_set_impure(new->d_parent, new_upperdir);
1106 if (err)
1107 goto out_revert_creds;
1108 }
Amir Goldsteinf3a15682017-05-24 15:29:33 +03001109 if (!overwrite && ovl_type_origin(new)) {
Amir Goldsteinee1d6d372017-05-11 16:42:26 +03001110 err = ovl_set_impure(old->d_parent, old_upperdir);
1111 if (err)
1112 goto out_revert_creds;
1113 }
1114 }
1115
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001116 trap = lock_rename(new_upperdir, old_upperdir);
1117
Miklos Szeredi11f37102016-03-21 17:31:44 +01001118 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1119 old->d_name.len);
1120 err = PTR_ERR(olddentry);
1121 if (IS_ERR(olddentry))
1122 goto out_unlock;
1123
1124 err = -ESTALE;
Miklos Szeredi9020df32017-07-04 22:03:16 +02001125 if (!ovl_matches_upper(old, olddentry))
Miklos Szeredi11f37102016-03-21 17:31:44 +01001126 goto out_dput_old;
1127
1128 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1129 new->d_name.len);
1130 err = PTR_ERR(newdentry);
1131 if (IS_ERR(newdentry))
1132 goto out_dput_old;
1133
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001134 old_opaque = ovl_dentry_is_opaque(old);
1135 new_opaque = ovl_dentry_is_opaque(new);
1136
Miklos Szeredi11f37102016-03-21 17:31:44 +01001137 err = -ESTALE;
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001138 if (d_inode(new) && ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001139 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001140 if (newdentry != opaquedir)
1141 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001142 } else {
Miklos Szeredi9020df32017-07-04 22:03:16 +02001143 if (!ovl_matches_upper(new, newdentry))
Miklos Szeredi11f37102016-03-21 17:31:44 +01001144 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001145 }
1146 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +01001147 if (!d_is_negative(newdentry) &&
1148 (!new_opaque || !ovl_is_whiteout(newdentry)))
1149 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001150 }
1151
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001152 if (olddentry == trap)
1153 goto out_dput;
1154 if (newdentry == trap)
1155 goto out_dput;
1156
Miklos Szeredi804032f2016-12-16 11:02:55 +01001157 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1158 goto out_dput;
1159
Miklos Szeredi5cf5b472016-12-16 11:02:57 +01001160 err = 0;
Vivek Goyal7bb08382018-05-11 11:49:32 -04001161 if (ovl_type_merge_or_lower(old))
1162 err = ovl_set_redirect(old, samedir);
1163 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1164 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1165 if (err)
1166 goto out_dput;
1167
1168 if (!overwrite && ovl_type_merge_or_lower(new))
1169 err = ovl_set_redirect(new, samedir);
1170 else if (!overwrite && new_is_dir && !new_opaque &&
1171 ovl_type_merge(old->d_parent))
1172 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1173 if (err)
1174 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001175
Miklos Szeredi3ee23ff2016-12-16 11:02:55 +01001176 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1177 new_upperdir->d_inode, newdentry, flags);
1178 if (err)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001179 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001180
1181 if (cleanup_whiteout)
1182 ovl_cleanup(old_upperdir->d_inode, newdentry);
1183
Amir Goldsteinf681eb12017-06-05 22:44:49 +03001184 if (overwrite && d_inode(new)) {
1185 if (new_is_dir)
1186 clear_nlink(d_inode(new));
1187 else
1188 drop_nlink(d_inode(new));
1189 }
1190
Miklos Szeredid9854c82018-07-18 15:44:40 +02001191 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1192 (!overwrite && ovl_type_origin(new)));
1193 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1194 (d_inode(new) && ovl_type_origin(new)));
1195
1196 /* copy ctime: */
1197 ovl_copyattr(d_inode(olddentry), d_inode(old));
1198 if (d_inode(new) && ovl_dentry_upper(new))
1199 ovl_copyattr(d_inode(newdentry), d_inode(new));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001200
1201out_dput:
1202 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +01001203out_dput_old:
1204 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001205out_unlock:
1206 unlock_rename(new_upperdir, old_upperdir);
1207out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -04001208 revert_creds(old_cred);
Amir Goldstein5f8415d2017-06-20 15:35:14 +03001209 ovl_nlink_end(new, locked);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001210out_drop_write:
1211 ovl_drop_write(old);
1212out:
1213 dput(opaquedir);
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001214 ovl_cache_free(&list);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001215 return err;
1216}
1217
1218const struct inode_operations ovl_dir_inode_operations = {
1219 .lookup = ovl_lookup,
1220 .mkdir = ovl_mkdir,
1221 .symlink = ovl_symlink,
1222 .unlink = ovl_unlink,
1223 .rmdir = ovl_rmdir,
Miklos Szeredi6c02cb52016-12-16 11:02:55 +01001224 .rename = ovl_rename,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001225 .link = ovl_link,
1226 .setattr = ovl_setattr,
1227 .create = ovl_create,
1228 .mknod = ovl_mknod,
1229 .permission = ovl_permission,
Miklos Szeredi5b712092017-05-05 11:38:58 +02001230 .getattr = ovl_getattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001231 .listxattr = ovl_listxattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -04001232 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001233 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001234};