blob: 06daf40d2a60743680bcea518eafd351bc41e483 [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) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -070085 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
86 // preventing costly operations or unexpected ENOSPC error.
87 // Ext4::format() uses default block size without clustering.
88 unsigned clusterSectors = 4096 / 512;
89 unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
90 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070091 return ROUND_UP_POWER_OF_2(numSectors, 3);
92}
93
94static int adjustSectorNumFAT(unsigned numSectors) {
95 /*
96 * Add some headroom
97 */
98 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
99 numSectors += fatSize + 2;
100 /*
101 * FAT is aligned to 32 kb with 512b sectors.
102 */
103 return ROUND_UP_POWER_OF_2(numSectors, 6);
104}
105
106static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
107 if (Loop::lookupActive(idHash, buffer, len)) {
108 if (Loop::create(idHash, asecFileName, buffer, len)) {
109 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
110 return -1;
111 }
112 if (debug) {
113 SLOGD("New loop device created at %s", buffer);
114 }
115 } else {
116 if (debug) {
117 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
118 }
119 }
120 return 0;
121}
122
123static 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) {
124 if (strcmp(key, "none")) {
125 if (Devmapper::lookupActive(idHash, buffer, len)) {
126 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
127 buffer, len)) {
128 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
129 return -1;
130 }
131 if (debug) {
132 SLOGD("New devmapper instance created at %s", buffer);
133 }
134 } else {
135 if (debug) {
136 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
137 }
138 }
139 *createdDMDevice = true;
140 } else {
141 strcpy(buffer, loopDevice);
142 *createdDMDevice = false;
143 }
144 return 0;
145}
146
147static void waitForDevMapper(const char *dmDevice) {
148 /*
149 * Wait for the device mapper node to be created. Sometimes it takes a
150 * while. Wait for up to 1 second. We could also inspect incoming uevents,
151 * but that would take more effort.
152 */
153 int tries = 25;
154 while (tries--) {
155 if (!access(dmDevice, F_OK) || errno != ENOENT) {
156 break;
157 }
158 usleep(40 * 1000);
159 }
160}
161
San Mehatf1b736b2009-10-10 17:22:08 -0700162VolumeManager *VolumeManager::sInstance = NULL;
163
164VolumeManager *VolumeManager::Instance() {
165 if (!sInstance)
166 sInstance = new VolumeManager();
167 return sInstance;
168}
169
170VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800171 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700172 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -0800173 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700174 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400175 mUmsSharingCount = 0;
176 mSavedDirtyRatio = -1;
177 // set dirty ratio to 0 when UMS is active
178 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -0700179 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700180}
181
182VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800183 delete mVolumes;
184 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700185}
186
Kenny Root7b18a7b2010-03-15 13:13:41 -0700187char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700188 static const char* digits = "0123456789abcdef";
189
Kenny Root7b18a7b2010-03-15 13:13:41 -0700190 unsigned char sig[MD5_DIGEST_LENGTH];
191
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700192 if (buffer == NULL) {
193 SLOGE("Destination buffer is NULL");
194 errno = ESPIPE;
195 return NULL;
196 } else if (id == NULL) {
197 SLOGE("Source buffer is NULL");
198 errno = ESPIPE;
199 return NULL;
200 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800201 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700202 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800203 errno = ESPIPE;
204 return NULL;
205 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700206
207 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800208
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700209 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700210 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700211 *p++ = digits[sig[i] >> 4];
212 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800213 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700214 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800215
216 return buffer;
217}
218
219void VolumeManager::setDebug(bool enable) {
220 mDebug = enable;
221 VolumeCollection::iterator it;
222 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
223 (*it)->setDebug(enable);
224 }
225}
226
San Mehatf1b736b2009-10-10 17:22:08 -0700227int VolumeManager::start() {
228 return 0;
229}
230
231int VolumeManager::stop() {
232 return 0;
233}
234
235int VolumeManager::addVolume(Volume *v) {
236 mVolumes->push_back(v);
237 return 0;
238}
239
San Mehatfd7f5872009-10-12 11:32:47 -0700240void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
241 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700242
San Mehatfd7f5872009-10-12 11:32:47 -0700243 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700244 VolumeCollection::iterator it;
245 bool hit = false;
246 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700247 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800248#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700249 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800250#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700251 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700252 break;
253 }
254 }
255
256 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800257#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700258 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800259#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700260 }
261}
262
JP Abgrall40b64a62014-07-24 18:02:16 -0700263int VolumeManager::listVolumes(SocketClient *cli, bool broadcast) {
San Mehatf1b736b2009-10-10 17:22:08 -0700264 VolumeCollection::iterator i;
JP Abgrall40b64a62014-07-24 18:02:16 -0700265 char msg[256];
San Mehatf1b736b2009-10-10 17:22:08 -0700266
267 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
268 char *buffer;
269 asprintf(&buffer, "%s %s %d",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700270 (*i)->getLabel(), (*i)->getFuseMountpoint(),
San Mehatf1b736b2009-10-10 17:22:08 -0700271 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800272 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700273 free(buffer);
JP Abgrall40b64a62014-07-24 18:02:16 -0700274 if (broadcast) {
275 if((*i)->getUuid()) {
276 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
277 (*i)->getFuseMountpoint(), (*i)->getUuid());
278 mBroadcaster->sendBroadcast(ResponseCode::VolumeUuidChange,
279 msg, false);
280 }
281 if((*i)->getUserLabel()) {
282 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
283 (*i)->getFuseMountpoint(), (*i)->getUserLabel());
284 mBroadcaster->sendBroadcast(ResponseCode::VolumeUserLabelChange,
285 msg, false);
286 }
287 }
San Mehatf1b736b2009-10-10 17:22:08 -0700288 }
San Mehata2677e42009-12-13 10:40:18 -0800289 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700290 return 0;
291}
San Mehat49e2bce2009-10-12 16:29:01 -0700292
Ken Sumrall9caab762013-06-11 19:10:20 -0700293int VolumeManager::formatVolume(const char *label, bool wipe) {
San Mehata2677e42009-12-13 10:40:18 -0800294 Volume *v = lookupVolume(label);
295
296 if (!v) {
297 errno = ENOENT;
298 return -1;
299 }
300
Ken Sumrall3b170052011-07-11 15:38:57 -0700301 if (mVolManagerDisabled) {
302 errno = EBUSY;
303 return -1;
304 }
305
Ken Sumrall9caab762013-06-11 19:10:20 -0700306 return v->formatVol(wipe);
San Mehata2677e42009-12-13 10:40:18 -0800307}
308
Kenny Root508c0e12010-07-12 09:59:49 -0700309int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
310 char idHash[33];
311 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
312 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
313 return -1;
314 }
315
316 memset(mountPath, 0, mountPathLen);
rpcraigd1c226f2012-10-09 06:58:16 -0400317 int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
318 if ((written < 0) || (written >= mountPathLen)) {
319 errno = EINVAL;
320 return -1;
321 }
Kenny Root508c0e12010-07-12 09:59:49 -0700322
323 if (access(mountPath, F_OK)) {
324 errno = ENOENT;
325 return -1;
326 }
327
328 return 0;
329}
330
San Mehata19b2502010-01-06 10:33:53 -0800331int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700332 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700333
Nick Kralevich0de7c612014-01-27 14:58:06 -0800334 if (!isLegalAsecId(id)) {
335 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
336 errno = EINVAL;
337 return -1;
338 }
339
Kenny Root344ca102012-04-03 17:23:01 -0700340 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
341 SLOGE("Couldn't find ASEC %s", id);
342 return -1;
343 }
San Mehat88ac2c02010-03-23 11:15:58 -0700344
345 memset(buffer, 0, maxlen);
346 if (access(asecFileName, F_OK)) {
347 errno = ENOENT;
348 return -1;
349 }
San Mehata19b2502010-01-06 10:33:53 -0800350
rpcraigd1c226f2012-10-09 06:58:16 -0400351 int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
352 if ((written < 0) || (written >= maxlen)) {
353 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
354 errno = EINVAL;
355 return -1;
356 }
357
San Mehata19b2502010-01-06 10:33:53 -0800358 return 0;
359}
360
Dianne Hackborn736910c2011-06-27 13:37:07 -0700361int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
362 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700363
Nick Kralevich0de7c612014-01-27 14:58:06 -0800364 if (!isLegalAsecId(id)) {
365 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
366 errno = EINVAL;
367 return -1;
368 }
369
Kenny Root344ca102012-04-03 17:23:01 -0700370 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
371 SLOGE("Couldn't find ASEC %s", id);
372 return -1;
373 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700374
375 memset(buffer, 0, maxlen);
376 if (access(asecFileName, F_OK)) {
377 errno = ENOENT;
378 return -1;
379 }
380
rpcraigd1c226f2012-10-09 06:58:16 -0400381 int written = snprintf(buffer, maxlen, "%s", asecFileName);
382 if ((written < 0) || (written >= maxlen)) {
383 errno = EINVAL;
384 return -1;
385 }
386
Dianne Hackborn736910c2011-06-27 13:37:07 -0700387 return 0;
388}
389
Kenny Root344ca102012-04-03 17:23:01 -0700390int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
391 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800392 struct asec_superblock sb;
393 memset(&sb, 0, sizeof(sb));
394
Nick Kralevich0de7c612014-01-27 14:58:06 -0800395 if (!isLegalAsecId(id)) {
396 SLOGE("createAsec: Invalid asec id \"%s\"", id);
397 errno = EINVAL;
398 return -1;
399 }
400
Kenny Root344ca102012-04-03 17:23:01 -0700401 const bool wantFilesystem = strcmp(fstype, "none");
402 bool usingExt4 = false;
403 if (wantFilesystem) {
404 usingExt4 = !strcmp(fstype, "ext4");
405 if (usingExt4) {
406 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
407 } else if (strcmp(fstype, "fat")) {
408 SLOGE("Invalid filesystem type %s", fstype);
409 errno = EINVAL;
410 return -1;
411 }
412 }
413
San Mehatfcf24fe2010-03-03 12:37:32 -0800414 sb.magic = ASEC_SB_MAGIC;
415 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800416
San Mehatd31e3802010-02-18 08:37:45 -0800417 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700418 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800419 errno = EINVAL;
420 return -1;
421 }
422
San Mehata19b2502010-01-06 10:33:53 -0800423 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700424 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800425 errno = EADDRINUSE;
426 return -1;
427 }
428
429 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700430
431 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
432 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
433 asecFileName, strerror(errno));
434 errno = EADDRINUSE;
435 return -1;
436 }
437
438 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
439
rpcraigd1c226f2012-10-09 06:58:16 -0400440 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
441 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
442 errno = EINVAL;
443 return -1;
444 }
San Mehata19b2502010-01-06 10:33:53 -0800445
446 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700447 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700448 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800449 errno = EADDRINUSE;
450 return -1;
451 }
452
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700453 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700454 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700455 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700456 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700457 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800458
459 // Add +1 for our superblock which is at the end
460 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700461 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800462 return -1;
463 }
464
San Mehatd9a4e352010-03-12 13:32:47 -0800465 char idHash[33];
466 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700467 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800468 unlink(asecFileName);
469 return -1;
470 }
471
San Mehata19b2502010-01-06 10:33:53 -0800472 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800473 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700474 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800475 unlink(asecFileName);
476 return -1;
477 }
478
San Mehatb78a32c2010-01-10 13:02:12 -0800479 char dmDevice[255];
480 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800481
San Mehatb78a32c2010-01-10 13:02:12 -0800482 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800483 // XXX: This is all we support for now
484 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800485 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800486 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700487 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800488 Loop::destroyByDevice(loopDevice);
489 unlink(asecFileName);
490 return -1;
491 }
492 cleanupDm = true;
493 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800494 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800495 strcpy(dmDevice, loopDevice);
496 }
497
San Mehatfcf24fe2010-03-03 12:37:32 -0800498 /*
499 * Drop down the superblock at the end of the file
500 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700501 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800502 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800503 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800504 }
505 Loop::destroyByDevice(loopDevice);
506 unlink(asecFileName);
507 return -1;
508 }
509
Kenny Root344ca102012-04-03 17:23:01 -0700510 if (wantFilesystem) {
511 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400512 char mountPoint[255];
513
rpcraigd1c226f2012-10-09 06:58:16 -0400514 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
515 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
516 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
517 if (cleanupDm) {
518 Devmapper::destroy(idHash);
519 }
520 Loop::destroyByDevice(loopDevice);
521 unlink(asecFileName);
522 return -1;
523 }
rpcraiga54e13a2012-09-21 14:17:08 -0400524
Kenny Root344ca102012-04-03 17:23:01 -0700525 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700526 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700527 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700528 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800529 }
San Mehata19b2502010-01-06 10:33:53 -0800530
Kenny Root344ca102012-04-03 17:23:01 -0700531 if (formatStatus < 0) {
532 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800533 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800534 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800535 }
San Mehateb13a902010-01-07 12:12:50 -0800536 Loop::destroyByDevice(loopDevice);
537 unlink(asecFileName);
538 return -1;
539 }
Kenny Root344ca102012-04-03 17:23:01 -0700540
Kenny Root344ca102012-04-03 17:23:01 -0700541 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800542 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700543 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800544 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800545 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800546 }
547 Loop::destroyByDevice(loopDevice);
548 unlink(asecFileName);
549 return -1;
550 }
San Mehatb78a32c2010-01-10 13:02:12 -0800551 }
San Mehata1091cb2010-02-28 20:17:20 -0800552
Kenny Root344ca102012-04-03 17:23:01 -0700553 int mountStatus;
554 if (usingExt4) {
555 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
556 } else {
557 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
558 false);
559 }
560
561 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700562 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800563 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800564 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800565 }
566 Loop::destroyByDevice(loopDevice);
567 unlink(asecFileName);
568 return -1;
569 }
Kenny Root344ca102012-04-03 17:23:01 -0700570
571 if (usingExt4) {
572 int dirfd = open(mountPoint, O_DIRECTORY);
573 if (dirfd >= 0) {
574 if (fchown(dirfd, ownerUid, AID_SYSTEM)
575 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
576 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
577 }
578 close(dirfd);
579 }
580 }
San Mehata1091cb2010-02-28 20:17:20 -0800581 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700582 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800583 }
San Mehat88705162010-01-15 09:26:28 -0800584
Kenny Rootcbacf782010-09-24 15:11:48 -0700585 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800586 return 0;
587}
588
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700589int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
590 char asecFileName[255];
591 char mountPoint[255];
592 bool cleanupDm = false;
593
594 if (!isLegalAsecId(id)) {
595 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
596 errno = EINVAL;
597 return -1;
598 }
599
600 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
601 SLOGE("Couldn't find ASEC %s", id);
602 return -1;
603 }
604
605 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
606 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
607 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
608 return -1;
609 }
610
611 if (isMountpointMounted(mountPoint)) {
612 SLOGE("ASEC %s mounted. Unmount before resizing", id);
613 errno = EBUSY;
614 return -1;
615 }
616
617 struct asec_superblock sb;
618 int fd;
619 unsigned int oldNumSec = 0;
620
621 if ((fd = open(asecFileName, O_RDONLY)) < 0) {
622 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
623 return -1;
624 }
625
626 struct stat info;
627 if (fstat(fd, &info) < 0) {
628 SLOGE("Failed to get file size (%s)", strerror(errno));
629 close(fd);
630 return -1;
631 }
632
633 oldNumSec = info.st_size / 512;
634
635 unsigned numImgSectors;
636 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
637 numImgSectors = adjustSectorNumExt4(numSectors);
638 else
639 numImgSectors = adjustSectorNumFAT(numSectors);
640 /*
641 * add one block for the superblock
642 */
643 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
644 if (oldNumSec >= numImgSectors + 1) {
645 SLOGE("Only growing is currently supported.");
646 close(fd);
647 return -1;
648 }
649
650 /*
651 * Try to read superblock.
652 */
653 memset(&sb, 0, sizeof(struct asec_superblock));
654 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
655 SLOGE("lseek failed (%s)", strerror(errno));
656 close(fd);
657 return -1;
658 }
659 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
660 SLOGE("superblock read failed (%s)", strerror(errno));
661 close(fd);
662 return -1;
663 }
664 close(fd);
665
666 if (mDebug) {
667 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
668 }
669 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
670 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
671 errno = EMEDIUMTYPE;
672 return -1;
673 }
674
675 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
676 SLOGE("Only ext4 partitions are supported for resize");
677 errno = EINVAL;
678 return -1;
679 }
680
681 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
682 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
683 return -1;
684 }
685
686 /*
687 * Drop down a copy of the superblock at the end of the file
688 */
689 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
690 goto fail;
691
692 char idHash[33];
693 if (!asecHash(id, idHash, sizeof(idHash))) {
694 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
695 goto fail;
696 }
697
698 char loopDevice[255];
699 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
700 goto fail;
701
702 char dmDevice[255];
703
704 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
705 Loop::destroyByDevice(loopDevice);
706 goto fail;
707 }
708
709 /*
710 * Wait for the device mapper node to be created.
711 */
712 waitForDevMapper(dmDevice);
713
714 if (Ext4::resize(dmDevice, numImgSectors)) {
715 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
716 if (cleanupDm) {
717 Devmapper::destroy(idHash);
718 }
719 Loop::destroyByDevice(loopDevice);
720 goto fail;
721 }
722
723 return 0;
724fail:
725 Loop::resizeImageFile(asecFileName, oldNumSec);
726 return -1;
727}
728
San Mehata19b2502010-01-06 10:33:53 -0800729int VolumeManager::finalizeAsec(const char *id) {
730 char asecFileName[255];
731 char loopDevice[255];
732 char mountPoint[255];
733
Nick Kralevich0de7c612014-01-27 14:58:06 -0800734 if (!isLegalAsecId(id)) {
735 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
736 errno = EINVAL;
737 return -1;
738 }
739
Kenny Root344ca102012-04-03 17:23:01 -0700740 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
741 SLOGE("Couldn't find ASEC %s", id);
742 return -1;
743 }
San Mehata19b2502010-01-06 10:33:53 -0800744
San Mehatd9a4e352010-03-12 13:32:47 -0800745 char idHash[33];
746 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700747 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800748 return -1;
749 }
750
751 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700752 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800753 return -1;
754 }
755
Kenny Root344ca102012-04-03 17:23:01 -0700756 unsigned int nr_sec = 0;
757 struct asec_superblock sb;
758
759 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
760 return -1;
761 }
762
rpcraigd1c226f2012-10-09 06:58:16 -0400763 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
764 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
765 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
766 return -1;
767 }
Kenny Root344ca102012-04-03 17:23:01 -0700768
769 int result = 0;
770 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
771 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
772 } else {
773 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
774 }
775
776 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700777 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800778 return -1;
779 }
780
San Mehatd9a4e352010-03-12 13:32:47 -0800781 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700782 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800783 }
San Mehata19b2502010-01-06 10:33:53 -0800784 return 0;
785}
786
Kenny Root344ca102012-04-03 17:23:01 -0700787int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
788 char asecFileName[255];
789 char loopDevice[255];
790 char mountPoint[255];
791
792 if (gid < AID_APP) {
793 SLOGE("Group ID is not in application range");
794 return -1;
795 }
796
Nick Kralevich0de7c612014-01-27 14:58:06 -0800797 if (!isLegalAsecId(id)) {
798 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
799 errno = EINVAL;
800 return -1;
801 }
802
Kenny Root344ca102012-04-03 17:23:01 -0700803 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
804 SLOGE("Couldn't find ASEC %s", id);
805 return -1;
806 }
807
808 char idHash[33];
809 if (!asecHash(id, idHash, sizeof(idHash))) {
810 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
811 return -1;
812 }
813
814 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
815 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
816 return -1;
817 }
818
819 unsigned int nr_sec = 0;
820 struct asec_superblock sb;
821
822 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
823 return -1;
824 }
825
rpcraigd1c226f2012-10-09 06:58:16 -0400826 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
827 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
828 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
829 return -1;
830 }
Kenny Root344ca102012-04-03 17:23:01 -0700831
832 int result = 0;
833 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
834 return 0;
835 }
836
837 int ret = Ext4::doMount(loopDevice, mountPoint,
838 false /* read-only */,
839 true /* remount */,
840 false /* executable */);
841 if (ret) {
842 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
843 return -1;
844 }
845
846 char *paths[] = { mountPoint, NULL };
847
848 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
849 if (fts) {
850 // Traverse the entire hierarchy and chown to system UID.
851 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
852 // We don't care about the lost+found directory.
853 if (!strcmp(ftsent->fts_name, "lost+found")) {
854 continue;
855 }
856
857 /*
858 * There can only be one file marked as private right now.
859 * This should be more robust, but it satisfies the requirements
860 * we have for right now.
861 */
862 const bool privateFile = !strcmp(ftsent->fts_name, filename);
863
864 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
865 if (fd < 0) {
866 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
867 result = -1;
868 continue;
869 }
870
871 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
872
873 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700874 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700875 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700876 result |= fchmod(fd, privateFile ? 0640 : 0644);
877 }
Robert Craigb9e3ba52014-02-04 10:53:00 -0500878
Stephen Smalley5093e612014-02-12 09:43:08 -0500879 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -0500880 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
881 result |= -1;
882 }
883
Kenny Root344ca102012-04-03 17:23:01 -0700884 close(fd);
885 }
886 fts_close(fts);
887
888 // Finally make the directory readable by everyone.
889 int dirfd = open(mountPoint, O_DIRECTORY);
890 if (dirfd < 0 || fchmod(dirfd, 0755)) {
891 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
892 result |= -1;
893 }
894 close(dirfd);
895 } else {
896 result |= -1;
897 }
898
899 result |= Ext4::doMount(loopDevice, mountPoint,
900 true /* read-only */,
901 true /* remount */,
902 true /* execute */);
903
904 if (result) {
905 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
906 return -1;
907 }
908
909 if (mDebug) {
910 SLOGD("ASEC %s permissions fixed", id);
911 }
912 return 0;
913}
914
San Mehat048b0802010-01-23 08:17:06 -0800915int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700916 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800917 char *asecFilename2;
918 char mountPoint[255];
919
Kenny Root344ca102012-04-03 17:23:01 -0700920 const char *dir;
921
Nick Kralevich0de7c612014-01-27 14:58:06 -0800922 if (!isLegalAsecId(id1)) {
923 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
924 errno = EINVAL;
925 return -1;
926 }
927
928 if (!isLegalAsecId(id2)) {
929 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
930 errno = EINVAL;
931 return -1;
932 }
933
Kenny Root344ca102012-04-03 17:23:01 -0700934 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
935 SLOGE("Couldn't find ASEC %s", id1);
936 return -1;
937 }
938
939 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800940
rpcraigd1c226f2012-10-09 06:58:16 -0400941 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
942 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
943 SLOGE("Rename failed: couldn't construct mountpoint");
944 goto out_err;
945 }
946
San Mehat048b0802010-01-23 08:17:06 -0800947 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700948 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800949 errno = EBUSY;
950 goto out_err;
951 }
952
rpcraigd1c226f2012-10-09 06:58:16 -0400953 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
954 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
955 SLOGE("Rename failed: couldn't construct mountpoint2");
956 goto out_err;
957 }
958
San Mehat96956ed2010-02-24 08:42:51 -0800959 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700960 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800961 errno = EBUSY;
962 goto out_err;
963 }
964
San Mehat048b0802010-01-23 08:17:06 -0800965 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700966 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800967 errno = EADDRINUSE;
968 goto out_err;
969 }
970
971 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700972 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800973 goto out_err;
974 }
975
San Mehat048b0802010-01-23 08:17:06 -0800976 free(asecFilename2);
977 return 0;
978
979out_err:
San Mehat048b0802010-01-23 08:17:06 -0800980 free(asecFilename2);
981 return -1;
982}
983
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700984#define UNMOUNT_RETRIES 5
985#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800986int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800987 char asecFileName[255];
988 char mountPoint[255];
989
Nick Kralevich0de7c612014-01-27 14:58:06 -0800990 if (!isLegalAsecId(id)) {
991 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
992 errno = EINVAL;
993 return -1;
994 }
995
Kenny Root344ca102012-04-03 17:23:01 -0700996 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
997 SLOGE("Couldn't find ASEC %s", id);
998 return -1;
999 }
1000
rpcraigd1c226f2012-10-09 06:58:16 -04001001 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1002 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1003 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1004 return -1;
1005 }
San Mehata19b2502010-01-06 10:33:53 -08001006
San Mehatd9a4e352010-03-12 13:32:47 -08001007 char idHash[33];
1008 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001009 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001010 return -1;
1011 }
1012
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001013 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1014}
1015
Kenny Root508c0e12010-07-12 09:59:49 -07001016int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001017 char mountPoint[255];
1018
1019 char idHash[33];
1020 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1021 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1022 return -1;
1023 }
1024
rpcraigd1c226f2012-10-09 06:58:16 -04001025 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1026 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1027 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1028 return -1;
1029 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001030
1031 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1032}
1033
1034int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1035 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001036 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001037 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001038 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001039 return -1;
1040 }
San Mehat23969932010-01-09 07:08:06 -08001041
San Mehatb78a32c2010-01-10 13:02:12 -08001042 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001043 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001044 rc = umount(mountPoint);
1045 if (!rc) {
1046 break;
San Mehata19b2502010-01-06 10:33:53 -08001047 }
San Mehatb78a32c2010-01-10 13:02:12 -08001048 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001049 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001050 rc = 0;
1051 break;
San Mehata19b2502010-01-06 10:33:53 -08001052 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001053 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001054 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001055
San Mehat4ba89482010-02-18 09:00:18 -08001056 int action = 0; // default is to just complain
1057
1058 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001059 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -08001060 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001061 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -08001062 action = 1; // SIGHUP
1063 }
San Mehat8c940ef2010-02-13 14:19:53 -08001064
San Mehat586536c2010-02-16 17:12:00 -08001065 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001066 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001067 }
1068
1069 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001070 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001071 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001072 return -1;
1073 }
1074
San Mehat12f4b892010-02-24 11:43:22 -08001075 int retries = 10;
1076
1077 while(retries--) {
1078 if (!rmdir(mountPoint)) {
1079 break;
1080 }
1081
San Mehat97ac40e2010-03-24 10:24:19 -07001082 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001083 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001084 }
1085
1086 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001087 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001088 }
San Mehat88705162010-01-15 09:26:28 -08001089
San Mehatd9a4e352010-03-12 13:32:47 -08001090 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -07001091 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001092 }
1093
1094 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001095 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001096 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001097 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001098 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001099 }
San Mehat88705162010-01-15 09:26:28 -08001100
1101 AsecIdCollection::iterator it;
1102 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001103 ContainerData* cd = *it;
1104 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001105 free(*it);
1106 mActiveContainers->erase(it);
1107 break;
1108 }
1109 }
1110 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001111 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001112 }
San Mehatb78a32c2010-01-10 13:02:12 -08001113 return 0;
1114}
1115
San Mehat4ba89482010-02-18 09:00:18 -08001116int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001117 char asecFileName[255];
1118 char mountPoint[255];
1119
Nick Kralevich0de7c612014-01-27 14:58:06 -08001120 if (!isLegalAsecId(id)) {
1121 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1122 errno = EINVAL;
1123 return -1;
1124 }
1125
Kenny Root344ca102012-04-03 17:23:01 -07001126 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1127 SLOGE("Couldn't find ASEC %s", id);
1128 return -1;
1129 }
1130
rpcraigd1c226f2012-10-09 06:58:16 -04001131 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1132 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1133 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1134 return -1;
1135 }
San Mehatb78a32c2010-01-10 13:02:12 -08001136
San Mehat0586d542010-01-12 15:38:59 -08001137 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001138 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001139 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001140 }
San Mehat4ba89482010-02-18 09:00:18 -08001141 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001142 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001143 return -1;
1144 }
1145 }
San Mehata19b2502010-01-06 10:33:53 -08001146
San Mehat0586d542010-01-12 15:38:59 -08001147 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001148 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001149 return -1;
1150 }
San Mehata19b2502010-01-06 10:33:53 -08001151
San Mehatd9a4e352010-03-12 13:32:47 -08001152 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001153 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001154 }
San Mehata19b2502010-01-06 10:33:53 -08001155 return 0;
1156}
1157
Nick Kralevich0de7c612014-01-27 14:58:06 -08001158/*
1159 * Legal ASEC ids consist of alphanumeric characters, '-',
1160 * '_', or '.'. ".." is not allowed. The first or last character
1161 * of the ASEC id cannot be '.' (dot).
1162 */
1163bool VolumeManager::isLegalAsecId(const char *id) const {
1164 size_t i;
1165 size_t len = strlen(id);
1166
1167 if (len == 0) {
1168 return false;
1169 }
1170 if ((id[0] == '.') || (id[len - 1] == '.')) {
1171 return false;
1172 }
1173
1174 for (i = 0; i < len; i++) {
1175 if (id[i] == '.') {
1176 // i=0 is guaranteed never to have a dot. See above.
1177 if (id[i-1] == '.') return false;
1178 continue;
1179 }
1180 if (id[i] == '_' || id[i] == '-') continue;
1181 if (id[i] >= 'a' && id[i] <= 'z') continue;
1182 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1183 if (id[i] >= '0' && id[i] <= '9') continue;
1184 return false;
1185 }
1186
1187 return true;
1188}
1189
Kenny Root344ca102012-04-03 17:23:01 -07001190bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1191 int dirfd = open(dir, O_DIRECTORY);
1192 if (dirfd < 0) {
1193 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
1194 return -1;
1195 }
1196
1197 bool ret = false;
1198
1199 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
1200 ret = true;
1201 }
1202
1203 close(dirfd);
1204
1205 return ret;
1206}
1207
1208int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1209 const char **directory) const {
1210 int dirfd, fd;
1211 const int idLen = strlen(id);
1212 char *asecName;
1213
Nick Kralevich0de7c612014-01-27 14:58:06 -08001214 if (!isLegalAsecId(id)) {
1215 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1216 errno = EINVAL;
1217 return -1;
1218 }
1219
Kenny Root344ca102012-04-03 17:23:01 -07001220 if (asprintf(&asecName, "%s.asec", id) < 0) {
1221 SLOGE("Couldn't allocate string to write ASEC name");
1222 return -1;
1223 }
1224
1225 const char *dir;
1226 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
1227 dir = Volume::SEC_ASECDIR_INT;
1228 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
1229 dir = Volume::SEC_ASECDIR_EXT;
1230 } else {
1231 free(asecName);
1232 return -1;
1233 }
1234
1235 if (directory != NULL) {
1236 *directory = dir;
1237 }
1238
1239 if (asecPath != NULL) {
1240 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001241 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1242 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001243 free(asecName);
1244 return -1;
1245 }
1246 }
1247
1248 free(asecName);
1249 return 0;
1250}
1251
San Mehata19b2502010-01-06 10:33:53 -08001252int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
1253 char asecFileName[255];
1254 char mountPoint[255];
1255
Nick Kralevich0de7c612014-01-27 14:58:06 -08001256 if (!isLegalAsecId(id)) {
1257 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1258 errno = EINVAL;
1259 return -1;
1260 }
1261
Kenny Root344ca102012-04-03 17:23:01 -07001262 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1263 SLOGE("Couldn't find ASEC %s", id);
1264 return -1;
1265 }
1266
rpcraigd1c226f2012-10-09 06:58:16 -04001267 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1268 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001269 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001270 return -1;
1271 }
San Mehata19b2502010-01-06 10:33:53 -08001272
1273 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001274 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001275 errno = EBUSY;
1276 return -1;
1277 }
1278
San Mehatd9a4e352010-03-12 13:32:47 -08001279 char idHash[33];
1280 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001281 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001282 return -1;
1283 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001284
San Mehata19b2502010-01-06 10:33:53 -08001285 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001286 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1287 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001288
1289 char dmDevice[255];
1290 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -08001291 int fd;
1292 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001293 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001294
Kenny Root344ca102012-04-03 17:23:01 -07001295 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1296 return -1;
1297 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001298
San Mehatd9a4e352010-03-12 13:32:47 -08001299 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001300 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001301 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001302 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001303 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001304 Loop::destroyByDevice(loopDevice);
1305 errno = EMEDIUMTYPE;
1306 return -1;
1307 }
1308 nr_sec--; // We don't want the devmapping to extend onto our superblock
1309
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001310 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1311 Loop::destroyByDevice(loopDevice);
1312 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001313 }
1314
Kenny Root344ca102012-04-03 17:23:01 -07001315 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001316 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001317 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001318 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001319 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001320 }
1321 Loop::destroyByDevice(loopDevice);
1322 return -1;
1323 }
San Mehata19b2502010-01-06 10:33:53 -08001324 }
1325
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001326 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001327 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001328 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001329 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001330
Kenny Root344ca102012-04-03 17:23:01 -07001331 int result;
1332 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1333 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
1334 } else {
1335 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
1336 }
1337
1338 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001339 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001340 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001341 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001342 }
1343 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001344 return -1;
1345 }
1346
Kenny Rootcbacf782010-09-24 15:11:48 -07001347 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001348 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001349 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001350 }
San Mehata19b2502010-01-06 10:33:53 -08001351 return 0;
1352}
1353
Kenny Root93ecb382012-08-09 11:28:37 -07001354Volume* VolumeManager::getVolumeForFile(const char *fileName) {
1355 VolumeCollection::iterator i;
1356
1357 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001358 const char* mountPoint = (*i)->getFuseMountpoint();
Kenny Root93ecb382012-08-09 11:28:37 -07001359 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
1360 return *i;
1361 }
1362 }
1363
1364 return NULL;
1365}
1366
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001367/**
1368 * Mounts an image file <code>img</code>.
1369 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001370int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001371 char mountPoint[255];
1372
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001373 char idHash[33];
1374 if (!asecHash(img, idHash, sizeof(idHash))) {
1375 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1376 return -1;
1377 }
1378
rpcraigd1c226f2012-10-09 06:58:16 -04001379 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1380 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001381 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001382 return -1;
1383 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001384
1385 if (isMountpointMounted(mountPoint)) {
1386 SLOGE("Image %s already mounted", img);
1387 errno = EBUSY;
1388 return -1;
1389 }
1390
1391 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001392 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1393 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001394
1395 char dmDevice[255];
1396 bool cleanupDm = false;
1397 int fd;
1398 unsigned int nr_sec = 0;
1399
1400 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1401 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1402 Loop::destroyByDevice(loopDevice);
1403 return -1;
1404 }
1405
1406 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1407 SLOGE("Failed to get loop size (%s)", strerror(errno));
1408 Loop::destroyByDevice(loopDevice);
1409 close(fd);
1410 return -1;
1411 }
1412
1413 close(fd);
1414
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001415 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash , nr_sec, &cleanupDm, mDebug)) {
1416 Loop::destroyByDevice(loopDevice);
1417 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001418 }
1419
1420 if (mkdir(mountPoint, 0755)) {
1421 if (errno != EEXIST) {
1422 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1423 if (cleanupDm) {
1424 Devmapper::destroy(idHash);
1425 }
1426 Loop::destroyByDevice(loopDevice);
1427 return -1;
1428 }
1429 }
1430
Jeff Sharkey69479042012-09-25 16:14:57 -07001431 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001432 0227, false)) {
1433 SLOGE("Image mount failed (%s)", strerror(errno));
1434 if (cleanupDm) {
1435 Devmapper::destroy(idHash);
1436 }
1437 Loop::destroyByDevice(loopDevice);
1438 return -1;
1439 }
1440
Kenny Rootcbacf782010-09-24 15:11:48 -07001441 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001442 if (mDebug) {
1443 SLOGD("Image %s mounted", img);
1444 }
1445 return 0;
1446}
1447
San Mehat49e2bce2009-10-12 16:29:01 -07001448int VolumeManager::mountVolume(const char *label) {
1449 Volume *v = lookupVolume(label);
1450
1451 if (!v) {
1452 errno = ENOENT;
1453 return -1;
1454 }
1455
San Mehata2677e42009-12-13 10:40:18 -08001456 return v->mountVol();
1457}
1458
Kenny Root508c0e12010-07-12 09:59:49 -07001459int VolumeManager::listMountedObbs(SocketClient* cli) {
1460 char device[256];
1461 char mount_path[256];
1462 char rest[256];
1463 FILE *fp;
1464 char line[1024];
1465
1466 if (!(fp = fopen("/proc/mounts", "r"))) {
1467 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1468 return -1;
1469 }
1470
1471 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001472 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001473 char loopDir[loopDirLen + 2];
1474 strcpy(loopDir, Volume::LOOPDIR);
1475 loopDir[loopDirLen++] = '/';
1476 loopDir[loopDirLen] = '\0';
1477
1478 while(fgets(line, sizeof(line), fp)) {
1479 line[strlen(line)-1] = '\0';
1480
1481 /*
1482 * Should look like:
1483 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1484 */
1485 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1486
1487 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1488 int fd = open(device, O_RDONLY);
1489 if (fd >= 0) {
1490 struct loop_info64 li;
1491 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1492 cli->sendMsg(ResponseCode::AsecListResult,
1493 (const char*) li.lo_file_name, false);
1494 }
1495 close(fd);
1496 }
1497 }
1498 }
1499
1500 fclose(fp);
1501 return 0;
1502}
1503
San Mehateba65e92010-01-29 05:15:16 -08001504int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1505 Volume *v = lookupVolume(label);
1506
1507 if (!v) {
1508 errno = ENOENT;
1509 return -1;
1510 }
1511
1512 if (strcmp(method, "ums")) {
1513 errno = ENOSYS;
1514 return -1;
1515 }
1516
1517 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001518 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001519 } else {
1520 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001521 }
1522 return 0;
1523}
1524
San Mehata2677e42009-12-13 10:40:18 -08001525int VolumeManager::shareVolume(const char *label, const char *method) {
1526 Volume *v = lookupVolume(label);
1527
1528 if (!v) {
1529 errno = ENOENT;
1530 return -1;
1531 }
1532
1533 /*
1534 * Eventually, we'll want to support additional share back-ends,
1535 * some of which may work while the media is mounted. For now,
1536 * we just support UMS
1537 */
1538 if (strcmp(method, "ums")) {
1539 errno = ENOSYS;
1540 return -1;
1541 }
1542
1543 if (v->getState() == Volume::State_NoMedia) {
1544 errno = ENODEV;
1545 return -1;
1546 }
1547
San Mehat49e2bce2009-10-12 16:29:01 -07001548 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001549 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001550 errno = EBUSY;
1551 return -1;
1552 }
1553
Ken Sumrall3b170052011-07-11 15:38:57 -07001554 if (mVolManagerDisabled) {
1555 errno = EBUSY;
1556 return -1;
1557 }
1558
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001559 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001560 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1561 // This volume does not support raw disk access
1562 errno = EINVAL;
1563 return -1;
1564 }
1565
1566 int fd;
1567 char nodepath[255];
rpcraigd1c226f2012-10-09 06:58:16 -04001568 int written = snprintf(nodepath,
San Mehata2677e42009-12-13 10:40:18 -08001569 sizeof(nodepath), "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -08001570 major(d), minor(d));
San Mehata2677e42009-12-13 10:40:18 -08001571
rpcraigd1c226f2012-10-09 06:58:16 -04001572 if ((written < 0) || (size_t(written) >= sizeof(nodepath))) {
1573 SLOGE("shareVolume failed: couldn't construct nodepath");
1574 return -1;
1575 }
1576
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001577 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001578 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001579 return -1;
1580 }
1581
1582 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001583 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001584 close(fd);
1585 return -1;
1586 }
1587
1588 close(fd);
1589 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001590 if (mUmsSharingCount++ == 0) {
1591 FILE* fp;
1592 mSavedDirtyRatio = -1; // in case we fail
1593 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1594 char line[16];
1595 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1596 fprintf(fp, "%d\n", mUmsDirtyRatio);
1597 } else {
1598 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1599 }
1600 fclose(fp);
1601 } else {
1602 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1603 }
1604 }
San Mehata2677e42009-12-13 10:40:18 -08001605 return 0;
1606}
1607
1608int VolumeManager::unshareVolume(const char *label, const char *method) {
1609 Volume *v = lookupVolume(label);
1610
1611 if (!v) {
1612 errno = ENOENT;
1613 return -1;
1614 }
1615
1616 if (strcmp(method, "ums")) {
1617 errno = ENOSYS;
1618 return -1;
1619 }
1620
1621 if (v->getState() != Volume::State_Shared) {
1622 errno = EINVAL;
1623 return -1;
1624 }
1625
San Mehata2677e42009-12-13 10:40:18 -08001626 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001627 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001628 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001629 return -1;
1630 }
1631
1632 char ch = 0;
1633 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001634 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001635 close(fd);
1636 return -1;
1637 }
1638
1639 close(fd);
1640 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001641 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1642 FILE* fp;
1643 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1644 fprintf(fp, "%d\n", mSavedDirtyRatio);
1645 fclose(fp);
1646 } else {
1647 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1648 }
1649 mSavedDirtyRatio = -1;
1650 }
San Mehata2677e42009-12-13 10:40:18 -08001651 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001652}
1653
Ken Sumrall3b170052011-07-11 15:38:57 -07001654extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001655 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001656 vm->disableVolumeManager();
1657 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001658 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001659}
1660
1661extern "C" int vold_getNumDirectVolumes(void) {
1662 VolumeManager *vm = VolumeManager::Instance();
1663 return vm->getNumDirectVolumes();
1664}
1665
1666int VolumeManager::getNumDirectVolumes(void) {
1667 VolumeCollection::iterator i;
1668 int n=0;
1669
1670 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1671 if ((*i)->getShareDevice() != (dev_t)0) {
1672 n++;
1673 }
1674 }
1675 return n;
1676}
1677
1678extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1679 VolumeManager *vm = VolumeManager::Instance();
1680 return vm->getDirectVolumeList(vol_list);
1681}
1682
1683int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1684 VolumeCollection::iterator i;
1685 int n=0;
1686 dev_t d;
1687
1688 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1689 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1690 (*i)->getVolInfo(&vol_list[n]);
1691 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
Colin Cross346c5b22014-01-22 23:59:41 -08001692 "/dev/block/vold/%d:%d", major(d), minor(d));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001693 n++;
1694 }
1695 }
1696
1697 return 0;
1698}
1699
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001700int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001701 Volume *v = lookupVolume(label);
1702
1703 if (!v) {
1704 errno = ENOENT;
1705 return -1;
1706 }
1707
San Mehata2677e42009-12-13 10:40:18 -08001708 if (v->getState() == Volume::State_NoMedia) {
1709 errno = ENODEV;
1710 return -1;
1711 }
1712
San Mehat49e2bce2009-10-12 16:29:01 -07001713 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001714 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001715 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001716 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001717 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001718 }
1719
San Mehat1a06eda2010-04-15 12:58:50 -07001720 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001721
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001722 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001723}
1724
Ken Sumrall425524d2012-06-14 20:55:28 -07001725extern "C" int vold_unmountAllAsecs(void) {
1726 int rc;
1727
1728 VolumeManager *vm = VolumeManager::Instance();
1729 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1730 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1731 rc = -1;
1732 }
1733 return rc;
1734}
1735
1736#define ID_BUF_LEN 256
1737#define ASEC_SUFFIX ".asec"
1738#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1739int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1740 DIR *d = opendir(directory);
1741 int rc = 0;
1742
1743 if (!d) {
1744 SLOGE("Could not open asec dir %s", directory);
1745 return -1;
1746 }
1747
1748 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -07001749 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Ken Sumrall425524d2012-06-14 20:55:28 -07001750
1751 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1752 if (dent == NULL) {
1753 SLOGE("Failed to allocate memory for asec dir");
1754 return -1;
1755 }
1756
1757 struct dirent *result;
1758 while (!readdir_r(d, dent, &result) && result != NULL) {
1759 if (dent->d_name[0] == '.')
1760 continue;
1761 if (dent->d_type != DT_REG)
1762 continue;
1763 size_t name_len = strlen(dent->d_name);
1764 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1765 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1766 char id[ID_BUF_LEN];
1767 strlcpy(id, dent->d_name, name_len - 4);
1768 if (unmountAsec(id, true)) {
1769 /* Register the error, but try to unmount more asecs */
1770 rc = -1;
1771 }
1772 }
1773 }
1774 closedir(d);
1775
1776 free(dent);
1777
1778 return rc;
1779}
1780
San Mehata2677e42009-12-13 10:40:18 -08001781/*
1782 * Looks up a volume by it's label or mount-point
1783 */
San Mehat49e2bce2009-10-12 16:29:01 -07001784Volume *VolumeManager::lookupVolume(const char *label) {
1785 VolumeCollection::iterator i;
1786
1787 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001788 if (label[0] == '/') {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001789 if (!strcmp(label, (*i)->getFuseMountpoint()))
San Mehata2677e42009-12-13 10:40:18 -08001790 return (*i);
1791 } else {
1792 if (!strcmp(label, (*i)->getLabel()))
1793 return (*i);
1794 }
San Mehat49e2bce2009-10-12 16:29:01 -07001795 }
1796 return NULL;
1797}
San Mehata19b2502010-01-06 10:33:53 -08001798
1799bool VolumeManager::isMountpointMounted(const char *mp)
1800{
1801 char device[256];
1802 char mount_path[256];
1803 char rest[256];
1804 FILE *fp;
1805 char line[1024];
1806
1807 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001808 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001809 return false;
1810 }
1811
1812 while(fgets(line, sizeof(line), fp)) {
1813 line[strlen(line)-1] = '\0';
1814 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1815 if (!strcmp(mount_path, mp)) {
1816 fclose(fp);
1817 return true;
1818 }
San Mehata19b2502010-01-06 10:33:53 -08001819 }
1820
1821 fclose(fp);
1822 return false;
1823}
1824
San Mehat1a06eda2010-04-15 12:58:50 -07001825int VolumeManager::cleanupAsec(Volume *v, bool force) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001826 int rc = 0;
Kenny Root93ecb382012-08-09 11:28:37 -07001827
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001828 char asecFileName[255];
1829
1830 AsecIdCollection removeAsec;
1831 AsecIdCollection removeObb;
1832
Kenny Root93ecb382012-08-09 11:28:37 -07001833 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1834 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001835 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07001836
Kenny Rootcbacf782010-09-24 15:11:48 -07001837 if (cd->type == ASEC) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001838 if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) {
1839 SLOGE("Couldn't find ASEC %s; cleaning up", cd->id);
1840 removeAsec.push_back(cd);
1841 } else {
1842 SLOGD("Found ASEC at path %s", asecFileName);
1843 if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT,
1844 strlen(Volume::SEC_ASECDIR_EXT))) {
1845 removeAsec.push_back(cd);
1846 }
1847 }
Kenny Rootcbacf782010-09-24 15:11:48 -07001848 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07001849 if (v == getVolumeForFile(cd->id)) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001850 removeObb.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001851 }
1852 } else {
1853 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001854 }
1855 }
Kenny Root93ecb382012-08-09 11:28:37 -07001856
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001857 for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) {
Kenny Root93ecb382012-08-09 11:28:37 -07001858 ContainerData *cd = *it;
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001859 SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel());
1860 if (unmountAsec(cd->id, force)) {
1861 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1862 rc = -1;
1863 }
1864 }
1865
1866 for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) {
1867 ContainerData *cd = *it;
1868 SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel());
Kenny Root93ecb382012-08-09 11:28:37 -07001869 if (unmountObb(cd->id, force)) {
1870 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1871 rc = -1;
1872 }
1873 }
1874
1875 return rc;
San Mehat1a06eda2010-04-15 12:58:50 -07001876}
1877
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001878int VolumeManager::mkdirs(char* path) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001879 // Require that path lives under a volume we manage and is mounted
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001880 const char* emulated_source = getenv("EMULATED_STORAGE_SOURCE");
1881 const char* root = NULL;
Marco Nelissen5ab02e72013-10-15 15:22:28 -07001882 if (emulated_source && !strncmp(path, emulated_source, strlen(emulated_source))) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001883 root = emulated_source;
1884 } else {
1885 Volume* vol = getVolumeForFile(path);
Cylen Yao27cfee32014-05-02 19:23:42 +08001886 if (vol && vol->getState() == Volume::State_Mounted) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001887 root = vol->getMountpoint();
1888 }
1889 }
1890
1891 if (!root) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001892 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001893 return -EINVAL;
1894 }
1895
1896 /* fs_mkdirs() does symlink checking and relative path enforcement */
1897 return fs_mkdirs(path, 0700);
1898}