xfs_trans.c 34 KB

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