blob: e1ee96ce9b852bf0e86d04ed1b0285cbdd763c46 [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
Yabin Cuid1104f72015-01-02 13:28:28 -080017#include <dirent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070018#include <errno.h>
San Mehata2677e42009-12-13 10:40:18 -080019#include <fcntl.h>
Kenny Root344ca102012-04-03 17:23:01 -070020#include <fts.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080021#include <mntent.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/mount.h>
San Mehata19b2502010-01-06 10:33:53 -080027#include <sys/stat.h>
28#include <sys/types.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070029#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080030#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080031
San Mehata2677e42009-12-13 10:40:18 -080032#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070033
34#define LOG_TAG "Vold"
35
Kenny Root7b18a7b2010-03-15 13:13:41 -070036#include <openssl/md5.h>
37
Elliott Hughes7e128fb2015-12-04 15:50:53 -080038#include <android-base/logging.h>
39#include <android-base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070040#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070041#include <cutils/log.h>
42
Robert Craigb9e3ba52014-02-04 10:53:00 -050043#include <selinux/android.h>
44
San Mehatfd7f5872009-10-12 11:32:47 -070045#include <sysutils/NetlinkEvent.h>
46
Kenny Root344ca102012-04-03 17:23:01 -070047#include <private/android_filesystem_config.h>
48
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070049#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070050#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070051#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070052#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080053#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080054#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070055#include "fs/Ext4.h"
56#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070057#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080058#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080059#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080060#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090061#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070062#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080063
Mike Lockwood97f2fc12011-06-07 10:51:38 -070064#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
65
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070066#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
67 + ((number) & (~((1U << (po2)) - 1))))
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070068
Jeff Sharkey36801cc2015-03-13 16:09:20 -070069using android::base::StringPrintf;
70
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070071/*
72 * Path to external storage where *only* root can access ASEC image files
73 */
74const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
75
76/*
77 * Path to internal storage where *only* root can access ASEC image files
78 */
79const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
80
81/*
82 * Path to where secure containers are mounted
83 */
84const char *VolumeManager::ASECDIR = "/mnt/asec";
85
86/*
87 * Path to where OBBs are mounted
88 */
89const char *VolumeManager::LOOPDIR = "/mnt/obb";
90
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091static const char* kUserMountPath = "/mnt/user";
92
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080094static const unsigned int kMajorBlockExperimentalMin = 240;
95static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070097/* writes superblock at end of file or device given by name */
98static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070099 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700100 if (sbfd < 0) {
101 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
102 return -1;
103 }
104
105 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
106 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
107 close(sbfd);
108 return -1;
109 }
110
111 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
112 SLOGE("Failed to write superblock (%s)", strerror(errno));
113 close(sbfd);
114 return -1;
115 }
116 close(sbfd);
117 return 0;
118}
119
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200120static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700121 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
122 // preventing costly operations or unexpected ENOSPC error.
123 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200124 unsigned long clusterSectors = 4096 / 512;
125 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700126 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700127 return ROUND_UP_POWER_OF_2(numSectors, 3);
128}
129
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200130static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700131 /*
132 * Add some headroom
133 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200134 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700135 numSectors += fatSize + 2;
136 /*
137 * FAT is aligned to 32 kb with 512b sectors.
138 */
139 return ROUND_UP_POWER_OF_2(numSectors, 6);
140}
141
142static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
143 if (Loop::lookupActive(idHash, buffer, len)) {
144 if (Loop::create(idHash, asecFileName, buffer, len)) {
145 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
146 return -1;
147 }
148 if (debug) {
149 SLOGD("New loop device created at %s", buffer);
150 }
151 } else {
152 if (debug) {
153 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
154 }
155 }
156 return 0;
157}
158
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200159static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , unsigned long numImgSectors, bool* createdDMDevice, bool debug) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700160 if (strcmp(key, "none")) {
161 if (Devmapper::lookupActive(idHash, buffer, len)) {
162 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
163 buffer, len)) {
164 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
165 return -1;
166 }
167 if (debug) {
168 SLOGD("New devmapper instance created at %s", buffer);
169 }
170 } else {
171 if (debug) {
172 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
173 }
174 }
175 *createdDMDevice = true;
176 } else {
177 strcpy(buffer, loopDevice);
178 *createdDMDevice = false;
179 }
180 return 0;
181}
182
183static void waitForDevMapper(const char *dmDevice) {
184 /*
185 * Wait for the device mapper node to be created. Sometimes it takes a
186 * while. Wait for up to 1 second. We could also inspect incoming uevents,
187 * but that would take more effort.
188 */
189 int tries = 25;
190 while (tries--) {
191 if (!access(dmDevice, F_OK) || errno != ENOENT) {
192 break;
193 }
194 usleep(40 * 1000);
195 }
196}
197
San Mehatf1b736b2009-10-10 17:22:08 -0700198VolumeManager *VolumeManager::sInstance = NULL;
199
200VolumeManager *VolumeManager::Instance() {
201 if (!sInstance)
202 sInstance = new VolumeManager();
203 return sInstance;
204}
205
206VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800207 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800208 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700209 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400210 mUmsSharingCount = 0;
211 mSavedDirtyRatio = -1;
212 // set dirty ratio to 0 when UMS is active
213 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700214}
215
216VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800217 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700218}
219
Kenny Root7b18a7b2010-03-15 13:13:41 -0700220char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700221 static const char* digits = "0123456789abcdef";
222
Kenny Root7b18a7b2010-03-15 13:13:41 -0700223 unsigned char sig[MD5_DIGEST_LENGTH];
224
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700225 if (buffer == NULL) {
226 SLOGE("Destination buffer is NULL");
227 errno = ESPIPE;
228 return NULL;
229 } else if (id == NULL) {
230 SLOGE("Source buffer is NULL");
231 errno = ESPIPE;
232 return NULL;
233 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800234 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700235 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800236 errno = ESPIPE;
237 return NULL;
238 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700239
240 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800241
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700242 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700243 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700244 *p++ = digits[sig[i] >> 4];
245 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800246 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700247 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800248
249 return buffer;
250}
251
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700252int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800253 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700254 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800255}
256
San Mehatf1b736b2009-10-10 17:22:08 -0700257int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700258 // Always start from a clean slate by unmounting everything in
259 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700260 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700261
262 // Assume that we always have an emulated volume on internal
263 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700264 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700265 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700266 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700267 mInternalEmulated->create();
268
San Mehatf1b736b2009-10-10 17:22:08 -0700269 return 0;
270}
271
272int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700273 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700274 mInternalEmulated->destroy();
275 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700276 return 0;
277}
278
San Mehatfd7f5872009-10-12 11:32:47 -0700279void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700280 std::lock_guard<std::mutex> lock(mLock);
281
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700282 if (mDebug) {
283 LOG(VERBOSE) << "----------------";
284 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
285 evt->dump();
286 }
San Mehatf1b736b2009-10-10 17:22:08 -0700287
Mateusz Nowak64403792015-08-03 16:39:19 +0200288 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
289 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700290
291 if (devType != "disk") return;
292
293 int major = atoi(evt->findParam("MAJOR"));
294 int minor = atoi(evt->findParam("MINOR"));
295 dev_t device = makedev(major, minor);
296
297 switch (evt->getAction()) {
298 case NetlinkEvent::Action::kAdd: {
299 for (auto source : mDiskSources) {
300 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800301 // For now, assume that MMC and virtio-blk (the latter is
302 // emulator-specific; see Disk.cpp for details) devices are SD,
303 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700304 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800305 if (major == kMajorBlockMmc
306 || (android::vold::IsRunningInEmulator()
307 && major >= (int) kMajorBlockExperimentalMin
308 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700309 flags |= android::vold::Disk::Flags::kSd;
310 } else {
311 flags |= android::vold::Disk::Flags::kUsb;
312 }
313
314 auto disk = new android::vold::Disk(eventPath, device,
315 source->getNickname(), flags);
316 disk->create();
317 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
318 break;
319 }
320 }
321 break;
322 }
323 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700324 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 for (auto disk : mDisks) {
326 if (disk->getDevice() == device) {
327 disk->readMetadata();
328 disk->readPartitions();
329 }
330 }
331 break;
332 }
333 case NetlinkEvent::Action::kRemove: {
334 auto i = mDisks.begin();
335 while (i != mDisks.end()) {
336 if ((*i)->getDevice() == device) {
337 (*i)->destroy();
338 i = mDisks.erase(i);
339 } else {
340 ++i;
341 }
342 }
343 break;
344 }
345 default: {
346 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
347 break;
348 }
349 }
350}
351
352void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
353 mDiskSources.push_back(diskSource);
354}
355
356std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
357 for (auto disk : mDisks) {
358 if (disk->getId() == id) {
359 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700360 }
361 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700362 return nullptr;
363}
San Mehatf1b736b2009-10-10 17:22:08 -0700364
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700365std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
366 if (mInternalEmulated->getId() == id) {
367 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700368 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700369 for (auto disk : mDisks) {
370 auto vol = disk->findVolume(id);
371 if (vol != nullptr) {
372 return vol;
373 }
374 }
375 return nullptr;
376}
377
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700378void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
379 std::list<std::string>& list) {
380 list.clear();
381 for (auto disk : mDisks) {
382 disk->listVolumes(type, list);
383 }
384}
385
386nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700387 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700388 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700389 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700390 } else {
391 auto vol = findVolume(id);
392 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
393 path = vol->getPath();
394 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700395 }
396
397 if (path.empty()) {
398 LOG(WARNING) << "Failed to find volume for " << id;
399 return -1;
400 }
401
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700402 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700403}
404
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700405int VolumeManager::forgetPartition(const std::string& partGuid) {
406 std::string normalizedGuid;
407 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
408 LOG(WARNING) << "Invalid GUID " << partGuid;
409 return -1;
410 }
411
412 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
413 if (unlink(keyPath.c_str()) != 0) {
414 LOG(ERROR) << "Failed to unlink " << keyPath;
415 return -1;
416 }
417
418 return 0;
419}
420
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700421int VolumeManager::linkPrimary(userid_t userId) {
422 std::string source(mPrimary->getPath());
423 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
424 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700425 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700426 }
427
428 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
429 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
430 if (errno != ENOENT) {
431 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
432 }
433 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700434 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700435 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
436 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
437 strerror(errno));
438 return -errno;
439 }
440 return 0;
441}
442
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700443int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
444 mAddedUsers[userId] = userSerialNumber;
445 return 0;
446}
447
448int VolumeManager::onUserRemoved(userid_t userId) {
449 mAddedUsers.erase(userId);
450 return 0;
451}
452
453int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700454 // Note that sometimes the system will spin up processes from Zygote
455 // before actually starting the user, so we're okay if Zygote
456 // already created this directory.
457 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
458 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
459
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700460 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700461 if (mPrimary) {
462 linkPrimary(userId);
463 }
464 return 0;
465}
466
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700467int VolumeManager::onUserStopped(userid_t userId) {
468 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700469 return 0;
470}
471
472int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
473 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700474 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700475 linkPrimary(userId);
476 }
477 return 0;
478}
479
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700480static int unmount_tree(const char* path) {
481 size_t path_len = strlen(path);
482
483 FILE* fp = setmntent("/proc/mounts", "r");
484 if (fp == NULL) {
485 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
486 return -errno;
487 }
488
489 // Some volumes can be stacked on each other, so force unmount in
490 // reverse order to give us the best chance of success.
491 std::list<std::string> toUnmount;
492 mntent* mentry;
493 while ((mentry = getmntent(fp)) != NULL) {
494 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
495 toUnmount.push_front(std::string(mentry->mnt_dir));
496 }
497 }
498 endmntent(fp);
499
500 for (auto path : toUnmount) {
501 if (umount2(path.c_str(), MNT_DETACH)) {
502 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
503 }
504 }
505 return 0;
506}
507
Jeff Sharkey66270a22015-06-24 11:49:24 -0700508int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
509 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
510
511 DIR* dir;
512 struct dirent* de;
513 char rootName[PATH_MAX];
514 char pidName[PATH_MAX];
515 int pidFd;
516 int nsFd;
517 struct stat sb;
518 pid_t child;
519
520 if (!(dir = opendir("/proc"))) {
521 PLOG(ERROR) << "Failed to opendir";
522 return -1;
523 }
524
525 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900526 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700527 PLOG(ERROR) << "Failed to readlink";
528 closedir(dir);
529 return -1;
530 }
531
532 // Poke through all running PIDs look for apps running as UID
533 while ((de = readdir(dir))) {
534 pidFd = -1;
535 nsFd = -1;
536
537 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
538 if (pidFd < 0) {
539 goto next;
540 }
541 if (fstat(pidFd, &sb) != 0) {
542 PLOG(WARNING) << "Failed to stat " << de->d_name;
543 goto next;
544 }
545 if (sb.st_uid != uid) {
546 goto next;
547 }
548
549 // Matches so far, but refuse to touch if in root namespace
550 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900551 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700552 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
553 goto next;
554 }
555 if (!strcmp(rootName, pidName)) {
556 LOG(WARNING) << "Skipping due to root namespace";
557 goto next;
558 }
559
560 // We purposefully leave the namespace open across the fork
561 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
562 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700563 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700564 goto next;
565 }
566
567 if (!(child = fork())) {
568 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700569 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700570 _exit(1);
571 }
572
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700573 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700574
575 std::string storageSource;
576 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700577 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700578 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700579 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700580 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700581 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700582 } else {
583 // Sane default of no storage visible
584 _exit(0);
585 }
586 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
587 NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700588 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
589 << de->d_name;
590 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700591 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700592
593 // Mount user-specific symlink helper into place
594 userid_t user_id = multiuser_get_user_id(uid);
595 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
596 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
597 NULL, MS_BIND, NULL)) == -1) {
598 PLOG(ERROR) << "Failed to mount " << userSource << " for "
599 << de->d_name;
600 _exit(1);
601 }
602
Jeff Sharkey66270a22015-06-24 11:49:24 -0700603 _exit(0);
604 }
605
606 if (child == -1) {
607 PLOG(ERROR) << "Failed to fork";
608 goto next;
609 } else {
610 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
611 }
612
613next:
614 close(nsFd);
615 close(pidFd);
616 }
617 closedir(dir);
618 return 0;
619}
620
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700621int VolumeManager::reset() {
622 // Tear down all existing disks/volumes and start from a blank slate so
623 // newly connected framework hears all events.
624 mInternalEmulated->destroy();
625 mInternalEmulated->create();
626 for (auto disk : mDisks) {
627 disk->destroy();
628 disk->create();
629 }
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700630 mAddedUsers.clear();
631 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700632 return 0;
633}
634
635int VolumeManager::shutdown() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700636 mInternalEmulated->destroy();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700637 for (auto disk : mDisks) {
638 disk->destroy();
639 }
640 mDisks.clear();
641 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700642}
643
Jeff Sharkey9c484982015-03-31 10:35:33 -0700644int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700645 std::lock_guard<std::mutex> lock(mLock);
646
Jeff Sharkey9c484982015-03-31 10:35:33 -0700647 // First, try gracefully unmounting all known devices
648 if (mInternalEmulated != nullptr) {
649 mInternalEmulated->unmount();
650 }
651 for (auto disk : mDisks) {
652 disk->unmountAll();
653 }
654
655 // Worst case we might have some stale mounts lurking around, so
656 // force unmount those just to be safe.
657 FILE* fp = setmntent("/proc/mounts", "r");
658 if (fp == NULL) {
659 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
660 return -errno;
661 }
662
663 // Some volumes can be stacked on each other, so force unmount in
664 // reverse order to give us the best chance of success.
665 std::list<std::string> toUnmount;
666 mntent* mentry;
667 while ((mentry = getmntent(fp)) != NULL) {
668 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
669 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
670 toUnmount.push_front(std::string(mentry->mnt_dir));
671 }
672 }
673 endmntent(fp);
674
675 for (auto path : toUnmount) {
676 SLOGW("Tearing down stale mount %s", path.c_str());
677 android::vold::ForceUnmount(path);
678 }
679
680 return 0;
681}
682
Kenny Root508c0e12010-07-12 09:59:49 -0700683int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
684 char idHash[33];
685 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
686 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
687 return -1;
688 }
689
690 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700691 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400692 if ((written < 0) || (written >= mountPathLen)) {
693 errno = EINVAL;
694 return -1;
695 }
Kenny Root508c0e12010-07-12 09:59:49 -0700696
697 if (access(mountPath, F_OK)) {
698 errno = ENOENT;
699 return -1;
700 }
701
702 return 0;
703}
704
San Mehata19b2502010-01-06 10:33:53 -0800705int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700706 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700707
Nick Kralevich0de7c612014-01-27 14:58:06 -0800708 if (!isLegalAsecId(id)) {
709 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
710 errno = EINVAL;
711 return -1;
712 }
713
Kenny Root344ca102012-04-03 17:23:01 -0700714 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
715 SLOGE("Couldn't find ASEC %s", id);
716 return -1;
717 }
San Mehat88ac2c02010-03-23 11:15:58 -0700718
719 memset(buffer, 0, maxlen);
720 if (access(asecFileName, F_OK)) {
721 errno = ENOENT;
722 return -1;
723 }
San Mehata19b2502010-01-06 10:33:53 -0800724
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700725 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400726 if ((written < 0) || (written >= maxlen)) {
727 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
728 errno = EINVAL;
729 return -1;
730 }
731
San Mehata19b2502010-01-06 10:33:53 -0800732 return 0;
733}
734
Dianne Hackborn736910c2011-06-27 13:37:07 -0700735int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
736 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700737
Nick Kralevich0de7c612014-01-27 14:58:06 -0800738 if (!isLegalAsecId(id)) {
739 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
740 errno = EINVAL;
741 return -1;
742 }
743
Kenny Root344ca102012-04-03 17:23:01 -0700744 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
745 SLOGE("Couldn't find ASEC %s", id);
746 return -1;
747 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700748
749 memset(buffer, 0, maxlen);
750 if (access(asecFileName, F_OK)) {
751 errno = ENOENT;
752 return -1;
753 }
754
rpcraigd1c226f2012-10-09 06:58:16 -0400755 int written = snprintf(buffer, maxlen, "%s", asecFileName);
756 if ((written < 0) || (written >= maxlen)) {
757 errno = EINVAL;
758 return -1;
759 }
760
Dianne Hackborn736910c2011-06-27 13:37:07 -0700761 return 0;
762}
763
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200764int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700765 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800766 struct asec_superblock sb;
767 memset(&sb, 0, sizeof(sb));
768
Nick Kralevich0de7c612014-01-27 14:58:06 -0800769 if (!isLegalAsecId(id)) {
770 SLOGE("createAsec: Invalid asec id \"%s\"", id);
771 errno = EINVAL;
772 return -1;
773 }
774
Kenny Root344ca102012-04-03 17:23:01 -0700775 const bool wantFilesystem = strcmp(fstype, "none");
776 bool usingExt4 = false;
777 if (wantFilesystem) {
778 usingExt4 = !strcmp(fstype, "ext4");
779 if (usingExt4) {
780 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
781 } else if (strcmp(fstype, "fat")) {
782 SLOGE("Invalid filesystem type %s", fstype);
783 errno = EINVAL;
784 return -1;
785 }
786 }
787
San Mehatfcf24fe2010-03-03 12:37:32 -0800788 sb.magic = ASEC_SB_MAGIC;
789 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800790
San Mehatd31e3802010-02-18 08:37:45 -0800791 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200792 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800793 errno = EINVAL;
794 return -1;
795 }
796
San Mehata19b2502010-01-06 10:33:53 -0800797 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700798
799 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
800 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
801 asecFileName, strerror(errno));
802 errno = EADDRINUSE;
803 return -1;
804 }
805
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700806 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700807
rpcraigd1c226f2012-10-09 06:58:16 -0400808 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
809 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
810 errno = EINVAL;
811 return -1;
812 }
San Mehata19b2502010-01-06 10:33:53 -0800813
814 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700815 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700816 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800817 errno = EADDRINUSE;
818 return -1;
819 }
820
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200821 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700822 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700823 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700824 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700825 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800826
827 // Add +1 for our superblock which is at the end
828 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700829 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800830 return -1;
831 }
832
San Mehatd9a4e352010-03-12 13:32:47 -0800833 char idHash[33];
834 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700835 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800836 unlink(asecFileName);
837 return -1;
838 }
839
San Mehata19b2502010-01-06 10:33:53 -0800840 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800841 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700842 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800843 unlink(asecFileName);
844 return -1;
845 }
846
San Mehatb78a32c2010-01-10 13:02:12 -0800847 char dmDevice[255];
848 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800849
San Mehatb78a32c2010-01-10 13:02:12 -0800850 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800851 // XXX: This is all we support for now
852 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800853 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800854 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700855 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800856 Loop::destroyByDevice(loopDevice);
857 unlink(asecFileName);
858 return -1;
859 }
860 cleanupDm = true;
861 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800862 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800863 strcpy(dmDevice, loopDevice);
864 }
865
San Mehatfcf24fe2010-03-03 12:37:32 -0800866 /*
867 * Drop down the superblock at the end of the file
868 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700869 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800870 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800871 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800872 }
873 Loop::destroyByDevice(loopDevice);
874 unlink(asecFileName);
875 return -1;
876 }
877
Kenny Root344ca102012-04-03 17:23:01 -0700878 if (wantFilesystem) {
879 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400880 char mountPoint[255];
881
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700882 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400883 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
884 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
885 if (cleanupDm) {
886 Devmapper::destroy(idHash);
887 }
888 Loop::destroyByDevice(loopDevice);
889 unlink(asecFileName);
890 return -1;
891 }
rpcraiga54e13a2012-09-21 14:17:08 -0400892
Kenny Root344ca102012-04-03 17:23:01 -0700893 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700894 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700895 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700896 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800897 }
San Mehata19b2502010-01-06 10:33:53 -0800898
Kenny Root344ca102012-04-03 17:23:01 -0700899 if (formatStatus < 0) {
900 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800901 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800902 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800903 }
San Mehateb13a902010-01-07 12:12:50 -0800904 Loop::destroyByDevice(loopDevice);
905 unlink(asecFileName);
906 return -1;
907 }
Kenny Root344ca102012-04-03 17:23:01 -0700908
Kenny Root344ca102012-04-03 17:23:01 -0700909 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800910 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700911 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800912 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800913 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800914 }
915 Loop::destroyByDevice(loopDevice);
916 unlink(asecFileName);
917 return -1;
918 }
San Mehatb78a32c2010-01-10 13:02:12 -0800919 }
San Mehata1091cb2010-02-28 20:17:20 -0800920
Kenny Root344ca102012-04-03 17:23:01 -0700921 int mountStatus;
922 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700923 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
924 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -0700925 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700926 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
927 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -0700928 }
929
930 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700931 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800932 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800933 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800934 }
935 Loop::destroyByDevice(loopDevice);
936 unlink(asecFileName);
937 return -1;
938 }
Kenny Root344ca102012-04-03 17:23:01 -0700939
940 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700941 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700942 if (dirfd >= 0) {
943 if (fchown(dirfd, ownerUid, AID_SYSTEM)
944 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
945 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
946 }
947 close(dirfd);
948 }
949 }
San Mehata1091cb2010-02-28 20:17:20 -0800950 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700951 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800952 }
San Mehat88705162010-01-15 09:26:28 -0800953
Kenny Rootcbacf782010-09-24 15:11:48 -0700954 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800955 return 0;
956}
957
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200958int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700959 char asecFileName[255];
960 char mountPoint[255];
961 bool cleanupDm = false;
962
963 if (!isLegalAsecId(id)) {
964 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
965 errno = EINVAL;
966 return -1;
967 }
968
969 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
970 SLOGE("Couldn't find ASEC %s", id);
971 return -1;
972 }
973
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700974 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700975 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
976 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
977 return -1;
978 }
979
980 if (isMountpointMounted(mountPoint)) {
981 SLOGE("ASEC %s mounted. Unmount before resizing", id);
982 errno = EBUSY;
983 return -1;
984 }
985
986 struct asec_superblock sb;
987 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200988 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700989
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700990 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700991 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
992 return -1;
993 }
994
995 struct stat info;
996 if (fstat(fd, &info) < 0) {
997 SLOGE("Failed to get file size (%s)", strerror(errno));
998 close(fd);
999 return -1;
1000 }
1001
1002 oldNumSec = info.st_size / 512;
1003
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001004 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001005 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
1006 numImgSectors = adjustSectorNumExt4(numSectors);
1007 else
1008 numImgSectors = adjustSectorNumFAT(numSectors);
1009 /*
1010 * add one block for the superblock
1011 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001012 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001013 if (oldNumSec == numImgSectors + 1) {
1014 SLOGW("Size unchanged; ignoring resize request");
1015 return 0;
1016 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001017 SLOGE("Only growing is currently supported.");
1018 close(fd);
1019 return -1;
1020 }
1021
1022 /*
1023 * Try to read superblock.
1024 */
1025 memset(&sb, 0, sizeof(struct asec_superblock));
1026 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1027 SLOGE("lseek failed (%s)", strerror(errno));
1028 close(fd);
1029 return -1;
1030 }
1031 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1032 SLOGE("superblock read failed (%s)", strerror(errno));
1033 close(fd);
1034 return -1;
1035 }
1036 close(fd);
1037
1038 if (mDebug) {
1039 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1040 }
1041 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1042 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1043 errno = EMEDIUMTYPE;
1044 return -1;
1045 }
1046
1047 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1048 SLOGE("Only ext4 partitions are supported for resize");
1049 errno = EINVAL;
1050 return -1;
1051 }
1052
1053 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1054 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1055 return -1;
1056 }
1057
1058 /*
1059 * Drop down a copy of the superblock at the end of the file
1060 */
1061 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1062 goto fail;
1063
1064 char idHash[33];
1065 if (!asecHash(id, idHash, sizeof(idHash))) {
1066 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1067 goto fail;
1068 }
1069
1070 char loopDevice[255];
1071 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1072 goto fail;
1073
1074 char dmDevice[255];
1075
1076 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1077 Loop::destroyByDevice(loopDevice);
1078 goto fail;
1079 }
1080
1081 /*
1082 * Wait for the device mapper node to be created.
1083 */
1084 waitForDevMapper(dmDevice);
1085
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001086 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001087 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1088 if (cleanupDm) {
1089 Devmapper::destroy(idHash);
1090 }
1091 Loop::destroyByDevice(loopDevice);
1092 goto fail;
1093 }
1094
1095 return 0;
1096fail:
1097 Loop::resizeImageFile(asecFileName, oldNumSec);
1098 return -1;
1099}
1100
San Mehata19b2502010-01-06 10:33:53 -08001101int VolumeManager::finalizeAsec(const char *id) {
1102 char asecFileName[255];
1103 char loopDevice[255];
1104 char mountPoint[255];
1105
Nick Kralevich0de7c612014-01-27 14:58:06 -08001106 if (!isLegalAsecId(id)) {
1107 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1108 errno = EINVAL;
1109 return -1;
1110 }
1111
Kenny Root344ca102012-04-03 17:23:01 -07001112 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1113 SLOGE("Couldn't find ASEC %s", id);
1114 return -1;
1115 }
San Mehata19b2502010-01-06 10:33:53 -08001116
San Mehatd9a4e352010-03-12 13:32:47 -08001117 char idHash[33];
1118 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001119 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001120 return -1;
1121 }
1122
1123 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001124 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001125 return -1;
1126 }
1127
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001128 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001129 struct asec_superblock sb;
1130
1131 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1132 return -1;
1133 }
1134
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001135 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001136 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1137 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1138 return -1;
1139 }
Kenny Root344ca102012-04-03 17:23:01 -07001140
1141 int result = 0;
1142 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001143 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1144 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001145 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001146 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1147 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001148 }
1149
1150 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001151 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001152 return -1;
1153 }
1154
San Mehatd9a4e352010-03-12 13:32:47 -08001155 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001156 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001157 }
San Mehata19b2502010-01-06 10:33:53 -08001158 return 0;
1159}
1160
Kenny Root344ca102012-04-03 17:23:01 -07001161int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1162 char asecFileName[255];
1163 char loopDevice[255];
1164 char mountPoint[255];
1165
1166 if (gid < AID_APP) {
1167 SLOGE("Group ID is not in application range");
1168 return -1;
1169 }
1170
Nick Kralevich0de7c612014-01-27 14:58:06 -08001171 if (!isLegalAsecId(id)) {
1172 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1173 errno = EINVAL;
1174 return -1;
1175 }
1176
Kenny Root344ca102012-04-03 17:23:01 -07001177 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1178 SLOGE("Couldn't find ASEC %s", id);
1179 return -1;
1180 }
1181
1182 char idHash[33];
1183 if (!asecHash(id, idHash, sizeof(idHash))) {
1184 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1185 return -1;
1186 }
1187
1188 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1189 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1190 return -1;
1191 }
1192
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001193 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001194 struct asec_superblock sb;
1195
1196 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1197 return -1;
1198 }
1199
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001200 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001201 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1202 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1203 return -1;
1204 }
Kenny Root344ca102012-04-03 17:23:01 -07001205
1206 int result = 0;
1207 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1208 return 0;
1209 }
1210
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001211 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001212 false /* read-only */,
1213 true /* remount */,
1214 false /* executable */);
1215 if (ret) {
1216 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1217 return -1;
1218 }
1219
1220 char *paths[] = { mountPoint, NULL };
1221
1222 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1223 if (fts) {
1224 // Traverse the entire hierarchy and chown to system UID.
1225 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1226 // We don't care about the lost+found directory.
1227 if (!strcmp(ftsent->fts_name, "lost+found")) {
1228 continue;
1229 }
1230
1231 /*
1232 * There can only be one file marked as private right now.
1233 * This should be more robust, but it satisfies the requirements
1234 * we have for right now.
1235 */
1236 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1237
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001238 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001239 if (fd < 0) {
1240 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1241 result = -1;
1242 continue;
1243 }
1244
1245 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1246
1247 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001248 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001249 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001250 result |= fchmod(fd, privateFile ? 0640 : 0644);
1251 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001252
Stephen Smalley5093e612014-02-12 09:43:08 -05001253 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001254 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1255 result |= -1;
1256 }
1257
Kenny Root344ca102012-04-03 17:23:01 -07001258 close(fd);
1259 }
1260 fts_close(fts);
1261
1262 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001263 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001264 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1265 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1266 result |= -1;
1267 }
1268 close(dirfd);
1269 } else {
1270 result |= -1;
1271 }
1272
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001273 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001274 true /* read-only */,
1275 true /* remount */,
1276 true /* execute */);
1277
1278 if (result) {
1279 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1280 return -1;
1281 }
1282
1283 if (mDebug) {
1284 SLOGD("ASEC %s permissions fixed", id);
1285 }
1286 return 0;
1287}
1288
San Mehat048b0802010-01-23 08:17:06 -08001289int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001290 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001291 char *asecFilename2;
1292 char mountPoint[255];
1293
Kenny Root344ca102012-04-03 17:23:01 -07001294 const char *dir;
1295
Nick Kralevich0de7c612014-01-27 14:58:06 -08001296 if (!isLegalAsecId(id1)) {
1297 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1298 errno = EINVAL;
1299 return -1;
1300 }
1301
1302 if (!isLegalAsecId(id2)) {
1303 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1304 errno = EINVAL;
1305 return -1;
1306 }
1307
Kenny Root344ca102012-04-03 17:23:01 -07001308 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1309 SLOGE("Couldn't find ASEC %s", id1);
1310 return -1;
1311 }
1312
1313 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001314
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001315 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001316 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1317 SLOGE("Rename failed: couldn't construct mountpoint");
1318 goto out_err;
1319 }
1320
San Mehat048b0802010-01-23 08:17:06 -08001321 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001322 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001323 errno = EBUSY;
1324 goto out_err;
1325 }
1326
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001327 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001328 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1329 SLOGE("Rename failed: couldn't construct mountpoint2");
1330 goto out_err;
1331 }
1332
San Mehat96956ed2010-02-24 08:42:51 -08001333 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001334 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001335 errno = EBUSY;
1336 goto out_err;
1337 }
1338
San Mehat048b0802010-01-23 08:17:06 -08001339 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001340 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001341 errno = EADDRINUSE;
1342 goto out_err;
1343 }
1344
1345 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001346 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001347 goto out_err;
1348 }
1349
San Mehat048b0802010-01-23 08:17:06 -08001350 free(asecFilename2);
1351 return 0;
1352
1353out_err:
San Mehat048b0802010-01-23 08:17:06 -08001354 free(asecFilename2);
1355 return -1;
1356}
1357
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001358#define UNMOUNT_RETRIES 5
1359#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001360int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001361 char asecFileName[255];
1362 char mountPoint[255];
1363
Nick Kralevich0de7c612014-01-27 14:58:06 -08001364 if (!isLegalAsecId(id)) {
1365 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1366 errno = EINVAL;
1367 return -1;
1368 }
1369
Kenny Root344ca102012-04-03 17:23:01 -07001370 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1371 SLOGE("Couldn't find ASEC %s", id);
1372 return -1;
1373 }
1374
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001375 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001376 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1377 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1378 return -1;
1379 }
San Mehata19b2502010-01-06 10:33:53 -08001380
San Mehatd9a4e352010-03-12 13:32:47 -08001381 char idHash[33];
1382 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001383 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001384 return -1;
1385 }
1386
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001387 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1388}
1389
Kenny Root508c0e12010-07-12 09:59:49 -07001390int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001391 char mountPoint[255];
1392
1393 char idHash[33];
1394 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1395 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1396 return -1;
1397 }
1398
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001399 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001400 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1401 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1402 return -1;
1403 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001404
1405 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1406}
1407
1408int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1409 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001410 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001411 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001412 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001413 return -1;
1414 }
San Mehat23969932010-01-09 07:08:06 -08001415
San Mehatb78a32c2010-01-10 13:02:12 -08001416 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001417 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001418 rc = umount(mountPoint);
1419 if (!rc) {
1420 break;
San Mehata19b2502010-01-06 10:33:53 -08001421 }
San Mehatb78a32c2010-01-10 13:02:12 -08001422 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001423 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001424 rc = 0;
1425 break;
San Mehata19b2502010-01-06 10:33:53 -08001426 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001427 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001428 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001429
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001430 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001431
1432 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001433 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001434 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001435 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001436 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001437 }
San Mehat8c940ef2010-02-13 14:19:53 -08001438
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001439 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001440 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001441 }
1442
1443 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001444 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001445 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001446 return -1;
1447 }
1448
San Mehat12f4b892010-02-24 11:43:22 -08001449 int retries = 10;
1450
1451 while(retries--) {
1452 if (!rmdir(mountPoint)) {
1453 break;
1454 }
1455
San Mehat97ac40e2010-03-24 10:24:19 -07001456 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001457 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001458 }
1459
1460 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001461 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001462 }
San Mehat88705162010-01-15 09:26:28 -08001463
Paul Lawrence60dec162014-09-02 10:52:15 -07001464 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1465 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1466 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1467 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1468 continue;
1469 } else {
1470 break;
1471 }
San Mehata19b2502010-01-06 10:33:53 -08001472 }
1473
1474 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001475 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001476 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001477 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001478 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001479 }
San Mehat88705162010-01-15 09:26:28 -08001480
1481 AsecIdCollection::iterator it;
1482 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001483 ContainerData* cd = *it;
1484 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001485 free(*it);
1486 mActiveContainers->erase(it);
1487 break;
1488 }
1489 }
1490 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001491 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001492 }
San Mehatb78a32c2010-01-10 13:02:12 -08001493 return 0;
1494}
1495
San Mehat4ba89482010-02-18 09:00:18 -08001496int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001497 char asecFileName[255];
1498 char mountPoint[255];
1499
Nick Kralevich0de7c612014-01-27 14:58:06 -08001500 if (!isLegalAsecId(id)) {
1501 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1502 errno = EINVAL;
1503 return -1;
1504 }
1505
Kenny Root344ca102012-04-03 17:23:01 -07001506 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1507 SLOGE("Couldn't find ASEC %s", id);
1508 return -1;
1509 }
1510
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001511 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001512 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1513 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1514 return -1;
1515 }
San Mehatb78a32c2010-01-10 13:02:12 -08001516
San Mehat0586d542010-01-12 15:38:59 -08001517 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001518 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001519 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001520 }
San Mehat4ba89482010-02-18 09:00:18 -08001521 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001522 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001523 return -1;
1524 }
1525 }
San Mehata19b2502010-01-06 10:33:53 -08001526
San Mehat0586d542010-01-12 15:38:59 -08001527 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001528 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001529 return -1;
1530 }
San Mehata19b2502010-01-06 10:33:53 -08001531
San Mehatd9a4e352010-03-12 13:32:47 -08001532 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001533 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001534 }
San Mehata19b2502010-01-06 10:33:53 -08001535 return 0;
1536}
1537
Nick Kralevich0de7c612014-01-27 14:58:06 -08001538/*
1539 * Legal ASEC ids consist of alphanumeric characters, '-',
1540 * '_', or '.'. ".." is not allowed. The first or last character
1541 * of the ASEC id cannot be '.' (dot).
1542 */
1543bool VolumeManager::isLegalAsecId(const char *id) const {
1544 size_t i;
1545 size_t len = strlen(id);
1546
1547 if (len == 0) {
1548 return false;
1549 }
1550 if ((id[0] == '.') || (id[len - 1] == '.')) {
1551 return false;
1552 }
1553
1554 for (i = 0; i < len; i++) {
1555 if (id[i] == '.') {
1556 // i=0 is guaranteed never to have a dot. See above.
1557 if (id[i-1] == '.') return false;
1558 continue;
1559 }
1560 if (id[i] == '_' || id[i] == '-') continue;
1561 if (id[i] >= 'a' && id[i] <= 'z') continue;
1562 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1563 if (id[i] >= '0' && id[i] <= '9') continue;
1564 return false;
1565 }
1566
1567 return true;
1568}
1569
Kenny Root344ca102012-04-03 17:23:01 -07001570bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001571 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001572 if (dirfd < 0) {
1573 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001574 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001575 }
1576
Nick Kralevich25e581a2015-02-06 08:55:08 -08001577 struct stat sb;
1578 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1579 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001580
1581 close(dirfd);
1582
1583 return ret;
1584}
1585
1586int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1587 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001588 char *asecName;
1589
Nick Kralevich0de7c612014-01-27 14:58:06 -08001590 if (!isLegalAsecId(id)) {
1591 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1592 errno = EINVAL;
1593 return -1;
1594 }
1595
Kenny Root344ca102012-04-03 17:23:01 -07001596 if (asprintf(&asecName, "%s.asec", id) < 0) {
1597 SLOGE("Couldn't allocate string to write ASEC name");
1598 return -1;
1599 }
1600
1601 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001602 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1603 dir = VolumeManager::SEC_ASECDIR_INT;
1604 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1605 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001606 } else {
1607 free(asecName);
1608 return -1;
1609 }
1610
1611 if (directory != NULL) {
1612 *directory = dir;
1613 }
1614
1615 if (asecPath != NULL) {
1616 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001617 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1618 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001619 free(asecName);
1620 return -1;
1621 }
1622 }
1623
1624 free(asecName);
1625 return 0;
1626}
1627
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001628int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001629 char asecFileName[255];
1630 char mountPoint[255];
1631
Nick Kralevich0de7c612014-01-27 14:58:06 -08001632 if (!isLegalAsecId(id)) {
1633 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1634 errno = EINVAL;
1635 return -1;
1636 }
1637
Kenny Root344ca102012-04-03 17:23:01 -07001638 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1639 SLOGE("Couldn't find ASEC %s", id);
1640 return -1;
1641 }
1642
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001643 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001644 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001645 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001646 return -1;
1647 }
San Mehata19b2502010-01-06 10:33:53 -08001648
1649 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001650 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001651 errno = EBUSY;
1652 return -1;
1653 }
1654
San Mehatd9a4e352010-03-12 13:32:47 -08001655 char idHash[33];
1656 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001657 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001658 return -1;
1659 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001660
San Mehata19b2502010-01-06 10:33:53 -08001661 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001662 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1663 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001664
1665 char dmDevice[255];
1666 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001667
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001668 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001669 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001670
Kenny Root344ca102012-04-03 17:23:01 -07001671 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1672 return -1;
1673 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001674
San Mehatd9a4e352010-03-12 13:32:47 -08001675 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001676 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001677 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001678 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001679 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001680 Loop::destroyByDevice(loopDevice);
1681 errno = EMEDIUMTYPE;
1682 return -1;
1683 }
1684 nr_sec--; // We don't want the devmapping to extend onto our superblock
1685
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001686 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1687 Loop::destroyByDevice(loopDevice);
1688 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001689 }
1690
Kenny Root344ca102012-04-03 17:23:01 -07001691 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001692 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001693 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001694 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001695 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001696 }
1697 Loop::destroyByDevice(loopDevice);
1698 return -1;
1699 }
San Mehata19b2502010-01-06 10:33:53 -08001700 }
1701
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001702 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001703 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001704 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001705 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001706
Kenny Root344ca102012-04-03 17:23:01 -07001707 int result;
1708 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001709 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1710 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001711 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001712 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1713 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001714 }
1715
1716 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001717 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001718 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001719 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001720 }
1721 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001722 return -1;
1723 }
1724
Kenny Rootcbacf782010-09-24 15:11:48 -07001725 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001726 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001727 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001728 }
San Mehata19b2502010-01-06 10:33:53 -08001729 return 0;
1730}
1731
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001732/**
1733 * Mounts an image file <code>img</code>.
1734 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001735int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001736 char mountPoint[255];
1737
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001738 char idHash[33];
1739 if (!asecHash(img, idHash, sizeof(idHash))) {
1740 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1741 return -1;
1742 }
1743
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001744 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001745 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001746 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001747 return -1;
1748 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001749
1750 if (isMountpointMounted(mountPoint)) {
1751 SLOGE("Image %s already mounted", img);
1752 errno = EBUSY;
1753 return -1;
1754 }
1755
1756 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001757 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1758 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001759
1760 char dmDevice[255];
1761 bool cleanupDm = false;
1762 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001763 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001764
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001765 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001766 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1767 Loop::destroyByDevice(loopDevice);
1768 return -1;
1769 }
1770
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001771 get_blkdev_size(fd, &nr_sec);
1772 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001773 SLOGE("Failed to get loop size (%s)", strerror(errno));
1774 Loop::destroyByDevice(loopDevice);
1775 close(fd);
1776 return -1;
1777 }
1778
1779 close(fd);
1780
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001781 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001782 Loop::destroyByDevice(loopDevice);
1783 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001784 }
1785
1786 if (mkdir(mountPoint, 0755)) {
1787 if (errno != EEXIST) {
1788 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1789 if (cleanupDm) {
1790 Devmapper::destroy(idHash);
1791 }
1792 Loop::destroyByDevice(loopDevice);
1793 return -1;
1794 }
1795 }
1796
yoshiyuki hama476a6272015-01-28 16:37:23 +09001797 /*
1798 * Wait for the device mapper node to be created.
1799 */
1800 waitForDevMapper(dmDevice);
1801
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001802 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1803 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001804 SLOGE("Image mount failed (%s)", strerror(errno));
1805 if (cleanupDm) {
1806 Devmapper::destroy(idHash);
1807 }
1808 Loop::destroyByDevice(loopDevice);
1809 return -1;
1810 }
1811
Kenny Rootcbacf782010-09-24 15:11:48 -07001812 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001813 if (mDebug) {
1814 SLOGD("Image %s mounted", img);
1815 }
1816 return 0;
1817}
1818
Kenny Root508c0e12010-07-12 09:59:49 -07001819int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001820 FILE *fp = setmntent("/proc/mounts", "r");
1821 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001822 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1823 return -1;
1824 }
1825
1826 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001827 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001828 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001829 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001830 loopDir[loopDirLen++] = '/';
1831 loopDir[loopDirLen] = '\0';
1832
Yabin Cuid1104f72015-01-02 13:28:28 -08001833 mntent* mentry;
1834 while ((mentry = getmntent(fp)) != NULL) {
1835 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001836 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001837 if (fd >= 0) {
1838 struct loop_info64 li;
1839 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1840 cli->sendMsg(ResponseCode::AsecListResult,
1841 (const char*) li.lo_file_name, false);
1842 }
1843 close(fd);
1844 }
1845 }
1846 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001847 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001848 return 0;
1849}
1850
Jeff Sharkey9c484982015-03-31 10:35:33 -07001851extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001852 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001853 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001854}
1855
San Mehata19b2502010-01-06 10:33:53 -08001856bool VolumeManager::isMountpointMounted(const char *mp)
1857{
Yabin Cuid1104f72015-01-02 13:28:28 -08001858 FILE *fp = setmntent("/proc/mounts", "r");
1859 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001860 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001861 return false;
1862 }
1863
Yabin Cuid1104f72015-01-02 13:28:28 -08001864 bool found_mp = false;
1865 mntent* mentry;
1866 while ((mentry = getmntent(fp)) != NULL) {
1867 if (strcmp(mentry->mnt_dir, mp) == 0) {
1868 found_mp = true;
1869 break;
San Mehata19b2502010-01-06 10:33:53 -08001870 }
San Mehata19b2502010-01-06 10:33:53 -08001871 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001872 endmntent(fp);
1873 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001874}
1875
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001876int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001877 // Only offer to create directories for paths managed by vold
1878 if (strncmp(path, "/storage/", 9) == 0) {
1879 // fs_mkdirs() does symlink checking and relative path enforcement
1880 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001881 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001882 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001883 return -EINVAL;
1884 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001885}