xfs_vnode.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_vnodeops.h"
  20. #include "xfs_bmap_btree.h"
  21. #include "xfs_inode.h"
  22. /*
  23. * And this gunk is needed for xfs_mount.h"
  24. */
  25. #include "xfs_log.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_sb.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_inum.h"
  30. #include "xfs_ag.h"
  31. #include "xfs_mount.h"
  32. uint64_t vn_generation; /* vnode generation number */
  33. DEFINE_SPINLOCK(vnumber_lock);
  34. /*
  35. * Dedicated vnode inactive/reclaim sync semaphores.
  36. * Prime number of hash buckets since address is used as the key.
  37. */
  38. #define NVSYNC 37
  39. #define vptosync(v) (&vsync[((unsigned long)v) % NVSYNC])
  40. static wait_queue_head_t vsync[NVSYNC];
  41. void
  42. vn_init(void)
  43. {
  44. int i;
  45. for (i = 0; i < NVSYNC; i++)
  46. init_waitqueue_head(&vsync[i]);
  47. }
  48. void
  49. vn_iowait(
  50. xfs_inode_t *ip)
  51. {
  52. wait_queue_head_t *wq = vptosync(ip);
  53. wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
  54. }
  55. void
  56. vn_iowake(
  57. xfs_inode_t *ip)
  58. {
  59. if (atomic_dec_and_test(&ip->i_iocount))
  60. wake_up(vptosync(ip));
  61. }
  62. /*
  63. * Volume managers supporting multiple paths can send back ENODEV when the
  64. * final path disappears. In this case continuing to fill the page cache
  65. * with dirty data which cannot be written out is evil, so prevent that.
  66. */
  67. void
  68. vn_ioerror(
  69. xfs_inode_t *ip,
  70. int error,
  71. char *f,
  72. int l)
  73. {
  74. bhv_vfs_t *vfsp = XFS_MTOVFS(ip->i_mount);
  75. if (unlikely(error == -ENODEV))
  76. bhv_vfs_force_shutdown(vfsp, SHUTDOWN_DEVICE_REQ, f, l);
  77. }
  78. bhv_vnode_t *
  79. vn_initialize(
  80. struct inode *inode)
  81. {
  82. bhv_vnode_t *vp = vn_from_inode(inode);
  83. XFS_STATS_INC(vn_active);
  84. XFS_STATS_INC(vn_alloc);
  85. spin_lock(&vnumber_lock);
  86. if (!++vn_generation) /* v_number shouldn't be zero */
  87. vn_generation++;
  88. vp->v_number = vn_generation;
  89. spin_unlock(&vnumber_lock);
  90. ASSERT(VN_CACHED(vp) == 0);
  91. #ifdef XFS_VNODE_TRACE
  92. vp->v_trace = ktrace_alloc(VNODE_TRACE_SIZE, KM_SLEEP);
  93. #endif /* XFS_VNODE_TRACE */
  94. vn_trace_exit(vp, __FUNCTION__, (inst_t *)__return_address);
  95. return vp;
  96. }
  97. /*
  98. * Revalidate the Linux inode from the vattr.
  99. * Note: i_size _not_ updated; we must hold the inode
  100. * semaphore when doing that - callers responsibility.
  101. */
  102. void
  103. vn_revalidate_core(
  104. bhv_vnode_t *vp,
  105. bhv_vattr_t *vap)
  106. {
  107. struct inode *inode = vn_to_inode(vp);
  108. inode->i_mode = vap->va_mode;
  109. inode->i_nlink = vap->va_nlink;
  110. inode->i_uid = vap->va_uid;
  111. inode->i_gid = vap->va_gid;
  112. inode->i_blocks = vap->va_nblocks;
  113. inode->i_mtime = vap->va_mtime;
  114. inode->i_ctime = vap->va_ctime;
  115. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  116. inode->i_flags |= S_IMMUTABLE;
  117. else
  118. inode->i_flags &= ~S_IMMUTABLE;
  119. if (vap->va_xflags & XFS_XFLAG_APPEND)
  120. inode->i_flags |= S_APPEND;
  121. else
  122. inode->i_flags &= ~S_APPEND;
  123. if (vap->va_xflags & XFS_XFLAG_SYNC)
  124. inode->i_flags |= S_SYNC;
  125. else
  126. inode->i_flags &= ~S_SYNC;
  127. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  128. inode->i_flags |= S_NOATIME;
  129. else
  130. inode->i_flags &= ~S_NOATIME;
  131. }
  132. /*
  133. * Revalidate the Linux inode from the vnode.
  134. */
  135. int
  136. __vn_revalidate(
  137. bhv_vnode_t *vp,
  138. bhv_vattr_t *vattr)
  139. {
  140. int error;
  141. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  142. vattr->va_mask = XFS_AT_STAT | XFS_AT_XFLAGS;
  143. error = xfs_getattr(xfs_vtoi(vp), vattr, 0);
  144. if (likely(!error)) {
  145. vn_revalidate_core(vp, vattr);
  146. xfs_iflags_clear(xfs_vtoi(vp), XFS_IMODIFIED);
  147. }
  148. return -error;
  149. }
  150. int
  151. vn_revalidate(
  152. bhv_vnode_t *vp)
  153. {
  154. bhv_vattr_t vattr;
  155. return __vn_revalidate(vp, &vattr);
  156. }
  157. /*
  158. * Add a reference to a referenced vnode.
  159. */
  160. bhv_vnode_t *
  161. vn_hold(
  162. bhv_vnode_t *vp)
  163. {
  164. struct inode *inode;
  165. XFS_STATS_INC(vn_hold);
  166. inode = igrab(vn_to_inode(vp));
  167. ASSERT(inode);
  168. return vp;
  169. }
  170. #ifdef XFS_VNODE_TRACE
  171. #define KTRACE_ENTER(vp, vk, s, line, ra) \
  172. ktrace_enter( (vp)->v_trace, \
  173. /* 0 */ (void *)(__psint_t)(vk), \
  174. /* 1 */ (void *)(s), \
  175. /* 2 */ (void *)(__psint_t) line, \
  176. /* 3 */ (void *)(__psint_t)(vn_count(vp)), \
  177. /* 4 */ (void *)(ra), \
  178. /* 5 */ NULL, \
  179. /* 6 */ (void *)(__psint_t)current_cpu(), \
  180. /* 7 */ (void *)(__psint_t)current_pid(), \
  181. /* 8 */ (void *)__return_address, \
  182. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  183. /*
  184. * Vnode tracing code.
  185. */
  186. void
  187. vn_trace_entry(bhv_vnode_t *vp, const char *func, inst_t *ra)
  188. {
  189. KTRACE_ENTER(vp, VNODE_KTRACE_ENTRY, func, 0, ra);
  190. }
  191. void
  192. vn_trace_exit(bhv_vnode_t *vp, const char *func, inst_t *ra)
  193. {
  194. KTRACE_ENTER(vp, VNODE_KTRACE_EXIT, func, 0, ra);
  195. }
  196. void
  197. vn_trace_hold(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  198. {
  199. KTRACE_ENTER(vp, VNODE_KTRACE_HOLD, file, line, ra);
  200. }
  201. void
  202. vn_trace_ref(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  203. {
  204. KTRACE_ENTER(vp, VNODE_KTRACE_REF, file, line, ra);
  205. }
  206. void
  207. vn_trace_rele(bhv_vnode_t *vp, char *file, int line, inst_t *ra)
  208. {
  209. KTRACE_ENTER(vp, VNODE_KTRACE_RELE, file, line, ra);
  210. }
  211. #endif /* XFS_VNODE_TRACE */