xfs_trans.c 35 KB

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