super.c 8.1 KB

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