ZeXFS filesystem was completely rewritten, brings stability, great recoverable space...
[ZeXOS.git] / kernel / core / fs.c
bloba2ce09c4f31c7a060f03587350a8a79a0859db69
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <build.h>
22 #include <system.h>
23 #include <string.h>
24 #include <partition.h>
25 #include <dev.h>
26 #include <fs.h>
27 #include <config.h>
29 fs_t fs_list;
31 static unsigned char tolower (char c)
33 return ((c >= 'A' && c <= 'Z') ? c + 32: c);
36 bool fs_list_add (char *name, fs_handler_t *handler)
38 unsigned name_len = strlen (name);
40 if (!name_len || !handler)
41 return 0;
43 /* alloc and init context */
44 fs_t *fs = (fs_t *) kmalloc (sizeof (fs_t));
46 if (!fs)
47 return 0;
49 memset (fs, 0, sizeof (fs_t));
51 fs->name = (char *) kmalloc (sizeof (char) * FS_NAME_LEN + 1);
53 if (!fs->name) {
54 kfree (fs);
55 return 0;
58 memset (fs->name, 0, FS_NAME_LEN);
59 memcpy (fs->name, name, name_len);
60 fs->name[name_len] = '\0';
62 fs->handler = handler;
64 /* add into list */
65 fs->next = &fs_list;
66 fs->prev = fs_list.prev;
67 fs->prev->next = fs;
68 fs->next->prev = fs;
70 handler (FS_ACT_INIT, (char *) NULL, NULL, NULL);
72 return 1;
75 bool fs_list_del (fs_t *fs)
77 if (fs) {
78 fs->next->prev = fs->prev;
79 fs->prev->next = fs->next;
81 //kfree (fs);
82 return 1;
85 return 0;
88 fs_t *fs_supported (char *name)
90 fs_t *fs;
91 for (fs = fs_list.next; fs != &fs_list; fs = fs->next) {
92 if (!strncmp (fs->name, name, strlen (fs->name)))
93 return fs;
96 return 0;
99 extern partition_t partition_list;
100 fs_t *fs_detect (partition_t *p)
102 if (!p)
103 return 0;
105 fs_t *fs;
107 char block[512];
109 dev_t *dev = (dev_t *) dev_findbypartition (p);
111 if (!dev)
112 return 0;
115 dev->handler (DEV_ACT_READ, p, block, "", 63);
117 char fstype[11];
119 int c;
120 for (c = 54; c < 64; c ++)
121 fstype[c-54] = block[c];
123 memset (block, 0, sizeof (block));
125 for (c = 0; c < 10; c ++)
126 block[c] = tolower (fstype[c]);
128 fs = fs_supported (block);
130 return fs;
133 extern bool fat_handler (unsigned act, char *block, unsigned n, unsigned long l);
134 extern bool fat16_handler (unsigned act, char *block, unsigned n, unsigned long l);
135 extern bool zexfs_handler (unsigned act, char *block, unsigned n, unsigned long l);
136 extern bool isofs_handler (unsigned act, char *block, unsigned n, unsigned long l);
137 extern bool znfs_handler (unsigned act, char *block, unsigned n, unsigned long l);
138 #ifdef CONFIG_DRV_EXT2
139 extern bool ext2_handler (unsigned act, char *block, unsigned n, unsigned long l);
140 #endif
141 unsigned int init_fs ()
143 fs_list.next = &fs_list;
144 fs_list.prev = &fs_list;
146 file_cache = (unsigned char *) 0x350000; // file_cache store address
147 file_cache_id = 0;
149 #ifdef ARCH_i386
150 #ifdef CONFIG_DRV_ISOFS
151 fs_list_add ("isofs", &isofs_handler);
152 #endif
154 #ifdef CONFIG_DRV_ZEXFS
155 fs_list_add ("zexfs", &zexfs_handler);
156 #endif
158 #ifdef CONFIG_DRV_EXT2
159 fs_list_add ("ext2", &ext2_handler);
160 #endif
162 #ifdef CONFIG_DRV_FAT16
163 fs_list_add ("fat16", &fat16_handler);
164 #endif
166 #ifdef CONFIG_DRV_FAT12
167 fs_list_add ("fat12", &fat_handler);
168 #endif
170 fs_list_add ("znfs", &znfs_handler);
171 #endif
172 return 1;