vxfs_super.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2000-2001 Christoph Hellwig.
  4. * Copyright (c) 2016 Krzysztof Blaszkowski
  5. */
  6. /*
  7. * Veritas filesystem driver - superblock related routines.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/fs.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/stat.h>
  17. #include <linux/vfs.h>
  18. #include <linux/mount.h>
  19. #include "vxfs.h"
  20. #include "vxfs_extern.h"
  21. #include "vxfs_dir.h"
  22. #include "vxfs_inode.h"
  23. MODULE_AUTHOR("Christoph Hellwig, Krzysztof Blaszkowski");
  24. MODULE_DESCRIPTION("Veritas Filesystem (VxFS) driver");
  25. MODULE_LICENSE("Dual BSD/GPL");
  26. static struct kmem_cache *vxfs_inode_cachep;
  27. /**
  28. * vxfs_put_super - free superblock resources
  29. * @sbp: VFS superblock.
  30. *
  31. * Description:
  32. * vxfs_put_super frees all resources allocated for @sbp
  33. * after the last instance of the filesystem is unmounted.
  34. */
  35. static void
  36. vxfs_put_super(struct super_block *sbp)
  37. {
  38. struct vxfs_sb_info *infp = VXFS_SBI(sbp);
  39. iput(infp->vsi_fship);
  40. iput(infp->vsi_ilist);
  41. iput(infp->vsi_stilist);
  42. brelse(infp->vsi_bp);
  43. kfree(infp);
  44. }
  45. /**
  46. * vxfs_statfs - get filesystem information
  47. * @dentry: VFS dentry to locate superblock
  48. * @bufp: output buffer
  49. *
  50. * Description:
  51. * vxfs_statfs fills the statfs buffer @bufp with information
  52. * about the filesystem described by @dentry.
  53. *
  54. * Returns:
  55. * Zero.
  56. *
  57. * Locking:
  58. * No locks held.
  59. *
  60. * Notes:
  61. * This is everything but complete...
  62. */
  63. static int
  64. vxfs_statfs(struct dentry *dentry, struct kstatfs *bufp)
  65. {
  66. struct vxfs_sb_info *infp = VXFS_SBI(dentry->d_sb);
  67. struct vxfs_sb *raw_sb = infp->vsi_raw;
  68. bufp->f_type = VXFS_SUPER_MAGIC;
  69. bufp->f_bsize = dentry->d_sb->s_blocksize;
  70. bufp->f_blocks = fs32_to_cpu(infp, raw_sb->vs_dsize);
  71. bufp->f_bfree = fs32_to_cpu(infp, raw_sb->vs_free);
  72. bufp->f_bavail = 0;
  73. bufp->f_files = 0;
  74. bufp->f_ffree = fs32_to_cpu(infp, raw_sb->vs_ifree);
  75. bufp->f_namelen = VXFS_NAMELEN;
  76. return 0;
  77. }
  78. static int vxfs_remount(struct super_block *sb, int *flags, char *data)
  79. {
  80. sync_filesystem(sb);
  81. *flags |= SB_RDONLY;
  82. return 0;
  83. }
  84. static struct inode *vxfs_alloc_inode(struct super_block *sb)
  85. {
  86. struct vxfs_inode_info *vi;
  87. vi = alloc_inode_sb(sb, vxfs_inode_cachep, GFP_KERNEL);
  88. if (!vi)
  89. return NULL;
  90. inode_init_once(&vi->vfs_inode);
  91. return &vi->vfs_inode;
  92. }
  93. static void vxfs_free_inode(struct inode *inode)
  94. {
  95. kmem_cache_free(vxfs_inode_cachep, VXFS_INO(inode));
  96. }
  97. static const struct super_operations vxfs_super_ops = {
  98. .alloc_inode = vxfs_alloc_inode,
  99. .free_inode = vxfs_free_inode,
  100. .evict_inode = vxfs_evict_inode,
  101. .put_super = vxfs_put_super,
  102. .statfs = vxfs_statfs,
  103. .remount_fs = vxfs_remount,
  104. };
  105. static int vxfs_try_sb_magic(struct super_block *sbp, int silent,
  106. unsigned blk, __fs32 magic)
  107. {
  108. struct buffer_head *bp;
  109. struct vxfs_sb *rsbp;
  110. struct vxfs_sb_info *infp = VXFS_SBI(sbp);
  111. int rc = -ENOMEM;
  112. bp = sb_bread(sbp, blk);
  113. do {
  114. if (!bp || !buffer_mapped(bp)) {
  115. if (!silent) {
  116. printk(KERN_WARNING
  117. "vxfs: unable to read disk superblock at %u\n",
  118. blk);
  119. }
  120. break;
  121. }
  122. rc = -EINVAL;
  123. rsbp = (struct vxfs_sb *)bp->b_data;
  124. if (rsbp->vs_magic != magic) {
  125. if (!silent)
  126. printk(KERN_NOTICE
  127. "vxfs: WRONG superblock magic %08x at %u\n",
  128. rsbp->vs_magic, blk);
  129. break;
  130. }
  131. rc = 0;
  132. infp->vsi_raw = rsbp;
  133. infp->vsi_bp = bp;
  134. } while (0);
  135. if (rc) {
  136. infp->vsi_raw = NULL;
  137. infp->vsi_bp = NULL;
  138. brelse(bp);
  139. }
  140. return rc;
  141. }
  142. /**
  143. * vxfs_read_super - read superblock into memory and initialize filesystem
  144. * @sbp: VFS superblock (to fill)
  145. * @dp: fs private mount data
  146. * @silent: do not complain loudly when sth is wrong
  147. *
  148. * Description:
  149. * We are called on the first mount of a filesystem to read the
  150. * superblock into memory and do some basic setup.
  151. *
  152. * Returns:
  153. * The superblock on success, else %NULL.
  154. *
  155. * Locking:
  156. * We are under @sbp->s_lock.
  157. */
  158. static int vxfs_fill_super(struct super_block *sbp, void *dp, int silent)
  159. {
  160. struct vxfs_sb_info *infp;
  161. struct vxfs_sb *rsbp;
  162. u_long bsize;
  163. struct inode *root;
  164. int ret = -EINVAL;
  165. u32 j;
  166. sbp->s_flags |= SB_RDONLY;
  167. infp = kzalloc(sizeof(*infp), GFP_KERNEL);
  168. if (!infp) {
  169. printk(KERN_WARNING "vxfs: unable to allocate incore superblock\n");
  170. return -ENOMEM;
  171. }
  172. bsize = sb_min_blocksize(sbp, BLOCK_SIZE);
  173. if (!bsize) {
  174. printk(KERN_WARNING "vxfs: unable to set blocksize\n");
  175. goto out;
  176. }
  177. sbp->s_op = &vxfs_super_ops;
  178. sbp->s_fs_info = infp;
  179. sbp->s_time_min = 0;
  180. sbp->s_time_max = U32_MAX;
  181. if (!vxfs_try_sb_magic(sbp, silent, 1,
  182. (__force __fs32)cpu_to_le32(VXFS_SUPER_MAGIC))) {
  183. /* Unixware, x86 */
  184. infp->byte_order = VXFS_BO_LE;
  185. } else if (!vxfs_try_sb_magic(sbp, silent, 8,
  186. (__force __fs32)cpu_to_be32(VXFS_SUPER_MAGIC))) {
  187. /* HP-UX, parisc */
  188. infp->byte_order = VXFS_BO_BE;
  189. } else {
  190. if (!silent)
  191. printk(KERN_NOTICE "vxfs: can't find superblock.\n");
  192. goto out;
  193. }
  194. rsbp = infp->vsi_raw;
  195. j = fs32_to_cpu(infp, rsbp->vs_version);
  196. if ((j < 2 || j > 4) && !silent) {
  197. printk(KERN_NOTICE "vxfs: unsupported VxFS version (%d)\n", j);
  198. goto out;
  199. }
  200. #ifdef DIAGNOSTIC
  201. printk(KERN_DEBUG "vxfs: supported VxFS version (%d)\n", j);
  202. printk(KERN_DEBUG "vxfs: blocksize: %d\n",
  203. fs32_to_cpu(infp, rsbp->vs_bsize));
  204. #endif
  205. sbp->s_magic = fs32_to_cpu(infp, rsbp->vs_magic);
  206. infp->vsi_oltext = fs32_to_cpu(infp, rsbp->vs_oltext[0]);
  207. infp->vsi_oltsize = fs32_to_cpu(infp, rsbp->vs_oltsize);
  208. j = fs32_to_cpu(infp, rsbp->vs_bsize);
  209. if (!sb_set_blocksize(sbp, j)) {
  210. printk(KERN_WARNING "vxfs: unable to set final block size\n");
  211. goto out;
  212. }
  213. if (vxfs_read_olt(sbp, bsize)) {
  214. printk(KERN_WARNING "vxfs: unable to read olt\n");
  215. goto out;
  216. }
  217. if (vxfs_read_fshead(sbp)) {
  218. printk(KERN_WARNING "vxfs: unable to read fshead\n");
  219. goto out;
  220. }
  221. root = vxfs_iget(sbp, VXFS_ROOT_INO);
  222. if (IS_ERR(root)) {
  223. ret = PTR_ERR(root);
  224. goto out;
  225. }
  226. sbp->s_root = d_make_root(root);
  227. if (!sbp->s_root) {
  228. printk(KERN_WARNING "vxfs: unable to get root dentry.\n");
  229. goto out_free_ilist;
  230. }
  231. return 0;
  232. out_free_ilist:
  233. iput(infp->vsi_fship);
  234. iput(infp->vsi_ilist);
  235. iput(infp->vsi_stilist);
  236. out:
  237. brelse(infp->vsi_bp);
  238. kfree(infp);
  239. return ret;
  240. }
  241. /*
  242. * The usual module blurb.
  243. */
  244. static struct dentry *vxfs_mount(struct file_system_type *fs_type,
  245. int flags, const char *dev_name, void *data)
  246. {
  247. return mount_bdev(fs_type, flags, dev_name, data, vxfs_fill_super);
  248. }
  249. static struct file_system_type vxfs_fs_type = {
  250. .owner = THIS_MODULE,
  251. .name = "vxfs",
  252. .mount = vxfs_mount,
  253. .kill_sb = kill_block_super,
  254. .fs_flags = FS_REQUIRES_DEV,
  255. };
  256. MODULE_ALIAS_FS("vxfs"); /* makes mount -t vxfs autoload the module */
  257. MODULE_ALIAS("vxfs");
  258. static int __init
  259. vxfs_init(void)
  260. {
  261. int rv;
  262. vxfs_inode_cachep = kmem_cache_create_usercopy("vxfs_inode",
  263. sizeof(struct vxfs_inode_info), 0,
  264. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
  265. offsetof(struct vxfs_inode_info, vii_immed.vi_immed),
  266. sizeof_field(struct vxfs_inode_info,
  267. vii_immed.vi_immed),
  268. NULL);
  269. if (!vxfs_inode_cachep)
  270. return -ENOMEM;
  271. rv = register_filesystem(&vxfs_fs_type);
  272. if (rv < 0)
  273. kmem_cache_destroy(vxfs_inode_cachep);
  274. return rv;
  275. }
  276. static void __exit
  277. vxfs_cleanup(void)
  278. {
  279. unregister_filesystem(&vxfs_fs_type);
  280. /*
  281. * Make sure all delayed rcu free inodes are flushed before we
  282. * destroy cache.
  283. */
  284. rcu_barrier();
  285. kmem_cache_destroy(vxfs_inode_cachep);
  286. }
  287. module_init(vxfs_init);
  288. module_exit(vxfs_cleanup);