xfs_trans.c 49 KB

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