xfs_trans.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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_trans_t *tp;
  551. sb_start_intwrite(mp->m_super);
  552. tp = _xfs_trans_alloc(mp, type, KM_SLEEP);
  553. tp->t_flags |= XFS_TRANS_FREEZE_PROT;
  554. return tp;
  555. }
  556. xfs_trans_t *
  557. _xfs_trans_alloc(
  558. xfs_mount_t *mp,
  559. uint type,
  560. xfs_km_flags_t memflags)
  561. {
  562. xfs_trans_t *tp;
  563. WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
  564. atomic_inc(&mp->m_active_trans);
  565. tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
  566. tp->t_magic = XFS_TRANS_MAGIC;
  567. tp->t_type = type;
  568. tp->t_mountp = mp;
  569. INIT_LIST_HEAD(&tp->t_items);
  570. INIT_LIST_HEAD(&tp->t_busy);
  571. return tp;
  572. }
  573. /*
  574. * Free the transaction structure. If there is more clean up
  575. * to do when the structure is freed, add it here.
  576. */
  577. STATIC void
  578. xfs_trans_free(
  579. struct xfs_trans *tp)
  580. {
  581. xfs_extent_busy_sort(&tp->t_busy);
  582. xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
  583. atomic_dec(&tp->t_mountp->m_active_trans);
  584. if (tp->t_flags & XFS_TRANS_FREEZE_PROT)
  585. sb_end_intwrite(tp->t_mountp->m_super);
  586. xfs_trans_free_dqinfo(tp);
  587. kmem_zone_free(xfs_trans_zone, tp);
  588. }
  589. /*
  590. * This is called to create a new transaction which will share the
  591. * permanent log reservation of the given transaction. The remaining
  592. * unused block and rt extent reservations are also inherited. This
  593. * implies that the original transaction is no longer allowed to allocate
  594. * blocks. Locks and log items, however, are no inherited. They must
  595. * be added to the new transaction explicitly.
  596. */
  597. xfs_trans_t *
  598. xfs_trans_dup(
  599. xfs_trans_t *tp)
  600. {
  601. xfs_trans_t *ntp;
  602. ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  603. /*
  604. * Initialize the new transaction structure.
  605. */
  606. ntp->t_magic = XFS_TRANS_MAGIC;
  607. ntp->t_type = tp->t_type;
  608. ntp->t_mountp = tp->t_mountp;
  609. INIT_LIST_HEAD(&ntp->t_items);
  610. INIT_LIST_HEAD(&ntp->t_busy);
  611. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  612. ASSERT(tp->t_ticket != NULL);
  613. ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
  614. (tp->t_flags & XFS_TRANS_RESERVE) |
  615. (tp->t_flags & XFS_TRANS_FREEZE_PROT);
  616. /* We gave our writer reference to the new transaction */
  617. tp->t_flags &= ~XFS_TRANS_FREEZE_PROT;
  618. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  619. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  620. tp->t_blk_res = tp->t_blk_res_used;
  621. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  622. tp->t_rtx_res = tp->t_rtx_res_used;
  623. ntp->t_pflags = tp->t_pflags;
  624. xfs_trans_dup_dqinfo(tp, ntp);
  625. atomic_inc(&tp->t_mountp->m_active_trans);
  626. return ntp;
  627. }
  628. /*
  629. * This is called to reserve free disk blocks and log space for the
  630. * given transaction. This must be done before allocating any resources
  631. * within the transaction.
  632. *
  633. * This will return ENOSPC if there are not enough blocks available.
  634. * It will sleep waiting for available log space.
  635. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  636. * is used by long running transactions. If any one of the reservations
  637. * fails then they will all be backed out.
  638. *
  639. * This does not do quota reservations. That typically is done by the
  640. * caller afterwards.
  641. */
  642. int
  643. xfs_trans_reserve(
  644. xfs_trans_t *tp,
  645. uint blocks,
  646. uint logspace,
  647. uint rtextents,
  648. uint flags,
  649. uint logcount)
  650. {
  651. int error = 0;
  652. int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  653. /* Mark this thread as being in a transaction */
  654. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  655. /*
  656. * Attempt to reserve the needed disk blocks by decrementing
  657. * the number needed from the number available. This will
  658. * fail if the count would go below zero.
  659. */
  660. if (blocks > 0) {
  661. error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  662. -((int64_t)blocks), rsvd);
  663. if (error != 0) {
  664. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  665. return (XFS_ERROR(ENOSPC));
  666. }
  667. tp->t_blk_res += blocks;
  668. }
  669. /*
  670. * Reserve the log space needed for this transaction.
  671. */
  672. if (logspace > 0) {
  673. bool permanent = false;
  674. ASSERT(tp->t_log_res == 0 || tp->t_log_res == logspace);
  675. ASSERT(tp->t_log_count == 0 || tp->t_log_count == logcount);
  676. if (flags & XFS_TRANS_PERM_LOG_RES) {
  677. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  678. permanent = true;
  679. } else {
  680. ASSERT(tp->t_ticket == NULL);
  681. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  682. }
  683. if (tp->t_ticket != NULL) {
  684. ASSERT(flags & XFS_TRANS_PERM_LOG_RES);
  685. error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
  686. } else {
  687. error = xfs_log_reserve(tp->t_mountp, logspace,
  688. logcount, &tp->t_ticket,
  689. XFS_TRANSACTION, permanent,
  690. tp->t_type);
  691. }
  692. if (error)
  693. goto undo_blocks;
  694. tp->t_log_res = logspace;
  695. tp->t_log_count = logcount;
  696. }
  697. /*
  698. * Attempt to reserve the needed realtime extents by decrementing
  699. * the number needed from the number available. This will
  700. * fail if the count would go below zero.
  701. */
  702. if (rtextents > 0) {
  703. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
  704. -((int64_t)rtextents), rsvd);
  705. if (error) {
  706. error = XFS_ERROR(ENOSPC);
  707. goto undo_log;
  708. }
  709. tp->t_rtx_res += rtextents;
  710. }
  711. return 0;
  712. /*
  713. * Error cases jump to one of these labels to undo any
  714. * reservations which have already been performed.
  715. */
  716. undo_log:
  717. if (logspace > 0) {
  718. int log_flags;
  719. if (flags & XFS_TRANS_PERM_LOG_RES) {
  720. log_flags = XFS_LOG_REL_PERM_RESERV;
  721. } else {
  722. log_flags = 0;
  723. }
  724. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  725. tp->t_ticket = NULL;
  726. tp->t_log_res = 0;
  727. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  728. }
  729. undo_blocks:
  730. if (blocks > 0) {
  731. xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
  732. (int64_t)blocks, rsvd);
  733. tp->t_blk_res = 0;
  734. }
  735. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  736. return error;
  737. }
  738. /*
  739. * Record the indicated change to the given field for application
  740. * to the file system's superblock when the transaction commits.
  741. * For now, just store the change in the transaction structure.
  742. *
  743. * Mark the transaction structure to indicate that the superblock
  744. * needs to be updated before committing.
  745. *
  746. * Because we may not be keeping track of allocated/free inodes and
  747. * used filesystem blocks in the superblock, we do not mark the
  748. * superblock dirty in this transaction if we modify these fields.
  749. * We still need to update the transaction deltas so that they get
  750. * applied to the incore superblock, but we don't want them to
  751. * cause the superblock to get locked and logged if these are the
  752. * only fields in the superblock that the transaction modifies.
  753. */
  754. void
  755. xfs_trans_mod_sb(
  756. xfs_trans_t *tp,
  757. uint field,
  758. int64_t delta)
  759. {
  760. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  761. xfs_mount_t *mp = tp->t_mountp;
  762. switch (field) {
  763. case XFS_TRANS_SB_ICOUNT:
  764. tp->t_icount_delta += delta;
  765. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  766. flags &= ~XFS_TRANS_SB_DIRTY;
  767. break;
  768. case XFS_TRANS_SB_IFREE:
  769. tp->t_ifree_delta += delta;
  770. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  771. flags &= ~XFS_TRANS_SB_DIRTY;
  772. break;
  773. case XFS_TRANS_SB_FDBLOCKS:
  774. /*
  775. * Track the number of blocks allocated in the
  776. * transaction. Make sure it does not exceed the
  777. * number reserved.
  778. */
  779. if (delta < 0) {
  780. tp->t_blk_res_used += (uint)-delta;
  781. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  782. }
  783. tp->t_fdblocks_delta += delta;
  784. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  785. flags &= ~XFS_TRANS_SB_DIRTY;
  786. break;
  787. case XFS_TRANS_SB_RES_FDBLOCKS:
  788. /*
  789. * The allocation has already been applied to the
  790. * in-core superblock's counter. This should only
  791. * be applied to the on-disk superblock.
  792. */
  793. ASSERT(delta < 0);
  794. tp->t_res_fdblocks_delta += delta;
  795. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  796. flags &= ~XFS_TRANS_SB_DIRTY;
  797. break;
  798. case XFS_TRANS_SB_FREXTENTS:
  799. /*
  800. * Track the number of blocks allocated in the
  801. * transaction. Make sure it does not exceed the
  802. * number reserved.
  803. */
  804. if (delta < 0) {
  805. tp->t_rtx_res_used += (uint)-delta;
  806. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  807. }
  808. tp->t_frextents_delta += delta;
  809. break;
  810. case XFS_TRANS_SB_RES_FREXTENTS:
  811. /*
  812. * The allocation has already been applied to the
  813. * in-core superblock's counter. This should only
  814. * be applied to the on-disk superblock.
  815. */
  816. ASSERT(delta < 0);
  817. tp->t_res_frextents_delta += delta;
  818. break;
  819. case XFS_TRANS_SB_DBLOCKS:
  820. ASSERT(delta > 0);
  821. tp->t_dblocks_delta += delta;
  822. break;
  823. case XFS_TRANS_SB_AGCOUNT:
  824. ASSERT(delta > 0);
  825. tp->t_agcount_delta += delta;
  826. break;
  827. case XFS_TRANS_SB_IMAXPCT:
  828. tp->t_imaxpct_delta += delta;
  829. break;
  830. case XFS_TRANS_SB_REXTSIZE:
  831. tp->t_rextsize_delta += delta;
  832. break;
  833. case XFS_TRANS_SB_RBMBLOCKS:
  834. tp->t_rbmblocks_delta += delta;
  835. break;
  836. case XFS_TRANS_SB_RBLOCKS:
  837. tp->t_rblocks_delta += delta;
  838. break;
  839. case XFS_TRANS_SB_REXTENTS:
  840. tp->t_rextents_delta += delta;
  841. break;
  842. case XFS_TRANS_SB_REXTSLOG:
  843. tp->t_rextslog_delta += delta;
  844. break;
  845. default:
  846. ASSERT(0);
  847. return;
  848. }
  849. tp->t_flags |= flags;
  850. }
  851. /*
  852. * xfs_trans_apply_sb_deltas() is called from the commit code
  853. * to bring the superblock buffer into the current transaction
  854. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  855. *
  856. * For now we just look at each field allowed to change and change
  857. * it if necessary.
  858. */
  859. STATIC void
  860. xfs_trans_apply_sb_deltas(
  861. xfs_trans_t *tp)
  862. {
  863. xfs_dsb_t *sbp;
  864. xfs_buf_t *bp;
  865. int whole = 0;
  866. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  867. sbp = XFS_BUF_TO_SBP(bp);
  868. /*
  869. * Check that superblock mods match the mods made to AGF counters.
  870. */
  871. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  872. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  873. tp->t_ag_btree_delta));
  874. /*
  875. * Only update the superblock counters if we are logging them
  876. */
  877. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  878. if (tp->t_icount_delta)
  879. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  880. if (tp->t_ifree_delta)
  881. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  882. if (tp->t_fdblocks_delta)
  883. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  884. if (tp->t_res_fdblocks_delta)
  885. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  886. }
  887. if (tp->t_frextents_delta)
  888. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  889. if (tp->t_res_frextents_delta)
  890. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  891. if (tp->t_dblocks_delta) {
  892. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  893. whole = 1;
  894. }
  895. if (tp->t_agcount_delta) {
  896. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  897. whole = 1;
  898. }
  899. if (tp->t_imaxpct_delta) {
  900. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  901. whole = 1;
  902. }
  903. if (tp->t_rextsize_delta) {
  904. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  905. whole = 1;
  906. }
  907. if (tp->t_rbmblocks_delta) {
  908. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  909. whole = 1;
  910. }
  911. if (tp->t_rblocks_delta) {
  912. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  913. whole = 1;
  914. }
  915. if (tp->t_rextents_delta) {
  916. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  917. whole = 1;
  918. }
  919. if (tp->t_rextslog_delta) {
  920. sbp->sb_rextslog += tp->t_rextslog_delta;
  921. whole = 1;
  922. }
  923. if (whole)
  924. /*
  925. * Log the whole thing, the fields are noncontiguous.
  926. */
  927. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  928. else
  929. /*
  930. * Since all the modifiable fields are contiguous, we
  931. * can get away with this.
  932. */
  933. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  934. offsetof(xfs_dsb_t, sb_frextents) +
  935. sizeof(sbp->sb_frextents) - 1);
  936. }
  937. /*
  938. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  939. * and apply superblock counter changes to the in-core superblock. The
  940. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  941. * applied to the in-core superblock. The idea is that that has already been
  942. * done.
  943. *
  944. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  945. * However, we have to ensure that we only modify each superblock field only
  946. * once because the application of the delta values may not be atomic. That can
  947. * lead to ENOSPC races occurring if we have two separate modifcations of the
  948. * free space counter to put back the entire reservation and then take away
  949. * what we used.
  950. *
  951. * If we are not logging superblock counters, then the inode allocated/free and
  952. * used block counts are not updated in the on disk superblock. In this case,
  953. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  954. * still need to update the incore superblock with the changes.
  955. */
  956. void
  957. xfs_trans_unreserve_and_mod_sb(
  958. xfs_trans_t *tp)
  959. {
  960. xfs_mod_sb_t msb[9]; /* If you add cases, add entries */
  961. xfs_mod_sb_t *msbp;
  962. xfs_mount_t *mp = tp->t_mountp;
  963. /* REFERENCED */
  964. int error;
  965. int rsvd;
  966. int64_t blkdelta = 0;
  967. int64_t rtxdelta = 0;
  968. int64_t idelta = 0;
  969. int64_t ifreedelta = 0;
  970. msbp = msb;
  971. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  972. /* calculate deltas */
  973. if (tp->t_blk_res > 0)
  974. blkdelta = tp->t_blk_res;
  975. if ((tp->t_fdblocks_delta != 0) &&
  976. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  977. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  978. blkdelta += tp->t_fdblocks_delta;
  979. if (tp->t_rtx_res > 0)
  980. rtxdelta = tp->t_rtx_res;
  981. if ((tp->t_frextents_delta != 0) &&
  982. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  983. rtxdelta += tp->t_frextents_delta;
  984. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  985. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  986. idelta = tp->t_icount_delta;
  987. ifreedelta = tp->t_ifree_delta;
  988. }
  989. /* apply the per-cpu counters */
  990. if (blkdelta) {
  991. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  992. blkdelta, rsvd);
  993. if (error)
  994. goto out;
  995. }
  996. if (idelta) {
  997. error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
  998. idelta, rsvd);
  999. if (error)
  1000. goto out_undo_fdblocks;
  1001. }
  1002. if (ifreedelta) {
  1003. error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
  1004. ifreedelta, rsvd);
  1005. if (error)
  1006. goto out_undo_icount;
  1007. }
  1008. /* apply remaining deltas */
  1009. if (rtxdelta != 0) {
  1010. msbp->msb_field = XFS_SBS_FREXTENTS;
  1011. msbp->msb_delta = rtxdelta;
  1012. msbp++;
  1013. }
  1014. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  1015. if (tp->t_dblocks_delta != 0) {
  1016. msbp->msb_field = XFS_SBS_DBLOCKS;
  1017. msbp->msb_delta = tp->t_dblocks_delta;
  1018. msbp++;
  1019. }
  1020. if (tp->t_agcount_delta != 0) {
  1021. msbp->msb_field = XFS_SBS_AGCOUNT;
  1022. msbp->msb_delta = tp->t_agcount_delta;
  1023. msbp++;
  1024. }
  1025. if (tp->t_imaxpct_delta != 0) {
  1026. msbp->msb_field = XFS_SBS_IMAX_PCT;
  1027. msbp->msb_delta = tp->t_imaxpct_delta;
  1028. msbp++;
  1029. }
  1030. if (tp->t_rextsize_delta != 0) {
  1031. msbp->msb_field = XFS_SBS_REXTSIZE;
  1032. msbp->msb_delta = tp->t_rextsize_delta;
  1033. msbp++;
  1034. }
  1035. if (tp->t_rbmblocks_delta != 0) {
  1036. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  1037. msbp->msb_delta = tp->t_rbmblocks_delta;
  1038. msbp++;
  1039. }
  1040. if (tp->t_rblocks_delta != 0) {
  1041. msbp->msb_field = XFS_SBS_RBLOCKS;
  1042. msbp->msb_delta = tp->t_rblocks_delta;
  1043. msbp++;
  1044. }
  1045. if (tp->t_rextents_delta != 0) {
  1046. msbp->msb_field = XFS_SBS_REXTENTS;
  1047. msbp->msb_delta = tp->t_rextents_delta;
  1048. msbp++;
  1049. }
  1050. if (tp->t_rextslog_delta != 0) {
  1051. msbp->msb_field = XFS_SBS_REXTSLOG;
  1052. msbp->msb_delta = tp->t_rextslog_delta;
  1053. msbp++;
  1054. }
  1055. }
  1056. /*
  1057. * If we need to change anything, do it.
  1058. */
  1059. if (msbp > msb) {
  1060. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  1061. (uint)(msbp - msb), rsvd);
  1062. if (error)
  1063. goto out_undo_ifreecount;
  1064. }
  1065. return;
  1066. out_undo_ifreecount:
  1067. if (ifreedelta)
  1068. xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
  1069. out_undo_icount:
  1070. if (idelta)
  1071. xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
  1072. out_undo_fdblocks:
  1073. if (blkdelta)
  1074. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
  1075. out:
  1076. ASSERT(error == 0);
  1077. return;
  1078. }
  1079. /*
  1080. * Add the given log item to the transaction's list of log items.
  1081. *
  1082. * The log item will now point to its new descriptor with its li_desc field.
  1083. */
  1084. void
  1085. xfs_trans_add_item(
  1086. struct xfs_trans *tp,
  1087. struct xfs_log_item *lip)
  1088. {
  1089. struct xfs_log_item_desc *lidp;
  1090. ASSERT(lip->li_mountp == tp->t_mountp);
  1091. ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
  1092. lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
  1093. lidp->lid_item = lip;
  1094. lidp->lid_flags = 0;
  1095. list_add_tail(&lidp->lid_trans, &tp->t_items);
  1096. lip->li_desc = lidp;
  1097. }
  1098. STATIC void
  1099. xfs_trans_free_item_desc(
  1100. struct xfs_log_item_desc *lidp)
  1101. {
  1102. list_del_init(&lidp->lid_trans);
  1103. kmem_zone_free(xfs_log_item_desc_zone, lidp);
  1104. }
  1105. /*
  1106. * Unlink and free the given descriptor.
  1107. */
  1108. void
  1109. xfs_trans_del_item(
  1110. struct xfs_log_item *lip)
  1111. {
  1112. xfs_trans_free_item_desc(lip->li_desc);
  1113. lip->li_desc = NULL;
  1114. }
  1115. /*
  1116. * Unlock all of the items of a transaction and free all the descriptors
  1117. * of that transaction.
  1118. */
  1119. void
  1120. xfs_trans_free_items(
  1121. struct xfs_trans *tp,
  1122. xfs_lsn_t commit_lsn,
  1123. int flags)
  1124. {
  1125. struct xfs_log_item_desc *lidp, *next;
  1126. list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
  1127. struct xfs_log_item *lip = lidp->lid_item;
  1128. lip->li_desc = NULL;
  1129. if (commit_lsn != NULLCOMMITLSN)
  1130. IOP_COMMITTING(lip, commit_lsn);
  1131. if (flags & XFS_TRANS_ABORT)
  1132. lip->li_flags |= XFS_LI_ABORTED;
  1133. IOP_UNLOCK(lip);
  1134. xfs_trans_free_item_desc(lidp);
  1135. }
  1136. }
  1137. static inline void
  1138. xfs_log_item_batch_insert(
  1139. struct xfs_ail *ailp,
  1140. struct xfs_ail_cursor *cur,
  1141. struct xfs_log_item **log_items,
  1142. int nr_items,
  1143. xfs_lsn_t commit_lsn)
  1144. {
  1145. int i;
  1146. spin_lock(&ailp->xa_lock);
  1147. /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
  1148. xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
  1149. for (i = 0; i < nr_items; i++)
  1150. IOP_UNPIN(log_items[i], 0);
  1151. }
  1152. /*
  1153. * Bulk operation version of xfs_trans_committed that takes a log vector of
  1154. * items to insert into the AIL. This uses bulk AIL insertion techniques to
  1155. * minimise lock traffic.
  1156. *
  1157. * If we are called with the aborted flag set, it is because a log write during
  1158. * a CIL checkpoint commit has failed. In this case, all the items in the
  1159. * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
  1160. * means that checkpoint commit abort handling is treated exactly the same
  1161. * as an iclog write error even though we haven't started any IO yet. Hence in
  1162. * this case all we need to do is IOP_COMMITTED processing, followed by an
  1163. * IOP_UNPIN(aborted) call.
  1164. *
  1165. * The AIL cursor is used to optimise the insert process. If commit_lsn is not
  1166. * at the end of the AIL, the insert cursor avoids the need to walk
  1167. * the AIL to find the insertion point on every xfs_log_item_batch_insert()
  1168. * call. This saves a lot of needless list walking and is a net win, even
  1169. * though it slightly increases that amount of AIL lock traffic to set it up
  1170. * and tear it down.
  1171. */
  1172. void
  1173. xfs_trans_committed_bulk(
  1174. struct xfs_ail *ailp,
  1175. struct xfs_log_vec *log_vector,
  1176. xfs_lsn_t commit_lsn,
  1177. int aborted)
  1178. {
  1179. #define LOG_ITEM_BATCH_SIZE 32
  1180. struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
  1181. struct xfs_log_vec *lv;
  1182. struct xfs_ail_cursor cur;
  1183. int i = 0;
  1184. spin_lock(&ailp->xa_lock);
  1185. xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
  1186. spin_unlock(&ailp->xa_lock);
  1187. /* unpin all the log items */
  1188. for (lv = log_vector; lv; lv = lv->lv_next ) {
  1189. struct xfs_log_item *lip = lv->lv_item;
  1190. xfs_lsn_t item_lsn;
  1191. if (aborted)
  1192. lip->li_flags |= XFS_LI_ABORTED;
  1193. item_lsn = IOP_COMMITTED(lip, commit_lsn);
  1194. /* item_lsn of -1 means the item needs no further processing */
  1195. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  1196. continue;
  1197. /*
  1198. * if we are aborting the operation, no point in inserting the
  1199. * object into the AIL as we are in a shutdown situation.
  1200. */
  1201. if (aborted) {
  1202. ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
  1203. IOP_UNPIN(lip, 1);
  1204. continue;
  1205. }
  1206. if (item_lsn != commit_lsn) {
  1207. /*
  1208. * Not a bulk update option due to unusual item_lsn.
  1209. * Push into AIL immediately, rechecking the lsn once
  1210. * we have the ail lock. Then unpin the item. This does
  1211. * not affect the AIL cursor the bulk insert path is
  1212. * using.
  1213. */
  1214. spin_lock(&ailp->xa_lock);
  1215. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
  1216. xfs_trans_ail_update(ailp, lip, item_lsn);
  1217. else
  1218. spin_unlock(&ailp->xa_lock);
  1219. IOP_UNPIN(lip, 0);
  1220. continue;
  1221. }
  1222. /* Item is a candidate for bulk AIL insert. */
  1223. log_items[i++] = lv->lv_item;
  1224. if (i >= LOG_ITEM_BATCH_SIZE) {
  1225. xfs_log_item_batch_insert(ailp, &cur, log_items,
  1226. LOG_ITEM_BATCH_SIZE, commit_lsn);
  1227. i = 0;
  1228. }
  1229. }
  1230. /* make sure we insert the remainder! */
  1231. if (i)
  1232. xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
  1233. spin_lock(&ailp->xa_lock);
  1234. xfs_trans_ail_cursor_done(ailp, &cur);
  1235. spin_unlock(&ailp->xa_lock);
  1236. }
  1237. /*
  1238. * Commit the given transaction to the log.
  1239. *
  1240. * XFS disk error handling mechanism is not based on a typical
  1241. * transaction abort mechanism. Logically after the filesystem
  1242. * gets marked 'SHUTDOWN', we can't let any new transactions
  1243. * be durable - ie. committed to disk - because some metadata might
  1244. * be inconsistent. In such cases, this returns an error, and the
  1245. * caller may assume that all locked objects joined to the transaction
  1246. * have already been unlocked as if the commit had succeeded.
  1247. * Do not reference the transaction structure after this call.
  1248. */
  1249. int
  1250. xfs_trans_commit(
  1251. struct xfs_trans *tp,
  1252. uint flags)
  1253. {
  1254. struct xfs_mount *mp = tp->t_mountp;
  1255. xfs_lsn_t commit_lsn = -1;
  1256. int error = 0;
  1257. int log_flags = 0;
  1258. int sync = tp->t_flags & XFS_TRANS_SYNC;
  1259. /*
  1260. * Determine whether this commit is releasing a permanent
  1261. * log reservation or not.
  1262. */
  1263. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1264. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1265. log_flags = XFS_LOG_REL_PERM_RESERV;
  1266. }
  1267. /*
  1268. * If there is nothing to be logged by the transaction,
  1269. * then unlock all of the items associated with the
  1270. * transaction and free the transaction structure.
  1271. * Also make sure to return any reserved blocks to
  1272. * the free pool.
  1273. */
  1274. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  1275. goto out_unreserve;
  1276. if (XFS_FORCED_SHUTDOWN(mp)) {
  1277. error = XFS_ERROR(EIO);
  1278. goto out_unreserve;
  1279. }
  1280. ASSERT(tp->t_ticket != NULL);
  1281. /*
  1282. * If we need to update the superblock, then do it now.
  1283. */
  1284. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  1285. xfs_trans_apply_sb_deltas(tp);
  1286. xfs_trans_apply_dquot_deltas(tp);
  1287. error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
  1288. if (error == ENOMEM) {
  1289. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  1290. error = XFS_ERROR(EIO);
  1291. goto out_unreserve;
  1292. }
  1293. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1294. xfs_trans_free(tp);
  1295. /*
  1296. * If the transaction needs to be synchronous, then force the
  1297. * log out now and wait for it.
  1298. */
  1299. if (sync) {
  1300. if (!error) {
  1301. error = _xfs_log_force_lsn(mp, commit_lsn,
  1302. XFS_LOG_SYNC, NULL);
  1303. }
  1304. XFS_STATS_INC(xs_trans_sync);
  1305. } else {
  1306. XFS_STATS_INC(xs_trans_async);
  1307. }
  1308. return error;
  1309. out_unreserve:
  1310. xfs_trans_unreserve_and_mod_sb(tp);
  1311. /*
  1312. * It is indeed possible for the transaction to be not dirty but
  1313. * the dqinfo portion to be. All that means is that we have some
  1314. * (non-persistent) quota reservations that need to be unreserved.
  1315. */
  1316. xfs_trans_unreserve_and_mod_dquots(tp);
  1317. if (tp->t_ticket) {
  1318. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1319. if (commit_lsn == -1 && !error)
  1320. error = XFS_ERROR(EIO);
  1321. }
  1322. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1323. xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
  1324. xfs_trans_free(tp);
  1325. XFS_STATS_INC(xs_trans_empty);
  1326. return error;
  1327. }
  1328. /*
  1329. * Unlock all of the transaction's items and free the transaction.
  1330. * The transaction must not have modified any of its items, because
  1331. * there is no way to restore them to their previous state.
  1332. *
  1333. * If the transaction has made a log reservation, make sure to release
  1334. * it as well.
  1335. */
  1336. void
  1337. xfs_trans_cancel(
  1338. xfs_trans_t *tp,
  1339. int flags)
  1340. {
  1341. int log_flags;
  1342. xfs_mount_t *mp = tp->t_mountp;
  1343. /*
  1344. * See if the caller is being too lazy to figure out if
  1345. * the transaction really needs an abort.
  1346. */
  1347. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  1348. flags &= ~XFS_TRANS_ABORT;
  1349. /*
  1350. * See if the caller is relying on us to shut down the
  1351. * filesystem. This happens in paths where we detect
  1352. * corruption and decide to give up.
  1353. */
  1354. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  1355. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  1356. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1357. }
  1358. #ifdef DEBUG
  1359. if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
  1360. struct xfs_log_item_desc *lidp;
  1361. list_for_each_entry(lidp, &tp->t_items, lid_trans)
  1362. ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
  1363. }
  1364. #endif
  1365. xfs_trans_unreserve_and_mod_sb(tp);
  1366. xfs_trans_unreserve_and_mod_dquots(tp);
  1367. if (tp->t_ticket) {
  1368. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1369. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1370. log_flags = XFS_LOG_REL_PERM_RESERV;
  1371. } else {
  1372. log_flags = 0;
  1373. }
  1374. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1375. }
  1376. /* mark this thread as no longer being in a transaction */
  1377. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1378. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1379. xfs_trans_free(tp);
  1380. }
  1381. /*
  1382. * Roll from one trans in the sequence of PERMANENT transactions to
  1383. * the next: permanent transactions are only flushed out when
  1384. * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
  1385. * as possible to let chunks of it go to the log. So we commit the
  1386. * chunk we've been working on and get a new transaction to continue.
  1387. */
  1388. int
  1389. xfs_trans_roll(
  1390. struct xfs_trans **tpp,
  1391. struct xfs_inode *dp)
  1392. {
  1393. struct xfs_trans *trans;
  1394. unsigned int logres, count;
  1395. int error;
  1396. /*
  1397. * Ensure that the inode is always logged.
  1398. */
  1399. trans = *tpp;
  1400. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  1401. /*
  1402. * Copy the critical parameters from one trans to the next.
  1403. */
  1404. logres = trans->t_log_res;
  1405. count = trans->t_log_count;
  1406. *tpp = xfs_trans_dup(trans);
  1407. /*
  1408. * Commit the current transaction.
  1409. * If this commit failed, then it'd just unlock those items that
  1410. * are not marked ihold. That also means that a filesystem shutdown
  1411. * is in progress. The caller takes the responsibility to cancel
  1412. * the duplicate transaction that gets returned.
  1413. */
  1414. error = xfs_trans_commit(trans, 0);
  1415. if (error)
  1416. return (error);
  1417. trans = *tpp;
  1418. /*
  1419. * transaction commit worked ok so we can drop the extra ticket
  1420. * reference that we gained in xfs_trans_dup()
  1421. */
  1422. xfs_log_ticket_put(trans->t_ticket);
  1423. /*
  1424. * Reserve space in the log for th next transaction.
  1425. * This also pushes items in the "AIL", the list of logged items,
  1426. * out to disk if they are taking up space at the tail of the log
  1427. * that we want to use. This requires that either nothing be locked
  1428. * across this call, or that anything that is locked be logged in
  1429. * the prior and the next transactions.
  1430. */
  1431. error = xfs_trans_reserve(trans, 0, logres, 0,
  1432. XFS_TRANS_PERM_LOG_RES, count);
  1433. /*
  1434. * Ensure that the inode is in the new transaction and locked.
  1435. */
  1436. if (error)
  1437. return error;
  1438. xfs_trans_ijoin(trans, dp, 0);
  1439. return 0;
  1440. }