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. |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 6 | Copyright (C) 2010-2015 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> |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 27 | #include <inttypes.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | static uint64_t rootdir_size(const struct exfat* ef) |
| 32 | { |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 33 | uint32_t clusters = 0; |
| 34 | uint32_t clusters_max = le32_to_cpu(ef->sb->cluster_count); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 35 | cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster); |
| 36 | |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 37 | /* Iterate all clusters of the root directory to calculate its size. |
| 38 | It can't be contiguous because there is no flag to indicate this. */ |
| 39 | do |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 40 | { |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 41 | if (clusters == clusters_max) /* infinite loop detected */ |
| 42 | { |
| 43 | exfat_error("root directory cannot occupy all %d clusters", |
| 44 | clusters); |
| 45 | return 0; |
| 46 | } |
| 47 | if (CLUSTER_INVALID(rootdir_cluster)) |
| 48 | { |
| 49 | exfat_error("bad cluster %#x while reading root directory", |
| 50 | rootdir_cluster); |
| 51 | return 0; |
| 52 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 53 | rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 54 | clusters++; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 55 | } |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 56 | while (rootdir_cluster != EXFAT_CLUSTER_END); |
| 57 | |
| 58 | return (uint64_t) clusters * CLUSTER_SIZE(*ef->sb); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static const char* get_option(const char* options, const char* option_name) |
| 62 | { |
| 63 | const char* p; |
| 64 | size_t length = strlen(option_name); |
| 65 | |
| 66 | for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name)) |
| 67 | if ((p == options || p[-1] == ',') && p[length] == '=') |
| 68 | return p + length + 1; |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | static int get_int_option(const char* options, const char* option_name, |
| 73 | int base, int default_value) |
| 74 | { |
| 75 | const char* p = get_option(options, option_name); |
| 76 | |
| 77 | if (p == NULL) |
| 78 | return default_value; |
| 79 | return strtol(p, NULL, base); |
| 80 | } |
| 81 | |
| 82 | static bool match_option(const char* options, const char* option_name) |
| 83 | { |
| 84 | const char* p; |
| 85 | size_t length = strlen(option_name); |
| 86 | |
| 87 | for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name)) |
| 88 | if ((p == options || p[-1] == ',') && |
| 89 | (p[length] == ',' || p[length] == '\0')) |
| 90 | return true; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | static void parse_options(struct exfat* ef, const char* options) |
| 95 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 96 | int opt_umask; |
| 97 | |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 98 | opt_umask = get_int_option(options, "umask", 8, 0); |
| 99 | ef->dmask = get_int_option(options, "dmask", 8, opt_umask); |
| 100 | ef->fmask = get_int_option(options, "fmask", 8, opt_umask); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 101 | |
| 102 | ef->uid = get_int_option(options, "uid", 10, geteuid()); |
| 103 | ef->gid = get_int_option(options, "gid", 10, getegid()); |
| 104 | |
| 105 | ef->noatime = match_option(options, "noatime"); |
| 106 | } |
| 107 | |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 108 | static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector, |
Spegelius | d69ac2b | 2014-11-23 15:15:06 +0200 | [diff] [blame] | 109 | loff_t sector_size) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 110 | { |
| 111 | uint32_t vbr_checksum; |
| 112 | int i; |
| 113 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 114 | if (exfat_pread(dev, sector, sector_size, 0) < 0) |
| 115 | { |
| 116 | exfat_error("failed to read boot sector"); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 117 | return false; |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 118 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 119 | vbr_checksum = exfat_vbr_start_checksum(sector, sector_size); |
| 120 | for (i = 1; i < 11; i++) |
| 121 | { |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 122 | if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0) |
| 123 | { |
| 124 | exfat_error("failed to read VBR sector"); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 125 | return false; |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 126 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 127 | vbr_checksum = exfat_vbr_add_checksum(sector, sector_size, |
| 128 | vbr_checksum); |
| 129 | } |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 130 | if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0) |
| 131 | { |
| 132 | exfat_error("failed to read VBR checksum sector"); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 133 | return false; |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 134 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 135 | for (i = 0; i < sector_size / sizeof(vbr_checksum); i++) |
| 136 | if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum) |
| 137 | { |
| 138 | exfat_error("invalid VBR checksum 0x%x (expected 0x%x)", |
| 139 | le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 140 | return false; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 141 | } |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 142 | return true; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static int commit_super_block(const struct exfat* ef) |
| 146 | { |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 147 | if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0) |
| 148 | { |
| 149 | exfat_error("failed to write super block"); |
| 150 | return 1; |
| 151 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 152 | return exfat_fsync(ef->dev); |
| 153 | } |
| 154 | |
| 155 | static int prepare_super_block(const struct exfat* ef) |
| 156 | { |
| 157 | if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED) |
| 158 | exfat_warn("volume was not unmounted cleanly"); |
| 159 | |
| 160 | if (ef->ro) |
| 161 | return 0; |
| 162 | |
| 163 | ef->sb->volume_state = cpu_to_le16( |
| 164 | le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED); |
| 165 | return commit_super_block(ef); |
| 166 | } |
| 167 | |
| 168 | int exfat_mount(struct exfat* ef, const char* spec, const char* options) |
| 169 | { |
| 170 | int rc; |
| 171 | enum exfat_mode mode; |
| 172 | |
| 173 | exfat_tzset(); |
| 174 | memset(ef, 0, sizeof(struct exfat)); |
| 175 | |
| 176 | parse_options(ef, options); |
| 177 | |
| 178 | if (match_option(options, "ro")) |
| 179 | mode = EXFAT_MODE_RO; |
| 180 | else if (match_option(options, "ro_fallback")) |
| 181 | mode = EXFAT_MODE_ANY; |
| 182 | else |
| 183 | mode = EXFAT_MODE_RW; |
| 184 | ef->dev = exfat_open(spec, mode); |
| 185 | if (ef->dev == NULL) |
| 186 | return -EIO; |
| 187 | if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO) |
| 188 | { |
| 189 | if (mode == EXFAT_MODE_ANY) |
| 190 | ef->ro = -1; |
| 191 | else |
| 192 | ef->ro = 1; |
| 193 | } |
| 194 | |
| 195 | ef->sb = malloc(sizeof(struct exfat_super_block)); |
| 196 | if (ef->sb == NULL) |
| 197 | { |
| 198 | exfat_close(ef->dev); |
| 199 | exfat_error("failed to allocate memory for the super block"); |
| 200 | return -ENOMEM; |
| 201 | } |
| 202 | memset(ef->sb, 0, sizeof(struct exfat_super_block)); |
| 203 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 204 | if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0) |
| 205 | { |
| 206 | exfat_close(ef->dev); |
| 207 | free(ef->sb); |
| 208 | exfat_error("failed to read boot sector"); |
| 209 | return -EIO; |
| 210 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 211 | if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0) |
| 212 | { |
| 213 | exfat_close(ef->dev); |
| 214 | free(ef->sb); |
| 215 | exfat_error("exFAT file system is not found"); |
| 216 | return -EIO; |
| 217 | } |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 218 | /* sector cannot be smaller than 512 bytes */ |
| 219 | if (ef->sb->sector_bits < 9) |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 220 | { |
| 221 | exfat_close(ef->dev); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 222 | exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 223 | free(ef->sb); |
| 224 | return -EIO; |
| 225 | } |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 226 | /* officially exFAT supports cluster size up to 32 MB */ |
| 227 | if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25) |
| 228 | { |
| 229 | exfat_close(ef->dev); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 230 | exfat_error("too big cluster size: 2^(%hhd+%hhd)", |
| 231 | ef->sb->sector_bits, ef->sb->spc_bits); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 232 | free(ef->sb); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 233 | return -EIO; |
| 234 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 235 | ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb)); |
| 236 | if (ef->zero_cluster == NULL) |
| 237 | { |
| 238 | exfat_close(ef->dev); |
| 239 | free(ef->sb); |
| 240 | exfat_error("failed to allocate zero sector"); |
| 241 | return -ENOMEM; |
| 242 | } |
| 243 | /* use zero_cluster as a temporary buffer for VBR checksum verification */ |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 244 | if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb))) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 245 | { |
| 246 | free(ef->zero_cluster); |
| 247 | exfat_close(ef->dev); |
| 248 | free(ef->sb); |
| 249 | return -EIO; |
| 250 | } |
| 251 | memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb)); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 252 | if (ef->sb->version.major != 1 || ef->sb->version.minor != 0) |
| 253 | { |
| 254 | free(ef->zero_cluster); |
| 255 | exfat_close(ef->dev); |
| 256 | exfat_error("unsupported exFAT version: %hhu.%hhu", |
| 257 | ef->sb->version.major, ef->sb->version.minor); |
| 258 | free(ef->sb); |
| 259 | return -EIO; |
| 260 | } |
| 261 | if (ef->sb->fat_count != 1) |
| 262 | { |
| 263 | free(ef->zero_cluster); |
| 264 | exfat_close(ef->dev); |
| 265 | exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count); |
| 266 | free(ef->sb); |
| 267 | return -EIO; |
| 268 | } |
| 269 | if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) > |
| 270 | exfat_get_size(ef->dev)) |
| 271 | { |
| 272 | /* this can cause I/O errors later but we don't fail mounting to let |
| 273 | user rescue data */ |
| 274 | exfat_warn("file system is larger than underlying device: " |
| 275 | "%"PRIu64" > %"PRIu64, |
| 276 | le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb), |
| 277 | exfat_get_size(ef->dev)); |
| 278 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 279 | |
| 280 | ef->root = malloc(sizeof(struct exfat_node)); |
| 281 | if (ef->root == NULL) |
| 282 | { |
| 283 | free(ef->zero_cluster); |
| 284 | exfat_close(ef->dev); |
| 285 | free(ef->sb); |
| 286 | exfat_error("failed to allocate root node"); |
| 287 | return -ENOMEM; |
| 288 | } |
| 289 | memset(ef->root, 0, sizeof(struct exfat_node)); |
| 290 | ef->root->flags = EXFAT_ATTRIB_DIR; |
| 291 | ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster); |
| 292 | ef->root->fptr_cluster = ef->root->start_cluster; |
| 293 | ef->root->name[0] = cpu_to_le16('\0'); |
| 294 | ef->root->size = rootdir_size(ef); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 295 | if (ef->root->size == 0) |
| 296 | { |
| 297 | free(ef->root); |
| 298 | free(ef->zero_cluster); |
| 299 | exfat_close(ef->dev); |
| 300 | free(ef->sb); |
| 301 | return -EIO; |
| 302 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 303 | /* exFAT does not have time attributes for the root directory */ |
| 304 | ef->root->mtime = 0; |
| 305 | ef->root->atime = 0; |
| 306 | /* always keep at least 1 reference to the root node */ |
| 307 | exfat_get_node(ef->root); |
| 308 | |
| 309 | rc = exfat_cache_directory(ef, ef->root); |
| 310 | if (rc != 0) |
| 311 | goto error; |
| 312 | if (ef->upcase == NULL) |
| 313 | { |
| 314 | exfat_error("upcase table is not found"); |
| 315 | goto error; |
| 316 | } |
| 317 | if (ef->cmap.chunk == NULL) |
| 318 | { |
| 319 | exfat_error("clusters bitmap is not found"); |
| 320 | goto error; |
| 321 | } |
| 322 | |
| 323 | if (prepare_super_block(ef) != 0) |
| 324 | goto error; |
| 325 | |
| 326 | return 0; |
| 327 | |
| 328 | error: |
| 329 | exfat_put_node(ef, ef->root); |
| 330 | exfat_reset_cache(ef); |
| 331 | free(ef->root); |
| 332 | free(ef->zero_cluster); |
| 333 | exfat_close(ef->dev); |
| 334 | free(ef->sb); |
| 335 | return -EIO; |
| 336 | } |
| 337 | |
| 338 | static void finalize_super_block(struct exfat* ef) |
| 339 | { |
| 340 | if (ef->ro) |
| 341 | return; |
| 342 | |
| 343 | ef->sb->volume_state = cpu_to_le16( |
| 344 | le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED); |
| 345 | |
| 346 | /* Some implementations set the percentage of allocated space to 0xff |
| 347 | on FS creation and never update it. In this case leave it as is. */ |
| 348 | if (ef->sb->allocated_percent != 0xff) |
| 349 | { |
| 350 | uint32_t free, total; |
| 351 | |
| 352 | free = exfat_count_free_clusters(ef); |
| 353 | total = le32_to_cpu(ef->sb->cluster_count); |
| 354 | ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total; |
| 355 | } |
| 356 | |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 357 | commit_super_block(ef); /* ignore return code */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void exfat_unmount(struct exfat* ef) |
| 361 | { |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame] | 362 | exfat_flush_nodes(ef); /* ignore return code */ |
| 363 | exfat_flush(ef); /* ignore return code */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 364 | exfat_put_node(ef, ef->root); |
| 365 | exfat_reset_cache(ef); |
| 366 | free(ef->root); |
| 367 | ef->root = NULL; |
| 368 | finalize_super_block(ef); |
| 369 | exfat_close(ef->dev); /* close descriptor immediately after fsync */ |
| 370 | ef->dev = NULL; |
| 371 | free(ef->zero_cluster); |
| 372 | ef->zero_cluster = NULL; |
| 373 | free(ef->cmap.chunk); |
| 374 | ef->cmap.chunk = NULL; |
| 375 | free(ef->sb); |
| 376 | ef->sb = NULL; |
| 377 | free(ef->upcase); |
| 378 | ef->upcase = NULL; |
| 379 | ef->upcase_chars = 0; |
| 380 | } |