vxfs_super.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2000-2001 Christoph Hellwig.
  3. * Copyright (c) 2016 Krzysztof Blaszkowski
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * Alternatively, this software may be distributed under the terms of the
  16. * GNU General Public License ("GPL").
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /*
  31. * Veritas filesystem driver - superblock related routines.
  32. */
  33. #include <linux/init.h>
  34. #include <linux/module.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/fs.h>
  37. #include <linux/buffer_head.h>
  38. #include <linux/kernel.h>
  39. #include <linux/slab.h>
  40. #include <linux/stat.h>
  41. #include <linux/vfs.h>
  42. #include <linux/mount.h>
  43. #include "vxfs.h"
  44. #include "vxfs_extern.h"
  45. #include "vxfs_dir.h"
  46. #include "vxfs_inode.h"
  47. MODULE_AUTHOR("Christoph Hellwig, Krzysztof Blaszkowski");
  48. MODULE_DESCRIPTION("Veritas Filesystem (VxFS) driver");
  49. MODULE_LICENSE("Dual BSD/GPL");
  50. static struct kmem_cache *vxfs_inode_cachep;
  51. /**
  52. * vxfs_put_super - free superblock resources
  53. * @sbp: VFS superblock.
  54. *
  55. * Description:
  56. * vxfs_put_super frees all resources allocated for @sbp
  57. * after the last instance of the filesystem is unmounted.
  58. */
  59. static void
  60. vxfs_put_super(struct super_block *sbp)
  61. {
  62. struct vxfs_sb_info *infp = VXFS_SBI(sbp);
  63. iput(infp->vsi_fship);
  64. iput(infp->vsi_ilist);
  65. iput(infp->vsi_stilist);
  66. brelse(infp->vsi_bp);
  67. kfree(infp);
  68. }
  69. /**
  70. * vxfs_statfs - get filesystem information
  71. * @dentry: VFS dentry to locate superblock
  72. * @bufp: output buffer
  73. *
  74. * Description:
  75. * vxfs_statfs fills the statfs buffer @bufp with information
  76. * about the filesystem described by @dentry.
  77. *
  78. * Returns:
  79. * Zero.
  80. *
  81. * Locking:
  82. * No locks held.
  83. *
  84. * Notes:
  85. * This is everything but complete...
  86. */
  87. static int
  88. vxfs_statfs(struct dentry *dentry, struct kstatfs *bufp)
  89. {
  90. struct vxfs_sb_info *infp = VXFS_SBI(dentry->d_sb);
  91. struct vxfs_sb *raw_sb = infp->vsi_raw;
  92. bufp->f_type = VXFS_SUPER_MAGIC;
  93. bufp->f_bsize = dentry->d_sb->s_blocksize;
  94. bufp->f_blocks = fs32_to_cpu(infp, raw_sb->vs_dsize);
  95. bufp->f_bfree = fs32_to_cpu(infp, raw_sb->vs_free);
  96. bufp->f_bavail = 0;
  97. bufp->f_files = 0;
  98. bufp->f_ffree = fs32_to_cpu(infp, raw_sb->vs_ifree);
  99. bufp->f_namelen = VXFS_NAMELEN;
  100. return 0;
  101. }
  102. static int vxfs_remount(struct super_block *sb, int *flags, char *data)
  103. {
  104. sync_filesystem(sb);
  105. *flags |= MS_RDONLY;
  106. return 0;
  107. }
  108. static struct inode *vxfs_alloc_inode(struct super_block *sb)
  109. {
  110. struct vxfs_inode_info *vi;
  111. vi = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL);
  112. if (!vi)
  113. return NULL;
  114. inode_init_once(&vi->vfs_inode);
  115. return &vi->vfs_inode;
  116. }
  117. static void vxfs_i_callback(struct rcu_head *head)
  118. {
  119. struct inode *inode = container_of(head, struct inode, i_rcu);
  120. kmem_cache_free(vxfs_inode_cachep, VXFS_INO(inode));
  121. }
  122. static void vxfs_destroy_inode(struct inode *inode)
  123. {
  124. call_rcu(&inode->i_rcu, vxfs_i_callback);
  125. }
  126. static const struct super_operations vxfs_super_ops = {
  127. .alloc_inode = vxfs_alloc_inode,
  128. .destroy_inode = vxfs_destroy_inode,
  129. .evict_inode = vxfs_evict_inode,
  130. .put_super = vxfs_put_super,
  131. .statfs = vxfs_statfs,
  132. .remount_fs = vxfs_remount,
  133. };
  134. static int vxfs_try_sb_magic(struct super_block *sbp, int silent,
  135. unsigned blk, __fs32 magic)
  136. {
  137. struct buffer_head *bp;
  138. struct vxfs_sb *rsbp;
  139. struct vxfs_sb_info *infp = VXFS_SBI(sbp);
  140. int rc = -ENOMEM;
  141. bp = sb_bread(sbp, blk);
  142. do {
  143. if (!bp || !buffer_mapped(bp)) {
  144. if (!silent) {
  145. printk(KERN_WARNING
  146. "vxfs: unable to read disk superblock at %u\n",
  147. blk);
  148. }
  149. break;
  150. }
  151. rc = -EINVAL;
  152. rsbp = (struct vxfs_sb *)bp->b_data;
  153. if (rsbp->vs_magic != magic) {
  154. if (!silent)
  155. printk(KERN_NOTICE
  156. "vxfs: WRONG superblock magic %08x at %u\n",
  157. rsbp->vs_magic, blk);
  158. break;
  159. }
  160. rc = 0;
  161. infp->vsi_raw = rsbp;
  162. infp->vsi_bp = bp;
  163. } while (0);
  164. if (rc) {
  165. infp->vsi_raw = NULL;
  166. infp->vsi_bp = NULL;
  167. brelse(bp);
  168. }
  169. return rc;
  170. }
  171. /**
  172. * vxfs_read_super - read superblock into memory and initialize filesystem
  173. * @sbp: VFS superblock (to fill)
  174. * @dp: fs private mount data
  175. * @silent: do not complain loudly when sth is wrong
  176. *
  177. * Description:
  178. * We are called on the first mount of a filesystem to read the
  179. * superblock into memory and do some basic setup.
  180. *
  181. * Returns:
  182. * The superblock on success, else %NULL.
  183. *
  184. * Locking:
  185. * We are under @sbp->s_lock.
  186. */
  187. static int vxfs_fill_super(struct super_block *sbp, void *dp, int silent)
  188. {
  189. struct vxfs_sb_info *infp;
  190. struct vxfs_sb *rsbp;
  191. u_long bsize;
  192. struct inode *root;
  193. int ret = -EINVAL;
  194. u32 j;
  195. sbp->s_flags |= MS_RDONLY;
  196. infp = kzalloc(sizeof(*infp), GFP_KERNEL);
  197. if (!infp) {
  198. printk(KERN_WARNING "vxfs: unable to allocate incore superblock\n");
  199. return -ENOMEM;
  200. }
  201. bsize = sb_min_blocksize(sbp, BLOCK_SIZE);
  202. if (!bsize) {
  203. printk(KERN_WARNING "vxfs: unable to set blocksize\n");
  204. goto out;
  205. }
  206. sbp->s_op = &vxfs_super_ops;
  207. sbp->s_fs_info = infp;
  208. if (!vxfs_try_sb_magic(sbp, silent, 1,
  209. (__force __fs32)cpu_to_le32(VXFS_SUPER_MAGIC))) {
  210. /* Unixware, x86 */
  211. infp->byte_order = VXFS_BO_LE;
  212. } else if (!vxfs_try_sb_magic(sbp, silent, 8,
  213. (__force __fs32)cpu_to_be32(VXFS_SUPER_MAGIC))) {
  214. /* HP-UX, parisc */
  215. infp->byte_order = VXFS_BO_BE;
  216. } else {
  217. if (!silent)
  218. printk(KERN_NOTICE "vxfs: can't find superblock.\n");
  219. goto out;
  220. }
  221. rsbp = infp->vsi_raw;
  222. j = fs32_to_cpu(infp, rsbp->vs_version);
  223. if ((j < 2 || j > 4) && !silent) {
  224. printk(KERN_NOTICE "vxfs: unsupported VxFS version (%d)\n", j);
  225. goto out;
  226. }
  227. #ifdef DIAGNOSTIC
  228. printk(KERN_DEBUG "vxfs: supported VxFS version (%d)\n", j);
  229. printk(KERN_DEBUG "vxfs: blocksize: %d\n",
  230. fs32_to_cpu(infp, rsbp->vs_bsize));
  231. #endif
  232. sbp->s_magic = fs32_to_cpu(infp, rsbp->vs_magic);
  233. infp->vsi_oltext = fs32_to_cpu(infp, rsbp->vs_oltext[0]);
  234. infp->vsi_oltsize = fs32_to_cpu(infp, rsbp->vs_oltsize);
  235. j = fs32_to_cpu(infp, rsbp->vs_bsize);
  236. if (!sb_set_blocksize(sbp, j)) {
  237. printk(KERN_WARNING "vxfs: unable to set final block size\n");
  238. goto out;
  239. }
  240. if (vxfs_read_olt(sbp, bsize)) {
  241. printk(KERN_WARNING "vxfs: unable to read olt\n");
  242. goto out;
  243. }
  244. if (vxfs_read_fshead(sbp)) {
  245. printk(KERN_WARNING "vxfs: unable to read fshead\n");
  246. goto out;
  247. }
  248. root = vxfs_iget(sbp, VXFS_ROOT_INO);
  249. if (IS_ERR(root)) {
  250. ret = PTR_ERR(root);
  251. goto out;
  252. }
  253. sbp->s_root = d_make_root(root);
  254. if (!sbp->s_root) {
  255. printk(KERN_WARNING "vxfs: unable to get root dentry.\n");
  256. goto out_free_ilist;
  257. }
  258. return 0;
  259. out_free_ilist:
  260. iput(infp->vsi_fship);
  261. iput(infp->vsi_ilist);
  262. iput(infp->vsi_stilist);
  263. out:
  264. brelse(infp->vsi_bp);
  265. kfree(infp);
  266. return ret;
  267. }
  268. /*
  269. * The usual module blurb.
  270. */
  271. static struct dentry *vxfs_mount(struct file_system_type *fs_type,
  272. int flags, const char *dev_name, void *data)
  273. {
  274. return mount_bdev(fs_type, flags, dev_name, data, vxfs_fill_super);
  275. }
  276. static struct file_system_type vxfs_fs_type = {
  277. .owner = THIS_MODULE,
  278. .name = "vxfs",
  279. .mount = vxfs_mount,
  280. .kill_sb = kill_block_super,
  281. .fs_flags = FS_REQUIRES_DEV,
  282. };
  283. MODULE_ALIAS_FS("vxfs"); /* makes mount -t vxfs autoload the module */
  284. MODULE_ALIAS("vxfs");
  285. static int __init
  286. vxfs_init(void)
  287. {
  288. int rv;
  289. vxfs_inode_cachep = kmem_cache_create("vxfs_inode",
  290. sizeof(struct vxfs_inode_info), 0,
  291. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
  292. if (!vxfs_inode_cachep)
  293. return -ENOMEM;
  294. rv = register_filesystem(&vxfs_fs_type);
  295. if (rv < 0)
  296. kmem_cache_destroy(vxfs_inode_cachep);
  297. return rv;
  298. }
  299. static void __exit
  300. vxfs_cleanup(void)
  301. {
  302. unregister_filesystem(&vxfs_fs_type);
  303. /*
  304. * Make sure all delayed rcu free inodes are flushed before we
  305. * destroy cache.
  306. */
  307. rcu_barrier();
  308. kmem_cache_destroy(vxfs_inode_cachep);
  309. }
  310. module_init(vxfs_init);
  311. module_exit(vxfs_cleanup);