Vfat.cpp 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
18
#include <stdlib.h>
19 20 21 22 23 24 25 26 27 28 29 30 31
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/mount.h>
32
#include <sys/wait.h>
33 34
#include <linux/fs.h>
#include <sys/ioctl.h>
35 36 37 38 39

#include <linux/kdev_t.h>

#define LOG_TAG "Vold"

40
#include <base/logging.h>
41
#include <base/stringprintf.h>
42 43
#include <cutils/log.h>
#include <cutils/properties.h>
44
#include <selinux/selinux.h>
45

46 47
#include <logwrap/logwrap.h>

48
#include "Vfat.h"
49
#include "Utils.h"
Rom Lemarchand's avatar
Rom Lemarchand committed
50
#include "VoldUtil.h"
51

52 53
using android::base::StringPrintf;

54 55 56 57
namespace android {
namespace vold {
namespace vfat {

58 59
static const char* kMkfsPath = "/system/bin/newfs_msdos";
static const char* kFsckPath = "/system/bin/fsck_msdos";
60

61 62 63 64 65 66 67
bool IsSupported() {
    return access(kMkfsPath, X_OK) == 0
            && access(kFsckPath, X_OK) == 0
            && IsFilesystemSupported("vfat");
}

status_t Check(const std::string& source) {
68
    if (access(kFsckPath, X_OK)) {
San Mehat's avatar
San Mehat committed
69
        SLOGW("Skipping fs checks\n");
70 71 72 73 74 75
        return 0;
    }

    int pass = 1;
    int rc = 0;
    do {
76 77 78 79
        std::vector<std::string> cmd;
        cmd.push_back(kFsckPath);
        cmd.push_back("-p");
        cmd.push_back("-f");
80
        cmd.push_back(source);
81

82
        // Fat devices are currently always untrusted
83
        rc = ForkExecvp(cmd, sFsckUntrustedContext);
84

85
        if (rc < 0) {
86 87 88 89 90
            SLOGE("Filesystem check failed due to logwrap error");
            errno = EIO;
            return -1;
        }

91
        switch(rc) {
92
        case 0:
San Mehat's avatar
San Mehat committed
93
            SLOGI("Filesystem check completed OK");
94 95 96
            return 0;

        case 2:
San Mehat's avatar
San Mehat committed
97
            SLOGE("Filesystem check failed (not a FAT filesystem)");
98 99 100 101 102
            errno = ENODATA;
            return -1;

        case 4:
            if (pass++ <= 3) {
San Mehat's avatar
San Mehat committed
103
                SLOGW("Filesystem modified - rechecking (pass %d)",
104 105 106
                        pass);
                continue;
            }
San Mehat's avatar
San Mehat committed
107
            SLOGE("Failing check after too many rechecks");
108 109 110 111
            errno = EIO;
            return -1;

        default:
112
            SLOGE("Filesystem check failed (unknown exit code %d)", rc);
113 114 115 116 117 118 119 120
            errno = EIO;
            return -1;
        }
    } while (0);

    return 0;
}

121 122 123
status_t Mount(const std::string& source, const std::string& target, bool ro,
        bool remount, bool executable, int ownerUid, int ownerGid, int permMask,
        bool createLost) {
124 125
    int rc;
    unsigned long flags;
126
    char mountData[255];
127

128 129 130
    const char* c_source = source.c_str();
    const char* c_target = target.c_str();

131
    flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC;
132

133
    flags |= (executable ? 0 : MS_NOEXEC);
134 135 136
    flags |= (ro ? MS_RDONLY : 0);
    flags |= (remount ? MS_REMOUNT : 0);

137 138 139 140 141 142 143 144 145
    /*
     * Note: This is a temporary hack. If the sampling profiler is enabled,
     * we make the SD card world-writable so any process can write snapshots.
     *
     * TODO: Remove this code once we have a drop box in system_server.
     */
    char value[PROPERTY_VALUE_MAX];
    property_get("persist.sampling_profiler", value, "");
    if (value[0] == '1') {
San Mehat's avatar
San Mehat committed
146
        SLOGW("The SD card is world-writable because the"
147
            " 'persist.sampling_profiler' system property is set to '1'.");
148
        permMask = 0;
149 150
    }

151 152 153 154
    sprintf(mountData,
            "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
            ownerUid, ownerGid, permMask, permMask);

155
    rc = mount(c_source, c_target, "vfat", flags, mountData);
156

157
    if (rc && errno == EROFS) {
158
        SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
159
        flags |= MS_RDONLY;
160
        rc = mount(c_source, c_target, "vfat", flags, mountData);
161 162
    }

163
    if (rc == 0 && createLost) {
164
        char *lost_path;
165
        asprintf(&lost_path, "%s/LOST.DIR", c_target);
166 167 168 169 170 171
        if (access(lost_path, F_OK)) {
            /*
             * Create a LOST.DIR in the root so we have somewhere to put
             * lost cluster chains (fsck_msdos doesn't currently do this)
             */
            if (mkdir(lost_path, 0755)) {
San Mehat's avatar
San Mehat committed
172
                SLOGE("Unable to create LOST.DIR (%s)", strerror(errno));
173 174 175 176 177 178 179 180
            }
        }
        free(lost_path);
    }

    return rc;
}

181
status_t Format(const std::string& source, unsigned int numSectors) {
182 183 184 185 186 187 188 189 190
    std::vector<std::string> cmd;
    cmd.push_back(kMkfsPath);
    cmd.push_back("-F");
    cmd.push_back("32");
    cmd.push_back("-O");
    cmd.push_back("android");
    cmd.push_back("-c");
    cmd.push_back("64");
    cmd.push_back("-A");
191 192

    if (numSectors) {
193 194
        cmd.push_back("-s");
        cmd.push_back(StringPrintf("%u", numSectors));
195
    }
196

197
    cmd.push_back(source);
198

199
    int rc = ForkExecvp(cmd);
200 201
    if (rc < 0) {
        SLOGE("Filesystem format failed due to logwrap error");
202 203 204 205
        errno = EIO;
        return -1;
    }

206
    if (rc == 0) {
San Mehat's avatar
San Mehat committed
207
        SLOGI("Filesystem formatted OK");
208 209
        return 0;
    } else {
210
        SLOGE("Format failed (unknown exit code %d)", rc);
211 212 213 214 215
        errno = EIO;
        return -1;
    }
    return 0;
}
216

217 218 219
}  // namespace vfat
}  // namespace vold
}  // namespace android