xfs_trans.c 46 KB

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