super.c 8.5 KB

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