OSDN Git Service

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