xfs_trans_inode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_macros.h"
  34. #include "xfs_types.h"
  35. #include "xfs_inum.h"
  36. #include "xfs_log.h"
  37. #include "xfs_trans.h"
  38. #include "xfs_sb.h"
  39. #include "xfs_ag.h"
  40. #include "xfs_dir.h"
  41. #include "xfs_dir2.h"
  42. #include "xfs_dmapi.h"
  43. #include "xfs_mount.h"
  44. #include "xfs_trans_priv.h"
  45. #include "xfs_alloc_btree.h"
  46. #include "xfs_bmap_btree.h"
  47. #include "xfs_ialloc_btree.h"
  48. #include "xfs_btree.h"
  49. #include "xfs_ialloc.h"
  50. #include "xfs_attr_sf.h"
  51. #include "xfs_dir_sf.h"
  52. #include "xfs_dir2_sf.h"
  53. #include "xfs_dinode.h"
  54. #include "xfs_inode_item.h"
  55. #include "xfs_inode.h"
  56. #ifdef XFS_TRANS_DEBUG
  57. STATIC void
  58. xfs_trans_inode_broot_debug(
  59. xfs_inode_t *ip);
  60. #else
  61. #define xfs_trans_inode_broot_debug(ip)
  62. #endif
  63. /*
  64. * Get and lock the inode for the caller if it is not already
  65. * locked within the given transaction. If it is already locked
  66. * within the transaction, just increment its lock recursion count
  67. * and return a pointer to it.
  68. *
  69. * For an inode to be locked in a transaction, the inode lock, as
  70. * opposed to the io lock, must be taken exclusively. This ensures
  71. * that the inode can be involved in only 1 transaction at a time.
  72. * Lock recursion is handled on the io lock, but only for lock modes
  73. * of equal or lesser strength. That is, you can recur on the io lock
  74. * held EXCL with a SHARED request but not vice versa. Also, if
  75. * the inode is already a part of the transaction then you cannot
  76. * go from not holding the io lock to having it EXCL or SHARED.
  77. *
  78. * Use the inode cache routine xfs_inode_incore() to find the inode
  79. * if it is already owned by this transaction.
  80. *
  81. * If we don't already own the inode, use xfs_iget() to get it.
  82. * Since the inode log item structure is embedded in the incore
  83. * inode structure and is initialized when the inode is brought
  84. * into memory, there is nothing to do with it here.
  85. *
  86. * If the given transaction pointer is NULL, just call xfs_iget().
  87. * This simplifies code which must handle both cases.
  88. */
  89. int
  90. xfs_trans_iget(
  91. xfs_mount_t *mp,
  92. xfs_trans_t *tp,
  93. xfs_ino_t ino,
  94. uint flags,
  95. uint lock_flags,
  96. xfs_inode_t **ipp)
  97. {
  98. int error;
  99. xfs_inode_t *ip;
  100. xfs_inode_log_item_t *iip;
  101. /*
  102. * If the transaction pointer is NULL, just call the normal
  103. * xfs_iget().
  104. */
  105. if (tp == NULL)
  106. return xfs_iget(mp, NULL, ino, flags, lock_flags, ipp, 0);
  107. /*
  108. * If we find the inode in core with this transaction
  109. * pointer in its i_transp field, then we know we already
  110. * have it locked. In this case we just increment the lock
  111. * recursion count and return the inode to the caller.
  112. * Assert that the inode is already locked in the mode requested
  113. * by the caller. We cannot do lock promotions yet, so
  114. * die if someone gets this wrong.
  115. */
  116. if ((ip = xfs_inode_incore(tp->t_mountp, ino, tp)) != NULL) {
  117. /*
  118. * Make sure that the inode lock is held EXCL and
  119. * that the io lock is never upgraded when the inode
  120. * is already a part of the transaction.
  121. */
  122. ASSERT(ip->i_itemp != NULL);
  123. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  124. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  125. ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
  126. ismrlocked(&ip->i_iolock, MR_UPDATE));
  127. ASSERT((!(lock_flags & XFS_IOLOCK_EXCL)) ||
  128. (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_EXCL));
  129. ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
  130. ismrlocked(&ip->i_iolock, (MR_UPDATE | MR_ACCESS)));
  131. ASSERT((!(lock_flags & XFS_IOLOCK_SHARED)) ||
  132. (ip->i_itemp->ili_flags & XFS_ILI_IOLOCKED_ANY));
  133. if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
  134. ip->i_itemp->ili_iolock_recur++;
  135. }
  136. if (lock_flags & XFS_ILOCK_EXCL) {
  137. ip->i_itemp->ili_ilock_recur++;
  138. }
  139. *ipp = ip;
  140. return 0;
  141. }
  142. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  143. error = xfs_iget(tp->t_mountp, tp, ino, flags, lock_flags, &ip, 0);
  144. if (error) {
  145. return error;
  146. }
  147. ASSERT(ip != NULL);
  148. /*
  149. * Get a log_item_desc to point at the new item.
  150. */
  151. if (ip->i_itemp == NULL)
  152. xfs_inode_item_init(ip, mp);
  153. iip = ip->i_itemp;
  154. (void) xfs_trans_add_item(tp, (xfs_log_item_t *)(iip));
  155. xfs_trans_inode_broot_debug(ip);
  156. /*
  157. * If the IO lock has been acquired, mark that in
  158. * the inode log item so we'll know to unlock it
  159. * when the transaction commits.
  160. */
  161. ASSERT(iip->ili_flags == 0);
  162. if (lock_flags & XFS_IOLOCK_EXCL) {
  163. iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
  164. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  165. iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
  166. }
  167. /*
  168. * Initialize i_transp so we can find it with xfs_inode_incore()
  169. * above.
  170. */
  171. ip->i_transp = tp;
  172. *ipp = ip;
  173. return 0;
  174. }
  175. /*
  176. * Add the locked inode to the transaction.
  177. * The inode must be locked, and it cannot be associated with any
  178. * transaction. The caller must specify the locks already held
  179. * on the inode.
  180. */
  181. void
  182. xfs_trans_ijoin(
  183. xfs_trans_t *tp,
  184. xfs_inode_t *ip,
  185. uint lock_flags)
  186. {
  187. xfs_inode_log_item_t *iip;
  188. ASSERT(ip->i_transp == NULL);
  189. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  190. ASSERT(lock_flags & XFS_ILOCK_EXCL);
  191. if (ip->i_itemp == NULL)
  192. xfs_inode_item_init(ip, ip->i_mount);
  193. iip = ip->i_itemp;
  194. ASSERT(iip->ili_flags == 0);
  195. ASSERT(iip->ili_ilock_recur == 0);
  196. ASSERT(iip->ili_iolock_recur == 0);
  197. /*
  198. * Get a log_item_desc to point at the new item.
  199. */
  200. (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(iip));
  201. xfs_trans_inode_broot_debug(ip);
  202. /*
  203. * If the IO lock is already held, mark that in the inode log item.
  204. */
  205. if (lock_flags & XFS_IOLOCK_EXCL) {
  206. iip->ili_flags |= XFS_ILI_IOLOCKED_EXCL;
  207. } else if (lock_flags & XFS_IOLOCK_SHARED) {
  208. iip->ili_flags |= XFS_ILI_IOLOCKED_SHARED;
  209. }
  210. /*
  211. * Initialize i_transp so we can find it with xfs_inode_incore()
  212. * in xfs_trans_iget() above.
  213. */
  214. ip->i_transp = tp;
  215. }
  216. /*
  217. * Mark the inode as not needing to be unlocked when the inode item's
  218. * IOP_UNLOCK() routine is called. The inode must already be locked
  219. * and associated with the given transaction.
  220. */
  221. /*ARGSUSED*/
  222. void
  223. xfs_trans_ihold(
  224. xfs_trans_t *tp,
  225. xfs_inode_t *ip)
  226. {
  227. ASSERT(ip->i_transp == tp);
  228. ASSERT(ip->i_itemp != NULL);
  229. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  230. ip->i_itemp->ili_flags |= XFS_ILI_HOLD;
  231. }
  232. /*
  233. * This is called to mark the fields indicated in fieldmask as needing
  234. * to be logged when the transaction is committed. The inode must
  235. * already be associated with the given transaction.
  236. *
  237. * The values for fieldmask are defined in xfs_inode_item.h. We always
  238. * log all of the core inode if any of it has changed, and we always log
  239. * all of the inline data/extents/b-tree root if any of them has changed.
  240. */
  241. void
  242. xfs_trans_log_inode(
  243. xfs_trans_t *tp,
  244. xfs_inode_t *ip,
  245. uint flags)
  246. {
  247. xfs_log_item_desc_t *lidp;
  248. ASSERT(ip->i_transp == tp);
  249. ASSERT(ip->i_itemp != NULL);
  250. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  251. lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(ip->i_itemp));
  252. ASSERT(lidp != NULL);
  253. tp->t_flags |= XFS_TRANS_DIRTY;
  254. lidp->lid_flags |= XFS_LID_DIRTY;
  255. /*
  256. * Always OR in the bits from the ili_last_fields field.
  257. * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
  258. * routines in the eventual clearing of the ilf_fields bits.
  259. * See the big comment in xfs_iflush() for an explanation of
  260. * this coorination mechanism.
  261. */
  262. flags |= ip->i_itemp->ili_last_fields;
  263. ip->i_itemp->ili_format.ilf_fields |= flags;
  264. }
  265. #ifdef XFS_TRANS_DEBUG
  266. /*
  267. * Keep track of the state of the inode btree root to make sure we
  268. * log it properly.
  269. */
  270. STATIC void
  271. xfs_trans_inode_broot_debug(
  272. xfs_inode_t *ip)
  273. {
  274. xfs_inode_log_item_t *iip;
  275. ASSERT(ip->i_itemp != NULL);
  276. iip = ip->i_itemp;
  277. if (iip->ili_root_size != 0) {
  278. ASSERT(iip->ili_orig_root != NULL);
  279. kmem_free(iip->ili_orig_root, iip->ili_root_size);
  280. iip->ili_root_size = 0;
  281. iip->ili_orig_root = NULL;
  282. }
  283. if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
  284. ASSERT((ip->i_df.if_broot != NULL) &&
  285. (ip->i_df.if_broot_bytes > 0));
  286. iip->ili_root_size = ip->i_df.if_broot_bytes;
  287. iip->ili_orig_root =
  288. (char*)kmem_alloc(iip->ili_root_size, KM_SLEEP);
  289. memcpy(iip->ili_orig_root, (char*)(ip->i_df.if_broot),
  290. iip->ili_root_size);
  291. }
  292. }
  293. #endif