1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/adfs/super.c 4 * 5 * Copyright (C) 1997-1999 Russell King 6 */ 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/buffer_head.h> 10 #include <linux/parser.h> 11 #include <linux/mount.h> 12 #include <linux/seq_file.h> 13 #include <linux/slab.h> 14 #include <linux/statfs.h> 15 #include <linux/user_namespace.h> 16 #include "adfs.h" 17 #include "dir_f.h" 18 #include "dir_fplus.h" 19 20 #define ADFS_DEFAULT_OWNER_MASK S_IRWXU 21 #define ADFS_DEFAULT_OTHER_MASK (S_IRWXG | S_IRWXO) 22 23 void __adfs_error(struct super_block *sb, const char *function, const char *fmt, ...) 24 { 25 char error_buf[128]; 26 va_list args; 27 28 va_start(args, fmt); 29 vsnprintf(error_buf, sizeof(error_buf), fmt, args); 30 va_end(args); 31 32 printk(KERN_CRIT "ADFS-fs error (device %s)%s%s: %s\n", 33 sb->s_id, function ? ": " : "", 34 function ? function : "", error_buf); 35 } 36 37 static int adfs_checkdiscrecord(struct adfs_discrecord *dr) 38 { 39 int i; 40 41 /* sector size must be 256, 512 or 1024 bytes */ 42 if (dr->log2secsize != 8 && 43 dr->log2secsize != 9 && 44 dr->log2secsize != 10) 45 return 1; 46 47 /* idlen must be at least log2secsize + 3 */ 48 if (dr->idlen < dr->log2secsize + 3) 49 return 1; 50 51 /* we cannot have such a large disc that we 52 * are unable to represent sector offsets in 53 * 32 bits. This works out at 2.0 TB. 54 */ 55 if (le32_to_cpu(dr->disc_size_high) >> dr->log2secsize) 56 return 1; 57 58 /* idlen must be no greater than 19 v2 [1.0] */ 59 if (dr->idlen > 19) 60 return 1; 61 62 /* reserved bytes should be zero */ 63 for (i = 0; i < sizeof(dr->unused52); i++) 64 if (dr->unused52[i] != 0) 65 return 1; 66 67 return 0; 68 } 69 70 static unsigned char adfs_calczonecheck(struct super_block *sb, unsigned char *map) 71 { 72 unsigned int v0, v1, v2, v3; 73 int i; 74 75 v0 = v1 = v2 = v3 = 0; 76 for (i = sb->s_blocksize - 4; i; i -= 4) { 77 v0 += map[i] + (v3 >> 8); 78 v3 &= 0xff; 79 v1 += map[i + 1] + (v0 >> 8); 80 v0 &= 0xff; 81 v2 += map[i + 2] + (v1 >> 8); 82 v1 &= 0xff; 83 v3 += map[i + 3] + (v2 >> 8); 84 v2 &= 0xff; 85 } 86 v0 += v3 >> 8; 87 v1 += map[1] + (v0 >> 8); 88 v2 += map[2] + (v1 >> 8); 89 v3 += map[3] + (v2 >> 8); 90 91 return v0 ^ v1 ^ v2 ^ v3; 92 } 93 94 static int adfs_checkmap(struct super_block *sb, struct adfs_discmap *dm) 95 { 96 unsigned char crosscheck = 0, zonecheck = 1; 97 int i; 98 99 for (i = 0; i < ADFS_SB(sb)->s_map_size; i++) { 100 unsigned char *map; 101 102 map = dm[i].dm_bh->b_data; 103 104 if (adfs_calczonecheck(sb, map) != map[0]) { 105 adfs_error(sb, "zone %d fails zonecheck", i); 106 zonecheck = 0; 107 } 108 crosscheck ^= map[3]; 109 } 110 if (crosscheck != 0xff) 111 adfs_error(sb, "crosscheck != 0xff"); 112 return crosscheck == 0xff && zonecheck; 113 } 114 115 static void adfs_put_super(struct super_block *sb) 116 { 117 int i; 118 struct adfs_sb_info *asb = ADFS_SB(sb); 119 120 for (i = 0; i < asb->s_map_size; i++) 121 brelse(asb->s_map[i].dm_bh); 122 kfree(asb->s_map); 123 kfree_rcu(asb, rcu); 124 } 125 126 static int adfs_show_options(struct seq_file *seq, struct dentry *root) 127 { 128 struct adfs_sb_info *asb = ADFS_SB(root->d_sb); 129 130 if (!uid_eq(asb->s_uid, GLOBAL_ROOT_UID)) 131 seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, asb->s_uid)); 132 if (!gid_eq(asb->s_gid, GLOBAL_ROOT_GID)) 133 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, asb->s_gid)); 134 if (asb->s_owner_mask != ADFS_DEFAULT_OWNER_MASK) 135 seq_printf(seq, ",ownmask=%o", asb->s_owner_mask); 136 if (asb->s_other_mask != ADFS_DEFAULT_OTHER_MASK) 137 seq_printf(seq, ",othmask=%o", asb->s_other_mask); 138 if (asb->s_ftsuffix != 0) 139 seq_printf(seq, ",ftsuffix=%u", asb->s_ftsuffix); 140 141 return 0; 142 } 143 144 enum {Opt_uid, Opt_gid, Opt_ownmask, Opt_othmask, Opt_ftsuffix, Opt_err}; 145 146 static const match_table_t tokens = { 147 {Opt_uid, "uid=%u"}, 148 {Opt_gid, "gid=%u"}, 149 {Opt_ownmask, "ownmask=%o"}, 150 {Opt_othmask, "othmask=%o"}, 151 {Opt_ftsuffix, "ftsuffix=%u"}, 152 {Opt_err, NULL} 153 }; 154 155 static int parse_options(struct super_block *sb, char *options) 156 { 157 char *p; 158 struct adfs_sb_info *asb = ADFS_SB(sb); 159 int option; 160 161 if (!options) 162 return 0; 163 164 while ((p = strsep(&options, ",")) != NULL) { 165 substring_t args[MAX_OPT_ARGS]; 166 int token; 167 if (!*p) 168 continue; 169 170 token = match_token(p, tokens, args); 171 switch (token) { 172 case Opt_uid: 173 if (match_int(args, &option)) 174 return -EINVAL; 175 asb->s_uid = make_kuid(current_user_ns(), option); 176 if (!uid_valid(asb->s_uid)) 177 return -EINVAL; 178 break; 179 case Opt_gid: 180 if (match_int(args, &option)) 181 return -EINVAL; 182 asb->s_gid = make_kgid(current_user_ns(), option); 183 if (!gid_valid(asb->s_gid)) 184 return -EINVAL; 185 break; 186 case Opt_ownmask: 187 if (match_octal(args, &option)) 188 return -EINVAL; 189 asb->s_owner_mask = option; 190 break; 191 case Opt_othmask: 192 if (match_octal(args, &option)) 193 return -EINVAL; 194 asb->s_other_mask = option; 195 break; 196 case Opt_ftsuffix: 197 if (match_int(args, &option)) 198 return -EINVAL; 199 asb->s_ftsuffix = option; 200 break; 201 default: 202 printk("ADFS-fs: unrecognised mount option \"%s\" " 203 "or missing value\n", p); 204 return -EINVAL; 205 } 206 } 207 return 0; 208 } 209 210 static int adfs_remount(struct super_block *sb, int *flags, char *data) 211 { 212 sync_filesystem(sb); 213 *flags |= SB_NODIRATIME; 214 return parse_options(sb, data); 215 } 216 217 static int adfs_statfs(struct dentry *dentry, struct kstatfs *buf) 218 { 219 struct super_block *sb = dentry->d_sb; 220 struct adfs_sb_info *sbi = ADFS_SB(sb); 221 u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 222 223 buf->f_type = ADFS_SUPER_MAGIC; 224 buf->f_namelen = sbi->s_namelen; 225 buf->f_bsize = sb->s_blocksize; 226 buf->f_blocks = sbi->s_size; 227 buf->f_files = sbi->s_ids_per_zone * sbi->s_map_size; 228 buf->f_bavail = 229 buf->f_bfree = adfs_map_free(sb); 230 buf->f_ffree = (long)(buf->f_bfree * buf->f_files) / (long)buf->f_blocks; 231 buf->f_fsid.val[0] = (u32)id; 232 buf->f_fsid.val[1] = (u32)(id >> 32); 233 234 return 0; 235 } 236 237 static struct kmem_cache *adfs_inode_cachep; 238 239 static struct inode *adfs_alloc_inode(struct super_block *sb) 240 { 241 struct adfs_inode_info *ei; 242 ei = kmem_cache_alloc(adfs_inode_cachep, GFP_KERNEL); 243 if (!ei) 244 return NULL; 245 return &ei->vfs_inode; 246 } 247 248 static void adfs_free_inode(struct inode *inode) 249 { 250 kmem_cache_free(adfs_inode_cachep, ADFS_I(inode)); 251 } 252 253 static void init_once(void *foo) 254 { 255 struct adfs_inode_info *ei = (struct adfs_inode_info *) foo; 256 257 inode_init_once(&ei->vfs_inode); 258 } 259 260 static int __init init_inodecache(void) 261 { 262 adfs_inode_cachep = kmem_cache_create("adfs_inode_cache", 263 sizeof(struct adfs_inode_info), 264 0, (SLAB_RECLAIM_ACCOUNT| 265 SLAB_MEM_SPREAD|SLAB_ACCOUNT), 266 init_once); 267 if (adfs_inode_cachep == NULL) 268 return -ENOMEM; 269 return 0; 270 } 271 272 static void destroy_inodecache(void) 273 { 274 /* 275 * Make sure all delayed rcu free inodes are flushed before we 276 * destroy cache. 277 */ 278 rcu_barrier(); 279 kmem_cache_destroy(adfs_inode_cachep); 280 } 281 282 static const struct super_operations adfs_sops = { 283 .alloc_inode = adfs_alloc_inode, 284 .free_inode = adfs_free_inode, 285 .drop_inode = generic_delete_inode, 286 .write_inode = adfs_write_inode, 287 .put_super = adfs_put_super, 288 .statfs = adfs_statfs, 289 .remount_fs = adfs_remount, 290 .show_options = adfs_show_options, 291 }; 292 293 static struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr) 294 { 295 struct adfs_discmap *dm; 296 unsigned int map_addr, zone_size, nzones; 297 int i, zone; 298 struct adfs_sb_info *asb = ADFS_SB(sb); 299 300 nzones = asb->s_map_size; 301 zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare); 302 map_addr = (nzones >> 1) * zone_size - 303 ((nzones > 1) ? ADFS_DR_SIZE_BITS : 0); 304 map_addr = signed_asl(map_addr, asb->s_map2blk); 305 306 asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1); 307 308 dm = kmalloc_array(nzones, sizeof(*dm), GFP_KERNEL); 309 if (dm == NULL) { 310 adfs_error(sb, "not enough memory"); 311 return ERR_PTR(-ENOMEM); 312 } 313 314 for (zone = 0; zone < nzones; zone++, map_addr++) { 315 dm[zone].dm_startbit = 0; 316 dm[zone].dm_endbit = zone_size; 317 dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS; 318 dm[zone].dm_bh = sb_bread(sb, map_addr); 319 320 if (!dm[zone].dm_bh) { 321 adfs_error(sb, "unable to read map"); 322 goto error_free; 323 } 324 } 325 326 /* adjust the limits for the first and last map zones */ 327 i = zone - 1; 328 dm[0].dm_startblk = 0; 329 dm[0].dm_startbit = ADFS_DR_SIZE_BITS; 330 dm[i].dm_endbit = (le32_to_cpu(dr->disc_size_high) << (32 - dr->log2bpmb)) + 331 (le32_to_cpu(dr->disc_size) >> dr->log2bpmb) + 332 (ADFS_DR_SIZE_BITS - i * zone_size); 333 334 if (adfs_checkmap(sb, dm)) 335 return dm; 336 337 adfs_error(sb, "map corrupted"); 338 339 error_free: 340 while (--zone >= 0) 341 brelse(dm[zone].dm_bh); 342 343 kfree(dm); 344 return ERR_PTR(-EIO); 345 } 346 347 static inline unsigned long adfs_discsize(struct adfs_discrecord *dr, int block_bits) 348 { 349 unsigned long discsize; 350 351 discsize = le32_to_cpu(dr->disc_size_high) << (32 - block_bits); 352 discsize |= le32_to_cpu(dr->disc_size) >> block_bits; 353 354 return discsize; 355 } 356 357 static int adfs_fill_super(struct super_block *sb, void *data, int silent) 358 { 359 struct adfs_discrecord *dr; 360 struct buffer_head *bh; 361 struct object_info root_obj; 362 unsigned char *b_data; 363 struct adfs_sb_info *asb; 364 struct inode *root; 365 int ret = -EINVAL; 366 367 sb->s_flags |= SB_NODIRATIME; 368 369 asb = kzalloc(sizeof(*asb), GFP_KERNEL); 370 if (!asb) 371 return -ENOMEM; 372 sb->s_fs_info = asb; 373 374 /* set default options */ 375 asb->s_uid = GLOBAL_ROOT_UID; 376 asb->s_gid = GLOBAL_ROOT_GID; 377 asb->s_owner_mask = ADFS_DEFAULT_OWNER_MASK; 378 asb->s_other_mask = ADFS_DEFAULT_OTHER_MASK; 379 asb->s_ftsuffix = 0; 380 381 if (parse_options(sb, data)) 382 goto error; 383 384 sb_set_blocksize(sb, BLOCK_SIZE); 385 if (!(bh = sb_bread(sb, ADFS_DISCRECORD / BLOCK_SIZE))) { 386 adfs_error(sb, "unable to read superblock"); 387 ret = -EIO; 388 goto error; 389 } 390 391 b_data = bh->b_data + (ADFS_DISCRECORD % BLOCK_SIZE); 392 393 if (adfs_checkbblk(b_data)) { 394 if (!silent) 395 printk("VFS: Can't find an adfs filesystem on dev " 396 "%s.\n", sb->s_id); 397 ret = -EINVAL; 398 goto error_free_bh; 399 } 400 401 dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET); 402 403 /* 404 * Do some sanity checks on the ADFS disc record 405 */ 406 if (adfs_checkdiscrecord(dr)) { 407 if (!silent) 408 printk("VPS: Can't find an adfs filesystem on dev " 409 "%s.\n", sb->s_id); 410 ret = -EINVAL; 411 goto error_free_bh; 412 } 413 414 brelse(bh); 415 if (sb_set_blocksize(sb, 1 << dr->log2secsize)) { 416 bh = sb_bread(sb, ADFS_DISCRECORD / sb->s_blocksize); 417 if (!bh) { 418 adfs_error(sb, "couldn't read superblock on " 419 "2nd try."); 420 ret = -EIO; 421 goto error; 422 } 423 b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize); 424 if (adfs_checkbblk(b_data)) { 425 adfs_error(sb, "disc record mismatch, very weird!"); 426 ret = -EINVAL; 427 goto error_free_bh; 428 } 429 dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET); 430 } else { 431 if (!silent) 432 printk(KERN_ERR "VFS: Unsupported blocksize on dev " 433 "%s.\n", sb->s_id); 434 ret = -EINVAL; 435 goto error; 436 } 437 438 /* 439 * blocksize on this device should now be set to the ADFS log2secsize 440 */ 441 442 sb->s_magic = ADFS_SUPER_MAGIC; 443 asb->s_idlen = dr->idlen; 444 asb->s_map_size = dr->nzones | (dr->nzones_high << 8); 445 asb->s_map2blk = dr->log2bpmb - dr->log2secsize; 446 asb->s_size = adfs_discsize(dr, sb->s_blocksize_bits); 447 asb->s_version = dr->format_version; 448 asb->s_log2sharesize = dr->log2sharesize; 449 450 asb->s_map = adfs_read_map(sb, dr); 451 if (IS_ERR(asb->s_map)) { 452 ret = PTR_ERR(asb->s_map); 453 goto error_free_bh; 454 } 455 456 brelse(bh); 457 458 /* 459 * set up enough so that we can read an inode 460 */ 461 sb->s_op = &adfs_sops; 462 463 dr = (struct adfs_discrecord *)(asb->s_map[0].dm_bh->b_data + 4); 464 465 root_obj.parent_id = root_obj.file_id = le32_to_cpu(dr->root); 466 root_obj.name_len = 0; 467 /* Set root object date as 01 Jan 1987 00:00:00 */ 468 root_obj.loadaddr = 0xfff0003f; 469 root_obj.execaddr = 0xec22c000; 470 root_obj.size = ADFS_NEWDIR_SIZE; 471 root_obj.attr = ADFS_NDA_DIRECTORY | ADFS_NDA_OWNER_READ | 472 ADFS_NDA_OWNER_WRITE | ADFS_NDA_PUBLIC_READ; 473 root_obj.filetype = -1; 474 475 /* 476 * If this is a F+ disk with variable length directories, 477 * get the root_size from the disc record. 478 */ 479 if (asb->s_version) { 480 root_obj.size = le32_to_cpu(dr->root_size); 481 asb->s_dir = &adfs_fplus_dir_ops; 482 asb->s_namelen = ADFS_FPLUS_NAME_LEN; 483 } else { 484 asb->s_dir = &adfs_f_dir_ops; 485 asb->s_namelen = ADFS_F_NAME_LEN; 486 } 487 /* 488 * ,xyz hex filetype suffix may be added by driver 489 * to files that have valid RISC OS filetype 490 */ 491 if (asb->s_ftsuffix) 492 asb->s_namelen += 4; 493 494 sb->s_d_op = &adfs_dentry_operations; 495 root = adfs_iget(sb, &root_obj); 496 sb->s_root = d_make_root(root); 497 if (!sb->s_root) { 498 int i; 499 for (i = 0; i < asb->s_map_size; i++) 500 brelse(asb->s_map[i].dm_bh); 501 kfree(asb->s_map); 502 adfs_error(sb, "get root inode failed\n"); 503 ret = -EIO; 504 goto error; 505 } 506 return 0; 507 508 error_free_bh: 509 brelse(bh); 510 error: 511 sb->s_fs_info = NULL; 512 kfree(asb); 513 return ret; 514 } 515 516 static struct dentry *adfs_mount(struct file_system_type *fs_type, 517 int flags, const char *dev_name, void *data) 518 { 519 return mount_bdev(fs_type, flags, dev_name, data, adfs_fill_super); 520 } 521 522 static struct file_system_type adfs_fs_type = { 523 .owner = THIS_MODULE, 524 .name = "adfs", 525 .mount = adfs_mount, 526 .kill_sb = kill_block_super, 527 .fs_flags = FS_REQUIRES_DEV, 528 }; 529 MODULE_ALIAS_FS("adfs"); 530 531 static int __init init_adfs_fs(void) 532 { 533 int err = init_inodecache(); 534 if (err) 535 goto out1; 536 err = register_filesystem(&adfs_fs_type); 537 if (err) 538 goto out; 539 return 0; 540 out: 541 destroy_inodecache(); 542 out1: 543 return err; 544 } 545 546 static void __exit exit_adfs_fs(void) 547 { 548 unregister_filesystem(&adfs_fs_type); 549 destroy_inodecache(); 550 } 551 552 module_init(init_adfs_fs) 553 module_exit(exit_adfs_fs) 554 MODULE_LICENSE("GPL"); 555