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
26 #include "qemu/osdep.h"
28 #include "qapi/error.h"
29 #include "block/block_int.h"
30 #include "block/qdict.h"
31 #include "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qemu/bswap.h"
34 #include "migration/blocker.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qstring.h"
37 #include "qemu/cutils.h"
38 #include "qemu/error-report.h"
47 /* TODO: add ":bootsector=blabla.img:" */
48 /* LATER TODO: add automatic boot sector generation from
49 BOOTEASY.ASM and Ranish Partition Manager
50 Note that DOS assumes the system files to be the first files in the
51 file system (test if the boot sector still relies on that fact)! */
52 /* MAYBE TODO: write block-visofs.c */
53 /* TODO: call try_commit() only after a timeout */
61 static void checkpoint(void);
69 /* bootsector OEM name. see related compatibility problems at:
70 * https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
71 * http://seasip.info/Misc/oemid.html
73 #define BOOTSECTOR_OEM_NAME "MSWIN4.1"
75 #define DIR_DELETED 0xe5
76 #define DIR_KANJI DIR_DELETED
77 #define DIR_KANJI_FAKE 0x05
80 /* dynamic array functions */
81 typedef struct array_t
{
83 unsigned int size
,next
,item_size
;
86 static inline void array_init(array_t
* array
,unsigned int item_size
)
88 array
->pointer
= NULL
;
91 array
->item_size
=item_size
;
94 static inline void array_free(array_t
* array
)
96 g_free(array
->pointer
);
97 array
->size
=array
->next
=0;
100 /* does not automatically grow */
101 static inline void* array_get(array_t
* array
,unsigned int index
) {
102 assert(index
< array
->next
);
103 return array
->pointer
+ index
* array
->item_size
;
106 static inline int array_ensure_allocated(array_t
* array
, int index
)
108 if((index
+ 1) * array
->item_size
> array
->size
) {
109 int new_size
= (index
+ 32) * array
->item_size
;
110 array
->pointer
= g_realloc(array
->pointer
, new_size
);
113 memset(array
->pointer
+ array
->size
, 0, new_size
- array
->size
);
114 array
->size
= new_size
;
115 array
->next
= index
+ 1;
121 static inline void* array_get_next(array_t
* array
) {
122 unsigned int next
= array
->next
;
124 if (array_ensure_allocated(array
, next
) < 0)
127 array
->next
= next
+ 1;
128 return array_get(array
, next
);
131 static inline void* array_insert(array_t
* array
,unsigned int index
,unsigned int count
) {
132 if((array
->next
+count
)*array
->item_size
>array
->size
) {
133 int increment
=count
*array
->item_size
;
134 array
->pointer
=g_realloc(array
->pointer
,array
->size
+increment
);
137 array
->size
+=increment
;
139 memmove(array
->pointer
+(index
+count
)*array
->item_size
,
140 array
->pointer
+index
*array
->item_size
,
141 (array
->next
-index
)*array
->item_size
);
143 return array
->pointer
+index
*array
->item_size
;
146 /* this performs a "roll", so that the element which was at index_from becomes
147 * index_to, but the order of all other elements is preserved. */
148 static inline int array_roll(array_t
* array
,int index_to
,int index_from
,int count
)
156 index_to
<0 || index_to
>=array
->next
||
157 index_from
<0 || index_from
>=array
->next
)
160 if(index_to
==index_from
)
164 from
=array
->pointer
+index_from
*is
;
165 to
=array
->pointer
+index_to
*is
;
166 buf
=g_malloc(is
*count
);
167 memcpy(buf
,from
,is
*count
);
169 if(index_to
<index_from
)
170 memmove(to
+is
*count
,to
,from
-to
);
172 memmove(from
,from
+is
*count
,to
-from
);
174 memcpy(to
,buf
,is
*count
);
181 static inline int array_remove_slice(array_t
* array
,int index
, int count
)
185 assert(index
+ count
<= array
->next
);
186 if(array_roll(array
,array
->next
-1,index
,count
))
188 array
->next
-= count
;
192 static int array_remove(array_t
* array
,int index
)
194 return array_remove_slice(array
, index
, 1);
197 /* return the index for a given member */
198 static int array_index(array_t
* array
, void* pointer
)
200 size_t offset
= (char*)pointer
- array
->pointer
;
201 assert((offset
% array
->item_size
) == 0);
202 assert(offset
/array
->item_size
< array
->next
);
203 return offset
/array
->item_size
;
206 /* These structures are used to fake a disk and the VFAT filesystem.
207 * For this reason we need to use QEMU_PACKED. */
209 typedef struct bootsector_t
{
212 uint16_t sector_size
;
213 uint8_t sectors_per_cluster
;
214 uint16_t reserved_sectors
;
215 uint8_t number_of_fats
;
216 uint16_t root_entries
;
217 uint16_t total_sectors16
;
219 uint16_t sectors_per_fat
;
220 uint16_t sectors_per_track
;
221 uint16_t number_of_heads
;
222 uint32_t hidden_sectors
;
223 uint32_t total_sectors
;
226 uint8_t drive_number
;
230 uint8_t volume_label
[11];
232 uint8_t ignored
[0x1c0];
235 uint32_t sectors_per_fat
;
238 uint32_t first_cluster_of_root_dir
;
239 uint16_t info_sector
;
240 uint16_t backup_boot_sector
;
241 uint8_t reserved
[12];
242 uint8_t drive_number
;
246 uint8_t volume_label
[11];
248 uint8_t ignored
[0x1a4];
252 } QEMU_PACKED bootsector_t
;
260 typedef struct partition_t
{
261 uint8_t attributes
; /* 0x80 = bootable */
263 uint8_t fs_type
; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
265 uint32_t start_sector_long
;
266 uint32_t length_sector_long
;
267 } QEMU_PACKED partition_t
;
269 typedef struct mbr_t
{
270 uint8_t ignored
[0x1b8];
273 partition_t partition
[4];
277 typedef struct direntry_t
{
289 } QEMU_PACKED direntry_t
;
291 /* this structure are used to transparently access the files */
293 typedef struct mapping_t
{
294 /* begin is the first cluster, end is the last+1 */
296 /* as s->directory is growable, no pointer may be used here */
297 unsigned int dir_index
;
298 /* the clusters of a file may be in any order; this points to the first */
299 int first_mapping_index
;
302 * - the offset in the file (in clusters) for a file, or
303 * - the next cluster of the directory for a directory
309 int parent_mapping_index
;
313 /* path contains the full path, i.e. it always starts with s->path */
327 static void print_direntry(const struct direntry_t
*);
328 static void print_mapping(const struct mapping_t
* mapping
);
331 /* here begins the real VVFAT driver */
333 typedef struct BDRVVVFATState
{
335 BlockDriverState
* bs
; /* pointer to parent */
336 unsigned char first_sectors
[0x40*0x200];
338 int fat_type
; /* 16 or 32 */
339 array_t fat
,directory
,mapping
;
340 char volume_label
[11];
342 uint32_t offset_to_bootsector
; /* 0 for floppy, 0x3f for disk */
344 unsigned int cluster_size
;
345 unsigned int sectors_per_cluster
;
346 unsigned int sectors_per_fat
;
347 uint32_t last_cluster_of_root_directory
;
348 /* how many entries are available in root directory (0 for FAT32) */
349 uint16_t root_entries
;
350 uint32_t sector_count
; /* total number of sectors of the partition */
351 uint32_t cluster_count
; /* total number of clusters of this partition */
352 uint32_t max_fat_value
;
353 uint32_t offset_to_fat
;
354 uint32_t offset_to_root_dir
;
357 mapping_t
* current_mapping
;
358 unsigned char* cluster
; /* points to current cluster */
359 unsigned char* cluster_buffer
; /* points to a buffer to hold temp data */
360 unsigned int current_cluster
;
369 int downcase_short_names
;
371 Error
*migration_blocker
;
374 /* take the sector position spos and convert it to Cylinder/Head/Sector position
375 * if the position is outside the specified geometry, fill maximum value for CHS
376 * and return 1 to signal overflow.
378 static int sector2CHS(mbr_chs_t
*chs
, int spos
, int cyls
, int heads
, int secs
)
381 sector
= spos
% secs
; spos
/= secs
;
382 head
= spos
% heads
; spos
/= heads
;
385 it happens if 32bit sector positions are used, while CHS is only 24bit.
386 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
389 chs
->cylinder
= 0xFF;
392 chs
->head
= (uint8_t)head
;
393 chs
->sector
= (uint8_t)( (sector
+1) | ((spos
>>8)<<6) );
394 chs
->cylinder
= (uint8_t)spos
;
398 static void init_mbr(BDRVVVFATState
*s
, int cyls
, int heads
, int secs
)
400 /* TODO: if the files mbr.img and bootsect.img exist, use them */
401 mbr_t
* real_mbr
=(mbr_t
*)s
->first_sectors
;
402 partition_t
* partition
= &(real_mbr
->partition
[0]);
405 memset(s
->first_sectors
,0,512);
407 /* Win NT Disk Signature */
408 real_mbr
->nt_id
= cpu_to_le32(0xbe1afdfa);
410 partition
->attributes
=0x80; /* bootable */
412 /* LBA is used when partition is outside the CHS geometry */
413 lba
= sector2CHS(&partition
->start_CHS
, s
->offset_to_bootsector
,
415 lba
|= sector2CHS(&partition
->end_CHS
, s
->bs
->total_sectors
- 1,
418 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
419 partition
->start_sector_long
= cpu_to_le32(s
->offset_to_bootsector
);
420 partition
->length_sector_long
= cpu_to_le32(s
->bs
->total_sectors
421 - s
->offset_to_bootsector
);
423 /* FAT12/FAT16/FAT32 */
424 /* DOS uses different types when partition is LBA,
425 probably to prevent older versions from using CHS on them */
426 partition
->fs_type
= s
->fat_type
== 12 ? 0x1 :
427 s
->fat_type
== 16 ? (lba
? 0xe : 0x06) :
428 /*s->fat_type == 32*/ (lba
? 0xc : 0x0b);
430 real_mbr
->magic
[0]=0x55; real_mbr
->magic
[1]=0xaa;
433 /* direntry functions */
435 static direntry_t
*create_long_filename(BDRVVVFATState
*s
, const char *filename
)
437 int number_of_entries
, i
;
441 gunichar2
*longname
= g_utf8_to_utf16(filename
, -1, NULL
, &length
, NULL
);
443 fprintf(stderr
, "vvfat: invalid UTF-8 name: %s\n", filename
);
447 number_of_entries
= DIV_ROUND_UP(length
* 2, 26);
449 for(i
=0;i
<number_of_entries
;i
++) {
450 entry
=array_get_next(&(s
->directory
));
451 entry
->attributes
=0xf;
452 entry
->reserved
[0]=0;
454 entry
->name
[0]=(number_of_entries
-i
)|(i
==0?0x40:0);
456 for(i
=0;i
<26*number_of_entries
;i
++) {
458 if(offset
<10) offset
=1+offset
;
459 else if(offset
<22) offset
=14+offset
-10;
460 else offset
=28+offset
-22;
461 entry
=array_get(&(s
->directory
),s
->directory
.next
-1-(i
/26));
462 if (i
>= 2 * length
+ 2) {
463 entry
->name
[offset
] = 0xff;
464 } else if (i
% 2 == 0) {
465 entry
->name
[offset
] = longname
[i
/ 2] & 0xff;
467 entry
->name
[offset
] = longname
[i
/ 2] >> 8;
471 return array_get(&(s
->directory
),s
->directory
.next
-number_of_entries
);
474 static char is_free(const direntry_t
* direntry
)
476 return direntry
->name
[0] == DIR_DELETED
|| direntry
->name
[0] == DIR_FREE
;
479 static char is_volume_label(const direntry_t
* direntry
)
481 return direntry
->attributes
== 0x28;
484 static char is_long_name(const direntry_t
* direntry
)
486 return direntry
->attributes
== 0xf;
489 static char is_short_name(const direntry_t
* direntry
)
491 return !is_volume_label(direntry
) && !is_long_name(direntry
)
492 && !is_free(direntry
);
495 static char is_directory(const direntry_t
* direntry
)
497 return direntry
->attributes
& 0x10 && direntry
->name
[0] != DIR_DELETED
;
500 static inline char is_dot(const direntry_t
* direntry
)
502 return is_short_name(direntry
) && direntry
->name
[0] == '.';
505 static char is_file(const direntry_t
* direntry
)
507 return is_short_name(direntry
) && !is_directory(direntry
);
510 static inline uint32_t begin_of_direntry(const direntry_t
* direntry
)
512 return le16_to_cpu(direntry
->begin
)|(le16_to_cpu(direntry
->begin_hi
)<<16);
515 static inline uint32_t filesize_of_direntry(const direntry_t
* direntry
)
517 return le32_to_cpu(direntry
->size
);
520 static void set_begin_of_direntry(direntry_t
* direntry
, uint32_t begin
)
522 direntry
->begin
= cpu_to_le16(begin
& 0xffff);
523 direntry
->begin_hi
= cpu_to_le16((begin
>> 16) & 0xffff);
526 static uint8_t to_valid_short_char(gunichar c
)
528 c
= g_unichar_toupper(c
);
529 if ((c
>= '0' && c
<= '9') ||
530 (c
>= 'A' && c
<= 'Z') ||
531 strchr("$%'-_@~`!(){}^#&", c
) != 0) {
538 static direntry_t
*create_short_filename(BDRVVVFATState
*s
,
539 const char *filename
,
540 unsigned int directory_start
)
543 direntry_t
*entry
= array_get_next(&(s
->directory
));
544 const gchar
*p
, *last_dot
= NULL
;
546 bool lossy_conversion
= false;
552 memset(entry
->name
, 0x20, sizeof(entry
->name
));
554 /* copy filename and search last dot */
555 for (p
= filename
; ; p
= g_utf8_next_char(p
)) {
556 c
= g_utf8_get_char(p
);
559 } else if (c
== '.') {
561 /* '.' at start of filename */
562 lossy_conversion
= true;
565 lossy_conversion
= true;
569 } else if (!last_dot
) {
570 /* first part of the name; copy it */
571 uint8_t v
= to_valid_short_char(c
);
573 entry
->name
[j
++] = v
;
575 lossy_conversion
= true;
580 /* copy extension (if any) */
583 for (p
= g_utf8_next_char(last_dot
); ; p
= g_utf8_next_char(p
)) {
584 c
= g_utf8_get_char(p
);
588 /* extension; copy it */
589 uint8_t v
= to_valid_short_char(c
);
591 entry
->name
[8 + (j
++)] = v
;
593 lossy_conversion
= true;
599 if (entry
->name
[0] == DIR_KANJI
) {
600 entry
->name
[0] = DIR_KANJI_FAKE
;
603 /* numeric-tail generation */
604 for (j
= 0; j
< 8; j
++) {
605 if (entry
->name
[j
] == ' ') {
609 for (i
= lossy_conversion
? 1 : 0; i
< 999999; i
++) {
612 int len
= snprintf(tail
, sizeof(tail
), "~%u", (unsigned)i
);
614 memcpy(entry
->name
+ MIN(j
, 8 - len
), tail
, len
);
616 for (entry1
= array_get(&(s
->directory
), directory_start
);
617 entry1
< entry
; entry1
++) {
618 if (!is_long_name(entry1
) &&
619 !memcmp(entry1
->name
, entry
->name
, 11)) {
620 break; /* found dupe */
623 if (entry1
== entry
) {
633 static inline uint8_t fat_chksum(const direntry_t
* entry
)
638 for (i
= 0; i
< ARRAY_SIZE(entry
->name
); i
++) {
639 chksum
= (((chksum
& 0xfe) >> 1) |
640 ((chksum
& 0x01) ? 0x80 : 0)) + entry
->name
[i
];
646 /* if return_time==0, this returns the fat_date, else the fat_time */
647 static uint16_t fat_datetime(time_t time
,int return_time
) {
651 localtime_r(&time
,t
);
653 return cpu_to_le16((t
->tm_sec
/2)|(t
->tm_min
<<5)|(t
->tm_hour
<<11));
654 return cpu_to_le16((t
->tm_mday
)|((t
->tm_mon
+1)<<5)|((t
->tm_year
-80)<<9));
657 static inline void fat_set(BDRVVVFATState
* s
,unsigned int cluster
,uint32_t value
)
659 if(s
->fat_type
==32) {
660 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
661 *entry
=cpu_to_le32(value
);
662 } else if(s
->fat_type
==16) {
663 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
664 *entry
=cpu_to_le16(value
&0xffff);
666 int offset
= (cluster
*3/2);
667 unsigned char* p
= array_get(&(s
->fat
), offset
);
671 p
[1] = (p
[1]&0xf0) | ((value
>>8)&0xf);
674 p
[0] = (p
[0]&0xf) | ((value
&0xf)<<4);
681 static inline uint32_t fat_get(BDRVVVFATState
* s
,unsigned int cluster
)
683 if(s
->fat_type
==32) {
684 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
685 return le32_to_cpu(*entry
);
686 } else if(s
->fat_type
==16) {
687 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
688 return le16_to_cpu(*entry
);
690 const uint8_t* x
=(uint8_t*)(s
->fat
.pointer
)+cluster
*3/2;
691 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
695 static inline int fat_eof(BDRVVVFATState
* s
,uint32_t fat_entry
)
697 if(fat_entry
>s
->max_fat_value
-8)
702 static inline void init_fat(BDRVVVFATState
* s
)
704 if (s
->fat_type
== 12) {
705 array_init(&(s
->fat
),1);
706 array_ensure_allocated(&(s
->fat
),
707 s
->sectors_per_fat
* 0x200 * 3 / 2 - 1);
709 array_init(&(s
->fat
),(s
->fat_type
==32?4:2));
710 array_ensure_allocated(&(s
->fat
),
711 s
->sectors_per_fat
* 0x200 / s
->fat
.item_size
- 1);
713 memset(s
->fat
.pointer
,0,s
->fat
.size
);
715 switch(s
->fat_type
) {
716 case 12: s
->max_fat_value
=0xfff; break;
717 case 16: s
->max_fat_value
=0xffff; break;
718 case 32: s
->max_fat_value
=0x0fffffff; break;
719 default: s
->max_fat_value
=0; /* error... */
724 static inline direntry_t
* create_short_and_long_name(BDRVVVFATState
* s
,
725 unsigned int directory_start
, const char* filename
, int is_dot
)
727 int long_index
= s
->directory
.next
;
728 direntry_t
* entry
= NULL
;
729 direntry_t
* entry_long
= NULL
;
732 entry
=array_get_next(&(s
->directory
));
733 memset(entry
->name
, 0x20, sizeof(entry
->name
));
734 memcpy(entry
->name
,filename
,strlen(filename
));
738 entry_long
=create_long_filename(s
,filename
);
739 entry
= create_short_filename(s
, filename
, directory_start
);
741 /* calculate checksum; propagate to long name */
743 uint8_t chksum
=fat_chksum(entry
);
745 /* calculate anew, because realloc could have taken place */
746 entry_long
=array_get(&(s
->directory
),long_index
);
747 while(entry_long
<entry
&& is_long_name(entry_long
)) {
748 entry_long
->reserved
[1]=chksum
;
757 * Read a directory. (the index of the corresponding mapping must be passed).
759 static int read_directory(BDRVVVFATState
* s
, int mapping_index
)
761 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
762 direntry_t
* direntry
;
763 const char* dirname
= mapping
->path
;
764 int first_cluster
= mapping
->begin
;
765 int parent_index
= mapping
->info
.dir
.parent_mapping_index
;
766 mapping_t
* parent_mapping
= (mapping_t
*)
767 (parent_index
>= 0 ? array_get(&(s
->mapping
), parent_index
) : NULL
);
768 int first_cluster_of_parent
= parent_mapping
? parent_mapping
->begin
: -1;
770 DIR* dir
=opendir(dirname
);
771 struct dirent
* entry
;
774 assert(mapping
->mode
& MODE_DIRECTORY
);
777 mapping
->end
= mapping
->begin
;
781 i
= mapping
->info
.dir
.first_dir_index
=
782 first_cluster
== 0 ? 0 : s
->directory
.next
;
784 if (first_cluster
!= 0) {
785 /* create the top entries of a subdirectory */
786 (void)create_short_and_long_name(s
, i
, ".", 1);
787 (void)create_short_and_long_name(s
, i
, "..", 1);
790 /* actually read the directory, and allocate the mappings */
791 while((entry
=readdir(dir
))) {
792 unsigned int length
=strlen(dirname
)+2+strlen(entry
->d_name
);
794 direntry_t
* direntry
;
796 int is_dot
=!strcmp(entry
->d_name
,".");
797 int is_dotdot
=!strcmp(entry
->d_name
,"..");
799 if (first_cluster
== 0 && s
->directory
.next
>= s
->root_entries
- 1) {
800 fprintf(stderr
, "Too many entries in root directory\n");
805 if(first_cluster
== 0 && (is_dotdot
|| is_dot
))
808 buffer
= g_malloc(length
);
809 snprintf(buffer
,length
,"%s/%s",dirname
,entry
->d_name
);
811 if(stat(buffer
,&st
)<0) {
816 /* create directory entry for this file */
817 if (!is_dot
&& !is_dotdot
) {
818 direntry
= create_short_and_long_name(s
, i
, entry
->d_name
, 0);
820 direntry
= array_get(&(s
->directory
), is_dot
? i
: i
+ 1);
822 direntry
->attributes
=(S_ISDIR(st
.st_mode
)?0x10:0x20);
823 direntry
->reserved
[0]=direntry
->reserved
[1]=0;
824 direntry
->ctime
=fat_datetime(st
.st_ctime
,1);
825 direntry
->cdate
=fat_datetime(st
.st_ctime
,0);
826 direntry
->adate
=fat_datetime(st
.st_atime
,0);
827 direntry
->begin_hi
=0;
828 direntry
->mtime
=fat_datetime(st
.st_mtime
,1);
829 direntry
->mdate
=fat_datetime(st
.st_mtime
,0);
831 set_begin_of_direntry(direntry
, first_cluster_of_parent
);
833 set_begin_of_direntry(direntry
, first_cluster
);
835 direntry
->begin
=0; /* do that later */
836 if (st
.st_size
> 0x7fffffff) {
837 fprintf(stderr
, "File %s is larger than 2GB\n", buffer
);
842 direntry
->size
=cpu_to_le32(S_ISDIR(st
.st_mode
)?0:st
.st_size
);
844 /* create mapping for this file */
845 if(!is_dot
&& !is_dotdot
&& (S_ISDIR(st
.st_mode
) || st
.st_size
)) {
846 s
->current_mapping
= array_get_next(&(s
->mapping
));
847 s
->current_mapping
->begin
=0;
848 s
->current_mapping
->end
=st
.st_size
;
850 * we get the direntry of the most recent direntry, which
851 * contains the short name and all the relevant information.
853 s
->current_mapping
->dir_index
=s
->directory
.next
-1;
854 s
->current_mapping
->first_mapping_index
= -1;
855 if (S_ISDIR(st
.st_mode
)) {
856 s
->current_mapping
->mode
= MODE_DIRECTORY
;
857 s
->current_mapping
->info
.dir
.parent_mapping_index
=
860 s
->current_mapping
->mode
= MODE_UNDEFINED
;
861 s
->current_mapping
->info
.file
.offset
= 0;
863 s
->current_mapping
->path
=buffer
;
864 s
->current_mapping
->read_only
=
865 (st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) == 0;
872 /* fill with zeroes up to the end of the cluster */
873 while(s
->directory
.next
%(0x10*s
->sectors_per_cluster
)) {
874 direntry_t
* direntry
=array_get_next(&(s
->directory
));
875 memset(direntry
,0,sizeof(direntry_t
));
878 if (s
->fat_type
!= 32 &&
879 mapping_index
== 0 &&
880 s
->directory
.next
< s
->root_entries
) {
882 int cur
= s
->directory
.next
;
883 array_ensure_allocated(&(s
->directory
), s
->root_entries
- 1);
884 s
->directory
.next
= s
->root_entries
;
885 memset(array_get(&(s
->directory
), cur
), 0,
886 (s
->root_entries
- cur
) * sizeof(direntry_t
));
889 /* re-get the mapping, since s->mapping was possibly realloc()ed */
890 mapping
= array_get(&(s
->mapping
), mapping_index
);
891 first_cluster
+= (s
->directory
.next
- mapping
->info
.dir
.first_dir_index
)
892 * 0x20 / s
->cluster_size
;
893 mapping
->end
= first_cluster
;
895 direntry
= array_get(&(s
->directory
), mapping
->dir_index
);
896 set_begin_of_direntry(direntry
, mapping
->begin
);
901 static inline uint32_t sector2cluster(BDRVVVFATState
* s
,off_t sector_num
)
903 return (sector_num
- s
->offset_to_root_dir
) / s
->sectors_per_cluster
;
906 static inline off_t
cluster2sector(BDRVVVFATState
* s
, uint32_t cluster_num
)
908 return s
->offset_to_root_dir
+ s
->sectors_per_cluster
* cluster_num
;
911 static int init_directories(BDRVVVFATState
* s
,
912 const char *dirname
, int heads
, int secs
,
915 bootsector_t
* bootsector
;
918 unsigned int cluster
;
920 memset(&(s
->first_sectors
[0]),0,0x40*0x200);
922 s
->cluster_size
=s
->sectors_per_cluster
*0x200;
923 s
->cluster_buffer
=g_malloc(s
->cluster_size
);
926 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
927 * where sc is sector_count,
928 * spf is sectors_per_fat,
929 * spc is sectors_per_clusters, and
930 * fat_type = 12, 16 or 32.
932 i
= 1+s
->sectors_per_cluster
*0x200*8/s
->fat_type
;
933 s
->sectors_per_fat
=(s
->sector_count
+i
)/i
; /* round up */
935 s
->offset_to_fat
= s
->offset_to_bootsector
+ 1;
936 s
->offset_to_root_dir
= s
->offset_to_fat
+ s
->sectors_per_fat
* 2;
938 array_init(&(s
->mapping
),sizeof(mapping_t
));
939 array_init(&(s
->directory
),sizeof(direntry_t
));
941 /* add volume label */
943 direntry_t
* entry
=array_get_next(&(s
->directory
));
944 entry
->attributes
=0x28; /* archive | volume label */
945 memcpy(entry
->name
, s
->volume_label
, sizeof(entry
->name
));
948 /* Now build FAT, and write back information into directory */
951 /* TODO: if there are more entries, bootsector has to be adjusted! */
952 s
->root_entries
= 0x02 * 0x10 * s
->sectors_per_cluster
;
953 s
->cluster_count
=sector2cluster(s
, s
->sector_count
);
955 mapping
= array_get_next(&(s
->mapping
));
957 mapping
->dir_index
= 0;
958 mapping
->info
.dir
.parent_mapping_index
= -1;
959 mapping
->first_mapping_index
= -1;
960 mapping
->path
= g_strdup(dirname
);
961 i
= strlen(mapping
->path
);
962 if (i
> 0 && mapping
->path
[i
- 1] == '/')
963 mapping
->path
[i
- 1] = '\0';
964 mapping
->mode
= MODE_DIRECTORY
;
965 mapping
->read_only
= 0;
966 s
->path
= mapping
->path
;
968 for (i
= 0, cluster
= 0; i
< s
->mapping
.next
; i
++) {
969 /* MS-DOS expects the FAT to be 0 for the root directory
970 * (except for the media byte). */
971 /* LATER TODO: still true for FAT32? */
972 int fix_fat
= (i
!= 0);
973 mapping
= array_get(&(s
->mapping
), i
);
975 if (mapping
->mode
& MODE_DIRECTORY
) {
976 char *path
= mapping
->path
;
977 mapping
->begin
= cluster
;
978 if(read_directory(s
, i
)) {
979 error_setg(errp
, "Could not read directory %s", path
);
982 mapping
= array_get(&(s
->mapping
), i
);
984 assert(mapping
->mode
== MODE_UNDEFINED
);
985 mapping
->mode
=MODE_NORMAL
;
986 mapping
->begin
= cluster
;
987 if (mapping
->end
> 0) {
988 direntry_t
* direntry
= array_get(&(s
->directory
),
991 mapping
->end
= cluster
+ 1 + (mapping
->end
-1)/s
->cluster_size
;
992 set_begin_of_direntry(direntry
, mapping
->begin
);
994 mapping
->end
= cluster
+ 1;
999 assert(mapping
->begin
< mapping
->end
);
1001 /* next free cluster */
1002 cluster
= mapping
->end
;
1004 if(cluster
> s
->cluster_count
) {
1006 "Directory does not fit in FAT%d (capacity %.2f MB)",
1007 s
->fat_type
, s
->sector_count
/ 2000.0);
1011 /* fix fat for entry */
1014 for(j
= mapping
->begin
; j
< mapping
->end
- 1; j
++)
1016 fat_set(s
, mapping
->end
- 1, s
->max_fat_value
);
1020 mapping
= array_get(&(s
->mapping
), 0);
1021 s
->last_cluster_of_root_directory
= mapping
->end
;
1023 /* the FAT signature */
1024 fat_set(s
,0,s
->max_fat_value
);
1025 fat_set(s
,1,s
->max_fat_value
);
1027 s
->current_mapping
= NULL
;
1029 bootsector
= (bootsector_t
*)(s
->first_sectors
1030 + s
->offset_to_bootsector
* 0x200);
1031 bootsector
->jump
[0]=0xeb;
1032 bootsector
->jump
[1]=0x3e;
1033 bootsector
->jump
[2]=0x90;
1034 memcpy(bootsector
->name
, BOOTSECTOR_OEM_NAME
, 8);
1035 bootsector
->sector_size
=cpu_to_le16(0x200);
1036 bootsector
->sectors_per_cluster
=s
->sectors_per_cluster
;
1037 bootsector
->reserved_sectors
=cpu_to_le16(1);
1038 bootsector
->number_of_fats
=0x2; /* number of FATs */
1039 bootsector
->root_entries
= cpu_to_le16(s
->root_entries
);
1040 bootsector
->total_sectors16
=s
->sector_count
>0xffff?0:cpu_to_le16(s
->sector_count
);
1041 /* media descriptor: hard disk=0xf8, floppy=0xf0 */
1042 bootsector
->media_type
= (s
->offset_to_bootsector
> 0 ? 0xf8 : 0xf0);
1043 s
->fat
.pointer
[0] = bootsector
->media_type
;
1044 bootsector
->sectors_per_fat
=cpu_to_le16(s
->sectors_per_fat
);
1045 bootsector
->sectors_per_track
= cpu_to_le16(secs
);
1046 bootsector
->number_of_heads
= cpu_to_le16(heads
);
1047 bootsector
->hidden_sectors
= cpu_to_le32(s
->offset_to_bootsector
);
1048 bootsector
->total_sectors
=cpu_to_le32(s
->sector_count
>0xffff?s
->sector_count
:0);
1050 /* LATER TODO: if FAT32, this is wrong */
1051 /* drive_number: fda=0, hda=0x80 */
1052 bootsector
->u
.fat16
.drive_number
= s
->offset_to_bootsector
== 0 ? 0 : 0x80;
1053 bootsector
->u
.fat16
.signature
=0x29;
1054 bootsector
->u
.fat16
.id
=cpu_to_le32(0xfabe1afd);
1056 memcpy(bootsector
->u
.fat16
.volume_label
, s
->volume_label
,
1057 sizeof(bootsector
->u
.fat16
.volume_label
));
1058 memcpy(bootsector
->u
.fat16
.fat_type
,
1059 s
->fat_type
== 12 ? "FAT12 " : "FAT16 ", 8);
1060 bootsector
->magic
[0]=0x55; bootsector
->magic
[1]=0xaa;
1066 static BDRVVVFATState
*vvv
= NULL
;
1069 static int enable_write_target(BlockDriverState
*bs
, Error
**errp
);
1070 static int is_consistent(BDRVVVFATState
*s
);
1072 static QemuOptsList runtime_opts
= {
1074 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1078 .type
= QEMU_OPT_STRING
,
1079 .help
= "Host directory to map to the vvfat device",
1083 .type
= QEMU_OPT_NUMBER
,
1084 .help
= "FAT type (12, 16 or 32)",
1088 .type
= QEMU_OPT_BOOL
,
1089 .help
= "Create a floppy rather than a hard disk image",
1093 .type
= QEMU_OPT_STRING
,
1094 .help
= "Use a volume label other than QEMU VVFAT",
1098 .type
= QEMU_OPT_BOOL
,
1099 .help
= "Make the image writable",
1101 { /* end of list */ }
1105 static void vvfat_parse_filename(const char *filename
, QDict
*options
,
1109 bool floppy
= false;
1113 if (!strstart(filename
, "fat:", NULL
)) {
1114 error_setg(errp
, "File name string must start with 'fat:'");
1119 if (strstr(filename
, ":32:")) {
1121 } else if (strstr(filename
, ":16:")) {
1123 } else if (strstr(filename
, ":12:")) {
1127 if (strstr(filename
, ":floppy:")) {
1131 if (strstr(filename
, ":rw:")) {
1135 /* Get the directory name without options */
1136 i
= strrchr(filename
, ':') - filename
;
1138 if (filename
[i
- 2] == ':' && qemu_isalpha(filename
[i
- 1])) {
1139 /* workaround for DOS drive names */
1145 /* Fill in the options QDict */
1146 qdict_put_str(options
, "dir", filename
);
1147 qdict_put_int(options
, "fat-type", fat_type
);
1148 qdict_put_bool(options
, "floppy", floppy
);
1149 qdict_put_bool(options
, "rw", rw
);
1152 static int vvfat_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1155 BDRVVVFATState
*s
= bs
->opaque
;
1156 int cyls
, heads
, secs
;
1158 const char *dirname
, *label
;
1160 Error
*local_err
= NULL
;
1167 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1168 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1170 error_propagate(errp
, local_err
);
1175 dirname
= qemu_opt_get(opts
, "dir");
1177 error_setg(errp
, "vvfat block driver requires a 'dir' option");
1182 s
->fat_type
= qemu_opt_get_number(opts
, "fat-type", 0);
1183 floppy
= qemu_opt_get_bool(opts
, "floppy", false);
1185 memset(s
->volume_label
, ' ', sizeof(s
->volume_label
));
1186 label
= qemu_opt_get(opts
, "label");
1188 size_t label_length
= strlen(label
);
1189 if (label_length
> 11) {
1190 error_setg(errp
, "vvfat label cannot be longer than 11 bytes");
1194 memcpy(s
->volume_label
, label
, label_length
);
1196 memcpy(s
->volume_label
, "QEMU VVFAT", 10);
1200 /* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
1204 s
->sectors_per_cluster
= 2;
1206 secs
= s
->fat_type
== 12 ? 18 : 36;
1207 s
->sectors_per_cluster
= 1;
1212 /* 32MB or 504MB disk*/
1216 s
->offset_to_bootsector
= 0x3f;
1217 cyls
= s
->fat_type
== 12 ? 64 : 1024;
1222 switch (s
->fat_type
) {
1224 warn_report("FAT32 has not been tested. You are welcome to do so!");
1230 error_setg(errp
, "Valid FAT types are only 12, 16 and 32");
1238 /* LATER TODO: if FAT32, adjust */
1239 s
->sectors_per_cluster
=0x10;
1241 s
->current_cluster
=0xffffffff;
1244 s
->qcow_filename
= NULL
;
1246 s
->downcase_short_names
= 1;
1248 DLOG(fprintf(stderr
, "vvfat %s chs %d,%d,%d\n",
1249 dirname
, cyls
, heads
, secs
));
1251 s
->sector_count
= cyls
* heads
* secs
- s
->offset_to_bootsector
;
1253 if (qemu_opt_get_bool(opts
, "rw", false)) {
1254 if (!bdrv_is_read_only(bs
)) {
1255 ret
= enable_write_target(bs
, errp
);
1262 "Unable to set VVFAT to 'rw' when drive is read-only");
1265 } else if (!bdrv_is_read_only(bs
)) {
1266 error_report("Opening non-rw vvfat images without an explicit "
1267 "read-only=on option is deprecated. Future versions "
1268 "will refuse to open the image instead of "
1269 "automatically marking the image read-only.");
1270 /* read only is the default for safety */
1271 ret
= bdrv_set_read_only(bs
, true, &local_err
);
1273 error_propagate(errp
, local_err
);
1278 bs
->total_sectors
= cyls
* heads
* secs
;
1280 if (init_directories(s
, dirname
, heads
, secs
, errp
)) {
1285 s
->sector_count
= s
->offset_to_root_dir
1286 + s
->sectors_per_cluster
* s
->cluster_count
;
1288 /* Disable migration when vvfat is used rw */
1290 error_setg(&s
->migration_blocker
,
1291 "The vvfat (rw) format used by node '%s' "
1292 "does not support live migration",
1293 bdrv_get_device_or_node_name(bs
));
1294 ret
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1296 error_propagate(errp
, local_err
);
1297 error_free(s
->migration_blocker
);
1302 if (s
->offset_to_bootsector
> 0) {
1303 init_mbr(s
, cyls
, heads
, secs
);
1306 qemu_co_mutex_init(&s
->lock
);
1310 qemu_opts_del(opts
);
1314 static void vvfat_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1316 bs
->bl
.request_alignment
= BDRV_SECTOR_SIZE
; /* No sub-sector I/O */
1319 static inline void vvfat_close_current_file(BDRVVVFATState
*s
)
1321 if(s
->current_mapping
) {
1322 s
->current_mapping
= NULL
;
1323 if (s
->current_fd
) {
1324 qemu_close(s
->current_fd
);
1328 s
->current_cluster
= -1;
1331 /* mappings between index1 and index2-1 are supposed to be ordered
1332 * return value is the index of the last mapping for which end>cluster_num
1334 static inline int find_mapping_for_cluster_aux(BDRVVVFATState
* s
,int cluster_num
,int index1
,int index2
)
1339 index3
=(index1
+index2
)/2;
1340 mapping
=array_get(&(s
->mapping
),index3
);
1341 assert(mapping
->begin
< mapping
->end
);
1342 if(mapping
->begin
>=cluster_num
) {
1343 assert(index2
!=index3
|| index2
==0);
1349 return mapping
->end
<=cluster_num
? index2
: index1
;
1352 assert(index1
<=index2
);
1353 DLOG(mapping
=array_get(&(s
->mapping
),index1
);
1354 assert(mapping
->begin
<=cluster_num
);
1355 assert(index2
>= s
->mapping
.next
||
1356 ((mapping
= array_get(&(s
->mapping
),index2
)) &&
1357 mapping
->end
>cluster_num
)));
1361 static inline mapping_t
* find_mapping_for_cluster(BDRVVVFATState
* s
,int cluster_num
)
1363 int index
=find_mapping_for_cluster_aux(s
,cluster_num
,0,s
->mapping
.next
);
1365 if(index
>=s
->mapping
.next
)
1367 mapping
=array_get(&(s
->mapping
),index
);
1368 if(mapping
->begin
>cluster_num
)
1370 assert(mapping
->begin
<=cluster_num
&& mapping
->end
>cluster_num
);
1374 static int open_file(BDRVVVFATState
* s
,mapping_t
* mapping
)
1378 if(!s
->current_mapping
||
1379 strcmp(s
->current_mapping
->path
,mapping
->path
)) {
1381 int fd
= qemu_open(mapping
->path
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1384 vvfat_close_current_file(s
);
1386 s
->current_mapping
= mapping
;
1391 static inline int read_cluster(BDRVVVFATState
*s
,int cluster_num
)
1393 if(s
->current_cluster
!= cluster_num
) {
1396 assert(!s
->current_mapping
|| s
->current_fd
|| (s
->current_mapping
->mode
& MODE_DIRECTORY
));
1397 if(!s
->current_mapping
1398 || s
->current_mapping
->begin
>cluster_num
1399 || s
->current_mapping
->end
<=cluster_num
) {
1400 /* binary search of mappings for file */
1401 mapping_t
* mapping
=find_mapping_for_cluster(s
,cluster_num
);
1403 assert(!mapping
|| (cluster_num
>=mapping
->begin
&& cluster_num
<mapping
->end
));
1405 if (mapping
&& mapping
->mode
& MODE_DIRECTORY
) {
1406 vvfat_close_current_file(s
);
1407 s
->current_mapping
= mapping
;
1408 read_cluster_directory
:
1409 offset
= s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
);
1410 s
->cluster
= (unsigned char*)s
->directory
.pointer
+offset
1411 + 0x20*s
->current_mapping
->info
.dir
.first_dir_index
;
1412 assert(((s
->cluster
-(unsigned char*)s
->directory
.pointer
)%s
->cluster_size
)==0);
1413 assert((char*)s
->cluster
+s
->cluster_size
<= s
->directory
.pointer
+s
->directory
.next
*s
->directory
.item_size
);
1414 s
->current_cluster
= cluster_num
;
1418 if(open_file(s
,mapping
))
1420 } else if (s
->current_mapping
->mode
& MODE_DIRECTORY
)
1421 goto read_cluster_directory
;
1423 assert(s
->current_fd
);
1425 offset
=s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
)+s
->current_mapping
->info
.file
.offset
;
1426 if(lseek(s
->current_fd
, offset
, SEEK_SET
)!=offset
)
1428 s
->cluster
=s
->cluster_buffer
;
1429 result
=read(s
->current_fd
,s
->cluster
,s
->cluster_size
);
1431 s
->current_cluster
= -1;
1434 s
->current_cluster
= cluster_num
;
1440 static void print_direntry(const direntry_t
* direntry
)
1445 fprintf(stderr
, "direntry %p: ", direntry
);
1448 if(is_long_name(direntry
)) {
1449 unsigned char* c
=(unsigned char*)direntry
;
1451 for(i
=1;i
<11 && c
[i
] && c
[i
]!=0xff;i
+=2)
1452 #define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
1454 for(i
=14;i
<26 && c
[i
] && c
[i
]!=0xff;i
+=2)
1456 for(i
=28;i
<32 && c
[i
] && c
[i
]!=0xff;i
+=2)
1459 fprintf(stderr
, "%s\n", buffer
);
1463 ADD_CHAR(direntry
->name
[i
]);
1465 fprintf(stderr
,"%s attributes=0x%02x begin=%d size=%d\n",
1467 direntry
->attributes
,
1468 begin_of_direntry(direntry
),le32_to_cpu(direntry
->size
));
1472 static void print_mapping(const mapping_t
* mapping
)
1474 fprintf(stderr
, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1475 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1476 mapping
, mapping
->begin
, mapping
->end
, mapping
->dir_index
,
1477 mapping
->first_mapping_index
, mapping
->path
, mapping
->mode
);
1479 if (mapping
->mode
& MODE_DIRECTORY
)
1480 fprintf(stderr
, "parent_mapping_index = %d, first_dir_index = %d\n", mapping
->info
.dir
.parent_mapping_index
, mapping
->info
.dir
.first_dir_index
);
1482 fprintf(stderr
, "offset = %d\n", mapping
->info
.file
.offset
);
1486 static int vvfat_read(BlockDriverState
*bs
, int64_t sector_num
,
1487 uint8_t *buf
, int nb_sectors
)
1489 BDRVVVFATState
*s
= bs
->opaque
;
1492 for(i
=0;i
<nb_sectors
;i
++,sector_num
++) {
1493 if (sector_num
>= bs
->total_sectors
)
1498 ret
= bdrv_is_allocated(s
->qcow
->bs
, sector_num
* BDRV_SECTOR_SIZE
,
1499 (nb_sectors
- i
) * BDRV_SECTOR_SIZE
, &n
);
1504 DLOG(fprintf(stderr
, "sectors %" PRId64
"+%" PRId64
1505 " allocated\n", sector_num
,
1506 n
>> BDRV_SECTOR_BITS
));
1507 if (bdrv_read(s
->qcow
, sector_num
, buf
+ i
* 0x200,
1508 n
>> BDRV_SECTOR_BITS
)) {
1511 i
+= (n
>> BDRV_SECTOR_BITS
) - 1;
1512 sector_num
+= (n
>> BDRV_SECTOR_BITS
) - 1;
1515 DLOG(fprintf(stderr
, "sector %" PRId64
" not allocated\n",
1518 if (sector_num
< s
->offset_to_root_dir
) {
1519 if (sector_num
< s
->offset_to_fat
) {
1520 memcpy(buf
+ i
* 0x200,
1521 &(s
->first_sectors
[sector_num
* 0x200]),
1523 } else if (sector_num
< s
->offset_to_fat
+ s
->sectors_per_fat
) {
1524 memcpy(buf
+ i
* 0x200,
1525 &(s
->fat
.pointer
[(sector_num
1526 - s
->offset_to_fat
) * 0x200]),
1528 } else if (sector_num
< s
->offset_to_root_dir
) {
1529 memcpy(buf
+ i
* 0x200,
1530 &(s
->fat
.pointer
[(sector_num
- s
->offset_to_fat
1531 - s
->sectors_per_fat
) * 0x200]),
1535 uint32_t sector
= sector_num
- s
->offset_to_root_dir
,
1536 sector_offset_in_cluster
=(sector
%s
->sectors_per_cluster
),
1537 cluster_num
=sector
/s
->sectors_per_cluster
;
1538 if(cluster_num
> s
->cluster_count
|| read_cluster(s
, cluster_num
) != 0) {
1539 /* LATER TODO: strict: return -1; */
1540 memset(buf
+i
*0x200,0,0x200);
1543 memcpy(buf
+i
*0x200,s
->cluster
+sector_offset_in_cluster
*0x200,0x200);
1549 static int coroutine_fn
1550 vvfat_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1551 QEMUIOVector
*qiov
, int flags
)
1554 BDRVVVFATState
*s
= bs
->opaque
;
1555 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
1556 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
1559 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
1560 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
1562 buf
= g_try_malloc(bytes
);
1563 if (bytes
&& buf
== NULL
) {
1567 qemu_co_mutex_lock(&s
->lock
);
1568 ret
= vvfat_read(bs
, sector_num
, buf
, nb_sectors
);
1569 qemu_co_mutex_unlock(&s
->lock
);
1571 qemu_iovec_from_buf(qiov
, 0, buf
, bytes
);
1577 /* LATER TODO: statify all functions */
1580 * Idea of the write support (use snapshot):
1582 * 1. check if all data is consistent, recording renames, modifications,
1583 * new files and directories (in s->commits).
1585 * 2. if the data is not consistent, stop committing
1587 * 3. handle renames, and create new files and directories (do not yet
1588 * write their contents)
1590 * 4. walk the directories, fixing the mapping and direntries, and marking
1591 * the handled mappings as not deleted
1593 * 5. commit the contents of the files
1595 * 6. handle deleted files and directories
1599 typedef struct commit_t
{
1602 struct { uint32_t cluster
; } rename
;
1603 struct { int dir_index
; uint32_t modified_offset
; } writeout
;
1604 struct { uint32_t first_cluster
; } new_file
;
1605 struct { uint32_t cluster
; } mkdir
;
1607 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1609 ACTION_RENAME
, ACTION_WRITEOUT
, ACTION_NEW_FILE
, ACTION_MKDIR
1613 static void clear_commits(BDRVVVFATState
* s
)
1616 DLOG(fprintf(stderr
, "clear_commits (%d commits)\n", s
->commits
.next
));
1617 for (i
= 0; i
< s
->commits
.next
; i
++) {
1618 commit_t
* commit
= array_get(&(s
->commits
), i
);
1619 assert(commit
->path
|| commit
->action
== ACTION_WRITEOUT
);
1620 if (commit
->action
!= ACTION_WRITEOUT
) {
1621 assert(commit
->path
);
1622 g_free(commit
->path
);
1624 assert(commit
->path
== NULL
);
1626 s
->commits
.next
= 0;
1629 static void schedule_rename(BDRVVVFATState
* s
,
1630 uint32_t cluster
, char* new_path
)
1632 commit_t
* commit
= array_get_next(&(s
->commits
));
1633 commit
->path
= new_path
;
1634 commit
->param
.rename
.cluster
= cluster
;
1635 commit
->action
= ACTION_RENAME
;
1638 static void schedule_writeout(BDRVVVFATState
* s
,
1639 int dir_index
, uint32_t modified_offset
)
1641 commit_t
* commit
= array_get_next(&(s
->commits
));
1642 commit
->path
= NULL
;
1643 commit
->param
.writeout
.dir_index
= dir_index
;
1644 commit
->param
.writeout
.modified_offset
= modified_offset
;
1645 commit
->action
= ACTION_WRITEOUT
;
1648 static void schedule_new_file(BDRVVVFATState
* s
,
1649 char* path
, uint32_t first_cluster
)
1651 commit_t
* commit
= array_get_next(&(s
->commits
));
1652 commit
->path
= path
;
1653 commit
->param
.new_file
.first_cluster
= first_cluster
;
1654 commit
->action
= ACTION_NEW_FILE
;
1657 static void schedule_mkdir(BDRVVVFATState
* s
, uint32_t cluster
, char* path
)
1659 commit_t
* commit
= array_get_next(&(s
->commits
));
1660 commit
->path
= path
;
1661 commit
->param
.mkdir
.cluster
= cluster
;
1662 commit
->action
= ACTION_MKDIR
;
1667 * Since the sequence number is at most 0x3f, and the filename
1668 * length is at most 13 times the sequence number, the maximal
1669 * filename length is 0x3f * 13 bytes.
1671 unsigned char name
[0x3f * 13 + 1];
1672 gunichar2 name2
[0x3f * 13 + 1];
1674 int sequence_number
;
1677 static void lfn_init(long_file_name
* lfn
)
1679 lfn
->sequence_number
= lfn
->len
= 0;
1680 lfn
->checksum
= 0x100;
1683 /* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1684 static int parse_long_name(long_file_name
* lfn
,
1685 const direntry_t
* direntry
)
1688 const unsigned char* pointer
= (const unsigned char*)direntry
;
1690 if (!is_long_name(direntry
))
1693 if (pointer
[0] & 0x40) {
1694 /* first entry; do some initialization */
1695 lfn
->sequence_number
= pointer
[0] & 0x3f;
1696 lfn
->checksum
= pointer
[13];
1698 lfn
->name
[lfn
->sequence_number
* 13] = 0;
1699 } else if ((pointer
[0] & 0x3f) != --lfn
->sequence_number
) {
1700 /* not the expected sequence number */
1702 } else if (pointer
[13] != lfn
->checksum
) {
1703 /* not the expected checksum */
1705 } else if (pointer
[12] || pointer
[26] || pointer
[27]) {
1706 /* invalid zero fields */
1710 offset
= 13 * (lfn
->sequence_number
- 1);
1711 for (i
= 0, j
= 1; i
< 13; i
++, j
+=2) {
1717 if (pointer
[j
] == 0 && pointer
[j
+ 1] == 0) {
1718 /* end of long file name */
1721 gunichar2 c
= (pointer
[j
+ 1] << 8) + pointer
[j
];
1722 lfn
->name2
[offset
+ i
] = c
;
1725 if (pointer
[0] & 0x40) {
1726 /* first entry; set len */
1727 lfn
->len
= offset
+ i
;
1729 if ((pointer
[0] & 0x3f) == 0x01) {
1730 /* last entry; finalize entry */
1732 gchar
*utf8
= g_utf16_to_utf8(lfn
->name2
, lfn
->len
, NULL
, &olen
, NULL
);
1737 memcpy(lfn
->name
, utf8
, olen
+ 1);
1744 /* returns 0 if successful, >0 if no short_name, and <0 on error */
1745 static int parse_short_name(BDRVVVFATState
* s
,
1746 long_file_name
* lfn
, direntry_t
* direntry
)
1750 if (!is_short_name(direntry
))
1753 for (j
= 7; j
>= 0 && direntry
->name
[j
] == ' '; j
--);
1754 for (i
= 0; i
<= j
; i
++) {
1755 uint8_t c
= direntry
->name
[i
];
1756 if (c
!= to_valid_short_char(c
)) {
1758 } else if (s
->downcase_short_names
) {
1759 lfn
->name
[i
] = qemu_tolower(direntry
->name
[i
]);
1761 lfn
->name
[i
] = direntry
->name
[i
];
1765 for (j
= 2; j
>= 0 && direntry
->name
[8 + j
] == ' '; j
--) {
1768 lfn
->name
[i
++] = '.';
1769 lfn
->name
[i
+ j
+ 1] = '\0';
1770 for (;j
>= 0; j
--) {
1771 uint8_t c
= direntry
->name
[8 + j
];
1772 if (c
!= to_valid_short_char(c
)) {
1774 } else if (s
->downcase_short_names
) {
1775 lfn
->name
[i
+ j
] = qemu_tolower(c
);
1777 lfn
->name
[i
+ j
] = c
;
1781 lfn
->name
[i
+ j
+ 1] = '\0';
1783 if (lfn
->name
[0] == DIR_KANJI_FAKE
) {
1784 lfn
->name
[0] = DIR_KANJI
;
1786 lfn
->len
= strlen((char*)lfn
->name
);
1791 static inline uint32_t modified_fat_get(BDRVVVFATState
* s
,
1792 unsigned int cluster
)
1794 if (cluster
< s
->last_cluster_of_root_directory
) {
1795 if (cluster
+ 1 == s
->last_cluster_of_root_directory
)
1796 return s
->max_fat_value
;
1801 if (s
->fat_type
==32) {
1802 uint32_t* entry
=((uint32_t*)s
->fat2
)+cluster
;
1803 return le32_to_cpu(*entry
);
1804 } else if (s
->fat_type
==16) {
1805 uint16_t* entry
=((uint16_t*)s
->fat2
)+cluster
;
1806 return le16_to_cpu(*entry
);
1808 const uint8_t* x
=s
->fat2
+cluster
*3/2;
1809 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
1813 static inline bool cluster_was_modified(BDRVVVFATState
*s
,
1814 uint32_t cluster_num
)
1816 int was_modified
= 0;
1819 if (s
->qcow
== NULL
) {
1823 for (i
= 0; !was_modified
&& i
< s
->sectors_per_cluster
; i
++) {
1824 was_modified
= bdrv_is_allocated(s
->qcow
->bs
,
1825 (cluster2sector(s
, cluster_num
) +
1826 i
) * BDRV_SECTOR_SIZE
,
1827 BDRV_SECTOR_SIZE
, NULL
);
1831 * Note that this treats failures to learn allocation status the
1832 * same as if an allocation has occurred. It's as safe as
1833 * anything else, given that a failure to learn allocation status
1834 * will probably result in more failures.
1836 return !!was_modified
;
1839 static const char* get_basename(const char* path
)
1841 char* basename
= strrchr(path
, '/');
1842 if (basename
== NULL
)
1845 return basename
+ 1; /* strip '/' */
1849 * The array s->used_clusters holds the states of the clusters. If it is
1850 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1851 * was modified, bit 3 is set.
1852 * If any cluster is allocated, but not part of a file or directory, this
1853 * driver refuses to commit.
1856 USED_DIRECTORY
= 1, USED_FILE
= 2, USED_ANY
= 3, USED_ALLOCATED
= 4
1860 * get_cluster_count_for_direntry() not only determines how many clusters
1861 * are occupied by direntry, but also if it was renamed or modified.
1863 * A file is thought to be renamed *only* if there already was a file with
1864 * exactly the same first cluster, but a different name.
1866 * Further, the files/directories handled by this function are
1867 * assumed to be *not* deleted (and *only* those).
1869 static uint32_t get_cluster_count_for_direntry(BDRVVVFATState
* s
,
1870 direntry_t
* direntry
, const char* path
)
1873 * This is a little bit tricky:
1874 * IF the guest OS just inserts a cluster into the file chain,
1875 * and leaves the rest alone, (i.e. the original file had clusters
1876 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1878 * - do_commit will write the cluster into the file at the given
1881 * - the cluster which is overwritten should be moved to a later
1882 * position in the file.
1884 * I am not aware that any OS does something as braindead, but this
1885 * situation could happen anyway when not committing for a long time.
1886 * Just to be sure that this does not bite us, detect it, and copy the
1887 * contents of the clusters to-be-overwritten into the qcow.
1890 int was_modified
= 0;
1893 uint32_t cluster_num
= begin_of_direntry(direntry
);
1894 uint32_t offset
= 0;
1895 int first_mapping_index
= -1;
1896 mapping_t
* mapping
= NULL
;
1897 const char* basename2
= NULL
;
1899 vvfat_close_current_file(s
);
1901 /* the root directory */
1902 if (cluster_num
== 0)
1907 basename2
= get_basename(path
);
1909 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1912 const char* basename
;
1914 assert(mapping
->mode
& MODE_DELETED
);
1915 mapping
->mode
&= ~MODE_DELETED
;
1917 basename
= get_basename(mapping
->path
);
1919 assert(mapping
->mode
& MODE_NORMAL
);
1922 if (strcmp(basename
, basename2
))
1923 schedule_rename(s
, cluster_num
, g_strdup(path
));
1924 } else if (is_file(direntry
))
1926 schedule_new_file(s
, g_strdup(path
), cluster_num
);
1935 if (!copy_it
&& cluster_was_modified(s
, cluster_num
)) {
1936 if (mapping
== NULL
||
1937 mapping
->begin
> cluster_num
||
1938 mapping
->end
<= cluster_num
)
1939 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1943 (mapping
->mode
& MODE_DIRECTORY
) == 0) {
1945 /* was modified in qcow */
1946 if (offset
!= mapping
->info
.file
.offset
+ s
->cluster_size
1947 * (cluster_num
- mapping
->begin
)) {
1948 /* offset of this cluster in file chain has changed */
1951 } else if (offset
== 0) {
1952 const char* basename
= get_basename(mapping
->path
);
1954 if (strcmp(basename
, basename2
))
1956 first_mapping_index
= array_index(&(s
->mapping
), mapping
);
1959 if (mapping
->first_mapping_index
!= first_mapping_index
1960 && mapping
->info
.file
.offset
> 0) {
1965 /* need to write out? */
1966 if (!was_modified
&& is_file(direntry
)) {
1968 schedule_writeout(s
, mapping
->dir_index
, offset
);
1976 * This is horribly inefficient, but that is okay, since
1977 * it is rarely executed, if at all.
1979 int64_t offset
= cluster2sector(s
, cluster_num
);
1981 vvfat_close_current_file(s
);
1982 for (i
= 0; i
< s
->sectors_per_cluster
; i
++) {
1985 res
= bdrv_is_allocated(s
->qcow
->bs
,
1986 (offset
+ i
) * BDRV_SECTOR_SIZE
,
1987 BDRV_SECTOR_SIZE
, NULL
);
1992 res
= vvfat_read(s
->bs
, offset
, s
->cluster_buffer
, 1);
1996 res
= bdrv_write(s
->qcow
, offset
, s
->cluster_buffer
, 1);
2006 if (s
->used_clusters
[cluster_num
] & USED_ANY
)
2008 s
->used_clusters
[cluster_num
] = USED_FILE
;
2010 cluster_num
= modified_fat_get(s
, cluster_num
);
2012 if (fat_eof(s
, cluster_num
))
2014 else if (cluster_num
< 2 || cluster_num
> s
->max_fat_value
- 16)
2017 offset
+= s
->cluster_size
;
2022 * This function looks at the modified data (qcow).
2023 * It returns 0 upon inconsistency or error, and the number of clusters
2024 * used by the directory, its subdirectories and their files.
2026 static int check_directory_consistency(BDRVVVFATState
*s
,
2027 int cluster_num
, const char* path
)
2030 unsigned char* cluster
= g_malloc(s
->cluster_size
);
2031 direntry_t
* direntries
= (direntry_t
*)cluster
;
2032 mapping_t
* mapping
= find_mapping_for_cluster(s
, cluster_num
);
2035 int path_len
= strlen(path
);
2036 char path2
[PATH_MAX
+ 1];
2038 assert(path_len
< PATH_MAX
); /* len was tested before! */
2039 pstrcpy(path2
, sizeof(path2
), path
);
2040 path2
[path_len
] = '/';
2041 path2
[path_len
+ 1] = '\0';
2044 const char* basename
= get_basename(mapping
->path
);
2045 const char* basename2
= get_basename(path
);
2047 assert(mapping
->mode
& MODE_DIRECTORY
);
2049 assert(mapping
->mode
& MODE_DELETED
);
2050 mapping
->mode
&= ~MODE_DELETED
;
2052 if (strcmp(basename
, basename2
))
2053 schedule_rename(s
, cluster_num
, g_strdup(path
));
2056 schedule_mkdir(s
, cluster_num
, g_strdup(path
));
2065 if (s
->used_clusters
[cluster_num
] & USED_ANY
) {
2066 fprintf(stderr
, "cluster %d used more than once\n", (int)cluster_num
);
2069 s
->used_clusters
[cluster_num
] = USED_DIRECTORY
;
2071 DLOG(fprintf(stderr
, "read cluster %d (sector %d)\n", (int)cluster_num
, (int)cluster2sector(s
, cluster_num
)));
2072 subret
= vvfat_read(s
->bs
, cluster2sector(s
, cluster_num
), cluster
,
2073 s
->sectors_per_cluster
);
2075 fprintf(stderr
, "Error fetching direntries\n");
2081 for (i
= 0; i
< 0x10 * s
->sectors_per_cluster
; i
++) {
2082 int cluster_count
= 0;
2084 DLOG(fprintf(stderr
, "check direntry %d:\n", i
); print_direntry(direntries
+ i
));
2085 if (is_volume_label(direntries
+ i
) || is_dot(direntries
+ i
) ||
2086 is_free(direntries
+ i
))
2089 subret
= parse_long_name(&lfn
, direntries
+ i
);
2091 fprintf(stderr
, "Error in long name\n");
2094 if (subret
== 0 || is_free(direntries
+ i
))
2097 if (fat_chksum(direntries
+i
) != lfn
.checksum
) {
2098 subret
= parse_short_name(s
, &lfn
, direntries
+ i
);
2100 fprintf(stderr
, "Error in short name (%d)\n", subret
);
2103 if (subret
> 0 || !strcmp((char*)lfn
.name
, ".")
2104 || !strcmp((char*)lfn
.name
, ".."))
2107 lfn
.checksum
= 0x100; /* cannot use long name twice */
2109 if (path_len
+ 1 + lfn
.len
>= PATH_MAX
) {
2110 fprintf(stderr
, "Name too long: %s/%s\n", path
, lfn
.name
);
2113 pstrcpy(path2
+ path_len
+ 1, sizeof(path2
) - path_len
- 1,
2116 if (is_directory(direntries
+ i
)) {
2117 if (begin_of_direntry(direntries
+ i
) == 0) {
2118 DLOG(fprintf(stderr
, "invalid begin for directory: %s\n", path2
); print_direntry(direntries
+ i
));
2121 cluster_count
= check_directory_consistency(s
,
2122 begin_of_direntry(direntries
+ i
), path2
);
2123 if (cluster_count
== 0) {
2124 DLOG(fprintf(stderr
, "problem in directory %s:\n", path2
); print_direntry(direntries
+ i
));
2127 } else if (is_file(direntries
+ i
)) {
2128 /* check file size with FAT */
2129 cluster_count
= get_cluster_count_for_direntry(s
, direntries
+ i
, path2
);
2130 if (cluster_count
!=
2131 DIV_ROUND_UP(le32_to_cpu(direntries
[i
].size
), s
->cluster_size
)) {
2132 DLOG(fprintf(stderr
, "Cluster count mismatch\n"));
2136 abort(); /* cluster_count = 0; */
2138 ret
+= cluster_count
;
2141 cluster_num
= modified_fat_get(s
, cluster_num
);
2142 } while(!fat_eof(s
, cluster_num
));
2148 /* returns 1 on success */
2149 static int is_consistent(BDRVVVFATState
* s
)
2152 int used_clusters_count
= 0;
2156 * - get modified FAT
2157 * - compare the two FATs (TODO)
2158 * - get buffer for marking used clusters
2159 * - recurse direntries from root (using bs->bdrv_read to make
2160 * sure to get the new data)
2161 * - check that the FAT agrees with the size
2162 * - count the number of clusters occupied by this directory and
2164 * - check that the cumulative used cluster count agrees with the
2166 * - if all is fine, return number of used clusters
2168 if (s
->fat2
== NULL
) {
2169 int size
= 0x200 * s
->sectors_per_fat
;
2170 s
->fat2
= g_malloc(size
);
2171 memcpy(s
->fat2
, s
->fat
.pointer
, size
);
2173 check
= vvfat_read(s
->bs
,
2174 s
->offset_to_fat
, s
->fat2
, s
->sectors_per_fat
);
2176 fprintf(stderr
, "Could not copy fat\n");
2179 assert (s
->used_clusters
);
2180 for (i
= 0; i
< sector2cluster(s
, s
->sector_count
); i
++)
2181 s
->used_clusters
[i
] &= ~USED_ANY
;
2185 /* mark every mapped file/directory as deleted.
2186 * (check_directory_consistency() will unmark those still present). */
2188 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2189 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2190 if (mapping
->first_mapping_index
< 0)
2191 mapping
->mode
|= MODE_DELETED
;
2194 used_clusters_count
= check_directory_consistency(s
, 0, s
->path
);
2195 if (used_clusters_count
<= 0) {
2196 DLOG(fprintf(stderr
, "problem in directory\n"));
2200 check
= s
->last_cluster_of_root_directory
;
2201 for (i
= check
; i
< sector2cluster(s
, s
->sector_count
); i
++) {
2202 if (modified_fat_get(s
, i
)) {
2203 if(!s
->used_clusters
[i
]) {
2204 DLOG(fprintf(stderr
, "FAT was modified (%d), but cluster is not used?\n", i
));
2210 if (s
->used_clusters
[i
] == USED_ALLOCATED
) {
2211 /* allocated, but not used... */
2212 DLOG(fprintf(stderr
, "unused, modified cluster: %d\n", i
));
2217 if (check
!= used_clusters_count
)
2220 return used_clusters_count
;
2223 static inline void adjust_mapping_indices(BDRVVVFATState
* s
,
2224 int offset
, int adjust
)
2228 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2229 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2231 #define ADJUST_MAPPING_INDEX(name) \
2232 if (mapping->name >= offset) \
2233 mapping->name += adjust
2235 ADJUST_MAPPING_INDEX(first_mapping_index
);
2236 if (mapping
->mode
& MODE_DIRECTORY
)
2237 ADJUST_MAPPING_INDEX(info
.dir
.parent_mapping_index
);
2241 /* insert or update mapping */
2242 static mapping_t
* insert_mapping(BDRVVVFATState
* s
,
2243 uint32_t begin
, uint32_t end
)
2246 * - find mapping where mapping->begin >= begin,
2247 * - if mapping->begin > begin: insert
2248 * - adjust all references to mappings!
2252 int index
= find_mapping_for_cluster_aux(s
, begin
, 0, s
->mapping
.next
);
2253 mapping_t
* mapping
= NULL
;
2254 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
2256 if (index
< s
->mapping
.next
&& (mapping
= array_get(&(s
->mapping
), index
))
2257 && mapping
->begin
< begin
) {
2258 mapping
->end
= begin
;
2260 mapping
= array_get(&(s
->mapping
), index
);
2262 if (index
>= s
->mapping
.next
|| mapping
->begin
> begin
) {
2263 mapping
= array_insert(&(s
->mapping
), index
, 1);
2264 mapping
->path
= NULL
;
2265 adjust_mapping_indices(s
, index
, +1);
2268 mapping
->begin
= begin
;
2271 DLOG(mapping_t
* next_mapping
;
2272 assert(index
+ 1 >= s
->mapping
.next
||
2273 ((next_mapping
= array_get(&(s
->mapping
), index
+ 1)) &&
2274 next_mapping
->begin
>= end
)));
2276 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
2277 s
->current_mapping
= array_get(&(s
->mapping
),
2278 s
->current_mapping
- first_mapping
);
2283 static int remove_mapping(BDRVVVFATState
* s
, int mapping_index
)
2285 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
2286 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
2289 if (mapping
->first_mapping_index
< 0) {
2290 g_free(mapping
->path
);
2293 /* remove from s->mapping */
2294 array_remove(&(s
->mapping
), mapping_index
);
2296 /* adjust all references to mappings */
2297 adjust_mapping_indices(s
, mapping_index
, -1);
2299 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
2300 s
->current_mapping
= array_get(&(s
->mapping
),
2301 s
->current_mapping
- first_mapping
);
2306 static void adjust_dirindices(BDRVVVFATState
* s
, int offset
, int adjust
)
2309 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2310 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2311 if (mapping
->dir_index
>= offset
)
2312 mapping
->dir_index
+= adjust
;
2313 if ((mapping
->mode
& MODE_DIRECTORY
) &&
2314 mapping
->info
.dir
.first_dir_index
>= offset
)
2315 mapping
->info
.dir
.first_dir_index
+= adjust
;
2319 static direntry_t
* insert_direntries(BDRVVVFATState
* s
,
2320 int dir_index
, int count
)
2323 * make room in s->directory,
2326 direntry_t
* result
= array_insert(&(s
->directory
), dir_index
, count
);
2329 adjust_dirindices(s
, dir_index
, count
);
2333 static int remove_direntries(BDRVVVFATState
* s
, int dir_index
, int count
)
2335 int ret
= array_remove_slice(&(s
->directory
), dir_index
, count
);
2338 adjust_dirindices(s
, dir_index
, -count
);
2343 * Adapt the mappings of the cluster chain starting at first cluster
2344 * (i.e. if a file starts at first_cluster, the chain is followed according
2345 * to the modified fat, and the corresponding entries in s->mapping are
2348 static int commit_mappings(BDRVVVFATState
* s
,
2349 uint32_t first_cluster
, int dir_index
)
2351 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2352 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2353 uint32_t cluster
= first_cluster
;
2355 vvfat_close_current_file(s
);
2358 assert(mapping
->begin
== first_cluster
);
2359 mapping
->first_mapping_index
= -1;
2360 mapping
->dir_index
= dir_index
;
2361 mapping
->mode
= (dir_index
<= 0 || is_directory(direntry
)) ?
2362 MODE_DIRECTORY
: MODE_NORMAL
;
2364 while (!fat_eof(s
, cluster
)) {
2367 for (c
= cluster
, c1
= modified_fat_get(s
, c
); c
+ 1 == c1
;
2368 c
= c1
, c1
= modified_fat_get(s
, c1
));
2371 if (c
> mapping
->end
) {
2372 int index
= array_index(&(s
->mapping
), mapping
);
2373 int i
, max_i
= s
->mapping
.next
- index
;
2374 for (i
= 1; i
< max_i
&& mapping
[i
].begin
< c
; i
++);
2376 remove_mapping(s
, index
+ 1);
2378 assert(mapping
== array_get(&(s
->mapping
), s
->mapping
.next
- 1)
2379 || mapping
[1].begin
>= c
);
2382 if (!fat_eof(s
, c1
)) {
2383 int i
= find_mapping_for_cluster_aux(s
, c1
, 0, s
->mapping
.next
);
2384 mapping_t
* next_mapping
= i
>= s
->mapping
.next
? NULL
:
2385 array_get(&(s
->mapping
), i
);
2387 if (next_mapping
== NULL
|| next_mapping
->begin
> c1
) {
2388 int i1
= array_index(&(s
->mapping
), mapping
);
2390 next_mapping
= insert_mapping(s
, c1
, c1
+1);
2394 mapping
= array_get(&(s
->mapping
), i1
);
2397 next_mapping
->dir_index
= mapping
->dir_index
;
2398 next_mapping
->first_mapping_index
=
2399 mapping
->first_mapping_index
< 0 ?
2400 array_index(&(s
->mapping
), mapping
) :
2401 mapping
->first_mapping_index
;
2402 next_mapping
->path
= mapping
->path
;
2403 next_mapping
->mode
= mapping
->mode
;
2404 next_mapping
->read_only
= mapping
->read_only
;
2405 if (mapping
->mode
& MODE_DIRECTORY
) {
2406 next_mapping
->info
.dir
.parent_mapping_index
=
2407 mapping
->info
.dir
.parent_mapping_index
;
2408 next_mapping
->info
.dir
.first_dir_index
=
2409 mapping
->info
.dir
.first_dir_index
+
2410 0x10 * s
->sectors_per_cluster
*
2411 (mapping
->end
- mapping
->begin
);
2413 next_mapping
->info
.file
.offset
= mapping
->info
.file
.offset
+
2414 mapping
->end
- mapping
->begin
;
2416 mapping
= next_mapping
;
2425 static int commit_direntries(BDRVVVFATState
* s
,
2426 int dir_index
, int parent_mapping_index
)
2428 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2429 uint32_t first_cluster
= dir_index
== 0 ? 0 : begin_of_direntry(direntry
);
2430 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2432 int factor
= 0x10 * s
->sectors_per_cluster
;
2433 int old_cluster_count
, new_cluster_count
;
2434 int current_dir_index
= mapping
->info
.dir
.first_dir_index
;
2435 int first_dir_index
= current_dir_index
;
2439 DLOG(fprintf(stderr
, "commit_direntries for %s, parent_mapping_index %d\n", mapping
->path
, parent_mapping_index
));
2443 assert(mapping
->begin
== first_cluster
);
2444 assert(mapping
->info
.dir
.first_dir_index
< s
->directory
.next
);
2445 assert(mapping
->mode
& MODE_DIRECTORY
);
2446 assert(dir_index
== 0 || is_directory(direntry
));
2448 mapping
->info
.dir
.parent_mapping_index
= parent_mapping_index
;
2450 if (first_cluster
== 0) {
2451 old_cluster_count
= new_cluster_count
=
2452 s
->last_cluster_of_root_directory
;
2454 for (old_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2456 old_cluster_count
++;
2458 for (new_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2459 c
= modified_fat_get(s
, c
))
2460 new_cluster_count
++;
2463 if (new_cluster_count
> old_cluster_count
) {
2464 if (insert_direntries(s
,
2465 current_dir_index
+ factor
* old_cluster_count
,
2466 factor
* (new_cluster_count
- old_cluster_count
)) == NULL
)
2468 } else if (new_cluster_count
< old_cluster_count
)
2469 remove_direntries(s
,
2470 current_dir_index
+ factor
* new_cluster_count
,
2471 factor
* (old_cluster_count
- new_cluster_count
));
2473 for (c
= first_cluster
; !fat_eof(s
, c
); c
= modified_fat_get(s
, c
)) {
2474 direntry_t
*first_direntry
;
2475 void* direntry
= array_get(&(s
->directory
), current_dir_index
);
2476 int ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
), direntry
,
2477 s
->sectors_per_cluster
);
2481 /* The first directory entry on the filesystem is the volume name */
2482 first_direntry
= (direntry_t
*) s
->directory
.pointer
;
2483 assert(!memcmp(first_direntry
->name
, s
->volume_label
, 11));
2485 current_dir_index
+= factor
;
2488 ret
= commit_mappings(s
, first_cluster
, dir_index
);
2493 for (i
= 0; i
< factor
* new_cluster_count
; i
++) {
2494 direntry
= array_get(&(s
->directory
), first_dir_index
+ i
);
2495 if (is_directory(direntry
) && !is_dot(direntry
)) {
2496 mapping
= find_mapping_for_cluster(s
, first_cluster
);
2497 assert(mapping
->mode
& MODE_DIRECTORY
);
2498 ret
= commit_direntries(s
, first_dir_index
+ i
,
2499 array_index(&(s
->mapping
), mapping
));
2508 /* commit one file (adjust contents, adjust mapping),
2509 return first_mapping_index */
2510 static int commit_one_file(BDRVVVFATState
* s
,
2511 int dir_index
, uint32_t offset
)
2513 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2514 uint32_t c
= begin_of_direntry(direntry
);
2515 uint32_t first_cluster
= c
;
2516 mapping_t
* mapping
= find_mapping_for_cluster(s
, c
);
2517 uint32_t size
= filesize_of_direntry(direntry
);
2518 char* cluster
= g_malloc(s
->cluster_size
);
2522 assert(offset
< size
);
2523 assert((offset
% s
->cluster_size
) == 0);
2525 for (i
= s
->cluster_size
; i
< offset
; i
+= s
->cluster_size
)
2526 c
= modified_fat_get(s
, c
);
2528 fd
= qemu_open(mapping
->path
, O_RDWR
| O_CREAT
| O_BINARY
, 0666);
2530 fprintf(stderr
, "Could not open %s... (%s, %d)\n", mapping
->path
,
2531 strerror(errno
), errno
);
2536 if (lseek(fd
, offset
, SEEK_SET
) != offset
) {
2543 while (offset
< size
) {
2545 int rest_size
= (size
- offset
> s
->cluster_size
?
2546 s
->cluster_size
: size
- offset
);
2549 c1
= modified_fat_get(s
, c
);
2551 assert((size
- offset
== 0 && fat_eof(s
, c
)) ||
2552 (size
> offset
&& c
>=2 && !fat_eof(s
, c
)));
2554 ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
),
2555 (uint8_t*)cluster
, DIV_ROUND_UP(rest_size
, 0x200));
2563 if (write(fd
, cluster
, rest_size
) < 0) {
2569 offset
+= rest_size
;
2573 if (ftruncate(fd
, size
)) {
2574 perror("ftruncate()");
2582 return commit_mappings(s
, first_cluster
, dir_index
);
2586 /* test, if all mappings point to valid direntries */
2587 static void check1(BDRVVVFATState
* s
)
2590 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2591 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2592 if (mapping
->mode
& MODE_DELETED
) {
2593 fprintf(stderr
, "deleted\n");
2596 assert(mapping
->dir_index
< s
->directory
.next
);
2597 direntry_t
* direntry
= array_get(&(s
->directory
), mapping
->dir_index
);
2598 assert(mapping
->begin
== begin_of_direntry(direntry
) || mapping
->first_mapping_index
>= 0);
2599 if (mapping
->mode
& MODE_DIRECTORY
) {
2600 assert(mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
* (mapping
->end
- mapping
->begin
) <= s
->directory
.next
);
2601 assert((mapping
->info
.dir
.first_dir_index
% (0x10 * s
->sectors_per_cluster
)) == 0);
2606 /* test, if all direntries have mappings */
2607 static void check2(BDRVVVFATState
* s
)
2610 int first_mapping
= -1;
2612 for (i
= 0; i
< s
->directory
.next
; i
++) {
2613 direntry_t
* direntry
= array_get(&(s
->directory
), i
);
2615 if (is_short_name(direntry
) && begin_of_direntry(direntry
)) {
2616 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin_of_direntry(direntry
));
2618 assert(mapping
->dir_index
== i
|| is_dot(direntry
));
2619 assert(mapping
->begin
== begin_of_direntry(direntry
) || is_dot(direntry
));
2622 if ((i
% (0x10 * s
->sectors_per_cluster
)) == 0) {
2626 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2627 mapping_t
* mapping
= array_get(&(s
->mapping
), j
);
2628 if (mapping
->mode
& MODE_DELETED
)
2630 if (mapping
->mode
& MODE_DIRECTORY
) {
2631 if (mapping
->info
.dir
.first_dir_index
<= i
&& mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
> i
) {
2632 assert(++count
== 1);
2633 if (mapping
->first_mapping_index
== -1)
2634 first_mapping
= array_index(&(s
->mapping
), mapping
);
2636 assert(first_mapping
== mapping
->first_mapping_index
);
2637 if (mapping
->info
.dir
.parent_mapping_index
< 0)
2640 mapping_t
* parent
= array_get(&(s
->mapping
), mapping
->info
.dir
.parent_mapping_index
);
2641 assert(parent
->mode
& MODE_DIRECTORY
);
2642 assert(parent
->info
.dir
.first_dir_index
< mapping
->info
.dir
.first_dir_index
);
2654 static int handle_renames_and_mkdirs(BDRVVVFATState
* s
)
2659 fprintf(stderr
, "handle_renames\n");
2660 for (i
= 0; i
< s
->commits
.next
; i
++) {
2661 commit_t
* commit
= array_get(&(s
->commits
), i
);
2662 fprintf(stderr
, "%d, %s (%d, %d)\n", i
, commit
->path
? commit
->path
: "(null)", commit
->param
.rename
.cluster
, commit
->action
);
2666 for (i
= 0; i
< s
->commits
.next
;) {
2667 commit_t
* commit
= array_get(&(s
->commits
), i
);
2668 if (commit
->action
== ACTION_RENAME
) {
2669 mapping_t
* mapping
= find_mapping_for_cluster(s
,
2670 commit
->param
.rename
.cluster
);
2671 char* old_path
= mapping
->path
;
2673 assert(commit
->path
);
2674 mapping
->path
= commit
->path
;
2675 if (rename(old_path
, mapping
->path
))
2678 if (mapping
->mode
& MODE_DIRECTORY
) {
2679 int l1
= strlen(mapping
->path
);
2680 int l2
= strlen(old_path
);
2682 direntry_t
* direntry
= array_get(&(s
->directory
),
2683 mapping
->info
.dir
.first_dir_index
);
2684 uint32_t c
= mapping
->begin
;
2688 while (!fat_eof(s
, c
)) {
2690 direntry_t
* d
= direntry
+ i
;
2692 if (is_file(d
) || (is_directory(d
) && !is_dot(d
))) {
2693 mapping_t
* m
= find_mapping_for_cluster(s
,
2694 begin_of_direntry(d
));
2695 int l
= strlen(m
->path
);
2696 char* new_path
= g_malloc(l
+ diff
+ 1);
2698 assert(!strncmp(m
->path
, mapping
->path
, l2
));
2700 pstrcpy(new_path
, l
+ diff
+ 1, mapping
->path
);
2701 pstrcpy(new_path
+ l1
, l
+ diff
+ 1 - l1
,
2704 schedule_rename(s
, m
->begin
, new_path
);
2707 } while((i
% (0x10 * s
->sectors_per_cluster
)) != 0);
2713 array_remove(&(s
->commits
), i
);
2715 } else if (commit
->action
== ACTION_MKDIR
) {
2717 int j
, parent_path_len
;
2720 if (mkdir(commit
->path
))
2723 if (mkdir(commit
->path
, 0755))
2727 mapping
= insert_mapping(s
, commit
->param
.mkdir
.cluster
,
2728 commit
->param
.mkdir
.cluster
+ 1);
2729 if (mapping
== NULL
)
2732 mapping
->mode
= MODE_DIRECTORY
;
2733 mapping
->read_only
= 0;
2734 mapping
->path
= commit
->path
;
2735 j
= s
->directory
.next
;
2737 insert_direntries(s
, s
->directory
.next
,
2738 0x10 * s
->sectors_per_cluster
);
2739 mapping
->info
.dir
.first_dir_index
= j
;
2741 parent_path_len
= strlen(commit
->path
)
2742 - strlen(get_basename(commit
->path
)) - 1;
2743 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2744 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2745 if (m
->first_mapping_index
< 0 && m
!= mapping
&&
2746 !strncmp(m
->path
, mapping
->path
, parent_path_len
) &&
2747 strlen(m
->path
) == parent_path_len
)
2750 assert(j
< s
->mapping
.next
);
2751 mapping
->info
.dir
.parent_mapping_index
= j
;
2753 array_remove(&(s
->commits
), i
);
2763 * TODO: make sure that the short name is not matching *another* file
2765 static int handle_commits(BDRVVVFATState
* s
)
2769 vvfat_close_current_file(s
);
2771 for (i
= 0; !fail
&& i
< s
->commits
.next
; i
++) {
2772 commit_t
* commit
= array_get(&(s
->commits
), i
);
2773 switch(commit
->action
) {
2774 case ACTION_RENAME
: case ACTION_MKDIR
:
2778 case ACTION_WRITEOUT
: {
2780 /* these variables are only used by assert() below */
2781 direntry_t
* entry
= array_get(&(s
->directory
),
2782 commit
->param
.writeout
.dir_index
);
2783 uint32_t begin
= begin_of_direntry(entry
);
2784 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2788 assert(mapping
->begin
== begin
);
2789 assert(commit
->path
== NULL
);
2791 if (commit_one_file(s
, commit
->param
.writeout
.dir_index
,
2792 commit
->param
.writeout
.modified_offset
))
2797 case ACTION_NEW_FILE
: {
2798 int begin
= commit
->param
.new_file
.first_cluster
;
2799 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2804 for (i
= 0; i
< s
->directory
.next
; i
++) {
2805 entry
= array_get(&(s
->directory
), i
);
2806 if (is_file(entry
) && begin_of_direntry(entry
) == begin
)
2810 if (i
>= s
->directory
.next
) {
2815 /* make sure there exists an initial mapping */
2816 if (mapping
&& mapping
->begin
!= begin
) {
2817 mapping
->end
= begin
;
2820 if (mapping
== NULL
) {
2821 mapping
= insert_mapping(s
, begin
, begin
+1);
2823 /* most members will be fixed in commit_mappings() */
2824 assert(commit
->path
);
2825 mapping
->path
= commit
->path
;
2826 mapping
->read_only
= 0;
2827 mapping
->mode
= MODE_NORMAL
;
2828 mapping
->info
.file
.offset
= 0;
2830 if (commit_one_file(s
, i
, 0))
2839 if (i
> 0 && array_remove_slice(&(s
->commits
), 0, i
))
2844 static int handle_deletes(BDRVVVFATState
* s
)
2846 int i
, deferred
= 1, deleted
= 1;
2848 /* delete files corresponding to mappings marked as deleted */
2849 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2850 while (deferred
&& deleted
) {
2854 for (i
= 1; i
< s
->mapping
.next
; i
++) {
2855 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2856 if (mapping
->mode
& MODE_DELETED
) {
2857 direntry_t
* entry
= array_get(&(s
->directory
),
2858 mapping
->dir_index
);
2860 if (is_free(entry
)) {
2861 /* remove file/directory */
2862 if (mapping
->mode
& MODE_DIRECTORY
) {
2863 int j
, next_dir_index
= s
->directory
.next
,
2864 first_dir_index
= mapping
->info
.dir
.first_dir_index
;
2866 if (rmdir(mapping
->path
) < 0) {
2867 if (errno
== ENOTEMPTY
) {
2874 for (j
= 1; j
< s
->mapping
.next
; j
++) {
2875 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2876 if (m
->mode
& MODE_DIRECTORY
&&
2877 m
->info
.dir
.first_dir_index
>
2879 m
->info
.dir
.first_dir_index
<
2882 m
->info
.dir
.first_dir_index
;
2884 remove_direntries(s
, first_dir_index
,
2885 next_dir_index
- first_dir_index
);
2890 if (unlink(mapping
->path
))
2894 DLOG(fprintf(stderr
, "DELETE (%d)\n", i
); print_mapping(mapping
); print_direntry(entry
));
2895 remove_mapping(s
, i
);
2904 * synchronize mapping with new state:
2906 * - copy FAT (with bdrv_read)
2907 * - mark all filenames corresponding to mappings as deleted
2908 * - recurse direntries from root (using bs->bdrv_read)
2909 * - delete files corresponding to mappings marked as deleted
2911 static int do_commit(BDRVVVFATState
* s
)
2915 /* the real meat are the commits. Nothing to do? Move along! */
2916 if (s
->commits
.next
== 0)
2919 vvfat_close_current_file(s
);
2921 ret
= handle_renames_and_mkdirs(s
);
2923 fprintf(stderr
, "Error handling renames (%d)\n", ret
);
2928 /* copy FAT (with bdrv_read) */
2929 memcpy(s
->fat
.pointer
, s
->fat2
, 0x200 * s
->sectors_per_fat
);
2931 /* recurse direntries from root (using bs->bdrv_read) */
2932 ret
= commit_direntries(s
, 0, -1);
2934 fprintf(stderr
, "Fatal: error while committing (%d)\n", ret
);
2939 ret
= handle_commits(s
);
2941 fprintf(stderr
, "Error handling commits (%d)\n", ret
);
2946 ret
= handle_deletes(s
);
2948 fprintf(stderr
, "Error deleting\n");
2953 if (s
->qcow
->bs
->drv
&& s
->qcow
->bs
->drv
->bdrv_make_empty
) {
2954 s
->qcow
->bs
->drv
->bdrv_make_empty(s
->qcow
->bs
);
2957 memset(s
->used_clusters
, 0, sector2cluster(s
, s
->sector_count
));
2963 static int try_commit(BDRVVVFATState
* s
)
2965 vvfat_close_current_file(s
);
2967 if(!is_consistent(s
))
2969 return do_commit(s
);
2972 static int vvfat_write(BlockDriverState
*bs
, int64_t sector_num
,
2973 const uint8_t *buf
, int nb_sectors
)
2975 BDRVVVFATState
*s
= bs
->opaque
;
2980 /* Check if we're operating in read-only mode */
2981 if (s
->qcow
== NULL
) {
2985 vvfat_close_current_file(s
);
2988 * Some sanity checks:
2989 * - do not allow writing to the boot sector
2992 if (sector_num
< s
->offset_to_fat
)
2995 for (i
= sector2cluster(s
, sector_num
);
2996 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1);) {
2997 mapping_t
* mapping
= find_mapping_for_cluster(s
, i
);
2999 if (mapping
->read_only
) {
3000 fprintf(stderr
, "Tried to write to write-protected file %s\n",
3005 if (mapping
->mode
& MODE_DIRECTORY
) {
3006 int begin
= cluster2sector(s
, i
);
3007 int end
= begin
+ s
->sectors_per_cluster
, k
;
3009 const direntry_t
* direntries
;
3014 if (begin
< sector_num
)
3016 if (end
> sector_num
+ nb_sectors
)
3017 end
= sector_num
+ nb_sectors
;
3018 dir_index
= mapping
->dir_index
+
3019 0x10 * (begin
- mapping
->begin
* s
->sectors_per_cluster
);
3020 direntries
= (direntry_t
*)(buf
+ 0x200 * (begin
- sector_num
));
3022 for (k
= 0; k
< (end
- begin
) * 0x10; k
++) {
3023 /* no access to the direntry of a read-only file */
3024 if (is_short_name(direntries
+ k
) &&
3025 (direntries
[k
].attributes
& 1)) {
3026 if (memcmp(direntries
+ k
,
3027 array_get(&(s
->directory
), dir_index
+ k
),
3028 sizeof(direntry_t
))) {
3029 warn_report("tried to write to write-protected "
3042 * Use qcow backend. Commit later.
3044 DLOG(fprintf(stderr
, "Write to qcow backend: %d + %d\n", (int)sector_num
, nb_sectors
));
3045 ret
= bdrv_write(s
->qcow
, sector_num
, buf
, nb_sectors
);
3047 fprintf(stderr
, "Error writing to qcow backend\n");
3051 for (i
= sector2cluster(s
, sector_num
);
3052 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1); i
++)
3054 s
->used_clusters
[i
] |= USED_ALLOCATED
;
3057 /* TODO: add timeout */
3064 static int coroutine_fn
3065 vvfat_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
3066 QEMUIOVector
*qiov
, int flags
)
3069 BDRVVVFATState
*s
= bs
->opaque
;
3070 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
3071 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
3074 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
3075 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
3077 buf
= g_try_malloc(bytes
);
3078 if (bytes
&& buf
== NULL
) {
3081 qemu_iovec_to_buf(qiov
, 0, buf
, bytes
);
3083 qemu_co_mutex_lock(&s
->lock
);
3084 ret
= vvfat_write(bs
, sector_num
, buf
, nb_sectors
);
3085 qemu_co_mutex_unlock(&s
->lock
);
3092 static int coroutine_fn
vvfat_co_block_status(BlockDriverState
*bs
,
3093 bool want_zero
, int64_t offset
,
3094 int64_t bytes
, int64_t *n
,
3096 BlockDriverState
**file
)
3099 return BDRV_BLOCK_DATA
;
3102 static int coroutine_fn
3103 write_target_commit(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
3104 QEMUIOVector
*qiov
, int flags
)
3108 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
3109 qemu_co_mutex_lock(&s
->lock
);
3110 ret
= try_commit(s
);
3111 qemu_co_mutex_unlock(&s
->lock
);
3116 static void write_target_close(BlockDriverState
*bs
) {
3117 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
3118 bdrv_unref_child(s
->bs
, s
->qcow
);
3119 g_free(s
->qcow_filename
);
3122 static BlockDriver vvfat_write_target
= {
3123 .format_name
= "vvfat_write_target",
3124 .instance_size
= sizeof(void*),
3125 .bdrv_co_pwritev
= write_target_commit
,
3126 .bdrv_close
= write_target_close
,
3129 static void vvfat_qcow_options(int *child_flags
, QDict
*child_options
,
3130 int parent_flags
, QDict
*parent_options
)
3132 qdict_set_default_str(child_options
, BDRV_OPT_READ_ONLY
, "off");
3133 qdict_set_default_str(child_options
, BDRV_OPT_CACHE_NO_FLUSH
, "on");
3136 static const BdrvChildRole child_vvfat_qcow
= {
3137 .parent_is_bds
= true,
3138 .inherit_options
= vvfat_qcow_options
,
3141 static int enable_write_target(BlockDriverState
*bs
, Error
**errp
)
3143 BDRVVVFATState
*s
= bs
->opaque
;
3144 BlockDriver
*bdrv_qcow
= NULL
;
3145 BlockDriverState
*backing
;
3146 QemuOpts
*opts
= NULL
;
3148 int size
= sector2cluster(s
, s
->sector_count
);
3151 s
->used_clusters
= calloc(size
, 1);
3153 array_init(&(s
->commits
), sizeof(commit_t
));
3155 s
->qcow_filename
= g_malloc(PATH_MAX
);
3156 ret
= get_tmp_filename(s
->qcow_filename
, PATH_MAX
);
3158 error_setg_errno(errp
, -ret
, "can't create temporary file");
3162 bdrv_qcow
= bdrv_find_format("qcow");
3164 error_setg(errp
, "Failed to locate qcow driver");
3169 opts
= qemu_opts_create(bdrv_qcow
->create_opts
, NULL
, 0, &error_abort
);
3170 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
->sector_count
* 512,
3172 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, "fat:", &error_abort
);
3174 ret
= bdrv_create(bdrv_qcow
, s
->qcow_filename
, opts
, errp
);
3175 qemu_opts_del(opts
);
3180 options
= qdict_new();
3181 qdict_put_str(options
, "write-target.driver", "qcow");
3182 s
->qcow
= bdrv_open_child(s
->qcow_filename
, options
, "write-target", bs
,
3183 &child_vvfat_qcow
, false, errp
);
3184 qobject_unref(options
);
3191 unlink(s
->qcow_filename
);
3194 backing
= bdrv_new_open_driver(&vvfat_write_target
, NULL
, BDRV_O_ALLOW_RDWR
,
3196 *(void**) backing
->opaque
= s
;
3198 bdrv_set_backing_hd(s
->bs
, backing
, &error_abort
);
3199 bdrv_unref(backing
);
3204 g_free(s
->qcow_filename
);
3205 s
->qcow_filename
= NULL
;
3209 static void vvfat_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
3210 const BdrvChildRole
*role
,
3211 BlockReopenQueue
*reopen_queue
,
3212 uint64_t perm
, uint64_t shared
,
3213 uint64_t *nperm
, uint64_t *nshared
)
3215 BDRVVVFATState
*s
= bs
->opaque
;
3217 assert(c
== s
->qcow
|| role
== &child_backing
);
3220 /* This is a private node, nobody should try to attach to it */
3221 *nperm
= BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
;
3222 *nshared
= BLK_PERM_WRITE_UNCHANGED
;
3224 /* The backing file is there so 'commit' can use it. vvfat doesn't
3225 * access it in any way. */
3227 *nshared
= BLK_PERM_ALL
;
3231 static void vvfat_close(BlockDriverState
*bs
)
3233 BDRVVVFATState
*s
= bs
->opaque
;
3235 vvfat_close_current_file(s
);
3236 array_free(&(s
->fat
));
3237 array_free(&(s
->directory
));
3238 array_free(&(s
->mapping
));
3239 g_free(s
->cluster_buffer
);
3242 migrate_del_blocker(s
->migration_blocker
);
3243 error_free(s
->migration_blocker
);
3247 static BlockDriver bdrv_vvfat
= {
3248 .format_name
= "vvfat",
3249 .protocol_name
= "fat",
3250 .instance_size
= sizeof(BDRVVVFATState
),
3252 .bdrv_parse_filename
= vvfat_parse_filename
,
3253 .bdrv_file_open
= vvfat_open
,
3254 .bdrv_refresh_limits
= vvfat_refresh_limits
,
3255 .bdrv_close
= vvfat_close
,
3256 .bdrv_child_perm
= vvfat_child_perm
,
3258 .bdrv_co_preadv
= vvfat_co_preadv
,
3259 .bdrv_co_pwritev
= vvfat_co_pwritev
,
3260 .bdrv_co_block_status
= vvfat_co_block_status
,
3263 static void bdrv_vvfat_init(void)
3265 bdrv_register(&bdrv_vvfat
);
3268 block_init(bdrv_vvfat_init
);
3271 static void checkpoint(void)
3273 assert(((mapping_t
*)array_get(&(vvv
->mapping
), 0))->end
== 2);
3276 assert(!vvv
->current_mapping
|| vvv
->current_fd
|| (vvv
->current_mapping
->mode
& MODE_DIRECTORY
));