Import 2.3.26pre2
[davej-history.git] / fs / efs / super.c
blobdf0f167cb986eee1c55c77d01a629bedd750e4d6
1 /*
2 * super.c
4 * Copyright (c) 1999 Al Smith
6 * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
7 */
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/locks.h>
12 #include <linux/efs_fs.h>
13 #include <linux/efs_vh.h>
14 #include <linux/efs_fs_sb.h>
16 static struct file_system_type efs_fs_type = {
17 "efs", /* filesystem name */
18 FS_REQUIRES_DEV, /* fs_flags */
19 efs_read_super, /* entry function pointer */
20 NULL /* next */
23 static struct super_operations efs_superblock_operations = {
24 efs_read_inode, /* read_inode */
25 NULL, /* write_inode */
26 NULL, /* put_inode */
27 NULL, /* delete_inode */
28 NULL, /* notify_change */
29 efs_put_super, /* put_super */
30 NULL, /* write_super */
31 efs_statfs, /* statfs */
32 NULL /* remount */
35 int __init init_efs_fs(void) {
36 return register_filesystem(&efs_fs_type);
39 #ifdef MODULE
40 EXPORT_NO_SYMBOLS;
42 int init_module(void) {
43 printk("EFS: "EFS_VERSION" - http://aeschi.ch.eu.org/efs/\n");
44 return init_efs_fs();
47 void cleanup_module(void) {
48 unregister_filesystem(&efs_fs_type);
50 #endif
52 static efs_block_t efs_validate_vh(struct volume_header *vh) {
53 int i;
54 unsigned int cs, csum, *ui;
55 efs_block_t sblock = 0; /* shuts up gcc */
56 struct pt_types *pt_entry;
57 int pt_type, slice = -1;
59 if (be32_to_cpu(vh->vh_magic) != VHMAGIC) {
61 * assume that we're dealing with a partition and allow
62 * read_super() to try and detect a valid superblock
63 * on the next block.
65 return 0;
68 ui = ((unsigned int *) (vh + 1)) - 1;
69 for(csum = 0; ui >= ((unsigned int *) vh);) {
70 cs = *ui--;
71 csum += be32_to_cpu(cs);
73 if (csum) {
74 printk(KERN_INFO "EFS: SGI disklabel: checksum bad, label corrupted\n");
75 return 0;
78 #ifdef DEBUG
79 printk(KERN_DEBUG "EFS: bf: \"%16s\"\n", vh->vh_bootfile);
81 for(i = 0; i < NVDIR; i++) {
82 int j;
83 char name[VDNAMESIZE+1];
85 for(j = 0; j < VDNAMESIZE; j++) {
86 name[j] = vh->vh_vd[i].vd_name[j];
88 name[j] = (char) 0;
90 if (name[0]) {
91 printk(KERN_DEBUG "EFS: vh: %8s block: 0x%08x size: 0x%08x\n",
92 name,
93 (int) be32_to_cpu(vh->vh_vd[i].vd_lbn),
94 (int) be32_to_cpu(vh->vh_vd[i].vd_nbytes));
97 #endif
99 for(i = 0; i < NPARTAB; i++) {
100 pt_type = (int) be32_to_cpu(vh->vh_pt[i].pt_type);
101 for(pt_entry = sgi_pt_types; pt_entry->pt_name; pt_entry++) {
102 if (pt_type == pt_entry->pt_type) break;
104 #ifdef DEBUG
105 if (be32_to_cpu(vh->vh_pt[i].pt_nblks)) {
106 printk(KERN_DEBUG "EFS: pt %2d: start: %08d size: %08d type: 0x%02x (%s)\n",
108 (int) be32_to_cpu(vh->vh_pt[i].pt_firstlbn),
109 (int) be32_to_cpu(vh->vh_pt[i].pt_nblks),
110 pt_type,
111 (pt_entry->pt_name) ? pt_entry->pt_name : "unknown");
113 #endif
114 if (IS_EFS(pt_type)) {
115 sblock = be32_to_cpu(vh->vh_pt[i].pt_firstlbn);
116 slice = i;
120 if (slice == -1) {
121 printk(KERN_NOTICE "EFS: partition table contained no EFS partitions\n");
122 #ifdef DEBUG
123 } else {
124 printk(KERN_INFO "EFS: using slice %d (type %s, offset 0x%x)\n",
125 slice,
126 (pt_entry->pt_name) ? pt_entry->pt_name : "unknown",
127 sblock);
128 #endif
130 return(sblock);
133 static int efs_validate_super(struct efs_sb_info *sb, struct efs_super *super) {
135 if (!IS_EFS_MAGIC(be32_to_cpu(super->fs_magic))) return -1;
137 sb->fs_magic = be32_to_cpu(super->fs_magic);
138 sb->total_blocks = be32_to_cpu(super->fs_size);
139 sb->first_block = be32_to_cpu(super->fs_firstcg);
140 sb->group_size = be32_to_cpu(super->fs_cgfsize);
141 sb->data_free = be32_to_cpu(super->fs_tfree);
142 sb->inode_free = be32_to_cpu(super->fs_tinode);
143 sb->inode_blocks = be16_to_cpu(super->fs_cgisize);
144 sb->total_groups = be16_to_cpu(super->fs_ncg);
146 return 0;
149 struct super_block *efs_read_super(struct super_block *s, void *d, int silent) {
150 kdev_t dev = s->s_dev;
151 struct efs_sb_info *sb;
152 struct buffer_head *bh;
154 MOD_INC_USE_COUNT;
155 lock_super(s);
157 sb = SUPER_INFO(s);
159 set_blocksize(dev, EFS_BLOCKSIZE);
161 /* read the vh (volume header) block */
162 bh = bread(dev, 0, EFS_BLOCKSIZE);
164 if (!bh) {
165 printk(KERN_ERR "EFS: cannot read volume header\n");
166 goto out_no_fs_ul;
170 * if this returns zero then we didn't find any partition table.
171 * this isn't (yet) an error - just assume for the moment that
172 * the device is valid and go on to search for a superblock.
174 sb->fs_start = efs_validate_vh((struct volume_header *) bh->b_data);
175 brelse(bh);
177 if (sb->fs_start == -1) {
178 goto out_no_fs_ul;
181 bh = bread(dev, sb->fs_start + EFS_SUPER, EFS_BLOCKSIZE);
182 if (!bh) {
183 printk(KERN_ERR "EFS: cannot read superblock\n");
184 goto out_no_fs_ul;
187 if (efs_validate_super(sb, (struct efs_super *) bh->b_data)) {
188 #ifdef DEBUG
189 printk(KERN_WARNING "EFS: invalid superblock at block %u\n", sb->fs_start + EFS_SUPER);
190 #endif
191 brelse(bh);
192 goto out_no_fs_ul;
194 brelse(bh);
196 s->s_magic = EFS_SUPER_MAGIC;
197 s->s_blocksize = EFS_BLOCKSIZE;
198 s->s_blocksize_bits = EFS_BLOCKSIZE_BITS;
199 if (!(s->s_flags & MS_RDONLY)) {
200 #ifdef DEBUG
201 printk(KERN_INFO "EFS: forcing read-only mode\n");
202 #endif
203 s->s_flags |= MS_RDONLY;
205 s->s_op = &efs_superblock_operations;
206 s->s_dev = dev;
207 s->s_root = d_alloc_root(iget(s, EFS_ROOTINODE));
208 unlock_super(s);
210 if (!(s->s_root)) {
211 printk(KERN_ERR "EFS: get root inode failed\n");
212 goto out_no_fs;
215 if (check_disk_change(s->s_dev)) {
216 printk(KERN_ERR "EFS: device changed\n");
217 goto out_no_fs;
220 return(s);
222 out_no_fs_ul:
223 unlock_super(s);
224 out_no_fs:
225 s->s_dev = 0;
226 MOD_DEC_USE_COUNT;
227 return(NULL);
230 void efs_put_super(struct super_block *s) {
231 MOD_DEC_USE_COUNT;
234 int efs_statfs(struct super_block *s, struct statfs *buf, int bufsiz) {
235 struct statfs ret;
236 struct efs_sb_info *sb = SUPER_INFO(s);
238 ret.f_type = EFS_SUPER_MAGIC; /* efs magic number */
239 ret.f_bsize = EFS_BLOCKSIZE; /* blocksize */
240 ret.f_blocks = sb->total_groups * /* total data blocks */
241 (sb->group_size - sb->inode_blocks);
242 ret.f_bfree = sb->data_free; /* free data blocks */
243 ret.f_bavail = sb->data_free; /* free blocks for non-root */
244 ret.f_files = sb->total_groups * /* total inodes */
245 sb->inode_blocks *
246 (EFS_BLOCKSIZE / sizeof(struct efs_dinode));
247 ret.f_ffree = sb->inode_free; /* free inodes */
248 ret.f_fsid.val[0] = (sb->fs_magic >> 16) & 0xffff; /* fs ID */
249 ret.f_fsid.val[1] = sb->fs_magic & 0xffff; /* fs ID */
250 ret.f_namelen = EFS_MAXNAMELEN; /* max filename length */
252 return copy_to_user(buf, &ret, bufsiz) ? -EFAULT : 0;