bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | mount.c (22.10.09) |
| 3 | exFAT file system implementation library. |
| 4 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 5 | Free exFAT implementation. |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 6 | Copyright (C) 2010-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 7 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 8 | This program is free software; you can redistribute it and/or modify |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | it under the terms of the GNU General Public License as published by |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 10 | the Free Software Foundation, either version 2 of the License, or |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License along |
| 19 | with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include "exfat.h" |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <errno.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | static uint64_t rootdir_size(const struct exfat* ef) |
| 31 | { |
| 32 | uint64_t clusters = 0; |
| 33 | cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster); |
| 34 | |
| 35 | while (!CLUSTER_INVALID(rootdir_cluster)) |
| 36 | { |
| 37 | clusters++; |
| 38 | /* root directory cannot be contiguous because there is no flag |
| 39 | to indicate this */ |
| 40 | rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster); |
| 41 | } |
| 42 | return clusters * CLUSTER_SIZE(*ef->sb); |
| 43 | } |
| 44 | |
| 45 | static const char* get_option(const char* options, const char* option_name) |
| 46 | { |
| 47 | const char* p; |
| 48 | size_t length = strlen(option_name); |
| 49 | |
| 50 | for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name)) |
| 51 | if ((p == options || p[-1] == ',') && p[length] == '=') |
| 52 | return p + length + 1; |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | static int get_int_option(const char* options, const char* option_name, |
| 57 | int base, int default_value) |
| 58 | { |
| 59 | const char* p = get_option(options, option_name); |
| 60 | |
| 61 | if (p == NULL) |
| 62 | return default_value; |
| 63 | return strtol(p, NULL, base); |
| 64 | } |
| 65 | |
| 66 | static bool match_option(const char* options, const char* option_name) |
| 67 | { |
| 68 | const char* p; |
| 69 | size_t length = strlen(option_name); |
| 70 | |
| 71 | for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name)) |
| 72 | if ((p == options || p[-1] == ',') && |
| 73 | (p[length] == ',' || p[length] == '\0')) |
| 74 | return true; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | static void parse_options(struct exfat* ef, const char* options) |
| 79 | { |
| 80 | int sys_umask = umask(0); |
| 81 | int opt_umask; |
| 82 | |
| 83 | umask(sys_umask); /* restore umask */ |
| 84 | opt_umask = get_int_option(options, "umask", 8, sys_umask); |
| 85 | ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777; |
| 86 | ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777; |
| 87 | |
| 88 | ef->uid = get_int_option(options, "uid", 10, geteuid()); |
| 89 | ef->gid = get_int_option(options, "gid", 10, getegid()); |
| 90 | |
| 91 | ef->noatime = match_option(options, "noatime"); |
| 92 | } |
| 93 | |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 94 | static int verify_vbr_checksum(struct exfat_dev* dev, void* sector, |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 95 | off64_t sector_size) |
| 96 | { |
| 97 | uint32_t vbr_checksum; |
| 98 | int i; |
| 99 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 100 | if (exfat_pread(dev, sector, sector_size, 0) < 0) |
| 101 | { |
| 102 | exfat_error("failed to read boot sector"); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 103 | return 1; |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 104 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 105 | vbr_checksum = exfat_vbr_start_checksum(sector, sector_size); |
| 106 | for (i = 1; i < 11; i++) |
| 107 | { |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 108 | if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0) |
| 109 | { |
| 110 | exfat_error("failed to read VBR sector"); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 111 | return 1; |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 112 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 113 | vbr_checksum = exfat_vbr_add_checksum(sector, sector_size, |
| 114 | vbr_checksum); |
| 115 | } |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 116 | if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0) |
| 117 | { |
| 118 | exfat_error("failed to read VBR checksum sector"); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 119 | return 1; |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 120 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 121 | for (i = 0; i < sector_size / sizeof(vbr_checksum); i++) |
| 122 | if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum) |
| 123 | { |
| 124 | exfat_error("invalid VBR checksum 0x%x (expected 0x%x)", |
| 125 | le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 126 | return 1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 127 | } |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 128 | return 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int commit_super_block(const struct exfat* ef) |
| 132 | { |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 133 | if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0) |
| 134 | { |
| 135 | exfat_error("failed to write super block"); |
| 136 | return 1; |
| 137 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 138 | return exfat_fsync(ef->dev); |
| 139 | } |
| 140 | |
| 141 | static int prepare_super_block(const struct exfat* ef) |
| 142 | { |
| 143 | if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED) |
| 144 | exfat_warn("volume was not unmounted cleanly"); |
| 145 | |
| 146 | if (ef->ro) |
| 147 | return 0; |
| 148 | |
| 149 | ef->sb->volume_state = cpu_to_le16( |
| 150 | le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED); |
| 151 | return commit_super_block(ef); |
| 152 | } |
| 153 | |
| 154 | int exfat_mount(struct exfat* ef, const char* spec, const char* options) |
| 155 | { |
| 156 | int rc; |
| 157 | enum exfat_mode mode; |
| 158 | |
| 159 | exfat_tzset(); |
| 160 | memset(ef, 0, sizeof(struct exfat)); |
| 161 | |
| 162 | parse_options(ef, options); |
| 163 | |
| 164 | if (match_option(options, "ro")) |
| 165 | mode = EXFAT_MODE_RO; |
| 166 | else if (match_option(options, "ro_fallback")) |
| 167 | mode = EXFAT_MODE_ANY; |
| 168 | else |
| 169 | mode = EXFAT_MODE_RW; |
| 170 | ef->dev = exfat_open(spec, mode); |
| 171 | if (ef->dev == NULL) |
| 172 | return -EIO; |
| 173 | if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO) |
| 174 | { |
| 175 | if (mode == EXFAT_MODE_ANY) |
| 176 | ef->ro = -1; |
| 177 | else |
| 178 | ef->ro = 1; |
| 179 | } |
| 180 | |
| 181 | ef->sb = malloc(sizeof(struct exfat_super_block)); |
| 182 | if (ef->sb == NULL) |
| 183 | { |
| 184 | exfat_close(ef->dev); |
| 185 | exfat_error("failed to allocate memory for the super block"); |
| 186 | return -ENOMEM; |
| 187 | } |
| 188 | memset(ef->sb, 0, sizeof(struct exfat_super_block)); |
| 189 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 190 | if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0) |
| 191 | { |
| 192 | exfat_close(ef->dev); |
| 193 | free(ef->sb); |
| 194 | exfat_error("failed to read boot sector"); |
| 195 | return -EIO; |
| 196 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 197 | if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0) |
| 198 | { |
| 199 | exfat_close(ef->dev); |
| 200 | free(ef->sb); |
| 201 | exfat_error("exFAT file system is not found"); |
| 202 | return -EIO; |
| 203 | } |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 204 | if (ef->sb->version.major != 1 || ef->sb->version.minor != 0) |
| 205 | { |
| 206 | exfat_close(ef->dev); |
| 207 | exfat_error("unsupported exFAT version: %hhu.%hhu", |
| 208 | ef->sb->version.major, ef->sb->version.minor); |
| 209 | free(ef->sb); |
| 210 | return -EIO; |
| 211 | } |
| 212 | if (ef->sb->fat_count != 1) |
| 213 | { |
| 214 | exfat_close(ef->dev); |
| 215 | free(ef->sb); |
| 216 | exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count); |
| 217 | return -EIO; |
| 218 | } |
| 219 | /* officially exFAT supports cluster size up to 32 MB */ |
| 220 | if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25) |
| 221 | { |
| 222 | exfat_close(ef->dev); |
| 223 | free(ef->sb); |
| 224 | exfat_error("too big cluster size: 2^%d", |
| 225 | (int) ef->sb->sector_bits + (int) ef->sb->spc_bits); |
| 226 | return -EIO; |
| 227 | } |
| 228 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 229 | ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb)); |
| 230 | if (ef->zero_cluster == NULL) |
| 231 | { |
| 232 | exfat_close(ef->dev); |
| 233 | free(ef->sb); |
| 234 | exfat_error("failed to allocate zero sector"); |
| 235 | return -ENOMEM; |
| 236 | } |
| 237 | /* use zero_cluster as a temporary buffer for VBR checksum verification */ |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 238 | if (verify_vbr_checksum(ef->dev, ef->zero_cluster, |
| 239 | SECTOR_SIZE(*ef->sb)) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 240 | { |
| 241 | free(ef->zero_cluster); |
| 242 | exfat_close(ef->dev); |
| 243 | free(ef->sb); |
| 244 | return -EIO; |
| 245 | } |
| 246 | memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb)); |
| 247 | |
| 248 | ef->root = malloc(sizeof(struct exfat_node)); |
| 249 | if (ef->root == NULL) |
| 250 | { |
| 251 | free(ef->zero_cluster); |
| 252 | exfat_close(ef->dev); |
| 253 | free(ef->sb); |
| 254 | exfat_error("failed to allocate root node"); |
| 255 | return -ENOMEM; |
| 256 | } |
| 257 | memset(ef->root, 0, sizeof(struct exfat_node)); |
| 258 | ef->root->flags = EXFAT_ATTRIB_DIR; |
| 259 | ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster); |
| 260 | ef->root->fptr_cluster = ef->root->start_cluster; |
| 261 | ef->root->name[0] = cpu_to_le16('\0'); |
| 262 | ef->root->size = rootdir_size(ef); |
| 263 | /* exFAT does not have time attributes for the root directory */ |
| 264 | ef->root->mtime = 0; |
| 265 | ef->root->atime = 0; |
| 266 | /* always keep at least 1 reference to the root node */ |
| 267 | exfat_get_node(ef->root); |
| 268 | |
| 269 | rc = exfat_cache_directory(ef, ef->root); |
| 270 | if (rc != 0) |
| 271 | goto error; |
| 272 | if (ef->upcase == NULL) |
| 273 | { |
| 274 | exfat_error("upcase table is not found"); |
| 275 | goto error; |
| 276 | } |
| 277 | if (ef->cmap.chunk == NULL) |
| 278 | { |
| 279 | exfat_error("clusters bitmap is not found"); |
| 280 | goto error; |
| 281 | } |
| 282 | |
| 283 | if (prepare_super_block(ef) != 0) |
| 284 | goto error; |
| 285 | |
| 286 | return 0; |
| 287 | |
| 288 | error: |
| 289 | exfat_put_node(ef, ef->root); |
| 290 | exfat_reset_cache(ef); |
| 291 | free(ef->root); |
| 292 | free(ef->zero_cluster); |
| 293 | exfat_close(ef->dev); |
| 294 | free(ef->sb); |
| 295 | return -EIO; |
| 296 | } |
| 297 | |
| 298 | static void finalize_super_block(struct exfat* ef) |
| 299 | { |
| 300 | if (ef->ro) |
| 301 | return; |
| 302 | |
| 303 | ef->sb->volume_state = cpu_to_le16( |
| 304 | le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED); |
| 305 | |
| 306 | /* Some implementations set the percentage of allocated space to 0xff |
| 307 | on FS creation and never update it. In this case leave it as is. */ |
| 308 | if (ef->sb->allocated_percent != 0xff) |
| 309 | { |
| 310 | uint32_t free, total; |
| 311 | |
| 312 | free = exfat_count_free_clusters(ef); |
| 313 | total = le32_to_cpu(ef->sb->cluster_count); |
| 314 | ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total; |
| 315 | } |
| 316 | |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 317 | commit_super_block(ef); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void exfat_unmount(struct exfat* ef) |
| 321 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 322 | exfat_put_node(ef, ef->root); |
| 323 | exfat_reset_cache(ef); |
| 324 | free(ef->root); |
| 325 | ef->root = NULL; |
| 326 | finalize_super_block(ef); |
| 327 | exfat_close(ef->dev); /* close descriptor immediately after fsync */ |
| 328 | ef->dev = NULL; |
| 329 | free(ef->zero_cluster); |
| 330 | ef->zero_cluster = NULL; |
| 331 | free(ef->cmap.chunk); |
| 332 | ef->cmap.chunk = NULL; |
| 333 | free(ef->sb); |
| 334 | ef->sb = NULL; |
| 335 | free(ef->upcase); |
| 336 | ef->upcase = NULL; |
| 337 | ef->upcase_chars = 0; |
| 338 | } |