xfs_trans_inode.c 8.6 KB

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