]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/mount.c
4240f63b7a34efece8f08f30db1feb241e64bb44
[sven/exfat-utils.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 #define _XOPEN_SOURCE /* for tzset() in Linux */
28 #include <time.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 int 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 1;
75         return 0;
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->ro = match_option(options, "ro");
92         ef->noatime = match_option(options, "noatime");
93 }
94
95 static int verify_vbr_checksum(void* sector, off_t sector_size, int fd)
96 {
97         uint32_t vbr_checksum;
98         int i;
99
100         exfat_read_raw(sector, sector_size, 0, fd);
101         vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
102         for (i = 1; i < 11; i++)
103         {
104                 exfat_read_raw(sector, sector_size, i * sector_size, fd);
105                 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
106                                 vbr_checksum);
107         }
108         exfat_read_raw(sector, sector_size, i * sector_size, fd);
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 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
120 {
121         int rc;
122
123         tzset();
124         memset(ef, 0, sizeof(struct exfat));
125
126         ef->sb = malloc(sizeof(struct exfat_super_block));
127         if (ef->sb == NULL)
128         {
129                 exfat_error("memory allocation failed");
130                 return -ENOMEM;
131         }
132         memset(ef->sb, 0, sizeof(struct exfat_super_block));
133
134         parse_options(ef, options);
135
136         ef->fd = exfat_open(spec, ef->ro);
137         if (ef->fd < 0)
138         {
139                 free(ef->sb);
140                 return -EIO;
141         }
142
143         exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
144         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
145         {
146                 close(ef->fd);
147                 free(ef->sb);
148                 exfat_error("exFAT file system is not found");
149                 return -EIO;
150         }
151         if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
152         {
153                 close(ef->fd);
154                 exfat_error("unsupported exFAT version: %hhu.%hhu",
155                                 ef->sb->version.major, ef->sb->version.minor);
156                 free(ef->sb);
157                 return -EIO;
158         }
159         if (ef->sb->fat_count != 1)
160         {
161                 close(ef->fd);
162                 free(ef->sb);
163                 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
164                 return -EIO;
165         }
166         /* officially exFAT supports cluster size up to 32 MB */
167         if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
168         {
169                 close(ef->fd);
170                 free(ef->sb);
171                 exfat_error("too big cluster size: 2^%d",
172                                 (int) ef->sb->sector_bits + (int) ef->sb->spc_bits);
173                 return -EIO;
174         }
175
176         ef->zero_sector = malloc(SECTOR_SIZE(*ef->sb));
177         if (ef->zero_sector == NULL)
178         {
179                 close(ef->fd);
180                 free(ef->sb);
181                 exfat_error("failed to allocate zero sector");
182                 return -ENOMEM;
183         }
184         /* use zero_sector as a temporary buffer for VBR checksum verification */
185         if (verify_vbr_checksum(ef->zero_sector, SECTOR_SIZE(*ef->sb), ef->fd) != 0)
186         {
187                 free(ef->zero_sector);
188                 close(ef->fd);
189                 free(ef->sb);
190                 return -EIO;
191         }
192         memset(ef->zero_sector, 0, SECTOR_SIZE(*ef->sb));
193
194         ef->root = malloc(sizeof(struct exfat_node));
195         if (ef->root == NULL)
196         {
197                 free(ef->zero_sector);
198                 close(ef->fd);
199                 free(ef->sb);
200                 exfat_error("failed to allocate root node");
201                 return -ENOMEM;
202         }
203         memset(ef->root, 0, sizeof(struct exfat_node));
204         ef->root->flags = EXFAT_ATTRIB_DIR;
205         ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
206         ef->root->fptr_cluster = ef->root->start_cluster;
207         ef->root->name[0] = cpu_to_le16('\0');
208         ef->root->size = rootdir_size(ef);
209         /* exFAT does not have time attributes for the root directory */
210         ef->root->mtime = 0;
211         ef->root->atime = 0;
212         /* always keep at least 1 reference to the root node */
213         exfat_get_node(ef->root);
214
215         rc = exfat_cache_directory(ef, ef->root);
216         if (rc != 0)
217                 goto error;
218         if (ef->upcase == NULL)
219         {
220                 exfat_error("upcase table is not found");
221                 goto error;
222         }
223         if (ef->cmap.chunk == NULL)
224         {
225                 exfat_error("clusters bitmap is not found");
226                 goto error;
227         }
228
229         return 0;
230
231 error:
232         exfat_put_node(ef, ef->root);
233         exfat_reset_cache(ef);
234         free(ef->root);
235         free(ef->zero_sector);
236         close(ef->fd);
237         free(ef->sb);
238         return -EIO;
239 }
240
241 void exfat_unmount(struct exfat* ef)
242 {
243         exfat_put_node(ef, ef->root);
244         exfat_reset_cache(ef);
245         free(ef->root);
246         ef->root = NULL;
247         free(ef->zero_sector);
248         ef->zero_sector = NULL;
249         free(ef->cmap.chunk);
250         ef->cmap.chunk = NULL;
251         if (fsync(ef->fd) < 0)
252                 exfat_error("fsync failed");
253         if (close(ef->fd) < 0)
254                 exfat_error("close failed");
255         ef->fd = 0;
256         free(ef->sb);
257         ef->sb = NULL;
258         free(ef->upcase);
259         ef->upcase = NULL;
260         ef->upcase_chars = 0;
261 }