super.c 43.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 *  linux/fs/ufs/super.c
 *
 * Copyright (C) 1998
 * Daniel Pirkl <daniel.pirkl@email.cz>
 * Charles University, Faculty of Mathematics and Physics
 */

/* Derived from
 *
 *  linux/fs/ext2/super.c
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  from
 *
 *  linux/fs/minix/inode.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  Big-endian to little-endian byte-swapping/bitmaps by
 *        David S. Miller (davem@caip.rutgers.edu), 1995
 */
 
/*
 * Inspired by
 *
 *  linux/fs/ufs/super.c
 *
 * Copyright (C) 1996
 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
 * Laboratory for Computer Science Research Computing Facility
 * Rutgers, The State University of New Jersey
 *
 * Copyright (C) 1996  Eddie C. Dost  (ecd@skynet.be)
 *
 * Kernel module support added on 96/04/26 by
 * Stefan Reinauer <stepan@home.culture.mipt.ru>
 *
 * Module usage counts added on 96/04/29 by
G
Gertjan van Wingerde 已提交
44
 * Gertjan van Wingerde <gwingerde@gmail.com>
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 *
 * Clean swab support on 19970406 by
 * Francois-Rene Rideau <fare@tunes.org>
 *
 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
 *
 * NeXTstep support added on February 5th 1998 by
 * Niels Kristian Bech Jensen <nkbj@image.dk>.
 *
 * write support Daniel Pirkl <daniel.pirkl@email.cz> 1998
 * 
 * HP/UX hfs filesystem support added by
 * Martin K. Petersen <mkp@mkp.net>, August 1999
 *
 * UFS2 (of FreeBSD 5.x) support added by
 * Niraj Kumar <niraj17@iitbombay.org>, Jan 2004
 *
64 65
 * UFS2 write support added by
 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
L
Linus Torvalds 已提交
66 67 68
 */


A
Alexey Dobriyan 已提交
69
#include <linux/exportfs.h>
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <linux/module.h>
#include <linux/bitops.h>

#include <stdarg.h>

#include <asm/uaccess.h>
#include <asm/system.h>

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/blkdev.h>
#include <linux/init.h>
#include <linux/parser.h>
#include <linux/buffer_head.h>
#include <linux/vfs.h>
V
vignesh babu 已提交
89
#include <linux/log2.h>
E
Evgeniy Dushistov 已提交
90 91
#include <linux/mount.h>
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
92

93
#include "ufs_fs.h"
94
#include "ufs.h"
L
Linus Torvalds 已提交
95 96 97
#include "swab.h"
#include "util.h"

A
Arnd Bergmann 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
void lock_ufs(struct super_block *sb)
{
#if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
	struct ufs_sb_info *sbi = UFS_SB(sb);

	mutex_lock(&sbi->mutex);
	sbi->mutex_owner = current;
#endif
}

void unlock_ufs(struct super_block *sb)
{
#if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
	struct ufs_sb_info *sbi = UFS_SB(sb);

	sbi->mutex_owner = NULL;
	mutex_unlock(&sbi->mutex);
#endif
}

