Devmapper.cpp revision 8b8f71b1d760411279f3b07a5c97709f052c689e
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <errno.h>
21#include <string.h>
22
23#include <sys/types.h>
24#include <sys/ioctl.h>
25#include <sys/stat.h>
26
27#define LOG_TAG "Vold"
28
29#include <cutils/log.h>
30
31#include "Devmapper.h"
32
33void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
34                          const char *name, unsigned flags) {
35    memset(io, 0, dataSize);
36    io->data_size = dataSize;
37    io->data_start = sizeof(struct dm_ioctl);
38    io->version[0] = 4;
39    io->version[1] = 0;
40    io->version[2] = 0;
41    io->flags = flags;
42    strncpy(io->name, name, sizeof(io->name));
43}
44
45int Devmapper::lookupActive(const char *name, char *ubuffer, size_t len) {
46    char *buffer = (char *) malloc(4096);
47    if (!buffer) {
48        LOGE("Error allocating memory (%s)", strerror(errno));
49        return -1;
50    }
51
52    int fd;
53    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
54        LOGE("Error opening devmapper (%s)", strerror(errno));
55        free(buffer);
56        return -1;
57    }
58
59    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
60
61    ioctlInit(io, 4096, name, 0);
62    if (ioctl(fd, DM_DEV_STATUS, io)) {
63        if (errno != ENXIO) {
64            LOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
65        }
66        free(buffer);
67        close(fd);
68        return -1;
69    }
70    close(fd);
71
72    unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
73    free(buffer);
74    LOGD("Newly created devmapper instance minor = %d\n", minor);
75    snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
76    return 0;
77}
78
79int Devmapper::create(const char *name, const char *loopFile, const char *key,
80                      unsigned int numSectors, char *ubuffer, size_t len) {
81    char *buffer = (char *) malloc(4096);
82    if (!buffer) {
83        LOGE("Error allocating memory (%s)", strerror(errno));
84        return -1;
85    }
86
87    int fd;
88    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
89        LOGE("Error opening devmapper (%s)", strerror(errno));
90        free(buffer);
91        return -1;
92    }
93
94    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
95
96    // Create the DM device
97    ioctlInit(io, 4096, name, 0);
98
99    if (ioctl(fd, DM_DEV_CREATE, io)) {
100        LOGE("Error creating device mapping (%s)", strerror(errno));
101        free(buffer);
102        close(fd);
103        return -1;
104    }
105
106    // Set the legacy geometry
107    ioctlInit(io, 4096, name, 0);
108
109    char *geoParams = buffer + sizeof(struct dm_ioctl);
110    // 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
111    strcpy(geoParams, "0 64 63 0");
112    geoParams += strlen(geoParams) + 1;
113    geoParams = (char *) _align(geoParams, 8);
114    if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
115        LOGE("Error setting device geometry (%s)", strerror(errno));
116        free(buffer);
117        close(fd);
118        return -1;
119    }
120
121    // Retrieve the device number we were allocated
122    ioctlInit(io, 4096, name, 0);
123    if (ioctl(fd, DM_DEV_STATUS, io)) {
124        LOGE("Error retrieving devmapper status (%s)", strerror(errno));
125        free(buffer);
126        close(fd);
127        return -1;
128    }
129
130    unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
131    LOGD("Newly created devmapper instance minor = %d\n", minor);
132    snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
133
134    // Load the table
135    struct dm_target_spec *tgt;
136    tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
137
138    ioctlInit(io, 4096, name, DM_STATUS_TABLE_FLAG);
139    io->target_count = 1;
140    tgt->status = 0;
141    tgt->sector_start = 0;
142    tgt->length = numSectors;
143    strcpy(tgt->target_type, "crypt");
144
145    char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
146    sprintf(cryptParams, "twofish %s 0 %s 0", key, loopFile);
147    cryptParams += strlen(cryptParams) + 1;
148    cryptParams = (char *) _align(cryptParams, 8);
149    tgt->next = cryptParams - buffer;
150
151    if (ioctl(fd, DM_TABLE_LOAD, io)) {
152        LOGE("Error loading mapping table (%s)", strerror(errno));
153        free(buffer);
154        close(fd);
155        return -1;
156    }
157
158    // Resume the new table
159    ioctlInit(io, 4096, name, 0);
160
161    if (ioctl(fd, DM_DEV_SUSPEND, io)) {
162        LOGE("Error Resuming (%s)", strerror(errno));
163        free(buffer);
164        close(fd);
165        return -1;
166    }
167
168    free(buffer);
169
170    return 0;
171}
172
173int Devmapper::destroy(const char *name) {
174    char *buffer = (char *) malloc(4096);
175    if (!buffer) {
176        LOGE("Error allocating memory (%s)", strerror(errno));
177        return -1;
178    }
179
180    int fd;
181    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
182        LOGE("Error opening devmapper (%s)", strerror(errno));
183        free(buffer);
184        return -1;
185    }
186
187    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
188
189    // Create the DM device
190    ioctlInit(io, 4096, name, 0);
191
192    if (ioctl(fd, DM_DEV_REMOVE, io)) {
193        LOGE("Error destroying device mapping (%s)", strerror(errno));
194        free(buffer);
195        close(fd);
196        return -1;
197    }
198
199    free(buffer);
200    close(fd);
201    return 0;
202}
203
204void *Devmapper::_align(void *ptr, unsigned int a)
205{
206        register unsigned long agn = --a;
207
208        return (void *) (((unsigned long) ptr + agn) & ~agn);
209}
210
211