0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/statfs.h>
0017 #include <linux/parser.h>
0018 #include <linux/magic.h>
0019 #include <linux/sched.h>
0020 #include <linux/cred.h>
0021 #include <linux/slab.h>
0022 #include <linux/writeback.h>
0023 #include <linux/blkdev.h>
0024 #include <linux/seq_file.h>
0025 #include <linux/iversion.h>
0026 #include "affs.h"
0027
0028 static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
0029 static int affs_show_options(struct seq_file *m, struct dentry *root);
0030 static int affs_remount (struct super_block *sb, int *flags, char *data);
0031
0032 static void
0033 affs_commit_super(struct super_block *sb, int wait)
0034 {
0035 struct affs_sb_info *sbi = AFFS_SB(sb);
0036 struct buffer_head *bh = sbi->s_root_bh;
0037 struct affs_root_tail *tail = AFFS_ROOT_TAIL(sb, bh);
0038
0039 lock_buffer(bh);
0040 affs_secs_to_datestamp(ktime_get_real_seconds(), &tail->disk_change);
0041 affs_fix_checksum(sb, bh);
0042 unlock_buffer(bh);
0043
0044 mark_buffer_dirty(bh);
0045 if (wait)
0046 sync_dirty_buffer(bh);
0047 }
0048
0049 static void
0050 affs_put_super(struct super_block *sb)
0051 {
0052 struct affs_sb_info *sbi = AFFS_SB(sb);
0053 pr_debug("%s()\n", __func__);
0054
0055 cancel_delayed_work_sync(&sbi->sb_work);
0056 }
0057
0058 static int
0059 affs_sync_fs(struct super_block *sb, int wait)
0060 {
0061 affs_commit_super(sb, wait);
0062 return 0;
0063 }
0064
0065 static void flush_superblock(struct work_struct *work)
0066 {
0067 struct affs_sb_info *sbi;
0068 struct super_block *sb;
0069
0070 sbi = container_of(work, struct affs_sb_info, sb_work.work);
0071 sb = sbi->sb;
0072
0073 spin_lock(&sbi->work_lock);
0074 sbi->work_queued = 0;
0075 spin_unlock(&sbi->work_lock);
0076
0077 affs_commit_super(sb, 1);
0078 }
0079
0080 void affs_mark_sb_dirty(struct super_block *sb)
0081 {
0082 struct affs_sb_info *sbi = AFFS_SB(sb);
0083 unsigned long delay;
0084
0085 if (sb_rdonly(sb))
0086 return;
0087
0088 spin_lock(&sbi->work_lock);
0089 if (!sbi->work_queued) {
0090 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
0091 queue_delayed_work(system_long_wq, &sbi->sb_work, delay);
0092 sbi->work_queued = 1;
0093 }
0094 spin_unlock(&sbi->work_lock);
0095 }
0096
0097 static struct kmem_cache * affs_inode_cachep;
0098
0099 static struct inode *affs_alloc_inode(struct super_block *sb)
0100 {
0101 struct affs_inode_info *i;
0102
0103 i = alloc_inode_sb(sb, affs_inode_cachep, GFP_KERNEL);
0104 if (!i)
0105 return NULL;
0106
0107 inode_set_iversion(&i->vfs_inode, 1);
0108 i->i_lc = NULL;
0109 i->i_ext_bh = NULL;
0110 i->i_pa_cnt = 0;
0111
0112 return &i->vfs_inode;
0113 }
0114
0115 static void affs_free_inode(struct inode *inode)
0116 {
0117 kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
0118 }
0119
0120 static void init_once(void *foo)
0121 {
0122 struct affs_inode_info *ei = (struct affs_inode_info *) foo;
0123
0124 mutex_init(&ei->i_link_lock);
0125 mutex_init(&ei->i_ext_lock);
0126 inode_init_once(&ei->vfs_inode);
0127 }
0128
0129 static int __init init_inodecache(void)
0130 {
0131 affs_inode_cachep = kmem_cache_create("affs_inode_cache",
0132 sizeof(struct affs_inode_info),
0133 0, (SLAB_RECLAIM_ACCOUNT|
0134 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
0135 init_once);
0136 if (affs_inode_cachep == NULL)
0137 return -ENOMEM;
0138 return 0;
0139 }
0140
0141 static void destroy_inodecache(void)
0142 {
0143
0144
0145
0146
0147 rcu_barrier();
0148 kmem_cache_destroy(affs_inode_cachep);
0149 }
0150
0151 static const struct super_operations affs_sops = {
0152 .alloc_inode = affs_alloc_inode,
0153 .free_inode = affs_free_inode,
0154 .write_inode = affs_write_inode,
0155 .evict_inode = affs_evict_inode,
0156 .put_super = affs_put_super,
0157 .sync_fs = affs_sync_fs,
0158 .statfs = affs_statfs,
0159 .remount_fs = affs_remount,
0160 .show_options = affs_show_options,
0161 };
0162
0163 enum {
0164 Opt_bs, Opt_mode, Opt_mufs, Opt_notruncate, Opt_prefix, Opt_protect,
0165 Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
0166 Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
0167 };
0168
0169 static const match_table_t tokens = {
0170 {Opt_bs, "bs=%u"},
0171 {Opt_mode, "mode=%o"},
0172 {Opt_mufs, "mufs"},
0173 {Opt_notruncate, "nofilenametruncate"},
0174 {Opt_prefix, "prefix=%s"},
0175 {Opt_protect, "protect"},
0176 {Opt_reserved, "reserved=%u"},
0177 {Opt_root, "root=%u"},
0178 {Opt_setgid, "setgid=%u"},
0179 {Opt_setuid, "setuid=%u"},
0180 {Opt_verbose, "verbose"},
0181 {Opt_volume, "volume=%s"},
0182 {Opt_ignore, "grpquota"},
0183 {Opt_ignore, "noquota"},
0184 {Opt_ignore, "quota"},
0185 {Opt_ignore, "usrquota"},
0186 {Opt_err, NULL},
0187 };
0188
0189 static int
0190 parse_options(char *options, kuid_t *uid, kgid_t *gid, int *mode, int *reserved, s32 *root,
0191 int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
0192 {
0193 char *p;
0194 substring_t args[MAX_OPT_ARGS];
0195
0196
0197
0198 *uid = current_uid();
0199 *gid = current_gid();
0200 *reserved = 2;
0201 *root = -1;
0202 *blocksize = -1;
0203 volume[0] = ':';
0204 volume[1] = 0;
0205 *mount_opts = 0;
0206 if (!options)
0207 return 1;
0208
0209 while ((p = strsep(&options, ",")) != NULL) {
0210 int token, n, option;
0211 if (!*p)
0212 continue;
0213
0214 token = match_token(p, tokens, args);
0215 switch (token) {
0216 case Opt_bs:
0217 if (match_int(&args[0], &n))
0218 return 0;
0219 if (n != 512 && n != 1024 && n != 2048
0220 && n != 4096) {
0221 pr_warn("Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
0222 return 0;
0223 }
0224 *blocksize = n;
0225 break;
0226 case Opt_mode:
0227 if (match_octal(&args[0], &option))
0228 return 0;
0229 *mode = option & 0777;
0230 affs_set_opt(*mount_opts, SF_SETMODE);
0231 break;
0232 case Opt_mufs:
0233 affs_set_opt(*mount_opts, SF_MUFS);
0234 break;
0235 case Opt_notruncate:
0236 affs_set_opt(*mount_opts, SF_NO_TRUNCATE);
0237 break;
0238 case Opt_prefix:
0239 kfree(*prefix);
0240 *prefix = match_strdup(&args[0]);
0241 if (!*prefix)
0242 return 0;
0243 affs_set_opt(*mount_opts, SF_PREFIX);
0244 break;
0245 case Opt_protect:
0246 affs_set_opt(*mount_opts, SF_IMMUTABLE);
0247 break;
0248 case Opt_reserved:
0249 if (match_int(&args[0], reserved))
0250 return 0;
0251 break;
0252 case Opt_root:
0253 if (match_int(&args[0], root))
0254 return 0;
0255 break;
0256 case Opt_setgid:
0257 if (match_int(&args[0], &option))
0258 return 0;
0259 *gid = make_kgid(current_user_ns(), option);
0260 if (!gid_valid(*gid))
0261 return 0;
0262 affs_set_opt(*mount_opts, SF_SETGID);
0263 break;
0264 case Opt_setuid:
0265 if (match_int(&args[0], &option))
0266 return 0;
0267 *uid = make_kuid(current_user_ns(), option);
0268 if (!uid_valid(*uid))
0269 return 0;
0270 affs_set_opt(*mount_opts, SF_SETUID);
0271 break;
0272 case Opt_verbose:
0273 affs_set_opt(*mount_opts, SF_VERBOSE);
0274 break;
0275 case Opt_volume: {
0276 char *vol = match_strdup(&args[0]);
0277 if (!vol)
0278 return 0;
0279 strlcpy(volume, vol, 32);
0280 kfree(vol);
0281 break;
0282 }
0283 case Opt_ignore:
0284
0285 break;
0286 default:
0287 pr_warn("Unrecognized mount option \"%s\" or missing value\n",
0288 p);
0289 return 0;
0290 }
0291 }
0292 return 1;
0293 }
0294
0295 static int affs_show_options(struct seq_file *m, struct dentry *root)
0296 {
0297 struct super_block *sb = root->d_sb;
0298 struct affs_sb_info *sbi = AFFS_SB(sb);
0299
0300 if (sb->s_blocksize)
0301 seq_printf(m, ",bs=%lu", sb->s_blocksize);
0302 if (affs_test_opt(sbi->s_flags, SF_SETMODE))
0303 seq_printf(m, ",mode=%o", sbi->s_mode);
0304 if (affs_test_opt(sbi->s_flags, SF_MUFS))
0305 seq_puts(m, ",mufs");
0306 if (affs_test_opt(sbi->s_flags, SF_NO_TRUNCATE))
0307 seq_puts(m, ",nofilenametruncate");
0308 if (affs_test_opt(sbi->s_flags, SF_PREFIX))
0309 seq_printf(m, ",prefix=%s", sbi->s_prefix);
0310 if (affs_test_opt(sbi->s_flags, SF_IMMUTABLE))
0311 seq_puts(m, ",protect");
0312 if (sbi->s_reserved != 2)
0313 seq_printf(m, ",reserved=%u", sbi->s_reserved);
0314 if (sbi->s_root_block != (sbi->s_reserved + sbi->s_partition_size - 1) / 2)
0315 seq_printf(m, ",root=%u", sbi->s_root_block);
0316 if (affs_test_opt(sbi->s_flags, SF_SETGID))
0317 seq_printf(m, ",setgid=%u",
0318 from_kgid_munged(&init_user_ns, sbi->s_gid));
0319 if (affs_test_opt(sbi->s_flags, SF_SETUID))
0320 seq_printf(m, ",setuid=%u",
0321 from_kuid_munged(&init_user_ns, sbi->s_uid));
0322 if (affs_test_opt(sbi->s_flags, SF_VERBOSE))
0323 seq_puts(m, ",verbose");
0324 if (sbi->s_volume[0])
0325 seq_printf(m, ",volume=%s", sbi->s_volume);
0326 return 0;
0327 }
0328
0329
0330
0331
0332
0333 static int affs_fill_super(struct super_block *sb, void *data, int silent)
0334 {
0335 struct affs_sb_info *sbi;
0336 struct buffer_head *root_bh = NULL;
0337 struct buffer_head *boot_bh;
0338 struct inode *root_inode = NULL;
0339 s32 root_block;
0340 int size, blocksize;
0341 u32 chksum;
0342 int num_bm;
0343 int i, j;
0344 kuid_t uid;
0345 kgid_t gid;
0346 int reserved;
0347 unsigned long mount_flags;
0348 int tmp_flags;
0349 u8 sig[4];
0350 int ret;
0351
0352 pr_debug("read_super(%s)\n", data ? (const char *)data : "no options");
0353
0354 sb->s_magic = AFFS_SUPER_MAGIC;
0355 sb->s_op = &affs_sops;
0356 sb->s_flags |= SB_NODIRATIME;
0357
0358 sb->s_time_gran = NSEC_PER_SEC;
0359 sb->s_time_min = sys_tz.tz_minuteswest * 60 + AFFS_EPOCH_DELTA;
0360 sb->s_time_max = 86400LL * U32_MAX + 86400 + sb->s_time_min;
0361
0362 sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
0363 if (!sbi)
0364 return -ENOMEM;
0365
0366 sb->s_fs_info = sbi;
0367 sbi->sb = sb;
0368 mutex_init(&sbi->s_bmlock);
0369 spin_lock_init(&sbi->symlink_lock);
0370 spin_lock_init(&sbi->work_lock);
0371 INIT_DELAYED_WORK(&sbi->sb_work, flush_superblock);
0372
0373 if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
0374 &blocksize,&sbi->s_prefix,
0375 sbi->s_volume, &mount_flags)) {
0376 pr_err("Error parsing options\n");
0377 return -EINVAL;
0378 }
0379
0380
0381 sbi->s_flags = mount_flags;
0382 sbi->s_mode = i;
0383 sbi->s_uid = uid;
0384 sbi->s_gid = gid;
0385 sbi->s_reserved= reserved;
0386
0387
0388
0389
0390
0391
0392 size = bdev_nr_sectors(sb->s_bdev);
0393 pr_debug("initial blocksize=%d, #blocks=%d\n", 512, size);
0394
0395 affs_set_blocksize(sb, PAGE_SIZE);
0396
0397
0398 i = bdev_logical_block_size(sb->s_bdev);
0399 j = PAGE_SIZE;
0400 if (blocksize > 0) {
0401 i = j = blocksize;
0402 size = size / (blocksize / 512);
0403 }
0404
0405 for (blocksize = i; blocksize <= j; blocksize <<= 1, size >>= 1) {
0406 sbi->s_root_block = root_block;
0407 if (root_block < 0)
0408 sbi->s_root_block = (reserved + size - 1) / 2;
0409 pr_debug("setting blocksize to %d\n", blocksize);
0410 affs_set_blocksize(sb, blocksize);
0411 sbi->s_partition_size = size;
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423 for (num_bm = 0; num_bm < 2; num_bm++) {
0424 pr_debug("Dev %s, trying root=%u, bs=%d, "
0425 "size=%d, reserved=%d\n",
0426 sb->s_id,
0427 sbi->s_root_block + num_bm,
0428 blocksize, size, reserved);
0429 root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
0430 if (!root_bh)
0431 continue;
0432 if (!affs_checksum_block(sb, root_bh) &&
0433 be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
0434 be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
0435 sbi->s_hashsize = blocksize / 4 - 56;
0436 sbi->s_root_block += num_bm;
0437 goto got_root;
0438 }
0439 affs_brelse(root_bh);
0440 root_bh = NULL;
0441 }
0442 }
0443 if (!silent)
0444 pr_err("No valid root block on device %s\n", sb->s_id);
0445 return -EINVAL;
0446
0447
0448 got_root:
0449
0450 sbi->s_root_bh = root_bh;
0451 root_block = sbi->s_root_block;
0452
0453
0454 boot_bh = sb_bread(sb, 0);
0455 if (!boot_bh) {
0456 pr_err("Cannot read boot block\n");
0457 return -EINVAL;
0458 }
0459 memcpy(sig, boot_bh->b_data, 4);
0460 brelse(boot_bh);
0461 chksum = be32_to_cpu(*(__be32 *)sig);
0462
0463
0464
0465
0466
0467 if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
0468 || chksum == MUFS_DCOFS) && !sb_rdonly(sb)) {
0469 pr_notice("Dircache FS - mounting %s read only\n", sb->s_id);
0470 sb->s_flags |= SB_RDONLY;
0471 }
0472 switch (chksum) {
0473 case MUFS_FS:
0474 case MUFS_INTLFFS:
0475 case MUFS_DCFFS:
0476 affs_set_opt(sbi->s_flags, SF_MUFS);
0477 fallthrough;
0478 case FS_INTLFFS:
0479 case FS_DCFFS:
0480 affs_set_opt(sbi->s_flags, SF_INTL);
0481 break;
0482 case MUFS_FFS:
0483 affs_set_opt(sbi->s_flags, SF_MUFS);
0484 break;
0485 case FS_FFS:
0486 break;
0487 case MUFS_OFS:
0488 affs_set_opt(sbi->s_flags, SF_MUFS);
0489 fallthrough;
0490 case FS_OFS:
0491 affs_set_opt(sbi->s_flags, SF_OFS);
0492 sb->s_flags |= SB_NOEXEC;
0493 break;
0494 case MUFS_DCOFS:
0495 case MUFS_INTLOFS:
0496 affs_set_opt(sbi->s_flags, SF_MUFS);
0497 fallthrough;
0498 case FS_DCOFS:
0499 case FS_INTLOFS:
0500 affs_set_opt(sbi->s_flags, SF_INTL);
0501 affs_set_opt(sbi->s_flags, SF_OFS);
0502 sb->s_flags |= SB_NOEXEC;
0503 break;
0504 default:
0505 pr_err("Unknown filesystem on device %s: %08X\n",
0506 sb->s_id, chksum);
0507 return -EINVAL;
0508 }
0509
0510 if (affs_test_opt(mount_flags, SF_VERBOSE)) {
0511 u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
0512 pr_notice("Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
0513 len > 31 ? 31 : len,
0514 AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
0515 sig, sig[3] + '0', blocksize);
0516 }
0517
0518 sb->s_flags |= SB_NODEV | SB_NOSUID;
0519
0520 sbi->s_data_blksize = sb->s_blocksize;
0521 if (affs_test_opt(sbi->s_flags, SF_OFS))
0522 sbi->s_data_blksize -= 24;
0523
0524 tmp_flags = sb->s_flags;
0525 ret = affs_init_bitmap(sb, &tmp_flags);
0526 if (ret)
0527 return ret;
0528 sb->s_flags = tmp_flags;
0529
0530
0531
0532 root_inode = affs_iget(sb, root_block);
0533 if (IS_ERR(root_inode))
0534 return PTR_ERR(root_inode);
0535
0536 if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL))
0537 sb->s_d_op = &affs_intl_dentry_operations;
0538 else
0539 sb->s_d_op = &affs_dentry_operations;
0540
0541 sb->s_root = d_make_root(root_inode);
0542 if (!sb->s_root) {
0543 pr_err("AFFS: Get root inode failed\n");
0544 return -ENOMEM;
0545 }
0546
0547 sb->s_export_op = &affs_export_ops;
0548 pr_debug("s_flags=%lX\n", sb->s_flags);
0549 return 0;
0550 }
0551
0552 static int
0553 affs_remount(struct super_block *sb, int *flags, char *data)
0554 {
0555 struct affs_sb_info *sbi = AFFS_SB(sb);
0556 int blocksize;
0557 kuid_t uid;
0558 kgid_t gid;
0559 int mode;
0560 int reserved;
0561 int root_block;
0562 unsigned long mount_flags;
0563 int res = 0;
0564 char volume[32];
0565 char *prefix = NULL;
0566
0567 pr_debug("%s(flags=0x%x,opts=\"%s\")\n", __func__, *flags, data);
0568
0569 sync_filesystem(sb);
0570 *flags |= SB_NODIRATIME;
0571
0572 memcpy(volume, sbi->s_volume, 32);
0573 if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
0574 &blocksize, &prefix, volume,
0575 &mount_flags)) {
0576 kfree(prefix);
0577 return -EINVAL;
0578 }
0579
0580 flush_delayed_work(&sbi->sb_work);
0581
0582 sbi->s_flags = mount_flags;
0583 sbi->s_mode = mode;
0584 sbi->s_uid = uid;
0585 sbi->s_gid = gid;
0586
0587 spin_lock(&sbi->symlink_lock);
0588 if (prefix) {
0589 kfree(sbi->s_prefix);
0590 sbi->s_prefix = prefix;
0591 }
0592 memcpy(sbi->s_volume, volume, 32);
0593 spin_unlock(&sbi->symlink_lock);
0594
0595 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
0596 return 0;
0597
0598 if (*flags & SB_RDONLY)
0599 affs_free_bitmap(sb);
0600 else
0601 res = affs_init_bitmap(sb, flags);
0602
0603 return res;
0604 }
0605
0606 static int
0607 affs_statfs(struct dentry *dentry, struct kstatfs *buf)
0608 {
0609 struct super_block *sb = dentry->d_sb;
0610 int free;
0611 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
0612
0613 pr_debug("%s() partsize=%d, reserved=%d\n",
0614 __func__, AFFS_SB(sb)->s_partition_size,
0615 AFFS_SB(sb)->s_reserved);
0616
0617 free = affs_count_free_blocks(sb);
0618 buf->f_type = AFFS_SUPER_MAGIC;
0619 buf->f_bsize = sb->s_blocksize;
0620 buf->f_blocks = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
0621 buf->f_bfree = free;
0622 buf->f_bavail = free;
0623 buf->f_fsid = u64_to_fsid(id);
0624 buf->f_namelen = AFFSNAMEMAX;
0625 return 0;
0626 }
0627
0628 static struct dentry *affs_mount(struct file_system_type *fs_type,
0629 int flags, const char *dev_name, void *data)
0630 {
0631 return mount_bdev(fs_type, flags, dev_name, data, affs_fill_super);
0632 }
0633
0634 static void affs_kill_sb(struct super_block *sb)
0635 {
0636 struct affs_sb_info *sbi = AFFS_SB(sb);
0637 kill_block_super(sb);
0638 if (sbi) {
0639 affs_free_bitmap(sb);
0640 affs_brelse(sbi->s_root_bh);
0641 kfree(sbi->s_prefix);
0642 mutex_destroy(&sbi->s_bmlock);
0643 kfree(sbi);
0644 }
0645 }
0646
0647 static struct file_system_type affs_fs_type = {
0648 .owner = THIS_MODULE,
0649 .name = "affs",
0650 .mount = affs_mount,
0651 .kill_sb = affs_kill_sb,
0652 .fs_flags = FS_REQUIRES_DEV,
0653 };
0654 MODULE_ALIAS_FS("affs");
0655
0656 static int __init init_affs_fs(void)
0657 {
0658 int err = init_inodecache();
0659 if (err)
0660 goto out1;
0661 err = register_filesystem(&affs_fs_type);
0662 if (err)
0663 goto out;
0664 return 0;
0665 out:
0666 destroy_inodecache();
0667 out1:
0668 return err;
0669 }
0670
0671 static void __exit exit_affs_fs(void)
0672 {
0673 unregister_filesystem(&affs_fs_type);
0674 destroy_inodecache();
0675 }
0676
0677 MODULE_DESCRIPTION("Amiga filesystem support for Linux");
0678 MODULE_LICENSE("GPL");
0679
0680 module_init(init_affs_fs)
0681 module_exit(exit_affs_fs)