super.c 8.2 KB

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