blob: 12bcd07b9e32c516f89605a7247da493d3ccedad [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>
15#include "overlayfs.h"
16
17void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
18{
19 int err;
20
21 dget(wdentry);
David Howellse36cb0b2015-01-29 12:02:35 +000022 if (d_is_dir(wdentry))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020023 err = ovl_do_rmdir(wdir, wdentry);
24 else
25 err = ovl_do_unlink(wdir, wdentry);
26 dput(wdentry);
27
28 if (err) {
29 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
30 wdentry, err);
31 }
32}
33
34struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
35{
36 struct dentry *temp;
37 char name[20];
38
39 snprintf(name, sizeof(name), "#%lx", (unsigned long) dentry);
40
41 temp = lookup_one_len(name, workdir, strlen(name));
42 if (!IS_ERR(temp) && temp->d_inode) {
43 pr_err("overlayfs: workdir/%s already exists\n", name);
44 dput(temp);
45 temp = ERR_PTR(-EIO);
46 }
47
48 return temp;
49}
50
51/* caller holds i_mutex on workdir */
52static struct dentry *ovl_whiteout(struct dentry *workdir,
53 struct dentry *dentry)
54{
55 int err;
56 struct dentry *whiteout;
57 struct inode *wdir = workdir->d_inode;
58
59 whiteout = ovl_lookup_temp(workdir, dentry);
60 if (IS_ERR(whiteout))
61 return whiteout;
62
63 err = ovl_do_whiteout(wdir, whiteout);
64 if (err) {
65 dput(whiteout);
66 whiteout = ERR_PTR(err);
67 }
68
69 return whiteout;
70}
71
72int ovl_create_real(struct inode *dir, struct dentry *newdentry,
73 struct kstat *stat, const char *link,
74 struct dentry *hardlink, bool debug)
75{
76 int err;
77
78 if (newdentry->d_inode)
79 return -ESTALE;
80
81 if (hardlink) {
82 err = ovl_do_link(hardlink, dir, newdentry, debug);
83 } else {
84 switch (stat->mode & S_IFMT) {
85 case S_IFREG:
86 err = ovl_do_create(dir, newdentry, stat->mode, debug);
87 break;
88
89 case S_IFDIR:
90 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
91 break;
92
93 case S_IFCHR:
94 case S_IFBLK:
95 case S_IFIFO:
96 case S_IFSOCK:
97 err = ovl_do_mknod(dir, newdentry,
98 stat->mode, stat->rdev, debug);
99 break;
100
101 case S_IFLNK:
102 err = ovl_do_symlink(dir, newdentry, link, debug);
103 break;
104
105 default:
106 err = -EPERM;
107 }
108 }
109 if (!err && WARN_ON(!newdentry->d_inode)) {
110 /*
111 * Not quite sure if non-instantiated dentry is legal or not.
112 * VFS doesn't seem to care so check and warn here.
113 */
114 err = -ENOENT;
115 }
116 return err;
117}
118
119static int ovl_set_opaque(struct dentry *upperdentry)
120{
hujianyangcead89b2014-11-24 18:25:21 +0800121 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200122}
123
124static void ovl_remove_opaque(struct dentry *upperdentry)
125{
126 int err;
127
hujianyangcead89b2014-11-24 18:25:21 +0800128 err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200129 if (err) {
130 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
131 upperdentry->d_name.name, err);
132 }
133}
134
135static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
136 struct kstat *stat)
137{
138 int err;
139 enum ovl_path_type type;
140 struct path realpath;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400141 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142
143 type = ovl_path_real(dentry, &realpath);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400144 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200145 err = vfs_getattr(&realpath, stat);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400146 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147 if (err)
148 return err;
149
150 stat->dev = dentry->d_sb->s_dev;
151 stat->ino = dentry->d_inode->i_ino;
152
153 /*
154 * It's probably not worth it to count subdirs to get the
155 * correct link count. nlink=1 seems to pacify 'find' and
156 * other utilities.
157 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100158 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200159 stat->nlink = 1;
160
161 return 0;
162}
163
Vivek Goyal72e48482016-06-16 10:09:14 -0400164/* Common operations required to be done after creation of file on upper */
165static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200166 struct dentry *newdentry, bool hardlink)
Vivek Goyal72e48482016-06-16 10:09:14 -0400167{
168 ovl_dentry_version_inc(dentry->d_parent);
169 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200170 if (!hardlink) {
171 ovl_inode_update(inode, d_inode(newdentry));
172 ovl_copyattr(newdentry->d_inode, inode);
173 } else {
174 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
175 inc_nlink(inode);
176 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400177 d_instantiate(dentry, inode);
178}
179
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200180static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
181 struct kstat *stat, const char *link,
182 struct dentry *hardlink)
183{
184 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
185 struct inode *udir = upperdir->d_inode;
186 struct dentry *newdentry;
187 int err;
188
Al Viro59551022016-01-22 15:40:57 -0500189 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200190 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
191 dentry->d_name.len);
192 err = PTR_ERR(newdentry);
193 if (IS_ERR(newdentry))
194 goto out_unlock;
195 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
196 if (err)
197 goto out_dput;
198
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200199 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200 newdentry = NULL;
201out_dput:
202 dput(newdentry);
203out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500204 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205 return err;
206}
207
208static int ovl_lock_rename_workdir(struct dentry *workdir,
209 struct dentry *upperdir)
210{
211 /* Workdir should not be the same as upperdir */
212 if (workdir == upperdir)
213 goto err;
214
215 /* Workdir should not be subdir of upperdir and vice versa */
216 if (lock_rename(workdir, upperdir) != NULL)
217 goto err_unlock;
218
219 return 0;
220
221err_unlock:
222 unlock_rename(workdir, upperdir);
223err:
224 pr_err("overlayfs: failed to lock workdir+upperdir\n");
225 return -EIO;
226}
227
228static struct dentry *ovl_clear_empty(struct dentry *dentry,
229 struct list_head *list)
230{
231 struct dentry *workdir = ovl_workdir(dentry);
232 struct inode *wdir = workdir->d_inode;
233 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
234 struct inode *udir = upperdir->d_inode;
235 struct path upperpath;
236 struct dentry *upper;
237 struct dentry *opaquedir;
238 struct kstat stat;
239 int err;
240
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200241 if (WARN_ON(!workdir))
242 return ERR_PTR(-EROFS);
243
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200244 err = ovl_lock_rename_workdir(workdir, upperdir);
245 if (err)
246 goto out;
247
248 ovl_path_upper(dentry, &upperpath);
249 err = vfs_getattr(&upperpath, &stat);
250 if (err)
251 goto out_unlock;
252
253 err = -ESTALE;
254 if (!S_ISDIR(stat.mode))
255 goto out_unlock;
256 upper = upperpath.dentry;
257 if (upper->d_parent->d_inode != udir)
258 goto out_unlock;
259
260 opaquedir = ovl_lookup_temp(workdir, dentry);
261 err = PTR_ERR(opaquedir);
262 if (IS_ERR(opaquedir))
263 goto out_unlock;
264
265 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
266 if (err)
267 goto out_dput;
268
269 err = ovl_copy_xattr(upper, opaquedir);
270 if (err)
271 goto out_cleanup;
272
273 err = ovl_set_opaque(opaquedir);
274 if (err)
275 goto out_cleanup;
276
Al Viro59551022016-01-22 15:40:57 -0500277 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200278 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500279 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200280 if (err)
281 goto out_cleanup;
282
283 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
284 if (err)
285 goto out_cleanup;
286
287 ovl_cleanup_whiteouts(upper, list);
288 ovl_cleanup(wdir, upper);
289 unlock_rename(workdir, upperdir);
290
291 /* dentry's upper doesn't match now, get rid of it */
292 d_drop(dentry);
293
294 return opaquedir;
295
296out_cleanup:
297 ovl_cleanup(wdir, opaquedir);
298out_dput:
299 dput(opaquedir);
300out_unlock:
301 unlock_rename(workdir, upperdir);
302out:
303 return ERR_PTR(err);
304}
305
Miklos Szeredia105d682014-11-20 16:39:59 +0100306static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307{
308 int err;
309 struct dentry *ret = NULL;
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200310 enum ovl_path_type type = ovl_path_type(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200311 LIST_HEAD(list);
312
313 err = ovl_check_empty_dir(dentry, &list);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200314 if (err) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200315 ret = ERR_PTR(err);
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200316 goto out_free;
Miklos Szeredia105d682014-11-20 16:39:59 +0100317 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200318
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200319 /*
320 * When removing an empty opaque directory, then it makes no sense to
321 * replace it with an exact replica of itself.
322 *
323 * If no upperdentry then skip clearing whiteouts.
324 *
325 * Can race with copy-up, since we don't hold the upperdir mutex.
326 * Doesn't matter, since copy-up can't create a non-empty directory
327 * from an empty one.
328 */
329 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
330 ret = ovl_clear_empty(dentry, &list);
331
332out_free:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200333 ovl_cache_free(&list);
334
335 return ret;
336}
337
338static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
339 struct kstat *stat, const char *link,
340 struct dentry *hardlink)
341{
342 struct dentry *workdir = ovl_workdir(dentry);
343 struct inode *wdir = workdir->d_inode;
344 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
345 struct inode *udir = upperdir->d_inode;
346 struct dentry *upper;
347 struct dentry *newdentry;
348 int err;
349
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200350 if (WARN_ON(!workdir))
351 return -EROFS;
352
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200353 err = ovl_lock_rename_workdir(workdir, upperdir);
354 if (err)
355 goto out;
356
357 newdentry = ovl_lookup_temp(workdir, dentry);
358 err = PTR_ERR(newdentry);
359 if (IS_ERR(newdentry))
360 goto out_unlock;
361
362 upper = lookup_one_len(dentry->d_name.name, upperdir,
363 dentry->d_name.len);
364 err = PTR_ERR(upper);
365 if (IS_ERR(upper))
366 goto out_dput;
367
368 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
369 if (err)
370 goto out_dput2;
371
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200372 /*
373 * mode could have been mutilated due to umask (e.g. sgid directory)
374 */
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200375 if (!hardlink &&
376 !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200377 struct iattr attr = {
378 .ia_valid = ATTR_MODE,
379 .ia_mode = stat->mode,
380 };
381 inode_lock(newdentry->d_inode);
382 err = notify_change(newdentry, &attr, NULL);
383 inode_unlock(newdentry->d_inode);
384 if (err)
385 goto out_cleanup;
386 }
387
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200388 if (!hardlink && S_ISDIR(stat->mode)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389 err = ovl_set_opaque(newdentry);
390 if (err)
391 goto out_cleanup;
392
393 err = ovl_do_rename(wdir, newdentry, udir, upper,
394 RENAME_EXCHANGE);
395 if (err)
396 goto out_cleanup;
397
398 ovl_cleanup(wdir, upper);
399 } else {
400 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
401 if (err)
402 goto out_cleanup;
403 }
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200404 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405 newdentry = NULL;
406out_dput2:
407 dput(upper);
408out_dput:
409 dput(newdentry);
410out_unlock:
411 unlock_rename(workdir, upperdir);
412out:
413 return err;
414
415out_cleanup:
416 ovl_cleanup(wdir, newdentry);
417 goto out_dput2;
418}
419
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200420static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
421 struct kstat *stat, const char *link,
422 struct dentry *hardlink)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200423{
424 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400425 const struct cred *old_cred;
426 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200427
428 err = ovl_copy_up(dentry->d_parent);
429 if (err)
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200430 return err;
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200431
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400432 old_cred = ovl_override_creds(dentry->d_sb);
433 err = -ENOMEM;
434 override_cred = prepare_creds();
435 if (override_cred) {
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200436 override_cred->fsuid = inode->i_uid;
437 override_cred->fsgid = inode->i_gid;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400438 put_cred(override_creds(override_cred));
439 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200440
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400441 if (!ovl_dentry_is_opaque(dentry))
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200442 err = ovl_create_upper(dentry, inode, stat, link,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400443 hardlink);
444 else
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200445 err = ovl_create_over_whiteout(dentry, inode, stat,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400446 link, hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200447 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400448 revert_creds(old_cred);
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200449 if (!err) {
450 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
451
452 WARN_ON(inode->i_mode != realinode->i_mode);
453 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
454 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
Miklos Szeredibb0d2b82016-07-29 12:05:23 +0200455 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200456 return err;
457}
458
459static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
460 const char *link)
461{
462 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200463 struct inode *inode;
464 struct kstat stat = {
465 .rdev = rdev,
466 };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200467
468 err = ovl_want_write(dentry);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200469 if (err)
470 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200471
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200472 err = -ENOMEM;
473 inode = ovl_new_inode(dentry->d_sb, mode);
474 if (!inode)
475 goto out_drop_write;
476
477 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
478 stat.mode = inode->i_mode;
479
480 err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
481 if (err)
482 iput(inode);
483
484out_drop_write:
485 ovl_drop_write(dentry);
486out:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487 return err;
488}
489
490static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
491 bool excl)
492{
493 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
494}
495
496static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
497{
498 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
499}
500
501static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
502 dev_t rdev)
503{
504 /* Don't allow creation of "whiteout" on overlay */
505 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
506 return -EPERM;
507
508 return ovl_create_object(dentry, mode, rdev, NULL);
509}
510
511static int ovl_symlink(struct inode *dir, struct dentry *dentry,
512 const char *link)
513{
514 return ovl_create_object(dentry, S_IFLNK, 0, link);
515}
516
517static int ovl_link(struct dentry *old, struct inode *newdir,
518 struct dentry *new)
519{
520 int err;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200521 struct inode *inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200522
523 err = ovl_want_write(old);
524 if (err)
525 goto out;
526
527 err = ovl_copy_up(old);
528 if (err)
529 goto out_drop_write;
530
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200531 inode = d_inode(old);
532 ihold(inode);
533
534 err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
535 if (err)
536 iput(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200537
538out_drop_write:
539 ovl_drop_write(old);
540out:
541 return err;
542}
543
Miklos Szeredia105d682014-11-20 16:39:59 +0100544static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200545{
546 struct dentry *workdir = ovl_workdir(dentry);
547 struct inode *wdir = workdir->d_inode;
548 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
549 struct inode *udir = upperdir->d_inode;
550 struct dentry *whiteout;
551 struct dentry *upper;
552 struct dentry *opaquedir = NULL;
553 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700554 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200555
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200556 if (WARN_ON(!workdir))
557 return -EROFS;
558
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200559 if (is_dir) {
Miklos Szeredi30c17eb2016-07-29 12:05:25 +0200560 opaquedir = ovl_check_empty_and_clear(dentry);
561 err = PTR_ERR(opaquedir);
562 if (IS_ERR(opaquedir))
563 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200564 }
565
566 err = ovl_lock_rename_workdir(workdir, upperdir);
567 if (err)
568 goto out_dput;
569
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700570 upper = lookup_one_len(dentry->d_name.name, upperdir,
571 dentry->d_name.len);
572 err = PTR_ERR(upper);
573 if (IS_ERR(upper))
574 goto out_unlock;
575
576 err = -ESTALE;
577 if ((opaquedir && upper != opaquedir) ||
578 (!opaquedir && ovl_dentry_upper(dentry) &&
579 upper != ovl_dentry_upper(dentry))) {
580 goto out_dput_upper;
581 }
582
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200583 whiteout = ovl_whiteout(workdir, dentry);
584 err = PTR_ERR(whiteout);
585 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700586 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200587
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700588 if (d_is_dir(upper))
589 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200590
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700591 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
592 if (err)
593 goto kill_whiteout;
594 if (flags)
595 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200596
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200597 ovl_dentry_version_inc(dentry->d_parent);
598out_d_drop:
599 d_drop(dentry);
600 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700601out_dput_upper:
602 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200603out_unlock:
604 unlock_rename(workdir, upperdir);
605out_dput:
606 dput(opaquedir);
607out:
608 return err;
609
610kill_whiteout:
611 ovl_cleanup(wdir, whiteout);
612 goto out_d_drop;
613}
614
615static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
616{
617 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
618 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100619 struct dentry *upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200620 int err;
621
Al Viro59551022016-01-22 15:40:57 -0500622 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100623 upper = lookup_one_len(dentry->d_name.name, upperdir,
624 dentry->d_name.len);
625 err = PTR_ERR(upper);
626 if (IS_ERR(upper))
627 goto out_unlock;
628
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200629 err = -ESTALE;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100630 if (upper == ovl_dentry_upper(dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200631 if (is_dir)
632 err = vfs_rmdir(dir, upper);
633 else
634 err = vfs_unlink(dir, upper, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200635 ovl_dentry_version_inc(dentry->d_parent);
636 }
Miklos Szeredi11f37102016-03-21 17:31:44 +0100637 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200638
639 /*
640 * Keeping this dentry hashed would mean having to release
641 * upperpath/lowerpath, which could only be done if we are the
642 * sole user of this dentry. Too tricky... Just unhash for
643 * now.
644 */
Rui Wangce9113b2016-01-08 23:09:59 +0800645 if (!err)
646 d_drop(dentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100647out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500648 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200649
650 return err;
651}
652
653static inline int ovl_check_sticky(struct dentry *dentry)
654{
655 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
656 struct inode *inode = ovl_dentry_real(dentry)->d_inode;
657
658 if (check_sticky(dir, inode))
659 return -EPERM;
660
661 return 0;
662}
663
664static int ovl_do_remove(struct dentry *dentry, bool is_dir)
665{
666 enum ovl_path_type type;
667 int err;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400668 const struct cred *old_cred;
669
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200670
671 err = ovl_check_sticky(dentry);
672 if (err)
673 goto out;
674
675 err = ovl_want_write(dentry);
676 if (err)
677 goto out;
678
679 err = ovl_copy_up(dentry->d_parent);
680 if (err)
681 goto out_drop_write;
682
683 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400684
685 old_cred = ovl_override_creds(dentry->d_sb);
686 if (OVL_TYPE_PURE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200687 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400688 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100689 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400690 revert_creds(old_cred);
Miklos Szeredidbc816d2016-07-29 12:05:24 +0200691 if (!err) {
692 if (is_dir)
693 clear_nlink(dentry->d_inode);
694 else
695 drop_nlink(dentry->d_inode);
696 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200697out_drop_write:
698 ovl_drop_write(dentry);
699out:
700 return err;
701}
702
703static int ovl_unlink(struct inode *dir, struct dentry *dentry)
704{
705 return ovl_do_remove(dentry, false);
706}
707
708static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
709{
710 return ovl_do_remove(dentry, true);
711}
712
713static int ovl_rename2(struct inode *olddir, struct dentry *old,
714 struct inode *newdir, struct dentry *new,
715 unsigned int flags)
716{
717 int err;
718 enum ovl_path_type old_type;
719 enum ovl_path_type new_type;
720 struct dentry *old_upperdir;
721 struct dentry *new_upperdir;
722 struct dentry *olddentry;
723 struct dentry *newdentry;
724 struct dentry *trap;
725 bool old_opaque;
726 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200727 bool cleanup_whiteout = false;
728 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000729 bool is_dir = d_is_dir(old);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200730 bool new_is_dir = false;
731 struct dentry *opaquedir = NULL;
732 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200733
734 err = -EINVAL;
735 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
736 goto out;
737
738 flags &= ~RENAME_NOREPLACE;
739
740 err = ovl_check_sticky(old);
741 if (err)
742 goto out;
743
744 /* Don't copy up directory trees */
745 old_type = ovl_path_type(old);
746 err = -EXDEV;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100747 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200748 goto out;
749
750 if (new->d_inode) {
751 err = ovl_check_sticky(new);
752 if (err)
753 goto out;
754
David Howellse36cb0b2015-01-29 12:02:35 +0000755 if (d_is_dir(new))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200756 new_is_dir = true;
757
758 new_type = ovl_path_type(new);
759 err = -EXDEV;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100760 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200761 goto out;
762
763 err = 0;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100764 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200765 if (ovl_dentry_lower(old)->d_inode ==
766 ovl_dentry_lower(new)->d_inode)
767 goto out;
768 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100769 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200770 if (ovl_dentry_upper(old)->d_inode ==
771 ovl_dentry_upper(new)->d_inode)
772 goto out;
773 }
774 } else {
775 if (ovl_dentry_is_opaque(new))
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100776 new_type = __OVL_PATH_UPPER;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200777 else
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100778 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200779 }
780
781 err = ovl_want_write(old);
782 if (err)
783 goto out;
784
785 err = ovl_copy_up(old);
786 if (err)
787 goto out_drop_write;
788
789 err = ovl_copy_up(new->d_parent);
790 if (err)
791 goto out_drop_write;
792 if (!overwrite) {
793 err = ovl_copy_up(new);
794 if (err)
795 goto out_drop_write;
796 }
797
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100798 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
799 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200800
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400801 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200802
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100803 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100804 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200805 err = PTR_ERR(opaquedir);
806 if (IS_ERR(opaquedir)) {
807 opaquedir = NULL;
808 goto out_revert_creds;
809 }
810 }
811
812 if (overwrite) {
813 if (old_opaque) {
814 if (new->d_inode || !new_opaque) {
815 /* Whiteout source */
816 flags |= RENAME_WHITEOUT;
817 } else {
818 /* Switch whiteouts */
819 flags |= RENAME_EXCHANGE;
820 }
821 } else if (is_dir && !new->d_inode && new_opaque) {
822 flags |= RENAME_EXCHANGE;
823 cleanup_whiteout = true;
824 }
825 }
826
827 old_upperdir = ovl_dentry_upper(old->d_parent);
828 new_upperdir = ovl_dentry_upper(new->d_parent);
829
830 trap = lock_rename(new_upperdir, old_upperdir);
831
Miklos Szeredi11f37102016-03-21 17:31:44 +0100832
833 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
834 old->d_name.len);
835 err = PTR_ERR(olddentry);
836 if (IS_ERR(olddentry))
837 goto out_unlock;
838
839 err = -ESTALE;
840 if (olddentry != ovl_dentry_upper(old))
841 goto out_dput_old;
842
843 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
844 new->d_name.len);
845 err = PTR_ERR(newdentry);
846 if (IS_ERR(newdentry))
847 goto out_dput_old;
848
849 err = -ESTALE;
850 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200851 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100852 if (newdentry != opaquedir)
853 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200854 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100855 if (newdentry != ovl_dentry_upper(new))
856 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200857 }
858 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100859 if (!d_is_negative(newdentry) &&
860 (!new_opaque || !ovl_is_whiteout(newdentry)))
861 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200862 }
863
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200864 if (olddentry == trap)
865 goto out_dput;
866 if (newdentry == trap)
867 goto out_dput;
868
869 if (is_dir && !old_opaque && new_opaque) {
870 err = ovl_set_opaque(olddentry);
871 if (err)
872 goto out_dput;
873 }
874 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
875 err = ovl_set_opaque(newdentry);
876 if (err)
877 goto out_dput;
878 }
879
880 if (old_opaque || new_opaque) {
881 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
882 new_upperdir->d_inode, newdentry,
883 flags);
884 } else {
885 /* No debug for the plain case */
886 BUG_ON(flags & ~RENAME_EXCHANGE);
887 err = vfs_rename(old_upperdir->d_inode, olddentry,
888 new_upperdir->d_inode, newdentry,
889 NULL, flags);
890 }
891
892 if (err) {
893 if (is_dir && !old_opaque && new_opaque)
894 ovl_remove_opaque(olddentry);
895 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
896 ovl_remove_opaque(newdentry);
897 goto out_dput;
898 }
899
900 if (is_dir && old_opaque && !new_opaque)
901 ovl_remove_opaque(olddentry);
902 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
903 ovl_remove_opaque(newdentry);
904
Konstantin Khlebnikov45d11732016-01-31 16:17:53 +0300905 /*
906 * Old dentry now lives in different location. Dentries in
907 * lowerstack are stale. We cannot drop them here because
908 * access to them is lockless. This could be only pure upper
909 * or opaque directory - numlower is zero. Or upper non-dir
910 * entry - its pureness is tracked by flag opaque.
911 */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200912 if (old_opaque != new_opaque) {
913 ovl_dentry_set_opaque(old, new_opaque);
914 if (!overwrite)
915 ovl_dentry_set_opaque(new, old_opaque);
916 }
917
918 if (cleanup_whiteout)
919 ovl_cleanup(old_upperdir->d_inode, newdentry);
920
921 ovl_dentry_version_inc(old->d_parent);
922 ovl_dentry_version_inc(new->d_parent);
923
924out_dput:
925 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100926out_dput_old:
927 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200928out_unlock:
929 unlock_rename(new_upperdir, old_upperdir);
930out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400931 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200932out_drop_write:
933 ovl_drop_write(old);
934out:
935 dput(opaquedir);
936 return err;
937}
938
939const struct inode_operations ovl_dir_inode_operations = {
940 .lookup = ovl_lookup,
941 .mkdir = ovl_mkdir,
942 .symlink = ovl_symlink,
943 .unlink = ovl_unlink,
944 .rmdir = ovl_rmdir,
945 .rename2 = ovl_rename2,
946 .link = ovl_link,
947 .setattr = ovl_setattr,
948 .create = ovl_create,
949 .mknod = ovl_mknod,
950 .permission = ovl_permission,
951 .getattr = ovl_dir_getattr,
Miklos Szeredid837a492016-07-29 12:05:24 +0200952 .setxattr = generic_setxattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200953 .getxattr = ovl_getxattr,
954 .listxattr = ovl_listxattr,
955 .removexattr = ovl_removexattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400956 .get_acl = ovl_get_acl,
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200957 .update_time = ovl_update_time,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200958};