A
Alexey Dobriyan 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static struct inode *ufs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
{
	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
	struct inode *inode;

	if (ino < UFS_ROOTINO || ino > uspi->s_ncg * uspi->s_ipg)
		return ERR_PTR(-ESTALE);

	inode = ufs_iget(sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
	if (generation && inode->i_generation != generation) {
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
	return inode;
}

static struct dentry *ufs_fh_to_dentry(struct super_block *sb, struct fid *fid,
				       int fh_len, int fh_type)
{
	return generic_fh_to_dentry(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
}

static struct dentry *ufs_fh_to_parent(struct super_block *sb, struct fid *fid,
				       int fh_len, int fh_type)
{
	return generic_fh_to_parent(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
}

static struct dentry *ufs_get_parent(struct dentry *child)
{
	struct qstr dot_dot = {
		.name	= "..",
		.len	= 2,
	};
	ino_t ino;

	ino = ufs_inode_by_name(child->d_inode, &dot_dot);
	if (!ino)
		return ERR_PTR(-ENOENT);
	return d_obtain_alias(ufs_iget(child->d_inode->i_sb, ino));
}

static const struct export_operations ufs_export_ops = {
	.fh_to_dentry	= ufs_fh_to_dentry,
	.fh_to_parent	= ufs_fh_to_parent,
	.get_parent	= ufs_get_parent,
};

E
Evgeniy Dushistov 已提交
168
#ifdef CONFIG_UFS_DEBUG
L
Linus Torvalds 已提交
169 170 171
/*
 * Print contents of ufs_super_block, useful for debugging
 */
172
static void ufs_print_super_stuff(struct super_block *sb,
173 174 175
				  struct ufs_super_block_first *usb1,
				  struct ufs_super_block_second *usb2,
				  struct ufs_super_block_third *usb3)
L
Linus Torvalds 已提交
176
{
177 178
	u32 magic = fs32_to_cpu(sb, usb3->fs_magic);

L
Linus Torvalds 已提交
179
	printk("ufs_print_super_stuff\n");
180 181
	printk("  magic:     0x%x\n", magic);
	if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
		printk("  fs_size:   %llu\n", (unsigned long long)
		       fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
		printk("  fs_dsize:  %llu\n", (unsigned long long)
		       fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
		printk("  bsize:         %u\n",
		       fs32_to_cpu(sb, usb1->fs_bsize));
		printk("  fsize:         %u\n",
		       fs32_to_cpu(sb, usb1->fs_fsize));
		printk("  fs_volname:  %s\n", usb2->fs_un.fs_u2.fs_volname);
		printk("  fs_sblockloc: %llu\n", (unsigned long long)
		       fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
		printk("  cs_ndir(No of dirs):  %llu\n", (unsigned long long)
		       fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
		printk("  cs_nbfree(No of free blocks):  %llu\n",
		       (unsigned long long)
		       fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
198 199 200 201 202 203
		printk(KERN_INFO"  cs_nifree(Num of free inodes): %llu\n",
		       (unsigned long long)
		       fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
		printk(KERN_INFO"  cs_nffree(Num of free frags): %llu\n",
		       (unsigned long long)
		       fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
204 205
		printk(KERN_INFO"  fs_maxsymlinklen: %u\n",
		       fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	} else {
		printk(" sblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
		printk(" cblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
		printk(" iblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
		printk(" dblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
		printk(" cgoffset:    %u\n",
		       fs32_to_cpu(sb, usb1->fs_cgoffset));
		printk(" ~cgmask:     0x%x\n",
		       ~fs32_to_cpu(sb, usb1->fs_cgmask));
		printk(" size:        %u\n", fs32_to_cpu(sb, usb1->fs_size));
		printk(" dsize:       %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
		printk(" ncg:         %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
		printk(" bsize:       %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
		printk(" fsize:       %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
		printk(" frag:        %u\n", fs32_to_cpu(sb, usb1->fs_frag));
		printk(" fragshift:   %u\n",
		       fs32_to_cpu(sb, usb1->fs_fragshift));
		printk(" ~fmask:      %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
		printk(" fshift:      %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
		printk(" sbsize:      %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
		printk(" spc:         %u\n", fs32_to_cpu(sb, usb1->fs_spc));
		printk(" cpg:         %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
		printk(" ipg:         %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
		printk(" fpg:         %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
		printk(" csaddr:      %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
		printk(" cssize:      %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
		printk(" cgsize:      %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
		printk(" fstodb:      %u\n",
		       fs32_to_cpu(sb, usb1->fs_fsbtodb));
		printk(" nrpos:       %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
		printk(" ndir         %u\n",
		       fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
		printk(" nifree       %u\n",
		       fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
		printk(" nbfree       %u\n",
		       fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
		printk(" nffree       %u\n",
		       fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
	}
L
Linus Torvalds 已提交
245 246 247 248 249 250
	printk("\n");
}

/*
 * Print contents of ufs_cylinder_group, useful for debugging
 */
251 252
static void ufs_print_cylinder_stuff(struct super_block *sb,
				     struct ufs_cylinder_group *cg)
L
Linus Torvalds 已提交
253 254
{
	printk("\nufs_print_cylinder_stuff\n");
255
	printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	printk("  magic:        %x\n", fs32_to_cpu(sb, cg->cg_magic));
	printk("  time:         %u\n", fs32_to_cpu(sb, cg->cg_time));
	printk("  cgx:          %u\n", fs32_to_cpu(sb, cg->cg_cgx));
	printk("  ncyl:         %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
	printk("  niblk:        %u\n", fs16_to_cpu(sb, cg->cg_niblk));
	printk("  ndblk:        %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
	printk("  cs_ndir:      %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
	printk("  cs_nbfree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
	printk("  cs_nifree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
	printk("  cs_nffree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
	printk("  rotor:        %u\n", fs32_to_cpu(sb, cg->cg_rotor));
	printk("  frotor:       %u\n", fs32_to_cpu(sb, cg->cg_frotor));
	printk("  irotor:       %u\n", fs32_to_cpu(sb, cg->cg_irotor));
	printk("  frsum:        %u, %u, %u, %u, %u, %u, %u, %u\n",
	    fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
	    fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
	    fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
	    fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
	printk("  btotoff:      %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
	printk("  boff:         %u\n", fs32_to_cpu(sb, cg->cg_boff));
	printk("  iuseoff:      %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
	printk("  freeoff:      %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
	printk("  nextfreeoff:  %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
279 280 281 282 283 284
	printk("  clustersumoff %u\n",
	       fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
	printk("  clusteroff    %u\n",
	       fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
	printk("  nclusterblks  %u\n",
	       fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
L
Linus Torvalds 已提交
285 286
	printk("\n");
}
287
#else
288
#  define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
289
#  define ufs_print_cylinder_stuff(sb, cg) /**/
E
Evgeniy Dushistov 已提交
290
#endif /* CONFIG_UFS_DEBUG */
L
Linus Torvalds 已提交
291

292
static const struct super_operations ufs_super_ops;
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300 301 302 303

static char error_buf[1024];

void ufs_error (struct super_block * sb, const char * function,
	const char * fmt, ...)
{
	struct ufs_sb_private_info * uspi;
	struct ufs_super_block_first * usb1;
	va_list args;

	uspi = UFS_SB(sb)->s_uspi;
E
Evgeniy 已提交
304
	usb1 = ubh_get_usb_first(uspi);
L
Linus Torvalds 已提交
305 306 307
	
	if (!(sb->s_flags & MS_RDONLY)) {
		usb1->fs_clean = UFS_FSBAD;
E
Evgeniy Dushistov 已提交
308
		ubh_mark_buffer_dirty(USPI_UBH(uspi));
L
Linus Torvalds 已提交
309 310 311 312
		sb->s_dirt = 1;
		sb->s_flags |= MS_RDONLY;
	}
	va_start (args, fmt);
313
	vsnprintf (error_buf, sizeof(error_buf), fmt, args);
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	va_end (args);
	switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
	case UFS_MOUNT_ONERROR_PANIC:
		panic ("UFS-fs panic (device %s): %s: %s\n", 
			sb->s_id, function, error_buf);

	case UFS_MOUNT_ONERROR_LOCK:
	case UFS_MOUNT_ONERROR_UMOUNT:
	case UFS_MOUNT_ONERROR_REPAIR:
		printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
			sb->s_id, function, error_buf);
	}		
}

void ufs_panic (struct super_block * sb, const char * function,
	const char * fmt, ...)
{
	struct ufs_sb_private_info * uspi;
	struct ufs_super_block_first * usb1;
	va_list args;
	
	uspi = UFS_SB(sb)->s_uspi;
E
Evgeniy 已提交
336
	usb1 = ubh_get_usb_first(uspi);
L
Linus Torvalds 已提交
337 338 339
	
	if (!(sb->s_flags & MS_RDONLY)) {
		usb1->fs_clean = UFS_FSBAD;
E
Evgeniy Dushistov 已提交
340
		ubh_mark_buffer_dirty(USPI_UBH(uspi));
L
Linus Torvalds 已提交
341 342 343
		sb->s_dirt = 1;
	}
	va_start (args, fmt);
344
	vsnprintf (error_buf, sizeof(error_buf), fmt, args);
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356
	va_end (args);
	sb->s_flags |= MS_RDONLY;
	printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
		sb->s_id, function, error_buf);
}

void ufs_warning (struct super_block * sb, const char * function,
	const char * fmt, ...)
{
	va_list args;

	va_start (args, fmt);
357
	vsnprintf (error_buf, sizeof(error_buf), fmt, args);
L
Linus Torvalds 已提交
358 359 360 361 362 363
	va_end (args);
	printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
		sb->s_id, function, error_buf);
}

enum {
E
Evgeniy Dushistov 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
       Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
       Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
       Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
       Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
       Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
       Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
       Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
       Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
       Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
       Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
       Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
       Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
       Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
       Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
       Opt_err
L
Linus Torvalds 已提交
379 380
};

381
static const match_table_t tokens = {
L
Linus Torvalds 已提交
382 383 384
	{Opt_type_old, "ufstype=old"},
	{Opt_type_sunx86, "ufstype=sunx86"},
	{Opt_type_sun, "ufstype=sun"},
385
	{Opt_type_sunos, "ufstype=sunos"},
L
Linus Torvalds 已提交
386 387 388 389 390 391 392
	{Opt_type_44bsd, "ufstype=44bsd"},
	{Opt_type_ufs2, "ufstype=ufs2"},
	{Opt_type_ufs2, "ufstype=5xbsd"},
	{Opt_type_hp, "ufstype=hp"},
	{Opt_type_nextstepcd, "ufstype=nextstep-cd"},
	{Opt_type_nextstep, "ufstype=nextstep"},
	{Opt_type_openstep, "ufstype=openstep"},
E
Evgeniy Dushistov 已提交
393
/*end of possible ufs types */
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404
	{Opt_onerror_panic, "onerror=panic"},
	{Opt_onerror_lock, "onerror=lock"},
	{Opt_onerror_umount, "onerror=umount"},
	{Opt_onerror_repair, "onerror=repair"},
	{Opt_err, NULL}
};

static int ufs_parse_options (char * options, unsigned * mount_options)
{
	char * p;
	
E
Evgeniy Dushistov 已提交
405
	UFSD("ENTER\n");
L
Linus Torvalds 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	
	if (!options)
		return 1;

	while ((p = strsep(&options, ",")) != NULL) {
		substring_t args[MAX_OPT_ARGS];
		int token;
		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case Opt_type_old:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_OLD);
			break;
		case Opt_type_sunx86:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
			break;
		case Opt_type_sun:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_SUN);
			break;
430 431 432 433
		case Opt_type_sunos:
			ufs_clear_opt(*mount_options, UFSTYPE);
			ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
			break;
L
Linus Torvalds 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
		case Opt_type_44bsd:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_44BSD);
			break;
		case Opt_type_ufs2:
			ufs_clear_opt(*mount_options, UFSTYPE);
			ufs_set_opt(*mount_options, UFSTYPE_UFS2);
			break;
		case Opt_type_hp:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_HP);
			break;
		case Opt_type_nextstepcd:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
			break;
		case Opt_type_nextstep:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
			break;
		case Opt_type_openstep:
			ufs_clear_opt (*mount_options, UFSTYPE);
			ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
			break;
		case Opt_onerror_panic:
			ufs_clear_opt (*mount_options, ONERROR);
			ufs_set_opt (*mount_options, ONERROR_PANIC);
			break;
		case Opt_onerror_lock:
			ufs_clear_opt (*mount_options, ONERROR);
			ufs_set_opt (*mount_options, ONERROR_LOCK);
			break;
		case Opt_onerror_umount:
			ufs_clear_opt (*mount_options, ONERROR);
			ufs_set_opt (*mount_options, ONERROR_UMOUNT);
			break;
		case Opt_onerror_repair:
			printk("UFS-fs: Unable to do repair on error, "
				"will lock lock instead\n");
			ufs_clear_opt (*mount_options, ONERROR);
			ufs_set_opt (*mount_options, ONERROR_REPAIR);
			break;
		default:
			printk("UFS-fs: Invalid option: \"%s\" "
					"or missing value\n", p);
			return 0;
		}
	}
	return 1;
}

/*
L
Lucas De Marchi 已提交
486 487 488
 * Different types of UFS hold fs_cstotal in different
 * places, and use different data structure for it.
 * To make things simpler we just copy fs_cstotal to ufs_sb_private_info
L
Linus Torvalds 已提交
489
 */
490
static void ufs_setup_cstotal(struct super_block *sb)
491
{
492 493
	struct ufs_sb_info *sbi = UFS_SB(sb);
	struct ufs_sb_private_info *uspi = sbi->s_uspi;
494 495
	struct ufs_super_block_first *usb1;
	struct ufs_super_block_second *usb2;
496
	struct ufs_super_block_third *usb3;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;

	UFSD("ENTER, mtype=%u\n", mtype);
	usb1 = ubh_get_usb_first(uspi);
	usb2 = ubh_get_usb_second(uspi);
	usb3 = ubh_get_usb_third(uspi);

	if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
	     (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
	    mtype == UFS_MOUNT_UFSTYPE_UFS2) {
		/*we have statistic in different place, then usual*/
		uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
		uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
		uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
		uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
	} else {
		uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
		uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
		uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
		uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
	}
	UFSD("EXIT\n");
}

/*
 * Read on-disk structures associated with cylinder groups
 */
static int ufs_read_cylinder_structures(struct super_block *sb)
{
	struct ufs_sb_info *sbi = UFS_SB(sb);
	struct ufs_sb_private_info *uspi = sbi->s_uspi;
L
Linus Torvalds 已提交
528 529 530
	struct ufs_buffer_head * ubh;
	unsigned char * base, * space;
	unsigned size, blks, i;
531 532
	struct ufs_super_block_third *usb3;

E
Evgeniy Dushistov 已提交
533
	UFSD("ENTER\n");
L
Linus Torvalds 已提交
534

535
	usb3 = ubh_get_usb_third(uspi);
L
Linus Torvalds 已提交
536 537 538 539 540 541
	/*
	 * Read cs structures from (usually) first data block
	 * on the device. 
	 */
	size = uspi->s_cssize;
	blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
A
Arnd Bergmann 已提交
542
	base = space = kmalloc(size, GFP_NOFS);
L
Linus Torvalds 已提交
543 544
	if (!base)
		goto failed; 
545
	sbi->s_csp = (struct ufs_csum *)space;
L
Linus Torvalds 已提交
546 547 548 549 550
	for (i = 0; i < blks; i += uspi->s_fpb) {
		size = uspi->s_bsize;
		if (i + uspi->s_fpb > blks)
			size = (blks - i) * uspi->s_fsize;

551
		ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
E
Evgeniy 已提交
552 553 554 555 556 557
		
		if (!ubh)
			goto failed;

		ubh_ubhcpymem (space, ubh, size);

L
Linus Torvalds 已提交
558 559 560 561 562 563 564 565 566
		space += size;
		ubh_brelse (ubh);
		ubh = NULL;
	}

	/*
	 * Read cylinder group (we read only first fragment from block
	 * at this time) and prepare internal data structures for cg caching.
	 */
A
Arnd Bergmann 已提交
567
	if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_NOFS)))
L
Linus Torvalds 已提交
568 569 570 571 572 573 574 575
		goto failed;
	for (i = 0; i < uspi->s_ncg; i++) 
		sbi->s_ucg[i] = NULL;
	for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
		sbi->s_ucpi[i] = NULL;
		sbi->s_cgno[i] = UFS_CGNO_EMPTY;
	}
	for (i = 0; i < uspi->s_ncg; i++) {
E
Evgeniy Dushistov 已提交
576
		UFSD("read cg %u\n", i);
L
Linus Torvalds 已提交
577 578 579 580
		if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
			goto failed;
		if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
			goto failed;
581

L
Linus Torvalds 已提交
582 583 584
		ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
	}
	for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
A
Arnd Bergmann 已提交
585
		if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_NOFS)))
L
Linus Torvalds 已提交
586 587 588 589
			goto failed;
		sbi->s_cgno[i] = UFS_CGNO_EMPTY;
	}
	sbi->s_cg_loaded = 0;
E
Evgeniy Dushistov 已提交
590
	UFSD("EXIT\n");
L
Linus Torvalds 已提交
591 592 593
	return 1;

failed:
J
Jesper Juhl 已提交
594
	kfree (base);
L
Linus Torvalds 已提交
595 596
	if (sbi->s_ucg) {
		for (i = 0; i < uspi->s_ncg; i++)
J
Jesper Juhl 已提交
597 598
			if (sbi->s_ucg[i])
				brelse (sbi->s_ucg[i]);
L
Linus Torvalds 已提交
599 600
		kfree (sbi->s_ucg);
		for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
J
Jesper Juhl 已提交
601
			kfree (sbi->s_ucpi[i]);
L
Linus Torvalds 已提交
602
	}
E
Evgeniy Dushistov 已提交
603
	UFSD("EXIT (FAILED)\n");
L
Linus Torvalds 已提交
604 605 606 607
	return 0;
}

/*
608
 * Sync our internal copy of fs_cstotal with disk
L
Linus Torvalds 已提交
609
 */
610
static void ufs_put_cstotal(struct super_block *sb)
611
{
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
	struct ufs_super_block_first *usb1;
	struct ufs_super_block_second *usb2;
	struct ufs_super_block_third *usb3;

	UFSD("ENTER\n");
	usb1 = ubh_get_usb_first(uspi);
	usb2 = ubh_get_usb_second(uspi);
	usb3 = ubh_get_usb_third(uspi);

	if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
	     (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
	    mtype == UFS_MOUNT_UFSTYPE_UFS2) {
		/*we have statistic in different place, then usual*/
		usb2->fs_un.fs_u2.cs_ndir =
			cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
		usb2->fs_un.fs_u2.cs_nbfree =
			cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
		usb3->fs_un1.fs_u2.cs_nifree =
			cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
		usb3->fs_un1.fs_u2.cs_nffree =
			cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
	} else {
		usb1->fs_cstotal.cs_ndir =
			cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
		usb1->fs_cstotal.cs_nbfree =
			cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
		usb1->fs_cstotal.cs_nifree =
			cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
		usb1->fs_cstotal.cs_nffree =
			cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
	}
	ubh_mark_buffer_dirty(USPI_UBH(uspi));
646
	ufs_print_super_stuff(sb, usb1, usb2, usb3);
647 648 649 650 651 652 653 654 655 656 657 658 659
	UFSD("EXIT\n");
}

/**
 * ufs_put_super_internal() - put on-disk intrenal structures
 * @sb: pointer to super_block structure
 * Put on-disk structures associated with cylinder groups
 * and write them back to disk, also update cs_total on disk
 */
static void ufs_put_super_internal(struct super_block *sb)
{
	struct ufs_sb_info *sbi = UFS_SB(sb);
	struct ufs_sb_private_info *uspi = sbi->s_uspi;
L
Linus Torvalds 已提交
660 661 662
	struct ufs_buffer_head * ubh;
	unsigned char * base, * space;
	unsigned blks, size, i;
663

L
Linus Torvalds 已提交
664
	
E
Evgeniy Dushistov 已提交
665
	UFSD("ENTER\n");
666

667
	ufs_put_cstotal(sb);
L
Linus Torvalds 已提交
668 669
	size = uspi->s_cssize;
	blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
670
	base = space = (char*) sbi->s_csp;
L
Linus Torvalds 已提交
671 672 673 674
	for (i = 0; i < blks; i += uspi->s_fpb) {
		size = uspi->s_bsize;
		if (i + uspi->s_fpb > blks)
			size = (blks - i) * uspi->s_fsize;
675

L
Linus Torvalds 已提交
676
		ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
677

L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		ubh_memcpyubh (ubh, space, size);
		space += size;
		ubh_mark_buffer_uptodate (ubh, 1);
		ubh_mark_buffer_dirty (ubh);
		ubh_brelse (ubh);
	}
	for (i = 0; i < sbi->s_cg_loaded; i++) {
		ufs_put_cylinder (sb, i);
		kfree (sbi->s_ucpi[i]);
	}
	for (; i < UFS_MAX_GROUP_LOADED; i++) 
		kfree (sbi->s_ucpi[i]);
	for (i = 0; i < uspi->s_ncg; i++) 
		brelse (sbi->s_ucg[i]);
	kfree (sbi->s_ucg);
	kfree (base);
694

E
Evgeniy Dushistov 已提交
695
	UFSD("EXIT\n");
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708
}

static int ufs_fill_super(struct super_block *sb, void *data, int silent)
{
	struct ufs_sb_info * sbi;
	struct ufs_sb_private_info * uspi;
	struct ufs_super_block_first * usb1;
	struct ufs_super_block_second * usb2;
	struct ufs_super_block_third * usb3;
	struct ufs_buffer_head * ubh;	
	struct inode *inode;
	unsigned block_size, super_block_size;
	unsigned flags;
E
Evgeniy 已提交
709
	unsigned super_block_offset;
710
	unsigned maxsymlen;
711
	int ret = -EINVAL;
L
Linus Torvalds 已提交
712 713 714 715 716

	uspi = NULL;
	ubh = NULL;
	flags = 0;
	
E
Evgeniy Dushistov 已提交
717
	UFSD("ENTER\n");
L
Linus Torvalds 已提交
718
		
719
	sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
L
Linus Torvalds 已提交
720 721 722 723
	if (!sbi)
		goto failed_nomem;
	sb->s_fs_info = sbi;

E
Evgeniy Dushistov 已提交
724
	UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732
	
#ifndef CONFIG_UFS_FS_WRITE
	if (!(sb->s_flags & MS_RDONLY)) {
		printk("ufs was compiled with read-only support, "
		"can't be mounted as read-write\n");
		goto failed;
	}
#endif
A
Arnd Bergmann 已提交
733
	mutex_init(&sbi->mutex);
L
Linus Torvalds 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747
	/*
	 * Set default mount options
	 * Parse mount options
	 */
	sbi->s_mount_opt = 0;
	ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
	if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
		printk("wrong mount options\n");
		goto failed;
	}
	if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
		if (!silent)
			printk("You didn't specify the type of your ufs filesystem\n\n"
			"mount -t ufs -o ufstype="
M
Michael Owen 已提交
748
			"sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
L
Linus Torvalds 已提交
749 750 751 752 753
			">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
			"default is ufstype=old\n");
		ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
	}

754 755
	uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
	sbi->s_uspi = uspi;
L
Linus Torvalds 已提交
756 757
	if (!uspi)
		goto failed;
758
	uspi->s_dirblksize = UFS_SECTOR_SIZE;
E
Evgeniy 已提交
759 760
	super_block_offset=UFS_SBLOCK;

L
Linus Torvalds 已提交
761 762 763 764 765
	/* Keep 2Gig file limit. Some UFS variants need to override 
	   this but as I don't know which I'll let those in the know loosen
	   the rules */
	switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
	case UFS_MOUNT_UFSTYPE_44BSD:
E
Evgeniy Dushistov 已提交
766
		UFSD("ufstype=44bsd\n");
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774
		uspi->s_fsize = block_size = 512;
		uspi->s_fmask = ~(512 - 1);
		uspi->s_fshift = 9;
		uspi->s_sbsize = super_block_size = 1536;
		uspi->s_sbbase = 0;
		flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
		break;
	case UFS_MOUNT_UFSTYPE_UFS2:
E
Evgeniy Dushistov 已提交
775
		UFSD("ufstype=ufs2\n");
E
Evgeniy 已提交
776
		super_block_offset=SBLOCK_UFS2;
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785
		uspi->s_fsize = block_size = 512;
		uspi->s_fmask = ~(512 - 1);
		uspi->s_fshift = 9;
		uspi->s_sbsize = super_block_size = 1536;
		uspi->s_sbbase =  0;
		flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
		break;
		
	case UFS_MOUNT_UFSTYPE_SUN:
E
Evgeniy Dushistov 已提交
786
		UFSD("ufstype=sun\n");
L
Linus Torvalds 已提交
787 788 789 790 791
		uspi->s_fsize = block_size = 1024;
		uspi->s_fmask = ~(1024 - 1);
		uspi->s_fshift = 10;
		uspi->s_sbsize = super_block_size = 2048;
		uspi->s_sbbase = 0;
792
		uspi->s_maxsymlinklen = 0; /* Not supported on disk */
L
Linus Torvalds 已提交
793 794 795
		flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
		break;

796 797 798 799 800 801 802 803 804 805 806 807
	case UFS_MOUNT_UFSTYPE_SUNOS:
		UFSD(("ufstype=sunos\n"))
		uspi->s_fsize = block_size = 1024;
		uspi->s_fmask = ~(1024 - 1);
		uspi->s_fshift = 10;
		uspi->s_sbsize = 2048;
		super_block_size = 2048;
		uspi->s_sbbase = 0;
		uspi->s_maxsymlinklen = 0; /* Not supported on disk */
		flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
		break;

L
Linus Torvalds 已提交
808
	case UFS_MOUNT_UFSTYPE_SUNx86:
E
Evgeniy Dushistov 已提交
809
		UFSD("ufstype=sunx86\n");
L
Linus Torvalds 已提交
810 811 812 813 814
		uspi->s_fsize = block_size = 1024;
		uspi->s_fmask = ~(1024 - 1);
		uspi->s_fshift = 10;
		uspi->s_sbsize = super_block_size = 2048;
		uspi->s_sbbase = 0;
815
		uspi->s_maxsymlinklen = 0; /* Not supported on disk */
L
Linus Torvalds 已提交
816 817 818 819
		flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
		break;

	case UFS_MOUNT_UFSTYPE_OLD:
E
Evgeniy Dushistov 已提交
820
		UFSD("ufstype=old\n");
L
Linus Torvalds 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834
		uspi->s_fsize = block_size = 1024;
		uspi->s_fmask = ~(1024 - 1);
		uspi->s_fshift = 10;
		uspi->s_sbsize = super_block_size = 2048;
		uspi->s_sbbase = 0;
		flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
		if (!(sb->s_flags & MS_RDONLY)) {
			if (!silent)
				printk(KERN_INFO "ufstype=old is supported read-only\n");
			sb->s_flags |= MS_RDONLY;
		}
		break;
	
	case UFS_MOUNT_UFSTYPE_NEXTSTEP:
E
Evgeniy Dushistov 已提交
835
		UFSD("ufstype=nextstep\n");
L
Linus Torvalds 已提交
836 837 838 839 840
		uspi->s_fsize = block_size = 1024;
		uspi->s_fmask = ~(1024 - 1);
		uspi->s_fshift = 10;
		uspi->s_sbsize = super_block_size = 2048;
		uspi->s_sbbase = 0;
841
		uspi->s_dirblksize = 1024;
L
Linus Torvalds 已提交
842 843 844 845 846 847 848 849 850
		flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
		if (!(sb->s_flags & MS_RDONLY)) {
			if (!silent)
				printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
			sb->s_flags |= MS_RDONLY;
		}
		break;
	
	case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
E
Evgeniy Dushistov 已提交
851
		UFSD("ufstype=nextstep-cd\n");
L
Linus Torvalds 已提交
852 853 854 855 856
		uspi->s_fsize = block_size = 2048;
		uspi->s_fmask = ~(2048 - 1);
		uspi->s_fshift = 11;
		uspi->s_sbsize = super_block_size = 2048;
		uspi->s_sbbase = 0;
857
		uspi->s_dirblksize = 1024;
L
Linus Torvalds 已提交
858 859 860 861 862 863 864 865 866
		flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
		if (!(sb->s_flags & MS_RDONLY)) {
			if (!silent)
				printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
			sb->s_flags |= MS_RDONLY;
		}
		break;
	
	case UFS_MOUNT_UFSTYPE_OPENSTEP:
E
Evgeniy Dushistov 已提交
867
		UFSD("ufstype=openstep\n");
L
Linus Torvalds 已提交
868 869 870 871 872
		uspi->s_fsize = block_size = 1024;
		uspi->s_fmask = ~(1024 - 1);
		uspi->s_fshift = 10;
		uspi->s_sbsize = super_block_size = 2048;
		uspi->s_sbbase = 0;
873
		uspi->s_dirblksize = 1024;
L
Linus Torvalds 已提交
874 875 876 877 878 879 880 881 882
		flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
		if (!(sb->s_flags & MS_RDONLY)) {
			if (!silent)
				printk(KERN_INFO "ufstype=openstep is supported read-only\n");
			sb->s_flags |= MS_RDONLY;
		}
		break;
	
	case UFS_MOUNT_UFSTYPE_HP:
E
Evgeniy Dushistov 已提交
883
		UFSD("ufstype=hp\n");
L
Linus Torvalds 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
		uspi->s_fsize = block_size = 1024;
		uspi->s_fmask = ~(1024 - 1);
		uspi->s_fshift = 10;
		uspi->s_sbsize = super_block_size = 2048;
		uspi->s_sbbase = 0;
		flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
		if (!(sb->s_flags & MS_RDONLY)) {
			if (!silent)
				printk(KERN_INFO "ufstype=hp is supported read-only\n");
			sb->s_flags |= MS_RDONLY;
 		}
 		break;
	default:
		if (!silent)
			printk("unknown ufstype\n");
		goto failed;
	}
	
again:	
	if (!sb_set_blocksize(sb, block_size)) {
		printk(KERN_ERR "UFS: failed to set blocksize\n");
		goto failed;
	}

	/*
	 * read ufs super block from device
	 */
E
Evgeniy 已提交
911 912 913

	ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
	
L
Linus Torvalds 已提交
914 915 916
	if (!ubh) 
            goto failed;

E
Evgeniy 已提交
917 918 919
	usb1 = ubh_get_usb_first(uspi);
	usb2 = ubh_get_usb_second(uspi);
	usb3 = ubh_get_usb_third(uspi);
L
Linus Torvalds 已提交
920

921 922 923 924 925 926 927 928
	/* Sort out mod used on SunOS 4.1.3 for fs_state */
	uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
	if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
	    (uspi->s_postblformat != UFS_42POSTBLFMT)) {
		flags &= ~UFS_ST_MASK;
		flags |=  UFS_ST_SUN;
	}

L
Linus Torvalds 已提交
929 930 931 932 933 934
	/*
	 * Check ufs magic number
	 */
	sbi->s_bytesex = BYTESEX_LE;
	switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
		case UFS_MAGIC:
935
		case UFS_MAGIC_BW:
L
Linus Torvalds 已提交
936 937 938 939 940 941 942 943 944
		case UFS2_MAGIC:
		case UFS_MAGIC_LFN:
	        case UFS_MAGIC_FEA:
	        case UFS_MAGIC_4GB:
			goto magic_found;
	}
	sbi->s_bytesex = BYTESEX_BE;
	switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
		case UFS_MAGIC:
945
		case UFS_MAGIC_BW:
L
Linus Torvalds 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
		case UFS2_MAGIC:
		case UFS_MAGIC_LFN:
	        case UFS_MAGIC_FEA:
	        case UFS_MAGIC_4GB:
			goto magic_found;
	}

	if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP) 
	  || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD) 
	  || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP)) 
	  && uspi->s_sbbase < 256) {
		ubh_brelse_uspi(uspi);
		ubh = NULL;
		uspi->s_sbbase += 8;
		goto again;
	}
	if (!silent)
		printk("ufs_read_super: bad magic number\n");
	goto failed;

magic_found:
	/*
	 * Check block and fragment sizes
	 */
	uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
	uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
	uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
	uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
	uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);

V
vignesh babu 已提交
976
	if (!is_power_of_2(uspi->s_fsize)) {
L
Linus Torvalds 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990
		printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
			uspi->s_fsize);
			goto failed;
	}
	if (uspi->s_fsize < 512) {
		printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
			uspi->s_fsize);
		goto failed;
	}
	if (uspi->s_fsize > 4096) {
		printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
			uspi->s_fsize);
		goto failed;
	}
V
vignesh babu 已提交
991
	if (!is_power_of_2(uspi->s_bsize)) {
L
Linus Torvalds 已提交
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
			uspi->s_bsize);
		goto failed;
	}
	if (uspi->s_bsize < 4096) {
		printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
			uspi->s_bsize);
		goto failed;
	}
	if (uspi->s_bsize / uspi->s_fsize > 8) {
		printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
			uspi->s_bsize / uspi->s_fsize);
		goto failed;
	}
	if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
		ubh_brelse_uspi(uspi);
		ubh = NULL;
		block_size = uspi->s_fsize;
		super_block_size = uspi->s_sbsize;
E
Evgeniy Dushistov 已提交
1011
		UFSD("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size);
L
Linus Torvalds 已提交
1012 1013 1014
		goto again;
	}

1015
	sbi->s_flags = flags;/*after that line some functions use s_flags*/
1016
	ufs_print_super_stuff(sb, usb1, usb2, usb3);
L
Linus Torvalds 已提交
1017 1018 1019 1020 1021

	/*
	 * Check, if file system was correctly unmounted.
	 * If not, make it read only.
	 */
1022 1023 1024 1025 1026 1027
	if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
	  ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
	  (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
	    (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
	  (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
	  (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
L
Linus Torvalds 已提交
1028 1029
		switch(usb1->fs_clean) {
		case UFS_FSCLEAN:
E
Evgeniy Dushistov 已提交
1030
			UFSD("fs is clean\n");
L
Linus Torvalds 已提交
1031 1032
			break;
		case UFS_FSSTABLE:
E
Evgeniy Dushistov 已提交
1033
			UFSD("fs is stable\n");
1034 1035 1036
			break;
		case UFS_FSLOG:
			UFSD("fs is logging fs\n");
L
Linus Torvalds 已提交
1037 1038
			break;
		case UFS_FSOSF1:
E
Evgeniy Dushistov 已提交
1039
			UFSD("fs is DEC OSF/1\n");
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
			break;
		case UFS_FSACTIVE:
			printk("ufs_read_super: fs is active\n");
			sb->s_flags |= MS_RDONLY;
			break;
		case UFS_FSBAD:
			printk("ufs_read_super: fs is bad\n");
			sb->s_flags |= MS_RDONLY;
			break;
		default:
			printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
			sb->s_flags |= MS_RDONLY;
			break;
		}
1054
	} else {
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060 1061 1062
		printk("ufs_read_super: fs needs fsck\n");
		sb->s_flags |= MS_RDONLY;
	}

	/*
	 * Read ufs_super_block into internal data structures
	 */
	sb->s_op = &ufs_super_ops;
A
Alexey Dobriyan 已提交
1063
	sb->s_export_op = &ufs_export_ops;
1064

L
Linus Torvalds 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);

	uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
	uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
	uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
	uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
	uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
	uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);

	if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1075 1076 1077
		uspi->s_u2_size  = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
		uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
	} else {
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
		uspi->s_size  =  fs32_to_cpu(sb, usb1->fs_size);
		uspi->s_dsize =  fs32_to_cpu(sb, usb1->fs_dsize);
	}

	uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
	/* s_bsize already set */
	/* s_fsize already set */
	uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
	uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
	uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
	uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
	uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
	uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
E
Evgeniy Dushistov 已提交
1091 1092
	UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
		uspi->s_fshift);
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
	uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
	/* s_sbsize already set */
	uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
	uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
	uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
	uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
	uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
	uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
	uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
	uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
1104 1105 1106 1107 1108 1109

	if (uspi->fs_magic == UFS2_MAGIC)
		uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
	else
		uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);

L
Linus Torvalds 已提交
1110 1111 1112 1113 1114 1115 1116
	uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
	uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
	uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
	uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
	uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
	uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
	uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
1117 1118
	uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
	uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
L
Linus Torvalds 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
	uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
	uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
	uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
	uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);

	/*
	 * Compute another frequently used values
	 */
	uspi->s_fpbmask = uspi->s_fpb - 1;
E
Evgeniy Dushistov 已提交
1129
	if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
L
Linus Torvalds 已提交
1130
		uspi->s_apbshift = uspi->s_bshift - 3;
E
Evgeniy Dushistov 已提交
1131
	else
L
Linus Torvalds 已提交
1132
		uspi->s_apbshift = uspi->s_bshift - 2;
E
Evgeniy Dushistov 已提交
1133

L
Linus Torvalds 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	uspi->s_2apbshift = uspi->s_apbshift * 2;
	uspi->s_3apbshift = uspi->s_apbshift * 3;
	uspi->s_apb = 1 << uspi->s_apbshift;
	uspi->s_2apb = 1 << uspi->s_2apbshift;
	uspi->s_3apb = 1 << uspi->s_3apbshift;
	uspi->s_apbmask = uspi->s_apb - 1;
	uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
	uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
	uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
	uspi->s_bpf = uspi->s_fsize << 3;
	uspi->s_bpfshift = uspi->s_fshift + 3;
	uspi->s_bpfmask = uspi->s_bpf - 1;
1146 1147
	if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
	    (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
L
Linus Torvalds 已提交
1148
		uspi->s_maxsymlinklen =
1149
		    fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
L
Linus Torvalds 已提交
1150

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	if (uspi->fs_magic == UFS2_MAGIC)
		maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
	else
		maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
	if (uspi->s_maxsymlinklen > maxsymlen) {
		ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
			    "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
		uspi->s_maxsymlinklen = maxsymlen;
	}

1161 1162 1163
	inode = ufs_iget(sb, UFS_ROOTINO);
	if (IS_ERR(inode)) {
		ret = PTR_ERR(inode);
L
Linus Torvalds 已提交
1164
		goto failed;
1165
	}
L
Linus Torvalds 已提交
1166
	sb->s_root = d_alloc_root(inode);
1167 1168
	if (!sb->s_root) {
		ret = -ENOMEM;
L
Linus Torvalds 已提交
1169
		goto dalloc_failed;
1170
	}
L
Linus Torvalds 已提交
1171

1172
	ufs_setup_cstotal(sb);
L
Linus Torvalds 已提交
1173 1174 1175 1176 1177 1178 1179
	/*
	 * Read cylinder group structures
	 */
	if (!(sb->s_flags & MS_RDONLY))
		if (!ufs_read_cylinder_structures(sb))
			goto failed;

E
Evgeniy Dushistov 已提交
1180
	UFSD("EXIT\n");
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185
	return 0;

dalloc_failed:
	iput(inode);
failed:
J
Jesper Juhl 已提交
1186 1187 1188 1189
	if (ubh)
		ubh_brelse_uspi (uspi);
	kfree (uspi);
	kfree(sbi);
L
Linus Torvalds 已提交
1190
	sb->s_fs_info = NULL;
E
Evgeniy Dushistov 已提交
1191
	UFSD("EXIT (FAILED)\n");
1192
	return ret;
L
Linus Torvalds 已提交
1193 1194

failed_nomem:
E
Evgeniy Dushistov 已提交
1195
	UFSD("EXIT (NOMEM)\n");
L
Linus Torvalds 已提交
1196 1197 1198
	return -ENOMEM;
}

C
Christoph Hellwig 已提交
1199
static int ufs_sync_fs(struct super_block *sb, int wait)
1200
{
L
Linus Torvalds 已提交
1201 1202 1203 1204 1205
	struct ufs_sb_private_info * uspi;
	struct ufs_super_block_first * usb1;
	struct ufs_super_block_third * usb3;
	unsigned flags;

A
Arnd Bergmann 已提交
1206
	lock_ufs(sb);
1207
	lock_super(sb);
C
Christoph Hellwig 已提交
1208

E
Evgeniy Dushistov 已提交
1209
	UFSD("ENTER\n");
C
Christoph Hellwig 已提交
1210

L
Linus Torvalds 已提交
1211 1212
	flags = UFS_SB(sb)->s_flags;
	uspi = UFS_SB(sb)->s_uspi;
E
Evgeniy 已提交
1213 1214
	usb1 = ubh_get_usb_first(uspi);
	usb3 = ubh_get_usb_third(uspi);
L
Linus Torvalds 已提交
1215

C
Christoph Hellwig 已提交
1216 1217 1218 1219 1220 1221 1222
	usb1->fs_time = cpu_to_fs32(sb, get_seconds());
	if ((flags & UFS_ST_MASK) == UFS_ST_SUN  ||
	    (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
	    (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
		ufs_set_fs_state(sb, usb1, usb3,
				UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
	ufs_put_cstotal(sb);
L
Linus Torvalds 已提交
1223
	sb->s_dirt = 0;
C
Christoph Hellwig 已提交
1224

E
Evgeniy Dushistov 已提交
1225
	UFSD("EXIT\n");
1226
	unlock_super(sb);
A
Arnd Bergmann 已提交
1227
	unlock_ufs(sb);
C
Christoph Hellwig 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237

	return 0;
}

static void ufs_write_super(struct super_block *sb)
{
	if (!(sb->s_flags & MS_RDONLY))
		ufs_sync_fs(sb, 1);
	else
		sb->s_dirt = 0;
L
Linus Torvalds 已提交
1238 1239
}

E
Evgeniy Dushistov 已提交
1240
static void ufs_put_super(struct super_block *sb)
L
Linus Torvalds 已提交
1241 1242 1243
{
	struct ufs_sb_info * sbi = UFS_SB(sb);
		
E
Evgeniy Dushistov 已提交
1244
	UFSD("ENTER\n");
L
Linus Torvalds 已提交
1245

1246 1247 1248
	if (sb->s_dirt)
		ufs_write_super(sb);

L
Linus Torvalds 已提交
1249
	if (!(sb->s_flags & MS_RDONLY))
1250
		ufs_put_super_internal(sb);
L
Linus Torvalds 已提交
1251 1252 1253 1254 1255
	
	ubh_brelse_uspi (sbi->s_uspi);
	kfree (sbi->s_uspi);
	kfree (sbi);
	sb->s_fs_info = NULL;
1256
	UFSD("EXIT\n");
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	return;
}


static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
{
	struct ufs_sb_private_info * uspi;
	struct ufs_super_block_first * usb1;
	struct ufs_super_block_third * usb3;
	unsigned new_mount_opt, ufstype;
	unsigned flags;
1268

A
Arnd Bergmann 已提交
1269
	lock_ufs(sb);
1270
	lock_super(sb);
L
Linus Torvalds 已提交
1271 1272
	uspi = UFS_SB(sb)->s_uspi;
	flags = UFS_SB(sb)->s_flags;
E
Evgeniy 已提交
1273 1274
	usb1 = ubh_get_usb_first(uspi);
	usb3 = ubh_get_usb_third(uspi);
L
Linus Torvalds 已提交
1275 1276 1277 1278 1279 1280 1281 1282
	
	/*
	 * Allow the "check" option to be passed as a remount option.
	 * It is not possible to change ufstype option during remount
	 */
	ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
	new_mount_opt = 0;
	ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1283 1284
	if (!ufs_parse_options (data, &new_mount_opt)) {
		unlock_super(sb);
A
Arnd Bergmann 已提交
1285
		unlock_ufs(sb);
L
Linus Torvalds 已提交
1286
		return -EINVAL;
1287
	}
L
Linus Torvalds 已提交
1288 1289
	if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
		new_mount_opt |= ufstype;
1290
	} else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
L
Linus Torvalds 已提交
1291
		printk("ufstype can't be changed during remount\n");
1292
		unlock_super(sb);
A
Arnd Bergmann 已提交
1293
		unlock_ufs(sb);
L
Linus Torvalds 已提交
1294 1295 1296 1297 1298
		return -EINVAL;
	}

	if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
		UFS_SB(sb)->s_mount_opt = new_mount_opt;
1299
		unlock_super(sb);
A
Arnd Bergmann 已提交
1300
		unlock_ufs(sb);
L
Linus Torvalds 已提交
1301 1302 1303 1304 1305 1306 1307
		return 0;
	}
	
	/*
	 * fs was mouted as rw, remounting ro
	 */
	if (*mount_flags & MS_RDONLY) {
1308
		ufs_put_super_internal(sb);
L
Linus Torvalds 已提交
1309 1310
		usb1->fs_time = cpu_to_fs32(sb, get_seconds());
		if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1311
		  || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
L
Linus Torvalds 已提交
1312 1313 1314
		  || (flags & UFS_ST_MASK) == UFS_ST_SUNx86) 
			ufs_set_fs_state(sb, usb1, usb3,
				UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
E
Evgeniy Dushistov 已提交
1315
		ubh_mark_buffer_dirty (USPI_UBH(uspi));
L
Linus Torvalds 已提交
1316 1317
		sb->s_dirt = 0;
		sb->s_flags |= MS_RDONLY;
1318
	} else {
L
Linus Torvalds 已提交
1319 1320 1321 1322 1323 1324
	/*
	 * fs was mounted as ro, remounting rw
	 */
#ifndef CONFIG_UFS_FS_WRITE
		printk("ufs was compiled with read-only support, "
		"can't be mounted as read-write\n");
1325
		unlock_super(sb);
A
Arnd Bergmann 已提交
1326
		unlock_ufs(sb);
L
Linus Torvalds 已提交
1327 1328 1329
		return -EINVAL;
#else
		if (ufstype != UFS_MOUNT_UFSTYPE_SUN && 
1330
		    ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
L
Linus Torvalds 已提交
1331
		    ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
1332 1333
		    ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
		    ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
L
Linus Torvalds 已提交
1334
			printk("this ufstype is read-only supported\n");
1335
			unlock_super(sb);
A
Arnd Bergmann 已提交
1336
			unlock_ufs(sb);
L
Linus Torvalds 已提交
1337 1338
			return -EINVAL;
		}
1339
		if (!ufs_read_cylinder_structures(sb)) {
L
Linus Torvalds 已提交
1340
			printk("failed during remounting\n");
1341
			unlock_super(sb);
A
Arnd Bergmann 已提交
1342
			unlock_ufs(sb);
L
Linus Torvalds 已提交
1343 1344 1345 1346 1347 1348
			return -EPERM;
		}
		sb->s_flags &= ~MS_RDONLY;
#endif
	}
	UFS_SB(sb)->s_mount_opt = new_mount_opt;
1349
	unlock_super(sb);
A
Arnd Bergmann 已提交
1350
	unlock_ufs(sb);
L
Linus Torvalds 已提交
1351 1352 1353
	return 0;
}

1354
static int ufs_show_options(struct seq_file *seq, struct dentry *root)
E
Evgeniy Dushistov 已提交
1355
{
1356
	struct ufs_sb_info *sbi = UFS_SB(root->d_sb);
E
Evgeniy Dushistov 已提交
1357
	unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
1358
	const struct match_token *tp = tokens;
E
Evgeniy Dushistov 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373

	while (tp->token != Opt_onerror_panic && tp->token != mval)
		++tp;
	BUG_ON(tp->token == Opt_onerror_panic);
	seq_printf(seq, ",%s", tp->pattern);

	mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
	while (tp->token != Opt_err && tp->token != mval)
		++tp;
	BUG_ON(tp->token == Opt_err);
	seq_printf(seq, ",%s", tp->pattern);

	return 0;
}

1374
static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
1375
{
1376
	struct super_block *sb = dentry->d_sb;
1377 1378 1379 1380 1381
	struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
	unsigned  flags = UFS_SB(sb)->s_flags;
	struct ufs_super_block_first *usb1;
	struct ufs_super_block_second *usb2;
	struct ufs_super_block_third *usb3;
C
Coly Li 已提交
1382
	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
L
Linus Torvalds 已提交
1383

A
Arnd Bergmann 已提交
1384
	lock_ufs(sb);
L
Linus Torvalds 已提交
1385

1386 1387 1388
	usb1 = ubh_get_usb_first(uspi);
	usb2 = ubh_get_usb_second(uspi);
	usb3 = ubh_get_usb_third(uspi);
L
Linus Torvalds 已提交
1389 1390 1391
	
	if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
		buf->f_type = UFS2_MAGIC;
1392 1393
		buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
	} else {
L
Linus Torvalds 已提交
1394 1395 1396
		buf->f_type = UFS_MAGIC;
		buf->f_blocks = uspi->s_dsize;
	}
1397 1398 1399
	buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
		uspi->cs_total.cs_nffree;
	buf->f_ffree = uspi->cs_total.cs_nifree;
L
Linus Torvalds 已提交
1400 1401 1402 1403 1404
	buf->f_bsize = sb->s_blocksize;
	buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
		? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
	buf->f_files = uspi->s_ncg * uspi->s_ipg;
	buf->f_namelen = UFS_MAXNAMLEN;
C
Coly Li 已提交
1405 1406
	buf->f_fsid.val[0] = (u32)id;
	buf->f_fsid.val[1] = (u32)(id >> 32);
L
Linus Torvalds 已提交
1407

A
Arnd Bergmann 已提交
1408
	unlock_ufs(sb);
L
Linus Torvalds 已提交
1409 1410 1411 1412

	return 0;
}

1413
static struct kmem_cache * ufs_inode_cachep;
L
Linus Torvalds 已提交
1414 1415 1416 1417

static struct inode *ufs_alloc_inode(struct super_block *sb)
{
	struct ufs_inode_info *ei;
A
Arnd Bergmann 已提交
1418
	ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424
	if (!ei)
		return NULL;
	ei->vfs_inode.i_version = 1;
	return &ei->vfs_inode;
}

N
Nick Piggin 已提交
1425
static void ufs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
1426
{
N
Nick Piggin 已提交
1427
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
1428 1429 1430
	kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
}

N
Nick Piggin 已提交
1431 1432 1433 1434 1435
static void ufs_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, ufs_i_callback);
}

1436
static void init_once(void *foo)
L
Linus Torvalds 已提交
1437 1438 1439
{
	struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;

C
Christoph Lameter 已提交
1440
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
1441
}
1442

L
Linus Torvalds 已提交
1443 1444 1445 1446
static int init_inodecache(void)
{
	ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
					     sizeof(struct ufs_inode_info),
1447 1448
					     0, (SLAB_RECLAIM_ACCOUNT|
						SLAB_MEM_SPREAD),
1449
					     init_once);
L
Linus Torvalds 已提交
1450 1451 1452 1453 1454 1455 1456
	if (ufs_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
1457
	kmem_cache_destroy(ufs_inode_cachep);
L
Linus Torvalds 已提交
1458 1459
}

1460
static const struct super_operations ufs_super_ops = {
L
Linus Torvalds 已提交
1461 1462 1463
	.alloc_inode	= ufs_alloc_inode,
	.destroy_inode	= ufs_destroy_inode,
	.write_inode	= ufs_write_inode,
A
Al Viro 已提交
1464
	.evict_inode	= ufs_evict_inode,
L
Linus Torvalds 已提交
1465 1466
	.put_super	= ufs_put_super,
	.write_super	= ufs_write_super,
C
Christoph Hellwig 已提交
1467
	.sync_fs	= ufs_sync_fs,
L
Linus Torvalds 已提交
1468 1469
	.statfs		= ufs_statfs,
	.remount_fs	= ufs_remount,
E
Evgeniy Dushistov 已提交
1470
	.show_options   = ufs_show_options,
L
Linus Torvalds 已提交
1471 1472
};

A
Al Viro 已提交
1473 1474
static struct dentry *ufs_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
1475
{
A
Al Viro 已提交
1476
	return mount_bdev(fs_type, flags, dev_name, data, ufs_fill_super);
L
Linus Torvalds 已提交
1477 1478 1479 1480 1481
}

static struct file_system_type ufs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ufs",
A
Al Viro 已提交
1482
	.mount		= ufs_mount,
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};

static int __init init_ufs_fs(void)
{
	int err = init_inodecache();
	if (err)
		goto out1;
	err = register_filesystem(&ufs_fs_type);
	if (err)
		goto out;
	return 0;
out:
	destroy_inodecache();
out1:
	return err;
}

static void __exit exit_ufs_fs(void)
{
	unregister_filesystem(&ufs_fs_type);
	destroy_inodecache();
}

module_init(init_ufs_fs)
module_exit(exit_ufs_fs)
MODULE_LICENSE("GPL");