blob: 2c3924042f06d0218c367e425e2a378480667e2e [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>
Elliott Hughes0e08e842017-05-18 09:08:24 -070029#include <sys/sysmacros.h>
Jeff Sharkey66270a22015-06-24 11:49:24 -070030#include <sys/wait.h>
Yabin Cuid1104f72015-01-02 13:28:28 -080031#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080032
San Mehata2677e42009-12-13 10:40:18 -080033#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070034
35#define LOG_TAG "Vold"
36
Kenny Root7b18a7b2010-03-15 13:13:41 -070037#include <openssl/md5.h>
38
Elliott Hughes7e128fb2015-12-04 15:50:53 -080039#include <android-base/logging.h>
40#include <android-base/stringprintf.h>
Jeff Sharkey71ebe152013-09-17 17:24:38 -070041#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070042#include <cutils/log.h>
43
Robert Craigb9e3ba52014-02-04 10:53:00 -050044#include <selinux/android.h>
45
San Mehatfd7f5872009-10-12 11:32:47 -070046#include <sysutils/NetlinkEvent.h>
47
Kenny Root344ca102012-04-03 17:23:01 -070048#include <private/android_filesystem_config.h>
49
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070050#include "Benchmark.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051#include "EmulatedVolume.h"
San Mehatf1b736b2009-10-10 17:22:08 -070052#include "VolumeManager.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070053#include "NetlinkManager.h"
San Mehata2677e42009-12-13 10:40:18 -080054#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080055#include "Loop.h"
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070056#include "fs/Ext4.h"
57#include "fs/Vfat.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070058#include "Utils.h"
San Mehatb78a32c2010-01-10 13:02:12 -080059#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080060#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080061#include "Asec.h"
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +090062#include "VoldUtil.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070063#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080064
Mike Lockwood97f2fc12011-06-07 10:51:38 -070065#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
66
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070067#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
68 + ((number) & (~((1U << (po2)) - 1))))
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070069
Jeff Sharkey36801cc2015-03-13 16:09:20 -070070using android::base::StringPrintf;
71
Jeff Sharkey9f18fe72015-04-01 23:32:18 -070072/*
73 * Path to external storage where *only* root can access ASEC image files
74 */
75const char *VolumeManager::SEC_ASECDIR_EXT = "/mnt/secure/asec";
76
77/*
78 * Path to internal storage where *only* root can access ASEC image files
79 */
80const char *VolumeManager::SEC_ASECDIR_INT = "/data/app-asec";
81
82/*
83 * Path to where secure containers are mounted
84 */
85const char *VolumeManager::ASECDIR = "/mnt/asec";
86
87/*
88 * Path to where OBBs are mounted
89 */
90const char *VolumeManager::LOOPDIR = "/mnt/obb";
91
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092static const char* kUserMountPath = "/mnt/user";
93
Jeff Sharkey36801cc2015-03-13 16:09:20 -070094static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080095static const unsigned int kMajorBlockExperimentalMin = 240;
96static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070098/* writes superblock at end of file or device given by name */
99static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700100 int sbfd = open(name, O_RDWR | O_CLOEXEC);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700101 if (sbfd < 0) {
102 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
103 return -1;
104 }
105
106 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
107 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
108 close(sbfd);
109 return -1;
110 }
111
112 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
113 SLOGE("Failed to write superblock (%s)", strerror(errno));
114 close(sbfd);
115 return -1;
116 }
117 close(sbfd);
118 return 0;
119}
120
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200121static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700122 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
123 // preventing costly operations or unexpected ENOSPC error.
124 // Ext4::format() uses default block size without clustering.
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200125 unsigned long clusterSectors = 4096 / 512;
126 unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
Daniel Rosenberge9196fe2014-06-10 17:16:03 -0700127 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700128 return ROUND_UP_POWER_OF_2(numSectors, 3);
129}
130
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200131static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700132 /*
133 * Add some headroom
134 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200135 unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700136 numSectors += fatSize + 2;
137 /*
138 * FAT is aligned to 32 kb with 512b sectors.
139 */
140 return ROUND_UP_POWER_OF_2(numSectors, 6);
141}
142
143static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
144 if (Loop::lookupActive(idHash, buffer, len)) {
145 if (Loop::create(idHash, asecFileName, buffer, len)) {
146 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
147 return -1;
148 }
149 if (debug) {
150 SLOGD("New loop device created at %s", buffer);
151 }
152 } else {
153 if (debug) {
154 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
155 }
156 }
157 return 0;
158}
159
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200160static 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 -0700161 if (strcmp(key, "none")) {
162 if (Devmapper::lookupActive(idHash, buffer, len)) {
163 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
164 buffer, len)) {
165 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
166 return -1;
167 }
168 if (debug) {
169 SLOGD("New devmapper instance created at %s", buffer);
170 }
171 } else {
172 if (debug) {
173 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
174 }
175 }
176 *createdDMDevice = true;
177 } else {
178 strcpy(buffer, loopDevice);
179 *createdDMDevice = false;
180 }
181 return 0;
182}
183
184static void waitForDevMapper(const char *dmDevice) {
185 /*
186 * Wait for the device mapper node to be created. Sometimes it takes a
187 * while. Wait for up to 1 second. We could also inspect incoming uevents,
188 * but that would take more effort.
189 */
190 int tries = 25;
191 while (tries--) {
192 if (!access(dmDevice, F_OK) || errno != ENOENT) {
193 break;
194 }
195 usleep(40 * 1000);
196 }
197}
198
San Mehatf1b736b2009-10-10 17:22:08 -0700199VolumeManager *VolumeManager::sInstance = NULL;
200
201VolumeManager *VolumeManager::Instance() {
202 if (!sInstance)
203 sInstance = new VolumeManager();
204 return sInstance;
205}
206
207VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800208 mDebug = false;
San Mehat88705162010-01-15 09:26:28 -0800209 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700210 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400211 mUmsSharingCount = 0;
212 mSavedDirtyRatio = -1;
213 // set dirty ratio to 0 when UMS is active
214 mUmsDirtyRatio = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700215}
216
217VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800218 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700219}
220
Kenny Root7b18a7b2010-03-15 13:13:41 -0700221char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700222 static const char* digits = "0123456789abcdef";
223
Kenny Root7b18a7b2010-03-15 13:13:41 -0700224 unsigned char sig[MD5_DIGEST_LENGTH];
225
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700226 if (buffer == NULL) {
227 SLOGE("Destination buffer is NULL");
228 errno = ESPIPE;
229 return NULL;
230 } else if (id == NULL) {
231 SLOGE("Source buffer is NULL");
232 errno = ESPIPE;
233 return NULL;
234 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800235 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700236 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800237 errno = ESPIPE;
238 return NULL;
239 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700240
241 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800242
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700243 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700244 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700245 *p++ = digits[sig[i] >> 4];
246 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800247 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700248 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800249
250 return buffer;
251}
252
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700253int VolumeManager::setDebug(bool enable) {
San Mehatd9a4e352010-03-12 13:32:47 -0800254 mDebug = enable;
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700255 return 0;
San Mehatd9a4e352010-03-12 13:32:47 -0800256}
257
San Mehatf1b736b2009-10-10 17:22:08 -0700258int VolumeManager::start() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700259 // Always start from a clean slate by unmounting everything in
260 // directories that we own, in case we crashed.
Jeff Sharkey9c484982015-03-31 10:35:33 -0700261 unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262
263 // Assume that we always have an emulated volume on internal
264 // storage; the framework will decide if it should be mounted.
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700265 CHECK(mInternalEmulated == nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700266 mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700267 new android::vold::EmulatedVolume("/data/media"));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700268 mInternalEmulated->create();
269
San Mehatf1b736b2009-10-10 17:22:08 -0700270 return 0;
271}
272
273int VolumeManager::stop() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700274 CHECK(mInternalEmulated != nullptr);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700275 mInternalEmulated->destroy();
276 mInternalEmulated = nullptr;
San Mehatf1b736b2009-10-10 17:22:08 -0700277 return 0;
278}
279
San Mehatfd7f5872009-10-12 11:32:47 -0700280void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700281 std::lock_guard<std::mutex> lock(mLock);
282
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700283 if (mDebug) {
284 LOG(VERBOSE) << "----------------";
285 LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
286 evt->dump();
287 }
San Mehatf1b736b2009-10-10 17:22:08 -0700288
Mateusz Nowak64403792015-08-03 16:39:19 +0200289 std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
290 std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700291
292 if (devType != "disk") return;
293
294 int major = atoi(evt->findParam("MAJOR"));
295 int minor = atoi(evt->findParam("MINOR"));
296 dev_t device = makedev(major, minor);
297
298 switch (evt->getAction()) {
299 case NetlinkEvent::Action::kAdd: {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700300 for (const auto& source : mDiskSources) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700301 if (source->matches(eventPath)) {
Yu Ning942d4e82016-01-08 17:36:47 +0800302 // For now, assume that MMC and virtio-blk (the latter is
303 // emulator-specific; see Disk.cpp for details) devices are SD,
304 // and that everything else is USB
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700305 int flags = source->getFlags();
Yu Ning942d4e82016-01-08 17:36:47 +0800306 if (major == kMajorBlockMmc
307 || (android::vold::IsRunningInEmulator()
308 && major >= (int) kMajorBlockExperimentalMin
309 && major <= (int) kMajorBlockExperimentalMax)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700310 flags |= android::vold::Disk::Flags::kSd;
311 } else {
312 flags |= android::vold::Disk::Flags::kUsb;
313 }
314
315 auto disk = new android::vold::Disk(eventPath, device,
316 source->getNickname(), flags);
317 disk->create();
318 mDisks.push_back(std::shared_ptr<android::vold::Disk>(disk));
319 break;
320 }
321 }
322 break;
323 }
324 case NetlinkEvent::Action::kChange: {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700325 LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700326 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700327 if (disk->getDevice() == device) {
328 disk->readMetadata();
329 disk->readPartitions();
330 }
331 }
332 break;
333 }
334 case NetlinkEvent::Action::kRemove: {
335 auto i = mDisks.begin();
336 while (i != mDisks.end()) {
337 if ((*i)->getDevice() == device) {
338 (*i)->destroy();
339 i = mDisks.erase(i);
340 } else {
341 ++i;
342 }
343 }
344 break;
345 }
346 default: {
347 LOG(WARNING) << "Unexpected block event action " << (int) evt->getAction();
348 break;
349 }
350 }
351}
352
353void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
Wei Wang6b455c22017-01-20 11:52:33 -0800354 std::lock_guard<std::mutex> lock(mLock);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355 mDiskSources.push_back(diskSource);
356}
357
358std::shared_ptr<android::vold::Disk> VolumeManager::findDisk(const std::string& id) {
359 for (auto disk : mDisks) {
360 if (disk->getId() == id) {
361 return disk;
San Mehatf1b736b2009-10-10 17:22:08 -0700362 }
363 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700364 return nullptr;
365}
San Mehatf1b736b2009-10-10 17:22:08 -0700366
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700367std::shared_ptr<android::vold::VolumeBase> VolumeManager::findVolume(const std::string& id) {
368 if (mInternalEmulated->getId() == id) {
369 return mInternalEmulated;
San Mehatf1b736b2009-10-10 17:22:08 -0700370 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700371 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700372 auto vol = disk->findVolume(id);
373 if (vol != nullptr) {
374 return vol;
375 }
376 }
377 return nullptr;
378}
379
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700380void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
381 std::list<std::string>& list) {
382 list.clear();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700383 for (const auto& disk : mDisks) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700384 disk->listVolumes(type, list);
385 }
386}
387
388nsecs_t VolumeManager::benchmarkPrivate(const std::string& id) {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700389 std::string path;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700390 if (id == "private" || id == "null") {
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700391 path = "/data";
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700392 } else {
393 auto vol = findVolume(id);
394 if (vol != nullptr && vol->getState() == android::vold::VolumeBase::State::kMounted) {
395 path = vol->getPath();
396 }
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700397 }
398
399 if (path.empty()) {
400 LOG(WARNING) << "Failed to find volume for " << id;
401 return -1;
402 }
403
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700404 return android::vold::BenchmarkPrivate(path);
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -0700405}
406
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700407int VolumeManager::forgetPartition(const std::string& partGuid) {
408 std::string normalizedGuid;
409 if (android::vold::NormalizeHex(partGuid, normalizedGuid)) {
410 LOG(WARNING) << "Invalid GUID " << partGuid;
411 return -1;
412 }
413
414 std::string keyPath = android::vold::BuildKeyPath(normalizedGuid);
415 if (unlink(keyPath.c_str()) != 0) {
416 LOG(ERROR) << "Failed to unlink " << keyPath;
417 return -1;
418 }
419
420 return 0;
421}
422
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700423int VolumeManager::linkPrimary(userid_t userId) {
424 std::string source(mPrimary->getPath());
425 if (mPrimary->getType() == android::vold::VolumeBase::Type::kEmulated) {
426 source = StringPrintf("%s/%d", source.c_str(), userId);
Jeff Sharkey32679a82015-07-21 14:22:01 -0700427 fs_prepare_dir(source.c_str(), 0755, AID_ROOT, AID_ROOT);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700428 }
429
430 std::string target(StringPrintf("/mnt/user/%d/primary", userId));
431 if (TEMP_FAILURE_RETRY(unlink(target.c_str()))) {
432 if (errno != ENOENT) {
433 SLOGW("Failed to unlink %s: %s", target.c_str(), strerror(errno));
434 }
435 }
Jeff Sharkey1bfb3752015-04-29 15:22:23 -0700436 LOG(DEBUG) << "Linking " << source << " to " << target;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700437 if (TEMP_FAILURE_RETRY(symlink(source.c_str(), target.c_str()))) {
438 SLOGW("Failed to link %s to %s: %s", source.c_str(), target.c_str(),
439 strerror(errno));
440 return -errno;
441 }
442 return 0;
443}
444
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700445int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
446 mAddedUsers[userId] = userSerialNumber;
447 return 0;
448}
449
450int VolumeManager::onUserRemoved(userid_t userId) {
451 mAddedUsers.erase(userId);
452 return 0;
453}
454
455int VolumeManager::onUserStarted(userid_t userId) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700456 // Note that sometimes the system will spin up processes from Zygote
457 // before actually starting the user, so we're okay if Zygote
458 // already created this directory.
459 std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
460 fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
461
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700462 mStartedUsers.insert(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700463 if (mPrimary) {
464 linkPrimary(userId);
465 }
466 return 0;
467}
468
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700469int VolumeManager::onUserStopped(userid_t userId) {
470 mStartedUsers.erase(userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700471 return 0;
472}
473
474int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
475 mPrimary = vol;
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700476 for (userid_t userId : mStartedUsers) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700477 linkPrimary(userId);
478 }
479 return 0;
480}
481
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700482static int unmount_tree(const char* path) {
483 size_t path_len = strlen(path);
484
485 FILE* fp = setmntent("/proc/mounts", "r");
486 if (fp == NULL) {
487 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
488 return -errno;
489 }
490
491 // Some volumes can be stacked on each other, so force unmount in
492 // reverse order to give us the best chance of success.
493 std::list<std::string> toUnmount;
494 mntent* mentry;
495 while ((mentry = getmntent(fp)) != NULL) {
496 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
497 toUnmount.push_front(std::string(mentry->mnt_dir));
498 }
499 }
500 endmntent(fp);
501
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700502 for (const auto& path : toUnmount) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700503 if (umount2(path.c_str(), MNT_DETACH)) {
504 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
505 }
506 }
507 return 0;
508}
509
Jeff Sharkey66270a22015-06-24 11:49:24 -0700510int VolumeManager::remountUid(uid_t uid, const std::string& mode) {
511 LOG(DEBUG) << "Remounting " << uid << " as mode " << mode;
512
513 DIR* dir;
514 struct dirent* de;
515 char rootName[PATH_MAX];
516 char pidName[PATH_MAX];
517 int pidFd;
518 int nsFd;
519 struct stat sb;
520 pid_t child;
521
522 if (!(dir = opendir("/proc"))) {
523 PLOG(ERROR) << "Failed to opendir";
524 return -1;
525 }
526
527 // Figure out root namespace to compare against below
Daichi Hirono10d34882016-01-29 14:33:51 +0900528 if (android::vold::SaneReadLinkAt(dirfd(dir), "1/ns/mnt", rootName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700529 PLOG(ERROR) << "Failed to readlink";
530 closedir(dir);
531 return -1;
532 }
533
534 // Poke through all running PIDs look for apps running as UID
535 while ((de = readdir(dir))) {
536 pidFd = -1;
537 nsFd = -1;
538
539 pidFd = openat(dirfd(dir), de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
540 if (pidFd < 0) {
541 goto next;
542 }
543 if (fstat(pidFd, &sb) != 0) {
544 PLOG(WARNING) << "Failed to stat " << de->d_name;
545 goto next;
546 }
547 if (sb.st_uid != uid) {
548 goto next;
549 }
550
551 // Matches so far, but refuse to touch if in root namespace
552 LOG(DEBUG) << "Found matching PID " << de->d_name;
Daichi Hirono10d34882016-01-29 14:33:51 +0900553 if (android::vold::SaneReadLinkAt(pidFd, "ns/mnt", pidName, PATH_MAX) == -1) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700554 PLOG(WARNING) << "Failed to read namespace for " << de->d_name;
555 goto next;
556 }
557 if (!strcmp(rootName, pidName)) {
558 LOG(WARNING) << "Skipping due to root namespace";
559 goto next;
560 }
561
562 // We purposefully leave the namespace open across the fork
563 nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
564 if (nsFd < 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700565 PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700566 goto next;
567 }
568
569 if (!(child = fork())) {
570 if (setns(nsFd, CLONE_NEWNS) != 0) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700571 PLOG(ERROR) << "Failed to setns for " << de->d_name;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700572 _exit(1);
573 }
574
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700575 unmount_tree("/storage");
Jeff Sharkey66270a22015-06-24 11:49:24 -0700576
577 std::string storageSource;
578 if (mode == "default") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700579 storageSource = "/mnt/runtime/default";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700580 } else if (mode == "read") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700581 storageSource = "/mnt/runtime/read";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700582 } else if (mode == "write") {
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700583 storageSource = "/mnt/runtime/write";
Jeff Sharkey66270a22015-06-24 11:49:24 -0700584 } else {
585 // Sane default of no storage visible
586 _exit(0);
587 }
588 if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
589 NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700590 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
591 << de->d_name;
592 _exit(1);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700593 }
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700594
595 // Mount user-specific symlink helper into place
596 userid_t user_id = multiuser_get_user_id(uid);
597 std::string userSource(StringPrintf("/mnt/user/%d", user_id));
598 if (TEMP_FAILURE_RETRY(mount(userSource.c_str(), "/storage/self",
599 NULL, MS_BIND, NULL)) == -1) {
600 PLOG(ERROR) << "Failed to mount " << userSource << " for "
601 << de->d_name;
602 _exit(1);
603 }
604
Jeff Sharkey66270a22015-06-24 11:49:24 -0700605 _exit(0);
606 }
607
608 if (child == -1) {
609 PLOG(ERROR) << "Failed to fork";
610 goto next;
611 } else {
612 TEMP_FAILURE_RETRY(waitpid(child, nullptr, 0));
613 }
614
615next:
616 close(nsFd);
617 close(pidFd);
618 }
619 closedir(dir);
620 return 0;
621}
622
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700623int VolumeManager::reset() {
624 // Tear down all existing disks/volumes and start from a blank slate so
625 // newly connected framework hears all events.
626 mInternalEmulated->destroy();
627 mInternalEmulated->create();
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700628 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700629 disk->destroy();
630 disk->create();
631 }
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700632 mAddedUsers.clear();
633 mStartedUsers.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700634 return 0;
635}
636
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700637// Can be called twice (sequentially) during shutdown. should be safe for that.
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700638int VolumeManager::shutdown() {
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700639 if (mInternalEmulated == nullptr) {
640 return 0; // already shutdown
641 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700642 mInternalEmulated->destroy();
Keun-young Parka5bbb5e2017-03-13 18:02:50 -0700643 mInternalEmulated = nullptr;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700644 for (const auto& disk : mDisks) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700645 disk->destroy();
646 }
647 mDisks.clear();
648 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700649}
650
Jeff Sharkey9c484982015-03-31 10:35:33 -0700651int VolumeManager::unmountAll() {
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700652 std::lock_guard<std::mutex> lock(mLock);
653
Jeff Sharkey9c484982015-03-31 10:35:33 -0700654 // First, try gracefully unmounting all known devices
655 if (mInternalEmulated != nullptr) {
656 mInternalEmulated->unmount();
657 }
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700658 for (const auto& disk : mDisks) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700659 disk->unmountAll();
660 }
661
662 // Worst case we might have some stale mounts lurking around, so
663 // force unmount those just to be safe.
664 FILE* fp = setmntent("/proc/mounts", "r");
665 if (fp == NULL) {
666 SLOGE("Error opening /proc/mounts: %s", strerror(errno));
667 return -errno;
668 }
669
670 // Some volumes can be stacked on each other, so force unmount in
671 // reverse order to give us the best chance of success.
672 std::list<std::string> toUnmount;
673 mntent* mentry;
674 while ((mentry = getmntent(fp)) != NULL) {
675 if (strncmp(mentry->mnt_dir, "/mnt/", 5) == 0
676 || strncmp(mentry->mnt_dir, "/storage/", 9) == 0) {
677 toUnmount.push_front(std::string(mentry->mnt_dir));
678 }
679 }
680 endmntent(fp);
681
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700682 for (const auto& path : toUnmount) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700683 SLOGW("Tearing down stale mount %s", path.c_str());
684 android::vold::ForceUnmount(path);
685 }
686
687 return 0;
688}
689
Kenny Root508c0e12010-07-12 09:59:49 -0700690int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
691 char idHash[33];
692 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
693 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
694 return -1;
695 }
696
697 memset(mountPath, 0, mountPathLen);
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700698 int written = snprintf(mountPath, mountPathLen, "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -0400699 if ((written < 0) || (written >= mountPathLen)) {
700 errno = EINVAL;
701 return -1;
702 }
Kenny Root508c0e12010-07-12 09:59:49 -0700703
704 if (access(mountPath, F_OK)) {
705 errno = ENOENT;
706 return -1;
707 }
708
709 return 0;
710}
711
San Mehata19b2502010-01-06 10:33:53 -0800712int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700713 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700714
Nick Kralevich0de7c612014-01-27 14:58:06 -0800715 if (!isLegalAsecId(id)) {
716 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
717 errno = EINVAL;
718 return -1;
719 }
720
Kenny Root344ca102012-04-03 17:23:01 -0700721 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
722 SLOGE("Couldn't find ASEC %s", id);
723 return -1;
724 }
San Mehat88ac2c02010-03-23 11:15:58 -0700725
726 memset(buffer, 0, maxlen);
727 if (access(asecFileName, F_OK)) {
728 errno = ENOENT;
729 return -1;
730 }
San Mehata19b2502010-01-06 10:33:53 -0800731
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700732 int written = snprintf(buffer, maxlen, "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400733 if ((written < 0) || (written >= maxlen)) {
734 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
735 errno = EINVAL;
736 return -1;
737 }
738
San Mehata19b2502010-01-06 10:33:53 -0800739 return 0;
740}
741
Dianne Hackborn736910c2011-06-27 13:37:07 -0700742int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
743 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700744
Nick Kralevich0de7c612014-01-27 14:58:06 -0800745 if (!isLegalAsecId(id)) {
746 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
747 errno = EINVAL;
748 return -1;
749 }
750
Kenny Root344ca102012-04-03 17:23:01 -0700751 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
752 SLOGE("Couldn't find ASEC %s", id);
753 return -1;
754 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700755
756 memset(buffer, 0, maxlen);
757 if (access(asecFileName, F_OK)) {
758 errno = ENOENT;
759 return -1;
760 }
761
rpcraigd1c226f2012-10-09 06:58:16 -0400762 int written = snprintf(buffer, maxlen, "%s", asecFileName);
763 if ((written < 0) || (written >= maxlen)) {
764 errno = EINVAL;
765 return -1;
766 }
767
Dianne Hackborn736910c2011-06-27 13:37:07 -0700768 return 0;
769}
770
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200771int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
Kenny Root344ca102012-04-03 17:23:01 -0700772 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800773 struct asec_superblock sb;
774 memset(&sb, 0, sizeof(sb));
775
Nick Kralevich0de7c612014-01-27 14:58:06 -0800776 if (!isLegalAsecId(id)) {
777 SLOGE("createAsec: Invalid asec id \"%s\"", id);
778 errno = EINVAL;
779 return -1;
780 }
781
Kenny Root344ca102012-04-03 17:23:01 -0700782 const bool wantFilesystem = strcmp(fstype, "none");
783 bool usingExt4 = false;
784 if (wantFilesystem) {
785 usingExt4 = !strcmp(fstype, "ext4");
786 if (usingExt4) {
787 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
788 } else if (strcmp(fstype, "fat")) {
789 SLOGE("Invalid filesystem type %s", fstype);
790 errno = EINVAL;
791 return -1;
792 }
793 }
794
San Mehatfcf24fe2010-03-03 12:37:32 -0800795 sb.magic = ASEC_SB_MAGIC;
796 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800797
San Mehatd31e3802010-02-18 08:37:45 -0800798 if (numSectors < ((1024*1024)/512)) {
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200799 SLOGE("Invalid container size specified (%lu sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800800 errno = EINVAL;
801 return -1;
802 }
803
San Mehata19b2502010-01-06 10:33:53 -0800804 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700805
806 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
807 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
808 asecFileName, strerror(errno));
809 errno = EADDRINUSE;
810 return -1;
811 }
812
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700813 const char *asecDir = isExternal ? VolumeManager::SEC_ASECDIR_EXT : VolumeManager::SEC_ASECDIR_INT;
Kenny Root344ca102012-04-03 17:23:01 -0700814
rpcraigd1c226f2012-10-09 06:58:16 -0400815 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
816 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
817 errno = EINVAL;
818 return -1;
819 }
San Mehata19b2502010-01-06 10:33:53 -0800820
821 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700822 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700823 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800824 errno = EADDRINUSE;
825 return -1;
826 }
827
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200828 unsigned long numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700829 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700830 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700831 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700832 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800833
834 // Add +1 for our superblock which is at the end
835 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700836 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800837 return -1;
838 }
839
San Mehatd9a4e352010-03-12 13:32:47 -0800840 char idHash[33];
841 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700842 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800843 unlink(asecFileName);
844 return -1;
845 }
846
San Mehata19b2502010-01-06 10:33:53 -0800847 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800848 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700849 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800850 unlink(asecFileName);
851 return -1;
852 }
853
San Mehatb78a32c2010-01-10 13:02:12 -0800854 char dmDevice[255];
855 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800856
San Mehatb78a32c2010-01-10 13:02:12 -0800857 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800858 // XXX: This is all we support for now
859 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800860 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800861 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700862 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800863 Loop::destroyByDevice(loopDevice);
864 unlink(asecFileName);
865 return -1;
866 }
867 cleanupDm = true;
868 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800869 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800870 strcpy(dmDevice, loopDevice);
871 }
872
San Mehatfcf24fe2010-03-03 12:37:32 -0800873 /*
874 * Drop down the superblock at the end of the file
875 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700876 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800877 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800878 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800879 }
880 Loop::destroyByDevice(loopDevice);
881 unlink(asecFileName);
882 return -1;
883 }
884
Kenny Root344ca102012-04-03 17:23:01 -0700885 if (wantFilesystem) {
886 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400887 char mountPoint[255];
888
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700889 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -0400890 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
891 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
892 if (cleanupDm) {
893 Devmapper::destroy(idHash);
894 }
895 Loop::destroyByDevice(loopDevice);
896 unlink(asecFileName);
897 return -1;
898 }
rpcraiga54e13a2012-09-21 14:17:08 -0400899
Kenny Root344ca102012-04-03 17:23:01 -0700900 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700901 formatStatus = android::vold::ext4::Format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700902 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700903 formatStatus = android::vold::vfat::Format(dmDevice, numImgSectors);
San Mehatb78a32c2010-01-10 13:02:12 -0800904 }
San Mehata19b2502010-01-06 10:33:53 -0800905
Kenny Root344ca102012-04-03 17:23:01 -0700906 if (formatStatus < 0) {
907 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800908 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800909 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800910 }
San Mehateb13a902010-01-07 12:12:50 -0800911 Loop::destroyByDevice(loopDevice);
912 unlink(asecFileName);
913 return -1;
914 }
Kenny Root344ca102012-04-03 17:23:01 -0700915
Kenny Root344ca102012-04-03 17:23:01 -0700916 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800917 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700918 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800919 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800920 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800921 }
922 Loop::destroyByDevice(loopDevice);
923 unlink(asecFileName);
924 return -1;
925 }
San Mehatb78a32c2010-01-10 13:02:12 -0800926 }
San Mehata1091cb2010-02-28 20:17:20 -0800927
Kenny Root344ca102012-04-03 17:23:01 -0700928 int mountStatus;
929 if (usingExt4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700930 mountStatus = android::vold::ext4::Mount(dmDevice, mountPoint,
931 false, false, false);
Kenny Root344ca102012-04-03 17:23:01 -0700932 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700933 mountStatus = android::vold::vfat::Mount(dmDevice, mountPoint,
934 false, false, false, ownerUid, 0, 0000, false);
Kenny Root344ca102012-04-03 17:23:01 -0700935 }
936
937 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700938 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800939 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800940 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800941 }
942 Loop::destroyByDevice(loopDevice);
943 unlink(asecFileName);
944 return -1;
945 }
Kenny Root344ca102012-04-03 17:23:01 -0700946
947 if (usingExt4) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700948 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -0700949 if (dirfd >= 0) {
950 if (fchown(dirfd, ownerUid, AID_SYSTEM)
951 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
952 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
953 }
954 close(dirfd);
955 }
956 }
San Mehata1091cb2010-02-28 20:17:20 -0800957 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700958 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800959 }
San Mehat88705162010-01-15 09:26:28 -0800960
Kenny Rootcbacf782010-09-24 15:11:48 -0700961 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800962 return 0;
963}
964
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200965int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700966 char asecFileName[255];
967 char mountPoint[255];
968 bool cleanupDm = false;
969
970 if (!isLegalAsecId(id)) {
971 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
972 errno = EINVAL;
973 return -1;
974 }
975
976 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
977 SLOGE("Couldn't find ASEC %s", id);
978 return -1;
979 }
980
Jeff Sharkey9f18fe72015-04-01 23:32:18 -0700981 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700982 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
983 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
984 return -1;
985 }
986
987 if (isMountpointMounted(mountPoint)) {
988 SLOGE("ASEC %s mounted. Unmount before resizing", id);
989 errno = EBUSY;
990 return -1;
991 }
992
993 struct asec_superblock sb;
994 int fd;
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200995 unsigned long oldNumSec = 0;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700996
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700997 if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700998 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
999 return -1;
1000 }
1001
1002 struct stat info;
1003 if (fstat(fd, &info) < 0) {
1004 SLOGE("Failed to get file size (%s)", strerror(errno));
1005 close(fd);
1006 return -1;
1007 }
1008
1009 oldNumSec = info.st_size / 512;
1010
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001011 unsigned long numImgSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001012 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
1013 numImgSectors = adjustSectorNumExt4(numSectors);
1014 else
1015 numImgSectors = adjustSectorNumFAT(numSectors);
1016 /*
1017 * add one block for the superblock
1018 */
Mateusz Nowaka4f48d02015-08-03 18:06:39 +02001019 SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001020 if (oldNumSec == numImgSectors + 1) {
1021 SLOGW("Size unchanged; ignoring resize request");
1022 return 0;
1023 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001024 SLOGE("Only growing is currently supported.");
1025 close(fd);
1026 return -1;
1027 }
1028
1029 /*
1030 * Try to read superblock.
1031 */
1032 memset(&sb, 0, sizeof(struct asec_superblock));
1033 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
1034 SLOGE("lseek failed (%s)", strerror(errno));
1035 close(fd);
1036 return -1;
1037 }
1038 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
1039 SLOGE("superblock read failed (%s)", strerror(errno));
1040 close(fd);
1041 return -1;
1042 }
1043 close(fd);
1044
1045 if (mDebug) {
1046 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
1047 }
1048 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
1049 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
1050 errno = EMEDIUMTYPE;
1051 return -1;
1052 }
1053
1054 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
1055 SLOGE("Only ext4 partitions are supported for resize");
1056 errno = EINVAL;
1057 return -1;
1058 }
1059
1060 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
1061 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
1062 return -1;
1063 }
1064
1065 /*
1066 * Drop down a copy of the superblock at the end of the file
1067 */
1068 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
1069 goto fail;
1070
1071 char idHash[33];
1072 if (!asecHash(id, idHash, sizeof(idHash))) {
1073 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1074 goto fail;
1075 }
1076
1077 char loopDevice[255];
1078 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1079 goto fail;
1080
1081 char dmDevice[255];
1082
1083 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
1084 Loop::destroyByDevice(loopDevice);
1085 goto fail;
1086 }
1087
1088 /*
1089 * Wait for the device mapper node to be created.
1090 */
1091 waitForDevMapper(dmDevice);
1092
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001093 if (android::vold::ext4::Resize(dmDevice, numImgSectors)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001094 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
1095 if (cleanupDm) {
1096 Devmapper::destroy(idHash);
1097 }
1098 Loop::destroyByDevice(loopDevice);
1099 goto fail;
1100 }
1101
1102 return 0;
1103fail:
1104 Loop::resizeImageFile(asecFileName, oldNumSec);
1105 return -1;
1106}
1107
San Mehata19b2502010-01-06 10:33:53 -08001108int VolumeManager::finalizeAsec(const char *id) {
1109 char asecFileName[255];
1110 char loopDevice[255];
1111 char mountPoint[255];
1112
Nick Kralevich0de7c612014-01-27 14:58:06 -08001113 if (!isLegalAsecId(id)) {
1114 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
1115 errno = EINVAL;
1116 return -1;
1117 }
1118
Kenny Root344ca102012-04-03 17:23:01 -07001119 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1120 SLOGE("Couldn't find ASEC %s", id);
1121 return -1;
1122 }
San Mehata19b2502010-01-06 10:33:53 -08001123
San Mehatd9a4e352010-03-12 13:32:47 -08001124 char idHash[33];
1125 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001126 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001127 return -1;
1128 }
1129
1130 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001131 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001132 return -1;
1133 }
1134
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001135 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001136 struct asec_superblock sb;
1137
1138 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1139 return -1;
1140 }
1141
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001142 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001143 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1144 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
1145 return -1;
1146 }
Kenny Root344ca102012-04-03 17:23:01 -07001147
1148 int result = 0;
1149 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001150 result = android::vold::ext4::Mount(loopDevice, mountPoint,
1151 true, true, true);
Kenny Root344ca102012-04-03 17:23:01 -07001152 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001153 result = android::vold::vfat::Mount(loopDevice, mountPoint,
1154 true, true, true, 0, 0, 0227, false);
Kenny Root344ca102012-04-03 17:23:01 -07001155 }
1156
1157 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001158 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001159 return -1;
1160 }
1161
San Mehatd9a4e352010-03-12 13:32:47 -08001162 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001163 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001164 }
San Mehata19b2502010-01-06 10:33:53 -08001165 return 0;
1166}
1167
Kenny Root344ca102012-04-03 17:23:01 -07001168int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
1169 char asecFileName[255];
1170 char loopDevice[255];
1171 char mountPoint[255];
1172
1173 if (gid < AID_APP) {
1174 SLOGE("Group ID is not in application range");
1175 return -1;
1176 }
1177
Nick Kralevich0de7c612014-01-27 14:58:06 -08001178 if (!isLegalAsecId(id)) {
1179 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
1180 errno = EINVAL;
1181 return -1;
1182 }
1183
Kenny Root344ca102012-04-03 17:23:01 -07001184 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1185 SLOGE("Couldn't find ASEC %s", id);
1186 return -1;
1187 }
1188
1189 char idHash[33];
1190 if (!asecHash(id, idHash, sizeof(idHash))) {
1191 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
1192 return -1;
1193 }
1194
1195 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
1196 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
1197 return -1;
1198 }
1199
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001200 unsigned long nr_sec = 0;
Kenny Root344ca102012-04-03 17:23:01 -07001201 struct asec_superblock sb;
1202
1203 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1204 return -1;
1205 }
1206
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001207 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001208 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1209 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
1210 return -1;
1211 }
Kenny Root344ca102012-04-03 17:23:01 -07001212
1213 int result = 0;
1214 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
1215 return 0;
1216 }
1217
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001218 int ret = android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001219 false /* read-only */,
1220 true /* remount */,
1221 false /* executable */);
1222 if (ret) {
1223 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
1224 return -1;
1225 }
1226
1227 char *paths[] = { mountPoint, NULL };
1228
1229 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
1230 if (fts) {
1231 // Traverse the entire hierarchy and chown to system UID.
1232 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
1233 // We don't care about the lost+found directory.
1234 if (!strcmp(ftsent->fts_name, "lost+found")) {
1235 continue;
1236 }
1237
1238 /*
1239 * There can only be one file marked as private right now.
1240 * This should be more robust, but it satisfies the requirements
1241 * we have for right now.
1242 */
1243 const bool privateFile = !strcmp(ftsent->fts_name, filename);
1244
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001245 int fd = open(ftsent->fts_accpath, O_NOFOLLOW | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001246 if (fd < 0) {
1247 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
1248 result = -1;
1249 continue;
1250 }
1251
1252 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
1253
1254 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -07001255 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -07001256 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -07001257 result |= fchmod(fd, privateFile ? 0640 : 0644);
1258 }
Robert Craigb9e3ba52014-02-04 10:53:00 -05001259
Stephen Smalley5093e612014-02-12 09:43:08 -05001260 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -05001261 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
1262 result |= -1;
1263 }
1264
Kenny Root344ca102012-04-03 17:23:01 -07001265 close(fd);
1266 }
1267 fts_close(fts);
1268
1269 // Finally make the directory readable by everyone.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001270 int dirfd = open(mountPoint, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001271 if (dirfd < 0 || fchmod(dirfd, 0755)) {
1272 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
1273 result |= -1;
1274 }
1275 close(dirfd);
1276 } else {
1277 result |= -1;
1278 }
1279
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001280 result |= android::vold::ext4::Mount(loopDevice, mountPoint,
Kenny Root344ca102012-04-03 17:23:01 -07001281 true /* read-only */,
1282 true /* remount */,
1283 true /* execute */);
1284
1285 if (result) {
1286 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
1287 return -1;
1288 }
1289
1290 if (mDebug) {
1291 SLOGD("ASEC %s permissions fixed", id);
1292 }
1293 return 0;
1294}
1295
San Mehat048b0802010-01-23 08:17:06 -08001296int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -07001297 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -08001298 char *asecFilename2;
1299 char mountPoint[255];
1300
Kenny Root344ca102012-04-03 17:23:01 -07001301 const char *dir;
1302
Nick Kralevich0de7c612014-01-27 14:58:06 -08001303 if (!isLegalAsecId(id1)) {
1304 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
1305 errno = EINVAL;
1306 return -1;
1307 }
1308
1309 if (!isLegalAsecId(id2)) {
1310 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
1311 errno = EINVAL;
1312 return -1;
1313 }
1314
Kenny Root344ca102012-04-03 17:23:01 -07001315 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
1316 SLOGE("Couldn't find ASEC %s", id1);
1317 return -1;
1318 }
1319
1320 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -08001321
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001322 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id1);
rpcraigd1c226f2012-10-09 06:58:16 -04001323 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1324 SLOGE("Rename failed: couldn't construct mountpoint");
1325 goto out_err;
1326 }
1327
San Mehat048b0802010-01-23 08:17:06 -08001328 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001329 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -08001330 errno = EBUSY;
1331 goto out_err;
1332 }
1333
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001334 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id2);
rpcraigd1c226f2012-10-09 06:58:16 -04001335 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1336 SLOGE("Rename failed: couldn't construct mountpoint2");
1337 goto out_err;
1338 }
1339
San Mehat96956ed2010-02-24 08:42:51 -08001340 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001341 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -08001342 errno = EBUSY;
1343 goto out_err;
1344 }
1345
San Mehat048b0802010-01-23 08:17:06 -08001346 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001347 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -08001348 errno = EADDRINUSE;
1349 goto out_err;
1350 }
1351
1352 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001353 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -08001354 goto out_err;
1355 }
1356
San Mehat048b0802010-01-23 08:17:06 -08001357 free(asecFilename2);
1358 return 0;
1359
1360out_err:
San Mehat048b0802010-01-23 08:17:06 -08001361 free(asecFilename2);
1362 return -1;
1363}
1364
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001365#define UNMOUNT_RETRIES 5
1366#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -08001367int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -08001368 char asecFileName[255];
1369 char mountPoint[255];
1370
Nick Kralevich0de7c612014-01-27 14:58:06 -08001371 if (!isLegalAsecId(id)) {
1372 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
1373 errno = EINVAL;
1374 return -1;
1375 }
1376
Kenny Root344ca102012-04-03 17:23:01 -07001377 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1378 SLOGE("Couldn't find ASEC %s", id);
1379 return -1;
1380 }
1381
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001382 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001383 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1384 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1385 return -1;
1386 }
San Mehata19b2502010-01-06 10:33:53 -08001387
San Mehatd9a4e352010-03-12 13:32:47 -08001388 char idHash[33];
1389 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001390 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001391 return -1;
1392 }
1393
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001394 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1395}
1396
Kenny Root508c0e12010-07-12 09:59:49 -07001397int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001398 char mountPoint[255];
1399
1400 char idHash[33];
1401 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1402 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1403 return -1;
1404 }
1405
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001406 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001407 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1408 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1409 return -1;
1410 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001411
1412 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1413}
1414
1415int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1416 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001417 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001418 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001419 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001420 return -1;
1421 }
San Mehat23969932010-01-09 07:08:06 -08001422
San Mehatb78a32c2010-01-10 13:02:12 -08001423 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001424 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001425 rc = umount(mountPoint);
1426 if (!rc) {
1427 break;
San Mehata19b2502010-01-06 10:33:53 -08001428 }
San Mehatb78a32c2010-01-10 13:02:12 -08001429 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001430 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001431 rc = 0;
1432 break;
San Mehata19b2502010-01-06 10:33:53 -08001433 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001434 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001435 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001436
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001437 int signal = 0; // default is to just complain
San Mehat4ba89482010-02-18 09:00:18 -08001438
1439 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001440 if (i > (UNMOUNT_RETRIES - 2))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001441 signal = SIGKILL;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001442 else if (i > (UNMOUNT_RETRIES - 3))
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001443 signal = SIGTERM;
San Mehat4ba89482010-02-18 09:00:18 -08001444 }
San Mehat8c940ef2010-02-13 14:19:53 -08001445
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001446 Process::killProcessesWithOpenFiles(mountPoint, signal);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001447 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001448 }
1449
1450 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001451 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001452 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001453 return -1;
1454 }
1455
San Mehat12f4b892010-02-24 11:43:22 -08001456 int retries = 10;
1457
1458 while(retries--) {
1459 if (!rmdir(mountPoint)) {
1460 break;
1461 }
1462
San Mehat97ac40e2010-03-24 10:24:19 -07001463 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001464 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001465 }
1466
1467 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001468 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001469 }
San Mehat88705162010-01-15 09:26:28 -08001470
Paul Lawrence60dec162014-09-02 10:52:15 -07001471 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1472 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1473 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1474 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1475 continue;
1476 } else {
1477 break;
1478 }
San Mehata19b2502010-01-06 10:33:53 -08001479 }
1480
1481 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001482 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001483 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001484 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001485 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001486 }
San Mehat88705162010-01-15 09:26:28 -08001487
1488 AsecIdCollection::iterator it;
1489 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001490 ContainerData* cd = *it;
1491 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001492 free(*it);
1493 mActiveContainers->erase(it);
1494 break;
1495 }
1496 }
1497 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001498 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001499 }
San Mehatb78a32c2010-01-10 13:02:12 -08001500 return 0;
1501}
1502
San Mehat4ba89482010-02-18 09:00:18 -08001503int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001504 char asecFileName[255];
1505 char mountPoint[255];
1506
Nick Kralevich0de7c612014-01-27 14:58:06 -08001507 if (!isLegalAsecId(id)) {
1508 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1509 errno = EINVAL;
1510 return -1;
1511 }
1512
Kenny Root344ca102012-04-03 17:23:01 -07001513 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1514 SLOGE("Couldn't find ASEC %s", id);
1515 return -1;
1516 }
1517
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001518 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001519 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1520 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1521 return -1;
1522 }
San Mehatb78a32c2010-01-10 13:02:12 -08001523
San Mehat0586d542010-01-12 15:38:59 -08001524 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001525 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001526 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001527 }
San Mehat4ba89482010-02-18 09:00:18 -08001528 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001529 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001530 return -1;
1531 }
1532 }
San Mehata19b2502010-01-06 10:33:53 -08001533
San Mehat0586d542010-01-12 15:38:59 -08001534 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001535 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001536 return -1;
1537 }
San Mehata19b2502010-01-06 10:33:53 -08001538
San Mehatd9a4e352010-03-12 13:32:47 -08001539 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001540 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001541 }
San Mehata19b2502010-01-06 10:33:53 -08001542 return 0;
1543}
1544
Nick Kralevich0de7c612014-01-27 14:58:06 -08001545/*
1546 * Legal ASEC ids consist of alphanumeric characters, '-',
1547 * '_', or '.'. ".." is not allowed. The first or last character
1548 * of the ASEC id cannot be '.' (dot).
1549 */
1550bool VolumeManager::isLegalAsecId(const char *id) const {
1551 size_t i;
1552 size_t len = strlen(id);
1553
1554 if (len == 0) {
1555 return false;
1556 }
1557 if ((id[0] == '.') || (id[len - 1] == '.')) {
1558 return false;
1559 }
1560
1561 for (i = 0; i < len; i++) {
1562 if (id[i] == '.') {
1563 // i=0 is guaranteed never to have a dot. See above.
1564 if (id[i-1] == '.') return false;
1565 continue;
1566 }
1567 if (id[i] == '_' || id[i] == '-') continue;
1568 if (id[i] >= 'a' && id[i] <= 'z') continue;
1569 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1570 if (id[i] >= '0' && id[i] <= '9') continue;
1571 return false;
1572 }
1573
1574 return true;
1575}
1576
Kenny Root344ca102012-04-03 17:23:01 -07001577bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001578 int dirfd = open(dir, O_DIRECTORY | O_CLOEXEC);
Kenny Root344ca102012-04-03 17:23:01 -07001579 if (dirfd < 0) {
1580 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
Nick Kralevich25e581a2015-02-06 08:55:08 -08001581 return false;
Kenny Root344ca102012-04-03 17:23:01 -07001582 }
1583
Nick Kralevich25e581a2015-02-06 08:55:08 -08001584 struct stat sb;
1585 bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)
1586 && S_ISREG(sb.st_mode);
Kenny Root344ca102012-04-03 17:23:01 -07001587
1588 close(dirfd);
1589
1590 return ret;
1591}
1592
1593int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1594 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001595 char *asecName;
1596
Nick Kralevich0de7c612014-01-27 14:58:06 -08001597 if (!isLegalAsecId(id)) {
1598 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1599 errno = EINVAL;
1600 return -1;
1601 }
1602
Kenny Root344ca102012-04-03 17:23:01 -07001603 if (asprintf(&asecName, "%s.asec", id) < 0) {
1604 SLOGE("Couldn't allocate string to write ASEC name");
1605 return -1;
1606 }
1607
1608 const char *dir;
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001609 if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_INT, asecName)) {
1610 dir = VolumeManager::SEC_ASECDIR_INT;
1611 } else if (isAsecInDirectory(VolumeManager::SEC_ASECDIR_EXT, asecName)) {
1612 dir = VolumeManager::SEC_ASECDIR_EXT;
Kenny Root344ca102012-04-03 17:23:01 -07001613 } else {
1614 free(asecName);
1615 return -1;
1616 }
1617
1618 if (directory != NULL) {
1619 *directory = dir;
1620 }
1621
1622 if (asecPath != NULL) {
1623 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001624 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1625 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001626 free(asecName);
1627 return -1;
1628 }
1629 }
1630
1631 free(asecName);
1632 return 0;
1633}
1634
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001635int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001636 char asecFileName[255];
1637 char mountPoint[255];
1638
Nick Kralevich0de7c612014-01-27 14:58:06 -08001639 if (!isLegalAsecId(id)) {
1640 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1641 errno = EINVAL;
1642 return -1;
1643 }
1644
Kenny Root344ca102012-04-03 17:23:01 -07001645 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1646 SLOGE("Couldn't find ASEC %s", id);
1647 return -1;
1648 }
1649
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001650 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::ASECDIR, id);
rpcraigd1c226f2012-10-09 06:58:16 -04001651 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001652 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001653 return -1;
1654 }
San Mehata19b2502010-01-06 10:33:53 -08001655
1656 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001657 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001658 errno = EBUSY;
1659 return -1;
1660 }
1661
San Mehatd9a4e352010-03-12 13:32:47 -08001662 char idHash[33];
1663 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001664 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001665 return -1;
1666 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001667
San Mehata19b2502010-01-06 10:33:53 -08001668 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001669 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1670 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001671
1672 char dmDevice[255];
1673 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001674
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001675 unsigned long nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001676 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001677
Kenny Root344ca102012-04-03 17:23:01 -07001678 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1679 return -1;
1680 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001681
San Mehatd9a4e352010-03-12 13:32:47 -08001682 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001683 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001684 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001685 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001686 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001687 Loop::destroyByDevice(loopDevice);
1688 errno = EMEDIUMTYPE;
1689 return -1;
1690 }
1691 nr_sec--; // We don't want the devmapping to extend onto our superblock
1692
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001693 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1694 Loop::destroyByDevice(loopDevice);
1695 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001696 }
1697
Kenny Root344ca102012-04-03 17:23:01 -07001698 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001699 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001700 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001701 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001702 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001703 }
1704 Loop::destroyByDevice(loopDevice);
1705 return -1;
1706 }
San Mehata19b2502010-01-06 10:33:53 -08001707 }
1708
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001709 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001710 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001711 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001712 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001713
Kenny Root344ca102012-04-03 17:23:01 -07001714 int result;
1715 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001716 result = android::vold::ext4::Mount(dmDevice, mountPoint,
1717 readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001718 } else {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001719 result = android::vold::vfat::Mount(dmDevice, mountPoint,
1720 readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001721 }
1722
1723 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001724 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001725 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001726 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001727 }
1728 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001729 return -1;
1730 }
1731
Kenny Rootcbacf782010-09-24 15:11:48 -07001732 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001733 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001734 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001735 }
San Mehata19b2502010-01-06 10:33:53 -08001736 return 0;
1737}
1738
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001739/**
1740 * Mounts an image file <code>img</code>.
1741 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001742int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001743 char mountPoint[255];
1744
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001745 char idHash[33];
1746 if (!asecHash(img, idHash, sizeof(idHash))) {
1747 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1748 return -1;
1749 }
1750
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001751 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", VolumeManager::LOOPDIR, idHash);
rpcraigd1c226f2012-10-09 06:58:16 -04001752 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001753 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001754 return -1;
1755 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001756
1757 if (isMountpointMounted(mountPoint)) {
1758 SLOGE("Image %s already mounted", img);
1759 errno = EBUSY;
1760 return -1;
1761 }
1762
1763 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001764 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1765 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001766
1767 char dmDevice[255];
1768 bool cleanupDm = false;
1769 int fd;
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001770 unsigned long nr_sec = 0;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001771
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001772 if ((fd = open(loopDevice, O_RDWR | O_CLOEXEC)) < 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001773 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1774 Loop::destroyByDevice(loopDevice);
1775 return -1;
1776 }
1777
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001778 get_blkdev_size(fd, &nr_sec);
1779 if (nr_sec == 0) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001780 SLOGE("Failed to get loop size (%s)", strerror(errno));
1781 Loop::destroyByDevice(loopDevice);
1782 close(fd);
1783 return -1;
1784 }
1785
1786 close(fd);
1787
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001788 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash, nr_sec, &cleanupDm, mDebug)) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001789 Loop::destroyByDevice(loopDevice);
1790 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001791 }
1792
1793 if (mkdir(mountPoint, 0755)) {
1794 if (errno != EEXIST) {
1795 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1796 if (cleanupDm) {
1797 Devmapper::destroy(idHash);
1798 }
1799 Loop::destroyByDevice(loopDevice);
1800 return -1;
1801 }
1802 }
1803
yoshiyuki hama476a6272015-01-28 16:37:23 +09001804 /*
1805 * Wait for the device mapper node to be created.
1806 */
1807 waitForDevMapper(dmDevice);
1808
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001809 if (android::vold::vfat::Mount(dmDevice, mountPoint,
1810 true, false, true, 0, ownerGid, 0227, false)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001811 SLOGE("Image mount failed (%s)", strerror(errno));
1812 if (cleanupDm) {
1813 Devmapper::destroy(idHash);
1814 }
1815 Loop::destroyByDevice(loopDevice);
1816 return -1;
1817 }
1818
Kenny Rootcbacf782010-09-24 15:11:48 -07001819 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001820 if (mDebug) {
1821 SLOGD("Image %s mounted", img);
1822 }
1823 return 0;
1824}
1825
Kenny Root508c0e12010-07-12 09:59:49 -07001826int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001827 FILE *fp = setmntent("/proc/mounts", "r");
1828 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001829 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1830 return -1;
1831 }
1832
1833 // Create a string to compare against that has a trailing slash
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001834 int loopDirLen = strlen(VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001835 char loopDir[loopDirLen + 2];
Jeff Sharkey9f18fe72015-04-01 23:32:18 -07001836 strcpy(loopDir, VolumeManager::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001837 loopDir[loopDirLen++] = '/';
1838 loopDir[loopDirLen] = '\0';
1839
Yabin Cuid1104f72015-01-02 13:28:28 -08001840 mntent* mentry;
1841 while ((mentry = getmntent(fp)) != NULL) {
1842 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001843 int fd = open(mentry->mnt_fsname, O_RDONLY | O_CLOEXEC);
Kenny Root508c0e12010-07-12 09:59:49 -07001844 if (fd >= 0) {
1845 struct loop_info64 li;
1846 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1847 cli->sendMsg(ResponseCode::AsecListResult,
1848 (const char*) li.lo_file_name, false);
1849 }
1850 close(fd);
1851 }
1852 }
1853 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001854 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001855 return 0;
1856}
1857
Jeff Sharkey9c484982015-03-31 10:35:33 -07001858extern "C" int vold_unmountAll(void) {
Ken Sumrall425524d2012-06-14 20:55:28 -07001859 VolumeManager *vm = VolumeManager::Instance();
Jeff Sharkey9c484982015-03-31 10:35:33 -07001860 return vm->unmountAll();
Ken Sumrall425524d2012-06-14 20:55:28 -07001861}
1862
San Mehata19b2502010-01-06 10:33:53 -08001863bool VolumeManager::isMountpointMounted(const char *mp)
1864{
Yabin Cuid1104f72015-01-02 13:28:28 -08001865 FILE *fp = setmntent("/proc/mounts", "r");
1866 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001867 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001868 return false;
1869 }
1870
Yabin Cuid1104f72015-01-02 13:28:28 -08001871 bool found_mp = false;
1872 mntent* mentry;
1873 while ((mentry = getmntent(fp)) != NULL) {
1874 if (strcmp(mentry->mnt_dir, mp) == 0) {
1875 found_mp = true;
1876 break;
San Mehata19b2502010-01-06 10:33:53 -08001877 }
San Mehata19b2502010-01-06 10:33:53 -08001878 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001879 endmntent(fp);
1880 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001881}
1882
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001883int VolumeManager::mkdirs(char* path) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001884 // Only offer to create directories for paths managed by vold
1885 if (strncmp(path, "/storage/", 9) == 0) {
1886 // fs_mkdirs() does symlink checking and relative path enforcement
1887 return fs_mkdirs(path, 0700);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001888 } else {
Cylen Yao27cfee32014-05-02 19:23:42 +08001889 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001890 return -EINVAL;
1891 }
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001892}