vxfs_inode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 <linux/namei.h>
  38. #include "vxfs.h"
  39. #include "vxfs_inode.h"
  40. #include "vxfs_extern.h"
  41. struct kmem_cache *vxfs_inode_cachep;
  42. #ifdef DIAGNOSTIC
  43. /*
  44. * Dump inode contents (partially).
  45. */
  46. void
  47. vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
  48. {
  49. printk(KERN_DEBUG "\n\n");
  50. if (ino)
  51. printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
  52. else
  53. printk(KERN_DEBUG "dumping unknown vxfs inode\n");
  54. printk(KERN_DEBUG "---------------------------\n");
  55. printk(KERN_DEBUG "mode is %x\n", vip->vii_mode);
  56. printk(KERN_DEBUG "nlink:%u, uid:%u, gid:%u\n",
  57. vip->vii_nlink, vip->vii_uid, vip->vii_gid);
  58. printk(KERN_DEBUG "size:%Lx, blocks:%u\n",
  59. vip->vii_size, vip->vii_blocks);
  60. printk(KERN_DEBUG "orgtype:%u\n", vip->vii_orgtype);
  61. }
  62. #endif
  63. /**
  64. * vxfs_blkiget - find inode based on extent #
  65. * @sbp: superblock of the filesystem we search in
  66. * @extent: number of the extent to search
  67. * @ino: inode number to search
  68. *
  69. * Description:
  70. * vxfs_blkiget searches inode @ino in the filesystem described by
  71. * @sbp in the extent @extent.
  72. * Returns the matching VxFS inode on success, else a NULL pointer.
  73. *
  74. * NOTE:
  75. * While __vxfs_iget uses the pagecache vxfs_blkiget uses the
  76. * buffercache. This function should not be used outside the
  77. * read_super() method, otherwise the data may be incoherent.
  78. */
  79. struct vxfs_inode_info *
  80. vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
  81. {
  82. struct buffer_head *bp;
  83. u_long block, offset;
  84. block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
  85. offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
  86. bp = sb_bread(sbp, block);
  87. if (bp && buffer_mapped(bp)) {
  88. struct vxfs_inode_info *vip;
  89. struct vxfs_dinode *dip;
  90. if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
  91. goto fail;
  92. dip = (struct vxfs_dinode *)(bp->b_data + offset);
  93. memcpy(vip, dip, sizeof(*vip));
  94. #ifdef DIAGNOSTIC
  95. vxfs_dumpi(vip, ino);
  96. #endif
  97. brelse(bp);
  98. return (vip);
  99. }
  100. fail:
  101. printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
  102. brelse(bp);
  103. return NULL;
  104. }
  105. /**
  106. * __vxfs_iget - generic find inode facility
  107. * @sbp: VFS superblock
  108. * @ino: inode number
  109. * @ilistp: inode list
  110. *
  111. * Description:
  112. * Search the for inode number @ino in the filesystem
  113. * described by @sbp. Use the specified inode table (@ilistp).
  114. * Returns the matching VxFS inode on success, else an error code.
  115. */
  116. static struct vxfs_inode_info *
  117. __vxfs_iget(ino_t ino, struct inode *ilistp)
  118. {
  119. struct page *pp;
  120. u_long offset;
  121. offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
  122. pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
  123. if (!IS_ERR(pp)) {
  124. struct vxfs_inode_info *vip;
  125. struct vxfs_dinode *dip;
  126. caddr_t kaddr = (char *)page_address(pp);
  127. if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
  128. goto fail;
  129. dip = (struct vxfs_dinode *)(kaddr + offset);
  130. memcpy(vip, dip, sizeof(*vip));
  131. #ifdef DIAGNOSTIC
  132. vxfs_dumpi(vip, ino);
  133. #endif
  134. vxfs_put_page(pp);
  135. return (vip);
  136. }
  137. printk(KERN_WARNING "vxfs: error on page %p\n", pp);
  138. return ERR_CAST(pp);
  139. fail:
  140. printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
  141. vxfs_put_page(pp);
  142. return ERR_PTR(-ENOMEM);
  143. }
  144. /**
  145. * vxfs_stiget - find inode using the structural inode list
  146. * @sbp: VFS superblock
  147. * @ino: inode #
  148. *
  149. * Description:
  150. * Find inode @ino in the filesystem described by @sbp using
  151. * the structural inode list.
  152. * Returns the matching VxFS inode on success, else a NULL pointer.
  153. */
  154. struct vxfs_inode_info *
  155. vxfs_stiget(struct super_block *sbp, ino_t ino)
  156. {
  157. struct vxfs_inode_info *vip;
  158. vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
  159. return IS_ERR(vip) ? NULL : vip;
  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__ umode_t
  170. vxfs_transmod(struct vxfs_inode_info *vip)
  171. {
  172. umode_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. i_uid_write(ip, (uid_t)vip->vii_uid);
  203. i_gid_write(ip, (gid_t)vip->vii_gid);
  204. set_nlink(ip, 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. ip->i_ino = get_next_ino();
  232. vxfs_iinit(ip, vip);
  233. ip->i_mapping->a_ops = &vxfs_aops;
  234. }
  235. return (ip);
  236. }
  237. /**
  238. * vxfs_put_fake_inode - free faked inode
  239. * *ip: VFS inode
  240. *
  241. * Description:
  242. * vxfs_put_fake_inode frees all data associated with @ip.
  243. */
  244. void
  245. vxfs_put_fake_inode(struct inode *ip)
  246. {
  247. iput(ip);
  248. }
  249. /**
  250. * vxfs_iget - get an inode
  251. * @sbp: the superblock to get the inode for
  252. * @ino: the number of the inode to get
  253. *
  254. * Description:
  255. * vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
  256. * in all relevant fields in the new inode.
  257. */
  258. struct inode *
  259. vxfs_iget(struct super_block *sbp, ino_t ino)
  260. {
  261. struct vxfs_inode_info *vip;
  262. const struct address_space_operations *aops;
  263. struct inode *ip;
  264. ip = iget_locked(sbp, ino);
  265. if (!ip)
  266. return ERR_PTR(-ENOMEM);
  267. if (!(ip->i_state & I_NEW))
  268. return ip;
  269. vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist);
  270. if (IS_ERR(vip)) {
  271. iget_failed(ip);
  272. return ERR_CAST(vip);
  273. }
  274. vxfs_iinit(ip, vip);
  275. if (VXFS_ISIMMED(vip))
  276. aops = &vxfs_immed_aops;
  277. else
  278. aops = &vxfs_aops;
  279. if (S_ISREG(ip->i_mode)) {
  280. ip->i_fop = &generic_ro_fops;
  281. ip->i_mapping->a_ops = aops;
  282. } else if (S_ISDIR(ip->i_mode)) {
  283. ip->i_op = &vxfs_dir_inode_ops;
  284. ip->i_fop = &vxfs_dir_operations;
  285. ip->i_mapping->a_ops = aops;
  286. } else if (S_ISLNK(ip->i_mode)) {
  287. if (!VXFS_ISIMMED(vip)) {
  288. ip->i_op = &page_symlink_inode_operations;
  289. ip->i_mapping->a_ops = &vxfs_aops;
  290. } else {
  291. ip->i_op = &simple_symlink_inode_operations;
  292. ip->i_link = vip->vii_immed.vi_immed;
  293. nd_terminate_link(ip->i_link, ip->i_size,
  294. sizeof(vip->vii_immed.vi_immed) - 1);
  295. }
  296. } else
  297. init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
  298. unlock_new_inode(ip);
  299. return ip;
  300. }
  301. static void vxfs_i_callback(struct rcu_head *head)
  302. {
  303. struct inode *inode = container_of(head, struct inode, i_rcu);
  304. kmem_cache_free(vxfs_inode_cachep, inode->i_private);
  305. }
  306. /**
  307. * vxfs_evict_inode - remove inode from main memory
  308. * @ip: inode to discard.
  309. *
  310. * Description:
  311. * vxfs_evict_inode() is called on the final iput and frees the private
  312. * inode area.
  313. */
  314. void
  315. vxfs_evict_inode(struct inode *ip)
  316. {
  317. truncate_inode_pages_final(&ip->i_data);
  318. clear_inode(ip);
  319. call_rcu(&ip->i_rcu, vxfs_i_callback);
  320. }