Devmapper.cpp 8.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
18
#include <stdlib.h>
19 20 21 22
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
Peter Bohm's avatar
Peter Bohm committed
23
#include <stdlib.h>
24 25 26 27 28

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

San Mehat's avatar
San Mehat committed
29 30
#include <linux/kdev_t.h>

31 32 33 34
#define LOG_TAG "Vold"

#include <cutils/log.h>

San Mehat's avatar
San Mehat committed
35 36
#include <sysutils/SocketClient.h>

37 38
#include "Devmapper.h"

Peter Bohm's avatar
Peter Bohm committed
39 40
#define DEVMAPPER_BUFFER_SIZE 4096

San Mehat's avatar
San Mehat committed
41 42 43 44
int Devmapper::dumpState(SocketClient *c) {

    char *buffer = (char *) malloc(1024 * 64);
    if (!buffer) {
San Mehat's avatar
San Mehat committed
45
        SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehat's avatar
San Mehat committed
46 47 48 49
        return -1;
    }
    memset(buffer, 0, (1024 * 64));

Peter Bohm's avatar
Peter Bohm committed
50
    char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
San Mehat's avatar
San Mehat committed
51
    if (!buffer2) {
San Mehat's avatar
San Mehat committed
52
        SLOGE("Error allocating memory (%s)", strerror(errno));
San Mehat's avatar
San Mehat committed
53 54 55 56 57 58
        free(buffer);
        return -1;
    }

    int fd;
    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
San Mehat's avatar
San Mehat committed
59
        SLOGE("Error opening devmapper (%s)", strerror(errno));
San Mehat's avatar
San Mehat committed
60 61 62 63 64 65 66 67 68
        free(buffer);
        free(buffer2);
        return -1;
    }

    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
    ioctlInit(io, (1024 * 64), NULL, 0);

    if (ioctl(fd, DM_LIST_DEVICES, io)) {
San Mehat's avatar
San Mehat committed
69
        SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
San Mehat's avatar
San Mehat committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        free(buffer);
        free(buffer2);
        close(fd);
        return -1;
    }

    struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
    if (!n->dev) {
        free(buffer);
        free(buffer2);
        close(fd);
        return 0;
    }

    unsigned nxt = 0;
    do {
        n = (struct dm_name_list *) (((char *) n) + nxt);

Peter Bohm's avatar
Peter Bohm committed
88
        memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
San Mehat's avatar
San Mehat committed
89
        struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
Peter Bohm's avatar
Peter Bohm committed
90
        ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
San Mehat's avatar
San Mehat committed
91 92
        if (ioctl(fd, DM_DEV_STATUS, io2)) {
            if (errno != ENXIO) {
San Mehat's avatar
San Mehat committed
93
                SLOGE("DM_DEV_STATUS ioctl failed (%s)", strerror(errno));
San Mehat's avatar
San Mehat committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            }
            io2 = NULL;
        }

        char *tmp;
        if (!io2) {
            asprintf(&tmp, "%s %llu:%llu (no status available)", n->name, MAJOR(n->dev), MINOR(n->dev));
        } else {
            asprintf(&tmp, "%s %llu:%llu %d %d 0x%.8x %llu:%llu", n->name, MAJOR(n->dev),
                    MINOR(n->dev), io2->target_count, io2->open_count, io2->flags, MAJOR(io2->dev),
                            MINOR(io2->dev));
        }
        c->sendMsg(0, tmp, false);
        free(tmp);
        nxt = n->next;
    } while (nxt);

    free(buffer);
    free(buffer2);
    close(fd);
    return 0;
}

117 118 119 120 121 122 123 124 125
void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
                          const char *name, unsigned flags) {
    memset(io, 0, dataSize);
    io->data_size = dataSize;
    io->data_start = sizeof(struct dm_ioctl);
    io->version[0] = 4;
    io->version[1] = 0;
    io->version[2] = 0;
    io->flags = flags;
San Mehat's avatar
San Mehat committed
126
    if (name) {
Chih-Wei Huang's avatar
Chih-Wei Huang committed
127
        size_t ret = strlcpy(io->name, name, sizeof(io->name));
128 129
        if (ret >= sizeof(io->name))
            abort();
San Mehat's avatar
San Mehat committed
130
    }
131 132 133
}

int Devmapper::lookupActive(const char *name, char *ubuffer, size_t len) {
Peter Bohm's avatar
Peter Bohm committed
134
    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
135
    if (!buffer) {
San Mehat's avatar
San Mehat committed
136
        SLOGE("Error allocating memory (%s)", strerror(errno));
137 138 139 140 141
        return -1;
    }

    int fd;
    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
San Mehat's avatar
San Mehat committed
142
        SLOGE("Error opening devmapper (%s)", strerror(errno));
143 144 145 146 147 148
        free(buffer);
        return -1;
    }

    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
 
Peter Bohm's avatar
Peter Bohm committed
149
    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
