blob: 77dad206fb299ce5a49d1992d50f62b16d011a10 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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>
San Mehatfd7f5872009-10-12 11:32:47 -070018#include <stdlib.h>
19#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080021#include <fcntl.h>
San Mehata19b2502010-01-06 10:33:53 -080022#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/mount.h>
25
San Mehata2677e42009-12-13 10:40:18 -080026#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070027
28#define LOG_TAG "Vold"
29
30#include <cutils/log.h>
31
San Mehatfd7f5872009-10-12 11:32:47 -070032#include <sysutils/NetlinkEvent.h>
33
San Mehatf1b736b2009-10-10 17:22:08 -070034#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070035#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080036#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080037#include "Loop.h"
38#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080039#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080040#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080041#include "Asec.h"
San Mehatd9a4e352010-03-12 13:32:47 -080042#include "md5.h"
San Mehat23969932010-01-09 07:08:06 -080043
San Mehatf1b736b2009-10-10 17:22:08 -070044VolumeManager *VolumeManager::sInstance = NULL;
45
46VolumeManager *VolumeManager::Instance() {
47 if (!sInstance)
48 sInstance = new VolumeManager();
49 return sInstance;
50}
51
52VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080053 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -070054 mBlockDevices = new BlockDeviceCollection();
55 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -080056 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -070057 mBroadcaster = NULL;
San Mehata2677e42009-12-13 10:40:18 -080058 mUsbMassStorageConnected = false;
San Mehatf1b736b2009-10-10 17:22:08 -070059}
60
61VolumeManager::~VolumeManager() {
62 delete mBlockDevices;
San Mehat88705162010-01-15 09:26:28 -080063 delete mVolumes;
64 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -070065}
66
San Mehatd9a4e352010-03-12 13:32:47 -080067char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
68 MD5_CTX ctx;
69 unsigned char sig[16];
70
71 if (len < 33) {
72 LOGE("Target hash buffer size < 33 bytes (%d)", len);
73 errno = ESPIPE;
74 return NULL;
75 }
76 MD5_Init(&ctx);
77 MD5_Update(&ctx, id, strlen(id));
78 MD5_Final(sig, &ctx);
79
80 memset(buffer, 0, len);
81
82 for (int i = 0; i < 16; i++) {
83 char tmp[3];
84 snprintf(tmp, 3, "%.02x", sig[i]);
85 strcat(buffer, tmp);
86 }
87
88 return buffer;
89}
90
91void VolumeManager::setDebug(bool enable) {
92 mDebug = enable;
93 VolumeCollection::iterator it;
94 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
95 (*it)->setDebug(enable);
96 }
97}
98
San Mehatf1b736b2009-10-10 17:22:08 -070099int VolumeManager::start() {
100 return 0;
101}
102
103int VolumeManager::stop() {
104 return 0;
105}
106
107int VolumeManager::addVolume(Volume *v) {
108 mVolumes->push_back(v);
109 return 0;
110}
111
San Mehata2677e42009-12-13 10:40:18 -0800112void VolumeManager::notifyUmsConnected(bool connected) {
113 char msg[255];
114
115 if (connected) {
116 mUsbMassStorageConnected = true;
117 } else {
118 mUsbMassStorageConnected = false;
119 }
120 snprintf(msg, sizeof(msg), "Share method ums now %s",
121 (connected ? "available" : "unavailable"));
122
123 getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange,
124 msg, false);
125}
126
127void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) {
San Mehat0cde53c2009-12-22 08:32:33 -0800128 const char *devpath = evt->findParam("DEVPATH");
San Mehata2677e42009-12-13 10:40:18 -0800129 const char *name = evt->findParam("SWITCH_NAME");
130 const char *state = evt->findParam("SWITCH_STATE");
131
San Mehat0cde53c2009-12-22 08:32:33 -0800132 if (!name || !state) {
133 LOGW("Switch %s event missing name/state info", devpath);
134 return;
135 }
136
San Mehata2677e42009-12-13 10:40:18 -0800137 if (!strcmp(name, "usb_mass_storage")) {
138
139 if (!strcmp(state, "online")) {
140 notifyUmsConnected(true);
141 } else {
142 notifyUmsConnected(false);
143 }
144 } else {
145 LOGW("Ignoring unknown switch '%s'", name);
146 }
147}
148
San Mehatfd7f5872009-10-12 11:32:47 -0700149void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
150 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700151
San Mehatfd7f5872009-10-12 11:32:47 -0700152 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700153 VolumeCollection::iterator it;
154 bool hit = false;
155 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700156 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800157#ifdef NETLINK_DEBUG
158 LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
159#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700160 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700161 break;
162 }
163 }
164
165 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800166#ifdef NETLINK_DEBUG
San Mehatfd7f5872009-10-12 11:32:47 -0700167 LOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800168#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700169 }
170}
171
San Mehatf1b736b2009-10-10 17:22:08 -0700172int VolumeManager::listVolumes(SocketClient *cli) {
173 VolumeCollection::iterator i;
174
175 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
176 char *buffer;
177 asprintf(&buffer, "%s %s %d",
178 (*i)->getLabel(), (*i)->getMountpoint(),
179 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800180 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700181 free(buffer);
182 }
San Mehata2677e42009-12-13 10:40:18 -0800183 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700184 return 0;
185}
San Mehat49e2bce2009-10-12 16:29:01 -0700186
San Mehata2677e42009-12-13 10:40:18 -0800187int VolumeManager::formatVolume(const char *label) {
188 Volume *v = lookupVolume(label);
189
190 if (!v) {
191 errno = ENOENT;
192 return -1;
193 }
194
195 return v->formatVol();
196}
197
San Mehata19b2502010-01-06 10:33:53 -0800198int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehata19b2502010-01-06 10:33:53 -0800199
San Mehat3bb60202010-02-19 18:14:36 -0800200 snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800201 return 0;
202}
203
San Mehat8b8f71b2010-01-11 09:17:25 -0800204int VolumeManager::createAsec(const char *id, unsigned int numSectors,
San Mehata19b2502010-01-06 10:33:53 -0800205 const char *fstype, const char *key, int ownerUid) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800206 struct asec_superblock sb;
207 memset(&sb, 0, sizeof(sb));
208
209 sb.magic = ASEC_SB_MAGIC;
210 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800211
San Mehatd31e3802010-02-18 08:37:45 -0800212 if (numSectors < ((1024*1024)/512)) {
213 LOGE("Invalid container size specified (%d sectors)", numSectors);
214 errno = EINVAL;
215 return -1;
216 }
217
San Mehata19b2502010-01-06 10:33:53 -0800218 if (lookupVolume(id)) {
San Mehat3bb60202010-02-19 18:14:36 -0800219 LOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800220 errno = EADDRINUSE;
221 return -1;
222 }
223
224 char asecFileName[255];
San Mehat3bb60202010-02-19 18:14:36 -0800225 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800226
227 if (!access(asecFileName, F_OK)) {
228 LOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
229 asecFileName, strerror(errno));
230 errno = EADDRINUSE;
231 return -1;
232 }
233
San Mehatfcf24fe2010-03-03 12:37:32 -0800234 /*
235 * Add some headroom
236 */
237 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
238 unsigned numImgSectors = numSectors + fatSize + 2;
239
240 if (numImgSectors % 63) {
241 numImgSectors += (63 - (numImgSectors % 63));
242 }
243
244 // Add +1 for our superblock which is at the end
245 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehata19b2502010-01-06 10:33:53 -0800246 LOGE("ASEC image file creation failed (%s)", strerror(errno));
247 return -1;
248 }
249
San Mehatd9a4e352010-03-12 13:32:47 -0800250 char idHash[33];
251 if (!asecHash(id, idHash, sizeof(idHash))) {
252 LOGE("Hash of '%s' failed (%s)", id, strerror(errno));
253 unlink(asecFileName);
254 return -1;
255 }
256
San Mehata19b2502010-01-06 10:33:53 -0800257 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800258 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800259 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
260 unlink(asecFileName);
261 return -1;
262 }
263
San Mehatb78a32c2010-01-10 13:02:12 -0800264 char dmDevice[255];
265 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800266
San Mehatb78a32c2010-01-10 13:02:12 -0800267 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800268 // XXX: This is all we support for now
269 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800270 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800271 sizeof(dmDevice))) {
272 LOGE("ASEC device mapping failed (%s)", strerror(errno));
273 Loop::destroyByDevice(loopDevice);
274 unlink(asecFileName);
275 return -1;
276 }
277 cleanupDm = true;
278 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800279 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800280 strcpy(dmDevice, loopDevice);
281 }
282
San Mehatfcf24fe2010-03-03 12:37:32 -0800283 /*
284 * Drop down the superblock at the end of the file
285 */
286
287 int sbfd = open(loopDevice, O_RDWR);
288 if (sbfd < 0) {
289 LOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
290 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800291 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800292 }
293 Loop::destroyByDevice(loopDevice);
294 unlink(asecFileName);
295 return -1;
296 }
297
298 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
299 close(sbfd);
300 LOGE("Failed to lseek for superblock (%s)", strerror(errno));
301 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800302 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800303 }
304 Loop::destroyByDevice(loopDevice);
305 unlink(asecFileName);
306 return -1;
307 }
308
309 if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
310 close(sbfd);
311 LOGE("Failed to write superblock (%s)", strerror(errno));
312 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800313 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800314 }
315 Loop::destroyByDevice(loopDevice);
316 unlink(asecFileName);
317 return -1;
318 }
319 close(sbfd);
320
San Mehata1091cb2010-02-28 20:17:20 -0800321 if (strcmp(fstype, "none")) {
322 if (strcmp(fstype, "fat")) {
323 LOGW("Unknown fstype '%s' specified for container", fstype);
San Mehatb78a32c2010-01-10 13:02:12 -0800324 }
San Mehata19b2502010-01-06 10:33:53 -0800325
San Mehatfcf24fe2010-03-03 12:37:32 -0800326 if (Fat::format(dmDevice, numImgSectors)) {
San Mehata1091cb2010-02-28 20:17:20 -0800327 LOGE("ASEC FAT format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800328 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800329 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800330 }
San Mehateb13a902010-01-07 12:12:50 -0800331 Loop::destroyByDevice(loopDevice);
332 unlink(asecFileName);
333 return -1;
334 }
San Mehata1091cb2010-02-28 20:17:20 -0800335 char mountPoint[255];
San Mehata19b2502010-01-06 10:33:53 -0800336
San Mehata1091cb2010-02-28 20:17:20 -0800337 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
338 if (mkdir(mountPoint, 0777)) {
339 if (errno != EEXIST) {
340 LOGE("Mountpoint creation failed (%s)", strerror(errno));
341 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800342 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800343 }
344 Loop::destroyByDevice(loopDevice);
345 unlink(asecFileName);
346 return -1;
347 }
San Mehatb78a32c2010-01-10 13:02:12 -0800348 }
San Mehata1091cb2010-02-28 20:17:20 -0800349
350 if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid,
351 0, 0000, false)) {
352 LOGE("ASEC FAT mount failed (%s)", strerror(errno));
353 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800354 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800355 }
356 Loop::destroyByDevice(loopDevice);
357 unlink(asecFileName);
358 return -1;
359 }
360 } else {
361 LOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800362 }
San Mehat88705162010-01-15 09:26:28 -0800363
364 mActiveContainers->push_back(strdup(id));
San Mehata19b2502010-01-06 10:33:53 -0800365 return 0;
366}
367
368int VolumeManager::finalizeAsec(const char *id) {
369 char asecFileName[255];
370 char loopDevice[255];
371 char mountPoint[255];
372
San Mehat3bb60202010-02-19 18:14:36 -0800373 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800374
San Mehatd9a4e352010-03-12 13:32:47 -0800375 char idHash[33];
376 if (!asecHash(id, idHash, sizeof(idHash))) {
377 LOGE("Hash of '%s' failed (%s)", id, strerror(errno));
378 return -1;
379 }
380
381 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800382 LOGE("Unable to finalize %s (%s)", id, strerror(errno));
383 return -1;
384 }
385
San Mehat3bb60202010-02-19 18:14:36 -0800386 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatfff0b472010-01-06 19:19:46 -0800387 // XXX:
388 if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800389 LOGE("ASEC finalize mount failed (%s)", strerror(errno));
390 return -1;
391 }
392
San Mehatd9a4e352010-03-12 13:32:47 -0800393 if (mDebug) {
394 LOGD("ASEC %s finalized", id);
395 }
San Mehata19b2502010-01-06 10:33:53 -0800396 return 0;
397}
398
San Mehat048b0802010-01-23 08:17:06 -0800399int VolumeManager::renameAsec(const char *id1, const char *id2) {
400 char *asecFilename1;
401 char *asecFilename2;
402 char mountPoint[255];
403
San Mehat3bb60202010-02-19 18:14:36 -0800404 asprintf(&asecFilename1, "%s/%s.asec", Volume::SEC_ASECDIR, id1);
405 asprintf(&asecFilename2, "%s/%s.asec", Volume::SEC_ASECDIR, id2);
San Mehat048b0802010-01-23 08:17:06 -0800406
San Mehat3bb60202010-02-19 18:14:36 -0800407 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
San Mehat048b0802010-01-23 08:17:06 -0800408 if (isMountpointMounted(mountPoint)) {
409 LOGW("Rename attempt when src mounted");
410 errno = EBUSY;
411 goto out_err;
412 }
413
San Mehat96956ed2010-02-24 08:42:51 -0800414 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
415 if (isMountpointMounted(mountPoint)) {
416 LOGW("Rename attempt when dst mounted");
417 errno = EBUSY;
418 goto out_err;
419 }
420
San Mehat048b0802010-01-23 08:17:06 -0800421 if (!access(asecFilename2, F_OK)) {
422 LOGE("Rename attempt when dst exists");
423 errno = EADDRINUSE;
424 goto out_err;
425 }
426
427 if (rename(asecFilename1, asecFilename2)) {
428 LOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
429 goto out_err;
430 }
431
432 free(asecFilename1);
433 free(asecFilename2);
434 return 0;
435
436out_err:
437 free(asecFilename1);
438 free(asecFilename2);
439 return -1;
440}
441
San Mehat4ba89482010-02-18 09:00:18 -0800442#define ASEC_UNMOUNT_RETRIES 5
443int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800444 char asecFileName[255];
445 char mountPoint[255];
446
San Mehat3bb60202010-02-19 18:14:36 -0800447 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
448 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800449
San Mehatd9a4e352010-03-12 13:32:47 -0800450 char idHash[33];
451 if (!asecHash(id, idHash, sizeof(idHash))) {
452 LOGE("Hash of '%s' failed (%s)", id, strerror(errno));
453 return -1;
454 }
455
San Mehat0586d542010-01-12 15:38:59 -0800456 if (!isMountpointMounted(mountPoint)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800457 LOGE("Unmount request for ASEC %s when not mounted", id);
458 errno = EINVAL;
459 return -1;
460 }
San Mehat23969932010-01-09 07:08:06 -0800461
San Mehatb78a32c2010-01-10 13:02:12 -0800462 int i, rc;
San Mehat8c940ef2010-02-13 14:19:53 -0800463 for (i = 1; i <= ASEC_UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -0800464 rc = umount(mountPoint);
465 if (!rc) {
466 break;
San Mehata19b2502010-01-06 10:33:53 -0800467 }
San Mehatb78a32c2010-01-10 13:02:12 -0800468 if (rc && (errno == EINVAL || errno == ENOENT)) {
San Mehat12f4b892010-02-24 11:43:22 -0800469 LOGI("Secure container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800470 rc = 0;
471 break;
San Mehata19b2502010-01-06 10:33:53 -0800472 }
San Mehatb78a32c2010-01-10 13:02:12 -0800473 LOGW("ASEC %s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -0800474 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800475
San Mehat4ba89482010-02-18 09:00:18 -0800476 int action = 0; // default is to just complain
477
478 if (force) {
479 if (i > (ASEC_UNMOUNT_RETRIES - 2))
480 action = 2; // SIGKILL
481 else if (i > (ASEC_UNMOUNT_RETRIES - 3))
482 action = 1; // SIGHUP
483 }
San Mehat8c940ef2010-02-13 14:19:53 -0800484
San Mehat586536c2010-02-16 17:12:00 -0800485 Process::killProcessesWithOpenFiles(mountPoint, action);
San Mehat8c940ef2010-02-13 14:19:53 -0800486 usleep(1000 * 1000);
San Mehatb78a32c2010-01-10 13:02:12 -0800487 }
488
489 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -0800490 errno = EBUSY;
491 LOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800492 return -1;
493 }
494
San Mehat12f4b892010-02-24 11:43:22 -0800495 int retries = 10;
496
497 while(retries--) {
498 if (!rmdir(mountPoint)) {
499 break;
500 }
501
502 LOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
503 usleep(1000 * 1000);
504 }
505
506 if (!retries) {
507 LOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -0800508 }
San Mehat88705162010-01-15 09:26:28 -0800509
San Mehatd9a4e352010-03-12 13:32:47 -0800510 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehatb78a32c2010-01-10 13:02:12 -0800511 LOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800512 }
513
514 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800515 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800516 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800517 } else {
518 LOGW("Failed to find loop device for {%s} (%s)", asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800519 }
San Mehat88705162010-01-15 09:26:28 -0800520
521 AsecIdCollection::iterator it;
522 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
523 if (!strcmp(*it, id)) {
524 free(*it);
525 mActiveContainers->erase(it);
526 break;
527 }
528 }
529 if (it == mActiveContainers->end()) {
530 LOGW("mActiveContainers is inconsistent!");
531 }
San Mehatb78a32c2010-01-10 13:02:12 -0800532 return 0;
533}
534
San Mehat4ba89482010-02-18 09:00:18 -0800535int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -0800536 char asecFileName[255];
537 char mountPoint[255];
538
San Mehat3bb60202010-02-19 18:14:36 -0800539 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehat55013f72010-02-24 12:12:34 -0800540 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatb78a32c2010-01-10 13:02:12 -0800541
San Mehat0586d542010-01-12 15:38:59 -0800542 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800543 if (mDebug) {
544 LOGD("Unmounting container before destroy");
545 }
San Mehat4ba89482010-02-18 09:00:18 -0800546 if (unmountAsec(id, force)) {
San Mehat0586d542010-01-12 15:38:59 -0800547 LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
548 return -1;
549 }
550 }
San Mehata19b2502010-01-06 10:33:53 -0800551
San Mehat0586d542010-01-12 15:38:59 -0800552 if (unlink(asecFileName)) {
San Mehat68f8ebd2010-01-23 07:21:21 -0800553 LOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800554 return -1;
555 }
San Mehata19b2502010-01-06 10:33:53 -0800556
San Mehatd9a4e352010-03-12 13:32:47 -0800557 if (mDebug) {
558 LOGD("ASEC %s destroyed", id);
559 }
San Mehata19b2502010-01-06 10:33:53 -0800560 return 0;
561}
562
563int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
564 char asecFileName[255];
565 char mountPoint[255];
566
San Mehat3bb60202010-02-19 18:14:36 -0800567 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
568 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800569
570 if (isMountpointMounted(mountPoint)) {
571 LOGE("ASEC %s already mounted", id);
572 errno = EBUSY;
573 return -1;
574 }
575
San Mehatd9a4e352010-03-12 13:32:47 -0800576 char idHash[33];
577 if (!asecHash(id, idHash, sizeof(idHash))) {
578 LOGE("Hash of '%s' failed (%s)", id, strerror(errno));
579 return -1;
580 }
San Mehata19b2502010-01-06 10:33:53 -0800581 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800582 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
583 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800584 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
585 return -1;
586 }
San Mehatd9a4e352010-03-12 13:32:47 -0800587 if (mDebug) {
588 LOGD("New loop device created at %s", loopDevice);
589 }
San Mehatb78a32c2010-01-10 13:02:12 -0800590 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800591 if (mDebug) {
592 LOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
593 }
San Mehatb78a32c2010-01-10 13:02:12 -0800594 }
595
596 char dmDevice[255];
597 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -0800598 int fd;
599 unsigned int nr_sec = 0;
600
601 if ((fd = open(loopDevice, O_RDWR)) < 0) {
602 LOGE("Failed to open loopdevice (%s)", strerror(errno));
603 Loop::destroyByDevice(loopDevice);
604 return -1;
605 }
606
607 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
608 LOGE("Failed to get loop size (%s)", strerror(errno));
609 Loop::destroyByDevice(loopDevice);
610 close(fd);
611 return -1;
612 }
613
614 /*
615 * Validate superblock
616 */
617 struct asec_superblock sb;
618 memset(&sb, 0, sizeof(sb));
619 if (lseek(fd, ((nr_sec-1) * 512), SEEK_SET) < 0) {
620 LOGE("lseek failed (%s)", strerror(errno));
621 close(fd);
622 Loop::destroyByDevice(loopDevice);
623 return -1;
624 }
625 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
626 LOGE("superblock read failed (%s)", strerror(errno));
627 close(fd);
628 Loop::destroyByDevice(loopDevice);
629 return -1;
630 }
631
632 close(fd);
633
San Mehatd9a4e352010-03-12 13:32:47 -0800634 if (mDebug) {
635 LOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
636 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800637 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
638 LOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
639 Loop::destroyByDevice(loopDevice);
640 errno = EMEDIUMTYPE;
641 return -1;
642 }
643 nr_sec--; // We don't want the devmapping to extend onto our superblock
644
San Mehatb78a32c2010-01-10 13:02:12 -0800645 if (strcmp(key, "none")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800646 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
647 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -0800648 dmDevice, sizeof(dmDevice))) {
649 LOGE("ASEC device mapping failed (%s)", strerror(errno));
650 Loop::destroyByDevice(loopDevice);
651 return -1;
652 }
San Mehatd9a4e352010-03-12 13:32:47 -0800653 if (mDebug) {
654 LOGD("New devmapper instance created at %s", dmDevice);
655 }
San Mehatb78a32c2010-01-10 13:02:12 -0800656 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800657 if (mDebug) {
658 LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
659 }
San Mehatb78a32c2010-01-10 13:02:12 -0800660 }
661 cleanupDm = true;
662 } else {
663 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800664 }
665
666 if (mkdir(mountPoint, 0777)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800667 if (errno != EEXIST) {
668 LOGE("Mountpoint creation failed (%s)", strerror(errno));
669 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800670 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800671 }
672 Loop::destroyByDevice(loopDevice);
673 return -1;
674 }
San Mehata19b2502010-01-06 10:33:53 -0800675 }
676
San Mehatb78a32c2010-01-10 13:02:12 -0800677 if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0,
San Mehatcff5ec32010-01-08 12:31:44 -0800678 0222, false)) {
679// 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800680 LOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800681 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800682 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800683 }
684 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800685 return -1;
686 }
687
San Mehat88705162010-01-15 09:26:28 -0800688 mActiveContainers->push_back(strdup(id));
San Mehatd9a4e352010-03-12 13:32:47 -0800689 if (mDebug) {
690 LOGD("ASEC %s mounted", id);
691 }
San Mehata19b2502010-01-06 10:33:53 -0800692 return 0;
693}
694
San Mehat49e2bce2009-10-12 16:29:01 -0700695int VolumeManager::mountVolume(const char *label) {
696 Volume *v = lookupVolume(label);
697
698 if (!v) {
699 errno = ENOENT;
700 return -1;
701 }
702
San Mehata2677e42009-12-13 10:40:18 -0800703 return v->mountVol();
704}
705
706int VolumeManager::shareAvailable(const char *method, bool *avail) {
707
708 if (strcmp(method, "ums")) {
709 errno = ENOSYS;
710 return -1;
711 }
712
713 if (mUsbMassStorageConnected)
714 *avail = true;
715 else
716 *avail = false;
717 return 0;
718}
719
San Mehateba65e92010-01-29 05:15:16 -0800720int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
721 Volume *v = lookupVolume(label);
722
723 if (!v) {
724 errno = ENOENT;
725 return -1;
726 }
727
728 if (strcmp(method, "ums")) {
729 errno = ENOSYS;
730 return -1;
731 }
732
733 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -0800734 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -0800735 } else {
736 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -0800737 }
738 return 0;
739}
740
San Mehata2677e42009-12-13 10:40:18 -0800741int VolumeManager::simulate(const char *cmd, const char *arg) {
742
743 if (!strcmp(cmd, "ums")) {
744 if (!strcmp(arg, "connect")) {
745 notifyUmsConnected(true);
746 } else if (!strcmp(arg, "disconnect")) {
747 notifyUmsConnected(false);
748 } else {
749 errno = EINVAL;
750 return -1;
751 }
752 } else {
753 errno = EINVAL;
754 return -1;
755 }
756 return 0;
757}
758
759int VolumeManager::shareVolume(const char *label, const char *method) {
760 Volume *v = lookupVolume(label);
761
762 if (!v) {
763 errno = ENOENT;
764 return -1;
765 }
766
767 /*
768 * Eventually, we'll want to support additional share back-ends,
769 * some of which may work while the media is mounted. For now,
770 * we just support UMS
771 */
772 if (strcmp(method, "ums")) {
773 errno = ENOSYS;
774 return -1;
775 }
776
777 if (v->getState() == Volume::State_NoMedia) {
778 errno = ENODEV;
779 return -1;
780 }
781
San Mehat49e2bce2009-10-12 16:29:01 -0700782 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -0800783 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -0700784 errno = EBUSY;
785 return -1;
786 }
787
San Mehata2677e42009-12-13 10:40:18 -0800788 dev_t d = v->getDiskDevice();
789 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
790 // This volume does not support raw disk access
791 errno = EINVAL;
792 return -1;
793 }
794
795 int fd;
796 char nodepath[255];
797 snprintf(nodepath,
798 sizeof(nodepath), "/dev/block/vold/%d:%d",
799 MAJOR(d), MINOR(d));
800
San Mehat0cde53c2009-12-22 08:32:33 -0800801 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file",
802 O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800803 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
804 return -1;
805 }
806
807 if (write(fd, nodepath, strlen(nodepath)) < 0) {
808 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
809 close(fd);
810 return -1;
811 }
812
813 close(fd);
814 v->handleVolumeShared();
815 return 0;
816}
817
818int VolumeManager::unshareVolume(const char *label, const char *method) {
819 Volume *v = lookupVolume(label);
820
821 if (!v) {
822 errno = ENOENT;
823 return -1;
824 }
825
826 if (strcmp(method, "ums")) {
827 errno = ENOSYS;
828 return -1;
829 }
830
831 if (v->getState() != Volume::State_Shared) {
832 errno = EINVAL;
833 return -1;
834 }
835
836 dev_t d = v->getDiskDevice();
837
838 int fd;
839 char nodepath[255];
840 snprintf(nodepath,
841 sizeof(nodepath), "/dev/block/vold/%d:%d",
842 MAJOR(d), MINOR(d));
843
San Mehat0cde53c2009-12-22 08:32:33 -0800844 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800845 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
846 return -1;
847 }
848
849 char ch = 0;
850 if (write(fd, &ch, 1) < 0) {
851 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
852 close(fd);
853 return -1;
854 }
855
856 close(fd);
857 v->handleVolumeUnshared();
858 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700859}
860
San Mehat4ba89482010-02-18 09:00:18 -0800861int VolumeManager::unmountVolume(const char *label, bool force) {
San Mehat49e2bce2009-10-12 16:29:01 -0700862 Volume *v = lookupVolume(label);
863
864 if (!v) {
865 errno = ENOENT;
866 return -1;
867 }
868
San Mehata2677e42009-12-13 10:40:18 -0800869 if (v->getState() == Volume::State_NoMedia) {
870 errno = ENODEV;
871 return -1;
872 }
873
San Mehat49e2bce2009-10-12 16:29:01 -0700874 if (v->getState() != Volume::State_Mounted) {
San Mehata2677e42009-12-13 10:40:18 -0800875 LOGW("Attempt to unmount volume which isn't mounted (%d)\n",
876 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -0700877 errno = EBUSY;
878 return -1;
879 }
880
San Mehat88705162010-01-15 09:26:28 -0800881 while(mActiveContainers->size()) {
882 AsecIdCollection::iterator it = mActiveContainers->begin();
883 LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint());
San Mehat4ba89482010-02-18 09:00:18 -0800884 if (unmountAsec(*it, force)) {
San Mehat12f4b892010-02-24 11:43:22 -0800885 LOGE("Failed to unmount ASEC %s (%s)", *it, strerror(errno));
San Mehat0e382532010-02-24 08:25:55 -0800886 return -1;
San Mehat88705162010-01-15 09:26:28 -0800887 }
888 }
889
San Mehat4ba89482010-02-18 09:00:18 -0800890 return v->unmountVol(force);
San Mehat49e2bce2009-10-12 16:29:01 -0700891}
892
San Mehata2677e42009-12-13 10:40:18 -0800893/*
894 * Looks up a volume by it's label or mount-point
895 */
San Mehat49e2bce2009-10-12 16:29:01 -0700896Volume *VolumeManager::lookupVolume(const char *label) {
897 VolumeCollection::iterator i;
898
899 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -0800900 if (label[0] == '/') {
901 if (!strcmp(label, (*i)->getMountpoint()))
902 return (*i);
903 } else {
904 if (!strcmp(label, (*i)->getLabel()))
905 return (*i);
906 }
San Mehat49e2bce2009-10-12 16:29:01 -0700907 }
908 return NULL;
909}
San Mehata19b2502010-01-06 10:33:53 -0800910
911bool VolumeManager::isMountpointMounted(const char *mp)
912{
913 char device[256];
914 char mount_path[256];
915 char rest[256];
916 FILE *fp;
917 char line[1024];
918
919 if (!(fp = fopen("/proc/mounts", "r"))) {
920 LOGE("Error opening /proc/mounts (%s)", strerror(errno));
921 return false;
922 }
923
924 while(fgets(line, sizeof(line), fp)) {
925 line[strlen(line)-1] = '\0';
926 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
927 if (!strcmp(mount_path, mp)) {
928 fclose(fp);
929 return true;
930 }
931
932 }
933
934 fclose(fp);
935 return false;
936}
937