pre-2.3.4..
[davej-history.git] / fs / ufs / super.c
blob4fe11c5648373b7d88674237231ffd0a2ac319f2
1 /*
2 * linux/fs/ufs/super.c
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 */
9 /* Derived from
11 * linux/fs/ext2/super.c
13 * Copyright (C) 1992, 1993, 1994, 1995
14 * Remy Card (card@masi.ibp.fr)
15 * Laboratoire MASI - Institut Blaise Pascal
16 * Universite Pierre et Marie Curie (Paris VI)
18 * from
20 * linux/fs/minix/inode.c
22 * Copyright (C) 1991, 1992 Linus Torvalds
24 * Big-endian to little-endian byte-swapping/bitmaps by
25 * David S. Miller (davem@caip.rutgers.edu), 1995
29 * Inspired by
31 * linux/fs/ufs/super.c
33 * Copyright (C) 1996
34 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
35 * Laboratory for Computer Science Research Computing Facility
36 * Rutgers, The State University of New Jersey
38 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
40 * Kernel module support added on 96/04/26 by
41 * Stefan Reinauer <stepan@home.culture.mipt.ru>
43 * Module usage counts added on 96/04/29 by
44 * Gertjan van Wingerde <gertjan@cs.vu.nl>
46 * Clean swab support on 19970406 by
47 * Francois-Rene Rideau <fare@tunes.org>
49 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
50 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
51 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
53 * NeXTstep support added on February 5th 1998 by
54 * Niels Kristian Bech Jensen <nkbj@image.dk>.
56 * write support Daniel Pirkl <daniel.pirkl@email.cz> 1998
61 #include <linux/config.h>
62 #include <linux/module.h>
64 #include <stdarg.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
68 #include <asm/system.h>
70 #include <linux/errno.h>
71 #include <linux/fs.h>
72 #include <linux/ufs_fs.h>
73 #include <linux/malloc.h>
74 #include <linux/sched.h>
75 #include <linux/stat.h>
76 #include <linux/string.h>
77 #include <linux/locks.h>
78 #include <linux/blkdev.h>
79 #include <linux/init.h>
81 #include "swab.h"
82 #include "util.h"
84 #undef UFS_SUPER_DEBUG
85 #undef UFS_SUPER_DEBUG_MORE
87 #ifdef UFS_SUPER_DEBUG
88 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
89 #else
90 #define UFSD(x)
91 #endif
93 #ifdef UFS_SUPER_DEBUG_MORE
95 * Print contents of ufs_super_block, useful for debugging
97 void ufs_print_super_stuff(struct ufs_super_block_first * usb1,
98 struct ufs_super_block_second * usb2,
99 struct ufs_super_block_third * usb3, unsigned swab)
101 printk("ufs_print_super_stuff\n");
102 printk("size of usb: %u\n", sizeof(struct ufs_super_block));
103 printk(" magic: 0x%x\n", SWAB32(usb3->fs_magic));
104 printk(" sblkno: %u\n", SWAB32(usb1->fs_sblkno));
105 printk(" cblkno: %u\n", SWAB32(usb1->fs_cblkno));
106 printk(" iblkno: %u\n", SWAB32(usb1->fs_iblkno));
107 printk(" dblkno: %u\n", SWAB32(usb1->fs_dblkno));
108 printk(" cgoffset: %u\n", SWAB32(usb1->fs_cgoffset));
109 printk(" ~cgmask: 0x%x\n", ~SWAB32(usb1->fs_cgmask));
110 printk(" size: %u\n", SWAB32(usb1->fs_size));
111 printk(" dsize: %u\n", SWAB32(usb1->fs_dsize));
112 printk(" ncg: %u\n", SWAB32(usb1->fs_ncg));
113 printk(" bsize: %u\n", SWAB32(usb1->fs_bsize));
114 printk(" fsize: %u\n", SWAB32(usb1->fs_fsize));
115 printk(" frag: %u\n", SWAB32(usb1->fs_frag));
116 printk(" fragshift: %u\n", SWAB32(usb1->fs_fragshift));
117 printk(" ~fmask: %u\n", ~SWAB32(usb1->fs_fmask));
118 printk(" fshift: %u\n", SWAB32(usb1->fs_fshift));
119 printk(" sbsize: %u\n", SWAB32(usb1->fs_sbsize));
120 printk(" spc: %u\n", SWAB32(usb1->fs_spc));
121 printk(" cpg: %u\n", SWAB32(usb1->fs_cpg));
122 printk(" ipg: %u\n", SWAB32(usb1->fs_ipg));
123 printk(" fpg: %u\n", SWAB32(usb1->fs_fpg));
124 printk(" csaddr: %u\n", SWAB32(usb1->fs_csaddr));
125 printk(" cssize: %u\n", SWAB32(usb1->fs_cssize));
126 printk(" cgsize: %u\n", SWAB32(usb1->fs_cgsize));
127 printk(" fstodb: %u\n", SWAB32(usb1->fs_fsbtodb));
128 printk(" contigsumsize: %d\n", SWAB32(usb3->fs_u2.fs_44.fs_contigsumsize));
129 printk(" postblformat: %u\n", SWAB32(usb3->fs_postblformat));
130 printk(" nrpos: %u\n", SWAB32(usb3->fs_nrpos));
131 printk(" ndir %u\n", SWAB32(usb1->fs_cstotal.cs_ndir));
132 printk(" nifree %u\n", SWAB32(usb1->fs_cstotal.cs_nifree));
133 printk(" nbfree %u\n", SWAB32(usb1->fs_cstotal.cs_nbfree));
134 printk(" nffree %u\n", SWAB32(usb1->fs_cstotal.cs_nffree));
135 printk("\n");
140 * Print contents of ufs_cylinder_group, useful for debugging
142 void ufs_print_cylinder_stuff(struct ufs_cylinder_group *cg, unsigned swab)
144 printk("\nufs_print_cylinder_stuff\n");
145 printk("size of ucg: %u\n", sizeof(struct ufs_cylinder_group));
146 printk(" magic: %x\n", SWAB32(cg->cg_magic));
147 printk(" time: %u\n", SWAB32(cg->cg_time));
148 printk(" cgx: %u\n", SWAB32(cg->cg_cgx));
149 printk(" ncyl: %u\n", SWAB16(cg->cg_ncyl));
150 printk(" niblk: %u\n", SWAB16(cg->cg_niblk));
151 printk(" ndblk: %u\n", SWAB32(cg->cg_ndblk));
152 printk(" cs_ndir: %u\n", SWAB32(cg->cg_cs.cs_ndir));
153 printk(" cs_nbfree: %u\n", SWAB32(cg->cg_cs.cs_nbfree));
154 printk(" cs_nifree: %u\n", SWAB32(cg->cg_cs.cs_nifree));
155 printk(" cs_nffree: %u\n", SWAB32(cg->cg_cs.cs_nffree));
156 printk(" rotor: %u\n", SWAB32(cg->cg_rotor));
157 printk(" frotor: %u\n", SWAB32(cg->cg_frotor));
158 printk(" irotor: %u\n", SWAB32(cg->cg_irotor));
159 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
160 SWAB32(cg->cg_frsum[0]), SWAB32(cg->cg_frsum[1]),
161 SWAB32(cg->cg_frsum[2]), SWAB32(cg->cg_frsum[3]),
162 SWAB32(cg->cg_frsum[4]), SWAB32(cg->cg_frsum[5]),
163 SWAB32(cg->cg_frsum[6]), SWAB32(cg->cg_frsum[7]));
164 printk(" btotoff: %u\n", SWAB32(cg->cg_btotoff));
165 printk(" boff: %u\n", SWAB32(cg->cg_boff));
166 printk(" iuseoff: %u\n", SWAB32(cg->cg_iusedoff));
167 printk(" freeoff: %u\n", SWAB32(cg->cg_freeoff));
168 printk(" nextfreeoff: %u\n", SWAB32(cg->cg_nextfreeoff));
169 printk(" clustersumoff %u\n", SWAB32(cg->cg_u.cg_44.cg_clustersumoff));
170 printk(" clusteroff %u\n", SWAB32(cg->cg_u.cg_44.cg_clusteroff));
171 printk(" nclusterblks %u\n", SWAB32(cg->cg_u.cg_44.cg_nclusterblks));
172 printk("\n");
174 #endif /* UFS_SUPER_DEBUG_MORE */
176 static char error_buf[1024];
178 void ufs_error (struct super_block * sb, const char * function,
179 const char * fmt, ...)
181 struct ufs_sb_private_info * uspi;
182 struct ufs_super_block_first * usb1;
183 va_list args;
185 uspi = sb->u.ufs_sb.s_uspi;
186 usb1 = ubh_get_usb_first(USPI_UBH);
188 if (!(sb->s_flags & MS_RDONLY)) {
189 usb1->fs_clean = UFS_FSBAD;
190 ubh_mark_buffer_dirty(USPI_UBH, 1);
191 sb->s_dirt = 1;
192 sb->s_flags |= MS_RDONLY;
194 va_start (args, fmt);
195 vsprintf (error_buf, fmt, args);
196 va_end (args);
197 switch (sb->u.ufs_sb.s_mount_opt & UFS_MOUNT_ONERROR) {
198 case UFS_MOUNT_ONERROR_PANIC:
199 panic ("UFS-fs panic (device %s): %s: %s\n",
200 kdevname(sb->s_dev), function, error_buf);
202 case UFS_MOUNT_ONERROR_LOCK:
203 case UFS_MOUNT_ONERROR_UMOUNT:
204 case UFS_MOUNT_ONERROR_REPAIR:
205 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
206 kdevname(sb->s_dev), function, error_buf);
210 void ufs_panic (struct super_block * sb, const char * function,
211 const char * fmt, ...)
213 struct ufs_sb_private_info * uspi;
214 struct ufs_super_block_first * usb1;
215 va_list args;
217 uspi = sb->u.ufs_sb.s_uspi;
218 usb1 = ubh_get_usb_first(USPI_UBH);
220 if (!(sb->s_flags & MS_RDONLY)) {
221 usb1->fs_clean = UFS_FSBAD;
222 ubh_mark_buffer_dirty(USPI_UBH, 1);
223 sb->s_dirt = 1;
225 va_start (args, fmt);
226 vsprintf (error_buf, fmt, args);
227 va_end (args);
228 /* this is to prevent panic from syncing this filesystem */
229 if (sb->s_lock)
230 sb->s_lock = 0;
231 sb->s_flags |= MS_RDONLY;
232 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
233 kdevname(sb->s_dev), function, error_buf);
236 void ufs_warning (struct super_block * sb, const char * function,
237 const char * fmt, ...)
239 va_list args;
241 va_start (args, fmt);
242 vsprintf (error_buf, fmt, args);
243 va_end (args);
244 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
245 kdevname(sb->s_dev), function, error_buf);
248 static int ufs_parse_options (char * options, unsigned * mount_options)
250 char * this_char;
251 char * value;
253 UFSD(("ENTER\n"))
255 if (!options)
256 return 1;
258 for (this_char = strtok (options, ",");
259 this_char != NULL;
260 this_char = strtok (NULL, ",")) {
262 if ((value = strchr (this_char, '=')) != NULL)
263 *value++ = 0;
264 if (!strcmp (this_char, "ufstype")) {
265 ufs_clear_opt (*mount_options, UFSTYPE);
266 if (!strcmp (value, "old"))
267 ufs_set_opt (*mount_options, UFSTYPE_OLD);
268 else if (!strcmp (value, "sun"))
269 ufs_set_opt (*mount_options, UFSTYPE_SUN);
270 else if (!strcmp (value, "44bsd"))
271 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
272 else if (!strcmp (value, "nextstep"))
273 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
274 else if (!strcmp (value, "openstep"))
275 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
276 else if (!strcmp (value, "sunx86"))
277 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
278 else {
279 printk ("UFS-fs: Invalid type option: %s\n", value);
280 return 0;
283 else if (!strcmp (this_char, "onerror")) {
284 ufs_clear_opt (*mount_options, ONERROR);
285 if (!strcmp (value, "panic"))
286 ufs_set_opt (*mount_options, ONERROR_PANIC);
287 else if (!strcmp (value, "lock"))
288 ufs_set_opt (*mount_options, ONERROR_LOCK);
289 else if (!strcmp (value, "umount"))
290 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
291 else if (!strcmp (value, "repair")) {
292 printk("UFS-fs: Unable to do repair on error, "
293 "will lock lock instead \n");
294 ufs_set_opt (*mount_options, ONERROR_REPAIR);
296 else {
297 printk ("UFS-fs: Invalid action onerror: %s\n", value);
298 return 0;
301 else {
302 printk("UFS-fs: Invalid option: %s\n", this_char);
303 return 0;
306 return 1;
310 * Read on-disk structures associated with cylinder groups
312 int ufs_read_cylinder_structures (struct super_block * sb) {
313 struct ufs_sb_private_info * uspi;
314 struct ufs_buffer_head * ubh;
315 unsigned char * base, * space;
316 unsigned size, blks, i;
317 unsigned swab;
319 UFSD(("ENTER\n"))
321 uspi = sb->u.ufs_sb.s_uspi;
322 swab = sb->u.ufs_sb.s_swab;
325 * Read cs structures from (usually) first data block
326 * on the device.
328 size = uspi->s_cssize;
329 blks = howmany(size, uspi->s_fsize);
330 base = space = kmalloc(size, GFP_KERNEL);
331 if (!base)
332 goto failed;
333 for (i = 0; i < blks; i += uspi->s_fpb) {
334 size = uspi->s_bsize;
335 if (i + uspi->s_fpb > blks)
336 size = (blks - i) * uspi->s_fsize;
337 ubh = ubh_bread(sb->s_dev, uspi->s_csaddr + i, size);
338 if (!ubh)
339 goto failed;
340 ubh_ubhcpymem (space, ubh, size);
341 sb->u.ufs_sb.s_csp[ufs_fragstoblks(i)] = (struct ufs_csum *)space;
342 space += size;
343 ubh_brelse (ubh);
344 ubh = NULL;
348 * Read cylinder group (we read only first fragment from block
349 * at this time) and prepare internal data structures for cg caching.
351 if (!(sb->u.ufs_sb.s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
352 goto failed;
353 for (i = 0; i < uspi->s_ncg; i++)
354 sb->u.ufs_sb.s_ucg[i] = NULL;
355 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
356 sb->u.ufs_sb.s_ucpi[i] = NULL;
357 sb->u.ufs_sb.s_cgno[i] = UFS_CGNO_EMPTY;
359 for (i = 0; i < uspi->s_ncg; i++) {
360 UFSD(("read cg %u\n", i))
361 if (!(sb->u.ufs_sb.s_ucg[i] = bread (sb->s_dev, ufs_cgcmin(i), sb->s_blocksize)))
362 goto failed;
363 if (!ufs_cg_chkmagic ((struct ufs_cylinder_group *) sb->u.ufs_sb.s_ucg[i]->b_data))
364 goto failed;
365 #ifdef UFS_SUPER_DEBUG_MORE
366 ufs_print_cylinder_stuff((struct ufs_cylinder_group *) sb->u.ufs_sb.s_ucg[i]->b_data, swab);
367 #endif
369 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
370 if (!(sb->u.ufs_sb.s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
371 goto failed;
372 sb->u.ufs_sb.s_cgno[i] = UFS_CGNO_EMPTY;
374 sb->u.ufs_sb.s_cg_loaded = 0;
375 UFSD(("EXIT\n"))
376 return 1;
378 failed:
379 if (base) kfree (base);
380 if (sb->u.ufs_sb.s_ucg) {
381 for (i = 0; i < uspi->s_ncg; i++)
382 if (sb->u.ufs_sb.s_ucg[i]) brelse (sb->u.ufs_sb.s_ucg[i]);
383 kfree (sb->u.ufs_sb.s_ucg);
384 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
385 if (sb->u.ufs_sb.s_ucpi[i]) kfree (sb->u.ufs_sb.s_ucpi[i]);
387 UFSD(("EXIT (FAILED)\n"))
388 return 0;
392 * Put on-disk structures associated with cylinder groups and
393 * write them back to disk
395 void ufs_put_cylinder_structures (struct super_block * sb) {
396 struct ufs_sb_private_info * uspi;
397 struct ufs_buffer_head * ubh;
398 unsigned char * base, * space;
399 unsigned blks, size, i;
401 UFSD(("ENTER\n"))
403 uspi = sb->u.ufs_sb.s_uspi;
405 size = uspi->s_cssize;
406 blks = howmany(size, uspi->s_fsize);
407 base = space = (char*) sb->u.ufs_sb.s_csp[0];
408 for (i = 0; i < blks; i += uspi->s_fpb) {
409 size = uspi->s_bsize;
410 if (i + uspi->s_fpb > blks)
411 size = (blks - i) * uspi->s_fsize;
412 ubh = ubh_bread (sb->s_dev, uspi->s_csaddr + i, size);
413 ubh_memcpyubh (ubh, space, size);
414 space += size;
415 ubh_mark_buffer_uptodate (ubh, 1);
416 ubh_mark_buffer_dirty (ubh, 0);
417 ubh_brelse (ubh);
419 for (i = 0; i < sb->u.ufs_sb.s_cg_loaded; i++) {
420 ufs_put_cylinder (sb, i);
421 kfree (sb->u.ufs_sb.s_ucpi[i]);
423 for (; i < UFS_MAX_GROUP_LOADED; i++)
424 kfree (sb->u.ufs_sb.s_ucpi[i]);
425 for (i = 0; i < uspi->s_ncg; i++)
426 brelse (sb->u.ufs_sb.s_ucg[i]);
427 kfree (sb->u.ufs_sb.s_ucg);
428 kfree (base);
429 UFSD(("EXIT\n"))
432 struct super_block * ufs_read_super (struct super_block * sb, void * data,
433 int silent)
435 struct ufs_sb_private_info * uspi;
436 struct ufs_super_block_first * usb1;
437 struct ufs_super_block_second * usb2;
438 struct ufs_super_block_third * usb3;
439 struct ufs_buffer_head * ubh;
440 unsigned block_size, super_block_size;
441 unsigned flags, swab;
443 uspi = NULL;
444 ubh = NULL;
445 flags = 0;
446 swab = 0;
448 UFSD(("ENTER\n"))
450 MOD_INC_USE_COUNT;
451 lock_super (sb);
453 #ifndef CONFIG_UFS_FS_WRITE
454 sb->s_flags |= MS_RDONLY;
455 #endif
457 * Set default mount options
458 * Parse mount options
460 sb->u.ufs_sb.s_mount_opt = 0;
461 ufs_set_opt (sb->u.ufs_sb.s_mount_opt, ONERROR_LOCK);
462 if (!ufs_parse_options ((char *) data, &sb->u.ufs_sb.s_mount_opt)) {
463 printk("wrong mount options\n");
464 goto failed;
466 if (!(sb->u.ufs_sb.s_mount_opt & UFS_MOUNT_UFSTYPE)) {
467 printk("You didn't specify the type of your ufs filesystem\n\n"
468 " mount -t ufs -o ufstype=sun|sunx86|44bsd|old|nextstep|openstep ....\n\n"
469 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
470 "default is ufstype=old\n");
471 ufs_set_opt (sb->u.ufs_sb.s_mount_opt, UFSTYPE_OLD);
474 sb->u.ufs_sb.s_uspi = uspi =
475 kmalloc (sizeof(struct ufs_sb_private_info), GFP_KERNEL);
476 if (!uspi)
477 goto failed;
479 switch (sb->u.ufs_sb.s_mount_opt & UFS_MOUNT_UFSTYPE) {
480 case UFS_MOUNT_UFSTYPE_44BSD:
481 UFSD(("ufstype=44bsd\n"))
482 uspi->s_fsize = block_size = 512;
483 uspi->s_fmask = ~(512 - 1);
484 uspi->s_fshift = 9;
485 uspi->s_sbsize = super_block_size = 1536;
486 uspi->s_sbbase = 0;
487 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
488 break;
490 case UFS_MOUNT_UFSTYPE_SUN:
491 UFSD(("ufstype=sun\n"))
492 uspi->s_fsize = block_size = 1024;
493 uspi->s_fmask = ~(1024 - 1);
494 uspi->s_fshift = 10;
495 uspi->s_sbsize = super_block_size = 2048;
496 uspi->s_sbbase = 0;
497 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
498 break;
500 case UFS_MOUNT_UFSTYPE_SUNx86:
501 UFSD(("ufstype=sunx86\n"))
502 uspi->s_fsize = block_size = 1024;
503 uspi->s_fmask = ~(1024 - 1);
504 uspi->s_fshift = 10;
505 uspi->s_sbsize = super_block_size = 2048;
506 uspi->s_sbbase = 0;
507 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
508 break;
510 case UFS_MOUNT_UFSTYPE_OLD:
511 UFSD(("ufstype=old\n"))
512 uspi->s_fsize = block_size = 1024;
513 uspi->s_fmask = ~(1024 - 1);
514 uspi->s_fshift = 10;
515 uspi->s_sbsize = super_block_size = 2048;
516 uspi->s_sbbase = 0;
517 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
518 if (!(sb->s_flags & MS_RDONLY)) {
519 printk(KERN_INFO "ufstype=old is supported read-only\n");
520 sb->s_flags |= MS_RDONLY;
522 break;
524 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
525 UFSD(("ufstype=nextstep\n"))
526 uspi->s_fsize = block_size = 1024;
527 uspi->s_fmask = ~(1024 - 1);
528 uspi->s_fshift = 10;
529 uspi->s_sbsize = super_block_size = 2048;
530 uspi->s_sbbase = 0;
531 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
532 if (!(sb->s_flags & MS_RDONLY)) {
533 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
534 sb->s_flags |= MS_RDONLY;
536 break;
538 case UFS_MOUNT_UFSTYPE_OPENSTEP:
539 UFSD(("ufstype=openstep\n"))
540 uspi->s_fsize = block_size = 1024;
541 uspi->s_fmask = ~(1024 - 1);
542 uspi->s_fshift = 10;
543 uspi->s_sbsize = super_block_size = 2048;
544 uspi->s_sbbase = 0;
545 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
546 if (!(sb->s_flags & MS_RDONLY)) {
547 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
548 sb->s_flags |= MS_RDONLY;
550 break;
552 default:
553 printk("unknown ufstype\n");
554 goto failed;
557 again:
558 set_blocksize (sb->s_dev, block_size);
561 * read ufs super block from device
563 ubh = ubh_bread_uspi (uspi, sb->s_dev, uspi->s_sbbase + UFS_SBLOCK/block_size, super_block_size);
564 if (!ubh)
565 goto failed;
567 usb1 = ubh_get_usb_first(USPI_UBH);
568 usb2 = ubh_get_usb_second(USPI_UBH);
569 usb3 = ubh_get_usb_third(USPI_UBH);
572 * Check ufs magic number
574 #if defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN) /* sane bytesex */
575 switch (usb3->fs_magic) {
576 case UFS_MAGIC:
577 swab = UFS_NATIVE_ENDIAN;
578 goto magic_found;
579 case UFS_CIGAM:
580 swab = UFS_SWABBED_ENDIAN;
581 goto magic_found;
583 #else /* bytesex perversion */
584 switch (le32_to_cpup(&usb3->fs_magic)) {
585 case UFS_MAGIC:
586 swab = UFS_LITTLE_ENDIAN;
587 goto magic_found;
588 case UFS_CIGAM:
589 swab = UFS_BIG_ENDIAN;
590 goto magic_found;
592 #endif
594 if ((((sb->u.ufs_sb.s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
595 || ((sb->u.ufs_sb.s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
596 && uspi->s_sbbase < 256) {
597 ubh_brelse_uspi(uspi);
598 ubh = NULL;
599 uspi->s_sbbase += 8;
600 goto again;
602 printk("ufs_read_super: bad magic number\n");
603 goto failed;
605 magic_found:
607 * Check block and fragment sizes
609 uspi->s_bsize = SWAB32(usb1->fs_bsize);
610 uspi->s_fsize = SWAB32(usb1->fs_fsize);
611 uspi->s_sbsize = SWAB32(usb1->fs_sbsize);
612 uspi->s_fmask = SWAB32(usb1->fs_fmask);
613 uspi->s_fshift = SWAB32(usb1->fs_fshift);
615 if (uspi->s_bsize != 4096 && uspi->s_bsize != 8192) {
616 printk("ufs_read_super: fs_bsize %u != {4096, 8192}\n", uspi->s_bsize);
617 goto failed;
619 if (uspi->s_fsize != 512 && uspi->s_fsize != 1024) {
620 printk("ufs_read_super: fs_fsize %u != {512, 1024}\n", uspi->s_fsize);
621 goto failed;
623 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
624 ubh_brelse_uspi(uspi);
625 ubh = NULL;
626 block_size = uspi->s_fsize;
627 super_block_size = uspi->s_sbsize;
628 UFSD(("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size))
629 goto again;
632 #ifdef UFS_SUPER_DEBUG_MORE
633 ufs_print_super_stuff (usb1, usb2, usb3, swab);
634 #endif
637 * Check, if file system was correctly unmounted.
638 * If not, make it read only.
640 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
641 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
642 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
643 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
644 (ufs_get_fs_state(usb1, usb3) == (UFS_FSOK - SWAB32(usb1->fs_time))))) {
645 switch(usb1->fs_clean) {
646 case UFS_FSCLEAN:
647 UFSD(("fs is clean\n"))
648 break;
649 case UFS_FSSTABLE:
650 UFSD(("fs is stable\n"))
651 break;
652 case UFS_FSOSF1:
653 UFSD(("fs is DEC OSF/1\n"))
654 break;
655 case UFS_FSACTIVE:
656 printk("ufs_read_super: fs is active\n");
657 sb->s_flags |= MS_RDONLY;
658 break;
659 case UFS_FSBAD:
660 printk("ufs_read_super: fs is bad\n");
661 sb->s_flags |= MS_RDONLY;
662 break;
663 default:
664 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
665 sb->s_flags |= MS_RDONLY;
666 break;
669 else {
670 printk("ufs_read_super: fs needs fsck\n");
671 sb->s_flags |= MS_RDONLY;
675 * Read ufs_super_block into internal data structures
677 sb->s_blocksize = SWAB32(usb1->fs_fsize);
678 sb->s_blocksize_bits = SWAB32(usb1->fs_fshift);
679 sb->s_op = &ufs_super_ops;
680 sb->dq_op = NULL; /***/
681 sb->s_magic = SWAB32(usb3->fs_magic);
683 uspi->s_sblkno = SWAB32(usb1->fs_sblkno);
684 uspi->s_cblkno = SWAB32(usb1->fs_cblkno);
685 uspi->s_iblkno = SWAB32(usb1->fs_iblkno);
686 uspi->s_dblkno = SWAB32(usb1->fs_dblkno);
687 uspi->s_cgoffset = SWAB32(usb1->fs_cgoffset);
688 uspi->s_cgmask = SWAB32(usb1->fs_cgmask);
689 uspi->s_size = SWAB32(usb1->fs_size);
690 uspi->s_dsize = SWAB32(usb1->fs_dsize);
691 uspi->s_ncg = SWAB32(usb1->fs_ncg);
692 /* s_bsize already set */
693 /* s_fsize already set */
694 uspi->s_fpb = SWAB32(usb1->fs_frag);
695 uspi->s_minfree = SWAB32(usb1->fs_minfree);
696 uspi->s_bmask = SWAB32(usb1->fs_bmask);
697 uspi->s_fmask = SWAB32(usb1->fs_fmask);
698 uspi->s_bshift = SWAB32(usb1->fs_bshift);
699 uspi->s_fshift = SWAB32(usb1->fs_fshift);
700 uspi->s_fpbshift = SWAB32(usb1->fs_fragshift);
701 uspi->s_fsbtodb = SWAB32(usb1->fs_fsbtodb);
702 /* s_sbsize already set */
703 uspi->s_csmask = SWAB32(usb1->fs_csmask);
704 uspi->s_csshift = SWAB32(usb1->fs_csshift);
705 uspi->s_nindir = SWAB32(usb1->fs_nindir);
706 uspi->s_inopb = SWAB32(usb1->fs_inopb);
707 uspi->s_nspf = SWAB32(usb1->fs_nspf);
708 uspi->s_npsect = ufs_get_fs_npsect(usb1, usb3);
709 uspi->s_interleave = SWAB32(usb1->fs_interleave);
710 uspi->s_trackskew = SWAB32(usb1->fs_trackskew);
711 uspi->s_csaddr = SWAB32(usb1->fs_csaddr);
712 uspi->s_cssize = SWAB32(usb1->fs_cssize);
713 uspi->s_cgsize = SWAB32(usb1->fs_cgsize);
714 uspi->s_ntrak = SWAB32(usb1->fs_ntrak);
715 uspi->s_nsect = SWAB32(usb1->fs_nsect);
716 uspi->s_spc = SWAB32(usb1->fs_spc);
717 uspi->s_ipg = SWAB32(usb1->fs_ipg);
718 uspi->s_fpg = SWAB32(usb1->fs_fpg);
719 uspi->s_cpc = SWAB32(usb2->fs_cpc);
720 uspi->s_contigsumsize = SWAB32(usb3->fs_u2.fs_44.fs_contigsumsize);
721 uspi->s_qbmask = ufs_get_fs_qbmask(usb3);
722 uspi->s_qfmask = ufs_get_fs_qfmask(usb3);
723 uspi->s_postblformat = SWAB32(usb3->fs_postblformat);
724 uspi->s_nrpos = SWAB32(usb3->fs_nrpos);
725 uspi->s_postbloff = SWAB32(usb3->fs_postbloff);
726 uspi->s_rotbloff = SWAB32(usb3->fs_rotbloff);
729 * Compute another frequently used values
731 uspi->s_fpbmask = uspi->s_fpb - 1;
732 uspi->s_apbshift = uspi->s_bshift - 2;
733 uspi->s_2apbshift = uspi->s_apbshift * 2;
734 uspi->s_3apbshift = uspi->s_apbshift * 3;
735 uspi->s_apb = 1 << uspi->s_apbshift;
736 uspi->s_2apb = 1 << uspi->s_2apbshift;
737 uspi->s_3apb = 1 << uspi->s_3apbshift;
738 uspi->s_apbmask = uspi->s_apb - 1;
739 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
740 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
741 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
742 uspi->s_bpf = uspi->s_fsize << 3;
743 uspi->s_bpfshift = uspi->s_fshift + 3;
744 uspi->s_bpfmask = uspi->s_bpf - 1;
746 sb->u.ufs_sb.s_flags = flags;
747 sb->u.ufs_sb.s_swab = swab;
749 sb->s_root = d_alloc_root(iget(sb, UFS_ROOTINO), NULL);
753 * Read cylinder group structures
755 if (!(sb->s_flags & MS_RDONLY))
756 if (!ufs_read_cylinder_structures(sb))
757 goto failed;
759 unlock_super(sb);
760 UFSD(("EXIT\n"))
761 return(sb);
763 failed:
764 if (ubh) ubh_brelse_uspi (uspi);
765 if (uspi) kfree (uspi);
766 sb->s_dev = 0;
767 unlock_super (sb);
768 MOD_DEC_USE_COUNT;
769 UFSD(("EXIT (FAILED)\n"))
770 return(NULL);
773 void ufs_write_super (struct super_block * sb) {
774 struct ufs_sb_private_info * uspi;
775 struct ufs_super_block_first * usb1;
776 struct ufs_super_block_third * usb3;
777 unsigned flags, swab;
779 UFSD(("ENTER\n"))
780 swab = sb->u.ufs_sb.s_swab;
781 flags = sb->u.ufs_sb.s_flags;
782 uspi = sb->u.ufs_sb.s_uspi;
783 usb1 = ubh_get_usb_first(USPI_UBH);
784 usb3 = ubh_get_usb_third(USPI_UBH);
786 if (!(sb->s_flags & MS_RDONLY)) {
787 usb1->fs_time = SWAB32(CURRENT_TIME);
788 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
789 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
790 ufs_set_fs_state(usb1, usb3, UFS_FSOK - SWAB32(usb1->fs_time));
791 ubh_mark_buffer_dirty (USPI_UBH, 1);
793 sb->s_dirt = 0;
794 UFSD(("EXIT\n"))
797 void ufs_put_super (struct super_block * sb)
799 struct ufs_sb_private_info * uspi;
800 unsigned swab;
802 UFSD(("ENTER\n"))
804 uspi = sb->u.ufs_sb.s_uspi;
805 swab = sb->u.ufs_sb.s_swab;
807 if (!(sb->s_flags & MS_RDONLY))
808 ufs_put_cylinder_structures (sb);
810 ubh_brelse_uspi (uspi);
811 kfree (sb->u.ufs_sb.s_uspi);
812 sb->s_dev = 0;
813 MOD_DEC_USE_COUNT;
814 return;
818 int ufs_remount (struct super_block * sb, int * mount_flags, char * data)
820 struct ufs_sb_private_info * uspi;
821 struct ufs_super_block_first * usb1;
822 struct ufs_super_block_third * usb3;
823 unsigned new_mount_opt, ufstype;
824 unsigned flags, swab;
826 uspi = sb->u.ufs_sb.s_uspi;
827 flags = sb->u.ufs_sb.s_flags;
828 swab = sb->u.ufs_sb.s_swab;
829 usb1 = ubh_get_usb_first(USPI_UBH);
830 usb3 = ubh_get_usb_third(USPI_UBH);
833 * Allow the "check" option to be passed as a remount option.
834 * It is not possible to change ufstype option during remount
836 ufstype = sb->u.ufs_sb.s_mount_opt & UFS_MOUNT_UFSTYPE;
837 new_mount_opt = 0;
838 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
839 if (!ufs_parse_options (data, &new_mount_opt))
840 return -EINVAL;
841 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
842 new_mount_opt |= ufstype;
844 else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
845 printk("ufstype can't be changed during remount\n");
846 return -EINVAL;
848 sb->u.ufs_sb.s_mount_opt = new_mount_opt;
850 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
851 return 0;
852 if (*mount_flags & MS_RDONLY) {
853 ufs_put_cylinder_structures(sb);
854 usb1->fs_time = SWAB32(CURRENT_TIME);
855 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
856 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
857 ufs_set_fs_state(usb1, usb3, UFS_FSOK - SWAB32(usb1->fs_time));
858 ubh_mark_buffer_dirty (USPI_UBH, 1);
859 sb->s_dirt = 0;
860 sb->s_flags |= MS_RDONLY;
862 else {
863 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
864 ufstype != UFS_MOUNT_UFSTYPE_44BSD) {
865 printk("this ufstype is read-only supported\n");
866 return 0;
868 if (!ufs_read_cylinder_structures (sb)) {
869 printk("failed during remounting\n");
870 return 0;
872 sb->s_flags &= ~MS_RDONLY;
874 return 0;
877 int ufs_statfs (struct super_block * sb, struct statfs * buf, int bufsiz)
879 struct ufs_sb_private_info * uspi;
880 struct ufs_super_block_first * usb1;
881 struct statfs tmp;
882 unsigned swab;
884 swab = sb->u.ufs_sb.s_swab;
885 uspi = sb->u.ufs_sb.s_uspi;
886 usb1 = ubh_get_usb_first (USPI_UBH);
888 tmp.f_type = UFS_MAGIC;
889 tmp.f_bsize = sb->s_blocksize;
890 tmp.f_blocks = uspi->s_dsize;
891 tmp.f_bfree = ufs_blkstofrags(SWAB32(usb1->fs_cstotal.cs_nbfree)) +
892 SWAB32(usb1->fs_cstotal.cs_nffree);
893 tmp.f_bavail = (tmp.f_bfree > ((tmp.f_blocks / 100) * uspi->s_minfree))
894 ? (tmp.f_bfree - ((tmp.f_blocks / 100) * uspi->s_minfree)) : 0;
895 tmp.f_files = uspi->s_ncg * uspi->s_ipg;
896 tmp.f_ffree = SWAB32(usb1->fs_cstotal.cs_nifree);
897 tmp.f_namelen = UFS_MAXNAMLEN;
898 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
901 static struct super_operations ufs_super_ops = {
902 ufs_read_inode,
903 ufs_write_inode,
904 ufs_put_inode,
905 ufs_delete_inode,
906 NULL, /* notify_change() */
907 ufs_put_super,
908 ufs_write_super,
909 ufs_statfs,
910 ufs_remount
913 static struct file_system_type ufs_fs_type = {
914 "ufs",
915 FS_REQUIRES_DEV,
916 ufs_read_super,
917 NULL
920 __initfunc(int init_ufs_fs(void))
922 return register_filesystem(&ufs_fs_type);
925 #ifdef MODULE
926 EXPORT_NO_SYMBOLS;
928 int init_module(void)
930 return init_ufs_fs();
933 void cleanup_module(void)
935 unregister_filesystem(&ufs_fs_type);
938 #endif