OSDN Git Service

Handle I/O errors.
[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         if (exfat_pread(dev, sector, sector_size, 0) < 0)
101         {
102                 exfat_error("failed to read boot sector");
103                 return 1;
104         }
105         vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
106         for (i = 1; i < 11; i++)
107         {
108                 if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
109                 {
110                         exfat_error("failed to read VBR sector");
111                         return 1;
112                 }
113                 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
114                                 vbr_checksum);
115         }
116         if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
117         {
118                 exfat_error("failed to read VBR checksum sector");
119                 return 1;
120         }
121         for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
122                 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
123                 {
124                         exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
125                                         le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
126                         return 1;
127                 }
128         return 0;
129 }
130
131 static int commit_super_block(const struct exfat* ef)
132 {
133         if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
134         {
135                 exfat_error("failed to write super block");
136                 return 1;
137         }
138         return exfat_fsync(ef->dev);
139 }
140
141 static int prepare_super_block(const struct exfat* ef)
142 {
143         if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
144                 exfat_warn("volume was not unmounted cleanly");
145
146         if (ef->ro)
147                 return 0;
148
149         ef->sb->volume_state = cpu_to_le16(
150                         le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
151         return commit_super_block(ef);
152 }
153
154 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
155 {
156         int rc;
157         enum exfat_mode mode;
158
159         exfat_tzset();
160         memset(ef, 0, sizeof(struct exfat));
161
162         parse_options(ef, options);
163
164         if (match_option(options, "ro"))
165                 mode = EXFAT_MODE_RO;
166         else if (match_option(options, "ro_fallback"))
167                 mode = EXFAT_MODE_ANY;
168         else
169                 mode = EXFAT_MODE_RW;
170         ef->dev = exfat_open(spec, mode);
171         if (ef->dev == NULL)
172                 return -EIO;
173         if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
174         {
175                 if (mode == EXFAT_MODE_ANY)
176                         ef->ro = -1;
177                 else
178                         ef->ro = 1;
179         }
180
181         ef->sb = malloc(sizeof(struct exfat_super_block));
182         if (ef->sb == NULL)
183         {
184                 exfat_close(ef->dev);
185                 exfat_error("failed to allocate memory for the super block");
186                 return -ENOMEM;
187         }
188         memset(ef->sb, 0, sizeof(struct exfat_super_block));
189
190         if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
191         {
192                 exfat_error("failed to read boot sector");
193                 return -EIO;
194         }
195         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
196         {
197                 exfat_close(ef->dev);
198                 free(ef->sb);
199                 exfat_error("exFAT file system is not found");
200                 return -EIO;
201         }
202         if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
203         {
204                 exfat_close(ef->dev);
205                 exfat_error("unsupported exFAT version: %hhu.%hhu",
206                                 ef->sb->version.major, ef->sb->version.minor);
207                 free(ef->sb);
208                 return -EIO;
209         }
210         if (ef->sb->fat_count != 1)
211         {
212                 exfat_close(ef->dev);
213                 free(ef->sb);
214                 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
215                 return -EIO;
216         }
217         /* officially exFAT supports cluster size up to 32 MB */
218         if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
219         {
220                 exfat_close(ef->dev);
221                 free(ef->sb);
222                 exfat_error("too big cluster size: 2^%d",
223                                 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
224                 return -EIO;
225         }
226
227         ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
228         if (ef->zero_cluster == NULL)
229         {
230                 exfat_close(ef->dev);
231                 free(ef->sb);
232                 exfat_error("failed to allocate zero sector");
233                 return -ENOMEM;
234         }
235         /* use zero_cluster as a temporary buffer for VBR checksum verification */
236         if (verify_vbr_checksum(ef->dev, ef->zero_cluster,
237                         SECTOR_SIZE(*ef->sb)) != 0)
238         {
239                 free(ef->zero_cluster);
240                 exfat_close(ef->dev);
241                 free(ef->sb);
242                 return -EIO;
243         }
244         memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
245
246         ef->root = malloc(sizeof(struct exfat_node));
247         if (ef->root == NULL)
248         {
249                 free(ef->zero_cluster);
250                 exfat_close(ef->dev);
251                 free(ef->sb);
252                 exfat_error("failed to allocate root node");
253                 return -ENOMEM;
254         }
255         memset(ef->root, 0, sizeof(struct exfat_node));
256         ef->root->flags = EXFAT_ATTRIB_DIR;
257         ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
258         ef->root->fptr_cluster = ef->root->start_cluster;
259         ef->root->name[0] = cpu_to_le16('\0');
260         ef->root->size = rootdir_size(ef);
261         /* exFAT does not have time attributes for the root directory */
262         ef->root->mtime = 0;
263         ef->root->atime = 0;
264         /* always keep at least 1 reference to the root node */
265         exfat_get_node(ef->root);
266
267         rc = exfat_cache_directory(ef, ef->root);
268         if (rc != 0)
269                 goto error;
270         if (ef->upcase == NULL)
271         {
272                 exfat_error("upcase table is not found");
273                 goto error;
274         }
275         if (ef->cmap.chunk == NULL)
276         {
277                 exfat_error("clusters bitmap is not found");
278                 goto error;
279         }
280
281         if (prepare_super_block(ef) != 0)
282                 goto error;
283
284         return 0;
285
286 error:
287         exfat_put_node(ef, ef->root);
288         exfat_reset_cache(ef);
289         free(ef->root);
290         free(ef->zero_cluster);
291         exfat_close(ef->dev);
292         free(ef->sb);
293         return -EIO;
294 }
295
296 static void finalize_super_block(struct exfat* ef)
297 {
298         if (ef->ro)
299                 return;
300
301         ef->sb->volume_state = cpu_to_le16(
302                         le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
303
304         /* Some implementations set the percentage of allocated space to 0xff
305            on FS creation and never update it. In this case leave it as is. */
306         if (ef->sb->allocated_percent != 0xff)
307         {
308                 uint32_t free, total;
309
310                 free = exfat_count_free_clusters(ef);
311                 total = le32_to_cpu(ef->sb->cluster_count);
312                 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
313         }
314
315         commit_super_block(ef);
316 }
317
318 void exfat_unmount(struct exfat* ef)
319 {
320         exfat_put_node(ef, ef->root);
321         exfat_reset_cache(ef);
322         free(ef->root);
323         ef->root = NULL;
324         finalize_super_block(ef);
325         exfat_close(ef->dev);   /* close descriptor immediately after fsync */
326         ef->dev = NULL;
327         free(ef->zero_cluster);
328         ef->zero_cluster = NULL;
329         free(ef->cmap.chunk);
330         ef->cmap.chunk = NULL;
331         free(ef->sb);
332         ef->sb = NULL;
333         free(ef->upcase);
334         ef->upcase = NULL;
335         ef->upcase_chars = 0;
336 }