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