super.c 8.2 KB

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