xref: /aosp_15_r20/system/vold/model/PrivateVolume.cpp (revision f40fafd4c6c2594924d919feffc1a1fd6e3b30f3)
1 /*
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 
17 #include "PrivateVolume.h"
18 #include "EmulatedVolume.h"
19 #include "Utils.h"
20 #include "VolumeEncryption.h"
21 #include "VolumeManager.h"
22 #include "fs/Ext4.h"
23 #include "fs/F2fs.h"
24 
25 #include <android-base/logging.h>
26 #include <android-base/stringprintf.h>
27 #include <cutils/fs.h>
28 #include <libdm/dm.h>
29 #include <private/android_filesystem_config.h>
30 
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <sys/mount.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/sysmacros.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <thread>
40 
41 using android::base::StringPrintf;
42 using android::vold::IsVirtioBlkDevice;
43 
44 namespace android {
45 namespace vold {
46 
47 static const unsigned int kMajorBlockLoop = 7;
48 static const unsigned int kMajorBlockHdd = 8;
49 static const unsigned int kMajorBlockMmc = 179;
50 
PrivateVolume(dev_t device,const KeyBuffer & keyRaw)51 PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
52     : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
53     setId(StringPrintf("private:%u,%u", major(device), minor(device)));
54     mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
55 }
56 
~PrivateVolume()57 PrivateVolume::~PrivateVolume() {}
58 
readMetadata()59 status_t PrivateVolume::readMetadata() {
60     status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
61 
62     auto listener = getListener();
63     if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
64 
65     return res;
66 }
67 
doCreate()68 status_t PrivateVolume::doCreate() {
69     if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
70         return -EIO;
71     }
72 
73     // Recover from stale vold by tearing down any old mappings
74     auto& dm = dm::DeviceMapper::Instance();
75     // TODO(b/149396179) there appears to be a race somewhere in the system where trying
76     // to delete the device fails with EBUSY; for now, work around this by retrying.
77     bool ret;
78     int tries = 10;
79     while (tries-- > 0) {
80         ret = dm.DeleteDeviceIfExists(getId());
81         if (ret || errno != EBUSY) {
82             break;
83         }
84         PLOG(ERROR) << "Cannot remove dm device " << getId();
85         std::this_thread::sleep_for(std::chrono::milliseconds(100));
86     }
87     if (!ret) {
88         return -EIO;
89     }
90 
91     // TODO: figure out better SELinux labels for private volumes
92 
93     if (!setup_ext_volume(getId(), mRawDevPath, mKeyRaw, &mDmDevPath)) {
94         LOG(ERROR) << getId() << " failed to setup metadata encryption";
95         return -EIO;
96     }
97 
98     return OK;
99 }
100 
doDestroy()101 status_t PrivateVolume::doDestroy() {
102     auto& dm = dm::DeviceMapper::Instance();
103     // TODO(b/149396179) there appears to be a race somewhere in the system where trying
104     // to delete the device fails with EBUSY; for now, work around this by retrying.
105     bool ret;
106     int tries = 10;
107     while (tries-- > 0) {
108         ret = dm.DeleteDevice(getId());
109         if (ret || errno != EBUSY) {
110             break;
111         }
112         PLOG(ERROR) << "Cannot remove dm device " << getId();
113         std::this_thread::sleep_for(std::chrono::milliseconds(100));
114     }
115     if (!ret) {
116         return -EIO;
117     }
118     return DestroyDeviceNode(mRawDevPath);
119 }
120 
doMount()121 status_t PrivateVolume::doMount() {
122     if (readMetadata()) {
123         LOG(ERROR) << getId() << " failed to read metadata";
124         return -EIO;
125     }
126 
127     mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
128     setPath(mPath);
129 
130     if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
131         PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
132         return -EIO;
133     }
134 
135     if (mFsType == "ext4") {
136         int res = ext4::Check(mDmDevPath, mPath);
137         if (res == 0 || res == 1) {
138             LOG(DEBUG) << getId() << " passed filesystem check";
139         } else {
140             PLOG(ERROR) << getId() << " failed filesystem check";
141             return -EIO;
142         }
143 
144         if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
145             PLOG(ERROR) << getId() << " failed to mount";
146             return -EIO;
147         }
148 
149     } else if (mFsType == "f2fs") {
150         int res = f2fs::Check(mDmDevPath);
151         if (res == 0) {
152             LOG(DEBUG) << getId() << " passed filesystem check";
153         } else {
154             PLOG(ERROR) << getId() << " failed filesystem check";
155             return -EIO;
156         }
157 
158         if (f2fs::Mount(mDmDevPath, mPath)) {
159             PLOG(ERROR) << getId() << " failed to mount";
160             return -EIO;
161         }
162 
163     } else {
164         LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
165         return -EIO;
166     }
167 
168     RestoreconRecursive(mPath);
169 
170     int attrs = 0;
171     if (!IsSdcardfsUsed()) attrs = FS_CASEFOLD_FL;
172 
173     // Verify that common directories are ready to roll
174     if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
175         PrepareDir(mPath + "/user", 0511, AID_SYSTEM, AID_SYSTEM) ||
176         PrepareDir(mPath + "/user_de", 0511, AID_SYSTEM, AID_SYSTEM) ||
177         PrepareDir(mPath + "/misc_ce", 0511, AID_SYSTEM, AID_SYSTEM) ||
178         PrepareDir(mPath + "/misc_de", 0511, AID_SYSTEM, AID_SYSTEM) ||
179         PrepareDir(mPath + "/media", 0550, AID_MEDIA_RW, AID_MEDIA_RW, attrs) ||
180         PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
181         PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
182         PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
183         PLOG(ERROR) << getId() << " failed to prepare";
184         return -EIO;
185     }
186 
187     return OK;
188 }
189 
doPostMount()190 void PrivateVolume::doPostMount() {
191     auto vol_manager = VolumeManager::Instance();
192     std::string mediaPath(mPath + "/media");
193 
194     // Create a new emulated volume stacked above us for all added users, they will automatically
195     // be destroyed during unmount
196     for (userid_t user : vol_manager->getStartedUsers()) {
197         auto vol = std::shared_ptr<VolumeBase>(
198                 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
199         vol->setMountUserId(user);
200         addVolume(vol);
201         vol->create();
202     }
203 }
204 
doUnmount()205 status_t PrivateVolume::doUnmount() {
206     ForceUnmount(mPath);
207 
208     if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
209         PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
210     }
211 
212     return OK;
213 }
214 
doFormat(const std::string & fsType)215 status_t PrivateVolume::doFormat(const std::string& fsType) {
216     std::string resolvedFsType = fsType;
217     if (fsType == "auto") {
218         // For now, assume that all MMC devices are flash-based SD cards, and
219         // give everyone else ext4 because sysfs rotational isn't reliable.
220         // Additionally, prefer f2fs for loop-based devices
221         if ((major(mRawDevice) == kMajorBlockMmc ||
222              major(mRawDevice) == kMajorBlockHdd ||
223              major(mRawDevice) == kMajorBlockLoop ||
224              IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) {
225             resolvedFsType = "f2fs";
226         } else {
227             resolvedFsType = "ext4";
228         }
229         LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
230     }
231 
232     if (resolvedFsType == "ext4") {
233         // TODO: change reported mountpoint once we have better selinux support
234         if (ext4::Format(mDmDevPath, 0, "/data")) {
235             PLOG(ERROR) << getId() << " failed to format";
236             return -EIO;
237         }
238     } else if (resolvedFsType == "f2fs") {
239         if (f2fs::Format(mDmDevPath, false, {}, {})) {
240             PLOG(ERROR) << getId() << " failed to format";
241             return -EIO;
242         }
243     } else {
244         LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
245         return -EINVAL;
246     }
247 
248     return OK;
249 }
250 
251 }  // namespace vold
252 }  // namespace android
253