hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/fs/overlayfs/util.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2011 Novell Inc.
34 * Copyright (C) 2016 Red Hat, 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.
85 */
96
107 #include <linux/fs.h>
....@@ -21,13 +18,13 @@
2118 int ovl_want_write(struct dentry *dentry)
2219 {
2320 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
24
- return mnt_want_write(ofs->upper_mnt);
21
+ return mnt_want_write(ovl_upper_mnt(ofs));
2522 }
2623
2724 void ovl_drop_write(struct dentry *dentry)
2825 {
2926 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
30
- mnt_drop_write(ofs->upper_mnt);
27
+ mnt_drop_write(ovl_upper_mnt(ofs));
3128 }
3229
3330 struct dentry *ovl_workdir(struct dentry *dentry)
....@@ -45,28 +42,10 @@
4542 return override_creds(ofs->creator_cred);
4643 }
4744
48
-void ovl_revert_creds(const struct cred *old_cred)
45
+void ovl_revert_creds(struct super_block *sb, const struct cred *old_cred)
4946 {
5047 if (old_cred)
5148 revert_creds(old_cred);
52
-}
53
-
54
-ssize_t ovl_vfs_getxattr(struct dentry *dentry, const char *name, void *buf,
55
- size_t size)
56
-{
57
- return __vfs_getxattr(dentry, d_inode(dentry), name, buf, size);
58
-}
59
-
60
-struct super_block *ovl_same_sb(struct super_block *sb)
61
-{
62
- struct ovl_fs *ofs = sb->s_fs_info;
63
-
64
- if (!ofs->numlowerfs)
65
- return ofs->upper_mnt->mnt_sb;
66
- else if (ofs->numlowerfs == 1 && !ofs->upper_mnt)
67
- return ofs->lower_fs[0].sb;
68
- else
69
- return NULL;
7049 }
7150
7251 /*
....@@ -79,8 +58,7 @@
7958 */
8059 int ovl_can_decode_fh(struct super_block *sb)
8160 {
82
- if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry ||
83
- uuid_is_null(&sb->s_uuid))
61
+ if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
8462 return 0;
8563
8664 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
....@@ -120,11 +98,43 @@
12098 return oe;
12199 }
122100
101
+#define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
102
+
123103 bool ovl_dentry_remote(struct dentry *dentry)
124104 {
125
- return dentry->d_flags &
126
- (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
127
- DCACHE_OP_REAL);
105
+ return dentry->d_flags & OVL_D_REVALIDATE;
106
+}
107
+
108
+void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
109
+{
110
+ if (!ovl_dentry_remote(realdentry))
111
+ return;
112
+
113
+ spin_lock(&dentry->d_lock);
114
+ dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
115
+ spin_unlock(&dentry->d_lock);
116
+}
117
+
118
+void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry)
119
+{
120
+ return ovl_dentry_init_flags(dentry, upperdentry, OVL_D_REVALIDATE);
121
+}
122
+
123
+void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
124
+ unsigned int mask)
125
+{
126
+ struct ovl_entry *oe = OVL_E(dentry);
127
+ unsigned int i, flags = 0;
128
+
129
+ if (upperdentry)
130
+ flags |= upperdentry->d_flags;
131
+ for (i = 0; i < oe->numlower; i++)
132
+ flags |= oe->lowerstack[i].dentry->d_flags;
133
+
134
+ spin_lock(&dentry->d_lock);
135
+ dentry->d_flags &= ~mask;
136
+ dentry->d_flags |= flags & mask;
137
+ spin_unlock(&dentry->d_lock);
128138 }
129139
130140 bool ovl_dentry_weird(struct dentry *dentry)
....@@ -164,7 +174,7 @@
164174 {
165175 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
166176
167
- path->mnt = ofs->upper_mnt;
177
+ path->mnt = ovl_upper_mnt(ofs);
168178 path->dentry = ovl_dentry_upper(dentry);
169179 }
170180
....@@ -216,7 +226,7 @@
216226 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
217227 }
218228
219
-struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
229
+const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
220230 {
221231 struct ovl_entry *oe = dentry->d_fsdata;
222232
....@@ -416,24 +426,6 @@
416426 oi->redirect = redirect;
417427 }
418428
419
-void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
420
- struct dentry *lowerdentry, struct dentry *lowerdata)
421
-{
422
- struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
423
-
424
- if (upperdentry)
425
- OVL_I(inode)->__upperdentry = upperdentry;
426
- if (lowerdentry)
427
- OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
428
- if (lowerdata)
429
- OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
430
-
431
- ovl_copyattr(realinode, inode);
432
- ovl_copyflags(realinode, inode);
433
- if (!inode->i_ino)
434
- inode->i_ino = realinode->i_ino;
435
-}
436
-
437429 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
438430 {
439431 struct inode *upperinode = d_inode(upperdentry);
....@@ -446,25 +438,25 @@
446438 smp_wmb();
447439 OVL_I(inode)->__upperdentry = upperdentry;
448440 if (inode_unhashed(inode)) {
449
- if (!inode->i_ino)
450
- inode->i_ino = upperinode->i_ino;
451441 inode->i_private = upperinode;
452442 __insert_inode_hash(inode, (unsigned long) upperinode);
453443 }
454444 }
455445
456
-static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
446
+static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
457447 {
458448 struct inode *inode = d_inode(dentry);
459449
460450 WARN_ON(!inode_is_locked(inode));
451
+ WARN_ON(!d_is_dir(dentry));
461452 /*
462
- * Version is used by readdir code to keep cache consistent. For merge
463
- * dirs all changes need to be noted. For non-merge dirs, cache only
464
- * contains impure (ones which have been copied up and have origins)
465
- * entries, so only need to note changes to impure entries.
453
+ * Version is used by readdir code to keep cache consistent.
454
+ * For merge dirs (or dirs with origin) all changes need to be noted.
455
+ * For non-merge dirs, cache contains only impure entries (i.e. ones
456
+ * which have been copied up and have origins), so only need to note
457
+ * changes to impure entries.
466458 */
467
- if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
459
+ if (!ovl_dir_is_real(dentry) || impurity)
468460 OVL_I(inode)->version++;
469461 }
470462
....@@ -473,7 +465,7 @@
473465 /* Copy mtime/ctime */
474466 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
475467
476
- ovl_dentry_version_inc(dentry, impurity);
468
+ ovl_dir_version_inc(dentry, impurity);
477469 }
478470
479471 u64 ovl_dentry_version_get(struct dentry *dentry)
....@@ -493,7 +485,32 @@
493485
494486 struct file *ovl_path_open(struct path *path, int flags)
495487 {
496
- return dentry_open(path, flags | O_NOATIME, current_cred());
488
+ struct inode *inode = d_inode(path->dentry);
489
+ int err, acc_mode;
490
+
491
+ if (flags & ~(O_ACCMODE | O_LARGEFILE))
492
+ BUG();
493
+
494
+ switch (flags & O_ACCMODE) {
495
+ case O_RDONLY:
496
+ acc_mode = MAY_READ;
497
+ break;
498
+ case O_WRONLY:
499
+ acc_mode = MAY_WRITE;
500
+ break;
501
+ default:
502
+ BUG();
503
+ }
504
+
505
+ err = inode_permission(inode, acc_mode | MAY_OPEN);
506
+ if (err)
507
+ return ERR_PTR(err);
508
+
509
+ /* O_NOATIME is an optimization, don't fail if not permitted */
510
+ if (inode_owner_or_capable(inode))
511
+ flags |= O_NOATIME;
512
+
513
+ return dentry_open(path, flags, current_cred());
497514 }
498515
499516 /* Caller should hold ovl_inode->lock */
....@@ -536,13 +553,13 @@
536553
537554 int ovl_copy_up_start(struct dentry *dentry, int flags)
538555 {
539
- struct ovl_inode *oi = OVL_I(d_inode(dentry));
556
+ struct inode *inode = d_inode(dentry);
540557 int err;
541558
542
- err = mutex_lock_interruptible(&oi->lock);
559
+ err = ovl_inode_lock_interruptible(inode);
543560 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
544561 err = 1; /* Already copied up */
545
- mutex_unlock(&oi->lock);
562
+ ovl_inode_unlock(inode);
546563 }
547564
548565 return err;
....@@ -550,14 +567,14 @@
550567
551568 void ovl_copy_up_end(struct dentry *dentry)
552569 {
553
- mutex_unlock(&OVL_I(d_inode(dentry))->lock);
570
+ ovl_inode_unlock(d_inode(dentry));
554571 }
555572
556
-bool ovl_check_origin_xattr(struct dentry *dentry)
573
+bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
557574 {
558575 ssize_t res;
559576
560
- res = ovl_vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
577
+ res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
561578
562579 /* Zero size value means "copied up but origin unknown" */
563580 if (res >= 0)
....@@ -566,7 +583,8 @@
566583 return false;
567584 }
568585
569
-bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
586
+bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
587
+ enum ovl_xattr ox)
570588 {
571589 ssize_t res;
572590 char val;
....@@ -574,15 +592,36 @@
574592 if (!d_is_dir(dentry))
575593 return false;
576594
577
- res = ovl_vfs_getxattr(dentry, name, &val, 1);
595
+ res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
578596 if (res == 1 && val == 'y')
579597 return true;
580598
581599 return false;
582600 }
583601
602
+#define OVL_XATTR_OPAQUE_POSTFIX "opaque"
603
+#define OVL_XATTR_REDIRECT_POSTFIX "redirect"
604
+#define OVL_XATTR_ORIGIN_POSTFIX "origin"
605
+#define OVL_XATTR_IMPURE_POSTFIX "impure"
606
+#define OVL_XATTR_NLINK_POSTFIX "nlink"
607
+#define OVL_XATTR_UPPER_POSTFIX "upper"
608
+#define OVL_XATTR_METACOPY_POSTFIX "metacopy"
609
+
610
+#define OVL_XATTR_TAB_ENTRY(x) \
611
+ [x] = OVL_XATTR_PREFIX x ## _POSTFIX
612
+
613
+const char *ovl_xattr_table[] = {
614
+ OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
615
+ OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
616
+ OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
617
+ OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
618
+ OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
619
+ OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
620
+ OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
621
+};
622
+
584623 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
585
- const char *name, const void *value, size_t size,
624
+ enum ovl_xattr ox, const void *value, size_t size,
586625 int xerr)
587626 {
588627 int err;
....@@ -591,10 +630,10 @@
591630 if (ofs->noxattr)
592631 return xerr;
593632
594
- err = ovl_do_setxattr(upperdentry, name, value, size, 0);
633
+ err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
595634
596635 if (err == -EOPNOTSUPP) {
597
- pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
636
+ pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
598637 ofs->noxattr = true;
599638 return xerr;
600639 }
....@@ -619,21 +658,6 @@
619658 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
620659
621660 return err;
622
-}
623
-
624
-void ovl_set_flag(unsigned long flag, struct inode *inode)
625
-{
626
- set_bit(flag, &OVL_I(inode)->flags);
627
-}
628
-
629
-void ovl_clear_flag(unsigned long flag, struct inode *inode)
630
-{
631
- clear_bit(flag, &OVL_I(inode)->flags);
632
-}
633
-
634
-bool ovl_test_flag(unsigned long flag, struct inode *inode)
635
-{
636
- return test_bit(flag, &OVL_I(inode)->flags);
637661 }
638662
639663 /**
....@@ -718,7 +742,7 @@
718742
719743 inode = d_inode(upperdentry);
720744 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
721
- pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
745
+ pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
722746 upperdentry, inode->i_ino, inode->i_nlink);
723747 /*
724748 * We either have a bug with persistent union nlink or a lower
....@@ -741,7 +765,8 @@
741765 index = NULL;
742766 } else if (ovl_index_all(dentry->d_sb)) {
743767 /* Whiteout orphan index to block future open by handle */
744
- err = ovl_cleanup_and_whiteout(indexdir, dir, index);
768
+ err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
769
+ dir, index);
745770 } else {
746771 /* Cleanup orphan index entries */
747772 err = ovl_cleanup(dir, index);
....@@ -757,7 +782,7 @@
757782 return;
758783
759784 fail:
760
- pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
785
+ pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
761786 goto out;
762787 }
763788
....@@ -765,14 +790,14 @@
765790 * Operations that change overlay inode and upper inode nlink need to be
766791 * synchronized with copy up for persistent nlink accounting.
767792 */
768
-int ovl_nlink_start(struct dentry *dentry, bool *locked)
793
+int ovl_nlink_start(struct dentry *dentry)
769794 {
770
- struct ovl_inode *oi = OVL_I(d_inode(dentry));
795
+ struct inode *inode = d_inode(dentry);
771796 const struct cred *old_cred;
772797 int err;
773798
774
- if (!d_inode(dentry))
775
- return 0;
799
+ if (WARN_ON(!inode))
800
+ return -ENOENT;
776801
777802 /*
778803 * With inodes index is enabled, we store the union overlay nlink
....@@ -794,11 +819,11 @@
794819 return err;
795820 }
796821
797
- err = mutex_lock_interruptible(&oi->lock);
822
+ err = ovl_inode_lock_interruptible(inode);
798823 if (err)
799824 return err;
800825
801
- if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
826
+ if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
802827 goto out;
803828
804829 old_cred = ovl_override_creds(dentry->d_sb);
....@@ -809,31 +834,28 @@
809834 * value relative to the upper inode nlink in an upper inode xattr.
810835 */
811836 err = ovl_set_nlink_upper(dentry);
812
- ovl_revert_creds(old_cred);
837
+ ovl_revert_creds(dentry->d_sb, old_cred);
813838
814839 out:
815840 if (err)
816
- mutex_unlock(&oi->lock);
817
- else
818
- *locked = true;
841
+ ovl_inode_unlock(inode);
819842
820843 return err;
821844 }
822845
823
-void ovl_nlink_end(struct dentry *dentry, bool locked)
846
+void ovl_nlink_end(struct dentry *dentry)
824847 {
825
- if (locked) {
826
- if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
827
- d_inode(dentry)->i_nlink == 0) {
828
- const struct cred *old_cred;
848
+ struct inode *inode = d_inode(dentry);
829849
830
- old_cred = ovl_override_creds(dentry->d_sb);
831
- ovl_cleanup_index(dentry);
832
- ovl_revert_creds(old_cred);
833
- }
850
+ if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
851
+ const struct cred *old_cred;
834852
835
- mutex_unlock(&OVL_I(d_inode(dentry))->lock);
853
+ old_cred = ovl_override_creds(dentry->d_sb);
854
+ ovl_cleanup_index(dentry);
855
+ ovl_revert_creds(dentry->d_sb, old_cred);
836856 }
857
+
858
+ ovl_inode_unlock(inode);
837859 }
838860
839861 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
....@@ -851,12 +873,12 @@
851873 err_unlock:
852874 unlock_rename(workdir, upperdir);
853875 err:
854
- pr_err("overlayfs: failed to lock workdir+upperdir\n");
876
+ pr_err("failed to lock workdir+upperdir\n");
855877 return -EIO;
856878 }
857879
858880 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
859
-int ovl_check_metacopy_xattr(struct dentry *dentry)
881
+int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
860882 {
861883 ssize_t res;
862884
....@@ -864,7 +886,7 @@
864886 if (!S_ISREG(d_inode(dentry)->i_mode))
865887 return 0;
866888
867
- res = ovl_vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
889
+ res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
868890 if (res < 0) {
869891 if (res == -ENODATA || res == -EOPNOTSUPP)
870892 return 0;
....@@ -873,7 +895,7 @@
873895
874896 return 1;
875897 out:
876
- pr_warn_ratelimited("overlayfs: failed to get metacopy (%zi)\n", res);
898
+ pr_warn_ratelimited("failed to get metacopy (%zi)\n", res);
877899 return res;
878900 }
879901
....@@ -893,49 +915,27 @@
893915 return (oe->numlower > 1);
894916 }
895917
896
-ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
897
- size_t padding)
898
-{
899
- ssize_t res;
900
- char *buf = NULL;
901
-
902
- res = ovl_vfs_getxattr(dentry, name, NULL, 0);
903
- if (res < 0) {
904
- if (res == -ENODATA || res == -EOPNOTSUPP)
905
- return -ENODATA;
906
- goto fail;
907
- }
908
-
909
- if (res != 0) {
910
- buf = kzalloc(res + padding, GFP_KERNEL);
911
- if (!buf)
912
- return -ENOMEM;
913
-
914
- res = ovl_vfs_getxattr(dentry, name, buf, res);
915
- if (res < 0)
916
- goto fail;
917
- }
918
- *value = buf;
919
-
920
- return res;
921
-
922
-fail:
923
- pr_warn_ratelimited("overlayfs: failed to get xattr %s: err=%zi)\n",
924
- name, res);
925
- kfree(buf);
926
- return res;
927
-}
928
-
929
-char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
918
+char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
919
+ int padding)
930920 {
931921 int res;
932922 char *s, *next, *buf = NULL;
933923
934
- res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
935
- if (res == -ENODATA)
924
+ res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
925
+ if (res == -ENODATA || res == -EOPNOTSUPP)
936926 return NULL;
937927 if (res < 0)
938
- return ERR_PTR(res);
928
+ goto fail;
929
+ if (res == 0)
930
+ goto invalid;
931
+
932
+ buf = kzalloc(res + padding + 1, GFP_KERNEL);
933
+ if (!buf)
934
+ return ERR_PTR(-ENOMEM);
935
+
936
+ res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
937
+ if (res < 0)
938
+ goto fail;
939939 if (res == 0)
940940 goto invalid;
941941
....@@ -952,8 +952,39 @@
952952
953953 return buf;
954954 invalid:
955
- pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
955
+ pr_warn_ratelimited("invalid redirect (%s)\n", buf);
956956 res = -EINVAL;
957
+ goto err_free;
958
+fail:
959
+ pr_warn_ratelimited("failed to get redirect (%i)\n", res);
960
+err_free:
957961 kfree(buf);
958962 return ERR_PTR(res);
959963 }
964
+
965
+/*
966
+ * ovl_sync_status() - Check fs sync status for volatile mounts
967
+ *
968
+ * Returns 1 if this is not a volatile mount and a real sync is required.
969
+ *
970
+ * Returns 0 if syncing can be skipped because mount is volatile, and no errors
971
+ * have occurred on the upperdir since the mount.
972
+ *
973
+ * Returns -errno if it is a volatile mount, and the error that occurred since
974
+ * the last mount. If the error code changes, it'll return the latest error
975
+ * code.
976
+ */
977
+
978
+int ovl_sync_status(struct ovl_fs *ofs)
979
+{
980
+ struct vfsmount *mnt;
981
+
982
+ if (ovl_should_sync(ofs))
983
+ return 1;
984
+
985
+ mnt = ovl_upper_mnt(ofs);
986
+ if (!mnt)
987
+ return 0;
988
+
989
+ return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
990
+}