blob: b9164f5e19845fb90a03eb7b4252d6aa15f2388b [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
2 * Copyright (C) 2015 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
Jeff Sharkeydeb24052015-03-02 21:01:40 -080017#include "PublicVolume.h"
Zim3623a212019-07-19 16:46:53 +010018
19#include "AppFuseUtil.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020#include "Utils.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070021#include "VolumeManager.h"
Jeff Sharkey37ba1252018-01-19 10:55:18 +090022#include "fs/Exfat.h"
23#include "fs/Vfat.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/logging.h>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070026#include <android-base/properties.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070027#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060030#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031
32#include <fcntl.h>
33#include <stdlib.h>
34#include <sys/mount.h>
35#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070036#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070037#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038#include <sys/wait.h>
39
Sudheer Shanka40ab6742018-09-18 13:07:45 -070040using android::base::GetBoolProperty;
Dan Albertae9e8902015-03-16 10:35:17 -070041using android::base::StringPrintf;
42
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043namespace android {
44namespace vold {
45
Martijn Coenenadcc8452019-12-09 14:18:01 +010046static const char* kSdcardFsPath = "/system/bin/sdcard";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080047
Jeff Sharkey36801cc2015-03-13 16:09:20 -070048static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049
Martijn Coenenadcc8452019-12-09 14:18:01 +010050PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
52 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Martijn Coenenfd7362d2019-12-11 14:57:59 +010053 mFuseMounted = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054}
55
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070056PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057
58status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060059 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060060
Jeff Sharkey814e9d32017-09-13 11:49:44 -060061 auto listener = getListener();
62 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060063
Jeff Sharkey36801cc2015-03-13 16:09:20 -070064 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080065}
66
67status_t PublicVolume::initAsecStage() {
68 std::string legacyPath(mRawPath + "/android_secure");
69 std::string securePath(mRawPath + "/.android_secure");
70
71 // Recover legacy secure path
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070072 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080073 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070074 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075 }
76 }
77
Jeff Sharkey36801cc2015-03-13 16:09:20 -070078 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
79 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070080 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081 return -errno;
82 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 }
84
Jeff Sharkey36801cc2015-03-13 16:09:20 -070085 BindMount(securePath, kAsecPath);
86
Jeff Sharkeydeb24052015-03-02 21:01:40 -080087 return OK;
88}
89
Jeff Sharkey9c484982015-03-31 10:35:33 -070090status_t PublicVolume::doCreate() {
91 return CreateDeviceNode(mDevPath, mDevice);
92}
93
94status_t PublicVolume::doDestroy() {
95 return DestroyDeviceNode(mDevPath);
96}
97
Jeff Sharkeydeb24052015-03-02 21:01:40 -080098status_t PublicVolume::doMount() {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010099 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700100 readMetadata();
101
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900102 if (mFsType == "vfat" && vfat::IsSupported()) {
103 if (vfat::Check(mDevPath)) {
104 LOG(ERROR) << getId() << " failed filesystem check";
105 return -EIO;
106 }
107 } else if (mFsType == "exfat" && exfat::IsSupported()) {
108 if (exfat::Check(mDevPath)) {
109 LOG(ERROR) << getId() << " failed filesystem check";
110 return -EIO;
111 }
112 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700113 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
114 return -EIO;
115 }
116
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700117 // Use UUID as stable name, if available
118 std::string stableName = getId();
119 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700120 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 }
122
123 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700124
Martijn Coenenadcc8452019-12-09 14:18:01 +0100125 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
126 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
127 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
128 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700129
130 setInternalPath(mRawPath);
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100131 if (isVisible) {
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700132 setPath(StringPrintf("/storage/%s", stableName.c_str()));
133 } else {
134 setPath(mRawPath);
135 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700136
Qin Chaoe0074f12015-12-15 15:20:41 +0800137 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700138 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139 return -errno;
140 }
141
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900142 if (mFsType == "vfat") {
143 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
144 true)) {
145 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
146 return -EIO;
147 }
148 } else if (mFsType == "exfat") {
149 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
150 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
151 return -EIO;
152 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800153 }
154
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700155 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156 initAsecStage();
157 }
158
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100159 if (!isVisible) {
Martijn Coenenadcc8452019-12-09 14:18:01 +0100160 // Not visible to apps, so no need to spin up sdcardfs or FUSE
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700161 return OK;
162 }
163
Martijn Coenenadcc8452019-12-09 14:18:01 +0100164 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
165 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
166 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
167 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
168 PLOG(ERROR) << getId() << " failed to create sdcardfs mount points";
Qin Chaoe0074f12015-12-15 15:20:41 +0800169 return -errno;
170 }
171
Martijn Coenenadcc8452019-12-09 14:18:01 +0100172 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700173
Martijn Coenenadcc8452019-12-09 14:18:01 +0100174 int sdcardFsPid;
175 if (!(sdcardFsPid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700176 if (getMountFlags() & MountFlags::kPrimary) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700177 // clang-format off
Martijn Coenenadcc8452019-12-09 14:18:01 +0100178 if (execl(kSdcardFsPath, kSdcardFsPath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800179 "-u", "1023", // AID_MEDIA_RW
180 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700181 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700182 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700184 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700185 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700186 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700187 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800188 }
189 } else {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700190 // clang-format off
Martijn Coenenadcc8452019-12-09 14:18:01 +0100191 if (execl(kSdcardFsPath, kSdcardFsPath,
Sudheer Shanka55049012019-01-09 12:15:15 -0800192 "-u", "1023", // AID_MEDIA_RW
193 "-g", "1023", // AID_MEDIA_RW
194 "-U", std::to_string(getMountUserId()).c_str(),
195 mRawPath.c_str(),
196 stableName.c_str(),
197 NULL)) {
198 // clang-format on
199 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800200 }
201 }
202
Martijn Coenenadcc8452019-12-09 14:18:01 +0100203 LOG(ERROR) << "sdcardfs exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800204 _exit(1);
205 }
206
Martijn Coenenadcc8452019-12-09 14:18:01 +0100207 if (sdcardFsPid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700208 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800209 return -errno;
210 }
211
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600212 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Martijn Coenenadcc8452019-12-09 14:18:01 +0100213 while (before == GetDevice(mSdcardFsFull)) {
214 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700215 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600216
217 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
218 if (nanoseconds_to_milliseconds(now - start) > 5000) {
Martijn Coenenadcc8452019-12-09 14:18:01 +0100219 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600220 return -ETIMEDOUT;
221 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700222 }
Martijn Coenenadcc8452019-12-09 14:18:01 +0100223 /* sdcardfs will have exited already. The filesystem will still be running */
224 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700225
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100226 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
227 if (isFuse) {
228 // We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend
229 // on sdcardfs being up.
230 LOG(INFO) << "Mounting public fuse volume";
231 android::base::unique_fd fd;
232 int user_id = getMountUserId();
233 int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd);
234
235 if (result != 0) {
236 LOG(ERROR) << "Failed to mount public fuse volume";
237 return -result;
238 }
239
240 mFuseMounted = true;
241 auto callback = getMountCallback();
242 if (callback) {
243 bool is_ready = false;
244 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
245 if (!is_ready) {
246 return -EIO;
247 }
248 }
249 }
250
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800251 return OK;
252}
253
254status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700255 // Unmount the storage before we kill the FUSE process. If we kill
256 // the FUSE process first, most file system operations will return
257 // ENOTCONN until the unmount completes. This is an exotic and unusual
258 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600259 KillProcessesUsingPath(getPath());
260
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100261 if (mFuseMounted) {
Zim3623a212019-07-19 16:46:53 +0100262 // Use UUID as stable name, if available
263 std::string stableName = getId();
264 if (!mFsUuid.empty()) {
265 stableName = mFsUuid;
266 }
267
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100268 if (UnmountUserFuse(getMountUserId(), getInternalPath(), stableName) != OK) {
269 PLOG(INFO) << "UnmountUserFuse failed on public fuse volume";
270 return -errno;
271 }
272
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100273 mFuseMounted = false;
Zim3623a212019-07-19 16:46:53 +0100274 }
275
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700276 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700277
Martijn Coenenadcc8452019-12-09 14:18:01 +0100278 ForceUnmount(mSdcardFsDefault);
279 ForceUnmount(mSdcardFsRead);
280 ForceUnmount(mSdcardFsWrite);
281 ForceUnmount(mSdcardFsFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800282 ForceUnmount(mRawPath);
283
Martijn Coenenadcc8452019-12-09 14:18:01 +0100284 rmdir(mSdcardFsDefault.c_str());
285 rmdir(mSdcardFsRead.c_str());
286 rmdir(mSdcardFsWrite.c_str());
287 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700288 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700289
Martijn Coenenadcc8452019-12-09 14:18:01 +0100290 mSdcardFsDefault.clear();
291 mSdcardFsRead.clear();
292 mSdcardFsWrite.clear();
293 mSdcardFsFull.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700294 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800295
296 return OK;
297}
298
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700299status_t PublicVolume::doFormat(const std::string& fsType) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200300 bool useVfat = vfat::IsSupported();
301 bool useExfat = exfat::IsSupported();
302 status_t res = OK;
303
304 // Resolve the target filesystem type
305 if (fsType == "auto" && useVfat && useExfat) {
306 uint64_t size = 0;
307
308 res = GetBlockDevSize(mDevPath, &size);
309 if (res != OK) {
310 LOG(ERROR) << "Couldn't get device size " << mDevPath;
311 return res;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700312 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200313
314 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
315 if (size > 32896LL * 1024 * 1024) {
316 useVfat = false;
317 } else {
318 useExfat = false;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700319 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200320 } else if (fsType == "vfat") {
321 useExfat = false;
322 } else if (fsType == "exfat") {
323 useVfat = false;
324 }
325
326 if (!useVfat && !useExfat) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700327 LOG(ERROR) << "Unsupported filesystem " << fsType;
328 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800329 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700330
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200331 if (WipeBlockDevice(mDevPath) != OK) {
332 LOG(WARNING) << getId() << " failed to wipe";
333 }
334
335 if (useVfat) {
336 res = vfat::Format(mDevPath, 0);
337 } else if (useExfat) {
338 res = exfat::Format(mDevPath);
339 }
340
341 if (res != OK) {
342 LOG(ERROR) << getId() << " failed to format";
343 res = -errno;
344 }
345
346 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800347}
348
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800349} // namespace vold
350} // namespace android