summaryrefslogtreecommitdiff
path: root/src/util/virdevmapper.c
blob: 33cf3e202b9f746e757a4798577390dd4110609a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * virdevmapper.c: Functions for handling device mapper
 *
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "virdevmapper.h"
#include "internal.h"

#ifdef __linux__
# include <sys/sysmacros.h>
# include <linux/dm-ioctl.h>
# include <sys/ioctl.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>

# include "virthread.h"
# include "viralloc.h"
# include "virstring.h"
# include "virfile.h"
# include "virlog.h"
# include "virglibutil.h"

# define VIR_FROM_THIS VIR_FROM_STORAGE

VIR_LOG_INIT("util.virdevmapper");

# define PROC_DEVICES "/proc/devices"
# define PROC_DEVICES_BUF_SIZE (16 * 1024)
# define DM_NAME "device-mapper"
# define DEV_DM_DIR "/dev/" DM_DIR
# define CONTROL_PATH DEV_DM_DIR "/" DM_CONTROL_NODE

# define VIR_DEVMAPPER_IOCTL_BUF_SIZE_INCREMENT (16 * 1024)
G_STATIC_ASSERT(VIR_DEVMAPPER_IOCTL_BUF_SIZE_INCREMENT > sizeof(struct dm_ioctl));


static int
virDevMapperGetMajor(unsigned int *major)
{
    g_autofree char *buf = NULL;
    g_auto(GStrv) lines = NULL;
    size_t i;

    if (!virFileExists(CONTROL_PATH))
        return -2;

    if (virFileReadAll(PROC_DEVICES, PROC_DEVICES_BUF_SIZE, &buf) < 0)
        return -1;

    lines = g_strsplit(buf, "\n", 0);
    if (!lines)
        return -1;

    for (i = 0; lines[i]; i++) {
        g_autofree char *dev = NULL;
        unsigned int maj;

        if (sscanf(lines[i], "%u %ms\n", &maj, &dev) == 2 &&
            STREQ(dev, DM_NAME)) {
            *major = maj;
            break;
        }
    }

    if (!lines[i]) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to find major for %1$s"),
                       DM_NAME);
        return -1;
    }

    return 0;
}


static void *
virDMIoctl(int controlFD, int cmd, struct dm_ioctl *dm, char **buf)
{
    size_t bufsize = VIR_DEVMAPPER_IOCTL_BUF_SIZE_INCREMENT;

 reread:
    *buf = g_new0(char, bufsize);

    dm->version[0] = DM_VERSION_MAJOR;
    dm->version[1] = 0;
    dm->version[2] = 0;
    dm->data_size = bufsize;
    dm->data_start = sizeof(struct dm_ioctl);

    memcpy(*buf, dm, sizeof(struct dm_ioctl));

    if (ioctl(controlFD, cmd, *buf) < 0) {
        VIR_FREE(*buf);
        return NULL;
    }

    memcpy(dm, *buf, sizeof(struct dm_ioctl));

    if (dm->flags & DM_BUFFER_FULL_FLAG) {
        bufsize += VIR_DEVMAPPER_IOCTL_BUF_SIZE_INCREMENT;
        VIR_FREE(*buf);
        goto reread;
    }

    return *buf + dm->data_start;
}


static int
virDMOpen(void)
{
    VIR_AUTOCLOSE controlFD = -1;
    struct dm_ioctl dm;
    g_autofree char *tmp = NULL;
    int ret;

    memset(&dm, 0, sizeof(dm));

    if ((controlFD = open(CONTROL_PATH, O_RDWR)) < 0) {
        /* We can't talk to devmapper. Produce a warning and let
         * the caller decide what to do next. */
        if (errno == ENOENT) {
            VIR_DEBUG("device mapper not available");
        } else {
            VIR_WARN("unable to open %s: %s",
                     CONTROL_PATH, g_strerror(errno));
        }
        return -2;
    }

    if (!virDMIoctl(controlFD, DM_VERSION, &dm, &tmp)) {
        virReportSystemError(errno, "%s",
                             _("Unable to get device-mapper version"));
        return -1;
    }

    if (dm.version[0] != DM_VERSION_MAJOR) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
                       _("Unsupported device-mapper version. Expected %1$d got %2$d"),
                       DM_VERSION_MAJOR, dm.version[0]);
        return -1;
    }

    ret = controlFD;
    controlFD = -1;
    return ret;
}


