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