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