xref: /linux/fs/affs/super.c (revision c8f33d0bec999a4f2b5c3f9380361b88ce6f6ab0)
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>
195a0e3ad6STejun Heo #include <linux/slab.h>
203dd84782SArtem Bityutskiy #include <linux/writeback.h>
211da177e4SLinus Torvalds #include "affs.h"
221da177e4SLinus Torvalds 
23726c3342SDavid Howells static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
241da177e4SLinus Torvalds static int affs_remount (struct super_block *sb, int *flags, char *data);
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static void
27bc86256dSArtem Bityutskiy affs_commit_super(struct super_block *sb, int wait)
28e2896436SChristoph Hellwig {
29e2896436SChristoph Hellwig 	struct affs_sb_info *sbi = AFFS_SB(sb);
30e2896436SChristoph Hellwig 	struct buffer_head *bh = sbi->s_root_bh;
31e2896436SChristoph Hellwig 	struct affs_root_tail *tail = AFFS_ROOT_TAIL(sb, bh);
32e2896436SChristoph Hellwig 
33a8371074SArtem Bityutskiy 	lock_buffer(bh);
34e2896436SChristoph Hellwig 	secs_to_datestamp(get_seconds(), &tail->disk_change);
35e2896436SChristoph Hellwig 	affs_fix_checksum(sb, bh);
36a8371074SArtem Bityutskiy 	unlock_buffer(bh);
37a8371074SArtem Bityutskiy 
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);
479606d9aaSFabian Frederick 	pr_debug("%s()\n", __func__);
481da177e4SLinus Torvalds 
493dd84782SArtem Bityutskiy 	cancel_delayed_work_sync(&sbi->sb_work);
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
52e2896436SChristoph Hellwig static int
53e2896436SChristoph Hellwig affs_sync_fs(struct super_block *sb, int wait)
54e2896436SChristoph Hellwig {
55bc86256dSArtem Bityutskiy 	affs_commit_super(sb, wait);
56e2896436SChristoph Hellwig 	return 0;
57e2896436SChristoph Hellwig }
58e2896436SChristoph Hellwig 
593dd84782SArtem Bityutskiy static void flush_superblock(struct work_struct *work)
603dd84782SArtem Bityutskiy {
613dd84782SArtem Bityutskiy 	struct affs_sb_info *sbi;
623dd84782SArtem Bityutskiy 	struct super_block *sb;
633dd84782SArtem Bityutskiy 
643dd84782SArtem Bityutskiy 	sbi = container_of(work, struct affs_sb_info, sb_work.work);
653dd84782SArtem Bityutskiy 	sb = sbi->sb;
663dd84782SArtem Bityutskiy 
673dd84782SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
683dd84782SArtem Bityutskiy 	sbi->work_queued = 0;
693dd84782SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
703dd84782SArtem Bityutskiy 
713dd84782SArtem Bityutskiy 	affs_commit_super(sb, 1);
723dd84782SArtem Bityutskiy }
733dd84782SArtem Bityutskiy 
743dd84782SArtem Bityutskiy void affs_mark_sb_dirty(struct super_block *sb)
753dd84782SArtem Bityutskiy {
763dd84782SArtem Bityutskiy 	struct affs_sb_info *sbi = AFFS_SB(sb);
773dd84782SArtem Bityutskiy 	unsigned long delay;
783dd84782SArtem Bityutskiy 
793dd84782SArtem Bityutskiy 	if (sb->s_flags & MS_RDONLY)
803dd84782SArtem Bityutskiy 	       return;
813dd84782SArtem Bityutskiy 
823dd84782SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
833dd84782SArtem Bityutskiy 	if (!sbi->work_queued) {
843dd84782SArtem Bityutskiy 	       delay = msecs_to_jiffies(dirty_writeback_interval * 10);
853dd84782SArtem Bityutskiy 	       queue_delayed_work(system_long_wq, &sbi->sb_work, delay);
863dd84782SArtem Bityutskiy 	       sbi->work_queued = 1;
873dd84782SArtem Bityutskiy 	}
883dd84782SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
893dd84782SArtem Bityutskiy }
903dd84782SArtem Bityutskiy 
91e18b890bSChristoph Lameter static struct kmem_cache * affs_inode_cachep;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static struct inode *affs_alloc_inode(struct super_block *sb)
941da177e4SLinus Torvalds {
95dca3c336SRoman Zippel 	struct affs_inode_info *i;
96dca3c336SRoman Zippel 
97dca3c336SRoman Zippel 	i = kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
98dca3c336SRoman Zippel 	if (!i)
991da177e4SLinus Torvalds 		return NULL;
100dca3c336SRoman Zippel 
101dca3c336SRoman Zippel 	i->vfs_inode.i_version = 1;
102dca3c336SRoman Zippel 	i->i_lc = NULL;
103dca3c336SRoman Zippel 	i->i_ext_bh = NULL;
104dca3c336SRoman Zippel 	i->i_pa_cnt = 0;
105dca3c336SRoman Zippel 
106dca3c336SRoman Zippel 	return &i->vfs_inode;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
109fa0d7e3dSNick Piggin static void affs_i_callback(struct rcu_head *head)
110fa0d7e3dSNick Piggin {
111fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
112fa0d7e3dSNick Piggin 	kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
113fa0d7e3dSNick Piggin }
114fa0d7e3dSNick Piggin 
1151da177e4SLinus Torvalds static void affs_destroy_inode(struct inode *inode)
1161da177e4SLinus Torvalds {
117fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, affs_i_callback);
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
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|
134fffb60f9SPaul Jackson 						SLAB_MEM_SPREAD),
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,
1531da177e4SLinus Torvalds 	.destroy_inode	= affs_destroy_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,
160e9b3961bSMiklos Szeredi 	.show_options	= generic_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:
2391da177e4SLinus Torvalds 			*prefix = match_strdup(&args[0]);
2401da177e4SLinus Torvalds 			if (!*prefix)
2411da177e4SLinus Torvalds 				return 0;
24234f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_PREFIX);
2431da177e4SLinus Torvalds 			break;
2441da177e4SLinus Torvalds 		case Opt_protect:
24534f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_IMMUTABLE);
2461da177e4SLinus Torvalds 			break;
2471da177e4SLinus Torvalds 		case Opt_reserved:
2481da177e4SLinus Torvalds 			if (match_int(&args[0], reserved))
249217686e9SAl Viro 				return 0;
2501da177e4SLinus Torvalds 			break;
2511da177e4SLinus Torvalds 		case Opt_root:
2521da177e4SLinus Torvalds 			if (match_int(&args[0], root))
253217686e9SAl Viro 				return 0;
2541da177e4SLinus Torvalds 			break;
2551da177e4SLinus Torvalds 		case Opt_setgid:
2561da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
257217686e9SAl Viro 				return 0;
2588fed10beSEric W. Biederman 			*gid = make_kgid(current_user_ns(), option);
2598fed10beSEric W. Biederman 			if (!gid_valid(*gid))
2608fed10beSEric W. Biederman 				return 0;
26134f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETGID);
2621da177e4SLinus Torvalds 			break;
2631da177e4SLinus Torvalds 		case Opt_setuid:
2641da177e4SLinus Torvalds 			if (match_int(&args[0], &option))
265217686e9SAl Viro 				return 0;
2668fed10beSEric W. Biederman 			*uid = make_kuid(current_user_ns(), option);
2678fed10beSEric W. Biederman 			if (!uid_valid(*uid))
2688fed10beSEric W. Biederman 				return 0;
26934f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_SETUID);
2701da177e4SLinus Torvalds 			break;
2711da177e4SLinus Torvalds 		case Opt_verbose:
27234f24835SFabian Frederick 			affs_set_opt(*mount_opts, SF_VERBOSE);
2731da177e4SLinus Torvalds 			break;
2741da177e4SLinus Torvalds 		case Opt_volume: {
2751da177e4SLinus Torvalds 			char *vol = match_strdup(&args[0]);
2766db27dd9SJim Meyering 			if (!vol)
2776db27dd9SJim Meyering 				return 0;
2781da177e4SLinus Torvalds 			strlcpy(volume, vol, 32);
2791da177e4SLinus Torvalds 			kfree(vol);
2801da177e4SLinus Torvalds 			break;
2811da177e4SLinus Torvalds 		}
2821da177e4SLinus Torvalds 		case Opt_ignore:
2831da177e4SLinus Torvalds 		 	/* Silently ignore the quota options */
2841da177e4SLinus Torvalds 			break;
2851da177e4SLinus Torvalds 		default:
2860158de12SFabian Frederick 			pr_warn("Unrecognized mount option \"%s\" or missing value\n",
2870158de12SFabian Frederick 				p);
2881da177e4SLinus Torvalds 			return 0;
2891da177e4SLinus Torvalds 		}
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	return 1;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /* This function definitely needs to be split up. Some fine day I'll
2951da177e4SLinus Torvalds  * hopefully have the guts to do so. Until then: sorry for the mess.
2961da177e4SLinus Torvalds  */
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static int affs_fill_super(struct super_block *sb, void *data, int silent)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct affs_sb_info	*sbi;
3011da177e4SLinus Torvalds 	struct buffer_head	*root_bh = NULL;
3021da177e4SLinus Torvalds 	struct buffer_head	*boot_bh;
3031da177e4SLinus Torvalds 	struct inode		*root_inode = NULL;
3041da177e4SLinus Torvalds 	s32			 root_block;
3051da177e4SLinus Torvalds 	int			 size, blocksize;
3061da177e4SLinus Torvalds 	u32			 chksum;
3071da177e4SLinus Torvalds 	int			 num_bm;
3081da177e4SLinus Torvalds 	int			 i, j;
3098fed10beSEric W. Biederman 	kuid_t			 uid;
3108fed10beSEric W. Biederman 	kgid_t			 gid;
3111da177e4SLinus Torvalds 	int			 reserved;
3121da177e4SLinus Torvalds 	unsigned long		 mount_flags;
3131da177e4SLinus Torvalds 	int			 tmp_flags;	/* fix remount prototype... */
3142b943cf0SAl Viro 	u8			 sig[4];
315842a859dSAl Viro 	int			 ret;
3161da177e4SLinus Torvalds 
317e9b3961bSMiklos Szeredi 	save_mount_options(sb, data);
318e9b3961bSMiklos Szeredi 
3199606d9aaSFabian Frederick 	pr_debug("read_super(%s)\n", data ? (const char *)data : "no options");
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	sb->s_magic             = AFFS_SUPER_MAGIC;
3221da177e4SLinus Torvalds 	sb->s_op                = &affs_sops;
3231da177e4SLinus Torvalds 	sb->s_flags |= MS_NODIRATIME;
3241da177e4SLinus Torvalds 
325f8314dc6SPanagiotis Issaris 	sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
3261da177e4SLinus Torvalds 	if (!sbi)
3271da177e4SLinus Torvalds 		return -ENOMEM;
32874c41429SJan Blunck 
3291da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
330a215fef7SArtem Bityutskiy 	sbi->sb = sb;
3317d135a5dSMatthias Kaehlcke 	mutex_init(&sbi->s_bmlock);
33229333920SAl Viro 	spin_lock_init(&sbi->symlink_lock);
3333dd84782SArtem Bityutskiy 	spin_lock_init(&sbi->work_lock);
3343dd84782SArtem Bityutskiy 	INIT_DELAYED_WORK(&sbi->sb_work, flush_superblock);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
3371da177e4SLinus Torvalds 				&blocksize,&sbi->s_prefix,
3381da177e4SLinus Torvalds 				sbi->s_volume, &mount_flags)) {
3390158de12SFabian Frederick 		pr_err("Error parsing options\n");
3401da177e4SLinus Torvalds 		return -EINVAL;
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 	/* N.B. after this point s_prefix must be released */
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	sbi->s_flags   = mount_flags;
3451da177e4SLinus Torvalds 	sbi->s_mode    = i;
3461da177e4SLinus Torvalds 	sbi->s_uid     = uid;
3471da177e4SLinus Torvalds 	sbi->s_gid     = gid;
3481da177e4SLinus Torvalds 	sbi->s_reserved= reserved;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	/* Get the size of the device in 512-byte blocks.
3511da177e4SLinus Torvalds 	 * If we later see that the partition uses bigger
3521da177e4SLinus Torvalds 	 * blocks, we will have to change it.
3531da177e4SLinus Torvalds 	 */
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	size = sb->s_bdev->bd_inode->i_size >> 9;
3569606d9aaSFabian Frederick 	pr_debug("initial blocksize=%d, #blocks=%d\n", 512, size);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	affs_set_blocksize(sb, PAGE_SIZE);
3591da177e4SLinus Torvalds 	/* Try to find root block. Its location depends on the block size. */
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	i = 512;
3621da177e4SLinus Torvalds 	j = 4096;
3631da177e4SLinus Torvalds 	if (blocksize > 0) {
3641da177e4SLinus Torvalds 		i = j = blocksize;
3651da177e4SLinus Torvalds 		size = size / (blocksize / 512);
3661da177e4SLinus Torvalds 	}
3671e907f4fSFabian Frederick 	for (blocksize = i; blocksize <= j; blocksize <<= 1, size >>= 1) {
3681da177e4SLinus Torvalds 		sbi->s_root_block = root_block;
3691da177e4SLinus Torvalds 		if (root_block < 0)
3701da177e4SLinus Torvalds 			sbi->s_root_block = (reserved + size - 1) / 2;
3719606d9aaSFabian Frederick 		pr_debug("setting blocksize to %d\n", blocksize);
3721da177e4SLinus Torvalds 		affs_set_blocksize(sb, blocksize);
3731da177e4SLinus Torvalds 		sbi->s_partition_size = size;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		/* The root block location that was calculated above is not
3761da177e4SLinus Torvalds 		 * correct if the partition size is an odd number of 512-
3771da177e4SLinus Torvalds 		 * byte blocks, which will be rounded down to a number of
3781da177e4SLinus Torvalds 		 * 1024-byte blocks, and if there were an even number of
3791da177e4SLinus Torvalds 		 * reserved blocks. Ideally, all partition checkers should
3801da177e4SLinus Torvalds 		 * report the real number of blocks of the real blocksize,
3811da177e4SLinus Torvalds 		 * but since this just cannot be done, we have to try to
3821da177e4SLinus Torvalds 		 * find the root block anyways. In the above case, it is one
3831da177e4SLinus Torvalds 		 * block behind the calculated one. So we check this one, too.
3841da177e4SLinus Torvalds 		 */
3851da177e4SLinus Torvalds 		for (num_bm = 0; num_bm < 2; num_bm++) {
3869606d9aaSFabian Frederick 			pr_debug("Dev %s, trying root=%u, bs=%d, "
3871da177e4SLinus Torvalds 				"size=%d, reserved=%d\n",
3881da177e4SLinus Torvalds 				sb->s_id,
3891da177e4SLinus Torvalds 				sbi->s_root_block + num_bm,
3901da177e4SLinus Torvalds 				blocksize, size, reserved);
3911da177e4SLinus Torvalds 			root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
3921da177e4SLinus Torvalds 			if (!root_bh)
3931da177e4SLinus Torvalds 				continue;
3941da177e4SLinus Torvalds 			if (!affs_checksum_block(sb, root_bh) &&
3951da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
3961da177e4SLinus Torvalds 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
3971da177e4SLinus Torvalds 				sbi->s_hashsize    = blocksize / 4 - 56;
3981da177e4SLinus Torvalds 				sbi->s_root_block += num_bm;
3991da177e4SLinus Torvalds 				goto got_root;
4001da177e4SLinus Torvalds 			}
4011da177e4SLinus Torvalds 			affs_brelse(root_bh);
4021da177e4SLinus Torvalds 			root_bh = NULL;
4031da177e4SLinus Torvalds 		}
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds 	if (!silent)
4060158de12SFabian Frederick 		pr_err("No valid root block on device %s\n", sb->s_id);
407842a859dSAl Viro 	return -EINVAL;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	/* N.B. after this point bh must be released */
4101da177e4SLinus Torvalds got_root:
411842a859dSAl Viro 	/* Keep super block in cache */
412842a859dSAl Viro 	sbi->s_root_bh = root_bh;
4131da177e4SLinus Torvalds 	root_block = sbi->s_root_block;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	/* Find out which kind of FS we have */
4161da177e4SLinus Torvalds 	boot_bh = sb_bread(sb, 0);
4171da177e4SLinus Torvalds 	if (!boot_bh) {
4180158de12SFabian Frederick 		pr_err("Cannot read boot block\n");
419842a859dSAl Viro 		return -EINVAL;
4201da177e4SLinus Torvalds 	}
4212b943cf0SAl Viro 	memcpy(sig, boot_bh->b_data, 4);
4221da177e4SLinus Torvalds 	brelse(boot_bh);
4232b943cf0SAl Viro 	chksum = be32_to_cpu(*(__be32 *)sig);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	/* Dircache filesystems are compatible with non-dircache ones
4261da177e4SLinus Torvalds 	 * when reading. As long as they aren't supported, writing is
4271da177e4SLinus Torvalds 	 * not recommended.
4281da177e4SLinus Torvalds 	 */
4291da177e4SLinus Torvalds 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
4301da177e4SLinus Torvalds 	     || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
4310158de12SFabian Frederick 		pr_notice("Dircache FS - mounting %s read only\n", sb->s_id);
4321da177e4SLinus Torvalds 		sb->s_flags |= MS_RDONLY;
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 	switch (chksum) {
4351da177e4SLinus Torvalds 	case MUFS_FS:
4361da177e4SLinus Torvalds 	case MUFS_INTLFFS:
4371da177e4SLinus Torvalds 	case MUFS_DCFFS:
43834f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4391da177e4SLinus Torvalds 		/* fall thru */
4401da177e4SLinus Torvalds 	case FS_INTLFFS:
4411da177e4SLinus Torvalds 	case FS_DCFFS:
44234f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_INTL);
4431da177e4SLinus Torvalds 		break;
4441da177e4SLinus Torvalds 	case MUFS_FFS:
44534f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4461da177e4SLinus Torvalds 		break;
4471da177e4SLinus Torvalds 	case FS_FFS:
4481da177e4SLinus Torvalds 		break;
4491da177e4SLinus Torvalds 	case MUFS_OFS:
45034f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4511da177e4SLinus Torvalds 		/* fall thru */
4521da177e4SLinus Torvalds 	case FS_OFS:
45334f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_OFS);
4541da177e4SLinus Torvalds 		sb->s_flags |= MS_NOEXEC;
4551da177e4SLinus Torvalds 		break;
4561da177e4SLinus Torvalds 	case MUFS_DCOFS:
4571da177e4SLinus Torvalds 	case MUFS_INTLOFS:
45834f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_MUFS);
4591da177e4SLinus Torvalds 	case FS_DCOFS:
4601da177e4SLinus Torvalds 	case FS_INTLOFS:
46134f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_INTL);
46234f24835SFabian Frederick 		affs_set_opt(sbi->s_flags, SF_OFS);
4631da177e4SLinus Torvalds 		sb->s_flags |= MS_NOEXEC;
4641da177e4SLinus Torvalds 		break;
4651da177e4SLinus Torvalds 	default:
4660158de12SFabian Frederick 		pr_err("Unknown filesystem on device %s: %08X\n",
4671da177e4SLinus Torvalds 		       sb->s_id, chksum);
468842a859dSAl Viro 		return -EINVAL;
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 
47179bda4d5SFabian Frederick 	if (affs_test_opt(mount_flags, SF_VERBOSE)) {
4722b943cf0SAl Viro 		u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
4730158de12SFabian Frederick 		pr_notice("Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
4742b943cf0SAl Viro 			len > 31 ? 31 : len,
4751da177e4SLinus Torvalds 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
4762b943cf0SAl Viro 			sig, sig[3] + '0', blocksize);
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	sb->s_flags |= MS_NODEV | MS_NOSUID;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	sbi->s_data_blksize = sb->s_blocksize;
48279bda4d5SFabian Frederick 	if (affs_test_opt(sbi->s_flags, SF_OFS))
4831da177e4SLinus Torvalds 		sbi->s_data_blksize -= 24;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	tmp_flags = sb->s_flags;
486842a859dSAl Viro 	ret = affs_init_bitmap(sb, &tmp_flags);
487842a859dSAl Viro 	if (ret)
488842a859dSAl Viro 		return ret;
4891da177e4SLinus Torvalds 	sb->s_flags = tmp_flags;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* set up enough so that it can read an inode */
4921da177e4SLinus Torvalds 
493210f8559SDavid Howells 	root_inode = affs_iget(sb, root_block);
494842a859dSAl Viro 	if (IS_ERR(root_inode))
495842a859dSAl Viro 		return PTR_ERR(root_inode);
496210f8559SDavid Howells 
49779bda4d5SFabian Frederick 	if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL))
498a129880dSAl Viro 		sb->s_d_op = &affs_intl_dentry_operations;
499a129880dSAl Viro 	else
500a129880dSAl Viro 		sb->s_d_op = &affs_dentry_operations;
501a129880dSAl Viro 
50248fde701SAl Viro 	sb->s_root = d_make_root(root_inode);
5031da177e4SLinus Torvalds 	if (!sb->s_root) {
5040158de12SFabian Frederick 		pr_err("AFFS: Get root inode failed\n");
505842a859dSAl Viro 		return -ENOMEM;
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 
5089606d9aaSFabian Frederick 	pr_debug("s_flags=%lX\n", sb->s_flags);
5091da177e4SLinus Torvalds 	return 0;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds static int
5131da177e4SLinus Torvalds affs_remount(struct super_block *sb, int *flags, char *data)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
5161da177e4SLinus Torvalds 	int			 blocksize;
5178fed10beSEric W. Biederman 	kuid_t			 uid;
5188fed10beSEric W. Biederman 	kgid_t			 gid;
5191da177e4SLinus Torvalds 	int			 mode;
5201da177e4SLinus Torvalds 	int			 reserved;
5211da177e4SLinus Torvalds 	int			 root_block;
5221da177e4SLinus Torvalds 	unsigned long		 mount_flags;
5231da177e4SLinus Torvalds 	int			 res = 0;
524*c8f33d0bSSanidhya Kashyap 	char			*new_opts;
52529333920SAl Viro 	char			 volume[32];
52629333920SAl Viro 	char			*prefix = NULL;
5271da177e4SLinus Torvalds 
528*c8f33d0bSSanidhya Kashyap 	new_opts = kstrdup(data, GFP_KERNEL);
529*c8f33d0bSSanidhya Kashyap 	if (!new_opts)
530*c8f33d0bSSanidhya Kashyap 		return -ENOMEM;
531*c8f33d0bSSanidhya Kashyap 
5329606d9aaSFabian Frederick 	pr_debug("%s(flags=0x%x,opts=\"%s\")\n", __func__, *flags, data);
5331da177e4SLinus Torvalds 
53402b9984dSTheodore Ts'o 	sync_filesystem(sb);
5351da177e4SLinus Torvalds 	*flags |= MS_NODIRATIME;
5361da177e4SLinus Torvalds 
53729333920SAl Viro 	memcpy(volume, sbi->s_volume, 32);
5381da177e4SLinus Torvalds 	if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
53929333920SAl Viro 			   &blocksize, &prefix, volume,
540e9b3961bSMiklos Szeredi 			   &mount_flags)) {
54129333920SAl Viro 		kfree(prefix);
542e9b3961bSMiklos Szeredi 		kfree(new_opts);
5431da177e4SLinus Torvalds 		return -EINVAL;
544e9b3961bSMiklos Szeredi 	}
54574c41429SJan Blunck 
54643829731STejun Heo 	flush_delayed_work(&sbi->sb_work);
5472a32cebdSAl Viro 	replace_mount_options(sb, new_opts);
548e9b3961bSMiklos Szeredi 
5491da177e4SLinus Torvalds 	sbi->s_flags = mount_flags;
5501da177e4SLinus Torvalds 	sbi->s_mode  = mode;
5511da177e4SLinus Torvalds 	sbi->s_uid   = uid;
5521da177e4SLinus Torvalds 	sbi->s_gid   = gid;
55329333920SAl Viro 	/* protect against readers */
55429333920SAl Viro 	spin_lock(&sbi->symlink_lock);
55529333920SAl Viro 	if (prefix) {
55629333920SAl Viro 		kfree(sbi->s_prefix);
55729333920SAl Viro 		sbi->s_prefix = prefix;
55829333920SAl Viro 	}
55929333920SAl Viro 	memcpy(sbi->s_volume, volume, 32);
56029333920SAl Viro 	spin_unlock(&sbi->symlink_lock);
5611da177e4SLinus Torvalds 
56274c41429SJan Blunck 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
5631da177e4SLinus Torvalds 		return 0;
56474c41429SJan Blunck 
5650164b1a3SArtem Bityutskiy 	if (*flags & MS_RDONLY)
5661da177e4SLinus Torvalds 		affs_free_bitmap(sb);
5670164b1a3SArtem Bityutskiy 	else
5681da177e4SLinus Torvalds 		res = affs_init_bitmap(sb, flags);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	return res;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds static int
574726c3342SDavid Howells affs_statfs(struct dentry *dentry, struct kstatfs *buf)
5751da177e4SLinus Torvalds {
576726c3342SDavid Howells 	struct super_block *sb = dentry->d_sb;
5771da177e4SLinus Torvalds 	int		 free;
578a6a2a73cSColy Li 	u64		 id = huge_encode_dev(sb->s_bdev->bd_dev);
5791da177e4SLinus Torvalds 
5809606d9aaSFabian Frederick 	pr_debug("%s() partsize=%d, reserved=%d\n",
5819606d9aaSFabian Frederick 		 __func__, AFFS_SB(sb)->s_partition_size,
5821da177e4SLinus Torvalds 		 AFFS_SB(sb)->s_reserved);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	free          = affs_count_free_blocks(sb);
5851da177e4SLinus Torvalds 	buf->f_type    = AFFS_SUPER_MAGIC;
5861da177e4SLinus Torvalds 	buf->f_bsize   = sb->s_blocksize;
5871da177e4SLinus Torvalds 	buf->f_blocks  = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
5881da177e4SLinus Torvalds 	buf->f_bfree   = free;
5891da177e4SLinus Torvalds 	buf->f_bavail  = free;
590a6a2a73cSColy Li 	buf->f_fsid.val[0] = (u32)id;
591a6a2a73cSColy Li 	buf->f_fsid.val[1] = (u32)(id >> 32);
592f157853eSFabian Frederick 	buf->f_namelen = AFFSNAMEMAX;
5931da177e4SLinus Torvalds 	return 0;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
596152a0836SAl Viro static struct dentry *affs_mount(struct file_system_type *fs_type,
597152a0836SAl Viro 	int flags, const char *dev_name, void *data)
5981da177e4SLinus Torvalds {
599152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, affs_fill_super);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
602842a859dSAl Viro static void affs_kill_sb(struct super_block *sb)
603842a859dSAl Viro {
604842a859dSAl Viro 	struct affs_sb_info *sbi = AFFS_SB(sb);
605842a859dSAl Viro 	kill_block_super(sb);
606842a859dSAl Viro 	if (sbi) {
607842a859dSAl Viro 		affs_free_bitmap(sb);
608842a859dSAl Viro 		affs_brelse(sbi->s_root_bh);
609842a859dSAl Viro 		kfree(sbi->s_prefix);
6104d29e571SFabian Frederick 		mutex_destroy(&sbi->s_bmlock);
611842a859dSAl Viro 		kfree(sbi);
612842a859dSAl Viro 	}
613842a859dSAl Viro }
614842a859dSAl Viro 
6151da177e4SLinus Torvalds static struct file_system_type affs_fs_type = {
6161da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
6171da177e4SLinus Torvalds 	.name		= "affs",
618152a0836SAl Viro 	.mount		= affs_mount,
619842a859dSAl Viro 	.kill_sb	= affs_kill_sb,
6201da177e4SLinus Torvalds 	.fs_flags	= FS_REQUIRES_DEV,
6211da177e4SLinus Torvalds };
6227f78e035SEric W. Biederman MODULE_ALIAS_FS("affs");
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds static int __init init_affs_fs(void)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	int err = init_inodecache();
6271da177e4SLinus Torvalds 	if (err)
6281da177e4SLinus Torvalds 		goto out1;
6291da177e4SLinus Torvalds 	err = register_filesystem(&affs_fs_type);
6301da177e4SLinus Torvalds 	if (err)
6311da177e4SLinus Torvalds 		goto out;
6321da177e4SLinus Torvalds 	return 0;
6331da177e4SLinus Torvalds out:
6341da177e4SLinus Torvalds 	destroy_inodecache();
6351da177e4SLinus Torvalds out1:
6361da177e4SLinus Torvalds 	return err;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds static void __exit exit_affs_fs(void)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	unregister_filesystem(&affs_fs_type);
6421da177e4SLinus Torvalds 	destroy_inodecache();
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds MODULE_DESCRIPTION("Amiga filesystem support for Linux");
6461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds module_init(init_affs_fs)
6491da177e4SLinus Torvalds module_exit(exit_affs_fs)
650