vxfs_inode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 - inode routines.
  32. */
  33. #include <linux/fs.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/kernel.h>
  37. #include <linux/slab.h>
  38. #include <linux/namei.h>
  39. #include "vxfs.h"
  40. #include "vxfs_inode.h"
  41. #include "vxfs_extern.h"
  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_transmod - mode for a VxFS inode
  65. * @vip: VxFS inode
  66. *
  67. * Description:
  68. * vxfs_transmod returns a Linux mode_t for a given
  69. * VxFS inode structure.
  70. */
  71. static __inline__ umode_t
  72. vxfs_transmod(struct vxfs_inode_info *vip)
  73. {
  74. umode_t ret = vip->vii_mode & ~VXFS_TYPE_MASK;
  75. if (VXFS_ISFIFO(vip))
  76. ret |= S_IFIFO;
  77. if (VXFS_ISCHR(vip))
  78. ret |= S_IFCHR;
  79. if (VXFS_ISDIR(vip))
  80. ret |= S_IFDIR;
  81. if (VXFS_ISBLK(vip))
  82. ret |= S_IFBLK;
  83. if (VXFS_ISLNK(vip))
  84. ret |= S_IFLNK;
  85. if (VXFS_ISREG(vip))
  86. ret |= S_IFREG;
  87. if (VXFS_ISSOC(vip))
  88. ret |= S_IFSOCK;
  89. return (ret);
  90. }
  91. static inline void dip2vip_cpy(struct vxfs_sb_info *sbi,
  92. struct vxfs_inode_info *vip, struct vxfs_dinode *dip)
  93. {
  94. struct inode *inode = &vip->vfs_inode;
  95. vip->vii_mode = fs32_to_cpu(sbi, dip->vdi_mode);
  96. vip->vii_nlink = fs32_to_cpu(sbi, dip->vdi_nlink);
  97. vip->vii_uid = fs32_to_cpu(sbi, dip->vdi_uid);
  98. vip->vii_gid = fs32_to_cpu(sbi, dip->vdi_gid);
  99. vip->vii_size = fs64_to_cpu(sbi, dip->vdi_size);
  100. vip->vii_atime = fs32_to_cpu(sbi, dip->vdi_atime);
  101. vip->vii_autime = fs32_to_cpu(sbi, dip->vdi_autime);
  102. vip->vii_mtime = fs32_to_cpu(sbi, dip->vdi_mtime);
  103. vip->vii_mutime = fs32_to_cpu(sbi, dip->vdi_mutime);
  104. vip->vii_ctime = fs32_to_cpu(sbi, dip->vdi_ctime);
  105. vip->vii_cutime = fs32_to_cpu(sbi, dip->vdi_cutime);
  106. vip->vii_orgtype = dip->vdi_orgtype;
  107. vip->vii_blocks = fs32_to_cpu(sbi, dip->vdi_blocks);
  108. vip->vii_gen = fs32_to_cpu(sbi, dip->vdi_gen);
  109. if (VXFS_ISDIR(vip))
  110. vip->vii_dotdot = fs32_to_cpu(sbi, dip->vdi_dotdot);
  111. else if (!VXFS_ISREG(vip) && !VXFS_ISLNK(vip))
  112. vip->vii_rdev = fs32_to_cpu(sbi, dip->vdi_rdev);
  113. /* don't endian swap the fields that differ by orgtype */
  114. memcpy(&vip->vii_org, &dip->vdi_org, sizeof(vip->vii_org));
  115. inode->i_mode = vxfs_transmod(vip);
  116. i_uid_write(inode, (uid_t)vip->vii_uid);
  117. i_gid_write(inode, (gid_t)vip->vii_gid);
  118. set_nlink(inode, vip->vii_nlink);
  119. inode->i_size = vip->vii_size;
  120. inode->i_atime.tv_sec = vip->vii_atime;
  121. inode->i_ctime.tv_sec = vip->vii_ctime;
  122. inode->i_mtime.tv_sec = vip->vii_mtime;
  123. inode->i_atime.tv_nsec = 0;
  124. inode->i_ctime.tv_nsec = 0;
  125. inode->i_mtime.tv_nsec = 0;
  126. inode->i_blocks = vip->vii_blocks;
  127. inode->i_generation = vip->vii_gen;
  128. }
  129. /**
  130. * vxfs_blkiget - find inode based on extent #
  131. * @sbp: superblock of the filesystem we search in
  132. * @extent: number of the extent to search
  133. * @ino: inode number to search
  134. *
  135. * Description:
  136. * vxfs_blkiget searches inode @ino in the filesystem described by
  137. * @sbp in the extent @extent.
  138. * Returns the matching VxFS inode on success, else a NULL pointer.
  139. *
  140. * NOTE:
  141. * While __vxfs_iget uses the pagecache vxfs_blkiget uses the
  142. * buffercache. This function should not be used outside the
  143. * read_super() method, otherwise the data may be incoherent.
  144. */
  145. struct inode *
  146. vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
  147. {
  148. struct buffer_head *bp;
  149. struct inode *inode;
  150. u_long block, offset;
  151. inode = new_inode(sbp);
  152. if (!inode)
  153. return NULL;
  154. inode->i_ino = get_next_ino();
  155. block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
  156. offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
  157. bp = sb_bread(sbp, block);
  158. if (bp && buffer_mapped(bp)) {
  159. struct vxfs_inode_info *vip = VXFS_INO(inode);
  160. struct vxfs_dinode *dip;
  161. dip = (struct vxfs_dinode *)(bp->b_data + offset);
  162. dip2vip_cpy(VXFS_SBI(sbp), vip, dip);
  163. vip->vfs_inode.i_mapping->a_ops = &vxfs_aops;
  164. #ifdef DIAGNOSTIC
  165. vxfs_dumpi(vip, ino);
  166. #endif
  167. brelse(bp);
  168. return inode;
  169. }
  170. printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
  171. brelse(bp);
  172. iput(inode);
  173. return NULL;
  174. }
  175. /**
  176. * __vxfs_iget - generic find inode facility
  177. * @ilistp: inode list
  178. * @vip: VxFS inode to fill in
  179. * @ino: inode number
  180. *
  181. * Description:
  182. * Search the for inode number @ino in the filesystem
  183. * described by @sbp. Use the specified inode table (@ilistp).
  184. * Returns the matching inode on success, else an error code.
  185. */
  186. static int
  187. __vxfs_iget(struct inode *ilistp, struct vxfs_inode_info *vip, ino_t ino)
  188. {
  189. struct page *pp;
  190. u_long offset;
  191. offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
  192. pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
  193. if (!IS_ERR(pp)) {
  194. struct vxfs_dinode *dip;
  195. caddr_t kaddr = (char *)page_address(pp);
  196. dip = (struct vxfs_dinode *)(kaddr + offset);
  197. dip2vip_cpy(VXFS_SBI(ilistp->i_sb), vip, dip);
  198. vip->vfs_inode.i_mapping->a_ops = &vxfs_aops;
  199. #ifdef DIAGNOSTIC
  200. vxfs_dumpi(vip, ino);
  201. #endif
  202. vxfs_put_page(pp);
  203. return 0;
  204. }
  205. printk(KERN_WARNING "vxfs: error on page 0x%p for inode %ld\n",
  206. pp, (unsigned long)ino);
  207. return PTR_ERR(pp);
  208. }
  209. /**
  210. * vxfs_stiget - find inode using the structural inode list
  211. * @sbp: VFS superblock
  212. * @ino: inode #
  213. *
  214. * Description:
  215. * Find inode @ino in the filesystem described by @sbp using
  216. * the structural inode list.
  217. * Returns the matching inode on success, else a NULL pointer.
  218. */
  219. struct inode *
  220. vxfs_stiget(struct super_block *sbp, ino_t ino)
  221. {
  222. struct inode *inode;
  223. int error;
  224. inode = new_inode(sbp);
  225. if (!inode)
  226. return NULL;
  227. inode->i_ino = get_next_ino();
  228. error = __vxfs_iget(VXFS_SBI(sbp)->vsi_stilist, VXFS_INO(inode), ino);
  229. if (error) {
  230. iput(inode);
  231. return NULL;
  232. }
  233. return inode;
  234. }
  235. /**
  236. * vxfs_iget - get an inode
  237. * @sbp: the superblock to get the inode for
  238. * @ino: the number of the inode to get
  239. *
  240. * Description:
  241. * vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
  242. * in all relevant fields in the new inode.
  243. */
  244. struct inode *
  245. vxfs_iget(struct super_block *sbp, ino_t ino)
  246. {
  247. struct vxfs_inode_info *vip;
  248. const struct address_space_operations *aops;
  249. struct inode *ip;
  250. int error;
  251. ip = iget_locked(sbp, ino);
  252. if (!ip)
  253. return ERR_PTR(-ENOMEM);
  254. if (!(ip->i_state & I_NEW))
  255. return ip;
  256. vip = VXFS_INO(ip);
  257. error = __vxfs_iget(VXFS_SBI(sbp)->vsi_ilist, vip, ino);
  258. if (error) {
  259. iget_failed(ip);
  260. return ERR_PTR(error);
  261. }
  262. if (VXFS_ISIMMED(vip))
  263. aops = &vxfs_immed_aops;
  264. else
  265. aops = &vxfs_aops;
  266. if (S_ISREG(ip->i_mode)) {
  267. ip->i_fop = &generic_ro_fops;
  268. ip->i_mapping->a_ops = aops;
  269. } else if (S_ISDIR(ip->i_mode)) {
  270. ip->i_op = &vxfs_dir_inode_ops;
  271. ip->i_fop = &vxfs_dir_operations;
  272. ip->i_mapping->a_ops = aops;
  273. } else if (S_ISLNK(ip->i_mode)) {
  274. if (!VXFS_ISIMMED(vip)) {
  275. ip->i_op = &page_symlink_inode_operations;
  276. inode_nohighmem(ip);
  277. ip->i_mapping->a_ops = &vxfs_aops;
  278. } else {
  279. ip->i_op = &simple_symlink_inode_operations;
  280. ip->i_link = vip->vii_immed.vi_immed;
  281. nd_terminate_link(ip->i_link, ip->i_size,
  282. sizeof(vip->vii_immed.vi_immed) - 1);
  283. }
  284. } else
  285. init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
  286. unlock_new_inode(ip);
  287. return ip;
  288. }
  289. /**
  290. * vxfs_evict_inode - remove inode from main memory
  291. * @ip: inode to discard.
  292. *
  293. * Description:
  294. * vxfs_evict_inode() is called on the final iput and frees the private
  295. * inode area.
  296. */
  297. void
  298. vxfs_evict_inode(struct inode *ip)
  299. {
  300. truncate_inode_pages_final(&ip->i_data);
  301. clear_inode(ip);
  302. }