super.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/parser.h>
  10. #include <linux/mount.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/statfs.h>
  14. #include <linux/user_namespace.h>
  15. #include <linux/blkdev.h>
  16. #include "adfs.h"
  17. #include "dir_f.h"
  18. #include "dir_fplus.h"
  19. #define ADFS_SB_FLAGS SB_NOATIME
  20. #define ADFS_DEFAULT_OWNER_MASK S_IRWXU
  21. #define ADFS_DEFAULT_OTHER_MASK (S_IRWXG | S_IRWXO)
  22. void __adfs_error(struct super_block *sb, const char *function, const char *fmt, ...)
  23. {
  24. struct va_format vaf;
  25. va_list args;
  26. va_start(args, fmt);
  27. vaf.fmt = fmt;
  28. vaf.va = &args;
  29. printk(KERN_CRIT "ADFS-fs error (device %s)%s%s: %pV\n",
  30. sb->s_id, function ? ": " : "",
  31. function ? function : "", &vaf);
  32. va_end(args);
  33. }
  34. void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...)
  35. {
  36. struct va_format vaf;
  37. va_list args;
  38. va_start(args, fmt);
  39. vaf.fmt = fmt;
  40. vaf.va = &args;
  41. printk("%sADFS-fs (%s): %pV\n", pfx, sb->s_id, &vaf);
  42. va_end(args);
  43. }
  44. static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
  45. {
  46. unsigned int max_idlen;
  47. int i;
  48. /* sector size must be 256, 512 or 1024 bytes */
  49. if (dr->log2secsize != 8 &&
  50. dr->log2secsize != 9 &&
  51. dr->log2secsize != 10)
  52. return 1;
  53. /* idlen must be at least log2secsize + 3 */
  54. if (dr->idlen < dr->log2secsize + 3)
  55. return 1;
  56. /* we cannot have such a large disc that we
  57. * are unable to represent sector offsets in
  58. * 32 bits. This works out at 2.0 TB.
  59. */
  60. if (le32_to_cpu(dr->disc_size_high) >> dr->log2secsize)
  61. return 1;
  62. /*
  63. * Maximum idlen is limited to 16 bits for new directories by
  64. * the three-byte storage of an indirect disc address. For
  65. * big directories, idlen must be no greater than 19 v2 [1.0]
  66. */
  67. max_idlen = dr->format_version ? 19 : 16;
  68. if (dr->idlen > max_idlen)
  69. return 1;
  70. /* reserved bytes should be zero */
  71. for (i = 0; i < sizeof(dr->unused52); i++)
  72. if (dr->unused52[i] != 0)
  73. return 1;
  74. return 0;
  75. }
  76. static void adfs_put_super(struct super_block *sb)
  77. {
  78. struct adfs_sb_info *asb = ADFS_SB(sb);
  79. adfs_free_map(sb);
  80. kfree_rcu(asb, rcu);
  81. }
  82. static int adfs_show_options(struct seq_file *seq, struct dentry *root)
  83. {
  84. struct adfs_sb_info *asb = ADFS_SB(root->d_sb);
  85. if (!uid_eq(asb->s_uid, GLOBAL_ROOT_UID))
  86. seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, asb->s_uid));
  87. if (!gid_eq(asb->s_gid, GLOBAL_ROOT_GID))
  88. seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, asb->s_gid));
  89. if (asb->s_owner_mask != ADFS_DEFAULT_OWNER_MASK)
  90. seq_printf(seq, ",ownmask=%o", asb->s_owner_mask);
  91. if (asb->s_other_mask != ADFS_DEFAULT_OTHER_MASK)
  92. seq_printf(seq, ",othmask=%o", asb->s_other_mask);
  93. if (asb->s_ftsuffix != 0)
  94. seq_printf(seq, ",ftsuffix=%u", asb->s_ftsuffix);
  95. return 0;
  96. }
  97. enum {Opt_uid, Opt_gid, Opt_ownmask, Opt_othmask, Opt_ftsuffix, Opt_err};
  98. static const match_table_t tokens = {
  99. {Opt_uid, "uid=%u"},
  100. {Opt_gid, "gid=%u"},
  101. {Opt_ownmask, "ownmask=%o"},
  102. {Opt_othmask, "othmask=%o"},
  103. {Opt_ftsuffix, "ftsuffix=%u"},
  104. {Opt_err, NULL}
  105. };
  106. static int parse_options(struct super_block *sb, struct adfs_sb_info *asb,
  107. char *options)
  108. {
  109. char *p;
  110. int option;
  111. if (!options)
  112. return 0;
  113. while ((p = strsep(&options, ",")) != NULL) {
  114. substring_t args[MAX_OPT_ARGS];
  115. int token;
  116. if (!*p)
  117. continue;
  118. token = match_token(p, tokens, args);
  119. switch (token) {
  120. case Opt_uid:
  121. if (match_int(args, &option))
  122. return -EINVAL;
  123. asb->s_uid = make_kuid(current_user_ns(), option);
  124. if (!uid_valid(asb->s_uid))
  125. return -EINVAL;
  126. break;
  127. case Opt_gid:
  128. if (match_int(args, &option))
  129. return -EINVAL;
  130. asb->s_gid = make_kgid(current_user_ns(), option);
  131. if (!gid_valid(asb->s_gid))
  132. return -EINVAL;
  133. break;
  134. case Opt_ownmask:
  135. if (match_octal(args, &option))
  136. return -EINVAL;
  137. asb->s_owner_mask = option;
  138. break;
  139. case Opt_othmask:
  140. if (match_octal(args, &option))
  141. return -EINVAL;
  142. asb->s_other_mask = option;
  143. break;
  144. case Opt_ftsuffix:
  145. if (match_int(args, &option))
  146. return -EINVAL;
  147. asb->s_ftsuffix = option;
  148. break;
  149. default:
  150. adfs_msg(sb, KERN_ERR,
  151. "unrecognised mount option \"%s\" or missing value",
  152. p);
  153. return -EINVAL;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int adfs_remount(struct super_block *sb, int *flags, char *data)
  159. {
  160. struct adfs_sb_info temp_asb;
  161. int ret;
  162. sync_filesystem(sb);
  163. *flags |= ADFS_SB_FLAGS;
  164. temp_asb = *ADFS_SB(sb);
  165. ret = parse_options(sb, &temp_asb, data);
  166. if (ret == 0)
  167. *ADFS_SB(sb) = temp_asb;
  168. return ret;
  169. }
  170. static int adfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  171. {
  172. struct super_block *sb = dentry->d_sb;
  173. struct adfs_sb_info *sbi = ADFS_SB(sb);
  174. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  175. adfs_map_statfs(sb, buf);
  176. buf->f_type = ADFS_SUPER_MAGIC;
  177. buf->f_namelen = sbi->s_namelen;
  178. buf->f_bsize = sb->s_blocksize;
  179. buf->f_ffree = (long)(buf->f_bfree * buf->f_files) / (long)buf->f_blocks;
  180. buf->f_fsid = u64_to_fsid(id);
  181. return 0;
  182. }
  183. static struct kmem_cache *adfs_inode_cachep;
  184. static struct inode *adfs_alloc_inode(struct super_block *sb)
  185. {
  186. struct adfs_inode_info *ei;
  187. ei = alloc_inode_sb(sb, adfs_inode_cachep, GFP_KERNEL);
  188. if (!ei)
  189. return NULL;
  190. return &ei->vfs_inode;
  191. }
  192. static void adfs_free_inode(struct inode *inode)
  193. {
  194. kmem_cache_free(adfs_inode_cachep, ADFS_I(inode));
  195. }
  196. static int adfs_drop_inode(struct inode *inode)
  197. {
  198. /* always drop inodes if we are read-only */
  199. return !IS_ENABLED(CONFIG_ADFS_FS_RW) || IS_RDONLY(inode);
  200. }
  201. static void init_once(void *foo)
  202. {
  203. struct adfs_inode_info *ei = (struct adfs_inode_info *) foo;
  204. inode_init_once(&ei->vfs_inode);
  205. }
  206. static int __init init_inodecache(void)
  207. {
  208. adfs_inode_cachep = kmem_cache_create("adfs_inode_cache",
  209. sizeof(struct adfs_inode_info),
  210. 0, (SLAB_RECLAIM_ACCOUNT|
  211. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  212. init_once);
  213. if (adfs_inode_cachep == NULL)
  214. return -ENOMEM;
  215. return 0;
  216. }
  217. static void destroy_inodecache(void)
  218. {
  219. /*
  220. * Make sure all delayed rcu free inodes are flushed before we
  221. * destroy cache.
  222. */
  223. rcu_barrier();
  224. kmem_cache_destroy(adfs_inode_cachep);
  225. }
  226. static const struct super_operations adfs_sops = {
  227. .alloc_inode = adfs_alloc_inode,
  228. .free_inode = adfs_free_inode,
  229. .drop_inode = adfs_drop_inode,
  230. .write_inode = adfs_write_inode,
  231. .put_super = adfs_put_super,
  232. .statfs = adfs_statfs,
  233. .remount_fs = adfs_remount,
  234. .show_options = adfs_show_options,
  235. };
  236. static int adfs_probe(struct super_block *sb, unsigned int offset, int silent,
  237. int (*validate)(struct super_block *sb,
  238. struct buffer_head *bh,
  239. struct adfs_discrecord **bhp))
  240. {
  241. struct adfs_sb_info *asb = ADFS_SB(sb);
  242. struct adfs_discrecord *dr;
  243. struct buffer_head *bh;
  244. unsigned int blocksize = BLOCK_SIZE;
  245. int ret, try;
  246. for (try = 0; try < 2; try++) {
  247. /* try to set the requested block size */
  248. if (sb->s_blocksize != blocksize &&
  249. !sb_set_blocksize(sb, blocksize)) {
  250. if (!silent)
  251. adfs_msg(sb, KERN_ERR,
  252. "error: unsupported blocksize");
  253. return -EINVAL;
  254. }
  255. /* read the buffer */
  256. bh = sb_bread(sb, offset >> sb->s_blocksize_bits);
  257. if (!bh) {
  258. adfs_msg(sb, KERN_ERR,
  259. "error: unable to read block %u, try %d",
  260. offset >> sb->s_blocksize_bits, try);
  261. return -EIO;
  262. }
  263. /* validate it */
  264. ret = validate(sb, bh, &dr);
  265. if (ret) {
  266. brelse(bh);
  267. return ret;
  268. }
  269. /* does the block size match the filesystem block size? */
  270. blocksize = 1 << dr->log2secsize;
  271. if (sb->s_blocksize == blocksize) {
  272. asb->s_map = adfs_read_map(sb, dr);
  273. brelse(bh);
  274. return PTR_ERR_OR_ZERO(asb->s_map);
  275. }
  276. brelse(bh);
  277. }
  278. return -EIO;
  279. }
  280. static int adfs_validate_bblk(struct super_block *sb, struct buffer_head *bh,
  281. struct adfs_discrecord **drp)
  282. {
  283. struct adfs_discrecord *dr;
  284. unsigned char *b_data;
  285. b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
  286. if (adfs_checkbblk(b_data))
  287. return -EILSEQ;
  288. /* Do some sanity checks on the ADFS disc record */
  289. dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
  290. if (adfs_checkdiscrecord(dr))
  291. return -EILSEQ;
  292. *drp = dr;
  293. return 0;
  294. }
  295. static int adfs_validate_dr0(struct super_block *sb, struct buffer_head *bh,
  296. struct adfs_discrecord **drp)
  297. {
  298. struct adfs_discrecord *dr;
  299. /* Do some sanity checks on the ADFS disc record */
  300. dr = (struct adfs_discrecord *)(bh->b_data + 4);
  301. if (adfs_checkdiscrecord(dr) || dr->nzones_high || dr->nzones != 1)
  302. return -EILSEQ;
  303. *drp = dr;
  304. return 0;
  305. }
  306. static int adfs_fill_super(struct super_block *sb, void *data, int silent)
  307. {
  308. struct adfs_discrecord *dr;
  309. struct object_info root_obj;
  310. struct adfs_sb_info *asb;
  311. struct inode *root;
  312. int ret = -EINVAL;
  313. sb->s_flags |= ADFS_SB_FLAGS;
  314. asb = kzalloc(sizeof(*asb), GFP_KERNEL);
  315. if (!asb)
  316. return -ENOMEM;
  317. sb->s_fs_info = asb;
  318. sb->s_magic = ADFS_SUPER_MAGIC;
  319. sb->s_time_gran = 10000000;
  320. /* set default options */
  321. asb->s_uid = GLOBAL_ROOT_UID;
  322. asb->s_gid = GLOBAL_ROOT_GID;
  323. asb->s_owner_mask = ADFS_DEFAULT_OWNER_MASK;
  324. asb->s_other_mask = ADFS_DEFAULT_OTHER_MASK;
  325. asb->s_ftsuffix = 0;
  326. if (parse_options(sb, asb, data))
  327. goto error;
  328. /* Try to probe the filesystem boot block */
  329. ret = adfs_probe(sb, ADFS_DISCRECORD, 1, adfs_validate_bblk);
  330. if (ret == -EILSEQ)
  331. ret = adfs_probe(sb, 0, silent, adfs_validate_dr0);
  332. if (ret == -EILSEQ) {
  333. if (!silent)
  334. adfs_msg(sb, KERN_ERR,
  335. "error: can't find an ADFS filesystem on dev %s.",
  336. sb->s_id);
  337. ret = -EINVAL;
  338. }
  339. if (ret)
  340. goto error;
  341. /* set up enough so that we can read an inode */
  342. sb->s_op = &adfs_sops;
  343. dr = adfs_map_discrecord(asb->s_map);
  344. root_obj.parent_id = root_obj.indaddr = le32_to_cpu(dr->root);
  345. root_obj.name_len = 0;
  346. /* Set root object date as 01 Jan 1987 00:00:00 */
  347. root_obj.loadaddr = 0xfff0003f;
  348. root_obj.execaddr = 0xec22c000;
  349. root_obj.size = ADFS_NEWDIR_SIZE;
  350. root_obj.attr = ADFS_NDA_DIRECTORY | ADFS_NDA_OWNER_READ |
  351. ADFS_NDA_OWNER_WRITE | ADFS_NDA_PUBLIC_READ;
  352. /*
  353. * If this is a F+ disk with variable length directories,
  354. * get the root_size from the disc record.
  355. */
  356. if (dr->format_version) {
  357. root_obj.size = le32_to_cpu(dr->root_size);
  358. asb->s_dir = &adfs_fplus_dir_ops;
  359. asb->s_namelen = ADFS_FPLUS_NAME_LEN;
  360. } else {
  361. asb->s_dir = &adfs_f_dir_ops;
  362. asb->s_namelen = ADFS_F_NAME_LEN;
  363. }
  364. /*
  365. * ,xyz hex filetype suffix may be added by driver
  366. * to files that have valid RISC OS filetype
  367. */
  368. if (asb->s_ftsuffix)
  369. asb->s_namelen += 4;
  370. sb->s_d_op = &adfs_dentry_operations;
  371. root = adfs_iget(sb, &root_obj);
  372. sb->s_root = d_make_root(root);
  373. if (!sb->s_root) {
  374. adfs_free_map(sb);
  375. adfs_error(sb, "get root inode failed\n");
  376. ret = -EIO;
  377. goto error;
  378. }
  379. return 0;
  380. error:
  381. sb->s_fs_info = NULL;
  382. kfree(asb);
  383. return ret;
  384. }
  385. static struct dentry *adfs_mount(struct file_system_type *fs_type,
  386. int flags, const char *dev_name, void *data)
  387. {
  388. return mount_bdev(fs_type, flags, dev_name, data, adfs_fill_super);
  389. }
  390. static struct file_system_type adfs_fs_type = {
  391. .owner = THIS_MODULE,
  392. .name = "adfs",
  393. .mount = adfs_mount,
  394. .kill_sb = kill_block_super,
  395. .fs_flags = FS_REQUIRES_DEV,
  396. };
  397. MODULE_ALIAS_FS("adfs");
  398. static int __init init_adfs_fs(void)
  399. {
  400. int err = init_inodecache();
  401. if (err)
  402. goto out1;
  403. err = register_filesystem(&adfs_fs_type);
  404. if (err)
  405. goto out;
  406. return 0;
  407. out:
  408. destroy_inodecache();
  409. out1:
  410. return err;
  411. }
  412. static void __exit exit_adfs_fs(void)
  413. {
  414. unregister_filesystem(&adfs_fs_type);
  415. destroy_inodecache();
  416. }
  417. module_init(init_adfs_fs)
  418. module_exit(exit_adfs_fs)
  419. MODULE_LICENSE("GPL");