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 if (rootdir_cluster != EXFAT_CLUSTER_END)
44 exfat_error("bad cluster %#x while reading root directory",
48 return clusters * CLUSTER_SIZE(*ef->sb);
51 static const char* get_option(const char* options, const char* option_name)
54 size_t length = strlen(option_name);
56 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
57 if ((p == options || p[-1] == ',') && p[length] == '=')
58 return p + length + 1;
62 static int get_int_option(const char* options, const char* option_name,
63 int base, int default_value)
65 const char* p = get_option(options, option_name);
69 return strtol(p, NULL, base);
72 static bool match_option(const char* options, const char* option_name)
75 size_t length = strlen(option_name);
77 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
78 if ((p == options || p[-1] == ',') &&
79 (p[length] == ',' || p[length] == '\0'))
84 static void parse_options(struct exfat* ef, const char* options)
86 int sys_umask = umask(0);
89 umask(sys_umask); /* restore umask */
90 opt_umask = get_int_option(options, "umask", 8, sys_umask);
91 ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
92 ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
94 ef->uid = get_int_option(options, "uid", 10, geteuid());
95 ef->gid = get_int_option(options, "gid", 10, getegid());
97 ef->noatime = match_option(options, "noatime");
100 static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
103 uint32_t vbr_checksum;
106 if (exfat_pread(dev, sector, sector_size, 0) < 0)
108 exfat_error("failed to read boot sector");
111 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
112 for (i = 1; i < 11; i++)
114 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
116 exfat_error("failed to read VBR sector");
119 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
122 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
124 exfat_error("failed to read VBR checksum sector");
127 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
128 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
130 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
131 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
137 static int commit_super_block(const struct exfat* ef)
139 if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
141 exfat_error("failed to write super block");
144 return exfat_fsync(ef->dev);
147 static int prepare_super_block(const struct exfat* ef)
149 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
150 exfat_warn("volume was not unmounted cleanly");
155 ef->sb->volume_state = cpu_to_le16(
156 le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
157 return commit_super_block(ef);
160 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
163 enum exfat_mode mode;
166 memset(ef, 0, sizeof(struct exfat));
168 parse_options(ef, options);
170 if (match_option(options, "ro"))
171 mode = EXFAT_MODE_RO;
172 else if (match_option(options, "ro_fallback"))
173 mode = EXFAT_MODE_ANY;
175 mode = EXFAT_MODE_RW;
176 ef->dev = exfat_open(spec, mode);
179 if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
181 if (mode == EXFAT_MODE_ANY)
187 ef->sb = malloc(sizeof(struct exfat_super_block));
190 exfat_close(ef->dev);
191 exfat_error("failed to allocate memory for the super block");
194 memset(ef->sb, 0, sizeof(struct exfat_super_block));
196 if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
198 exfat_close(ef->dev);
200 exfat_error("failed to read boot sector");
203 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
205 exfat_close(ef->dev);
207 exfat_error("exFAT file system is not found");
210 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
212 exfat_close(ef->dev);
213 exfat_error("unsupported exFAT version: %hhu.%hhu",
214 ef->sb->version.major, ef->sb->version.minor);
218 if (ef->sb->fat_count != 1)
220 exfat_close(ef->dev);
222 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
225 /* officially exFAT supports cluster size up to 32 MB */
226 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
228 exfat_close(ef->dev);
230 exfat_error("too big cluster size: 2^%d",
231 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
235 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
236 if (ef->zero_cluster == NULL)
238 exfat_close(ef->dev);
240 exfat_error("failed to allocate zero sector");
243 /* use zero_cluster as a temporary buffer for VBR checksum verification */
244 if (verify_vbr_checksum(ef->dev, ef->zero_cluster,
245 SECTOR_SIZE(*ef->sb)) != 0)
247 free(ef->zero_cluster);
248 exfat_close(ef->dev);
252 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
254 ef->root = malloc(sizeof(struct exfat_node));
255 if (ef->root == NULL)
257 free(ef->zero_cluster);
258 exfat_close(ef->dev);
260 exfat_error("failed to allocate root node");
263 memset(ef->root, 0, sizeof(struct exfat_node));
264 ef->root->flags = EXFAT_ATTRIB_DIR;
265 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
266 ef->root->fptr_cluster = ef->root->start_cluster;
267 ef->root->name[0] = cpu_to_le16('\0');
268 ef->root->size = rootdir_size(ef);
269 if (ef->root->size == 0)
272 free(ef->zero_cluster);
273 exfat_close(ef->dev);
277 /* exFAT does not have time attributes for the root directory */
280 /* always keep at least 1 reference to the root node */
281 exfat_get_node(ef->root);
283 rc = exfat_cache_directory(ef, ef->root);
286 if (ef->upcase == NULL)
288 exfat_error("upcase table is not found");
291 if (ef->cmap.chunk == NULL)
293 exfat_error("clusters bitmap is not found");
297 if (prepare_super_block(ef) != 0)
303 exfat_put_node(ef, ef->root);
304 exfat_reset_cache(ef);
306 free(ef->zero_cluster);
307 exfat_close(ef->dev);
312 static void finalize_super_block(struct exfat* ef)
317 ef->sb->volume_state = cpu_to_le16(
318 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
320 /* Some implementations set the percentage of allocated space to 0xff
321 on FS creation and never update it. In this case leave it as is. */
322 if (ef->sb->allocated_percent != 0xff)
324 uint32_t free, total;
326 free = exfat_count_free_clusters(ef);
327 total = le32_to_cpu(ef->sb->cluster_count);
328 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
331 commit_super_block(ef);
334 void exfat_unmount(struct exfat* ef)
336 exfat_put_node(ef, ef->root);
337 exfat_reset_cache(ef);
340 finalize_super_block(ef);
341 exfat_close(ef->dev); /* close descriptor immediately after fsync */
343 free(ef->zero_cluster);
344 ef->zero_cluster = NULL;
345 free(ef->cmap.chunk);
346 ef->cmap.chunk = NULL;
351 ef->upcase_chars = 0;