San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 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> |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 20 | #include <errno.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 21 | #include <fcntl.h> |
| 22 | #include <linux/kdev_t.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 23 | |
| 24 | #define LOG_TAG "Vold" |
| 25 | |
| 26 | #include <cutils/log.h> |
| 27 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 28 | #include <sysutils/NetlinkEvent.h> |
| 29 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 30 | #include "VolumeManager.h" |
San Mehat | ae10b91 | 2009-10-12 14:57:05 -0700 | [diff] [blame] | 31 | #include "DirectVolume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 32 | #include "ResponseCode.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 33 | |
| 34 | VolumeManager *VolumeManager::sInstance = NULL; |
| 35 | |
| 36 | VolumeManager *VolumeManager::Instance() { |
| 37 | if (!sInstance) |
| 38 | sInstance = new VolumeManager(); |
| 39 | return sInstance; |
| 40 | } |
| 41 | |
| 42 | VolumeManager::VolumeManager() { |
| 43 | mBlockDevices = new BlockDeviceCollection(); |
| 44 | mVolumes = new VolumeCollection(); |
| 45 | mBroadcaster = NULL; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 46 | mUsbMassStorageConnected = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | VolumeManager::~VolumeManager() { |
| 50 | delete mBlockDevices; |
| 51 | } |
| 52 | |
| 53 | int VolumeManager::start() { |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | int VolumeManager::stop() { |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int VolumeManager::addVolume(Volume *v) { |
| 62 | mVolumes->push_back(v); |
| 63 | return 0; |
| 64 | } |
| 65 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 66 | void VolumeManager::notifyUmsConnected(bool connected) { |
| 67 | char msg[255]; |
| 68 | |
| 69 | if (connected) { |
| 70 | mUsbMassStorageConnected = true; |
| 71 | } else { |
| 72 | mUsbMassStorageConnected = false; |
| 73 | } |
| 74 | snprintf(msg, sizeof(msg), "Share method ums now %s", |
| 75 | (connected ? "available" : "unavailable")); |
| 76 | |
| 77 | getBroadcaster()->sendBroadcast(ResponseCode::ShareAvailabilityChange, |
| 78 | msg, false); |
| 79 | } |
| 80 | |
| 81 | void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) { |
| 82 | const char *name = evt->findParam("SWITCH_NAME"); |
| 83 | const char *state = evt->findParam("SWITCH_STATE"); |
| 84 | |
| 85 | if (!strcmp(name, "usb_mass_storage")) { |
| 86 | |
| 87 | if (!strcmp(state, "online")) { |
| 88 | notifyUmsConnected(true); |
| 89 | } else { |
| 90 | notifyUmsConnected(false); |
| 91 | } |
| 92 | } else { |
| 93 | LOGW("Ignoring unknown switch '%s'", name); |
| 94 | } |
| 95 | } |
| 96 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 97 | void VolumeManager::handleBlockEvent(NetlinkEvent *evt) { |
| 98 | const char *devpath = evt->findParam("DEVPATH"); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 99 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 100 | /* Lookup a volume to handle this device */ |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 101 | VolumeCollection::iterator it; |
| 102 | bool hit = false; |
| 103 | for (it = mVolumes->begin(); it != mVolumes->end(); ++it) { |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 104 | if (!(*it)->handleBlockEvent(evt)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 105 | #ifdef NETLINK_DEBUG |
| 106 | LOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel()); |
| 107 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 108 | hit = true; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (!hit) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 114 | #ifdef NETLINK_DEBUG |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 115 | LOGW("No volumes handled block event for '%s'", devpath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 116 | #endif |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 120 | int VolumeManager::listVolumes(SocketClient *cli) { |
| 121 | VolumeCollection::iterator i; |
| 122 | |
| 123 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
| 124 | char *buffer; |
| 125 | asprintf(&buffer, "%s %s %d", |
| 126 | (*i)->getLabel(), (*i)->getMountpoint(), |
| 127 | (*i)->getState()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 128 | cli->sendMsg(ResponseCode::VolumeListResult, buffer, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 129 | free(buffer); |
| 130 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 131 | cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 134 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 135 | int VolumeManager::formatVolume(const char *label) { |
| 136 | Volume *v = lookupVolume(label); |
| 137 | |
| 138 | if (!v) { |
| 139 | errno = ENOENT; |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | return v->formatVol(); |
| 144 | } |
| 145 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 146 | int VolumeManager::mountVolume(const char *label) { |
| 147 | Volume *v = lookupVolume(label); |
| 148 | |
| 149 | if (!v) { |
| 150 | errno = ENOENT; |
| 151 | return -1; |
| 152 | } |
| 153 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 154 | return v->mountVol(); |
| 155 | } |
| 156 | |
| 157 | int VolumeManager::shareAvailable(const char *method, bool *avail) { |
| 158 | |
| 159 | if (strcmp(method, "ums")) { |
| 160 | errno = ENOSYS; |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | if (mUsbMassStorageConnected) |
| 165 | *avail = true; |
| 166 | else |
| 167 | *avail = false; |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int VolumeManager::simulate(const char *cmd, const char *arg) { |
| 172 | |
| 173 | if (!strcmp(cmd, "ums")) { |
| 174 | if (!strcmp(arg, "connect")) { |
| 175 | notifyUmsConnected(true); |
| 176 | } else if (!strcmp(arg, "disconnect")) { |
| 177 | notifyUmsConnected(false); |
| 178 | } else { |
| 179 | errno = EINVAL; |
| 180 | return -1; |
| 181 | } |
| 182 | } else { |
| 183 | errno = EINVAL; |
| 184 | return -1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int VolumeManager::shareVolume(const char *label, const char *method) { |
| 190 | Volume *v = lookupVolume(label); |
| 191 | |
| 192 | if (!v) { |
| 193 | errno = ENOENT; |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Eventually, we'll want to support additional share back-ends, |
| 199 | * some of which may work while the media is mounted. For now, |
| 200 | * we just support UMS |
| 201 | */ |
| 202 | if (strcmp(method, "ums")) { |
| 203 | errno = ENOSYS; |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | if (v->getState() == Volume::State_NoMedia) { |
| 208 | errno = ENODEV; |
| 209 | return -1; |
| 210 | } |
| 211 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 212 | if (v->getState() != Volume::State_Idle) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 213 | // You need to unmount manually befoe sharing |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 214 | errno = EBUSY; |
| 215 | return -1; |
| 216 | } |
| 217 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 218 | dev_t d = v->getDiskDevice(); |
| 219 | if ((MAJOR(d) == 0) && (MINOR(d) == 0)) { |
| 220 | // This volume does not support raw disk access |
| 221 | errno = EINVAL; |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | int fd; |
| 226 | char nodepath[255]; |
| 227 | snprintf(nodepath, |
| 228 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 229 | MAJOR(d), MINOR(d)); |
| 230 | |
| 231 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0", O_WRONLY)) < 0) { |
| 232 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | if (write(fd, nodepath, strlen(nodepath)) < 0) { |
| 237 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 238 | close(fd); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | close(fd); |
| 243 | v->handleVolumeShared(); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | int VolumeManager::unshareVolume(const char *label, const char *method) { |
| 248 | Volume *v = lookupVolume(label); |
| 249 | |
| 250 | if (!v) { |
| 251 | errno = ENOENT; |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | if (strcmp(method, "ums")) { |
| 256 | errno = ENOSYS; |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | if (v->getState() != Volume::State_Shared) { |
| 261 | errno = EINVAL; |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | dev_t d = v->getDiskDevice(); |
| 266 | |
| 267 | int fd; |
| 268 | char nodepath[255]; |
| 269 | snprintf(nodepath, |
| 270 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 271 | MAJOR(d), MINOR(d)); |
| 272 | |
| 273 | if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0", O_WRONLY)) < 0) { |
| 274 | LOGE("Unable to open ums lunfile (%s)", strerror(errno)); |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | char ch = 0; |
| 279 | if (write(fd, &ch, 1) < 0) { |
| 280 | LOGE("Unable to write to ums lunfile (%s)", strerror(errno)); |
| 281 | close(fd); |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | close(fd); |
| 286 | v->handleVolumeUnshared(); |
| 287 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | int VolumeManager::unmountVolume(const char *label) { |
| 291 | Volume *v = lookupVolume(label); |
| 292 | |
| 293 | if (!v) { |
| 294 | errno = ENOENT; |
| 295 | return -1; |
| 296 | } |
| 297 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 298 | if (v->getState() == Volume::State_NoMedia) { |
| 299 | errno = ENODEV; |
| 300 | return -1; |
| 301 | } |
| 302 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 303 | if (v->getState() != Volume::State_Mounted) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 304 | LOGW("Attempt to unmount volume which isn't mounted (%d)\n", |
| 305 | v->getState()); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 306 | errno = EBUSY; |
| 307 | return -1; |
| 308 | } |
| 309 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 310 | return v->unmountVol(); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 311 | } |
| 312 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 313 | /* |
| 314 | * Looks up a volume by it's label or mount-point |
| 315 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 316 | Volume *VolumeManager::lookupVolume(const char *label) { |
| 317 | VolumeCollection::iterator i; |
| 318 | |
| 319 | for (i = mVolumes->begin(); i != mVolumes->end(); ++i) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame^] | 320 | if (label[0] == '/') { |
| 321 | if (!strcmp(label, (*i)->getMountpoint())) |
| 322 | return (*i); |
| 323 | } else { |
| 324 | if (!strcmp(label, (*i)->getLabel())) |
| 325 | return (*i); |
| 326 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 327 | } |
| 328 | return NULL; |
| 329 | } |