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