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>
29 static uint64_t rootdir_size(const struct exfat* ef)
31 uint64_t clusters = 0;
32 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
34 while (!CLUSTER_INVALID(rootdir_cluster))
37 /* root directory cannot be contiguous because there is no flag
39 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
41 return clusters * CLUSTER_SIZE(*ef->sb);
44 static const char* get_option(const char* options, const char* option_name)
47 size_t length = strlen(option_name);
49 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
50 if ((p == options || p[-1] == ',') && p[length] == '=')
51 return p + length + 1;
55 static int get_int_option(const char* options, const char* option_name,
56 int base, int default_value)
58 const char* p = get_option(options, option_name);
62 return strtol(p, NULL, base);
65 static int match_option(const char* options, const char* option_name)
68 size_t length = strlen(option_name);
70 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
71 if ((p == options || p[-1] == ',') &&
72 (p[length] == ',' || p[length] == '\0'))
77 static void parse_options(struct exfat* ef, const char* options)
79 int sys_umask = umask(0);
82 umask(sys_umask); /* restore umask */
83 opt_umask = get_int_option(options, "umask", 8, sys_umask);
84 ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
85 ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
87 ef->uid = get_int_option(options, "uid", 10, geteuid());
88 ef->gid = get_int_option(options, "gid", 10, getegid());
90 ef->ro = match_option(options, "ro");
91 ef->noatime = match_option(options, "noatime");
94 static int verify_vbr_checksum(void* sector, off_t sector_size, int fd)
96 uint32_t vbr_checksum;
99 exfat_read_raw(sector, sector_size, 0, fd);
100 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
101 for (i = 1; i < 11; i++)
103 exfat_read_raw(sector, sector_size, i * sector_size, fd);
104 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
107 exfat_read_raw(sector, sector_size, i * sector_size, fd);
108 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
109 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
111 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
112 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
118 static int commit_super_block(const struct exfat* ef)
120 exfat_write_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
121 if (fsync(ef->fd) < 0)
123 exfat_error("fsync failed");
129 static int prepare_super_block(const struct exfat* ef)
131 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
132 exfat_warn("volume was not unmounted cleanly");
137 ef->sb->volume_state = cpu_to_le16(
138 le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
139 return commit_super_block(ef);
142 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
147 memset(ef, 0, sizeof(struct exfat));
149 parse_options(ef, options);
151 ef->fd = exfat_open(spec, ef->ro);
154 if (ef->ro || !match_option(options, "ro_fallback"))
156 ef->fd = exfat_open(spec, 1);
159 exfat_warn("device is write-protected, mounting read-only");
160 ef->ro_fallback = ef->ro = 1;
163 ef->sb = malloc(sizeof(struct exfat_super_block));
167 exfat_error("failed to allocate memory for the super block");
170 memset(ef->sb, 0, sizeof(struct exfat_super_block));
172 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
173 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
177 exfat_error("exFAT file system is not found");
180 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
183 exfat_error("unsupported exFAT version: %hhu.%hhu",
184 ef->sb->version.major, ef->sb->version.minor);
188 if (ef->sb->fat_count != 1)
192 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
195 /* officially exFAT supports cluster size up to 32 MB */
196 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
200 exfat_error("too big cluster size: 2^%d",
201 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
205 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
206 if (ef->zero_cluster == NULL)
210 exfat_error("failed to allocate zero sector");
213 /* use zero_cluster as a temporary buffer for VBR checksum verification */
214 if (verify_vbr_checksum(ef->zero_cluster, SECTOR_SIZE(*ef->sb),
217 free(ef->zero_cluster);
222 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
224 ef->root = malloc(sizeof(struct exfat_node));
225 if (ef->root == NULL)
227 free(ef->zero_cluster);
230 exfat_error("failed to allocate root node");
233 memset(ef->root, 0, sizeof(struct exfat_node));
234 ef->root->flags = EXFAT_ATTRIB_DIR;
235 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
236 ef->root->fptr_cluster = ef->root->start_cluster;
237 ef->root->name[0] = cpu_to_le16('\0');
238 ef->root->size = rootdir_size(ef);
239 /* exFAT does not have time attributes for the root directory */
242 /* always keep at least 1 reference to the root node */
243 exfat_get_node(ef->root);
245 rc = exfat_cache_directory(ef, ef->root);
248 if (ef->upcase == NULL)
250 exfat_error("upcase table is not found");
253 if (ef->cmap.chunk == NULL)
255 exfat_error("clusters bitmap is not found");
259 if (prepare_super_block(ef) != 0)
265 exfat_put_node(ef, ef->root);
266 exfat_reset_cache(ef);
268 free(ef->zero_cluster);
274 static void finalize_super_block(struct exfat* ef)
279 ef->sb->volume_state = cpu_to_le16(
280 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
282 /* Some implementations set the percentage of allocated space to 0xff
283 on FS creation and never update it. In this case leave it as is. */
284 if (ef->sb->allocated_percent != 0xff)
286 uint32_t free, total;
288 free = exfat_count_free_clusters(ef);
289 total = le32_to_cpu(ef->sb->cluster_count);
290 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
293 commit_super_block(ef);
296 void exfat_unmount(struct exfat* ef)
298 exfat_put_node(ef, ef->root);
299 exfat_reset_cache(ef);
302 finalize_super_block(ef);
303 if (close(ef->fd) < 0) /* close descriptor immediately after fsync */
304 exfat_error("close failed");
306 free(ef->zero_cluster);
307 ef->zero_cluster = NULL;
308 free(ef->cmap.chunk);
309 ef->cmap.chunk = NULL;
314 ef->upcase_chars = 0;