VolumeManager.cpp revision 68f8ebdb24dfe1fe94de2c8fc11084ebfab9fa5d
1/* 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 <stdlib.h> 19#include <string.h> 20#include <errno.h> 21#include <fcntl.h> 22#include <sys/stat.h> 23#include <sys/types.h> 24#include <sys/mount.h> 25 26#include <linux/kdev_t.h> 27 28#define LOG_TAG "Vold" 29 30#include <cutils/log.h> 31 32#include <sysutils/NetlinkEvent.h> 33 34#include "VolumeManager.h" 35#include "DirectVolume.h" 36#include "ResponseCode.h" 37#include "Loop.h" 38#include "Fat.h" 39#include "Devmapper.h" 40 41extern "C" void KillProcessesWithOpenFiles(const char *, int, int, int); 42 43VolumeManager *VolumeManager::sInstance = NULL; 44 45VolumeManager *VolumeManager::Instance() { 46 if (!sInstance) 47 sInstance = new VolumeManager(); 48 return sInstance; 49} 50 51VolumeManager::VolumeManager() { 52 mBlockDevices = new BlockDeviceCollection(); 53 mVolumes = new VolumeCollection(); 54 mActiveContainers = new AsecIdCollection(); 55 mBroadcaster = NULL; 56 mUsbMassStorageConnected = false; 57} 58 59VolumeManager::~VolumeManager() { 60 delete mBlockDevices; 61 delete mVolumes; 62 delete mActiveContainers; 63} 64 65int VolumeManager::start() { 66 return 0; 67} 68 69int VolumeManager::stop() { 70 return 0; 71} 72 73int VolumeManager::addVolume(Volume *v) { 74 mVolumes->push_back(v); 75 return 0; 76} 77 78void VolumeManager::notifyUmsConnected(bool connected) { 79 char msg[255]; 80 81 if (connected) { 82 mUsbMassStorageConnected = true; 83 } else { 84 mUsbMassStorageConnected = false; 85 } 86 snprintf(msg, sizeof(msg), "Share method ums now %s", 87 (connected ? "available" : "unavailable")); 88 89 getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, 90 msg, false); 91} 92 93void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { 94 const char *devpath = evt->findParam("DEVPATH"); 95 const char *name = evt->findParam("SWITCH_NAME"); 96 const char *state = evt->findParam("SWITCH_STATE"); 97 98 if (!name || !state) { 99 LOGW("Switch %s event missing name/state info", devpath); 100 return; 101 } 102 103 if (!strcmp(name, "usb_mass_storage")) { 104 105 if (!strcmp(state, "online")) { 106 notifyUmsConnected(true); 107 } else { 108 notifyUmsConnected(false); 109 } 110 } else { 111 LOGW("Ignoring unknown switch '%s'", name); 112 } 113} 114 115void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { 116 const char *devpath = evt->findParam("DEVPATH"); 117 118 /* Lookup a volume to handle this device */ 119 VolumeCollection::iterator it; 120 bool hit = false; 121 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { 122 if (!(*it)->handleBlockEvent(evt)) { 123#ifdef NETLINK_DEBUG 124 LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); 125#endif 126 hit = true; 127 break; 128 } 129 } 130 131 if (!hit) { 132#ifdef NETLINK_DEBUG 133 LOGW("No volumes handled block event for '%s'", devpath); 134#endif 135 } 136} 137 138int VolumeManager::listVolumes(SocketClient *cli) { 139 VolumeCollection::iterator i; 140 141 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { 142 char *buffer; 143 asprintf(&buffer, "%s %s %d", 144 (*i)->getLabel(), (*i)->getMountpoint(), 145 (*i)->getState()); 146 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); 147 free(buffer); 148 } 149 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); 150 return 0; 151} 152 153int VolumeManager::formatVolume(const char *label) { 154 Volume *v = lookupVolume(label); 155 156 if (!v) { 157 errno = ENOENT; 158 return -1; 159 } 160 161 return v->formatVol(); 162} 163 164int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) { 165 char mountPoint[255]; 166 167 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); 168 snprintf(buffer, maxlen, "/asec/%s", id); 169 return 0; 170} 171 172int VolumeManager::createAsec(const char *id, unsigned int numSectors, 173 const char *fstype, const char *key, int ownerUid) { 174 175 mkdir("/sdcard/android_secure", 0777); 176 177 if (lookupVolume(id)) { 178 LOGE("ASEC volume '%s' currently exists", id); 179 errno = EADDRINUSE; 180 return -1; 181 } 182 183 char asecFileName[255]; 184 snprintf(asecFileName, sizeof(asecFileName), 185 "/sdcard/android_secure/%s.asec", id); 186 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 194 if (Loop::createImageFile(asecFileName, numSectors)) { 195 LOGE("ASEC image file creation failed (%s)", strerror(errno)); 196 return -1; 197 } 198 199 char loopDevice[255]; 200 if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) { 201 LOGE("ASEC loop device creation failed (%s)", strerror(errno)); 202 unlink(asecFileName); 203 return -1; 204 } 205 206 char dmDevice[255]; 207 bool cleanupDm = false; 208 209 if (strcmp(key, "none")) { 210 if (Devmapper::create(id, loopDevice, key, numSectors, dmDevice, 211 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)) { 223 LOGE("ASEC FAT format failed (%s)", strerror(errno)); 224 if (cleanupDm) { 225 Devmapper::destroy(id); 226 } 227 Loop::destroyByDevice(loopDevice); 228 unlink(asecFileName); 229 return -1; 230 } 231 232 char mountPoint[255]; 233 234 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); 235 if (mkdir(mountPoint, 0777)) { 236 if (errno != EEXIST) { 237 LOGE("Mountpoint creation failed (%s)", strerror(errno)); 238 if (cleanupDm) { 239 Devmapper::destroy(id); 240 } 241 Loop::destroyByDevice(loopDevice); 242 unlink(asecFileName); 243 return -1; 244 } 245 } 246 247 if (Fat::doMount(dmDevice, mountPoint, false, false, ownerUid, 248 0, 0000, false)) { 249// 0, 0007, false)) { 250 LOGE("ASEC FAT mount failed (%s)", strerror(errno)); 251 if (cleanupDm) { 252 Devmapper::destroy(id); 253 } 254 Loop::destroyByDevice(loopDevice); 255 unlink(asecFileName); 256 return -1; 257 } 258 259 mActiveContainers->push_back(strdup(id)); 260 return 0; 261} 262 263int VolumeManager::finalizeAsec(const char *id) { 264 char asecFileName[255]; 265 char loopDevice[255]; 266 char mountPoint[255]; 267 268 snprintf(asecFileName, sizeof(asecFileName), 269 "/sdcard/android_secure/%s.asec", id); 270 271 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { 272 LOGE("Unable to finalize %s (%s)", id, strerror(errno)); 273 return -1; 274 } 275 276 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); 277 // XXX: 278 if (Fat::doMount(loopDevice, mountPoint, true, true, 0, 0, 0227, false)) { 279 LOGE("ASEC finalize mount failed (%s)", strerror(errno)); 280 return -1; 281 } 282 283 LOGD("ASEC %s finalized", id); 284 return 0; 285} 286 287int VolumeManager::unmountAsec(const char *id) { 288 char asecFileName[255]; 289 char mountPoint[255]; 290 291 snprintf(asecFileName, sizeof(asecFileName), 292 "/sdcard/android_secure/%s.asec", id); 293 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); 294 295 if (!isMountpointMounted(mountPoint)) { 296 LOGE("Unmount request for ASEC %s when not mounted", id); 297 errno = EINVAL; 298 return -1; 299 } 300 301 int i, rc; 302 for (i = 0; i < 10; i++) { 303 rc = umount(mountPoint); 304 if (!rc) { 305 break; 306 } 307 if (rc && (errno == EINVAL || errno == ENOENT)) { 308 rc = 0; 309 break; 310 } 311 LOGW("ASEC %s unmount attempt %d failed (%s)", 312 id, i +1, strerror(errno)); 313 314 if (i >= 5) { 315 KillProcessesWithOpenFiles(mountPoint, (i < 7 ? 0 : 1), 316 NULL, 0); 317 } 318 usleep(1000 * 250); 319 } 320 321 if (rc) { 322 LOGE("Failed to unmount ASEC %s", id); 323 return -1; 324 } 325 326 unlink(mountPoint); 327 328 if (Devmapper::destroy(id) && errno != ENXIO) { 329 LOGE("Failed to destroy devmapper instance (%s)", strerror(errno)); 330 } 331 332 char loopDevice[255]; 333 if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { 334 Loop::destroyByDevice(loopDevice); 335 } 336 337 AsecIdCollection::iterator it; 338 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) { 339 if (!strcmp(*it, id)) { 340 free(*it); 341 mActiveContainers->erase(it); 342 break; 343 } 344 } 345 if (it == mActiveContainers->end()) { 346 LOGW("mActiveContainers is inconsistent!"); 347 } 348 return 0; 349} 350 351int VolumeManager::destroyAsec(const char *id) { 352 char asecFileName[255]; 353 char mountPoint[255]; 354 355 snprintf(asecFileName, sizeof(asecFileName), 356 "/sdcard/android_secure/%s.asec", id); 357 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); 358 359 if (isMountpointMounted(mountPoint)) { 360 LOGD("Unmounting container before destroy"); 361 if (unmountAsec(id)) { 362 LOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno)); 363 return -1; 364 } 365 } 366 367 if (unlink(asecFileName)) { 368 LOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno)); 369 return -1; 370 } 371 372 LOGD("ASEC %s destroyed", id); 373 return 0; 374} 375 376int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) { 377 char asecFileName[255]; 378 char mountPoint[255]; 379 380 snprintf(asecFileName, sizeof(asecFileName), 381 "/sdcard/android_secure/%s.asec", id); 382 snprintf(mountPoint, sizeof(mountPoint), "/asec/%s", id); 383 384 if (isMountpointMounted(mountPoint)) { 385 LOGE("ASEC %s already mounted", id); 386 errno = EBUSY; 387 return -1; 388 } 389 390 char loopDevice[255]; 391 if (Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) { 392 if (Loop::create(asecFileName, loopDevice, sizeof(loopDevice))) { 393 LOGE("ASEC loop device creation failed (%s)", strerror(errno)); 394 return -1; 395 } 396 LOGD("New loop device created at %s", loopDevice); 397 } else { 398 LOGD("Found active loopback for %s at %s", asecFileName, loopDevice); 399 } 400 401 char dmDevice[255]; 402 bool cleanupDm = false; 403 if (strcmp(key, "none")) { 404 if (Devmapper::lookupActive(id, dmDevice, sizeof(dmDevice))) { 405 unsigned int nr_sec = 0; 406 int fd; 407 408 if ((fd = open(loopDevice, O_RDWR)) < 0) { 409 LOGE("Failed to open loopdevice (%s)", strerror(errno)); 410 Loop::destroyByDevice(loopDevice); 411 return -1; 412 } 413 414 if (ioctl(fd, BLKGETSIZE, &nr_sec)) { 415 LOGE("Failed to get loop size (%s)", strerror(errno)); 416 Loop::destroyByDevice(loopDevice); 417 close(fd); 418 return -1; 419 } 420 close(fd); 421 if (Devmapper::create(id, loopDevice, key, nr_sec, 422 dmDevice, sizeof(dmDevice))) { 423 LOGE("ASEC device mapping failed (%s)", strerror(errno)); 424 Loop::destroyByDevice(loopDevice); 425 return -1; 426 } 427 LOGD("New devmapper instance created at %s", dmDevice); 428 } else { 429 LOGD("Found active devmapper for %s at %s", asecFileName, dmDevice); 430 } 431 cleanupDm = true; 432 } else { 433 strcpy(dmDevice, loopDevice); 434 } 435 436 if (mkdir(mountPoint, 0777)) { 437 if (errno != EEXIST) { 438 LOGE("Mountpoint creation failed (%s)", strerror(errno)); 439 if (cleanupDm) { 440 Devmapper::destroy(id); 441 } 442 Loop::destroyByDevice(loopDevice); 443 return -1; 444 } 445 } 446 447 if (Fat::doMount(dmDevice, mountPoint, true, false, ownerUid, 0, 448 0222, false)) { 449// 0227, false)) { 450 LOGE("ASEC mount failed (%s)", strerror(errno)); 451 if (cleanupDm) { 452 Devmapper::destroy(id); 453 } 454 Loop::destroyByDevice(loopDevice); 455 return -1; 456 } 457 458 mActiveContainers->push_back(strdup(id)); 459 LOGD("ASEC %s mounted", id); 460 return 0; 461} 462 463int VolumeManager::mountVolume(const char *label) { 464 Volume *v = lookupVolume(label); 465 466 if (!v) { 467 errno = ENOENT; 468 return -1; 469 } 470 471 return v->mountVol(); 472} 473 474int VolumeManager::shareAvailable(const char *method, bool *avail) { 475 476 if (strcmp(method, "ums")) { 477 errno = ENOSYS; 478 return -1; 479 } 480 481 if (mUsbMassStorageConnected) 482 *avail = true; 483 else 484 *avail = false; 485 return 0; 486} 487 488int VolumeManager::simulate(const char *cmd, const char *arg) { 489 490 if (!strcmp(cmd, "ums")) { 491 if (!strcmp(arg, "connect")) { 492 notifyUmsConnected(true); 493 } else if (!strcmp(arg, "disconnect")) { 494 notifyUmsConnected(false); 495 } else { 496 errno = EINVAL; 497 return -1; 498 } 499 } else { 500 errno = EINVAL; 501 return -1; 502 } 503 return 0; 504} 505 506int VolumeManager::shareVolume(const char *label, const char *method) { 507 Volume *v = lookupVolume(label); 508 509 if (!v) { 510 errno = ENOENT; 511 return -1; 512 } 513 514 /* 515 * Eventually, we'll want to support additional share back-ends, 516 * some of which may work while the media is mounted. For now, 517 * we just support UMS 518 */ 519 if (strcmp(method, "ums")) { 520 errno = ENOSYS; 521 return -1; 522 } 523 524 if (v->getState() == Volume::State_NoMedia) { 525 errno = ENODEV; 526 return -1; 527 } 528 529 if (v->getState() != Volume::State_Idle) { 530 // You need to unmount manually befoe sharing 531 errno = EBUSY; 532 return -1; 533 } 534 535 dev_t d = v->getDiskDevice(); 536 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { 537 // This volume does not support raw disk access 538 errno = EINVAL; 539 return -1; 540 } 541 542 int fd; 543 char nodepath[255]; 544 snprintf(nodepath, 545 sizeof(nodepath), "/dev/block/vold/%d:%d", 546 MAJOR(d), MINOR(d)); 547 548 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", 549 O_WRONLY)) < 0) { 550 LOGE("Unable to open ums lunfile (%s)", strerror(errno)); 551 return -1; 552 } 553 554 if (write(fd, nodepath, strlen(nodepath)) < 0) { 555 LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); 556 close(fd); 557 return -1; 558 } 559 560 close(fd); 561 v->handleVolumeShared(); 562 return 0; 563} 564 565int VolumeManager::unshareVolume(const char *label, const char *method) { 566 Volume *v = lookupVolume(label); 567 568 if (!v) { 569 errno = ENOENT; 570 return -1; 571 } 572 573 if (strcmp(method, "ums")) { 574 errno = ENOSYS; 575 return -1; 576 } 577 578 if (v->getState() != Volume::State_Shared) { 579 errno = EINVAL; 580 return -1; 581 } 582 583 dev_t d = v->getDiskDevice(); 584 585 int fd; 586 char nodepath[255]; 587 snprintf(nodepath, 588 sizeof(nodepath), "/dev/block/vold/%d:%d", 589 MAJOR(d), MINOR(d)); 590 591 if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) { 592 LOGE("Unable to open ums lunfile (%s)", strerror(errno)); 593 return -1; 594 } 595 596 char ch = 0; 597 if (write(fd, &ch, 1) < 0) { 598 LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); 599 close(fd); 600 return -1; 601 } 602 603 close(fd); 604 v->handleVolumeUnshared(); 605 return 0; 606} 607 608int VolumeManager::unmountVolume(const char *label) { 609 Volume *v = lookupVolume(label); 610 611 if (!v) { 612 errno = ENOENT; 613 return -1; 614 } 615 616 if (v->getState() == Volume::State_NoMedia) { 617 errno = ENODEV; 618 return -1; 619 } 620 621 if (v->getState() != Volume::State_Mounted) { 622 LOGW("Attempt to unmount volume which isn't mounted (%d)\n", 623 v->getState()); 624 errno = EBUSY; 625 return -1; 626 } 627 628 while(mActiveContainers->size()) { 629 AsecIdCollection::iterator it = mActiveContainers->begin(); 630 LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint()); 631 if (unmountAsec(*it)) { 632 LOGE("Failed to unmount ASEC %s (%s) - unmount of %s may fail!", *it, 633 strerror(errno), v->getMountpoint()); 634 } 635 } 636 637 return v->unmountVol(); 638} 639 640/* 641 * Looks up a volume by it's label or mount-point 642 */ 643Volume *VolumeManager::lookupVolume(const char *label) { 644 VolumeCollection::iterator i; 645 646 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { 647 if (label[0] == '/') { 648 if (!strcmp(label, (*i)->getMountpoint())) 649 return (*i); 650 } else { 651 if (!strcmp(label, (*i)->getLabel())) 652 return (*i); 653 } 654 } 655 return NULL; 656} 657 658bool VolumeManager::isMountpointMounted(const char *mp) 659{ 660 char device[256]; 661 char mount_path[256]; 662 char rest[256]; 663 FILE *fp; 664 char line[1024]; 665 666 if (!(fp = fopen("/proc/mounts", "r"))) { 667 LOGE("Error opening /proc/mounts (%s)", strerror(errno)); 668 return false; 669 } 670 671 while(fgets(line, sizeof(line), fp)) { 672 line[strlen(line)-1] = '\0'; 673 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); 674 if (!strcmp(mount_path, mp)) { 675 fclose(fp); 676 return true; 677 } 678 679 } 680 681 fclose(fp); 682 return false; 683} 684 685