xref: /linux/fs/affs/super.c (revision 09c434b8a0047c69e48499de0107de312901e798)
1*09c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/affs/inode.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  (C) 1991  Linus Torvalds - minix filesystem
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/statfs.h>
171da177e4SLinus Torvalds #include <linux/parser.h>
18e18fa700SJeff Garzik #include <linux/magic.h>
19e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
205b825c3aSIngo Molnar #include <linux/cred.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
223dd84782SArtem Bityutskiy #include <linux/writeback.h>
23e852d82aSPranay Kr. Srivastava #include <linux/blkdev.h>
2426a7655eSDavid Howells #include <linux/seq_file.h>
259dffe569SJeff Layton #include <linux/iversion.h>
261da177e4SLinus Torvalds #include "affs.h"
271da177e4SLinus Torvalds 
28726c3342SDavid Howells static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
2926a7655eSDavid Howells static int affs_show_options(struct seq_file *m, struct dentry *root);
301da177e4SLinus Torvalds static int affs_remount (struct super_block *sb, int *flags, char *data);
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds static void
33bc86256dSArtem Bityutskiy affs_commit_super(struct super_block *sb, int wait)
34e2896436SChristoph Hellwig {
35e2896436SChristoph Hellwig 	struct affs_sb_info *sbi = AFFS_SB(sb);
36e2896436SChristoph Hellwig 	struct buffer_head *bh = sbi->s_root_bh;
37e2896436SChristoph Hellwig 	struct affs_root_tail *tail = AFFS_ROOT_TAIL(sb, bh);
38e2896436SChristoph Hellwig 
39a8371074SArtem Bityutskiy 	lock_buffer(bh);
40c1618208SFabian Frederick 	affs_secs_to_datestamp(ktime_get_real_seconds(), &tail->disk_change);
41e2896436SChristoph Hellwig 	affs_fix_checksum(sb, bh);
42a8371074SArtem Bityutskiy 	unlock_buffer(bh);
43a8371074SArtem Bityutskiy 
44e2896436SChristoph Hellwig 	mark_buffer_dirty(bh);
457435d506SArtem Bityutskiy 	if (wait)
467435d506SArtem Bityutskiy 		sync_dirty_buffer(bh);
47e2896436SChristoph Hellwig }
48e2896436SChristoph Hellwig 
49e2896436SChristoph Hellwig static void
501da177e4SLinus Torvalds affs_put_super(struct super_block *sb)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	struct affs_sb_info *sbi = AFFS_SB(sb);
539606d9aaSFabian Frederick 	pr_debug("%s()\n", __func__);
541da177e4SLinus Torvalds 
553dd84782SArtem Bityutskiy 	cancel_delayed_work_sync(&sbi->sb_work);
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
58e2896436SChristoph Hellwig static int
59e2896436SChristoph Hellwig affs_sync_fs(struct super_block *sb, int wait)
60e2896436SChristoph Hellwig {
61bc86256dSArtem Bityutskiy 	affs_commit_super(sb, wait);
62e2896436SChristoph Hellwig 	return 0;
63e2896436SChristoph Hellwig }
64e2896436SChristoph Hellwig 
653dd84782SArtem Bityutskiy static void flush_superblock(struct work_struct *work)
663dd84782SArtem Bityutskiy {
673dd84782SArtem Bityutskiy 	struct affs_sb_info *sbi;
683dd84782SArtem Bityutskiy 	struct super_block *sb;
693dd84782SArtem Bityutskiy 
703dd84782SArtem Bityutskiy 	sbi = container_of(work, struct affs_sb_info, sb_work.work);
713dd84782SArtem Bityutskiy 	sb = sbi->sb;
723dd84782SArtem Bityutskiy 
733dd84782SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
743dd84782SArtem Bityutskiy 	sbi->work_queued = 0;
753dd84782SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
763dd84782SArtem Bityutskiy 
773dd84782SArtem Bityutskiy 	affs_commit_super(sb, 1);
783dd84782SArtem Bityutskiy }
793dd84782SArtem Bityutskiy 
803dd84782SArtem Bityutskiy void affs_mark_sb_dirty(struct super_block *sb)
813dd84782SArtem Bityutskiy {
823dd84782SArtem Bityutskiy 	struct affs_sb_info *sbi = AFFS_SB(sb);
833dd84782SArtem Bityutskiy 	unsigned long delay;
843dd84782SArtem Bityutskiy 
85bc98a42cSDavid Howells 	if (sb_rdonly(sb))
863dd84782SArtem Bityutskiy 	       return;
873dd84782SArtem Bityutskiy 
883dd84782SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
893dd84782SArtem Bityutskiy 	if (!sbi->work_queued) {
903dd84782SArtem Bityutskiy 	       delay = msecs_to_jiffies(dirty_writeback_interval * 10);
913dd84782SArtem Bityutskiy 	       queue_delayed_work(system_long_wq, &sbi->sb_work, delay);
923dd84782SArtem Bityutskiy 	       sbi->work_queued = 1;
933dd84782SArtem Bityutskiy 	}
943dd84782SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
953dd84782SArtem Bityutskiy }
963dd84782SArtem Bityutskiy 
97e18b890bSChristoph Lameter static struct kmem_cache * affs_inode_cachep;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static struct inode *affs_alloc_inode(struct super_block *sb)
1001da177e4SLinus Torvalds {
101dca3c336SRoman Zippel 	struct affs_inode_info *i;
102dca3c336SRoman Zippel 
103dca3c336SRoman Zippel 	i = kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
104dca3c336SRoman Zippel 	if (!i)
1051da177e4SLinus Torvalds 		return NULL;
106dca3c336SRoman Zippel 
1079dffe569SJeff Layton 	inode_set_iversion(&i->vfs_inode, 1);
108dca3c336SRoman Zippel 	i->i_lc = NULL;
109dca3c336SRoman Zippel 	i->i_ext_bh = NULL;
110dca3c336SRoman Zippel 	i->i_pa_cnt = 0;
111dca3c336SRoman Zippel 
112dca3c336SRoman Zippel 	return &i->vfs_inode;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
115312a6791SAl Viro static void affs_free_inode(struct inode *inode)
116fa0d7e3dSNick Piggin {
117fa0d7e3dSNick Piggin 	kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
118fa0d7e3dSNick Piggin }
119fa0d7e3dSNick Piggin 
12051cc5068SAlexey Dobriyan static void init_once(void *foo)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	struct affs_inode_info *ei = (struct affs_inode_info *) foo;
1231da177e4SLinus Torvalds 
124756b0322SThomas Gleixner 	sema_init(&ei->i_link_lock, 1);
125756b0322SThomas Gleixner 	sema_init(&ei->i_ext_lock, 1);
1261da177e4SLinus Torvalds 	inode_init_once(&ei->vfs_inode);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129adbd319eSFabian Frederick static int __init init_inodecache(void)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	affs_inode_cachep = kmem_cache_create("affs_inode_cache",
1321da177e4SLinus Torvalds 					     sizeof(struct affs_inode_info),
133fffb60f9SPaul Jackson 					     0, (SLAB_RECLAIM_ACCOUNT|
1345d097056SVladimir Davydov 						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
13520c2df83SPaul Mundt 					     init_once);
1361da177e4SLinus Torvalds 	if (affs_inode_cachep == NULL)
1371da177e4SLinus Torvalds 		return -ENOMEM;
1381da177e4SLinus Torvalds 	return 0;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static void destroy_inodecache(void)
1421da177e4SLinus Torvalds {
1438c0a8537SKirill A. Shutemov 	/*
1448c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
1458c0a8537SKirill A. Shutemov 	 * destroy cache.
1468c0a8537SKirill A. Shutemov 	 */
1478c0a8537SKirill A. Shutemov 	rcu_barrier();
1481a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(affs_inode_cachep);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
151ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations affs_sops = {
1521da177e4SLinus Torvalds 	.alloc_inode	= affs_alloc_inode,
153312a6791SAl Viro 	.free_inode	= affs_free_inode,
1541da177e4SLinus Torvalds 	.write_inode	= affs_write_inode,
155f053dddeSAl Viro 	.evict_inode	= affs_evict_inode,
1561da177e4SLinus Torvalds 	.put_super	= affs_put_super,
157e2896436SChristoph Hellwig 	.sync_fs	= affs_sync_fs,
1581da177e4SLinus Torvalds 	.statfs		= affs_statfs,
1591da177e4SLinus Torvalds 	.remount_fs	= affs_remount,
16026a7655eSDavid Howells 	.show_options	= affs_show_options,
1611da177e4SLinus Torvalds };
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds enum {
1648ca57722SFabian Frederick 	Opt_bs, Opt_mode, Opt_mufs, Opt_notruncate, Opt_prefix, Opt_protect,
1651da177e4SLinus Torvalds 	Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
1661da177e4SLinus Torvalds 	Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
1671da177e4SLinus Torvalds };
1681da177e4SLinus Torvalds 
169a447c093SSteven Whitehouse static const match_table_t tokens = {
1701da177e4SLinus Torvalds 	{Opt_bs, "bs=%u"},
1711da177e4SLinus Torvalds 	{Opt_mode, "mode=%o"},
1721da177e4SLinus Torvalds 	{Opt_mufs, "mufs"},
1738ca57722SFabian Frederick 	{Opt_notruncate, "nofilenametruncate"},
1741da177e4SLinus Torvalds 	{Opt_prefix, "prefix=%s"},
1751da177e4SLinus Torvalds 	{Opt_protect, "protect"},
1761da177e4SLinus Torvalds 	{Opt_reserved, "reserved=%u"},
1771da177e4SLinus Torvalds 	{Opt_root, "root=%u"},
1781da177e4SLinus Torvalds 	{Opt_setgid, "setgid=%u"},
1791da177e4SLinus Torvalds 	{Opt_setuid, "setuid=%u"},
1801da177e4SLinus Torvalds 	{Opt_verbose, "verbose"},
1811da177e4SLinus Torvalds 	{Opt_volume, "volume=%s"},
1821da177e4SLinus Torvalds 	{Opt_ignore, "grpquota"},
1831da177e4SLinus Torvalds 	{Opt_ignore, "noquota"},
1841da177e4SLinus Torvalds 	{Opt_ignore, "quota"},
1851da177e4SLinus Torvalds 	{Opt_ignore, "usrquota"},
1861da177e4SLinus Torvalds 	{Opt_err, NULL},
1871da177e4SLinus Torvalds };
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds static int
1908fed10beSEric W. Biederman parse_options(char *options, kuid_t *uid, kgid_t *gid, int *mode, int *reserved, s32 *root,
1911da177e4SLinus Torvalds 		int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	char *p;
1941da177e4SLinus Torvalds 	substring_t args[MAX_OPT_ARGS];
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/* Fill in defaults */
1971da177e4SLinus Torvalds 
19821559981SDavid Howells 	*uid        = current_uid();
19921559981SDavid Howells 	*gid        = current_gid();
2001da177e4SLinus Torvalds 	*reserved   = 2;
2011da177e4SLinus Torvalds 	*root       = -1;
2021da177e4SLinus Torvalds 	*blocksize  = -1;
2031da177e4SLinus Torvalds 	volume[0]   = ':';
2041da177e4SLinus Torvalds 	volume[1]   = 0;
2051da177e4SLinus Torvalds 	*mount_opts = 0;
2061da177e4SLinus Torvalds 	if (!options)
2071da177e4SLinus Torvalds 		return 1;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	while ((p = strsep(&options, ",")) != NULL) {
2101da177e4SLinus Torvalds 		int token, n, option;
2111da177e4SLinus Torvalds 		if (!*p)
2121da177e4SLinus Torvalds 			continue;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 		token = match_token(p, tokens, args);
2151da177e4SLinus Torvalds 		switch (token) {
2161da177e4SLinus Torvalds 		case Opt_bs:
2171da177e4SLinus Torvalds 			if (match_int(&args[0], &n))
218217686e9SAl Viro 				return 0;
2191da177e4SLinus Torvalds 			if (n != 512 && n != 1024 && n != 2048
2201da177e4SLinus Torvalds 			    && n != 4096) {
2210158de12SFabian Frederick 				pr_warn("Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
2221da177e4SLinus Torvalds 				return 0;
2231da177e4SLinus Torvalds 			}
2241da177e4SLinus Torvalds 			*blocksize = n;
2251da177e4SLinus Torvalds 			break;
2261da177e4SLinus Torvalds 		case Opt_mode:
2271da177e4SLinus Torvalds 			if (match_octal(&args[0], &option))
228217686e9SAl Viro 				return 0;
2291da177e4SLinus Torvalds 			*mode = option & 0777;
23034f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETMODE);
2311da177e4SLinus Torvalds 			break;
2321da177e4SLinus Torvalds 		case Opt_mufs:
23334f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_MUFS);
2341da177e4SLinus Torvalds 			break;
2358ca57722SFabian Frederick 		case Opt_notruncate:
23634f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_NO_TRUNCATE);
2378ca57722SFabian Frederick 			break;
2381da177e4SLinus Torvalds 		case Opt_prefix:
23984ae6f82SChengguang Xu 			kfree(*prefix);
2401da177e4SLinus Torvalds 			*prefix = match_strdup(&args[0]);
2411da177e4SLinus Torvalds 			if (!*prefix)
2421da177e4SLinus Torvalds 				return 0;
24334f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_PREFIX);
2441da177e4SLinus Torvalds 			break;
2451da177e4SLinus Torvalds 		case Opt_protect:
24634f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_IMMUTABLE);
2471da177e4SLinus Torvalds 			break;
2481da177e4SLinus Torvalds 		case Opt_reserved:
2491da177e4SLinus Torvalds 			if (match_int(&args[0], reserved))
250217686e9SAl Viro 				return 0;
2511da177e4SLinus Torvalds 			break;
2521da177e4SLinus Torvalds 		case Opt_root:
2531da177e4SLinus Torvalds 			if (match_int(&args[0], root))
254217686e9SAl Viro 				return 0;
2551da177e4SLinus Torvalds 			break;
2561da177e4SLinus Torvalds 		case Opt_setgid:
2571da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
258217686e9SAl Viro 				return 0;
2598fed10beSEric W. Biederman 			*gid = make_kgid(current_user_ns(), option);
2608fed10beSEric W. Biederman 			if (!gid_valid(*gid))
2618fed10beSEric W. Biederman 				return 0;
26234f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETGID);
2631da177e4SLinus Torvalds 			break;
2641da177e4SLinus Torvalds 		case Opt_setuid:
2651da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
266217686e9SAl Viro 				return 0;
2678fed10beSEric W. Biederman 			*uid = make_kuid(current_user_ns(), option);
2688fed10beSEric W. Biederman 			if (!uid_valid(*uid))
2698fed10beSEric W. Biederman 				return 0;
27034f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETUID);
2711da177e4SLinus Torvalds 			break;
2721da177e4SLinus Torvalds 		case Opt_verbose:
27334f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_VERBOSE);
2741da177e4SLinus Torvalds 			break;
2751da177e4SLinus Torvalds 		case Opt_volume: {
2761da177e4SLinus Torvalds 			char *vol = match_strdup(&args[0]);
2776db27dd9SJim Meyering 			if (!vol)
2786db27dd9SJim Meyering 				return 0;
2791da177e4SLinus Torvalds 			strlcpy(volume, vol, 32);
2801da177e4SLinus Torvalds 			kfree(vol);
2811da177e4SLinus Torvalds 			break;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 		case Opt_ignore:
2841da177e4SLinus Torvalds 		 	/* Silently ignore the quota options */
2851da177e4SLinus Torvalds 			break;
2861da177e4SLinus Torvalds 		default:
2870158de12SFabian Frederick 			pr_warn("Unrecognized mount option \"%s\" or missing value\n",
2880158de12SFabian Frederick 				p);
2891da177e4SLinus Torvalds 			return 0;
2901da177e4SLinus Torvalds 		}
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	return 1;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
29526a7655eSDavid Howells static int affs_show_options(struct seq_file *m, struct dentry *root)
29626a7655eSDavid Howells {
29726a7655eSDavid Howells 	struct super_block *sb = root->d_sb;
29826a7655eSDavid Howells 	struct affs_sb_info *sbi = AFFS_SB(sb);
29926a7655eSDavid Howells 
30026a7655eSDavid Howells 	if (sb->s_blocksize)
30126a7655eSDavid Howells 		seq_printf(m, ",bs=%lu", sb->s_blocksize);
30226a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_SETMODE))
30326a7655eSDavid Howells 		seq_printf(m, ",mode=%o", sbi->s_mode);
30426a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_MUFS))
30526a7655eSDavid Howells 		seq_puts(m, ",mufs");
30626a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_NO_TRUNCATE))
30726a7655eSDavid Howells 		seq_puts(m, ",nofilenametruncate");
30826a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_PREFIX))
30926a7655eSDavid Howells 		seq_printf(m, ",prefix=%s", sbi->s_prefix);
31026a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_IMMUTABLE))
31126a7655eSDavid Howells 		seq_puts(m, ",protect");
31226a7655eSDavid Howells 	if (sbi->s_reserved != 2)
31326a7655eSDavid Howells 		seq_printf(m, ",reserved=%u", sbi->s_reserved);
31426a7655eSDavid Howells 	if (sbi->s_root_block != (sbi->s_reserved + sbi->s_partition_size - 1) / 2)
31526a7655eSDavid Howells 		seq_printf(m, ",root=%u", sbi->s_root_block);
31626a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_SETGID))
31726a7655eSDavid Howells 		seq_printf(m, ",setgid=%u",
31826a7655eSDavid Howells 			   from_kgid_munged(&init_user_ns, sbi->s_gid));
31926a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_SETUID))
32026a7655eSDavid Howells 		seq_printf(m, ",setuid=%u",
32126a7655eSDavid Howells 			   from_kuid_munged(&init_user_ns, sbi->s_uid));
32226a7655eSDavid Howells 	if (affs_test_opt(sbi->s_flags, SF_VERBOSE))
32326a7655eSDavid Howells 		seq_puts(m, ",verbose");
32426a7655eSDavid Howells 	if (sbi->s_volume[0])
32526a7655eSDavid Howells 		seq_printf(m, ",volume=%s", sbi->s_volume);
32626a7655eSDavid Howells 	return 0;
32726a7655eSDavid Howells }
32826a7655eSDavid Howells 
3291da177e4SLinus Torvalds /* This function definitely needs to be split up. Some fine day I'll
3301da177e4SLinus Torvalds  * hopefully have the guts to do so. Until then: sorry for the mess.
3311da177e4SLinus Torvalds  */
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds static int affs_fill_super(struct super_block *sb, void *data, int silent)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	struct affs_sb_info	*sbi;
3361da177e4SLinus Torvalds 	struct buffer_head	*root_bh = NULL;
3371da177e4SLinus Torvalds 	struct buffer_head	*boot_bh;
3381da177e4SLinus Torvalds 	struct inode		*root_inode = NULL;
3391da177e4SLinus Torvalds 	s32			 root_block;
3401da177e4SLinus Torvalds 	int			 size, blocksize;
3411da177e4SLinus Torvalds 	u32			 chksum;
3421da177e4SLinus Torvalds 	int			 num_bm;
3431da177e4SLinus Torvalds 	int			 i, j;
3448fed10beSEric W. Biederman 	kuid_t			 uid;
3458fed10beSEric W. Biederman 	kgid_t			 gid;
3461da177e4SLinus Torvalds 	int			 reserved;
3471da177e4SLinus Torvalds 	unsigned long		 mount_flags;
3481da177e4SLinus Torvalds 	int			 tmp_flags;	/* fix remount prototype... */
3492b943cf0SAl Viro 	u8			 sig[4];
350842a859dSAl Viro 	int			 ret;
3511da177e4SLinus Torvalds 
3529606d9aaSFabian Frederick 	pr_debug("read_super(%s)\n", data ? (const char *)data : "no options");
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	sb->s_magic             = AFFS_SUPER_MAGIC;
3551da177e4SLinus Torvalds 	sb->s_op                = &affs_sops;
3561751e8a6SLinus Torvalds 	sb->s_flags |= SB_NODIRATIME;
3571da177e4SLinus Torvalds 
358f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
3591da177e4SLinus Torvalds 	if (!sbi)
3601da177e4SLinus Torvalds 		return -ENOMEM;
36174c41429SJan Blunck 
3621da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
363a215fef7SArtem Bityutskiy 	sbi->sb = sb;
3647d135a5dSMatthias Kaehlcke 	mutex_init(&sbi->s_bmlock);
36529333920SAl Viro 	spin_lock_init(&sbi->symlink_lock);
3663dd84782SArtem Bityutskiy 	spin_lock_init(&sbi->work_lock);
3673dd84782SArtem Bityutskiy 	INIT_DELAYED_WORK(&sbi->sb_work, flush_superblock);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
3701da177e4SLinus Torvalds 				&blocksize,&sbi->s_prefix,
3711da177e4SLinus Torvalds 				sbi->s_volume, &mount_flags)) {
3720158de12SFabian Frederick 		pr_err("Error parsing options\n");
3731da177e4SLinus Torvalds 		return -EINVAL;
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 	/* N.B. after this point s_prefix must be released */
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	sbi->s_flags   = mount_flags;
3781da177e4SLinus Torvalds 	sbi->s_mode    = i;
3791da177e4SLinus Torvalds 	sbi->s_uid     = uid;
3801da177e4SLinus Torvalds 	sbi->s_gid     = gid;
3811da177e4SLinus Torvalds 	sbi->s_reserved= reserved;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	/* Get the size of the device in 512-byte blocks.
3841da177e4SLinus Torvalds 	 * If we later see that the partition uses bigger
3851da177e4SLinus Torvalds 	 * blocks, we will have to change it.
3861da177e4SLinus Torvalds 	 */
3871da177e4SLinus Torvalds 
388e852d82aSPranay Kr. Srivastava 	size = i_size_read(sb->s_bdev->bd_inode) >> 9;
3899606d9aaSFabian Frederick 	pr_debug("initial blocksize=%d, #blocks=%d\n", 512, size);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	affs_set_blocksize(sb, PAGE_SIZE);
3921da177e4SLinus Torvalds 	/* Try to find root block. Its location depends on the block size. */
3931da177e4SLinus Torvalds 
394e852d82aSPranay Kr. Srivastava 	i = bdev_logical_block_size(sb->s_bdev);
395e852d82aSPranay Kr. Srivastava 	j = PAGE_SIZE;
3961da177e4SLinus Torvalds 	if (blocksize > 0) {
3971da177e4SLinus Torvalds 		i = j = blocksize;
3981da177e4SLinus Torvalds 		size = size / (blocksize / 512);
3991da177e4SLinus Torvalds 	}
400e852d82aSPranay Kr. Srivastava 
4011e907f4fSFabian Frederick 	for (blocksize = i; blocksize <= j; blocksize <<= 1, size >>= 1) {
4021da177e4SLinus Torvalds 		sbi->s_root_block = root_block;
4031da177e4SLinus Torvalds 		if (root_block < 0)
4041da177e4SLinus Torvalds 			sbi->s_root_block = (reserved + size - 1) / 2;
4059606d9aaSFabian Frederick 		pr_debug("setting blocksize to %d\n", blocksize);
4061da177e4SLinus Torvalds 		affs_set_blocksize(sb, blocksize);
4071da177e4SLinus Torvalds 		sbi->s_partition_size = size;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		/* The root block location that was calculated above is not
4101da177e4SLinus Torvalds 		 * correct if the partition size is an odd number of 512-
4111da177e4SLinus Torvalds 		 * byte blocks, which will be rounded down to a number of
4121da177e4SLinus Torvalds 		 * 1024-byte blocks, and if there were an even number of
4131da177e4SLinus Torvalds 		 * reserved blocks. Ideally, all partition checkers should
4141da177e4SLinus Torvalds 		 * report the real number of blocks of the real blocksize,
4151da177e4SLinus Torvalds 		 * but since this just cannot be done, we have to try to
4161da177e4SLinus Torvalds 		 * find the root block anyways. In the above case, it is one
4171da177e4SLinus Torvalds 		 * block behind the calculated one. So we check this one, too.
4181da177e4SLinus Torvalds 		 */
4191da177e4SLinus Torvalds 		for (num_bm = 0; num_bm < 2; num_bm++) {
4209606d9aaSFabian Frederick 			pr_debug("Dev %s, trying root=%u, bs=%d, "
4211da177e4SLinus Torvalds 				"size=%d, reserved=%d\n",
4221da177e4SLinus Torvalds 				sb->s_id,
4231da177e4SLinus Torvalds 				sbi->s_root_block + num_bm,
4241da177e4SLinus Torvalds 				blocksize, size, reserved);
4251da177e4SLinus Torvalds 			root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
4261da177e4SLinus Torvalds 			if (!root_bh)
4271da177e4SLinus Torvalds 				continue;
4281da177e4SLinus Torvalds 			if (!affs_checksum_block(sb, root_bh) &&
4291da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
4301da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
4311da177e4SLinus Torvalds 				sbi->s_hashsize    = blocksize / 4 - 56;
4321da177e4SLinus Torvalds 				sbi->s_root_block += num_bm;
4331da177e4SLinus Torvalds 				goto got_root;
4341da177e4SLinus Torvalds 			}
4351da177e4SLinus Torvalds 			affs_brelse(root_bh);
4361da177e4SLinus Torvalds 			root_bh = NULL;
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 	if (!silent)
4400158de12SFabian Frederick 		pr_err("No valid root block on device %s\n", sb->s_id);
441842a859dSAl Viro 	return -EINVAL;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	/* N.B. after this point bh must be released */
4441da177e4SLinus Torvalds got_root:
445842a859dSAl Viro 	/* Keep super block in cache */
446842a859dSAl Viro 	sbi->s_root_bh = root_bh;
4471da177e4SLinus Torvalds 	root_block = sbi->s_root_block;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	/* Find out which kind of FS we have */
4501da177e4SLinus Torvalds 	boot_bh = sb_bread(sb, 0);
4511da177e4SLinus Torvalds 	if (!boot_bh) {
4520158de12SFabian Frederick 		pr_err("Cannot read boot block\n");
453842a859dSAl Viro 		return -EINVAL;
4541da177e4SLinus Torvalds 	}
4552b943cf0SAl Viro 	memcpy(sig, boot_bh->b_data, 4);
4561da177e4SLinus Torvalds 	brelse(boot_bh);
4572b943cf0SAl Viro 	chksum = be32_to_cpu(*(__be32 *)sig);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/* Dircache filesystems are compatible with non-dircache ones
4601da177e4SLinus Torvalds 	 * when reading. As long as they aren't supported, writing is
4611da177e4SLinus Torvalds 	 * not recommended.
4621da177e4SLinus Torvalds 	 */
4631da177e4SLinus Torvalds 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
464bc98a42cSDavid Howells 	     || chksum == MUFS_DCOFS) && !sb_rdonly(sb)) {
4650158de12SFabian Frederick 		pr_notice("Dircache FS - mounting %s read only\n", sb->s_id);
4661751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 	switch (chksum) {
4691da177e4SLinus Torvalds 	case MUFS_FS:
4701da177e4SLinus Torvalds 	case MUFS_INTLFFS:
4711da177e4SLinus Torvalds 	case MUFS_DCFFS:
47234f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4731da177e4SLinus Torvalds 		/* fall thru */
4741da177e4SLinus Torvalds 	case FS_INTLFFS:
4751da177e4SLinus Torvalds 	case FS_DCFFS:
47634f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_INTL);
4771da177e4SLinus Torvalds 		break;
4781da177e4SLinus Torvalds 	case MUFS_FFS:
47934f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4801da177e4SLinus Torvalds 		break;
4811da177e4SLinus Torvalds 	case FS_FFS:
4821da177e4SLinus Torvalds 		break;
4831da177e4SLinus Torvalds 	case MUFS_OFS:
48434f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4850a4c9265SGustavo A. R. Silva 		/* fall through */
4861da177e4SLinus Torvalds 	case FS_OFS:
48734f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_OFS);
4881751e8a6SLinus Torvalds 		sb->s_flags |= SB_NOEXEC;
4891da177e4SLinus Torvalds 		break;
4901da177e4SLinus Torvalds 	case MUFS_DCOFS:
4911da177e4SLinus Torvalds 	case MUFS_INTLOFS:
49234f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4930a4c9265SGustavo A. R. Silva 		/* fall through */
4941da177e4SLinus Torvalds 	case FS_DCOFS:
4951da177e4SLinus Torvalds 	case FS_INTLOFS:
49634f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_INTL);
49734f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_OFS);
4981751e8a6SLinus Torvalds 		sb->s_flags |= SB_NOEXEC;
4991da177e4SLinus Torvalds 		break;
5001da177e4SLinus Torvalds 	default:
5010158de12SFabian Frederick 		pr_err("Unknown filesystem on device %s: %08X\n",
5021da177e4SLinus Torvalds 		       sb->s_id, chksum);
503842a859dSAl Viro 		return -EINVAL;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 
50679bda4d5SFabian Frederick 	if (affs_test_opt(mount_flags, SF_VERBOSE)) {
5072b943cf0SAl Viro 		u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
5080158de12SFabian Frederick 		pr_notice("Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
5092b943cf0SAl Viro 			len > 31 ? 31 : len,
5101da177e4SLinus Torvalds 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
5112b943cf0SAl Viro 			sig, sig[3] + '0', blocksize);
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 
5141751e8a6SLinus Torvalds 	sb->s_flags |= SB_NODEV | SB_NOSUID;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	sbi->s_data_blksize = sb->s_blocksize;
51779bda4d5SFabian Frederick 	if (affs_test_opt(sbi->s_flags, SF_OFS))
5181da177e4SLinus Torvalds 		sbi->s_data_blksize -= 24;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	tmp_flags = sb->s_flags;
521842a859dSAl Viro 	ret = affs_init_bitmap(sb, &tmp_flags);
522842a859dSAl Viro 	if (ret)
523842a859dSAl Viro 		return ret;
5241da177e4SLinus Torvalds 	sb->s_flags = tmp_flags;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
5271da177e4SLinus Torvalds 
528210f8559SDavid Howells 	root_inode = affs_iget(sb, root_block);
529842a859dSAl Viro 	if (IS_ERR(root_inode))
530842a859dSAl Viro 		return PTR_ERR(root_inode);
531210f8559SDavid Howells 
53279bda4d5SFabian Frederick 	if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL))
533a129880dSAl Viro 		sb->s_d_op = &affs_intl_dentry_operations;
534a129880dSAl Viro 	else
535a129880dSAl Viro 		sb->s_d_op = &affs_dentry_operations;
536a129880dSAl Viro 
53748fde701SAl Viro 	sb->s_root = d_make_root(root_inode);
5381da177e4SLinus Torvalds 	if (!sb->s_root) {
5390158de12SFabian Frederick 		pr_err("AFFS: Get root inode failed\n");
540842a859dSAl Viro 		return -ENOMEM;
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds 
543ed4433d7SFabian Frederick 	sb->s_export_op = &affs_export_ops;
5449606d9aaSFabian Frederick 	pr_debug("s_flags=%lX\n", sb->s_flags);
5451da177e4SLinus Torvalds 	return 0;
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static int
5491da177e4SLinus Torvalds affs_remount(struct super_block *sb, int *flags, char *data)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
5521da177e4SLinus Torvalds 	int			 blocksize;
5538fed10beSEric W. Biederman 	kuid_t			 uid;
5548fed10beSEric W. Biederman 	kgid_t			 gid;
5551da177e4SLinus Torvalds 	int			 mode;
5561da177e4SLinus Torvalds 	int			 reserved;
5571da177e4SLinus Torvalds 	int			 root_block;
5581da177e4SLinus Torvalds 	unsigned long		 mount_flags;
5591da177e4SLinus Torvalds 	int			 res = 0;
560c8f33d0bSSanidhya Kashyap 	char			*new_opts;
56129333920SAl Viro 	char			 volume[32];
56229333920SAl Viro 	char			*prefix = NULL;
5631da177e4SLinus Torvalds 
564c8f33d0bSSanidhya Kashyap 	new_opts = kstrdup(data, GFP_KERNEL);
56501d6e087SMikulas Patocka 	if (data && !new_opts)
566c8f33d0bSSanidhya Kashyap 		return -ENOMEM;
567c8f33d0bSSanidhya Kashyap 
5689606d9aaSFabian Frederick 	pr_debug("%s(flags=0x%x,opts=\"%s\")\n", __func__, *flags, data);
5691da177e4SLinus Torvalds 
57002b9984dSTheodore Ts'o 	sync_filesystem(sb);
5711751e8a6SLinus Torvalds 	*flags |= SB_NODIRATIME;
5721da177e4SLinus Torvalds 
57329333920SAl Viro 	memcpy(volume, sbi->s_volume, 32);
5741da177e4SLinus Torvalds 	if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
57529333920SAl Viro 			   &blocksize, &prefix, volume,
576e9b3961bSMiklos Szeredi 			   &mount_flags)) {
57729333920SAl Viro 		kfree(prefix);
578e9b3961bSMiklos Szeredi 		kfree(new_opts);
5791da177e4SLinus Torvalds 		return -EINVAL;
580e9b3961bSMiklos Szeredi 	}
58174c41429SJan Blunck 
58243829731STejun Heo 	flush_delayed_work(&sbi->sb_work);
583e9b3961bSMiklos Szeredi 
5841da177e4SLinus Torvalds 	sbi->s_flags = mount_flags;
5851da177e4SLinus Torvalds 	sbi->s_mode  = mode;
5861da177e4SLinus Torvalds 	sbi->s_uid   = uid;
5871da177e4SLinus Torvalds 	sbi->s_gid   = gid;
58829333920SAl Viro 	/* protect against readers */
58929333920SAl Viro 	spin_lock(&sbi->symlink_lock);
59029333920SAl Viro 	if (prefix) {
59129333920SAl Viro 		kfree(sbi->s_prefix);
59229333920SAl Viro 		sbi->s_prefix = prefix;
59329333920SAl Viro 	}
59429333920SAl Viro 	memcpy(sbi->s_volume, volume, 32);
59529333920SAl Viro 	spin_unlock(&sbi->symlink_lock);
5961da177e4SLinus Torvalds 
5971751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
5981da177e4SLinus Torvalds 		return 0;
59974c41429SJan Blunck 
6001751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY)
6011da177e4SLinus Torvalds 		affs_free_bitmap(sb);
6020164b1a3SArtem Bityutskiy 	else
6031da177e4SLinus Torvalds 		res = affs_init_bitmap(sb, flags);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	return res;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds static int
609726c3342SDavid Howells affs_statfs(struct dentry *dentry, struct kstatfs *buf)
6101da177e4SLinus Torvalds {
611726c3342SDavid Howells 	struct super_block *sb = dentry->d_sb;
6121da177e4SLinus Torvalds 	int		 free;
613a6a2a73cSColy Li 	u64		 id = huge_encode_dev(sb->s_bdev->bd_dev);
6141da177e4SLinus Torvalds 
6159606d9aaSFabian Frederick 	pr_debug("%s() partsize=%d, reserved=%d\n",
6169606d9aaSFabian Frederick 		 __func__, AFFS_SB(sb)->s_partition_size,
6171da177e4SLinus Torvalds 		 AFFS_SB(sb)->s_reserved);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	free          = affs_count_free_blocks(sb);
6201da177e4SLinus Torvalds 	buf->f_type    = AFFS_SUPER_MAGIC;
6211da177e4SLinus Torvalds 	buf->f_bsize   = sb->s_blocksize;
6221da177e4SLinus Torvalds 	buf->f_blocks  = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
6231da177e4SLinus Torvalds 	buf->f_bfree   = free;
6241da177e4SLinus Torvalds 	buf->f_bavail  = free;
625a6a2a73cSColy Li 	buf->f_fsid.val[0] = (u32)id;
626a6a2a73cSColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
627f157853eSFabian Frederick 	buf->f_namelen = AFFSNAMEMAX;
6281da177e4SLinus Torvalds 	return 0;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
631152a0836SAl Viro static struct dentry *affs_mount(struct file_system_type *fs_type,
632152a0836SAl Viro 	int flags, const char *dev_name, void *data)
6331da177e4SLinus Torvalds {
634152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, affs_fill_super);
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
637842a859dSAl Viro static void affs_kill_sb(struct super_block *sb)
638842a859dSAl Viro {
639842a859dSAl Viro 	struct affs_sb_info *sbi = AFFS_SB(sb);
640842a859dSAl Viro 	kill_block_super(sb);
641842a859dSAl Viro 	if (sbi) {
642842a859dSAl Viro 		affs_free_bitmap(sb);
643842a859dSAl Viro 		affs_brelse(sbi->s_root_bh);
644842a859dSAl Viro 		kfree(sbi->s_prefix);
6454d29e571SFabian Frederick 		mutex_destroy(&sbi->s_bmlock);
646842a859dSAl Viro 		kfree(sbi);
647842a859dSAl Viro 	}
648842a859dSAl Viro }
649842a859dSAl Viro 
6501da177e4SLinus Torvalds static struct file_system_type affs_fs_type = {
6511da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
6521da177e4SLinus Torvalds 	.name		= "affs",
653152a0836SAl Viro 	.mount		= affs_mount,
654842a859dSAl Viro 	.kill_sb	= affs_kill_sb,
6551da177e4SLinus Torvalds 	.fs_flags	= FS_REQUIRES_DEV,
6561da177e4SLinus Torvalds };
6577f78e035SEric W. Biederman MODULE_ALIAS_FS("affs");
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds static int __init init_affs_fs(void)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	int err = init_inodecache();
6621da177e4SLinus Torvalds 	if (err)
6631da177e4SLinus Torvalds 		goto out1;
6641da177e4SLinus Torvalds 	err = register_filesystem(&affs_fs_type);
6651da177e4SLinus Torvalds 	if (err)
6661da177e4SLinus Torvalds 		goto out;
6671da177e4SLinus Torvalds 	return 0;
6681da177e4SLinus Torvalds out:
6691da177e4SLinus Torvalds 	destroy_inodecache();
6701da177e4SLinus Torvalds out1:
6711da177e4SLinus Torvalds 	return err;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds static void __exit exit_affs_fs(void)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	unregister_filesystem(&affs_fs_type);
6771da177e4SLinus Torvalds 	destroy_inodecache();
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds MODULE_DESCRIPTION("Amiga filesystem support for Linux");
6811da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds module_init(init_affs_fs)
6841da177e4SLinus Torvalds module_exit(exit_affs_fs)
685