OSDN Git Service

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