blob: cbec5d4efdf335a56e9b7aeeb60bb9677ebb3bdb [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
Jeff Sharkey0de365f2013-10-16 16:24:19 -070043#include <string>
44
San Mehatf1b736b2009-10-10 17:22:08 -070045#include "Volume.h"
San Mehata2677e42009-12-13 10:40:18 -080046#include "VolumeManager.h"
47#include "ResponseCode.h"
San Mehatbf041852010-01-04 10:09:16 -080048#include "Fat.h"
San Mehat586536c2010-02-16 17:12:00 -080049#include "Process.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070050#include "cryptfs.h"
San Mehatf1b736b2009-10-10 17:22:08 -070051
San Mehata2677e42009-12-13 10:40:18 -080052extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d);
53extern "C" void dos_partition_enc(void *pp, struct dos_partition *d);
San Mehat49e2bce2009-10-12 16:29:01 -070054
San Mehat3bb60202010-02-19 18:14:36 -080055
56/*
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070057 * Media directory - stuff that only media_rw user can see
San Mehat3bb60202010-02-19 18:14:36 -080058 */
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070059const char *Volume::MEDIA_DIR = "/mnt/media_rw";
San Mehat3bb60202010-02-19 18:14:36 -080060
61/*
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070062 * Fuse directory - location where fuse wrapped filesystems go
San Mehat3bb60202010-02-19 18:14:36 -080063 */
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070064const char *Volume::FUSE_DIR = "/storage";
San Mehat3bb60202010-02-19 18:14:36 -080065
66/*
Kenny Root344ca102012-04-03 17:23:01 -070067 * Path to external storage where *only* root can access ASEC image files
San Mehat3bb60202010-02-19 18:14:36 -080068 */
Kenny Root344ca102012-04-03 17:23:01 -070069const char *Volume::SEC_ASECDIR_EXT = "/mnt/secure/asec";
San Mehat3bb60202010-02-19 18:14:36 -080070
71/*
Kenny Root344ca102012-04-03 17:23:01 -070072 * Path to internal storage where *only* root can access ASEC image files
73 */
74const char *Volume::SEC_ASECDIR_INT = "/data/app-asec";
75/*
San Mehat3bb60202010-02-19 18:14:36 -080076 * Path to where secure containers are mounted
77 */
78const char *Volume::ASECDIR = "/mnt/asec";
79
Kenny Rootfb7c4d52010-06-30 18:48:41 -070080/*
Kenny Root508c0e12010-07-12 09:59:49 -070081 * Path to where OBBs are mounted
Kenny Rootfb7c4d52010-06-30 18:48:41 -070082 */
Kenny Root508c0e12010-07-12 09:59:49 -070083const char *Volume::LOOPDIR = "/mnt/obb";
Kenny Rootfb7c4d52010-06-30 18:48:41 -070084
Jeff Sharkey0de365f2013-10-16 16:24:19 -070085const char *Volume::BLKID_PATH = "/system/bin/blkid";
86
San Mehata2677e42009-12-13 10:40:18 -080087static const char *stateToStr(int state) {
88 if (state == Volume::State_Init)
89 return "Initializing";
90 else if (state == Volume::State_NoMedia)
91 return "No-Media";
92 else if (state == Volume::State_Idle)
93 return "Idle-Unmounted";
94 else if (state == Volume::State_Pending)
95 return "Pending";
96 else if (state == Volume::State_Mounted)
97 return "Mounted";
98 else if (state == Volume::State_Unmounting)
99 return "Unmounting";
100 else if (state == Volume::State_Checking)
101 return "Checking";
102 else if (state == Volume::State_Formatting)
103 return "Formatting";
104 else if (state == Volume::State_Shared)
105 return "Shared-Unmounted";
106 else if (state == Volume::State_SharedMnt)
107 return "Shared-Mounted";
108 else
109 return "Unknown-Error";
110}
111
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700112Volume::Volume(VolumeManager *vm, const fstab_rec* rec, int flags) {
San Mehata2677e42009-12-13 10:40:18 -0800113 mVm = vm;
San Mehatd9a4e352010-03-12 13:32:47 -0800114 mDebug = false;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700115 mLabel = strdup(rec->label);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700116 mUuid = NULL;
117 mUserLabel = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700118 mState = Volume::State_Init;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700119 mFlags = flags;
San Mehata2677e42009-12-13 10:40:18 -0800120 mCurrentlyMountedKdev = -1;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700121 mPartIdx = rec->partnum;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700122 mRetryMount = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700123}
124
125Volume::~Volume() {
126 free(mLabel);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700127 free(mUuid);
128 free(mUserLabel);
San Mehatcb4dac82010-03-14 13:41:54 -0700129}
130
San Mehatd9a4e352010-03-12 13:32:47 -0800131void Volume::setDebug(bool enable) {
132 mDebug = enable;
133}
134
San Mehata2677e42009-12-13 10:40:18 -0800135dev_t Volume::getDiskDevice() {
136 return MKDEV(0, 0);
137};
138
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400139dev_t Volume::getShareDevice() {
140 return getDiskDevice();
141}
142
San Mehata2677e42009-12-13 10:40:18 -0800143void Volume::handleVolumeShared() {
144}
145
146void Volume::handleVolumeUnshared() {
147}
148
San Mehatfd7f5872009-10-12 11:32:47 -0700149int Volume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatf1b736b2009-10-10 17:22:08 -0700150 errno = ENOSYS;
151 return -1;
152}
153
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700154void Volume::setUuid(const char* uuid) {
155 char msg[256];
156
157 if (mUuid) {
158 free(mUuid);
159 }
160
161 if (uuid) {
162 mUuid = strdup(uuid);
163 snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(),
164 getFuseMountpoint(), mUuid);
165 } else {
166 mUuid = NULL;
167 snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint());
168 }
169
170 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUuidChange, msg,
171 false);
172}
173
174void Volume::setUserLabel(const char* userLabel) {
175 char msg[256];
176
177 if (mUserLabel) {
178 free(mUserLabel);
179 }
180
181 if (userLabel) {
182 mUserLabel = strdup(userLabel);
183 snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(),
184 getFuseMountpoint(), mUserLabel);
185 } else {
186 mUserLabel = NULL;
187 snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint());
188 }
189
190 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUserLabelChange,
191 msg, false);
192}
193
San Mehatf1b736b2009-10-10 17:22:08 -0700194void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800195 char msg[255];
196 int oldState = mState;
197
198 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700199 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800200 return;
201 }
202
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700203 if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) {
204 mRetryMount = false;
205 }
206
San Mehatf1b736b2009-10-10 17:22:08 -0700207 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800208
San Mehat97ac40e2010-03-24 10:24:19 -0700209 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800210 oldState, stateToStr(oldState), mState, stateToStr(mState));
211 snprintf(msg, sizeof(msg),
212 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700213 getFuseMountpoint(), oldState, stateToStr(oldState), mState,
San Mehata2677e42009-12-13 10:40:18 -0800214 stateToStr(mState));
215
216 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
217 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700218}
San Mehat49e2bce2009-10-12 16:29:01 -0700219
San Mehatdd9b8e92009-10-21 11:06:52 -0700220int Volume::createDeviceNode(const char *path, int major, int minor) {
221 mode_t mode = 0660 | S_IFBLK;
222 dev_t dev = (major << 8) | minor;
223 if (mknod(path, mode, dev) < 0) {
224 if (errno != EEXIST) {
225 return -1;
226 }
227 }
228 return 0;
229}
230
Ken Sumrall9caab762013-06-11 19:10:20 -0700231int Volume::formatVol(bool wipe) {
San Mehat49e2bce2009-10-12 16:29:01 -0700232
San Mehata2677e42009-12-13 10:40:18 -0800233 if (getState() == Volume::State_NoMedia) {
234 errno = ENODEV;
235 return -1;
236 } else if (getState() != Volume::State_Idle) {
237 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700238 return -1;
239 }
240
San Mehata2677e42009-12-13 10:40:18 -0800241 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700242 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800243 setState(Volume::State_Mounted);
244 // mCurrentlyMountedKdev = XXX
245 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700246 return -1;
247 }
248
Mike Lockwooda4886f12010-09-21 13:56:35 -0400249 bool formatEntireDevice = (mPartIdx == -1);
San Mehata2677e42009-12-13 10:40:18 -0800250 char devicePath[255];
251 dev_t diskNode = getDiskDevice();
Mike Lockwooda4886f12010-09-21 13:56:35 -0400252 dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800253
San Mehat2a5b8ce2010-03-10 12:48:57 -0800254 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800255
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800256 int ret = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400257 // Only initialize the MBR if we are formatting the entire device
258 if (formatEntireDevice) {
259 sprintf(devicePath, "/dev/block/vold/%d:%d",
260 MAJOR(diskNode), MINOR(diskNode));
261
262 if (initializeMbr(devicePath)) {
263 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
264 goto err;
265 }
San Mehat49e2bce2009-10-12 16:29:01 -0700266 }
267
San Mehata2677e42009-12-13 10:40:18 -0800268 sprintf(devicePath, "/dev/block/vold/%d:%d",
269 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700270
Mike Lockwooda4886f12010-09-21 13:56:35 -0400271 if (mDebug) {
272 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
273 }
274
Ken Sumrall9caab762013-06-11 19:10:20 -0700275 if (Fat::format(devicePath, 0, wipe)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700276 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800277 goto err;
278 }
279
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800280 ret = 0;
281
San Mehata2677e42009-12-13 10:40:18 -0800282err:
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800283 setState(Volume::State_Idle);
284 return ret;
San Mehata2677e42009-12-13 10:40:18 -0800285}
286
287bool Volume::isMountpointMounted(const char *path) {
288 char device[256];
289 char mount_path[256];
290 char rest[256];
291 FILE *fp;
292 char line[1024];
293
294 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700295 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800296 return false;
297 }
298
299 while(fgets(line, sizeof(line), fp)) {
300 line[strlen(line)-1] = '\0';
301 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
302 if (!strcmp(mount_path, path)) {
303 fclose(fp);
304 return true;
305 }
San Mehata2677e42009-12-13 10:40:18 -0800306 }
307
308 fclose(fp);
309 return false;
310}
311
312int Volume::mountVol() {
313 dev_t deviceNodes[4];
314 int n, i, rc = 0;
315 char errmsg[255];
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700316
317 int flags = getFlags();
318 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
319
320 // TODO: handle "bind" style mounts, for emulated storage
321
Ken Sumrall29d8da82011-05-18 17:20:07 -0700322 char decrypt_state[PROPERTY_VALUE_MAX];
323 char crypto_state[PROPERTY_VALUE_MAX];
324 char encrypt_progress[PROPERTY_VALUE_MAX];
San Mehata2677e42009-12-13 10:40:18 -0800325
Ken Sumrall29d8da82011-05-18 17:20:07 -0700326 property_get("vold.decrypt", decrypt_state, "");
327 property_get("vold.encrypt_progress", encrypt_progress, "");
328
329 /* Don't try to mount the volumes if we have not yet entered the disk password
330 * or are in the process of encrypting.
331 */
332 if ((getState() == Volume::State_NoMedia) ||
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700333 ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) {
San Mehata2677e42009-12-13 10:40:18 -0800334 snprintf(errmsg, sizeof(errmsg),
335 "Volume %s %s mount failed - no media",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700336 getLabel(), getFuseMountpoint());
San Mehata2677e42009-12-13 10:40:18 -0800337 mVm->getBroadcaster()->sendBroadcast(
338 ResponseCode::VolumeMountFailedNoMedia,
339 errmsg, false);
340 errno = ENODEV;
341 return -1;
342 } else if (getState() != Volume::State_Idle) {
343 errno = EBUSY;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700344 if (getState() == Volume::State_Pending) {
345 mRetryMount = true;
346 }
San Mehata2677e42009-12-13 10:40:18 -0800347 return -1;
348 }
349
350 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700351 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800352 setState(Volume::State_Mounted);
353 // mCurrentlyMountedKdev = XXX
354 return 0;
355 }
356
357 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
358 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700359 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800360 return -1;
361 }
362
Ken Sumrall29d8da82011-05-18 17:20:07 -0700363 /* If we're running encrypted, and the volume is marked as encryptable and nonremovable,
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700364 * and also marked as providing Asec storage, then we need to decrypt
Ken Sumrall29d8da82011-05-18 17:20:07 -0700365 * that partition, and update the volume object to point to it's new decrypted
366 * block device
367 */
368 property_get("ro.crypto.state", crypto_state, "");
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700369 if (providesAsec &&
Ken Sumrall29d8da82011-05-18 17:20:07 -0700370 ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) &&
371 !strcmp(crypto_state, "encrypted") && !isDecrypted()) {
372 char new_sys_path[MAXPATHLEN];
373 char nodepath[256];
374 int new_major, new_minor;
375
376 if (n != 1) {
377 /* We only expect one device node returned when mounting encryptable volumes */
378 SLOGE("Too many device nodes returned when mounting %d\n", getMountpoint());
379 return -1;
380 }
381
382 if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]),
383 new_sys_path, sizeof(new_sys_path),
384 &new_major, &new_minor)) {
385 SLOGE("Cannot setup encryption mapping for %d\n", getMountpoint());
386 return -1;
387 }
388 /* We now have the new sysfs path for the decrypted block device, and the
389 * majore and minor numbers for it. So, create the device, update the
390 * path to the new sysfs path, and continue.
391 */
392 snprintf(nodepath,
393 sizeof(nodepath), "/dev/block/vold/%d:%d",
394 new_major, new_minor);
395 if (createDeviceNode(nodepath, new_major, new_minor)) {
396 SLOGE("Error making device node '%s' (%s)", nodepath,
397 strerror(errno));
398 }
399
400 // Todo: Either create sys filename from nodepath, or pass in bogus path so
401 // vold ignores state changes on this internal device.
402 updateDeviceInfo(nodepath, new_major, new_minor);
403
404 /* Get the device nodes again, because they just changed */
405 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
406 if (!n) {
407 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
408 return -1;
409 }
410 }
411
San Mehata2677e42009-12-13 10:40:18 -0800412 for (i = 0; i < n; i++) {
413 char devicePath[255];
414
415 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
416 MINOR(deviceNodes[i]));
417
San Mehat97ac40e2010-03-24 10:24:19 -0700418 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800419
420 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800421 setState(Volume::State_Checking);
422
San Mehat3bb60202010-02-19 18:14:36 -0800423 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800424 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700425 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800426 continue;
San Mehata2677e42009-12-13 10:40:18 -0800427 }
San Mehateba65e92010-01-29 05:15:16 -0800428 errno = EIO;
429 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700430 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800431 setState(Volume::State_Idle);
432 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800433 }
434
San Mehata2677e42009-12-13 10:40:18 -0800435 errno = 0;
Mike Lockwood9092b1d2011-03-23 14:55:49 -0400436 int gid;
437
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700438 if (Fat::doMount(devicePath, getMountpoint(), false, false, false,
439 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700440 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800441 continue;
San Mehata2677e42009-12-13 10:40:18 -0800442 }
443
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700444 extractMetadata(devicePath);
San Mehatcb4dac82010-03-14 13:41:54 -0700445
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700446 if (providesAsec && mountAsecExternal() != 0) {
447 SLOGE("Failed to mount secure area (%s)", strerror(errno));
448 umount(getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800449 setState(Volume::State_Idle);
450 return -1;
451 }
452
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700453 char service[64];
454 snprintf(service, 64, "fuse_%s", getLabel());
455 property_set("ctl.start", service);
456
San Mehat3bb60202010-02-19 18:14:36 -0800457 setState(Volume::State_Mounted);
458 mCurrentlyMountedKdev = deviceNodes[i];
459 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800460 }
461
San Mehat97ac40e2010-03-24 10:24:19 -0700462 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800463 setState(Volume::State_Idle);
464
San Mehateba65e92010-01-29 05:15:16 -0800465 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800466}
467
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700468int Volume::mountAsecExternal() {
469 char legacy_path[PATH_MAX];
470 char secure_path[PATH_MAX];
San Mehat3bb60202010-02-19 18:14:36 -0800471
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700472 snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint());
473 snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint());
474
475 // Recover legacy secure path
476 if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) {
477 if (rename(legacy_path, secure_path)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700478 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800479 }
480 }
481
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700482 if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
San Mehat3bb60202010-02-19 18:14:36 -0800483 return -1;
484 }
485
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700486 if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) {
487 SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path,
488 SEC_ASECDIR_EXT, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800489 return -1;
490 }
491
492 return 0;
493}
494
San Mehat3bb60202010-02-19 18:14:36 -0800495int Volume::doUnmount(const char *path, bool force) {
496 int retries = 10;
497
San Mehatd9a4e352010-03-12 13:32:47 -0800498 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700499 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800500 }
501
San Mehat3bb60202010-02-19 18:14:36 -0800502 while (retries--) {
503 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700504 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800505 return 0;
506 }
507
508 int action = 0;
509
510 if (force) {
511 if (retries == 1) {
512 action = 2; // SIGKILL
513 } else if (retries == 2) {
514 action = 1; // SIGHUP
515 }
516 }
517
San Mehat97ac40e2010-03-24 10:24:19 -0700518 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800519 path, strerror(errno), retries, action);
520
521 Process::killProcessesWithOpenFiles(path, action);
522 usleep(1000*1000);
523 }
524 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700525 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800526 return -1;
527}
528
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700529int Volume::unmountVol(bool force, bool revert) {
San Mehata2677e42009-12-13 10:40:18 -0800530 int i, rc;
531
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700532 int flags = getFlags();
533 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
534
San Mehata2677e42009-12-13 10:40:18 -0800535 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700536 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800537 errno = EINVAL;
Ken Sumrall319b1042011-06-14 14:01:55 -0700538 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehata2677e42009-12-13 10:40:18 -0800539 }
540
541 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800542 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800543
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700544 char service[64];
545 snprintf(service, 64, "fuse_%s", getLabel());
546 property_set("ctl.stop", service);
547 /* Give it a chance to stop. I wish we had a synchronous way to determine this... */
548 sleep(1);
549
550 // TODO: determine failure mode if FUSE times out
551
552 if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) {
553 SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno));
554 goto out_mounted;
San Mehat3bb60202010-02-19 18:14:36 -0800555 }
556
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700557 /* Now that the fuse daemon is dead, unmount it */
558 if (doUnmount(getFuseMountpoint(), force) != 0) {
559 SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno));
560 goto fail_remount_secure;
Jeff Sharkey7a3c3d42012-10-04 16:49:22 -0700561 }
562
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700563 /* Unmount the real sd card */
564 if (doUnmount(getMountpoint(), force) != 0) {
565 SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno));
566 goto fail_remount_secure;
San Mehat3bb60202010-02-19 18:14:36 -0800567 }
568
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700569 SLOGI("%s unmounted successfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800570
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700571 /* If this is an encrypted volume, and we've been asked to undo
572 * the crypto mapping, then revert the dm-crypt mapping, and revert
573 * the device info to the original values.
574 */
575 if (revert && isDecrypted()) {
576 cryptfs_revert_volume(getLabel());
577 revertDeviceInfo();
578 SLOGI("Encrypted volume %s reverted successfully", getMountpoint());
579 }
580
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700581 setUuid(NULL);
582 setUserLabel(NULL);
San Mehat3bb60202010-02-19 18:14:36 -0800583 setState(Volume::State_Idle);
584 mCurrentlyMountedKdev = -1;
585 return 0;
586
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700587fail_remount_secure:
588 if (providesAsec && mountAsecExternal() != 0) {
589 SLOGE("Failed to remount secure area (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800590 goto out_nomedia;
591 }
592
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700593out_mounted:
San Mehata2677e42009-12-13 10:40:18 -0800594 setState(Volume::State_Mounted);
595 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800596
597out_nomedia:
598 setState(Volume::State_NoMedia);
599 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800600}
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700601
San Mehata2677e42009-12-13 10:40:18 -0800602int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800603 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800604
San Mehat2a5b8ce2010-03-10 12:48:57 -0800605 memset(&dinfo, 0, sizeof(dinfo));
606
607 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700608 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800609 return -1;
610 }
611
San Mehat2a5b8ce2010-03-10 12:48:57 -0800612 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
613 dinfo.device = strdup(deviceNode);
614 dinfo.scheme = PART_SCHEME_MBR;
615 dinfo.sect_size = 512;
616 dinfo.skip_lba = 2048;
617 dinfo.num_lba = 0;
618 dinfo.num_parts = 1;
619
620 struct part_info *pinfo = &dinfo.part_lst[0];
621
622 pinfo->name = strdup("android_sdcard");
623 pinfo->flags |= PART_ACTIVE_FLAG;
624 pinfo->type = PC_PART_TYPE_FAT32;
625 pinfo->len_kb = -1;
626
627 int rc = apply_disk_config(&dinfo, 0);
628
629 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700630 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800631 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800632 }
633
San Mehat2a5b8ce2010-03-10 12:48:57 -0800634 out:
635 free(pinfo->name);
636 free(dinfo.device);
637 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800638
San Mehat2a5b8ce2010-03-10 12:48:57 -0800639 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800640}
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700641
642/*
643 * Use blkid to extract UUID and label from device, since it handles many
644 * obscure edge cases around partition types and formats. Always broadcasts
645 * updated metadata values.
646 */
647int Volume::extractMetadata(const char* devicePath) {
648 int res = 0;
649
650 std::string cmd;
651 cmd = BLKID_PATH;
652 cmd += " -c /dev/null ";
653 cmd += devicePath;
654
655 FILE* fp = popen(cmd.c_str(), "r");
656 if (!fp) {
657 ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno));
658 res = -1;
659 goto done;
660 }
661
662 char line[1024];
663 char value[128];
664 if (fgets(line, sizeof(line), fp) != NULL) {
665 ALOGD("blkid reported: %s", line);
666
667 char* start = strstr(line, "UUID=") + 5;
668 if (sscanf(start, "\"%127[^\"]\"", value) == 1) {
669 setUuid(value);
670 } else {
671 setUuid(NULL);
672 }
673
674 start = strstr(line, "LABEL=") + 6;
675 if (sscanf(start, "\"%127[^\"]\"", value) == 1) {
676 setUserLabel(value);
677 } else {
678 setUserLabel(NULL);
679 }
680 } else {
681 res = -1;
682 }
683
684 pclose(fp);
685
686done:
687 if (res == -1) {
688 setUuid(NULL);
689 setUserLabel(NULL);
690 }
691 return res;
692}