1 /* vim:set shiftwidth=4 ts=4: */
3 * QEMU Block driver for virtual VFAT (shadows a local directory)
5 * Copyright (c) 2004,2005 Johannes E. Schindelin
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
30 #include "qemu/bswap.h"
31 #include "migration/blocker.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/cutils.h"
35 #include "qemu/error-report.h"
44 /* TODO: add ":bootsector=blabla.img:" */
45 /* LATER TODO: add automatic boot sector generation from
46 BOOTEASY.ASM and Ranish Partition Manager
47 Note that DOS assumes the system files to be the first files in the
48 file system (test if the boot sector still relies on that fact)! */
49 /* MAYBE TODO: write block-visofs.c */
50 /* TODO: call try_commit() only after a timeout */
58 static void checkpoint(void);
61 void nonono(const char* file
, int line
, const char* msg
) {
62 fprintf(stderr
, "Nonono! %s:%d %s\n", file
, line
, msg
);
66 #define assert(a) do {if (!(a)) nonono(__FILE__, __LINE__, #a);}while(0)
75 /* bootsector OEM name. see related compatibility problems at:
76 * https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
77 * http://seasip.info/Misc/oemid.html
79 #define BOOTSECTOR_OEM_NAME "MSWIN4.1"
81 #define DIR_DELETED 0xe5
82 #define DIR_KANJI DIR_DELETED
83 #define DIR_KANJI_FAKE 0x05
86 /* dynamic array functions */
87 typedef struct array_t
{
89 unsigned int size
,next
,item_size
;
92 static inline void array_init(array_t
* array
,unsigned int item_size
)
94 array
->pointer
= NULL
;
97 array
->item_size
=item_size
;
100 static inline void array_free(array_t
* array
)
102 g_free(array
->pointer
);
103 array
->size
=array
->next
=0;
106 /* does not automatically grow */
107 static inline void* array_get(array_t
* array
,unsigned int index
) {
108 assert(index
< array
->next
);
109 return array
->pointer
+ index
* array
->item_size
;
112 static inline int array_ensure_allocated(array_t
* array
, int index
)
114 if((index
+ 1) * array
->item_size
> array
->size
) {
115 int new_size
= (index
+ 32) * array
->item_size
;
116 array
->pointer
= g_realloc(array
->pointer
, new_size
);
119 memset(array
->pointer
+ array
->size
, 0, new_size
- array
->size
);
120 array
->size
= new_size
;
121 array
->next
= index
+ 1;
127 static inline void* array_get_next(array_t
* array
) {
128 unsigned int next
= array
->next
;
130 if (array_ensure_allocated(array
, next
) < 0)
133 array
->next
= next
+ 1;
134 return array_get(array
, next
);
137 static inline void* array_insert(array_t
* array
,unsigned int index
,unsigned int count
) {
138 if((array
->next
+count
)*array
->item_size
>array
->size
) {
139 int increment
=count
*array
->item_size
;
140 array
->pointer
=g_realloc(array
->pointer
,array
->size
+increment
);
143 array
->size
+=increment
;
145 memmove(array
->pointer
+(index
+count
)*array
->item_size
,
146 array
->pointer
+index
*array
->item_size
,
147 (array
->next
-index
)*array
->item_size
);
149 return array
->pointer
+index
*array
->item_size
;
152 /* this performs a "roll", so that the element which was at index_from becomes
153 * index_to, but the order of all other elements is preserved. */
154 static inline int array_roll(array_t
* array
,int index_to
,int index_from
,int count
)
162 index_to
<0 || index_to
>=array
->next
||
163 index_from
<0 || index_from
>=array
->next
)
166 if(index_to
==index_from
)
170 from
=array
->pointer
+index_from
*is
;
171 to
=array
->pointer
+index_to
*is
;
172 buf
=g_malloc(is
*count
);
173 memcpy(buf
,from
,is
*count
);
175 if(index_to
<index_from
)
176 memmove(to
+is
*count
,to
,from
-to
);
178 memmove(from
,from
+is
*count
,to
-from
);
180 memcpy(to
,buf
,is
*count
);
187 static inline int array_remove_slice(array_t
* array
,int index
, int count
)
191 assert(index
+ count
<= array
->next
);
192 if(array_roll(array
,array
->next
-1,index
,count
))
194 array
->next
-= count
;
198 static int array_remove(array_t
* array
,int index
)
200 return array_remove_slice(array
, index
, 1);
203 /* return the index for a given member */
204 static int array_index(array_t
* array
, void* pointer
)
206 size_t offset
= (char*)pointer
- array
->pointer
;
207 assert((offset
% array
->item_size
) == 0);
208 assert(offset
/array
->item_size
< array
->next
);
209 return offset
/array
->item_size
;
212 /* These structures are used to fake a disk and the VFAT filesystem.
213 * For this reason we need to use QEMU_PACKED. */
215 typedef struct bootsector_t
{
218 uint16_t sector_size
;
219 uint8_t sectors_per_cluster
;
220 uint16_t reserved_sectors
;
221 uint8_t number_of_fats
;
222 uint16_t root_entries
;
223 uint16_t total_sectors16
;
225 uint16_t sectors_per_fat
;
226 uint16_t sectors_per_track
;
227 uint16_t number_of_heads
;
228 uint32_t hidden_sectors
;
229 uint32_t total_sectors
;
232 uint8_t drive_number
;
236 uint8_t volume_label
[11];
238 uint8_t ignored
[0x1c0];
241 uint32_t sectors_per_fat
;
244 uint32_t first_cluster_of_root_dir
;
245 uint16_t info_sector
;
246 uint16_t backup_boot_sector
;
247 uint8_t reserved
[12];
248 uint8_t drive_number
;
252 uint8_t volume_label
[11];
254 uint8_t ignored
[0x1a4];
258 } QEMU_PACKED bootsector_t
;
266 typedef struct partition_t
{
267 uint8_t attributes
; /* 0x80 = bootable */
269 uint8_t fs_type
; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
271 uint32_t start_sector_long
;
272 uint32_t length_sector_long
;
273 } QEMU_PACKED partition_t
;
275 typedef struct mbr_t
{
276 uint8_t ignored
[0x1b8];
279 partition_t partition
[4];
283 typedef struct direntry_t
{
295 } QEMU_PACKED direntry_t
;
297 /* this structure are used to transparently access the files */
299 typedef struct mapping_t
{
300 /* begin is the first cluster, end is the last+1 */
302 /* as s->directory is growable, no pointer may be used here */
303 unsigned int dir_index
;
304 /* the clusters of a file may be in any order; this points to the first */
305 int first_mapping_index
;
308 * - the offset in the file (in clusters) for a file, or
309 * - the next cluster of the directory for a directory
315 int parent_mapping_index
;
319 /* path contains the full path, i.e. it always starts with s->path */
333 static void print_direntry(const struct direntry_t
*);
334 static void print_mapping(const struct mapping_t
* mapping
);
337 /* here begins the real VVFAT driver */
339 typedef struct BDRVVVFATState
{
341 BlockDriverState
* bs
; /* pointer to parent */
342 unsigned char first_sectors
[0x40*0x200];
344 int fat_type
; /* 16 or 32 */
345 array_t fat
,directory
,mapping
;
346 char volume_label
[11];
348 uint32_t offset_to_bootsector
; /* 0 for floppy, 0x3f for disk */
350 unsigned int cluster_size
;
351 unsigned int sectors_per_cluster
;
352 unsigned int sectors_per_fat
;
353 uint32_t last_cluster_of_root_directory
;
354 /* how many entries are available in root directory (0 for FAT32) */
355 uint16_t root_entries
;
356 uint32_t sector_count
; /* total number of sectors of the partition */
357 uint32_t cluster_count
; /* total number of clusters of this partition */
358 uint32_t max_fat_value
;
359 uint32_t offset_to_fat
;
360 uint32_t offset_to_root_dir
;
363 mapping_t
* current_mapping
;
364 unsigned char* cluster
; /* points to current cluster */
365 unsigned char* cluster_buffer
; /* points to a buffer to hold temp data */
366 unsigned int current_cluster
;
375 int downcase_short_names
;
377 Error
*migration_blocker
;
380 /* take the sector position spos and convert it to Cylinder/Head/Sector position
381 * if the position is outside the specified geometry, fill maximum value for CHS
382 * and return 1 to signal overflow.
384 static int sector2CHS(mbr_chs_t
*chs
, int spos
, int cyls
, int heads
, int secs
)
387 sector
= spos
% secs
; spos
/= secs
;
388 head
= spos
% heads
; spos
/= heads
;
391 it happens if 32bit sector positions are used, while CHS is only 24bit.
392 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
395 chs
->cylinder
= 0xFF;
398 chs
->head
= (uint8_t)head
;
399 chs
->sector
= (uint8_t)( (sector
+1) | ((spos
>>8)<<6) );
400 chs
->cylinder
= (uint8_t)spos
;
404 static void init_mbr(BDRVVVFATState
*s
, int cyls
, int heads
, int secs
)
406 /* TODO: if the files mbr.img and bootsect.img exist, use them */
407 mbr_t
* real_mbr
=(mbr_t
*)s
->first_sectors
;
408 partition_t
* partition
= &(real_mbr
->partition
[0]);
411 memset(s
->first_sectors
,0,512);
413 /* Win NT Disk Signature */
414 real_mbr
->nt_id
= cpu_to_le32(0xbe1afdfa);
416 partition
->attributes
=0x80; /* bootable */
418 /* LBA is used when partition is outside the CHS geometry */
419 lba
= sector2CHS(&partition
->start_CHS
, s
->offset_to_bootsector
,
421 lba
|= sector2CHS(&partition
->end_CHS
, s
->bs
->total_sectors
- 1,
424 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
425 partition
->start_sector_long
= cpu_to_le32(s
->offset_to_bootsector
);
426 partition
->length_sector_long
= cpu_to_le32(s
->bs
->total_sectors
427 - s
->offset_to_bootsector
);
429 /* FAT12/FAT16/FAT32 */
430 /* DOS uses different types when partition is LBA,
431 probably to prevent older versions from using CHS on them */
432 partition
->fs_type
= s
->fat_type
== 12 ? 0x1 :
433 s
->fat_type
== 16 ? (lba
? 0xe : 0x06) :
434 /*s->fat_type == 32*/ (lba
? 0xc : 0x0b);
436 real_mbr
->magic
[0]=0x55; real_mbr
->magic
[1]=0xaa;
439 /* direntry functions */
441 static direntry_t
*create_long_filename(BDRVVVFATState
*s
, const char *filename
)
443 int number_of_entries
, i
;
447 gunichar2
*longname
= g_utf8_to_utf16(filename
, -1, NULL
, &length
, NULL
);
449 fprintf(stderr
, "vvfat: invalid UTF-8 name: %s\n", filename
);
453 number_of_entries
= DIV_ROUND_UP(length
* 2, 26);
455 for(i
=0;i
<number_of_entries
;i
++) {
456 entry
=array_get_next(&(s
->directory
));
457 entry
->attributes
=0xf;
458 entry
->reserved
[0]=0;
460 entry
->name
[0]=(number_of_entries
-i
)|(i
==0?0x40:0);
462 for(i
=0;i
<26*number_of_entries
;i
++) {
464 if(offset
<10) offset
=1+offset
;
465 else if(offset
<22) offset
=14+offset
-10;
466 else offset
=28+offset
-22;
467 entry
=array_get(&(s
->directory
),s
->directory
.next
-1-(i
/26));
468 if (i
>= 2 * length
+ 2) {
469 entry
->name
[offset
] = 0xff;
470 } else if (i
% 2 == 0) {
471 entry
->name
[offset
] = longname
[i
/ 2] & 0xff;
473 entry
->name
[offset
] = longname
[i
/ 2] >> 8;
477 return array_get(&(s
->directory
),s
->directory
.next
-number_of_entries
);
480 static char is_free(const direntry_t
* direntry
)
482 return direntry
->name
[0] == DIR_DELETED
|| direntry
->name
[0] == DIR_FREE
;
485 static char is_volume_label(const direntry_t
* direntry
)
487 return direntry
->attributes
== 0x28;
490 static char is_long_name(const direntry_t
* direntry
)
492 return direntry
->attributes
== 0xf;
495 static char is_short_name(const direntry_t
* direntry
)
497 return !is_volume_label(direntry
) && !is_long_name(direntry
)
498 && !is_free(direntry
);
501 static char is_directory(const direntry_t
* direntry
)
503 return direntry
->attributes
& 0x10 && direntry
->name
[0] != DIR_DELETED
;
506 static inline char is_dot(const direntry_t
* direntry
)
508 return is_short_name(direntry
) && direntry
->name
[0] == '.';
511 static char is_file(const direntry_t
* direntry
)
513 return is_short_name(direntry
) && !is_directory(direntry
);
516 static inline uint32_t begin_of_direntry(const direntry_t
* direntry
)
518 return le16_to_cpu(direntry
->begin
)|(le16_to_cpu(direntry
->begin_hi
)<<16);
521 static inline uint32_t filesize_of_direntry(const direntry_t
* direntry
)
523 return le32_to_cpu(direntry
->size
);
526 static void set_begin_of_direntry(direntry_t
* direntry
, uint32_t begin
)
528 direntry
->begin
= cpu_to_le16(begin
& 0xffff);
529 direntry
->begin_hi
= cpu_to_le16((begin
>> 16) & 0xffff);
532 static uint8_t to_valid_short_char(gunichar c
)
534 c
= g_unichar_toupper(c
);
535 if ((c
>= '0' && c
<= '9') ||
536 (c
>= 'A' && c
<= 'Z') ||
537 strchr("$%'-_@~`!(){}^#&", c
) != 0) {
544 static direntry_t
*create_short_filename(BDRVVVFATState
*s
,
545 const char *filename
,
546 unsigned int directory_start
)
549 direntry_t
*entry
= array_get_next(&(s
->directory
));
550 const gchar
*p
, *last_dot
= NULL
;
552 bool lossy_conversion
= false;
558 memset(entry
->name
, 0x20, sizeof(entry
->name
));
560 /* copy filename and search last dot */
561 for (p
= filename
; ; p
= g_utf8_next_char(p
)) {
562 c
= g_utf8_get_char(p
);
565 } else if (c
== '.') {
567 /* '.' at start of filename */
568 lossy_conversion
= true;
571 lossy_conversion
= true;
575 } else if (!last_dot
) {
576 /* first part of the name; copy it */
577 uint8_t v
= to_valid_short_char(c
);
579 entry
->name
[j
++] = v
;
581 lossy_conversion
= true;
586 /* copy extension (if any) */
589 for (p
= g_utf8_next_char(last_dot
); ; p
= g_utf8_next_char(p
)) {
590 c
= g_utf8_get_char(p
);
594 /* extension; copy it */
595 uint8_t v
= to_valid_short_char(c
);
597 entry
->name
[8 + (j
++)] = v
;
599 lossy_conversion
= true;
605 if (entry
->name
[0] == DIR_KANJI
) {
606 entry
->name
[0] = DIR_KANJI_FAKE
;
609 /* numeric-tail generation */
610 for (j
= 0; j
< 8; j
++) {
611 if (entry
->name
[j
] == ' ') {
615 for (i
= lossy_conversion
? 1 : 0; i
< 999999; i
++) {
618 int len
= snprintf(tail
, sizeof(tail
), "~%u", (unsigned)i
);
620 memcpy(entry
->name
+ MIN(j
, 8 - len
), tail
, len
);
622 for (entry1
= array_get(&(s
->directory
), directory_start
);
623 entry1
< entry
; entry1
++) {
624 if (!is_long_name(entry1
) &&
625 !memcmp(entry1
->name
, entry
->name
, 11)) {
626 break; /* found dupe */
629 if (entry1
== entry
) {
639 static inline uint8_t fat_chksum(const direntry_t
* entry
)
644 for (i
= 0; i
< ARRAY_SIZE(entry
->name
); i
++) {
645 chksum
= (((chksum
& 0xfe) >> 1) |
646 ((chksum
& 0x01) ? 0x80 : 0)) + entry
->name
[i
];
652 /* if return_time==0, this returns the fat_date, else the fat_time */
653 static uint16_t fat_datetime(time_t time
,int return_time
) {
657 localtime_r(&time
,t
);
659 return cpu_to_le16((t
->tm_sec
/2)|(t
->tm_min
<<5)|(t
->tm_hour
<<11));
660 return cpu_to_le16((t
->tm_mday
)|((t
->tm_mon
+1)<<5)|((t
->tm_year
-80)<<9));
663 static inline void fat_set(BDRVVVFATState
* s
,unsigned int cluster
,uint32_t value
)
665 if(s
->fat_type
==32) {
666 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
667 *entry
=cpu_to_le32(value
);
668 } else if(s
->fat_type
==16) {
669 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
670 *entry
=cpu_to_le16(value
&0xffff);
672 int offset
= (cluster
*3/2);
673 unsigned char* p
= array_get(&(s
->fat
), offset
);
677 p
[1] = (p
[1]&0xf0) | ((value
>>8)&0xf);
680 p
[0] = (p
[0]&0xf) | ((value
&0xf)<<4);
687 static inline uint32_t fat_get(BDRVVVFATState
* s
,unsigned int cluster
)
689 if(s
->fat_type
==32) {
690 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
691 return le32_to_cpu(*entry
);
692 } else if(s
->fat_type
==16) {
693 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
694 return le16_to_cpu(*entry
);
696 const uint8_t* x
=(uint8_t*)(s
->fat
.pointer
)+cluster
*3/2;
697 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
701 static inline int fat_eof(BDRVVVFATState
* s
,uint32_t fat_entry
)
703 if(fat_entry
>s
->max_fat_value
-8)
708 static inline void init_fat(BDRVVVFATState
* s
)
710 if (s
->fat_type
== 12) {
711 array_init(&(s
->fat
),1);
712 array_ensure_allocated(&(s
->fat
),
713 s
->sectors_per_fat
* 0x200 * 3 / 2 - 1);
715 array_init(&(s
->fat
),(s
->fat_type
==32?4:2));
716 array_ensure_allocated(&(s
->fat
),
717 s
->sectors_per_fat
* 0x200 / s
->fat
.item_size
- 1);
719 memset(s
->fat
.pointer
,0,s
->fat
.size
);
721 switch(s
->fat_type
) {
722 case 12: s
->max_fat_value
=0xfff; break;
723 case 16: s
->max_fat_value
=0xffff; break;
724 case 32: s
->max_fat_value
=0x0fffffff; break;
725 default: s
->max_fat_value
=0; /* error... */
730 static inline direntry_t
* create_short_and_long_name(BDRVVVFATState
* s
,
731 unsigned int directory_start
, const char* filename
, int is_dot
)
733 int long_index
= s
->directory
.next
;
734 direntry_t
* entry
= NULL
;
735 direntry_t
* entry_long
= NULL
;
738 entry
=array_get_next(&(s
->directory
));
739 memset(entry
->name
, 0x20, sizeof(entry
->name
));
740 memcpy(entry
->name
,filename
,strlen(filename
));
744 entry_long
=create_long_filename(s
,filename
);
745 entry
= create_short_filename(s
, filename
, directory_start
);
747 /* calculate checksum; propagate to long name */
749 uint8_t chksum
=fat_chksum(entry
);
751 /* calculate anew, because realloc could have taken place */
752 entry_long
=array_get(&(s
->directory
),long_index
);
753 while(entry_long
<entry
&& is_long_name(entry_long
)) {
754 entry_long
->reserved
[1]=chksum
;
763 * Read a directory. (the index of the corresponding mapping must be passed).
765 static int read_directory(BDRVVVFATState
* s
, int mapping_index
)
767 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
768 direntry_t
* direntry
;
769 const char* dirname
= mapping
->path
;
770 int first_cluster
= mapping
->begin
;
771 int parent_index
= mapping
->info
.dir
.parent_mapping_index
;
772 mapping_t
* parent_mapping
= (mapping_t
*)
773 (parent_index
>= 0 ? array_get(&(s
->mapping
), parent_index
) : NULL
);
774 int first_cluster_of_parent
= parent_mapping
? parent_mapping
->begin
: -1;
776 DIR* dir
=opendir(dirname
);
777 struct dirent
* entry
;
780 assert(mapping
->mode
& MODE_DIRECTORY
);
783 mapping
->end
= mapping
->begin
;
787 i
= mapping
->info
.dir
.first_dir_index
=
788 first_cluster
== 0 ? 0 : s
->directory
.next
;
790 if (first_cluster
!= 0) {
791 /* create the top entries of a subdirectory */
792 (void)create_short_and_long_name(s
, i
, ".", 1);
793 (void)create_short_and_long_name(s
, i
, "..", 1);
796 /* actually read the directory, and allocate the mappings */
797 while((entry
=readdir(dir
))) {
798 unsigned int length
=strlen(dirname
)+2+strlen(entry
->d_name
);
800 direntry_t
* direntry
;
802 int is_dot
=!strcmp(entry
->d_name
,".");
803 int is_dotdot
=!strcmp(entry
->d_name
,"..");
805 if (first_cluster
== 0 && s
->directory
.next
>= s
->root_entries
- 1) {
806 fprintf(stderr
, "Too many entries in root directory\n");
811 if(first_cluster
== 0 && (is_dotdot
|| is_dot
))
814 buffer
= g_malloc(length
);
815 snprintf(buffer
,length
,"%s/%s",dirname
,entry
->d_name
);
817 if(stat(buffer
,&st
)<0) {
822 /* create directory entry for this file */
823 if (!is_dot
&& !is_dotdot
) {
824 direntry
= create_short_and_long_name(s
, i
, entry
->d_name
, 0);
826 direntry
= array_get(&(s
->directory
), is_dot
? i
: i
+ 1);
828 direntry
->attributes
=(S_ISDIR(st
.st_mode
)?0x10:0x20);
829 direntry
->reserved
[0]=direntry
->reserved
[1]=0;
830 direntry
->ctime
=fat_datetime(st
.st_ctime
,1);
831 direntry
->cdate
=fat_datetime(st
.st_ctime
,0);
832 direntry
->adate
=fat_datetime(st
.st_atime
,0);
833 direntry
->begin_hi
=0;
834 direntry
->mtime
=fat_datetime(st
.st_mtime
,1);
835 direntry
->mdate
=fat_datetime(st
.st_mtime
,0);
837 set_begin_of_direntry(direntry
, first_cluster_of_parent
);
839 set_begin_of_direntry(direntry
, first_cluster
);
841 direntry
->begin
=0; /* do that later */
842 if (st
.st_size
> 0x7fffffff) {
843 fprintf(stderr
, "File %s is larger than 2GB\n", buffer
);
848 direntry
->size
=cpu_to_le32(S_ISDIR(st
.st_mode
)?0:st
.st_size
);
850 /* create mapping for this file */
851 if(!is_dot
&& !is_dotdot
&& (S_ISDIR(st
.st_mode
) || st
.st_size
)) {
852 s
->current_mapping
= array_get_next(&(s
->mapping
));
853 s
->current_mapping
->begin
=0;
854 s
->current_mapping
->end
=st
.st_size
;
856 * we get the direntry of the most recent direntry, which
857 * contains the short name and all the relevant information.
859 s
->current_mapping
->dir_index
=s
->directory
.next
-1;
860 s
->current_mapping
->first_mapping_index
= -1;
861 if (S_ISDIR(st
.st_mode
)) {
862 s
->current_mapping
->mode
= MODE_DIRECTORY
;
863 s
->current_mapping
->info
.dir
.parent_mapping_index
=
866 s
->current_mapping
->mode
= MODE_UNDEFINED
;
867 s
->current_mapping
->info
.file
.offset
= 0;
869 s
->current_mapping
->path
=buffer
;
870 s
->current_mapping
->read_only
=
871 (st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) == 0;
878 /* fill with zeroes up to the end of the cluster */
879 while(s
->directory
.next
%(0x10*s
->sectors_per_cluster
)) {
880 direntry_t
* direntry
=array_get_next(&(s
->directory
));
881 memset(direntry
,0,sizeof(direntry_t
));
884 if (s
->fat_type
!= 32 &&
885 mapping_index
== 0 &&
886 s
->directory
.next
< s
->root_entries
) {
888 int cur
= s
->directory
.next
;
889 array_ensure_allocated(&(s
->directory
), s
->root_entries
- 1);
890 s
->directory
.next
= s
->root_entries
;
891 memset(array_get(&(s
->directory
), cur
), 0,
892 (s
->root_entries
- cur
) * sizeof(direntry_t
));
895 /* re-get the mapping, since s->mapping was possibly realloc()ed */
896 mapping
= array_get(&(s
->mapping
), mapping_index
);
897 first_cluster
+= (s
->directory
.next
- mapping
->info
.dir
.first_dir_index
)
898 * 0x20 / s
->cluster_size
;
899 mapping
->end
= first_cluster
;
901 direntry
= array_get(&(s
->directory
), mapping
->dir_index
);
902 set_begin_of_direntry(direntry
, mapping
->begin
);
907 static inline uint32_t sector2cluster(BDRVVVFATState
* s
,off_t sector_num
)
909 return (sector_num
- s
->offset_to_root_dir
) / s
->sectors_per_cluster
;
912 static inline off_t
cluster2sector(BDRVVVFATState
* s
, uint32_t cluster_num
)
914 return s
->offset_to_root_dir
+ s
->sectors_per_cluster
* cluster_num
;
917 static int init_directories(BDRVVVFATState
* s
,
918 const char *dirname
, int heads
, int secs
,
921 bootsector_t
* bootsector
;
924 unsigned int cluster
;
926 memset(&(s
->first_sectors
[0]),0,0x40*0x200);
928 s
->cluster_size
=s
->sectors_per_cluster
*0x200;
929 s
->cluster_buffer
=g_malloc(s
->cluster_size
);
932 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
933 * where sc is sector_count,
934 * spf is sectors_per_fat,
935 * spc is sectors_per_clusters, and
936 * fat_type = 12, 16 or 32.
938 i
= 1+s
->sectors_per_cluster
*0x200*8/s
->fat_type
;
939 s
->sectors_per_fat
=(s
->sector_count
+i
)/i
; /* round up */
941 s
->offset_to_fat
= s
->offset_to_bootsector
+ 1;
942 s
->offset_to_root_dir
= s
->offset_to_fat
+ s
->sectors_per_fat
* 2;
944 array_init(&(s
->mapping
),sizeof(mapping_t
));
945 array_init(&(s
->directory
),sizeof(direntry_t
));
947 /* add volume label */
949 direntry_t
* entry
=array_get_next(&(s
->directory
));
950 entry
->attributes
=0x28; /* archive | volume label */
951 memcpy(entry
->name
, s
->volume_label
, sizeof(entry
->name
));
954 /* Now build FAT, and write back information into directory */
957 /* TODO: if there are more entries, bootsector has to be adjusted! */
958 s
->root_entries
= 0x02 * 0x10 * s
->sectors_per_cluster
;
959 s
->cluster_count
=sector2cluster(s
, s
->sector_count
);
961 mapping
= array_get_next(&(s
->mapping
));
963 mapping
->dir_index
= 0;
964 mapping
->info
.dir
.parent_mapping_index
= -1;
965 mapping
->first_mapping_index
= -1;
966 mapping
->path
= g_strdup(dirname
);
967 i
= strlen(mapping
->path
);
968 if (i
> 0 && mapping
->path
[i
- 1] == '/')
969 mapping
->path
[i
- 1] = '\0';
970 mapping
->mode
= MODE_DIRECTORY
;
971 mapping
->read_only
= 0;
972 s
->path
= mapping
->path
;
974 for (i
= 0, cluster
= 0; i
< s
->mapping
.next
; i
++) {
975 /* MS-DOS expects the FAT to be 0 for the root directory
976 * (except for the media byte). */
977 /* LATER TODO: still true for FAT32? */
978 int fix_fat
= (i
!= 0);
979 mapping
= array_get(&(s
->mapping
), i
);
981 if (mapping
->mode
& MODE_DIRECTORY
) {
982 mapping
->begin
= cluster
;
983 if(read_directory(s
, i
)) {
984 error_setg(errp
, "Could not read directory %s",
988 mapping
= array_get(&(s
->mapping
), i
);
990 assert(mapping
->mode
== MODE_UNDEFINED
);
991 mapping
->mode
=MODE_NORMAL
;
992 mapping
->begin
= cluster
;
993 if (mapping
->end
> 0) {
994 direntry_t
* direntry
= array_get(&(s
->directory
),
997 mapping
->end
= cluster
+ 1 + (mapping
->end
-1)/s
->cluster_size
;
998 set_begin_of_direntry(direntry
, mapping
->begin
);
1000 mapping
->end
= cluster
+ 1;
1005 assert(mapping
->begin
< mapping
->end
);
1007 /* next free cluster */
1008 cluster
= mapping
->end
;
1010 if(cluster
> s
->cluster_count
) {
1012 "Directory does not fit in FAT%d (capacity %.2f MB)",
1013 s
->fat_type
, s
->sector_count
/ 2000.0);
1017 /* fix fat for entry */
1020 for(j
= mapping
->begin
; j
< mapping
->end
- 1; j
++)
1022 fat_set(s
, mapping
->end
- 1, s
->max_fat_value
);
1026 mapping
= array_get(&(s
->mapping
), 0);
1027 s
->last_cluster_of_root_directory
= mapping
->end
;
1029 /* the FAT signature */
1030 fat_set(s
,0,s
->max_fat_value
);
1031 fat_set(s
,1,s
->max_fat_value
);
1033 s
->current_mapping
= NULL
;
1035 bootsector
= (bootsector_t
*)(s
->first_sectors
1036 + s
->offset_to_bootsector
* 0x200);
1037 bootsector
->jump
[0]=0xeb;
1038 bootsector
->jump
[1]=0x3e;
1039 bootsector
->jump
[2]=0x90;
1040 memcpy(bootsector
->name
, BOOTSECTOR_OEM_NAME
, 8);
1041 bootsector
->sector_size
=cpu_to_le16(0x200);
1042 bootsector
->sectors_per_cluster
=s
->sectors_per_cluster
;
1043 bootsector
->reserved_sectors
=cpu_to_le16(1);
1044 bootsector
->number_of_fats
=0x2; /* number of FATs */
1045 bootsector
->root_entries
= cpu_to_le16(s
->root_entries
);
1046 bootsector
->total_sectors16
=s
->sector_count
>0xffff?0:cpu_to_le16(s
->sector_count
);
1047 /* media descriptor: hard disk=0xf8, floppy=0xf0 */
1048 bootsector
->media_type
= (s
->offset_to_bootsector
> 0 ? 0xf8 : 0xf0);
1049 s
->fat
.pointer
[0] = bootsector
->media_type
;
1050 bootsector
->sectors_per_fat
=cpu_to_le16(s
->sectors_per_fat
);
1051 bootsector
->sectors_per_track
= cpu_to_le16(secs
);
1052 bootsector
->number_of_heads
= cpu_to_le16(heads
);
1053 bootsector
->hidden_sectors
= cpu_to_le32(s
->offset_to_bootsector
);
1054 bootsector
->total_sectors
=cpu_to_le32(s
->sector_count
>0xffff?s
->sector_count
:0);
1056 /* LATER TODO: if FAT32, this is wrong */
1057 /* drive_number: fda=0, hda=0x80 */
1058 bootsector
->u
.fat16
.drive_number
= s
->offset_to_bootsector
== 0 ? 0 : 0x80;
1059 bootsector
->u
.fat16
.signature
=0x29;
1060 bootsector
->u
.fat16
.id
=cpu_to_le32(0xfabe1afd);
1062 memcpy(bootsector
->u
.fat16
.volume_label
, s
->volume_label
,
1063 sizeof(bootsector
->u
.fat16
.volume_label
));
1064 memcpy(bootsector
->u
.fat16
.fat_type
,
1065 s
->fat_type
== 12 ? "FAT12 " : "FAT16 ", 8);
1066 bootsector
->magic
[0]=0x55; bootsector
->magic
[1]=0xaa;
1072 static BDRVVVFATState
*vvv
= NULL
;
1075 static int enable_write_target(BlockDriverState
*bs
, Error
**errp
);
1076 static int is_consistent(BDRVVVFATState
*s
);
1078 static QemuOptsList runtime_opts
= {
1080 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1084 .type
= QEMU_OPT_STRING
,
1085 .help
= "Host directory to map to the vvfat device",
1089 .type
= QEMU_OPT_NUMBER
,
1090 .help
= "FAT type (12, 16 or 32)",
1094 .type
= QEMU_OPT_BOOL
,
1095 .help
= "Create a floppy rather than a hard disk image",
1099 .type
= QEMU_OPT_STRING
,
1100 .help
= "Use a volume label other than QEMU VVFAT",
1104 .type
= QEMU_OPT_BOOL
,
1105 .help
= "Make the image writable",
1107 { /* end of list */ }
1111 static void vvfat_parse_filename(const char *filename
, QDict
*options
,
1115 bool floppy
= false;
1119 if (!strstart(filename
, "fat:", NULL
)) {
1120 error_setg(errp
, "File name string must start with 'fat:'");
1125 if (strstr(filename
, ":32:")) {
1127 } else if (strstr(filename
, ":16:")) {
1129 } else if (strstr(filename
, ":12:")) {
1133 if (strstr(filename
, ":floppy:")) {
1137 if (strstr(filename
, ":rw:")) {
1141 /* Get the directory name without options */
1142 i
= strrchr(filename
, ':') - filename
;
1144 if (filename
[i
- 2] == ':' && qemu_isalpha(filename
[i
- 1])) {
1145 /* workaround for DOS drive names */
1151 /* Fill in the options QDict */
1152 qdict_put_str(options
, "dir", filename
);
1153 qdict_put_int(options
, "fat-type", fat_type
);
1154 qdict_put_bool(options
, "floppy", floppy
);
1155 qdict_put_bool(options
, "rw", rw
);
1158 static int vvfat_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1161 BDRVVVFATState
*s
= bs
->opaque
;
1162 int cyls
, heads
, secs
;
1164 const char *dirname
, *label
;
1166 Error
*local_err
= NULL
;
1173 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1174 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1176 error_propagate(errp
, local_err
);
1181 dirname
= qemu_opt_get(opts
, "dir");
1183 error_setg(errp
, "vvfat block driver requires a 'dir' option");
1188 s
->fat_type
= qemu_opt_get_number(opts
, "fat-type", 0);
1189 floppy
= qemu_opt_get_bool(opts
, "floppy", false);
1191 memset(s
->volume_label
, ' ', sizeof(s
->volume_label
));
1192 label
= qemu_opt_get(opts
, "label");
1194 size_t label_length
= strlen(label
);
1195 if (label_length
> 11) {
1196 error_setg(errp
, "vvfat label cannot be longer than 11 bytes");
1200 memcpy(s
->volume_label
, label
, label_length
);
1202 memcpy(s
->volume_label
, "QEMU VVFAT", 10);
1206 /* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
1210 s
->sectors_per_cluster
= 2;
1212 secs
= s
->fat_type
== 12 ? 18 : 36;
1213 s
->sectors_per_cluster
= 1;
1218 /* 32MB or 504MB disk*/
1222 s
->offset_to_bootsector
= 0x3f;
1223 cyls
= s
->fat_type
== 12 ? 64 : 1024;
1228 switch (s
->fat_type
) {
1230 warn_report("FAT32 has not been tested. You are welcome to do so!");
1236 error_setg(errp
, "Valid FAT types are only 12, 16 and 32");
1244 /* LATER TODO: if FAT32, adjust */
1245 s
->sectors_per_cluster
=0x10;
1247 s
->current_cluster
=0xffffffff;
1250 s
->qcow_filename
= NULL
;
1252 s
->downcase_short_names
= 1;
1254 fprintf(stderr
, "vvfat %s chs %d,%d,%d\n",
1255 dirname
, cyls
, heads
, secs
);
1257 s
->sector_count
= cyls
* heads
* secs
- s
->offset_to_bootsector
;
1259 if (qemu_opt_get_bool(opts
, "rw", false)) {
1260 if (!bdrv_is_read_only(bs
)) {
1261 ret
= enable_write_target(bs
, errp
);
1268 "Unable to set VVFAT to 'rw' when drive is read-only");
1272 /* read only is the default for safety */
1273 ret
= bdrv_set_read_only(bs
, true, &local_err
);
1275 error_propagate(errp
, local_err
);
1280 bs
->total_sectors
= cyls
* heads
* secs
;
1282 if (init_directories(s
, dirname
, heads
, secs
, errp
)) {
1287 s
->sector_count
= s
->offset_to_root_dir
1288 + s
->sectors_per_cluster
* s
->cluster_count
;
1290 /* Disable migration when vvfat is used rw */
1292 error_setg(&s
->migration_blocker
,
1293 "The vvfat (rw) format used by node '%s' "
1294 "does not support live migration",
1295 bdrv_get_device_or_node_name(bs
));
1296 ret
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1298 error_propagate(errp
, local_err
);
1299 error_free(s
->migration_blocker
);
1304 if (s
->offset_to_bootsector
> 0) {
1305 init_mbr(s
, cyls
, heads
, secs
);
1308 qemu_co_mutex_init(&s
->lock
);
1312 qemu_opts_del(opts
);
1316 static void vvfat_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1318 bs
->bl
.request_alignment
= BDRV_SECTOR_SIZE
; /* No sub-sector I/O */
1321 static inline void vvfat_close_current_file(BDRVVVFATState
*s
)
1323 if(s
->current_mapping
) {
1324 s
->current_mapping
= NULL
;
1325 if (s
->current_fd
) {
1326 qemu_close(s
->current_fd
);
1330 s
->current_cluster
= -1;
1333 /* mappings between index1 and index2-1 are supposed to be ordered
1334 * return value is the index of the last mapping for which end>cluster_num
1336 static inline int find_mapping_for_cluster_aux(BDRVVVFATState
* s
,int cluster_num
,int index1
,int index2
)
1341 index3
=(index1
+index2
)/2;
1342 mapping
=array_get(&(s
->mapping
),index3
);
1343 assert(mapping
->begin
< mapping
->end
);
1344 if(mapping
->begin
>=cluster_num
) {
1345 assert(index2
!=index3
|| index2
==0);
1351 return mapping
->end
<=cluster_num
? index2
: index1
;
1354 assert(index1
<=index2
);
1355 DLOG(mapping
=array_get(&(s
->mapping
),index1
);
1356 assert(mapping
->begin
<=cluster_num
);
1357 assert(index2
>= s
->mapping
.next
||
1358 ((mapping
= array_get(&(s
->mapping
),index2
)) &&
1359 mapping
->end
>cluster_num
)));
1363 static inline mapping_t
* find_mapping_for_cluster(BDRVVVFATState
* s
,int cluster_num
)
1365 int index
=find_mapping_for_cluster_aux(s
,cluster_num
,0,s
->mapping
.next
);
1367 if(index
>=s
->mapping
.next
)
1369 mapping
=array_get(&(s
->mapping
),index
);
1370 if(mapping
->begin
>cluster_num
)
1372 assert(mapping
->begin
<=cluster_num
&& mapping
->end
>cluster_num
);
1376 static int open_file(BDRVVVFATState
* s
,mapping_t
* mapping
)
1380 if(!s
->current_mapping
||
1381 strcmp(s
->current_mapping
->path
,mapping
->path
)) {
1383 int fd
= qemu_open(mapping
->path
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1386 vvfat_close_current_file(s
);
1388 s
->current_mapping
= mapping
;
1393 static inline int read_cluster(BDRVVVFATState
*s
,int cluster_num
)
1395 if(s
->current_cluster
!= cluster_num
) {
1398 assert(!s
->current_mapping
|| s
->current_fd
|| (s
->current_mapping
->mode
& MODE_DIRECTORY
));
1399 if(!s
->current_mapping
1400 || s
->current_mapping
->begin
>cluster_num
1401 || s
->current_mapping
->end
<=cluster_num
) {
1402 /* binary search of mappings for file */
1403 mapping_t
* mapping
=find_mapping_for_cluster(s
,cluster_num
);
1405 assert(!mapping
|| (cluster_num
>=mapping
->begin
&& cluster_num
<mapping
->end
));
1407 if (mapping
&& mapping
->mode
& MODE_DIRECTORY
) {
1408 vvfat_close_current_file(s
);
1409 s
->current_mapping
= mapping
;
1410 read_cluster_directory
:
1411 offset
= s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
);
1412 s
->cluster
= (unsigned char*)s
->directory
.pointer
+offset
1413 + 0x20*s
->current_mapping
->info
.dir
.first_dir_index
;
1414 assert(((s
->cluster
-(unsigned char*)s
->directory
.pointer
)%s
->cluster_size
)==0);
1415 assert((char*)s
->cluster
+s
->cluster_size
<= s
->directory
.pointer
+s
->directory
.next
*s
->directory
.item_size
);
1416 s
->current_cluster
= cluster_num
;
1420 if(open_file(s
,mapping
))
1422 } else if (s
->current_mapping
->mode
& MODE_DIRECTORY
)
1423 goto read_cluster_directory
;
1425 assert(s
->current_fd
);
1427 offset
=s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
)+s
->current_mapping
->info
.file
.offset
;
1428 if(lseek(s
->current_fd
, offset
, SEEK_SET
)!=offset
)
1430 s
->cluster
=s
->cluster_buffer
;
1431 result
=read(s
->current_fd
,s
->cluster
,s
->cluster_size
);
1433 s
->current_cluster
= -1;
1436 s
->current_cluster
= cluster_num
;
1442 static void print_direntry(const direntry_t
* direntry
)
1447 fprintf(stderr
, "direntry %p: ", direntry
);
1450 if(is_long_name(direntry
)) {
1451 unsigned char* c
=(unsigned char*)direntry
;
1453 for(i
=1;i
<11 && c
[i
] && c
[i
]!=0xff;i
+=2)
1454 #define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
1456 for(i
=14;i
<26 && c
[i
] && c
[i
]!=0xff;i
+=2)
1458 for(i
=28;i
<32 && c
[i
] && c
[i
]!=0xff;i
+=2)
1461 fprintf(stderr
, "%s\n", buffer
);
1465 ADD_CHAR(direntry
->name
[i
]);
1467 fprintf(stderr
,"%s attributes=0x%02x begin=%d size=%d\n",
1469 direntry
->attributes
,
1470 begin_of_direntry(direntry
),le32_to_cpu(direntry
->size
));
1474 static void print_mapping(const mapping_t
* mapping
)
1476 fprintf(stderr
, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1477 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1478 mapping
, mapping
->begin
, mapping
->end
, mapping
->dir_index
,
1479 mapping
->first_mapping_index
, mapping
->path
, mapping
->mode
);
1481 if (mapping
->mode
& MODE_DIRECTORY
)
1482 fprintf(stderr
, "parent_mapping_index = %d, first_dir_index = %d\n", mapping
->info
.dir
.parent_mapping_index
, mapping
->info
.dir
.first_dir_index
);
1484 fprintf(stderr
, "offset = %d\n", mapping
->info
.file
.offset
);
1488 static int vvfat_read(BlockDriverState
*bs
, int64_t sector_num
,
1489 uint8_t *buf
, int nb_sectors
)
1491 BDRVVVFATState
*s
= bs
->opaque
;
1494 for(i
=0;i
<nb_sectors
;i
++,sector_num
++) {
1495 if (sector_num
>= bs
->total_sectors
)
1500 ret
= bdrv_is_allocated(s
->qcow
->bs
, sector_num
* BDRV_SECTOR_SIZE
,
1501 (nb_sectors
- i
) * BDRV_SECTOR_SIZE
, &n
);
1506 DLOG(fprintf(stderr
, "sectors %" PRId64
"+%" PRId64
1507 " allocated\n", sector_num
,
1508 n
>> BDRV_SECTOR_BITS
));
1509 if (bdrv_read(s
->qcow
, sector_num
, buf
+ i
* 0x200,
1510 n
>> BDRV_SECTOR_BITS
)) {
1513 i
+= (n
>> BDRV_SECTOR_BITS
) - 1;
1514 sector_num
+= (n
>> BDRV_SECTOR_BITS
) - 1;
1517 DLOG(fprintf(stderr
, "sector %" PRId64
" not allocated\n",
1520 if (sector_num
< s
->offset_to_root_dir
) {
1521 if (sector_num
< s
->offset_to_fat
) {
1522 memcpy(buf
+ i
* 0x200,
1523 &(s
->first_sectors
[sector_num
* 0x200]),
1525 } else if (sector_num
< s
->offset_to_fat
+ s
->sectors_per_fat
) {
1526 memcpy(buf
+ i
* 0x200,
1527 &(s
->fat
.pointer
[(sector_num
1528 - s
->offset_to_fat
) * 0x200]),
1530 } else if (sector_num
< s
->offset_to_root_dir
) {
1531 memcpy(buf
+ i
* 0x200,
1532 &(s
->fat
.pointer
[(sector_num
- s
->offset_to_fat
1533 - s
->sectors_per_fat
) * 0x200]),
1537 uint32_t sector
= sector_num
- s
->offset_to_root_dir
,
1538 sector_offset_in_cluster
=(sector
%s
->sectors_per_cluster
),
1539 cluster_num
=sector
/s
->sectors_per_cluster
;
1540 if(cluster_num
> s
->cluster_count
|| read_cluster(s
, cluster_num
) != 0) {
1541 /* LATER TODO: strict: return -1; */
1542 memset(buf
+i
*0x200,0,0x200);
1545 memcpy(buf
+i
*0x200,s
->cluster
+sector_offset_in_cluster
*0x200,0x200);
1551 static int coroutine_fn
1552 vvfat_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1553 QEMUIOVector
*qiov
, int flags
)
1556 BDRVVVFATState
*s
= bs
->opaque
;
1557 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
1558 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1561 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
1562 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
1564 buf
= g_try_malloc(bytes
);
1565 if (bytes
&& buf
== NULL
) {
1569 qemu_co_mutex_lock(&s
->lock
);
1570 ret
= vvfat_read(bs
, sector_num
, buf
, nb_sectors
);
1571 qemu_co_mutex_unlock(&s
->lock
);
1573 qemu_iovec_from_buf(qiov
, 0, buf
, bytes
);
1579 /* LATER TODO: statify all functions */
1582 * Idea of the write support (use snapshot):
1584 * 1. check if all data is consistent, recording renames, modifications,
1585 * new files and directories (in s->commits).
1587 * 2. if the data is not consistent, stop committing
1589 * 3. handle renames, and create new files and directories (do not yet
1590 * write their contents)
1592 * 4. walk the directories, fixing the mapping and direntries, and marking
1593 * the handled mappings as not deleted
1595 * 5. commit the contents of the files
1597 * 6. handle deleted files and directories
1601 typedef struct commit_t
{
1604 struct { uint32_t cluster
; } rename
;
1605 struct { int dir_index
; uint32_t modified_offset
; } writeout
;
1606 struct { uint32_t first_cluster
; } new_file
;
1607 struct { uint32_t cluster
; } mkdir
;
1609 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1611 ACTION_RENAME
, ACTION_WRITEOUT
, ACTION_NEW_FILE
, ACTION_MKDIR
1615 static void clear_commits(BDRVVVFATState
* s
)
1618 DLOG(fprintf(stderr
, "clear_commits (%d commits)\n", s
->commits
.next
));
1619 for (i
= 0; i
< s
->commits
.next
; i
++) {
1620 commit_t
* commit
= array_get(&(s
->commits
), i
);
1621 assert(commit
->path
|| commit
->action
== ACTION_WRITEOUT
);
1622 if (commit
->action
!= ACTION_WRITEOUT
) {
1623 assert(commit
->path
);
1624 g_free(commit
->path
);
1626 assert(commit
->path
== NULL
);
1628 s
->commits
.next
= 0;
1631 static void schedule_rename(BDRVVVFATState
* s
,
1632 uint32_t cluster
, char* new_path
)
1634 commit_t
* commit
= array_get_next(&(s
->commits
));
1635 commit
->path
= new_path
;
1636 commit
->param
.rename
.cluster
= cluster
;
1637 commit
->action
= ACTION_RENAME
;
1640 static void schedule_writeout(BDRVVVFATState
* s
,
1641 int dir_index
, uint32_t modified_offset
)
1643 commit_t
* commit
= array_get_next(&(s
->commits
));
1644 commit
->path
= NULL
;
1645 commit
->param
.writeout
.dir_index
= dir_index
;
1646 commit
->param
.writeout
.modified_offset
= modified_offset
;
1647 commit
->action
= ACTION_WRITEOUT
;
1650 static void schedule_new_file(BDRVVVFATState
* s
,
1651 char* path
, uint32_t first_cluster
)
1653 commit_t
* commit
= array_get_next(&(s
->commits
));
1654 commit
->path
= path
;
1655 commit
->param
.new_file
.first_cluster
= first_cluster
;
1656 commit
->action
= ACTION_NEW_FILE
;
1659 static void schedule_mkdir(BDRVVVFATState
* s
, uint32_t cluster
, char* path
)
1661 commit_t
* commit
= array_get_next(&(s
->commits
));
1662 commit
->path
= path
;
1663 commit
->param
.mkdir
.cluster
= cluster
;
1664 commit
->action
= ACTION_MKDIR
;
1669 * Since the sequence number is at most 0x3f, and the filename
1670 * length is at most 13 times the sequence number, the maximal
1671 * filename length is 0x3f * 13 bytes.
1673 unsigned char name
[0x3f * 13 + 1];
1674 gunichar2 name2
[0x3f * 13 + 1];
1676 int sequence_number
;
1679 static void lfn_init(long_file_name
* lfn
)
1681 lfn
->sequence_number
= lfn
->len
= 0;
1682 lfn
->checksum
= 0x100;
1685 /* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1686 static int parse_long_name(long_file_name
* lfn
,
1687 const direntry_t
* direntry
)
1690 const unsigned char* pointer
= (const unsigned char*)direntry
;
1692 if (!is_long_name(direntry
))
1695 if (pointer
[0] & 0x40) {
1696 /* first entry; do some initialization */
1697 lfn
->sequence_number
= pointer
[0] & 0x3f;
1698 lfn
->checksum
= pointer
[13];
1700 lfn
->name
[lfn
->sequence_number
* 13] = 0;
1701 } else if ((pointer
[0] & 0x3f) != --lfn
->sequence_number
) {
1702 /* not the expected sequence number */
1704 } else if (pointer
[13] != lfn
->checksum
) {
1705 /* not the expected checksum */
1707 } else if (pointer
[12] || pointer
[26] || pointer
[27]) {
1708 /* invalid zero fields */
1712 offset
= 13 * (lfn
->sequence_number
- 1);
1713 for (i
= 0, j
= 1; i
< 13; i
++, j
+=2) {
1719 if (pointer
[j
] == 0 && pointer
[j
+ 1] == 0) {
1720 /* end of long file name */
1723 gunichar2 c
= (pointer
[j
+ 1] << 8) + pointer
[j
];
1724 lfn
->name2
[offset
+ i
] = c
;
1727 if (pointer
[0] & 0x40) {
1728 /* first entry; set len */
1729 lfn
->len
= offset
+ i
;
1731 if ((pointer
[0] & 0x3f) == 0x01) {
1732 /* last entry; finalize entry */
1734 gchar
*utf8
= g_utf16_to_utf8(lfn
->name2
, lfn
->len
, NULL
, &olen
, NULL
);
1739 memcpy(lfn
->name
, utf8
, olen
+ 1);
1746 /* returns 0 if successful, >0 if no short_name, and <0 on error */
1747 static int parse_short_name(BDRVVVFATState
* s
,
1748 long_file_name
* lfn
, direntry_t
* direntry
)
1752 if (!is_short_name(direntry
))
1755 for (j
= 7; j
>= 0 && direntry
->name
[j
] == ' '; j
--);
1756 for (i
= 0; i
<= j
; i
++) {
1757 uint8_t c
= direntry
->name
[i
];
1758 if (c
!= to_valid_short_char(c
)) {
1760 } else if (s
->downcase_short_names
) {
1761 lfn
->name
[i
] = qemu_tolower(direntry
->name
[i
]);
1763 lfn
->name
[i
] = direntry
->name
[i
];
1767 for (j
= 2; j
>= 0 && direntry
->name
[8 + j
] == ' '; j
--) {
1770 lfn
->name
[i
++] = '.';
1771 lfn
->name
[i
+ j
+ 1] = '\0';
1772 for (;j
>= 0; j
--) {
1773 uint8_t c
= direntry
->name
[8 + j
];
1774 if (c
!= to_valid_short_char(c
)) {
1776 } else if (s
->downcase_short_names
) {
1777 lfn
->name
[i
+ j
] = qemu_tolower(c
);
1779 lfn
->name
[i
+ j
] = c
;
1783 lfn
->name
[i
+ j
+ 1] = '\0';
1785 if (lfn
->name
[0] == DIR_KANJI_FAKE
) {
1786 lfn
->name
[0] = DIR_KANJI
;
1788 lfn
->len
= strlen((char*)lfn
->name
);
1793 static inline uint32_t modified_fat_get(BDRVVVFATState
* s
,
1794 unsigned int cluster
)
1796 if (cluster
< s
->last_cluster_of_root_directory
) {
1797 if (cluster
+ 1 == s
->last_cluster_of_root_directory
)
1798 return s
->max_fat_value
;
1803 if (s
->fat_type
==32) {
1804 uint32_t* entry
=((uint32_t*)s
->fat2
)+cluster
;
1805 return le32_to_cpu(*entry
);
1806 } else if (s
->fat_type
==16) {
1807 uint16_t* entry
=((uint16_t*)s
->fat2
)+cluster
;
1808 return le16_to_cpu(*entry
);
1810 const uint8_t* x
=s
->fat2
+cluster
*3/2;
1811 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
1815 static inline bool cluster_was_modified(BDRVVVFATState
*s
,
1816 uint32_t cluster_num
)
1818 int was_modified
= 0;
1821 if (s
->qcow
== NULL
) {
1825 for (i
= 0; !was_modified
&& i
< s
->sectors_per_cluster
; i
++) {
1826 was_modified
= bdrv_is_allocated(s
->qcow
->bs
,
1827 (cluster2sector(s
, cluster_num
) +
1828 i
) * BDRV_SECTOR_SIZE
,
1829 BDRV_SECTOR_SIZE
, NULL
);
1833 * Note that this treats failures to learn allocation status the
1834 * same as if an allocation has occurred. It's as safe as
1835 * anything else, given that a failure to learn allocation status
1836 * will probably result in more failures.
1838 return !!was_modified
;
1841 static const char* get_basename(const char* path
)
1843 char* basename
= strrchr(path
, '/');
1844 if (basename
== NULL
)
1847 return basename
+ 1; /* strip '/' */
1851 * The array s->used_clusters holds the states of the clusters. If it is
1852 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1853 * was modified, bit 3 is set.
1854 * If any cluster is allocated, but not part of a file or directory, this
1855 * driver refuses to commit.
1858 USED_DIRECTORY
= 1, USED_FILE
= 2, USED_ANY
= 3, USED_ALLOCATED
= 4
1862 * get_cluster_count_for_direntry() not only determines how many clusters
1863 * are occupied by direntry, but also if it was renamed or modified.
1865 * A file is thought to be renamed *only* if there already was a file with
1866 * exactly the same first cluster, but a different name.
1868 * Further, the files/directories handled by this function are
1869 * assumed to be *not* deleted (and *only* those).
1871 static uint32_t get_cluster_count_for_direntry(BDRVVVFATState
* s
,
1872 direntry_t
* direntry
, const char* path
)
1875 * This is a little bit tricky:
1876 * IF the guest OS just inserts a cluster into the file chain,
1877 * and leaves the rest alone, (i.e. the original file had clusters
1878 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1880 * - do_commit will write the cluster into the file at the given
1883 * - the cluster which is overwritten should be moved to a later
1884 * position in the file.
1886 * I am not aware that any OS does something as braindead, but this
1887 * situation could happen anyway when not committing for a long time.
1888 * Just to be sure that this does not bite us, detect it, and copy the
1889 * contents of the clusters to-be-overwritten into the qcow.
1892 int was_modified
= 0;
1895 uint32_t cluster_num
= begin_of_direntry(direntry
);
1896 uint32_t offset
= 0;
1897 int first_mapping_index
= -1;
1898 mapping_t
* mapping
= NULL
;
1899 const char* basename2
= NULL
;
1901 vvfat_close_current_file(s
);
1903 /* the root directory */
1904 if (cluster_num
== 0)
1909 basename2
= get_basename(path
);
1911 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1914 const char* basename
;
1916 assert(mapping
->mode
& MODE_DELETED
);
1917 mapping
->mode
&= ~MODE_DELETED
;
1919 basename
= get_basename(mapping
->path
);
1921 assert(mapping
->mode
& MODE_NORMAL
);
1924 if (strcmp(basename
, basename2
))
1925 schedule_rename(s
, cluster_num
, g_strdup(path
));
1926 } else if (is_file(direntry
))
1928 schedule_new_file(s
, g_strdup(path
), cluster_num
);
1937 if (!copy_it
&& cluster_was_modified(s
, cluster_num
)) {
1938 if (mapping
== NULL
||
1939 mapping
->begin
> cluster_num
||
1940 mapping
->end
<= cluster_num
)
1941 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1945 (mapping
->mode
& MODE_DIRECTORY
) == 0) {
1947 /* was modified in qcow */
1948 if (offset
!= mapping
->info
.file
.offset
+ s
->cluster_size
1949 * (cluster_num
- mapping
->begin
)) {
1950 /* offset of this cluster in file chain has changed */
1953 } else if (offset
== 0) {
1954 const char* basename
= get_basename(mapping
->path
);
1956 if (strcmp(basename
, basename2
))
1958 first_mapping_index
= array_index(&(s
->mapping
), mapping
);
1961 if (mapping
->first_mapping_index
!= first_mapping_index
1962 && mapping
->info
.file
.offset
> 0) {
1967 /* need to write out? */
1968 if (!was_modified
&& is_file(direntry
)) {
1970 schedule_writeout(s
, mapping
->dir_index
, offset
);
1978 * This is horribly inefficient, but that is okay, since
1979 * it is rarely executed, if at all.
1981 int64_t offset
= cluster2sector(s
, cluster_num
);
1983 vvfat_close_current_file(s
);
1984 for (i
= 0; i
< s
->sectors_per_cluster
; i
++) {
1987 res
= bdrv_is_allocated(s
->qcow
->bs
,
1988 (offset
+ i
) * BDRV_SECTOR_SIZE
,
1989 BDRV_SECTOR_SIZE
, NULL
);
1994 res
= vvfat_read(s
->bs
, offset
, s
->cluster_buffer
, 1);
1998 res
= bdrv_write(s
->qcow
, offset
, s
->cluster_buffer
, 1);
2008 if (s
->used_clusters
[cluster_num
] & USED_ANY
)
2010 s
->used_clusters
[cluster_num
] = USED_FILE
;
2012 cluster_num
= modified_fat_get(s
, cluster_num
);
2014 if (fat_eof(s
, cluster_num
))
2016 else if (cluster_num
< 2 || cluster_num
> s
->max_fat_value
- 16)
2019 offset
+= s
->cluster_size
;
2024 * This function looks at the modified data (qcow).
2025 * It returns 0 upon inconsistency or error, and the number of clusters
2026 * used by the directory, its subdirectories and their files.
2028 static int check_directory_consistency(BDRVVVFATState
*s
,
2029 int cluster_num
, const char* path
)
2032 unsigned char* cluster
= g_malloc(s
->cluster_size
);
2033 direntry_t
* direntries
= (direntry_t
*)cluster
;
2034 mapping_t
* mapping
= find_mapping_for_cluster(s
, cluster_num
);
2037 int path_len
= strlen(path
);
2038 char path2
[PATH_MAX
+ 1];
2040 assert(path_len
< PATH_MAX
); /* len was tested before! */
2041 pstrcpy(path2
, sizeof(path2
), path
);
2042 path2
[path_len
] = '/';
2043 path2
[path_len
+ 1] = '\0';
2046 const char* basename
= get_basename(mapping
->path
);
2047 const char* basename2
= get_basename(path
);
2049 assert(mapping
->mode
& MODE_DIRECTORY
);
2051 assert(mapping
->mode
& MODE_DELETED
);
2052 mapping
->mode
&= ~MODE_DELETED
;
2054 if (strcmp(basename
, basename2
))
2055 schedule_rename(s
, cluster_num
, g_strdup(path
));
2058 schedule_mkdir(s
, cluster_num
, g_strdup(path
));
2067 if (s
->used_clusters
[cluster_num
] & USED_ANY
) {
2068 fprintf(stderr
, "cluster %d used more than once\n", (int)cluster_num
);
2071 s
->used_clusters
[cluster_num
] = USED_DIRECTORY
;
2073 DLOG(fprintf(stderr
, "read cluster %d (sector %d)\n", (int)cluster_num
, (int)cluster2sector(s
, cluster_num
)));
2074 subret
= vvfat_read(s
->bs
, cluster2sector(s
, cluster_num
), cluster
,
2075 s
->sectors_per_cluster
);
2077 fprintf(stderr
, "Error fetching direntries\n");
2083 for (i
= 0; i
< 0x10 * s
->sectors_per_cluster
; i
++) {
2084 int cluster_count
= 0;
2086 DLOG(fprintf(stderr
, "check direntry %d:\n", i
); print_direntry(direntries
+ i
));
2087 if (is_volume_label(direntries
+ i
) || is_dot(direntries
+ i
) ||
2088 is_free(direntries
+ i
))
2091 subret
= parse_long_name(&lfn
, direntries
+ i
);
2093 fprintf(stderr
, "Error in long name\n");
2096 if (subret
== 0 || is_free(direntries
+ i
))
2099 if (fat_chksum(direntries
+i
) != lfn
.checksum
) {
2100 subret
= parse_short_name(s
, &lfn
, direntries
+ i
);
2102 fprintf(stderr
, "Error in short name (%d)\n", subret
);
2105 if (subret
> 0 || !strcmp((char*)lfn
.name
, ".")
2106 || !strcmp((char*)lfn
.name
, ".."))
2109 lfn
.checksum
= 0x100; /* cannot use long name twice */
2111 if (path_len
+ 1 + lfn
.len
>= PATH_MAX
) {
2112 fprintf(stderr
, "Name too long: %s/%s\n", path
, lfn
.name
);
2115 pstrcpy(path2
+ path_len
+ 1, sizeof(path2
) - path_len
- 1,
2118 if (is_directory(direntries
+ i
)) {
2119 if (begin_of_direntry(direntries
+ i
) == 0) {
2120 DLOG(fprintf(stderr
, "invalid begin for directory: %s\n", path2
); print_direntry(direntries
+ i
));
2123 cluster_count
= check_directory_consistency(s
,
2124 begin_of_direntry(direntries
+ i
), path2
);
2125 if (cluster_count
== 0) {
2126 DLOG(fprintf(stderr
, "problem in directory %s:\n", path2
); print_direntry(direntries
+ i
));
2129 } else if (is_file(direntries
+ i
)) {
2130 /* check file size with FAT */
2131 cluster_count
= get_cluster_count_for_direntry(s
, direntries
+ i
, path2
);
2132 if (cluster_count
!=
2133 DIV_ROUND_UP(le32_to_cpu(direntries
[i
].size
), s
->cluster_size
)) {
2134 DLOG(fprintf(stderr
, "Cluster count mismatch\n"));
2138 abort(); /* cluster_count = 0; */
2140 ret
+= cluster_count
;
2143 cluster_num
= modified_fat_get(s
, cluster_num
);
2144 } while(!fat_eof(s
, cluster_num
));
2150 /* returns 1 on success */
2151 static int is_consistent(BDRVVVFATState
* s
)
2154 int used_clusters_count
= 0;
2158 * - get modified FAT
2159 * - compare the two FATs (TODO)
2160 * - get buffer for marking used clusters
2161 * - recurse direntries from root (using bs->bdrv_read to make
2162 * sure to get the new data)
2163 * - check that the FAT agrees with the size
2164 * - count the number of clusters occupied by this directory and
2166 * - check that the cumulative used cluster count agrees with the
2168 * - if all is fine, return number of used clusters
2170 if (s
->fat2
== NULL
) {
2171 int size
= 0x200 * s
->sectors_per_fat
;
2172 s
->fat2
= g_malloc(size
);
2173 memcpy(s
->fat2
, s
->fat
.pointer
, size
);
2175 check
= vvfat_read(s
->bs
,
2176 s
->offset_to_fat
, s
->fat2
, s
->sectors_per_fat
);
2178 fprintf(stderr
, "Could not copy fat\n");
2181 assert (s
->used_clusters
);
2182 for (i
= 0; i
< sector2cluster(s
, s
->sector_count
); i
++)
2183 s
->used_clusters
[i
] &= ~USED_ANY
;
2187 /* mark every mapped file/directory as deleted.
2188 * (check_directory_consistency() will unmark those still present). */
2190 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2191 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2192 if (mapping
->first_mapping_index
< 0)
2193 mapping
->mode
|= MODE_DELETED
;
2196 used_clusters_count
= check_directory_consistency(s
, 0, s
->path
);
2197 if (used_clusters_count
<= 0) {
2198 DLOG(fprintf(stderr
, "problem in directory\n"));
2202 check
= s
->last_cluster_of_root_directory
;
2203 for (i
= check
; i
< sector2cluster(s
, s
->sector_count
); i
++) {
2204 if (modified_fat_get(s
, i
)) {
2205 if(!s
->used_clusters
[i
]) {
2206 DLOG(fprintf(stderr
, "FAT was modified (%d), but cluster is not used?\n", i
));
2212 if (s
->used_clusters
[i
] == USED_ALLOCATED
) {
2213 /* allocated, but not used... */
2214 DLOG(fprintf(stderr
, "unused, modified cluster: %d\n", i
));
2219 if (check
!= used_clusters_count
)
2222 return used_clusters_count
;
2225 static inline void adjust_mapping_indices(BDRVVVFATState
* s
,
2226 int offset
, int adjust
)
2230 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2231 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2233 #define ADJUST_MAPPING_INDEX(name) \
2234 if (mapping->name >= offset) \
2235 mapping->name += adjust
2237 ADJUST_MAPPING_INDEX(first_mapping_index
);
2238 if (mapping
->mode
& MODE_DIRECTORY
)
2239 ADJUST_MAPPING_INDEX(info
.dir
.parent_mapping_index
);
2243 /* insert or update mapping */
2244 static mapping_t
* insert_mapping(BDRVVVFATState
* s
,
2245 uint32_t begin
, uint32_t end
)
2248 * - find mapping where mapping->begin >= begin,
2249 * - if mapping->begin > begin: insert
2250 * - adjust all references to mappings!
2254 int index
= find_mapping_for_cluster_aux(s
, begin
, 0, s
->mapping
.next
);
2255 mapping_t
* mapping
= NULL
;
2256 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
2258 if (index
< s
->mapping
.next
&& (mapping
= array_get(&(s
->mapping
), index
))
2259 && mapping
->begin
< begin
) {
2260 mapping
->end
= begin
;
2262 mapping
= array_get(&(s
->mapping
), index
);
2264 if (index
>= s
->mapping
.next
|| mapping
->begin
> begin
) {
2265 mapping
= array_insert(&(s
->mapping
), index
, 1);
2266 mapping
->path
= NULL
;
2267 adjust_mapping_indices(s
, index
, +1);
2270 mapping
->begin
= begin
;
2273 DLOG(mapping_t
* next_mapping
;
2274 assert(index
+ 1 >= s
->mapping
.next
||
2275 ((next_mapping
= array_get(&(s
->mapping
), index
+ 1)) &&
2276 next_mapping
->begin
>= end
)));
2278 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
2279 s
->current_mapping
= array_get(&(s
->mapping
),
2280 s
->current_mapping
- first_mapping
);
2285 static int remove_mapping(BDRVVVFATState
* s
, int mapping_index
)
2287 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
2288 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
2291 if (mapping
->first_mapping_index
< 0) {
2292 g_free(mapping
->path
);
2295 /* remove from s->mapping */
2296 array_remove(&(s
->mapping
), mapping_index
);
2298 /* adjust all references to mappings */
2299 adjust_mapping_indices(s
, mapping_index
, -1);
2301 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
2302 s
->current_mapping
= array_get(&(s
->mapping
),
2303 s
->current_mapping
- first_mapping
);
2308 static void adjust_dirindices(BDRVVVFATState
* s
, int offset
, int adjust
)
2311 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2312 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2313 if (mapping
->dir_index
>= offset
)
2314 mapping
->dir_index
+= adjust
;
2315 if ((mapping
->mode
& MODE_DIRECTORY
) &&
2316 mapping
->info
.dir
.first_dir_index
>= offset
)
2317 mapping
->info
.dir
.first_dir_index
+= adjust
;
2321 static direntry_t
* insert_direntries(BDRVVVFATState
* s
,
2322 int dir_index
, int count
)
2325 * make room in s->directory,
2328 direntry_t
* result
= array_insert(&(s
->directory
), dir_index
, count
);
2331 adjust_dirindices(s
, dir_index
, count
);
2335 static int remove_direntries(BDRVVVFATState
* s
, int dir_index
, int count
)
2337 int ret
= array_remove_slice(&(s
->directory
), dir_index
, count
);
2340 adjust_dirindices(s
, dir_index
, -count
);
2345 * Adapt the mappings of the cluster chain starting at first cluster
2346 * (i.e. if a file starts at first_cluster, the chain is followed according
2347 * to the modified fat, and the corresponding entries in s->mapping are
2350 static int commit_mappings(BDRVVVFATState
* s
,
2351 uint32_t first_cluster
, int dir_index
)
2353 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2354 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2355 uint32_t cluster
= first_cluster
;
2357 vvfat_close_current_file(s
);
2360 assert(mapping
->begin
== first_cluster
);
2361 mapping
->first_mapping_index
= -1;
2362 mapping
->dir_index
= dir_index
;
2363 mapping
->mode
= (dir_index
<= 0 || is_directory(direntry
)) ?
2364 MODE_DIRECTORY
: MODE_NORMAL
;
2366 while (!fat_eof(s
, cluster
)) {
2369 for (c
= cluster
, c1
= modified_fat_get(s
, c
); c
+ 1 == c1
;
2370 c
= c1
, c1
= modified_fat_get(s
, c1
));
2373 if (c
> mapping
->end
) {
2374 int index
= array_index(&(s
->mapping
), mapping
);
2375 int i
, max_i
= s
->mapping
.next
- index
;
2376 for (i
= 1; i
< max_i
&& mapping
[i
].begin
< c
; i
++);
2378 remove_mapping(s
, index
+ 1);
2380 assert(mapping
== array_get(&(s
->mapping
), s
->mapping
.next
- 1)
2381 || mapping
[1].begin
>= c
);
2384 if (!fat_eof(s
, c1
)) {
2385 int i
= find_mapping_for_cluster_aux(s
, c1
, 0, s
->mapping
.next
);
2386 mapping_t
* next_mapping
= i
>= s
->mapping
.next
? NULL
:
2387 array_get(&(s
->mapping
), i
);
2389 if (next_mapping
== NULL
|| next_mapping
->begin
> c1
) {
2390 int i1
= array_index(&(s
->mapping
), mapping
);
2392 next_mapping
= insert_mapping(s
, c1
, c1
+1);
2396 mapping
= array_get(&(s
->mapping
), i1
);
2399 next_mapping
->dir_index
= mapping
->dir_index
;
2400 next_mapping
->first_mapping_index
=
2401 mapping
->first_mapping_index
< 0 ?
2402 array_index(&(s
->mapping
), mapping
) :
2403 mapping
->first_mapping_index
;
2404 next_mapping
->path
= mapping
->path
;
2405 next_mapping
->mode
= mapping
->mode
;
2406 next_mapping
->read_only
= mapping
->read_only
;
2407 if (mapping
->mode
& MODE_DIRECTORY
) {
2408 next_mapping
->info
.dir
.parent_mapping_index
=
2409 mapping
->info
.dir
.parent_mapping_index
;
2410 next_mapping
->info
.dir
.first_dir_index
=
2411 mapping
->info
.dir
.first_dir_index
+
2412 0x10 * s
->sectors_per_cluster
*
2413 (mapping
->end
- mapping
->begin
);
2415 next_mapping
->info
.file
.offset
= mapping
->info
.file
.offset
+
2416 mapping
->end
- mapping
->begin
;
2418 mapping
= next_mapping
;
2427 static int commit_direntries(BDRVVVFATState
* s
,
2428 int dir_index
, int parent_mapping_index
)
2430 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2431 uint32_t first_cluster
= dir_index
== 0 ? 0 : begin_of_direntry(direntry
);
2432 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2434 int factor
= 0x10 * s
->sectors_per_cluster
;
2435 int old_cluster_count
, new_cluster_count
;
2436 int current_dir_index
= mapping
->info
.dir
.first_dir_index
;
2437 int first_dir_index
= current_dir_index
;
2441 DLOG(fprintf(stderr
, "commit_direntries for %s, parent_mapping_index %d\n", mapping
->path
, parent_mapping_index
));
2445 assert(mapping
->begin
== first_cluster
);
2446 assert(mapping
->info
.dir
.first_dir_index
< s
->directory
.next
);
2447 assert(mapping
->mode
& MODE_DIRECTORY
);
2448 assert(dir_index
== 0 || is_directory(direntry
));
2450 mapping
->info
.dir
.parent_mapping_index
= parent_mapping_index
;
2452 if (first_cluster
== 0) {
2453 old_cluster_count
= new_cluster_count
=
2454 s
->last_cluster_of_root_directory
;
2456 for (old_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2458 old_cluster_count
++;
2460 for (new_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2461 c
= modified_fat_get(s
, c
))
2462 new_cluster_count
++;
2465 if (new_cluster_count
> old_cluster_count
) {
2466 if (insert_direntries(s
,
2467 current_dir_index
+ factor
* old_cluster_count
,
2468 factor
* (new_cluster_count
- old_cluster_count
)) == NULL
)
2470 } else if (new_cluster_count
< old_cluster_count
)
2471 remove_direntries(s
,
2472 current_dir_index
+ factor
* new_cluster_count
,
2473 factor
* (old_cluster_count
- new_cluster_count
));
2475 for (c
= first_cluster
; !fat_eof(s
, c
); c
= modified_fat_get(s
, c
)) {
2476 direntry_t
*first_direntry
;
2477 void* direntry
= array_get(&(s
->directory
), current_dir_index
);
2478 int ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
), direntry
,
2479 s
->sectors_per_cluster
);
2483 /* The first directory entry on the filesystem is the volume name */
2484 first_direntry
= (direntry_t
*) s
->directory
.pointer
;
2485 assert(!memcmp(first_direntry
->name
, s
->volume_label
, 11));
2487 current_dir_index
+= factor
;
2490 ret
= commit_mappings(s
, first_cluster
, dir_index
);
2495 for (i
= 0; i
< factor
* new_cluster_count
; i
++) {
2496 direntry
= array_get(&(s
->directory
), first_dir_index
+ i
);
2497 if (is_directory(direntry
) && !is_dot(direntry
)) {
2498 mapping
= find_mapping_for_cluster(s
, first_cluster
);
2499 assert(mapping
->mode
& MODE_DIRECTORY
);
2500 ret
= commit_direntries(s
, first_dir_index
+ i
,
2501 array_index(&(s
->mapping
), mapping
));
2510 /* commit one file (adjust contents, adjust mapping),
2511 return first_mapping_index */
2512 static int commit_one_file(BDRVVVFATState
* s
,
2513 int dir_index
, uint32_t offset
)
2515 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2516 uint32_t c
= begin_of_direntry(direntry
);
2517 uint32_t first_cluster
= c
;
2518 mapping_t
* mapping
= find_mapping_for_cluster(s
, c
);
2519 uint32_t size
= filesize_of_direntry(direntry
);
2520 char* cluster
= g_malloc(s
->cluster_size
);
2524 assert(offset
< size
);
2525 assert((offset
% s
->cluster_size
) == 0);
2527 for (i
= s
->cluster_size
; i
< offset
; i
+= s
->cluster_size
)
2528 c
= modified_fat_get(s
, c
);
2530 fd
= qemu_open(mapping
->path
, O_RDWR
| O_CREAT
| O_BINARY
, 0666);
2532 fprintf(stderr
, "Could not open %s... (%s, %d)\n", mapping
->path
,
2533 strerror(errno
), errno
);
2538 if (lseek(fd
, offset
, SEEK_SET
) != offset
) {
2545 while (offset
< size
) {
2547 int rest_size
= (size
- offset
> s
->cluster_size
?
2548 s
->cluster_size
: size
- offset
);
2551 c1
= modified_fat_get(s
, c
);
2553 assert((size
- offset
== 0 && fat_eof(s
, c
)) ||
2554 (size
> offset
&& c
>=2 && !fat_eof(s
, c
)));
2556 ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
),
2557 (uint8_t*)cluster
, DIV_ROUND_UP(rest_size
, 0x200));
2565 if (write(fd
, cluster
, rest_size
) < 0) {
2571 offset
+= rest_size
;
2575 if (ftruncate(fd
, size
)) {
2576 perror("ftruncate()");
2584 return commit_mappings(s
, first_cluster
, dir_index
);
2588 /* test, if all mappings point to valid direntries */
2589 static void check1(BDRVVVFATState
* s
)
2592 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2593 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2594 if (mapping
->mode
& MODE_DELETED
) {
2595 fprintf(stderr
, "deleted\n");
2598 assert(mapping
->dir_index
< s
->directory
.next
);
2599 direntry_t
* direntry
= array_get(&(s
->directory
), mapping
->dir_index
);
2600 assert(mapping
->begin
== begin_of_direntry(direntry
) || mapping
->first_mapping_index
>= 0);
2601 if (mapping
->mode
& MODE_DIRECTORY
) {
2602 assert(mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
* (mapping
->end
- mapping
->begin
) <= s
->directory
.next
);
2603 assert((mapping
->info
.dir
.first_dir_index
% (0x10 * s
->sectors_per_cluster
)) == 0);
2608 /* test, if all direntries have mappings */
2609 static void check2(BDRVVVFATState
* s
)
2612 int first_mapping
= -1;
2614 for (i
= 0; i
< s
->directory
.next
; i
++) {
2615 direntry_t
* direntry
= array_get(&(s
->directory
), i
);
2617 if (is_short_name(direntry
) && begin_of_direntry(direntry
)) {
2618 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin_of_direntry(direntry
));
2620 assert(mapping
->dir_index
== i
|| is_dot(direntry
));
2621 assert(mapping
->begin
== begin_of_direntry(direntry
) || is_dot(direntry
));
2624 if ((i
% (0x10 * s
->sectors_per_cluster
)) == 0) {
2628 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2629 mapping_t
* mapping
= array_get(&(s
->mapping
), j
);
2630 if (mapping
->mode
& MODE_DELETED
)
2632 if (mapping
->mode
& MODE_DIRECTORY
) {
2633 if (mapping
->info
.dir
.first_dir_index
<= i
&& mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
> i
) {
2634 assert(++count
== 1);
2635 if (mapping
->first_mapping_index
== -1)
2636 first_mapping
= array_index(&(s
->mapping
), mapping
);
2638 assert(first_mapping
== mapping
->first_mapping_index
);
2639 if (mapping
->info
.dir
.parent_mapping_index
< 0)
2642 mapping_t
* parent
= array_get(&(s
->mapping
), mapping
->info
.dir
.parent_mapping_index
);
2643 assert(parent
->mode
& MODE_DIRECTORY
);
2644 assert(parent
->info
.dir
.first_dir_index
< mapping
->info
.dir
.first_dir_index
);
2656 static int handle_renames_and_mkdirs(BDRVVVFATState
* s
)
2661 fprintf(stderr
, "handle_renames\n");
2662 for (i
= 0; i
< s
->commits
.next
; i
++) {
2663 commit_t
* commit
= array_get(&(s
->commits
), i
);
2664 fprintf(stderr
, "%d, %s (%d, %d)\n", i
, commit
->path
? commit
->path
: "(null)", commit
->param
.rename
.cluster
, commit
->action
);
2668 for (i
= 0; i
< s
->commits
.next
;) {
2669 commit_t
* commit
= array_get(&(s
->commits
), i
);
2670 if (commit
->action
== ACTION_RENAME
) {
2671 mapping_t
* mapping
= find_mapping_for_cluster(s
,
2672 commit
->param
.rename
.cluster
);
2673 char* old_path
= mapping
->path
;
2675 assert(commit
->path
);
2676 mapping
->path
= commit
->path
;
2677 if (rename(old_path
, mapping
->path
))
2680 if (mapping
->mode
& MODE_DIRECTORY
) {
2681 int l1
= strlen(mapping
->path
);
2682 int l2
= strlen(old_path
);
2684 direntry_t
* direntry
= array_get(&(s
->directory
),
2685 mapping
->info
.dir
.first_dir_index
);
2686 uint32_t c
= mapping
->begin
;
2690 while (!fat_eof(s
, c
)) {
2692 direntry_t
* d
= direntry
+ i
;
2694 if (is_file(d
) || (is_directory(d
) && !is_dot(d
))) {
2695 mapping_t
* m
= find_mapping_for_cluster(s
,
2696 begin_of_direntry(d
));
2697 int l
= strlen(m
->path
);
2698 char* new_path
= g_malloc(l
+ diff
+ 1);
2700 assert(!strncmp(m
->path
, mapping
->path
, l2
));
2702 pstrcpy(new_path
, l
+ diff
+ 1, mapping
->path
);
2703 pstrcpy(new_path
+ l1
, l
+ diff
+ 1 - l1
,
2706 schedule_rename(s
, m
->begin
, new_path
);
2709 } while((i
% (0x10 * s
->sectors_per_cluster
)) != 0);
2715 array_remove(&(s
->commits
), i
);
2717 } else if (commit
->action
== ACTION_MKDIR
) {
2719 int j
, parent_path_len
;
2722 if (mkdir(commit
->path
))
2725 if (mkdir(commit
->path
, 0755))
2729 mapping
= insert_mapping(s
, commit
->param
.mkdir
.cluster
,
2730 commit
->param
.mkdir
.cluster
+ 1);
2731 if (mapping
== NULL
)
2734 mapping
->mode
= MODE_DIRECTORY
;
2735 mapping
->read_only
= 0;
2736 mapping
->path
= commit
->path
;
2737 j
= s
->directory
.next
;
2739 insert_direntries(s
, s
->directory
.next
,
2740 0x10 * s
->sectors_per_cluster
);
2741 mapping
->info
.dir
.first_dir_index
= j
;
2743 parent_path_len
= strlen(commit
->path
)
2744 - strlen(get_basename(commit
->path
)) - 1;
2745 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2746 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2747 if (m
->first_mapping_index
< 0 && m
!= mapping
&&
2748 !strncmp(m
->path
, mapping
->path
, parent_path_len
) &&
2749 strlen(m
->path
) == parent_path_len
)
2752 assert(j
< s
->mapping
.next
);
2753 mapping
->info
.dir
.parent_mapping_index
= j
;
2755 array_remove(&(s
->commits
), i
);
2765 * TODO: make sure that the short name is not matching *another* file
2767 static int handle_commits(BDRVVVFATState
* s
)
2771 vvfat_close_current_file(s
);
2773 for (i
= 0; !fail
&& i
< s
->commits
.next
; i
++) {
2774 commit_t
* commit
= array_get(&(s
->commits
), i
);
2775 switch(commit
->action
) {
2776 case ACTION_RENAME
: case ACTION_MKDIR
:
2780 case ACTION_WRITEOUT
: {
2782 /* these variables are only used by assert() below */
2783 direntry_t
* entry
= array_get(&(s
->directory
),
2784 commit
->param
.writeout
.dir_index
);
2785 uint32_t begin
= begin_of_direntry(entry
);
2786 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2790 assert(mapping
->begin
== begin
);
2791 assert(commit
->path
== NULL
);
2793 if (commit_one_file(s
, commit
->param
.writeout
.dir_index
,
2794 commit
->param
.writeout
.modified_offset
))
2799 case ACTION_NEW_FILE
: {
2800 int begin
= commit
->param
.new_file
.first_cluster
;
2801 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2806 for (i
= 0; i
< s
->directory
.next
; i
++) {
2807 entry
= array_get(&(s
->directory
), i
);
2808 if (is_file(entry
) && begin_of_direntry(entry
) == begin
)
2812 if (i
>= s
->directory
.next
) {
2817 /* make sure there exists an initial mapping */
2818 if (mapping
&& mapping
->begin
!= begin
) {
2819 mapping
->end
= begin
;
2822 if (mapping
== NULL
) {
2823 mapping
= insert_mapping(s
, begin
, begin
+1);
2825 /* most members will be fixed in commit_mappings() */
2826 assert(commit
->path
);
2827 mapping
->path
= commit
->path
;
2828 mapping
->read_only
= 0;
2829 mapping
->mode
= MODE_NORMAL
;
2830 mapping
->info
.file
.offset
= 0;
2832 if (commit_one_file(s
, i
, 0))
2841 if (i
> 0 && array_remove_slice(&(s
->commits
), 0, i
))
2846 static int handle_deletes(BDRVVVFATState
* s
)
2848 int i
, deferred
= 1, deleted
= 1;
2850 /* delete files corresponding to mappings marked as deleted */
2851 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2852 while (deferred
&& deleted
) {
2856 for (i
= 1; i
< s
->mapping
.next
; i
++) {
2857 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2858 if (mapping
->mode
& MODE_DELETED
) {
2859 direntry_t
* entry
= array_get(&(s
->directory
),
2860 mapping
->dir_index
);
2862 if (is_free(entry
)) {
2863 /* remove file/directory */
2864 if (mapping
->mode
& MODE_DIRECTORY
) {
2865 int j
, next_dir_index
= s
->directory
.next
,
2866 first_dir_index
= mapping
->info
.dir
.first_dir_index
;
2868 if (rmdir(mapping
->path
) < 0) {
2869 if (errno
== ENOTEMPTY
) {
2876 for (j
= 1; j
< s
->mapping
.next
; j
++) {
2877 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2878 if (m
->mode
& MODE_DIRECTORY
&&
2879 m
->info
.dir
.first_dir_index
>
2881 m
->info
.dir
.first_dir_index
<
2884 m
->info
.dir
.first_dir_index
;
2886 remove_direntries(s
, first_dir_index
,
2887 next_dir_index
- first_dir_index
);
2892 if (unlink(mapping
->path
))
2896 DLOG(fprintf(stderr
, "DELETE (%d)\n", i
); print_mapping(mapping
); print_direntry(entry
));
2897 remove_mapping(s
, i
);
2906 * synchronize mapping with new state:
2908 * - copy FAT (with bdrv_read)
2909 * - mark all filenames corresponding to mappings as deleted
2910 * - recurse direntries from root (using bs->bdrv_read)
2911 * - delete files corresponding to mappings marked as deleted
2913 static int do_commit(BDRVVVFATState
* s
)
2917 /* the real meat are the commits. Nothing to do? Move along! */
2918 if (s
->commits
.next
== 0)
2921 vvfat_close_current_file(s
);
2923 ret
= handle_renames_and_mkdirs(s
);
2925 fprintf(stderr
, "Error handling renames (%d)\n", ret
);
2930 /* copy FAT (with bdrv_read) */
2931 memcpy(s
->fat
.pointer
, s
->fat2
, 0x200 * s
->sectors_per_fat
);
2933 /* recurse direntries from root (using bs->bdrv_read) */
2934 ret
= commit_direntries(s
, 0, -1);
2936 fprintf(stderr
, "Fatal: error while committing (%d)\n", ret
);
2941 ret
= handle_commits(s
);
2943 fprintf(stderr
, "Error handling commits (%d)\n", ret
);
2948 ret
= handle_deletes(s
);
2950 fprintf(stderr
, "Error deleting\n");
2955 if (s
->qcow
->bs
->drv
->bdrv_make_empty
) {
2956 s
->qcow
->bs
->drv
->bdrv_make_empty(s
->qcow
->bs
);
2959 memset(s
->used_clusters
, 0, sector2cluster(s
, s
->sector_count
));
2965 static int try_commit(BDRVVVFATState
* s
)
2967 vvfat_close_current_file(s
);
2969 if(!is_consistent(s
))
2971 return do_commit(s
);
2974 static int vvfat_write(BlockDriverState
*bs
, int64_t sector_num
,
2975 const uint8_t *buf
, int nb_sectors
)
2977 BDRVVVFATState
*s
= bs
->opaque
;
2982 /* Check if we're operating in read-only mode */
2983 if (s
->qcow
== NULL
) {
2987 vvfat_close_current_file(s
);
2990 * Some sanity checks:
2991 * - do not allow writing to the boot sector
2994 if (sector_num
< s
->offset_to_fat
)
2997 for (i
= sector2cluster(s
, sector_num
);
2998 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1);) {
2999 mapping_t
* mapping
= find_mapping_for_cluster(s
, i
);
3001 if (mapping
->read_only
) {
3002 fprintf(stderr
, "Tried to write to write-protected file %s\n",
3007 if (mapping
->mode
& MODE_DIRECTORY
) {
3008 int begin
= cluster2sector(s
, i
);
3009 int end
= begin
+ s
->sectors_per_cluster
, k
;
3011 const direntry_t
* direntries
;
3016 if (begin
< sector_num
)
3018 if (end
> sector_num
+ nb_sectors
)
3019 end
= sector_num
+ nb_sectors
;
3020 dir_index
= mapping
->dir_index
+
3021 0x10 * (begin
- mapping
->begin
* s
->sectors_per_cluster
);
3022 direntries
= (direntry_t
*)(buf
+ 0x200 * (begin
- sector_num
));
3024 for (k
= 0; k
< (end
- begin
) * 0x10; k
++) {
3025 /* no access to the direntry of a read-only file */
3026 if (is_short_name(direntries
+ k
) &&
3027 (direntries
[k
].attributes
& 1)) {
3028 if (memcmp(direntries
+ k
,
3029 array_get(&(s
->directory
), dir_index
+ k
),
3030 sizeof(direntry_t
))) {
3031 warn_report("tried to write to write-protected "
3044 * Use qcow backend. Commit later.
3046 DLOG(fprintf(stderr
, "Write to qcow backend: %d + %d\n", (int)sector_num
, nb_sectors
));
3047 ret
= bdrv_write(s
->qcow
, sector_num
, buf
, nb_sectors
);
3049 fprintf(stderr
, "Error writing to qcow backend\n");
3053 for (i
= sector2cluster(s
, sector_num
);
3054 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1); i
++)
3056 s
->used_clusters
[i
] |= USED_ALLOCATED
;
3059 /* TODO: add timeout */
3066 static int coroutine_fn
3067 vvfat_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
3068 QEMUIOVector
*qiov
, int flags
)
3071 BDRVVVFATState
*s
= bs
->opaque
;
3072 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
3073 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
3076 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
3077 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
3079 buf
= g_try_malloc(bytes
);
3080 if (bytes
&& buf
== NULL
) {
3083 qemu_iovec_to_buf(qiov
, 0, buf
, bytes
);
3085 qemu_co_mutex_lock(&s
->lock
);
3086 ret
= vvfat_write(bs
, sector_num
, buf
, nb_sectors
);
3087 qemu_co_mutex_unlock(&s
->lock
);
3094 static int64_t coroutine_fn
vvfat_co_get_block_status(BlockDriverState
*bs
,
3095 int64_t sector_num
, int nb_sectors
, int *n
, BlockDriverState
**file
)
3097 *n
= bs
->total_sectors
- sector_num
;
3098 if (*n
> nb_sectors
) {
3100 } else if (*n
< 0) {
3103 return BDRV_BLOCK_DATA
;
3106 static int coroutine_fn
3107 write_target_commit(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
3108 QEMUIOVector
*qiov
, int flags
)
3112 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
3113 qemu_co_mutex_lock(&s
->lock
);
3114 ret
= try_commit(s
);
3115 qemu_co_mutex_unlock(&s
->lock
);
3120 static void write_target_close(BlockDriverState
*bs
) {
3121 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
3122 bdrv_unref_child(s
->bs
, s
->qcow
);
3123 g_free(s
->qcow_filename
);
3126 static BlockDriver vvfat_write_target
= {
3127 .format_name
= "vvfat_write_target",
3128 .instance_size
= sizeof(void*),
3129 .bdrv_co_pwritev
= write_target_commit
,
3130 .bdrv_close
= write_target_close
,
3133 static void vvfat_qcow_options(int *child_flags
, QDict
*child_options
,
3134 int parent_flags
, QDict
*parent_options
)
3136 qdict_set_default_str(child_options
, BDRV_OPT_READ_ONLY
, "off");
3137 *child_flags
= BDRV_O_NO_FLUSH
;
3140 static const BdrvChildRole child_vvfat_qcow
= {
3141 .inherit_options
= vvfat_qcow_options
,
3144 static int enable_write_target(BlockDriverState
*bs
, Error
**errp
)
3146 BDRVVVFATState
*s
= bs
->opaque
;
3147 BlockDriver
*bdrv_qcow
= NULL
;
3148 BlockDriverState
*backing
;
3149 QemuOpts
*opts
= NULL
;
3151 int size
= sector2cluster(s
, s
->sector_count
);
3154 s
->used_clusters
= calloc(size
, 1);
3156 array_init(&(s
->commits
), sizeof(commit_t
));
3158 s
->qcow_filename
= g_malloc(PATH_MAX
);
3159 ret
= get_tmp_filename(s
->qcow_filename
, PATH_MAX
);
3161 error_setg_errno(errp
, -ret
, "can't create temporary file");
3165 bdrv_qcow
= bdrv_find_format("qcow");
3167 error_setg(errp
, "Failed to locate qcow driver");
3172 opts
= qemu_opts_create(bdrv_qcow
->create_opts
, NULL
, 0, &error_abort
);
3173 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
->sector_count
* 512,
3175 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, "fat:", &error_abort
);
3177 ret
= bdrv_create(bdrv_qcow
, s
->qcow_filename
, opts
, errp
);
3178 qemu_opts_del(opts
);
3183 options
= qdict_new();
3184 qdict_put_str(options
, "write-target.driver", "qcow");
3185 s
->qcow
= bdrv_open_child(s
->qcow_filename
, options
, "write-target", bs
,
3186 &child_vvfat_qcow
, false, errp
);
3194 unlink(s
->qcow_filename
);
3197 backing
= bdrv_new_open_driver(&vvfat_write_target
, NULL
, BDRV_O_ALLOW_RDWR
,
3199 *(void**) backing
->opaque
= s
;
3201 bdrv_set_backing_hd(s
->bs
, backing
, &error_abort
);
3202 bdrv_unref(backing
);
3207 g_free(s
->qcow_filename
);
3208 s
->qcow_filename
= NULL
;
3212 static void vvfat_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
3213 const BdrvChildRole
*role
,
3214 uint64_t perm
, uint64_t shared
,
3215 uint64_t *nperm
, uint64_t *nshared
)
3217 BDRVVVFATState
*s
= bs
->opaque
;
3219 assert(c
== s
->qcow
|| role
== &child_backing
);
3222 /* This is a private node, nobody should try to attach to it */
3223 *nperm
= BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
;
3224 *nshared
= BLK_PERM_WRITE_UNCHANGED
;
3226 /* The backing file is there so 'commit' can use it. vvfat doesn't
3227 * access it in any way. */
3229 *nshared
= BLK_PERM_ALL
;
3233 static void vvfat_close(BlockDriverState
*bs
)
3235 BDRVVVFATState
*s
= bs
->opaque
;
3237 vvfat_close_current_file(s
);
3238 array_free(&(s
->fat
));
3239 array_free(&(s
->directory
));
3240 array_free(&(s
->mapping
));
3241 g_free(s
->cluster_buffer
);
3244 migrate_del_blocker(s
->migration_blocker
);
3245 error_free(s
->migration_blocker
);
3249 static BlockDriver bdrv_vvfat
= {
3250 .format_name
= "vvfat",
3251 .protocol_name
= "fat",
3252 .instance_size
= sizeof(BDRVVVFATState
),
3254 .bdrv_parse_filename
= vvfat_parse_filename
,
3255 .bdrv_file_open
= vvfat_open
,
3256 .bdrv_refresh_limits
= vvfat_refresh_limits
,
3257 .bdrv_close
= vvfat_close
,
3258 .bdrv_child_perm
= vvfat_child_perm
,
3260 .bdrv_co_preadv
= vvfat_co_preadv
,
3261 .bdrv_co_pwritev
= vvfat_co_pwritev
,
3262 .bdrv_co_get_block_status
= vvfat_co_get_block_status
,
3265 static void bdrv_vvfat_init(void)
3267 bdrv_register(&bdrv_vvfat
);
3270 block_init(bdrv_vvfat_init
);
3273 static void checkpoint(void) {
3274 assert(((mapping_t
*)array_get(&(vvv
->mapping
), 0))->end
== 2);
3277 assert(!vvv
->current_mapping
|| vvv
->current_fd
|| (vvv
->current_mapping
->mode
& MODE_DIRECTORY
));
3279 if (((direntry_t
*)vvv
->directory
.pointer
)[1].attributes
!= 0xf)
3280 fprintf(stderr
, "Nonono!\n");
3282 direntry_t
* direntry
;
3283 assert(vvv
->mapping
.size
>= vvv
->mapping
.item_size
* vvv
->mapping
.next
);
3284 assert(vvv
->directory
.size
>= vvv
->directory
.item_size
* vvv
->directory
.next
);
3285 if (vvv
->mapping
.next
<47)
3287 assert((mapping
= array_get(&(vvv
->mapping
), 47)));
3288 assert(mapping
->dir_index
< vvv
->directory
.next
);
3289 direntry
= array_get(&(vvv
->directory
), mapping
->dir_index
);
3290 assert(!memcmp(direntry
->name
, "USB H ", 11) || direntry
->name
[0]==0);