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