OSDN Git Service

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