blob: 1e1f15fde07a7a524c28d5392c4eb90dfa201129 [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 Mehatf1b736b2009-10-10 17:22:08 -070039
San Mehat23969932010-01-09 07:08:06 -080040extern "C" void KillProcessesWithOpenFiles(const char *, int, int, int);
41
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();
53 mBroadcaster = NULL;
San Mehata2677e42009-12-13 10:40:18 -080054 mUsbMassStorageConnected = false;
San Mehatf1b736b2009-10-10 17:22:08 -070055}
56
57VolumeManager::~VolumeManager() {
58 delete mBlockDevices;
59}
60
61int VolumeManager::start() {
62 return 0;
63}
64
65int VolumeManager::stop() {
66 return 0;
67}
68
69int VolumeManager::addVolume(Volume *v) {
70 mVolumes->push_back(v);
71 return 0;
72}
73
San Mehata2677e42009-12-13 10:40:18 -080074void VolumeManager::notifyUmsConnected(bool connected) {
75 char msg[255];
76
77 if (connected) {
78 mUsbMassStorageConnected = true;
79 } else {
80 mUsbMassStorageConnected = false;
81 }
82 snprintf(msg, sizeof(msg), "Share method ums now %s",
83 (connected ? "available" : "unavailable"));
84
85 getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange,
86 msg, false);
87}
88
89void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) {
San Mehat0cde53c2009-12-22 08:32:33 -080090 const char *devpath = evt->findParam("DEVPATH");
San Mehata2677e42009-12-13 10:40:18 -080091 const char *name = evt->findParam("SWITCH_NAME");
92 const char *state = evt->findParam("SWITCH_STATE");
93
San Mehat0cde53c2009-12-22 08:32:33 -080094 if (!name || !state) {
95 LOGW("Switch %s event missing name/state info", devpath);
96 return;
97 }
98
San Mehata2677e42009-12-13 10:40:18 -080099 if (!strcmp(name, "usb_mass_storage")) {
100
101 if (!strcmp(state, "online")) {
102 notifyUmsConnected(true);
103 } else {
104 notifyUmsConnected(false);
105 }
106 } else {
107 LOGW("Ignoring unknown switch '%s'", name);
108 }
109}
110
San Mehatfd7f5872009-10-12 11:32:47 -0700111void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
112 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700113
San Mehatfd7f5872009-10-12 11:32:47 -0700114 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700115 VolumeCollection::iterator it;
116 bool hit = false;
117 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700118 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800119#ifdef NETLINK_DEBUG
120 LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
121#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700122 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700123 break;
124 }
125 }
126
127 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800128#ifdef NETLINK_DEBUG
San Mehatfd7f5872009-10-12 11:32:47 -0700129 LOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800130#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700131 }
132}
133
San Mehatf1b736b2009-10-10 17:22:08 -0700134int VolumeManager::listVolumes(SocketClient *cli) {
135 VolumeCollection::iterator i;
136
137 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
138 char *buffer;
139 asprintf(&buffer, "%s %s %d",
140 (*i)->getLabel(), (*i)->getMountpoint(),
141 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800142 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700143 free(buffer);
144 }
San Mehata2677e42009-12-13 10:40:18 -0800145 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700146 return 0;
147}
San Mehat49e2bce2009-10-12 16:29:01 -0700148
San Mehata2677e42009-12-13 10:40:18 -0800149int VolumeManager::formatVolume(const char *label) {
150 Volume *v = lookupVolume(label);
151
152 if (!v) {
153 errno = ENOENT;
154 return -1;
155 }
156
157 return v->formatVol();
158}
159
San Mehata19b2502010-01-06 10:33:53 -0800160int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
161 char mountPoint[255];
162
163 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
164
165 if (!isMountpointMounted(mountPoint)) {
166 errno = ENOENT;
167 return -1;
168 }
169 snprintf(buffer, maxlen, "/asec/%s", id);
170 return 0;
171}
172
173int VolumeManager::createAsec(const char *id, int sizeMb,
174 const char *fstype, const char *key, int ownerUid) {
175
176 mkdir("/sdcard/android_secure", 0777);
177
178 if (lookupVolume(id)) {
179 LOGE("ASEC volume '%s' currently exists", id);
180 errno = EADDRINUSE;
181 return -1;
182 }
183
184 char asecFileName[255];
185 snprintf(asecFileName, sizeof(asecFileName),
186 "/sdcard/android_secure/%s.asec", id);
187
188 if (!access(asecFileName, F_OK)) {
189 LOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
190 asecFileName, strerror(errno));
191 errno = EADDRINUSE;
192 return -1;
193 }
194
195 if (Loop::createImageFile(asecFileName, sizeMb)) {
196 LOGE("ASEC image file creation failed (%s)", strerror(errno));
197 return -1;
198 }
199
200 char loopDevice[255];
201 if (Loop::getNextAvailable(loopDevice, sizeof(loopDevice))) {
202 unlink(asecFileName);
203 return -1;
204 }
205
206 if (Loop::create(loopDevice, asecFileName)) {
207 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
208 unlink(asecFileName);
209 return -1;
210 }
211
212 /* XXX: Start devmapper */
213
214 if (Fat::format(loopDevice)) {
215 LOGE("ASEC FAT format failed (%s)", strerror(errno));
216 Loop::destroyByDevice(loopDevice);
217 unlink(asecFileName);
218 return -1;
219 }
220
221 char mountPoint[255];
222
223 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
224 if (mkdir(mountPoint, 0777)) {
San Mehateb13a902010-01-07 12:12:50 -0800225 if (errno != EEXIST) {
226 LOGE("Mountpoint creation failed (%s)", strerror(errno));
227 Loop::destroyByDevice(loopDevice);
228 unlink(asecFileName);
229 return -1;
230 }
San Mehata19b2502010-01-06 10:33:53 -0800231 }
232
San Mehatfff0b472010-01-06 19:19:46 -0800233 if (Fat::doMount(loopDevice, mountPoint, false, false, ownerUid,
San Mehatcff5ec32010-01-08 12:31:44 -0800234 0, 0000, false)) {
235// 0, 0007, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800236 LOGE("ASEC FAT mount failed (%s)", strerror(errno));
237 Loop::destroyByDevice(loopDevice);
238 unlink(asecFileName);
239 return -1;
240 }
241
242 return 0;
243}
244
245int VolumeManager::finalizeAsec(const char *id) {
246 char asecFileName[255];
247 char loopDevice[255];
248 char mountPoint[255];
249
250 snprintf(asecFileName, sizeof(asecFileName),
251 "/sdcard/android_secure/%s.asec", id);
252
253 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
254 LOGE("Unable to finalize %s (%s)", id, strerror(errno));
255 return -1;
256 }
257
258 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
San Mehatfff0b472010-01-06 19:19:46 -0800259 // XXX:
260 if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800261 LOGE("ASEC finalize mount failed (%s)", strerror(errno));
262 return -1;
263 }
264
265 LOGD("ASEC %s finalized", id);
266 return 0;
267}
268
269int VolumeManager::destroyAsec(const char *id) {
270 char asecFileName[255];
271 char mountPoint[255];
272
273 snprintf(asecFileName, sizeof(asecFileName),
274 "/sdcard/android_secure/%s.asec", id);
275 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
276
277 if (isMountpointMounted(mountPoint)) {
278 int i, rc;
279 for (i = 0; i < 10; i++) {
280 rc = umount(mountPoint);
281 if (!rc) {
282 break;
283 }
284 if (rc && (errno == EINVAL || errno == ENOENT)) {
285 rc = 0;
286 break;
287 }
288 LOGW("ASEC %s unmount attempt %d failed (%s)",
289 id, i +1, strerror(errno));
San Mehat23969932010-01-09 07:08:06 -0800290
291 if (i >= 5) {
292 KillProcessesWithOpenFiles(mountPoint, (i < 7 ? 0 : 1),
293 NULL, 0);
294 }
San Mehata19b2502010-01-06 10:33:53 -0800295 usleep(1000 * 250);
296 }
297 if (rc) {
298 LOGE("Failed to unmount ASEC %s for destroy", id);
299 return -1;
300 }
301 }
302
303 char loopDevice[255];
304 if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
305 Loop::destroyByDevice(loopDevice);
306 }
307
308 unlink(asecFileName);
309
310 LOGD("ASEC %s destroyed", id);
311 return 0;
312}
313
314int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
315 char asecFileName[255];
316 char mountPoint[255];
317
318 snprintf(asecFileName, sizeof(asecFileName),
319 "/sdcard/android_secure/%s.asec", id);
320 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id);
321
322 if (isMountpointMounted(mountPoint)) {
323 LOGE("ASEC %s already mounted", id);
324 errno = EBUSY;
325 return -1;
326 }
327
328 char loopDevice[255];
329 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
330 if (Loop::getNextAvailable(loopDevice, sizeof(loopDevice))) {
331 LOGE("Unable to find loop device for ASEC mount");
332 return -1;
333 }
334
335 if (Loop::create(loopDevice, asecFileName)) {
336 LOGE("ASEC loop device creation failed (%s)", strerror(errno));
337 return -1;
338 }
339 }
340
341 if (mkdir(mountPoint, 0777)) {
342 LOGE("Mountpoint creation failed (%s)", strerror(errno));
343 return -1;
344 }
345
San Mehatfff0b472010-01-06 19:19:46 -0800346 if (Fat::doMount(loopDevice, mountPoint, true, false, ownerUid, 0,
San Mehatcff5ec32010-01-08 12:31:44 -0800347 0222, false)) {
348// 0227, false)) {
San Mehata19b2502010-01-06 10:33:53 -0800349 LOGE("ASEC mount failed (%s)", strerror(errno));
350 return -1;
351 }
352
353 LOGD("ASEC %s mounted", id);
354 return 0;
355}
356
San Mehat49e2bce2009-10-12 16:29:01 -0700357int VolumeManager::mountVolume(const char *label) {
358 Volume *v = lookupVolume(label);
359
360 if (!v) {
361 errno = ENOENT;
362 return -1;
363 }
364
San Mehata2677e42009-12-13 10:40:18 -0800365 return v->mountVol();
366}
367
368int VolumeManager::shareAvailable(const char *method, bool *avail) {
369
370 if (strcmp(method, "ums")) {
371 errno = ENOSYS;
372 return -1;
373 }
374
375 if (mUsbMassStorageConnected)
376 *avail = true;
377 else
378 *avail = false;
379 return 0;
380}
381
382int VolumeManager::simulate(const char *cmd, const char *arg) {
383
384 if (!strcmp(cmd, "ums")) {
385 if (!strcmp(arg, "connect")) {
386 notifyUmsConnected(true);
387 } else if (!strcmp(arg, "disconnect")) {
388 notifyUmsConnected(false);
389 } else {
390 errno = EINVAL;
391 return -1;
392 }
393 } else {
394 errno = EINVAL;
395 return -1;
396 }
397 return 0;
398}
399
400int VolumeManager::shareVolume(const char *label, const char *method) {
401 Volume *v = lookupVolume(label);
402
403 if (!v) {
404 errno = ENOENT;
405 return -1;
406 }
407
408 /*
409 * Eventually, we'll want to support additional share back-ends,
410 * some of which may work while the media is mounted. For now,
411 * we just support UMS
412 */
413 if (strcmp(method, "ums")) {
414 errno = ENOSYS;
415 return -1;
416 }
417
418 if (v->getState() == Volume::State_NoMedia) {
419 errno = ENODEV;
420 return -1;
421 }
422
San Mehat49e2bce2009-10-12 16:29:01 -0700423 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -0800424 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -0700425 errno = EBUSY;
426 return -1;
427 }
428
San Mehata2677e42009-12-13 10:40:18 -0800429 dev_t d = v->getDiskDevice();
430 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
431 // This volume does not support raw disk access
432 errno = EINVAL;
433 return -1;
434 }
435
436 int fd;
437 char nodepath[255];
438 snprintf(nodepath,
439 sizeof(nodepath), "/dev/block/vold/%d:%d",
440 MAJOR(d), MINOR(d));
441
San Mehat0cde53c2009-12-22 08:32:33 -0800442 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file",
443 O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800444 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
445 return -1;
446 }
447
448 if (write(fd, nodepath, strlen(nodepath)) < 0) {
449 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
450 close(fd);
451 return -1;
452 }
453
454 close(fd);
455 v->handleVolumeShared();
456 return 0;
457}
458
459int VolumeManager::unshareVolume(const char *label, const char *method) {
460 Volume *v = lookupVolume(label);
461
462 if (!v) {
463 errno = ENOENT;
464 return -1;
465 }
466
467 if (strcmp(method, "ums")) {
468 errno = ENOSYS;
469 return -1;
470 }
471
472 if (v->getState() != Volume::State_Shared) {
473 errno = EINVAL;
474 return -1;
475 }
476
477 dev_t d = v->getDiskDevice();
478
479 int fd;
480 char nodepath[255];
481 snprintf(nodepath,
482 sizeof(nodepath), "/dev/block/vold/%d:%d",
483 MAJOR(d), MINOR(d));
484
San Mehat0cde53c2009-12-22 08:32:33 -0800485 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) {
San Mehata2677e42009-12-13 10:40:18 -0800486 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
487 return -1;
488 }
489
490 char ch = 0;
491 if (write(fd, &ch, 1) < 0) {
492 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
493 close(fd);
494 return -1;
495 }
496
497 close(fd);
498 v->handleVolumeUnshared();
499 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -0700500}
501
502int VolumeManager::unmountVolume(const char *label) {
503 Volume *v = lookupVolume(label);
504
505 if (!v) {
506 errno = ENOENT;
507 return -1;
508 }
509
San Mehata2677e42009-12-13 10:40:18 -0800510 if (v->getState() == Volume::State_NoMedia) {
511 errno = ENODEV;
512 return -1;
513 }
514
San Mehat49e2bce2009-10-12 16:29:01 -0700515 if (v->getState() != Volume::State_Mounted) {
San Mehata2677e42009-12-13 10:40:18 -0800516 LOGW("Attempt to unmount volume which isn't mounted (%d)\n",
517 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -0700518 errno = EBUSY;
519 return -1;
520 }
521
San Mehata2677e42009-12-13 10:40:18 -0800522 return v->unmountVol();
San Mehat49e2bce2009-10-12 16:29:01 -0700523}
524
San Mehata2677e42009-12-13 10:40:18 -0800525/*
526 * Looks up a volume by it's label or mount-point
527 */
San Mehat49e2bce2009-10-12 16:29:01 -0700528Volume *VolumeManager::lookupVolume(const char *label) {
529 VolumeCollection::iterator i;
530
531 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -0800532 if (label[0] == '/') {
533 if (!strcmp(label, (*i)->getMountpoint()))
534 return (*i);
535 } else {
536 if (!strcmp(label, (*i)->getLabel()))
537 return (*i);
538 }
San Mehat49e2bce2009-10-12 16:29:01 -0700539 }
540 return NULL;
541}
San Mehata19b2502010-01-06 10:33:53 -0800542
543bool VolumeManager::isMountpointMounted(const char *mp)
544{
545 char device[256];
546 char mount_path[256];
547 char rest[256];
548 FILE *fp;
549 char line[1024];
550
551 if (!(fp = fopen("/proc/mounts", "r"))) {
552 LOGE("Error opening /proc/mounts (%s)", strerror(errno));
553 return false;
554 }
555
556 while(fgets(line, sizeof(line), fp)) {
557 line[strlen(line)-1] = '\0';
558 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
559 if (!strcmp(mount_path, mp)) {
560 fclose(fp);
561 return true;
562 }
563
564 }
565
566 fclose(fp);
567 return false;
568}
569