]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/bfs/inode.c | |
3 | * BFS superblock and inode operations. | |
4 | * Copyright (C) 1999,2000 Tigran Aivazian <[email protected]> | |
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 | ||
19 | MODULE_AUTHOR("Tigran A. Aivazian <[email protected]>"); | |
20 | MODULE_DESCRIPTION("SCO UnixWare BFS filesystem for Linux"); | |
21 | MODULE_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 | ||
31 | void dump_imap(const char *prefix, struct super_block * s); | |
32 | ||
33 | static 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 | ||
88 | static 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 | ||
135 | static 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 | ||
fef26658 MF |
146 | truncate_inode_pages(&inode->i_data, 0); |
147 | ||
1da177e4 LT |
148 | if (inode->i_ino < BFS_ROOT_INO || inode->i_ino > info->si_lasti) { |
149 | printf("invalid ino=%08lx\n", inode->i_ino); | |
150 | return; | |
151 | } | |
152 | ||
153 | inode->i_size = 0; | |
154 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; | |
155 | lock_kernel(); | |
156 | mark_inode_dirty(inode); | |
157 | block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1; | |
158 | bh = sb_bread(s, block); | |
159 | if (!bh) { | |
160 | printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino); | |
161 | unlock_kernel(); | |
162 | return; | |
163 | } | |
164 | off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK; | |
165 | di = (struct bfs_inode *)bh->b_data + off; | |
166 | if (di->i_ino) { | |
167 | info->si_freeb += BFS_FILEBLOCKS(di); | |
168 | info->si_freei++; | |
169 | clear_bit(di->i_ino, info->si_imap); | |
170 | dump_imap("delete_inode", s); | |
171 | } | |
172 | di->i_ino = 0; | |
173 | di->i_sblock = 0; | |
174 | mark_buffer_dirty(bh); | |
175 | brelse(bh); | |
176 | ||
177 | /* if this was the last file, make the previous | |
178 | block "last files last block" even if there is no real file there, | |
179 | saves us 1 gap */ | |
180 | if (info->si_lf_eblk == BFS_I(inode)->i_eblock) { | |
181 | info->si_lf_eblk = BFS_I(inode)->i_sblock - 1; | |
182 | mark_buffer_dirty(info->si_sbh); | |
183 | } | |
184 | unlock_kernel(); | |
185 | clear_inode(inode); | |
186 | } | |
187 | ||
188 | static void bfs_put_super(struct super_block *s) | |
189 | { | |
190 | struct bfs_sb_info *info = BFS_SB(s); | |
191 | brelse(info->si_sbh); | |
192 | kfree(info->si_imap); | |
193 | kfree(info); | |
194 | s->s_fs_info = NULL; | |
195 | } | |
196 | ||
197 | static int bfs_statfs(struct super_block *s, struct kstatfs *buf) | |
198 | { | |
199 | struct bfs_sb_info *info = BFS_SB(s); | |
200 | u64 id = huge_encode_dev(s->s_bdev->bd_dev); | |
201 | buf->f_type = BFS_MAGIC; | |
202 | buf->f_bsize = s->s_blocksize; | |
203 | buf->f_blocks = info->si_blocks; | |
204 | buf->f_bfree = buf->f_bavail = info->si_freeb; | |
205 | buf->f_files = info->si_lasti + 1 - BFS_ROOT_INO; | |
206 | buf->f_ffree = info->si_freei; | |
207 | buf->f_fsid.val[0] = (u32)id; | |
208 | buf->f_fsid.val[1] = (u32)(id >> 32); | |
209 | buf->f_namelen = BFS_NAMELEN; | |
210 | return 0; | |
211 | } | |
212 | ||
213 | static void bfs_write_super(struct super_block *s) | |
214 | { | |
215 | lock_kernel(); | |
216 | if (!(s->s_flags & MS_RDONLY)) | |
217 | mark_buffer_dirty(BFS_SB(s)->si_sbh); | |
218 | s->s_dirt = 0; | |
219 | unlock_kernel(); | |
220 | } | |
221 | ||
222 | static kmem_cache_t * bfs_inode_cachep; | |
223 | ||
224 | static struct inode *bfs_alloc_inode(struct super_block *sb) | |
225 | { | |
226 | struct bfs_inode_info *bi; | |
227 | bi = kmem_cache_alloc(bfs_inode_cachep, SLAB_KERNEL); | |
228 | if (!bi) | |
229 | return NULL; | |
230 | return &bi->vfs_inode; | |
231 | } | |
232 | ||
233 | static void bfs_destroy_inode(struct inode *inode) | |
234 | { | |
235 | kmem_cache_free(bfs_inode_cachep, BFS_I(inode)); | |
236 | } | |
237 | ||
238 | static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) | |
239 | { | |
240 | struct bfs_inode_info *bi = foo; | |
241 | ||
242 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
243 | SLAB_CTOR_CONSTRUCTOR) | |
244 | inode_init_once(&bi->vfs_inode); | |
245 | } | |
246 | ||
247 | static int init_inodecache(void) | |
248 | { | |
249 | bfs_inode_cachep = kmem_cache_create("bfs_inode_cache", | |
250 | sizeof(struct bfs_inode_info), | |
251 | 0, SLAB_RECLAIM_ACCOUNT, | |
252 | init_once, NULL); | |
253 | if (bfs_inode_cachep == NULL) | |
254 | return -ENOMEM; | |
255 | return 0; | |
256 | } | |
257 | ||
258 | static void destroy_inodecache(void) | |
259 | { | |
260 | if (kmem_cache_destroy(bfs_inode_cachep)) | |
261 | printk(KERN_INFO "bfs_inode_cache: not all structures were freed\n"); | |
262 | } | |
263 | ||
264 | static struct super_operations bfs_sops = { | |
265 | .alloc_inode = bfs_alloc_inode, | |
266 | .destroy_inode = bfs_destroy_inode, | |
267 | .read_inode = bfs_read_inode, | |
268 | .write_inode = bfs_write_inode, | |
269 | .delete_inode = bfs_delete_inode, | |
270 | .put_super = bfs_put_super, | |
271 | .write_super = bfs_write_super, | |
272 | .statfs = bfs_statfs, | |
273 | }; | |
274 | ||
275 | void dump_imap(const char *prefix, struct super_block * s) | |
276 | { | |
277 | #if 0 | |
278 | int i; | |
279 | char *tmpbuf = (char *)get_zeroed_page(GFP_KERNEL); | |
280 | ||
281 | if (!tmpbuf) | |
282 | return; | |
283 | for (i=BFS_SB(s)->si_lasti; i>=0; i--) { | |
284 | if (i>PAGE_SIZE-100) break; | |
285 | if (test_bit(i, BFS_SB(s)->si_imap)) | |
286 | strcat(tmpbuf, "1"); | |
287 | else | |
288 | strcat(tmpbuf, "0"); | |
289 | } | |
290 | printk(KERN_ERR "BFS-fs: %s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf); | |
291 | free_page((unsigned long)tmpbuf); | |
292 | #endif | |
293 | } | |
294 | ||
295 | static int bfs_fill_super(struct super_block *s, void *data, int silent) | |
296 | { | |
297 | struct buffer_head * bh; | |
298 | struct bfs_super_block * bfs_sb; | |
299 | struct inode * inode; | |
300 | int i, imap_len; | |
301 | struct bfs_sb_info * info; | |
302 | ||
303 | info = kmalloc(sizeof(*info), GFP_KERNEL); | |
304 | if (!info) | |
305 | return -ENOMEM; | |
306 | s->s_fs_info = info; | |
307 | memset(info, 0, sizeof(*info)); | |
308 | ||
309 | sb_set_blocksize(s, BFS_BSIZE); | |
310 | ||
311 | bh = sb_bread(s, 0); | |
312 | if(!bh) | |
313 | goto out; | |
314 | bfs_sb = (struct bfs_super_block *)bh->b_data; | |
315 | if (bfs_sb->s_magic != BFS_MAGIC) { | |
316 | if (!silent) | |
317 | printf("No BFS filesystem on %s (magic=%08x)\n", | |
318 | s->s_id, bfs_sb->s_magic); | |
319 | goto out; | |
320 | } | |
321 | if (BFS_UNCLEAN(bfs_sb, s) && !silent) | |
322 | printf("%s is unclean, continuing\n", s->s_id); | |
323 | ||
324 | s->s_magic = BFS_MAGIC; | |
325 | info->si_bfs_sb = bfs_sb; | |
326 | info->si_sbh = bh; | |
327 | info->si_lasti = (bfs_sb->s_start - BFS_BSIZE)/sizeof(struct bfs_inode) | |
328 | + BFS_ROOT_INO - 1; | |
329 | ||
330 | imap_len = info->si_lasti/8 + 1; | |
331 | info->si_imap = kmalloc(imap_len, GFP_KERNEL); | |
332 | if (!info->si_imap) | |
333 | goto out; | |
334 | memset(info->si_imap, 0, imap_len); | |
335 | for (i=0; i<BFS_ROOT_INO; i++) | |
336 | set_bit(i, info->si_imap); | |
337 | ||
338 | s->s_op = &bfs_sops; | |
339 | inode = iget(s, BFS_ROOT_INO); | |
340 | if (!inode) { | |
341 | kfree(info->si_imap); | |
342 | goto out; | |
343 | } | |
344 | s->s_root = d_alloc_root(inode); | |
345 | if (!s->s_root) { | |
346 | iput(inode); | |
347 | kfree(info->si_imap); | |
348 | goto out; | |
349 | } | |
350 | ||
351 | info->si_blocks = (bfs_sb->s_end + 1)>>BFS_BSIZE_BITS; /* for statfs(2) */ | |
352 | info->si_freeb = (bfs_sb->s_end + 1 - bfs_sb->s_start)>>BFS_BSIZE_BITS; | |
353 | info->si_freei = 0; | |
354 | info->si_lf_eblk = 0; | |
355 | info->si_lf_sblk = 0; | |
356 | info->si_lf_ioff = 0; | |
357 | for (i=BFS_ROOT_INO; i<=info->si_lasti; i++) { | |
358 | inode = iget(s,i); | |
359 | if (BFS_I(inode)->i_dsk_ino == 0) | |
360 | info->si_freei++; | |
361 | else { | |
362 | set_bit(i, info->si_imap); | |
363 | info->si_freeb -= inode->i_blocks; | |
364 | if (BFS_I(inode)->i_eblock > info->si_lf_eblk) { | |
365 | info->si_lf_eblk = BFS_I(inode)->i_eblock; | |
366 | info->si_lf_sblk = BFS_I(inode)->i_sblock; | |
367 | info->si_lf_ioff = BFS_INO2OFF(i); | |
368 | } | |
369 | } | |
370 | iput(inode); | |
371 | } | |
372 | if (!(s->s_flags & MS_RDONLY)) { | |
373 | mark_buffer_dirty(bh); | |
374 | s->s_dirt = 1; | |
375 | } | |
376 | dump_imap("read_super", s); | |
377 | return 0; | |
378 | ||
379 | out: | |
380 | brelse(bh); | |
381 | kfree(info); | |
382 | s->s_fs_info = NULL; | |
383 | return -EINVAL; | |
384 | } | |
385 | ||
386 | static struct super_block *bfs_get_sb(struct file_system_type *fs_type, | |
387 | int flags, const char *dev_name, void *data) | |
388 | { | |
389 | return get_sb_bdev(fs_type, flags, dev_name, data, bfs_fill_super); | |
390 | } | |
391 | ||
392 | static struct file_system_type bfs_fs_type = { | |
393 | .owner = THIS_MODULE, | |
394 | .name = "bfs", | |
395 | .get_sb = bfs_get_sb, | |
396 | .kill_sb = kill_block_super, | |
397 | .fs_flags = FS_REQUIRES_DEV, | |
398 | }; | |
399 | ||
400 | static int __init init_bfs_fs(void) | |
401 | { | |
402 | int err = init_inodecache(); | |
403 | if (err) | |
404 | goto out1; | |
405 | err = register_filesystem(&bfs_fs_type); | |
406 | if (err) | |
407 | goto out; | |
408 | return 0; | |
409 | out: | |
410 | destroy_inodecache(); | |
411 | out1: | |
412 | return err; | |
413 | } | |
414 | ||
415 | static void __exit exit_bfs_fs(void) | |
416 | { | |
417 | unregister_filesystem(&bfs_fs_type); | |
418 | destroy_inodecache(); | |
419 | } | |
420 | ||
421 | module_init(init_bfs_fs) | |
422 | module_exit(exit_bfs_fs) |