]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/mount.c
Add the fix for https://github.com/relan/exfat/issues/6 found and reported by The...
[sven/exfat-utils.git] / libexfat / mount.c
1 /*
2         mount.c (22.10.09)
3         exFAT file system implementation library.
4
5         Free exFAT implementation.
6         Copyright (C) 2010-2014  Andrew Nayenko
7
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.
12
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.
17
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.
21 */
22
23 #include "exfat.h"
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30
31 static uint64_t rootdir_size(const struct exfat* ef)
32 {
33         uint32_t clusters = 0;
34         uint32_t clusters_max = le32_to_cpu(ef->sb->cluster_count);
35         cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
36
37         /* Iterate all clusters of the root directory to calculate its size.
38            It can't be contiguous because there is no flag to indicate this. */
39         do
40         {
41                 if (clusters == clusters_max) /* infinite loop detected */
42                 {
43                         exfat_error("root directory cannot occupy all %d clusters",
44                                         clusters);
45                         return 0;
46                 }
47                 if (CLUSTER_INVALID(rootdir_cluster))
48                 {
49                         exfat_error("bad cluster %#x while reading root directory",
50                                         rootdir_cluster);
51                         return 0;
52                 }
53                 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
54                 clusters++;
55         }
56         while (rootdir_cluster != EXFAT_CLUSTER_END);
57
58         return (uint64_t) clusters * CLUSTER_SIZE(*ef->sb);
59 }
60
61 static const char* get_option(const char* options, const char* option_name)
62 {
63         const char* p;
64         size_t length = strlen(option_name);
65
66         for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
67                 if ((p == options || p[-1] == ',') && p[length] == '=')
68                         return p + length + 1;
69         return NULL;
70 }
71
72 static int get_int_option(const char* options, const char* option_name,
73                 int base, int default_value)
74 {
75         const char* p = get_option(options, option_name);
76
77         if (p == NULL)
78                 return default_value;
79         return strtol(p, NULL, base);
80 }
81
82 static bool match_option(const char* options, const char* option_name)
83 {
84         const char* p;
85         size_t length = strlen(option_name);
86
87         for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
88                 if ((p == options || p[-1] == ',') &&
89                                 (p[length] == ',' || p[length] == '\0'))
90                         return true;
91         return false;
92 }
93
94 static void parse_options(struct exfat* ef, const char* options)
95 {
96         int sys_umask = umask(0);
97         int opt_umask;
98
99         umask(sys_umask); /* restore umask */
100         opt_umask = get_int_option(options, "umask", 8, sys_umask);
101         ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
102         ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
103
104         ef->uid = get_int_option(options, "uid", 10, geteuid());
105         ef->gid = get_int_option(options, "gid", 10, getegid());
106
107         ef->noatime = match_option(options, "noatime");
108 }
109
110 static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
111                 off_t sector_size)
112 {
113         uint32_t vbr_checksum;
114         int i;
115
116         if (exfat_pread(dev, sector, sector_size, 0) < 0)
117         {
118                 exfat_error("failed to read boot sector");
119                 return false;
120         }
121         vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
122         for (i = 1; i < 11; i++)
123         {
124                 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
125                 {
126                         exfat_error("failed to read VBR sector");
127                         return false;
128                 }
129                 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
130                                 vbr_checksum);
131         }
132         if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
133         {
134                 exfat_error("failed to read VBR checksum sector");
135                 return false;
136         }
137         for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
138                 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
139                 {
140                         exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
141                                         le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
142                         return false;
143                 }
144         return true;
145 }
146
147 static int commit_super_block(const struct exfat* ef)
148 {
149         if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
150         {
151                 exfat_error("failed to write super block");
152                 return 1;
153         }
154         return exfat_fsync(ef->dev);
155 }
156
157 static int prepare_super_block(const struct exfat* ef)
158 {
159         if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
160                 exfat_warn("volume was not unmounted cleanly");
161
162         if (ef->ro)
163                 return 0;
164
165         ef->sb->volume_state = cpu_to_le16(
166                         le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
167         return commit_super_block(ef);
168 }
169
170 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
171 {
172         int rc;
173         enum exfat_mode mode;
174
175         exfat_tzset();
176         memset(ef, 0, sizeof(struct exfat));
177
178         parse_options(ef, options);
179
180         if (match_option(options, "ro"))
181                 mode = EXFAT_MODE_RO;
182         else if (match_option(options, "ro_fallback"))
183                 mode = EXFAT_MODE_ANY;
184         else
185                 mode = EXFAT_MODE_RW;
186         ef->dev = exfat_open(spec, mode);
187         if (ef->dev == NULL)
188                 return -EIO;
189         if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
190         {
191                 if (mode == EXFAT_MODE_ANY)
192                         ef->ro = -1;
193                 else
194                         ef->ro = 1;
195         }
196
197         ef->sb = malloc(sizeof(struct exfat_super_block));
198         if (ef->sb == NULL)
199         {
200                 exfat_close(ef->dev);
201                 exfat_error("failed to allocate memory for the super block");
202                 return -ENOMEM;
203         }
204         memset(ef->sb, 0, sizeof(struct exfat_super_block));
205
206         if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
207         {
208                 exfat_close(ef->dev);
209                 free(ef->sb);
210                 exfat_error("failed to read boot sector");
211                 return -EIO;
212         }
213         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
214         {
215                 exfat_close(ef->dev);
216                 free(ef->sb);
217                 exfat_error("exFAT file system is not found");
218                 return -EIO;
219         }
220         /* sector cannot be smaller than 512 bytes */
221         if (ef->sb->sector_bits < 9)
222         {
223                 exfat_close(ef->dev);
224                 exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits);
225                 free(ef->sb);
226                 return -EIO;
227         }
228         /* officially exFAT supports cluster size up to 32 MB */
229         if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
230         {
231                 exfat_close(ef->dev);
232                 exfat_error("too big cluster size: 2^(%hhd+%hhd)",
233                                 ef->sb->sector_bits, ef->sb->spc_bits);
234                 free(ef->sb);
235                 return -EIO;
236         }
237         ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
238         if (ef->zero_cluster == NULL)
239         {
240                 exfat_close(ef->dev);
241                 free(ef->sb);
242                 exfat_error("failed to allocate zero sector");
243                 return -ENOMEM;
244         }
245         /* use zero_cluster as a temporary buffer for VBR checksum verification */
246         if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb)))
247         {
248                 free(ef->zero_cluster);
249                 exfat_close(ef->dev);
250                 free(ef->sb);
251                 return -EIO;
252         }
253         memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
254         if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
255         {
256                 free(ef->zero_cluster);
257                 exfat_close(ef->dev);
258                 exfat_error("unsupported exFAT version: %hhu.%hhu",
259                                 ef->sb->version.major, ef->sb->version.minor);
260                 free(ef->sb);
261                 return -EIO;
262         }
263         if (ef->sb->fat_count != 1)
264         {
265                 free(ef->zero_cluster);
266                 exfat_close(ef->dev);
267                 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
268                 free(ef->sb);
269                 return -EIO;
270         }
271         if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
272                         exfat_get_size(ef->dev))
273         {
274                 free(ef->zero_cluster);
275                 exfat_error("file system is larger than underlying device: "
276                                 "%"PRIu64" > %"PRIu64,
277                                 le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb),
278                                 exfat_get_size(ef->dev));
279                 exfat_close(ef->dev);
280                 free(ef->sb);
281                 return -EIO;
282         }
283
284         ef->root = malloc(sizeof(struct exfat_node));
285         if (ef->root == NULL)
286         {
287                 free(ef->zero_cluster);
288                 exfat_close(ef->dev);
289                 free(ef->sb);
290                 exfat_error("failed to allocate root node");
291                 return -ENOMEM;
292         }
293         memset(ef->root, 0, sizeof(struct exfat_node));
294         ef->root->flags = EXFAT_ATTRIB_DIR;
295         ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
296         ef->root->fptr_cluster = ef->root->start_cluster;
297         ef->root->name[0] = cpu_to_le16('\0');
298         ef->root->size = rootdir_size(ef);
299         if (ef->root->size == 0)
300         {
301                 free(ef->root);
302                 free(ef->zero_cluster);
303                 exfat_close(ef->dev);
304                 free(ef->sb);
305                 return -EIO;
306         }
307         /* exFAT does not have time attributes for the root directory */
308         ef->root->mtime = 0;
309         ef->root->atime = 0;
310         /* always keep at least 1 reference to the root node */
311         exfat_get_node(ef->root);
312
313         rc = exfat_cache_directory(ef, ef->root);
314         if (rc != 0)
315                 goto error;
316         if (ef->upcase == NULL)
317         {
318                 exfat_error("upcase table is not found");
319                 goto error;
320         }
321         if (ef->cmap.chunk == NULL)
322         {
323                 exfat_error("clusters bitmap is not found");
324                 goto error;
325         }
326
327         if (prepare_super_block(ef) != 0)
328                 goto error;
329
330         return 0;
331
332 error:
333         exfat_put_node(ef, ef->root);
334         exfat_reset_cache(ef);
335         free(ef->root);
336         free(ef->zero_cluster);
337         exfat_close(ef->dev);
338         free(ef->sb);
339         return -EIO;
340 }
341
342 static void finalize_super_block(struct exfat* ef)
343 {
344         if (ef->ro)
345                 return;
346
347         ef->sb->volume_state = cpu_to_le16(
348                         le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
349
350         /* Some implementations set the percentage of allocated space to 0xff
351            on FS creation and never update it. In this case leave it as is. */
352         if (ef->sb->allocated_percent != 0xff)
353         {
354                 uint32_t free, total;
355
356                 free = exfat_count_free_clusters(ef);
357                 total = le32_to_cpu(ef->sb->cluster_count);
358                 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
359         }
360
361         commit_super_block(ef); /* ignore return code */
362 }
363
364 void exfat_unmount(struct exfat* ef)
365 {
366         exfat_flush(ef);        /* ignore return code */
367         exfat_put_node(ef, ef->root);
368         exfat_reset_cache(ef);
369         free(ef->root);
370         ef->root = NULL;
371         finalize_super_block(ef);
372         exfat_close(ef->dev);   /* close descriptor immediately after fsync */
373         ef->dev = NULL;
374         free(ef->zero_cluster);
375         ef->zero_cluster = NULL;
376         free(ef->cmap.chunk);
377         ef->cmap.chunk = NULL;
378         free(ef->sb);
379         ef->sb = NULL;
380         free(ef->upcase);
381         ef->upcase = NULL;
382         ef->upcase_chars = 0;
383 }