summaryrefslogtreecommitdiff
path: root/apps/main.c
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2013-08-05 22:02:45 -0400
committerMichael Sevakis <jethead71@rockbox.org>2014-08-30 03:48:23 +0200
commit7d1a47cf13726c95ac46027156cc12dd9da5b855 (patch)
treeeb20d07656806479a8e1fea25887a490ea30d1d8 /apps/main.c
parent95a4c3afcd53a1f8b835dec33de51f9c304de4d9 (diff)
downloadrockbox-7d1a47cf13726c95ac46027156cc12dd9da5b855.tar.gz
rockbox-7d1a47cf13726c95ac46027156cc12dd9da5b855.zip
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>
Diffstat (limited to 'apps/main.c')
-rw-r--r--apps/main.c102
1 files changed, 42 insertions, 60 deletions
diff --git a/apps/main.c b/apps/main.c
index 6c6f0d6aba..9098180fb8 100644
--- a/apps/main.c
+++ b/apps/main.c
@@ -19,11 +19,12 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21#include "config.h" 21#include "config.h"
22#include "system.h"
22 23
23#include "gcc_extensions.h" 24#include "gcc_extensions.h"
24#include "storage.h" 25#include "storage.h"
25#include "disk.h" 26#include "disk.h"
26#include "fat.h" 27#include "file_internal.h"
27#include "lcd.h" 28#include "lcd.h"
28#include "rtc.h" 29#include "rtc.h"
29#include "debug.h" 30#include "debug.h"
@@ -34,7 +35,6 @@
34#include "filetypes.h" 35#include "filetypes.h"
35#include "panic.h" 36#include "panic.h"
36#include "menu.h" 37#include "menu.h"
37#include "system.h"
38#include "usb.h" 38#include "usb.h"
39#include "powermgmt.h" 39#include "powermgmt.h"
40#include "adc.h" 40#include "adc.h"
@@ -203,80 +203,53 @@ int main(void)
203 root_menu(); 203 root_menu();
204} 204}
205 205
206static int init_dircache(bool preinit) INIT_ATTR;
207static int init_dircache(bool preinit)
208{
209#ifdef HAVE_DIRCACHE 206#ifdef HAVE_DIRCACHE
210 int result = 0; 207static int INIT_ATTR init_dircache(bool preinit)
211 bool clear = false; 208{
212
213 if (preinit) 209 if (preinit)
214 dircache_init(); 210 dircache_init(MAX(global_status.dircache_size, 0));
215 211
216 if (!global_settings.dircache) 212 if (!global_settings.dircache)
217 return 0; 213 return -1;
214
215 int result = -1;
218 216
219# ifdef HAVE_EEPROM_SETTINGS 217#ifdef HAVE_EEPROM_SETTINGS
220 if (firmware_settings.initialized && firmware_settings.disk_clean 218 if (firmware_settings.initialized &&
221 && preinit) 219 firmware_settings.disk_clean &&
220 preinit)
222 { 221 {
223 result = dircache_load(); 222 result = dircache_load();
224
225 if (result < 0) 223 if (result < 0)
226 {
227 firmware_settings.disk_clean = false; 224 firmware_settings.disk_clean = false;
228 if (global_status.dircache_size <= 0)
229 {
230 /* This will be in default language, settings are not
231 applied yet. Not really any easy way to fix that. */
232 splash(0, str(LANG_SCANNING_DISK));
233 clear = true;
234 }
235
236 dircache_build(global_status.dircache_size);
237 }
238 } 225 }
239 else 226 else
240# endif 227#endif /* HAVE_EEPROM_SETTINGS */
228 if (!preinit)
241 { 229 {
242 if (preinit) 230 result = dircache_enable();
243 return -1; 231 if (result != 0)
244
245 if (!dircache_is_enabled()
246 && !dircache_is_initializing())
247 { 232 {
248 if (global_status.dircache_size <= 0) 233 if (result > 0)
249 { 234 {
235 /* Print "Scanning disk..." to the display. */
250 splash(0, str(LANG_SCANNING_DISK)); 236 splash(0, str(LANG_SCANNING_DISK));
251 clear = true; 237 dircache_wait();
238 backlight_on();
239 show_logo();
252 } 240 }
253 result = dircache_build(global_status.dircache_size);
254 }
255 241
256 if (result < 0) 242 struct dircache_info info;
257 { 243 dircache_get_info(&info);
258 /* Initialization of dircache failed. Manual action is 244 global_status.dircache_size = info.size;
259 * necessary to enable dircache again. 245 status_save();
260 */
261 splashf(0, "Dircache failed, disabled. Result: %d", result);
262 global_settings.dircache = false;
263 } 246 }
264 } 247 /* else don't wait or already enabled by load */
265
266 if (clear)
267 {
268 backlight_on();
269 show_logo();
270 global_status.dircache_size = dircache_get_cache_size();
271 status_save();
272 } 248 }
273 249
274 return result; 250 return result;
275#else
276 (void)preinit;
277 return 0;
278#endif
279} 251}
252#endif /* HAVE_DIRCACHE */
280 253
281#ifdef HAVE_TAGCACHE 254#ifdef HAVE_TAGCACHE
282static void init_tagcache(void) INIT_ATTR; 255static void init_tagcache(void) INIT_ATTR;
@@ -363,6 +336,7 @@ static void init(void)
363 button_init(); 336 button_init();
364 powermgmt_init(); 337 powermgmt_init();
365 backlight_init(); 338 backlight_init();
339 unicode_init();
366#ifdef SIMULATOR 340#ifdef SIMULATOR
367 sim_tasks_init(); 341 sim_tasks_init();
368#endif 342#endif
@@ -392,8 +366,10 @@ static void init(void)
392 settings_reset(); 366 settings_reset();
393 settings_load(SETTINGS_ALL); 367 settings_load(SETTINGS_ALL);
394 settings_apply(true); 368 settings_apply(true);
369#ifdef HAVE_DIRCACHE
395 init_dircache(true); 370 init_dircache(true);
396 init_dircache(false); 371 init_dircache(false);
372#endif
397#ifdef HAVE_TAGCACHE 373#ifdef HAVE_TAGCACHE
398 init_tagcache(); 374 init_tagcache();
399#endif 375#endif
@@ -429,6 +405,8 @@ static void init(void)
429 405
430#else 406#else
431 407
408#include "errno.h"
409
432static void init(void) INIT_ATTR; 410static void init(void) INIT_ATTR;
433static void init(void) 411static void init(void)
434{ 412{
@@ -443,6 +421,9 @@ static void init(void)
443 core_allocator_init(); 421 core_allocator_init();
444 kernel_init(); 422 kernel_init();
445 423
424 /* early early early! */
425 filesystem_init();
426
446#ifdef HAVE_ADJUSTABLE_CPU_FREQ 427#ifdef HAVE_ADJUSTABLE_CPU_FREQ
447 set_cpu_frequency(CPUFREQ_NORMAL); 428 set_cpu_frequency(CPUFREQ_NORMAL);
448#ifdef CPU_COLDFIRE 429#ifdef CPU_COLDFIRE
@@ -462,6 +443,7 @@ static void init(void)
462 /* current_tick should be ticking by now */ 443 /* current_tick should be ticking by now */
463 CHART("ticking"); 444 CHART("ticking");
464 445
446 unicode_init();
465 lcd_init(); 447 lcd_init();
466#ifdef HAVE_REMOTE_LCD 448#ifdef HAVE_REMOTE_LCD
467 lcd_remote_init(); 449 lcd_remote_init();
@@ -558,8 +540,6 @@ static void init(void)
558 } 540 }
559#endif 541#endif
560 542
561
562 disk_init_subsystem();
563 CHART(">storage_init"); 543 CHART(">storage_init");
564 rc = storage_init(); 544 rc = storage_init();
565 CHART("<storage_init"); 545 CHART("<storage_init");
@@ -661,22 +641,24 @@ static void init(void)
661 CHART("<settings_load(ALL)"); 641 CHART("<settings_load(ALL)");
662 } 642 }
663 643
644#ifdef HAVE_DIRCACHE
664 CHART(">init_dircache(true)"); 645 CHART(">init_dircache(true)");
665 rc = init_dircache(true); 646 rc = init_dircache(true);
666 CHART("<init_dircache(true)"); 647 CHART("<init_dircache(true)");
667 if (rc < 0)
668 {
669#ifdef HAVE_TAGCACHE 648#ifdef HAVE_TAGCACHE
649 if (rc < 0)
670 remove(TAGCACHE_STATEFILE); 650 remove(TAGCACHE_STATEFILE);
671#endif 651#endif /* HAVE_TAGCACHE */
672 } 652#endif /* HAVE_DIRCACHE */
673 653
674 CHART(">settings_apply(true)"); 654 CHART(">settings_apply(true)");
675 settings_apply(true); 655 settings_apply(true);
676 CHART("<settings_apply(true)"); 656 CHART("<settings_apply(true)");
657#ifdef HAVE_DIRCACHE
677 CHART(">init_dircache(false)"); 658 CHART(">init_dircache(false)");
678 init_dircache(false); 659 init_dircache(false);
679 CHART("<init_dircache(false)"); 660 CHART("<init_dircache(false)");
661#endif
680#ifdef HAVE_TAGCACHE 662#ifdef HAVE_TAGCACHE
681 CHART(">init_tagcache"); 663 CHART(">init_tagcache");
682 init_tagcache(); 664 init_tagcache();