xref: /linux/fs/affs/super.c (revision fffb60f93ce5880aade88e01d7133b52a4879710)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/affs/inode.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  (C) 1991  Linus Torvalds - minix filesystem
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/statfs.h>
161da177e4SLinus Torvalds #include <linux/parser.h>
171da177e4SLinus Torvalds #include "affs.h"
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds extern struct timezone sys_tz;
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds static int affs_statfs(struct super_block *sb, struct kstatfs *buf);
221da177e4SLinus Torvalds static int affs_remount (struct super_block *sb, int *flags, char *data);
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds static void
251da177e4SLinus Torvalds affs_put_super(struct super_block *sb)
261da177e4SLinus Torvalds {
271da177e4SLinus Torvalds 	struct affs_sb_info *sbi = AFFS_SB(sb);
281da177e4SLinus Torvalds 	pr_debug("AFFS: put_super()\n");
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_RDONLY)) {
311da177e4SLinus Torvalds 		AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->bm_flag = cpu_to_be32(1);
321da177e4SLinus Torvalds 		secs_to_datestamp(get_seconds(),
331da177e4SLinus Torvalds 				  &AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->disk_change);
341da177e4SLinus Torvalds 		affs_fix_checksum(sb, sbi->s_root_bh);
351da177e4SLinus Torvalds 		mark_buffer_dirty(sbi->s_root_bh);
361da177e4SLinus Torvalds 	}
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	kfree(sbi->s_prefix);
391da177e4SLinus Torvalds 	affs_free_bitmap(sb);
401da177e4SLinus Torvalds 	affs_brelse(sbi->s_root_bh);
411da177e4SLinus Torvalds 	kfree(sbi);
421da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
431da177e4SLinus Torvalds 	return;
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static void
471da177e4SLinus Torvalds affs_write_super(struct super_block *sb)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	int clean = 2;
501da177e4SLinus Torvalds 	struct affs_sb_info *sbi = AFFS_SB(sb);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_RDONLY)) {
531da177e4SLinus Torvalds 		//	if (sbi->s_bitmap[i].bm_bh) {
541da177e4SLinus Torvalds 		//		if (buffer_dirty(sbi->s_bitmap[i].bm_bh)) {
551da177e4SLinus Torvalds 		//			clean = 0;
561da177e4SLinus Torvalds 		AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->bm_flag = cpu_to_be32(clean);
571da177e4SLinus Torvalds 		secs_to_datestamp(get_seconds(),
581da177e4SLinus Torvalds 				  &AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->disk_change);
591da177e4SLinus Torvalds 		affs_fix_checksum(sb, sbi->s_root_bh);
601da177e4SLinus Torvalds 		mark_buffer_dirty(sbi->s_root_bh);
611da177e4SLinus Torvalds 		sb->s_dirt = !clean;	/* redo until bitmap synced */
621da177e4SLinus Torvalds 	} else
631da177e4SLinus Torvalds 		sb->s_dirt = 0;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	pr_debug("AFFS: write_super() at %lu, clean=%d\n", get_seconds(), clean);
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds static kmem_cache_t * affs_inode_cachep;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static struct inode *affs_alloc_inode(struct super_block *sb)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct affs_inode_info *ei;
731da177e4SLinus Torvalds 	ei = (struct affs_inode_info *)kmem_cache_alloc(affs_inode_cachep, SLAB_KERNEL);
741da177e4SLinus Torvalds 	if (!ei)
751da177e4SLinus Torvalds 		return NULL;
761da177e4SLinus Torvalds 	ei->vfs_inode.i_version = 1;
771da177e4SLinus Torvalds 	return &ei->vfs_inode;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static void affs_destroy_inode(struct inode *inode)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	struct affs_inode_info *ei = (struct affs_inode_info *) foo;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
901da177e4SLinus Torvalds 	    SLAB_CTOR_CONSTRUCTOR) {
911da177e4SLinus Torvalds 		init_MUTEX(&ei->i_link_lock);
921da177e4SLinus Torvalds 		init_MUTEX(&ei->i_ext_lock);
931da177e4SLinus Torvalds 		inode_init_once(&ei->vfs_inode);
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds static int init_inodecache(void)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	affs_inode_cachep = kmem_cache_create("affs_inode_cache",
1001da177e4SLinus Torvalds 					     sizeof(struct affs_inode_info),
101*fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
102*fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
1031da177e4SLinus Torvalds 					     init_once, NULL);
1041da177e4SLinus Torvalds 	if (affs_inode_cachep == NULL)
1051da177e4SLinus Torvalds 		return -ENOMEM;
1061da177e4SLinus Torvalds 	return 0;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds static void destroy_inodecache(void)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	if (kmem_cache_destroy(affs_inode_cachep))
1121da177e4SLinus Torvalds 		printk(KERN_INFO "affs_inode_cache: not all structures were freed\n");
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static struct super_operations affs_sops = {
1161da177e4SLinus Torvalds 	.alloc_inode	= affs_alloc_inode,
1171da177e4SLinus Torvalds 	.destroy_inode	= affs_destroy_inode,
1181da177e4SLinus Torvalds 	.read_inode	= affs_read_inode,
1191da177e4SLinus Torvalds 	.write_inode	= affs_write_inode,
1201da177e4SLinus Torvalds 	.put_inode	= affs_put_inode,
1211da177e4SLinus Torvalds 	.delete_inode	= affs_delete_inode,
1221da177e4SLinus Torvalds 	.clear_inode	= affs_clear_inode,
1231da177e4SLinus Torvalds 	.put_super	= affs_put_super,
1241da177e4SLinus Torvalds 	.write_super	= affs_write_super,
1251da177e4SLinus Torvalds 	.statfs		= affs_statfs,
1261da177e4SLinus Torvalds 	.remount_fs	= affs_remount,
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds enum {
1301da177e4SLinus Torvalds 	Opt_bs, Opt_mode, Opt_mufs, Opt_prefix, Opt_protect,
1311da177e4SLinus Torvalds 	Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
1321da177e4SLinus Torvalds 	Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
1331da177e4SLinus Torvalds };
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static match_table_t tokens = {
1361da177e4SLinus Torvalds 	{Opt_bs, "bs=%u"},
1371da177e4SLinus Torvalds 	{Opt_mode, "mode=%o"},
1381da177e4SLinus Torvalds 	{Opt_mufs, "mufs"},
1391da177e4SLinus Torvalds 	{Opt_prefix, "prefix=%s"},
1401da177e4SLinus Torvalds 	{Opt_protect, "protect"},
1411da177e4SLinus Torvalds 	{Opt_reserved, "reserved=%u"},
1421da177e4SLinus Torvalds 	{Opt_root, "root=%u"},
1431da177e4SLinus Torvalds 	{Opt_setgid, "setgid=%u"},
1441da177e4SLinus Torvalds 	{Opt_setuid, "setuid=%u"},
1451da177e4SLinus Torvalds 	{Opt_verbose, "verbose"},
1461da177e4SLinus Torvalds 	{Opt_volume, "volume=%s"},
1471da177e4SLinus Torvalds 	{Opt_ignore, "grpquota"},
1481da177e4SLinus Torvalds 	{Opt_ignore, "noquota"},
1491da177e4SLinus Torvalds 	{Opt_ignore, "quota"},
1501da177e4SLinus Torvalds 	{Opt_ignore, "usrquota"},
1511da177e4SLinus Torvalds 	{Opt_err, NULL},
1521da177e4SLinus Torvalds };
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static int
1551da177e4SLinus Torvalds parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
1561da177e4SLinus Torvalds 		int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	char *p;
1591da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* Fill in defaults */
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	*uid        = current->uid;
1641da177e4SLinus Torvalds 	*gid        = current->gid;
1651da177e4SLinus Torvalds 	*reserved   = 2;
1661da177e4SLinus Torvalds 	*root       = -1;
1671da177e4SLinus Torvalds 	*blocksize  = -1;
1681da177e4SLinus Torvalds 	volume[0]   = ':';
1691da177e4SLinus Torvalds 	volume[1]   = 0;
1701da177e4SLinus Torvalds 	*mount_opts = 0;
1711da177e4SLinus Torvalds 	if (!options)
1721da177e4SLinus Torvalds 		return 1;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
1751da177e4SLinus Torvalds 		int token, n, option;
1761da177e4SLinus Torvalds 		if (!*p)
1771da177e4SLinus Torvalds 			continue;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 		token = match_token(p, tokens, args);
1801da177e4SLinus Torvalds 		switch (token) {
1811da177e4SLinus Torvalds 		case Opt_bs:
1821da177e4SLinus Torvalds 			if (match_int(&args[0], &n))
1831da177e4SLinus Torvalds 				return -EINVAL;
1841da177e4SLinus Torvalds 			if (n != 512 && n != 1024 && n != 2048
1851da177e4SLinus Torvalds 			    && n != 4096) {
1861da177e4SLinus Torvalds 				printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
1871da177e4SLinus Torvalds 				return 0;
1881da177e4SLinus Torvalds 			}
1891da177e4SLinus Torvalds 			*blocksize = n;
1901da177e4SLinus Torvalds 			break;
1911da177e4SLinus Torvalds 		case Opt_mode:
1921da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
1931da177e4SLinus Torvalds 				return 1;
1941da177e4SLinus Torvalds 			*mode = option & 0777;
1951da177e4SLinus Torvalds 			*mount_opts |= SF_SETMODE;
1961da177e4SLinus Torvalds 			break;
1971da177e4SLinus Torvalds 		case Opt_mufs:
1981da177e4SLinus Torvalds 			*mount_opts |= SF_MUFS;
1991da177e4SLinus Torvalds 			break;
2001da177e4SLinus Torvalds 		case Opt_prefix:
201f99d49adSJesper Juhl 			/* Free any previous prefix */
2021da177e4SLinus Torvalds 			kfree(*prefix);
2031da177e4SLinus Torvalds 			*prefix = NULL;
2041da177e4SLinus Torvalds 			*prefix = match_strdup(&args[0]);
2051da177e4SLinus Torvalds 			if (!*prefix)
2061da177e4SLinus Torvalds 				return 0;
2071da177e4SLinus Torvalds 			*mount_opts |= SF_PREFIX;
2081da177e4SLinus Torvalds 			break;
2091da177e4SLinus Torvalds 		case Opt_protect:
2101da177e4SLinus Torvalds 			*mount_opts |= SF_IMMUTABLE;
2111da177e4SLinus Torvalds 			break;
2121da177e4SLinus Torvalds 		case Opt_reserved:
2131da177e4SLinus Torvalds 			if (match_int(&args[0], reserved))
2141da177e4SLinus Torvalds 				return 1;
2151da177e4SLinus Torvalds 			break;
2161da177e4SLinus Torvalds 		case Opt_root:
2171da177e4SLinus Torvalds 			if (match_int(&args[0], root))
2181da177e4SLinus Torvalds 				return 1;
2191da177e4SLinus Torvalds 			break;
2201da177e4SLinus Torvalds 		case Opt_setgid:
2211da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
2221da177e4SLinus Torvalds 				return 1;
2231da177e4SLinus Torvalds 			*gid = option;
2241da177e4SLinus Torvalds 			*mount_opts |= SF_SETGID;
2251da177e4SLinus Torvalds 			break;
2261da177e4SLinus Torvalds 		case Opt_setuid:
2271da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
2281da177e4SLinus Torvalds 				return -EINVAL;
2291da177e4SLinus Torvalds 			*uid = option;
2301da177e4SLinus Torvalds 			*mount_opts |= SF_SETUID;
2311da177e4SLinus Torvalds 			break;
2321da177e4SLinus Torvalds 		case Opt_verbose:
2331da177e4SLinus Torvalds 			*mount_opts |= SF_VERBOSE;
2341da177e4SLinus Torvalds 			break;
2351da177e4SLinus Torvalds 		case Opt_volume: {
2361da177e4SLinus Torvalds 			char *vol = match_strdup(&args[0]);
2371da177e4SLinus Torvalds 			strlcpy(volume, vol, 32);
2381da177e4SLinus Torvalds 			kfree(vol);
2391da177e4SLinus Torvalds 			break;
2401da177e4SLinus Torvalds 		}
2411da177e4SLinus Torvalds 		case Opt_ignore:
2421da177e4SLinus Torvalds 		 	/* Silently ignore the quota options */
2431da177e4SLinus Torvalds 			break;
2441da177e4SLinus Torvalds 		default:
2451da177e4SLinus Torvalds 			printk("AFFS: Unrecognized mount option \"%s\" "
2461da177e4SLinus Torvalds 					"or missing value\n", p);
2471da177e4SLinus Torvalds 			return 0;
2481da177e4SLinus Torvalds 		}
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 	return 1;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /* This function definitely needs to be split up. Some fine day I'll
2541da177e4SLinus Torvalds  * hopefully have the guts to do so. Until then: sorry for the mess.
2551da177e4SLinus Torvalds  */
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static int affs_fill_super(struct super_block *sb, void *data, int silent)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct affs_sb_info	*sbi;
2601da177e4SLinus Torvalds 	struct buffer_head	*root_bh = NULL;
2611da177e4SLinus Torvalds 	struct buffer_head	*boot_bh;
2621da177e4SLinus Torvalds 	struct inode		*root_inode = NULL;
2631da177e4SLinus Torvalds 	s32			 root_block;
2641da177e4SLinus Torvalds 	int			 size, blocksize;
2651da177e4SLinus Torvalds 	u32			 chksum;
2661da177e4SLinus Torvalds 	int			 num_bm;
2671da177e4SLinus Torvalds 	int			 i, j;
2681da177e4SLinus Torvalds 	s32			 key;
2691da177e4SLinus Torvalds 	uid_t			 uid;
2701da177e4SLinus Torvalds 	gid_t			 gid;
2711da177e4SLinus Torvalds 	int			 reserved;
2721da177e4SLinus Torvalds 	unsigned long		 mount_flags;
2731da177e4SLinus Torvalds 	int			 tmp_flags;	/* fix remount prototype... */
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	sb->s_magic             = AFFS_SUPER_MAGIC;
2781da177e4SLinus Torvalds 	sb->s_op                = &affs_sops;
2791da177e4SLinus Torvalds 	sb->s_flags |= MS_NODIRATIME;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	sbi = kmalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
2821da177e4SLinus Torvalds 	if (!sbi)
2831da177e4SLinus Torvalds 		return -ENOMEM;
2841da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
2851da177e4SLinus Torvalds 	memset(sbi, 0, sizeof(*sbi));
2861da177e4SLinus Torvalds 	init_MUTEX(&sbi->s_bmlock);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
2891da177e4SLinus Torvalds 				&blocksize,&sbi->s_prefix,
2901da177e4SLinus Torvalds 				sbi->s_volume, &mount_flags)) {
2911da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Error parsing options\n");
2921da177e4SLinus Torvalds 		return -EINVAL;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 	/* N.B. after this point s_prefix must be released */
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	sbi->s_flags   = mount_flags;
2971da177e4SLinus Torvalds 	sbi->s_mode    = i;
2981da177e4SLinus Torvalds 	sbi->s_uid     = uid;
2991da177e4SLinus Torvalds 	sbi->s_gid     = gid;
3001da177e4SLinus Torvalds 	sbi->s_reserved= reserved;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	/* Get the size of the device in 512-byte blocks.
3031da177e4SLinus Torvalds 	 * If we later see that the partition uses bigger
3041da177e4SLinus Torvalds 	 * blocks, we will have to change it.
3051da177e4SLinus Torvalds 	 */
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	size = sb->s_bdev->bd_inode->i_size >> 9;
3081da177e4SLinus Torvalds 	pr_debug("AFFS: initial blocksize=%d, #blocks=%d\n", 512, size);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	affs_set_blocksize(sb, PAGE_SIZE);
3111da177e4SLinus Torvalds 	/* Try to find root block. Its location depends on the block size. */
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	i = 512;
3141da177e4SLinus Torvalds 	j = 4096;
3151da177e4SLinus Torvalds 	if (blocksize > 0) {
3161da177e4SLinus Torvalds 		i = j = blocksize;
3171da177e4SLinus Torvalds 		size = size / (blocksize / 512);
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
3201da177e4SLinus Torvalds 		sbi->s_root_block = root_block;
3211da177e4SLinus Torvalds 		if (root_block < 0)
3221da177e4SLinus Torvalds 			sbi->s_root_block = (reserved + size - 1) / 2;
3231da177e4SLinus Torvalds 		pr_debug("AFFS: setting blocksize to %d\n", blocksize);
3241da177e4SLinus Torvalds 		affs_set_blocksize(sb, blocksize);
3251da177e4SLinus Torvalds 		sbi->s_partition_size = size;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 		/* The root block location that was calculated above is not
3281da177e4SLinus Torvalds 		 * correct if the partition size is an odd number of 512-
3291da177e4SLinus Torvalds 		 * byte blocks, which will be rounded down to a number of
3301da177e4SLinus Torvalds 		 * 1024-byte blocks, and if there were an even number of
3311da177e4SLinus Torvalds 		 * reserved blocks. Ideally, all partition checkers should
3321da177e4SLinus Torvalds 		 * report the real number of blocks of the real blocksize,
3331da177e4SLinus Torvalds 		 * but since this just cannot be done, we have to try to
3341da177e4SLinus Torvalds 		 * find the root block anyways. In the above case, it is one
3351da177e4SLinus Torvalds 		 * block behind the calculated one. So we check this one, too.
3361da177e4SLinus Torvalds 		 */
3371da177e4SLinus Torvalds 		for (num_bm = 0; num_bm < 2; num_bm++) {
3381da177e4SLinus Torvalds 			pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
3391da177e4SLinus Torvalds 				"size=%d, reserved=%d\n",
3401da177e4SLinus Torvalds 				sb->s_id,
3411da177e4SLinus Torvalds 				sbi->s_root_block + num_bm,
3421da177e4SLinus Torvalds 				blocksize, size, reserved);
3431da177e4SLinus Torvalds 			root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
3441da177e4SLinus Torvalds 			if (!root_bh)
3451da177e4SLinus Torvalds 				continue;
3461da177e4SLinus Torvalds 			if (!affs_checksum_block(sb, root_bh) &&
3471da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
3481da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
3491da177e4SLinus Torvalds 				sbi->s_hashsize    = blocksize / 4 - 56;
3501da177e4SLinus Torvalds 				sbi->s_root_block += num_bm;
3511da177e4SLinus Torvalds 				key                        = 1;
3521da177e4SLinus Torvalds 				goto got_root;
3531da177e4SLinus Torvalds 			}
3541da177e4SLinus Torvalds 			affs_brelse(root_bh);
3551da177e4SLinus Torvalds 			root_bh = NULL;
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 	if (!silent)
3591da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: No valid root block on device %s\n",
3601da177e4SLinus Torvalds 			sb->s_id);
3611da177e4SLinus Torvalds 	goto out_error;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	/* N.B. after this point bh must be released */
3641da177e4SLinus Torvalds got_root:
3651da177e4SLinus Torvalds 	root_block = sbi->s_root_block;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	/* Find out which kind of FS we have */
3681da177e4SLinus Torvalds 	boot_bh = sb_bread(sb, 0);
3691da177e4SLinus Torvalds 	if (!boot_bh) {
3701da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Cannot read boot block\n");
3711da177e4SLinus Torvalds 		goto out_error;
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 	chksum = be32_to_cpu(*(__be32 *)boot_bh->b_data);
3741da177e4SLinus Torvalds 	brelse(boot_bh);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	/* Dircache filesystems are compatible with non-dircache ones
3771da177e4SLinus Torvalds 	 * when reading. As long as they aren't supported, writing is
3781da177e4SLinus Torvalds 	 * not recommended.
3791da177e4SLinus Torvalds 	 */
3801da177e4SLinus Torvalds 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
3811da177e4SLinus Torvalds 	     || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
3821da177e4SLinus Torvalds 		printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
3831da177e4SLinus Torvalds 			sb->s_id);
3841da177e4SLinus Torvalds 		sb->s_flags |= MS_RDONLY;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 	switch (chksum) {
3871da177e4SLinus Torvalds 		case MUFS_FS:
3881da177e4SLinus Torvalds 		case MUFS_INTLFFS:
3891da177e4SLinus Torvalds 		case MUFS_DCFFS:
3901da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
3911da177e4SLinus Torvalds 			/* fall thru */
3921da177e4SLinus Torvalds 		case FS_INTLFFS:
3931da177e4SLinus Torvalds 		case FS_DCFFS:
3941da177e4SLinus Torvalds 			sbi->s_flags |= SF_INTL;
3951da177e4SLinus Torvalds 			break;
3961da177e4SLinus Torvalds 		case MUFS_FFS:
3971da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
3981da177e4SLinus Torvalds 			break;
3991da177e4SLinus Torvalds 		case FS_FFS:
4001da177e4SLinus Torvalds 			break;
4011da177e4SLinus Torvalds 		case MUFS_OFS:
4021da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4031da177e4SLinus Torvalds 			/* fall thru */
4041da177e4SLinus Torvalds 		case FS_OFS:
4051da177e4SLinus Torvalds 			sbi->s_flags |= SF_OFS;
4061da177e4SLinus Torvalds 			sb->s_flags |= MS_NOEXEC;
4071da177e4SLinus Torvalds 			break;
4081da177e4SLinus Torvalds 		case MUFS_DCOFS:
4091da177e4SLinus Torvalds 		case MUFS_INTLOFS:
4101da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4111da177e4SLinus Torvalds 		case FS_DCOFS:
4121da177e4SLinus Torvalds 		case FS_INTLOFS:
4131da177e4SLinus Torvalds 			sbi->s_flags |= SF_INTL | SF_OFS;
4141da177e4SLinus Torvalds 			sb->s_flags |= MS_NOEXEC;
4151da177e4SLinus Torvalds 			break;
4161da177e4SLinus Torvalds 		default:
4171da177e4SLinus Torvalds 			printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
4181da177e4SLinus Torvalds 				sb->s_id, chksum);
4191da177e4SLinus Torvalds 			goto out_error;
4201da177e4SLinus Torvalds 	}
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	if (mount_flags & SF_VERBOSE) {
4231da177e4SLinus Torvalds 		chksum = cpu_to_be32(chksum);
4241da177e4SLinus Torvalds 		printk(KERN_NOTICE "AFFS: Mounting volume \"%*s\": Type=%.3s\\%c, Blocksize=%d\n",
4251da177e4SLinus Torvalds 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0],
4261da177e4SLinus Torvalds 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
4271da177e4SLinus Torvalds 			(char *)&chksum,((char *)&chksum)[3] + '0',blocksize);
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	sb->s_flags |= MS_NODEV | MS_NOSUID;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	sbi->s_data_blksize = sb->s_blocksize;
4331da177e4SLinus Torvalds 	if (sbi->s_flags & SF_OFS)
4341da177e4SLinus Torvalds 		sbi->s_data_blksize -= 24;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/* Keep super block in cache */
4371da177e4SLinus Torvalds 	sbi->s_root_bh = root_bh;
4381da177e4SLinus Torvalds 	/* N.B. after this point s_root_bh must be released */
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	tmp_flags = sb->s_flags;
4411da177e4SLinus Torvalds 	if (affs_init_bitmap(sb, &tmp_flags))
4421da177e4SLinus Torvalds 		goto out_error;
4431da177e4SLinus Torvalds 	sb->s_flags = tmp_flags;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	root_inode = iget(sb, root_block);
4481da177e4SLinus Torvalds 	sb->s_root = d_alloc_root(root_inode);
4491da177e4SLinus Torvalds 	if (!sb->s_root) {
4501da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Get root inode failed\n");
4511da177e4SLinus Torvalds 		goto out_error;
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds 	sb->s_root->d_op = &affs_dentry_operations;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
4561da177e4SLinus Torvalds 	return 0;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	/*
4591da177e4SLinus Torvalds 	 * Begin the cascaded cleanup ...
4601da177e4SLinus Torvalds 	 */
4611da177e4SLinus Torvalds out_error:
4621da177e4SLinus Torvalds 	if (root_inode)
4631da177e4SLinus Torvalds 		iput(root_inode);
4641da177e4SLinus Torvalds 	kfree(sbi->s_bitmap);
4651da177e4SLinus Torvalds 	affs_brelse(root_bh);
4661da177e4SLinus Torvalds 	kfree(sbi->s_prefix);
4671da177e4SLinus Torvalds 	kfree(sbi);
4681da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
4691da177e4SLinus Torvalds 	return -EINVAL;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds static int
4731da177e4SLinus Torvalds affs_remount(struct super_block *sb, int *flags, char *data)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
4761da177e4SLinus Torvalds 	int			 blocksize;
4771da177e4SLinus Torvalds 	uid_t			 uid;
4781da177e4SLinus Torvalds 	gid_t			 gid;
4791da177e4SLinus Torvalds 	int			 mode;
4801da177e4SLinus Torvalds 	int			 reserved;
4811da177e4SLinus Torvalds 	int			 root_block;
4821da177e4SLinus Torvalds 	unsigned long		 mount_flags;
4831da177e4SLinus Torvalds 	int			 res = 0;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	*flags |= MS_NODIRATIME;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (!parse_options(data,&uid,&gid,&mode,&reserved,&root_block,
4901da177e4SLinus Torvalds 	    &blocksize,&sbi->s_prefix,sbi->s_volume,&mount_flags))
4911da177e4SLinus Torvalds 		return -EINVAL;
4921da177e4SLinus Torvalds 	sbi->s_flags = mount_flags;
4931da177e4SLinus Torvalds 	sbi->s_mode  = mode;
4941da177e4SLinus Torvalds 	sbi->s_uid   = uid;
4951da177e4SLinus Torvalds 	sbi->s_gid   = gid;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
4981da177e4SLinus Torvalds 		return 0;
4991da177e4SLinus Torvalds 	if (*flags & MS_RDONLY) {
5001da177e4SLinus Torvalds 		sb->s_dirt = 1;
5011da177e4SLinus Torvalds 		while (sb->s_dirt)
5021da177e4SLinus Torvalds 			affs_write_super(sb);
5031da177e4SLinus Torvalds 		affs_free_bitmap(sb);
5041da177e4SLinus Torvalds 	} else
5051da177e4SLinus Torvalds 		res = affs_init_bitmap(sb, flags);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	return res;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static int
5111da177e4SLinus Torvalds affs_statfs(struct super_block *sb, struct kstatfs *buf)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	int		 free;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB(sb)->s_partition_size,
5161da177e4SLinus Torvalds 	     AFFS_SB(sb)->s_reserved);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	free          = affs_count_free_blocks(sb);
5191da177e4SLinus Torvalds 	buf->f_type    = AFFS_SUPER_MAGIC;
5201da177e4SLinus Torvalds 	buf->f_bsize   = sb->s_blocksize;
5211da177e4SLinus Torvalds 	buf->f_blocks  = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
5221da177e4SLinus Torvalds 	buf->f_bfree   = free;
5231da177e4SLinus Torvalds 	buf->f_bavail  = free;
5241da177e4SLinus Torvalds 	return 0;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds static struct super_block *affs_get_sb(struct file_system_type *fs_type,
5281da177e4SLinus Torvalds 	int flags, const char *dev_name, void *data)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	return get_sb_bdev(fs_type, flags, dev_name, data, affs_fill_super);
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static struct file_system_type affs_fs_type = {
5341da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
5351da177e4SLinus Torvalds 	.name		= "affs",
5361da177e4SLinus Torvalds 	.get_sb		= affs_get_sb,
5371da177e4SLinus Torvalds 	.kill_sb	= kill_block_super,
5381da177e4SLinus Torvalds 	.fs_flags	= FS_REQUIRES_DEV,
5391da177e4SLinus Torvalds };
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds static int __init init_affs_fs(void)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	int err = init_inodecache();
5441da177e4SLinus Torvalds 	if (err)
5451da177e4SLinus Torvalds 		goto out1;
5461da177e4SLinus Torvalds 	err = register_filesystem(&affs_fs_type);
5471da177e4SLinus Torvalds 	if (err)
5481da177e4SLinus Torvalds 		goto out;
5491da177e4SLinus Torvalds 	return 0;
5501da177e4SLinus Torvalds out:
5511da177e4SLinus Torvalds 	destroy_inodecache();
5521da177e4SLinus Torvalds out1:
5531da177e4SLinus Torvalds 	return err;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds static void __exit exit_affs_fs(void)
5571da177e4SLinus Torvalds {
5581da177e4SLinus Torvalds 	unregister_filesystem(&affs_fs_type);
5591da177e4SLinus Torvalds 	destroy_inodecache();
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds MODULE_DESCRIPTION("Amiga filesystem support for Linux");
5631da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds module_init(init_affs_fs)
5661da177e4SLinus Torvalds module_exit(exit_affs_fs)
567