summaryrefslogtreecommitdiff
path: root/src/basic/blockdev-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-06-11 12:06:27 +0200
committerLennart Poettering <lennart@poettering.net>2018-06-11 18:01:06 +0200
commit880f09bd91b84580fb61da3561d3830f8a951aa9 (patch)
treebc471ac41089111a212799fe8dc70ca99b0028bb /src/basic/blockdev-util.c
parent66ae5130a0ecf72b42503abe68c865e97ab512fd (diff)
downloadsystemd-880f09bd91b84580fb61da3561d3830f8a951aa9.tar.gz
blockdev: split out actual DM sysfs code of get_block_device_harder() into function of its own
That way we can use it in code that already acquired a dev_t from some source.
Diffstat (limited to 'src/basic/blockdev-util.c')
-rw-r--r--src/basic/blockdev-util.c65
1 files changed, 31 insertions, 34 deletions
diff --git a/src/basic/blockdev-util.c b/src/basic/blockdev-util.c
index 6495a71658..0b090bb2ed 100644
--- a/src/basic/blockdev-util.c
+++ b/src/basic/blockdev-util.c
@@ -85,35 +85,22 @@ int get_block_device(const char *path, dev_t *dev) {
return 0;
}
-int get_block_device_harder(const char *path, dev_t *dev) {
+int block_get_originating(dev_t dt, dev_t *ret) {
_cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ char *t = NULL;
char p[SYS_BLOCK_PATH_MAX("/slaves")];
struct dirent *de, *found = NULL;
- const char *q;
unsigned maj, min;
- dev_t dt;
+ const char *q;
int r;
- assert(path);
- assert(dev);
-
- /* Gets the backing block device for a file system, and
- * handles LUKS encrypted file systems, looking for its
- * immediate parent, if there is one. */
-
- r = get_block_device(path, &dt);
- if (r <= 0)
- return r;
+ /* For the specified block device tries to chase it through the layers, in case LUKS-style DM stacking is used,
+ * trying to find the next underlying layer. */
xsprintf_sys_block_path(p, "/slaves", dt);
d = opendir(p);
- if (!d) {
- if (errno == ENOENT)
- goto fallback;
-
+ if (!d)
return -errno;
- }
FOREACH_DIRENT_ALL(de, d, return -errno) {
@@ -141,34 +128,28 @@ int get_block_device_harder(const char *path, dev_t *dev) {
return -ENOMEM;
r = read_one_line_file(u, &a);
- if (r < 0) {
- log_debug_errno(r, "Failed to read %s: %m", u);
- goto fallback;
- }
+ if (r < 0)
+ return log_debug_errno(r, "Failed to read %s: %m", u);
r = read_one_line_file(v, &b);
- if (r < 0) {
- log_debug_errno(r, "Failed to read %s: %m", v);
- goto fallback;
- }
+ if (r < 0)
+ return log_debug_errno(r, "Failed to read %s: %m", v);
/* Check if the parent device is the same. If not, then the two backing devices are on
* different physical devices, and we don't support that. */
if (!streq(a, b))
- goto fallback;
+ return -ENOTUNIQ;
}
found = de;
}
if (!found)
- goto fallback;
+ return -ENOENT;
q = strjoina(p, "/", found->d_name, "/dev");
r = read_one_line_file(q, &t);
- if (r == -ENOENT)
- goto fallback;
if (r < 0)
return r;
@@ -176,12 +157,28 @@ int get_block_device_harder(const char *path, dev_t *dev) {
return -EINVAL;
if (maj == 0)
- goto fallback;
+ return -ENOENT;
- *dev = makedev(maj, min);
+ *ret = makedev(maj, min);
return 1;
+}
+
+int get_block_device_harder(const char *path, dev_t *ret) {
+ int r;
+
+ assert(path);
+ assert(ret);
+
+ /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking for its
+ * immediate parent, if there is one. */
+
+ r = get_block_device(path, ret);
+ if (r <= 0)
+ return r;
+
+ r = block_get_originating(*ret, ret);
+ if (r < 0)
+ log_debug_errno(r, "Failed to chase block device '%s', ignoring: %m", path);
-fallback:
- *dev = dt;
return 1;
}