blob: f9b7b6a895110c31caef5a18c6660ef4fb2f34f6 [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>
Ken Sumrall425524d2012-06-14 20:55:28 -070027#include <dirent.h>
San Mehata19b2502010-01-06 10:33:53 -080028
San Mehata2677e42009-12-13 10:40:18 -080029#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070030
31#define LOG_TAG "Vold"
32
Kenny Root7b18a7b2010-03-15 13:13:41 -070033#include <openssl/md5.h>
34
Jeff Sharkey71ebe152013-09-17 17:24:38 -070035#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070036#include <cutils/log.h>
37
San Mehatfd7f5872009-10-12 11:32:47 -070038#include <sysutils/NetlinkEvent.h>
39
Kenny Root344ca102012-04-03 17:23:01 -070040#include <private/android_filesystem_config.h>
41
San Mehatf1b736b2009-10-10 17:22:08 -070042#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070043#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080044#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080045#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070046#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080047#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080048#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080049#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080050#include "Asec.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070051#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080052
Mike Lockwood97f2fc12011-06-07 10:51:38 -070053#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
54
San Mehatf1b736b2009-10-10 17:22:08 -070055VolumeManager *VolumeManager::sInstance = NULL;
56
57VolumeManager *VolumeManager::Instance() {
58 if (!sInstance)
59 sInstance = new VolumeManager();
60 return sInstance;
61}
62
63VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -080064 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -070065 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -080066 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -070067 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -040068 mUmsSharingCount = 0;
69 mSavedDirtyRatio = -1;
70 // set dirty ratio to 0 when UMS is active
71 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -070072 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -070073}
74
75VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -080076 delete mVolumes;
77 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -070078}
79
Kenny Root7b18a7b2010-03-15 13:13:41 -070080char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -070081 static const char* digits = "0123456789abcdef";
82
Kenny Root7b18a7b2010-03-15 13:13:41 -070083 unsigned char sig[MD5_DIGEST_LENGTH];
84
Kenny Rootacc9e7d2010-06-18 19:06:50 -070085 if (buffer == NULL) {
86 SLOGE("Destination buffer is NULL");
87 errno = ESPIPE;
88 return NULL;
89 } else if (id == NULL) {
90 SLOGE("Source buffer is NULL");
91 errno = ESPIPE;
92 return NULL;
93 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
94 SLOGE("Target hash buffer size < %d bytes (%d)",
95 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -080096 errno = ESPIPE;
97 return NULL;
98 }
Kenny Root7b18a7b2010-03-15 13:13:41 -070099
100 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800101
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700102 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700103 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700104 *p++ = digits[sig[i] >> 4];
105 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800106 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700107 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800108
109 return buffer;
110}
111
112void VolumeManager::setDebug(bool enable) {
113 mDebug = enable;
114 VolumeCollection::iterator it;
115 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
116 (*it)->setDebug(enable);
117 }
118}
119
San Mehatf1b736b2009-10-10 17:22:08 -0700120int VolumeManager::start() {
121 return 0;
122}
123
124int VolumeManager::stop() {
125 return 0;
126}
127
128int VolumeManager::addVolume(Volume *v) {
129 mVolumes->push_back(v);
130 return 0;
131}
132
San Mehatfd7f5872009-10-12 11:32:47 -0700133void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
134 const char *devpath = evt->findParam("DEVPATH");
San Mehatf1b736b2009-10-10 17:22:08 -0700135
San Mehatfd7f5872009-10-12 11:32:47 -0700136 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700137 VolumeCollection::iterator it;
138 bool hit = false;
139 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700140 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800141#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700142 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800143#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700144 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700145 break;
146 }
147 }
148
149 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800150#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700151 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800152#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700153 }
154}
155
San Mehatf1b736b2009-10-10 17:22:08 -0700156int VolumeManager::listVolumes(SocketClient *cli) {
157 VolumeCollection::iterator i;
158
159 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
160 char *buffer;
161 asprintf(&buffer, "%s %s %d",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700162 (*i)->getLabel(), (*i)->getFuseMountpoint(),
San Mehatf1b736b2009-10-10 17:22:08 -0700163 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800164 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700165 free(buffer);
166 }
San Mehata2677e42009-12-13 10:40:18 -0800167 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700168 return 0;
169}
San Mehat49e2bce2009-10-12 16:29:01 -0700170
Ken Sumrall9caab762013-06-11 19:10:20 -0700171int VolumeManager::formatVolume(const char *label, bool wipe) {
San Mehata2677e42009-12-13 10:40:18 -0800172 Volume *v = lookupVolume(label);
173
174 if (!v) {
175 errno = ENOENT;
176 return -1;
177 }
178
Ken Sumrall3b170052011-07-11 15:38:57 -0700179 if (mVolManagerDisabled) {
180 errno = EBUSY;
181 return -1;
182 }
183
Ken Sumrall9caab762013-06-11 19:10:20 -0700184 return v->formatVol(wipe);
San Mehata2677e42009-12-13 10:40:18 -0800185}
186
Kenny Root508c0e12010-07-12 09:59:49 -0700187int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
188 char idHash[33];
189 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
190 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
191 return -1;
192 }
193
194 memset(mountPath, 0, mountPathLen);
rpcraigd1c226f2012-10-09 06:58:16 -0400195 int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
196 if ((written < 0) || (written >= mountPathLen)) {
197 errno = EINVAL;
198 return -1;
199 }
Kenny Root508c0e12010-07-12 09:59:49 -0700200
201 if (access(mountPath, F_OK)) {
202 errno = ENOENT;
203 return -1;
204 }
205
206 return 0;
207}
208
San Mehata19b2502010-01-06 10:33:53 -0800209int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700210 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700211
Nick Kralevich66962602014-01-27 14:58:06 -0800212 if (!isLegalAsecId(id)) {
213 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
214 errno = EINVAL;
215 return -1;
216 }
217
Kenny Root344ca102012-04-03 17:23:01 -0700218 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
219 SLOGE("Couldn't find ASEC %s", id);
220 return -1;
221 }
San Mehat88ac2c02010-03-23 11:15:58 -0700222
223 memset(buffer, 0, maxlen);
224 if (access(asecFileName, F_OK)) {
225 errno = ENOENT;
226 return -1;
227 }
San Mehata19b2502010-01-06 10:33:53 -0800228
rpcraigd1c226f2012-10-09 06:58:16 -0400229 int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
230 if ((written < 0) || (written >= maxlen)) {
231 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
232 errno = EINVAL;
233 return -1;
234 }
235
San Mehata19b2502010-01-06 10:33:53 -0800236 return 0;
237}
238
Dianne Hackborn736910c2011-06-27 13:37:07 -0700239int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
240 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700241
Nick Kralevich66962602014-01-27 14:58:06 -0800242 if (!isLegalAsecId(id)) {
243 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
244 errno = EINVAL;
245 return -1;
246 }
247
Kenny Root344ca102012-04-03 17:23:01 -0700248 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
249 SLOGE("Couldn't find ASEC %s", id);
250 return -1;
251 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700252
253 memset(buffer, 0, maxlen);
254 if (access(asecFileName, F_OK)) {
255 errno = ENOENT;
256 return -1;
257 }
258
rpcraigd1c226f2012-10-09 06:58:16 -0400259 int written = snprintf(buffer, maxlen, "%s", asecFileName);
260 if ((written < 0) || (written >= maxlen)) {
261 errno = EINVAL;
262 return -1;
263 }
264
Dianne Hackborn736910c2011-06-27 13:37:07 -0700265 return 0;
266}
267
Kenny Root344ca102012-04-03 17:23:01 -0700268int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
269 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800270 struct asec_superblock sb;
271 memset(&sb, 0, sizeof(sb));
272
Nick Kralevich66962602014-01-27 14:58:06 -0800273 if (!isLegalAsecId(id)) {
274 SLOGE("createAsec: Invalid asec id \"%s\"", id);
275 errno = EINVAL;
276 return -1;
277 }
278
Kenny Root344ca102012-04-03 17:23:01 -0700279 const bool wantFilesystem = strcmp(fstype, "none");
280 bool usingExt4 = false;
281 if (wantFilesystem) {
282 usingExt4 = !strcmp(fstype, "ext4");
283 if (usingExt4) {
284 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
285 } else if (strcmp(fstype, "fat")) {
286 SLOGE("Invalid filesystem type %s", fstype);
287 errno = EINVAL;
288 return -1;
289 }
290 }
291
San Mehatfcf24fe2010-03-03 12:37:32 -0800292 sb.magic = ASEC_SB_MAGIC;
293 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800294
San Mehatd31e3802010-02-18 08:37:45 -0800295 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700296 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800297 errno = EINVAL;
298 return -1;
299 }
300
San Mehata19b2502010-01-06 10:33:53 -0800301 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700302 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800303 errno = EADDRINUSE;
304 return -1;
305 }
306
307 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700308
309 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
310 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
311 asecFileName, strerror(errno));
312 errno = EADDRINUSE;
313 return -1;
314 }
315
316 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
317
rpcraigd1c226f2012-10-09 06:58:16 -0400318 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
319 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
320 errno = EINVAL;
321 return -1;
322 }
San Mehata19b2502010-01-06 10:33:53 -0800323
324 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700325 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700326 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800327 errno = EADDRINUSE;
328 return -1;
329 }
330
San Mehatfcf24fe2010-03-03 12:37:32 -0800331 /*
332 * Add some headroom
333 */
334 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
335 unsigned numImgSectors = numSectors + fatSize + 2;
336
337 if (numImgSectors % 63) {
338 numImgSectors += (63 - (numImgSectors % 63));
339 }
340
341 // Add +1 for our superblock which is at the end
342 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700343 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800344 return -1;
345 }
346
San Mehatd9a4e352010-03-12 13:32:47 -0800347 char idHash[33];
348 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700349 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800350 unlink(asecFileName);
351 return -1;
352 }
353
San Mehata19b2502010-01-06 10:33:53 -0800354 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800355 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700356 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800357 unlink(asecFileName);
358 return -1;
359 }
360
San Mehatb78a32c2010-01-10 13:02:12 -0800361 char dmDevice[255];
362 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800363
San Mehatb78a32c2010-01-10 13:02:12 -0800364 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800365 // XXX: This is all we support for now
366 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800367 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800368 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700369 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800370 Loop::destroyByDevice(loopDevice);
371 unlink(asecFileName);
372 return -1;
373 }
374 cleanupDm = true;
375 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800376 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800377 strcpy(dmDevice, loopDevice);
378 }
379
San Mehatfcf24fe2010-03-03 12:37:32 -0800380 /*
381 * Drop down the superblock at the end of the file
382 */
383
384 int sbfd = open(loopDevice, O_RDWR);
385 if (sbfd < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700386 SLOGE("Failed to open new DM device for superblock write (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800387 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800388 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800389 }
390 Loop::destroyByDevice(loopDevice);
391 unlink(asecFileName);
392 return -1;
393 }
394
395 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
396 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700397 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800398 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800399 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800400 }
401 Loop::destroyByDevice(loopDevice);
402 unlink(asecFileName);
403 return -1;
404 }
405
406 if (write(sbfd, &sb, sizeof(sb)) != sizeof(sb)) {
407 close(sbfd);
San Mehat97ac40e2010-03-24 10:24:19 -0700408 SLOGE("Failed to write superblock (%s)", strerror(errno));
San Mehatfcf24fe2010-03-03 12:37:32 -0800409 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800410 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800411 }
412 Loop::destroyByDevice(loopDevice);
413 unlink(asecFileName);
414 return -1;
415 }
416 close(sbfd);
417
Kenny Root344ca102012-04-03 17:23:01 -0700418 if (wantFilesystem) {
419 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400420 char mountPoint[255];
421
rpcraigd1c226f2012-10-09 06:58:16 -0400422 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
423 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
424 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
425 if (cleanupDm) {
426 Devmapper::destroy(idHash);
427 }
428 Loop::destroyByDevice(loopDevice);
429 unlink(asecFileName);
430 return -1;
431 }
rpcraiga54e13a2012-09-21 14:17:08 -0400432
Kenny Root344ca102012-04-03 17:23:01 -0700433 if (usingExt4) {
rpcraiga54e13a2012-09-21 14:17:08 -0400434 formatStatus = Ext4::format(dmDevice, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700435 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700436 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800437 }
San Mehata19b2502010-01-06 10:33:53 -0800438
Kenny Root344ca102012-04-03 17:23:01 -0700439 if (formatStatus < 0) {
440 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800441 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800442 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800443 }
San Mehateb13a902010-01-07 12:12:50 -0800444 Loop::destroyByDevice(loopDevice);
445 unlink(asecFileName);
446 return -1;
447 }
Kenny Root344ca102012-04-03 17:23:01 -0700448
Kenny Root344ca102012-04-03 17:23:01 -0700449 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800450 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700451 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800452 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800453 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800454 }
455 Loop::destroyByDevice(loopDevice);
456 unlink(asecFileName);
457 return -1;
458 }
San Mehatb78a32c2010-01-10 13:02:12 -0800459 }
San Mehata1091cb2010-02-28 20:17:20 -0800460
Kenny Root344ca102012-04-03 17:23:01 -0700461 int mountStatus;
462 if (usingExt4) {
463 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
464 } else {
465 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
466 false);
467 }
468
469 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700470 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800471 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800472 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800473 }
474 Loop::destroyByDevice(loopDevice);
475 unlink(asecFileName);
476 return -1;
477 }
Kenny Root344ca102012-04-03 17:23:01 -0700478
479 if (usingExt4) {
480 int dirfd = open(mountPoint, O_DIRECTORY);
481 if (dirfd >= 0) {
482 if (fchown(dirfd, ownerUid, AID_SYSTEM)
483 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
484 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
485 }
486 close(dirfd);
487 }
488 }
San Mehata1091cb2010-02-28 20:17:20 -0800489 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700490 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800491 }
San Mehat88705162010-01-15 09:26:28 -0800492
Kenny Rootcbacf782010-09-24 15:11:48 -0700493 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800494 return 0;
495}
496
497int VolumeManager::finalizeAsec(const char *id) {
498 char asecFileName[255];
499 char loopDevice[255];
500 char mountPoint[255];
501
Nick Kralevich66962602014-01-27 14:58:06 -0800502 if (!isLegalAsecId(id)) {
503 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
504 errno = EINVAL;
505 return -1;
506 }
507
Kenny Root344ca102012-04-03 17:23:01 -0700508 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
509 SLOGE("Couldn't find ASEC %s", id);
510 return -1;
511 }
San Mehata19b2502010-01-06 10:33:53 -0800512
San Mehatd9a4e352010-03-12 13:32:47 -0800513 char idHash[33];
514 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700515 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800516 return -1;
517 }
518
519 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700520 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800521 return -1;
522 }
523
Kenny Root344ca102012-04-03 17:23:01 -0700524 unsigned int nr_sec = 0;
525 struct asec_superblock sb;
526
527 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
528 return -1;
529 }
530
rpcraigd1c226f2012-10-09 06:58:16 -0400531 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
532 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
533 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
534 return -1;
535 }
Kenny Root344ca102012-04-03 17:23:01 -0700536
537 int result = 0;
538 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
539 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
540 } else {
541 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
542 }
543
544 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700545 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800546 return -1;
547 }
548
San Mehatd9a4e352010-03-12 13:32:47 -0800549 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700550 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800551 }
San Mehata19b2502010-01-06 10:33:53 -0800552 return 0;
553}
554
Kenny Root344ca102012-04-03 17:23:01 -0700555int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
556 char asecFileName[255];
557 char loopDevice[255];
558 char mountPoint[255];
559
560 if (gid < AID_APP) {
561 SLOGE("Group ID is not in application range");
562 return -1;
563 }
564
Nick Kralevich66962602014-01-27 14:58:06 -0800565 if (!isLegalAsecId(id)) {
566 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
567 errno = EINVAL;
568 return -1;
569 }
570
Kenny Root344ca102012-04-03 17:23:01 -0700571 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
572 SLOGE("Couldn't find ASEC %s", id);
573 return -1;
574 }
575
576 char idHash[33];
577 if (!asecHash(id, idHash, sizeof(idHash))) {
578 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
579 return -1;
580 }
581
582 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
583 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
584 return -1;
585 }
586
587 unsigned int nr_sec = 0;
588 struct asec_superblock sb;
589
590 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
591 return -1;
592 }
593
rpcraigd1c226f2012-10-09 06:58:16 -0400594 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
595 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
596 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
597 return -1;
598 }
Kenny Root344ca102012-04-03 17:23:01 -0700599
600 int result = 0;
601 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
602 return 0;
603 }
604
605 int ret = Ext4::doMount(loopDevice, mountPoint,
606 false /* read-only */,
607 true /* remount */,
608 false /* executable */);
609 if (ret) {
610 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
611 return -1;
612 }
613
614 char *paths[] = { mountPoint, NULL };
615
616 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
617 if (fts) {
618 // Traverse the entire hierarchy and chown to system UID.
619 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
620 // We don't care about the lost+found directory.
621 if (!strcmp(ftsent->fts_name, "lost+found")) {
622 continue;
623 }
624
625 /*
626 * There can only be one file marked as private right now.
627 * This should be more robust, but it satisfies the requirements
628 * we have for right now.
629 */
630 const bool privateFile = !strcmp(ftsent->fts_name, filename);
631
632 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
633 if (fd < 0) {
634 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
635 result = -1;
636 continue;
637 }
638
639 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
640
641 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700642 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700643 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700644 result |= fchmod(fd, privateFile ? 0640 : 0644);
645 }
646 close(fd);
647 }
648 fts_close(fts);
649
650 // Finally make the directory readable by everyone.
651 int dirfd = open(mountPoint, O_DIRECTORY);
652 if (dirfd < 0 || fchmod(dirfd, 0755)) {
653 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
654 result |= -1;
655 }
656 close(dirfd);
657 } else {
658 result |= -1;
659 }
660
661 result |= Ext4::doMount(loopDevice, mountPoint,
662 true /* read-only */,
663 true /* remount */,
664 true /* execute */);
665
666 if (result) {
667 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
668 return -1;
669 }
670
671 if (mDebug) {
672 SLOGD("ASEC %s permissions fixed", id);
673 }
674 return 0;
675}
676
San Mehat048b0802010-01-23 08:17:06 -0800677int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700678 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800679 char *asecFilename2;
680 char mountPoint[255];
681
Kenny Root344ca102012-04-03 17:23:01 -0700682 const char *dir;
683
Nick Kralevich66962602014-01-27 14:58:06 -0800684 if (!isLegalAsecId(id1)) {
685 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
686 errno = EINVAL;
687 return -1;
688 }
689
690 if (!isLegalAsecId(id2)) {
691 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
692 errno = EINVAL;
693 return -1;
694 }
695
Kenny Root344ca102012-04-03 17:23:01 -0700696 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
697 SLOGE("Couldn't find ASEC %s", id1);
698 return -1;
699 }
700
701 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800702
rpcraigd1c226f2012-10-09 06:58:16 -0400703 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
704 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
705 SLOGE("Rename failed: couldn't construct mountpoint");
706 goto out_err;
707 }
708
San Mehat048b0802010-01-23 08:17:06 -0800709 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700710 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800711 errno = EBUSY;
712 goto out_err;
713 }
714
rpcraigd1c226f2012-10-09 06:58:16 -0400715 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
716 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
717 SLOGE("Rename failed: couldn't construct mountpoint2");
718 goto out_err;
719 }
720
San Mehat96956ed2010-02-24 08:42:51 -0800721 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700722 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800723 errno = EBUSY;
724 goto out_err;
725 }
726
San Mehat048b0802010-01-23 08:17:06 -0800727 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700728 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800729 errno = EADDRINUSE;
730 goto out_err;
731 }
732
733 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700734 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800735 goto out_err;
736 }
737
San Mehat048b0802010-01-23 08:17:06 -0800738 free(asecFilename2);
739 return 0;
740
741out_err:
San Mehat048b0802010-01-23 08:17:06 -0800742 free(asecFilename2);
743 return -1;
744}
745
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700746#define UNMOUNT_RETRIES 5
747#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800748int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800749 char asecFileName[255];
750 char mountPoint[255];
751
Nick Kralevich66962602014-01-27 14:58:06 -0800752 if (!isLegalAsecId(id)) {
753 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
754 errno = EINVAL;
755 return -1;
756 }
757
Kenny Root344ca102012-04-03 17:23:01 -0700758 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
759 SLOGE("Couldn't find ASEC %s", id);
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 unmount failed for %s: couldn't construct mountpoint", id);
766 return -1;
767 }
San Mehata19b2502010-01-06 10:33:53 -0800768
San Mehatd9a4e352010-03-12 13:32:47 -0800769 char idHash[33];
770 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700771 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800772 return -1;
773 }
774
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700775 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
776}
777
Kenny Root508c0e12010-07-12 09:59:49 -0700778int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700779 char mountPoint[255];
780
781 char idHash[33];
782 if (!asecHash(fileName, idHash, sizeof(idHash))) {
783 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
784 return -1;
785 }
786
rpcraigd1c226f2012-10-09 06:58:16 -0400787 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
788 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
789 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
790 return -1;
791 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700792
793 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
794}
795
796int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
797 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -0800798 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700799 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -0700800 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -0800801 return -1;
802 }
San Mehat23969932010-01-09 07:08:06 -0800803
San Mehatb78a32c2010-01-10 13:02:12 -0800804 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700805 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -0800806 rc = umount(mountPoint);
807 if (!rc) {
808 break;
San Mehata19b2502010-01-06 10:33:53 -0800809 }
San Mehatb78a32c2010-01-10 13:02:12 -0800810 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700811 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -0800812 rc = 0;
813 break;
San Mehata19b2502010-01-06 10:33:53 -0800814 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700815 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -0800816 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800817
San Mehat4ba89482010-02-18 09:00:18 -0800818 int action = 0; // default is to just complain
819
820 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700821 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -0800822 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700823 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -0800824 action = 1; // SIGHUP
825 }
San Mehat8c940ef2010-02-13 14:19:53 -0800826
San Mehat586536c2010-02-16 17:12:00 -0800827 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700828 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -0800829 }
830
831 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -0800832 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -0700833 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800834 return -1;
835 }
836
San Mehat12f4b892010-02-24 11:43:22 -0800837 int retries = 10;
838
839 while(retries--) {
840 if (!rmdir(mountPoint)) {
841 break;
842 }
843
San Mehat97ac40e2010-03-24 10:24:19 -0700844 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700845 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -0800846 }
847
848 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -0700849 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -0800850 }
San Mehat88705162010-01-15 09:26:28 -0800851
San Mehatd9a4e352010-03-12 13:32:47 -0800852 if (Devmapper::destroy(idHash) && errno != ENXIO) {
San Mehat97ac40e2010-03-24 10:24:19 -0700853 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800854 }
855
856 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800857 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -0800858 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -0800859 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700860 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800861 }
San Mehat88705162010-01-15 09:26:28 -0800862
863 AsecIdCollection::iterator it;
864 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -0700865 ContainerData* cd = *it;
866 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -0800867 free(*it);
868 mActiveContainers->erase(it);
869 break;
870 }
871 }
872 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -0700873 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -0800874 }
San Mehatb78a32c2010-01-10 13:02:12 -0800875 return 0;
876}
877
San Mehat4ba89482010-02-18 09:00:18 -0800878int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -0800879 char asecFileName[255];
880 char mountPoint[255];
881
Nick Kralevich66962602014-01-27 14:58:06 -0800882 if (!isLegalAsecId(id)) {
883 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
884 errno = EINVAL;
885 return -1;
886 }
887
Kenny Root344ca102012-04-03 17:23:01 -0700888 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
889 SLOGE("Couldn't find ASEC %s", id);
890 return -1;
891 }
892
rpcraigd1c226f2012-10-09 06:58:16 -0400893 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
894 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
895 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
896 return -1;
897 }
San Mehatb78a32c2010-01-10 13:02:12 -0800898
San Mehat0586d542010-01-12 15:38:59 -0800899 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -0800900 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700901 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -0800902 }
San Mehat4ba89482010-02-18 09:00:18 -0800903 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700904 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800905 return -1;
906 }
907 }
San Mehata19b2502010-01-06 10:33:53 -0800908
San Mehat0586d542010-01-12 15:38:59 -0800909 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700910 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -0800911 return -1;
912 }
San Mehata19b2502010-01-06 10:33:53 -0800913
San Mehatd9a4e352010-03-12 13:32:47 -0800914 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700915 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800916 }
San Mehata19b2502010-01-06 10:33:53 -0800917 return 0;
918}
919
Nick Kralevich66962602014-01-27 14:58:06 -0800920/*
921 * Legal ASEC ids consist of alphanumeric characters, '-',
922 * '_', or '.'. ".." is not allowed. The first or last character
923 * of the ASEC id cannot be '.' (dot).
924 */
925bool VolumeManager::isLegalAsecId(const char *id) const {
926 size_t i;
927 size_t len = strlen(id);
928
929 if (len == 0) {
930 return false;
931 }
932 if ((id[0] == '.') || (id[len - 1] == '.')) {
933 return false;
934 }
935
936 for (i = 0; i < len; i++) {
937 if (id[i] == '.') {
938 // i=0 is guaranteed never to have a dot. See above.
939 if (id[i-1] == '.') return false;
940 continue;
941 }
942 if (id[i] == '_' || id[i] == '-') continue;
943 if (id[i] >= 'a' && id[i] <= 'z') continue;
944 if (id[i] >= 'A' && id[i] <= 'Z') continue;
945 if (id[i] >= '0' && id[i] <= '9') continue;
946 return false;
947 }
948
949 return true;
950}
951
Kenny Root344ca102012-04-03 17:23:01 -0700952bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
953 int dirfd = open(dir, O_DIRECTORY);
954 if (dirfd < 0) {
955 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
956 return -1;
957 }
958
959 bool ret = false;
960
961 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
962 ret = true;
963 }
964
965 close(dirfd);
966
967 return ret;
968}
969
970int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
971 const char **directory) const {
972 int dirfd, fd;
973 const int idLen = strlen(id);
974 char *asecName;
975
Nick Kralevich66962602014-01-27 14:58:06 -0800976 if (!isLegalAsecId(id)) {
977 SLOGE("findAsec: Invalid asec id \"%s\"", id);
978 errno = EINVAL;
979 return -1;
980 }
981
Kenny Root344ca102012-04-03 17:23:01 -0700982 if (asprintf(&asecName, "%s.asec", id) < 0) {
983 SLOGE("Couldn't allocate string to write ASEC name");
984 return -1;
985 }
986
987 const char *dir;
988 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
989 dir = Volume::SEC_ASECDIR_INT;
990 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
991 dir = Volume::SEC_ASECDIR_EXT;
992 } else {
993 free(asecName);
994 return -1;
995 }
996
997 if (directory != NULL) {
998 *directory = dir;
999 }
1000
1001 if (asecPath != NULL) {
1002 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001003 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1004 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001005 free(asecName);
1006 return -1;
1007 }
1008 }
1009
1010 free(asecName);
1011 return 0;
1012}
1013
San Mehata19b2502010-01-06 10:33:53 -08001014int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {
1015 char asecFileName[255];
1016 char mountPoint[255];
1017
Nick Kralevich66962602014-01-27 14:58:06 -08001018 if (!isLegalAsecId(id)) {
1019 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1020 errno = EINVAL;
1021 return -1;
1022 }
1023
Kenny Root344ca102012-04-03 17:23:01 -07001024 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1025 SLOGE("Couldn't find ASEC %s", id);
1026 return -1;
1027 }
1028
rpcraigd1c226f2012-10-09 06:58:16 -04001029 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1030 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1031 SLOGE("ASEC mount failed: couldn't construct mountpoint", id);
1032 return -1;
1033 }
San Mehata19b2502010-01-06 10:33:53 -08001034
1035 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001036 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001037 errno = EBUSY;
1038 return -1;
1039 }
1040
San Mehatd9a4e352010-03-12 13:32:47 -08001041 char idHash[33];
1042 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001043 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001044 return -1;
1045 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001046
San Mehata19b2502010-01-06 10:33:53 -08001047 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001048 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1049 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001050 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001051 return -1;
1052 }
San Mehatd9a4e352010-03-12 13:32:47 -08001053 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001054 SLOGD("New loop device created at %s", loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001055 }
San Mehatb78a32c2010-01-10 13:02:12 -08001056 } else {
San Mehatd9a4e352010-03-12 13:32:47 -08001057 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001058 SLOGD("Found active loopback for %s at %s", asecFileName, loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001059 }
San Mehatb78a32c2010-01-10 13:02:12 -08001060 }
1061
1062 char dmDevice[255];
1063 bool cleanupDm = false;
San Mehatfcf24fe2010-03-03 12:37:32 -08001064 int fd;
1065 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001066 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001067
Kenny Root344ca102012-04-03 17:23:01 -07001068 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1069 return -1;
1070 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001071
San Mehatd9a4e352010-03-12 13:32:47 -08001072 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001073 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001074 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001075 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001076 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001077 Loop::destroyByDevice(loopDevice);
1078 errno = EMEDIUMTYPE;
1079 return -1;
1080 }
1081 nr_sec--; // We don't want the devmapping to extend onto our superblock
1082
San Mehatb78a32c2010-01-10 13:02:12 -08001083 if (strcmp(key, "none")) {
San Mehatd9a4e352010-03-12 13:32:47 -08001084 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
1085 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
San Mehatb78a32c2010-01-10 13:02:12 -08001086 dmDevice, sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001087 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001088 Loop::destroyByDevice(loopDevice);
1089 return -1;
1090 }
San Mehatd9a4e352010-03-12 13:32:47 -08001091 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001092 SLOGD("New devmapper instance created at %s", dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001093 }
San Mehatb78a32c2010-01-10 13:02:12 -08001094 } else {
San Mehatd9a4e352010-03-12 13:32:47 -08001095 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001096 SLOGD("Found active devmapper for %s at %s", asecFileName, dmDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001097 }
San Mehatb78a32c2010-01-10 13:02:12 -08001098 }
1099 cleanupDm = true;
1100 } else {
1101 strcpy(dmDevice, loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001102 }
1103
Kenny Root344ca102012-04-03 17:23:01 -07001104 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001105 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001106 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001107 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001108 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001109 }
1110 Loop::destroyByDevice(loopDevice);
1111 return -1;
1112 }
San Mehata19b2502010-01-06 10:33:53 -08001113 }
1114
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001115 /*
1116 * The device mapper node needs to be created. Sometimes it takes a
1117 * while. Wait for up to 1 second. We could also inspect incoming uevents,
1118 * but that would take more effort.
1119 */
1120 int tries = 25;
1121 while (tries--) {
1122 if (!access(dmDevice, F_OK) || errno != ENOENT) {
1123 break;
1124 }
1125 usleep(40 * 1000);
1126 }
1127
Kenny Root344ca102012-04-03 17:23:01 -07001128 int result;
1129 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
1130 result = Ext4::doMount(dmDevice, mountPoint, true, false, true);
1131 } else {
1132 result = Fat::doMount(dmDevice, mountPoint, true, false, true, ownerUid, 0, 0222, false);
1133 }
1134
1135 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001136 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001137 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001138 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001139 }
1140 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001141 return -1;
1142 }
1143
Kenny Rootcbacf782010-09-24 15:11:48 -07001144 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001145 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001146 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001147 }
San Mehata19b2502010-01-06 10:33:53 -08001148 return 0;
1149}
1150
Kenny Root93ecb382012-08-09 11:28:37 -07001151Volume* VolumeManager::getVolumeForFile(const char *fileName) {
1152 VolumeCollection::iterator i;
1153
1154 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001155 const char* mountPoint = (*i)->getFuseMountpoint();
Kenny Root93ecb382012-08-09 11:28:37 -07001156 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
1157 return *i;
1158 }
1159 }
1160
1161 return NULL;
1162}
1163
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001164/**
1165 * Mounts an image file <code>img</code>.
1166 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001167int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001168 char mountPoint[255];
1169
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001170 char idHash[33];
1171 if (!asecHash(img, idHash, sizeof(idHash))) {
1172 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1173 return -1;
1174 }
1175
rpcraigd1c226f2012-10-09 06:58:16 -04001176 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1177 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1178 SLOGE("OBB mount failed: couldn't construct mountpoint", img);
1179 return -1;
1180 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001181
1182 if (isMountpointMounted(mountPoint)) {
1183 SLOGE("Image %s already mounted", img);
1184 errno = EBUSY;
1185 return -1;
1186 }
1187
1188 char loopDevice[255];
1189 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1190 if (Loop::create(idHash, img, loopDevice, sizeof(loopDevice))) {
1191 SLOGE("Image loop device creation failed (%s)", strerror(errno));
1192 return -1;
1193 }
1194 if (mDebug) {
1195 SLOGD("New loop device created at %s", loopDevice);
1196 }
1197 } else {
1198 if (mDebug) {
1199 SLOGD("Found active loopback for %s at %s", img, loopDevice);
1200 }
1201 }
1202
1203 char dmDevice[255];
1204 bool cleanupDm = false;
1205 int fd;
1206 unsigned int nr_sec = 0;
1207
1208 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1209 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1210 Loop::destroyByDevice(loopDevice);
1211 return -1;
1212 }
1213
1214 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1215 SLOGE("Failed to get loop size (%s)", strerror(errno));
1216 Loop::destroyByDevice(loopDevice);
1217 close(fd);
1218 return -1;
1219 }
1220
1221 close(fd);
1222
1223 if (strcmp(key, "none")) {
1224 if (Devmapper::lookupActive(idHash, dmDevice, sizeof(dmDevice))) {
1225 if (Devmapper::create(idHash, loopDevice, key, nr_sec,
1226 dmDevice, sizeof(dmDevice))) {
1227 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
1228 Loop::destroyByDevice(loopDevice);
1229 return -1;
1230 }
1231 if (mDebug) {
1232 SLOGD("New devmapper instance created at %s", dmDevice);
1233 }
1234 } else {
1235 if (mDebug) {
1236 SLOGD("Found active devmapper for %s at %s", img, dmDevice);
1237 }
1238 }
1239 cleanupDm = true;
1240 } else {
1241 strcpy(dmDevice, loopDevice);
1242 }
1243
1244 if (mkdir(mountPoint, 0755)) {
1245 if (errno != EEXIST) {
1246 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1247 if (cleanupDm) {
1248 Devmapper::destroy(idHash);
1249 }
1250 Loop::destroyByDevice(loopDevice);
1251 return -1;
1252 }
1253 }
1254
Jeff Sharkey69479042012-09-25 16:14:57 -07001255 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001256 0227, false)) {
1257 SLOGE("Image mount failed (%s)", strerror(errno));
1258 if (cleanupDm) {
1259 Devmapper::destroy(idHash);
1260 }
1261 Loop::destroyByDevice(loopDevice);
1262 return -1;
1263 }
1264
Kenny Rootcbacf782010-09-24 15:11:48 -07001265 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001266 if (mDebug) {
1267 SLOGD("Image %s mounted", img);
1268 }
1269 return 0;
1270}
1271
San Mehat49e2bce2009-10-12 16:29:01 -07001272int VolumeManager::mountVolume(const char *label) {
1273 Volume *v = lookupVolume(label);
1274
1275 if (!v) {
1276 errno = ENOENT;
1277 return -1;
1278 }
1279
San Mehata2677e42009-12-13 10:40:18 -08001280 return v->mountVol();
1281}
1282
Kenny Root508c0e12010-07-12 09:59:49 -07001283int VolumeManager::listMountedObbs(SocketClient* cli) {
1284 char device[256];
1285 char mount_path[256];
1286 char rest[256];
1287 FILE *fp;
1288 char line[1024];
1289
1290 if (!(fp = fopen("/proc/mounts", "r"))) {
1291 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1292 return -1;
1293 }
1294
1295 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001296 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001297 char loopDir[loopDirLen + 2];
1298 strcpy(loopDir, Volume::LOOPDIR);
1299 loopDir[loopDirLen++] = '/';
1300 loopDir[loopDirLen] = '\0';
1301
1302 while(fgets(line, sizeof(line), fp)) {
1303 line[strlen(line)-1] = '\0';
1304
1305 /*
1306 * Should look like:
1307 * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
1308 */
1309 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1310
1311 if (!strncmp(mount_path, loopDir, loopDirLen)) {
1312 int fd = open(device, O_RDONLY);
1313 if (fd >= 0) {
1314 struct loop_info64 li;
1315 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1316 cli->sendMsg(ResponseCode::AsecListResult,
1317 (const char*) li.lo_file_name, false);
1318 }
1319 close(fd);
1320 }
1321 }
1322 }
1323
1324 fclose(fp);
1325 return 0;
1326}
1327
San Mehateba65e92010-01-29 05:15:16 -08001328int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1329 Volume *v = lookupVolume(label);
1330
1331 if (!v) {
1332 errno = ENOENT;
1333 return -1;
1334 }
1335
1336 if (strcmp(method, "ums")) {
1337 errno = ENOSYS;
1338 return -1;
1339 }
1340
1341 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001342 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001343 } else {
1344 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001345 }
1346 return 0;
1347}
1348
San Mehata2677e42009-12-13 10:40:18 -08001349int VolumeManager::shareVolume(const char *label, const char *method) {
1350 Volume *v = lookupVolume(label);
1351
1352 if (!v) {
1353 errno = ENOENT;
1354 return -1;
1355 }
1356
1357 /*
1358 * Eventually, we'll want to support additional share back-ends,
1359 * some of which may work while the media is mounted. For now,
1360 * we just support UMS
1361 */
1362 if (strcmp(method, "ums")) {
1363 errno = ENOSYS;
1364 return -1;
1365 }
1366
1367 if (v->getState() == Volume::State_NoMedia) {
1368 errno = ENODEV;
1369 return -1;
1370 }
1371
San Mehat49e2bce2009-10-12 16:29:01 -07001372 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001373 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001374 errno = EBUSY;
1375 return -1;
1376 }
1377
Ken Sumrall3b170052011-07-11 15:38:57 -07001378 if (mVolManagerDisabled) {
1379 errno = EBUSY;
1380 return -1;
1381 }
1382
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001383 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001384 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1385 // This volume does not support raw disk access
1386 errno = EINVAL;
1387 return -1;
1388 }
1389
1390 int fd;
1391 char nodepath[255];
rpcraigd1c226f2012-10-09 06:58:16 -04001392 int written = snprintf(nodepath,
San Mehata2677e42009-12-13 10:40:18 -08001393 sizeof(nodepath), "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -08001394 major(d), minor(d));
San Mehata2677e42009-12-13 10:40:18 -08001395
rpcraigd1c226f2012-10-09 06:58:16 -04001396 if ((written < 0) || (size_t(written) >= sizeof(nodepath))) {
1397 SLOGE("shareVolume failed: couldn't construct nodepath");
1398 return -1;
1399 }
1400
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001401 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001402 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001403 return -1;
1404 }
1405
1406 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001407 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001408 close(fd);
1409 return -1;
1410 }
1411
1412 close(fd);
1413 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001414 if (mUmsSharingCount++ == 0) {
1415 FILE* fp;
1416 mSavedDirtyRatio = -1; // in case we fail
1417 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1418 char line[16];
1419 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1420 fprintf(fp, "%d\n", mUmsDirtyRatio);
1421 } else {
1422 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1423 }
1424 fclose(fp);
1425 } else {
1426 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1427 }
1428 }
San Mehata2677e42009-12-13 10:40:18 -08001429 return 0;
1430}
1431
1432int VolumeManager::unshareVolume(const char *label, const char *method) {
1433 Volume *v = lookupVolume(label);
1434
1435 if (!v) {
1436 errno = ENOENT;
1437 return -1;
1438 }
1439
1440 if (strcmp(method, "ums")) {
1441 errno = ENOSYS;
1442 return -1;
1443 }
1444
1445 if (v->getState() != Volume::State_Shared) {
1446 errno = EINVAL;
1447 return -1;
1448 }
1449
San Mehata2677e42009-12-13 10:40:18 -08001450 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001451 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001452 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001453 return -1;
1454 }
1455
1456 char ch = 0;
1457 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001458 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001459 close(fd);
1460 return -1;
1461 }
1462
1463 close(fd);
1464 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001465 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1466 FILE* fp;
1467 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1468 fprintf(fp, "%d\n", mSavedDirtyRatio);
1469 fclose(fp);
1470 } else {
1471 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1472 }
1473 mSavedDirtyRatio = -1;
1474 }
San Mehata2677e42009-12-13 10:40:18 -08001475 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001476}
1477
Ken Sumrall3b170052011-07-11 15:38:57 -07001478extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001479 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001480 vm->disableVolumeManager();
1481 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001482 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001483}
1484
1485extern "C" int vold_getNumDirectVolumes(void) {
1486 VolumeManager *vm = VolumeManager::Instance();
1487 return vm->getNumDirectVolumes();
1488}
1489
1490int VolumeManager::getNumDirectVolumes(void) {
1491 VolumeCollection::iterator i;
1492 int n=0;
1493
1494 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1495 if ((*i)->getShareDevice() != (dev_t)0) {
1496 n++;
1497 }
1498 }
1499 return n;
1500}
1501
1502extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1503 VolumeManager *vm = VolumeManager::Instance();
1504 return vm->getDirectVolumeList(vol_list);
1505}
1506
1507int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1508 VolumeCollection::iterator i;
1509 int n=0;
1510 dev_t d;
1511
1512 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1513 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1514 (*i)->getVolInfo(&vol_list[n]);
1515 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
Colin Cross346c5b22014-01-22 23:59:41 -08001516 "/dev/block/vold/%d:%d", major(d), minor(d));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001517 n++;
1518 }
1519 }
1520
1521 return 0;
1522}
1523
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001524int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001525 Volume *v = lookupVolume(label);
1526
1527 if (!v) {
1528 errno = ENOENT;
1529 return -1;
1530 }
1531
San Mehata2677e42009-12-13 10:40:18 -08001532 if (v->getState() == Volume::State_NoMedia) {
1533 errno = ENODEV;
1534 return -1;
1535 }
1536
San Mehat49e2bce2009-10-12 16:29:01 -07001537 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001538 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001539 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001540 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001541 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001542 }
1543
San Mehat1a06eda2010-04-15 12:58:50 -07001544 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001545
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001546 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001547}
1548
Ken Sumrall425524d2012-06-14 20:55:28 -07001549extern "C" int vold_unmountAllAsecs(void) {
1550 int rc;
1551
1552 VolumeManager *vm = VolumeManager::Instance();
1553 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1554 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1555 rc = -1;
1556 }
1557 return rc;
1558}
1559
1560#define ID_BUF_LEN 256
1561#define ASEC_SUFFIX ".asec"
1562#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1563int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1564 DIR *d = opendir(directory);
1565 int rc = 0;
1566
1567 if (!d) {
1568 SLOGE("Could not open asec dir %s", directory);
1569 return -1;
1570 }
1571
1572 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -07001573 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Ken Sumrall425524d2012-06-14 20:55:28 -07001574
1575 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1576 if (dent == NULL) {
1577 SLOGE("Failed to allocate memory for asec dir");
1578 return -1;
1579 }
1580
1581 struct dirent *result;
1582 while (!readdir_r(d, dent, &result) && result != NULL) {
1583 if (dent->d_name[0] == '.')
1584 continue;
1585 if (dent->d_type != DT_REG)
1586 continue;
1587 size_t name_len = strlen(dent->d_name);
1588 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1589 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1590 char id[ID_BUF_LEN];
1591 strlcpy(id, dent->d_name, name_len - 4);
1592 if (unmountAsec(id, true)) {
1593 /* Register the error, but try to unmount more asecs */
1594 rc = -1;
1595 }
1596 }
1597 }
1598 closedir(d);
1599
1600 free(dent);
1601
1602 return rc;
1603}
1604
San Mehata2677e42009-12-13 10:40:18 -08001605/*
1606 * Looks up a volume by it's label or mount-point
1607 */
San Mehat49e2bce2009-10-12 16:29:01 -07001608Volume *VolumeManager::lookupVolume(const char *label) {
1609 VolumeCollection::iterator i;
1610
1611 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001612 if (label[0] == '/') {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001613 if (!strcmp(label, (*i)->getFuseMountpoint()))
San Mehata2677e42009-12-13 10:40:18 -08001614 return (*i);
1615 } else {
1616 if (!strcmp(label, (*i)->getLabel()))
1617 return (*i);
1618 }
San Mehat49e2bce2009-10-12 16:29:01 -07001619 }
1620 return NULL;
1621}
San Mehata19b2502010-01-06 10:33:53 -08001622
1623bool VolumeManager::isMountpointMounted(const char *mp)
1624{
1625 char device[256];
1626 char mount_path[256];
1627 char rest[256];
1628 FILE *fp;
1629 char line[1024];
1630
1631 if (!(fp = fopen("/proc/mounts", "r"))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001632 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001633 return false;
1634 }
1635
1636 while(fgets(line, sizeof(line), fp)) {
1637 line[strlen(line)-1] = '\0';
1638 sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
1639 if (!strcmp(mount_path, mp)) {
1640 fclose(fp);
1641 return true;
1642 }
San Mehata19b2502010-01-06 10:33:53 -08001643 }
1644
1645 fclose(fp);
1646 return false;
1647}
1648
San Mehat1a06eda2010-04-15 12:58:50 -07001649int VolumeManager::cleanupAsec(Volume *v, bool force) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001650 int rc = 0;
Kenny Root93ecb382012-08-09 11:28:37 -07001651
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001652 char asecFileName[255];
1653
1654 AsecIdCollection removeAsec;
1655 AsecIdCollection removeObb;
1656
Kenny Root93ecb382012-08-09 11:28:37 -07001657 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1658 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001659 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07001660
Kenny Rootcbacf782010-09-24 15:11:48 -07001661 if (cd->type == ASEC) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001662 if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) {
1663 SLOGE("Couldn't find ASEC %s; cleaning up", cd->id);
1664 removeAsec.push_back(cd);
1665 } else {
1666 SLOGD("Found ASEC at path %s", asecFileName);
1667 if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT,
1668 strlen(Volume::SEC_ASECDIR_EXT))) {
1669 removeAsec.push_back(cd);
1670 }
1671 }
Kenny Rootcbacf782010-09-24 15:11:48 -07001672 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07001673 if (v == getVolumeForFile(cd->id)) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001674 removeObb.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001675 }
1676 } else {
1677 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001678 }
1679 }
Kenny Root93ecb382012-08-09 11:28:37 -07001680
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001681 for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) {
Kenny Root93ecb382012-08-09 11:28:37 -07001682 ContainerData *cd = *it;
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001683 SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel());
1684 if (unmountAsec(cd->id, force)) {
1685 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1686 rc = -1;
1687 }
1688 }
1689
1690 for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) {
1691 ContainerData *cd = *it;
1692 SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel());
Kenny Root93ecb382012-08-09 11:28:37 -07001693 if (unmountObb(cd->id, force)) {
1694 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1695 rc = -1;
1696 }
1697 }
1698
1699 return rc;
San Mehat1a06eda2010-04-15 12:58:50 -07001700}
1701
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001702int VolumeManager::mkdirs(char* path) {
1703 // Require that path lives under a volume we manage
1704 const char* emulated_source = getenv("EMULATED_STORAGE_SOURCE");
1705 const char* root = NULL;
Marco Nelissen5ab02e72013-10-15 15:22:28 -07001706 if (emulated_source && !strncmp(path, emulated_source, strlen(emulated_source))) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001707 root = emulated_source;
1708 } else {
1709 Volume* vol = getVolumeForFile(path);
1710 if (vol) {
1711 root = vol->getMountpoint();
1712 }
1713 }
1714
1715 if (!root) {
1716 SLOGE("Failed to find volume for %s", path);
1717 return -EINVAL;
1718 }
1719
1720 /* fs_mkdirs() does symlink checking and relative path enforcement */
1721 return fs_mkdirs(path, 0700);
1722}