OSDN Git Service

a0809da5a96c79138ab8283d187e133070baa35c
[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 check_slot(struct exfat* ef, struct exfat_node* dir, off_t offset,
873                 int n)
874 {
875         struct exfat_entry entries[n];
876         int rc;
877         size_t i;
878
879         /* Root directory contains entries, that don't have any nodes associated
880            with them (clusters bitmap, upper case table, label). We need to be
881            careful not to overwrite them. */
882         if (dir != ef->root)
883                 return 0;
884
885         rc = read_entries(ef, dir, entries, n, offset);
886         if (rc != 0)
887                 return rc;
888         for (i = 0; i < n; i++)
889                 if (entries[i].type & EXFAT_ENTRY_VALID)
890                         return -EINVAL;
891         return 0;
892 }
893
894 static int find_slot(struct exfat* ef, struct exfat_node* dir,
895                 off_t* offset, int n)
896 {
897         bitmap_t* dmap;
898         struct exfat_node* p;
899         size_t i;
900         int contiguous = 0;
901
902         if (!dir->is_cached)
903                 exfat_bug("directory is not cached");
904
905         /* build a bitmap of valid entries in the directory */
906         dmap = calloc(BMAP_SIZE(dir->size / sizeof(struct exfat_entry)),
907                         sizeof(bitmap_t));
908         if (dmap == NULL)
909         {
910                 exfat_error("failed to allocate directory bitmap (%"PRIu64")",
911                                 dir->size / sizeof(struct exfat_entry));
912                 return -ENOMEM;
913         }
914         for (p = dir->child; p != NULL; p = p->next)
915                 for (i = 0; i < 1 + p->continuations; i++)
916                         BMAP_SET(dmap, p->entry_offset / sizeof(struct exfat_entry) + i);
917
918         /* find a slot in the directory entries bitmap */
919         for (i = 0; i < dir->size / sizeof(struct exfat_entry); i++)
920         {
921                 if (BMAP_GET(dmap, i) == 0)
922                 {
923                         if (contiguous++ == 0)
924                                 *offset = (off_t) i * sizeof(struct exfat_entry);
925                         if (contiguous == n)
926                                 /* suitable slot is found, check that it's not occupied */
927                                 switch (check_slot(ef, dir, *offset, n))
928                                 {
929                                 case 0:
930                                         free(dmap);
931                                         return 0;
932                                 case -EIO:
933                                         free(dmap);
934                                         return -EIO;
935                                 case -EINVAL:
936                                         /* slot is occupied, continue searching */
937                                         contiguous = 0;
938                                         break;
939                                 }
940                 }
941                 else
942                         contiguous = 0;
943         }
944         free(dmap);
945
946         /* no suitable slots found, extend the directory */
947         if (contiguous == 0)
948                 *offset = dir->size;
949         return exfat_truncate(ef, dir,
950                         ROUND_UP(dir->size + sizeof(struct exfat_entry[n - contiguous]),
951                                         CLUSTER_SIZE(*ef->sb)),
952                         true);
953 }
954
955 static int commit_entry(struct exfat* ef, struct exfat_node* dir,
956                 const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
957 {
958         struct exfat_node* node;
959         const size_t name_length = utf16_length(name);
960         const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
961         struct exfat_entry entries[2 + name_entries];
962         struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
963         struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
964         int i;
965         int rc;
966
967         memset(entries, 0, sizeof(struct exfat_entry[2]));
968
969         meta1->type = EXFAT_ENTRY_FILE;
970         meta1->continuations = 1 + name_entries;
971         meta1->attrib = cpu_to_le16(attrib);
972         exfat_unix2exfat(time(NULL), &meta1->crdate, &meta1->crtime,
973                         &meta1->crtime_cs);
974         meta1->adate = meta1->mdate = meta1->crdate;
975         meta1->atime = meta1->mtime = meta1->crtime;
976         meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
977
978         meta2->type = EXFAT_ENTRY_FILE_INFO;
979         meta2->flags = EXFAT_FLAG_ALWAYS1;
980         meta2->name_length = name_length;
981         meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
982         meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
983
984         meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
985
986         for (i = 0; i < name_entries; i++)
987         {
988                 struct exfat_entry_name* name_entry;
989
990                 name_entry = (struct exfat_entry_name*) &entries[2 + i];
991                 name_entry->type = EXFAT_ENTRY_FILE_NAME;
992                 name_entry->__unknown = 0;
993                 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
994                                 EXFAT_ENAME_MAX * sizeof(le16_t));
995         }
996
997         rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
998         if (rc != 0)
999                 return rc;
1000
1001         node = allocate_node();
1002         if (node == NULL)
1003                 return -ENOMEM;
1004         node->entry_cluster = cluster;
1005         node->entry_offset = offset;
1006         memcpy(node->name, name, name_length * sizeof(le16_t));
1007         init_node_meta1(node, meta1);
1008         init_node_meta2(node, meta2);
1009
1010         tree_attach(dir, node);
1011         return 0;
1012 }
1013
1014 static int create(struct exfat* ef, const char* path, uint16_t attrib)
1015 {
1016         struct exfat_node* dir;
1017         struct exfat_node* existing;
1018         cluster_t cluster = EXFAT_CLUSTER_BAD;
1019         off_t offset = -1;
1020         le16_t name[EXFAT_NAME_MAX + 1];
1021         int rc;
1022
1023         rc = exfat_split(ef, &dir, &existing, name, path);
1024         if (rc != 0)
1025                 return rc;
1026         if (existing != NULL)
1027         {
1028                 exfat_put_node(ef, existing);
1029                 exfat_put_node(ef, dir);
1030                 return -EEXIST;
1031         }
1032
1033         rc = find_slot(ef, dir, &offset,
1034                         2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1035         if (rc != 0)
1036         {
1037                 exfat_put_node(ef, dir);
1038                 return rc;
1039         }
1040         rc = commit_entry(ef, dir, name, cluster, offset, attrib);
1041         if (rc != 0)
1042         {
1043                 exfat_put_node(ef, dir);
1044                 return rc;
1045         }
1046         exfat_update_mtime(dir);
1047         rc = exfat_flush_node(ef, dir);
1048         exfat_put_node(ef, dir);
1049         return rc;
1050 }
1051
1052 int exfat_mknod(struct exfat* ef, const char* path)
1053 {
1054         return create(ef, path, EXFAT_ATTRIB_ARCH);
1055 }
1056
1057 int exfat_mkdir(struct exfat* ef, const char* path)
1058 {
1059         int rc;
1060         struct exfat_node* node;
1061
1062         rc = create(ef, path, EXFAT_ATTRIB_DIR);
1063         if (rc != 0)
1064                 return rc;
1065         rc = exfat_lookup(ef, &node, path);
1066         if (rc != 0)
1067                 return 0;
1068         /* directories always have at least one cluster */
1069         rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb), true);
1070         if (rc != 0)
1071         {
1072                 delete(ef, node);
1073                 exfat_put_node(ef, node);
1074                 return rc;
1075         }
1076         rc = exfat_flush_node(ef, node);
1077         if (rc != 0)
1078         {
1079                 delete(ef, node);
1080                 exfat_put_node(ef, node);
1081                 return rc;
1082         }
1083         exfat_put_node(ef, node);
1084         return 0;
1085 }
1086
1087 static int rename_entry(struct exfat* ef, struct exfat_node* dir,
1088                 struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
1089                 off_t new_offset)
1090 {
1091         const size_t name_length = utf16_length(name);
1092         const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
1093         struct exfat_entry entries[2 + name_entries];
1094         struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
1095         struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
1096         int rc;
1097         int i;
1098
1099         rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
1100         if (rc != 0)
1101                 return rc;
1102
1103         meta1->continuations = 1 + name_entries;
1104         meta2->name_length = name_length;
1105         meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
1106         meta1->checksum = exfat_calc_checksum(meta1, meta2, name);
1107
1108         rc = erase_node(ef, node);
1109         if (rc != 0)
1110                 return rc;
1111
1112         node->entry_cluster = new_cluster;
1113         node->entry_offset = new_offset;
1114         node->continuations = 1 + name_entries;
1115
1116         for (i = 0; i < name_entries; i++)
1117         {
1118                 struct exfat_entry_name* name_entry;
1119
1120                 name_entry = (struct exfat_entry_name*) &entries[2 + i];
1121                 name_entry->type = EXFAT_ENTRY_FILE_NAME;
1122                 name_entry->__unknown = 0;
1123                 memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
1124                                 EXFAT_ENAME_MAX * sizeof(le16_t));
1125         }
1126
1127         rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
1128         if (rc != 0)
1129                 return rc;
1130
1131         memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
1132         tree_detach(node);
1133         tree_attach(dir, node);
1134         return 0;
1135 }
1136
1137 int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
1138 {
1139         struct exfat_node* node;
1140         struct exfat_node* existing;
1141         struct exfat_node* dir;
1142         cluster_t cluster = EXFAT_CLUSTER_BAD;
1143         off_t offset = -1;
1144         le16_t name[EXFAT_NAME_MAX + 1];
1145         int rc;
1146
1147         rc = exfat_lookup(ef, &node, old_path);
1148         if (rc != 0)
1149                 return rc;
1150
1151         rc = exfat_split(ef, &dir, &existing, name, new_path);
1152         if (rc != 0)
1153         {
1154                 exfat_put_node(ef, node);
1155                 return rc;
1156         }
1157
1158         /* check that target is not a subdirectory of the source */
1159         if (node->attrib & EXFAT_ATTRIB_DIR)
1160         {
1161                 struct exfat_node* p;
1162
1163                 for (p = dir; p; p = p->parent)
1164                         if (node == p)
1165                         {
1166                                 if (existing != NULL)
1167                                         exfat_put_node(ef, existing);
1168                                 exfat_put_node(ef, dir);
1169                                 exfat_put_node(ef, node);
1170                                 return -EINVAL;
1171                         }
1172         }
1173
1174         if (existing != NULL)
1175         {
1176                 /* remove target if it's not the same node as source */
1177                 if (existing != node)
1178                 {
1179                         if (existing->attrib & EXFAT_ATTRIB_DIR)
1180                         {
1181                                 if (node->attrib & EXFAT_ATTRIB_DIR)
1182                                         rc = exfat_rmdir(ef, existing);
1183                                 else
1184                                         rc = -ENOTDIR;
1185                         }
1186                         else
1187                         {
1188                                 if (!(node->attrib & EXFAT_ATTRIB_DIR))
1189                                         rc = exfat_unlink(ef, existing);
1190                                 else
1191                                         rc = -EISDIR;
1192                         }
1193                         exfat_put_node(ef, existing);
1194                         if (rc != 0)
1195                         {
1196                                 /* free clusters even if something went wrong; overwise they
1197                                    will be just lost */
1198                                 exfat_cleanup_node(ef, existing);
1199                                 exfat_put_node(ef, dir);
1200                                 exfat_put_node(ef, node);
1201                                 return rc;
1202                         }
1203                         rc = exfat_cleanup_node(ef, existing);
1204                         if (rc != 0)
1205                         {
1206                                 exfat_put_node(ef, dir);
1207                                 exfat_put_node(ef, node);
1208                                 return rc;
1209                         }
1210                 }
1211                 else
1212                         exfat_put_node(ef, existing);
1213         }
1214
1215         rc = find_slot(ef, dir, &offset,
1216                         2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
1217         if (rc != 0)
1218         {
1219                 exfat_put_node(ef, dir);
1220                 exfat_put_node(ef, node);
1221                 return rc;
1222         }
1223         rc = rename_entry(ef, dir, node, name, cluster, offset);
1224         if (rc != 0)
1225         {
1226                 exfat_put_node(ef, dir);
1227                 exfat_put_node(ef, node);
1228                 return rc;
1229         }
1230         rc = exfat_flush_node(ef, dir);
1231         exfat_put_node(ef, dir);
1232         exfat_put_node(ef, node);
1233         /* node itself is not marked as dirty, no need to flush it */
1234         return rc;
1235 }
1236
1237 void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
1238 {
1239         node->atime = tv[0].tv_sec;
1240         node->mtime = tv[1].tv_sec;
1241         node->is_dirty = true;
1242 }
1243
1244 void exfat_update_atime(struct exfat_node* node)
1245 {
1246         node->atime = time(NULL);
1247         node->is_dirty = true;
1248 }
1249
1250 void exfat_update_mtime(struct exfat_node* node)
1251 {
1252         node->mtime = time(NULL);
1253         node->is_dirty = true;
1254 }
1255
1256 const char* exfat_get_label(struct exfat* ef)
1257 {
1258         return ef->label;
1259 }
1260
1261 static int find_label(struct exfat* ef, off_t* offset)
1262 {
1263         struct exfat_entry entry;
1264         int rc;
1265
1266         for (*offset = 0; ; *offset += sizeof(entry))
1267         {
1268                 rc = read_entries(ef, ef->root, &entry, 1, *offset);
1269                 if (rc != 0)
1270                         return rc;
1271
1272                 if (entry.type == EXFAT_ENTRY_LABEL)
1273                         return 0;
1274         }
1275 }
1276
1277 int exfat_set_label(struct exfat* ef, const char* label)
1278 {
1279         le16_t label_utf16[EXFAT_ENAME_MAX + 1];
1280         int rc;
1281         off_t offset;
1282         struct exfat_entry_label entry;
1283
1284         memset(label_utf16, 0, sizeof(label_utf16));
1285         rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
1286         if (rc != 0)
1287                 return rc;
1288
1289         rc = find_label(ef, &offset);
1290         if (rc == -ENOENT)
1291                 rc = find_slot(ef, ef->root, &offset, 1);
1292         if (rc != 0)
1293                 return rc;
1294
1295         entry.type = EXFAT_ENTRY_LABEL;
1296         entry.length = utf16_length(label_utf16);
1297         memcpy(entry.name, label_utf16, sizeof(entry.name));
1298         if (entry.length == 0)
1299                 entry.type ^= EXFAT_ENTRY_VALID;
1300
1301         rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
1302         if (rc != 0)
1303                 return rc;
1304
1305         strcpy(ef->label, label);
1306         return 0;
1307 }