OSDN Git Service

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