blob: 9dd8ef3b1a1af3ef68e660b08c24b65fc9d8b41d [file] [log] [blame]
San Mehatb78a32c2010-01-10 13:02:12 -08001/*
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)) {
San Mehat8b8f71b2010-01-11 09:17:25 -080063 if (errno != ENXIO) {
64 LOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -080065 }
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
San Mehat8b8f71b2010-01-11 09:17:25 -080079int Devmapper::create(const char *name, const char *loopFile, const char *key,
80 unsigned int numSectors, char *ubuffer, size_t len) {
San Mehatb78a32c2010-01-10 13:02:12 -080081 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)) {
San Mehat8b8f71b2010-01-11 09:17:25 -0800124 LOGE("Error retrieving devmapper status (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800125 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;
San Mehat8b8f71b2010-01-11 09:17:25 -0800142 tgt->length = numSectors;
San Mehatb78a32c2010-01-10 13:02:12 -0800143 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)) {
San Mehat0586d542010-01-12 15:38:59 -0800193 if (errno != ENXIO) {
194 LOGE("Error destroying device mapping (%s)", strerror(errno));
195 }
San Mehatb78a32c2010-01-10 13:02:12 -0800196 free(buffer);
197 close(fd);
198 return -1;
199 }
200
201 free(buffer);
202 close(fd);
203 return 0;
204}
205
206void *Devmapper::_align(void *ptr, unsigned int a)
207{
208 register unsigned long agn = --a;
209
210 return (void *) (((unsigned long) ptr + agn) & ~agn);
211}
212