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