xfs_trans.c 57 KB

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