blob: 4c1621b3dc706ead5fb9b91fc772b24f90630322 [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
17#include <stdio.h>
San Mehatfd7f5872009-10-12 11:32:47 -070018#include <stdlib.h>
19#include <string.h>
San Mehatf1b736b2009-10-10 17:22:08 -070020#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080021#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070022#include <fts.h>
23#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080024#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/mount.h>
27
San Mehata2677e42009-12-13 10:40:18 -080028#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070029
30#define LOG_TAG "Vold"
31
Kenny Root7b18a7b2010-03-15 13:13:41 -070032#include <openssl/md5.h>
33
San Mehatf1b736b2009-10-10 17:22:08 -070034#include <cutils/log.h>
35
San Mehatfd7f5872009-10-12 11:32:47 -070036#include <sysutils/NetlinkEvent.h>
37
Kenny Root344ca102012-04-03 17:23:01 -070038#include <private/android_filesystem_config.h>
39
San Mehatf1b736b2009-10-10 17:22:08 -070040#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070041#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080042#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080043#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070044#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080045#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080046#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080047#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080048#include "Asec.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070049#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080050
Mike Lockwood97f2fc12011-06-07 10:51:38 -070051#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
52
San Mehatf1b736b2009-10-10 17:22:08 -070053VolumeManager *VolumeManager::sInstance = NULL;
54
55VolumeManager *VolumeManager::Instance() {
56 if (!sInstance)
57 sInstance = new VolumeManager();
58 return sInstance;
59}
60
61VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080062 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -070063 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -080064 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -070065 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -040066 mUmsSharingCount = 0;
67 mSavedDirtyRatio = -1;
68 // set dirty ratio to 0 when UMS is active
69 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -070070 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -070071}
72
73VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -080074 delete mVolumes;
75 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -070076}
77
Kenny Root7b18a7b2010-03-15 13:13:41 -070078char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -070079 static const char* digits = "0123456789abcdef";
80
Kenny Root7b18a7b2010-03-15 13:13:41 -070081 unsigned char sig[MD5_DIGEST_LENGTH];
82
Kenny Rootacc9e7d2010-06-18 19:06:50 -070083 if (buffer == NULL) {
84 SLOGE("Destination buffer is NULL");
85 errno = ESPIPE;
86 return NULL;
87 } else if (id == NULL) {
88 SLOGE("Source buffer is NULL");
89 errno = ESPIPE;
90 return NULL;
91 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
92 SLOGE("Target hash buffer size < %d bytes (%d)",
93 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -080094 errno = ESPIPE;
95 return NULL;
96 }
Kenny Root7b18a7b2010-03-15 13:13:41 -070097
98 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -080099
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700100 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700101 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700102 *p++ = digits[sig[i] >> 4];
103 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800104 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700105 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800106
107 return buffer;
108}
109
110void VolumeManager::setDebug(bool enable) {
111 mDebug = enable;
112 VolumeCollection::iterator it;
113 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
114 (*it)->setDebug(enable);
115 }
116}
117
San Mehatf1b736b2009-10-10 17:22:08 -0700118int VolumeManager::start() {
119 return 0;
120}
121
122int VolumeManager::stop() {
123 return 0;
124}
125
126int VolumeManager::addVolume(Volume *v) {
127 mVolumes->push_back(v);
128 return 0;
129}
130
San Mehatfd7f5872009-10-12 11:32:47 -0700131void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
132 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700133
San Mehatfd7f5872009-10-12 11:32:47 -0700134 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700135 VolumeCollection::iterator it;
136 bool hit = false;
137 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700138 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800139#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700140 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800141#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700142 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700143 break;
144 }
145 }
146
147 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800148#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700149 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800150#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700151 }
152}
153
San Mehatf1b736b2009-10-10 17:22:08 -0700154int VolumeManager::listVolumes(SocketClient *cli) {
155 VolumeCollection::iterator i;
156
157 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
158 char *buffer;
159 asprintf(&buffer, "%s %s %d",
160 (*i)->getLabel(), (*i)->getMountpoint(),
161 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800162 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700163 free(buffer);
164 }
San Mehata2677e42009-12-13 10:40:18 -0800165 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700166 return 0;
167}
San Mehat49e2bce2009-10-12 16:29:01 -0700168
San Mehata2677e42009-12-13 10:40:18 -0800169int VolumeManager::formatVolume(const char *label) {
170 Volume *v = lookupVolume(label);
171
172 if (!v) {
173 errno = ENOENT;
174 return -1;
175 }
176
Ken Sumrall3b170052011-07-11 15:38:57 -0700177 if (mVolManagerDisabled) {
178 errno = EBUSY;
179 return -1;
180 }
181
San Mehata2677e42009-12-13 10:40:18 -0800182 return v->formatVol();
183}
184
Kenny Root508c0e12010-07-12 09:59:49 -0700185int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
186 char idHash[33];
187 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
188 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
189 return -1;
190 }
191
192 memset(mountPath, 0, mountPathLen);
193 snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
194
195 if (access(mountPath, F_OK)) {
196 errno = ENOENT;
197 return -1;
198 }
199
200 return 0;
201}
202
San Mehata19b2502010-01-06 10:33:53 -0800203int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700204 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700205
206 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
207 SLOGE("Couldn't find ASEC %s", id);
208 return -1;
209 }
San Mehat88ac2c02010-03-23 11:15:58 -0700210
211 memset(buffer, 0, maxlen);
212 if (access(asecFileName, F_OK)) {
213 errno = ENOENT;
214 return -1;
215 }
San Mehata19b2502010-01-06 10:33:53 -0800216
San Mehat3bb60202010-02-19 18:14:36 -0800217 snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800218 return 0;
219}
220
Dianne Hackborn736910c2011-06-27 13:37:07 -0700221int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
222 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700223
224 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
225 SLOGE("Couldn't find ASEC %s", id);
226 return -1;
227 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700228
229 memset(buffer, 0, maxlen);
230 if (access(asecFileName, F_OK)) {
231 errno = ENOENT;
232 return -1;
233 }
234
235 snprintf(buffer, maxlen, "%s", asecFileName);
236 return 0;
237}
238
Kenny Root344ca102012-04-03 17:23:01 -0700239int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
240 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800241 struct asec_superblock sb;
242 memset(&sb, 0, sizeof(sb));
243
Kenny Root344ca102012-04-03 17:23:01 -0700244 const bool wantFilesystem = strcmp(fstype, "none");
245 bool usingExt4 = false;
246 if (wantFilesystem) {
247 usingExt4 = !strcmp(fstype, "ext4");
248 if (usingExt4) {
249 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
250 } else if (strcmp(fstype, "fat")) {
251 SLOGE("Invalid filesystem type %s", fstype);
252 errno = EINVAL;
253 return -1;
254 }
255 }
256
San Mehatfcf24fe2010-03-03 12:37:32 -0800257 sb.magic = ASEC_SB_MAGIC;
258 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800259
San Mehatd31e3802010-02-18 08:37:45 -0800260 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700261 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800262 errno = EINVAL;
263 return -1;
264 }
265
San Mehata19b2502010-01-06 10:33:53 -0800266 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700267 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800268 errno = EADDRINUSE;
269 return -1;
270 }
271
272 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700273
274 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
275 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
276 asecFileName, strerror(errno));
277 errno = EADDRINUSE;
278 return -1;
279 }
280
281 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
282
283 snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
San Mehata19b2502010-01-06 10:33:53 -0800284
285 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700286 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700287 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800288 errno = EADDRINUSE;
289 return -1;
290 }
291
San Mehatfcf24fe2010-03-03 12:37:32 -0800292 /*
293 * Add some headroom
294 */
295 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
296 unsigned numImgSectors = numSectors + fatSize + 2;
297
298 if (numImgSectors % 63) {
299 numImgSectors += (63 - (numImgSectors % 63));
300 }
301
302 // Add +1 for our superblock which is at the end
303 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700304 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800305 return -1;
306 }
307
San Mehatd9a4e352010-03-12 13:32:47 -0800308 char idHash[33];
309 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700310 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800311 unlink(asecFileName);
312 return -1;
313 }
314
San Mehata19b2502010-01-06 10:33:53 -0800315 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800316 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700317 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800318 unlink(asecFileName);
319 return -1;
320 }
321
San Mehatb78a32c2010-01-10 13:02:12 -0800322 char dmDevice[255];
323 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800324
San Mehatb78a32c2010-01-10 13:02:12 -0800325 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800326 // XXX: This is all we support for now
327 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800328 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800329 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700330 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800331 Loop::destroyByDevice(loopDevice);
332 unlink(asecFileName);
333 return -1;
334 }
335 cleanupDm = true;
336 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800337 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800338 strcpy(dmDevice, loopDevice);
339 }
340
San Mehatfcf24fe2010-03-03 12:37:32 -0800341 /*
342 * Drop down the superblock at the end of the file
343 */
344
345 int sbfd = open(loopDevice, O_RDWR);
346 if (sbfd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700347 SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800348 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800349 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800350 }
351 Loop::destroyByDevice(loopDevice);
352 unlink(asecFileName);
353 return -1;
354 }
355
356 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
357 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700358 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800359 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800360 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800361 }
362 Loop::destroyByDevice(loopDevice);
363 unlink(asecFileName);
364 return -1;
365 }
366
367 if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
368 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700369 SLOGE("Failed to write superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800370 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800371 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800372 }
373 Loop::destroyByDevice(loopDevice);
374 unlink(asecFileName);
375 return -1;
376 }
377 close(sbfd);
378
Kenny Root344ca102012-04-03 17:23:01 -0700379 if (wantFilesystem) {
380 int formatStatus;
381 if (usingExt4) {
382 formatStatus = Ext4::format(dmDevice);
383 } else {
384 formatStatus = Fat::format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800385 }
San Mehata19b2502010-01-06 10:33:53 -0800386
Kenny Root344ca102012-04-03 17:23:01 -0700387 if (formatStatus < 0) {
388 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800389 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800390 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800391 }
San Mehateb13a902010-01-07 12:12:50 -0800392 Loop::destroyByDevice(loopDevice);
393 unlink(asecFileName);
394 return -1;
395 }
Kenny Root344ca102012-04-03 17:23:01 -0700396
San Mehata1091cb2010-02-28 20:17:20 -0800397 char mountPoint[255];
San Mehata19b2502010-01-06 10:33:53 -0800398
San Mehata1091cb2010-02-28 20:17:20 -0800399 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
Kenny Root344ca102012-04-03 17:23:01 -0700400 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800401 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700402 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800403 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800404 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800405 }
406 Loop::destroyByDevice(loopDevice);
407 unlink(asecFileName);
408 return -1;
409 }
San Mehatb78a32c2010-01-10 13:02:12 -0800410 }
San Mehata1091cb2010-02-28 20:17:20 -0800411
Kenny Root344ca102012-04-03 17:23:01 -0700412 int mountStatus;
413 if (usingExt4) {
414 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
415 } else {
416 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
417 false);
418 }
419
420 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700421 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800422 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800423 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800424 }
425 Loop::destroyByDevice(loopDevice);
426 unlink(asecFileName);
427 return -1;
428 }
Kenny Root344ca102012-04-03 17:23:01 -0700429
430 if (usingExt4) {
431 int dirfd = open(mountPoint, O_DIRECTORY);
432 if (dirfd >= 0) {
433 if (fchown(dirfd, ownerUid, AID_SYSTEM)
434 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
435 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
436 }
437 close(dirfd);
438 }
439 }
San Mehata1091cb2010-02-28 20:17:20 -0800440 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700441 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800442 }
San Mehat88705162010-01-15 09:26:28 -0800443
Kenny Rootcbacf782010-09-24 15:11:48 -0700444 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800445 return 0;
446}
447
448int VolumeManager::finalizeAsec(const char *id) {
449 char asecFileName[255];
450 char loopDevice[255];
451 char mountPoint[255];
452
Kenny Root344ca102012-04-03 17:23:01 -0700453 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
454 SLOGE("Couldn't find ASEC %s", id);
455 return -1;
456 }
San Mehata19b2502010-01-06 10:33:53 -0800457
San Mehatd9a4e352010-03-12 13:32:47 -0800458 char idHash[33];
459 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700460 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800461 return -1;
462 }
463
464 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700465 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800466 return -1;
467 }
468
Kenny Root344ca102012-04-03 17:23:01 -0700469 unsigned int nr_sec = 0;
470 struct asec_superblock sb;
471
472 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
473 return -1;
474 }
475
San Mehat3bb60202010-02-19 18:14:36 -0800476 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
Kenny Root344ca102012-04-03 17:23:01 -0700477
478 int result = 0;
479 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
480 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
481 } else {
482 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
483 }
484
485 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700486 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800487 return -1;
488 }
489
San Mehatd9a4e352010-03-12 13:32:47 -0800490 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700491 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800492 }
San Mehata19b2502010-01-06 10:33:53 -0800493 return 0;
494}
495
Kenny Root344ca102012-04-03 17:23:01 -0700496int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
497 char asecFileName[255];
498 char loopDevice[255];
499 char mountPoint[255];
500
501 if (gid < AID_APP) {
502 SLOGE("Group ID is not in application range");
503 return -1;
504 }
505
506 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
507 SLOGE("Couldn't find ASEC %s", id);
508 return -1;
509 }
510
511 char idHash[33];
512 if (!asecHash(id, idHash, sizeof(idHash))) {
513 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
514 return -1;
515 }
516
517 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
518 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
519 return -1;
520 }
521
522 unsigned int nr_sec = 0;
523 struct asec_superblock sb;
524
525 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
526 return -1;
527 }
528
529 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
530
531 int result = 0;
532 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
533 return 0;
534 }
535
536 int ret = Ext4::doMount(loopDevice, mountPoint,
537 false /* read-only */,
538 true /* remount */,
539 false /* executable */);
540 if (ret) {
541 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
542 return -1;
543 }
544
545 char *paths[] = { mountPoint, NULL };
546
547 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
548 if (fts) {
549 // Traverse the entire hierarchy and chown to system UID.
550 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
551 // We don't care about the lost+found directory.
552 if (!strcmp(ftsent->fts_name, "lost+found")) {
553 continue;
554 }
555
556 /*
557 * There can only be one file marked as private right now.
558 * This should be more robust, but it satisfies the requirements
559 * we have for right now.
560 */
561 const bool privateFile = !strcmp(ftsent->fts_name, filename);
562
563 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
564 if (fd < 0) {
565 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
566 result = -1;
567 continue;
568 }
569
570 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
571
572 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700573 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700574 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700575 result |= fchmod(fd, privateFile ? 0640 : 0644);
576 }
577 close(fd);
578 }
579 fts_close(fts);
580
581 // Finally make the directory readable by everyone.
582 int dirfd = open(mountPoint, O_DIRECTORY);
583 if (dirfd < 0 || fchmod(dirfd, 0755)) {
584 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
585 result |= -1;
586 }
587 close(dirfd);
588 } else {
589 result |= -1;
590 }
591
592 result |= Ext4::doMount(loopDevice, mountPoint,
593 true /* read-only */,
594 true /* remount */,
595 true /* execute */);
596
597 if (result) {
598 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
599 return -1;
600 }
601
602 if (mDebug) {
603 SLOGD("ASEC %s permissions fixed", id);
604 }
605 return 0;
606}
607
San Mehat048b0802010-01-23 08:17:06 -0800608int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700609 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800610 char *asecFilename2;
611 char mountPoint[255];
612
Kenny Root344ca102012-04-03 17:23:01 -0700613 const char *dir;
614
615 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
616 SLOGE("Couldn't find ASEC %s", id1);
617 return -1;
618 }
619
620 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800621
San Mehat3bb60202010-02-19 18:14:36 -0800622 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
San Mehat048b0802010-01-23 08:17:06 -0800623 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700624 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800625 errno = EBUSY;
626 goto out_err;
627 }
628
San Mehat96956ed2010-02-24 08:42:51 -0800629 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
630 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700631 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800632 errno = EBUSY;
633 goto out_err;
634 }
635
San Mehat048b0802010-01-23 08:17:06 -0800636 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700637 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800638 errno = EADDRINUSE;
639 goto out_err;
640 }
641
642 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700643 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800644 goto out_err;
645 }
646
San Mehat048b0802010-01-23 08:17:06 -0800647 free(asecFilename2);
648 return 0;
649
650out_err:
San Mehat048b0802010-01-23 08:17:06 -0800651 free(asecFilename2);
652 return -1;
653}
654
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700655#define UNMOUNT_RETRIES 5
656#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800657int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800658 char asecFileName[255];
659 char mountPoint[255];
660
Kenny Root344ca102012-04-03 17:23:01 -0700661 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
662 SLOGE("Couldn't find ASEC %s", id);
663 return -1;
664 }
665
San Mehat3bb60202010-02-19 18:14:36 -0800666 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800667
San Mehatd9a4e352010-03-12 13:32:47 -0800668 char idHash[33];
669 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700670 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800671 return -1;
672 }
673
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700674 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
675}
676
Kenny Root508c0e12010-07-12 09:59:49 -0700677int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700678 char mountPoint[255];
679
680 char idHash[33];
681 if (!asecHash(fileName, idHash, sizeof(idHash))) {
682 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
683 return -1;
684 }
685
686 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
687
688 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
689}
690
691int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
692 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -0800693 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700694 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -0700695 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -0800696 return -1;
697 }
San Mehat23969932010-01-09 07:08:06 -0800698
San Mehatb78a32c2010-01-10 13:02:12 -0800699 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700700 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -0800701 rc = umount(mountPoint);
702 if (!rc) {
703 break;
San Mehata19b2502010-01-06 10:33:53 -0800704 }
San Mehatb78a32c2010-01-10 13:02:12 -0800705 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700706 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800707 rc = 0;
708 break;
San Mehata19b2502010-01-06 10:33:53 -0800709 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700710 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -0800711 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800712
San Mehat4ba89482010-02-18 09:00:18 -0800713 int action = 0; // default is to just complain
714
715 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700716 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -0800717 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700718 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -0800719 action = 1; // SIGHUP
720 }
San Mehat8c940ef2010-02-13 14:19:53 -0800721
San Mehat586536c2010-02-16 17:12:00 -0800722 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700723 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -0800724 }
725
726 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -0800727 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700728 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800729 return -1;
730 }
731
San Mehat12f4b892010-02-24 11:43:22 -0800732 int retries = 10;
733
734 while(retries--) {
735 if (!rmdir(mountPoint)) {
736 break;
737 }
738
San Mehat97ac40e2010-03-24 10:24:19 -0700739 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700740 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -0800741 }
742
743 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -0700744 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -0800745 }
San Mehat88705162010-01-15 09:26:28 -0800746
San Mehatd9a4e352010-03-12 13:32:47 -0800747 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700748 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800749 }
750
751 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800752 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800753 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800754 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700755 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800756 }
San Mehat88705162010-01-15 09:26:28 -0800757
758 AsecIdCollection::iterator it;
759 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -0700760 ContainerData* cd = *it;
761 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -0800762 free(*it);
763 mActiveContainers->erase(it);
764 break;
765 }
766 }
767 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700768 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -0800769 }
San Mehatb78a32c2010-01-10 13:02:12 -0800770 return 0;
771}
772
San Mehat4ba89482010-02-18 09:00:18 -0800773int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -0800774 char asecFileName[255];
775 char mountPoint[255];
776
Kenny Root344ca102012-04-03 17:23:01 -0700777 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
778 SLOGE("Couldn't find ASEC %s", id);
779 return -1;
780 }
781
San Mehat55013f72010-02-24 12:12:34 -0800782 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehatb78a32c2010-01-10 13:02:12 -0800783
San Mehat0586d542010-01-12 15:38:59 -0800784 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800785 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700786 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -0800787 }
San Mehat4ba89482010-02-18 09:00:18 -0800788 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700789 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800790 return -1;
791 }
792 }
San Mehata19b2502010-01-06 10:33:53 -0800793
San Mehat0586d542010-01-12 15:38:59 -0800794 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700795 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800796 return -1;
797 }
San Mehata19b2502010-01-06 10:33:53 -0800798
San Mehatd9a4e352010-03-12 13:32:47 -0800799 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700800 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800801 }
San Mehata19b2502010-01-06 10:33:53 -0800802 return 0;
803}
804
Kenny Root344ca102012-04-03 17:23:01 -0700805bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
806 int dirfd = open(dir, O_DIRECTORY);
807 if (dirfd < 0) {
808 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
809 return -1;
810 }
811
812 bool ret = false;
813
814 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
815 ret = true;
816 }
817
818 close(dirfd);
819
820 return ret;
821}
822
823int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
824 const char **directory) const {
825 int dirfd, fd;
826 const int idLen = strlen(id);
827 char *asecName;
828
829 if (asprintf(&asecName, "%s.asec", id) < 0) {
830 SLOGE("Couldn't allocate string to write ASEC name");
831 return -1;
832 }
833
834 const char *dir;
835 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
836 dir = Volume::SEC_ASECDIR_INT;
837 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
838 dir = Volume::SEC_ASECDIR_EXT;
839 } else {
840 free(asecName);
841 return -1;
842 }
843
844 if (directory != NULL) {
845 *directory = dir;
846 }
847
848 if (asecPath != NULL) {
849 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
850 if (written < 0 || static_cast<size_t>(written) >= asecPathLen) {
851 free(asecName);
852 return -1;
853 }
854 }
855
856 free(asecName);
857 return 0;
858}
859
San Mehata19b2502010-01-06 10:33:53 -0800860int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
861 char asecFileName[255];
862 char mountPoint[255];
863
Kenny Root344ca102012-04-03 17:23:01 -0700864 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
865 SLOGE("Couldn't find ASEC %s", id);
866 return -1;
867 }
868
San Mehat3bb60202010-02-19 18:14:36 -0800869 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
San Mehata19b2502010-01-06 10:33:53 -0800870
871 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700872 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -0800873 errno = EBUSY;
874 return -1;
875 }
876
San Mehatd9a4e352010-03-12 13:32:47 -0800877 char idHash[33];
878 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700879 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800880 return -1;
881 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700882
San Mehata19b2502010-01-06 10:33:53 -0800883 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800884 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
885 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700886 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800887 return -1;
888 }
San Mehatd9a4e352010-03-12 13:32:47 -0800889 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700890 SLOGD("New loop device created at %s", loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800891 }
San Mehatb78a32c2010-01-10 13:02:12 -0800892 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800893 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700894 SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800895 }
San Mehatb78a32c2010-01-10 13:02:12 -0800896 }
897
898 char dmDevice[255];
899 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -0800900 int fd;
901 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -0800902 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -0800903
Kenny Root344ca102012-04-03 17:23:01 -0700904 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
905 return -1;
906 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800907
San Mehatd9a4e352010-03-12 13:32:47 -0800908 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700909 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -0800910 }
San Mehatfcf24fe2010-03-03 12:37:32 -0800911 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -0700912 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -0800913 Loop::destroyByDevice(loopDevice);
914 errno = EMEDIUMTYPE;
915 return -1;
916 }
917 nr_sec--; // We don't want the devmapping to extend onto our superblock
918
San Mehatb78a32c2010-01-10 13:02:12 -0800919 if (strcmp(key, "none")) {
San Mehatd9a4e352010-03-12 13:32:47 -0800920 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
921 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -0800922 dmDevice, sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700923 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800924 Loop::destroyByDevice(loopDevice);
925 return -1;
926 }
San Mehatd9a4e352010-03-12 13:32:47 -0800927 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700928 SLOGD("New devmapper instance created at %s", dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800929 }
San Mehatb78a32c2010-01-10 13:02:12 -0800930 } else {
San Mehatd9a4e352010-03-12 13:32:47 -0800931 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700932 SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800933 }
San Mehatb78a32c2010-01-10 13:02:12 -0800934 }
935 cleanupDm = true;
936 } else {
937 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800938 }
939
Kenny Root344ca102012-04-03 17:23:01 -0700940 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -0800941 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700942 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800943 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800944 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800945 }
946 Loop::destroyByDevice(loopDevice);
947 return -1;
948 }
San Mehata19b2502010-01-06 10:33:53 -0800949 }
950
Kenny Root344ca102012-04-03 17:23:01 -0700951 int result;
952 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
953 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
954 } else {
955 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
956 }
957
958 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700959 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800960 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800961 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800962 }
963 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800964 return -1;
965 }
966
Kenny Rootcbacf782010-09-24 15:11:48 -0700967 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -0800968 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700969 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800970 }
San Mehata19b2502010-01-06 10:33:53 -0800971 return 0;
972}
973
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700974/**
975 * Mounts an image file <code>img</code>.
976 */
Kenny Root508c0e12010-07-12 09:59:49 -0700977int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700978 char mountPoint[255];
979
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700980 char idHash[33];
981 if (!asecHash(img, idHash, sizeof(idHash))) {
982 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
983 return -1;
984 }
985
986 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
987
988 if (isMountpointMounted(mountPoint)) {
989 SLOGE("Image %s already mounted", img);
990 errno = EBUSY;
991 return -1;
992 }
993
994 char loopDevice[255];
995 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
996 if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) {
997 SLOGE("Image loop device creation failed (%s)", strerror(errno));
998 return -1;
999 }
1000 if (mDebug) {
1001 SLOGD("New loop device created at %s", loopDevice);
1002 }
1003 } else {
1004 if (mDebug) {
1005 SLOGD("Found active loopback for %s at %s", img, loopDevice);
1006 }
1007 }
1008
1009 char dmDevice[255];
1010 bool cleanupDm = false;
1011 int fd;
1012 unsigned int nr_sec = 0;
1013
1014 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1015 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1016 Loop::destroyByDevice(loopDevice);
1017 return -1;
1018 }
1019
1020 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1021 SLOGE("Failed to get loop size (%s)", strerror(errno));
1022 Loop::destroyByDevice(loopDevice);
1023 close(fd);
1024 return -1;
1025 }
1026
1027 close(fd);
1028
1029 if (strcmp(key, "none")) {
1030 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
1031 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
1032 dmDevice, sizeof(dmDevice))) {
1033 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
1034 Loop::destroyByDevice(loopDevice);
1035 return -1;
1036 }
1037 if (mDebug) {
1038 SLOGD("New devmapper instance created at %s", dmDevice);
1039 }
1040 } else {
1041 if (mDebug) {
1042 SLOGD("Found active devmapper for %s at %s", img, dmDevice);
1043 }
1044 }
1045 cleanupDm = true;
1046 } else {
1047 strcpy(dmDevice, loopDevice);
1048 }
1049
1050 if (mkdir(mountPoint, 0755)) {
1051 if (errno != EEXIST) {
1052 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1053 if (cleanupDm) {
1054 Devmapper::destroy(idHash);
1055 }
1056 Loop::destroyByDevice(loopDevice);
1057 return -1;
1058 }
1059 }
1060
Kenny Roota3e06082010-08-27 08:31:35 -07001061 if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001062 0227, false)) {
1063 SLOGE("Image mount failed (%s)", strerror(errno));
1064 if (cleanupDm) {
1065 Devmapper::destroy(idHash);
1066 }
1067 Loop::destroyByDevice(loopDevice);
1068 return -1;
1069 }
1070
Kenny Rootcbacf782010-09-24 15:11:48 -07001071 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001072 if (mDebug) {
1073 SLOGD("Image %s mounted", img);
1074 }
1075 return 0;
1076}
1077
San Mehat49e2bce2009-10-12 16:29:01 -07001078int VolumeManager::mountVolume(const char *label) {
1079 Volume *v = lookupVolume(label);
1080
1081 if (!v) {
1082 errno = ENOENT;
1083 return -1;
1084 }
1085
San Mehata2677e42009-12-13 10:40:18 -08001086 return v->mountVol();
1087}
1088
Kenny Root508c0e12010-07-12 09:59:49 -07001089int VolumeManager::listMountedObbs(SocketClient* cli) {
1090 char device[256];
1091 char mount_path[256];
1092 char rest[256];
1093 FILE *fp;
1094 char line[1024];
1095
1096 if (!(fp = fopen("/proc/mounts", "r"))) {
1097 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1098 return -1;
1099 }
1100
1101 // Create a string to compare against that has a trailing slash
1102 int loopDirLen = sizeof(Volume::LOOPDIR);
1103 char loopDir[loopDirLen + 2];
1104 strcpy(loopDir, Volume::LOOPDIR);
1105 loopDir[loopDirLen++] = '/';
1106 loopDir[loopDirLen] = '\0';
1107
1108 while(fgets(line, sizeof(line), fp)) {
1109 line[strlen(line)-1] = '\0';
1110
1111 /*
1112 * Should look like:
1113 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1114 */
1115 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1116
1117 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1118 int fd = open(device, O_RDONLY);
1119 if (fd >= 0) {
1120 struct loop_info64 li;
1121 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1122 cli->sendMsg(ResponseCode::AsecListResult,
1123 (const char*) li.lo_file_name, false);
1124 }
1125 close(fd);
1126 }
1127 }
1128 }
1129
1130 fclose(fp);
1131 return 0;
1132}
1133
San Mehateba65e92010-01-29 05:15:16 -08001134int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1135 Volume *v = lookupVolume(label);
1136
1137 if (!v) {
1138 errno = ENOENT;
1139 return -1;
1140 }
1141
1142 if (strcmp(method, "ums")) {
1143 errno = ENOSYS;
1144 return -1;
1145 }
1146
1147 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001148 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001149 } else {
1150 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001151 }
1152 return 0;
1153}
1154
San Mehata2677e42009-12-13 10:40:18 -08001155int VolumeManager::shareVolume(const char *label, const char *method) {
1156 Volume *v = lookupVolume(label);
1157
1158 if (!v) {
1159 errno = ENOENT;
1160 return -1;
1161 }
1162
1163 /*
1164 * Eventually, we'll want to support additional share back-ends,
1165 * some of which may work while the media is mounted. For now,
1166 * we just support UMS
1167 */
1168 if (strcmp(method, "ums")) {
1169 errno = ENOSYS;
1170 return -1;
1171 }
1172
1173 if (v->getState() == Volume::State_NoMedia) {
1174 errno = ENODEV;
1175 return -1;
1176 }
1177
San Mehat49e2bce2009-10-12 16:29:01 -07001178 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001179 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001180 errno = EBUSY;
1181 return -1;
1182 }
1183
Ken Sumrall3b170052011-07-11 15:38:57 -07001184 if (mVolManagerDisabled) {
1185 errno = EBUSY;
1186 return -1;
1187 }
1188
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001189 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001190 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1191 // This volume does not support raw disk access
1192 errno = EINVAL;
1193 return -1;
1194 }
1195
1196 int fd;
1197 char nodepath[255];
1198 snprintf(nodepath,
1199 sizeof(nodepath), "/dev/block/vold/%d:%d",
1200 MAJOR(d), MINOR(d));
1201
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001202 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001203 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001204 return -1;
1205 }
1206
1207 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001208 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001209 close(fd);
1210 return -1;
1211 }
1212
1213 close(fd);
1214 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001215 if (mUmsSharingCount++ == 0) {
1216 FILE* fp;
1217 mSavedDirtyRatio = -1; // in case we fail
1218 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1219 char line[16];
1220 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1221 fprintf(fp, "%d\n", mUmsDirtyRatio);
1222 } else {
1223 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1224 }
1225 fclose(fp);
1226 } else {
1227 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1228 }
1229 }
San Mehata2677e42009-12-13 10:40:18 -08001230 return 0;
1231}
1232
1233int VolumeManager::unshareVolume(const char *label, const char *method) {
1234 Volume *v = lookupVolume(label);
1235
1236 if (!v) {
1237 errno = ENOENT;
1238 return -1;
1239 }
1240
1241 if (strcmp(method, "ums")) {
1242 errno = ENOSYS;
1243 return -1;
1244 }
1245
1246 if (v->getState() != Volume::State_Shared) {
1247 errno = EINVAL;
1248 return -1;
1249 }
1250
San Mehata2677e42009-12-13 10:40:18 -08001251 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001252 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001253 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001254 return -1;
1255 }
1256
1257 char ch = 0;
1258 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001259 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001260 close(fd);
1261 return -1;
1262 }
1263
1264 close(fd);
1265 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001266 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1267 FILE* fp;
1268 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1269 fprintf(fp, "%d\n", mSavedDirtyRatio);
1270 fclose(fp);
1271 } else {
1272 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1273 }
1274 mSavedDirtyRatio = -1;
1275 }
San Mehata2677e42009-12-13 10:40:18 -08001276 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001277}
1278
Ken Sumrall3b170052011-07-11 15:38:57 -07001279extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001280 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001281 vm->disableVolumeManager();
1282 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001283 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001284}
1285
1286extern "C" int vold_getNumDirectVolumes(void) {
1287 VolumeManager *vm = VolumeManager::Instance();
1288 return vm->getNumDirectVolumes();
1289}
1290
1291int VolumeManager::getNumDirectVolumes(void) {
1292 VolumeCollection::iterator i;
1293 int n=0;
1294
1295 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1296 if ((*i)->getShareDevice() != (dev_t)0) {
1297 n++;
1298 }
1299 }
1300 return n;
1301}
1302
1303extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1304 VolumeManager *vm = VolumeManager::Instance();
1305 return vm->getDirectVolumeList(vol_list);
1306}
1307
1308int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1309 VolumeCollection::iterator i;
1310 int n=0;
1311 dev_t d;
1312
1313 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1314 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1315 (*i)->getVolInfo(&vol_list[n]);
1316 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
1317 "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d));
1318 n++;
1319 }
1320 }
1321
1322 return 0;
1323}
1324
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001325int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001326 Volume *v = lookupVolume(label);
1327
1328 if (!v) {
1329 errno = ENOENT;
1330 return -1;
1331 }
1332
San Mehata2677e42009-12-13 10:40:18 -08001333 if (v->getState() == Volume::State_NoMedia) {
1334 errno = ENODEV;
1335 return -1;
1336 }
1337
San Mehat49e2bce2009-10-12 16:29:01 -07001338 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001339 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001340 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001341 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001342 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001343 }
1344
San Mehat1a06eda2010-04-15 12:58:50 -07001345 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001346
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001347 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001348}
1349
San Mehata2677e42009-12-13 10:40:18 -08001350/*
1351 * Looks up a volume by it's label or mount-point
1352 */
San Mehat49e2bce2009-10-12 16:29:01 -07001353Volume *VolumeManager::lookupVolume(const char *label) {
1354 VolumeCollection::iterator i;
1355
1356 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001357 if (label[0] == '/') {
1358 if (!strcmp(label, (*i)->getMountpoint()))
1359 return (*i);
1360 } else {
1361 if (!strcmp(label, (*i)->getLabel()))
1362 return (*i);
1363 }
San Mehat49e2bce2009-10-12 16:29:01 -07001364 }
1365 return NULL;
1366}
San Mehata19b2502010-01-06 10:33:53 -08001367
1368bool VolumeManager::isMountpointMounted(const char *mp)
1369{
1370 char device[256];
1371 char mount_path[256];
1372 char rest[256];
1373 FILE *fp;
1374 char line[1024];
1375
1376 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001377 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001378 return false;
1379 }
1380
1381 while(fgets(line, sizeof(line), fp)) {
1382 line[strlen(line)-1] = '\0';
1383 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1384 if (!strcmp(mount_path, mp)) {
1385 fclose(fp);
1386 return true;
1387 }
San Mehata19b2502010-01-06 10:33:53 -08001388 }
1389
1390 fclose(fp);
1391 return false;
1392}
1393
San Mehat1a06eda2010-04-15 12:58:50 -07001394int VolumeManager::cleanupAsec(Volume *v, bool force) {
1395 while(mActiveContainers->size()) {
1396 AsecIdCollection::iterator it = mActiveContainers->begin();
Kenny Rootcbacf782010-09-24 15:11:48 -07001397 ContainerData* cd = *it;
1398 SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
1399 if (cd->type == ASEC) {
1400 if (unmountAsec(cd->id, force)) {
1401 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1402 return -1;
1403 }
1404 } else if (cd->type == OBB) {
1405 if (unmountObb(cd->id, force)) {
1406 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1407 return -1;
1408 }
1409 } else {
1410 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001411 return -1;
1412 }
1413 }
1414 return 0;
1415}
1416