OSDN Git Service

Use a more portable way to obtain timezone offset.
[android-x86/external-exfat.git] / libexfat / mount.c
1 /*
2         mount.c (22.10.09)
3         exFAT file system implementation library.
4
5         Copyright (C) 2009, 2010  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
28 static 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
43 static 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
54 static 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
64 static int 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 1;
73         return 0;
74 }
75
76 static 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->ro = match_option(options, "ro");
90         ef->noatime = match_option(options, "noatime");
91 }
92
93 static int verify_vbr_checksum(void* sector, off_t sector_size, int fd)
94 {
95         uint32_t vbr_checksum;
96         int i;
97
98         exfat_read_raw(sector, sector_size, 0, fd);
99         vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
100         for (i = 1; i < 11; i++)
101         {
102                 exfat_read_raw(sector, sector_size, i * sector_size, fd);
103                 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
104                                 vbr_checksum);
105         }
106         exfat_read_raw(sector, sector_size, i * sector_size, fd);
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
117 static int commit_super_block(const struct exfat* ef)
118 {
119         exfat_write_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
120         if (fsync(ef->fd) < 0)
121         {
122                 exfat_error("fsync failed");
123                 return 1;
124         }
125         return 0;
126 }
127
128 static int prepare_super_block(const struct exfat* ef)
129 {
130         if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
131                 exfat_warn("volume was not unmounted cleanly");
132
133         if (ef->ro)
134                 return 0;
135
136         ef->sb->volume_state = cpu_to_le16(
137                         le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
138         return commit_super_block(ef);
139 }
140
141 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
142 {
143         int rc;
144
145         exfat_tzset();
146         memset(ef, 0, sizeof(struct exfat));
147
148         parse_options(ef, options);
149
150         ef->fd = exfat_open(spec, ef->ro);
151         if (ef->fd < 0)
152         {
153                 if (ef->ro || !match_option(options, "ro_fallback"))
154                         return -EIO;
155                 ef->fd = exfat_open(spec, 1);
156                 if (ef->fd < 0)
157                         return -EIO;
158                 exfat_warn("device is write-protected, mounting read-only");
159                 ef->ro_fallback = ef->ro = 1;
160         }
161
162         ef->sb = malloc(sizeof(struct exfat_super_block));
163         if (ef->sb == NULL)
164         {
165                 close(ef->fd);
166                 exfat_error("failed to allocate memory for the super block");
167                 return -ENOMEM;
168         }
169         memset(ef->sb, 0, sizeof(struct exfat_super_block));
170
171         exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
172         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
173         {
174                 close(ef->fd);
175                 free(ef->sb);
176                 exfat_error("exFAT file system is not found");
177                 return -EIO;
178         }
179         if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
180         {
181                 close(ef->fd);
182                 exfat_error("unsupported exFAT version: %hhu.%hhu",
183                                 ef->sb->version.major, ef->sb->version.minor);
184                 free(ef->sb);
185                 return -EIO;
186         }
187         if (ef->sb->fat_count != 1)
188         {
189                 close(ef->fd);
190                 free(ef->sb);
191                 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
192                 return -EIO;
193         }
194         /* officially exFAT supports cluster size up to 32 MB */
195         if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
196         {
197                 close(ef->fd);
198                 free(ef->sb);
199                 exfat_error("too big cluster size: 2^%d",
200                                 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
201                 return -EIO;
202         }
203
204         ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
205         if (ef->zero_cluster == NULL)
206         {
207                 close(ef->fd);
208                 free(ef->sb);
209                 exfat_error("failed to allocate zero sector");
210                 return -ENOMEM;
211         }
212         /* use zero_cluster as a temporary buffer for VBR checksum verification */
213         if (verify_vbr_checksum(ef->zero_cluster, SECTOR_SIZE(*ef->sb),
214                         ef->fd) != 0)
215         {
216                 free(ef->zero_cluster);
217                 close(ef->fd);
218                 free(ef->sb);
219                 return -EIO;
220         }
221         memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
222
223         ef->root = malloc(sizeof(struct exfat_node));
224         if (ef->root == NULL)
225         {
226                 free(ef->zero_cluster);
227                 close(ef->fd);
228                 free(ef->sb);
229                 exfat_error("failed to allocate root node");
230                 return -ENOMEM;
231         }
232         memset(ef->root, 0, sizeof(struct exfat_node));
233         ef->root->flags = EXFAT_ATTRIB_DIR;
234         ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
235         ef->root->fptr_cluster = ef->root->start_cluster;
236         ef->root->name[0] = cpu_to_le16('\0');
237         ef->root->size = rootdir_size(ef);
238         /* exFAT does not have time attributes for the root directory */
239         ef->root->mtime = 0;
240         ef->root->atime = 0;
241         /* always keep at least 1 reference to the root node */
242         exfat_get_node(ef->root);
243
244         rc = exfat_cache_directory(ef, ef->root);
245         if (rc != 0)
246                 goto error;
247         if (ef->upcase == NULL)
248         {
249                 exfat_error("upcase table is not found");
250                 goto error;
251         }
252         if (ef->cmap.chunk == NULL)
253         {
254                 exfat_error("clusters bitmap is not found");
255                 goto error;
256         }
257
258         if (prepare_super_block(ef) != 0)
259                 goto error;
260
261         return 0;
262
263 error:
264         exfat_put_node(ef, ef->root);
265         exfat_reset_cache(ef);
266         free(ef->root);
267         free(ef->zero_cluster);
268         close(ef->fd);
269         free(ef->sb);
270         return -EIO;
271 }
272
273 static void finalize_super_block(struct exfat* ef)
274 {
275         if (ef->ro)
276                 return;
277
278         ef->sb->volume_state = cpu_to_le16(
279                         le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
280
281         /* Some implementations set the percentage of allocated space to 0xff
282            on FS creation and never update it. In this case leave it as is. */
283         if (ef->sb->allocated_percent != 0xff)
284         {
285                 uint32_t free, total;
286
287                 free = exfat_count_free_clusters(ef);
288                 total = le32_to_cpu(ef->sb->cluster_count);
289                 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
290         }
291
292         commit_super_block(ef);
293 }
294
295 void exfat_unmount(struct exfat* ef)
296 {
297         exfat_put_node(ef, ef->root);
298         exfat_reset_cache(ef);
299         free(ef->root);
300         ef->root = NULL;
301         finalize_super_block(ef);
302         if (close(ef->fd) < 0)  /* close descriptor immediately after fsync */
303                 exfat_error("close failed");
304         ef->fd = 0;
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 }