aboutsummaryrefslogtreecommitdiffstats
path: root/fs/bfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/bfs')
-rw-r--r--fs/bfs/Makefile7
-rw-r--r--fs/bfs/bfs.h60
-rw-r--r--fs/bfs/dir.c362
-rw-r--r--fs/bfs/file.c162
-rw-r--r--fs/bfs/inode.c420
5 files changed, 1011 insertions, 0 deletions
diff --git a/fs/bfs/Makefile b/fs/bfs/Makefile
new file mode 100644
index 000000000000..c787b36d940c
--- /dev/null
+++ b/fs/bfs/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for BFS filesystem.
3#
4
5obj-$(CONFIG_BFS_FS) += bfs.o
6
7bfs-objs := inode.o file.o dir.o
diff --git a/fs/bfs/bfs.h b/fs/bfs/bfs.h
new file mode 100644
index 000000000000..1020dbc88bec
--- /dev/null
+++ b/fs/bfs/bfs.h
@@ -0,0 +1,60 @@
1/*
2 * fs/bfs/bfs.h
3 * Copyright (C) 1999 Tigran Aivazian <tigran@veritas.com>
4 */
5#ifndef _FS_BFS_BFS_H
6#define _FS_BFS_BFS_H
7
8#include <linux/bfs_fs.h>
9
10/*
11 * BFS file system in-core superblock info
12 */
13struct bfs_sb_info {
14 unsigned long si_blocks;
15 unsigned long si_freeb;
16 unsigned long si_freei;
17 unsigned long si_lf_ioff;
18 unsigned long si_lf_sblk;
19 unsigned long si_lf_eblk;
20 unsigned long si_lasti;
21 unsigned long * si_imap;
22 struct buffer_head * si_sbh; /* buffer header w/superblock */
23 struct bfs_super_block * si_bfs_sb; /* superblock in si_sbh->b_data */
24};
25
26/*
27 * BFS file system in-core inode info
28 */
29struct bfs_inode_info {
30 unsigned long i_dsk_ino; /* inode number from the disk, can be 0 */
31 unsigned long i_sblock;
32 unsigned long i_eblock;
33 struct inode vfs_inode;
34};
35
36static inline struct bfs_sb_info *BFS_SB(struct super_block *sb)
37{
38 return sb->s_fs_info;
39}
40
41static inline struct bfs_inode_info *BFS_I(struct inode *inode)
42{
43 return list_entry(inode, struct bfs_inode_info, vfs_inode);
44}
45
46
47#define printf(format, args...) \
48 printk(KERN_ERR "BFS-fs: %s(): " format, __FUNCTION__, ## args)
49
50
51/* file.c */
52extern struct inode_operations bfs_file_inops;
53extern struct file_operations bfs_file_operations;
54extern struct address_space_operations bfs_aops;
55
56/* dir.c */
57extern struct inode_operations bfs_dir_inops;
58extern struct file_operations bfs_dir_operations;
59
60#endif /* _FS_BFS_BFS_H */
diff --git a/fs/bfs/dir.c b/fs/bfs/dir.c
new file mode 100644
index 000000000000..5a1e5ce057ff
--- /dev/null
+++ b/fs/bfs/dir.c
@@ -0,0 +1,362 @@
1/*
2 * fs/bfs/dir.c
3 * BFS directory operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
5 */
6
7#include <linux/time.h>
8#include <linux/string.h>
9#include <linux/fs.h>
10#include <linux/smp_lock.h>
11#include <linux/buffer_head.h>
12#include <linux/sched.h>
13#include "bfs.h"
14
15#undef DEBUG
16
17#ifdef DEBUG
18#define dprintf(x...) printf(x)
19#else
20#define dprintf(x...)
21#endif
22
23static int bfs_add_entry(struct inode * dir, const char * name, int namelen, int ino);
24static struct buffer_head * bfs_find_entry(struct inode * dir,
25 const char * name, int namelen, struct bfs_dirent ** res_dir);
26
27static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
28{
29 struct inode * dir = f->f_dentry->d_inode;
30 struct buffer_head * bh;
31 struct bfs_dirent * de;
32 unsigned int offset;
33 int block;
34
35 lock_kernel();
36
37 if (f->f_pos & (BFS_DIRENT_SIZE-1)) {
38 printf("Bad f_pos=%08lx for %s:%08lx\n", (unsigned long)f->f_pos,
39 dir->i_sb->s_id, dir->i_ino);
40 unlock_kernel();
41 return -EBADF;
42 }
43
44 while (f->f_pos < dir->i_size) {
45 offset = f->f_pos & (BFS_BSIZE-1);
46 block = BFS_I(dir)->i_sblock + (f->f_pos >> BFS_BSIZE_BITS);
47 bh = sb_bread(dir->i_sb, block);
48 if (!bh) {
49 f->f_pos += BFS_BSIZE - offset;
50 continue;
51 }
52 do {
53 de = (struct bfs_dirent *)(bh->b_data + offset);
54 if (de->ino) {
55 int size = strnlen(de->name, BFS_NAMELEN);
56 if (filldir(dirent, de->name, size, f->f_pos, de->ino, DT_UNKNOWN) < 0) {
57 brelse(bh);
58 unlock_kernel();
59 return 0;
60 }
61 }
62 offset += BFS_DIRENT_SIZE;
63 f->f_pos += BFS_DIRENT_SIZE;
64 } while (offset < BFS_BSIZE && f->f_pos < dir->i_size);
65 brelse(bh);
66 }
67
68 unlock_kernel();
69 return 0;
70}
71
72struct file_operations bfs_dir_operations = {
73 .read = generic_read_dir,
74 .readdir = bfs_readdir,
75 .fsync = file_fsync,
76};
77
78extern void dump_imap(const char *, struct super_block *);
79
80static int bfs_create(struct inode * dir, struct dentry * dentry, int mode,
81 struct nameidata *nd)
82{
83 int err;
84 struct inode * inode;
85 struct super_block * s = dir->i_sb;
86 struct bfs_sb_info * info = BFS_SB(s);
87 unsigned long ino;
88
89 inode = new_inode(s);
90 if (!inode)
91 return -ENOSPC;
92 lock_kernel();
93 ino = find_first_zero_bit(info->si_imap, info->si_lasti);
94 if (ino > info->si_lasti) {
95 unlock_kernel();
96 iput(inode);
97 return -ENOSPC;
98 }
99 set_bit(ino, info->si_imap);
100 info->si_freei--;
101 inode->i_uid = current->fsuid;
102 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
103 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
104 inode->i_blocks = inode->i_blksize = 0;
105 inode->i_op = &bfs_file_inops;
106 inode->i_fop = &bfs_file_operations;
107 inode->i_mapping->a_ops = &bfs_aops;
108 inode->i_mode = mode;
109 inode->i_ino = ino;
110 BFS_I(inode)->i_dsk_ino = ino;
111 BFS_I(inode)->i_sblock = 0;
112 BFS_I(inode)->i_eblock = 0;
113 insert_inode_hash(inode);
114 mark_inode_dirty(inode);
115 dump_imap("create",s);
116
117 err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, inode->i_ino);
118 if (err) {
119 inode->i_nlink--;
120 mark_inode_dirty(inode);
121 iput(inode);
122 unlock_kernel();
123 return err;
124 }
125 unlock_kernel();
126 d_instantiate(dentry, inode);
127 return 0;
128}
129
130static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
131{
132 struct inode * inode = NULL;
133 struct buffer_head * bh;
134 struct bfs_dirent * de;
135
136 if (dentry->d_name.len > BFS_NAMELEN)
137 return ERR_PTR(-ENAMETOOLONG);
138
139 lock_kernel();
140 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
141 if (bh) {
142 unsigned long ino = le32_to_cpu(de->ino);
143 brelse(bh);
144 inode = iget(dir->i_sb, ino);
145 if (!inode) {
146 unlock_kernel();
147 return ERR_PTR(-EACCES);
148 }
149 }
150 unlock_kernel();
151 d_add(dentry, inode);
152 return NULL;
153}
154
155static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new)
156{
157 struct inode * inode = old->d_inode;
158 int err;
159
160 lock_kernel();
161 err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, inode->i_ino);
162 if (err) {
163 unlock_kernel();
164 return err;
165 }
166 inode->i_nlink++;
167 inode->i_ctime = CURRENT_TIME_SEC;
168 mark_inode_dirty(inode);
169 atomic_inc(&inode->i_count);
170 d_instantiate(new, inode);
171 unlock_kernel();
172 return 0;
173}
174
175
176static int bfs_unlink(struct inode * dir, struct dentry * dentry)
177{
178 int error = -ENOENT;
179 struct inode * inode;
180 struct buffer_head * bh;
181 struct bfs_dirent * de;
182
183 inode = dentry->d_inode;
184 lock_kernel();
185 bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
186 if (!bh || de->ino != inode->i_ino)
187 goto out_brelse;
188
189 if (!inode->i_nlink) {
190 printf("unlinking non-existent file %s:%lu (nlink=%d)\n", inode->i_sb->s_id,
191 inode->i_ino, inode->i_nlink);
192 inode->i_nlink = 1;
193 }
194 de->ino = 0;
195 mark_buffer_dirty(bh);
196 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
197 mark_inode_dirty(dir);
198 inode->i_nlink--;
199 inode->i_ctime = dir->i_ctime;
200 mark_inode_dirty(inode);
201 error = 0;
202
203out_brelse:
204 brelse(bh);
205 unlock_kernel();
206 return error;
207}
208
209static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
210 struct inode * new_dir, struct dentry * new_dentry)
211{
212 struct inode * old_inode, * new_inode;
213 struct buffer_head * old_bh, * new_bh;
214 struct bfs_dirent * old_de, * new_de;
215 int error = -ENOENT;
216
217 old_bh = new_bh = NULL;
218 old_inode = old_dentry->d_inode;
219 if (S_ISDIR(old_inode->i_mode))
220 return -EINVAL;
221
222 lock_kernel();
223 old_bh = bfs_find_entry(old_dir,
224 old_dentry->d_name.name,
225 old_dentry->d_name.len, &old_de);
226
227 if (!old_bh || old_de->ino != old_inode->i_ino)
228 goto end_rename;
229
230 error = -EPERM;
231 new_inode = new_dentry->d_inode;
232 new_bh = bfs_find_entry(new_dir,
233 new_dentry->d_name.name,
234 new_dentry->d_name.len, &new_de);
235
236 if (new_bh && !new_inode) {
237 brelse(new_bh);
238 new_bh = NULL;
239 }
240 if (!new_bh) {
241 error = bfs_add_entry(new_dir,
242 new_dentry->d_name.name,
243 new_dentry->d_name.len, old_inode->i_ino);
244 if (error)
245 goto end_rename;
246 }
247 old_de->ino = 0;
248 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
249 mark_inode_dirty(old_dir);
250 if (new_inode) {
251 new_inode->i_nlink--;
252 new_inode->i_ctime = CURRENT_TIME_SEC;
253 mark_inode_dirty(new_inode);
254 }
255 mark_buffer_dirty(old_bh);
256 error = 0;
257
258end_rename:
259 unlock_kernel();
260 brelse(old_bh);
261 brelse(new_bh);
262 return error;
263}
264
265struct inode_operations bfs_dir_inops = {
266 .create = bfs_create,
267 .lookup = bfs_lookup,
268 .link = bfs_link,
269 .unlink = bfs_unlink,
270 .rename = bfs_rename,
271};
272
273static int bfs_add_entry(struct inode * dir, const char * name, int namelen, int ino)
274{
275 struct buffer_head * bh;
276 struct bfs_dirent * de;
277 int block, sblock, eblock, off, eoff;
278 int i;
279
280 dprintf("name=%s, namelen=%d\n", name, namelen);
281
282 if (!namelen)
283 return -ENOENT;
284 if (namelen > BFS_NAMELEN)
285 return -ENAMETOOLONG;
286
287 sblock = BFS_I(dir)->i_sblock;
288 eblock = BFS_I(dir)->i_eblock;
289 eoff = dir->i_size % BFS_BSIZE;
290 for (block=sblock; block<=eblock; block++) {
291 bh = sb_bread(dir->i_sb, block);
292 if(!bh)
293 return -ENOSPC;
294 for (off=0; off<BFS_BSIZE; off+=BFS_DIRENT_SIZE) {
295 de = (struct bfs_dirent *)(bh->b_data + off);
296 if (block==eblock && off>=eoff) {
297 /* Do not read/interpret the garbage in the end of eblock. */
298 de->ino = 0;
299 }
300 if (!de->ino) {
301 if ((block-sblock)*BFS_BSIZE + off >= dir->i_size) {
302 dir->i_size += BFS_DIRENT_SIZE;
303 dir->i_ctime = CURRENT_TIME_SEC;
304 }
305 dir->i_mtime = CURRENT_TIME_SEC;
306 mark_inode_dirty(dir);
307 de->ino = ino;
308 for (i=0; i<BFS_NAMELEN; i++)
309 de->name[i] = (i < namelen) ? name[i] : 0;
310 mark_buffer_dirty(bh);
311 brelse(bh);
312 return 0;
313 }
314 }
315 brelse(bh);
316 }
317 return -ENOSPC;
318}
319
320static inline int bfs_namecmp(int len, const char * name, const char * buffer)
321{
322 if (len < BFS_NAMELEN && buffer[len])
323 return 0;
324 return !memcmp(name, buffer, len);
325}
326
327static struct buffer_head * bfs_find_entry(struct inode * dir,
328 const char * name, int namelen, struct bfs_dirent ** res_dir)
329{
330 unsigned long block, offset;
331 struct buffer_head * bh;
332 struct bfs_dirent * de;
333
334 *res_dir = NULL;
335 if (namelen > BFS_NAMELEN)
336 return NULL;
337 bh = NULL;
338 block = offset = 0;
339 while (block * BFS_BSIZE + offset < dir->i_size) {
340 if (!bh) {
341 bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
342 if (!bh) {
343 block++;
344 continue;
345 }
346 }
347 de = (struct bfs_dirent *)(bh->b_data + offset);
348 offset += BFS_DIRENT_SIZE;
349 if (de->ino && bfs_namecmp(namelen, name, de->name)) {
350 *res_dir = de;
351 return bh;
352 }
353 if (offset < bh->b_size)
354 continue;
355 brelse(bh);
356 bh = NULL;
357 offset = 0;
358 block++;
359 }
360 brelse(bh);
361 return NULL;
362}
diff --git a/fs/bfs/file.c b/fs/bfs/file.c
new file mode 100644
index 000000000000..747fd1ea55e0
--- /dev/null
+++ b/fs/bfs/file.c
@@ -0,0 +1,162 @@
1/*
2 * fs/bfs/file.c
3 * BFS file operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
5 */
6
7#include <linux/fs.h>
8#include <linux/buffer_head.h>
9#include <linux/smp_lock.h>
10#include "bfs.h"
11
12#undef DEBUG
13
14#ifdef DEBUG
15#define dprintf(x...) printf(x)
16#else
17#define dprintf(x...)
18#endif
19
20struct file_operations bfs_file_operations = {
21 .llseek = generic_file_llseek,
22 .read = generic_file_read,
23 .write = generic_file_write,
24 .mmap = generic_file_mmap,
25 .sendfile = generic_file_sendfile,
26};
27
28static int bfs_move_block(unsigned long from, unsigned long to, struct super_block *sb)
29{
30 struct buffer_head *bh, *new;
31
32 bh = sb_bread(sb, from);
33 if (!bh)
34 return -EIO;
35 new = sb_getblk(sb, to);
36 memcpy(new->b_data, bh->b_data, bh->b_size);
37 mark_buffer_dirty(new);
38 bforget(bh);
39 brelse(new);
40 return 0;
41}
42
43static int bfs_move_blocks(struct super_block *sb, unsigned long start, unsigned long end,
44 unsigned long where)
45{
46 unsigned long i;
47
48 dprintf("%08lx-%08lx->%08lx\n", start, end, where);
49 for (i = start; i <= end; i++)
50 if(bfs_move_block(i, where + i, sb)) {
51 dprintf("failed to move block %08lx -> %08lx\n", i, where + i);
52 return -EIO;
53 }
54 return 0;
55}
56
57static int bfs_get_block(struct inode * inode, sector_t block,
58 struct buffer_head * bh_result, int create)
59{
60 long phys;
61 int err;
62 struct super_block *sb = inode->i_sb;
63 struct bfs_sb_info *info = BFS_SB(sb);
64 struct bfs_inode_info *bi = BFS_I(inode);
65 struct buffer_head *sbh = info->si_sbh;
66
67 if (block < 0 || block > info->si_blocks)
68 return -EIO;
69
70 phys = bi->i_sblock + block;
71 if (!create) {
72 if (phys <= bi->i_eblock) {
73 dprintf("c=%d, b=%08lx, phys=%08lx (granted)\n", create, block, phys);
74 map_bh(bh_result, sb, phys);
75 }
76 return 0;
77 }
78
79 /* if the file is not empty and the requested block is within the range
80 of blocks allocated for this file, we can grant it */
81 if (inode->i_size && phys <= bi->i_eblock) {
82 dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
83 create, block, phys);
84 map_bh(bh_result, sb, phys);
85 return 0;
86 }
87
88 /* the rest has to be protected against itself */
89 lock_kernel();
90
91 /* if the last data block for this file is the last allocated block, we can
92 extend the file trivially, without moving it anywhere */
93 if (bi->i_eblock == info->si_lf_eblk) {
94 dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
95 create, block, phys);
96 map_bh(bh_result, sb, phys);
97 info->si_freeb -= phys - bi->i_eblock;
98 info->si_lf_eblk = bi->i_eblock = phys;
99 mark_inode_dirty(inode);
100 mark_buffer_dirty(sbh);
101 err = 0;
102 goto out;
103 }
104
105 /* Ok, we have to move this entire file to the next free block */
106 phys = info->si_lf_eblk + 1;
107 if (bi->i_sblock) { /* if data starts on block 0 then there is no data */
108 err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
109 bi->i_eblock, phys);
110 if (err) {
111 dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino);
112 goto out;
113 }
114 } else
115 err = 0;
116
117 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n", create, block, phys);
118 bi->i_sblock = phys;
119 phys += block;
120 info->si_lf_eblk = bi->i_eblock = phys;
121
122 /* this assumes nothing can write the inode back while we are here
123 * and thus update inode->i_blocks! (XXX)*/
124 info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
125 mark_inode_dirty(inode);
126 mark_buffer_dirty(sbh);
127 map_bh(bh_result, sb, phys);
128out:
129 unlock_kernel();
130 return err;
131}
132
133static int bfs_writepage(struct page *page, struct writeback_control *wbc)
134{
135 return block_write_full_page(page, bfs_get_block, wbc);
136}
137
138static int bfs_readpage(struct file *file, struct page *page)
139{
140 return block_read_full_page(page, bfs_get_block);
141}
142
143static int bfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
144{
145 return block_prepare_write(page, from, to, bfs_get_block);
146}
147
148static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
149{
150 return generic_block_bmap(mapping, block, bfs_get_block);
151}
152
153struct address_space_operations bfs_aops = {
154 .readpage = bfs_readpage,
155 .writepage = bfs_writepage,
156 .sync_page = block_sync_page,
157 .prepare_write = bfs_prepare_write,
158 .commit_write = generic_commit_write,
159 .bmap = bfs_bmap,
160};
161
162struct inode_operations bfs_file_inops;
diff --git a/fs/bfs/inode.c b/fs/bfs/inode.c
new file mode 100644
index 000000000000..64e0fb33fc0c
--- /dev/null
+++ b/fs/bfs/inode.c
@@ -0,0 +1,420 @@
1/*
2 * fs/bfs/inode.c
3 * BFS superblock and inode operations.
4 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
5 * From fs/minix, Copyright (C) 1991, 1992 Linus Torvalds.
6 */
7
8#include <linux/module.h>
9#include <linux/mm.h>
10#include <linux/slab.h>
11#include <linux/init.h>
12#include <linux/fs.h>
13#include <linux/smp_lock.h>
14#include <linux/buffer_head.h>
15#include <linux/vfs.h>
16#include <asm/uaccess.h>
17#include "bfs.h"
18
19MODULE_AUTHOR("Tigran A. Aivazian <tigran@veritas.com>");
20MODULE_DESCRIPTION("SCO UnixWare BFS filesystem for Linux");
21MODULE_LICENSE("GPL");
22
23#undef DEBUG
24
25#ifdef DEBUG
26#define dprintf(x...) printf(x)
27#else
28#define dprintf(x...)
29#endif
30
31void dump_imap(const char *prefix, struct super_block * s);
32
33static void bfs_read_inode(struct inode * inode)
34{
35 unsigned long ino = inode->i_ino;
36 struct bfs_inode * di;
37 struct buffer_head * bh;
38 int block, off;
39
40 if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
41 printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
42 make_bad_inode(inode);
43 return;
44 }
45
46 block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
47 bh = sb_bread(inode->i_sb, block);
48 if (!bh) {
49 printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
50 make_bad_inode(inode);
51 return;
52 }
53
54 off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
55 di = (struct bfs_inode *)bh->b_data + off;
56
57 inode->i_mode = 0x0000FFFF & di->i_mode;
58 if (di->i_vtype == BFS_VDIR) {
59 inode->i_mode |= S_IFDIR;
60 inode->i_op = &bfs_dir_inops;
61 inode->i_fop = &bfs_dir_operations;
62 } else if (di->i_vtype == BFS_VREG) {
63 inode->i_mode |= S_IFREG;
64 inode->i_op = &bfs_file_inops;
65 inode->i_fop = &bfs_file_operations;
66 inode->i_mapping->a_ops = &bfs_aops;
67 }
68
69 inode->i_uid = di->i_uid;
70 inode->i_gid = di->i_gid;
71 inode->i_nlink = di->i_nlink;
72 inode->i_size = BFS_FILESIZE(di);
73 inode->i_blocks = BFS_FILEBLOCKS(di);
74 inode->i_blksize = PAGE_SIZE;
75 inode->i_atime.tv_sec = di->i_atime;
76 inode->i_mtime.tv_sec = di->i_mtime;
77 inode->i_ctime.tv_sec = di->i_ctime;
78 inode->i_atime.tv_nsec = 0;
79 inode->i_mtime.tv_nsec = 0;
80 inode->i_ctime.tv_nsec = 0;
81 BFS_I(inode)->i_dsk_ino = di->i_ino; /* can be 0 so we store a copy */
82 BFS_I(inode)->i_sblock = di->i_sblock;
83 BFS_I(inode)->i_eblock = di->i_eblock;
84
85 brelse(bh);
86}
87
88static int bfs_write_inode(struct inode * inode, int unused)
89{
90 unsigned long ino = inode->i_ino;
91 struct bfs_inode * di;
92 struct buffer_head * bh;
93 int block, off;
94
95 if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
96 printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
97 return -EIO;
98 }
99
100 lock_kernel();
101 block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
102 bh = sb_bread(inode->i_sb, block);
103 if (!bh) {
104 printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
105 unlock_kernel();
106 return -EIO;
107 }
108
109 off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
110 di = (struct bfs_inode *)bh->b_data + off;
111
112 if (inode->i_ino == BFS_ROOT_INO)
113 di->i_vtype = BFS_VDIR;
114 else
115 di->i_vtype = BFS_VREG;
116
117 di->i_ino = inode->i_ino;
118 di->i_mode = inode->i_mode;
119 di->i_uid = inode->i_uid;
120 di->i_gid = inode->i_gid;
121 di->i_nlink = inode->i_nlink;
122 di->i_atime = inode->i_atime.tv_sec;
123 di->i_mtime = inode->i_mtime.tv_sec;
124 di->i_ctime = inode->i_ctime.tv_sec;
125 di->i_sblock = BFS_I(inode)->i_sblock;
126 di->i_eblock = BFS_I(inode)->i_eblock;
127 di->i_eoffset = di->i_sblock * BFS_BSIZE + inode->i_size - 1;
128
129 mark_buffer_dirty(bh);
130 brelse(bh);
131 unlock_kernel();
132 return 0;
133}
134
135static void bfs_delete_inode(struct inode * inode)
136{
137 unsigned long ino = inode->i_ino;
138 struct bfs_inode * di;
139 struct buffer_head * bh;
140 int block, off;
141 struct super_block * s = inode->i_sb;
142 struct bfs_sb_info * info = BFS_SB(s);
143
144 dprintf("ino=%08lx\n", inode->i_ino);
145
146 if (inode->i_ino < BFS_ROOT_INO || inode->i_ino > info->si_lasti) {
147 printf("invalid ino=%08lx\n", inode->i_ino);
148 return;
149 }
150
151 inode->i_size = 0;
152 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
153 lock_kernel();
154 mark_inode_dirty(inode);
155 block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
156 bh = sb_bread(s, block);
157 if (!bh) {
158 printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
159 unlock_kernel();
160 return;
161 }
162 off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
163 di = (struct bfs_inode *)bh->b_data + off;
164 if (di->i_ino) {
165 info->si_freeb += BFS_FILEBLOCKS(di);
166 info->si_freei++;
167 clear_bit(di->i_ino, info->si_imap);
168 dump_imap("delete_inode", s);
169 }
170 di->i_ino = 0;
171 di->i_sblock = 0;
172 mark_buffer_dirty(bh);
173 brelse(bh);
174
175 /* if this was the last file, make the previous
176 block "last files last block" even if there is no real file there,
177 saves us 1 gap */
178 if (info->si_lf_eblk == BFS_I(inode)->i_eblock) {
179 info->si_lf_eblk = BFS_I(inode)->i_sblock - 1;
180 mark_buffer_dirty(info->si_sbh);
181 }
182 unlock_kernel();
183 clear_inode(inode);
184}
185
186static void bfs_put_super(struct super_block *s)
187{
188 struct bfs_sb_info *info = BFS_SB(s);
189 brelse(info->si_sbh);
190 kfree(info->si_imap);
191 kfree(info);
192 s->s_fs_info = NULL;
193}
194
195static int bfs_statfs(struct super_block *s, struct kstatfs *buf)
196{
197 struct bfs_sb_info *info = BFS_SB(s);
198 u64 id = huge_encode_dev(s->s_bdev->bd_dev);
199 buf->f_type = BFS_MAGIC;
200 buf->f_bsize = s->s_blocksize;
201 buf->f_blocks = info->si_blocks;
202 buf->f_bfree = buf->f_bavail = info->si_freeb;
203 buf->f_files = info->si_lasti + 1 - BFS_ROOT_INO;
204 buf->f_ffree = info->si_freei;
205 buf->f_fsid.val[0] = (u32)id;
206 buf->f_fsid.val[1] = (u32)(id >> 32);
207 buf->f_namelen = BFS_NAMELEN;
208 return 0;
209}
210
211static void bfs_write_super(struct super_block *s)
212{
213 lock_kernel();
214 if (!(s->s_flags & MS_RDONLY))
215 mark_buffer_dirty(BFS_SB(s)->si_sbh);
216 s->s_dirt = 0;
217 unlock_kernel();
218}
219
220static kmem_cache_t * bfs_inode_cachep;
221
222static struct inode *bfs_alloc_inode(struct super_block *sb)
223{
224 struct bfs_inode_info *bi;
225 bi = kmem_cache_alloc(bfs_inode_cachep, SLAB_KERNEL);
226 if (!bi)
227 return NULL;
228 return &bi->vfs_inode;
229}
230
231static void bfs_destroy_inode(struct inode *inode)
232{
233 kmem_cache_free(bfs_inode_cachep, BFS_I(inode));
234}
235
236static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
237{
238 struct bfs_inode_info *bi = foo;
239
240 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
241 SLAB_CTOR_CONSTRUCTOR)
242 inode_init_once(&bi->vfs_inode);
243}
244
245static int init_inodecache(void)
246{
247 bfs_inode_cachep = kmem_cache_create("bfs_inode_cache",
248 sizeof(struct bfs_inode_info),
249 0, SLAB_RECLAIM_ACCOUNT,
250 init_once, NULL);
251 if (bfs_inode_cachep == NULL)
252 return -ENOMEM;
253 return 0;
254}
255
256static void destroy_inodecache(void)
257{
258 if (kmem_cache_destroy(bfs_inode_cachep))
259 printk(KERN_INFO "bfs_inode_cache: not all structures were freed\n");
260}
261
262static struct super_operations bfs_sops = {
263 .alloc_inode = bfs_alloc_inode,
264 .destroy_inode = bfs_destroy_inode,
265 .read_inode = bfs_read_inode,
266 .write_inode = bfs_write_inode,
267 .delete_inode = bfs_delete_inode,
268 .put_super = bfs_put_super,
269 .write_super = bfs_write_super,
270 .statfs = bfs_statfs,
271};
272
273void dump_imap(const char *prefix, struct super_block * s)
274{
275#if 0
276 int i;
277 char *tmpbuf = (char *)get_zeroed_page(GFP_KERNEL);
278
279 if (!tmpbuf)
280 return;
281 for (i=BFS_SB(s)->si_lasti; i>=0; i--) {
282 if (i>PAGE_SIZE-100) break;
283 if (test_bit(i, BFS_SB(s)->si_imap))
284 strcat(tmpbuf, "1");
285 else
286 strcat(tmpbuf, "0");
287 }
288 printk(KERN_ERR "BFS-fs: %s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf);
289 free_page((unsigned long)tmpbuf);
290#endif
291}
292
293static int bfs_fill_super(struct super_block *s, void *data, int silent)
294{
295 struct buffer_head * bh;
296 struct bfs_super_block * bfs_sb;
297 struct inode * inode;
298 int i, imap_len;
299 struct bfs_sb_info * info;
300
301 info = kmalloc(sizeof(*info), GFP_KERNEL);
302 if (!info)
303 return -ENOMEM;
304 s->s_fs_info = info;
305 memset(info, 0, sizeof(*info));
306
307 sb_set_blocksize(s, BFS_BSIZE);
308
309 bh = sb_bread(s, 0);
310 if(!bh)
311 goto out;
312 bfs_sb = (struct bfs_super_block *)bh->b_data;
313 if (bfs_sb->s_magic != BFS_MAGIC) {
314 if (!silent)
315 printf("No BFS filesystem on %s (magic=%08x)\n",
316 s->s_id, bfs_sb->s_magic);
317 goto out;
318 }
319 if (BFS_UNCLEAN(bfs_sb, s) && !silent)
320 printf("%s is unclean, continuing\n", s->s_id);
321
322 s->s_magic = BFS_MAGIC;
323 info->si_bfs_sb = bfs_sb;
324 info->si_sbh = bh;
325 info->si_lasti = (bfs_sb->s_start - BFS_BSIZE)/sizeof(struct bfs_inode)
326 + BFS_ROOT_INO - 1;
327
328 imap_len = info->si_lasti/8 + 1;
329 info->si_imap = kmalloc(imap_len, GFP_KERNEL);
330 if (!info->si_imap)
331 goto out;
332 memset(info->si_imap, 0, imap_len);
333 for (i=0; i<BFS_ROOT_INO; i++)
334 set_bit(i, info->si_imap);
335
336 s->s_op = &bfs_sops;
337 inode = iget(s, BFS_ROOT_INO);
338 if (!inode) {
339 kfree(info->si_imap);
340 goto out;
341 }
342 s->s_root = d_alloc_root(inode);
343 if (!s->s_root) {
344 iput(inode);
345 kfree(info->si_imap);
346 goto out;
347 }
348
349 info->si_blocks = (bfs_sb->s_end + 1)>>BFS_BSIZE_BITS; /* for statfs(2) */
350 info->si_freeb = (bfs_sb->s_end + 1 - bfs_sb->s_start)>>BFS_BSIZE_BITS;
351 info->si_freei = 0;
352 info->si_lf_eblk = 0;
353 info->si_lf_sblk = 0;
354 info->si_lf_ioff = 0;
355 for (i=BFS_ROOT_INO; i<=info->si_lasti; i++) {
356 inode = iget(s,i);
357 if (BFS_I(inode)->i_dsk_ino == 0)
358 info->si_freei++;
359 else {
360 set_bit(i, info->si_imap);
361 info->si_freeb -= inode->i_blocks;
362 if (BFS_I(inode)->i_eblock > info->si_lf_eblk) {
363 info->si_lf_eblk = BFS_I(inode)->i_eblock;
364 info->si_lf_sblk = BFS_I(inode)->i_sblock;
365 info->si_lf_ioff = BFS_INO2OFF(i);
366 }
367 }
368 iput(inode);
369 }
370 if (!(s->s_flags & MS_RDONLY)) {
371 mark_buffer_dirty(bh);
372 s->s_dirt = 1;
373 }
374 dump_imap("read_super", s);
375 return 0;
376
377out:
378 brelse(bh);
379 kfree(info);
380 s->s_fs_info = NULL;
381 return -EINVAL;
382}
383
384static struct super_block *bfs_get_sb(struct file_system_type *fs_type,
385 int flags, const char *dev_name, void *data)
386{
387 return get_sb_bdev(fs_type, flags, dev_name, data, bfs_fill_super);
388}
389
390static struct file_system_type bfs_fs_type = {
391 .owner = THIS_MODULE,
392 .name = "bfs",
393 .get_sb = bfs_get_sb,
394 .kill_sb = kill_block_super,
395 .fs_flags = FS_REQUIRES_DEV,
396};
397
398static int __init init_bfs_fs(void)
399{
400 int err = init_inodecache();
401 if (err)
402 goto out1;
403 err = register_filesystem(&bfs_fs_type);
404 if (err)
405 goto out;
406 return 0;
407out:
408 destroy_inodecache();
409out1:
410 return err;
411}
412
413static void __exit exit_bfs_fs(void)
414{
415 unregister_filesystem(&bfs_fs_type);
416 destroy_inodecache();
417}
418
419module_init(init_bfs_fs)
420module_exit(exit_bfs_fs)