blob: 746d84fd7b45f5896d807a56c2fe5301aa4b2bfd [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
Mike Lockwood9092b1d2011-03-23 14:55:49 -040036#include <private/android_filesystem_config.h>
37
San Mehatf1b736b2009-10-10 17:22:08 -070038#define LOG_TAG "Vold"
39
40#include <cutils/log.h>
41
42#include "Volume.h"
San Mehata2677e42009-12-13 10:40:18 -080043#include "VolumeManager.h"
44#include "ResponseCode.h"
San Mehatbf041852010-01-04 10:09:16 -080045#include "Fat.h"
San Mehat586536c2010-02-16 17:12:00 -080046#include "Process.h"
San Mehatf1b736b2009-10-10 17:22:08 -070047
San Mehata2677e42009-12-13 10:40:18 -080048extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d);
49extern "C" void dos_partition_enc(void *pp, struct dos_partition *d);
San Mehat49e2bce2009-10-12 16:29:01 -070050
San Mehat3bb60202010-02-19 18:14:36 -080051
52/*
53 * Secure directory - stuff that only root can see
54 */
55const char *Volume::SECDIR = "/mnt/secure";
56
57/*
58 * Secure staging directory - where media is mounted for preparation
59 */
60const char *Volume::SEC_STGDIR = "/mnt/secure/staging";
61
62/*
63 * Path to the directory on the media which contains publicly accessable
64 * asec imagefiles. This path will be obscured before the mount is
65 * exposed to non priviledged users.
66 */
San Mehat52c2ccb2010-02-23 18:26:13 -080067const char *Volume::SEC_STG_SECIMGDIR = "/mnt/secure/staging/.android_secure";
San Mehat3bb60202010-02-19 18:14:36 -080068
69/*
70 * Path to where *only* root can access asec imagefiles
71 */
72const char *Volume::SEC_ASECDIR = "/mnt/secure/asec";
73
74/*
75 * Path to where secure containers are mounted
76 */
77const char *Volume::ASECDIR = "/mnt/asec";
78
Kenny Rootfb7c4d52010-06-30 18:48:41 -070079/*
Kenny Root508c0e12010-07-12 09:59:49 -070080 * Path to where OBBs are mounted
Kenny Rootfb7c4d52010-06-30 18:48:41 -070081 */
Kenny Root508c0e12010-07-12 09:59:49 -070082const char *Volume::LOOPDIR = "/mnt/obb";
Kenny Rootfb7c4d52010-06-30 18:48:41 -070083
San Mehata2677e42009-12-13 10:40:18 -080084static const char *stateToStr(int state) {
85 if (state == Volume::State_Init)
86 return "Initializing";
87 else if (state == Volume::State_NoMedia)
88 return "No-Media";
89 else if (state == Volume::State_Idle)
90 return "Idle-Unmounted";
91 else if (state == Volume::State_Pending)
92 return "Pending";
93 else if (state == Volume::State_Mounted)
94 return "Mounted";
95 else if (state == Volume::State_Unmounting)
96 return "Unmounting";
97 else if (state == Volume::State_Checking)
98 return "Checking";
99 else if (state == Volume::State_Formatting)
100 return "Formatting";
101 else if (state == Volume::State_Shared)
102 return "Shared-Unmounted";
103 else if (state == Volume::State_SharedMnt)
104 return "Shared-Mounted";
105 else
106 return "Unknown-Error";
107}
108
109Volume::Volume(VolumeManager *vm, const char *label, const char *mount_point) {
110 mVm = vm;
San Mehatd9a4e352010-03-12 13:32:47 -0800111 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700112 mLabel = strdup(label);
113 mMountpoint = strdup(mount_point);
114 mState = Volume::State_Init;
San Mehata2677e42009-12-13 10:40:18 -0800115 mCurrentlyMountedKdev = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400116 mPartIdx = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700117}
118
119Volume::~Volume() {
120 free(mLabel);
121 free(mMountpoint);
122}
123
San Mehatcb4dac82010-03-14 13:41:54 -0700124void Volume::protectFromAutorunStupidity() {
125 char filename[255];
126
127 snprintf(filename, sizeof(filename), "%s/autorun.inf", SEC_STGDIR);
128 if (!access(filename, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700129 SLOGW("Volume contains an autorun.inf! - removing");
San Mehatcb4dac82010-03-14 13:41:54 -0700130 /*
131 * Ensure the filename is all lower-case so
132 * the process killer can find the inode.
133 * Probably being paranoid here but meh.
134 */
135 rename(filename, filename);
136 Process::killProcessesWithOpenFiles(filename, 2);
137 if (unlink(filename)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700138 SLOGE("Failed to remove %s (%s)", filename, strerror(errno));
San Mehatcb4dac82010-03-14 13:41:54 -0700139 }
140 }
141}
142
San Mehatd9a4e352010-03-12 13:32:47 -0800143void Volume::setDebug(bool enable) {
144 mDebug = enable;
145}
146
San Mehata2677e42009-12-13 10:40:18 -0800147dev_t Volume::getDiskDevice() {
148 return MKDEV(0, 0);
149};
150
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400151dev_t Volume::getShareDevice() {
152 return getDiskDevice();
153}
154
San Mehata2677e42009-12-13 10:40:18 -0800155void Volume::handleVolumeShared() {
156}
157
158void Volume::handleVolumeUnshared() {
159}
160
San Mehatfd7f5872009-10-12 11:32:47 -0700161int Volume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatf1b736b2009-10-10 17:22:08 -0700162 errno = ENOSYS;
163 return -1;
164}
165
166void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800167 char msg[255];
168 int oldState = mState;
169
170 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700171 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800172 return;
173 }
174
San Mehatf1b736b2009-10-10 17:22:08 -0700175 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800176
San Mehat97ac40e2010-03-24 10:24:19 -0700177 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800178 oldState, stateToStr(oldState), mState, stateToStr(mState));
179 snprintf(msg, sizeof(msg),
180 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
181 getMountpoint(), oldState, stateToStr(oldState), mState,
182 stateToStr(mState));
183
184 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
185 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700186}
San Mehat49e2bce2009-10-12 16:29:01 -0700187
San Mehatdd9b8e92009-10-21 11:06:52 -0700188int Volume::createDeviceNode(const char *path, int major, int minor) {
189 mode_t mode = 0660 | S_IFBLK;
190 dev_t dev = (major << 8) | minor;
191 if (mknod(path, mode, dev) < 0) {
192 if (errno != EEXIST) {
193 return -1;
194 }
195 }
196 return 0;
197}
198
San Mehata2677e42009-12-13 10:40:18 -0800199int Volume::formatVol() {
San Mehat49e2bce2009-10-12 16:29:01 -0700200
San Mehata2677e42009-12-13 10:40:18 -0800201 if (getState() == Volume::State_NoMedia) {
202 errno = ENODEV;
203 return -1;
204 } else if (getState() != Volume::State_Idle) {
205 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700206 return -1;
207 }
208
San Mehata2677e42009-12-13 10:40:18 -0800209 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700210 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800211 setState(Volume::State_Mounted);
212 // mCurrentlyMountedKdev = XXX
213 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700214 return -1;
215 }
216
Mike Lockwooda4886f12010-09-21 13:56:35 -0400217 bool formatEntireDevice = (mPartIdx == -1);
San Mehata2677e42009-12-13 10:40:18 -0800218 char devicePath[255];
219 dev_t diskNode = getDiskDevice();
Mike Lockwooda4886f12010-09-21 13:56:35 -0400220 dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800221
San Mehat2a5b8ce2010-03-10 12:48:57 -0800222 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800223
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800224 int ret = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400225 // Only initialize the MBR if we are formatting the entire device
226 if (formatEntireDevice) {
227 sprintf(devicePath, "/dev/block/vold/%d:%d",
228 MAJOR(diskNode), MINOR(diskNode));
229
230 if (initializeMbr(devicePath)) {
231 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
232 goto err;
233 }
San Mehat49e2bce2009-10-12 16:29:01 -0700234 }
235
San Mehata2677e42009-12-13 10:40:18 -0800236 sprintf(devicePath, "/dev/block/vold/%d:%d",
237 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700238
Mike Lockwooda4886f12010-09-21 13:56:35 -0400239 if (mDebug) {
240 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
241 }
242
San Mehatfcf24fe2010-03-03 12:37:32 -0800243 if (Fat::format(devicePath, 0)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700244 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800245 goto err;
246 }
247
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800248 ret = 0;
249
San Mehata2677e42009-12-13 10:40:18 -0800250err:
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800251 setState(Volume::State_Idle);
252 return ret;
San Mehata2677e42009-12-13 10:40:18 -0800253}
254
255bool Volume::isMountpointMounted(const char *path) {
256 char device[256];
257 char mount_path[256];
258 char rest[256];
259 FILE *fp;
260 char line[1024];
261
262 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700263 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800264 return false;
265 }
266
267 while(fgets(line, sizeof(line), fp)) {
268 line[strlen(line)-1] = '\0';
269 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
270 if (!strcmp(mount_path, path)) {
271 fclose(fp);
272 return true;
273 }
274
275 }
276
277 fclose(fp);
278 return false;
279}
280
281int Volume::mountVol() {
282 dev_t deviceNodes[4];
283 int n, i, rc = 0;
284 char errmsg[255];
285
286 if (getState() == Volume::State_NoMedia) {
287 snprintf(errmsg, sizeof(errmsg),
288 "Volume %s %s mount failed - no media",
289 getLabel(), getMountpoint());
290 mVm->getBroadcaster()->sendBroadcast(
291 ResponseCode::VolumeMountFailedNoMedia,
292 errmsg, false);
293 errno = ENODEV;
294 return -1;
295 } else if (getState() != Volume::State_Idle) {
296 errno = EBUSY;
297 return -1;
298 }
299
300 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700301 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800302 setState(Volume::State_Mounted);
303 // mCurrentlyMountedKdev = XXX
304 return 0;
305 }
306
307 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
308 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700309 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800310 return -1;
311 }
312
313 for (i = 0; i < n; i++) {
314 char devicePath[255];
315
316 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
317 MINOR(deviceNodes[i]));
318
San Mehat97ac40e2010-03-24 10:24:19 -0700319 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800320
321 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800322 setState(Volume::State_Checking);
323
San Mehat3bb60202010-02-19 18:14:36 -0800324 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800325 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700326 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800327 continue;
San Mehata2677e42009-12-13 10:40:18 -0800328 }
San Mehateba65e92010-01-29 05:15:16 -0800329 errno = EIO;
330 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700331 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800332 setState(Volume::State_Idle);
333 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800334 }
335
San Mehat3bb60202010-02-19 18:14:36 -0800336 /*
337 * Mount the device on our internal staging mountpoint so we can
338 * muck with it before exposing it to non priviledged users.
339 */
San Mehata2677e42009-12-13 10:40:18 -0800340 errno = 0;
Mike Lockwood9092b1d2011-03-23 14:55:49 -0400341 int gid;
342
343 if (!strcmp(getMountpoint(), "/mnt/sdcard")) {
344 // Special case the primary SD card.
345 // For this we grant write access to the SDCARD_RW group.
346 gid = AID_SDCARD_RW;
347 } else {
348 // For secondary external storage we keep things locked up.
349 gid = AID_MEDIA_RW;
350 }
Kenny Roota3e06082010-08-27 08:31:35 -0700351 if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false,
Mike Lockwood9092b1d2011-03-23 14:55:49 -0400352 AID_SYSTEM, gid, 0702, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700353 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800354 continue;
San Mehata2677e42009-12-13 10:40:18 -0800355 }
356
San Mehat97ac40e2010-03-24 10:24:19 -0700357 SLOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800358
San Mehatcb4dac82010-03-14 13:41:54 -0700359 protectFromAutorunStupidity();
360
San Mehat3bb60202010-02-19 18:14:36 -0800361 if (createBindMounts()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700362 SLOGE("Failed to create bindmounts (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800363 umount("/mnt/secure/staging");
364 setState(Volume::State_Idle);
365 return -1;
366 }
367
368 /*
369 * Now that the bindmount trickery is done, atomically move the
370 * whole subtree to expose it to non priviledged users.
371 */
372 if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700373 SLOGE("Failed to move mount (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800374 umount("/mnt/secure/staging");
375 setState(Volume::State_Idle);
376 return -1;
377 }
378 setState(Volume::State_Mounted);
379 mCurrentlyMountedKdev = deviceNodes[i];
380 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800381 }
382
San Mehat97ac40e2010-03-24 10:24:19 -0700383 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800384 setState(Volume::State_Idle);
385
San Mehateba65e92010-01-29 05:15:16 -0800386 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800387}
388
San Mehat3bb60202010-02-19 18:14:36 -0800389int Volume::createBindMounts() {
390 unsigned long flags;
391
392 /*
San Mehat52c2ccb2010-02-23 18:26:13 -0800393 * Rename old /android_secure -> /.android_secure
394 */
395 if (!access("/mnt/secure/staging/android_secure", R_OK | X_OK) &&
396 access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
397 if (rename("/mnt/secure/staging/android_secure", SEC_STG_SECIMGDIR)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700398 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800399 }
400 }
401
402 /*
San Mehat3bb60202010-02-19 18:14:36 -0800403 * Ensure that /android_secure exists and is a directory
404 */
405 if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
406 if (errno == ENOENT) {
407 if (mkdir(SEC_STG_SECIMGDIR, 0777)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700408 SLOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800409 return -1;
410 }
411 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700412 SLOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800413 return -1;
414 }
415 } else {
416 struct stat sbuf;
417
418 if (stat(SEC_STG_SECIMGDIR, &sbuf)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700419 SLOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800420 return -1;
421 }
422 if (!S_ISDIR(sbuf.st_mode)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700423 SLOGE("%s is not a directory", SEC_STG_SECIMGDIR);
San Mehat3bb60202010-02-19 18:14:36 -0800424 errno = ENOTDIR;
425 return -1;
426 }
427 }
428
429 /*
430 * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll
431 * have a root only accessable mountpoint for it.
432 */
433 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700434 SLOGE("Failed to bind mount points %s -> %s (%s)",
San Mehat3bb60202010-02-19 18:14:36 -0800435 SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno));
436 return -1;
437 }
438
439 /*
440 * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to
441 * obscure the underlying directory from everybody - sneaky eh? ;)
442 */
443 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700444 SLOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800445 umount("/mnt/asec_secure");
446 return -1;
447 }
448
449 return 0;
450}
451
452int Volume::doMoveMount(const char *src, const char *dst, bool force) {
453 unsigned int flags = MS_MOVE;
454 int retries = 5;
455
456 while(retries--) {
457 if (!mount(src, dst, "", flags, NULL)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800458 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700459 SLOGD("Moved mount %s -> %s sucessfully", src, dst);
San Mehatd9a4e352010-03-12 13:32:47 -0800460 }
San Mehat3bb60202010-02-19 18:14:36 -0800461 return 0;
462 } else if (errno != EBUSY) {
San Mehat97ac40e2010-03-24 10:24:19 -0700463 SLOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800464 return -1;
465 }
466 int action = 0;
467
468 if (force) {
469 if (retries == 1) {
470 action = 2; // SIGKILL
471 } else if (retries == 2) {
472 action = 1; // SIGHUP
473 }
474 }
San Mehat97ac40e2010-03-24 10:24:19 -0700475 SLOGW("Failed to move %s -> %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800476 src, dst, strerror(errno), retries, action);
477 Process::killProcessesWithOpenFiles(src, action);
478 usleep(1000*250);
479 }
480
481 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700482 SLOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800483 return -1;
484}
485
486int Volume::doUnmount(const char *path, bool force) {
487 int retries = 10;
488
San Mehatd9a4e352010-03-12 13:32:47 -0800489 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700490 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800491 }
492
San Mehat3bb60202010-02-19 18:14:36 -0800493 while (retries--) {
494 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700495 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800496 return 0;
497 }
498
499 int action = 0;
500
501 if (force) {
502 if (retries == 1) {
503 action = 2; // SIGKILL
504 } else if (retries == 2) {
505 action = 1; // SIGHUP
506 }
507 }
508
San Mehat97ac40e2010-03-24 10:24:19 -0700509 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800510 path, strerror(errno), retries, action);
511
512 Process::killProcessesWithOpenFiles(path, action);
513 usleep(1000*1000);
514 }
515 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700516 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800517 return -1;
518}
519
San Mehat4ba89482010-02-18 09:00:18 -0800520int Volume::unmountVol(bool force) {
San Mehata2677e42009-12-13 10:40:18 -0800521 int i, rc;
522
523 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700524 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800525 errno = EINVAL;
526 return -1;
527 }
528
529 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800530 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800531
San Mehat3bb60202010-02-19 18:14:36 -0800532 /*
533 * First move the mountpoint back to our internal staging point
534 * so nobody else can muck with it while we work.
535 */
536 if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700537 SLOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800538 setState(Volume::State_Mounted);
539 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800540 }
541
San Mehatcb4dac82010-03-14 13:41:54 -0700542 protectFromAutorunStupidity();
543
San Mehat3bb60202010-02-19 18:14:36 -0800544 /*
545 * Unmount the tmpfs which was obscuring the asec image directory
546 * from non root users
547 */
548
549 if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700550 SLOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800551 goto fail_republish;
San Mehata2677e42009-12-13 10:40:18 -0800552 }
553
San Mehat3bb60202010-02-19 18:14:36 -0800554 /*
555 * Remove the bindmount we were using to keep a reference to
556 * the previously obscured directory.
557 */
558
559 if (doUnmount(Volume::SEC_ASECDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700560 SLOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800561 goto fail_remount_tmpfs;
562 }
563
564 /*
565 * Finally, unmount the actual block device from the staging dir
566 */
567 if (doUnmount(Volume::SEC_STGDIR, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700568 SLOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800569 goto fail_recreate_bindmount;
570 }
571
San Mehat97ac40e2010-03-24 10:24:19 -0700572 SLOGI("%s unmounted sucessfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800573
574 setState(Volume::State_Idle);
575 mCurrentlyMountedKdev = -1;
576 return 0;
577
578 /*
579 * Failure handling - try to restore everything back the way it was
580 */
581fail_recreate_bindmount:
582 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700583 SLOGE("Failed to restore bindmount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800584 goto out_nomedia;
585 }
586fail_remount_tmpfs:
587 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) {
San Mehat97ac40e2010-03-24 10:24:19 -0700588 SLOGE("Failed to restore tmpfs after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800589 goto out_nomedia;
590 }
591fail_republish:
592 if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700593 SLOGE("Failed to republish mount after failure! - Storage will appear offline!");
San Mehat3bb60202010-02-19 18:14:36 -0800594 goto out_nomedia;
595 }
596
San Mehata2677e42009-12-13 10:40:18 -0800597 setState(Volume::State_Mounted);
598 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800599
600out_nomedia:
601 setState(Volume::State_NoMedia);
602 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800603}
San Mehata2677e42009-12-13 10:40:18 -0800604int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800605 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800606
San Mehat2a5b8ce2010-03-10 12:48:57 -0800607 memset(&dinfo, 0, sizeof(dinfo));
608
609 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700610 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800611 return -1;
612 }
613
San Mehat2a5b8ce2010-03-10 12:48:57 -0800614 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
615 dinfo.device = strdup(deviceNode);
616 dinfo.scheme = PART_SCHEME_MBR;
617 dinfo.sect_size = 512;
618 dinfo.skip_lba = 2048;
619 dinfo.num_lba = 0;
620 dinfo.num_parts = 1;
621
622 struct part_info *pinfo = &dinfo.part_lst[0];
623
624 pinfo->name = strdup("android_sdcard");
625 pinfo->flags |= PART_ACTIVE_FLAG;
626 pinfo->type = PC_PART_TYPE_FAT32;
627 pinfo->len_kb = -1;
628
629 int rc = apply_disk_config(&dinfo, 0);
630
631 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700632 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800633 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800634 }
635
San Mehat2a5b8ce2010-03-10 12:48:57 -0800636 out:
637 free(pinfo->name);
638 free(dinfo.device);
639 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800640
San Mehat2a5b8ce2010-03-10 12:48:57 -0800641 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800642}