vxfs_inode.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 (bp && 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 an error code.
  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 ERR_CAST(pp);
  141. fail:
  142. printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
  143. vxfs_put_page(pp);
  144. return ERR_PTR(-ENOMEM);
  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. struct vxfs_inode_info *vip;
  160. vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
  161. return IS_ERR(vip) ? NULL : vip;
  162. }
  163. /**
  164. * vxfs_transmod - mode for a VxFS inode
  165. * @vip: VxFS inode
  166. *
  167. * Description:
  168. * vxfs_transmod returns a Linux mode_t for a given
  169. * VxFS inode structure.
  170. */
  171. static __inline__ mode_t
  172. vxfs_transmod(struct vxfs_inode_info *vip)
  173. {
  174. mode_t ret = vip->vii_mode & ~VXFS_TYPE_MASK;
  175. if (VXFS_ISFIFO(vip))
  176. ret |= S_IFIFO;
  177. if (VXFS_ISCHR(vip))
  178. ret |= S_IFCHR;
  179. if (VXFS_ISDIR(vip))
  180. ret |= S_IFDIR;
  181. if (VXFS_ISBLK(vip))
  182. ret |= S_IFBLK;
  183. if (VXFS_ISLNK(vip))
  184. ret |= S_IFLNK;
  185. if (VXFS_ISREG(vip))
  186. ret |= S_IFREG;
  187. if (VXFS_ISSOC(vip))
  188. ret |= S_IFSOCK;
  189. return (ret);
  190. }
  191. /**
  192. * vxfs_iinit- helper to fill inode fields
  193. * @ip: VFS inode
  194. * @vip: VxFS inode
  195. *
  196. * Description:
  197. * vxfs_instino is a helper function to fill in all relevant
  198. * fields in @ip from @vip.
  199. */
  200. static void
  201. vxfs_iinit(struct inode *ip, struct vxfs_inode_info *vip)
  202. {
  203. ip->i_mode = vxfs_transmod(vip);
  204. ip->i_uid = (uid_t)vip->vii_uid;
  205. ip->i_gid = (gid_t)vip->vii_gid;
  206. ip->i_nlink = vip->vii_nlink;
  207. ip->i_size = vip->vii_size;
  208. ip->i_atime.tv_sec = vip->vii_atime;
  209. ip->i_ctime.tv_sec = vip->vii_ctime;
  210. ip->i_mtime.tv_sec = vip->vii_mtime;
  211. ip->i_atime.tv_nsec = 0;
  212. ip->i_ctime.tv_nsec = 0;
  213. ip->i_mtime.tv_nsec = 0;
  214. ip->i_blocks = vip->vii_blocks;
  215. ip->i_generation = vip->vii_gen;
  216. ip->i_private = vip;
  217. }
  218. /**
  219. * vxfs_get_fake_inode - get fake inode structure
  220. * @sbp: filesystem superblock
  221. * @vip: fspriv inode
  222. *
  223. * Description:
  224. * vxfs_fake_inode gets a fake inode (not in the inode hash) for a
  225. * superblock, vxfs_inode pair.
  226. * Returns the filled VFS inode.
  227. */
  228. struct inode *
  229. vxfs_get_fake_inode(struct super_block *sbp, struct vxfs_inode_info *vip)
  230. {
  231. struct inode *ip = NULL;
  232. if ((ip = new_inode(sbp))) {
  233. vxfs_iinit(ip, vip);
  234. ip->i_mapping->a_ops = &vxfs_aops;
  235. }
  236. return (ip);
  237. }
  238. /**
  239. * vxfs_put_fake_inode - free faked inode
  240. * *ip: VFS inode
  241. *
  242. * Description:
  243. * vxfs_put_fake_inode frees all data asssociated with @ip.
  244. */
  245. void
  246. vxfs_put_fake_inode(struct inode *ip)
  247. {
  248. iput(ip);
  249. }
  250. /**
  251. * vxfs_iget - get an inode
  252. * @sbp: the superblock to get the inode for
  253. * @ino: the number of the inode to get
  254. *
  255. * Description:
  256. * vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
  257. * in all relevant fields in the new inode.
  258. */
  259. struct inode *
  260. vxfs_iget(struct super_block *sbp, ino_t ino)
  261. {
  262. struct vxfs_inode_info *vip;
  263. const struct address_space_operations *aops;
  264. struct inode *ip;
  265. ip = iget_locked(sbp, ino);
  266. if (!ip)
  267. return ERR_PTR(-ENOMEM);
  268. if (!(ip->i_state & I_NEW))
  269. return ip;
  270. vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist);
  271. if (IS_ERR(vip)) {
  272. iget_failed(ip);
  273. return ERR_CAST(vip);
  274. }
  275. vxfs_iinit(ip, vip);
  276. if (VXFS_ISIMMED(vip))
  277. aops = &vxfs_immed_aops;
  278. else
  279. aops = &vxfs_aops;
  280. if (S_ISREG(ip->i_mode)) {
  281. ip->i_fop = &generic_ro_fops;
  282. ip->i_mapping->a_ops = aops;
  283. } else if (S_ISDIR(ip->i_mode)) {
  284. ip->i_op = &vxfs_dir_inode_ops;
  285. ip->i_fop = &vxfs_dir_operations;
  286. ip->i_mapping->a_ops = aops;
  287. } else if (S_ISLNK(ip->i_mode)) {
  288. if (!VXFS_ISIMMED(vip)) {
  289. ip->i_op = &page_symlink_inode_operations;
  290. ip->i_mapping->a_ops = &vxfs_aops;
  291. } else
  292. ip->i_op = &vxfs_immed_symlink_iops;
  293. } else
  294. init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
  295. unlock_new_inode(ip);
  296. return ip;
  297. }
  298. /**
  299. * vxfs_clear_inode - remove inode from main memory
  300. * @ip: inode to discard.
  301. *
  302. * Description:
  303. * vxfs_clear_inode() is called on the final iput and frees the private
  304. * inode area.
  305. */
  306. void
  307. vxfs_clear_inode(struct inode *ip)
  308. {
  309. kmem_cache_free(vxfs_inode_cachep, ip->i_private);
  310. }