blob: 76febed6b96b84db8e24af5879d94653a3d59310 [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) {
573 result |= fchmod(fd, 0711);
574 } else {
575 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 Rootcdc2a1c2012-05-03 13:49:46 -0700951 /*
952 * The device mapper node needs to be created. Sometimes it takes a
953 * while. Wait for up to 1 second. We could also inspect incoming uevents,
954 * but that would take more effort.
955 */
956 int tries = 25;
957 while (tries--) {
958 if (!access(dmDevice, F_OK) || errno != ENOENT) {
959 break;
960 }
961 usleep(40 * 1000);
962 }
963
Kenny Root344ca102012-04-03 17:23:01 -0700964 int result;
965 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
966 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
967 } else {
968 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
969 }
970
971 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700972 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800973 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800974 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800975 }
976 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -0800977 return -1;
978 }
979
Kenny Rootcbacf782010-09-24 15:11:48 -0700980 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -0800981 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700982 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800983 }
San Mehata19b2502010-01-06 10:33:53 -0800984 return 0;
985}
986
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700987/**
988 * Mounts an image file <code>img</code>.
989 */
Kenny Root508c0e12010-07-12 09:59:49 -0700990int VolumeManager::mountObb(const char *img, const char *key, int ownerUid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700991 char mountPoint[255];
992
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700993 char idHash[33];
994 if (!asecHash(img, idHash, sizeof(idHash))) {
995 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
996 return -1;
997 }
998
999 snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1000
1001 if (isMountpointMounted(mountPoint)) {
1002 SLOGE("Image %s already mounted", img);
1003 errno = EBUSY;
1004 return -1;
1005 }
1006
1007 char loopDevice[255];
1008 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1009 if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) {
1010 SLOGE("Image loop device creation failed (%s)", strerror(errno));
1011 return -1;
1012 }
1013 if (mDebug) {
1014 SLOGD("New loop device created at %s", loopDevice);
1015 }
1016 } else {
1017 if (mDebug) {
1018 SLOGD("Found active loopback for %s at %s", img, loopDevice);
1019 }
1020 }
1021
1022 char dmDevice[255];
1023 bool cleanupDm = false;
1024 int fd;
1025 unsigned int nr_sec = 0;
1026
1027 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1028 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1029 Loop::destroyByDevice(loopDevice);
1030 return -1;
1031 }
1032
1033 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1034 SLOGE("Failed to get loop size (%s)", strerror(errno));
1035 Loop::destroyByDevice(loopDevice);
1036 close(fd);
1037 return -1;
1038 }
1039
1040 close(fd);
1041
1042 if (strcmp(key, "none")) {
1043 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
1044 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
1045 dmDevice, sizeof(dmDevice))) {
1046 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
1047 Loop::destroyByDevice(loopDevice);
1048 return -1;
1049 }
1050 if (mDebug) {
1051 SLOGD("New devmapper instance created at %s", dmDevice);
1052 }
1053 } else {
1054 if (mDebug) {
1055 SLOGD("Found active devmapper for %s at %s", img, dmDevice);
1056 }
1057 }
1058 cleanupDm = true;
1059 } else {
1060 strcpy(dmDevice, loopDevice);
1061 }
1062
1063 if (mkdir(mountPoint, 0755)) {
1064 if (errno != EEXIST) {
1065 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1066 if (cleanupDm) {
1067 Devmapper::destroy(idHash);
1068 }
1069 Loop::destroyByDevice(loopDevice);
1070 return -1;
1071 }
1072 }
1073
Kenny Roota3e06082010-08-27 08:31:35 -07001074 if (Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001075 0227, false)) {
1076 SLOGE("Image mount failed (%s)", strerror(errno));
1077 if (cleanupDm) {
1078 Devmapper::destroy(idHash);
1079 }
1080 Loop::destroyByDevice(loopDevice);
1081 return -1;
1082 }
1083
Kenny Rootcbacf782010-09-24 15:11:48 -07001084 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001085 if (mDebug) {
1086 SLOGD("Image %s mounted", img);
1087 }
1088 return 0;
1089}
1090
San Mehat49e2bce2009-10-12 16:29:01 -07001091int VolumeManager::mountVolume(const char *label) {
1092 Volume *v = lookupVolume(label);
1093
1094 if (!v) {
1095 errno = ENOENT;
1096 return -1;
1097 }
1098
San Mehata2677e42009-12-13 10:40:18 -08001099 return v->mountVol();
1100}
1101
Kenny Root508c0e12010-07-12 09:59:49 -07001102int VolumeManager::listMountedObbs(SocketClient* cli) {
1103 char device[256];
1104 char mount_path[256];
1105 char rest[256];
1106 FILE *fp;
1107 char line[1024];
1108
1109 if (!(fp = fopen("/proc/mounts", "r"))) {
1110 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1111 return -1;
1112 }
1113
1114 // Create a string to compare against that has a trailing slash
1115 int loopDirLen = sizeof(Volume::LOOPDIR);
1116 char loopDir[loopDirLen + 2];
1117 strcpy(loopDir, Volume::LOOPDIR);
1118 loopDir[loopDirLen++] = '/';
1119 loopDir[loopDirLen] = '\0';
1120
1121 while(fgets(line, sizeof(line), fp)) {
1122 line[strlen(line)-1] = '\0';
1123
1124 /*
1125 * Should look like:
1126 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1127 */
1128 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1129
1130 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1131 int fd = open(device, O_RDONLY);
1132 if (fd >= 0) {
1133 struct loop_info64 li;
1134 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1135 cli->sendMsg(ResponseCode::AsecListResult,
1136 (const char*) li.lo_file_name, false);
1137 }
1138 close(fd);
1139 }
1140 }
1141 }
1142
1143 fclose(fp);
1144 return 0;
1145}
1146
San Mehateba65e92010-01-29 05:15:16 -08001147int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1148 Volume *v = lookupVolume(label);
1149
1150 if (!v) {
1151 errno = ENOENT;
1152 return -1;
1153 }
1154
1155 if (strcmp(method, "ums")) {
1156 errno = ENOSYS;
1157 return -1;
1158 }
1159
1160 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001161 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001162 } else {
1163 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001164 }
1165 return 0;
1166}
1167
San Mehata2677e42009-12-13 10:40:18 -08001168int VolumeManager::shareVolume(const char *label, const char *method) {
1169 Volume *v = lookupVolume(label);
1170
1171 if (!v) {
1172 errno = ENOENT;
1173 return -1;
1174 }
1175
1176 /*
1177 * Eventually, we'll want to support additional share back-ends,
1178 * some of which may work while the media is mounted. For now,
1179 * we just support UMS
1180 */
1181 if (strcmp(method, "ums")) {
1182 errno = ENOSYS;
1183 return -1;
1184 }
1185
1186 if (v->getState() == Volume::State_NoMedia) {
1187 errno = ENODEV;
1188 return -1;
1189 }
1190
San Mehat49e2bce2009-10-12 16:29:01 -07001191 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001192 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001193 errno = EBUSY;
1194 return -1;
1195 }
1196
Ken Sumrall3b170052011-07-11 15:38:57 -07001197 if (mVolManagerDisabled) {
1198 errno = EBUSY;
1199 return -1;
1200 }
1201
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001202 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001203 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1204 // This volume does not support raw disk access
1205 errno = EINVAL;
1206 return -1;
1207 }
1208
1209 int fd;
1210 char nodepath[255];
1211 snprintf(nodepath,
1212 sizeof(nodepath), "/dev/block/vold/%d:%d",
1213 MAJOR(d), MINOR(d));
1214
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001215 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001216 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001217 return -1;
1218 }
1219
1220 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001221 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001222 close(fd);
1223 return -1;
1224 }
1225
1226 close(fd);
1227 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001228 if (mUmsSharingCount++ == 0) {
1229 FILE* fp;
1230 mSavedDirtyRatio = -1; // in case we fail
1231 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1232 char line[16];
1233 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1234 fprintf(fp, "%d\n", mUmsDirtyRatio);
1235 } else {
1236 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1237 }
1238 fclose(fp);
1239 } else {
1240 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1241 }
1242 }
San Mehata2677e42009-12-13 10:40:18 -08001243 return 0;
1244}
1245
1246int VolumeManager::unshareVolume(const char *label, const char *method) {
1247 Volume *v = lookupVolume(label);
1248
1249 if (!v) {
1250 errno = ENOENT;
1251 return -1;
1252 }
1253
1254 if (strcmp(method, "ums")) {
1255 errno = ENOSYS;
1256 return -1;
1257 }
1258
1259 if (v->getState() != Volume::State_Shared) {
1260 errno = EINVAL;
1261 return -1;
1262 }
1263
San Mehata2677e42009-12-13 10:40:18 -08001264 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001265 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001266 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001267 return -1;
1268 }
1269
1270 char ch = 0;
1271 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001272 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001273 close(fd);
1274 return -1;
1275 }
1276
1277 close(fd);
1278 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001279 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1280 FILE* fp;
1281 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1282 fprintf(fp, "%d\n", mSavedDirtyRatio);
1283 fclose(fp);
1284 } else {
1285 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1286 }
1287 mSavedDirtyRatio = -1;
1288 }
San Mehata2677e42009-12-13 10:40:18 -08001289 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001290}
1291
Ken Sumrall3b170052011-07-11 15:38:57 -07001292extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001293 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001294 vm->disableVolumeManager();
1295 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001296 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001297}
1298
1299extern "C" int vold_getNumDirectVolumes(void) {
1300 VolumeManager *vm = VolumeManager::Instance();
1301 return vm->getNumDirectVolumes();
1302}
1303
1304int VolumeManager::getNumDirectVolumes(void) {
1305 VolumeCollection::iterator i;
1306 int n=0;
1307
1308 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1309 if ((*i)->getShareDevice() != (dev_t)0) {
1310 n++;
1311 }
1312 }
1313 return n;
1314}
1315
1316extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1317 VolumeManager *vm = VolumeManager::Instance();
1318 return vm->getDirectVolumeList(vol_list);
1319}
1320
1321int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1322 VolumeCollection::iterator i;
1323 int n=0;
1324 dev_t d;
1325
1326 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1327 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1328 (*i)->getVolInfo(&vol_list[n]);
1329 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
1330 "/dev/block/vold/%d:%d",MAJOR(d), MINOR(d));
1331 n++;
1332 }
1333 }
1334
1335 return 0;
1336}
1337
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001338int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001339 Volume *v = lookupVolume(label);
1340
1341 if (!v) {
1342 errno = ENOENT;
1343 return -1;
1344 }
1345
San Mehata2677e42009-12-13 10:40:18 -08001346 if (v->getState() == Volume::State_NoMedia) {
1347 errno = ENODEV;
1348 return -1;
1349 }
1350
San Mehat49e2bce2009-10-12 16:29:01 -07001351 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001352 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001353 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001354 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001355 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001356 }
1357
San Mehat1a06eda2010-04-15 12:58:50 -07001358 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001359
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001360 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001361}
1362
San Mehata2677e42009-12-13 10:40:18 -08001363/*
1364 * Looks up a volume by it's label or mount-point
1365 */
San Mehat49e2bce2009-10-12 16:29:01 -07001366Volume *VolumeManager::lookupVolume(const char *label) {
1367 VolumeCollection::iterator i;
1368
1369 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001370 if (label[0] == '/') {
1371 if (!strcmp(label, (*i)->getMountpoint()))
1372 return (*i);
1373 } else {
1374 if (!strcmp(label, (*i)->getLabel()))
1375 return (*i);
1376 }
San Mehat49e2bce2009-10-12 16:29:01 -07001377 }
1378 return NULL;
1379}
San Mehata19b2502010-01-06 10:33:53 -08001380
1381bool VolumeManager::isMountpointMounted(const char *mp)
1382{
1383 char device[256];
1384 char mount_path[256];
1385 char rest[256];
1386 FILE *fp;
1387 char line[1024];
1388
1389 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001390 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001391 return false;
1392 }
1393
1394 while(fgets(line, sizeof(line), fp)) {
1395 line[strlen(line)-1] = '\0';
1396 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1397 if (!strcmp(mount_path, mp)) {
1398 fclose(fp);
1399 return true;
1400 }
San Mehata19b2502010-01-06 10:33:53 -08001401 }
1402
1403 fclose(fp);
1404 return false;
1405}
1406
San Mehat1a06eda2010-04-15 12:58:50 -07001407int VolumeManager::cleanupAsec(Volume *v, bool force) {
1408 while(mActiveContainers->size()) {
1409 AsecIdCollection::iterator it = mActiveContainers->begin();
Kenny Rootcbacf782010-09-24 15:11:48 -07001410 ContainerData* cd = *it;
1411 SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
1412 if (cd->type == ASEC) {
1413 if (unmountAsec(cd->id, force)) {
1414 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1415 return -1;
1416 }
1417 } else if (cd->type == OBB) {
1418 if (unmountObb(cd->id, force)) {
1419 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1420 return -1;
1421 }
1422 } else {
1423 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001424 return -1;
1425 }
1426 }
1427 return 0;
1428}
1429