xref: /linux/fs/affs/super.c (revision 756b0322e50aebc4b9afb4488a2d3f6c802b4e64)
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>
19337eb00aSAlessio Igor Bogani #include <linux/smp_lock.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
211da177e4SLinus Torvalds #include "affs.h"
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds extern struct timezone sys_tz;
241da177e4SLinus Torvalds 
25726c3342SDavid Howells static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
261da177e4SLinus Torvalds static int affs_remount (struct super_block *sb, int *flags, char *data);
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds static void
297435d506SArtem Bityutskiy affs_commit_super(struct super_block *sb, int wait, int clean)
30e2896436SChristoph Hellwig {
31e2896436SChristoph Hellwig 	struct affs_sb_info *sbi = AFFS_SB(sb);
32e2896436SChristoph Hellwig 	struct buffer_head *bh = sbi->s_root_bh;
33e2896436SChristoph Hellwig 	struct affs_root_tail *tail = AFFS_ROOT_TAIL(sb, bh);
34e2896436SChristoph Hellwig 
35e2896436SChristoph Hellwig 	tail->bm_flag = cpu_to_be32(clean);
36e2896436SChristoph Hellwig 	secs_to_datestamp(get_seconds(), &tail->disk_change);
37e2896436SChristoph Hellwig 	affs_fix_checksum(sb, bh);
38e2896436SChristoph Hellwig 	mark_buffer_dirty(bh);
397435d506SArtem Bityutskiy 	if (wait)
407435d506SArtem Bityutskiy 		sync_dirty_buffer(bh);
41e2896436SChristoph Hellwig }
42e2896436SChristoph Hellwig 
43e2896436SChristoph Hellwig static void
441da177e4SLinus Torvalds affs_put_super(struct super_block *sb)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct affs_sb_info *sbi = AFFS_SB(sb);
471da177e4SLinus Torvalds 	pr_debug("AFFS: put_super()\n");
481da177e4SLinus Torvalds 
496cfd0148SChristoph Hellwig 	lock_kernel();
506cfd0148SChristoph Hellwig 
517435d506SArtem Bityutskiy 	if (!(sb->s_flags & MS_RDONLY) && sb->s_dirt)
527435d506SArtem Bityutskiy 		affs_commit_super(sb, 1, 1);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	kfree(sbi->s_prefix);
551da177e4SLinus Torvalds 	affs_free_bitmap(sb);
561da177e4SLinus Torvalds 	affs_brelse(sbi->s_root_bh);
571da177e4SLinus Torvalds 	kfree(sbi);
581da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
596cfd0148SChristoph Hellwig 
606cfd0148SChristoph Hellwig 	unlock_kernel();
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds static void
641da177e4SLinus Torvalds affs_write_super(struct super_block *sb)
651da177e4SLinus Torvalds {
66ebc1ac16SChristoph Hellwig 	lock_super(sb);
67669d5f1fSArtem Bityutskiy 	if (!(sb->s_flags & MS_RDONLY))
687435d506SArtem Bityutskiy 		affs_commit_super(sb, 1, 2);
691da177e4SLinus Torvalds 	sb->s_dirt = 0;
70ebc1ac16SChristoph Hellwig 	unlock_super(sb);
711da177e4SLinus Torvalds 
72669d5f1fSArtem Bityutskiy 	pr_debug("AFFS: write_super() at %lu, clean=2\n", get_seconds());
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
75e2896436SChristoph Hellwig static int
76e2896436SChristoph Hellwig affs_sync_fs(struct super_block *sb, int wait)
77e2896436SChristoph Hellwig {
78e2896436SChristoph Hellwig 	lock_super(sb);
797435d506SArtem Bityutskiy 	affs_commit_super(sb, wait, 2);
80e2896436SChristoph Hellwig 	sb->s_dirt = 0;
81e2896436SChristoph Hellwig 	unlock_super(sb);
82e2896436SChristoph Hellwig 	return 0;
83e2896436SChristoph Hellwig }
84e2896436SChristoph Hellwig 
85e18b890bSChristoph Lameter static struct kmem_cache * affs_inode_cachep;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds static struct inode *affs_alloc_inode(struct super_block *sb)
881da177e4SLinus Torvalds {
89dca3c336SRoman Zippel 	struct affs_inode_info *i;
90dca3c336SRoman Zippel 
91dca3c336SRoman Zippel 	i = kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
92dca3c336SRoman Zippel 	if (!i)
931da177e4SLinus Torvalds 		return NULL;
94dca3c336SRoman Zippel 
95dca3c336SRoman Zippel 	i->vfs_inode.i_version = 1;
96dca3c336SRoman Zippel 	i->i_lc = NULL;
97dca3c336SRoman Zippel 	i->i_ext_bh = NULL;
98dca3c336SRoman Zippel 	i->i_pa_cnt = 0;
99dca3c336SRoman Zippel 
100dca3c336SRoman Zippel 	return &i->vfs_inode;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static void affs_destroy_inode(struct inode *inode)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
10851cc5068SAlexey Dobriyan static void init_once(void *foo)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	struct affs_inode_info *ei = (struct affs_inode_info *) foo;
1111da177e4SLinus Torvalds 
112*756b0322SThomas Gleixner 	sema_init(&ei->i_link_lock, 1);
113*756b0322SThomas Gleixner 	sema_init(&ei->i_ext_lock, 1);
1141da177e4SLinus Torvalds 	inode_init_once(&ei->vfs_inode);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static int init_inodecache(void)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	affs_inode_cachep = kmem_cache_create("affs_inode_cache",
1201da177e4SLinus Torvalds 					     sizeof(struct affs_inode_info),
121fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
122fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
12320c2df83SPaul Mundt 					     init_once);
1241da177e4SLinus Torvalds 	if (affs_inode_cachep == NULL)
1251da177e4SLinus Torvalds 		return -ENOMEM;
1261da177e4SLinus Torvalds 	return 0;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static void destroy_inodecache(void)
1301da177e4SLinus Torvalds {
1311a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(affs_inode_cachep);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
134ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations affs_sops = {
1351da177e4SLinus Torvalds 	.alloc_inode	= affs_alloc_inode,
1361da177e4SLinus Torvalds 	.destroy_inode	= affs_destroy_inode,
1371da177e4SLinus Torvalds 	.write_inode	= affs_write_inode,
138f053dddeSAl Viro 	.evict_inode	= affs_evict_inode,
1391da177e4SLinus Torvalds 	.put_super	= affs_put_super,
1401da177e4SLinus Torvalds 	.write_super	= affs_write_super,
141e2896436SChristoph Hellwig 	.sync_fs	= affs_sync_fs,
1421da177e4SLinus Torvalds 	.statfs		= affs_statfs,
1431da177e4SLinus Torvalds 	.remount_fs	= affs_remount,
144e9b3961bSMiklos Szeredi 	.show_options	= generic_show_options,
1451da177e4SLinus Torvalds };
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds enum {
1481da177e4SLinus Torvalds 	Opt_bs, Opt_mode, Opt_mufs, Opt_prefix, Opt_protect,
1491da177e4SLinus Torvalds 	Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
1501da177e4SLinus Torvalds 	Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
1511da177e4SLinus Torvalds };
1521da177e4SLinus Torvalds 
153a447c093SSteven Whitehouse static const match_table_t tokens = {
1541da177e4SLinus Torvalds 	{Opt_bs, "bs=%u"},
1551da177e4SLinus Torvalds 	{Opt_mode, "mode=%o"},
1561da177e4SLinus Torvalds 	{Opt_mufs, "mufs"},
1571da177e4SLinus Torvalds 	{Opt_prefix, "prefix=%s"},
1581da177e4SLinus Torvalds 	{Opt_protect, "protect"},
1591da177e4SLinus Torvalds 	{Opt_reserved, "reserved=%u"},
1601da177e4SLinus Torvalds 	{Opt_root, "root=%u"},
1611da177e4SLinus Torvalds 	{Opt_setgid, "setgid=%u"},
1621da177e4SLinus Torvalds 	{Opt_setuid, "setuid=%u"},
1631da177e4SLinus Torvalds 	{Opt_verbose, "verbose"},
1641da177e4SLinus Torvalds 	{Opt_volume, "volume=%s"},
1651da177e4SLinus Torvalds 	{Opt_ignore, "grpquota"},
1661da177e4SLinus Torvalds 	{Opt_ignore, "noquota"},
1671da177e4SLinus Torvalds 	{Opt_ignore, "quota"},
1681da177e4SLinus Torvalds 	{Opt_ignore, "usrquota"},
1691da177e4SLinus Torvalds 	{Opt_err, NULL},
1701da177e4SLinus Torvalds };
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static int
1731da177e4SLinus Torvalds parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
1741da177e4SLinus Torvalds 		int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	char *p;
1771da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* Fill in defaults */
1801da177e4SLinus Torvalds 
18121559981SDavid Howells 	*uid        = current_uid();
18221559981SDavid Howells 	*gid        = current_gid();
1831da177e4SLinus Torvalds 	*reserved   = 2;
1841da177e4SLinus Torvalds 	*root       = -1;
1851da177e4SLinus Torvalds 	*blocksize  = -1;
1861da177e4SLinus Torvalds 	volume[0]   = ':';
1871da177e4SLinus Torvalds 	volume[1]   = 0;
1881da177e4SLinus Torvalds 	*mount_opts = 0;
1891da177e4SLinus Torvalds 	if (!options)
1901da177e4SLinus Torvalds 		return 1;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
1931da177e4SLinus Torvalds 		int token, n, option;
1941da177e4SLinus Torvalds 		if (!*p)
1951da177e4SLinus Torvalds 			continue;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 		token = match_token(p, tokens, args);
1981da177e4SLinus Torvalds 		switch (token) {
1991da177e4SLinus Torvalds 		case Opt_bs:
2001da177e4SLinus Torvalds 			if (match_int(&args[0], &n))
201217686e9SAl Viro 				return 0;
2021da177e4SLinus Torvalds 			if (n != 512 && n != 1024 && n != 2048
2031da177e4SLinus Torvalds 			    && n != 4096) {
2041da177e4SLinus Torvalds 				printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
2051da177e4SLinus Torvalds 				return 0;
2061da177e4SLinus Torvalds 			}
2071da177e4SLinus Torvalds 			*blocksize = n;
2081da177e4SLinus Torvalds 			break;
2091da177e4SLinus Torvalds 		case Opt_mode:
2101da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
211217686e9SAl Viro 				return 0;
2121da177e4SLinus Torvalds 			*mode = option & 0777;
2131da177e4SLinus Torvalds 			*mount_opts |= SF_SETMODE;
2141da177e4SLinus Torvalds 			break;
2151da177e4SLinus Torvalds 		case Opt_mufs:
2161da177e4SLinus Torvalds 			*mount_opts |= SF_MUFS;
2171da177e4SLinus Torvalds 			break;
2181da177e4SLinus Torvalds 		case Opt_prefix:
2191da177e4SLinus Torvalds 			*prefix = match_strdup(&args[0]);
2201da177e4SLinus Torvalds 			if (!*prefix)
2211da177e4SLinus Torvalds 				return 0;
2221da177e4SLinus Torvalds 			*mount_opts |= SF_PREFIX;
2231da177e4SLinus Torvalds 			break;
2241da177e4SLinus Torvalds 		case Opt_protect:
2251da177e4SLinus Torvalds 			*mount_opts |= SF_IMMUTABLE;
2261da177e4SLinus Torvalds 			break;
2271da177e4SLinus Torvalds 		case Opt_reserved:
2281da177e4SLinus Torvalds 			if (match_int(&args[0], reserved))
229217686e9SAl Viro 				return 0;
2301da177e4SLinus Torvalds 			break;
2311da177e4SLinus Torvalds 		case Opt_root:
2321da177e4SLinus Torvalds 			if (match_int(&args[0], root))
233217686e9SAl Viro 				return 0;
2341da177e4SLinus Torvalds 			break;
2351da177e4SLinus Torvalds 		case Opt_setgid:
2361da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
237217686e9SAl Viro 				return 0;
2381da177e4SLinus Torvalds 			*gid = option;
2391da177e4SLinus Torvalds 			*mount_opts |= SF_SETGID;
2401da177e4SLinus Torvalds 			break;
2411da177e4SLinus Torvalds 		case Opt_setuid:
2421da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
243217686e9SAl Viro 				return 0;
2441da177e4SLinus Torvalds 			*uid = option;
2451da177e4SLinus Torvalds 			*mount_opts |= SF_SETUID;
2461da177e4SLinus Torvalds 			break;
2471da177e4SLinus Torvalds 		case Opt_verbose:
2481da177e4SLinus Torvalds 			*mount_opts |= SF_VERBOSE;
2491da177e4SLinus Torvalds 			break;
2501da177e4SLinus Torvalds 		case Opt_volume: {
2511da177e4SLinus Torvalds 			char *vol = match_strdup(&args[0]);
2526db27dd9SJim Meyering 			if (!vol)
2536db27dd9SJim Meyering 				return 0;
2541da177e4SLinus Torvalds 			strlcpy(volume, vol, 32);
2551da177e4SLinus Torvalds 			kfree(vol);
2561da177e4SLinus Torvalds 			break;
2571da177e4SLinus Torvalds 		}
2581da177e4SLinus Torvalds 		case Opt_ignore:
2591da177e4SLinus Torvalds 		 	/* Silently ignore the quota options */
2601da177e4SLinus Torvalds 			break;
2611da177e4SLinus Torvalds 		default:
2621da177e4SLinus Torvalds 			printk("AFFS: Unrecognized mount option \"%s\" "
2631da177e4SLinus Torvalds 					"or missing value\n", p);
2641da177e4SLinus Torvalds 			return 0;
2651da177e4SLinus Torvalds 		}
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 	return 1;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /* This function definitely needs to be split up. Some fine day I'll
2711da177e4SLinus Torvalds  * hopefully have the guts to do so. Until then: sorry for the mess.
2721da177e4SLinus Torvalds  */
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds static int affs_fill_super(struct super_block *sb, void *data, int silent)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	struct affs_sb_info	*sbi;
2771da177e4SLinus Torvalds 	struct buffer_head	*root_bh = NULL;
2781da177e4SLinus Torvalds 	struct buffer_head	*boot_bh;
2791da177e4SLinus Torvalds 	struct inode		*root_inode = NULL;
2801da177e4SLinus Torvalds 	s32			 root_block;
2811da177e4SLinus Torvalds 	int			 size, blocksize;
2821da177e4SLinus Torvalds 	u32			 chksum;
2831da177e4SLinus Torvalds 	int			 num_bm;
2841da177e4SLinus Torvalds 	int			 i, j;
2851da177e4SLinus Torvalds 	s32			 key;
2861da177e4SLinus Torvalds 	uid_t			 uid;
2871da177e4SLinus Torvalds 	gid_t			 gid;
2881da177e4SLinus Torvalds 	int			 reserved;
2891da177e4SLinus Torvalds 	unsigned long		 mount_flags;
2901da177e4SLinus Torvalds 	int			 tmp_flags;	/* fix remount prototype... */
2912b943cf0SAl Viro 	u8			 sig[4];
292210f8559SDavid Howells 	int			 ret = -EINVAL;
2931da177e4SLinus Torvalds 
294e9b3961bSMiklos Szeredi 	save_mount_options(sb, data);
295e9b3961bSMiklos Szeredi 
2961da177e4SLinus Torvalds 	pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	sb->s_magic             = AFFS_SUPER_MAGIC;
2991da177e4SLinus Torvalds 	sb->s_op                = &affs_sops;
3001da177e4SLinus Torvalds 	sb->s_flags |= MS_NODIRATIME;
3011da177e4SLinus Torvalds 
302f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
3031da177e4SLinus Torvalds 	if (!sbi)
3041da177e4SLinus Torvalds 		return -ENOMEM;
3051da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
3067d135a5dSMatthias Kaehlcke 	mutex_init(&sbi->s_bmlock);
30729333920SAl Viro 	spin_lock_init(&sbi->symlink_lock);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
3101da177e4SLinus Torvalds 				&blocksize,&sbi->s_prefix,
3111da177e4SLinus Torvalds 				sbi->s_volume, &mount_flags)) {
3121da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Error parsing options\n");
313afc70ed0SAl Viro 		kfree(sbi->s_prefix);
314afc70ed0SAl Viro 		kfree(sbi);
3151da177e4SLinus Torvalds 		return -EINVAL;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 	/* N.B. after this point s_prefix must be released */
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	sbi->s_flags   = mount_flags;
3201da177e4SLinus Torvalds 	sbi->s_mode    = i;
3211da177e4SLinus Torvalds 	sbi->s_uid     = uid;
3221da177e4SLinus Torvalds 	sbi->s_gid     = gid;
3231da177e4SLinus Torvalds 	sbi->s_reserved= reserved;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	/* Get the size of the device in 512-byte blocks.
3261da177e4SLinus Torvalds 	 * If we later see that the partition uses bigger
3271da177e4SLinus Torvalds 	 * blocks, we will have to change it.
3281da177e4SLinus Torvalds 	 */
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	size = sb->s_bdev->bd_inode->i_size >> 9;
3311da177e4SLinus Torvalds 	pr_debug("AFFS: initial blocksize=%d, #blocks=%d\n", 512, size);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	affs_set_blocksize(sb, PAGE_SIZE);
3341da177e4SLinus Torvalds 	/* Try to find root block. Its location depends on the block size. */
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	i = 512;
3371da177e4SLinus Torvalds 	j = 4096;
3381da177e4SLinus Torvalds 	if (blocksize > 0) {
3391da177e4SLinus Torvalds 		i = j = blocksize;
3401da177e4SLinus Torvalds 		size = size / (blocksize / 512);
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 	for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
3431da177e4SLinus Torvalds 		sbi->s_root_block = root_block;
3441da177e4SLinus Torvalds 		if (root_block < 0)
3451da177e4SLinus Torvalds 			sbi->s_root_block = (reserved + size - 1) / 2;
3461da177e4SLinus Torvalds 		pr_debug("AFFS: setting blocksize to %d\n", blocksize);
3471da177e4SLinus Torvalds 		affs_set_blocksize(sb, blocksize);
3481da177e4SLinus Torvalds 		sbi->s_partition_size = size;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		/* The root block location that was calculated above is not
3511da177e4SLinus Torvalds 		 * correct if the partition size is an odd number of 512-
3521da177e4SLinus Torvalds 		 * byte blocks, which will be rounded down to a number of
3531da177e4SLinus Torvalds 		 * 1024-byte blocks, and if there were an even number of
3541da177e4SLinus Torvalds 		 * reserved blocks. Ideally, all partition checkers should
3551da177e4SLinus Torvalds 		 * report the real number of blocks of the real blocksize,
3561da177e4SLinus Torvalds 		 * but since this just cannot be done, we have to try to
3571da177e4SLinus Torvalds 		 * find the root block anyways. In the above case, it is one
3581da177e4SLinus Torvalds 		 * block behind the calculated one. So we check this one, too.
3591da177e4SLinus Torvalds 		 */
3601da177e4SLinus Torvalds 		for (num_bm = 0; num_bm < 2; num_bm++) {
3611da177e4SLinus Torvalds 			pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
3621da177e4SLinus Torvalds 				"size=%d, reserved=%d\n",
3631da177e4SLinus Torvalds 				sb->s_id,
3641da177e4SLinus Torvalds 				sbi->s_root_block + num_bm,
3651da177e4SLinus Torvalds 				blocksize, size, reserved);
3661da177e4SLinus Torvalds 			root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
3671da177e4SLinus Torvalds 			if (!root_bh)
3681da177e4SLinus Torvalds 				continue;
3691da177e4SLinus Torvalds 			if (!affs_checksum_block(sb, root_bh) &&
3701da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
3711da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
3721da177e4SLinus Torvalds 				sbi->s_hashsize    = blocksize / 4 - 56;
3731da177e4SLinus Torvalds 				sbi->s_root_block += num_bm;
3741da177e4SLinus Torvalds 				key                        = 1;
3751da177e4SLinus Torvalds 				goto got_root;
3761da177e4SLinus Torvalds 			}
3771da177e4SLinus Torvalds 			affs_brelse(root_bh);
3781da177e4SLinus Torvalds 			root_bh = NULL;
3791da177e4SLinus Torvalds 		}
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	if (!silent)
3821da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: No valid root block on device %s\n",
3831da177e4SLinus Torvalds 			sb->s_id);
3841da177e4SLinus Torvalds 	goto out_error;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	/* N.B. after this point bh must be released */
3871da177e4SLinus Torvalds got_root:
3881da177e4SLinus Torvalds 	root_block = sbi->s_root_block;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	/* Find out which kind of FS we have */
3911da177e4SLinus Torvalds 	boot_bh = sb_bread(sb, 0);
3921da177e4SLinus Torvalds 	if (!boot_bh) {
3931da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Cannot read boot block\n");
3941da177e4SLinus Torvalds 		goto out_error;
3951da177e4SLinus Torvalds 	}
3962b943cf0SAl Viro 	memcpy(sig, boot_bh->b_data, 4);
3971da177e4SLinus Torvalds 	brelse(boot_bh);
3982b943cf0SAl Viro 	chksum = be32_to_cpu(*(__be32 *)sig);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	/* Dircache filesystems are compatible with non-dircache ones
4011da177e4SLinus Torvalds 	 * when reading. As long as they aren't supported, writing is
4021da177e4SLinus Torvalds 	 * not recommended.
4031da177e4SLinus Torvalds 	 */
4041da177e4SLinus Torvalds 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
4051da177e4SLinus Torvalds 	     || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
4061da177e4SLinus Torvalds 		printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
4071da177e4SLinus Torvalds 			sb->s_id);
4081da177e4SLinus Torvalds 		sb->s_flags |= MS_RDONLY;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 	switch (chksum) {
4111da177e4SLinus Torvalds 		case MUFS_FS:
4121da177e4SLinus Torvalds 		case MUFS_INTLFFS:
4131da177e4SLinus Torvalds 		case MUFS_DCFFS:
4141da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4151da177e4SLinus Torvalds 			/* fall thru */
4161da177e4SLinus Torvalds 		case FS_INTLFFS:
4171da177e4SLinus Torvalds 		case FS_DCFFS:
4181da177e4SLinus Torvalds 			sbi->s_flags |= SF_INTL;
4191da177e4SLinus Torvalds 			break;
4201da177e4SLinus Torvalds 		case MUFS_FFS:
4211da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4221da177e4SLinus Torvalds 			break;
4231da177e4SLinus Torvalds 		case FS_FFS:
4241da177e4SLinus Torvalds 			break;
4251da177e4SLinus Torvalds 		case MUFS_OFS:
4261da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4271da177e4SLinus Torvalds 			/* fall thru */
4281da177e4SLinus Torvalds 		case FS_OFS:
4291da177e4SLinus Torvalds 			sbi->s_flags |= SF_OFS;
4301da177e4SLinus Torvalds 			sb->s_flags |= MS_NOEXEC;
4311da177e4SLinus Torvalds 			break;
4321da177e4SLinus Torvalds 		case MUFS_DCOFS:
4331da177e4SLinus Torvalds 		case MUFS_INTLOFS:
4341da177e4SLinus Torvalds 			sbi->s_flags |= SF_MUFS;
4351da177e4SLinus Torvalds 		case FS_DCOFS:
4361da177e4SLinus Torvalds 		case FS_INTLOFS:
4371da177e4SLinus Torvalds 			sbi->s_flags |= SF_INTL | SF_OFS;
4381da177e4SLinus Torvalds 			sb->s_flags |= MS_NOEXEC;
4391da177e4SLinus Torvalds 			break;
4401da177e4SLinus Torvalds 		default:
4411da177e4SLinus Torvalds 			printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
4421da177e4SLinus Torvalds 				sb->s_id, chksum);
4431da177e4SLinus Torvalds 			goto out_error;
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if (mount_flags & SF_VERBOSE) {
4472b943cf0SAl Viro 		u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
4482b943cf0SAl Viro 		printk(KERN_NOTICE "AFFS: Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
4492b943cf0SAl Viro 			len > 31 ? 31 : len,
4501da177e4SLinus Torvalds 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
4512b943cf0SAl Viro 			sig, sig[3] + '0', blocksize);
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	sb->s_flags |= MS_NODEV | MS_NOSUID;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	sbi->s_data_blksize = sb->s_blocksize;
4571da177e4SLinus Torvalds 	if (sbi->s_flags & SF_OFS)
4581da177e4SLinus Torvalds 		sbi->s_data_blksize -= 24;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	/* Keep super block in cache */
4611da177e4SLinus Torvalds 	sbi->s_root_bh = root_bh;
4621da177e4SLinus Torvalds 	/* N.B. after this point s_root_bh must be released */
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	tmp_flags = sb->s_flags;
4651da177e4SLinus Torvalds 	if (affs_init_bitmap(sb, &tmp_flags))
4661da177e4SLinus Torvalds 		goto out_error;
4671da177e4SLinus Torvalds 	sb->s_flags = tmp_flags;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
4701da177e4SLinus Torvalds 
471210f8559SDavid Howells 	root_inode = affs_iget(sb, root_block);
472210f8559SDavid Howells 	if (IS_ERR(root_inode)) {
473210f8559SDavid Howells 		ret = PTR_ERR(root_inode);
474210f8559SDavid Howells 		goto out_error_noinode;
475210f8559SDavid Howells 	}
476210f8559SDavid Howells 
4771da177e4SLinus Torvalds 	sb->s_root = d_alloc_root(root_inode);
4781da177e4SLinus Torvalds 	if (!sb->s_root) {
4791da177e4SLinus Torvalds 		printk(KERN_ERR "AFFS: Get root inode failed\n");
4801da177e4SLinus Torvalds 		goto out_error;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 	sb->s_root->d_op = &affs_dentry_operations;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
4851da177e4SLinus Torvalds 	return 0;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/*
4881da177e4SLinus Torvalds 	 * Begin the cascaded cleanup ...
4891da177e4SLinus Torvalds 	 */
4901da177e4SLinus Torvalds out_error:
4911da177e4SLinus Torvalds 	if (root_inode)
4921da177e4SLinus Torvalds 		iput(root_inode);
493210f8559SDavid Howells out_error_noinode:
4941da177e4SLinus Torvalds 	kfree(sbi->s_bitmap);
4951da177e4SLinus Torvalds 	affs_brelse(root_bh);
4961da177e4SLinus Torvalds 	kfree(sbi->s_prefix);
4971da177e4SLinus Torvalds 	kfree(sbi);
4981da177e4SLinus Torvalds 	sb->s_fs_info = NULL;
499210f8559SDavid Howells 	return ret;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static int
5031da177e4SLinus Torvalds affs_remount(struct super_block *sb, int *flags, char *data)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
5061da177e4SLinus Torvalds 	int			 blocksize;
5071da177e4SLinus Torvalds 	uid_t			 uid;
5081da177e4SLinus Torvalds 	gid_t			 gid;
5091da177e4SLinus Torvalds 	int			 mode;
5101da177e4SLinus Torvalds 	int			 reserved;
5111da177e4SLinus Torvalds 	int			 root_block;
5121da177e4SLinus Torvalds 	unsigned long		 mount_flags;
5131da177e4SLinus Torvalds 	int			 res = 0;
514e9b3961bSMiklos Szeredi 	char			*new_opts = kstrdup(data, GFP_KERNEL);
51529333920SAl Viro 	char			 volume[32];
51629333920SAl Viro 	char			*prefix = NULL;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	*flags |= MS_NODIRATIME;
5211da177e4SLinus Torvalds 
52229333920SAl Viro 	memcpy(volume, sbi->s_volume, 32);
5231da177e4SLinus Torvalds 	if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
52429333920SAl Viro 			   &blocksize, &prefix, volume,
525e9b3961bSMiklos Szeredi 			   &mount_flags)) {
52629333920SAl Viro 		kfree(prefix);
527e9b3961bSMiklos Szeredi 		kfree(new_opts);
5281da177e4SLinus Torvalds 		return -EINVAL;
529e9b3961bSMiklos Szeredi 	}
530337eb00aSAlessio Igor Bogani 	lock_kernel();
5312a32cebdSAl Viro 	replace_mount_options(sb, new_opts);
532e9b3961bSMiklos Szeredi 
5331da177e4SLinus Torvalds 	sbi->s_flags = mount_flags;
5341da177e4SLinus Torvalds 	sbi->s_mode  = mode;
5351da177e4SLinus Torvalds 	sbi->s_uid   = uid;
5361da177e4SLinus Torvalds 	sbi->s_gid   = gid;
53729333920SAl Viro 	/* protect against readers */
53829333920SAl Viro 	spin_lock(&sbi->symlink_lock);
53929333920SAl Viro 	if (prefix) {
54029333920SAl Viro 		kfree(sbi->s_prefix);
54129333920SAl Viro 		sbi->s_prefix = prefix;
54229333920SAl Viro 	}
54329333920SAl Viro 	memcpy(sbi->s_volume, volume, 32);
54429333920SAl Viro 	spin_unlock(&sbi->symlink_lock);
5451da177e4SLinus Torvalds 
546337eb00aSAlessio Igor Bogani 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
547337eb00aSAlessio Igor Bogani 		unlock_kernel();
5481da177e4SLinus Torvalds 		return 0;
549337eb00aSAlessio Igor Bogani 	}
5501da177e4SLinus Torvalds 	if (*flags & MS_RDONLY) {
5511da177e4SLinus Torvalds 		affs_write_super(sb);
5521da177e4SLinus Torvalds 		affs_free_bitmap(sb);
5531da177e4SLinus Torvalds 	} else
5541da177e4SLinus Torvalds 		res = affs_init_bitmap(sb, flags);
5551da177e4SLinus Torvalds 
556337eb00aSAlessio Igor Bogani 	unlock_kernel();
5571da177e4SLinus Torvalds 	return res;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static int
561726c3342SDavid Howells affs_statfs(struct dentry *dentry, struct kstatfs *buf)
5621da177e4SLinus Torvalds {
563726c3342SDavid Howells 	struct super_block *sb = dentry->d_sb;
5641da177e4SLinus Torvalds 	int		 free;
565a6a2a73cSColy Li 	u64		 id = huge_encode_dev(sb->s_bdev->bd_dev);
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB(sb)->s_partition_size,
5681da177e4SLinus Torvalds 	     AFFS_SB(sb)->s_reserved);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	free          = affs_count_free_blocks(sb);
5711da177e4SLinus Torvalds 	buf->f_type    = AFFS_SUPER_MAGIC;
5721da177e4SLinus Torvalds 	buf->f_bsize   = sb->s_blocksize;
5731da177e4SLinus Torvalds 	buf->f_blocks  = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
5741da177e4SLinus Torvalds 	buf->f_bfree   = free;
5751da177e4SLinus Torvalds 	buf->f_bavail  = free;
576a6a2a73cSColy Li 	buf->f_fsid.val[0] = (u32)id;
577a6a2a73cSColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
578a6a2a73cSColy Li 	buf->f_namelen = 30;
5791da177e4SLinus Torvalds 	return 0;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
582454e2398SDavid Howells static int affs_get_sb(struct file_system_type *fs_type,
583454e2398SDavid Howells 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
5841da177e4SLinus Torvalds {
585454e2398SDavid Howells 	return get_sb_bdev(fs_type, flags, dev_name, data, affs_fill_super,
586454e2398SDavid Howells 			   mnt);
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds static struct file_system_type affs_fs_type = {
5901da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
5911da177e4SLinus Torvalds 	.name		= "affs",
5921da177e4SLinus Torvalds 	.get_sb		= affs_get_sb,
5931da177e4SLinus Torvalds 	.kill_sb	= kill_block_super,
5941da177e4SLinus Torvalds 	.fs_flags	= FS_REQUIRES_DEV,
5951da177e4SLinus Torvalds };
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static int __init init_affs_fs(void)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	int err = init_inodecache();
6001da177e4SLinus Torvalds 	if (err)
6011da177e4SLinus Torvalds 		goto out1;
6021da177e4SLinus Torvalds 	err = register_filesystem(&affs_fs_type);
6031da177e4SLinus Torvalds 	if (err)
6041da177e4SLinus Torvalds 		goto out;
6051da177e4SLinus Torvalds 	return 0;
6061da177e4SLinus Torvalds out:
6071da177e4SLinus Torvalds 	destroy_inodecache();
6081da177e4SLinus Torvalds out1:
6091da177e4SLinus Torvalds 	return err;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds static void __exit exit_affs_fs(void)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	unregister_filesystem(&affs_fs_type);
6151da177e4SLinus Torvalds 	destroy_inodecache();
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds MODULE_DESCRIPTION("Amiga filesystem support for Linux");
6191da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds module_init(init_affs_fs)
6221da177e4SLinus Torvalds module_exit(exit_affs_fs)
623