blob: 21b66b1e2bb20ad972ebcb5cb3008a0e25805767 [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
San Mehatfd7f5872009-10-12 11:32:47 -0700150int 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();
Mike Lockwooda4886f12010-09-21 13:56:35 -0400253 dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx));
San Mehata2677e42009-12-13 10:40:18 -0800254
San Mehat2a5b8ce2010-03-10 12:48:57 -0800255 setState(Volume::State_Formatting);
San Mehata2677e42009-12-13 10:40:18 -0800256
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800257 int ret = -1;
Mike Lockwooda4886f12010-09-21 13:56:35 -0400258 // Only initialize the MBR if we are formatting the entire device
259 if (formatEntireDevice) {
260 sprintf(devicePath, "/dev/block/vold/%d:%d",
261 MAJOR(diskNode), MINOR(diskNode));
262
263 if (initializeMbr(devicePath)) {
264 SLOGE("Failed to initialize MBR (%s)", strerror(errno));
265 goto err;
266 }
San Mehat49e2bce2009-10-12 16:29:01 -0700267 }
268
San Mehata2677e42009-12-13 10:40:18 -0800269 sprintf(devicePath, "/dev/block/vold/%d:%d",
270 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700271
Mike Lockwooda4886f12010-09-21 13:56:35 -0400272 if (mDebug) {
273 SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
274 }
275
Ken Sumrall9caab762013-06-11 19:10:20 -0700276 if (Fat::format(devicePath, 0, wipe)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700277 SLOGE("Failed to format (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800278 goto err;
279 }
280
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800281 ret = 0;
282
San Mehata2677e42009-12-13 10:40:18 -0800283err:
Chih-Wei Huang64382de2010-11-16 13:18:19 +0800284 setState(Volume::State_Idle);
285 return ret;
San Mehata2677e42009-12-13 10:40:18 -0800286}
287
288bool Volume::isMountpointMounted(const char *path) {
289 char device[256];
290 char mount_path[256];
291 char rest[256];
292 FILE *fp;
293 char line[1024];
294
295 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700296 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800297 return false;
298 }
299
300 while(fgets(line, sizeof(line), fp)) {
301 line[strlen(line)-1] = '\0';
302 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
303 if (!strcmp(mount_path, path)) {
304 fclose(fp);
305 return true;
306 }
San Mehata2677e42009-12-13 10:40:18 -0800307 }
308
309 fclose(fp);
310 return false;
311}
312
313int Volume::mountVol() {
314 dev_t deviceNodes[4];
315 int n, i, rc = 0;
316 char errmsg[255];
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700317
318 int flags = getFlags();
319 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
320
321 // TODO: handle "bind" style mounts, for emulated storage
322
Ken Sumrall29d8da82011-05-18 17:20:07 -0700323 char decrypt_state[PROPERTY_VALUE_MAX];
324 char crypto_state[PROPERTY_VALUE_MAX];
325 char encrypt_progress[PROPERTY_VALUE_MAX];
San Mehata2677e42009-12-13 10:40:18 -0800326
Ken Sumrall29d8da82011-05-18 17:20:07 -0700327 property_get("vold.decrypt", decrypt_state, "");
328 property_get("vold.encrypt_progress", encrypt_progress, "");
329
330 /* Don't try to mount the volumes if we have not yet entered the disk password
331 * or are in the process of encrypting.
332 */
333 if ((getState() == Volume::State_NoMedia) ||
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700334 ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) {
San Mehata2677e42009-12-13 10:40:18 -0800335 snprintf(errmsg, sizeof(errmsg),
336 "Volume %s %s mount failed - no media",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700337 getLabel(), getFuseMountpoint());
San Mehata2677e42009-12-13 10:40:18 -0800338 mVm->getBroadcaster()->sendBroadcast(
339 ResponseCode::VolumeMountFailedNoMedia,
340 errmsg, false);
341 errno = ENODEV;
342 return -1;
343 } else if (getState() != Volume::State_Idle) {
344 errno = EBUSY;
Joseph Lehrer507d31b2011-04-11 15:02:50 -0700345 if (getState() == Volume::State_Pending) {
346 mRetryMount = true;
347 }
San Mehata2677e42009-12-13 10:40:18 -0800348 return -1;
349 }
350
351 if (isMountpointMounted(getMountpoint())) {
San Mehat97ac40e2010-03-24 10:24:19 -0700352 SLOGW("Volume is idle but appears to be mounted - fixing");
San Mehata2677e42009-12-13 10:40:18 -0800353 setState(Volume::State_Mounted);
354 // mCurrentlyMountedKdev = XXX
355 return 0;
356 }
357
358 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
359 if (!n) {
San Mehat97ac40e2010-03-24 10:24:19 -0700360 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -0800361 return -1;
362 }
363
Ken Sumrall29d8da82011-05-18 17:20:07 -0700364 /* If we're running encrypted, and the volume is marked as encryptable and nonremovable,
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700365 * and also marked as providing Asec storage, then we need to decrypt
Ken Sumrall29d8da82011-05-18 17:20:07 -0700366 * that partition, and update the volume object to point to it's new decrypted
367 * block device
368 */
369 property_get("ro.crypto.state", crypto_state, "");
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700370 if (providesAsec &&
Ken Sumrall29d8da82011-05-18 17:20:07 -0700371 ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) &&
372 !strcmp(crypto_state, "encrypted") && !isDecrypted()) {
373 char new_sys_path[MAXPATHLEN];
374 char nodepath[256];
375 int new_major, new_minor;
376
377 if (n != 1) {
378 /* We only expect one device node returned when mounting encryptable volumes */
379 SLOGE("Too many device nodes returned when mounting %d\n", getMountpoint());
380 return -1;
381 }
382
383 if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]),
384 new_sys_path, sizeof(new_sys_path),
385 &new_major, &new_minor)) {
386 SLOGE("Cannot setup encryption mapping for %d\n", getMountpoint());
387 return -1;
388 }
389 /* We now have the new sysfs path for the decrypted block device, and the
390 * majore and minor numbers for it. So, create the device, update the
391 * path to the new sysfs path, and continue.
392 */
393 snprintf(nodepath,
394 sizeof(nodepath), "/dev/block/vold/%d:%d",
395 new_major, new_minor);
396 if (createDeviceNode(nodepath, new_major, new_minor)) {
397 SLOGE("Error making device node '%s' (%s)", nodepath,
398 strerror(errno));
399 }
400
401 // Todo: Either create sys filename from nodepath, or pass in bogus path so
402 // vold ignores state changes on this internal device.
403 updateDeviceInfo(nodepath, new_major, new_minor);
404
405 /* Get the device nodes again, because they just changed */
406 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
407 if (!n) {
408 SLOGE("Failed to get device nodes (%s)\n", strerror(errno));
409 return -1;
410 }
411 }
412
San Mehata2677e42009-12-13 10:40:18 -0800413 for (i = 0; i < n; i++) {
414 char devicePath[255];
415
416 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
417 MINOR(deviceNodes[i]));
418
San Mehat97ac40e2010-03-24 10:24:19 -0700419 SLOGI("%s being considered for volume %s\n", devicePath, getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800420
421 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800422 setState(Volume::State_Checking);
423
San Mehat3bb60202010-02-19 18:14:36 -0800424 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800425 if (errno == ENODATA) {
San Mehat97ac40e2010-03-24 10:24:19 -0700426 SLOGW("%s does not contain a FAT filesystem\n", devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800427 continue;
San Mehata2677e42009-12-13 10:40:18 -0800428 }
San Mehateba65e92010-01-29 05:15:16 -0800429 errno = EIO;
430 /* Badness - abort the mount */
San Mehat97ac40e2010-03-24 10:24:19 -0700431 SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
San Mehateba65e92010-01-29 05:15:16 -0800432 setState(Volume::State_Idle);
433 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800434 }
435
San Mehata2677e42009-12-13 10:40:18 -0800436 errno = 0;
Mike Lockwood9092b1d2011-03-23 14:55:49 -0400437 int gid;
438
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700439 if (Fat::doMount(devicePath, getMountpoint(), false, false, false,
440 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700441 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800442 continue;
San Mehata2677e42009-12-13 10:40:18 -0800443 }
444
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700445 extractMetadata(devicePath);
San Mehatcb4dac82010-03-14 13:41:54 -0700446
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700447 if (providesAsec && mountAsecExternal() != 0) {
448 SLOGE("Failed to mount secure area (%s)", strerror(errno));
449 umount(getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800450 setState(Volume::State_Idle);
451 return -1;
452 }
453
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700454 char service[64];
455 snprintf(service, 64, "fuse_%s", getLabel());
456 property_set("ctl.start", service);
457
San Mehat3bb60202010-02-19 18:14:36 -0800458 setState(Volume::State_Mounted);
459 mCurrentlyMountedKdev = deviceNodes[i];
460 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800461 }
462
San Mehat97ac40e2010-03-24 10:24:19 -0700463 SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800464 setState(Volume::State_Idle);
465
San Mehateba65e92010-01-29 05:15:16 -0800466 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800467}
468
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700469int Volume::mountAsecExternal() {
470 char legacy_path[PATH_MAX];
471 char secure_path[PATH_MAX];
San Mehat3bb60202010-02-19 18:14:36 -0800472
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700473 snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint());
474 snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint());
475
476 // Recover legacy secure path
477 if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) {
478 if (rename(legacy_path, secure_path)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700479 SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno));
San Mehat52c2ccb2010-02-23 18:26:13 -0800480 }
481 }
482
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700483 if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700484 SLOGW("fs_prepare_dir failed: %s", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800485 return -1;
486 }
487
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700488 if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) {
489 SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path,
490 SEC_ASECDIR_EXT, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800491 return -1;
492 }
493
494 return 0;
495}
496
San Mehat3bb60202010-02-19 18:14:36 -0800497int Volume::doUnmount(const char *path, bool force) {
498 int retries = 10;
499
San Mehatd9a4e352010-03-12 13:32:47 -0800500 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700501 SLOGD("Unmounting {%s}, force = %d", path, force);
San Mehatd9a4e352010-03-12 13:32:47 -0800502 }
503
San Mehat3bb60202010-02-19 18:14:36 -0800504 while (retries--) {
505 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
San Mehat97ac40e2010-03-24 10:24:19 -0700506 SLOGI("%s sucessfully unmounted", path);
San Mehat3bb60202010-02-19 18:14:36 -0800507 return 0;
508 }
509
510 int action = 0;
511
512 if (force) {
513 if (retries == 1) {
514 action = 2; // SIGKILL
515 } else if (retries == 2) {
516 action = 1; // SIGHUP
517 }
518 }
519
San Mehat97ac40e2010-03-24 10:24:19 -0700520 SLOGW("Failed to unmount %s (%s, retries %d, action %d)",
San Mehat3bb60202010-02-19 18:14:36 -0800521 path, strerror(errno), retries, action);
522
523 Process::killProcessesWithOpenFiles(path, action);
524 usleep(1000*1000);
525 }
526 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700527 SLOGE("Giving up on unmount %s (%s)", path, strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800528 return -1;
529}
530
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700531int Volume::unmountVol(bool force, bool revert) {
San Mehata2677e42009-12-13 10:40:18 -0800532 int i, rc;
533
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700534 int flags = getFlags();
535 bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0;
536
San Mehata2677e42009-12-13 10:40:18 -0800537 if (getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -0700538 SLOGE("Volume %s unmount request when not mounted", getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800539 errno = EINVAL;
Ken Sumrall319b1042011-06-14 14:01:55 -0700540 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehata2677e42009-12-13 10:40:18 -0800541 }
542
543 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800544 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800545
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700546 char service[64];
547 snprintf(service, 64, "fuse_%s", getLabel());
548 property_set("ctl.stop", service);
549 /* Give it a chance to stop. I wish we had a synchronous way to determine this... */
550 sleep(1);
551
552 // TODO: determine failure mode if FUSE times out
553
554 if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) {
555 SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno));
556 goto out_mounted;
San Mehat3bb60202010-02-19 18:14:36 -0800557 }
558
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700559 /* Now that the fuse daemon is dead, unmount it */
560 if (doUnmount(getFuseMountpoint(), force) != 0) {
561 SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno));
562 goto fail_remount_secure;
Jeff Sharkey7a3c3d42012-10-04 16:49:22 -0700563 }
564
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700565 /* Unmount the real sd card */
566 if (doUnmount(getMountpoint(), force) != 0) {
567 SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno));
568 goto fail_remount_secure;
San Mehat3bb60202010-02-19 18:14:36 -0800569 }
570
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700571 SLOGI("%s unmounted successfully", getMountpoint());
San Mehat3bb60202010-02-19 18:14:36 -0800572
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700573 /* If this is an encrypted volume, and we've been asked to undo
574 * the crypto mapping, then revert the dm-crypt mapping, and revert
575 * the device info to the original values.
576 */
577 if (revert && isDecrypted()) {
578 cryptfs_revert_volume(getLabel());
579 revertDeviceInfo();
580 SLOGI("Encrypted volume %s reverted successfully", getMountpoint());
581 }
582
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700583 setUuid(NULL);
584 setUserLabel(NULL);
San Mehat3bb60202010-02-19 18:14:36 -0800585 setState(Volume::State_Idle);
586 mCurrentlyMountedKdev = -1;
587 return 0;
588
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700589fail_remount_secure:
590 if (providesAsec && mountAsecExternal() != 0) {
591 SLOGE("Failed to remount secure area (%s)", strerror(errno));
San Mehat3bb60202010-02-19 18:14:36 -0800592 goto out_nomedia;
593 }
594
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700595out_mounted:
San Mehata2677e42009-12-13 10:40:18 -0800596 setState(Volume::State_Mounted);
597 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800598
599out_nomedia:
600 setState(Volume::State_NoMedia);
601 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800602}
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700603
San Mehata2677e42009-12-13 10:40:18 -0800604int Volume::initializeMbr(const char *deviceNode) {
San Mehat2a5b8ce2010-03-10 12:48:57 -0800605 struct disk_info dinfo;
San Mehata2677e42009-12-13 10:40:18 -0800606
San Mehat2a5b8ce2010-03-10 12:48:57 -0800607 memset(&dinfo, 0, sizeof(dinfo));
608
609 if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700610 SLOGE("Failed to malloc prt_lst");
San Mehata2677e42009-12-13 10:40:18 -0800611 return -1;
612 }
613
San Mehat2a5b8ce2010-03-10 12:48:57 -0800614 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
615 dinfo.device = strdup(deviceNode);
616 dinfo.scheme = PART_SCHEME_MBR;
617 dinfo.sect_size = 512;
618 dinfo.skip_lba = 2048;
619 dinfo.num_lba = 0;
620 dinfo.num_parts = 1;
621
622 struct part_info *pinfo = &dinfo.part_lst[0];
623
624 pinfo->name = strdup("android_sdcard");
625 pinfo->flags |= PART_ACTIVE_FLAG;
626 pinfo->type = PC_PART_TYPE_FAT32;
627 pinfo->len_kb = -1;
628
629 int rc = apply_disk_config(&dinfo, 0);
630
631 if (rc) {
San Mehat97ac40e2010-03-24 10:24:19 -0700632 SLOGE("Failed to apply disk configuration (%d)", rc);
San Mehat2a5b8ce2010-03-10 12:48:57 -0800633 goto out;
San Mehata2677e42009-12-13 10:40:18 -0800634 }
635
San Mehat2a5b8ce2010-03-10 12:48:57 -0800636 out:
637 free(pinfo->name);
638 free(dinfo.device);
639 free(dinfo.part_lst);
San Mehata2677e42009-12-13 10:40:18 -0800640
San Mehat2a5b8ce2010-03-10 12:48:57 -0800641 return rc;
San Mehata2677e42009-12-13 10:40:18 -0800642}
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700643
644/*
645 * Use blkid to extract UUID and label from device, since it handles many
646 * obscure edge cases around partition types and formats. Always broadcasts
647 * updated metadata values.
648 */
649int Volume::extractMetadata(const char* devicePath) {
650 int res = 0;
651
652 std::string cmd;
653 cmd = BLKID_PATH;
654 cmd += " -c /dev/null ";
655 cmd += devicePath;
656
657 FILE* fp = popen(cmd.c_str(), "r");
658 if (!fp) {
659 ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno));
660 res = -1;
661 goto done;
662 }
663
664 char line[1024];
665 char value[128];
666 if (fgets(line, sizeof(line), fp) != NULL) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700667 ALOGD("blkid identified as %s", line);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700668
669 char* start = strstr(line, "UUID=") + 5;
670 if (sscanf(start, "\"%127[^\"]\"", value) == 1) {
671 setUuid(value);
672 } else {
673 setUuid(NULL);
674 }
675
676 start = strstr(line, "LABEL=") + 6;
677 if (sscanf(start, "\"%127[^\"]\"", value) == 1) {
678 setUserLabel(value);
679 } else {
680 setUserLabel(NULL);
681 }
682 } else {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -0700683 ALOGW("blkid failed to identify %s", devicePath);
Jeff Sharkey0de365f2013-10-16 16:24:19 -0700684 res = -1;
685 }
686
687 pclose(fp);
688
689done:
690 if (res == -1) {
691 setUuid(NULL);
692 setUserLabel(NULL);
693 }
694 return res;
695}