blob: 75cc4c15319c975807ee07c477081c7d0f913099 [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"
Stephen Smalley684e6622014-09-30 10:29:24 -040051#include "sehandle.h"
San Mehatf1b736b2009-10-10 17:22:08 -070052
San Mehata2677e42009-12-13 10:40:18 -080053extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d);
54extern "C" void dos_partition_enc(void *pp, struct dos_partition *d);
San Mehat49e2bce2009-10-12 16:29:01 -070055
San Mehat3bb60202010-02-19 18:14:36 -080056
57/*
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070058 * Media directory - stuff that only media_rw user can see
San Mehat3bb60202010-02-19 18:14:36 -080059 */
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070060const char *Volume::MEDIA_DIR = "/mnt/media_rw";
San Mehat3bb60202010-02-19 18:14:36 -080061
62/*
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070063 * Fuse directory - location where fuse wrapped filesystems go
San Mehat3bb60202010-02-19 18:14:36 -080064 */
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -070065const char *Volume::FUSE_DIR = "/storage";
San Mehat3bb60202010-02-19 18:14:36 -080066
67/*
Kenny Root344ca102012-04-03 17:23:01 -070068 * Path to external storage where *only* root can access ASEC image files
San Mehat3bb60202010-02-19 18:14:36 -080069 */
Kenny Root344ca102012-04-03 17:23:01 -070070const char *Volume::SEC_ASECDIR_EXT = "/mnt/secure/asec";
San Mehat3bb60202010-02-19 18:14:36 -080071
72/*
Kenny Root344ca102012-04-03 17:23:01 -070073 * Path to internal storage where *only* root can access ASEC image files
74 */
75const char *Volume::SEC_ASECDIR_INT = "/data/app-asec";
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -070076
Kenny Root344ca102012-04-03 17:23:01 -070077/*
San Mehat3bb60202010-02-19 18:14:36 -080078 * Path to where secure containers are mounted
79 */
80const char *Volume::ASECDIR = "/mnt/asec";
81
Kenny Rootfb7c4d52010-06-30 18:48:41 -070082/*
Kenny Root508c0e12010-07-12 09:59:49 -070083 * Path to where OBBs are mounted
Kenny Rootfb7c4d52010-06-30 18:48:41 -070084 */
Kenny Root508c0e12010-07-12 09:59:49 -070085const char *Volume::LOOPDIR = "/mnt/obb";
Kenny Rootfb7c4d52010-06-30 18:48:41 -070086
Jeff Sharkey0de365f2013-10-16 16:24:19 -070087const char *Volume::BLKID_PATH = "/system/bin/blkid";
88
San Mehata2677e42009-12-13 10:40:18 -080089static const char *stateToStr(int state) {
90 if (state == Volume::State_Init)
91 return "Initializing";
92 else if (state == Volume::State_NoMedia)
93 return "No-Media";
94 else if (state == Volume::State_Idle)
95 return "Idle-Unmounted";
96 else if (state == Volume::State_Pending)
97 return "Pending";
98 else if (state == Volume::State_Mounted)
99 return "Mounted";
100 else if (state == Volume::State_Unmounting)
101 return "Unmounting";
102 else if (state == Volume::State_Checking)
103 return "Checking";
104 else if (state == Volume::State_Formatting)
105 return "Formatting";
106 else if (state == Volume::State_Shared)
107 return "Shared-Unmounted";
108 else if (state == Volume::State_SharedMnt)
109 return "Shared-Mounted";
110 else
111 return "Unknown-Error";
112}
113
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700114Volume::Volume(VolumeManager *vm, const fstab_rec* rec, int flags) {
San Mehata2677e42009-12-13 10:40:18 -0800115 mVm = vm;
San Mehatd9a4e352010-03-12 13:32:47 -0800116 mDebug = false;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700117 mLabel = strdup(rec->label);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700118 mUuid = NULL;
119 mUserLabel = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700120 mState = Volume::State_Init;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700121 mFlags = flags;
San Mehata2677e42009-12-13 10:40:18 -0800122 mCurrentlyMountedKdev = -1;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700123 mPartIdx = rec->partnum;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700124 mRetryMount = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700125}
126
127Volume::~Volume() {
128 free(mLabel);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700129 free(mUuid);
130 free(mUserLabel);
San Mehatcb4dac82010-03-14 13:41:54 -0700131}
132
San Mehatd9a4e352010-03-12 13:32:47 -0800133void Volume::setDebug(bool enable) {
134 mDebug = enable;
135}
136
San Mehata2677e42009-12-13 10:40:18 -0800137dev_t Volume::getDiskDevice() {
138 return MKDEV(0, 0);
139};
140
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400141dev_t Volume::getShareDevice() {
142 return getDiskDevice();
143}
144
San Mehata2677e42009-12-13 10:40:18 -0800145void Volume::handleVolumeShared() {
146}
147
148void Volume::handleVolumeUnshared() {
149}
150
Mark Salyzyn5eecc442014-02-12 14:16:14 -0800151int Volume::handleBlockEvent(NetlinkEvent * /*evt*/) {
San Mehatf1b736b2009-10-10 17:22:08 -0700152 errno = ENOSYS;
153 return -1;
154}
155
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700156void Volume::setUuid(const char* uuid) {
157 char msg[256];
158
159 if (mUuid) {
160 free(mUuid);
161 }
162
163 if (uuid) {
164 mUuid = strdup(uuid);
165 snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(),
166 getFuseMountpoint(), mUuid);
167 } else {
168 mUuid = NULL;
169 snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint());
170 }
171
172 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUuidChange, msg,
173 false);
174}
175
176void Volume::setUserLabel(const char* userLabel) {
177 char msg[256];
178
179 if (mUserLabel) {
180 free(mUserLabel);
181 }
182
183 if (userLabel) {
184 mUserLabel = strdup(userLabel);
185 snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(),
186 getFuseMountpoint(), mUserLabel);
187 } else {
188 mUserLabel = NULL;
189 snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint());
190 }
191
192 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUserLabelChange,
193 msg, false);
194}
195
San Mehatf1b736b2009-10-10 17:22:08 -0700196void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800197 char msg[255];
198 int oldState = mState;
199
200 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700201 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800202 return;
203 }
204
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700205 if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) {
206 mRetryMount = false;
207 }
208
San Mehatf1b736b2009-10-10 17:22:08 -0700209 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800210
San Mehat97ac40e2010-03-24 10:24:19 -0700211 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800212 oldState, stateToStr(oldState), mState, stateToStr(mState));
213 snprintf(msg, sizeof(msg),
214 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700215 getFuseMountpoint(), oldState, stateToStr(oldState), mState,
San Mehata2677e42009-12-13 10:40:18 -0800216 stateToStr(mState));
217
218 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
219 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700220}
San Mehat49e2bce2009-10-12 16:29:01 -0700221
San Mehatdd9b8e92009-10-21 11:06:52 -0700222int Volume::createDeviceNode(const char *path, int major, int minor) {
Stephen Smalley684e6622014-09-30 10:29:24 -0400223 char *secontext = NULL;
San Mehatdd9b8e92009-10-21 11:06:52 -0700224 mode_t mode = 0660 | S_IFBLK;
225 dev_t dev = (major << 8) | minor;
Stephen Smalley684e6622014-09-30 10:29:24 -0400226 int rc;
227 if (sehandle) {
228 rc = selabel_lookup(sehandle, &secontext, path, S_IFBLK);
229 if (rc == 0)
230 setfscreatecon(secontext);
231 }
San Mehatdd9b8e92009-10-21 11:06:52 -0700232 if (mknod(path, mode, dev) < 0) {
233 if (errno != EEXIST) {
Stephen Smalley684e6622014-09-30 10:29:24 -0400234 int sverrno = errno;
235 if (secontext) {
236 freecon(secontext);
237 setfscreatecon(NULL);
238 }
239 errno = sverrno;
San Mehatdd9b8e92009-10-21 11:06:52 -0700240 return -1;
241 }
242 }
Stephen Smalley684e6622014-09-30 10:29:24 -0400243 if (secontext) {
244 setfscreatecon(NULL);
245 freecon(secontext);
246 }
San Mehatdd9b8e92009-10-21 11:06:52 -0700247 return 0;
248}
249
Ken Sumrall9caab762013-06-11 19:10:20 -0700250int Volume::formatVol(bool wipe) {
San Mehat49e2bce2009-10-12 16:29:01 -0700251
San Mehata2677e42009-12-13 10:40:18 -0800252 if (getState() == Volume::State_NoMedia) {
253 errno = ENODEV;
254 return -1;
255 } else if (getState() != Volume::State_Idle) {
256 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700257 return -1;
258 }
259
San Mehata2677e42009-12-13 10:40:18 -0800260 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700261 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800262 setState(Volume::State_Mounted);
263 // mCurrentlyMountedKdev = XXX
264 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700265 return -1;
266 }
267
Mike Lockwooda4886f12010-09-21 13:56:35 -0400268 bool formatEntireDevice = (mPartIdx == -1);
San Mehata2677e42009-12-13 10:40:18 -0800269 char devicePath[255];
270 dev_t diskNode = getDiskDevice();
Blair Prescottcc21d6e2013-10-21 15:25:37 -0700271 dev_t partNode =
272 MKDEV(MAJOR(diskNode),
Bogdan George Stefan1a89e8b2014-06-30 15:40:03 +0300273 MINOR(diskNode) + (formatEntireDevice ? 0 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800274
San Mehat2a5b8ce2010-03-10 12:48:57 -0800275 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800276
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800277 int ret = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400278 // Only initialize the MBR if we are formatting the entire device
279 if (formatEntireDevice) {
280 sprintf(devicePath, "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -0800281 major(diskNode), minor(diskNode));
Mike Lockwooda4886f12010-09-21 13:56:35 -0400282
283 if (initializeMbr(devicePath)) {
284 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
285 goto err;
286 }
San Mehat49e2bce2009-10-12 16:29:01 -0700287 }
288
San Mehata2677e42009-12-13 10:40:18 -0800289 sprintf(devicePath, "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -0800290 major(partNode), minor(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700291
Mike Lockwooda4886f12010-09-21 13:56:35 -0400292 if (mDebug) {
293 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
294 }
295
Ken Sumrall9caab762013-06-11 19:10:20 -0700296 if (Fat::format(devicePath, 0, wipe)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700297 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800298 goto err;
299 }
300
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800301 ret = 0;
302
San Mehata2677e42009-12-13 10:40:18 -0800303err:
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800304 setState(Volume::State_Idle);
305 return ret;
San Mehata2677e42009-12-13 10:40:18 -0800306}
307
308bool Volume::isMountpointMounted(const char *path) {
309 char device[256];
310 char mount_path[256];
311 char rest[256];
312 FILE *fp;
313 char line[1024];
314
315 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700316 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800317 return false;
318 }
319
320 while(fgets(line, sizeof(line), fp)) {
321 line[strlen(line)-1] = '\0';
322 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
323 if (!strcmp(mount_path, path)) {
324 fclose(fp);
325 return true;
326 }
San Mehata2677e42009-12-13 10:40:18 -0800327 }
328
329 fclose(fp);
330 return false;
331}
332
333int Volume::mountVol() {
334 dev_t deviceNodes[4];
Tim Murray8439dc92014-12-15 11:56:11 -0800335 int n, i;
San Mehata2677e42009-12-13 10:40:18 -0800336 char errmsg[255];
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700337
338 int flags = getFlags();
339 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
340
341 // TODO: handle "bind" style mounts, for emulated storage
342
Ken Sumrall29d8da82011-05-18 17:20:07 -0700343 char decrypt_state[PROPERTY_VALUE_MAX];
344 char crypto_state[PROPERTY_VALUE_MAX];
345 char encrypt_progress[PROPERTY_VALUE_MAX];
San Mehata2677e42009-12-13 10:40:18 -0800346
Ken Sumrall29d8da82011-05-18 17:20:07 -0700347 property_get("vold.decrypt", decrypt_state, "");
348 property_get("vold.encrypt_progress", encrypt_progress, "");
349
350 /* Don't try to mount the volumes if we have not yet entered the disk password
351 * or are in the process of encrypting.
352 */
353 if ((getState() == Volume::State_NoMedia) ||
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700354 ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) {
San Mehata2677e42009-12-13 10:40:18 -0800355 snprintf(errmsg, sizeof(errmsg),
356 "Volume %s %s mount failed - no media",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700357 getLabel(), getFuseMountpoint());
San Mehata2677e42009-12-13 10:40:18 -0800358 mVm->getBroadcaster()->sendBroadcast(
359 ResponseCode::VolumeMountFailedNoMedia,
360 errmsg, false);
361 errno = ENODEV;
362 return -1;
363 } else if (getState() != Volume::State_Idle) {
364 errno = EBUSY;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700365 if (getState() == Volume::State_Pending) {
366 mRetryMount = true;
367 }
San Mehata2677e42009-12-13 10:40:18 -0800368 return -1;
369 }
370
371 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700372 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800373 setState(Volume::State_Mounted);
374 // mCurrentlyMountedKdev = XXX
375 return 0;
376 }
377
378 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
379 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700380 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800381 return -1;
382 }
383
Ken Sumrall29d8da82011-05-18 17:20:07 -0700384 /* If we're running encrypted, and the volume is marked as encryptable and nonremovable,
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700385 * and also marked as providing Asec storage, then we need to decrypt
Ken Sumrall29d8da82011-05-18 17:20:07 -0700386 * that partition, and update the volume object to point to it's new decrypted
387 * block device
388 */
389 property_get("ro.crypto.state", crypto_state, "");
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700390 if (providesAsec &&
Ken Sumrall29d8da82011-05-18 17:20:07 -0700391 ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) &&
392 !strcmp(crypto_state, "encrypted") && !isDecrypted()) {
393 char new_sys_path[MAXPATHLEN];
394 char nodepath[256];
395 int new_major, new_minor;
396
397 if (n != 1) {
398 /* We only expect one device node returned when mounting encryptable volumes */
Colin Cross59846b62014-02-06 20:34:29 -0800399 SLOGE("Too many device nodes returned when mounting %s\n", getMountpoint());
Ken Sumrall29d8da82011-05-18 17:20:07 -0700400 return -1;
401 }
402
403 if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]),
404 new_sys_path, sizeof(new_sys_path),
405 &new_major, &new_minor)) {
Colin Cross59846b62014-02-06 20:34:29 -0800406 SLOGE("Cannot setup encryption mapping for %s\n", getMountpoint());
Ken Sumrall29d8da82011-05-18 17:20:07 -0700407 return -1;
408 }
409 /* We now have the new sysfs path for the decrypted block device, and the
410 * majore and minor numbers for it. So, create the device, update the
411 * path to the new sysfs path, and continue.
412 */
413 snprintf(nodepath,
414 sizeof(nodepath), "/dev/block/vold/%d:%d",
415 new_major, new_minor);
416 if (createDeviceNode(nodepath, new_major, new_minor)) {
417 SLOGE("Error making device node '%s' (%s)", nodepath,
418 strerror(errno));
419 }
420
421 // Todo: Either create sys filename from nodepath, or pass in bogus path so
422 // vold ignores state changes on this internal device.
423 updateDeviceInfo(nodepath, new_major, new_minor);
424
425 /* Get the device nodes again, because they just changed */
426 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
427 if (!n) {
428 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
429 return -1;
430 }
431 }
432
San Mehata2677e42009-12-13 10:40:18 -0800433 for (i = 0; i < n; i++) {
434 char devicePath[255];
435
Colin Cross346c5b22014-01-22 23:59:41 -0800436 sprintf(devicePath, "/dev/block/vold/%d:%d", major(deviceNodes[i]),
437 minor(deviceNodes[i]));
San Mehata2677e42009-12-13 10:40:18 -0800438
San Mehat97ac40e2010-03-24 10:24:19 -0700439 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800440
441 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800442 setState(Volume::State_Checking);
443
San Mehat3bb60202010-02-19 18:14:36 -0800444 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800445 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700446 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800447 continue;
San Mehata2677e42009-12-13 10:40:18 -0800448 }
San Mehateba65e92010-01-29 05:15:16 -0800449 errno = EIO;
450 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700451 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800452 setState(Volume::State_Idle);
453 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800454 }
455
San Mehata2677e42009-12-13 10:40:18 -0800456 errno = 0;
Mike Lockwood9092b1d2011-03-23 14:55:49 -0400457
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700458 if (Fat::doMount(devicePath, getMountpoint(), false, false, false,
459 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700460 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800461 continue;
San Mehata2677e42009-12-13 10:40:18 -0800462 }
463
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700464 extractMetadata(devicePath);
San Mehatcb4dac82010-03-14 13:41:54 -0700465
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700466 if (providesAsec && mountAsecExternal() != 0) {
467 SLOGE("Failed to mount secure area (%s)", strerror(errno));
468 umount(getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800469 setState(Volume::State_Idle);
470 return -1;
471 }
472
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700473 char service[64];
474 snprintf(service, 64, "fuse_%s", getLabel());
475 property_set("ctl.start", service);
476
San Mehat3bb60202010-02-19 18:14:36 -0800477 setState(Volume::State_Mounted);
478 mCurrentlyMountedKdev = deviceNodes[i];
479 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800480 }
481
San Mehat97ac40e2010-03-24 10:24:19 -0700482 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800483 setState(Volume::State_Idle);
484
San Mehateba65e92010-01-29 05:15:16 -0800485 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800486}
487
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700488int Volume::mountAsecExternal() {
489 char legacy_path[PATH_MAX];
490 char secure_path[PATH_MAX];
San Mehat3bb60202010-02-19 18:14:36 -0800491
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700492 snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint());
493 snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint());
494
495 // Recover legacy secure path
496 if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) {
497 if (rename(legacy_path, secure_path)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700498 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800499 }
500 }
501
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700502 if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700503 SLOGW("fs_prepare_dir failed: %s", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800504 return -1;
505 }
506
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700507 if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) {
508 SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path,
509 SEC_ASECDIR_EXT, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800510 return -1;
511 }
512
513 return 0;
514}
515
San Mehat3bb60202010-02-19 18:14:36 -0800516int Volume::doUnmount(const char *path, bool force) {
517 int retries = 10;
518
San Mehatd9a4e352010-03-12 13:32:47 -0800519 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700520 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800521 }
522
San Mehat3bb60202010-02-19 18:14:36 -0800523 while (retries--) {
524 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700525 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800526 return 0;
527 }
528
529 int action = 0;
530
531 if (force) {
532 if (retries == 1) {
533 action = 2; // SIGKILL
534 } else if (retries == 2) {
535 action = 1; // SIGHUP
536 }
537 }
538
San Mehat97ac40e2010-03-24 10:24:19 -0700539 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800540 path, strerror(errno), retries, action);
541
542 Process::killProcessesWithOpenFiles(path, action);
543 usleep(1000*1000);
544 }
545 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700546 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800547 return -1;
548}
549
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700550int Volume::unmountVol(bool force, bool revert) {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700551 int flags = getFlags();
552 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
553
San Mehata2677e42009-12-13 10:40:18 -0800554 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700555 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800556 errno = EINVAL;
Ken Sumrall319b1042011-06-14 14:01:55 -0700557 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehata2677e42009-12-13 10:40:18 -0800558 }
559
560 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800561 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800562
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700563 char service[64];
564 snprintf(service, 64, "fuse_%s", getLabel());
565 property_set("ctl.stop", service);
566 /* Give it a chance to stop. I wish we had a synchronous way to determine this... */
567 sleep(1);
568
569 // TODO: determine failure mode if FUSE times out
570
571 if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) {
572 SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno));
573 goto out_mounted;
San Mehat3bb60202010-02-19 18:14:36 -0800574 }
575
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700576 /* Now that the fuse daemon is dead, unmount it */
577 if (doUnmount(getFuseMountpoint(), force) != 0) {
578 SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno));
579 goto fail_remount_secure;
Jeff Sharkey7a3c3d42012-10-04 16:49:22 -0700580 }
581
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700582 /* Unmount the real sd card */
583 if (doUnmount(getMountpoint(), force) != 0) {
584 SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno));
585 goto fail_remount_secure;
San Mehat3bb60202010-02-19 18:14:36 -0800586 }
587
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700588 SLOGI("%s unmounted successfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800589
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700590 /* If this is an encrypted volume, and we've been asked to undo
591 * the crypto mapping, then revert the dm-crypt mapping, and revert
592 * the device info to the original values.
593 */
594 if (revert && isDecrypted()) {
595 cryptfs_revert_volume(getLabel());
596 revertDeviceInfo();
597 SLOGI("Encrypted volume %s reverted successfully", getMountpoint());
598 }
599
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700600 setUuid(NULL);
601 setUserLabel(NULL);
San Mehat3bb60202010-02-19 18:14:36 -0800602 setState(Volume::State_Idle);
603 mCurrentlyMountedKdev = -1;
604 return 0;
605
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700606fail_remount_secure:
607 if (providesAsec && mountAsecExternal() != 0) {
608 SLOGE("Failed to remount secure area (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800609 goto out_nomedia;
610 }
611
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700612out_mounted:
San Mehata2677e42009-12-13 10:40:18 -0800613 setState(Volume::State_Mounted);
614 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800615
616out_nomedia:
617 setState(Volume::State_NoMedia);
618 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800619}
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700620
San Mehata2677e42009-12-13 10:40:18 -0800621int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800622 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800623
San Mehat2a5b8ce2010-03-10 12:48:57 -0800624 memset(&dinfo, 0, sizeof(dinfo));
625
626 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700627 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800628 return -1;
629 }
630
San Mehat2a5b8ce2010-03-10 12:48:57 -0800631 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
632 dinfo.device = strdup(deviceNode);
633 dinfo.scheme = PART_SCHEME_MBR;
634 dinfo.sect_size = 512;
635 dinfo.skip_lba = 2048;
636 dinfo.num_lba = 0;
637 dinfo.num_parts = 1;
638
639 struct part_info *pinfo = &dinfo.part_lst[0];
640
641 pinfo->name = strdup("android_sdcard");
642 pinfo->flags |= PART_ACTIVE_FLAG;
643 pinfo->type = PC_PART_TYPE_FAT32;
644 pinfo->len_kb = -1;
645
646 int rc = apply_disk_config(&dinfo, 0);
647
648 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700649 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800650 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800651 }
652
San Mehat2a5b8ce2010-03-10 12:48:57 -0800653 out:
654 free(pinfo->name);
655 free(dinfo.device);
656 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800657
San Mehat2a5b8ce2010-03-10 12:48:57 -0800658 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800659}
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700660
661/*
662 * Use blkid to extract UUID and label from device, since it handles many
663 * obscure edge cases around partition types and formats. Always broadcasts
664 * updated metadata values.
665 */
666int Volume::extractMetadata(const char* devicePath) {
667 int res = 0;
668
669 std::string cmd;
670 cmd = BLKID_PATH;
671 cmd += " -c /dev/null ";
672 cmd += devicePath;
673
674 FILE* fp = popen(cmd.c_str(), "r");
675 if (!fp) {
676 ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno));
677 res = -1;
678 goto done;
679 }
680
681 char line[1024];
682 char value[128];
683 if (fgets(line, sizeof(line), fp) != NULL) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700684 ALOGD("blkid identified as %s", line);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700685
Jeff Sharkey6bcd3622013-11-11 14:46:39 -0800686 char* start = strstr(line, "UUID=");
687 if (start != NULL && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700688 setUuid(value);
689 } else {
690 setUuid(NULL);
691 }
692
Jeff Sharkey6bcd3622013-11-11 14:46:39 -0800693 start = strstr(line, "LABEL=");
694 if (start != NULL && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700695 setUserLabel(value);
696 } else {
697 setUserLabel(NULL);
698 }
699 } else {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700700 ALOGW("blkid failed to identify %s", devicePath);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700701 res = -1;
702 }
703
704 pclose(fp);
705
706done:
707 if (res == -1) {
708 setUuid(NULL);
709 setUserLabel(NULL);
710 }
711 return res;
712}