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