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