blob: e1606a3c048796564365200eaccc214a6faa092d [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());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080053}
54
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070055PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080056
57status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060058 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060059
Jeff Sharkey814e9d32017-09-13 11:49:44 -060060 auto listener = getListener();
61 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060062
Jeff Sharkey36801cc2015-03-13 16:09:20 -070063 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080064}
65
66status_t PublicVolume::initAsecStage() {
67 std::string legacyPath(mRawPath + "/android_secure");
68 std::string securePath(mRawPath + "/.android_secure");
69
70 // Recover legacy secure path
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070071 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070073 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074 }
75 }
76
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
78 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070079 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080 return -errno;
81 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082 }
83
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084 BindMount(securePath, kAsecPath);
85
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086 return OK;
87}
88
Jeff Sharkey9c484982015-03-31 10:35:33 -070089status_t PublicVolume::doCreate() {
90 return CreateDeviceNode(mDevPath, mDevice);
91}
92
93status_t PublicVolume::doDestroy() {
94 return DestroyDeviceNode(mDevPath);
95}
96
Jeff Sharkeydeb24052015-03-02 21:01:40 -080097status_t PublicVolume::doMount() {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010098 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey9c484982015-03-31 10:35:33 -070099 readMetadata();
100
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900101 if (mFsType == "vfat" && vfat::IsSupported()) {
102 if (vfat::Check(mDevPath)) {
103 LOG(ERROR) << getId() << " failed filesystem check";
104 return -EIO;
105 }
106 } else if (mFsType == "exfat" && exfat::IsSupported()) {
107 if (exfat::Check(mDevPath)) {
108 LOG(ERROR) << getId() << " failed filesystem check";
109 return -EIO;
110 }
111 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700112 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
113 return -EIO;
114 }
115
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 // Use UUID as stable name, if available
117 std::string stableName = getId();
118 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700119 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700120 }
121
122 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700123
Martijn Coenenadcc8452019-12-09 14:18:01 +0100124 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
125 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
126 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
127 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700128
129 setInternalPath(mRawPath);
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100130 if (isVisible) {
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700131 setPath(StringPrintf("/storage/%s", stableName.c_str()));
132 } else {
133 setPath(mRawPath);
134 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700135
Qin Chaoe0074f12015-12-15 15:20:41 +0800136 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700137 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138 return -errno;
139 }
140
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900141 if (mFsType == "vfat") {
142 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
143 true)) {
144 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
145 return -EIO;
146 }
147 } else if (mFsType == "exfat") {
148 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
149 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
150 return -EIO;
151 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800152 }
153
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700154 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 initAsecStage();
156 }
157
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100158 if (!isVisible) {
Martijn Coenenadcc8452019-12-09 14:18:01 +0100159 // Not visible to apps, so no need to spin up sdcardfs or FUSE
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700160 return OK;
161 }
162
Martijn Coenenadcc8452019-12-09 14:18:01 +0100163 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
164 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
165 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
166 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
167 PLOG(ERROR) << getId() << " failed to create sdcardfs mount points";
Qin Chaoe0074f12015-12-15 15:20:41 +0800168 return -errno;
169 }
170
Martijn Coenenadcc8452019-12-09 14:18:01 +0100171 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700172
Martijn Coenenadcc8452019-12-09 14:18:01 +0100173 int sdcardFsPid;
174 if (!(sdcardFsPid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700175 if (getMountFlags() & MountFlags::kPrimary) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700176 // clang-format off
Martijn Coenenadcc8452019-12-09 14:18:01 +0100177 if (execl(kSdcardFsPath, kSdcardFsPath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800178 "-u", "1023", // AID_MEDIA_RW
179 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700180 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700181 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800182 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700183 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700185 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700186 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800187 }
188 } else {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700189 // clang-format off
Martijn Coenenadcc8452019-12-09 14:18:01 +0100190 if (execl(kSdcardFsPath, kSdcardFsPath,
Sudheer Shanka55049012019-01-09 12:15:15 -0800191 "-u", "1023", // AID_MEDIA_RW
192 "-g", "1023", // AID_MEDIA_RW
193 "-U", std::to_string(getMountUserId()).c_str(),
194 mRawPath.c_str(),
195 stableName.c_str(),
196 NULL)) {
197 // clang-format on
198 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800199 }
200 }
201
Martijn Coenenadcc8452019-12-09 14:18:01 +0100202 LOG(ERROR) << "sdcardfs exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800203 _exit(1);
204 }
205
Martijn Coenenadcc8452019-12-09 14:18:01 +0100206 if (sdcardFsPid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700207 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800208 return -errno;
209 }
210
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600211 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Martijn Coenenadcc8452019-12-09 14:18:01 +0100212 while (before == GetDevice(mSdcardFsFull)) {
213 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700214 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600215
216 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
217 if (nanoseconds_to_milliseconds(now - start) > 5000) {
Martijn Coenenadcc8452019-12-09 14:18:01 +0100218 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600219 return -ETIMEDOUT;
220 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700221 }
Martijn Coenenadcc8452019-12-09 14:18:01 +0100222 /* sdcardfs will have exited already. The filesystem will still be running */
223 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700224
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100225 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
226 if (isFuse) {
227 // We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend
228 // on sdcardfs being up.
229 LOG(INFO) << "Mounting public fuse volume";
230 android::base::unique_fd fd;
231 int user_id = getMountUserId();
232 int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd);
233
234 if (result != 0) {
235 LOG(ERROR) << "Failed to mount public fuse volume";
236 return -result;
237 }
238
239 mFuseMounted = true;
240 auto callback = getMountCallback();
241 if (callback) {
242 bool is_ready = false;
243 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
244 if (!is_ready) {
245 return -EIO;
246 }
247 }
248 }
249
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800250 return OK;
251}
252
253status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700254 // Unmount the storage before we kill the FUSE process. If we kill
255 // the FUSE process first, most file system operations will return
256 // ENOTCONN until the unmount completes. This is an exotic and unusual
257 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600258 KillProcessesUsingPath(getPath());
259
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100260 if (mFuseMounted) {
Zim3623a212019-07-19 16:46:53 +0100261 // Use UUID as stable name, if available
262 std::string stableName = getId();
263 if (!mFsUuid.empty()) {
264 stableName = mFsUuid;
265 }
266
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100267 if (UnmountUserFuse(getMountUserId(), getInternalPath(), stableName) != OK) {
268 PLOG(INFO) << "UnmountUserFuse failed on public fuse volume";
269 return -errno;
270 }
271
Zima438b242019-09-25 14:37:38 +0100272 std::string fuse_path(
273 StringPrintf("/mnt/user/%d/%s", getMountUserId(), stableName.c_str()));
274 std::string pass_through_path(
275 StringPrintf("/mnt/pass_through/%d/%s", getMountUserId(), stableName.c_str()));
Zima438b242019-09-25 14:37:38 +0100276 rmdir(fuse_path.c_str());
277 rmdir(pass_through_path.c_str());
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100278
279 mFuseMounted = false;
Zim3623a212019-07-19 16:46:53 +0100280 }
281
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700282 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700283
Martijn Coenenadcc8452019-12-09 14:18:01 +0100284 ForceUnmount(mSdcardFsDefault);
285 ForceUnmount(mSdcardFsRead);
286 ForceUnmount(mSdcardFsWrite);
287 ForceUnmount(mSdcardFsFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800288 ForceUnmount(mRawPath);
289
Martijn Coenenadcc8452019-12-09 14:18:01 +0100290 rmdir(mSdcardFsDefault.c_str());
291 rmdir(mSdcardFsRead.c_str());
292 rmdir(mSdcardFsWrite.c_str());
293 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700294 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700295
Martijn Coenenadcc8452019-12-09 14:18:01 +0100296 mSdcardFsDefault.clear();
297 mSdcardFsRead.clear();
298 mSdcardFsWrite.clear();
299 mSdcardFsFull.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700300 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800301
302 return OK;
303}
304
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700305status_t PublicVolume::doFormat(const std::string& fsType) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200306 bool useVfat = vfat::IsSupported();
307 bool useExfat = exfat::IsSupported();
308 status_t res = OK;
309
310 // Resolve the target filesystem type
311 if (fsType == "auto" && useVfat && useExfat) {
312 uint64_t size = 0;
313
314 res = GetBlockDevSize(mDevPath, &size);
315 if (res != OK) {
316 LOG(ERROR) << "Couldn't get device size " << mDevPath;
317 return res;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700318 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200319
320 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
321 if (size > 32896LL * 1024 * 1024) {
322 useVfat = false;
323 } else {
324 useExfat = false;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700325 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200326 } else if (fsType == "vfat") {
327 useExfat = false;
328 } else if (fsType == "exfat") {
329 useVfat = false;
330 }
331
332 if (!useVfat && !useExfat) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700333 LOG(ERROR) << "Unsupported filesystem " << fsType;
334 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800335 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700336
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200337 if (WipeBlockDevice(mDevPath) != OK) {
338 LOG(WARNING) << getId() << " failed to wipe";
339 }
340
341 if (useVfat) {
342 res = vfat::Format(mDevPath, 0);
343 } else if (useExfat) {
344 res = exfat::Format(mDevPath);
345 }
346
347 if (res != OK) {
348 LOG(ERROR) << getId() << " failed to format";
349 res = -errno;
350 }
351
352 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800353}
354
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800355} // namespace vold
356} // namespace android