blob: d4dad2295f61e919e37b2f5c9a416601b713cb16 [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 Mehat23969932010-01-09 07:08:06 -080041
San Mehatf1b736b2009-10-10 17:22:08 -070042VolumeManager *VolumeManager::sInstance = NULL;
43
44VolumeManager *VolumeManager::Instance() {
45 if (!sInstance)
46 sInstance = new VolumeManager();
47 return sInstance;
48}
49
50VolumeManager::VolumeManager() {
51 mBlockDevices = new BlockDeviceCollection();
52 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -080053 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -070054 mBroadcaster = NULL;
San Mehata2677e42009-12-13 10:40:18 -080055 mUsbMassStorageConnected = false;
San Mehatf1b736b2009-10-10 17:22:08 -070056}
57
58VolumeManager::~VolumeManager() {
59 delete mBlockDevices;
San Mehat88705162010-01-15 09:26:28 -080060 delete mVolumes;
61 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -070062}
63
64int VolumeManager::start() {
65 return 0;
66}
67
68int VolumeManager::stop() {
69 return 0;
70}
71
72int VolumeManager::addVolume(Volume *v) {
73 mVolumes->push_back(v);
74 return 0;
75}
76
San Mehata2677e42009-12-13 10:40:18 -080077void VolumeManager::notifyUmsConnected(bool connected) {
78 char msg[255];
79
80 if (connected) {
81 mUsbMassStorageConnected = true;
82 } else {
83 mUsbMassStorageConnected = false;
84 }
85 snprintf(msg, sizeof(msg), "Share method ums now %s",
86 (connected ? "available" : "unavailable"));
87
88 getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange,
89 msg, false);
90}
91
92void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) {
San Mehat0cde53c2009-12-22 08:32:33 -080093 const char *devpath = evt->findParam("DEVPATH");
San Mehata2677e42009-12-13 10:40:18 -080094 const char *name = evt->findParam("SWITCH_NAME");
95 const char *state = evt->findParam("SWITCH_STATE");
96
San Mehat0cde53c2009-12-22 08:32:33 -080097 if (!name || !state) {
98 LOGW("Switch %s event missing name/state info", devpath);
99 return;
100 }
101
San Mehata2677e42009-12-13 10:40:18 -0800102 if (!strcmp(name, "usb_mass_storage")) {
103
104 if (!strcmp(state, "online")) {
105 notifyUmsConnected(true);
106 } else {
107 notifyUmsConnected(false);
108 }
109 } else {
110 LOGW("Ignoring unknown switch '%s'", name);
111 }
112}
113
San Mehatfd7f5872009-10-12 11:32:47 -0700114void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
115 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700116
San Mehatfd7f5872009-10-12 11:32:47 -0700117 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700118 VolumeCollection::iterator it;
119 bool hit = false;
120 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700121 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800122#ifdef NETLINK_DEBUG
123 LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
124#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700125 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700126 break;
127 }
128 }
129
130 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800131#ifdef NETLINK_DEBUG
San Mehatfd7f5872009-10-12 11:32:47 -0700132 LOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800133#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700134 }
135}
136
San Mehatf1b736b2009-10-10 17:22:08 -0700137int VolumeManager::listVolumes(SocketClient *cli) {
138 VolumeCollection::iterator i;
139
140 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
141 char *buffer;
142 asprintf(&buffer, "%s %s %d",
143 (*i)->getLabel(), (*i)->getMountpoint(),
144 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800145 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700146 free(buffer);
147 }
San Mehata2677e42009-12-13 10:40:18 -0800148 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700149 return 0;
150}
San Mehat49e2bce2009-10-12 16:29:01 -0700151
San Mehata2677e42009-12-13 10:40:18 -0800152int VolumeManager::formatVolume(const char *label) {
153 Volume *v = lookupVolume(label);
154
155 if (!v) {
156 errno = ENOENT;
157 return -1;
158 }
159
160 return v->formatVol();
161}
162
San Mehata19b2502010-01-06 10:33:53 -0800163int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehata19b2502010-01-06 10:33:53 -0800164
San Mehat3bb60202010-02-19 18:14:36 -0800165 snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800166 return 0;
167}
168
San Mehat8b8f71b2010-01-11 09:17:25 -0800169int VolumeManager::createAsec(const char *id, unsigned int numSectors,
San Mehata19b2502010-01-06 10:33:53 -0800170 const char *fstype, const char *key, int ownerUid) {
171
San Mehatd31e3802010-02-18 08:37:45 -0800172 if (numSectors < ((1024*1024)/512)) {
173 LOGE("Invalid container size specified (%d sectors)", numSectors);
174 errno = EINVAL;
175 return -1;
176 }
177
San Mehata19b2502010-01-06 10:33:53 -0800178 if (lookupVolume(id)) {
San Mehat3bb60202010-02-19 18:14:36 -0800179 LOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800180 errno = EADDRINUSE;
181 return -1;
182 }
183
184 char asecFileName[255];
San Mehat3bb60202010-02-19 18:14:36 -0800185 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800186
187 if (!access(asecFileName, F_OK)) {
188 LOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
189 asecFileName, strerror(errno));
190 errno = EADDRINUSE;
191 return -1;
192 }
193
San Mehat8b8f71b2010-01-11 09:17:25 -0800194 if (Loop::createImageFile(asecFileName, numSectors)) {
San Mehata19b2502010-01-06 10:33:53 -0800195 LOGE("ASEC image file creation failed (%s)", strerror(errno));
196 return -1;
197 }
198
199 char loopDevice[255];
San Mehat8da6bcb2010-01-09 12:24:05 -0800200 if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800201 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
202 unlink(asecFileName);
203 return -1;
204 }
205
San Mehatb78a32c2010-01-10 13:02:12 -0800206 char dmDevice[255];
207 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800208
San Mehatb78a32c2010-01-10 13:02:12 -0800209 if (strcmp(key, "none")) {
San Mehat8b8f71b2010-01-11 09:17:25 -0800210 if (Devmapper::create(id, loopDevice, key, numSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800211 sizeof(dmDevice))) {
212 LOGE("ASEC device mapping failed (%s)", strerror(errno));
213 Loop::destroyByDevice(loopDevice);
214 unlink(asecFileName);
215 return -1;
216 }
217 cleanupDm = true;
218 } else {
219 strcpy(dmDevice, loopDevice);
220 }
221
222 if (Fat::format(dmDevice)) {
San Mehata19b2502010-01-06 10:33:53 -0800223 LOGE("ASEC FAT format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800224 if (cleanupDm) {
225 Devmapper::destroy(id);
226 }
San Mehata19b2502010-01-06 10:33:53 -0800227 Loop::destroyByDevice(loopDevice);
228 unlink(asecFileName);
229 return -1;
230 }
231
232 char mountPoint[255];
233
San Mehat3bb60202010-02-19 18:14:36 -0800234 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800235 if (mkdir(mountPoint, 0777)) {
San Mehateb13a902010-01-07 12:12:50 -0800236 if (errno != EEXIST) {
237 LOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800238 if (cleanupDm) {
239 Devmapper::destroy(id);
240 }
San Mehateb13a902010-01-07 12:12:50 -0800241 Loop::destroyByDevice(loopDevice);
242 unlink(asecFileName);
243 return -1;
244 }
San Mehata19b2502010-01-06 10:33:53 -0800245 }
246
San Mehatb78a32c2010-01-10 13:02:12 -0800247 if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid,
San Mehatcff5ec32010-01-08 12:31:44 -0800248 0, 0000, false)) {
249// 0, 0007, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800250 LOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800251 if (cleanupDm) {
252 Devmapper::destroy(id);
253 }
San Mehata19b2502010-01-06 10:33:53 -0800254 Loop::destroyByDevice(loopDevice);
255 unlink(asecFileName);
256 return -1;
257 }
San Mehat88705162010-01-15 09:26:28 -0800258
259 mActiveContainers->push_back(strdup(id));
San Mehata19b2502010-01-06 10:33:53 -0800260 return 0;
261}
262
263int VolumeManager::finalizeAsec(const char *id) {
264 char asecFileName[255];
265 char loopDevice[255];
266 char mountPoint[255];
267
San Mehat3bb60202010-02-19 18:14:36 -0800268 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800269
270 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
271 LOGE("Unable to finalize %s (%s)", id, strerror(errno));
272 return -1;
273 }
274
San Mehat3bb60202010-02-19 18:14:36 -0800275 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatfff0b472010-01-06 19:19:46 -0800276 // XXX:
277 if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800278 LOGE("ASEC finalize mount failed (%s)", strerror(errno));
279 return -1;
280 }
281
282 LOGD("ASEC %s finalized", id);
283 return 0;
284}
285
San Mehat048b0802010-01-23 08:17:06 -0800286int VolumeManager::renameAsec(const char *id1, const char *id2) {
287 char *asecFilename1;
288 char *asecFilename2;
289 char mountPoint[255];
290
San Mehat3bb60202010-02-19 18:14:36 -0800291 asprintf(&asecFilename1, "%s/%s.asec", Volume::SEC_ASECDIR, id1);
292 asprintf(&asecFilename2, "%s/%s.asec", Volume::SEC_ASECDIR, id2);
San Mehat048b0802010-01-23 08:17:06 -0800293
San Mehat3bb60202010-02-19 18:14:36 -0800294 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
San Mehat048b0802010-01-23 08:17:06 -0800295 if (isMountpointMounted(mountPoint)) {
296 LOGW("Rename attempt when src mounted");
297 errno = EBUSY;
298 goto out_err;
299 }
300
San Mehat96956ed2010-02-24 08:42:51 -0800301 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
302 if (isMountpointMounted(mountPoint)) {
303 LOGW("Rename attempt when dst mounted");
304 errno = EBUSY;
305 goto out_err;
306 }
307
San Mehat048b0802010-01-23 08:17:06 -0800308 if (!access(asecFilename2, F_OK)) {
309 LOGE("Rename attempt when dst exists");
310 errno = EADDRINUSE;
311 goto out_err;
312 }
313
314 if (rename(asecFilename1, asecFilename2)) {
315 LOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
316 goto out_err;
317 }
318
319 free(asecFilename1);
320 free(asecFilename2);
321 return 0;
322
323out_err:
324 free(asecFilename1);
325 free(asecFilename2);
326 return -1;
327}
328
San Mehat4ba89482010-02-18 09:00:18 -0800329#define ASEC_UNMOUNT_RETRIES 5
330int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800331 char asecFileName[255];
332 char mountPoint[255];
333
San Mehat3bb60202010-02-19 18:14:36 -0800334 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
335 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800336
San Mehat0586d542010-01-12 15:38:59 -0800337 if (!isMountpointMounted(mountPoint)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800338 LOGE("Unmount request for ASEC %s when not mounted", id);
339 errno = EINVAL;
340 return -1;
341 }
San Mehat23969932010-01-09 07:08:06 -0800342
San Mehatb78a32c2010-01-10 13:02:12 -0800343 int i, rc;
San Mehat8c940ef2010-02-13 14:19:53 -0800344 for (i = 1; i <= ASEC_UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -0800345 rc = umount(mountPoint);
346 if (!rc) {
347 break;
San Mehata19b2502010-01-06 10:33:53 -0800348 }
San Mehatb78a32c2010-01-10 13:02:12 -0800349 if (rc && (errno == EINVAL || errno == ENOENT)) {
San Mehat12f4b892010-02-24 11:43:22 -0800350 LOGI("Secure container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800351 rc = 0;
352 break;
San Mehata19b2502010-01-06 10:33:53 -0800353 }
San Mehatb78a32c2010-01-10 13:02:12 -0800354 LOGW("ASEC %s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -0800355 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800356
San Mehat4ba89482010-02-18 09:00:18 -0800357 int action = 0; // default is to just complain
358
359 if (force) {
360 if (i > (ASEC_UNMOUNT_RETRIES - 2))
361 action = 2; // SIGKILL
362 else if (i > (ASEC_UNMOUNT_RETRIES - 3))
363 action = 1; // SIGHUP
364 }
San Mehat8c940ef2010-02-13 14:19:53 -0800365
San Mehat586536c2010-02-16 17:12:00 -0800366 Process::killProcessesWithOpenFiles(mountPoint, action);
San Mehat8c940ef2010-02-13 14:19:53 -0800367 usleep(1000 * 1000);
San Mehatb78a32c2010-01-10 13:02:12 -0800368 }
369
370 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -0800371 errno = EBUSY;
372 LOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800373 return -1;
374 }
375
San Mehat12f4b892010-02-24 11:43:22 -0800376 int retries = 10;
377
378 while(retries--) {
379 if (!rmdir(mountPoint)) {
380 break;
381 }
382
383 LOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
384 usleep(1000 * 1000);
385 }
386
387 if (!retries) {
388 LOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -0800389 }
San Mehat88705162010-01-15 09:26:28 -0800390
San Mehatb78a32c2010-01-10 13:02:12 -0800391 if (Devmapper::destroy(id) && errno != ENXIO) {
392 LOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800393 }
394
395 char loopDevice[255];
396 if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
397 Loop::destroyByDevice(loopDevice);
398 }
San Mehat88705162010-01-15 09:26:28 -0800399
400 AsecIdCollection::iterator it;
401 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
402 if (!strcmp(*it, id)) {
403 free(*it);
404 mActiveContainers->erase(it);
405 break;
406 }
407 }
408 if (it == mActiveContainers->end()) {
409 LOGW("mActiveContainers is inconsistent!");
410 }
San Mehatb78a32c2010-01-10 13:02:12 -0800411 return 0;
412}
413
San Mehat4ba89482010-02-18 09:00:18 -0800414int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -0800415 char asecFileName[255];
416 char mountPoint[255];
417
San Mehat3bb60202010-02-19 18:14:36 -0800418 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
San Mehatb78a32c2010-01-10 13:02:12 -0800419
San Mehat0586d542010-01-12 15:38:59 -0800420 if (isMountpointMounted(mountPoint)) {
San Mehat68f8ebd2010-01-23 07:21:21 -0800421 LOGD("Unmounting container before destroy");
San Mehat4ba89482010-02-18 09:00:18 -0800422 if (unmountAsec(id, force)) {
San Mehat0586d542010-01-12 15:38:59 -0800423 LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
424 return -1;
425 }
426 }
San Mehata19b2502010-01-06 10:33:53 -0800427
San Mehat0586d542010-01-12 15:38:59 -0800428 if (unlink(asecFileName)) {
San Mehat68f8ebd2010-01-23 07:21:21 -0800429 LOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800430 return -1;
431 }
San Mehata19b2502010-01-06 10:33:53 -0800432
433 LOGD("ASEC %s destroyed", id);
434 return 0;
435}
436
437int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
438 char asecFileName[255];
439 char mountPoint[255];
440
San Mehat3bb60202010-02-19 18:14:36 -0800441 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", Volume::SEC_ASECDIR, id);
442 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800443
444 if (isMountpointMounted(mountPoint)) {
445 LOGE("ASEC %s already mounted", id);
446 errno = EBUSY;
447 return -1;
448 }
449
450 char loopDevice[255];
451 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat8da6bcb2010-01-09 12:24:05 -0800452 if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800453 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
454 return -1;
455 }
San Mehatb78a32c2010-01-10 13:02:12 -0800456 LOGD("New loop device created at %s", loopDevice);
457 } else {
458 LOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
459 }
460
461 char dmDevice[255];
462 bool cleanupDm = false;
463 if (strcmp(key, "none")) {
464 if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) {
465 unsigned int nr_sec = 0;
466 int fd;
467
468 if ((fd = open(loopDevice, O_RDWR)) < 0) {
469 LOGE("Failed to open loopdevice (%s)", strerror(errno));
470 Loop::destroyByDevice(loopDevice);
471 return -1;
472 }
473
474 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
475 LOGE("Failed to get loop size (%s)", strerror(errno));
476 Loop::destroyByDevice(loopDevice);
477 close(fd);
478 return -1;
479 }
480 close(fd);
San Mehat8b8f71b2010-01-11 09:17:25 -0800481 if (Devmapper::create(id, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -0800482 dmDevice, sizeof(dmDevice))) {
483 LOGE("ASEC device mapping failed (%s)", strerror(errno));
484 Loop::destroyByDevice(loopDevice);
485 return -1;
486 }
487 LOGD("New devmapper instance created at %s", dmDevice);
488 } else {
489 LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
490 }
491 cleanupDm = true;
492 } else {
493 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800494 }
495
496 if (mkdir(mountPoint, 0777)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800497 if (errno != EEXIST) {
498 LOGE("Mountpoint creation failed (%s)", strerror(errno));
499 if (cleanupDm) {
500 Devmapper::destroy(id);
501 }
502 Loop::destroyByDevice(loopDevice);
503 return -1;
504 }
San Mehata19b2502010-01-06 10:33:53 -0800505 }
506
San Mehatb78a32c2010-01-10 13:02:12 -0800507 if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0,
San Mehatcff5ec32010-01-08 12:31:44 -0800508 0222, false)) {
509// 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800510 LOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800511 if (cleanupDm) {
512 Devmapper::destroy(id);
513 }
514 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800515 return -1;
516 }
517
San Mehat88705162010-01-15 09:26:28 -0800518 mActiveContainers->push_back(strdup(id));
San Mehata19b2502010-01-06 10:33:53 -0800519 LOGD("ASEC %s mounted", id);
520 return 0;
521}
522
San Mehat49e2bce2009-10-12 16:29:01 -0700523int VolumeManager::mountVolume(const char *label) {
524 Volume *v = lookupVolume(label);
525
526 if (!v) {
527 errno = ENOENT;
528 return -1;
529 }
530
San Mehata2677e42009-12-13 10:40:18 -0800531 return v->mountVol();
532}
533
534int VolumeManager::shareAvailable(const char *method, bool *avail) {
535
536 if (strcmp(method, "ums")) {
537 errno = ENOSYS;
538 return -1;
539 }
540
541 if (mUsbMassStorageConnected)
542 *avail = true;
543 else
544 *avail = false;
545 return 0;
546}
547
San Mehateba65e92010-01-29 05:15:16 -0800548int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
549 Volume *v = lookupVolume(label);
550
551 if (!v) {
552 errno = ENOENT;
553 return -1;
554 }
555
556 if (strcmp(method, "ums")) {
557 errno = ENOSYS;
558 return -1;
559 }
560
561 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -0800562 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -0800563 } else {
564 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -0800565 }
566 return 0;
567}
568
San Mehata2677e42009-12-13 10:40:18 -0800569int VolumeManager::simulate(const char *cmd, const char *arg) {
570
571 if (!strcmp(cmd, "ums")) {
572 if (!strcmp(arg, "connect")) {
573 notifyUmsConnected(true);
574 } else if (!strcmp(arg, "disconnect")) {
575 notifyUmsConnected(false);
576 } else {
577 errno = EINVAL;
578 return -1;
579 }
580 } else {
581 errno = EINVAL;
582 return -1;
583 }
584 return 0;
585}
586
587int VolumeManager::shareVolume(const char *label, const char *method) {
588 Volume *v = lookupVolume(label);
589
590 if (!v) {
591 errno = ENOENT;
592 return -1;
593 }
594
595 /*
596 * Eventually, we'll want to support additional share back-ends,
597 * some of which may work while the media is mounted. For now,
598 * we just support UMS
599 */
600 if (strcmp(method, "ums")) {
601 errno = ENOSYS;
602 return -1;
603 }
604
605 if (v->getState() == Volume::State_NoMedia) {
606 errno = ENODEV;
607 return -1;
608 }
609
San Mehat49e2bce2009-10-12 16:29:01 -0700610 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -0800611 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -0700612 errno = EBUSY;
613 return -1;
614 }
615
San Mehata2677e42009-12-13 10:40:18 -0800616 dev_t d = v->getDiskDevice();
617 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
618 // This volume does not support raw disk access
619 errno = EINVAL;
620 return -1;
621 }
622
623 int fd;
624 char nodepath[255];
625 snprintf(nodepath,
626 sizeof(nodepath), "/dev/block/vold/%d:%d",
627 MAJOR(d), MINOR(d));
628
San Mehat0cde53c2009-12-22 08:32:33 -0800629 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file",
630 O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800631 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
632 return -1;
633 }
634
635 if (write(fd, nodepath, strlen(nodepath)) < 0) {
636 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
637 close(fd);
638 return -1;
639 }
640
641 close(fd);
642 v->handleVolumeShared();
643 return 0;
644}
645
646int VolumeManager::unshareVolume(const char *label, const char *method) {
647 Volume *v = lookupVolume(label);
648
649 if (!v) {
650 errno = ENOENT;
651 return -1;
652 }
653
654 if (strcmp(method, "ums")) {
655 errno = ENOSYS;
656 return -1;
657 }
658
659 if (v->getState() != Volume::State_Shared) {
660 errno = EINVAL;
661 return -1;
662 }
663
664 dev_t d = v->getDiskDevice();
665
666 int fd;
667 char nodepath[255];
668 snprintf(nodepath,
669 sizeof(nodepath), "/dev/block/vold/%d:%d",
670 MAJOR(d), MINOR(d));
671
San Mehat0cde53c2009-12-22 08:32:33 -0800672 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800673 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
674 return -1;
675 }
676
677 char ch = 0;
678 if (write(fd, &ch, 1) < 0) {
679 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
680 close(fd);
681 return -1;
682 }
683
684 close(fd);
685 v->handleVolumeUnshared();
686 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700687}
688
San Mehat4ba89482010-02-18 09:00:18 -0800689int VolumeManager::unmountVolume(const char *label, bool force) {
San Mehat49e2bce2009-10-12 16:29:01 -0700690 Volume *v = lookupVolume(label);
691
692 if (!v) {
693 errno = ENOENT;
694 return -1;
695 }
696
San Mehata2677e42009-12-13 10:40:18 -0800697 if (v->getState() == Volume::State_NoMedia) {
698 errno = ENODEV;
699 return -1;
700 }
701
San Mehat49e2bce2009-10-12 16:29:01 -0700702 if (v->getState() != Volume::State_Mounted) {
San Mehata2677e42009-12-13 10:40:18 -0800703 LOGW("Attempt to unmount volume which isn't mounted (%d)\n",
704 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -0700705 errno = EBUSY;
706 return -1;
707 }
708
San Mehat88705162010-01-15 09:26:28 -0800709 while(mActiveContainers->size()) {
710 AsecIdCollection::iterator it = mActiveContainers->begin();
711 LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint());
San Mehat4ba89482010-02-18 09:00:18 -0800712 if (unmountAsec(*it, force)) {
San Mehat12f4b892010-02-24 11:43:22 -0800713 LOGE("Failed to unmount ASEC %s (%s)", *it, strerror(errno));
San Mehat0e382532010-02-24 08:25:55 -0800714 return -1;
San Mehat88705162010-01-15 09:26:28 -0800715 }
716 }
717
San Mehat4ba89482010-02-18 09:00:18 -0800718 return v->unmountVol(force);
San Mehat49e2bce2009-10-12 16:29:01 -0700719}
720
San Mehata2677e42009-12-13 10:40:18 -0800721/*
722 * Looks up a volume by it's label or mount-point
723 */
San Mehat49e2bce2009-10-12 16:29:01 -0700724Volume *VolumeManager::lookupVolume(const char *label) {
725 VolumeCollection::iterator i;
726
727 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -0800728 if (label[0] == '/') {
729 if (!strcmp(label, (*i)->getMountpoint()))
730 return (*i);
731 } else {
732 if (!strcmp(label, (*i)->getLabel()))
733 return (*i);
734 }
San Mehat49e2bce2009-10-12 16:29:01 -0700735 }
736 return NULL;
737}
San Mehata19b2502010-01-06 10:33:53 -0800738
739bool VolumeManager::isMountpointMounted(const char *mp)
740{
741 char device[256];
742 char mount_path[256];
743 char rest[256];
744 FILE *fp;
745 char line[1024];
746
747 if (!(fp = fopen("/proc/mounts", "r"))) {
748 LOGE("Error opening /proc/mounts (%s)", strerror(errno));
749 return false;
750 }
751
752 while(fgets(line, sizeof(line), fp)) {
753 line[strlen(line)-1] = '\0';
754 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
755 if (!strcmp(mount_path, mp)) {
756 fclose(fp);
757 return true;
758 }
759
760 }
761
762 fclose(fp);
763 return false;
764}
765