xfs_trans.c 38 KB

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