vxfs_inode.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2000-2001 Christoph Hellwig.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions, and the following disclaimer,
  10. * without modification.
  11. * 2. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * Alternatively, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL").
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Veritas filesystem driver - inode routines.
  31. */
  32. #include <linux/fs.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/kernel.h>
  36. #include <linux/slab.h>
  37. #include "vxfs.h"
  38. #include "vxfs_inode.h"
  39. #include "vxfs_extern.h"
  40. extern const struct address_space_operations vxfs_aops;
  41. extern const struct address_space_operations vxfs_immed_aops;
  42. extern const struct inode_operations vxfs_immed_symlink_iops;
  43. struct kmem_cache *vxfs_inode_cachep;
  44. #ifdef DIAGNOSTIC
  45. /*
  46. * Dump inode contents (partially).
  47. */
  48. void
  49. vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
  50. {
  51. printk(KERN_DEBUG "\n\n");
  52. if (ino)
  53. printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
  54. else
  55. printk(KERN_DEBUG "dumping unknown vxfs inode\n");
  56. printk(KERN_DEBUG "---------------------------\n");
  57. printk(KERN_DEBUG "mode is %x\n", vip->vii_mode);
  58. printk(KERN_DEBUG "nlink:%u, uid:%u, gid:%u\n",
  59. vip->vii_nlink, vip->vii_uid, vip->vii_gid);
  60. printk(KERN_DEBUG "size:%Lx, blocks:%u\n",
  61. vip->vii_size, vip->vii_blocks);
  62. printk(KERN_DEBUG "orgtype:%u\n", vip->vii_orgtype);
  63. }
  64. #endif
  65. /**
  66. * vxfs_blkiget - find inode based on extent #
  67. * @sbp: superblock of the filesystem we search in
  68. * @extent: number of the extent to search
  69. * @ino: inode number to search
  70. *
  71. * Description:
  72. * vxfs_blkiget searches inode @ino in the filesystem described by
  73. * @sbp in the extent @extent.
  74. * Returns the matching VxFS inode on success, else a NULL pointer.
  75. *
  76. * NOTE:
  77. * While __vxfs_iget uses the pagecache vxfs_blkiget uses the
  78. * buffercache. This function should not be used outside the
  79. * read_super() method, otherwise the data may be incoherent.
  80. */
  81. struct vxfs_inode_info *
  82. vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
  83. {
  84. struct buffer_head *bp;
  85. u_long block, offset;
  86. block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
  87. offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
  88. bp = sb_bread(sbp, block);
  89. if (buffer_mapped(bp)) {
  90. struct vxfs_inode_info *vip;
  91. struct vxfs_dinode *dip;
  92. if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
  93. goto fail;
  94. dip = (struct vxfs_dinode *)(bp->b_data + offset);
  95. memcpy(vip, dip, sizeof(*vip));
  96. #ifdef DIAGNOSTIC
  97. vxfs_dumpi(vip, ino);
  98. #endif
  99. brelse(bp);
  100. return (vip);
  101. }
  102. fail:
  103. printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
  104. brelse(bp);
  105. return NULL;
  106. }
  107. /**
  108. * __vxfs_iget - generic find inode facility
  109. * @sbp: VFS superblock
  110. * @ino: inode number
  111. * @ilistp: inode list
  112. *
  113. * Description:
  114. * Search the for inode number @ino in the filesystem
  115. * described by @sbp. Use the specified inode table (@ilistp).
  116. * Returns the matching VxFS inode on success, else a NULL pointer.
  117. */
  118. static struct vxfs_inode_info *
  119. __vxfs_iget(ino_t ino, struct inode *ilistp)
  120. {
  121. struct page *pp;
  122. u_long offset;
  123. offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
  124. pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
  125. if (!IS_ERR(pp)) {
  126. struct vxfs_inode_info *vip;
  127. struct vxfs_dinode *dip;
  128. caddr_t kaddr = (char *)page_address(pp);
  129. if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
  130. goto fail;
  131. dip = (struct vxfs_dinode *)(kaddr + offset);
  132. memcpy(vip, dip, sizeof(*vip));
  133. #ifdef DIAGNOSTIC
  134. vxfs_dumpi(vip, ino);
  135. #endif
  136. vxfs_put_page(pp);
  137. return (vip);
  138. }
  139. printk(KERN_WARNING "vxfs: error on page %p\n", pp);
  140. return NULL;
  141. fail:
  142. printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
  143. vxfs_put_page(pp);
  144. return NULL;
  145. }
  146. /**
  147. * vxfs_stiget - find inode using the structural inode list
  148. * @sbp: VFS superblock
  149. * @ino: inode #
  150. *
  151. * Description:
  152. * Find inode @ino in the filesystem described by @sbp using
  153. * the structural inode list.
  154. * Returns the matching VxFS inode on success, else a NULL pointer.
  155. */
  156. struct vxfs_inode_info *
  157. vxfs_stiget(struct super_block *sbp, ino_t ino)
  158. {
  159. return __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
  160. }
  161. /**
  162. * vxfs_transmod - mode for a VxFS inode
  163. * @vip: VxFS inode
  164. *
  165. * Description:
  166. * vxfs_transmod returns a Linux mode_t for a given
  167. * VxFS inode structure.
  168. */
  169. static __inline__ mode_t
  170. vxfs_transmod(struct vxfs_inode_info *vip)
  171. {
  172. mode_t ret = vip->vii_mode & ~VXFS_TYPE_MASK;
  173. if (VXFS_ISFIFO(vip))
  174. ret |= S_IFIFO;
  175. if (VXFS_ISCHR(vip))
  176. ret |= S_IFCHR;
  177. if (VXFS_ISDIR(vip))
  178. ret |= S_IFDIR;
  179. if (VXFS_ISBLK(vip))
  180. ret |= S_IFBLK;
  181. if (VXFS_ISLNK(vip))
  182. ret |= S_IFLNK;
  183. if (VXFS_ISREG(vip))
  184. ret |= S_IFREG;
  185. if (VXFS_ISSOC(vip))
  186. ret |= S_IFSOCK;
  187. return (ret);
  188. }
  189. /**
  190. * vxfs_iinit- helper to fill inode fields
  191. * @ip: VFS inode
  192. * @vip: VxFS inode
  193. *
  194. * Description:
  195. * vxfs_instino is a helper function to fill in all relevant
  196. * fields in @ip from @vip.
  197. */
  198. static void
  199. vxfs_iinit(struct inode *ip, struct vxfs_inode_info *vip)
  200. {
  201. ip->i_mode = vxfs_transmod(vip);
  202. ip->i_uid = (uid_t)vip->vii_uid;
  203. ip->i_gid = (gid_t)vip->vii_gid;
  204. ip->i_nlink = vip->vii_nlink;
  205. ip->i_size = vip->vii_size;
  206. ip->i_atime.tv_sec = vip->vii_atime;
  207. ip->i_ctime.tv_sec = vip->vii_ctime;
  208. ip->i_mtime.tv_sec = vip->vii_mtime;
  209. ip->i_atime.tv_nsec = 0;
  210. ip->i_ctime.tv_nsec = 0;
  211. ip->i_mtime.tv_nsec = 0;
  212. ip->i_blocks = vip->vii_blocks;
  213. ip->i_generation = vip->vii_gen;
  214. ip->i_private = vip;
  215. }
  216. /**
  217. * vxfs_get_fake_inode - get fake inode structure
  218. * @sbp: filesystem superblock
  219. * @vip: fspriv inode
  220. *
  221. * Description:
  222. * vxfs_fake_inode gets a fake inode (not in the inode hash) for a
  223. * superblock, vxfs_inode pair.
  224. * Returns the filled VFS inode.
  225. */
  226. struct inode *
  227. vxfs_get_fake_inode(struct super_block *sbp, struct vxfs_inode_info *vip)
  228. {
  229. struct inode *ip = NULL;
  230. if ((ip = new_inode(sbp))) {
  231. vxfs_iinit(ip, vip);
  232. ip->i_mapping->a_ops = &vxfs_aops;
  233. }
  234. return (ip);
  235. }
  236. /**
  237. * vxfs_put_fake_inode - free faked inode
  238. * *ip: VFS inode
  239. *
  240. * Description:
  241. * vxfs_put_fake_inode frees all data asssociated with @ip.
  242. */
  243. void
  244. vxfs_put_fake_inode(struct inode *ip)
  245. {
  246. iput(ip);
  247. }
  248. /**
  249. * vxfs_read_inode - fill in inode information
  250. * @ip: inode pointer to fill
  251. *
  252. * Description:
  253. * vxfs_read_inode reads the disk inode for @ip and fills
  254. * in all relevant fields in @ip.
  255. */
  256. void
  257. vxfs_read_inode(struct inode *ip)
  258. {
  259. struct super_block *sbp = ip->i_sb;
  260. struct vxfs_inode_info *vip;
  261. const struct address_space_operations *aops;
  262. ino_t ino = ip->i_ino;
  263. if (!(vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist)))
  264. return;
  265. vxfs_iinit(ip, vip);
  266. if (VXFS_ISIMMED(vip))
  267. aops = &vxfs_immed_aops;
  268. else
  269. aops = &vxfs_aops;
  270. if (S_ISREG(ip->i_mode)) {
  271. ip->i_fop = &generic_ro_fops;
  272. ip->i_mapping->a_ops = aops;
  273. } else if (S_ISDIR(ip->i_mode)) {
  274. ip->i_op = &vxfs_dir_inode_ops;
  275. ip->i_fop = &vxfs_dir_operations;
  276. ip->i_mapping->a_ops = aops;
  277. } else if (S_ISLNK(ip->i_mode)) {
  278. if (!VXFS_ISIMMED(vip)) {
  279. ip->i_op = &page_symlink_inode_operations;
  280. ip->i_mapping->a_ops = &vxfs_aops;
  281. } else
  282. ip->i_op = &vxfs_immed_symlink_iops;
  283. } else
  284. init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
  285. return;
  286. }
  287. /**
  288. * vxfs_clear_inode - remove inode from main memory
  289. * @ip: inode to discard.
  290. *
  291. * Description:
  292. * vxfs_clear_inode() is called on the final iput and frees the private
  293. * inode area.
  294. */
  295. void
  296. vxfs_clear_inode(struct inode *ip)
  297. {
  298. kmem_cache_free(vxfs_inode_cachep, ip->i_private);
  299. }