OSDN Git Service

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