xfs_vnode.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. return vp;
  92. }
  93. /*
  94. * Revalidate the Linux inode from the vattr.
  95. * Note: i_size _not_ updated; we must hold the inode
  96. * semaphore when doing that - callers responsibility.
  97. */
  98. void
  99. vn_revalidate_core(
  100. bhv_vnode_t *vp,
  101. bhv_vattr_t *vap)
  102. {
  103. struct inode *inode = vn_to_inode(vp);
  104. inode->i_mode = vap->va_mode;
  105. inode->i_nlink = vap->va_nlink;
  106. inode->i_uid = vap->va_uid;
  107. inode->i_gid = vap->va_gid;
  108. inode->i_blocks = vap->va_nblocks;
  109. inode->i_mtime = vap->va_mtime;
  110. inode->i_ctime = vap->va_ctime;
  111. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  112. inode->i_flags |= S_IMMUTABLE;
  113. else
  114. inode->i_flags &= ~S_IMMUTABLE;
  115. if (vap->va_xflags & XFS_XFLAG_APPEND)
  116. inode->i_flags |= S_APPEND;
  117. else
  118. inode->i_flags &= ~S_APPEND;
  119. if (vap->va_xflags & XFS_XFLAG_SYNC)
  120. inode->i_flags |= S_SYNC;
  121. else
  122. inode->i_flags &= ~S_SYNC;
  123. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  124. inode->i_flags |= S_NOATIME;
  125. else
  126. inode->i_flags &= ~S_NOATIME;
  127. }
  128. /*
  129. * Revalidate the Linux inode from the vnode.
  130. */
  131. int
  132. __vn_revalidate(
  133. bhv_vnode_t *vp,
  134. bhv_vattr_t *vattr)
  135. {
  136. int error;
  137. vn_trace_entry(xfs_vtoi(vp), __FUNCTION__, (inst_t *)__return_address);
  138. vattr->va_mask = XFS_AT_STAT | XFS_AT_XFLAGS;
  139. error = xfs_getattr(xfs_vtoi(vp), vattr, 0);
  140. if (likely(!error)) {
  141. vn_revalidate_core(vp, vattr);
  142. xfs_iflags_clear(xfs_vtoi(vp), XFS_IMODIFIED);
  143. }
  144. return -error;
  145. }
  146. int
  147. vn_revalidate(
  148. bhv_vnode_t *vp)
  149. {
  150. bhv_vattr_t vattr;
  151. return __vn_revalidate(vp, &vattr);
  152. }
  153. /*
  154. * Add a reference to a referenced vnode.
  155. */
  156. bhv_vnode_t *
  157. vn_hold(
  158. bhv_vnode_t *vp)
  159. {
  160. struct inode *inode;
  161. XFS_STATS_INC(vn_hold);
  162. inode = igrab(vn_to_inode(vp));
  163. ASSERT(inode);
  164. return vp;
  165. }
  166. #ifdef XFS_VNODE_TRACE
  167. /*
  168. * Reference count of Linux inode if present, -1 if the xfs_inode
  169. * has no associated Linux inode.
  170. */
  171. static inline int xfs_icount(struct xfs_inode *ip)
  172. {
  173. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  174. if (vp)
  175. return vn_count(vp);
  176. return -1;
  177. }
  178. #define KTRACE_ENTER(ip, vk, s, line, ra) \
  179. ktrace_enter( (ip)->i_trace, \
  180. /* 0 */ (void *)(__psint_t)(vk), \
  181. /* 1 */ (void *)(s), \
  182. /* 2 */ (void *)(__psint_t) line, \
  183. /* 3 */ (void *)(__psint_t)xfs_icount(ip), \
  184. /* 4 */ (void *)(ra), \
  185. /* 5 */ NULL, \
  186. /* 6 */ (void *)(__psint_t)current_cpu(), \
  187. /* 7 */ (void *)(__psint_t)current_pid(), \
  188. /* 8 */ (void *)__return_address, \
  189. /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  190. /*
  191. * Vnode tracing code.
  192. */
  193. void
  194. vn_trace_entry(xfs_inode_t *ip, const char *func, inst_t *ra)
  195. {
  196. KTRACE_ENTER(ip, VNODE_KTRACE_ENTRY, func, 0, ra);
  197. }
  198. void
  199. vn_trace_exit(xfs_inode_t *ip, const char *func, inst_t *ra)
  200. {
  201. KTRACE_ENTER(ip, VNODE_KTRACE_EXIT, func, 0, ra);
  202. }
  203. void
  204. vn_trace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  205. {
  206. KTRACE_ENTER(ip, VNODE_KTRACE_HOLD, file, line, ra);
  207. }
  208. void
  209. vn_trace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  210. {
  211. KTRACE_ENTER(ip, VNODE_KTRACE_REF, file, line, ra);
  212. }
  213. void
  214. vn_trace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra)
  215. {
  216. KTRACE_ENTER(ip, VNODE_KTRACE_RELE, file, line, ra);
  217. }
  218. #endif /* XFS_VNODE_TRACE */