OSDN Git Service

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