blob: d67a6c4e5c2802a18d1e844556063219be1269f3 [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>
Olivier Bailly37dcda62010-11-16 10:41:53 -080030#include <linux/fs.h>
San Mehata2677e42009-12-13 10:40:18 -080031
32#include <cutils/properties.h>
33
San Mehat2a5b8ce2010-03-10 12:48:57 -080034#include <diskconfig/diskconfig.h>
San Mehatf1b736b2009-10-10 17:22:08 -070035
36#define LOG_TAG "Vold"
37
38#include <cutils/log.h>
39
40#include "Volume.h"
San Mehata2677e42009-12-13 10:40:18 -080041#include "VolumeManager.h"
42#include "ResponseCode.h"
San Mehatbf041852010-01-04 10:09:16 -080043#include "Fat.h"
San Mehat586536c2010-02-16 17:12:00 -080044#include "Process.h"
San Mehatf1b736b2009-10-10 17:22:08 -070045
San Mehata2677e42009-12-13 10:40:18 -080046extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d);
47extern "C" void dos_partition_enc(void *pp, struct dos_partition *d);
San Mehat49e2bce2009-10-12 16:29:01 -070048
San Mehat3bb60202010-02-19 18:14:36 -080049
50/*
51 * Secure directory - stuff that only root can see
52 */
53const char *Volume::SECDIR = "/mnt/secure";
54
55/*
56 * Secure staging directory - where media is mounted for preparation
57 */
58const char *Volume::SEC_STGDIR = "/mnt/secure/staging";
59
60/*
61 * Path to the directory on the media which contains publicly accessable
62 * asec imagefiles. This path will be obscured before the mount is
63 * exposed to non priviledged users.
64 */
San Mehat52c2ccb2010-02-23 18:26:13 -080065const char *Volume::SEC_STG_SECIMGDIR = "/mnt/secure/staging/.android_secure";
San Mehat3bb60202010-02-19 18:14:36 -080066
67/*
68 * Path to where *only* root can access asec imagefiles
69 */
70const char *Volume::SEC_ASECDIR = "/mnt/secure/asec";
71
72/*
73 * Path to where secure containers are mounted
74 */
75const char *Volume::ASECDIR = "/mnt/asec";
76
Kenny Rootfb7c4d52010-06-30 18:48:41 -070077/*
Kenny Root508c0e12010-07-12 09:59:49 -070078 * Path to where OBBs are mounted
Kenny Rootfb7c4d52010-06-30 18:48:41 -070079 */
Kenny Root508c0e12010-07-12 09:59:49 -070080const char *Volume::LOOPDIR = "/mnt/obb";
Kenny Rootfb7c4d52010-06-30 18:48:41 -070081
San Mehata2677e42009-12-13 10:40:18 -080082static const char *stateToStr(int state) {
83 if (state == Volume::State_Init)
84 return "Initializing";
85 else if (state == Volume::State_NoMedia)
86 return "No-Media";
87 else if (state == Volume::State_Idle)
88 return "Idle-Unmounted";
89 else if (state == Volume::State_Pending)
90 return "Pending";
91 else if (state == Volume::State_Mounted)
92 return "Mounted";
93 else if (state == Volume::State_Unmounting)
94 return "Unmounting";
95 else if (state == Volume::State_Checking)
96 return "Checking";
97 else if (state == Volume::State_Formatting)
98 return "Formatting";
99 else if (state == Volume::State_Shared)
100 return "Shared-Unmounted";
101 else if (state == Volume::State_SharedMnt)
102 return "Shared-Mounted";
103 else
104 return "Unknown-Error";
105}
106
107Volume::Volume(VolumeManager *vm, const char *label, const char *mount_point) {
108 mVm = vm;
San Mehatd9a4e352010-03-12 13:32:47 -0800109 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700110 mLabel = strdup(label);
111 mMountpoint = strdup(mount_point);
112 mState = Volume::State_Init;
San Mehata2677e42009-12-13 10:40:18 -0800113 mCurrentlyMountedKdev = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400114 mPartIdx = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700115}
116
117Volume::~Volume() {
118 free(mLabel);
119 free(mMountpoint);
120}
121
San Mehatcb4dac82010-03-14 13:41:54 -0700122void Volume::protectFromAutorunStupidity() {
123 char filename[255];
124
125 snprintf(filename, sizeof(filename), "%s/autorun.inf", SEC_STGDIR);
126 if (!access(filename, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700127 SLOGW("Volume contains an autorun.inf! - removing");
San Mehatcb4dac82010-03-14 13:41:54 -0700128 /*
129 * Ensure the filename is all lower-case so
130 * the process killer can find the inode.
131 * Probably being paranoid here but meh.
132 */
133 rename(filename, filename);
134 Process::killProcessesWithOpenFiles(filename, 2);
135 if (unlink(filename)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700136 SLOGE("Failed to remove %s (%s)", filename, strerror(errno));
San Mehatcb4dac82010-03-14 13:41:54 -0700137 }
138 }
139}
140
San Mehatd9a4e352010-03-12 13:32:47 -0800141void Volume::setDebug(bool enable) {
142 mDebug = enable;
143}
144
San Mehata2677e42009-12-13 10:40:18 -0800145dev_t Volume::getDiskDevice() {
146 return MKDEV(0, 0);
147};
148
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400149dev_t Volume::getShareDevice() {
150 return getDiskDevice();
151}
152
San Mehata2677e42009-12-13 10:40:18 -0800153void Volume::handleVolumeShared() {
154}
155
156void Volume::handleVolumeUnshared() {
157}
158
San Mehatfd7f5872009-10-12 11:32:47 -0700159int Volume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatf1b736b2009-10-10 17:22:08 -0700160 errno = ENOSYS;
161 return -1;
162}
163
164void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800165 char msg[255];
166 int oldState = mState;
167
168 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700169 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800170 return;
171 }
172
San Mehatf1b736b2009-10-10 17:22:08 -0700173 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800174
San Mehat97ac40e2010-03-24 10:24:19 -0700175 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800176 oldState, stateToStr(oldState), mState, stateToStr(mState));
177 snprintf(msg, sizeof(msg),
178 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
179 getMountpoint(), oldState, stateToStr(oldState), mState,
180 stateToStr(mState));
181
182 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
183 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700184}
San Mehat49e2bce2009-10-12 16:29:01 -0700185
San Mehatdd9b8e92009-10-21 11:06:52 -0700186int Volume::createDeviceNode(const char *path, int major, int minor) {
187 mode_t mode = 0660 | S_IFBLK;
188 dev_t dev = (major << 8) | minor;
189 if (mknod(path, mode, dev) < 0) {
190 if (errno != EEXIST) {
191 return -1;
192 }
193 }
194 return 0;
195}
196
San Mehata2677e42009-12-13 10:40:18 -0800197int Volume::formatVol() {
San Mehat49e2bce2009-10-12 16:29:01 -0700198
San Mehata2677e42009-12-13 10:40:18 -0800199 if (getState() == Volume::State_NoMedia) {
200 errno = ENODEV;
201 return -1;
202 } else if (getState() != Volume::State_Idle) {
203 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700204 return -1;
205 }
206
San Mehata2677e42009-12-13 10:40:18 -0800207 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700208 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800209 setState(Volume::State_Mounted);
210 // mCurrentlyMountedKdev = XXX
211 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700212 return -1;
213 }
214
Mike Lockwooda4886f12010-09-21 13:56:35 -0400215 bool formatEntireDevice = (mPartIdx == -1);
San Mehata2677e42009-12-13 10:40:18 -0800216 char devicePath[255];
217 dev_t diskNode = getDiskDevice();
Mike Lockwooda4886f12010-09-21 13:56:35 -0400218 dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800219
San Mehat2a5b8ce2010-03-10 12:48:57 -0800220 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800221
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800222 int ret = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400223 // Only initialize the MBR if we are formatting the entire device
224 if (formatEntireDevice) {
225 sprintf(devicePath, "/dev/block/vold/%d:%d",
226 MAJOR(diskNode), MINOR(diskNode));
227
228 if (initializeMbr(devicePath)) {
229 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
230 goto err;
231 }
San Mehat49e2bce2009-10-12 16:29:01 -0700232 }
233
San Mehata2677e42009-12-13 10:40:18 -0800234 sprintf(devicePath, "/dev/block/vold/%d:%d",
235 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700236
Mike Lockwooda4886f12010-09-21 13:56:35 -0400237 if (mDebug) {
238 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
239 }
240
San Mehatfcf24fe2010-03-03 12:37:32 -0800241 if (Fat::format(devicePath, 0)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700242 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800243 goto err;
244 }
245
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800246 ret = 0;
247
San Mehata2677e42009-12-13 10:40:18 -0800248err:
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800249 setState(Volume::State_Idle);
250 return ret;
San Mehata2677e42009-12-13 10:40:18 -0800251}
252
253bool Volume::isMountpointMounted(const char *path) {
254 char device[256];
255 char mount_path[256];
256 char rest[256];
257 FILE *fp;
258 char line[1024];
259
260 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700261 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800262 return false;
263 }
264
265 while(fgets(line, sizeof(line), fp)) {
266 line[strlen(line)-1] = '\0';
267 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
268 if (!strcmp(mount_path, path)) {
269 fclose(fp);
270 return true;
271 }
272
273 }
274
275 fclose(fp);
276 return false;
277}
278
279int Volume::mountVol() {
280 dev_t deviceNodes[4];
281 int n, i, rc = 0;
282 char errmsg[255];
283
284 if (getState() == Volume::State_NoMedia) {
285 snprintf(errmsg, sizeof(errmsg),
286 "Volume %s %s mount failed - no media",
287 getLabel(), getMountpoint());
288 mVm->getBroadcaster()->sendBroadcast(
289 ResponseCode::VolumeMountFailedNoMedia,
290 errmsg, false);
291 errno = ENODEV;
292 return -1;
293 } else if (getState() != Volume::State_Idle) {
294 errno = EBUSY;
295 return -1;
296 }
297
298 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700299 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800300 setState(Volume::State_Mounted);
301 // mCurrentlyMountedKdev = XXX
302 return 0;
303 }
304
305 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
306 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700307 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800308 return -1;
309 }
310
311 for (i = 0; i < n; i++) {
312 char devicePath[255];
313
314 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
315 MINOR(deviceNodes[i]));
316
San Mehat97ac40e2010-03-24 10:24:19 -0700317 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800318
319 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800320 setState(Volume::State_Checking);
321
San Mehat3bb60202010-02-19 18:14:36 -0800322 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800323 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700324 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800325 continue;
San Mehata2677e42009-12-13 10:40:18 -0800326 }
San Mehateba65e92010-01-29 05:15:16 -0800327 errno = EIO;
328 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700329 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800330 setState(Volume::State_Idle);
331 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800332 }
333
San Mehat3bb60202010-02-19 18:14:36 -0800334 /*
335 * Mount the device on our internal staging mountpoint so we can
336 * muck with it before exposing it to non priviledged users.
337 */
San Mehata2677e42009-12-13 10:40:18 -0800338 errno = 0;
Kenny Roota3e06082010-08-27 08:31:35 -0700339 if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false,
340 1000, 1015, 0702, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700341 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800342 continue;
San Mehata2677e42009-12-13 10:40:18 -0800343 }
344
San Mehat97ac40e2010-03-24 10:24:19 -0700345 SLOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800346
San Mehatcb4dac82010-03-14 13:41:54 -0700347 protectFromAutorunStupidity();
348
San Mehat3bb60202010-02-19 18:14:36 -0800349 if (createBindMounts()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700350 SLOGE("Failed to create bindmounts (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800351 umount("/mnt/secure/staging");
352 setState(Volume::State_Idle);
353 return -1;
354 }
355
356 /*
357 * Now that the bindmount trickery is done, atomically move the
358 * whole subtree to expose it to non priviledged users.
359 */
360 if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700361 SLOGE("Failed to move mount (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800362 umount("/mnt/secure/staging");
363 setState(Volume::State_Idle);
364 return -1;
365 }
366 setState(Volume::State_Mounted);
367 mCurrentlyMountedKdev = deviceNodes[i];
368 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800369 }
370
San Mehat97ac40e2010-03-24 10:24:19 -0700371 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800372 setState(Volume::State_Idle);
373
San Mehateba65e92010-01-29 05:15:16 -0800374 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800375}
376
San Mehat3bb60202010-02-19 18:14:36 -0800377int Volume::createBindMounts() {
378 unsigned long flags;
379
380 /*
San Mehat52c2ccb2010-02-23 18:26:13 -0800381 * Rename old /android_secure -> /.android_secure
382 */
383 if (!access("/mnt/secure/staging/android_secure", R_OK | X_OK) &&
384 access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
385 if (rename("/mnt/secure/staging/android_secure", SEC_STG_SECIMGDIR)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700386 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800387 }
388 }
389
390 /*
San Mehat3bb60202010-02-19 18:14:36 -0800391 * Ensure that /android_secure exists and is a directory
392 */
393 if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
394 if (errno == ENOENT) {
395 if (mkdir(SEC_STG_SECIMGDIR, 0777)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700396 SLOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800397 return -1;
398 }
399 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700400 SLOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800401 return -1;
402 }
403 } else {
404 struct stat sbuf;
405
406 if (stat(SEC_STG_SECIMGDIR, &sbuf)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700407 SLOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800408 return -1;
409 }
410 if (!S_ISDIR(sbuf.st_mode)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700411 SLOGE("%s is not a directory", SEC_STG_SECIMGDIR);
San Mehat3bb60202010-02-19 18:14:36 -0800412 errno = ENOTDIR;
413 return -1;
414 }
415 }
416
417 /*
418 * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll
419 * have a root only accessable mountpoint for it.
420 */
421 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700422 SLOGE("Failed to bind mount points %s -> %s (%s)",
San Mehat3bb60202010-02-19 18:14:36 -0800423 SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno));
424 return -1;
425 }
426
427 /*
428 * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to
429 * obscure the underlying directory from everybody - sneaky eh? ;)
430 */
431 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700432 SLOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800433 umount("/mnt/asec_secure");
434 return -1;
435 }
436
437 return 0;
438}
439
440int Volume::doMoveMount(const char *src, const char *dst, bool force) {
441 unsigned int flags = MS_MOVE;
442 int retries = 5;
443
444 while(retries--) {
445 if (!mount(src, dst, "", flags, NULL)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800446 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700447 SLOGD("Moved mount %s -> %s sucessfully", src, dst);
San Mehatd9a4e352010-03-12 13:32:47 -0800448 }
San Mehat3bb60202010-02-19 18:14:36 -0800449 return 0;
450 } else if (errno != EBUSY) {
San Mehat97ac40e2010-03-24 10:24:19 -0700451 SLOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800452 return -1;
453 }
454 int action = 0;
455
456 if (force) {
457 if (retries == 1) {
458 action = 2; // SIGKILL
459 } else if (retries == 2) {
460 action = 1; // SIGHUP
461 }
462 }
San Mehat97ac40e2010-03-24 10:24:19 -0700463 SLOGW("Failed to move %s -> %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800464 src, dst, strerror(errno), retries, action);
465 Process::killProcessesWithOpenFiles(src, action);
466 usleep(1000*250);
467 }
468
469 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700470 SLOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800471 return -1;
472}
473
474int Volume::doUnmount(const char *path, bool force) {
475 int retries = 10;
476
San Mehatd9a4e352010-03-12 13:32:47 -0800477 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700478 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800479 }
480
San Mehat3bb60202010-02-19 18:14:36 -0800481 while (retries--) {
482 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700483 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800484 return 0;
485 }
486
487 int action = 0;
488
489 if (force) {
490 if (retries == 1) {
491 action = 2; // SIGKILL
492 } else if (retries == 2) {
493 action = 1; // SIGHUP
494 }
495 }
496
San Mehat97ac40e2010-03-24 10:24:19 -0700497 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800498 path, strerror(errno), retries, action);
499
500 Process::killProcessesWithOpenFiles(path, action);
501 usleep(1000*1000);
502 }
503 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700504 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800505 return -1;
506}
507
San Mehat4ba89482010-02-18 09:00:18 -0800508int Volume::unmountVol(bool force) {
San Mehata2677e42009-12-13 10:40:18 -0800509 int i, rc;
510
511 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700512 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800513 errno = EINVAL;
514 return -1;
515 }
516
517 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800518 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800519
San Mehat3bb60202010-02-19 18:14:36 -0800520 /*
521 * First move the mountpoint back to our internal staging point
522 * so nobody else can muck with it while we work.
523 */
524 if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700525 SLOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800526 setState(Volume::State_Mounted);
527 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800528 }
529
San Mehatcb4dac82010-03-14 13:41:54 -0700530 protectFromAutorunStupidity();
531
San Mehat3bb60202010-02-19 18:14:36 -0800532 /*
533 * Unmount the tmpfs which was obscuring the asec image directory
534 * from non root users
535 */
536
537 if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700538 SLOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800539 goto fail_republish;
San Mehata2677e42009-12-13 10:40:18 -0800540 }
541
San Mehat3bb60202010-02-19 18:14:36 -0800542 /*
543 * Remove the bindmount we were using to keep a reference to
544 * the previously obscured directory.
545 */
546
547 if (doUnmount(Volume::SEC_ASECDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700548 SLOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800549 goto fail_remount_tmpfs;
550 }
551
552 /*
553 * Finally, unmount the actual block device from the staging dir
554 */
555 if (doUnmount(Volume::SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700556 SLOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800557 goto fail_recreate_bindmount;
558 }
559
San Mehat97ac40e2010-03-24 10:24:19 -0700560 SLOGI("%s unmounted sucessfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800561
562 setState(Volume::State_Idle);
563 mCurrentlyMountedKdev = -1;
564 return 0;
565
566 /*
567 * Failure handling - try to restore everything back the way it was
568 */
569fail_recreate_bindmount:
570 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700571 SLOGE("Failed to restore bindmount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800572 goto out_nomedia;
573 }
574fail_remount_tmpfs:
575 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700576 SLOGE("Failed to restore tmpfs after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800577 goto out_nomedia;
578 }
579fail_republish:
580 if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700581 SLOGE("Failed to republish mount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800582 goto out_nomedia;
583 }
584
San Mehata2677e42009-12-13 10:40:18 -0800585 setState(Volume::State_Mounted);
586 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800587
588out_nomedia:
589 setState(Volume::State_NoMedia);
590 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800591}
San Mehata2677e42009-12-13 10:40:18 -0800592int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800593 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800594
San Mehat2a5b8ce2010-03-10 12:48:57 -0800595 memset(&dinfo, 0, sizeof(dinfo));
596
597 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700598 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800599 return -1;
600 }
601
San Mehat2a5b8ce2010-03-10 12:48:57 -0800602 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
603 dinfo.device = strdup(deviceNode);
604 dinfo.scheme = PART_SCHEME_MBR;
605 dinfo.sect_size = 512;
606 dinfo.skip_lba = 2048;
607 dinfo.num_lba = 0;
608 dinfo.num_parts = 1;
609
610 struct part_info *pinfo = &dinfo.part_lst[0];
611
612 pinfo->name = strdup("android_sdcard");
613 pinfo->flags |= PART_ACTIVE_FLAG;
614 pinfo->type = PC_PART_TYPE_FAT32;
615 pinfo->len_kb = -1;
616
617 int rc = apply_disk_config(&dinfo, 0);
618
619 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700620 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800621 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800622 }
623
San Mehat2a5b8ce2010-03-10 12:48:57 -0800624 out:
625 free(pinfo->name);
626 free(dinfo.device);
627 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800628
San Mehat2a5b8ce2010-03-10 12:48:57 -0800629 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800630}