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