xfs_trans_inode.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. xfs_inode_log_item_t *iip;
  86. /*
  87. * If the transaction pointer is NULL, just call the normal
  88. * xfs_iget().
  89. */
  90. if (tp == NULL)
  91. return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0);
  92. /*
  93. * If we find the inode in core with this transaction
  94. * pointer in its i_transp field, then we know we already
  95. * have it locked. In this case we just increment the lock
  96. * recursion count and return the inode to the caller.
  97. * Assert that the inode is already locked in the mode requested
  98. * by the caller. We cannot do lock promotions yet, so
  99. * die if someone gets this wrong.
  100. */
  101. if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) {
  102. /*
  103. * Make sure that the inode lock is held EXCL and
  104. * that the io lock is never upgraded when the inode
  105. * is already a part of the transaction.
  106. */
  107. ASSERT(ip->i_itemp != NULL);
  108. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  109. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  110. ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
  111. ismrlocked(&ip->i_iolock, MR_UPDATE));
  112. ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
  113. (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL));
  114. ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
  115. ismrlocked(&ip->i_iolock, (MR_UPDATE | MR_ACCESS)));
  116. ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
  117. (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY));
  118. if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
  119. ip->i_itemp->ili_iolock_recur++;
  120. }
  121. if (lock_flags & XFS_ILOCK_EXCL) {
  122. ip->i_itemp->ili_ilock_recur++;
  123. }
  124. *ipp = ip;
  125. return 0;
  126. }
  127. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  128. error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0);
  129. if (error) {
  130. return error;
  131. }
  132. ASSERT(ip != NULL);
  133. /*
  134. * Get a log_item_desc to point at the new item.
  135. */
  136. if (ip->i_itemp == NULL)
  137. xfs_inode_item_init(ip, mp);
  138. iip = ip->i_itemp;
  139. (void) xfs_trans_add_item(tp, (xfs_log_item_t *)(iip));
  140. xfs_trans_inode_broot_debug(ip);
  141. /*
  142. * If the IO lock has been acquired, mark that in
  143. * the inode log item so we'll know to unlock it
  144. * when the transaction commits.
  145. */
  146. ASSERT(iip->ili_flags == 0);
  147. if (lock_flags & XFS_IOLOCK_EXCL) {
  148. iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
  149. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  150. iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
  151. }
  152. /*
  153. * Initialize i_transp so we can find it with xfs_inode_incore()
  154. * above.
  155. */
  156. ip->i_transp = tp;
  157. *ipp = ip;
  158. return 0;
  159. }
  160. /*
  161. * Add the locked inode to the transaction.
  162. * The inode must be locked, and it cannot be associated with any
  163. * transaction. The caller must specify the locks already held
  164. * on the inode.
  165. */
  166. void
  167. xfs_trans_ijoin(
  168. xfs_trans_t *tp,
  169. xfs_inode_t *ip,
  170. uint lock_flags)
  171. {
  172. xfs_inode_log_item_t *iip;
  173. ASSERT(ip->i_transp == NULL);
  174. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  175. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  176. if (ip->i_itemp == NULL)
  177. xfs_inode_item_init(ip, ip->i_mount);
  178. iip = ip->i_itemp;
  179. ASSERT(iip->ili_flags == 0);
  180. ASSERT(iip->ili_ilock_recur == 0);
  181. ASSERT(iip->ili_iolock_recur == 0);
  182. /*
  183. * Get a log_item_desc to point at the new item.
  184. */
  185. (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip));
  186. xfs_trans_inode_broot_debug(ip);
  187. /*
  188. * If the IO lock is already held, mark that in the inode log item.
  189. */
  190. if (lock_flags & XFS_IOLOCK_EXCL) {
  191. iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
  192. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  193. iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
  194. }
  195. /*
  196. * Initialize i_transp so we can find it with xfs_inode_incore()
  197. * in xfs_trans_iget() above.
  198. */
  199. ip->i_transp = tp;
  200. }
  201. /*
  202. * Mark the inode as not needing to be unlocked when the inode item's
  203. * IOP_UNLOCK() routine is called. The inode must already be locked
  204. * and associated with the given transaction.
  205. */
  206. /*ARGSUSED*/
  207. void
  208. xfs_trans_ihold(
  209. xfs_trans_t *tp,
  210. xfs_inode_t *ip)
  211. {
  212. ASSERT(ip->i_transp == tp);
  213. ASSERT(ip->i_itemp != NULL);
  214. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  215. ip->i_itemp->ili_flags |= XFS_ILI_HOLD;
  216. }
  217. /*
  218. * This is called to mark the fields indicated in fieldmask as needing
  219. * to be logged when the transaction is committed. The inode must
  220. * already be associated with the given transaction.
  221. *
  222. * The values for fieldmask are defined in xfs_inode_item.h. We always
  223. * log all of the core inode if any of it has changed, and we always log
  224. * all of the inline data/extents/b-tree root if any of them has changed.
  225. */
  226. void
  227. xfs_trans_log_inode(
  228. xfs_trans_t *tp,
  229. xfs_inode_t *ip,
  230. uint flags)
  231. {
  232. xfs_log_item_desc_t *lidp;
  233. ASSERT(ip->i_transp == tp);
  234. ASSERT(ip->i_itemp != NULL);
  235. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  236. lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp));
  237. ASSERT(lidp != NULL);
  238. tp->t_flags |= XFS_TRANS_DIRTY;
  239. lidp->lid_flags |= XFS_LID_DIRTY;
  240. /*
  241. * Always OR in the bits from the ili_last_fields field.
  242. * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
  243. * routines in the eventual clearing of the ilf_fields bits.
  244. * See the big comment in xfs_iflush() for an explanation of
  245. * this coordination mechanism.
  246. */
  247. flags |= ip->i_itemp->ili_last_fields;
  248. ip->i_itemp->ili_format.ilf_fields |= flags;
  249. }
  250. #ifdef XFS_TRANS_DEBUG
  251. /*
  252. * Keep track of the state of the inode btree root to make sure we
  253. * log it properly.
  254. */
  255. STATIC void
  256. xfs_trans_inode_broot_debug(
  257. xfs_inode_t *ip)
  258. {
  259. xfs_inode_log_item_t *iip;
  260. ASSERT(ip->i_itemp != NULL);
  261. iip = ip->i_itemp;
  262. if (iip->ili_root_size != 0) {
  263. ASSERT(iip->ili_orig_root != NULL);
  264. kmem_free(iip->ili_orig_root, iip->ili_root_size);
  265. iip->ili_root_size = 0;
  266. iip->ili_orig_root = NULL;
  267. }
  268. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  269. ASSERT((ip->i_df.if_broot != NULL) &&
  270. (ip->i_df.if_broot_bytes > 0));
  271. iip->ili_root_size = ip->i_df.if_broot_bytes;
  272. iip->ili_orig_root =
  273. (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP);
  274. memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot),
  275. iip->ili_root_size);
  276. }
  277. }
  278. #endif