blob: b246c9597f6d92ab797fc3dd0ec1286ee4b605d4 [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;
Martijn Coenen86f21a22020-01-06 09:48:14 +010054 mUseSdcardFs = IsFilesystemSupported("sdcardfs");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080055}
56
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070057PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080058
59status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060060 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060061
Jeff Sharkey814e9d32017-09-13 11:49:44 -060062 auto listener = getListener();
63 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060064
Jeff Sharkey36801cc2015-03-13 16:09:20 -070065 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080066}
67
68status_t PublicVolume::initAsecStage() {
69 std::string legacyPath(mRawPath + "/android_secure");
70 std::string securePath(mRawPath + "/.android_secure");
71
72 // Recover legacy secure path
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070073 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070075 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076 }
77 }
78
Jeff Sharkey36801cc2015-03-13 16:09:20 -070079 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
80 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070081 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082 return -errno;
83 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080084 }
85
Jeff Sharkey36801cc2015-03-13 16:09:20 -070086 BindMount(securePath, kAsecPath);
87
Jeff Sharkeydeb24052015-03-02 21:01:40 -080088 return OK;
89}
90
Jeff Sharkey9c484982015-03-31 10:35:33 -070091status_t PublicVolume::doCreate() {
92 return CreateDeviceNode(mDevPath, mDevice);
93}
94
95status_t PublicVolume::doDestroy() {
96 return DestroyDeviceNode(mDevPath);
97}
98
Jeff Sharkeydeb24052015-03-02 21:01:40 -080099status_t PublicVolume::doMount() {
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100100 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700101 readMetadata();
102
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900103 if (mFsType == "vfat" && vfat::IsSupported()) {
104 if (vfat::Check(mDevPath)) {
105 LOG(ERROR) << getId() << " failed filesystem check";
106 return -EIO;
107 }
108 } else if (mFsType == "exfat" && exfat::IsSupported()) {
109 if (exfat::Check(mDevPath)) {
110 LOG(ERROR) << getId() << " failed filesystem check";
111 return -EIO;
112 }
113 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700114 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
115 return -EIO;
116 }
117
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700118 // Use UUID as stable name, if available
119 std::string stableName = getId();
120 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700121 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700122 }
123
124 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700125
Martijn Coenenadcc8452019-12-09 14:18:01 +0100126 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
127 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
128 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
129 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700130
131 setInternalPath(mRawPath);
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100132 if (isVisible) {
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700133 setPath(StringPrintf("/storage/%s", stableName.c_str()));
134 } else {
135 setPath(mRawPath);
136 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700137
Qin Chaoe0074f12015-12-15 15:20:41 +0800138 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700139 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800140 return -errno;
141 }
142
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900143 if (mFsType == "vfat") {
144 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
145 true)) {
146 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
147 return -EIO;
148 }
149 } else if (mFsType == "exfat") {
150 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
151 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
152 return -EIO;
153 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800154 }
155
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700156 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700157 initAsecStage();
158 }
159
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100160 if (!isVisible) {
Martijn Coenenadcc8452019-12-09 14:18:01 +0100161 // Not visible to apps, so no need to spin up sdcardfs or FUSE
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700162 return OK;
163 }
164
Martijn Coenen86f21a22020-01-06 09:48:14 +0100165 if (mUseSdcardFs) {
166 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
167 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
168 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
169 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
170 PLOG(ERROR) << getId() << " failed to create sdcardfs mount points";
171 return -errno;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800172 }
173
Martijn Coenen86f21a22020-01-06 09:48:14 +0100174 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800175
Martijn Coenen86f21a22020-01-06 09:48:14 +0100176 int sdcardFsPid;
177 if (!(sdcardFsPid = fork())) {
178 if (getMountFlags() & MountFlags::kPrimary) {
179 // clang-format off
180 if (execl(kSdcardFsPath, kSdcardFsPath,
181 "-u", "1023", // AID_MEDIA_RW
182 "-g", "1023", // AID_MEDIA_RW
183 "-U", std::to_string(getMountUserId()).c_str(),
184 "-w",
185 mRawPath.c_str(),
186 stableName.c_str(),
187 NULL)) {
188 // clang-format on
189 PLOG(ERROR) << "Failed to exec";
190 }
191 } else {
192 // clang-format off
193 if (execl(kSdcardFsPath, kSdcardFsPath,
194 "-u", "1023", // AID_MEDIA_RW
195 "-g", "1023", // AID_MEDIA_RW
196 "-U", std::to_string(getMountUserId()).c_str(),
197 mRawPath.c_str(),
198 stableName.c_str(),
199 NULL)) {
200 // clang-format on
201 PLOG(ERROR) << "Failed to exec";
202 }
203 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800204
Martijn Coenen86f21a22020-01-06 09:48:14 +0100205 LOG(ERROR) << "sdcardfs exiting";
206 _exit(1);
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600207 }
Martijn Coenen86f21a22020-01-06 09:48:14 +0100208
209 if (sdcardFsPid == -1) {
210 PLOG(ERROR) << getId() << " failed to fork";
211 return -errno;
212 }
213
214 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
215 while (before == GetDevice(mSdcardFsFull)) {
216 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
217 usleep(50000); // 50ms
218
219 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
220 if (nanoseconds_to_milliseconds(now - start) > 5000) {
221 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
222 return -ETIMEDOUT;
223 }
224 }
225 /* sdcardfs will have exited already. The filesystem will still be running */
226 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700227 }
228
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000229 bool isFuse = base::GetBoolProperty(kPropFuse, false);
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100230 if (isFuse) {
231 // We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend
232 // on sdcardfs being up.
233 LOG(INFO) << "Mounting public fuse volume";
234 android::base::unique_fd fd;
235 int user_id = getMountUserId();
236 int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd);
237
238 if (result != 0) {
239 LOG(ERROR) << "Failed to mount public fuse volume";
240 return -result;
241 }
242
243 mFuseMounted = true;
244 auto callback = getMountCallback();
245 if (callback) {
246 bool is_ready = false;
247 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
248 if (!is_ready) {
249 return -EIO;
250 }
251 }
252 }
253
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800254 return OK;
255}
256
257status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700258 // Unmount the storage before we kill the FUSE process. If we kill
259 // the FUSE process first, most file system operations will return
260 // ENOTCONN until the unmount completes. This is an exotic and unusual
261 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600262 KillProcessesUsingPath(getPath());
263
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100264 if (mFuseMounted) {
Zim3623a212019-07-19 16:46:53 +0100265 // Use UUID as stable name, if available
266 std::string stableName = getId();
267 if (!mFsUuid.empty()) {
268 stableName = mFsUuid;
269 }
270
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100271 if (UnmountUserFuse(getMountUserId(), getInternalPath(), stableName) != OK) {
272 PLOG(INFO) << "UnmountUserFuse failed on public fuse volume";
273 return -errno;
274 }
275
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100276 mFuseMounted = false;
Zim3623a212019-07-19 16:46:53 +0100277 }
278
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700279 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700280
Martijn Coenen86f21a22020-01-06 09:48:14 +0100281 if (mUseSdcardFs) {
282 ForceUnmount(mSdcardFsDefault);
283 ForceUnmount(mSdcardFsRead);
284 ForceUnmount(mSdcardFsWrite);
285 ForceUnmount(mSdcardFsFull);
286
287 rmdir(mSdcardFsDefault.c_str());
288 rmdir(mSdcardFsRead.c_str());
289 rmdir(mSdcardFsWrite.c_str());
290 rmdir(mSdcardFsFull.c_str());
291
292 mSdcardFsDefault.clear();
293 mSdcardFsRead.clear();
294 mSdcardFsWrite.clear();
295 mSdcardFsFull.clear();
296 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800297 ForceUnmount(mRawPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700298 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700299 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800300
301 return OK;
302}
303
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700304status_t PublicVolume::doFormat(const std::string& fsType) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200305 bool useVfat = vfat::IsSupported();
306 bool useExfat = exfat::IsSupported();
307 status_t res = OK;
308
309 // Resolve the target filesystem type
310 if (fsType == "auto" && useVfat && useExfat) {
311 uint64_t size = 0;
312
313 res = GetBlockDevSize(mDevPath, &size);
314 if (res != OK) {
315 LOG(ERROR) << "Couldn't get device size " << mDevPath;
316 return res;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700317 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200318
319 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
320 if (size > 32896LL * 1024 * 1024) {
321 useVfat = false;
322 } else {
323 useExfat = false;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700324 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200325 } else if (fsType == "vfat") {
326 useExfat = false;
327 } else if (fsType == "exfat") {
328 useVfat = false;
329 }
330
331 if (!useVfat && !useExfat) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700332 LOG(ERROR) << "Unsupported filesystem " << fsType;
333 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800334 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700335
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200336 if (WipeBlockDevice(mDevPath) != OK) {
337 LOG(WARNING) << getId() << " failed to wipe";
338 }
339
340 if (useVfat) {
341 res = vfat::Format(mDevPath, 0);
342 } else if (useExfat) {
343 res = exfat::Format(mDevPath);
344 }
345
346 if (res != OK) {
347 LOG(ERROR) << getId() << " failed to format";
348 res = -errno;
349 }
350
351 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800352}
353
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800354} // namespace vold
355} // namespace android