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