rockbox/firmware/common/fileobj_mgr.c

397 lines
12 KiB
C
Raw Normal View History

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 <jethead71@rockbox.org> Tested: Michael Sevakis <jethead71@rockbox.org>
2013-08-06 02:02:45 +00:00
/***************************************************************************
* __________ __ ___.
* 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.
*
****************************************************************************/
#include "config.h"
#include "system.h"
#include "debug.h"
#include "file.h"
#include "dir.h"
#include "disk_cache.h"
#include "fileobj_mgr.h"
#include "dircache_redirect.h"
/**
* Manages file and directory streams on all volumes
*
* Intended for internal use by disk, file and directory code
*/
/* there will always be enough of these for all user handles, thus these
functions don't return failure codes */
#define MAX_FILEOBJS (MAX_OPEN_HANDLES + AUX_FILEOBJS)
/* describes the file as an image on the storage medium */
static struct fileobj_binding
{
struct file_base_binding bind; /* base info list item (first!) */
uint16_t flags; /* F(D)(O)_* bits of this file/dir */
uint16_t writers; /* number of writer streams */
struct filestr_cache cache; /* write mode shared cache */
file_size_t size; /* size of this file */
struct ll_head list; /* open streams for this file/dir */
} fobindings[MAX_FILEOBJS];
static struct mutex stream_mutexes[MAX_FILEOBJS] SHAREDBSS_ATTR;
static struct ll_head free_bindings;
static struct ll_head busy_bindings[NUM_VOLUMES];
#define BUSY_BINDINGS(volume) \
(&busy_bindings[IF_MV_VOL(volume)])
#define BASEBINDING_LIST(bindp) \
(BUSY_BINDINGS(BASEBINDING_VOL(bindp)))
#define FREE_BINDINGS() \
(&free_bindings)
#define BINDING_FIRST(type, volume...) \
((struct fileobj_binding *)type##_BINDINGS(volume)->head)
#define BINDING_NEXT(fobp) \
((struct fileobj_binding *)(fobp)->bind.node.next)
#define FOR_EACH_BINDING(volume, fobp) \
for (struct fileobj_binding *fobp = BINDING_FIRST(BUSY, volume); \
fobp; fobp = BINDING_NEXT(fobp))
#define STREAM_FIRST(fobp) \
((struct filestr_base *)(fobp)->list.head)
#define STREAM_NEXT(s) \
((struct filestr_base *)(s)->node.next)
#define STREAM_THIS(s) \
(s)
#define FOR_EACH_STREAM(what, start, s) \
for (struct filestr_base *s = STREAM_##what(start); \
s; s = STREAM_NEXT(s))
/* syncs information for the stream's old and new parent directory if any are
currently opened */
static void fileobj_sync_parent(const struct file_base_info *infop[],
int count)
{
FOR_EACH_BINDING(infop[0]->volume, fobp)
{
if ((fobp->flags & (FO_DIRECTORY|FO_REMOVED)) != FO_DIRECTORY)
continue; /* not directory or removed can't be parent of anything */
struct filestr_base *parentstrp = STREAM_FIRST(fobp);
struct fat_file *parentfilep = &parentstrp->infop->fatfile;
for (int i = 0; i < count; i++)
{
if (!fat_dir_is_parent(parentfilep, &infop[i]->fatfile))
continue;
/* discard scan/read caches' parent dir info */
FOR_EACH_STREAM(THIS, parentstrp, s)
filestr_discard_cache(s);
}
}
}
/* see if this file has open streams and return that fileobj_binding if so,
else grab a new one from the free list; returns true if this stream is
the only open one */
static bool binding_assign(const struct file_base_info *srcinfop,
struct fileobj_binding **fobpp)
{
FOR_EACH_BINDING(srcinfop->fatfile.volume, fobp)
{
if (fobp->flags & FO_REMOVED)
continue;
if (fat_file_is_same(&srcinfop->fatfile, &fobp->bind.info.fatfile))
{
/* already has open streams */
*fobpp = fobp;
return false;
}
}
/* not found - allocate anew */
*fobpp = BINDING_FIRST(FREE);
ll_remove_first(FREE_BINDINGS());
ll_init(&(*fobpp)->list);
return true;
}
/* mark descriptor as unused and return to the free list */
static void binding_add_to_free_list(struct fileobj_binding *fobp)
{
fobp->flags = 0;
ll_insert_last(FREE_BINDINGS(), &fobp->bind.node);
}
/** File and directory internal interface **/
void file_binding_insert_last(struct file_base_binding *bindp)
{
ll_insert_last(BASEBINDING_LIST(bindp), &bindp->node);
}
void file_binding_remove(struct file_base_binding *bindp)
{
ll_remove(BASEBINDING_LIST(bindp), &bindp->node);
}
#ifdef HAVE_DIRCACHE
void file_binding_insert_first(struct file_base_binding *bindp)
{
ll_insert_first(BASEBINDING_LIST(bindp), &bindp->node);
}
void file_binding_remove_next(struct file_base_binding *prevp,
struct file_base_binding *bindp)
{
ll_remove_next(BASEBINDING_LIST(bindp), &prevp->node);
(void)bindp;
}
#endif /* HAVE_DIRCACHE */
/* opens the file object for a new stream and sets up the caches;
* the stream must already be opened at the FS driver level and *stream
* initialized.
*
* NOTE: switches stream->infop to the one kept in common for all streams of
* the same file, making a copy for only the first stream
*/
void fileobj_fileop_open(struct filestr_base *stream,
const struct file_base_info *srcinfop,
unsigned int callflags)
{
struct fileobj_binding *fobp;
bool first = binding_assign(srcinfop, &fobp);
/* add stream to this file's list */
ll_insert_last(&fobp->list, &stream->node);
/* initiate the new stream into the enclave */
stream->flags = FDO_BUSY | (callflags & (FD_WRITE|FD_WRONLY|FD_APPEND));
stream->infop = &fobp->bind.info;
stream->fatstr.fatfilep = &fobp->bind.info.fatfile;
stream->bindp = &fobp->bind;
stream->mtx = &stream_mutexes[fobp - fobindings];
if (first)
{
/* first stream for file */
fobp->bind.info = *srcinfop;
fobp->flags = FDO_BUSY | FO_SINGLE |
(callflags & (FO_DIRECTORY|FO_TRUNC));
fobp->writers = 0;
fobp->size = 0;
if (callflags & FD_WRITE)
{
/* first one is a writer */
fobp->writers = 1;
file_cache_init(&fobp->cache);
filestr_assign_cache(stream, &fobp->cache);
}
fileobj_bind_file(&fobp->bind);
}
else
{
/* additional stream for file */
fobp->flags &= ~FO_SINGLE;
fobp->flags |= callflags & FO_TRUNC;
/* once a file/directory, always a file/directory; such a change
is a bug */
if ((callflags ^ fobp->flags) & FO_DIRECTORY)
{
DEBUGF("%s - FO_DIRECTORY flag does not match: %p %u\n",
__func__, stream, callflags);
}
if (fobp->writers)
{
/* already writers present */
fobp->writers++;
filestr_assign_cache(stream, &fobp->cache);
}
else if (callflags & FD_WRITE)
{
/* first writer */
fobp->writers = 1;
file_cache_init(&fobp->cache);
FOR_EACH_STREAM(FIRST, fobp, s)
filestr_assign_cache(s, &fobp->cache);
}
/* else another reader */
}
}
/* close the stream and free associated resources */
void fileobj_fileop_close(struct filestr_base *stream)
{
switch (stream->flags)
{
case 0: /* not added to manager */
case FV_NONEXIST: /* forced-closed by unmounting */
filestr_base_destroy(stream);
return;
}
struct fileobj_binding *fobp = (struct fileobj_binding *)stream->bindp;
unsigned int foflags = fobp->flags;
ll_remove(&fobp->list, &stream->node);
if ((foflags & FO_SINGLE) || fobp->writers == 0)
{
if (foflags & FO_SINGLE)
{
/* last stream for file; close everything */
fileobj_unbind_file(&fobp->bind);
if (fobp->writers)
file_cache_free(&fobp->cache);
binding_add_to_free_list(fobp);
}
}
else if ((stream->flags & FD_WRITE) && --fobp->writers == 0)
{
/* only readers remain; switch back to stream-local caching */
FOR_EACH_STREAM(FIRST, fobp, s)
filestr_copy_cache(s, &fobp->cache);
file_cache_free(&fobp->cache);
}
if (!(foflags & FO_SINGLE) && fobp->list.head == fobp->list.tail)
fobp->flags |= FO_SINGLE; /* only one open stream remaining */
filestr_base_destroy(stream);
}
/* informs manager that file has been created */
void fileobj_fileop_create(struct filestr_base *stream,
const struct file_base_info *srcinfop,
unsigned int callflags)
{
fileobj_fileop_open(stream, srcinfop, callflags);
fileobj_sync_parent((const struct file_base_info *[]){ stream->infop }, 1);
}
/* informs manager that file has been removed */
void fileobj_fileop_remove(struct filestr_base *stream,
const struct file_base_info *oldinfop)
{
((struct fileobj_binding *)stream->bindp)->flags |= FO_REMOVED;
fileobj_sync_parent((const struct file_base_info *[]){ oldinfop }, 1);
}
/* informs manager that file has been renamed */
void fileobj_fileop_rename(struct filestr_base *stream,
const struct file_base_info *oldinfop)
{
/* if there is old info then this was a move and the old parent has to be
informed */
int count = oldinfop ? 2 : 1;
fileobj_sync_parent(&(const struct file_base_info *[])
{ oldinfop, stream->infop }[2 - count],
count);
}
/* informs manager than directory entries have been updated */
void fileobj_fileop_sync(struct filestr_base *stream)
{
fileobj_sync_parent((const struct file_base_info *[]){ stream->infop }, 1);
}
/* inform manager that file has been truncated */
void fileobj_fileop_truncate(struct filestr_base *stream)
{
/* let caller update internal info */
FOR_EACH_STREAM(FIRST, (struct fileobj_binding *)stream->bindp, s)
ftruncate_internal_callback(stream, s);
}
/* query for the pointer to the size storage for the file object */
file_size_t * fileobj_get_sizep(const struct filestr_base *stream)
{
if (!stream->bindp)
return NULL;
return &((struct fileobj_binding *)stream->bindp)->size;
}
/* query manager bitflags for the file object */
unsigned int fileobj_get_flags(const struct filestr_base *stream)
{
if (!stream->bindp)
return 0;
return ((struct fileobj_binding *)stream->bindp)->flags;
}
/* change manager bitflags for the file object */
void fileobj_change_flags(struct filestr_base *stream,
unsigned int flags, unsigned int mask)
{
struct fileobj_binding *fobp = (struct fileobj_binding *)stream->bindp;
if (fobp)
fobp->flags = (fobp->flags & ~mask) | (flags & mask);
}
/* mark all open streams on a device as "nonexistant" */
void fileobj_mgr_unmount(IF_MV_NONVOID(int volume))
{
/* right now, there is nothing else to be freed when marking a descriptor
as "nonexistant" but a callback could be added if that changes */
FOR_EACH_VOLUME(volume, v)
{
struct fileobj_binding *fobp;
while ((fobp = BINDING_FIRST(BUSY, v)))
{
struct filestr_base *s;
while ((s = STREAM_FIRST(fobp)))
{
/* keep it "busy" to avoid races; any valid file/directory
descriptor returned by an open call should always be
closed by whomever opened it (of course!) */
fileop_onclose_internal(s);
s->flags = FV_NONEXIST;
}
}
}
}
/* one-time init at startup */
void fileobj_mgr_init(void)
{
for (unsigned int i = 0; i < NUM_VOLUMES; i++)
ll_init(BUSY_BINDINGS(i));
ll_init(FREE_BINDINGS());
for (unsigned int i = 0; i < MAX_FILEOBJS; i++)
{
mutex_init(&stream_mutexes[i]);
binding_add_to_free_list(&fobindings[i]);
}
}