DirectVolume.cpp revision 137858b43b7e0ed46fb8ebce9230eb40f0a62432
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 22#include <linux/kdev_t.h> 23 24#define LOG_TAG "DirectVolume" 25 26#include <cutils/log.h> 27#include <sysutils/NetlinkEvent.h> 28 29#include "DirectVolume.h" 30#include "VolumeManager.h" 31#include "ResponseCode.h" 32 33// #define PARTITION_DEBUG 34 35DirectVolume::DirectVolume(VolumeManager *vm, const char *label, 36 const char *mount_point, int partIdx) : 37 Volume(vm, label, mount_point) { 38 mPartIdx = partIdx; 39 40 mPaths = new PathCollection(); 41 for (int i = 0; i < MAX_PARTITIONS; i++) 42 mPartMinors[i] = -1; 43 mPendingPartMap = 0; 44 mDiskMajor = -1; 45 mDiskMinor = -1; 46 mDiskNumParts = 0; 47 48 setState(Volume::State_NoMedia); 49} 50 51DirectVolume::~DirectVolume() { 52 PathCollection::iterator it; 53 54 for (it = mPaths->begin(); it != mPaths->end(); ++it) 55 free(*it); 56 delete mPaths; 57} 58 59int DirectVolume::addPath(const char *path) { 60 mPaths->push_back(strdup(path)); 61 return 0; 62} 63 64dev_t DirectVolume::getDiskDevice() { 65 return MKDEV(mDiskMajor, mDiskMinor); 66} 67 68dev_t DirectVolume::getShareDevice() { 69 if (mPartIdx != -1) { 70 return MKDEV(mDiskMajor, mPartIdx); 71 } else { 72 return MKDEV(mDiskMajor, mDiskMinor); 73 } 74} 75 76void DirectVolume::handleVolumeShared() { 77 setState(Volume::State_Shared); 78} 79 80void DirectVolume::handleVolumeUnshared() { 81 setState(Volume::State_Idle); 82} 83 84int DirectVolume::handleBlockEvent(NetlinkEvent *evt) { 85 const char *dp = evt->findParam("DEVPATH"); 86 87 PathCollection::iterator it; 88 for (it = mPaths->begin(); it != mPaths->end(); ++it) { 89 if (!strncmp(dp, *it, strlen(*it))) { 90 /* We can handle this disk */ 91 int action = evt->getAction(); 92 const char *devtype = evt->findParam("DEVTYPE"); 93 94 if (action == NetlinkEvent::NlActionAdd) { 95 int major = atoi(evt->findParam("MAJOR")); 96 int minor = atoi(evt->findParam("MINOR")); 97 char nodepath[255]; 98 99 snprintf(nodepath, 100 sizeof(nodepath), "/dev/block/vold/%d:%d", 101 major, minor); 102 if (createDeviceNode(nodepath, major, minor)) { 103 SLOGE("Error making device node '%s' (%s)", nodepath, 104 strerror(errno)); 105 } 106 if (!strcmp(devtype, "disk")) { 107 handleDiskAdded(dp, evt); 108 } else { 109 handlePartitionAdded(dp, evt); 110 } 111 } else if (action == NetlinkEvent::NlActionRemove) { 112 if (!strcmp(devtype, "disk")) { 113 handleDiskRemoved(dp, evt); 114 } else { 115 handlePartitionRemoved(dp, evt); 116 } 117 } else if (action == NetlinkEvent::NlActionChange) { 118 if (!strcmp(devtype, "disk")) { 119 handleDiskChanged(dp, evt); 120 } else { 121 handlePartitionChanged(dp, evt); 122 } 123 } else { 124 SLOGW("Ignoring non add/remove/change event"); 125 } 126 127 return 0; 128 } 129 } 130 errno = ENODEV; 131 return -1; 132} 133 134void DirectVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) { 135 mDiskMajor = atoi(evt->findParam("MAJOR")); 136 mDiskMinor = atoi(evt->findParam("MINOR")); 137 138 const char *tmp = evt->findParam("NPARTS"); 139 if (tmp) { 140 mDiskNumParts = atoi(tmp); 141 } else { 142 SLOGW("Kernel block uevent missing 'NPARTS'"); 143 mDiskNumParts = 1; 144 } 145 146 char msg[255]; 147 148 int partmask = 0; 149 int i; 150 for (i = 1; i <= mDiskNumParts; i++) { 151 partmask |= (1 << i); 152 } 153 mPendingPartMap = partmask; 154 155 if (mDiskNumParts == 0) { 156#ifdef PARTITION_DEBUG 157 SLOGD("Dv::diskIns - No partitions - good to go son!"); 158#endif 159 setState(Volume::State_Idle); 160 } else { 161#ifdef PARTITION_DEBUG 162 SLOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)", 163 mDiskNumParts, mPendingPartMap); 164#endif 165 setState(Volume::State_Pending); 166 } 167 168 snprintf(msg, sizeof(msg), "Volume %s %s disk inserted (%d:%d)", 169 getLabel(), getMountpoint(), mDiskMajor, mDiskMinor); 170 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted, 171 msg, false); 172} 173 174void DirectVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) { 175 int major = atoi(evt->findParam("MAJOR")); 176 int minor = atoi(evt->findParam("MINOR")); 177 178 int part_num; 179 180 const char *tmp = evt->findParam("PARTN"); 181 182 if (tmp) { 183 part_num = atoi(tmp); 184 } else { 185 SLOGW("Kernel block uevent missing 'PARTN'"); 186 part_num = 1; 187 } 188 189 if (part_num > MAX_PARTITIONS || part_num < 1) { 190 SLOGW("Invalid 'PARTN' value"); 191 part_num = 1; 192 } 193 194 if (part_num > mDiskNumParts) { 195 mDiskNumParts = part_num; 196 } 197 198 if (major != mDiskMajor) { 199 SLOGE("Partition '%s' has a different major than its disk!", devpath); 200 return; 201 } 202#ifdef PARTITION_DEBUG 203 SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor); 204#endif 205 if (part_num >= MAX_PARTITIONS) { 206 SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1); 207 } else { 208 mPartMinors[part_num -1] = minor; 209 } 210 mPendingPartMap &= ~(1 << part_num); 211 212 if (!mPendingPartMap) { 213#ifdef PARTITION_DEBUG 214 SLOGD("Dv:partAdd: Got all partitions - ready to rock!"); 215#endif 216 if (getState() != Volume::State_Formatting) { 217 setState(Volume::State_Idle); 218 } 219 } else { 220#ifdef PARTITION_DEBUG 221 SLOGD("Dv:partAdd: pending mask now = 0x%x", mPendingPartMap); 222#endif 223 } 224} 225 226void DirectVolume::handleDiskChanged(const char *devpath, NetlinkEvent *evt) { 227 int major = atoi(evt->findParam("MAJOR")); 228 int minor = atoi(evt->findParam("MINOR")); 229 230 if ((major != mDiskMajor) || (minor != mDiskMinor)) { 231 return; 232 } 233 234 SLOGI("Volume %s disk has changed", getLabel()); 235 const char *tmp = evt->findParam("NPARTS"); 236 if (tmp) { 237 mDiskNumParts = atoi(tmp); 238 } else { 239 SLOGW("Kernel block uevent missing 'NPARTS'"); 240 mDiskNumParts = 1; 241 } 242 243 int partmask = 0; 244 int i; 245 for (i = 1; i <= mDiskNumParts; i++) { 246 partmask |= (1 << i); 247 } 248 mPendingPartMap = partmask; 249 250 if (getState() != Volume::State_Formatting) { 251 if (mDiskNumParts == 0) { 252 setState(Volume::State_Idle); 253 } else { 254 setState(Volume::State_Pending); 255 } 256 } 257} 258 259void DirectVolume::handlePartitionChanged(const char *devpath, NetlinkEvent *evt) { 260 int major = atoi(evt->findParam("MAJOR")); 261 int minor = atoi(evt->findParam("MINOR")); 262 SLOGD("Volume %s %s partition %d:%d changed\n", getLabel(), getMountpoint(), major, minor); 263} 264 265void DirectVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) { 266 int major = atoi(evt->findParam("MAJOR")); 267 int minor = atoi(evt->findParam("MINOR")); 268 char msg[255]; 269 270 SLOGD("Volume %s %s disk %d:%d removed\n", getLabel(), getMountpoint(), major, minor); 271 snprintf(msg, sizeof(msg), "Volume %s %s disk removed (%d:%d)", 272 getLabel(), getMountpoint(), major, minor); 273 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskRemoved, 274 msg, false); 275 setState(Volume::State_NoMedia); 276} 277 278void DirectVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) { 279 int major = atoi(evt->findParam("MAJOR")); 280 int minor = atoi(evt->findParam("MINOR")); 281 char msg[255]; 282 int state; 283 284 SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor); 285 286 /* 287 * The framework doesn't need to get notified of 288 * partition removal unless it's mounted. Otherwise 289 * the removal notification will be sent on the Disk 290 * itself 291 */ 292 state = getState(); 293 if (state != Volume::State_Mounted && state != Volume::State_Shared) { 294 return; 295 } 296 297 if ((dev_t) MKDEV(major, minor) == mCurrentlyMountedKdev) { 298 /* 299 * Yikes, our mounted partition is going away! 300 */ 301 302 snprintf(msg, sizeof(msg), "Volume %s %s bad removal (%d:%d)", 303 getLabel(), getMountpoint(), major, minor); 304 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval, 305 msg, false); 306 307 if (mVm->cleanupAsec(this, true)) { 308 SLOGE("Failed to cleanup ASEC - unmount will probably fail!"); 309 } 310 311 if (Volume::unmountVol(true)) { 312 SLOGE("Failed to unmount volume on bad removal (%s)", 313 strerror(errno)); 314 // XXX: At this point we're screwed for now 315 } else { 316 SLOGD("Crisis averted"); 317 } 318 } else if (state == Volume::State_Shared) { 319 /* removed during mass storage */ 320 snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)", 321 getLabel(), major, minor); 322 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval, 323 msg, false); 324 325 if (mVm->unshareVolume(getLabel(), "ums")) { 326 SLOGE("Failed to unshare volume on bad removal (%s)", 327 strerror(errno)); 328 } else { 329 SLOGD("Crisis averted"); 330 } 331 } 332} 333 334/* 335 * Called from base to get a list of devicenodes for mounting 336 */ 337int DirectVolume::getDeviceNodes(dev_t *devs, int max) { 338 339 if (mPartIdx == -1) { 340 // If the disk has no partitions, try the disk itself 341 if (!mDiskNumParts) { 342 devs[0] = MKDEV(mDiskMajor, mDiskMinor); 343 return 1; 344 } 345 346 int i; 347 for (i = 0; i < mDiskNumParts; i++) { 348 if (i == max) 349 break; 350 devs[i] = MKDEV(mDiskMajor, mPartMinors[i]); 351 } 352 return mDiskNumParts; 353 } 354 devs[0] = MKDEV(mDiskMajor, mPartMinors[mPartIdx -1]); 355 return 1; 356} 357