3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2013 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.
28 #include <sys/types.h>
30 static uint64_t rootdir_size(const struct exfat* ef)
32 uint64_t clusters = 0;
33 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
35 while (!CLUSTER_INVALID(rootdir_cluster))
38 /* root directory cannot be contiguous because there is no flag
40 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
42 return clusters * CLUSTER_SIZE(*ef->sb);
45 static const char* get_option(const char* options, const char* option_name)
48 size_t length = strlen(option_name);
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;
56 static int get_int_option(const char* options, const char* option_name,
57 int base, int default_value)
59 const char* p = get_option(options, option_name);
63 return strtol(p, NULL, base);
66 static bool match_option(const char* options, const char* option_name)
69 size_t length = strlen(option_name);
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'))
78 static void parse_options(struct exfat* ef, const char* options)
80 int sys_umask = umask(0);
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;
88 ef->uid = get_int_option(options, "uid", 10, geteuid());
89 ef->gid = get_int_option(options, "gid", 10, getegid());
91 ef->noatime = match_option(options, "noatime");
94 static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
97 uint32_t vbr_checksum;
100 if (exfat_pread(dev, sector, sector_size, 0) < 0)
102 exfat_error("failed to read boot sector");
105 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
106 for (i = 1; i < 11; i++)
108 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
110 exfat_error("failed to read VBR sector");
113 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
116 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
118 exfat_error("failed to read VBR checksum sector");
121 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
122 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
124 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
125 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
131 static int commit_super_block(const struct exfat* ef)
133 if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
135 exfat_error("failed to write super block");
138 return exfat_fsync(ef->dev);
141 static int prepare_super_block(const struct exfat* ef)
143 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
144 exfat_warn("volume was not unmounted cleanly");
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);
154 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
157 enum exfat_mode mode;
160 memset(ef, 0, sizeof(struct exfat));
162 parse_options(ef, options);
164 if (match_option(options, "ro"))
165 mode = EXFAT_MODE_RO;
166 else if (match_option(options, "ro_fallback"))
167 mode = EXFAT_MODE_ANY;
169 mode = EXFAT_MODE_RW;
170 ef->dev = exfat_open(spec, mode);
173 if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
175 if (mode == EXFAT_MODE_ANY)
181 ef->sb = malloc(sizeof(struct exfat_super_block));
184 exfat_close(ef->dev);
185 exfat_error("failed to allocate memory for the super block");
188 memset(ef->sb, 0, sizeof(struct exfat_super_block));
190 if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
192 exfat_close(ef->dev);
194 exfat_error("failed to read boot sector");
197 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
199 exfat_close(ef->dev);
201 exfat_error("exFAT file system is not found");
204 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
206 exfat_close(ef->dev);
207 exfat_error("unsupported exFAT version: %hhu.%hhu",
208 ef->sb->version.major, ef->sb->version.minor);
212 if (ef->sb->fat_count != 1)
214 exfat_close(ef->dev);
216 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
219 /* officially exFAT supports cluster size up to 32 MB */
220 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
222 exfat_close(ef->dev);
224 exfat_error("too big cluster size: 2^%d",
225 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
229 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
230 if (ef->zero_cluster == NULL)
232 exfat_close(ef->dev);
234 exfat_error("failed to allocate zero sector");
237 /* use zero_cluster as a temporary buffer for VBR checksum verification */
238 if (verify_vbr_checksum(ef->dev, ef->zero_cluster,
239 SECTOR_SIZE(*ef->sb)) != 0)
241 free(ef->zero_cluster);
242 exfat_close(ef->dev);
246 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
248 ef->root = malloc(sizeof(struct exfat_node));
249 if (ef->root == NULL)
251 free(ef->zero_cluster);
252 exfat_close(ef->dev);
254 exfat_error("failed to allocate root node");
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 */
266 /* always keep at least 1 reference to the root node */
267 exfat_get_node(ef->root);
269 rc = exfat_cache_directory(ef, ef->root);
272 if (ef->upcase == NULL)
274 exfat_error("upcase table is not found");
277 if (ef->cmap.chunk == NULL)
279 exfat_error("clusters bitmap is not found");
283 if (prepare_super_block(ef) != 0)
289 exfat_put_node(ef, ef->root);
290 exfat_reset_cache(ef);
292 free(ef->zero_cluster);
293 exfat_close(ef->dev);
298 static void finalize_super_block(struct exfat* ef)
303 ef->sb->volume_state = cpu_to_le16(
304 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
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)
310 uint32_t free, total;
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;
317 commit_super_block(ef);
320 void exfat_unmount(struct exfat* ef)
322 exfat_put_node(ef, ef->root);
323 exfat_reset_cache(ef);
326 finalize_super_block(ef);
327 exfat_close(ef->dev); /* close descriptor immediately after fsync */
329 free(ef->zero_cluster);
330 ef->zero_cluster = NULL;
331 free(ef->cmap.chunk);
332 ef->cmap.chunk = NULL;
337 ef->upcase_chars = 0;