• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 <dirent.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include <linux/fs.h>
26 #include <sys/ioctl.h>
27 #include <sys/mman.h>
28 #include <sys/mount.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 
33 #include <linux/kdev_t.h>
34 
35 #include <android-base/logging.h>
36 #include <android-base/stringprintf.h>
37 #include <selinux/selinux.h>
38 
39 #include <logwrap/logwrap.h>
40 
41 #include "Utils.h"
42 #include "Vfat.h"
43 #include "VoldUtil.h"
44 
45 using android::base::StringPrintf;
46 
47 namespace android {
48 namespace vold {
49 namespace vfat {
50 
51 static const char* kMkfsPath = "/system/bin/newfs_msdos";
52 static const char* kFsckPath = "/system/bin/fsck_msdos";
53 
IsSupported()54 bool IsSupported() {
55     return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
56            IsFilesystemSupported("vfat");
57 }
58 
Check(const std::string & source)59 status_t Check(const std::string& source) {
60     int pass = 1;
61     int rc = 0;
62     do {
63         std::vector<std::string> cmd;
64         cmd.push_back(kFsckPath);
65         cmd.push_back("-p");
66         cmd.push_back("-f");
67         cmd.push_back("-y");
68         cmd.push_back(source);
69 
70         // Fat devices are currently always untrusted
71         rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
72 
73         if (rc < 0) {
74             LOG(ERROR) << "Filesystem check failed due to logwrap error";
75             errno = EIO;
76             return -1;
77         }
78 
79         switch (rc) {
80             case 0:
81                 LOG(INFO) << "Filesystem check completed OK";
82                 return 0;
83 
84             case 2:
85                 LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
86                 errno = ENODATA;
87                 return -1;
88 
89             case 4:
90                 if (pass++ <= 3) {
91                     LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
92                     continue;
93                 }
94                 LOG(ERROR) << "Failing check after too many rechecks";
95                 errno = EIO;
96                 return -1;
97 
98             case 8:
99                 LOG(ERROR) << "Filesystem check failed (no filesystem)";
100                 errno = ENODATA;
101                 return -1;
102 
103             default:
104                 LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
105                 errno = EIO;
106                 return -1;
107         }
108     } while (0);
109 
110     return 0;
111 }
112 
Mount(const std::string & source,const std::string & target,bool ro,bool remount,bool executable,int ownerUid,int ownerGid,int permMask,bool createLost)113 status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
114                bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) {
115     int rc;
116     unsigned long flags;
117 
118     const char* c_source = source.c_str();
119     const char* c_target = target.c_str();
120 
121     flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME;
122 
123     flags |= (executable ? 0 : MS_NOEXEC);
124     flags |= (ro ? MS_RDONLY : 0);
125     flags |= (remount ? MS_REMOUNT : 0);
126 
127     auto mountData =
128         android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
129                                     ownerUid, ownerGid, permMask, permMask);
130 
131     rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
132 
133     if (rc && errno == EROFS) {
134         LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
135         flags |= MS_RDONLY;
136         rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
137     }
138 
139     if (rc == 0 && createLost) {
140         auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str());
141         if (access(lost_path.c_str(), F_OK)) {
142             /*
143              * Create a LOST.DIR in the root so we have somewhere to put
144              * lost cluster chains (fsck_msdos doesn't currently do this)
145              */
146             if (mkdir(lost_path.c_str(), 0755)) {
147                 PLOG(ERROR) << "Unable to create LOST.DIR";
148             }
149         }
150     }
151 
152     return rc;
153 }
154 
Format(const std::string & source,unsigned long numSectors)155 status_t Format(const std::string& source, unsigned long numSectors) {
156     std::vector<std::string> cmd;
157     cmd.push_back(kMkfsPath);
158     cmd.push_back("-O");
159     cmd.push_back("android");
160     cmd.push_back("-A");
161 
162     if (numSectors) {
163         cmd.push_back("-s");
164         cmd.push_back(StringPrintf("%lu", numSectors));
165     }
166 
167     cmd.push_back(source);
168 
169     int rc = ForkExecvp(cmd);
170     if (rc < 0) {
171         LOG(ERROR) << "Filesystem format failed due to logwrap error";
172         errno = EIO;
173         return -1;
174     }
175 
176     if (rc == 0) {
177         LOG(INFO) << "Filesystem formatted OK";
178         return 0;
179     } else {
180         LOG(ERROR) << "Format failed (unknown exit code " << rc << ")";
181         errno = EIO;
182         return -1;
183     }
184     return 0;
185 }
186 
187 }  // namespace vfat
188 }  // namespace vold
189 }  // namespace android
190