xfs_trans.c 33 KB

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