OSDN Git Service

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