]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/mount.c
releasing package exfat-utils version 1.3.0-2
[sven/exfat-utils.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-2018  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         uint32_t clusters = 0;
34         uint32_t clusters_max = le32_to_cpu(ef->sb->cluster_count);
35         cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
36
37         /* Iterate all clusters of the root directory to calculate its size.
38            It can't be contiguous because there is no flag to indicate this. */
39         do
40         {
41                 if (clusters == clusters_max) /* infinite loop detected */
42                 {
43                         exfat_error("root directory cannot occupy all %d clusters",
44                                         clusters);
45                         return 0;
46                 }
47                 if (CLUSTER_INVALID(*ef->sb, rootdir_cluster))
48                 {
49                         exfat_error("bad cluster %#x while reading root directory",
50                                         rootdir_cluster);
51                         return 0;
52                 }
53                 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
54                 clusters++;
55         }
56         while (rootdir_cluster != EXFAT_CLUSTER_END);
57
58         return (uint64_t) clusters * CLUSTER_SIZE(*ef->sb);
59 }
60
61 static const char* get_option(const char* options, const char* option_name)
62 {
63         const char* p;
64         size_t length = strlen(option_name);
65
66         for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
67                 if ((p == options || p[-1] == ',') && p[length] == '=')
68                         return p + length + 1;
69         return NULL;
70 }
71
72 static int get_int_option(const char* options, const char* option_name,
73                 int base, int default_value)
74 {
75         const char* p = get_option(options, option_name);
76
77         if (p == NULL)
78                 return default_value;
79         return strtol(p, NULL, base);
80 }
81
82 static bool match_option(const char* options, const char* option_name)
83 {
84         const char* p;
85         size_t length = strlen(option_name);
86
87         for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
88                 if ((p == options || p[-1] == ',') &&
89                                 (p[length] == ',' || p[length] == '\0'))
90                         return true;
91         return false;
92 }
93
94 static void parse_options(struct exfat* ef, const char* options)
95 {
96         int opt_umask;
97
98         opt_umask = get_int_option(options, "umask", 8, 0);
99         ef->dmask = get_int_option(options, "dmask", 8, opt_umask);
100         ef->fmask = get_int_option(options, "fmask", 8, opt_umask);
101
102         ef->uid = get_int_option(options, "uid", 10, geteuid());
103         ef->gid = get_int_option(options, "gid", 10, getegid());
104
105         ef->noatime = match_option(options, "noatime");
106
107         switch (get_int_option(options, "repair", 10, 0))
108         {
109         case 1:
110                 ef->repair = EXFAT_REPAIR_ASK;
111                 break;
112         case 2:
113                 ef->repair = EXFAT_REPAIR_YES;
114                 break;
115         default:
116                 ef->repair = EXFAT_REPAIR_NO;
117                 break;
118         }
119 }
120
121 static bool verify_vbr_checksum(const struct exfat* ef, void* sector)
122 {
123         off_t sector_size = SECTOR_SIZE(*ef->sb);
124         uint32_t vbr_checksum;
125         int i;
126
127         if (exfat_pread(ef->dev, sector, sector_size, 0) < 0)
128         {
129                 exfat_error("failed to read boot sector");
130                 return false;
131         }
132         vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
133         for (i = 1; i < 11; i++)
134         {
135                 if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
136                 {
137                         exfat_error("failed to read VBR sector");
138                         return false;
139                 }
140                 vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
141                                 vbr_checksum);
142         }
143         if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
144         {
145                 exfat_error("failed to read VBR checksum sector");
146                 return false;
147         }
148         for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
149                 if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum)
150                 {
151                         exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
152                                         le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
153                         if (!EXFAT_REPAIR(invalid_vbr_checksum, ef, sector, vbr_checksum))
154                                 return false;
155                 }
156         return true;
157 }
158
159 static int commit_super_block(const struct exfat* ef)
160 {
161         if (exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
162         {
163                 exfat_error("failed to write super block");
164                 return 1;
165         }
166         return exfat_fsync(ef->dev);
167 }
168
169 static int prepare_super_block(const struct exfat* ef)
170 {
171         if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
172                 exfat_warn("volume was not unmounted cleanly");
173
174         if (ef->ro)
175                 return 0;
176
177         ef->sb->volume_state = cpu_to_le16(
178                         le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED);
179         return commit_super_block(ef);
180 }
181
182 static void exfat_free(struct exfat* ef)
183 {
184         exfat_close(ef->dev);   /* first of all, close the descriptor */
185         ef->dev = NULL;                 /* struct exfat_dev is freed by exfat_close() */
186         free(ef->root);
187         ef->root = NULL;
188         free(ef->zero_cluster);
189         ef->zero_cluster = NULL;
190         free(ef->cmap.chunk);
191         ef->cmap.chunk = NULL;
192         free(ef->upcase);
193         ef->upcase = NULL;
194         free(ef->sb);
195         ef->sb = NULL;
196 }
197
198 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
199 {
200         int rc;
201         enum exfat_mode mode;
202
203         exfat_tzset();
204         memset(ef, 0, sizeof(struct exfat));
205
206         parse_options(ef, options);
207
208         if (match_option(options, "ro"))
209                 mode = EXFAT_MODE_RO;
210         else if (match_option(options, "ro_fallback"))
211                 mode = EXFAT_MODE_ANY;
212         else
213                 mode = EXFAT_MODE_RW;
214         ef->dev = exfat_open(spec, mode);
215         if (ef->dev == NULL)
216                 return -EIO;
217         if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
218         {
219                 if (mode == EXFAT_MODE_ANY)
220                         ef->ro = -1;
221                 else
222                         ef->ro = 1;
223         }
224
225         ef->sb = malloc(sizeof(struct exfat_super_block));
226         if (ef->sb == NULL)
227         {
228                 exfat_error("failed to allocate memory for the super block");
229                 exfat_free(ef);
230                 return -ENOMEM;
231         }
232         memset(ef->sb, 0, sizeof(struct exfat_super_block));
233
234         if (exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0) < 0)
235         {
236                 exfat_error("failed to read boot sector");
237                 exfat_free(ef);
238                 return -EIO;
239         }
240         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
241         {
242                 exfat_error("exFAT file system is not found");
243                 exfat_free(ef);
244                 return -EIO;
245         }
246         /* sector cannot be smaller than 512 bytes */
247         if (ef->sb->sector_bits < 9)
248         {
249                 exfat_error("too small sector size: 2^%hhd", ef->sb->sector_bits);
250                 exfat_free(ef);
251                 return -EIO;
252         }
253         /* officially exFAT supports cluster size up to 32 MB */
254         if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25)
255         {
256                 exfat_error("too big cluster size: 2^(%hhd+%hhd)",
257                                 ef->sb->sector_bits, ef->sb->spc_bits);
258                 exfat_free(ef);
259                 return -EIO;
260         }
261         ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb));
262         if (ef->zero_cluster == NULL)
263         {
264                 exfat_error("failed to allocate zero sector");
265                 exfat_free(ef);
266                 return -ENOMEM;
267         }
268         /* use zero_cluster as a temporary buffer for VBR checksum verification */
269         if (!verify_vbr_checksum(ef, ef->zero_cluster))
270         {
271                 exfat_free(ef);
272                 return -EIO;
273         }
274         memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb));
275         if (ef->sb->version.major != 1 || ef->sb->version.minor != 0)
276         {
277                 exfat_error("unsupported exFAT version: %hhu.%hhu",
278                                 ef->sb->version.major, ef->sb->version.minor);
279                 exfat_free(ef);
280                 return -EIO;
281         }
282         if (ef->sb->fat_count != 1)
283         {
284                 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
285                 exfat_free(ef);
286                 return -EIO;
287         }
288         if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
289                         exfat_get_size(ef->dev))
290         {
291                 /* this can cause I/O errors later but we don't fail mounting to let
292                    user rescue data */
293                 exfat_warn("file system in sectors is larger than device: "
294                                 "%"PRIu64" * %d > %"PRIu64,
295                                 le64_to_cpu(ef->sb->sector_count), SECTOR_SIZE(*ef->sb),
296                                 exfat_get_size(ef->dev));
297         }
298         if ((off_t) le32_to_cpu(ef->sb->cluster_count) * CLUSTER_SIZE(*ef->sb) >
299                         exfat_get_size(ef->dev))
300         {
301                 exfat_error("file system in clusters is larger than device: "
302                                 "%u * %d > %"PRIu64,
303                                 le32_to_cpu(ef->sb->cluster_count), CLUSTER_SIZE(*ef->sb),
304                                 exfat_get_size(ef->dev));
305                 exfat_free(ef);
306                 return -EIO;
307         }
308
309         ef->root = malloc(sizeof(struct exfat_node));
310         if (ef->root == NULL)
311         {
312                 exfat_error("failed to allocate root node");
313                 exfat_free(ef);
314                 return -ENOMEM;
315         }
316         memset(ef->root, 0, sizeof(struct exfat_node));
317         ef->root->attrib = EXFAT_ATTRIB_DIR;
318         ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
319         ef->root->fptr_cluster = ef->root->start_cluster;
320         ef->root->name[0] = cpu_to_le16('\0');
321         ef->root->size = rootdir_size(ef);
322         if (ef->root->size == 0)
323         {
324                 exfat_free(ef);
325                 return -EIO;
326         }
327         /* exFAT does not have time attributes for the root directory */
328         ef->root->mtime = 0;
329         ef->root->atime = 0;
330         /* always keep at least 1 reference to the root node */
331         exfat_get_node(ef->root);
332
333         rc = exfat_cache_directory(ef, ef->root);
334         if (rc != 0)
335                 goto error;
336         if (ef->upcase == NULL)
337         {
338                 exfat_error("upcase table is not found");
339                 goto error;
340         }
341         if (ef->cmap.chunk == NULL)
342         {
343                 exfat_error("clusters bitmap is not found");
344                 goto error;
345         }
346
347         if (prepare_super_block(ef) != 0)
348                 goto error;
349
350         return 0;
351
352 error:
353         exfat_put_node(ef, ef->root);
354         exfat_reset_cache(ef);
355         exfat_free(ef);
356         return -EIO;
357 }
358
359 static void finalize_super_block(struct exfat* ef)
360 {
361         if (ef->ro)
362                 return;
363
364         ef->sb->volume_state = cpu_to_le16(
365                         le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED);
366
367         /* Some implementations set the percentage of allocated space to 0xff
368            on FS creation and never update it. In this case leave it as is. */
369         if (ef->sb->allocated_percent != 0xff)
370         {
371                 uint32_t free, total;
372
373                 free = exfat_count_free_clusters(ef);
374                 total = le32_to_cpu(ef->sb->cluster_count);
375                 ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total;
376         }
377
378         commit_super_block(ef); /* ignore return code */
379 }
380
381 void exfat_unmount(struct exfat* ef)
382 {
383         exfat_flush_nodes(ef);  /* ignore return code */
384         exfat_flush(ef);                /* ignore return code */
385         exfat_put_node(ef, ef->root);
386         exfat_reset_cache(ef);
387         finalize_super_block(ef);
388         exfat_free(ef);                 /* will close the descriptor */
389 }