xref: /linux/fs/affs/super.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/affs/inode.c
4  *
5  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
6  *
7  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
8  *
9  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
10  *
11  *  (C) 1991  Linus Torvalds - minix filesystem
12  */
13 
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/statfs.h>
17 #include <linux/fs_parser.h>
18 #include <linux/fs_context.h>
19 #include <linux/magic.h>
20 #include <linux/sched.h>
21 #include <linux/cred.h>
22 #include <linux/slab.h>
23 #include <linux/writeback.h>
24 #include <linux/blkdev.h>
25 #include <linux/seq_file.h>
26 #include <linux/iversion.h>
27 #include "affs.h"
28 
29 static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
30 static int affs_show_options(struct seq_file *m, struct dentry *root);
31 
32 static void
33 affs_commit_super(struct super_block *sb, int wait)
34 {
35 	struct affs_sb_info *sbi = AFFS_SB(sb);
36 	struct buffer_head *bh = sbi->s_root_bh;
37 	struct affs_root_tail *tail = AFFS_ROOT_TAIL(sb, bh);
38 
39 	lock_buffer(bh);
40 	affs_secs_to_datestamp(ktime_get_real_seconds(), &tail->disk_change);
41 	affs_fix_checksum(sb, bh);
42 	unlock_buffer(bh);
43 
44 	mark_buffer_dirty(bh);
45 	if (wait)
46 		sync_dirty_buffer(bh);
47 }
48 
49 static void
50 affs_put_super(struct super_block *sb)
51 {
52 	struct affs_sb_info *sbi = AFFS_SB(sb);
53 	pr_debug("%s()\n", __func__);
54 
55 	cancel_delayed_work_sync(&sbi->sb_work);
56 }
57 
58 static int
59 affs_sync_fs(struct super_block *sb, int wait)
60 {
61 	affs_commit_super(sb, wait);
62 	return 0;
63 }
64 
65 static void flush_superblock(struct work_struct *work)
66 {
67 	struct affs_sb_info *sbi;
68 	struct super_block *sb;
69 
70 	sbi = container_of(work, struct affs_sb_info, sb_work.work);
71 	sb = sbi->sb;
72 
73 	spin_lock(&sbi->work_lock);
74 	sbi->work_queued = 0;
75 	spin_unlock(&sbi->work_lock);
76 
77 	affs_commit_super(sb, 1);
78 }
79 
80 void affs_mark_sb_dirty(struct super_block *sb)
81 {
82 	struct affs_sb_info *sbi = AFFS_SB(sb);
83 	unsigned long delay;
84 
85 	if (sb_rdonly(sb))
86 	       return;
87 
88 	spin_lock(&sbi->work_lock);
89 	if (!sbi->work_queued) {
90 	       delay = msecs_to_jiffies(dirty_writeback_interval * 10);
91 	       queue_delayed_work(system_long_wq, &sbi->sb_work, delay);
92 	       sbi->work_queued = 1;
93 	}
94 	spin_unlock(&sbi->work_lock);
95 }
96 
97 static struct kmem_cache * affs_inode_cachep;
98 
99 static struct inode *affs_alloc_inode(struct super_block *sb)
100 {
101 	struct affs_inode_info *i;
102 
103 	i = alloc_inode_sb(sb, affs_inode_cachep, GFP_KERNEL);
104 	if (!i)
105 		return NULL;
106 
107 	inode_set_iversion(&i->vfs_inode, 1);
108 	i->i_lc = NULL;
109 	i->i_ext_bh = NULL;
110 	i->i_pa_cnt = 0;
111 	mmb_init(&i->i_metadata_bhs, &i->vfs_inode.i_data);
112 
113 	return &i->vfs_inode;
114 }
115 
116 static void affs_free_inode(struct inode *inode)
117 {
118 	kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
119 }
120 
121 static void init_once(void *foo)
122 {
123 	struct affs_inode_info *ei = (struct affs_inode_info *) foo;
124 
125 	mutex_init(&ei->i_link_lock);
126 	mutex_init(&ei->i_ext_lock);
127 	inode_init_once(&ei->vfs_inode);
128 }
129 
130 static int __init init_inodecache(void)
131 {
132 	affs_inode_cachep = kmem_cache_create("affs_inode_cache",
133 					     sizeof(struct affs_inode_info),
134 					     0, (SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT),
135 					     init_once);
136 	if (affs_inode_cachep == NULL)
137 		return -ENOMEM;
138 	return 0;
139 }
140 
141 static void destroy_inodecache(void)
142 {
143 	/*
144 	 * Make sure all delayed rcu free inodes are flushed before we
145 	 * destroy cache.
146 	 */
147 	rcu_barrier();
148 	kmem_cache_destroy(affs_inode_cachep);
149 }
150 
151 static const struct super_operations affs_sops = {
152 	.alloc_inode	= affs_alloc_inode,
153 	.free_inode	= affs_free_inode,
154 	.write_inode	= affs_write_inode,
155 	.evict_inode	= affs_evict_inode,
156 	.put_super	= affs_put_super,
157 	.sync_fs	= affs_sync_fs,
158 	.statfs		= affs_statfs,
159 	.show_options	= affs_show_options,
160 };
161 
162 enum {
163 	Opt_bs, Opt_mode, Opt_mufs, Opt_notruncate, Opt_prefix, Opt_protect,
164 	Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
165 	Opt_verbose, Opt_volume, Opt_ignore,
166 };
167 
168 struct affs_context {
169 	kuid_t		uid;		/* uid to override */
170 	kgid_t		gid;		/* gid to override */
171 	unsigned int	mode;		/* mode to override */
172 	unsigned int	reserved;	/* Number of reserved blocks */
173 	int		root_block;	/* FFS root block number */
174 	int		blocksize;	/* Initial device blksize */
175 	char		*prefix;	/* Prefix for volumes and assigns */
176 	char		volume[32];	/* Vol. prefix for absolute symlinks */
177 	unsigned long	mount_flags;	/* Options */
178 };
179 
180 static const struct fs_parameter_spec affs_param_spec[] = {
181 	fsparam_u32	("bs",		Opt_bs),
182 	fsparam_u32oct	("mode",	Opt_mode),
183 	fsparam_flag	("mufs",	Opt_mufs),
184 	fsparam_flag	("nofilenametruncate",	Opt_notruncate),
185 	fsparam_string	("prefix",	Opt_prefix),
186 	fsparam_flag	("protect",	Opt_protect),
187 	fsparam_u32	("reserved",	Opt_reserved),
188 	fsparam_u32	("root",	Opt_root),
189 	fsparam_gid	("setgid",	Opt_setgid),
190 	fsparam_uid	("setuid",	Opt_setuid),
191 	fsparam_flag	("verbose",	Opt_verbose),
192 	fsparam_string	("volume",	Opt_volume),
193 	fsparam_flag	("grpquota",	Opt_ignore),
194 	fsparam_flag	("noquota",	Opt_ignore),
195 	fsparam_flag	("quota",	Opt_ignore),
196 	fsparam_flag	("usrquota",	Opt_ignore),
197 	{},
198 };
199 
200 static int affs_parse_param(struct fs_context *fc, struct fs_parameter *param)
201 {
202 	struct affs_context *ctx = fc->fs_private;
203 	struct fs_parse_result result;
204 	int n;
205 	int opt;
206 
207 	opt = fs_parse(fc, affs_param_spec, param, &result);
208 	if (opt < 0)
209 		return opt;
210 
211 	switch (opt) {
212 	case Opt_bs:
213 		n = result.uint_32;
214 		if (n != 512 && n != 1024 && n != 2048
215 		    && n != 4096) {
216 			pr_warn("Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
217 			return -EINVAL;
218 		}
219 		ctx->blocksize = n;
220 		break;
221 	case Opt_mode:
222 		ctx->mode = result.uint_32 & 0777;
223 		affs_set_opt(ctx->mount_flags, SF_SETMODE);
224 		break;
225 	case Opt_mufs:
226 		affs_set_opt(ctx->mount_flags, SF_MUFS);
227 		break;
228 	case Opt_notruncate:
229 		affs_set_opt(ctx->mount_flags, SF_NO_TRUNCATE);
230 		break;
231 	case Opt_prefix:
232 		kfree(ctx->prefix);
233 		ctx->prefix = param->string;
234 		param->string = NULL;
235 		affs_set_opt(ctx->mount_flags, SF_PREFIX);
236 		break;
237 	case Opt_protect:
238 		affs_set_opt(ctx->mount_flags, SF_IMMUTABLE);
239 		break;
240 	case Opt_reserved:
241 		ctx->reserved = result.uint_32;
242 		break;
243 	case Opt_root:
244 		ctx->root_block = result.uint_32;
245 		break;
246 	case Opt_setgid:
247 		ctx->gid = result.gid;
248 		affs_set_opt(ctx->mount_flags, SF_SETGID);
249 		break;
250 	case Opt_setuid:
251 		ctx->uid = result.uid;
252 		affs_set_opt(ctx->mount_flags, SF_SETUID);
253 		break;
254 	case Opt_verbose:
255 		affs_set_opt(ctx->mount_flags, SF_VERBOSE);
256 		break;
257 	case Opt_volume:
258 		strscpy(ctx->volume, param->string, 32);
259 		break;
260 	case Opt_ignore:
261 		/* Silently ignore the quota options */
262 		break;
263 	default:
264 		return -EINVAL;
265 	}
266 	return 0;
267 }
268 
269 static int affs_show_options(struct seq_file *m, struct dentry *root)
270 {
271 	struct super_block *sb = root->d_sb;
272 	struct affs_sb_info *sbi = AFFS_SB(sb);
273 
274 	if (sb->s_blocksize)
275 		seq_printf(m, ",bs=%lu", sb->s_blocksize);
276 	if (affs_test_opt(sbi->s_flags, SF_SETMODE))
277 		seq_printf(m, ",mode=%o", sbi->s_mode);
278 	if (affs_test_opt(sbi->s_flags, SF_MUFS))
279 		seq_puts(m, ",mufs");
280 	if (affs_test_opt(sbi->s_flags, SF_NO_TRUNCATE))
281 		seq_puts(m, ",nofilenametruncate");
282 	if (affs_test_opt(sbi->s_flags, SF_PREFIX))
283 		seq_printf(m, ",prefix=%s", sbi->s_prefix);
284 	if (affs_test_opt(sbi->s_flags, SF_IMMUTABLE))
285 		seq_puts(m, ",protect");
286 	if (sbi->s_reserved != 2)
287 		seq_printf(m, ",reserved=%u", sbi->s_reserved);
288 	if (sbi->s_root_block != (sbi->s_reserved + sbi->s_partition_size - 1) / 2)
289 		seq_printf(m, ",root=%u", sbi->s_root_block);
290 	if (affs_test_opt(sbi->s_flags, SF_SETGID))
291 		seq_printf(m, ",setgid=%u",
292 			   from_kgid_munged(&init_user_ns, sbi->s_gid));
293 	if (affs_test_opt(sbi->s_flags, SF_SETUID))
294 		seq_printf(m, ",setuid=%u",
295 			   from_kuid_munged(&init_user_ns, sbi->s_uid));
296 	if (affs_test_opt(sbi->s_flags, SF_VERBOSE))
297 		seq_puts(m, ",verbose");
298 	if (sbi->s_volume[0])
299 		seq_printf(m, ",volume=%s", sbi->s_volume);
300 	return 0;
301 }
302 
303 /* This function definitely needs to be split up. Some fine day I'll
304  * hopefully have the guts to do so. Until then: sorry for the mess.
305  */
306 
307 static int affs_fill_super(struct super_block *sb, struct fs_context *fc)
308 {
309 	struct affs_sb_info	*sbi;
310 	struct affs_context	*ctx = fc->fs_private;
311 	struct buffer_head	*root_bh = NULL;
312 	struct buffer_head	*boot_bh;
313 	struct inode		*root_inode = NULL;
314 	int			 silent = fc->sb_flags & SB_SILENT;
315 	int			 size, blocksize;
316 	u32			 chksum;
317 	int			 num_bm;
318 	int			 i, j;
319 	int			 tmp_flags;	/* fix remount prototype... */
320 	u8			 sig[4];
321 	int			 ret;
322 
323 	sb->s_magic             = AFFS_SUPER_MAGIC;
324 	sb->s_op                = &affs_sops;
325 	sb->s_flags |= SB_NODIRATIME;
326 
327 	sb->s_time_gran = NSEC_PER_SEC;
328 	sb->s_time_min = sys_tz.tz_minuteswest * 60 + AFFS_EPOCH_DELTA;
329 	sb->s_time_max = 86400LL * U32_MAX + 86400 + sb->s_time_min;
330 
331 	sbi = kzalloc_obj(struct affs_sb_info);
332 	if (!sbi)
333 		return -ENOMEM;
334 
335 	sb->s_fs_info = sbi;
336 	sbi->sb = sb;
337 	mutex_init(&sbi->s_bmlock);
338 	spin_lock_init(&sbi->symlink_lock);
339 	spin_lock_init(&sbi->work_lock);
340 	INIT_DELAYED_WORK(&sbi->sb_work, flush_superblock);
341 
342 	sbi->s_flags	= ctx->mount_flags;
343 	sbi->s_mode	= ctx->mode;
344 	sbi->s_uid	= ctx->uid;
345 	sbi->s_gid	= ctx->gid;
346 	sbi->s_reserved	= ctx->reserved;
347 	sbi->s_prefix	= ctx->prefix;
348 	ctx->prefix	= NULL;
349 	memcpy(sbi->s_volume, ctx->volume, 32);
350 
351 	/* N.B. after this point s_prefix must be released */
352 
353 	/* Get the size of the device in 512-byte blocks.
354 	 * If we later see that the partition uses bigger
355 	 * blocks, we will have to change it.
356 	 */
357 
358 	size = bdev_nr_sectors(sb->s_bdev);
359 	pr_debug("initial blocksize=%d, #blocks=%d\n", 512, size);
360 
361 	if (!sb_set_blocksize(sb, PAGE_SIZE))
362 		return -EINVAL;
363 	/* Try to find root block. Its location depends on the block size. */
364 
365 	i = bdev_logical_block_size(sb->s_bdev);
366 	j = PAGE_SIZE;
367 	blocksize = ctx->blocksize;
368 	if (blocksize > 0) {
369 		i = j = blocksize;
370 		size = size / (blocksize / 512);
371 	}
372 
373 	for (blocksize = i; blocksize <= j; blocksize <<= 1, size >>= 1) {
374 		sbi->s_root_block = ctx->root_block;
375 		if (ctx->root_block < 0)
376 			sbi->s_root_block = (ctx->reserved + size - 1) / 2;
377 		pr_debug("setting blocksize to %d\n", blocksize);
378 		if (!sb_set_blocksize(sb, blocksize))
379 			return -EINVAL;
380 		sbi->s_partition_size = size;
381 
382 		/* The root block location that was calculated above is not
383 		 * correct if the partition size is an odd number of 512-
384 		 * byte blocks, which will be rounded down to a number of
385 		 * 1024-byte blocks, and if there were an even number of
386 		 * reserved blocks. Ideally, all partition checkers should
387 		 * report the real number of blocks of the real blocksize,
388 		 * but since this just cannot be done, we have to try to
389 		 * find the root block anyways. In the above case, it is one
390 		 * block behind the calculated one. So we check this one, too.
391 		 */
392 		for (num_bm = 0; num_bm < 2; num_bm++) {
393 			pr_debug("Dev %s, trying root=%u, bs=%d, "
394 				"size=%d, reserved=%d\n",
395 				sb->s_id,
396 				sbi->s_root_block + num_bm,
397 				ctx->blocksize, size, ctx->reserved);
398 			root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
399 			if (!root_bh)
400 				continue;
401 			if (!affs_checksum_block(sb, root_bh) &&
402 			    be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
403 			    be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
404 				sbi->s_hashsize    = blocksize / 4 - 56;
405 				sbi->s_root_block += num_bm;
406 				goto got_root;
407 			}
408 			affs_brelse(root_bh);
409 			root_bh = NULL;
410 		}
411 	}
412 	if (!silent)
413 		pr_err("No valid root block on device %s\n", sb->s_id);
414 	return -EINVAL;
415 
416 	/* N.B. after this point bh must be released */
417 got_root:
418 	/* Keep super block in cache */
419 	sbi->s_root_bh = root_bh;
420 	ctx->root_block = sbi->s_root_block;
421 
422 	/* Find out which kind of FS we have */
423 	boot_bh = sb_bread(sb, 0);
424 	if (!boot_bh) {
425 		pr_err("Cannot read boot block\n");
426 		return -EINVAL;
427 	}
428 	memcpy(sig, boot_bh->b_data, 4);
429 	brelse(boot_bh);
430 	chksum = be32_to_cpu(*(__be32 *)sig);
431 
432 	/* Dircache filesystems are compatible with non-dircache ones
433 	 * when reading. As long as they aren't supported, writing is
434 	 * not recommended.
435 	 */
436 	if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
437 	     || chksum == MUFS_DCOFS) && !sb_rdonly(sb)) {
438 		pr_notice("Dircache FS - mounting %s read only\n", sb->s_id);
439 		sb->s_flags |= SB_RDONLY;
440 	}
441 	switch (chksum) {
442 	case MUFS_FS:
443 	case MUFS_INTLFFS:
444 	case MUFS_DCFFS:
445 		affs_set_opt(sbi->s_flags, SF_MUFS);
446 		fallthrough;
447 	case FS_INTLFFS:
448 	case FS_DCFFS:
449 		affs_set_opt(sbi->s_flags, SF_INTL);
450 		break;
451 	case MUFS_FFS:
452 		affs_set_opt(sbi->s_flags, SF_MUFS);
453 		break;
454 	case FS_FFS:
455 		break;
456 	case MUFS_OFS:
457 		affs_set_opt(sbi->s_flags, SF_MUFS);
458 		fallthrough;
459 	case FS_OFS:
460 		affs_set_opt(sbi->s_flags, SF_OFS);
461 		sb->s_flags |= SB_NOEXEC;
462 		break;
463 	case MUFS_DCOFS:
464 	case MUFS_INTLOFS:
465 		affs_set_opt(sbi->s_flags, SF_MUFS);
466 		fallthrough;
467 	case FS_DCOFS:
468 	case FS_INTLOFS:
469 		affs_set_opt(sbi->s_flags, SF_INTL);
470 		affs_set_opt(sbi->s_flags, SF_OFS);
471 		sb->s_flags |= SB_NOEXEC;
472 		break;
473 	default:
474 		pr_err("Unknown filesystem on device %s: %08X\n",
475 		       sb->s_id, chksum);
476 		return -EINVAL;
477 	}
478 
479 	if (affs_test_opt(ctx->mount_flags, SF_VERBOSE)) {
480 		u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
481 		pr_notice("Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
482 			len > 31 ? 31 : len,
483 			AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
484 			sig, sig[3] + '0', blocksize);
485 	}
486 
487 	sb->s_flags |= SB_NODEV | SB_NOSUID;
488 
489 	sbi->s_data_blksize = sb->s_blocksize;
490 	if (affs_test_opt(sbi->s_flags, SF_OFS))
491 		sbi->s_data_blksize -= 24;
492 
493 	tmp_flags = sb->s_flags;
494 	ret = affs_init_bitmap(sb, &tmp_flags);
495 	if (ret)
496 		return ret;
497 	sb->s_flags = tmp_flags;
498 
499 	/* set up enough so that it can read an inode */
500 
501 	root_inode = affs_iget(sb, ctx->root_block);
502 	if (IS_ERR(root_inode))
503 		return PTR_ERR(root_inode);
504 
505 	if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL))
506 		set_default_d_op(sb, &affs_intl_dentry_operations);
507 	else
508 		set_default_d_op(sb, &affs_dentry_operations);
509 
510 	sb->s_root = d_make_root(root_inode);
511 	if (!sb->s_root) {
512 		pr_err("AFFS: Get root inode failed\n");
513 		return -ENOMEM;
514 	}
515 
516 	sb->s_export_op = &affs_export_ops;
517 	pr_debug("s_flags=%lX\n", sb->s_flags);
518 	return 0;
519 }
520 
521 static int affs_reconfigure(struct fs_context *fc)
522 {
523 	struct super_block	*sb = fc->root->d_sb;
524 	struct affs_context	*ctx = fc->fs_private;
525 	struct affs_sb_info	*sbi = AFFS_SB(sb);
526 	int			 res = 0;
527 
528 	sync_filesystem(sb);
529 	fc->sb_flags |= SB_NODIRATIME;
530 
531 	flush_delayed_work(&sbi->sb_work);
532 
533 	/*
534 	 * NB: Historically, only mount_flags, mode, uid, gic, prefix,
535 	 * and volume are accepted during remount.
536 	 */
537 	sbi->s_flags = ctx->mount_flags;
538 	sbi->s_mode  = ctx->mode;
539 	sbi->s_uid   = ctx->uid;
540 	sbi->s_gid   = ctx->gid;
541 	/* protect against readers */
542 	spin_lock(&sbi->symlink_lock);
543 	if (ctx->prefix) {
544 		kfree(sbi->s_prefix);
545 		sbi->s_prefix = ctx->prefix;
546 		ctx->prefix = NULL;
547 	}
548 	memcpy(sbi->s_volume, ctx->volume, 32);
549 	spin_unlock(&sbi->symlink_lock);
550 
551 	if ((bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb))
552 		return 0;
553 
554 	if (fc->sb_flags & SB_RDONLY)
555 		affs_free_bitmap(sb);
556 	else
557 		res = affs_init_bitmap(sb, &fc->sb_flags);
558 
559 	return res;
560 }
561 
562 static int
563 affs_statfs(struct dentry *dentry, struct kstatfs *buf)
564 {
565 	struct super_block *sb = dentry->d_sb;
566 	int		 free;
567 	u64		 id = huge_encode_dev(sb->s_bdev->bd_dev);
568 
569 	pr_debug("%s() partsize=%d, reserved=%d\n",
570 		 __func__, AFFS_SB(sb)->s_partition_size,
571 		 AFFS_SB(sb)->s_reserved);
572 
573 	free          = affs_count_free_blocks(sb);
574 	buf->f_type    = AFFS_SUPER_MAGIC;
575 	buf->f_bsize   = sb->s_blocksize;
576 	buf->f_blocks  = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
577 	buf->f_bfree   = free;
578 	buf->f_bavail  = free;
579 	buf->f_fsid    = u64_to_fsid(id);
580 	buf->f_namelen = AFFSNAMEMAX;
581 	return 0;
582 }
583 
584 static int affs_get_tree(struct fs_context *fc)
585 {
586 	return get_tree_bdev(fc, affs_fill_super);
587 }
588 
589 static void affs_kill_sb(struct super_block *sb)
590 {
591 	struct affs_sb_info *sbi = AFFS_SB(sb);
592 	kill_block_super(sb);
593 	if (sbi) {
594 		affs_free_bitmap(sb);
595 		affs_brelse(sbi->s_root_bh);
596 		kfree(sbi->s_prefix);
597 		mutex_destroy(&sbi->s_bmlock);
598 		kfree_rcu(sbi, rcu);
599 	}
600 }
601 
602 static void affs_free_fc(struct fs_context *fc)
603 {
604 	struct affs_context *ctx = fc->fs_private;
605 
606 	kfree(ctx->prefix);
607 	kfree(ctx);
608 }
609 
610 static const struct fs_context_operations affs_context_ops = {
611 	.parse_param	= affs_parse_param,
612 	.get_tree	= affs_get_tree,
613 	.reconfigure	= affs_reconfigure,
614 	.free		= affs_free_fc,
615 };
616 
617 static int affs_init_fs_context(struct fs_context *fc)
618 {
619 	struct affs_context *ctx;
620 
621 	ctx = kzalloc_obj(struct affs_context);
622 	if (!ctx)
623 		return -ENOMEM;
624 
625 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
626 		struct super_block *sb = fc->root->d_sb;
627 		struct affs_sb_info *sbi = AFFS_SB(sb);
628 
629 		/*
630 		 * NB: historically, no options other than volume were
631 		 * preserved across a remount unless they were explicitly
632 		 * passed in.
633 		 */
634 		memcpy(ctx->volume, sbi->s_volume, 32);
635 	} else {
636 		ctx->uid	= current_uid();
637 		ctx->gid	= current_gid();
638 		ctx->reserved	= 2;
639 		ctx->root_block	= -1;
640 		ctx->blocksize	= -1;
641 		ctx->volume[0]	= ':';
642 	}
643 
644 	fc->ops = &affs_context_ops;
645 	fc->fs_private = ctx;
646 
647 	return 0;
648 }
649 
650 static struct file_system_type affs_fs_type = {
651 	.owner		= THIS_MODULE,
652 	.name		= "affs",
653 	.kill_sb	= affs_kill_sb,
654 	.fs_flags	= FS_REQUIRES_DEV,
655 	.init_fs_context = affs_init_fs_context,
656 	.parameters	= affs_param_spec,
657 };
658 MODULE_ALIAS_FS("affs");
659 
660 static int __init init_affs_fs(void)
661 {
662 	int err = init_inodecache();
663 	if (err)
664 		goto out1;
665 	err = register_filesystem(&affs_fs_type);
666 	if (err)
667 		goto out;
668 	return 0;
669 out:
670 	destroy_inodecache();
671 out1:
672 	return err;
673 }
674 
675 static void __exit exit_affs_fs(void)
676 {
677 	unregister_filesystem(&affs_fs_type);
678 	destroy_inodecache();
679 }
680 
681 MODULE_DESCRIPTION("Amiga filesystem support for Linux");
682 MODULE_LICENSE("GPL");
683 
684 module_init(init_affs_fs)
685 module_exit(exit_affs_fs)
686