150
    if (ioctl(fd, DM_DEV_STATUS, io)) {
151
        if (errno != ENXIO) {
San Mehat's avatar
San Mehat committed
152
            SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
153 154 155 156 157 158 159 160 161 162 163 164 165
        }
        free(buffer);
        close(fd);
        return -1;
    }
    close(fd);

    unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
    free(buffer);
    snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
    return 0;
}

166 167
int Devmapper::create(const char *name, const char *loopFile, const char *key,
                      unsigned int numSectors, char *ubuffer, size_t len) {
Peter Bohm's avatar
Peter Bohm committed
168
    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
169
    if (!buffer) {
San Mehat's avatar
San Mehat committed
170
        SLOGE("Error allocating memory (%s)", strerror(errno));
171 172 173 174 175
        return -1;
    }

    int fd;
    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
San Mehat's avatar
San Mehat committed
176
        SLOGE("Error opening devmapper (%s)", strerror(errno));
177 178 179 180 181 182 183
        free(buffer);
        return -1;
    }

    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
 
    // Create the DM device
Peter Bohm's avatar
Peter Bohm committed
184
    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
185 186

    if (ioctl(fd, DM_DEV_CREATE, io)) {
San Mehat's avatar
San Mehat committed
187
        SLOGE("Error creating device mapping (%s)", strerror(errno));
188 189 190 191 192 193
        free(buffer);
        close(fd);
        return -1;
    }

    // Set the legacy geometry
Peter Bohm's avatar
Peter Bohm committed
194
    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
195 196 197 198 199 200 201

    char *geoParams = buffer + sizeof(struct dm_ioctl);
    // bps=512 spc=8 res=32 nft=2 sec=8190 mid=0xf0 spt=63 hds=64 hid=0 bspf=8 rdcl=2 infs=1 bkbs=2
    strcpy(geoParams, "0 64 63 0");
    geoParams += strlen(geoParams) + 1;
    geoParams = (char *) _align(geoParams, 8);
    if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
San Mehat's avatar
San Mehat committed
202
        SLOGE("Error setting device geometry (%s)", strerror(errno));
203 204 205 206 207 208
        free(buffer);
        close(fd);
        return -1;
    }

    // Retrieve the device number we were allocated
Peter Bohm's avatar
Peter Bohm committed
209
    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
210
    if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat's avatar
San Mehat committed
211
        SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
212 213 214 215 216 217 218 219 220 221 222 223
        free(buffer);
        close(fd);
        return -1;
    }

    unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
    snprintf(ubuffer, len, "/dev/block/dm-%u", minor);

    // Load the table
    struct dm_target_spec *tgt;
    tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];

Peter Bohm's avatar
Peter Bohm committed
224
    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
225 226
    io->target_count = 1;
    tgt->status = 0;
227

228
    tgt->sector_start = 0;
229
    tgt->length = numSectors;
230

Peter Bohm's avatar
Peter Bohm committed
231
    strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
232 233

    char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
Peter Bohm's avatar
Peter Bohm committed
234 235 236
    snprintf(cryptParams,
            DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
            "twofish %s 0 %s 0", key, loopFile);
237 238 239 240 241
    cryptParams += strlen(cryptParams) + 1;
    cryptParams = (char *) _align(cryptParams, 8);
    tgt->next = cryptParams - buffer;

    if (ioctl(fd, DM_TABLE_LOAD, io)) {
San Mehat's avatar
San Mehat committed
242
        SLOGE("Error loading mapping table (%s)", strerror(errno));
243 244 245 246 247 248
        free(buffer);
        close(fd);
        return -1;
    }

    // Resume the new table
Peter Bohm's avatar
Peter Bohm committed
249
    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
250 251

    if (ioctl(fd, DM_DEV_SUSPEND, io)) {
San Mehat's avatar
San Mehat committed
252
        SLOGE("Error Resuming (%s)", strerror(errno));
253 254 255 256 257 258 259
        free(buffer);
        close(fd);
        return -1;
    }

    free(buffer);

260
    close(fd);
261 262 263 264
    return 0;
}

int Devmapper::destroy(const char *name) {
Peter Bohm's avatar
Peter Bohm committed
265
    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
266
    if (!buffer) {
San Mehat's avatar
San Mehat committed
267
        SLOGE("Error allocating memory (%s)", strerror(errno));
268 269 270 271 272
        return -1;
    }

    int fd;
    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
San Mehat's avatar
San Mehat committed
273
        SLOGE("Error opening devmapper (%s)", strerror(errno));
274 275 276 277 278 279 280
        free(buffer);
        return -1;
    }

    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
 
    // Create the DM device
Peter Bohm's avatar
Peter Bohm committed
281
    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
282 283

    if (ioctl(fd, DM_DEV_REMOVE, io)) {
284
        if (errno != ENXIO) {
San Mehat's avatar
San Mehat committed
285
            SLOGE("Error destroying device mapping (%s)", strerror(errno));
286
        }
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        free(buffer);
        close(fd);
        return -1;
    }

    free(buffer);
    close(fd);
    return 0;
}

void *Devmapper::_align(void *ptr, unsigned int a)
{
        register unsigned long agn = --a;

        return (void *) (((unsigned long) ptr + agn) & ~agn);
}