blob: 44fb581386518b2e4c648074aefd2c79e5429177 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 mount.c (22.10.09)
3 exFAT file system implementation library.
4
5 Copyright (C) 2010-2012 Andrew Nayenko
6
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.
11
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.
16
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/>.
19*/
20
21#include "exfat.h"
22#include <string.h>
23#include <stdlib.h>
24#include <errno.h>
25#include <unistd.h>
26#include <sys/types.h>
27
28static uint64_t rootdir_size(const struct exfat* ef)
29{
30 uint64_t clusters = 0;
31 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
32
33 while (!CLUSTER_INVALID(rootdir_cluster))
34 {
35 clusters++;
36 /* root directory cannot be contiguous because there is no flag
37 to indicate this */
38 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
39 }
40 return clusters * CLUSTER_SIZE(*ef->sb);
41}
42
43static const char* get_option(const char* options, const char* option_name)
44{
45 const char* p;
46 size_t length = strlen(option_name);
47
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;
51 return NULL;
52}
53
54static int get_int_option(const char* options, const char* option_name,
55 int base, int default_value)
56{
57 const char* p = get_option(options, option_name);
58
59 if (p == NULL)
60 return default_value;
61 return strtol(p, NULL, base);
62}
63
64static bool match_option(const char* options, const char* option_name)
65{
66 const char* p;
67 size_t length = strlen(option_name);
68
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'))
72 return true;
73 return false;
74}
75
76static void parse_options(struct exfat* ef, const char* options)
77{
78 int sys_umask = umask(0);
79 int opt_umask;
80
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;
85
86 ef->uid = get_int_option(options, "uid", 10, geteuid());
87 ef->gid = get_int_option(options, "gid", 10, getegid());
88
89 ef->noatime = match_option(options, "noatime");
90}
91
92static int verify_vbr_checksum(struct exfat_dev* dev, void* sector,
93 off64_t sector_size)
94{
95 uint32_t vbr_checksum;
96 int i;
97
98 exfat_pread(dev, sector, sector_size, 0);
99 vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
100 for (i = 1; i < 11; i++)
101 {
102 exfat_pread(dev, sector, sector_size, i * sector_size);
103 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
104 vbr_checksum);
105 }
106 exfat_pread(dev, sector, sector_size, i * sector_size);
107 for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
108 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
109 {
110 exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
111 le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
112 return 1;
113 }
114 return 0;
115}
116
117static int commit_super_block(const struct exfat* ef)
118{
119 exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0);
120 return exfat_fsync(ef->dev);
121}
122
123static int prepare_super_block(const struct exfat* ef)
124{
125 if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
126 exfat_warn("volume was not unmounted cleanly");
127
128 if (ef->ro)
129 return 0;
130
131 ef->sb->volume_state = cpu_to_le16(
132 le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
133 return commit_super_block(ef);
134}
135
136int exfat_mount(struct exfat* ef, const char* spec, const char* options)
137{
138 int rc;
139 enum exfat_mode mode;
140
141 exfat_tzset();
142 memset(ef, 0, sizeof(struct exfat));
143
144 parse_options(ef, options);
145
146 if (match_option(options, "ro"))
147 mode = EXFAT_MODE_RO;
148 else if (match_option(options, "ro_fallback"))
149 mode = EXFAT_MODE_ANY;
150 else
151 mode = EXFAT_MODE_RW;
152 ef->dev = exfat_open(spec, mode);
153 if (ef->dev == NULL)
154 return -EIO;
155 if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
156 {
157 if (mode == EXFAT_MODE_ANY)
158 ef->ro = -1;
159 else
160 ef->ro = 1;
161 }
162
163 ef->sb = malloc(sizeof(struct exfat_super_block));
164 if (ef->sb == NULL)
165 {
166 exfat_close(ef->dev);
167 exfat_error("failed to allocate memory for the super block");
168 return -ENOMEM;
169 }
170 memset(ef->sb, 0, sizeof(struct exfat_super_block));
171
172 exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0);
173 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
174 {
175 exfat_close(ef->dev);
176 free(ef->sb);
177 exfat_error("exFAT file system is not found");
178 return -EIO;
179 }
180 if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
181 {
182 exfat_close(ef->dev);
183 exfat_error("unsupported exFAT version: %hhu.%hhu",
184 ef->sb->version.major, ef->sb->version.minor);
185 free(ef->sb);
186 return -EIO;
187 }
188 if (ef->sb->fat_count != 1)
189 {
190 exfat_close(ef->dev);
191 free(ef->sb);
192 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
193 return -EIO;
194 }
195 /* officially exFAT supports cluster size up to 32 MB */
196 if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
197 {
198 exfat_close(ef->dev);
199 free(ef->sb);
200 exfat_error("too big cluster size: 2^%d",
201 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
202 return -EIO;
203 }
204
205 ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
206 if (ef->zero_cluster == NULL)
207 {
208 exfat_close(ef->dev);
209 free(ef->sb);
210 exfat_error("failed to allocate zero sector");
211 return -ENOMEM;
212 }
213 /* use zero_cluster as a temporary buffer for VBR checksum verification */
214 if (verify_vbr_checksum(ef->dev, ef->zero_cluster,
215 SECTOR_SIZE(*ef->sb)) != 0)
216 {
217 free(ef->zero_cluster);
218 exfat_close(ef->dev);
219 free(ef->sb);
220 return -EIO;
221 }
222 memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
223
224 ef->root = malloc(sizeof(struct exfat_node));
225 if (ef->root == NULL)
226 {
227 free(ef->zero_cluster);
228 exfat_close(ef->dev);
229 free(ef->sb);
230 exfat_error("failed to allocate root node");
231 return -ENOMEM;
232 }
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 */
240 ef->root->mtime = 0;
241 ef->root->atime = 0;
242 /* always keep at least 1 reference to the root node */
243 exfat_get_node(ef->root);
244
245 rc = exfat_cache_directory(ef, ef->root);
246 if (rc != 0)
247 goto error;
248 if (ef->upcase == NULL)
249 {
250 exfat_error("upcase table is not found");
251 goto error;
252 }
253 if (ef->cmap.chunk == NULL)
254 {
255 exfat_error("clusters bitmap is not found");
256 goto error;
257 }
258
259 if (prepare_super_block(ef) != 0)
260 goto error;
261
262 return 0;
263
264error:
265 exfat_put_node(ef, ef->root);
266 exfat_reset_cache(ef);
267 free(ef->root);
268 free(ef->zero_cluster);
269 exfat_close(ef->dev);
270 free(ef->sb);
271 return -EIO;
272}
273
274static void finalize_super_block(struct exfat* ef)
275{
276 if (ef->ro)
277 return;
278
279 ef->sb->volume_state = cpu_to_le16(
280 le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
281
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)
285 {
286 uint32_t free, total;
287
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;
291 }
292
293 commit_super_block(ef);
294}
295
296void exfat_unmount(struct exfat* ef)
297{
298 exfat_put_node(ef, ef->root);
299 exfat_reset_cache(ef);
300 free(ef->root);
301 ef->root = NULL;
302 finalize_super_block(ef);
303 exfat_close(ef->dev); /* close descriptor immediately after fsync */
304 ef->dev = NULL;
305 free(ef->zero_cluster);
306 ef->zero_cluster = NULL;
307 free(ef->cmap.chunk);
308 ef->cmap.chunk = NULL;
309 free(ef->sb);
310 ef->sb = NULL;
311 free(ef->upcase);
312 ef->upcase = NULL;
313 ef->upcase_chars = 0;
314}