xfs_trans.c 36 KB

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