blob: 02f7d98e51a635591c1307f45dff90644cef6476 [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);
San Mehatb78a32c2010-01-10 13:02:12 -080074 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
75 return 0;
76}
77
San Mehat8b8f71b2010-01-11 09:17:25 -080078int Devmapper::create(const char *name, const char *loopFile, const char *key,
79 unsigned int numSectors, char *ubuffer, size_t len) {
San Mehatb78a32c2010-01-10 13:02:12 -080080 char *buffer = (char *) malloc(4096);
81 if (!buffer) {
82 LOGE("Error allocating memory (%s)", strerror(errno));
83 return -1;
84 }
85
86 int fd;
87 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
88 LOGE("Error opening devmapper (%s)", strerror(errno));
89 free(buffer);
90 return -1;
91 }
92
93 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
94
95 // Create the DM device
96 ioctlInit(io, 4096, name, 0);
97
98 if (ioctl(fd, DM_DEV_CREATE, io)) {
99 LOGE("Error creating device mapping (%s)", strerror(errno));
100 free(buffer);
101 close(fd);
102 return -1;
103 }
104
105 // Set the legacy geometry
106 ioctlInit(io, 4096, name, 0);
107
108 char *geoParams = buffer + sizeof(struct dm_ioctl);
109 // 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
110 strcpy(geoParams, "0 64 63 0");
111 geoParams += strlen(geoParams) + 1;
112 geoParams = (char *) _align(geoParams, 8);
113 if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
114 LOGE("Error setting device geometry (%s)", strerror(errno));
115 free(buffer);
116 close(fd);
117 return -1;
118 }
119
120 // Retrieve the device number we were allocated
121 ioctlInit(io, 4096, name, 0);
122 if (ioctl(fd, DM_DEV_STATUS, io)) {
San Mehat8b8f71b2010-01-11 09:17:25 -0800123 LOGE("Error retrieving devmapper status (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800124 free(buffer);
125 close(fd);
126 return -1;
127 }
128
129 unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
San Mehatb78a32c2010-01-10 13:02:12 -0800130 snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
131
132 // Load the table
133 struct dm_target_spec *tgt;
134 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
135
136 ioctlInit(io, 4096, name, DM_STATUS_TABLE_FLAG);
137 io->target_count = 1;
138 tgt->status = 0;
139 tgt->sector_start = 0;
San Mehat8b8f71b2010-01-11 09:17:25 -0800140 tgt->length = numSectors;
San Mehatb78a32c2010-01-10 13:02:12 -0800141 strcpy(tgt->target_type, "crypt");
142
143 char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
144 sprintf(cryptParams, "twofish %s 0 %s 0", key, loopFile);
145 cryptParams += strlen(cryptParams) + 1;
146 cryptParams = (char *) _align(cryptParams, 8);
147 tgt->next = cryptParams - buffer;
148
149 if (ioctl(fd, DM_TABLE_LOAD, io)) {
150 LOGE("Error loading mapping table (%s)", strerror(errno));
151 free(buffer);
152 close(fd);
153 return -1;
154 }
155
156 // Resume the new table
157 ioctlInit(io, 4096, name, 0);
158
159 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
160 LOGE("Error Resuming (%s)", strerror(errno));
161 free(buffer);
162 close(fd);
163 return -1;
164 }
165
166 free(buffer);
167
San Mehat8c940ef2010-02-13 14:19:53 -0800168 close(fd);
San Mehatb78a32c2010-01-10 13:02:12 -0800169 return 0;
170}
171
172int Devmapper::destroy(const char *name) {
173 char *buffer = (char *) malloc(4096);
174 if (!buffer) {
175 LOGE("Error allocating memory (%s)", strerror(errno));
176 return -1;
177 }
178
179 int fd;
180 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
181 LOGE("Error opening devmapper (%s)", strerror(errno));
182 free(buffer);
183 return -1;
184 }
185
186 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
187
188 // Create the DM device
189 ioctlInit(io, 4096, name, 0);
190
191 if (ioctl(fd, DM_DEV_REMOVE, io)) {
San Mehat0586d542010-01-12 15:38:59 -0800192 if (errno != ENXIO) {
193 LOGE("Error destroying device mapping (%s)", strerror(errno));
194 }
San Mehatb78a32c2010-01-10 13:02:12 -0800195 free(buffer);
196 close(fd);
197 return -1;
198 }
199
200 free(buffer);
201 close(fd);
202 return 0;
203}
204
205void *Devmapper::_align(void *ptr, unsigned int a)
206{
207 register unsigned long agn = --a;
208
209 return (void *) (((unsigned long) ptr + agn) & ~agn);
210}
211