blob: f89d0ff009667af42294d69ca2b1b24e63328151 [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"
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +053057#define UICC0_MASS_STORAGE_PATH "/sys/class/android_usb/android0/f_mass_storage/uicc0/file"
Mike Lockwood97f2fc12011-06-07 10:51:38 -070058
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070059#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
60 + (number & (~((1U << po2) - 1))))
61
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070062/* writes superblock at end of file or device given by name */
63static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
64 int sbfd = open(name, O_RDWR);
65 if (sbfd < 0) {
66 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
67 return -1;
68 }
69
70 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
71 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
72 close(sbfd);
73 return -1;
74 }
75
76 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
77 SLOGE("Failed to write superblock (%s)", strerror(errno));
78 close(sbfd);
79 return -1;
80 }
81 close(sbfd);
82 return 0;
83}
84
85static int adjustSectorNumExt4(unsigned numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -070086 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
87 // preventing costly operations or unexpected ENOSPC error.
88 // Ext4::format() uses default block size without clustering.
89 unsigned clusterSectors = 4096 / 512;
90 unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
91 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070092 return ROUND_UP_POWER_OF_2(numSectors, 3);
93}
94
95static int adjustSectorNumFAT(unsigned numSectors) {
96 /*
97 * Add some headroom
98 */
99 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
100 numSectors += fatSize + 2;
101 /*
102 * FAT is aligned to 32 kb with 512b sectors.
103 */
104 return ROUND_UP_POWER_OF_2(numSectors, 6);
105}
106
107static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
108 if (Loop::lookupActive(idHash, buffer, len)) {
109 if (Loop::create(idHash, asecFileName, buffer, len)) {
110 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
111 return -1;
112 }
113 if (debug) {
114 SLOGD("New loop device created at %s", buffer);
115 }
116 } else {
117 if (debug) {
118 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
119 }
120 }
121 return 0;
122}
123
124static 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) {
125 if (strcmp(key, "none")) {
126 if (Devmapper::lookupActive(idHash, buffer, len)) {
127 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
128 buffer, len)) {
129 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
130 return -1;
131 }
132 if (debug) {
133 SLOGD("New devmapper instance created at %s", buffer);
134 }
135 } else {
136 if (debug) {
137 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
138 }
139 }
140 *createdDMDevice = true;
141 } else {
142 strcpy(buffer, loopDevice);
143 *createdDMDevice = false;
144 }
145 return 0;
146}
147
148static void waitForDevMapper(const char *dmDevice) {
149 /*
150 * Wait for the device mapper node to be created. Sometimes it takes a
151 * while. Wait for up to 1 second. We could also inspect incoming uevents,
152 * but that would take more effort.
153 */
154 int tries = 25;
155 while (tries--) {
156 if (!access(dmDevice, F_OK) || errno != ENOENT) {
157 break;
158 }
159 usleep(40 * 1000);
160 }
161}
162
San Mehatf1b736b2009-10-10 17:22:08 -0700163VolumeManager *VolumeManager::sInstance = NULL;
164
165VolumeManager *VolumeManager::Instance() {
166 if (!sInstance)
167 sInstance = new VolumeManager();
168 return sInstance;
169}
170
171VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800172 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700173 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -0800174 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700175 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400176 mUmsSharingCount = 0;
177 mSavedDirtyRatio = -1;
178 // set dirty ratio to 0 when UMS is active
179 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -0700180 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700181}
182
183VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800184 delete mVolumes;
185 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700186}
187
Kenny Root7b18a7b2010-03-15 13:13:41 -0700188char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700189 static const char* digits = "0123456789abcdef";
190
Kenny Root7b18a7b2010-03-15 13:13:41 -0700191 unsigned char sig[MD5_DIGEST_LENGTH];
192
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700193 if (buffer == NULL) {
194 SLOGE("Destination buffer is NULL");
195 errno = ESPIPE;
196 return NULL;
197 } else if (id == NULL) {
198 SLOGE("Source buffer is NULL");
199 errno = ESPIPE;
200 return NULL;
201 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800202 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700203 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800204 errno = ESPIPE;
205 return NULL;
206 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700207
208 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800209
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700210 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700211 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700212 *p++ = digits[sig[i] >> 4];
213 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800214 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700215 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800216
217 return buffer;
218}
219
220void VolumeManager::setDebug(bool enable) {
221 mDebug = enable;
222 VolumeCollection::iterator it;
223 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
224 (*it)->setDebug(enable);
225 }
226}
227
San Mehatf1b736b2009-10-10 17:22:08 -0700228int VolumeManager::start() {
229 return 0;
230}
231
232int VolumeManager::stop() {
233 return 0;
234}
235
236int VolumeManager::addVolume(Volume *v) {
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +0530237 // If entry is duplicated then mark isValid to false for both of them.
238 // Mark the valid entry on the first insertion of the card on handleBlockEvent()
239 VolumeCollection::iterator it;
240 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
241 if(!strncmp((*it)->getLabel(), v->getLabel(), strlen((*it)->getLabel()))) {
242 (*it)->setValidSysfs(false);
243 v->setValidSysfs(false);
244 }
245 }
246
San Mehatf1b736b2009-10-10 17:22:08 -0700247 mVolumes->push_back(v);
248 return 0;
249}
250
San Mehatfd7f5872009-10-12 11:32:47 -0700251void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
252 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700253
San Mehatfd7f5872009-10-12 11:32:47 -0700254 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700255 VolumeCollection::iterator it;
256 bool hit = false;
257 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700258 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800259#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700260 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800261#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700262 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700263 break;
264 }
265 }
266
267 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800268#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700269 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800270#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700271 }
272}
273
JP Abgrall40b64a62014-07-24 18:02:16 -0700274int VolumeManager::listVolumes(SocketClient *cli, bool broadcast) {
San Mehatf1b736b2009-10-10 17:22:08 -0700275 VolumeCollection::iterator i;
JP Abgrall40b64a62014-07-24 18:02:16 -0700276 char msg[256];
San Mehatf1b736b2009-10-10 17:22:08 -0700277
278 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +0530279 if ((*i)->isValidSysfs()) {
280 char *buffer;
281 asprintf(&buffer, "%s %s %d",
282 (*i)->getLabel(), (*i)->getFuseMountpoint(),
283 (*i)->getState());
284 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
285 free(buffer);
286 }
287
JP Abgrall40b64a62014-07-24 18:02:16 -0700288 if (broadcast) {
289 if((*i)->getUuid()) {
290 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
291 (*i)->getFuseMountpoint(), (*i)->getUuid());
292 mBroadcaster->sendBroadcast(ResponseCode::VolumeUuidChange,
293 msg, false);
294 }
295 if((*i)->getUserLabel()) {
296 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
297 (*i)->getFuseMountpoint(), (*i)->getUserLabel());
298 mBroadcaster->sendBroadcast(ResponseCode::VolumeUserLabelChange,
299 msg, false);
300 }
301 }
San Mehatf1b736b2009-10-10 17:22:08 -0700302 }
San Mehata2677e42009-12-13 10:40:18 -0800303 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700304 return 0;
305}
San Mehat49e2bce2009-10-12 16:29:01 -0700306
Ken Sumrall9caab762013-06-11 19:10:20 -0700307int VolumeManager::formatVolume(const char *label, bool wipe) {
San Mehata2677e42009-12-13 10:40:18 -0800308 Volume *v = lookupVolume(label);
309
310 if (!v) {
311 errno = ENOENT;
312 return -1;
313 }
314
Ken Sumrall3b170052011-07-11 15:38:57 -0700315 if (mVolManagerDisabled) {
316 errno = EBUSY;
317 return -1;
318 }
319
Ken Sumrall9caab762013-06-11 19:10:20 -0700320 return v->formatVol(wipe);
San Mehata2677e42009-12-13 10:40:18 -0800321}
322
Kenny Root508c0e12010-07-12 09:59:49 -0700323int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
324 char idHash[33];
325 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
326 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
327 return -1;
328 }
329
330 memset(mountPath, 0, mountPathLen);
rpcraigd1c226f2012-10-09 06:58:16 -0400331 int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
332 if ((written < 0) || (written >= mountPathLen)) {
333 errno = EINVAL;
334 return -1;
335 }
Kenny Root508c0e12010-07-12 09:59:49 -0700336
337 if (access(mountPath, F_OK)) {
338 errno = ENOENT;
339 return -1;
340 }
341
342 return 0;
343}
344
San Mehata19b2502010-01-06 10:33:53 -0800345int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700346 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700347
Nick Kralevich0de7c612014-01-27 14:58:06 -0800348 if (!isLegalAsecId(id)) {
349 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
350 errno = EINVAL;
351 return -1;
352 }
353
Kenny Root344ca102012-04-03 17:23:01 -0700354 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
355 SLOGE("Couldn't find ASEC %s", id);
356 return -1;
357 }
San Mehat88ac2c02010-03-23 11:15:58 -0700358
359 memset(buffer, 0, maxlen);
360 if (access(asecFileName, F_OK)) {
361 errno = ENOENT;
362 return -1;
363 }
San Mehata19b2502010-01-06 10:33:53 -0800364
rpcraigd1c226f2012-10-09 06:58:16 -0400365 int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
366 if ((written < 0) || (written >= maxlen)) {
367 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
368 errno = EINVAL;
369 return -1;
370 }
371
San Mehata19b2502010-01-06 10:33:53 -0800372 return 0;
373}
374
Dianne Hackborn736910c2011-06-27 13:37:07 -0700375int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
376 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700377
Nick Kralevich0de7c612014-01-27 14:58:06 -0800378 if (!isLegalAsecId(id)) {
379 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
380 errno = EINVAL;
381 return -1;
382 }
383
Kenny Root344ca102012-04-03 17:23:01 -0700384 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
385 SLOGE("Couldn't find ASEC %s", id);
386 return -1;
387 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700388
389 memset(buffer, 0, maxlen);
390 if (access(asecFileName, F_OK)) {
391 errno = ENOENT;
392 return -1;
393 }
394
rpcraigd1c226f2012-10-09 06:58:16 -0400395 int written = snprintf(buffer, maxlen, "%s", asecFileName);
396 if ((written < 0) || (written >= maxlen)) {
397 errno = EINVAL;
398 return -1;
399 }
400
Dianne Hackborn736910c2011-06-27 13:37:07 -0700401 return 0;
402}
403
Kenny Root344ca102012-04-03 17:23:01 -0700404int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
405 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800406 struct asec_superblock sb;
407 memset(&sb, 0, sizeof(sb));
408
Nick Kralevich0de7c612014-01-27 14:58:06 -0800409 if (!isLegalAsecId(id)) {
410 SLOGE("createAsec: Invalid asec id \"%s\"", id);
411 errno = EINVAL;
412 return -1;
413 }
414
Kenny Root344ca102012-04-03 17:23:01 -0700415 const bool wantFilesystem = strcmp(fstype, "none");
416 bool usingExt4 = false;
417 if (wantFilesystem) {
418 usingExt4 = !strcmp(fstype, "ext4");
419 if (usingExt4) {
420 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
421 } else if (strcmp(fstype, "fat")) {
422 SLOGE("Invalid filesystem type %s", fstype);
423 errno = EINVAL;
424 return -1;
425 }
426 }
427
San Mehatfcf24fe2010-03-03 12:37:32 -0800428 sb.magic = ASEC_SB_MAGIC;
429 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800430
San Mehatd31e3802010-02-18 08:37:45 -0800431 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700432 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800433 errno = EINVAL;
434 return -1;
435 }
436
San Mehata19b2502010-01-06 10:33:53 -0800437 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700438 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800439 errno = EADDRINUSE;
440 return -1;
441 }
442
443 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700444
445 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
446 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
447 asecFileName, strerror(errno));
448 errno = EADDRINUSE;
449 return -1;
450 }
451
452 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
453
rpcraigd1c226f2012-10-09 06:58:16 -0400454 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
455 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
456 errno = EINVAL;
457 return -1;
458 }
San Mehata19b2502010-01-06 10:33:53 -0800459
460 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700461 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700462 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800463 errno = EADDRINUSE;
464 return -1;
465 }
466
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700467 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700468 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700469 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700470 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700471 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800472
473 // Add +1 for our superblock which is at the end
474 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700475 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800476 return -1;
477 }
478
San Mehatd9a4e352010-03-12 13:32:47 -0800479 char idHash[33];
480 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700481 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800482 unlink(asecFileName);
483 return -1;
484 }
485
San Mehata19b2502010-01-06 10:33:53 -0800486 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800487 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700488 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800489 unlink(asecFileName);
490 return -1;
491 }
492
San Mehatb78a32c2010-01-10 13:02:12 -0800493 char dmDevice[255];
494 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800495
San Mehatb78a32c2010-01-10 13:02:12 -0800496 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800497 // XXX: This is all we support for now
498 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800499 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800500 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700501 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800502 Loop::destroyByDevice(loopDevice);
503 unlink(asecFileName);
504 return -1;
505 }
506 cleanupDm = true;
507 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800508 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800509 strcpy(dmDevice, loopDevice);
510 }
511
San Mehatfcf24fe2010-03-03 12:37:32 -0800512 /*
513 * Drop down the superblock at the end of the file
514 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700515 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800516 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800517 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800518 }
519 Loop::destroyByDevice(loopDevice);
520 unlink(asecFileName);
521 return -1;
522 }
523
Kenny Root344ca102012-04-03 17:23:01 -0700524 if (wantFilesystem) {
525 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400526 char mountPoint[255];
527
rpcraigd1c226f2012-10-09 06:58:16 -0400528 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
529 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
530 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
531 if (cleanupDm) {
532 Devmapper::destroy(idHash);
533 }
534 Loop::destroyByDevice(loopDevice);
535 unlink(asecFileName);
536 return -1;
537 }
rpcraiga54e13a2012-09-21 14:17:08 -0400538
Kenny Root344ca102012-04-03 17:23:01 -0700539 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700540 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700541 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700542 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800543 }
San Mehata19b2502010-01-06 10:33:53 -0800544
Kenny Root344ca102012-04-03 17:23:01 -0700545 if (formatStatus < 0) {
546 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800547 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800548 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800549 }
San Mehateb13a902010-01-07 12:12:50 -0800550 Loop::destroyByDevice(loopDevice);
551 unlink(asecFileName);
552 return -1;
553 }
Kenny Root344ca102012-04-03 17:23:01 -0700554
Kenny Root344ca102012-04-03 17:23:01 -0700555 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800556 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700557 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800558 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800559 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800560 }
561 Loop::destroyByDevice(loopDevice);
562 unlink(asecFileName);
563 return -1;
564 }
San Mehatb78a32c2010-01-10 13:02:12 -0800565 }
San Mehata1091cb2010-02-28 20:17:20 -0800566
Kenny Root344ca102012-04-03 17:23:01 -0700567 int mountStatus;
568 if (usingExt4) {
569 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
570 } else {
571 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
572 false);
573 }
574
575 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700576 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800577 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800578 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800579 }
580 Loop::destroyByDevice(loopDevice);
581 unlink(asecFileName);
582 return -1;
583 }
Kenny Root344ca102012-04-03 17:23:01 -0700584
585 if (usingExt4) {
586 int dirfd = open(mountPoint, O_DIRECTORY);
587 if (dirfd >= 0) {
588 if (fchown(dirfd, ownerUid, AID_SYSTEM)
589 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
590 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
591 }
592 close(dirfd);
593 }
594 }
San Mehata1091cb2010-02-28 20:17:20 -0800595 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700596 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800597 }
San Mehat88705162010-01-15 09:26:28 -0800598
Kenny Rootcbacf782010-09-24 15:11:48 -0700599 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800600 return 0;
601}
602
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700603int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
604 char asecFileName[255];
605 char mountPoint[255];
606 bool cleanupDm = false;
607
608 if (!isLegalAsecId(id)) {
609 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
610 errno = EINVAL;
611 return -1;
612 }
613
614 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
615 SLOGE("Couldn't find ASEC %s", id);
616 return -1;
617 }
618
619 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
620 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
621 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
622 return -1;
623 }
624
625 if (isMountpointMounted(mountPoint)) {
626 SLOGE("ASEC %s mounted. Unmount before resizing", id);
627 errno = EBUSY;
628 return -1;
629 }
630
631 struct asec_superblock sb;
632 int fd;
633 unsigned int oldNumSec = 0;
634
635 if ((fd = open(asecFileName, O_RDONLY)) < 0) {
636 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
637 return -1;
638 }
639
640 struct stat info;
641 if (fstat(fd, &info) < 0) {
642 SLOGE("Failed to get file size (%s)", strerror(errno));
643 close(fd);
644 return -1;
645 }
646
647 oldNumSec = info.st_size / 512;
648
649 unsigned numImgSectors;
650 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
651 numImgSectors = adjustSectorNumExt4(numSectors);
652 else
653 numImgSectors = adjustSectorNumFAT(numSectors);
654 /*
655 * add one block for the superblock
656 */
657 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700658 if (oldNumSec == numImgSectors + 1) {
659 SLOGW("Size unchanged; ignoring resize request");
660 return 0;
661 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700662 SLOGE("Only growing is currently supported.");
663 close(fd);
664 return -1;
665 }
666
667 /*
668 * Try to read superblock.
669 */
670 memset(&sb, 0, sizeof(struct asec_superblock));
671 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
672 SLOGE("lseek failed (%s)", strerror(errno));
673 close(fd);
674 return -1;
675 }
676 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
677 SLOGE("superblock read failed (%s)", strerror(errno));
678 close(fd);
679 return -1;
680 }
681 close(fd);
682
683 if (mDebug) {
684 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
685 }
686 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
687 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
688 errno = EMEDIUMTYPE;
689 return -1;
690 }
691
692 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
693 SLOGE("Only ext4 partitions are supported for resize");
694 errno = EINVAL;
695 return -1;
696 }
697
698 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
699 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
700 return -1;
701 }
702
703 /*
704 * Drop down a copy of the superblock at the end of the file
705 */
706 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
707 goto fail;
708
709 char idHash[33];
710 if (!asecHash(id, idHash, sizeof(idHash))) {
711 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
712 goto fail;
713 }
714
715 char loopDevice[255];
716 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
717 goto fail;
718
719 char dmDevice[255];
720
721 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
722 Loop::destroyByDevice(loopDevice);
723 goto fail;
724 }
725
726 /*
727 * Wait for the device mapper node to be created.
728 */
729 waitForDevMapper(dmDevice);
730
731 if (Ext4::resize(dmDevice, numImgSectors)) {
732 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
733 if (cleanupDm) {
734 Devmapper::destroy(idHash);
735 }
736 Loop::destroyByDevice(loopDevice);
737 goto fail;
738 }
739
740 return 0;
741fail:
742 Loop::resizeImageFile(asecFileName, oldNumSec);
743 return -1;
744}
745
San Mehata19b2502010-01-06 10:33:53 -0800746int VolumeManager::finalizeAsec(const char *id) {
747 char asecFileName[255];
748 char loopDevice[255];
749 char mountPoint[255];
750
Nick Kralevich0de7c612014-01-27 14:58:06 -0800751 if (!isLegalAsecId(id)) {
752 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
753 errno = EINVAL;
754 return -1;
755 }
756
Kenny Root344ca102012-04-03 17:23:01 -0700757 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
758 SLOGE("Couldn't find ASEC %s", id);
759 return -1;
760 }
San Mehata19b2502010-01-06 10:33:53 -0800761
San Mehatd9a4e352010-03-12 13:32:47 -0800762 char idHash[33];
763 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700764 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800765 return -1;
766 }
767
768 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700769 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800770 return -1;
771 }
772
Kenny Root344ca102012-04-03 17:23:01 -0700773 unsigned int nr_sec = 0;
774 struct asec_superblock sb;
775
776 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
777 return -1;
778 }
779
rpcraigd1c226f2012-10-09 06:58:16 -0400780 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
781 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
782 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
783 return -1;
784 }
Kenny Root344ca102012-04-03 17:23:01 -0700785
786 int result = 0;
787 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
788 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
789 } else {
790 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
791 }
792
793 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700794 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800795 return -1;
796 }
797
San Mehatd9a4e352010-03-12 13:32:47 -0800798 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700799 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800800 }
San Mehata19b2502010-01-06 10:33:53 -0800801 return 0;
802}
803
Kenny Root344ca102012-04-03 17:23:01 -0700804int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
805 char asecFileName[255];
806 char loopDevice[255];
807 char mountPoint[255];
808
809 if (gid < AID_APP) {
810 SLOGE("Group ID is not in application range");
811 return -1;
812 }
813
Nick Kralevich0de7c612014-01-27 14:58:06 -0800814 if (!isLegalAsecId(id)) {
815 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
816 errno = EINVAL;
817 return -1;
818 }
819
Kenny Root344ca102012-04-03 17:23:01 -0700820 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
821 SLOGE("Couldn't find ASEC %s", id);
822 return -1;
823 }
824
825 char idHash[33];
826 if (!asecHash(id, idHash, sizeof(idHash))) {
827 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
828 return -1;
829 }
830
831 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
832 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
833 return -1;
834 }
835
836 unsigned int nr_sec = 0;
837 struct asec_superblock sb;
838
839 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
840 return -1;
841 }
842
rpcraigd1c226f2012-10-09 06:58:16 -0400843 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
844 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
845 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
846 return -1;
847 }
Kenny Root344ca102012-04-03 17:23:01 -0700848
849 int result = 0;
850 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
851 return 0;
852 }
853
854 int ret = Ext4::doMount(loopDevice, mountPoint,
855 false /* read-only */,
856 true /* remount */,
857 false /* executable */);
858 if (ret) {
859 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
860 return -1;
861 }
862
863 char *paths[] = { mountPoint, NULL };
864
865 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
866 if (fts) {
867 // Traverse the entire hierarchy and chown to system UID.
868 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
869 // We don't care about the lost+found directory.
870 if (!strcmp(ftsent->fts_name, "lost+found")) {
871 continue;
872 }
873
874 /*
875 * There can only be one file marked as private right now.
876 * This should be more robust, but it satisfies the requirements
877 * we have for right now.
878 */
879 const bool privateFile = !strcmp(ftsent->fts_name, filename);
880
881 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
882 if (fd < 0) {
883 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
884 result = -1;
885 continue;
886 }
887
888 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
889
890 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700891 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700892 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700893 result |= fchmod(fd, privateFile ? 0640 : 0644);
894 }
Robert Craigb9e3ba52014-02-04 10:53:00 -0500895
Stephen Smalley5093e612014-02-12 09:43:08 -0500896 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -0500897 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
898 result |= -1;
899 }
900
Kenny Root344ca102012-04-03 17:23:01 -0700901 close(fd);
902 }
903 fts_close(fts);
904
905 // Finally make the directory readable by everyone.
906 int dirfd = open(mountPoint, O_DIRECTORY);
907 if (dirfd < 0 || fchmod(dirfd, 0755)) {
908 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
909 result |= -1;
910 }
911 close(dirfd);
912 } else {
913 result |= -1;
914 }
915
916 result |= Ext4::doMount(loopDevice, mountPoint,
917 true /* read-only */,
918 true /* remount */,
919 true /* execute */);
920
921 if (result) {
922 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
923 return -1;
924 }
925
926 if (mDebug) {
927 SLOGD("ASEC %s permissions fixed", id);
928 }
929 return 0;
930}
931
San Mehat048b0802010-01-23 08:17:06 -0800932int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700933 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800934 char *asecFilename2;
935 char mountPoint[255];
936
Kenny Root344ca102012-04-03 17:23:01 -0700937 const char *dir;
938
Nick Kralevich0de7c612014-01-27 14:58:06 -0800939 if (!isLegalAsecId(id1)) {
940 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
941 errno = EINVAL;
942 return -1;
943 }
944
945 if (!isLegalAsecId(id2)) {
946 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
947 errno = EINVAL;
948 return -1;
949 }
950
Kenny Root344ca102012-04-03 17:23:01 -0700951 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
952 SLOGE("Couldn't find ASEC %s", id1);
953 return -1;
954 }
955
956 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800957
rpcraigd1c226f2012-10-09 06:58:16 -0400958 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
959 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
960 SLOGE("Rename failed: couldn't construct mountpoint");
961 goto out_err;
962 }
963
San Mehat048b0802010-01-23 08:17:06 -0800964 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700965 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800966 errno = EBUSY;
967 goto out_err;
968 }
969
rpcraigd1c226f2012-10-09 06:58:16 -0400970 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
971 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
972 SLOGE("Rename failed: couldn't construct mountpoint2");
973 goto out_err;
974 }
975
San Mehat96956ed2010-02-24 08:42:51 -0800976 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700977 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800978 errno = EBUSY;
979 goto out_err;
980 }
981
San Mehat048b0802010-01-23 08:17:06 -0800982 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700983 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800984 errno = EADDRINUSE;
985 goto out_err;
986 }
987
988 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700989 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800990 goto out_err;
991 }
992
San Mehat048b0802010-01-23 08:17:06 -0800993 free(asecFilename2);
994 return 0;
995
996out_err:
San Mehat048b0802010-01-23 08:17:06 -0800997 free(asecFilename2);
998 return -1;
999}
1000
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001001#define UNMOUNT_RETRIES 5
1002#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001003int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001004 char asecFileName[255];
1005 char mountPoint[255];
1006
Nick Kralevich0de7c612014-01-27 14:58:06 -08001007 if (!isLegalAsecId(id)) {
1008 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1009 errno = EINVAL;
1010 return -1;
1011 }
1012
Kenny Root344ca102012-04-03 17:23:01 -07001013 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1014 SLOGE("Couldn't find ASEC %s", id);
1015 return -1;
1016 }
1017
rpcraigd1c226f2012-10-09 06:58:16 -04001018 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1019 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1020 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1021 return -1;
1022 }
San Mehata19b2502010-01-06 10:33:53 -08001023
San Mehatd9a4e352010-03-12 13:32:47 -08001024 char idHash[33];
1025 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001026 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001027 return -1;
1028 }
1029
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001030 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1031}
1032
Kenny Root508c0e12010-07-12 09:59:49 -07001033int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001034 char mountPoint[255];
1035
1036 char idHash[33];
1037 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1038 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1039 return -1;
1040 }
1041
rpcraigd1c226f2012-10-09 06:58:16 -04001042 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1043 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1044 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1045 return -1;
1046 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001047
1048 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1049}
1050
1051int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1052 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001053 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001054 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001055 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001056 return -1;
1057 }
San Mehat23969932010-01-09 07:08:06 -08001058
San Mehatb78a32c2010-01-10 13:02:12 -08001059 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001060 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001061 rc = umount(mountPoint);
1062 if (!rc) {
1063 break;
San Mehata19b2502010-01-06 10:33:53 -08001064 }
San Mehatb78a32c2010-01-10 13:02:12 -08001065 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001066 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001067 rc = 0;
1068 break;
San Mehata19b2502010-01-06 10:33:53 -08001069 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001070 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001071 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001072
San Mehat4ba89482010-02-18 09:00:18 -08001073 int action = 0; // default is to just complain
1074
1075 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001076 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -08001077 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001078 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -08001079 action = 1; // SIGHUP
1080 }
San Mehat8c940ef2010-02-13 14:19:53 -08001081
San Mehat586536c2010-02-16 17:12:00 -08001082 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001083 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001084 }
1085
1086 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001087 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001088 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001089 return -1;
1090 }
1091
San Mehat12f4b892010-02-24 11:43:22 -08001092 int retries = 10;
1093
1094 while(retries--) {
1095 if (!rmdir(mountPoint)) {
1096 break;
1097 }
1098
San Mehat97ac40e2010-03-24 10:24:19 -07001099 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001100 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001101 }
1102
1103 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001104 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001105 }
San Mehat88705162010-01-15 09:26:28 -08001106
Paul Lawrence60dec162014-09-02 10:52:15 -07001107 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1108 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1109 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1110 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1111 continue;
1112 } else {
1113 break;
1114 }
San Mehata19b2502010-01-06 10:33:53 -08001115 }
1116
1117 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001118 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001119 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001120 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001121 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001122 }
San Mehat88705162010-01-15 09:26:28 -08001123
1124 AsecIdCollection::iterator it;
1125 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001126 ContainerData* cd = *it;
1127 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001128 free(*it);
1129 mActiveContainers->erase(it);
1130 break;
1131 }
1132 }
1133 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001134 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001135 }
San Mehatb78a32c2010-01-10 13:02:12 -08001136 return 0;
1137}
1138
San Mehat4ba89482010-02-18 09:00:18 -08001139int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001140 char asecFileName[255];
1141 char mountPoint[255];
1142
Nick Kralevich0de7c612014-01-27 14:58:06 -08001143 if (!isLegalAsecId(id)) {
1144 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1145 errno = EINVAL;
1146 return -1;
1147 }
1148
Kenny Root344ca102012-04-03 17:23:01 -07001149 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1150 SLOGE("Couldn't find ASEC %s", id);
1151 return -1;
1152 }
1153
rpcraigd1c226f2012-10-09 06:58:16 -04001154 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1155 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1156 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1157 return -1;
1158 }
San Mehatb78a32c2010-01-10 13:02:12 -08001159
San Mehat0586d542010-01-12 15:38:59 -08001160 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001161 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001162 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001163 }
San Mehat4ba89482010-02-18 09:00:18 -08001164 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001165 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001166 return -1;
1167 }
1168 }
San Mehata19b2502010-01-06 10:33:53 -08001169
San Mehat0586d542010-01-12 15:38:59 -08001170 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001171 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001172 return -1;
1173 }
San Mehata19b2502010-01-06 10:33:53 -08001174
San Mehatd9a4e352010-03-12 13:32:47 -08001175 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001176 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001177 }
San Mehata19b2502010-01-06 10:33:53 -08001178 return 0;
1179}
1180
Nick Kralevich0de7c612014-01-27 14:58:06 -08001181/*
1182 * Legal ASEC ids consist of alphanumeric characters, '-',
1183 * '_', or '.'. ".." is not allowed. The first or last character
1184 * of the ASEC id cannot be '.' (dot).
1185 */
1186bool VolumeManager::isLegalAsecId(const char *id) const {
1187 size_t i;
1188 size_t len = strlen(id);
1189
1190 if (len == 0) {
1191 return false;
1192 }
1193 if ((id[0] == '.') || (id[len - 1] == '.')) {
1194 return false;
1195 }
1196
1197 for (i = 0; i < len; i++) {
1198 if (id[i] == '.') {
1199 // i=0 is guaranteed never to have a dot. See above.
1200 if (id[i-1] == '.') return false;
1201 continue;
1202 }
1203 if (id[i] == '_' || id[i] == '-') continue;
1204 if (id[i] >= 'a' && id[i] <= 'z') continue;
1205 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1206 if (id[i] >= '0' && id[i] <= '9') continue;
1207 return false;
1208 }
1209
1210 return true;
1211}
1212
Kenny Root344ca102012-04-03 17:23:01 -07001213bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1214 int dirfd = open(dir, O_DIRECTORY);
1215 if (dirfd < 0) {
1216 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
1217 return -1;
1218 }
1219
1220 bool ret = false;
1221
1222 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
1223 ret = true;
1224 }
1225
1226 close(dirfd);
1227
1228 return ret;
1229}
1230
1231int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1232 const char **directory) const {
1233 int dirfd, fd;
1234 const int idLen = strlen(id);
1235 char *asecName;
1236
Nick Kralevich0de7c612014-01-27 14:58:06 -08001237 if (!isLegalAsecId(id)) {
1238 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1239 errno = EINVAL;
1240 return -1;
1241 }
1242
Kenny Root344ca102012-04-03 17:23:01 -07001243 if (asprintf(&asecName, "%s.asec", id) < 0) {
1244 SLOGE("Couldn't allocate string to write ASEC name");
1245 return -1;
1246 }
1247
1248 const char *dir;
1249 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
1250 dir = Volume::SEC_ASECDIR_INT;
1251 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
1252 dir = Volume::SEC_ASECDIR_EXT;
1253 } else {
1254 free(asecName);
1255 return -1;
1256 }
1257
1258 if (directory != NULL) {
1259 *directory = dir;
1260 }
1261
1262 if (asecPath != NULL) {
1263 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001264 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1265 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001266 free(asecName);
1267 return -1;
1268 }
1269 }
1270
1271 free(asecName);
1272 return 0;
1273}
1274
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001275int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001276 char asecFileName[255];
1277 char mountPoint[255];
1278
Nick Kralevich0de7c612014-01-27 14:58:06 -08001279 if (!isLegalAsecId(id)) {
1280 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1281 errno = EINVAL;
1282 return -1;
1283 }
1284
Kenny Root344ca102012-04-03 17:23:01 -07001285 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1286 SLOGE("Couldn't find ASEC %s", id);
1287 return -1;
1288 }
1289
rpcraigd1c226f2012-10-09 06:58:16 -04001290 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1291 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001292 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001293 return -1;
1294 }
San Mehata19b2502010-01-06 10:33:53 -08001295
1296 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001297 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001298 errno = EBUSY;
1299 return -1;
1300 }
1301
San Mehatd9a4e352010-03-12 13:32:47 -08001302 char idHash[33];
1303 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001304 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001305 return -1;
1306 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001307
San Mehata19b2502010-01-06 10:33:53 -08001308 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001309 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1310 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001311
1312 char dmDevice[255];
1313 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -08001314 int fd;
1315 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001316 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001317
Kenny Root344ca102012-04-03 17:23:01 -07001318 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1319 return -1;
1320 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001321
San Mehatd9a4e352010-03-12 13:32:47 -08001322 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001323 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001324 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001325 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001326 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001327 Loop::destroyByDevice(loopDevice);
1328 errno = EMEDIUMTYPE;
1329 return -1;
1330 }
1331 nr_sec--; // We don't want the devmapping to extend onto our superblock
1332
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001333 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1334 Loop::destroyByDevice(loopDevice);
1335 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001336 }
1337
Kenny Root344ca102012-04-03 17:23:01 -07001338 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001339 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001340 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001341 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001342 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001343 }
1344 Loop::destroyByDevice(loopDevice);
1345 return -1;
1346 }
San Mehata19b2502010-01-06 10:33:53 -08001347 }
1348
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001349 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001350 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001351 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001352 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001353
Kenny Root344ca102012-04-03 17:23:01 -07001354 int result;
1355 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001356 result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001357 } else {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001358 result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001359 }
1360
1361 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001362 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001363 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001364 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001365 }
1366 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001367 return -1;
1368 }
1369
Kenny Rootcbacf782010-09-24 15:11:48 -07001370 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001371 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001372 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001373 }
San Mehata19b2502010-01-06 10:33:53 -08001374 return 0;
1375}
1376
Kenny Root93ecb382012-08-09 11:28:37 -07001377Volume* VolumeManager::getVolumeForFile(const char *fileName) {
1378 VolumeCollection::iterator i;
1379
1380 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +05301381 if ((*i)->isValidSysfs()) {
1382 const char* mountPoint = (*i)->getFuseMountpoint();
1383 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
1384 return *i;
1385 }
Kenny Root93ecb382012-08-09 11:28:37 -07001386 }
1387 }
1388
1389 return NULL;
1390}
1391
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001392/**
1393 * Mounts an image file <code>img</code>.
1394 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001395int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001396 char mountPoint[255];
1397
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001398 char idHash[33];
1399 if (!asecHash(img, idHash, sizeof(idHash))) {
1400 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1401 return -1;
1402 }
1403
rpcraigd1c226f2012-10-09 06:58:16 -04001404 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1405 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001406 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001407 return -1;
1408 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001409
1410 if (isMountpointMounted(mountPoint)) {
1411 SLOGE("Image %s already mounted", img);
1412 errno = EBUSY;
1413 return -1;
1414 }
1415
1416 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001417 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1418 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001419
1420 char dmDevice[255];
1421 bool cleanupDm = false;
1422 int fd;
1423 unsigned int nr_sec = 0;
1424
1425 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1426 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1427 Loop::destroyByDevice(loopDevice);
1428 return -1;
1429 }
1430
1431 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1432 SLOGE("Failed to get loop size (%s)", strerror(errno));
1433 Loop::destroyByDevice(loopDevice);
1434 close(fd);
1435 return -1;
1436 }
1437
1438 close(fd);
1439
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001440 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash , nr_sec, &cleanupDm, mDebug)) {
1441 Loop::destroyByDevice(loopDevice);
1442 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001443 }
1444
1445 if (mkdir(mountPoint, 0755)) {
1446 if (errno != EEXIST) {
1447 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1448 if (cleanupDm) {
1449 Devmapper::destroy(idHash);
1450 }
1451 Loop::destroyByDevice(loopDevice);
1452 return -1;
1453 }
1454 }
1455
Jeff Sharkey69479042012-09-25 16:14:57 -07001456 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001457 0227, false)) {
1458 SLOGE("Image mount failed (%s)", strerror(errno));
1459 if (cleanupDm) {
1460 Devmapper::destroy(idHash);
1461 }
1462 Loop::destroyByDevice(loopDevice);
1463 return -1;
1464 }
1465
Kenny Rootcbacf782010-09-24 15:11:48 -07001466 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001467 if (mDebug) {
1468 SLOGD("Image %s mounted", img);
1469 }
1470 return 0;
1471}
1472
San Mehat49e2bce2009-10-12 16:29:01 -07001473int VolumeManager::mountVolume(const char *label) {
1474 Volume *v = lookupVolume(label);
1475
1476 if (!v) {
1477 errno = ENOENT;
1478 return -1;
1479 }
1480
San Mehata2677e42009-12-13 10:40:18 -08001481 return v->mountVol();
1482}
1483
Kenny Root508c0e12010-07-12 09:59:49 -07001484int VolumeManager::listMountedObbs(SocketClient* cli) {
1485 char device[256];
1486 char mount_path[256];
1487 char rest[256];
1488 FILE *fp;
1489 char line[1024];
1490
1491 if (!(fp = fopen("/proc/mounts", "r"))) {
1492 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1493 return -1;
1494 }
1495
1496 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001497 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001498 char loopDir[loopDirLen + 2];
1499 strcpy(loopDir, Volume::LOOPDIR);
1500 loopDir[loopDirLen++] = '/';
1501 loopDir[loopDirLen] = '\0';
1502
1503 while(fgets(line, sizeof(line), fp)) {
1504 line[strlen(line)-1] = '\0';
1505
1506 /*
1507 * Should look like:
1508 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1509 */
1510 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1511
1512 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1513 int fd = open(device, O_RDONLY);
1514 if (fd >= 0) {
1515 struct loop_info64 li;
1516 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1517 cli->sendMsg(ResponseCode::AsecListResult,
1518 (const char*) li.lo_file_name, false);
1519 }
1520 close(fd);
1521 }
1522 }
1523 }
1524
1525 fclose(fp);
1526 return 0;
1527}
1528
San Mehateba65e92010-01-29 05:15:16 -08001529int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1530 Volume *v = lookupVolume(label);
1531
1532 if (!v) {
1533 errno = ENOENT;
1534 return -1;
1535 }
1536
1537 if (strcmp(method, "ums")) {
1538 errno = ENOSYS;
1539 return -1;
1540 }
1541
1542 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001543 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001544 } else {
1545 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001546 }
1547 return 0;
1548}
1549
San Mehata2677e42009-12-13 10:40:18 -08001550int VolumeManager::shareVolume(const char *label, const char *method) {
1551 Volume *v = lookupVolume(label);
1552
1553 if (!v) {
1554 errno = ENOENT;
1555 return -1;
1556 }
1557
1558 /*
1559 * Eventually, we'll want to support additional share back-ends,
1560 * some of which may work while the media is mounted. For now,
1561 * we just support UMS
1562 */
1563 if (strcmp(method, "ums")) {
1564 errno = ENOSYS;
1565 return -1;
1566 }
1567
1568 if (v->getState() == Volume::State_NoMedia) {
1569 errno = ENODEV;
1570 return -1;
1571 }
1572
San Mehat49e2bce2009-10-12 16:29:01 -07001573 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001574 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001575 errno = EBUSY;
1576 return -1;
1577 }
1578
Ken Sumrall3b170052011-07-11 15:38:57 -07001579 if (mVolManagerDisabled) {
1580 errno = EBUSY;
1581 return -1;
1582 }
1583
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001584 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001585 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1586 // This volume does not support raw disk access
1587 errno = EINVAL;
1588 return -1;
1589 }
1590
1591 int fd;
1592 char nodepath[255];
rpcraigd1c226f2012-10-09 06:58:16 -04001593 int written = snprintf(nodepath,
San Mehata2677e42009-12-13 10:40:18 -08001594 sizeof(nodepath), "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -08001595 major(d), minor(d));
San Mehata2677e42009-12-13 10:40:18 -08001596
rpcraigd1c226f2012-10-09 06:58:16 -04001597 if ((written < 0) || (size_t(written) >= sizeof(nodepath))) {
1598 SLOGE("shareVolume failed: couldn't construct nodepath");
1599 return -1;
1600 }
1601
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +05301602 if (!strncmp(v->getLabel(), "uicc0", strlen("uicc0"))) {
1603 if ((fd = open(UICC0_MASS_STORAGE_PATH, O_WRONLY)) < 0) {
1604 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
1605 return -1;
1606 }
1607 } else {
1608 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
1609 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
1610 return -1;
1611 }
1612 }
1613
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001614 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001615 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001616 return -1;
1617 }
1618
1619 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001620 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001621 close(fd);
1622 return -1;
1623 }
1624
1625 close(fd);
1626 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001627 if (mUmsSharingCount++ == 0) {
1628 FILE* fp;
1629 mSavedDirtyRatio = -1; // in case we fail
1630 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1631 char line[16];
1632 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1633 fprintf(fp, "%d\n", mUmsDirtyRatio);
1634 } else {
1635 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1636 }
1637 fclose(fp);
1638 } else {
1639 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1640 }
1641 }
San Mehata2677e42009-12-13 10:40:18 -08001642 return 0;
1643}
1644
1645int VolumeManager::unshareVolume(const char *label, const char *method) {
1646 Volume *v = lookupVolume(label);
1647
1648 if (!v) {
1649 errno = ENOENT;
1650 return -1;
1651 }
1652
1653 if (strcmp(method, "ums")) {
1654 errno = ENOSYS;
1655 return -1;
1656 }
1657
1658 if (v->getState() != Volume::State_Shared) {
1659 errno = EINVAL;
1660 return -1;
1661 }
1662
San Mehata2677e42009-12-13 10:40:18 -08001663 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001664 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001665 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001666 return -1;
1667 }
1668
1669 char ch = 0;
1670 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001671 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001672 close(fd);
1673 return -1;
1674 }
1675
1676 close(fd);
1677 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001678 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1679 FILE* fp;
1680 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1681 fprintf(fp, "%d\n", mSavedDirtyRatio);
1682 fclose(fp);
1683 } else {
1684 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1685 }
1686 mSavedDirtyRatio = -1;
1687 }
San Mehata2677e42009-12-13 10:40:18 -08001688 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001689}
1690
Ken Sumrall3b170052011-07-11 15:38:57 -07001691extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001692 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001693 vm->disableVolumeManager();
1694 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001695 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001696}
1697
1698extern "C" int vold_getNumDirectVolumes(void) {
1699 VolumeManager *vm = VolumeManager::Instance();
1700 return vm->getNumDirectVolumes();
1701}
1702
1703int VolumeManager::getNumDirectVolumes(void) {
1704 VolumeCollection::iterator i;
1705 int n=0;
1706
1707 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +05301708 if ((*i)->isValidSysfs()) {
1709 if ((*i)->getShareDevice() != (dev_t)0) {
1710 n++;
1711 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001712 }
1713 }
1714 return n;
1715}
1716
1717extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1718 VolumeManager *vm = VolumeManager::Instance();
1719 return vm->getDirectVolumeList(vol_list);
1720}
1721
1722int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1723 VolumeCollection::iterator i;
1724 int n=0;
1725 dev_t d;
1726
1727 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +05301728 if ((*i)->isValidSysfs()) {
1729 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1730 (*i)->getVolInfo(&vol_list[n]);
1731 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
1732 "/dev/block/vold/%d:%d", major(d), minor(d));
1733 n++;
1734 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001735 }
1736 }
1737
1738 return 0;
1739}
1740
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001741int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001742 Volume *v = lookupVolume(label);
1743
1744 if (!v) {
1745 errno = ENOENT;
1746 return -1;
1747 }
1748
San Mehata2677e42009-12-13 10:40:18 -08001749 if (v->getState() == Volume::State_NoMedia) {
1750 errno = ENODEV;
1751 return -1;
1752 }
1753
San Mehat49e2bce2009-10-12 16:29:01 -07001754 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001755 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001756 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001757 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001758 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001759 }
1760
San Mehat1a06eda2010-04-15 12:58:50 -07001761 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001762
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001763 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001764}
1765
Ken Sumrall425524d2012-06-14 20:55:28 -07001766extern "C" int vold_unmountAllAsecs(void) {
1767 int rc;
1768
1769 VolumeManager *vm = VolumeManager::Instance();
1770 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1771 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1772 rc = -1;
1773 }
1774 return rc;
1775}
1776
1777#define ID_BUF_LEN 256
1778#define ASEC_SUFFIX ".asec"
1779#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1780int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1781 DIR *d = opendir(directory);
1782 int rc = 0;
1783
1784 if (!d) {
1785 SLOGE("Could not open asec dir %s", directory);
1786 return -1;
1787 }
1788
1789 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -07001790 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Ken Sumrall425524d2012-06-14 20:55:28 -07001791
1792 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1793 if (dent == NULL) {
1794 SLOGE("Failed to allocate memory for asec dir");
1795 return -1;
1796 }
1797
1798 struct dirent *result;
1799 while (!readdir_r(d, dent, &result) && result != NULL) {
1800 if (dent->d_name[0] == '.')
1801 continue;
1802 if (dent->d_type != DT_REG)
1803 continue;
1804 size_t name_len = strlen(dent->d_name);
1805 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1806 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1807 char id[ID_BUF_LEN];
1808 strlcpy(id, dent->d_name, name_len - 4);
1809 if (unmountAsec(id, true)) {
1810 /* Register the error, but try to unmount more asecs */
1811 rc = -1;
1812 }
1813 }
1814 }
1815 closedir(d);
1816
1817 free(dent);
1818
1819 return rc;
1820}
1821
San Mehata2677e42009-12-13 10:40:18 -08001822/*
1823 * Looks up a volume by it's label or mount-point
1824 */
San Mehat49e2bce2009-10-12 16:29:01 -07001825Volume *VolumeManager::lookupVolume(const char *label) {
1826 VolumeCollection::iterator i;
1827
1828 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Uday Kiran Jandhyalacef44122014-09-04 11:45:32 +05301829 if ((*i)->isValidSysfs()) {
1830 if (label[0] == '/') {
1831 if (!strcmp(label, (*i)->getFuseMountpoint()))
1832 return (*i);
1833 } else {
1834 if (!strcmp(label, (*i)->getLabel()))
1835 return (*i);
1836 }
San Mehata2677e42009-12-13 10:40:18 -08001837 }
San Mehat49e2bce2009-10-12 16:29:01 -07001838 }
1839 return NULL;
1840}
San Mehata19b2502010-01-06 10:33:53 -08001841
1842bool VolumeManager::isMountpointMounted(const char *mp)
1843{
1844 char device[256];
1845 char mount_path[256];
1846 char rest[256];
1847 FILE *fp;
1848 char line[1024];
1849
1850 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001851 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001852 return false;
1853 }
1854
1855 while(fgets(line, sizeof(line), fp)) {
1856 line[strlen(line)-1] = '\0';
1857 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1858 if (!strcmp(mount_path, mp)) {
1859 fclose(fp);
1860 return true;
1861 }
San Mehata19b2502010-01-06 10:33:53 -08001862 }
1863
1864 fclose(fp);
1865 return false;
1866}
1867
San Mehat1a06eda2010-04-15 12:58:50 -07001868int VolumeManager::cleanupAsec(Volume *v, bool force) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001869 int rc = 0;
Kenny Root93ecb382012-08-09 11:28:37 -07001870
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001871 char asecFileName[255];
1872
1873 AsecIdCollection removeAsec;
1874 AsecIdCollection removeObb;
1875
Kenny Root93ecb382012-08-09 11:28:37 -07001876 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1877 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001878 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07001879
Kenny Rootcbacf782010-09-24 15:11:48 -07001880 if (cd->type == ASEC) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001881 if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) {
1882 SLOGE("Couldn't find ASEC %s; cleaning up", cd->id);
1883 removeAsec.push_back(cd);
1884 } else {
1885 SLOGD("Found ASEC at path %s", asecFileName);
1886 if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT,
1887 strlen(Volume::SEC_ASECDIR_EXT))) {
1888 removeAsec.push_back(cd);
1889 }
1890 }
Kenny Rootcbacf782010-09-24 15:11:48 -07001891 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07001892 if (v == getVolumeForFile(cd->id)) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001893 removeObb.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001894 }
1895 } else {
1896 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001897 }
1898 }
Kenny Root93ecb382012-08-09 11:28:37 -07001899
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001900 for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) {
Kenny Root93ecb382012-08-09 11:28:37 -07001901 ContainerData *cd = *it;
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001902 SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel());
1903 if (unmountAsec(cd->id, force)) {
1904 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1905 rc = -1;
1906 }
1907 }
1908
1909 for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) {
1910 ContainerData *cd = *it;
1911 SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel());
Kenny Root93ecb382012-08-09 11:28:37 -07001912 if (unmountObb(cd->id, force)) {
1913 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1914 rc = -1;
1915 }
1916 }
1917
1918 return rc;
San Mehat1a06eda2010-04-15 12:58:50 -07001919}
1920
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001921int VolumeManager::mkdirs(char* path) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001922 // Require that path lives under a volume we manage and is mounted
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001923 const char* emulated_source = getenv("EMULATED_STORAGE_SOURCE");
1924 const char* root = NULL;
Marco Nelissen5ab02e72013-10-15 15:22:28 -07001925 if (emulated_source && !strncmp(path, emulated_source, strlen(emulated_source))) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001926 root = emulated_source;
1927 } else {
1928 Volume* vol = getVolumeForFile(path);
Cylen Yao27cfee32014-05-02 19:23:42 +08001929 if (vol && vol->getState() == Volume::State_Mounted) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001930 root = vol->getMountpoint();
1931 }
1932 }
1933
1934 if (!root) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001935 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001936 return -EINVAL;
1937 }
1938
1939 /* fs_mkdirs() does symlink checking and relative path enforcement */
1940 return fs_mkdirs(path, 0700);
1941}