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