1From 84d38ae3eca523ef990cb848563cc63de25266e6 Mon Sep 17 00:00:00 2001
2From: Karel Zak <kzak@redhat.com>
3Date: Fri, 19 Nov 2021 14:19:03 +0100
4Subject: [PATCH] libblkid: don't mark cache as "probed" if /sys not available
5
6For "mount --all" we need to read the cache more than once in a short
7time. The library checks the delay between probes, and if the delay is
8too short, it does not read devices. This is a problem on boot when there
9are no /sys, and the cache is empty. In this case, we need to check
10for /sys until it's available constantly.
11
12https://github.com/util-linux/util-linux/issues/1492
13Signed-off-by: Karel Zak <kzak@redhat.com>
14
15[Retrieved from:
16https://github.com/util-linux/util-linux/commit/84d38ae3eca523ef990cb848563cc63de25266e6]
17Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
18---
19 libblkid/src/devname.c | 26 +++++++++++++++++---------
20 libblkid/src/resolve.c |  2 +-
21 libblkid/src/tag.c     |  8 +++++---
22 3 files changed, 23 insertions(+), 13 deletions(-)
23
24diff --git a/libblkid/src/devname.c b/libblkid/src/devname.c
25index 90a8245fc9..9a173e3489 100644
26--- a/libblkid/src/devname.c
27+++ b/libblkid/src/devname.c
28@@ -429,6 +429,8 @@ sysfs_probe_all(blkid_cache cache, int only_if_new, int only_removable)
29 	if (!sysfs)
30 		return -BLKID_ERR_SYSFS;
31
32+	DBG(DEVNAME, ul_debug(" probe /sys/block"));
33+
34 	/* scan /sys/block */
35 	while ((dev = xreaddir(sysfs))) {
36 		DIR *dir = NULL;
37@@ -533,14 +535,18 @@ sysfs_probe_all(blkid_cache cache, int only_if_new, int only_removable)
38 /*
39  * Read the device data for all available block devices in the system.
40  */
41-static int probe_all(blkid_cache cache, int only_if_new)
42+static int probe_all(blkid_cache cache, int only_if_new, int update_interval)
43 {
44+	int rc;
45+
46 	if (!cache)
47 		return -BLKID_ERR_PARAM;
48
49 	if (cache->bic_flags & BLKID_BIC_FL_PROBED &&
50-	    time(NULL) - cache->bic_time < BLKID_PROBE_INTERVAL)
51+	    time(NULL) - cache->bic_time < BLKID_PROBE_INTERVAL) {
52+		DBG(PROBE, ul_debug("don't re-probe [delay < %d]", BLKID_PROBE_INTERVAL));
53 		return 0;
54+	}
55
56 	blkid_read_cache(cache);
57 #ifdef VG_DIR
58@@ -548,7 +554,13 @@ static int probe_all(blkid_cache cache, int only_if_new)
59 #endif
60 	ubi_probe_all(cache, only_if_new);
61
62-	sysfs_probe_all(cache, only_if_new, 0);
63+	rc = sysfs_probe_all(cache, only_if_new, 0);
64+
65+	/* Don't mark the change as "probed" if /sys not avalable */
66+	if (update_interval && rc == 0) {
67+		cache->bic_time = time(NULL);
68+		cache->bic_flags |= BLKID_BIC_FL_PROBED;
69+	}
70
71 	blkid_flush_cache(cache);
72 	return 0;
73@@ -567,11 +579,7 @@ int blkid_probe_all(blkid_cache cache)
74 	int ret;
75
76 	DBG(PROBE, ul_debug("Begin blkid_probe_all()"));
77-	ret = probe_all(cache, 0);
78-	if (ret == 0) {
79-		cache->bic_time = time(NULL);
80-		cache->bic_flags |= BLKID_BIC_FL_PROBED;
81-	}
82+	ret = probe_all(cache, 0, 1);
83 	DBG(PROBE, ul_debug("End blkid_probe_all() [rc=%d]", ret));
84 	return ret;
85 }
86@@ -589,7 +597,7 @@ int blkid_probe_all_new(blkid_cache cache)
87 	int ret;
88
89 	DBG(PROBE, ul_debug("Begin blkid_probe_all_new()"));
90-	ret = probe_all(cache, 1);
91+	ret = probe_all(cache, 1, 0);
92 	DBG(PROBE, ul_debug("End blkid_probe_all_new() [rc=%d]", ret));
93 	return ret;
94 }
95diff --git a/libblkid/src/resolve.c b/libblkid/src/resolve.c
96index 641b022860..16653fa8e1 100644
97--- a/libblkid/src/resolve.c
98+++ b/libblkid/src/resolve.c
99@@ -32,7 +32,7 @@ char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
100 	blkid_cache c = cache;
101 	char *ret = NULL;
102
103-	DBG(TAG, ul_debug("looking for %s on %s", tagname, devname));
104+	DBG(TAG, ul_debug("looking for tag %s on %s device", tagname, devname));
105
106 	if (!devname)
107 		return NULL;
108diff --git a/libblkid/src/tag.c b/libblkid/src/tag.c
109index 390a648648..178336505f 100644
110--- a/libblkid/src/tag.c
111+++ b/libblkid/src/tag.c
112@@ -326,14 +326,14 @@ blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
113 	blkid_dev	dev;
114 	int		pri;
115 	struct list_head *p;
116-	int		probe_new = 0;
117+	int		probe_new = 0, probe_all = 0;
118
119 	if (!cache || !type || !value)
120 		return NULL;
121
122 	blkid_read_cache(cache);
123
124-	DBG(TAG, ul_debug("looking for %s=%s in cache", type, value));
125+	DBG(TAG, ul_debug("looking for tag %s=%s in cache", type, value));
126
127 try_again:
128 	pri = -1;
129@@ -366,9 +366,11 @@ blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
130 		goto try_again;
131 	}
132
133-	if (!dev && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
134+	if (!dev && !probe_all
135+	    && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
136 		if (blkid_probe_all(cache) < 0)
137 			return NULL;
138+		probe_all++;
139 		goto try_again;
140 	}
141 	return dev;
142