blob: 796cb11bb63a0232e92471e4e99d76b014282d47 [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>
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070027#include <sys/ioctl.h>
Ken Sumrall425524d2012-06-14 20:55:28 -070028#include <dirent.h>
San Mehata19b2502010-01-06 10:33:53 -080029
San Mehata2677e42009-12-13 10:40:18 -080030#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070031
32#define LOG_TAG "Vold"
33
Kenny Root7b18a7b2010-03-15 13:13:41 -070034#include <openssl/md5.h>
35
Jeff Sharkey71ebe152013-09-17 17:24:38 -070036#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070037#include <cutils/log.h>
38
Robert Craigb9e3ba52014-02-04 10:53:00 -050039#include <selinux/android.h>
40
San Mehatfd7f5872009-10-12 11:32:47 -070041#include <sysutils/NetlinkEvent.h>
42
Kenny Root344ca102012-04-03 17:23:01 -070043#include <private/android_filesystem_config.h>
44
San Mehatf1b736b2009-10-10 17:22:08 -070045#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070046#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080047#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080048#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070049#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080050#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080051#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080052#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080053#include "Asec.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070054#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080055
Mike Lockwood97f2fc12011-06-07 10:51:38 -070056#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
57
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070058#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
59 + (number & (~((1U << po2) - 1))))
60
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070061/* writes superblock at end of file or device given by name */
62static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
63 int sbfd = open(name, O_RDWR);
64 if (sbfd < 0) {
65 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
66 return -1;
67 }
68
69 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
70 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
71 close(sbfd);
72 return -1;
73 }
74
75 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
76 SLOGE("Failed to write superblock (%s)", strerror(errno));
77 close(sbfd);
78 return -1;
79 }
80 close(sbfd);
81 return 0;
82}
83
84static int adjustSectorNumExt4(unsigned numSectors) {
85 return ROUND_UP_POWER_OF_2(numSectors, 3);
86}
87
88static int adjustSectorNumFAT(unsigned numSectors) {
89 /*
90 * Add some headroom
91 */
92 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
93 numSectors += fatSize + 2;
94 /*
95 * FAT is aligned to 32 kb with 512b sectors.
96 */
97 return ROUND_UP_POWER_OF_2(numSectors, 6);
98}
99
100static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
101 if (Loop::lookupActive(idHash, buffer, len)) {
102 if (Loop::create(idHash, asecFileName, buffer, len)) {
103 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
104 return -1;
105 }
106 if (debug) {
107 SLOGD("New loop device created at %s", buffer);
108 }
109 } else {
110 if (debug) {
111 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
112 }
113 }
114 return 0;
115}
116
117static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) {
118 if (strcmp(key, "none")) {
119 if (Devmapper::lookupActive(idHash, buffer, len)) {
120 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
121 buffer, len)) {
122 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
123 return -1;
124 }
125 if (debug) {
126 SLOGD("New devmapper instance created at %s", buffer);
127 }
128 } else {
129 if (debug) {
130 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
131 }
132 }
133 *createdDMDevice = true;
134 } else {
135 strcpy(buffer, loopDevice);
136 *createdDMDevice = false;
137 }
138 return 0;
139}
140
141static void waitForDevMapper(const char *dmDevice) {
142 /*
143 * Wait for the device mapper node to be created. Sometimes it takes a
144 * while. Wait for up to 1 second. We could also inspect incoming uevents,
145 * but that would take more effort.
146 */
147 int tries = 25;
148 while (tries--) {
149 if (!access(dmDevice, F_OK) || errno != ENOENT) {
150 break;
151 }
152 usleep(40 * 1000);
153 }
154}
155
San Mehatf1b736b2009-10-10 17:22:08 -0700156VolumeManager *VolumeManager::sInstance = NULL;
157
158VolumeManager *VolumeManager::Instance() {
159 if (!sInstance)
160 sInstance = new VolumeManager();
161 return sInstance;
162}
163
164VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800165 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700166 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -0800167 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700168 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400169 mUmsSharingCount = 0;
170 mSavedDirtyRatio = -1;
171 // set dirty ratio to 0 when UMS is active
172 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -0700173 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700174}
175
176VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800177 delete mVolumes;
178 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700179}
180
Kenny Root7b18a7b2010-03-15 13:13:41 -0700181char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700182 static const char* digits = "0123456789abcdef";
183
Kenny Root7b18a7b2010-03-15 13:13:41 -0700184 unsigned char sig[MD5_DIGEST_LENGTH];
185
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700186 if (buffer == NULL) {
187 SLOGE("Destination buffer is NULL");
188 errno = ESPIPE;
189 return NULL;
190 } else if (id == NULL) {
191 SLOGE("Source buffer is NULL");
192 errno = ESPIPE;
193 return NULL;
194 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800195 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700196 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800197 errno = ESPIPE;
198 return NULL;
199 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700200
201 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800202
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700203 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700204 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700205 *p++ = digits[sig[i] >> 4];
206 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800207 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700208 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800209
210 return buffer;
211}
212
213void VolumeManager::setDebug(bool enable) {
214 mDebug = enable;
215 VolumeCollection::iterator it;
216 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
217 (*it)->setDebug(enable);
218 }
219}
220
San Mehatf1b736b2009-10-10 17:22:08 -0700221int VolumeManager::start() {
222 return 0;
223}
224
225int VolumeManager::stop() {
226 return 0;
227}
228
229int VolumeManager::addVolume(Volume *v) {
230 mVolumes->push_back(v);
231 return 0;
232}
233
San Mehatfd7f5872009-10-12 11:32:47 -0700234void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
235 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700236
San Mehatfd7f5872009-10-12 11:32:47 -0700237 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700238 VolumeCollection::iterator it;
239 bool hit = false;
240 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700241 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800242#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700243 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800244#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700245 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700246 break;
247 }
248 }
249
250 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800251#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700252 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800253#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700254 }
255}
256
San Mehatf1b736b2009-10-10 17:22:08 -0700257int VolumeManager::listVolumes(SocketClient *cli) {
258 VolumeCollection::iterator i;
259
260 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
261 char *buffer;
262 asprintf(&buffer, "%s %s %d",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700263 (*i)->getLabel(), (*i)->getFuseMountpoint(),
San Mehatf1b736b2009-10-10 17:22:08 -0700264 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800265 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700266 free(buffer);
267 }
San Mehata2677e42009-12-13 10:40:18 -0800268 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700269 return 0;
270}
San Mehat49e2bce2009-10-12 16:29:01 -0700271
Ken Sumrall9caab762013-06-11 19:10:20 -0700272int VolumeManager::formatVolume(const char *label, bool wipe) {
San Mehata2677e42009-12-13 10:40:18 -0800273 Volume *v = lookupVolume(label);
274
275 if (!v) {
276 errno = ENOENT;
277 return -1;
278 }
279
Ken Sumrall3b170052011-07-11 15:38:57 -0700280 if (mVolManagerDisabled) {
281 errno = EBUSY;
282 return -1;
283 }
284
Ken Sumrall9caab762013-06-11 19:10:20 -0700285 return v->formatVol(wipe);
San Mehata2677e42009-12-13 10:40:18 -0800286}
287
Kenny Root508c0e12010-07-12 09:59:49 -0700288int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
289 char idHash[33];
290 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
291 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
292 return -1;
293 }
294
295 memset(mountPath, 0, mountPathLen);
rpcraigd1c226f2012-10-09 06:58:16 -0400296 int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
297 if ((written < 0) || (written >= mountPathLen)) {
298 errno = EINVAL;
299 return -1;
300 }
Kenny Root508c0e12010-07-12 09:59:49 -0700301
302 if (access(mountPath, F_OK)) {
303 errno = ENOENT;
304 return -1;
305 }
306
307 return 0;
308}
309
San Mehata19b2502010-01-06 10:33:53 -0800310int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700311 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700312
Nick Kralevich0de7c612014-01-27 14:58:06 -0800313 if (!isLegalAsecId(id)) {
314 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
315 errno = EINVAL;
316 return -1;
317 }
318
Kenny Root344ca102012-04-03 17:23:01 -0700319 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
320 SLOGE("Couldn't find ASEC %s", id);
321 return -1;
322 }
San Mehat88ac2c02010-03-23 11:15:58 -0700323
324 memset(buffer, 0, maxlen);
325 if (access(asecFileName, F_OK)) {
326 errno = ENOENT;
327 return -1;
328 }
San Mehata19b2502010-01-06 10:33:53 -0800329
rpcraigd1c226f2012-10-09 06:58:16 -0400330 int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
331 if ((written < 0) || (written >= maxlen)) {
332 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
333 errno = EINVAL;
334 return -1;
335 }
336
San Mehata19b2502010-01-06 10:33:53 -0800337 return 0;
338}
339
Dianne Hackborn736910c2011-06-27 13:37:07 -0700340int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
341 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700342
Nick Kralevich0de7c612014-01-27 14:58:06 -0800343 if (!isLegalAsecId(id)) {
344 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
345 errno = EINVAL;
346 return -1;
347 }
348
Kenny Root344ca102012-04-03 17:23:01 -0700349 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
350 SLOGE("Couldn't find ASEC %s", id);
351 return -1;
352 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700353
354 memset(buffer, 0, maxlen);
355 if (access(asecFileName, F_OK)) {
356 errno = ENOENT;
357 return -1;
358 }
359
rpcraigd1c226f2012-10-09 06:58:16 -0400360 int written = snprintf(buffer, maxlen, "%s", asecFileName);
361 if ((written < 0) || (written >= maxlen)) {
362 errno = EINVAL;
363 return -1;
364 }
365
Dianne Hackborn736910c2011-06-27 13:37:07 -0700366 return 0;
367}
368
Kenny Root344ca102012-04-03 17:23:01 -0700369int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
370 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800371 struct asec_superblock sb;
372 memset(&sb, 0, sizeof(sb));
373
Nick Kralevich0de7c612014-01-27 14:58:06 -0800374 if (!isLegalAsecId(id)) {
375 SLOGE("createAsec: Invalid asec id \"%s\"", id);
376 errno = EINVAL;
377 return -1;
378 }
379
Kenny Root344ca102012-04-03 17:23:01 -0700380 const bool wantFilesystem = strcmp(fstype, "none");
381 bool usingExt4 = false;
382 if (wantFilesystem) {
383 usingExt4 = !strcmp(fstype, "ext4");
384 if (usingExt4) {
385 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
386 } else if (strcmp(fstype, "fat")) {
387 SLOGE("Invalid filesystem type %s", fstype);
388 errno = EINVAL;
389 return -1;
390 }
391 }
392
San Mehatfcf24fe2010-03-03 12:37:32 -0800393 sb.magic = ASEC_SB_MAGIC;
394 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800395
San Mehatd31e3802010-02-18 08:37:45 -0800396 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700397 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800398 errno = EINVAL;
399 return -1;
400 }
401
San Mehata19b2502010-01-06 10:33:53 -0800402 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700403 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800404 errno = EADDRINUSE;
405 return -1;
406 }
407
408 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700409
410 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
411 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
412 asecFileName, strerror(errno));
413 errno = EADDRINUSE;
414 return -1;
415 }
416
417 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
418
rpcraigd1c226f2012-10-09 06:58:16 -0400419 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
420 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
421 errno = EINVAL;
422 return -1;
423 }
San Mehata19b2502010-01-06 10:33:53 -0800424
425 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700426 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700427 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800428 errno = EADDRINUSE;
429 return -1;
430 }
431
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700432 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700433 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700434 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700435 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700436 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800437
438 // Add +1 for our superblock which is at the end
439 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700440 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800441 return -1;
442 }
443
San Mehatd9a4e352010-03-12 13:32:47 -0800444 char idHash[33];
445 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700446 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800447 unlink(asecFileName);
448 return -1;
449 }
450
San Mehata19b2502010-01-06 10:33:53 -0800451 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800452 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700453 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800454 unlink(asecFileName);
455 return -1;
456 }
457
San Mehatb78a32c2010-01-10 13:02:12 -0800458 char dmDevice[255];
459 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800460
San Mehatb78a32c2010-01-10 13:02:12 -0800461 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800462 // XXX: This is all we support for now
463 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800464 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800465 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700466 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800467 Loop::destroyByDevice(loopDevice);
468 unlink(asecFileName);
469 return -1;
470 }
471 cleanupDm = true;
472 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800473 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800474 strcpy(dmDevice, loopDevice);
475 }
476
San Mehatfcf24fe2010-03-03 12:37:32 -0800477 /*
478 * Drop down the superblock at the end of the file
479 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700480 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800481 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800482 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800483 }
484 Loop::destroyByDevice(loopDevice);
485 unlink(asecFileName);
486 return -1;
487 }
488
Kenny Root344ca102012-04-03 17:23:01 -0700489 if (wantFilesystem) {
490 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400491 char mountPoint[255];
492
rpcraigd1c226f2012-10-09 06:58:16 -0400493 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
494 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
495 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
496 if (cleanupDm) {
497 Devmapper::destroy(idHash);
498 }
499 Loop::destroyByDevice(loopDevice);
500 unlink(asecFileName);
501 return -1;
502 }
rpcraiga54e13a2012-09-21 14:17:08 -0400503
Kenny Root344ca102012-04-03 17:23:01 -0700504 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700505 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700506 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700507 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800508 }
San Mehata19b2502010-01-06 10:33:53 -0800509
Kenny Root344ca102012-04-03 17:23:01 -0700510 if (formatStatus < 0) {
511 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800512 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800513 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800514 }
San Mehateb13a902010-01-07 12:12:50 -0800515 Loop::destroyByDevice(loopDevice);
516 unlink(asecFileName);
517 return -1;
518 }
Kenny Root344ca102012-04-03 17:23:01 -0700519
Kenny Root344ca102012-04-03 17:23:01 -0700520 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800521 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700522 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800523 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800524 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800525 }
526 Loop::destroyByDevice(loopDevice);
527 unlink(asecFileName);
528 return -1;
529 }
San Mehatb78a32c2010-01-10 13:02:12 -0800530 }
San Mehata1091cb2010-02-28 20:17:20 -0800531
Kenny Root344ca102012-04-03 17:23:01 -0700532 int mountStatus;
533 if (usingExt4) {
534 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
535 } else {
536 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
537 false);
538 }
539
540 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700541 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800542 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800543 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800544 }
545 Loop::destroyByDevice(loopDevice);
546 unlink(asecFileName);
547 return -1;
548 }
Kenny Root344ca102012-04-03 17:23:01 -0700549
550 if (usingExt4) {
551 int dirfd = open(mountPoint, O_DIRECTORY);
552 if (dirfd >= 0) {
553 if (fchown(dirfd, ownerUid, AID_SYSTEM)
554 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
555 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
556 }
557 close(dirfd);
558 }
559 }
San Mehata1091cb2010-02-28 20:17:20 -0800560 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700561 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800562 }
San Mehat88705162010-01-15 09:26:28 -0800563
Kenny Rootcbacf782010-09-24 15:11:48 -0700564 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800565 return 0;
566}
567
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700568int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
569 char asecFileName[255];
570 char mountPoint[255];
571 bool cleanupDm = false;
572
573 if (!isLegalAsecId(id)) {
574 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
575 errno = EINVAL;
576 return -1;
577 }
578
579 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
580 SLOGE("Couldn't find ASEC %s", id);
581 return -1;
582 }
583
584 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
585 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
586 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
587 return -1;
588 }
589
590 if (isMountpointMounted(mountPoint)) {
591 SLOGE("ASEC %s mounted. Unmount before resizing", id);
592 errno = EBUSY;
593 return -1;
594 }
595
596 struct asec_superblock sb;
597 int fd;
598 unsigned int oldNumSec = 0;
599
600 if ((fd = open(asecFileName, O_RDONLY)) < 0) {
601 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
602 return -1;
603 }
604
605 struct stat info;
606 if (fstat(fd, &info) < 0) {
607 SLOGE("Failed to get file size (%s)", strerror(errno));
608 close(fd);
609 return -1;
610 }
611
612 oldNumSec = info.st_size / 512;
613
614 unsigned numImgSectors;
615 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
616 numImgSectors = adjustSectorNumExt4(numSectors);
617 else
618 numImgSectors = adjustSectorNumFAT(numSectors);
619 /*
620 * add one block for the superblock
621 */
622 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
623 if (oldNumSec >= numImgSectors + 1) {
624 SLOGE("Only growing is currently supported.");
625 close(fd);
626 return -1;
627 }
628
629 /*
630 * Try to read superblock.
631 */
632 memset(&sb, 0, sizeof(struct asec_superblock));
633 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
634 SLOGE("lseek failed (%s)", strerror(errno));
635 close(fd);
636 return -1;
637 }
638 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
639 SLOGE("superblock read failed (%s)", strerror(errno));
640 close(fd);
641 return -1;
642 }
643 close(fd);
644
645 if (mDebug) {
646 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
647 }
648 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
649 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
650 errno = EMEDIUMTYPE;
651 return -1;
652 }
653
654 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
655 SLOGE("Only ext4 partitions are supported for resize");
656 errno = EINVAL;
657 return -1;
658 }
659
660 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
661 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
662 return -1;
663 }
664
665 /*
666 * Drop down a copy of the superblock at the end of the file
667 */
668 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
669 goto fail;
670
671 char idHash[33];
672 if (!asecHash(id, idHash, sizeof(idHash))) {
673 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
674 goto fail;
675 }
676
677 char loopDevice[255];
678 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
679 goto fail;
680
681 char dmDevice[255];
682
683 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
684 Loop::destroyByDevice(loopDevice);
685 goto fail;
686 }
687
688 /*
689 * Wait for the device mapper node to be created.
690 */
691 waitForDevMapper(dmDevice);
692
693 if (Ext4::resize(dmDevice, numImgSectors)) {
694 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
695 if (cleanupDm) {
696 Devmapper::destroy(idHash);
697 }
698 Loop::destroyByDevice(loopDevice);
699 goto fail;
700 }
701
702 return 0;
703fail:
704 Loop::resizeImageFile(asecFileName, oldNumSec);
705 return -1;
706}
707
San Mehata19b2502010-01-06 10:33:53 -0800708int VolumeManager::finalizeAsec(const char *id) {
709 char asecFileName[255];
710 char loopDevice[255];
711 char mountPoint[255];
712
Nick Kralevich0de7c612014-01-27 14:58:06 -0800713 if (!isLegalAsecId(id)) {
714 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
715 errno = EINVAL;
716 return -1;
717 }
718
Kenny Root344ca102012-04-03 17:23:01 -0700719 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
720 SLOGE("Couldn't find ASEC %s", id);
721 return -1;
722 }
San Mehata19b2502010-01-06 10:33:53 -0800723
San Mehatd9a4e352010-03-12 13:32:47 -0800724 char idHash[33];
725 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700726 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800727 return -1;
728 }
729
730 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700731 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800732 return -1;
733 }
734
Kenny Root344ca102012-04-03 17:23:01 -0700735 unsigned int nr_sec = 0;
736 struct asec_superblock sb;
737
738 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
739 return -1;
740 }
741
rpcraigd1c226f2012-10-09 06:58:16 -0400742 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
743 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
744 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
745 return -1;
746 }
Kenny Root344ca102012-04-03 17:23:01 -0700747
748 int result = 0;
749 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
750 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
751 } else {
752 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
753 }
754
755 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700756 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800757 return -1;
758 }
759
San Mehatd9a4e352010-03-12 13:32:47 -0800760 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700761 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800762 }
San Mehata19b2502010-01-06 10:33:53 -0800763 return 0;
764}
765
Kenny Root344ca102012-04-03 17:23:01 -0700766int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
767 char asecFileName[255];
768 char loopDevice[255];
769 char mountPoint[255];
770
771 if (gid < AID_APP) {
772 SLOGE("Group ID is not in application range");
773 return -1;
774 }
775
Nick Kralevich0de7c612014-01-27 14:58:06 -0800776 if (!isLegalAsecId(id)) {
777 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
778 errno = EINVAL;
779 return -1;
780 }
781
Kenny Root344ca102012-04-03 17:23:01 -0700782 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
783 SLOGE("Couldn't find ASEC %s", id);
784 return -1;
785 }
786
787 char idHash[33];
788 if (!asecHash(id, idHash, sizeof(idHash))) {
789 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
790 return -1;
791 }
792
793 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
794 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
795 return -1;
796 }
797
798 unsigned int nr_sec = 0;
799 struct asec_superblock sb;
800
801 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
802 return -1;
803 }
804
rpcraigd1c226f2012-10-09 06:58:16 -0400805 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
806 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
807 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
808 return -1;
809 }
Kenny Root344ca102012-04-03 17:23:01 -0700810
811 int result = 0;
812 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
813 return 0;
814 }
815
816 int ret = Ext4::doMount(loopDevice, mountPoint,
817 false /* read-only */,
818 true /* remount */,
819 false /* executable */);
820 if (ret) {
821 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
822 return -1;
823 }
824
825 char *paths[] = { mountPoint, NULL };
826
827 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
828 if (fts) {
829 // Traverse the entire hierarchy and chown to system UID.
830 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
831 // We don't care about the lost+found directory.
832 if (!strcmp(ftsent->fts_name, "lost+found")) {
833 continue;
834 }
835
836 /*
837 * There can only be one file marked as private right now.
838 * This should be more robust, but it satisfies the requirements
839 * we have for right now.
840 */
841 const bool privateFile = !strcmp(ftsent->fts_name, filename);
842
843 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
844 if (fd < 0) {
845 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
846 result = -1;
847 continue;
848 }
849
850 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
851
852 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700853 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700854 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700855 result |= fchmod(fd, privateFile ? 0640 : 0644);
856 }
Robert Craigb9e3ba52014-02-04 10:53:00 -0500857
Stephen Smalley5093e612014-02-12 09:43:08 -0500858 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -0500859 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
860 result |= -1;
861 }
862
Kenny Root344ca102012-04-03 17:23:01 -0700863 close(fd);
864 }
865 fts_close(fts);
866
867 // Finally make the directory readable by everyone.
868 int dirfd = open(mountPoint, O_DIRECTORY);
869 if (dirfd < 0 || fchmod(dirfd, 0755)) {
870 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
871 result |= -1;
872 }
873 close(dirfd);
874 } else {
875 result |= -1;
876 }
877
878 result |= Ext4::doMount(loopDevice, mountPoint,
879 true /* read-only */,
880 true /* remount */,
881 true /* execute */);
882
883 if (result) {
884 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
885 return -1;
886 }
887
888 if (mDebug) {
889 SLOGD("ASEC %s permissions fixed", id);
890 }
891 return 0;
892}
893
San Mehat048b0802010-01-23 08:17:06 -0800894int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700895 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800896 char *asecFilename2;
897 char mountPoint[255];
898
Kenny Root344ca102012-04-03 17:23:01 -0700899 const char *dir;
900
Nick Kralevich0de7c612014-01-27 14:58:06 -0800901 if (!isLegalAsecId(id1)) {
902 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
903 errno = EINVAL;
904 return -1;
905 }
906
907 if (!isLegalAsecId(id2)) {
908 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
909 errno = EINVAL;
910 return -1;
911 }
912
Kenny Root344ca102012-04-03 17:23:01 -0700913 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
914 SLOGE("Couldn't find ASEC %s", id1);
915 return -1;
916 }
917
918 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800919
rpcraigd1c226f2012-10-09 06:58:16 -0400920 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
921 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
922 SLOGE("Rename failed: couldn't construct mountpoint");
923 goto out_err;
924 }
925
San Mehat048b0802010-01-23 08:17:06 -0800926 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700927 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800928 errno = EBUSY;
929 goto out_err;
930 }
931
rpcraigd1c226f2012-10-09 06:58:16 -0400932 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
933 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
934 SLOGE("Rename failed: couldn't construct mountpoint2");
935 goto out_err;
936 }
937
San Mehat96956ed2010-02-24 08:42:51 -0800938 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700939 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800940 errno = EBUSY;
941 goto out_err;
942 }
943
San Mehat048b0802010-01-23 08:17:06 -0800944 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700945 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800946 errno = EADDRINUSE;
947 goto out_err;
948 }
949
950 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700951 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800952 goto out_err;
953 }
954
San Mehat048b0802010-01-23 08:17:06 -0800955 free(asecFilename2);
956 return 0;
957
958out_err:
San Mehat048b0802010-01-23 08:17:06 -0800959 free(asecFilename2);
960 return -1;
961}
962
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700963#define UNMOUNT_RETRIES 5
964#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800965int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800966 char asecFileName[255];
967 char mountPoint[255];
968
Nick Kralevich0de7c612014-01-27 14:58:06 -0800969 if (!isLegalAsecId(id)) {
970 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
971 errno = EINVAL;
972 return -1;
973 }
974
Kenny Root344ca102012-04-03 17:23:01 -0700975 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
976 SLOGE("Couldn't find ASEC %s", id);
977 return -1;
978 }
979
rpcraigd1c226f2012-10-09 06:58:16 -0400980 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
981 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
982 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
983 return -1;
984 }
San Mehata19b2502010-01-06 10:33:53 -0800985
San Mehatd9a4e352010-03-12 13:32:47 -0800986 char idHash[33];
987 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700988 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800989 return -1;
990 }
991
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700992 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
993}
994
Kenny Root508c0e12010-07-12 09:59:49 -0700995int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700996 char mountPoint[255];
997
998 char idHash[33];
999 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1000 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1001 return -1;
1002 }
1003
rpcraigd1c226f2012-10-09 06:58:16 -04001004 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1005 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1006 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1007 return -1;
1008 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001009
1010 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1011}
1012
1013int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1014 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001015 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001016 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001017 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001018 return -1;
1019 }
San Mehat23969932010-01-09 07:08:06 -08001020
San Mehatb78a32c2010-01-10 13:02:12 -08001021 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001022 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001023 rc = umount(mountPoint);
1024 if (!rc) {
1025 break;
San Mehata19b2502010-01-06 10:33:53 -08001026 }
San Mehatb78a32c2010-01-10 13:02:12 -08001027 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001028 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001029 rc = 0;
1030 break;
San Mehata19b2502010-01-06 10:33:53 -08001031 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001032 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001033 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001034
San Mehat4ba89482010-02-18 09:00:18 -08001035 int action = 0; // default is to just complain
1036
1037 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001038 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -08001039 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001040 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -08001041 action = 1; // SIGHUP
1042 }
San Mehat8c940ef2010-02-13 14:19:53 -08001043
San Mehat586536c2010-02-16 17:12:00 -08001044 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001045 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001046 }
1047
1048 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001049 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001050 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001051 return -1;
1052 }
1053
San Mehat12f4b892010-02-24 11:43:22 -08001054 int retries = 10;
1055
1056 while(retries--) {
1057 if (!rmdir(mountPoint)) {
1058 break;
1059 }
1060
San Mehat97ac40e2010-03-24 10:24:19 -07001061 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001062 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001063 }
1064
1065 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001066 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001067 }
San Mehat88705162010-01-15 09:26:28 -08001068
San Mehatd9a4e352010-03-12 13:32:47 -08001069 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -07001070 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001071 }
1072
1073 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001074 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001075 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001076 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001077 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001078 }
San Mehat88705162010-01-15 09:26:28 -08001079
1080 AsecIdCollection::iterator it;
1081 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001082 ContainerData* cd = *it;
1083 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001084 free(*it);
1085 mActiveContainers->erase(it);
1086 break;
1087 }
1088 }
1089 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001090 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001091 }
San Mehatb78a32c2010-01-10 13:02:12 -08001092 return 0;
1093}
1094
San Mehat4ba89482010-02-18 09:00:18 -08001095int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001096 char asecFileName[255];
1097 char mountPoint[255];
1098
Nick Kralevich0de7c612014-01-27 14:58:06 -08001099 if (!isLegalAsecId(id)) {
1100 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1101 errno = EINVAL;
1102 return -1;
1103 }
1104
Kenny Root344ca102012-04-03 17:23:01 -07001105 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1106 SLOGE("Couldn't find ASEC %s", id);
1107 return -1;
1108 }
1109
rpcraigd1c226f2012-10-09 06:58:16 -04001110 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1111 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1112 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1113 return -1;
1114 }
San Mehatb78a32c2010-01-10 13:02:12 -08001115
San Mehat0586d542010-01-12 15:38:59 -08001116 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001117 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001118 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001119 }
San Mehat4ba89482010-02-18 09:00:18 -08001120 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001121 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001122 return -1;
1123 }
1124 }
San Mehata19b2502010-01-06 10:33:53 -08001125
San Mehat0586d542010-01-12 15:38:59 -08001126 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001127 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001128 return -1;
1129 }
San Mehata19b2502010-01-06 10:33:53 -08001130
San Mehatd9a4e352010-03-12 13:32:47 -08001131 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001132 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001133 }
San Mehata19b2502010-01-06 10:33:53 -08001134 return 0;
1135}
1136
Nick Kralevich0de7c612014-01-27 14:58:06 -08001137/*
1138 * Legal ASEC ids consist of alphanumeric characters, '-',
1139 * '_', or '.'. ".." is not allowed. The first or last character
1140 * of the ASEC id cannot be '.' (dot).
1141 */
1142bool VolumeManager::isLegalAsecId(const char *id) const {
1143 size_t i;
1144 size_t len = strlen(id);
1145
1146 if (len == 0) {
1147 return false;
1148 }
1149 if ((id[0] == '.') || (id[len - 1] == '.')) {
1150 return false;
1151 }
1152
1153 for (i = 0; i < len; i++) {
1154 if (id[i] == '.') {
1155 // i=0 is guaranteed never to have a dot. See above.
1156 if (id[i-1] == '.') return false;
1157 continue;
1158 }
1159 if (id[i] == '_' || id[i] == '-') continue;
1160 if (id[i] >= 'a' && id[i] <= 'z') continue;
1161 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1162 if (id[i] >= '0' && id[i] <= '9') continue;
1163 return false;
1164 }
1165
1166 return true;
1167}
1168
Kenny Root344ca102012-04-03 17:23:01 -07001169bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1170 int dirfd = open(dir, O_DIRECTORY);
1171 if (dirfd < 0) {
1172 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
1173 return -1;
1174 }
1175
1176 bool ret = false;
1177
1178 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
1179 ret = true;
1180 }
1181
1182 close(dirfd);
1183
1184 return ret;
1185}
1186
1187int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1188 const char **directory) const {
1189 int dirfd, fd;
1190 const int idLen = strlen(id);
1191 char *asecName;
1192
Nick Kralevich0de7c612014-01-27 14:58:06 -08001193 if (!isLegalAsecId(id)) {
1194 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1195 errno = EINVAL;
1196 return -1;
1197 }
1198
Kenny Root344ca102012-04-03 17:23:01 -07001199 if (asprintf(&asecName, "%s.asec", id) < 0) {
1200 SLOGE("Couldn't allocate string to write ASEC name");
1201 return -1;
1202 }
1203
1204 const char *dir;
1205 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
1206 dir = Volume::SEC_ASECDIR_INT;
1207 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
1208 dir = Volume::SEC_ASECDIR_EXT;
1209 } else {
1210 free(asecName);
1211 return -1;
1212 }
1213
1214 if (directory != NULL) {
1215 *directory = dir;
1216 }
1217
1218 if (asecPath != NULL) {
1219 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001220 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1221 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001222 free(asecName);
1223 return -1;
1224 }
1225 }
1226
1227 free(asecName);
1228 return 0;
1229}
1230
San Mehata19b2502010-01-06 10:33:53 -08001231int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
1232 char asecFileName[255];
1233 char mountPoint[255];
1234
Nick Kralevich0de7c612014-01-27 14:58:06 -08001235 if (!isLegalAsecId(id)) {
1236 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1237 errno = EINVAL;
1238 return -1;
1239 }
1240
Kenny Root344ca102012-04-03 17:23:01 -07001241 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1242 SLOGE("Couldn't find ASEC %s", id);
1243 return -1;
1244 }
1245
rpcraigd1c226f2012-10-09 06:58:16 -04001246 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1247 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001248 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001249 return -1;
1250 }
San Mehata19b2502010-01-06 10:33:53 -08001251
1252 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001253 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001254 errno = EBUSY;
1255 return -1;
1256 }
1257
San Mehatd9a4e352010-03-12 13:32:47 -08001258 char idHash[33];
1259 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001260 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001261 return -1;
1262 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001263
San Mehata19b2502010-01-06 10:33:53 -08001264 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001265 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1266 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001267
1268 char dmDevice[255];
1269 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -08001270 int fd;
1271 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001272 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001273
Kenny Root344ca102012-04-03 17:23:01 -07001274 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1275 return -1;
1276 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001277
San Mehatd9a4e352010-03-12 13:32:47 -08001278 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001279 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001280 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001281 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001282 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001283 Loop::destroyByDevice(loopDevice);
1284 errno = EMEDIUMTYPE;
1285 return -1;
1286 }
1287 nr_sec--; // We don't want the devmapping to extend onto our superblock
1288
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001289 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1290 Loop::destroyByDevice(loopDevice);
1291 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001292 }
1293
Kenny Root344ca102012-04-03 17:23:01 -07001294 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001295 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001296 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001297 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001298 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001299 }
1300 Loop::destroyByDevice(loopDevice);
1301 return -1;
1302 }
San Mehata19b2502010-01-06 10:33:53 -08001303 }
1304
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001305 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001306 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001307 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001308 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001309
Kenny Root344ca102012-04-03 17:23:01 -07001310 int result;
1311 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1312 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
1313 } else {
1314 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
1315 }
1316
1317 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001318 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001319 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001320 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001321 }
1322 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001323 return -1;
1324 }
1325
Kenny Rootcbacf782010-09-24 15:11:48 -07001326 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001327 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001328 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001329 }
San Mehata19b2502010-01-06 10:33:53 -08001330 return 0;
1331}
1332
Kenny Root93ecb382012-08-09 11:28:37 -07001333Volume* VolumeManager::getVolumeForFile(const char *fileName) {
1334 VolumeCollection::iterator i;
1335
1336 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001337 const char* mountPoint = (*i)->getFuseMountpoint();
Kenny Root93ecb382012-08-09 11:28:37 -07001338 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
1339 return *i;
1340 }
1341 }
1342
1343 return NULL;
1344}
1345
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001346/**
1347 * Mounts an image file <code>img</code>.
1348 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001349int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001350 char mountPoint[255];
1351
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001352 char idHash[33];
1353 if (!asecHash(img, idHash, sizeof(idHash))) {
1354 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1355 return -1;
1356 }
1357
rpcraigd1c226f2012-10-09 06:58:16 -04001358 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1359 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001360 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001361 return -1;
1362 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001363
1364 if (isMountpointMounted(mountPoint)) {
1365 SLOGE("Image %s already mounted", img);
1366 errno = EBUSY;
1367 return -1;
1368 }
1369
1370 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001371 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1372 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001373
1374 char dmDevice[255];
1375 bool cleanupDm = false;
1376 int fd;
1377 unsigned int nr_sec = 0;
1378
1379 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1380 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1381 Loop::destroyByDevice(loopDevice);
1382 return -1;
1383 }
1384
1385 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1386 SLOGE("Failed to get loop size (%s)", strerror(errno));
1387 Loop::destroyByDevice(loopDevice);
1388 close(fd);
1389 return -1;
1390 }
1391
1392 close(fd);
1393
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001394 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash , nr_sec, &cleanupDm, mDebug)) {
1395 Loop::destroyByDevice(loopDevice);
1396 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001397 }
1398
1399 if (mkdir(mountPoint, 0755)) {
1400 if (errno != EEXIST) {
1401 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1402 if (cleanupDm) {
1403 Devmapper::destroy(idHash);
1404 }
1405 Loop::destroyByDevice(loopDevice);
1406 return -1;
1407 }
1408 }
1409
Jeff Sharkey69479042012-09-25 16:14:57 -07001410 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001411 0227, false)) {
1412 SLOGE("Image mount failed (%s)", strerror(errno));
1413 if (cleanupDm) {
1414 Devmapper::destroy(idHash);
1415 }
1416 Loop::destroyByDevice(loopDevice);
1417 return -1;
1418 }
1419
Kenny Rootcbacf782010-09-24 15:11:48 -07001420 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001421 if (mDebug) {
1422 SLOGD("Image %s mounted", img);
1423 }
1424 return 0;
1425}
1426
San Mehat49e2bce2009-10-12 16:29:01 -07001427int VolumeManager::mountVolume(const char *label) {
1428 Volume *v = lookupVolume(label);
1429
1430 if (!v) {
1431 errno = ENOENT;
1432 return -1;
1433 }
1434
San Mehata2677e42009-12-13 10:40:18 -08001435 return v->mountVol();
1436}
1437
Kenny Root508c0e12010-07-12 09:59:49 -07001438int VolumeManager::listMountedObbs(SocketClient* cli) {
1439 char device[256];
1440 char mount_path[256];
1441 char rest[256];
1442 FILE *fp;
1443 char line[1024];
1444
1445 if (!(fp = fopen("/proc/mounts", "r"))) {
1446 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1447 return -1;
1448 }
1449
1450 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001451 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001452 char loopDir[loopDirLen + 2];
1453 strcpy(loopDir, Volume::LOOPDIR);
1454 loopDir[loopDirLen++] = '/';
1455 loopDir[loopDirLen] = '\0';
1456
1457 while(fgets(line, sizeof(line), fp)) {
1458 line[strlen(line)-1] = '\0';
1459
1460 /*
1461 * Should look like:
1462 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1463 */
1464 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1465
1466 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1467 int fd = open(device, O_RDONLY);
1468 if (fd >= 0) {
1469 struct loop_info64 li;
1470 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1471 cli->sendMsg(ResponseCode::AsecListResult,
1472 (const char*) li.lo_file_name, false);
1473 }
1474 close(fd);
1475 }
1476 }
1477 }
1478
1479 fclose(fp);
1480 return 0;
1481}
1482
San Mehateba65e92010-01-29 05:15:16 -08001483int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1484 Volume *v = lookupVolume(label);
1485
1486 if (!v) {
1487 errno = ENOENT;
1488 return -1;
1489 }
1490
1491 if (strcmp(method, "ums")) {
1492 errno = ENOSYS;
1493 return -1;
1494 }
1495
1496 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001497 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001498 } else {
1499 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001500 }
1501 return 0;
1502}
1503
San Mehata2677e42009-12-13 10:40:18 -08001504int VolumeManager::shareVolume(const char *label, const char *method) {
1505 Volume *v = lookupVolume(label);
1506
1507 if (!v) {
1508 errno = ENOENT;
1509 return -1;
1510 }
1511
1512 /*
1513 * Eventually, we'll want to support additional share back-ends,
1514 * some of which may work while the media is mounted. For now,
1515 * we just support UMS
1516 */
1517 if (strcmp(method, "ums")) {
1518 errno = ENOSYS;
1519 return -1;
1520 }
1521
1522 if (v->getState() == Volume::State_NoMedia) {
1523 errno = ENODEV;
1524 return -1;
1525 }
1526
San Mehat49e2bce2009-10-12 16:29:01 -07001527 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001528 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001529 errno = EBUSY;
1530 return -1;
1531 }
1532
Ken Sumrall3b170052011-07-11 15:38:57 -07001533 if (mVolManagerDisabled) {
1534 errno = EBUSY;
1535 return -1;
1536 }
1537
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001538 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001539 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1540 // This volume does not support raw disk access
1541 errno = EINVAL;
1542 return -1;
1543 }
1544
1545 int fd;
1546 char nodepath[255];
rpcraigd1c226f2012-10-09 06:58:16 -04001547 int written = snprintf(nodepath,
San Mehata2677e42009-12-13 10:40:18 -08001548 sizeof(nodepath), "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -08001549 major(d), minor(d));
San Mehata2677e42009-12-13 10:40:18 -08001550
rpcraigd1c226f2012-10-09 06:58:16 -04001551 if ((written < 0) || (size_t(written) >= sizeof(nodepath))) {
1552 SLOGE("shareVolume failed: couldn't construct nodepath");
1553 return -1;
1554 }
1555
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001556 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001557 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001558 return -1;
1559 }
1560
1561 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001562 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001563 close(fd);
1564 return -1;
1565 }
1566
1567 close(fd);
1568 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001569 if (mUmsSharingCount++ == 0) {
1570 FILE* fp;
1571 mSavedDirtyRatio = -1; // in case we fail
1572 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1573 char line[16];
1574 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1575 fprintf(fp, "%d\n", mUmsDirtyRatio);
1576 } else {
1577 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1578 }
1579 fclose(fp);
1580 } else {
1581 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1582 }
1583 }
San Mehata2677e42009-12-13 10:40:18 -08001584 return 0;
1585}
1586
1587int VolumeManager::unshareVolume(const char *label, const char *method) {
1588 Volume *v = lookupVolume(label);
1589
1590 if (!v) {
1591 errno = ENOENT;
1592 return -1;
1593 }
1594
1595 if (strcmp(method, "ums")) {
1596 errno = ENOSYS;
1597 return -1;
1598 }
1599
1600 if (v->getState() != Volume::State_Shared) {
1601 errno = EINVAL;
1602 return -1;
1603 }
1604
San Mehata2677e42009-12-13 10:40:18 -08001605 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001606 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001607 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001608 return -1;
1609 }
1610
1611 char ch = 0;
1612 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001613 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001614 close(fd);
1615 return -1;
1616 }
1617
1618 close(fd);
1619 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001620 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1621 FILE* fp;
1622 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1623 fprintf(fp, "%d\n", mSavedDirtyRatio);
1624 fclose(fp);
1625 } else {
1626 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1627 }
1628 mSavedDirtyRatio = -1;
1629 }
San Mehata2677e42009-12-13 10:40:18 -08001630 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001631}
1632
Ken Sumrall3b170052011-07-11 15:38:57 -07001633extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001634 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001635 vm->disableVolumeManager();
1636 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001637 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001638}
1639
1640extern "C" int vold_getNumDirectVolumes(void) {
1641 VolumeManager *vm = VolumeManager::Instance();
1642 return vm->getNumDirectVolumes();
1643}
1644
1645int VolumeManager::getNumDirectVolumes(void) {
1646 VolumeCollection::iterator i;
1647 int n=0;
1648
1649 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1650 if ((*i)->getShareDevice() != (dev_t)0) {
1651 n++;
1652 }
1653 }
1654 return n;
1655}
1656
1657extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1658 VolumeManager *vm = VolumeManager::Instance();
1659 return vm->getDirectVolumeList(vol_list);
1660}
1661
1662int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1663 VolumeCollection::iterator i;
1664 int n=0;
1665 dev_t d;
1666
1667 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1668 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1669 (*i)->getVolInfo(&vol_list[n]);
1670 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
Colin Cross346c5b22014-01-22 23:59:41 -08001671 "/dev/block/vold/%d:%d", major(d), minor(d));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001672 n++;
1673 }
1674 }
1675
1676 return 0;
1677}
1678
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001679int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001680 Volume *v = lookupVolume(label);
1681
1682 if (!v) {
1683 errno = ENOENT;
1684 return -1;
1685 }
1686
San Mehata2677e42009-12-13 10:40:18 -08001687 if (v->getState() == Volume::State_NoMedia) {
1688 errno = ENODEV;
1689 return -1;
1690 }
1691
San Mehat49e2bce2009-10-12 16:29:01 -07001692 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001693 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001694 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001695 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001696 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001697 }
1698
San Mehat1a06eda2010-04-15 12:58:50 -07001699 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001700
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001701 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001702}
1703
Ken Sumrall425524d2012-06-14 20:55:28 -07001704extern "C" int vold_unmountAllAsecs(void) {
1705 int rc;
1706
1707 VolumeManager *vm = VolumeManager::Instance();
1708 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1709 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1710 rc = -1;
1711 }
1712 return rc;
1713}
1714
1715#define ID_BUF_LEN 256
1716#define ASEC_SUFFIX ".asec"
1717#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1718int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1719 DIR *d = opendir(directory);
1720 int rc = 0;
1721
1722 if (!d) {
1723 SLOGE("Could not open asec dir %s", directory);
1724 return -1;
1725 }
1726
1727 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -07001728 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Ken Sumrall425524d2012-06-14 20:55:28 -07001729
1730 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1731 if (dent == NULL) {
1732 SLOGE("Failed to allocate memory for asec dir");
1733 return -1;
1734 }
1735
1736 struct dirent *result;
1737 while (!readdir_r(d, dent, &result) && result != NULL) {
1738 if (dent->d_name[0] == '.')
1739 continue;
1740 if (dent->d_type != DT_REG)
1741 continue;
1742 size_t name_len = strlen(dent->d_name);
1743 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1744 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1745 char id[ID_BUF_LEN];
1746 strlcpy(id, dent->d_name, name_len - 4);
1747 if (unmountAsec(id, true)) {
1748 /* Register the error, but try to unmount more asecs */
1749 rc = -1;
1750 }
1751 }
1752 }
1753 closedir(d);
1754
1755 free(dent);
1756
1757 return rc;
1758}
1759
San Mehata2677e42009-12-13 10:40:18 -08001760/*
1761 * Looks up a volume by it's label or mount-point
1762 */
San Mehat49e2bce2009-10-12 16:29:01 -07001763Volume *VolumeManager::lookupVolume(const char *label) {
1764 VolumeCollection::iterator i;
1765
1766 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001767 if (label[0] == '/') {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001768 if (!strcmp(label, (*i)->getFuseMountpoint()))
San Mehata2677e42009-12-13 10:40:18 -08001769 return (*i);
1770 } else {
1771 if (!strcmp(label, (*i)->getLabel()))
1772 return (*i);
1773 }
San Mehat49e2bce2009-10-12 16:29:01 -07001774 }
1775 return NULL;
1776}
San Mehata19b2502010-01-06 10:33:53 -08001777
1778bool VolumeManager::isMountpointMounted(const char *mp)
1779{
1780 char device[256];
1781 char mount_path[256];
1782 char rest[256];
1783 FILE *fp;
1784 char line[1024];
1785
1786 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001787 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001788 return false;
1789 }
1790
1791 while(fgets(line, sizeof(line), fp)) {
1792 line[strlen(line)-1] = '\0';
1793 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1794 if (!strcmp(mount_path, mp)) {
1795 fclose(fp);
1796 return true;
1797 }
San Mehata19b2502010-01-06 10:33:53 -08001798 }
1799
1800 fclose(fp);
1801 return false;
1802}
1803
San Mehat1a06eda2010-04-15 12:58:50 -07001804int VolumeManager::cleanupAsec(Volume *v, bool force) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001805 int rc = 0;
Kenny Root93ecb382012-08-09 11:28:37 -07001806
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001807 char asecFileName[255];
1808
1809 AsecIdCollection removeAsec;
1810 AsecIdCollection removeObb;
1811
Kenny Root93ecb382012-08-09 11:28:37 -07001812 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1813 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001814 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07001815
Kenny Rootcbacf782010-09-24 15:11:48 -07001816 if (cd->type == ASEC) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001817 if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) {
1818 SLOGE("Couldn't find ASEC %s; cleaning up", cd->id);
1819 removeAsec.push_back(cd);
1820 } else {
1821 SLOGD("Found ASEC at path %s", asecFileName);
1822 if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT,
1823 strlen(Volume::SEC_ASECDIR_EXT))) {
1824 removeAsec.push_back(cd);
1825 }
1826 }
Kenny Rootcbacf782010-09-24 15:11:48 -07001827 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07001828 if (v == getVolumeForFile(cd->id)) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001829 removeObb.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001830 }
1831 } else {
1832 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001833 }
1834 }
Kenny Root93ecb382012-08-09 11:28:37 -07001835
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001836 for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) {
Kenny Root93ecb382012-08-09 11:28:37 -07001837 ContainerData *cd = *it;
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001838 SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel());
1839 if (unmountAsec(cd->id, force)) {
1840 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1841 rc = -1;
1842 }
1843 }
1844
1845 for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) {
1846 ContainerData *cd = *it;
1847 SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel());
Kenny Root93ecb382012-08-09 11:28:37 -07001848 if (unmountObb(cd->id, force)) {
1849 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1850 rc = -1;
1851 }
1852 }
1853
1854 return rc;
San Mehat1a06eda2010-04-15 12:58:50 -07001855}
1856
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001857int VolumeManager::mkdirs(char* path) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001858 // Require that path lives under a volume we manage and is mounted
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001859 const char* emulated_source = getenv("EMULATED_STORAGE_SOURCE");
1860 const char* root = NULL;
Marco Nelissen5ab02e72013-10-15 15:22:28 -07001861 if (emulated_source && !strncmp(path, emulated_source, strlen(emulated_source))) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001862 root = emulated_source;
1863 } else {
1864 Volume* vol = getVolumeForFile(path);
Cylen Yao27cfee32014-05-02 19:23:42 +08001865 if (vol && vol->getState() == Volume::State_Mounted) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001866 root = vol->getMountpoint();
1867 }
1868 }
1869
1870 if (!root) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001871 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001872 return -EINVAL;
1873 }
1874
1875 /* fs_mkdirs() does symlink checking and relative path enforcement */
1876 return fs_mkdirs(path, 0700);
1877}