3 exFAT file system implementation library.
5 Copyright (C) 2010-2012 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>
28 static uint64_t rootdir_size(const struct exfat* ef)
30 uint64_t clusters = 0;
31 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
33 while (!CLUSTER_INVALID(rootdir_cluster))
36 /* root directory cannot be contiguous because there is no flag
38 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
40 return clusters * CLUSTER_SIZE(*ef->sb);
43 static const char* get_option(const char* options, const char* option_name)
46 size_t length = strlen(option_name);
48 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
49 if ((p == options || p[-1] == ',') && p[length] == '=')
50 return p + length + 1;
54 static int get_int_option(const char* options, const char* option_name,
55 int base, int default_value)
57 const char* p = get_option(options, option_name);
61 return strtol(p, NULL, base);
64 static int match_option(const char* options, const char* option_name)
67 size_t length = strlen(option_name);
69 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
70 if ((p == options || p[-1] == ',') &&
71 (p[length] == ',' || p[length] == '\0'))
76 static void parse_options(struct exfat* ef, const char* options)
78 int sys_umask = umask(0);
81 umask(sys_umask); /* restore umask */
82 opt_umask = get_int_option(options, "umask", 8, sys_umask);
83 ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
84 ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
86 ef->uid = get_int_option(options, "uid", 10, geteuid());
87 ef->gid = get_int_option(options, "gid", 10, getegid());
89 ef->ro = match_option(options, "ro");
90 ef->noatime = match_option(options, "noatime");
93 static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
96 uint32_t vbr_checksum;
99 exfat_pread(dev, sector, sector_size, 0);
100 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
101 for (i = 1; i < 11; i++)
103 exfat_pread(dev, sector, sector_size, i * sector_size);
104 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
107 exfat_pread(dev, sector, sector_size, i * sector_size);
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_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0);
121 return exfat_fsync(ef->dev);
124 static int prepare_super_block(const struct exfat* ef)
126 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
127 exfat_warn("volume was not unmounted cleanly");
132 ef->sb->volume_state = cpu_to_le16(
133 le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
134 return commit_super_block(ef);
137 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
142 memset(ef, 0, sizeof(struct exfat));
144 parse_options(ef, options);
146 ef->dev = exfat_open(spec, ef->ro);
149 if (ef->ro || !match_option(options, "ro_fallback"))
151 ef->dev = exfat_open(spec, 1);
154 exfat_warn("device is write-protected, mounting read-only");
155 ef->ro_fallback = ef->ro = 1;
158 ef->sb = malloc(sizeof(struct exfat_super_block));
161 exfat_close(ef->dev);
162 exfat_error("failed to allocate memory for the super block");
165 memset(ef->sb, 0, sizeof(struct exfat_super_block));
167 exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0);
168 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
170 exfat_close(ef->dev);
172 exfat_error("exFAT file system is not found");
175 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
177 exfat_close(ef->dev);
178 exfat_error("unsupported exFAT version: %hhu.%hhu",
179 ef->sb->version.major, ef->sb->version.minor);
183 if (ef->sb->fat_count != 1)
185 exfat_close(ef->dev);
187 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
190 /* officially exFAT supports cluster size up to 32 MB */
191 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
193 exfat_close(ef->dev);
195 exfat_error("too big cluster size: 2^%d",
196 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
200 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
201 if (ef->zero_cluster == NULL)
203 exfat_close(ef->dev);
205 exfat_error("failed to allocate zero sector");
208 /* use zero_cluster as a temporary buffer for VBR checksum verification */
209 if (verify_vbr_checksum(ef->dev, ef->zero_cluster,
210 SECTOR_SIZE(*ef->sb)) != 0)
212 free(ef->zero_cluster);
213 exfat_close(ef->dev);
217 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
219 ef->root = malloc(sizeof(struct exfat_node));
220 if (ef->root == NULL)
222 free(ef->zero_cluster);
223 exfat_close(ef->dev);
225 exfat_error("failed to allocate root node");
228 memset(ef->root, 0, sizeof(struct exfat_node));
229 ef->root->flags = EXFAT_ATTRIB_DIR;
230 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
231 ef->root->fptr_cluster = ef->root->start_cluster;
232 ef->root->name[0] = cpu_to_le16('\0');
233 ef->root->size = rootdir_size(ef);
234 /* exFAT does not have time attributes for the root directory */
237 /* always keep at least 1 reference to the root node */
238 exfat_get_node(ef->root);
240 rc = exfat_cache_directory(ef, ef->root);
243 if (ef->upcase == NULL)
245 exfat_error("upcase table is not found");
248 if (ef->cmap.chunk == NULL)
250 exfat_error("clusters bitmap is not found");
254 if (prepare_super_block(ef) != 0)
260 exfat_put_node(ef, ef->root);
261 exfat_reset_cache(ef);
263 free(ef->zero_cluster);
264 exfat_close(ef->dev);
269 static void finalize_super_block(struct exfat* ef)
274 ef->sb->volume_state = cpu_to_le16(
275 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
277 /* Some implementations set the percentage of allocated space to 0xff
278 on FS creation and never update it. In this case leave it as is. */
279 if (ef->sb->allocated_percent != 0xff)
281 uint32_t free, total;
283 free = exfat_count_free_clusters(ef);
284 total = le32_to_cpu(ef->sb->cluster_count);
285 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
288 commit_super_block(ef);
291 void exfat_unmount(struct exfat* ef)
293 exfat_put_node(ef, ef->root);
294 exfat_reset_cache(ef);
297 finalize_super_block(ef);
298 exfat_close(ef->dev); /* close descriptor immediately after fsync */
300 free(ef->zero_cluster);
301 ef->zero_cluster = NULL;
302 free(ef->cmap.chunk);
303 ef->cmap.chunk = NULL;
308 ef->upcase_chars = 0;