blob: ca56d1cf245fe3b49998fdaae9ab7c7adcfa665c [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";
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -070075
Kenny Root344ca102012-04-03 17:23:01 -070076/*
San Mehat3bb60202010-02-19 18:14:36 -080077 * Path to where secure containers are mounted
78 */
79const char *Volume::ASECDIR = "/mnt/asec";
80
Kenny Rootfb7c4d52010-06-30 18:48:41 -070081/*
Kenny Root508c0e12010-07-12 09:59:49 -070082 * Path to where OBBs are mounted
Kenny Rootfb7c4d52010-06-30 18:48:41 -070083 */
Kenny Root508c0e12010-07-12 09:59:49 -070084const char *Volume::LOOPDIR = "/mnt/obb";
Kenny Rootfb7c4d52010-06-30 18:48:41 -070085
Jeff Sharkey0de365f2013-10-16 16:24:19 -070086const char *Volume::BLKID_PATH = "/system/bin/blkid";
87
San Mehata2677e42009-12-13 10:40:18 -080088static const char *stateToStr(int state) {
89 if (state == Volume::State_Init)
90 return "Initializing";
91 else if (state == Volume::State_NoMedia)
92 return "No-Media";
93 else if (state == Volume::State_Idle)
94 return "Idle-Unmounted";
95 else if (state == Volume::State_Pending)
96 return "Pending";
97 else if (state == Volume::State_Mounted)
98 return "Mounted";
99 else if (state == Volume::State_Unmounting)
100 return "Unmounting";
101 else if (state == Volume::State_Checking)
102 return "Checking";
103 else if (state == Volume::State_Formatting)
104 return "Formatting";
105 else if (state == Volume::State_Shared)
106 return "Shared-Unmounted";
107 else if (state == Volume::State_SharedMnt)
108 return "Shared-Mounted";
109 else
110 return "Unknown-Error";
111}
112
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700113Volume::Volume(VolumeManager *vm, const fstab_rec* rec, int flags) {
San Mehata2677e42009-12-13 10:40:18 -0800114 mVm = vm;
San Mehatd9a4e352010-03-12 13:32:47 -0800115 mDebug = false;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700116 mLabel = strdup(rec->label);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700117 mUuid = NULL;
118 mUserLabel = NULL;
San Mehatf1b736b2009-10-10 17:22:08 -0700119 mState = Volume::State_Init;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700120 mFlags = flags;
San Mehata2677e42009-12-13 10:40:18 -0800121 mCurrentlyMountedKdev = -1;
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700122 mPartIdx = rec->partnum;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700123 mRetryMount = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700124}
125
126Volume::~Volume() {
127 free(mLabel);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700128 free(mUuid);
129 free(mUserLabel);
San Mehatcb4dac82010-03-14 13:41:54 -0700130}
131
San Mehatd9a4e352010-03-12 13:32:47 -0800132void Volume::setDebug(bool enable) {
133 mDebug = enable;
134}
135
San Mehata2677e42009-12-13 10:40:18 -0800136dev_t Volume::getDiskDevice() {
137 return MKDEV(0, 0);
138};
139
Mike Lockwood2dfe2972010-09-17 18:50:51 -0400140dev_t Volume::getShareDevice() {
141 return getDiskDevice();
142}
143
San Mehata2677e42009-12-13 10:40:18 -0800144void Volume::handleVolumeShared() {
145}
146
147void Volume::handleVolumeUnshared() {
148}
149
Mark Salyzyn3e971272014-01-21 13:27:04 -0800150int Volume::handleBlockEvent(NetlinkEvent * /*evt*/) {
San Mehatf1b736b2009-10-10 17:22:08 -0700151 errno = ENOSYS;
152 return -1;
153}
154
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700155void Volume::setUuid(const char* uuid) {
156 char msg[256];
157
158 if (mUuid) {
159 free(mUuid);
160 }
161
162 if (uuid) {
163 mUuid = strdup(uuid);
164 snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(),
165 getFuseMountpoint(), mUuid);
166 } else {
167 mUuid = NULL;
168 snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint());
169 }
170
171 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUuidChange, msg,
172 false);
173}
174
175void Volume::setUserLabel(const char* userLabel) {
176 char msg[256];
177
178 if (mUserLabel) {
179 free(mUserLabel);
180 }
181
182 if (userLabel) {
183 mUserLabel = strdup(userLabel);
184 snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(),
185 getFuseMountpoint(), mUserLabel);
186 } else {
187 mUserLabel = NULL;
188 snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint());
189 }
190
191 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUserLabelChange,
192 msg, false);
193}
194
San Mehatf1b736b2009-10-10 17:22:08 -0700195void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800196 char msg[255];
197 int oldState = mState;
198
199 if (oldState == state) {
San Mehat97ac40e2010-03-24 10:24:19 -0700200 SLOGW("Duplicate state (%d)\n", state);
San Mehata2677e42009-12-13 10:40:18 -0800201 return;
202 }
203
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700204 if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) {
205 mRetryMount = false;
206 }
207
San Mehatf1b736b2009-10-10 17:22:08 -0700208 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800209
San Mehat97ac40e2010-03-24 10:24:19 -0700210 SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
San Mehata2677e42009-12-13 10:40:18 -0800211 oldState, stateToStr(oldState), mState, stateToStr(mState));
212 snprintf(msg, sizeof(msg),
213 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700214 getFuseMountpoint(), oldState, stateToStr(oldState), mState,
San Mehata2677e42009-12-13 10:40:18 -0800215 stateToStr(mState));
216
217 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
218 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700219}
San Mehat49e2bce2009-10-12 16:29:01 -0700220
San Mehatdd9b8e92009-10-21 11:06:52 -0700221int Volume::createDeviceNode(const char *path, int major, int minor) {
222 mode_t mode = 0660 | S_IFBLK;
223 dev_t dev = (major << 8) | minor;
224 if (mknod(path, mode, dev) < 0) {
225 if (errno != EEXIST) {
226 return -1;
227 }
228 }
229 return 0;
230}
231
Ken Sumrall9caab762013-06-11 19:10:20 -0700232int Volume::formatVol(bool wipe) {
San Mehat49e2bce2009-10-12 16:29:01 -0700233
San Mehata2677e42009-12-13 10:40:18 -0800234 if (getState() == Volume::State_NoMedia) {
235 errno = ENODEV;
236 return -1;
237 } else if (getState() != Volume::State_Idle) {
238 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700239 return -1;
240 }
241
San Mehata2677e42009-12-13 10:40:18 -0800242 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700243 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800244 setState(Volume::State_Mounted);
245 // mCurrentlyMountedKdev = XXX
246 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700247 return -1;
248 }
249
Mike Lockwooda4886f12010-09-21 13:56:35 -0400250 bool formatEntireDevice = (mPartIdx == -1);
San Mehata2677e42009-12-13 10:40:18 -0800251 char devicePath[255];
252 dev_t diskNode = getDiskDevice();
Blair Prescottcc21d6e2013-10-21 15:25:37 -0700253 dev_t partNode =
254 MKDEV(MAJOR(diskNode),
255 MINOR(diskNode) + (formatEntireDevice ? 1 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800256
San Mehat2a5b8ce2010-03-10 12:48:57 -0800257 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800258
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800259 int ret = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400260 // Only initialize the MBR if we are formatting the entire device
261 if (formatEntireDevice) {
262 sprintf(devicePath, "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -0800263 major(diskNode), minor(diskNode));
Mike Lockwooda4886f12010-09-21 13:56:35 -0400264
265 if (initializeMbr(devicePath)) {
266 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
267 goto err;
268 }
San Mehat49e2bce2009-10-12 16:29:01 -0700269 }
270
San Mehata2677e42009-12-13 10:40:18 -0800271 sprintf(devicePath, "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -0800272 major(partNode), minor(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700273
Mike Lockwooda4886f12010-09-21 13:56:35 -0400274 if (mDebug) {
275 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
276 }
277
Ken Sumrall9caab762013-06-11 19:10:20 -0700278 if (Fat::format(devicePath, 0, wipe)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700279 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800280 goto err;
281 }
282
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800283 ret = 0;
284
San Mehata2677e42009-12-13 10:40:18 -0800285err:
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800286 setState(Volume::State_Idle);
287 return ret;
San Mehata2677e42009-12-13 10:40:18 -0800288}
289
290bool Volume::isMountpointMounted(const char *path) {
291 char device[256];
292 char mount_path[256];
293 char rest[256];
294 FILE *fp;
295 char line[1024];
296
297 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700298 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800299 return false;
300 }
301
302 while(fgets(line, sizeof(line), fp)) {
303 line[strlen(line)-1] = '\0';
304 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
305 if (!strcmp(mount_path, path)) {
306 fclose(fp);
307 return true;
308 }
San Mehata2677e42009-12-13 10:40:18 -0800309 }
310
311 fclose(fp);
312 return false;
313}
314
315int Volume::mountVol() {
316 dev_t deviceNodes[4];
317 int n, i, rc = 0;
318 char errmsg[255];
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700319
320 int flags = getFlags();
321 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
322
323 // TODO: handle "bind" style mounts, for emulated storage
324
Ken Sumrall29d8da82011-05-18 17:20:07 -0700325 char decrypt_state[PROPERTY_VALUE_MAX];
326 char crypto_state[PROPERTY_VALUE_MAX];
327 char encrypt_progress[PROPERTY_VALUE_MAX];
San Mehata2677e42009-12-13 10:40:18 -0800328
Ken Sumrall29d8da82011-05-18 17:20:07 -0700329 property_get("vold.decrypt", decrypt_state, "");
330 property_get("vold.encrypt_progress", encrypt_progress, "");
331
332 /* Don't try to mount the volumes if we have not yet entered the disk password
333 * or are in the process of encrypting.
334 */
335 if ((getState() == Volume::State_NoMedia) ||
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700336 ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) {
San Mehata2677e42009-12-13 10:40:18 -0800337 snprintf(errmsg, sizeof(errmsg),
338 "Volume %s %s mount failed - no media",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700339 getLabel(), getFuseMountpoint());
San Mehata2677e42009-12-13 10:40:18 -0800340 mVm->getBroadcaster()->sendBroadcast(
341 ResponseCode::VolumeMountFailedNoMedia,
342 errmsg, false);
343 errno = ENODEV;
344 return -1;
345 } else if (getState() != Volume::State_Idle) {
346 errno = EBUSY;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700347 if (getState() == Volume::State_Pending) {
348 mRetryMount = true;
349 }
San Mehata2677e42009-12-13 10:40:18 -0800350 return -1;
351 }
352
353 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700354 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800355 setState(Volume::State_Mounted);
356 // mCurrentlyMountedKdev = XXX
357 return 0;
358 }
359
360 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
361 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700362 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800363 return -1;
364 }
365
Ken Sumrall29d8da82011-05-18 17:20:07 -0700366 /* If we're running encrypted, and the volume is marked as encryptable and nonremovable,
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700367 * and also marked as providing Asec storage, then we need to decrypt
Ken Sumrall29d8da82011-05-18 17:20:07 -0700368 * that partition, and update the volume object to point to it's new decrypted
369 * block device
370 */
371 property_get("ro.crypto.state", crypto_state, "");
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700372 if (providesAsec &&
Ken Sumrall29d8da82011-05-18 17:20:07 -0700373 ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) &&
374 !strcmp(crypto_state, "encrypted") && !isDecrypted()) {
375 char new_sys_path[MAXPATHLEN];
376 char nodepath[256];
377 int new_major, new_minor;
378
379 if (n != 1) {
380 /* We only expect one device node returned when mounting encryptable volumes */
Colin Cross59846b62014-02-06 20:34:29 -0800381 SLOGE("Too many device nodes returned when mounting %s\n", getMountpoint());
Ken Sumrall29d8da82011-05-18 17:20:07 -0700382 return -1;
383 }
384
385 if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]),
386 new_sys_path, sizeof(new_sys_path),
387 &new_major, &new_minor)) {
Colin Cross59846b62014-02-06 20:34:29 -0800388 SLOGE("Cannot setup encryption mapping for %s\n", getMountpoint());
Ken Sumrall29d8da82011-05-18 17:20:07 -0700389 return -1;
390 }
391 /* We now have the new sysfs path for the decrypted block device, and the
392 * majore and minor numbers for it. So, create the device, update the
393 * path to the new sysfs path, and continue.
394 */
395 snprintf(nodepath,
396 sizeof(nodepath), "/dev/block/vold/%d:%d",
397 new_major, new_minor);
398 if (createDeviceNode(nodepath, new_major, new_minor)) {
399 SLOGE("Error making device node '%s' (%s)", nodepath,
400 strerror(errno));
401 }
402
403 // Todo: Either create sys filename from nodepath, or pass in bogus path so
404 // vold ignores state changes on this internal device.
405 updateDeviceInfo(nodepath, new_major, new_minor);
406
407 /* Get the device nodes again, because they just changed */
408 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
409 if (!n) {
410 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
411 return -1;
412 }
413 }
414
San Mehata2677e42009-12-13 10:40:18 -0800415 for (i = 0; i < n; i++) {
416 char devicePath[255];
417
Colin Cross346c5b22014-01-22 23:59:41 -0800418 sprintf(devicePath, "/dev/block/vold/%d:%d", major(deviceNodes[i]),
419 minor(deviceNodes[i]));
San Mehata2677e42009-12-13 10:40:18 -0800420
San Mehat97ac40e2010-03-24 10:24:19 -0700421 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800422
423 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800424 setState(Volume::State_Checking);
425
San Mehat3bb60202010-02-19 18:14:36 -0800426 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800427 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700428 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800429 continue;
San Mehata2677e42009-12-13 10:40:18 -0800430 }
San Mehateba65e92010-01-29 05:15:16 -0800431 errno = EIO;
432 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700433 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800434 setState(Volume::State_Idle);
435 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800436 }
437
San Mehata2677e42009-12-13 10:40:18 -0800438 errno = 0;
Mike Lockwood9092b1d2011-03-23 14:55:49 -0400439 int gid;
440
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700441 if (Fat::doMount(devicePath, getMountpoint(), false, false, false,
442 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700443 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800444 continue;
San Mehata2677e42009-12-13 10:40:18 -0800445 }
446
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700447 extractMetadata(devicePath);
San Mehatcb4dac82010-03-14 13:41:54 -0700448
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700449 if (providesAsec && mountAsecExternal() != 0) {
450 SLOGE("Failed to mount secure area (%s)", strerror(errno));
451 umount(getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800452 setState(Volume::State_Idle);
453 return -1;
454 }
455
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700456 char service[64];
457 snprintf(service, 64, "fuse_%s", getLabel());
458 property_set("ctl.start", service);
459
San Mehat3bb60202010-02-19 18:14:36 -0800460 setState(Volume::State_Mounted);
461 mCurrentlyMountedKdev = deviceNodes[i];
462 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800463 }
464
San Mehat97ac40e2010-03-24 10:24:19 -0700465 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800466 setState(Volume::State_Idle);
467
San Mehateba65e92010-01-29 05:15:16 -0800468 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800469}
470
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700471int Volume::mountAsecExternal() {
472 char legacy_path[PATH_MAX];
473 char secure_path[PATH_MAX];
San Mehat3bb60202010-02-19 18:14:36 -0800474
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700475 snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint());
476 snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint());
477
478 // Recover legacy secure path
479 if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) {
480 if (rename(legacy_path, secure_path)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700481 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800482 }
483 }
484
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700485 if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700486 SLOGW("fs_prepare_dir failed: %s", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800487 return -1;
488 }
489
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700490 if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) {
491 SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path,
492 SEC_ASECDIR_EXT, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800493 return -1;
494 }
495
496 return 0;
497}
498
San Mehat3bb60202010-02-19 18:14:36 -0800499int Volume::doUnmount(const char *path, bool force) {
500 int retries = 10;
501
San Mehatd9a4e352010-03-12 13:32:47 -0800502 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700503 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800504 }
505
San Mehat3bb60202010-02-19 18:14:36 -0800506 while (retries--) {
507 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700508 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800509 return 0;
510 }
511
512 int action = 0;
513
514 if (force) {
515 if (retries == 1) {
516 action = 2; // SIGKILL
517 } else if (retries == 2) {
518 action = 1; // SIGHUP
519 }
520 }
521
San Mehat97ac40e2010-03-24 10:24:19 -0700522 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800523 path, strerror(errno), retries, action);
524
525 Process::killProcessesWithOpenFiles(path, action);
526 usleep(1000*1000);
527 }
528 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700529 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800530 return -1;
531}
532
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700533int Volume::unmountVol(bool force, bool revert) {
San Mehata2677e42009-12-13 10:40:18 -0800534 int i, rc;
535
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700536 int flags = getFlags();
537 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
538
San Mehata2677e42009-12-13 10:40:18 -0800539 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700540 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800541 errno = EINVAL;
Ken Sumrall319b1042011-06-14 14:01:55 -0700542 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehata2677e42009-12-13 10:40:18 -0800543 }
544
545 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800546 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800547
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700548 char service[64];
549 snprintf(service, 64, "fuse_%s", getLabel());
550 property_set("ctl.stop", service);
551 /* Give it a chance to stop. I wish we had a synchronous way to determine this... */
552 sleep(1);
553
554 // TODO: determine failure mode if FUSE times out
555
556 if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) {
557 SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno));
558 goto out_mounted;
San Mehat3bb60202010-02-19 18:14:36 -0800559 }
560
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700561 /* Now that the fuse daemon is dead, unmount it */
562 if (doUnmount(getFuseMountpoint(), force) != 0) {
563 SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno));
564 goto fail_remount_secure;
Jeff Sharkey7a3c3d42012-10-04 16:49:22 -0700565 }
566
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700567 /* Unmount the real sd card */
568 if (doUnmount(getMountpoint(), force) != 0) {
569 SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno));
570 goto fail_remount_secure;
San Mehat3bb60202010-02-19 18:14:36 -0800571 }
572
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700573 SLOGI("%s unmounted successfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800574
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700575 /* If this is an encrypted volume, and we've been asked to undo
576 * the crypto mapping, then revert the dm-crypt mapping, and revert
577 * the device info to the original values.
578 */
579 if (revert && isDecrypted()) {
580 cryptfs_revert_volume(getLabel());
581 revertDeviceInfo();
582 SLOGI("Encrypted volume %s reverted successfully", getMountpoint());
583 }
584
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700585 setUuid(NULL);
586 setUserLabel(NULL);
San Mehat3bb60202010-02-19 18:14:36 -0800587 setState(Volume::State_Idle);
588 mCurrentlyMountedKdev = -1;
589 return 0;
590
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700591fail_remount_secure:
592 if (providesAsec && mountAsecExternal() != 0) {
593 SLOGE("Failed to remount secure area (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800594 goto out_nomedia;
595 }
596
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700597out_mounted:
San Mehata2677e42009-12-13 10:40:18 -0800598 setState(Volume::State_Mounted);
599 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800600
601out_nomedia:
602 setState(Volume::State_NoMedia);
603 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800604}
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700605
San Mehata2677e42009-12-13 10:40:18 -0800606int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800607 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800608
San Mehat2a5b8ce2010-03-10 12:48:57 -0800609 memset(&dinfo, 0, sizeof(dinfo));
610
611 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700612 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800613 return -1;
614 }
615
San Mehat2a5b8ce2010-03-10 12:48:57 -0800616 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
617 dinfo.device = strdup(deviceNode);
618 dinfo.scheme = PART_SCHEME_MBR;
619 dinfo.sect_size = 512;
620 dinfo.skip_lba = 2048;
621 dinfo.num_lba = 0;
622 dinfo.num_parts = 1;
623
624 struct part_info *pinfo = &dinfo.part_lst[0];
625
626 pinfo->name = strdup("android_sdcard");
627 pinfo->flags |= PART_ACTIVE_FLAG;
628 pinfo->type = PC_PART_TYPE_FAT32;
629 pinfo->len_kb = -1;
630
631 int rc = apply_disk_config(&dinfo, 0);
632
633 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700634 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800635 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800636 }
637
San Mehat2a5b8ce2010-03-10 12:48:57 -0800638 out:
639 free(pinfo->name);
640 free(dinfo.device);
641 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800642
San Mehat2a5b8ce2010-03-10 12:48:57 -0800643 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800644}
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700645
646/*
647 * Use blkid to extract UUID and label from device, since it handles many
648 * obscure edge cases around partition types and formats. Always broadcasts
649 * updated metadata values.
650 */
651int Volume::extractMetadata(const char* devicePath) {
652 int res = 0;
653
654 std::string cmd;
655 cmd = BLKID_PATH;
656 cmd += " -c /dev/null ";
657 cmd += devicePath;
658
659 FILE* fp = popen(cmd.c_str(), "r");
660 if (!fp) {
661 ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno));
662 res = -1;
663 goto done;
664 }
665
666 char line[1024];
667 char value[128];
668 if (fgets(line, sizeof(line), fp) != NULL) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700669 ALOGD("blkid identified as %s", line);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700670
Jeff Sharkey6bcd3622013-11-11 14:46:39 -0800671 char* start = strstr(line, "UUID=");
672 if (start != NULL && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700673 setUuid(value);
674 } else {
675 setUuid(NULL);
676 }
677
Jeff Sharkey6bcd3622013-11-11 14:46:39 -0800678 start = strstr(line, "LABEL=");
679 if (start != NULL && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700680 setUserLabel(value);
681 } else {
682 setUserLabel(NULL);
683 }
684 } else {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700685 ALOGW("blkid failed to identify %s", devicePath);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700686 res = -1;
687 }
688
689 pclose(fp);
690
691done:
692 if (res == -1) {
693 setUuid(NULL);
694 setUserLabel(NULL);
695 }
696 return res;
697}