xfs_trans.c 45 KB

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