xref: /linux/fs/affs/super.c (revision 2a32cebd6cbcc43996c3e2d114fa32ba1e71192a)
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>
17e18fa700SJeff Garzik #include <linux/magic.h>
18e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
191da177e4SLinus Torvalds #include "affs.h"
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds extern struct timezone sys_tz;
221da177e4SLinus Torvalds 
23726c3342SDavid Howells static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
241da177e4SLinus Torvalds static int affs_remount (struct super_block *sb, int *flags, char *data);
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static void
271da177e4SLinus Torvalds affs_put_super(struct super_block *sb)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	struct affs_sb_info *sbi = AFFS_SB(sb);
301da177e4SLinus Torvalds 	pr_debug("AFFS: put_super()\n");
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_RDONLY)) {
331da177e4SLinus Torvalds 		AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->bm_flag = cpu_to_be32(1);
341da177e4SLinus Torvalds 		secs_to_datestamp(get_seconds(),
351da177e4SLinus Torvalds 				  &AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->disk_change);
361da177e4SLinus Torvalds 		affs_fix_checksum(sb, sbi->s_root_bh);
371da177e4SLinus Torvalds 		mark_buffer_dirty(sbi->s_root_bh);
381da177e4SLinus Torvalds 	}
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	kfree(sbi->s_prefix);
411da177e4SLinus Torvalds 	affs_free_bitmap(sb);
421da177e4SLinus Torvalds 	affs_brelse(sbi->s_root_bh);
431da177e4SLinus Torvalds 	kfree(sbi);
441da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
451da177e4SLinus Torvalds 	return;
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static void
491da177e4SLinus Torvalds affs_write_super(struct super_block *sb)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	int clean = 2;
521da177e4SLinus Torvalds 	struct affs_sb_info *sbi = AFFS_SB(sb);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	if (!(sb->s_flags & MS_RDONLY)) {
551da177e4SLinus Torvalds 		//	if (sbi->s_bitmap[i].bm_bh) {
561da177e4SLinus Torvalds 		//		if (buffer_dirty(sbi->s_bitmap[i].bm_bh)) {
571da177e4SLinus Torvalds 		//			clean = 0;
581da177e4SLinus Torvalds 		AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->bm_flag = cpu_to_be32(clean);
591da177e4SLinus Torvalds 		secs_to_datestamp(get_seconds(),
601da177e4SLinus Torvalds 				  &AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->disk_change);
611da177e4SLinus Torvalds 		affs_fix_checksum(sb, sbi->s_root_bh);
621da177e4SLinus Torvalds 		mark_buffer_dirty(sbi->s_root_bh);
631da177e4SLinus Torvalds 		sb->s_dirt = !clean;	/* redo until bitmap synced */
641da177e4SLinus Torvalds 	} else
651da177e4SLinus Torvalds 		sb->s_dirt = 0;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	pr_debug("AFFS: write_super() at %lu, clean=%d\n", get_seconds(), clean);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
70e18b890bSChristoph Lameter static struct kmem_cache * affs_inode_cachep;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static struct inode *affs_alloc_inode(struct super_block *sb)
731da177e4SLinus Torvalds {
74dca3c336SRoman Zippel 	struct affs_inode_info *i;
75dca3c336SRoman Zippel 
76dca3c336SRoman Zippel 	i = kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
77dca3c336SRoman Zippel 	if (!i)
781da177e4SLinus Torvalds 		return NULL;
79dca3c336SRoman Zippel 
80dca3c336SRoman Zippel 	i->vfs_inode.i_version = 1;
81dca3c336SRoman Zippel 	i->i_lc = NULL;
82dca3c336SRoman Zippel 	i->i_ext_bh = NULL;
83dca3c336SRoman Zippel 	i->i_pa_cnt = 0;
84dca3c336SRoman Zippel 
85dca3c336SRoman Zippel 	return &i->vfs_inode;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static void affs_destroy_inode(struct inode *inode)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
9351cc5068SAlexey Dobriyan static void init_once(void *foo)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	struct affs_inode_info *ei = (struct affs_inode_info *) foo;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	init_MUTEX(&ei->i_link_lock);
981da177e4SLinus Torvalds 	init_MUTEX(&ei->i_ext_lock);
991da177e4SLinus Torvalds 	inode_init_once(&ei->vfs_inode);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static int init_inodecache(void)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	affs_inode_cachep = kmem_cache_create("affs_inode_cache",
1051da177e4SLinus Torvalds 					     sizeof(struct affs_inode_info),
106fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
107fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
10820c2df83SPaul Mundt 					     init_once);
1091da177e4SLinus Torvalds 	if (affs_inode_cachep == NULL)
1101da177e4SLinus Torvalds 		return -ENOMEM;
1111da177e4SLinus Torvalds 	return 0;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static void destroy_inodecache(void)
1151da177e4SLinus Torvalds {
1161a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(affs_inode_cachep);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
119ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations affs_sops = {
1201da177e4SLinus Torvalds 	.alloc_inode	= affs_alloc_inode,
1211da177e4SLinus Torvalds 	.destroy_inode	= affs_destroy_inode,
1221da177e4SLinus Torvalds 	.write_inode	= affs_write_inode,
1231da177e4SLinus Torvalds 	.delete_inode	= affs_delete_inode,
1241da177e4SLinus Torvalds 	.clear_inode	= affs_clear_inode,
1251da177e4SLinus Torvalds 	.put_super	= affs_put_super,
1261da177e4SLinus Torvalds 	.write_super	= affs_write_super,
1271da177e4SLinus Torvalds 	.statfs		= affs_statfs,
1281da177e4SLinus Torvalds 	.remount_fs	= affs_remount,
129e9b3961bSMiklos Szeredi 	.show_options	= generic_show_options,
1301da177e4SLinus Torvalds };
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds enum {
1331da177e4SLinus Torvalds 	Opt_bs, Opt_mode, Opt_mufs, Opt_prefix, Opt_protect,
1341da177e4SLinus Torvalds 	Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
1351da177e4SLinus Torvalds 	Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
1361da177e4SLinus Torvalds };
1371da177e4SLinus Torvalds 
138a447c093SSteven Whitehouse static const match_table_t tokens = {
1391da177e4SLinus Torvalds 	{Opt_bs, "bs=%u"},
1401da177e4SLinus Torvalds 	{Opt_mode, "mode=%o"},
1411da177e4SLinus Torvalds 	{Opt_mufs, "mufs"},
1421da177e4SLinus Torvalds 	{Opt_prefix, "prefix=%s"},
1431da177e4SLinus Torvalds 	{Opt_protect, "protect"},
1441da177e4SLinus Torvalds 	{Opt_reserved, "reserved=%u"},
1451da177e4SLinus Torvalds 	{Opt_root, "root=%u"},
1461da177e4SLinus Torvalds 	{Opt_setgid, "setgid=%u"},
1471da177e4SLinus Torvalds 	{Opt_setuid, "setuid=%u"},
1481da177e4SLinus Torvalds 	{Opt_verbose, "verbose"},
1491da177e4SLinus Torvalds 	{Opt_volume, "volume=%s"},
1501da177e4SLinus Torvalds 	{Opt_ignore, "grpquota"},
1511da177e4SLinus Torvalds 	{Opt_ignore, "noquota"},
1521da177e4SLinus Torvalds 	{Opt_ignore, "quota"},
1531da177e4SLinus Torvalds 	{Opt_ignore, "usrquota"},
1541da177e4SLinus Torvalds 	{Opt_err, NULL},
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static int
1581da177e4SLinus Torvalds parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
1591da177e4SLinus Torvalds 		int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	char *p;
1621da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/* Fill in defaults */
1651da177e4SLinus Torvalds 
16621559981SDavid Howells 	*uid        = current_uid();
16721559981SDavid Howells 	*gid        = current_gid();
1681da177e4SLinus Torvalds 	*reserved   = 2;
1691da177e4SLinus Torvalds 	*root       = -1;
1701da177e4SLinus Torvalds 	*blocksize  = -1;
1711da177e4SLinus Torvalds 	volume[0]   = ':';
1721da177e4SLinus Torvalds 	volume[1]   = 0;
1731da177e4SLinus Torvalds 	*mount_opts = 0;
1741da177e4SLinus Torvalds 	if (!options)
1751da177e4SLinus Torvalds 		return 1;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
1781da177e4SLinus Torvalds 		int token, n, option;
1791da177e4SLinus Torvalds 		if (!*p)
1801da177e4SLinus Torvalds 			continue;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 		token = match_token(p, tokens, args);
1831da177e4SLinus Torvalds 		switch (token) {
1841da177e4SLinus Torvalds 		case Opt_bs:
1851da177e4SLinus Torvalds 			if (match_int(&args[0], &n))
1861da177e4SLinus Torvalds 				return -EINVAL;
1871da177e4SLinus Torvalds 			if (n != 512 && n != 1024 && n != 2048
1881da177e4SLinus Torvalds 			    && n != 4096) {
1891da177e4SLinus Torvalds 				printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
1901da177e4SLinus Torvalds 				return 0;
1911da177e4SLinus Torvalds 			}
1921da177e4SLinus Torvalds 			*blocksize = n;
1931da177e4SLinus Torvalds 			break;
1941da177e4SLinus Torvalds 		case Opt_mode:
1951da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
1961da177e4SLinus Torvalds 				return 1;
1971da177e4SLinus Torvalds 			*mode = option & 0777;
1981da177e4SLinus Torvalds 			*mount_opts |= SF_SETMODE;
1991da177e4SLinus Torvalds 			break;
2001da177e4SLinus Torvalds 		case Opt_mufs:
2011da177e4SLinus Torvalds 			*mount_opts |= SF_MUFS;
2021da177e4SLinus Torvalds 			break;
2031da177e4SLinus Torvalds 		case Opt_prefix:
204f99d49adSJesper Juhl 			/* Free any previous prefix */
2051da177e4SLinus Torvalds 			kfree(*prefix);
2061da177e4SLinus Torvalds 			*prefix = match_strdup(&args[0]);
2071da177e4SLinus Torvalds 			if (!*prefix)
2081da177e4SLinus Torvalds 				return 0;
2091da177e4SLinus Torvalds 			*mount_opts |= SF_PREFIX;
2101da177e4SLinus Torvalds 			break;
2111da177e4SLinus Torvalds 		case Opt_protect:
2121da177e4SLinus Torvalds 			*mount_opts |= SF_IMMUTABLE;
2131da177e4SLinus Torvalds 			break;
2141da177e4SLinus Torvalds 		case Opt_reserved:
2151da177e4SLinus Torvalds 			if (match_int(&args[0], reserved))
2161da177e4SLinus Torvalds 				return 1;
2171da177e4SLinus Torvalds 			break;
2181da177e4SLinus Torvalds 		case Opt_root:
2191da177e4SLinus Torvalds 			if (match_int(&args[0], root))
2201da177e4SLinus Torvalds 				return 1;
2211da177e4SLinus Torvalds 			break;
2221da177e4SLinus Torvalds 		case Opt_setgid:
2231da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
2241da177e4SLinus Torvalds 				return 1;
2251da177e4SLinus Torvalds 			*gid = option;
2261da177e4SLinus Torvalds 			*mount_opts |= SF_SETGID;
2271da177e4SLinus Torvalds 			break;
2281da177e4SLinus Torvalds 		case Opt_setuid:
2291da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
2301da177e4SLinus Torvalds 				return -EINVAL;
2311da177e4SLinus Torvalds 			*uid = option;
2321da177e4SLinus Torvalds 			*mount_opts |= SF_SETUID;
2331da177e4SLinus Torvalds 			break;
2341da177e4SLinus Torvalds 		case Opt_verbose:
2351da177e4SLinus Torvalds 			*mount_opts |= SF_VERBOSE;
2361da177e4SLinus Torvalds 			break;
2371da177e4SLinus Torvalds 		case Opt_volume: {
2381da177e4SLinus Torvalds 			char *vol = match_strdup(&args[0]);
2396db27dd9SJim Meyering 			if (!vol)
2406db27dd9SJim Meyering 				return 0;
2411da177e4SLinus Torvalds 			strlcpy(volume, vol, 32);
2421da177e4SLinus Torvalds 			kfree(vol);
2431da177e4SLinus Torvalds 			break;
2441da177e4SLinus Torvalds 		}
2451da177e4SLinus Torvalds 		case Opt_ignore:
2461da177e4SLinus Torvalds 		 	/* Silently ignore the quota options */
2471da177e4SLinus Torvalds 			break;
2481da177e4SLinus Torvalds 		default:
2491da177e4SLinus Torvalds 			printk("AFFS: Unrecognized mount option \"%s\" "
2501da177e4SLinus Torvalds 					"or missing value\n", p);
2511da177e4SLinus Torvalds 			return 0;
2521da177e4SLinus Torvalds 		}
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	return 1;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /* This function definitely needs to be split up. Some fine day I'll
2581da177e4SLinus Torvalds  * hopefully have the guts to do so. Until then: sorry for the mess.
2591da177e4SLinus Torvalds  */
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds static int affs_fill_super(struct super_block *sb, void *data, int silent)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	struct affs_sb_info	*sbi;
2641da177e4SLinus Torvalds 	struct buffer_head	*root_bh = NULL;
2651da177e4SLinus Torvalds 	struct buffer_head	*boot_bh;
2661da177e4SLinus Torvalds 	struct inode		*root_inode = NULL;
2671da177e4SLinus Torvalds 	s32			 root_block;
2681da177e4SLinus Torvalds 	int			 size, blocksize;
2691da177e4SLinus Torvalds 	u32			 chksum;
2701da177e4SLinus Torvalds 	int			 num_bm;
2711da177e4SLinus Torvalds 	int			 i, j;
2721da177e4SLinus Torvalds 	s32			 key;
2731da177e4SLinus Torvalds 	uid_t			 uid;
2741da177e4SLinus Torvalds 	gid_t			 gid;
2751da177e4SLinus Torvalds 	int			 reserved;
2761da177e4SLinus Torvalds 	unsigned long		 mount_flags;
2771da177e4SLinus Torvalds 	int			 tmp_flags;	/* fix remount prototype... */
2782b943cf0SAl Viro 	u8			 sig[4];
279210f8559SDavid Howells 	int			 ret = -EINVAL;
2801da177e4SLinus Torvalds 
281e9b3961bSMiklos Szeredi 	save_mount_options(sb, data);
282e9b3961bSMiklos Szeredi 
2831da177e4SLinus Torvalds 	pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	sb->s_magic             = AFFS_SUPER_MAGIC;
2861da177e4SLinus Torvalds 	sb->s_op                = &affs_sops;
2871da177e4SLinus Torvalds 	sb->s_flags |= MS_NODIRATIME;
2881da177e4SLinus Torvalds 
289f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
2901da177e4SLinus Torvalds 	if (!sbi)
2911da177e4SLinus Torvalds 		return -ENOMEM;
2921da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
2937d135a5dSMatthias Kaehlcke 	mutex_init(&sbi->s_bmlock);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
2961da177e4SLinus Torvalds 				&blocksize,&sbi->s_prefix,
2971da177e4SLinus Torvalds 				sbi->s_volume, &mount_flags)) {
2981da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Error parsing options\n");
2991da177e4SLinus Torvalds 		return -EINVAL;
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 	/* N.B. after this point s_prefix must be released */
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	sbi->s_flags   = mount_flags;
3041da177e4SLinus Torvalds 	sbi->s_mode    = i;
3051da177e4SLinus Torvalds 	sbi->s_uid     = uid;
3061da177e4SLinus Torvalds 	sbi->s_gid     = gid;
3071da177e4SLinus Torvalds 	sbi->s_reserved= reserved;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* Get the size of the device in 512-byte blocks.
3101da177e4SLinus Torvalds 	 * If we later see that the partition uses bigger
3111da177e4SLinus Torvalds 	 * blocks, we will have to change it.
3121da177e4SLinus Torvalds 	 */
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	size = sb->s_bdev->bd_inode->i_size >> 9;
3151da177e4SLinus Torvalds 	pr_debug("AFFS: initial blocksize=%d, #blocks=%d\n", 512, size);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	affs_set_blocksize(sb, PAGE_SIZE);
3181da177e4SLinus Torvalds 	/* Try to find root block. Its location depends on the block size. */
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	i = 512;
3211da177e4SLinus Torvalds 	j = 4096;
3221da177e4SLinus Torvalds 	if (blocksize > 0) {
3231da177e4SLinus Torvalds 		i = j = blocksize;
3241da177e4SLinus Torvalds 		size = size / (blocksize / 512);
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 	for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
3271da177e4SLinus Torvalds 		sbi->s_root_block = root_block;
3281da177e4SLinus Torvalds 		if (root_block < 0)
3291da177e4SLinus Torvalds 			sbi->s_root_block = (reserved + size - 1) / 2;
3301da177e4SLinus Torvalds 		pr_debug("AFFS: setting blocksize to %d\n", blocksize);
3311da177e4SLinus Torvalds 		affs_set_blocksize(sb, blocksize);
3321da177e4SLinus Torvalds 		sbi->s_partition_size = size;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		/* The root block location that was calculated above is not
3351da177e4SLinus Torvalds 		 * correct if the partition size is an odd number of 512-
3361da177e4SLinus Torvalds 		 * byte blocks, which will be rounded down to a number of
3371da177e4SLinus Torvalds 		 * 1024-byte blocks, and if there were an even number of
3381da177e4SLinus Torvalds 		 * reserved blocks. Ideally, all partition checkers should
3391da177e4SLinus Torvalds 		 * report the real number of blocks of the real blocksize,
3401da177e4SLinus Torvalds 		 * but since this just cannot be done, we have to try to
3411da177e4SLinus Torvalds 		 * find the root block anyways. In the above case, it is one
3421da177e4SLinus Torvalds 		 * block behind the calculated one. So we check this one, too.
3431da177e4SLinus Torvalds 		 */
3441da177e4SLinus Torvalds 		for (num_bm = 0; num_bm < 2; num_bm++) {
3451da177e4SLinus Torvalds 			pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
3461da177e4SLinus Torvalds 				"size=%d, reserved=%d\n",
3471da177e4SLinus Torvalds 				sb->s_id,
3481da177e4SLinus Torvalds 				sbi->s_root_block + num_bm,
3491da177e4SLinus Torvalds 				blocksize, size, reserved);
3501da177e4SLinus Torvalds 			root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
3511da177e4SLinus Torvalds 			if (!root_bh)
3521da177e4SLinus Torvalds 				continue;
3531da177e4SLinus Torvalds 			if (!affs_checksum_block(sb, root_bh) &&
3541da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
3551da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
3561da177e4SLinus Torvalds 				sbi->s_hashsize    = blocksize / 4 - 56;
3571da177e4SLinus Torvalds 				sbi->s_root_block += num_bm;
3581da177e4SLinus Torvalds 				key                        = 1;
3591da177e4SLinus Torvalds 				goto got_root;
3601da177e4SLinus Torvalds 			}
3611da177e4SLinus Torvalds 			affs_brelse(root_bh);
3621da177e4SLinus Torvalds 			root_bh = NULL;
3631da177e4SLinus Torvalds 		}
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 	if (!silent)
3661da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: No valid root block on device %s\n",
3671da177e4SLinus Torvalds 			sb->s_id);
3681da177e4SLinus Torvalds 	goto out_error;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* N.B. after this point bh must be released */
3711da177e4SLinus Torvalds got_root:
3721da177e4SLinus Torvalds 	root_block = sbi->s_root_block;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/* Find out which kind of FS we have */
3751da177e4SLinus Torvalds 	boot_bh = sb_bread(sb, 0);
3761da177e4SLinus Torvalds 	if (!boot_bh) {
3771da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Cannot read boot block\n");
3781da177e4SLinus Torvalds 		goto out_error;
3791da177e4SLinus Torvalds 	}
3802b943cf0SAl Viro 	memcpy(sig, boot_bh->b_data, 4);
3811da177e4SLinus Torvalds 	brelse(boot_bh);
3822b943cf0SAl Viro 	chksum = be32_to_cpu(*(__be32 *)sig);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Dircache filesystems are compatible with non-dircache ones
3851da177e4SLinus Torvalds 	 * when reading. As long as they aren't supported, writing is
3861da177e4SLinus Torvalds 	 * not recommended.
3871da177e4SLinus Torvalds 	 */
3881da177e4SLinus Torvalds 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
3891da177e4SLinus Torvalds 	     || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
3901da177e4SLinus Torvalds 		printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
3911da177e4SLinus Torvalds 			sb->s_id);
3921da177e4SLinus Torvalds 		sb->s_flags |= MS_RDONLY;
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 	switch (chksum) {
3951da177e4SLinus Torvalds 		case MUFS_FS:
3961da177e4SLinus Torvalds 		case MUFS_INTLFFS:
3971da177e4SLinus Torvalds 		case MUFS_DCFFS:
3981da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
3991da177e4SLinus Torvalds 			/* fall thru */
4001da177e4SLinus Torvalds 		case FS_INTLFFS:
4011da177e4SLinus Torvalds 		case FS_DCFFS:
4021da177e4SLinus Torvalds 			sbi->s_flags |= SF_INTL;
4031da177e4SLinus Torvalds 			break;
4041da177e4SLinus Torvalds 		case MUFS_FFS:
4051da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4061da177e4SLinus Torvalds 			break;
4071da177e4SLinus Torvalds 		case FS_FFS:
4081da177e4SLinus Torvalds 			break;
4091da177e4SLinus Torvalds 		case MUFS_OFS:
4101da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4111da177e4SLinus Torvalds 			/* fall thru */
4121da177e4SLinus Torvalds 		case FS_OFS:
4131da177e4SLinus Torvalds 			sbi->s_flags |= SF_OFS;
4141da177e4SLinus Torvalds 			sb->s_flags |= MS_NOEXEC;
4151da177e4SLinus Torvalds 			break;
4161da177e4SLinus Torvalds 		case MUFS_DCOFS:
4171da177e4SLinus Torvalds 		case MUFS_INTLOFS:
4181da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4191da177e4SLinus Torvalds 		case FS_DCOFS:
4201da177e4SLinus Torvalds 		case FS_INTLOFS:
4211da177e4SLinus Torvalds 			sbi->s_flags |= SF_INTL | SF_OFS;
4221da177e4SLinus Torvalds 			sb->s_flags |= MS_NOEXEC;
4231da177e4SLinus Torvalds 			break;
4241da177e4SLinus Torvalds 		default:
4251da177e4SLinus Torvalds 			printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
4261da177e4SLinus Torvalds 				sb->s_id, chksum);
4271da177e4SLinus Torvalds 			goto out_error;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (mount_flags & SF_VERBOSE) {
4312b943cf0SAl Viro 		u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
4322b943cf0SAl Viro 		printk(KERN_NOTICE "AFFS: Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
4332b943cf0SAl Viro 			len > 31 ? 31 : len,
4341da177e4SLinus Torvalds 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
4352b943cf0SAl Viro 			sig, sig[3] + '0', blocksize);
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	sb->s_flags |= MS_NODEV | MS_NOSUID;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	sbi->s_data_blksize = sb->s_blocksize;
4411da177e4SLinus Torvalds 	if (sbi->s_flags & SF_OFS)
4421da177e4SLinus Torvalds 		sbi->s_data_blksize -= 24;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	/* Keep super block in cache */
4451da177e4SLinus Torvalds 	sbi->s_root_bh = root_bh;
4461da177e4SLinus Torvalds 	/* N.B. after this point s_root_bh must be released */
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	tmp_flags = sb->s_flags;
4491da177e4SLinus Torvalds 	if (affs_init_bitmap(sb, &tmp_flags))
4501da177e4SLinus Torvalds 		goto out_error;
4511da177e4SLinus Torvalds 	sb->s_flags = tmp_flags;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
4541da177e4SLinus Torvalds 
455210f8559SDavid Howells 	root_inode = affs_iget(sb, root_block);
456210f8559SDavid Howells 	if (IS_ERR(root_inode)) {
457210f8559SDavid Howells 		ret = PTR_ERR(root_inode);
458210f8559SDavid Howells 		goto out_error_noinode;
459210f8559SDavid Howells 	}
460210f8559SDavid Howells 
4611da177e4SLinus Torvalds 	sb->s_root = d_alloc_root(root_inode);
4621da177e4SLinus Torvalds 	if (!sb->s_root) {
4631da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Get root inode failed\n");
4641da177e4SLinus Torvalds 		goto out_error;
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 	sb->s_root->d_op = &affs_dentry_operations;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
4691da177e4SLinus Torvalds 	return 0;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	/*
4721da177e4SLinus Torvalds 	 * Begin the cascaded cleanup ...
4731da177e4SLinus Torvalds 	 */
4741da177e4SLinus Torvalds out_error:
4751da177e4SLinus Torvalds 	if (root_inode)
4761da177e4SLinus Torvalds 		iput(root_inode);
477210f8559SDavid Howells out_error_noinode:
4781da177e4SLinus Torvalds 	kfree(sbi->s_bitmap);
4791da177e4SLinus Torvalds 	affs_brelse(root_bh);
4801da177e4SLinus Torvalds 	kfree(sbi->s_prefix);
4811da177e4SLinus Torvalds 	kfree(sbi);
4821da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
483210f8559SDavid Howells 	return ret;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds static int
4871da177e4SLinus Torvalds affs_remount(struct super_block *sb, int *flags, char *data)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
4901da177e4SLinus Torvalds 	int			 blocksize;
4911da177e4SLinus Torvalds 	uid_t			 uid;
4921da177e4SLinus Torvalds 	gid_t			 gid;
4931da177e4SLinus Torvalds 	int			 mode;
4941da177e4SLinus Torvalds 	int			 reserved;
4951da177e4SLinus Torvalds 	int			 root_block;
4961da177e4SLinus Torvalds 	unsigned long		 mount_flags;
4971da177e4SLinus Torvalds 	int			 res = 0;
498e9b3961bSMiklos Szeredi 	char			*new_opts = kstrdup(data, GFP_KERNEL);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	*flags |= MS_NODIRATIME;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
505e9b3961bSMiklos Szeredi 			   &blocksize, &sbi->s_prefix, sbi->s_volume,
506e9b3961bSMiklos Szeredi 			   &mount_flags)) {
507e9b3961bSMiklos Szeredi 		kfree(new_opts);
5081da177e4SLinus Torvalds 		return -EINVAL;
509e9b3961bSMiklos Szeredi 	}
510*2a32cebdSAl Viro 	replace_mount_options(sb, new_opts);
511e9b3961bSMiklos Szeredi 
5121da177e4SLinus Torvalds 	sbi->s_flags = mount_flags;
5131da177e4SLinus Torvalds 	sbi->s_mode  = mode;
5141da177e4SLinus Torvalds 	sbi->s_uid   = uid;
5151da177e4SLinus Torvalds 	sbi->s_gid   = gid;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
5181da177e4SLinus Torvalds 		return 0;
5191da177e4SLinus Torvalds 	if (*flags & MS_RDONLY) {
5201da177e4SLinus Torvalds 		sb->s_dirt = 1;
5211da177e4SLinus Torvalds 		while (sb->s_dirt)
5221da177e4SLinus Torvalds 			affs_write_super(sb);
5231da177e4SLinus Torvalds 		affs_free_bitmap(sb);
5241da177e4SLinus Torvalds 	} else
5251da177e4SLinus Torvalds 		res = affs_init_bitmap(sb, flags);
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	return res;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds static int
531726c3342SDavid Howells affs_statfs(struct dentry *dentry, struct kstatfs *buf)
5321da177e4SLinus Torvalds {
533726c3342SDavid Howells 	struct super_block *sb = dentry->d_sb;
5341da177e4SLinus Torvalds 	int		 free;
535a6a2a73cSColy Li 	u64		 id = huge_encode_dev(sb->s_bdev->bd_dev);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB(sb)->s_partition_size,
5381da177e4SLinus Torvalds 	     AFFS_SB(sb)->s_reserved);
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	free          = affs_count_free_blocks(sb);
5411da177e4SLinus Torvalds 	buf->f_type    = AFFS_SUPER_MAGIC;
5421da177e4SLinus Torvalds 	buf->f_bsize   = sb->s_blocksize;
5431da177e4SLinus Torvalds 	buf->f_blocks  = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
5441da177e4SLinus Torvalds 	buf->f_bfree   = free;
5451da177e4SLinus Torvalds 	buf->f_bavail  = free;
546a6a2a73cSColy Li 	buf->f_fsid.val[0] = (u32)id;
547a6a2a73cSColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
548a6a2a73cSColy Li 	buf->f_namelen = 30;
5491da177e4SLinus Torvalds 	return 0;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
552454e2398SDavid Howells static int affs_get_sb(struct file_system_type *fs_type,
553454e2398SDavid Howells 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
5541da177e4SLinus Torvalds {
555454e2398SDavid Howells 	return get_sb_bdev(fs_type, flags, dev_name, data, affs_fill_super,
556454e2398SDavid Howells 			   mnt);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds static struct file_system_type affs_fs_type = {
5601da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
5611da177e4SLinus Torvalds 	.name		= "affs",
5621da177e4SLinus Torvalds 	.get_sb		= affs_get_sb,
5631da177e4SLinus Torvalds 	.kill_sb	= kill_block_super,
5641da177e4SLinus Torvalds 	.fs_flags	= FS_REQUIRES_DEV,
5651da177e4SLinus Torvalds };
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds static int __init init_affs_fs(void)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	int err = init_inodecache();
5701da177e4SLinus Torvalds 	if (err)
5711da177e4SLinus Torvalds 		goto out1;
5721da177e4SLinus Torvalds 	err = register_filesystem(&affs_fs_type);
5731da177e4SLinus Torvalds 	if (err)
5741da177e4SLinus Torvalds 		goto out;
5751da177e4SLinus Torvalds 	return 0;
5761da177e4SLinus Torvalds out:
5771da177e4SLinus Torvalds 	destroy_inodecache();
5781da177e4SLinus Torvalds out1:
5791da177e4SLinus Torvalds 	return err;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds static void __exit exit_affs_fs(void)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds 	unregister_filesystem(&affs_fs_type);
5851da177e4SLinus Torvalds 	destroy_inodecache();
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds MODULE_DESCRIPTION("Amiga filesystem support for Linux");
5891da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds module_init(init_affs_fs)
5921da177e4SLinus Torvalds module_exit(exit_affs_fs)
593