vxfs_super.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/fs_context.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. u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
  69. bufp->f_type = VXFS_SUPER_MAGIC;
  70. bufp->f_bsize = dentry->d_sb->s_blocksize;
  71. bufp->f_blocks = fs32_to_cpu(infp, raw_sb->vs_dsize);
  72. bufp->f_bfree = fs32_to_cpu(infp, raw_sb->vs_free);
  73. bufp->f_bavail = 0;
  74. bufp->f_files = 0;
  75. bufp->f_ffree = fs32_to_cpu(infp, raw_sb->vs_ifree);
  76. bufp->f_fsid = u64_to_fsid(id);
  77. bufp->f_namelen = VXFS_NAMELEN;
  78. return 0;
  79. }
  80. static int vxfs_reconfigure(struct fs_context *fc)
  81. {
  82. sync_filesystem(fc->root->d_sb);
  83. fc->sb_flags |= SB_RDONLY;
  84. return 0;
  85. }
  86. static struct inode *vxfs_alloc_inode(struct super_block *sb)
  87. {
  88. struct vxfs_inode_info *vi;
  89. vi = alloc_inode_sb(sb, vxfs_inode_cachep, GFP_KERNEL);
  90. if (!vi)
  91. return NULL;
  92. inode_init_once(&vi->vfs_inode);
  93. return &vi->vfs_inode;
  94. }
  95. static void vxfs_free_inode(struct inode *inode)
  96. {
  97. kmem_cache_free(vxfs_inode_cachep, VXFS_INO(inode));
  98. }
  99. static const struct super_operations vxfs_super_ops = {
  100. .alloc_inode = vxfs_alloc_inode,
  101. .free_inode = vxfs_free_inode,
  102. .evict_inode = vxfs_evict_inode,
  103. .put_super = vxfs_put_super,
  104. .statfs = vxfs_statfs,
  105. };
  106. static int vxfs_try_sb_magic(struct super_block *sbp, struct fs_context *fc,
  107. unsigned blk, __fs32 magic)
  108. {
  109. struct buffer_head *bp;
  110. struct vxfs_sb *rsbp;
  111. struct vxfs_sb_info *infp = VXFS_SBI(sbp);
  112. int silent = fc->sb_flags & SB_SILENT;
  113. int rc = -ENOMEM;
  114. bp = sb_bread(sbp, blk);
  115. do {
  116. if (!bp || !buffer_mapped(bp)) {
  117. if (!silent) {
  118. warnf(fc,
  119. "vxfs: unable to read disk superblock at %u",
  120. blk);
  121. }
  122. break;
  123. }
  124. rc = -EINVAL;
  125. rsbp = (struct vxfs_sb *)bp->b_data;
  126. if (rsbp->vs_magic != magic) {
  127. if (!silent)
  128. infof(fc,
  129. "vxfs: WRONG superblock magic %08x at %u",
  130. rsbp->vs_magic, blk);
  131. break;
  132. }
  133. rc = 0;
  134. infp->vsi_raw = rsbp;
  135. infp->vsi_bp = bp;
  136. } while (0);
  137. if (rc) {
  138. infp->vsi_raw = NULL;
  139. infp->vsi_bp = NULL;
  140. brelse(bp);
  141. }
  142. return rc;
  143. }
  144. /**
  145. * vxfs_fill_super - read superblock into memory and initialize filesystem
  146. * @sbp: VFS superblock (to fill)
  147. * @fc: filesytem context
  148. *
  149. * Description:
  150. * We are called on the first mount of a filesystem to read the
  151. * superblock into memory and do some basic setup.
  152. *
  153. * Returns:
  154. * The superblock on success, else %NULL.
  155. *
  156. * Locking:
  157. * We are under @sbp->s_lock.
  158. */
  159. static int vxfs_fill_super(struct super_block *sbp, struct fs_context *fc)
  160. {
  161. struct vxfs_sb_info *infp;
  162. struct vxfs_sb *rsbp;
  163. u_long bsize;
  164. struct inode *root;
  165. int ret = -EINVAL;
  166. int silent = fc->sb_flags & SB_SILENT;
  167. u32 j;
  168. sbp->s_flags |= SB_RDONLY;
  169. infp = kzalloc_obj(*infp);
  170. if (!infp) {
  171. warnf(fc, "vxfs: unable to allocate incore superblock");
  172. return -ENOMEM;
  173. }
  174. bsize = sb_min_blocksize(sbp, BLOCK_SIZE);
  175. if (!bsize) {
  176. warnf(fc, "vxfs: unable to set blocksize");
  177. goto out;
  178. }
  179. sbp->s_op = &vxfs_super_ops;
  180. sbp->s_fs_info = infp;
  181. sbp->s_time_min = 0;
  182. sbp->s_time_max = U32_MAX;
  183. if (!vxfs_try_sb_magic(sbp, fc, 1,
  184. (__force __fs32)cpu_to_le32(VXFS_SUPER_MAGIC))) {
  185. /* Unixware, x86 */
  186. infp->byte_order = VXFS_BO_LE;
  187. } else if (!vxfs_try_sb_magic(sbp, fc, 8,
  188. (__force __fs32)cpu_to_be32(VXFS_SUPER_MAGIC))) {
  189. /* HP-UX, parisc */
  190. infp->byte_order = VXFS_BO_BE;
  191. } else {
  192. if (!silent)
  193. infof(fc, "vxfs: can't find superblock.");
  194. goto out;
  195. }
  196. rsbp = infp->vsi_raw;
  197. j = fs32_to_cpu(infp, rsbp->vs_version);
  198. if ((j < 2 || j > 4) && !silent) {
  199. infof(fc, "vxfs: unsupported VxFS version (%d)", j);
  200. goto out;
  201. }
  202. #ifdef DIAGNOSTIC
  203. printk(KERN_DEBUG "vxfs: supported VxFS version (%d)\n", j);
  204. printk(KERN_DEBUG "vxfs: blocksize: %d\n",
  205. fs32_to_cpu(infp, rsbp->vs_bsize));
  206. #endif
  207. sbp->s_magic = fs32_to_cpu(infp, rsbp->vs_magic);
  208. infp->vsi_oltext = fs32_to_cpu(infp, rsbp->vs_oltext[0]);
  209. infp->vsi_oltsize = fs32_to_cpu(infp, rsbp->vs_oltsize);
  210. j = fs32_to_cpu(infp, rsbp->vs_bsize);
  211. if (!sb_set_blocksize(sbp, j)) {
  212. warnf(fc, "vxfs: unable to set final block size");
  213. goto out;
  214. }
  215. if (vxfs_read_olt(sbp, bsize)) {
  216. warnf(fc, "vxfs: unable to read olt");
  217. goto out;
  218. }
  219. if (vxfs_read_fshead(sbp)) {
  220. warnf(fc, "vxfs: unable to read fshead");
  221. goto out;
  222. }
  223. root = vxfs_iget(sbp, VXFS_ROOT_INO);
  224. if (IS_ERR(root)) {
  225. ret = PTR_ERR(root);
  226. goto out;
  227. }
  228. sbp->s_root = d_make_root(root);
  229. if (!sbp->s_root) {
  230. warnf(fc, "vxfs: unable to get root dentry.");
  231. goto out_free_ilist;
  232. }
  233. return 0;
  234. out_free_ilist:
  235. iput(infp->vsi_fship);
  236. iput(infp->vsi_ilist);
  237. iput(infp->vsi_stilist);
  238. out:
  239. brelse(infp->vsi_bp);
  240. kfree(infp);
  241. return ret;
  242. }
  243. /*
  244. * The usual module blurb.
  245. */
  246. static int vxfs_get_tree(struct fs_context *fc)
  247. {
  248. return get_tree_bdev(fc, vxfs_fill_super);
  249. }
  250. static const struct fs_context_operations vxfs_context_ops = {
  251. .get_tree = vxfs_get_tree,
  252. .reconfigure = vxfs_reconfigure,
  253. };
  254. static int vxfs_init_fs_context(struct fs_context *fc)
  255. {
  256. fc->ops = &vxfs_context_ops;
  257. return 0;
  258. }
  259. static struct file_system_type vxfs_fs_type = {
  260. .owner = THIS_MODULE,
  261. .name = "vxfs",
  262. .kill_sb = kill_block_super,
  263. .fs_flags = FS_REQUIRES_DEV,
  264. .init_fs_context = vxfs_init_fs_context,
  265. };
  266. MODULE_ALIAS_FS("vxfs"); /* makes mount -t vxfs autoload the module */
  267. MODULE_ALIAS("vxfs");
  268. static int __init
  269. vxfs_init(void)
  270. {
  271. int rv;
  272. vxfs_inode_cachep = kmem_cache_create_usercopy("vxfs_inode",
  273. sizeof(struct vxfs_inode_info), 0,
  274. SLAB_RECLAIM_ACCOUNT,
  275. offsetof(struct vxfs_inode_info, vii_immed.vi_immed),
  276. sizeof_field(struct vxfs_inode_info,
  277. vii_immed.vi_immed),
  278. NULL);
  279. if (!vxfs_inode_cachep)
  280. return -ENOMEM;
  281. rv = register_filesystem(&vxfs_fs_type);
  282. if (rv < 0)
  283. kmem_cache_destroy(vxfs_inode_cachep);
  284. return rv;
  285. }
  286. static void __exit
  287. vxfs_cleanup(void)
  288. {
  289. unregister_filesystem(&vxfs_fs_type);
  290. /*
  291. * Make sure all delayed rcu free inodes are flushed before we
  292. * destroy cache.
  293. */
  294. rcu_barrier();
  295. kmem_cache_destroy(vxfs_inode_cachep);
  296. }
  297. module_init(vxfs_init);
  298. module_exit(vxfs_cleanup);