blob: 7195306e9f8436285853ebd3228cc71eef49e287 [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,
166 struct dentry *newdentry)
167{
168 ovl_dentry_version_inc(dentry->d_parent);
169 ovl_dentry_update(dentry, newdentry);
170 ovl_copyattr(newdentry->d_inode, inode);
171 d_instantiate(dentry, inode);
172}
173
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
175 struct kstat *stat, const char *link,
176 struct dentry *hardlink)
177{
178 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
179 struct inode *udir = upperdir->d_inode;
180 struct dentry *newdentry;
181 int err;
182
Al Viro59551022016-01-22 15:40:57 -0500183 inode_lock_nested(udir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200184 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
185 dentry->d_name.len);
186 err = PTR_ERR(newdentry);
187 if (IS_ERR(newdentry))
188 goto out_unlock;
189 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
190 if (err)
191 goto out_dput;
192
Vivek Goyal72e48482016-06-16 10:09:14 -0400193 ovl_instantiate(dentry, inode, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200194 newdentry = NULL;
195out_dput:
196 dput(newdentry);
197out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500198 inode_unlock(udir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199 return err;
200}
201
202static int ovl_lock_rename_workdir(struct dentry *workdir,
203 struct dentry *upperdir)
204{
205 /* Workdir should not be the same as upperdir */
206 if (workdir == upperdir)
207 goto err;
208
209 /* Workdir should not be subdir of upperdir and vice versa */
210 if (lock_rename(workdir, upperdir) != NULL)
211 goto err_unlock;
212
213 return 0;
214
215err_unlock:
216 unlock_rename(workdir, upperdir);
217err:
218 pr_err("overlayfs: failed to lock workdir+upperdir\n");
219 return -EIO;
220}
221
222static struct dentry *ovl_clear_empty(struct dentry *dentry,
223 struct list_head *list)
224{
225 struct dentry *workdir = ovl_workdir(dentry);
226 struct inode *wdir = workdir->d_inode;
227 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
228 struct inode *udir = upperdir->d_inode;
229 struct path upperpath;
230 struct dentry *upper;
231 struct dentry *opaquedir;
232 struct kstat stat;
233 int err;
234
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200235 if (WARN_ON(!workdir))
236 return ERR_PTR(-EROFS);
237
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200238 err = ovl_lock_rename_workdir(workdir, upperdir);
239 if (err)
240 goto out;
241
242 ovl_path_upper(dentry, &upperpath);
243 err = vfs_getattr(&upperpath, &stat);
244 if (err)
245 goto out_unlock;
246
247 err = -ESTALE;
248 if (!S_ISDIR(stat.mode))
249 goto out_unlock;
250 upper = upperpath.dentry;
251 if (upper->d_parent->d_inode != udir)
252 goto out_unlock;
253
254 opaquedir = ovl_lookup_temp(workdir, dentry);
255 err = PTR_ERR(opaquedir);
256 if (IS_ERR(opaquedir))
257 goto out_unlock;
258
259 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
260 if (err)
261 goto out_dput;
262
263 err = ovl_copy_xattr(upper, opaquedir);
264 if (err)
265 goto out_cleanup;
266
267 err = ovl_set_opaque(opaquedir);
268 if (err)
269 goto out_cleanup;
270
Al Viro59551022016-01-22 15:40:57 -0500271 inode_lock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272 err = ovl_set_attr(opaquedir, &stat);
Al Viro59551022016-01-22 15:40:57 -0500273 inode_unlock(opaquedir->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200274 if (err)
275 goto out_cleanup;
276
277 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
278 if (err)
279 goto out_cleanup;
280
281 ovl_cleanup_whiteouts(upper, list);
282 ovl_cleanup(wdir, upper);
283 unlock_rename(workdir, upperdir);
284
285 /* dentry's upper doesn't match now, get rid of it */
286 d_drop(dentry);
287
288 return opaquedir;
289
290out_cleanup:
291 ovl_cleanup(wdir, opaquedir);
292out_dput:
293 dput(opaquedir);
294out_unlock:
295 unlock_rename(workdir, upperdir);
296out:
297 return ERR_PTR(err);
298}
299
Miklos Szeredia105d682014-11-20 16:39:59 +0100300static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301{
302 int err;
303 struct dentry *ret = NULL;
304 LIST_HEAD(list);
305
306 err = ovl_check_empty_dir(dentry, &list);
307 if (err)
308 ret = ERR_PTR(err);
Miklos Szeredia105d682014-11-20 16:39:59 +0100309 else {
310 /*
311 * If no upperdentry then skip clearing whiteouts.
312 *
313 * Can race with copy-up, since we don't hold the upperdir
314 * mutex. Doesn't matter, since copy-up can't create a
315 * non-empty directory from an empty one.
316 */
317 if (ovl_dentry_upper(dentry))
318 ret = ovl_clear_empty(dentry, &list);
319 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320
321 ovl_cache_free(&list);
322
323 return ret;
324}
325
326static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
327 struct kstat *stat, const char *link,
328 struct dentry *hardlink)
329{
330 struct dentry *workdir = ovl_workdir(dentry);
331 struct inode *wdir = workdir->d_inode;
332 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
333 struct inode *udir = upperdir->d_inode;
334 struct dentry *upper;
335 struct dentry *newdentry;
336 int err;
337
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200338 if (WARN_ON(!workdir))
339 return -EROFS;
340
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200341 err = ovl_lock_rename_workdir(workdir, upperdir);
342 if (err)
343 goto out;
344
345 newdentry = ovl_lookup_temp(workdir, dentry);
346 err = PTR_ERR(newdentry);
347 if (IS_ERR(newdentry))
348 goto out_unlock;
349
350 upper = lookup_one_len(dentry->d_name.name, upperdir,
351 dentry->d_name.len);
352 err = PTR_ERR(upper);
353 if (IS_ERR(upper))
354 goto out_dput;
355
356 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
357 if (err)
358 goto out_dput2;
359
360 if (S_ISDIR(stat->mode)) {
361 err = ovl_set_opaque(newdentry);
362 if (err)
363 goto out_cleanup;
364
365 err = ovl_do_rename(wdir, newdentry, udir, upper,
366 RENAME_EXCHANGE);
367 if (err)
368 goto out_cleanup;
369
370 ovl_cleanup(wdir, upper);
371 } else {
372 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
373 if (err)
374 goto out_cleanup;
375 }
Vivek Goyal72e48482016-06-16 10:09:14 -0400376 ovl_instantiate(dentry, inode, newdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377 newdentry = NULL;
378out_dput2:
379 dput(upper);
380out_dput:
381 dput(newdentry);
382out_unlock:
383 unlock_rename(workdir, upperdir);
384out:
385 return err;
386
387out_cleanup:
388 ovl_cleanup(wdir, newdentry);
389 goto out_dput2;
390}
391
392static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev,
393 const char *link, struct dentry *hardlink)
394{
395 int err;
396 struct inode *inode;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400397 const struct cred *old_cred;
398 struct cred *override_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200399 struct kstat stat = {
400 .mode = mode,
401 .rdev = rdev,
402 };
403
404 err = -ENOMEM;
405 inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
406 if (!inode)
407 goto out;
408
409 err = ovl_copy_up(dentry->d_parent);
410 if (err)
411 goto out_iput;
412
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400413 old_cred = ovl_override_creds(dentry->d_sb);
414 err = -ENOMEM;
415 override_cred = prepare_creds();
416 if (override_cred) {
417 override_cred->fsuid = old_cred->fsuid;
418 override_cred->fsgid = old_cred->fsgid;
419 put_cred(override_creds(override_cred));
420 put_cred(override_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200421
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400422 if (!ovl_dentry_is_opaque(dentry))
423 err = ovl_create_upper(dentry, inode, &stat, link,
424 hardlink);
425 else
Miklos Szeredid0e13f52016-06-15 14:18:59 +0200426 err = ovl_create_over_whiteout(dentry, inode, &stat,
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400427 link, hardlink);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428 }
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400429 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430 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;
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400640 const struct cred *old_cred;
641
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200642
643 err = ovl_check_sticky(dentry);
644 if (err)
645 goto out;
646
647 err = ovl_want_write(dentry);
648 if (err)
649 goto out;
650
651 err = ovl_copy_up(dentry->d_parent);
652 if (err)
653 goto out_drop_write;
654
655 type = ovl_path_type(dentry);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400656
657 old_cred = ovl_override_creds(dentry->d_sb);
658 if (OVL_TYPE_PURE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200659 err = ovl_remove_upper(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400660 else
Miklos Szeredia105d682014-11-20 16:39:59 +0100661 err = ovl_remove_and_whiteout(dentry, is_dir);
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400662 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200663out_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
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400767 old_cred = ovl_override_creds(old->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200768
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100769 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
Miklos Szeredia105d682014-11-20 16:39:59 +0100770 opaquedir = ovl_check_empty_and_clear(new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200771 err = PTR_ERR(opaquedir);
772 if (IS_ERR(opaquedir)) {
773 opaquedir = NULL;
774 goto out_revert_creds;
775 }
776 }
777
778 if (overwrite) {
779 if (old_opaque) {
780 if (new->d_inode || !new_opaque) {
781 /* Whiteout source */
782 flags |= RENAME_WHITEOUT;
783 } else {
784 /* Switch whiteouts */
785 flags |= RENAME_EXCHANGE;
786 }
787 } else if (is_dir && !new->d_inode && new_opaque) {
788 flags |= RENAME_EXCHANGE;
789 cleanup_whiteout = true;
790 }
791 }
792
793 old_upperdir = ovl_dentry_upper(old->d_parent);
794 new_upperdir = ovl_dentry_upper(new->d_parent);
795
796 trap = lock_rename(new_upperdir, old_upperdir);
797
Miklos Szeredi11f37102016-03-21 17:31:44 +0100798
799 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
800 old->d_name.len);
801 err = PTR_ERR(olddentry);
802 if (IS_ERR(olddentry))
803 goto out_unlock;
804
805 err = -ESTALE;
806 if (olddentry != ovl_dentry_upper(old))
807 goto out_dput_old;
808
809 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
810 new->d_name.len);
811 err = PTR_ERR(newdentry);
812 if (IS_ERR(newdentry))
813 goto out_dput_old;
814
815 err = -ESTALE;
816 if (ovl_dentry_upper(new)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200817 if (opaquedir) {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100818 if (newdentry != opaquedir)
819 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200820 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100821 if (newdentry != ovl_dentry_upper(new))
822 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200823 }
824 } else {
Miklos Szeredi11f37102016-03-21 17:31:44 +0100825 if (!d_is_negative(newdentry) &&
826 (!new_opaque || !ovl_is_whiteout(newdentry)))
827 goto out_dput;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200828 }
829
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200830 if (olddentry == trap)
831 goto out_dput;
832 if (newdentry == trap)
833 goto out_dput;
834
835 if (is_dir && !old_opaque && new_opaque) {
836 err = ovl_set_opaque(olddentry);
837 if (err)
838 goto out_dput;
839 }
840 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
841 err = ovl_set_opaque(newdentry);
842 if (err)
843 goto out_dput;
844 }
845
846 if (old_opaque || new_opaque) {
847 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
848 new_upperdir->d_inode, newdentry,
849 flags);
850 } else {
851 /* No debug for the plain case */
852 BUG_ON(flags & ~RENAME_EXCHANGE);
853 err = vfs_rename(old_upperdir->d_inode, olddentry,
854 new_upperdir->d_inode, newdentry,
855 NULL, flags);
856 }
857
858 if (err) {
859 if (is_dir && !old_opaque && new_opaque)
860 ovl_remove_opaque(olddentry);
861 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
862 ovl_remove_opaque(newdentry);
863 goto out_dput;
864 }
865
866 if (is_dir && old_opaque && !new_opaque)
867 ovl_remove_opaque(olddentry);
868 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
869 ovl_remove_opaque(newdentry);
870
Konstantin Khlebnikov45d11732016-01-31 16:17:53 +0300871 /*
872 * Old dentry now lives in different location. Dentries in
873 * lowerstack are stale. We cannot drop them here because
874 * access to them is lockless. This could be only pure upper
875 * or opaque directory - numlower is zero. Or upper non-dir
876 * entry - its pureness is tracked by flag opaque.
877 */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200878 if (old_opaque != new_opaque) {
879 ovl_dentry_set_opaque(old, new_opaque);
880 if (!overwrite)
881 ovl_dentry_set_opaque(new, old_opaque);
882 }
883
884 if (cleanup_whiteout)
885 ovl_cleanup(old_upperdir->d_inode, newdentry);
886
887 ovl_dentry_version_inc(old->d_parent);
888 ovl_dentry_version_inc(new->d_parent);
889
890out_dput:
891 dput(newdentry);
Miklos Szeredi11f37102016-03-21 17:31:44 +0100892out_dput_old:
893 dput(olddentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200894out_unlock:
895 unlock_rename(new_upperdir, old_upperdir);
896out_revert_creds:
Vivek Goyal1175b6b2016-07-01 16:34:28 -0400897 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200898out_drop_write:
899 ovl_drop_write(old);
900out:
901 dput(opaquedir);
902 return err;
903}
904
905const struct inode_operations ovl_dir_inode_operations = {
906 .lookup = ovl_lookup,
907 .mkdir = ovl_mkdir,
908 .symlink = ovl_symlink,
909 .unlink = ovl_unlink,
910 .rmdir = ovl_rmdir,
911 .rename2 = ovl_rename2,
912 .link = ovl_link,
913 .setattr = ovl_setattr,
914 .create = ovl_create,
915 .mknod = ovl_mknod,
916 .permission = ovl_permission,
917 .getattr = ovl_dir_getattr,
918 .setxattr = ovl_setxattr,
919 .getxattr = ovl_getxattr,
920 .listxattr = ovl_listxattr,
921 .removexattr = ovl_removexattr,
Vivek Goyal39a25b22016-07-01 16:34:26 -0400922 .get_acl = ovl_get_acl,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200923};