OSDN Git Service

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