xfs_trans.c 44 KB

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