blob: aa632055719678b5f358efd355cb3a889d2c40f7 [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;
141
142 type = ovl_path_real(dentry, &realpath);
143 err = vfs_getattr(&realpath, stat);
144 if (err)
145 return err;
146
147 stat->dev = dentry->d_sb->s_dev;
148 stat->ino = dentry->d_inode->i_ino;
149
150 /*
151 * It's probably not worth it to count subdirs to get the
152 * correct link count. nlink=1 seems to pacify 'find' and
153 * other utilities.
154 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100155 if (OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200156 stat->nlink = 1;
157
158 return 0;
159}
160
Vivek Goyal72e48482016-06-16 10:09:14 -0400161/* Common operations required to be done after creation of file on upper */
162static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
163 struct dentry *newdentry)
164{
165 ovl_dentry_version_inc(dentry->d_parent);
166 ovl_dentry_update(dentry, newdentry);
167 ovl_copyattr(newdentry->d_inode, inode);
168 d_instantiate(dentry, inode);
169}
170
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200171static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
172 struct kstat *stat, const char *link,
173 struct dentry *hardlink)
174{
175 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
176 struct inode *udir = upperdir->d_inode;
177 struct dentry *newdentry;
178 int err;
179
Al Viro59551022016-01-22 15:40:57 -0500180 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
182 dentry->d_name.len);
183 err = PTR_ERR(newdentry);
184 if (IS_ERR(newdentry))
185 goto out_unlock;
186 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
187 if (err)
188 goto out_dput;
189
Vivek Goyal72e48482016-06-16 10:09:14 -0400190 ovl_instantiate(dentry, inode, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200191 newdentry = NULL;
192out_dput:
193 dput(newdentry);
194out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500195 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196 return err;
197}
198
199static int ovl_lock_rename_workdir(struct dentry *workdir,
200 struct dentry *upperdir)
201{
202 /* Workdir should not be the same as upperdir */
203 if (workdir == upperdir)
204 goto err;
205
206 /* Workdir should not be subdir of upperdir and vice versa */
207 if (lock_rename(workdir, upperdir) != NULL)
208 goto err_unlock;
209
210 return 0;
211
212err_unlock:
213 unlock_rename(workdir, upperdir);
214err:
215 pr_err("overlayfs: failed to lock workdir+upperdir\n");
216 return -EIO;
217}
218
219static struct dentry *ovl_clear_empty(struct dentry *dentry,
220 struct list_head *list)
221{
222 struct dentry *workdir = ovl_workdir(dentry);
223 struct inode *wdir = workdir->d_inode;
224 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
225 struct inode *udir = upperdir->d_inode;
226 struct path upperpath;
227 struct dentry *upper;
228 struct dentry *opaquedir;
229 struct kstat stat;
230 int err;
231
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200232 if (WARN_ON(!workdir))
233 return ERR_PTR(-EROFS);
234
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200235 err = ovl_lock_rename_workdir(workdir, upperdir);
236 if (err)
237 goto out;
238
239 ovl_path_upper(dentry, &upperpath);
240 err = vfs_getattr(&upperpath, &stat);
241 if (err)
242 goto out_unlock;
243
244 err = -ESTALE;
245 if (!S_ISDIR(stat.mode))
246 goto out_unlock;
247 upper = upperpath.dentry;
248 if (upper->d_parent->d_inode != udir)
249 goto out_unlock;
250
251 opaquedir = ovl_lookup_temp(workdir, dentry);
252 err = PTR_ERR(opaquedir);
253 if (IS_ERR(opaquedir))
254 goto out_unlock;
255
256 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
257 if (err)
258 goto out_dput;
259
260 err = ovl_copy_xattr(upper, opaquedir);
261 if (err)
262 goto out_cleanup;
263
264 err = ovl_set_opaque(opaquedir);
265 if (err)
266 goto out_cleanup;
267
Al Viro59551022016-01-22 15:40:57 -0500268 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200269 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500270 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200271 if (err)
272 goto out_cleanup;
273
274 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
275 if (err)
276 goto out_cleanup;
277
278 ovl_cleanup_whiteouts(upper, list);
279 ovl_cleanup(wdir, upper);
280 unlock_rename(workdir, upperdir);
281
282 /* dentry's upper doesn't match now, get rid of it */
283 d_drop(dentry);
284
285 return opaquedir;
286
287out_cleanup:
288 ovl_cleanup(wdir, opaquedir);
289out_dput:
290 dput(opaquedir);
291out_unlock:
292 unlock_rename(workdir, upperdir);
293out:
294 return ERR_PTR(err);
295}
296
Miklos Szeredia105d682014-11-20 16:39:59 +0100297static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200298{
299 int err;
300 struct dentry *ret = NULL;
301 LIST_HEAD(list);
302
303 err = ovl_check_empty_dir(dentry, &list);
304 if (err)
305 ret = ERR_PTR(err);
Miklos Szeredia105d682014-11-20 16:39:59 +0100306 else {
307 /*
308 * If no upperdentry then skip clearing whiteouts.
309 *
310 * Can race with copy-up, since we don't hold the upperdir
311 * mutex. Doesn't matter, since copy-up can't create a
312 * non-empty directory from an empty one.
313 */
314 if (ovl_dentry_upper(dentry))
315 ret = ovl_clear_empty(dentry, &list);
316 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317
318 ovl_cache_free(&list);
319
320 return ret;
321}
322
323static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
324 struct kstat *stat, const char *link,
325 struct dentry *hardlink)
326{
327 struct dentry *workdir = ovl_workdir(dentry);
328 struct inode *wdir = workdir->d_inode;
329 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
330 struct inode *udir = upperdir->d_inode;
331 struct dentry *upper;
332 struct dentry *newdentry;
333 int err;
334
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200335 if (WARN_ON(!workdir))
336 return -EROFS;
337
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 err = ovl_lock_rename_workdir(workdir, upperdir);
339 if (err)
340 goto out;
341
342 newdentry = ovl_lookup_temp(workdir, dentry);
343 err = PTR_ERR(newdentry);
344 if (IS_ERR(newdentry))
345 goto out_unlock;
346
347 upper = lookup_one_len(dentry->d_name.name, upperdir,
348 dentry->d_name.len);
349 err = PTR_ERR(upper);
350 if (IS_ERR(upper))
351 goto out_dput;
352
353 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
354 if (err)
355 goto out_dput2;
356
357 if (S_ISDIR(stat->mode)) {
358 err = ovl_set_opaque(newdentry);
359 if (err)
360 goto out_cleanup;
361
362 err = ovl_do_rename(wdir, newdentry, udir, upper,
363 RENAME_EXCHANGE);
364 if (err)
365 goto out_cleanup;
366
367 ovl_cleanup(wdir, upper);
368 } else {
369 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
370 if (err)
371 goto out_cleanup;
372 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400373 ovl_instantiate(dentry, inode, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200374 newdentry = NULL;
375out_dput2:
376 dput(upper);
377out_dput:
378 dput(newdentry);
379out_unlock:
380 unlock_rename(workdir, upperdir);
381out:
382 return err;
383
384out_cleanup:
385 ovl_cleanup(wdir, newdentry);
386 goto out_dput2;
387}
388
389static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev,
390 const char *link, struct dentry *hardlink)
391{
392 int err;
393 struct inode *inode;
394 struct kstat stat = {
395 .mode = mode,
396 .rdev = rdev,
397 };
398
399 err = -ENOMEM;
400 inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
401 if (!inode)
402 goto out;
403
404 err = ovl_copy_up(dentry->d_parent);
405 if (err)
406 goto out_iput;
407
408 if (!ovl_dentry_is_opaque(dentry)) {
409 err = ovl_create_upper(dentry, inode, &stat, link, hardlink);
410 } else {
411 const struct cred *old_cred;
Miklos Szeredid0e13f52016-06-15 14:18:59 +0200412 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200414 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200415
Miklos Szeredid0e13f52016-06-15 14:18:59 +0200416 err = -ENOMEM;
417 override_cred = prepare_creds();
418 if (override_cred) {
419 override_cred->fsuid = old_cred->fsuid;
420 override_cred->fsgid = old_cred->fsgid;
421 put_cred(override_creds(override_cred));
422 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200423
Miklos Szeredid0e13f52016-06-15 14:18:59 +0200424 err = ovl_create_over_whiteout(dentry, inode, &stat,
425 link, hardlink);
426 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200427 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428 }
429
430 if (!err)
431 inode = NULL;
432out_iput:
433 iput(inode);
434out:
435 return err;
436}
437
438static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
439 const char *link)
440{
441 int err;
442
443 err = ovl_want_write(dentry);
444 if (!err) {
445 err = ovl_create_or_link(dentry, mode, rdev, link, NULL);
446 ovl_drop_write(dentry);
447 }
448
449 return err;
450}
451
452static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
453 bool excl)
454{
455 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
456}
457
458static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
459{
460 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
461}
462
463static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
464 dev_t rdev)
465{
466 /* Don't allow creation of "whiteout" on overlay */
467 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
468 return -EPERM;
469
470 return ovl_create_object(dentry, mode, rdev, NULL);
471}
472
473static int ovl_symlink(struct inode *dir, struct dentry *dentry,
474 const char *link)
475{
476 return ovl_create_object(dentry, S_IFLNK, 0, link);
477}
478
479static int ovl_link(struct dentry *old, struct inode *newdir,
480 struct dentry *new)
481{
482 int err;
483 struct dentry *upper;
484
485 err = ovl_want_write(old);
486 if (err)
487 goto out;
488
489 err = ovl_copy_up(old);
490 if (err)
491 goto out_drop_write;
492
493 upper = ovl_dentry_upper(old);
494 err = ovl_create_or_link(new, upper->d_inode->i_mode, 0, NULL, upper);
495
496out_drop_write:
497 ovl_drop_write(old);
498out:
499 return err;
500}
501
Miklos Szeredia105d682014-11-20 16:39:59 +0100502static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503{
504 struct dentry *workdir = ovl_workdir(dentry);
505 struct inode *wdir = workdir->d_inode;
506 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
507 struct inode *udir = upperdir->d_inode;
508 struct dentry *whiteout;
509 struct dentry *upper;
510 struct dentry *opaquedir = NULL;
511 int err;
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700512 int flags = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200513
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200514 if (WARN_ON(!workdir))
515 return -EROFS;
516
Miklos Szeredid377c5e2015-05-14 10:04:44 +0200517 if (is_dir) {
518 if (OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) {
519 opaquedir = ovl_check_empty_and_clear(dentry);
520 err = PTR_ERR(opaquedir);
521 if (IS_ERR(opaquedir))
522 goto out;
523 } else {
524 LIST_HEAD(list);
525
526 /*
527 * When removing an empty opaque directory, then it
528 * makes no sense to replace it with an exact replica of
529 * itself. But emptiness still needs to be checked.
530 */
531 err = ovl_check_empty_dir(dentry, &list);
532 ovl_cache_free(&list);
533 if (err)
534 goto out;
535 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200536 }
537
538 err = ovl_lock_rename_workdir(workdir, upperdir);
539 if (err)
540 goto out_dput;
541
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700542 upper = lookup_one_len(dentry->d_name.name, upperdir,
543 dentry->d_name.len);
544 err = PTR_ERR(upper);
545 if (IS_ERR(upper))
546 goto out_unlock;
547
548 err = -ESTALE;
549 if ((opaquedir && upper != opaquedir) ||
550 (!opaquedir && ovl_dentry_upper(dentry) &&
551 upper != ovl_dentry_upper(dentry))) {
552 goto out_dput_upper;
553 }
554
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200555 whiteout = ovl_whiteout(workdir, dentry);
556 err = PTR_ERR(whiteout);
557 if (IS_ERR(whiteout))
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700558 goto out_dput_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200559
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700560 if (d_is_dir(upper))
561 flags = RENAME_EXCHANGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200562
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700563 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
564 if (err)
565 goto kill_whiteout;
566 if (flags)
567 ovl_cleanup(wdir, upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200568
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200569 ovl_dentry_version_inc(dentry->d_parent);
570out_d_drop:
571 d_drop(dentry);
572 dput(whiteout);
Maxim Patlasovcfc9fde2016-07-21 18:24:26 -0700573out_dput_upper:
574 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200575out_unlock:
576 unlock_rename(workdir, upperdir);
577out_dput:
578 dput(opaquedir);
579out:
580 return err;
581
582kill_whiteout:
583 ovl_cleanup(wdir, whiteout);
584 goto out_d_drop;
585}
586
587static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
588{
589 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
590 struct inode *dir = upperdir->d_inode;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100591 struct dentry *upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200592 int err;
593
Al Viro59551022016-01-22 15:40:57 -0500594 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100595 upper = lookup_one_len(dentry->d_name.name, upperdir,
596 dentry->d_name.len);
597 err = PTR_ERR(upper);
598 if (IS_ERR(upper))
599 goto out_unlock;
600
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200601 err = -ESTALE;
Miklos Szeredi11f37102016-03-21 17:31:44 +0100602 if (upper == ovl_dentry_upper(dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200603 if (is_dir)
604 err = vfs_rmdir(dir, upper);
605 else
606 err = vfs_unlink(dir, upper, NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200607 ovl_dentry_version_inc(dentry->d_parent);
608 }
Miklos Szeredi11f37102016-03-21 17:31:44 +0100609 dput(upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200610
611 /*
612 * Keeping this dentry hashed would mean having to release
613 * upperpath/lowerpath, which could only be done if we are the
614 * sole user of this dentry. Too tricky... Just unhash for
615 * now.
616 */
Rui Wangce9113b2016-01-08 23:09:59 +0800617 if (!err)
618 d_drop(dentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100619out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500620 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200621
622 return err;
623}
624
625static inline int ovl_check_sticky(struct dentry *dentry)
626{
627 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
628 struct inode *inode = ovl_dentry_real(dentry)->d_inode;
629
630 if (check_sticky(dir, inode))
631 return -EPERM;
632
633 return 0;
634}
635
636static int ovl_do_remove(struct dentry *dentry, bool is_dir)
637{
638 enum ovl_path_type type;
639 int err;
640
641 err = ovl_check_sticky(dentry);
642 if (err)
643 goto out;
644
645 err = ovl_want_write(dentry);
646 if (err)
647 goto out;
648
649 err = ovl_copy_up(dentry->d_parent);
650 if (err)
651 goto out_drop_write;
652
653 type = ovl_path_type(dentry);
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100654 if (OVL_TYPE_PURE_UPPER(type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200655 err = ovl_remove_upper(dentry, is_dir);
656 } else {
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200657 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200658
Miklos Szeredia105d682014-11-20 16:39:59 +0100659 err = ovl_remove_and_whiteout(dentry, is_dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200660
661 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200662 }
663out_drop_write:
664 ovl_drop_write(dentry);
665out:
666 return err;
667}
668
669static int ovl_unlink(struct inode *dir, struct dentry *dentry)
670{
671 return ovl_do_remove(dentry, false);
672}
673
674static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
675{
676 return ovl_do_remove(dentry, true);
677}
678
679static int ovl_rename2(struct inode *olddir, struct dentry *old,
680 struct inode *newdir, struct dentry *new,
681 unsigned int flags)
682{
683 int err;
684 enum ovl_path_type old_type;
685 enum ovl_path_type new_type;
686 struct dentry *old_upperdir;
687 struct dentry *new_upperdir;
688 struct dentry *olddentry;
689 struct dentry *newdentry;
690 struct dentry *trap;
691 bool old_opaque;
692 bool new_opaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200693 bool cleanup_whiteout = false;
694 bool overwrite = !(flags & RENAME_EXCHANGE);
David Howellse36cb0b2015-01-29 12:02:35 +0000695 bool is_dir = d_is_dir(old);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200696 bool new_is_dir = false;
697 struct dentry *opaquedir = NULL;
698 const struct cred *old_cred = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200699
700 err = -EINVAL;
701 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
702 goto out;
703
704 flags &= ~RENAME_NOREPLACE;
705
706 err = ovl_check_sticky(old);
707 if (err)
708 goto out;
709
710 /* Don't copy up directory trees */
711 old_type = ovl_path_type(old);
712 err = -EXDEV;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100713 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200714 goto out;
715
716 if (new->d_inode) {
717 err = ovl_check_sticky(new);
718 if (err)
719 goto out;
720
David Howellse36cb0b2015-01-29 12:02:35 +0000721 if (d_is_dir(new))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200722 new_is_dir = true;
723
724 new_type = ovl_path_type(new);
725 err = -EXDEV;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100726 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200727 goto out;
728
729 err = 0;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100730 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200731 if (ovl_dentry_lower(old)->d_inode ==
732 ovl_dentry_lower(new)->d_inode)
733 goto out;
734 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100735 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200736 if (ovl_dentry_upper(old)->d_inode ==
737 ovl_dentry_upper(new)->d_inode)
738 goto out;
739 }
740 } else {
741 if (ovl_dentry_is_opaque(new))
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100742 new_type = __OVL_PATH_UPPER;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200743 else
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100744 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200745 }
746
747 err = ovl_want_write(old);
748 if (err)
749 goto out;
750
751 err = ovl_copy_up(old);
752 if (err)
753 goto out_drop_write;
754
755 err = ovl_copy_up(new->d_parent);
756 if (err)
757 goto out_drop_write;
758 if (!overwrite) {
759 err = ovl_copy_up(new);
760 if (err)
761 goto out_drop_write;
762 }
763
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100764 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
765 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200766
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200767 if (old_opaque || new_opaque)
768 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200769
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100770 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100771 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200772 err = PTR_ERR(opaquedir);
773 if (IS_ERR(opaquedir)) {
774 opaquedir = NULL;
775 goto out_revert_creds;
776 }
777 }
778
779 if (overwrite) {
780 if (old_opaque) {
781 if (new->d_inode || !new_opaque) {
782 /* Whiteout source */
783 flags |= RENAME_WHITEOUT;
784 } else {
785 /* Switch whiteouts */
786 flags |= RENAME_EXCHANGE;
787 }
788 } else if (is_dir && !new->d_inode && new_opaque) {
789 flags |= RENAME_EXCHANGE;
790 cleanup_whiteout = true;
791 }
792 }
793
794 old_upperdir = ovl_dentry_upper(old->d_parent);
795 new_upperdir = ovl_dentry_upper(new->d_parent);
796
797 trap = lock_rename(new_upperdir, old_upperdir);
798
Miklos Szeredi11f37102016-03-21 17:31:44 +0100799
800 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
801 old->d_name.len);
802 err = PTR_ERR(olddentry);
803 if (IS_ERR(olddentry))
804 goto out_unlock;
805
806 err = -ESTALE;
807 if (olddentry != ovl_dentry_upper(old))
808 goto out_dput_old;
809
810 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
811 new->d_name.len);
812 err = PTR_ERR(newdentry);
813 if (IS_ERR(newdentry))
814 goto out_dput_old;
815
816 err = -ESTALE;
817 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200818 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100819 if (newdentry != opaquedir)
820 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200821 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100822 if (newdentry != ovl_dentry_upper(new))
823 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200824 }
825 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100826 if (!d_is_negative(newdentry) &&
827 (!new_opaque || !ovl_is_whiteout(newdentry)))
828 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200829 }
830
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200831 if (olddentry == trap)
832 goto out_dput;
833 if (newdentry == trap)
834 goto out_dput;
835
836 if (is_dir && !old_opaque && new_opaque) {
837 err = ovl_set_opaque(olddentry);
838 if (err)
839 goto out_dput;
840 }
841 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
842 err = ovl_set_opaque(newdentry);
843 if (err)
844 goto out_dput;
845 }
846
847 if (old_opaque || new_opaque) {
848 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
849 new_upperdir->d_inode, newdentry,
850 flags);
851 } else {
852 /* No debug for the plain case */
853 BUG_ON(flags & ~RENAME_EXCHANGE);
854 err = vfs_rename(old_upperdir->d_inode, olddentry,
855 new_upperdir->d_inode, newdentry,
856 NULL, flags);
857 }
858
859 if (err) {
860 if (is_dir && !old_opaque && new_opaque)
861 ovl_remove_opaque(olddentry);
862 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
863 ovl_remove_opaque(newdentry);
864 goto out_dput;
865 }
866
867 if (is_dir && old_opaque && !new_opaque)
868 ovl_remove_opaque(olddentry);
869 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
870 ovl_remove_opaque(newdentry);
871
Konstantin Khlebnikov45d11732016-01-31 16:17:53 +0300872 /*
873 * Old dentry now lives in different location. Dentries in
874 * lowerstack are stale. We cannot drop them here because
875 * access to them is lockless. This could be only pure upper
876 * or opaque directory - numlower is zero. Or upper non-dir
877 * entry - its pureness is tracked by flag opaque.
878 */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200879 if (old_opaque != new_opaque) {
880 ovl_dentry_set_opaque(old, new_opaque);
881 if (!overwrite)
882 ovl_dentry_set_opaque(new, old_opaque);
883 }
884
885 if (cleanup_whiteout)
886 ovl_cleanup(old_upperdir->d_inode, newdentry);
887
888 ovl_dentry_version_inc(old->d_parent);
889 ovl_dentry_version_inc(new->d_parent);
890
891out_dput:
892 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100893out_dput_old:
894 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200895out_unlock:
896 unlock_rename(new_upperdir, old_upperdir);
897out_revert_creds:
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200898 if (old_opaque || new_opaque)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200899 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200900out_drop_write:
901 ovl_drop_write(old);
902out:
903 dput(opaquedir);
904 return err;
905}
906
907const struct inode_operations ovl_dir_inode_operations = {
908 .lookup = ovl_lookup,
909 .mkdir = ovl_mkdir,
910 .symlink = ovl_symlink,
911 .unlink = ovl_unlink,
912 .rmdir = ovl_rmdir,
913 .rename2 = ovl_rename2,
914 .link = ovl_link,
915 .setattr = ovl_setattr,
916 .create = ovl_create,
917 .mknod = ovl_mknod,
918 .permission = ovl_permission,
919 .getattr = ovl_dir_getattr,
920 .setxattr = ovl_setxattr,
921 .getxattr = ovl_getxattr,
922 .listxattr = ovl_listxattr,
923 .removexattr = ovl_removexattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400924 .get_acl = ovl_get_acl,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200925};