xfs_trans.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_error.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_btree.h"
  36. #include "xfs_ialloc.h"
  37. #include "xfs_alloc.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. /*
  46. * Various log reservation values.
  47. *
  48. * These are based on the size of the file system block because that is what
  49. * most transactions manipulate. Each adds in an additional 128 bytes per
  50. * item logged to try to account for the overhead of the transaction mechanism.
  51. *
  52. * Note: Most of the reservations underestimate the number of allocation
  53. * groups into which they could free extents in the xfs_bmap_finish() call.
  54. * This is because the number in the worst case is quite high and quite
  55. * unusual. In order to fix this we need to change xfs_bmap_finish() to free
  56. * extents in only a single AG at a time. This will require changes to the
  57. * EFI code as well, however, so that the EFI for the extents not freed is
  58. * logged again in each transaction. See SGI PV #261917.
  59. *
  60. * Reservation functions here avoid a huge stack in xfs_trans_init due to
  61. * register overflow from temporaries in the calculations.
  62. */
  63. /*
  64. * In a write transaction we can allocate a maximum of 2
  65. * extents. This gives:
  66. * the inode getting the new extents: inode size
  67. * the inode's bmap btree: max depth * block size
  68. * the agfs of the ags from which the extents are allocated: 2 * sector
  69. * the superblock free block counter: sector size
  70. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  71. * And the bmap_finish transaction can free bmap blocks in a join:
  72. * the agfs of the ags containing the blocks: 2 * sector size
  73. * the agfls of the ags containing the blocks: 2 * sector size
  74. * the super block free block counter: sector size
  75. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  76. */
  77. STATIC uint
  78. xfs_calc_write_reservation(
  79. struct xfs_mount *mp)
  80. {
  81. return XFS_DQUOT_LOGRES(mp) +
  82. MAX((mp->m_sb.sb_inodesize +
  83. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
  84. 2 * mp->m_sb.sb_sectsize +
  85. mp->m_sb.sb_sectsize +
  86. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  87. 128 * (4 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
  88. XFS_ALLOCFREE_LOG_COUNT(mp, 2))),
  89. (2 * mp->m_sb.sb_sectsize +
  90. 2 * mp->m_sb.sb_sectsize +
  91. mp->m_sb.sb_sectsize +
  92. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  93. 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
  94. }
  95. /*
  96. * In truncating a file we free up to two extents at once. We can modify:
  97. * the inode being truncated: inode size
  98. * the inode's bmap btree: (max depth + 1) * block size
  99. * And the bmap_finish transaction can free the blocks and bmap blocks:
  100. * the agf for each of the ags: 4 * sector size
  101. * the agfl for each of the ags: 4 * sector size
  102. * the super block to reflect the freed blocks: sector size
  103. * worst case split in allocation btrees per extent assuming 4 extents:
  104. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  105. * the inode btree: max depth * blocksize
  106. * the allocation btrees: 2 trees * (max depth - 1) * block size
  107. */
  108. STATIC uint
  109. xfs_calc_itruncate_reservation(
  110. struct xfs_mount *mp)
  111. {
  112. return XFS_DQUOT_LOGRES(mp) +
  113. MAX((mp->m_sb.sb_inodesize +
  114. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1) +
  115. 128 * (2 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
  116. (4 * mp->m_sb.sb_sectsize +
  117. 4 * mp->m_sb.sb_sectsize +
  118. mp->m_sb.sb_sectsize +
  119. XFS_ALLOCFREE_LOG_RES(mp, 4) +
  120. 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4)) +
  121. 128 * 5 +
  122. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  123. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  124. XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  125. }
  126. /*
  127. * In renaming a files we can modify:
  128. * the four inodes involved: 4 * inode size
  129. * the two directory btrees: 2 * (max depth + v2) * dir block size
  130. * the two directory bmap btrees: 2 * max depth * block size
  131. * And the bmap_finish transaction can free dir and bmap blocks (two sets
  132. * of bmap blocks) giving:
  133. * the agf for the ags in which the blocks live: 3 * sector size
  134. * the agfl for the ags in which the blocks live: 3 * sector size
  135. * the superblock for the free block count: sector size
  136. * the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
  137. */
  138. STATIC uint
  139. xfs_calc_rename_reservation(
  140. struct xfs_mount *mp)
  141. {
  142. return XFS_DQUOT_LOGRES(mp) +
  143. MAX((4 * mp->m_sb.sb_inodesize +
  144. 2 * XFS_DIROP_LOG_RES(mp) +
  145. 128 * (4 + 2 * XFS_DIROP_LOG_COUNT(mp))),
  146. (3 * mp->m_sb.sb_sectsize +
  147. 3 * mp->m_sb.sb_sectsize +
  148. mp->m_sb.sb_sectsize +
  149. XFS_ALLOCFREE_LOG_RES(mp, 3) +
  150. 128 * (7 + XFS_ALLOCFREE_LOG_COUNT(mp, 3))));
  151. }
  152. /*
  153. * For creating a link to an inode:
  154. * the parent directory inode: inode size
  155. * the linked inode: inode size
  156. * the directory btree could split: (max depth + v2) * dir block size
  157. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  158. * And the bmap_finish transaction can free some bmap blocks giving:
  159. * the agf for the ag in which the blocks live: sector size
  160. * the agfl for the ag in which the blocks live: sector size
  161. * the superblock for the free block count: sector size
  162. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  163. */
  164. STATIC uint
  165. xfs_calc_link_reservation(
  166. struct xfs_mount *mp)
  167. {
  168. return XFS_DQUOT_LOGRES(mp) +
  169. MAX((mp->m_sb.sb_inodesize +
  170. mp->m_sb.sb_inodesize +
  171. XFS_DIROP_LOG_RES(mp) +
  172. 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
  173. (mp->m_sb.sb_sectsize +
  174. mp->m_sb.sb_sectsize +
  175. mp->m_sb.sb_sectsize +
  176. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  177. 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  178. }
  179. /*
  180. * For removing a directory entry we can modify:
  181. * the parent directory inode: inode size
  182. * the removed inode: inode size
  183. * the directory btree could join: (max depth + v2) * dir block size
  184. * the directory bmap btree could join or split: (max depth + v2) * blocksize
  185. * And the bmap_finish transaction can free the dir and bmap blocks giving:
  186. * the agf for the ag in which the blocks live: 2 * sector size
  187. * the agfl for the ag in which the blocks live: 2 * sector size
  188. * the superblock for the free block count: sector size
  189. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  190. */
  191. STATIC uint
  192. xfs_calc_remove_reservation(
  193. struct xfs_mount *mp)
  194. {
  195. return XFS_DQUOT_LOGRES(mp) +
  196. MAX((mp->m_sb.sb_inodesize +
  197. mp->m_sb.sb_inodesize +
  198. XFS_DIROP_LOG_RES(mp) +
  199. 128 * (2 + XFS_DIROP_LOG_COUNT(mp))),
  200. (2 * mp->m_sb.sb_sectsize +
  201. 2 * mp->m_sb.sb_sectsize +
  202. mp->m_sb.sb_sectsize +
  203. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  204. 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
  205. }
  206. /*
  207. * For symlink we can modify:
  208. * the parent directory inode: inode size
  209. * the new inode: inode size
  210. * the inode btree entry: 1 block
  211. * the directory btree: (max depth + v2) * dir block size
  212. * the directory inode's bmap btree: (max depth + v2) * block size
  213. * the blocks for the symlink: 1 kB
  214. * Or in the first xact we allocate some inodes giving:
  215. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  216. * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
  217. * the inode btree: max depth * blocksize
  218. * the allocation btrees: 2 trees * (2 * max depth - 1) * block size
  219. */
  220. STATIC uint
  221. xfs_calc_symlink_reservation(
  222. struct xfs_mount *mp)
  223. {
  224. return XFS_DQUOT_LOGRES(mp) +
  225. MAX((mp->m_sb.sb_inodesize +
  226. mp->m_sb.sb_inodesize +
  227. XFS_FSB_TO_B(mp, 1) +
  228. XFS_DIROP_LOG_RES(mp) +
  229. 1024 +
  230. 128 * (4 + XFS_DIROP_LOG_COUNT(mp))),
  231. (2 * mp->m_sb.sb_sectsize +
  232. XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
  233. XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
  234. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  235. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  236. XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  237. }
  238. /*
  239. * For create we can modify:
  240. * the parent directory inode: inode size
  241. * the new inode: inode size
  242. * the inode btree entry: block size
  243. * the superblock for the nlink flag: sector size
  244. * the directory btree: (max depth + v2) * dir block size
  245. * the directory inode's bmap btree: (max depth + v2) * block size
  246. * Or in the first xact we allocate some inodes giving:
  247. * the agi and agf of the ag getting the new inodes: 2 * sectorsize
  248. * the superblock for the nlink flag: sector size
  249. * the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
  250. * the inode btree: max depth * blocksize
  251. * the allocation btrees: 2 trees * (max depth - 1) * block size
  252. */
  253. STATIC uint
  254. xfs_calc_create_reservation(
  255. struct xfs_mount *mp)
  256. {
  257. return XFS_DQUOT_LOGRES(mp) +
  258. MAX((mp->m_sb.sb_inodesize +
  259. mp->m_sb.sb_inodesize +
  260. mp->m_sb.sb_sectsize +
  261. XFS_FSB_TO_B(mp, 1) +
  262. XFS_DIROP_LOG_RES(mp) +
  263. 128 * (3 + XFS_DIROP_LOG_COUNT(mp))),
  264. (3 * mp->m_sb.sb_sectsize +
  265. XFS_FSB_TO_B(mp, XFS_IALLOC_BLOCKS(mp)) +
  266. XFS_FSB_TO_B(mp, mp->m_in_maxlevels) +
  267. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  268. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  269. XFS_ALLOCFREE_LOG_COUNT(mp, 1))));
  270. }
  271. /*
  272. * Making a new directory is the same as creating a new file.
  273. */
  274. STATIC uint
  275. xfs_calc_mkdir_reservation(
  276. struct xfs_mount *mp)
  277. {
  278. return xfs_calc_create_reservation(mp);
  279. }
  280. /*
  281. * In freeing an inode we can modify:
  282. * the inode being freed: inode size
  283. * the super block free inode counter: sector size
  284. * the agi hash list and counters: sector size
  285. * the inode btree entry: block size
  286. * the on disk inode before ours in the agi hash list: inode cluster size
  287. * the inode btree: max depth * blocksize
  288. * the allocation btrees: 2 trees * (max depth - 1) * block size
  289. */
  290. STATIC uint
  291. xfs_calc_ifree_reservation(
  292. struct xfs_mount *mp)
  293. {
  294. return XFS_DQUOT_LOGRES(mp) +
  295. mp->m_sb.sb_inodesize +
  296. mp->m_sb.sb_sectsize +
  297. mp->m_sb.sb_sectsize +
  298. XFS_FSB_TO_B(mp, 1) +
  299. MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
  300. XFS_INODE_CLUSTER_SIZE(mp)) +
  301. 128 * 5 +
  302. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  303. 128 * (2 + XFS_IALLOC_BLOCKS(mp) + mp->m_in_maxlevels +
  304. XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  305. }
  306. /*
  307. * When only changing the inode we log the inode and possibly the superblock
  308. * We also add a bit of slop for the transaction stuff.
  309. */
  310. STATIC uint
  311. xfs_calc_ichange_reservation(
  312. struct xfs_mount *mp)
  313. {
  314. return XFS_DQUOT_LOGRES(mp) +
  315. mp->m_sb.sb_inodesize +
  316. mp->m_sb.sb_sectsize +
  317. 512;
  318. }
  319. /*
  320. * Growing the data section of the filesystem.
  321. * superblock
  322. * agi and agf
  323. * allocation btrees
  324. */
  325. STATIC uint
  326. xfs_calc_growdata_reservation(
  327. struct xfs_mount *mp)
  328. {
  329. return mp->m_sb.sb_sectsize * 3 +
  330. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  331. 128 * (3 + XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  332. }
  333. /*
  334. * Growing the rt section of the filesystem.
  335. * In the first set of transactions (ALLOC) we allocate space to the
  336. * bitmap or summary files.
  337. * superblock: sector size
  338. * agf of the ag from which the extent is allocated: sector size
  339. * bmap btree for bitmap/summary inode: max depth * blocksize
  340. * bitmap/summary inode: inode size
  341. * allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
  342. */
  343. STATIC uint
  344. xfs_calc_growrtalloc_reservation(
  345. struct xfs_mount *mp)
  346. {
  347. return 2 * mp->m_sb.sb_sectsize +
  348. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK)) +
  349. mp->m_sb.sb_inodesize +
  350. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  351. 128 * (3 + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) +
  352. XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  353. }
  354. /*
  355. * Growing the rt section of the filesystem.
  356. * In the second set of transactions (ZERO) we zero the new metadata blocks.
  357. * one bitmap/summary block: blocksize
  358. */
  359. STATIC uint
  360. xfs_calc_growrtzero_reservation(
  361. struct xfs_mount *mp)
  362. {
  363. return mp->m_sb.sb_blocksize + 128;
  364. }
  365. /*
  366. * Growing the rt section of the filesystem.
  367. * In the third set of transactions (FREE) we update metadata without
  368. * allocating any new blocks.
  369. * superblock: sector size
  370. * bitmap inode: inode size
  371. * summary inode: inode size
  372. * one bitmap block: blocksize
  373. * summary blocks: new summary size
  374. */
  375. STATIC uint
  376. xfs_calc_growrtfree_reservation(
  377. struct xfs_mount *mp)
  378. {
  379. return mp->m_sb.sb_sectsize +
  380. 2 * mp->m_sb.sb_inodesize +
  381. mp->m_sb.sb_blocksize +
  382. mp->m_rsumsize +
  383. 128 * 5;
  384. }
  385. /*
  386. * Logging the inode modification timestamp on a synchronous write.
  387. * inode
  388. */
  389. STATIC uint
  390. xfs_calc_swrite_reservation(
  391. struct xfs_mount *mp)
  392. {
  393. return mp->m_sb.sb_inodesize + 128;
  394. }
  395. /*
  396. * Logging the inode mode bits when writing a setuid/setgid file
  397. * inode
  398. */
  399. STATIC uint
  400. xfs_calc_writeid_reservation(xfs_mount_t *mp)
  401. {
  402. return mp->m_sb.sb_inodesize + 128;
  403. }
  404. /*
  405. * Converting the inode from non-attributed to attributed.
  406. * the inode being converted: inode size
  407. * agf block and superblock (for block allocation)
  408. * the new block (directory sized)
  409. * bmap blocks for the new directory block
  410. * allocation btrees
  411. */
  412. STATIC uint
  413. xfs_calc_addafork_reservation(
  414. struct xfs_mount *mp)
  415. {
  416. return XFS_DQUOT_LOGRES(mp) +
  417. mp->m_sb.sb_inodesize +
  418. mp->m_sb.sb_sectsize * 2 +
  419. mp->m_dirblksize +
  420. XFS_FSB_TO_B(mp, XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1) +
  421. XFS_ALLOCFREE_LOG_RES(mp, 1) +
  422. 128 * (4 + XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1 +
  423. XFS_ALLOCFREE_LOG_COUNT(mp, 1));
  424. }
  425. /*
  426. * Removing the attribute fork of a file
  427. * the inode being truncated: inode size
  428. * the inode's bmap btree: max depth * block size
  429. * And the bmap_finish transaction can free the blocks and bmap blocks:
  430. * the agf for each of the ags: 4 * sector size
  431. * the agfl for each of the ags: 4 * sector size
  432. * the super block to reflect the freed blocks: sector size
  433. * worst case split in allocation btrees per extent assuming 4 extents:
  434. * 4 exts * 2 trees * (2 * max depth - 1) * block size
  435. */
  436. STATIC uint
  437. xfs_calc_attrinval_reservation(
  438. struct xfs_mount *mp)
  439. {
  440. return MAX((mp->m_sb.sb_inodesize +
  441. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
  442. 128 * (1 + XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK))),
  443. (4 * mp->m_sb.sb_sectsize +
  444. 4 * mp->m_sb.sb_sectsize +
  445. mp->m_sb.sb_sectsize +
  446. XFS_ALLOCFREE_LOG_RES(mp, 4) +
  447. 128 * (9 + XFS_ALLOCFREE_LOG_COUNT(mp, 4))));
  448. }
  449. /*
  450. * Setting an attribute.
  451. * the inode getting the attribute
  452. * the superblock for allocations
  453. * the agfs extents are allocated from
  454. * the attribute btree * max depth
  455. * the inode allocation btree
  456. * Since attribute transaction space is dependent on the size of the attribute,
  457. * the calculation is done partially at mount time and partially at runtime.
  458. */
  459. STATIC uint
  460. xfs_calc_attrset_reservation(
  461. struct xfs_mount *mp)
  462. {
  463. return XFS_DQUOT_LOGRES(mp) +
  464. mp->m_sb.sb_inodesize +
  465. mp->m_sb.sb_sectsize +
  466. XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
  467. 128 * (2 + XFS_DA_NODE_MAXDEPTH);
  468. }
  469. /*
  470. * Removing an attribute.
  471. * the inode: inode size
  472. * the attribute btree could join: max depth * block size
  473. * the inode bmap btree could join or split: max depth * block size
  474. * And the bmap_finish transaction can free the attr blocks freed giving:
  475. * the agf for the ag in which the blocks live: 2 * sector size
  476. * the agfl for the ag in which the blocks live: 2 * sector size
  477. * the superblock for the free block count: sector size
  478. * the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
  479. */
  480. STATIC uint
  481. xfs_calc_attrrm_reservation(
  482. struct xfs_mount *mp)
  483. {
  484. return XFS_DQUOT_LOGRES(mp) +
  485. MAX((mp->m_sb.sb_inodesize +
  486. XFS_FSB_TO_B(mp, XFS_DA_NODE_MAXDEPTH) +
  487. XFS_FSB_TO_B(mp, XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
  488. 128 * (1 + XFS_DA_NODE_MAXDEPTH +
  489. XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK))),
  490. (2 * mp->m_sb.sb_sectsize +
  491. 2 * mp->m_sb.sb_sectsize +
  492. mp->m_sb.sb_sectsize +
  493. XFS_ALLOCFREE_LOG_RES(mp, 2) +
  494. 128 * (5 + XFS_ALLOCFREE_LOG_COUNT(mp, 2))));
  495. }
  496. /*
  497. * Clearing a bad agino number in an agi hash bucket.
  498. */
  499. STATIC uint
  500. xfs_calc_clear_agi_bucket_reservation(
  501. struct xfs_mount *mp)
  502. {
  503. return mp->m_sb.sb_sectsize + 128;
  504. }
  505. /*
  506. * Initialize the precomputed transaction reservation values
  507. * in the mount structure.
  508. */
  509. void
  510. xfs_trans_init(
  511. struct xfs_mount *mp)
  512. {
  513. struct xfs_trans_reservations *resp = &mp->m_reservations;
  514. resp->tr_write = xfs_calc_write_reservation(mp);
  515. resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
  516. resp->tr_rename = xfs_calc_rename_reservation(mp);
  517. resp->tr_link = xfs_calc_link_reservation(mp);
  518. resp->tr_remove = xfs_calc_remove_reservation(mp);
  519. resp->tr_symlink = xfs_calc_symlink_reservation(mp);
  520. resp->tr_create = xfs_calc_create_reservation(mp);
  521. resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
  522. resp->tr_ifree = xfs_calc_ifree_reservation(mp);
  523. resp->tr_ichange = xfs_calc_ichange_reservation(mp);
  524. resp->tr_growdata = xfs_calc_growdata_reservation(mp);
  525. resp->tr_swrite = xfs_calc_swrite_reservation(mp);
  526. resp->tr_writeid = xfs_calc_writeid_reservation(mp);
  527. resp->tr_addafork = xfs_calc_addafork_reservation(mp);
  528. resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
  529. resp->tr_attrset = xfs_calc_attrset_reservation(mp);
  530. resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
  531. resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
  532. resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
  533. resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
  534. resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
  535. }
  536. /*
  537. * This routine is called to allocate a transaction structure.
  538. * The type parameter indicates the type of the transaction. These
  539. * are enumerated in xfs_trans.h.
  540. *
  541. * Dynamically allocate the transaction structure from the transaction
  542. * zone, initialize it, and return it to the caller.
  543. */
  544. xfs_trans_t *
  545. xfs_trans_alloc(
  546. xfs_mount_t *mp,
  547. uint type)
  548. {
  549. xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
  550. return _xfs_trans_alloc(mp, type, KM_SLEEP);
  551. }
  552. xfs_trans_t *
  553. _xfs_trans_alloc(
  554. xfs_mount_t *mp,
  555. uint type,
  556. uint memflags)
  557. {
  558. xfs_trans_t *tp;
  559. atomic_inc(&mp->m_active_trans);
  560. tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
  561. tp->t_magic = XFS_TRANS_MAGIC;
  562. tp->t_type = type;
  563. tp->t_mountp = mp;
  564. tp->t_items_free = XFS_LIC_NUM_SLOTS;
  565. xfs_lic_init(&(tp->t_items));
  566. INIT_LIST_HEAD(&tp->t_busy);
  567. return tp;
  568. }
  569. /*
  570. * Free the transaction structure. If there is more clean up
  571. * to do when the structure is freed, add it here.
  572. */
  573. STATIC void
  574. xfs_trans_free(
  575. struct xfs_trans *tp)
  576. {
  577. struct xfs_busy_extent *busyp, *n;
  578. list_for_each_entry_safe(busyp, n, &tp->t_busy, list)
  579. xfs_alloc_busy_clear(tp->t_mountp, busyp);
  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. ntp->t_items_free = XFS_LIC_NUM_SLOTS;
  605. xfs_lic_init(&(ntp->t_items));
  606. INIT_LIST_HEAD(&ntp->t_busy);
  607. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  608. ASSERT(tp->t_ticket != NULL);
  609. ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
  610. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  611. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  612. tp->t_blk_res = tp->t_blk_res_used;
  613. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  614. tp->t_rtx_res = tp->t_rtx_res_used;
  615. ntp->t_pflags = tp->t_pflags;
  616. xfs_trans_dup_dqinfo(tp, ntp);
  617. atomic_inc(&tp->t_mountp->m_active_trans);
  618. return ntp;
  619. }
  620. /*
  621. * This is called to reserve free disk blocks and log space for the
  622. * given transaction. This must be done before allocating any resources
  623. * within the transaction.
  624. *
  625. * This will return ENOSPC if there are not enough blocks available.
  626. * It will sleep waiting for available log space.
  627. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  628. * is used by long running transactions. If any one of the reservations
  629. * fails then they will all be backed out.
  630. *
  631. * This does not do quota reservations. That typically is done by the
  632. * caller afterwards.
  633. */
  634. int
  635. xfs_trans_reserve(
  636. xfs_trans_t *tp,
  637. uint blocks,
  638. uint logspace,
  639. uint rtextents,
  640. uint flags,
  641. uint logcount)
  642. {
  643. int log_flags;
  644. int error = 0;
  645. int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  646. /* Mark this thread as being in a transaction */
  647. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  648. /*
  649. * Attempt to reserve the needed disk blocks by decrementing
  650. * the number needed from the number available. This will
  651. * fail if the count would go below zero.
  652. */
  653. if (blocks > 0) {
  654. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
  655. -((int64_t)blocks), rsvd);
  656. if (error != 0) {
  657. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  658. return (XFS_ERROR(ENOSPC));
  659. }
  660. tp->t_blk_res += blocks;
  661. }
  662. /*
  663. * Reserve the log space needed for this transaction.
  664. */
  665. if (logspace > 0) {
  666. ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
  667. ASSERT((tp->t_log_count == 0) ||
  668. (tp->t_log_count == logcount));
  669. if (flags & XFS_TRANS_PERM_LOG_RES) {
  670. log_flags = XFS_LOG_PERM_RESERV;
  671. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  672. } else {
  673. ASSERT(tp->t_ticket == NULL);
  674. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  675. log_flags = 0;
  676. }
  677. error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
  678. &tp->t_ticket,
  679. XFS_TRANSACTION, log_flags, tp->t_type);
  680. if (error) {
  681. goto undo_blocks;
  682. }
  683. tp->t_log_res = logspace;
  684. tp->t_log_count = logcount;
  685. }
  686. /*
  687. * Attempt to reserve the needed realtime extents by decrementing
  688. * the number needed from the number available. This will
  689. * fail if the count would go below zero.
  690. */
  691. if (rtextents > 0) {
  692. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
  693. -((int64_t)rtextents), rsvd);
  694. if (error) {
  695. error = XFS_ERROR(ENOSPC);
  696. goto undo_log;
  697. }
  698. tp->t_rtx_res += rtextents;
  699. }
  700. return 0;
  701. /*
  702. * Error cases jump to one of these labels to undo any
  703. * reservations which have already been performed.
  704. */
  705. undo_log:
  706. if (logspace > 0) {
  707. if (flags & XFS_TRANS_PERM_LOG_RES) {
  708. log_flags = XFS_LOG_REL_PERM_RESERV;
  709. } else {
  710. log_flags = 0;
  711. }
  712. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  713. tp->t_ticket = NULL;
  714. tp->t_log_res = 0;
  715. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  716. }
  717. undo_blocks:
  718. if (blocks > 0) {
  719. (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
  720. (int64_t)blocks, rsvd);
  721. tp->t_blk_res = 0;
  722. }
  723. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  724. return error;
  725. }
  726. /*
  727. * Record the indicated change to the given field for application
  728. * to the file system's superblock when the transaction commits.
  729. * For now, just store the change in the transaction structure.
  730. *
  731. * Mark the transaction structure to indicate that the superblock
  732. * needs to be updated before committing.
  733. *
  734. * Because we may not be keeping track of allocated/free inodes and
  735. * used filesystem blocks in the superblock, we do not mark the
  736. * superblock dirty in this transaction if we modify these fields.
  737. * We still need to update the transaction deltas so that they get
  738. * applied to the incore superblock, but we don't want them to
  739. * cause the superblock to get locked and logged if these are the
  740. * only fields in the superblock that the transaction modifies.
  741. */
  742. void
  743. xfs_trans_mod_sb(
  744. xfs_trans_t *tp,
  745. uint field,
  746. int64_t delta)
  747. {
  748. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  749. xfs_mount_t *mp = tp->t_mountp;
  750. switch (field) {
  751. case XFS_TRANS_SB_ICOUNT:
  752. tp->t_icount_delta += delta;
  753. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  754. flags &= ~XFS_TRANS_SB_DIRTY;
  755. break;
  756. case XFS_TRANS_SB_IFREE:
  757. tp->t_ifree_delta += delta;
  758. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  759. flags &= ~XFS_TRANS_SB_DIRTY;
  760. break;
  761. case XFS_TRANS_SB_FDBLOCKS:
  762. /*
  763. * Track the number of blocks allocated in the
  764. * transaction. Make sure it does not exceed the
  765. * number reserved.
  766. */
  767. if (delta < 0) {
  768. tp->t_blk_res_used += (uint)-delta;
  769. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  770. }
  771. tp->t_fdblocks_delta += delta;
  772. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  773. flags &= ~XFS_TRANS_SB_DIRTY;
  774. break;
  775. case XFS_TRANS_SB_RES_FDBLOCKS:
  776. /*
  777. * The allocation has already been applied to the
  778. * in-core superblock's counter. This should only
  779. * be applied to the on-disk superblock.
  780. */
  781. ASSERT(delta < 0);
  782. tp->t_res_fdblocks_delta += delta;
  783. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  784. flags &= ~XFS_TRANS_SB_DIRTY;
  785. break;
  786. case XFS_TRANS_SB_FREXTENTS:
  787. /*
  788. * Track the number of blocks allocated in the
  789. * transaction. Make sure it does not exceed the
  790. * number reserved.
  791. */
  792. if (delta < 0) {
  793. tp->t_rtx_res_used += (uint)-delta;
  794. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  795. }
  796. tp->t_frextents_delta += delta;
  797. break;
  798. case XFS_TRANS_SB_RES_FREXTENTS:
  799. /*
  800. * The allocation has already been applied to the
  801. * in-core superblock's counter. This should only
  802. * be applied to the on-disk superblock.
  803. */
  804. ASSERT(delta < 0);
  805. tp->t_res_frextents_delta += delta;
  806. break;
  807. case XFS_TRANS_SB_DBLOCKS:
  808. ASSERT(delta > 0);
  809. tp->t_dblocks_delta += delta;
  810. break;
  811. case XFS_TRANS_SB_AGCOUNT:
  812. ASSERT(delta > 0);
  813. tp->t_agcount_delta += delta;
  814. break;
  815. case XFS_TRANS_SB_IMAXPCT:
  816. tp->t_imaxpct_delta += delta;
  817. break;
  818. case XFS_TRANS_SB_REXTSIZE:
  819. tp->t_rextsize_delta += delta;
  820. break;
  821. case XFS_TRANS_SB_RBMBLOCKS:
  822. tp->t_rbmblocks_delta += delta;
  823. break;
  824. case XFS_TRANS_SB_RBLOCKS:
  825. tp->t_rblocks_delta += delta;
  826. break;
  827. case XFS_TRANS_SB_REXTENTS:
  828. tp->t_rextents_delta += delta;
  829. break;
  830. case XFS_TRANS_SB_REXTSLOG:
  831. tp->t_rextslog_delta += delta;
  832. break;
  833. default:
  834. ASSERT(0);
  835. return;
  836. }
  837. tp->t_flags |= flags;
  838. }
  839. /*
  840. * xfs_trans_apply_sb_deltas() is called from the commit code
  841. * to bring the superblock buffer into the current transaction
  842. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  843. *
  844. * For now we just look at each field allowed to change and change
  845. * it if necessary.
  846. */
  847. STATIC void
  848. xfs_trans_apply_sb_deltas(
  849. xfs_trans_t *tp)
  850. {
  851. xfs_dsb_t *sbp;
  852. xfs_buf_t *bp;
  853. int whole = 0;
  854. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  855. sbp = XFS_BUF_TO_SBP(bp);
  856. /*
  857. * Check that superblock mods match the mods made to AGF counters.
  858. */
  859. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  860. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  861. tp->t_ag_btree_delta));
  862. /*
  863. * Only update the superblock counters if we are logging them
  864. */
  865. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  866. if (tp->t_icount_delta)
  867. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  868. if (tp->t_ifree_delta)
  869. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  870. if (tp->t_fdblocks_delta)
  871. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  872. if (tp->t_res_fdblocks_delta)
  873. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  874. }
  875. if (tp->t_frextents_delta)
  876. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  877. if (tp->t_res_frextents_delta)
  878. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  879. if (tp->t_dblocks_delta) {
  880. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  881. whole = 1;
  882. }
  883. if (tp->t_agcount_delta) {
  884. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  885. whole = 1;
  886. }
  887. if (tp->t_imaxpct_delta) {
  888. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  889. whole = 1;
  890. }
  891. if (tp->t_rextsize_delta) {
  892. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  893. whole = 1;
  894. }
  895. if (tp->t_rbmblocks_delta) {
  896. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  897. whole = 1;
  898. }
  899. if (tp->t_rblocks_delta) {
  900. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  901. whole = 1;
  902. }
  903. if (tp->t_rextents_delta) {
  904. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  905. whole = 1;
  906. }
  907. if (tp->t_rextslog_delta) {
  908. sbp->sb_rextslog += tp->t_rextslog_delta;
  909. whole = 1;
  910. }
  911. if (whole)
  912. /*
  913. * Log the whole thing, the fields are noncontiguous.
  914. */
  915. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  916. else
  917. /*
  918. * Since all the modifiable fields are contiguous, we
  919. * can get away with this.
  920. */
  921. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  922. offsetof(xfs_dsb_t, sb_frextents) +
  923. sizeof(sbp->sb_frextents) - 1);
  924. }
  925. /*
  926. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  927. * and apply superblock counter changes to the in-core superblock. The
  928. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  929. * applied to the in-core superblock. The idea is that that has already been
  930. * done.
  931. *
  932. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  933. * However, we have to ensure that we only modify each superblock field only
  934. * once because the application of the delta values may not be atomic. That can
  935. * lead to ENOSPC races occurring if we have two separate modifcations of the
  936. * free space counter to put back the entire reservation and then take away
  937. * what we used.
  938. *
  939. * If we are not logging superblock counters, then the inode allocated/free and
  940. * used block counts are not updated in the on disk superblock. In this case,
  941. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  942. * still need to update the incore superblock with the changes.
  943. */
  944. void
  945. xfs_trans_unreserve_and_mod_sb(
  946. xfs_trans_t *tp)
  947. {
  948. xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
  949. xfs_mod_sb_t *msbp;
  950. xfs_mount_t *mp = tp->t_mountp;
  951. /* REFERENCED */
  952. int error;
  953. int rsvd;
  954. int64_t blkdelta = 0;
  955. int64_t rtxdelta = 0;
  956. msbp = msb;
  957. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  958. /* calculate free blocks delta */
  959. if (tp->t_blk_res > 0)
  960. blkdelta = tp->t_blk_res;
  961. if ((tp->t_fdblocks_delta != 0) &&
  962. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  963. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  964. blkdelta += tp->t_fdblocks_delta;
  965. if (blkdelta != 0) {
  966. msbp->msb_field = XFS_SBS_FDBLOCKS;
  967. msbp->msb_delta = blkdelta;
  968. msbp++;
  969. }
  970. /* calculate free realtime extents delta */
  971. if (tp->t_rtx_res > 0)
  972. rtxdelta = tp->t_rtx_res;
  973. if ((tp->t_frextents_delta != 0) &&
  974. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  975. rtxdelta += tp->t_frextents_delta;
  976. if (rtxdelta != 0) {
  977. msbp->msb_field = XFS_SBS_FREXTENTS;
  978. msbp->msb_delta = rtxdelta;
  979. msbp++;
  980. }
  981. /* apply remaining deltas */
  982. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  983. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  984. if (tp->t_icount_delta != 0) {
  985. msbp->msb_field = XFS_SBS_ICOUNT;
  986. msbp->msb_delta = tp->t_icount_delta;
  987. msbp++;
  988. }
  989. if (tp->t_ifree_delta != 0) {
  990. msbp->msb_field = XFS_SBS_IFREE;
  991. msbp->msb_delta = tp->t_ifree_delta;
  992. msbp++;
  993. }
  994. }
  995. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  996. if (tp->t_dblocks_delta != 0) {
  997. msbp->msb_field = XFS_SBS_DBLOCKS;
  998. msbp->msb_delta = tp->t_dblocks_delta;
  999. msbp++;
  1000. }
  1001. if (tp->t_agcount_delta != 0) {
  1002. msbp->msb_field = XFS_SBS_AGCOUNT;
  1003. msbp->msb_delta = tp->t_agcount_delta;
  1004. msbp++;
  1005. }
  1006. if (tp->t_imaxpct_delta != 0) {
  1007. msbp->msb_field = XFS_SBS_IMAX_PCT;
  1008. msbp->msb_delta = tp->t_imaxpct_delta;
  1009. msbp++;
  1010. }
  1011. if (tp->t_rextsize_delta != 0) {
  1012. msbp->msb_field = XFS_SBS_REXTSIZE;
  1013. msbp->msb_delta = tp->t_rextsize_delta;
  1014. msbp++;
  1015. }
  1016. if (tp->t_rbmblocks_delta != 0) {
  1017. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  1018. msbp->msb_delta = tp->t_rbmblocks_delta;
  1019. msbp++;
  1020. }
  1021. if (tp->t_rblocks_delta != 0) {
  1022. msbp->msb_field = XFS_SBS_RBLOCKS;
  1023. msbp->msb_delta = tp->t_rblocks_delta;
  1024. msbp++;
  1025. }
  1026. if (tp->t_rextents_delta != 0) {
  1027. msbp->msb_field = XFS_SBS_REXTENTS;
  1028. msbp->msb_delta = tp->t_rextents_delta;
  1029. msbp++;
  1030. }
  1031. if (tp->t_rextslog_delta != 0) {
  1032. msbp->msb_field = XFS_SBS_REXTSLOG;
  1033. msbp->msb_delta = tp->t_rextslog_delta;
  1034. msbp++;
  1035. }
  1036. }
  1037. /*
  1038. * If we need to change anything, do it.
  1039. */
  1040. if (msbp > msb) {
  1041. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  1042. (uint)(msbp - msb), rsvd);
  1043. ASSERT(error == 0);
  1044. }
  1045. }
  1046. /*
  1047. * Total up the number of log iovecs needed to commit this
  1048. * transaction. The transaction itself needs one for the
  1049. * transaction header. Ask each dirty item in turn how many
  1050. * it needs to get the total.
  1051. */
  1052. static uint
  1053. xfs_trans_count_vecs(
  1054. struct xfs_trans *tp)
  1055. {
  1056. int nvecs;
  1057. xfs_log_item_desc_t *lidp;
  1058. nvecs = 1;
  1059. lidp = xfs_trans_first_item(tp);
  1060. ASSERT(lidp != NULL);
  1061. /* In the non-debug case we need to start bailing out if we
  1062. * didn't find a log_item here, return zero and let trans_commit
  1063. * deal with it.
  1064. */
  1065. if (lidp == NULL)
  1066. return 0;
  1067. while (lidp != NULL) {
  1068. /*
  1069. * Skip items which aren't dirty in this transaction.
  1070. */
  1071. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  1072. lidp = xfs_trans_next_item(tp, lidp);
  1073. continue;
  1074. }
  1075. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  1076. nvecs += lidp->lid_size;
  1077. lidp = xfs_trans_next_item(tp, lidp);
  1078. }
  1079. return nvecs;
  1080. }
  1081. /*
  1082. * Fill in the vector with pointers to data to be logged
  1083. * by this transaction. The transaction header takes
  1084. * the first vector, and then each dirty item takes the
  1085. * number of vectors it indicated it needed in xfs_trans_count_vecs().
  1086. *
  1087. * As each item fills in the entries it needs, also pin the item
  1088. * so that it cannot be flushed out until the log write completes.
  1089. */
  1090. static void
  1091. xfs_trans_fill_vecs(
  1092. struct xfs_trans *tp,
  1093. struct xfs_log_iovec *log_vector)
  1094. {
  1095. xfs_log_item_desc_t *lidp;
  1096. struct xfs_log_iovec *vecp;
  1097. uint nitems;
  1098. /*
  1099. * Skip over the entry for the transaction header, we'll
  1100. * fill that in at the end.
  1101. */
  1102. vecp = log_vector + 1;
  1103. nitems = 0;
  1104. lidp = xfs_trans_first_item(tp);
  1105. ASSERT(lidp);
  1106. while (lidp) {
  1107. /* Skip items which aren't dirty in this transaction. */
  1108. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  1109. lidp = xfs_trans_next_item(tp, lidp);
  1110. continue;
  1111. }
  1112. /*
  1113. * The item may be marked dirty but not log anything. This can
  1114. * be used to get called when a transaction is committed.
  1115. */
  1116. if (lidp->lid_size)
  1117. nitems++;
  1118. IOP_FORMAT(lidp->lid_item, vecp);
  1119. vecp += lidp->lid_size;
  1120. IOP_PIN(lidp->lid_item);
  1121. lidp = xfs_trans_next_item(tp, lidp);
  1122. }
  1123. /*
  1124. * Now that we've counted the number of items in this transaction, fill
  1125. * in the transaction header. Note that the transaction header does not
  1126. * have a log item.
  1127. */
  1128. tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
  1129. tp->t_header.th_type = tp->t_type;
  1130. tp->t_header.th_num_items = nitems;
  1131. log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
  1132. log_vector->i_len = sizeof(xfs_trans_header_t);
  1133. log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
  1134. }
  1135. /*
  1136. * The committed item processing consists of calling the committed routine of
  1137. * each logged item, updating the item's position in the AIL if necessary, and
  1138. * unpinning each item. If the committed routine returns -1, then do nothing
  1139. * further with the item because it may have been freed.
  1140. *
  1141. * Since items are unlocked when they are copied to the incore log, it is
  1142. * possible for two transactions to be completing and manipulating the same
  1143. * item simultaneously. The AIL lock will protect the lsn field of each item.
  1144. * The value of this field can never go backwards.
  1145. *
  1146. * We unpin the items after repositioning them in the AIL, because otherwise
  1147. * they could be immediately flushed and we'd have to race with the flusher
  1148. * trying to pull the item from the AIL as we add it.
  1149. */
  1150. void
  1151. xfs_trans_item_committed(
  1152. struct xfs_log_item *lip,
  1153. xfs_lsn_t commit_lsn,
  1154. int aborted)
  1155. {
  1156. xfs_lsn_t item_lsn;
  1157. struct xfs_ail *ailp;
  1158. if (aborted)
  1159. lip->li_flags |= XFS_LI_ABORTED;
  1160. item_lsn = IOP_COMMITTED(lip, commit_lsn);
  1161. /* If the committed routine returns -1, item has been freed. */
  1162. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
  1163. return;
  1164. /*
  1165. * If the returned lsn is greater than what it contained before, update
  1166. * the location of the item in the AIL. If it is not, then do nothing.
  1167. * Items can never move backwards in the AIL.
  1168. *
  1169. * While the new lsn should usually be greater, it is possible that a
  1170. * later transaction completing simultaneously with an earlier one
  1171. * using the same item could complete first with a higher lsn. This
  1172. * would cause the earlier transaction to fail the test below.
  1173. */
  1174. ailp = lip->li_ailp;
  1175. spin_lock(&ailp->xa_lock);
  1176. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
  1177. /*
  1178. * This will set the item's lsn to item_lsn and update the
  1179. * position of the item in the AIL.
  1180. *
  1181. * xfs_trans_ail_update() drops the AIL lock.
  1182. */
  1183. xfs_trans_ail_update(ailp, lip, item_lsn);
  1184. } else {
  1185. spin_unlock(&ailp->xa_lock);
  1186. }
  1187. /*
  1188. * Now that we've repositioned the item in the AIL, unpin it so it can
  1189. * be flushed. Pass information about buffer stale state down from the
  1190. * log item flags, if anyone else stales the buffer we do not want to
  1191. * pay any attention to it.
  1192. */
  1193. IOP_UNPIN(lip);
  1194. }
  1195. /*
  1196. * This is typically called by the LM when a transaction has been fully
  1197. * committed to disk. It needs to unpin the items which have
  1198. * been logged by the transaction and update their positions
  1199. * in the AIL if necessary.
  1200. *
  1201. * This also gets called when the transactions didn't get written out
  1202. * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
  1203. */
  1204. STATIC void
  1205. xfs_trans_committed(
  1206. struct xfs_trans *tp,
  1207. int abortflag)
  1208. {
  1209. xfs_log_item_desc_t *lidp;
  1210. xfs_log_item_chunk_t *licp;
  1211. xfs_log_item_chunk_t *next_licp;
  1212. /* Call the transaction's completion callback if there is one. */
  1213. if (tp->t_callback != NULL)
  1214. tp->t_callback(tp, tp->t_callarg);
  1215. for (lidp = xfs_trans_first_item(tp);
  1216. lidp != NULL;
  1217. lidp = xfs_trans_next_item(tp, lidp)) {
  1218. xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
  1219. }
  1220. /* free the item chunks, ignoring the embedded chunk */
  1221. for (licp = tp->t_items.lic_next; licp != NULL; licp = next_licp) {
  1222. next_licp = licp->lic_next;
  1223. kmem_free(licp);
  1224. }
  1225. xfs_trans_free(tp);
  1226. }
  1227. /*
  1228. * Called from the trans_commit code when we notice that
  1229. * the filesystem is in the middle of a forced shutdown.
  1230. */
  1231. STATIC void
  1232. xfs_trans_uncommit(
  1233. struct xfs_trans *tp,
  1234. uint flags)
  1235. {
  1236. xfs_log_item_desc_t *lidp;
  1237. for (lidp = xfs_trans_first_item(tp);
  1238. lidp != NULL;
  1239. lidp = xfs_trans_next_item(tp, lidp)) {
  1240. /*
  1241. * Unpin all but those that aren't dirty.
  1242. */
  1243. if (lidp->lid_flags & XFS_LID_DIRTY)
  1244. IOP_UNPIN_REMOVE(lidp->lid_item, tp);
  1245. }
  1246. xfs_trans_unreserve_and_mod_sb(tp);
  1247. xfs_trans_unreserve_and_mod_dquots(tp);
  1248. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1249. xfs_trans_free(tp);
  1250. }
  1251. /*
  1252. * Format the transaction direct to the iclog. This isolates the physical
  1253. * transaction commit operation from the logical operation and hence allows
  1254. * other methods to be introduced without affecting the existing commit path.
  1255. */
  1256. static int
  1257. xfs_trans_commit_iclog(
  1258. struct xfs_mount *mp,
  1259. struct xfs_trans *tp,
  1260. xfs_lsn_t *commit_lsn,
  1261. int flags)
  1262. {
  1263. int shutdown;
  1264. int error;
  1265. int log_flags = 0;
  1266. struct xlog_in_core *commit_iclog;
  1267. #define XFS_TRANS_LOGVEC_COUNT 16
  1268. struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
  1269. struct xfs_log_iovec *log_vector;
  1270. uint nvec;
  1271. /*
  1272. * Ask each log item how many log_vector entries it will
  1273. * need so we can figure out how many to allocate.
  1274. * Try to avoid the kmem_alloc() call in the common case
  1275. * by using a vector from the stack when it fits.
  1276. */
  1277. nvec = xfs_trans_count_vecs(tp);
  1278. if (nvec == 0) {
  1279. return ENOMEM; /* triggers a shutdown! */
  1280. } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
  1281. log_vector = log_vector_fast;
  1282. } else {
  1283. log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
  1284. sizeof(xfs_log_iovec_t),
  1285. KM_SLEEP);
  1286. }
  1287. /*
  1288. * Fill in the log_vector and pin the logged items, and
  1289. * then write the transaction to the log.
  1290. */
  1291. xfs_trans_fill_vecs(tp, log_vector);
  1292. if (flags & XFS_TRANS_RELEASE_LOG_RES)
  1293. log_flags = XFS_LOG_REL_PERM_RESERV;
  1294. error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
  1295. /*
  1296. * The transaction is committed incore here, and can go out to disk
  1297. * at any time after this call. However, all the items associated
  1298. * with the transaction are still locked and pinned in memory.
  1299. */
  1300. *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
  1301. tp->t_commit_lsn = *commit_lsn;
  1302. trace_xfs_trans_commit_lsn(tp);
  1303. if (nvec > XFS_TRANS_LOGVEC_COUNT)
  1304. kmem_free(log_vector);
  1305. /*
  1306. * If we got a log write error. Unpin the logitems that we
  1307. * had pinned, clean up, free trans structure, and return error.
  1308. */
  1309. if (error || *commit_lsn == -1) {
  1310. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1311. xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
  1312. return XFS_ERROR(EIO);
  1313. }
  1314. /*
  1315. * Once the transaction has committed, unused
  1316. * reservations need to be released and changes to
  1317. * the superblock need to be reflected in the in-core
  1318. * version. Do that now.
  1319. */
  1320. xfs_trans_unreserve_and_mod_sb(tp);
  1321. /*
  1322. * Tell the LM to call the transaction completion routine
  1323. * when the log write with LSN commit_lsn completes (e.g.
  1324. * when the transaction commit really hits the on-disk log).
  1325. * After this call we cannot reference tp, because the call
  1326. * can happen at any time and the call will free the transaction
  1327. * structure pointed to by tp. The only case where we call
  1328. * the completion routine (xfs_trans_committed) directly is
  1329. * if the log is turned off on a debug kernel or we're
  1330. * running in simulation mode (the log is explicitly turned
  1331. * off).
  1332. */
  1333. tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
  1334. tp->t_logcb.cb_arg = tp;
  1335. /*
  1336. * We need to pass the iclog buffer which was used for the
  1337. * transaction commit record into this function, and attach
  1338. * the callback to it. The callback must be attached before
  1339. * the items are unlocked to avoid racing with other threads
  1340. * waiting for an item to unlock.
  1341. */
  1342. shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
  1343. /*
  1344. * Mark this thread as no longer being in a transaction
  1345. */
  1346. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1347. /*
  1348. * Once all the items of the transaction have been copied
  1349. * to the in core log and the callback is attached, the
  1350. * items can be unlocked.
  1351. *
  1352. * This will free descriptors pointing to items which were
  1353. * not logged since there is nothing more to do with them.
  1354. * For items which were logged, we will keep pointers to them
  1355. * so they can be unpinned after the transaction commits to disk.
  1356. * This will also stamp each modified meta-data item with
  1357. * the commit lsn of this transaction for dependency tracking
  1358. * purposes.
  1359. */
  1360. xfs_trans_unlock_items(tp, *commit_lsn);
  1361. /*
  1362. * If we detected a log error earlier, finish committing
  1363. * the transaction now (unpin log items, etc).
  1364. *
  1365. * Order is critical here, to avoid using the transaction
  1366. * pointer after its been freed (by xfs_trans_committed
  1367. * either here now, or as a callback). We cannot do this
  1368. * step inside xfs_log_notify as was done earlier because
  1369. * of this issue.
  1370. */
  1371. if (shutdown)
  1372. xfs_trans_committed(tp, XFS_LI_ABORTED);
  1373. /*
  1374. * Now that the xfs_trans_committed callback has been attached,
  1375. * and the items are released we can finally allow the iclog to
  1376. * go to disk.
  1377. */
  1378. return xfs_log_release_iclog(mp, commit_iclog);
  1379. }
  1380. /*
  1381. * Walk the log items and allocate log vector structures for
  1382. * each item large enough to fit all the vectors they require.
  1383. * Note that this format differs from the old log vector format in
  1384. * that there is no transaction header in these log vectors.
  1385. */
  1386. STATIC struct xfs_log_vec *
  1387. xfs_trans_alloc_log_vecs(
  1388. xfs_trans_t *tp)
  1389. {
  1390. xfs_log_item_desc_t *lidp;
  1391. struct xfs_log_vec *lv = NULL;
  1392. struct xfs_log_vec *ret_lv = NULL;
  1393. lidp = xfs_trans_first_item(tp);
  1394. /* Bail out if we didn't find a log item. */
  1395. if (!lidp) {
  1396. ASSERT(0);
  1397. return NULL;
  1398. }
  1399. while (lidp != NULL) {
  1400. struct xfs_log_vec *new_lv;
  1401. /* Skip items which aren't dirty in this transaction. */
  1402. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  1403. lidp = xfs_trans_next_item(tp, lidp);
  1404. continue;
  1405. }
  1406. /* Skip items that do not have any vectors for writing */
  1407. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  1408. if (!lidp->lid_size) {
  1409. lidp = xfs_trans_next_item(tp, lidp);
  1410. continue;
  1411. }
  1412. new_lv = kmem_zalloc(sizeof(*new_lv) +
  1413. lidp->lid_size * sizeof(struct xfs_log_iovec),
  1414. KM_SLEEP);
  1415. /* The allocated iovec region lies beyond the log vector. */
  1416. new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
  1417. new_lv->lv_niovecs = lidp->lid_size;
  1418. new_lv->lv_item = lidp->lid_item;
  1419. if (!ret_lv)
  1420. ret_lv = new_lv;
  1421. else
  1422. lv->lv_next = new_lv;
  1423. lv = new_lv;
  1424. lidp = xfs_trans_next_item(tp, lidp);
  1425. }
  1426. return ret_lv;
  1427. }
  1428. static int
  1429. xfs_trans_commit_cil(
  1430. struct xfs_mount *mp,
  1431. struct xfs_trans *tp,
  1432. xfs_lsn_t *commit_lsn,
  1433. int flags)
  1434. {
  1435. struct xfs_log_vec *log_vector;
  1436. int error;
  1437. /*
  1438. * Get each log item to allocate a vector structure for
  1439. * the log item to to pass to the log write code. The
  1440. * CIL commit code will format the vector and save it away.
  1441. */
  1442. log_vector = xfs_trans_alloc_log_vecs(tp);
  1443. if (!log_vector)
  1444. return ENOMEM;
  1445. error = xfs_log_commit_cil(mp, tp, log_vector, commit_lsn, flags);
  1446. if (error)
  1447. return error;
  1448. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1449. /* xfs_trans_free_items() unlocks them first */
  1450. xfs_trans_free_items(tp, *commit_lsn, 0);
  1451. xfs_trans_free(tp);
  1452. return 0;
  1453. }
  1454. /*
  1455. * xfs_trans_commit
  1456. *
  1457. * Commit the given transaction to the log a/synchronously.
  1458. *
  1459. * XFS disk error handling mechanism is not based on a typical
  1460. * transaction abort mechanism. Logically after the filesystem
  1461. * gets marked 'SHUTDOWN', we can't let any new transactions
  1462. * be durable - ie. committed to disk - because some metadata might
  1463. * be inconsistent. In such cases, this returns an error, and the
  1464. * caller may assume that all locked objects joined to the transaction
  1465. * have already been unlocked as if the commit had succeeded.
  1466. * Do not reference the transaction structure after this call.
  1467. */
  1468. int
  1469. _xfs_trans_commit(
  1470. struct xfs_trans *tp,
  1471. uint flags,
  1472. int *log_flushed)
  1473. {
  1474. struct xfs_mount *mp = tp->t_mountp;
  1475. xfs_lsn_t commit_lsn = -1;
  1476. int error = 0;
  1477. int log_flags = 0;
  1478. int sync = tp->t_flags & XFS_TRANS_SYNC;
  1479. /*
  1480. * Determine whether this commit is releasing a permanent
  1481. * log reservation or not.
  1482. */
  1483. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1484. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1485. log_flags = XFS_LOG_REL_PERM_RESERV;
  1486. }
  1487. /*
  1488. * If there is nothing to be logged by the transaction,
  1489. * then unlock all of the items associated with the
  1490. * transaction and free the transaction structure.
  1491. * Also make sure to return any reserved blocks to
  1492. * the free pool.
  1493. */
  1494. if (!(tp->t_flags & XFS_TRANS_DIRTY))
  1495. goto out_unreserve;
  1496. if (XFS_FORCED_SHUTDOWN(mp)) {
  1497. error = XFS_ERROR(EIO);
  1498. goto out_unreserve;
  1499. }
  1500. ASSERT(tp->t_ticket != NULL);
  1501. /*
  1502. * If we need to update the superblock, then do it now.
  1503. */
  1504. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  1505. xfs_trans_apply_sb_deltas(tp);
  1506. xfs_trans_apply_dquot_deltas(tp);
  1507. if (mp->m_flags & XFS_MOUNT_DELAYLOG)
  1508. error = xfs_trans_commit_cil(mp, tp, &commit_lsn, flags);
  1509. else
  1510. error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
  1511. if (error == ENOMEM) {
  1512. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  1513. error = XFS_ERROR(EIO);
  1514. goto out_unreserve;
  1515. }
  1516. /*
  1517. * If the transaction needs to be synchronous, then force the
  1518. * log out now and wait for it.
  1519. */
  1520. if (sync) {
  1521. if (!error) {
  1522. error = _xfs_log_force_lsn(mp, commit_lsn,
  1523. XFS_LOG_SYNC, log_flushed);
  1524. }
  1525. XFS_STATS_INC(xs_trans_sync);
  1526. } else {
  1527. XFS_STATS_INC(xs_trans_async);
  1528. }
  1529. return error;
  1530. out_unreserve:
  1531. xfs_trans_unreserve_and_mod_sb(tp);
  1532. /*
  1533. * It is indeed possible for the transaction to be not dirty but
  1534. * the dqinfo portion to be. All that means is that we have some
  1535. * (non-persistent) quota reservations that need to be unreserved.
  1536. */
  1537. xfs_trans_unreserve_and_mod_dquots(tp);
  1538. if (tp->t_ticket) {
  1539. commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1540. if (commit_lsn == -1 && !error)
  1541. error = XFS_ERROR(EIO);
  1542. }
  1543. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1544. xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
  1545. xfs_trans_free(tp);
  1546. XFS_STATS_INC(xs_trans_empty);
  1547. return error;
  1548. }
  1549. /*
  1550. * Unlock all of the transaction's items and free the transaction.
  1551. * The transaction must not have modified any of its items, because
  1552. * there is no way to restore them to their previous state.
  1553. *
  1554. * If the transaction has made a log reservation, make sure to release
  1555. * it as well.
  1556. */
  1557. void
  1558. xfs_trans_cancel(
  1559. xfs_trans_t *tp,
  1560. int flags)
  1561. {
  1562. int log_flags;
  1563. #ifdef DEBUG
  1564. xfs_log_item_chunk_t *licp;
  1565. xfs_log_item_desc_t *lidp;
  1566. xfs_log_item_t *lip;
  1567. int i;
  1568. #endif
  1569. xfs_mount_t *mp = tp->t_mountp;
  1570. /*
  1571. * See if the caller is being too lazy to figure out if
  1572. * the transaction really needs an abort.
  1573. */
  1574. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  1575. flags &= ~XFS_TRANS_ABORT;
  1576. /*
  1577. * See if the caller is relying on us to shut down the
  1578. * filesystem. This happens in paths where we detect
  1579. * corruption and decide to give up.
  1580. */
  1581. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  1582. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  1583. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1584. }
  1585. #ifdef DEBUG
  1586. if (!(flags & XFS_TRANS_ABORT)) {
  1587. licp = &(tp->t_items);
  1588. while (licp != NULL) {
  1589. lidp = licp->lic_descs;
  1590. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  1591. if (xfs_lic_isfree(licp, i)) {
  1592. continue;
  1593. }
  1594. lip = lidp->lid_item;
  1595. if (!XFS_FORCED_SHUTDOWN(mp))
  1596. ASSERT(!(lip->li_type == XFS_LI_EFD));
  1597. }
  1598. licp = licp->lic_next;
  1599. }
  1600. }
  1601. #endif
  1602. xfs_trans_unreserve_and_mod_sb(tp);
  1603. xfs_trans_unreserve_and_mod_dquots(tp);
  1604. if (tp->t_ticket) {
  1605. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1606. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1607. log_flags = XFS_LOG_REL_PERM_RESERV;
  1608. } else {
  1609. log_flags = 0;
  1610. }
  1611. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1612. }
  1613. /* mark this thread as no longer being in a transaction */
  1614. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1615. xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
  1616. xfs_trans_free(tp);
  1617. }
  1618. /*
  1619. * Roll from one trans in the sequence of PERMANENT transactions to
  1620. * the next: permanent transactions are only flushed out when
  1621. * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
  1622. * as possible to let chunks of it go to the log. So we commit the
  1623. * chunk we've been working on and get a new transaction to continue.
  1624. */
  1625. int
  1626. xfs_trans_roll(
  1627. struct xfs_trans **tpp,
  1628. struct xfs_inode *dp)
  1629. {
  1630. struct xfs_trans *trans;
  1631. unsigned int logres, count;
  1632. int error;
  1633. /*
  1634. * Ensure that the inode is always logged.
  1635. */
  1636. trans = *tpp;
  1637. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  1638. /*
  1639. * Copy the critical parameters from one trans to the next.
  1640. */
  1641. logres = trans->t_log_res;
  1642. count = trans->t_log_count;
  1643. *tpp = xfs_trans_dup(trans);
  1644. /*
  1645. * Commit the current transaction.
  1646. * If this commit failed, then it'd just unlock those items that
  1647. * are not marked ihold. That also means that a filesystem shutdown
  1648. * is in progress. The caller takes the responsibility to cancel
  1649. * the duplicate transaction that gets returned.
  1650. */
  1651. error = xfs_trans_commit(trans, 0);
  1652. if (error)
  1653. return (error);
  1654. trans = *tpp;
  1655. /*
  1656. * transaction commit worked ok so we can drop the extra ticket
  1657. * reference that we gained in xfs_trans_dup()
  1658. */
  1659. xfs_log_ticket_put(trans->t_ticket);
  1660. /*
  1661. * Reserve space in the log for th next transaction.
  1662. * This also pushes items in the "AIL", the list of logged items,
  1663. * out to disk if they are taking up space at the tail of the log
  1664. * that we want to use. This requires that either nothing be locked
  1665. * across this call, or that anything that is locked be logged in
  1666. * the prior and the next transactions.
  1667. */
  1668. error = xfs_trans_reserve(trans, 0, logres, 0,
  1669. XFS_TRANS_PERM_LOG_RES, count);
  1670. /*
  1671. * Ensure that the inode is in the new transaction and locked.
  1672. */
  1673. if (error)
  1674. return error;
  1675. xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
  1676. xfs_trans_ihold(trans, dp);
  1677. return 0;
  1678. }