blob: 7d33919e62cae72d0ad16ad27ed8703d2c7095d2 [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>
Ken Sumrall29d8da82011-05-18 17:20:07 -070028#include <sys/param.h>
San Mehata2677e42009-12-13 10:40:18 -080029
30#include <linux/kdev_t.h>
31
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
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070040#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070041#include <cutils/log.h>
42
43#include "Volume.h"
San Mehata2677e42009-12-13 10:40:18 -080044#include "VolumeManager.h"
45#include "ResponseCode.h"
San Mehatbf041852010-01-04 10:09:16 -080046#include "Fat.h"
San Mehat586536c2010-02-16 17:12:00 -080047#include "Process.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070048#include "cryptfs.h"
San Mehatf1b736b2009-10-10 17:22:08 -070049
San Mehata2677e42009-12-13 10:40:18 -080050extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d);
51extern "C" void dos_partition_enc(void *pp, struct dos_partition *d);
San Mehat49e2bce2009-10-12 16:29:01 -070052
San Mehat3bb60202010-02-19 18:14:36 -080053
54/*
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070055 * Media directory - stuff that only media_rw user can see
San Mehat3bb60202010-02-19 18:14:36 -080056 */
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070057const char *Volume::MEDIA_DIR = "/mnt/media_rw";
San Mehat3bb60202010-02-19 18:14:36 -080058
59/*
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070060 * Fuse directory - location where fuse wrapped filesystems go
San Mehat3bb60202010-02-19 18:14:36 -080061 */
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070062const char *Volume::FUSE_DIR = "/storage";
San Mehat3bb60202010-02-19 18:14:36 -080063
64/*
Kenny Root344ca102012-04-03 17:23:01 -070065 * Path to external storage where *only* root can access ASEC image files
San Mehat3bb60202010-02-19 18:14:36 -080066 */
Kenny Root344ca102012-04-03 17:23:01 -070067const char *Volume::SEC_ASECDIR_EXT = "/mnt/secure/asec";
San Mehat3bb60202010-02-19 18:14:36 -080068
69/*
Kenny Root344ca102012-04-03 17:23:01 -070070 * Path to internal storage where *only* root can access ASEC image files
71 */
72const char *Volume::SEC_ASECDIR_INT = "/data/app-asec";
73/*
San Mehat3bb60202010-02-19 18:14:36 -080074 * Path to where secure containers are mounted
75 */
76const char *Volume::ASECDIR = "/mnt/asec";
77
Kenny Rootfb7c4d52010-06-30 18:48:41 -070078/*
Kenny Root508c0e12010-07-12 09:59:49 -070079 * Path to where OBBs are mounted
Kenny Rootfb7c4d52010-06-30 18:48:41 -070080 */
Kenny Root508c0e12010-07-12 09:59:49 -070081const char *Volume::LOOPDIR = "/mnt/obb";
Kenny Rootfb7c4d52010-06-30 18:48:41 -070082
San Mehata2677e42009-12-13 10:40:18 -080083static const char *stateToStr(int state) {
84 if (state == Volume::State_Init)
85 return "Initializing";
86 else if (state == Volume::State_NoMedia)
87 return "No-Media";
88 else if (state == Volume::State_Idle)
89 return "Idle-Unmounted";
90 else if (state == Volume::State_Pending)
91 return "Pending";
92 else if (state == Volume::State_Mounted)
93 return "Mounted";
94 else if (state == Volume::State_Unmounting)
95 return "Unmounting";
96 else if (state == Volume::State_Checking)
97 return "Checking";
98 else if (state == Volume::State_Formatting)
99 return "Formatting";
100 else if (state == Volume::State_Shared)
101 return "Shared-Unmounted";
102 else if (state == Volume::State_SharedMnt)
103 return "Shared-Mounted";
104 else
105 return "Unknown-Error";
106}
107
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700108Volume::Volume(VolumeManager *vm, const fstab_rec* rec, int flags) {
San Mehata2677e42009-12-13 10:40:18 -0800109 mVm = vm;
San Mehatd9a4e352010-03-12 13:32:47 -0800110 mDebug = false;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700111 mLabel = strdup(rec->label);
San Mehatf1b736b2009-10-10 17:22:08 -0700112 mState = Volume::State_Init;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700113 mFlags = flags;
San Mehata2677e42009-12-13 10:40:18 -0800114 mCurrentlyMountedKdev = -1;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700115 mPartIdx = rec->partnum;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700116 mRetryMount = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700117}
118
119Volume::~Volume() {
120 free(mLabel);
San Mehatf1b736b2009-10-10 17:22:08 -0700121}
122
San Mehatcb4dac82010-03-14 13:41:54 -0700123void Volume::protectFromAutorunStupidity() {
124 char filename[255];
125
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700126 snprintf(filename, sizeof(filename), "%s/autorun.inf", getMountpoint());
San Mehatcb4dac82010-03-14 13:41:54 -0700127 if (!access(filename, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700128 SLOGW("Volume contains an autorun.inf! - removing");
San Mehatcb4dac82010-03-14 13:41:54 -0700129 /*
130 * Ensure the filename is all lower-case so
131 * the process killer can find the inode.
132 * Probably being paranoid here but meh.
133 */
134 rename(filename, filename);
135 Process::killProcessesWithOpenFiles(filename, 2);
136 if (unlink(filename)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700137 SLOGE("Failed to remove %s (%s)", filename, strerror(errno));
San Mehatcb4dac82010-03-14 13:41:54 -0700138 }
139 }
140}
141
San Mehatd9a4e352010-03-12 13:32:47 -0800142void Volume::setDebug(bool enable) {
143 mDebug = enable;
144}
145
San Mehata2677e42009-12-13 10:40:18 -0800146dev_t Volume::getDiskDevice() {
147 return MKDEV(0, 0);
148};
149
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400150dev_t Volume::getShareDevice() {
151 return getDiskDevice();
152}
153
San Mehata2677e42009-12-13 10:40:18 -0800154void Volume::handleVolumeShared() {
155}
156
157void Volume::handleVolumeUnshared() {
158}
159
San Mehatfd7f5872009-10-12 11:32:47 -0700160int Volume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatf1b736b2009-10-10 17:22:08 -0700161 errno = ENOSYS;
162 return -1;
163}
164
165void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800166 char msg[255];
167 int oldState = mState;
168
169 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700170 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800171 return;
172 }
173
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700174 if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) {
175 mRetryMount = false;
176 }
177
San Mehatf1b736b2009-10-10 17:22:08 -0700178 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800179
San Mehat97ac40e2010-03-24 10:24:19 -0700180 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800181 oldState, stateToStr(oldState), mState, stateToStr(mState));
182 snprintf(msg, sizeof(msg),
183 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700184 getFuseMountpoint(), oldState, stateToStr(oldState), mState,
San Mehata2677e42009-12-13 10:40:18 -0800185 stateToStr(mState));
186
187 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
188 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700189}
San Mehat49e2bce2009-10-12 16:29:01 -0700190
San Mehatdd9b8e92009-10-21 11:06:52 -0700191int Volume::createDeviceNode(const char *path, int major, int minor) {
192 mode_t mode = 0660 | S_IFBLK;
193 dev_t dev = (major << 8) | minor;
194 if (mknod(path, mode, dev) < 0) {
195 if (errno != EEXIST) {
196 return -1;
197 }
198 }
199 return 0;
200}
201
Ken Sumrall9caab762013-06-11 19:10:20 -0700202int Volume::formatVol(bool wipe) {
San Mehat49e2bce2009-10-12 16:29:01 -0700203
San Mehata2677e42009-12-13 10:40:18 -0800204 if (getState() == Volume::State_NoMedia) {
205 errno = ENODEV;
206 return -1;
207 } else if (getState() != Volume::State_Idle) {
208 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700209 return -1;
210 }
211
San Mehata2677e42009-12-13 10:40:18 -0800212 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700213 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800214 setState(Volume::State_Mounted);
215 // mCurrentlyMountedKdev = XXX
216 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700217 return -1;
218 }
219
Mike Lockwooda4886f12010-09-21 13:56:35 -0400220 bool formatEntireDevice = (mPartIdx == -1);
San Mehata2677e42009-12-13 10:40:18 -0800221 char devicePath[255];
222 dev_t diskNode = getDiskDevice();
Mike Lockwooda4886f12010-09-21 13:56:35 -0400223 dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800224
San Mehat2a5b8ce2010-03-10 12:48:57 -0800225 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800226
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800227 int ret = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400228 // Only initialize the MBR if we are formatting the entire device
229 if (formatEntireDevice) {
230 sprintf(devicePath, "/dev/block/vold/%d:%d",
231 MAJOR(diskNode), MINOR(diskNode));
232
233 if (initializeMbr(devicePath)) {
234 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
235 goto err;
236 }
San Mehat49e2bce2009-10-12 16:29:01 -0700237 }
238
San Mehata2677e42009-12-13 10:40:18 -0800239 sprintf(devicePath, "/dev/block/vold/%d:%d",
240 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700241
Mike Lockwooda4886f12010-09-21 13:56:35 -0400242 if (mDebug) {
243 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
244 }
245
Ken Sumrall9caab762013-06-11 19:10:20 -0700246 if (Fat::format(devicePath, 0, wipe)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700247 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800248 goto err;
249 }
250
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800251 ret = 0;
252
San Mehata2677e42009-12-13 10:40:18 -0800253err:
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800254 setState(Volume::State_Idle);
255 return ret;
San Mehata2677e42009-12-13 10:40:18 -0800256}
257
258bool Volume::isMountpointMounted(const char *path) {
259 char device[256];
260 char mount_path[256];
261 char rest[256];
262 FILE *fp;
263 char line[1024];
264
265 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700266 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800267 return false;
268 }
269
270 while(fgets(line, sizeof(line), fp)) {
271 line[strlen(line)-1] = '\0';
272 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
273 if (!strcmp(mount_path, path)) {
274 fclose(fp);
275 return true;
276 }
277
278 }
279
280 fclose(fp);
281 return false;
282}
283
284int Volume::mountVol() {
285 dev_t deviceNodes[4];
286 int n, i, rc = 0;
287 char errmsg[255];
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700288
289 int flags = getFlags();
290 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
291
292 // TODO: handle "bind" style mounts, for emulated storage
293
Ken Sumrall29d8da82011-05-18 17:20:07 -0700294 char decrypt_state[PROPERTY_VALUE_MAX];
295 char crypto_state[PROPERTY_VALUE_MAX];
296 char encrypt_progress[PROPERTY_VALUE_MAX];
San Mehata2677e42009-12-13 10:40:18 -0800297
Ken Sumrall29d8da82011-05-18 17:20:07 -0700298 property_get("vold.decrypt", decrypt_state, "");
299 property_get("vold.encrypt_progress", encrypt_progress, "");
300
301 /* Don't try to mount the volumes if we have not yet entered the disk password
302 * or are in the process of encrypting.
303 */
304 if ((getState() == Volume::State_NoMedia) ||
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700305 ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) {
San Mehata2677e42009-12-13 10:40:18 -0800306 snprintf(errmsg, sizeof(errmsg),
307 "Volume %s %s mount failed - no media",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700308 getLabel(), getFuseMountpoint());
San Mehata2677e42009-12-13 10:40:18 -0800309 mVm->getBroadcaster()->sendBroadcast(
310 ResponseCode::VolumeMountFailedNoMedia,
311 errmsg, false);
312 errno = ENODEV;
313 return -1;
314 } else if (getState() != Volume::State_Idle) {
315 errno = EBUSY;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700316 if (getState() == Volume::State_Pending) {
317 mRetryMount = true;
318 }
San Mehata2677e42009-12-13 10:40:18 -0800319 return -1;
320 }
321
322 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700323 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800324 setState(Volume::State_Mounted);
325 // mCurrentlyMountedKdev = XXX
326 return 0;
327 }
328
329 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
330 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700331 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800332 return -1;
333 }
334
Ken Sumrall29d8da82011-05-18 17:20:07 -0700335 /* If we're running encrypted, and the volume is marked as encryptable and nonremovable,
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700336 * and also marked as providing Asec storage, then we need to decrypt
Ken Sumrall29d8da82011-05-18 17:20:07 -0700337 * that partition, and update the volume object to point to it's new decrypted
338 * block device
339 */
340 property_get("ro.crypto.state", crypto_state, "");
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700341 if (providesAsec &&
Ken Sumrall29d8da82011-05-18 17:20:07 -0700342 ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) &&
343 !strcmp(crypto_state, "encrypted") && !isDecrypted()) {
344 char new_sys_path[MAXPATHLEN];
345 char nodepath[256];
346 int new_major, new_minor;
347
348 if (n != 1) {
349 /* We only expect one device node returned when mounting encryptable volumes */
350 SLOGE("Too many device nodes returned when mounting %d\n", getMountpoint());
351 return -1;
352 }
353
354 if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]),
355 new_sys_path, sizeof(new_sys_path),
356 &new_major, &new_minor)) {
357 SLOGE("Cannot setup encryption mapping for %d\n", getMountpoint());
358 return -1;
359 }
360 /* We now have the new sysfs path for the decrypted block device, and the
361 * majore and minor numbers for it. So, create the device, update the
362 * path to the new sysfs path, and continue.
363 */
364 snprintf(nodepath,
365 sizeof(nodepath), "/dev/block/vold/%d:%d",
366 new_major, new_minor);
367 if (createDeviceNode(nodepath, new_major, new_minor)) {
368 SLOGE("Error making device node '%s' (%s)", nodepath,
369 strerror(errno));
370 }
371
372 // Todo: Either create sys filename from nodepath, or pass in bogus path so
373 // vold ignores state changes on this internal device.
374 updateDeviceInfo(nodepath, new_major, new_minor);
375
376 /* Get the device nodes again, because they just changed */
377 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
378 if (!n) {
379 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
380 return -1;
381 }
382 }
383
San Mehata2677e42009-12-13 10:40:18 -0800384 for (i = 0; i < n; i++) {
385 char devicePath[255];
386
387 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
388 MINOR(deviceNodes[i]));
389
San Mehat97ac40e2010-03-24 10:24:19 -0700390 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800391
392 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800393 setState(Volume::State_Checking);
394
San Mehat3bb60202010-02-19 18:14:36 -0800395 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800396 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700397 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800398 continue;
San Mehata2677e42009-12-13 10:40:18 -0800399 }
San Mehateba65e92010-01-29 05:15:16 -0800400 errno = EIO;
401 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700402 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800403 setState(Volume::State_Idle);
404 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800405 }
406
San Mehata2677e42009-12-13 10:40:18 -0800407 errno = 0;
Mike Lockwood9092b1d2011-03-23 14:55:49 -0400408 int gid;
409
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700410 if (Fat::doMount(devicePath, getMountpoint(), false, false, false,
411 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700412 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800413 continue;
San Mehata2677e42009-12-13 10:40:18 -0800414 }
415
San Mehatcb4dac82010-03-14 13:41:54 -0700416 protectFromAutorunStupidity();
417
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700418 if (providesAsec && mountAsecExternal() != 0) {
419 SLOGE("Failed to mount secure area (%s)", strerror(errno));
420 umount(getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800421 setState(Volume::State_Idle);
422 return -1;
423 }
424
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700425 char service[64];
426 snprintf(service, 64, "fuse_%s", getLabel());
427 property_set("ctl.start", service);
428
San Mehat3bb60202010-02-19 18:14:36 -0800429 setState(Volume::State_Mounted);
430 mCurrentlyMountedKdev = deviceNodes[i];
431 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800432 }
433
San Mehat97ac40e2010-03-24 10:24:19 -0700434 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800435 setState(Volume::State_Idle);
436
San Mehateba65e92010-01-29 05:15:16 -0800437 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800438}
439
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700440int Volume::mountAsecExternal() {
441 char legacy_path[PATH_MAX];
442 char secure_path[PATH_MAX];
San Mehat3bb60202010-02-19 18:14:36 -0800443
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700444 snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint());
445 snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint());
446
447 // Recover legacy secure path
448 if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) {
449 if (rename(legacy_path, secure_path)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700450 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800451 }
452 }
453
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700454 if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
San Mehat3bb60202010-02-19 18:14:36 -0800455 return -1;
456 }
457
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700458 if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) {
459 SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path,
460 SEC_ASECDIR_EXT, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800461 return -1;
462 }
463
464 return 0;
465}
466
San Mehat3bb60202010-02-19 18:14:36 -0800467int Volume::doUnmount(const char *path, bool force) {
468 int retries = 10;
469
San Mehatd9a4e352010-03-12 13:32:47 -0800470 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700471 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800472 }
473
San Mehat3bb60202010-02-19 18:14:36 -0800474 while (retries--) {
475 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700476 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800477 return 0;
478 }
479
480 int action = 0;
481
482 if (force) {
483 if (retries == 1) {
484 action = 2; // SIGKILL
485 } else if (retries == 2) {
486 action = 1; // SIGHUP
487 }
488 }
489
San Mehat97ac40e2010-03-24 10:24:19 -0700490 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800491 path, strerror(errno), retries, action);
492
493 Process::killProcessesWithOpenFiles(path, action);
494 usleep(1000*1000);
495 }
496 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700497 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800498 return -1;
499}
500
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700501int Volume::unmountVol(bool force, bool revert) {
San Mehata2677e42009-12-13 10:40:18 -0800502 int i, rc;
503
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700504 int flags = getFlags();
505 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
506
San Mehata2677e42009-12-13 10:40:18 -0800507 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700508 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800509 errno = EINVAL;
Ken Sumrall319b1042011-06-14 14:01:55 -0700510 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehata2677e42009-12-13 10:40:18 -0800511 }
512
513 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800514 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800515
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700516 char service[64];
517 snprintf(service, 64, "fuse_%s", getLabel());
518 property_set("ctl.stop", service);
519 /* Give it a chance to stop. I wish we had a synchronous way to determine this... */
520 sleep(1);
521
522 // TODO: determine failure mode if FUSE times out
523
524 if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) {
525 SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno));
526 goto out_mounted;
San Mehat3bb60202010-02-19 18:14:36 -0800527 }
528
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700529 /* Now that the fuse daemon is dead, unmount it */
530 if (doUnmount(getFuseMountpoint(), force) != 0) {
531 SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno));
532 goto fail_remount_secure;
Jeff Sharkey7a3c3d42012-10-04 16:49:22 -0700533 }
534
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700535 /* Unmount the real sd card */
536 if (doUnmount(getMountpoint(), force) != 0) {
537 SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno));
538 goto fail_remount_secure;
San Mehat3bb60202010-02-19 18:14:36 -0800539 }
540
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700541 SLOGI("%s unmounted successfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800542
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700543 /* If this is an encrypted volume, and we've been asked to undo
544 * the crypto mapping, then revert the dm-crypt mapping, and revert
545 * the device info to the original values.
546 */
547 if (revert && isDecrypted()) {
548 cryptfs_revert_volume(getLabel());
549 revertDeviceInfo();
550 SLOGI("Encrypted volume %s reverted successfully", getMountpoint());
551 }
552
San Mehat3bb60202010-02-19 18:14:36 -0800553 setState(Volume::State_Idle);
554 mCurrentlyMountedKdev = -1;
555 return 0;
556
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700557fail_remount_secure:
558 if (providesAsec && mountAsecExternal() != 0) {
559 SLOGE("Failed to remount secure area (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800560 goto out_nomedia;
561 }
562
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700563out_mounted:
San Mehata2677e42009-12-13 10:40:18 -0800564 setState(Volume::State_Mounted);
565 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800566
567out_nomedia:
568 setState(Volume::State_NoMedia);
569 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800570}
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700571
San Mehata2677e42009-12-13 10:40:18 -0800572int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800573 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800574
San Mehat2a5b8ce2010-03-10 12:48:57 -0800575 memset(&dinfo, 0, sizeof(dinfo));
576
577 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700578 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800579 return -1;
580 }
581
San Mehat2a5b8ce2010-03-10 12:48:57 -0800582 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
583 dinfo.device = strdup(deviceNode);
584 dinfo.scheme = PART_SCHEME_MBR;
585 dinfo.sect_size = 512;
586 dinfo.skip_lba = 2048;
587 dinfo.num_lba = 0;
588 dinfo.num_parts = 1;
589
590 struct part_info *pinfo = &dinfo.part_lst[0];
591
592 pinfo->name = strdup("android_sdcard");
593 pinfo->flags |= PART_ACTIVE_FLAG;
594 pinfo->type = PC_PART_TYPE_FAT32;
595 pinfo->len_kb = -1;
596
597 int rc = apply_disk_config(&dinfo, 0);
598
599 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700600 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800601 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800602 }
603
San Mehat2a5b8ce2010-03-10 12:48:57 -0800604 out:
605 free(pinfo->name);
606 free(dinfo.device);
607 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800608
San Mehat2a5b8ce2010-03-10 12:48:57 -0800609 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800610}