blob: 190b64b29b4070a56658f58eef11fd813375cefa [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;
San Mehatf1b736b2009-10-10 17:22:08 -0700113}
114
115Volume::~Volume() {
116 free(mLabel);
117 free(mMountpoint);
118}
119
San Mehatcb4dac82010-03-14 13:41:54 -0700120void Volume::protectFromAutorunStupidity() {
121 char filename[255];
122
123 snprintf(filename, sizeof(filename), "%s/autorun.inf", SEC_STGDIR);
124 if (!access(filename, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700125 SLOGW("Volume contains an autorun.inf! - removing");
San Mehatcb4dac82010-03-14 13:41:54 -0700126 /*
127 * Ensure the filename is all lower-case so
128 * the process killer can find the inode.
129 * Probably being paranoid here but meh.
130 */
131 rename(filename, filename);
132 Process::killProcessesWithOpenFiles(filename, 2);
133 if (unlink(filename)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700134 SLOGE("Failed to remove %s (%s)", filename, strerror(errno));
San Mehatcb4dac82010-03-14 13:41:54 -0700135 }
136 }
137}
138
San Mehatd9a4e352010-03-12 13:32:47 -0800139void Volume::setDebug(bool enable) {
140 mDebug = enable;
141}
142
San Mehata2677e42009-12-13 10:40:18 -0800143dev_t Volume::getDiskDevice() {
144 return MKDEV(0, 0);
145};
146
147void Volume::handleVolumeShared() {
148}
149
150void Volume::handleVolumeUnshared() {
151}
152
San Mehatfd7f5872009-10-12 11:32:47 -0700153int Volume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatf1b736b2009-10-10 17:22:08 -0700154 errno = ENOSYS;
155 return -1;
156}
157
158void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800159 char msg[255];
160 int oldState = mState;
161
162 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700163 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800164 return;
165 }
166
San Mehatf1b736b2009-10-10 17:22:08 -0700167 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800168
San Mehat97ac40e2010-03-24 10:24:19 -0700169 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800170 oldState, stateToStr(oldState), mState, stateToStr(mState));
171 snprintf(msg, sizeof(msg),
172 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
173 getMountpoint(), oldState, stateToStr(oldState), mState,
174 stateToStr(mState));
175
176 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
177 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700178}
San Mehat49e2bce2009-10-12 16:29:01 -0700179
San Mehatdd9b8e92009-10-21 11:06:52 -0700180int Volume::createDeviceNode(const char *path, int major, int minor) {
181 mode_t mode = 0660 | S_IFBLK;
182 dev_t dev = (major << 8) | minor;
183 if (mknod(path, mode, dev) < 0) {
184 if (errno != EEXIST) {
185 return -1;
186 }
187 }
188 return 0;
189}
190
San Mehata2677e42009-12-13 10:40:18 -0800191int Volume::formatVol() {
San Mehat49e2bce2009-10-12 16:29:01 -0700192
San Mehata2677e42009-12-13 10:40:18 -0800193 if (getState() == Volume::State_NoMedia) {
194 errno = ENODEV;
195 return -1;
196 } else if (getState() != Volume::State_Idle) {
197 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700198 return -1;
199 }
200
San Mehata2677e42009-12-13 10:40:18 -0800201 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700202 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800203 setState(Volume::State_Mounted);
204 // mCurrentlyMountedKdev = XXX
205 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700206 return -1;
207 }
208
San Mehata2677e42009-12-13 10:40:18 -0800209 char devicePath[255];
210 dev_t diskNode = getDiskDevice();
211 dev_t partNode = MKDEV(MAJOR(diskNode), 1); // XXX: Hmmm
212
213 sprintf(devicePath, "/dev/block/vold/%d:%d",
214 MAJOR(diskNode), MINOR(diskNode));
215
San Mehatd9a4e352010-03-12 13:32:47 -0800216 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700217 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
San Mehatd9a4e352010-03-12 13:32:47 -0800218 }
San Mehat2a5b8ce2010-03-10 12:48:57 -0800219 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800220
221 if (initializeMbr(devicePath)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700222 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800223 goto err;
San Mehat49e2bce2009-10-12 16:29:01 -0700224 }
225
San Mehata2677e42009-12-13 10:40:18 -0800226 sprintf(devicePath, "/dev/block/vold/%d:%d",
227 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700228
San Mehatfcf24fe2010-03-03 12:37:32 -0800229 if (Fat::format(devicePath, 0)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700230 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800231 goto err;
232 }
233
San Mehat2a5b8ce2010-03-10 12:48:57 -0800234 setState(Volume::State_Idle);
San Mehat49e2bce2009-10-12 16:29:01 -0700235 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800236err:
237 return -1;
238}
239
240bool Volume::isMountpointMounted(const char *path) {
241 char device[256];
242 char mount_path[256];
243 char rest[256];
244 FILE *fp;
245 char line[1024];
246
247 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700248 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800249 return false;
250 }
251
252 while(fgets(line, sizeof(line), fp)) {
253 line[strlen(line)-1] = '\0';
254 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
255 if (!strcmp(mount_path, path)) {
256 fclose(fp);
257 return true;
258 }
259
260 }
261
262 fclose(fp);
263 return false;
264}
265
266int Volume::mountVol() {
267 dev_t deviceNodes[4];
268 int n, i, rc = 0;
269 char errmsg[255];
270
271 if (getState() == Volume::State_NoMedia) {
272 snprintf(errmsg, sizeof(errmsg),
273 "Volume %s %s mount failed - no media",
274 getLabel(), getMountpoint());
275 mVm->getBroadcaster()->sendBroadcast(
276 ResponseCode::VolumeMountFailedNoMedia,
277 errmsg, false);
278 errno = ENODEV;
279 return -1;
280 } else if (getState() != Volume::State_Idle) {
281 errno = EBUSY;
282 return -1;
283 }
284
285 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700286 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800287 setState(Volume::State_Mounted);
288 // mCurrentlyMountedKdev = XXX
289 return 0;
290 }
291
292 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
293 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700294 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800295 return -1;
296 }
297
298 for (i = 0; i < n; i++) {
299 char devicePath[255];
300
301 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
302 MINOR(deviceNodes[i]));
303
San Mehat97ac40e2010-03-24 10:24:19 -0700304 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800305
306 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800307 setState(Volume::State_Checking);
308
San Mehat3bb60202010-02-19 18:14:36 -0800309 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800310 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700311 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800312 continue;
San Mehata2677e42009-12-13 10:40:18 -0800313 }
San Mehateba65e92010-01-29 05:15:16 -0800314 errno = EIO;
315 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700316 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800317 setState(Volume::State_Idle);
318 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800319 }
320
San Mehat3bb60202010-02-19 18:14:36 -0800321 /*
322 * Mount the device on our internal staging mountpoint so we can
323 * muck with it before exposing it to non priviledged users.
324 */
San Mehata2677e42009-12-13 10:40:18 -0800325 errno = 0;
Kenny Roota3e06082010-08-27 08:31:35 -0700326 if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false,
327 1000, 1015, 0702, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700328 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800329 continue;
San Mehata2677e42009-12-13 10:40:18 -0800330 }
331
San Mehat97ac40e2010-03-24 10:24:19 -0700332 SLOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800333
San Mehatcb4dac82010-03-14 13:41:54 -0700334 protectFromAutorunStupidity();
335
San Mehat3bb60202010-02-19 18:14:36 -0800336 if (createBindMounts()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700337 SLOGE("Failed to create bindmounts (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800338 umount("/mnt/secure/staging");
339 setState(Volume::State_Idle);
340 return -1;
341 }
342
343 /*
344 * Now that the bindmount trickery is done, atomically move the
345 * whole subtree to expose it to non priviledged users.
346 */
347 if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700348 SLOGE("Failed to move mount (%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 setState(Volume::State_Mounted);
354 mCurrentlyMountedKdev = deviceNodes[i];
355 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800356 }
357
San Mehat97ac40e2010-03-24 10:24:19 -0700358 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800359 setState(Volume::State_Idle);
360
San Mehateba65e92010-01-29 05:15:16 -0800361 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800362}
363
San Mehat3bb60202010-02-19 18:14:36 -0800364int Volume::createBindMounts() {
365 unsigned long flags;
366
367 /*
San Mehat52c2ccb2010-02-23 18:26:13 -0800368 * Rename old /android_secure -> /.android_secure
369 */
370 if (!access("/mnt/secure/staging/android_secure", R_OK | X_OK) &&
371 access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
372 if (rename("/mnt/secure/staging/android_secure", SEC_STG_SECIMGDIR)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700373 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800374 }
375 }
376
377 /*
San Mehat3bb60202010-02-19 18:14:36 -0800378 * Ensure that /android_secure exists and is a directory
379 */
380 if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
381 if (errno == ENOENT) {
382 if (mkdir(SEC_STG_SECIMGDIR, 0777)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700383 SLOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800384 return -1;
385 }
386 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700387 SLOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800388 return -1;
389 }
390 } else {
391 struct stat sbuf;
392
393 if (stat(SEC_STG_SECIMGDIR, &sbuf)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700394 SLOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800395 return -1;
396 }
397 if (!S_ISDIR(sbuf.st_mode)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700398 SLOGE("%s is not a directory", SEC_STG_SECIMGDIR);
San Mehat3bb60202010-02-19 18:14:36 -0800399 errno = ENOTDIR;
400 return -1;
401 }
402 }
403
404 /*
405 * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll
406 * have a root only accessable mountpoint for it.
407 */
408 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700409 SLOGE("Failed to bind mount points %s -> %s (%s)",
San Mehat3bb60202010-02-19 18:14:36 -0800410 SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno));
411 return -1;
412 }
413
414 /*
415 * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to
416 * obscure the underlying directory from everybody - sneaky eh? ;)
417 */
418 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700419 SLOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800420 umount("/mnt/asec_secure");
421 return -1;
422 }
423
424 return 0;
425}
426
427int Volume::doMoveMount(const char *src, const char *dst, bool force) {
428 unsigned int flags = MS_MOVE;
429 int retries = 5;
430
431 while(retries--) {
432 if (!mount(src, dst, "", flags, NULL)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800433 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700434 SLOGD("Moved mount %s -> %s sucessfully", src, dst);
San Mehatd9a4e352010-03-12 13:32:47 -0800435 }
San Mehat3bb60202010-02-19 18:14:36 -0800436 return 0;
437 } else if (errno != EBUSY) {
San Mehat97ac40e2010-03-24 10:24:19 -0700438 SLOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800439 return -1;
440 }
441 int action = 0;
442
443 if (force) {
444 if (retries == 1) {
445 action = 2; // SIGKILL
446 } else if (retries == 2) {
447 action = 1; // SIGHUP
448 }
449 }
San Mehat97ac40e2010-03-24 10:24:19 -0700450 SLOGW("Failed to move %s -> %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800451 src, dst, strerror(errno), retries, action);
452 Process::killProcessesWithOpenFiles(src, action);
453 usleep(1000*250);
454 }
455
456 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700457 SLOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800458 return -1;
459}
460
461int Volume::doUnmount(const char *path, bool force) {
462 int retries = 10;
463
San Mehatd9a4e352010-03-12 13:32:47 -0800464 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700465 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800466 }
467
San Mehat3bb60202010-02-19 18:14:36 -0800468 while (retries--) {
469 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700470 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800471 return 0;
472 }
473
474 int action = 0;
475
476 if (force) {
477 if (retries == 1) {
478 action = 2; // SIGKILL
479 } else if (retries == 2) {
480 action = 1; // SIGHUP
481 }
482 }
483
San Mehat97ac40e2010-03-24 10:24:19 -0700484 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800485 path, strerror(errno), retries, action);
486
487 Process::killProcessesWithOpenFiles(path, action);
488 usleep(1000*1000);
489 }
490 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700491 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800492 return -1;
493}
494
San Mehat4ba89482010-02-18 09:00:18 -0800495int Volume::unmountVol(bool force) {
San Mehata2677e42009-12-13 10:40:18 -0800496 int i, rc;
497
498 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700499 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800500 errno = EINVAL;
501 return -1;
502 }
503
504 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800505 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800506
San Mehat3bb60202010-02-19 18:14:36 -0800507 /*
508 * First move the mountpoint back to our internal staging point
509 * so nobody else can muck with it while we work.
510 */
511 if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700512 SLOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800513 setState(Volume::State_Mounted);
514 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800515 }
516
San Mehatcb4dac82010-03-14 13:41:54 -0700517 protectFromAutorunStupidity();
518
San Mehat3bb60202010-02-19 18:14:36 -0800519 /*
520 * Unmount the tmpfs which was obscuring the asec image directory
521 * from non root users
522 */
523
524 if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700525 SLOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800526 goto fail_republish;
San Mehata2677e42009-12-13 10:40:18 -0800527 }
528
San Mehat3bb60202010-02-19 18:14:36 -0800529 /*
530 * Remove the bindmount we were using to keep a reference to
531 * the previously obscured directory.
532 */
533
534 if (doUnmount(Volume::SEC_ASECDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700535 SLOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800536 goto fail_remount_tmpfs;
537 }
538
539 /*
540 * Finally, unmount the actual block device from the staging dir
541 */
542 if (doUnmount(Volume::SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700543 SLOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800544 goto fail_recreate_bindmount;
545 }
546
San Mehat97ac40e2010-03-24 10:24:19 -0700547 SLOGI("%s unmounted sucessfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800548
549 setState(Volume::State_Idle);
550 mCurrentlyMountedKdev = -1;
551 return 0;
552
553 /*
554 * Failure handling - try to restore everything back the way it was
555 */
556fail_recreate_bindmount:
557 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700558 SLOGE("Failed to restore bindmount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800559 goto out_nomedia;
560 }
561fail_remount_tmpfs:
562 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700563 SLOGE("Failed to restore tmpfs after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800564 goto out_nomedia;
565 }
566fail_republish:
567 if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700568 SLOGE("Failed to republish mount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800569 goto out_nomedia;
570 }
571
San Mehata2677e42009-12-13 10:40:18 -0800572 setState(Volume::State_Mounted);
573 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800574
575out_nomedia:
576 setState(Volume::State_NoMedia);
577 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800578}
579
580int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800581 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800582
San Mehat2a5b8ce2010-03-10 12:48:57 -0800583 memset(&dinfo, 0, sizeof(dinfo));
584
585 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700586 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800587 return -1;
588 }
589
San Mehat2a5b8ce2010-03-10 12:48:57 -0800590 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
591 dinfo.device = strdup(deviceNode);
592 dinfo.scheme = PART_SCHEME_MBR;
593 dinfo.sect_size = 512;
594 dinfo.skip_lba = 2048;
595 dinfo.num_lba = 0;
596 dinfo.num_parts = 1;
597
598 struct part_info *pinfo = &dinfo.part_lst[0];
599
600 pinfo->name = strdup("android_sdcard");
601 pinfo->flags |= PART_ACTIVE_FLAG;
602 pinfo->type = PC_PART_TYPE_FAT32;
603 pinfo->len_kb = -1;
604
605 int rc = apply_disk_config(&dinfo, 0);
606
607 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700608 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800609 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800610 }
611
San Mehat2a5b8ce2010-03-10 12:48:57 -0800612 out:
613 free(pinfo->name);
614 free(dinfo.device);
615 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800616
San Mehat2a5b8ce2010-03-10 12:48:57 -0800617 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800618}