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