xfs_trans.c 35 KB

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