super.c 8.2 KB

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