blob: 416d8c629eca47016d91282b03ab55375b16ebee [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
Mike Lockwooda4886f12010-09-21 13:56:35 -0400222 // Only initialize the MBR if we are formatting the entire device
223 if (formatEntireDevice) {
224 sprintf(devicePath, "/dev/block/vold/%d:%d",
225 MAJOR(diskNode), MINOR(diskNode));
226
227 if (initializeMbr(devicePath)) {
228 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
229 goto err;
230 }
San Mehat49e2bce2009-10-12 16:29:01 -0700231 }
232
San Mehata2677e42009-12-13 10:40:18 -0800233 sprintf(devicePath, "/dev/block/vold/%d:%d",
234 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700235
Mike Lockwooda4886f12010-09-21 13:56:35 -0400236 if (mDebug) {
237 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
238 }
239
San Mehatfcf24fe2010-03-03 12:37:32 -0800240 if (Fat::format(devicePath, 0)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700241 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800242 goto err;
243 }
244
San Mehat2a5b8ce2010-03-10 12:48:57 -0800245 setState(Volume::State_Idle);
San Mehat49e2bce2009-10-12 16:29:01 -0700246 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800247err:
248 return -1;
249}
250
251bool Volume::isMountpointMounted(const char *path) {
252 char device[256];
253 char mount_path[256];
254 char rest[256];
255 FILE *fp;
256 char line[1024];
257
258 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700259 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800260 return false;
261 }
262
263 while(fgets(line, sizeof(line), fp)) {
264 line[strlen(line)-1] = '\0';
265 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
266 if (!strcmp(mount_path, path)) {
267 fclose(fp);
268 return true;
269 }
270
271 }
272
273 fclose(fp);
274 return false;
275}
276
277int Volume::mountVol() {
278 dev_t deviceNodes[4];
279 int n, i, rc = 0;
280 char errmsg[255];
281
282 if (getState() == Volume::State_NoMedia) {
283 snprintf(errmsg, sizeof(errmsg),
284 "Volume %s %s mount failed - no media",
285 getLabel(), getMountpoint());
286 mVm->getBroadcaster()->sendBroadcast(
287 ResponseCode::VolumeMountFailedNoMedia,
288 errmsg, false);
289 errno = ENODEV;
290 return -1;
291 } else if (getState() != Volume::State_Idle) {
292 errno = EBUSY;
293 return -1;
294 }
295
296 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700297 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800298 setState(Volume::State_Mounted);
299 // mCurrentlyMountedKdev = XXX
300 return 0;
301 }
302
303 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
304 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700305 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800306 return -1;
307 }
308
309 for (i = 0; i < n; i++) {
310 char devicePath[255];
311
312 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
313 MINOR(deviceNodes[i]));
314
San Mehat97ac40e2010-03-24 10:24:19 -0700315 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800316
317 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800318 setState(Volume::State_Checking);
319
San Mehat3bb60202010-02-19 18:14:36 -0800320 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800321 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700322 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800323 continue;
San Mehata2677e42009-12-13 10:40:18 -0800324 }
San Mehateba65e92010-01-29 05:15:16 -0800325 errno = EIO;
326 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700327 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800328 setState(Volume::State_Idle);
329 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800330 }
331
San Mehat3bb60202010-02-19 18:14:36 -0800332 /*
333 * Mount the device on our internal staging mountpoint so we can
334 * muck with it before exposing it to non priviledged users.
335 */
San Mehata2677e42009-12-13 10:40:18 -0800336 errno = 0;
Kenny Roota3e06082010-08-27 08:31:35 -0700337 if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false,
338 1000, 1015, 0702, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700339 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800340 continue;
San Mehata2677e42009-12-13 10:40:18 -0800341 }
342
San Mehat97ac40e2010-03-24 10:24:19 -0700343 SLOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800344
San Mehatcb4dac82010-03-14 13:41:54 -0700345 protectFromAutorunStupidity();
346
San Mehat3bb60202010-02-19 18:14:36 -0800347 if (createBindMounts()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700348 SLOGE("Failed to create bindmounts (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800349 umount("/mnt/secure/staging");
350 setState(Volume::State_Idle);
351 return -1;
352 }
353
354 /*
355 * Now that the bindmount trickery is done, atomically move the
356 * whole subtree to expose it to non priviledged users.
357 */
358 if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700359 SLOGE("Failed to move mount (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800360 umount("/mnt/secure/staging");
361 setState(Volume::State_Idle);
362 return -1;
363 }
364 setState(Volume::State_Mounted);
365 mCurrentlyMountedKdev = deviceNodes[i];
366 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800367 }
368
San Mehat97ac40e2010-03-24 10:24:19 -0700369 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800370 setState(Volume::State_Idle);
371
San Mehateba65e92010-01-29 05:15:16 -0800372 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800373}
374
San Mehat3bb60202010-02-19 18:14:36 -0800375int Volume::createBindMounts() {
376 unsigned long flags;
377
378 /*
San Mehat52c2ccb2010-02-23 18:26:13 -0800379 * Rename old /android_secure -> /.android_secure
380 */
381 if (!access("/mnt/secure/staging/android_secure", R_OK | X_OK) &&
382 access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
383 if (rename("/mnt/secure/staging/android_secure", SEC_STG_SECIMGDIR)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700384 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800385 }
386 }
387
388 /*
San Mehat3bb60202010-02-19 18:14:36 -0800389 * Ensure that /android_secure exists and is a directory
390 */
391 if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
392 if (errno == ENOENT) {
393 if (mkdir(SEC_STG_SECIMGDIR, 0777)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700394 SLOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800395 return -1;
396 }
397 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700398 SLOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800399 return -1;
400 }
401 } else {
402 struct stat sbuf;
403
404 if (stat(SEC_STG_SECIMGDIR, &sbuf)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700405 SLOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800406 return -1;
407 }
408 if (!S_ISDIR(sbuf.st_mode)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700409 SLOGE("%s is not a directory", SEC_STG_SECIMGDIR);
San Mehat3bb60202010-02-19 18:14:36 -0800410 errno = ENOTDIR;
411 return -1;
412 }
413 }
414
415 /*
416 * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll
417 * have a root only accessable mountpoint for it.
418 */
419 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700420 SLOGE("Failed to bind mount points %s -> %s (%s)",
San Mehat3bb60202010-02-19 18:14:36 -0800421 SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno));
422 return -1;
423 }
424
425 /*
426 * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to
427 * obscure the underlying directory from everybody - sneaky eh? ;)
428 */
429 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700430 SLOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800431 umount("/mnt/asec_secure");
432 return -1;
433 }
434
435 return 0;
436}
437
438int Volume::doMoveMount(const char *src, const char *dst, bool force) {
439 unsigned int flags = MS_MOVE;
440 int retries = 5;
441
442 while(retries--) {
443 if (!mount(src, dst, "", flags, NULL)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800444 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700445 SLOGD("Moved mount %s -> %s sucessfully", src, dst);
San Mehatd9a4e352010-03-12 13:32:47 -0800446 }
San Mehat3bb60202010-02-19 18:14:36 -0800447 return 0;
448 } else if (errno != EBUSY) {
San Mehat97ac40e2010-03-24 10:24:19 -0700449 SLOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800450 return -1;
451 }
452 int action = 0;
453
454 if (force) {
455 if (retries == 1) {
456 action = 2; // SIGKILL
457 } else if (retries == 2) {
458 action = 1; // SIGHUP
459 }
460 }
San Mehat97ac40e2010-03-24 10:24:19 -0700461 SLOGW("Failed to move %s -> %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800462 src, dst, strerror(errno), retries, action);
463 Process::killProcessesWithOpenFiles(src, action);
464 usleep(1000*250);
465 }
466
467 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700468 SLOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800469 return -1;
470}
471
472int Volume::doUnmount(const char *path, bool force) {
473 int retries = 10;
474
San Mehatd9a4e352010-03-12 13:32:47 -0800475 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700476 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800477 }
478
San Mehat3bb60202010-02-19 18:14:36 -0800479 while (retries--) {
480 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700481 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800482 return 0;
483 }
484
485 int action = 0;
486
487 if (force) {
488 if (retries == 1) {
489 action = 2; // SIGKILL
490 } else if (retries == 2) {
491 action = 1; // SIGHUP
492 }
493 }
494
San Mehat97ac40e2010-03-24 10:24:19 -0700495 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800496 path, strerror(errno), retries, action);
497
498 Process::killProcessesWithOpenFiles(path, action);
499 usleep(1000*1000);
500 }
501 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700502 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800503 return -1;
504}
505
San Mehat4ba89482010-02-18 09:00:18 -0800506int Volume::unmountVol(bool force) {
San Mehata2677e42009-12-13 10:40:18 -0800507 int i, rc;
508
509 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700510 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800511 errno = EINVAL;
512 return -1;
513 }
514
515 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800516 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800517
San Mehat3bb60202010-02-19 18:14:36 -0800518 /*
519 * First move the mountpoint back to our internal staging point
520 * so nobody else can muck with it while we work.
521 */
522 if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700523 SLOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800524 setState(Volume::State_Mounted);
525 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800526 }
527
San Mehatcb4dac82010-03-14 13:41:54 -0700528 protectFromAutorunStupidity();
529
San Mehat3bb60202010-02-19 18:14:36 -0800530 /*
531 * Unmount the tmpfs which was obscuring the asec image directory
532 * from non root users
533 */
534
535 if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700536 SLOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800537 goto fail_republish;
San Mehata2677e42009-12-13 10:40:18 -0800538 }
539
San Mehat3bb60202010-02-19 18:14:36 -0800540 /*
541 * Remove the bindmount we were using to keep a reference to
542 * the previously obscured directory.
543 */
544
545 if (doUnmount(Volume::SEC_ASECDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700546 SLOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800547 goto fail_remount_tmpfs;
548 }
549
550 /*
551 * Finally, unmount the actual block device from the staging dir
552 */
553 if (doUnmount(Volume::SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700554 SLOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800555 goto fail_recreate_bindmount;
556 }
557
San Mehat97ac40e2010-03-24 10:24:19 -0700558 SLOGI("%s unmounted sucessfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800559
560 setState(Volume::State_Idle);
561 mCurrentlyMountedKdev = -1;
562 return 0;
563
564 /*
565 * Failure handling - try to restore everything back the way it was
566 */
567fail_recreate_bindmount:
568 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700569 SLOGE("Failed to restore bindmount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800570 goto out_nomedia;
571 }
572fail_remount_tmpfs:
573 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700574 SLOGE("Failed to restore tmpfs after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800575 goto out_nomedia;
576 }
577fail_republish:
578 if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700579 SLOGE("Failed to republish mount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800580 goto out_nomedia;
581 }
582
San Mehata2677e42009-12-13 10:40:18 -0800583 setState(Volume::State_Mounted);
584 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800585
586out_nomedia:
587 setState(Volume::State_NoMedia);
588 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800589}
San Mehata2677e42009-12-13 10:40:18 -0800590int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800591 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800592
San Mehat2a5b8ce2010-03-10 12:48:57 -0800593 memset(&dinfo, 0, sizeof(dinfo));
594
595 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700596 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800597 return -1;
598 }
599
San Mehat2a5b8ce2010-03-10 12:48:57 -0800600 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
601 dinfo.device = strdup(deviceNode);
602 dinfo.scheme = PART_SCHEME_MBR;
603 dinfo.sect_size = 512;
604 dinfo.skip_lba = 2048;
605 dinfo.num_lba = 0;
606 dinfo.num_parts = 1;
607
608 struct part_info *pinfo = &dinfo.part_lst[0];
609
610 pinfo->name = strdup("android_sdcard");
611 pinfo->flags |= PART_ACTIVE_FLAG;
612 pinfo->type = PC_PART_TYPE_FAT32;
613 pinfo->len_kb = -1;
614
615 int rc = apply_disk_config(&dinfo, 0);
616
617 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700618 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800619 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800620 }
621
San Mehat2a5b8ce2010-03-10 12:48:57 -0800622 out:
623 free(pinfo->name);
624 free(dinfo.device);
625 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800626
San Mehat2a5b8ce2010-03-10 12:48:57 -0800627 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800628}