xfs_trans.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_types.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_inum.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_error.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dinode.h"
  35. #include "xfs_inode.h"
  36. #include "xfs_btree.h"
  37. #include "xfs_ialloc.h"
  38. #include "xfs_alloc.h"
  39. #include "xfs_bmap.h"
  40. #include "xfs_quota.h"
  41. #include "xfs_trans_priv.h"
  42. #include "xfs_trans_space.h"
  43. #include "xfs_inode_item.h"
  44. #include "xfs_trace.h"
  45. kmem_zone_t *xfs_trans_zone;
  46. kmem_zone_t *xfs_log_item_desc_zone;
  47. /*
  48. * Various log reservation values.
  49. *
  50. * These are based on the size of the file system block because that is what
  51. * most transactions manipulate. Each adds in an additional 128 bytes per
  52. * item logged to try to account for the overhead of the transaction mechanism.
  53. *
  54. * Note: Most of the reservations underestimate the number of allocation
  55. * groups into which they could free extents in the xfs_bmap_finish() call.
  56. * This is because the number in the worst case is quite high and quite
  57. * unusual. In order to fix this we need to change xfs_bmap_finish() to free
  58. * extents in only a single AG at a time. This will require changes to the
  59. * EFI code as well, however, so that the EFI for the extents not freed is
  60. * logged again in each transaction. See SGI PV #261917.
  61. *
  62. * Reservation functions here avoid a huge stack in xfs_trans_init due to
  63. * register overflow from temporaries in the calculations.
  64. */
  65. /*
  66. * In a write transaction we can allocate a maximum of 2
  67. * extents. This gives:
  68. * the inode getting the new extents: inode size
  69. * the inode's bmap btree: max depth * block size
  70. * the agfs of the ags from which the extents are allocated: 2 * sector
  71. * the superblock free block counter: sector size
  72. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  73. * And the bmap_finish transaction can free bmap blocks in a join:
  74. * the agfs of the ags containing the blocks: 2 * sector size
  75. * the agfls of the ags containing the blocks: 2 * sector size
  76. * the super block free block counter: sector size
  77. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  78. */
  79. STATIC uint
  80. xfs_calc_write_reservation(
  81. struct xfs_mount *mp)
  82. {
  83. return XFS_DQUOT_LOGRES(mp) +
  84. MAX((mp->m_sb.sb_inodesize +
  85. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
  86. 2 * mp->m_sb.sb_sectsize +
  87. mp->m_sb.sb_sectsize +
  88. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  89. 128 * (4 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
  90. XFS_ALLOCFREE_LOG_COUNT(mp, 2))),
  91. (2 * mp->m_sb.sb_sectsize +
  92. 2 * mp->m_sb.sb_sectsize +
  93. mp->m_sb.sb_sectsize +
  94. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  95. 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
  96. }
  97. /*
  98. * In truncating a file we free up to two extents at once. We can modify:
  99. * the inode being truncated: inode size
  100. * the inode's bmap btree: (max depth + 1) * block size
  101. * And the bmap_finish transaction can free the blocks and bmap blocks:
  102. * the agf for each of the ags: 4 * sector size
  103. * the agfl for each of the ags: 4 * sector size
  104. * the super block to reflect the freed blocks: sector size
  105. * worst case split in allocation btrees per extent assuming 4 extents:
  106. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  107. * the inode btree: max depth * blocksize
  108. * the allocation btrees: 2 trees * (max depth - 1) * block size
  109. */
  110. STATIC uint
  111. xfs_calc_itruncate_reservation(
  112. struct xfs_mount *mp)
  113. {
  114. return XFS_DQUOT_LOGRES(mp) +
  115. MAX((mp->m_sb.sb_inodesize +
  116. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1) +
  117. 128 * (2 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
  118. (4 * mp->m_sb.sb_sectsize +
  119. 4 * mp->m_sb.sb_sectsize +
  120. mp->m_sb.sb_sectsize +
  121. XFS_ALLOCFREE_LOG_RES(mp, 4) +
  122. 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4)) +
  123. 128 * 5 +
  124. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  125. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  126. XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  127. }
  128. /*
  129. * In renaming a files we can modify:
  130. * the four inodes involved: 4 * inode size
  131. * the two directory btrees: 2 * (max depth + v2) * dir block size
  132. * the two directory bmap btrees: 2 * max depth * block size
  133. * And the bmap_finish transaction can free dir and bmap blocks (two sets
  134. * of bmap blocks) giving:
  135. * the agf for the ags in which the blocks live: 3 * sector size
  136. * the agfl for the ags in which the blocks live: 3 * sector size
  137. * the superblock for the free block count: sector size
  138. * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
  139. */
  140. STATIC uint
  141. xfs_calc_rename_reservation(
  142. struct xfs_mount *mp)
  143. {
  144. return XFS_DQUOT_LOGRES(mp) +
  145. MAX((4 * mp->m_sb.sb_inodesize +
  146. 2 * XFS_DIROP_LOG_RES(mp) +
  147. 128 * (4 + 2 * XFS_DIROP_LOG_COUNT(mp))),
  148. (3 * mp->m_sb.sb_sectsize +
  149. 3 * mp->m_sb.sb_sectsize +
  150. mp->m_sb.sb_sectsize +
  151. XFS_ALLOCFREE_LOG_RES(mp, 3) +
  152. 128 * (7 + XFS_ALLOCFREE_LOG_COUNT(mp, 3))));
  153. }
  154. /*
  155. * For creating a link to an inode:
  156. * the parent directory inode: inode size
  157. * the linked inode: inode size
  158. * the directory btree could split: (max depth + v2) * dir block size
  159. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  160. * And the bmap_finish transaction can free some bmap blocks giving:
  161. * the agf for the ag in which the blocks live: sector size
  162. * the agfl for the ag in which the blocks live: sector size
  163. * the superblock for the free block count: sector size
  164. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  165. */
  166. STATIC uint
  167. xfs_calc_link_reservation(
  168. struct xfs_mount *mp)
  169. {
  170. return XFS_DQUOT_LOGRES(mp) +
  171. MAX((mp->m_sb.sb_inodesize +
  172. mp->m_sb.sb_inodesize +
  173. XFS_DIROP_LOG_RES(mp) +
  174. 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
  175. (mp->m_sb.sb_sectsize +
  176. mp->m_sb.sb_sectsize +
  177. mp->m_sb.sb_sectsize +
  178. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  179. 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  180. }
  181. /*
  182. * For removing a directory entry we can modify:
  183. * the parent directory inode: inode size
  184. * the removed inode: inode size
  185. * the directory btree could join: (max depth + v2) * dir block size
  186. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  187. * And the bmap_finish transaction can free the dir and bmap blocks giving:
  188. * the agf for the ag in which the blocks live: 2 * sector size
  189. * the agfl for the ag in which the blocks live: 2 * sector size
  190. * the superblock for the free block count: sector size
  191. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  192. */
  193. STATIC uint
  194. xfs_calc_remove_reservation(
  195. struct xfs_mount *mp)
  196. {
  197. return XFS_DQUOT_LOGRES(mp) +
  198. MAX((mp->m_sb.sb_inodesize +
  199. mp->m_sb.sb_inodesize +
  200. XFS_DIROP_LOG_RES(mp) +
  201. 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
  202. (2 * mp->m_sb.sb_sectsize +
  203. 2 * mp->m_sb.sb_sectsize +
  204. mp->m_sb.sb_sectsize +
  205. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  206. 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
  207. }
  208. /*
  209. * For symlink we can modify:
  210. * the parent directory inode: inode size
  211. * the new inode: inode size
  212. * the inode btree entry: 1 block
  213. * the directory btree: (max depth + v2) * dir block size
  214. * the directory inode's bmap btree: (max depth + v2) * block size
  215. * the blocks for the symlink: 1 kB
  216. * Or in the first xact we allocate some inodes giving:
  217. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  218. * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
  219. * the inode btree: max depth * blocksize
  220. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  221. */
  222. STATIC uint
  223. xfs_calc_symlink_reservation(
  224. struct xfs_mount *mp)
  225. {
  226. return XFS_DQUOT_LOGRES(mp) +
  227. MAX((mp->m_sb.sb_inodesize +
  228. mp->m_sb.sb_inodesize +
  229. XFS_FSB_TO_B(mp, 1) +
  230. XFS_DIROP_LOG_RES(mp) +
  231. 1024 +
  232. 128 * (4 + XFS_DIROP_LOG_COUNT(mp))),
  233. (2 * mp->m_sb.sb_sectsize +
  234. XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
  235. XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
  236. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  237. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  238. XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  239. }
  240. /*
  241. * For create we can modify:
  242. * the parent directory inode: inode size
  243. * the new inode: inode size
  244. * the inode btree entry: block size
  245. * the superblock for the nlink flag: sector size
  246. * the directory btree: (max depth + v2) * dir block size
  247. * the directory inode's bmap btree: (max depth + v2) * block size
  248. * Or in the first xact we allocate some inodes giving:
  249. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  250. * the superblock for the nlink flag: sector size
  251. * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
  252. * the inode btree: max depth * blocksize
  253. * the allocation btrees: 2 trees * (max depth - 1) * block size
  254. */
  255. STATIC uint
  256. xfs_calc_create_reservation(
  257. struct xfs_mount *mp)
  258. {
  259. return XFS_DQUOT_LOGRES(mp) +
  260. MAX((mp->m_sb.sb_inodesize +
  261. mp->m_sb.sb_inodesize +
  262. mp->m_sb.sb_sectsize +
  263. XFS_FSB_TO_B(mp, 1) +
  264. XFS_DIROP_LOG_RES(mp) +
  265. 128 * (3 + XFS_DIROP_LOG_COUNT(mp))),
  266. (3 * mp->m_sb.sb_sectsize +
  267. XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
  268. XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
  269. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  270. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  271. XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  272. }
  273. /*
  274. * Making a new directory is the same as creating a new file.
  275. */
  276. STATIC uint
  277. xfs_calc_mkdir_reservation(
  278. struct xfs_mount *mp)
  279. {
  280. return xfs_calc_create_reservation(mp);
  281. }
  282. /*
  283. * In freeing an inode we can modify:
  284. * the inode being freed: inode size
  285. * the super block free inode counter: sector size
  286. * the agi hash list and counters: sector size
  287. * the inode btree entry: block size
  288. * the on disk inode before ours in the agi hash list: inode cluster size
  289. * the inode btree: max depth * blocksize
  290. * the allocation btrees: 2 trees * (max depth - 1) * block size
  291. */
  292. STATIC uint
  293. xfs_calc_ifree_reservation(
  294. struct xfs_mount *mp)
  295. {
  296. return XFS_DQUOT_LOGRES(mp) +
  297. mp->m_sb.sb_inodesize +
  298. mp->m_sb.sb_sectsize +
  299. mp->m_sb.sb_sectsize +
  300. XFS_FSB_TO_B(mp, 1) +
  301. MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
  302. XFS_INODE_CLUSTER_SIZE(mp)) +
  303. 128 * 5 +
  304. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  305. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  306. XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  307. }
  308. /*
  309. * When only changing the inode we log the inode and possibly the superblock
  310. * We also add a bit of slop for the transaction stuff.
  311. */
  312. STATIC uint
  313. xfs_calc_ichange_reservation(
  314. struct xfs_mount *mp)
  315. {
  316. return XFS_DQUOT_LOGRES(mp) +
  317. mp->m_sb.sb_inodesize +
  318. mp->m_sb.sb_sectsize +
  319. 512;
  320. }
  321. /*
  322. * Growing the data section of the filesystem.
  323. * superblock
  324. * agi and agf
  325. * allocation btrees
  326. */
  327. STATIC uint
  328. xfs_calc_growdata_reservation(
  329. struct xfs_mount *mp)
  330. {
  331. return mp->m_sb.sb_sectsize * 3 +
  332. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  333. 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  334. }
  335. /*
  336. * Growing the rt section of the filesystem.
  337. * In the first set of transactions (ALLOC) we allocate space to the
  338. * bitmap or summary files.
  339. * superblock: sector size
  340. * agf of the ag from which the extent is allocated: sector size
  341. * bmap btree for bitmap/summary inode: max depth * blocksize
  342. * bitmap/summary inode: inode size
  343. * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
  344. */
  345. STATIC uint
  346. xfs_calc_growrtalloc_reservation(
  347. struct xfs_mount *mp)
  348. {
  349. return 2 * mp->m_sb.sb_sectsize +
  350. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
  351. mp->m_sb.sb_inodesize +
  352. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  353. 128 * (3 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
  354. XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  355. }
  356. /*
  357. * Growing the rt section of the filesystem.
  358. * In the second set of transactions (ZERO) we zero the new metadata blocks.
  359. * one bitmap/summary block: blocksize
  360. */
  361. STATIC uint
  362. xfs_calc_growrtzero_reservation(
  363. struct xfs_mount *mp)
  364. {
  365. return mp->m_sb.sb_blocksize + 128;
  366. }
  367. /*
  368. * Growing the rt section of the filesystem.
  369. * In the third set of transactions (FREE) we update metadata without
  370. * allocating any new blocks.
  371. * superblock: sector size
  372. * bitmap inode: inode size
  373. * summary inode: inode size
  374. * one bitmap block: blocksize
  375. * summary blocks: new summary size
  376. */
  377. STATIC uint
  378. xfs_calc_growrtfree_reservation(
  379. struct xfs_mount *mp)
  380. {
  381. return mp->m_sb.sb_sectsize +
  382. 2 * mp->m_sb.sb_inodesize +
  383. mp->m_sb.sb_blocksize +
  384. mp->m_rsumsize +
  385. 128 * 5;
  386. }
  387. /*
  388. * Logging the inode modification timestamp on a synchronous write.
  389. * inode
  390. */
  391. STATIC uint
  392. xfs_calc_swrite_reservation(
  393. struct xfs_mount *mp)
  394. {
  395. return mp->m_sb.sb_inodesize + 128;
  396. }
  397. /*
  398. * Logging the inode mode bits when writing a setuid/setgid file
  399. * inode
  400. */
  401. STATIC uint
  402. xfs_calc_writeid_reservation(xfs_mount_t *mp)
  403. {
  404. return mp->m_sb.sb_inodesize + 128;
  405. }
  406. /*
  407. * Converting the inode from non-attributed to attributed.
  408. * the inode being converted: inode size
  409. * agf block and superblock (for block allocation)
  410. * the new block (directory sized)
  411. * bmap blocks for the new directory block
  412. * allocation btrees
  413. */
  414. STATIC uint
  415. xfs_calc_addafork_reservation(
  416. struct xfs_mount *mp)
  417. {
  418. return XFS_DQUOT_LOGRES(mp) +
  419. mp->m_sb.sb_inodesize +
  420. mp->m_sb.sb_sectsize * 2 +
  421. mp->m_dirblksize +
  422. XFS_FSB_TO_B(mp, XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1) +
  423. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  424. 128 * (4 + XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1 +
  425. XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  426. }
  427. /*
  428. * Removing the attribute fork of a file
  429. * the inode being truncated: inode size
  430. * the inode's bmap btree: max depth * block size
  431. * And the bmap_finish transaction can free the blocks and bmap blocks:
  432. * the agf for each of the ags: 4 * sector size
  433. * the agfl for each of the ags: 4 * sector size
  434. * the super block to reflect the freed blocks: sector size
  435. * worst case split in allocation btrees per extent assuming 4 extents:
  436. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  437. */
  438. STATIC uint
  439. xfs_calc_attrinval_reservation(
  440. struct xfs_mount *mp)
  441. {
  442. return MAX((mp->m_sb.sb_inodesize +
  443. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
  444. 128 * (1 + XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK))),
  445. (4 * mp->m_sb.sb_sectsize +
  446. 4 * mp->m_sb.sb_sectsize +
  447. mp->m_sb.sb_sectsize +
  448. XFS_ALLOCFREE_LOG_RES(mp, 4) +
  449. 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4))));
  450. }
  451. /*
  452. * Setting an attribute.
  453. * the inode getting the attribute
  454. * the superblock for allocations
  455. * the agfs extents are allocated from
  456. * the attribute btree * max depth
  457. * the inode allocation btree
  458. * Since attribute transaction space is dependent on the size of the attribute,
  459. * the calculation is done partially at mount time and partially at runtime.
  460. */
  461. STATIC uint
  462. xfs_calc_attrset_reservation(
  463. struct xfs_mount *mp)
  464. {
  465. return XFS_DQUOT_LOGRES(mp) +
  466. mp->m_sb.sb_inodesize +
  467. mp->m_sb.sb_sectsize +
  468. XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
  469. 128 * (2 + XFS_DA_NODE_MAXDEPTH);
  470. }
  471. /*
  472. * Removing an attribute.
  473. * the inode: inode size
  474. * the attribute btree could join: max depth * block size
  475. * the inode bmap btree could join or split: max depth * block size
  476. * And the bmap_finish transaction can free the attr blocks freed giving:
  477. * the agf for the ag in which the blocks live: 2 * sector size
  478. * the agfl for the ag in which the blocks live: 2 * sector size
  479. * the superblock for the free block count: sector size
  480. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  481. */
  482. STATIC uint
  483. xfs_calc_attrrm_reservation(
  484. struct xfs_mount *mp)
  485. {
  486. return XFS_DQUOT_LOGRES(mp) +
  487. MAX((mp->m_sb.sb_inodesize +
  488. XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
  489. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
  490. 128 * (1 + XFS_DA_NODE_MAXDEPTH +
  491. XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
  492. (2 * mp->m_sb.sb_sectsize +
  493. 2 * mp->m_sb.sb_sectsize +
  494. mp->m_sb.sb_sectsize +
  495. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  496. 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
  497. }
  498. /*
  499. * Clearing a bad agino number in an agi hash bucket.
  500. */
  501. STATIC uint
  502. xfs_calc_clear_agi_bucket_reservation(
  503. struct xfs_mount *mp)
  504. {
  505. return mp->m_sb.sb_sectsize + 128;
  506. }
  507. /*
  508. * Initialize the precomputed transaction reservation values
  509. * in the mount structure.
  510. */
  511. void
  512. xfs_trans_init(
  513. struct xfs_mount *mp)
  514. {
  515. struct xfs_trans_reservations *resp = &mp->m_reservations;
  516. resp->tr_write = xfs_calc_write_reservation(mp);
  517. resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
  518. resp->tr_rename = xfs_calc_rename_reservation(mp);
  519. resp->tr_link = xfs_calc_link_reservation(mp);
  520. resp->tr_remove = xfs_calc_remove_reservation(mp);
  521. resp->tr_symlink = xfs_calc_symlink_reservation(mp);
  522. resp->tr_create = xfs_calc_create_reservation(mp);
  523. resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
  524. resp->tr_ifree = xfs_calc_ifree_reservation(mp);
  525. resp->tr_ichange = xfs_calc_ichange_reservation(mp);
  526. resp->tr_growdata = xfs_calc_growdata_reservation(mp);
  527. resp->tr_swrite = xfs_calc_swrite_reservation(mp);
  528. resp->tr_writeid = xfs_calc_writeid_reservation(mp);
  529. resp->tr_addafork = xfs_calc_addafork_reservation(mp);
  530. resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
  531. resp->tr_attrset = xfs_calc_attrset_reservation(mp);
  532. resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
  533. resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
  534. resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
  535. resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
  536. resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
  537. }
  538. /*
  539. * This routine is called to allocate a transaction structure.
  540. * The type parameter indicates the type of the transaction. These
  541. * are enumerated in xfs_trans.h.
  542. *
  543. * Dynamically allocate the transaction structure from the transaction
  544. * zone, initialize it, and return it to the caller.
  545. */
  546. xfs_trans_t *
  547. xfs_trans_alloc(
  548. xfs_mount_t *mp,
  549. uint type)
  550. {
  551. xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
  552. return _xfs_trans_alloc(mp, type, KM_SLEEP);
  553. }
  554. xfs_trans_t *
  555. _xfs_trans_alloc(
  556. xfs_mount_t *mp,
  557. uint type,
  558. uint memflags)
  559. {
  560. xfs_trans_t *tp;
  561. atomic_inc(&mp->m_active_trans);
  562. tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
  563. tp->t_magic = XFS_TRANS_MAGIC;
  564. tp->t_type = type;
  565. tp->t_mountp = mp;
  566. INIT_LIST_HEAD(&tp->t_items);
  567. INIT_LIST_HEAD(&tp->t_busy);
  568. return tp;
  569. }
  570. /*
  571. * Free the transaction structure. If there is more clean up
  572. * to do when the structure is freed, add it here.
  573. */
  574. STATIC void
  575. xfs_trans_free(
  576. struct xfs_trans *tp)
  577. {
  578. xfs_alloc_busy_sort(&tp->t_busy);
  579. xfs_alloc_busy_clear(tp->t_mountp, &tp->t_busy, false);
  580. atomic_dec(&tp->t_mountp->m_active_trans);
  581. xfs_trans_free_dqinfo(tp);
  582. kmem_zone_free(xfs_trans_zone, tp);
  583. }
  584. /*
  585. * This is called to create a new transaction which will share the
  586. * permanent log reservation of the given transaction. The remaining
  587. * unused block and rt extent reservations are also inherited. This
  588. * implies that the original transaction is no longer allowed to allocate
  589. * blocks. Locks and log items, however, are no inherited. They must
  590. * be added to the new transaction explicitly.
  591. */
  592. xfs_trans_t *
  593. xfs_trans_dup(
  594. xfs_trans_t *tp)
  595. {
  596. xfs_trans_t *ntp;
  597. ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  598. /*
  599. * Initialize the new transaction structure.
  600. */
  601. ntp->t_magic = XFS_TRANS_MAGIC;
  602. ntp->t_type = tp->t_type;
  603. ntp->t_mountp = tp->t_mountp;
  604. INIT_LIST_HEAD(&ntp->t_items);
  605. INIT_LIST_HEAD(&ntp->t_busy);
  606. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  607. ASSERT(tp->t_ticket != NULL);
  608. ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
  609. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  610. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  611. tp->t_blk_res = tp->t_blk_res_used;
  612. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  613. tp->t_rtx_res = tp->t_rtx_res_used;
  614. ntp->t_pflags = tp->t_pflags;
  615. xfs_trans_dup_dqinfo(tp, ntp);
  616. atomic_inc(&tp->t_mountp->m_active_trans);
  617. return ntp;
  618. }
  619. /*
  620. * This is called to reserve free disk blocks and log space for the
  621. * given transaction. This must be done before allocating any resources
  622. * within the transaction.
  623. *
  624. * This will return ENOSPC if there are not enough blocks available.
  625. * It will sleep waiting for available log space.
  626. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  627. * is used by long running transactions. If any one of the reservations
  628. * fails then they will all be backed out.
  629. *
  630. * This does not do quota reservations. That typically is done by the
  631. * caller afterwards.
  632. */
  633. int
  634. xfs_trans_reserve(
  635. xfs_trans_t *tp,
  636. uint blocks,
  637. uint logspace,
  638. uint rtextents,
  639. uint flags,
  640. uint logcount)
  641. {
  642. int log_flags;
  643. int error = 0;
  644. int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  645. /* Mark this thread as being in a transaction */
  646. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  647. /*
  648. * Attempt to reserve the needed disk blocks by decrementing
  649. * the number needed from the number available. This will
  650. * fail if the count would go below zero.
  651. */
  652. if (blocks > 0) {
  653. error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  654. -((int64_t)blocks), rsvd);
  655. if (error != 0) {
  656. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  657. return (XFS_ERROR(ENOSPC));
  658. }
  659. tp->t_blk_res += blocks;
  660. }
  661. /*
  662. * Reserve the log space needed for this transaction.
  663. */
  664. if (logspace > 0) {
  665. ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
  666. ASSERT((tp->t_log_count == 0) ||
  667. (tp->t_log_count == logcount));
  668. if (flags & XFS_TRANS_PERM_LOG_RES) {
  669. log_flags = XFS_LOG_PERM_RESERV;
  670. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  671. } else {
  672. ASSERT(tp->t_ticket == NULL);
  673. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  674. log_flags = 0;
  675. }
  676. error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
  677. &tp->t_ticket,
  678. XFS_TRANSACTION, log_flags, tp->t_type);
  679. if (error) {
  680. goto undo_blocks;
  681. }
  682. tp->t_log_res = logspace;
  683. tp->t_log_count = logcount;
  684. }
  685. /*
  686. * Attempt to reserve the needed realtime extents by decrementing
  687. * the number needed from the number available. This will
  688. * fail if the count would go below zero.
  689. */
  690. if (rtextents > 0) {
  691. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
  692. -((int64_t)rtextents), rsvd);
  693. if (error) {
  694. error = XFS_ERROR(ENOSPC);
  695. goto undo_log;
  696. }
  697. tp->t_rtx_res += rtextents;
  698. }
  699. return 0;
  700. /*
  701. * Error cases jump to one of these labels to undo any
  702. * reservations which have already been performed.
  703. */
  704. undo_log:
  705. if (logspace > 0) {
  706. if (flags & XFS_TRANS_PERM_LOG_RES) {
  707. log_flags = XFS_LOG_REL_PERM_RESERV;
  708. } else {
  709. log_flags = 0;
  710. }
  711. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  712. tp->t_ticket = NULL;
  713. tp->t_log_res = 0;
  714. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  715. }
  716. undo_blocks:
  717. if (blocks > 0) {
  718. xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  719. (int64_t)blocks, rsvd);
  720. tp->t_blk_res = 0;
  721. }
  722. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  723. return error;
  724. }
  725. /*
  726. * Record the indicated change to the given field for application
  727. * to the file system's superblock when the transaction commits.
  728. * For now, just store the change in the transaction structure.
  729. *
  730. * Mark the transaction structure to indicate that the superblock
  731. * needs to be updated before committing.
  732. *
  733. * Because we may not be keeping track of allocated/free inodes and
  734. * used filesystem blocks in the superblock, we do not mark the
  735. * superblock dirty in this transaction if we modify these fields.
  736. * We still need to update the transaction deltas so that they get
  737. * applied to the incore superblock, but we don't want them to
  738. * cause the superblock to get locked and logged if these are the
  739. * only fields in the superblock that the transaction modifies.
  740. */
  741. void
  742. xfs_trans_mod_sb(
  743. xfs_trans_t *tp,
  744. uint field,
  745. int64_t delta)
  746. {
  747. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  748. xfs_mount_t *mp = tp->t_mountp;
  749. switch (field) {
  750. case XFS_TRANS_SB_ICOUNT:
  751. tp->t_icount_delta += delta;
  752. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  753. flags &= ~XFS_TRANS_SB_DIRTY;
  754. break;
  755. case XFS_TRANS_SB_IFREE:
  756. tp->t_ifree_delta += delta;
  757. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  758. flags &= ~XFS_TRANS_SB_DIRTY;
  759. break;
  760. case XFS_TRANS_SB_FDBLOCKS:
  761. /*
  762. * Track the number of blocks allocated in the
  763. * transaction. Make sure it does not exceed the
  764. * number reserved.
  765. */
  766. if (delta < 0) {
  767. tp->t_blk_res_used += (uint)-delta;
  768. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  769. }
  770. tp->t_fdblocks_delta += delta;
  771. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  772. flags &= ~XFS_TRANS_SB_DIRTY;
  773. break;
  774. case XFS_TRANS_SB_RES_FDBLOCKS:
  775. /*
  776. * The allocation has already been applied to the
  777. * in-core superblock's counter. This should only
  778. * be applied to the on-disk superblock.
  779. */
  780. ASSERT(delta < 0);
  781. tp->t_res_fdblocks_delta += delta;
  782. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  783. flags &= ~XFS_TRANS_SB_DIRTY;
  784. break;
  785. case XFS_TRANS_SB_FREXTENTS:
  786. /*
  787. * Track the number of blocks allocated in the
  788. * transaction. Make sure it does not exceed the
  789. * number reserved.
  790. */
  791. if (delta < 0) {
  792. tp->t_rtx_res_used += (uint)-delta;
  793. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  794. }
  795. tp->t_frextents_delta += delta;
  796. break;
  797. case XFS_TRANS_SB_RES_FREXTENTS:
  798. /*
  799. * The allocation has already been applied to the
  800. * in-core superblock's counter. This should only
  801. * be applied to the on-disk superblock.
  802. */
  803. ASSERT(delta < 0);
  804. tp->t_res_frextents_delta += delta;
  805. break;
  806. case XFS_TRANS_SB_DBLOCKS:
  807. ASSERT(delta > 0);
  808. tp->t_dblocks_delta += delta;
  809. break;
  810. case XFS_TRANS_SB_AGCOUNT:
  811. ASSERT(delta > 0);
  812. tp->t_agcount_delta += delta;
  813. break;
  814. case XFS_TRANS_SB_IMAXPCT:
  815. tp->t_imaxpct_delta += delta;
  816. break;
  817. case XFS_TRANS_SB_REXTSIZE:
  818. tp->t_rextsize_delta += delta;
  819. break;
  820. case XFS_TRANS_SB_RBMBLOCKS:
  821. tp->t_rbmblocks_delta += delta;
  822. break;
  823. case XFS_TRANS_SB_RBLOCKS:
  824. tp->t_rblocks_delta += delta;
  825. break;
  826. case XFS_TRANS_SB_REXTENTS:
  827. tp->t_rextents_delta += delta;
  828. break;
  829. case XFS_TRANS_SB_REXTSLOG:
  830. tp->t_rextslog_delta += delta;
  831. break;
  832. default:
  833. ASSERT(0);
  834. return;
  835. }
  836. tp->t_flags |= flags;
  837. }
  838. /*
  839. * xfs_trans_apply_sb_deltas() is called from the commit code
  840. * to bring the superblock buffer into the current transaction
  841. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  842. *
  843. * For now we just look at each field allowed to change and change
  844. * it if necessary.
  845. */
  846. STATIC void
  847. xfs_trans_apply_sb_deltas(
  848. xfs_trans_t *tp)
  849. {
  850. xfs_dsb_t *sbp;
  851. xfs_buf_t *bp;
  852. int whole = 0;
  853. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  854. sbp = XFS_BUF_TO_SBP(bp);
  855. /*
  856. * Check that superblock mods match the mods made to AGF counters.
  857. */
  858. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  859. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  860. tp->t_ag_btree_delta));
  861. /*
  862. * Only update the superblock counters if we are logging them
  863. */
  864. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  865. if (tp->t_icount_delta)
  866. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  867. if (tp->t_ifree_delta)
  868. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  869. if (tp->t_fdblocks_delta)
  870. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  871. if (tp->t_res_fdblocks_delta)
  872. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  873. }
  874. if (tp->t_frextents_delta)
  875. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  876. if (tp->t_res_frextents_delta)
  877. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  878. if (tp->t_dblocks_delta) {
  879. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  880. whole = 1;
  881. }
  882. if (tp->t_agcount_delta) {
  883. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  884. whole = 1;
  885. }
  886. if (tp->t_imaxpct_delta) {
  887. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  888. whole = 1;
  889. }
  890. if (tp->t_rextsize_delta) {
  891. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  892. whole = 1;
  893. }
  894. if (tp->t_rbmblocks_delta) {
  895. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  896. whole = 1;
  897. }
  898. if (tp->t_rblocks_delta) {
  899. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  900. whole = 1;
  901. }
  902. if (tp->t_rextents_delta) {
  903. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  904. whole = 1;
  905. }
  906. if (tp->t_rextslog_delta) {
  907. sbp->sb_rextslog += tp->t_rextslog_delta;
  908. whole = 1;
  909. }
  910. if (whole)
  911. /*
  912. * Log the whole thing, the fields are noncontiguous.
  913. */
  914. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  915. else
  916. /*
  917. * Since all the modifiable fields are contiguous, we
  918. * can get away with this.
  919. */
  920. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  921. offsetof(xfs_dsb_t, sb_frextents) +
  922. sizeof(sbp->sb_frextents) - 1);
  923. }
  924. /*
  925. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  926. * and apply superblock counter changes to the in-core superblock. The
  927. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  928. * applied to the in-core superblock. The idea is that that has already been
  929. * done.
  930. *
  931. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  932. * However, we have to ensure that we only modify each superblock field only
  933. * once because the application of the delta values may not be atomic. That can
  934. * lead to ENOSPC races occurring if we have two separate modifcations of the
  935. * free space counter to put back the entire reservation and then take away
  936. * what we used.
  937. *
  938. * If we are not logging superblock counters, then the inode allocated/free and
  939. * used block counts are not updated in the on disk superblock. In this case,
  940. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  941. * still need to update the incore superblock with the changes.
  942. */
  943. void
  944. xfs_trans_unreserve_and_mod_sb(
  945. xfs_trans_t *tp)
  946. {
  947. xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
  948. xfs_mod_sb_t *msbp;
  949. xfs_mount_t *mp = tp->t_mountp;
  950. /* REFERENCED */
  951. int error;
  952. int rsvd;
  953. int64_t blkdelta = 0;
  954. int64_t rtxdelta = 0;
  955. int64_t idelta = 0;
  956. int64_t ifreedelta = 0;
  957. msbp = msb;
  958. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  959. /* calculate deltas */
  960. if (tp->t_blk_res > 0)
  961. blkdelta = tp->t_blk_res;
  962. if ((tp->t_fdblocks_delta != 0) &&
  963. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  964. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  965. blkdelta += tp->t_fdblocks_delta;
  966. if (tp->t_rtx_res > 0)
  967. rtxdelta = tp->t_rtx_res;
  968. if ((tp->t_frextents_delta != 0) &&
  969. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  970. rtxdelta += tp->t_frextents_delta;
  971. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  972. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  973. idelta = tp->t_icount_delta;
  974. ifreedelta = tp->t_ifree_delta;
  975. }
  976. /* apply the per-cpu counters */
  977. if (blkdelta) {
  978. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  979. blkdelta, rsvd);
  980. if (error)
  981. goto out;
  982. }
  983. if (idelta) {
  984. error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
  985. idelta, rsvd);
  986. if (error)
  987. goto out_undo_fdblocks;
  988. }
  989. if (ifreedelta) {
  990. error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
  991. ifreedelta, rsvd);
  992. if (error)
  993. goto out_undo_icount;
  994. }
  995. /* apply remaining deltas */
  996. if (rtxdelta != 0) {
  997. msbp->msb_field = XFS_SBS_FREXTENTS;
  998. msbp->msb_delta = rtxdelta;
  999. msbp++;
  1000. }
  1001. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  1002. if (tp->t_dblocks_delta != 0) {
  1003. msbp->msb_field = XFS_SBS_DBLOCKS;
  1004. msbp->msb_delta = tp->t_dblocks_delta;
  1005. msbp++;
  1006. }
  1007. if (tp->t_agcount_delta != 0) {
  1008. msbp->msb_field = XFS_SBS_AGCOUNT;
  1009. msbp->msb_delta = tp->t_agcount_delta;
  1010. msbp++;
  1011. }
  1012. if (tp->t_imaxpct_delta != 0) {
  1013. msbp->msb_field = XFS_SBS_IMAX_PCT;
  1014. msbp->msb_delta = tp->t_imaxpct_delta;
  1015. msbp++;
  1016. }
  1017. if (tp->t_rextsize_delta != 0) {
  1018. msbp->msb_field = XFS_SBS_REXTSIZE;
  1019. msbp->msb_delta = tp->t_rextsize_delta;
  1020. msbp++;
  1021. }
  1022. if (tp->t_rbmblocks_delta != 0) {
  1023. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  1024. msbp->msb_delta = tp->t_rbmblocks_delta;
  1025. msbp++;
  1026. }
  1027. if (tp->t_rblocks_delta != 0) {
  1028. msbp->msb_field = XFS_SBS_RBLOCKS;
  1029. msbp->msb_delta = tp->t_rblocks_delta;
  1030. msbp++;
  1031. }
  1032. if (tp->t_rextents_delta != 0) {
  1033. msbp->msb_field = XFS_SBS_REXTENTS;
  1034. msbp->msb_delta = tp->t_rextents_delta;
  1035. msbp++;
  1036. }
  1037. if (tp->t_rextslog_delta != 0) {
  1038. msbp->msb_field = XFS_SBS_REXTSLOG;
  1039. msbp->msb_delta = tp->t_rextslog_delta;
  1040. msbp++;
  1041. }
  1042. }
  1043. /*
  1044. * If we need to change anything, do it.
  1045. */
  1046. if (msbp > msb) {
  1047. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  1048. (uint)(msbp - msb), rsvd);
  1049. if (error)
  1050. goto out_undo_ifreecount;
  1051. }
  1052. return;
  1053. out_undo_ifreecount:
  1054. if (ifreedelta)
  1055. xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
  1056. out_undo_icount:
  1057. if (idelta)
  1058. xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
  1059. out_undo_fdblocks:
  1060. if (blkdelta)
  1061. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
  1062. out:
  1063. ASSERT(error == 0);
  1064. return;
  1065. }
  1066. /*
  1067. * Add the given log item to the transaction's list of log items.
  1068. *
  1069. * The log item will now point to its new descriptor with its li_desc field.
  1070. */
  1071. void
  1072. xfs_trans_add_item(
  1073. struct xfs_trans *tp,
  1074. struct xfs_log_item *lip)
  1075. {
  1076. struct xfs_log_item_desc *lidp;
  1077. ASSERT(lip->li_mountp = tp->t_mountp);
  1078. ASSERT(lip->li_ailp = tp->t_mountp->m_ail);
  1079. lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
  1080. lidp->lid_item = lip;
  1081. lidp->lid_flags = 0;
  1082. lidp->lid_size = 0;
  1083. list_add_tail(&lidp->lid_trans, &tp->t_items);
  1084. lip->li_desc = lidp;
  1085. }
  1086. STATIC void
  1087. xfs_trans_free_item_desc(
  1088. struct xfs_log_item_desc *lidp)
  1089. {
  1090. list_del_init(&lidp->lid_trans);
  1091. kmem_zone_free(xfs_log_item_desc_zone, lidp);
  1092. }
  1093. /*
  1094. * Unlink and free the given descriptor.
  1095. */
  1096. void
  1097. xfs_trans_del_item(
  1098. struct xfs_log_item *lip)
  1099. {
  1100. xfs_trans_free_item_desc(lip->li_desc);
  1101. lip->li_desc = NULL;
  1102. }
  1103. /*
  1104. * Unlock all of the items of a transaction and free all the descriptors
  1105. * of that transaction.
  1106. */
  1107. void
  1108. xfs_trans_free_items(
  1109. struct xfs_trans *tp,
  1110. xfs_lsn_t commit_lsn,
  1111. int flags)
  1112. {
  1113. struct xfs_log_item_desc *lidp, *next;
  1114. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1115. struct xfs_log_item *lip = lidp->lid_item;
  1116. lip->li_desc = NULL;
  1117. if (commit_lsn != NULLCOMMITLSN)
  1118. IOP_COMMITTING(lip, commit_lsn);
  1119. if (flags & XFS_TRANS_ABORT)
  1120. lip->li_flags |= XFS_LI_ABORTED;
  1121. IOP_UNLOCK(lip);
  1122. xfs_trans_free_item_desc(lidp);
  1123. }
  1124. }
  1125. /*
  1126. * Unlock the items associated with a transaction.
  1127. *
  1128. * Items which were not logged should be freed. Those which were logged must
  1129. * still be tracked so they can be unpinned when the transaction commits.
  1130. */
  1131. STATIC void
  1132. xfs_trans_unlock_items(
  1133. struct xfs_trans *tp,
  1134. xfs_lsn_t commit_lsn)
  1135. {
  1136. struct xfs_log_item_desc *lidp, *next;
  1137. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1138. struct xfs_log_item *lip = lidp->lid_item;
  1139. lip->li_desc = NULL;
  1140. if (commit_lsn != NULLCOMMITLSN)
  1141. IOP_COMMITTING(lip, commit_lsn);
  1142. IOP_UNLOCK(lip);
  1143. /*
  1144. * Free the descriptor if the item is not dirty
  1145. * within this transaction.
  1146. */
  1147. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1148. xfs_trans_free_item_desc(lidp);
  1149. }
  1150. }
  1151. /*
  1152. * Total up the number of log iovecs needed to commit this
  1153. * transaction. The transaction itself needs one for the
  1154. * transaction header. Ask each dirty item in turn how many
  1155. * it needs to get the total.
  1156. */
  1157. static uint
  1158. xfs_trans_count_vecs(
  1159. struct xfs_trans *tp)
  1160. {
  1161. int nvecs;
  1162. struct xfs_log_item_desc *lidp;
  1163. nvecs = 1;
  1164. /* In the non-debug case we need to start bailing out if we
  1165. * didn't find a log_item here, return zero and let trans_commit
  1166. * deal with it.
  1167. */
  1168. if (list_empty(&tp->t_items)) {
  1169. ASSERT(0);
  1170. return 0;
  1171. }
  1172. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  1173. /*
  1174. * Skip items which aren't dirty in this transaction.
  1175. */
  1176. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1177. continue;
  1178. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  1179. nvecs += lidp->lid_size;
  1180. }
  1181. return nvecs;
  1182. }
  1183. /*
  1184. * Fill in the vector with pointers to data to be logged
  1185. * by this transaction. The transaction header takes
  1186. * the first vector, and then each dirty item takes the
  1187. * number of vectors it indicated it needed in xfs_trans_count_vecs().
  1188. *
  1189. * As each item fills in the entries it needs, also pin the item
  1190. * so that it cannot be flushed out until the log write completes.
  1191. */
  1192. static void
  1193. xfs_trans_fill_vecs(
  1194. struct xfs_trans *tp,
  1195. struct xfs_log_iovec *log_vector)
  1196. {
  1197. struct xfs_log_item_desc *lidp;
  1198. struct xfs_log_iovec *vecp;
  1199. uint nitems;
  1200. /*
  1201. * Skip over the entry for the transaction header, we'll
  1202. * fill that in at the end.
  1203. */
  1204. vecp = log_vector + 1;
  1205. nitems = 0;
  1206. ASSERT(!list_empty(&tp->t_items));
  1207. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  1208. /* Skip items which aren't dirty in this transaction. */
  1209. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1210. continue;
  1211. /*
  1212. * The item may be marked dirty but not log anything. This can
  1213. * be used to get called when a transaction is committed.
  1214. */
  1215. if (lidp->lid_size)
  1216. nitems++;
  1217. IOP_FORMAT(lidp->lid_item, vecp);
  1218. vecp += lidp->lid_size;
  1219. IOP_PIN(lidp->lid_item);
  1220. }
  1221. /*
  1222. * Now that we've counted the number of items in this transaction, fill
  1223. * in the transaction header. Note that the transaction header does not
  1224. * have a log item.
  1225. */
  1226. tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
  1227. tp->t_header.th_type = tp->t_type;
  1228. tp->t_header.th_num_items = nitems;
  1229. log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
  1230. log_vector->i_len = sizeof(xfs_trans_header_t);
  1231. log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
  1232. }
  1233. /*
  1234. * The committed item processing consists of calling the committed routine of
  1235. * each logged item, updating the item's position in the AIL if necessary, and
  1236. * unpinning each item. If the committed routine returns -1, then do nothing
  1237. * further with the item because it may have been freed.
  1238. *
  1239. * Since items are unlocked when they are copied to the incore log, it is
  1240. * possible for two transactions to be completing and manipulating the same
  1241. * item simultaneously. The AIL lock will protect the lsn field of each item.
  1242. * The value of this field can never go backwards.
  1243. *
  1244. * We unpin the items after repositioning them in the AIL, because otherwise
  1245. * they could be immediately flushed and we'd have to race with the flusher
  1246. * trying to pull the item from the AIL as we add it.
  1247. */
  1248. static void
  1249. xfs_trans_item_committed(
  1250. struct xfs_log_item *lip,
  1251. xfs_lsn_t commit_lsn,
  1252. int aborted)
  1253. {
  1254. xfs_lsn_t item_lsn;
  1255. struct xfs_ail *ailp;
  1256. if (aborted)
  1257. lip->li_flags |= XFS_LI_ABORTED;
  1258. item_lsn = IOP_COMMITTED(lip, commit_lsn);
  1259. /* item_lsn of -1 means the item needs no further processing */
  1260. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  1261. return;
  1262. /*
  1263. * If the returned lsn is greater than what it contained before, update
  1264. * the location of the item in the AIL. If it is not, then do nothing.
  1265. * Items can never move backwards in the AIL.
  1266. *
  1267. * While the new lsn should usually be greater, it is possible that a
  1268. * later transaction completing simultaneously with an earlier one
  1269. * using the same item could complete first with a higher lsn. This
  1270. * would cause the earlier transaction to fail the test below.
  1271. */
  1272. ailp = lip->li_ailp;
  1273. spin_lock(&ailp->xa_lock);
  1274. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
  1275. /*
  1276. * This will set the item's lsn to item_lsn and update the
  1277. * position of the item in the AIL.
  1278. *
  1279. * xfs_trans_ail_update() drops the AIL lock.
  1280. */
  1281. xfs_trans_ail_update(ailp, lip, item_lsn);
  1282. } else {
  1283. spin_unlock(&ailp->xa_lock);
  1284. }
  1285. /*
  1286. * Now that we've repositioned the item in the AIL, unpin it so it can
  1287. * be flushed. Pass information about buffer stale state down from the
  1288. * log item flags, if anyone else stales the buffer we do not want to
  1289. * pay any attention to it.
  1290. */
  1291. IOP_UNPIN(lip, 0);
  1292. }
  1293. /*
  1294. * This is typically called by the LM when a transaction has been fully
  1295. * committed to disk. It needs to unpin the items which have
  1296. * been logged by the transaction and update their positions
  1297. * in the AIL if necessary.
  1298. *
  1299. * This also gets called when the transactions didn't get written out
  1300. * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
  1301. */
  1302. STATIC void
  1303. xfs_trans_committed(
  1304. void *arg,
  1305. int abortflag)
  1306. {
  1307. struct xfs_trans *tp = arg;
  1308. struct xfs_log_item_desc *lidp, *next;
  1309. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1310. xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
  1311. xfs_trans_free_item_desc(lidp);
  1312. }
  1313. xfs_trans_free(tp);
  1314. }
  1315. static inline void
  1316. xfs_log_item_batch_insert(
  1317. struct xfs_ail *ailp,
  1318. struct xfs_ail_cursor *cur,
  1319. struct xfs_log_item **log_items,
  1320. int nr_items,
  1321. xfs_lsn_t commit_lsn)
  1322. {
  1323. int i;
  1324. spin_lock(&ailp->xa_lock);
  1325. /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
  1326. xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
  1327. for (i = 0; i < nr_items; i++)
  1328. IOP_UNPIN(log_items[i], 0);
  1329. }
  1330. /*
  1331. * Bulk operation version of xfs_trans_committed that takes a log vector of
  1332. * items to insert into the AIL. This uses bulk AIL insertion techniques to
  1333. * minimise lock traffic.
  1334. *
  1335. * If we are called with the aborted flag set, it is because a log write during
  1336. * a CIL checkpoint commit has failed. In this case, all the items in the
  1337. * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
  1338. * means that checkpoint commit abort handling is treated exactly the same
  1339. * as an iclog write error even though we haven't started any IO yet. Hence in
  1340. * this case all we need to do is IOP_COMMITTED processing, followed by an
  1341. * IOP_UNPIN(aborted) call.
  1342. *
  1343. * The AIL cursor is used to optimise the insert process. If commit_lsn is not
  1344. * at the end of the AIL, the insert cursor avoids the need to walk
  1345. * the AIL to find the insertion point on every xfs_log_item_batch_insert()
  1346. * call. This saves a lot of needless list walking and is a net win, even
  1347. * though it slightly increases that amount of AIL lock traffic to set it up
  1348. * and tear it down.
  1349. */
  1350. void
  1351. xfs_trans_committed_bulk(
  1352. struct xfs_ail *ailp,
  1353. struct xfs_log_vec *log_vector,
  1354. xfs_lsn_t commit_lsn,
  1355. int aborted)
  1356. {
  1357. #define LOG_ITEM_BATCH_SIZE 32
  1358. struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
  1359. struct xfs_log_vec *lv;
  1360. struct xfs_ail_cursor cur;
  1361. int i = 0;
  1362. spin_lock(&ailp->xa_lock);
  1363. xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
  1364. spin_unlock(&ailp->xa_lock);
  1365. /* unpin all the log items */
  1366. for (lv = log_vector; lv; lv = lv->lv_next ) {
  1367. struct xfs_log_item *lip = lv->lv_item;
  1368. xfs_lsn_t item_lsn;
  1369. if (aborted)
  1370. lip->li_flags |= XFS_LI_ABORTED;
  1371. item_lsn = IOP_COMMITTED(lip, commit_lsn);
  1372. /* item_lsn of -1 means the item needs no further processing */
  1373. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  1374. continue;
  1375. /*
  1376. * if we are aborting the operation, no point in inserting the
  1377. * object into the AIL as we are in a shutdown situation.
  1378. */
  1379. if (aborted) {
  1380. ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
  1381. IOP_UNPIN(lip, 1);
  1382. continue;
  1383. }
  1384. if (item_lsn != commit_lsn) {
  1385. /*
  1386. * Not a bulk update option due to unusual item_lsn.
  1387. * Push into AIL immediately, rechecking the lsn once
  1388. * we have the ail lock. Then unpin the item. This does
  1389. * not affect the AIL cursor the bulk insert path is
  1390. * using.
  1391. */
  1392. spin_lock(&ailp->xa_lock);
  1393. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
  1394. xfs_trans_ail_update(ailp, lip, item_lsn);
  1395. else
  1396. spin_unlock(&ailp->xa_lock);
  1397. IOP_UNPIN(lip, 0);
  1398. continue;
  1399. }
  1400. /* Item is a candidate for bulk AIL insert. */
  1401. log_items[i++] = lv->lv_item;
  1402. if (i >= LOG_ITEM_BATCH_SIZE) {
  1403. xfs_log_item_batch_insert(ailp, &cur, log_items,
  1404. LOG_ITEM_BATCH_SIZE, commit_lsn);
  1405. i = 0;
  1406. }
  1407. }
  1408. /* make sure we insert the remainder! */
  1409. if (i)
  1410. xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
  1411. spin_lock(&ailp->xa_lock);
  1412. xfs_trans_ail_cursor_done(ailp, &cur);
  1413. spin_unlock(&ailp->xa_lock);
  1414. }
  1415. /*
  1416. * Called from the trans_commit code when we notice that the filesystem is in
  1417. * the middle of a forced shutdown.
  1418. *
  1419. * When we are called here, we have already pinned all the items in the
  1420. * transaction. However, neither IOP_COMMITTING or IOP_UNLOCK has been called
  1421. * so we can simply walk the items in the transaction, unpin them with an abort
  1422. * flag and then free the items. Note that unpinning the items can result in
  1423. * them being freed immediately, so we need to use a safe list traversal method
  1424. * here.
  1425. */
  1426. STATIC void
  1427. xfs_trans_uncommit(
  1428. struct xfs_trans *tp,
  1429. uint flags)
  1430. {
  1431. struct xfs_log_item_desc *lidp, *n;
  1432. list_for_each_entry_safe(lidp, n, &tp->t_items, lid_trans) {
  1433. if (lidp->lid_flags & XFS_LID_DIRTY)
  1434. IOP_UNPIN(lidp->lid_item, 1);
  1435. }
  1436. xfs_trans_unreserve_and_mod_sb(tp);
  1437. xfs_trans_unreserve_and_mod_dquots(tp);
  1438. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1439. xfs_trans_free(tp);
  1440. }
  1441. /*
  1442. * Format the transaction direct to the iclog. This isolates the physical
  1443. * transaction commit operation from the logical operation and hence allows
  1444. * other methods to be introduced without affecting the existing commit path.
  1445. */
  1446. static int
  1447. xfs_trans_commit_iclog(
  1448. struct xfs_mount *mp,
  1449. struct xfs_trans *tp,
  1450. xfs_lsn_t *commit_lsn,
  1451. int flags)
  1452. {
  1453. int shutdown;
  1454. int error;
  1455. int log_flags = 0;
  1456. struct xlog_in_core *commit_iclog;
  1457. #define XFS_TRANS_LOGVEC_COUNT 16
  1458. struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
  1459. struct xfs_log_iovec *log_vector;
  1460. uint nvec;
  1461. /*
  1462. * Ask each log item how many log_vector entries it will
  1463. * need so we can figure out how many to allocate.
  1464. * Try to avoid the kmem_alloc() call in the common case
  1465. * by using a vector from the stack when it fits.
  1466. */
  1467. nvec = xfs_trans_count_vecs(tp);
  1468. if (nvec == 0) {
  1469. return ENOMEM; /* triggers a shutdown! */
  1470. } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
  1471. log_vector = log_vector_fast;
  1472. } else {
  1473. log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
  1474. sizeof(xfs_log_iovec_t),
  1475. KM_SLEEP);
  1476. }
  1477. /*
  1478. * Fill in the log_vector and pin the logged items, and
  1479. * then write the transaction to the log.
  1480. */
  1481. xfs_trans_fill_vecs(tp, log_vector);
  1482. if (flags & XFS_TRANS_RELEASE_LOG_RES)
  1483. log_flags = XFS_LOG_REL_PERM_RESERV;
  1484. error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
  1485. /*
  1486. * The transaction is committed incore here, and can go out to disk
  1487. * at any time after this call. However, all the items associated
  1488. * with the transaction are still locked and pinned in memory.
  1489. */
  1490. *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
  1491. tp->t_commit_lsn = *commit_lsn;
  1492. trace_xfs_trans_commit_lsn(tp);
  1493. if (nvec > XFS_TRANS_LOGVEC_COUNT)
  1494. kmem_free(log_vector);
  1495. /*
  1496. * If we got a log write error. Unpin the logitems that we
  1497. * had pinned, clean up, free trans structure, and return error.
  1498. */
  1499. if (error || *commit_lsn == -1) {
  1500. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1501. xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
  1502. return XFS_ERROR(EIO);
  1503. }
  1504. /*
  1505. * Once the transaction has committed, unused
  1506. * reservations need to be released and changes to
  1507. * the superblock need to be reflected in the in-core
  1508. * version. Do that now.
  1509. */
  1510. xfs_trans_unreserve_and_mod_sb(tp);
  1511. /*
  1512. * Tell the LM to call the transaction completion routine
  1513. * when the log write with LSN commit_lsn completes (e.g.
  1514. * when the transaction commit really hits the on-disk log).
  1515. * After this call we cannot reference tp, because the call
  1516. * can happen at any time and the call will free the transaction
  1517. * structure pointed to by tp. The only case where we call
  1518. * the completion routine (xfs_trans_committed) directly is
  1519. * if the log is turned off on a debug kernel or we're
  1520. * running in simulation mode (the log is explicitly turned
  1521. * off).
  1522. */
  1523. tp->t_logcb.cb_func = xfs_trans_committed;
  1524. tp->t_logcb.cb_arg = tp;
  1525. /*
  1526. * We need to pass the iclog buffer which was used for the
  1527. * transaction commit record into this function, and attach
  1528. * the callback to it. The callback must be attached before
  1529. * the items are unlocked to avoid racing with other threads
  1530. * waiting for an item to unlock.
  1531. */
  1532. shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
  1533. /*
  1534. * Mark this thread as no longer being in a transaction
  1535. */
  1536. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1537. /*
  1538. * Once all the items of the transaction have been copied
  1539. * to the in core log and the callback is attached, the
  1540. * items can be unlocked.
  1541. *
  1542. * This will free descriptors pointing to items which were
  1543. * not logged since there is nothing more to do with them.
  1544. * For items which were logged, we will keep pointers to them
  1545. * so they can be unpinned after the transaction commits to disk.
  1546. * This will also stamp each modified meta-data item with
  1547. * the commit lsn of this transaction for dependency tracking
  1548. * purposes.
  1549. */
  1550. xfs_trans_unlock_items(tp, *commit_lsn);
  1551. /*
  1552. * If we detected a log error earlier, finish committing
  1553. * the transaction now (unpin log items, etc).
  1554. *
  1555. * Order is critical here, to avoid using the transaction
  1556. * pointer after its been freed (by xfs_trans_committed
  1557. * either here now, or as a callback). We cannot do this
  1558. * step inside xfs_log_notify as was done earlier because
  1559. * of this issue.
  1560. */
  1561. if (shutdown)
  1562. xfs_trans_committed(tp, XFS_LI_ABORTED);
  1563. /*
  1564. * Now that the xfs_trans_committed callback has been attached,
  1565. * and the items are released we can finally allow the iclog to
  1566. * go to disk.
  1567. */
  1568. return xfs_log_release_iclog(mp, commit_iclog);
  1569. }
  1570. /*
  1571. * Walk the log items and allocate log vector structures for
  1572. * each item large enough to fit all the vectors they require.
  1573. * Note that this format differs from the old log vector format in
  1574. * that there is no transaction header in these log vectors.
  1575. */
  1576. STATIC struct xfs_log_vec *
  1577. xfs_trans_alloc_log_vecs(
  1578. xfs_trans_t *tp)
  1579. {
  1580. struct xfs_log_item_desc *lidp;
  1581. struct xfs_log_vec *lv = NULL;
  1582. struct xfs_log_vec *ret_lv = NULL;
  1583. /* Bail out if we didn't find a log item. */
  1584. if (list_empty(&tp->t_items)) {
  1585. ASSERT(0);
  1586. return NULL;
  1587. }
  1588. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  1589. struct xfs_log_vec *new_lv;
  1590. /* Skip items which aren't dirty in this transaction. */
  1591. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1592. continue;
  1593. /* Skip items that do not have any vectors for writing */
  1594. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  1595. if (!lidp->lid_size)
  1596. continue;
  1597. new_lv = kmem_zalloc(sizeof(*new_lv) +
  1598. lidp->lid_size * sizeof(struct xfs_log_iovec),
  1599. KM_SLEEP);
  1600. /* The allocated iovec region lies beyond the log vector. */
  1601. new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
  1602. new_lv->lv_niovecs = lidp->lid_size;
  1603. new_lv->lv_item = lidp->lid_item;
  1604. if (!ret_lv)
  1605. ret_lv = new_lv;
  1606. else
  1607. lv->lv_next = new_lv;
  1608. lv = new_lv;
  1609. }
  1610. return ret_lv;
  1611. }
  1612. static int
  1613. xfs_trans_commit_cil(
  1614. struct xfs_mount *mp,
  1615. struct xfs_trans *tp,
  1616. xfs_lsn_t *commit_lsn,
  1617. int flags)
  1618. {
  1619. struct xfs_log_vec *log_vector;
  1620. /*
  1621. * Get each log item to allocate a vector structure for
  1622. * the log item to to pass to the log write code. The
  1623. * CIL commit code will format the vector and save it away.
  1624. */
  1625. log_vector = xfs_trans_alloc_log_vecs(tp);
  1626. if (!log_vector)
  1627. return ENOMEM;
  1628. xfs_log_commit_cil(mp, tp, log_vector, commit_lsn, flags);
  1629. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1630. xfs_trans_free(tp);
  1631. return 0;
  1632. }
  1633. /*
  1634. * xfs_trans_commit
  1635. *
  1636. * Commit the given transaction to the log a/synchronously.
  1637. *
  1638. * XFS disk error handling mechanism is not based on a typical
  1639. * transaction abort mechanism. Logically after the filesystem
  1640. * gets marked 'SHUTDOWN', we can't let any new transactions
  1641. * be durable - ie. committed to disk - because some metadata might
  1642. * be inconsistent. In such cases, this returns an error, and the
  1643. * caller may assume that all locked objects joined to the transaction
  1644. * have already been unlocked as if the commit had succeeded.
  1645. * Do not reference the transaction structure after this call.
  1646. */
  1647. int
  1648. _xfs_trans_commit(
  1649. struct xfs_trans *tp,
  1650. uint flags,
  1651. int *log_flushed)
  1652. {
  1653. struct xfs_mount *mp = tp->t_mountp;
  1654. xfs_lsn_t commit_lsn = -1;
  1655. int error = 0;
  1656. int log_flags = 0;
  1657. int sync = tp->t_flags & XFS_TRANS_SYNC;
  1658. /*
  1659. * Determine whether this commit is releasing a permanent
  1660. * log reservation or not.
  1661. */
  1662. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1663. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1664. log_flags = XFS_LOG_REL_PERM_RESERV;
  1665. }
  1666. /*
  1667. * If there is nothing to be logged by the transaction,
  1668. * then unlock all of the items associated with the
  1669. * transaction and free the transaction structure.
  1670. * Also make sure to return any reserved blocks to
  1671. * the free pool.
  1672. */
  1673. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  1674. goto out_unreserve;
  1675. if (XFS_FORCED_SHUTDOWN(mp)) {
  1676. error = XFS_ERROR(EIO);
  1677. goto out_unreserve;
  1678. }
  1679. ASSERT(tp->t_ticket != NULL);
  1680. /*
  1681. * If we need to update the superblock, then do it now.
  1682. */
  1683. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  1684. xfs_trans_apply_sb_deltas(tp);
  1685. xfs_trans_apply_dquot_deltas(tp);
  1686. if (mp->m_flags & XFS_MOUNT_DELAYLOG)
  1687. error = xfs_trans_commit_cil(mp, tp, &commit_lsn, flags);
  1688. else
  1689. error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
  1690. if (error == ENOMEM) {
  1691. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  1692. error = XFS_ERROR(EIO);
  1693. goto out_unreserve;
  1694. }
  1695. /*
  1696. * If the transaction needs to be synchronous, then force the
  1697. * log out now and wait for it.
  1698. */
  1699. if (sync) {
  1700. if (!error) {
  1701. error = _xfs_log_force_lsn(mp, commit_lsn,
  1702. XFS_LOG_SYNC, log_flushed);
  1703. }
  1704. XFS_STATS_INC(xs_trans_sync);
  1705. } else {
  1706. XFS_STATS_INC(xs_trans_async);
  1707. }
  1708. return error;
  1709. out_unreserve:
  1710. xfs_trans_unreserve_and_mod_sb(tp);
  1711. /*
  1712. * It is indeed possible for the transaction to be not dirty but
  1713. * the dqinfo portion to be. All that means is that we have some
  1714. * (non-persistent) quota reservations that need to be unreserved.
  1715. */
  1716. xfs_trans_unreserve_and_mod_dquots(tp);
  1717. if (tp->t_ticket) {
  1718. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1719. if (commit_lsn == -1 && !error)
  1720. error = XFS_ERROR(EIO);
  1721. }
  1722. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1723. xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
  1724. xfs_trans_free(tp);
  1725. XFS_STATS_INC(xs_trans_empty);
  1726. return error;
  1727. }
  1728. /*
  1729. * Unlock all of the transaction's items and free the transaction.
  1730. * The transaction must not have modified any of its items, because
  1731. * there is no way to restore them to their previous state.
  1732. *
  1733. * If the transaction has made a log reservation, make sure to release
  1734. * it as well.
  1735. */
  1736. void
  1737. xfs_trans_cancel(
  1738. xfs_trans_t *tp,
  1739. int flags)
  1740. {
  1741. int log_flags;
  1742. xfs_mount_t *mp = tp->t_mountp;
  1743. /*
  1744. * See if the caller is being too lazy to figure out if
  1745. * the transaction really needs an abort.
  1746. */
  1747. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  1748. flags &= ~XFS_TRANS_ABORT;
  1749. /*
  1750. * See if the caller is relying on us to shut down the
  1751. * filesystem. This happens in paths where we detect
  1752. * corruption and decide to give up.
  1753. */
  1754. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  1755. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  1756. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1757. }
  1758. #ifdef DEBUG
  1759. if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
  1760. struct xfs_log_item_desc *lidp;
  1761. list_for_each_entry(lidp, &tp->t_items, lid_trans)
  1762. ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
  1763. }
  1764. #endif
  1765. xfs_trans_unreserve_and_mod_sb(tp);
  1766. xfs_trans_unreserve_and_mod_dquots(tp);
  1767. if (tp->t_ticket) {
  1768. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1769. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1770. log_flags = XFS_LOG_REL_PERM_RESERV;
  1771. } else {
  1772. log_flags = 0;
  1773. }
  1774. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1775. }
  1776. /* mark this thread as no longer being in a transaction */
  1777. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1778. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1779. xfs_trans_free(tp);
  1780. }
  1781. /*
  1782. * Roll from one trans in the sequence of PERMANENT transactions to
  1783. * the next: permanent transactions are only flushed out when
  1784. * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
  1785. * as possible to let chunks of it go to the log. So we commit the
  1786. * chunk we've been working on and get a new transaction to continue.
  1787. */
  1788. int
  1789. xfs_trans_roll(
  1790. struct xfs_trans **tpp,
  1791. struct xfs_inode *dp)
  1792. {
  1793. struct xfs_trans *trans;
  1794. unsigned int logres, count;
  1795. int error;
  1796. /*
  1797. * Ensure that the inode is always logged.
  1798. */
  1799. trans = *tpp;
  1800. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  1801. /*
  1802. * Copy the critical parameters from one trans to the next.
  1803. */
  1804. logres = trans->t_log_res;
  1805. count = trans->t_log_count;
  1806. *tpp = xfs_trans_dup(trans);
  1807. /*
  1808. * Commit the current transaction.
  1809. * If this commit failed, then it'd just unlock those items that
  1810. * are not marked ihold. That also means that a filesystem shutdown
  1811. * is in progress. The caller takes the responsibility to cancel
  1812. * the duplicate transaction that gets returned.
  1813. */
  1814. error = xfs_trans_commit(trans, 0);
  1815. if (error)
  1816. return (error);
  1817. trans = *tpp;
  1818. /*
  1819. * transaction commit worked ok so we can drop the extra ticket
  1820. * reference that we gained in xfs_trans_dup()
  1821. */
  1822. xfs_log_ticket_put(trans->t_ticket);
  1823. /*
  1824. * Reserve space in the log for th next transaction.
  1825. * This also pushes items in the "AIL", the list of logged items,
  1826. * out to disk if they are taking up space at the tail of the log
  1827. * that we want to use. This requires that either nothing be locked
  1828. * across this call, or that anything that is locked be logged in
  1829. * the prior and the next transactions.
  1830. */
  1831. error = xfs_trans_reserve(trans, 0, logres, 0,
  1832. XFS_TRANS_PERM_LOG_RES, count);
  1833. /*
  1834. * Ensure that the inode is in the new transaction and locked.
  1835. */
  1836. if (error)
  1837. return error;
  1838. xfs_trans_ijoin(trans, dp);
  1839. return 0;
  1840. }