xref: /openbmc/qemu/block/vvfat.c (revision 8297be80)
1 /* vim:set shiftwidth=4 ts=4: */
2 /*
3  * QEMU Block driver for virtual VFAT (shadows a local directory)
4  *
5  * Copyright (c) 2004,2005 Johannes E. Schindelin
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 #include "qemu/osdep.h"
26 #include <dirent.h>
27 #include "qapi/error.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
30 #include "qemu/bswap.h"
31 #include "migration/blocker.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/cutils.h"
35 #include "qemu/error-report.h"
36 
37 #ifndef S_IWGRP
38 #define S_IWGRP 0
39 #endif
40 #ifndef S_IWOTH
41 #define S_IWOTH 0
42 #endif
43 
44 /* TODO: add ":bootsector=blabla.img:" */
45 /* LATER TODO: add automatic boot sector generation from
46     BOOTEASY.ASM and Ranish Partition Manager
47     Note that DOS assumes the system files to be the first files in the
48     file system (test if the boot sector still relies on that fact)! */
49 /* MAYBE TODO: write block-visofs.c */
50 /* TODO: call try_commit() only after a timeout */
51 
52 /* #define DEBUG */
53 
54 #ifdef DEBUG
55 
56 #define DLOG(a) a
57 
58 static void checkpoint(void);
59 
60 #ifdef __MINGW32__
61 void nonono(const char* file, int line, const char* msg) {
62     fprintf(stderr, "Nonono! %s:%d %s\n", file, line, msg);
63     exit(-5);
64 }
65 #undef assert
66 #define assert(a) do {if (!(a)) nonono(__FILE__, __LINE__, #a);}while(0)
67 #endif
68 
69 #else
70 
71 #define DLOG(a)
72 
73 #endif
74 
75 /* bootsector OEM name. see related compatibility problems at:
76  * https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
77  * http://seasip.info/Misc/oemid.html
78  */
79 #define BOOTSECTOR_OEM_NAME "MSWIN4.1"
80 
81 #define DIR_DELETED 0xe5
82 #define DIR_KANJI DIR_DELETED
83 #define DIR_KANJI_FAKE 0x05
84 #define DIR_FREE 0x00
85 
86 /* dynamic array functions */
87 typedef struct array_t {
88     char* pointer;
89     unsigned int size,next,item_size;
90 } array_t;
91 
92 static inline void array_init(array_t* array,unsigned int item_size)
93 {
94     array->pointer = NULL;
95     array->size=0;
96     array->next=0;
97     array->item_size=item_size;
98 }
99 
100 static inline void array_free(array_t* array)
101 {
102     g_free(array->pointer);
103     array->size=array->next=0;
104 }
105 
106 /* does not automatically grow */
107 static inline void* array_get(array_t* array,unsigned int index) {
108     assert(index < array->next);
109     return array->pointer + index * array->item_size;
110 }
111 
112 static inline int array_ensure_allocated(array_t* array, int index)
113 {
114     if((index + 1) * array->item_size > array->size) {
115         int new_size = (index + 32) * array->item_size;
116         array->pointer = g_realloc(array->pointer, new_size);
117         if (!array->pointer)
118             return -1;
119         memset(array->pointer + array->size, 0, new_size - array->size);
120         array->size = new_size;
121         array->next = index + 1;
122     }
123 
124     return 0;
125 }
126 
127 static inline void* array_get_next(array_t* array) {
128     unsigned int next = array->next;
129 
130     if (array_ensure_allocated(array, next) < 0)
131         return NULL;
132 
133     array->next = next + 1;
134     return array_get(array, next);
135 }
136 
137 static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
138     if((array->next+count)*array->item_size>array->size) {
139         int increment=count*array->item_size;
140         array->pointer=g_realloc(array->pointer,array->size+increment);
141         if(!array->pointer)
142             return NULL;
143         array->size+=increment;
144     }
145     memmove(array->pointer+(index+count)*array->item_size,
146                 array->pointer+index*array->item_size,
147                 (array->next-index)*array->item_size);
148     array->next+=count;
149     return array->pointer+index*array->item_size;
150 }
151 
152 /* this performs a "roll", so that the element which was at index_from becomes
153  * index_to, but the order of all other elements is preserved. */
154 static inline int array_roll(array_t* array,int index_to,int index_from,int count)
155 {
156     char* buf;
157     char* from;
158     char* to;
159     int is;
160 
161     if(!array ||
162             index_to<0 || index_to>=array->next ||
163             index_from<0 || index_from>=array->next)
164         return -1;
165 
166     if(index_to==index_from)
167         return 0;
168 
169     is=array->item_size;
170     from=array->pointer+index_from*is;
171     to=array->pointer+index_to*is;
172     buf=g_malloc(is*count);
173     memcpy(buf,from,is*count);
174 
175     if(index_to<index_from)
176         memmove(to+is*count,to,from-to);
177     else
178         memmove(from,from+is*count,to-from);
179 
180     memcpy(to,buf,is*count);
181 
182     g_free(buf);
183 
184     return 0;
185 }
186 
187 static inline int array_remove_slice(array_t* array,int index, int count)
188 {
189     assert(index >=0);
190     assert(count > 0);
191     assert(index + count <= array->next);
192     if(array_roll(array,array->next-1,index,count))
193         return -1;
194     array->next -= count;
195     return 0;
196 }
197 
198 static int array_remove(array_t* array,int index)
199 {
200     return array_remove_slice(array, index, 1);
201 }
202 
203 /* return the index for a given member */
204 static int array_index(array_t* array, void* pointer)
205 {
206     size_t offset = (char*)pointer - array->pointer;
207     assert((offset % array->item_size) == 0);
208     assert(offset/array->item_size < array->next);
209     return offset/array->item_size;
210 }
211 
212 /* These structures are used to fake a disk and the VFAT filesystem.
213  * For this reason we need to use QEMU_PACKED. */
214 
215 typedef struct bootsector_t {
216     uint8_t jump[3];
217     uint8_t name[8];
218     uint16_t sector_size;
219     uint8_t sectors_per_cluster;
220     uint16_t reserved_sectors;
221     uint8_t number_of_fats;
222     uint16_t root_entries;
223     uint16_t total_sectors16;
224     uint8_t media_type;
225     uint16_t sectors_per_fat;
226     uint16_t sectors_per_track;
227     uint16_t number_of_heads;
228     uint32_t hidden_sectors;
229     uint32_t total_sectors;
230     union {
231         struct {
232             uint8_t drive_number;
233             uint8_t reserved1;
234             uint8_t signature;
235             uint32_t id;
236             uint8_t volume_label[11];
237             uint8_t fat_type[8];
238             uint8_t ignored[0x1c0];
239         } QEMU_PACKED fat16;
240         struct {
241             uint32_t sectors_per_fat;
242             uint16_t flags;
243             uint8_t major,minor;
244             uint32_t first_cluster_of_root_dir;
245             uint16_t info_sector;
246             uint16_t backup_boot_sector;
247             uint8_t reserved[12];
248             uint8_t drive_number;
249             uint8_t reserved1;
250             uint8_t signature;
251             uint32_t id;
252             uint8_t volume_label[11];
253             uint8_t fat_type[8];
254             uint8_t ignored[0x1a4];
255         } QEMU_PACKED fat32;
256     } u;
257     uint8_t magic[2];
258 } QEMU_PACKED bootsector_t;
259 
260 typedef struct {
261     uint8_t head;
262     uint8_t sector;
263     uint8_t cylinder;
264 } mbr_chs_t;
265 
266 typedef struct partition_t {
267     uint8_t attributes; /* 0x80 = bootable */
268     mbr_chs_t start_CHS;
269     uint8_t   fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
270     mbr_chs_t end_CHS;
271     uint32_t start_sector_long;
272     uint32_t length_sector_long;
273 } QEMU_PACKED partition_t;
274 
275 typedef struct mbr_t {
276     uint8_t ignored[0x1b8];
277     uint32_t nt_id;
278     uint8_t ignored2[2];
279     partition_t partition[4];
280     uint8_t magic[2];
281 } QEMU_PACKED mbr_t;
282 
283 typedef struct direntry_t {
284     uint8_t name[8 + 3];
285     uint8_t attributes;
286     uint8_t reserved[2];
287     uint16_t ctime;
288     uint16_t cdate;
289     uint16_t adate;
290     uint16_t begin_hi;
291     uint16_t mtime;
292     uint16_t mdate;
293     uint16_t begin;
294     uint32_t size;
295 } QEMU_PACKED direntry_t;
296 
297 /* this structure are used to transparently access the files */
298 
299 typedef struct mapping_t {
300     /* begin is the first cluster, end is the last+1 */
301     uint32_t begin,end;
302     /* as s->directory is growable, no pointer may be used here */
303     unsigned int dir_index;
304     /* the clusters of a file may be in any order; this points to the first */
305     int first_mapping_index;
306     union {
307         /* offset is
308          * - the offset in the file (in clusters) for a file, or
309          * - the next cluster of the directory for a directory
310          */
311         struct {
312             uint32_t offset;
313         } file;
314         struct {
315             int parent_mapping_index;
316             int first_dir_index;
317         } dir;
318     } info;
319     /* path contains the full path, i.e. it always starts with s->path */
320     char* path;
321 
322     enum {
323         MODE_UNDEFINED = 0,
324         MODE_NORMAL = 1,
325         MODE_MODIFIED = 2,
326         MODE_DIRECTORY = 4,
327         MODE_DELETED = 8,
328     } mode;
329     int read_only;
330 } mapping_t;
331 
332 #ifdef DEBUG
333 static void print_direntry(const struct direntry_t*);
334 static void print_mapping(const struct mapping_t* mapping);
335 #endif
336 
337 /* here begins the real VVFAT driver */
338 
339 typedef struct BDRVVVFATState {
340     CoMutex lock;
341     BlockDriverState* bs; /* pointer to parent */
342     unsigned char first_sectors[0x40*0x200];
343 
344     int fat_type; /* 16 or 32 */
345     array_t fat,directory,mapping;
346     char volume_label[11];
347 
348     uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
349 
350     unsigned int cluster_size;
351     unsigned int sectors_per_cluster;
352     unsigned int sectors_per_fat;
353     uint32_t last_cluster_of_root_directory;
354     /* how many entries are available in root directory (0 for FAT32) */
355     uint16_t root_entries;
356     uint32_t sector_count; /* total number of sectors of the partition */
357     uint32_t cluster_count; /* total number of clusters of this partition */
358     uint32_t max_fat_value;
359     uint32_t offset_to_fat;
360     uint32_t offset_to_root_dir;
361 
362     int current_fd;
363     mapping_t* current_mapping;
364     unsigned char* cluster; /* points to current cluster */
365     unsigned char* cluster_buffer; /* points to a buffer to hold temp data */
366     unsigned int current_cluster;
367 
368     /* write support */
369     char* qcow_filename;
370     BdrvChild* qcow;
371     void* fat2;
372     char* used_clusters;
373     array_t commits;
374     const char* path;
375     int downcase_short_names;
376 
377     Error *migration_blocker;
378 } BDRVVVFATState;
379 
380 /* take the sector position spos and convert it to Cylinder/Head/Sector position
381  * if the position is outside the specified geometry, fill maximum value for CHS
382  * and return 1 to signal overflow.
383  */
384 static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
385 {
386     int head,sector;
387     sector   = spos % secs;  spos /= secs;
388     head     = spos % heads; spos /= heads;
389     if (spos >= cyls) {
390         /* Overflow,
391         it happens if 32bit sector positions are used, while CHS is only 24bit.
392         Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
393         chs->head     = 0xFF;
394         chs->sector   = 0xFF;
395         chs->cylinder = 0xFF;
396         return 1;
397     }
398     chs->head     = (uint8_t)head;
399     chs->sector   = (uint8_t)( (sector+1) | ((spos>>8)<<6) );
400     chs->cylinder = (uint8_t)spos;
401     return 0;
402 }
403 
404 static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
405 {
406     /* TODO: if the files mbr.img and bootsect.img exist, use them */
407     mbr_t* real_mbr=(mbr_t*)s->first_sectors;
408     partition_t* partition = &(real_mbr->partition[0]);
409     int lba;
410 
411     memset(s->first_sectors,0,512);
412 
413     /* Win NT Disk Signature */
414     real_mbr->nt_id= cpu_to_le32(0xbe1afdfa);
415 
416     partition->attributes=0x80; /* bootable */
417 
418     /* LBA is used when partition is outside the CHS geometry */
419     lba  = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
420                      cyls, heads, secs);
421     lba |= sector2CHS(&partition->end_CHS,   s->bs->total_sectors - 1,
422                      cyls, heads, secs);
423 
424     /*LBA partitions are identified only by start/length_sector_long not by CHS*/
425     partition->start_sector_long  = cpu_to_le32(s->offset_to_bootsector);
426     partition->length_sector_long = cpu_to_le32(s->bs->total_sectors
427                                                 - s->offset_to_bootsector);
428 
429     /* FAT12/FAT16/FAT32 */
430     /* DOS uses different types when partition is LBA,
431        probably to prevent older versions from using CHS on them */
432     partition->fs_type = s->fat_type == 12 ? 0x1 :
433                          s->fat_type == 16 ? (lba ? 0xe : 0x06) :
434                        /*s->fat_type == 32*/ (lba ? 0xc : 0x0b);
435 
436     real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
437 }
438 
439 /* direntry functions */
440 
441 static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
442 {
443     int number_of_entries, i;
444     glong length;
445     direntry_t *entry;
446 
447     gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
448     if (!longname) {
449         fprintf(stderr, "vvfat: invalid UTF-8 name: %s\n", filename);
450         return NULL;
451     }
452 
453     number_of_entries = DIV_ROUND_UP(length * 2, 26);
454 
455     for(i=0;i<number_of_entries;i++) {
456         entry=array_get_next(&(s->directory));
457         entry->attributes=0xf;
458         entry->reserved[0]=0;
459         entry->begin=0;
460         entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
461     }
462     for(i=0;i<26*number_of_entries;i++) {
463         int offset=(i%26);
464         if(offset<10) offset=1+offset;
465         else if(offset<22) offset=14+offset-10;
466         else offset=28+offset-22;
467         entry=array_get(&(s->directory),s->directory.next-1-(i/26));
468         if (i >= 2 * length + 2) {
469             entry->name[offset] = 0xff;
470         } else if (i % 2 == 0) {
471             entry->name[offset] = longname[i / 2] & 0xff;
472         } else {
473             entry->name[offset] = longname[i / 2] >> 8;
474         }
475     }
476     g_free(longname);
477     return array_get(&(s->directory),s->directory.next-number_of_entries);
478 }
479 
480 static char is_free(const direntry_t* direntry)
481 {
482     return direntry->name[0] == DIR_DELETED || direntry->name[0] == DIR_FREE;
483 }
484 
485 static char is_volume_label(const direntry_t* direntry)
486 {
487     return direntry->attributes == 0x28;
488 }
489 
490 static char is_long_name(const direntry_t* direntry)
491 {
492     return direntry->attributes == 0xf;
493 }
494 
495 static char is_short_name(const direntry_t* direntry)
496 {
497     return !is_volume_label(direntry) && !is_long_name(direntry)
498         && !is_free(direntry);
499 }
500 
501 static char is_directory(const direntry_t* direntry)
502 {
503     return direntry->attributes & 0x10 && direntry->name[0] != DIR_DELETED;
504 }
505 
506 static inline char is_dot(const direntry_t* direntry)
507 {
508     return is_short_name(direntry) && direntry->name[0] == '.';
509 }
510 
511 static char is_file(const direntry_t* direntry)
512 {
513     return is_short_name(direntry) && !is_directory(direntry);
514 }
515 
516 static inline uint32_t begin_of_direntry(const direntry_t* direntry)
517 {
518     return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
519 }
520 
521 static inline uint32_t filesize_of_direntry(const direntry_t* direntry)
522 {
523     return le32_to_cpu(direntry->size);
524 }
525 
526 static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin)
527 {
528     direntry->begin = cpu_to_le16(begin & 0xffff);
529     direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff);
530 }
531 
532 static uint8_t to_valid_short_char(gunichar c)
533 {
534     c = g_unichar_toupper(c);
535     if ((c >= '0' && c <= '9') ||
536         (c >= 'A' && c <= 'Z') ||
537         strchr("$%'-_@~`!(){}^#&", c) != 0) {
538         return c;
539     } else {
540         return 0;
541     }
542 }
543 
544 static direntry_t *create_short_filename(BDRVVVFATState *s,
545                                          const char *filename,
546                                          unsigned int directory_start)
547 {
548     int i, j = 0;
549     direntry_t *entry = array_get_next(&(s->directory));
550     const gchar *p, *last_dot = NULL;
551     gunichar c;
552     bool lossy_conversion = false;
553     char tail[8];
554 
555     if (!entry) {
556         return NULL;
557     }
558     memset(entry->name, 0x20, sizeof(entry->name));
559 
560     /* copy filename and search last dot */
561     for (p = filename; ; p = g_utf8_next_char(p)) {
562         c = g_utf8_get_char(p);
563         if (c == '\0') {
564             break;
565         } else if (c == '.') {
566             if (j == 0) {
567                 /* '.' at start of filename */
568                 lossy_conversion = true;
569             } else {
570                 if (last_dot) {
571                     lossy_conversion = true;
572                 }
573                 last_dot = p;
574             }
575         } else if (!last_dot) {
576             /* first part of the name; copy it */
577             uint8_t v = to_valid_short_char(c);
578             if (j < 8 && v) {
579                 entry->name[j++] = v;
580             } else {
581                 lossy_conversion = true;
582             }
583         }
584     }
585 
586     /* copy extension (if any) */
587     if (last_dot) {
588         j = 0;
589         for (p = g_utf8_next_char(last_dot); ; p = g_utf8_next_char(p)) {
590             c = g_utf8_get_char(p);
591             if (c == '\0') {
592                 break;
593             } else {
594                 /* extension; copy it */
595                 uint8_t v = to_valid_short_char(c);
596                 if (j < 3 && v) {
597                     entry->name[8 + (j++)] = v;
598                 } else {
599                     lossy_conversion = true;
600                 }
601             }
602         }
603     }
604 
605     if (entry->name[0] == DIR_KANJI) {
606         entry->name[0] = DIR_KANJI_FAKE;
607     }
608 
609     /* numeric-tail generation */
610     for (j = 0; j < 8; j++) {
611         if (entry->name[j] == ' ') {
612             break;
613         }
614     }
615     for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
616         direntry_t *entry1;
617         if (i > 0) {
618             int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
619             assert(len <= 7);
620             memcpy(entry->name + MIN(j, 8 - len), tail, len);
621         }
622         for (entry1 = array_get(&(s->directory), directory_start);
623              entry1 < entry; entry1++) {
624             if (!is_long_name(entry1) &&
625                 !memcmp(entry1->name, entry->name, 11)) {
626                 break; /* found dupe */
627             }
628         }
629         if (entry1 == entry) {
630             /* no dupe found */
631             return entry;
632         }
633     }
634     return NULL;
635 }
636 
637 /* fat functions */
638 
639 static inline uint8_t fat_chksum(const direntry_t* entry)
640 {
641     uint8_t chksum=0;
642     int i;
643 
644     for (i = 0; i < ARRAY_SIZE(entry->name); i++) {
645         chksum = (((chksum & 0xfe) >> 1) |
646                   ((chksum & 0x01) ? 0x80 : 0)) + entry->name[i];
647     }
648 
649     return chksum;
650 }
651 
652 /* if return_time==0, this returns the fat_date, else the fat_time */
653 static uint16_t fat_datetime(time_t time,int return_time) {
654     struct tm* t;
655     struct tm t1;
656     t = &t1;
657     localtime_r(&time,t);
658     if(return_time)
659         return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
660     return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
661 }
662 
663 static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
664 {
665     if(s->fat_type==32) {
666         uint32_t* entry=array_get(&(s->fat),cluster);
667         *entry=cpu_to_le32(value);
668     } else if(s->fat_type==16) {
669         uint16_t* entry=array_get(&(s->fat),cluster);
670         *entry=cpu_to_le16(value&0xffff);
671     } else {
672         int offset = (cluster*3/2);
673         unsigned char* p = array_get(&(s->fat), offset);
674         switch (cluster&1) {
675         case 0:
676                 p[0] = value&0xff;
677                 p[1] = (p[1]&0xf0) | ((value>>8)&0xf);
678                 break;
679         case 1:
680                 p[0] = (p[0]&0xf) | ((value&0xf)<<4);
681                 p[1] = (value>>4);
682                 break;
683         }
684     }
685 }
686 
687 static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
688 {
689     if(s->fat_type==32) {
690         uint32_t* entry=array_get(&(s->fat),cluster);
691         return le32_to_cpu(*entry);
692     } else if(s->fat_type==16) {
693         uint16_t* entry=array_get(&(s->fat),cluster);
694         return le16_to_cpu(*entry);
695     } else {
696         const uint8_t* x=(uint8_t*)(s->fat.pointer)+cluster*3/2;
697         return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
698     }
699 }
700 
701 static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
702 {
703     if(fat_entry>s->max_fat_value-8)
704         return -1;
705     return 0;
706 }
707 
708 static inline void init_fat(BDRVVVFATState* s)
709 {
710     if (s->fat_type == 12) {
711         array_init(&(s->fat),1);
712         array_ensure_allocated(&(s->fat),
713                 s->sectors_per_fat * 0x200 * 3 / 2 - 1);
714     } else {
715         array_init(&(s->fat),(s->fat_type==32?4:2));
716         array_ensure_allocated(&(s->fat),
717                 s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
718     }
719     memset(s->fat.pointer,0,s->fat.size);
720 
721     switch(s->fat_type) {
722         case 12: s->max_fat_value=0xfff; break;
723         case 16: s->max_fat_value=0xffff; break;
724         case 32: s->max_fat_value=0x0fffffff; break;
725         default: s->max_fat_value=0; /* error... */
726     }
727 
728 }
729 
730 static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
731         unsigned int directory_start, const char* filename, int is_dot)
732 {
733     int long_index = s->directory.next;
734     direntry_t* entry = NULL;
735     direntry_t* entry_long = NULL;
736 
737     if(is_dot) {
738         entry=array_get_next(&(s->directory));
739         memset(entry->name, 0x20, sizeof(entry->name));
740         memcpy(entry->name,filename,strlen(filename));
741         return entry;
742     }
743 
744     entry_long=create_long_filename(s,filename);
745     entry = create_short_filename(s, filename, directory_start);
746 
747     /* calculate checksum; propagate to long name */
748     if(entry_long) {
749         uint8_t chksum=fat_chksum(entry);
750 
751         /* calculate anew, because realloc could have taken place */
752         entry_long=array_get(&(s->directory),long_index);
753         while(entry_long<entry && is_long_name(entry_long)) {
754             entry_long->reserved[1]=chksum;
755             entry_long++;
756         }
757     }
758 
759     return entry;
760 }
761 
762 /*
763  * Read a directory. (the index of the corresponding mapping must be passed).
764  */
765 static int read_directory(BDRVVVFATState* s, int mapping_index)
766 {
767     mapping_t* mapping = array_get(&(s->mapping), mapping_index);
768     direntry_t* direntry;
769     const char* dirname = mapping->path;
770     int first_cluster = mapping->begin;
771     int parent_index = mapping->info.dir.parent_mapping_index;
772     mapping_t* parent_mapping = (mapping_t*)
773         (parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
774     int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
775 
776     DIR* dir=opendir(dirname);
777     struct dirent* entry;
778     int i;
779 
780     assert(mapping->mode & MODE_DIRECTORY);
781 
782     if(!dir) {
783         mapping->end = mapping->begin;
784         return -1;
785     }
786 
787     i = mapping->info.dir.first_dir_index =
788             first_cluster == 0 ? 0 : s->directory.next;
789 
790     if (first_cluster != 0) {
791         /* create the top entries of a subdirectory */
792         (void)create_short_and_long_name(s, i, ".", 1);
793         (void)create_short_and_long_name(s, i, "..", 1);
794     }
795 
796     /* actually read the directory, and allocate the mappings */
797     while((entry=readdir(dir))) {
798         unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
799         char* buffer;
800         direntry_t* direntry;
801         struct stat st;
802         int is_dot=!strcmp(entry->d_name,".");
803         int is_dotdot=!strcmp(entry->d_name,"..");
804 
805         if (first_cluster == 0 && s->directory.next >= s->root_entries - 1) {
806             fprintf(stderr, "Too many entries in root directory\n");
807             closedir(dir);
808             return -2;
809         }
810 
811         if(first_cluster == 0 && (is_dotdot || is_dot))
812             continue;
813 
814         buffer = g_malloc(length);
815         snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
816 
817         if(stat(buffer,&st)<0) {
818             g_free(buffer);
819             continue;
820         }
821 
822         /* create directory entry for this file */
823         if (!is_dot && !is_dotdot) {
824             direntry = create_short_and_long_name(s, i, entry->d_name, 0);
825         } else {
826             direntry = array_get(&(s->directory), is_dot ? i : i + 1);
827         }
828         direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
829         direntry->reserved[0]=direntry->reserved[1]=0;
830         direntry->ctime=fat_datetime(st.st_ctime,1);
831         direntry->cdate=fat_datetime(st.st_ctime,0);
832         direntry->adate=fat_datetime(st.st_atime,0);
833         direntry->begin_hi=0;
834         direntry->mtime=fat_datetime(st.st_mtime,1);
835         direntry->mdate=fat_datetime(st.st_mtime,0);
836         if(is_dotdot)
837             set_begin_of_direntry(direntry, first_cluster_of_parent);
838         else if(is_dot)
839             set_begin_of_direntry(direntry, first_cluster);
840         else
841             direntry->begin=0; /* do that later */
842         if (st.st_size > 0x7fffffff) {
843             fprintf(stderr, "File %s is larger than 2GB\n", buffer);
844             g_free(buffer);
845             closedir(dir);
846             return -2;
847         }
848         direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size);
849 
850         /* create mapping for this file */
851         if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
852             s->current_mapping = array_get_next(&(s->mapping));
853             s->current_mapping->begin=0;
854             s->current_mapping->end=st.st_size;
855             /*
856              * we get the direntry of the most recent direntry, which
857              * contains the short name and all the relevant information.
858              */
859             s->current_mapping->dir_index=s->directory.next-1;
860             s->current_mapping->first_mapping_index = -1;
861             if (S_ISDIR(st.st_mode)) {
862                 s->current_mapping->mode = MODE_DIRECTORY;
863                 s->current_mapping->info.dir.parent_mapping_index =
864                     mapping_index;
865             } else {
866                 s->current_mapping->mode = MODE_UNDEFINED;
867                 s->current_mapping->info.file.offset = 0;
868             }
869             s->current_mapping->path=buffer;
870             s->current_mapping->read_only =
871                 (st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0;
872         } else {
873             g_free(buffer);
874         }
875     }
876     closedir(dir);
877 
878     /* fill with zeroes up to the end of the cluster */
879     while(s->directory.next%(0x10*s->sectors_per_cluster)) {
880         direntry_t* direntry=array_get_next(&(s->directory));
881         memset(direntry,0,sizeof(direntry_t));
882     }
883 
884     if (s->fat_type != 32 &&
885         mapping_index == 0 &&
886         s->directory.next < s->root_entries) {
887         /* root directory */
888         int cur = s->directory.next;
889         array_ensure_allocated(&(s->directory), s->root_entries - 1);
890         s->directory.next = s->root_entries;
891         memset(array_get(&(s->directory), cur), 0,
892                 (s->root_entries - cur) * sizeof(direntry_t));
893     }
894 
895     /* re-get the mapping, since s->mapping was possibly realloc()ed */
896     mapping = array_get(&(s->mapping), mapping_index);
897     first_cluster += (s->directory.next - mapping->info.dir.first_dir_index)
898         * 0x20 / s->cluster_size;
899     mapping->end = first_cluster;
900 
901     direntry = array_get(&(s->directory), mapping->dir_index);
902     set_begin_of_direntry(direntry, mapping->begin);
903 
904     return 0;
905 }
906 
907 static inline uint32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
908 {
909     return (sector_num - s->offset_to_root_dir) / s->sectors_per_cluster;
910 }
911 
912 static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
913 {
914     return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
915 }
916 
917 static int init_directories(BDRVVVFATState* s,
918                             const char *dirname, int heads, int secs,
919                             Error **errp)
920 {
921     bootsector_t* bootsector;
922     mapping_t* mapping;
923     unsigned int i;
924     unsigned int cluster;
925 
926     memset(&(s->first_sectors[0]),0,0x40*0x200);
927 
928     s->cluster_size=s->sectors_per_cluster*0x200;
929     s->cluster_buffer=g_malloc(s->cluster_size);
930 
931     /*
932      * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
933      * where sc is sector_count,
934      * spf is sectors_per_fat,
935      * spc is sectors_per_clusters, and
936      * fat_type = 12, 16 or 32.
937      */
938     i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
939     s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
940 
941     s->offset_to_fat = s->offset_to_bootsector + 1;
942     s->offset_to_root_dir = s->offset_to_fat + s->sectors_per_fat * 2;
943 
944     array_init(&(s->mapping),sizeof(mapping_t));
945     array_init(&(s->directory),sizeof(direntry_t));
946 
947     /* add volume label */
948     {
949         direntry_t* entry=array_get_next(&(s->directory));
950         entry->attributes=0x28; /* archive | volume label */
951         memcpy(entry->name, s->volume_label, sizeof(entry->name));
952     }
953 
954     /* Now build FAT, and write back information into directory */
955     init_fat(s);
956 
957     /* TODO: if there are more entries, bootsector has to be adjusted! */
958     s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
959     s->cluster_count=sector2cluster(s, s->sector_count);
960 
961     mapping = array_get_next(&(s->mapping));
962     mapping->begin = 0;
963     mapping->dir_index = 0;
964     mapping->info.dir.parent_mapping_index = -1;
965     mapping->first_mapping_index = -1;
966     mapping->path = g_strdup(dirname);
967     i = strlen(mapping->path);
968     if (i > 0 && mapping->path[i - 1] == '/')
969         mapping->path[i - 1] = '\0';
970     mapping->mode = MODE_DIRECTORY;
971     mapping->read_only = 0;
972     s->path = mapping->path;
973 
974     for (i = 0, cluster = 0; i < s->mapping.next; i++) {
975         /* MS-DOS expects the FAT to be 0 for the root directory
976          * (except for the media byte). */
977         /* LATER TODO: still true for FAT32? */
978         int fix_fat = (i != 0);
979         mapping = array_get(&(s->mapping), i);
980 
981         if (mapping->mode & MODE_DIRECTORY) {
982             mapping->begin = cluster;
983             if(read_directory(s, i)) {
984                 error_setg(errp, "Could not read directory %s",
985                            mapping->path);
986                 return -1;
987             }
988             mapping = array_get(&(s->mapping), i);
989         } else {
990             assert(mapping->mode == MODE_UNDEFINED);
991             mapping->mode=MODE_NORMAL;
992             mapping->begin = cluster;
993             if (mapping->end > 0) {
994                 direntry_t* direntry = array_get(&(s->directory),
995                         mapping->dir_index);
996 
997                 mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size;
998                 set_begin_of_direntry(direntry, mapping->begin);
999             } else {
1000                 mapping->end = cluster + 1;
1001                 fix_fat = 0;
1002             }
1003         }
1004 
1005         assert(mapping->begin < mapping->end);
1006 
1007         /* next free cluster */
1008         cluster = mapping->end;
1009 
1010         if(cluster > s->cluster_count) {
1011             error_setg(errp,
1012                        "Directory does not fit in FAT%d (capacity %.2f MB)",
1013                        s->fat_type, s->sector_count / 2000.0);
1014             return -1;
1015         }
1016 
1017         /* fix fat for entry */
1018         if (fix_fat) {
1019             int j;
1020             for(j = mapping->begin; j < mapping->end - 1; j++)
1021                 fat_set(s, j, j+1);
1022             fat_set(s, mapping->end - 1, s->max_fat_value);
1023         }
1024     }
1025 
1026     mapping = array_get(&(s->mapping), 0);
1027     s->last_cluster_of_root_directory = mapping->end;
1028 
1029     /* the FAT signature */
1030     fat_set(s,0,s->max_fat_value);
1031     fat_set(s,1,s->max_fat_value);
1032 
1033     s->current_mapping = NULL;
1034 
1035     bootsector = (bootsector_t *)(s->first_sectors
1036                                   + s->offset_to_bootsector * 0x200);
1037     bootsector->jump[0]=0xeb;
1038     bootsector->jump[1]=0x3e;
1039     bootsector->jump[2]=0x90;
1040     memcpy(bootsector->name, BOOTSECTOR_OEM_NAME, 8);
1041     bootsector->sector_size=cpu_to_le16(0x200);
1042     bootsector->sectors_per_cluster=s->sectors_per_cluster;
1043     bootsector->reserved_sectors=cpu_to_le16(1);
1044     bootsector->number_of_fats=0x2; /* number of FATs */
1045     bootsector->root_entries = cpu_to_le16(s->root_entries);
1046     bootsector->total_sectors16=s->sector_count>0xffff?0:cpu_to_le16(s->sector_count);
1047     /* media descriptor: hard disk=0xf8, floppy=0xf0 */
1048     bootsector->media_type = (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0);
1049     s->fat.pointer[0] = bootsector->media_type;
1050     bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
1051     bootsector->sectors_per_track = cpu_to_le16(secs);
1052     bootsector->number_of_heads = cpu_to_le16(heads);
1053     bootsector->hidden_sectors = cpu_to_le32(s->offset_to_bootsector);
1054     bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0);
1055 
1056     /* LATER TODO: if FAT32, this is wrong */
1057     /* drive_number: fda=0, hda=0x80 */
1058     bootsector->u.fat16.drive_number = s->offset_to_bootsector == 0 ? 0 : 0x80;
1059     bootsector->u.fat16.signature=0x29;
1060     bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);
1061 
1062     memcpy(bootsector->u.fat16.volume_label, s->volume_label,
1063            sizeof(bootsector->u.fat16.volume_label));
1064     memcpy(bootsector->u.fat16.fat_type,
1065            s->fat_type == 12 ? "FAT12   " : "FAT16   ", 8);
1066     bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;
1067 
1068     return 0;
1069 }
1070 
1071 #ifdef DEBUG
1072 static BDRVVVFATState *vvv = NULL;
1073 #endif
1074 
1075 static int enable_write_target(BlockDriverState *bs, Error **errp);
1076 static int is_consistent(BDRVVVFATState *s);
1077 
1078 static QemuOptsList runtime_opts = {
1079     .name = "vvfat",
1080     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1081     .desc = {
1082         {
1083             .name = "dir",
1084             .type = QEMU_OPT_STRING,
1085             .help = "Host directory to map to the vvfat device",
1086         },
1087         {
1088             .name = "fat-type",
1089             .type = QEMU_OPT_NUMBER,
1090             .help = "FAT type (12, 16 or 32)",
1091         },
1092         {
1093             .name = "floppy",
1094             .type = QEMU_OPT_BOOL,
1095             .help = "Create a floppy rather than a hard disk image",
1096         },
1097         {
1098             .name = "label",
1099             .type = QEMU_OPT_STRING,
1100             .help = "Use a volume label other than QEMU VVFAT",
1101         },
1102         {
1103             .name = "rw",
1104             .type = QEMU_OPT_BOOL,
1105             .help = "Make the image writable",
1106         },
1107         { /* end of list */ }
1108     },
1109 };
1110 
1111 static void vvfat_parse_filename(const char *filename, QDict *options,
1112                                  Error **errp)
1113 {
1114     int fat_type = 0;
1115     bool floppy = false;
1116     bool rw = false;
1117     int i;
1118 
1119     if (!strstart(filename, "fat:", NULL)) {
1120         error_setg(errp, "File name string must start with 'fat:'");
1121         return;
1122     }
1123 
1124     /* Parse options */
1125     if (strstr(filename, ":32:")) {
1126         fat_type = 32;
1127     } else if (strstr(filename, ":16:")) {
1128         fat_type = 16;
1129     } else if (strstr(filename, ":12:")) {
1130         fat_type = 12;
1131     }
1132 
1133     if (strstr(filename, ":floppy:")) {
1134         floppy = true;
1135     }
1136 
1137     if (strstr(filename, ":rw:")) {
1138         rw = true;
1139     }
1140 
1141     /* Get the directory name without options */
1142     i = strrchr(filename, ':') - filename;
1143     assert(i >= 3);
1144     if (filename[i - 2] == ':' && qemu_isalpha(filename[i - 1])) {
1145         /* workaround for DOS drive names */
1146         filename += i - 1;
1147     } else {
1148         filename += i + 1;
1149     }
1150 
1151     /* Fill in the options QDict */
1152     qdict_put_str(options, "dir", filename);
1153     qdict_put_int(options, "fat-type", fat_type);
1154     qdict_put_bool(options, "floppy", floppy);
1155     qdict_put_bool(options, "rw", rw);
1156 }
1157 
1158 static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
1159                       Error **errp)
1160 {
1161     BDRVVVFATState *s = bs->opaque;
1162     int cyls, heads, secs;
1163     bool floppy;
1164     const char *dirname, *label;
1165     QemuOpts *opts;
1166     Error *local_err = NULL;
1167     int ret;
1168 
1169 #ifdef DEBUG
1170     vvv = s;
1171 #endif
1172 
1173     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1174     qemu_opts_absorb_qdict(opts, options, &local_err);
1175     if (local_err) {
1176         error_propagate(errp, local_err);
1177         ret = -EINVAL;
1178         goto fail;
1179     }
1180 
1181     dirname = qemu_opt_get(opts, "dir");
1182     if (!dirname) {
1183         error_setg(errp, "vvfat block driver requires a 'dir' option");
1184         ret = -EINVAL;
1185         goto fail;
1186     }
1187 
1188     s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
1189     floppy = qemu_opt_get_bool(opts, "floppy", false);
1190 
1191     memset(s->volume_label, ' ', sizeof(s->volume_label));
1192     label = qemu_opt_get(opts, "label");
1193     if (label) {
1194         size_t label_length = strlen(label);
1195         if (label_length > 11) {
1196             error_setg(errp, "vvfat label cannot be longer than 11 bytes");
1197             ret = -EINVAL;
1198             goto fail;
1199         }
1200         memcpy(s->volume_label, label, label_length);
1201     } else {
1202         memcpy(s->volume_label, "QEMU VVFAT", 10);
1203     }
1204 
1205     if (floppy) {
1206         /* 1.44MB or 2.88MB floppy.  2.88MB can be FAT12 (default) or FAT16. */
1207         if (!s->fat_type) {
1208             s->fat_type = 12;
1209             secs = 36;
1210             s->sectors_per_cluster = 2;
1211         } else {
1212             secs = s->fat_type == 12 ? 18 : 36;
1213             s->sectors_per_cluster = 1;
1214         }
1215         cyls = 80;
1216         heads = 2;
1217     } else {
1218         /* 32MB or 504MB disk*/
1219         if (!s->fat_type) {
1220             s->fat_type = 16;
1221         }
1222         s->offset_to_bootsector = 0x3f;
1223         cyls = s->fat_type == 12 ? 64 : 1024;
1224         heads = 16;
1225         secs = 63;
1226     }
1227 
1228     switch (s->fat_type) {
1229     case 32:
1230         warn_report("FAT32 has not been tested. "
1231                     "You are welcome to do so!");
1232         break;
1233     case 16:
1234     case 12:
1235         break;
1236     default:
1237         error_setg(errp, "Valid FAT types are only 12, 16 and 32");
1238         ret = -EINVAL;
1239         goto fail;
1240     }
1241 
1242 
1243     s->bs = bs;
1244 
1245     /* LATER TODO: if FAT32, adjust */
1246     s->sectors_per_cluster=0x10;
1247 
1248     s->current_cluster=0xffffffff;
1249 
1250     s->qcow = NULL;
1251     s->qcow_filename = NULL;
1252     s->fat2 = NULL;
1253     s->downcase_short_names = 1;
1254 
1255     fprintf(stderr, "vvfat %s chs %d,%d,%d\n",
1256             dirname, cyls, heads, secs);
1257 
1258     s->sector_count = cyls * heads * secs - s->offset_to_bootsector;
1259 
1260     if (qemu_opt_get_bool(opts, "rw", false)) {
1261         if (!bdrv_is_read_only(bs)) {
1262             ret = enable_write_target(bs, errp);
1263             if (ret < 0) {
1264                 goto fail;
1265             }
1266         } else {
1267             ret = -EPERM;
1268             error_setg(errp,
1269                        "Unable to set VVFAT to 'rw' when drive is read-only");
1270             goto fail;
1271         }
1272     } else  {
1273         /* read only is the default for safety */
1274         ret = bdrv_set_read_only(bs, true, &local_err);
1275         if (ret < 0) {
1276             error_propagate(errp, local_err);
1277             goto fail;
1278         }
1279     }
1280 
1281     bs->total_sectors = cyls * heads * secs;
1282 
1283     if (init_directories(s, dirname, heads, secs, errp)) {
1284         ret = -EIO;
1285         goto fail;
1286     }
1287 
1288     s->sector_count = s->offset_to_root_dir
1289                     + s->sectors_per_cluster * s->cluster_count;
1290 
1291     /* Disable migration when vvfat is used rw */
1292     if (s->qcow) {
1293         error_setg(&s->migration_blocker,
1294                    "The vvfat (rw) format used by node '%s' "
1295                    "does not support live migration",
1296                    bdrv_get_device_or_node_name(bs));
1297         ret = migrate_add_blocker(s->migration_blocker, &local_err);
1298         if (local_err) {
1299             error_propagate(errp, local_err);
1300             error_free(s->migration_blocker);
1301             goto fail;
1302         }
1303     }
1304 
1305     if (s->offset_to_bootsector > 0) {
1306         init_mbr(s, cyls, heads, secs);
1307     }
1308 
1309     qemu_co_mutex_init(&s->lock);
1310 
1311     ret = 0;
1312 fail:
1313     qemu_opts_del(opts);
1314     return ret;
1315 }
1316 
1317 static void vvfat_refresh_limits(BlockDriverState *bs, Error **errp)
1318 {
1319     bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
1320 }
1321 
1322 static inline void vvfat_close_current_file(BDRVVVFATState *s)
1323 {
1324     if(s->current_mapping) {
1325         s->current_mapping = NULL;
1326         if (s->current_fd) {
1327                 qemu_close(s->current_fd);
1328                 s->current_fd = 0;
1329         }
1330     }
1331     s->current_cluster = -1;
1332 }
1333 
1334 /* mappings between index1 and index2-1 are supposed to be ordered
1335  * return value is the index of the last mapping for which end>cluster_num
1336  */
1337 static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
1338 {
1339     while(1) {
1340         int index3;
1341         mapping_t* mapping;
1342         index3=(index1+index2)/2;
1343         mapping=array_get(&(s->mapping),index3);
1344         assert(mapping->begin < mapping->end);
1345         if(mapping->begin>=cluster_num) {
1346             assert(index2!=index3 || index2==0);
1347             if(index2==index3)
1348                 return index1;
1349             index2=index3;
1350         } else {
1351             if(index1==index3)
1352                 return mapping->end<=cluster_num ? index2 : index1;
1353             index1=index3;
1354         }
1355         assert(index1<=index2);
1356         DLOG(mapping=array_get(&(s->mapping),index1);
1357         assert(mapping->begin<=cluster_num);
1358         assert(index2 >= s->mapping.next ||
1359                 ((mapping = array_get(&(s->mapping),index2)) &&
1360                 mapping->end>cluster_num)));
1361     }
1362 }
1363 
1364 static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num)
1365 {
1366     int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
1367     mapping_t* mapping;
1368     if(index>=s->mapping.next)
1369         return NULL;
1370     mapping=array_get(&(s->mapping),index);
1371     if(mapping->begin>cluster_num)
1372         return NULL;
1373     assert(mapping->begin<=cluster_num && mapping->end>cluster_num);
1374     return mapping;
1375 }
1376 
1377 static int open_file(BDRVVVFATState* s,mapping_t* mapping)
1378 {
1379     if(!mapping)
1380         return -1;
1381     if(!s->current_mapping ||
1382             strcmp(s->current_mapping->path,mapping->path)) {
1383         /* open file */
1384         int fd = qemu_open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE);
1385         if(fd<0)
1386             return -1;
1387         vvfat_close_current_file(s);
1388         s->current_fd = fd;
1389         s->current_mapping = mapping;
1390     }
1391     return 0;
1392 }
1393 
1394 static inline int read_cluster(BDRVVVFATState *s,int cluster_num)
1395 {
1396     if(s->current_cluster != cluster_num) {
1397         int result=0;
1398         off_t offset;
1399         assert(!s->current_mapping || s->current_fd || (s->current_mapping->mode & MODE_DIRECTORY));
1400         if(!s->current_mapping
1401                 || s->current_mapping->begin>cluster_num
1402                 || s->current_mapping->end<=cluster_num) {
1403             /* binary search of mappings for file */
1404             mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
1405 
1406             assert(!mapping || (cluster_num>=mapping->begin && cluster_num<mapping->end));
1407 
1408             if (mapping && mapping->mode & MODE_DIRECTORY) {
1409                 vvfat_close_current_file(s);
1410                 s->current_mapping = mapping;
1411 read_cluster_directory:
1412                 offset = s->cluster_size*(cluster_num-s->current_mapping->begin);
1413                 s->cluster = (unsigned char*)s->directory.pointer+offset
1414                         + 0x20*s->current_mapping->info.dir.first_dir_index;
1415                 assert(((s->cluster-(unsigned char*)s->directory.pointer)%s->cluster_size)==0);
1416                 assert((char*)s->cluster+s->cluster_size <= s->directory.pointer+s->directory.next*s->directory.item_size);
1417                 s->current_cluster = cluster_num;
1418                 return 0;
1419             }
1420 
1421             if(open_file(s,mapping))
1422                 return -2;
1423         } else if (s->current_mapping->mode & MODE_DIRECTORY)
1424             goto read_cluster_directory;
1425 
1426         assert(s->current_fd);
1427 
1428         offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset;
1429         if(lseek(s->current_fd, offset, SEEK_SET)!=offset)
1430             return -3;
1431         s->cluster=s->cluster_buffer;
1432         result=read(s->current_fd,s->cluster,s->cluster_size);
1433         if(result<0) {
1434             s->current_cluster = -1;
1435             return -1;
1436         }
1437         s->current_cluster = cluster_num;
1438     }
1439     return 0;
1440 }
1441 
1442 #ifdef DEBUG
1443 static void print_direntry(const direntry_t* direntry)
1444 {
1445     int j = 0;
1446     char buffer[1024];
1447 
1448     fprintf(stderr, "direntry %p: ", direntry);
1449     if(!direntry)
1450         return;
1451     if(is_long_name(direntry)) {
1452         unsigned char* c=(unsigned char*)direntry;
1453         int i;
1454         for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
1455 #define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
1456             ADD_CHAR(c[i]);
1457         for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
1458             ADD_CHAR(c[i]);
1459         for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2)
1460             ADD_CHAR(c[i]);
1461         buffer[j] = 0;
1462         fprintf(stderr, "%s\n", buffer);
1463     } else {
1464         int i;
1465         for(i=0;i<11;i++)
1466             ADD_CHAR(direntry->name[i]);
1467         buffer[j] = 0;
1468         fprintf(stderr,"%s attributes=0x%02x begin=%d size=%d\n",
1469                 buffer,
1470                 direntry->attributes,
1471                 begin_of_direntry(direntry),le32_to_cpu(direntry->size));
1472     }
1473 }
1474 
1475 static void print_mapping(const mapping_t* mapping)
1476 {
1477     fprintf(stderr, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1478         "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1479         mapping, mapping->begin, mapping->end, mapping->dir_index,
1480         mapping->first_mapping_index, mapping->path, mapping->mode);
1481 
1482     if (mapping->mode & MODE_DIRECTORY)
1483         fprintf(stderr, "parent_mapping_index = %d, first_dir_index = %d\n", mapping->info.dir.parent_mapping_index, mapping->info.dir.first_dir_index);
1484     else
1485         fprintf(stderr, "offset = %d\n", mapping->info.file.offset);
1486 }
1487 #endif
1488 
1489 static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
1490                     uint8_t *buf, int nb_sectors)
1491 {
1492     BDRVVVFATState *s = bs->opaque;
1493     int i;
1494 
1495     for(i=0;i<nb_sectors;i++,sector_num++) {
1496         if (sector_num >= bs->total_sectors)
1497            return -1;
1498         if (s->qcow) {
1499             int64_t n;
1500             int ret;
1501             ret = bdrv_is_allocated(s->qcow->bs, sector_num * BDRV_SECTOR_SIZE,
1502                                     (nb_sectors - i) * BDRV_SECTOR_SIZE, &n);
1503             if (ret < 0) {
1504                 return ret;
1505             }
1506             if (ret) {
1507                 DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
1508                              " allocated\n", sector_num,
1509                              n >> BDRV_SECTOR_BITS));
1510                 if (bdrv_read(s->qcow, sector_num, buf + i * 0x200,
1511                               n >> BDRV_SECTOR_BITS)) {
1512                     return -1;
1513                 }
1514                 i += (n >> BDRV_SECTOR_BITS) - 1;
1515                 sector_num += (n >> BDRV_SECTOR_BITS) - 1;
1516                 continue;
1517             }
1518             DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n",
1519                          sector_num));
1520         }
1521         if (sector_num < s->offset_to_root_dir) {
1522             if (sector_num < s->offset_to_fat) {
1523                 memcpy(buf + i * 0x200,
1524                        &(s->first_sectors[sector_num * 0x200]),
1525                        0x200);
1526             } else if (sector_num < s->offset_to_fat + s->sectors_per_fat) {
1527                 memcpy(buf + i * 0x200,
1528                        &(s->fat.pointer[(sector_num
1529                                        - s->offset_to_fat) * 0x200]),
1530                        0x200);
1531             } else if (sector_num < s->offset_to_root_dir) {
1532                 memcpy(buf + i * 0x200,
1533                        &(s->fat.pointer[(sector_num - s->offset_to_fat
1534                                        - s->sectors_per_fat) * 0x200]),
1535                        0x200);
1536             }
1537         } else {
1538             uint32_t sector = sector_num - s->offset_to_root_dir,
1539             sector_offset_in_cluster=(sector%s->sectors_per_cluster),
1540             cluster_num=sector/s->sectors_per_cluster;
1541             if(cluster_num > s->cluster_count || read_cluster(s, cluster_num) != 0) {
1542                 /* LATER TODO: strict: return -1; */
1543                 memset(buf+i*0x200,0,0x200);
1544                 continue;
1545             }
1546             memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
1547         }
1548     }
1549     return 0;
1550 }
1551 
1552 static int coroutine_fn
1553 vvfat_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1554                 QEMUIOVector *qiov, int flags)
1555 {
1556     int ret;
1557     BDRVVVFATState *s = bs->opaque;
1558     uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
1559     int nb_sectors = bytes >> BDRV_SECTOR_BITS;
1560     void *buf;
1561 
1562     assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1563     assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1564 
1565     buf = g_try_malloc(bytes);
1566     if (bytes && buf == NULL) {
1567         return -ENOMEM;
1568     }
1569 
1570     qemu_co_mutex_lock(&s->lock);
1571     ret = vvfat_read(bs, sector_num, buf, nb_sectors);
1572     qemu_co_mutex_unlock(&s->lock);
1573 
1574     qemu_iovec_from_buf(qiov, 0, buf, bytes);
1575     g_free(buf);
1576 
1577     return ret;
1578 }
1579 
1580 /* LATER TODO: statify all functions */
1581 
1582 /*
1583  * Idea of the write support (use snapshot):
1584  *
1585  * 1. check if all data is consistent, recording renames, modifications,
1586  *    new files and directories (in s->commits).
1587  *
1588  * 2. if the data is not consistent, stop committing
1589  *
1590  * 3. handle renames, and create new files and directories (do not yet
1591  *    write their contents)
1592  *
1593  * 4. walk the directories, fixing the mapping and direntries, and marking
1594  *    the handled mappings as not deleted
1595  *
1596  * 5. commit the contents of the files
1597  *
1598  * 6. handle deleted files and directories
1599  *
1600  */
1601 
1602 typedef struct commit_t {
1603     char* path;
1604     union {
1605         struct { uint32_t cluster; } rename;
1606         struct { int dir_index; uint32_t modified_offset; } writeout;
1607         struct { uint32_t first_cluster; } new_file;
1608         struct { uint32_t cluster; } mkdir;
1609     } param;
1610     /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1611     enum {
1612         ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR
1613     } action;
1614 } commit_t;
1615 
1616 static void clear_commits(BDRVVVFATState* s)
1617 {
1618     int i;
1619 DLOG(fprintf(stderr, "clear_commits (%d commits)\n", s->commits.next));
1620     for (i = 0; i < s->commits.next; i++) {
1621         commit_t* commit = array_get(&(s->commits), i);
1622         assert(commit->path || commit->action == ACTION_WRITEOUT);
1623         if (commit->action != ACTION_WRITEOUT) {
1624             assert(commit->path);
1625             g_free(commit->path);
1626         } else
1627             assert(commit->path == NULL);
1628     }
1629     s->commits.next = 0;
1630 }
1631 
1632 static void schedule_rename(BDRVVVFATState* s,
1633         uint32_t cluster, char* new_path)
1634 {
1635     commit_t* commit = array_get_next(&(s->commits));
1636     commit->path = new_path;
1637     commit->param.rename.cluster = cluster;
1638     commit->action = ACTION_RENAME;
1639 }
1640 
1641 static void schedule_writeout(BDRVVVFATState* s,
1642         int dir_index, uint32_t modified_offset)
1643 {
1644     commit_t* commit = array_get_next(&(s->commits));
1645     commit->path = NULL;
1646     commit->param.writeout.dir_index = dir_index;
1647     commit->param.writeout.modified_offset = modified_offset;
1648     commit->action = ACTION_WRITEOUT;
1649 }
1650 
1651 static void schedule_new_file(BDRVVVFATState* s,
1652         char* path, uint32_t first_cluster)
1653 {
1654     commit_t* commit = array_get_next(&(s->commits));
1655     commit->path = path;
1656     commit->param.new_file.first_cluster = first_cluster;
1657     commit->action = ACTION_NEW_FILE;
1658 }
1659 
1660 static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path)
1661 {
1662     commit_t* commit = array_get_next(&(s->commits));
1663     commit->path = path;
1664     commit->param.mkdir.cluster = cluster;
1665     commit->action = ACTION_MKDIR;
1666 }
1667 
1668 typedef struct {
1669     /*
1670      * Since the sequence number is at most 0x3f, and the filename
1671      * length is at most 13 times the sequence number, the maximal
1672      * filename length is 0x3f * 13 bytes.
1673      */
1674     unsigned char name[0x3f * 13 + 1];
1675     gunichar2 name2[0x3f * 13 + 1];
1676     int checksum, len;
1677     int sequence_number;
1678 } long_file_name;
1679 
1680 static void lfn_init(long_file_name* lfn)
1681 {
1682    lfn->sequence_number = lfn->len = 0;
1683    lfn->checksum = 0x100;
1684 }
1685 
1686 /* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1687 static int parse_long_name(long_file_name* lfn,
1688         const direntry_t* direntry)
1689 {
1690     int i, j, offset;
1691     const unsigned char* pointer = (const unsigned char*)direntry;
1692 
1693     if (!is_long_name(direntry))
1694         return 1;
1695 
1696     if (pointer[0] & 0x40) {
1697         /* first entry; do some initialization */
1698         lfn->sequence_number = pointer[0] & 0x3f;
1699         lfn->checksum = pointer[13];
1700         lfn->name[0] = 0;
1701         lfn->name[lfn->sequence_number * 13] = 0;
1702     } else if ((pointer[0] & 0x3f) != --lfn->sequence_number) {
1703         /* not the expected sequence number */
1704         return -1;
1705     } else if (pointer[13] != lfn->checksum) {
1706         /* not the expected checksum */
1707         return -2;
1708     } else if (pointer[12] || pointer[26] || pointer[27]) {
1709         /* invalid zero fields */
1710         return -3;
1711     }
1712 
1713     offset = 13 * (lfn->sequence_number - 1);
1714     for (i = 0, j = 1; i < 13; i++, j+=2) {
1715         if (j == 11)
1716             j = 14;
1717         else if (j == 26)
1718             j = 28;
1719 
1720         if (pointer[j] == 0 && pointer[j + 1] == 0) {
1721             /* end of long file name */
1722             break;
1723         }
1724         gunichar2 c = (pointer[j + 1] << 8) + pointer[j];
1725         lfn->name2[offset + i] = c;
1726     }
1727 
1728     if (pointer[0] & 0x40) {
1729         /* first entry; set len */
1730         lfn->len = offset + i;
1731     }
1732     if ((pointer[0] & 0x3f) == 0x01) {
1733         /* last entry; finalize entry */
1734         glong olen;
1735         gchar *utf8 = g_utf16_to_utf8(lfn->name2, lfn->len, NULL, &olen, NULL);
1736         if (!utf8) {
1737             return -4;
1738         }
1739         lfn->len = olen;
1740         memcpy(lfn->name, utf8, olen + 1);
1741         g_free(utf8);
1742     }
1743 
1744     return 0;
1745 }
1746 
1747 /* returns 0 if successful, >0 if no short_name, and <0 on error */
1748 static int parse_short_name(BDRVVVFATState* s,
1749         long_file_name* lfn, direntry_t* direntry)
1750 {
1751     int i, j;
1752 
1753     if (!is_short_name(direntry))
1754         return 1;
1755 
1756     for (j = 7; j >= 0 && direntry->name[j] == ' '; j--);
1757     for (i = 0; i <= j; i++) {
1758         uint8_t c = direntry->name[i];
1759         if (c != to_valid_short_char(c)) {
1760             return -1;
1761         } else if (s->downcase_short_names) {
1762             lfn->name[i] = qemu_tolower(direntry->name[i]);
1763         } else {
1764             lfn->name[i] = direntry->name[i];
1765         }
1766     }
1767 
1768     for (j = 2; j >= 0 && direntry->name[8 + j] == ' '; j--) {
1769     }
1770     if (j >= 0) {
1771         lfn->name[i++] = '.';
1772         lfn->name[i + j + 1] = '\0';
1773         for (;j >= 0; j--) {
1774             uint8_t c = direntry->name[8 + j];
1775             if (c != to_valid_short_char(c)) {
1776                 return -2;
1777             } else if (s->downcase_short_names) {
1778                 lfn->name[i + j] = qemu_tolower(c);
1779             } else {
1780                 lfn->name[i + j] = c;
1781             }
1782         }
1783     } else
1784         lfn->name[i + j + 1] = '\0';
1785 
1786     if (lfn->name[0] == DIR_KANJI_FAKE) {
1787         lfn->name[0] = DIR_KANJI;
1788     }
1789     lfn->len = strlen((char*)lfn->name);
1790 
1791     return 0;
1792 }
1793 
1794 static inline uint32_t modified_fat_get(BDRVVVFATState* s,
1795         unsigned int cluster)
1796 {
1797     if (cluster < s->last_cluster_of_root_directory) {
1798         if (cluster + 1 == s->last_cluster_of_root_directory)
1799             return s->max_fat_value;
1800         else
1801             return cluster + 1;
1802     }
1803 
1804     if (s->fat_type==32) {
1805         uint32_t* entry=((uint32_t*)s->fat2)+cluster;
1806         return le32_to_cpu(*entry);
1807     } else if (s->fat_type==16) {
1808         uint16_t* entry=((uint16_t*)s->fat2)+cluster;
1809         return le16_to_cpu(*entry);
1810     } else {
1811         const uint8_t* x=s->fat2+cluster*3/2;
1812         return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
1813     }
1814 }
1815 
1816 static inline bool cluster_was_modified(BDRVVVFATState *s,
1817                                         uint32_t cluster_num)
1818 {
1819     int was_modified = 0;
1820     int i;
1821 
1822     if (s->qcow == NULL) {
1823         return 0;
1824     }
1825 
1826     for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) {
1827         was_modified = bdrv_is_allocated(s->qcow->bs,
1828                                          (cluster2sector(s, cluster_num) +
1829                                           i) * BDRV_SECTOR_SIZE,
1830                                          BDRV_SECTOR_SIZE, NULL);
1831     }
1832 
1833     /*
1834      * Note that this treats failures to learn allocation status the
1835      * same as if an allocation has occurred.  It's as safe as
1836      * anything else, given that a failure to learn allocation status
1837      * will probably result in more failures.
1838      */
1839     return !!was_modified;
1840 }
1841 
1842 static const char* get_basename(const char* path)
1843 {
1844     char* basename = strrchr(path, '/');
1845     if (basename == NULL)
1846         return path;
1847     else
1848         return basename + 1; /* strip '/' */
1849 }
1850 
1851 /*
1852  * The array s->used_clusters holds the states of the clusters. If it is
1853  * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1854  * was modified, bit 3 is set.
1855  * If any cluster is allocated, but not part of a file or directory, this
1856  * driver refuses to commit.
1857  */
1858 typedef enum {
1859      USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4
1860 } used_t;
1861 
1862 /*
1863  * get_cluster_count_for_direntry() not only determines how many clusters
1864  * are occupied by direntry, but also if it was renamed or modified.
1865  *
1866  * A file is thought to be renamed *only* if there already was a file with
1867  * exactly the same first cluster, but a different name.
1868  *
1869  * Further, the files/directories handled by this function are
1870  * assumed to be *not* deleted (and *only* those).
1871  */
1872 static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
1873         direntry_t* direntry, const char* path)
1874 {
1875     /*
1876      * This is a little bit tricky:
1877      * IF the guest OS just inserts a cluster into the file chain,
1878      * and leaves the rest alone, (i.e. the original file had clusters
1879      * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1880      *
1881      * - do_commit will write the cluster into the file at the given
1882      *   offset, but
1883      *
1884      * - the cluster which is overwritten should be moved to a later
1885      *   position in the file.
1886      *
1887      * I am not aware that any OS does something as braindead, but this
1888      * situation could happen anyway when not committing for a long time.
1889      * Just to be sure that this does not bite us, detect it, and copy the
1890      * contents of the clusters to-be-overwritten into the qcow.
1891      */
1892     int copy_it = 0;
1893     int was_modified = 0;
1894     int32_t ret = 0;
1895 
1896     uint32_t cluster_num = begin_of_direntry(direntry);
1897     uint32_t offset = 0;
1898     int first_mapping_index = -1;
1899     mapping_t* mapping = NULL;
1900     const char* basename2 = NULL;
1901 
1902     vvfat_close_current_file(s);
1903 
1904     /* the root directory */
1905     if (cluster_num == 0)
1906         return 0;
1907 
1908     /* write support */
1909     if (s->qcow) {
1910         basename2 = get_basename(path);
1911 
1912         mapping = find_mapping_for_cluster(s, cluster_num);
1913 
1914         if (mapping) {
1915             const char* basename;
1916 
1917             assert(mapping->mode & MODE_DELETED);
1918             mapping->mode &= ~MODE_DELETED;
1919 
1920             basename = get_basename(mapping->path);
1921 
1922             assert(mapping->mode & MODE_NORMAL);
1923 
1924             /* rename */
1925             if (strcmp(basename, basename2))
1926                 schedule_rename(s, cluster_num, g_strdup(path));
1927         } else if (is_file(direntry))
1928             /* new file */
1929             schedule_new_file(s, g_strdup(path), cluster_num);
1930         else {
1931             abort();
1932             return 0;
1933         }
1934     }
1935 
1936     while(1) {
1937         if (s->qcow) {
1938             if (!copy_it && cluster_was_modified(s, cluster_num)) {
1939                 if (mapping == NULL ||
1940                         mapping->begin > cluster_num ||
1941                         mapping->end <= cluster_num)
1942                 mapping = find_mapping_for_cluster(s, cluster_num);
1943 
1944 
1945                 if (mapping &&
1946                         (mapping->mode & MODE_DIRECTORY) == 0) {
1947 
1948                     /* was modified in qcow */
1949                     if (offset != mapping->info.file.offset + s->cluster_size
1950                             * (cluster_num - mapping->begin)) {
1951                         /* offset of this cluster in file chain has changed */
1952                         abort();
1953                         copy_it = 1;
1954                     } else if (offset == 0) {
1955                         const char* basename = get_basename(mapping->path);
1956 
1957                         if (strcmp(basename, basename2))
1958                             copy_it = 1;
1959                         first_mapping_index = array_index(&(s->mapping), mapping);
1960                     }
1961 
1962                     if (mapping->first_mapping_index != first_mapping_index
1963                             && mapping->info.file.offset > 0) {
1964                         abort();
1965                         copy_it = 1;
1966                     }
1967 
1968                     /* need to write out? */
1969                     if (!was_modified && is_file(direntry)) {
1970                         was_modified = 1;
1971                         schedule_writeout(s, mapping->dir_index, offset);
1972                     }
1973                 }
1974             }
1975 
1976             if (copy_it) {
1977                 int i;
1978                 /*
1979                  * This is horribly inefficient, but that is okay, since
1980                  * it is rarely executed, if at all.
1981                  */
1982                 int64_t offset = cluster2sector(s, cluster_num);
1983 
1984                 vvfat_close_current_file(s);
1985                 for (i = 0; i < s->sectors_per_cluster; i++) {
1986                     int res;
1987 
1988                     res = bdrv_is_allocated(s->qcow->bs,
1989                                             (offset + i) * BDRV_SECTOR_SIZE,
1990                                             BDRV_SECTOR_SIZE, NULL);
1991                     if (res < 0) {
1992                         return -1;
1993                     }
1994                     if (!res) {
1995                         res = vvfat_read(s->bs, offset, s->cluster_buffer, 1);
1996                         if (res) {
1997                             return -1;
1998                         }
1999                         res = bdrv_write(s->qcow, offset, s->cluster_buffer, 1);
2000                         if (res) {
2001                             return -2;
2002                         }
2003                     }
2004                 }
2005             }
2006         }
2007 
2008         ret++;
2009         if (s->used_clusters[cluster_num] & USED_ANY)
2010             return 0;
2011         s->used_clusters[cluster_num] = USED_FILE;
2012 
2013         cluster_num = modified_fat_get(s, cluster_num);
2014 
2015         if (fat_eof(s, cluster_num))
2016             return ret;
2017         else if (cluster_num < 2 || cluster_num > s->max_fat_value - 16)
2018             return -1;
2019 
2020         offset += s->cluster_size;
2021     }
2022 }
2023 
2024 /*
2025  * This function looks at the modified data (qcow).
2026  * It returns 0 upon inconsistency or error, and the number of clusters
2027  * used by the directory, its subdirectories and their files.
2028  */
2029 static int check_directory_consistency(BDRVVVFATState *s,
2030         int cluster_num, const char* path)
2031 {
2032     int ret = 0;
2033     unsigned char* cluster = g_malloc(s->cluster_size);
2034     direntry_t* direntries = (direntry_t*)cluster;
2035     mapping_t* mapping = find_mapping_for_cluster(s, cluster_num);
2036 
2037     long_file_name lfn;
2038     int path_len = strlen(path);
2039     char path2[PATH_MAX + 1];
2040 
2041     assert(path_len < PATH_MAX); /* len was tested before! */
2042     pstrcpy(path2, sizeof(path2), path);
2043     path2[path_len] = '/';
2044     path2[path_len + 1] = '\0';
2045 
2046     if (mapping) {
2047         const char* basename = get_basename(mapping->path);
2048         const char* basename2 = get_basename(path);
2049 
2050         assert(mapping->mode & MODE_DIRECTORY);
2051 
2052         assert(mapping->mode & MODE_DELETED);
2053         mapping->mode &= ~MODE_DELETED;
2054 
2055         if (strcmp(basename, basename2))
2056             schedule_rename(s, cluster_num, g_strdup(path));
2057     } else
2058         /* new directory */
2059         schedule_mkdir(s, cluster_num, g_strdup(path));
2060 
2061     lfn_init(&lfn);
2062     do {
2063         int i;
2064         int subret = 0;
2065 
2066         ret++;
2067 
2068         if (s->used_clusters[cluster_num] & USED_ANY) {
2069             fprintf(stderr, "cluster %d used more than once\n", (int)cluster_num);
2070             goto fail;
2071         }
2072         s->used_clusters[cluster_num] = USED_DIRECTORY;
2073 
2074 DLOG(fprintf(stderr, "read cluster %d (sector %d)\n", (int)cluster_num, (int)cluster2sector(s, cluster_num)));
2075         subret = vvfat_read(s->bs, cluster2sector(s, cluster_num), cluster,
2076                 s->sectors_per_cluster);
2077         if (subret) {
2078             fprintf(stderr, "Error fetching direntries\n");
2079         fail:
2080             g_free(cluster);
2081             return 0;
2082         }
2083 
2084         for (i = 0; i < 0x10 * s->sectors_per_cluster; i++) {
2085             int cluster_count = 0;
2086 
2087 DLOG(fprintf(stderr, "check direntry %d:\n", i); print_direntry(direntries + i));
2088             if (is_volume_label(direntries + i) || is_dot(direntries + i) ||
2089                     is_free(direntries + i))
2090                 continue;
2091 
2092             subret = parse_long_name(&lfn, direntries + i);
2093             if (subret < 0) {
2094                 fprintf(stderr, "Error in long name\n");
2095                 goto fail;
2096             }
2097             if (subret == 0 || is_free(direntries + i))
2098                 continue;
2099 
2100             if (fat_chksum(direntries+i) != lfn.checksum) {
2101                 subret = parse_short_name(s, &lfn, direntries + i);
2102                 if (subret < 0) {
2103                     fprintf(stderr, "Error in short name (%d)\n", subret);
2104                     goto fail;
2105                 }
2106                 if (subret > 0 || !strcmp((char*)lfn.name, ".")
2107                         || !strcmp((char*)lfn.name, ".."))
2108                     continue;
2109             }
2110             lfn.checksum = 0x100; /* cannot use long name twice */
2111 
2112             if (path_len + 1 + lfn.len >= PATH_MAX) {
2113                 fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name);
2114                 goto fail;
2115             }
2116             pstrcpy(path2 + path_len + 1, sizeof(path2) - path_len - 1,
2117                     (char*)lfn.name);
2118 
2119             if (is_directory(direntries + i)) {
2120                 if (begin_of_direntry(direntries + i) == 0) {
2121                     DLOG(fprintf(stderr, "invalid begin for directory: %s\n", path2); print_direntry(direntries + i));
2122                     goto fail;
2123                 }
2124                 cluster_count = check_directory_consistency(s,
2125                         begin_of_direntry(direntries + i), path2);
2126                 if (cluster_count == 0) {
2127                     DLOG(fprintf(stderr, "problem in directory %s:\n", path2); print_direntry(direntries + i));
2128                     goto fail;
2129                 }
2130             } else if (is_file(direntries + i)) {
2131                 /* check file size with FAT */
2132                 cluster_count = get_cluster_count_for_direntry(s, direntries + i, path2);
2133                 if (cluster_count !=
2134             DIV_ROUND_UP(le32_to_cpu(direntries[i].size), s->cluster_size)) {
2135                     DLOG(fprintf(stderr, "Cluster count mismatch\n"));
2136                     goto fail;
2137                 }
2138             } else
2139                 abort(); /* cluster_count = 0; */
2140 
2141             ret += cluster_count;
2142         }
2143 
2144         cluster_num = modified_fat_get(s, cluster_num);
2145     } while(!fat_eof(s, cluster_num));
2146 
2147     g_free(cluster);
2148     return ret;
2149 }
2150 
2151 /* returns 1 on success */
2152 static int is_consistent(BDRVVVFATState* s)
2153 {
2154     int i, check;
2155     int used_clusters_count = 0;
2156 
2157 DLOG(checkpoint());
2158     /*
2159      * - get modified FAT
2160      * - compare the two FATs (TODO)
2161      * - get buffer for marking used clusters
2162      * - recurse direntries from root (using bs->bdrv_read to make
2163      *    sure to get the new data)
2164      *   - check that the FAT agrees with the size
2165      *   - count the number of clusters occupied by this directory and
2166      *     its files
2167      * - check that the cumulative used cluster count agrees with the
2168      *   FAT
2169      * - if all is fine, return number of used clusters
2170      */
2171     if (s->fat2 == NULL) {
2172         int size = 0x200 * s->sectors_per_fat;
2173         s->fat2 = g_malloc(size);
2174         memcpy(s->fat2, s->fat.pointer, size);
2175     }
2176     check = vvfat_read(s->bs,
2177             s->offset_to_fat, s->fat2, s->sectors_per_fat);
2178     if (check) {
2179         fprintf(stderr, "Could not copy fat\n");
2180         return 0;
2181     }
2182     assert (s->used_clusters);
2183     for (i = 0; i < sector2cluster(s, s->sector_count); i++)
2184         s->used_clusters[i] &= ~USED_ANY;
2185 
2186     clear_commits(s);
2187 
2188     /* mark every mapped file/directory as deleted.
2189      * (check_directory_consistency() will unmark those still present). */
2190     if (s->qcow)
2191         for (i = 0; i < s->mapping.next; i++) {
2192             mapping_t* mapping = array_get(&(s->mapping), i);
2193             if (mapping->first_mapping_index < 0)
2194                 mapping->mode |= MODE_DELETED;
2195         }
2196 
2197     used_clusters_count = check_directory_consistency(s, 0, s->path);
2198     if (used_clusters_count <= 0) {
2199         DLOG(fprintf(stderr, "problem in directory\n"));
2200         return 0;
2201     }
2202 
2203     check = s->last_cluster_of_root_directory;
2204     for (i = check; i < sector2cluster(s, s->sector_count); i++) {
2205         if (modified_fat_get(s, i)) {
2206             if(!s->used_clusters[i]) {
2207                 DLOG(fprintf(stderr, "FAT was modified (%d), but cluster is not used?\n", i));
2208                 return 0;
2209             }
2210             check++;
2211         }
2212 
2213         if (s->used_clusters[i] == USED_ALLOCATED) {
2214             /* allocated, but not used... */
2215             DLOG(fprintf(stderr, "unused, modified cluster: %d\n", i));
2216             return 0;
2217         }
2218     }
2219 
2220     if (check != used_clusters_count)
2221         return 0;
2222 
2223     return used_clusters_count;
2224 }
2225 
2226 static inline void adjust_mapping_indices(BDRVVVFATState* s,
2227         int offset, int adjust)
2228 {
2229     int i;
2230 
2231     for (i = 0; i < s->mapping.next; i++) {
2232         mapping_t* mapping = array_get(&(s->mapping), i);
2233 
2234 #define ADJUST_MAPPING_INDEX(name) \
2235         if (mapping->name >= offset) \
2236             mapping->name += adjust
2237 
2238         ADJUST_MAPPING_INDEX(first_mapping_index);
2239         if (mapping->mode & MODE_DIRECTORY)
2240             ADJUST_MAPPING_INDEX(info.dir.parent_mapping_index);
2241     }
2242 }
2243 
2244 /* insert or update mapping */
2245 static mapping_t* insert_mapping(BDRVVVFATState* s,
2246         uint32_t begin, uint32_t end)
2247 {
2248     /*
2249      * - find mapping where mapping->begin >= begin,
2250      * - if mapping->begin > begin: insert
2251      *   - adjust all references to mappings!
2252      * - else: adjust
2253      * - replace name
2254      */
2255     int index = find_mapping_for_cluster_aux(s, begin, 0, s->mapping.next);
2256     mapping_t* mapping = NULL;
2257     mapping_t* first_mapping = array_get(&(s->mapping), 0);
2258 
2259     if (index < s->mapping.next && (mapping = array_get(&(s->mapping), index))
2260             && mapping->begin < begin) {
2261         mapping->end = begin;
2262         index++;
2263         mapping = array_get(&(s->mapping), index);
2264     }
2265     if (index >= s->mapping.next || mapping->begin > begin) {
2266         mapping = array_insert(&(s->mapping), index, 1);
2267         mapping->path = NULL;
2268         adjust_mapping_indices(s, index, +1);
2269     }
2270 
2271     mapping->begin = begin;
2272     mapping->end = end;
2273 
2274 DLOG(mapping_t* next_mapping;
2275 assert(index + 1 >= s->mapping.next ||
2276 ((next_mapping = array_get(&(s->mapping), index + 1)) &&
2277  next_mapping->begin >= end)));
2278 
2279     if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
2280         s->current_mapping = array_get(&(s->mapping),
2281                 s->current_mapping - first_mapping);
2282 
2283     return mapping;
2284 }
2285 
2286 static int remove_mapping(BDRVVVFATState* s, int mapping_index)
2287 {
2288     mapping_t* mapping = array_get(&(s->mapping), mapping_index);
2289     mapping_t* first_mapping = array_get(&(s->mapping), 0);
2290 
2291     /* free mapping */
2292     if (mapping->first_mapping_index < 0) {
2293         g_free(mapping->path);
2294     }
2295 
2296     /* remove from s->mapping */
2297     array_remove(&(s->mapping), mapping_index);
2298 
2299     /* adjust all references to mappings */
2300     adjust_mapping_indices(s, mapping_index, -1);
2301 
2302     if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
2303         s->current_mapping = array_get(&(s->mapping),
2304                 s->current_mapping - first_mapping);
2305 
2306     return 0;
2307 }
2308 
2309 static void adjust_dirindices(BDRVVVFATState* s, int offset, int adjust)
2310 {
2311     int i;
2312     for (i = 0; i < s->mapping.next; i++) {
2313         mapping_t* mapping = array_get(&(s->mapping), i);
2314         if (mapping->dir_index >= offset)
2315             mapping->dir_index += adjust;
2316         if ((mapping->mode & MODE_DIRECTORY) &&
2317                 mapping->info.dir.first_dir_index >= offset)
2318             mapping->info.dir.first_dir_index += adjust;
2319     }
2320 }
2321 
2322 static direntry_t* insert_direntries(BDRVVVFATState* s,
2323         int dir_index, int count)
2324 {
2325     /*
2326      * make room in s->directory,
2327      * adjust_dirindices
2328      */
2329     direntry_t* result = array_insert(&(s->directory), dir_index, count);
2330     if (result == NULL)
2331         return NULL;
2332     adjust_dirindices(s, dir_index, count);
2333     return result;
2334 }
2335 
2336 static int remove_direntries(BDRVVVFATState* s, int dir_index, int count)
2337 {
2338     int ret = array_remove_slice(&(s->directory), dir_index, count);
2339     if (ret)
2340         return ret;
2341     adjust_dirindices(s, dir_index, -count);
2342     return 0;
2343 }
2344 
2345 /*
2346  * Adapt the mappings of the cluster chain starting at first cluster
2347  * (i.e. if a file starts at first_cluster, the chain is followed according
2348  * to the modified fat, and the corresponding entries in s->mapping are
2349  * adjusted)
2350  */
2351 static int commit_mappings(BDRVVVFATState* s,
2352         uint32_t first_cluster, int dir_index)
2353 {
2354     mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
2355     direntry_t* direntry = array_get(&(s->directory), dir_index);
2356     uint32_t cluster = first_cluster;
2357 
2358     vvfat_close_current_file(s);
2359 
2360     assert(mapping);
2361     assert(mapping->begin == first_cluster);
2362     mapping->first_mapping_index = -1;
2363     mapping->dir_index = dir_index;
2364     mapping->mode = (dir_index <= 0 || is_directory(direntry)) ?
2365         MODE_DIRECTORY : MODE_NORMAL;
2366 
2367     while (!fat_eof(s, cluster)) {
2368         uint32_t c, c1;
2369 
2370         for (c = cluster, c1 = modified_fat_get(s, c); c + 1 == c1;
2371                 c = c1, c1 = modified_fat_get(s, c1));
2372 
2373         c++;
2374         if (c > mapping->end) {
2375             int index = array_index(&(s->mapping), mapping);
2376             int i, max_i = s->mapping.next - index;
2377             for (i = 1; i < max_i && mapping[i].begin < c; i++);
2378             while (--i > 0)
2379                 remove_mapping(s, index + 1);
2380         }
2381         assert(mapping == array_get(&(s->mapping), s->mapping.next - 1)
2382                 || mapping[1].begin >= c);
2383         mapping->end = c;
2384 
2385         if (!fat_eof(s, c1)) {
2386             int i = find_mapping_for_cluster_aux(s, c1, 0, s->mapping.next);
2387             mapping_t* next_mapping = i >= s->mapping.next ? NULL :
2388                 array_get(&(s->mapping), i);
2389 
2390             if (next_mapping == NULL || next_mapping->begin > c1) {
2391                 int i1 = array_index(&(s->mapping), mapping);
2392 
2393                 next_mapping = insert_mapping(s, c1, c1+1);
2394 
2395                 if (c1 < c)
2396                     i1++;
2397                 mapping = array_get(&(s->mapping), i1);
2398             }
2399 
2400             next_mapping->dir_index = mapping->dir_index;
2401             next_mapping->first_mapping_index =
2402                 mapping->first_mapping_index < 0 ?
2403                 array_index(&(s->mapping), mapping) :
2404                 mapping->first_mapping_index;
2405             next_mapping->path = mapping->path;
2406             next_mapping->mode = mapping->mode;
2407             next_mapping->read_only = mapping->read_only;
2408             if (mapping->mode & MODE_DIRECTORY) {
2409                 next_mapping->info.dir.parent_mapping_index =
2410                         mapping->info.dir.parent_mapping_index;
2411                 next_mapping->info.dir.first_dir_index =
2412                         mapping->info.dir.first_dir_index +
2413                         0x10 * s->sectors_per_cluster *
2414                         (mapping->end - mapping->begin);
2415             } else
2416                 next_mapping->info.file.offset = mapping->info.file.offset +
2417                         mapping->end - mapping->begin;
2418 
2419             mapping = next_mapping;
2420         }
2421 
2422         cluster = c1;
2423     }
2424 
2425     return 0;
2426 }
2427 
2428 static int commit_direntries(BDRVVVFATState* s,
2429         int dir_index, int parent_mapping_index)
2430 {
2431     direntry_t* direntry = array_get(&(s->directory), dir_index);
2432     uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry(direntry);
2433     mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
2434 
2435     int factor = 0x10 * s->sectors_per_cluster;
2436     int old_cluster_count, new_cluster_count;
2437     int current_dir_index = mapping->info.dir.first_dir_index;
2438     int first_dir_index = current_dir_index;
2439     int ret, i;
2440     uint32_t c;
2441 
2442 DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n", mapping->path, parent_mapping_index));
2443 
2444     assert(direntry);
2445     assert(mapping);
2446     assert(mapping->begin == first_cluster);
2447     assert(mapping->info.dir.first_dir_index < s->directory.next);
2448     assert(mapping->mode & MODE_DIRECTORY);
2449     assert(dir_index == 0 || is_directory(direntry));
2450 
2451     mapping->info.dir.parent_mapping_index = parent_mapping_index;
2452 
2453     if (first_cluster == 0) {
2454         old_cluster_count = new_cluster_count =
2455             s->last_cluster_of_root_directory;
2456     } else {
2457         for (old_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2458                 c = fat_get(s, c))
2459             old_cluster_count++;
2460 
2461         for (new_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
2462                 c = modified_fat_get(s, c))
2463             new_cluster_count++;
2464     }
2465 
2466     if (new_cluster_count > old_cluster_count) {
2467         if (insert_direntries(s,
2468                 current_dir_index + factor * old_cluster_count,
2469                 factor * (new_cluster_count - old_cluster_count)) == NULL)
2470             return -1;
2471     } else if (new_cluster_count < old_cluster_count)
2472         remove_direntries(s,
2473                 current_dir_index + factor * new_cluster_count,
2474                 factor * (old_cluster_count - new_cluster_count));
2475 
2476     for (c = first_cluster; !fat_eof(s, c); c = modified_fat_get(s, c)) {
2477         direntry_t *first_direntry;
2478         void* direntry = array_get(&(s->directory), current_dir_index);
2479         int ret = vvfat_read(s->bs, cluster2sector(s, c), direntry,
2480                 s->sectors_per_cluster);
2481         if (ret)
2482             return ret;
2483 
2484         /* The first directory entry on the filesystem is the volume name */
2485         first_direntry = (direntry_t*) s->directory.pointer;
2486         assert(!memcmp(first_direntry->name, s->volume_label, 11));
2487 
2488         current_dir_index += factor;
2489     }
2490 
2491     ret = commit_mappings(s, first_cluster, dir_index);
2492     if (ret)
2493         return ret;
2494 
2495     /* recurse */
2496     for (i = 0; i < factor * new_cluster_count; i++) {
2497         direntry = array_get(&(s->directory), first_dir_index + i);
2498         if (is_directory(direntry) && !is_dot(direntry)) {
2499             mapping = find_mapping_for_cluster(s, first_cluster);
2500             assert(mapping->mode & MODE_DIRECTORY);
2501             ret = commit_direntries(s, first_dir_index + i,
2502                 array_index(&(s->mapping), mapping));
2503             if (ret)
2504                 return ret;
2505         }
2506     }
2507 
2508     return 0;
2509 }
2510 
2511 /* commit one file (adjust contents, adjust mapping),
2512    return first_mapping_index */
2513 static int commit_one_file(BDRVVVFATState* s,
2514         int dir_index, uint32_t offset)
2515 {
2516     direntry_t* direntry = array_get(&(s->directory), dir_index);
2517     uint32_t c = begin_of_direntry(direntry);
2518     uint32_t first_cluster = c;
2519     mapping_t* mapping = find_mapping_for_cluster(s, c);
2520     uint32_t size = filesize_of_direntry(direntry);
2521     char* cluster = g_malloc(s->cluster_size);
2522     uint32_t i;
2523     int fd = 0;
2524 
2525     assert(offset < size);
2526     assert((offset % s->cluster_size) == 0);
2527 
2528     for (i = s->cluster_size; i < offset; i += s->cluster_size)
2529         c = modified_fat_get(s, c);
2530 
2531     fd = qemu_open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
2532     if (fd < 0) {
2533         fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,
2534                 strerror(errno), errno);
2535         g_free(cluster);
2536         return fd;
2537     }
2538     if (offset > 0) {
2539         if (lseek(fd, offset, SEEK_SET) != offset) {
2540             qemu_close(fd);
2541             g_free(cluster);
2542             return -3;
2543         }
2544     }
2545 
2546     while (offset < size) {
2547         uint32_t c1;
2548         int rest_size = (size - offset > s->cluster_size ?
2549                 s->cluster_size : size - offset);
2550         int ret;
2551 
2552         c1 = modified_fat_get(s, c);
2553 
2554         assert((size - offset == 0 && fat_eof(s, c)) ||
2555                 (size > offset && c >=2 && !fat_eof(s, c)));
2556 
2557         ret = vvfat_read(s->bs, cluster2sector(s, c),
2558             (uint8_t*)cluster, DIV_ROUND_UP(rest_size, 0x200));
2559 
2560         if (ret < 0) {
2561             qemu_close(fd);
2562             g_free(cluster);
2563             return ret;
2564         }
2565 
2566         if (write(fd, cluster, rest_size) < 0) {
2567             qemu_close(fd);
2568             g_free(cluster);
2569             return -2;
2570         }
2571 
2572         offset += rest_size;
2573         c = c1;
2574     }
2575 
2576     if (ftruncate(fd, size)) {
2577         perror("ftruncate()");
2578         qemu_close(fd);
2579         g_free(cluster);
2580         return -4;
2581     }
2582     qemu_close(fd);
2583     g_free(cluster);
2584 
2585     return commit_mappings(s, first_cluster, dir_index);
2586 }
2587 
2588 #ifdef DEBUG
2589 /* test, if all mappings point to valid direntries */
2590 static void check1(BDRVVVFATState* s)
2591 {
2592     int i;
2593     for (i = 0; i < s->mapping.next; i++) {
2594         mapping_t* mapping = array_get(&(s->mapping), i);
2595         if (mapping->mode & MODE_DELETED) {
2596             fprintf(stderr, "deleted\n");
2597             continue;
2598         }
2599         assert(mapping->dir_index < s->directory.next);
2600         direntry_t* direntry = array_get(&(s->directory), mapping->dir_index);
2601         assert(mapping->begin == begin_of_direntry(direntry) || mapping->first_mapping_index >= 0);
2602         if (mapping->mode & MODE_DIRECTORY) {
2603             assert(mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * (mapping->end - mapping->begin) <= s->directory.next);
2604             assert((mapping->info.dir.first_dir_index % (0x10 * s->sectors_per_cluster)) == 0);
2605         }
2606     }
2607 }
2608 
2609 /* test, if all direntries have mappings */
2610 static void check2(BDRVVVFATState* s)
2611 {
2612     int i;
2613     int first_mapping = -1;
2614 
2615     for (i = 0; i < s->directory.next; i++) {
2616         direntry_t* direntry = array_get(&(s->directory), i);
2617 
2618         if (is_short_name(direntry) && begin_of_direntry(direntry)) {
2619             mapping_t* mapping = find_mapping_for_cluster(s, begin_of_direntry(direntry));
2620             assert(mapping);
2621             assert(mapping->dir_index == i || is_dot(direntry));
2622             assert(mapping->begin == begin_of_direntry(direntry) || is_dot(direntry));
2623         }
2624 
2625         if ((i % (0x10 * s->sectors_per_cluster)) == 0) {
2626             /* cluster start */
2627             int j, count = 0;
2628 
2629             for (j = 0; j < s->mapping.next; j++) {
2630                 mapping_t* mapping = array_get(&(s->mapping), j);
2631                 if (mapping->mode & MODE_DELETED)
2632                     continue;
2633                 if (mapping->mode & MODE_DIRECTORY) {
2634                     if (mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i) {
2635                         assert(++count == 1);
2636                         if (mapping->first_mapping_index == -1)
2637                             first_mapping = array_index(&(s->mapping), mapping);
2638                         else
2639                             assert(first_mapping == mapping->first_mapping_index);
2640                         if (mapping->info.dir.parent_mapping_index < 0)
2641                             assert(j == 0);
2642                         else {
2643                             mapping_t* parent = array_get(&(s->mapping), mapping->info.dir.parent_mapping_index);
2644                             assert(parent->mode & MODE_DIRECTORY);
2645                             assert(parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index);
2646                         }
2647                     }
2648                 }
2649             }
2650             if (count == 0)
2651                 first_mapping = -1;
2652         }
2653     }
2654 }
2655 #endif
2656 
2657 static int handle_renames_and_mkdirs(BDRVVVFATState* s)
2658 {
2659     int i;
2660 
2661 #ifdef DEBUG
2662     fprintf(stderr, "handle_renames\n");
2663     for (i = 0; i < s->commits.next; i++) {
2664         commit_t* commit = array_get(&(s->commits), i);
2665         fprintf(stderr, "%d, %s (%d, %d)\n", i, commit->path ? commit->path : "(null)", commit->param.rename.cluster, commit->action);
2666     }
2667 #endif
2668 
2669     for (i = 0; i < s->commits.next;) {
2670         commit_t* commit = array_get(&(s->commits), i);
2671         if (commit->action == ACTION_RENAME) {
2672             mapping_t* mapping = find_mapping_for_cluster(s,
2673                     commit->param.rename.cluster);
2674             char* old_path = mapping->path;
2675 
2676             assert(commit->path);
2677             mapping->path = commit->path;
2678             if (rename(old_path, mapping->path))
2679                 return -2;
2680 
2681             if (mapping->mode & MODE_DIRECTORY) {
2682                 int l1 = strlen(mapping->path);
2683                 int l2 = strlen(old_path);
2684                 int diff = l1 - l2;
2685                 direntry_t* direntry = array_get(&(s->directory),
2686                         mapping->info.dir.first_dir_index);
2687                 uint32_t c = mapping->begin;
2688                 int i = 0;
2689 
2690                 /* recurse */
2691                 while (!fat_eof(s, c)) {
2692                     do {
2693                         direntry_t* d = direntry + i;
2694 
2695                         if (is_file(d) || (is_directory(d) && !is_dot(d))) {
2696                             mapping_t* m = find_mapping_for_cluster(s,
2697                                     begin_of_direntry(d));
2698                             int l = strlen(m->path);
2699                             char* new_path = g_malloc(l + diff + 1);
2700 
2701                             assert(!strncmp(m->path, mapping->path, l2));
2702 
2703                             pstrcpy(new_path, l + diff + 1, mapping->path);
2704                             pstrcpy(new_path + l1, l + diff + 1 - l1,
2705                                     m->path + l2);
2706 
2707                             schedule_rename(s, m->begin, new_path);
2708                         }
2709                         i++;
2710                     } while((i % (0x10 * s->sectors_per_cluster)) != 0);
2711                     c = fat_get(s, c);
2712                 }
2713             }
2714 
2715             g_free(old_path);
2716             array_remove(&(s->commits), i);
2717             continue;
2718         } else if (commit->action == ACTION_MKDIR) {
2719             mapping_t* mapping;
2720             int j, parent_path_len;
2721 
2722 #ifdef __MINGW32__
2723             if (mkdir(commit->path))
2724                 return -5;
2725 #else
2726             if (mkdir(commit->path, 0755))
2727                 return -5;
2728 #endif
2729 
2730             mapping = insert_mapping(s, commit->param.mkdir.cluster,
2731                     commit->param.mkdir.cluster + 1);
2732             if (mapping == NULL)
2733                 return -6;
2734 
2735             mapping->mode = MODE_DIRECTORY;
2736             mapping->read_only = 0;
2737             mapping->path = commit->path;
2738             j = s->directory.next;
2739             assert(j);
2740             insert_direntries(s, s->directory.next,
2741                     0x10 * s->sectors_per_cluster);
2742             mapping->info.dir.first_dir_index = j;
2743 
2744             parent_path_len = strlen(commit->path)
2745                 - strlen(get_basename(commit->path)) - 1;
2746             for (j = 0; j < s->mapping.next; j++) {
2747                 mapping_t* m = array_get(&(s->mapping), j);
2748                 if (m->first_mapping_index < 0 && m != mapping &&
2749                         !strncmp(m->path, mapping->path, parent_path_len) &&
2750                         strlen(m->path) == parent_path_len)
2751                     break;
2752             }
2753             assert(j < s->mapping.next);
2754             mapping->info.dir.parent_mapping_index = j;
2755 
2756             array_remove(&(s->commits), i);
2757             continue;
2758         }
2759 
2760         i++;
2761     }
2762     return 0;
2763 }
2764 
2765 /*
2766  * TODO: make sure that the short name is not matching *another* file
2767  */
2768 static int handle_commits(BDRVVVFATState* s)
2769 {
2770     int i, fail = 0;
2771 
2772     vvfat_close_current_file(s);
2773 
2774     for (i = 0; !fail && i < s->commits.next; i++) {
2775         commit_t* commit = array_get(&(s->commits), i);
2776         switch(commit->action) {
2777         case ACTION_RENAME: case ACTION_MKDIR:
2778             abort();
2779             fail = -2;
2780             break;
2781         case ACTION_WRITEOUT: {
2782 #ifndef NDEBUG
2783             /* these variables are only used by assert() below */
2784             direntry_t* entry = array_get(&(s->directory),
2785                     commit->param.writeout.dir_index);
2786             uint32_t begin = begin_of_direntry(entry);
2787             mapping_t* mapping = find_mapping_for_cluster(s, begin);
2788 #endif
2789 
2790             assert(mapping);
2791             assert(mapping->begin == begin);
2792             assert(commit->path == NULL);
2793 
2794             if (commit_one_file(s, commit->param.writeout.dir_index,
2795                         commit->param.writeout.modified_offset))
2796                 fail = -3;
2797 
2798             break;
2799         }
2800         case ACTION_NEW_FILE: {
2801             int begin = commit->param.new_file.first_cluster;
2802             mapping_t* mapping = find_mapping_for_cluster(s, begin);
2803             direntry_t* entry;
2804             int i;
2805 
2806             /* find direntry */
2807             for (i = 0; i < s->directory.next; i++) {
2808                 entry = array_get(&(s->directory), i);
2809                 if (is_file(entry) && begin_of_direntry(entry) == begin)
2810                     break;
2811             }
2812 
2813             if (i >= s->directory.next) {
2814                 fail = -6;
2815                 continue;
2816             }
2817 
2818             /* make sure there exists an initial mapping */
2819             if (mapping && mapping->begin != begin) {
2820                 mapping->end = begin;
2821                 mapping = NULL;
2822             }
2823             if (mapping == NULL) {
2824                 mapping = insert_mapping(s, begin, begin+1);
2825             }
2826             /* most members will be fixed in commit_mappings() */
2827             assert(commit->path);
2828             mapping->path = commit->path;
2829             mapping->read_only = 0;
2830             mapping->mode = MODE_NORMAL;
2831             mapping->info.file.offset = 0;
2832 
2833             if (commit_one_file(s, i, 0))
2834                 fail = -7;
2835 
2836             break;
2837         }
2838         default:
2839             abort();
2840         }
2841     }
2842     if (i > 0 && array_remove_slice(&(s->commits), 0, i))
2843         return -1;
2844     return fail;
2845 }
2846 
2847 static int handle_deletes(BDRVVVFATState* s)
2848 {
2849     int i, deferred = 1, deleted = 1;
2850 
2851     /* delete files corresponding to mappings marked as deleted */
2852     /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2853     while (deferred && deleted) {
2854         deferred = 0;
2855         deleted = 0;
2856 
2857         for (i = 1; i < s->mapping.next; i++) {
2858             mapping_t* mapping = array_get(&(s->mapping), i);
2859             if (mapping->mode & MODE_DELETED) {
2860                 direntry_t* entry = array_get(&(s->directory),
2861                         mapping->dir_index);
2862 
2863                 if (is_free(entry)) {
2864                     /* remove file/directory */
2865                     if (mapping->mode & MODE_DIRECTORY) {
2866                         int j, next_dir_index = s->directory.next,
2867                         first_dir_index = mapping->info.dir.first_dir_index;
2868 
2869                         if (rmdir(mapping->path) < 0) {
2870                             if (errno == ENOTEMPTY) {
2871                                 deferred++;
2872                                 continue;
2873                             } else
2874                                 return -5;
2875                         }
2876 
2877                         for (j = 1; j < s->mapping.next; j++) {
2878                             mapping_t* m = array_get(&(s->mapping), j);
2879                             if (m->mode & MODE_DIRECTORY &&
2880                                     m->info.dir.first_dir_index >
2881                                     first_dir_index &&
2882                                     m->info.dir.first_dir_index <
2883                                     next_dir_index)
2884                                 next_dir_index =
2885                                     m->info.dir.first_dir_index;
2886                         }
2887                         remove_direntries(s, first_dir_index,
2888                                 next_dir_index - first_dir_index);
2889 
2890                         deleted++;
2891                     }
2892                 } else {
2893                     if (unlink(mapping->path))
2894                         return -4;
2895                     deleted++;
2896                 }
2897                 DLOG(fprintf(stderr, "DELETE (%d)\n", i); print_mapping(mapping); print_direntry(entry));
2898                 remove_mapping(s, i);
2899             }
2900         }
2901     }
2902 
2903     return 0;
2904 }
2905 
2906 /*
2907  * synchronize mapping with new state:
2908  *
2909  * - copy FAT (with bdrv_read)
2910  * - mark all filenames corresponding to mappings as deleted
2911  * - recurse direntries from root (using bs->bdrv_read)
2912  * - delete files corresponding to mappings marked as deleted
2913  */
2914 static int do_commit(BDRVVVFATState* s)
2915 {
2916     int ret = 0;
2917 
2918     /* the real meat are the commits. Nothing to do? Move along! */
2919     if (s->commits.next == 0)
2920         return 0;
2921 
2922     vvfat_close_current_file(s);
2923 
2924     ret = handle_renames_and_mkdirs(s);
2925     if (ret) {
2926         fprintf(stderr, "Error handling renames (%d)\n", ret);
2927         abort();
2928         return ret;
2929     }
2930 
2931     /* copy FAT (with bdrv_read) */
2932     memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat);
2933 
2934     /* recurse direntries from root (using bs->bdrv_read) */
2935     ret = commit_direntries(s, 0, -1);
2936     if (ret) {
2937         fprintf(stderr, "Fatal: error while committing (%d)\n", ret);
2938         abort();
2939         return ret;
2940     }
2941 
2942     ret = handle_commits(s);
2943     if (ret) {
2944         fprintf(stderr, "Error handling commits (%d)\n", ret);
2945         abort();
2946         return ret;
2947     }
2948 
2949     ret = handle_deletes(s);
2950     if (ret) {
2951         fprintf(stderr, "Error deleting\n");
2952         abort();
2953         return ret;
2954     }
2955 
2956     if (s->qcow->bs->drv->bdrv_make_empty) {
2957         s->qcow->bs->drv->bdrv_make_empty(s->qcow->bs);
2958     }
2959 
2960     memset(s->used_clusters, 0, sector2cluster(s, s->sector_count));
2961 
2962 DLOG(checkpoint());
2963     return 0;
2964 }
2965 
2966 static int try_commit(BDRVVVFATState* s)
2967 {
2968     vvfat_close_current_file(s);
2969 DLOG(checkpoint());
2970     if(!is_consistent(s))
2971         return -1;
2972     return do_commit(s);
2973 }
2974 
2975 static int vvfat_write(BlockDriverState *bs, int64_t sector_num,
2976                     const uint8_t *buf, int nb_sectors)
2977 {
2978     BDRVVVFATState *s = bs->opaque;
2979     int i, ret;
2980 
2981 DLOG(checkpoint());
2982 
2983     /* Check if we're operating in read-only mode */
2984     if (s->qcow == NULL) {
2985         return -EACCES;
2986     }
2987 
2988     vvfat_close_current_file(s);
2989 
2990     /*
2991      * Some sanity checks:
2992      * - do not allow writing to the boot sector
2993      */
2994 
2995     if (sector_num < s->offset_to_fat)
2996         return -1;
2997 
2998     for (i = sector2cluster(s, sector_num);
2999             i <= sector2cluster(s, sector_num + nb_sectors - 1);) {
3000         mapping_t* mapping = find_mapping_for_cluster(s, i);
3001         if (mapping) {
3002             if (mapping->read_only) {
3003                 fprintf(stderr, "Tried to write to write-protected file %s\n",
3004                         mapping->path);
3005                 return -1;
3006             }
3007 
3008             if (mapping->mode & MODE_DIRECTORY) {
3009                 int begin = cluster2sector(s, i);
3010                 int end = begin + s->sectors_per_cluster, k;
3011                 int dir_index;
3012                 const direntry_t* direntries;
3013                 long_file_name lfn;
3014 
3015                 lfn_init(&lfn);
3016 
3017                 if (begin < sector_num)
3018                     begin = sector_num;
3019                 if (end > sector_num + nb_sectors)
3020                     end = sector_num + nb_sectors;
3021                 dir_index  = mapping->dir_index +
3022                     0x10 * (begin - mapping->begin * s->sectors_per_cluster);
3023                 direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
3024 
3025                 for (k = 0; k < (end - begin) * 0x10; k++) {
3026                     /* no access to the direntry of a read-only file */
3027                     if (is_short_name(direntries + k) &&
3028                             (direntries[k].attributes & 1)) {
3029                         if (memcmp(direntries + k,
3030                                     array_get(&(s->directory), dir_index + k),
3031                                     sizeof(direntry_t))) {
3032                             warn_report("tried to write to write-protected "
3033                                         "file");
3034                             return -1;
3035                         }
3036                     }
3037                 }
3038             }
3039             i = mapping->end;
3040         } else
3041             i++;
3042     }
3043 
3044     /*
3045      * Use qcow backend. Commit later.
3046      */
3047 DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
3048     ret = bdrv_write(s->qcow, sector_num, buf, nb_sectors);
3049     if (ret < 0) {
3050         fprintf(stderr, "Error writing to qcow backend\n");
3051         return ret;
3052     }
3053 
3054     for (i = sector2cluster(s, sector_num);
3055             i <= sector2cluster(s, sector_num + nb_sectors - 1); i++)
3056         if (i >= 0)
3057             s->used_clusters[i] |= USED_ALLOCATED;
3058 
3059 DLOG(checkpoint());
3060     /* TODO: add timeout */
3061     try_commit(s);
3062 
3063 DLOG(checkpoint());
3064     return 0;
3065 }
3066 
3067 static int coroutine_fn
3068 vvfat_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3069                  QEMUIOVector *qiov, int flags)
3070 {
3071     int ret;
3072     BDRVVVFATState *s = bs->opaque;
3073     uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
3074     int nb_sectors = bytes >> BDRV_SECTOR_BITS;
3075     void *buf;
3076 
3077     assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
3078     assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
3079 
3080     buf = g_try_malloc(bytes);
3081     if (bytes && buf == NULL) {
3082         return -ENOMEM;
3083     }
3084     qemu_iovec_to_buf(qiov, 0, buf, bytes);
3085 
3086     qemu_co_mutex_lock(&s->lock);
3087     ret = vvfat_write(bs, sector_num, buf, nb_sectors);
3088     qemu_co_mutex_unlock(&s->lock);
3089 
3090     g_free(buf);
3091 
3092     return ret;
3093 }
3094 
3095 static int64_t coroutine_fn vvfat_co_get_block_status(BlockDriverState *bs,
3096         int64_t sector_num, int nb_sectors, int *n, BlockDriverState **file)
3097 {
3098     *n = bs->total_sectors - sector_num;
3099     if (*n > nb_sectors) {
3100         *n = nb_sectors;
3101     } else if (*n < 0) {
3102         return 0;
3103     }
3104     return BDRV_BLOCK_DATA;
3105 }
3106 
3107 static int coroutine_fn
3108 write_target_commit(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
3109                     QEMUIOVector *qiov, int flags)
3110 {
3111     int ret;
3112 
3113     BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
3114     qemu_co_mutex_lock(&s->lock);
3115     ret = try_commit(s);
3116     qemu_co_mutex_unlock(&s->lock);
3117 
3118     return ret;
3119 }
3120 
3121 static void write_target_close(BlockDriverState *bs) {
3122     BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
3123     bdrv_unref_child(s->bs, s->qcow);
3124     g_free(s->qcow_filename);
3125 }
3126 
3127 static BlockDriver vvfat_write_target = {
3128     .format_name        = "vvfat_write_target",
3129     .instance_size      = sizeof(void*),
3130     .bdrv_co_pwritev    = write_target_commit,
3131     .bdrv_close         = write_target_close,
3132 };
3133 
3134 static void vvfat_qcow_options(int *child_flags, QDict *child_options,
3135                                int parent_flags, QDict *parent_options)
3136 {
3137     qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "off");
3138     *child_flags = BDRV_O_NO_FLUSH;
3139 }
3140 
3141 static const BdrvChildRole child_vvfat_qcow = {
3142     .inherit_options    = vvfat_qcow_options,
3143 };
3144 
3145 static int enable_write_target(BlockDriverState *bs, Error **errp)
3146 {
3147     BDRVVVFATState *s = bs->opaque;
3148     BlockDriver *bdrv_qcow = NULL;
3149     BlockDriverState *backing;
3150     QemuOpts *opts = NULL;
3151     int ret;
3152     int size = sector2cluster(s, s->sector_count);
3153     QDict *options;
3154 
3155     s->used_clusters = calloc(size, 1);
3156 
3157     array_init(&(s->commits), sizeof(commit_t));
3158 
3159     s->qcow_filename = g_malloc(PATH_MAX);
3160     ret = get_tmp_filename(s->qcow_filename, PATH_MAX);
3161     if (ret < 0) {
3162         error_setg_errno(errp, -ret, "can't create temporary file");
3163         goto err;
3164     }
3165 
3166     bdrv_qcow = bdrv_find_format("qcow");
3167     if (!bdrv_qcow) {
3168         error_setg(errp, "Failed to locate qcow driver");
3169         ret = -ENOENT;
3170         goto err;
3171     }
3172 
3173     opts = qemu_opts_create(bdrv_qcow->create_opts, NULL, 0, &error_abort);
3174     qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512,
3175                         &error_abort);
3176     qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:", &error_abort);
3177 
3178     ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, errp);
3179     qemu_opts_del(opts);
3180     if (ret < 0) {
3181         goto err;
3182     }
3183 
3184     options = qdict_new();
3185     qdict_put_str(options, "write-target.driver", "qcow");
3186     s->qcow = bdrv_open_child(s->qcow_filename, options, "write-target", bs,
3187                               &child_vvfat_qcow, false, errp);
3188     QDECREF(options);
3189     if (!s->qcow) {
3190         ret = -EINVAL;
3191         goto err;
3192     }
3193 
3194 #ifndef _WIN32
3195     unlink(s->qcow_filename);
3196 #endif
3197 
3198     backing = bdrv_new_open_driver(&vvfat_write_target, NULL, BDRV_O_ALLOW_RDWR,
3199                                    &error_abort);
3200     *(void**) backing->opaque = s;
3201 
3202     bdrv_set_backing_hd(s->bs, backing, &error_abort);
3203     bdrv_unref(backing);
3204 
3205     return 0;
3206 
3207 err:
3208     g_free(s->qcow_filename);
3209     s->qcow_filename = NULL;
3210     return ret;
3211 }
3212 
3213 static void vvfat_child_perm(BlockDriverState *bs, BdrvChild *c,
3214                              const BdrvChildRole *role,
3215                              uint64_t perm, uint64_t shared,
3216                              uint64_t *nperm, uint64_t *nshared)
3217 {
3218     BDRVVVFATState *s = bs->opaque;
3219 
3220     assert(c == s->qcow || role == &child_backing);
3221 
3222     if (c == s->qcow) {
3223         /* This is a private node, nobody should try to attach to it */
3224         *nperm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
3225         *nshared = BLK_PERM_WRITE_UNCHANGED;
3226     } else {
3227         /* The backing file is there so 'commit' can use it. vvfat doesn't
3228          * access it in any way. */
3229         *nperm = 0;
3230         *nshared = BLK_PERM_ALL;
3231     }
3232 }
3233 
3234 static void vvfat_close(BlockDriverState *bs)
3235 {
3236     BDRVVVFATState *s = bs->opaque;
3237 
3238     vvfat_close_current_file(s);
3239     array_free(&(s->fat));
3240     array_free(&(s->directory));
3241     array_free(&(s->mapping));
3242     g_free(s->cluster_buffer);
3243 
3244     if (s->qcow) {
3245         migrate_del_blocker(s->migration_blocker);
3246         error_free(s->migration_blocker);
3247     }
3248 }
3249 
3250 static BlockDriver bdrv_vvfat = {
3251     .format_name            = "vvfat",
3252     .protocol_name          = "fat",
3253     .instance_size          = sizeof(BDRVVVFATState),
3254 
3255     .bdrv_parse_filename    = vvfat_parse_filename,
3256     .bdrv_file_open         = vvfat_open,
3257     .bdrv_refresh_limits    = vvfat_refresh_limits,
3258     .bdrv_close             = vvfat_close,
3259     .bdrv_child_perm        = vvfat_child_perm,
3260 
3261     .bdrv_co_preadv         = vvfat_co_preadv,
3262     .bdrv_co_pwritev        = vvfat_co_pwritev,
3263     .bdrv_co_get_block_status = vvfat_co_get_block_status,
3264 };
3265 
3266 static void bdrv_vvfat_init(void)
3267 {
3268     bdrv_register(&bdrv_vvfat);
3269 }
3270 
3271 block_init(bdrv_vvfat_init);
3272 
3273 #ifdef DEBUG
3274 static void checkpoint(void) {
3275     assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2);
3276     check1(vvv);
3277     check2(vvv);
3278     assert(!vvv->current_mapping || vvv->current_fd || (vvv->current_mapping->mode & MODE_DIRECTORY));
3279 #if 0
3280     if (((direntry_t*)vvv->directory.pointer)[1].attributes != 0xf)
3281         fprintf(stderr, "Nonono!\n");
3282     mapping_t* mapping;
3283     direntry_t* direntry;
3284     assert(vvv->mapping.size >= vvv->mapping.item_size * vvv->mapping.next);
3285     assert(vvv->directory.size >= vvv->directory.item_size * vvv->directory.next);
3286     if (vvv->mapping.next<47)
3287         return;
3288     assert((mapping = array_get(&(vvv->mapping), 47)));
3289     assert(mapping->dir_index < vvv->directory.next);
3290     direntry = array_get(&(vvv->directory), mapping->dir_index);
3291     assert(!memcmp(direntry->name, "USB     H  ", 11) || direntry->name[0]==0);
3292 #endif
3293 }
3294 #endif
3295