xfs_trans.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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. xfs_mount_t *mp = tp->t_mountp;
  929. /*
  930. * See if the caller is being too lazy to figure out if
  931. * the transaction really needs an abort.
  932. */
  933. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  934. flags &= ~XFS_TRANS_ABORT;
  935. /*
  936. * See if the caller is relying on us to shut down the
  937. * filesystem. This happens in paths where we detect
  938. * corruption and decide to give up.
  939. */
  940. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  941. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  942. xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
  943. }
  944. #ifdef DEBUG
  945. if (!(flags & XFS_TRANS_ABORT)) {
  946. licp = &(tp->t_items);
  947. while (licp != NULL) {
  948. lidp = licp->lic_descs;
  949. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  950. if (XFS_LIC_ISFREE(licp, i)) {
  951. continue;
  952. }
  953. lip = lidp->lid_item;
  954. if (!XFS_FORCED_SHUTDOWN(mp))
  955. ASSERT(!(lip->li_type == XFS_LI_EFD));
  956. }
  957. licp = licp->lic_next;
  958. }
  959. }
  960. #endif
  961. xfs_trans_unreserve_and_mod_sb(tp);
  962. XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
  963. if (tp->t_ticket) {
  964. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  965. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  966. log_flags = XFS_LOG_REL_PERM_RESERV;
  967. } else {
  968. log_flags = 0;
  969. }
  970. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  971. }
  972. /* mark this thread as no longer being in a transaction */
  973. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  974. xfs_trans_free_items(tp, flags);
  975. xfs_trans_free_busy(tp);
  976. xfs_trans_free(tp);
  977. }
  978. /*
  979. * Free the transaction structure. If there is more clean up
  980. * to do when the structure is freed, add it here.
  981. */
  982. STATIC void
  983. xfs_trans_free(
  984. xfs_trans_t *tp)
  985. {
  986. atomic_dec(&tp->t_mountp->m_active_trans);
  987. XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
  988. kmem_zone_free(xfs_trans_zone, tp);
  989. }
  990. /*
  991. * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
  992. *
  993. * This is typically called by the LM when a transaction has been fully
  994. * committed to disk. It needs to unpin the items which have
  995. * been logged by the transaction and update their positions
  996. * in the AIL if necessary.
  997. * This also gets called when the transactions didn't get written out
  998. * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
  999. *
  1000. * Call xfs_trans_chunk_committed() to process the items in
  1001. * each chunk.
  1002. */
  1003. STATIC void
  1004. xfs_trans_committed(
  1005. xfs_trans_t *tp,
  1006. int abortflag)
  1007. {
  1008. xfs_log_item_chunk_t *licp;
  1009. xfs_log_item_chunk_t *next_licp;
  1010. xfs_log_busy_chunk_t *lbcp;
  1011. xfs_log_busy_slot_t *lbsp;
  1012. int i;
  1013. /*
  1014. * Call the transaction's completion callback if there
  1015. * is one.
  1016. */
  1017. if (tp->t_callback != NULL) {
  1018. tp->t_callback(tp, tp->t_callarg);
  1019. }
  1020. /*
  1021. * Special case the chunk embedded in the transaction.
  1022. */
  1023. licp = &(tp->t_items);
  1024. if (!(XFS_LIC_ARE_ALL_FREE(licp))) {
  1025. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1026. }
  1027. /*
  1028. * Process the items in each chunk in turn.
  1029. */
  1030. licp = licp->lic_next;
  1031. while (licp != NULL) {
  1032. ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
  1033. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1034. next_licp = licp->lic_next;
  1035. kmem_free(licp, sizeof(xfs_log_item_chunk_t));
  1036. licp = next_licp;
  1037. }
  1038. /*
  1039. * Clear all the per-AG busy list items listed in this transaction
  1040. */
  1041. lbcp = &tp->t_busy;
  1042. while (lbcp != NULL) {
  1043. for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
  1044. if (!XFS_LBC_ISFREE(lbcp, i)) {
  1045. xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
  1046. lbsp->lbc_idx);
  1047. }
  1048. }
  1049. lbcp = lbcp->lbc_next;
  1050. }
  1051. xfs_trans_free_busy(tp);
  1052. /*
  1053. * That's it for the transaction structure. Free it.
  1054. */
  1055. xfs_trans_free(tp);
  1056. }
  1057. /*
  1058. * This is called to perform the commit processing for each
  1059. * item described by the given chunk.
  1060. *
  1061. * The commit processing consists of unlocking items which were
  1062. * held locked with the SYNC_UNLOCK attribute, calling the committed
  1063. * routine of each logged item, updating the item's position in the AIL
  1064. * if necessary, and unpinning each item. If the committed routine
  1065. * returns -1, then do nothing further with the item because it
  1066. * may have been freed.
  1067. *
  1068. * Since items are unlocked when they are copied to the incore
  1069. * log, it is possible for two transactions to be completing
  1070. * and manipulating the same item simultaneously. The AIL lock
  1071. * will protect the lsn field of each item. The value of this
  1072. * field can never go backwards.
  1073. *
  1074. * We unpin the items after repositioning them in the AIL, because
  1075. * otherwise they could be immediately flushed and we'd have to race
  1076. * with the flusher trying to pull the item from the AIL as we add it.
  1077. */
  1078. STATIC void
  1079. xfs_trans_chunk_committed(
  1080. xfs_log_item_chunk_t *licp,
  1081. xfs_lsn_t lsn,
  1082. int aborted)
  1083. {
  1084. xfs_log_item_desc_t *lidp;
  1085. xfs_log_item_t *lip;
  1086. xfs_lsn_t item_lsn;
  1087. struct xfs_mount *mp;
  1088. int i;
  1089. SPLDECL(s);
  1090. lidp = licp->lic_descs;
  1091. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  1092. if (XFS_LIC_ISFREE(licp, i)) {
  1093. continue;
  1094. }
  1095. lip = lidp->lid_item;
  1096. if (aborted)
  1097. lip->li_flags |= XFS_LI_ABORTED;
  1098. /*
  1099. * Send in the ABORTED flag to the COMMITTED routine
  1100. * so that it knows whether the transaction was aborted
  1101. * or not.
  1102. */
  1103. item_lsn = IOP_COMMITTED(lip, lsn);
  1104. /*
  1105. * If the committed routine returns -1, make
  1106. * no more references to the item.
  1107. */
  1108. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
  1109. continue;
  1110. }
  1111. /*
  1112. * If the returned lsn is greater than what it
  1113. * contained before, update the location of the
  1114. * item in the AIL. If it is not, then do nothing.
  1115. * Items can never move backwards in the AIL.
  1116. *
  1117. * While the new lsn should usually be greater, it
  1118. * is possible that a later transaction completing
  1119. * simultaneously with an earlier one using the
  1120. * same item could complete first with a higher lsn.
  1121. * This would cause the earlier transaction to fail
  1122. * the test below.
  1123. */
  1124. mp = lip->li_mountp;
  1125. AIL_LOCK(mp,s);
  1126. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
  1127. /*
  1128. * This will set the item's lsn to item_lsn
  1129. * and update the position of the item in
  1130. * the AIL.
  1131. *
  1132. * xfs_trans_update_ail() drops the AIL lock.
  1133. */
  1134. xfs_trans_update_ail(mp, lip, item_lsn, s);
  1135. } else {
  1136. AIL_UNLOCK(mp, s);
  1137. }
  1138. /*
  1139. * Now that we've repositioned the item in the AIL,
  1140. * unpin it so it can be flushed. Pass information
  1141. * about buffer stale state down from the log item
  1142. * flags, if anyone else stales the buffer we do not
  1143. * want to pay any attention to it.
  1144. */
  1145. IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
  1146. }
  1147. }