OSDN Git Service

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