vxfs_inode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. struct kmem_cache *vxfs_inode_cachep;
  41. #ifdef DIAGNOSTIC
  42. /*
  43. * Dump inode contents (partially).
  44. */
  45. void
  46. vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
  47. {
  48. printk(KERN_DEBUG "\n\n");
  49. if (ino)
  50. printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
  51. else
  52. printk(KERN_DEBUG "dumping unknown vxfs inode\n");
  53. printk(KERN_DEBUG "---------------------------\n");
  54. printk(KERN_DEBUG "mode is %x\n", vip->vii_mode);
  55. printk(KERN_DEBUG "nlink:%u, uid:%u, gid:%u\n",
  56. vip->vii_nlink, vip->vii_uid, vip->vii_gid);
  57. printk(KERN_DEBUG "size:%Lx, blocks:%u\n",
  58. vip->vii_size, vip->vii_blocks);
  59. printk(KERN_DEBUG "orgtype:%u\n", vip->vii_orgtype);
  60. }
  61. #endif
  62. /**
  63. * vxfs_blkiget - find inode based on extent #
  64. * @sbp: superblock of the filesystem we search in
  65. * @extent: number of the extent to search
  66. * @ino: inode number to search
  67. *
  68. * Description:
  69. * vxfs_blkiget searches inode @ino in the filesystem described by
  70. * @sbp in the extent @extent.
  71. * Returns the matching VxFS inode on success, else a NULL pointer.
  72. *
  73. * NOTE:
  74. * While __vxfs_iget uses the pagecache vxfs_blkiget uses the
  75. * buffercache. This function should not be used outside the
  76. * read_super() method, otherwise the data may be incoherent.
  77. */
  78. struct vxfs_inode_info *
  79. vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
  80. {
  81. struct buffer_head *bp;
  82. u_long block, offset;
  83. block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
  84. offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
  85. bp = sb_bread(sbp, block);
  86. if (bp && buffer_mapped(bp)) {
  87. struct vxfs_inode_info *vip;
  88. struct vxfs_dinode *dip;
  89. if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
  90. goto fail;
  91. dip = (struct vxfs_dinode *)(bp->b_data + offset);
  92. memcpy(vip, dip, sizeof(*vip));
  93. #ifdef DIAGNOSTIC
  94. vxfs_dumpi(vip, ino);
  95. #endif
  96. brelse(bp);
  97. return (vip);
  98. }
  99. fail:
  100. printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
  101. brelse(bp);
  102. return NULL;
  103. }
  104. /**
  105. * __vxfs_iget - generic find inode facility
  106. * @sbp: VFS superblock
  107. * @ino: inode number
  108. * @ilistp: inode list
  109. *
  110. * Description:
  111. * Search the for inode number @ino in the filesystem
  112. * described by @sbp. Use the specified inode table (@ilistp).
  113. * Returns the matching VxFS inode on success, else an error code.
  114. */
  115. static struct vxfs_inode_info *
  116. __vxfs_iget(ino_t ino, struct inode *ilistp)
  117. {
  118. struct page *pp;
  119. u_long offset;
  120. offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
  121. pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
  122. if (!IS_ERR(pp)) {
  123. struct vxfs_inode_info *vip;
  124. struct vxfs_dinode *dip;
  125. caddr_t kaddr = (char *)page_address(pp);
  126. if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
  127. goto fail;
  128. dip = (struct vxfs_dinode *)(kaddr + offset);
  129. memcpy(vip, dip, sizeof(*vip));
  130. #ifdef DIAGNOSTIC
  131. vxfs_dumpi(vip, ino);
  132. #endif
  133. vxfs_put_page(pp);
  134. return (vip);
  135. }
  136. printk(KERN_WARNING "vxfs: error on page %p\n", pp);
  137. return ERR_CAST(pp);
  138. fail:
  139. printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
  140. vxfs_put_page(pp);
  141. return ERR_PTR(-ENOMEM);
  142. }
  143. /**
  144. * vxfs_stiget - find inode using the structural inode list
  145. * @sbp: VFS superblock
  146. * @ino: inode #
  147. *
  148. * Description:
  149. * Find inode @ino in the filesystem described by @sbp using
  150. * the structural inode list.
  151. * Returns the matching VxFS inode on success, else a NULL pointer.
  152. */
  153. struct vxfs_inode_info *
  154. vxfs_stiget(struct super_block *sbp, ino_t ino)
  155. {
  156. struct vxfs_inode_info *vip;
  157. vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
  158. return IS_ERR(vip) ? NULL : vip;
  159. }
  160. /**
  161. * vxfs_transmod - mode for a VxFS inode
  162. * @vip: VxFS inode
  163. *
  164. * Description:
  165. * vxfs_transmod returns a Linux mode_t for a given
  166. * VxFS inode structure.
  167. */
  168. static __inline__ umode_t
  169. vxfs_transmod(struct vxfs_inode_info *vip)
  170. {
  171. umode_t ret = vip->vii_mode & ~VXFS_TYPE_MASK;
  172. if (VXFS_ISFIFO(vip))
  173. ret |= S_IFIFO;
  174. if (VXFS_ISCHR(vip))
  175. ret |= S_IFCHR;
  176. if (VXFS_ISDIR(vip))
  177. ret |= S_IFDIR;
  178. if (VXFS_ISBLK(vip))
  179. ret |= S_IFBLK;
  180. if (VXFS_ISLNK(vip))
  181. ret |= S_IFLNK;
  182. if (VXFS_ISREG(vip))
  183. ret |= S_IFREG;
  184. if (VXFS_ISSOC(vip))
  185. ret |= S_IFSOCK;
  186. return (ret);
  187. }
  188. /**
  189. * vxfs_iinit- helper to fill inode fields
  190. * @ip: VFS inode
  191. * @vip: VxFS inode
  192. *
  193. * Description:
  194. * vxfs_instino is a helper function to fill in all relevant
  195. * fields in @ip from @vip.
  196. */
  197. static void
  198. vxfs_iinit(struct inode *ip, struct vxfs_inode_info *vip)
  199. {
  200. ip->i_mode = vxfs_transmod(vip);
  201. ip->i_uid = (uid_t)vip->vii_uid;
  202. ip->i_gid = (gid_t)vip->vii_gid;
  203. set_nlink(ip, vip->vii_nlink);
  204. ip->i_size = vip->vii_size;
  205. ip->i_atime.tv_sec = vip->vii_atime;
  206. ip->i_ctime.tv_sec = vip->vii_ctime;
  207. ip->i_mtime.tv_sec = vip->vii_mtime;
  208. ip->i_atime.tv_nsec = 0;
  209. ip->i_ctime.tv_nsec = 0;
  210. ip->i_mtime.tv_nsec = 0;
  211. ip->i_blocks = vip->vii_blocks;
  212. ip->i_generation = vip->vii_gen;
  213. ip->i_private = vip;
  214. }
  215. /**
  216. * vxfs_get_fake_inode - get fake inode structure
  217. * @sbp: filesystem superblock
  218. * @vip: fspriv inode
  219. *
  220. * Description:
  221. * vxfs_fake_inode gets a fake inode (not in the inode hash) for a
  222. * superblock, vxfs_inode pair.
  223. * Returns the filled VFS inode.
  224. */
  225. struct inode *
  226. vxfs_get_fake_inode(struct super_block *sbp, struct vxfs_inode_info *vip)
  227. {
  228. struct inode *ip = NULL;
  229. if ((ip = new_inode(sbp))) {
  230. ip->i_ino = get_next_ino();
  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 associated with @ip.
  242. */
  243. void
  244. vxfs_put_fake_inode(struct inode *ip)
  245. {
  246. iput(ip);
  247. }
  248. /**
  249. * vxfs_iget - get an inode
  250. * @sbp: the superblock to get the inode for
  251. * @ino: the number of the inode to get
  252. *
  253. * Description:
  254. * vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
  255. * in all relevant fields in the new inode.
  256. */
  257. struct inode *
  258. vxfs_iget(struct super_block *sbp, ino_t ino)
  259. {
  260. struct vxfs_inode_info *vip;
  261. const struct address_space_operations *aops;
  262. struct inode *ip;
  263. ip = iget_locked(sbp, ino);
  264. if (!ip)
  265. return ERR_PTR(-ENOMEM);
  266. if (!(ip->i_state & I_NEW))
  267. return ip;
  268. vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist);
  269. if (IS_ERR(vip)) {
  270. iget_failed(ip);
  271. return ERR_CAST(vip);
  272. }
  273. vxfs_iinit(ip, vip);
  274. if (VXFS_ISIMMED(vip))
  275. aops = &vxfs_immed_aops;
  276. else
  277. aops = &vxfs_aops;
  278. if (S_ISREG(ip->i_mode)) {
  279. ip->i_fop = &generic_ro_fops;
  280. ip->i_mapping->a_ops = aops;
  281. } else if (S_ISDIR(ip->i_mode)) {
  282. ip->i_op = &vxfs_dir_inode_ops;
  283. ip->i_fop = &vxfs_dir_operations;
  284. ip->i_mapping->a_ops = aops;
  285. } else if (S_ISLNK(ip->i_mode)) {
  286. if (!VXFS_ISIMMED(vip)) {
  287. ip->i_op = &page_symlink_inode_operations;
  288. ip->i_mapping->a_ops = &vxfs_aops;
  289. } else {
  290. ip->i_op = &vxfs_immed_symlink_iops;
  291. vip->vii_immed.vi_immed[ip->i_size] = '\0';
  292. }
  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. static void vxfs_i_callback(struct rcu_head *head)
  299. {
  300. struct inode *inode = container_of(head, struct inode, i_rcu);
  301. kmem_cache_free(vxfs_inode_cachep, inode->i_private);
  302. }
  303. /**
  304. * vxfs_evict_inode - remove inode from main memory
  305. * @ip: inode to discard.
  306. *
  307. * Description:
  308. * vxfs_evict_inode() is called on the final iput and frees the private
  309. * inode area.
  310. */
  311. void
  312. vxfs_evict_inode(struct inode *ip)
  313. {
  314. truncate_inode_pages(&ip->i_data, 0);
  315. end_writeback(ip);
  316. call_rcu(&ip->i_rcu, vxfs_i_callback);
  317. }