blob: 3a2b55936d475e9d2158d50f8c67ff327654e973 [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
San Mehatf1b736b2009-10-10 17:22:08 -0700263int VolumeManager::listVolumes(SocketClient *cli) {
264 VolumeCollection::iterator i;
265
266 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
267 char *buffer;
268 asprintf(&buffer, "%s %s %d",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700269 (*i)->getLabel(), (*i)->getFuseMountpoint(),
San Mehatf1b736b2009-10-10 17:22:08 -0700270 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800271 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700272 free(buffer);
273 }
San Mehata2677e42009-12-13 10:40:18 -0800274 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700275 return 0;
276}
San Mehat49e2bce2009-10-12 16:29:01 -0700277
Ken Sumrall9caab762013-06-11 19:10:20 -0700278int VolumeManager::formatVolume(const char *label, bool wipe) {
San Mehata2677e42009-12-13 10:40:18 -0800279 Volume *v = lookupVolume(label);
280
281 if (!v) {
282 errno = ENOENT;
283 return -1;
284 }
285
Ken Sumrall3b170052011-07-11 15:38:57 -0700286 if (mVolManagerDisabled) {
287 errno = EBUSY;
288 return -1;
289 }
290
Ken Sumrall9caab762013-06-11 19:10:20 -0700291 return v->formatVol(wipe);
San Mehata2677e42009-12-13 10:40:18 -0800292}
293
Kenny Root508c0e12010-07-12 09:59:49 -0700294int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
295 char idHash[33];
296 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
297 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
298 return -1;
299 }
300
301 memset(mountPath, 0, mountPathLen);
rpcraigd1c226f2012-10-09 06:58:16 -0400302 int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
303 if ((written < 0) || (written >= mountPathLen)) {
304 errno = EINVAL;
305 return -1;
306 }
Kenny Root508c0e12010-07-12 09:59:49 -0700307
308 if (access(mountPath, F_OK)) {
309 errno = ENOENT;
310 return -1;
311 }
312
313 return 0;
314}
315
San Mehata19b2502010-01-06 10:33:53 -0800316int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700317 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700318
Nick Kralevich0de7c612014-01-27 14:58:06 -0800319 if (!isLegalAsecId(id)) {
320 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
321 errno = EINVAL;
322 return -1;
323 }
324
Kenny Root344ca102012-04-03 17:23:01 -0700325 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
326 SLOGE("Couldn't find ASEC %s", id);
327 return -1;
328 }
San Mehat88ac2c02010-03-23 11:15:58 -0700329
330 memset(buffer, 0, maxlen);
331 if (access(asecFileName, F_OK)) {
332 errno = ENOENT;
333 return -1;
334 }
San Mehata19b2502010-01-06 10:33:53 -0800335
rpcraigd1c226f2012-10-09 06:58:16 -0400336 int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
337 if ((written < 0) || (written >= maxlen)) {
338 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
339 errno = EINVAL;
340 return -1;
341 }
342
San Mehata19b2502010-01-06 10:33:53 -0800343 return 0;
344}
345
Dianne Hackborn736910c2011-06-27 13:37:07 -0700346int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
347 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700348
Nick Kralevich0de7c612014-01-27 14:58:06 -0800349 if (!isLegalAsecId(id)) {
350 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
351 errno = EINVAL;
352 return -1;
353 }
354
Kenny Root344ca102012-04-03 17:23:01 -0700355 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
356 SLOGE("Couldn't find ASEC %s", id);
357 return -1;
358 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700359
360 memset(buffer, 0, maxlen);
361 if (access(asecFileName, F_OK)) {
362 errno = ENOENT;
363 return -1;
364 }
365
rpcraigd1c226f2012-10-09 06:58:16 -0400366 int written = snprintf(buffer, maxlen, "%s", asecFileName);
367 if ((written < 0) || (written >= maxlen)) {
368 errno = EINVAL;
369 return -1;
370 }
371
Dianne Hackborn736910c2011-06-27 13:37:07 -0700372 return 0;
373}
374
Kenny Root344ca102012-04-03 17:23:01 -0700375int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
376 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800377 struct asec_superblock sb;
378 memset(&sb, 0, sizeof(sb));
379
Nick Kralevich0de7c612014-01-27 14:58:06 -0800380 if (!isLegalAsecId(id)) {
381 SLOGE("createAsec: Invalid asec id \"%s\"", id);
382 errno = EINVAL;
383 return -1;
384 }
385
Kenny Root344ca102012-04-03 17:23:01 -0700386 const bool wantFilesystem = strcmp(fstype, "none");
387 bool usingExt4 = false;
388 if (wantFilesystem) {
389 usingExt4 = !strcmp(fstype, "ext4");
390 if (usingExt4) {
391 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
392 } else if (strcmp(fstype, "fat")) {
393 SLOGE("Invalid filesystem type %s", fstype);
394 errno = EINVAL;
395 return -1;
396 }
397 }
398
San Mehatfcf24fe2010-03-03 12:37:32 -0800399 sb.magic = ASEC_SB_MAGIC;
400 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800401
San Mehatd31e3802010-02-18 08:37:45 -0800402 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700403 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800404 errno = EINVAL;
405 return -1;
406 }
407
San Mehata19b2502010-01-06 10:33:53 -0800408 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700409 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800410 errno = EADDRINUSE;
411 return -1;
412 }
413
414 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700415
416 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
417 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
418 asecFileName, strerror(errno));
419 errno = EADDRINUSE;
420 return -1;
421 }
422
423 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
424
rpcraigd1c226f2012-10-09 06:58:16 -0400425 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
426 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
427 errno = EINVAL;
428 return -1;
429 }
San Mehata19b2502010-01-06 10:33:53 -0800430
431 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700432 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700433 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800434 errno = EADDRINUSE;
435 return -1;
436 }
437
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700438 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700439 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700440 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700441 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700442 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800443
444 // Add +1 for our superblock which is at the end
445 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700446 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800447 return -1;
448 }
449
San Mehatd9a4e352010-03-12 13:32:47 -0800450 char idHash[33];
451 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700452 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800453 unlink(asecFileName);
454 return -1;
455 }
456
San Mehata19b2502010-01-06 10:33:53 -0800457 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800458 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700459 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800460 unlink(asecFileName);
461 return -1;
462 }
463
San Mehatb78a32c2010-01-10 13:02:12 -0800464 char dmDevice[255];
465 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800466
San Mehatb78a32c2010-01-10 13:02:12 -0800467 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800468 // XXX: This is all we support for now
469 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800470 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800471 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700472 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800473 Loop::destroyByDevice(loopDevice);
474 unlink(asecFileName);
475 return -1;
476 }
477 cleanupDm = true;
478 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800479 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800480 strcpy(dmDevice, loopDevice);
481 }
482
San Mehatfcf24fe2010-03-03 12:37:32 -0800483 /*
484 * Drop down the superblock at the end of the file
485 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700486 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800487 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800488 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800489 }
490 Loop::destroyByDevice(loopDevice);
491 unlink(asecFileName);
492 return -1;
493 }
494
Kenny Root344ca102012-04-03 17:23:01 -0700495 if (wantFilesystem) {
496 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400497 char mountPoint[255];
498
rpcraigd1c226f2012-10-09 06:58:16 -0400499 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
500 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
501 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
502 if (cleanupDm) {
503 Devmapper::destroy(idHash);
504 }
505 Loop::destroyByDevice(loopDevice);
506 unlink(asecFileName);
507 return -1;
508 }
rpcraiga54e13a2012-09-21 14:17:08 -0400509
Kenny Root344ca102012-04-03 17:23:01 -0700510 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700511 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700512 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700513 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800514 }
San Mehata19b2502010-01-06 10:33:53 -0800515
Kenny Root344ca102012-04-03 17:23:01 -0700516 if (formatStatus < 0) {
517 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800518 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800519 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800520 }
San Mehateb13a902010-01-07 12:12:50 -0800521 Loop::destroyByDevice(loopDevice);
522 unlink(asecFileName);
523 return -1;
524 }
Kenny Root344ca102012-04-03 17:23:01 -0700525
Kenny Root344ca102012-04-03 17:23:01 -0700526 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800527 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700528 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800529 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800530 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800531 }
532 Loop::destroyByDevice(loopDevice);
533 unlink(asecFileName);
534 return -1;
535 }
San Mehatb78a32c2010-01-10 13:02:12 -0800536 }
San Mehata1091cb2010-02-28 20:17:20 -0800537
Kenny Root344ca102012-04-03 17:23:01 -0700538 int mountStatus;
539 if (usingExt4) {
540 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
541 } else {
542 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
543 false);
544 }
545
546 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700547 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800548 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800549 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800550 }
551 Loop::destroyByDevice(loopDevice);
552 unlink(asecFileName);
553 return -1;
554 }
Kenny Root344ca102012-04-03 17:23:01 -0700555
556 if (usingExt4) {
557 int dirfd = open(mountPoint, O_DIRECTORY);
558 if (dirfd >= 0) {
559 if (fchown(dirfd, ownerUid, AID_SYSTEM)
560 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
561 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
562 }
563 close(dirfd);
564 }
565 }
San Mehata1091cb2010-02-28 20:17:20 -0800566 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700567 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800568 }
San Mehat88705162010-01-15 09:26:28 -0800569
Kenny Rootcbacf782010-09-24 15:11:48 -0700570 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800571 return 0;
572}
573
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700574int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
575 char asecFileName[255];
576 char mountPoint[255];
577 bool cleanupDm = false;
578
579 if (!isLegalAsecId(id)) {
580 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
581 errno = EINVAL;
582 return -1;
583 }
584
585 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
586 SLOGE("Couldn't find ASEC %s", id);
587 return -1;
588 }
589
590 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
591 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
592 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
593 return -1;
594 }
595
596 if (isMountpointMounted(mountPoint)) {
597 SLOGE("ASEC %s mounted. Unmount before resizing", id);
598 errno = EBUSY;
599 return -1;
600 }
601
602 struct asec_superblock sb;
603 int fd;
604 unsigned int oldNumSec = 0;
605
606 if ((fd = open(asecFileName, O_RDONLY)) < 0) {
607 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
608 return -1;
609 }
610
611 struct stat info;
612 if (fstat(fd, &info) < 0) {
613 SLOGE("Failed to get file size (%s)", strerror(errno));
614 close(fd);
615 return -1;
616 }
617
618 oldNumSec = info.st_size / 512;
619
620 unsigned numImgSectors;
621 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
622 numImgSectors = adjustSectorNumExt4(numSectors);
623 else
624 numImgSectors = adjustSectorNumFAT(numSectors);
625 /*
626 * add one block for the superblock
627 */
628 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
629 if (oldNumSec >= numImgSectors + 1) {
630 SLOGE("Only growing is currently supported.");
631 close(fd);
632 return -1;
633 }
634
635 /*
636 * Try to read superblock.
637 */
638 memset(&sb, 0, sizeof(struct asec_superblock));
639 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
640 SLOGE("lseek failed (%s)", strerror(errno));
641 close(fd);
642 return -1;
643 }
644 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
645 SLOGE("superblock read failed (%s)", strerror(errno));
646 close(fd);
647 return -1;
648 }
649 close(fd);
650
651 if (mDebug) {
652 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
653 }
654 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
655 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
656 errno = EMEDIUMTYPE;
657 return -1;
658 }
659
660 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
661 SLOGE("Only ext4 partitions are supported for resize");
662 errno = EINVAL;
663 return -1;
664 }
665
666 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
667 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
668 return -1;
669 }
670
671 /*
672 * Drop down a copy of the superblock at the end of the file
673 */
674 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
675 goto fail;
676
677 char idHash[33];
678 if (!asecHash(id, idHash, sizeof(idHash))) {
679 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
680 goto fail;
681 }
682
683 char loopDevice[255];
684 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
685 goto fail;
686
687 char dmDevice[255];
688
689 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
690 Loop::destroyByDevice(loopDevice);
691 goto fail;
692 }
693
694 /*
695 * Wait for the device mapper node to be created.
696 */
697 waitForDevMapper(dmDevice);
698
699 if (Ext4::resize(dmDevice, numImgSectors)) {
700 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
701 if (cleanupDm) {
702 Devmapper::destroy(idHash);
703 }
704 Loop::destroyByDevice(loopDevice);
705 goto fail;
706 }
707
708 return 0;
709fail:
710 Loop::resizeImageFile(asecFileName, oldNumSec);
711 return -1;
712}
713
San Mehata19b2502010-01-06 10:33:53 -0800714int VolumeManager::finalizeAsec(const char *id) {
715 char asecFileName[255];
716 char loopDevice[255];
717 char mountPoint[255];
718
Nick Kralevich0de7c612014-01-27 14:58:06 -0800719 if (!isLegalAsecId(id)) {
720 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
721 errno = EINVAL;
722 return -1;
723 }
724
Kenny Root344ca102012-04-03 17:23:01 -0700725 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
726 SLOGE("Couldn't find ASEC %s", id);
727 return -1;
728 }
San Mehata19b2502010-01-06 10:33:53 -0800729
San Mehatd9a4e352010-03-12 13:32:47 -0800730 char idHash[33];
731 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700732 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800733 return -1;
734 }
735
736 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700737 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800738 return -1;
739 }
740
Kenny Root344ca102012-04-03 17:23:01 -0700741 unsigned int nr_sec = 0;
742 struct asec_superblock sb;
743
744 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
745 return -1;
746 }
747
rpcraigd1c226f2012-10-09 06:58:16 -0400748 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
749 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
750 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
751 return -1;
752 }
Kenny Root344ca102012-04-03 17:23:01 -0700753
754 int result = 0;
755 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
756 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
757 } else {
758 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
759 }
760
761 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700762 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800763 return -1;
764 }
765
San Mehatd9a4e352010-03-12 13:32:47 -0800766 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700767 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800768 }
San Mehata19b2502010-01-06 10:33:53 -0800769 return 0;
770}
771
Kenny Root344ca102012-04-03 17:23:01 -0700772int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
773 char asecFileName[255];
774 char loopDevice[255];
775 char mountPoint[255];
776
777 if (gid < AID_APP) {
778 SLOGE("Group ID is not in application range");
779 return -1;
780 }
781
Nick Kralevich0de7c612014-01-27 14:58:06 -0800782 if (!isLegalAsecId(id)) {
783 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
784 errno = EINVAL;
785 return -1;
786 }
787
Kenny Root344ca102012-04-03 17:23:01 -0700788 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
789 SLOGE("Couldn't find ASEC %s", id);
790 return -1;
791 }
792
793 char idHash[33];
794 if (!asecHash(id, idHash, sizeof(idHash))) {
795 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
796 return -1;
797 }
798
799 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
800 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
801 return -1;
802 }
803
804 unsigned int nr_sec = 0;
805 struct asec_superblock sb;
806
807 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
808 return -1;
809 }
810
rpcraigd1c226f2012-10-09 06:58:16 -0400811 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
812 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
813 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
814 return -1;
815 }
Kenny Root344ca102012-04-03 17:23:01 -0700816
817 int result = 0;
818 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
819 return 0;
820 }
821
822 int ret = Ext4::doMount(loopDevice, mountPoint,
823 false /* read-only */,
824 true /* remount */,
825 false /* executable */);
826 if (ret) {
827 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
828 return -1;
829 }
830
831 char *paths[] = { mountPoint, NULL };
832
833 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
834 if (fts) {
835 // Traverse the entire hierarchy and chown to system UID.
836 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
837 // We don't care about the lost+found directory.
838 if (!strcmp(ftsent->fts_name, "lost+found")) {
839 continue;
840 }
841
842 /*
843 * There can only be one file marked as private right now.
844 * This should be more robust, but it satisfies the requirements
845 * we have for right now.
846 */
847 const bool privateFile = !strcmp(ftsent->fts_name, filename);
848
849 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
850 if (fd < 0) {
851 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
852 result = -1;
853 continue;
854 }
855
856 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
857
858 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700859 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700860 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700861 result |= fchmod(fd, privateFile ? 0640 : 0644);
862 }
Robert Craigb9e3ba52014-02-04 10:53:00 -0500863
Stephen Smalley5093e612014-02-12 09:43:08 -0500864 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -0500865 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
866 result |= -1;
867 }
868
Kenny Root344ca102012-04-03 17:23:01 -0700869 close(fd);
870 }
871 fts_close(fts);
872
873 // Finally make the directory readable by everyone.
874 int dirfd = open(mountPoint, O_DIRECTORY);
875 if (dirfd < 0 || fchmod(dirfd, 0755)) {
876 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
877 result |= -1;
878 }
879 close(dirfd);
880 } else {
881 result |= -1;
882 }
883
884 result |= Ext4::doMount(loopDevice, mountPoint,
885 true /* read-only */,
886 true /* remount */,
887 true /* execute */);
888
889 if (result) {
890 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
891 return -1;
892 }
893
894 if (mDebug) {
895 SLOGD("ASEC %s permissions fixed", id);
896 }
897 return 0;
898}
899
San Mehat048b0802010-01-23 08:17:06 -0800900int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700901 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800902 char *asecFilename2;
903 char mountPoint[255];
904
Kenny Root344ca102012-04-03 17:23:01 -0700905 const char *dir;
906
Nick Kralevich0de7c612014-01-27 14:58:06 -0800907 if (!isLegalAsecId(id1)) {
908 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
909 errno = EINVAL;
910 return -1;
911 }
912
913 if (!isLegalAsecId(id2)) {
914 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
915 errno = EINVAL;
916 return -1;
917 }
918
Kenny Root344ca102012-04-03 17:23:01 -0700919 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
920 SLOGE("Couldn't find ASEC %s", id1);
921 return -1;
922 }
923
924 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800925
rpcraigd1c226f2012-10-09 06:58:16 -0400926 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
927 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
928 SLOGE("Rename failed: couldn't construct mountpoint");
929 goto out_err;
930 }
931
San Mehat048b0802010-01-23 08:17:06 -0800932 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700933 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800934 errno = EBUSY;
935 goto out_err;
936 }
937
rpcraigd1c226f2012-10-09 06:58:16 -0400938 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
939 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
940 SLOGE("Rename failed: couldn't construct mountpoint2");
941 goto out_err;
942 }
943
San Mehat96956ed2010-02-24 08:42:51 -0800944 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700945 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800946 errno = EBUSY;
947 goto out_err;
948 }
949
San Mehat048b0802010-01-23 08:17:06 -0800950 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700951 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800952 errno = EADDRINUSE;
953 goto out_err;
954 }
955
956 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700957 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800958 goto out_err;
959 }
960
San Mehat048b0802010-01-23 08:17:06 -0800961 free(asecFilename2);
962 return 0;
963
964out_err:
San Mehat048b0802010-01-23 08:17:06 -0800965 free(asecFilename2);
966 return -1;
967}
968
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700969#define UNMOUNT_RETRIES 5
970#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800971int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800972 char asecFileName[255];
973 char mountPoint[255];
974
Nick Kralevich0de7c612014-01-27 14:58:06 -0800975 if (!isLegalAsecId(id)) {
976 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
977 errno = EINVAL;
978 return -1;
979 }
980
Kenny Root344ca102012-04-03 17:23:01 -0700981 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
982 SLOGE("Couldn't find ASEC %s", id);
983 return -1;
984 }
985
rpcraigd1c226f2012-10-09 06:58:16 -0400986 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
987 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
988 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
989 return -1;
990 }
San Mehata19b2502010-01-06 10:33:53 -0800991
San Mehatd9a4e352010-03-12 13:32:47 -0800992 char idHash[33];
993 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700994 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800995 return -1;
996 }
997
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700998 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
999}
1000
Kenny Root508c0e12010-07-12 09:59:49 -07001001int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001002 char mountPoint[255];
1003
1004 char idHash[33];
1005 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1006 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1007 return -1;
1008 }
1009
rpcraigd1c226f2012-10-09 06:58:16 -04001010 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1011 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1012 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1013 return -1;
1014 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001015
1016 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1017}
1018
1019int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1020 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001021 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001022 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001023 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001024 return -1;
1025 }
San Mehat23969932010-01-09 07:08:06 -08001026
San Mehatb78a32c2010-01-10 13:02:12 -08001027 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001028 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001029 rc = umount(mountPoint);
1030 if (!rc) {
1031 break;
San Mehata19b2502010-01-06 10:33:53 -08001032 }
San Mehatb78a32c2010-01-10 13:02:12 -08001033 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001034 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001035 rc = 0;
1036 break;
San Mehata19b2502010-01-06 10:33:53 -08001037 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001038 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001039 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001040
San Mehat4ba89482010-02-18 09:00:18 -08001041 int action = 0; // default is to just complain
1042
1043 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001044 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -08001045 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001046 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -08001047 action = 1; // SIGHUP
1048 }
San Mehat8c940ef2010-02-13 14:19:53 -08001049
San Mehat586536c2010-02-16 17:12:00 -08001050 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001051 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001052 }
1053
1054 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001055 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001056 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001057 return -1;
1058 }
1059
San Mehat12f4b892010-02-24 11:43:22 -08001060 int retries = 10;
1061
1062 while(retries--) {
1063 if (!rmdir(mountPoint)) {
1064 break;
1065 }
1066
San Mehat97ac40e2010-03-24 10:24:19 -07001067 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001068 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001069 }
1070
1071 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001072 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001073 }
San Mehat88705162010-01-15 09:26:28 -08001074
San Mehatd9a4e352010-03-12 13:32:47 -08001075 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -07001076 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001077 }
1078
1079 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001080 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001081 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001082 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001083 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001084 }
San Mehat88705162010-01-15 09:26:28 -08001085
1086 AsecIdCollection::iterator it;
1087 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001088 ContainerData* cd = *it;
1089 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001090 free(*it);
1091 mActiveContainers->erase(it);
1092 break;
1093 }
1094 }
1095 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001096 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001097 }
San Mehatb78a32c2010-01-10 13:02:12 -08001098 return 0;
1099}
1100
San Mehat4ba89482010-02-18 09:00:18 -08001101int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001102 char asecFileName[255];
1103 char mountPoint[255];
1104
Nick Kralevich0de7c612014-01-27 14:58:06 -08001105 if (!isLegalAsecId(id)) {
1106 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1107 errno = EINVAL;
1108 return -1;
1109 }
1110
Kenny Root344ca102012-04-03 17:23:01 -07001111 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1112 SLOGE("Couldn't find ASEC %s", id);
1113 return -1;
1114 }
1115
rpcraigd1c226f2012-10-09 06:58:16 -04001116 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1117 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1118 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1119 return -1;
1120 }
San Mehatb78a32c2010-01-10 13:02:12 -08001121
San Mehat0586d542010-01-12 15:38:59 -08001122 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001123 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001124 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001125 }
San Mehat4ba89482010-02-18 09:00:18 -08001126 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001127 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001128 return -1;
1129 }
1130 }
San Mehata19b2502010-01-06 10:33:53 -08001131
San Mehat0586d542010-01-12 15:38:59 -08001132 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001133 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001134 return -1;
1135 }
San Mehata19b2502010-01-06 10:33:53 -08001136
San Mehatd9a4e352010-03-12 13:32:47 -08001137 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001138 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001139 }
San Mehata19b2502010-01-06 10:33:53 -08001140 return 0;
1141}
1142
Nick Kralevich0de7c612014-01-27 14:58:06 -08001143/*
1144 * Legal ASEC ids consist of alphanumeric characters, '-',
1145 * '_', or '.'. ".." is not allowed. The first or last character
1146 * of the ASEC id cannot be '.' (dot).
1147 */
1148bool VolumeManager::isLegalAsecId(const char *id) const {
1149 size_t i;
1150 size_t len = strlen(id);
1151
1152 if (len == 0) {
1153 return false;
1154 }
1155 if ((id[0] == '.') || (id[len - 1] == '.')) {
1156 return false;
1157 }
1158
1159 for (i = 0; i < len; i++) {
1160 if (id[i] == '.') {
1161 // i=0 is guaranteed never to have a dot. See above.
1162 if (id[i-1] == '.') return false;
1163 continue;
1164 }
1165 if (id[i] == '_' || id[i] == '-') continue;
1166 if (id[i] >= 'a' && id[i] <= 'z') continue;
1167 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1168 if (id[i] >= '0' && id[i] <= '9') continue;
1169 return false;
1170 }
1171
1172 return true;
1173}
1174
Kenny Root344ca102012-04-03 17:23:01 -07001175bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1176 int dirfd = open(dir, O_DIRECTORY);
1177 if (dirfd < 0) {
1178 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
1179 return -1;
1180 }
1181
1182 bool ret = false;
1183
1184 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
1185 ret = true;
1186 }
1187
1188 close(dirfd);
1189
1190 return ret;
1191}
1192
1193int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1194 const char **directory) const {
1195 int dirfd, fd;
1196 const int idLen = strlen(id);
1197 char *asecName;
1198
Nick Kralevich0de7c612014-01-27 14:58:06 -08001199 if (!isLegalAsecId(id)) {
1200 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1201 errno = EINVAL;
1202 return -1;
1203 }
1204
Kenny Root344ca102012-04-03 17:23:01 -07001205 if (asprintf(&asecName, "%s.asec", id) < 0) {
1206 SLOGE("Couldn't allocate string to write ASEC name");
1207 return -1;
1208 }
1209
1210 const char *dir;
1211 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
1212 dir = Volume::SEC_ASECDIR_INT;
1213 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
1214 dir = Volume::SEC_ASECDIR_EXT;
1215 } else {
1216 free(asecName);
1217 return -1;
1218 }
1219
1220 if (directory != NULL) {
1221 *directory = dir;
1222 }
1223
1224 if (asecPath != NULL) {
1225 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001226 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1227 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001228 free(asecName);
1229 return -1;
1230 }
1231 }
1232
1233 free(asecName);
1234 return 0;
1235}
1236
San Mehata19b2502010-01-06 10:33:53 -08001237int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
1238 char asecFileName[255];
1239 char mountPoint[255];
1240
Nick Kralevich0de7c612014-01-27 14:58:06 -08001241 if (!isLegalAsecId(id)) {
1242 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1243 errno = EINVAL;
1244 return -1;
1245 }
1246
Kenny Root344ca102012-04-03 17:23:01 -07001247 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1248 SLOGE("Couldn't find ASEC %s", id);
1249 return -1;
1250 }
1251
rpcraigd1c226f2012-10-09 06:58:16 -04001252 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1253 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001254 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001255 return -1;
1256 }
San Mehata19b2502010-01-06 10:33:53 -08001257
1258 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001259 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001260 errno = EBUSY;
1261 return -1;
1262 }
1263
San Mehatd9a4e352010-03-12 13:32:47 -08001264 char idHash[33];
1265 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001266 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001267 return -1;
1268 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001269
San Mehata19b2502010-01-06 10:33:53 -08001270 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001271 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1272 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001273
1274 char dmDevice[255];
1275 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -08001276 int fd;
1277 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001278 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001279
Kenny Root344ca102012-04-03 17:23:01 -07001280 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1281 return -1;
1282 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001283
San Mehatd9a4e352010-03-12 13:32:47 -08001284 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001285 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001286 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001287 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001288 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001289 Loop::destroyByDevice(loopDevice);
1290 errno = EMEDIUMTYPE;
1291 return -1;
1292 }
1293 nr_sec--; // We don't want the devmapping to extend onto our superblock
1294
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001295 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1296 Loop::destroyByDevice(loopDevice);
1297 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001298 }
1299
Kenny Root344ca102012-04-03 17:23:01 -07001300 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001301 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001302 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001303 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001304 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001305 }
1306 Loop::destroyByDevice(loopDevice);
1307 return -1;
1308 }
San Mehata19b2502010-01-06 10:33:53 -08001309 }
1310
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001311 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001312 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001313 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001314 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001315
Kenny Root344ca102012-04-03 17:23:01 -07001316 int result;
1317 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1318 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
1319 } else {
1320 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
1321 }
1322
1323 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001324 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001325 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001326 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001327 }
1328 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001329 return -1;
1330 }
1331
Kenny Rootcbacf782010-09-24 15:11:48 -07001332 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001333 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001334 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001335 }
San Mehata19b2502010-01-06 10:33:53 -08001336 return 0;
1337}
1338
Kenny Root93ecb382012-08-09 11:28:37 -07001339Volume* VolumeManager::getVolumeForFile(const char *fileName) {
1340 VolumeCollection::iterator i;
1341
1342 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001343 const char* mountPoint = (*i)->getFuseMountpoint();
Kenny Root93ecb382012-08-09 11:28:37 -07001344 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
1345 return *i;
1346 }
1347 }
1348
1349 return NULL;
1350}
1351
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001352/**
1353 * Mounts an image file <code>img</code>.
1354 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001355int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001356 char mountPoint[255];
1357
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001358 char idHash[33];
1359 if (!asecHash(img, idHash, sizeof(idHash))) {
1360 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1361 return -1;
1362 }
1363
rpcraigd1c226f2012-10-09 06:58:16 -04001364 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1365 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001366 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001367 return -1;
1368 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001369
1370 if (isMountpointMounted(mountPoint)) {
1371 SLOGE("Image %s already mounted", img);
1372 errno = EBUSY;
1373 return -1;
1374 }
1375
1376 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001377 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1378 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001379
1380 char dmDevice[255];
1381 bool cleanupDm = false;
1382 int fd;
1383 unsigned int nr_sec = 0;
1384
1385 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1386 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1387 Loop::destroyByDevice(loopDevice);
1388 return -1;
1389 }
1390
1391 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1392 SLOGE("Failed to get loop size (%s)", strerror(errno));
1393 Loop::destroyByDevice(loopDevice);
1394 close(fd);
1395 return -1;
1396 }
1397
1398 close(fd);
1399
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001400 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash , nr_sec, &cleanupDm, mDebug)) {
1401 Loop::destroyByDevice(loopDevice);
1402 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001403 }
1404
1405 if (mkdir(mountPoint, 0755)) {
1406 if (errno != EEXIST) {
1407 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1408 if (cleanupDm) {
1409 Devmapper::destroy(idHash);
1410 }
1411 Loop::destroyByDevice(loopDevice);
1412 return -1;
1413 }
1414 }
1415
Jeff Sharkey69479042012-09-25 16:14:57 -07001416 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001417 0227, false)) {
1418 SLOGE("Image mount failed (%s)", strerror(errno));
1419 if (cleanupDm) {
1420 Devmapper::destroy(idHash);
1421 }
1422 Loop::destroyByDevice(loopDevice);
1423 return -1;
1424 }
1425
Kenny Rootcbacf782010-09-24 15:11:48 -07001426 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001427 if (mDebug) {
1428 SLOGD("Image %s mounted", img);
1429 }
1430 return 0;
1431}
1432
San Mehat49e2bce2009-10-12 16:29:01 -07001433int VolumeManager::mountVolume(const char *label) {
1434 Volume *v = lookupVolume(label);
1435
1436 if (!v) {
1437 errno = ENOENT;
1438 return -1;
1439 }
1440
San Mehata2677e42009-12-13 10:40:18 -08001441 return v->mountVol();
1442}
1443
Kenny Root508c0e12010-07-12 09:59:49 -07001444int VolumeManager::listMountedObbs(SocketClient* cli) {
1445 char device[256];
1446 char mount_path[256];
1447 char rest[256];
1448 FILE *fp;
1449 char line[1024];
1450
1451 if (!(fp = fopen("/proc/mounts", "r"))) {
1452 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1453 return -1;
1454 }
1455
1456 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001457 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001458 char loopDir[loopDirLen + 2];
1459 strcpy(loopDir, Volume::LOOPDIR);
1460 loopDir[loopDirLen++] = '/';
1461 loopDir[loopDirLen] = '\0';
1462
1463 while(fgets(line, sizeof(line), fp)) {
1464 line[strlen(line)-1] = '\0';
1465
1466 /*
1467 * Should look like:
1468 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1469 */
1470 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1471
1472 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1473 int fd = open(device, O_RDONLY);
1474 if (fd >= 0) {
1475 struct loop_info64 li;
1476 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1477 cli->sendMsg(ResponseCode::AsecListResult,
1478 (const char*) li.lo_file_name, false);
1479 }
1480 close(fd);
1481 }
1482 }
1483 }
1484
1485 fclose(fp);
1486 return 0;
1487}
1488
San Mehateba65e92010-01-29 05:15:16 -08001489int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1490 Volume *v = lookupVolume(label);
1491
1492 if (!v) {
1493 errno = ENOENT;
1494 return -1;
1495 }
1496
1497 if (strcmp(method, "ums")) {
1498 errno = ENOSYS;
1499 return -1;
1500 }
1501
1502 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001503 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001504 } else {
1505 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001506 }
1507 return 0;
1508}
1509
San Mehata2677e42009-12-13 10:40:18 -08001510int VolumeManager::shareVolume(const char *label, const char *method) {
1511 Volume *v = lookupVolume(label);
1512
1513 if (!v) {
1514 errno = ENOENT;
1515 return -1;
1516 }
1517
1518 /*
1519 * Eventually, we'll want to support additional share back-ends,
1520 * some of which may work while the media is mounted. For now,
1521 * we just support UMS
1522 */
1523 if (strcmp(method, "ums")) {
1524 errno = ENOSYS;
1525 return -1;
1526 }
1527
1528 if (v->getState() == Volume::State_NoMedia) {
1529 errno = ENODEV;
1530 return -1;
1531 }
1532
San Mehat49e2bce2009-10-12 16:29:01 -07001533 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001534 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001535 errno = EBUSY;
1536 return -1;
1537 }
1538
Ken Sumrall3b170052011-07-11 15:38:57 -07001539 if (mVolManagerDisabled) {
1540 errno = EBUSY;
1541 return -1;
1542 }
1543
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001544 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001545 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1546 // This volume does not support raw disk access
1547 errno = EINVAL;
1548 return -1;
1549 }
1550
1551 int fd;
1552 char nodepath[255];
rpcraigd1c226f2012-10-09 06:58:16 -04001553 int written = snprintf(nodepath,
San Mehata2677e42009-12-13 10:40:18 -08001554 sizeof(nodepath), "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -08001555 major(d), minor(d));
San Mehata2677e42009-12-13 10:40:18 -08001556
rpcraigd1c226f2012-10-09 06:58:16 -04001557 if ((written < 0) || (size_t(written) >= sizeof(nodepath))) {
1558 SLOGE("shareVolume failed: couldn't construct nodepath");
1559 return -1;
1560 }
1561
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001562 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001563 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001564 return -1;
1565 }
1566
1567 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001568 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001569 close(fd);
1570 return -1;
1571 }
1572
1573 close(fd);
1574 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001575 if (mUmsSharingCount++ == 0) {
1576 FILE* fp;
1577 mSavedDirtyRatio = -1; // in case we fail
1578 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1579 char line[16];
1580 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1581 fprintf(fp, "%d\n", mUmsDirtyRatio);
1582 } else {
1583 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1584 }
1585 fclose(fp);
1586 } else {
1587 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1588 }
1589 }
San Mehata2677e42009-12-13 10:40:18 -08001590 return 0;
1591}
1592
1593int VolumeManager::unshareVolume(const char *label, const char *method) {
1594 Volume *v = lookupVolume(label);
1595
1596 if (!v) {
1597 errno = ENOENT;
1598 return -1;
1599 }
1600
1601 if (strcmp(method, "ums")) {
1602 errno = ENOSYS;
1603 return -1;
1604 }
1605
1606 if (v->getState() != Volume::State_Shared) {
1607 errno = EINVAL;
1608 return -1;
1609 }
1610
San Mehata2677e42009-12-13 10:40:18 -08001611 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001612 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001613 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001614 return -1;
1615 }
1616
1617 char ch = 0;
1618 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001619 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001620 close(fd);
1621 return -1;
1622 }
1623
1624 close(fd);
1625 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001626 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1627 FILE* fp;
1628 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1629 fprintf(fp, "%d\n", mSavedDirtyRatio);
1630 fclose(fp);
1631 } else {
1632 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1633 }
1634 mSavedDirtyRatio = -1;
1635 }
San Mehata2677e42009-12-13 10:40:18 -08001636 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001637}
1638
Ken Sumrall3b170052011-07-11 15:38:57 -07001639extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001640 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001641 vm->disableVolumeManager();
1642 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001643 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001644}
1645
1646extern "C" int vold_getNumDirectVolumes(void) {
1647 VolumeManager *vm = VolumeManager::Instance();
1648 return vm->getNumDirectVolumes();
1649}
1650
1651int VolumeManager::getNumDirectVolumes(void) {
1652 VolumeCollection::iterator i;
1653 int n=0;
1654
1655 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1656 if ((*i)->getShareDevice() != (dev_t)0) {
1657 n++;
1658 }
1659 }
1660 return n;
1661}
1662
1663extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1664 VolumeManager *vm = VolumeManager::Instance();
1665 return vm->getDirectVolumeList(vol_list);
1666}
1667
1668int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1669 VolumeCollection::iterator i;
1670 int n=0;
1671 dev_t d;
1672
1673 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1674 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1675 (*i)->getVolInfo(&vol_list[n]);
1676 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
Colin Cross346c5b22014-01-22 23:59:41 -08001677 "/dev/block/vold/%d:%d", major(d), minor(d));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001678 n++;
1679 }
1680 }
1681
1682 return 0;
1683}
1684
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001685int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001686 Volume *v = lookupVolume(label);
1687
1688 if (!v) {
1689 errno = ENOENT;
1690 return -1;
1691 }
1692
San Mehata2677e42009-12-13 10:40:18 -08001693 if (v->getState() == Volume::State_NoMedia) {
1694 errno = ENODEV;
1695 return -1;
1696 }
1697
San Mehat49e2bce2009-10-12 16:29:01 -07001698 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001699 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001700 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001701 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001702 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001703 }
1704
San Mehat1a06eda2010-04-15 12:58:50 -07001705 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001706
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001707 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001708}
1709
Ken Sumrall425524d2012-06-14 20:55:28 -07001710extern "C" int vold_unmountAllAsecs(void) {
1711 int rc;
1712
1713 VolumeManager *vm = VolumeManager::Instance();
1714 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1715 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1716 rc = -1;
1717 }
1718 return rc;
1719}
1720
1721#define ID_BUF_LEN 256
1722#define ASEC_SUFFIX ".asec"
1723#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1724int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1725 DIR *d = opendir(directory);
1726 int rc = 0;
1727
1728 if (!d) {
1729 SLOGE("Could not open asec dir %s", directory);
1730 return -1;
1731 }
1732
1733 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -07001734 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Ken Sumrall425524d2012-06-14 20:55:28 -07001735
1736 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1737 if (dent == NULL) {
1738 SLOGE("Failed to allocate memory for asec dir");
1739 return -1;
1740 }
1741
1742 struct dirent *result;
1743 while (!readdir_r(d, dent, &result) && result != NULL) {
1744 if (dent->d_name[0] == '.')
1745 continue;
1746 if (dent->d_type != DT_REG)
1747 continue;
1748 size_t name_len = strlen(dent->d_name);
1749 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1750 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1751 char id[ID_BUF_LEN];
1752 strlcpy(id, dent->d_name, name_len - 4);
1753 if (unmountAsec(id, true)) {
1754 /* Register the error, but try to unmount more asecs */
1755 rc = -1;
1756 }
1757 }
1758 }
1759 closedir(d);
1760
1761 free(dent);
1762
1763 return rc;
1764}
1765
San Mehata2677e42009-12-13 10:40:18 -08001766/*
1767 * Looks up a volume by it's label or mount-point
1768 */
San Mehat49e2bce2009-10-12 16:29:01 -07001769Volume *VolumeManager::lookupVolume(const char *label) {
1770 VolumeCollection::iterator i;
1771
1772 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001773 if (label[0] == '/') {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001774 if (!strcmp(label, (*i)->getFuseMountpoint()))
San Mehata2677e42009-12-13 10:40:18 -08001775 return (*i);
1776 } else {
1777 if (!strcmp(label, (*i)->getLabel()))
1778 return (*i);
1779 }
San Mehat49e2bce2009-10-12 16:29:01 -07001780 }
1781 return NULL;
1782}
San Mehata19b2502010-01-06 10:33:53 -08001783
1784bool VolumeManager::isMountpointMounted(const char *mp)
1785{
1786 char device[256];
1787 char mount_path[256];
1788 char rest[256];
1789 FILE *fp;
1790 char line[1024];
1791
1792 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001793 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001794 return false;
1795 }
1796
1797 while(fgets(line, sizeof(line), fp)) {
1798 line[strlen(line)-1] = '\0';
1799 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1800 if (!strcmp(mount_path, mp)) {
1801 fclose(fp);
1802 return true;
1803 }
San Mehata19b2502010-01-06 10:33:53 -08001804 }
1805
1806 fclose(fp);
1807 return false;
1808}
1809
San Mehat1a06eda2010-04-15 12:58:50 -07001810int VolumeManager::cleanupAsec(Volume *v, bool force) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001811 int rc = 0;
Kenny Root93ecb382012-08-09 11:28:37 -07001812
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001813 char asecFileName[255];
1814
1815 AsecIdCollection removeAsec;
1816 AsecIdCollection removeObb;
1817
Kenny Root93ecb382012-08-09 11:28:37 -07001818 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1819 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001820 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07001821
Kenny Rootcbacf782010-09-24 15:11:48 -07001822 if (cd->type == ASEC) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001823 if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) {
1824 SLOGE("Couldn't find ASEC %s; cleaning up", cd->id);
1825 removeAsec.push_back(cd);
1826 } else {
1827 SLOGD("Found ASEC at path %s", asecFileName);
1828 if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT,
1829 strlen(Volume::SEC_ASECDIR_EXT))) {
1830 removeAsec.push_back(cd);
1831 }
1832 }
Kenny Rootcbacf782010-09-24 15:11:48 -07001833 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07001834 if (v == getVolumeForFile(cd->id)) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001835 removeObb.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001836 }
1837 } else {
1838 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001839 }
1840 }
Kenny Root93ecb382012-08-09 11:28:37 -07001841
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001842 for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) {
Kenny Root93ecb382012-08-09 11:28:37 -07001843 ContainerData *cd = *it;
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001844 SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel());
1845 if (unmountAsec(cd->id, force)) {
1846 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1847 rc = -1;
1848 }
1849 }
1850
1851 for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) {
1852 ContainerData *cd = *it;
1853 SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel());
Kenny Root93ecb382012-08-09 11:28:37 -07001854 if (unmountObb(cd->id, force)) {
1855 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1856 rc = -1;
1857 }
1858 }
1859
1860 return rc;
San Mehat1a06eda2010-04-15 12:58:50 -07001861}
1862
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001863int VolumeManager::mkdirs(char* path) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001864 // Require that path lives under a volume we manage and is mounted
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001865 const char* emulated_source = getenv("EMULATED_STORAGE_SOURCE");
1866 const char* root = NULL;
Marco Nelissen5ab02e72013-10-15 15:22:28 -07001867 if (emulated_source && !strncmp(path, emulated_source, strlen(emulated_source))) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001868 root = emulated_source;
1869 } else {
1870 Volume* vol = getVolumeForFile(path);
Cylen Yao27cfee32014-05-02 19:23:42 +08001871 if (vol && vol->getState() == Volume::State_Mounted) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001872 root = vol->getMountpoint();
1873 }
1874 }
1875
1876 if (!root) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001877 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001878 return -EINVAL;
1879 }
1880
1881 /* fs_mkdirs() does symlink checking and relative path enforcement */
1882 return fs_mkdirs(path, 0700);
1883}