3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2015 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
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.
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.
29 #include <sys/types.h>
31 static uint64_t rootdir_size(const struct exfat* ef)
33 uint64_t clusters = 0;
34 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
36 while (!CLUSTER_INVALID(rootdir_cluster))
39 /* root directory cannot be contiguous because there is no flag
41 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
43 if (rootdir_cluster != EXFAT_CLUSTER_END)
45 exfat_error("bad cluster %#x while reading root directory",
49 return clusters * CLUSTER_SIZE(*ef->sb);
52 static const char* get_option(const char* options, const char* option_name)
55 size_t length = strlen(option_name);
57 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
58 if ((p == options || p[-1] == ',') && p[length] == '=')
59 return p + length + 1;
63 static int get_int_option(const char* options, const char* option_name,
64 int base, int default_value)
66 const char* p = get_option(options, option_name);
70 return strtol(p, NULL, base);
73 static bool match_option(const char* options, const char* option_name)
76 size_t length = strlen(option_name);
78 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
79 if ((p == options || p[-1] == ',') &&
80 (p[length] == ',' || p[length] == '\0'))
85 static void parse_options(struct exfat* ef, const char* options)
89 opt_umask = get_int_option(options, "umask", 8, 0);
90 ef->dmask = get_int_option(options, "dmask", 8, opt_umask);
91 ef->fmask = get_int_option(options, "fmask", 8, opt_umask);
93 ef->uid = get_int_option(options, "uid", 10, geteuid());
94 ef->gid = get_int_option(options, "gid", 10, getegid());
96 ef->noatime = match_option(options, "noatime");
99 static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
102 uint32_t vbr_checksum;
105 if (exfat_pread(dev, sector, sector_size, 0) < 0)
107 exfat_error("failed to read boot sector");
110 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
111 for (i = 1; i < 11; i++)
113 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
115 exfat_error("failed to read VBR sector");
118 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
121 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
123 exfat_error("failed to read VBR checksum sector");
126 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
127 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
129 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
130 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
136 static int commit_super_block(const struct exfat* ef)
138 if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
140 exfat_error("failed to write super block");
143 return exfat_fsync(ef->dev);
146 static int prepare_super_block(const struct exfat* ef)
148 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
149 exfat_warn("volume was not unmounted cleanly");
154 ef->sb->volume_state = cpu_to_le16(
155 le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
156 return commit_super_block(ef);
159 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
162 enum exfat_mode mode;
165 memset(ef, 0, sizeof(struct exfat));
167 parse_options(ef, options);
169 if (match_option(options, "ro"))
170 mode = EXFAT_MODE_RO;
171 else if (match_option(options, "ro_fallback"))
172 mode = EXFAT_MODE_ANY;
174 mode = EXFAT_MODE_RW;
175 ef->dev = exfat_open(spec, mode);
178 if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
180 if (mode == EXFAT_MODE_ANY)
186 ef->sb = malloc(sizeof(struct exfat_super_block));
189 exfat_close(ef->dev);
190 exfat_error("failed to allocate memory for the super block");
193 memset(ef->sb, 0, sizeof(struct exfat_super_block));
195 if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
197 exfat_close(ef->dev);
199 exfat_error("failed to read boot sector");
202 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
204 exfat_close(ef->dev);
206 exfat_error("exFAT file system is not found");
209 /* sector cannot be smaller than 512 bytes */
210 if (ef->sb->sector_bits < 9)
212 exfat_close(ef->dev);
213 exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits);
217 /* officially exFAT supports cluster size up to 32 MB */
218 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
220 exfat_close(ef->dev);
221 exfat_error("too big cluster size: 2^(%hhd+%hhd)",
222 ef->sb->sector_bits, ef->sb->spc_bits);
226 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
227 if (ef->zero_cluster == NULL)
229 exfat_close(ef->dev);
231 exfat_error("failed to allocate zero sector");
234 /* use zero_cluster as a temporary buffer for VBR checksum verification */
235 if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb)))
237 free(ef->zero_cluster);
238 exfat_close(ef->dev);
242 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
243 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
245 free(ef->zero_cluster);
246 exfat_close(ef->dev);
247 exfat_error("unsupported exFAT version: %hhu.%hhu",
248 ef->sb->version.major, ef->sb->version.minor);
252 if (ef->sb->fat_count != 1)
254 free(ef->zero_cluster);
255 exfat_close(ef->dev);
256 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
260 if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
261 exfat_get_size(ef->dev))
263 /* this can cause I/O errors later but we don't fail mounting to let
265 exfat_warn("file system is larger than underlying device: "
266 "%"PRIu64" > %"PRIu64,
267 le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb),
268 exfat_get_size(ef->dev));
271 ef->root = malloc(sizeof(struct exfat_node));
272 if (ef->root == NULL)
274 free(ef->zero_cluster);
275 exfat_close(ef->dev);
277 exfat_error("failed to allocate root node");
280 memset(ef->root, 0, sizeof(struct exfat_node));
281 ef->root->flags = EXFAT_ATTRIB_DIR;
282 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
283 ef->root->fptr_cluster = ef->root->start_cluster;
284 ef->root->name[0] = cpu_to_le16('\0');
285 ef->root->size = rootdir_size(ef);
286 if (ef->root->size == 0)
289 free(ef->zero_cluster);
290 exfat_close(ef->dev);
294 /* exFAT does not have time attributes for the root directory */
297 /* always keep at least 1 reference to the root node */
298 exfat_get_node(ef->root);
300 rc = exfat_cache_directory(ef, ef->root);
303 if (ef->upcase == NULL)
305 exfat_error("upcase table is not found");
308 if (ef->cmap.chunk == NULL)
310 exfat_error("clusters bitmap is not found");
314 if (prepare_super_block(ef) != 0)
320 exfat_put_node(ef, ef->root);
321 exfat_reset_cache(ef);
323 free(ef->zero_cluster);
324 exfat_close(ef->dev);
329 static void finalize_super_block(struct exfat* ef)
334 ef->sb->volume_state = cpu_to_le16(
335 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
337 /* Some implementations set the percentage of allocated space to 0xff
338 on FS creation and never update it. In this case leave it as is. */
339 if (ef->sb->allocated_percent != 0xff)
341 uint32_t free, total;
343 free = exfat_count_free_clusters(ef);
344 total = le32_to_cpu(ef->sb->cluster_count);
345 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
348 commit_super_block(ef); /* ignore return code */
351 void exfat_unmount(struct exfat* ef)
353 exfat_flush(ef); /* ignore return code */
354 exfat_put_node(ef, ef->root);
355 exfat_reset_cache(ef);
358 finalize_super_block(ef);
359 exfat_close(ef->dev); /* close descriptor immediately after fsync */
361 free(ef->zero_cluster);
362 ef->zero_cluster = NULL;
363 free(ef->cmap.chunk);
364 ef->cmap.chunk = NULL;
369 ef->upcase_chars = 0;