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