xfs_trans.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. * This is called to set the a callback to be called when the given
  301. * transaction is committed to disk. The transaction pointer and the
  302. * argument pointer will be passed to the callback routine.
  303. *
  304. * Only one callback can be associated with any single transaction.
  305. */
  306. void
  307. xfs_trans_callback(
  308. xfs_trans_t *tp,
  309. xfs_trans_callback_t callback,
  310. void *arg)
  311. {
  312. ASSERT(tp->t_callback == NULL);
  313. tp->t_callback = callback;
  314. tp->t_callarg = arg;
  315. }
  316. /*
  317. * Record the indicated change to the given field for application
  318. * to the file system's superblock when the transaction commits.
  319. * For now, just store the change in the transaction structure.
  320. *
  321. * Mark the transaction structure to indicate that the superblock
  322. * needs to be updated before committing.
  323. */
  324. void
  325. xfs_trans_mod_sb(
  326. xfs_trans_t *tp,
  327. uint field,
  328. long delta)
  329. {
  330. switch (field) {
  331. case XFS_TRANS_SB_ICOUNT:
  332. tp->t_icount_delta += delta;
  333. break;
  334. case XFS_TRANS_SB_IFREE:
  335. tp->t_ifree_delta += delta;
  336. break;
  337. case XFS_TRANS_SB_FDBLOCKS:
  338. /*
  339. * Track the number of blocks allocated in the
  340. * transaction. Make sure it does not exceed the
  341. * number reserved.
  342. */
  343. if (delta < 0) {
  344. tp->t_blk_res_used += (uint)-delta;
  345. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  346. }
  347. tp->t_fdblocks_delta += delta;
  348. break;
  349. case XFS_TRANS_SB_RES_FDBLOCKS:
  350. /*
  351. * The allocation has already been applied to the
  352. * in-core superblock's counter. This should only
  353. * be applied to the on-disk superblock.
  354. */
  355. ASSERT(delta < 0);
  356. tp->t_res_fdblocks_delta += delta;
  357. break;
  358. case XFS_TRANS_SB_FREXTENTS:
  359. /*
  360. * Track the number of blocks allocated in the
  361. * transaction. Make sure it does not exceed the
  362. * number reserved.
  363. */
  364. if (delta < 0) {
  365. tp->t_rtx_res_used += (uint)-delta;
  366. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  367. }
  368. tp->t_frextents_delta += delta;
  369. break;
  370. case XFS_TRANS_SB_RES_FREXTENTS:
  371. /*
  372. * The allocation has already been applied to the
  373. * in-core superblocks's counter. This should only
  374. * be applied to the on-disk superblock.
  375. */
  376. ASSERT(delta < 0);
  377. tp->t_res_frextents_delta += delta;
  378. break;
  379. case XFS_TRANS_SB_DBLOCKS:
  380. ASSERT(delta > 0);
  381. tp->t_dblocks_delta += delta;
  382. break;
  383. case XFS_TRANS_SB_AGCOUNT:
  384. ASSERT(delta > 0);
  385. tp->t_agcount_delta += delta;
  386. break;
  387. case XFS_TRANS_SB_IMAXPCT:
  388. tp->t_imaxpct_delta += delta;
  389. break;
  390. case XFS_TRANS_SB_REXTSIZE:
  391. tp->t_rextsize_delta += delta;
  392. break;
  393. case XFS_TRANS_SB_RBMBLOCKS:
  394. tp->t_rbmblocks_delta += delta;
  395. break;
  396. case XFS_TRANS_SB_RBLOCKS:
  397. tp->t_rblocks_delta += delta;
  398. break;
  399. case XFS_TRANS_SB_REXTENTS:
  400. tp->t_rextents_delta += delta;
  401. break;
  402. case XFS_TRANS_SB_REXTSLOG:
  403. tp->t_rextslog_delta += delta;
  404. break;
  405. default:
  406. ASSERT(0);
  407. return;
  408. }
  409. tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY);
  410. }
  411. /*
  412. * xfs_trans_apply_sb_deltas() is called from the commit code
  413. * to bring the superblock buffer into the current transaction
  414. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  415. *
  416. * For now we just look at each field allowed to change and change
  417. * it if necessary.
  418. */
  419. STATIC void
  420. xfs_trans_apply_sb_deltas(
  421. xfs_trans_t *tp)
  422. {
  423. xfs_sb_t *sbp;
  424. xfs_buf_t *bp;
  425. int whole = 0;
  426. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  427. sbp = XFS_BUF_TO_SBP(bp);
  428. /*
  429. * Check that superblock mods match the mods made to AGF counters.
  430. */
  431. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  432. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  433. tp->t_ag_btree_delta));
  434. if (tp->t_icount_delta != 0) {
  435. INT_MOD(sbp->sb_icount, ARCH_CONVERT, tp->t_icount_delta);
  436. }
  437. if (tp->t_ifree_delta != 0) {
  438. INT_MOD(sbp->sb_ifree, ARCH_CONVERT, tp->t_ifree_delta);
  439. }
  440. if (tp->t_fdblocks_delta != 0) {
  441. INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_fdblocks_delta);
  442. }
  443. if (tp->t_res_fdblocks_delta != 0) {
  444. INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_res_fdblocks_delta);
  445. }
  446. if (tp->t_frextents_delta != 0) {
  447. INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_frextents_delta);
  448. }
  449. if (tp->t_res_frextents_delta != 0) {
  450. INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_res_frextents_delta);
  451. }
  452. if (tp->t_dblocks_delta != 0) {
  453. INT_MOD(sbp->sb_dblocks, ARCH_CONVERT, tp->t_dblocks_delta);
  454. whole = 1;
  455. }
  456. if (tp->t_agcount_delta != 0) {
  457. INT_MOD(sbp->sb_agcount, ARCH_CONVERT, tp->t_agcount_delta);
  458. whole = 1;
  459. }
  460. if (tp->t_imaxpct_delta != 0) {
  461. INT_MOD(sbp->sb_imax_pct, ARCH_CONVERT, tp->t_imaxpct_delta);
  462. whole = 1;
  463. }
  464. if (tp->t_rextsize_delta != 0) {
  465. INT_MOD(sbp->sb_rextsize, ARCH_CONVERT, tp->t_rextsize_delta);
  466. whole = 1;
  467. }
  468. if (tp->t_rbmblocks_delta != 0) {
  469. INT_MOD(sbp->sb_rbmblocks, ARCH_CONVERT, tp->t_rbmblocks_delta);
  470. whole = 1;
  471. }
  472. if (tp->t_rblocks_delta != 0) {
  473. INT_MOD(sbp->sb_rblocks, ARCH_CONVERT, tp->t_rblocks_delta);
  474. whole = 1;
  475. }
  476. if (tp->t_rextents_delta != 0) {
  477. INT_MOD(sbp->sb_rextents, ARCH_CONVERT, tp->t_rextents_delta);
  478. whole = 1;
  479. }
  480. if (tp->t_rextslog_delta != 0) {
  481. INT_MOD(sbp->sb_rextslog, ARCH_CONVERT, tp->t_rextslog_delta);
  482. whole = 1;
  483. }
  484. if (whole)
  485. /*
  486. * Log the whole thing, the fields are discontiguous.
  487. */
  488. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_sb_t) - 1);
  489. else
  490. /*
  491. * Since all the modifiable fields are contiguous, we
  492. * can get away with this.
  493. */
  494. xfs_trans_log_buf(tp, bp, offsetof(xfs_sb_t, sb_icount),
  495. offsetof(xfs_sb_t, sb_frextents) +
  496. sizeof(sbp->sb_frextents) - 1);
  497. XFS_MTOVFS(tp->t_mountp)->vfs_super->s_dirt = 1;
  498. }
  499. /*
  500. * xfs_trans_unreserve_and_mod_sb() is called to release unused
  501. * reservations and apply superblock counter changes to the in-core
  502. * superblock.
  503. *
  504. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  505. */
  506. void
  507. xfs_trans_unreserve_and_mod_sb(
  508. xfs_trans_t *tp)
  509. {
  510. xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
  511. xfs_mod_sb_t *msbp;
  512. /* REFERENCED */
  513. int error;
  514. int rsvd;
  515. msbp = msb;
  516. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  517. /*
  518. * Release any reserved blocks. Any that were allocated
  519. * will be taken back again by fdblocks_delta below.
  520. */
  521. if (tp->t_blk_res > 0) {
  522. msbp->msb_field = XFS_SBS_FDBLOCKS;
  523. msbp->msb_delta = tp->t_blk_res;
  524. msbp++;
  525. }
  526. /*
  527. * Release any reserved real time extents . Any that were
  528. * allocated will be taken back again by frextents_delta below.
  529. */
  530. if (tp->t_rtx_res > 0) {
  531. msbp->msb_field = XFS_SBS_FREXTENTS;
  532. msbp->msb_delta = tp->t_rtx_res;
  533. msbp++;
  534. }
  535. /*
  536. * Apply any superblock modifications to the in-core version.
  537. * The t_res_fdblocks_delta and t_res_frextents_delta fields are
  538. * explicity NOT applied to the in-core superblock.
  539. * The idea is that that has already been done.
  540. */
  541. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  542. if (tp->t_icount_delta != 0) {
  543. msbp->msb_field = XFS_SBS_ICOUNT;
  544. msbp->msb_delta = (int)tp->t_icount_delta;
  545. msbp++;
  546. }
  547. if (tp->t_ifree_delta != 0) {
  548. msbp->msb_field = XFS_SBS_IFREE;
  549. msbp->msb_delta = (int)tp->t_ifree_delta;
  550. msbp++;
  551. }
  552. if (tp->t_fdblocks_delta != 0) {
  553. msbp->msb_field = XFS_SBS_FDBLOCKS;
  554. msbp->msb_delta = (int)tp->t_fdblocks_delta;
  555. msbp++;
  556. }
  557. if (tp->t_frextents_delta != 0) {
  558. msbp->msb_field = XFS_SBS_FREXTENTS;
  559. msbp->msb_delta = (int)tp->t_frextents_delta;
  560. msbp++;
  561. }
  562. if (tp->t_dblocks_delta != 0) {
  563. msbp->msb_field = XFS_SBS_DBLOCKS;
  564. msbp->msb_delta = (int)tp->t_dblocks_delta;
  565. msbp++;
  566. }
  567. if (tp->t_agcount_delta != 0) {
  568. msbp->msb_field = XFS_SBS_AGCOUNT;
  569. msbp->msb_delta = (int)tp->t_agcount_delta;
  570. msbp++;
  571. }
  572. if (tp->t_imaxpct_delta != 0) {
  573. msbp->msb_field = XFS_SBS_IMAX_PCT;
  574. msbp->msb_delta = (int)tp->t_imaxpct_delta;
  575. msbp++;
  576. }
  577. if (tp->t_rextsize_delta != 0) {
  578. msbp->msb_field = XFS_SBS_REXTSIZE;
  579. msbp->msb_delta = (int)tp->t_rextsize_delta;
  580. msbp++;
  581. }
  582. if (tp->t_rbmblocks_delta != 0) {
  583. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  584. msbp->msb_delta = (int)tp->t_rbmblocks_delta;
  585. msbp++;
  586. }
  587. if (tp->t_rblocks_delta != 0) {
  588. msbp->msb_field = XFS_SBS_RBLOCKS;
  589. msbp->msb_delta = (int)tp->t_rblocks_delta;
  590. msbp++;
  591. }
  592. if (tp->t_rextents_delta != 0) {
  593. msbp->msb_field = XFS_SBS_REXTENTS;
  594. msbp->msb_delta = (int)tp->t_rextents_delta;
  595. msbp++;
  596. }
  597. if (tp->t_rextslog_delta != 0) {
  598. msbp->msb_field = XFS_SBS_REXTSLOG;
  599. msbp->msb_delta = (int)tp->t_rextslog_delta;
  600. msbp++;
  601. }
  602. }
  603. /*
  604. * If we need to change anything, do it.
  605. */
  606. if (msbp > msb) {
  607. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  608. (uint)(msbp - msb), rsvd);
  609. ASSERT(error == 0);
  610. }
  611. }
  612. /*
  613. * xfs_trans_commit
  614. *
  615. * Commit the given transaction to the log a/synchronously.
  616. *
  617. * XFS disk error handling mechanism is not based on a typical
  618. * transaction abort mechanism. Logically after the filesystem
  619. * gets marked 'SHUTDOWN', we can't let any new transactions
  620. * be durable - ie. committed to disk - because some metadata might
  621. * be inconsistent. In such cases, this returns an error, and the
  622. * caller may assume that all locked objects joined to the transaction
  623. * have already been unlocked as if the commit had succeeded.
  624. * Do not reference the transaction structure after this call.
  625. */
  626. /*ARGSUSED*/
  627. int
  628. xfs_trans_commit(
  629. xfs_trans_t *tp,
  630. uint flags,
  631. xfs_lsn_t *commit_lsn_p)
  632. {
  633. xfs_log_iovec_t *log_vector;
  634. int nvec;
  635. xfs_mount_t *mp;
  636. xfs_lsn_t commit_lsn;
  637. /* REFERENCED */
  638. int error;
  639. int log_flags;
  640. int sync;
  641. #define XFS_TRANS_LOGVEC_COUNT 16
  642. xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
  643. #if defined(XLOG_NOLOG) || defined(DEBUG)
  644. static xfs_lsn_t trans_lsn = 1;
  645. #endif
  646. void *commit_iclog;
  647. int shutdown;
  648. commit_lsn = -1;
  649. /*
  650. * Determine whether this commit is releasing a permanent
  651. * log reservation or not.
  652. */
  653. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  654. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  655. log_flags = XFS_LOG_REL_PERM_RESERV;
  656. } else {
  657. log_flags = 0;
  658. }
  659. mp = tp->t_mountp;
  660. /*
  661. * If there is nothing to be logged by the transaction,
  662. * then unlock all of the items associated with the
  663. * transaction and free the transaction structure.
  664. * Also make sure to return any reserved blocks to
  665. * the free pool.
  666. */
  667. shut_us_down:
  668. shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
  669. if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
  670. xfs_trans_unreserve_and_mod_sb(tp);
  671. /*
  672. * It is indeed possible for the transaction to be
  673. * not dirty but the dqinfo portion to be. All that
  674. * means is that we have some (non-persistent) quota
  675. * reservations that need to be unreserved.
  676. */
  677. XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
  678. if (tp->t_ticket) {
  679. commit_lsn = xfs_log_done(mp, tp->t_ticket,
  680. NULL, log_flags);
  681. if (commit_lsn == -1 && !shutdown)
  682. shutdown = XFS_ERROR(EIO);
  683. }
  684. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  685. xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
  686. xfs_trans_free_busy(tp);
  687. xfs_trans_free(tp);
  688. XFS_STATS_INC(xs_trans_empty);
  689. if (commit_lsn_p)
  690. *commit_lsn_p = commit_lsn;
  691. return (shutdown);
  692. }
  693. #if defined(XLOG_NOLOG) || defined(DEBUG)
  694. ASSERT(!xlog_debug || tp->t_ticket != NULL);
  695. #else
  696. ASSERT(tp->t_ticket != NULL);
  697. #endif
  698. /*
  699. * If we need to update the superblock, then do it now.
  700. */
  701. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  702. xfs_trans_apply_sb_deltas(tp);
  703. }
  704. XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp);
  705. /*
  706. * Ask each log item how many log_vector entries it will
  707. * need so we can figure out how many to allocate.
  708. * Try to avoid the kmem_alloc() call in the common case
  709. * by using a vector from the stack when it fits.
  710. */
  711. nvec = xfs_trans_count_vecs(tp);
  712. if (nvec == 0) {
  713. xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
  714. goto shut_us_down;
  715. }
  716. if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
  717. log_vector = log_vector_fast;
  718. } else {
  719. log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
  720. sizeof(xfs_log_iovec_t),
  721. KM_SLEEP);
  722. }
  723. /*
  724. * Fill in the log_vector and pin the logged items, and
  725. * then write the transaction to the log.
  726. */
  727. xfs_trans_fill_vecs(tp, log_vector);
  728. /*
  729. * Ignore errors here. xfs_log_done would do the right thing.
  730. * We need to put the ticket, etc. away.
  731. */
  732. error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket,
  733. &(tp->t_lsn));
  734. #if defined(XLOG_NOLOG) || defined(DEBUG)
  735. if (xlog_debug) {
  736. commit_lsn = xfs_log_done(mp, tp->t_ticket,
  737. &commit_iclog, log_flags);
  738. } else {
  739. commit_lsn = 0;
  740. tp->t_lsn = trans_lsn++;
  741. }
  742. #else
  743. /*
  744. * This is the regular case. At this point (after the call finishes),
  745. * the transaction is committed incore and could go out to disk at
  746. * any time. However, all the items associated with the transaction
  747. * are still locked and pinned in memory.
  748. */
  749. commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
  750. #endif
  751. tp->t_commit_lsn = commit_lsn;
  752. if (nvec > XFS_TRANS_LOGVEC_COUNT) {
  753. kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t));
  754. }
  755. if (commit_lsn_p)
  756. *commit_lsn_p = commit_lsn;
  757. /*
  758. * If we got a log write error. Unpin the logitems that we
  759. * had pinned, clean up, free trans structure, and return error.
  760. */
  761. if (error || commit_lsn == -1) {
  762. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  763. xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
  764. return XFS_ERROR(EIO);
  765. }
  766. /*
  767. * Once the transaction has committed, unused
  768. * reservations need to be released and changes to
  769. * the superblock need to be reflected in the in-core
  770. * version. Do that now.
  771. */
  772. xfs_trans_unreserve_and_mod_sb(tp);
  773. sync = tp->t_flags & XFS_TRANS_SYNC;
  774. /*
  775. * Tell the LM to call the transaction completion routine
  776. * when the log write with LSN commit_lsn completes (e.g.
  777. * when the transaction commit really hits the on-disk log).
  778. * After this call we cannot reference tp, because the call
  779. * can happen at any time and the call will free the transaction
  780. * structure pointed to by tp. The only case where we call
  781. * the completion routine (xfs_trans_committed) directly is
  782. * if the log is turned off on a debug kernel or we're
  783. * running in simulation mode (the log is explicitly turned
  784. * off).
  785. */
  786. tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
  787. tp->t_logcb.cb_arg = tp;
  788. /*
  789. * We need to pass the iclog buffer which was used for the
  790. * transaction commit record into this function, and attach
  791. * the callback to it. The callback must be attached before
  792. * the items are unlocked to avoid racing with other threads
  793. * waiting for an item to unlock.
  794. */
  795. shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
  796. /*
  797. * Mark this thread as no longer being in a transaction
  798. */
  799. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  800. /*
  801. * Once all the items of the transaction have been copied
  802. * to the in core log and the callback is attached, the
  803. * items can be unlocked.
  804. *
  805. * This will free descriptors pointing to items which were
  806. * not logged since there is nothing more to do with them.
  807. * For items which were logged, we will keep pointers to them
  808. * so they can be unpinned after the transaction commits to disk.
  809. * This will also stamp each modified meta-data item with
  810. * the commit lsn of this transaction for dependency tracking
  811. * purposes.
  812. */
  813. xfs_trans_unlock_items(tp, commit_lsn);
  814. /*
  815. * If we detected a log error earlier, finish committing
  816. * the transaction now (unpin log items, etc).
  817. *
  818. * Order is critical here, to avoid using the transaction
  819. * pointer after its been freed (by xfs_trans_committed
  820. * either here now, or as a callback). We cannot do this
  821. * step inside xfs_log_notify as was done earlier because
  822. * of this issue.
  823. */
  824. if (shutdown)
  825. xfs_trans_committed(tp, XFS_LI_ABORTED);
  826. /*
  827. * Now that the xfs_trans_committed callback has been attached,
  828. * and the items are released we can finally allow the iclog to
  829. * go to disk.
  830. */
  831. error = xfs_log_release_iclog(mp, commit_iclog);
  832. /*
  833. * If the transaction needs to be synchronous, then force the
  834. * log out now and wait for it.
  835. */
  836. if (sync) {
  837. if (!error)
  838. error = xfs_log_force(mp, commit_lsn,
  839. XFS_LOG_FORCE | XFS_LOG_SYNC);
  840. XFS_STATS_INC(xs_trans_sync);
  841. } else {
  842. XFS_STATS_INC(xs_trans_async);
  843. }
  844. return (error);
  845. }
  846. /*
  847. * Total up the number of log iovecs needed to commit this
  848. * transaction. The transaction itself needs one for the
  849. * transaction header. Ask each dirty item in turn how many
  850. * it needs to get the total.
  851. */
  852. STATIC uint
  853. xfs_trans_count_vecs(
  854. xfs_trans_t *tp)
  855. {
  856. int nvecs;
  857. xfs_log_item_desc_t *lidp;
  858. nvecs = 1;
  859. lidp = xfs_trans_first_item(tp);
  860. ASSERT(lidp != NULL);
  861. /* In the non-debug case we need to start bailing out if we
  862. * didn't find a log_item here, return zero and let trans_commit
  863. * deal with it.
  864. */
  865. if (lidp == NULL)
  866. return 0;
  867. while (lidp != NULL) {
  868. /*
  869. * Skip items which aren't dirty in this transaction.
  870. */
  871. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  872. lidp = xfs_trans_next_item(tp, lidp);
  873. continue;
  874. }
  875. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  876. nvecs += lidp->lid_size;
  877. lidp = xfs_trans_next_item(tp, lidp);
  878. }
  879. return nvecs;
  880. }
  881. /*
  882. * Called from the trans_commit code when we notice that
  883. * the filesystem is in the middle of a forced shutdown.
  884. */
  885. STATIC void
  886. xfs_trans_uncommit(
  887. xfs_trans_t *tp,
  888. uint flags)
  889. {
  890. xfs_log_item_desc_t *lidp;
  891. for (lidp = xfs_trans_first_item(tp);
  892. lidp != NULL;
  893. lidp = xfs_trans_next_item(tp, lidp)) {
  894. /*
  895. * Unpin all but those that aren't dirty.
  896. */
  897. if (lidp->lid_flags & XFS_LID_DIRTY)
  898. IOP_UNPIN_REMOVE(lidp->lid_item, tp);
  899. }
  900. xfs_trans_unreserve_and_mod_sb(tp);
  901. XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
  902. xfs_trans_free_items(tp, flags);
  903. xfs_trans_free_busy(tp);
  904. xfs_trans_free(tp);
  905. }
  906. /*
  907. * Fill in the vector with pointers to data to be logged
  908. * by this transaction. The transaction header takes
  909. * the first vector, and then each dirty item takes the
  910. * number of vectors it indicated it needed in xfs_trans_count_vecs().
  911. *
  912. * As each item fills in the entries it needs, also pin the item
  913. * so that it cannot be flushed out until the log write completes.
  914. */
  915. STATIC void
  916. xfs_trans_fill_vecs(
  917. xfs_trans_t *tp,
  918. xfs_log_iovec_t *log_vector)
  919. {
  920. xfs_log_item_desc_t *lidp;
  921. xfs_log_iovec_t *vecp;
  922. uint nitems;
  923. /*
  924. * Skip over the entry for the transaction header, we'll
  925. * fill that in at the end.
  926. */
  927. vecp = log_vector + 1; /* pointer arithmetic */
  928. nitems = 0;
  929. lidp = xfs_trans_first_item(tp);
  930. ASSERT(lidp != NULL);
  931. while (lidp != NULL) {
  932. /*
  933. * Skip items which aren't dirty in this transaction.
  934. */
  935. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  936. lidp = xfs_trans_next_item(tp, lidp);
  937. continue;
  938. }
  939. /*
  940. * The item may be marked dirty but not log anything.
  941. * This can be used to get called when a transaction
  942. * is committed.
  943. */
  944. if (lidp->lid_size) {
  945. nitems++;
  946. }
  947. IOP_FORMAT(lidp->lid_item, vecp);
  948. vecp += lidp->lid_size; /* pointer arithmetic */
  949. IOP_PIN(lidp->lid_item);
  950. lidp = xfs_trans_next_item(tp, lidp);
  951. }
  952. /*
  953. * Now that we've counted the number of items in this
  954. * transaction, fill in the transaction header.
  955. */
  956. tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
  957. tp->t_header.th_type = tp->t_type;
  958. tp->t_header.th_num_items = nitems;
  959. log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
  960. log_vector->i_len = sizeof(xfs_trans_header_t);
  961. }
  962. /*
  963. * Unlock all of the transaction's items and free the transaction.
  964. * The transaction must not have modified any of its items, because
  965. * there is no way to restore them to their previous state.
  966. *
  967. * If the transaction has made a log reservation, make sure to release
  968. * it as well.
  969. */
  970. void
  971. xfs_trans_cancel(
  972. xfs_trans_t *tp,
  973. int flags)
  974. {
  975. int log_flags;
  976. #ifdef DEBUG
  977. xfs_log_item_chunk_t *licp;
  978. xfs_log_item_desc_t *lidp;
  979. xfs_log_item_t *lip;
  980. int i;
  981. #endif
  982. /*
  983. * See if the caller is being too lazy to figure out if
  984. * the transaction really needs an abort.
  985. */
  986. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  987. flags &= ~XFS_TRANS_ABORT;
  988. /*
  989. * See if the caller is relying on us to shut down the
  990. * filesystem. This happens in paths where we detect
  991. * corruption and decide to give up.
  992. */
  993. if ((tp->t_flags & XFS_TRANS_DIRTY) &&
  994. !XFS_FORCED_SHUTDOWN(tp->t_mountp))
  995. xfs_force_shutdown(tp->t_mountp, XFS_CORRUPT_INCORE);
  996. #ifdef DEBUG
  997. if (!(flags & XFS_TRANS_ABORT)) {
  998. licp = &(tp->t_items);
  999. while (licp != NULL) {
  1000. lidp = licp->lic_descs;
  1001. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  1002. if (XFS_LIC_ISFREE(licp, i)) {
  1003. continue;
  1004. }
  1005. lip = lidp->lid_item;
  1006. if (!XFS_FORCED_SHUTDOWN(tp->t_mountp))
  1007. ASSERT(!(lip->li_type == XFS_LI_EFD));
  1008. }
  1009. licp = licp->lic_next;
  1010. }
  1011. }
  1012. #endif
  1013. xfs_trans_unreserve_and_mod_sb(tp);
  1014. XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
  1015. if (tp->t_ticket) {
  1016. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1017. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1018. log_flags = XFS_LOG_REL_PERM_RESERV;
  1019. } else {
  1020. log_flags = 0;
  1021. }
  1022. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  1023. }
  1024. /* mark this thread as no longer being in a transaction */
  1025. PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
  1026. xfs_trans_free_items(tp, flags);
  1027. xfs_trans_free_busy(tp);
  1028. xfs_trans_free(tp);
  1029. }
  1030. /*
  1031. * Free the transaction structure. If there is more clean up
  1032. * to do when the structure is freed, add it here.
  1033. */
  1034. STATIC void
  1035. xfs_trans_free(
  1036. xfs_trans_t *tp)
  1037. {
  1038. atomic_dec(&tp->t_mountp->m_active_trans);
  1039. XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
  1040. kmem_zone_free(xfs_trans_zone, tp);
  1041. }
  1042. /*
  1043. * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
  1044. *
  1045. * This is typically called by the LM when a transaction has been fully
  1046. * committed to disk. It needs to unpin the items which have
  1047. * been logged by the transaction and update their positions
  1048. * in the AIL if necessary.
  1049. * This also gets called when the transactions didn't get written out
  1050. * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
  1051. *
  1052. * Call xfs_trans_chunk_committed() to process the items in
  1053. * each chunk.
  1054. */
  1055. STATIC void
  1056. xfs_trans_committed(
  1057. xfs_trans_t *tp,
  1058. int abortflag)
  1059. {
  1060. xfs_log_item_chunk_t *licp;
  1061. xfs_log_item_chunk_t *next_licp;
  1062. xfs_log_busy_chunk_t *lbcp;
  1063. xfs_log_busy_slot_t *lbsp;
  1064. int i;
  1065. /*
  1066. * Call the transaction's completion callback if there
  1067. * is one.
  1068. */
  1069. if (tp->t_callback != NULL) {
  1070. tp->t_callback(tp, tp->t_callarg);
  1071. }
  1072. /*
  1073. * Special case the chunk embedded in the transaction.
  1074. */
  1075. licp = &(tp->t_items);
  1076. if (!(XFS_LIC_ARE_ALL_FREE(licp))) {
  1077. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1078. }
  1079. /*
  1080. * Process the items in each chunk in turn.
  1081. */
  1082. licp = licp->lic_next;
  1083. while (licp != NULL) {
  1084. ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
  1085. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1086. next_licp = licp->lic_next;
  1087. kmem_free(licp, sizeof(xfs_log_item_chunk_t));
  1088. licp = next_licp;
  1089. }
  1090. /*
  1091. * Clear all the per-AG busy list items listed in this transaction
  1092. */
  1093. lbcp = &tp->t_busy;
  1094. while (lbcp != NULL) {
  1095. for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
  1096. if (!XFS_LBC_ISFREE(lbcp, i)) {
  1097. xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
  1098. lbsp->lbc_idx);
  1099. }
  1100. }
  1101. lbcp = lbcp->lbc_next;
  1102. }
  1103. xfs_trans_free_busy(tp);
  1104. /*
  1105. * That's it for the transaction structure. Free it.
  1106. */
  1107. xfs_trans_free(tp);
  1108. }
  1109. /*
  1110. * This is called to perform the commit processing for each
  1111. * item described by the given chunk.
  1112. *
  1113. * The commit processing consists of unlocking items which were
  1114. * held locked with the SYNC_UNLOCK attribute, calling the committed
  1115. * routine of each logged item, updating the item's position in the AIL
  1116. * if necessary, and unpinning each item. If the committed routine
  1117. * returns -1, then do nothing further with the item because it
  1118. * may have been freed.
  1119. *
  1120. * Since items are unlocked when they are copied to the incore
  1121. * log, it is possible for two transactions to be completing
  1122. * and manipulating the same item simultaneously. The AIL lock
  1123. * will protect the lsn field of each item. The value of this
  1124. * field can never go backwards.
  1125. *
  1126. * We unpin the items after repositioning them in the AIL, because
  1127. * otherwise they could be immediately flushed and we'd have to race
  1128. * with the flusher trying to pull the item from the AIL as we add it.
  1129. */
  1130. STATIC void
  1131. xfs_trans_chunk_committed(
  1132. xfs_log_item_chunk_t *licp,
  1133. xfs_lsn_t lsn,
  1134. int aborted)
  1135. {
  1136. xfs_log_item_desc_t *lidp;
  1137. xfs_log_item_t *lip;
  1138. xfs_lsn_t item_lsn;
  1139. struct xfs_mount *mp;
  1140. int i;
  1141. SPLDECL(s);
  1142. lidp = licp->lic_descs;
  1143. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  1144. if (XFS_LIC_ISFREE(licp, i)) {
  1145. continue;
  1146. }
  1147. lip = lidp->lid_item;
  1148. if (aborted)
  1149. lip->li_flags |= XFS_LI_ABORTED;
  1150. /*
  1151. * Send in the ABORTED flag to the COMMITTED routine
  1152. * so that it knows whether the transaction was aborted
  1153. * or not.
  1154. */
  1155. item_lsn = IOP_COMMITTED(lip, lsn);
  1156. /*
  1157. * If the committed routine returns -1, make
  1158. * no more references to the item.
  1159. */
  1160. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
  1161. continue;
  1162. }
  1163. /*
  1164. * If the returned lsn is greater than what it
  1165. * contained before, update the location of the
  1166. * item in the AIL. If it is not, then do nothing.
  1167. * Items can never move backwards in the AIL.
  1168. *
  1169. * While the new lsn should usually be greater, it
  1170. * is possible that a later transaction completing
  1171. * simultaneously with an earlier one using the
  1172. * same item could complete first with a higher lsn.
  1173. * This would cause the earlier transaction to fail
  1174. * the test below.
  1175. */
  1176. mp = lip->li_mountp;
  1177. AIL_LOCK(mp,s);
  1178. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
  1179. /*
  1180. * This will set the item's lsn to item_lsn
  1181. * and update the position of the item in
  1182. * the AIL.
  1183. *
  1184. * xfs_trans_update_ail() drops the AIL lock.
  1185. */
  1186. xfs_trans_update_ail(mp, lip, item_lsn, s);
  1187. } else {
  1188. AIL_UNLOCK(mp, s);
  1189. }
  1190. /*
  1191. * Now that we've repositioned the item in the AIL,
  1192. * unpin it so it can be flushed. Pass information
  1193. * about buffer stale state down from the log item
  1194. * flags, if anyone else stales the buffer we do not
  1195. * want to pay any attention to it.
  1196. */
  1197. IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
  1198. }
  1199. }