xfs_trans.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_error.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_dinode.h"
  33. #include "xfs_inode.h"
  34. #include "xfs_btree.h"
  35. #include "xfs_ialloc.h"
  36. #include "xfs_alloc.h"
  37. #include "xfs_extent_busy.h"
  38. #include "xfs_bmap.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_qm.h"
  41. #include "xfs_trans_priv.h"
  42. #include "xfs_trans_space.h"
  43. #include "xfs_inode_item.h"
  44. #include "xfs_log_priv.h"
  45. #include "xfs_buf_item.h"
  46. #include "xfs_trace.h"
  47. kmem_zone_t *xfs_trans_zone;
  48. kmem_zone_t *xfs_log_item_desc_zone;
  49. /*
  50. * A buffer has a format structure overhead in the log in addition
  51. * to the data, so we need to take this into account when reserving
  52. * space in a transaction for a buffer. Round the space required up
  53. * to a multiple of 128 bytes so that we don't change the historical
  54. * reservation that has been used for this overhead.
  55. */
  56. STATIC uint
  57. xfs_buf_log_overhead(void)
  58. {
  59. return round_up(sizeof(struct xlog_op_header) +
  60. sizeof(struct xfs_buf_log_format), 128);
  61. }
  62. /*
  63. * Calculate out transaction log reservation per item in bytes.
  64. *
  65. * The nbufs argument is used to indicate the number of items that
  66. * will be changed in a transaction. size is used to tell how many
  67. * bytes should be reserved per item.
  68. */
  69. STATIC uint
  70. xfs_calc_buf_res(
  71. uint nbufs,
  72. uint size)
  73. {
  74. return nbufs * (size + xfs_buf_log_overhead());
  75. }
  76. /*
  77. * Various log reservation values.
  78. *
  79. * These are based on the size of the file system block because that is what
  80. * most transactions manipulate. Each adds in an additional 128 bytes per
  81. * item logged to try to account for the overhead of the transaction mechanism.
  82. *
  83. * Note: Most of the reservations underestimate the number of allocation
  84. * groups into which they could free extents in the xfs_bmap_finish() call.
  85. * This is because the number in the worst case is quite high and quite
  86. * unusual. In order to fix this we need to change xfs_bmap_finish() to free
  87. * extents in only a single AG at a time. This will require changes to the
  88. * EFI code as well, however, so that the EFI for the extents not freed is
  89. * logged again in each transaction. See SGI PV #261917.
  90. *
  91. * Reservation functions here avoid a huge stack in xfs_trans_init due to
  92. * register overflow from temporaries in the calculations.
  93. */
  94. /*
  95. * In a write transaction we can allocate a maximum of 2
  96. * extents. This gives:
  97. * the inode getting the new extents: inode size
  98. * the inode's bmap btree: max depth * block size
  99. * the agfs of the ags from which the extents are allocated: 2 * sector
  100. * the superblock free block counter: sector size
  101. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  102. * And the bmap_finish transaction can free bmap blocks in a join:
  103. * the agfs of the ags containing the blocks: 2 * sector size
  104. * the agfls of the ags containing the blocks: 2 * sector size
  105. * the super block free block counter: sector size
  106. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  107. */
  108. STATIC uint
  109. xfs_calc_write_reservation(
  110. struct xfs_mount *mp)
  111. {
  112. return XFS_DQUOT_LOGRES(mp) +
  113. MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  114. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
  115. XFS_FSB_TO_B(mp, 1)) +
  116. xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  117. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
  118. XFS_FSB_TO_B(mp, 1))),
  119. (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  120. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
  121. XFS_FSB_TO_B(mp, 1))));
  122. }
  123. /*
  124. * In truncating a file we free up to two extents at once. We can modify:
  125. * the inode being truncated: inode size
  126. * the inode's bmap btree: (max depth + 1) * block size
  127. * And the bmap_finish transaction can free the blocks and bmap blocks:
  128. * the agf for each of the ags: 4 * sector size
  129. * the agfl for each of the ags: 4 * sector size
  130. * the super block to reflect the freed blocks: sector size
  131. * worst case split in allocation btrees per extent assuming 4 extents:
  132. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  133. * the inode btree: max depth * blocksize
  134. * the allocation btrees: 2 trees * (max depth - 1) * block size
  135. */
  136. STATIC uint
  137. xfs_calc_itruncate_reservation(
  138. struct xfs_mount *mp)
  139. {
  140. return XFS_DQUOT_LOGRES(mp) +
  141. MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  142. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
  143. XFS_FSB_TO_B(mp, 1))),
  144. (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
  145. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
  146. XFS_FSB_TO_B(mp, 1)) +
  147. xfs_calc_buf_res(5, 0) +
  148. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  149. XFS_FSB_TO_B(mp, 1)) +
  150. xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
  151. mp->m_in_maxlevels, 0)));
  152. }
  153. /*
  154. * In renaming a files we can modify:
  155. * the four inodes involved: 4 * inode size
  156. * the two directory btrees: 2 * (max depth + v2) * dir block size
  157. * the two directory bmap btrees: 2 * max depth * block size
  158. * And the bmap_finish transaction can free dir and bmap blocks (two sets
  159. * of bmap blocks) giving:
  160. * the agf for the ags in which the blocks live: 3 * sector size
  161. * the agfl for the ags in which the blocks live: 3 * sector size
  162. * the superblock for the free block count: sector size
  163. * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
  164. */
  165. STATIC uint
  166. xfs_calc_rename_reservation(
  167. struct xfs_mount *mp)
  168. {
  169. return XFS_DQUOT_LOGRES(mp) +
  170. MAX((xfs_calc_buf_res(4, mp->m_sb.sb_inodesize) +
  171. xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
  172. XFS_FSB_TO_B(mp, 1))),
  173. (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
  174. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
  175. XFS_FSB_TO_B(mp, 1))));
  176. }
  177. /*
  178. * For creating a link to an inode:
  179. * the parent directory inode: inode size
  180. * the linked inode: inode size
  181. * the directory btree could split: (max depth + v2) * dir block size
  182. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  183. * And the bmap_finish transaction can free some bmap blocks giving:
  184. * the agf for the ag in which the blocks live: sector size
  185. * the agfl for the ag in which the blocks live: sector size
  186. * the superblock for the free block count: sector size
  187. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  188. */
  189. STATIC uint
  190. xfs_calc_link_reservation(
  191. struct xfs_mount *mp)
  192. {
  193. return XFS_DQUOT_LOGRES(mp) +
  194. MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
  195. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  196. XFS_FSB_TO_B(mp, 1))),
  197. (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  198. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  199. XFS_FSB_TO_B(mp, 1))));
  200. }
  201. /*
  202. * For removing a directory entry we can modify:
  203. * the parent directory inode: inode size
  204. * the removed inode: inode size
  205. * the directory btree could join: (max depth + v2) * dir block size
  206. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  207. * And the bmap_finish transaction can free the dir and bmap blocks giving:
  208. * the agf for the ag in which the blocks live: 2 * sector size
  209. * the agfl for the ag in which the blocks live: 2 * sector size
  210. * the superblock for the free block count: sector size
  211. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  212. */
  213. STATIC uint
  214. xfs_calc_remove_reservation(
  215. struct xfs_mount *mp)
  216. {
  217. return XFS_DQUOT_LOGRES(mp) +
  218. MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
  219. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  220. XFS_FSB_TO_B(mp, 1))),
  221. (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  222. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
  223. XFS_FSB_TO_B(mp, 1))));
  224. }
  225. /*
  226. * For symlink we can modify:
  227. * the parent directory inode: inode size
  228. * the new inode: inode size
  229. * the inode btree entry: 1 block
  230. * the directory btree: (max depth + v2) * dir block size
  231. * the directory inode's bmap btree: (max depth + v2) * block size
  232. * the blocks for the symlink: 1 kB
  233. * Or in the first xact we allocate some inodes giving:
  234. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  235. * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
  236. * the inode btree: max depth * blocksize
  237. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  238. */
  239. STATIC uint
  240. xfs_calc_symlink_reservation(
  241. struct xfs_mount *mp)
  242. {
  243. return XFS_DQUOT_LOGRES(mp) +
  244. MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
  245. xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
  246. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  247. XFS_FSB_TO_B(mp, 1)) +
  248. xfs_calc_buf_res(1, 1024)),
  249. (xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  250. xfs_calc_buf_res(XFS_IALLOC_BLOCKS(mp),
  251. XFS_FSB_TO_B(mp, 1)) +
  252. xfs_calc_buf_res(mp->m_in_maxlevels,
  253. XFS_FSB_TO_B(mp, 1)) +
  254. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  255. XFS_FSB_TO_B(mp, 1))));
  256. }
  257. /*
  258. * For create we can modify:
  259. * the parent directory inode: inode size
  260. * the new inode: inode size
  261. * the inode btree entry: block size
  262. * the superblock for the nlink flag: sector size
  263. * the directory btree: (max depth + v2) * dir block size
  264. * the directory inode's bmap btree: (max depth + v2) * block size
  265. * Or in the first xact we allocate some inodes giving:
  266. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  267. * the superblock for the nlink flag: sector size
  268. * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
  269. * the inode btree: max depth * blocksize
  270. * the allocation btrees: 2 trees * (max depth - 1) * block size
  271. */
  272. STATIC uint
  273. xfs_calc_create_reservation(
  274. struct xfs_mount *mp)
  275. {
  276. return XFS_DQUOT_LOGRES(mp) +
  277. MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
  278. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  279. (uint)XFS_FSB_TO_B(mp, 1) +
  280. xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
  281. XFS_FSB_TO_B(mp, 1))),
  282. (xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  283. mp->m_sb.sb_sectsize +
  284. xfs_calc_buf_res(XFS_IALLOC_BLOCKS(mp),
  285. XFS_FSB_TO_B(mp, 1)) +
  286. xfs_calc_buf_res(mp->m_in_maxlevels,
  287. XFS_FSB_TO_B(mp, 1)) +
  288. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  289. XFS_FSB_TO_B(mp, 1))));
  290. }
  291. /*
  292. * Making a new directory is the same as creating a new file.
  293. */
  294. STATIC uint
  295. xfs_calc_mkdir_reservation(
  296. struct xfs_mount *mp)
  297. {
  298. return xfs_calc_create_reservation(mp);
  299. }
  300. /*
  301. * In freeing an inode we can modify:
  302. * the inode being freed: inode size
  303. * the super block free inode counter: sector size
  304. * the agi hash list and counters: sector size
  305. * the inode btree entry: block size
  306. * the on disk inode before ours in the agi hash list: inode cluster size
  307. * the inode btree: max depth * blocksize
  308. * the allocation btrees: 2 trees * (max depth - 1) * block size
  309. */
  310. STATIC uint
  311. xfs_calc_ifree_reservation(
  312. struct xfs_mount *mp)
  313. {
  314. return XFS_DQUOT_LOGRES(mp) +
  315. xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  316. xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  317. xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
  318. MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
  319. XFS_INODE_CLUSTER_SIZE(mp)) +
  320. xfs_calc_buf_res(1, 0) +
  321. xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
  322. mp->m_in_maxlevels, 0) +
  323. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  324. XFS_FSB_TO_B(mp, 1));
  325. }
  326. /*
  327. * When only changing the inode we log the inode and possibly the superblock
  328. * We also add a bit of slop for the transaction stuff.
  329. */
  330. STATIC uint
  331. xfs_calc_ichange_reservation(
  332. struct xfs_mount *mp)
  333. {
  334. return XFS_DQUOT_LOGRES(mp) +
  335. mp->m_sb.sb_inodesize +
  336. mp->m_sb.sb_sectsize +
  337. 512;
  338. }
  339. /*
  340. * Growing the data section of the filesystem.
  341. * superblock
  342. * agi and agf
  343. * allocation btrees
  344. */
  345. STATIC uint
  346. xfs_calc_growdata_reservation(
  347. struct xfs_mount *mp)
  348. {
  349. return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
  350. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  351. XFS_FSB_TO_B(mp, 1));
  352. }
  353. /*
  354. * Growing the rt section of the filesystem.
  355. * In the first set of transactions (ALLOC) we allocate space to the
  356. * bitmap or summary files.
  357. * superblock: sector size
  358. * agf of the ag from which the extent is allocated: sector size
  359. * bmap btree for bitmap/summary inode: max depth * blocksize
  360. * bitmap/summary inode: inode size
  361. * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
  362. */
  363. STATIC uint
  364. xfs_calc_growrtalloc_reservation(
  365. struct xfs_mount *mp)
  366. {
  367. return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  368. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
  369. XFS_FSB_TO_B(mp, 1)) +
  370. xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  371. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  372. XFS_FSB_TO_B(mp, 1));
  373. }
  374. /*
  375. * Growing the rt section of the filesystem.
  376. * In the second set of transactions (ZERO) we zero the new metadata blocks.
  377. * one bitmap/summary block: blocksize
  378. */
  379. STATIC uint
  380. xfs_calc_growrtzero_reservation(
  381. struct xfs_mount *mp)
  382. {
  383. return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
  384. }
  385. /*
  386. * Growing the rt section of the filesystem.
  387. * In the third set of transactions (FREE) we update metadata without
  388. * allocating any new blocks.
  389. * superblock: sector size
  390. * bitmap inode: inode size
  391. * summary inode: inode size
  392. * one bitmap block: blocksize
  393. * summary blocks: new summary size
  394. */
  395. STATIC uint
  396. xfs_calc_growrtfree_reservation(
  397. struct xfs_mount *mp)
  398. {
  399. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  400. xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
  401. xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
  402. xfs_calc_buf_res(1, mp->m_rsumsize);
  403. }
  404. /*
  405. * Logging the inode modification timestamp on a synchronous write.
  406. * inode
  407. */
  408. STATIC uint
  409. xfs_calc_swrite_reservation(
  410. struct xfs_mount *mp)
  411. {
  412. return xfs_calc_buf_res(1, mp->m_sb.sb_inodesize);
  413. }
  414. /*
  415. * Logging the inode mode bits when writing a setuid/setgid file
  416. * inode
  417. */
  418. STATIC uint
  419. xfs_calc_writeid_reservation(xfs_mount_t *mp)
  420. {
  421. return xfs_calc_buf_res(1, mp->m_sb.sb_inodesize);
  422. }
  423. /*
  424. * Converting the inode from non-attributed to attributed.
  425. * the inode being converted: inode size
  426. * agf block and superblock (for block allocation)
  427. * the new block (directory sized)
  428. * bmap blocks for the new directory block
  429. * allocation btrees
  430. */
  431. STATIC uint
  432. xfs_calc_addafork_reservation(
  433. struct xfs_mount *mp)
  434. {
  435. return XFS_DQUOT_LOGRES(mp) +
  436. xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  437. xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
  438. xfs_calc_buf_res(1, mp->m_dirblksize) +
  439. xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
  440. XFS_FSB_TO_B(mp, 1)) +
  441. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
  442. XFS_FSB_TO_B(mp, 1));
  443. }
  444. /*
  445. * Removing the attribute fork of a file
  446. * the inode being truncated: inode size
  447. * the inode's bmap btree: max depth * block size
  448. * And the bmap_finish transaction can free the blocks and bmap blocks:
  449. * the agf for each of the ags: 4 * sector size
  450. * the agfl for each of the ags: 4 * sector size
  451. * the super block to reflect the freed blocks: sector size
  452. * worst case split in allocation btrees per extent assuming 4 extents:
  453. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  454. */
  455. STATIC uint
  456. xfs_calc_attrinval_reservation(
  457. struct xfs_mount *mp)
  458. {
  459. return MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  460. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
  461. XFS_FSB_TO_B(mp, 1))),
  462. (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
  463. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
  464. XFS_FSB_TO_B(mp, 1))));
  465. }
  466. /*
  467. * Setting an attribute.
  468. * the inode getting the attribute
  469. * the superblock for allocations
  470. * the agfs extents are allocated from
  471. * the attribute btree * max depth
  472. * the inode allocation btree
  473. * Since attribute transaction space is dependent on the size of the attribute,
  474. * the calculation is done partially at mount time and partially at runtime.
  475. */
  476. STATIC uint
  477. xfs_calc_attrset_reservation(
  478. struct xfs_mount *mp)
  479. {
  480. return XFS_DQUOT_LOGRES(mp) +
  481. xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  482. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
  483. xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
  484. }
  485. /*
  486. * Removing an attribute.
  487. * the inode: inode size
  488. * the attribute btree could join: max depth * block size
  489. * the inode bmap btree could join or split: max depth * block size
  490. * And the bmap_finish transaction can free the attr blocks freed giving:
  491. * the agf for the ag in which the blocks live: 2 * sector size
  492. * the agfl for the ag in which the blocks live: 2 * sector size
  493. * the superblock for the free block count: sector size
  494. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  495. */
  496. STATIC uint
  497. xfs_calc_attrrm_reservation(
  498. struct xfs_mount *mp)
  499. {
  500. return XFS_DQUOT_LOGRES(mp) +
  501. MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
  502. xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
  503. XFS_FSB_TO_B(mp, 1)) +
  504. (uint)XFS_FSB_TO_B(mp,
  505. XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
  506. xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
  507. (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
  508. xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
  509. XFS_FSB_TO_B(mp, 1))));
  510. }
  511. /*
  512. * Clearing a bad agino number in an agi hash bucket.
  513. */
  514. STATIC uint
  515. xfs_calc_clear_agi_bucket_reservation(
  516. struct xfs_mount *mp)
  517. {
  518. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  519. }
  520. /*
  521. * Clearing the quotaflags in the superblock.
  522. * the super block for changing quota flags: sector size
  523. */
  524. STATIC uint
  525. xfs_calc_qm_sbchange_reservation(
  526. struct xfs_mount *mp)
  527. {
  528. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  529. }
  530. /*
  531. * Adjusting quota limits.
  532. * the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
  533. */
  534. STATIC uint
  535. xfs_calc_qm_setqlim_reservation(
  536. struct xfs_mount *mp)
  537. {
  538. return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
  539. }
  540. /*
  541. * Allocating quota on disk if needed.
  542. * the write transaction log space: XFS_WRITE_LOG_RES(mp)
  543. * the unit of quota allocation: one system block size
  544. */
  545. STATIC uint
  546. xfs_calc_qm_dqalloc_reservation(
  547. struct xfs_mount *mp)
  548. {
  549. return XFS_WRITE_LOG_RES(mp) +
  550. xfs_calc_buf_res(1,
  551. XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
  552. }
  553. /*
  554. * Turning off quotas.
  555. * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
  556. * the superblock for the quota flags: sector size
  557. */
  558. STATIC uint
  559. xfs_calc_qm_quotaoff_reservation(
  560. struct xfs_mount *mp)
  561. {
  562. return sizeof(struct xfs_qoff_logitem) * 2 +
  563. xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  564. }
  565. /*
  566. * End of turning off quotas.
  567. * the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
  568. */
  569. STATIC uint
  570. xfs_calc_qm_quotaoff_end_reservation(
  571. struct xfs_mount *mp)
  572. {
  573. return sizeof(struct xfs_qoff_logitem) * 2;
  574. }
  575. /*
  576. * Syncing the incore super block changes to disk.
  577. * the super block to reflect the changes: sector size
  578. */
  579. STATIC uint
  580. xfs_calc_sb_reservation(
  581. struct xfs_mount *mp)
  582. {
  583. return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
  584. }
  585. /*
  586. * Initialize the precomputed transaction reservation values
  587. * in the mount structure.
  588. */
  589. void
  590. xfs_trans_init(
  591. struct xfs_mount *mp)
  592. {
  593. struct xfs_trans_reservations *resp = &mp->m_reservations;
  594. resp->tr_write = xfs_calc_write_reservation(mp);
  595. resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
  596. resp->tr_rename = xfs_calc_rename_reservation(mp);
  597. resp->tr_link = xfs_calc_link_reservation(mp);
  598. resp->tr_remove = xfs_calc_remove_reservation(mp);
  599. resp->tr_symlink = xfs_calc_symlink_reservation(mp);
  600. resp->tr_create = xfs_calc_create_reservation(mp);
  601. resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
  602. resp->tr_ifree = xfs_calc_ifree_reservation(mp);
  603. resp->tr_ichange = xfs_calc_ichange_reservation(mp);
  604. resp->tr_growdata = xfs_calc_growdata_reservation(mp);
  605. resp->tr_swrite = xfs_calc_swrite_reservation(mp);
  606. resp->tr_writeid = xfs_calc_writeid_reservation(mp);
  607. resp->tr_addafork = xfs_calc_addafork_reservation(mp);
  608. resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
  609. resp->tr_attrset = xfs_calc_attrset_reservation(mp);
  610. resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
  611. resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
  612. resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
  613. resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
  614. resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
  615. resp->tr_qm_sbchange = xfs_calc_qm_sbchange_reservation(mp);
  616. resp->tr_qm_setqlim = xfs_calc_qm_setqlim_reservation(mp);
  617. resp->tr_qm_dqalloc = xfs_calc_qm_dqalloc_reservation(mp);
  618. resp->tr_qm_quotaoff = xfs_calc_qm_quotaoff_reservation(mp);
  619. resp->tr_qm_equotaoff = xfs_calc_qm_quotaoff_end_reservation(mp);
  620. resp->tr_sb = xfs_calc_sb_reservation(mp);
  621. }
  622. /*
  623. * This routine is called to allocate a transaction structure.
  624. * The type parameter indicates the type of the transaction. These
  625. * are enumerated in xfs_trans.h.
  626. *
  627. * Dynamically allocate the transaction structure from the transaction
  628. * zone, initialize it, and return it to the caller.
  629. */
  630. xfs_trans_t *
  631. xfs_trans_alloc(
  632. xfs_mount_t *mp,
  633. uint type)
  634. {
  635. xfs_trans_t *tp;
  636. sb_start_intwrite(mp->m_super);
  637. tp = _xfs_trans_alloc(mp, type, KM_SLEEP);
  638. tp->t_flags |= XFS_TRANS_FREEZE_PROT;
  639. return tp;
  640. }
  641. xfs_trans_t *
  642. _xfs_trans_alloc(
  643. xfs_mount_t *mp,
  644. uint type,
  645. xfs_km_flags_t memflags)
  646. {
  647. xfs_trans_t *tp;
  648. WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
  649. atomic_inc(&mp->m_active_trans);
  650. tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
  651. tp->t_magic = XFS_TRANS_MAGIC;
  652. tp->t_type = type;
  653. tp->t_mountp = mp;
  654. INIT_LIST_HEAD(&tp->t_items);
  655. INIT_LIST_HEAD(&tp->t_busy);
  656. return tp;
  657. }
  658. /*
  659. * Free the transaction structure. If there is more clean up
  660. * to do when the structure is freed, add it here.
  661. */
  662. STATIC void
  663. xfs_trans_free(
  664. struct xfs_trans *tp)
  665. {
  666. xfs_extent_busy_sort(&tp->t_busy);
  667. xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
  668. atomic_dec(&tp->t_mountp->m_active_trans);
  669. if (tp->t_flags & XFS_TRANS_FREEZE_PROT)
  670. sb_end_intwrite(tp->t_mountp->m_super);
  671. xfs_trans_free_dqinfo(tp);
  672. kmem_zone_free(xfs_trans_zone, tp);
  673. }
  674. /*
  675. * This is called to create a new transaction which will share the
  676. * permanent log reservation of the given transaction. The remaining
  677. * unused block and rt extent reservations are also inherited. This
  678. * implies that the original transaction is no longer allowed to allocate
  679. * blocks. Locks and log items, however, are no inherited. They must
  680. * be added to the new transaction explicitly.
  681. */
  682. xfs_trans_t *
  683. xfs_trans_dup(
  684. xfs_trans_t *tp)
  685. {
  686. xfs_trans_t *ntp;
  687. ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  688. /*
  689. * Initialize the new transaction structure.
  690. */
  691. ntp->t_magic = XFS_TRANS_MAGIC;
  692. ntp->t_type = tp->t_type;
  693. ntp->t_mountp = tp->t_mountp;
  694. INIT_LIST_HEAD(&ntp->t_items);
  695. INIT_LIST_HEAD(&ntp->t_busy);
  696. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  697. ASSERT(tp->t_ticket != NULL);
  698. ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
  699. (tp->t_flags & XFS_TRANS_RESERVE) |
  700. (tp->t_flags & XFS_TRANS_FREEZE_PROT);
  701. /* We gave our writer reference to the new transaction */
  702. tp->t_flags &= ~XFS_TRANS_FREEZE_PROT;
  703. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  704. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  705. tp->t_blk_res = tp->t_blk_res_used;
  706. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  707. tp->t_rtx_res = tp->t_rtx_res_used;
  708. ntp->t_pflags = tp->t_pflags;
  709. xfs_trans_dup_dqinfo(tp, ntp);
  710. atomic_inc(&tp->t_mountp->m_active_trans);
  711. return ntp;
  712. }
  713. /*
  714. * This is called to reserve free disk blocks and log space for the
  715. * given transaction. This must be done before allocating any resources
  716. * within the transaction.
  717. *
  718. * This will return ENOSPC if there are not enough blocks available.
  719. * It will sleep waiting for available log space.
  720. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  721. * is used by long running transactions. If any one of the reservations
  722. * fails then they will all be backed out.
  723. *
  724. * This does not do quota reservations. That typically is done by the
  725. * caller afterwards.
  726. */
  727. int
  728. xfs_trans_reserve(
  729. xfs_trans_t *tp,
  730. uint blocks,
  731. uint logspace,
  732. uint rtextents,
  733. uint flags,
  734. uint logcount)
  735. {
  736. int error = 0;
  737. int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  738. /* Mark this thread as being in a transaction */
  739. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  740. /*
  741. * Attempt to reserve the needed disk blocks by decrementing
  742. * the number needed from the number available. This will
  743. * fail if the count would go below zero.
  744. */
  745. if (blocks > 0) {
  746. error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  747. -((int64_t)blocks), rsvd);
  748. if (error != 0) {
  749. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  750. return (XFS_ERROR(ENOSPC));
  751. }
  752. tp->t_blk_res += blocks;
  753. }
  754. /*
  755. * Reserve the log space needed for this transaction.
  756. */
  757. if (logspace > 0) {
  758. bool permanent = false;
  759. ASSERT(tp->t_log_res == 0 || tp->t_log_res == logspace);
  760. ASSERT(tp->t_log_count == 0 || tp->t_log_count == logcount);
  761. if (flags & XFS_TRANS_PERM_LOG_RES) {
  762. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  763. permanent = true;
  764. } else {
  765. ASSERT(tp->t_ticket == NULL);
  766. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  767. }
  768. if (tp->t_ticket != NULL) {
  769. ASSERT(flags & XFS_TRANS_PERM_LOG_RES);
  770. error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
  771. } else {
  772. error = xfs_log_reserve(tp->t_mountp, logspace,
  773. logcount, &tp->t_ticket,
  774. XFS_TRANSACTION, permanent,
  775. tp->t_type);
  776. }
  777. if (error)
  778. goto undo_blocks;
  779. tp->t_log_res = logspace;
  780. tp->t_log_count = logcount;
  781. }
  782. /*
  783. * Attempt to reserve the needed realtime extents by decrementing
  784. * the number needed from the number available. This will
  785. * fail if the count would go below zero.
  786. */
  787. if (rtextents > 0) {
  788. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
  789. -((int64_t)rtextents), rsvd);
  790. if (error) {
  791. error = XFS_ERROR(ENOSPC);
  792. goto undo_log;
  793. }
  794. tp->t_rtx_res += rtextents;
  795. }
  796. return 0;
  797. /*
  798. * Error cases jump to one of these labels to undo any
  799. * reservations which have already been performed.
  800. */
  801. undo_log:
  802. if (logspace > 0) {
  803. int log_flags;
  804. if (flags & XFS_TRANS_PERM_LOG_RES) {
  805. log_flags = XFS_LOG_REL_PERM_RESERV;
  806. } else {
  807. log_flags = 0;
  808. }
  809. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  810. tp->t_ticket = NULL;
  811. tp->t_log_res = 0;
  812. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  813. }
  814. undo_blocks:
  815. if (blocks > 0) {
  816. xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  817. (int64_t)blocks, rsvd);
  818. tp->t_blk_res = 0;
  819. }
  820. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  821. return error;
  822. }
  823. /*
  824. * Record the indicated change to the given field for application
  825. * to the file system's superblock when the transaction commits.
  826. * For now, just store the change in the transaction structure.
  827. *
  828. * Mark the transaction structure to indicate that the superblock
  829. * needs to be updated before committing.
  830. *
  831. * Because we may not be keeping track of allocated/free inodes and
  832. * used filesystem blocks in the superblock, we do not mark the
  833. * superblock dirty in this transaction if we modify these fields.
  834. * We still need to update the transaction deltas so that they get
  835. * applied to the incore superblock, but we don't want them to
  836. * cause the superblock to get locked and logged if these are the
  837. * only fields in the superblock that the transaction modifies.
  838. */
  839. void
  840. xfs_trans_mod_sb(
  841. xfs_trans_t *tp,
  842. uint field,
  843. int64_t delta)
  844. {
  845. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  846. xfs_mount_t *mp = tp->t_mountp;
  847. switch (field) {
  848. case XFS_TRANS_SB_ICOUNT:
  849. tp->t_icount_delta += delta;
  850. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  851. flags &= ~XFS_TRANS_SB_DIRTY;
  852. break;
  853. case XFS_TRANS_SB_IFREE:
  854. tp->t_ifree_delta += delta;
  855. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  856. flags &= ~XFS_TRANS_SB_DIRTY;
  857. break;
  858. case XFS_TRANS_SB_FDBLOCKS:
  859. /*
  860. * Track the number of blocks allocated in the
  861. * transaction. Make sure it does not exceed the
  862. * number reserved.
  863. */
  864. if (delta < 0) {
  865. tp->t_blk_res_used += (uint)-delta;
  866. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  867. }
  868. tp->t_fdblocks_delta += delta;
  869. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  870. flags &= ~XFS_TRANS_SB_DIRTY;
  871. break;
  872. case XFS_TRANS_SB_RES_FDBLOCKS:
  873. /*
  874. * The allocation has already been applied to the
  875. * in-core superblock's counter. This should only
  876. * be applied to the on-disk superblock.
  877. */
  878. ASSERT(delta < 0);
  879. tp->t_res_fdblocks_delta += delta;
  880. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  881. flags &= ~XFS_TRANS_SB_DIRTY;
  882. break;
  883. case XFS_TRANS_SB_FREXTENTS:
  884. /*
  885. * Track the number of blocks allocated in the
  886. * transaction. Make sure it does not exceed the
  887. * number reserved.
  888. */
  889. if (delta < 0) {
  890. tp->t_rtx_res_used += (uint)-delta;
  891. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  892. }
  893. tp->t_frextents_delta += delta;
  894. break;
  895. case XFS_TRANS_SB_RES_FREXTENTS:
  896. /*
  897. * The allocation has already been applied to the
  898. * in-core superblock's counter. This should only
  899. * be applied to the on-disk superblock.
  900. */
  901. ASSERT(delta < 0);
  902. tp->t_res_frextents_delta += delta;
  903. break;
  904. case XFS_TRANS_SB_DBLOCKS:
  905. ASSERT(delta > 0);
  906. tp->t_dblocks_delta += delta;
  907. break;
  908. case XFS_TRANS_SB_AGCOUNT:
  909. ASSERT(delta > 0);
  910. tp->t_agcount_delta += delta;
  911. break;
  912. case XFS_TRANS_SB_IMAXPCT:
  913. tp->t_imaxpct_delta += delta;
  914. break;
  915. case XFS_TRANS_SB_REXTSIZE:
  916. tp->t_rextsize_delta += delta;
  917. break;
  918. case XFS_TRANS_SB_RBMBLOCKS:
  919. tp->t_rbmblocks_delta += delta;
  920. break;
  921. case XFS_TRANS_SB_RBLOCKS:
  922. tp->t_rblocks_delta += delta;
  923. break;
  924. case XFS_TRANS_SB_REXTENTS:
  925. tp->t_rextents_delta += delta;
  926. break;
  927. case XFS_TRANS_SB_REXTSLOG:
  928. tp->t_rextslog_delta += delta;
  929. break;
  930. default:
  931. ASSERT(0);
  932. return;
  933. }
  934. tp->t_flags |= flags;
  935. }
  936. /*
  937. * xfs_trans_apply_sb_deltas() is called from the commit code
  938. * to bring the superblock buffer into the current transaction
  939. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  940. *
  941. * For now we just look at each field allowed to change and change
  942. * it if necessary.
  943. */
  944. STATIC void
  945. xfs_trans_apply_sb_deltas(
  946. xfs_trans_t *tp)
  947. {
  948. xfs_dsb_t *sbp;
  949. xfs_buf_t *bp;
  950. int whole = 0;
  951. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  952. sbp = XFS_BUF_TO_SBP(bp);
  953. /*
  954. * Check that superblock mods match the mods made to AGF counters.
  955. */
  956. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  957. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  958. tp->t_ag_btree_delta));
  959. /*
  960. * Only update the superblock counters if we are logging them
  961. */
  962. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  963. if (tp->t_icount_delta)
  964. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  965. if (tp->t_ifree_delta)
  966. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  967. if (tp->t_fdblocks_delta)
  968. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  969. if (tp->t_res_fdblocks_delta)
  970. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  971. }
  972. if (tp->t_frextents_delta)
  973. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  974. if (tp->t_res_frextents_delta)
  975. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  976. if (tp->t_dblocks_delta) {
  977. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  978. whole = 1;
  979. }
  980. if (tp->t_agcount_delta) {
  981. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  982. whole = 1;
  983. }
  984. if (tp->t_imaxpct_delta) {
  985. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  986. whole = 1;
  987. }
  988. if (tp->t_rextsize_delta) {
  989. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  990. whole = 1;
  991. }
  992. if (tp->t_rbmblocks_delta) {
  993. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  994. whole = 1;
  995. }
  996. if (tp->t_rblocks_delta) {
  997. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  998. whole = 1;
  999. }
  1000. if (tp->t_rextents_delta) {
  1001. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  1002. whole = 1;
  1003. }
  1004. if (tp->t_rextslog_delta) {
  1005. sbp->sb_rextslog += tp->t_rextslog_delta;
  1006. whole = 1;
  1007. }
  1008. if (whole)
  1009. /*
  1010. * Log the whole thing, the fields are noncontiguous.
  1011. */
  1012. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  1013. else
  1014. /*
  1015. * Since all the modifiable fields are contiguous, we
  1016. * can get away with this.
  1017. */
  1018. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  1019. offsetof(xfs_dsb_t, sb_frextents) +
  1020. sizeof(sbp->sb_frextents) - 1);
  1021. }
  1022. /*
  1023. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  1024. * and apply superblock counter changes to the in-core superblock. The
  1025. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  1026. * applied to the in-core superblock. The idea is that that has already been
  1027. * done.
  1028. *
  1029. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  1030. * However, we have to ensure that we only modify each superblock field only
  1031. * once because the application of the delta values may not be atomic. That can
  1032. * lead to ENOSPC races occurring if we have two separate modifcations of the
  1033. * free space counter to put back the entire reservation and then take away
  1034. * what we used.
  1035. *
  1036. * If we are not logging superblock counters, then the inode allocated/free and
  1037. * used block counts are not updated in the on disk superblock. In this case,
  1038. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  1039. * still need to update the incore superblock with the changes.
  1040. */
  1041. void
  1042. xfs_trans_unreserve_and_mod_sb(
  1043. xfs_trans_t *tp)
  1044. {
  1045. xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
  1046. xfs_mod_sb_t *msbp;
  1047. xfs_mount_t *mp = tp->t_mountp;
  1048. /* REFERENCED */
  1049. int error;
  1050. int rsvd;
  1051. int64_t blkdelta = 0;
  1052. int64_t rtxdelta = 0;
  1053. int64_t idelta = 0;
  1054. int64_t ifreedelta = 0;
  1055. msbp = msb;
  1056. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  1057. /* calculate deltas */
  1058. if (tp->t_blk_res > 0)
  1059. blkdelta = tp->t_blk_res;
  1060. if ((tp->t_fdblocks_delta != 0) &&
  1061. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  1062. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  1063. blkdelta += tp->t_fdblocks_delta;
  1064. if (tp->t_rtx_res > 0)
  1065. rtxdelta = tp->t_rtx_res;
  1066. if ((tp->t_frextents_delta != 0) &&
  1067. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  1068. rtxdelta += tp->t_frextents_delta;
  1069. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  1070. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  1071. idelta = tp->t_icount_delta;
  1072. ifreedelta = tp->t_ifree_delta;
  1073. }
  1074. /* apply the per-cpu counters */
  1075. if (blkdelta) {
  1076. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  1077. blkdelta, rsvd);
  1078. if (error)
  1079. goto out;
  1080. }
  1081. if (idelta) {
  1082. error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
  1083. idelta, rsvd);
  1084. if (error)
  1085. goto out_undo_fdblocks;
  1086. }
  1087. if (ifreedelta) {
  1088. error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
  1089. ifreedelta, rsvd);
  1090. if (error)
  1091. goto out_undo_icount;
  1092. }
  1093. /* apply remaining deltas */
  1094. if (rtxdelta != 0) {
  1095. msbp->msb_field = XFS_SBS_FREXTENTS;
  1096. msbp->msb_delta = rtxdelta;
  1097. msbp++;
  1098. }
  1099. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  1100. if (tp->t_dblocks_delta != 0) {
  1101. msbp->msb_field = XFS_SBS_DBLOCKS;
  1102. msbp->msb_delta = tp->t_dblocks_delta;
  1103. msbp++;
  1104. }
  1105. if (tp->t_agcount_delta != 0) {
  1106. msbp->msb_field = XFS_SBS_AGCOUNT;
  1107. msbp->msb_delta = tp->t_agcount_delta;
  1108. msbp++;
  1109. }
  1110. if (tp->t_imaxpct_delta != 0) {
  1111. msbp->msb_field = XFS_SBS_IMAX_PCT;
  1112. msbp->msb_delta = tp->t_imaxpct_delta;
  1113. msbp++;
  1114. }
  1115. if (tp->t_rextsize_delta != 0) {
  1116. msbp->msb_field = XFS_SBS_REXTSIZE;
  1117. msbp->msb_delta = tp->t_rextsize_delta;
  1118. msbp++;
  1119. }
  1120. if (tp->t_rbmblocks_delta != 0) {
  1121. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  1122. msbp->msb_delta = tp->t_rbmblocks_delta;
  1123. msbp++;
  1124. }
  1125. if (tp->t_rblocks_delta != 0) {
  1126. msbp->msb_field = XFS_SBS_RBLOCKS;
  1127. msbp->msb_delta = tp->t_rblocks_delta;
  1128. msbp++;
  1129. }
  1130. if (tp->t_rextents_delta != 0) {
  1131. msbp->msb_field = XFS_SBS_REXTENTS;
  1132. msbp->msb_delta = tp->t_rextents_delta;
  1133. msbp++;
  1134. }
  1135. if (tp->t_rextslog_delta != 0) {
  1136. msbp->msb_field = XFS_SBS_REXTSLOG;
  1137. msbp->msb_delta = tp->t_rextslog_delta;
  1138. msbp++;
  1139. }
  1140. }
  1141. /*
  1142. * If we need to change anything, do it.
  1143. */
  1144. if (msbp > msb) {
  1145. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  1146. (uint)(msbp - msb), rsvd);
  1147. if (error)
  1148. goto out_undo_ifreecount;
  1149. }
  1150. return;
  1151. out_undo_ifreecount:
  1152. if (ifreedelta)
  1153. xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
  1154. out_undo_icount:
  1155. if (idelta)
  1156. xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
  1157. out_undo_fdblocks:
  1158. if (blkdelta)
  1159. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
  1160. out:
  1161. ASSERT(error == 0);
  1162. return;
  1163. }
  1164. /*
  1165. * Add the given log item to the transaction's list of log items.
  1166. *
  1167. * The log item will now point to its new descriptor with its li_desc field.
  1168. */
  1169. void
  1170. xfs_trans_add_item(
  1171. struct xfs_trans *tp,
  1172. struct xfs_log_item *lip)
  1173. {
  1174. struct xfs_log_item_desc *lidp;
  1175. ASSERT(lip->li_mountp == tp->t_mountp);
  1176. ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
  1177. lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
  1178. lidp->lid_item = lip;
  1179. lidp->lid_flags = 0;
  1180. list_add_tail(&lidp->lid_trans, &tp->t_items);
  1181. lip->li_desc = lidp;
  1182. }
  1183. STATIC void
  1184. xfs_trans_free_item_desc(
  1185. struct xfs_log_item_desc *lidp)
  1186. {
  1187. list_del_init(&lidp->lid_trans);
  1188. kmem_zone_free(xfs_log_item_desc_zone, lidp);
  1189. }
  1190. /*
  1191. * Unlink and free the given descriptor.
  1192. */
  1193. void
  1194. xfs_trans_del_item(
  1195. struct xfs_log_item *lip)
  1196. {
  1197. xfs_trans_free_item_desc(lip->li_desc);
  1198. lip->li_desc = NULL;
  1199. }
  1200. /*
  1201. * Unlock all of the items of a transaction and free all the descriptors
  1202. * of that transaction.
  1203. */
  1204. void
  1205. xfs_trans_free_items(
  1206. struct xfs_trans *tp,
  1207. xfs_lsn_t commit_lsn,
  1208. int flags)
  1209. {
  1210. struct xfs_log_item_desc *lidp, *next;
  1211. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1212. struct xfs_log_item *lip = lidp->lid_item;
  1213. lip->li_desc = NULL;
  1214. if (commit_lsn != NULLCOMMITLSN)
  1215. IOP_COMMITTING(lip, commit_lsn);
  1216. if (flags & XFS_TRANS_ABORT)
  1217. lip->li_flags |= XFS_LI_ABORTED;
  1218. IOP_UNLOCK(lip);
  1219. xfs_trans_free_item_desc(lidp);
  1220. }
  1221. }
  1222. static inline void
  1223. xfs_log_item_batch_insert(
  1224. struct xfs_ail *ailp,
  1225. struct xfs_ail_cursor *cur,
  1226. struct xfs_log_item **log_items,
  1227. int nr_items,
  1228. xfs_lsn_t commit_lsn)
  1229. {
  1230. int i;
  1231. spin_lock(&ailp->xa_lock);
  1232. /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
  1233. xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
  1234. for (i = 0; i < nr_items; i++)
  1235. IOP_UNPIN(log_items[i], 0);
  1236. }
  1237. /*
  1238. * Bulk operation version of xfs_trans_committed that takes a log vector of
  1239. * items to insert into the AIL. This uses bulk AIL insertion techniques to
  1240. * minimise lock traffic.
  1241. *
  1242. * If we are called with the aborted flag set, it is because a log write during
  1243. * a CIL checkpoint commit has failed. In this case, all the items in the
  1244. * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
  1245. * means that checkpoint commit abort handling is treated exactly the same
  1246. * as an iclog write error even though we haven't started any IO yet. Hence in
  1247. * this case all we need to do is IOP_COMMITTED processing, followed by an
  1248. * IOP_UNPIN(aborted) call.
  1249. *
  1250. * The AIL cursor is used to optimise the insert process. If commit_lsn is not
  1251. * at the end of the AIL, the insert cursor avoids the need to walk
  1252. * the AIL to find the insertion point on every xfs_log_item_batch_insert()
  1253. * call. This saves a lot of needless list walking and is a net win, even
  1254. * though it slightly increases that amount of AIL lock traffic to set it up
  1255. * and tear it down.
  1256. */
  1257. void
  1258. xfs_trans_committed_bulk(
  1259. struct xfs_ail *ailp,
  1260. struct xfs_log_vec *log_vector,
  1261. xfs_lsn_t commit_lsn,
  1262. int aborted)
  1263. {
  1264. #define LOG_ITEM_BATCH_SIZE 32
  1265. struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
  1266. struct xfs_log_vec *lv;
  1267. struct xfs_ail_cursor cur;
  1268. int i = 0;
  1269. spin_lock(&ailp->xa_lock);
  1270. xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
  1271. spin_unlock(&ailp->xa_lock);
  1272. /* unpin all the log items */
  1273. for (lv = log_vector; lv; lv = lv->lv_next ) {
  1274. struct xfs_log_item *lip = lv->lv_item;
  1275. xfs_lsn_t item_lsn;
  1276. if (aborted)
  1277. lip->li_flags |= XFS_LI_ABORTED;
  1278. item_lsn = IOP_COMMITTED(lip, commit_lsn);
  1279. /* item_lsn of -1 means the item needs no further processing */
  1280. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  1281. continue;
  1282. /*
  1283. * if we are aborting the operation, no point in inserting the
  1284. * object into the AIL as we are in a shutdown situation.
  1285. */
  1286. if (aborted) {
  1287. ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
  1288. IOP_UNPIN(lip, 1);
  1289. continue;
  1290. }
  1291. if (item_lsn != commit_lsn) {
  1292. /*
  1293. * Not a bulk update option due to unusual item_lsn.
  1294. * Push into AIL immediately, rechecking the lsn once
  1295. * we have the ail lock. Then unpin the item. This does
  1296. * not affect the AIL cursor the bulk insert path is
  1297. * using.
  1298. */
  1299. spin_lock(&ailp->xa_lock);
  1300. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
  1301. xfs_trans_ail_update(ailp, lip, item_lsn);
  1302. else
  1303. spin_unlock(&ailp->xa_lock);
  1304. IOP_UNPIN(lip, 0);
  1305. continue;
  1306. }
  1307. /* Item is a candidate for bulk AIL insert. */
  1308. log_items[i++] = lv->lv_item;
  1309. if (i >= LOG_ITEM_BATCH_SIZE) {
  1310. xfs_log_item_batch_insert(ailp, &cur, log_items,
  1311. LOG_ITEM_BATCH_SIZE, commit_lsn);
  1312. i = 0;
  1313. }
  1314. }
  1315. /* make sure we insert the remainder! */
  1316. if (i)
  1317. xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
  1318. spin_lock(&ailp->xa_lock);
  1319. xfs_trans_ail_cursor_done(ailp, &cur);
  1320. spin_unlock(&ailp->xa_lock);
  1321. }
  1322. /*
  1323. * Commit the given transaction to the log.
  1324. *
  1325. * XFS disk error handling mechanism is not based on a typical
  1326. * transaction abort mechanism. Logically after the filesystem
  1327. * gets marked 'SHUTDOWN', we can't let any new transactions
  1328. * be durable - ie. committed to disk - because some metadata might
  1329. * be inconsistent. In such cases, this returns an error, and the
  1330. * caller may assume that all locked objects joined to the transaction
  1331. * have already been unlocked as if the commit had succeeded.
  1332. * Do not reference the transaction structure after this call.
  1333. */
  1334. int
  1335. xfs_trans_commit(
  1336. struct xfs_trans *tp,
  1337. uint flags)
  1338. {
  1339. struct xfs_mount *mp = tp->t_mountp;
  1340. xfs_lsn_t commit_lsn = -1;
  1341. int error = 0;
  1342. int log_flags = 0;
  1343. int sync = tp->t_flags & XFS_TRANS_SYNC;
  1344. /*
  1345. * Determine whether this commit is releasing a permanent
  1346. * log reservation or not.
  1347. */
  1348. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1349. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1350. log_flags = XFS_LOG_REL_PERM_RESERV;
  1351. }
  1352. /*
  1353. * If there is nothing to be logged by the transaction,
  1354. * then unlock all of the items associated with the
  1355. * transaction and free the transaction structure.
  1356. * Also make sure to return any reserved blocks to
  1357. * the free pool.
  1358. */
  1359. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  1360. goto out_unreserve;
  1361. if (XFS_FORCED_SHUTDOWN(mp)) {
  1362. error = XFS_ERROR(EIO);
  1363. goto out_unreserve;
  1364. }
  1365. ASSERT(tp->t_ticket != NULL);
  1366. /*
  1367. * If we need to update the superblock, then do it now.
  1368. */
  1369. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  1370. xfs_trans_apply_sb_deltas(tp);
  1371. xfs_trans_apply_dquot_deltas(tp);
  1372. error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
  1373. if (error == ENOMEM) {
  1374. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  1375. error = XFS_ERROR(EIO);
  1376. goto out_unreserve;
  1377. }
  1378. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1379. xfs_trans_free(tp);
  1380. /*
  1381. * If the transaction needs to be synchronous, then force the
  1382. * log out now and wait for it.
  1383. */
  1384. if (sync) {
  1385. if (!error) {
  1386. error = _xfs_log_force_lsn(mp, commit_lsn,
  1387. XFS_LOG_SYNC, NULL);
  1388. }
  1389. XFS_STATS_INC(xs_trans_sync);
  1390. } else {
  1391. XFS_STATS_INC(xs_trans_async);
  1392. }
  1393. return error;
  1394. out_unreserve:
  1395. xfs_trans_unreserve_and_mod_sb(tp);
  1396. /*
  1397. * It is indeed possible for the transaction to be not dirty but
  1398. * the dqinfo portion to be. All that means is that we have some
  1399. * (non-persistent) quota reservations that need to be unreserved.
  1400. */
  1401. xfs_trans_unreserve_and_mod_dquots(tp);
  1402. if (tp->t_ticket) {
  1403. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1404. if (commit_lsn == -1 && !error)
  1405. error = XFS_ERROR(EIO);
  1406. }
  1407. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1408. xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
  1409. xfs_trans_free(tp);
  1410. XFS_STATS_INC(xs_trans_empty);
  1411. return error;
  1412. }
  1413. /*
  1414. * Unlock all of the transaction's items and free the transaction.
  1415. * The transaction must not have modified any of its items, because
  1416. * there is no way to restore them to their previous state.
  1417. *
  1418. * If the transaction has made a log reservation, make sure to release
  1419. * it as well.
  1420. */
  1421. void
  1422. xfs_trans_cancel(
  1423. xfs_trans_t *tp,
  1424. int flags)
  1425. {
  1426. int log_flags;
  1427. xfs_mount_t *mp = tp->t_mountp;
  1428. /*
  1429. * See if the caller is being too lazy to figure out if
  1430. * the transaction really needs an abort.
  1431. */
  1432. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  1433. flags &= ~XFS_TRANS_ABORT;
  1434. /*
  1435. * See if the caller is relying on us to shut down the
  1436. * filesystem. This happens in paths where we detect
  1437. * corruption and decide to give up.
  1438. */
  1439. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  1440. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  1441. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1442. }
  1443. #ifdef DEBUG
  1444. if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
  1445. struct xfs_log_item_desc *lidp;
  1446. list_for_each_entry(lidp, &tp->t_items, lid_trans)
  1447. ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
  1448. }
  1449. #endif
  1450. xfs_trans_unreserve_and_mod_sb(tp);
  1451. xfs_trans_unreserve_and_mod_dquots(tp);
  1452. if (tp->t_ticket) {
  1453. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1454. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1455. log_flags = XFS_LOG_REL_PERM_RESERV;
  1456. } else {
  1457. log_flags = 0;
  1458. }
  1459. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1460. }
  1461. /* mark this thread as no longer being in a transaction */
  1462. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1463. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1464. xfs_trans_free(tp);
  1465. }
  1466. /*
  1467. * Roll from one trans in the sequence of PERMANENT transactions to
  1468. * the next: permanent transactions are only flushed out when
  1469. * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
  1470. * as possible to let chunks of it go to the log. So we commit the
  1471. * chunk we've been working on and get a new transaction to continue.
  1472. */
  1473. int
  1474. xfs_trans_roll(
  1475. struct xfs_trans **tpp,
  1476. struct xfs_inode *dp)
  1477. {
  1478. struct xfs_trans *trans;
  1479. unsigned int logres, count;
  1480. int error;
  1481. /*
  1482. * Ensure that the inode is always logged.
  1483. */
  1484. trans = *tpp;
  1485. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  1486. /*
  1487. * Copy the critical parameters from one trans to the next.
  1488. */
  1489. logres = trans->t_log_res;
  1490. count = trans->t_log_count;
  1491. *tpp = xfs_trans_dup(trans);
  1492. /*
  1493. * Commit the current transaction.
  1494. * If this commit failed, then it'd just unlock those items that
  1495. * are not marked ihold. That also means that a filesystem shutdown
  1496. * is in progress. The caller takes the responsibility to cancel
  1497. * the duplicate transaction that gets returned.
  1498. */
  1499. error = xfs_trans_commit(trans, 0);
  1500. if (error)
  1501. return (error);
  1502. trans = *tpp;
  1503. /*
  1504. * transaction commit worked ok so we can drop the extra ticket
  1505. * reference that we gained in xfs_trans_dup()
  1506. */
  1507. xfs_log_ticket_put(trans->t_ticket);
  1508. /*
  1509. * Reserve space in the log for th next transaction.
  1510. * This also pushes items in the "AIL", the list of logged items,
  1511. * out to disk if they are taking up space at the tail of the log
  1512. * that we want to use. This requires that either nothing be locked
  1513. * across this call, or that anything that is locked be logged in
  1514. * the prior and the next transactions.
  1515. */
  1516. error = xfs_trans_reserve(trans, 0, logres, 0,
  1517. XFS_TRANS_PERM_LOG_RES, count);
  1518. /*
  1519. * Ensure that the inode is in the new transaction and locked.
  1520. */
  1521. if (error)
  1522. return error;
  1523. xfs_trans_ijoin(trans, dp, 0);
  1524. return 0;
  1525. }