]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/sysv/super.c
Merge branch 'uc-logic' into for-linus
[net-next-2.6.git] / fs / sysv / super.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/sysv/inode.c
3 *
4 * minix/inode.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * xenix/inode.c
8 * Copyright (C) 1992 Doug Evans
9 *
10 * coh/inode.c
11 * Copyright (C) 1993 Pascal Haible, Bruno Haible
12 *
13 * sysv/inode.c
14 * Copyright (C) 1993 Paul B. Monday
15 *
16 * sysv/inode.c
17 * Copyright (C) 1993 Bruno Haible
18 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
19 *
20 * This file contains code for read/parsing the superblock.
21 */
22
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"
28
29/*
30 * The following functions try to recognize specific filesystems.
31 *
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.
38 *
39 * We discriminate among SystemV4 and SystemV2 FS by the assumption that
40 * the time stamp is not < 01-01-1980.
41 */
42
43enum {
44 JAN_1_1980 = (10*365 + 2) * 24 * 60 * 60
45};
46
47static void detected_xenix(struct sysv_sb_info *sbi)
48{
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;
53
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);
60 }
61
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);
76}
77
78static void detected_sysv4(struct sysv_sb_info *sbi)
79{
80 struct sysv4_super_block * sbd;
81 struct buffer_head *bh1 = sbi->s_bh1;
82 struct buffer_head *bh2 = sbi->s_bh2;
83
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;
88
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);
104}
105
106static void detected_sysv2(struct sysv_sb_info *sbi)
107{
108 struct sysv2_super_block *sbd;
109 struct buffer_head *bh1 = sbi->s_bh1;
110 struct buffer_head *bh2 = sbi->s_bh2;
111
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;
116
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);
132}
133
134static void detected_coherent(struct sysv_sb_info *sbi)
135{
136 struct coh_super_block * sbd;
137 struct buffer_head *bh1 = sbi->s_bh1;
138
139 sbd = (struct coh_super_block *) bh1->b_data;
140
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);
155}
156
157static void detected_v7(struct sysv_sb_info *sbi)
158{
159 struct buffer_head *bh2 = sbi->s_bh2;
160 struct v7_super_block *sbd = (struct v7_super_block *)bh2->b_data;
161
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);
176}
177
178static int detect_xenix(struct sysv_sb_info *sbi, struct buffer_head *bh)
179{
180 struct xenix_super_block *sbd = (struct xenix_super_block *)bh->b_data;
181 if (*(__le32 *)&sbd->s_magic == cpu_to_le32(0x2b5544))
182 sbi->s_bytesex = BYTESEX_LE;
183 else if (*(__be32 *)&sbd->s_magic == cpu_to_be32(0x2b5544))
184 sbi->s_bytesex = BYTESEX_BE;
185 else
186 return 0;
187 switch (fs32_to_cpu(sbi, sbd->s_type)) {
188 case 1:
189 sbi->s_type = FSTYPE_XENIX;
190 return 1;
191 case 2:
192 sbi->s_type = FSTYPE_XENIX;
193 return 2;
194 default:
195 return 0;
196 }
197}
198
199static int detect_sysv(struct sysv_sb_info *sbi, struct buffer_head *bh)
200{
201 struct super_block *sb = sbi->s_sb;
202 /* All relevant fields are at the same offsets in R2 and R4 */
203 struct sysv4_super_block * sbd;
204 u32 type;
205
206 sbd = (struct sysv4_super_block *) (bh->b_data + BLOCK_SIZE/2);
207 if (*(__le32 *)&sbd->s_magic == cpu_to_le32(0xfd187e20))
208 sbi->s_bytesex = BYTESEX_LE;
209 else if (*(__be32 *)&sbd->s_magic == cpu_to_be32(0xfd187e20))
210 sbi->s_bytesex = BYTESEX_BE;
211 else
212 return 0;
213
214 type = fs32_to_cpu(sbi, sbd->s_type);
215
216 if (fs16_to_cpu(sbi, sbd->s_nfree) == 0xffff) {
217 sbi->s_type = FSTYPE_AFS;
218 sbi->s_forced_ro = 1;
219 if (!(sb->s_flags & MS_RDONLY)) {
220 printk("SysV FS: SCO EAFS on %s detected, "
221 "forcing read-only mode.\n",
222 sb->s_id);
223 }
224 return type;
225 }
226
227 if (fs32_to_cpu(sbi, sbd->s_time) < JAN_1_1980) {
228 /* this is likely to happen on SystemV2 FS */
229 if (type > 3 || type < 1)
230 return 0;
231 sbi->s_type = FSTYPE_SYSV2;
232 return type;
233 }
234 if ((type > 3 || type < 1) && (type > 0x30 || type < 0x10))
235 return 0;
236
237 /* On Interactive Unix (ISC) Version 4.0/3.x s_type field = 0x10,
238 0x20 or 0x30 indicates that symbolic links and the 14-character
239 filename limit is gone. Due to lack of information about this
240 feature read-only mode seems to be a reasonable approach... -KGB */
241
242 if (type >= 0x10) {
243 printk("SysV FS: can't handle long file names on %s, "
244 "forcing read-only mode.\n", sb->s_id);
245 sbi->s_forced_ro = 1;
246 }
247
248 sbi->s_type = FSTYPE_SYSV4;
249 return type >= 0x10 ? type >> 4 : type;
250}
251
252static int detect_coherent(struct sysv_sb_info *sbi, struct buffer_head *bh)
253{
254 struct coh_super_block * sbd;
255
256 sbd = (struct coh_super_block *) (bh->b_data + BLOCK_SIZE/2);
257 if ((memcmp(sbd->s_fname,"noname",6) && memcmp(sbd->s_fname,"xxxxx ",6))
258 || (memcmp(sbd->s_fpack,"nopack",6) && memcmp(sbd->s_fpack,"xxxxx\n",6)))
259 return 0;
260 sbi->s_bytesex = BYTESEX_PDP;
261 sbi->s_type = FSTYPE_COH;
262 return 1;
263}
264
265static int detect_sysv_odd(struct sysv_sb_info *sbi, struct buffer_head *bh)
266{
267 int size = detect_sysv(sbi, bh);
268
269 return size>2 ? 0 : size;
270}
271
272static struct {
273 int block;
274 int (*test)(struct sysv_sb_info *, struct buffer_head *);
275} flavours[] = {
276 {1, detect_xenix},
277 {0, detect_sysv},
278 {0, detect_coherent},
279 {9, detect_sysv_odd},
280 {15,detect_sysv_odd},
281 {18,detect_sysv},
282};
283
284static char *flavour_names[] = {
285 [FSTYPE_XENIX] = "Xenix",
286 [FSTYPE_SYSV4] = "SystemV",
287 [FSTYPE_SYSV2] = "SystemV Release 2",
288 [FSTYPE_COH] = "Coherent",
289 [FSTYPE_V7] = "V7",
290 [FSTYPE_AFS] = "AFS",
291};
292
293static void (*flavour_setup[])(struct sysv_sb_info *) = {
294 [FSTYPE_XENIX] = detected_xenix,
295 [FSTYPE_SYSV4] = detected_sysv4,
296 [FSTYPE_SYSV2] = detected_sysv2,
297 [FSTYPE_COH] = detected_coherent,
298 [FSTYPE_V7] = detected_v7,
299 [FSTYPE_AFS] = detected_sysv4,
300};
301
302static int complete_read_super(struct super_block *sb, int silent, int size)
303{
304 struct sysv_sb_info *sbi = SYSV_SB(sb);
305 struct inode *root_inode;
306 char *found = flavour_names[sbi->s_type];
307 u_char n_bits = size+8;
308 int bsize = 1 << n_bits;
309 int bsize_4 = bsize >> 2;
310
311 sbi->s_firstinodezone = 2;
312
313 flavour_setup[sbi->s_type](sbi);
314
315 sbi->s_truncate = 1;
316 sbi->s_ndatazones = sbi->s_nzones - sbi->s_firstdatazone;
317 sbi->s_inodes_per_block = bsize >> 6;
318 sbi->s_inodes_per_block_1 = (bsize >> 6)-1;
319 sbi->s_inodes_per_block_bits = n_bits-6;
320 sbi->s_ind_per_block = bsize_4;
321 sbi->s_ind_per_block_2 = bsize_4*bsize_4;
322 sbi->s_toobig_block = 10 + bsize_4 * (1 + bsize_4 * (1 + bsize_4));
323 sbi->s_ind_per_block_bits = n_bits-2;
324
325 sbi->s_ninodes = (sbi->s_firstdatazone - sbi->s_firstinodezone)
326 << sbi->s_inodes_per_block_bits;
327
328 if (!silent)
329 printk("VFS: Found a %s FS (block size = %ld) on device %s\n",
330 found, sb->s_blocksize, sb->s_id);
331
332 sb->s_magic = SYSV_MAGIC_BASE + sbi->s_type;
333 /* set up enough so that it can read an inode */
334 sb->s_op = &sysv_sops;
b8e1343f
DH
335 root_inode = sysv_iget(sb, SYSV_ROOT_INO);
336 if (IS_ERR(root_inode)) {
1da177e4
LT
337 printk("SysV FS: get root inode failed\n");
338 return 0;
339 }
340 sb->s_root = d_alloc_root(root_inode);
341 if (!sb->s_root) {
342 iput(root_inode);
343 printk("SysV FS: get root dentry failed\n");
344 return 0;
345 }
346 if (sbi->s_forced_ro)
347 sb->s_flags |= MS_RDONLY;
348 if (sbi->s_truncate)
349 sb->s_root->d_op = &sysv_dentry_operations;
1da177e4
LT
350 return 1;
351}
352
353static int sysv_fill_super(struct super_block *sb, void *data, int silent)
354{
355 struct buffer_head *bh1, *bh = NULL;
356 struct sysv_sb_info *sbi;
357 unsigned long blocknr;
358 int size = 0, i;
359
2ecd05ae
AD
360 BUILD_BUG_ON(1024 != sizeof (struct xenix_super_block));
361 BUILD_BUG_ON(512 != sizeof (struct sysv4_super_block));
362 BUILD_BUG_ON(512 != sizeof (struct sysv2_super_block));
363 BUILD_BUG_ON(500 != sizeof (struct coh_super_block));
364 BUILD_BUG_ON(64 != sizeof (struct sysv_inode));
1da177e4 365
f8314dc6 366 sbi = kzalloc(sizeof(struct sysv_sb_info), GFP_KERNEL);
1da177e4
LT
367 if (!sbi)
368 return -ENOMEM;
1da177e4
LT
369
370 sbi->s_sb = sb;
371 sbi->s_block_base = 0;
372 sb->s_fs_info = sbi;
e8c96f8c 373
1da177e4
LT
374 sb_set_blocksize(sb, BLOCK_SIZE);
375
e8c96f8c 376 for (i = 0; i < ARRAY_SIZE(flavours) && !size; i++) {
1da177e4
LT
377 brelse(bh);
378 bh = sb_bread(sb, flavours[i].block);
379 if (!bh)
380 continue;
381 size = flavours[i].test(SYSV_SB(sb), bh);
382 }
383
384 if (!size)
385 goto Eunknown;
386
387 switch (size) {
388 case 1:
389 blocknr = bh->b_blocknr << 1;
390 brelse(bh);
391 sb_set_blocksize(sb, 512);
392 bh1 = sb_bread(sb, blocknr);
393 bh = sb_bread(sb, blocknr + 1);
394 break;
395 case 2:
396 bh1 = bh;
397 break;
398 case 3:
399 blocknr = bh->b_blocknr >> 1;
400 brelse(bh);
401 sb_set_blocksize(sb, 2048);
402 bh1 = bh = sb_bread(sb, blocknr);
403 break;
404 default:
405 goto Ebadsize;
406 }
407
408 if (bh && bh1) {
409 sbi->s_bh1 = bh1;
410 sbi->s_bh2 = bh;
411 if (complete_read_super(sb, silent, size))
412 return 0;
413 }
414
415 brelse(bh1);
416 brelse(bh);
417 sb_set_blocksize(sb, BLOCK_SIZE);
418 printk("oldfs: cannot read superblock\n");
419failed:
420 kfree(sbi);
421 return -EINVAL;
422
423Eunknown:
424 brelse(bh);
425 if (!silent)
426 printk("VFS: unable to find oldfs superblock on device %s\n",
427 sb->s_id);
428 goto failed;
429Ebadsize:
430 brelse(bh);
431 if (!silent)
432 printk("VFS: oldfs: unsupported block size (%dKb)\n",
433 1<<(size-2));
434 goto failed;
435}
436
6d0b5456 437static int v7_sanity_check(struct super_block *sb, struct buffer_head *bh)
1da177e4 438{
1da177e4
LT
439 struct v7_super_block *v7sb;
440 struct sysv_inode *v7i;
6d0b5456
LR
441 struct buffer_head *bh2;
442 struct sysv_sb_info *sbi;
443
444 sbi = sb->s_fs_info;
445
446 /* plausibility check on superblock */
447 v7sb = (struct v7_super_block *) bh->b_data;
448 if (fs16_to_cpu(sbi, v7sb->s_nfree) > V7_NICFREE ||
449 fs16_to_cpu(sbi, v7sb->s_ninode) > V7_NICINOD ||
450 fs32_to_cpu(sbi, v7sb->s_fsize) > V7_MAXSIZE)
451 return 0;
452
453 /* plausibility check on root inode: it is a directory,
454 with a nonzero size that is a multiple of 16 */
455 bh2 = sb_bread(sb, 2);
456 if (bh2 == NULL)
457 return 0;
458
459 v7i = (struct sysv_inode *)(bh2->b_data + 64);
460 if ((fs16_to_cpu(sbi, v7i->i_mode) & ~0777) != S_IFDIR ||
461 (fs32_to_cpu(sbi, v7i->i_size) == 0) ||
462 (fs32_to_cpu(sbi, v7i->i_size) & 017) ||
463 (fs32_to_cpu(sbi, v7i->i_size) > V7_NFILES *
464 sizeof(struct sysv_dir_entry))) {
465 brelse(bh2);
466 return 0;
467 }
468
469 brelse(bh2);
470 return 1;
471}
472
473static int v7_fill_super(struct super_block *sb, void *data, int silent)
474{
475 struct sysv_sb_info *sbi;
476 struct buffer_head *bh;
1da177e4
LT
477
478 if (440 != sizeof (struct v7_super_block))
479 panic("V7 FS: bad super-block size");
480 if (64 != sizeof (struct sysv_inode))
481 panic("sysv fs: bad i-node size");
482
f8314dc6 483 sbi = kzalloc(sizeof(struct sysv_sb_info), GFP_KERNEL);
1da177e4
LT
484 if (!sbi)
485 return -ENOMEM;
1da177e4
LT
486
487 sbi->s_sb = sb;
488 sbi->s_block_base = 0;
489 sbi->s_type = FSTYPE_V7;
1da177e4
LT
490 sb->s_fs_info = sbi;
491
492 sb_set_blocksize(sb, 512);
493
494 if ((bh = sb_bread(sb, 1)) == NULL) {
495 if (!silent)
496 printk("VFS: unable to read V7 FS superblock on "
497 "device %s.\n", sb->s_id);
498 goto failed;
499 }
500
6d0b5456
LR
501 /* Try PDP-11 UNIX */
502 sbi->s_bytesex = BYTESEX_PDP;
503 if (v7_sanity_check(sb, bh))
504 goto detected;
1da177e4 505
6d0b5456
LR
506 /* Try PC/IX, v7/x86 */
507 sbi->s_bytesex = BYTESEX_LE;
508 if (v7_sanity_check(sb, bh))
509 goto detected;
1da177e4 510
6d0b5456
LR
511 goto failed;
512
513detected:
1da177e4
LT
514 sbi->s_bh1 = bh;
515 sbi->s_bh2 = bh;
516 if (complete_read_super(sb, silent, 1))
517 return 0;
518
519failed:
6d0b5456
LR
520 printk(KERN_ERR "VFS: could not find a valid V7 on %s.\n",
521 sb->s_id);
1da177e4
LT
522 brelse(bh);
523 kfree(sbi);
524 return -EINVAL;
525}
526
527/* Every kernel module contains stuff like this. */
528
454e2398
DH
529static int sysv_get_sb(struct file_system_type *fs_type,
530 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1da177e4 531{
454e2398
DH
532 return get_sb_bdev(fs_type, flags, dev_name, data, sysv_fill_super,
533 mnt);
1da177e4
LT
534}
535
454e2398
DH
536static int v7_get_sb(struct file_system_type *fs_type,
537 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1da177e4 538{
454e2398 539 return get_sb_bdev(fs_type, flags, dev_name, data, v7_fill_super, mnt);
1da177e4
LT
540}
541
542static struct file_system_type sysv_fs_type = {
543 .owner = THIS_MODULE,
544 .name = "sysv",
545 .get_sb = sysv_get_sb,
546 .kill_sb = kill_block_super,
547 .fs_flags = FS_REQUIRES_DEV,
548};
549
550static struct file_system_type v7_fs_type = {
551 .owner = THIS_MODULE,
552 .name = "v7",
553 .get_sb = v7_get_sb,
554 .kill_sb = kill_block_super,
555 .fs_flags = FS_REQUIRES_DEV,
556};
557
1da177e4
LT
558static int __init init_sysv_fs(void)
559{
560 int error;
561
562 error = sysv_init_icache();
563 if (error)
564 goto out;
565 error = register_filesystem(&sysv_fs_type);
566 if (error)
567 goto destroy_icache;
568 error = register_filesystem(&v7_fs_type);
569 if (error)
570 goto unregister;
571 return 0;
572
573unregister:
574 unregister_filesystem(&sysv_fs_type);
575destroy_icache:
576 sysv_destroy_icache();
577out:
578 return error;
579}
580
581static void __exit exit_sysv_fs(void)
582{
583 unregister_filesystem(&sysv_fs_type);
584 unregister_filesystem(&v7_fs_type);
585 sysv_destroy_icache();
586}
587
588module_init(init_sysv_fs)
589module_exit(exit_sysv_fs)
b76212d7 590MODULE_ALIAS("v7");
1da177e4 591MODULE_LICENSE("GPL");