super.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * super.c
  4. *
  5. * Copyright (c) 1999 Al Smith
  6. *
  7. * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/exportfs.h>
  12. #include <linux/slab.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/vfs.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/fs_context.h>
  17. #include "efs.h"
  18. #include <linux/efs_vh.h>
  19. #include <linux/efs_fs_sb.h>
  20. static int efs_statfs(struct dentry *dentry, struct kstatfs *buf);
  21. static int efs_init_fs_context(struct fs_context *fc);
  22. static void efs_kill_sb(struct super_block *s)
  23. {
  24. struct efs_sb_info *sbi = SUPER_INFO(s);
  25. kill_block_super(s);
  26. kfree(sbi);
  27. }
  28. static struct pt_types sgi_pt_types[] = {
  29. {0x00, "SGI vh"},
  30. {0x01, "SGI trkrepl"},
  31. {0x02, "SGI secrepl"},
  32. {0x03, "SGI raw"},
  33. {0x04, "SGI bsd"},
  34. {SGI_SYSV, "SGI sysv"},
  35. {0x06, "SGI vol"},
  36. {SGI_EFS, "SGI efs"},
  37. {0x08, "SGI lv"},
  38. {0x09, "SGI rlv"},
  39. {0x0A, "SGI xfs"},
  40. {0x0B, "SGI xfslog"},
  41. {0x0C, "SGI xlv"},
  42. {0x82, "Linux swap"},
  43. {0x83, "Linux native"},
  44. {0, NULL}
  45. };
  46. /*
  47. * File system definition and registration.
  48. */
  49. static struct file_system_type efs_fs_type = {
  50. .owner = THIS_MODULE,
  51. .name = "efs",
  52. .kill_sb = efs_kill_sb,
  53. .fs_flags = FS_REQUIRES_DEV,
  54. .init_fs_context = efs_init_fs_context,
  55. };
  56. MODULE_ALIAS_FS("efs");
  57. static struct kmem_cache * efs_inode_cachep;
  58. static struct inode *efs_alloc_inode(struct super_block *sb)
  59. {
  60. struct efs_inode_info *ei;
  61. ei = alloc_inode_sb(sb, efs_inode_cachep, GFP_KERNEL);
  62. if (!ei)
  63. return NULL;
  64. return &ei->vfs_inode;
  65. }
  66. static void efs_free_inode(struct inode *inode)
  67. {
  68. kmem_cache_free(efs_inode_cachep, INODE_INFO(inode));
  69. }
  70. static void init_once(void *foo)
  71. {
  72. struct efs_inode_info *ei = (struct efs_inode_info *) foo;
  73. inode_init_once(&ei->vfs_inode);
  74. }
  75. static int __init init_inodecache(void)
  76. {
  77. efs_inode_cachep = kmem_cache_create("efs_inode_cache",
  78. sizeof(struct efs_inode_info), 0,
  79. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
  80. init_once);
  81. if (efs_inode_cachep == NULL)
  82. return -ENOMEM;
  83. return 0;
  84. }
  85. static void destroy_inodecache(void)
  86. {
  87. /*
  88. * Make sure all delayed rcu free inodes are flushed before we
  89. * destroy cache.
  90. */
  91. rcu_barrier();
  92. kmem_cache_destroy(efs_inode_cachep);
  93. }
  94. static const struct super_operations efs_superblock_operations = {
  95. .alloc_inode = efs_alloc_inode,
  96. .free_inode = efs_free_inode,
  97. .statfs = efs_statfs,
  98. };
  99. static const struct export_operations efs_export_ops = {
  100. .encode_fh = generic_encode_ino32_fh,
  101. .fh_to_dentry = efs_fh_to_dentry,
  102. .fh_to_parent = efs_fh_to_parent,
  103. .get_parent = efs_get_parent,
  104. };
  105. static int __init init_efs_fs(void) {
  106. int err;
  107. pr_info(EFS_VERSION" - http://aeschi.ch.eu.org/efs/\n");
  108. err = init_inodecache();
  109. if (err)
  110. goto out1;
  111. err = register_filesystem(&efs_fs_type);
  112. if (err)
  113. goto out;
  114. return 0;
  115. out:
  116. destroy_inodecache();
  117. out1:
  118. return err;
  119. }
  120. static void __exit exit_efs_fs(void) {
  121. unregister_filesystem(&efs_fs_type);
  122. destroy_inodecache();
  123. }
  124. module_init(init_efs_fs)
  125. module_exit(exit_efs_fs)
  126. static efs_block_t efs_validate_vh(struct volume_header *vh) {
  127. int i;
  128. __be32 cs, *ui;
  129. int csum;
  130. efs_block_t sblock = 0; /* shuts up gcc */
  131. struct pt_types *pt_entry;
  132. int pt_type, slice = -1;
  133. if (be32_to_cpu(vh->vh_magic) != VHMAGIC) {
  134. /*
  135. * assume that we're dealing with a partition and allow
  136. * read_super() to try and detect a valid superblock
  137. * on the next block.
  138. */
  139. return 0;
  140. }
  141. ui = ((__be32 *) (vh + 1)) - 1;
  142. for(csum = 0; ui >= ((__be32 *) vh);) {
  143. cs = *ui--;
  144. csum += be32_to_cpu(cs);
  145. }
  146. if (csum) {
  147. pr_warn("SGI disklabel: checksum bad, label corrupted\n");
  148. return 0;
  149. }
  150. #ifdef DEBUG
  151. pr_debug("bf: \"%16s\"\n", vh->vh_bootfile);
  152. for(i = 0; i < NVDIR; i++) {
  153. int j;
  154. char name[VDNAMESIZE+1];
  155. for(j = 0; j < VDNAMESIZE; j++) {
  156. name[j] = vh->vh_vd[i].vd_name[j];
  157. }
  158. name[j] = (char) 0;
  159. if (name[0]) {
  160. pr_debug("vh: %8s block: 0x%08x size: 0x%08x\n",
  161. name, (int) be32_to_cpu(vh->vh_vd[i].vd_lbn),
  162. (int) be32_to_cpu(vh->vh_vd[i].vd_nbytes));
  163. }
  164. }
  165. #endif
  166. for(i = 0; i < NPARTAB; i++) {
  167. pt_type = (int) be32_to_cpu(vh->vh_pt[i].pt_type);
  168. for(pt_entry = sgi_pt_types; pt_entry->pt_name; pt_entry++) {
  169. if (pt_type == pt_entry->pt_type) break;
  170. }
  171. #ifdef DEBUG
  172. if (be32_to_cpu(vh->vh_pt[i].pt_nblks)) {
  173. pr_debug("pt %2d: start: %08d size: %08d type: 0x%02x (%s)\n",
  174. i, (int)be32_to_cpu(vh->vh_pt[i].pt_firstlbn),
  175. (int)be32_to_cpu(vh->vh_pt[i].pt_nblks),
  176. pt_type, (pt_entry->pt_name) ?
  177. pt_entry->pt_name : "unknown");
  178. }
  179. #endif
  180. if (IS_EFS(pt_type)) {
  181. sblock = be32_to_cpu(vh->vh_pt[i].pt_firstlbn);
  182. slice = i;
  183. }
  184. }
  185. if (slice == -1) {
  186. pr_notice("partition table contained no EFS partitions\n");
  187. #ifdef DEBUG
  188. } else {
  189. pr_info("using slice %d (type %s, offset 0x%x)\n", slice,
  190. (pt_entry->pt_name) ? pt_entry->pt_name : "unknown",
  191. sblock);
  192. #endif
  193. }
  194. return sblock;
  195. }
  196. static int efs_validate_super(struct efs_sb_info *sb, struct efs_super *super) {
  197. if (!IS_EFS_MAGIC(be32_to_cpu(super->fs_magic)))
  198. return -1;
  199. sb->fs_magic = be32_to_cpu(super->fs_magic);
  200. sb->total_blocks = be32_to_cpu(super->fs_size);
  201. sb->first_block = be32_to_cpu(super->fs_firstcg);
  202. sb->group_size = be32_to_cpu(super->fs_cgfsize);
  203. sb->data_free = be32_to_cpu(super->fs_tfree);
  204. sb->inode_free = be32_to_cpu(super->fs_tinode);
  205. sb->inode_blocks = be16_to_cpu(super->fs_cgisize);
  206. sb->total_groups = be16_to_cpu(super->fs_ncg);
  207. return 0;
  208. }
  209. static int efs_fill_super(struct super_block *s, struct fs_context *fc)
  210. {
  211. struct efs_sb_info *sb;
  212. struct buffer_head *bh;
  213. struct inode *root;
  214. sb = kzalloc(sizeof(struct efs_sb_info), GFP_KERNEL);
  215. if (!sb)
  216. return -ENOMEM;
  217. s->s_fs_info = sb;
  218. s->s_time_min = 0;
  219. s->s_time_max = U32_MAX;
  220. s->s_magic = EFS_SUPER_MAGIC;
  221. if (!sb_set_blocksize(s, EFS_BLOCKSIZE)) {
  222. pr_err("device does not support %d byte blocks\n",
  223. EFS_BLOCKSIZE);
  224. return invalf(fc, "device does not support %d byte blocks\n",
  225. EFS_BLOCKSIZE);
  226. }
  227. /* read the vh (volume header) block */
  228. bh = sb_bread(s, 0);
  229. if (!bh) {
  230. pr_err("cannot read volume header\n");
  231. return -EIO;
  232. }
  233. /*
  234. * if this returns zero then we didn't find any partition table.
  235. * this isn't (yet) an error - just assume for the moment that
  236. * the device is valid and go on to search for a superblock.
  237. */
  238. sb->fs_start = efs_validate_vh((struct volume_header *) bh->b_data);
  239. brelse(bh);
  240. if (sb->fs_start == -1) {
  241. return -EINVAL;
  242. }
  243. bh = sb_bread(s, sb->fs_start + EFS_SUPER);
  244. if (!bh) {
  245. pr_err("cannot read superblock\n");
  246. return -EIO;
  247. }
  248. if (efs_validate_super(sb, (struct efs_super *) bh->b_data)) {
  249. #ifdef DEBUG
  250. pr_warn("invalid superblock at block %u\n",
  251. sb->fs_start + EFS_SUPER);
  252. #endif
  253. brelse(bh);
  254. return -EINVAL;
  255. }
  256. brelse(bh);
  257. if (!sb_rdonly(s)) {
  258. #ifdef DEBUG
  259. pr_info("forcing read-only mode\n");
  260. #endif
  261. s->s_flags |= SB_RDONLY;
  262. }
  263. s->s_op = &efs_superblock_operations;
  264. s->s_export_op = &efs_export_ops;
  265. root = efs_iget(s, EFS_ROOTINODE);
  266. if (IS_ERR(root)) {
  267. pr_err("get root inode failed\n");
  268. return PTR_ERR(root);
  269. }
  270. s->s_root = d_make_root(root);
  271. if (!(s->s_root)) {
  272. pr_err("get root dentry failed\n");
  273. return -ENOMEM;
  274. }
  275. return 0;
  276. }
  277. static int efs_get_tree(struct fs_context *fc)
  278. {
  279. return get_tree_bdev(fc, efs_fill_super);
  280. }
  281. static int efs_reconfigure(struct fs_context *fc)
  282. {
  283. sync_filesystem(fc->root->d_sb);
  284. fc->sb_flags |= SB_RDONLY;
  285. return 0;
  286. }
  287. static const struct fs_context_operations efs_context_opts = {
  288. .get_tree = efs_get_tree,
  289. .reconfigure = efs_reconfigure,
  290. };
  291. /*
  292. * Set up the filesystem mount context.
  293. */
  294. static int efs_init_fs_context(struct fs_context *fc)
  295. {
  296. fc->ops = &efs_context_opts;
  297. return 0;
  298. }
  299. static int efs_statfs(struct dentry *dentry, struct kstatfs *buf) {
  300. struct super_block *sb = dentry->d_sb;
  301. struct efs_sb_info *sbi = SUPER_INFO(sb);
  302. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  303. buf->f_type = EFS_SUPER_MAGIC; /* efs magic number */
  304. buf->f_bsize = EFS_BLOCKSIZE; /* blocksize */
  305. buf->f_blocks = sbi->total_groups * /* total data blocks */
  306. (sbi->group_size - sbi->inode_blocks);
  307. buf->f_bfree = sbi->data_free; /* free data blocks */
  308. buf->f_bavail = sbi->data_free; /* free blocks for non-root */
  309. buf->f_files = sbi->total_groups * /* total inodes */
  310. sbi->inode_blocks *
  311. (EFS_BLOCKSIZE / sizeof(struct efs_dinode));
  312. buf->f_ffree = sbi->inode_free; /* free inodes */
  313. buf->f_fsid = u64_to_fsid(id);
  314. buf->f_namelen = EFS_MAXNAMELEN; /* max filename length */
  315. return 0;
  316. }