xfs_trans.c 46 KB

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