3 exFAT file system implementation library.
5 Copyright (C) 2009, 2010 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
27 #define _XOPEN_SOURCE /* for tzset() in Linux */
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 int 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->ro = match_option(options, "ro");
92 ef->noatime = match_option(options, "noatime");
95 static int verify_vbr_checksum(void* sector, off_t sector_size, int fd)
97 uint32_t vbr_checksum;
100 exfat_read_raw(sector, sector_size, 0, fd);
101 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
102 for (i = 1; i < 11; i++)
104 exfat_read_raw(sector, sector_size, i * sector_size, fd);
105 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
108 exfat_read_raw(sector, sector_size, i * sector_size, fd);
109 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
110 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
112 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
113 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
119 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
124 memset(ef, 0, sizeof(struct exfat));
126 parse_options(ef, options);
128 ef->fd = exfat_open(spec, ef->ro);
132 ef->sb = malloc(sizeof(struct exfat_super_block));
136 exfat_error("failed to allocate memory for the super block");
139 memset(ef->sb, 0, sizeof(struct exfat_super_block));
141 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
142 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
146 exfat_error("exFAT file system is not found");
149 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
152 exfat_error("unsupported exFAT version: %hhu.%hhu",
153 ef->sb->version.major, ef->sb->version.minor);
157 if (ef->sb->fat_count != 1)
161 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
164 /* officially exFAT supports cluster size up to 32 MB */
165 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
169 exfat_error("too big cluster size: 2^%d",
170 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
174 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
175 if (ef->zero_cluster == NULL)
179 exfat_error("failed to allocate zero sector");
182 /* use zero_cluster as a temporary buffer for VBR checksum verification */
183 if (verify_vbr_checksum(ef->zero_cluster, SECTOR_SIZE(*ef->sb),
186 free(ef->zero_cluster);
191 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
193 ef->root = malloc(sizeof(struct exfat_node));
194 if (ef->root == NULL)
196 free(ef->zero_cluster);
199 exfat_error("failed to allocate root node");
202 memset(ef->root, 0, sizeof(struct exfat_node));
203 ef->root->flags = EXFAT_ATTRIB_DIR;
204 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
205 ef->root->fptr_cluster = ef->root->start_cluster;
206 ef->root->name[0] = cpu_to_le16('\0');
207 ef->root->size = rootdir_size(ef);
208 /* exFAT does not have time attributes for the root directory */
211 /* always keep at least 1 reference to the root node */
212 exfat_get_node(ef->root);
214 rc = exfat_cache_directory(ef, ef->root);
217 if (ef->upcase == NULL)
219 exfat_error("upcase table is not found");
222 if (ef->cmap.chunk == NULL)
224 exfat_error("clusters bitmap is not found");
231 exfat_put_node(ef, ef->root);
232 exfat_reset_cache(ef);
234 free(ef->zero_cluster);
240 void exfat_unmount(struct exfat* ef)
242 exfat_put_node(ef, ef->root);
243 exfat_reset_cache(ef);
246 free(ef->zero_cluster);
247 ef->zero_cluster = NULL;
248 free(ef->cmap.chunk);
249 ef->cmap.chunk = NULL;
250 if (fsync(ef->fd) < 0)
251 exfat_error("fsync failed");
252 if (close(ef->fd) < 0)
253 exfat_error("close failed");
259 ef->upcase_chars = 0;