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/include/file_internal.h | 371 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 371 insertions(+) create mode 100644 firmware/include/file_internal.h (limited to 'firmware/include/file_internal.h') diff --git a/firmware/include/file_internal.h b/firmware/include/file_internal.h new file mode 100644 index 0000000000..d1bb67406a --- /dev/null +++ b/firmware/include/file_internal.h @@ -0,0 +1,371 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2014 by Michael Sevakis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef _FILE_INTERNAL_H_ +#define _FILE_INTERNAL_H_ + +#include +#include +#include "mv.h" +#include "linked_list.h" +#include "mutex.h" +#include "mrsw_lock.h" +#include "fs_attr.h" +#include "fat.h" +#ifdef HAVE_DIRCACHE +#include "dircache.h" +#endif + +/** Tuneable parameters **/ + +/* limits for number of open descriptors - if you increase these values, make + certain that the disk cache has enough available buffers */ +#define MAX_OPEN_FILES 11 +#define MAX_OPEN_DIRS 12 +#define MAX_OPEN_HANDLES (MAX_OPEN_FILES+MAX_OPEN_DIRS) + +/* internal functions open streams as well; make sure they don't fail if all + user descs are busy; this needs to be at least the greatest quantity needed + at once by all internal functions */ +#ifdef HAVE_DIRCACHE +#define AUX_FILEOBJS 3 +#else +#define AUX_FILEOBJS 2 +#endif + +/* number of components statically allocated to handle the vast majority + of path depths; should maybe be tuned for >= 90th percentile but for now, + imma just guessing based on something like: + root + 'Music' + 'Artist' + 'Album' + 'Disc N' + filename */ +#define STATIC_PATHCOMP_NUM 6 + +#define MAX_NAME 255 + +/* unsigned value that will also hold the off_t range we need without + overflow */ +#define file_size_t uint32_t + +#ifdef __USE_FILE_OFFSET64 +/* if we want, we can deal with files up to 2^32-1 bytes-- the full FAT16/32 + range */ +#define FILE_SIZE_MAX (0xffffffffu) +#else +/* file contents and size will be preserved by the APIs so long as ftruncate() + isn't used; bytes passed 2^31-1 will not accessible nor will writes succeed + that would extend the file beyond the max for a 32-bit off_t */ +#define FILE_SIZE_MAX (0x7fffffffu) +#endif + +/* if file is "large(ish)", then get rid of the contents now rather than + lazily when the file is synced or closed in order to free-up space */ +#define O_TRUNC_THRESH 65536 + +/* default attributes when creating new files and directories */ +#define ATTR_NEW_FILE (ATTR_ARCHIVE) +#define ATTR_NEW_DIRECTORY (ATTR_DIRECTORY) + +#define ATTR_MOUNT_POINT (ATTR_VOLUME | ATTR_DIRECTORY) + +/** File sector cache **/ + +enum filestr_cache_flags +{ + FSC_DIRTY = 0x1, /* buffer is dirty (needs writeback) */ + FSC_NEW = 0x2, /* buffer is new (never yet written) */ +}; + +struct filestr_cache +{ + uint8_t *buffer; /* buffer to hold sector */ + unsigned long sector; /* file sector that is in buffer */ + unsigned int flags; /* FSC_* bits */ +}; + +void file_cache_init(struct filestr_cache *cachep); +void file_cache_reset(struct filestr_cache *cachep); +void file_cache_alloc(struct filestr_cache *cachep); +void file_cache_free(struct filestr_cache *cachep); + + +/** Common bitflags used throughout **/ + +/* bitflags used by open files and descriptors */ +enum fildes_and_obj_flags +{ + /* used in descriptor and common */ + FDO_BUSY = 0x0001, /* descriptor/object is in use */ + /* only used in individual stream descriptor */ + FD_WRITE = 0x0002, /* descriptor has write mode */ + FD_WRONLY = 0x0004, /* descriptor is write mode only */ + FD_APPEND = 0x0008, /* descriptor is append mode */ + /* only used as common flags */ + FO_DIRECTORY = 0x0010, /* fileobj is a directory */ + FO_TRUNC = 0x0020, /* fileobj is opened to be truncated */ + FO_REMOVED = 0x0040, /* fileobj was deleted while open */ + FO_SINGLE = 0x0080, /* fileobj has only one stream open */ + FDO_MASK = 0x00ff, + /* bitflags that instruct various 'open' functions how to behave */ + FF_FILE = 0x0000, /* expect file; accept file only */ + FF_DIR = 0x0100, /* expect dir; accept dir only */ + FF_ANYTYPE = 0x0200, /* succeed if either file or dir */ + FF_TYPEMASK = 0x0300, /* mask of typeflags */ + FF_CREAT = 0x0400, /* create if file doesn't exist */ + FF_EXCL = 0x0800, /* fail if creating and file exists */ + FF_CHECKPREFIX = 0x1000, /* detect if file is prefix of path */ + FF_NOISO = 0x2000, /* do not decode ISO filenames to UTF-8 */ + FF_MASK = 0x3f00, + /* special values used in isolation */ + FV_NONEXIST = 0x8000, /* closed but not freed (unmounted) */ + FV_OPENSYSROOT = 0xc001, /* open sysroot, volume 0 not mounted */ +}; + + +/** Common data structures used throughout **/ + +/* basic file information about its location */ +struct file_base_info +{ + union { +#ifdef HAVE_MULTIVOLUME + int volume; /* file's volume (overlaps fatfile.volume) */ +#endif +#if CONFIG_PLATFORM & PLATFORM_NATIVE + struct fat_file fatfile; /* FS driver file info */ +#endif + }; +#ifdef HAVE_DIRCACHE + struct dircache_file dcfile; /* dircache file info */ +#endif +}; + +#define BASEINFO_VOL(infop) \ + IF_MV_VOL((infop)->volume) + +/* open files binding item */ +struct file_base_binding +{ + struct ll_node node; /* list item node (first!) */ + struct file_base_info info; /* basic file info */ +}; + +#define BASEBINDING_VOL(bindp) \ + BASEINFO_VOL(&(bindp)->info) + +/* directory scanning position info */ +struct dirscan_info +{ +#if CONFIG_PLATFORM & PLATFORM_NATIVE + struct fat_dirscan_info fatscan; /* FS driver scan info */ +#endif +#ifdef HAVE_DIRCACHE + struct dircache_file dcscan; /* dircache scan info */ +#endif +}; + +/* describes the file as an open stream */ +struct filestr_base +{ + struct ll_node node; /* list item node (first!) */ + uint16_t flags; /* FD_* bits of this stream */ + uint16_t unused; /* not used */ + struct filestr_cache cache; /* stream-local cache */ + struct filestr_cache *cachep; /* the cache in use (local or shared) */ + struct file_base_info *infop; /* base file information */ + struct fat_filestr fatstr; /* FS driver information */ + struct file_base_binding *bindp; /* common binding for file/dir */ + struct mutex *mtx; /* serialization for this stream */ +}; + +void filestr_base_init(struct filestr_base *stream); +void filestr_base_destroy(struct filestr_base *stream); +void filestr_alloc_cache(struct filestr_base *stream); +void filestr_free_cache(struct filestr_base *stream); +void filestr_assign_cache(struct filestr_base *stream, + struct filestr_cache *cachep); +void filestr_copy_cache(struct filestr_base *stream, + struct filestr_cache *cachep); +void filestr_discard_cache(struct filestr_base *stream); + +/* allocates a cache buffer if needed and returns the cache pointer */ +static inline struct filestr_cache * +filestr_get_cache(struct filestr_base *stream) +{ + struct filestr_cache *cachep = stream->cachep; + + if (!cachep->buffer) + filestr_alloc_cache(stream); + + return cachep; +} + +static inline void filestr_lock(struct filestr_base *stream) +{ + mutex_lock(stream->mtx); +} + +static inline void filestr_unlock(struct filestr_base *stream) +{ + mutex_unlock(stream->mtx); +} + +/* stream lock doesn't have to be used if getting RW lock writer access */ +#define FILESTR_WRITER 0 +#define FILESTR_READER 1 + +#define FILESTR_LOCK(type, stream) \ + ({ if (FILESTR_##type) filestr_lock(stream); }) + +#define FILESTR_UNLOCK(type, stream) \ + ({ if (FILESTR_##type) filestr_unlock(stream); }) + +#define ATTR_PREFIX (0x8000) /* out of the way of all ATTR_* bits */ + +/* structure to return detailed information about what you opened */ +struct path_component_info +{ + const char *name; /* pointer to name within 'path' */ + size_t length; /* length of component within 'path' */ + file_size_t filesize; /* size of the opened file (0 if dir) */ + unsigned int attr; /* attributes of this component */ + struct file_base_info *prefixp; /* base info to check as prefix (IN) */ + struct file_base_info parentinfo; /* parent directory info of file */ +}; + +int open_stream_internal(const char *path, unsigned int callflags, + struct filestr_base *stream, + struct path_component_info *compinfo); +int close_stream_internal(struct filestr_base *stream); +int create_stream_internal(struct file_base_info *parentinfop, + const char *basename, size_t length, + unsigned int attr, unsigned int callflags, + struct filestr_base *stream); +int remove_stream_internal(const char *path, struct filestr_base *stream, + unsigned int callflags); +int test_stream_exists_internal(const char *path, unsigned int callflags); + +int open_noiso_internal(const char *path, int oflag); /* file.c */ + +struct dirent; +int uncached_readdir_dirent(struct filestr_base *stream, + struct dirscan_info *scanp, + struct dirent *entry); +void uncached_rewinddir_dirent(struct dirscan_info *scanp); + +int uncached_readdir_internal(struct filestr_base *stream, + struct file_base_info *infop, + struct fat_direntry *fatent); +void uncached_rewinddir_internal(struct file_base_info *infop); + +int test_dir_empty_internal(struct filestr_base *stream); + +struct dirinfo_internal +{ + unsigned int attr; + file_size_t size; + uint16_t wrtdate; + uint16_t wrttime; +}; + +/** Synchronization used throughout **/ + +/* acquire the filesystem lock as READER */ +static inline void file_internal_lock_READER(void) +{ + extern struct mrsw_lock file_internal_mrsw; + mrsw_read_acquire(&file_internal_mrsw); +} + +/* release the filesystem lock as READER */ +static inline void file_internal_unlock_READER(void) +{ + extern struct mrsw_lock file_internal_mrsw; + mrsw_read_release(&file_internal_mrsw); +} + +/* acquire the filesystem lock as WRITER */ +static inline void file_internal_lock_WRITER(void) +{ + extern struct mrsw_lock file_internal_mrsw; + mrsw_write_acquire(&file_internal_mrsw); +} + +/* release the filesystem lock as WRITER */ +static inline void file_internal_unlock_WRITER(void) +{ + extern struct mrsw_lock file_internal_mrsw; + mrsw_write_release(&file_internal_mrsw); +} + +#define ERRNO 0 /* maintain errno value */ +#define RC 0 /* maintain rc value */ + +/* NOTES: if _errno is a non-constant expression, it must set an error + * number and not return the ERRNO constant which will merely set + * errno to zero, not preserve the current value; if you must set + * errno to zero, set it explicitly, not in the macro + * + * if _rc is constant-expression evaluation to 'RC', then rc will + * NOT be altered; i.e. if you must set rc to zero, set it explicitly, + * not in the macro + */ + +/* set errno and rc and proceed to the "file_error:" label */ +#define FILE_ERROR(_errno, _rc) \ + ({ __builtin_constant_p(_errno) ? \ + ({ if ((_errno) != ERRNO) errno = (_errno); }) : \ + ({ errno = (_errno); }); \ + __builtin_constant_p(_rc) ? \ + ({ if ((_rc) != RC) rc = (_rc); }) : \ + ({ rc = (_rc); }); \ + goto file_error; }) + +/* set errno and return a value at the point of invocation */ +#define FILE_ERROR_RETURN(_errno, _rc...) \ + ({ __builtin_constant_p(_errno) ? \ + ({ if ((_errno) != ERRNO) errno = (_errno); }) : \ + ({ errno = (_errno); }); \ + return _rc; }) + + +/** Misc. stuff **/ + +/* iterate through all the volumes if volume < 0, else just the given volume */ +#define FOR_EACH_VOLUME(volume, i) \ + for (int i = (IF_MV_VOL(volume) >= 0 ? IF_MV_VOL(volume) : 0), \ + _end = (IF_MV_VOL(volume) >= 0 ? i : NUM_VOLUMES-1); \ + i <= _end; i++) + +/* return a pointer to the static struct fat_direntry */ +static inline struct fat_direntry *get_dir_fatent(void) +{ + extern struct fat_direntry dir_fatent; + return &dir_fatent; +} + +void iso_decode_d_name(char *d_name); + +#ifdef HAVE_DIRCACHE +void empty_dirent(struct dirent *entry); +void fill_dirinfo_native(struct dirinfo_native *din); +#endif /* HAVE_DIRCACHE */ + +void filesystem_init(void) INIT_ATTR; + +#endif /* _FILE_INTERNAL_H_ */ -- cgit v1.2.3