OSDN Git Service

50f29e9c0bbb529ec3745b3050d3bd185de30bbe
[android-x86/external-exfat.git] / libexfat / node.c
1 /*
2         node.c (09.10.09)
3         exFAT file system implementation library.
4
5         Free exFAT implementation.
6         Copyright (C) 2010-2016  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 <errno.h>
25 #include <string.h>
26 #include <inttypes.h>
27
28 /* on-disk nodes iterator */
29 struct iterator
30 {
31         cluster_t cluster;
32         off_t offset;
33         char* chunk;
34 };
35
36 struct exfat_node* exfat_get_node(struct exfat_node* node)
37 {
38         /* if we switch to multi-threaded mode we will need atomic
39            increment here and atomic decrement in exfat_put_node() */
40         node->references++;
41         return node;
42 }
43
44 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
45 {
46         char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
47
48         --node->references;
49         if (node->references < 0)
50         {
51                 exfat_get_name(node, buffer);
52                 exfat_bug("reference counter of '%s' is below zero", buffer);
53         }
54         else if (node->references == 0 && node != ef->root)
55         {
56                 if (node->is_dirty)
57                 {
58                         exfat_get_name(node, buffer);
59                         exfat_warn("dirty node '%s' with zero references", buffer);
60                 }
61         }
62 }
63
64 /**
65  * This function must be called on rmdir and unlink (after the last
66  * exfat_put_node()) to free clusters.
67  */
68 int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
69 {
70         int rc = 0;
71
72         if (node->references != 0)
73                 exfat_bug("unable to cleanup a node with %d references",
74                                 node->references);
75
76         if (node->is_unlinked)
77         {
78                 /* free all clusters and node structure itself */
79                 rc = exfat_truncate(ef, node, 0, true);
80                 /* free the node even in case of error or its memory will be lost */
81                 free(node);
82         }
83         return rc;
84 }
85
86 /**
87  * Cluster + offset from the beginning of the directory to absolute offset.
88  */
89 static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
90 {
91         return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
92 }
93
94 static int opendir(struct exfat* ef, const struct exfat_node* dir,
95                 struct iterator* it)
96 {
97         char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
98
99         if (!(dir->attrib & EXFAT_ATTRIB_DIR))
100         {
101                 exfat_get_name(dir, buffer);
102                 exfat_bug("'%s' is not a directory", buffer);
103         }
104         if (CLUSTER_INVALID(dir->start_cluster))
105         {
106                 exfat_get_name(dir, buffer);
107                 exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
108                                 dir->start_cluster);
109                 return -EIO;
110         }
111         it->cluster = dir->start_cluster;
112         it->offset = 0;
113         it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
114         if (it->chunk == NULL)
115         {
116                 exfat_error("failed to allocate memory for directory cluster");
117                 return -ENOMEM;
118         }
119         if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
120                         exfat_c2o(ef, it->cluster)) < 0)
121         {
122                 free(it->chunk);
123                 exfat_get_name(dir, buffer);
124                 exfat_error("failed to read '%s' directory cluster %#x", buffer,
125                                 it->cluster);
126                 return -EIO;
127         }
128         return 0;
129 }
130
131 static void closedir(struct iterator* it)
132 {
133         it->cluster = 0;
134         it->offset = 0;
135         free(it->chunk);
136         it->chunk = NULL;
137 }
138
139 static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
140                 struct iterator* it)
141 {
142         /* move iterator to the next entry in the directory */
143         it->offset += sizeof(struct exfat_entry);
144         /* fetch the next cluster if needed */
145         if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
146         {
147                 /* reached the end of directory; the caller should check this
148                    condition too */
149                 if (it->offset >= parent->size)
150                         return true;
151                 it->cluster = exfat_next_cluster(ef, parent, it->cluster);
152                 if (CLUSTER_INVALID(it->cluster))
153                 {
154                         exfat_error("invalid cluster 0x%x while reading directory",
155                                         it->cluster);
156                         return false;
157                 }
158                 if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
159                                 exfat_c2o(ef, it->cluster)) < 0)
160                 {
161                         exfat_error("failed to read the next directory cluster %#x",
162                                         it->cluster);
163                         return false;
164                 }
165         }
166         return true;
167 }
168
169 static int read_entries(struct exfat* ef, struct exfat_node* dir,
170                 struct exfat_entry* entries, int n, off_t offset)
171 {
172         ssize_t size;
173
174         if (!(dir->attrib & EXFAT_ATTRIB_DIR))
175                 exfat_bug("attempted to read entries from a file");
176
177         size = exfat_generic_pread(ef, dir, entries,
178                         sizeof(struct exfat_entry[n]), offset);
179         if (size == sizeof(struct exfat_entry[n]))
180                 return 0; /* success */
181         if (size == 0)
182                 return -ENOENT;
183         if (size < 0)
184                 return -EIO;
185         exfat_error("read %zd bytes instead of %zu bytes", size,
186                         sizeof(struct exfat_entry[n]));
187         return -EIO;
188 }
189
190 static int write_entries(struct exfat* ef, struct exfat_node* dir,
191                 const struct exfat_entry* entries, int n, off_t offset)
192 {
193         ssize_t size;
194
195         if (!(dir->attrib & EXFAT_ATTRIB_DIR))
196                 exfat_bug("attempted to write entries into a file");
197
198         size = exfat_generic_pwrite(ef, dir, entries,
199                         sizeof(struct exfat_entry[n]), offset);
200         if (size == sizeof(struct exfat_entry[n]))
201                 return 0; /* success */
202         if (size < 0)
203                 return -EIO;
204         exfat_error("wrote %zd bytes instead of %zu bytes", size,
205                         sizeof(struct exfat_entry[n]));
206         return -EIO;
207 }
208
209 static struct exfat_node* allocate_node(void)
210 {
211         struct exfat_node* node = malloc(sizeof(struct exfat_node));
212         if (node == NULL)
213         {
214                 exfat_error("failed to allocate node");
215                 return NULL;
216         }
217         memset(node, 0, sizeof(struct exfat_node));
218         return node;
219 }
220
221 static void init_node_meta1(struct exfat_node* node,
222                 const struct exfat_entry_meta1* meta1)
223 {
224         node->attrib = le16_to_cpu(meta1->attrib);
225         node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
226                         meta1->mtime_cs);
227         /* there is no centiseconds field for atime */
228         node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0);
229 }
230
231 static void init_node_meta2(struct exfat_node* node,
232                 const struct exfat_entry_meta2* meta2)
233 {
234         node->size = le64_to_cpu(meta2->size);
235         node->start_cluster = le32_to_cpu(meta2->start_cluster);
236         node->fptr_cluster = node->start_cluster;
237         node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
238 }
239
240 static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
241                 const struct iterator* it)
242 {
243         return (const struct exfat_entry*)
244                         (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
245 }
246
247 static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
248                 uint16_t reference_checksum, uint64_t valid_size, int cluster_size)
249 {
250         char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
251         bool ret = true;
252
253         /*
254            Validate checksum first. If it's invalid all other fields probably
255            contain just garbage.
256         */
257         if (actual_checksum != reference_checksum)
258         {
259                 exfat_get_name(node, buffer);
260                 exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
261                                 actual_checksum, reference_checksum);
262                 ret = false;
263         }
264
265         /*
266            exFAT does not support sparse files but allows files with uninitialized
267            clusters. For such files valid_size means initialized data size and
268            cannot be greater than file size. See SetFileValidData() function
269            description in MSDN.
270         */
271         if (valid_size > node->size)
272         {
273                 exfat_get_name(node, buffer);
274                 exfat_error("'%s' has valid size (%"PRIu64") greater than size "
275                                 "(%"PRIu64")", buffer, valid_size, node->size);
276                 ret = false;
277         }
278
279         /*
280            Empty file must have zero start cluster. Non-empty file must start
281            with a valid cluster. Directories cannot be empty (i.e. must always
282            have a valid start cluster), but we will check this later in opendir()
283            to give user a chance to read current directory.
284         */
285         if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
286         {
287                 exfat_get_name(node, buffer);
288                 exfat_error("'%s' is empty but start cluster is %#x", buffer,
289                                 node->start_cluster);
290                 ret = false;
291         }
292         if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
293         {
294                 exfat_get_name(node, buffer);
295                 exfat_error("'%s' points to invalid cluster %#x", buffer,
296                                 node->start_cluster);
297                 ret = false;
298         }
299
300         /* Empty file or directory must be marked as non-contiguous. */
301         if (node->size == 0 && node->is_contiguous)
302         {
303                 exfat_get_name(node, buffer);
304                 exfat_error("'%s' is empty but marked as contiguous (%#hx)", buffer,
305                                 node->attrib);
306                 ret = false;
307         }
308
309         /* Directory size must be aligned on at cluster boundary. */
310         if ((node->attrib & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
311         {
312                 exfat_get_name(node, buffer);
313                 exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
314                                 node->size, cluster_size);
315                 ret = false;
316         }
317
318         return ret;
319 }
320
321 static void decompress_upcase(uint16_t* output, const le16_t* source,
322                 size_t size)
323 {
324         size_t si;
325         size_t oi;
326
327         for (oi = 0; oi < EXFAT_UPCASE_CHARS; oi++)
328                 output[oi] = oi;
329
330         for (si = 0, oi = 0; si < size && oi < EXFAT_UPCASE_CHARS; si++)
331         {
332                 uint16_t ch = le16_to_cpu(source[si]);
333
334                 if (ch == 0xffff && si + 1 < size)      /* indicates a run */
335                         oi += le16_to_cpu(source[++si]);
336                 else
337                         output[oi++] = ch;
338         }
339 }
340
341 /*
342  * Reads one entry in directory at position pointed by iterator and fills
343  * node structure.
344  */
345 static int readdir(struct exfat* ef, const struct exfat_node* parent,
346                 struct exfat_node** node, struct iterator* it)
347 {
348         int rc = -EIO;
349         const struct exfat_entry* entry;
350         const struct exfat_entry_meta1* meta1;
351         const struct exfat_entry_meta2* meta2;
352         const struct exfat_entry_name* file_name;
353         const struct exfat_entry_upcase* upcase;
354         const struct exfat_entry_bitmap* bitmap;
355         const struct exfat_entry_label* label;
356         uint8_t continuations = 0;
357         le16_t* namep = NULL;
358         uint16_t reference_checksum = 0;
359         uint16_t actual_checksum = 0;
360         uint64_t valid_size = 0;
361         uint64_t upcase_size = 0;
362         le16_t* upcase_comp = NULL;
363
364         *node = NULL;
365
366         for (;;)
367         {
368                 if (it->offset >= parent->size)
369                 {
370                         if (continuations != 0)
371                         {
372                                 exfat_error("expected %hhu continuations", continuations);
373                                 goto error;
374                         }
375                         return -ENOENT; /* that's OK, means end of directory */
376                 }
377
378                 entry = get_entry_ptr(ef, it);
379                 switch (entry->type)
380                 {
381                 case EXFAT_ENTRY_FILE:
382                         if (continuations != 0)
383                         {
384                                 exfat_error("expected %hhu continuations before new entry",
385                                                 continuations);
386                                 goto error;
387                         }
388                         meta1 = (const struct exfat_entry_meta1*) entry;
389                         continuations = meta1->continuations;
390                         /* each file entry must have at least 2 continuations:
391                            info and name */
392                         if (continuations < 2)
393                         {
394                                 exfat_error("too few continuations (%hhu)", continuations);
395                                 goto error;
396                         }
397                         if (continuations > 1 +
398                                         DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
399                         {
400                                 exfat_error("too many continuations (%hhu)", continuations);
401                                 goto error;
402                         }
403                         reference_checksum = le16_to_cpu(meta1->checksum);
404                         actual_checksum = exfat_start_checksum(meta1);
405                         *node = allocate_node();
406                         if (*node == NULL)
407                         {
408                                 rc = -ENOMEM;
409                                 goto error;
410                         }
411                         /* new node has zero reference counter */
412                         (*node)->entry_cluster = it->cluster;
413                         (*node)->entry_offset = it->offset;
414                         init_node_meta1(*node, meta1);
415                         namep = (*node)->name;
416                         break;
417
418                 case EXFAT_ENTRY_FILE_INFO:
419                         if (continuations < 2)
420                         {
421                                 exfat_error("unexpected continuation (%hhu)",
422                                                 continuations);
423                                 goto error;
424                         }
425                         meta2 = (const struct exfat_entry_meta2*) entry;
426                         if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
427                         {
428                                 exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
429                                 goto error;
430                         }
431                         init_node_meta2(*node, meta2);
432                         actual_checksum = exfat_add_checksum(entry, actual_checksum);
433                         valid_size = le64_to_cpu(meta2->valid_size);
434                         --continuations;
435                         break;
436
437                 case EXFAT_ENTRY_FILE_NAME:
438                         if (continuations == 0)
439                         {
440                                 exfat_error("unexpected continuation");
441                                 goto error;
442                         }
443                         file_name = (const struct exfat_entry_name*) entry;
444                         actual_checksum = exfat_add_checksum(entry, actual_checksum);
445
446                         memcpy(namep, file_name->name,
447                                         MIN(EXFAT_ENAME_MAX,
448                                                 ((*node)->name + EXFAT_NAME_MAX - namep)) *
449                                         sizeof(le16_t));
450                         namep += EXFAT_ENAME_MAX;
451                         if (--continuations == 0)
452                         {
453                                 if (!check_node(*node, actual_checksum, reference_checksum,
454                                                 valid_size, CLUSTER_SIZE(*ef->sb)))
455                                         goto error;
456                                 if (!fetch_next_entry(ef, parent, it))
457                                         goto error;
458                                 return 0; /* entry completed */
459                         }
460                         break;
461
462                 case EXFAT_ENTRY_UPCASE:
463                         if (ef->upcase != NULL)
464                                 break;
465                         upcase = (const struct exfat_entry_upcase*) entry;
466                         if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
467                         {
468                                 exfat_error("invalid cluster 0x%x in upcase table",
469                                                 le32_to_cpu(upcase->start_cluster));
470                                 goto error;
471                         }
472                         upcase_size = le64_to_cpu(upcase->size);
473                         if (upcase_size == 0 ||
474                                 upcase_size > EXFAT_UPCASE_CHARS * sizeof(uint16_t) ||
475                                 upcase_size % sizeof(uint16_t) != 0)
476                         {
477                                 exfat_error("bad upcase table size (%"PRIu64" bytes)",
478                                                 upcase_size);
479                                 goto error;
480                         }
481                         upcase_comp = malloc(upcase_size);
482                         if (upcase_comp == NULL)
483                         {
484                                 exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
485                                                 upcase_size);
486                                 rc = -ENOMEM;
487                                 goto error;
488                         }
489
490                         /* read compressed upcase table */
491                         if (exfat_pread(ef->dev, upcase_comp, upcase_size,
492                                         exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))) < 0)
493                         {
494                                 free(upcase_comp);
495                                 exfat_error("failed to read upper case table "
496                                                 "(%"PRIu64" bytes starting at cluster %#x)",
497                                                 upcase_size,
498                                                 le32_to_cpu(upcase->start_cluster));
499                                 goto error;
500                         }
501
502                         /* decompress upcase table */
503                         ef->upcase = calloc(EXFAT_UPCASE_CHARS, sizeof(uint16_t));
504                         if (ef->upcase == NULL)
505                         {
506                                 free(upcase_comp);
507                                 exfat_error("failed to allocate decompressed upcase table");
508                                 rc = -ENOMEM;
509                                 goto error;
510                         }
511                         decompress_upcase(ef->upcase, upcase_comp,
512                                         upcase_size / sizeof(uint16_t));
513                         free(upcase_comp);
514                         break;
515
516                 case EXFAT_ENTRY_BITMAP:
517                         bitmap = (const struct exfat_entry_bitmap*) entry;
518                         ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
519                         if (CLUSTER_INVALID(ef->cmap.start_cluster))
520                         {
521                                 exfat_error("invalid cluster 0x%x in clusters bitmap",
522                                                 ef->cmap.start_cluster);
523                                 goto error;
524                         }
525                         ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
526                                 EXFAT_FIRST_DATA_CLUSTER;
527                         if (le64_to_cpu(bitmap->size) < DIV_ROUND_UP(ef->cmap.size, 8))
528                         {
529                                 exfat_error("invalid clusters bitmap size: %"PRIu64
530                                                 " (expected at least %u)",
531                                                 le64_to_cpu(bitmap->size),
532                                                 DIV_ROUND_UP(ef->cmap.size, 8));
533                                 goto error;
534                         }
535                         /* FIXME bitmap can be rather big, up to 512 MB */
536                         ef->cmap.chunk_size = ef->cmap.size;
537                         ef->cmap.chunk = malloc(BMAP_SIZE(ef->cmap.chunk_size));
538                         if (ef->cmap.chunk == NULL)
539                         {
540                                 exfat_error("failed to allocate clusters bitmap chunk "
541                                                 "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
542                                 rc = -ENOMEM;
543                                 goto error;
544                         }
545
546                         if (exfat_pread(ef->dev, ef->cmap.chunk,
547                                         BMAP_SIZE(ef->cmap.chunk_size),
548                                         exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
549                         {
550                                 exfat_error("failed to read clusters bitmap "
551                                                 "(%"PRIu64" bytes starting at cluster %#x)",
552                                                 le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
553                                 goto error;
554                         }
555                         break;
556
557                 case EXFAT_ENTRY_LABEL:
558                         label = (const struct exfat_entry_label*) entry;
559                         if (label->length > EXFAT_ENAME_MAX)
560                         {
561                                 exfat_error("too long label (%hhu chars)", label->length);
562                                 goto error;
563                         }
564                         if (utf16_to_utf8(ef->label, label->name,
565                                                 sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
566                                 goto error;
567                         break;
568
569                 default:
570                         if (!(entry->type & EXFAT_ENTRY_VALID))
571                                 break; /* deleted entry, ignore it */
572                         if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
573                         {
574                                 exfat_error("unknown entry type %#hhx", entry->type);
575                                 goto error;
576                         }
577                         /* optional entry, warn and skip */
578                         exfat_warn("unknown entry type %#hhx", entry->type);
579                         if (continuations == 0)
580                         {
581                                 exfat_error("unexpected continuation");
582                                 goto error;
583                         }
584                         --continuations;
585                         break;
586                 }
587
588                 if (!fetch_next_entry(ef, parent, it))
589                         goto error;
590         }
591         /* we never reach here */
592
593 error:
594         free(*node);
595         *node = NULL;
596         return rc;
597 }
598
599 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
600 {
601         struct iterator it;
602         int rc;
603         struct exfat_node* node;
604         struct exfat_node* current = NULL;
605
606         if (dir->is_cached)
607                 return 0; /* already cached */
608
609         rc = opendir(ef, dir, &it);
610         if (rc != 0)
611                 return rc;
612         while ((rc = readdir(ef, dir, &node, &it)) == 0)
613         {
614                 node->parent = dir;
615                 if (current != NULL)
616                 {
617                         current->next = node;
618                         node->prev = current;
619                 }
620                 else
621                         dir->child = node;
622
623                 current = node;
624         }
625         closedir(&it);
626
627         if (rc != -ENOENT)
628         {
629                 /* rollback */
630                 for (current = dir->child; current; current = node)
631                 {
632                         node = current->next;
633                         free(current);
634                 }
635                 dir->child = NULL;
636                 return rc;
637         }
638
639         dir->is_cached = true;
640         return 0;
641 }
642
643 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
644 {
645         node->parent = dir;
646         if (dir->child)
647         {
648                 dir->child->prev = node;
649                 node->next = dir->child;
650         }
651         dir->child = node;
652 }
653
654 static void tree_detach(struct exfat_node* node)
655 {
656         if (node->prev)
657                 node->prev->next = node->next;
658         else /* this is the first node in the list */
659                 node->parent->child = node->next;
660         if (node->next)
661                 node->next->prev = node->prev;
662         node->parent = NULL;
663         node->prev = NULL;
664         node->next = NULL;
665 }
666
667 static void reset_cache(struct exfat* ef, struct exfat_node* node)
668 {
669         char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
670
671         while (node->child)
672         {
673                 struct exfat_node* p = node->child;
674                 reset_cache(ef, p);
675                 tree_detach(p);
676                 free(p);
677         }
678         node->is_cached = false;
679         if (node->references != 0)
680         {
681                 exfat_get_name(node, buffer);
682                 exfat_warn("non-zero reference counter (%d) for '%s'",
683                                 node->references, buffer);
684         }
685         if (node != ef->root && node->is_dirty)
686         {
687                 exfat_get_name(node, buffer);
688                 exfat_bug("node '%s' is dirty", buffer);
689         }
690         while (node->references)
691                 exfat_put_node(ef, node);
692 }
693
694 void exfat_reset_cache(struct exfat* ef)
695 {
696         reset_cache(ef, ef->root);
697 }
698
699 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
700                 cluster_t* cluster, off_t* offset)
701 {
702         *offset += sizeof(struct exfat_entry);
703         if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
704         {
705                 *cluster = exfat_next_cluster(ef, parent, *cluster);
706                 if (CLUSTER_INVALID(*cluster))
707                 {
708                         exfat_error("invalid cluster %#x while getting next entry",
709                                         *cluster);
710                         return false;
711                 }
712         }
713         return true;
714 }
715
716 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
717 {
718         cluster_t cluster;
719         off_t offset;
720         off_t meta1_offset, meta2_offset;
721         struct exfat_entry_meta1 meta1;
722         struct exfat_entry_meta2 meta2;
723
724         if (!node->is_dirty)
725                 return 0; /* no need to flush */
726
727         if (ef->ro)
728                 exfat_bug("unable to flush node to read-only FS");
729
730         if (node->parent == NULL)
731                 return 0; /* do not flush unlinked node */
732
733         cluster = node->entry_cluster;
734         offset = node->entry_offset;
735         meta1_offset = co2o(ef, cluster, offset);
736         if (!next_entry(ef, node->parent, &cluster, &offset))
737                 return -EIO;
738         meta2_offset = co2o(ef, cluster, offset);
739
740         if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
741         {
742                 exfat_error("failed to read meta1 entry on flush");
743                 return -EIO;
744         }
745         if (meta1.type != EXFAT_ENTRY_FILE)
746                 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
747         meta1.attrib = cpu_to_le16(node->attrib);
748         exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
749         exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
750
751         if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
752         {
753                 exfat_error("failed to read meta2 entry on flush");
754                 return -EIO;
755         }
756         if (meta2.type != EXFAT_ENTRY_FILE_INFO)
757                 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
758         meta2.size = meta2.valid_size = cpu_to_le64(node->size);
759         meta2.start_cluster = cpu_to_le32(node->start_cluster);
760         meta2.flags = EXFAT_FLAG_ALWAYS1;
761         /* empty files must not be marked as contiguous */
762         if (node->size != 0 && node->is_contiguous)
763                 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
764         /* name hash remains unchanged, no need to recalculate it */
765
766         meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
767
768         if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
769         {
770                 exfat_error("failed to write meta1 entry on flush");
771                 return -EIO;
772         }
773         if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
774         {
775                 exfat_error("failed to write meta2 entry on flush");
776                 return -EIO;
777         }
778
779         node->is_dirty = false;
780         return exfat_flush(ef);
781 }
782
783 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
784 {
785         cluster_t cluster = node->entry_cluster;
786         off_t offset = node->entry_offset;
787         int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
788         uint8_t entry_type;
789
790         entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
791         if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
792         {
793                 exfat_error("failed to erase meta1 entry");
794                 return false;
795         }
796
797         if (!next_entry(ef, node->parent, &cluster, &offset))
798                 return false;
799         entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
800         if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
801         {
802                 exfat_error("failed to erase meta2 entry");
803                 return false;
804         }
805
806         while (name_entries--)
807         {
808                 if (!next_entry(ef, node->parent, &cluster, &offset))
809                         return false;
810                 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
811                 if (exfat_pwrite(ef->dev, &entry_type, 1,
812                                 co2o(ef, cluster, offset)) < 0)
813                 {
814                         exfat_error("failed to erase name entry");
815                         return false;
816                 }
817         }
818         return true;
819 }
820
821 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
822                 off_t deleted_offset)
823 {
824         const struct exfat_node* node;
825         const struct exfat_node* last_node;
826         uint64_t entries = 0;
827         uint64_t new_size;
828
829         if (!(dir->attrib & EXFAT_ATTRIB_DIR))
830                 exfat_bug("attempted to shrink a file");
831         if (!dir->is_cached)
832                 exfat_bug("attempted to shrink uncached directory");
833
834         for (last_node = node = dir->child; node; node = node->next)
835         {
836                 if (deleted_offset < node->entry_offset)
837                 {
838                         /* there are other entries after the removed one, no way to shrink
839                            this directory */
840                         return 0;
841                 }
842                 if (last_node->entry_offset < node->entry_offset)
843                         last_node = node;
844         }
845
846         if (last_node)
847         {
848                 /* offset of the last entry */
849                 entries += last_node->entry_offset / sizeof(struct exfat_entry);
850                 /* two subentries with meta info */
851                 entries += 2;
852                 /* subentries with file name */
853                 entries += DIV_ROUND_UP(utf16_length(last_node->name),
854                                 EXFAT_ENAME_MAX);
855         }
856
857         new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
858                                  CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
859         if (new_size == 0) /* directory always has at least 1 cluster */
860                 new_size = CLUSTER_SIZE(*ef->sb);
861         if (new_size == dir->size)
862                 return 0;
863         return exfat_truncate(ef, dir, new_size, true);
864 }
865
866 static int delete(struct exfat* ef, struct exfat_node* node)
867 {
868         struct exfat_node* parent = node->parent;
869         off_t deleted_offset = node->entry_offset;
870         int rc;
871
872         exfat_get_node(parent);
873         if (!erase_entry(ef, node))
874         {
875                 exfat_put_node(ef, parent);
876                 return -EIO;
877         }
878         exfat_update_mtime(parent);
879         tree_detach(node);
880         rc = shrink_directory(ef, parent, deleted_offset);
881         node->is_unlinked = true;
882         if (rc != 0)
883         {
884                 exfat_flush_node(ef, parent);
885                 exfat_put_node(ef, parent);
886                 return rc;
887         }
888         rc = exfat_flush_node(ef, parent);
889         exfat_put_node(ef, parent);
890         return rc;
891 }
892
893 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
894 {
895         if (node->attrib & EXFAT_ATTRIB_DIR)
896                 return -EISDIR;
897         return delete(ef, node);
898 }
899
900 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
901 {
902         int rc;
903
904         if (!(node->attrib & EXFAT_ATTRIB_DIR))
905                 return -ENOTDIR;
906         /* check that directory is empty */
907         rc = exfat_cache_directory(ef, node);
908         if (rc != 0)
909                 return rc;
910         if (node->child)
911                 return -ENOTEMPTY;
912         return delete(ef, node);
913 }
914
915 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
916                 uint64_t asize, uint32_t difference)
917 {
918         return exfat_truncate(ef, dir,
919                         DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
920                                 * CLUSTER_SIZE(*ef->sb), true);
921 }
922
923 static int find_slot(struct exfat* ef, struct exfat_node* dir,
924                 cluster_t* cluster, off_t* offset, int subentries)
925 {
926         struct iterator it;
927         int rc;
928         const struct exfat_entry* entry;
929         int contiguous = 0;
930
931         rc = opendir(ef, dir, &it);
932         if (rc != 0)
933                 return rc;
934         for (;;)
935         {
936                 if (contiguous == 0)
937                 {
938                         *cluster = it.cluster;
939                         *offset = it.offset;
940                 }
941                 entry = get_entry_ptr(ef, &it);
942                 if (entry->type & EXFAT_ENTRY_VALID)
943                         contiguous = 0;
944                 else
945                         contiguous++;
946                 if (contiguous == subentries)
947                         break;  /* suitable slot is found */
948                 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
949                 {
950                         rc = grow_directory(ef, dir, dir->size,
951                                         (subentries - contiguous) * sizeof(struct exfat_entry));
952                         if (rc != 0)
953                         {
954                                 closedir(&it);
955                                 return rc;
956                         }
957                 }
958                 if (!fetch_next_entry(ef, dir, &it))
959                 {
960                         closedir(&it);
961                         return -EIO;
962                 }
963         }
964         closedir(&it);
965         return 0;
966 }
967
968 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
969                 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
970 {
971         struct exfat_node* node;
972         const size_t name_length = utf16_length(name);
973         const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
974         struct exfat_entry entries[2 + name_entries];
975         struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
976         struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
977         int i;
978         int rc;
979
980         memset(entries, 0, sizeof(struct exfat_entry[2]));
981
982         meta1->type = EXFAT_ENTRY_FILE;
983         meta1->continuations = 1 + name_entries;
984         meta1->attrib = cpu_to_le16(attrib);
985         exfat_unix2exfat(time(NULL), &meta1->crdate, &meta1->crtime,
986                         &meta1->crtime_cs);
987         meta1->adate = meta1->mdate = meta1->crdate;
988         meta1->atime = meta1->mtime = meta1->crtime;
989         meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
990
991         meta2->type = EXFAT_ENTRY_FILE_INFO;
992         meta2->flags = EXFAT_FLAG_ALWAYS1;
993         meta2->name_length = name_length;
994         meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
995         meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
996
997         meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
998
999         for (i = 0; i < name_entries; i++)
1000         {
1001                 struct exfat_entry_name* name_entry;
1002
1003                 name_entry = (struct exfat_entry_name*) &entries[2 + i];
1004                 name_entry->type = EXFAT_ENTRY_FILE_NAME;
1005                 name_entry->__unknown = 0;
1006                 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
1007                                 EXFAT_ENAME_MAX * sizeof(le16_t));
1008         }
1009
1010         rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
1011         if (rc != 0)
1012                 return rc;
1013
1014         node = allocate_node();
1015         if (node == NULL)
1016                 return -ENOMEM;
1017         node->entry_cluster = cluster;
1018         node->entry_offset = offset;
1019         memcpy(node->name, name, name_length * sizeof(le16_t));
1020         init_node_meta1(node, meta1);
1021         init_node_meta2(node, meta2);
1022
1023         tree_attach(dir, node);
1024         return 0;
1025 }
1026
1027 static int create(struct exfat* ef, const char* path, uint16_t attrib)
1028 {
1029         struct exfat_node* dir;
1030         struct exfat_node* existing;
1031         cluster_t cluster = EXFAT_CLUSTER_BAD;
1032         off_t offset = -1;
1033         le16_t name[EXFAT_NAME_MAX + 1];
1034         int rc;
1035
1036         rc = exfat_split(ef, &dir, &existing, name, path);
1037         if (rc != 0)
1038                 return rc;
1039         if (existing != NULL)
1040         {
1041                 exfat_put_node(ef, existing);
1042                 exfat_put_node(ef, dir);
1043                 return -EEXIST;
1044         }
1045
1046         rc = find_slot(ef, dir, &cluster, &offset,
1047                         2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1048         if (rc != 0)
1049         {
1050                 exfat_put_node(ef, dir);
1051                 return rc;
1052         }
1053         rc = commit_entry(ef, dir, name, cluster, offset, attrib);
1054         if (rc != 0)
1055         {
1056                 exfat_put_node(ef, dir);
1057                 return rc;
1058         }
1059         exfat_update_mtime(dir);
1060         rc = exfat_flush_node(ef, dir);
1061         exfat_put_node(ef, dir);
1062         return rc;
1063 }
1064
1065 int exfat_mknod(struct exfat* ef, const char* path)
1066 {
1067         return create(ef, path, EXFAT_ATTRIB_ARCH);
1068 }
1069
1070 int exfat_mkdir(struct exfat* ef, const char* path)
1071 {
1072         int rc;
1073         struct exfat_node* node;
1074
1075         rc = create(ef, path, EXFAT_ATTRIB_DIR);
1076         if (rc != 0)
1077                 return rc;
1078         rc = exfat_lookup(ef, &node, path);
1079         if (rc != 0)
1080                 return 0;
1081         /* directories always have at least one cluster */
1082         rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1083         if (rc != 0)
1084         {
1085                 delete(ef, node);
1086                 exfat_put_node(ef, node);
1087                 return rc;
1088         }
1089         rc = exfat_flush_node(ef, node);
1090         if (rc != 0)
1091         {
1092                 delete(ef, node);
1093                 exfat_put_node(ef, node);
1094                 return rc;
1095         }
1096         exfat_put_node(ef, node);
1097         return 0;
1098 }
1099
1100 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1101                 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1102                 off_t new_offset)
1103 {
1104         struct exfat_entry_meta1 meta1;
1105         struct exfat_entry_meta2 meta2;
1106         cluster_t old_cluster = node->entry_cluster;
1107         off_t old_offset = node->entry_offset;
1108         const size_t name_length = utf16_length(name);
1109         const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1110         int i;
1111
1112         if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1113                         co2o(ef, old_cluster, old_offset)) < 0)
1114         {
1115                 exfat_error("failed to read meta1 entry on rename");
1116                 return -EIO;
1117         }
1118         if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1119                 return -EIO;
1120         if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1121                         co2o(ef, old_cluster, old_offset)) < 0)
1122         {
1123                 exfat_error("failed to read meta2 entry on rename");
1124                 return -EIO;
1125         }
1126         meta1.continuations = 1 + name_entries;
1127         meta2.name_hash = exfat_calc_name_hash(ef, name, name_length);
1128         meta2.name_length = name_length;
1129         meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1130
1131         if (!erase_entry(ef, node))
1132                 return -EIO;
1133
1134         node->entry_cluster = new_cluster;
1135         node->entry_offset = new_offset;
1136
1137         if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1138                         co2o(ef, new_cluster, new_offset)) < 0)
1139         {
1140                 exfat_error("failed to write meta1 entry on rename");
1141                 return -EIO;
1142         }
1143         if (!next_entry(ef, dir, &new_cluster, &new_offset))
1144                 return -EIO;
1145         if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1146                         co2o(ef, new_cluster, new_offset)) < 0)
1147         {
1148                 exfat_error("failed to write meta2 entry on rename");
1149                 return -EIO;
1150         }
1151
1152         for (i = 0; i < name_entries; i++)
1153         {
1154                 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1155                 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1156                                 EXFAT_ENAME_MAX * sizeof(le16_t));
1157                 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1158                         return -EIO;
1159                 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1160                                 co2o(ef, new_cluster, new_offset)) < 0)
1161                 {
1162                         exfat_error("failed to write name entry on rename");
1163                         return -EIO;
1164                 }
1165         }
1166
1167         memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1168         tree_detach(node);
1169         tree_attach(dir, node);
1170         return 0;
1171 }
1172
1173 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1174 {
1175         struct exfat_node* node;
1176         struct exfat_node* existing;
1177         struct exfat_node* dir;
1178         cluster_t cluster = EXFAT_CLUSTER_BAD;
1179         off_t offset = -1;
1180         le16_t name[EXFAT_NAME_MAX + 1];
1181         int rc;
1182
1183         rc = exfat_lookup(ef, &node, old_path);
1184         if (rc != 0)
1185                 return rc;
1186
1187         rc = exfat_split(ef, &dir, &existing, name, new_path);
1188         if (rc != 0)
1189         {
1190                 exfat_put_node(ef, node);
1191                 return rc;
1192         }
1193
1194         /* check that target is not a subdirectory of the source */
1195         if (node->attrib & EXFAT_ATTRIB_DIR)
1196         {
1197                 struct exfat_node* p;
1198
1199                 for (p = dir; p; p = p->parent)
1200                         if (node == p)
1201                         {
1202                                 if (existing != NULL)
1203                                         exfat_put_node(ef, existing);
1204                                 exfat_put_node(ef, dir);
1205                                 exfat_put_node(ef, node);
1206                                 return -EINVAL;
1207                         }
1208         }
1209
1210         if (existing != NULL)
1211         {
1212                 /* remove target if it's not the same node as source */
1213                 if (existing != node)
1214                 {
1215                         if (existing->attrib & EXFAT_ATTRIB_DIR)
1216                         {
1217                                 if (node->attrib & EXFAT_ATTRIB_DIR)
1218                                         rc = exfat_rmdir(ef, existing);
1219                                 else
1220                                         rc = -ENOTDIR;
1221                         }
1222                         else
1223                         {
1224                                 if (!(node->attrib & EXFAT_ATTRIB_DIR))
1225                                         rc = exfat_unlink(ef, existing);
1226                                 else
1227                                         rc = -EISDIR;
1228                         }
1229                         exfat_put_node(ef, existing);
1230                         if (rc != 0)
1231                         {
1232                                 /* free clusters even if something went wrong; overwise they
1233                                    will be just lost */
1234                                 exfat_cleanup_node(ef, existing);
1235                                 exfat_put_node(ef, dir);
1236                                 exfat_put_node(ef, node);
1237                                 return rc;
1238                         }
1239                         rc = exfat_cleanup_node(ef, existing);
1240                         if (rc != 0)
1241                         {
1242                                 exfat_put_node(ef, dir);
1243                                 exfat_put_node(ef, node);
1244                                 return rc;
1245                         }
1246                 }
1247                 else
1248                         exfat_put_node(ef, existing);
1249         }
1250
1251         rc = find_slot(ef, dir, &cluster, &offset,
1252                         2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1253         if (rc != 0)
1254         {
1255                 exfat_put_node(ef, dir);
1256                 exfat_put_node(ef, node);
1257                 return rc;
1258         }
1259         rc = rename_entry(ef, dir, node, name, cluster, offset);
1260         exfat_put_node(ef, dir);
1261         exfat_put_node(ef, node);
1262         return rc;
1263 }
1264
1265 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1266 {
1267         node->atime = tv[0].tv_sec;
1268         node->mtime = tv[1].tv_sec;
1269         node->is_dirty = true;
1270 }
1271
1272 void exfat_update_atime(struct exfat_node* node)
1273 {
1274         node->atime = time(NULL);
1275         node->is_dirty = true;
1276 }
1277
1278 void exfat_update_mtime(struct exfat_node* node)
1279 {
1280         node->mtime = time(NULL);
1281         node->is_dirty = true;
1282 }
1283
1284 const char* exfat_get_label(struct exfat* ef)
1285 {
1286         return ef->label;
1287 }
1288
1289 static int find_label(struct exfat* ef, off_t* offset)
1290 {
1291         struct exfat_entry entry;
1292         int rc;
1293
1294         for (*offset = 0; ; *offset += sizeof(entry))
1295         {
1296                 rc = read_entries(ef, ef->root, &entry, 1, *offset);
1297                 if (rc != 0)
1298                         return rc;
1299
1300                 if (entry.type == EXFAT_ENTRY_LABEL)
1301                         return 0;
1302         }
1303 }
1304
1305 int exfat_set_label(struct exfat* ef, const char* label)
1306 {
1307         le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1308         int rc;
1309         cluster_t cluster;
1310         off_t offset;
1311         struct exfat_entry_label entry;
1312
1313         memset(label_utf16, 0, sizeof(label_utf16));
1314         rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
1315         if (rc != 0)
1316                 return rc;
1317
1318         rc = find_label(ef, &offset);
1319         if (rc == -ENOENT)
1320                 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1321         if (rc != 0)
1322                 return rc;
1323
1324         entry.type = EXFAT_ENTRY_LABEL;
1325         entry.length = utf16_length(label_utf16);
1326         memcpy(entry.name, label_utf16, sizeof(entry.name));
1327         if (entry.length == 0)
1328                 entry.type ^= EXFAT_ENTRY_VALID;
1329
1330         rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
1331         if (rc != 0)
1332                 return rc;
1333
1334         strcpy(ef->label, label);
1335         return 0;
1336 }