xfs_trans.c 45 KB

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