ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / efs / super.c
1 /*
2  * super.c
3  *
4  * Copyright (c) 1999 Al Smith
5  *
6  * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
7  */
8
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/efs_fs.h>
12 #include <linux/efs_vh.h>
13 #include <linux/efs_fs_sb.h>
14 #include <linux/slab.h>
15 #include <linux/buffer_head.h>
16 #include <linux/vfs.h>
17
18 static struct super_block *efs_get_sb(struct file_system_type *fs_type,
19         int flags, const char *dev_name, void *data)
20 {
21         return get_sb_bdev(fs_type, flags, dev_name, data, efs_fill_super);
22 }
23
24 static struct file_system_type efs_fs_type = {
25         .owner          = THIS_MODULE,
26         .name           = "efs",
27         .get_sb         = efs_get_sb,
28         .kill_sb        = kill_block_super,
29         .fs_flags       = FS_REQUIRES_DEV,
30 };
31
32 static kmem_cache_t * efs_inode_cachep;
33
34 static struct inode *efs_alloc_inode(struct super_block *sb)
35 {
36         struct efs_inode_info *ei;
37         ei = (struct efs_inode_info *)kmem_cache_alloc(efs_inode_cachep, SLAB_KERNEL);
38         if (!ei)
39                 return NULL;
40         return &ei->vfs_inode;
41 }
42
43 static void efs_destroy_inode(struct inode *inode)
44 {
45         kmem_cache_free(efs_inode_cachep, INODE_INFO(inode));
46 }
47
48 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
49 {
50         struct efs_inode_info *ei = (struct efs_inode_info *) foo;
51
52         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
53             SLAB_CTOR_CONSTRUCTOR)
54                 inode_init_once(&ei->vfs_inode);
55 }
56  
57 static int init_inodecache(void)
58 {
59         efs_inode_cachep = kmem_cache_create("efs_inode_cache",
60                                 sizeof(struct efs_inode_info),
61                                 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
62                                 init_once, NULL);
63         if (efs_inode_cachep == NULL)
64                 return -ENOMEM;
65         return 0;
66 }
67
68 static void destroy_inodecache(void)
69 {
70         if (kmem_cache_destroy(efs_inode_cachep))
71                 printk(KERN_INFO "efs_inode_cache: not all structures were freed\n");
72 }
73
74 void efs_put_super(struct super_block *s)
75 {
76         kfree(s->s_fs_info);
77         s->s_fs_info = NULL;
78 }
79
80 static int efs_remount(struct super_block *sb, int *flags, char *data)
81 {
82         *flags |= MS_RDONLY;
83         return 0;
84 }
85
86 static struct super_operations efs_superblock_operations = {
87         .alloc_inode    = efs_alloc_inode,
88         .destroy_inode  = efs_destroy_inode,
89         .read_inode     = efs_read_inode,
90         .put_super      = efs_put_super,
91         .statfs         = efs_statfs,
92         .remount_fs     = efs_remount,
93 };
94
95 static int __init init_efs_fs(void) {
96         int err;
97         printk("EFS: "EFS_VERSION" - http://aeschi.ch.eu.org/efs/\n");
98         err = init_inodecache();
99         if (err)
100                 goto out1;
101         err = register_filesystem(&efs_fs_type);
102         if (err)
103                 goto out;
104         return 0;
105 out:
106         destroy_inodecache();
107 out1:
108         return err;
109 }
110
111 static void __exit exit_efs_fs(void) {
112         unregister_filesystem(&efs_fs_type);
113         destroy_inodecache();
114 }
115
116 module_init(init_efs_fs)
117 module_exit(exit_efs_fs)
118
119 static efs_block_t efs_validate_vh(struct volume_header *vh) {
120         int             i;
121         unsigned int    cs, csum, *ui;
122         efs_block_t     sblock = 0; /* shuts up gcc */
123         struct pt_types *pt_entry;
124         int             pt_type, slice = -1;
125
126         if (be32_to_cpu(vh->vh_magic) != VHMAGIC) {
127                 /*
128                  * assume that we're dealing with a partition and allow
129                  * read_super() to try and detect a valid superblock
130                  * on the next block.
131                  */
132                 return 0;
133         }
134
135         ui = ((unsigned int *) (vh + 1)) - 1;
136         for(csum = 0; ui >= ((unsigned int *) vh);) {
137                 cs = *ui--;
138                 csum += be32_to_cpu(cs);
139         }
140         if (csum) {
141                 printk(KERN_INFO "EFS: SGI disklabel: checksum bad, label corrupted\n");
142                 return 0;
143         }
144
145 #ifdef DEBUG
146         printk(KERN_DEBUG "EFS: bf: \"%16s\"\n", vh->vh_bootfile);
147
148         for(i = 0; i < NVDIR; i++) {
149                 int     j;
150                 char    name[VDNAMESIZE+1];
151
152                 for(j = 0; j < VDNAMESIZE; j++) {
153                         name[j] = vh->vh_vd[i].vd_name[j];
154                 }
155                 name[j] = (char) 0;
156
157                 if (name[0]) {
158                         printk(KERN_DEBUG "EFS: vh: %8s block: 0x%08x size: 0x%08x\n",
159                                 name,
160                                 (int) be32_to_cpu(vh->vh_vd[i].vd_lbn),
161                                 (int) be32_to_cpu(vh->vh_vd[i].vd_nbytes));
162                 }
163         }
164 #endif
165
166         for(i = 0; i < NPARTAB; i++) {
167                 pt_type = (int) be32_to_cpu(vh->vh_pt[i].pt_type);
168                 for(pt_entry = sgi_pt_types; pt_entry->pt_name; pt_entry++) {
169                         if (pt_type == pt_entry->pt_type) break;
170                 }
171 #ifdef DEBUG
172                 if (be32_to_cpu(vh->vh_pt[i].pt_nblks)) {
173                         printk(KERN_DEBUG "EFS: pt %2d: start: %08d size: %08d type: 0x%02x (%s)\n",
174                                 i,
175                                 (int) be32_to_cpu(vh->vh_pt[i].pt_firstlbn),
176                                 (int) be32_to_cpu(vh->vh_pt[i].pt_nblks),
177                                 pt_type,
178                                 (pt_entry->pt_name) ? pt_entry->pt_name : "unknown");
179                 }
180 #endif
181                 if (IS_EFS(pt_type)) {
182                         sblock = be32_to_cpu(vh->vh_pt[i].pt_firstlbn);
183                         slice = i;
184                 }
185         }
186
187         if (slice == -1) {
188                 printk(KERN_NOTICE "EFS: partition table contained no EFS partitions\n");
189 #ifdef DEBUG
190         } else {
191                 printk(KERN_INFO "EFS: using slice %d (type %s, offset 0x%x)\n",
192                         slice,
193                         (pt_entry->pt_name) ? pt_entry->pt_name : "unknown",
194                         sblock);
195 #endif
196         }
197         return(sblock);
198 }
199
200 static int efs_validate_super(struct efs_sb_info *sb, struct efs_super *super) {
201
202         if (!IS_EFS_MAGIC(be32_to_cpu(super->fs_magic))) return -1;
203
204         sb->fs_magic     = be32_to_cpu(super->fs_magic);
205         sb->total_blocks = be32_to_cpu(super->fs_size);
206         sb->first_block  = be32_to_cpu(super->fs_firstcg);
207         sb->group_size   = be32_to_cpu(super->fs_cgfsize);
208         sb->data_free    = be32_to_cpu(super->fs_tfree);
209         sb->inode_free   = be32_to_cpu(super->fs_tinode);
210         sb->inode_blocks = be16_to_cpu(super->fs_cgisize);
211         sb->total_groups = be16_to_cpu(super->fs_ncg);
212     
213         return 0;    
214 }
215
216 int efs_fill_super(struct super_block *s, void *d, int silent)
217 {
218         struct efs_sb_info *sb;
219         struct buffer_head *bh;
220         struct inode *root;
221
222         sb = kmalloc(sizeof(struct efs_sb_info), GFP_KERNEL);
223         if (!sb)
224                 return -ENOMEM;
225         s->s_fs_info = sb;
226         memset(sb, 0, sizeof(struct efs_sb_info));
227  
228         s->s_magic              = EFS_SUPER_MAGIC;
229         if (!sb_set_blocksize(s, EFS_BLOCKSIZE)) {
230                 printk(KERN_ERR "EFS: device does not support %d byte blocks\n",
231                         EFS_BLOCKSIZE);
232                 goto out_no_fs_ul;
233         }
234   
235         /* read the vh (volume header) block */
236         bh = sb_bread(s, 0);
237
238         if (!bh) {
239                 printk(KERN_ERR "EFS: cannot read volume header\n");
240                 goto out_no_fs_ul;
241         }
242
243         /*
244          * if this returns zero then we didn't find any partition table.
245          * this isn't (yet) an error - just assume for the moment that
246          * the device is valid and go on to search for a superblock.
247          */
248         sb->fs_start = efs_validate_vh((struct volume_header *) bh->b_data);
249         brelse(bh);
250
251         if (sb->fs_start == -1) {
252                 goto out_no_fs_ul;
253         }
254
255         bh = sb_bread(s, sb->fs_start + EFS_SUPER);
256         if (!bh) {
257                 printk(KERN_ERR "EFS: cannot read superblock\n");
258                 goto out_no_fs_ul;
259         }
260                 
261         if (efs_validate_super(sb, (struct efs_super *) bh->b_data)) {
262 #ifdef DEBUG
263                 printk(KERN_WARNING "EFS: invalid superblock at block %u\n", sb->fs_start + EFS_SUPER);
264 #endif
265                 brelse(bh);
266                 goto out_no_fs_ul;
267         }
268         brelse(bh);
269
270         if (!(s->s_flags & MS_RDONLY)) {
271 #ifdef DEBUG
272                 printk(KERN_INFO "EFS: forcing read-only mode\n");
273 #endif
274                 s->s_flags |= MS_RDONLY;
275         }
276         s->s_op   = &efs_superblock_operations;
277         root = iget(s, EFS_ROOTINODE);
278         s->s_root = d_alloc_root(root);
279  
280         if (!(s->s_root)) {
281                 printk(KERN_ERR "EFS: get root inode failed\n");
282                 iput(root);
283                 goto out_no_fs;
284         }
285
286         return 0;
287
288 out_no_fs_ul:
289 out_no_fs:
290         s->s_fs_info = NULL;
291         kfree(sb);
292         return -EINVAL;
293 }
294
295 int efs_statfs(struct super_block *s, struct kstatfs *buf) {
296         struct efs_sb_info *sb = SUPER_INFO(s);
297
298         buf->f_type    = EFS_SUPER_MAGIC;       /* efs magic number */
299         buf->f_bsize   = EFS_BLOCKSIZE;         /* blocksize */
300         buf->f_blocks  = sb->total_groups *     /* total data blocks */
301                         (sb->group_size - sb->inode_blocks);
302         buf->f_bfree   = sb->data_free;         /* free data blocks */
303         buf->f_bavail  = sb->data_free;         /* free blocks for non-root */
304         buf->f_files   = sb->total_groups *     /* total inodes */
305                         sb->inode_blocks *
306                         (EFS_BLOCKSIZE / sizeof(struct efs_dinode));
307         buf->f_ffree   = sb->inode_free;        /* free inodes */
308         buf->f_fsid.val[0] = (sb->fs_magic >> 16) & 0xffff; /* fs ID */
309         buf->f_fsid.val[1] =  sb->fs_magic        & 0xffff; /* fs ID */
310         buf->f_namelen = EFS_MAXNAMELEN;        /* max filename length */
311
312         return 0;
313 }
314