blob: c1aa713212379efaf6841b3ad2b15f4f7b01487a [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;
San Mehatfcf24fe2010-03-03 12:37:32 -0800139
San Mehatb78a32c2010-01-10 13:02:12 -0800140 tgt->sector_start = 0;
San Mehat8b8f71b2010-01-11 09:17:25 -0800141 tgt->length = numSectors;
San Mehatfcf24fe2010-03-03 12:37:32 -0800142
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
San Mehat8c940ef2010-02-13 14:19:53 -0800170 close(fd);
San Mehatb78a32c2010-01-10 13:02:12 -0800171 return 0;
172}
173
174int Devmapper::destroy(const char *name) {
175 char *buffer = (char *) malloc(4096);
176 if (!buffer) {
177 LOGE("Error allocating memory (%s)", strerror(errno));
178 return -1;
179 }
180
181 int fd;
182 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
183 LOGE("Error opening devmapper (%s)", strerror(errno));
184 free(buffer);
185 return -1;
186 }
187
188 struct dm_ioctl *io = (struct dm_ioctl *) buffer;
189
190 // Create the DM device
191 ioctlInit(io, 4096, name, 0);
192
193 if (ioctl(fd, DM_DEV_REMOVE, io)) {
San Mehat0586d542010-01-12 15:38:59 -0800194 if (errno != ENXIO) {
195 LOGE("Error destroying device mapping (%s)", strerror(errno));
196 }
San Mehatb78a32c2010-01-10 13:02:12 -0800197 free(buffer);
198 close(fd);
199 return -1;
200 }
201
202 free(buffer);
203 close(fd);
204 return 0;
205}
206
207void *Devmapper::_align(void *ptr, unsigned int a)
208{
209 register unsigned long agn = --a;
210
211 return (void *) (((unsigned long) ptr + agn) & ~agn);
212}
213