hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/fs/overlayfs/readdir.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 *
34 * 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.
85 */
96
107 #include <linux/fs.h>
....@@ -289,7 +286,7 @@
289286 }
290287 inode_unlock(dir->d_inode);
291288 }
292
- ovl_revert_creds(old_cred);
289
+ ovl_revert_creds(rdd->dentry->d_sb, old_cred);
293290
294291 return err;
295292 }
....@@ -300,7 +297,7 @@
300297 struct file *realfile;
301298 int err;
302299
303
- realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
300
+ realfile = ovl_path_open(realpath, O_RDONLY | O_LARGEFILE);
304301 if (IS_ERR(realfile))
305302 return PTR_ERR(realfile);
306303
....@@ -320,18 +317,6 @@
320317 fput(realfile);
321318
322319 return err;
323
-}
324
-
325
-/*
326
- * Can we iterate real dir directly?
327
- *
328
- * Non-merge dir may contain whiteouts from a time it was a merge upper, before
329
- * lower dir was removed under it and possibly before it was rotated from upper
330
- * to lower layer.
331
- */
332
-static bool ovl_dir_is_real(struct dentry *dir)
333
-{
334
- return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
335320 }
336321
337322 static void ovl_dir_reset(struct file *file)
....@@ -441,15 +426,23 @@
441426
442427 /* Map inode number to lower fs unique range */
443428 static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
444
- const char *name, int namelen)
429
+ const char *name, int namelen, bool warn)
445430 {
446
- if (ino >> (64 - xinobits)) {
447
- pr_warn_ratelimited("overlayfs: d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
448
- namelen, name, ino, xinobits);
431
+ unsigned int xinoshift = 64 - xinobits;
432
+
433
+ if (unlikely(ino >> xinoshift)) {
434
+ if (warn) {
435
+ pr_warn_ratelimited("d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
436
+ namelen, name, ino, xinobits);
437
+ }
449438 return ino;
450439 }
451440
452
- return ino | ((u64)fsid) << (64 - xinobits);
441
+ /*
442
+ * The lowest xinobit is reserved for mapping the non-peresistent inode
443
+ * numbers range, but this range is only exposed via st_ino, not here.
444
+ */
445
+ return ino | ((u64)fsid) << (xinoshift + 1);
453446 }
454447
455448 /*
....@@ -472,7 +465,7 @@
472465 int xinobits = ovl_xino_bits(dir->d_sb);
473466 int err = 0;
474467
475
- if (!ovl_same_sb(dir->d_sb) && !xinobits)
468
+ if (!ovl_same_dev(dir->d_sb))
476469 goto out;
477470
478471 if (p->name[0] == '.') {
....@@ -518,7 +511,8 @@
518511 } else if (xinobits && !OVL_TYPE_UPPER(type)) {
519512 ino = ovl_remap_lower_ino(ino, xinobits,
520513 ovl_layer_lower(this)->fsid,
521
- p->name, p->len);
514
+ p->name, p->len,
515
+ ovl_xino_warn(dir->d_sb));
522516 }
523517
524518 out:
....@@ -527,7 +521,7 @@
527521 return err;
528522
529523 fail:
530
- pr_warn_ratelimited("overlayfs: failed to look up (%s) for ino (%i)\n",
524
+ pr_warn_ratelimited("failed to look up (%s) for ino (%i)\n",
531525 p->name, err);
532526 goto out;
533527 }
....@@ -600,6 +594,7 @@
600594 {
601595 int res;
602596 struct dentry *dentry = path->dentry;
597
+ struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
603598 struct ovl_dir_cache *cache;
604599
605600 cache = ovl_dir_cache(d_inode(dentry));
....@@ -626,7 +621,7 @@
626621 * Removing the "impure" xattr is best effort.
627622 */
628623 if (!ovl_want_write(dentry)) {
629
- ovl_do_removexattr(ovl_dentry_upper(dentry),
624
+ ovl_do_removexattr(ofs, ovl_dentry_upper(dentry),
630625 OVL_XATTR_IMPURE);
631626 ovl_drop_write(dentry);
632627 }
....@@ -648,6 +643,7 @@
648643 u64 parent_ino;
649644 int fsid;
650645 int xinobits;
646
+ bool xinowarn;
651647 };
652648
653649 static int ovl_fill_real(struct dir_context *ctx, const char *name,
....@@ -668,7 +664,7 @@
668664 ino = p->ino;
669665 } else if (rdt->xinobits) {
670666 ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
671
- name, namelen);
667
+ name, namelen, rdt->xinowarn);
672668 }
673669
674670 return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
....@@ -694,11 +690,12 @@
694690 int err;
695691 struct ovl_dir_file *od = file->private_data;
696692 struct dentry *dir = file->f_path.dentry;
697
- struct ovl_layer *lower_layer = ovl_layer_lower(dir);
693
+ const struct ovl_layer *lower_layer = ovl_layer_lower(dir);
698694 struct ovl_readdir_translate rdt = {
699695 .ctx.actor = ovl_fill_real,
700696 .orig_ctx = ctx,
701697 .xinobits = ovl_xino_bits(dir->d_sb),
698
+ .xinowarn = ovl_xino_warn(dir->d_sb),
702699 };
703700
704701 if (rdt.xinobits && lower_layer)
....@@ -735,8 +732,10 @@
735732 struct ovl_dir_file *od = file->private_data;
736733 struct dentry *dentry = file->f_path.dentry;
737734 struct ovl_cache_entry *p;
735
+ const struct cred *old_cred;
738736 int err;
739737
738
+ old_cred = ovl_override_creds(dentry->d_sb);
740739 if (!ctx->pos)
741740 ovl_dir_reset(file);
742741
....@@ -747,20 +746,23 @@
747746 * entries.
748747 */
749748 if (ovl_xino_bits(dentry->d_sb) ||
750
- (ovl_same_sb(dentry->d_sb) &&
749
+ (ovl_same_fs(dentry->d_sb) &&
751750 (ovl_is_impure_dir(file) ||
752751 OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
753
- return ovl_iterate_real(file, ctx);
752
+ err = ovl_iterate_real(file, ctx);
753
+ } else {
754
+ err = iterate_dir(od->realfile, ctx);
754755 }
755
- return iterate_dir(od->realfile, ctx);
756
+ goto out;
756757 }
757758
758759 if (!od->cache) {
759760 struct ovl_dir_cache *cache;
760761
761762 cache = ovl_cache_get(dentry);
763
+ err = PTR_ERR(cache);
762764 if (IS_ERR(cache))
763
- return PTR_ERR(cache);
765
+ goto out;
764766
765767 od->cache = cache;
766768 ovl_seek_cursor(od, ctx->pos);
....@@ -772,7 +774,7 @@
772774 if (!p->ino) {
773775 err = ovl_cache_update_ino(&file->f_path, p);
774776 if (err)
775
- return err;
777
+ goto out;
776778 }
777779 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
778780 break;
....@@ -780,7 +782,10 @@
780782 od->cursor = p->l_node.next;
781783 ctx->pos++;
782784 }
783
- return 0;
785
+ err = 0;
786
+out:
787
+ ovl_revert_creds(dentry->d_sb, old_cred);
788
+ return err;
784789 }
785790
786791 static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
....@@ -823,46 +828,76 @@
823828 return res;
824829 }
825830
826
-static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
827
- int datasync)
831
+static struct file *ovl_dir_open_realfile(const struct file *file,
832
+ struct path *realpath)
828833 {
834
+ struct file *res;
835
+ const struct cred *old_cred;
836
+
837
+ old_cred = ovl_override_creds(file_inode(file)->i_sb);
838
+ res = ovl_path_open(realpath, O_RDONLY | (file->f_flags & O_LARGEFILE));
839
+ ovl_revert_creds(file_inode(file)->i_sb, old_cred);
840
+
841
+ return res;
842
+}
843
+
844
+/*
845
+ * Like ovl_real_fdget(), returns upperfile if dir was copied up since open.
846
+ * Unlike ovl_real_fdget(), this caches upperfile in file->private_data.
847
+ *
848
+ * TODO: use same abstract type for file->private_data of dir and file so
849
+ * upperfile could also be cached for files as well.
850
+ */
851
+struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
852
+{
853
+
829854 struct ovl_dir_file *od = file->private_data;
830855 struct dentry *dentry = file->f_path.dentry;
831
- struct file *realfile = od->realfile;
856
+ struct file *old, *realfile = od->realfile;
832857
833
- /* Nothing to sync for lower */
834858 if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
835
- return 0;
859
+ return want_upper ? NULL : realfile;
836860
837861 /*
838862 * Need to check if we started out being a lower dir, but got copied up
839863 */
840864 if (!od->is_upper) {
841
- struct inode *inode = file_inode(file);
842
-
843865 realfile = READ_ONCE(od->upperfile);
844866 if (!realfile) {
845867 struct path upperpath;
846868
847869 ovl_path_upper(dentry, &upperpath);
848
- realfile = ovl_path_open(&upperpath, O_RDONLY);
870
+ realfile = ovl_dir_open_realfile(file, &upperpath);
871
+ if (IS_ERR(realfile))
872
+ return realfile;
849873
850
- inode_lock(inode);
851
- if (!od->upperfile) {
852
- if (IS_ERR(realfile)) {
853
- inode_unlock(inode);
854
- return PTR_ERR(realfile);
855
- }
856
- smp_store_release(&od->upperfile, realfile);
857
- } else {
858
- /* somebody has beaten us to it */
859
- if (!IS_ERR(realfile))
860
- fput(realfile);
861
- realfile = od->upperfile;
874
+ old = cmpxchg_release(&od->upperfile, NULL, realfile);
875
+ if (old) {
876
+ fput(realfile);
877
+ realfile = old;
862878 }
863
- inode_unlock(inode);
864879 }
865880 }
881
+
882
+ return realfile;
883
+}
884
+
885
+static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
886
+ int datasync)
887
+{
888
+ struct file *realfile;
889
+ int err;
890
+
891
+ err = ovl_sync_status(OVL_FS(file->f_path.dentry->d_sb));
892
+ if (err <= 0)
893
+ return err;
894
+
895
+ realfile = ovl_dir_real_file(file, true);
896
+ err = PTR_ERR_OR_ZERO(realfile);
897
+
898
+ /* Nothing to sync for lower */
899
+ if (!realfile || err)
900
+ return err;
866901
867902 return vfs_fsync_range(realfile, start, end, datasync);
868903 }
....@@ -896,7 +931,7 @@
896931 return -ENOMEM;
897932
898933 type = ovl_path_real(file->f_path.dentry, &realpath);
899
- realfile = ovl_path_open(&realpath, file->f_flags);
934
+ realfile = ovl_dir_open_realfile(file, &realpath);
900935 if (IS_ERR(realfile)) {
901936 kfree(od);
902937 return PTR_ERR(realfile);
....@@ -916,6 +951,10 @@
916951 .llseek = ovl_dir_llseek,
917952 .fsync = ovl_dir_fsync,
918953 .release = ovl_dir_release,
954
+ .unlocked_ioctl = ovl_ioctl,
955
+#ifdef CONFIG_COMPAT
956
+ .compat_ioctl = ovl_compat_ioctl,
957
+#endif
919958 };
920959
921960 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
....@@ -927,7 +966,7 @@
927966
928967 old_cred = ovl_override_creds(dentry->d_sb);
929968 err = ovl_dir_read_merged(dentry, list, &root);
930
- ovl_revert_creds(old_cred);
969
+ ovl_revert_creds(dentry->d_sb, old_cred);
931970 if (err)
932971 return err;
933972
....@@ -974,7 +1013,7 @@
9741013
9751014 dentry = lookup_one_len(p->name, upper, p->len);
9761015 if (IS_ERR(dentry)) {
977
- pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
1016
+ pr_err("lookup '%s/%.*s' failed (%i)\n",
9781017 upper->d_name.name, p->len, p->name,
9791018 (int) PTR_ERR(dentry));
9801019 continue;
....@@ -1022,7 +1061,9 @@
10221061 return rdd.d_type_supported;
10231062 }
10241063
1025
-static void ovl_workdir_cleanup_recurse(struct path *path, int level)
1064
+#define OVL_INCOMPATDIR_NAME "incompat"
1065
+
1066
+static int ovl_workdir_cleanup_recurse(struct path *path, int level)
10261067 {
10271068 int err;
10281069 struct inode *dir = path->dentry->d_inode;
....@@ -1036,6 +1077,19 @@
10361077 .root = &root,
10371078 .is_lowest = false,
10381079 };
1080
+ bool incompat = false;
1081
+
1082
+ /*
1083
+ * The "work/incompat" directory is treated specially - if it is not
1084
+ * empty, instead of printing a generic error and mounting read-only,
1085
+ * we will error about incompat features and fail the mount.
1086
+ *
1087
+ * When called from ovl_indexdir_cleanup(), path->dentry->d_name.name
1088
+ * starts with '#'.
1089
+ */
1090
+ if (level == 2 &&
1091
+ !strcmp(path->dentry->d_name.name, OVL_INCOMPATDIR_NAME))
1092
+ incompat = true;
10391093
10401094 err = ovl_dir_read(path, &rdd);
10411095 if (err)
....@@ -1050,27 +1104,34 @@
10501104 continue;
10511105 if (p->len == 2 && p->name[1] == '.')
10521106 continue;
1107
+ } else if (incompat) {
1108
+ pr_err("overlay with incompat feature '%s' cannot be mounted\n",
1109
+ p->name);
1110
+ err = -EINVAL;
1111
+ break;
10531112 }
10541113 dentry = lookup_one_len(p->name, path->dentry, p->len);
10551114 if (IS_ERR(dentry))
10561115 continue;
10571116 if (dentry->d_inode)
1058
- ovl_workdir_cleanup(dir, path->mnt, dentry, level);
1117
+ err = ovl_workdir_cleanup(dir, path->mnt, dentry, level);
10591118 dput(dentry);
1119
+ if (err)
1120
+ break;
10601121 }
10611122 inode_unlock(dir);
10621123 out:
10631124 ovl_cache_free(&list);
1125
+ return err;
10641126 }
10651127
1066
-void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
1128
+int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
10671129 struct dentry *dentry, int level)
10681130 {
10691131 int err;
10701132
10711133 if (!d_is_dir(dentry) || level > 1) {
1072
- ovl_cleanup(dir, dentry);
1073
- return;
1134
+ return ovl_cleanup(dir, dentry);
10741135 }
10751136
10761137 err = ovl_do_rmdir(dir, dentry);
....@@ -1078,10 +1139,13 @@
10781139 struct path path = { .mnt = mnt, .dentry = dentry };
10791140
10801141 inode_unlock(dir);
1081
- ovl_workdir_cleanup_recurse(&path, level + 1);
1142
+ err = ovl_workdir_cleanup_recurse(&path, level + 1);
10821143 inode_lock_nested(dir, I_MUTEX_PARENT);
1083
- ovl_cleanup(dir, dentry);
1144
+ if (!err)
1145
+ err = ovl_cleanup(dir, dentry);
10841146 }
1147
+
1148
+ return err;
10851149 }
10861150
10871151 int ovl_indexdir_cleanup(struct ovl_fs *ofs)
....@@ -1090,7 +1154,7 @@
10901154 struct dentry *indexdir = ofs->indexdir;
10911155 struct dentry *index = NULL;
10921156 struct inode *dir = indexdir->d_inode;
1093
- struct path path = { .mnt = ofs->upper_mnt, .dentry = indexdir };
1157
+ struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = indexdir };
10941158 LIST_HEAD(list);
10951159 struct rb_root root = RB_ROOT;
10961160 struct ovl_cache_entry *p;
....@@ -1120,6 +1184,13 @@
11201184 index = NULL;
11211185 break;
11221186 }
1187
+ /* Cleanup leftover from index create/cleanup attempt */
1188
+ if (index->d_name.name[0] == '#') {
1189
+ err = ovl_workdir_cleanup(dir, path.mnt, index, 1);
1190
+ if (err)
1191
+ break;
1192
+ goto next;
1193
+ }
11231194 err = ovl_verify_index(ofs, index);
11241195 if (!err) {
11251196 goto next;
....@@ -1138,7 +1209,7 @@
11381209 * Whiteout orphan index to block future open by
11391210 * handle after overlay nlink dropped to zero.
11401211 */
1141
- err = ovl_cleanup_and_whiteout(indexdir, dir, index);
1212
+ err = ovl_cleanup_and_whiteout(ofs, dir, index);
11421213 } else {
11431214 /* Cleanup orphan index entries */
11441215 err = ovl_cleanup(dir, index);
....@@ -1156,6 +1227,6 @@
11561227 out:
11571228 ovl_cache_free(&list);
11581229 if (err)
1159
- pr_err("overlayfs: failed index dir cleanup (%i)\n", err);
1230
+ pr_err("failed index dir cleanup (%i)\n", err);
11601231 return err;
11611232 }