3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2018 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 uint32_t clusters = 0;
34 uint32_t clusters_max = le32_to_cpu(ef->sb->cluster_count);
35 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
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. */
41 if (clusters == clusters_max) /* infinite loop detected */
43 exfat_error("root directory cannot occupy all %d clusters",
47 if (CLUSTER_INVALID(*ef->sb, rootdir_cluster))
49 exfat_error("bad cluster %#x while reading root directory",
53 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
56 while (rootdir_cluster != EXFAT_CLUSTER_END);
58 return (uint64_t) clusters * CLUSTER_SIZE(*ef->sb);
61 static const char* get_option(const char* options, const char* option_name)
64 size_t length = strlen(option_name);
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;
72 static int get_int_option(const char* options, const char* option_name,
73 int base, int default_value)
75 const char* p = get_option(options, option_name);
79 return strtol(p, NULL, base);
82 static bool match_option(const char* options, const char* option_name)
85 size_t length = strlen(option_name);
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'))
94 static void parse_options(struct exfat* ef, const char* options)
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);
102 ef->uid = get_int_option(options, "uid", 10, geteuid());
103 ef->gid = get_int_option(options, "gid", 10, getegid());
105 ef->noatime = match_option(options, "noatime");
107 switch (get_int_option(options, "repair", 10, 0))
110 ef->repair = EXFAT_REPAIR_ASK;
113 ef->repair = EXFAT_REPAIR_YES;
116 ef->repair = EXFAT_REPAIR_NO;
121 static bool verify_vbr_checksum(const struct exfat* ef, void* sector)
123 off_t sector_size = SECTOR_SIZE(*ef->sb);
124 uint32_t vbr_checksum;
127 if (exfat_pread(ef->dev, sector, sector_size, 0) < 0)
129 exfat_error("failed to read boot sector");
132 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
133 for (i = 1; i < 11; i++)
135 if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
137 exfat_error("failed to read VBR sector");
140 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
143 if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
145 exfat_error("failed to read VBR checksum sector");
148 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
149 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
151 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
152 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
158 static int commit_super_block(const struct exfat* ef)
160 if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
162 exfat_error("failed to write super block");
165 return exfat_fsync(ef->dev);
168 static int prepare_super_block(const struct exfat* ef)
170 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
171 exfat_warn("volume was not unmounted cleanly");
176 ef->sb->volume_state = cpu_to_le16(
177 le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
178 return commit_super_block(ef);
181 static void exfat_free(struct exfat* ef)
183 exfat_close(ef->dev); /* first of all, close the descriptor */
184 ef->dev = NULL; /* struct exfat_dev is freed by exfat_close() */
187 free(ef->zero_cluster);
188 ef->zero_cluster = NULL;
189 free(ef->cmap.chunk);
190 ef->cmap.chunk = NULL;
197 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
200 enum exfat_mode mode;
203 memset(ef, 0, sizeof(struct exfat));
205 parse_options(ef, options);
207 if (match_option(options, "ro"))
208 mode = EXFAT_MODE_RO;
209 else if (match_option(options, "ro_fallback"))
210 mode = EXFAT_MODE_ANY;
212 mode = EXFAT_MODE_RW;
213 ef->dev = exfat_open(spec, mode);
216 if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
218 if (mode == EXFAT_MODE_ANY)
224 ef->sb = malloc(sizeof(struct exfat_super_block));
227 exfat_error("failed to allocate memory for the super block");
231 memset(ef->sb, 0, sizeof(struct exfat_super_block));
233 if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
235 exfat_error("failed to read boot sector");
239 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
241 exfat_error("exFAT file system is not found");
245 /* sector cannot be smaller than 512 bytes */
246 if (ef->sb->sector_bits < 9)
248 exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits);
252 /* officially exFAT supports cluster size up to 32 MB */
253 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
255 exfat_error("too big cluster size: 2^(%hhd+%hhd)",
256 ef->sb->sector_bits, ef->sb->spc_bits);
260 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
261 if (ef->zero_cluster == NULL)
263 exfat_error("failed to allocate zero sector");
267 /* use zero_cluster as a temporary buffer for VBR checksum verification */
268 if (!verify_vbr_checksum(ef, ef->zero_cluster))
273 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
274 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
276 exfat_error("unsupported exFAT version: %hhu.%hhu",
277 ef->sb->version.major, ef->sb->version.minor);
281 if (ef->sb->fat_count != 1)
283 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
287 if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
288 exfat_get_size(ef->dev))
290 /* this can cause I/O errors later but we don't fail mounting to let
292 exfat_warn("file system in sectors is larger than device: "
293 "%"PRIu64" * %d > %"PRIu64,
294 le64_to_cpu(ef->sb->sector_count), SECTOR_SIZE(*ef->sb),
295 exfat_get_size(ef->dev));
297 if ((off_t) le32_to_cpu(ef->sb->cluster_count) * CLUSTER_SIZE(*ef->sb) >
298 exfat_get_size(ef->dev))
300 exfat_error("file system in clusters is larger than device: "
302 le32_to_cpu(ef->sb->cluster_count), CLUSTER_SIZE(*ef->sb),
303 exfat_get_size(ef->dev));
308 ef->root = malloc(sizeof(struct exfat_node));
309 if (ef->root == NULL)
311 exfat_error("failed to allocate root node");
315 memset(ef->root, 0, sizeof(struct exfat_node));
316 ef->root->attrib = EXFAT_ATTRIB_DIR;
317 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
318 ef->root->fptr_cluster = ef->root->start_cluster;
319 ef->root->name[0] = cpu_to_le16('\0');
320 ef->root->size = rootdir_size(ef);
321 if (ef->root->size == 0)
326 /* exFAT does not have time attributes for the root directory */
329 /* always keep at least 1 reference to the root node */
330 exfat_get_node(ef->root);
332 rc = exfat_cache_directory(ef, ef->root);
335 if (ef->upcase == NULL)
337 exfat_error("upcase table is not found");
340 if (ef->cmap.chunk == NULL)
342 exfat_error("clusters bitmap is not found");
346 if (prepare_super_block(ef) != 0)
352 exfat_put_node(ef, ef->root);
353 exfat_reset_cache(ef);
358 static void finalize_super_block(struct exfat* ef)
363 ef->sb->volume_state = cpu_to_le16(
364 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
366 /* Some implementations set the percentage of allocated space to 0xff
367 on FS creation and never update it. In this case leave it as is. */
368 if (ef->sb->allocated_percent != 0xff)
370 uint32_t free, total;
372 free = exfat_count_free_clusters(ef);
373 total = le32_to_cpu(ef->sb->cluster_count);
374 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
377 commit_super_block(ef); /* ignore return code */
380 void exfat_unmount(struct exfat* ef)
382 exfat_flush_nodes(ef); /* ignore return code */
383 exfat_flush(ef); /* ignore return code */
384 exfat_put_node(ef, ef->root);
385 exfat_reset_cache(ef);
386 finalize_super_block(ef);
387 exfat_free(ef); /* will close the descriptor */