2013-04-12 21:42:04 +04:00
|
|
|
/* vim:set shiftwidth=4 ts=4: */
|
2005-04-29 01:15:08 +04:00
|
|
|
/*
|
|
|
|
* QEMU Block driver for virtual VFAT (shadows a local directory)
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2005-12-18 21:29:50 +03:00
|
|
|
* Copyright (c) 2004,2005 Johannes E. Schindelin
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2005-04-29 01:15:08 +04:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2018-02-01 14:18:39 +03:00
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
2005-04-29 01:15:08 +04:00
|
|
|
#include <dirent.h>
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2018-06-14 22:14:28 +03:00
|
|
|
#include "block/qdict.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/module.h"
|
2018-02-01 14:18:46 +03:00
|
|
|
#include "qemu/option.h"
|
2016-03-15 19:22:36 +03:00
|
|
|
#include "qemu/bswap.h"
|
2017-04-06 13:00:28 +03:00
|
|
|
#include "migration/blocker.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2015-03-17 20:29:20 +03:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2019-05-23 17:35:06 +03:00
|
|
|
#include "qemu/ctype.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2017-09-11 22:52:50 +03:00
|
|
|
#include "qemu/error-report.h"
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
#ifndef S_IWGRP
|
|
|
|
#define S_IWGRP 0
|
|
|
|
#endif
|
|
|
|
#ifndef S_IWOTH
|
|
|
|
#define S_IWOTH 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* TODO: add ":bootsector=blabla.img:" */
|
|
|
|
/* LATER TODO: add automatic boot sector generation from
|
|
|
|
BOOTEASY.ASM and Ranish Partition Manager
|
2007-09-17 01:08:06 +04:00
|
|
|
Note that DOS assumes the system files to be the first files in the
|
2005-12-18 21:29:50 +03:00
|
|
|
file system (test if the boot sector still relies on that fact)! */
|
|
|
|
/* MAYBE TODO: write block-visofs.c */
|
|
|
|
/* TODO: call try_commit() only after a timeout */
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
#define DLOG(a) a
|
|
|
|
|
2008-03-09 09:59:01 +03:00
|
|
|
static void checkpoint(void);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define DLOG(a)
|
|
|
|
|
|
|
|
#endif
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2017-07-15 16:28:39 +03:00
|
|
|
/* bootsector OEM name. see related compatibility problems at:
|
|
|
|
* https://jdebp.eu/FGA/volume-boot-block-oem-name-field.html
|
|
|
|
* http://seasip.info/Misc/oemid.html
|
|
|
|
*/
|
|
|
|
#define BOOTSECTOR_OEM_NAME "MSWIN4.1"
|
|
|
|
|
2017-07-15 16:28:38 +03:00
|
|
|
#define DIR_DELETED 0xe5
|
|
|
|
#define DIR_KANJI DIR_DELETED
|
|
|
|
#define DIR_KANJI_FAKE 0x05
|
|
|
|
#define DIR_FREE 0x00
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
/* dynamic array functions */
|
2009-10-02 01:12:16 +04:00
|
|
|
typedef struct array_t {
|
2005-04-29 01:15:08 +04:00
|
|
|
char* pointer;
|
|
|
|
unsigned int size,next,item_size;
|
2009-10-02 01:12:16 +04:00
|
|
|
} array_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline void array_init(array_t* array,unsigned int item_size)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-03-07 18:32:56 +03:00
|
|
|
array->pointer = NULL;
|
2005-04-29 01:15:08 +04:00
|
|
|
array->size=0;
|
|
|
|
array->next=0;
|
|
|
|
array->item_size=item_size;
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline void array_free(array_t* array)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(array->pointer);
|
2005-04-29 01:15:08 +04:00
|
|
|
array->size=array->next=0;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* does not automatically grow */
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline void* array_get(array_t* array,unsigned int index) {
|
2005-12-18 21:29:50 +03:00
|
|
|
assert(index < array->next);
|
2018-11-06 00:38:38 +03:00
|
|
|
assert(array->pointer);
|
2005-12-18 21:29:50 +03:00
|
|
|
return array->pointer + index * array->item_size;
|
|
|
|
}
|
|
|
|
|
2018-11-06 00:38:38 +03:00
|
|
|
static inline void array_ensure_allocated(array_t *array, int index)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
if((index + 1) * array->item_size > array->size) {
|
2017-05-23 00:11:54 +03:00
|
|
|
int new_size = (index + 32) * array->item_size;
|
|
|
|
array->pointer = g_realloc(array->pointer, new_size);
|
2018-11-06 00:38:38 +03:00
|
|
|
assert(array->pointer);
|
2017-07-15 16:28:41 +03:00
|
|
|
memset(array->pointer + array->size, 0, new_size - array->size);
|
2017-05-23 00:11:54 +03:00
|
|
|
array->size = new_size;
|
|
|
|
array->next = index + 1;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline void* array_get_next(array_t* array) {
|
2005-12-18 21:29:50 +03:00
|
|
|
unsigned int next = array->next;
|
|
|
|
|
2018-11-06 00:38:38 +03:00
|
|
|
array_ensure_allocated(array, next);
|
2005-12-18 21:29:50 +03:00
|
|
|
array->next = next + 1;
|
2016-06-14 00:57:58 +03:00
|
|
|
return array_get(array, next);
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline void* array_insert(array_t* array,unsigned int index,unsigned int count) {
|
2005-04-29 01:15:08 +04:00
|
|
|
if((array->next+count)*array->item_size>array->size) {
|
2017-05-23 00:11:54 +03:00
|
|
|
int increment=count*array->item_size;
|
|
|
|
array->pointer=g_realloc(array->pointer,array->size+increment);
|
|
|
|
if(!array->pointer)
|
2009-03-07 18:32:56 +03:00
|
|
|
return NULL;
|
2017-05-23 00:11:54 +03:00
|
|
|
array->size+=increment;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
memmove(array->pointer+(index+count)*array->item_size,
|
2017-05-23 00:11:54 +03:00
|
|
|
array->pointer+index*array->item_size,
|
|
|
|
(array->next-index)*array->item_size);
|
2005-04-29 01:15:08 +04:00
|
|
|
array->next+=count;
|
|
|
|
return array->pointer+index*array->item_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this performs a "roll", so that the element which was at index_from becomes
|
|
|
|
* index_to, but the order of all other elements is preserved. */
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline int array_roll(array_t* array,int index_to,int index_from,int count)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
char* buf;
|
|
|
|
char* from;
|
|
|
|
char* to;
|
|
|
|
int is;
|
|
|
|
|
|
|
|
if(!array ||
|
2017-05-23 00:11:54 +03:00
|
|
|
index_to<0 || index_to>=array->next ||
|
|
|
|
index_from<0 || index_from>=array->next)
|
|
|
|
return -1;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
if(index_to==index_from)
|
2017-05-23 00:11:54 +03:00
|
|
|
return 0;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
is=array->item_size;
|
|
|
|
from=array->pointer+index_from*is;
|
|
|
|
to=array->pointer+index_to*is;
|
2011-08-21 07:09:37 +04:00
|
|
|
buf=g_malloc(is*count);
|
2005-04-29 01:15:08 +04:00
|
|
|
memcpy(buf,from,is*count);
|
|
|
|
|
|
|
|
if(index_to<index_from)
|
2017-05-23 00:11:54 +03:00
|
|
|
memmove(to+is*count,to,from-to);
|
2005-04-29 01:15:08 +04:00
|
|
|
else
|
2017-05-23 00:11:54 +03:00
|
|
|
memmove(from,from+is*count,to-from);
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
memcpy(to,buf,is*count);
|
|
|
|
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(buf);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline int array_remove_slice(array_t* array,int index, int count)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
assert(index >=0);
|
|
|
|
assert(count > 0);
|
|
|
|
assert(index + count <= array->next);
|
|
|
|
if(array_roll(array,array->next-1,index,count))
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2005-12-18 21:29:50 +03:00
|
|
|
array->next -= count;
|
2005-04-29 01:15:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static int array_remove(array_t* array,int index)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return array_remove_slice(array, index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the index for a given member */
|
2009-10-02 01:12:16 +04:00
|
|
|
static int array_index(array_t* array, void* pointer)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
size_t offset = (char*)pointer - array->pointer;
|
|
|
|
assert((offset % array->item_size) == 0);
|
|
|
|
assert(offset/array->item_size < array->next);
|
|
|
|
return offset/array->item_size;
|
|
|
|
}
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
/* These structures are used to fake a disk and the VFAT filesystem.
|
2011-08-31 14:38:01 +04:00
|
|
|
* For this reason we need to use QEMU_PACKED. */
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
typedef struct bootsector_t {
|
2005-04-29 01:15:08 +04:00
|
|
|
uint8_t jump[3];
|
|
|
|
uint8_t name[8];
|
|
|
|
uint16_t sector_size;
|
|
|
|
uint8_t sectors_per_cluster;
|
|
|
|
uint16_t reserved_sectors;
|
|
|
|
uint8_t number_of_fats;
|
|
|
|
uint16_t root_entries;
|
2005-12-18 21:29:50 +03:00
|
|
|
uint16_t total_sectors16;
|
2005-04-29 01:15:08 +04:00
|
|
|
uint8_t media_type;
|
|
|
|
uint16_t sectors_per_fat;
|
|
|
|
uint16_t sectors_per_track;
|
|
|
|
uint16_t number_of_heads;
|
|
|
|
uint32_t hidden_sectors;
|
|
|
|
uint32_t total_sectors;
|
|
|
|
union {
|
|
|
|
struct {
|
2017-05-23 00:11:54 +03:00
|
|
|
uint8_t drive_number;
|
2017-05-23 00:11:58 +03:00
|
|
|
uint8_t reserved1;
|
2017-05-23 00:11:54 +03:00
|
|
|
uint8_t signature;
|
|
|
|
uint32_t id;
|
|
|
|
uint8_t volume_label[11];
|
2017-05-23 00:11:58 +03:00
|
|
|
uint8_t fat_type[8];
|
|
|
|
uint8_t ignored[0x1c0];
|
2017-05-23 00:11:54 +03:00
|
|
|
} QEMU_PACKED fat16;
|
|
|
|
struct {
|
|
|
|
uint32_t sectors_per_fat;
|
|
|
|
uint16_t flags;
|
|
|
|
uint8_t major,minor;
|
2017-05-23 00:11:58 +03:00
|
|
|
uint32_t first_cluster_of_root_dir;
|
2017-05-23 00:11:54 +03:00
|
|
|
uint16_t info_sector;
|
|
|
|
uint16_t backup_boot_sector;
|
2017-05-23 00:11:58 +03:00
|
|
|
uint8_t reserved[12];
|
|
|
|
uint8_t drive_number;
|
|
|
|
uint8_t reserved1;
|
|
|
|
uint8_t signature;
|
|
|
|
uint32_t id;
|
|
|
|
uint8_t volume_label[11];
|
|
|
|
uint8_t fat_type[8];
|
|
|
|
uint8_t ignored[0x1a4];
|
2017-05-23 00:11:54 +03:00
|
|
|
} QEMU_PACKED fat32;
|
2005-04-29 01:15:08 +04:00
|
|
|
} u;
|
|
|
|
uint8_t magic[2];
|
2011-08-31 14:38:01 +04:00
|
|
|
} QEMU_PACKED bootsector_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2007-09-25 18:47:03 +04:00
|
|
|
typedef struct {
|
|
|
|
uint8_t head;
|
|
|
|
uint8_t sector;
|
|
|
|
uint8_t cylinder;
|
2009-10-02 01:12:16 +04:00
|
|
|
} mbr_chs_t;
|
2007-09-25 18:47:03 +04:00
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
typedef struct partition_t {
|
2005-04-29 01:15:08 +04:00
|
|
|
uint8_t attributes; /* 0x80 = bootable */
|
2009-10-02 01:12:16 +04:00
|
|
|
mbr_chs_t start_CHS;
|
2007-09-25 18:47:03 +04:00
|
|
|
uint8_t fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
|
2009-10-02 01:12:16 +04:00
|
|
|
mbr_chs_t end_CHS;
|
2005-04-29 01:15:08 +04:00
|
|
|
uint32_t start_sector_long;
|
2007-09-25 18:47:03 +04:00
|
|
|
uint32_t length_sector_long;
|
2011-08-31 14:38:01 +04:00
|
|
|
} QEMU_PACKED partition_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
typedef struct mbr_t {
|
2007-09-25 18:47:03 +04:00
|
|
|
uint8_t ignored[0x1b8];
|
|
|
|
uint32_t nt_id;
|
|
|
|
uint8_t ignored2[2];
|
2009-10-02 01:12:16 +04:00
|
|
|
partition_t partition[4];
|
2005-04-29 01:15:08 +04:00
|
|
|
uint8_t magic[2];
|
2011-08-31 14:38:01 +04:00
|
|
|
} QEMU_PACKED mbr_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
typedef struct direntry_t {
|
2013-12-12 00:37:11 +04:00
|
|
|
uint8_t name[8 + 3];
|
2005-04-29 01:15:08 +04:00
|
|
|
uint8_t attributes;
|
|
|
|
uint8_t reserved[2];
|
|
|
|
uint16_t ctime;
|
|
|
|
uint16_t cdate;
|
|
|
|
uint16_t adate;
|
|
|
|
uint16_t begin_hi;
|
|
|
|
uint16_t mtime;
|
|
|
|
uint16_t mdate;
|
|
|
|
uint16_t begin;
|
|
|
|
uint32_t size;
|
2011-08-31 14:38:01 +04:00
|
|
|
} QEMU_PACKED direntry_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
/* this structure are used to transparently access the files */
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
typedef struct mapping_t {
|
2005-12-18 21:29:50 +03:00
|
|
|
/* begin is the first cluster, end is the last+1 */
|
|
|
|
uint32_t begin,end;
|
2005-04-29 01:15:08 +04:00
|
|
|
/* as s->directory is growable, no pointer may be used here */
|
|
|
|
unsigned int dir_index;
|
2005-12-18 21:29:50 +03:00
|
|
|
/* the clusters of a file may be in any order; this points to the first */
|
|
|
|
int first_mapping_index;
|
|
|
|
union {
|
2017-05-23 00:11:54 +03:00
|
|
|
/* offset is
|
|
|
|
* - the offset in the file (in clusters) for a file, or
|
2017-05-23 00:11:56 +03:00
|
|
|
* - the next cluster of the directory for a directory
|
2017-05-23 00:11:54 +03:00
|
|
|
*/
|
|
|
|
struct {
|
|
|
|
uint32_t offset;
|
|
|
|
} file;
|
|
|
|
struct {
|
|
|
|
int parent_mapping_index;
|
|
|
|
int first_dir_index;
|
|
|
|
} dir;
|
2005-12-18 21:29:50 +03:00
|
|
|
} info;
|
|
|
|
/* path contains the full path, i.e. it always starts with s->path */
|
|
|
|
char* path;
|
|
|
|
|
2017-05-23 00:11:56 +03:00
|
|
|
enum {
|
|
|
|
MODE_UNDEFINED = 0,
|
|
|
|
MODE_NORMAL = 1,
|
|
|
|
MODE_MODIFIED = 2,
|
|
|
|
MODE_DIRECTORY = 4,
|
|
|
|
MODE_DELETED = 8,
|
|
|
|
} mode;
|
2005-12-18 21:29:50 +03:00
|
|
|
int read_only;
|
2009-10-02 01:12:16 +04:00
|
|
|
} mapping_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
#ifdef DEBUG
|
2009-10-02 01:12:16 +04:00
|
|
|
static void print_direntry(const struct direntry_t*);
|
|
|
|
static void print_mapping(const struct mapping_t* mapping);
|
2005-12-18 21:29:50 +03:00
|
|
|
#endif
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
/* here begins the real VVFAT driver */
|
|
|
|
|
|
|
|
typedef struct BDRVVVFATState {
|
2011-10-20 15:16:21 +04:00
|
|
|
CoMutex lock;
|
2005-12-18 21:29:50 +03:00
|
|
|
BlockDriverState* bs; /* pointer to parent */
|
2005-04-29 01:15:08 +04:00
|
|
|
unsigned char first_sectors[0x40*0x200];
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
int fat_type; /* 16 or 32 */
|
2009-10-02 01:12:16 +04:00
|
|
|
array_t fat,directory,mapping;
|
2015-06-19 12:35:29 +03:00
|
|
|
char volume_label[11];
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2017-05-23 00:11:57 +03:00
|
|
|
uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
unsigned int cluster_size;
|
|
|
|
unsigned int sectors_per_cluster;
|
|
|
|
unsigned int sectors_per_fat;
|
2005-12-18 21:29:50 +03:00
|
|
|
uint32_t last_cluster_of_root_directory;
|
2017-05-23 00:12:03 +03:00
|
|
|
/* how many entries are available in root directory (0 for FAT32) */
|
|
|
|
uint16_t root_entries;
|
2005-04-29 01:15:08 +04:00
|
|
|
uint32_t sector_count; /* total number of sectors of the partition */
|
|
|
|
uint32_t cluster_count; /* total number of clusters of this partition */
|
|
|
|
uint32_t max_fat_value;
|
2017-05-23 00:11:57 +03:00
|
|
|
uint32_t offset_to_fat;
|
|
|
|
uint32_t offset_to_root_dir;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
int current_fd;
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* current_mapping;
|
2005-12-18 21:29:50 +03:00
|
|
|
unsigned char* cluster; /* points to current cluster */
|
|
|
|
unsigned char* cluster_buffer; /* points to a buffer to hold temp data */
|
2005-04-29 01:15:08 +04:00
|
|
|
unsigned int current_cluster;
|
|
|
|
|
|
|
|
/* write support */
|
2005-12-18 21:29:50 +03:00
|
|
|
char* qcow_filename;
|
2016-05-30 18:13:09 +03:00
|
|
|
BdrvChild* qcow;
|
2005-12-18 21:29:50 +03:00
|
|
|
void* fat2;
|
|
|
|
char* used_clusters;
|
2009-10-02 01:12:16 +04:00
|
|
|
array_t commits;
|
2005-12-18 21:29:50 +03:00
|
|
|
const char* path;
|
|
|
|
int downcase_short_names;
|
2011-11-22 19:52:13 +04:00
|
|
|
|
|
|
|
Error *migration_blocker;
|
2005-04-29 01:15:08 +04:00
|
|
|
} BDRVVVFATState;
|
|
|
|
|
2007-09-25 18:47:03 +04:00
|
|
|
/* take the sector position spos and convert it to Cylinder/Head/Sector position
|
|
|
|
* if the position is outside the specified geometry, fill maximum value for CHS
|
|
|
|
* and return 1 to signal overflow.
|
|
|
|
*/
|
vvfat: Do not clobber the user's geometry
vvfat creates a virtual VFAT filesystem with a certain logical
geometry that depends on its options. It sets the "geometry hint" to
this geometry. It is the only block driver to do this.
The geometry hint is about about *physical* geometry, and used only by
certain hard disk device models.
vvfat's hint is normally invisible for device models, because
bdrv_open() puts a raw format on top of vvfat's fat protocol. That
raw format is where drive_init() puts the user's geometry (if any),
and where the device model gets it from.
Nobody complained, because the default physical geometry is the same
as vvfat's logical geometry:
opts LCHS def. PCHS
1024,16,63 same
:32: 1024,16,63 same
:16: 1024,16,63 same
:12: 64,16,63 same
Except when you specify :floppy:
opts LCHS def. PCHS
:floppy: 80, 2,36 5,16,63
:32:floppy: 80, 2,36 5,16,63
:16:floppy: 80, 2,36 5,16,63
:12:floppy: 80, 2,18 2,16,63
Silly thing to do for use with a hard disk.
However, the "raw" format can be suppressed by adding an
redundant-looking "format=vvfat" to "file=fat:FOO". Then, vvfat's
hint clobbers the user's geometry, i.e. -drive options cyls, heads,
secs get silently ignored. Don't do that.
No change without format=vvfat. With it, the user's hard disk
geometry (-drive options cyls, heads, secs) is now obeyed, and the
default hard disk geometry with :floppy: now matches the one without
format=vvfat.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:29 +04:00
|
|
|
static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
|
|
|
|
{
|
2007-09-25 18:47:03 +04:00
|
|
|
int head,sector;
|
vvfat: Do not clobber the user's geometry
vvfat creates a virtual VFAT filesystem with a certain logical
geometry that depends on its options. It sets the "geometry hint" to
this geometry. It is the only block driver to do this.
The geometry hint is about about *physical* geometry, and used only by
certain hard disk device models.
vvfat's hint is normally invisible for device models, because
bdrv_open() puts a raw format on top of vvfat's fat protocol. That
raw format is where drive_init() puts the user's geometry (if any),
and where the device model gets it from.
Nobody complained, because the default physical geometry is the same
as vvfat's logical geometry:
opts LCHS def. PCHS
1024,16,63 same
:32: 1024,16,63 same
:16: 1024,16,63 same
:12: 64,16,63 same
Except when you specify :floppy:
opts LCHS def. PCHS
:floppy: 80, 2,36 5,16,63
:32:floppy: 80, 2,36 5,16,63
:16:floppy: 80, 2,36 5,16,63
:12:floppy: 80, 2,18 2,16,63
Silly thing to do for use with a hard disk.
However, the "raw" format can be suppressed by adding an
redundant-looking "format=vvfat" to "file=fat:FOO". Then, vvfat's
hint clobbers the user's geometry, i.e. -drive options cyls, heads,
secs get silently ignored. Don't do that.
No change without format=vvfat. With it, the user's hard disk
geometry (-drive options cyls, heads, secs) is now obeyed, and the
default hard disk geometry with :floppy: now matches the one without
format=vvfat.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:29 +04:00
|
|
|
sector = spos % secs; spos /= secs;
|
|
|
|
head = spos % heads; spos /= heads;
|
|
|
|
if (spos >= cyls) {
|
2007-09-25 18:47:03 +04:00
|
|
|
/* Overflow,
|
|
|
|
it happens if 32bit sector positions are used, while CHS is only 24bit.
|
|
|
|
Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
|
|
|
|
chs->head = 0xFF;
|
|
|
|
chs->sector = 0xFF;
|
|
|
|
chs->cylinder = 0xFF;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
chs->head = (uint8_t)head;
|
|
|
|
chs->sector = (uint8_t)( (sector+1) | ((spos>>8)<<6) );
|
|
|
|
chs->cylinder = (uint8_t)spos;
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
vvfat: Do not clobber the user's geometry
vvfat creates a virtual VFAT filesystem with a certain logical
geometry that depends on its options. It sets the "geometry hint" to
this geometry. It is the only block driver to do this.
The geometry hint is about about *physical* geometry, and used only by
certain hard disk device models.
vvfat's hint is normally invisible for device models, because
bdrv_open() puts a raw format on top of vvfat's fat protocol. That
raw format is where drive_init() puts the user's geometry (if any),
and where the device model gets it from.
Nobody complained, because the default physical geometry is the same
as vvfat's logical geometry:
opts LCHS def. PCHS
1024,16,63 same
:32: 1024,16,63 same
:16: 1024,16,63 same
:12: 64,16,63 same
Except when you specify :floppy:
opts LCHS def. PCHS
:floppy: 80, 2,36 5,16,63
:32:floppy: 80, 2,36 5,16,63
:16:floppy: 80, 2,36 5,16,63
:12:floppy: 80, 2,18 2,16,63
Silly thing to do for use with a hard disk.
However, the "raw" format can be suppressed by adding an
redundant-looking "format=vvfat" to "file=fat:FOO". Then, vvfat's
hint clobbers the user's geometry, i.e. -drive options cyls, heads,
secs get silently ignored. Don't do that.
No change without format=vvfat. With it, the user's hard disk
geometry (-drive options cyls, heads, secs) is now obeyed, and the
default hard disk geometry with :floppy: now matches the one without
format=vvfat.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:29 +04:00
|
|
|
static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
/* TODO: if the files mbr.img and bootsect.img exist, use them */
|
2009-10-02 01:12:16 +04:00
|
|
|
mbr_t* real_mbr=(mbr_t*)s->first_sectors;
|
|
|
|
partition_t* partition = &(real_mbr->partition[0]);
|
2007-09-25 18:47:03 +04:00
|
|
|
int lba;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
memset(s->first_sectors,0,512);
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2007-09-25 18:47:03 +04:00
|
|
|
/* Win NT Disk Signature */
|
|
|
|
real_mbr->nt_id= cpu_to_le32(0xbe1afdfa);
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
partition->attributes=0x80; /* bootable */
|
2007-09-25 18:47:03 +04:00
|
|
|
|
|
|
|
/* LBA is used when partition is outside the CHS geometry */
|
2017-05-23 00:11:57 +03:00
|
|
|
lba = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
|
vvfat: Do not clobber the user's geometry
vvfat creates a virtual VFAT filesystem with a certain logical
geometry that depends on its options. It sets the "geometry hint" to
this geometry. It is the only block driver to do this.
The geometry hint is about about *physical* geometry, and used only by
certain hard disk device models.
vvfat's hint is normally invisible for device models, because
bdrv_open() puts a raw format on top of vvfat's fat protocol. That
raw format is where drive_init() puts the user's geometry (if any),
and where the device model gets it from.
Nobody complained, because the default physical geometry is the same
as vvfat's logical geometry:
opts LCHS def. PCHS
1024,16,63 same
:32: 1024,16,63 same
:16: 1024,16,63 same
:12: 64,16,63 same
Except when you specify :floppy:
opts LCHS def. PCHS
:floppy: 80, 2,36 5,16,63
:32:floppy: 80, 2,36 5,16,63
:16:floppy: 80, 2,36 5,16,63
:12:floppy: 80, 2,18 2,16,63
Silly thing to do for use with a hard disk.
However, the "raw" format can be suppressed by adding an
redundant-looking "format=vvfat" to "file=fat:FOO". Then, vvfat's
hint clobbers the user's geometry, i.e. -drive options cyls, heads,
secs get silently ignored. Don't do that.
No change without format=vvfat. With it, the user's hard disk
geometry (-drive options cyls, heads, secs) is now obeyed, and the
default hard disk geometry with :floppy: now matches the one without
format=vvfat.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:29 +04:00
|
|
|
cyls, heads, secs);
|
|
|
|
lba |= sector2CHS(&partition->end_CHS, s->bs->total_sectors - 1,
|
|
|
|
cyls, heads, secs);
|
2007-09-25 18:47:03 +04:00
|
|
|
|
|
|
|
/*LBA partitions are identified only by start/length_sector_long not by CHS*/
|
2017-05-23 00:11:57 +03:00
|
|
|
partition->start_sector_long = cpu_to_le32(s->offset_to_bootsector);
|
vvfat: Fix partition table
Unless parameter ":floppy:" is given, vvfat creates a virtual image
with DOS MBR defining a single partition which holds the FAT file
system. The size of the virtual image depends on the width of the
FAT: 32 MiB (CHS 64, 16, 63) for 12 bit FAT, 504 MiB (CHS 1024, 16,
63) for 16 and 32 bit FAT, leaving (64*16-1)*63 = 64449 and
(1024*16-1)*64 = 1032129 sectors for the partition.
However, it screws up the end of the partition in the MBR:
FAT width param. start CHS end CHS start LBA size
:32: 0,1,1 1023,14,63 63 1032065
:16: 0,1,1 1023,14,55 63 1032057
:12: 0,1,1 63,14,55 63 64377
The actual FAT file system nevertheless assumes the partition has
1032129 or 64449 sectors. Oops.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:28 +04:00
|
|
|
partition->length_sector_long = cpu_to_le32(s->bs->total_sectors
|
2017-05-23 00:11:57 +03:00
|
|
|
- s->offset_to_bootsector);
|
2007-09-25 18:47:03 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* FAT12/FAT16/FAT32 */
|
2007-09-25 18:47:03 +04:00
|
|
|
/* DOS uses different types when partition is LBA,
|
|
|
|
probably to prevent older versions from using CHS on them */
|
2017-05-23 00:11:55 +03:00
|
|
|
partition->fs_type = s->fat_type == 12 ? 0x1 :
|
|
|
|
s->fat_type == 16 ? (lba ? 0xe : 0x06) :
|
|
|
|
/*s->fat_type == 32*/ (lba ? 0xc : 0x0b);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
real_mbr->magic[0]=0x55; real_mbr->magic[1]=0xaa;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* direntry functions */
|
|
|
|
|
2017-05-23 00:12:00 +03:00
|
|
|
static direntry_t *create_long_filename(BDRVVVFATState *s, const char *filename)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2017-05-23 00:12:00 +03:00
|
|
|
int number_of_entries, i;
|
|
|
|
glong length;
|
|
|
|
direntry_t *entry;
|
|
|
|
|
|
|
|
gunichar2 *longname = g_utf8_to_utf16(filename, -1, NULL, &length, NULL);
|
|
|
|
if (!longname) {
|
|
|
|
fprintf(stderr, "vvfat: invalid UTF-8 name: %s\n", filename);
|
|
|
|
return NULL;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:04:16 +03:00
|
|
|
number_of_entries = DIV_ROUND_UP(length * 2, 26);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
for(i=0;i<number_of_entries;i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
entry=array_get_next(&(s->directory));
|
|
|
|
entry->attributes=0xf;
|
|
|
|
entry->reserved[0]=0;
|
|
|
|
entry->begin=0;
|
|
|
|
entry->name[0]=(number_of_entries-i)|(i==0?0x40:0);
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2007-12-24 16:26:04 +03:00
|
|
|
for(i=0;i<26*number_of_entries;i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
int offset=(i%26);
|
|
|
|
if(offset<10) offset=1+offset;
|
|
|
|
else if(offset<22) offset=14+offset-10;
|
|
|
|
else offset=28+offset-22;
|
|
|
|
entry=array_get(&(s->directory),s->directory.next-1-(i/26));
|
2017-05-23 00:12:00 +03:00
|
|
|
if (i >= 2 * length + 2) {
|
|
|
|
entry->name[offset] = 0xff;
|
|
|
|
} else if (i % 2 == 0) {
|
|
|
|
entry->name[offset] = longname[i / 2] & 0xff;
|
|
|
|
} else {
|
|
|
|
entry->name[offset] = longname[i / 2] >> 8;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2017-05-23 00:12:00 +03:00
|
|
|
g_free(longname);
|
2005-04-29 01:15:08 +04:00
|
|
|
return array_get(&(s->directory),s->directory.next-number_of_entries);
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static char is_free(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
2017-07-15 16:28:38 +03:00
|
|
|
return direntry->name[0] == DIR_DELETED || direntry->name[0] == DIR_FREE;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static char is_volume_label(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return direntry->attributes == 0x28;
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static char is_long_name(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return direntry->attributes == 0xf;
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static char is_short_name(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return !is_volume_label(direntry) && !is_long_name(direntry)
|
2017-05-23 00:11:54 +03:00
|
|
|
&& !is_free(direntry);
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static char is_directory(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
2017-07-15 16:28:38 +03:00
|
|
|
return direntry->attributes & 0x10 && direntry->name[0] != DIR_DELETED;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline char is_dot(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return is_short_name(direntry) && direntry->name[0] == '.';
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static char is_file(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return is_short_name(direntry) && !is_directory(direntry);
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline uint32_t begin_of_direntry(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return le16_to_cpu(direntry->begin)|(le16_to_cpu(direntry->begin_hi)<<16);
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline uint32_t filesize_of_direntry(const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
return le32_to_cpu(direntry->size);
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
direntry->begin = cpu_to_le16(begin & 0xffff);
|
|
|
|
direntry->begin_hi = cpu_to_le16((begin >> 16) & 0xffff);
|
|
|
|
}
|
|
|
|
|
2017-05-23 00:12:01 +03:00
|
|
|
static uint8_t to_valid_short_char(gunichar c)
|
|
|
|
{
|
|
|
|
c = g_unichar_toupper(c);
|
|
|
|
if ((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
strchr("$%'-_@~`!(){}^#&", c) != 0) {
|
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static direntry_t *create_short_filename(BDRVVVFATState *s,
|
2017-05-23 00:12:02 +03:00
|
|
|
const char *filename,
|
|
|
|
unsigned int directory_start)
|
2017-05-23 00:12:01 +03:00
|
|
|
{
|
2017-05-23 00:12:02 +03:00
|
|
|
int i, j = 0;
|
2017-05-23 00:12:01 +03:00
|
|
|
direntry_t *entry = array_get_next(&(s->directory));
|
|
|
|
const gchar *p, *last_dot = NULL;
|
|
|
|
gunichar c;
|
|
|
|
bool lossy_conversion = false;
|
2017-07-17 18:12:07 +03:00
|
|
|
char tail[8];
|
2017-05-23 00:12:01 +03:00
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(entry->name, 0x20, sizeof(entry->name));
|
|
|
|
|
|
|
|
/* copy filename and search last dot */
|
|
|
|
for (p = filename; ; p = g_utf8_next_char(p)) {
|
|
|
|
c = g_utf8_get_char(p);
|
|
|
|
if (c == '\0') {
|
|
|
|
break;
|
|
|
|
} else if (c == '.') {
|
|
|
|
if (j == 0) {
|
|
|
|
/* '.' at start of filename */
|
|
|
|
lossy_conversion = true;
|
|
|
|
} else {
|
|
|
|
if (last_dot) {
|
|
|
|
lossy_conversion = true;
|
|
|
|
}
|
|
|
|
last_dot = p;
|
|
|
|
}
|
|
|
|
} else if (!last_dot) {
|
|
|
|
/* first part of the name; copy it */
|
|
|
|
uint8_t v = to_valid_short_char(c);
|
|
|
|
if (j < 8 && v) {
|
|
|
|
entry->name[j++] = v;
|
|
|
|
} else {
|
|
|
|
lossy_conversion = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy extension (if any) */
|
|
|
|
if (last_dot) {
|
|
|
|
j = 0;
|
|
|
|
for (p = g_utf8_next_char(last_dot); ; p = g_utf8_next_char(p)) {
|
|
|
|
c = g_utf8_get_char(p);
|
|
|
|
if (c == '\0') {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* extension; copy it */
|
|
|
|
uint8_t v = to_valid_short_char(c);
|
|
|
|
if (j < 3 && v) {
|
|
|
|
entry->name[8 + (j++)] = v;
|
|
|
|
} else {
|
|
|
|
lossy_conversion = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 00:12:02 +03:00
|
|
|
|
2017-07-15 16:28:38 +03:00
|
|
|
if (entry->name[0] == DIR_KANJI) {
|
|
|
|
entry->name[0] = DIR_KANJI_FAKE;
|
2017-05-23 00:12:04 +03:00
|
|
|
}
|
|
|
|
|
2017-05-23 00:12:02 +03:00
|
|
|
/* numeric-tail generation */
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (entry->name[j] == ' ') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
|
|
|
|
direntry_t *entry1;
|
|
|
|
if (i > 0) {
|
2017-07-17 18:12:07 +03:00
|
|
|
int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
|
|
|
|
assert(len <= 7);
|
2017-05-23 00:12:02 +03:00
|
|
|
memcpy(entry->name + MIN(j, 8 - len), tail, len);
|
|
|
|
}
|
|
|
|
for (entry1 = array_get(&(s->directory), directory_start);
|
|
|
|
entry1 < entry; entry1++) {
|
|
|
|
if (!is_long_name(entry1) &&
|
|
|
|
!memcmp(entry1->name, entry->name, 11)) {
|
|
|
|
break; /* found dupe */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entry1 == entry) {
|
|
|
|
/* no dupe found */
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2017-05-23 00:12:01 +03:00
|
|
|
}
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
/* fat functions */
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline uint8_t fat_chksum(const direntry_t* entry)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
uint8_t chksum=0;
|
|
|
|
int i;
|
|
|
|
|
2013-12-12 00:37:11 +04:00
|
|
|
for (i = 0; i < ARRAY_SIZE(entry->name); i++) {
|
|
|
|
chksum = (((chksum & 0xfe) >> 1) |
|
|
|
|
((chksum & 0x01) ? 0x80 : 0)) + entry->name[i];
|
2009-04-25 02:08:05 +04:00
|
|
|
}
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
return chksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if return_time==0, this returns the fat_date, else the fat_time */
|
|
|
|
static uint16_t fat_datetime(time_t time,int return_time) {
|
|
|
|
struct tm* t;
|
|
|
|
struct tm t1;
|
2009-09-30 21:43:31 +04:00
|
|
|
t = &t1;
|
2005-04-29 01:15:08 +04:00
|
|
|
localtime_r(&time,t);
|
|
|
|
if(return_time)
|
2017-05-23 00:11:54 +03:00
|
|
|
return cpu_to_le16((t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11));
|
2005-04-29 01:15:08 +04:00
|
|
|
return cpu_to_le16((t->tm_mday)|((t->tm_mon+1)<<5)|((t->tm_year-80)<<9));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fat_set(BDRVVVFATState* s,unsigned int cluster,uint32_t value)
|
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
if(s->fat_type==32) {
|
2017-05-23 00:11:54 +03:00
|
|
|
uint32_t* entry=array_get(&(s->fat),cluster);
|
|
|
|
*entry=cpu_to_le32(value);
|
2005-04-29 01:15:08 +04:00
|
|
|
} else if(s->fat_type==16) {
|
2017-05-23 00:11:54 +03:00
|
|
|
uint16_t* entry=array_get(&(s->fat),cluster);
|
|
|
|
*entry=cpu_to_le16(value&0xffff);
|
2005-04-29 01:15:08 +04:00
|
|
|
} else {
|
2017-05-23 00:11:54 +03:00
|
|
|
int offset = (cluster*3/2);
|
|
|
|
unsigned char* p = array_get(&(s->fat), offset);
|
2005-12-18 21:29:50 +03:00
|
|
|
switch (cluster&1) {
|
2017-05-23 00:11:54 +03:00
|
|
|
case 0:
|
|
|
|
p[0] = value&0xff;
|
|
|
|
p[1] = (p[1]&0xf0) | ((value>>8)&0xf);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
p[0] = (p[0]&0xf) | ((value&0xf)<<4);
|
|
|
|
p[1] = (value>>4);
|
|
|
|
break;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t fat_get(BDRVVVFATState* s,unsigned int cluster)
|
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
if(s->fat_type==32) {
|
2017-05-23 00:11:54 +03:00
|
|
|
uint32_t* entry=array_get(&(s->fat),cluster);
|
|
|
|
return le32_to_cpu(*entry);
|
2005-04-29 01:15:08 +04:00
|
|
|
} else if(s->fat_type==16) {
|
2017-05-23 00:11:54 +03:00
|
|
|
uint16_t* entry=array_get(&(s->fat),cluster);
|
|
|
|
return le16_to_cpu(*entry);
|
2005-04-29 01:15:08 +04:00
|
|
|
} else {
|
2017-05-23 00:11:54 +03:00
|
|
|
const uint8_t* x=(uint8_t*)(s->fat.pointer)+cluster*3/2;
|
|
|
|
return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fat_eof(BDRVVVFATState* s,uint32_t fat_entry)
|
|
|
|
{
|
|
|
|
if(fat_entry>s->max_fat_value-8)
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2005-04-29 01:15:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void init_fat(BDRVVVFATState* s)
|
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
if (s->fat_type == 12) {
|
2017-05-23 00:11:54 +03:00
|
|
|
array_init(&(s->fat),1);
|
|
|
|
array_ensure_allocated(&(s->fat),
|
|
|
|
s->sectors_per_fat * 0x200 * 3 / 2 - 1);
|
2005-12-18 21:29:50 +03:00
|
|
|
} else {
|
2017-05-23 00:11:54 +03:00
|
|
|
array_init(&(s->fat),(s->fat_type==32?4:2));
|
|
|
|
array_ensure_allocated(&(s->fat),
|
|
|
|
s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
memset(s->fat.pointer,0,s->fat.size);
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
switch(s->fat_type) {
|
2017-05-23 00:11:54 +03:00
|
|
|
case 12: s->max_fat_value=0xfff; break;
|
|
|
|
case 16: s->max_fat_value=0xffff; break;
|
|
|
|
case 32: s->max_fat_value=0x0fffffff; break;
|
|
|
|
default: s->max_fat_value=0; /* error... */
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline direntry_t* create_short_and_long_name(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
unsigned int directory_start, const char* filename, int is_dot)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2017-05-23 00:12:01 +03:00
|
|
|
int long_index = s->directory.next;
|
2009-10-02 01:12:16 +04:00
|
|
|
direntry_t* entry = NULL;
|
|
|
|
direntry_t* entry_long = NULL;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
if(is_dot) {
|
2017-05-23 00:11:54 +03:00
|
|
|
entry=array_get_next(&(s->directory));
|
2013-12-12 00:37:11 +04:00
|
|
|
memset(entry->name, 0x20, sizeof(entry->name));
|
2017-05-23 00:11:54 +03:00
|
|
|
memcpy(entry->name,filename,strlen(filename));
|
|
|
|
return entry;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
entry_long=create_long_filename(s,filename);
|
2017-05-23 00:12:02 +03:00
|
|
|
entry = create_short_filename(s, filename, directory_start);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
/* calculate checksum; propagate to long name */
|
|
|
|
if(entry_long) {
|
|
|
|
uint8_t chksum=fat_chksum(entry);
|
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
/* calculate anew, because realloc could have taken place */
|
|
|
|
entry_long=array_get(&(s->directory),long_index);
|
|
|
|
while(entry_long<entry && is_long_name(entry_long)) {
|
|
|
|
entry_long->reserved[1]=chksum;
|
|
|
|
entry_long++;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
|
|
|
* Read a directory. (the index of the corresponding mapping must be passed).
|
|
|
|
*/
|
|
|
|
static int read_directory(BDRVVVFATState* s, int mapping_index)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping = array_get(&(s->mapping), mapping_index);
|
|
|
|
direntry_t* direntry;
|
2005-12-18 21:29:50 +03:00
|
|
|
const char* dirname = mapping->path;
|
|
|
|
int first_cluster = mapping->begin;
|
|
|
|
int parent_index = mapping->info.dir.parent_mapping_index;
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* parent_mapping = (mapping_t*)
|
2009-03-07 18:32:56 +03:00
|
|
|
(parent_index >= 0 ? array_get(&(s->mapping), parent_index) : NULL);
|
2005-12-18 21:29:50 +03:00
|
|
|
int first_cluster_of_parent = parent_mapping ? parent_mapping->begin : -1;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
DIR* dir=opendir(dirname);
|
|
|
|
struct dirent* entry;
|
|
|
|
int i;
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
assert(mapping->mode & MODE_DIRECTORY);
|
|
|
|
|
|
|
|
if(!dir) {
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping->end = mapping->begin;
|
|
|
|
return -1;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
i = mapping->info.dir.first_dir_index =
|
2017-05-23 00:11:54 +03:00
|
|
|
first_cluster == 0 ? 0 : s->directory.next;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:59 +03:00
|
|
|
if (first_cluster != 0) {
|
|
|
|
/* create the top entries of a subdirectory */
|
|
|
|
(void)create_short_and_long_name(s, i, ".", 1);
|
|
|
|
(void)create_short_and_long_name(s, i, "..", 1);
|
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
/* actually read the directory, and allocate the mappings */
|
2005-04-29 01:15:08 +04:00
|
|
|
while((entry=readdir(dir))) {
|
2017-05-23 00:11:54 +03:00
|
|
|
unsigned int length=strlen(dirname)+2+strlen(entry->d_name);
|
2005-04-29 01:15:08 +04:00
|
|
|
char* buffer;
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry_t* direntry;
|
2005-12-18 21:29:50 +03:00
|
|
|
struct stat st;
|
2017-05-23 00:11:54 +03:00
|
|
|
int is_dot=!strcmp(entry->d_name,".");
|
|
|
|
int is_dotdot=!strcmp(entry->d_name,"..");
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2017-05-23 00:12:03 +03:00
|
|
|
if (first_cluster == 0 && s->directory.next >= s->root_entries - 1) {
|
|
|
|
fprintf(stderr, "Too many entries in root directory\n");
|
|
|
|
closedir(dir);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if(first_cluster == 0 && (is_dotdot || is_dot))
|
|
|
|
continue;
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
buffer = g_malloc(length);
|
|
|
|
snprintf(buffer,length,"%s/%s",dirname,entry->d_name);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if(stat(buffer,&st)<0) {
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(buffer);
|
2005-04-29 01:15:08 +04:00
|
|
|
continue;
|
2017-05-23 00:11:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create directory entry for this file */
|
2017-05-23 00:11:59 +03:00
|
|
|
if (!is_dot && !is_dotdot) {
|
|
|
|
direntry = create_short_and_long_name(s, i, entry->d_name, 0);
|
|
|
|
} else {
|
|
|
|
direntry = array_get(&(s->directory), is_dot ? i : i + 1);
|
|
|
|
}
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry->attributes=(S_ISDIR(st.st_mode)?0x10:0x20);
|
|
|
|
direntry->reserved[0]=direntry->reserved[1]=0;
|
|
|
|
direntry->ctime=fat_datetime(st.st_ctime,1);
|
|
|
|
direntry->cdate=fat_datetime(st.st_ctime,0);
|
|
|
|
direntry->adate=fat_datetime(st.st_atime,0);
|
|
|
|
direntry->begin_hi=0;
|
|
|
|
direntry->mtime=fat_datetime(st.st_mtime,1);
|
|
|
|
direntry->mdate=fat_datetime(st.st_mtime,0);
|
|
|
|
if(is_dotdot)
|
|
|
|
set_begin_of_direntry(direntry, first_cluster_of_parent);
|
|
|
|
else if(is_dot)
|
|
|
|
set_begin_of_direntry(direntry, first_cluster);
|
|
|
|
else
|
|
|
|
direntry->begin=0; /* do that later */
|
2005-12-18 21:29:50 +03:00
|
|
|
if (st.st_size > 0x7fffffff) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "File %s is larger than 2GB\n", buffer);
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(buffer);
|
2011-01-12 22:48:58 +03:00
|
|
|
closedir(dir);
|
2017-05-23 00:11:54 +03:00
|
|
|
return -2;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry->size=cpu_to_le32(S_ISDIR(st.st_mode)?0:st.st_size);
|
|
|
|
|
|
|
|
/* create mapping for this file */
|
|
|
|
if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
|
|
|
|
s->current_mapping = array_get_next(&(s->mapping));
|
|
|
|
s->current_mapping->begin=0;
|
|
|
|
s->current_mapping->end=st.st_size;
|
|
|
|
/*
|
|
|
|
* we get the direntry of the most recent direntry, which
|
|
|
|
* contains the short name and all the relevant information.
|
|
|
|
*/
|
|
|
|
s->current_mapping->dir_index=s->directory.next-1;
|
|
|
|
s->current_mapping->first_mapping_index = -1;
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
s->current_mapping->mode = MODE_DIRECTORY;
|
|
|
|
s->current_mapping->info.dir.parent_mapping_index =
|
|
|
|
mapping_index;
|
|
|
|
} else {
|
|
|
|
s->current_mapping->mode = MODE_UNDEFINED;
|
|
|
|
s->current_mapping->info.file.offset = 0;
|
|
|
|
}
|
|
|
|
s->current_mapping->path=buffer;
|
|
|
|
s->current_mapping->read_only =
|
|
|
|
(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0;
|
2014-05-28 13:17:05 +04:00
|
|
|
} else {
|
|
|
|
g_free(buffer);
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
/* fill with zeroes up to the end of the cluster */
|
|
|
|
while(s->directory.next%(0x10*s->sectors_per_cluster)) {
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry_t* direntry=array_get_next(&(s->directory));
|
|
|
|
memset(direntry,0,sizeof(direntry_t));
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2017-05-23 00:12:03 +03:00
|
|
|
if (s->fat_type != 32 &&
|
|
|
|
mapping_index == 0 &&
|
|
|
|
s->directory.next < s->root_entries) {
|
2017-05-23 00:11:54 +03:00
|
|
|
/* root directory */
|
|
|
|
int cur = s->directory.next;
|
2017-05-23 00:12:03 +03:00
|
|
|
array_ensure_allocated(&(s->directory), s->root_entries - 1);
|
|
|
|
s->directory.next = s->root_entries;
|
2017-05-23 00:11:54 +03:00
|
|
|
memset(array_get(&(s->directory), cur), 0,
|
2017-05-23 00:12:03 +03:00
|
|
|
(s->root_entries - cur) * sizeof(direntry_t));
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2007-09-17 01:08:06 +04:00
|
|
|
|
2017-05-23 00:11:55 +03:00
|
|
|
/* re-get the mapping, since s->mapping was possibly realloc()ed */
|
2014-08-19 12:31:11 +04:00
|
|
|
mapping = array_get(&(s->mapping), mapping_index);
|
2005-12-18 21:29:50 +03:00
|
|
|
first_cluster += (s->directory.next - mapping->info.dir.first_dir_index)
|
2017-05-23 00:11:54 +03:00
|
|
|
* 0x20 / s->cluster_size;
|
2005-12-18 21:29:50 +03:00
|
|
|
mapping->end = first_cluster;
|
|
|
|
|
2014-08-19 12:31:11 +04:00
|
|
|
direntry = array_get(&(s->directory), mapping->dir_index);
|
2005-12-18 21:29:50 +03:00
|
|
|
set_begin_of_direntry(direntry, mapping->begin);
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static inline uint32_t sector2cluster(BDRVVVFATState* s,off_t sector_num)
|
|
|
|
{
|
2017-05-23 00:11:57 +03:00
|
|
|
return (sector_num - s->offset_to_root_dir) / s->sectors_per_cluster;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
|
|
|
|
{
|
2017-05-23 00:11:57 +03:00
|
|
|
return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static int init_directories(BDRVVVFATState* s,
|
2014-05-16 13:00:18 +04:00
|
|
|
const char *dirname, int heads, int secs,
|
|
|
|
Error **errp)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
bootsector_t* bootsector;
|
|
|
|
mapping_t* mapping;
|
2005-04-29 01:15:08 +04:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int cluster;
|
|
|
|
|
|
|
|
memset(&(s->first_sectors[0]),0,0x40*0x200);
|
|
|
|
|
|
|
|
s->cluster_size=s->sectors_per_cluster*0x200;
|
2011-08-21 07:09:37 +04:00
|
|
|
s->cluster_buffer=g_malloc(s->cluster_size);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The formula: sc = spf+1+spf*spc*(512*8/fat_type),
|
|
|
|
* where sc is sector_count,
|
|
|
|
* spf is sectors_per_fat,
|
|
|
|
* spc is sectors_per_clusters, and
|
|
|
|
* fat_type = 12, 16 or 32.
|
|
|
|
*/
|
|
|
|
i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
|
|
|
|
s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2017-05-23 00:11:57 +03:00
|
|
|
s->offset_to_fat = s->offset_to_bootsector + 1;
|
|
|
|
s->offset_to_root_dir = s->offset_to_fat + s->sectors_per_fat * 2;
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
array_init(&(s->mapping),sizeof(mapping_t));
|
|
|
|
array_init(&(s->directory),sizeof(direntry_t));
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
/* add volume label */
|
|
|
|
{
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry_t* entry=array_get_next(&(s->directory));
|
|
|
|
entry->attributes=0x28; /* archive | volume label */
|
2015-06-19 12:35:29 +03:00
|
|
|
memcpy(entry->name, s->volume_label, sizeof(entry->name));
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now build FAT, and write back information into directory */
|
|
|
|
init_fat(s);
|
|
|
|
|
2017-05-23 00:12:03 +03:00
|
|
|
/* TODO: if there are more entries, bootsector has to be adjusted! */
|
|
|
|
s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
|
2005-12-18 21:29:50 +03:00
|
|
|
s->cluster_count=sector2cluster(s, s->sector_count);
|
|
|
|
|
|
|
|
mapping = array_get_next(&(s->mapping));
|
|
|
|
mapping->begin = 0;
|
|
|
|
mapping->dir_index = 0;
|
|
|
|
mapping->info.dir.parent_mapping_index = -1;
|
|
|
|
mapping->first_mapping_index = -1;
|
2011-08-21 07:09:37 +04:00
|
|
|
mapping->path = g_strdup(dirname);
|
2005-12-18 21:29:50 +03:00
|
|
|
i = strlen(mapping->path);
|
|
|
|
if (i > 0 && mapping->path[i - 1] == '/')
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping->path[i - 1] = '\0';
|
2005-12-18 21:29:50 +03:00
|
|
|
mapping->mode = MODE_DIRECTORY;
|
|
|
|
mapping->read_only = 0;
|
|
|
|
s->path = mapping->path;
|
|
|
|
|
|
|
|
for (i = 0, cluster = 0; i < s->mapping.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
/* MS-DOS expects the FAT to be 0 for the root directory
|
|
|
|
* (except for the media byte). */
|
|
|
|
/* LATER TODO: still true for FAT32? */
|
|
|
|
int fix_fat = (i != 0);
|
|
|
|
mapping = array_get(&(s->mapping), i);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
if (mapping->mode & MODE_DIRECTORY) {
|
2018-07-24 14:52:04 +03:00
|
|
|
char *path = mapping->path;
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping->begin = cluster;
|
|
|
|
if(read_directory(s, i)) {
|
2018-07-24 14:52:04 +03:00
|
|
|
error_setg(errp, "Could not read directory %s", path);
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mapping = array_get(&(s->mapping), i);
|
|
|
|
} else {
|
|
|
|
assert(mapping->mode == MODE_UNDEFINED);
|
|
|
|
mapping->mode=MODE_NORMAL;
|
|
|
|
mapping->begin = cluster;
|
|
|
|
if (mapping->end > 0) {
|
|
|
|
direntry_t* direntry = array_get(&(s->directory),
|
|
|
|
mapping->dir_index);
|
|
|
|
|
|
|
|
mapping->end = cluster + 1 + (mapping->end-1)/s->cluster_size;
|
|
|
|
set_begin_of_direntry(direntry, mapping->begin);
|
|
|
|
} else {
|
|
|
|
mapping->end = cluster + 1;
|
|
|
|
fix_fat = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(mapping->begin < mapping->end);
|
|
|
|
|
|
|
|
/* next free cluster */
|
|
|
|
cluster = mapping->end;
|
|
|
|
|
|
|
|
if(cluster > s->cluster_count) {
|
2014-05-16 13:00:18 +04:00
|
|
|
error_setg(errp,
|
|
|
|
"Directory does not fit in FAT%d (capacity %.2f MB)",
|
|
|
|
s->fat_type, s->sector_count / 2000.0);
|
|
|
|
return -1;
|
2017-05-23 00:11:54 +03:00
|
|
|
}
|
2008-11-10 04:34:27 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
/* fix fat for entry */
|
|
|
|
if (fix_fat) {
|
|
|
|
int j;
|
|
|
|
for(j = mapping->begin; j < mapping->end - 1; j++)
|
|
|
|
fat_set(s, j, j+1);
|
|
|
|
fat_set(s, mapping->end - 1, s->max_fat_value);
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
mapping = array_get(&(s->mapping), 0);
|
|
|
|
s->last_cluster_of_root_directory = mapping->end;
|
|
|
|
|
|
|
|
/* the FAT signature */
|
|
|
|
fat_set(s,0,s->max_fat_value);
|
|
|
|
fat_set(s,1,s->max_fat_value);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
s->current_mapping = NULL;
|
|
|
|
|
2017-05-23 00:11:57 +03:00
|
|
|
bootsector = (bootsector_t *)(s->first_sectors
|
|
|
|
+ s->offset_to_bootsector * 0x200);
|
2005-04-29 01:15:08 +04:00
|
|
|
bootsector->jump[0]=0xeb;
|
|
|
|
bootsector->jump[1]=0x3e;
|
|
|
|
bootsector->jump[2]=0x90;
|
2017-07-15 16:28:39 +03:00
|
|
|
memcpy(bootsector->name, BOOTSECTOR_OEM_NAME, 8);
|
2005-04-29 01:15:08 +04:00
|
|
|
bootsector->sector_size=cpu_to_le16(0x200);
|
|
|
|
bootsector->sectors_per_cluster=s->sectors_per_cluster;
|
|
|
|
bootsector->reserved_sectors=cpu_to_le16(1);
|
|
|
|
bootsector->number_of_fats=0x2; /* number of FATs */
|
2017-05-23 00:12:03 +03:00
|
|
|
bootsector->root_entries = cpu_to_le16(s->root_entries);
|
2005-12-18 21:29:50 +03:00
|
|
|
bootsector->total_sectors16=s->sector_count>0xffff?0:cpu_to_le16(s->sector_count);
|
2017-05-23 00:11:57 +03:00
|
|
|
/* media descriptor: hard disk=0xf8, floppy=0xf0 */
|
|
|
|
bootsector->media_type = (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0);
|
2005-12-18 21:29:50 +03:00
|
|
|
s->fat.pointer[0] = bootsector->media_type;
|
2005-04-29 01:15:08 +04:00
|
|
|
bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
|
vvfat: Do not clobber the user's geometry
vvfat creates a virtual VFAT filesystem with a certain logical
geometry that depends on its options. It sets the "geometry hint" to
this geometry. It is the only block driver to do this.
The geometry hint is about about *physical* geometry, and used only by
certain hard disk device models.
vvfat's hint is normally invisible for device models, because
bdrv_open() puts a raw format on top of vvfat's fat protocol. That
raw format is where drive_init() puts the user's geometry (if any),
and where the device model gets it from.
Nobody complained, because the default physical geometry is the same
as vvfat's logical geometry:
opts LCHS def. PCHS
1024,16,63 same
:32: 1024,16,63 same
:16: 1024,16,63 same
:12: 64,16,63 same
Except when you specify :floppy:
opts LCHS def. PCHS
:floppy: 80, 2,36 5,16,63
:32:floppy: 80, 2,36 5,16,63
:16:floppy: 80, 2,36 5,16,63
:12:floppy: 80, 2,18 2,16,63
Silly thing to do for use with a hard disk.
However, the "raw" format can be suppressed by adding an
redundant-looking "format=vvfat" to "file=fat:FOO". Then, vvfat's
hint clobbers the user's geometry, i.e. -drive options cyls, heads,
secs get silently ignored. Don't do that.
No change without format=vvfat. With it, the user's hard disk
geometry (-drive options cyls, heads, secs) is now obeyed, and the
default hard disk geometry with :floppy: now matches the one without
format=vvfat.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:29 +04:00
|
|
|
bootsector->sectors_per_track = cpu_to_le16(secs);
|
|
|
|
bootsector->number_of_heads = cpu_to_le16(heads);
|
2017-05-23 00:11:57 +03:00
|
|
|
bootsector->hidden_sectors = cpu_to_le32(s->offset_to_bootsector);
|
2005-12-18 21:29:50 +03:00
|
|
|
bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* LATER TODO: if FAT32, this is wrong */
|
2017-05-23 00:11:57 +03:00
|
|
|
/* drive_number: fda=0, hda=0x80 */
|
|
|
|
bootsector->u.fat16.drive_number = s->offset_to_bootsector == 0 ? 0 : 0x80;
|
2005-04-29 01:15:08 +04:00
|
|
|
bootsector->u.fat16.signature=0x29;
|
|
|
|
bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);
|
|
|
|
|
2015-06-19 12:35:29 +03:00
|
|
|
memcpy(bootsector->u.fat16.volume_label, s->volume_label,
|
|
|
|
sizeof(bootsector->u.fat16.volume_label));
|
2017-05-23 00:11:58 +03:00
|
|
|
memcpy(bootsector->u.fat16.fat_type,
|
|
|
|
s->fat_type == 12 ? "FAT12 " : "FAT16 ", 8);
|
2005-04-29 01:15:08 +04:00
|
|
|
bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-01 20:21:11 +04:00
|
|
|
#ifdef DEBUG
|
2005-12-18 21:29:50 +03:00
|
|
|
static BDRVVVFATState *vvv = NULL;
|
2006-08-01 20:21:11 +04:00
|
|
|
#endif
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2016-05-30 18:13:09 +03:00
|
|
|
static int enable_write_target(BlockDriverState *bs, Error **errp);
|
2005-12-18 21:29:50 +03:00
|
|
|
static int is_consistent(BDRVVVFATState *s);
|
|
|
|
|
2013-04-12 21:42:04 +04:00
|
|
|
static QemuOptsList runtime_opts = {
|
|
|
|
.name = "vvfat",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = "dir",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "Host directory to map to the vvfat device",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "fat-type",
|
|
|
|
.type = QEMU_OPT_NUMBER,
|
|
|
|
.help = "FAT type (12, 16 or 32)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "floppy",
|
|
|
|
.type = QEMU_OPT_BOOL,
|
|
|
|
.help = "Create a floppy rather than a hard disk image",
|
|
|
|
},
|
2015-06-19 12:35:29 +03:00
|
|
|
{
|
|
|
|
.name = "label",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "Use a volume label other than QEMU VVFAT",
|
|
|
|
},
|
2013-04-12 21:42:04 +04:00
|
|
|
{
|
|
|
|
.name = "rw",
|
|
|
|
.type = QEMU_OPT_BOOL,
|
|
|
|
.help = "Make the image writable",
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void vvfat_parse_filename(const char *filename, QDict *options,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
int fat_type = 0;
|
|
|
|
bool floppy = false;
|
|
|
|
bool rw = false;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!strstart(filename, "fat:", NULL)) {
|
|
|
|
error_setg(errp, "File name string must start with 'fat:'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse options */
|
|
|
|
if (strstr(filename, ":32:")) {
|
|
|
|
fat_type = 32;
|
|
|
|
} else if (strstr(filename, ":16:")) {
|
|
|
|
fat_type = 16;
|
|
|
|
} else if (strstr(filename, ":12:")) {
|
|
|
|
fat_type = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(filename, ":floppy:")) {
|
|
|
|
floppy = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(filename, ":rw:")) {
|
|
|
|
rw = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the directory name without options */
|
|
|
|
i = strrchr(filename, ':') - filename;
|
|
|
|
assert(i >= 3);
|
|
|
|
if (filename[i - 2] == ':' && qemu_isalpha(filename[i - 1])) {
|
|
|
|
/* workaround for DOS drive names */
|
|
|
|
filename += i - 1;
|
|
|
|
} else {
|
|
|
|
filename += i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the options QDict */
|
2017-04-28 00:58:17 +03:00
|
|
|
qdict_put_str(options, "dir", filename);
|
|
|
|
qdict_put_int(options, "fat-type", fat_type);
|
|
|
|
qdict_put_bool(options, "floppy", floppy);
|
|
|
|
qdict_put_bool(options, "rw", rw);
|
2013-04-12 21:42:04 +04:00
|
|
|
}
|
|
|
|
|
2013-09-05 16:22:29 +04:00
|
|
|
static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
BDRVVVFATState *s = bs->opaque;
|
2013-04-12 21:42:04 +04:00
|
|
|
int cyls, heads, secs;
|
|
|
|
bool floppy;
|
2015-06-19 12:35:29 +03:00
|
|
|
const char *dirname, *label;
|
2013-04-12 21:42:04 +04:00
|
|
|
QemuOpts *opts;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2006-08-01 20:21:11 +04:00
|
|
|
#ifdef DEBUG
|
2005-12-18 21:29:50 +03:00
|
|
|
vvv = s;
|
2006-08-01 20:21:11 +04:00
|
|
|
#endif
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2014-01-02 06:49:17 +04:00
|
|
|
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
2013-04-12 21:42:04 +04:00
|
|
|
qemu_opts_absorb_qdict(opts, options, &local_err);
|
2014-01-30 18:07:28 +04:00
|
|
|
if (local_err) {
|
2014-02-17 17:44:01 +04:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-12 21:42:04 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirname = qemu_opt_get(opts, "dir");
|
|
|
|
if (!dirname) {
|
2014-02-17 17:44:01 +04:00
|
|
|
error_setg(errp, "vvfat block driver requires a 'dir' option");
|
2013-04-12 21:42:04 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
|
|
|
|
floppy = qemu_opt_get_bool(opts, "floppy", false);
|
|
|
|
|
2015-06-19 12:35:29 +03:00
|
|
|
memset(s->volume_label, ' ', sizeof(s->volume_label));
|
|
|
|
label = qemu_opt_get(opts, "label");
|
|
|
|
if (label) {
|
|
|
|
size_t label_length = strlen(label);
|
|
|
|
if (label_length > 11) {
|
|
|
|
error_setg(errp, "vvfat label cannot be longer than 11 bytes");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
memcpy(s->volume_label, label, label_length);
|
2016-04-27 15:18:16 +03:00
|
|
|
} else {
|
|
|
|
memcpy(s->volume_label, "QEMU VVFAT", 10);
|
2015-06-19 12:35:29 +03:00
|
|
|
}
|
|
|
|
|
2013-04-12 21:42:04 +04:00
|
|
|
if (floppy) {
|
|
|
|
/* 1.44MB or 2.88MB floppy. 2.88MB can be FAT12 (default) or FAT16. */
|
|
|
|
if (!s->fat_type) {
|
|
|
|
s->fat_type = 12;
|
|
|
|
secs = 36;
|
|
|
|
s->sectors_per_cluster = 2;
|
|
|
|
} else {
|
|
|
|
secs = s->fat_type == 12 ? 18 : 36;
|
|
|
|
s->sectors_per_cluster = 1;
|
|
|
|
}
|
|
|
|
cyls = 80;
|
|
|
|
heads = 2;
|
|
|
|
} else {
|
|
|
|
/* 32MB or 504MB disk*/
|
|
|
|
if (!s->fat_type) {
|
|
|
|
s->fat_type = 16;
|
|
|
|
}
|
2017-05-23 00:11:57 +03:00
|
|
|
s->offset_to_bootsector = 0x3f;
|
2013-04-12 21:42:04 +04:00
|
|
|
cyls = s->fat_type == 12 ? 64 : 1024;
|
|
|
|
heads = 16;
|
|
|
|
secs = 63;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->fat_type) {
|
|
|
|
case 32:
|
2017-09-11 22:52:56 +03:00
|
|
|
warn_report("FAT32 has not been tested. You are welcome to do so!");
|
2013-04-12 21:42:04 +04:00
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
case 12:
|
|
|
|
break;
|
|
|
|
default:
|
2014-02-17 17:44:01 +04:00
|
|
|
error_setg(errp, "Valid FAT types are only 12, 16 and 32");
|
2013-04-12 21:42:04 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
s->bs = bs;
|
|
|
|
|
|
|
|
/* LATER TODO: if FAT32, adjust */
|
|
|
|
s->sectors_per_cluster=0x10;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
s->current_cluster=0xffffffff;
|
|
|
|
|
2016-05-30 18:13:09 +03:00
|
|
|
s->qcow = NULL;
|
2005-12-18 21:29:50 +03:00
|
|
|
s->qcow_filename = NULL;
|
|
|
|
s->fat2 = NULL;
|
|
|
|
s->downcase_short_names = 1;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2018-07-18 18:08:29 +03:00
|
|
|
DLOG(fprintf(stderr, "vvfat %s chs %d,%d,%d\n",
|
|
|
|
dirname, cyls, heads, secs));
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:57 +03:00
|
|
|
s->sector_count = cyls * heads * secs - s->offset_to_bootsector;
|
2011-10-05 11:12:06 +04:00
|
|
|
|
2013-04-12 21:42:04 +04:00
|
|
|
if (qemu_opt_get_bool(opts, "rw", false)) {
|
block: do not set BDS read_only if copy_on_read enabled
A few block drivers will set the BDS read_only flag from their
.bdrv_open() function. This means the bs->read_only flag could
be set after we enable copy_on_read, as the BDRV_O_COPY_ON_READ
flag check occurs prior to the call to bdrv->bdrv_open().
This adds an error return to bdrv_set_read_only(), and an error will be
return if we try to set the BDS to read_only while copy_on_read is
enabled.
This patch also changes the behavior of vvfat. Before, vvfat could
override the drive 'readonly' flag with its own, internal 'rw' flag.
For instance, this -drive parameter would result in a writable image:
"-drive format=vvfat,dir=/tmp/vvfat,rw,if=virtio,readonly=on"
This is not correct. Now, attempting to use the above -drive parameter
will result in an error (i.e., 'rw' is incompatible with 'readonly=on').
Signed-off-by: Jeff Cody <jcody@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
Message-id: 0c5b4c1cc2c651471b131f21376dfd5ea24d2196.1491597120.git.jcody@redhat.com
2017-04-07 23:55:26 +03:00
|
|
|
if (!bdrv_is_read_only(bs)) {
|
|
|
|
ret = enable_write_target(bs, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = -EPERM;
|
|
|
|
error_setg(errp,
|
|
|
|
"Unable to set VVFAT to 'rw' when drive is read-only");
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-10-12 12:27:41 +03:00
|
|
|
} else {
|
|
|
|
ret = bdrv_apply_auto_read_only(bs, NULL, errp);
|
2013-07-17 13:57:37 +04:00
|
|
|
if (ret < 0) {
|
2013-04-12 21:42:04 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
2007-09-25 18:47:03 +04:00
|
|
|
}
|
|
|
|
|
vvfat: Do not clobber the user's geometry
vvfat creates a virtual VFAT filesystem with a certain logical
geometry that depends on its options. It sets the "geometry hint" to
this geometry. It is the only block driver to do this.
The geometry hint is about about *physical* geometry, and used only by
certain hard disk device models.
vvfat's hint is normally invisible for device models, because
bdrv_open() puts a raw format on top of vvfat's fat protocol. That
raw format is where drive_init() puts the user's geometry (if any),
and where the device model gets it from.
Nobody complained, because the default physical geometry is the same
as vvfat's logical geometry:
opts LCHS def. PCHS
1024,16,63 same
:32: 1024,16,63 same
:16: 1024,16,63 same
:12: 64,16,63 same
Except when you specify :floppy:
opts LCHS def. PCHS
:floppy: 80, 2,36 5,16,63
:32:floppy: 80, 2,36 5,16,63
:16:floppy: 80, 2,36 5,16,63
:12:floppy: 80, 2,18 2,16,63
Silly thing to do for use with a hard disk.
However, the "raw" format can be suppressed by adding an
redundant-looking "format=vvfat" to "file=fat:FOO". Then, vvfat's
hint clobbers the user's geometry, i.e. -drive options cyls, heads,
secs get silently ignored. Don't do that.
No change without format=vvfat. With it, the user's hard disk
geometry (-drive options cyls, heads, secs) is now obeyed, and the
default hard disk geometry with :floppy: now matches the one without
format=vvfat.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:29 +04:00
|
|
|
bs->total_sectors = cyls * heads * secs;
|
2007-09-25 18:47:03 +04:00
|
|
|
|
2014-05-16 13:00:18 +04:00
|
|
|
if (init_directories(s, dirname, heads, secs, errp)) {
|
2013-04-12 21:42:04 +04:00
|
|
|
ret = -EIO;
|
|
|
|
goto fail;
|
vvfat: Do not clobber the user's geometry
vvfat creates a virtual VFAT filesystem with a certain logical
geometry that depends on its options. It sets the "geometry hint" to
this geometry. It is the only block driver to do this.
The geometry hint is about about *physical* geometry, and used only by
certain hard disk device models.
vvfat's hint is normally invisible for device models, because
bdrv_open() puts a raw format on top of vvfat's fat protocol. That
raw format is where drive_init() puts the user's geometry (if any),
and where the device model gets it from.
Nobody complained, because the default physical geometry is the same
as vvfat's logical geometry:
opts LCHS def. PCHS
1024,16,63 same
:32: 1024,16,63 same
:16: 1024,16,63 same
:12: 64,16,63 same
Except when you specify :floppy:
opts LCHS def. PCHS
:floppy: 80, 2,36 5,16,63
:32:floppy: 80, 2,36 5,16,63
:16:floppy: 80, 2,36 5,16,63
:12:floppy: 80, 2,18 2,16,63
Silly thing to do for use with a hard disk.
However, the "raw" format can be suppressed by adding an
redundant-looking "format=vvfat" to "file=fat:FOO". Then, vvfat's
hint clobbers the user's geometry, i.e. -drive options cyls, heads,
secs get silently ignored. Don't do that.
No change without format=vvfat. With it, the user's hard disk
geometry (-drive options cyls, heads, secs) is now obeyed, and the
default hard disk geometry with :floppy: now matches the one without
format=vvfat.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2012-07-10 13:12:29 +04:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2017-05-23 00:11:57 +03:00
|
|
|
s->sector_count = s->offset_to_root_dir
|
|
|
|
+ s->sectors_per_cluster * s->cluster_count;
|
2007-09-25 18:47:03 +04:00
|
|
|
|
2011-11-22 19:52:13 +04:00
|
|
|
/* Disable migration when vvfat is used rw */
|
|
|
|
if (s->qcow) {
|
2015-04-08 12:29:19 +03:00
|
|
|
error_setg(&s->migration_blocker,
|
|
|
|
"The vvfat (rw) format used by node '%s' "
|
|
|
|
"does not support live migration",
|
|
|
|
bdrv_get_device_or_node_name(bs));
|
2017-01-16 14:31:53 +03:00
|
|
|
ret = migrate_add_blocker(s->migration_blocker, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
error_free(s->migration_blocker);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-11-22 19:52:13 +04:00
|
|
|
}
|
|
|
|
|
2017-05-23 00:11:57 +03:00
|
|
|
if (s->offset_to_bootsector > 0) {
|
2017-01-16 14:31:53 +03:00
|
|
|
init_mbr(s, cyls, heads, secs);
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_co_mutex_init(&s->lock);
|
|
|
|
|
2013-04-12 21:42:04 +04:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
qemu_opts_del(opts);
|
|
|
|
return ret;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2016-06-24 01:37:17 +03:00
|
|
|
static void vvfat_refresh_limits(BlockDriverState *bs, Error **errp)
|
|
|
|
{
|
2016-06-24 01:37:24 +03:00
|
|
|
bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
|
2016-06-24 01:37:17 +03:00
|
|
|
}
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
static inline void vvfat_close_current_file(BDRVVVFATState *s)
|
|
|
|
{
|
|
|
|
if(s->current_mapping) {
|
2017-05-23 00:11:54 +03:00
|
|
|
s->current_mapping = NULL;
|
|
|
|
if (s->current_fd) {
|
|
|
|
qemu_close(s->current_fd);
|
|
|
|
s->current_fd = 0;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
s->current_cluster = -1;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mappings between index1 and index2-1 are supposed to be ordered
|
|
|
|
* return value is the index of the last mapping for which end>cluster_num
|
|
|
|
*/
|
|
|
|
static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
|
|
|
|
{
|
|
|
|
while(1) {
|
2010-04-25 19:27:14 +04:00
|
|
|
int index3;
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping_t* mapping;
|
|
|
|
index3=(index1+index2)/2;
|
|
|
|
mapping=array_get(&(s->mapping),index3);
|
|
|
|
assert(mapping->begin < mapping->end);
|
|
|
|
if(mapping->begin>=cluster_num) {
|
|
|
|
assert(index2!=index3 || index2==0);
|
|
|
|
if(index2==index3)
|
|
|
|
return index1;
|
|
|
|
index2=index3;
|
|
|
|
} else {
|
|
|
|
if(index1==index3)
|
|
|
|
return mapping->end<=cluster_num ? index2 : index1;
|
|
|
|
index1=index3;
|
|
|
|
}
|
|
|
|
assert(index1<=index2);
|
|
|
|
DLOG(mapping=array_get(&(s->mapping),index1);
|
|
|
|
assert(mapping->begin<=cluster_num);
|
|
|
|
assert(index2 >= s->mapping.next ||
|
|
|
|
((mapping = array_get(&(s->mapping),index2)) &&
|
|
|
|
mapping->end>cluster_num)));
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static inline mapping_t* find_mapping_for_cluster(BDRVVVFATState* s,int cluster_num)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
int index=find_mapping_for_cluster_aux(s,cluster_num,0,s->mapping.next);
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping;
|
2005-04-29 01:15:08 +04:00
|
|
|
if(index>=s->mapping.next)
|
2009-03-07 18:32:56 +03:00
|
|
|
return NULL;
|
2005-04-29 01:15:08 +04:00
|
|
|
mapping=array_get(&(s->mapping),index);
|
|
|
|
if(mapping->begin>cluster_num)
|
2009-03-07 18:32:56 +03:00
|
|
|
return NULL;
|
2005-12-18 21:29:50 +03:00
|
|
|
assert(mapping->begin<=cluster_num && mapping->end>cluster_num);
|
2005-04-29 01:15:08 +04:00
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static int open_file(BDRVVVFATState* s,mapping_t* mapping)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
if(!mapping)
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2005-04-29 01:15:08 +04:00
|
|
|
if(!s->current_mapping ||
|
2017-05-23 00:11:54 +03:00
|
|
|
strcmp(s->current_mapping->path,mapping->path)) {
|
|
|
|
/* open file */
|
|
|
|
int fd = qemu_open(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE);
|
|
|
|
if(fd<0)
|
|
|
|
return -1;
|
|
|
|
vvfat_close_current_file(s);
|
|
|
|
s->current_fd = fd;
|
|
|
|
s->current_mapping = mapping;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int read_cluster(BDRVVVFATState *s,int cluster_num)
|
|
|
|
{
|
|
|
|
if(s->current_cluster != cluster_num) {
|
2017-05-23 00:11:54 +03:00
|
|
|
int result=0;
|
|
|
|
off_t offset;
|
|
|
|
assert(!s->current_mapping || s->current_fd || (s->current_mapping->mode & MODE_DIRECTORY));
|
|
|
|
if(!s->current_mapping
|
|
|
|
|| s->current_mapping->begin>cluster_num
|
|
|
|
|| s->current_mapping->end<=cluster_num) {
|
|
|
|
/* binary search of mappings for file */
|
|
|
|
mapping_t* mapping=find_mapping_for_cluster(s,cluster_num);
|
|
|
|
|
|
|
|
assert(!mapping || (cluster_num>=mapping->begin && cluster_num<mapping->end));
|
|
|
|
|
|
|
|
if (mapping && mapping->mode & MODE_DIRECTORY) {
|
|
|
|
vvfat_close_current_file(s);
|
|
|
|
s->current_mapping = mapping;
|
2005-12-18 21:29:50 +03:00
|
|
|
read_cluster_directory:
|
2017-05-23 00:11:54 +03:00
|
|
|
offset = s->cluster_size*(cluster_num-s->current_mapping->begin);
|
|
|
|
s->cluster = (unsigned char*)s->directory.pointer+offset
|
|
|
|
+ 0x20*s->current_mapping->info.dir.first_dir_index;
|
|
|
|
assert(((s->cluster-(unsigned char*)s->directory.pointer)%s->cluster_size)==0);
|
|
|
|
assert((char*)s->cluster+s->cluster_size <= s->directory.pointer+s->directory.next*s->directory.item_size);
|
|
|
|
s->current_cluster = cluster_num;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(open_file(s,mapping))
|
|
|
|
return -2;
|
|
|
|
} else if (s->current_mapping->mode & MODE_DIRECTORY)
|
|
|
|
goto read_cluster_directory;
|
|
|
|
|
|
|
|
assert(s->current_fd);
|
|
|
|
|
|
|
|
offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset;
|
|
|
|
if(lseek(s->current_fd, offset, SEEK_SET)!=offset)
|
|
|
|
return -3;
|
|
|
|
s->cluster=s->cluster_buffer;
|
|
|
|
result=read(s->current_fd,s->cluster,s->cluster_size);
|
|
|
|
if(result<0) {
|
|
|
|
s->current_cluster = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s->current_cluster = cluster_num;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
#ifdef DEBUG
|
2009-10-02 01:12:16 +04:00
|
|
|
static void print_direntry(const direntry_t* direntry)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
int j = 0;
|
|
|
|
char buffer[1024];
|
|
|
|
|
2010-05-20 12:34:50 +04:00
|
|
|
fprintf(stderr, "direntry %p: ", direntry);
|
2005-04-29 01:15:08 +04:00
|
|
|
if(!direntry)
|
2017-05-23 00:11:54 +03:00
|
|
|
return;
|
2005-12-18 21:29:50 +03:00
|
|
|
if(is_long_name(direntry)) {
|
2017-05-23 00:11:54 +03:00
|
|
|
unsigned char* c=(unsigned char*)direntry;
|
|
|
|
int i;
|
|
|
|
for(i=1;i<11 && c[i] && c[i]!=0xff;i+=2)
|
2008-12-14 12:30:41 +03:00
|
|
|
#define ADD_CHAR(c) {buffer[j] = (c); if (buffer[j] < ' ') buffer[j] = 0xb0; j++;}
|
2017-05-23 00:11:54 +03:00
|
|
|
ADD_CHAR(c[i]);
|
|
|
|
for(i=14;i<26 && c[i] && c[i]!=0xff;i+=2)
|
|
|
|
ADD_CHAR(c[i]);
|
|
|
|
for(i=28;i<32 && c[i] && c[i]!=0xff;i+=2)
|
|
|
|
ADD_CHAR(c[i]);
|
|
|
|
buffer[j] = 0;
|
|
|
|
fprintf(stderr, "%s\n", buffer);
|
2005-04-29 01:15:08 +04:00
|
|
|
} else {
|
2017-05-23 00:11:54 +03:00
|
|
|
int i;
|
|
|
|
for(i=0;i<11;i++)
|
|
|
|
ADD_CHAR(direntry->name[i]);
|
|
|
|
buffer[j] = 0;
|
|
|
|
fprintf(stderr,"%s attributes=0x%02x begin=%d size=%d\n",
|
|
|
|
buffer,
|
|
|
|
direntry->attributes,
|
|
|
|
begin_of_direntry(direntry),le32_to_cpu(direntry->size));
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static void print_mapping(const mapping_t* mapping)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2010-05-20 12:34:50 +04:00
|
|
|
fprintf(stderr, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
|
|
|
|
"first_mapping_index = %d, name = %s, mode = 0x%x, " ,
|
|
|
|
mapping, mapping->begin, mapping->end, mapping->dir_index,
|
|
|
|
mapping->first_mapping_index, mapping->path, mapping->mode);
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
if (mapping->mode & MODE_DIRECTORY)
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "parent_mapping_index = %d, first_dir_index = %d\n", mapping->info.dir.parent_mapping_index, mapping->info.dir.first_dir_index);
|
2005-12-18 21:29:50 +03:00
|
|
|
else
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "offset = %d\n", mapping->info.file.offset);
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
#endif
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
static int vvfat_read(BlockDriverState *bs, int64_t sector_num,
|
2005-12-18 21:29:50 +03:00
|
|
|
uint8_t *buf, int nb_sectors)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
BDRVVVFATState *s = bs->opaque;
|
2005-04-29 01:15:08 +04:00
|
|
|
int i;
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
for(i=0;i<nb_sectors;i++,sector_num++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
if (sector_num >= bs->total_sectors)
|
|
|
|
return -1;
|
|
|
|
if (s->qcow) {
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
int64_t n;
|
2017-03-09 00:34:28 +03:00
|
|
|
int ret;
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
ret = bdrv_is_allocated(s->qcow->bs, sector_num * BDRV_SECTOR_SIZE,
|
|
|
|
(nb_sectors - i) * BDRV_SECTOR_SIZE, &n);
|
2017-03-09 00:34:28 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (ret) {
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
|
|
|
|
" allocated\n", sector_num,
|
|
|
|
n >> BDRV_SECTOR_BITS));
|
2019-05-01 21:13:57 +03:00
|
|
|
if (bdrv_pread(s->qcow, sector_num * BDRV_SECTOR_SIZE,
|
|
|
|
buf + i * 0x200, n) < 0) {
|
2011-11-08 13:50:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
i += (n >> BDRV_SECTOR_BITS) - 1;
|
|
|
|
sector_num += (n >> BDRV_SECTOR_BITS) - 1;
|
2011-11-08 13:50:12 +04:00
|
|
|
continue;
|
|
|
|
}
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n",
|
|
|
|
sector_num));
|
2017-05-23 00:11:54 +03:00
|
|
|
}
|
2017-05-23 00:11:57 +03:00
|
|
|
if (sector_num < s->offset_to_root_dir) {
|
|
|
|
if (sector_num < s->offset_to_fat) {
|
|
|
|
memcpy(buf + i * 0x200,
|
|
|
|
&(s->first_sectors[sector_num * 0x200]),
|
|
|
|
0x200);
|
|
|
|
} else if (sector_num < s->offset_to_fat + s->sectors_per_fat) {
|
|
|
|
memcpy(buf + i * 0x200,
|
|
|
|
&(s->fat.pointer[(sector_num
|
|
|
|
- s->offset_to_fat) * 0x200]),
|
|
|
|
0x200);
|
|
|
|
} else if (sector_num < s->offset_to_root_dir) {
|
|
|
|
memcpy(buf + i * 0x200,
|
|
|
|
&(s->fat.pointer[(sector_num - s->offset_to_fat
|
|
|
|
- s->sectors_per_fat) * 0x200]),
|
|
|
|
0x200);
|
|
|
|
}
|
2017-05-23 00:11:54 +03:00
|
|
|
} else {
|
2017-05-23 00:11:57 +03:00
|
|
|
uint32_t sector = sector_num - s->offset_to_root_dir,
|
2017-05-23 00:11:54 +03:00
|
|
|
sector_offset_in_cluster=(sector%s->sectors_per_cluster),
|
|
|
|
cluster_num=sector/s->sectors_per_cluster;
|
|
|
|
if(cluster_num > s->cluster_count || read_cluster(s, cluster_num) != 0) {
|
|
|
|
/* LATER TODO: strict: return -1; */
|
|
|
|
memset(buf+i*0x200,0,0x200);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:14:08 +03:00
|
|
|
static int coroutine_fn
|
|
|
|
vvfat_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags)
|
2011-10-20 15:16:22 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
BDRVVVFATState *s = bs->opaque;
|
2016-04-26 18:14:08 +03:00
|
|
|
uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
|
|
|
|
int nb_sectors = bytes >> BDRV_SECTOR_BITS;
|
|
|
|
void *buf;
|
|
|
|
|
2019-08-27 21:59:12 +03:00
|
|
|
assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
|
|
|
|
assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
|
2016-04-26 18:14:08 +03:00
|
|
|
|
|
|
|
buf = g_try_malloc(bytes);
|
|
|
|
if (bytes && buf == NULL) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2011-10-20 15:16:22 +04:00
|
|
|
qemu_co_mutex_lock(&s->lock);
|
|
|
|
ret = vvfat_read(bs, sector_num, buf, nb_sectors);
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
2016-04-26 18:14:08 +03:00
|
|
|
|
|
|
|
qemu_iovec_from_buf(qiov, 0, buf, bytes);
|
|
|
|
g_free(buf);
|
|
|
|
|
2011-10-20 15:16:22 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* LATER TODO: statify all functions */
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
|
|
|
* Idea of the write support (use snapshot):
|
2005-04-29 01:15:08 +04:00
|
|
|
*
|
2005-12-18 21:29:50 +03:00
|
|
|
* 1. check if all data is consistent, recording renames, modifications,
|
|
|
|
* new files and directories (in s->commits).
|
2005-04-29 01:15:08 +04:00
|
|
|
*
|
2005-12-18 21:29:50 +03:00
|
|
|
* 2. if the data is not consistent, stop committing
|
2005-04-29 01:15:08 +04:00
|
|
|
*
|
2005-12-18 21:29:50 +03:00
|
|
|
* 3. handle renames, and create new files and directories (do not yet
|
|
|
|
* write their contents)
|
2005-04-29 01:15:08 +04:00
|
|
|
*
|
2005-12-18 21:29:50 +03:00
|
|
|
* 4. walk the directories, fixing the mapping and direntries, and marking
|
|
|
|
* the handled mappings as not deleted
|
2005-04-29 01:15:08 +04:00
|
|
|
*
|
2005-12-18 21:29:50 +03:00
|
|
|
* 5. commit the contents of the files
|
2005-04-29 01:15:08 +04:00
|
|
|
*
|
2005-12-18 21:29:50 +03:00
|
|
|
* 6. handle deleted files and directories
|
2005-04-29 01:15:08 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
typedef struct commit_t {
|
2005-12-18 21:29:50 +03:00
|
|
|
char* path;
|
|
|
|
union {
|
2017-05-23 00:11:54 +03:00
|
|
|
struct { uint32_t cluster; } rename;
|
|
|
|
struct { int dir_index; uint32_t modified_offset; } writeout;
|
|
|
|
struct { uint32_t first_cluster; } new_file;
|
|
|
|
struct { uint32_t cluster; } mkdir;
|
2005-12-18 21:29:50 +03:00
|
|
|
} param;
|
|
|
|
/* DELETEs and RMDIRs are handled differently: see handle_deletes() */
|
|
|
|
enum {
|
2017-05-23 00:11:54 +03:00
|
|
|
ACTION_RENAME, ACTION_WRITEOUT, ACTION_NEW_FILE, ACTION_MKDIR
|
2005-12-18 21:29:50 +03:00
|
|
|
} action;
|
2009-10-02 01:12:16 +04:00
|
|
|
} commit_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static void clear_commits(BDRVVVFATState* s)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
int i;
|
2005-12-18 21:29:50 +03:00
|
|
|
DLOG(fprintf(stderr, "clear_commits (%d commits)\n", s->commits.next));
|
|
|
|
for (i = 0; i < s->commits.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
commit_t* commit = array_get(&(s->commits), i);
|
|
|
|
assert(commit->path || commit->action == ACTION_WRITEOUT);
|
|
|
|
if (commit->action != ACTION_WRITEOUT) {
|
|
|
|
assert(commit->path);
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(commit->path);
|
2017-05-23 00:11:54 +03:00
|
|
|
} else
|
|
|
|
assert(commit->path == NULL);
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
s->commits.next = 0;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static void schedule_rename(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
uint32_t cluster, char* new_path)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
commit_t* commit = array_get_next(&(s->commits));
|
2005-12-18 21:29:50 +03:00
|
|
|
commit->path = new_path;
|
|
|
|
commit->param.rename.cluster = cluster;
|
|
|
|
commit->action = ACTION_RENAME;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static void schedule_writeout(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
int dir_index, uint32_t modified_offset)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
commit_t* commit = array_get_next(&(s->commits));
|
2005-12-18 21:29:50 +03:00
|
|
|
commit->path = NULL;
|
|
|
|
commit->param.writeout.dir_index = dir_index;
|
|
|
|
commit->param.writeout.modified_offset = modified_offset;
|
|
|
|
commit->action = ACTION_WRITEOUT;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static void schedule_new_file(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
char* path, uint32_t first_cluster)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
commit_t* commit = array_get_next(&(s->commits));
|
2005-12-18 21:29:50 +03:00
|
|
|
commit->path = path;
|
|
|
|
commit->param.new_file.first_cluster = first_cluster;
|
|
|
|
commit->action = ACTION_NEW_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void schedule_mkdir(BDRVVVFATState* s, uint32_t cluster, char* path)
|
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
commit_t* commit = array_get_next(&(s->commits));
|
2005-12-18 21:29:50 +03:00
|
|
|
commit->path = path;
|
|
|
|
commit->param.mkdir.cluster = cluster;
|
|
|
|
commit->action = ACTION_MKDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
2008-07-03 23:54:19 +04:00
|
|
|
/*
|
|
|
|
* Since the sequence number is at most 0x3f, and the filename
|
|
|
|
* length is at most 13 times the sequence number, the maximal
|
|
|
|
* filename length is 0x3f * 13 bytes.
|
|
|
|
*/
|
|
|
|
unsigned char name[0x3f * 13 + 1];
|
2017-07-15 16:28:40 +03:00
|
|
|
gunichar2 name2[0x3f * 13 + 1];
|
2005-12-18 21:29:50 +03:00
|
|
|
int checksum, len;
|
|
|
|
int sequence_number;
|
|
|
|
} long_file_name;
|
|
|
|
|
|
|
|
static void lfn_init(long_file_name* lfn)
|
|
|
|
{
|
|
|
|
lfn->sequence_number = lfn->len = 0;
|
|
|
|
lfn->checksum = 0x100;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 0 if parsed successfully, > 0 if no long name, < 0 if error */
|
|
|
|
static int parse_long_name(long_file_name* lfn,
|
2017-05-23 00:11:54 +03:00
|
|
|
const direntry_t* direntry)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
int i, j, offset;
|
|
|
|
const unsigned char* pointer = (const unsigned char*)direntry;
|
|
|
|
|
|
|
|
if (!is_long_name(direntry))
|
2017-05-23 00:11:54 +03:00
|
|
|
return 1;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
if (pointer[0] & 0x40) {
|
2017-07-15 16:28:40 +03:00
|
|
|
/* first entry; do some initialization */
|
2017-05-23 00:11:54 +03:00
|
|
|
lfn->sequence_number = pointer[0] & 0x3f;
|
|
|
|
lfn->checksum = pointer[13];
|
|
|
|
lfn->name[0] = 0;
|
|
|
|
lfn->name[lfn->sequence_number * 13] = 0;
|
2017-07-15 16:28:40 +03:00
|
|
|
} else if ((pointer[0] & 0x3f) != --lfn->sequence_number) {
|
|
|
|
/* not the expected sequence number */
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2017-07-15 16:28:40 +03:00
|
|
|
} else if (pointer[13] != lfn->checksum) {
|
|
|
|
/* not the expected checksum */
|
2017-05-23 00:11:54 +03:00
|
|
|
return -2;
|
2017-07-15 16:28:40 +03:00
|
|
|
} else if (pointer[12] || pointer[26] || pointer[27]) {
|
|
|
|
/* invalid zero fields */
|
2017-05-23 00:11:54 +03:00
|
|
|
return -3;
|
2017-07-15 16:28:40 +03:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
offset = 13 * (lfn->sequence_number - 1);
|
|
|
|
for (i = 0, j = 1; i < 13; i++, j+=2) {
|
2017-05-23 00:11:54 +03:00
|
|
|
if (j == 11)
|
|
|
|
j = 14;
|
|
|
|
else if (j == 26)
|
|
|
|
j = 28;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-07-15 16:28:40 +03:00
|
|
|
if (pointer[j] == 0 && pointer[j + 1] == 0) {
|
|
|
|
/* end of long file name */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gunichar2 c = (pointer[j + 1] << 8) + pointer[j];
|
|
|
|
lfn->name2[offset + i] = c;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-07-15 16:28:40 +03:00
|
|
|
if (pointer[0] & 0x40) {
|
|
|
|
/* first entry; set len */
|
|
|
|
lfn->len = offset + i;
|
|
|
|
}
|
|
|
|
if ((pointer[0] & 0x3f) == 0x01) {
|
|
|
|
/* last entry; finalize entry */
|
|
|
|
glong olen;
|
|
|
|
gchar *utf8 = g_utf16_to_utf8(lfn->name2, lfn->len, NULL, &olen, NULL);
|
|
|
|
if (!utf8) {
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
lfn->len = olen;
|
|
|
|
memcpy(lfn->name, utf8, olen + 1);
|
|
|
|
g_free(utf8);
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* returns 0 if successful, >0 if no short_name, and <0 on error */
|
|
|
|
static int parse_short_name(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
long_file_name* lfn, direntry_t* direntry)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
int i, j;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
if (!is_short_name(direntry))
|
2017-05-23 00:11:54 +03:00
|
|
|
return 1;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
for (j = 7; j >= 0 && direntry->name[j] == ' '; j--);
|
|
|
|
for (i = 0; i <= j; i++) {
|
2017-07-15 16:28:40 +03:00
|
|
|
uint8_t c = direntry->name[i];
|
|
|
|
if (c != to_valid_short_char(c)) {
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2017-07-15 16:28:40 +03:00
|
|
|
} else if (s->downcase_short_names) {
|
2017-05-23 00:11:54 +03:00
|
|
|
lfn->name[i] = qemu_tolower(direntry->name[i]);
|
2017-07-15 16:28:40 +03:00
|
|
|
} else {
|
2017-05-23 00:11:54 +03:00
|
|
|
lfn->name[i] = direntry->name[i];
|
2017-07-15 16:28:40 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2013-12-12 00:37:11 +04:00
|
|
|
for (j = 2; j >= 0 && direntry->name[8 + j] == ' '; j--) {
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
if (j >= 0) {
|
2017-05-23 00:11:54 +03:00
|
|
|
lfn->name[i++] = '.';
|
|
|
|
lfn->name[i + j + 1] = '\0';
|
|
|
|
for (;j >= 0; j--) {
|
2013-12-12 00:37:11 +04:00
|
|
|
uint8_t c = direntry->name[8 + j];
|
2017-07-15 16:28:40 +03:00
|
|
|
if (c != to_valid_short_char(c)) {
|
2013-12-12 00:37:11 +04:00
|
|
|
return -2;
|
|
|
|
} else if (s->downcase_short_names) {
|
|
|
|
lfn->name[i + j] = qemu_tolower(c);
|
|
|
|
} else {
|
|
|
|
lfn->name[i + j] = c;
|
|
|
|
}
|
2017-05-23 00:11:54 +03:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
} else
|
2017-05-23 00:11:54 +03:00
|
|
|
lfn->name[i + j + 1] = '\0';
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-07-15 16:28:38 +03:00
|
|
|
if (lfn->name[0] == DIR_KANJI_FAKE) {
|
|
|
|
lfn->name[0] = DIR_KANJI;
|
2017-05-23 00:12:04 +03:00
|
|
|
}
|
2007-12-16 06:16:05 +03:00
|
|
|
lfn->len = strlen((char*)lfn->name);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
return 0;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static inline uint32_t modified_fat_get(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
unsigned int cluster)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
if (cluster < s->last_cluster_of_root_directory) {
|
2017-05-23 00:11:54 +03:00
|
|
|
if (cluster + 1 == s->last_cluster_of_root_directory)
|
|
|
|
return s->max_fat_value;
|
|
|
|
else
|
|
|
|
return cluster + 1;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->fat_type==32) {
|
|
|
|
uint32_t* entry=((uint32_t*)s->fat2)+cluster;
|
|
|
|
return le32_to_cpu(*entry);
|
|
|
|
} else if (s->fat_type==16) {
|
|
|
|
uint16_t* entry=((uint16_t*)s->fat2)+cluster;
|
|
|
|
return le16_to_cpu(*entry);
|
|
|
|
} else {
|
|
|
|
const uint8_t* x=s->fat2+cluster*3/2;
|
|
|
|
return ((x[0]|(x[1]<<8))>>(cluster&1?4:0))&0x0fff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 00:34:28 +03:00
|
|
|
static inline bool cluster_was_modified(BDRVVVFATState *s,
|
|
|
|
uint32_t cluster_num)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
int was_modified = 0;
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
int i;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2016-05-30 18:13:09 +03:00
|
|
|
if (s->qcow == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2016-05-30 18:13:09 +03:00
|
|
|
for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) {
|
|
|
|
was_modified = bdrv_is_allocated(s->qcow->bs,
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
(cluster2sector(s, cluster_num) +
|
|
|
|
i) * BDRV_SECTOR_SIZE,
|
|
|
|
BDRV_SECTOR_SIZE, NULL);
|
2016-05-30 18:13:09 +03:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-03-09 00:34:28 +03:00
|
|
|
/*
|
|
|
|
* Note that this treats failures to learn allocation status the
|
|
|
|
* same as if an allocation has occurred. It's as safe as
|
|
|
|
* anything else, given that a failure to learn allocation status
|
|
|
|
* will probably result in more failures.
|
|
|
|
*/
|
|
|
|
return !!was_modified;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static const char* get_basename(const char* path)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
char* basename = strrchr(path, '/');
|
|
|
|
if (basename == NULL)
|
2017-05-23 00:11:54 +03:00
|
|
|
return path;
|
2005-12-18 21:29:50 +03:00
|
|
|
else
|
2017-05-23 00:11:54 +03:00
|
|
|
return basename + 1; /* strip '/' */
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
|
|
|
* The array s->used_clusters holds the states of the clusters. If it is
|
|
|
|
* part of a file, it has bit 2 set, in case of a directory, bit 1. If it
|
|
|
|
* was modified, bit 3 is set.
|
|
|
|
* If any cluster is allocated, but not part of a file or directory, this
|
|
|
|
* driver refuses to commit.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
USED_DIRECTORY = 1, USED_FILE = 2, USED_ANY = 3, USED_ALLOCATED = 4
|
2009-10-02 01:12:16 +04:00
|
|
|
} used_t;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
|
|
|
* get_cluster_count_for_direntry() not only determines how many clusters
|
|
|
|
* are occupied by direntry, but also if it was renamed or modified.
|
|
|
|
*
|
|
|
|
* A file is thought to be renamed *only* if there already was a file with
|
|
|
|
* exactly the same first cluster, but a different name.
|
|
|
|
*
|
|
|
|
* Further, the files/directories handled by this function are
|
|
|
|
* assumed to be *not* deleted (and *only* those).
|
|
|
|
*/
|
|
|
|
static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry_t* direntry, const char* path)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
|
|
|
* This is a little bit tricky:
|
|
|
|
* IF the guest OS just inserts a cluster into the file chain,
|
|
|
|
* and leaves the rest alone, (i.e. the original file had clusters
|
|
|
|
* 15 -> 16, but now has 15 -> 32 -> 16), then the following happens:
|
|
|
|
*
|
|
|
|
* - do_commit will write the cluster into the file at the given
|
|
|
|
* offset, but
|
|
|
|
*
|
|
|
|
* - the cluster which is overwritten should be moved to a later
|
|
|
|
* position in the file.
|
|
|
|
*
|
|
|
|
* I am not aware that any OS does something as braindead, but this
|
|
|
|
* situation could happen anyway when not committing for a long time.
|
|
|
|
* Just to be sure that this does not bite us, detect it, and copy the
|
|
|
|
* contents of the clusters to-be-overwritten into the qcow.
|
|
|
|
*/
|
|
|
|
int copy_it = 0;
|
|
|
|
int was_modified = 0;
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
|
|
|
uint32_t cluster_num = begin_of_direntry(direntry);
|
|
|
|
uint32_t offset = 0;
|
|
|
|
int first_mapping_index = -1;
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping = NULL;
|
2005-12-18 21:29:50 +03:00
|
|
|
const char* basename2 = NULL;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
vvfat_close_current_file(s);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* the root directory */
|
|
|
|
if (cluster_num == 0)
|
2017-05-23 00:11:54 +03:00
|
|
|
return 0;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* write support */
|
|
|
|
if (s->qcow) {
|
2017-05-23 00:11:54 +03:00
|
|
|
basename2 = get_basename(path);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping = find_mapping_for_cluster(s, cluster_num);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (mapping) {
|
|
|
|
const char* basename;
|
2006-04-23 18:36:41 +04:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
assert(mapping->mode & MODE_DELETED);
|
|
|
|
mapping->mode &= ~MODE_DELETED;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
basename = get_basename(mapping->path);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
assert(mapping->mode & MODE_NORMAL);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
/* rename */
|
|
|
|
if (strcmp(basename, basename2))
|
|
|
|
schedule_rename(s, cluster_num, g_strdup(path));
|
|
|
|
} else if (is_file(direntry))
|
|
|
|
/* new file */
|
|
|
|
schedule_new_file(s, g_strdup(path), cluster_num);
|
|
|
|
else {
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
while(1) {
|
2017-05-23 00:11:54 +03:00
|
|
|
if (s->qcow) {
|
|
|
|
if (!copy_it && cluster_was_modified(s, cluster_num)) {
|
|
|
|
if (mapping == NULL ||
|
|
|
|
mapping->begin > cluster_num ||
|
|
|
|
mapping->end <= cluster_num)
|
|
|
|
mapping = find_mapping_for_cluster(s, cluster_num);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (mapping &&
|
|
|
|
(mapping->mode & MODE_DIRECTORY) == 0) {
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
/* was modified in qcow */
|
|
|
|
if (offset != mapping->info.file.offset + s->cluster_size
|
|
|
|
* (cluster_num - mapping->begin)) {
|
|
|
|
/* offset of this cluster in file chain has changed */
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
copy_it = 1;
|
|
|
|
} else if (offset == 0) {
|
|
|
|
const char* basename = get_basename(mapping->path);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (strcmp(basename, basename2))
|
|
|
|
copy_it = 1;
|
|
|
|
first_mapping_index = array_index(&(s->mapping), mapping);
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (mapping->first_mapping_index != first_mapping_index
|
|
|
|
&& mapping->info.file.offset > 0) {
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
copy_it = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to write out? */
|
|
|
|
if (!was_modified && is_file(direntry)) {
|
|
|
|
was_modified = 1;
|
|
|
|
schedule_writeout(s, mapping->dir_index, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_it) {
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
int i;
|
2017-05-23 00:11:54 +03:00
|
|
|
/*
|
|
|
|
* This is horribly inefficient, but that is okay, since
|
|
|
|
* it is rarely executed, if at all.
|
|
|
|
*/
|
|
|
|
int64_t offset = cluster2sector(s, cluster_num);
|
|
|
|
|
|
|
|
vvfat_close_current_file(s);
|
2011-11-08 13:50:12 +04:00
|
|
|
for (i = 0; i < s->sectors_per_cluster; i++) {
|
2016-05-30 18:13:09 +03:00
|
|
|
int res;
|
|
|
|
|
block: Make bdrv_is_allocated() byte-based
We are gradually moving away from sector-based interfaces, towards
byte-based. In the common case, allocation is unlikely to ever use
values that are not naturally sector-aligned, but it is possible
that byte-based values will let us be more precise about allocation
at the end of an unaligned file that can do byte-based access.
Changing the signature of the function to use int64_t *pnum ensures
that the compiler enforces that all callers are updated. For now,
the io.c layer still assert()s that all callers are sector-aligned
on input and that *pnum is sector-aligned on return to the caller,
but that can be relaxed when a later patch implements byte-based
block status. Therefore, this code adds usages like
DIV_ROUND_UP(,BDRV_SECTOR_SIZE) to callers that still want aligned
values, where the call might reasonbly give non-aligned results
in the future; on the other hand, no rounding is needed for callers
that should just continue to work with byte alignment.
For the most part this patch is just the addition of scaling at the
callers followed by inverse scaling at bdrv_is_allocated(). But
some code, particularly bdrv_commit(), gets a lot simpler because it
no longer has to mess with sectors; also, it is now possible to pass
NULL if the caller does not care how much of the image is allocated
beyond the initial offset. Leave comments where we can further
simplify once a later patch eliminates the need for sector-aligned
requests through bdrv_is_allocated().
For ease of review, bdrv_is_allocated_above() will be tackled
separately.
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2017-07-07 15:44:57 +03:00
|
|
|
res = bdrv_is_allocated(s->qcow->bs,
|
|
|
|
(offset + i) * BDRV_SECTOR_SIZE,
|
|
|
|
BDRV_SECTOR_SIZE, NULL);
|
2017-03-09 00:34:28 +03:00
|
|
|
if (res < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-05-30 18:13:09 +03:00
|
|
|
if (!res) {
|
|
|
|
res = vvfat_read(s->bs, offset, s->cluster_buffer, 1);
|
|
|
|
if (res) {
|
2011-11-08 13:50:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2019-05-01 21:13:57 +03:00
|
|
|
res = bdrv_pwrite(s->qcow, offset * BDRV_SECTOR_SIZE,
|
|
|
|
s->cluster_buffer, BDRV_SECTOR_SIZE);
|
|
|
|
if (res < 0) {
|
2011-11-08 13:50:12 +04:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 00:11:54 +03:00
|
|
|
}
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
ret++;
|
|
|
|
if (s->used_clusters[cluster_num] & USED_ANY)
|
|
|
|
return 0;
|
|
|
|
s->used_clusters[cluster_num] = USED_FILE;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
cluster_num = modified_fat_get(s, cluster_num);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (fat_eof(s, cluster_num))
|
|
|
|
return ret;
|
|
|
|
else if (cluster_num < 2 || cluster_num > s->max_fat_value - 16)
|
|
|
|
return -1;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
offset += s->cluster_size;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
2007-09-17 01:08:06 +04:00
|
|
|
* This function looks at the modified data (qcow).
|
2005-12-18 21:29:50 +03:00
|
|
|
* It returns 0 upon inconsistency or error, and the number of clusters
|
|
|
|
* used by the directory, its subdirectories and their files.
|
|
|
|
*/
|
|
|
|
static int check_directory_consistency(BDRVVVFATState *s,
|
2017-05-23 00:11:54 +03:00
|
|
|
int cluster_num, const char* path)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
int ret = 0;
|
2011-08-21 07:09:37 +04:00
|
|
|
unsigned char* cluster = g_malloc(s->cluster_size);
|
2009-10-02 01:12:16 +04:00
|
|
|
direntry_t* direntries = (direntry_t*)cluster;
|
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, cluster_num);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
long_file_name lfn;
|
|
|
|
int path_len = strlen(path);
|
2011-06-01 12:57:00 +04:00
|
|
|
char path2[PATH_MAX + 1];
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
assert(path_len < PATH_MAX); /* len was tested before! */
|
2008-08-21 21:58:08 +04:00
|
|
|
pstrcpy(path2, sizeof(path2), path);
|
2005-12-18 21:29:50 +03:00
|
|
|
path2[path_len] = '/';
|
|
|
|
path2[path_len + 1] = '\0';
|
|
|
|
|
|
|
|
if (mapping) {
|
2017-05-23 00:11:54 +03:00
|
|
|
const char* basename = get_basename(mapping->path);
|
|
|
|
const char* basename2 = get_basename(path);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
assert(mapping->mode & MODE_DIRECTORY);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
assert(mapping->mode & MODE_DELETED);
|
|
|
|
mapping->mode &= ~MODE_DELETED;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (strcmp(basename, basename2))
|
|
|
|
schedule_rename(s, cluster_num, g_strdup(path));
|
2005-12-18 21:29:50 +03:00
|
|
|
} else
|
2017-05-23 00:11:54 +03:00
|
|
|
/* new directory */
|
|
|
|
schedule_mkdir(s, cluster_num, g_strdup(path));
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
lfn_init(&lfn);
|
|
|
|
do {
|
2017-05-23 00:11:54 +03:00
|
|
|
int i;
|
|
|
|
int subret = 0;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
ret++;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (s->used_clusters[cluster_num] & USED_ANY) {
|
|
|
|
fprintf(stderr, "cluster %d used more than once\n", (int)cluster_num);
|
2014-05-28 13:17:04 +04:00
|
|
|
goto fail;
|
2017-05-23 00:11:54 +03:00
|
|
|
}
|
|
|
|
s->used_clusters[cluster_num] = USED_DIRECTORY;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
DLOG(fprintf(stderr, "read cluster %d (sector %d)\n", (int)cluster_num, (int)cluster2sector(s, cluster_num)));
|
2017-05-23 00:11:54 +03:00
|
|
|
subret = vvfat_read(s->bs, cluster2sector(s, cluster_num), cluster,
|
|
|
|
s->sectors_per_cluster);
|
|
|
|
if (subret) {
|
|
|
|
fprintf(stderr, "Error fetching direntries\n");
|
|
|
|
fail:
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(cluster);
|
2017-05-23 00:11:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
for (i = 0; i < 0x10 * s->sectors_per_cluster; i++) {
|
|
|
|
int cluster_count = 0;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2011-09-13 00:33:01 +04:00
|
|
|
DLOG(fprintf(stderr, "check direntry %d:\n", i); print_direntry(direntries + i));
|
2017-05-23 00:11:54 +03:00
|
|
|
if (is_volume_label(direntries + i) || is_dot(direntries + i) ||
|
|
|
|
is_free(direntries + i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
subret = parse_long_name(&lfn, direntries + i);
|
|
|
|
if (subret < 0) {
|
|
|
|
fprintf(stderr, "Error in long name\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (subret == 0 || is_free(direntries + i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fat_chksum(direntries+i) != lfn.checksum) {
|
|
|
|
subret = parse_short_name(s, &lfn, direntries + i);
|
|
|
|
if (subret < 0) {
|
|
|
|
fprintf(stderr, "Error in short name (%d)\n", subret);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (subret > 0 || !strcmp((char*)lfn.name, ".")
|
|
|
|
|| !strcmp((char*)lfn.name, ".."))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lfn.checksum = 0x100; /* cannot use long name twice */
|
|
|
|
|
|
|
|
if (path_len + 1 + lfn.len >= PATH_MAX) {
|
|
|
|
fprintf(stderr, "Name too long: %s/%s\n", path, lfn.name);
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-08-21 21:58:08 +04:00
|
|
|
pstrcpy(path2 + path_len + 1, sizeof(path2) - path_len - 1,
|
|
|
|
(char*)lfn.name);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (is_directory(direntries + i)) {
|
|
|
|
if (begin_of_direntry(direntries + i) == 0) {
|
|
|
|
DLOG(fprintf(stderr, "invalid begin for directory: %s\n", path2); print_direntry(direntries + i));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
cluster_count = check_directory_consistency(s,
|
|
|
|
begin_of_direntry(direntries + i), path2);
|
|
|
|
if (cluster_count == 0) {
|
|
|
|
DLOG(fprintf(stderr, "problem in directory %s:\n", path2); print_direntry(direntries + i));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else if (is_file(direntries + i)) {
|
|
|
|
/* check file size with FAT */
|
|
|
|
cluster_count = get_cluster_count_for_direntry(s, direntries + i, path2);
|
|
|
|
if (cluster_count !=
|
2016-05-31 19:35:54 +03:00
|
|
|
DIV_ROUND_UP(le32_to_cpu(direntries[i].size), s->cluster_size)) {
|
2017-05-23 00:11:54 +03:00
|
|
|
DLOG(fprintf(stderr, "Cluster count mismatch\n"));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else
|
2010-03-18 21:41:57 +03:00
|
|
|
abort(); /* cluster_count = 0; */
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
ret += cluster_count;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
cluster_num = modified_fat_get(s, cluster_num);
|
2005-12-18 21:29:50 +03:00
|
|
|
} while(!fat_eof(s, cluster_num));
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(cluster);
|
2005-12-18 21:29:50 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 1 on success */
|
|
|
|
static int is_consistent(BDRVVVFATState* s)
|
|
|
|
{
|
|
|
|
int i, check;
|
|
|
|
int used_clusters_count = 0;
|
|
|
|
|
|
|
|
DLOG(checkpoint());
|
|
|
|
/*
|
|
|
|
* - get modified FAT
|
|
|
|
* - compare the two FATs (TODO)
|
|
|
|
* - get buffer for marking used clusters
|
|
|
|
* - recurse direntries from root (using bs->bdrv_read to make
|
|
|
|
* sure to get the new data)
|
|
|
|
* - check that the FAT agrees with the size
|
|
|
|
* - count the number of clusters occupied by this directory and
|
|
|
|
* its files
|
|
|
|
* - check that the cumulative used cluster count agrees with the
|
|
|
|
* FAT
|
|
|
|
* - if all is fine, return number of used clusters
|
|
|
|
*/
|
|
|
|
if (s->fat2 == NULL) {
|
2017-05-23 00:11:54 +03:00
|
|
|
int size = 0x200 * s->sectors_per_fat;
|
|
|
|
s->fat2 = g_malloc(size);
|
|
|
|
memcpy(s->fat2, s->fat.pointer, size);
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
check = vvfat_read(s->bs,
|
2017-05-23 00:11:57 +03:00
|
|
|
s->offset_to_fat, s->fat2, s->sectors_per_fat);
|
2005-12-18 21:29:50 +03:00
|
|
|
if (check) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "Could not copy fat\n");
|
|
|
|
return 0;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
assert (s->used_clusters);
|
|
|
|
for (i = 0; i < sector2cluster(s, s->sector_count); i++)
|
2017-05-23 00:11:54 +03:00
|
|
|
s->used_clusters[i] &= ~USED_ANY;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
clear_commits(s);
|
|
|
|
|
|
|
|
/* mark every mapped file/directory as deleted.
|
|
|
|
* (check_directory_consistency() will unmark those still present). */
|
|
|
|
if (s->qcow)
|
2017-05-23 00:11:54 +03:00
|
|
|
for (i = 0; i < s->mapping.next; i++) {
|
|
|
|
mapping_t* mapping = array_get(&(s->mapping), i);
|
|
|
|
if (mapping->first_mapping_index < 0)
|
|
|
|
mapping->mode |= MODE_DELETED;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
used_clusters_count = check_directory_consistency(s, 0, s->path);
|
|
|
|
if (used_clusters_count <= 0) {
|
2017-05-23 00:11:54 +03:00
|
|
|
DLOG(fprintf(stderr, "problem in directory\n"));
|
|
|
|
return 0;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
check = s->last_cluster_of_root_directory;
|
|
|
|
for (i = check; i < sector2cluster(s, s->sector_count); i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
if (modified_fat_get(s, i)) {
|
|
|
|
if(!s->used_clusters[i]) {
|
|
|
|
DLOG(fprintf(stderr, "FAT was modified (%d), but cluster is not used?\n", i));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
check++;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
if (s->used_clusters[i] == USED_ALLOCATED) {
|
|
|
|
/* allocated, but not used... */
|
|
|
|
DLOG(fprintf(stderr, "unused, modified cluster: %d\n", i));
|
|
|
|
return 0;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (check != used_clusters_count)
|
2017-05-23 00:11:54 +03:00
|
|
|
return 0;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
return used_clusters_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void adjust_mapping_indices(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
int offset, int adjust)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s->mapping.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping_t* mapping = array_get(&(s->mapping), i);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
#define ADJUST_MAPPING_INDEX(name) \
|
2017-05-23 00:11:54 +03:00
|
|
|
if (mapping->name >= offset) \
|
|
|
|
mapping->name += adjust
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
ADJUST_MAPPING_INDEX(first_mapping_index);
|
|
|
|
if (mapping->mode & MODE_DIRECTORY)
|
|
|
|
ADJUST_MAPPING_INDEX(info.dir.parent_mapping_index);
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* insert or update mapping */
|
2009-10-02 01:12:16 +04:00
|
|
|
static mapping_t* insert_mapping(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
uint32_t begin, uint32_t end)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* - find mapping where mapping->begin >= begin,
|
|
|
|
* - if mapping->begin > begin: insert
|
|
|
|
* - adjust all references to mappings!
|
|
|
|
* - else: adjust
|
|
|
|
* - replace name
|
|
|
|
*/
|
|
|
|
int index = find_mapping_for_cluster_aux(s, begin, 0, s->mapping.next);
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping = NULL;
|
|
|
|
mapping_t* first_mapping = array_get(&(s->mapping), 0);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
if (index < s->mapping.next && (mapping = array_get(&(s->mapping), index))
|
2017-05-23 00:11:54 +03:00
|
|
|
&& mapping->begin < begin) {
|
|
|
|
mapping->end = begin;
|
|
|
|
index++;
|
|
|
|
mapping = array_get(&(s->mapping), index);
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
if (index >= s->mapping.next || mapping->begin > begin) {
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping = array_insert(&(s->mapping), index, 1);
|
|
|
|
mapping->path = NULL;
|
|
|
|
adjust_mapping_indices(s, index, +1);
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mapping->begin = begin;
|
|
|
|
mapping->end = end;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
DLOG(mapping_t* next_mapping;
|
2005-12-18 21:29:50 +03:00
|
|
|
assert(index + 1 >= s->mapping.next ||
|
|
|
|
((next_mapping = array_get(&(s->mapping), index + 1)) &&
|
|
|
|
next_mapping->begin >= end)));
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
|
2017-05-23 00:11:54 +03:00
|
|
|
s->current_mapping = array_get(&(s->mapping),
|
|
|
|
s->current_mapping - first_mapping);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int remove_mapping(BDRVVVFATState* s, int mapping_index)
|
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping = array_get(&(s->mapping), mapping_index);
|
|
|
|
mapping_t* first_mapping = array_get(&(s->mapping), 0);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
/* free mapping */
|
2011-10-01 01:29:53 +04:00
|
|
|
if (mapping->first_mapping_index < 0) {
|
|
|
|
g_free(mapping->path);
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
/* remove from s->mapping */
|
|
|
|
array_remove(&(s->mapping), mapping_index);
|
|
|
|
|
|
|
|
/* adjust all references to mappings */
|
|
|
|
adjust_mapping_indices(s, mapping_index, -1);
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
if (s->current_mapping && first_mapping != (mapping_t*)s->mapping.pointer)
|
2017-05-23 00:11:54 +03:00
|
|
|
s->current_mapping = array_get(&(s->mapping),
|
|
|
|
s->current_mapping - first_mapping);
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static void adjust_dirindices(BDRVVVFATState* s, int offset, int adjust)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < s->mapping.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping_t* mapping = array_get(&(s->mapping), i);
|
|
|
|
if (mapping->dir_index >= offset)
|
|
|
|
mapping->dir_index += adjust;
|
|
|
|
if ((mapping->mode & MODE_DIRECTORY) &&
|
|
|
|
mapping->info.dir.first_dir_index >= offset)
|
|
|
|
mapping->info.dir.first_dir_index += adjust;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static direntry_t* insert_direntries(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
int dir_index, int count)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
|
|
|
* make room in s->directory,
|
|
|
|
* adjust_dirindices
|
|
|
|
*/
|
2009-10-02 01:12:16 +04:00
|
|
|
direntry_t* result = array_insert(&(s->directory), dir_index, count);
|
2005-12-18 21:29:50 +03:00
|
|
|
if (result == NULL)
|
2017-05-23 00:11:54 +03:00
|
|
|
return NULL;
|
2005-12-18 21:29:50 +03:00
|
|
|
adjust_dirindices(s, dir_index, count);
|
2005-04-29 01:15:08 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static int remove_direntries(BDRVVVFATState* s, int dir_index, int count)
|
|
|
|
{
|
|
|
|
int ret = array_remove_slice(&(s->directory), dir_index, count);
|
|
|
|
if (ret)
|
2017-05-23 00:11:54 +03:00
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
adjust_dirindices(s, dir_index, -count);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/*
|
|
|
|
* Adapt the mappings of the cluster chain starting at first cluster
|
|
|
|
* (i.e. if a file starts at first_cluster, the chain is followed according
|
|
|
|
* to the modified fat, and the corresponding entries in s->mapping are
|
|
|
|
* adjusted)
|
|
|
|
*/
|
|
|
|
static int commit_mappings(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
uint32_t first_cluster, int dir_index)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
|
|
|
|
direntry_t* direntry = array_get(&(s->directory), dir_index);
|
2005-12-18 21:29:50 +03:00
|
|
|
uint32_t cluster = first_cluster;
|
|
|
|
|
|
|
|
vvfat_close_current_file(s);
|
|
|
|
|
|
|
|
assert(mapping);
|
|
|
|
assert(mapping->begin == first_cluster);
|
|
|
|
mapping->first_mapping_index = -1;
|
|
|
|
mapping->dir_index = dir_index;
|
|
|
|
mapping->mode = (dir_index <= 0 || is_directory(direntry)) ?
|
2017-05-23 00:11:54 +03:00
|
|
|
MODE_DIRECTORY : MODE_NORMAL;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
while (!fat_eof(s, cluster)) {
|
2017-05-23 00:11:54 +03:00
|
|
|
uint32_t c, c1;
|
|
|
|
|
|
|
|
for (c = cluster, c1 = modified_fat_get(s, c); c + 1 == c1;
|
|
|
|
c = c1, c1 = modified_fat_get(s, c1));
|
|
|
|
|
|
|
|
c++;
|
|
|
|
if (c > mapping->end) {
|
|
|
|
int index = array_index(&(s->mapping), mapping);
|
|
|
|
int i, max_i = s->mapping.next - index;
|
|
|
|
for (i = 1; i < max_i && mapping[i].begin < c; i++);
|
|
|
|
while (--i > 0)
|
|
|
|
remove_mapping(s, index + 1);
|
|
|
|
}
|
|
|
|
assert(mapping == array_get(&(s->mapping), s->mapping.next - 1)
|
|
|
|
|| mapping[1].begin >= c);
|
|
|
|
mapping->end = c;
|
|
|
|
|
|
|
|
if (!fat_eof(s, c1)) {
|
|
|
|
int i = find_mapping_for_cluster_aux(s, c1, 0, s->mapping.next);
|
|
|
|
mapping_t* next_mapping = i >= s->mapping.next ? NULL :
|
|
|
|
array_get(&(s->mapping), i);
|
|
|
|
|
|
|
|
if (next_mapping == NULL || next_mapping->begin > c1) {
|
|
|
|
int i1 = array_index(&(s->mapping), mapping);
|
|
|
|
|
|
|
|
next_mapping = insert_mapping(s, c1, c1+1);
|
|
|
|
|
|
|
|
if (c1 < c)
|
|
|
|
i1++;
|
|
|
|
mapping = array_get(&(s->mapping), i1);
|
|
|
|
}
|
|
|
|
|
|
|
|
next_mapping->dir_index = mapping->dir_index;
|
|
|
|
next_mapping->first_mapping_index =
|
|
|
|
mapping->first_mapping_index < 0 ?
|
|
|
|
array_index(&(s->mapping), mapping) :
|
|
|
|
mapping->first_mapping_index;
|
|
|
|
next_mapping->path = mapping->path;
|
|
|
|
next_mapping->mode = mapping->mode;
|
|
|
|
next_mapping->read_only = mapping->read_only;
|
|
|
|
if (mapping->mode & MODE_DIRECTORY) {
|
|
|
|
next_mapping->info.dir.parent_mapping_index =
|
|
|
|
mapping->info.dir.parent_mapping_index;
|
|
|
|
next_mapping->info.dir.first_dir_index =
|
|
|
|
mapping->info.dir.first_dir_index +
|
|
|
|
0x10 * s->sectors_per_cluster *
|
|
|
|
(mapping->end - mapping->begin);
|
|
|
|
} else
|
|
|
|
next_mapping->info.file.offset = mapping->info.file.offset +
|
|
|
|
mapping->end - mapping->begin;
|
|
|
|
|
|
|
|
mapping = next_mapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster = c1;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static int commit_direntries(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
int dir_index, int parent_mapping_index)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
direntry_t* direntry = array_get(&(s->directory), dir_index);
|
2005-12-18 21:29:50 +03:00
|
|
|
uint32_t first_cluster = dir_index == 0 ? 0 : begin_of_direntry(direntry);
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, first_cluster);
|
2005-12-18 21:29:50 +03:00
|
|
|
int factor = 0x10 * s->sectors_per_cluster;
|
|
|
|
int old_cluster_count, new_cluster_count;
|
2018-11-06 00:38:38 +03:00
|
|
|
int current_dir_index;
|
|
|
|
int first_dir_index;
|
2005-12-18 21:29:50 +03:00
|
|
|
int ret, i;
|
|
|
|
uint32_t c;
|
|
|
|
|
|
|
|
assert(direntry);
|
|
|
|
assert(mapping);
|
|
|
|
assert(mapping->begin == first_cluster);
|
|
|
|
assert(mapping->info.dir.first_dir_index < s->directory.next);
|
|
|
|
assert(mapping->mode & MODE_DIRECTORY);
|
|
|
|
assert(dir_index == 0 || is_directory(direntry));
|
|
|
|
|
2018-11-06 00:38:38 +03:00
|
|
|
DLOG(fprintf(stderr, "commit_direntries for %s, parent_mapping_index %d\n",
|
|
|
|
mapping->path, parent_mapping_index));
|
|
|
|
|
|
|
|
current_dir_index = mapping->info.dir.first_dir_index;
|
|
|
|
first_dir_index = current_dir_index;
|
2005-12-18 21:29:50 +03:00
|
|
|
mapping->info.dir.parent_mapping_index = parent_mapping_index;
|
|
|
|
|
|
|
|
if (first_cluster == 0) {
|
2017-05-23 00:11:54 +03:00
|
|
|
old_cluster_count = new_cluster_count =
|
|
|
|
s->last_cluster_of_root_directory;
|
2005-12-18 21:29:50 +03:00
|
|
|
} else {
|
2017-05-23 00:11:54 +03:00
|
|
|
for (old_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
|
|
|
|
c = fat_get(s, c))
|
|
|
|
old_cluster_count++;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
for (new_cluster_count = 0, c = first_cluster; !fat_eof(s, c);
|
|
|
|
c = modified_fat_get(s, c))
|
|
|
|
new_cluster_count++;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
if (new_cluster_count > old_cluster_count) {
|
2017-05-23 00:11:54 +03:00
|
|
|
if (insert_direntries(s,
|
|
|
|
current_dir_index + factor * old_cluster_count,
|
|
|
|
factor * (new_cluster_count - old_cluster_count)) == NULL)
|
|
|
|
return -1;
|
2005-12-18 21:29:50 +03:00
|
|
|
} else if (new_cluster_count < old_cluster_count)
|
2017-05-23 00:11:54 +03:00
|
|
|
remove_direntries(s,
|
|
|
|
current_dir_index + factor * new_cluster_count,
|
|
|
|
factor * (old_cluster_count - new_cluster_count));
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
for (c = first_cluster; !fat_eof(s, c); c = modified_fat_get(s, c)) {
|
2016-04-27 15:11:38 +03:00
|
|
|
direntry_t *first_direntry;
|
2017-05-23 00:11:54 +03:00
|
|
|
void* direntry = array_get(&(s->directory), current_dir_index);
|
|
|
|
int ret = vvfat_read(s->bs, cluster2sector(s, c), direntry,
|
|
|
|
s->sectors_per_cluster);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-04-27 15:11:38 +03:00
|
|
|
|
|
|
|
/* The first directory entry on the filesystem is the volume name */
|
|
|
|
first_direntry = (direntry_t*) s->directory.pointer;
|
|
|
|
assert(!memcmp(first_direntry->name, s->volume_label, 11));
|
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
current_dir_index += factor;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
ret = commit_mappings(s, first_cluster, dir_index);
|
|
|
|
if (ret)
|
2017-05-23 00:11:54 +03:00
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
/* recurse */
|
|
|
|
for (i = 0; i < factor * new_cluster_count; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry = array_get(&(s->directory), first_dir_index + i);
|
|
|
|
if (is_directory(direntry) && !is_dot(direntry)) {
|
|
|
|
mapping = find_mapping_for_cluster(s, first_cluster);
|
2018-11-06 00:38:38 +03:00
|
|
|
if (mapping == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-05-23 00:11:54 +03:00
|
|
|
assert(mapping->mode & MODE_DIRECTORY);
|
|
|
|
ret = commit_direntries(s, first_dir_index + i,
|
|
|
|
array_index(&(s->mapping), mapping));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* commit one file (adjust contents, adjust mapping),
|
|
|
|
return first_mapping_index */
|
|
|
|
static int commit_one_file(BDRVVVFATState* s,
|
2017-05-23 00:11:54 +03:00
|
|
|
int dir_index, uint32_t offset)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
direntry_t* direntry = array_get(&(s->directory), dir_index);
|
2005-12-18 21:29:50 +03:00
|
|
|
uint32_t c = begin_of_direntry(direntry);
|
|
|
|
uint32_t first_cluster = c;
|
2009-10-02 01:12:16 +04:00
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, c);
|
2005-12-18 21:29:50 +03:00
|
|
|
uint32_t size = filesize_of_direntry(direntry);
|
2018-11-14 15:50:16 +03:00
|
|
|
char *cluster;
|
2005-12-18 21:29:50 +03:00
|
|
|
uint32_t i;
|
|
|
|
int fd = 0;
|
|
|
|
|
|
|
|
assert(offset < size);
|
|
|
|
assert((offset % s->cluster_size) == 0);
|
|
|
|
|
2018-11-06 00:38:38 +03:00
|
|
|
if (mapping == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
for (i = s->cluster_size; i < offset; i += s->cluster_size)
|
2017-05-23 00:11:54 +03:00
|
|
|
c = modified_fat_get(s, c);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2012-08-15 00:43:45 +04:00
|
|
|
fd = qemu_open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);
|
2005-12-18 21:29:50 +03:00
|
|
|
if (fd < 0) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,
|
|
|
|
strerror(errno), errno);
|
|
|
|
return fd;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2011-10-01 01:29:53 +04:00
|
|
|
if (offset > 0) {
|
|
|
|
if (lseek(fd, offset, SEEK_SET) != offset) {
|
2012-08-15 00:43:46 +04:00
|
|
|
qemu_close(fd);
|
2011-10-01 01:29:53 +04:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2018-11-14 15:50:16 +03:00
|
|
|
cluster = g_malloc(s->cluster_size);
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
while (offset < size) {
|
2017-05-23 00:11:54 +03:00
|
|
|
uint32_t c1;
|
|
|
|
int rest_size = (size - offset > s->cluster_size ?
|
|
|
|
s->cluster_size : size - offset);
|
|
|
|
int ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
c1 = modified_fat_get(s, c);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
assert((size - offset == 0 && fat_eof(s, c)) ||
|
|
|
|
(size > offset && c >=2 && !fat_eof(s, c)));
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
ret = vvfat_read(s->bs, cluster2sector(s, c),
|
2017-06-22 14:04:16 +03:00
|
|
|
(uint8_t*)cluster, DIV_ROUND_UP(rest_size, 0x200));
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2011-10-01 01:29:53 +04:00
|
|
|
if (ret < 0) {
|
2012-08-15 00:43:46 +04:00
|
|
|
qemu_close(fd);
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(cluster);
|
|
|
|
return ret;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2011-10-01 01:29:53 +04:00
|
|
|
if (write(fd, cluster, rest_size) < 0) {
|
2012-08-15 00:43:46 +04:00
|
|
|
qemu_close(fd);
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(cluster);
|
|
|
|
return -2;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
offset += rest_size;
|
|
|
|
c = c1;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
2010-01-20 02:56:14 +03:00
|
|
|
if (ftruncate(fd, size)) {
|
|
|
|
perror("ftruncate()");
|
2012-08-15 00:43:46 +04:00
|
|
|
qemu_close(fd);
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(cluster);
|
2010-01-20 02:56:14 +03:00
|
|
|
return -4;
|
|
|
|
}
|
2012-08-15 00:43:46 +04:00
|
|
|
qemu_close(fd);
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(cluster);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
return commit_mappings(s, first_cluster, dir_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* test, if all mappings point to valid direntries */
|
|
|
|
static void check1(BDRVVVFATState* s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < s->mapping.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping_t* mapping = array_get(&(s->mapping), i);
|
|
|
|
if (mapping->mode & MODE_DELETED) {
|
|
|
|
fprintf(stderr, "deleted\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert(mapping->dir_index < s->directory.next);
|
|
|
|
direntry_t* direntry = array_get(&(s->directory), mapping->dir_index);
|
|
|
|
assert(mapping->begin == begin_of_direntry(direntry) || mapping->first_mapping_index >= 0);
|
|
|
|
if (mapping->mode & MODE_DIRECTORY) {
|
|
|
|
assert(mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster * (mapping->end - mapping->begin) <= s->directory.next);
|
|
|
|
assert((mapping->info.dir.first_dir_index % (0x10 * s->sectors_per_cluster)) == 0);
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
/* test, if all direntries have mappings */
|
|
|
|
static void check2(BDRVVVFATState* s)
|
2005-04-29 01:15:08 +04:00
|
|
|
{
|
|
|
|
int i;
|
2005-12-18 21:29:50 +03:00
|
|
|
int first_mapping = -1;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
for (i = 0; i < s->directory.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry_t* direntry = array_get(&(s->directory), i);
|
|
|
|
|
|
|
|
if (is_short_name(direntry) && begin_of_direntry(direntry)) {
|
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, begin_of_direntry(direntry));
|
|
|
|
assert(mapping);
|
|
|
|
assert(mapping->dir_index == i || is_dot(direntry));
|
|
|
|
assert(mapping->begin == begin_of_direntry(direntry) || is_dot(direntry));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((i % (0x10 * s->sectors_per_cluster)) == 0) {
|
|
|
|
/* cluster start */
|
|
|
|
int j, count = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < s->mapping.next; j++) {
|
|
|
|
mapping_t* mapping = array_get(&(s->mapping), j);
|
|
|
|
if (mapping->mode & MODE_DELETED)
|
|
|
|
continue;
|
|
|
|
if (mapping->mode & MODE_DIRECTORY) {
|
|
|
|
if (mapping->info.dir.first_dir_index <= i && mapping->info.dir.first_dir_index + 0x10 * s->sectors_per_cluster > i) {
|
|
|
|
assert(++count == 1);
|
|
|
|
if (mapping->first_mapping_index == -1)
|
|
|
|
first_mapping = array_index(&(s->mapping), mapping);
|
|
|
|
else
|
|
|
|
assert(first_mapping == mapping->first_mapping_index);
|
|
|
|
if (mapping->info.dir.parent_mapping_index < 0)
|
|
|
|
assert(j == 0);
|
|
|
|
else {
|
|
|
|
mapping_t* parent = array_get(&(s->mapping), mapping->info.dir.parent_mapping_index);
|
|
|
|
assert(parent->mode & MODE_DIRECTORY);
|
|
|
|
assert(parent->info.dir.first_dir_index < mapping->info.dir.first_dir_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count == 0)
|
|
|
|
first_mapping = -1;
|
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
static int handle_renames_and_mkdirs(BDRVVVFATState* s)
|
|
|
|
{
|
|
|
|
int i;
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "handle_renames\n");
|
|
|
|
for (i = 0; i < s->commits.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
commit_t* commit = array_get(&(s->commits), i);
|
|
|
|
fprintf(stderr, "%d, %s (%d, %d)\n", i, commit->path ? commit->path : "(null)", commit->param.rename.cluster, commit->action);
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < s->commits.next;) {
|
2017-05-23 00:11:54 +03:00
|
|
|
commit_t* commit = array_get(&(s->commits), i);
|
|
|
|
if (commit->action == ACTION_RENAME) {
|
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s,
|
|
|
|
commit->param.rename.cluster);
|
2018-11-06 00:38:38 +03:00
|
|
|
char *old_path;
|
2017-05-23 00:11:54 +03:00
|
|
|
|
2018-11-06 00:38:38 +03:00
|
|
|
if (mapping == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
old_path = mapping->path;
|
2017-05-23 00:11:54 +03:00
|
|
|
assert(commit->path);
|
|
|
|
mapping->path = commit->path;
|
|
|
|
if (rename(old_path, mapping->path))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
if (mapping->mode & MODE_DIRECTORY) {
|
|
|
|
int l1 = strlen(mapping->path);
|
|
|
|
int l2 = strlen(old_path);
|
|
|
|
int diff = l1 - l2;
|
|
|
|
direntry_t* direntry = array_get(&(s->directory),
|
|
|
|
mapping->info.dir.first_dir_index);
|
|
|
|
uint32_t c = mapping->begin;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* recurse */
|
|
|
|
while (!fat_eof(s, c)) {
|
|
|
|
do {
|
|
|
|
direntry_t* d = direntry + i;
|
|
|
|
|
|
|
|
if (is_file(d) || (is_directory(d) && !is_dot(d))) {
|
2018-11-06 00:38:38 +03:00
|
|
|
int l;
|
|
|
|
char *new_path;
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping_t* m = find_mapping_for_cluster(s,
|
|
|
|
begin_of_direntry(d));
|
2018-11-06 00:38:38 +03:00
|
|
|
if (m == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
l = strlen(m->path);
|
|
|
|
new_path = g_malloc(l + diff + 1);
|
2017-05-23 00:11:54 +03:00
|
|
|
|
|
|
|
assert(!strncmp(m->path, mapping->path, l2));
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2008-08-21 21:58:08 +04:00
|
|
|
pstrcpy(new_path, l + diff + 1, mapping->path);
|
|
|
|
pstrcpy(new_path + l1, l + diff + 1 - l1,
|
|
|
|
m->path + l2);
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
schedule_rename(s, m->begin, new_path);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} while((i % (0x10 * s->sectors_per_cluster)) != 0);
|
|
|
|
c = fat_get(s, c);
|
|
|
|
}
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(old_path);
|
2017-05-23 00:11:54 +03:00
|
|
|
array_remove(&(s->commits), i);
|
|
|
|
continue;
|
|
|
|
} else if (commit->action == ACTION_MKDIR) {
|
|
|
|
mapping_t* mapping;
|
|
|
|
int j, parent_path_len;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2005-12-20 01:11:49 +03:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
if (mkdir(commit->path))
|
|
|
|
return -5;
|
|
|
|
#else
|
|
|
|
if (mkdir(commit->path, 0755))
|
|
|
|
return -5;
|
|
|
|
#endif
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
mapping = insert_mapping(s, commit->param.mkdir.cluster,
|
|
|
|
commit->param.mkdir.cluster + 1);
|
|
|
|
if (mapping == NULL)
|
|
|
|
return -6;
|
|
|
|
|
|
|
|
mapping->mode = MODE_DIRECTORY;
|
|
|
|
mapping->read_only = 0;
|
|
|
|
mapping->path = commit->path;
|
|
|
|
j = s->directory.next;
|
|
|
|
assert(j);
|
|
|
|
insert_direntries(s, s->directory.next,
|
|
|
|
0x10 * s->sectors_per_cluster);
|
|
|
|
mapping->info.dir.first_dir_index = j;
|
|
|
|
|
|
|
|
parent_path_len = strlen(commit->path)
|
|
|
|
- strlen(get_basename(commit->path)) - 1;
|
|
|
|
for (j = 0; j < s->mapping.next; j++) {
|
|
|
|
mapping_t* m = array_get(&(s->mapping), j);
|
|
|
|
if (m->first_mapping_index < 0 && m != mapping &&
|
|
|
|
!strncmp(m->path, mapping->path, parent_path_len) &&
|
|
|
|
strlen(m->path) == parent_path_len)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(j < s->mapping.next);
|
|
|
|
mapping->info.dir.parent_mapping_index = j;
|
|
|
|
|
|
|
|
array_remove(&(s->commits), i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: make sure that the short name is not matching *another* file
|
|
|
|
*/
|
|
|
|
static int handle_commits(BDRVVVFATState* s)
|
|
|
|
{
|
|
|
|
int i, fail = 0;
|
|
|
|
|
|
|
|
vvfat_close_current_file(s);
|
|
|
|
|
|
|
|
for (i = 0; !fail && i < s->commits.next; i++) {
|
2017-05-23 00:11:54 +03:00
|
|
|
commit_t* commit = array_get(&(s->commits), i);
|
|
|
|
switch(commit->action) {
|
|
|
|
case ACTION_RENAME: case ACTION_MKDIR:
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
fail = -2;
|
|
|
|
break;
|
|
|
|
case ACTION_WRITEOUT: {
|
2010-03-13 17:18:50 +03:00
|
|
|
#ifndef NDEBUG
|
|
|
|
/* these variables are only used by assert() below */
|
2017-05-23 00:11:54 +03:00
|
|
|
direntry_t* entry = array_get(&(s->directory),
|
|
|
|
commit->param.writeout.dir_index);
|
|
|
|
uint32_t begin = begin_of_direntry(entry);
|
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, begin);
|
2010-03-13 17:18:50 +03:00
|
|
|
#endif
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2017-05-23 00:11:54 +03:00
|
|
|
assert(mapping);
|
|
|
|
assert(mapping->begin == begin);
|
|
|
|
assert(commit->path == NULL);
|
|
|
|
|
|
|
|
if (commit_one_file(s, commit->param.writeout.dir_index,
|
|
|
|
commit->param.writeout.modified_offset))
|
|
|
|
fail = -3;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTION_NEW_FILE: {
|
|
|
|
int begin = commit->param.new_file.first_cluster;
|
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, begin);
|
|
|
|
direntry_t* entry;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* find direntry */
|
|
|
|
for (i = 0; i < s->directory.next; i++) {
|
|
|
|
entry = array_get(&(s->directory), i);
|
|
|
|
if (is_file(entry) && begin_of_direntry(entry) == begin)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= s->directory.next) {
|
|
|
|
fail = -6;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure there exists an initial mapping */
|
|
|
|
if (mapping && mapping->begin != begin) {
|
|
|
|
mapping->end = begin;
|
|
|
|
mapping = NULL;
|
|
|
|
}
|
|
|
|
if (mapping == NULL) {
|
|
|
|
mapping = insert_mapping(s, begin, begin+1);
|
|
|
|
}
|
|
|
|
/* most members will be fixed in commit_mappings() */
|
|
|
|
assert(commit->path);
|
|
|
|
mapping->path = commit->path;
|
|
|
|
mapping->read_only = 0;
|
|
|
|
mapping->mode = MODE_NORMAL;
|
|
|
|
mapping->info.file.offset = 0;
|
|
|
|
|
|
|
|
if (commit_one_file(s, i, 0))
|
|
|
|
fail = -7;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
if (i > 0 && array_remove_slice(&(s->commits), 0, i))
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2005-12-18 21:29:50 +03:00
|
|
|
return fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_deletes(BDRVVVFATState* s)
|
|
|
|
{
|
|
|
|
int i, deferred = 1, deleted = 1;
|
|
|
|
|
|
|
|
/* delete files corresponding to mappings marked as deleted */
|
|
|
|
/* handle DELETEs and unused mappings (modified_fat_get(s, mapping->begin) == 0) */
|
|
|
|
while (deferred && deleted) {
|
2017-05-23 00:11:54 +03:00
|
|
|
deferred = 0;
|
|
|
|
deleted = 0;
|
|
|
|
|
|
|
|
for (i = 1; i < s->mapping.next; i++) {
|
|
|
|
mapping_t* mapping = array_get(&(s->mapping), i);
|
|
|
|
if (mapping->mode & MODE_DELETED) {
|
|
|
|
direntry_t* entry = array_get(&(s->directory),
|
|
|
|
mapping->dir_index);
|
|
|
|
|
|
|
|
if (is_free(entry)) {
|
|
|
|
/* remove file/directory */
|
|
|
|
if (mapping->mode & MODE_DIRECTORY) {
|
|
|
|
int j, next_dir_index = s->directory.next,
|
|
|
|
first_dir_index = mapping->info.dir.first_dir_index;
|
|
|
|
|
|
|
|
if (rmdir(mapping->path) < 0) {
|
|
|
|
if (errno == ENOTEMPTY) {
|
|
|
|
deferred++;
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 1; j < s->mapping.next; j++) {
|
|
|
|
mapping_t* m = array_get(&(s->mapping), j);
|
|
|
|
if (m->mode & MODE_DIRECTORY &&
|
|
|
|
m->info.dir.first_dir_index >
|
|
|
|
first_dir_index &&
|
|
|
|
m->info.dir.first_dir_index <
|
|
|
|
next_dir_index)
|
|
|
|
next_dir_index =
|
|
|
|
m->info.dir.first_dir_index;
|
|
|
|
}
|
|
|
|
remove_direntries(s, first_dir_index,
|
|
|
|
next_dir_index - first_dir_index);
|
|
|
|
|
|
|
|
deleted++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unlink(mapping->path))
|
|
|
|
return -4;
|
|
|
|
deleted++;
|
|
|
|
}
|
|
|
|
DLOG(fprintf(stderr, "DELETE (%d)\n", i); print_mapping(mapping); print_direntry(entry));
|
|
|
|
remove_mapping(s, i);
|
|
|
|
}
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* synchronize mapping with new state:
|
|
|
|
*
|
|
|
|
* - copy FAT (with bdrv_read)
|
|
|
|
* - mark all filenames corresponding to mappings as deleted
|
|
|
|
* - recurse direntries from root (using bs->bdrv_read)
|
|
|
|
* - delete files corresponding to mappings marked as deleted
|
|
|
|
*/
|
|
|
|
static int do_commit(BDRVVVFATState* s)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* the real meat are the commits. Nothing to do? Move along! */
|
|
|
|
if (s->commits.next == 0)
|
2017-05-23 00:11:54 +03:00
|
|
|
return 0;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
vvfat_close_current_file(s);
|
|
|
|
|
|
|
|
ret = handle_renames_and_mkdirs(s);
|
|
|
|
if (ret) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "Error handling renames (%d)\n", ret);
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
/* copy FAT (with bdrv_read) */
|
2005-12-18 21:29:50 +03:00
|
|
|
memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat);
|
|
|
|
|
|
|
|
/* recurse direntries from root (using bs->bdrv_read) */
|
|
|
|
ret = commit_direntries(s, 0, -1);
|
|
|
|
if (ret) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "Fatal: error while committing (%d)\n", ret);
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = handle_commits(s);
|
|
|
|
if (ret) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "Error handling commits (%d)\n", ret);
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = handle_deletes(s);
|
|
|
|
if (ret) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "Error deleting\n");
|
2010-03-18 21:41:57 +03:00
|
|
|
abort();
|
2017-05-23 00:11:54 +03:00
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 23:31:09 +03:00
|
|
|
if (s->qcow->bs->drv && s->qcow->bs->drv->bdrv_make_empty) {
|
2016-05-30 18:13:09 +03:00
|
|
|
s->qcow->bs->drv->bdrv_make_empty(s->qcow->bs);
|
2011-11-08 13:50:12 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
memset(s->used_clusters, 0, sector2cluster(s, s->sector_count));
|
|
|
|
|
|
|
|
DLOG(checkpoint());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int try_commit(BDRVVVFATState* s)
|
|
|
|
{
|
|
|
|
vvfat_close_current_file(s);
|
|
|
|
DLOG(checkpoint());
|
|
|
|
if(!is_consistent(s))
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2005-12-18 21:29:50 +03:00
|
|
|
return do_commit(s);
|
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
static int vvfat_write(BlockDriverState *bs, int64_t sector_num,
|
2005-12-18 21:29:50 +03:00
|
|
|
const uint8_t *buf, int nb_sectors)
|
|
|
|
{
|
2007-09-17 01:08:06 +04:00
|
|
|
BDRVVVFATState *s = bs->opaque;
|
2005-12-18 21:29:50 +03:00
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
DLOG(checkpoint());
|
|
|
|
|
2010-09-10 14:27:02 +04:00
|
|
|
/* Check if we're operating in read-only mode */
|
|
|
|
if (s->qcow == NULL) {
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
vvfat_close_current_file(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some sanity checks:
|
|
|
|
* - do not allow writing to the boot sector
|
|
|
|
*/
|
|
|
|
|
2017-05-23 00:11:57 +03:00
|
|
|
if (sector_num < s->offset_to_fat)
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
for (i = sector2cluster(s, sector_num);
|
2017-05-23 00:11:54 +03:00
|
|
|
i <= sector2cluster(s, sector_num + nb_sectors - 1);) {
|
|
|
|
mapping_t* mapping = find_mapping_for_cluster(s, i);
|
|
|
|
if (mapping) {
|
|
|
|
if (mapping->read_only) {
|
|
|
|
fprintf(stderr, "Tried to write to write-protected file %s\n",
|
|
|
|
mapping->path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mapping->mode & MODE_DIRECTORY) {
|
|
|
|
int begin = cluster2sector(s, i);
|
|
|
|
int end = begin + s->sectors_per_cluster, k;
|
|
|
|
int dir_index;
|
|
|
|
const direntry_t* direntries;
|
|
|
|
long_file_name lfn;
|
|
|
|
|
|
|
|
lfn_init(&lfn);
|
|
|
|
|
|
|
|
if (begin < sector_num)
|
|
|
|
begin = sector_num;
|
|
|
|
if (end > sector_num + nb_sectors)
|
|
|
|
end = sector_num + nb_sectors;
|
|
|
|
dir_index = mapping->dir_index +
|
|
|
|
0x10 * (begin - mapping->begin * s->sectors_per_cluster);
|
|
|
|
direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
|
|
|
|
|
|
|
|
for (k = 0; k < (end - begin) * 0x10; k++) {
|
|
|
|
/* no access to the direntry of a read-only file */
|
2017-07-15 16:28:40 +03:00
|
|
|
if (is_short_name(direntries + k) &&
|
2017-05-23 00:11:54 +03:00
|
|
|
(direntries[k].attributes & 1)) {
|
|
|
|
if (memcmp(direntries + k,
|
|
|
|
array_get(&(s->directory), dir_index + k),
|
|
|
|
sizeof(direntry_t))) {
|
2017-09-11 22:52:50 +03:00
|
|
|
warn_report("tried to write to write-protected "
|
|
|
|
"file");
|
2017-05-23 00:11:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = mapping->end;
|
|
|
|
} else
|
|
|
|
i++;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use qcow backend. Commit later.
|
|
|
|
*/
|
|
|
|
DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
|
2019-05-01 21:13:57 +03:00
|
|
|
ret = bdrv_pwrite(s->qcow, sector_num * BDRV_SECTOR_SIZE, buf,
|
|
|
|
nb_sectors * BDRV_SECTOR_SIZE);
|
2005-12-18 21:29:50 +03:00
|
|
|
if (ret < 0) {
|
2017-05-23 00:11:54 +03:00
|
|
|
fprintf(stderr, "Error writing to qcow backend\n");
|
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = sector2cluster(s, sector_num);
|
2017-05-23 00:11:54 +03:00
|
|
|
i <= sector2cluster(s, sector_num + nb_sectors - 1); i++)
|
|
|
|
if (i >= 0)
|
|
|
|
s->used_clusters[i] |= USED_ALLOCATED;
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
DLOG(checkpoint());
|
|
|
|
/* TODO: add timeout */
|
|
|
|
try_commit(s);
|
|
|
|
|
|
|
|
DLOG(checkpoint());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:14:08 +03:00
|
|
|
static int coroutine_fn
|
|
|
|
vvfat_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags)
|
2011-10-20 15:16:23 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
BDRVVVFATState *s = bs->opaque;
|
2016-04-26 18:14:08 +03:00
|
|
|
uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
|
|
|
|
int nb_sectors = bytes >> BDRV_SECTOR_BITS;
|
|
|
|
void *buf;
|
|
|
|
|
2019-08-27 21:59:12 +03:00
|
|
|
assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
|
|
|
|
assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
|
2016-04-26 18:14:08 +03:00
|
|
|
|
|
|
|
buf = g_try_malloc(bytes);
|
|
|
|
if (bytes && buf == NULL) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
qemu_iovec_to_buf(qiov, 0, buf, bytes);
|
|
|
|
|
2011-10-20 15:16:23 +04:00
|
|
|
qemu_co_mutex_lock(&s->lock);
|
|
|
|
ret = vvfat_write(bs, sector_num, buf, nb_sectors);
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
2016-04-26 18:14:08 +03:00
|
|
|
|
|
|
|
g_free(buf);
|
|
|
|
|
2011-10-20 15:16:23 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:27:00 +03:00
|
|
|
static int coroutine_fn vvfat_co_block_status(BlockDriverState *bs,
|
|
|
|
bool want_zero, int64_t offset,
|
|
|
|
int64_t bytes, int64_t *n,
|
|
|
|
int64_t *map,
|
|
|
|
BlockDriverState **file)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
2018-02-13 23:27:00 +03:00
|
|
|
*n = bytes;
|
2013-09-04 21:00:30 +04:00
|
|
|
return BDRV_BLOCK_DATA;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
2016-04-26 18:14:08 +03:00
|
|
|
static int coroutine_fn
|
|
|
|
write_target_commit(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags)
|
|
|
|
{
|
2017-06-29 16:27:43 +03:00
|
|
|
int ret;
|
|
|
|
|
2010-09-10 14:27:03 +04:00
|
|
|
BDRVVVFATState* s = *((BDRVVVFATState**) bs->opaque);
|
2017-06-29 16:27:43 +03:00
|
|
|
qemu_co_mutex_lock(&s->lock);
|
|
|
|
ret = try_commit(s);
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
|
|
|
|
|
|
|
return ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static BlockDriver vvfat_write_target = {
|
2009-05-27 18:14:13 +04:00
|
|
|
.format_name = "vvfat_write_target",
|
2017-02-15 22:51:58 +03:00
|
|
|
.instance_size = sizeof(void*),
|
2016-04-26 18:14:08 +03:00
|
|
|
.bdrv_co_pwritev = write_target_commit,
|
2005-12-18 21:29:50 +03:00
|
|
|
};
|
|
|
|
|
2016-05-30 18:13:09 +03:00
|
|
|
static void vvfat_qcow_options(int *child_flags, QDict *child_options,
|
|
|
|
int parent_flags, QDict *parent_options)
|
2005-12-18 21:29:50 +03:00
|
|
|
{
|
2016-09-15 17:53:02 +03:00
|
|
|
qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "off");
|
block: Add auto-read-only option
If a management application builds the block graph node by node, the
protocol layer doesn't inherit its read-only option from the format
layer any more, so it must be set explicitly.
Backing files should work on read-only storage, but at the same time, a
block job like commit should be able to reopen them read-write if they
are on read-write storage. However, without option inheritance, reopen
only changes the read-only option for the root node (typically the
format layer), but not the protocol layer, so reopening fails (the
format layer wants to get write permissions, but the protocol layer is
still read-only).
A simple workaround for the problem in the management tool would be to
open the protocol layer always read-write and to make only the format
layer read-only for backing files. However, sometimes the file is
actually stored on read-only storage and we don't know whether the image
can be opened read-write (for example, for NBD it depends on the server
we're trying to connect to). This adds an option that makes QEMU try to
open the image read-write, but allows it to degrade to a read-only mode
without returning an error.
The documentation for this option is consciously phrased in a way that
allows QEMU to switch to a better model eventually: Instead of trying
when the image is first opened, making the read-only flag dynamic and
changing it automatically whenever the first BLK_PERM_WRITE user is
attached or the last one is detached would be much more useful
behaviour.
Unfortunately, this more useful behaviour is also a lot harder to
implement, and libvirt needs a solution now before it can switch to
-blockdev, so let's start with this easier approach for now.
Instead of adding a new auto-read-only option, turning the existing
read-only into an enum (with a bool alternate for compatibility) was
considered, but it complicated the implementation to the point that it
didn't seem to be worth it.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2018-10-05 19:57:40 +03:00
|
|
|
qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off");
|
2018-03-15 06:45:07 +03:00
|
|
|
qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on");
|
2016-05-30 18:13:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const BdrvChildRole child_vvfat_qcow = {
|
2018-05-29 18:17:45 +03:00
|
|
|
.parent_is_bds = true,
|
2016-05-30 18:13:09 +03:00
|
|
|
.inherit_options = vvfat_qcow_options,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int enable_write_target(BlockDriverState *bs, Error **errp)
|
|
|
|
{
|
|
|
|
BDRVVVFATState *s = bs->opaque;
|
2014-06-05 13:20:52 +04:00
|
|
|
BlockDriver *bdrv_qcow = NULL;
|
2015-09-14 16:33:33 +03:00
|
|
|
BlockDriverState *backing;
|
2014-06-05 13:20:52 +04:00
|
|
|
QemuOpts *opts = NULL;
|
2010-09-10 14:27:04 +04:00
|
|
|
int ret;
|
2005-12-18 21:29:50 +03:00
|
|
|
int size = sector2cluster(s, s->sector_count);
|
2015-08-26 20:47:48 +03:00
|
|
|
QDict *options;
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
s->used_clusters = calloc(size, 1);
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
array_init(&(s->commits), sizeof(commit_t));
|
2005-12-18 21:29:50 +03:00
|
|
|
|
2015-01-22 16:03:30 +03:00
|
|
|
s->qcow_filename = g_malloc(PATH_MAX);
|
|
|
|
ret = get_tmp_filename(s->qcow_filename, PATH_MAX);
|
2012-05-28 11:27:54 +04:00
|
|
|
if (ret < 0) {
|
2014-05-16 13:00:17 +04:00
|
|
|
error_setg_errno(errp, -ret, "can't create temporary file");
|
2013-07-17 13:57:37 +04:00
|
|
|
goto err;
|
2012-05-28 11:27:54 +04:00
|
|
|
}
|
2009-05-27 16:48:06 +04:00
|
|
|
|
|
|
|
bdrv_qcow = bdrv_find_format("qcow");
|
2014-12-02 20:32:43 +03:00
|
|
|
if (!bdrv_qcow) {
|
|
|
|
error_setg(errp, "Failed to locate qcow driver");
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-06-05 13:21:11 +04:00
|
|
|
opts = qemu_opts_create(bdrv_qcow->create_opts, NULL, 0, &error_abort);
|
2015-02-12 18:46:36 +03:00
|
|
|
qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s->sector_count * 512,
|
|
|
|
&error_abort);
|
2015-02-12 19:52:20 +03:00
|
|
|
qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:", &error_abort);
|
2009-05-27 16:48:06 +04:00
|
|
|
|
2014-06-05 13:21:11 +04:00
|
|
|
ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, errp);
|
2014-06-05 13:20:52 +04:00
|
|
|
qemu_opts_del(opts);
|
2013-07-17 13:57:37 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
2010-09-10 14:27:04 +04:00
|
|
|
|
2015-08-26 20:47:48 +03:00
|
|
|
options = qdict_new();
|
2017-04-28 00:58:17 +03:00
|
|
|
qdict_put_str(options, "write-target.driver", "qcow");
|
2016-05-30 18:13:09 +03:00
|
|
|
s->qcow = bdrv_open_child(s->qcow_filename, options, "write-target", bs,
|
|
|
|
&child_vvfat_qcow, false, errp);
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(options);
|
2016-05-17 17:41:31 +03:00
|
|
|
if (!s->qcow) {
|
|
|
|
ret = -EINVAL;
|
2013-07-17 13:57:37 +04:00
|
|
|
goto err;
|
2010-03-31 16:40:27 +04:00
|
|
|
}
|
2005-12-18 21:29:50 +03:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
unlink(s->qcow_filename);
|
|
|
|
#endif
|
|
|
|
|
2017-02-15 22:51:58 +03:00
|
|
|
backing = bdrv_new_open_driver(&vvfat_write_target, NULL, BDRV_O_ALLOW_RDWR,
|
|
|
|
&error_abort);
|
|
|
|
*(void**) backing->opaque = s;
|
|
|
|
|
2017-02-17 22:42:32 +03:00
|
|
|
bdrv_set_backing_hd(s->bs, backing, &error_abort);
|
2015-09-14 16:33:33 +03:00
|
|
|
bdrv_unref(backing);
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
return 0;
|
2013-07-17 13:57:37 +04:00
|
|
|
|
|
|
|
err:
|
|
|
|
g_free(s->qcow_filename);
|
|
|
|
s->qcow_filename = NULL;
|
|
|
|
return ret;
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2016-12-20 18:23:46 +03:00
|
|
|
static void vvfat_child_perm(BlockDriverState *bs, BdrvChild *c,
|
|
|
|
const BdrvChildRole *role,
|
2017-09-14 13:47:11 +03:00
|
|
|
BlockReopenQueue *reopen_queue,
|
2016-12-20 18:23:46 +03:00
|
|
|
uint64_t perm, uint64_t shared,
|
|
|
|
uint64_t *nperm, uint64_t *nshared)
|
|
|
|
{
|
|
|
|
BDRVVVFATState *s = bs->opaque;
|
|
|
|
|
|
|
|
assert(c == s->qcow || role == &child_backing);
|
|
|
|
|
|
|
|
if (c == s->qcow) {
|
|
|
|
/* This is a private node, nobody should try to attach to it */
|
|
|
|
*nperm = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE;
|
|
|
|
*nshared = BLK_PERM_WRITE_UNCHANGED;
|
|
|
|
} else {
|
|
|
|
/* The backing file is there so 'commit' can use it. vvfat doesn't
|
|
|
|
* access it in any way. */
|
|
|
|
*nperm = 0;
|
|
|
|
*nshared = BLK_PERM_ALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-29 01:15:08 +04:00
|
|
|
static void vvfat_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVVVFATState *s = bs->opaque;
|
|
|
|
|
|
|
|
vvfat_close_current_file(s);
|
|
|
|
array_free(&(s->fat));
|
|
|
|
array_free(&(s->directory));
|
|
|
|
array_free(&(s->mapping));
|
2011-10-01 01:29:53 +04:00
|
|
|
g_free(s->cluster_buffer);
|
2011-11-22 19:52:13 +04:00
|
|
|
|
|
|
|
if (s->qcow) {
|
|
|
|
migrate_del_blocker(s->migration_blocker);
|
|
|
|
error_free(s->migration_blocker);
|
|
|
|
}
|
2005-04-29 01:15:08 +04:00
|
|
|
}
|
|
|
|
|
2019-02-01 22:29:25 +03:00
|
|
|
static const char *const vvfat_strong_runtime_opts[] = {
|
|
|
|
"dir",
|
|
|
|
"fat-type",
|
|
|
|
"floppy",
|
|
|
|
"label",
|
|
|
|
"rw",
|
|
|
|
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-05-10 02:03:42 +04:00
|
|
|
static BlockDriver bdrv_vvfat = {
|
2013-04-12 21:42:04 +04:00
|
|
|
.format_name = "vvfat",
|
|
|
|
.protocol_name = "fat",
|
|
|
|
.instance_size = sizeof(BDRVVVFATState),
|
|
|
|
|
|
|
|
.bdrv_parse_filename = vvfat_parse_filename,
|
|
|
|
.bdrv_file_open = vvfat_open,
|
2016-06-24 01:37:17 +03:00
|
|
|
.bdrv_refresh_limits = vvfat_refresh_limits,
|
2013-04-12 21:42:04 +04:00
|
|
|
.bdrv_close = vvfat_close,
|
2016-12-20 18:23:46 +03:00
|
|
|
.bdrv_child_perm = vvfat_child_perm,
|
2013-04-12 21:42:04 +04:00
|
|
|
|
2016-04-26 18:14:08 +03:00
|
|
|
.bdrv_co_preadv = vvfat_co_preadv,
|
|
|
|
.bdrv_co_pwritev = vvfat_co_pwritev,
|
2018-02-13 23:27:00 +03:00
|
|
|
.bdrv_co_block_status = vvfat_co_block_status,
|
2019-02-01 22:29:25 +03:00
|
|
|
|
|
|
|
.strong_runtime_opts = vvfat_strong_runtime_opts,
|
2005-04-29 01:15:08 +04:00
|
|
|
};
|
|
|
|
|
2009-05-10 02:03:42 +04:00
|
|
|
static void bdrv_vvfat_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_vvfat);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_vvfat_init);
|
|
|
|
|
2005-12-18 21:29:50 +03:00
|
|
|
#ifdef DEBUG
|
2017-09-13 13:21:28 +03:00
|
|
|
static void checkpoint(void)
|
|
|
|
{
|
2009-10-02 01:12:16 +04:00
|
|
|
assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2);
|
2005-12-18 21:29:50 +03:00
|
|
|
check1(vvv);
|
|
|
|
check2(vvv);
|
|
|
|
assert(!vvv->current_mapping || vvv->current_fd || (vvv->current_mapping->mode & MODE_DIRECTORY));
|
|
|
|
}
|
|
|
|
#endif
|