1 /*
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 <linux/module.h>
16 #include <linux/posix_acl.h>
17 #include <linux/posix_acl_xattr.h>
18 #include <linux/atomic.h>
19 #include <linux/ratelimit.h>
20 #include "overlayfs.h"
21
22 static unsigned short ovl_redirect_max = 256;
23 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
24 MODULE_PARM_DESC(ovl_redirect_max,
25 "Maximum length of absolute redirect xattr value");
26
27 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
28
ovl_cleanup(struct inode * wdir,struct dentry * wdentry)29 int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
30 {
31 int err;
32
33 dget(wdentry);
34 if (d_is_dir(wdentry))
35 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 }
44
45 return err;
46 }
47
ovl_lookup_temp(struct dentry * workdir)48 static struct dentry *ovl_lookup_temp(struct dentry *workdir)
49 {
50 struct dentry *temp;
51 char name[20];
52 static atomic_t temp_id = ATOMIC_INIT(0);
53
54 /* counter is allowed to wrap, since temp dentries are ephemeral */
55 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
56
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 */
ovl_whiteout(struct dentry * workdir)68 static struct dentry *ovl_whiteout(struct dentry *workdir)
69 {
70 int err;
71 struct dentry *whiteout;
72 struct inode *wdir = workdir->d_inode;
73
74 whiteout = ovl_lookup_temp(workdir);
75 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
87 /* Caller must hold i_mutex on both workdir and dir */
ovl_cleanup_and_whiteout(struct dentry * workdir,struct inode * dir,struct dentry * dentry)88 int 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
110 out:
111 dput(whiteout);
112 return err;
113
114 kill_whiteout:
115 ovl_cleanup(wdir, whiteout);
116 goto out;
117 }
118
ovl_mkdir_real(struct inode * dir,struct dentry ** newdentry,umode_t mode)119 int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry, umode_t mode)
120 {
121 int err;
122 struct dentry *d, *dentry = *newdentry;
123
124 err = ovl_do_mkdir(dir, dentry, mode);
125 if (err)
126 return err;
127
128 if (likely(!d_unhashed(dentry)))
129 return 0;
130
131 /*
132 * vfs_mkdir() may succeed and leave the dentry passed
133 * to it unhashed and negative. If that happens, try to
134 * lookup a new hashed and positive dentry.
135 */
136 d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
137 dentry->d_name.len);
138 if (IS_ERR(d)) {
139 pr_warn("overlayfs: failed lookup after mkdir (%pd2, err=%i).\n",
140 dentry, err);
141 return PTR_ERR(d);
142 }
143 dput(dentry);
144 *newdentry = d;
145
146 return 0;
147 }
148
ovl_create_real(struct inode * dir,struct dentry * newdentry,struct ovl_cattr * attr)149 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
150 struct ovl_cattr *attr)
151 {
152 int err;
153
154 if (IS_ERR(newdentry))
155 return newdentry;
156
157 err = -ESTALE;
158 if (newdentry->d_inode)
159 goto out;
160
161 if (attr->hardlink) {
162 err = ovl_do_link(attr->hardlink, dir, newdentry);
163 } else {
164 switch (attr->mode & S_IFMT) {
165 case S_IFREG:
166 err = ovl_do_create(dir, newdentry, attr->mode);
167 break;
168
169 case S_IFDIR:
170 /* mkdir is special... */
171 err = ovl_mkdir_real(dir, &newdentry, attr->mode);
172 break;
173
174 case S_IFCHR:
175 case S_IFBLK:
176 case S_IFIFO:
177 case S_IFSOCK:
178 err = ovl_do_mknod(dir, newdentry, attr->mode,
179 attr->rdev);
180 break;
181
182 case S_IFLNK:
183 err = ovl_do_symlink(dir, newdentry, attr->link);
184 break;
185
186 default:
187 err = -EPERM;
188 }
189 }
190 if (!err && WARN_ON(!newdentry->d_inode)) {
191 /*
192 * Not quite sure if non-instantiated dentry is legal or not.
193 * VFS doesn't seem to care so check and warn here.
194 */
195 err = -EIO;
196 }
197 out:
198 if (err) {
199 dput(newdentry);
200 return ERR_PTR(err);
201 }
202 return newdentry;
203 }
204
ovl_create_temp(struct dentry * workdir,struct ovl_cattr * attr)205 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
206 {
207 return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
208 attr);
209 }
210
ovl_set_opaque_xerr(struct dentry * dentry,struct dentry * upper,int xerr)211 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
212 int xerr)
213 {
214 int err;
215
216 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
217 if (!err)
218 ovl_dentry_set_opaque(dentry);
219
220 return err;
221 }
222
ovl_set_opaque(struct dentry * dentry,struct dentry * upperdentry)223 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
224 {
225 /*
226 * Fail with -EIO when trying to create opaque dir and upper doesn't
227 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
228 * return a specific error for noxattr case.
229 */
230 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
231 }
232
233 /*
234 * Common operations required to be done after creation of file on upper.
235 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
236 * may not use to instantiate the new dentry.
237 */
ovl_instantiate(struct dentry * dentry,struct inode * inode,struct dentry * newdentry,bool hardlink)238 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
239 struct dentry *newdentry, bool hardlink)
240 {
241 struct ovl_inode_params oip = {
242 .upperdentry = newdentry,
243 .newinode = inode,
244 };
245
246 ovl_dir_modified(dentry->d_parent, false);
247 ovl_dentry_set_upper_alias(dentry);
248 if (!hardlink) {
249 /*
250 * ovl_obtain_alias() can be called after ovl_create_real()
251 * and before we get here, so we may get an inode from cache
252 * with the same real upperdentry that is not the inode we
253 * pre-allocated. In this case we will use the cached inode
254 * to instantiate the new dentry.
255 *
256 * XXX: if we ever use ovl_obtain_alias() to decode directory
257 * file handles, need to use ovl_get_inode_locked() and
258 * d_instantiate_new() here to prevent from creating two
259 * hashed directory inode aliases.
260 */
261 inode = ovl_get_inode(dentry->d_sb, &oip);
262 if (IS_ERR(inode))
263 return PTR_ERR(inode);
264 } else {
265 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
266 dput(newdentry);
267 inc_nlink(inode);
268 }
269
270 d_instantiate(dentry, inode);
271 if (inode != oip.newinode) {
272 pr_warn_ratelimited("overlayfs: newly created inode found in cache (%pd2)\n",
273 dentry);
274 }
275
276 /* Force lookup of new upper hardlink to find its lower */
277 if (hardlink)
278 d_drop(dentry);
279
280 return 0;
281 }
282
ovl_type_merge(struct dentry * dentry)283 static bool ovl_type_merge(struct dentry *dentry)
284 {
285 return OVL_TYPE_MERGE(ovl_path_type(dentry));
286 }
287
ovl_type_origin(struct dentry * dentry)288 static bool ovl_type_origin(struct dentry *dentry)
289 {
290 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
291 }
292
ovl_create_upper(struct dentry * dentry,struct inode * inode,struct ovl_cattr * attr)293 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
294 struct ovl_cattr *attr)
295 {
296 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
297 struct inode *udir = upperdir->d_inode;
298 struct dentry *newdentry;
299 int err;
300
301 if (!attr->hardlink && !IS_POSIXACL(udir))
302 attr->mode &= ~current_umask();
303
304 inode_lock_nested(udir, I_MUTEX_PARENT);
305 newdentry = ovl_create_real(udir,
306 lookup_one_len(dentry->d_name.name,
307 upperdir,
308 dentry->d_name.len),
309 attr);
310 err = PTR_ERR(newdentry);
311 if (IS_ERR(newdentry))
312 goto out_unlock;
313
314 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
315 /* Setting opaque here is just an optimization, allow to fail */
316 ovl_set_opaque(dentry, newdentry);
317 }
318
319 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
320 if (err)
321 goto out_cleanup;
322 out_unlock:
323 inode_unlock(udir);
324 return err;
325
326 out_cleanup:
327 ovl_cleanup(udir, newdentry);
328 dput(newdentry);
329 goto out_unlock;
330 }
331
ovl_clear_empty(struct dentry * dentry,struct list_head * list)332 static struct dentry *ovl_clear_empty(struct dentry *dentry,
333 struct list_head *list)
334 {
335 struct dentry *workdir = ovl_workdir(dentry);
336 struct inode *wdir = workdir->d_inode;
337 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
338 struct inode *udir = upperdir->d_inode;
339 struct path upperpath;
340 struct dentry *upper;
341 struct dentry *opaquedir;
342 struct kstat stat;
343 int err;
344
345 if (WARN_ON(!workdir))
346 return ERR_PTR(-EROFS);
347
348 err = ovl_lock_rename_workdir(workdir, upperdir);
349 if (err)
350 goto out;
351
352 ovl_path_upper(dentry, &upperpath);
353 err = vfs_getattr(&upperpath, &stat,
354 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
355 if (err)
356 goto out_unlock;
357
358 err = -ESTALE;
359 if (!S_ISDIR(stat.mode))
360 goto out_unlock;
361 upper = upperpath.dentry;
362 if (upper->d_parent->d_inode != udir)
363 goto out_unlock;
364
365 opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
366 err = PTR_ERR(opaquedir);
367 if (IS_ERR(opaquedir))
368 goto out_unlock;
369
370 err = ovl_copy_xattr(upper, opaquedir);
371 if (err)
372 goto out_cleanup;
373
374 err = ovl_set_opaque(dentry, opaquedir);
375 if (err)
376 goto out_cleanup;
377
378 inode_lock(opaquedir->d_inode);
379 err = ovl_set_attr(opaquedir, &stat);
380 inode_unlock(opaquedir->d_inode);
381 if (err)
382 goto out_cleanup;
383
384 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
385 if (err)
386 goto out_cleanup;
387
388 ovl_cleanup_whiteouts(upper, list);
389 ovl_cleanup(wdir, upper);
390 unlock_rename(workdir, upperdir);
391
392 /* dentry's upper doesn't match now, get rid of it */
393 d_drop(dentry);
394
395 return opaquedir;
396
397 out_cleanup:
398 ovl_cleanup(wdir, opaquedir);
399 dput(opaquedir);
400 out_unlock:
401 unlock_rename(workdir, upperdir);
402 out:
403 return ERR_PTR(err);
404 }
405
ovl_set_upper_acl(struct dentry * upperdentry,const char * name,const struct posix_acl * acl)406 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
407 const struct posix_acl *acl)
408 {
409 void *buffer;
410 size_t size;
411 int err;
412
413 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
414 return 0;
415
416 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
417 buffer = kmalloc(size, GFP_KERNEL);
418 if (!buffer)
419 return -ENOMEM;
420
421 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
422 err = size;
423 if (err < 0)
424 goto out_free;
425
426 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
427 out_free:
428 kfree(buffer);
429 return err;
430 }
431
ovl_create_over_whiteout(struct dentry * dentry,struct inode * inode,struct ovl_cattr * cattr)432 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
433 struct ovl_cattr *cattr)
434 {
435 struct dentry *workdir = ovl_workdir(dentry);
436 struct inode *wdir = workdir->d_inode;
437 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
438 struct inode *udir = upperdir->d_inode;
439 struct dentry *upper;
440 struct dentry *newdentry;
441 int err;
442 struct posix_acl *acl, *default_acl;
443 bool hardlink = !!cattr->hardlink;
444
445 if (WARN_ON(!workdir))
446 return -EROFS;
447
448 if (!hardlink) {
449 err = posix_acl_create(dentry->d_parent->d_inode,
450 &cattr->mode, &default_acl, &acl);
451 if (err)
452 return err;
453 }
454
455 err = ovl_lock_rename_workdir(workdir, upperdir);
456 if (err)
457 goto out;
458
459 upper = lookup_one_len(dentry->d_name.name, upperdir,
460 dentry->d_name.len);
461 err = PTR_ERR(upper);
462 if (IS_ERR(upper))
463 goto out_unlock;
464
465 err = -ESTALE;
466 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
467 goto out_dput;
468
469 newdentry = ovl_create_temp(workdir, cattr);
470 err = PTR_ERR(newdentry);
471 if (IS_ERR(newdentry))
472 goto out_dput;
473
474 /*
475 * mode could have been mutilated due to umask (e.g. sgid directory)
476 */
477 if (!hardlink &&
478 !S_ISLNK(cattr->mode) &&
479 newdentry->d_inode->i_mode != cattr->mode) {
480 struct iattr attr = {
481 .ia_valid = ATTR_MODE,
482 .ia_mode = cattr->mode,
483 };
484 inode_lock(newdentry->d_inode);
485 err = notify_change(newdentry, &attr, NULL);
486 inode_unlock(newdentry->d_inode);
487 if (err)
488 goto out_cleanup;
489 }
490 if (!hardlink) {
491 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
492 acl);
493 if (err)
494 goto out_cleanup;
495
496 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
497 default_acl);
498 if (err)
499 goto out_cleanup;
500 }
501
502 if (!hardlink && S_ISDIR(cattr->mode)) {
503 err = ovl_set_opaque(dentry, newdentry);
504 if (err)
505 goto out_cleanup;
506
507 err = ovl_do_rename(wdir, newdentry, udir, upper,
508 RENAME_EXCHANGE);
509 if (err)
510 goto out_cleanup;
511
512 ovl_cleanup(wdir, upper);
513 } else {
514 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
515 if (err)
516 goto out_cleanup;
517 }
518 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
519 if (err) {
520 ovl_cleanup(udir, newdentry);
521 dput(newdentry);
522 }
523 out_dput:
524 dput(upper);
525 out_unlock:
526 unlock_rename(workdir, upperdir);
527 out:
528 if (!hardlink) {
529 posix_acl_release(acl);
530 posix_acl_release(default_acl);
531 }
532 return err;
533
534 out_cleanup:
535 ovl_cleanup(wdir, newdentry);
536 dput(newdentry);
537 goto out_dput;
538 }
539
ovl_create_or_link(struct dentry * dentry,struct inode * inode,struct ovl_cattr * attr,bool origin)540 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
541 struct ovl_cattr *attr, bool origin)
542 {
543 int err;
544 const struct cred *old_cred;
545 struct cred *override_cred;
546 struct dentry *parent = dentry->d_parent;
547
548 err = ovl_copy_up(parent);
549 if (err)
550 return err;
551
552 old_cred = ovl_override_creds(dentry->d_sb);
553
554 /*
555 * When linking a file with copy up origin into a new parent, mark the
556 * new parent dir "impure".
557 */
558 if (origin) {
559 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
560 if (err)
561 goto out_revert_creds;
562 }
563
564 if (!attr->hardlink) {
565 err = -ENOMEM;
566 override_cred = prepare_creds();
567 if (!override_cred)
568 goto out_revert_creds;
569 /*
570 * In the creation cases(create, mkdir, mknod, symlink),
571 * ovl should transfer current's fs{u,g}id to underlying
572 * fs. Because underlying fs want to initialize its new
573 * inode owner using current's fs{u,g}id. And in this
574 * case, the @inode is a new inode that is initialized
575 * in inode_init_owner() to current's fs{u,g}id. So use
576 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
577 *
578 * But in the other hardlink case, ovl_link() does not
579 * create a new inode, so just use the ovl mounter's
580 * fs{u,g}id.
581 */
582 override_cred->fsuid = inode->i_uid;
583 override_cred->fsgid = inode->i_gid;
584 err = security_dentry_create_files_as(dentry,
585 attr->mode, &dentry->d_name, old_cred,
586 override_cred);
587 if (err) {
588 put_cred(override_cred);
589 goto out_revert_creds;
590 }
591 put_cred(override_creds(override_cred));
592 put_cred(override_cred);
593 }
594
595 if (!ovl_dentry_is_whiteout(dentry))
596 err = ovl_create_upper(dentry, inode, attr);
597 else
598 err = ovl_create_over_whiteout(dentry, inode, attr);
599
600 out_revert_creds:
601 revert_creds(old_cred);
602 return err;
603 }
604
ovl_create_object(struct dentry * dentry,int mode,dev_t rdev,const char * link)605 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
606 const char *link)
607 {
608 int err;
609 struct inode *inode;
610 struct ovl_cattr attr = {
611 .rdev = rdev,
612 .link = link,
613 };
614
615 err = ovl_want_write(dentry);
616 if (err)
617 goto out;
618
619 /* Preallocate inode to be used by ovl_get_inode() */
620 err = -ENOMEM;
621 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
622 if (!inode)
623 goto out_drop_write;
624
625 spin_lock(&inode->i_lock);
626 inode->i_state |= I_CREATING;
627 spin_unlock(&inode->i_lock);
628
629 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
630 attr.mode = inode->i_mode;
631
632 err = ovl_create_or_link(dentry, inode, &attr, false);
633 /* Did we end up using the preallocated inode? */
634 if (inode != d_inode(dentry))
635 iput(inode);
636
637 out_drop_write:
638 ovl_drop_write(dentry);
639 out:
640 return err;
641 }
642
ovl_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)643 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
644 bool excl)
645 {
646 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
647 }
648
ovl_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)649 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
650 {
651 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
652 }
653
ovl_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)654 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
655 dev_t rdev)
656 {
657 /* Don't allow creation of "whiteout" on overlay */
658 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
659 return -EPERM;
660
661 return ovl_create_object(dentry, mode, rdev, NULL);
662 }
663
ovl_symlink(struct inode * dir,struct dentry * dentry,const char * link)664 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
665 const char *link)
666 {
667 return ovl_create_object(dentry, S_IFLNK, 0, link);
668 }
669
ovl_set_link_redirect(struct dentry * dentry)670 static int ovl_set_link_redirect(struct dentry *dentry)
671 {
672 const struct cred *old_cred;
673 int err;
674
675 old_cred = ovl_override_creds(dentry->d_sb);
676 err = ovl_set_redirect(dentry, false);
677 revert_creds(old_cred);
678
679 return err;
680 }
681
ovl_link(struct dentry * old,struct inode * newdir,struct dentry * new)682 static int ovl_link(struct dentry *old, struct inode *newdir,
683 struct dentry *new)
684 {
685 int err;
686 bool locked = false;
687 struct inode *inode;
688
689 err = ovl_want_write(old);
690 if (err)
691 goto out;
692
693 err = ovl_copy_up(old);
694 if (err)
695 goto out_drop_write;
696
697 err = ovl_copy_up(new->d_parent);
698 if (err)
699 goto out_drop_write;
700
701 if (ovl_is_metacopy_dentry(old)) {
702 err = ovl_set_link_redirect(old);
703 if (err)
704 goto out_drop_write;
705 }
706
707 err = ovl_nlink_start(old, &locked);
708 if (err)
709 goto out_drop_write;
710
711 inode = d_inode(old);
712 ihold(inode);
713
714 err = ovl_create_or_link(new, inode,
715 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
716 ovl_type_origin(old));
717 if (err)
718 iput(inode);
719
720 ovl_nlink_end(old, locked);
721 out_drop_write:
722 ovl_drop_write(old);
723 out:
724 return err;
725 }
726
ovl_matches_upper(struct dentry * dentry,struct dentry * upper)727 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
728 {
729 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
730 }
731
ovl_remove_and_whiteout(struct dentry * dentry,struct list_head * list)732 static int ovl_remove_and_whiteout(struct dentry *dentry,
733 struct list_head *list)
734 {
735 struct dentry *workdir = ovl_workdir(dentry);
736 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
737 struct dentry *upper;
738 struct dentry *opaquedir = NULL;
739 int err;
740
741 if (WARN_ON(!workdir))
742 return -EROFS;
743
744 if (!list_empty(list)) {
745 opaquedir = ovl_clear_empty(dentry, list);
746 err = PTR_ERR(opaquedir);
747 if (IS_ERR(opaquedir))
748 goto out;
749 }
750
751 err = ovl_lock_rename_workdir(workdir, upperdir);
752 if (err)
753 goto out_dput;
754
755 upper = lookup_one_len(dentry->d_name.name, upperdir,
756 dentry->d_name.len);
757 err = PTR_ERR(upper);
758 if (IS_ERR(upper))
759 goto out_unlock;
760
761 err = -ESTALE;
762 if ((opaquedir && upper != opaquedir) ||
763 (!opaquedir && ovl_dentry_upper(dentry) &&
764 !ovl_matches_upper(dentry, upper))) {
765 goto out_dput_upper;
766 }
767
768 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
769 if (err)
770 goto out_d_drop;
771
772 ovl_dir_modified(dentry->d_parent, true);
773 out_d_drop:
774 d_drop(dentry);
775 out_dput_upper:
776 dput(upper);
777 out_unlock:
778 unlock_rename(workdir, upperdir);
779 out_dput:
780 dput(opaquedir);
781 out:
782 return err;
783 }
784
ovl_remove_upper(struct dentry * dentry,bool is_dir,struct list_head * list)785 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
786 struct list_head *list)
787 {
788 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
789 struct inode *dir = upperdir->d_inode;
790 struct dentry *upper;
791 struct dentry *opaquedir = NULL;
792 int err;
793
794 if (!list_empty(list)) {
795 opaquedir = ovl_clear_empty(dentry, list);
796 err = PTR_ERR(opaquedir);
797 if (IS_ERR(opaquedir))
798 goto out;
799 }
800
801 inode_lock_nested(dir, I_MUTEX_PARENT);
802 upper = lookup_one_len(dentry->d_name.name, upperdir,
803 dentry->d_name.len);
804 err = PTR_ERR(upper);
805 if (IS_ERR(upper))
806 goto out_unlock;
807
808 err = -ESTALE;
809 if ((opaquedir && upper != opaquedir) ||
810 (!opaquedir && !ovl_matches_upper(dentry, upper)))
811 goto out_dput_upper;
812
813 if (is_dir)
814 err = vfs_rmdir(dir, upper);
815 else
816 err = vfs_unlink(dir, upper, NULL);
817 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
818
819 /*
820 * Keeping this dentry hashed would mean having to release
821 * upperpath/lowerpath, which could only be done if we are the
822 * sole user of this dentry. Too tricky... Just unhash for
823 * now.
824 */
825 if (!err)
826 d_drop(dentry);
827 out_dput_upper:
828 dput(upper);
829 out_unlock:
830 inode_unlock(dir);
831 dput(opaquedir);
832 out:
833 return err;
834 }
835
ovl_pure_upper(struct dentry * dentry)836 static bool ovl_pure_upper(struct dentry *dentry)
837 {
838 return !ovl_dentry_lower(dentry) &&
839 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
840 }
841
ovl_do_remove(struct dentry * dentry,bool is_dir)842 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
843 {
844 int err;
845 bool locked = false;
846 const struct cred *old_cred;
847 struct dentry *upperdentry;
848 bool lower_positive = ovl_lower_positive(dentry);
849 LIST_HEAD(list);
850
851 /* No need to clean pure upper removed by vfs_rmdir() */
852 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
853 err = ovl_check_empty_dir(dentry, &list);
854 if (err)
855 goto out;
856 }
857
858 err = ovl_want_write(dentry);
859 if (err)
860 goto out;
861
862 err = ovl_copy_up(dentry->d_parent);
863 if (err)
864 goto out_drop_write;
865
866 err = ovl_nlink_start(dentry, &locked);
867 if (err)
868 goto out_drop_write;
869
870 old_cred = ovl_override_creds(dentry->d_sb);
871 if (!lower_positive)
872 err = ovl_remove_upper(dentry, is_dir, &list);
873 else
874 err = ovl_remove_and_whiteout(dentry, &list);
875 revert_creds(old_cred);
876 if (!err) {
877 if (is_dir)
878 clear_nlink(dentry->d_inode);
879 else
880 drop_nlink(dentry->d_inode);
881 }
882 ovl_nlink_end(dentry, locked);
883
884 /*
885 * Copy ctime
886 *
887 * Note: we fail to update ctime if there was no copy-up, only a
888 * whiteout
889 */
890 upperdentry = ovl_dentry_upper(dentry);
891 if (upperdentry)
892 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
893
894 out_drop_write:
895 ovl_drop_write(dentry);
896 out:
897 ovl_cache_free(&list);
898 return err;
899 }
900
ovl_unlink(struct inode * dir,struct dentry * dentry)901 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
902 {
903 return ovl_do_remove(dentry, false);
904 }
905
ovl_rmdir(struct inode * dir,struct dentry * dentry)906 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
907 {
908 return ovl_do_remove(dentry, true);
909 }
910
ovl_type_merge_or_lower(struct dentry * dentry)911 static bool ovl_type_merge_or_lower(struct dentry *dentry)
912 {
913 enum ovl_path_type type = ovl_path_type(dentry);
914
915 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
916 }
917
ovl_can_move(struct dentry * dentry)918 static bool ovl_can_move(struct dentry *dentry)
919 {
920 return ovl_redirect_dir(dentry->d_sb) ||
921 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
922 }
923
ovl_get_redirect(struct dentry * dentry,bool abs_redirect)924 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
925 {
926 char *buf, *ret;
927 struct dentry *d, *tmp;
928 int buflen = ovl_redirect_max + 1;
929
930 if (!abs_redirect) {
931 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
932 GFP_KERNEL);
933 goto out;
934 }
935
936 buf = ret = kmalloc(buflen, GFP_KERNEL);
937 if (!buf)
938 goto out;
939
940 buflen--;
941 buf[buflen] = '\0';
942 for (d = dget(dentry); !IS_ROOT(d);) {
943 const char *name;
944 int thislen;
945
946 spin_lock(&d->d_lock);
947 name = ovl_dentry_get_redirect(d);
948 if (name) {
949 thislen = strlen(name);
950 } else {
951 name = d->d_name.name;
952 thislen = d->d_name.len;
953 }
954
955 /* If path is too long, fall back to userspace move */
956 if (thislen + (name[0] != '/') > buflen) {
957 ret = ERR_PTR(-EXDEV);
958 spin_unlock(&d->d_lock);
959 goto out_put;
960 }
961
962 buflen -= thislen;
963 memcpy(&buf[buflen], name, thislen);
964 spin_unlock(&d->d_lock);
965 tmp = dget_parent(d);
966
967 dput(d);
968 d = tmp;
969
970 /* Absolute redirect: finished */
971 if (buf[buflen] == '/')
972 break;
973 buflen--;
974 buf[buflen] = '/';
975 }
976 ret = kstrdup(&buf[buflen], GFP_KERNEL);
977 out_put:
978 dput(d);
979 kfree(buf);
980 out:
981 return ret ? ret : ERR_PTR(-ENOMEM);
982 }
983
ovl_need_absolute_redirect(struct dentry * dentry,bool samedir)984 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
985 {
986 struct dentry *lowerdentry;
987
988 if (!samedir)
989 return true;
990
991 if (d_is_dir(dentry))
992 return false;
993
994 /*
995 * For non-dir hardlinked files, we need absolute redirects
996 * in general as two upper hardlinks could be in different
997 * dirs. We could put a relative redirect now and convert
998 * it to absolute redirect later. But when nlink > 1 and
999 * indexing is on, that means relative redirect needs to be
1000 * converted to absolute during copy up of another lower
1001 * hardllink as well.
1002 *
1003 * So without optimizing too much, just check if lower is
1004 * a hard link or not. If lower is hard link, put absolute
1005 * redirect.
1006 */
1007 lowerdentry = ovl_dentry_lower(dentry);
1008 return (d_inode(lowerdentry)->i_nlink > 1);
1009 }
1010
ovl_set_redirect(struct dentry * dentry,bool samedir)1011 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1012 {
1013 int err;
1014 const char *redirect = ovl_dentry_get_redirect(dentry);
1015 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1016
1017 if (redirect && (!absolute_redirect || redirect[0] == '/'))
1018 return 0;
1019
1020 redirect = ovl_get_redirect(dentry, absolute_redirect);
1021 if (IS_ERR(redirect))
1022 return PTR_ERR(redirect);
1023
1024 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
1025 OVL_XATTR_REDIRECT,
1026 redirect, strlen(redirect), -EXDEV);
1027 if (!err) {
1028 spin_lock(&dentry->d_lock);
1029 ovl_dentry_set_redirect(dentry, redirect);
1030 spin_unlock(&dentry->d_lock);
1031 } else {
1032 kfree(redirect);
1033 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
1034 err);
1035 /* Fall back to userspace copy-up */
1036 err = -EXDEV;
1037 }
1038 return err;
1039 }
1040
ovl_rename(struct inode * olddir,struct dentry * old,struct inode * newdir,struct dentry * new,unsigned int flags)1041 static int ovl_rename(struct inode *olddir, struct dentry *old,
1042 struct inode *newdir, struct dentry *new,
1043 unsigned int flags)
1044 {
1045 int err;
1046 bool locked = false;
1047 struct dentry *old_upperdir;
1048 struct dentry *new_upperdir;
1049 struct dentry *olddentry;
1050 struct dentry *newdentry;
1051 struct dentry *trap;
1052 bool old_opaque;
1053 bool new_opaque;
1054 bool cleanup_whiteout = false;
1055 bool overwrite = !(flags & RENAME_EXCHANGE);
1056 bool is_dir = d_is_dir(old);
1057 bool new_is_dir = d_is_dir(new);
1058 bool samedir = olddir == newdir;
1059 struct dentry *opaquedir = NULL;
1060 const struct cred *old_cred = NULL;
1061 LIST_HEAD(list);
1062
1063 err = -EINVAL;
1064 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1065 goto out;
1066
1067 flags &= ~RENAME_NOREPLACE;
1068
1069 /* Don't copy up directory trees */
1070 err = -EXDEV;
1071 if (!ovl_can_move(old))
1072 goto out;
1073 if (!overwrite && !ovl_can_move(new))
1074 goto out;
1075
1076 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1077 err = ovl_check_empty_dir(new, &list);
1078 if (err)
1079 goto out;
1080 }
1081
1082 if (overwrite) {
1083 if (ovl_lower_positive(old)) {
1084 if (!ovl_dentry_is_whiteout(new)) {
1085 /* Whiteout source */
1086 flags |= RENAME_WHITEOUT;
1087 } else {
1088 /* Switch whiteouts */
1089 flags |= RENAME_EXCHANGE;
1090 }
1091 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1092 flags |= RENAME_EXCHANGE;
1093 cleanup_whiteout = true;
1094 }
1095 }
1096
1097 err = ovl_want_write(old);
1098 if (err)
1099 goto out;
1100
1101 err = ovl_copy_up(old);
1102 if (err)
1103 goto out_drop_write;
1104
1105 err = ovl_copy_up(new->d_parent);
1106 if (err)
1107 goto out_drop_write;
1108 if (!overwrite) {
1109 err = ovl_copy_up(new);
1110 if (err)
1111 goto out_drop_write;
1112 } else {
1113 err = ovl_nlink_start(new, &locked);
1114 if (err)
1115 goto out_drop_write;
1116 }
1117
1118 old_cred = ovl_override_creds(old->d_sb);
1119
1120 if (!list_empty(&list)) {
1121 opaquedir = ovl_clear_empty(new, &list);
1122 err = PTR_ERR(opaquedir);
1123 if (IS_ERR(opaquedir)) {
1124 opaquedir = NULL;
1125 goto out_revert_creds;
1126 }
1127 }
1128
1129 old_upperdir = ovl_dentry_upper(old->d_parent);
1130 new_upperdir = ovl_dentry_upper(new->d_parent);
1131
1132 if (!samedir) {
1133 /*
1134 * When moving a merge dir or non-dir with copy up origin into
1135 * a new parent, we are marking the new parent dir "impure".
1136 * When ovl_iterate() iterates an "impure" upper dir, it will
1137 * lookup the origin inodes of the entries to fill d_ino.
1138 */
1139 if (ovl_type_origin(old)) {
1140 err = ovl_set_impure(new->d_parent, new_upperdir);
1141 if (err)
1142 goto out_revert_creds;
1143 }
1144 if (!overwrite && ovl_type_origin(new)) {
1145 err = ovl_set_impure(old->d_parent, old_upperdir);
1146 if (err)
1147 goto out_revert_creds;
1148 }
1149 }
1150
1151 trap = lock_rename(new_upperdir, old_upperdir);
1152
1153 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1154 old->d_name.len);
1155 err = PTR_ERR(olddentry);
1156 if (IS_ERR(olddentry))
1157 goto out_unlock;
1158
1159 err = -ESTALE;
1160 if (!ovl_matches_upper(old, olddentry))
1161 goto out_dput_old;
1162
1163 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1164 new->d_name.len);
1165 err = PTR_ERR(newdentry);
1166 if (IS_ERR(newdentry))
1167 goto out_dput_old;
1168
1169 old_opaque = ovl_dentry_is_opaque(old);
1170 new_opaque = ovl_dentry_is_opaque(new);
1171
1172 err = -ESTALE;
1173 if (d_inode(new) && ovl_dentry_upper(new)) {
1174 if (opaquedir) {
1175 if (newdentry != opaquedir)
1176 goto out_dput;
1177 } else {
1178 if (!ovl_matches_upper(new, newdentry))
1179 goto out_dput;
1180 }
1181 } else {
1182 if (!d_is_negative(newdentry)) {
1183 if (!new_opaque || !ovl_is_whiteout(newdentry))
1184 goto out_dput;
1185 } else {
1186 if (flags & RENAME_EXCHANGE)
1187 goto out_dput;
1188 }
1189 }
1190
1191 if (olddentry == trap)
1192 goto out_dput;
1193 if (newdentry == trap)
1194 goto out_dput;
1195
1196 if (olddentry->d_inode == newdentry->d_inode)
1197 goto out_dput;
1198
1199 err = 0;
1200 if (ovl_type_merge_or_lower(old))
1201 err = ovl_set_redirect(old, samedir);
1202 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1203 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1204 if (err)
1205 goto out_dput;
1206
1207 if (!overwrite && ovl_type_merge_or_lower(new))
1208 err = ovl_set_redirect(new, samedir);
1209 else if (!overwrite && new_is_dir && !new_opaque &&
1210 ovl_type_merge(old->d_parent))
1211 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1212 if (err)
1213 goto out_dput;
1214
1215 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1216 new_upperdir->d_inode, newdentry, flags);
1217 if (err)
1218 goto out_dput;
1219
1220 if (cleanup_whiteout)
1221 ovl_cleanup(old_upperdir->d_inode, newdentry);
1222
1223 if (overwrite && d_inode(new)) {
1224 if (new_is_dir)
1225 clear_nlink(d_inode(new));
1226 else
1227 drop_nlink(d_inode(new));
1228 }
1229
1230 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1231 (!overwrite && ovl_type_origin(new)));
1232 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1233 (d_inode(new) && ovl_type_origin(new)));
1234
1235 /* copy ctime: */
1236 ovl_copyattr(d_inode(olddentry), d_inode(old));
1237 if (d_inode(new) && ovl_dentry_upper(new))
1238 ovl_copyattr(d_inode(newdentry), d_inode(new));
1239
1240 out_dput:
1241 dput(newdentry);
1242 out_dput_old:
1243 dput(olddentry);
1244 out_unlock:
1245 unlock_rename(new_upperdir, old_upperdir);
1246 out_revert_creds:
1247 revert_creds(old_cred);
1248 ovl_nlink_end(new, locked);
1249 out_drop_write:
1250 ovl_drop_write(old);
1251 out:
1252 dput(opaquedir);
1253 ovl_cache_free(&list);
1254 return err;
1255 }
1256
1257 const struct inode_operations ovl_dir_inode_operations = {
1258 .lookup = ovl_lookup,
1259 .mkdir = ovl_mkdir,
1260 .symlink = ovl_symlink,
1261 .unlink = ovl_unlink,
1262 .rmdir = ovl_rmdir,
1263 .rename = ovl_rename,
1264 .link = ovl_link,
1265 .setattr = ovl_setattr,
1266 .create = ovl_create,
1267 .mknod = ovl_mknod,
1268 .permission = ovl_permission,
1269 .getattr = ovl_getattr,
1270 .listxattr = ovl_listxattr,
1271 .get_acl = ovl_get_acl,
1272 .update_time = ovl_update_time,
1273 };
1274