xfs_trans.c 49 KB

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