blob: a41e583075f7870e23c450be81a89567c22c86da [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>
Yabin Cuid1104f72015-01-02 13:28:28 -080029#include <unistd.h>
San Mehata19b2502010-01-06 10:33:53 -080030
San Mehata2677e42009-12-13 10:40:18 -080031#include <linux/kdev_t.h>
San Mehatf1b736b2009-10-10 17:22:08 -070032
33#define LOG_TAG "Vold"
34
Kenny Root7b18a7b2010-03-15 13:13:41 -070035#include <openssl/md5.h>
36
Jeff Sharkey71ebe152013-09-17 17:24:38 -070037#include <cutils/fs.h>
San Mehatf1b736b2009-10-10 17:22:08 -070038#include <cutils/log.h>
39
Robert Craigb9e3ba52014-02-04 10:53:00 -050040#include <selinux/android.h>
41
San Mehatfd7f5872009-10-12 11:32:47 -070042#include <sysutils/NetlinkEvent.h>
43
Kenny Root344ca102012-04-03 17:23:01 -070044#include <private/android_filesystem_config.h>
45
San Mehatf1b736b2009-10-10 17:22:08 -070046#include "VolumeManager.h"
San Mehatae10b912009-10-12 14:57:05 -070047#include "DirectVolume.h"
San Mehata2677e42009-12-13 10:40:18 -080048#include "ResponseCode.h"
San Mehata19b2502010-01-06 10:33:53 -080049#include "Loop.h"
Kenny Root344ca102012-04-03 17:23:01 -070050#include "Ext4.h"
San Mehata19b2502010-01-06 10:33:53 -080051#include "Fat.h"
San Mehatb78a32c2010-01-10 13:02:12 -080052#include "Devmapper.h"
San Mehat586536c2010-02-16 17:12:00 -080053#include "Process.h"
San Mehatfcf24fe2010-03-03 12:37:32 -080054#include "Asec.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070055#include "cryptfs.h"
San Mehat23969932010-01-09 07:08:06 -080056
Mike Lockwood97f2fc12011-06-07 10:51:38 -070057#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
58
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -070059#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
60 + (number & (~((1U << po2) - 1))))
61
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070062/* writes superblock at end of file or device given by name */
63static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
64 int sbfd = open(name, O_RDWR);
65 if (sbfd < 0) {
66 SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
67 return -1;
68 }
69
70 if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
71 SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
72 close(sbfd);
73 return -1;
74 }
75
76 if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
77 SLOGE("Failed to write superblock (%s)", strerror(errno));
78 close(sbfd);
79 return -1;
80 }
81 close(sbfd);
82 return 0;
83}
84
85static int adjustSectorNumExt4(unsigned numSectors) {
Daniel Rosenberge9196fe2014-06-10 17:16:03 -070086 // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
87 // preventing costly operations or unexpected ENOSPC error.
88 // Ext4::format() uses default block size without clustering.
89 unsigned clusterSectors = 4096 / 512;
90 unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
91 numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -070092 return ROUND_UP_POWER_OF_2(numSectors, 3);
93}
94
95static int adjustSectorNumFAT(unsigned numSectors) {
96 /*
97 * Add some headroom
98 */
99 unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
100 numSectors += fatSize + 2;
101 /*
102 * FAT is aligned to 32 kb with 512b sectors.
103 */
104 return ROUND_UP_POWER_OF_2(numSectors, 6);
105}
106
107static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
108 if (Loop::lookupActive(idHash, buffer, len)) {
109 if (Loop::create(idHash, asecFileName, buffer, len)) {
110 SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
111 return -1;
112 }
113 if (debug) {
114 SLOGD("New loop device created at %s", buffer);
115 }
116 } else {
117 if (debug) {
118 SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
119 }
120 }
121 return 0;
122}
123
124static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) {
125 if (strcmp(key, "none")) {
126 if (Devmapper::lookupActive(idHash, buffer, len)) {
127 if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
128 buffer, len)) {
129 SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
130 return -1;
131 }
132 if (debug) {
133 SLOGD("New devmapper instance created at %s", buffer);
134 }
135 } else {
136 if (debug) {
137 SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
138 }
139 }
140 *createdDMDevice = true;
141 } else {
142 strcpy(buffer, loopDevice);
143 *createdDMDevice = false;
144 }
145 return 0;
146}
147
148static void waitForDevMapper(const char *dmDevice) {
149 /*
150 * Wait for the device mapper node to be created. Sometimes it takes a
151 * while. Wait for up to 1 second. We could also inspect incoming uevents,
152 * but that would take more effort.
153 */
154 int tries = 25;
155 while (tries--) {
156 if (!access(dmDevice, F_OK) || errno != ENOENT) {
157 break;
158 }
159 usleep(40 * 1000);
160 }
161}
162
San Mehatf1b736b2009-10-10 17:22:08 -0700163VolumeManager *VolumeManager::sInstance = NULL;
164
165VolumeManager *VolumeManager::Instance() {
166 if (!sInstance)
167 sInstance = new VolumeManager();
168 return sInstance;
169}
170
171VolumeManager::VolumeManager() {
San Mehatd9a4e352010-03-12 13:32:47 -0800172 mDebug = false;
San Mehatf1b736b2009-10-10 17:22:08 -0700173 mVolumes = new VolumeCollection();
San Mehat88705162010-01-15 09:26:28 -0800174 mActiveContainers = new AsecIdCollection();
San Mehatf1b736b2009-10-10 17:22:08 -0700175 mBroadcaster = NULL;
Mike Lockwooda28056b2010-10-28 15:21:24 -0400176 mUmsSharingCount = 0;
177 mSavedDirtyRatio = -1;
178 // set dirty ratio to 0 when UMS is active
179 mUmsDirtyRatio = 0;
Ken Sumrall3b170052011-07-11 15:38:57 -0700180 mVolManagerDisabled = 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700181}
182
183VolumeManager::~VolumeManager() {
San Mehat88705162010-01-15 09:26:28 -0800184 delete mVolumes;
185 delete mActiveContainers;
San Mehatf1b736b2009-10-10 17:22:08 -0700186}
187
Kenny Root7b18a7b2010-03-15 13:13:41 -0700188char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700189 static const char* digits = "0123456789abcdef";
190
Kenny Root7b18a7b2010-03-15 13:13:41 -0700191 unsigned char sig[MD5_DIGEST_LENGTH];
192
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700193 if (buffer == NULL) {
194 SLOGE("Destination buffer is NULL");
195 errno = ESPIPE;
196 return NULL;
197 } else if (id == NULL) {
198 SLOGE("Source buffer is NULL");
199 errno = ESPIPE;
200 return NULL;
201 } else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
Colin Cross59846b62014-02-06 20:34:29 -0800202 SLOGE("Target hash buffer size < %d bytes (%zu)",
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700203 MD5_ASCII_LENGTH_PLUS_NULL, len);
San Mehatd9a4e352010-03-12 13:32:47 -0800204 errno = ESPIPE;
205 return NULL;
206 }
Kenny Root7b18a7b2010-03-15 13:13:41 -0700207
208 MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
San Mehatd9a4e352010-03-12 13:32:47 -0800209
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700210 char *p = buffer;
Kenny Root7b18a7b2010-03-15 13:13:41 -0700211 for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700212 *p++ = digits[sig[i] >> 4];
213 *p++ = digits[sig[i] & 0x0F];
San Mehatd9a4e352010-03-12 13:32:47 -0800214 }
Kenny Rootacc9e7d2010-06-18 19:06:50 -0700215 *p = '\0';
San Mehatd9a4e352010-03-12 13:32:47 -0800216
217 return buffer;
218}
219
220void VolumeManager::setDebug(bool enable) {
221 mDebug = enable;
222 VolumeCollection::iterator it;
223 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
224 (*it)->setDebug(enable);
225 }
226}
227
San Mehatf1b736b2009-10-10 17:22:08 -0700228int VolumeManager::start() {
229 return 0;
230}
231
232int VolumeManager::stop() {
233 return 0;
234}
235
236int VolumeManager::addVolume(Volume *v) {
237 mVolumes->push_back(v);
238 return 0;
239}
240
San Mehatfd7f5872009-10-12 11:32:47 -0700241void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
Tim Murray8439dc92014-12-15 11:56:11 -0800242#ifdef NETLINK_DEBUG
San Mehatfd7f5872009-10-12 11:32:47 -0700243 const char *devpath = evt->findParam("DEVPATH");
Tim Murray8439dc92014-12-15 11:56:11 -0800244#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700245
San Mehatfd7f5872009-10-12 11:32:47 -0700246 /* Lookup a volume to handle this device */
San Mehatf1b736b2009-10-10 17:22:08 -0700247 VolumeCollection::iterator it;
248 bool hit = false;
249 for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
San Mehatfd7f5872009-10-12 11:32:47 -0700250 if (!(*it)->handleBlockEvent(evt)) {
San Mehata2677e42009-12-13 10:40:18 -0800251#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700252 SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
San Mehata2677e42009-12-13 10:40:18 -0800253#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700254 hit = true;
San Mehatf1b736b2009-10-10 17:22:08 -0700255 break;
256 }
257 }
258
259 if (!hit) {
San Mehata2677e42009-12-13 10:40:18 -0800260#ifdef NETLINK_DEBUG
San Mehat97ac40e2010-03-24 10:24:19 -0700261 SLOGW("No volumes handled block event for '%s'", devpath);
San Mehata2677e42009-12-13 10:40:18 -0800262#endif
San Mehatf1b736b2009-10-10 17:22:08 -0700263 }
264}
265
JP Abgrall40b64a62014-07-24 18:02:16 -0700266int VolumeManager::listVolumes(SocketClient *cli, bool broadcast) {
San Mehatf1b736b2009-10-10 17:22:08 -0700267 VolumeCollection::iterator i;
JP Abgrall40b64a62014-07-24 18:02:16 -0700268 char msg[256];
San Mehatf1b736b2009-10-10 17:22:08 -0700269
270 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
271 char *buffer;
272 asprintf(&buffer, "%s %s %d",
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -0700273 (*i)->getLabel(), (*i)->getFuseMountpoint(),
San Mehatf1b736b2009-10-10 17:22:08 -0700274 (*i)->getState());
San Mehata2677e42009-12-13 10:40:18 -0800275 cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
San Mehatf1b736b2009-10-10 17:22:08 -0700276 free(buffer);
JP Abgrall40b64a62014-07-24 18:02:16 -0700277 if (broadcast) {
278 if((*i)->getUuid()) {
279 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
280 (*i)->getFuseMountpoint(), (*i)->getUuid());
281 mBroadcaster->sendBroadcast(ResponseCode::VolumeUuidChange,
282 msg, false);
283 }
284 if((*i)->getUserLabel()) {
285 snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
286 (*i)->getFuseMountpoint(), (*i)->getUserLabel());
287 mBroadcaster->sendBroadcast(ResponseCode::VolumeUserLabelChange,
288 msg, false);
289 }
290 }
San Mehatf1b736b2009-10-10 17:22:08 -0700291 }
San Mehata2677e42009-12-13 10:40:18 -0800292 cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
San Mehatf1b736b2009-10-10 17:22:08 -0700293 return 0;
294}
San Mehat49e2bce2009-10-12 16:29:01 -0700295
Ken Sumrall9caab762013-06-11 19:10:20 -0700296int VolumeManager::formatVolume(const char *label, bool wipe) {
San Mehata2677e42009-12-13 10:40:18 -0800297 Volume *v = lookupVolume(label);
298
299 if (!v) {
300 errno = ENOENT;
301 return -1;
302 }
303
Ken Sumrall3b170052011-07-11 15:38:57 -0700304 if (mVolManagerDisabled) {
305 errno = EBUSY;
306 return -1;
307 }
308
Ken Sumrall9caab762013-06-11 19:10:20 -0700309 return v->formatVol(wipe);
San Mehata2677e42009-12-13 10:40:18 -0800310}
311
Kenny Root508c0e12010-07-12 09:59:49 -0700312int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
313 char idHash[33];
314 if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
315 SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
316 return -1;
317 }
318
319 memset(mountPath, 0, mountPathLen);
rpcraigd1c226f2012-10-09 06:58:16 -0400320 int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
321 if ((written < 0) || (written >= mountPathLen)) {
322 errno = EINVAL;
323 return -1;
324 }
Kenny Root508c0e12010-07-12 09:59:49 -0700325
326 if (access(mountPath, F_OK)) {
327 errno = ENOENT;
328 return -1;
329 }
330
331 return 0;
332}
333
San Mehata19b2502010-01-06 10:33:53 -0800334int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
San Mehat88ac2c02010-03-23 11:15:58 -0700335 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700336
Nick Kralevich0de7c612014-01-27 14:58:06 -0800337 if (!isLegalAsecId(id)) {
338 SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
339 errno = EINVAL;
340 return -1;
341 }
342
Kenny Root344ca102012-04-03 17:23:01 -0700343 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
344 SLOGE("Couldn't find ASEC %s", id);
345 return -1;
346 }
San Mehat88ac2c02010-03-23 11:15:58 -0700347
348 memset(buffer, 0, maxlen);
349 if (access(asecFileName, F_OK)) {
350 errno = ENOENT;
351 return -1;
352 }
San Mehata19b2502010-01-06 10:33:53 -0800353
rpcraigd1c226f2012-10-09 06:58:16 -0400354 int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
355 if ((written < 0) || (written >= maxlen)) {
356 SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
357 errno = EINVAL;
358 return -1;
359 }
360
San Mehata19b2502010-01-06 10:33:53 -0800361 return 0;
362}
363
Dianne Hackborn736910c2011-06-27 13:37:07 -0700364int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
365 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700366
Nick Kralevich0de7c612014-01-27 14:58:06 -0800367 if (!isLegalAsecId(id)) {
368 SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
369 errno = EINVAL;
370 return -1;
371 }
372
Kenny Root344ca102012-04-03 17:23:01 -0700373 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
374 SLOGE("Couldn't find ASEC %s", id);
375 return -1;
376 }
Dianne Hackborn736910c2011-06-27 13:37:07 -0700377
378 memset(buffer, 0, maxlen);
379 if (access(asecFileName, F_OK)) {
380 errno = ENOENT;
381 return -1;
382 }
383
rpcraigd1c226f2012-10-09 06:58:16 -0400384 int written = snprintf(buffer, maxlen, "%s", asecFileName);
385 if ((written < 0) || (written >= maxlen)) {
386 errno = EINVAL;
387 return -1;
388 }
389
Dianne Hackborn736910c2011-06-27 13:37:07 -0700390 return 0;
391}
392
Kenny Root344ca102012-04-03 17:23:01 -0700393int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
394 const char *key, const int ownerUid, bool isExternal) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800395 struct asec_superblock sb;
396 memset(&sb, 0, sizeof(sb));
397
Nick Kralevich0de7c612014-01-27 14:58:06 -0800398 if (!isLegalAsecId(id)) {
399 SLOGE("createAsec: Invalid asec id \"%s\"", id);
400 errno = EINVAL;
401 return -1;
402 }
403
Kenny Root344ca102012-04-03 17:23:01 -0700404 const bool wantFilesystem = strcmp(fstype, "none");
405 bool usingExt4 = false;
406 if (wantFilesystem) {
407 usingExt4 = !strcmp(fstype, "ext4");
408 if (usingExt4) {
409 sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
410 } else if (strcmp(fstype, "fat")) {
411 SLOGE("Invalid filesystem type %s", fstype);
412 errno = EINVAL;
413 return -1;
414 }
415 }
416
San Mehatfcf24fe2010-03-03 12:37:32 -0800417 sb.magic = ASEC_SB_MAGIC;
418 sb.ver = ASEC_SB_VER;
San Mehata19b2502010-01-06 10:33:53 -0800419
San Mehatd31e3802010-02-18 08:37:45 -0800420 if (numSectors < ((1024*1024)/512)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700421 SLOGE("Invalid container size specified (%d sectors)", numSectors);
San Mehatd31e3802010-02-18 08:37:45 -0800422 errno = EINVAL;
423 return -1;
424 }
425
San Mehata19b2502010-01-06 10:33:53 -0800426 if (lookupVolume(id)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700427 SLOGE("ASEC id '%s' currently exists", id);
San Mehata19b2502010-01-06 10:33:53 -0800428 errno = EADDRINUSE;
429 return -1;
430 }
431
432 char asecFileName[255];
Kenny Root344ca102012-04-03 17:23:01 -0700433
434 if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
435 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
436 asecFileName, strerror(errno));
437 errno = EADDRINUSE;
438 return -1;
439 }
440
441 const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
442
rpcraigd1c226f2012-10-09 06:58:16 -0400443 int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
444 if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
445 errno = EINVAL;
446 return -1;
447 }
San Mehata19b2502010-01-06 10:33:53 -0800448
449 if (!access(asecFileName, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700450 SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
Kenny Root344ca102012-04-03 17:23:01 -0700451 asecFileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800452 errno = EADDRINUSE;
453 return -1;
454 }
455
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700456 unsigned numImgSectors;
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700457 if (usingExt4)
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700458 numImgSectors = adjustSectorNumExt4(numSectors);
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700459 else
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700460 numImgSectors = adjustSectorNumFAT(numSectors);
San Mehatfcf24fe2010-03-03 12:37:32 -0800461
462 // Add +1 for our superblock which is at the end
463 if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700464 SLOGE("ASEC image file creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800465 return -1;
466 }
467
San Mehatd9a4e352010-03-12 13:32:47 -0800468 char idHash[33];
469 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700470 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800471 unlink(asecFileName);
472 return -1;
473 }
474
San Mehata19b2502010-01-06 10:33:53 -0800475 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -0800476 if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700477 SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800478 unlink(asecFileName);
479 return -1;
480 }
481
San Mehatb78a32c2010-01-10 13:02:12 -0800482 char dmDevice[255];
483 bool cleanupDm = false;
San Mehata19b2502010-01-06 10:33:53 -0800484
San Mehatb78a32c2010-01-10 13:02:12 -0800485 if (strcmp(key, "none")) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800486 // XXX: This is all we support for now
487 sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
San Mehatd9a4e352010-03-12 13:32:47 -0800488 if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
San Mehatb78a32c2010-01-10 13:02:12 -0800489 sizeof(dmDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700490 SLOGE("ASEC device mapping failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800491 Loop::destroyByDevice(loopDevice);
492 unlink(asecFileName);
493 return -1;
494 }
495 cleanupDm = true;
496 } else {
San Mehatfcf24fe2010-03-03 12:37:32 -0800497 sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
San Mehatb78a32c2010-01-10 13:02:12 -0800498 strcpy(dmDevice, loopDevice);
499 }
500
San Mehatfcf24fe2010-03-03 12:37:32 -0800501 /*
502 * Drop down the superblock at the end of the file
503 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700504 if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
San Mehatfcf24fe2010-03-03 12:37:32 -0800505 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800506 Devmapper::destroy(idHash);
San Mehatfcf24fe2010-03-03 12:37:32 -0800507 }
508 Loop::destroyByDevice(loopDevice);
509 unlink(asecFileName);
510 return -1;
511 }
512
Kenny Root344ca102012-04-03 17:23:01 -0700513 if (wantFilesystem) {
514 int formatStatus;
rpcraiga54e13a2012-09-21 14:17:08 -0400515 char mountPoint[255];
516
rpcraigd1c226f2012-10-09 06:58:16 -0400517 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
518 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
519 SLOGE("ASEC fs format failed: couldn't construct mountPoint");
520 if (cleanupDm) {
521 Devmapper::destroy(idHash);
522 }
523 Loop::destroyByDevice(loopDevice);
524 unlink(asecFileName);
525 return -1;
526 }
rpcraiga54e13a2012-09-21 14:17:08 -0400527
Kenny Root344ca102012-04-03 17:23:01 -0700528 if (usingExt4) {
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700529 formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
Kenny Root344ca102012-04-03 17:23:01 -0700530 } else {
Ken Sumrall9caab762013-06-11 19:10:20 -0700531 formatStatus = Fat::format(dmDevice, numImgSectors, 0);
San Mehatb78a32c2010-01-10 13:02:12 -0800532 }
San Mehata19b2502010-01-06 10:33:53 -0800533
Kenny Root344ca102012-04-03 17:23:01 -0700534 if (formatStatus < 0) {
535 SLOGE("ASEC fs format failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -0800536 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800537 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -0800538 }
San Mehateb13a902010-01-07 12:12:50 -0800539 Loop::destroyByDevice(loopDevice);
540 unlink(asecFileName);
541 return -1;
542 }
Kenny Root344ca102012-04-03 17:23:01 -0700543
Kenny Root344ca102012-04-03 17:23:01 -0700544 if (mkdir(mountPoint, 0000)) {
San Mehata1091cb2010-02-28 20:17:20 -0800545 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -0700546 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800547 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800548 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800549 }
550 Loop::destroyByDevice(loopDevice);
551 unlink(asecFileName);
552 return -1;
553 }
San Mehatb78a32c2010-01-10 13:02:12 -0800554 }
San Mehata1091cb2010-02-28 20:17:20 -0800555
Kenny Root344ca102012-04-03 17:23:01 -0700556 int mountStatus;
557 if (usingExt4) {
558 mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
559 } else {
560 mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
561 false);
562 }
563
564 if (mountStatus) {
San Mehat97ac40e2010-03-24 10:24:19 -0700565 SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
San Mehata1091cb2010-02-28 20:17:20 -0800566 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -0800567 Devmapper::destroy(idHash);
San Mehata1091cb2010-02-28 20:17:20 -0800568 }
569 Loop::destroyByDevice(loopDevice);
570 unlink(asecFileName);
571 return -1;
572 }
Kenny Root344ca102012-04-03 17:23:01 -0700573
574 if (usingExt4) {
575 int dirfd = open(mountPoint, O_DIRECTORY);
576 if (dirfd >= 0) {
577 if (fchown(dirfd, ownerUid, AID_SYSTEM)
578 || fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
579 SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
580 }
581 close(dirfd);
582 }
583 }
San Mehata1091cb2010-02-28 20:17:20 -0800584 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700585 SLOGI("Created raw secure container %s (no filesystem)", id);
San Mehata19b2502010-01-06 10:33:53 -0800586 }
San Mehat88705162010-01-15 09:26:28 -0800587
Kenny Rootcbacf782010-09-24 15:11:48 -0700588 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehata19b2502010-01-06 10:33:53 -0800589 return 0;
590}
591
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700592int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
593 char asecFileName[255];
594 char mountPoint[255];
595 bool cleanupDm = false;
596
597 if (!isLegalAsecId(id)) {
598 SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
599 errno = EINVAL;
600 return -1;
601 }
602
603 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
604 SLOGE("Couldn't find ASEC %s", id);
605 return -1;
606 }
607
608 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
609 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
610 SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
611 return -1;
612 }
613
614 if (isMountpointMounted(mountPoint)) {
615 SLOGE("ASEC %s mounted. Unmount before resizing", id);
616 errno = EBUSY;
617 return -1;
618 }
619
620 struct asec_superblock sb;
621 int fd;
622 unsigned int oldNumSec = 0;
623
624 if ((fd = open(asecFileName, O_RDONLY)) < 0) {
625 SLOGE("Failed to open ASEC file (%s)", strerror(errno));
626 return -1;
627 }
628
629 struct stat info;
630 if (fstat(fd, &info) < 0) {
631 SLOGE("Failed to get file size (%s)", strerror(errno));
632 close(fd);
633 return -1;
634 }
635
636 oldNumSec = info.st_size / 512;
637
638 unsigned numImgSectors;
639 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
640 numImgSectors = adjustSectorNumExt4(numSectors);
641 else
642 numImgSectors = adjustSectorNumFAT(numSectors);
643 /*
644 * add one block for the superblock
645 */
646 SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
Jeff Sharkey43ed1232014-08-22 12:29:05 -0700647 if (oldNumSec == numImgSectors + 1) {
648 SLOGW("Size unchanged; ignoring resize request");
649 return 0;
650 } else if (oldNumSec > numImgSectors + 1) {
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700651 SLOGE("Only growing is currently supported.");
652 close(fd);
653 return -1;
654 }
655
656 /*
657 * Try to read superblock.
658 */
659 memset(&sb, 0, sizeof(struct asec_superblock));
660 if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
661 SLOGE("lseek failed (%s)", strerror(errno));
662 close(fd);
663 return -1;
664 }
665 if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
666 SLOGE("superblock read failed (%s)", strerror(errno));
667 close(fd);
668 return -1;
669 }
670 close(fd);
671
672 if (mDebug) {
673 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
674 }
675 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
676 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
677 errno = EMEDIUMTYPE;
678 return -1;
679 }
680
681 if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
682 SLOGE("Only ext4 partitions are supported for resize");
683 errno = EINVAL;
684 return -1;
685 }
686
687 if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
688 SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
689 return -1;
690 }
691
692 /*
693 * Drop down a copy of the superblock at the end of the file
694 */
695 if (writeSuperBlock(asecFileName, &sb, numImgSectors))
696 goto fail;
697
698 char idHash[33];
699 if (!asecHash(id, idHash, sizeof(idHash))) {
700 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
701 goto fail;
702 }
703
704 char loopDevice[255];
705 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
706 goto fail;
707
708 char dmDevice[255];
709
710 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
711 Loop::destroyByDevice(loopDevice);
712 goto fail;
713 }
714
715 /*
716 * Wait for the device mapper node to be created.
717 */
718 waitForDevMapper(dmDevice);
719
720 if (Ext4::resize(dmDevice, numImgSectors)) {
721 SLOGE("Unable to resize %s (%s)", id, strerror(errno));
722 if (cleanupDm) {
723 Devmapper::destroy(idHash);
724 }
725 Loop::destroyByDevice(loopDevice);
726 goto fail;
727 }
728
729 return 0;
730fail:
731 Loop::resizeImageFile(asecFileName, oldNumSec);
732 return -1;
733}
734
San Mehata19b2502010-01-06 10:33:53 -0800735int VolumeManager::finalizeAsec(const char *id) {
736 char asecFileName[255];
737 char loopDevice[255];
738 char mountPoint[255];
739
Nick Kralevich0de7c612014-01-27 14:58:06 -0800740 if (!isLegalAsecId(id)) {
741 SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
742 errno = EINVAL;
743 return -1;
744 }
745
Kenny Root344ca102012-04-03 17:23:01 -0700746 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
747 SLOGE("Couldn't find ASEC %s", id);
748 return -1;
749 }
San Mehata19b2502010-01-06 10:33:53 -0800750
San Mehatd9a4e352010-03-12 13:32:47 -0800751 char idHash[33];
752 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700753 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -0800754 return -1;
755 }
756
757 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehat97ac40e2010-03-24 10:24:19 -0700758 SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800759 return -1;
760 }
761
Kenny Root344ca102012-04-03 17:23:01 -0700762 unsigned int nr_sec = 0;
763 struct asec_superblock sb;
764
765 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
766 return -1;
767 }
768
rpcraigd1c226f2012-10-09 06:58:16 -0400769 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
770 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
771 SLOGE("ASEC finalize failed: couldn't construct mountPoint");
772 return -1;
773 }
Kenny Root344ca102012-04-03 17:23:01 -0700774
775 int result = 0;
776 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
777 result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
778 } else {
779 result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
780 }
781
782 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -0700783 SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -0800784 return -1;
785 }
786
San Mehatd9a4e352010-03-12 13:32:47 -0800787 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -0700788 SLOGD("ASEC %s finalized", id);
San Mehatd9a4e352010-03-12 13:32:47 -0800789 }
San Mehata19b2502010-01-06 10:33:53 -0800790 return 0;
791}
792
Kenny Root344ca102012-04-03 17:23:01 -0700793int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
794 char asecFileName[255];
795 char loopDevice[255];
796 char mountPoint[255];
797
798 if (gid < AID_APP) {
799 SLOGE("Group ID is not in application range");
800 return -1;
801 }
802
Nick Kralevich0de7c612014-01-27 14:58:06 -0800803 if (!isLegalAsecId(id)) {
804 SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
805 errno = EINVAL;
806 return -1;
807 }
808
Kenny Root344ca102012-04-03 17:23:01 -0700809 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
810 SLOGE("Couldn't find ASEC %s", id);
811 return -1;
812 }
813
814 char idHash[33];
815 if (!asecHash(id, idHash, sizeof(idHash))) {
816 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
817 return -1;
818 }
819
820 if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
821 SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
822 return -1;
823 }
824
825 unsigned int nr_sec = 0;
826 struct asec_superblock sb;
827
828 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
829 return -1;
830 }
831
rpcraigd1c226f2012-10-09 06:58:16 -0400832 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
833 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
834 SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
835 return -1;
836 }
Kenny Root344ca102012-04-03 17:23:01 -0700837
838 int result = 0;
839 if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
840 return 0;
841 }
842
843 int ret = Ext4::doMount(loopDevice, mountPoint,
844 false /* read-only */,
845 true /* remount */,
846 false /* executable */);
847 if (ret) {
848 SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
849 return -1;
850 }
851
852 char *paths[] = { mountPoint, NULL };
853
854 FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
855 if (fts) {
856 // Traverse the entire hierarchy and chown to system UID.
857 for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
858 // We don't care about the lost+found directory.
859 if (!strcmp(ftsent->fts_name, "lost+found")) {
860 continue;
861 }
862
863 /*
864 * There can only be one file marked as private right now.
865 * This should be more robust, but it satisfies the requirements
866 * we have for right now.
867 */
868 const bool privateFile = !strcmp(ftsent->fts_name, filename);
869
870 int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
871 if (fd < 0) {
872 SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
873 result = -1;
874 continue;
875 }
876
877 result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
878
879 if (ftsent->fts_info & FTS_D) {
Kenny Root1a673c82012-05-10 16:45:29 -0700880 result |= fchmod(fd, 0755);
Kenny Root348c8ab2012-05-10 15:39:53 -0700881 } else if (ftsent->fts_info & FTS_F) {
Kenny Root344ca102012-04-03 17:23:01 -0700882 result |= fchmod(fd, privateFile ? 0640 : 0644);
883 }
Robert Craigb9e3ba52014-02-04 10:53:00 -0500884
Stephen Smalley5093e612014-02-12 09:43:08 -0500885 if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
Robert Craigb9e3ba52014-02-04 10:53:00 -0500886 SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
887 result |= -1;
888 }
889
Kenny Root344ca102012-04-03 17:23:01 -0700890 close(fd);
891 }
892 fts_close(fts);
893
894 // Finally make the directory readable by everyone.
895 int dirfd = open(mountPoint, O_DIRECTORY);
896 if (dirfd < 0 || fchmod(dirfd, 0755)) {
897 SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
898 result |= -1;
899 }
900 close(dirfd);
901 } else {
902 result |= -1;
903 }
904
905 result |= Ext4::doMount(loopDevice, mountPoint,
906 true /* read-only */,
907 true /* remount */,
908 true /* execute */);
909
910 if (result) {
911 SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
912 return -1;
913 }
914
915 if (mDebug) {
916 SLOGD("ASEC %s permissions fixed", id);
917 }
918 return 0;
919}
920
San Mehat048b0802010-01-23 08:17:06 -0800921int VolumeManager::renameAsec(const char *id1, const char *id2) {
Kenny Root344ca102012-04-03 17:23:01 -0700922 char asecFilename1[255];
San Mehat048b0802010-01-23 08:17:06 -0800923 char *asecFilename2;
924 char mountPoint[255];
925
Kenny Root344ca102012-04-03 17:23:01 -0700926 const char *dir;
927
Nick Kralevich0de7c612014-01-27 14:58:06 -0800928 if (!isLegalAsecId(id1)) {
929 SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
930 errno = EINVAL;
931 return -1;
932 }
933
934 if (!isLegalAsecId(id2)) {
935 SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
936 errno = EINVAL;
937 return -1;
938 }
939
Kenny Root344ca102012-04-03 17:23:01 -0700940 if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
941 SLOGE("Couldn't find ASEC %s", id1);
942 return -1;
943 }
944
945 asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
San Mehat048b0802010-01-23 08:17:06 -0800946
rpcraigd1c226f2012-10-09 06:58:16 -0400947 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
948 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
949 SLOGE("Rename failed: couldn't construct mountpoint");
950 goto out_err;
951 }
952
San Mehat048b0802010-01-23 08:17:06 -0800953 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700954 SLOGW("Rename attempt when src mounted");
San Mehat048b0802010-01-23 08:17:06 -0800955 errno = EBUSY;
956 goto out_err;
957 }
958
rpcraigd1c226f2012-10-09 06:58:16 -0400959 written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
960 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
961 SLOGE("Rename failed: couldn't construct mountpoint2");
962 goto out_err;
963 }
964
San Mehat96956ed2010-02-24 08:42:51 -0800965 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700966 SLOGW("Rename attempt when dst mounted");
San Mehat96956ed2010-02-24 08:42:51 -0800967 errno = EBUSY;
968 goto out_err;
969 }
970
San Mehat048b0802010-01-23 08:17:06 -0800971 if (!access(asecFilename2, F_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700972 SLOGE("Rename attempt when dst exists");
San Mehat048b0802010-01-23 08:17:06 -0800973 errno = EADDRINUSE;
974 goto out_err;
975 }
976
977 if (rename(asecFilename1, asecFilename2)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700978 SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
San Mehat048b0802010-01-23 08:17:06 -0800979 goto out_err;
980 }
981
San Mehat048b0802010-01-23 08:17:06 -0800982 free(asecFilename2);
983 return 0;
984
985out_err:
San Mehat048b0802010-01-23 08:17:06 -0800986 free(asecFilename2);
987 return -1;
988}
989
Kenny Rootfb7c4d52010-06-30 18:48:41 -0700990#define UNMOUNT_RETRIES 5
991#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
San Mehat4ba89482010-02-18 09:00:18 -0800992int VolumeManager::unmountAsec(const char *id, bool force) {
San Mehata19b2502010-01-06 10:33:53 -0800993 char asecFileName[255];
994 char mountPoint[255];
995
Nick Kralevich0de7c612014-01-27 14:58:06 -0800996 if (!isLegalAsecId(id)) {
997 SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
998 errno = EINVAL;
999 return -1;
1000 }
1001
Kenny Root344ca102012-04-03 17:23:01 -07001002 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1003 SLOGE("Couldn't find ASEC %s", id);
1004 return -1;
1005 }
1006
rpcraigd1c226f2012-10-09 06:58:16 -04001007 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1008 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1009 SLOGE("ASEC unmount failed for %s: couldn't construct mountpoint", id);
1010 return -1;
1011 }
San Mehata19b2502010-01-06 10:33:53 -08001012
San Mehatd9a4e352010-03-12 13:32:47 -08001013 char idHash[33];
1014 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001015 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001016 return -1;
1017 }
1018
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001019 return unmountLoopImage(id, idHash, asecFileName, mountPoint, force);
1020}
1021
Kenny Root508c0e12010-07-12 09:59:49 -07001022int VolumeManager::unmountObb(const char *fileName, bool force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001023 char mountPoint[255];
1024
1025 char idHash[33];
1026 if (!asecHash(fileName, idHash, sizeof(idHash))) {
1027 SLOGE("Hash of '%s' failed (%s)", fileName, strerror(errno));
1028 return -1;
1029 }
1030
rpcraigd1c226f2012-10-09 06:58:16 -04001031 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1032 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1033 SLOGE("OBB unmount failed for %s: couldn't construct mountpoint", fileName);
1034 return -1;
1035 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001036
1037 return unmountLoopImage(fileName, idHash, fileName, mountPoint, force);
1038}
1039
1040int VolumeManager::unmountLoopImage(const char *id, const char *idHash,
1041 const char *fileName, const char *mountPoint, bool force) {
San Mehat0586d542010-01-12 15:38:59 -08001042 if (!isMountpointMounted(mountPoint)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001043 SLOGE("Unmount request for %s when not mounted", id);
Kenny Root918e5f92010-09-30 18:00:52 -07001044 errno = ENOENT;
San Mehatb78a32c2010-01-10 13:02:12 -08001045 return -1;
1046 }
San Mehat23969932010-01-09 07:08:06 -08001047
San Mehatb78a32c2010-01-10 13:02:12 -08001048 int i, rc;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001049 for (i = 1; i <= UNMOUNT_RETRIES; i++) {
San Mehatb78a32c2010-01-10 13:02:12 -08001050 rc = umount(mountPoint);
1051 if (!rc) {
1052 break;
San Mehata19b2502010-01-06 10:33:53 -08001053 }
San Mehatb78a32c2010-01-10 13:02:12 -08001054 if (rc && (errno == EINVAL || errno == ENOENT)) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001055 SLOGI("Container %s unmounted OK", id);
San Mehatb78a32c2010-01-10 13:02:12 -08001056 rc = 0;
1057 break;
San Mehata19b2502010-01-06 10:33:53 -08001058 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001059 SLOGW("%s unmount attempt %d failed (%s)",
San Mehat8c940ef2010-02-13 14:19:53 -08001060 id, i, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001061
San Mehat4ba89482010-02-18 09:00:18 -08001062 int action = 0; // default is to just complain
1063
1064 if (force) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001065 if (i > (UNMOUNT_RETRIES - 2))
San Mehat4ba89482010-02-18 09:00:18 -08001066 action = 2; // SIGKILL
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001067 else if (i > (UNMOUNT_RETRIES - 3))
San Mehat4ba89482010-02-18 09:00:18 -08001068 action = 1; // SIGHUP
1069 }
San Mehat8c940ef2010-02-13 14:19:53 -08001070
San Mehat586536c2010-02-16 17:12:00 -08001071 Process::killProcessesWithOpenFiles(mountPoint, action);
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001072 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehatb78a32c2010-01-10 13:02:12 -08001073 }
1074
1075 if (rc) {
San Mehat4ba89482010-02-18 09:00:18 -08001076 errno = EBUSY;
San Mehat97ac40e2010-03-24 10:24:19 -07001077 SLOGE("Failed to unmount container %s (%s)", id, strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001078 return -1;
1079 }
1080
San Mehat12f4b892010-02-24 11:43:22 -08001081 int retries = 10;
1082
1083 while(retries--) {
1084 if (!rmdir(mountPoint)) {
1085 break;
1086 }
1087
San Mehat97ac40e2010-03-24 10:24:19 -07001088 SLOGW("Failed to rmdir %s (%s)", mountPoint, strerror(errno));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001089 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
San Mehat12f4b892010-02-24 11:43:22 -08001090 }
1091
1092 if (!retries) {
San Mehat97ac40e2010-03-24 10:24:19 -07001093 SLOGE("Timed out trying to rmdir %s (%s)", mountPoint, strerror(errno));
San Mehatf5c61982010-02-03 11:04:46 -08001094 }
San Mehat88705162010-01-15 09:26:28 -08001095
Paul Lawrence60dec162014-09-02 10:52:15 -07001096 for (i=1; i <= UNMOUNT_RETRIES; i++) {
1097 if (Devmapper::destroy(idHash) && errno != ENXIO) {
1098 SLOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
1099 usleep(UNMOUNT_SLEEP_BETWEEN_RETRY_MS);
1100 continue;
1101 } else {
1102 break;
1103 }
San Mehata19b2502010-01-06 10:33:53 -08001104 }
1105
1106 char loopDevice[255];
San Mehatd9a4e352010-03-12 13:32:47 -08001107 if (!Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
San Mehata19b2502010-01-06 10:33:53 -08001108 Loop::destroyByDevice(loopDevice);
San Mehatd9a4e352010-03-12 13:32:47 -08001109 } else {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001110 SLOGW("Failed to find loop device for {%s} (%s)", fileName, strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001111 }
San Mehat88705162010-01-15 09:26:28 -08001112
1113 AsecIdCollection::iterator it;
1114 for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001115 ContainerData* cd = *it;
1116 if (!strcmp(cd->id, id)) {
San Mehat88705162010-01-15 09:26:28 -08001117 free(*it);
1118 mActiveContainers->erase(it);
1119 break;
1120 }
1121 }
1122 if (it == mActiveContainers->end()) {
San Mehat97ac40e2010-03-24 10:24:19 -07001123 SLOGW("mActiveContainers is inconsistent!");
San Mehat88705162010-01-15 09:26:28 -08001124 }
San Mehatb78a32c2010-01-10 13:02:12 -08001125 return 0;
1126}
1127
San Mehat4ba89482010-02-18 09:00:18 -08001128int VolumeManager::destroyAsec(const char *id, bool force) {
San Mehatb78a32c2010-01-10 13:02:12 -08001129 char asecFileName[255];
1130 char mountPoint[255];
1131
Nick Kralevich0de7c612014-01-27 14:58:06 -08001132 if (!isLegalAsecId(id)) {
1133 SLOGE("destroyAsec: Invalid asec id \"%s\"", id);
1134 errno = EINVAL;
1135 return -1;
1136 }
1137
Kenny Root344ca102012-04-03 17:23:01 -07001138 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1139 SLOGE("Couldn't find ASEC %s", id);
1140 return -1;
1141 }
1142
rpcraigd1c226f2012-10-09 06:58:16 -04001143 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1144 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
1145 SLOGE("ASEC destroy failed for %s: couldn't construct mountpoint", id);
1146 return -1;
1147 }
San Mehatb78a32c2010-01-10 13:02:12 -08001148
San Mehat0586d542010-01-12 15:38:59 -08001149 if (isMountpointMounted(mountPoint)) {
San Mehatd9a4e352010-03-12 13:32:47 -08001150 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001151 SLOGD("Unmounting container before destroy");
San Mehatd9a4e352010-03-12 13:32:47 -08001152 }
San Mehat4ba89482010-02-18 09:00:18 -08001153 if (unmountAsec(id, force)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001154 SLOGE("Failed to unmount asec %s for destroy (%s)", id, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001155 return -1;
1156 }
1157 }
San Mehata19b2502010-01-06 10:33:53 -08001158
San Mehat0586d542010-01-12 15:38:59 -08001159 if (unlink(asecFileName)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001160 SLOGE("Failed to unlink asec '%s' (%s)", asecFileName, strerror(errno));
San Mehat0586d542010-01-12 15:38:59 -08001161 return -1;
1162 }
San Mehata19b2502010-01-06 10:33:53 -08001163
San Mehatd9a4e352010-03-12 13:32:47 -08001164 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001165 SLOGD("ASEC %s destroyed", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001166 }
San Mehata19b2502010-01-06 10:33:53 -08001167 return 0;
1168}
1169
Nick Kralevich0de7c612014-01-27 14:58:06 -08001170/*
1171 * Legal ASEC ids consist of alphanumeric characters, '-',
1172 * '_', or '.'. ".." is not allowed. The first or last character
1173 * of the ASEC id cannot be '.' (dot).
1174 */
1175bool VolumeManager::isLegalAsecId(const char *id) const {
1176 size_t i;
1177 size_t len = strlen(id);
1178
1179 if (len == 0) {
1180 return false;
1181 }
1182 if ((id[0] == '.') || (id[len - 1] == '.')) {
1183 return false;
1184 }
1185
1186 for (i = 0; i < len; i++) {
1187 if (id[i] == '.') {
1188 // i=0 is guaranteed never to have a dot. See above.
1189 if (id[i-1] == '.') return false;
1190 continue;
1191 }
1192 if (id[i] == '_' || id[i] == '-') continue;
1193 if (id[i] >= 'a' && id[i] <= 'z') continue;
1194 if (id[i] >= 'A' && id[i] <= 'Z') continue;
1195 if (id[i] >= '0' && id[i] <= '9') continue;
1196 return false;
1197 }
1198
1199 return true;
1200}
1201
Kenny Root344ca102012-04-03 17:23:01 -07001202bool VolumeManager::isAsecInDirectory(const char *dir, const char *asecName) const {
1203 int dirfd = open(dir, O_DIRECTORY);
1204 if (dirfd < 0) {
1205 SLOGE("Couldn't open internal ASEC dir (%s)", strerror(errno));
1206 return -1;
1207 }
1208
1209 bool ret = false;
1210
1211 if (!faccessat(dirfd, asecName, F_OK, AT_SYMLINK_NOFOLLOW)) {
1212 ret = true;
1213 }
1214
1215 close(dirfd);
1216
1217 return ret;
1218}
1219
1220int VolumeManager::findAsec(const char *id, char *asecPath, size_t asecPathLen,
1221 const char **directory) const {
Kenny Root344ca102012-04-03 17:23:01 -07001222 char *asecName;
1223
Nick Kralevich0de7c612014-01-27 14:58:06 -08001224 if (!isLegalAsecId(id)) {
1225 SLOGE("findAsec: Invalid asec id \"%s\"", id);
1226 errno = EINVAL;
1227 return -1;
1228 }
1229
Kenny Root344ca102012-04-03 17:23:01 -07001230 if (asprintf(&asecName, "%s.asec", id) < 0) {
1231 SLOGE("Couldn't allocate string to write ASEC name");
1232 return -1;
1233 }
1234
1235 const char *dir;
1236 if (isAsecInDirectory(Volume::SEC_ASECDIR_INT, asecName)) {
1237 dir = Volume::SEC_ASECDIR_INT;
1238 } else if (isAsecInDirectory(Volume::SEC_ASECDIR_EXT, asecName)) {
1239 dir = Volume::SEC_ASECDIR_EXT;
1240 } else {
1241 free(asecName);
1242 return -1;
1243 }
1244
1245 if (directory != NULL) {
1246 *directory = dir;
1247 }
1248
1249 if (asecPath != NULL) {
1250 int written = snprintf(asecPath, asecPathLen, "%s/%s", dir, asecName);
rpcraigd1c226f2012-10-09 06:58:16 -04001251 if ((written < 0) || (size_t(written) >= asecPathLen)) {
1252 SLOGE("findAsec failed for %s: couldn't construct ASEC path", id);
Kenny Root344ca102012-04-03 17:23:01 -07001253 free(asecName);
1254 return -1;
1255 }
1256 }
1257
1258 free(asecName);
1259 return 0;
1260}
1261
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001262int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid, bool readOnly) {
San Mehata19b2502010-01-06 10:33:53 -08001263 char asecFileName[255];
1264 char mountPoint[255];
1265
Nick Kralevich0de7c612014-01-27 14:58:06 -08001266 if (!isLegalAsecId(id)) {
1267 SLOGE("mountAsec: Invalid asec id \"%s\"", id);
1268 errno = EINVAL;
1269 return -1;
1270 }
1271
Kenny Root344ca102012-04-03 17:23:01 -07001272 if (findAsec(id, asecFileName, sizeof(asecFileName))) {
1273 SLOGE("Couldn't find ASEC %s", id);
1274 return -1;
1275 }
1276
rpcraigd1c226f2012-10-09 06:58:16 -04001277 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
1278 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001279 SLOGE("ASEC mount failed for %s: couldn't construct mountpoint", id);
rpcraigd1c226f2012-10-09 06:58:16 -04001280 return -1;
1281 }
San Mehata19b2502010-01-06 10:33:53 -08001282
1283 if (isMountpointMounted(mountPoint)) {
San Mehat97ac40e2010-03-24 10:24:19 -07001284 SLOGE("ASEC %s already mounted", id);
San Mehata19b2502010-01-06 10:33:53 -08001285 errno = EBUSY;
1286 return -1;
1287 }
1288
San Mehatd9a4e352010-03-12 13:32:47 -08001289 char idHash[33];
1290 if (!asecHash(id, idHash, sizeof(idHash))) {
San Mehat97ac40e2010-03-24 10:24:19 -07001291 SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
San Mehatd9a4e352010-03-12 13:32:47 -08001292 return -1;
1293 }
Kenny Root7b18a7b2010-03-15 13:13:41 -07001294
San Mehata19b2502010-01-06 10:33:53 -08001295 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001296 if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
1297 return -1;
San Mehatb78a32c2010-01-10 13:02:12 -08001298
1299 char dmDevice[255];
1300 bool cleanupDm = false;
Tim Murray8439dc92014-12-15 11:56:11 -08001301
San Mehatfcf24fe2010-03-03 12:37:32 -08001302 unsigned int nr_sec = 0;
San Mehatfcf24fe2010-03-03 12:37:32 -08001303 struct asec_superblock sb;
San Mehatfcf24fe2010-03-03 12:37:32 -08001304
Kenny Root344ca102012-04-03 17:23:01 -07001305 if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
1306 return -1;
1307 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001308
San Mehatd9a4e352010-03-12 13:32:47 -08001309 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001310 SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatd9a4e352010-03-12 13:32:47 -08001311 }
San Mehatfcf24fe2010-03-03 12:37:32 -08001312 if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
San Mehat97ac40e2010-03-24 10:24:19 -07001313 SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
San Mehatfcf24fe2010-03-03 12:37:32 -08001314 Loop::destroyByDevice(loopDevice);
1315 errno = EMEDIUMTYPE;
1316 return -1;
1317 }
1318 nr_sec--; // We don't want the devmapping to extend onto our superblock
1319
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001320 if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash , nr_sec, &cleanupDm, mDebug)) {
1321 Loop::destroyByDevice(loopDevice);
1322 return -1;
San Mehata19b2502010-01-06 10:33:53 -08001323 }
1324
Kenny Root344ca102012-04-03 17:23:01 -07001325 if (mkdir(mountPoint, 0000)) {
San Mehatb78a32c2010-01-10 13:02:12 -08001326 if (errno != EEXIST) {
San Mehat97ac40e2010-03-24 10:24:19 -07001327 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001328 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001329 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001330 }
1331 Loop::destroyByDevice(loopDevice);
1332 return -1;
1333 }
San Mehata19b2502010-01-06 10:33:53 -08001334 }
1335
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001336 /*
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001337 * Wait for the device mapper node to be created.
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001338 */
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001339 waitForDevMapper(dmDevice);
Kenny Rootcdc2a1c2012-05-03 13:49:46 -07001340
Kenny Root344ca102012-04-03 17:23:01 -07001341 int result;
1342 if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001343 result = Ext4::doMount(dmDevice, mountPoint, readOnly, false, readOnly);
Kenny Root344ca102012-04-03 17:23:01 -07001344 } else {
Jeff Sharkey43ed1232014-08-22 12:29:05 -07001345 result = Fat::doMount(dmDevice, mountPoint, readOnly, false, readOnly, ownerUid, 0, 0222, false);
Kenny Root344ca102012-04-03 17:23:01 -07001346 }
1347
1348 if (result) {
San Mehat97ac40e2010-03-24 10:24:19 -07001349 SLOGE("ASEC mount failed (%s)", strerror(errno));
San Mehatb78a32c2010-01-10 13:02:12 -08001350 if (cleanupDm) {
San Mehatd9a4e352010-03-12 13:32:47 -08001351 Devmapper::destroy(idHash);
San Mehatb78a32c2010-01-10 13:02:12 -08001352 }
1353 Loop::destroyByDevice(loopDevice);
San Mehata19b2502010-01-06 10:33:53 -08001354 return -1;
1355 }
1356
Kenny Rootcbacf782010-09-24 15:11:48 -07001357 mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
San Mehatd9a4e352010-03-12 13:32:47 -08001358 if (mDebug) {
San Mehat97ac40e2010-03-24 10:24:19 -07001359 SLOGD("ASEC %s mounted", id);
San Mehatd9a4e352010-03-12 13:32:47 -08001360 }
San Mehata19b2502010-01-06 10:33:53 -08001361 return 0;
1362}
1363
Kenny Root93ecb382012-08-09 11:28:37 -07001364Volume* VolumeManager::getVolumeForFile(const char *fileName) {
1365 VolumeCollection::iterator i;
1366
1367 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001368 const char* mountPoint = (*i)->getFuseMountpoint();
Kenny Root93ecb382012-08-09 11:28:37 -07001369 if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
1370 return *i;
1371 }
1372 }
1373
1374 return NULL;
1375}
1376
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001377/**
1378 * Mounts an image file <code>img</code>.
1379 */
Jeff Sharkey69479042012-09-25 16:14:57 -07001380int VolumeManager::mountObb(const char *img, const char *key, int ownerGid) {
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001381 char mountPoint[255];
1382
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001383 char idHash[33];
1384 if (!asecHash(img, idHash, sizeof(idHash))) {
1385 SLOGE("Hash of '%s' failed (%s)", img, strerror(errno));
1386 return -1;
1387 }
1388
rpcraigd1c226f2012-10-09 06:58:16 -04001389 int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::LOOPDIR, idHash);
1390 if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
Colin Cross59846b62014-02-06 20:34:29 -08001391 SLOGE("OBB mount failed for %s: couldn't construct mountpoint", img);
rpcraigd1c226f2012-10-09 06:58:16 -04001392 return -1;
1393 }
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001394
1395 if (isMountpointMounted(mountPoint)) {
1396 SLOGE("Image %s already mounted", img);
1397 errno = EBUSY;
1398 return -1;
1399 }
1400
1401 char loopDevice[255];
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001402 if (setupLoopDevice(loopDevice, sizeof(loopDevice), img, idHash, mDebug))
1403 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001404
1405 char dmDevice[255];
1406 bool cleanupDm = false;
1407 int fd;
1408 unsigned int nr_sec = 0;
1409
1410 if ((fd = open(loopDevice, O_RDWR)) < 0) {
1411 SLOGE("Failed to open loopdevice (%s)", strerror(errno));
1412 Loop::destroyByDevice(loopDevice);
1413 return -1;
1414 }
1415
1416 if (ioctl(fd, BLKGETSIZE, &nr_sec)) {
1417 SLOGE("Failed to get loop size (%s)", strerror(errno));
1418 Loop::destroyByDevice(loopDevice);
1419 close(fd);
1420 return -1;
1421 }
1422
1423 close(fd);
1424
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -07001425 if (setupDevMapperDevice(dmDevice, sizeof(loopDevice), loopDevice, img,key, idHash , nr_sec, &cleanupDm, mDebug)) {
1426 Loop::destroyByDevice(loopDevice);
1427 return -1;
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001428 }
1429
1430 if (mkdir(mountPoint, 0755)) {
1431 if (errno != EEXIST) {
1432 SLOGE("Mountpoint creation failed (%s)", strerror(errno));
1433 if (cleanupDm) {
1434 Devmapper::destroy(idHash);
1435 }
1436 Loop::destroyByDevice(loopDevice);
1437 return -1;
1438 }
1439 }
1440
Jeff Sharkey69479042012-09-25 16:14:57 -07001441 if (Fat::doMount(dmDevice, mountPoint, true, false, true, 0, ownerGid,
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001442 0227, false)) {
1443 SLOGE("Image mount failed (%s)", strerror(errno));
1444 if (cleanupDm) {
1445 Devmapper::destroy(idHash);
1446 }
1447 Loop::destroyByDevice(loopDevice);
1448 return -1;
1449 }
1450
Kenny Rootcbacf782010-09-24 15:11:48 -07001451 mActiveContainers->push_back(new ContainerData(strdup(img), OBB));
Kenny Rootfb7c4d52010-06-30 18:48:41 -07001452 if (mDebug) {
1453 SLOGD("Image %s mounted", img);
1454 }
1455 return 0;
1456}
1457
San Mehat49e2bce2009-10-12 16:29:01 -07001458int VolumeManager::mountVolume(const char *label) {
1459 Volume *v = lookupVolume(label);
1460
1461 if (!v) {
1462 errno = ENOENT;
1463 return -1;
1464 }
1465
San Mehata2677e42009-12-13 10:40:18 -08001466 return v->mountVol();
1467}
1468
Kenny Root508c0e12010-07-12 09:59:49 -07001469int VolumeManager::listMountedObbs(SocketClient* cli) {
Yabin Cuid1104f72015-01-02 13:28:28 -08001470 FILE *fp = setmntent("/proc/mounts", "r");
1471 if (fp == NULL) {
Kenny Root508c0e12010-07-12 09:59:49 -07001472 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
1473 return -1;
1474 }
1475
1476 // Create a string to compare against that has a trailing slash
Kenny Root93ecb382012-08-09 11:28:37 -07001477 int loopDirLen = strlen(Volume::LOOPDIR);
Kenny Root508c0e12010-07-12 09:59:49 -07001478 char loopDir[loopDirLen + 2];
1479 strcpy(loopDir, Volume::LOOPDIR);
1480 loopDir[loopDirLen++] = '/';
1481 loopDir[loopDirLen] = '\0';
1482
Yabin Cuid1104f72015-01-02 13:28:28 -08001483 mntent* mentry;
1484 while ((mentry = getmntent(fp)) != NULL) {
1485 if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
1486 int fd = open(mentry->mnt_fsname, O_RDONLY);
Kenny Root508c0e12010-07-12 09:59:49 -07001487 if (fd >= 0) {
1488 struct loop_info64 li;
1489 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
1490 cli->sendMsg(ResponseCode::AsecListResult,
1491 (const char*) li.lo_file_name, false);
1492 }
1493 close(fd);
1494 }
1495 }
1496 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001497 endmntent(fp);
Kenny Root508c0e12010-07-12 09:59:49 -07001498 return 0;
1499}
1500
San Mehateba65e92010-01-29 05:15:16 -08001501int VolumeManager::shareEnabled(const char *label, const char *method, bool *enabled) {
1502 Volume *v = lookupVolume(label);
1503
1504 if (!v) {
1505 errno = ENOENT;
1506 return -1;
1507 }
1508
1509 if (strcmp(method, "ums")) {
1510 errno = ENOSYS;
1511 return -1;
1512 }
1513
1514 if (v->getState() != Volume::State_Shared) {
San Mehateba65e92010-01-29 05:15:16 -08001515 *enabled = false;
San Mehatb9aed742010-02-04 15:07:01 -08001516 } else {
1517 *enabled = true;
San Mehateba65e92010-01-29 05:15:16 -08001518 }
1519 return 0;
1520}
1521
San Mehata2677e42009-12-13 10:40:18 -08001522int VolumeManager::shareVolume(const char *label, const char *method) {
1523 Volume *v = lookupVolume(label);
1524
1525 if (!v) {
1526 errno = ENOENT;
1527 return -1;
1528 }
1529
1530 /*
1531 * Eventually, we'll want to support additional share back-ends,
1532 * some of which may work while the media is mounted. For now,
1533 * we just support UMS
1534 */
1535 if (strcmp(method, "ums")) {
1536 errno = ENOSYS;
1537 return -1;
1538 }
1539
1540 if (v->getState() == Volume::State_NoMedia) {
1541 errno = ENODEV;
1542 return -1;
1543 }
1544
San Mehat49e2bce2009-10-12 16:29:01 -07001545 if (v->getState() != Volume::State_Idle) {
San Mehata2677e42009-12-13 10:40:18 -08001546 // You need to unmount manually befoe sharing
San Mehat49e2bce2009-10-12 16:29:01 -07001547 errno = EBUSY;
1548 return -1;
1549 }
1550
Ken Sumrall3b170052011-07-11 15:38:57 -07001551 if (mVolManagerDisabled) {
1552 errno = EBUSY;
1553 return -1;
1554 }
1555
Mike Lockwood2dfe2972010-09-17 18:50:51 -04001556 dev_t d = v->getShareDevice();
San Mehata2677e42009-12-13 10:40:18 -08001557 if ((MAJOR(d) == 0) && (MINOR(d) == 0)) {
1558 // This volume does not support raw disk access
1559 errno = EINVAL;
1560 return -1;
1561 }
1562
1563 int fd;
1564 char nodepath[255];
rpcraigd1c226f2012-10-09 06:58:16 -04001565 int written = snprintf(nodepath,
San Mehata2677e42009-12-13 10:40:18 -08001566 sizeof(nodepath), "/dev/block/vold/%d:%d",
Colin Cross346c5b22014-01-22 23:59:41 -08001567 major(d), minor(d));
San Mehata2677e42009-12-13 10:40:18 -08001568
rpcraigd1c226f2012-10-09 06:58:16 -04001569 if ((written < 0) || (size_t(written) >= sizeof(nodepath))) {
1570 SLOGE("shareVolume failed: couldn't construct nodepath");
1571 return -1;
1572 }
1573
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001574 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001575 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001576 return -1;
1577 }
1578
1579 if (write(fd, nodepath, strlen(nodepath)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001580 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001581 close(fd);
1582 return -1;
1583 }
1584
1585 close(fd);
1586 v->handleVolumeShared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001587 if (mUmsSharingCount++ == 0) {
1588 FILE* fp;
1589 mSavedDirtyRatio = -1; // in case we fail
1590 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1591 char line[16];
1592 if (fgets(line, sizeof(line), fp) && sscanf(line, "%d", &mSavedDirtyRatio)) {
1593 fprintf(fp, "%d\n", mUmsDirtyRatio);
1594 } else {
1595 SLOGE("Failed to read dirty_ratio (%s)", strerror(errno));
1596 }
1597 fclose(fp);
1598 } else {
1599 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1600 }
1601 }
San Mehata2677e42009-12-13 10:40:18 -08001602 return 0;
1603}
1604
1605int VolumeManager::unshareVolume(const char *label, const char *method) {
1606 Volume *v = lookupVolume(label);
1607
1608 if (!v) {
1609 errno = ENOENT;
1610 return -1;
1611 }
1612
1613 if (strcmp(method, "ums")) {
1614 errno = ENOSYS;
1615 return -1;
1616 }
1617
1618 if (v->getState() != Volume::State_Shared) {
1619 errno = EINVAL;
1620 return -1;
1621 }
1622
San Mehata2677e42009-12-13 10:40:18 -08001623 int fd;
Mike Lockwood97f2fc12011-06-07 10:51:38 -07001624 if ((fd = open(MASS_STORAGE_FILE_PATH, O_WRONLY)) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001625 SLOGE("Unable to open ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001626 return -1;
1627 }
1628
1629 char ch = 0;
1630 if (write(fd, &ch, 1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -07001631 SLOGE("Unable to write to ums lunfile (%s)", strerror(errno));
San Mehata2677e42009-12-13 10:40:18 -08001632 close(fd);
1633 return -1;
1634 }
1635
1636 close(fd);
1637 v->handleVolumeUnshared();
Mike Lockwooda28056b2010-10-28 15:21:24 -04001638 if (--mUmsSharingCount == 0 && mSavedDirtyRatio != -1) {
1639 FILE* fp;
1640 if ((fp = fopen("/proc/sys/vm/dirty_ratio", "r+"))) {
1641 fprintf(fp, "%d\n", mSavedDirtyRatio);
1642 fclose(fp);
1643 } else {
1644 SLOGE("Failed to open /proc/sys/vm/dirty_ratio (%s)", strerror(errno));
1645 }
1646 mSavedDirtyRatio = -1;
1647 }
San Mehata2677e42009-12-13 10:40:18 -08001648 return 0;
San Mehat49e2bce2009-10-12 16:29:01 -07001649}
1650
Ken Sumrall3b170052011-07-11 15:38:57 -07001651extern "C" int vold_disableVol(const char *label) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001652 VolumeManager *vm = VolumeManager::Instance();
Ken Sumrall3b170052011-07-11 15:38:57 -07001653 vm->disableVolumeManager();
1654 vm->unshareVolume(label, "ums");
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001655 return vm->unmountVolume(label, true, false);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001656}
1657
1658extern "C" int vold_getNumDirectVolumes(void) {
1659 VolumeManager *vm = VolumeManager::Instance();
1660 return vm->getNumDirectVolumes();
1661}
1662
1663int VolumeManager::getNumDirectVolumes(void) {
1664 VolumeCollection::iterator i;
1665 int n=0;
1666
1667 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1668 if ((*i)->getShareDevice() != (dev_t)0) {
1669 n++;
1670 }
1671 }
1672 return n;
1673}
1674
1675extern "C" int vold_getDirectVolumeList(struct volume_info *vol_list) {
1676 VolumeManager *vm = VolumeManager::Instance();
1677 return vm->getDirectVolumeList(vol_list);
1678}
1679
1680int VolumeManager::getDirectVolumeList(struct volume_info *vol_list) {
1681 VolumeCollection::iterator i;
1682 int n=0;
1683 dev_t d;
1684
1685 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
1686 if ((d=(*i)->getShareDevice()) != (dev_t)0) {
1687 (*i)->getVolInfo(&vol_list[n]);
1688 snprintf(vol_list[n].blk_dev, sizeof(vol_list[n].blk_dev),
Colin Cross346c5b22014-01-22 23:59:41 -08001689 "/dev/block/vold/%d:%d", major(d), minor(d));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001690 n++;
1691 }
1692 }
1693
1694 return 0;
1695}
1696
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001697int VolumeManager::unmountVolume(const char *label, bool force, bool revert) {
San Mehat49e2bce2009-10-12 16:29:01 -07001698 Volume *v = lookupVolume(label);
1699
1700 if (!v) {
1701 errno = ENOENT;
1702 return -1;
1703 }
1704
San Mehata2677e42009-12-13 10:40:18 -08001705 if (v->getState() == Volume::State_NoMedia) {
1706 errno = ENODEV;
1707 return -1;
1708 }
1709
San Mehat49e2bce2009-10-12 16:29:01 -07001710 if (v->getState() != Volume::State_Mounted) {
San Mehat97ac40e2010-03-24 10:24:19 -07001711 SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
San Mehata2677e42009-12-13 10:40:18 -08001712 v->getState());
San Mehat49e2bce2009-10-12 16:29:01 -07001713 errno = EBUSY;
Ken Sumrall319b1042011-06-14 14:01:55 -07001714 return UNMOUNT_NOT_MOUNTED_ERR;
San Mehat49e2bce2009-10-12 16:29:01 -07001715 }
1716
San Mehat1a06eda2010-04-15 12:58:50 -07001717 cleanupAsec(v, force);
San Mehat88705162010-01-15 09:26:28 -08001718
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001719 return v->unmountVol(force, revert);
San Mehat49e2bce2009-10-12 16:29:01 -07001720}
1721
Ken Sumrall425524d2012-06-14 20:55:28 -07001722extern "C" int vold_unmountAllAsecs(void) {
1723 int rc;
1724
1725 VolumeManager *vm = VolumeManager::Instance();
1726 rc = vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
1727 if (vm->unmountAllAsecsInDir(Volume::SEC_ASECDIR_INT)) {
1728 rc = -1;
1729 }
1730 return rc;
1731}
1732
1733#define ID_BUF_LEN 256
1734#define ASEC_SUFFIX ".asec"
1735#define ASEC_SUFFIX_LEN (sizeof(ASEC_SUFFIX) - 1)
1736int VolumeManager::unmountAllAsecsInDir(const char *directory) {
1737 DIR *d = opendir(directory);
1738 int rc = 0;
1739
1740 if (!d) {
1741 SLOGE("Could not open asec dir %s", directory);
1742 return -1;
1743 }
1744
1745 size_t dirent_len = offsetof(struct dirent, d_name) +
Elliott Hughes8c480f72012-10-26 16:57:19 -07001746 fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
Ken Sumrall425524d2012-06-14 20:55:28 -07001747
1748 struct dirent *dent = (struct dirent *) malloc(dirent_len);
1749 if (dent == NULL) {
1750 SLOGE("Failed to allocate memory for asec dir");
1751 return -1;
1752 }
1753
1754 struct dirent *result;
1755 while (!readdir_r(d, dent, &result) && result != NULL) {
1756 if (dent->d_name[0] == '.')
1757 continue;
1758 if (dent->d_type != DT_REG)
1759 continue;
1760 size_t name_len = strlen(dent->d_name);
1761 if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
1762 !strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
1763 char id[ID_BUF_LEN];
1764 strlcpy(id, dent->d_name, name_len - 4);
1765 if (unmountAsec(id, true)) {
1766 /* Register the error, but try to unmount more asecs */
1767 rc = -1;
1768 }
1769 }
1770 }
1771 closedir(d);
1772
1773 free(dent);
1774
1775 return rc;
1776}
1777
San Mehata2677e42009-12-13 10:40:18 -08001778/*
1779 * Looks up a volume by it's label or mount-point
1780 */
San Mehat49e2bce2009-10-12 16:29:01 -07001781Volume *VolumeManager::lookupVolume(const char *label) {
1782 VolumeCollection::iterator i;
1783
1784 for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
San Mehata2677e42009-12-13 10:40:18 -08001785 if (label[0] == '/') {
Jeff Sharkeyba6ae8d2013-07-15 18:14:25 -07001786 if (!strcmp(label, (*i)->getFuseMountpoint()))
San Mehata2677e42009-12-13 10:40:18 -08001787 return (*i);
1788 } else {
1789 if (!strcmp(label, (*i)->getLabel()))
1790 return (*i);
1791 }
San Mehat49e2bce2009-10-12 16:29:01 -07001792 }
1793 return NULL;
1794}
San Mehata19b2502010-01-06 10:33:53 -08001795
1796bool VolumeManager::isMountpointMounted(const char *mp)
1797{
Yabin Cuid1104f72015-01-02 13:28:28 -08001798 FILE *fp = setmntent("/proc/mounts", "r");
1799 if (fp == NULL) {
San Mehat97ac40e2010-03-24 10:24:19 -07001800 SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
San Mehata19b2502010-01-06 10:33:53 -08001801 return false;
1802 }
1803
Yabin Cuid1104f72015-01-02 13:28:28 -08001804 bool found_mp = false;
1805 mntent* mentry;
1806 while ((mentry = getmntent(fp)) != NULL) {
1807 if (strcmp(mentry->mnt_dir, mp) == 0) {
1808 found_mp = true;
1809 break;
San Mehata19b2502010-01-06 10:33:53 -08001810 }
San Mehata19b2502010-01-06 10:33:53 -08001811 }
Yabin Cuid1104f72015-01-02 13:28:28 -08001812 endmntent(fp);
1813 return found_mp;
San Mehata19b2502010-01-06 10:33:53 -08001814}
1815
San Mehat1a06eda2010-04-15 12:58:50 -07001816int VolumeManager::cleanupAsec(Volume *v, bool force) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001817 int rc = 0;
Kenny Root93ecb382012-08-09 11:28:37 -07001818
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001819 char asecFileName[255];
1820
1821 AsecIdCollection removeAsec;
1822 AsecIdCollection removeObb;
1823
Kenny Root93ecb382012-08-09 11:28:37 -07001824 for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
1825 ++it) {
Kenny Rootcbacf782010-09-24 15:11:48 -07001826 ContainerData* cd = *it;
Kenny Root93ecb382012-08-09 11:28:37 -07001827
Kenny Rootcbacf782010-09-24 15:11:48 -07001828 if (cd->type == ASEC) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001829 if (findAsec(cd->id, asecFileName, sizeof(asecFileName))) {
1830 SLOGE("Couldn't find ASEC %s; cleaning up", cd->id);
1831 removeAsec.push_back(cd);
1832 } else {
1833 SLOGD("Found ASEC at path %s", asecFileName);
1834 if (!strncmp(asecFileName, Volume::SEC_ASECDIR_EXT,
1835 strlen(Volume::SEC_ASECDIR_EXT))) {
1836 removeAsec.push_back(cd);
1837 }
1838 }
Kenny Rootcbacf782010-09-24 15:11:48 -07001839 } else if (cd->type == OBB) {
Kenny Root93ecb382012-08-09 11:28:37 -07001840 if (v == getVolumeForFile(cd->id)) {
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001841 removeObb.push_back(cd);
Kenny Rootcbacf782010-09-24 15:11:48 -07001842 }
1843 } else {
1844 SLOGE("Unknown container type %d!", cd->type);
San Mehat1a06eda2010-04-15 12:58:50 -07001845 }
1846 }
Kenny Root93ecb382012-08-09 11:28:37 -07001847
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001848 for (AsecIdCollection::iterator it = removeAsec.begin(); it != removeAsec.end(); ++it) {
Kenny Root93ecb382012-08-09 11:28:37 -07001849 ContainerData *cd = *it;
Jeff Sharkey8c2c15b2013-10-17 15:17:19 -07001850 SLOGI("Unmounting ASEC %s (dependent on %s)", cd->id, v->getLabel());
1851 if (unmountAsec(cd->id, force)) {
1852 SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
1853 rc = -1;
1854 }
1855 }
1856
1857 for (AsecIdCollection::iterator it = removeObb.begin(); it != removeObb.end(); ++it) {
1858 ContainerData *cd = *it;
1859 SLOGI("Unmounting OBB %s (dependent on %s)", cd->id, v->getLabel());
Kenny Root93ecb382012-08-09 11:28:37 -07001860 if (unmountObb(cd->id, force)) {
1861 SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
1862 rc = -1;
1863 }
1864 }
1865
1866 return rc;
San Mehat1a06eda2010-04-15 12:58:50 -07001867}
1868
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001869int VolumeManager::mkdirs(char* path) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001870 // Require that path lives under a volume we manage and is mounted
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001871 const char* emulated_source = getenv("EMULATED_STORAGE_SOURCE");
1872 const char* root = NULL;
Marco Nelissen5ab02e72013-10-15 15:22:28 -07001873 if (emulated_source && !strncmp(path, emulated_source, strlen(emulated_source))) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001874 root = emulated_source;
1875 } else {
1876 Volume* vol = getVolumeForFile(path);
Cylen Yao27cfee32014-05-02 19:23:42 +08001877 if (vol && vol->getState() == Volume::State_Mounted) {
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001878 root = vol->getMountpoint();
1879 }
1880 }
1881
1882 if (!root) {
Cylen Yao27cfee32014-05-02 19:23:42 +08001883 SLOGE("Failed to find mounted volume for %s", path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -07001884 return -EINVAL;
1885 }
1886
1887 /* fs_mkdirs() does symlink checking and relative path enforcement */
1888 return fs_mkdirs(path, 0700);
1889}