xfs_trans.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  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. *
  541. * The type parameter indicates the type of the transaction. These
  542. * are enumerated in xfs_trans.h.
  543. */
  544. struct xfs_trans *
  545. _xfs_trans_alloc(
  546. struct xfs_mount *mp,
  547. uint type,
  548. uint memflags,
  549. bool wait_for_freeze)
  550. {
  551. struct xfs_trans *tp;
  552. atomic_inc(&mp->m_active_trans);
  553. if (wait_for_freeze)
  554. xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
  555. tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
  556. tp->t_magic = XFS_TRANS_MAGIC;
  557. tp->t_type = type;
  558. tp->t_mountp = mp;
  559. INIT_LIST_HEAD(&tp->t_items);
  560. INIT_LIST_HEAD(&tp->t_busy);
  561. return tp;
  562. }
  563. /*
  564. * Free the transaction structure. If there is more clean up
  565. * to do when the structure is freed, add it here.
  566. */
  567. STATIC void
  568. xfs_trans_free(
  569. struct xfs_trans *tp)
  570. {
  571. xfs_alloc_busy_sort(&tp->t_busy);
  572. xfs_alloc_busy_clear(tp->t_mountp, &tp->t_busy, false);
  573. atomic_dec(&tp->t_mountp->m_active_trans);
  574. xfs_trans_free_dqinfo(tp);
  575. kmem_zone_free(xfs_trans_zone, tp);
  576. }
  577. /*
  578. * This is called to create a new transaction which will share the
  579. * permanent log reservation of the given transaction. The remaining
  580. * unused block and rt extent reservations are also inherited. This
  581. * implies that the original transaction is no longer allowed to allocate
  582. * blocks. Locks and log items, however, are no inherited. They must
  583. * be added to the new transaction explicitly.
  584. */
  585. xfs_trans_t *
  586. xfs_trans_dup(
  587. xfs_trans_t *tp)
  588. {
  589. xfs_trans_t *ntp;
  590. ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  591. /*
  592. * Initialize the new transaction structure.
  593. */
  594. ntp->t_magic = XFS_TRANS_MAGIC;
  595. ntp->t_type = tp->t_type;
  596. ntp->t_mountp = tp->t_mountp;
  597. INIT_LIST_HEAD(&ntp->t_items);
  598. INIT_LIST_HEAD(&ntp->t_busy);
  599. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  600. ASSERT(tp->t_ticket != NULL);
  601. ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
  602. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  603. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  604. tp->t_blk_res = tp->t_blk_res_used;
  605. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  606. tp->t_rtx_res = tp->t_rtx_res_used;
  607. ntp->t_pflags = tp->t_pflags;
  608. xfs_trans_dup_dqinfo(tp, ntp);
  609. atomic_inc(&tp->t_mountp->m_active_trans);
  610. return ntp;
  611. }
  612. /*
  613. * This is called to reserve free disk blocks and log space for the
  614. * given transaction. This must be done before allocating any resources
  615. * within the transaction.
  616. *
  617. * This will return ENOSPC if there are not enough blocks available.
  618. * It will sleep waiting for available log space.
  619. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  620. * is used by long running transactions. If any one of the reservations
  621. * fails then they will all be backed out.
  622. *
  623. * This does not do quota reservations. That typically is done by the
  624. * caller afterwards.
  625. */
  626. int
  627. xfs_trans_reserve(
  628. xfs_trans_t *tp,
  629. uint blocks,
  630. uint logspace,
  631. uint rtextents,
  632. uint flags,
  633. uint logcount)
  634. {
  635. int log_flags;
  636. int error = 0;
  637. int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  638. /* Mark this thread as being in a transaction */
  639. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  640. /*
  641. * Attempt to reserve the needed disk blocks by decrementing
  642. * the number needed from the number available. This will
  643. * fail if the count would go below zero.
  644. */
  645. if (blocks > 0) {
  646. error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  647. -((int64_t)blocks), rsvd);
  648. if (error != 0) {
  649. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  650. return (XFS_ERROR(ENOSPC));
  651. }
  652. tp->t_blk_res += blocks;
  653. }
  654. /*
  655. * Reserve the log space needed for this transaction.
  656. */
  657. if (logspace > 0) {
  658. ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
  659. ASSERT((tp->t_log_count == 0) ||
  660. (tp->t_log_count == logcount));
  661. if (flags & XFS_TRANS_PERM_LOG_RES) {
  662. log_flags = XFS_LOG_PERM_RESERV;
  663. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  664. } else {
  665. ASSERT(tp->t_ticket == NULL);
  666. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  667. log_flags = 0;
  668. }
  669. error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
  670. &tp->t_ticket,
  671. XFS_TRANSACTION, log_flags, tp->t_type);
  672. if (error) {
  673. goto undo_blocks;
  674. }
  675. tp->t_log_res = logspace;
  676. tp->t_log_count = logcount;
  677. }
  678. /*
  679. * Attempt to reserve the needed realtime extents by decrementing
  680. * the number needed from the number available. This will
  681. * fail if the count would go below zero.
  682. */
  683. if (rtextents > 0) {
  684. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
  685. -((int64_t)rtextents), rsvd);
  686. if (error) {
  687. error = XFS_ERROR(ENOSPC);
  688. goto undo_log;
  689. }
  690. tp->t_rtx_res += rtextents;
  691. }
  692. return 0;
  693. /*
  694. * Error cases jump to one of these labels to undo any
  695. * reservations which have already been performed.
  696. */
  697. undo_log:
  698. if (logspace > 0) {
  699. if (flags & XFS_TRANS_PERM_LOG_RES) {
  700. log_flags = XFS_LOG_REL_PERM_RESERV;
  701. } else {
  702. log_flags = 0;
  703. }
  704. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  705. tp->t_ticket = NULL;
  706. tp->t_log_res = 0;
  707. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  708. }
  709. undo_blocks:
  710. if (blocks > 0) {
  711. xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  712. (int64_t)blocks, rsvd);
  713. tp->t_blk_res = 0;
  714. }
  715. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  716. return error;
  717. }
  718. /*
  719. * Record the indicated change to the given field for application
  720. * to the file system's superblock when the transaction commits.
  721. * For now, just store the change in the transaction structure.
  722. *
  723. * Mark the transaction structure to indicate that the superblock
  724. * needs to be updated before committing.
  725. *
  726. * Because we may not be keeping track of allocated/free inodes and
  727. * used filesystem blocks in the superblock, we do not mark the
  728. * superblock dirty in this transaction if we modify these fields.
  729. * We still need to update the transaction deltas so that they get
  730. * applied to the incore superblock, but we don't want them to
  731. * cause the superblock to get locked and logged if these are the
  732. * only fields in the superblock that the transaction modifies.
  733. */
  734. void
  735. xfs_trans_mod_sb(
  736. xfs_trans_t *tp,
  737. uint field,
  738. int64_t delta)
  739. {
  740. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  741. xfs_mount_t *mp = tp->t_mountp;
  742. switch (field) {
  743. case XFS_TRANS_SB_ICOUNT:
  744. tp->t_icount_delta += delta;
  745. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  746. flags &= ~XFS_TRANS_SB_DIRTY;
  747. break;
  748. case XFS_TRANS_SB_IFREE:
  749. tp->t_ifree_delta += delta;
  750. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  751. flags &= ~XFS_TRANS_SB_DIRTY;
  752. break;
  753. case XFS_TRANS_SB_FDBLOCKS:
  754. /*
  755. * Track the number of blocks allocated in the
  756. * transaction. Make sure it does not exceed the
  757. * number reserved.
  758. */
  759. if (delta < 0) {
  760. tp->t_blk_res_used += (uint)-delta;
  761. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  762. }
  763. tp->t_fdblocks_delta += delta;
  764. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  765. flags &= ~XFS_TRANS_SB_DIRTY;
  766. break;
  767. case XFS_TRANS_SB_RES_FDBLOCKS:
  768. /*
  769. * The allocation has already been applied to the
  770. * in-core superblock's counter. This should only
  771. * be applied to the on-disk superblock.
  772. */
  773. ASSERT(delta < 0);
  774. tp->t_res_fdblocks_delta += delta;
  775. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  776. flags &= ~XFS_TRANS_SB_DIRTY;
  777. break;
  778. case XFS_TRANS_SB_FREXTENTS:
  779. /*
  780. * Track the number of blocks allocated in the
  781. * transaction. Make sure it does not exceed the
  782. * number reserved.
  783. */
  784. if (delta < 0) {
  785. tp->t_rtx_res_used += (uint)-delta;
  786. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  787. }
  788. tp->t_frextents_delta += delta;
  789. break;
  790. case XFS_TRANS_SB_RES_FREXTENTS:
  791. /*
  792. * The allocation has already been applied to the
  793. * in-core superblock's counter. This should only
  794. * be applied to the on-disk superblock.
  795. */
  796. ASSERT(delta < 0);
  797. tp->t_res_frextents_delta += delta;
  798. break;
  799. case XFS_TRANS_SB_DBLOCKS:
  800. ASSERT(delta > 0);
  801. tp->t_dblocks_delta += delta;
  802. break;
  803. case XFS_TRANS_SB_AGCOUNT:
  804. ASSERT(delta > 0);
  805. tp->t_agcount_delta += delta;
  806. break;
  807. case XFS_TRANS_SB_IMAXPCT:
  808. tp->t_imaxpct_delta += delta;
  809. break;
  810. case XFS_TRANS_SB_REXTSIZE:
  811. tp->t_rextsize_delta += delta;
  812. break;
  813. case XFS_TRANS_SB_RBMBLOCKS:
  814. tp->t_rbmblocks_delta += delta;
  815. break;
  816. case XFS_TRANS_SB_RBLOCKS:
  817. tp->t_rblocks_delta += delta;
  818. break;
  819. case XFS_TRANS_SB_REXTENTS:
  820. tp->t_rextents_delta += delta;
  821. break;
  822. case XFS_TRANS_SB_REXTSLOG:
  823. tp->t_rextslog_delta += delta;
  824. break;
  825. default:
  826. ASSERT(0);
  827. return;
  828. }
  829. tp->t_flags |= flags;
  830. }
  831. /*
  832. * xfs_trans_apply_sb_deltas() is called from the commit code
  833. * to bring the superblock buffer into the current transaction
  834. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  835. *
  836. * For now we just look at each field allowed to change and change
  837. * it if necessary.
  838. */
  839. STATIC void
  840. xfs_trans_apply_sb_deltas(
  841. xfs_trans_t *tp)
  842. {
  843. xfs_dsb_t *sbp;
  844. xfs_buf_t *bp;
  845. int whole = 0;
  846. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  847. sbp = XFS_BUF_TO_SBP(bp);
  848. /*
  849. * Check that superblock mods match the mods made to AGF counters.
  850. */
  851. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  852. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  853. tp->t_ag_btree_delta));
  854. /*
  855. * Only update the superblock counters if we are logging them
  856. */
  857. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  858. if (tp->t_icount_delta)
  859. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  860. if (tp->t_ifree_delta)
  861. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  862. if (tp->t_fdblocks_delta)
  863. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  864. if (tp->t_res_fdblocks_delta)
  865. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  866. }
  867. if (tp->t_frextents_delta)
  868. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  869. if (tp->t_res_frextents_delta)
  870. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  871. if (tp->t_dblocks_delta) {
  872. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  873. whole = 1;
  874. }
  875. if (tp->t_agcount_delta) {
  876. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  877. whole = 1;
  878. }
  879. if (tp->t_imaxpct_delta) {
  880. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  881. whole = 1;
  882. }
  883. if (tp->t_rextsize_delta) {
  884. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  885. whole = 1;
  886. }
  887. if (tp->t_rbmblocks_delta) {
  888. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  889. whole = 1;
  890. }
  891. if (tp->t_rblocks_delta) {
  892. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  893. whole = 1;
  894. }
  895. if (tp->t_rextents_delta) {
  896. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  897. whole = 1;
  898. }
  899. if (tp->t_rextslog_delta) {
  900. sbp->sb_rextslog += tp->t_rextslog_delta;
  901. whole = 1;
  902. }
  903. if (whole)
  904. /*
  905. * Log the whole thing, the fields are noncontiguous.
  906. */
  907. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  908. else
  909. /*
  910. * Since all the modifiable fields are contiguous, we
  911. * can get away with this.
  912. */
  913. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  914. offsetof(xfs_dsb_t, sb_frextents) +
  915. sizeof(sbp->sb_frextents) - 1);
  916. }
  917. /*
  918. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  919. * and apply superblock counter changes to the in-core superblock. The
  920. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  921. * applied to the in-core superblock. The idea is that that has already been
  922. * done.
  923. *
  924. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  925. * However, we have to ensure that we only modify each superblock field only
  926. * once because the application of the delta values may not be atomic. That can
  927. * lead to ENOSPC races occurring if we have two separate modifcations of the
  928. * free space counter to put back the entire reservation and then take away
  929. * what we used.
  930. *
  931. * If we are not logging superblock counters, then the inode allocated/free and
  932. * used block counts are not updated in the on disk superblock. In this case,
  933. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  934. * still need to update the incore superblock with the changes.
  935. */
  936. void
  937. xfs_trans_unreserve_and_mod_sb(
  938. xfs_trans_t *tp)
  939. {
  940. xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
  941. xfs_mod_sb_t *msbp;
  942. xfs_mount_t *mp = tp->t_mountp;
  943. /* REFERENCED */
  944. int error;
  945. int rsvd;
  946. int64_t blkdelta = 0;
  947. int64_t rtxdelta = 0;
  948. int64_t idelta = 0;
  949. int64_t ifreedelta = 0;
  950. msbp = msb;
  951. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  952. /* calculate deltas */
  953. if (tp->t_blk_res > 0)
  954. blkdelta = tp->t_blk_res;
  955. if ((tp->t_fdblocks_delta != 0) &&
  956. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  957. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  958. blkdelta += tp->t_fdblocks_delta;
  959. if (tp->t_rtx_res > 0)
  960. rtxdelta = tp->t_rtx_res;
  961. if ((tp->t_frextents_delta != 0) &&
  962. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  963. rtxdelta += tp->t_frextents_delta;
  964. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  965. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  966. idelta = tp->t_icount_delta;
  967. ifreedelta = tp->t_ifree_delta;
  968. }
  969. /* apply the per-cpu counters */
  970. if (blkdelta) {
  971. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  972. blkdelta, rsvd);
  973. if (error)
  974. goto out;
  975. }
  976. if (idelta) {
  977. error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
  978. idelta, rsvd);
  979. if (error)
  980. goto out_undo_fdblocks;
  981. }
  982. if (ifreedelta) {
  983. error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
  984. ifreedelta, rsvd);
  985. if (error)
  986. goto out_undo_icount;
  987. }
  988. /* apply remaining deltas */
  989. if (rtxdelta != 0) {
  990. msbp->msb_field = XFS_SBS_FREXTENTS;
  991. msbp->msb_delta = rtxdelta;
  992. msbp++;
  993. }
  994. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  995. if (tp->t_dblocks_delta != 0) {
  996. msbp->msb_field = XFS_SBS_DBLOCKS;
  997. msbp->msb_delta = tp->t_dblocks_delta;
  998. msbp++;
  999. }
  1000. if (tp->t_agcount_delta != 0) {
  1001. msbp->msb_field = XFS_SBS_AGCOUNT;
  1002. msbp->msb_delta = tp->t_agcount_delta;
  1003. msbp++;
  1004. }
  1005. if (tp->t_imaxpct_delta != 0) {
  1006. msbp->msb_field = XFS_SBS_IMAX_PCT;
  1007. msbp->msb_delta = tp->t_imaxpct_delta;
  1008. msbp++;
  1009. }
  1010. if (tp->t_rextsize_delta != 0) {
  1011. msbp->msb_field = XFS_SBS_REXTSIZE;
  1012. msbp->msb_delta = tp->t_rextsize_delta;
  1013. msbp++;
  1014. }
  1015. if (tp->t_rbmblocks_delta != 0) {
  1016. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  1017. msbp->msb_delta = tp->t_rbmblocks_delta;
  1018. msbp++;
  1019. }
  1020. if (tp->t_rblocks_delta != 0) {
  1021. msbp->msb_field = XFS_SBS_RBLOCKS;
  1022. msbp->msb_delta = tp->t_rblocks_delta;
  1023. msbp++;
  1024. }
  1025. if (tp->t_rextents_delta != 0) {
  1026. msbp->msb_field = XFS_SBS_REXTENTS;
  1027. msbp->msb_delta = tp->t_rextents_delta;
  1028. msbp++;
  1029. }
  1030. if (tp->t_rextslog_delta != 0) {
  1031. msbp->msb_field = XFS_SBS_REXTSLOG;
  1032. msbp->msb_delta = tp->t_rextslog_delta;
  1033. msbp++;
  1034. }
  1035. }
  1036. /*
  1037. * If we need to change anything, do it.
  1038. */
  1039. if (msbp > msb) {
  1040. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  1041. (uint)(msbp - msb), rsvd);
  1042. if (error)
  1043. goto out_undo_ifreecount;
  1044. }
  1045. return;
  1046. out_undo_ifreecount:
  1047. if (ifreedelta)
  1048. xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
  1049. out_undo_icount:
  1050. if (idelta)
  1051. xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
  1052. out_undo_fdblocks:
  1053. if (blkdelta)
  1054. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
  1055. out:
  1056. ASSERT(error == 0);
  1057. return;
  1058. }
  1059. /*
  1060. * Add the given log item to the transaction's list of log items.
  1061. *
  1062. * The log item will now point to its new descriptor with its li_desc field.
  1063. */
  1064. void
  1065. xfs_trans_add_item(
  1066. struct xfs_trans *tp,
  1067. struct xfs_log_item *lip)
  1068. {
  1069. struct xfs_log_item_desc *lidp;
  1070. ASSERT(lip->li_mountp = tp->t_mountp);
  1071. ASSERT(lip->li_ailp = tp->t_mountp->m_ail);
  1072. lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
  1073. lidp->lid_item = lip;
  1074. lidp->lid_flags = 0;
  1075. lidp->lid_size = 0;
  1076. list_add_tail(&lidp->lid_trans, &tp->t_items);
  1077. lip->li_desc = lidp;
  1078. }
  1079. STATIC void
  1080. xfs_trans_free_item_desc(
  1081. struct xfs_log_item_desc *lidp)
  1082. {
  1083. list_del_init(&lidp->lid_trans);
  1084. kmem_zone_free(xfs_log_item_desc_zone, lidp);
  1085. }
  1086. /*
  1087. * Unlink and free the given descriptor.
  1088. */
  1089. void
  1090. xfs_trans_del_item(
  1091. struct xfs_log_item *lip)
  1092. {
  1093. xfs_trans_free_item_desc(lip->li_desc);
  1094. lip->li_desc = NULL;
  1095. }
  1096. /*
  1097. * Unlock all of the items of a transaction and free all the descriptors
  1098. * of that transaction.
  1099. */
  1100. void
  1101. xfs_trans_free_items(
  1102. struct xfs_trans *tp,
  1103. xfs_lsn_t commit_lsn,
  1104. int flags)
  1105. {
  1106. struct xfs_log_item_desc *lidp, *next;
  1107. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1108. struct xfs_log_item *lip = lidp->lid_item;
  1109. lip->li_desc = NULL;
  1110. if (commit_lsn != NULLCOMMITLSN)
  1111. IOP_COMMITTING(lip, commit_lsn);
  1112. if (flags & XFS_TRANS_ABORT)
  1113. lip->li_flags |= XFS_LI_ABORTED;
  1114. IOP_UNLOCK(lip);
  1115. xfs_trans_free_item_desc(lidp);
  1116. }
  1117. }
  1118. /*
  1119. * Unlock the items associated with a transaction.
  1120. *
  1121. * Items which were not logged should be freed. Those which were logged must
  1122. * still be tracked so they can be unpinned when the transaction commits.
  1123. */
  1124. STATIC void
  1125. xfs_trans_unlock_items(
  1126. struct xfs_trans *tp,
  1127. xfs_lsn_t commit_lsn)
  1128. {
  1129. struct xfs_log_item_desc *lidp, *next;
  1130. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1131. struct xfs_log_item *lip = lidp->lid_item;
  1132. lip->li_desc = NULL;
  1133. if (commit_lsn != NULLCOMMITLSN)
  1134. IOP_COMMITTING(lip, commit_lsn);
  1135. IOP_UNLOCK(lip);
  1136. /*
  1137. * Free the descriptor if the item is not dirty
  1138. * within this transaction.
  1139. */
  1140. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1141. xfs_trans_free_item_desc(lidp);
  1142. }
  1143. }
  1144. /*
  1145. * Total up the number of log iovecs needed to commit this
  1146. * transaction. The transaction itself needs one for the
  1147. * transaction header. Ask each dirty item in turn how many
  1148. * it needs to get the total.
  1149. */
  1150. static uint
  1151. xfs_trans_count_vecs(
  1152. struct xfs_trans *tp)
  1153. {
  1154. int nvecs;
  1155. struct xfs_log_item_desc *lidp;
  1156. nvecs = 1;
  1157. /* In the non-debug case we need to start bailing out if we
  1158. * didn't find a log_item here, return zero and let trans_commit
  1159. * deal with it.
  1160. */
  1161. if (list_empty(&tp->t_items)) {
  1162. ASSERT(0);
  1163. return 0;
  1164. }
  1165. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  1166. /*
  1167. * Skip items which aren't dirty in this transaction.
  1168. */
  1169. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1170. continue;
  1171. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  1172. nvecs += lidp->lid_size;
  1173. }
  1174. return nvecs;
  1175. }
  1176. /*
  1177. * Fill in the vector with pointers to data to be logged
  1178. * by this transaction. The transaction header takes
  1179. * the first vector, and then each dirty item takes the
  1180. * number of vectors it indicated it needed in xfs_trans_count_vecs().
  1181. *
  1182. * As each item fills in the entries it needs, also pin the item
  1183. * so that it cannot be flushed out until the log write completes.
  1184. */
  1185. static void
  1186. xfs_trans_fill_vecs(
  1187. struct xfs_trans *tp,
  1188. struct xfs_log_iovec *log_vector)
  1189. {
  1190. struct xfs_log_item_desc *lidp;
  1191. struct xfs_log_iovec *vecp;
  1192. uint nitems;
  1193. /*
  1194. * Skip over the entry for the transaction header, we'll
  1195. * fill that in at the end.
  1196. */
  1197. vecp = log_vector + 1;
  1198. nitems = 0;
  1199. ASSERT(!list_empty(&tp->t_items));
  1200. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  1201. /* Skip items which aren't dirty in this transaction. */
  1202. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1203. continue;
  1204. /*
  1205. * The item may be marked dirty but not log anything. This can
  1206. * be used to get called when a transaction is committed.
  1207. */
  1208. if (lidp->lid_size)
  1209. nitems++;
  1210. IOP_FORMAT(lidp->lid_item, vecp);
  1211. vecp += lidp->lid_size;
  1212. IOP_PIN(lidp->lid_item);
  1213. }
  1214. /*
  1215. * Now that we've counted the number of items in this transaction, fill
  1216. * in the transaction header. Note that the transaction header does not
  1217. * have a log item.
  1218. */
  1219. tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
  1220. tp->t_header.th_type = tp->t_type;
  1221. tp->t_header.th_num_items = nitems;
  1222. log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
  1223. log_vector->i_len = sizeof(xfs_trans_header_t);
  1224. log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
  1225. }
  1226. /*
  1227. * The committed item processing consists of calling the committed routine of
  1228. * each logged item, updating the item's position in the AIL if necessary, and
  1229. * unpinning each item. If the committed routine returns -1, then do nothing
  1230. * further with the item because it may have been freed.
  1231. *
  1232. * Since items are unlocked when they are copied to the incore log, it is
  1233. * possible for two transactions to be completing and manipulating the same
  1234. * item simultaneously. The AIL lock will protect the lsn field of each item.
  1235. * The value of this field can never go backwards.
  1236. *
  1237. * We unpin the items after repositioning them in the AIL, because otherwise
  1238. * they could be immediately flushed and we'd have to race with the flusher
  1239. * trying to pull the item from the AIL as we add it.
  1240. */
  1241. static void
  1242. xfs_trans_item_committed(
  1243. struct xfs_log_item *lip,
  1244. xfs_lsn_t commit_lsn,
  1245. int aborted)
  1246. {
  1247. xfs_lsn_t item_lsn;
  1248. struct xfs_ail *ailp;
  1249. if (aborted)
  1250. lip->li_flags |= XFS_LI_ABORTED;
  1251. item_lsn = IOP_COMMITTED(lip, commit_lsn);
  1252. /* item_lsn of -1 means the item needs no further processing */
  1253. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  1254. return;
  1255. /*
  1256. * If the returned lsn is greater than what it contained before, update
  1257. * the location of the item in the AIL. If it is not, then do nothing.
  1258. * Items can never move backwards in the AIL.
  1259. *
  1260. * While the new lsn should usually be greater, it is possible that a
  1261. * later transaction completing simultaneously with an earlier one
  1262. * using the same item could complete first with a higher lsn. This
  1263. * would cause the earlier transaction to fail the test below.
  1264. */
  1265. ailp = lip->li_ailp;
  1266. spin_lock(&ailp->xa_lock);
  1267. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
  1268. /*
  1269. * This will set the item's lsn to item_lsn and update the
  1270. * position of the item in the AIL.
  1271. *
  1272. * xfs_trans_ail_update() drops the AIL lock.
  1273. */
  1274. xfs_trans_ail_update(ailp, lip, item_lsn);
  1275. } else {
  1276. spin_unlock(&ailp->xa_lock);
  1277. }
  1278. /*
  1279. * Now that we've repositioned the item in the AIL, unpin it so it can
  1280. * be flushed. Pass information about buffer stale state down from the
  1281. * log item flags, if anyone else stales the buffer we do not want to
  1282. * pay any attention to it.
  1283. */
  1284. IOP_UNPIN(lip, 0);
  1285. }
  1286. /*
  1287. * This is typically called by the LM when a transaction has been fully
  1288. * committed to disk. It needs to unpin the items which have
  1289. * been logged by the transaction and update their positions
  1290. * in the AIL if necessary.
  1291. *
  1292. * This also gets called when the transactions didn't get written out
  1293. * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
  1294. */
  1295. STATIC void
  1296. xfs_trans_committed(
  1297. void *arg,
  1298. int abortflag)
  1299. {
  1300. struct xfs_trans *tp = arg;
  1301. struct xfs_log_item_desc *lidp, *next;
  1302. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1303. xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
  1304. xfs_trans_free_item_desc(lidp);
  1305. }
  1306. xfs_trans_free(tp);
  1307. }
  1308. static inline void
  1309. xfs_log_item_batch_insert(
  1310. struct xfs_ail *ailp,
  1311. struct xfs_log_item **log_items,
  1312. int nr_items,
  1313. xfs_lsn_t commit_lsn)
  1314. {
  1315. int i;
  1316. spin_lock(&ailp->xa_lock);
  1317. /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
  1318. xfs_trans_ail_update_bulk(ailp, log_items, nr_items, commit_lsn);
  1319. for (i = 0; i < nr_items; i++)
  1320. IOP_UNPIN(log_items[i], 0);
  1321. }
  1322. /*
  1323. * Bulk operation version of xfs_trans_committed that takes a log vector of
  1324. * items to insert into the AIL. This uses bulk AIL insertion techniques to
  1325. * minimise lock traffic.
  1326. *
  1327. * If we are called with the aborted flag set, it is because a log write during
  1328. * a CIL checkpoint commit has failed. In this case, all the items in the
  1329. * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
  1330. * means that checkpoint commit abort handling is treated exactly the same
  1331. * as an iclog write error even though we haven't started any IO yet. Hence in
  1332. * this case all we need to do is IOP_COMMITTED processing, followed by an
  1333. * IOP_UNPIN(aborted) call.
  1334. */
  1335. void
  1336. xfs_trans_committed_bulk(
  1337. struct xfs_ail *ailp,
  1338. struct xfs_log_vec *log_vector,
  1339. xfs_lsn_t commit_lsn,
  1340. int aborted)
  1341. {
  1342. #define LOG_ITEM_BATCH_SIZE 32
  1343. struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
  1344. struct xfs_log_vec *lv;
  1345. int i = 0;
  1346. /* unpin all the log items */
  1347. for (lv = log_vector; lv; lv = lv->lv_next ) {
  1348. struct xfs_log_item *lip = lv->lv_item;
  1349. xfs_lsn_t item_lsn;
  1350. if (aborted)
  1351. lip->li_flags |= XFS_LI_ABORTED;
  1352. item_lsn = IOP_COMMITTED(lip, commit_lsn);
  1353. /* item_lsn of -1 means the item needs no further processing */
  1354. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  1355. continue;
  1356. /*
  1357. * if we are aborting the operation, no point in inserting the
  1358. * object into the AIL as we are in a shutdown situation.
  1359. */
  1360. if (aborted) {
  1361. ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
  1362. IOP_UNPIN(lip, 1);
  1363. continue;
  1364. }
  1365. if (item_lsn != commit_lsn) {
  1366. /*
  1367. * Not a bulk update option due to unusual item_lsn.
  1368. * Push into AIL immediately, rechecking the lsn once
  1369. * we have the ail lock. Then unpin the item.
  1370. */
  1371. spin_lock(&ailp->xa_lock);
  1372. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
  1373. xfs_trans_ail_update(ailp, lip, item_lsn);
  1374. else
  1375. spin_unlock(&ailp->xa_lock);
  1376. IOP_UNPIN(lip, 0);
  1377. continue;
  1378. }
  1379. /* Item is a candidate for bulk AIL insert. */
  1380. log_items[i++] = lv->lv_item;
  1381. if (i >= LOG_ITEM_BATCH_SIZE) {
  1382. xfs_log_item_batch_insert(ailp, log_items,
  1383. LOG_ITEM_BATCH_SIZE, commit_lsn);
  1384. i = 0;
  1385. }
  1386. }
  1387. /* make sure we insert the remainder! */
  1388. if (i)
  1389. xfs_log_item_batch_insert(ailp, log_items, i, commit_lsn);
  1390. }
  1391. /*
  1392. * Called from the trans_commit code when we notice that the filesystem is in
  1393. * the middle of a forced shutdown.
  1394. *
  1395. * When we are called here, we have already pinned all the items in the
  1396. * transaction. However, neither IOP_COMMITTING or IOP_UNLOCK has been called
  1397. * so we can simply walk the items in the transaction, unpin them with an abort
  1398. * flag and then free the items. Note that unpinning the items can result in
  1399. * them being freed immediately, so we need to use a safe list traversal method
  1400. * here.
  1401. */
  1402. STATIC void
  1403. xfs_trans_uncommit(
  1404. struct xfs_trans *tp,
  1405. uint flags)
  1406. {
  1407. struct xfs_log_item_desc *lidp, *n;
  1408. list_for_each_entry_safe(lidp, n, &tp->t_items, lid_trans) {
  1409. if (lidp->lid_flags & XFS_LID_DIRTY)
  1410. IOP_UNPIN(lidp->lid_item, 1);
  1411. }
  1412. xfs_trans_unreserve_and_mod_sb(tp);
  1413. xfs_trans_unreserve_and_mod_dquots(tp);
  1414. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1415. xfs_trans_free(tp);
  1416. }
  1417. /*
  1418. * Format the transaction direct to the iclog. This isolates the physical
  1419. * transaction commit operation from the logical operation and hence allows
  1420. * other methods to be introduced without affecting the existing commit path.
  1421. */
  1422. static int
  1423. xfs_trans_commit_iclog(
  1424. struct xfs_mount *mp,
  1425. struct xfs_trans *tp,
  1426. xfs_lsn_t *commit_lsn,
  1427. int flags)
  1428. {
  1429. int shutdown;
  1430. int error;
  1431. int log_flags = 0;
  1432. struct xlog_in_core *commit_iclog;
  1433. #define XFS_TRANS_LOGVEC_COUNT 16
  1434. struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
  1435. struct xfs_log_iovec *log_vector;
  1436. uint nvec;
  1437. /*
  1438. * Ask each log item how many log_vector entries it will
  1439. * need so we can figure out how many to allocate.
  1440. * Try to avoid the kmem_alloc() call in the common case
  1441. * by using a vector from the stack when it fits.
  1442. */
  1443. nvec = xfs_trans_count_vecs(tp);
  1444. if (nvec == 0) {
  1445. return ENOMEM; /* triggers a shutdown! */
  1446. } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
  1447. log_vector = log_vector_fast;
  1448. } else {
  1449. log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
  1450. sizeof(xfs_log_iovec_t),
  1451. KM_SLEEP);
  1452. }
  1453. /*
  1454. * Fill in the log_vector and pin the logged items, and
  1455. * then write the transaction to the log.
  1456. */
  1457. xfs_trans_fill_vecs(tp, log_vector);
  1458. if (flags & XFS_TRANS_RELEASE_LOG_RES)
  1459. log_flags = XFS_LOG_REL_PERM_RESERV;
  1460. error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
  1461. /*
  1462. * The transaction is committed incore here, and can go out to disk
  1463. * at any time after this call. However, all the items associated
  1464. * with the transaction are still locked and pinned in memory.
  1465. */
  1466. *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
  1467. tp->t_commit_lsn = *commit_lsn;
  1468. trace_xfs_trans_commit_lsn(tp);
  1469. if (nvec > XFS_TRANS_LOGVEC_COUNT)
  1470. kmem_free(log_vector);
  1471. /*
  1472. * If we got a log write error. Unpin the logitems that we
  1473. * had pinned, clean up, free trans structure, and return error.
  1474. */
  1475. if (error || *commit_lsn == -1) {
  1476. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1477. xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
  1478. return XFS_ERROR(EIO);
  1479. }
  1480. /*
  1481. * Once the transaction has committed, unused
  1482. * reservations need to be released and changes to
  1483. * the superblock need to be reflected in the in-core
  1484. * version. Do that now.
  1485. */
  1486. xfs_trans_unreserve_and_mod_sb(tp);
  1487. /*
  1488. * Tell the LM to call the transaction completion routine
  1489. * when the log write with LSN commit_lsn completes (e.g.
  1490. * when the transaction commit really hits the on-disk log).
  1491. * After this call we cannot reference tp, because the call
  1492. * can happen at any time and the call will free the transaction
  1493. * structure pointed to by tp. The only case where we call
  1494. * the completion routine (xfs_trans_committed) directly is
  1495. * if the log is turned off on a debug kernel or we're
  1496. * running in simulation mode (the log is explicitly turned
  1497. * off).
  1498. */
  1499. tp->t_logcb.cb_func = xfs_trans_committed;
  1500. tp->t_logcb.cb_arg = tp;
  1501. /*
  1502. * We need to pass the iclog buffer which was used for the
  1503. * transaction commit record into this function, and attach
  1504. * the callback to it. The callback must be attached before
  1505. * the items are unlocked to avoid racing with other threads
  1506. * waiting for an item to unlock.
  1507. */
  1508. shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
  1509. /*
  1510. * Mark this thread as no longer being in a transaction
  1511. */
  1512. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1513. /*
  1514. * Once all the items of the transaction have been copied
  1515. * to the in core log and the callback is attached, the
  1516. * items can be unlocked.
  1517. *
  1518. * This will free descriptors pointing to items which were
  1519. * not logged since there is nothing more to do with them.
  1520. * For items which were logged, we will keep pointers to them
  1521. * so they can be unpinned after the transaction commits to disk.
  1522. * This will also stamp each modified meta-data item with
  1523. * the commit lsn of this transaction for dependency tracking
  1524. * purposes.
  1525. */
  1526. xfs_trans_unlock_items(tp, *commit_lsn);
  1527. /*
  1528. * If we detected a log error earlier, finish committing
  1529. * the transaction now (unpin log items, etc).
  1530. *
  1531. * Order is critical here, to avoid using the transaction
  1532. * pointer after its been freed (by xfs_trans_committed
  1533. * either here now, or as a callback). We cannot do this
  1534. * step inside xfs_log_notify as was done earlier because
  1535. * of this issue.
  1536. */
  1537. if (shutdown)
  1538. xfs_trans_committed(tp, XFS_LI_ABORTED);
  1539. /*
  1540. * Now that the xfs_trans_committed callback has been attached,
  1541. * and the items are released we can finally allow the iclog to
  1542. * go to disk.
  1543. */
  1544. return xfs_log_release_iclog(mp, commit_iclog);
  1545. }
  1546. /*
  1547. * Walk the log items and allocate log vector structures for
  1548. * each item large enough to fit all the vectors they require.
  1549. * Note that this format differs from the old log vector format in
  1550. * that there is no transaction header in these log vectors.
  1551. */
  1552. STATIC struct xfs_log_vec *
  1553. xfs_trans_alloc_log_vecs(
  1554. xfs_trans_t *tp)
  1555. {
  1556. struct xfs_log_item_desc *lidp;
  1557. struct xfs_log_vec *lv = NULL;
  1558. struct xfs_log_vec *ret_lv = NULL;
  1559. /* Bail out if we didn't find a log item. */
  1560. if (list_empty(&tp->t_items)) {
  1561. ASSERT(0);
  1562. return NULL;
  1563. }
  1564. list_for_each_entry(lidp, &tp->t_items, lid_trans) {
  1565. struct xfs_log_vec *new_lv;
  1566. /* Skip items which aren't dirty in this transaction. */
  1567. if (!(lidp->lid_flags & XFS_LID_DIRTY))
  1568. continue;
  1569. /* Skip items that do not have any vectors for writing */
  1570. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  1571. if (!lidp->lid_size)
  1572. continue;
  1573. new_lv = kmem_zalloc(sizeof(*new_lv) +
  1574. lidp->lid_size * sizeof(struct xfs_log_iovec),
  1575. KM_SLEEP);
  1576. /* The allocated iovec region lies beyond the log vector. */
  1577. new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
  1578. new_lv->lv_niovecs = lidp->lid_size;
  1579. new_lv->lv_item = lidp->lid_item;
  1580. if (!ret_lv)
  1581. ret_lv = new_lv;
  1582. else
  1583. lv->lv_next = new_lv;
  1584. lv = new_lv;
  1585. }
  1586. return ret_lv;
  1587. }
  1588. static int
  1589. xfs_trans_commit_cil(
  1590. struct xfs_mount *mp,
  1591. struct xfs_trans *tp,
  1592. xfs_lsn_t *commit_lsn,
  1593. int flags)
  1594. {
  1595. struct xfs_log_vec *log_vector;
  1596. /*
  1597. * Get each log item to allocate a vector structure for
  1598. * the log item to to pass to the log write code. The
  1599. * CIL commit code will format the vector and save it away.
  1600. */
  1601. log_vector = xfs_trans_alloc_log_vecs(tp);
  1602. if (!log_vector)
  1603. return ENOMEM;
  1604. xfs_log_commit_cil(mp, tp, log_vector, commit_lsn, flags);
  1605. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1606. xfs_trans_free(tp);
  1607. return 0;
  1608. }
  1609. /*
  1610. * xfs_trans_commit
  1611. *
  1612. * Commit the given transaction to the log a/synchronously.
  1613. *
  1614. * XFS disk error handling mechanism is not based on a typical
  1615. * transaction abort mechanism. Logically after the filesystem
  1616. * gets marked 'SHUTDOWN', we can't let any new transactions
  1617. * be durable - ie. committed to disk - because some metadata might
  1618. * be inconsistent. In such cases, this returns an error, and the
  1619. * caller may assume that all locked objects joined to the transaction
  1620. * have already been unlocked as if the commit had succeeded.
  1621. * Do not reference the transaction structure after this call.
  1622. */
  1623. int
  1624. _xfs_trans_commit(
  1625. struct xfs_trans *tp,
  1626. uint flags,
  1627. int *log_flushed)
  1628. {
  1629. struct xfs_mount *mp = tp->t_mountp;
  1630. xfs_lsn_t commit_lsn = -1;
  1631. int error = 0;
  1632. int log_flags = 0;
  1633. int sync = tp->t_flags & XFS_TRANS_SYNC;
  1634. /*
  1635. * Determine whether this commit is releasing a permanent
  1636. * log reservation or not.
  1637. */
  1638. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1639. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1640. log_flags = XFS_LOG_REL_PERM_RESERV;
  1641. }
  1642. /*
  1643. * If there is nothing to be logged by the transaction,
  1644. * then unlock all of the items associated with the
  1645. * transaction and free the transaction structure.
  1646. * Also make sure to return any reserved blocks to
  1647. * the free pool.
  1648. */
  1649. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  1650. goto out_unreserve;
  1651. if (XFS_FORCED_SHUTDOWN(mp)) {
  1652. error = XFS_ERROR(EIO);
  1653. goto out_unreserve;
  1654. }
  1655. ASSERT(tp->t_ticket != NULL);
  1656. /*
  1657. * If we need to update the superblock, then do it now.
  1658. */
  1659. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  1660. xfs_trans_apply_sb_deltas(tp);
  1661. xfs_trans_apply_dquot_deltas(tp);
  1662. if (mp->m_flags & XFS_MOUNT_DELAYLOG)
  1663. error = xfs_trans_commit_cil(mp, tp, &commit_lsn, flags);
  1664. else
  1665. error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
  1666. if (error == ENOMEM) {
  1667. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  1668. error = XFS_ERROR(EIO);
  1669. goto out_unreserve;
  1670. }
  1671. /*
  1672. * If the transaction needs to be synchronous, then force the
  1673. * log out now and wait for it.
  1674. */
  1675. if (sync) {
  1676. if (!error) {
  1677. error = _xfs_log_force_lsn(mp, commit_lsn,
  1678. XFS_LOG_SYNC, log_flushed);
  1679. }
  1680. XFS_STATS_INC(xs_trans_sync);
  1681. } else {
  1682. XFS_STATS_INC(xs_trans_async);
  1683. }
  1684. return error;
  1685. out_unreserve:
  1686. xfs_trans_unreserve_and_mod_sb(tp);
  1687. /*
  1688. * It is indeed possible for the transaction to be not dirty but
  1689. * the dqinfo portion to be. All that means is that we have some
  1690. * (non-persistent) quota reservations that need to be unreserved.
  1691. */
  1692. xfs_trans_unreserve_and_mod_dquots(tp);
  1693. if (tp->t_ticket) {
  1694. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1695. if (commit_lsn == -1 && !error)
  1696. error = XFS_ERROR(EIO);
  1697. }
  1698. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1699. xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
  1700. xfs_trans_free(tp);
  1701. XFS_STATS_INC(xs_trans_empty);
  1702. return error;
  1703. }
  1704. /*
  1705. * Unlock all of the transaction's items and free the transaction.
  1706. * The transaction must not have modified any of its items, because
  1707. * there is no way to restore them to their previous state.
  1708. *
  1709. * If the transaction has made a log reservation, make sure to release
  1710. * it as well.
  1711. */
  1712. void
  1713. xfs_trans_cancel(
  1714. xfs_trans_t *tp,
  1715. int flags)
  1716. {
  1717. int log_flags;
  1718. xfs_mount_t *mp = tp->t_mountp;
  1719. /*
  1720. * See if the caller is being too lazy to figure out if
  1721. * the transaction really needs an abort.
  1722. */
  1723. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  1724. flags &= ~XFS_TRANS_ABORT;
  1725. /*
  1726. * See if the caller is relying on us to shut down the
  1727. * filesystem. This happens in paths where we detect
  1728. * corruption and decide to give up.
  1729. */
  1730. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  1731. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  1732. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1733. }
  1734. #ifdef DEBUG
  1735. if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
  1736. struct xfs_log_item_desc *lidp;
  1737. list_for_each_entry(lidp, &tp->t_items, lid_trans)
  1738. ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
  1739. }
  1740. #endif
  1741. xfs_trans_unreserve_and_mod_sb(tp);
  1742. xfs_trans_unreserve_and_mod_dquots(tp);
  1743. if (tp->t_ticket) {
  1744. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1745. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1746. log_flags = XFS_LOG_REL_PERM_RESERV;
  1747. } else {
  1748. log_flags = 0;
  1749. }
  1750. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1751. }
  1752. /* mark this thread as no longer being in a transaction */
  1753. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1754. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1755. xfs_trans_free(tp);
  1756. }
  1757. /*
  1758. * Roll from one trans in the sequence of PERMANENT transactions to
  1759. * the next: permanent transactions are only flushed out when
  1760. * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
  1761. * as possible to let chunks of it go to the log. So we commit the
  1762. * chunk we've been working on and get a new transaction to continue.
  1763. */
  1764. int
  1765. xfs_trans_roll(
  1766. struct xfs_trans **tpp,
  1767. struct xfs_inode *dp)
  1768. {
  1769. struct xfs_trans *trans;
  1770. unsigned int logres, count;
  1771. int error;
  1772. /*
  1773. * Ensure that the inode is always logged.
  1774. */
  1775. trans = *tpp;
  1776. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  1777. /*
  1778. * Copy the critical parameters from one trans to the next.
  1779. */
  1780. logres = trans->t_log_res;
  1781. count = trans->t_log_count;
  1782. *tpp = xfs_trans_dup(trans);
  1783. /*
  1784. * Commit the current transaction.
  1785. * If this commit failed, then it'd just unlock those items that
  1786. * are not marked ihold. That also means that a filesystem shutdown
  1787. * is in progress. The caller takes the responsibility to cancel
  1788. * the duplicate transaction that gets returned.
  1789. */
  1790. error = xfs_trans_commit(trans, 0);
  1791. if (error)
  1792. return (error);
  1793. trans = *tpp;
  1794. /*
  1795. * transaction commit worked ok so we can drop the extra ticket
  1796. * reference that we gained in xfs_trans_dup()
  1797. */
  1798. xfs_log_ticket_put(trans->t_ticket);
  1799. /*
  1800. * Reserve space in the log for th next transaction.
  1801. * This also pushes items in the "AIL", the list of logged items,
  1802. * out to disk if they are taking up space at the tail of the log
  1803. * that we want to use. This requires that either nothing be locked
  1804. * across this call, or that anything that is locked be logged in
  1805. * the prior and the next transactions.
  1806. */
  1807. error = xfs_trans_reserve(trans, 0, logres, 0,
  1808. XFS_TRANS_PERM_LOG_RES, count);
  1809. /*
  1810. * Ensure that the inode is in the new transaction and locked.
  1811. */
  1812. if (error)
  1813. return error;
  1814. xfs_trans_ijoin(trans, dp);
  1815. return 0;
  1816. }