blob: cd91a9fae22480ff796c1481e4e8820ace19387b [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>
28
29#include <linux/kdev_t.h>
30
31#include <cutils/properties.h>
32
33#include "diskmbr.h"
San Mehatf1b736b2009-10-10 17:22:08 -070034
35#define LOG_TAG "Vold"
36
37#include <cutils/log.h>
38
39#include "Volume.h"
San Mehata2677e42009-12-13 10:40:18 -080040#include "VolumeManager.h"
41#include "ResponseCode.h"
San Mehatbf041852010-01-04 10:09:16 -080042#include "Fat.h"
San Mehat586536c2010-02-16 17:12:00 -080043#include "Process.h"
San Mehatf1b736b2009-10-10 17:22:08 -070044
San Mehata2677e42009-12-13 10:40:18 -080045extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d);
46extern "C" void dos_partition_enc(void *pp, struct dos_partition *d);
San Mehat49e2bce2009-10-12 16:29:01 -070047
San Mehat3bb60202010-02-19 18:14:36 -080048
49/*
50 * Secure directory - stuff that only root can see
51 */
52const char *Volume::SECDIR = "/mnt/secure";
53
54/*
55 * Secure staging directory - where media is mounted for preparation
56 */
57const char *Volume::SEC_STGDIR = "/mnt/secure/staging";
58
59/*
60 * Path to the directory on the media which contains publicly accessable
61 * asec imagefiles. This path will be obscured before the mount is
62 * exposed to non priviledged users.
63 */
64const char *Volume::SEC_STG_SECIMGDIR = "/mnt/secure/staging/android_secure";
65
66/*
67 * Path to where *only* root can access asec imagefiles
68 */
69const char *Volume::SEC_ASECDIR = "/mnt/secure/asec";
70
71/*
72 * Path to where secure containers are mounted
73 */
74const char *Volume::ASECDIR = "/mnt/asec";
75
San Mehata2677e42009-12-13 10:40:18 -080076static const char *stateToStr(int state) {
77 if (state == Volume::State_Init)
78 return "Initializing";
79 else if (state == Volume::State_NoMedia)
80 return "No-Media";
81 else if (state == Volume::State_Idle)
82 return "Idle-Unmounted";
83 else if (state == Volume::State_Pending)
84 return "Pending";
85 else if (state == Volume::State_Mounted)
86 return "Mounted";
87 else if (state == Volume::State_Unmounting)
88 return "Unmounting";
89 else if (state == Volume::State_Checking)
90 return "Checking";
91 else if (state == Volume::State_Formatting)
92 return "Formatting";
93 else if (state == Volume::State_Shared)
94 return "Shared-Unmounted";
95 else if (state == Volume::State_SharedMnt)
96 return "Shared-Mounted";
97 else
98 return "Unknown-Error";
99}
100
101Volume::Volume(VolumeManager *vm, const char *label, const char *mount_point) {
102 mVm = vm;
San Mehatf1b736b2009-10-10 17:22:08 -0700103 mLabel = strdup(label);
104 mMountpoint = strdup(mount_point);
105 mState = Volume::State_Init;
San Mehata2677e42009-12-13 10:40:18 -0800106 mCurrentlyMountedKdev = -1;
San Mehatf1b736b2009-10-10 17:22:08 -0700107}
108
109Volume::~Volume() {
110 free(mLabel);
111 free(mMountpoint);
112}
113
San Mehata2677e42009-12-13 10:40:18 -0800114dev_t Volume::getDiskDevice() {
115 return MKDEV(0, 0);
116};
117
118void Volume::handleVolumeShared() {
119}
120
121void Volume::handleVolumeUnshared() {
122}
123
San Mehatfd7f5872009-10-12 11:32:47 -0700124int Volume::handleBlockEvent(NetlinkEvent *evt) {
San Mehatf1b736b2009-10-10 17:22:08 -0700125 errno = ENOSYS;
126 return -1;
127}
128
129void Volume::setState(int state) {
San Mehata2677e42009-12-13 10:40:18 -0800130 char msg[255];
131 int oldState = mState;
132
133 if (oldState == state) {
134 LOGW("Duplicate state (%d)\n", state);
135 return;
136 }
137
San Mehatf1b736b2009-10-10 17:22:08 -0700138 mState = state;
San Mehata2677e42009-12-13 10:40:18 -0800139
140 LOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel,
141 oldState, stateToStr(oldState), mState, stateToStr(mState));
142 snprintf(msg, sizeof(msg),
143 "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(),
144 getMountpoint(), oldState, stateToStr(oldState), mState,
145 stateToStr(mState));
146
147 mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange,
148 msg, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700149}
San Mehat49e2bce2009-10-12 16:29:01 -0700150
San Mehatdd9b8e92009-10-21 11:06:52 -0700151int Volume::createDeviceNode(const char *path, int major, int minor) {
152 mode_t mode = 0660 | S_IFBLK;
153 dev_t dev = (major << 8) | minor;
154 if (mknod(path, mode, dev) < 0) {
155 if (errno != EEXIST) {
156 return -1;
157 }
158 }
159 return 0;
160}
161
San Mehata2677e42009-12-13 10:40:18 -0800162int Volume::formatVol() {
San Mehat49e2bce2009-10-12 16:29:01 -0700163
San Mehata2677e42009-12-13 10:40:18 -0800164 if (getState() == Volume::State_NoMedia) {
165 errno = ENODEV;
166 return -1;
167 } else if (getState() != Volume::State_Idle) {
168 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700169 return -1;
170 }
171
San Mehata2677e42009-12-13 10:40:18 -0800172 if (isMountpointMounted(getMountpoint())) {
173 LOGW("Volume is idle but appears to be mounted - fixing");
174 setState(Volume::State_Mounted);
175 // mCurrentlyMountedKdev = XXX
176 errno = EBUSY;
San Mehat49e2bce2009-10-12 16:29:01 -0700177 return -1;
178 }
179
San Mehata2677e42009-12-13 10:40:18 -0800180 char devicePath[255];
181 dev_t diskNode = getDiskDevice();
182 dev_t partNode = MKDEV(MAJOR(diskNode), 1); // XXX: Hmmm
183
184 sprintf(devicePath, "/dev/block/vold/%d:%d",
185 MAJOR(diskNode), MINOR(diskNode));
186
San Mehat62e5dd22010-02-06 09:53:22 -0800187 LOGI("Formatting volume %s (%s)", getLabel(), devicePath);
San Mehata2677e42009-12-13 10:40:18 -0800188
189 if (initializeMbr(devicePath)) {
190 LOGE("Failed to initialize MBR (%s)", strerror(errno));
191 goto err;
San Mehat49e2bce2009-10-12 16:29:01 -0700192 }
193
San Mehata2677e42009-12-13 10:40:18 -0800194 sprintf(devicePath, "/dev/block/vold/%d:%d",
195 MAJOR(partNode), MINOR(partNode));
San Mehatdd9b8e92009-10-21 11:06:52 -0700196
San Mehatbf041852010-01-04 10:09:16 -0800197 if (Fat::format(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800198 LOGE("Failed to format (%s)", strerror(errno));
199 goto err;
200 }
201
San Mehat49e2bce2009-10-12 16:29:01 -0700202 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800203err:
204 return -1;
205}
206
207bool Volume::isMountpointMounted(const char *path) {
208 char device[256];
209 char mount_path[256];
210 char rest[256];
211 FILE *fp;
212 char line[1024];
213
214 if (!(fp = fopen("/proc/mounts", "r"))) {
215 LOGE("Error opening /proc/mounts (%s)", strerror(errno));
216 return false;
217 }
218
219 while(fgets(line, sizeof(line), fp)) {
220 line[strlen(line)-1] = '\0';
221 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
222 if (!strcmp(mount_path, path)) {
223 fclose(fp);
224 return true;
225 }
226
227 }
228
229 fclose(fp);
230 return false;
231}
232
233int Volume::mountVol() {
234 dev_t deviceNodes[4];
235 int n, i, rc = 0;
236 char errmsg[255];
237
238 if (getState() == Volume::State_NoMedia) {
239 snprintf(errmsg, sizeof(errmsg),
240 "Volume %s %s mount failed - no media",
241 getLabel(), getMountpoint());
242 mVm->getBroadcaster()->sendBroadcast(
243 ResponseCode::VolumeMountFailedNoMedia,
244 errmsg, false);
245 errno = ENODEV;
246 return -1;
247 } else if (getState() != Volume::State_Idle) {
248 errno = EBUSY;
249 return -1;
250 }
251
252 if (isMountpointMounted(getMountpoint())) {
253 LOGW("Volume is idle but appears to be mounted - fixing");
254 setState(Volume::State_Mounted);
255 // mCurrentlyMountedKdev = XXX
256 return 0;
257 }
258
259 n = getDeviceNodes((dev_t *) &deviceNodes, 4);
260 if (!n) {
261 LOGE("Failed to get device nodes (%s)\n", strerror(errno));
262 return -1;
263 }
264
265 for (i = 0; i < n; i++) {
266 char devicePath[255];
267
268 sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),
269 MINOR(deviceNodes[i]));
270
271 LOGI("%s being considered for volume %s\n", devicePath, getLabel());
272
273 errno = 0;
San Mehatbf041852010-01-04 10:09:16 -0800274 setState(Volume::State_Checking);
275
San Mehat3bb60202010-02-19 18:14:36 -0800276 if (Fat::check(devicePath)) {
San Mehata2677e42009-12-13 10:40:18 -0800277 if (errno == ENODATA) {
278 LOGW("%s does not contain a FAT filesystem\n", devicePath);
279 continue;
San Mehata2677e42009-12-13 10:40:18 -0800280 }
San Mehateba65e92010-01-29 05:15:16 -0800281 errno = EIO;
282 /* Badness - abort the mount */
283 LOGE("%s failed FS checks (%s)", devicePath, strerror(errno));
284 setState(Volume::State_Idle);
285 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800286 }
287
San Mehat3bb60202010-02-19 18:14:36 -0800288 /*
289 * Mount the device on our internal staging mountpoint so we can
290 * muck with it before exposing it to non priviledged users.
291 */
San Mehata2677e42009-12-13 10:40:18 -0800292 errno = 0;
San Mehat3bb60202010-02-19 18:14:36 -0800293 if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, 1000, 1015, 0702, true)) {
294 LOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
295 continue;
San Mehata2677e42009-12-13 10:40:18 -0800296 }
297
San Mehat3bb60202010-02-19 18:14:36 -0800298 LOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint());
299
300 if (createBindMounts()) {
301 LOGE("Failed to create bindmounts (%s)", strerror(errno));
302 umount("/mnt/secure/staging");
303 setState(Volume::State_Idle);
304 return -1;
305 }
306
307 /*
308 * Now that the bindmount trickery is done, atomically move the
309 * whole subtree to expose it to non priviledged users.
310 */
311 if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) {
312 LOGE("Failed to move mount (%s)", strerror(errno));
313 umount("/mnt/secure/staging");
314 setState(Volume::State_Idle);
315 return -1;
316 }
317 setState(Volume::State_Mounted);
318 mCurrentlyMountedKdev = deviceNodes[i];
319 return 0;
San Mehata2677e42009-12-13 10:40:18 -0800320 }
321
San Mehata2677e42009-12-13 10:40:18 -0800322 LOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());
323 setState(Volume::State_Idle);
324
San Mehateba65e92010-01-29 05:15:16 -0800325 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800326}
327
San Mehat3bb60202010-02-19 18:14:36 -0800328int Volume::createBindMounts() {
329 unsigned long flags;
330
331 /*
332 * Ensure that /android_secure exists and is a directory
333 */
334 if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) {
335 if (errno == ENOENT) {
336 if (mkdir(SEC_STG_SECIMGDIR, 0777)) {
337 LOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
338 return -1;
339 }
340 } else {
341 LOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
342 return -1;
343 }
344 } else {
345 struct stat sbuf;
346
347 if (stat(SEC_STG_SECIMGDIR, &sbuf)) {
348 LOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
349 return -1;
350 }
351 if (!S_ISDIR(sbuf.st_mode)) {
352 LOGE("%s is not a directory", SEC_STG_SECIMGDIR);
353 errno = ENOTDIR;
354 return -1;
355 }
356 }
357
358 /*
359 * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll
360 * have a root only accessable mountpoint for it.
361 */
362 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
363 LOGE("Failed to bind mount points %s -> %s (%s)",
364 SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno));
365 return -1;
366 }
367
368 /*
369 * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to
370 * obscure the underlying directory from everybody - sneaky eh? ;)
371 */
372 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) {
373 LOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
374 umount("/mnt/asec_secure");
375 return -1;
376 }
377
378 return 0;
379}
380
381int Volume::doMoveMount(const char *src, const char *dst, bool force) {
382 unsigned int flags = MS_MOVE;
383 int retries = 5;
384
385 while(retries--) {
386 if (!mount(src, dst, "", flags, NULL)) {
387 LOGD("Moved mount %s -> %s sucessfully", src, dst);
388 return 0;
389 } else if (errno != EBUSY) {
390 LOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno));
391 return -1;
392 }
393 int action = 0;
394
395 if (force) {
396 if (retries == 1) {
397 action = 2; // SIGKILL
398 } else if (retries == 2) {
399 action = 1; // SIGHUP
400 }
401 }
402 LOGW("Failed to move %s -> %s (%s, retries %d, action %d)",
403 src, dst, strerror(errno), retries, action);
404 Process::killProcessesWithOpenFiles(src, action);
405 usleep(1000*250);
406 }
407
408 errno = EBUSY;
409 LOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno));
410 return -1;
411}
412
413int Volume::doUnmount(const char *path, bool force) {
414 int retries = 10;
415
416 while (retries--) {
417 if (!umount(path) || errno == EINVAL || errno == ENOENT) {
418 LOGI("%s sucessfully unmounted", path);
419 return 0;
420 }
421
422 int action = 0;
423
424 if (force) {
425 if (retries == 1) {
426 action = 2; // SIGKILL
427 } else if (retries == 2) {
428 action = 1; // SIGHUP
429 }
430 }
431
432 LOGW("Failed to unmount %s (%s, retries %d, action %d)",
433 path, strerror(errno), retries, action);
434
435 Process::killProcessesWithOpenFiles(path, action);
436 usleep(1000*1000);
437 }
438 errno = EBUSY;
439 LOGE("Giving up on unmount %s (%s)", path, strerror(errno));
440 return -1;
441}
442
San Mehat4ba89482010-02-18 09:00:18 -0800443int Volume::unmountVol(bool force) {
San Mehata2677e42009-12-13 10:40:18 -0800444 int i, rc;
445
446 if (getState() != Volume::State_Mounted) {
447 LOGE("Volume %s unmount request when not mounted", getLabel());
448 errno = EINVAL;
449 return -1;
450 }
451
452 setState(Volume::State_Unmounting);
San Mehat4ba89482010-02-18 09:00:18 -0800453 usleep(1000 * 1000); // Give the framework some time to react
San Mehata2677e42009-12-13 10:40:18 -0800454
San Mehat3bb60202010-02-19 18:14:36 -0800455 /*
456 * First move the mountpoint back to our internal staging point
457 * so nobody else can muck with it while we work.
458 */
459 if (doMoveMount(getMountpoint(), SEC_STGDIR, force)) {
460 LOGE("Failed to move mount %s => %s (%s)", getMountpoint(), SEC_STGDIR, strerror(errno));
461 setState(Volume::State_Mounted);
462 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800463 }
464
San Mehat3bb60202010-02-19 18:14:36 -0800465 /*
466 * Unmount the tmpfs which was obscuring the asec image directory
467 * from non root users
468 */
469
470 if (doUnmount(Volume::SEC_STG_SECIMGDIR, force)) {
471 LOGE("Failed to unmount tmpfs on %s (%s)", SEC_STG_SECIMGDIR, strerror(errno));
472 goto fail_republish;
San Mehata2677e42009-12-13 10:40:18 -0800473 }
474
San Mehat3bb60202010-02-19 18:14:36 -0800475 /*
476 * Remove the bindmount we were using to keep a reference to
477 * the previously obscured directory.
478 */
479
480 if (doUnmount(Volume::SEC_ASECDIR, force)) {
481 LOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR, strerror(errno));
482 goto fail_remount_tmpfs;
483 }
484
485 /*
486 * Finally, unmount the actual block device from the staging dir
487 */
488 if (doUnmount(Volume::SEC_STGDIR, force)) {
489 LOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno));
490 goto fail_recreate_bindmount;
491 }
492
493 LOGI("%s unmounted sucessfully", getMountpoint());
494
495 setState(Volume::State_Idle);
496 mCurrentlyMountedKdev = -1;
497 return 0;
498
499 /*
500 * Failure handling - try to restore everything back the way it was
501 */
502fail_recreate_bindmount:
503 if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, "", MS_BIND, NULL)) {
504 LOGE("Failed to restore bindmount after failure! - Storage will appear offline!");
505 goto out_nomedia;
506 }
507fail_remount_tmpfs:
508 if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) {
509 LOGE("Failed to restore tmpfs after failure! - Storage will appear offline!");
510 goto out_nomedia;
511 }
512fail_republish:
513 if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) {
514 LOGE("Failed to republish mount after failure! - Storage will appear offline!");
515 goto out_nomedia;
516 }
517
San Mehata2677e42009-12-13 10:40:18 -0800518 setState(Volume::State_Mounted);
519 return -1;
San Mehat3bb60202010-02-19 18:14:36 -0800520
521out_nomedia:
522 setState(Volume::State_NoMedia);
523 return -1;
San Mehata2677e42009-12-13 10:40:18 -0800524}
525
526int Volume::initializeMbr(const char *deviceNode) {
527 int fd, rc;
528 unsigned char block[512];
529 struct dos_partition part;
530 unsigned int nr_sec;
531
532 if ((fd = open(deviceNode, O_RDWR)) < 0) {
533 LOGE("Error opening disk file (%s)", strerror(errno));
534 return -1;
535 }
536
537 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
538 LOGE("Unable to get device size (%s)", strerror(errno));
539 close(fd);
540 return -1;
541 }
542
543 memset(&part, 0, sizeof(part));
544 part.dp_flag = 0x80;
545 part.dp_typ = 0xc;
546 part.dp_start = ((1024 * 64) / 512) + 1;
547 part.dp_size = nr_sec - part.dp_start;
548
549 memset(block, 0, sizeof(block));
550 block[0x1fe] = 0x55;
551 block[0x1ff] = 0xaa;
552
553 dos_partition_enc(block + DOSPARTOFF, &part);
554
555 if (write(fd, block, sizeof(block)) < 0) {
556 LOGE("Error writing MBR (%s)", strerror(errno));
557 close(fd);
558 return -1;
559 }
560
561 if (ioctl(fd, BLKRRPART, NULL) < 0) {
562 LOGE("Error re-reading partition table (%s)", strerror(errno));
563 close(fd);
564 return -1;
565 }
566 close(fd);
567 return 0;
568}