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