xfs_trans.c 54 KB

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