blob: bfad29d878bb552cc7573bfc7f30eb65db910627 [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];
335 int n, i, rc = 0;
336 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 int gid;
458
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700459 if (Fat::doMount(devicePath, getMountpoint(), false, false, false,
460 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700461 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800462 continue;
San Mehata2677e42009-12-13 10:40:18 -0800463 }
464
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700465 extractMetadata(devicePath);
San Mehatcb4dac82010-03-14 13:41:54 -0700466
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700467 if (providesAsec && mountAsecExternal() != 0) {
468 SLOGE("Failed to mount secure area (%s)", strerror(errno));
469 umount(getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800470 setState(Volume::State_Idle);
471 return -1;
472 }
473
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700474 char service[64];
475 snprintf(service, 64, "fuse_%s", getLabel());
476 property_set("ctl.start", service);
477
San Mehat3bb60202010-02-19 18:14:36 -0800478 setState(Volume::State_Mounted);
479 mCurrentlyMountedKdev = deviceNodes[i];
480 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800481 }
482
San Mehat97ac40e2010-03-24 10:24:19 -0700483 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800484 setState(Volume::State_Idle);
485
San Mehateba65e92010-01-29 05:15:16 -0800486 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800487}
488
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700489int Volume::mountAsecExternal() {
490 char legacy_path[PATH_MAX];
491 char secure_path[PATH_MAX];
San Mehat3bb60202010-02-19 18:14:36 -0800492
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700493 snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint());
494 snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint());
495
496 // Recover legacy secure path
497 if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) {
498 if (rename(legacy_path, secure_path)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700499 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800500 }
501 }
502
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700503 if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700504 SLOGW("fs_prepare_dir failed: %s", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800505 return -1;
506 }
507
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700508 if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) {
509 SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path,
510 SEC_ASECDIR_EXT, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800511 return -1;
512 }
513
514 return 0;
515}
516
San Mehat3bb60202010-02-19 18:14:36 -0800517int Volume::doUnmount(const char *path, bool force) {
518 int retries = 10;
519
San Mehatd9a4e352010-03-12 13:32:47 -0800520 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700521 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800522 }
523
San Mehat3bb60202010-02-19 18:14:36 -0800524 while (retries--) {
525 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700526 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800527 return 0;
528 }
529
530 int action = 0;
531
532 if (force) {
533 if (retries == 1) {
534 action = 2; // SIGKILL
535 } else if (retries == 2) {
536 action = 1; // SIGHUP
537 }
538 }
539
San Mehat97ac40e2010-03-24 10:24:19 -0700540 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800541 path, strerror(errno), retries, action);
542
543 Process::killProcessesWithOpenFiles(path, action);
544 usleep(1000*1000);
545 }
546 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700547 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800548 return -1;
549}
550
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700551int Volume::unmountVol(bool force, bool revert) {
San Mehata2677e42009-12-13 10:40:18 -0800552 int i, rc;
553
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700554 int flags = getFlags();
555 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
556
San Mehata2677e42009-12-13 10:40:18 -0800557 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700558 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800559 errno = EINVAL;
Ken Sumrall319b1042011-06-14 14:01:55 -0700560 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehata2677e42009-12-13 10:40:18 -0800561 }
562
563 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800564 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800565
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700566 char service[64];
567 snprintf(service, 64, "fuse_%s", getLabel());
568 property_set("ctl.stop", service);
569 /* Give it a chance to stop. I wish we had a synchronous way to determine this... */
570 sleep(1);
571
572 // TODO: determine failure mode if FUSE times out
573
574 if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) {
575 SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno));
576 goto out_mounted;
San Mehat3bb60202010-02-19 18:14:36 -0800577 }
578
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700579 /* Now that the fuse daemon is dead, unmount it */
580 if (doUnmount(getFuseMountpoint(), force) != 0) {
581 SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno));
582 goto fail_remount_secure;
Jeff Sharkey7a3c3d42012-10-04 16:49:22 -0700583 }
584
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700585 /* Unmount the real sd card */
586 if (doUnmount(getMountpoint(), force) != 0) {
587 SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno));
588 goto fail_remount_secure;
San Mehat3bb60202010-02-19 18:14:36 -0800589 }
590
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700591 SLOGI("%s unmounted successfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800592
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700593 /* If this is an encrypted volume, and we've been asked to undo
594 * the crypto mapping, then revert the dm-crypt mapping, and revert
595 * the device info to the original values.
596 */
597 if (revert && isDecrypted()) {
598 cryptfs_revert_volume(getLabel());
599 revertDeviceInfo();
600 SLOGI("Encrypted volume %s reverted successfully", getMountpoint());
601 }
602
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700603 setUuid(NULL);
604 setUserLabel(NULL);
San Mehat3bb60202010-02-19 18:14:36 -0800605 setState(Volume::State_Idle);
606 mCurrentlyMountedKdev = -1;
607 return 0;
608
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700609fail_remount_secure:
610 if (providesAsec && mountAsecExternal() != 0) {
611 SLOGE("Failed to remount secure area (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800612 goto out_nomedia;
613 }
614
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700615out_mounted:
San Mehata2677e42009-12-13 10:40:18 -0800616 setState(Volume::State_Mounted);
617 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800618
619out_nomedia:
620 setState(Volume::State_NoMedia);
621 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800622}
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700623
San Mehata2677e42009-12-13 10:40:18 -0800624int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800625 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800626
San Mehat2a5b8ce2010-03-10 12:48:57 -0800627 memset(&dinfo, 0, sizeof(dinfo));
628
629 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700630 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800631 return -1;
632 }
633
San Mehat2a5b8ce2010-03-10 12:48:57 -0800634 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
635 dinfo.device = strdup(deviceNode);
636 dinfo.scheme = PART_SCHEME_MBR;
637 dinfo.sect_size = 512;
638 dinfo.skip_lba = 2048;
639 dinfo.num_lba = 0;
640 dinfo.num_parts = 1;
641
642 struct part_info *pinfo = &dinfo.part_lst[0];
643
644 pinfo->name = strdup("android_sdcard");
645 pinfo->flags |= PART_ACTIVE_FLAG;
646 pinfo->type = PC_PART_TYPE_FAT32;
647 pinfo->len_kb = -1;
648
649 int rc = apply_disk_config(&dinfo, 0);
650
651 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700652 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800653 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800654 }
655
San Mehat2a5b8ce2010-03-10 12:48:57 -0800656 out:
657 free(pinfo->name);
658 free(dinfo.device);
659 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800660
San Mehat2a5b8ce2010-03-10 12:48:57 -0800661 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800662}
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700663
664/*
665 * Use blkid to extract UUID and label from device, since it handles many
666 * obscure edge cases around partition types and formats. Always broadcasts
667 * updated metadata values.
668 */
669int Volume::extractMetadata(const char* devicePath) {
670 int res = 0;
671
672 std::string cmd;
673 cmd = BLKID_PATH;
674 cmd += " -c /dev/null ";
675 cmd += devicePath;
676
677 FILE* fp = popen(cmd.c_str(), "r");
678 if (!fp) {
679 ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno));
680 res = -1;
681 goto done;
682 }
683
684 char line[1024];
685 char value[128];
686 if (fgets(line, sizeof(line), fp) != NULL) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700687 ALOGD("blkid identified as %s", line);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700688
Jeff Sharkey6bcd3622013-11-11 14:46:39 -0800689 char* start = strstr(line, "UUID=");
690 if (start != NULL && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700691 setUuid(value);
692 } else {
693 setUuid(NULL);
694 }
695
Jeff Sharkey6bcd3622013-11-11 14:46:39 -0800696 start = strstr(line, "LABEL=");
697 if (start != NULL && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700698 setUserLabel(value);
699 } else {
700 setUserLabel(NULL);
701 }
702 } else {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700703 ALOGW("blkid failed to identify %s", devicePath);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700704 res = -1;
705 }
706
707 pclose(fp);
708
709done:
710 if (res == -1) {
711 setUuid(NULL);
712 setUserLabel(NULL);
713 }
714 return res;
715}