OSDN Git Service

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