[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / sysv / super.c
blobbaf3157204b3f86d4e2f7fa54b8e0b94414553d9
1 /*
2 * linux/fs/sysv/inode.c
4 * minix/inode.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * xenix/inode.c
8 * Copyright (C) 1992 Doug Evans
10 * coh/inode.c
11 * Copyright (C) 1993 Pascal Haible, Bruno Haible
13 * sysv/inode.c
14 * Copyright (C) 1993 Paul B. Monday
16 * sysv/inode.c
17 * Copyright (C) 1993 Bruno Haible
18 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
20 * This file contains code for read/parsing the superblock.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/buffer_head.h>
27 #include "sysv.h"
30 * The following functions try to recognize specific filesystems.
32 * We recognize:
33 * - Xenix FS by its magic number.
34 * - SystemV FS by its magic number.
35 * - Coherent FS by its funny fname/fpack field.
36 * - SCO AFS by s_nfree == 0xffff
37 * - V7 FS has no distinguishing features.
39 * We discriminate among SystemV4 and SystemV2 FS by the assumption that
40 * the time stamp is not < 01-01-1980.
43 enum {
44 JAN_1_1980 = (10*365 + 2) * 24 * 60 * 60
47 static void detected_xenix(struct sysv_sb_info *sbi)
49 struct buffer_head *bh1 = sbi->s_bh1;
50 struct buffer_head *bh2 = sbi->s_bh2;
51 struct xenix_super_block * sbd1;
52 struct xenix_super_block * sbd2;
54 if (bh1 != bh2)
55 sbd1 = sbd2 = (struct xenix_super_block *) bh1->b_data;
56 else {
57 /* block size = 512, so bh1 != bh2 */
58 sbd1 = (struct xenix_super_block *) bh1->b_data;
59 sbd2 = (struct xenix_super_block *) (bh2->b_data - 512);
62 sbi->s_link_max = XENIX_LINK_MAX;
63 sbi->s_fic_size = XENIX_NICINOD;
64 sbi->s_flc_size = XENIX_NICFREE;
65 sbi->s_sbd1 = (char *)sbd1;
66 sbi->s_sbd2 = (char *)sbd2;
67 sbi->s_sb_fic_count = &sbd1->s_ninode;
68 sbi->s_sb_fic_inodes = &sbd1->s_inode[0];
69 sbi->s_sb_total_free_inodes = &sbd2->s_tinode;
70 sbi->s_bcache_count = &sbd1->s_nfree;
71 sbi->s_bcache = &sbd1->s_free[0];
72 sbi->s_free_blocks = &sbd2->s_tfree;
73 sbi->s_sb_time = &sbd2->s_time;
74 sbi->s_firstdatazone = fs16_to_cpu(sbi, sbd1->s_isize);
75 sbi->s_nzones = fs32_to_cpu(sbi, sbd1->s_fsize);
78 static void detected_sysv4(struct sysv_sb_info *sbi)
80 struct sysv4_super_block * sbd;
81 struct buffer_head *bh1 = sbi->s_bh1;
82 struct buffer_head *bh2 = sbi->s_bh2;
84 if (bh1 == bh2)
85 sbd = (struct sysv4_super_block *) (bh1->b_data + BLOCK_SIZE/2);
86 else
87 sbd = (struct sysv4_super_block *) bh2->b_data;
89 sbi->s_link_max = SYSV_LINK_MAX;
90 sbi->s_fic_size = SYSV_NICINOD;
91 sbi->s_flc_size = SYSV_NICFREE;
92 sbi->s_sbd1 = (char *)sbd;
93 sbi->s_sbd2 = (char *)sbd;
94 sbi->s_sb_fic_count = &sbd->s_ninode;
95 sbi->s_sb_fic_inodes = &sbd->s_inode[0];
96 sbi->s_sb_total_free_inodes = &sbd->s_tinode;
97 sbi->s_bcache_count = &sbd->s_nfree;
98 sbi->s_bcache = &sbd->s_free[0];
99 sbi->s_free_blocks = &sbd->s_tfree;
100 sbi->s_sb_time = &sbd->s_time;
101 sbi->s_sb_state = &sbd->s_state;
102 sbi->s_firstdatazone = fs16_to_cpu(sbi, sbd->s_isize);
103 sbi->s_nzones = fs32_to_cpu(sbi, sbd->s_fsize);
106 static void detected_sysv2(struct sysv_sb_info *sbi)
108 struct sysv2_super_block *sbd;
109 struct buffer_head *bh1 = sbi->s_bh1;
110 struct buffer_head *bh2 = sbi->s_bh2;
112 if (bh1 == bh2)
113 sbd = (struct sysv2_super_block *) (bh1->b_data + BLOCK_SIZE/2);
114 else
115 sbd = (struct sysv2_super_block *) bh2->b_data;
117 sbi->s_link_max = SYSV_LINK_MAX;
118 sbi->s_fic_size = SYSV_NICINOD;
119 sbi->s_flc_size = SYSV_NICFREE;
120 sbi->s_sbd1 = (char *)sbd;
121 sbi->s_sbd2 = (char *)sbd;
122 sbi->s_sb_fic_count = &sbd->s_ninode;
123 sbi->s_sb_fic_inodes = &sbd->s_inode[0];
124 sbi->s_sb_total_free_inodes = &sbd->s_tinode;
125 sbi->s_bcache_count = &sbd->s_nfree;
126 sbi->s_bcache = &sbd->s_free[0];
127 sbi->s_free_blocks = &sbd->s_tfree;
128 sbi->s_sb_time = &sbd->s_time;
129 sbi->s_sb_state = &sbd->s_state;
130 sbi->s_firstdatazone = fs16_to_cpu(sbi, sbd->s_isize);
131 sbi->s_nzones = fs32_to_cpu(sbi, sbd->s_fsize);
134 static void detected_coherent(struct sysv_sb_info *sbi)
136 struct coh_super_block * sbd;
137 struct buffer_head *bh1 = sbi->s_bh1;
139 sbd = (struct coh_super_block *) bh1->b_data;
141 sbi->s_link_max = COH_LINK_MAX;
142 sbi->s_fic_size = COH_NICINOD;
143 sbi->s_flc_size = COH_NICFREE;
144 sbi->s_sbd1 = (char *)sbd;
145 sbi->s_sbd2 = (char *)sbd;
146 sbi->s_sb_fic_count = &sbd->s_ninode;
147 sbi->s_sb_fic_inodes = &sbd->s_inode[0];
148 sbi->s_sb_total_free_inodes = &sbd->s_tinode;
149 sbi->s_bcache_count = &sbd->s_nfree;
150 sbi->s_bcache = &sbd->s_free[0];
151 sbi->s_free_blocks = &sbd->s_tfree;
152 sbi->s_sb_time = &sbd->s_time;
153 sbi->s_firstdatazone = fs16_to_cpu(sbi, sbd->s_isize);
154 sbi->s_nzones = fs32_to_cpu(sbi, sbd->s_fsize);
157 static void detected_v7(struct sysv_sb_info *sbi)
159 struct buffer_head *bh2 = sbi->s_bh2;
160 struct v7_super_block *sbd = (struct v7_super_block *)bh2->b_data;
162 sbi->s_link_max = V7_LINK_MAX;
163 sbi->s_fic_size = V7_NICINOD;
164 sbi->s_flc_size = V7_NICFREE;
165 sbi->s_sbd1 = (char *)sbd;
166 sbi->s_sbd2 = (char *)sbd;
167 sbi->s_sb_fic_count = &sbd->s_ninode;
168 sbi->s_sb_fic_inodes = &sbd->s_inode[0];
169 sbi->s_sb_total_free_inodes = &sbd->s_tinode;
170 sbi->s_bcache_count = &sbd->s_nfree;
171 sbi->s_bcache = &sbd->s_free[0];
172 sbi->s_free_blocks = &sbd->s_tfree;
173 sbi->s_sb_time = &sbd->s_time;
174 sbi->s_firstdatazone = fs16_to_cpu(sbi, sbd->s_isize);
175 sbi->s_nzones = fs32_to_cpu(sbi, sbd->s_fsize);
178 static int detect_xenix(struct sysv_sb_info *sbi, struct buffer_head *bh)
180 struct xenix_super_block *sbd = (struct xenix_super_block *)bh->b_data;
181 if (sbd->s_magic == cpu_to_le32(0x2b5544))
182 sbi->s_bytesex = BYTESEX_LE;
183 else if (sbd->s_magic == cpu_to_be32(0x2b5544))
184 sbi->s_bytesex = BYTESEX_BE;
185 else
186 return 0;
187 if (sbd->s_type > 2 || sbd->s_type < 1)
188 return 0;
189 sbi->s_type = FSTYPE_XENIX;
190 return sbd->s_type;
193 static int detect_sysv(struct sysv_sb_info *sbi, struct buffer_head *bh)
195 struct super_block *sb = sbi->s_sb;
196 /* All relevant fields are at the same offsets in R2 and R4 */
197 struct sysv4_super_block * sbd;
199 sbd = (struct sysv4_super_block *) (bh->b_data + BLOCK_SIZE/2);
200 if (sbd->s_magic == cpu_to_le32(0xfd187e20))
201 sbi->s_bytesex = BYTESEX_LE;
202 else if (sbd->s_magic == cpu_to_be32(0xfd187e20))
203 sbi->s_bytesex = BYTESEX_BE;
204 else
205 return 0;
207 if (fs16_to_cpu(sbi, sbd->s_nfree) == 0xffff) {
208 sbi->s_type = FSTYPE_AFS;
209 if (!(sb->s_flags & MS_RDONLY)) {
210 printk("SysV FS: SCO EAFS on %s detected, "
211 "forcing read-only mode.\n",
212 sb->s_id);
213 sb->s_flags |= MS_RDONLY;
215 return sbd->s_type;
218 if (fs32_to_cpu(sbi, sbd->s_time) < JAN_1_1980) {
219 /* this is likely to happen on SystemV2 FS */
220 if (sbd->s_type > 3 || sbd->s_type < 1)
221 return 0;
222 sbi->s_type = FSTYPE_SYSV2;
223 return sbd->s_type;
225 if ((sbd->s_type > 3 || sbd->s_type < 1) &&
226 (sbd->s_type > 0x30 || sbd->s_type < 0x10))
227 return 0;
229 /* On Interactive Unix (ISC) Version 4.0/3.x s_type field = 0x10,
230 0x20 or 0x30 indicates that symbolic links and the 14-character
231 filename limit is gone. Due to lack of information about this
232 feature read-only mode seems to be a reasonable approach... -KGB */
234 if (sbd->s_type >= 0x10) {
235 printk("SysV FS: can't handle long file names on %s, "
236 "forcing read-only mode.\n", sb->s_id);
237 sb->s_flags |= MS_RDONLY;
240 sbi->s_type = FSTYPE_SYSV4;
241 return sbd->s_type >= 0x10 ? (sbd->s_type >> 4) : sbd->s_type;
244 static int detect_coherent(struct sysv_sb_info *sbi, struct buffer_head *bh)
246 struct coh_super_block * sbd;
248 sbd = (struct coh_super_block *) (bh->b_data + BLOCK_SIZE/2);
249 if ((memcmp(sbd->s_fname,"noname",6) && memcmp(sbd->s_fname,"xxxxx ",6))
250 || (memcmp(sbd->s_fpack,"nopack",6) && memcmp(sbd->s_fpack,"xxxxx\n",6)))
251 return 0;
252 sbi->s_bytesex = BYTESEX_PDP;
253 sbi->s_type = FSTYPE_COH;
254 return 1;
257 static int detect_sysv_odd(struct sysv_sb_info *sbi, struct buffer_head *bh)
259 int size = detect_sysv(sbi, bh);
261 return size>2 ? 0 : size;
264 static struct {
265 int block;
266 int (*test)(struct sysv_sb_info *, struct buffer_head *);
267 } flavours[] = {
268 {1, detect_xenix},
269 {0, detect_sysv},
270 {0, detect_coherent},
271 {9, detect_sysv_odd},
272 {15,detect_sysv_odd},
273 {18,detect_sysv},
276 static char *flavour_names[] = {
277 [FSTYPE_XENIX] = "Xenix",
278 [FSTYPE_SYSV4] = "SystemV",
279 [FSTYPE_SYSV2] = "SystemV Release 2",
280 [FSTYPE_COH] = "Coherent",
281 [FSTYPE_V7] = "V7",
282 [FSTYPE_AFS] = "AFS",
285 static void (*flavour_setup[])(struct sysv_sb_info *) = {
286 [FSTYPE_XENIX] = detected_xenix,
287 [FSTYPE_SYSV4] = detected_sysv4,
288 [FSTYPE_SYSV2] = detected_sysv2,
289 [FSTYPE_COH] = detected_coherent,
290 [FSTYPE_V7] = detected_v7,
291 [FSTYPE_AFS] = detected_sysv4,
294 static int complete_read_super(struct super_block *sb, int silent, int size)
296 struct sysv_sb_info *sbi = SYSV_SB(sb);
297 struct inode *root_inode;
298 char *found = flavour_names[sbi->s_type];
299 u_char n_bits = size+8;
300 int bsize = 1 << n_bits;
301 int bsize_4 = bsize >> 2;
303 sbi->s_firstinodezone = 2;
305 flavour_setup[sbi->s_type](sbi);
307 sbi->s_truncate = 1;
308 sbi->s_ndatazones = sbi->s_nzones - sbi->s_firstdatazone;
309 sbi->s_inodes_per_block = bsize >> 6;
310 sbi->s_inodes_per_block_1 = (bsize >> 6)-1;
311 sbi->s_inodes_per_block_bits = n_bits-6;
312 sbi->s_ind_per_block = bsize_4;
313 sbi->s_ind_per_block_2 = bsize_4*bsize_4;
314 sbi->s_toobig_block = 10 + bsize_4 * (1 + bsize_4 * (1 + bsize_4));
315 sbi->s_ind_per_block_bits = n_bits-2;
317 sbi->s_ninodes = (sbi->s_firstdatazone - sbi->s_firstinodezone)
318 << sbi->s_inodes_per_block_bits;
320 if (!silent)
321 printk("VFS: Found a %s FS (block size = %ld) on device %s\n",
322 found, sb->s_blocksize, sb->s_id);
324 sb->s_magic = SYSV_MAGIC_BASE + sbi->s_type;
325 /* set up enough so that it can read an inode */
326 sb->s_op = &sysv_sops;
327 root_inode = iget(sb,SYSV_ROOT_INO);
328 if (!root_inode || is_bad_inode(root_inode)) {
329 printk("SysV FS: get root inode failed\n");
330 return 0;
332 sb->s_root = d_alloc_root(root_inode);
333 if (!sb->s_root) {
334 iput(root_inode);
335 printk("SysV FS: get root dentry failed\n");
336 return 0;
338 if (sbi->s_truncate)
339 sb->s_root->d_op = &sysv_dentry_operations;
340 sb->s_flags |= MS_RDONLY;
341 sb->s_dirt = 1;
342 return 1;
345 static int sysv_fill_super(struct super_block *sb, void *data, int silent)
347 struct buffer_head *bh1, *bh = NULL;
348 struct sysv_sb_info *sbi;
349 unsigned long blocknr;
350 int size = 0, i;
352 if (1024 != sizeof (struct xenix_super_block))
353 panic("Xenix FS: bad superblock size");
354 if (512 != sizeof (struct sysv4_super_block))
355 panic("SystemV FS: bad superblock size");
356 if (512 != sizeof (struct sysv2_super_block))
357 panic("SystemV FS: bad superblock size");
358 if (500 != sizeof (struct coh_super_block))
359 panic("Coherent FS: bad superblock size");
360 if (64 != sizeof (struct sysv_inode))
361 panic("sysv fs: bad inode size");
363 sbi = kmalloc(sizeof(struct sysv_sb_info), GFP_KERNEL);
364 if (!sbi)
365 return -ENOMEM;
366 memset(sbi, 0, sizeof(struct sysv_sb_info));
368 sbi->s_sb = sb;
369 sbi->s_block_base = 0;
370 sb->s_fs_info = sbi;
372 sb_set_blocksize(sb, BLOCK_SIZE);
374 for (i = 0; i < sizeof(flavours)/sizeof(flavours[0]) && !size; i++) {
375 brelse(bh);
376 bh = sb_bread(sb, flavours[i].block);
377 if (!bh)
378 continue;
379 size = flavours[i].test(SYSV_SB(sb), bh);
382 if (!size)
383 goto Eunknown;
385 switch (size) {
386 case 1:
387 blocknr = bh->b_blocknr << 1;
388 brelse(bh);
389 sb_set_blocksize(sb, 512);
390 bh1 = sb_bread(sb, blocknr);
391 bh = sb_bread(sb, blocknr + 1);
392 break;
393 case 2:
394 bh1 = bh;
395 break;
396 case 3:
397 blocknr = bh->b_blocknr >> 1;
398 brelse(bh);
399 sb_set_blocksize(sb, 2048);
400 bh1 = bh = sb_bread(sb, blocknr);
401 break;
402 default:
403 goto Ebadsize;
406 if (bh && bh1) {
407 sbi->s_bh1 = bh1;
408 sbi->s_bh2 = bh;
409 if (complete_read_super(sb, silent, size))
410 return 0;
413 brelse(bh1);
414 brelse(bh);
415 sb_set_blocksize(sb, BLOCK_SIZE);
416 printk("oldfs: cannot read superblock\n");
417 failed:
418 kfree(sbi);
419 return -EINVAL;
421 Eunknown:
422 brelse(bh);
423 if (!silent)
424 printk("VFS: unable to find oldfs superblock on device %s\n",
425 sb->s_id);
426 goto failed;
427 Ebadsize:
428 brelse(bh);
429 if (!silent)
430 printk("VFS: oldfs: unsupported block size (%dKb)\n",
431 1<<(size-2));
432 goto failed;
435 static int v7_fill_super(struct super_block *sb, void *data, int silent)
437 struct sysv_sb_info *sbi;
438 struct buffer_head *bh, *bh2 = NULL;
439 struct v7_super_block *v7sb;
440 struct sysv_inode *v7i;
442 if (440 != sizeof (struct v7_super_block))
443 panic("V7 FS: bad super-block size");
444 if (64 != sizeof (struct sysv_inode))
445 panic("sysv fs: bad i-node size");
447 sbi = kmalloc(sizeof(struct sysv_sb_info), GFP_KERNEL);
448 if (!sbi)
449 return -ENOMEM;
450 memset(sbi, 0, sizeof(struct sysv_sb_info));
452 sbi->s_sb = sb;
453 sbi->s_block_base = 0;
454 sbi->s_type = FSTYPE_V7;
455 sbi->s_bytesex = BYTESEX_PDP;
456 sb->s_fs_info = sbi;
458 sb_set_blocksize(sb, 512);
460 if ((bh = sb_bread(sb, 1)) == NULL) {
461 if (!silent)
462 printk("VFS: unable to read V7 FS superblock on "
463 "device %s.\n", sb->s_id);
464 goto failed;
467 /* plausibility check on superblock */
468 v7sb = (struct v7_super_block *) bh->b_data;
469 if (fs16_to_cpu(sbi, v7sb->s_nfree) > V7_NICFREE ||
470 fs16_to_cpu(sbi, v7sb->s_ninode) > V7_NICINOD ||
471 fs32_to_cpu(sbi, v7sb->s_time) == 0)
472 goto failed;
474 /* plausibility check on root inode: it is a directory,
475 with a nonzero size that is a multiple of 16 */
476 if ((bh2 = sb_bread(sb, 2)) == NULL)
477 goto failed;
478 v7i = (struct sysv_inode *)(bh2->b_data + 64);
479 if ((fs16_to_cpu(sbi, v7i->i_mode) & ~0777) != S_IFDIR ||
480 (fs32_to_cpu(sbi, v7i->i_size) == 0) ||
481 (fs32_to_cpu(sbi, v7i->i_size) & 017) != 0)
482 goto failed;
483 brelse(bh2);
485 sbi->s_bh1 = bh;
486 sbi->s_bh2 = bh;
487 if (complete_read_super(sb, silent, 1))
488 return 0;
490 failed:
491 brelse(bh2);
492 brelse(bh);
493 kfree(sbi);
494 return -EINVAL;
497 /* Every kernel module contains stuff like this. */
499 static struct super_block *sysv_get_sb(struct file_system_type *fs_type,
500 int flags, const char *dev_name, void *data)
502 return get_sb_bdev(fs_type, flags, dev_name, data, sysv_fill_super);
505 static struct super_block *v7_get_sb(struct file_system_type *fs_type,
506 int flags, const char *dev_name, void *data)
508 return get_sb_bdev(fs_type, flags, dev_name, data, v7_fill_super);
511 static struct file_system_type sysv_fs_type = {
512 .owner = THIS_MODULE,
513 .name = "sysv",
514 .get_sb = sysv_get_sb,
515 .kill_sb = kill_block_super,
516 .fs_flags = FS_REQUIRES_DEV,
519 static struct file_system_type v7_fs_type = {
520 .owner = THIS_MODULE,
521 .name = "v7",
522 .get_sb = v7_get_sb,
523 .kill_sb = kill_block_super,
524 .fs_flags = FS_REQUIRES_DEV,
527 extern int sysv_init_icache(void) __init;
528 extern void sysv_destroy_icache(void);
530 static int __init init_sysv_fs(void)
532 int error;
534 error = sysv_init_icache();
535 if (error)
536 goto out;
537 error = register_filesystem(&sysv_fs_type);
538 if (error)
539 goto destroy_icache;
540 error = register_filesystem(&v7_fs_type);
541 if (error)
542 goto unregister;
543 return 0;
545 unregister:
546 unregister_filesystem(&sysv_fs_type);
547 destroy_icache:
548 sysv_destroy_icache();
549 out:
550 return error;
553 static void __exit exit_sysv_fs(void)
555 unregister_filesystem(&sysv_fs_type);
556 unregister_filesystem(&v7_fs_type);
557 sysv_destroy_icache();
560 module_init(init_sysv_fs)
561 module_exit(exit_sysv_fs)
562 MODULE_LICENSE("GPL");