From 7d1a47cf13726c95ac46027156cc12dd9da5b855 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Mon, 5 Aug 2013 22:02:45 -0400 Subject: Rewrite filesystem code (WIP) This patch redoes the filesystem code from the FAT driver up to the clipboard code in onplay.c. Not every aspect of this is finished therefore it is still "WIP". I don't wish to do too much at once (haha!). What is left to do is get dircache back in the sim and find an implementation for the dircache indicies in the tagcache and playlist code or do something else that has the same benefit. Leaving these out for now does not make anything unusable. All the basics are done. Phone app code should probably get vetted (and app path handling just plain rewritten as environment expansions); the SDL app and Android run well. Main things addressed: 1) Thread safety: There is none right now in the trunk code. Most of what currently works is luck when multiple threads are involved or multiple descriptors to the same file are open. 2) POSIX compliance: Many of the functions behave nothing like their counterparts on a host system. This leads to inconsistent code or very different behavior from native to hosted. One huge offender was rename(). Going point by point would fill a book. 3) Actual running RAM usage: Many targets will use less RAM and less stack space (some more RAM because I upped the number of cache buffers for large memory). There's very little memory lying fallow in rarely-used areas (see 'Key core changes' below). Also, all targets may open the same number of directory streams whereas before those with less than 8MB RAM were limited to 8, not 12 implying those targets will save slightly less. 4) Performance: The test_disk plugin shows markedly improved performance, particularly in the area of (uncached) directory scanning, due partly to more optimal directory reading and to a better sector cache algorithm. Uncached times tend to be better while there is a bit of a slowdown in dircache due to it being a bit heavier of an implementation. It's not noticeable by a human as far as I can say. Key core changes: 1) Files and directories share core code and data structures. 2) The filesystem code knows which descriptors refer to same file. This ensures that changes from one stream are appropriately reflected in every open descriptor for that file (fileobj_mgr.c). 3) File and directory cache buffers are borrowed from the main sector cache. This means that when they are not in use by a file, they are not wasted, but used for the cache. Most of the time, only a few of them are needed. It also means that adding more file and directory handles is less expensive. All one must do in ensure a large enough cache to borrow from. 4) Relative path components are supported and the namespace is unified. It does not support full relative paths to an implied current directory; what is does support is use of "." and "..". Adding the former would not be very difficult. The namespace is unified in the sense that volumes may be specified several times along with relative parts, e.g.: "/<0>/foo/../../<1>/bar" :<=> "/<1>/bar". 5) Stack usage is down due to sharing of data, static allocation and less duplication of strings on the stack. This requires more serialization than I would like but since the number of threads is limited to a low number, the tradoff in favor of the stack seems reasonable. 6) Separates and heirarchicalizes (sic) the SIM and APP filesystem code. SIM path and volume handling is just like the target. Some aspects of the APP file code get more straightforward (e.g. no path hashing is needed). Dircache: Deserves its own section. Dircache is new but pays homage to the old. The old one was not compatible and so it, since it got redone, does all the stuff it always should have done such as: 1) It may be update and used at any time during the build process. No longer has one to wait for it to finish building to do basic file management (create, remove, rename, etc.). 2) It does not need to be either fully scanned or completely disabled; it can be incomplete (i.e. overfilled, missing paths), still be of benefit and be correct. 3) Handles mounting and dismounting of individual volumes which means a full rebuild is not needed just because you pop a new SD card in the slot. Now, because it reuses its freed entry data, may rebuild only that volume. 4) Much more fundamental to the file code. When it is built, it is the keeper of the master file list whether enabled or not ("disabled" is just a state of the cache). Its must always to ready to be started and bind all streams opened prior to being enabled. 5) Maintains any short filenames in OEM format which means that it does not need to be rebuilt when changing the default codepage. Miscellaneous Compatibility: 1) Update any other code that would otherwise not work such as the hotswap mounting code in various card drivers. 2) File management: Clipboard needed updating because of the behavioral changes. Still needs a little more work on some finer points. 3) Remove now-obsolete functionality such as the mutex's "no preempt" flag (which was only for the prior FAT driver). 4) struct dirinfo uses time_t rather than raw FAT directory entry time fields. I plan to follow up on genericizing everything there (i.e. no FAT attributes). 5) unicode.c needed some redoing so that the file code does not try try to load codepages during a scan, which is actually a problem with the current code. The default codepage, if any is required, is now kept in RAM separarately (bufalloced) from codepages specified to iso_decode() (which must not be bufalloced because the conversion may be done by playback threads). Brings with it some additional reusable core code: 1) Revised file functions: Reusable code that does things such as safe path concatenation and parsing without buffer limitations or data duplication. Variants that copy or alter the input path may be based off these. To do: 1) Put dircache functionality back in the sim. Treating it internally as a different kind of file system seems the best approach at this time. 2) Restore use of dircache indexes in the playlist and database or something effectively the same. Since the cache doesn't have to be complete in order to be used, not getting a hit on the cache doesn't unambiguously say if the path exists or not. Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8 Reviewed-on: http://gerrit.rockbox.org/566 Reviewed-by: Michael Sevakis Tested: Michael Sevakis --- firmware/export/fat.h | 225 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 135 insertions(+), 90 deletions(-) (limited to 'firmware/export/fat.h') diff --git a/firmware/export/fat.h b/firmware/export/fat.h index a0d52acc35..3aa1e254dc 100644 --- a/firmware/export/fat.h +++ b/firmware/export/fat.h @@ -18,123 +18,168 @@ * KIND, either express or implied. * ****************************************************************************/ - #ifndef FAT_H #define FAT_H #include -#include "mv.h" /* for volume definitions */ +#include +#include #include "config.h" #include "system.h" +#include "mv.h" /* for volume definitions */ + +/******** + **** DO NOT use these functions directly unless otherwise noted. Required + **** synchronization is done by higher-level interfaces to minimize locking + **** overhead. + **** + **** Volume, drive, string, etc. parameters should also be checked by + **** callers for gross violations-- NULL strings, out-of-bounds values, + **** etc. + ****/ + +/**************************************************************************** + ** Values that can be overridden by a target in config-[target].h + **/ + +/* if your ATA implementation can do better, go right ahead and increase this + * value */ +#ifndef FAT_MAX_TRANSFER_SIZE +#define FAT_MAX_TRANSFER_SIZE 256 +#endif -/* This value can be overwritten by a target in config-[target].h, but - that behaviour is still experimental */ +/* still experimental? */ +/* increasing this will increase the total memory used by the cache; the + cache, as noted in disk_cache.h, has other minimum requirements that may + prevent reducing its number of entries in order to compensate */ #ifndef SECTOR_SIZE #define SECTOR_SIZE 512 #endif +/** + ****************************************************************************/ + +#define INVALID_SECNUM (0xfffffffeul) /* sequential, not FAT */ +#define FAT_MAX_FILE_SIZE (0xfffffffful) /* 2^32-1 bytes */ +#define MAX_DIRENTRIES 65536 +#define MAX_DIRECTORY_SIZE (MAX_DIRENTRIES*32) /* 2MB max size */ + +/* these aren't I/O error conditions, so define specially; failure rc's + * shouldn't return the last digit as "0", therefore this is unambiguous */ +#define FAT_RC_ENOSPC (-10) +#define FAT_SEEK_EOF (-20) + /* Number of bytes reserved for a file name (including the trailing \0). - Since names are stored in the entry as UTF-8, we won't be able to + Since names are stored in the entry as UTF-8, we won't be ble to store all names allowed by FAT. In FAT, a name can have max 255 characters (not bytes!). Since the UTF-8 encoding of a char may take up to 4 bytes, there will be names that we won't be able to store completely. For such names, the short DOS name is used. */ -#define FAT_FILENAME_BYTES 256 - +#define FAT_DIRENTRY_NAME_MAX 255 struct fat_direntry { - unsigned char name[FAT_FILENAME_BYTES]; /* UTF-8 encoded name plus \0 */ - unsigned short attr; /* Attributes */ - unsigned char crttimetenth; /* Millisecond creation - time stamp (0-199) */ - unsigned short crttime; /* Creation time */ - unsigned short crtdate; /* Creation date */ - unsigned short lstaccdate; /* Last access date */ - unsigned short wrttime; /* Last write time */ - unsigned short wrtdate; /* Last write date */ - unsigned long filesize; /* File size in bytes */ - long firstcluster; /* fstclusterhi<<16 + fstcluslo */ + union { + uint8_t name[255+1+4]; /* UTF-8 name plus \0 plus parse slop */ + uint16_t ucssegs[5+20][13]; /* UTF-16 segment buffer - layout saves... */ + }; /* ...130 bytes (important if stacked) */ + uint16_t ucsterm; /* allow one NULL-term after ucssegs */ + uint8_t shortname[13]; /* DOS filename (OEM charset) */ + uint8_t attr; /* file attributes */ + uint8_t crttimetenth; /* millisecond creation time stamp (0-199) */ + uint16_t crttime; /* creation time */ + uint16_t crtdate; /* creation date */ + uint16_t lstaccdate; /* last access date */ + uint16_t wrttime; /* last write time */ + uint16_t wrtdate; /* last write date */ + uint32_t filesize; /* file size in bytes */ + int32_t firstcluster; /* first FAT cluster of file, 0 if empty */ }; -#define FAT_ATTR_READ_ONLY 0x01 -#define FAT_ATTR_HIDDEN 0x02 -#define FAT_ATTR_SYSTEM 0x04 -#define FAT_ATTR_VOLUME_ID 0x08 -#define FAT_ATTR_DIRECTORY 0x10 -#define FAT_ATTR_ARCHIVE 0x20 -#define FAT_ATTR_VOLUME 0x40 /* this is a volume, not a real directory */ +/* cursor structure used for scanning directories; holds the last-returned + entry information */ +struct fat_dirscan_info +{ + unsigned int entry; /* short dir entry index in parent */ + unsigned int entries; /* number of dir entries used */ +}; + +#define FAT_RW_VAL (0u - 1) +/* basic FAT file information about where to find a file and who houses it */ struct fat_file { - long firstcluster; /* first cluster in file */ - long lastcluster; /* cluster of last access */ - long lastsector; /* sector of last access */ - long clusternum; /* current clusternum */ - long sectornum; /* sector number in this cluster */ - unsigned int direntry; /* short dir entry index from start of dir */ - unsigned int direntries; /* number of dir entries used by this file */ - long dircluster; /* first cluster of dir */ - bool eof; #ifdef HAVE_MULTIVOLUME - int volume; /* file resides on which volume */ + int volume; /* file resides on which volume (first!) */ #endif + long firstcluster; /* first cluster in file */ + long dircluster; /* first cluster of parent directory */ + struct fat_dirscan_info e; /* entry information */ }; -struct fat_dir +/* this stores what was last accessed when read or writing a file's data */ +struct fat_filestr { - unsigned char sectorcache[SECTOR_SIZE] CACHEALIGN_ATTR; - unsigned int entry; - unsigned int entrycount; - long sector; - struct fat_file file; - /* There are 2-bytes per characters. We don't want to bother too much, as LFN entries are - * at much 255 characters longs, that's at most 20 LFN entries. Each entry hold at most - * 13 characters, that a total of 260 characters. So we keep a buffer of that size. - * Keep coherent with fat.c code. */ - unsigned char longname[260 * 2]; -} CACHEALIGN_ATTR; - -#ifdef HAVE_HOTSWAP -extern void fat_lock(void); -extern void fat_unlock(void); -#endif + struct fat_file *fatfilep; /* common file information */ + long lastcluster; /* cluster of last access */ + unsigned long lastsector; /* sector of last access */ + long clusternum; /* cluster number of last access */ + unsigned long sectornum; /* sector number within current cluster */ + bool eof; /* end-of-file reached */ +}; + +/** File entity functions **/ +int fat_create_file(struct fat_file *parent, const char *name, + uint8_t attr, struct fat_file *file, + struct fat_direntry *fatent); +bool fat_dir_is_parent(const struct fat_file *dir, const struct fat_file *file); +bool fat_file_is_same(const struct fat_file *file1, const struct fat_file *file2); +int fat_fstat(struct fat_file *file, struct fat_direntry *entry); +int fat_open(const struct fat_file *parent, long startcluster, + struct fat_file *file); +int fat_open_rootdir(IF_MV(int volume,) struct fat_file *dir); +enum fat_remove_op /* what should fat_remove(), remove? */ +{ + FAT_RM_DIRENTRIES = 0x1, /* remove only directory entries */ + FAT_RM_DATA = 0x2, /* remove only file data */ + FAT_RM_ALL = 0x3, /* remove all of above */ +}; +int fat_remove(struct fat_file *file, enum fat_remove_op what); +int fat_rename(struct fat_file *parent, struct fat_file *file, + const unsigned char *newname); -extern void fat_init(void); -extern int fat_get_bytes_per_sector(IF_MV_NONVOID(int volume)); -extern int fat_mount(IF_MV(int volume,) IF_MD(int drive,) long startsector); -extern int fat_unmount(int volume, bool flush); -extern void fat_size(IF_MV(int volume,) /* public for info */ - unsigned long* size, - unsigned long* free); -extern void fat_recalc_free(IF_MV_NONVOID(int volume)); /* public for debug info screen */ -extern int fat_create_dir(const char* name, - struct fat_dir* newdir, - struct fat_dir* dir); -extern int fat_open(IF_MV(int volume,) - long cluster, - struct fat_file* ent, - const struct fat_dir* dir); -extern int fat_create_file(const char* name, - struct fat_file* ent, - struct fat_dir* dir); -extern long fat_readwrite(struct fat_file *ent, long sectorcount, - void* buf, bool write ); -extern int fat_closewrite(struct fat_file *ent, long size, int attr); -extern int fat_seek(struct fat_file *ent, unsigned long sector ); -extern int fat_remove(struct fat_file *ent); -extern int fat_truncate(const struct fat_file *ent); -extern int fat_rename(struct fat_file* file, - struct fat_dir* dir, - const unsigned char* newname, - long size, int attr); - -extern int fat_opendir(IF_MV(int volume,) - struct fat_dir *ent, unsigned long startcluster, - const struct fat_dir *parent_dir); -extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry); -extern unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)); /* public for debug info screen */ -extern bool fat_ismounted(int volume); -extern void* fat_get_sector_buffer(void); -extern void fat_release_sector_buffer(void); +/** File stream functions **/ +int fat_closewrite(struct fat_filestr *filestr, uint32_t size, + struct fat_direntry *fatentp); +void fat_filestr_init(struct fat_filestr *filestr, struct fat_file *file); +unsigned long fat_query_sectornum(const struct fat_filestr *filestr); +long fat_readwrite(struct fat_filestr *filestr, unsigned long sectorcount, + void *buf, bool write); +void fat_rewind(struct fat_filestr *filestr); +int fat_seek(struct fat_filestr *filestr, unsigned long sector); +int fat_truncate(const struct fat_filestr *filestr); -#endif +/** Directory stream functions **/ +struct filestr_cache; +int fat_readdir(struct fat_filestr *dirstr, struct fat_dirscan_info *scan, + struct filestr_cache *cachep, struct fat_direntry *entry); +void fat_rewinddir(struct fat_dirscan_info *scan); + +/** Mounting and unmounting functions **/ +bool fat_ismounted(IF_MV_NONVOID(int volume)); +int fat_mount(IF_MV(int volume,) IF_MD(int drive,) unsigned long startsector); +int fat_unmount(IF_MV_NONVOID(int volume)); + +/** Debug screen stuff **/ +#ifdef MAX_LOG_SECTOR_SIZE +int fat_get_bytes_per_sector(IF_MV_NONVOID(int volume)); +#endif /* MAX_LOG_SECTOR_SIZE */ +unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)); +void fat_recalc_free(IF_MV_NONVOID(int volume)); +bool fat_size(IF_MV(int volume,) unsigned long *size, unsigned long *free); + +/** Misc. **/ +time_t fattime_mktime(uint16_t fatdate, uint16_t fattime); +void fat_empty_fat_direntry(struct fat_direntry *entry); +void fat_init(void); + +#endif /* FAT_H */ -- cgit v1.2.3