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