xfs_trans.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /*
  2. * Copyright (c) 2000-2003 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_fs.h"
  34. #include "xfs_types.h"
  35. #include "xfs_bit.h"
  36. #include "xfs_log.h"
  37. #include "xfs_inum.h"
  38. #include "xfs_trans.h"
  39. #include "xfs_sb.h"
  40. #include "xfs_ag.h"
  41. #include "xfs_dir.h"
  42. #include "xfs_dir2.h"
  43. #include "xfs_dmapi.h"
  44. #include "xfs_mount.h"
  45. #include "xfs_error.h"
  46. #include "xfs_da_btree.h"
  47. #include "xfs_bmap_btree.h"
  48. #include "xfs_alloc_btree.h"
  49. #include "xfs_ialloc_btree.h"
  50. #include "xfs_dir_sf.h"
  51. #include "xfs_dir2_sf.h"
  52. #include "xfs_attr_sf.h"
  53. #include "xfs_dinode.h"
  54. #include "xfs_inode.h"
  55. #include "xfs_btree.h"
  56. #include "xfs_ialloc.h"
  57. #include "xfs_alloc.h"
  58. #include "xfs_bmap.h"
  59. #include "xfs_quota.h"
  60. #include "xfs_trans_priv.h"
  61. #include "xfs_trans_space.h"
  62. STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *);
  63. STATIC uint xfs_trans_count_vecs(xfs_trans_t *);
  64. STATIC void xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *);
  65. STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
  66. STATIC void xfs_trans_committed(xfs_trans_t *, int);
  67. STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
  68. STATIC void xfs_trans_free(xfs_trans_t *);
  69. kmem_zone_t *xfs_trans_zone;
  70. /*
  71. * Initialize the precomputed transaction reservation values
  72. * in the mount structure.
  73. */
  74. void
  75. xfs_trans_init(
  76. xfs_mount_t *mp)
  77. {
  78. xfs_trans_reservations_t *resp;
  79. resp = &(mp->m_reservations);
  80. resp->tr_write =
  81. (uint)(XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  82. resp->tr_itruncate =
  83. (uint)(XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  84. resp->tr_rename =
  85. (uint)(XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  86. resp->tr_link = (uint)XFS_CALC_LINK_LOG_RES(mp);
  87. resp->tr_remove =
  88. (uint)(XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  89. resp->tr_symlink =
  90. (uint)(XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  91. resp->tr_create =
  92. (uint)(XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  93. resp->tr_mkdir =
  94. (uint)(XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  95. resp->tr_ifree =
  96. (uint)(XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  97. resp->tr_ichange =
  98. (uint)(XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  99. resp->tr_growdata = (uint)XFS_CALC_GROWDATA_LOG_RES(mp);
  100. resp->tr_swrite = (uint)XFS_CALC_SWRITE_LOG_RES(mp);
  101. resp->tr_writeid = (uint)XFS_CALC_WRITEID_LOG_RES(mp);
  102. resp->tr_addafork =
  103. (uint)(XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  104. resp->tr_attrinval = (uint)XFS_CALC_ATTRINVAL_LOG_RES(mp);
  105. resp->tr_attrset =
  106. (uint)(XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  107. resp->tr_attrrm =
  108. (uint)(XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp));
  109. resp->tr_clearagi = (uint)XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
  110. resp->tr_growrtalloc = (uint)XFS_CALC_GROWRTALLOC_LOG_RES(mp);
  111. resp->tr_growrtzero = (uint)XFS_CALC_GROWRTZERO_LOG_RES(mp);
  112. resp->tr_growrtfree = (uint)XFS_CALC_GROWRTFREE_LOG_RES(mp);
  113. }
  114. /*
  115. * This routine is called to allocate a transaction structure.
  116. * The type parameter indicates the type of the transaction. These
  117. * are enumerated in xfs_trans.h.
  118. *
  119. * Dynamically allocate the transaction structure from the transaction
  120. * zone, initialize it, and return it to the caller.
  121. */
  122. xfs_trans_t *
  123. xfs_trans_alloc(
  124. xfs_mount_t *mp,
  125. uint type)
  126. {
  127. fs_check_frozen(XFS_MTOVFS(mp), SB_FREEZE_TRANS);
  128. atomic_inc(&mp->m_active_trans);
  129. return (_xfs_trans_alloc(mp, type));
  130. }
  131. xfs_trans_t *
  132. _xfs_trans_alloc(
  133. xfs_mount_t *mp,
  134. uint type)
  135. {
  136. xfs_trans_t *tp;
  137. ASSERT(xfs_trans_zone != NULL);
  138. tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  139. /*
  140. * Initialize the transaction structure.
  141. */
  142. tp->t_magic = XFS_TRANS_MAGIC;
  143. tp->t_type = type;
  144. tp->t_mountp = mp;
  145. tp->t_items_free = XFS_LIC_NUM_SLOTS;
  146. tp->t_busy_free = XFS_LBC_NUM_SLOTS;
  147. XFS_LIC_INIT(&(tp->t_items));
  148. XFS_LBC_INIT(&(tp->t_busy));
  149. return (tp);
  150. }
  151. /*
  152. * This is called to create a new transaction which will share the
  153. * permanent log reservation of the given transaction. The remaining
  154. * unused block and rt extent reservations are also inherited. This
  155. * implies that the original transaction is no longer allowed to allocate
  156. * blocks. Locks and log items, however, are no inherited. They must
  157. * be added to the new transaction explicitly.
  158. */
  159. xfs_trans_t *
  160. xfs_trans_dup(
  161. xfs_trans_t *tp)
  162. {
  163. xfs_trans_t *ntp;
  164. ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  165. /*
  166. * Initialize the new transaction structure.
  167. */
  168. ntp->t_magic = XFS_TRANS_MAGIC;
  169. ntp->t_type = tp->t_type;
  170. ntp->t_mountp = tp->t_mountp;
  171. ntp->t_items_free = XFS_LIC_NUM_SLOTS;
  172. ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
  173. XFS_LIC_INIT(&(ntp->t_items));
  174. XFS_LBC_INIT(&(ntp->t_busy));
  175. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  176. #if defined(XLOG_NOLOG) || defined(DEBUG)
  177. ASSERT(!xlog_debug || tp->t_ticket != NULL);
  178. #else
  179. ASSERT(tp->t_ticket != NULL);
  180. #endif
  181. ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
  182. ntp->t_ticket = tp->t_ticket;
  183. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  184. tp->t_blk_res = tp->t_blk_res_used;
  185. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  186. tp->t_rtx_res = tp->t_rtx_res_used;
  187. PFLAGS_DUP(&tp->t_pflags, &ntp->t_pflags);
  188. XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp);
  189. atomic_inc(&tp->t_mountp->m_active_trans);
  190. return ntp;
  191. }
  192. /*
  193. * This is called to reserve free disk blocks and log space for the
  194. * given transaction. This must be done before allocating any resources
  195. * within the transaction.
  196. *
  197. * This will return ENOSPC if there are not enough blocks available.
  198. * It will sleep waiting for available log space.
  199. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  200. * is used by long running transactions. If any one of the reservations
  201. * fails then they will all be backed out.
  202. *
  203. * This does not do quota reservations. That typically is done by the
  204. * caller afterwards.
  205. */
  206. int
  207. xfs_trans_reserve(
  208. xfs_trans_t *tp,
  209. uint blocks,
  210. uint logspace,
  211. uint rtextents,
  212. uint flags,
  213. uint logcount)
  214. {
  215. int log_flags;
  216. int error;
  217. int rsvd;
  218. error = 0;
  219. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  220. /* Mark this thread as being in a transaction */
  221. PFLAGS_SET_FSTRANS(&tp->t_pflags);
  222. /*
  223. * Attempt to reserve the needed disk blocks by decrementing
  224. * the number needed from the number available. This will
  225. * fail if the count would go below zero.
  226. */
  227. if (blocks > 0) {
  228. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
  229. -blocks, rsvd);
  230. if (error != 0) {
  231. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  232. return (XFS_ERROR(ENOSPC));
  233. }
  234. tp->t_blk_res += blocks;
  235. }
  236. /*
  237. * Reserve the log space needed for this transaction.
  238. */
  239. if (logspace > 0) {
  240. ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
  241. ASSERT((tp->t_log_count == 0) ||
  242. (tp->t_log_count == logcount));
  243. if (flags & XFS_TRANS_PERM_LOG_RES) {
  244. log_flags = XFS_LOG_PERM_RESERV;
  245. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  246. } else {
  247. ASSERT(tp->t_ticket == NULL);
  248. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  249. log_flags = 0;
  250. }
  251. error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
  252. &tp->t_ticket,
  253. XFS_TRANSACTION, log_flags, tp->t_type);
  254. if (error) {
  255. goto undo_blocks;
  256. }
  257. tp->t_log_res = logspace;
  258. tp->t_log_count = logcount;
  259. }
  260. /*
  261. * Attempt to reserve the needed realtime extents by decrementing
  262. * the number needed from the number available. This will
  263. * fail if the count would go below zero.
  264. */
  265. if (rtextents > 0) {
  266. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
  267. -rtextents, rsvd);
  268. if (error) {
  269. error = XFS_ERROR(ENOSPC);
  270. goto undo_log;
  271. }
  272. tp->t_rtx_res += rtextents;
  273. }
  274. return 0;
  275. /*
  276. * Error cases jump to one of these labels to undo any
  277. * reservations which have already been performed.
  278. */
  279. undo_log:
  280. if (logspace > 0) {
  281. if (flags & XFS_TRANS_PERM_LOG_RES) {
  282. log_flags = XFS_LOG_REL_PERM_RESERV;
  283. } else {
  284. log_flags = 0;
  285. }
  286. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  287. tp->t_ticket = NULL;
  288. tp->t_log_res = 0;
  289. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  290. }
  291. undo_blocks:
  292. if (blocks > 0) {
  293. (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
  294. blocks, rsvd);
  295. tp->t_blk_res = 0;
  296. }
  297. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  298. return (error);
  299. }
  300. /*
  301. * Record the indicated change to the given field for application
  302. * to the file system's superblock when the transaction commits.
  303. * For now, just store the change in the transaction structure.
  304. *
  305. * Mark the transaction structure to indicate that the superblock
  306. * needs to be updated before committing.
  307. */
  308. void
  309. xfs_trans_mod_sb(
  310. xfs_trans_t *tp,
  311. uint field,
  312. long delta)
  313. {
  314. switch (field) {
  315. case XFS_TRANS_SB_ICOUNT:
  316. tp->t_icount_delta += delta;
  317. break;
  318. case XFS_TRANS_SB_IFREE:
  319. tp->t_ifree_delta += delta;
  320. break;
  321. case XFS_TRANS_SB_FDBLOCKS:
  322. /*
  323. * Track the number of blocks allocated in the
  324. * transaction. Make sure it does not exceed the
  325. * number reserved.
  326. */
  327. if (delta < 0) {
  328. tp->t_blk_res_used += (uint)-delta;
  329. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  330. }
  331. tp->t_fdblocks_delta += delta;
  332. break;
  333. case XFS_TRANS_SB_RES_FDBLOCKS:
  334. /*
  335. * The allocation has already been applied to the
  336. * in-core superblock's counter. This should only
  337. * be applied to the on-disk superblock.
  338. */
  339. ASSERT(delta < 0);
  340. tp->t_res_fdblocks_delta += delta;
  341. break;
  342. case XFS_TRANS_SB_FREXTENTS:
  343. /*
  344. * Track the number of blocks allocated in the
  345. * transaction. Make sure it does not exceed the
  346. * number reserved.
  347. */
  348. if (delta < 0) {
  349. tp->t_rtx_res_used += (uint)-delta;
  350. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  351. }
  352. tp->t_frextents_delta += delta;
  353. break;
  354. case XFS_TRANS_SB_RES_FREXTENTS:
  355. /*
  356. * The allocation has already been applied to the
  357. * in-core superblocks's counter. This should only
  358. * be applied to the on-disk superblock.
  359. */
  360. ASSERT(delta < 0);
  361. tp->t_res_frextents_delta += delta;
  362. break;
  363. case XFS_TRANS_SB_DBLOCKS:
  364. ASSERT(delta > 0);
  365. tp->t_dblocks_delta += delta;
  366. break;
  367. case XFS_TRANS_SB_AGCOUNT:
  368. ASSERT(delta > 0);
  369. tp->t_agcount_delta += delta;
  370. break;
  371. case XFS_TRANS_SB_IMAXPCT:
  372. tp->t_imaxpct_delta += delta;
  373. break;
  374. case XFS_TRANS_SB_REXTSIZE:
  375. tp->t_rextsize_delta += delta;
  376. break;
  377. case XFS_TRANS_SB_RBMBLOCKS:
  378. tp->t_rbmblocks_delta += delta;
  379. break;
  380. case XFS_TRANS_SB_RBLOCKS:
  381. tp->t_rblocks_delta += delta;
  382. break;
  383. case XFS_TRANS_SB_REXTENTS:
  384. tp->t_rextents_delta += delta;
  385. break;
  386. case XFS_TRANS_SB_REXTSLOG:
  387. tp->t_rextslog_delta += delta;
  388. break;
  389. default:
  390. ASSERT(0);
  391. return;
  392. }
  393. tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY);
  394. }
  395. /*
  396. * xfs_trans_apply_sb_deltas() is called from the commit code
  397. * to bring the superblock buffer into the current transaction
  398. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  399. *
  400. * For now we just look at each field allowed to change and change
  401. * it if necessary.
  402. */
  403. STATIC void
  404. xfs_trans_apply_sb_deltas(
  405. xfs_trans_t *tp)
  406. {
  407. xfs_sb_t *sbp;
  408. xfs_buf_t *bp;
  409. int whole = 0;
  410. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  411. sbp = XFS_BUF_TO_SBP(bp);
  412. /*
  413. * Check that superblock mods match the mods made to AGF counters.
  414. */
  415. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  416. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  417. tp->t_ag_btree_delta));
  418. if (tp->t_icount_delta != 0) {
  419. INT_MOD(sbp->sb_icount, ARCH_CONVERT, tp->t_icount_delta);
  420. }
  421. if (tp->t_ifree_delta != 0) {
  422. INT_MOD(sbp->sb_ifree, ARCH_CONVERT, tp->t_ifree_delta);
  423. }
  424. if (tp->t_fdblocks_delta != 0) {
  425. INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_fdblocks_delta);
  426. }
  427. if (tp->t_res_fdblocks_delta != 0) {
  428. INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_res_fdblocks_delta);
  429. }
  430. if (tp->t_frextents_delta != 0) {
  431. INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_frextents_delta);
  432. }
  433. if (tp->t_res_frextents_delta != 0) {
  434. INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_res_frextents_delta);
  435. }
  436. if (tp->t_dblocks_delta != 0) {
  437. INT_MOD(sbp->sb_dblocks, ARCH_CONVERT, tp->t_dblocks_delta);
  438. whole = 1;
  439. }
  440. if (tp->t_agcount_delta != 0) {
  441. INT_MOD(sbp->sb_agcount, ARCH_CONVERT, tp->t_agcount_delta);
  442. whole = 1;
  443. }
  444. if (tp->t_imaxpct_delta != 0) {
  445. INT_MOD(sbp->sb_imax_pct, ARCH_CONVERT, tp->t_imaxpct_delta);
  446. whole = 1;
  447. }
  448. if (tp->t_rextsize_delta != 0) {
  449. INT_MOD(sbp->sb_rextsize, ARCH_CONVERT, tp->t_rextsize_delta);
  450. whole = 1;
  451. }
  452. if (tp->t_rbmblocks_delta != 0) {
  453. INT_MOD(sbp->sb_rbmblocks, ARCH_CONVERT, tp->t_rbmblocks_delta);
  454. whole = 1;
  455. }
  456. if (tp->t_rblocks_delta != 0) {
  457. INT_MOD(sbp->sb_rblocks, ARCH_CONVERT, tp->t_rblocks_delta);
  458. whole = 1;
  459. }
  460. if (tp->t_rextents_delta != 0) {
  461. INT_MOD(sbp->sb_rextents, ARCH_CONVERT, tp->t_rextents_delta);
  462. whole = 1;
  463. }
  464. if (tp->t_rextslog_delta != 0) {
  465. INT_MOD(sbp->sb_rextslog, ARCH_CONVERT, tp->t_rextslog_delta);
  466. whole = 1;
  467. }
  468. if (whole)
  469. /*
  470. * Log the whole thing, the fields are discontiguous.
  471. */
  472. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_sb_t) - 1);
  473. else
  474. /*
  475. * Since all the modifiable fields are contiguous, we
  476. * can get away with this.
  477. */
  478. xfs_trans_log_buf(tp, bp, offsetof(xfs_sb_t, sb_icount),
  479. offsetof(xfs_sb_t, sb_frextents) +
  480. sizeof(sbp->sb_frextents) - 1);
  481. XFS_MTOVFS(tp->t_mountp)->vfs_super->s_dirt = 1;
  482. }
  483. /*
  484. * xfs_trans_unreserve_and_mod_sb() is called to release unused
  485. * reservations and apply superblock counter changes to the in-core
  486. * superblock.
  487. *
  488. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  489. */
  490. STATIC void
  491. xfs_trans_unreserve_and_mod_sb(
  492. xfs_trans_t *tp)
  493. {
  494. xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
  495. xfs_mod_sb_t *msbp;
  496. /* REFERENCED */
  497. int error;
  498. int rsvd;
  499. msbp = msb;
  500. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  501. /*
  502. * Release any reserved blocks. Any that were allocated
  503. * will be taken back again by fdblocks_delta below.
  504. */
  505. if (tp->t_blk_res > 0) {
  506. msbp->msb_field = XFS_SBS_FDBLOCKS;
  507. msbp->msb_delta = tp->t_blk_res;
  508. msbp++;
  509. }
  510. /*
  511. * Release any reserved real time extents . Any that were
  512. * allocated will be taken back again by frextents_delta below.
  513. */
  514. if (tp->t_rtx_res > 0) {
  515. msbp->msb_field = XFS_SBS_FREXTENTS;
  516. msbp->msb_delta = tp->t_rtx_res;
  517. msbp++;
  518. }
  519. /*
  520. * Apply any superblock modifications to the in-core version.
  521. * The t_res_fdblocks_delta and t_res_frextents_delta fields are
  522. * explicity NOT applied to the in-core superblock.
  523. * The idea is that that has already been done.
  524. */
  525. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  526. if (tp->t_icount_delta != 0) {
  527. msbp->msb_field = XFS_SBS_ICOUNT;
  528. msbp->msb_delta = (int)tp->t_icount_delta;
  529. msbp++;
  530. }
  531. if (tp->t_ifree_delta != 0) {
  532. msbp->msb_field = XFS_SBS_IFREE;
  533. msbp->msb_delta = (int)tp->t_ifree_delta;
  534. msbp++;
  535. }
  536. if (tp->t_fdblocks_delta != 0) {
  537. msbp->msb_field = XFS_SBS_FDBLOCKS;
  538. msbp->msb_delta = (int)tp->t_fdblocks_delta;
  539. msbp++;
  540. }
  541. if (tp->t_frextents_delta != 0) {
  542. msbp->msb_field = XFS_SBS_FREXTENTS;
  543. msbp->msb_delta = (int)tp->t_frextents_delta;
  544. msbp++;
  545. }
  546. if (tp->t_dblocks_delta != 0) {
  547. msbp->msb_field = XFS_SBS_DBLOCKS;
  548. msbp->msb_delta = (int)tp->t_dblocks_delta;
  549. msbp++;
  550. }
  551. if (tp->t_agcount_delta != 0) {
  552. msbp->msb_field = XFS_SBS_AGCOUNT;
  553. msbp->msb_delta = (int)tp->t_agcount_delta;
  554. msbp++;
  555. }
  556. if (tp->t_imaxpct_delta != 0) {
  557. msbp->msb_field = XFS_SBS_IMAX_PCT;
  558. msbp->msb_delta = (int)tp->t_imaxpct_delta;
  559. msbp++;
  560. }
  561. if (tp->t_rextsize_delta != 0) {
  562. msbp->msb_field = XFS_SBS_REXTSIZE;
  563. msbp->msb_delta = (int)tp->t_rextsize_delta;
  564. msbp++;
  565. }
  566. if (tp->t_rbmblocks_delta != 0) {
  567. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  568. msbp->msb_delta = (int)tp->t_rbmblocks_delta;
  569. msbp++;
  570. }
  571. if (tp->t_rblocks_delta != 0) {
  572. msbp->msb_field = XFS_SBS_RBLOCKS;
  573. msbp->msb_delta = (int)tp->t_rblocks_delta;
  574. msbp++;
  575. }
  576. if (tp->t_rextents_delta != 0) {
  577. msbp->msb_field = XFS_SBS_REXTENTS;
  578. msbp->msb_delta = (int)tp->t_rextents_delta;
  579. msbp++;
  580. }
  581. if (tp->t_rextslog_delta != 0) {
  582. msbp->msb_field = XFS_SBS_REXTSLOG;
  583. msbp->msb_delta = (int)tp->t_rextslog_delta;
  584. msbp++;
  585. }
  586. }
  587. /*
  588. * If we need to change anything, do it.
  589. */
  590. if (msbp > msb) {
  591. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  592. (uint)(msbp - msb), rsvd);
  593. ASSERT(error == 0);
  594. }
  595. }
  596. /*
  597. * xfs_trans_commit
  598. *
  599. * Commit the given transaction to the log a/synchronously.
  600. *
  601. * XFS disk error handling mechanism is not based on a typical
  602. * transaction abort mechanism. Logically after the filesystem
  603. * gets marked 'SHUTDOWN', we can't let any new transactions
  604. * be durable - ie. committed to disk - because some metadata might
  605. * be inconsistent. In such cases, this returns an error, and the
  606. * caller may assume that all locked objects joined to the transaction
  607. * have already been unlocked as if the commit had succeeded.
  608. * Do not reference the transaction structure after this call.
  609. */
  610. /*ARGSUSED*/
  611. int
  612. _xfs_trans_commit(
  613. xfs_trans_t *tp,
  614. uint flags,
  615. xfs_lsn_t *commit_lsn_p,
  616. int *log_flushed)
  617. {
  618. xfs_log_iovec_t *log_vector;
  619. int nvec;
  620. xfs_mount_t *mp;
  621. xfs_lsn_t commit_lsn;
  622. /* REFERENCED */
  623. int error;
  624. int log_flags;
  625. int sync;
  626. #define XFS_TRANS_LOGVEC_COUNT 16
  627. xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
  628. #if defined(XLOG_NOLOG) || defined(DEBUG)
  629. static xfs_lsn_t trans_lsn = 1;
  630. #endif
  631. void *commit_iclog;
  632. int shutdown;
  633. commit_lsn = -1;
  634. /*
  635. * Determine whether this commit is releasing a permanent
  636. * log reservation or not.
  637. */
  638. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  639. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  640. log_flags = XFS_LOG_REL_PERM_RESERV;
  641. } else {
  642. log_flags = 0;
  643. }
  644. mp = tp->t_mountp;
  645. /*
  646. * If there is nothing to be logged by the transaction,
  647. * then unlock all of the items associated with the
  648. * transaction and free the transaction structure.
  649. * Also make sure to return any reserved blocks to
  650. * the free pool.
  651. */
  652. shut_us_down:
  653. shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
  654. if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
  655. xfs_trans_unreserve_and_mod_sb(tp);
  656. /*
  657. * It is indeed possible for the transaction to be
  658. * not dirty but the dqinfo portion to be. All that
  659. * means is that we have some (non-persistent) quota
  660. * reservations that need to be unreserved.
  661. */
  662. XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
  663. if (tp->t_ticket) {
  664. commit_lsn = xfs_log_done(mp, tp->t_ticket,
  665. NULL, log_flags);
  666. if (commit_lsn == -1 && !shutdown)
  667. shutdown = XFS_ERROR(EIO);
  668. }
  669. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  670. xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
  671. xfs_trans_free_busy(tp);
  672. xfs_trans_free(tp);
  673. XFS_STATS_INC(xs_trans_empty);
  674. if (commit_lsn_p)
  675. *commit_lsn_p = commit_lsn;
  676. return (shutdown);
  677. }
  678. #if defined(XLOG_NOLOG) || defined(DEBUG)
  679. ASSERT(!xlog_debug || tp->t_ticket != NULL);
  680. #else
  681. ASSERT(tp->t_ticket != NULL);
  682. #endif
  683. /*
  684. * If we need to update the superblock, then do it now.
  685. */
  686. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  687. xfs_trans_apply_sb_deltas(tp);
  688. }
  689. XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp);
  690. /*
  691. * Ask each log item how many log_vector entries it will
  692. * need so we can figure out how many to allocate.
  693. * Try to avoid the kmem_alloc() call in the common case
  694. * by using a vector from the stack when it fits.
  695. */
  696. nvec = xfs_trans_count_vecs(tp);
  697. if (nvec == 0) {
  698. xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
  699. goto shut_us_down;
  700. }
  701. if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
  702. log_vector = log_vector_fast;
  703. } else {
  704. log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
  705. sizeof(xfs_log_iovec_t),
  706. KM_SLEEP);
  707. }
  708. /*
  709. * Fill in the log_vector and pin the logged items, and
  710. * then write the transaction to the log.
  711. */
  712. xfs_trans_fill_vecs(tp, log_vector);
  713. /*
  714. * Ignore errors here. xfs_log_done would do the right thing.
  715. * We need to put the ticket, etc. away.
  716. */
  717. error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket,
  718. &(tp->t_lsn));
  719. #if defined(XLOG_NOLOG) || defined(DEBUG)
  720. if (xlog_debug) {
  721. commit_lsn = xfs_log_done(mp, tp->t_ticket,
  722. &commit_iclog, log_flags);
  723. } else {
  724. commit_lsn = 0;
  725. tp->t_lsn = trans_lsn++;
  726. }
  727. #else
  728. /*
  729. * This is the regular case. At this point (after the call finishes),
  730. * the transaction is committed incore and could go out to disk at
  731. * any time. However, all the items associated with the transaction
  732. * are still locked and pinned in memory.
  733. */
  734. commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
  735. #endif
  736. tp->t_commit_lsn = commit_lsn;
  737. if (nvec > XFS_TRANS_LOGVEC_COUNT) {
  738. kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t));
  739. }
  740. if (commit_lsn_p)
  741. *commit_lsn_p = commit_lsn;
  742. /*
  743. * If we got a log write error. Unpin the logitems that we
  744. * had pinned, clean up, free trans structure, and return error.
  745. */
  746. if (error || commit_lsn == -1) {
  747. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  748. xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
  749. return XFS_ERROR(EIO);
  750. }
  751. /*
  752. * Once the transaction has committed, unused
  753. * reservations need to be released and changes to
  754. * the superblock need to be reflected in the in-core
  755. * version. Do that now.
  756. */
  757. xfs_trans_unreserve_and_mod_sb(tp);
  758. sync = tp->t_flags & XFS_TRANS_SYNC;
  759. /*
  760. * Tell the LM to call the transaction completion routine
  761. * when the log write with LSN commit_lsn completes (e.g.
  762. * when the transaction commit really hits the on-disk log).
  763. * After this call we cannot reference tp, because the call
  764. * can happen at any time and the call will free the transaction
  765. * structure pointed to by tp. The only case where we call
  766. * the completion routine (xfs_trans_committed) directly is
  767. * if the log is turned off on a debug kernel or we're
  768. * running in simulation mode (the log is explicitly turned
  769. * off).
  770. */
  771. tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
  772. tp->t_logcb.cb_arg = tp;
  773. /*
  774. * We need to pass the iclog buffer which was used for the
  775. * transaction commit record into this function, and attach
  776. * the callback to it. The callback must be attached before
  777. * the items are unlocked to avoid racing with other threads
  778. * waiting for an item to unlock.
  779. */
  780. shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
  781. /*
  782. * Mark this thread as no longer being in a transaction
  783. */
  784. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  785. /*
  786. * Once all the items of the transaction have been copied
  787. * to the in core log and the callback is attached, the
  788. * items can be unlocked.
  789. *
  790. * This will free descriptors pointing to items which were
  791. * not logged since there is nothing more to do with them.
  792. * For items which were logged, we will keep pointers to them
  793. * so they can be unpinned after the transaction commits to disk.
  794. * This will also stamp each modified meta-data item with
  795. * the commit lsn of this transaction for dependency tracking
  796. * purposes.
  797. */
  798. xfs_trans_unlock_items(tp, commit_lsn);
  799. /*
  800. * If we detected a log error earlier, finish committing
  801. * the transaction now (unpin log items, etc).
  802. *
  803. * Order is critical here, to avoid using the transaction
  804. * pointer after its been freed (by xfs_trans_committed
  805. * either here now, or as a callback). We cannot do this
  806. * step inside xfs_log_notify as was done earlier because
  807. * of this issue.
  808. */
  809. if (shutdown)
  810. xfs_trans_committed(tp, XFS_LI_ABORTED);
  811. /*
  812. * Now that the xfs_trans_committed callback has been attached,
  813. * and the items are released we can finally allow the iclog to
  814. * go to disk.
  815. */
  816. error = xfs_log_release_iclog(mp, commit_iclog);
  817. /*
  818. * If the transaction needs to be synchronous, then force the
  819. * log out now and wait for it.
  820. */
  821. if (sync) {
  822. if (!error) {
  823. error = _xfs_log_force(mp, commit_lsn,
  824. XFS_LOG_FORCE | XFS_LOG_SYNC,
  825. log_flushed);
  826. }
  827. XFS_STATS_INC(xs_trans_sync);
  828. } else {
  829. XFS_STATS_INC(xs_trans_async);
  830. }
  831. return (error);
  832. }
  833. /*
  834. * Total up the number of log iovecs needed to commit this
  835. * transaction. The transaction itself needs one for the
  836. * transaction header. Ask each dirty item in turn how many
  837. * it needs to get the total.
  838. */
  839. STATIC uint
  840. xfs_trans_count_vecs(
  841. xfs_trans_t *tp)
  842. {
  843. int nvecs;
  844. xfs_log_item_desc_t *lidp;
  845. nvecs = 1;
  846. lidp = xfs_trans_first_item(tp);
  847. ASSERT(lidp != NULL);
  848. /* In the non-debug case we need to start bailing out if we
  849. * didn't find a log_item here, return zero and let trans_commit
  850. * deal with it.
  851. */
  852. if (lidp == NULL)
  853. return 0;
  854. while (lidp != NULL) {
  855. /*
  856. * Skip items which aren't dirty in this transaction.
  857. */
  858. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  859. lidp = xfs_trans_next_item(tp, lidp);
  860. continue;
  861. }
  862. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  863. nvecs += lidp->lid_size;
  864. lidp = xfs_trans_next_item(tp, lidp);
  865. }
  866. return nvecs;
  867. }
  868. /*
  869. * Called from the trans_commit code when we notice that
  870. * the filesystem is in the middle of a forced shutdown.
  871. */
  872. STATIC void
  873. xfs_trans_uncommit(
  874. xfs_trans_t *tp,
  875. uint flags)
  876. {
  877. xfs_log_item_desc_t *lidp;
  878. for (lidp = xfs_trans_first_item(tp);
  879. lidp != NULL;
  880. lidp = xfs_trans_next_item(tp, lidp)) {
  881. /*
  882. * Unpin all but those that aren't dirty.
  883. */
  884. if (lidp->lid_flags & XFS_LID_DIRTY)
  885. IOP_UNPIN_REMOVE(lidp->lid_item, tp);
  886. }
  887. xfs_trans_unreserve_and_mod_sb(tp);
  888. XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
  889. xfs_trans_free_items(tp, flags);
  890. xfs_trans_free_busy(tp);
  891. xfs_trans_free(tp);
  892. }
  893. /*
  894. * Fill in the vector with pointers to data to be logged
  895. * by this transaction. The transaction header takes
  896. * the first vector, and then each dirty item takes the
  897. * number of vectors it indicated it needed in xfs_trans_count_vecs().
  898. *
  899. * As each item fills in the entries it needs, also pin the item
  900. * so that it cannot be flushed out until the log write completes.
  901. */
  902. STATIC void
  903. xfs_trans_fill_vecs(
  904. xfs_trans_t *tp,
  905. xfs_log_iovec_t *log_vector)
  906. {
  907. xfs_log_item_desc_t *lidp;
  908. xfs_log_iovec_t *vecp;
  909. uint nitems;
  910. /*
  911. * Skip over the entry for the transaction header, we'll
  912. * fill that in at the end.
  913. */
  914. vecp = log_vector + 1; /* pointer arithmetic */
  915. nitems = 0;
  916. lidp = xfs_trans_first_item(tp);
  917. ASSERT(lidp != NULL);
  918. while (lidp != NULL) {
  919. /*
  920. * Skip items which aren't dirty in this transaction.
  921. */
  922. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  923. lidp = xfs_trans_next_item(tp, lidp);
  924. continue;
  925. }
  926. /*
  927. * The item may be marked dirty but not log anything.
  928. * This can be used to get called when a transaction
  929. * is committed.
  930. */
  931. if (lidp->lid_size) {
  932. nitems++;
  933. }
  934. IOP_FORMAT(lidp->lid_item, vecp);
  935. vecp += lidp->lid_size; /* pointer arithmetic */
  936. IOP_PIN(lidp->lid_item);
  937. lidp = xfs_trans_next_item(tp, lidp);
  938. }
  939. /*
  940. * Now that we've counted the number of items in this
  941. * transaction, fill in the transaction header.
  942. */
  943. tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
  944. tp->t_header.th_type = tp->t_type;
  945. tp->t_header.th_num_items = nitems;
  946. log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
  947. log_vector->i_len = sizeof(xfs_trans_header_t);
  948. XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_TRANSHDR);
  949. }
  950. /*
  951. * Unlock all of the transaction's items and free the transaction.
  952. * The transaction must not have modified any of its items, because
  953. * there is no way to restore them to their previous state.
  954. *
  955. * If the transaction has made a log reservation, make sure to release
  956. * it as well.
  957. */
  958. void
  959. xfs_trans_cancel(
  960. xfs_trans_t *tp,
  961. int flags)
  962. {
  963. int log_flags;
  964. #ifdef DEBUG
  965. xfs_log_item_chunk_t *licp;
  966. xfs_log_item_desc_t *lidp;
  967. xfs_log_item_t *lip;
  968. int i;
  969. #endif
  970. /*
  971. * See if the caller is being too lazy to figure out if
  972. * the transaction really needs an abort.
  973. */
  974. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  975. flags &= ~XFS_TRANS_ABORT;
  976. /*
  977. * See if the caller is relying on us to shut down the
  978. * filesystem. This happens in paths where we detect
  979. * corruption and decide to give up.
  980. */
  981. if ((tp->t_flags & XFS_TRANS_DIRTY) &&
  982. !XFS_FORCED_SHUTDOWN(tp->t_mountp))
  983. xfs_force_shutdown(tp->t_mountp, XFS_CORRUPT_INCORE);
  984. #ifdef DEBUG
  985. if (!(flags & XFS_TRANS_ABORT)) {
  986. licp = &(tp->t_items);
  987. while (licp != NULL) {
  988. lidp = licp->lic_descs;
  989. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  990. if (XFS_LIC_ISFREE(licp, i)) {
  991. continue;
  992. }
  993. lip = lidp->lid_item;
  994. if (!XFS_FORCED_SHUTDOWN(tp->t_mountp))
  995. ASSERT(!(lip->li_type == XFS_LI_EFD));
  996. }
  997. licp = licp->lic_next;
  998. }
  999. }
  1000. #endif
  1001. xfs_trans_unreserve_and_mod_sb(tp);
  1002. XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
  1003. if (tp->t_ticket) {
  1004. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1005. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1006. log_flags = XFS_LOG_REL_PERM_RESERV;
  1007. } else {
  1008. log_flags = 0;
  1009. }
  1010. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  1011. }
  1012. /* mark this thread as no longer being in a transaction */
  1013. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  1014. xfs_trans_free_items(tp, flags);
  1015. xfs_trans_free_busy(tp);
  1016. xfs_trans_free(tp);
  1017. }
  1018. /*
  1019. * Free the transaction structure. If there is more clean up
  1020. * to do when the structure is freed, add it here.
  1021. */
  1022. STATIC void
  1023. xfs_trans_free(
  1024. xfs_trans_t *tp)
  1025. {
  1026. atomic_dec(&tp->t_mountp->m_active_trans);
  1027. XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
  1028. kmem_zone_free(xfs_trans_zone, tp);
  1029. }
  1030. /*
  1031. * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
  1032. *
  1033. * This is typically called by the LM when a transaction has been fully
  1034. * committed to disk. It needs to unpin the items which have
  1035. * been logged by the transaction and update their positions
  1036. * in the AIL if necessary.
  1037. * This also gets called when the transactions didn't get written out
  1038. * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
  1039. *
  1040. * Call xfs_trans_chunk_committed() to process the items in
  1041. * each chunk.
  1042. */
  1043. STATIC void
  1044. xfs_trans_committed(
  1045. xfs_trans_t *tp,
  1046. int abortflag)
  1047. {
  1048. xfs_log_item_chunk_t *licp;
  1049. xfs_log_item_chunk_t *next_licp;
  1050. xfs_log_busy_chunk_t *lbcp;
  1051. xfs_log_busy_slot_t *lbsp;
  1052. int i;
  1053. /*
  1054. * Call the transaction's completion callback if there
  1055. * is one.
  1056. */
  1057. if (tp->t_callback != NULL) {
  1058. tp->t_callback(tp, tp->t_callarg);
  1059. }
  1060. /*
  1061. * Special case the chunk embedded in the transaction.
  1062. */
  1063. licp = &(tp->t_items);
  1064. if (!(XFS_LIC_ARE_ALL_FREE(licp))) {
  1065. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1066. }
  1067. /*
  1068. * Process the items in each chunk in turn.
  1069. */
  1070. licp = licp->lic_next;
  1071. while (licp != NULL) {
  1072. ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
  1073. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1074. next_licp = licp->lic_next;
  1075. kmem_free(licp, sizeof(xfs_log_item_chunk_t));
  1076. licp = next_licp;
  1077. }
  1078. /*
  1079. * Clear all the per-AG busy list items listed in this transaction
  1080. */
  1081. lbcp = &tp->t_busy;
  1082. while (lbcp != NULL) {
  1083. for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
  1084. if (!XFS_LBC_ISFREE(lbcp, i)) {
  1085. xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
  1086. lbsp->lbc_idx);
  1087. }
  1088. }
  1089. lbcp = lbcp->lbc_next;
  1090. }
  1091. xfs_trans_free_busy(tp);
  1092. /*
  1093. * That's it for the transaction structure. Free it.
  1094. */
  1095. xfs_trans_free(tp);
  1096. }
  1097. /*
  1098. * This is called to perform the commit processing for each
  1099. * item described by the given chunk.
  1100. *
  1101. * The commit processing consists of unlocking items which were
  1102. * held locked with the SYNC_UNLOCK attribute, calling the committed
  1103. * routine of each logged item, updating the item's position in the AIL
  1104. * if necessary, and unpinning each item. If the committed routine
  1105. * returns -1, then do nothing further with the item because it
  1106. * may have been freed.
  1107. *
  1108. * Since items are unlocked when they are copied to the incore
  1109. * log, it is possible for two transactions to be completing
  1110. * and manipulating the same item simultaneously. The AIL lock
  1111. * will protect the lsn field of each item. The value of this
  1112. * field can never go backwards.
  1113. *
  1114. * We unpin the items after repositioning them in the AIL, because
  1115. * otherwise they could be immediately flushed and we'd have to race
  1116. * with the flusher trying to pull the item from the AIL as we add it.
  1117. */
  1118. STATIC void
  1119. xfs_trans_chunk_committed(
  1120. xfs_log_item_chunk_t *licp,
  1121. xfs_lsn_t lsn,
  1122. int aborted)
  1123. {
  1124. xfs_log_item_desc_t *lidp;
  1125. xfs_log_item_t *lip;
  1126. xfs_lsn_t item_lsn;
  1127. struct xfs_mount *mp;
  1128. int i;
  1129. SPLDECL(s);
  1130. lidp = licp->lic_descs;
  1131. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  1132. if (XFS_LIC_ISFREE(licp, i)) {
  1133. continue;
  1134. }
  1135. lip = lidp->lid_item;
  1136. if (aborted)
  1137. lip->li_flags |= XFS_LI_ABORTED;
  1138. /*
  1139. * Send in the ABORTED flag to the COMMITTED routine
  1140. * so that it knows whether the transaction was aborted
  1141. * or not.
  1142. */
  1143. item_lsn = IOP_COMMITTED(lip, lsn);
  1144. /*
  1145. * If the committed routine returns -1, make
  1146. * no more references to the item.
  1147. */
  1148. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
  1149. continue;
  1150. }
  1151. /*
  1152. * If the returned lsn is greater than what it
  1153. * contained before, update the location of the
  1154. * item in the AIL. If it is not, then do nothing.
  1155. * Items can never move backwards in the AIL.
  1156. *
  1157. * While the new lsn should usually be greater, it
  1158. * is possible that a later transaction completing
  1159. * simultaneously with an earlier one using the
  1160. * same item could complete first with a higher lsn.
  1161. * This would cause the earlier transaction to fail
  1162. * the test below.
  1163. */
  1164. mp = lip->li_mountp;
  1165. AIL_LOCK(mp,s);
  1166. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
  1167. /*
  1168. * This will set the item's lsn to item_lsn
  1169. * and update the position of the item in
  1170. * the AIL.
  1171. *
  1172. * xfs_trans_update_ail() drops the AIL lock.
  1173. */
  1174. xfs_trans_update_ail(mp, lip, item_lsn, s);
  1175. } else {
  1176. AIL_UNLOCK(mp, s);
  1177. }
  1178. /*
  1179. * Now that we've repositioned the item in the AIL,
  1180. * unpin it so it can be flushed. Pass information
  1181. * about buffer stale state down from the log item
  1182. * flags, if anyone else stales the buffer we do not
  1183. * want to pay any attention to it.
  1184. */
  1185. IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
  1186. }
  1187. }