vxfs_super.c 8.7 KB

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