blob: 48d848c0810efaeea1ca70d3c65f997bb0cebdd2 [file] [log] [blame]
Tom Marshalldd9b45d2019-01-04 14:37:31 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 * Copyright (C) 2019 The LineageOS Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <dirent.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <string>
26#include <vector>
27
28#include <sys/mman.h>
29#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <sys/wait.h>
33
34#include <linux/kdev_t.h>
35
36#define LOG_TAG "Vold"
37#include <android-base/logging.h>
38#include <android-base/properties.h>
39#include <android-base/stringprintf.h>
40#include <cutils/log.h>
41#include <cutils/properties.h>
42#include <logwrap/logwrap.h>
43#include <private/android_filesystem_config.h>
44#include <selinux/selinux.h>
45
46#include "Ext4.h"
47#include "Utils.h"
48
49using android::base::StringPrintf;
50
51namespace android {
52namespace volmgr {
53namespace ext4 {
54
55status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
56 bool executable, const std::string& opts /* = "" */, bool trusted, bool portable) {
57 int rc;
58 unsigned long flags;
59
60 std::string data(opts);
61
62 if (portable) {
63 if (!data.empty()) {
64 data += ",";
65 }
66 data += "context=u:object_r:sdcard_posix:s0";
67 }
68 const char* c_source = source.c_str();
69 const char* c_target = target.c_str();
70 const char* c_data = data.c_str();
71
72 flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
73
74 // Only use MS_DIRSYNC if we're not mounting adopted storage
75 if (!trusted) {
76 flags |= MS_DIRSYNC;
77 }
78
79 flags |= (executable ? 0 : MS_NOEXEC);
80 flags |= (ro ? MS_RDONLY : 0);
81 flags |= (remount ? MS_REMOUNT : 0);
82
83 rc = mount(c_source, c_target, "ext4", flags, c_data);
84 if (portable && rc == 0) {
85 chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW);
86 chmod(c_target, 0775);
87 }
88
89 if (rc && errno == EROFS) {
90 SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
91 flags |= MS_RDONLY;
92 rc = mount(c_source, c_target, "ext4", flags, c_data);
93 }
94
95 return rc;
96}
97
98} // namespace ext4
99} // namespace volmgr
100} // namespace android