xfs_trans.c 53 KB

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