1f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat/*
2f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Copyright (C) 2008 The Android Open Source Project
3f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
4f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Licensed under the Apache License, Version 2.0 (the "License");
5f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * you may not use this file except in compliance with the License.
6f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * You may obtain a copy of the License at
7f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
8f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *      http://www.apache.org/licenses/LICENSE-2.0
9f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
10f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Unless required by applicable law or agreed to in writing, software
11f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * distributed under the License is distributed on an "AS IS" BASIS,
12f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * See the License for the specific language governing permissions and
14f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * limitations under the License.
15f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat */
16f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
17f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <stdio.h>
18fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehat#include <stdlib.h>
19fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehat#include <string.h>
20f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <errno.h>
21a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#include <fcntl.h>
22344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <fts.h>
23344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <unistd.h>
24a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <sys/stat.h>
25a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <sys/types.h>
26a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <sys/mount.h>
27425524dba1552ab3d2ad39e205e65d0a2af997f2Ken Sumrall#include <dirent.h>
28a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
29a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#include <linux/kdev_t.h>
30f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
31f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#define LOG_TAG "Vold"
32f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
337b18a7b36f61574c0f0bdde0a7409dc36676fa12Kenny Root#include <openssl/md5.h>
347b18a7b36f61574c0f0bdde0a7409dc36676fa12Kenny Root
35f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <cutils/log.h>
36f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
37fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehat#include <sysutils/NetlinkEvent.h>
38fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehat
39344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <private/android_filesystem_config.h>
40344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
41f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include "VolumeManager.h"
42ae10b91044bf76b40b77d81c169e48e0bbdf6d75San Mehat#include "DirectVolume.h"
43a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#include "ResponseCode.h"
44a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include "Loop.h"
45344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include "Ext4.h"
46a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include "Fat.h"
47b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat#include "Devmapper.h"
48586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat#include "Process.h"
49fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat#include "Asec.h"
5029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall#include "cryptfs.h"
5123969931fad6e993832208f099f6eea0f6f76eb5San Mehat
5297f2fc110b2ace7914671c2f5852379bd78922e4Mike Lockwood#define MASS_STORAGE_FILE_PATH  "/sys/class/android_usb/android0/f_mass_storage/lun/file"
5397f2fc110b2ace7914671c2f5852379bd78922e4Mike Lockwood
54f1b736bc5605e92e917ab27f5abf3ba839be2270San MehatVolumeManager *VolumeManager::sInstance = NULL;
55f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
56f1b736bc5605e92e917ab27f5abf3ba839be2270San MehatVolumeManager *VolumeManager::Instance() {
57f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    if (!sInstance)
58f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        sInstance = new VolumeManager();
59f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    return sInstance;
60f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
61f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
62f1b736bc5605e92e917ab27f5abf3ba839be2270San MehatVolumeManager::VolumeManager() {
63d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    mDebug = false;
64f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    mVolumes = new VolumeCollection();
6588705166ab82057090a070c6d4200c3d9db76f11San Mehat    mActiveContainers = new AsecIdCollection();
66f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    mBroadcaster = NULL;
67a28056b38275003895ff5d9576681aca01544822Mike Lockwood    mUmsSharingCount = 0;
68a28056b38275003895ff5d9576681aca01544822Mike Lockwood    mSavedDirtyRatio = -1;
69a28056b38275003895ff5d9576681aca01544822Mike Lockwood    // set dirty ratio to 0 when UMS is active
70a28056b38275003895ff5d9576681aca01544822Mike Lockwood    mUmsDirtyRatio = 0;
713b17005083be230509480ea65ae67c237142fadaKen Sumrall    mVolManagerDisabled = 0;
72f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
73f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
74f1b736bc5605e92e917ab27f5abf3ba839be2270San MehatVolumeManager::~VolumeManager() {
7588705166ab82057090a070c6d4200c3d9db76f11San Mehat    delete mVolumes;
7688705166ab82057090a070c6d4200c3d9db76f11San Mehat    delete mActiveContainers;
77f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
78f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
79d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehatchar *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
80acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root    static const char* digits = "0123456789abcdef";
81acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root
827b18a7b36f61574c0f0bdde0a7409dc36676fa12Kenny Root    unsigned char sig[MD5_DIGEST_LENGTH];
83d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
84acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root    if (buffer == NULL) {
85acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        SLOGE("Destination buffer is NULL");
86acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        errno = ESPIPE;
87acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        return NULL;
88acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root    } else if (id == NULL) {
89acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        SLOGE("Source buffer is NULL");
90acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        errno = ESPIPE;
91acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        return NULL;
92acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root    } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
93acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        SLOGE("Target hash buffer size < %d bytes (%d)",
94acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root                MD5_ASCII_LENGTH_PLUS_NULL, len);
95d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        errno = ESPIPE;
96d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        return NULL;
97d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
987b18a7b36f61574c0f0bdde0a7409dc36676fa12Kenny Root
997b18a7b36f61574c0f0bdde0a7409dc36676fa12Kenny Root    MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
100d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
101acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root    char *p = buffer;
1027b18a7b36f61574c0f0bdde0a7409dc36676fa12Kenny Root    for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
103acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        *p++ = digits[sig[i] >> 4];
104acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root        *p++ = digits[sig[i] & 0x0F];
105d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
106acc9e7dcca8978fc809fa5b4d9b819c515a980ffKenny Root    *p = '\0';
107d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
108d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    return buffer;
109d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat}
110d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
111d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehatvoid VolumeManager::setDebug(bool enable) {
112d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    mDebug = enable;
113d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    VolumeCollection::iterator it;
114d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
115d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        (*it)->setDebug(enable);
116d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
117d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat}
118d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
119f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatint VolumeManager::start() {
120f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    return 0;
121f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
122f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
123f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatint VolumeManager::stop() {
124f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    return 0;
125f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
126f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
127f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatint VolumeManager::addVolume(Volume *v) {
128f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    mVolumes->push_back(v);
129f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    return 0;
130f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
131f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
132fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehatvoid VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
133fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehat    const char *devpath = evt->findParam("DEVPATH");
134f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
135fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehat    /* Lookup a volume to handle this device */
136f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    VolumeCollection::iterator it;
137f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    bool hit = false;
138f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
139fd7f5875129adfe2845f4f3fffb17db3a89eea25San Mehat        if (!(*it)->handleBlockEvent(evt)) {
140a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#ifdef NETLINK_DEBUG
14197ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
142a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#endif
143f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            hit = true;
144f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat            break;
145f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        }
146f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    }
147f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
148f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    if (!hit) {
149a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#ifdef NETLINK_DEBUG
15097ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGW("No volumes handled block event for '%s'", devpath);
151a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#endif
152f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    }
153f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
154f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
155f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehatint VolumeManager::listVolumes(SocketClient *cli) {
156f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    VolumeCollection::iterator i;
157f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
158f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
159f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        char *buffer;
160f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        asprintf(&buffer, "%s %s %d",
161f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                 (*i)->getLabel(), (*i)->getMountpoint(),
162f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                 (*i)->getState());
163a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat        cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
164f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat        free(buffer);
165f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    }
166a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
167f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat    return 0;
168f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
16949e2bce5b74129c26a35e25d4693cbfe98c4688eSan Mehat
170a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehatint VolumeManager::formatVolume(const char *label) {
171a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    Volume *v = lookupVolume(label);
172a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat
173a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    if (!v) {
174a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat        errno = ENOENT;
175a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat        return -1;
176a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    }
177a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat
1783b17005083be230509480ea65ae67c237142fadaKen Sumrall    if (mVolManagerDisabled) {
1793b17005083be230509480ea65ae67c237142fadaKen Sumrall        errno = EBUSY;
1803b17005083be230509480ea65ae67c237142fadaKen Sumrall        return -1;
1813b17005083be230509480ea65ae67c237142fadaKen Sumrall    }
1823b17005083be230509480ea65ae67c237142fadaKen Sumrall
183a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    return v->formatVol();
184a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat}
185a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat
186508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Rootint VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
187508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    char idHash[33];
188508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
189508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
190508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        return -1;
191508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    }
192508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root
193508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    memset(mountPath, 0, mountPathLen);
194508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
195508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root
196508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    if (access(mountPath, F_OK)) {
197508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        errno = ENOENT;
198508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        return -1;
199508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    }
200508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root
201508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    return 0;
202508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root}
203508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root
204a19b250bd273455933ca3502cf2c2e0a803aff77San Mehatint VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
20588ac2c06539485942bf414efda2d39647fa1a415San Mehat    char asecFileName[255];
206344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
207344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (findAsec(id, asecFileName, sizeof(asecFileName))) {
208344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't find ASEC %s", id);
209344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
210344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
21188ac2c06539485942bf414efda2d39647fa1a415San Mehat
21288ac2c06539485942bf414efda2d39647fa1a415San Mehat    memset(buffer, 0, maxlen);
21388ac2c06539485942bf414efda2d39647fa1a415San Mehat    if (access(asecFileName, F_OK)) {
21488ac2c06539485942bf414efda2d39647fa1a415San Mehat        errno = ENOENT;
21588ac2c06539485942bf414efda2d39647fa1a415San Mehat        return -1;
21688ac2c06539485942bf414efda2d39647fa1a415San Mehat    }
217a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
2183bb6020e461e8872e8df0775cba6eb32e06b93ecSan Mehat    snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
219a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
220a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
221a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
222736910ca99a40b9add4353bf619e778c40938948Dianne Hackbornint VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
223736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn    char asecFileName[255];
224344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
225344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (findAsec(id, asecFileName, sizeof(asecFileName))) {
226344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't find ASEC %s", id);
227344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
228344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
229736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn
230736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn    memset(buffer, 0, maxlen);
231736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn    if (access(asecFileName, F_OK)) {
232736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        errno = ENOENT;
233736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        return -1;
234736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn    }
235736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn
236736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn    snprintf(buffer, maxlen, "%s", asecFileName);
237736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn    return 0;
238736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn}
239736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn
240344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootint VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
241344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        const char *key, const int ownerUid, bool isExternal) {
242fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    struct asec_superblock sb;
243fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    memset(&sb, 0, sizeof(sb));
244fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat
245344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    const bool wantFilesystem = strcmp(fstype, "none");
246344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    bool usingExt4 = false;
247344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (wantFilesystem) {
248344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        usingExt4 = !strcmp(fstype, "ext4");
249344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (usingExt4) {
250344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
251344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        } else if (strcmp(fstype, "fat")) {
252344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            SLOGE("Invalid filesystem type %s", fstype);
253344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            errno = EINVAL;
254344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            return -1;
255344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
256344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
257344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
258fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    sb.magic = ASEC_SB_MAGIC;
259fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    sb.ver = ASEC_SB_VER;
260a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
261d31e380bd9689dd9629b510ffe324707e261b439San Mehat    if (numSectors < ((1024*1024)/512)) {
26297ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Invalid container size specified (%d sectors)", numSectors);
263d31e380bd9689dd9629b510ffe324707e261b439San Mehat        errno = EINVAL;
264d31e380bd9689dd9629b510ffe324707e261b439San Mehat        return -1;
265d31e380bd9689dd9629b510ffe324707e261b439San Mehat    }
266d31e380bd9689dd9629b510ffe324707e261b439San Mehat
267a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if (lookupVolume(id)) {
26897ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("ASEC id '%s' currently exists", id);
269a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        errno = EADDRINUSE;
270a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
271a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
272a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
273a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char asecFileName[255];
274344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
275344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
276344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
277344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                asecFileName, strerror(errno));
278344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        errno = EADDRINUSE;
279344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
280344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
281344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
282344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
283344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
284344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
285a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
286a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if (!access(asecFileName, F_OK)) {
28797ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
288344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                asecFileName, strerror(errno));
289a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        errno = EADDRINUSE;
290a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
291a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
292a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
293fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    /*
294fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat     * Add some headroom
295fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat     */
296fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
297fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    unsigned numImgSectors = numSectors + fatSize + 2;
298fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat
299fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    if (numImgSectors % 63) {
300fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        numImgSectors += (63 - (numImgSectors % 63));
301fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    }
302fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat
303fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    // Add +1 for our superblock which is at the end
304fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
30597ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("ASEC image file creation failed (%s)", strerror(errno));
306a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
307a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
308a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
309d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    char idHash[33];
310d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (!asecHash(id, idHash, sizeof(idHash))) {
31197ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
312d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        unlink(asecFileName);
313d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        return -1;
314d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
315d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
316a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char loopDevice[255];
317d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
31897ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
319a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        unlink(asecFileName);
320a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
321a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
322a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
323b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    char dmDevice[255];
324b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    bool cleanupDm = false;
325a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
326b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    if (strcmp(key, "none")) {
327fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        // XXX: This is all we support for now
328fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
329d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
330b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat                             sizeof(dmDevice))) {
33197ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGE("ASEC device mapping failed (%s)", strerror(errno));
332b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            Loop::destroyByDevice(loopDevice);
333b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            unlink(asecFileName);
334b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            return -1;
335b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        }
336b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        cleanupDm = true;
337b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    } else {
338fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
339b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        strcpy(dmDevice, loopDevice);
340b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    }
341b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat
342fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    /*
343fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat     * Drop down the superblock at the end of the file
344fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat     */
345fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat
346fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    int sbfd = open(loopDevice, O_RDWR);
347fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    if (sbfd < 0) {
34897ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
349fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        if (cleanupDm) {
350d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            Devmapper::destroy(idHash);
351fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        }
352fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        Loop::destroyByDevice(loopDevice);
353fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        unlink(asecFileName);
354fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        return -1;
355fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    }
356fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat
357fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
358fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        close(sbfd);
35997ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
360fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        if (cleanupDm) {
361d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            Devmapper::destroy(idHash);
362fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        }
363fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        Loop::destroyByDevice(loopDevice);
364fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        unlink(asecFileName);
365fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        return -1;
366fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    }
367fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat
368fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
369fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        close(sbfd);
37097ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to write superblock (%s)", strerror(errno));
371fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        if (cleanupDm) {
372d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            Devmapper::destroy(idHash);
373fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        }
374fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        Loop::destroyByDevice(loopDevice);
375fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        unlink(asecFileName);
376fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat        return -1;
377fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    }
378fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat    close(sbfd);
379fcf24fe62f98c5d44431aa575555569c2c7a29b0San Mehat
380344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (wantFilesystem) {
381344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        int formatStatus;
382344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (usingExt4) {
383344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            formatStatus = Ext4::format(dmDevice);
384344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        } else {
385344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            formatStatus = Fat::format(dmDevice, numImgSectors);
386b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        }
387a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
388344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (formatStatus < 0) {
389344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            SLOGE("ASEC fs format failed (%s)", strerror(errno));
390b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            if (cleanupDm) {
391d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                Devmapper::destroy(idHash);
392b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            }
393eb13a90bb96b329d8e24a6c3d4720ae88451d301San Mehat            Loop::destroyByDevice(loopDevice);
394eb13a90bb96b329d8e24a6c3d4720ae88451d301San Mehat            unlink(asecFileName);
395eb13a90bb96b329d8e24a6c3d4720ae88451d301San Mehat            return -1;
396eb13a90bb96b329d8e24a6c3d4720ae88451d301San Mehat        }
397344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
398a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat        char mountPoint[255];
399a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat
400a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat        snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
401344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (mkdir(mountPoint, 0000)) {
402a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat            if (errno != EEXIST) {
40397ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat                SLOGE("Mountpoint creation failed (%s)", strerror(errno));
404a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat                if (cleanupDm) {
405d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                    Devmapper::destroy(idHash);
406a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat                }
407a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat                Loop::destroyByDevice(loopDevice);
408a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat                unlink(asecFileName);
409a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat                return -1;
410a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat            }
411a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat        }
412a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
413344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        int mountStatus;
414344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (usingExt4) {
415344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
416344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        } else {
417344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
418344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                    false);
419344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
420344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
421344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (mountStatus) {
42297ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
423a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat            if (cleanupDm) {
424d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                Devmapper::destroy(idHash);
425a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat            }
426a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat            Loop::destroyByDevice(loopDevice);
427a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat            unlink(asecFileName);
428a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat            return -1;
429b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        }
430344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
431344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (usingExt4) {
432344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            int dirfd = open(mountPoint, O_DIRECTORY);
433344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            if (dirfd >= 0) {
434344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                if (fchown(dirfd, ownerUid, AID_SYSTEM)
435344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                        || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
436344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                    SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
437344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                }
438344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                close(dirfd);
439344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            }
440344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
441a1091cb0c448a933068f9120fe6946c09812bfb6San Mehat    } else {
44297ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGI("Created raw secure container %s (no filesystem)", id);
443a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
44488705166ab82057090a070c6d4200c3d9db76f11San Mehat
445cbacf78eff70bd43bb899e164ec2ab409bc0904cKenny Root    mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
446a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
447a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
448a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
449a19b250bd273455933ca3502cf2c2e0a803aff77San Mehatint VolumeManager::finalizeAsec(const char *id) {
450a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char asecFileName[255];
451a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char loopDevice[255];
452a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char mountPoint[255];
453a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
454344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (findAsec(id, asecFileName, sizeof(asecFileName))) {
455344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't find ASEC %s", id);
456344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
457344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
458a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
459d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    char idHash[33];
460d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (!asecHash(id, idHash, sizeof(idHash))) {
46197ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
462d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        return -1;
463d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
464d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
465d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
46697ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
467a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
468a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
469a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
470344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    unsigned int nr_sec = 0;
471344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    struct asec_superblock sb;
472344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
473344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
474344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
475344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
476344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
4773bb6020e461e8872e8df0775cba6eb32e06b93ecSan Mehat    snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
478344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
479344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int result = 0;
480344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
481344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
482344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    } else {
483344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
484344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
485344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
486344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (result) {
48797ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
488a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
489a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
490a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
491d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (mDebug) {
49297ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGD("ASEC %s finalized", id);
493d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
494a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
495a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
496a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
497344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootint VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
498344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    char asecFileName[255];
499344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    char loopDevice[255];
500344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    char mountPoint[255];
501344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
502344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (gid < AID_APP) {
503344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Group ID is not in application range");
504344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
505344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
506344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
507344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (findAsec(id, asecFileName, sizeof(asecFileName))) {
508344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't find ASEC %s", id);
509344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
510344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
511344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
512344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    char idHash[33];
513344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (!asecHash(id, idHash, sizeof(idHash))) {
514344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
515344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
516344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
517344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
518344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
519344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
520344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
521344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
522344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
523344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    unsigned int nr_sec = 0;
524344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    struct asec_superblock sb;
525344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
526344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
527344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
528344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
529344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
530344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
531344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
532344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int result = 0;
533344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
534344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return 0;
535344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
536344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
537344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int ret = Ext4::doMount(loopDevice, mountPoint,
538344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            false /* read-only */,
539344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            true  /* remount */,
540344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            false /* executable */);
541344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (ret) {
542344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
543344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
544344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
545344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
546344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    char *paths[] = { mountPoint, NULL };
547344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
548344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
549344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (fts) {
550344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        // Traverse the entire hierarchy and chown to system UID.
551344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
552344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            // We don't care about the lost+found directory.
553344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            if (!strcmp(ftsent->fts_name, "lost+found")) {
554344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                continue;
555344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            }
556344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
557344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            /*
558344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root             * There can only be one file marked as private right now.
559344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root             * This should be more robust, but it satisfies the requirements
560344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root             * we have for right now.
561344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root             */
562344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            const bool privateFile = !strcmp(ftsent->fts_name, filename);
563344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
564344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
565344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            if (fd < 0) {
566344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
567344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                result = -1;
568344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                continue;
569344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            }
570344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
571344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
572344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
573344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            if (ftsent->fts_info & FTS_D) {
5741a673c868c2d2d81fcaeab34b4a7c75d4a978584Kenny Root                result |= fchmod(fd, 0755);
575348c8aba0d2df2996e0fe57900ef518c6aeb4b29Kenny Root            } else if (ftsent->fts_info & FTS_F) {
576344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                result |= fchmod(fd, privateFile ? 0640 : 0644);
577344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            }
578344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            close(fd);
579344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
580344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        fts_close(fts);
581344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
582344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        // Finally make the directory readable by everyone.
583344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        int dirfd = open(mountPoint, O_DIRECTORY);
584344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (dirfd < 0 || fchmod(dirfd, 0755)) {
585344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
586344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            result |= -1;
587344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
588344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        close(dirfd);
589344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    } else {
590344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        result |= -1;
591344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
592344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
593344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    result |= Ext4::doMount(loopDevice, mountPoint,
594344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            true /* read-only */,
595344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            true /* remount */,
596344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            true /* execute */);
597344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
598344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (result) {
599344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
600344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
601344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
602344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
603344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (mDebug) {
604344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGD("ASEC %s permissions fixed", id);
605344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
606344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    return 0;
607344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
608344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
609048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehatint VolumeManager::renameAsec(const char *id1, const char *id2) {
610344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    char asecFilename1[255];
611048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    char *asecFilename2;
612048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    char mountPoint[255];
613048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
614344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    const char *dir;
615344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
616344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
617344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't find ASEC %s", id1);
618344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
619344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
620344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
621344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
622048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
6233bb6020e461e8872e8df0775cba6eb32e06b93ecSan Mehat    snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
624048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    if (isMountpointMounted(mountPoint)) {
62597ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGW("Rename attempt when src mounted");
626048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat        errno = EBUSY;
627048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat        goto out_err;
628048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    }
629048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
63096956ed0e220cb62a4a96136976ded0d8c2d9075San Mehat    snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
63196956ed0e220cb62a4a96136976ded0d8c2d9075San Mehat    if (isMountpointMounted(mountPoint)) {
63297ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGW("Rename attempt when dst mounted");
63396956ed0e220cb62a4a96136976ded0d8c2d9075San Mehat        errno = EBUSY;
63496956ed0e220cb62a4a96136976ded0d8c2d9075San Mehat        goto out_err;
63596956ed0e220cb62a4a96136976ded0d8c2d9075San Mehat    }
63696956ed0e220cb62a4a96136976ded0d8c2d9075San Mehat
637048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    if (!access(asecFilename2, F_OK)) {
63897ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Rename attempt when dst exists");
639048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat        errno = EADDRINUSE;
640048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat        goto out_err;
641048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    }
642048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
643048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    if (rename(asecFilename1, asecFilename2)) {
64497ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
645048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat        goto out_err;
646048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    }
647048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
648048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    free(asecFilename2);
649048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    return 0;
650048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
651048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehatout_err:
652048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    free(asecFilename2);
653048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    return -1;
654048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat}
655048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
656fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root#define UNMOUNT_RETRIES 5
657fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
6584ba8948dc16463053e21cda5744f519a555080d0San Mehatint VolumeManager::unmountAsec(const char *id, bool force) {
659a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char asecFileName[255];
660a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char mountPoint[255];
661a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
662344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (findAsec(id, asecFileName, sizeof(asecFileName))) {
663344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't find ASEC %s", id);
664344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
665344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
666344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
6673bb6020e461e8872e8df0775cba6eb32e06b93ecSan Mehat    snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
668a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
669d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    char idHash[33];
670d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (!asecHash(id, idHash, sizeof(idHash))) {
67197ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
672d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        return -1;
673d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
674d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
675fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
676fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root}
677fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
678508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Rootint VolumeManager::unmountObb(const char *fileName, bool force) {
679fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    char mountPoint[255];
680fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
681fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    char idHash[33];
682fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    if (!asecHash(fileName, idHash, sizeof(idHash))) {
683fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
684fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        return -1;
685fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    }
686fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
687fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
688fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
689fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
690fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root}
691fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
692fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Rootint VolumeManager::unmountLoopImage(const char *id, const char *idHash,
693fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        const char *fileName, const char *mountPoint, bool force) {
6940586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat    if (!isMountpointMounted(mountPoint)) {
695fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        SLOGE("Unmount request for %s when not mounted", id);
696918e5f9f10b9c1ff929683743ffbf229027ce240Kenny Root        errno = ENOENT;
697b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        return -1;
698b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    }
69923969931fad6e993832208f099f6eea0f6f76eb5San Mehat
700b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    int i, rc;
701fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    for (i = 1; i <= UNMOUNT_RETRIES; i++) {
702b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        rc = umount(mountPoint);
703b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        if (!rc) {
704b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            break;
705a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
706b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        if (rc && (errno == EINVAL || errno == ENOENT)) {
707fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            SLOGI("Container %s unmounted OK", id);
708b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            rc = 0;
709b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            break;
710a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
711fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        SLOGW("%s unmount attempt %d failed (%s)",
7128c940ef7dbd423cadc92982b44a65ed1014389e2San Mehat              id, i, strerror(errno));
7138c940ef7dbd423cadc92982b44a65ed1014389e2San Mehat
7144ba8948dc16463053e21cda5744f519a555080d0San Mehat        int action = 0; // default is to just complain
7154ba8948dc16463053e21cda5744f519a555080d0San Mehat
7164ba8948dc16463053e21cda5744f519a555080d0San Mehat        if (force) {
717fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            if (i > (UNMOUNT_RETRIES - 2))
7184ba8948dc16463053e21cda5744f519a555080d0San Mehat                action = 2; // SIGKILL
719fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            else if (i > (UNMOUNT_RETRIES - 3))
7204ba8948dc16463053e21cda5744f519a555080d0San Mehat                action = 1; // SIGHUP
7214ba8948dc16463053e21cda5744f519a555080d0San Mehat        }
7228c940ef7dbd423cadc92982b44a65ed1014389e2San Mehat
723586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        Process::killProcessesWithOpenFiles(mountPoint, action);
724fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
725b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    }
726b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat
727b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    if (rc) {
7284ba8948dc16463053e21cda5744f519a555080d0San Mehat        errno = EBUSY;
72997ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
730b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        return -1;
731b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    }
732b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat
73312f4b89046b54de1bdc188b7057ba77d7566e573San Mehat    int retries = 10;
73412f4b89046b54de1bdc188b7057ba77d7566e573San Mehat
73512f4b89046b54de1bdc188b7057ba77d7566e573San Mehat    while(retries--) {
73612f4b89046b54de1bdc188b7057ba77d7566e573San Mehat        if (!rmdir(mountPoint)) {
73712f4b89046b54de1bdc188b7057ba77d7566e573San Mehat            break;
73812f4b89046b54de1bdc188b7057ba77d7566e573San Mehat        }
73912f4b89046b54de1bdc188b7057ba77d7566e573San Mehat
74097ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
741fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
74212f4b89046b54de1bdc188b7057ba77d7566e573San Mehat    }
74312f4b89046b54de1bdc188b7057ba77d7566e573San Mehat
74412f4b89046b54de1bdc188b7057ba77d7566e573San Mehat    if (!retries) {
74597ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
746f5c61980969a0b49bda37b5dc94ffe675ebd5a5aSan Mehat    }
74788705166ab82057090a070c6d4200c3d9db76f11San Mehat
748d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (Devmapper::destroy(idHash) && errno != ENXIO) {
74997ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
750a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
751a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
752a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char loopDevice[255];
753d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
754a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        Loop::destroyByDevice(loopDevice);
755d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    } else {
756fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
757a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
75888705166ab82057090a070c6d4200c3d9db76f11San Mehat
75988705166ab82057090a070c6d4200c3d9db76f11San Mehat    AsecIdCollection::iterator it;
76088705166ab82057090a070c6d4200c3d9db76f11San Mehat    for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
761cbacf78eff70bd43bb899e164ec2ab409bc0904cKenny Root        ContainerData* cd = *it;
762cbacf78eff70bd43bb899e164ec2ab409bc0904cKenny Root        if (!strcmp(cd->id, id)) {
76388705166ab82057090a070c6d4200c3d9db76f11San Mehat            free(*it);
76488705166ab82057090a070c6d4200c3d9db76f11San Mehat            mActiveContainers->erase(it);
76588705166ab82057090a070c6d4200c3d9db76f11San Mehat            break;
76688705166ab82057090a070c6d4200c3d9db76f11San Mehat        }
76788705166ab82057090a070c6d4200c3d9db76f11San Mehat    }
76888705166ab82057090a070c6d4200c3d9db76f11San Mehat    if (it == mActiveContainers->end()) {
76997ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGW("mActiveContainers is inconsistent!");
77088705166ab82057090a070c6d4200c3d9db76f11San Mehat    }
771b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    return 0;
772b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat}
773b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat
7744ba8948dc16463053e21cda5744f519a555080d0San Mehatint VolumeManager::destroyAsec(const char *id, bool force) {
775b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    char asecFileName[255];
776b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat    char mountPoint[255];
777b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat
778344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (findAsec(id, asecFileName, sizeof(asecFileName))) {
779344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't find ASEC %s", id);
780344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
781344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
782344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
78355013f7131ffe094e1c7d929cfc32b3b25096a9bSan Mehat    snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
784b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat
7850586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat    if (isMountpointMounted(mountPoint)) {
786d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        if (mDebug) {
78797ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGD("Unmounting container before destroy");
788d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        }
7894ba8948dc16463053e21cda5744f519a555080d0San Mehat        if (unmountAsec(id, force)) {
79097ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
7910586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat            return -1;
7920586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat        }
7930586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat    }
794a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
7950586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat    if (unlink(asecFileName)) {
79697ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
7970586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat        return -1;
7980586d54053ee00e6d6523d4f125282ccb9a24aabSan Mehat    }
799a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
800d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (mDebug) {
80197ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGD("ASEC %s destroyed", id);
802d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
803a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
804a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
805a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
806344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootbool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
807344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int dirfd = open(dir, O_DIRECTORY);
808344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (dirfd < 0) {
809344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
810344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
811344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
812344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
813344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    bool ret = false;
814344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
815344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
816344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        ret = true;
817344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
818344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
819344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    close(dirfd);
820344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
821344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    return ret;
822344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
823344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
824344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootint VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
825344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        const char **directory) const {
826344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int dirfd, fd;
827344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    const int idLen = strlen(id);
828344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    char *asecName;
829344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
830344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (asprintf(&asecName, "%s.asec", id) < 0) {
831344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Couldn't allocate string to write ASEC name");
832344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
833344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
834344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
835344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    const char *dir;
836344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
837344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        dir = Volume::SEC_ASECDIR_INT;
838344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
839344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        dir = Volume::SEC_ASECDIR_EXT;
840344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    } else {
841344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        free(asecName);
842