blob: d2b87b65ae9b103563b18b6a90b4d390756be276 [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
San Mehat49e2bce2009-10-12 16:29:01 -070017#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070018#include <string.h>
San Mehat49e2bce2009-10-12 16:29:01 -070019#include <dirent.h>
20#include <errno.h>
21#include <fcntl.h>
22
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/mman.h>
San Mehata2677e42009-12-13 10:40:18 -080027#include <sys/mount.h>
28
29#include <linux/kdev_t.h>
30
31#include <cutils/properties.h>
32
San Mehat2a5b8ce2010-03-10 12:48:57 -080033#include <diskconfig/diskconfig.h>
San Mehatf1b736b2009-10-10 17:22:08 -070034
35#define LOG_TAG "Vold"
36
37#include <cutils/log.h>
38
39#include "Volume.h"
San Mehata2677e42009-12-13 10:40:18 -080040#include "VolumeManager.h"
41#include "ResponseCode.h"
San Mehatbf041852010-01-04 10:09:16 -080042#include "Fat.h"
San Mehat586536c2010-02-16 17:12:00 -080043#include "Process.h"
San Mehatf1b736b2009-10-10 17:22:08 -070044
San Mehata2677e42009-12-13 10:40:18 -080045extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d);
46extern "C" void dos_partition_enc(void *pp, struct dos_partition *d);
San Mehat49e2bce2009-10-12 16:29:01 -070047
San Mehat3bb60202010-02-19 18:14:36 -080048
49/*
50 * Secure directory - stuff that only root can see
51 */
52const char *Volume::SECDIR = "/mnt/secure";
53
54/*
55 * Secure staging directory - where media is mounted for preparation
56 */
57const char *Volume::SEC_STGDIR = "/mnt/secure/staging";
58
59/*
60 * Path to the directory on the media which contains publicly accessable
61 * asec imagefiles. This path will be obscured before the mount is
62 * exposed to non priviledged users.
63 */
San Mehat52c2ccb2010-02-23 18:26:13 -080064const char *Volume::SEC_STG_SECIMGDIR = "/mnt/secure/staging/.android_secure";
San Mehat3bb60202010-02-19 18:14:36 -080065
66/*
67 * Path to where *only* root can access asec imagefiles
68 */
69const char *Volume::SEC_ASECDIR = "/mnt/secure/asec";
70
71/*
72 * Path to where secure containers are mounted
73 */
74const char *Volume::ASECDIR = "/mnt/asec";
75
Kenny Rootfb7c4d52010-06-30 18:48:41 -070076/*
Kenny Root508c0e12010-07-12 09:59:49 -070077 * Path to where OBBs are mounted
Kenny Rootfb7c4d52010-06-30 18:48:41 -070078 */
Kenny Root508c0e12010-07-12 09:59:49 -070079const char *Volume::LOOPDIR = "/mnt/obb";
Kenny Rootfb7c4d52010-06-30 18:48:41 -070080
San Mehata2677e42009-12-13 10:40:18 -080081static const char *stateToStr(int state) {
82 if (state == Volume::State_Init)
83 return "Initializing";
84 else if (state == Volume::State_NoMedia)
85 return "No-Media";
86 else if (state == Volume::State_Idle)
87 return "Idle-Unmounted";
88 else if (state == Volume::State_Pending)
89 return "Pending";
90 else if (state == Volume::State_Mounted)
91 return "Mounted";
92 else if (state == Volume::State_Unmounting)
93 return "Unmounting";
94 else if (state == Volume::State_Checking)
95 return "Checking";
96 else if (state == Volume::State_Formatting)
97 return "Formatting";
98 else if (state == Volume::State_Shared)
99 return "Shared-Unmounted";
100 else if (state == Volume::State_SharedMnt)
101 return "Shared-Mounted";
102 else
103 return "Unknown-Error";
104}
105
106Volume::Volume(VolumeManager *vm, const char *label, const char *mount_point) {
107 mVm = vm;
San Mehatd9a4e352010-03-12 13:32:47 -0800108 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700109 mLabel = strdup(label);
110 mMountpoint = strdup(mount_point);
111 mState = Volume::State_Init;
San Mehata2677e42009-12-13 10:40:18 -0800112 mCurrentlyMountedKdev = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400113 mPartIdx = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700114}
115
116Volume::~Volume() {
117 free(mLabel);
118 free(mMountpoint);
119}
120
San Mehatcb4dac82010-03-14 13:41:54 -0700121void Volume::protectFromAutorunStupidity() {
122 char filename[255];
123
124 snprintf(filename, sizeof(filename), "%s/autorun.inf", SEC_STGDIR);
125 if (!access(filename, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700126 SLOGW("Volume contains an autorun.inf! - removing");
San Mehatcb4dac82010-03-14 13:41:54 -0700127 /*
128 * Ensure the filename is all lower-case so
129 * the process killer can find the inode.
130 * Probably being paranoid here but meh.
131 */
132 rename(filename, filename);
133 Process::killProcessesWithOpenFiles(filename, 2);
134 if (unlink(filename)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700135 SLOGE("Failed to remove %s (%s)", filename, strerror(errno));
San Mehatcb4dac82010-03-14 13:41:54 -0700136 }
137 }
138}
139
San Mehatd9a4e352010-03-12 13:32:47 -0800140void Volume::setDebug(bool enable) {
141 mDebug = enable;
142}
143
San Mehata2677e42009-12-13 10:40:18 -0800144dev_t Volume::getDiskDevice() {
145 return MKDEV(0, 0);
146};
147
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400148dev_t Volume::getShareDevice() {
149 return getDiskDevice();
150}
151
San Mehata2677e42009-12-13 10:40:18 -0800152void Volume::handleVolumeShared() {
153}
154
155void Volume::handleVolumeUnshared() {
156}
157
San Mehatfd7f5872009-10-12 11:32:47 -0700158int Volume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatf1b736b2009-10-10 17:22:08 -0700159 errno = ENOSYS;
160 return -1;
161}
162
163void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800164 char msg[255];
165 int oldState = mState;
166
167 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700168 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800169 return;
170 }
171
San Mehatf1b736b2009-10-10 17:22:08 -0700172 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800173
San Mehat97ac40e2010-03-24 10:24:19 -0700174 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800175 oldState, stateToStr(oldState), mState, stateToStr(mState));
176 snprintf(msg, sizeof(msg),
177 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
178 getMountpoint(), oldState, stateToStr(oldState), mState,
179 stateToStr(mState));
180
181 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
182 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700183}
San Mehat49e2bce2009-10-12 16:29:01 -0700184
San Mehatdd9b8e92009-10-21 11:06:52 -0700185int Volume::createDeviceNode(const char *path, int major, int minor) {
186 mode_t mode = 0660 | S_IFBLK;
187 dev_t dev = (major << 8) | minor;
188 if (mknod(path, mode, dev) < 0) {
189 if (errno != EEXIST) {
190 return -1;
191 }
192 }
193 return 0;
194}
195
San Mehata2677e42009-12-13 10:40:18 -0800196int Volume::formatVol() {
San Mehat49e2bce2009-10-12 16:29:01 -0700197
San Mehata2677e42009-12-13 10:40:18 -0800198 if (getState() == Volume::State_NoMedia) {
199 errno = ENODEV;
200 return -1;
201 } else if (getState() != Volume::State_Idle) {
202 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700203 return -1;
204 }
205
San Mehata2677e42009-12-13 10:40:18 -0800206 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700207 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800208 setState(Volume::State_Mounted);
209 // mCurrentlyMountedKdev = XXX
210 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700211 return -1;
212 }
213
Mike Lockwooda4886f12010-09-21 13:56:35 -0400214 bool formatEntireDevice = (mPartIdx == -1);
San Mehata2677e42009-12-13 10:40:18 -0800215 char devicePath[255];
216 dev_t diskNode = getDiskDevice();
Mike Lockwooda4886f12010-09-21 13:56:35 -0400217 dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800218
San Mehat2a5b8ce2010-03-10 12:48:57 -0800219 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800220
Mike Lockwooda4886f12010-09-21 13:56:35 -0400221 // Only initialize the MBR if we are formatting the entire device
222 if (formatEntireDevice) {
223 sprintf(devicePath, "/dev/block/vold/%d:%d",
224 MAJOR(diskNode), MINOR(diskNode));
225
226 if (initializeMbr(devicePath)) {
227 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
228 goto err;
229 }
San Mehat49e2bce2009-10-12 16:29:01 -0700230 }
231
San Mehata2677e42009-12-13 10:40:18 -0800232 sprintf(devicePath, "/dev/block/vold/%d:%d",
233 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700234
Mike Lockwooda4886f12010-09-21 13:56:35 -0400235 if (mDebug) {
236 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
237 }
238
San Mehatfcf24fe2010-03-03 12:37:32 -0800239 if (Fat::format(devicePath, 0)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700240 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800241 goto err;
242 }
243
San Mehat2a5b8ce2010-03-10 12:48:57 -0800244 setState(Volume::State_Idle);
San Mehat49e2bce2009-10-12 16:29:01 -0700245 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800246err:
247 return -1;
248}
249
250bool Volume::isMountpointMounted(const char *path) {
251 char device[256];
252 char mount_path[256];
253 char rest[256];
254 FILE *fp;
255 char line[1024];
256
257 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700258 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800259 return false;
260 }
261
262 while(fgets(line, sizeof(line), fp)) {
263 line[strlen(line)-1] = '\0';
264 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
265 if (!strcmp(mount_path, path)) {
266 fclose(fp);
267 return true;
268 }
269
270 }
271
272 fclose(fp);
273 return false;
274}
275
276int Volume::mountVol() {
277 dev_t deviceNodes[4];
278 int n, i, rc = 0;
279 char errmsg[255];
280
281 if (getState() == Volume::State_NoMedia) {
282 snprintf(errmsg, sizeof(errmsg),
283 "Volume %s %s mount failed - no media",
284 getLabel(), getMountpoint());
285 mVm->getBroadcaster()->sendBroadcast(
286 ResponseCode::VolumeMountFailedNoMedia,
287 errmsg, false);
288 errno = ENODEV;
289 return -1;
290 } else if (getState() != Volume::State_Idle) {
291 errno = EBUSY;
292 return -1;
293 }
294
295 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700296 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800297 setState(Volume::State_Mounted);
298 // mCurrentlyMountedKdev = XXX
299 return 0;
300 }
301
302 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
303 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700304 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800305 return -1;
306 }
307
308 for (i = 0; i < n; i++) {
309 char devicePath[255];
310
311 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
312 MINOR(deviceNodes[i]));
313
San Mehat97ac40e2010-03-24 10:24:19 -0700314 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800315
316 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800317 setState(Volume::State_Checking);
318
San Mehat3bb60202010-02-19 18:14:36 -0800319 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800320 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700321 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800322 continue;
San Mehata2677e42009-12-13 10:40:18 -0800323 }
San Mehateba65e92010-01-29 05:15:16 -0800324 errno = EIO;
325 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700326 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800327 setState(Volume::State_Idle);
328 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800329 }
330
San Mehat3bb60202010-02-19 18:14:36 -0800331 /*
332 * Mount the device on our internal staging mountpoint so we can
333 * muck with it before exposing it to non priviledged users.
334 */
San Mehata2677e42009-12-13 10:40:18 -0800335 errno = 0;
Kenny Roota3e06082010-08-27 08:31:35 -0700336 if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false,
337 1000, 1015, 0702, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700338 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800339 continue;
San Mehata2677e42009-12-13 10:40:18 -0800340 }
341
San Mehat97ac40e2010-03-24 10:24:19 -0700342 SLOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800343
San Mehatcb4dac82010-03-14 13:41:54 -0700344 protectFromAutorunStupidity();
345
San Mehat3bb60202010-02-19 18:14:36 -0800346 if (createBindMounts()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700347 SLOGE("Failed to create bindmounts (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800348 umount("/mnt/secure/staging");
349 setState(Volume::State_Idle);
350 return -1;
351 }
352
353 /*
354 * Now that the bindmount trickery is done, atomically move the
355 * whole subtree to expose it to non priviledged users.
356 */
357 if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700358 SLOGE("Failed to move mount (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800359 umount("/mnt/secure/staging");
360 setState(Volume::State_Idle);
361 return -1;
362 }
363 setState(Volume::State_Mounted);
364 mCurrentlyMountedKdev = deviceNodes[i];
365 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800366 }
367
San Mehat97ac40e2010-03-24 10:24:19 -0700368 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800369 setState(Volume::State_Idle);
370
San Mehateba65e92010-01-29 05:15:16 -0800371 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800372}
373
San Mehat3bb60202010-02-19 18:14:36 -0800374int Volume::createBindMounts() {
375 unsigned long flags;
376
377 /*
San Mehat52c2ccb2010-02-23 18:26:13 -0800378 * Rename old /android_secure -> /.android_secure
379 */
380 if (!access("/mnt/secure/staging/android_secure", R_OK | X_OK) &&
381 access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
382 if (rename("/mnt/secure/staging/android_secure", SEC_STG_SECIMGDIR)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700383 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800384 }
385 }
386
387 /*
San Mehat3bb60202010-02-19 18:14:36 -0800388 * Ensure that /android_secure exists and is a directory
389 */
390 if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
391 if (errno == ENOENT) {
392 if (mkdir(SEC_STG_SECIMGDIR, 0777)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700393 SLOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800394 return -1;
395 }
396 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700397 SLOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800398 return -1;
399 }
400 } else {
401 struct stat sbuf;
402
403 if (stat(SEC_STG_SECIMGDIR, &sbuf)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700404 SLOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800405 return -1;
406 }
407 if (!S_ISDIR(sbuf.st_mode)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700408 SLOGE("%s is not a directory", SEC_STG_SECIMGDIR);
San Mehat3bb60202010-02-19 18:14:36 -0800409 errno = ENOTDIR;
410 return -1;
411 }
412 }
413
414 /*
415 * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll
416 * have a root only accessable mountpoint for it.
417 */
418 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700419 SLOGE("Failed to bind mount points %s -> %s (%s)",
San Mehat3bb60202010-02-19 18:14:36 -0800420 SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno));
421 return -1;
422 }
423
424 /*
425 * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to
426 * obscure the underlying directory from everybody - sneaky eh? ;)
427 */
428 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700429 SLOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800430 umount("/mnt/asec_secure");
431 return -1;
432 }
433
434 return 0;
435}
436
437int Volume::doMoveMount(const char *src, const char *dst, bool force) {
438 unsigned int flags = MS_MOVE;
439 int retries = 5;
440
441 while(retries--) {
442 if (!mount(src, dst, "", flags, NULL)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800443 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700444 SLOGD("Moved mount %s -> %s sucessfully", src, dst);
San Mehatd9a4e352010-03-12 13:32:47 -0800445 }
San Mehat3bb60202010-02-19 18:14:36 -0800446 return 0;
447 } else if (errno != EBUSY) {
San Mehat97ac40e2010-03-24 10:24:19 -0700448 SLOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800449 return -1;
450 }
451 int action = 0;
452
453 if (force) {
454 if (retries == 1) {
455 action = 2; // SIGKILL
456 } else if (retries == 2) {
457 action = 1; // SIGHUP
458 }
459 }
San Mehat97ac40e2010-03-24 10:24:19 -0700460 SLOGW("Failed to move %s -> %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800461 src, dst, strerror(errno), retries, action);
462 Process::killProcessesWithOpenFiles(src, action);
463 usleep(1000*250);
464 }
465
466 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700467 SLOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800468 return -1;
469}
470
471int Volume::doUnmount(const char *path, bool force) {
472 int retries = 10;
473
San Mehatd9a4e352010-03-12 13:32:47 -0800474 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700475 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800476 }
477
San Mehat3bb60202010-02-19 18:14:36 -0800478 while (retries--) {
479 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700480 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800481 return 0;
482 }
483
484 int action = 0;
485
486 if (force) {
487 if (retries == 1) {
488 action = 2; // SIGKILL
489 } else if (retries == 2) {
490 action = 1; // SIGHUP
491 }
492 }
493
San Mehat97ac40e2010-03-24 10:24:19 -0700494 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800495 path, strerror(errno), retries, action);
496
497 Process::killProcessesWithOpenFiles(path, action);
498 usleep(1000*1000);
499 }
500 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700501 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800502 return -1;
503}
504
San Mehat4ba89482010-02-18 09:00:18 -0800505int Volume::unmountVol(bool force) {
San Mehata2677e42009-12-13 10:40:18 -0800506 int i, rc;
507
508 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700509 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800510 errno = EINVAL;
511 return -1;
512 }
513
514 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800515 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800516
San Mehat3bb60202010-02-19 18:14:36 -0800517 /*
518 * First move the mountpoint back to our internal staging point
519 * so nobody else can muck with it while we work.
520 */
521 if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700522 SLOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800523 setState(Volume::State_Mounted);
524 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800525 }
526
San Mehatcb4dac82010-03-14 13:41:54 -0700527 protectFromAutorunStupidity();
528
San Mehat3bb60202010-02-19 18:14:36 -0800529 /*
530 * Unmount the tmpfs which was obscuring the asec image directory
531 * from non root users
532 */
533
534 if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700535 SLOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800536 goto fail_republish;
San Mehata2677e42009-12-13 10:40:18 -0800537 }
538
San Mehat3bb60202010-02-19 18:14:36 -0800539 /*
540 * Remove the bindmount we were using to keep a reference to
541 * the previously obscured directory.
542 */
543
544 if (doUnmount(Volume::SEC_ASECDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700545 SLOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800546 goto fail_remount_tmpfs;
547 }
548
549 /*
550 * Finally, unmount the actual block device from the staging dir
551 */
552 if (doUnmount(Volume::SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700553 SLOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800554 goto fail_recreate_bindmount;
555 }
556
San Mehat97ac40e2010-03-24 10:24:19 -0700557 SLOGI("%s unmounted sucessfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800558
559 setState(Volume::State_Idle);
560 mCurrentlyMountedKdev = -1;
561 return 0;
562
563 /*
564 * Failure handling - try to restore everything back the way it was
565 */
566fail_recreate_bindmount:
567 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700568 SLOGE("Failed to restore bindmount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800569 goto out_nomedia;
570 }
571fail_remount_tmpfs:
572 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700573 SLOGE("Failed to restore tmpfs after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800574 goto out_nomedia;
575 }
576fail_republish:
577 if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700578 SLOGE("Failed to republish mount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800579 goto out_nomedia;
580 }
581
San Mehata2677e42009-12-13 10:40:18 -0800582 setState(Volume::State_Mounted);
583 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800584
585out_nomedia:
586 setState(Volume::State_NoMedia);
587 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800588}
San Mehata2677e42009-12-13 10:40:18 -0800589int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800590 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800591
San Mehat2a5b8ce2010-03-10 12:48:57 -0800592 memset(&dinfo, 0, sizeof(dinfo));
593
594 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700595 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800596 return -1;
597 }
598
San Mehat2a5b8ce2010-03-10 12:48:57 -0800599 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
600 dinfo.device = strdup(deviceNode);
601 dinfo.scheme = PART_SCHEME_MBR;
602 dinfo.sect_size = 512;
603 dinfo.skip_lba = 2048;
604 dinfo.num_lba = 0;
605 dinfo.num_parts = 1;
606
607 struct part_info *pinfo = &dinfo.part_lst[0];
608
609 pinfo->name = strdup("android_sdcard");
610 pinfo->flags |= PART_ACTIVE_FLAG;
611 pinfo->type = PC_PART_TYPE_FAT32;
612 pinfo->len_kb = -1;
613
614 int rc = apply_disk_config(&dinfo, 0);
615
616 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700617 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800618 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800619 }
620
San Mehat2a5b8ce2010-03-10 12:48:57 -0800621 out:
622 free(pinfo->name);
623 free(dinfo.device);
624 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800625
San Mehat2a5b8ce2010-03-10 12:48:57 -0800626 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800627}