vxfs_inode.c 8.8 KB

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