xref: /linux/fs/affs/super.c (revision 0a4c92657f2543bb8fd480c5af90c6bb87f18e00)
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>
195b825c3aSIngo Molnar #include <linux/cred.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
213dd84782SArtem Bityutskiy #include <linux/writeback.h>
22e852d82aSPranay Kr. Srivastava #include <linux/blkdev.h>
2326a7655eSDavid Howells #include <linux/seq_file.h>
249dffe569SJeff Layton #include <linux/iversion.h>
251da177e4SLinus Torvalds #include "affs.h"
261da177e4SLinus Torvalds 
27726c3342SDavid Howells static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
2826a7655eSDavid Howells static int affs_show_options(struct seq_file *m, struct dentry *root);
291da177e4SLinus Torvalds static int affs_remount (struct super_block *sb, int *flags, char *data);
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds static void
32bc86256dSArtem Bityutskiy affs_commit_super(struct super_block *sb, int wait)
33e2896436SChristoph Hellwig {
34e2896436SChristoph Hellwig 	struct affs_sb_info *sbi = AFFS_SB(sb);
35e2896436SChristoph Hellwig 	struct buffer_head *bh = sbi->s_root_bh;
36e2896436SChristoph Hellwig 	struct affs_root_tail *tail = AFFS_ROOT_TAIL(sb, bh);
37e2896436SChristoph Hellwig 
38a8371074SArtem Bityutskiy 	lock_buffer(bh);
39c1618208SFabian Frederick 	affs_secs_to_datestamp(ktime_get_real_seconds(), &tail->disk_change);
40e2896436SChristoph Hellwig 	affs_fix_checksum(sb, bh);
41a8371074SArtem Bityutskiy 	unlock_buffer(bh);
42a8371074SArtem Bityutskiy 
43e2896436SChristoph Hellwig 	mark_buffer_dirty(bh);
447435d506SArtem Bityutskiy 	if (wait)
457435d506SArtem Bityutskiy 		sync_dirty_buffer(bh);
46e2896436SChristoph Hellwig }
47e2896436SChristoph Hellwig 
48e2896436SChristoph Hellwig static void
491da177e4SLinus Torvalds affs_put_super(struct super_block *sb)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	struct affs_sb_info *sbi = AFFS_SB(sb);
529606d9aaSFabian Frederick 	pr_debug("%s()\n", __func__);
531da177e4SLinus Torvalds 
543dd84782SArtem Bityutskiy 	cancel_delayed_work_sync(&sbi->sb_work);
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
57e2896436SChristoph Hellwig static int
58e2896436SChristoph Hellwig affs_sync_fs(struct super_block *sb, int wait)
59e2896436SChristoph Hellwig {
60bc86256dSArtem Bityutskiy 	affs_commit_super(sb, wait);
61e2896436SChristoph Hellwig 	return 0;
62e2896436SChristoph Hellwig }
63e2896436SChristoph Hellwig 
643dd84782SArtem Bityutskiy static void flush_superblock(struct work_struct *work)
653dd84782SArtem Bityutskiy {
663dd84782SArtem Bityutskiy 	struct affs_sb_info *sbi;
673dd84782SArtem Bityutskiy 	struct super_block *sb;
683dd84782SArtem Bityutskiy 
693dd84782SArtem Bityutskiy 	sbi = container_of(work, struct affs_sb_info, sb_work.work);
703dd84782SArtem Bityutskiy 	sb = sbi->sb;
713dd84782SArtem Bityutskiy 
723dd84782SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
733dd84782SArtem Bityutskiy 	sbi->work_queued = 0;
743dd84782SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
753dd84782SArtem Bityutskiy 
763dd84782SArtem Bityutskiy 	affs_commit_super(sb, 1);
773dd84782SArtem Bityutskiy }
783dd84782SArtem Bityutskiy 
793dd84782SArtem Bityutskiy void affs_mark_sb_dirty(struct super_block *sb)
803dd84782SArtem Bityutskiy {
813dd84782SArtem Bityutskiy 	struct affs_sb_info *sbi = AFFS_SB(sb);
823dd84782SArtem Bityutskiy 	unsigned long delay;
833dd84782SArtem Bityutskiy 
84bc98a42cSDavid Howells 	if (sb_rdonly(sb))
853dd84782SArtem Bityutskiy 	       return;
863dd84782SArtem Bityutskiy 
873dd84782SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
883dd84782SArtem Bityutskiy 	if (!sbi->work_queued) {
893dd84782SArtem Bityutskiy 	       delay = msecs_to_jiffies(dirty_writeback_interval * 10);
903dd84782SArtem Bityutskiy 	       queue_delayed_work(system_long_wq, &sbi->sb_work, delay);
913dd84782SArtem Bityutskiy 	       sbi->work_queued = 1;
923dd84782SArtem Bityutskiy 	}
933dd84782SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
943dd84782SArtem Bityutskiy }
953dd84782SArtem Bityutskiy 
96e18b890bSChristoph Lameter static struct kmem_cache * affs_inode_cachep;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds static struct inode *affs_alloc_inode(struct super_block *sb)
991da177e4SLinus Torvalds {
100dca3c336SRoman Zippel 	struct affs_inode_info *i;
101dca3c336SRoman Zippel 
102dca3c336SRoman Zippel 	i = kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
103dca3c336SRoman Zippel 	if (!i)
1041da177e4SLinus Torvalds 		return NULL;
105dca3c336SRoman Zippel 
1069dffe569SJeff Layton 	inode_set_iversion(&i->vfs_inode, 1);
107dca3c336SRoman Zippel 	i->i_lc = NULL;
108dca3c336SRoman Zippel 	i->i_ext_bh = NULL;
109dca3c336SRoman Zippel 	i->i_pa_cnt = 0;
110dca3c336SRoman Zippel 
111dca3c336SRoman Zippel 	return &i->vfs_inode;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
114fa0d7e3dSNick Piggin static void affs_i_callback(struct rcu_head *head)
115fa0d7e3dSNick Piggin {
116fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
117fa0d7e3dSNick Piggin 	kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
118fa0d7e3dSNick Piggin }
119fa0d7e3dSNick Piggin 
1201da177e4SLinus Torvalds static void affs_destroy_inode(struct inode *inode)
1211da177e4SLinus Torvalds {
122fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, affs_i_callback);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
12551cc5068SAlexey Dobriyan static void init_once(void *foo)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct affs_inode_info *ei = (struct affs_inode_info *) foo;
1281da177e4SLinus Torvalds 
129756b0322SThomas Gleixner 	sema_init(&ei->i_link_lock, 1);
130756b0322SThomas Gleixner 	sema_init(&ei->i_ext_lock, 1);
1311da177e4SLinus Torvalds 	inode_init_once(&ei->vfs_inode);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
134adbd319eSFabian Frederick static int __init init_inodecache(void)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	affs_inode_cachep = kmem_cache_create("affs_inode_cache",
1371da177e4SLinus Torvalds 					     sizeof(struct affs_inode_info),
138fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
1395d097056SVladimir Davydov 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
14020c2df83SPaul Mundt 					     init_once);
1411da177e4SLinus Torvalds 	if (affs_inode_cachep == NULL)
1421da177e4SLinus Torvalds 		return -ENOMEM;
1431da177e4SLinus Torvalds 	return 0;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static void destroy_inodecache(void)
1471da177e4SLinus Torvalds {
1488c0a8537SKirill A. Shutemov 	/*
1498c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
1508c0a8537SKirill A. Shutemov 	 * destroy cache.
1518c0a8537SKirill A. Shutemov 	 */
1528c0a8537SKirill A. Shutemov 	rcu_barrier();
1531a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(affs_inode_cachep);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
156ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations affs_sops = {
1571da177e4SLinus Torvalds 	.alloc_inode	= affs_alloc_inode,
1581da177e4SLinus Torvalds 	.destroy_inode	= affs_destroy_inode,
1591da177e4SLinus Torvalds 	.write_inode	= affs_write_inode,
160f053dddeSAl Viro 	.evict_inode	= affs_evict_inode,
1611da177e4SLinus Torvalds 	.put_super	= affs_put_super,
162e2896436SChristoph Hellwig 	.sync_fs	= affs_sync_fs,
1631da177e4SLinus Torvalds 	.statfs		= affs_statfs,
1641da177e4SLinus Torvalds 	.remount_fs	= affs_remount,
16526a7655eSDavid Howells 	.show_options	= affs_show_options,
1661da177e4SLinus Torvalds };
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds enum {
1698ca57722SFabian Frederick 	Opt_bs, Opt_mode, Opt_mufs, Opt_notruncate, Opt_prefix, Opt_protect,
1701da177e4SLinus Torvalds 	Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
1711da177e4SLinus Torvalds 	Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
1721da177e4SLinus Torvalds };
1731da177e4SLinus Torvalds 
174a447c093SSteven Whitehouse static const match_table_t tokens = {
1751da177e4SLinus Torvalds 	{Opt_bs, "bs=%u"},
1761da177e4SLinus Torvalds 	{Opt_mode, "mode=%o"},
1771da177e4SLinus Torvalds 	{Opt_mufs, "mufs"},
1788ca57722SFabian Frederick 	{Opt_notruncate, "nofilenametruncate"},
1791da177e4SLinus Torvalds 	{Opt_prefix, "prefix=%s"},
1801da177e4SLinus Torvalds 	{Opt_protect, "protect"},
1811da177e4SLinus Torvalds 	{Opt_reserved, "reserved=%u"},
1821da177e4SLinus Torvalds 	{Opt_root, "root=%u"},
1831da177e4SLinus Torvalds 	{Opt_setgid, "setgid=%u"},
1841da177e4SLinus Torvalds 	{Opt_setuid, "setuid=%u"},
1851da177e4SLinus Torvalds 	{Opt_verbose, "verbose"},
1861da177e4SLinus Torvalds 	{Opt_volume, "volume=%s"},
1871da177e4SLinus Torvalds 	{Opt_ignore, "grpquota"},
1881da177e4SLinus Torvalds 	{Opt_ignore, "noquota"},
1891da177e4SLinus Torvalds 	{Opt_ignore, "quota"},
1901da177e4SLinus Torvalds 	{Opt_ignore, "usrquota"},
1911da177e4SLinus Torvalds 	{Opt_err, NULL},
1921da177e4SLinus Torvalds };
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static int
1958fed10beSEric W. Biederman parse_options(char *options, kuid_t *uid, kgid_t *gid, int *mode, int *reserved, s32 *root,
1961da177e4SLinus Torvalds 		int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	char *p;
1991da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* Fill in defaults */
2021da177e4SLinus Torvalds 
20321559981SDavid Howells 	*uid        = current_uid();
20421559981SDavid Howells 	*gid        = current_gid();
2051da177e4SLinus Torvalds 	*reserved   = 2;
2061da177e4SLinus Torvalds 	*root       = -1;
2071da177e4SLinus Torvalds 	*blocksize  = -1;
2081da177e4SLinus Torvalds 	volume[0]   = ':';
2091da177e4SLinus Torvalds 	volume[1]   = 0;
2101da177e4SLinus Torvalds 	*mount_opts = 0;
2111da177e4SLinus Torvalds 	if (!options)
2121da177e4SLinus Torvalds 		return 1;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
2151da177e4SLinus Torvalds 		int token, n, option;
2161da177e4SLinus Torvalds 		if (!*p)
2171da177e4SLinus Torvalds 			continue;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 		token = match_token(p, tokens, args);
2201da177e4SLinus Torvalds 		switch (token) {
2211da177e4SLinus Torvalds 		case Opt_bs:
2221da177e4SLinus Torvalds 			if (match_int(&args[0], &n))
223217686e9SAl Viro 				return 0;
2241da177e4SLinus Torvalds 			if (n != 512 && n != 1024 && n != 2048
2251da177e4SLinus Torvalds 			    && n != 4096) {
2260158de12SFabian Frederick 				pr_warn("Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
2271da177e4SLinus Torvalds 				return 0;
2281da177e4SLinus Torvalds 			}
2291da177e4SLinus Torvalds 			*blocksize = n;
2301da177e4SLinus Torvalds 			break;
2311da177e4SLinus Torvalds 		case Opt_mode:
2321da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
233217686e9SAl Viro 				return 0;
2341da177e4SLinus Torvalds 			*mode = option & 0777;
23534f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETMODE);
2361da177e4SLinus Torvalds 			break;
2371da177e4SLinus Torvalds 		case Opt_mufs:
23834f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_MUFS);
2391da177e4SLinus Torvalds 			break;
2408ca57722SFabian Frederick 		case Opt_notruncate:
24134f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_NO_TRUNCATE);
2428ca57722SFabian Frederick 			break;
2431da177e4SLinus Torvalds 		case Opt_prefix:
24484ae6f82SChengguang Xu 			kfree(*prefix);
2451da177e4SLinus Torvalds 			*prefix = match_strdup(&args[0]);
2461da177e4SLinus Torvalds 			if (!*prefix)
2471da177e4SLinus Torvalds 				return 0;
24834f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_PREFIX);
2491da177e4SLinus Torvalds 			break;
2501da177e4SLinus Torvalds 		case Opt_protect:
25134f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_IMMUTABLE);
2521da177e4SLinus Torvalds 			break;
2531da177e4SLinus Torvalds 		case Opt_reserved:
2541da177e4SLinus Torvalds 			if (match_int(&args[0], reserved))
255217686e9SAl Viro 				return 0;
2561da177e4SLinus Torvalds 			break;
2571da177e4SLinus Torvalds 		case Opt_root:
2581da177e4SLinus Torvalds 			if (match_int(&args[0], root))
259217686e9SAl Viro 				return 0;
2601da177e4SLinus Torvalds 			break;
2611da177e4SLinus Torvalds 		case Opt_setgid:
2621da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
263217686e9SAl Viro 				return 0;
2648fed10beSEric W. Biederman 			*gid = make_kgid(current_user_ns(), option);
2658fed10beSEric W. Biederman 			if (!gid_valid(*gid))
2668fed10beSEric W. Biederman 				return 0;
26734f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETGID);
2681da177e4SLinus Torvalds 			break;
2691da177e4SLinus Torvalds 		case Opt_setuid:
2701da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
271217686e9SAl Viro 				return 0;
2728fed10beSEric W. Biederman 			*uid = make_kuid(current_user_ns(), option);
2738fed10beSEric W. Biederman 			if (!uid_valid(*uid))
2748fed10beSEric W. Biederman 				return 0;
27534f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETUID);
2761da177e4SLinus Torvalds 			break;
2771da177e4SLinus Torvalds 		case Opt_verbose:
27834f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_VERBOSE);
2791da177e4SLinus Torvalds 			break;
2801da177e4SLinus Torvalds 		case Opt_volume: {
2811da177e4SLinus Torvalds 			char *vol = match_strdup(&args[0]);
2826db27dd9SJim Meyering 			if (!vol)
2836db27dd9SJim Meyering 				return 0;
2841da177e4SLinus Torvalds 			strlcpy(volume, vol, 32);
2851da177e4SLinus Torvalds 			kfree(vol);
2861da177e4SLinus Torvalds 			break;
2871da177e4SLinus Torvalds 		}
2881da177e4SLinus Torvalds 		case Opt_ignore:
2891da177e4SLinus Torvalds 		 	/* Silently ignore the quota options */
2901da177e4SLinus Torvalds 			break;
2911da177e4SLinus Torvalds 		default:
2920158de12SFabian Frederick 			pr_warn("Unrecognized mount option \"%s\" or missing value\n",
2930158de12SFabian Frederick 				p);
2941da177e4SLinus Torvalds 			return 0;
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 	return 1;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
30026a7655eSDavid Howells static int affs_show_options(struct seq_file *m, struct dentry *root)
30126a7655eSDavid Howells {
30226a7655eSDavid Howells 	struct super_block *sb = root->d_sb;
30326a7655eSDavid Howells 	struct affs_sb_info *sbi = AFFS_SB(sb);
30426a7655eSDavid Howells 
30526a7655eSDavid Howells 	if (sb->s_blocksize)
30626a7655eSDavid Howells 		seq_printf(m, ",bs=%lu", sb->s_blocksize);
30726a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_SETMODE))
30826a7655eSDavid Howells 		seq_printf(m, ",mode=%o", sbi->s_mode);
30926a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_MUFS))
31026a7655eSDavid Howells 		seq_puts(m, ",mufs");
31126a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_NO_TRUNCATE))
31226a7655eSDavid Howells 		seq_puts(m, ",nofilenametruncate");
31326a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_PREFIX))
31426a7655eSDavid Howells 		seq_printf(m, ",prefix=%s", sbi->s_prefix);
31526a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_IMMUTABLE))
31626a7655eSDavid Howells 		seq_puts(m, ",protect");
31726a7655eSDavid Howells 	if (sbi->s_reserved != 2)
31826a7655eSDavid Howells 		seq_printf(m, ",reserved=%u", sbi->s_reserved);
31926a7655eSDavid Howells 	if (sbi->s_root_block != (sbi->s_reserved + sbi->s_partition_size - 1) / 2)
32026a7655eSDavid Howells 		seq_printf(m, ",root=%u", sbi->s_root_block);
32126a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_SETGID))
32226a7655eSDavid Howells 		seq_printf(m, ",setgid=%u",
32326a7655eSDavid Howells 			   from_kgid_munged(&init_user_ns, sbi->s_gid));
32426a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_SETUID))
32526a7655eSDavid Howells 		seq_printf(m, ",setuid=%u",
32626a7655eSDavid Howells 			   from_kuid_munged(&init_user_ns, sbi->s_uid));
32726a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_VERBOSE))
32826a7655eSDavid Howells 		seq_puts(m, ",verbose");
32926a7655eSDavid Howells 	if (sbi->s_volume[0])
33026a7655eSDavid Howells 		seq_printf(m, ",volume=%s", sbi->s_volume);
33126a7655eSDavid Howells 	return 0;
33226a7655eSDavid Howells }
33326a7655eSDavid Howells 
3341da177e4SLinus Torvalds /* This function definitely needs to be split up. Some fine day I'll
3351da177e4SLinus Torvalds  * hopefully have the guts to do so. Until then: sorry for the mess.
3361da177e4SLinus Torvalds  */
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static int affs_fill_super(struct super_block *sb, void *data, int silent)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	struct affs_sb_info	*sbi;
3411da177e4SLinus Torvalds 	struct buffer_head	*root_bh = NULL;
3421da177e4SLinus Torvalds 	struct buffer_head	*boot_bh;
3431da177e4SLinus Torvalds 	struct inode		*root_inode = NULL;
3441da177e4SLinus Torvalds 	s32			 root_block;
3451da177e4SLinus Torvalds 	int			 size, blocksize;
3461da177e4SLinus Torvalds 	u32			 chksum;
3471da177e4SLinus Torvalds 	int			 num_bm;
3481da177e4SLinus Torvalds 	int			 i, j;
3498fed10beSEric W. Biederman 	kuid_t			 uid;
3508fed10beSEric W. Biederman 	kgid_t			 gid;
3511da177e4SLinus Torvalds 	int			 reserved;
3521da177e4SLinus Torvalds 	unsigned long		 mount_flags;
3531da177e4SLinus Torvalds 	int			 tmp_flags;	/* fix remount prototype... */
3542b943cf0SAl Viro 	u8			 sig[4];
355842a859dSAl Viro 	int			 ret;
3561da177e4SLinus Torvalds 
3579606d9aaSFabian Frederick 	pr_debug("read_super(%s)\n", data ? (const char *)data : "no options");
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	sb->s_magic             = AFFS_SUPER_MAGIC;
3601da177e4SLinus Torvalds 	sb->s_op                = &affs_sops;
3611751e8a6SLinus Torvalds 	sb->s_flags |= SB_NODIRATIME;
3621da177e4SLinus Torvalds 
363f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
3641da177e4SLinus Torvalds 	if (!sbi)
3651da177e4SLinus Torvalds 		return -ENOMEM;
36674c41429SJan Blunck 
3671da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
368a215fef7SArtem Bityutskiy 	sbi->sb = sb;
3697d135a5dSMatthias Kaehlcke 	mutex_init(&sbi->s_bmlock);
37029333920SAl Viro 	spin_lock_init(&sbi->symlink_lock);
3713dd84782SArtem Bityutskiy 	spin_lock_init(&sbi->work_lock);
3723dd84782SArtem Bityutskiy 	INIT_DELAYED_WORK(&sbi->sb_work, flush_superblock);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
3751da177e4SLinus Torvalds 				&blocksize,&sbi->s_prefix,
3761da177e4SLinus Torvalds 				sbi->s_volume, &mount_flags)) {
3770158de12SFabian Frederick 		pr_err("Error parsing options\n");
3781da177e4SLinus Torvalds 		return -EINVAL;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 	/* N.B. after this point s_prefix must be released */
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	sbi->s_flags   = mount_flags;
3831da177e4SLinus Torvalds 	sbi->s_mode    = i;
3841da177e4SLinus Torvalds 	sbi->s_uid     = uid;
3851da177e4SLinus Torvalds 	sbi->s_gid     = gid;
3861da177e4SLinus Torvalds 	sbi->s_reserved= reserved;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	/* Get the size of the device in 512-byte blocks.
3891da177e4SLinus Torvalds 	 * If we later see that the partition uses bigger
3901da177e4SLinus Torvalds 	 * blocks, we will have to change it.
3911da177e4SLinus Torvalds 	 */
3921da177e4SLinus Torvalds 
393e852d82aSPranay Kr. Srivastava 	size = i_size_read(sb->s_bdev->bd_inode) >> 9;
3949606d9aaSFabian Frederick 	pr_debug("initial blocksize=%d, #blocks=%d\n", 512, size);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	affs_set_blocksize(sb, PAGE_SIZE);
3971da177e4SLinus Torvalds 	/* Try to find root block. Its location depends on the block size. */
3981da177e4SLinus Torvalds 
399e852d82aSPranay Kr. Srivastava 	i = bdev_logical_block_size(sb->s_bdev);
400e852d82aSPranay Kr. Srivastava 	j = PAGE_SIZE;
4011da177e4SLinus Torvalds 	if (blocksize > 0) {
4021da177e4SLinus Torvalds 		i = j = blocksize;
4031da177e4SLinus Torvalds 		size = size / (blocksize / 512);
4041da177e4SLinus Torvalds 	}
405e852d82aSPranay Kr. Srivastava 
4061e907f4fSFabian Frederick 	for (blocksize = i; blocksize <= j; blocksize <<= 1, size >>= 1) {
4071da177e4SLinus Torvalds 		sbi->s_root_block = root_block;
4081da177e4SLinus Torvalds 		if (root_block < 0)
4091da177e4SLinus Torvalds 			sbi->s_root_block = (reserved + size - 1) / 2;
4109606d9aaSFabian Frederick 		pr_debug("setting blocksize to %d\n", blocksize);
4111da177e4SLinus Torvalds 		affs_set_blocksize(sb, blocksize);
4121da177e4SLinus Torvalds 		sbi->s_partition_size = size;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 		/* The root block location that was calculated above is not
4151da177e4SLinus Torvalds 		 * correct if the partition size is an odd number of 512-
4161da177e4SLinus Torvalds 		 * byte blocks, which will be rounded down to a number of
4171da177e4SLinus Torvalds 		 * 1024-byte blocks, and if there were an even number of
4181da177e4SLinus Torvalds 		 * reserved blocks. Ideally, all partition checkers should
4191da177e4SLinus Torvalds 		 * report the real number of blocks of the real blocksize,
4201da177e4SLinus Torvalds 		 * but since this just cannot be done, we have to try to
4211da177e4SLinus Torvalds 		 * find the root block anyways. In the above case, it is one
4221da177e4SLinus Torvalds 		 * block behind the calculated one. So we check this one, too.
4231da177e4SLinus Torvalds 		 */
4241da177e4SLinus Torvalds 		for (num_bm = 0; num_bm < 2; num_bm++) {
4259606d9aaSFabian Frederick 			pr_debug("Dev %s, trying root=%u, bs=%d, "
4261da177e4SLinus Torvalds 				"size=%d, reserved=%d\n",
4271da177e4SLinus Torvalds 				sb->s_id,
4281da177e4SLinus Torvalds 				sbi->s_root_block + num_bm,
4291da177e4SLinus Torvalds 				blocksize, size, reserved);
4301da177e4SLinus Torvalds 			root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
4311da177e4SLinus Torvalds 			if (!root_bh)
4321da177e4SLinus Torvalds 				continue;
4331da177e4SLinus Torvalds 			if (!affs_checksum_block(sb, root_bh) &&
4341da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
4351da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
4361da177e4SLinus Torvalds 				sbi->s_hashsize    = blocksize / 4 - 56;
4371da177e4SLinus Torvalds 				sbi->s_root_block += num_bm;
4381da177e4SLinus Torvalds 				goto got_root;
4391da177e4SLinus Torvalds 			}
4401da177e4SLinus Torvalds 			affs_brelse(root_bh);
4411da177e4SLinus Torvalds 			root_bh = NULL;
4421da177e4SLinus Torvalds 		}
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 	if (!silent)
4450158de12SFabian Frederick 		pr_err("No valid root block on device %s\n", sb->s_id);
446842a859dSAl Viro 	return -EINVAL;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	/* N.B. after this point bh must be released */
4491da177e4SLinus Torvalds got_root:
450842a859dSAl Viro 	/* Keep super block in cache */
451842a859dSAl Viro 	sbi->s_root_bh = root_bh;
4521da177e4SLinus Torvalds 	root_block = sbi->s_root_block;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	/* Find out which kind of FS we have */
4551da177e4SLinus Torvalds 	boot_bh = sb_bread(sb, 0);
4561da177e4SLinus Torvalds 	if (!boot_bh) {
4570158de12SFabian Frederick 		pr_err("Cannot read boot block\n");
458842a859dSAl Viro 		return -EINVAL;
4591da177e4SLinus Torvalds 	}
4602b943cf0SAl Viro 	memcpy(sig, boot_bh->b_data, 4);
4611da177e4SLinus Torvalds 	brelse(boot_bh);
4622b943cf0SAl Viro 	chksum = be32_to_cpu(*(__be32 *)sig);
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	/* Dircache filesystems are compatible with non-dircache ones
4651da177e4SLinus Torvalds 	 * when reading. As long as they aren't supported, writing is
4661da177e4SLinus Torvalds 	 * not recommended.
4671da177e4SLinus Torvalds 	 */
4681da177e4SLinus Torvalds 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
469bc98a42cSDavid Howells 	     || chksum == MUFS_DCOFS) && !sb_rdonly(sb)) {
4700158de12SFabian Frederick 		pr_notice("Dircache FS - mounting %s read only\n", sb->s_id);
4711751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 	switch (chksum) {
4741da177e4SLinus Torvalds 	case MUFS_FS:
4751da177e4SLinus Torvalds 	case MUFS_INTLFFS:
4761da177e4SLinus Torvalds 	case MUFS_DCFFS:
47734f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4781da177e4SLinus Torvalds 		/* fall thru */
4791da177e4SLinus Torvalds 	case FS_INTLFFS:
4801da177e4SLinus Torvalds 	case FS_DCFFS:
48134f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_INTL);
4821da177e4SLinus Torvalds 		break;
4831da177e4SLinus Torvalds 	case MUFS_FFS:
48434f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4851da177e4SLinus Torvalds 		break;
4861da177e4SLinus Torvalds 	case FS_FFS:
4871da177e4SLinus Torvalds 		break;
4881da177e4SLinus Torvalds 	case MUFS_OFS:
48934f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
490*0a4c9265SGustavo A. R. Silva 		/* fall through */
4911da177e4SLinus Torvalds 	case FS_OFS:
49234f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_OFS);
4931751e8a6SLinus Torvalds 		sb->s_flags |= SB_NOEXEC;
4941da177e4SLinus Torvalds 		break;
4951da177e4SLinus Torvalds 	case MUFS_DCOFS:
4961da177e4SLinus Torvalds 	case MUFS_INTLOFS:
49734f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
498*0a4c9265SGustavo A. R. Silva 		/* fall through */
4991da177e4SLinus Torvalds 	case FS_DCOFS:
5001da177e4SLinus Torvalds 	case FS_INTLOFS:
50134f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_INTL);
50234f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_OFS);
5031751e8a6SLinus Torvalds 		sb->s_flags |= SB_NOEXEC;
5041da177e4SLinus Torvalds 		break;
5051da177e4SLinus Torvalds 	default:
5060158de12SFabian Frederick 		pr_err("Unknown filesystem on device %s: %08X\n",
5071da177e4SLinus Torvalds 		       sb->s_id, chksum);
508842a859dSAl Viro 		return -EINVAL;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
51179bda4d5SFabian Frederick 	if (affs_test_opt(mount_flags, SF_VERBOSE)) {
5122b943cf0SAl Viro 		u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
5130158de12SFabian Frederick 		pr_notice("Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
5142b943cf0SAl Viro 			len > 31 ? 31 : len,
5151da177e4SLinus Torvalds 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
5162b943cf0SAl Viro 			sig, sig[3] + '0', blocksize);
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 
5191751e8a6SLinus Torvalds 	sb->s_flags |= SB_NODEV | SB_NOSUID;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	sbi->s_data_blksize = sb->s_blocksize;
52279bda4d5SFabian Frederick 	if (affs_test_opt(sbi->s_flags, SF_OFS))
5231da177e4SLinus Torvalds 		sbi->s_data_blksize -= 24;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	tmp_flags = sb->s_flags;
526842a859dSAl Viro 	ret = affs_init_bitmap(sb, &tmp_flags);
527842a859dSAl Viro 	if (ret)
528842a859dSAl Viro 		return ret;
5291da177e4SLinus Torvalds 	sb->s_flags = tmp_flags;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
5321da177e4SLinus Torvalds 
533210f8559SDavid Howells 	root_inode = affs_iget(sb, root_block);
534842a859dSAl Viro 	if (IS_ERR(root_inode))
535842a859dSAl Viro 		return PTR_ERR(root_inode);
536210f8559SDavid Howells 
53779bda4d5SFabian Frederick 	if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL))
538a129880dSAl Viro 		sb->s_d_op = &affs_intl_dentry_operations;
539a129880dSAl Viro 	else
540a129880dSAl Viro 		sb->s_d_op = &affs_dentry_operations;
541a129880dSAl Viro 
54248fde701SAl Viro 	sb->s_root = d_make_root(root_inode);
5431da177e4SLinus Torvalds 	if (!sb->s_root) {
5440158de12SFabian Frederick 		pr_err("AFFS: Get root inode failed\n");
545842a859dSAl Viro 		return -ENOMEM;
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 
548ed4433d7SFabian Frederick 	sb->s_export_op = &affs_export_ops;
5499606d9aaSFabian Frederick 	pr_debug("s_flags=%lX\n", sb->s_flags);
5501da177e4SLinus Torvalds 	return 0;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds static int
5541da177e4SLinus Torvalds affs_remount(struct super_block *sb, int *flags, char *data)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
5571da177e4SLinus Torvalds 	int			 blocksize;
5588fed10beSEric W. Biederman 	kuid_t			 uid;
5598fed10beSEric W. Biederman 	kgid_t			 gid;
5601da177e4SLinus Torvalds 	int			 mode;
5611da177e4SLinus Torvalds 	int			 reserved;
5621da177e4SLinus Torvalds 	int			 root_block;
5631da177e4SLinus Torvalds 	unsigned long		 mount_flags;
5641da177e4SLinus Torvalds 	int			 res = 0;
565c8f33d0bSSanidhya Kashyap 	char			*new_opts;
56629333920SAl Viro 	char			 volume[32];
56729333920SAl Viro 	char			*prefix = NULL;
5681da177e4SLinus Torvalds 
569c8f33d0bSSanidhya Kashyap 	new_opts = kstrdup(data, GFP_KERNEL);
57001d6e087SMikulas Patocka 	if (data && !new_opts)
571c8f33d0bSSanidhya Kashyap 		return -ENOMEM;
572c8f33d0bSSanidhya Kashyap 
5739606d9aaSFabian Frederick 	pr_debug("%s(flags=0x%x,opts=\"%s\")\n", __func__, *flags, data);
5741da177e4SLinus Torvalds 
57502b9984dSTheodore Ts'o 	sync_filesystem(sb);
5761751e8a6SLinus Torvalds 	*flags |= SB_NODIRATIME;
5771da177e4SLinus Torvalds 
57829333920SAl Viro 	memcpy(volume, sbi->s_volume, 32);
5791da177e4SLinus Torvalds 	if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
58029333920SAl Viro 			   &blocksize, &prefix, volume,
581e9b3961bSMiklos Szeredi 			   &mount_flags)) {
58229333920SAl Viro 		kfree(prefix);
583e9b3961bSMiklos Szeredi 		kfree(new_opts);
5841da177e4SLinus Torvalds 		return -EINVAL;
585e9b3961bSMiklos Szeredi 	}
58674c41429SJan Blunck 
58743829731STejun Heo 	flush_delayed_work(&sbi->sb_work);
588e9b3961bSMiklos Szeredi 
5891da177e4SLinus Torvalds 	sbi->s_flags = mount_flags;
5901da177e4SLinus Torvalds 	sbi->s_mode  = mode;
5911da177e4SLinus Torvalds 	sbi->s_uid   = uid;
5921da177e4SLinus Torvalds 	sbi->s_gid   = gid;
59329333920SAl Viro 	/* protect against readers */
59429333920SAl Viro 	spin_lock(&sbi->symlink_lock);
59529333920SAl Viro 	if (prefix) {
59629333920SAl Viro 		kfree(sbi->s_prefix);
59729333920SAl Viro 		sbi->s_prefix = prefix;
59829333920SAl Viro 	}
59929333920SAl Viro 	memcpy(sbi->s_volume, volume, 32);
60029333920SAl Viro 	spin_unlock(&sbi->symlink_lock);
6011da177e4SLinus Torvalds 
6021751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
6031da177e4SLinus Torvalds 		return 0;
60474c41429SJan Blunck 
6051751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY)
6061da177e4SLinus Torvalds 		affs_free_bitmap(sb);
6070164b1a3SArtem Bityutskiy 	else
6081da177e4SLinus Torvalds 		res = affs_init_bitmap(sb, flags);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	return res;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds static int
614726c3342SDavid Howells affs_statfs(struct dentry *dentry, struct kstatfs *buf)
6151da177e4SLinus Torvalds {
616726c3342SDavid Howells 	struct super_block *sb = dentry->d_sb;
6171da177e4SLinus Torvalds 	int		 free;
618a6a2a73cSColy Li 	u64		 id = huge_encode_dev(sb->s_bdev->bd_dev);
6191da177e4SLinus Torvalds 
6209606d9aaSFabian Frederick 	pr_debug("%s() partsize=%d, reserved=%d\n",
6219606d9aaSFabian Frederick 		 __func__, AFFS_SB(sb)->s_partition_size,
6221da177e4SLinus Torvalds 		 AFFS_SB(sb)->s_reserved);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	free          = affs_count_free_blocks(sb);
6251da177e4SLinus Torvalds 	buf->f_type    = AFFS_SUPER_MAGIC;
6261da177e4SLinus Torvalds 	buf->f_bsize   = sb->s_blocksize;
6271da177e4SLinus Torvalds 	buf->f_blocks  = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
6281da177e4SLinus Torvalds 	buf->f_bfree   = free;
6291da177e4SLinus Torvalds 	buf->f_bavail  = free;
630a6a2a73cSColy Li 	buf->f_fsid.val[0] = (u32)id;
631a6a2a73cSColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
632f157853eSFabian Frederick 	buf->f_namelen = AFFSNAMEMAX;
6331da177e4SLinus Torvalds 	return 0;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
636152a0836SAl Viro static struct dentry *affs_mount(struct file_system_type *fs_type,
637152a0836SAl Viro 	int flags, const char *dev_name, void *data)
6381da177e4SLinus Torvalds {
639152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, affs_fill_super);
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
642842a859dSAl Viro static void affs_kill_sb(struct super_block *sb)
643842a859dSAl Viro {
644842a859dSAl Viro 	struct affs_sb_info *sbi = AFFS_SB(sb);
645842a859dSAl Viro 	kill_block_super(sb);
646842a859dSAl Viro 	if (sbi) {
647842a859dSAl Viro 		affs_free_bitmap(sb);
648842a859dSAl Viro 		affs_brelse(sbi->s_root_bh);
649842a859dSAl Viro 		kfree(sbi->s_prefix);
6504d29e571SFabian Frederick 		mutex_destroy(&sbi->s_bmlock);
651842a859dSAl Viro 		kfree(sbi);
652842a859dSAl Viro 	}
653842a859dSAl Viro }
654842a859dSAl Viro 
6551da177e4SLinus Torvalds static struct file_system_type affs_fs_type = {
6561da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
6571da177e4SLinus Torvalds 	.name		= "affs",
658152a0836SAl Viro 	.mount		= affs_mount,
659842a859dSAl Viro 	.kill_sb	= affs_kill_sb,
6601da177e4SLinus Torvalds 	.fs_flags	= FS_REQUIRES_DEV,
6611da177e4SLinus Torvalds };
6627f78e035SEric W. Biederman MODULE_ALIAS_FS("affs");
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds static int __init init_affs_fs(void)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	int err = init_inodecache();
6671da177e4SLinus Torvalds 	if (err)
6681da177e4SLinus Torvalds 		goto out1;
6691da177e4SLinus Torvalds 	err = register_filesystem(&affs_fs_type);
6701da177e4SLinus Torvalds 	if (err)
6711da177e4SLinus Torvalds 		goto out;
6721da177e4SLinus Torvalds 	return 0;
6731da177e4SLinus Torvalds out:
6741da177e4SLinus Torvalds 	destroy_inodecache();
6751da177e4SLinus Torvalds out1:
6761da177e4SLinus Torvalds 	return err;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds static void __exit exit_affs_fs(void)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	unregister_filesystem(&affs_fs_type);
6821da177e4SLinus Torvalds 	destroy_inodecache();
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds MODULE_DESCRIPTION("Amiga filesystem support for Linux");
6861da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds module_init(init_affs_fs)
6891da177e4SLinus Torvalds module_exit(exit_affs_fs)
690