c8339e56638038525376291350abb7806bd599f6
[linux-2.6.git] / fs / ufs / super.c
1 /*
2  *  linux/fs/ufs/super.c
3  *
4  * Copyright (C) 1998
5  * Daniel Pirkl <daniel.pirkl@email.cz>
6  * Charles University, Faculty of Mathematics and Physics
7  */
8
9 /* Derived from
10  *
11  *  linux/fs/ext2/super.c
12  *
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)
17  *
18  *  from
19  *
20  *  linux/fs/minix/inode.c
21  *
22  *  Copyright (C) 1991, 1992  Linus Torvalds
23  *
24  *  Big-endian to little-endian byte-swapping/bitmaps by
25  *        David S. Miller (davem@caip.rutgers.edu), 1995
26  */
27  
28 /*
29  * Inspired by
30  *
31  *  linux/fs/ufs/super.c
32  *
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
37  *
38  * Copyright (C) 1996  Eddie C. Dost  (ecd@skynet.be)
39  *
40  * Kernel module support added on 96/04/26 by
41  * Stefan Reinauer <stepan@home.culture.mipt.ru>
42  *
43  * Module usage counts added on 96/04/29 by
44  * Gertjan van Wingerde <gertjan@cs.vu.nl>
45  *
46  * Clean swab support on 19970406 by
47  * Francois-Rene Rideau <fare@tunes.org>
48  *
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>.
52  *
53  * NeXTstep support added on February 5th 1998 by
54  * Niels Kristian Bech Jensen <nkbj@image.dk>.
55  *
56  * write support Daniel Pirkl <daniel.pirkl@email.cz> 1998
57  * 
58  * HP/UX hfs filesystem support added by
59  * Martin K. Petersen <mkp@mkp.net>, August 1999
60  *
61  * UFS2 (of FreeBSD 5.x) support added by
62  * Niraj Kumar <niraj17@iitbombay.org>, Jan 2004
63  *
64  */
65
66
67 #include <linux/config.h>
68 #include <linux/module.h>
69 #include <linux/bitops.h>
70
71 #include <stdarg.h>
72
73 #include <asm/uaccess.h>
74 #include <asm/system.h>
75
76 #include <linux/errno.h>
77 #include <linux/fs.h>
78 #include <linux/ufs_fs.h>
79 #include <linux/slab.h>
80 #include <linux/time.h>
81 #include <linux/stat.h>
82 #include <linux/string.h>
83 #include <linux/blkdev.h>
84 #include <linux/init.h>
85 #include <linux/parser.h>
86 #include <linux/smp_lock.h>
87 #include <linux/buffer_head.h>
88 #include <linux/vfs.h>
89
90 #include "swab.h"
91 #include "util.h"
92
93 #ifdef CONFIG_UFS_DEBUG
94 /*
95  * Print contents of ufs_super_block, useful for debugging
96  */
97 void ufs_print_super_stuff(struct super_block *sb,
98         struct ufs_super_block_first * usb1,
99         struct ufs_super_block_second * usb2, 
100         struct ufs_super_block_third * usb3)
101 {
102         printk("ufs_print_super_stuff\n");
103         printk("size of usb:     %zu\n", sizeof(struct ufs_super_block));
104         printk("  magic:         0x%x\n", fs32_to_cpu(sb, usb3->fs_magic));
105         printk("  sblkno:        %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
106         printk("  cblkno:        %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
107         printk("  iblkno:        %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
108         printk("  dblkno:        %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
109         printk("  cgoffset:      %u\n", fs32_to_cpu(sb, usb1->fs_cgoffset));
110         printk("  ~cgmask:       0x%x\n", ~fs32_to_cpu(sb, usb1->fs_cgmask));
111         printk("  size:          %u\n", fs32_to_cpu(sb, usb1->fs_size));
112         printk("  dsize:         %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
113         printk("  ncg:           %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
114         printk("  bsize:         %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
115         printk("  fsize:         %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
116         printk("  frag:          %u\n", fs32_to_cpu(sb, usb1->fs_frag));
117         printk("  fragshift:     %u\n", fs32_to_cpu(sb, usb1->fs_fragshift));
118         printk("  ~fmask:        %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
119         printk("  fshift:        %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
120         printk("  sbsize:        %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
121         printk("  spc:           %u\n", fs32_to_cpu(sb, usb1->fs_spc));
122         printk("  cpg:           %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
123         printk("  ipg:           %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
124         printk("  fpg:           %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
125         printk("  csaddr:        %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
126         printk("  cssize:        %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
127         printk("  cgsize:        %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
128         printk("  fstodb:        %u\n", fs32_to_cpu(sb, usb1->fs_fsbtodb));
129         printk("  contigsumsize: %d\n", fs32_to_cpu(sb, usb3->fs_u2.fs_44.fs_contigsumsize));
130         printk("  postblformat:  %u\n", fs32_to_cpu(sb, usb3->fs_postblformat));
131         printk("  nrpos:         %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
132         printk("  ndir           %u\n", fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
133         printk("  nifree         %u\n", fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
134         printk("  nbfree         %u\n", fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
135         printk("  nffree         %u\n", fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
136         printk("\n");
137 }
138
139 /*
140  * Print contents of ufs2 ufs_super_block, useful for debugging
141  */
142 void ufs2_print_super_stuff(
143      struct super_block *sb,
144       struct ufs_super_block *usb)
145 {
146         printk("ufs_print_super_stuff\n");
147         printk("size of usb:     %zu\n", sizeof(struct ufs_super_block));
148         printk("  magic:         0x%x\n", fs32_to_cpu(sb, usb->fs_magic));
149         printk("  fs_size:   %llu\n",
150                (unsigned long long)fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_size));
151         printk("  fs_dsize:  %llu\n",
152                (unsigned long long)fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_dsize));
153         printk("  bsize:         %u\n", fs32_to_cpu(sb, usb->fs_bsize));
154         printk("  fsize:         %u\n", fs32_to_cpu(sb, usb->fs_fsize));
155         printk("  fs_volname:  %s\n", usb->fs_u11.fs_u2.fs_volname);
156         printk("  fs_fsmnt:  %s\n", usb->fs_u11.fs_u2.fs_fsmnt);
157         printk("  fs_sblockloc: %llu\n",
158                (unsigned long long)fs64_to_cpu(sb,
159                                                usb->fs_u11.fs_u2.fs_sblockloc));
160         printk("  cs_ndir(No of dirs):  %llu\n",
161                (unsigned long long)fs64_to_cpu(sb,
162                                          usb->fs_u11.fs_u2.fs_cstotal.cs_ndir));
163         printk("  cs_nbfree(No of free blocks):  %llu\n",
164                (unsigned long long)fs64_to_cpu(sb,
165                                        usb->fs_u11.fs_u2.fs_cstotal.cs_nbfree));
166         printk("\n");
167 }
168
169 /*
170  * Print contents of ufs_cylinder_group, useful for debugging
171  */
172 void ufs_print_cylinder_stuff(struct super_block *sb, struct ufs_cylinder_group *cg)
173 {
174         printk("\nufs_print_cylinder_stuff\n");
175         printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
176         printk("  magic:        %x\n", fs32_to_cpu(sb, cg->cg_magic));
177         printk("  time:         %u\n", fs32_to_cpu(sb, cg->cg_time));
178         printk("  cgx:          %u\n", fs32_to_cpu(sb, cg->cg_cgx));
179         printk("  ncyl:         %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
180         printk("  niblk:        %u\n", fs16_to_cpu(sb, cg->cg_niblk));
181         printk("  ndblk:        %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
182         printk("  cs_ndir:      %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
183         printk("  cs_nbfree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
184         printk("  cs_nifree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
185         printk("  cs_nffree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
186         printk("  rotor:        %u\n", fs32_to_cpu(sb, cg->cg_rotor));
187         printk("  frotor:       %u\n", fs32_to_cpu(sb, cg->cg_frotor));
188         printk("  irotor:       %u\n", fs32_to_cpu(sb, cg->cg_irotor));
189         printk("  frsum:        %u, %u, %u, %u, %u, %u, %u, %u\n",
190             fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
191             fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
192             fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
193             fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
194         printk("  btotoff:      %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
195         printk("  boff:         %u\n", fs32_to_cpu(sb, cg->cg_boff));
196         printk("  iuseoff:      %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
197         printk("  freeoff:      %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
198         printk("  nextfreeoff:  %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
199         printk("  clustersumoff %u\n", fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
200         printk("  clusteroff    %u\n", fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
201         printk("  nclusterblks  %u\n", fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
202         printk("\n");
203 }
204 #endif /* CONFIG_UFS_DEBUG */
205
206 static struct super_operations ufs_super_ops;
207
208 static char error_buf[1024];
209
210 void ufs_error (struct super_block * sb, const char * function,
211         const char * fmt, ...)
212 {
213         struct ufs_sb_private_info * uspi;
214         struct ufs_super_block_first * usb1;
215         va_list args;
216
217         uspi = UFS_SB(sb)->s_uspi;
218         usb1 = ubh_get_usb_first(uspi);
219         
220         if (!(sb->s_flags & MS_RDONLY)) {
221                 usb1->fs_clean = UFS_FSBAD;
222                 ubh_mark_buffer_dirty(USPI_UBH(uspi));
223                 sb->s_dirt = 1;
224                 sb->s_flags |= MS_RDONLY;
225         }
226         va_start (args, fmt);
227         vsprintf (error_buf, fmt, args);
228         va_end (args);
229         switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
230         case UFS_MOUNT_ONERROR_PANIC:
231                 panic ("UFS-fs panic (device %s): %s: %s\n", 
232                         sb->s_id, function, error_buf);
233
234         case UFS_MOUNT_ONERROR_LOCK:
235         case UFS_MOUNT_ONERROR_UMOUNT:
236         case UFS_MOUNT_ONERROR_REPAIR:
237                 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
238                         sb->s_id, function, error_buf);
239         }               
240 }
241
242 void ufs_panic (struct super_block * sb, const char * function,
243         const char * fmt, ...)
244 {
245         struct ufs_sb_private_info * uspi;
246         struct ufs_super_block_first * usb1;
247         va_list args;
248         
249         uspi = UFS_SB(sb)->s_uspi;
250         usb1 = ubh_get_usb_first(uspi);
251         
252         if (!(sb->s_flags & MS_RDONLY)) {
253                 usb1->fs_clean = UFS_FSBAD;
254                 ubh_mark_buffer_dirty(USPI_UBH(uspi));
255                 sb->s_dirt = 1;
256         }
257         va_start (args, fmt);
258         vsprintf (error_buf, fmt, args);
259         va_end (args);
260         sb->s_flags |= MS_RDONLY;
261         printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
262                 sb->s_id, function, error_buf);
263 }
264
265 void ufs_warning (struct super_block * sb, const char * function,
266         const char * fmt, ...)
267 {
268         va_list args;
269
270         va_start (args, fmt);
271         vsprintf (error_buf, fmt, args);
272         va_end (args);
273         printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
274                 sb->s_id, function, error_buf);
275 }
276
277 enum {
278         Opt_type_old, Opt_type_sunx86, Opt_type_sun, Opt_type_44bsd,
279         Opt_type_ufs2, Opt_type_hp, Opt_type_nextstepcd, Opt_type_nextstep,
280         Opt_type_openstep, Opt_onerror_panic, Opt_onerror_lock,
281         Opt_onerror_umount, Opt_onerror_repair, Opt_err
282 };
283
284 static match_table_t tokens = {
285         {Opt_type_old, "ufstype=old"},
286         {Opt_type_sunx86, "ufstype=sunx86"},
287         {Opt_type_sun, "ufstype=sun"},
288         {Opt_type_44bsd, "ufstype=44bsd"},
289         {Opt_type_ufs2, "ufstype=ufs2"},
290         {Opt_type_ufs2, "ufstype=5xbsd"},
291         {Opt_type_hp, "ufstype=hp"},
292         {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
293         {Opt_type_nextstep, "ufstype=nextstep"},
294         {Opt_type_openstep, "ufstype=openstep"},
295         {Opt_onerror_panic, "onerror=panic"},
296         {Opt_onerror_lock, "onerror=lock"},
297         {Opt_onerror_umount, "onerror=umount"},
298         {Opt_onerror_repair, "onerror=repair"},
299         {Opt_err, NULL}
300 };
301
302 static int ufs_parse_options (char * options, unsigned * mount_options)
303 {
304         char * p;
305         
306         UFSD("ENTER\n");
307         
308         if (!options)
309                 return 1;
310
311         while ((p = strsep(&options, ",")) != NULL) {
312                 substring_t args[MAX_OPT_ARGS];
313                 int token;
314                 if (!*p)
315                         continue;
316
317                 token = match_token(p, tokens, args);
318                 switch (token) {
319                 case Opt_type_old:
320                         ufs_clear_opt (*mount_options, UFSTYPE);
321                         ufs_set_opt (*mount_options, UFSTYPE_OLD);
322                         break;
323                 case Opt_type_sunx86:
324                         ufs_clear_opt (*mount_options, UFSTYPE);
325                         ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
326                         break;
327                 case Opt_type_sun:
328                         ufs_clear_opt (*mount_options, UFSTYPE);
329                         ufs_set_opt (*mount_options, UFSTYPE_SUN);
330                         break;
331                 case Opt_type_44bsd:
332                         ufs_clear_opt (*mount_options, UFSTYPE);
333                         ufs_set_opt (*mount_options, UFSTYPE_44BSD);
334                         break;
335                 case Opt_type_ufs2:
336                         ufs_clear_opt(*mount_options, UFSTYPE);
337                         ufs_set_opt(*mount_options, UFSTYPE_UFS2);
338                         break;
339                 case Opt_type_hp:
340                         ufs_clear_opt (*mount_options, UFSTYPE);
341                         ufs_set_opt (*mount_options, UFSTYPE_HP);
342                         break;
343                 case Opt_type_nextstepcd:
344                         ufs_clear_opt (*mount_options, UFSTYPE);
345                         ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
346                         break;
347                 case Opt_type_nextstep:
348                         ufs_clear_opt (*mount_options, UFSTYPE);
349                         ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
350                         break;
351                 case Opt_type_openstep:
352                         ufs_clear_opt (*mount_options, UFSTYPE);
353                         ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
354                         break;
355                 case Opt_onerror_panic:
356                         ufs_clear_opt (*mount_options, ONERROR);
357                         ufs_set_opt (*mount_options, ONERROR_PANIC);
358                         break;
359                 case Opt_onerror_lock:
360                         ufs_clear_opt (*mount_options, ONERROR);
361                         ufs_set_opt (*mount_options, ONERROR_LOCK);
362                         break;
363                 case Opt_onerror_umount:
364                         ufs_clear_opt (*mount_options, ONERROR);
365                         ufs_set_opt (*mount_options, ONERROR_UMOUNT);
366                         break;
367                 case Opt_onerror_repair:
368                         printk("UFS-fs: Unable to do repair on error, "
369                                 "will lock lock instead\n");
370                         ufs_clear_opt (*mount_options, ONERROR);
371                         ufs_set_opt (*mount_options, ONERROR_REPAIR);
372                         break;
373                 default:
374                         printk("UFS-fs: Invalid option: \"%s\" "
375                                         "or missing value\n", p);
376                         return 0;
377                 }
378         }
379         return 1;
380 }
381
382 /*
383  * Read on-disk structures associated with cylinder groups
384  */
385 static int ufs_read_cylinder_structures (struct super_block *sb)
386 {
387         struct ufs_sb_info * sbi = UFS_SB(sb);
388         struct ufs_sb_private_info * uspi;
389         struct ufs_super_block *usb;
390         struct ufs_buffer_head * ubh;
391         unsigned char * base, * space;
392         unsigned size, blks, i;
393         unsigned flags = 0;
394         
395         UFSD("ENTER\n");
396         
397         uspi = sbi->s_uspi;
398
399         usb  = (struct ufs_super_block *)
400                 ((struct ufs_buffer_head *)uspi)->bh[0]->b_data;
401
402         flags = UFS_SB(sb)->s_flags;
403         
404         /*
405          * Read cs structures from (usually) first data block
406          * on the device. 
407          */
408         size = uspi->s_cssize;
409         blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
410         base = space = kmalloc(size, GFP_KERNEL);
411         if (!base)
412                 goto failed; 
413         sbi->s_csp = (struct ufs_csum *)space;
414         for (i = 0; i < blks; i += uspi->s_fpb) {
415                 size = uspi->s_bsize;
416                 if (i + uspi->s_fpb > blks)
417                         size = (blks - i) * uspi->s_fsize;
418
419                 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) 
420                         ubh = ubh_bread(sb,
421                                 fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_csaddr) + i, size);
422                 else 
423                         ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
424                 
425                 if (!ubh)
426                         goto failed;
427
428                 ubh_ubhcpymem (space, ubh, size);
429
430                 space += size;
431                 ubh_brelse (ubh);
432                 ubh = NULL;
433         }
434
435         /*
436          * Read cylinder group (we read only first fragment from block
437          * at this time) and prepare internal data structures for cg caching.
438          */
439         if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
440                 goto failed;
441         for (i = 0; i < uspi->s_ncg; i++) 
442                 sbi->s_ucg[i] = NULL;
443         for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
444                 sbi->s_ucpi[i] = NULL;
445                 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
446         }
447         for (i = 0; i < uspi->s_ncg; i++) {
448                 UFSD("read cg %u\n", i);
449                 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
450                         goto failed;
451                 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
452                         goto failed;
453 #ifdef CONFIG_UFS_DEBUG
454                 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
455 #endif
456         }
457         for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
458                 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
459                         goto failed;
460                 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
461         }
462         sbi->s_cg_loaded = 0;
463         UFSD("EXIT\n");
464         return 1;
465
466 failed:
467         kfree (base);
468         if (sbi->s_ucg) {
469                 for (i = 0; i < uspi->s_ncg; i++)
470                         if (sbi->s_ucg[i])
471                                 brelse (sbi->s_ucg[i]);
472                 kfree (sbi->s_ucg);
473                 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
474                         kfree (sbi->s_ucpi[i]);
475         }
476         UFSD("EXIT (FAILED)\n");
477         return 0;
478 }
479
480 /*
481  * Put on-disk structures associated with cylinder groups and 
482  * write them back to disk
483  */
484 static void ufs_put_cylinder_structures (struct super_block *sb)
485 {
486         struct ufs_sb_info * sbi = UFS_SB(sb);
487         struct ufs_sb_private_info * uspi;
488         struct ufs_buffer_head * ubh;
489         unsigned char * base, * space;
490         unsigned blks, size, i;
491         
492         UFSD("ENTER\n");
493         
494         uspi = sbi->s_uspi;
495
496         size = uspi->s_cssize;
497         blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
498         base = space = (char*) sbi->s_csp;
499         for (i = 0; i < blks; i += uspi->s_fpb) {
500                 size = uspi->s_bsize;
501                 if (i + uspi->s_fpb > blks)
502                         size = (blks - i) * uspi->s_fsize;
503                 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
504                 ubh_memcpyubh (ubh, space, size);
505                 space += size;
506                 ubh_mark_buffer_uptodate (ubh, 1);
507                 ubh_mark_buffer_dirty (ubh);
508                 ubh_brelse (ubh);
509         }
510         for (i = 0; i < sbi->s_cg_loaded; i++) {
511                 ufs_put_cylinder (sb, i);
512                 kfree (sbi->s_ucpi[i]);
513         }
514         for (; i < UFS_MAX_GROUP_LOADED; i++) 
515                 kfree (sbi->s_ucpi[i]);
516         for (i = 0; i < uspi->s_ncg; i++) 
517                 brelse (sbi->s_ucg[i]);
518         kfree (sbi->s_ucg);
519         kfree (base);
520         UFSD("EXIT\n");
521 }
522
523 static int ufs_fill_super(struct super_block *sb, void *data, int silent)
524 {
525         struct ufs_sb_info * sbi;
526         struct ufs_sb_private_info * uspi;
527         struct ufs_super_block_first * usb1;
528         struct ufs_super_block_second * usb2;
529         struct ufs_super_block_third * usb3;
530         struct ufs_super_block *usb;
531         struct ufs_buffer_head * ubh;   
532         struct inode *inode;
533         unsigned block_size, super_block_size;
534         unsigned flags;
535         unsigned super_block_offset;
536
537         uspi = NULL;
538         ubh = NULL;
539         flags = 0;
540         
541         UFSD("ENTER\n");
542                 
543         sbi = kmalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
544         if (!sbi)
545                 goto failed_nomem;
546         sb->s_fs_info = sbi;
547         memset(sbi, 0, sizeof(struct ufs_sb_info));
548
549         UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
550         
551 #ifndef CONFIG_UFS_FS_WRITE
552         if (!(sb->s_flags & MS_RDONLY)) {
553                 printk("ufs was compiled with read-only support, "
554                 "can't be mounted as read-write\n");
555                 goto failed;
556         }
557 #endif
558         /*
559          * Set default mount options
560          * Parse mount options
561          */
562         sbi->s_mount_opt = 0;
563         ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
564         if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
565                 printk("wrong mount options\n");
566                 goto failed;
567         }
568         if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
569                 if (!silent)
570                         printk("You didn't specify the type of your ufs filesystem\n\n"
571                         "mount -t ufs -o ufstype="
572                         "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
573                         ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
574                         "default is ufstype=old\n");
575                 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
576         }
577
578         sbi->s_uspi = uspi =
579                 kmalloc (sizeof(struct ufs_sb_private_info), GFP_KERNEL);
580         if (!uspi)
581                 goto failed;
582
583         super_block_offset=UFS_SBLOCK;
584
585         /* Keep 2Gig file limit. Some UFS variants need to override 
586            this but as I don't know which I'll let those in the know loosen
587            the rules */
588         switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
589         case UFS_MOUNT_UFSTYPE_44BSD:
590                 UFSD("ufstype=44bsd\n");
591                 uspi->s_fsize = block_size = 512;
592                 uspi->s_fmask = ~(512 - 1);
593                 uspi->s_fshift = 9;
594                 uspi->s_sbsize = super_block_size = 1536;
595                 uspi->s_sbbase = 0;
596                 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
597                 break;
598         case UFS_MOUNT_UFSTYPE_UFS2:
599                 UFSD("ufstype=ufs2\n");
600                 super_block_offset=SBLOCK_UFS2;
601                 uspi->s_fsize = block_size = 512;
602                 uspi->s_fmask = ~(512 - 1);
603                 uspi->s_fshift = 9;
604                 uspi->s_sbsize = super_block_size = 1536;
605                 uspi->s_sbbase =  0;
606                 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
607                 if (!(sb->s_flags & MS_RDONLY)) {
608                         printk(KERN_INFO "ufstype=ufs2 is supported read-only\n");
609                         sb->s_flags |= MS_RDONLY;
610                 }
611                 break;
612                 
613         case UFS_MOUNT_UFSTYPE_SUN:
614                 UFSD("ufstype=sun\n");
615                 uspi->s_fsize = block_size = 1024;
616                 uspi->s_fmask = ~(1024 - 1);
617                 uspi->s_fshift = 10;
618                 uspi->s_sbsize = super_block_size = 2048;
619                 uspi->s_sbbase = 0;
620                 uspi->s_maxsymlinklen = 56;
621                 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
622                 break;
623
624         case UFS_MOUNT_UFSTYPE_SUNx86:
625                 UFSD("ufstype=sunx86\n");
626                 uspi->s_fsize = block_size = 1024;
627                 uspi->s_fmask = ~(1024 - 1);
628                 uspi->s_fshift = 10;
629                 uspi->s_sbsize = super_block_size = 2048;
630                 uspi->s_sbbase = 0;
631                 uspi->s_maxsymlinklen = 56;
632                 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
633                 break;
634
635         case UFS_MOUNT_UFSTYPE_OLD:
636                 UFSD("ufstype=old\n");
637                 uspi->s_fsize = block_size = 1024;
638                 uspi->s_fmask = ~(1024 - 1);
639                 uspi->s_fshift = 10;
640                 uspi->s_sbsize = super_block_size = 2048;
641                 uspi->s_sbbase = 0;
642                 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
643                 if (!(sb->s_flags & MS_RDONLY)) {
644                         if (!silent)
645                                 printk(KERN_INFO "ufstype=old is supported read-only\n");
646                         sb->s_flags |= MS_RDONLY;
647                 }
648                 break;
649         
650         case UFS_MOUNT_UFSTYPE_NEXTSTEP:
651                 UFSD("ufstype=nextstep\n");
652                 uspi->s_fsize = block_size = 1024;
653                 uspi->s_fmask = ~(1024 - 1);
654                 uspi->s_fshift = 10;
655                 uspi->s_sbsize = super_block_size = 2048;
656                 uspi->s_sbbase = 0;
657                 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
658                 if (!(sb->s_flags & MS_RDONLY)) {
659                         if (!silent)
660                                 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
661                         sb->s_flags |= MS_RDONLY;
662                 }
663                 break;
664         
665         case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
666                 UFSD("ufstype=nextstep-cd\n");
667                 uspi->s_fsize = block_size = 2048;
668                 uspi->s_fmask = ~(2048 - 1);
669                 uspi->s_fshift = 11;
670                 uspi->s_sbsize = super_block_size = 2048;
671                 uspi->s_sbbase = 0;
672                 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
673                 if (!(sb->s_flags & MS_RDONLY)) {
674                         if (!silent)
675                                 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
676                         sb->s_flags |= MS_RDONLY;
677                 }
678                 break;
679         
680         case UFS_MOUNT_UFSTYPE_OPENSTEP:
681                 UFSD("ufstype=openstep\n");
682                 uspi->s_fsize = block_size = 1024;
683                 uspi->s_fmask = ~(1024 - 1);
684                 uspi->s_fshift = 10;
685                 uspi->s_sbsize = super_block_size = 2048;
686                 uspi->s_sbbase = 0;
687                 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
688                 if (!(sb->s_flags & MS_RDONLY)) {
689                         if (!silent)
690                                 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
691                         sb->s_flags |= MS_RDONLY;
692                 }
693                 break;
694         
695         case UFS_MOUNT_UFSTYPE_HP:
696                 UFSD("ufstype=hp\n");
697                 uspi->s_fsize = block_size = 1024;
698                 uspi->s_fmask = ~(1024 - 1);
699                 uspi->s_fshift = 10;
700                 uspi->s_sbsize = super_block_size = 2048;
701                 uspi->s_sbbase = 0;
702                 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
703                 if (!(sb->s_flags & MS_RDONLY)) {
704                         if (!silent)
705                                 printk(KERN_INFO "ufstype=hp is supported read-only\n");
706                         sb->s_flags |= MS_RDONLY;
707                 }
708                 break;
709         default:
710                 if (!silent)
711                         printk("unknown ufstype\n");
712                 goto failed;
713         }
714         
715 again:  
716         if (!sb_set_blocksize(sb, block_size)) {
717                 printk(KERN_ERR "UFS: failed to set blocksize\n");
718                 goto failed;
719         }
720
721         /*
722          * read ufs super block from device
723          */
724
725         ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
726         
727         if (!ubh) 
728             goto failed;
729
730         
731         usb1 = ubh_get_usb_first(uspi);
732         usb2 = ubh_get_usb_second(uspi);
733         usb3 = ubh_get_usb_third(uspi);
734         usb  = (struct ufs_super_block *)
735                 ((struct ufs_buffer_head *)uspi)->bh[0]->b_data ;
736
737         /*
738          * Check ufs magic number
739          */
740         sbi->s_bytesex = BYTESEX_LE;
741         switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
742                 case UFS_MAGIC:
743                 case UFS2_MAGIC:
744                 case UFS_MAGIC_LFN:
745                 case UFS_MAGIC_FEA:
746                 case UFS_MAGIC_4GB:
747                         goto magic_found;
748         }
749         sbi->s_bytesex = BYTESEX_BE;
750         switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
751                 case UFS_MAGIC:
752                 case UFS2_MAGIC:
753                 case UFS_MAGIC_LFN:
754                 case UFS_MAGIC_FEA:
755                 case UFS_MAGIC_4GB:
756                         goto magic_found;
757         }
758
759         if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP) 
760           || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD) 
761           || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP)) 
762           && uspi->s_sbbase < 256) {
763                 ubh_brelse_uspi(uspi);
764                 ubh = NULL;
765                 uspi->s_sbbase += 8;
766                 goto again;
767         }
768         if (!silent)
769                 printk("ufs_read_super: bad magic number\n");
770         goto failed;
771
772 magic_found:
773         /*
774          * Check block and fragment sizes
775          */
776         uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
777         uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
778         uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
779         uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
780         uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
781
782         if (uspi->s_fsize & (uspi->s_fsize - 1)) {
783                 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
784                         uspi->s_fsize);
785                         goto failed;
786         }
787         if (uspi->s_fsize < 512) {
788                 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
789                         uspi->s_fsize);
790                 goto failed;
791         }
792         if (uspi->s_fsize > 4096) {
793                 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
794                         uspi->s_fsize);
795                 goto failed;
796         }
797         if (uspi->s_bsize & (uspi->s_bsize - 1)) {
798                 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
799                         uspi->s_bsize);
800                 goto failed;
801         }
802         if (uspi->s_bsize < 4096) {
803                 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
804                         uspi->s_bsize);
805                 goto failed;
806         }
807         if (uspi->s_bsize / uspi->s_fsize > 8) {
808                 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
809                         uspi->s_bsize / uspi->s_fsize);
810                 goto failed;
811         }
812         if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
813                 ubh_brelse_uspi(uspi);
814                 ubh = NULL;
815                 block_size = uspi->s_fsize;
816                 super_block_size = uspi->s_sbsize;
817                 UFSD("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size);
818                 goto again;
819         }
820
821 #ifdef CONFIG_UFS_DEBUG
822         if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
823                 ufs2_print_super_stuff(sb,usb);
824         else
825                 ufs_print_super_stuff(sb, usb1, usb2, usb3);
826 #endif
827
828         /*
829          * Check, if file system was correctly unmounted.
830          * If not, make it read only.
831          */
832         if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
833           ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
834           (((flags & UFS_ST_MASK) == UFS_ST_SUN || 
835           (flags & UFS_ST_MASK) == UFS_ST_SUNx86) && 
836           (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
837                 switch(usb1->fs_clean) {
838                 case UFS_FSCLEAN:
839                         UFSD("fs is clean\n");
840                         break;
841                 case UFS_FSSTABLE:
842                         UFSD("fs is stable\n");
843                         break;
844                 case UFS_FSOSF1:
845                         UFSD("fs is DEC OSF/1\n");
846                         break;
847                 case UFS_FSACTIVE:
848                         printk("ufs_read_super: fs is active\n");
849                         sb->s_flags |= MS_RDONLY;
850                         break;
851                 case UFS_FSBAD:
852                         printk("ufs_read_super: fs is bad\n");
853                         sb->s_flags |= MS_RDONLY;
854                         break;
855                 default:
856                         printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
857                         sb->s_flags |= MS_RDONLY;
858                         break;
859                 }
860         }
861         else {
862                 printk("ufs_read_super: fs needs fsck\n");
863                 sb->s_flags |= MS_RDONLY;
864         }
865
866         /*
867          * Read ufs_super_block into internal data structures
868          */
869         sb->s_op = &ufs_super_ops;
870         sb->dq_op = NULL; /***/
871         sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
872
873         uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
874         uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
875         uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
876         uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
877         uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
878         uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
879
880         if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
881                 uspi->s_u2_size  = fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_size);
882                 uspi->s_u2_dsize = fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_dsize);
883         }
884         else {
885                 uspi->s_size  =  fs32_to_cpu(sb, usb1->fs_size);
886                 uspi->s_dsize =  fs32_to_cpu(sb, usb1->fs_dsize);
887         }
888
889         uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
890         /* s_bsize already set */
891         /* s_fsize already set */
892         uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
893         uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
894         uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
895         uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
896         uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
897         uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
898         UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
899                 uspi->s_fshift);
900         uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
901         uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
902         /* s_sbsize already set */
903         uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
904         uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
905         uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
906         uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
907         uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
908         uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
909         uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
910         uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
911         uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
912         uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
913         uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
914         uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
915         uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
916         uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
917         uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
918         uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
919         uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_cpc);
920         uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_u2.fs_44.fs_contigsumsize);
921         uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
922         uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
923         uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
924         uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
925         uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
926         uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
927
928         /*
929          * Compute another frequently used values
930          */
931         uspi->s_fpbmask = uspi->s_fpb - 1;
932         if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
933                 uspi->s_apbshift = uspi->s_bshift - 3;
934         else
935                 uspi->s_apbshift = uspi->s_bshift - 2;
936
937         uspi->s_2apbshift = uspi->s_apbshift * 2;
938         uspi->s_3apbshift = uspi->s_apbshift * 3;
939         uspi->s_apb = 1 << uspi->s_apbshift;
940         uspi->s_2apb = 1 << uspi->s_2apbshift;
941         uspi->s_3apb = 1 << uspi->s_3apbshift;
942         uspi->s_apbmask = uspi->s_apb - 1;
943         uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
944         uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
945         uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
946         uspi->s_bpf = uspi->s_fsize << 3;
947         uspi->s_bpfshift = uspi->s_fshift + 3;
948         uspi->s_bpfmask = uspi->s_bpf - 1;
949         if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) ==
950             UFS_MOUNT_UFSTYPE_44BSD)
951                 uspi->s_maxsymlinklen =
952                     fs32_to_cpu(sb, usb3->fs_u2.fs_44.fs_maxsymlinklen);
953         
954         sbi->s_flags = flags;
955
956         inode = iget(sb, UFS_ROOTINO);
957         if (!inode || is_bad_inode(inode))
958                 goto failed;
959         sb->s_root = d_alloc_root(inode);
960         if (!sb->s_root)
961                 goto dalloc_failed;
962
963
964         /*
965          * Read cylinder group structures
966          */
967         if (!(sb->s_flags & MS_RDONLY))
968                 if (!ufs_read_cylinder_structures(sb))
969                         goto failed;
970
971         UFSD("EXIT\n");
972         return 0;
973
974 dalloc_failed:
975         iput(inode);
976 failed:
977         if (ubh)
978                 ubh_brelse_uspi (uspi);
979         kfree (uspi);
980         kfree(sbi);
981         sb->s_fs_info = NULL;
982         UFSD("EXIT (FAILED)\n");
983         return -EINVAL;
984
985 failed_nomem:
986         UFSD("EXIT (NOMEM)\n");
987         return -ENOMEM;
988 }
989
990 static void ufs_write_super (struct super_block *sb) {
991         struct ufs_sb_private_info * uspi;
992         struct ufs_super_block_first * usb1;
993         struct ufs_super_block_third * usb3;
994         unsigned flags;
995
996         lock_kernel();
997
998         UFSD("ENTER\n");
999         flags = UFS_SB(sb)->s_flags;
1000         uspi = UFS_SB(sb)->s_uspi;
1001         usb1 = ubh_get_usb_first(uspi);
1002         usb3 = ubh_get_usb_third(uspi);
1003
1004         if (!(sb->s_flags & MS_RDONLY)) {
1005                 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1006                 if ((flags & UFS_ST_MASK) == UFS_ST_SUN 
1007                   || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1008                         ufs_set_fs_state(sb, usb1, usb3,
1009                                         UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1010                 ubh_mark_buffer_dirty (USPI_UBH(uspi));
1011         }
1012         sb->s_dirt = 0;
1013         UFSD("EXIT\n");
1014         unlock_kernel();
1015 }
1016
1017 static void ufs_put_super(struct super_block *sb)
1018 {
1019         struct ufs_sb_info * sbi = UFS_SB(sb);
1020                 
1021         UFSD("ENTER\n");
1022
1023         if (!(sb->s_flags & MS_RDONLY))
1024                 ufs_put_cylinder_structures (sb);
1025         
1026         ubh_brelse_uspi (sbi->s_uspi);
1027         kfree (sbi->s_uspi);
1028         kfree (sbi);
1029         sb->s_fs_info = NULL;
1030 UFSD("EXIT\n");
1031         return;
1032 }
1033
1034
1035 static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1036 {
1037         struct ufs_sb_private_info * uspi;
1038         struct ufs_super_block_first * usb1;
1039         struct ufs_super_block_third * usb3;
1040         unsigned new_mount_opt, ufstype;
1041         unsigned flags;
1042         
1043         uspi = UFS_SB(sb)->s_uspi;
1044         flags = UFS_SB(sb)->s_flags;
1045         usb1 = ubh_get_usb_first(uspi);
1046         usb3 = ubh_get_usb_third(uspi);
1047         
1048         /*
1049          * Allow the "check" option to be passed as a remount option.
1050          * It is not possible to change ufstype option during remount
1051          */
1052         ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1053         new_mount_opt = 0;
1054         ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1055         if (!ufs_parse_options (data, &new_mount_opt))
1056                 return -EINVAL;
1057         if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1058                 new_mount_opt |= ufstype;
1059         }
1060         else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
1061                 printk("ufstype can't be changed during remount\n");
1062                 return -EINVAL;
1063         }
1064
1065         if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1066                 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1067                 return 0;
1068         }
1069         
1070         /*
1071          * fs was mouted as rw, remounting ro
1072          */
1073         if (*mount_flags & MS_RDONLY) {
1074                 ufs_put_cylinder_structures(sb);
1075                 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1076                 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1077                   || (flags & UFS_ST_MASK) == UFS_ST_SUNx86) 
1078                         ufs_set_fs_state(sb, usb1, usb3,
1079                                 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1080                 ubh_mark_buffer_dirty (USPI_UBH(uspi));
1081                 sb->s_dirt = 0;
1082                 sb->s_flags |= MS_RDONLY;
1083         }
1084         /*
1085          * fs was mounted as ro, remounting rw
1086          */
1087         else {
1088 #ifndef CONFIG_UFS_FS_WRITE
1089                 printk("ufs was compiled with read-only support, "
1090                 "can't be mounted as read-write\n");
1091                 return -EINVAL;
1092 #else
1093                 if (ufstype != UFS_MOUNT_UFSTYPE_SUN && 
1094                     ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
1095                     ufstype != UFS_MOUNT_UFSTYPE_SUNx86) {
1096                         printk("this ufstype is read-only supported\n");
1097                         return -EINVAL;
1098                 }
1099                 if (!ufs_read_cylinder_structures (sb)) {
1100                         printk("failed during remounting\n");
1101                         return -EPERM;
1102                 }
1103                 sb->s_flags &= ~MS_RDONLY;
1104 #endif
1105         }
1106         UFS_SB(sb)->s_mount_opt = new_mount_opt;
1107         return 0;
1108 }
1109
1110 static int ufs_statfs (struct dentry *dentry, struct kstatfs *buf)
1111 {
1112         struct super_block *sb = dentry->d_sb;
1113         struct ufs_sb_private_info * uspi;
1114         struct ufs_super_block_first * usb1;
1115         struct ufs_super_block * usb;
1116         unsigned  flags = 0;
1117
1118         lock_kernel();
1119
1120         uspi = UFS_SB(sb)->s_uspi;
1121         usb1 = ubh_get_usb_first (uspi);
1122         usb  = (struct ufs_super_block *)
1123                 ((struct ufs_buffer_head *)uspi)->bh[0]->b_data ;
1124         
1125         flags = UFS_SB(sb)->s_flags;
1126         if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1127                 buf->f_type = UFS2_MAGIC;
1128                 buf->f_blocks = fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_dsize);
1129                 buf->f_bfree = ufs_blkstofrags(fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_cstotal.cs_nbfree)) +
1130                         fs64_to_cpu(sb, usb->fs_u11.fs_u2.fs_cstotal.cs_nffree);
1131                 buf->f_ffree = fs64_to_cpu(sb,
1132                         usb->fs_u11.fs_u2.fs_cstotal.cs_nifree);
1133         }
1134         else {
1135                 buf->f_type = UFS_MAGIC;
1136                 buf->f_blocks = uspi->s_dsize;
1137                 buf->f_bfree = ufs_blkstofrags(fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree)) +
1138                         fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
1139                 buf->f_ffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
1140         }
1141         buf->f_bsize = sb->s_blocksize;
1142         buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1143                 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1144         buf->f_files = uspi->s_ncg * uspi->s_ipg;
1145         buf->f_namelen = UFS_MAXNAMLEN;
1146
1147         unlock_kernel();
1148
1149         return 0;
1150 }
1151
1152 static kmem_cache_t * ufs_inode_cachep;
1153
1154 static struct inode *ufs_alloc_inode(struct super_block *sb)
1155 {
1156         struct ufs_inode_info *ei;
1157         ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, SLAB_KERNEL);
1158         if (!ei)
1159                 return NULL;
1160         ei->vfs_inode.i_version = 1;
1161         return &ei->vfs_inode;
1162 }
1163
1164 static void ufs_destroy_inode(struct inode *inode)
1165 {
1166         kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1167 }
1168
1169 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1170 {
1171         struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1172
1173         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1174             SLAB_CTOR_CONSTRUCTOR)
1175                 inode_init_once(&ei->vfs_inode);
1176 }
1177  
1178 static int init_inodecache(void)
1179 {
1180         ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1181                                              sizeof(struct ufs_inode_info),
1182                                              0, (SLAB_RECLAIM_ACCOUNT|
1183                                                 SLAB_MEM_SPREAD),
1184                                              init_once, NULL);
1185         if (ufs_inode_cachep == NULL)
1186                 return -ENOMEM;
1187         return 0;
1188 }
1189
1190 static void destroy_inodecache(void)
1191 {
1192         if (kmem_cache_destroy(ufs_inode_cachep))
1193                 printk(KERN_INFO "ufs_inode_cache: not all structures were freed\n");
1194 }
1195
1196 #ifdef CONFIG_QUOTA
1197 static ssize_t ufs_quota_read(struct super_block *, int, char *,size_t, loff_t);
1198 static ssize_t ufs_quota_write(struct super_block *, int, const char *, size_t, loff_t);
1199 #endif
1200
1201 static struct super_operations ufs_super_ops = {
1202         .alloc_inode    = ufs_alloc_inode,
1203         .destroy_inode  = ufs_destroy_inode,
1204         .read_inode     = ufs_read_inode,
1205         .write_inode    = ufs_write_inode,
1206         .delete_inode   = ufs_delete_inode,
1207         .put_super      = ufs_put_super,
1208         .write_super    = ufs_write_super,
1209         .statfs         = ufs_statfs,
1210         .remount_fs     = ufs_remount,
1211 #ifdef CONFIG_QUOTA
1212         .quota_read     = ufs_quota_read,
1213         .quota_write    = ufs_quota_write,
1214 #endif
1215 };
1216
1217 #ifdef CONFIG_QUOTA
1218
1219 /* Read data from quotafile - avoid pagecache and such because we cannot afford
1220  * acquiring the locks... As quota files are never truncated and quota code
1221  * itself serializes the operations (and noone else should touch the files)
1222  * we don't have to be afraid of races */
1223 static ssize_t ufs_quota_read(struct super_block *sb, int type, char *data,
1224                                size_t len, loff_t off)
1225 {
1226         struct inode *inode = sb_dqopt(sb)->files[type];
1227         sector_t blk = off >> sb->s_blocksize_bits;
1228         int err = 0;
1229         int offset = off & (sb->s_blocksize - 1);
1230         int tocopy;
1231         size_t toread;
1232         struct buffer_head *bh;
1233         loff_t i_size = i_size_read(inode);
1234
1235         if (off > i_size)
1236                 return 0;
1237         if (off+len > i_size)
1238                 len = i_size-off;
1239         toread = len;
1240         while (toread > 0) {
1241                 tocopy = sb->s_blocksize - offset < toread ?
1242                                 sb->s_blocksize - offset : toread;
1243
1244                 bh = ufs_bread(inode, blk, 0, &err);
1245                 if (err)
1246                         return err;
1247                 if (!bh)        /* A hole? */
1248                         memset(data, 0, tocopy);
1249                 else {
1250                         memcpy(data, bh->b_data+offset, tocopy);
1251                         brelse(bh);
1252                 }
1253                 offset = 0;
1254                 toread -= tocopy;
1255                 data += tocopy;
1256                 blk++;
1257         }
1258         return len;
1259 }
1260
1261 /* Write to quotafile */
1262 static ssize_t ufs_quota_write(struct super_block *sb, int type,
1263                                 const char *data, size_t len, loff_t off)
1264 {
1265         struct inode *inode = sb_dqopt(sb)->files[type];
1266         sector_t blk = off >> sb->s_blocksize_bits;
1267         int err = 0;
1268         int offset = off & (sb->s_blocksize - 1);
1269         int tocopy;
1270         size_t towrite = len;
1271         struct buffer_head *bh;
1272
1273         mutex_lock(&inode->i_mutex);
1274         while (towrite > 0) {
1275                 tocopy = sb->s_blocksize - offset < towrite ?
1276                                 sb->s_blocksize - offset : towrite;
1277
1278                 bh = ufs_bread(inode, blk, 1, &err);
1279                 if (!bh)
1280                         goto out;
1281                 lock_buffer(bh);
1282                 memcpy(bh->b_data+offset, data, tocopy);
1283                 flush_dcache_page(bh->b_page);
1284                 set_buffer_uptodate(bh);
1285                 mark_buffer_dirty(bh);
1286                 unlock_buffer(bh);
1287                 brelse(bh);
1288                 offset = 0;
1289                 towrite -= tocopy;
1290                 data += tocopy;
1291                 blk++;
1292         }
1293 out:
1294         if (len == towrite) {
1295                 mutex_unlock(&inode->i_mutex);
1296                 return err;
1297         }
1298         if (inode->i_size < off+len-towrite)
1299                 i_size_write(inode, off+len-towrite);
1300         inode->i_version++;
1301         inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1302         mark_inode_dirty(inode);
1303         mutex_unlock(&inode->i_mutex);
1304         return len - towrite;
1305 }
1306
1307 #endif
1308
1309 static int ufs_get_sb(struct file_system_type *fs_type,
1310         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1311 {
1312         return get_sb_bdev(fs_type, flags, dev_name, data, ufs_fill_super, mnt);
1313 }
1314
1315 static struct file_system_type ufs_fs_type = {
1316         .owner          = THIS_MODULE,
1317         .name           = "ufs",
1318         .get_sb         = ufs_get_sb,
1319         .kill_sb        = kill_block_super,
1320         .fs_flags       = FS_REQUIRES_DEV,
1321 };
1322
1323 static int __init init_ufs_fs(void)
1324 {
1325         int err = init_inodecache();
1326         if (err)
1327                 goto out1;
1328         err = register_filesystem(&ufs_fs_type);
1329         if (err)
1330                 goto out;
1331         return 0;
1332 out:
1333         destroy_inodecache();
1334 out1:
1335         return err;
1336 }
1337
1338 static void __exit exit_ufs_fs(void)
1339 {
1340         unregister_filesystem(&ufs_fs_type);
1341         destroy_inodecache();
1342 }
1343
1344 module_init(init_ufs_fs)
1345 module_exit(exit_ufs_fs)
1346 MODULE_LICENSE("GPL");