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