]> git.sven.stormbind.net Git - sven/fuse-exfat.git/blob - libexfat/node.c
merge release 1.2.1
[sven/fuse-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-2015  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                                 break; /* deleted entry, ignore it */
459                         if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
460                         {
461                                 exfat_error("unknown entry type %#hhx", entry->type);
462                                 goto error;
463                         }
464                         /* optional entry, warn and skip */
465                         exfat_warn("unknown entry type %#hhx", entry->type);
466                         if (continuations == 0)
467                         {
468                                 exfat_error("unexpected continuation");
469                                 goto error;
470                         }
471                         --continuations;
472                         break;
473                 }
474
475                 if (!fetch_next_entry(ef, parent, it))
476                         goto error;
477         }
478         /* we never reach here */
479
480 error:
481         free(*node);
482         *node = NULL;
483         return rc;
484 }
485
486 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
487 {
488         struct iterator it;
489         int rc;
490         struct exfat_node* node;
491         struct exfat_node* current = NULL;
492
493         if (dir->flags & EXFAT_ATTRIB_CACHED)
494                 return 0; /* already cached */
495
496         rc = opendir(ef, dir, &it);
497         if (rc != 0)
498                 return rc;
499         while ((rc = readdir(ef, dir, &node, &it)) == 0)
500         {
501                 node->parent = dir;
502                 if (current != NULL)
503                 {
504                         current->next = node;
505                         node->prev = current;
506                 }
507                 else
508                         dir->child = node;
509
510                 current = node;
511         }
512         closedir(&it);
513
514         if (rc != -ENOENT)
515         {
516                 /* rollback */
517                 for (current = dir->child; current; current = node)
518                 {
519                         node = current->next;
520                         free(current);
521                 }
522                 dir->child = NULL;
523                 return rc;
524         }
525
526         dir->flags |= EXFAT_ATTRIB_CACHED;
527         return 0;
528 }
529
530 static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
531 {
532         node->parent = dir;
533         if (dir->child)
534         {
535                 dir->child->prev = node;
536                 node->next = dir->child;
537         }
538         dir->child = node;
539 }
540
541 static void tree_detach(struct exfat_node* node)
542 {
543         if (node->prev)
544                 node->prev->next = node->next;
545         else /* this is the first node in the list */
546                 node->parent->child = node->next;
547         if (node->next)
548                 node->next->prev = node->prev;
549         node->parent = NULL;
550         node->prev = NULL;
551         node->next = NULL;
552 }
553
554 static void reset_cache(struct exfat* ef, struct exfat_node* node)
555 {
556         char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
557
558         while (node->child)
559         {
560                 struct exfat_node* p = node->child;
561                 reset_cache(ef, p);
562                 tree_detach(p);
563                 free(p);
564         }
565         node->flags &= ~EXFAT_ATTRIB_CACHED;
566         if (node->references != 0)
567         {
568                 exfat_get_name(node, buffer, sizeof(buffer) - 1);
569                 exfat_warn("non-zero reference counter (%d) for '%s'",
570                                 node->references, buffer);
571         }
572         if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
573         {
574                 exfat_get_name(node, buffer, sizeof(buffer) - 1);
575                 exfat_bug("node '%s' is dirty", buffer);
576         }
577         while (node->references)
578                 exfat_put_node(ef, node);
579 }
580
581 void exfat_reset_cache(struct exfat* ef)
582 {
583         reset_cache(ef, ef->root);
584 }
585
586 static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
587                 cluster_t* cluster, off_t* offset)
588 {
589         *offset += sizeof(struct exfat_entry);
590         if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
591         {
592                 *cluster = exfat_next_cluster(ef, parent, *cluster);
593                 if (CLUSTER_INVALID(*cluster))
594                 {
595                         exfat_error("invalid cluster %#x while getting next entry",
596                                         *cluster);
597                         return false;
598                 }
599         }
600         return true;
601 }
602
603 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
604 {
605         cluster_t cluster;
606         off_t offset;
607         off_t meta1_offset, meta2_offset;
608         struct exfat_entry_meta1 meta1;
609         struct exfat_entry_meta2 meta2;
610
611         if (!(node->flags & EXFAT_ATTRIB_DIRTY))
612                 return 0; /* no need to flush */
613
614         if (ef->ro)
615                 exfat_bug("unable to flush node to read-only FS");
616
617         if (node->parent == NULL)
618                 return 0; /* do not flush unlinked node */
619
620         cluster = node->entry_cluster;
621         offset = node->entry_offset;
622         meta1_offset = co2o(ef, cluster, offset);
623         if (!next_entry(ef, node->parent, &cluster, &offset))
624                 return -EIO;
625         meta2_offset = co2o(ef, cluster, offset);
626
627         if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
628         {
629                 exfat_error("failed to read meta1 entry on flush");
630                 return -EIO;
631         }
632         if (meta1.type != EXFAT_ENTRY_FILE)
633                 exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
634         meta1.attrib = cpu_to_le16(node->flags);
635         exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
636         exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
637
638         if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
639         {
640                 exfat_error("failed to read meta2 entry on flush");
641                 return -EIO;
642         }
643         if (meta2.type != EXFAT_ENTRY_FILE_INFO)
644                 exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
645         meta2.size = meta2.valid_size = cpu_to_le64(node->size);
646         meta2.start_cluster = cpu_to_le32(node->start_cluster);
647         meta2.flags = EXFAT_FLAG_ALWAYS1;
648         /* empty files must not be marked as contiguous */
649         if (node->size != 0 && IS_CONTIGUOUS(*node))
650                 meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
651         /* name hash remains unchanged, no need to recalculate it */
652
653         meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
654
655         if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
656         {
657                 exfat_error("failed to write meta1 entry on flush");
658                 return -EIO;
659         }
660         if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
661         {
662                 exfat_error("failed to write meta2 entry on flush");
663                 return -EIO;
664         }
665
666         node->flags &= ~EXFAT_ATTRIB_DIRTY;
667         return 0;
668 }
669
670 static bool erase_entry(struct exfat* ef, struct exfat_node* node)
671 {
672         cluster_t cluster = node->entry_cluster;
673         off_t offset = node->entry_offset;
674         int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
675         uint8_t entry_type;
676
677         entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
678         if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
679         {
680                 exfat_error("failed to erase meta1 entry");
681                 return false;
682         }
683
684         if (!next_entry(ef, node->parent, &cluster, &offset))
685                 return false;
686         entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
687         if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
688         {
689                 exfat_error("failed to erase meta2 entry");
690                 return false;
691         }
692
693         while (name_entries--)
694         {
695                 if (!next_entry(ef, node->parent, &cluster, &offset))
696                         return false;
697                 entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
698                 if (exfat_pwrite(ef->dev, &entry_type, 1,
699                                 co2o(ef, cluster, offset)) < 0)
700                 {
701                         exfat_error("failed to erase name entry");
702                         return false;
703                 }
704         }
705         return true;
706 }
707
708 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
709                 off_t deleted_offset)
710 {
711         const struct exfat_node* node;
712         const struct exfat_node* last_node;
713         uint64_t entries = 0;
714         uint64_t new_size;
715
716         if (!(dir->flags & EXFAT_ATTRIB_DIR))
717                 exfat_bug("attempted to shrink a file");
718         if (!(dir->flags & EXFAT_ATTRIB_CACHED))
719                 exfat_bug("attempted to shrink uncached directory");
720
721         for (last_node = node = dir->child; node; node = node->next)
722         {
723                 if (deleted_offset < node->entry_offset)
724                 {
725                         /* there are other entries after the removed one, no way to shrink
726                            this directory */
727                         return 0;
728                 }
729                 if (last_node->entry_offset < node->entry_offset)
730                         last_node = node;
731         }
732
733         if (last_node)
734         {
735                 /* offset of the last entry */
736                 entries += last_node->entry_offset / sizeof(struct exfat_entry);
737                 /* two subentries with meta info */
738                 entries += 2;
739                 /* subentries with file name */
740                 entries += DIV_ROUND_UP(utf16_length(last_node->name),
741                                 EXFAT_ENAME_MAX);
742         }
743
744         new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry),
745                                  CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb);
746         if (new_size == 0) /* directory always has at least 1 cluster */
747                 new_size = CLUSTER_SIZE(*ef->sb);
748         if (new_size == dir->size)
749                 return 0;
750         return exfat_truncate(ef, dir, new_size, true);
751 }
752
753 static int delete(struct exfat* ef, struct exfat_node* node)
754 {
755         struct exfat_node* parent = node->parent;
756         off_t deleted_offset = node->entry_offset;
757         int rc;
758
759         exfat_get_node(parent);
760         if (!erase_entry(ef, node))
761         {
762                 exfat_put_node(ef, parent);
763                 return -EIO;
764         }
765         exfat_update_mtime(parent);
766         tree_detach(node);
767         rc = shrink_directory(ef, parent, deleted_offset);
768         node->flags |= EXFAT_ATTRIB_UNLINKED;
769         if (rc != 0)
770         {
771                 exfat_flush_node(ef, parent);
772                 exfat_put_node(ef, parent);
773                 return rc;
774         }
775         rc = exfat_flush_node(ef, parent);
776         exfat_put_node(ef, parent);
777         return rc;
778 }
779
780 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
781 {
782         if (node->flags & EXFAT_ATTRIB_DIR)
783                 return -EISDIR;
784         return delete(ef, node);
785 }
786
787 int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
788 {
789         int rc;
790
791         if (!(node->flags & EXFAT_ATTRIB_DIR))
792                 return -ENOTDIR;
793         /* check that directory is empty */
794         rc = exfat_cache_directory(ef, node);
795         if (rc != 0)
796                 return rc;
797         if (node->child)
798                 return -ENOTEMPTY;
799         return delete(ef, node);
800 }
801
802 static int grow_directory(struct exfat* ef, struct exfat_node* dir,
803                 uint64_t asize, uint32_t difference)
804 {
805         return exfat_truncate(ef, dir,
806                         DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
807                                 * CLUSTER_SIZE(*ef->sb), true);
808 }
809
810 static int find_slot(struct exfat* ef, struct exfat_node* dir,
811                 cluster_t* cluster, off_t* offset, int subentries)
812 {
813         struct iterator it;
814         int rc;
815         const struct exfat_entry* entry;
816         int contiguous = 0;
817
818         rc = opendir(ef, dir, &it);
819         if (rc != 0)
820                 return rc;
821         for (;;)
822         {
823                 if (contiguous == 0)
824                 {
825                         *cluster = it.cluster;
826                         *offset = it.offset;
827                 }
828                 entry = get_entry_ptr(ef, &it);
829                 if (entry->type & EXFAT_ENTRY_VALID)
830                         contiguous = 0;
831                 else
832                         contiguous++;
833                 if (contiguous == subentries)
834                         break;  /* suitable slot is found */
835                 if (it.offset + sizeof(struct exfat_entry) >= dir->size)
836                 {
837                         rc = grow_directory(ef, dir, dir->size,
838                                         (subentries - contiguous) * sizeof(struct exfat_entry));
839                         if (rc != 0)
840                         {
841                                 closedir(&it);
842                                 return rc;
843                         }
844                 }
845                 if (!fetch_next_entry(ef, dir, &it))
846                 {
847                         closedir(&it);
848                         return -EIO;
849                 }
850         }
851         closedir(&it);
852         return 0;
853 }
854
855 static int write_entry(struct exfat* ef, struct exfat_node* dir,
856                 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
857 {
858         struct exfat_node* node;
859         struct exfat_entry_meta1 meta1;
860         struct exfat_entry_meta2 meta2;
861         const size_t name_length = utf16_length(name);
862         const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
863         int i;
864
865         node = allocate_node();
866         if (node == NULL)
867                 return -ENOMEM;
868         node->entry_cluster = cluster;
869         node->entry_offset = offset;
870         memcpy(node->name, name, name_length * sizeof(le16_t));
871
872         memset(&meta1, 0, sizeof(meta1));
873         meta1.type = EXFAT_ENTRY_FILE;
874         meta1.continuations = 1 + name_entries;
875         meta1.attrib = cpu_to_le16(attrib);
876         exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
877                         &meta1.crtime_cs);
878         meta1.adate = meta1.mdate = meta1.crdate;
879         meta1.atime = meta1.mtime = meta1.crtime;
880         meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
881
882         memset(&meta2, 0, sizeof(meta2));
883         meta2.type = EXFAT_ENTRY_FILE_INFO;
884         meta2.flags = EXFAT_FLAG_ALWAYS1;
885         meta2.name_length = name_length;
886         meta2.name_hash = exfat_calc_name_hash(ef, node->name);
887         meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
888
889         meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
890
891         if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
892                         co2o(ef, cluster, offset)) < 0)
893         {
894                 exfat_error("failed to write meta1 entry");
895                 return -EIO;
896         }
897         if (!next_entry(ef, dir, &cluster, &offset))
898                 return -EIO;
899         if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
900                         co2o(ef, cluster, offset)) < 0)
901         {
902                 exfat_error("failed to write meta2 entry");
903                 return -EIO;
904         }
905         for (i = 0; i < name_entries; i++)
906         {
907                 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
908                 memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
909                                 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
910                                 sizeof(le16_t));
911                 if (!next_entry(ef, dir, &cluster, &offset))
912                         return -EIO;
913                 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
914                                 co2o(ef, cluster, offset)) < 0)
915                 {
916                         exfat_error("failed to write name entry");
917                         return -EIO;
918                 }
919         }
920
921         init_node_meta1(node, &meta1);
922         init_node_meta2(node, &meta2);
923
924         tree_attach(dir, node);
925         exfat_update_mtime(dir);
926         return 0;
927 }
928
929 static int create(struct exfat* ef, const char* path, uint16_t attrib)
930 {
931         struct exfat_node* dir;
932         struct exfat_node* existing;
933         cluster_t cluster = EXFAT_CLUSTER_BAD;
934         off_t offset = -1;
935         le16_t name[EXFAT_NAME_MAX + 1];
936         int rc;
937
938         rc = exfat_split(ef, &dir, &existing, name, path);
939         if (rc != 0)
940                 return rc;
941         if (existing != NULL)
942         {
943                 exfat_put_node(ef, existing);
944                 exfat_put_node(ef, dir);
945                 return -EEXIST;
946         }
947
948         rc = find_slot(ef, dir, &cluster, &offset,
949                         2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
950         if (rc != 0)
951         {
952                 exfat_put_node(ef, dir);
953                 return rc;
954         }
955         rc = write_entry(ef, dir, name, cluster, offset, attrib);
956         if (rc != 0)
957         {
958                 exfat_put_node(ef, dir);
959                 return rc;
960         }
961         rc = exfat_flush_node(ef, dir);
962         exfat_put_node(ef, dir);
963         return rc;
964 }
965
966 int exfat_mknod(struct exfat* ef, const char* path)
967 {
968         return create(ef, path, EXFAT_ATTRIB_ARCH);
969 }
970
971 int exfat_mkdir(struct exfat* ef, const char* path)
972 {
973         int rc;
974         struct exfat_node* node;
975
976         rc = create(ef, path, EXFAT_ATTRIB_DIR);
977         if (rc != 0)
978                 return rc;
979         rc = exfat_lookup(ef, &node, path);
980         if (rc != 0)
981                 return 0;
982         /* directories always have at least one cluster */
983         rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
984         if (rc != 0)
985         {
986                 delete(ef, node);
987                 exfat_put_node(ef, node);
988                 return rc;
989         }
990         rc = exfat_flush_node(ef, node);
991         if (rc != 0)
992         {
993                 delete(ef, node);
994                 exfat_put_node(ef, node);
995                 return rc;
996         }
997         exfat_put_node(ef, node);
998         return 0;
999 }
1000
1001 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1002                 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1003                 off_t new_offset)
1004 {
1005         struct exfat_entry_meta1 meta1;
1006         struct exfat_entry_meta2 meta2;
1007         cluster_t old_cluster = node->entry_cluster;
1008         off_t old_offset = node->entry_offset;
1009         const size_t name_length = utf16_length(name);
1010         const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1011         int i;
1012
1013         if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
1014                         co2o(ef, old_cluster, old_offset)) < 0)
1015         {
1016                 exfat_error("failed to read meta1 entry on rename");
1017                 return -EIO;
1018         }
1019         if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
1020                 return -EIO;
1021         if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
1022                         co2o(ef, old_cluster, old_offset)) < 0)
1023         {
1024                 exfat_error("failed to read meta2 entry on rename");
1025                 return -EIO;
1026         }
1027         meta1.continuations = 1 + name_entries;
1028         meta2.name_hash = exfat_calc_name_hash(ef, name);
1029         meta2.name_length = name_length;
1030         meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
1031
1032         if (!erase_entry(ef, node))
1033                 return -EIO;
1034
1035         node->entry_cluster = new_cluster;
1036         node->entry_offset = new_offset;
1037
1038         if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
1039                         co2o(ef, new_cluster, new_offset)) < 0)
1040         {
1041                 exfat_error("failed to write meta1 entry on rename");
1042                 return -EIO;
1043         }
1044         if (!next_entry(ef, dir, &new_cluster, &new_offset))
1045                 return -EIO;
1046         if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
1047                         co2o(ef, new_cluster, new_offset)) < 0)
1048         {
1049                 exfat_error("failed to write meta2 entry on rename");
1050                 return -EIO;
1051         }
1052
1053         for (i = 0; i < name_entries; i++)
1054         {
1055                 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
1056                 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
1057                                 EXFAT_ENAME_MAX * sizeof(le16_t));
1058                 if (!next_entry(ef, dir, &new_cluster, &new_offset))
1059                         return -EIO;
1060                 if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
1061                                 co2o(ef, new_cluster, new_offset)) < 0)
1062                 {
1063                         exfat_error("failed to write name entry on rename");
1064                         return -EIO;
1065                 }
1066         }
1067
1068         memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1069         tree_detach(node);
1070         tree_attach(dir, node);
1071         return 0;
1072 }
1073
1074 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1075 {
1076         struct exfat_node* node;
1077         struct exfat_node* existing;
1078         struct exfat_node* dir;
1079         cluster_t cluster = EXFAT_CLUSTER_BAD;
1080         off_t offset = -1;
1081         le16_t name[EXFAT_NAME_MAX + 1];
1082         int rc;
1083
1084         rc = exfat_lookup(ef, &node, old_path);
1085         if (rc != 0)
1086                 return rc;
1087
1088         rc = exfat_split(ef, &dir, &existing, name, new_path);
1089         if (rc != 0)
1090         {
1091                 exfat_put_node(ef, node);
1092                 return rc;
1093         }
1094
1095         /* check that target is not a subdirectory of the source */
1096         if (node->flags & EXFAT_ATTRIB_DIR)
1097         {
1098                 struct exfat_node* p;
1099
1100                 for (p = dir; p; p = p->parent)
1101                         if (node == p)
1102                         {
1103                                 if (existing != NULL)
1104                                         exfat_put_node(ef, existing);
1105                                 exfat_put_node(ef, dir);
1106                                 exfat_put_node(ef, node);
1107                                 return -EINVAL;
1108                         }
1109         }
1110
1111         if (existing != NULL)
1112         {
1113                 /* remove target if it's not the same node as source */
1114                 if (existing != node)
1115                 {
1116                         if (existing->flags & EXFAT_ATTRIB_DIR)
1117                         {
1118                                 if (node->flags & EXFAT_ATTRIB_DIR)
1119                                         rc = exfat_rmdir(ef, existing);
1120                                 else
1121                                         rc = -ENOTDIR;
1122                         }
1123                         else
1124                         {
1125                                 if (!(node->flags & EXFAT_ATTRIB_DIR))
1126                                         rc = exfat_unlink(ef, existing);
1127                                 else
1128                                         rc = -EISDIR;
1129                         }
1130                         exfat_put_node(ef, existing);
1131                         if (rc != 0)
1132                         {
1133                                 exfat_put_node(ef, dir);
1134                                 exfat_put_node(ef, node);
1135                                 return rc;
1136                         }
1137                 }
1138                 else
1139                         exfat_put_node(ef, existing);
1140         }
1141
1142         rc = find_slot(ef, dir, &cluster, &offset,
1143                         2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1144         if (rc != 0)
1145         {
1146                 exfat_put_node(ef, dir);
1147                 exfat_put_node(ef, node);
1148                 return rc;
1149         }
1150         rc = rename_entry(ef, dir, node, name, cluster, offset);
1151         exfat_put_node(ef, dir);
1152         exfat_put_node(ef, node);
1153         return rc;
1154 }
1155
1156 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1157 {
1158         node->atime = tv[0].tv_sec;
1159         node->mtime = tv[1].tv_sec;
1160         node->flags |= EXFAT_ATTRIB_DIRTY;
1161 }
1162
1163 void exfat_update_atime(struct exfat_node* node)
1164 {
1165         node->atime = time(NULL);
1166         node->flags |= EXFAT_ATTRIB_DIRTY;
1167 }
1168
1169 void exfat_update_mtime(struct exfat_node* node)
1170 {
1171         node->mtime = time(NULL);
1172         node->flags |= EXFAT_ATTRIB_DIRTY;
1173 }
1174
1175 const char* exfat_get_label(struct exfat* ef)
1176 {
1177         return ef->label;
1178 }
1179
1180 static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
1181 {
1182         struct iterator it;
1183         int rc;
1184
1185         rc = opendir(ef, ef->root, &it);
1186         if (rc != 0)
1187                 return rc;
1188
1189         for (;;)
1190         {
1191                 if (it.offset >= ef->root->size)
1192                 {
1193                         closedir(&it);
1194                         return -ENOENT;
1195                 }
1196
1197                 if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
1198                 {
1199                         *cluster = it.cluster;
1200                         *offset = it.offset;
1201                         closedir(&it);
1202                         return 0;
1203                 }
1204
1205                 if (!fetch_next_entry(ef, ef->root, &it))
1206                 {
1207                         closedir(&it);
1208                         return -EIO;
1209                 }
1210         }
1211 }
1212
1213 int exfat_set_label(struct exfat* ef, const char* label)
1214 {
1215         le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1216         int rc;
1217         cluster_t cluster;
1218         off_t offset;
1219         struct exfat_entry_label entry;
1220
1221         memset(label_utf16, 0, sizeof(label_utf16));
1222         rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
1223         if (rc != 0)
1224                 return rc;
1225
1226         rc = find_label(ef, &cluster, &offset);
1227         if (rc == -ENOENT)
1228                 rc = find_slot(ef, ef->root, &cluster, &offset, 1);
1229         if (rc != 0)
1230                 return rc;
1231
1232         entry.type = EXFAT_ENTRY_LABEL;
1233         entry.length = utf16_length(label_utf16);
1234         memcpy(entry.name, label_utf16, sizeof(entry.name));
1235         if (entry.length == 0)
1236                 entry.type ^= EXFAT_ENTRY_VALID;
1237
1238         if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
1239                         co2o(ef, cluster, offset)) < 0)
1240         {
1241                 exfat_error("failed to write label entry");
1242                 return -EIO;
1243         }
1244         strcpy(ef->label, label);
1245         return 0;
1246 }