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