Devmapper.cpp revision 092aa1c585fedd9e169eece41b8a471f1739908a
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#include <stdlib.h>
23
24#include <sys/types.h>
25#include <sys/ioctl.h>
26#include <sys/stat.h>
27
28#include <linux/kdev_t.h>
29
30#define LOG_TAG "Vold"
31
32#include <cutils/log.h>
33
34#include <sysutils/SocketClient.h>
35
36#include "Devmapper.h"
37
38#define DEVMAPPER_BUFFER_SIZE 4096
39
40int Devmapper::dumpState(SocketClient *c) {
41
42    char *buffer = (char *) malloc(1024 * 64);
43    if (!buffer) {
44        SLOGE("Error allocating memory (%s)", strerror(errno));
45        return -1;
46    }
47    memset(buffer, 0, (1024 * 64));
48
49    char *buffer2 = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
50    if (!buffer2) {
51        SLOGE("Error allocating memory (%s)", strerror(errno));
52        free(buffer);
53        return -1;
54    }
55
56    int fd;
57    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
58        SLOGE("Error opening devmapper (%s)", strerror(errno));
59        free(buffer);
60        free(buffer2);
61        return -1;
62    }
63
64    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
65    ioctlInit(io, (1024 * 64), NULL, 0);
66
67    if (ioctl(fd, DM_LIST_DEVICES, io)) {
68        SLOGE("DM_LIST_DEVICES ioctl failed (%s)", strerror(errno));
69        free(buffer);
70        free(buffer2);
71        close(fd);
72        return -1;
73    }
74
75    struct dm_name_list *n = (struct dm_name_list *) (((char *) buffer) + io->data_start);
76    if (!n->dev) {
77        free(buffer);
78        free(buffer2);
79        close(fd);
80        return 0;
81    }
82
83    unsigned nxt = 0;
84    do {
85        n = (struct dm_name_list *) (((char *) n) + nxt);
86
87        memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
88        struct dm_ioctl *io2 = (struct dm_ioctl *) buffer2;
89        ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
90        if (ioctl(fd, DM_DEV_STATUS, io2)) {
91            if (errno != ENXIO) {
92                SLOGE("DM_DEV_STATUS ioctl failed (%s)", strerror(errno));
93            }
94            io2 = NULL;
95        }
96
97        char *tmp;
98        if (!io2) {
99            asprintf(&tmp, "%s %llu:%llu (no status available)", n->name, MAJOR(n->dev), MINOR(n->dev));
100        } else {
101            asprintf(&tmp, "%s %llu:%llu %d %d 0x%.8x %llu:%llu", n->name, MAJOR(n->dev),
102                    MINOR(n->dev), io2->target_count, io2->open_count, io2->flags, MAJOR(io2->dev),
103                            MINOR(io2->dev));
104        }
105        c->sendMsg(0, tmp, false);
106        free(tmp);
107        nxt = n->next;
108    } while (nxt);
109
110    free(buffer);
111    free(buffer2);
112    close(fd);
113    return 0;
114}
115
116void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
117                          const char *name, unsigned flags) {
118    memset(io, 0, dataSize);
119    io->data_size = dataSize;
120    io->data_start = sizeof(struct dm_ioctl);
121    io->version[0] = 4;
122    io->version[1] = 0;
123    io->version[2] = 0;
124    io->flags = flags;
125    if (name) {
126        int ret = strlcpy(io->name, name, sizeof(io->name));
127	if (ret >= sizeof(io->name))
128		abort();
129    }
130}
131
132int Devmapper::lookupActive(const char *name, char *ubuffer, size_t len) {
133    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
134    if (!buffer) {
135        SLOGE("Error allocating memory (%s)", strerror(errno));
136        return -1;
137    }
138
139    int fd;
140    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
141        SLOGE("Error opening devmapper (%s)", strerror(errno));
142        free(buffer);
143        return -1;
144    }
145
146    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
147
148    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
149    if (ioctl(fd, DM_DEV_STATUS, io)) {
150        if (errno != ENXIO) {
151            SLOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
152        }
153        free(buffer);
154        close(fd);
155        return -1;
156    }
157    close(fd);
158
159    unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
160    free(buffer);
161    snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
162    return 0;
163}
164
165int Devmapper::create(const char *name, const char *loopFile, const char *key,
166                      unsigned int numSectors, char *ubuffer, size_t len) {
167    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
168    if (!buffer) {
169        SLOGE("Error allocating memory (%s)", strerror(errno));
170        return -1;
171    }
172
173    int fd;
174    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
175        SLOGE("Error opening devmapper (%s)", strerror(errno));
176        free(buffer);
177        return -1;
178    }
179
180    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
181
182    // Create the DM device
183    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
184
185    if (ioctl(fd, DM_DEV_CREATE, io)) {
186        SLOGE("Error creating device mapping (%s)", strerror(errno));
187        free(buffer);
188        close(fd);
189        return -1;
190    }
191
192    // Set the legacy geometry
193    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
194
195    char *geoParams = buffer + sizeof(struct dm_ioctl);
196    // 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
197    strcpy(geoParams, "0 64 63 0");
198    geoParams += strlen(geoParams) + 1;
199    geoParams = (char *) _align(geoParams, 8);
200    if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
201        SLOGE("Error setting device geometry (%s)", strerror(errno));
202        free(buffer);
203        close(fd);
204        return -1;
205    }
206
207    // Retrieve the device number we were allocated
208    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
209    if (ioctl(fd, DM_DEV_STATUS, io)) {
210        SLOGE("Error retrieving devmapper status (%s)", strerror(errno));
211        free(buffer);
212        close(fd);
213        return -1;
214    }
215
216    unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
217    snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
218
219    // Load the table
220    struct dm_target_spec *tgt;
221    tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
222
223    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
224    io->target_count = 1;
225    tgt->status = 0;
226
227    tgt->sector_start = 0;
228    tgt->length = numSectors;
229
230    strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
231
232    char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
233    snprintf(cryptParams,
234            DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
235            "twofish %s 0 %s 0", key, loopFile);
236    cryptParams += strlen(cryptParams) + 1;
237    cryptParams = (char *) _align(cryptParams, 8);
238    tgt->next = cryptParams - buffer;
239
240    if (ioctl(fd, DM_TABLE_LOAD, io)) {
241        SLOGE("Error loading mapping table (%s)", strerror(errno));
242        free(buffer);
243        close(fd);
244        return -1;
245    }
246
247    // Resume the new table
248    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
249
250    if (ioctl(fd, DM_DEV_SUSPEND, io)) {
251        SLOGE("Error Resuming (%s)", strerror(errno));
252        free(buffer);
253        close(fd);
254        return -1;
255    }
256
257    free(buffer);
258
259    close(fd);
260    return 0;
261}
262
263int Devmapper::destroy(const char *name) {
264    char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
265    if (!buffer) {
266        SLOGE("Error allocating memory (%s)", strerror(errno));
267        return -1;
268    }
269
270    int fd;
271    if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
272        SLOGE("Error opening devmapper (%s)", strerror(errno));
273        free(buffer);
274        return -1;
275    }
276
277    struct dm_ioctl *io = (struct dm_ioctl *) buffer;
278
279    // Create the DM device
280    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
281
282    if (ioctl(fd, DM_DEV_REMOVE, io)) {
283        if (errno != ENXIO) {
284            SLOGE("Error destroying device mapping (%s)", strerror(errno));
285        }
286        free(buffer);
287        close(fd);
288        return -1;
289    }
290
291    free(buffer);
292    close(fd);
293    return 0;
294}
295
296void *Devmapper::_align(void *ptr, unsigned int a)
297{
298        register unsigned long agn = --a;
299
300        return (void *) (((unsigned long) ptr + agn) & ~agn);
301}
302
303