xfs_trans_inode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2000,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_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_trans_priv.h"
  40. #include "xfs_inode_item.h"
  41. #ifdef XFS_TRANS_DEBUG
  42. STATIC void
  43. xfs_trans_inode_broot_debug(
  44. xfs_inode_t *ip);
  45. #else
  46. #define xfs_trans_inode_broot_debug(ip)
  47. #endif
  48. /*
  49. * Get and lock the inode for the caller if it is not already
  50. * locked within the given transaction. If it is already locked
  51. * within the transaction, just increment its lock recursion count
  52. * and return a pointer to it.
  53. *
  54. * For an inode to be locked in a transaction, the inode lock, as
  55. * opposed to the io lock, must be taken exclusively. This ensures
  56. * that the inode can be involved in only 1 transaction at a time.
  57. * Lock recursion is handled on the io lock, but only for lock modes
  58. * of equal or lesser strength. That is, you can recur on the io lock
  59. * held EXCL with a SHARED request but not vice versa. Also, if
  60. * the inode is already a part of the transaction then you cannot
  61. * go from not holding the io lock to having it EXCL or SHARED.
  62. *
  63. * Use the inode cache routine xfs_inode_incore() to find the inode
  64. * if it is already owned by this transaction.
  65. *
  66. * If we don't already own the inode, use xfs_iget() to get it.
  67. * Since the inode log item structure is embedded in the incore
  68. * inode structure and is initialized when the inode is brought
  69. * into memory, there is nothing to do with it here.
  70. *
  71. * If the given transaction pointer is NULL, just call xfs_iget().
  72. * This simplifies code which must handle both cases.
  73. */
  74. int
  75. xfs_trans_iget(
  76. xfs_mount_t *mp,
  77. xfs_trans_t *tp,
  78. xfs_ino_t ino,
  79. uint flags,
  80. uint lock_flags,
  81. xfs_inode_t **ipp)
  82. {
  83. int error;
  84. xfs_inode_t *ip;
  85. /*
  86. * If the transaction pointer is NULL, just call the normal
  87. * xfs_iget().
  88. */
  89. if (tp == NULL)
  90. return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0);
  91. /*
  92. * If we find the inode in core with this transaction
  93. * pointer in its i_transp field, then we know we already
  94. * have it locked. In this case we just increment the lock
  95. * recursion count and return the inode to the caller.
  96. * Assert that the inode is already locked in the mode requested
  97. * by the caller. We cannot do lock promotions yet, so
  98. * die if someone gets this wrong.
  99. */
  100. if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) {
  101. /*
  102. * Make sure that the inode lock is held EXCL and
  103. * that the io lock is never upgraded when the inode
  104. * is already a part of the transaction.
  105. */
  106. ASSERT(ip->i_itemp != NULL);
  107. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  108. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  109. ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
  110. xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  111. ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
  112. (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL));
  113. ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
  114. xfs_isilocked(ip, XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED));
  115. ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
  116. (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY));
  117. if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
  118. ip->i_itemp->ili_iolock_recur++;
  119. }
  120. if (lock_flags & XFS_ILOCK_EXCL) {
  121. ip->i_itemp->ili_ilock_recur++;
  122. }
  123. *ipp = ip;
  124. return 0;
  125. }
  126. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  127. error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0);
  128. if (error) {
  129. return error;
  130. }
  131. ASSERT(ip != NULL);
  132. xfs_trans_ijoin(tp, ip, lock_flags);
  133. *ipp = ip;
  134. return 0;
  135. }
  136. /*
  137. * Add the locked inode to the transaction.
  138. * The inode must be locked, and it cannot be associated with any
  139. * transaction. The caller must specify the locks already held
  140. * on the inode.
  141. */
  142. void
  143. xfs_trans_ijoin(
  144. xfs_trans_t *tp,
  145. xfs_inode_t *ip,
  146. uint lock_flags)
  147. {
  148. xfs_inode_log_item_t *iip;
  149. ASSERT(ip->i_transp == NULL);
  150. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  151. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  152. if (ip->i_itemp == NULL)
  153. xfs_inode_item_init(ip, ip->i_mount);
  154. iip = ip->i_itemp;
  155. ASSERT(iip->ili_flags == 0);
  156. ASSERT(iip->ili_ilock_recur == 0);
  157. ASSERT(iip->ili_iolock_recur == 0);
  158. /*
  159. * Get a log_item_desc to point at the new item.
  160. */
  161. (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip));
  162. xfs_trans_inode_broot_debug(ip);
  163. /*
  164. * If the IO lock is already held, mark that in the inode log item.
  165. */
  166. if (lock_flags & XFS_IOLOCK_EXCL) {
  167. iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
  168. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  169. iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
  170. }
  171. /*
  172. * Initialize i_transp so we can find it with xfs_inode_incore()
  173. * in xfs_trans_iget() above.
  174. */
  175. ip->i_transp = tp;
  176. }
  177. /*
  178. * Mark the inode as not needing to be unlocked when the inode item's
  179. * IOP_UNLOCK() routine is called. The inode must already be locked
  180. * and associated with the given transaction.
  181. */
  182. /*ARGSUSED*/
  183. void
  184. xfs_trans_ihold(
  185. xfs_trans_t *tp,
  186. xfs_inode_t *ip)
  187. {
  188. ASSERT(ip->i_transp == tp);
  189. ASSERT(ip->i_itemp != NULL);
  190. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  191. ip->i_itemp->ili_flags |= XFS_ILI_HOLD;
  192. }
  193. /*
  194. * This is called to mark the fields indicated in fieldmask as needing
  195. * to be logged when the transaction is committed. The inode must
  196. * already be associated with the given transaction.
  197. *
  198. * The values for fieldmask are defined in xfs_inode_item.h. We always
  199. * log all of the core inode if any of it has changed, and we always log
  200. * all of the inline data/extents/b-tree root if any of them has changed.
  201. */
  202. void
  203. xfs_trans_log_inode(
  204. xfs_trans_t *tp,
  205. xfs_inode_t *ip,
  206. uint flags)
  207. {
  208. xfs_log_item_desc_t *lidp;
  209. ASSERT(ip->i_transp == tp);
  210. ASSERT(ip->i_itemp != NULL);
  211. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  212. lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp));
  213. ASSERT(lidp != NULL);
  214. tp->t_flags |= XFS_TRANS_DIRTY;
  215. lidp->lid_flags |= XFS_LID_DIRTY;
  216. /*
  217. * Always OR in the bits from the ili_last_fields field.
  218. * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
  219. * routines in the eventual clearing of the ilf_fields bits.
  220. * See the big comment in xfs_iflush() for an explanation of
  221. * this coordination mechanism.
  222. */
  223. flags |= ip->i_itemp->ili_last_fields;
  224. ip->i_itemp->ili_format.ilf_fields |= flags;
  225. }
  226. #ifdef XFS_TRANS_DEBUG
  227. /*
  228. * Keep track of the state of the inode btree root to make sure we
  229. * log it properly.
  230. */
  231. STATIC void
  232. xfs_trans_inode_broot_debug(
  233. xfs_inode_t *ip)
  234. {
  235. xfs_inode_log_item_t *iip;
  236. ASSERT(ip->i_itemp != NULL);
  237. iip = ip->i_itemp;
  238. if (iip->ili_root_size != 0) {
  239. ASSERT(iip->ili_orig_root != NULL);
  240. kmem_free(iip->ili_orig_root);
  241. iip->ili_root_size = 0;
  242. iip->ili_orig_root = NULL;
  243. }
  244. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  245. ASSERT((ip->i_df.if_broot != NULL) &&
  246. (ip->i_df.if_broot_bytes > 0));
  247. iip->ili_root_size = ip->i_df.if_broot_bytes;
  248. iip->ili_orig_root =
  249. (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP);
  250. memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot),
  251. iip->ili_root_size);
  252. }
  253. }
  254. #endif