static char *
virDMSanitizepath(const char *path)
{
    g_autofree char *dmDirPath = NULL;
    struct dirent *ent = NULL;
    struct stat sb[2];
    g_autoptr(DIR) dh = NULL;
    const char *p;

    /* If a path is NOT provided then assume it's DM name */
    p = strrchr(path, '/');

    if (!p)
        return g_strdup(path);
    else
        p++;

    /* It's a path. Check if the last component is DM name */
    if (stat(path, &sb[0]) < 0) {
        virReportError(errno,
                       _("Unable to stat %1$p"),
                       path);
        return NULL;
    }

    dmDirPath = g_strdup_printf(DEV_DM_DIR "/%s", p);

    if (stat(dmDirPath, &sb[1]) == 0 &&
        sb[0].st_rdev == sb[1].st_rdev) {
        return g_strdup(p);
    }

    /* The last component of @path wasn't DM name. Let's check if
     * there's a device under /dev/mapper/ with the same rdev. */
    if (virDirOpen(&dh, DEV_DM_DIR) < 0)
        return NULL;

    while (virDirRead(dh, &ent, DEV_DM_DIR) > 0) {
        g_autofree char *tmp = g_strdup_printf(DEV_DM_DIR "/%s", ent->d_name);

        if (stat(tmp, &sb[1]) == 0 &&
            sb[0].st_rdev == sb[1].st_rdev) {
            return g_steal_pointer(&tmp);
        }
    }

    return NULL;
}


static int
virDevMapperGetTargetsImpl(int controlFD,
                           const char *path,
                           GSList **devPaths,
                           unsigned int ttl)
{
    g_autofree char *sanitizedPath = NULL;
    g_autofree char *buf = NULL;
    struct dm_ioctl dm;
    struct dm_target_deps *deps = NULL;
    size_t i;

    memset(&dm, 0, sizeof(dm));

    if (ttl == 0) {
        errno = ELOOP;
        return -1;
    }

    if (!virIsDevMapperDevice(path))
        return 0;

    if (!(sanitizedPath = virDMSanitizepath(path)))
        return 0;

    if (virStrcpy(dm.name, sanitizedPath, DM_NAME_LEN) < 0) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                       _("Resolved device mapper name too long"));
        return -1;
    }

    deps = virDMIoctl(controlFD, DM_TABLE_DEPS, &dm, &buf);
    if (!deps) {
        if (errno == ENXIO)
            return 0;

        virReportSystemError(errno,
                             _("Unable to query dependencies for %1$s"),
                             path);
        return -1;
    }

    for (i = 0; i < deps->count; i++) {
        char *curpath = g_strdup_printf("/dev/block/%u:%u",
                                        major(deps->dev[i]),
                                        minor(deps->dev[i]));

        *devPaths = g_slist_prepend(*devPaths, curpath);

        if (virDevMapperGetTargetsImpl(controlFD, curpath, devPaths, ttl - 1) < 0)
            return -1;
    }

    return 0;
}


/**
 * virDevMapperGetTargets:
 * @path: devmapper target
 * @devPaths: filled in by a GSList containing the paths
 *
 * For given @path figure out its targets, and store them in
 * @devPaths.
 *
 * If @path is not a devmapper device, @devPaths is set to NULL and
 * success is returned.
 *
 * If @path consists of yet another devmapper targets these are
 * consulted recursively.
 *
 * Returns 0 on success,
 *        -1 otherwise (with errno set, no libvirt error is
 *        reported)
 */
int
virDevMapperGetTargets(const char *path,
                       GSList **devPaths)
{
    VIR_AUTOCLOSE controlFD = -1;
    const unsigned int ttl = 32;
    g_autoptr(virGSListString) paths = NULL;

    /* Arbitrary limit on recursion level. A devmapper target can
     * consist of devices or yet another targets. If that's the
     * case, we have to stop recursion somewhere. */

    if ((controlFD = virDMOpen()) < 0) {
        if (controlFD == -2) {
            /* The CONTROL_PATH doesn't exist or is unusable.
             * Probably the module isn't loaded, yet. Don't error
             * out, just exit. */
            return 0;
        }

        return -1;
    }

    if (virDevMapperGetTargetsImpl(controlFD, path, &paths, ttl) < 0)
        return -1;

    *devPaths = g_slist_reverse(g_steal_pointer(&paths));
    return 0;
}


bool
virIsDevMapperDevice(const char *dev_name)
{
    struct stat buf;
    unsigned int major;

    if (virDevMapperGetMajor(&major) < 0)
        return false;

    if (!stat(dev_name, &buf) &&
        S_ISBLK(buf.st_mode) &&
        major(buf.st_rdev) == major)
        return true;

    return false;
}

#else /* !defined(__linux__)  */

int
virDevMapperGetTargets(const char *path G_GNUC_UNUSED,
                       GSList **devPaths G_GNUC_UNUSED)
{
    errno = ENOSYS;
    return -1;
}


bool
virIsDevMapperDevice(const char *dev_name G_GNUC_UNUSED)
{
    return false;
}
#endif /* ! defined(__linux__) */