OSDN Git Service

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