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