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 "qemu-common.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
30 #include "migration/migration.h"
31 #include "qapi/qmp/qint.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qstring.h"
42 /* TODO: add ":bootsector=blabla.img:" */
43 /* LATER TODO: add automatic boot sector generation from
44 BOOTEASY.ASM and Ranish Partition Manager
45 Note that DOS assumes the system files to be the first files in the
46 file system (test if the boot sector still relies on that fact)! */
47 /* MAYBE TODO: write block-visofs.c */
48 /* TODO: call try_commit() only after a timeout */
56 static void checkpoint(void);
59 void nonono(const char* file
, int line
, const char* msg
) {
60 fprintf(stderr
, "Nonono! %s:%d %s\n", file
, line
, msg
);
64 #define assert(a) do {if (!(a)) nonono(__FILE__, __LINE__, #a);}while(0)
73 /* dynamic array functions */
74 typedef struct array_t
{
76 unsigned int size
,next
,item_size
;
79 static inline void array_init(array_t
* array
,unsigned int item_size
)
81 array
->pointer
= NULL
;
84 array
->item_size
=item_size
;
87 static inline void array_free(array_t
* array
)
89 g_free(array
->pointer
);
90 array
->size
=array
->next
=0;
93 /* does not automatically grow */
94 static inline void* array_get(array_t
* array
,unsigned int index
) {
95 assert(index
< array
->next
);
96 return array
->pointer
+ index
* array
->item_size
;
99 static inline int array_ensure_allocated(array_t
* array
, int index
)
101 if((index
+ 1) * array
->item_size
> array
->size
) {
102 int new_size
= (index
+ 32) * array
->item_size
;
103 array
->pointer
= g_realloc(array
->pointer
, new_size
);
106 array
->size
= new_size
;
107 array
->next
= index
+ 1;
113 static inline void* array_get_next(array_t
* array
) {
114 unsigned int next
= array
->next
;
117 if (array_ensure_allocated(array
, next
) < 0)
120 array
->next
= next
+ 1;
121 result
= array_get(array
, next
);
126 static inline void* array_insert(array_t
* array
,unsigned int index
,unsigned int count
) {
127 if((array
->next
+count
)*array
->item_size
>array
->size
) {
128 int increment
=count
*array
->item_size
;
129 array
->pointer
=g_realloc(array
->pointer
,array
->size
+increment
);
132 array
->size
+=increment
;
134 memmove(array
->pointer
+(index
+count
)*array
->item_size
,
135 array
->pointer
+index
*array
->item_size
,
136 (array
->next
-index
)*array
->item_size
);
138 return array
->pointer
+index
*array
->item_size
;
141 /* this performs a "roll", so that the element which was at index_from becomes
142 * index_to, but the order of all other elements is preserved. */
143 static inline int array_roll(array_t
* array
,int index_to
,int index_from
,int count
)
151 index_to
<0 || index_to
>=array
->next
||
152 index_from
<0 || index_from
>=array
->next
)
155 if(index_to
==index_from
)
159 from
=array
->pointer
+index_from
*is
;
160 to
=array
->pointer
+index_to
*is
;
161 buf
=g_malloc(is
*count
);
162 memcpy(buf
,from
,is
*count
);
164 if(index_to
<index_from
)
165 memmove(to
+is
*count
,to
,from
-to
);
167 memmove(from
,from
+is
*count
,to
-from
);
169 memcpy(to
,buf
,is
*count
);
176 static inline int array_remove_slice(array_t
* array
,int index
, int count
)
180 assert(index
+ count
<= array
->next
);
181 if(array_roll(array
,array
->next
-1,index
,count
))
183 array
->next
-= count
;
187 static int array_remove(array_t
* array
,int index
)
189 return array_remove_slice(array
, index
, 1);
192 /* return the index for a given member */
193 static int array_index(array_t
* array
, void* pointer
)
195 size_t offset
= (char*)pointer
- array
->pointer
;
196 assert((offset
% array
->item_size
) == 0);
197 assert(offset
/array
->item_size
< array
->next
);
198 return offset
/array
->item_size
;
201 /* These structures are used to fake a disk and the VFAT filesystem.
202 * For this reason we need to use QEMU_PACKED. */
204 typedef struct bootsector_t
{
207 uint16_t sector_size
;
208 uint8_t sectors_per_cluster
;
209 uint16_t reserved_sectors
;
210 uint8_t number_of_fats
;
211 uint16_t root_entries
;
212 uint16_t total_sectors16
;
214 uint16_t sectors_per_fat
;
215 uint16_t sectors_per_track
;
216 uint16_t number_of_heads
;
217 uint32_t hidden_sectors
;
218 uint32_t total_sectors
;
221 uint8_t drive_number
;
222 uint8_t current_head
;
225 uint8_t volume_label
[11];
228 uint32_t sectors_per_fat
;
231 uint32_t first_cluster_of_root_directory
;
232 uint16_t info_sector
;
233 uint16_t backup_boot_sector
;
238 uint8_t ignored
[0x1c0];
240 } QEMU_PACKED bootsector_t
;
248 typedef struct partition_t
{
249 uint8_t attributes
; /* 0x80 = bootable */
251 uint8_t fs_type
; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
253 uint32_t start_sector_long
;
254 uint32_t length_sector_long
;
255 } QEMU_PACKED partition_t
;
257 typedef struct mbr_t
{
258 uint8_t ignored
[0x1b8];
261 partition_t partition
[4];
265 typedef struct direntry_t
{
277 } QEMU_PACKED direntry_t
;
279 /* this structure are used to transparently access the files */
281 typedef struct mapping_t
{
282 /* begin is the first cluster, end is the last+1 */
284 /* as s->directory is growable, no pointer may be used here */
285 unsigned int dir_index
;
286 /* the clusters of a file may be in any order; this points to the first */
287 int first_mapping_index
;
290 * - the offset in the file (in clusters) for a file, or
291 * - the next cluster of the directory for a directory, and
292 * - the address of the buffer for a faked entry
298 int parent_mapping_index
;
302 /* path contains the full path, i.e. it always starts with s->path */
305 enum { MODE_UNDEFINED
= 0, MODE_NORMAL
= 1, MODE_MODIFIED
= 2,
306 MODE_DIRECTORY
= 4, MODE_FAKED
= 8,
307 MODE_DELETED
= 16, MODE_RENAMED
= 32 } mode
;
312 static void print_direntry(const struct direntry_t
*);
313 static void print_mapping(const struct mapping_t
* mapping
);
316 /* here begins the real VVFAT driver */
318 typedef struct BDRVVVFATState
{
320 BlockDriverState
* bs
; /* pointer to parent */
321 unsigned int first_sectors_number
; /* 1 for a single partition, 0x40 for a disk with partition table */
322 unsigned char first_sectors
[0x40*0x200];
324 int fat_type
; /* 16 or 32 */
325 array_t fat
,directory
,mapping
;
326 char volume_label
[11];
328 unsigned int cluster_size
;
329 unsigned int sectors_per_cluster
;
330 unsigned int sectors_per_fat
;
331 unsigned int sectors_of_root_directory
;
332 uint32_t last_cluster_of_root_directory
;
333 unsigned int faked_sectors
; /* how many sectors are faked before file data */
334 uint32_t sector_count
; /* total number of sectors of the partition */
335 uint32_t cluster_count
; /* total number of clusters of this partition */
336 uint32_t max_fat_value
;
339 mapping_t
* current_mapping
;
340 unsigned char* cluster
; /* points to current cluster */
341 unsigned char* cluster_buffer
; /* points to a buffer to hold temp data */
342 unsigned int current_cluster
;
345 BlockDriverState
* write_target
;
347 BlockDriverState
* qcow
;
352 int downcase_short_names
;
354 Error
*migration_blocker
;
357 /* take the sector position spos and convert it to Cylinder/Head/Sector position
358 * if the position is outside the specified geometry, fill maximum value for CHS
359 * and return 1 to signal overflow.
361 static int sector2CHS(mbr_chs_t
*chs
, int spos
, int cyls
, int heads
, int secs
)
364 sector
= spos
% secs
; spos
/= secs
;
365 head
= spos
% heads
; spos
/= heads
;
368 it happens if 32bit sector positions are used, while CHS is only 24bit.
369 Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
372 chs
->cylinder
= 0xFF;
375 chs
->head
= (uint8_t)head
;
376 chs
->sector
= (uint8_t)( (sector
+1) | ((spos
>>8)<<6) );
377 chs
->cylinder
= (uint8_t)spos
;
381 static void init_mbr(BDRVVVFATState
*s
, int cyls
, int heads
, int secs
)
383 /* TODO: if the files mbr.img and bootsect.img exist, use them */
384 mbr_t
* real_mbr
=(mbr_t
*)s
->first_sectors
;
385 partition_t
* partition
= &(real_mbr
->partition
[0]);
388 memset(s
->first_sectors
,0,512);
390 /* Win NT Disk Signature */
391 real_mbr
->nt_id
= cpu_to_le32(0xbe1afdfa);
393 partition
->attributes
=0x80; /* bootable */
395 /* LBA is used when partition is outside the CHS geometry */
396 lba
= sector2CHS(&partition
->start_CHS
, s
->first_sectors_number
- 1,
398 lba
|= sector2CHS(&partition
->end_CHS
, s
->bs
->total_sectors
- 1,
401 /*LBA partitions are identified only by start/length_sector_long not by CHS*/
402 partition
->start_sector_long
= cpu_to_le32(s
->first_sectors_number
- 1);
403 partition
->length_sector_long
= cpu_to_le32(s
->bs
->total_sectors
404 - s
->first_sectors_number
+ 1);
406 /* FAT12/FAT16/FAT32 */
407 /* DOS uses different types when partition is LBA,
408 probably to prevent older versions from using CHS on them */
409 partition
->fs_type
= s
->fat_type
==12 ? 0x1:
410 s
->fat_type
==16 ? (lba
?0xe:0x06):
411 /*fat_tyoe==32*/ (lba
?0xc:0x0b);
413 real_mbr
->magic
[0]=0x55; real_mbr
->magic
[1]=0xaa;
416 /* direntry functions */
418 /* dest is assumed to hold 258 bytes, and pads with 0xffff up to next multiple of 26 */
419 static inline int short2long_name(char* dest
,const char* src
)
423 for(i
=0;i
<129 && src
[i
];i
++) {
428 dest
[2*i
]=dest
[2*i
+1]=0;
429 for(i
=2*i
+2;(i
%26);i
++)
434 static inline direntry_t
* create_long_filename(BDRVVVFATState
* s
,const char* filename
)
437 int length
=short2long_name(buffer
,filename
),
438 number_of_entries
=(length
+25)/26,i
;
441 for(i
=0;i
<number_of_entries
;i
++) {
442 entry
=array_get_next(&(s
->directory
));
443 entry
->attributes
=0xf;
444 entry
->reserved
[0]=0;
446 entry
->name
[0]=(number_of_entries
-i
)|(i
==0?0x40:0);
448 for(i
=0;i
<26*number_of_entries
;i
++) {
450 if(offset
<10) offset
=1+offset
;
451 else if(offset
<22) offset
=14+offset
-10;
452 else offset
=28+offset
-22;
453 entry
=array_get(&(s
->directory
),s
->directory
.next
-1-(i
/26));
454 entry
->name
[offset
]=buffer
[i
];
456 return array_get(&(s
->directory
),s
->directory
.next
-number_of_entries
);
459 static char is_free(const direntry_t
* direntry
)
461 return direntry
->name
[0]==0xe5 || direntry
->name
[0]==0x00;
464 static char is_volume_label(const direntry_t
* direntry
)
466 return direntry
->attributes
== 0x28;
469 static char is_long_name(const direntry_t
* direntry
)
471 return direntry
->attributes
== 0xf;
474 static char is_short_name(const direntry_t
* direntry
)
476 return !is_volume_label(direntry
) && !is_long_name(direntry
)
477 && !is_free(direntry
);
480 static char is_directory(const direntry_t
* direntry
)
482 return direntry
->attributes
& 0x10 && direntry
->name
[0] != 0xe5;
485 static inline char is_dot(const direntry_t
* direntry
)
487 return is_short_name(direntry
) && direntry
->name
[0] == '.';
490 static char is_file(const direntry_t
* direntry
)
492 return is_short_name(direntry
) && !is_directory(direntry
);
495 static inline uint32_t begin_of_direntry(const direntry_t
* direntry
)
497 return le16_to_cpu(direntry
->begin
)|(le16_to_cpu(direntry
->begin_hi
)<<16);
500 static inline uint32_t filesize_of_direntry(const direntry_t
* direntry
)
502 return le32_to_cpu(direntry
->size
);
505 static void set_begin_of_direntry(direntry_t
* direntry
, uint32_t begin
)
507 direntry
->begin
= cpu_to_le16(begin
& 0xffff);
508 direntry
->begin_hi
= cpu_to_le16((begin
>> 16) & 0xffff);
513 static inline uint8_t fat_chksum(const direntry_t
* entry
)
518 for (i
= 0; i
< ARRAY_SIZE(entry
->name
); i
++) {
519 chksum
= (((chksum
& 0xfe) >> 1) |
520 ((chksum
& 0x01) ? 0x80 : 0)) + entry
->name
[i
];
526 /* if return_time==0, this returns the fat_date, else the fat_time */
527 static uint16_t fat_datetime(time_t time
,int return_time
) {
531 localtime_r(&time
,t
);
533 return cpu_to_le16((t
->tm_sec
/2)|(t
->tm_min
<<5)|(t
->tm_hour
<<11));
534 return cpu_to_le16((t
->tm_mday
)|((t
->tm_mon
+1)<<5)|((t
->tm_year
-80)<<9));
537 static inline void fat_set(BDRVVVFATState
* s
,unsigned int cluster
,uint32_t value
)
539 if(s
->fat_type
==32) {
540 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
541 *entry
=cpu_to_le32(value
);
542 } else if(s
->fat_type
==16) {
543 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
544 *entry
=cpu_to_le16(value
&0xffff);
546 int offset
= (cluster
*3/2);
547 unsigned char* p
= array_get(&(s
->fat
), offset
);
551 p
[1] = (p
[1]&0xf0) | ((value
>>8)&0xf);
554 p
[0] = (p
[0]&0xf) | ((value
&0xf)<<4);
561 static inline uint32_t fat_get(BDRVVVFATState
* s
,unsigned int cluster
)
563 if(s
->fat_type
==32) {
564 uint32_t* entry
=array_get(&(s
->fat
),cluster
);
565 return le32_to_cpu(*entry
);
566 } else if(s
->fat_type
==16) {
567 uint16_t* entry
=array_get(&(s
->fat
),cluster
);
568 return le16_to_cpu(*entry
);
570 const uint8_t* x
=(uint8_t*)(s
->fat
.pointer
)+cluster
*3/2;
571 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
575 static inline int fat_eof(BDRVVVFATState
* s
,uint32_t fat_entry
)
577 if(fat_entry
>s
->max_fat_value
-8)
582 static inline void init_fat(BDRVVVFATState
* s
)
584 if (s
->fat_type
== 12) {
585 array_init(&(s
->fat
),1);
586 array_ensure_allocated(&(s
->fat
),
587 s
->sectors_per_fat
* 0x200 * 3 / 2 - 1);
589 array_init(&(s
->fat
),(s
->fat_type
==32?4:2));
590 array_ensure_allocated(&(s
->fat
),
591 s
->sectors_per_fat
* 0x200 / s
->fat
.item_size
- 1);
593 memset(s
->fat
.pointer
,0,s
->fat
.size
);
595 switch(s
->fat_type
) {
596 case 12: s
->max_fat_value
=0xfff; break;
597 case 16: s
->max_fat_value
=0xffff; break;
598 case 32: s
->max_fat_value
=0x0fffffff; break;
599 default: s
->max_fat_value
=0; /* error... */
604 /* TODO: in create_short_filename, 0xe5->0x05 is not yet handled! */
605 /* TODO: in parse_short_filename, 0x05->0xe5 is not yet handled! */
606 static inline direntry_t
* create_short_and_long_name(BDRVVVFATState
* s
,
607 unsigned int directory_start
, const char* filename
, int is_dot
)
609 int i
,j
,long_index
=s
->directory
.next
;
610 direntry_t
* entry
= NULL
;
611 direntry_t
* entry_long
= NULL
;
614 entry
=array_get_next(&(s
->directory
));
615 memset(entry
->name
, 0x20, sizeof(entry
->name
));
616 memcpy(entry
->name
,filename
,strlen(filename
));
620 entry_long
=create_long_filename(s
,filename
);
622 i
= strlen(filename
);
623 for(j
= i
- 1; j
>0 && filename
[j
]!='.';j
--);
629 entry
=array_get_next(&(s
->directory
));
630 memset(entry
->name
, 0x20, sizeof(entry
->name
));
631 memcpy(entry
->name
, filename
, i
);
634 for (i
= 0; i
< 3 && filename
[j
+ 1 + i
]; i
++) {
635 entry
->name
[8 + i
] = filename
[j
+ 1 + i
];
639 /* upcase & remove unwanted characters */
641 if(i
==10 || i
==7) for(;i
>0 && entry
->name
[i
]==' ';i
--);
642 if(entry
->name
[i
]<=' ' || entry
->name
[i
]>0x7f
643 || strchr(".*?<>|\":/\\[];,+='",entry
->name
[i
]))
645 else if(entry
->name
[i
]>='a' && entry
->name
[i
]<='z')
646 entry
->name
[i
]+='A'-'a';
649 /* mangle duplicates */
651 direntry_t
* entry1
=array_get(&(s
->directory
),directory_start
);
654 for(;entry1
<entry
;entry1
++)
655 if(!is_long_name(entry1
) && !memcmp(entry1
->name
,entry
->name
,11))
656 break; /* found dupe */
657 if(entry1
==entry
) /* no dupe found */
660 /* use all 8 characters of name */
661 if(entry
->name
[7]==' ') {
663 for(j
=6;j
>0 && entry
->name
[j
]==' ';j
--)
667 /* increment number */
668 for(j
=7;j
>0 && entry
->name
[j
]=='9';j
--)
671 if(entry
->name
[j
]<'0' || entry
->name
[j
]>'9')
678 /* calculate checksum; propagate to long name */
680 uint8_t chksum
=fat_chksum(entry
);
682 /* calculate anew, because realloc could have taken place */
683 entry_long
=array_get(&(s
->directory
),long_index
);
684 while(entry_long
<entry
&& is_long_name(entry_long
)) {
685 entry_long
->reserved
[1]=chksum
;
694 * Read a directory. (the index of the corresponding mapping must be passed).
696 static int read_directory(BDRVVVFATState
* s
, int mapping_index
)
698 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
699 direntry_t
* direntry
;
700 const char* dirname
= mapping
->path
;
701 int first_cluster
= mapping
->begin
;
702 int parent_index
= mapping
->info
.dir
.parent_mapping_index
;
703 mapping_t
* parent_mapping
= (mapping_t
*)
704 (parent_index
>= 0 ? array_get(&(s
->mapping
), parent_index
) : NULL
);
705 int first_cluster_of_parent
= parent_mapping
? parent_mapping
->begin
: -1;
707 DIR* dir
=opendir(dirname
);
708 struct dirent
* entry
;
711 assert(mapping
->mode
& MODE_DIRECTORY
);
714 mapping
->end
= mapping
->begin
;
718 i
= mapping
->info
.dir
.first_dir_index
=
719 first_cluster
== 0 ? 0 : s
->directory
.next
;
721 /* actually read the directory, and allocate the mappings */
722 while((entry
=readdir(dir
))) {
723 unsigned int length
=strlen(dirname
)+2+strlen(entry
->d_name
);
725 direntry_t
* direntry
;
727 int is_dot
=!strcmp(entry
->d_name
,".");
728 int is_dotdot
=!strcmp(entry
->d_name
,"..");
730 if(first_cluster
== 0 && (is_dotdot
|| is_dot
))
733 buffer
= g_malloc(length
);
734 snprintf(buffer
,length
,"%s/%s",dirname
,entry
->d_name
);
736 if(stat(buffer
,&st
)<0) {
741 /* create directory entry for this file */
742 direntry
=create_short_and_long_name(s
, i
, entry
->d_name
,
743 is_dot
|| is_dotdot
);
744 direntry
->attributes
=(S_ISDIR(st
.st_mode
)?0x10:0x20);
745 direntry
->reserved
[0]=direntry
->reserved
[1]=0;
746 direntry
->ctime
=fat_datetime(st
.st_ctime
,1);
747 direntry
->cdate
=fat_datetime(st
.st_ctime
,0);
748 direntry
->adate
=fat_datetime(st
.st_atime
,0);
749 direntry
->begin_hi
=0;
750 direntry
->mtime
=fat_datetime(st
.st_mtime
,1);
751 direntry
->mdate
=fat_datetime(st
.st_mtime
,0);
753 set_begin_of_direntry(direntry
, first_cluster_of_parent
);
755 set_begin_of_direntry(direntry
, first_cluster
);
757 direntry
->begin
=0; /* do that later */
758 if (st
.st_size
> 0x7fffffff) {
759 fprintf(stderr
, "File %s is larger than 2GB\n", buffer
);
764 direntry
->size
=cpu_to_le32(S_ISDIR(st
.st_mode
)?0:st
.st_size
);
766 /* create mapping for this file */
767 if(!is_dot
&& !is_dotdot
&& (S_ISDIR(st
.st_mode
) || st
.st_size
)) {
768 s
->current_mapping
= array_get_next(&(s
->mapping
));
769 s
->current_mapping
->begin
=0;
770 s
->current_mapping
->end
=st
.st_size
;
772 * we get the direntry of the most recent direntry, which
773 * contains the short name and all the relevant information.
775 s
->current_mapping
->dir_index
=s
->directory
.next
-1;
776 s
->current_mapping
->first_mapping_index
= -1;
777 if (S_ISDIR(st
.st_mode
)) {
778 s
->current_mapping
->mode
= MODE_DIRECTORY
;
779 s
->current_mapping
->info
.dir
.parent_mapping_index
=
782 s
->current_mapping
->mode
= MODE_UNDEFINED
;
783 s
->current_mapping
->info
.file
.offset
= 0;
785 s
->current_mapping
->path
=buffer
;
786 s
->current_mapping
->read_only
=
787 (st
.st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) == 0;
794 /* fill with zeroes up to the end of the cluster */
795 while(s
->directory
.next
%(0x10*s
->sectors_per_cluster
)) {
796 direntry_t
* direntry
=array_get_next(&(s
->directory
));
797 memset(direntry
,0,sizeof(direntry_t
));
800 /* TODO: if there are more entries, bootsector has to be adjusted! */
801 #define ROOT_ENTRIES (0x02 * 0x10 * s->sectors_per_cluster)
802 if (mapping_index
== 0 && s
->directory
.next
< ROOT_ENTRIES
) {
804 int cur
= s
->directory
.next
;
805 array_ensure_allocated(&(s
->directory
), ROOT_ENTRIES
- 1);
806 s
->directory
.next
= ROOT_ENTRIES
;
807 memset(array_get(&(s
->directory
), cur
), 0,
808 (ROOT_ENTRIES
- cur
) * sizeof(direntry_t
));
811 /* reget the mapping, since s->mapping was possibly realloc()ed */
812 mapping
= array_get(&(s
->mapping
), mapping_index
);
813 first_cluster
+= (s
->directory
.next
- mapping
->info
.dir
.first_dir_index
)
814 * 0x20 / s
->cluster_size
;
815 mapping
->end
= first_cluster
;
817 direntry
= array_get(&(s
->directory
), mapping
->dir_index
);
818 set_begin_of_direntry(direntry
, mapping
->begin
);
823 static inline uint32_t sector2cluster(BDRVVVFATState
* s
,off_t sector_num
)
825 return (sector_num
-s
->faked_sectors
)/s
->sectors_per_cluster
;
828 static inline off_t
cluster2sector(BDRVVVFATState
* s
, uint32_t cluster_num
)
830 return s
->faked_sectors
+ s
->sectors_per_cluster
* cluster_num
;
833 static int init_directories(BDRVVVFATState
* s
,
834 const char *dirname
, int heads
, int secs
,
837 bootsector_t
* bootsector
;
840 unsigned int cluster
;
842 memset(&(s
->first_sectors
[0]),0,0x40*0x200);
844 s
->cluster_size
=s
->sectors_per_cluster
*0x200;
845 s
->cluster_buffer
=g_malloc(s
->cluster_size
);
848 * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
849 * where sc is sector_count,
850 * spf is sectors_per_fat,
851 * spc is sectors_per_clusters, and
852 * fat_type = 12, 16 or 32.
854 i
= 1+s
->sectors_per_cluster
*0x200*8/s
->fat_type
;
855 s
->sectors_per_fat
=(s
->sector_count
+i
)/i
; /* round up */
857 array_init(&(s
->mapping
),sizeof(mapping_t
));
858 array_init(&(s
->directory
),sizeof(direntry_t
));
860 /* add volume label */
862 direntry_t
* entry
=array_get_next(&(s
->directory
));
863 entry
->attributes
=0x28; /* archive | volume label */
864 memcpy(entry
->name
, s
->volume_label
, sizeof(entry
->name
));
867 /* Now build FAT, and write back information into directory */
870 s
->faked_sectors
=s
->first_sectors_number
+s
->sectors_per_fat
*2;
871 s
->cluster_count
=sector2cluster(s
, s
->sector_count
);
873 mapping
= array_get_next(&(s
->mapping
));
875 mapping
->dir_index
= 0;
876 mapping
->info
.dir
.parent_mapping_index
= -1;
877 mapping
->first_mapping_index
= -1;
878 mapping
->path
= g_strdup(dirname
);
879 i
= strlen(mapping
->path
);
880 if (i
> 0 && mapping
->path
[i
- 1] == '/')
881 mapping
->path
[i
- 1] = '\0';
882 mapping
->mode
= MODE_DIRECTORY
;
883 mapping
->read_only
= 0;
884 s
->path
= mapping
->path
;
886 for (i
= 0, cluster
= 0; i
< s
->mapping
.next
; i
++) {
887 /* MS-DOS expects the FAT to be 0 for the root directory
888 * (except for the media byte). */
889 /* LATER TODO: still true for FAT32? */
890 int fix_fat
= (i
!= 0);
891 mapping
= array_get(&(s
->mapping
), i
);
893 if (mapping
->mode
& MODE_DIRECTORY
) {
894 mapping
->begin
= cluster
;
895 if(read_directory(s
, i
)) {
896 error_setg(errp
, "Could not read directory %s",
900 mapping
= array_get(&(s
->mapping
), i
);
902 assert(mapping
->mode
== MODE_UNDEFINED
);
903 mapping
->mode
=MODE_NORMAL
;
904 mapping
->begin
= cluster
;
905 if (mapping
->end
> 0) {
906 direntry_t
* direntry
= array_get(&(s
->directory
),
909 mapping
->end
= cluster
+ 1 + (mapping
->end
-1)/s
->cluster_size
;
910 set_begin_of_direntry(direntry
, mapping
->begin
);
912 mapping
->end
= cluster
+ 1;
917 assert(mapping
->begin
< mapping
->end
);
919 /* next free cluster */
920 cluster
= mapping
->end
;
922 if(cluster
> s
->cluster_count
) {
924 "Directory does not fit in FAT%d (capacity %.2f MB)",
925 s
->fat_type
, s
->sector_count
/ 2000.0);
929 /* fix fat for entry */
932 for(j
= mapping
->begin
; j
< mapping
->end
- 1; j
++)
934 fat_set(s
, mapping
->end
- 1, s
->max_fat_value
);
938 mapping
= array_get(&(s
->mapping
), 0);
939 s
->sectors_of_root_directory
= mapping
->end
* s
->sectors_per_cluster
;
940 s
->last_cluster_of_root_directory
= mapping
->end
;
942 /* the FAT signature */
943 fat_set(s
,0,s
->max_fat_value
);
944 fat_set(s
,1,s
->max_fat_value
);
946 s
->current_mapping
= NULL
;
948 bootsector
=(bootsector_t
*)(s
->first_sectors
+(s
->first_sectors_number
-1)*0x200);
949 bootsector
->jump
[0]=0xeb;
950 bootsector
->jump
[1]=0x3e;
951 bootsector
->jump
[2]=0x90;
952 memcpy(bootsector
->name
,"QEMU ",8);
953 bootsector
->sector_size
=cpu_to_le16(0x200);
954 bootsector
->sectors_per_cluster
=s
->sectors_per_cluster
;
955 bootsector
->reserved_sectors
=cpu_to_le16(1);
956 bootsector
->number_of_fats
=0x2; /* number of FATs */
957 bootsector
->root_entries
=cpu_to_le16(s
->sectors_of_root_directory
*0x10);
958 bootsector
->total_sectors16
=s
->sector_count
>0xffff?0:cpu_to_le16(s
->sector_count
);
959 bootsector
->media_type
=(s
->first_sectors_number
>1?0xf8:0xf0); /* media descriptor (f8=hd, f0=3.5 fd)*/
960 s
->fat
.pointer
[0] = bootsector
->media_type
;
961 bootsector
->sectors_per_fat
=cpu_to_le16(s
->sectors_per_fat
);
962 bootsector
->sectors_per_track
= cpu_to_le16(secs
);
963 bootsector
->number_of_heads
= cpu_to_le16(heads
);
964 bootsector
->hidden_sectors
=cpu_to_le32(s
->first_sectors_number
==1?0:0x3f);
965 bootsector
->total_sectors
=cpu_to_le32(s
->sector_count
>0xffff?s
->sector_count
:0);
967 /* LATER TODO: if FAT32, this is wrong */
968 bootsector
->u
.fat16
.drive_number
=s
->first_sectors_number
==1?0:0x80; /* fda=0, hda=0x80 */
969 bootsector
->u
.fat16
.current_head
=0;
970 bootsector
->u
.fat16
.signature
=0x29;
971 bootsector
->u
.fat16
.id
=cpu_to_le32(0xfabe1afd);
973 memcpy(bootsector
->u
.fat16
.volume_label
, s
->volume_label
,
974 sizeof(bootsector
->u
.fat16
.volume_label
));
975 memcpy(bootsector
->fat_type
,(s
->fat_type
==12?"FAT12 ":s
->fat_type
==16?"FAT16 ":"FAT32 "),8);
976 bootsector
->magic
[0]=0x55; bootsector
->magic
[1]=0xaa;
982 static BDRVVVFATState
*vvv
= NULL
;
985 static int enable_write_target(BDRVVVFATState
*s
, Error
**errp
);
986 static int is_consistent(BDRVVVFATState
*s
);
988 static QemuOptsList runtime_opts
= {
990 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
994 .type
= QEMU_OPT_STRING
,
995 .help
= "Host directory to map to the vvfat device",
999 .type
= QEMU_OPT_NUMBER
,
1000 .help
= "FAT type (12, 16 or 32)",
1004 .type
= QEMU_OPT_BOOL
,
1005 .help
= "Create a floppy rather than a hard disk image",
1009 .type
= QEMU_OPT_STRING
,
1010 .help
= "Use a volume label other than QEMU VVFAT",
1014 .type
= QEMU_OPT_BOOL
,
1015 .help
= "Make the image writable",
1017 { /* end of list */ }
1021 static void vvfat_parse_filename(const char *filename
, QDict
*options
,
1025 bool floppy
= false;
1029 if (!strstart(filename
, "fat:", NULL
)) {
1030 error_setg(errp
, "File name string must start with 'fat:'");
1035 if (strstr(filename
, ":32:")) {
1037 } else if (strstr(filename
, ":16:")) {
1039 } else if (strstr(filename
, ":12:")) {
1043 if (strstr(filename
, ":floppy:")) {
1047 if (strstr(filename
, ":rw:")) {
1051 /* Get the directory name without options */
1052 i
= strrchr(filename
, ':') - filename
;
1054 if (filename
[i
- 2] == ':' && qemu_isalpha(filename
[i
- 1])) {
1055 /* workaround for DOS drive names */
1061 /* Fill in the options QDict */
1062 qdict_put(options
, "dir", qstring_from_str(filename
));
1063 qdict_put(options
, "fat-type", qint_from_int(fat_type
));
1064 qdict_put(options
, "floppy", qbool_from_bool(floppy
));
1065 qdict_put(options
, "rw", qbool_from_bool(rw
));
1068 static int vvfat_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1071 BDRVVVFATState
*s
= bs
->opaque
;
1072 int cyls
, heads
, secs
;
1074 const char *dirname
, *label
;
1076 Error
*local_err
= NULL
;
1083 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1084 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1086 error_propagate(errp
, local_err
);
1091 dirname
= qemu_opt_get(opts
, "dir");
1093 error_setg(errp
, "vvfat block driver requires a 'dir' option");
1098 s
->fat_type
= qemu_opt_get_number(opts
, "fat-type", 0);
1099 floppy
= qemu_opt_get_bool(opts
, "floppy", false);
1101 memset(s
->volume_label
, ' ', sizeof(s
->volume_label
));
1102 label
= qemu_opt_get(opts
, "label");
1104 size_t label_length
= strlen(label
);
1105 if (label_length
> 11) {
1106 error_setg(errp
, "vvfat label cannot be longer than 11 bytes");
1110 memcpy(s
->volume_label
, label
, label_length
);
1114 /* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
1118 s
->sectors_per_cluster
= 2;
1120 secs
= s
->fat_type
== 12 ? 18 : 36;
1121 s
->sectors_per_cluster
= 1;
1123 s
->first_sectors_number
= 1;
1127 /* 32MB or 504MB disk*/
1131 s
->first_sectors_number
= 0x40;
1132 cyls
= s
->fat_type
== 12 ? 64 : 1024;
1137 switch (s
->fat_type
) {
1139 fprintf(stderr
, "Big fat greek warning: FAT32 has not been tested. "
1140 "You are welcome to do so!\n");
1146 error_setg(errp
, "Valid FAT types are only 12, 16 and 32");
1154 /* LATER TODO: if FAT32, adjust */
1155 s
->sectors_per_cluster
=0x10;
1157 s
->current_cluster
=0xffffffff;
1159 /* read only is the default for safety */
1161 s
->qcow
= s
->write_target
= NULL
;
1162 s
->qcow_filename
= NULL
;
1164 s
->downcase_short_names
= 1;
1166 fprintf(stderr
, "vvfat %s chs %d,%d,%d\n",
1167 dirname
, cyls
, heads
, secs
);
1169 s
->sector_count
= cyls
* heads
* secs
- (s
->first_sectors_number
- 1);
1171 if (qemu_opt_get_bool(opts
, "rw", false)) {
1172 ret
= enable_write_target(s
, errp
);
1179 bs
->total_sectors
= cyls
* heads
* secs
;
1181 if (init_directories(s
, dirname
, heads
, secs
, errp
)) {
1186 s
->sector_count
= s
->faked_sectors
+ s
->sectors_per_cluster
*s
->cluster_count
;
1188 if (s
->first_sectors_number
== 0x40) {
1189 init_mbr(s
, cyls
, heads
, secs
);
1192 // assert(is_consistent(s));
1193 qemu_co_mutex_init(&s
->lock
);
1195 /* Disable migration when vvfat is used rw */
1197 error_setg(&s
->migration_blocker
,
1198 "The vvfat (rw) format used by node '%s' "
1199 "does not support live migration",
1200 bdrv_get_device_or_node_name(bs
));
1201 migrate_add_blocker(s
->migration_blocker
);
1206 qemu_opts_del(opts
);
1210 static inline void vvfat_close_current_file(BDRVVVFATState
*s
)
1212 if(s
->current_mapping
) {
1213 s
->current_mapping
= NULL
;
1214 if (s
->current_fd
) {
1215 qemu_close(s
->current_fd
);
1219 s
->current_cluster
= -1;
1222 /* mappings between index1 and index2-1 are supposed to be ordered
1223 * return value is the index of the last mapping for which end>cluster_num
1225 static inline int find_mapping_for_cluster_aux(BDRVVVFATState
* s
,int cluster_num
,int index1
,int index2
)
1230 index3
=(index1
+index2
)/2;
1231 mapping
=array_get(&(s
->mapping
),index3
);
1232 assert(mapping
->begin
< mapping
->end
);
1233 if(mapping
->begin
>=cluster_num
) {
1234 assert(index2
!=index3
|| index2
==0);
1240 return mapping
->end
<=cluster_num
? index2
: index1
;
1243 assert(index1
<=index2
);
1244 DLOG(mapping
=array_get(&(s
->mapping
),index1
);
1245 assert(mapping
->begin
<=cluster_num
);
1246 assert(index2
>= s
->mapping
.next
||
1247 ((mapping
= array_get(&(s
->mapping
),index2
)) &&
1248 mapping
->end
>cluster_num
)));
1252 static inline mapping_t
* find_mapping_for_cluster(BDRVVVFATState
* s
,int cluster_num
)
1254 int index
=find_mapping_for_cluster_aux(s
,cluster_num
,0,s
->mapping
.next
);
1256 if(index
>=s
->mapping
.next
)
1258 mapping
=array_get(&(s
->mapping
),index
);
1259 if(mapping
->begin
>cluster_num
)
1261 assert(mapping
->begin
<=cluster_num
&& mapping
->end
>cluster_num
);
1265 static int open_file(BDRVVVFATState
* s
,mapping_t
* mapping
)
1269 if(!s
->current_mapping
||
1270 strcmp(s
->current_mapping
->path
,mapping
->path
)) {
1272 int fd
= qemu_open(mapping
->path
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1275 vvfat_close_current_file(s
);
1277 s
->current_mapping
= mapping
;
1282 static inline int read_cluster(BDRVVVFATState
*s
,int cluster_num
)
1284 if(s
->current_cluster
!= cluster_num
) {
1287 assert(!s
->current_mapping
|| s
->current_fd
|| (s
->current_mapping
->mode
& MODE_DIRECTORY
));
1288 if(!s
->current_mapping
1289 || s
->current_mapping
->begin
>cluster_num
1290 || s
->current_mapping
->end
<=cluster_num
) {
1291 /* binary search of mappings for file */
1292 mapping_t
* mapping
=find_mapping_for_cluster(s
,cluster_num
);
1294 assert(!mapping
|| (cluster_num
>=mapping
->begin
&& cluster_num
<mapping
->end
));
1296 if (mapping
&& mapping
->mode
& MODE_DIRECTORY
) {
1297 vvfat_close_current_file(s
);
1298 s
->current_mapping
= mapping
;
1299 read_cluster_directory
:
1300 offset
= s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
);
1301 s
->cluster
= (unsigned char*)s
->directory
.pointer
+offset
1302 + 0x20*s
->current_mapping
->info
.dir
.first_dir_index
;
1303 assert(((s
->cluster
-(unsigned char*)s
->directory
.pointer
)%s
->cluster_size
)==0);
1304 assert((char*)s
->cluster
+s
->cluster_size
<= s
->directory
.pointer
+s
->directory
.next
*s
->directory
.item_size
);
1305 s
->current_cluster
= cluster_num
;
1309 if(open_file(s
,mapping
))
1311 } else if (s
->current_mapping
->mode
& MODE_DIRECTORY
)
1312 goto read_cluster_directory
;
1314 assert(s
->current_fd
);
1316 offset
=s
->cluster_size
*(cluster_num
-s
->current_mapping
->begin
)+s
->current_mapping
->info
.file
.offset
;
1317 if(lseek(s
->current_fd
, offset
, SEEK_SET
)!=offset
)
1319 s
->cluster
=s
->cluster_buffer
;
1320 result
=read(s
->current_fd
,s
->cluster
,s
->cluster_size
);
1322 s
->current_cluster
= -1;
1325 s
->current_cluster
= cluster_num
;
1331 static void print_direntry(const direntry_t
* direntry
)
1336 fprintf(stderr
, "direntry %p: ", direntry
);
1339 if(is_long_name(direntry
)) {
1340 unsigned char* c
=(unsigned char*)direntry
;
1342 for(i
=1;i
<11 && c
[i
] && c
[i
]!=0xff;i
+=2)
1343 #define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
1345 for(i
=14;i
<26 && c
[i
] && c
[i
]!=0xff;i
+=2)
1347 for(i
=28;i
<32 && c
[i
] && c
[i
]!=0xff;i
+=2)
1350 fprintf(stderr
, "%s\n", buffer
);
1354 ADD_CHAR(direntry
->name
[i
]);
1356 fprintf(stderr
,"%s attributes=0x%02x begin=%d size=%d\n",
1358 direntry
->attributes
,
1359 begin_of_direntry(direntry
),le32_to_cpu(direntry
->size
));
1363 static void print_mapping(const mapping_t
* mapping
)
1365 fprintf(stderr
, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
1366 "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
1367 mapping
, mapping
->begin
, mapping
->end
, mapping
->dir_index
,
1368 mapping
->first_mapping_index
, mapping
->path
, mapping
->mode
);
1370 if (mapping
->mode
& MODE_DIRECTORY
)
1371 fprintf(stderr
, "parent_mapping_index = %d, first_dir_index = %d\n", mapping
->info
.dir
.parent_mapping_index
, mapping
->info
.dir
.first_dir_index
);
1373 fprintf(stderr
, "offset = %d\n", mapping
->info
.file
.offset
);
1377 static int vvfat_read(BlockDriverState
*bs
, int64_t sector_num
,
1378 uint8_t *buf
, int nb_sectors
)
1380 BDRVVVFATState
*s
= bs
->opaque
;
1383 for(i
=0;i
<nb_sectors
;i
++,sector_num
++) {
1384 if (sector_num
>= bs
->total_sectors
)
1388 if (bdrv_is_allocated(s
->qcow
, sector_num
, nb_sectors
-i
, &n
)) {
1389 DLOG(fprintf(stderr
, "sectors %d+%d allocated\n", (int)sector_num
, n
));
1390 if (bdrv_read(s
->qcow
, sector_num
, buf
+ i
*0x200, n
)) {
1394 sector_num
+= n
- 1;
1397 DLOG(fprintf(stderr
, "sector %d not allocated\n", (int)sector_num
));
1399 if(sector_num
<s
->faked_sectors
) {
1400 if(sector_num
<s
->first_sectors_number
)
1401 memcpy(buf
+i
*0x200,&(s
->first_sectors
[sector_num
*0x200]),0x200);
1402 else if(sector_num
-s
->first_sectors_number
<s
->sectors_per_fat
)
1403 memcpy(buf
+i
*0x200,&(s
->fat
.pointer
[(sector_num
-s
->first_sectors_number
)*0x200]),0x200);
1404 else if(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
<s
->sectors_per_fat
)
1405 memcpy(buf
+i
*0x200,&(s
->fat
.pointer
[(sector_num
-s
->first_sectors_number
-s
->sectors_per_fat
)*0x200]),0x200);
1407 uint32_t sector
=sector_num
-s
->faked_sectors
,
1408 sector_offset_in_cluster
=(sector
%s
->sectors_per_cluster
),
1409 cluster_num
=sector
/s
->sectors_per_cluster
;
1410 if(cluster_num
> s
->cluster_count
|| read_cluster(s
, cluster_num
) != 0) {
1411 /* LATER TODO: strict: return -1; */
1412 memset(buf
+i
*0x200,0,0x200);
1415 memcpy(buf
+i
*0x200,s
->cluster
+sector_offset_in_cluster
*0x200,0x200);
1421 static coroutine_fn
int vvfat_co_read(BlockDriverState
*bs
, int64_t sector_num
,
1422 uint8_t *buf
, int nb_sectors
)
1425 BDRVVVFATState
*s
= bs
->opaque
;
1426 qemu_co_mutex_lock(&s
->lock
);
1427 ret
= vvfat_read(bs
, sector_num
, buf
, nb_sectors
);
1428 qemu_co_mutex_unlock(&s
->lock
);
1432 /* LATER TODO: statify all functions */
1435 * Idea of the write support (use snapshot):
1437 * 1. check if all data is consistent, recording renames, modifications,
1438 * new files and directories (in s->commits).
1440 * 2. if the data is not consistent, stop committing
1442 * 3. handle renames, and create new files and directories (do not yet
1443 * write their contents)
1445 * 4. walk the directories, fixing the mapping and direntries, and marking
1446 * the handled mappings as not deleted
1448 * 5. commit the contents of the files
1450 * 6. handle deleted files and directories
1454 typedef struct commit_t
{
1457 struct { uint32_t cluster
; } rename
;
1458 struct { int dir_index
; uint32_t modified_offset
; } writeout
;
1459 struct { uint32_t first_cluster
; } new_file
;
1460 struct { uint32_t cluster
; } mkdir
;
1462 /* DELETEs and RMDIRs are handled differently: see handle_deletes() */
1464 ACTION_RENAME
, ACTION_WRITEOUT
, ACTION_NEW_FILE
, ACTION_MKDIR
1468 static void clear_commits(BDRVVVFATState
* s
)
1471 DLOG(fprintf(stderr
, "clear_commits (%d commits)\n", s
->commits
.next
));
1472 for (i
= 0; i
< s
->commits
.next
; i
++) {
1473 commit_t
* commit
= array_get(&(s
->commits
), i
);
1474 assert(commit
->path
|| commit
->action
== ACTION_WRITEOUT
);
1475 if (commit
->action
!= ACTION_WRITEOUT
) {
1476 assert(commit
->path
);
1477 g_free(commit
->path
);
1479 assert(commit
->path
== NULL
);
1481 s
->commits
.next
= 0;
1484 static void schedule_rename(BDRVVVFATState
* s
,
1485 uint32_t cluster
, char* new_path
)
1487 commit_t
* commit
= array_get_next(&(s
->commits
));
1488 commit
->path
= new_path
;
1489 commit
->param
.rename
.cluster
= cluster
;
1490 commit
->action
= ACTION_RENAME
;
1493 static void schedule_writeout(BDRVVVFATState
* s
,
1494 int dir_index
, uint32_t modified_offset
)
1496 commit_t
* commit
= array_get_next(&(s
->commits
));
1497 commit
->path
= NULL
;
1498 commit
->param
.writeout
.dir_index
= dir_index
;
1499 commit
->param
.writeout
.modified_offset
= modified_offset
;
1500 commit
->action
= ACTION_WRITEOUT
;
1503 static void schedule_new_file(BDRVVVFATState
* s
,
1504 char* path
, uint32_t first_cluster
)
1506 commit_t
* commit
= array_get_next(&(s
->commits
));
1507 commit
->path
= path
;
1508 commit
->param
.new_file
.first_cluster
= first_cluster
;
1509 commit
->action
= ACTION_NEW_FILE
;
1512 static void schedule_mkdir(BDRVVVFATState
* s
, uint32_t cluster
, char* path
)
1514 commit_t
* commit
= array_get_next(&(s
->commits
));
1515 commit
->path
= path
;
1516 commit
->param
.mkdir
.cluster
= cluster
;
1517 commit
->action
= ACTION_MKDIR
;
1522 * Since the sequence number is at most 0x3f, and the filename
1523 * length is at most 13 times the sequence number, the maximal
1524 * filename length is 0x3f * 13 bytes.
1526 unsigned char name
[0x3f * 13 + 1];
1528 int sequence_number
;
1531 static void lfn_init(long_file_name
* lfn
)
1533 lfn
->sequence_number
= lfn
->len
= 0;
1534 lfn
->checksum
= 0x100;
1537 /* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
1538 static int parse_long_name(long_file_name
* lfn
,
1539 const direntry_t
* direntry
)
1542 const unsigned char* pointer
= (const unsigned char*)direntry
;
1544 if (!is_long_name(direntry
))
1547 if (pointer
[0] & 0x40) {
1548 lfn
->sequence_number
= pointer
[0] & 0x3f;
1549 lfn
->checksum
= pointer
[13];
1551 lfn
->name
[lfn
->sequence_number
* 13] = 0;
1552 } else if ((pointer
[0] & 0x3f) != --lfn
->sequence_number
)
1554 else if (pointer
[13] != lfn
->checksum
)
1556 else if (pointer
[12] || pointer
[26] || pointer
[27])
1559 offset
= 13 * (lfn
->sequence_number
- 1);
1560 for (i
= 0, j
= 1; i
< 13; i
++, j
+=2) {
1566 if (pointer
[j
+1] == 0)
1567 lfn
->name
[offset
+ i
] = pointer
[j
];
1568 else if (pointer
[j
+1] != 0xff || (pointer
[0] & 0x40) == 0)
1571 lfn
->name
[offset
+ i
] = 0;
1574 if (pointer
[0] & 0x40)
1575 lfn
->len
= offset
+ strlen((char*)lfn
->name
+ offset
);
1580 /* returns 0 if successful, >0 if no short_name, and <0 on error */
1581 static int parse_short_name(BDRVVVFATState
* s
,
1582 long_file_name
* lfn
, direntry_t
* direntry
)
1586 if (!is_short_name(direntry
))
1589 for (j
= 7; j
>= 0 && direntry
->name
[j
] == ' '; j
--);
1590 for (i
= 0; i
<= j
; i
++) {
1591 if (direntry
->name
[i
] <= ' ' || direntry
->name
[i
] > 0x7f)
1593 else if (s
->downcase_short_names
)
1594 lfn
->name
[i
] = qemu_tolower(direntry
->name
[i
]);
1596 lfn
->name
[i
] = direntry
->name
[i
];
1599 for (j
= 2; j
>= 0 && direntry
->name
[8 + j
] == ' '; j
--) {
1602 lfn
->name
[i
++] = '.';
1603 lfn
->name
[i
+ j
+ 1] = '\0';
1604 for (;j
>= 0; j
--) {
1605 uint8_t c
= direntry
->name
[8 + j
];
1606 if (c
<= ' ' || c
> 0x7f) {
1608 } else if (s
->downcase_short_names
) {
1609 lfn
->name
[i
+ j
] = qemu_tolower(c
);
1611 lfn
->name
[i
+ j
] = c
;
1615 lfn
->name
[i
+ j
+ 1] = '\0';
1617 lfn
->len
= strlen((char*)lfn
->name
);
1622 static inline uint32_t modified_fat_get(BDRVVVFATState
* s
,
1623 unsigned int cluster
)
1625 if (cluster
< s
->last_cluster_of_root_directory
) {
1626 if (cluster
+ 1 == s
->last_cluster_of_root_directory
)
1627 return s
->max_fat_value
;
1632 if (s
->fat_type
==32) {
1633 uint32_t* entry
=((uint32_t*)s
->fat2
)+cluster
;
1634 return le32_to_cpu(*entry
);
1635 } else if (s
->fat_type
==16) {
1636 uint16_t* entry
=((uint16_t*)s
->fat2
)+cluster
;
1637 return le16_to_cpu(*entry
);
1639 const uint8_t* x
=s
->fat2
+cluster
*3/2;
1640 return ((x
[0]|(x
[1]<<8))>>(cluster
&1?4:0))&0x0fff;
1644 static inline int cluster_was_modified(BDRVVVFATState
* s
, uint32_t cluster_num
)
1646 int was_modified
= 0;
1649 if (s
->qcow
== NULL
)
1652 for (i
= 0; !was_modified
&& i
< s
->sectors_per_cluster
; i
++)
1653 was_modified
= bdrv_is_allocated(s
->qcow
,
1654 cluster2sector(s
, cluster_num
) + i
, 1, &dummy
);
1656 return was_modified
;
1659 static const char* get_basename(const char* path
)
1661 char* basename
= strrchr(path
, '/');
1662 if (basename
== NULL
)
1665 return basename
+ 1; /* strip '/' */
1669 * The array s->used_clusters holds the states of the clusters. If it is
1670 * part of a file, it has bit 2 set, in case of a directory, bit 1. If it
1671 * was modified, bit 3 is set.
1672 * If any cluster is allocated, but not part of a file or directory, this
1673 * driver refuses to commit.
1676 USED_DIRECTORY
= 1, USED_FILE
= 2, USED_ANY
= 3, USED_ALLOCATED
= 4
1680 * get_cluster_count_for_direntry() not only determines how many clusters
1681 * are occupied by direntry, but also if it was renamed or modified.
1683 * A file is thought to be renamed *only* if there already was a file with
1684 * exactly the same first cluster, but a different name.
1686 * Further, the files/directories handled by this function are
1687 * assumed to be *not* deleted (and *only* those).
1689 static uint32_t get_cluster_count_for_direntry(BDRVVVFATState
* s
,
1690 direntry_t
* direntry
, const char* path
)
1693 * This is a little bit tricky:
1694 * IF the guest OS just inserts a cluster into the file chain,
1695 * and leaves the rest alone, (i.e. the original file had clusters
1696 * 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
1698 * - do_commit will write the cluster into the file at the given
1701 * - the cluster which is overwritten should be moved to a later
1702 * position in the file.
1704 * I am not aware that any OS does something as braindead, but this
1705 * situation could happen anyway when not committing for a long time.
1706 * Just to be sure that this does not bite us, detect it, and copy the
1707 * contents of the clusters to-be-overwritten into the qcow.
1710 int was_modified
= 0;
1713 uint32_t cluster_num
= begin_of_direntry(direntry
);
1714 uint32_t offset
= 0;
1715 int first_mapping_index
= -1;
1716 mapping_t
* mapping
= NULL
;
1717 const char* basename2
= NULL
;
1719 vvfat_close_current_file(s
);
1721 /* the root directory */
1722 if (cluster_num
== 0)
1727 basename2
= get_basename(path
);
1729 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1732 const char* basename
;
1734 assert(mapping
->mode
& MODE_DELETED
);
1735 mapping
->mode
&= ~MODE_DELETED
;
1737 basename
= get_basename(mapping
->path
);
1739 assert(mapping
->mode
& MODE_NORMAL
);
1742 if (strcmp(basename
, basename2
))
1743 schedule_rename(s
, cluster_num
, g_strdup(path
));
1744 } else if (is_file(direntry
))
1746 schedule_new_file(s
, g_strdup(path
), cluster_num
);
1755 if (!copy_it
&& cluster_was_modified(s
, cluster_num
)) {
1756 if (mapping
== NULL
||
1757 mapping
->begin
> cluster_num
||
1758 mapping
->end
<= cluster_num
)
1759 mapping
= find_mapping_for_cluster(s
, cluster_num
);
1763 (mapping
->mode
& MODE_DIRECTORY
) == 0) {
1765 /* was modified in qcow */
1766 if (offset
!= mapping
->info
.file
.offset
+ s
->cluster_size
1767 * (cluster_num
- mapping
->begin
)) {
1768 /* offset of this cluster in file chain has changed */
1771 } else if (offset
== 0) {
1772 const char* basename
= get_basename(mapping
->path
);
1774 if (strcmp(basename
, basename2
))
1776 first_mapping_index
= array_index(&(s
->mapping
), mapping
);
1779 if (mapping
->first_mapping_index
!= first_mapping_index
1780 && mapping
->info
.file
.offset
> 0) {
1785 /* need to write out? */
1786 if (!was_modified
&& is_file(direntry
)) {
1788 schedule_writeout(s
, mapping
->dir_index
, offset
);
1796 * This is horribly inefficient, but that is okay, since
1797 * it is rarely executed, if at all.
1799 int64_t offset
= cluster2sector(s
, cluster_num
);
1801 vvfat_close_current_file(s
);
1802 for (i
= 0; i
< s
->sectors_per_cluster
; i
++) {
1803 if (!bdrv_is_allocated(s
->qcow
, offset
+ i
, 1, &dummy
)) {
1804 if (vvfat_read(s
->bs
, offset
, s
->cluster_buffer
, 1)) {
1807 if (bdrv_write(s
->qcow
, offset
, s
->cluster_buffer
, 1)) {
1816 if (s
->used_clusters
[cluster_num
] & USED_ANY
)
1818 s
->used_clusters
[cluster_num
] = USED_FILE
;
1820 cluster_num
= modified_fat_get(s
, cluster_num
);
1822 if (fat_eof(s
, cluster_num
))
1824 else if (cluster_num
< 2 || cluster_num
> s
->max_fat_value
- 16)
1827 offset
+= s
->cluster_size
;
1832 * This function looks at the modified data (qcow).
1833 * It returns 0 upon inconsistency or error, and the number of clusters
1834 * used by the directory, its subdirectories and their files.
1836 static int check_directory_consistency(BDRVVVFATState
*s
,
1837 int cluster_num
, const char* path
)
1840 unsigned char* cluster
= g_malloc(s
->cluster_size
);
1841 direntry_t
* direntries
= (direntry_t
*)cluster
;
1842 mapping_t
* mapping
= find_mapping_for_cluster(s
, cluster_num
);
1845 int path_len
= strlen(path
);
1846 char path2
[PATH_MAX
+ 1];
1848 assert(path_len
< PATH_MAX
); /* len was tested before! */
1849 pstrcpy(path2
, sizeof(path2
), path
);
1850 path2
[path_len
] = '/';
1851 path2
[path_len
+ 1] = '\0';
1854 const char* basename
= get_basename(mapping
->path
);
1855 const char* basename2
= get_basename(path
);
1857 assert(mapping
->mode
& MODE_DIRECTORY
);
1859 assert(mapping
->mode
& MODE_DELETED
);
1860 mapping
->mode
&= ~MODE_DELETED
;
1862 if (strcmp(basename
, basename2
))
1863 schedule_rename(s
, cluster_num
, g_strdup(path
));
1866 schedule_mkdir(s
, cluster_num
, g_strdup(path
));
1875 if (s
->used_clusters
[cluster_num
] & USED_ANY
) {
1876 fprintf(stderr
, "cluster %d used more than once\n", (int)cluster_num
);
1879 s
->used_clusters
[cluster_num
] = USED_DIRECTORY
;
1881 DLOG(fprintf(stderr
, "read cluster %d (sector %d)\n", (int)cluster_num
, (int)cluster2sector(s
, cluster_num
)));
1882 subret
= vvfat_read(s
->bs
, cluster2sector(s
, cluster_num
), cluster
,
1883 s
->sectors_per_cluster
);
1885 fprintf(stderr
, "Error fetching direntries\n");
1891 for (i
= 0; i
< 0x10 * s
->sectors_per_cluster
; i
++) {
1892 int cluster_count
= 0;
1894 DLOG(fprintf(stderr
, "check direntry %d:\n", i
); print_direntry(direntries
+ i
));
1895 if (is_volume_label(direntries
+ i
) || is_dot(direntries
+ i
) ||
1896 is_free(direntries
+ i
))
1899 subret
= parse_long_name(&lfn
, direntries
+ i
);
1901 fprintf(stderr
, "Error in long name\n");
1904 if (subret
== 0 || is_free(direntries
+ i
))
1907 if (fat_chksum(direntries
+i
) != lfn
.checksum
) {
1908 subret
= parse_short_name(s
, &lfn
, direntries
+ i
);
1910 fprintf(stderr
, "Error in short name (%d)\n", subret
);
1913 if (subret
> 0 || !strcmp((char*)lfn
.name
, ".")
1914 || !strcmp((char*)lfn
.name
, ".."))
1917 lfn
.checksum
= 0x100; /* cannot use long name twice */
1919 if (path_len
+ 1 + lfn
.len
>= PATH_MAX
) {
1920 fprintf(stderr
, "Name too long: %s/%s\n", path
, lfn
.name
);
1923 pstrcpy(path2
+ path_len
+ 1, sizeof(path2
) - path_len
- 1,
1926 if (is_directory(direntries
+ i
)) {
1927 if (begin_of_direntry(direntries
+ i
) == 0) {
1928 DLOG(fprintf(stderr
, "invalid begin for directory: %s\n", path2
); print_direntry(direntries
+ i
));
1931 cluster_count
= check_directory_consistency(s
,
1932 begin_of_direntry(direntries
+ i
), path2
);
1933 if (cluster_count
== 0) {
1934 DLOG(fprintf(stderr
, "problem in directory %s:\n", path2
); print_direntry(direntries
+ i
));
1937 } else if (is_file(direntries
+ i
)) {
1938 /* check file size with FAT */
1939 cluster_count
= get_cluster_count_for_direntry(s
, direntries
+ i
, path2
);
1940 if (cluster_count
!=
1941 (le32_to_cpu(direntries
[i
].size
) + s
->cluster_size
1942 - 1) / s
->cluster_size
) {
1943 DLOG(fprintf(stderr
, "Cluster count mismatch\n"));
1947 abort(); /* cluster_count = 0; */
1949 ret
+= cluster_count
;
1952 cluster_num
= modified_fat_get(s
, cluster_num
);
1953 } while(!fat_eof(s
, cluster_num
));
1959 /* returns 1 on success */
1960 static int is_consistent(BDRVVVFATState
* s
)
1963 int used_clusters_count
= 0;
1967 * - get modified FAT
1968 * - compare the two FATs (TODO)
1969 * - get buffer for marking used clusters
1970 * - recurse direntries from root (using bs->bdrv_read to make
1971 * sure to get the new data)
1972 * - check that the FAT agrees with the size
1973 * - count the number of clusters occupied by this directory and
1975 * - check that the cumulative used cluster count agrees with the
1977 * - if all is fine, return number of used clusters
1979 if (s
->fat2
== NULL
) {
1980 int size
= 0x200 * s
->sectors_per_fat
;
1981 s
->fat2
= g_malloc(size
);
1982 memcpy(s
->fat2
, s
->fat
.pointer
, size
);
1984 check
= vvfat_read(s
->bs
,
1985 s
->first_sectors_number
, s
->fat2
, s
->sectors_per_fat
);
1987 fprintf(stderr
, "Could not copy fat\n");
1990 assert (s
->used_clusters
);
1991 for (i
= 0; i
< sector2cluster(s
, s
->sector_count
); i
++)
1992 s
->used_clusters
[i
] &= ~USED_ANY
;
1996 /* mark every mapped file/directory as deleted.
1997 * (check_directory_consistency() will unmark those still present). */
1999 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2000 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2001 if (mapping
->first_mapping_index
< 0)
2002 mapping
->mode
|= MODE_DELETED
;
2005 used_clusters_count
= check_directory_consistency(s
, 0, s
->path
);
2006 if (used_clusters_count
<= 0) {
2007 DLOG(fprintf(stderr
, "problem in directory\n"));
2011 check
= s
->last_cluster_of_root_directory
;
2012 for (i
= check
; i
< sector2cluster(s
, s
->sector_count
); i
++) {
2013 if (modified_fat_get(s
, i
)) {
2014 if(!s
->used_clusters
[i
]) {
2015 DLOG(fprintf(stderr
, "FAT was modified (%d), but cluster is not used?\n", i
));
2021 if (s
->used_clusters
[i
] == USED_ALLOCATED
) {
2022 /* allocated, but not used... */
2023 DLOG(fprintf(stderr
, "unused, modified cluster: %d\n", i
));
2028 if (check
!= used_clusters_count
)
2031 return used_clusters_count
;
2034 static inline void adjust_mapping_indices(BDRVVVFATState
* s
,
2035 int offset
, int adjust
)
2039 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2040 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2042 #define ADJUST_MAPPING_INDEX(name) \
2043 if (mapping->name >= offset) \
2044 mapping->name += adjust
2046 ADJUST_MAPPING_INDEX(first_mapping_index
);
2047 if (mapping
->mode
& MODE_DIRECTORY
)
2048 ADJUST_MAPPING_INDEX(info
.dir
.parent_mapping_index
);
2052 /* insert or update mapping */
2053 static mapping_t
* insert_mapping(BDRVVVFATState
* s
,
2054 uint32_t begin
, uint32_t end
)
2057 * - find mapping where mapping->begin >= begin,
2058 * - if mapping->begin > begin: insert
2059 * - adjust all references to mappings!
2063 int index
= find_mapping_for_cluster_aux(s
, begin
, 0, s
->mapping
.next
);
2064 mapping_t
* mapping
= NULL
;
2065 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
2067 if (index
< s
->mapping
.next
&& (mapping
= array_get(&(s
->mapping
), index
))
2068 && mapping
->begin
< begin
) {
2069 mapping
->end
= begin
;
2071 mapping
= array_get(&(s
->mapping
), index
);
2073 if (index
>= s
->mapping
.next
|| mapping
->begin
> begin
) {
2074 mapping
= array_insert(&(s
->mapping
), index
, 1);
2075 mapping
->path
= NULL
;
2076 adjust_mapping_indices(s
, index
, +1);
2079 mapping
->begin
= begin
;
2082 DLOG(mapping_t
* next_mapping
;
2083 assert(index
+ 1 >= s
->mapping
.next
||
2084 ((next_mapping
= array_get(&(s
->mapping
), index
+ 1)) &&
2085 next_mapping
->begin
>= end
)));
2087 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
2088 s
->current_mapping
= array_get(&(s
->mapping
),
2089 s
->current_mapping
- first_mapping
);
2094 static int remove_mapping(BDRVVVFATState
* s
, int mapping_index
)
2096 mapping_t
* mapping
= array_get(&(s
->mapping
), mapping_index
);
2097 mapping_t
* first_mapping
= array_get(&(s
->mapping
), 0);
2100 if (mapping
->first_mapping_index
< 0) {
2101 g_free(mapping
->path
);
2104 /* remove from s->mapping */
2105 array_remove(&(s
->mapping
), mapping_index
);
2107 /* adjust all references to mappings */
2108 adjust_mapping_indices(s
, mapping_index
, -1);
2110 if (s
->current_mapping
&& first_mapping
!= (mapping_t
*)s
->mapping
.pointer
)
2111 s
->current_mapping
= array_get(&(s
->mapping
),
2112 s
->current_mapping
- first_mapping
);
2117 static void adjust_dirindices(BDRVVVFATState
* s
, int offset
, int adjust
)
2120 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2121 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2122 if (mapping
->dir_index
>= offset
)
2123 mapping
->dir_index
+= adjust
;
2124 if ((mapping
->mode
& MODE_DIRECTORY
) &&
2125 mapping
->info
.dir
.first_dir_index
>= offset
)
2126 mapping
->info
.dir
.first_dir_index
+= adjust
;
2130 static direntry_t
* insert_direntries(BDRVVVFATState
* s
,
2131 int dir_index
, int count
)
2134 * make room in s->directory,
2137 direntry_t
* result
= array_insert(&(s
->directory
), dir_index
, count
);
2140 adjust_dirindices(s
, dir_index
, count
);
2144 static int remove_direntries(BDRVVVFATState
* s
, int dir_index
, int count
)
2146 int ret
= array_remove_slice(&(s
->directory
), dir_index
, count
);
2149 adjust_dirindices(s
, dir_index
, -count
);
2154 * Adapt the mappings of the cluster chain starting at first cluster
2155 * (i.e. if a file starts at first_cluster, the chain is followed according
2156 * to the modified fat, and the corresponding entries in s->mapping are
2159 static int commit_mappings(BDRVVVFATState
* s
,
2160 uint32_t first_cluster
, int dir_index
)
2162 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2163 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2164 uint32_t cluster
= first_cluster
;
2166 vvfat_close_current_file(s
);
2169 assert(mapping
->begin
== first_cluster
);
2170 mapping
->first_mapping_index
= -1;
2171 mapping
->dir_index
= dir_index
;
2172 mapping
->mode
= (dir_index
<= 0 || is_directory(direntry
)) ?
2173 MODE_DIRECTORY
: MODE_NORMAL
;
2175 while (!fat_eof(s
, cluster
)) {
2178 for (c
= cluster
, c1
= modified_fat_get(s
, c
); c
+ 1 == c1
;
2179 c
= c1
, c1
= modified_fat_get(s
, c1
));
2182 if (c
> mapping
->end
) {
2183 int index
= array_index(&(s
->mapping
), mapping
);
2184 int i
, max_i
= s
->mapping
.next
- index
;
2185 for (i
= 1; i
< max_i
&& mapping
[i
].begin
< c
; i
++);
2187 remove_mapping(s
, index
+ 1);
2189 assert(mapping
== array_get(&(s
->mapping
), s
->mapping
.next
- 1)
2190 || mapping
[1].begin
>= c
);
2193 if (!fat_eof(s
, c1
)) {
2194 int i
= find_mapping_for_cluster_aux(s
, c1
, 0, s
->mapping
.next
);
2195 mapping_t
* next_mapping
= i
>= s
->mapping
.next
? NULL
:
2196 array_get(&(s
->mapping
), i
);
2198 if (next_mapping
== NULL
|| next_mapping
->begin
> c1
) {
2199 int i1
= array_index(&(s
->mapping
), mapping
);
2201 next_mapping
= insert_mapping(s
, c1
, c1
+1);
2205 mapping
= array_get(&(s
->mapping
), i1
);
2208 next_mapping
->dir_index
= mapping
->dir_index
;
2209 next_mapping
->first_mapping_index
=
2210 mapping
->first_mapping_index
< 0 ?
2211 array_index(&(s
->mapping
), mapping
) :
2212 mapping
->first_mapping_index
;
2213 next_mapping
->path
= mapping
->path
;
2214 next_mapping
->mode
= mapping
->mode
;
2215 next_mapping
->read_only
= mapping
->read_only
;
2216 if (mapping
->mode
& MODE_DIRECTORY
) {
2217 next_mapping
->info
.dir
.parent_mapping_index
=
2218 mapping
->info
.dir
.parent_mapping_index
;
2219 next_mapping
->info
.dir
.first_dir_index
=
2220 mapping
->info
.dir
.first_dir_index
+
2221 0x10 * s
->sectors_per_cluster
*
2222 (mapping
->end
- mapping
->begin
);
2224 next_mapping
->info
.file
.offset
= mapping
->info
.file
.offset
+
2225 mapping
->end
- mapping
->begin
;
2227 mapping
= next_mapping
;
2236 static int commit_direntries(BDRVVVFATState
* s
,
2237 int dir_index
, int parent_mapping_index
)
2239 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2240 uint32_t first_cluster
= dir_index
== 0 ? 0 : begin_of_direntry(direntry
);
2241 mapping_t
* mapping
= find_mapping_for_cluster(s
, first_cluster
);
2243 int factor
= 0x10 * s
->sectors_per_cluster
;
2244 int old_cluster_count
, new_cluster_count
;
2245 int current_dir_index
= mapping
->info
.dir
.first_dir_index
;
2246 int first_dir_index
= current_dir_index
;
2250 DLOG(fprintf(stderr
, "commit_direntries for %s, parent_mapping_index %d\n", mapping
->path
, parent_mapping_index
));
2254 assert(mapping
->begin
== first_cluster
);
2255 assert(mapping
->info
.dir
.first_dir_index
< s
->directory
.next
);
2256 assert(mapping
->mode
& MODE_DIRECTORY
);
2257 assert(dir_index
== 0 || is_directory(direntry
));
2259 mapping
->info
.dir
.parent_mapping_index
= parent_mapping_index
;
2261 if (first_cluster
== 0) {
2262 old_cluster_count
= new_cluster_count
=
2263 s
->last_cluster_of_root_directory
;
2265 for (old_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2267 old_cluster_count
++;
2269 for (new_cluster_count
= 0, c
= first_cluster
; !fat_eof(s
, c
);
2270 c
= modified_fat_get(s
, c
))
2271 new_cluster_count
++;
2274 if (new_cluster_count
> old_cluster_count
) {
2275 if (insert_direntries(s
,
2276 current_dir_index
+ factor
* old_cluster_count
,
2277 factor
* (new_cluster_count
- old_cluster_count
)) == NULL
)
2279 } else if (new_cluster_count
< old_cluster_count
)
2280 remove_direntries(s
,
2281 current_dir_index
+ factor
* new_cluster_count
,
2282 factor
* (old_cluster_count
- new_cluster_count
));
2284 for (c
= first_cluster
; !fat_eof(s
, c
); c
= modified_fat_get(s
, c
)) {
2285 void* direntry
= array_get(&(s
->directory
), current_dir_index
);
2286 int ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
), direntry
,
2287 s
->sectors_per_cluster
);
2290 assert(!strncmp(s
->directory
.pointer
, "QEMU", 4));
2291 current_dir_index
+= factor
;
2294 ret
= commit_mappings(s
, first_cluster
, dir_index
);
2299 for (i
= 0; i
< factor
* new_cluster_count
; i
++) {
2300 direntry
= array_get(&(s
->directory
), first_dir_index
+ i
);
2301 if (is_directory(direntry
) && !is_dot(direntry
)) {
2302 mapping
= find_mapping_for_cluster(s
, first_cluster
);
2303 assert(mapping
->mode
& MODE_DIRECTORY
);
2304 ret
= commit_direntries(s
, first_dir_index
+ i
,
2305 array_index(&(s
->mapping
), mapping
));
2314 /* commit one file (adjust contents, adjust mapping),
2315 return first_mapping_index */
2316 static int commit_one_file(BDRVVVFATState
* s
,
2317 int dir_index
, uint32_t offset
)
2319 direntry_t
* direntry
= array_get(&(s
->directory
), dir_index
);
2320 uint32_t c
= begin_of_direntry(direntry
);
2321 uint32_t first_cluster
= c
;
2322 mapping_t
* mapping
= find_mapping_for_cluster(s
, c
);
2323 uint32_t size
= filesize_of_direntry(direntry
);
2324 char* cluster
= g_malloc(s
->cluster_size
);
2328 assert(offset
< size
);
2329 assert((offset
% s
->cluster_size
) == 0);
2331 for (i
= s
->cluster_size
; i
< offset
; i
+= s
->cluster_size
)
2332 c
= modified_fat_get(s
, c
);
2334 fd
= qemu_open(mapping
->path
, O_RDWR
| O_CREAT
| O_BINARY
, 0666);
2336 fprintf(stderr
, "Could not open %s... (%s, %d)\n", mapping
->path
,
2337 strerror(errno
), errno
);
2342 if (lseek(fd
, offset
, SEEK_SET
) != offset
) {
2349 while (offset
< size
) {
2351 int rest_size
= (size
- offset
> s
->cluster_size
?
2352 s
->cluster_size
: size
- offset
);
2355 c1
= modified_fat_get(s
, c
);
2357 assert((size
- offset
== 0 && fat_eof(s
, c
)) ||
2358 (size
> offset
&& c
>=2 && !fat_eof(s
, c
)));
2360 ret
= vvfat_read(s
->bs
, cluster2sector(s
, c
),
2361 (uint8_t*)cluster
, (rest_size
+ 0x1ff) / 0x200);
2369 if (write(fd
, cluster
, rest_size
) < 0) {
2375 offset
+= rest_size
;
2379 if (ftruncate(fd
, size
)) {
2380 perror("ftruncate()");
2388 return commit_mappings(s
, first_cluster
, dir_index
);
2392 /* test, if all mappings point to valid direntries */
2393 static void check1(BDRVVVFATState
* s
)
2396 for (i
= 0; i
< s
->mapping
.next
; i
++) {
2397 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2398 if (mapping
->mode
& MODE_DELETED
) {
2399 fprintf(stderr
, "deleted\n");
2402 assert(mapping
->dir_index
< s
->directory
.next
);
2403 direntry_t
* direntry
= array_get(&(s
->directory
), mapping
->dir_index
);
2404 assert(mapping
->begin
== begin_of_direntry(direntry
) || mapping
->first_mapping_index
>= 0);
2405 if (mapping
->mode
& MODE_DIRECTORY
) {
2406 assert(mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
* (mapping
->end
- mapping
->begin
) <= s
->directory
.next
);
2407 assert((mapping
->info
.dir
.first_dir_index
% (0x10 * s
->sectors_per_cluster
)) == 0);
2412 /* test, if all direntries have mappings */
2413 static void check2(BDRVVVFATState
* s
)
2416 int first_mapping
= -1;
2418 for (i
= 0; i
< s
->directory
.next
; i
++) {
2419 direntry_t
* direntry
= array_get(&(s
->directory
), i
);
2421 if (is_short_name(direntry
) && begin_of_direntry(direntry
)) {
2422 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin_of_direntry(direntry
));
2424 assert(mapping
->dir_index
== i
|| is_dot(direntry
));
2425 assert(mapping
->begin
== begin_of_direntry(direntry
) || is_dot(direntry
));
2428 if ((i
% (0x10 * s
->sectors_per_cluster
)) == 0) {
2432 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2433 mapping_t
* mapping
= array_get(&(s
->mapping
), j
);
2434 if (mapping
->mode
& MODE_DELETED
)
2436 if (mapping
->mode
& MODE_DIRECTORY
) {
2437 if (mapping
->info
.dir
.first_dir_index
<= i
&& mapping
->info
.dir
.first_dir_index
+ 0x10 * s
->sectors_per_cluster
> i
) {
2438 assert(++count
== 1);
2439 if (mapping
->first_mapping_index
== -1)
2440 first_mapping
= array_index(&(s
->mapping
), mapping
);
2442 assert(first_mapping
== mapping
->first_mapping_index
);
2443 if (mapping
->info
.dir
.parent_mapping_index
< 0)
2446 mapping_t
* parent
= array_get(&(s
->mapping
), mapping
->info
.dir
.parent_mapping_index
);
2447 assert(parent
->mode
& MODE_DIRECTORY
);
2448 assert(parent
->info
.dir
.first_dir_index
< mapping
->info
.dir
.first_dir_index
);
2460 static int handle_renames_and_mkdirs(BDRVVVFATState
* s
)
2465 fprintf(stderr
, "handle_renames\n");
2466 for (i
= 0; i
< s
->commits
.next
; i
++) {
2467 commit_t
* commit
= array_get(&(s
->commits
), i
);
2468 fprintf(stderr
, "%d, %s (%d, %d)\n", i
, commit
->path
? commit
->path
: "(null)", commit
->param
.rename
.cluster
, commit
->action
);
2472 for (i
= 0; i
< s
->commits
.next
;) {
2473 commit_t
* commit
= array_get(&(s
->commits
), i
);
2474 if (commit
->action
== ACTION_RENAME
) {
2475 mapping_t
* mapping
= find_mapping_for_cluster(s
,
2476 commit
->param
.rename
.cluster
);
2477 char* old_path
= mapping
->path
;
2479 assert(commit
->path
);
2480 mapping
->path
= commit
->path
;
2481 if (rename(old_path
, mapping
->path
))
2484 if (mapping
->mode
& MODE_DIRECTORY
) {
2485 int l1
= strlen(mapping
->path
);
2486 int l2
= strlen(old_path
);
2488 direntry_t
* direntry
= array_get(&(s
->directory
),
2489 mapping
->info
.dir
.first_dir_index
);
2490 uint32_t c
= mapping
->begin
;
2494 while (!fat_eof(s
, c
)) {
2496 direntry_t
* d
= direntry
+ i
;
2498 if (is_file(d
) || (is_directory(d
) && !is_dot(d
))) {
2499 mapping_t
* m
= find_mapping_for_cluster(s
,
2500 begin_of_direntry(d
));
2501 int l
= strlen(m
->path
);
2502 char* new_path
= g_malloc(l
+ diff
+ 1);
2504 assert(!strncmp(m
->path
, mapping
->path
, l2
));
2506 pstrcpy(new_path
, l
+ diff
+ 1, mapping
->path
);
2507 pstrcpy(new_path
+ l1
, l
+ diff
+ 1 - l1
,
2510 schedule_rename(s
, m
->begin
, new_path
);
2513 } while((i
% (0x10 * s
->sectors_per_cluster
)) != 0);
2519 array_remove(&(s
->commits
), i
);
2521 } else if (commit
->action
== ACTION_MKDIR
) {
2523 int j
, parent_path_len
;
2526 if (mkdir(commit
->path
))
2529 if (mkdir(commit
->path
, 0755))
2533 mapping
= insert_mapping(s
, commit
->param
.mkdir
.cluster
,
2534 commit
->param
.mkdir
.cluster
+ 1);
2535 if (mapping
== NULL
)
2538 mapping
->mode
= MODE_DIRECTORY
;
2539 mapping
->read_only
= 0;
2540 mapping
->path
= commit
->path
;
2541 j
= s
->directory
.next
;
2543 insert_direntries(s
, s
->directory
.next
,
2544 0x10 * s
->sectors_per_cluster
);
2545 mapping
->info
.dir
.first_dir_index
= j
;
2547 parent_path_len
= strlen(commit
->path
)
2548 - strlen(get_basename(commit
->path
)) - 1;
2549 for (j
= 0; j
< s
->mapping
.next
; j
++) {
2550 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2551 if (m
->first_mapping_index
< 0 && m
!= mapping
&&
2552 !strncmp(m
->path
, mapping
->path
, parent_path_len
) &&
2553 strlen(m
->path
) == parent_path_len
)
2556 assert(j
< s
->mapping
.next
);
2557 mapping
->info
.dir
.parent_mapping_index
= j
;
2559 array_remove(&(s
->commits
), i
);
2569 * TODO: make sure that the short name is not matching *another* file
2571 static int handle_commits(BDRVVVFATState
* s
)
2575 vvfat_close_current_file(s
);
2577 for (i
= 0; !fail
&& i
< s
->commits
.next
; i
++) {
2578 commit_t
* commit
= array_get(&(s
->commits
), i
);
2579 switch(commit
->action
) {
2580 case ACTION_RENAME
: case ACTION_MKDIR
:
2584 case ACTION_WRITEOUT
: {
2586 /* these variables are only used by assert() below */
2587 direntry_t
* entry
= array_get(&(s
->directory
),
2588 commit
->param
.writeout
.dir_index
);
2589 uint32_t begin
= begin_of_direntry(entry
);
2590 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2594 assert(mapping
->begin
== begin
);
2595 assert(commit
->path
== NULL
);
2597 if (commit_one_file(s
, commit
->param
.writeout
.dir_index
,
2598 commit
->param
.writeout
.modified_offset
))
2603 case ACTION_NEW_FILE
: {
2604 int begin
= commit
->param
.new_file
.first_cluster
;
2605 mapping_t
* mapping
= find_mapping_for_cluster(s
, begin
);
2610 for (i
= 0; i
< s
->directory
.next
; i
++) {
2611 entry
= array_get(&(s
->directory
), i
);
2612 if (is_file(entry
) && begin_of_direntry(entry
) == begin
)
2616 if (i
>= s
->directory
.next
) {
2621 /* make sure there exists an initial mapping */
2622 if (mapping
&& mapping
->begin
!= begin
) {
2623 mapping
->end
= begin
;
2626 if (mapping
== NULL
) {
2627 mapping
= insert_mapping(s
, begin
, begin
+1);
2629 /* most members will be fixed in commit_mappings() */
2630 assert(commit
->path
);
2631 mapping
->path
= commit
->path
;
2632 mapping
->read_only
= 0;
2633 mapping
->mode
= MODE_NORMAL
;
2634 mapping
->info
.file
.offset
= 0;
2636 if (commit_one_file(s
, i
, 0))
2645 if (i
> 0 && array_remove_slice(&(s
->commits
), 0, i
))
2650 static int handle_deletes(BDRVVVFATState
* s
)
2652 int i
, deferred
= 1, deleted
= 1;
2654 /* delete files corresponding to mappings marked as deleted */
2655 /* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
2656 while (deferred
&& deleted
) {
2660 for (i
= 1; i
< s
->mapping
.next
; i
++) {
2661 mapping_t
* mapping
= array_get(&(s
->mapping
), i
);
2662 if (mapping
->mode
& MODE_DELETED
) {
2663 direntry_t
* entry
= array_get(&(s
->directory
),
2664 mapping
->dir_index
);
2666 if (is_free(entry
)) {
2667 /* remove file/directory */
2668 if (mapping
->mode
& MODE_DIRECTORY
) {
2669 int j
, next_dir_index
= s
->directory
.next
,
2670 first_dir_index
= mapping
->info
.dir
.first_dir_index
;
2672 if (rmdir(mapping
->path
) < 0) {
2673 if (errno
== ENOTEMPTY
) {
2680 for (j
= 1; j
< s
->mapping
.next
; j
++) {
2681 mapping_t
* m
= array_get(&(s
->mapping
), j
);
2682 if (m
->mode
& MODE_DIRECTORY
&&
2683 m
->info
.dir
.first_dir_index
>
2685 m
->info
.dir
.first_dir_index
<
2688 m
->info
.dir
.first_dir_index
;
2690 remove_direntries(s
, first_dir_index
,
2691 next_dir_index
- first_dir_index
);
2696 if (unlink(mapping
->path
))
2700 DLOG(fprintf(stderr
, "DELETE (%d)\n", i
); print_mapping(mapping
); print_direntry(entry
));
2701 remove_mapping(s
, i
);
2710 * synchronize mapping with new state:
2712 * - copy FAT (with bdrv_read)
2713 * - mark all filenames corresponding to mappings as deleted
2714 * - recurse direntries from root (using bs->bdrv_read)
2715 * - delete files corresponding to mappings marked as deleted
2717 static int do_commit(BDRVVVFATState
* s
)
2721 /* the real meat are the commits. Nothing to do? Move along! */
2722 if (s
->commits
.next
== 0)
2725 vvfat_close_current_file(s
);
2727 ret
= handle_renames_and_mkdirs(s
);
2729 fprintf(stderr
, "Error handling renames (%d)\n", ret
);
2734 /* copy FAT (with bdrv_read) */
2735 memcpy(s
->fat
.pointer
, s
->fat2
, 0x200 * s
->sectors_per_fat
);
2737 /* recurse direntries from root (using bs->bdrv_read) */
2738 ret
= commit_direntries(s
, 0, -1);
2740 fprintf(stderr
, "Fatal: error while committing (%d)\n", ret
);
2745 ret
= handle_commits(s
);
2747 fprintf(stderr
, "Error handling commits (%d)\n", ret
);
2752 ret
= handle_deletes(s
);
2754 fprintf(stderr
, "Error deleting\n");
2759 if (s
->qcow
->drv
->bdrv_make_empty
) {
2760 s
->qcow
->drv
->bdrv_make_empty(s
->qcow
);
2763 memset(s
->used_clusters
, 0, sector2cluster(s
, s
->sector_count
));
2769 static int try_commit(BDRVVVFATState
* s
)
2771 vvfat_close_current_file(s
);
2773 if(!is_consistent(s
))
2775 return do_commit(s
);
2778 static int vvfat_write(BlockDriverState
*bs
, int64_t sector_num
,
2779 const uint8_t *buf
, int nb_sectors
)
2781 BDRVVVFATState
*s
= bs
->opaque
;
2786 /* Check if we're operating in read-only mode */
2787 if (s
->qcow
== NULL
) {
2791 vvfat_close_current_file(s
);
2794 * Some sanity checks:
2795 * - do not allow writing to the boot sector
2796 * - do not allow to write non-ASCII filenames
2799 if (sector_num
< s
->first_sectors_number
)
2802 for (i
= sector2cluster(s
, sector_num
);
2803 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1);) {
2804 mapping_t
* mapping
= find_mapping_for_cluster(s
, i
);
2806 if (mapping
->read_only
) {
2807 fprintf(stderr
, "Tried to write to write-protected file %s\n",
2812 if (mapping
->mode
& MODE_DIRECTORY
) {
2813 int begin
= cluster2sector(s
, i
);
2814 int end
= begin
+ s
->sectors_per_cluster
, k
;
2816 const direntry_t
* direntries
;
2821 if (begin
< sector_num
)
2823 if (end
> sector_num
+ nb_sectors
)
2824 end
= sector_num
+ nb_sectors
;
2825 dir_index
= mapping
->dir_index
+
2826 0x10 * (begin
- mapping
->begin
* s
->sectors_per_cluster
);
2827 direntries
= (direntry_t
*)(buf
+ 0x200 * (begin
- sector_num
));
2829 for (k
= 0; k
< (end
- begin
) * 0x10; k
++) {
2830 /* do not allow non-ASCII filenames */
2831 if (parse_long_name(&lfn
, direntries
+ k
) < 0) {
2832 fprintf(stderr
, "Warning: non-ASCII filename\n");
2835 /* no access to the direntry of a read-only file */
2836 else if (is_short_name(direntries
+k
) &&
2837 (direntries
[k
].attributes
& 1)) {
2838 if (memcmp(direntries
+ k
,
2839 array_get(&(s
->directory
), dir_index
+ k
),
2840 sizeof(direntry_t
))) {
2841 fprintf(stderr
, "Warning: tried to write to write-protected file\n");
2853 * Use qcow backend. Commit later.
2855 DLOG(fprintf(stderr
, "Write to qcow backend: %d + %d\n", (int)sector_num
, nb_sectors
));
2856 ret
= bdrv_write(s
->qcow
, sector_num
, buf
, nb_sectors
);
2858 fprintf(stderr
, "Error writing to qcow backend\n");
2862 for (i
= sector2cluster(s
, sector_num
);
2863 i
<= sector2cluster(s
, sector_num
+ nb_sectors
- 1); i
++)
2865 s
->used_clusters
[i
] |= USED_ALLOCATED
;
2868 /* TODO: add timeout */
2875 static coroutine_fn
int vvfat_co_write(BlockDriverState
*bs
, int64_t sector_num
,
2876 const uint8_t *buf
, int nb_sectors
)
2879 BDRVVVFATState
*s
= bs
->opaque
;
2880 qemu_co_mutex_lock(&s
->lock
);
2881 ret
= vvfat_write(bs
, sector_num
, buf
, nb_sectors
);
2882 qemu_co_mutex_unlock(&s
->lock
);
2886 static int64_t coroutine_fn
vvfat_co_get_block_status(BlockDriverState
*bs
,
2887 int64_t sector_num
, int nb_sectors
, int *n
, BlockDriverState
**file
)
2889 BDRVVVFATState
* s
= bs
->opaque
;
2890 *n
= s
->sector_count
- sector_num
;
2891 if (*n
> nb_sectors
) {
2893 } else if (*n
< 0) {
2896 return BDRV_BLOCK_DATA
;
2899 static int write_target_commit(BlockDriverState
*bs
, int64_t sector_num
,
2900 const uint8_t* buffer
, int nb_sectors
) {
2901 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
2902 return try_commit(s
);
2905 static void write_target_close(BlockDriverState
*bs
) {
2906 BDRVVVFATState
* s
= *((BDRVVVFATState
**) bs
->opaque
);
2907 bdrv_unref(s
->qcow
);
2908 g_free(s
->qcow_filename
);
2911 static BlockDriver vvfat_write_target
= {
2912 .format_name
= "vvfat_write_target",
2913 .bdrv_write
= write_target_commit
,
2914 .bdrv_close
= write_target_close
,
2917 static int enable_write_target(BDRVVVFATState
*s
, Error
**errp
)
2919 BlockDriver
*bdrv_qcow
= NULL
;
2920 BlockDriverState
*backing
;
2921 QemuOpts
*opts
= NULL
;
2923 int size
= sector2cluster(s
, s
->sector_count
);
2926 s
->used_clusters
= calloc(size
, 1);
2928 array_init(&(s
->commits
), sizeof(commit_t
));
2930 s
->qcow_filename
= g_malloc(PATH_MAX
);
2931 ret
= get_tmp_filename(s
->qcow_filename
, PATH_MAX
);
2933 error_setg_errno(errp
, -ret
, "can't create temporary file");
2937 bdrv_qcow
= bdrv_find_format("qcow");
2939 error_setg(errp
, "Failed to locate qcow driver");
2944 opts
= qemu_opts_create(bdrv_qcow
->create_opts
, NULL
, 0, &error_abort
);
2945 qemu_opt_set_number(opts
, BLOCK_OPT_SIZE
, s
->sector_count
* 512,
2947 qemu_opt_set(opts
, BLOCK_OPT_BACKING_FILE
, "fat:", &error_abort
);
2949 ret
= bdrv_create(bdrv_qcow
, s
->qcow_filename
, opts
, errp
);
2950 qemu_opts_del(opts
);
2956 options
= qdict_new();
2957 qdict_put(options
, "driver", qstring_from_str("qcow"));
2958 ret
= bdrv_open(&s
->qcow
, s
->qcow_filename
, NULL
, options
,
2959 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
,
2966 unlink(s
->qcow_filename
);
2969 backing
= bdrv_new();
2970 bdrv_set_backing_hd(s
->bs
, backing
);
2971 bdrv_unref(backing
);
2973 s
->bs
->backing
->bs
->drv
= &vvfat_write_target
;
2974 s
->bs
->backing
->bs
->opaque
= g_new(void *, 1);
2975 *(void**)s
->bs
->backing
->bs
->opaque
= s
;
2980 g_free(s
->qcow_filename
);
2981 s
->qcow_filename
= NULL
;
2985 static void vvfat_close(BlockDriverState
*bs
)
2987 BDRVVVFATState
*s
= bs
->opaque
;
2989 vvfat_close_current_file(s
);
2990 array_free(&(s
->fat
));
2991 array_free(&(s
->directory
));
2992 array_free(&(s
->mapping
));
2993 g_free(s
->cluster_buffer
);
2996 migrate_del_blocker(s
->migration_blocker
);
2997 error_free(s
->migration_blocker
);
3001 static BlockDriver bdrv_vvfat
= {
3002 .format_name
= "vvfat",
3003 .protocol_name
= "fat",
3004 .instance_size
= sizeof(BDRVVVFATState
),
3006 .bdrv_parse_filename
= vvfat_parse_filename
,
3007 .bdrv_file_open
= vvfat_open
,
3008 .bdrv_close
= vvfat_close
,
3010 .bdrv_read
= vvfat_co_read
,
3011 .bdrv_write
= vvfat_co_write
,
3012 .bdrv_co_get_block_status
= vvfat_co_get_block_status
,
3015 static void bdrv_vvfat_init(void)
3017 bdrv_register(&bdrv_vvfat
);
3020 block_init(bdrv_vvfat_init
);
3023 static void checkpoint(void) {
3024 assert(((mapping_t
*)array_get(&(vvv
->mapping
), 0))->end
== 2);
3027 assert(!vvv
->current_mapping
|| vvv
->current_fd
|| (vvv
->current_mapping
->mode
& MODE_DIRECTORY
));
3029 if (((direntry_t
*)vvv
->directory
.pointer
)[1].attributes
!= 0xf)
3030 fprintf(stderr
, "Nonono!\n");
3032 direntry_t
* direntry
;
3033 assert(vvv
->mapping
.size
>= vvv
->mapping
.item_size
* vvv
->mapping
.next
);
3034 assert(vvv
->directory
.size
>= vvv
->directory
.item_size
* vvv
->directory
.next
);
3035 if (vvv
->mapping
.next
<47)
3037 assert((mapping
= array_get(&(vvv
->mapping
), 47)));
3038 assert(mapping
->dir_index
< vvv
->directory
.next
);
3039 direntry
= array_get(&(vvv
->directory
), mapping
->dir_index
);
3040 assert(!memcmp(direntry
->name
, "USB H ", 11) || direntry
->name
[0]==0);