xfs_trans.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_error.h"
  31. #include "xfs_da_btree.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir2_sf.h"
  36. #include "xfs_attr_sf.h"
  37. #include "xfs_dinode.h"
  38. #include "xfs_inode.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_ialloc.h"
  41. #include "xfs_alloc.h"
  42. #include "xfs_bmap.h"
  43. #include "xfs_quota.h"
  44. #include "xfs_trans_priv.h"
  45. #include "xfs_trans_space.h"
  46. #include "xfs_inode_item.h"
  47. STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *);
  48. STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
  49. STATIC void xfs_trans_committed(xfs_trans_t *, int);
  50. STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
  51. STATIC void xfs_trans_free(xfs_trans_t *);
  52. kmem_zone_t *xfs_trans_zone;
  53. /*
  54. * Reservation functions here avoid a huge stack in xfs_trans_init
  55. * due to register overflow from temporaries in the calculations.
  56. */
  57. STATIC uint
  58. xfs_calc_write_reservation(xfs_mount_t *mp)
  59. {
  60. return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  61. }
  62. STATIC uint
  63. xfs_calc_itruncate_reservation(xfs_mount_t *mp)
  64. {
  65. return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  66. }
  67. STATIC uint
  68. xfs_calc_rename_reservation(xfs_mount_t *mp)
  69. {
  70. return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  71. }
  72. STATIC uint
  73. xfs_calc_link_reservation(xfs_mount_t *mp)
  74. {
  75. return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  76. }
  77. STATIC uint
  78. xfs_calc_remove_reservation(xfs_mount_t *mp)
  79. {
  80. return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  81. }
  82. STATIC uint
  83. xfs_calc_symlink_reservation(xfs_mount_t *mp)
  84. {
  85. return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  86. }
  87. STATIC uint
  88. xfs_calc_create_reservation(xfs_mount_t *mp)
  89. {
  90. return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  91. }
  92. STATIC uint
  93. xfs_calc_mkdir_reservation(xfs_mount_t *mp)
  94. {
  95. return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  96. }
  97. STATIC uint
  98. xfs_calc_ifree_reservation(xfs_mount_t *mp)
  99. {
  100. return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  101. }
  102. STATIC uint
  103. xfs_calc_ichange_reservation(xfs_mount_t *mp)
  104. {
  105. return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  106. }
  107. STATIC uint
  108. xfs_calc_growdata_reservation(xfs_mount_t *mp)
  109. {
  110. return XFS_CALC_GROWDATA_LOG_RES(mp);
  111. }
  112. STATIC uint
  113. xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
  114. {
  115. return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
  116. }
  117. STATIC uint
  118. xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
  119. {
  120. return XFS_CALC_GROWRTZERO_LOG_RES(mp);
  121. }
  122. STATIC uint
  123. xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
  124. {
  125. return XFS_CALC_GROWRTFREE_LOG_RES(mp);
  126. }
  127. STATIC uint
  128. xfs_calc_swrite_reservation(xfs_mount_t *mp)
  129. {
  130. return XFS_CALC_SWRITE_LOG_RES(mp);
  131. }
  132. STATIC uint
  133. xfs_calc_writeid_reservation(xfs_mount_t *mp)
  134. {
  135. return XFS_CALC_WRITEID_LOG_RES(mp);
  136. }
  137. STATIC uint
  138. xfs_calc_addafork_reservation(xfs_mount_t *mp)
  139. {
  140. return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  141. }
  142. STATIC uint
  143. xfs_calc_attrinval_reservation(xfs_mount_t *mp)
  144. {
  145. return XFS_CALC_ATTRINVAL_LOG_RES(mp);
  146. }
  147. STATIC uint
  148. xfs_calc_attrset_reservation(xfs_mount_t *mp)
  149. {
  150. return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  151. }
  152. STATIC uint
  153. xfs_calc_attrrm_reservation(xfs_mount_t *mp)
  154. {
  155. return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
  156. }
  157. STATIC uint
  158. xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
  159. {
  160. return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
  161. }
  162. /*
  163. * Initialize the precomputed transaction reservation values
  164. * in the mount structure.
  165. */
  166. void
  167. xfs_trans_init(
  168. xfs_mount_t *mp)
  169. {
  170. xfs_trans_reservations_t *resp;
  171. resp = &(mp->m_reservations);
  172. resp->tr_write = xfs_calc_write_reservation(mp);
  173. resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
  174. resp->tr_rename = xfs_calc_rename_reservation(mp);
  175. resp->tr_link = xfs_calc_link_reservation(mp);
  176. resp->tr_remove = xfs_calc_remove_reservation(mp);
  177. resp->tr_symlink = xfs_calc_symlink_reservation(mp);
  178. resp->tr_create = xfs_calc_create_reservation(mp);
  179. resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
  180. resp->tr_ifree = xfs_calc_ifree_reservation(mp);
  181. resp->tr_ichange = xfs_calc_ichange_reservation(mp);
  182. resp->tr_growdata = xfs_calc_growdata_reservation(mp);
  183. resp->tr_swrite = xfs_calc_swrite_reservation(mp);
  184. resp->tr_writeid = xfs_calc_writeid_reservation(mp);
  185. resp->tr_addafork = xfs_calc_addafork_reservation(mp);
  186. resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
  187. resp->tr_attrset = xfs_calc_attrset_reservation(mp);
  188. resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
  189. resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
  190. resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
  191. resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
  192. resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
  193. }
  194. /*
  195. * This routine is called to allocate a transaction structure.
  196. * The type parameter indicates the type of the transaction. These
  197. * are enumerated in xfs_trans.h.
  198. *
  199. * Dynamically allocate the transaction structure from the transaction
  200. * zone, initialize it, and return it to the caller.
  201. */
  202. xfs_trans_t *
  203. xfs_trans_alloc(
  204. xfs_mount_t *mp,
  205. uint type)
  206. {
  207. xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
  208. return _xfs_trans_alloc(mp, type, KM_SLEEP);
  209. }
  210. xfs_trans_t *
  211. _xfs_trans_alloc(
  212. xfs_mount_t *mp,
  213. uint type,
  214. uint memflags)
  215. {
  216. xfs_trans_t *tp;
  217. atomic_inc(&mp->m_active_trans);
  218. tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
  219. tp->t_magic = XFS_TRANS_MAGIC;
  220. tp->t_type = type;
  221. tp->t_mountp = mp;
  222. tp->t_items_free = XFS_LIC_NUM_SLOTS;
  223. tp->t_busy_free = XFS_LBC_NUM_SLOTS;
  224. xfs_lic_init(&(tp->t_items));
  225. XFS_LBC_INIT(&(tp->t_busy));
  226. return tp;
  227. }
  228. /*
  229. * This is called to create a new transaction which will share the
  230. * permanent log reservation of the given transaction. The remaining
  231. * unused block and rt extent reservations are also inherited. This
  232. * implies that the original transaction is no longer allowed to allocate
  233. * blocks. Locks and log items, however, are no inherited. They must
  234. * be added to the new transaction explicitly.
  235. */
  236. xfs_trans_t *
  237. xfs_trans_dup(
  238. xfs_trans_t *tp)
  239. {
  240. xfs_trans_t *ntp;
  241. ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
  242. /*
  243. * Initialize the new transaction structure.
  244. */
  245. ntp->t_magic = XFS_TRANS_MAGIC;
  246. ntp->t_type = tp->t_type;
  247. ntp->t_mountp = tp->t_mountp;
  248. ntp->t_items_free = XFS_LIC_NUM_SLOTS;
  249. ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
  250. xfs_lic_init(&(ntp->t_items));
  251. XFS_LBC_INIT(&(ntp->t_busy));
  252. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  253. ASSERT(tp->t_ticket != NULL);
  254. ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
  255. ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
  256. ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
  257. tp->t_blk_res = tp->t_blk_res_used;
  258. ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
  259. tp->t_rtx_res = tp->t_rtx_res_used;
  260. ntp->t_pflags = tp->t_pflags;
  261. xfs_trans_dup_dqinfo(tp, ntp);
  262. atomic_inc(&tp->t_mountp->m_active_trans);
  263. return ntp;
  264. }
  265. /*
  266. * This is called to reserve free disk blocks and log space for the
  267. * given transaction. This must be done before allocating any resources
  268. * within the transaction.
  269. *
  270. * This will return ENOSPC if there are not enough blocks available.
  271. * It will sleep waiting for available log space.
  272. * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
  273. * is used by long running transactions. If any one of the reservations
  274. * fails then they will all be backed out.
  275. *
  276. * This does not do quota reservations. That typically is done by the
  277. * caller afterwards.
  278. */
  279. int
  280. xfs_trans_reserve(
  281. xfs_trans_t *tp,
  282. uint blocks,
  283. uint logspace,
  284. uint rtextents,
  285. uint flags,
  286. uint logcount)
  287. {
  288. int log_flags;
  289. int error = 0;
  290. int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  291. /* Mark this thread as being in a transaction */
  292. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  293. /*
  294. * Attempt to reserve the needed disk blocks by decrementing
  295. * the number needed from the number available. This will
  296. * fail if the count would go below zero.
  297. */
  298. if (blocks > 0) {
  299. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
  300. -((int64_t)blocks), rsvd);
  301. if (error != 0) {
  302. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  303. return (XFS_ERROR(ENOSPC));
  304. }
  305. tp->t_blk_res += blocks;
  306. }
  307. /*
  308. * Reserve the log space needed for this transaction.
  309. */
  310. if (logspace > 0) {
  311. ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
  312. ASSERT((tp->t_log_count == 0) ||
  313. (tp->t_log_count == logcount));
  314. if (flags & XFS_TRANS_PERM_LOG_RES) {
  315. log_flags = XFS_LOG_PERM_RESERV;
  316. tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
  317. } else {
  318. ASSERT(tp->t_ticket == NULL);
  319. ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
  320. log_flags = 0;
  321. }
  322. error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
  323. &tp->t_ticket,
  324. XFS_TRANSACTION, log_flags, tp->t_type);
  325. if (error) {
  326. goto undo_blocks;
  327. }
  328. tp->t_log_res = logspace;
  329. tp->t_log_count = logcount;
  330. }
  331. /*
  332. * Attempt to reserve the needed realtime extents by decrementing
  333. * the number needed from the number available. This will
  334. * fail if the count would go below zero.
  335. */
  336. if (rtextents > 0) {
  337. error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
  338. -((int64_t)rtextents), rsvd);
  339. if (error) {
  340. error = XFS_ERROR(ENOSPC);
  341. goto undo_log;
  342. }
  343. tp->t_rtx_res += rtextents;
  344. }
  345. return 0;
  346. /*
  347. * Error cases jump to one of these labels to undo any
  348. * reservations which have already been performed.
  349. */
  350. undo_log:
  351. if (logspace > 0) {
  352. if (flags & XFS_TRANS_PERM_LOG_RES) {
  353. log_flags = XFS_LOG_REL_PERM_RESERV;
  354. } else {
  355. log_flags = 0;
  356. }
  357. xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
  358. tp->t_ticket = NULL;
  359. tp->t_log_res = 0;
  360. tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
  361. }
  362. undo_blocks:
  363. if (blocks > 0) {
  364. (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
  365. (int64_t)blocks, rsvd);
  366. tp->t_blk_res = 0;
  367. }
  368. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  369. return error;
  370. }
  371. /*
  372. * Record the indicated change to the given field for application
  373. * to the file system's superblock when the transaction commits.
  374. * For now, just store the change in the transaction structure.
  375. *
  376. * Mark the transaction structure to indicate that the superblock
  377. * needs to be updated before committing.
  378. *
  379. * Because we may not be keeping track of allocated/free inodes and
  380. * used filesystem blocks in the superblock, we do not mark the
  381. * superblock dirty in this transaction if we modify these fields.
  382. * We still need to update the transaction deltas so that they get
  383. * applied to the incore superblock, but we don't want them to
  384. * cause the superblock to get locked and logged if these are the
  385. * only fields in the superblock that the transaction modifies.
  386. */
  387. void
  388. xfs_trans_mod_sb(
  389. xfs_trans_t *tp,
  390. uint field,
  391. int64_t delta)
  392. {
  393. uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
  394. xfs_mount_t *mp = tp->t_mountp;
  395. switch (field) {
  396. case XFS_TRANS_SB_ICOUNT:
  397. tp->t_icount_delta += delta;
  398. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  399. flags &= ~XFS_TRANS_SB_DIRTY;
  400. break;
  401. case XFS_TRANS_SB_IFREE:
  402. tp->t_ifree_delta += delta;
  403. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  404. flags &= ~XFS_TRANS_SB_DIRTY;
  405. break;
  406. case XFS_TRANS_SB_FDBLOCKS:
  407. /*
  408. * Track the number of blocks allocated in the
  409. * transaction. Make sure it does not exceed the
  410. * number reserved.
  411. */
  412. if (delta < 0) {
  413. tp->t_blk_res_used += (uint)-delta;
  414. ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
  415. }
  416. tp->t_fdblocks_delta += delta;
  417. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  418. flags &= ~XFS_TRANS_SB_DIRTY;
  419. break;
  420. case XFS_TRANS_SB_RES_FDBLOCKS:
  421. /*
  422. * The allocation has already been applied to the
  423. * in-core superblock's counter. This should only
  424. * be applied to the on-disk superblock.
  425. */
  426. ASSERT(delta < 0);
  427. tp->t_res_fdblocks_delta += delta;
  428. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  429. flags &= ~XFS_TRANS_SB_DIRTY;
  430. break;
  431. case XFS_TRANS_SB_FREXTENTS:
  432. /*
  433. * Track the number of blocks allocated in the
  434. * transaction. Make sure it does not exceed the
  435. * number reserved.
  436. */
  437. if (delta < 0) {
  438. tp->t_rtx_res_used += (uint)-delta;
  439. ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
  440. }
  441. tp->t_frextents_delta += delta;
  442. break;
  443. case XFS_TRANS_SB_RES_FREXTENTS:
  444. /*
  445. * The allocation has already been applied to the
  446. * in-core superblock's counter. This should only
  447. * be applied to the on-disk superblock.
  448. */
  449. ASSERT(delta < 0);
  450. tp->t_res_frextents_delta += delta;
  451. break;
  452. case XFS_TRANS_SB_DBLOCKS:
  453. ASSERT(delta > 0);
  454. tp->t_dblocks_delta += delta;
  455. break;
  456. case XFS_TRANS_SB_AGCOUNT:
  457. ASSERT(delta > 0);
  458. tp->t_agcount_delta += delta;
  459. break;
  460. case XFS_TRANS_SB_IMAXPCT:
  461. tp->t_imaxpct_delta += delta;
  462. break;
  463. case XFS_TRANS_SB_REXTSIZE:
  464. tp->t_rextsize_delta += delta;
  465. break;
  466. case XFS_TRANS_SB_RBMBLOCKS:
  467. tp->t_rbmblocks_delta += delta;
  468. break;
  469. case XFS_TRANS_SB_RBLOCKS:
  470. tp->t_rblocks_delta += delta;
  471. break;
  472. case XFS_TRANS_SB_REXTENTS:
  473. tp->t_rextents_delta += delta;
  474. break;
  475. case XFS_TRANS_SB_REXTSLOG:
  476. tp->t_rextslog_delta += delta;
  477. break;
  478. default:
  479. ASSERT(0);
  480. return;
  481. }
  482. tp->t_flags |= flags;
  483. }
  484. /*
  485. * xfs_trans_apply_sb_deltas() is called from the commit code
  486. * to bring the superblock buffer into the current transaction
  487. * and modify it as requested by earlier calls to xfs_trans_mod_sb().
  488. *
  489. * For now we just look at each field allowed to change and change
  490. * it if necessary.
  491. */
  492. STATIC void
  493. xfs_trans_apply_sb_deltas(
  494. xfs_trans_t *tp)
  495. {
  496. xfs_dsb_t *sbp;
  497. xfs_buf_t *bp;
  498. int whole = 0;
  499. bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
  500. sbp = XFS_BUF_TO_SBP(bp);
  501. /*
  502. * Check that superblock mods match the mods made to AGF counters.
  503. */
  504. ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
  505. (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
  506. tp->t_ag_btree_delta));
  507. /*
  508. * Only update the superblock counters if we are logging them
  509. */
  510. if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
  511. if (tp->t_icount_delta)
  512. be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
  513. if (tp->t_ifree_delta)
  514. be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
  515. if (tp->t_fdblocks_delta)
  516. be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
  517. if (tp->t_res_fdblocks_delta)
  518. be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
  519. }
  520. if (tp->t_frextents_delta)
  521. be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
  522. if (tp->t_res_frextents_delta)
  523. be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
  524. if (tp->t_dblocks_delta) {
  525. be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
  526. whole = 1;
  527. }
  528. if (tp->t_agcount_delta) {
  529. be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
  530. whole = 1;
  531. }
  532. if (tp->t_imaxpct_delta) {
  533. sbp->sb_imax_pct += tp->t_imaxpct_delta;
  534. whole = 1;
  535. }
  536. if (tp->t_rextsize_delta) {
  537. be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
  538. whole = 1;
  539. }
  540. if (tp->t_rbmblocks_delta) {
  541. be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
  542. whole = 1;
  543. }
  544. if (tp->t_rblocks_delta) {
  545. be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
  546. whole = 1;
  547. }
  548. if (tp->t_rextents_delta) {
  549. be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
  550. whole = 1;
  551. }
  552. if (tp->t_rextslog_delta) {
  553. sbp->sb_rextslog += tp->t_rextslog_delta;
  554. whole = 1;
  555. }
  556. if (whole)
  557. /*
  558. * Log the whole thing, the fields are noncontiguous.
  559. */
  560. xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
  561. else
  562. /*
  563. * Since all the modifiable fields are contiguous, we
  564. * can get away with this.
  565. */
  566. xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
  567. offsetof(xfs_dsb_t, sb_frextents) +
  568. sizeof(sbp->sb_frextents) - 1);
  569. }
  570. /*
  571. * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
  572. * and apply superblock counter changes to the in-core superblock. The
  573. * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
  574. * applied to the in-core superblock. The idea is that that has already been
  575. * done.
  576. *
  577. * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
  578. * However, we have to ensure that we only modify each superblock field only
  579. * once because the application of the delta values may not be atomic. That can
  580. * lead to ENOSPC races occurring if we have two separate modifcations of the
  581. * free space counter to put back the entire reservation and then take away
  582. * what we used.
  583. *
  584. * If we are not logging superblock counters, then the inode allocated/free and
  585. * used block counts are not updated in the on disk superblock. In this case,
  586. * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
  587. * still need to update the incore superblock with the changes.
  588. */
  589. STATIC void
  590. xfs_trans_unreserve_and_mod_sb(
  591. xfs_trans_t *tp)
  592. {
  593. xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
  594. xfs_mod_sb_t *msbp;
  595. xfs_mount_t *mp = tp->t_mountp;
  596. /* REFERENCED */
  597. int error;
  598. int rsvd;
  599. int64_t blkdelta = 0;
  600. int64_t rtxdelta = 0;
  601. msbp = msb;
  602. rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
  603. /* calculate free blocks delta */
  604. if (tp->t_blk_res > 0)
  605. blkdelta = tp->t_blk_res;
  606. if ((tp->t_fdblocks_delta != 0) &&
  607. (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  608. (tp->t_flags & XFS_TRANS_SB_DIRTY)))
  609. blkdelta += tp->t_fdblocks_delta;
  610. if (blkdelta != 0) {
  611. msbp->msb_field = XFS_SBS_FDBLOCKS;
  612. msbp->msb_delta = blkdelta;
  613. msbp++;
  614. }
  615. /* calculate free realtime extents delta */
  616. if (tp->t_rtx_res > 0)
  617. rtxdelta = tp->t_rtx_res;
  618. if ((tp->t_frextents_delta != 0) &&
  619. (tp->t_flags & XFS_TRANS_SB_DIRTY))
  620. rtxdelta += tp->t_frextents_delta;
  621. if (rtxdelta != 0) {
  622. msbp->msb_field = XFS_SBS_FREXTENTS;
  623. msbp->msb_delta = rtxdelta;
  624. msbp++;
  625. }
  626. /* apply remaining deltas */
  627. if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  628. (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
  629. if (tp->t_icount_delta != 0) {
  630. msbp->msb_field = XFS_SBS_ICOUNT;
  631. msbp->msb_delta = tp->t_icount_delta;
  632. msbp++;
  633. }
  634. if (tp->t_ifree_delta != 0) {
  635. msbp->msb_field = XFS_SBS_IFREE;
  636. msbp->msb_delta = tp->t_ifree_delta;
  637. msbp++;
  638. }
  639. }
  640. if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
  641. if (tp->t_dblocks_delta != 0) {
  642. msbp->msb_field = XFS_SBS_DBLOCKS;
  643. msbp->msb_delta = tp->t_dblocks_delta;
  644. msbp++;
  645. }
  646. if (tp->t_agcount_delta != 0) {
  647. msbp->msb_field = XFS_SBS_AGCOUNT;
  648. msbp->msb_delta = tp->t_agcount_delta;
  649. msbp++;
  650. }
  651. if (tp->t_imaxpct_delta != 0) {
  652. msbp->msb_field = XFS_SBS_IMAX_PCT;
  653. msbp->msb_delta = tp->t_imaxpct_delta;
  654. msbp++;
  655. }
  656. if (tp->t_rextsize_delta != 0) {
  657. msbp->msb_field = XFS_SBS_REXTSIZE;
  658. msbp->msb_delta = tp->t_rextsize_delta;
  659. msbp++;
  660. }
  661. if (tp->t_rbmblocks_delta != 0) {
  662. msbp->msb_field = XFS_SBS_RBMBLOCKS;
  663. msbp->msb_delta = tp->t_rbmblocks_delta;
  664. msbp++;
  665. }
  666. if (tp->t_rblocks_delta != 0) {
  667. msbp->msb_field = XFS_SBS_RBLOCKS;
  668. msbp->msb_delta = tp->t_rblocks_delta;
  669. msbp++;
  670. }
  671. if (tp->t_rextents_delta != 0) {
  672. msbp->msb_field = XFS_SBS_REXTENTS;
  673. msbp->msb_delta = tp->t_rextents_delta;
  674. msbp++;
  675. }
  676. if (tp->t_rextslog_delta != 0) {
  677. msbp->msb_field = XFS_SBS_REXTSLOG;
  678. msbp->msb_delta = tp->t_rextslog_delta;
  679. msbp++;
  680. }
  681. }
  682. /*
  683. * If we need to change anything, do it.
  684. */
  685. if (msbp > msb) {
  686. error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
  687. (uint)(msbp - msb), rsvd);
  688. ASSERT(error == 0);
  689. }
  690. }
  691. /*
  692. * Total up the number of log iovecs needed to commit this
  693. * transaction. The transaction itself needs one for the
  694. * transaction header. Ask each dirty item in turn how many
  695. * it needs to get the total.
  696. */
  697. static uint
  698. xfs_trans_count_vecs(
  699. xfs_trans_t *tp)
  700. {
  701. int nvecs;
  702. xfs_log_item_desc_t *lidp;
  703. nvecs = 1;
  704. lidp = xfs_trans_first_item(tp);
  705. ASSERT(lidp != NULL);
  706. /* In the non-debug case we need to start bailing out if we
  707. * didn't find a log_item here, return zero and let trans_commit
  708. * deal with it.
  709. */
  710. if (lidp == NULL)
  711. return 0;
  712. while (lidp != NULL) {
  713. /*
  714. * Skip items which aren't dirty in this transaction.
  715. */
  716. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  717. lidp = xfs_trans_next_item(tp, lidp);
  718. continue;
  719. }
  720. lidp->lid_size = IOP_SIZE(lidp->lid_item);
  721. nvecs += lidp->lid_size;
  722. lidp = xfs_trans_next_item(tp, lidp);
  723. }
  724. return nvecs;
  725. }
  726. /*
  727. * Fill in the vector with pointers to data to be logged
  728. * by this transaction. The transaction header takes
  729. * the first vector, and then each dirty item takes the
  730. * number of vectors it indicated it needed in xfs_trans_count_vecs().
  731. *
  732. * As each item fills in the entries it needs, also pin the item
  733. * so that it cannot be flushed out until the log write completes.
  734. */
  735. static void
  736. xfs_trans_fill_vecs(
  737. struct xfs_trans *tp,
  738. struct xfs_log_iovec *log_vector)
  739. {
  740. xfs_log_item_desc_t *lidp;
  741. struct xfs_log_iovec *vecp;
  742. uint nitems;
  743. /*
  744. * Skip over the entry for the transaction header, we'll
  745. * fill that in at the end.
  746. */
  747. vecp = log_vector + 1;
  748. nitems = 0;
  749. lidp = xfs_trans_first_item(tp);
  750. ASSERT(lidp);
  751. while (lidp) {
  752. /* Skip items which aren't dirty in this transaction. */
  753. if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
  754. lidp = xfs_trans_next_item(tp, lidp);
  755. continue;
  756. }
  757. /*
  758. * The item may be marked dirty but not log anything. This can
  759. * be used to get called when a transaction is committed.
  760. */
  761. if (lidp->lid_size)
  762. nitems++;
  763. IOP_FORMAT(lidp->lid_item, vecp);
  764. vecp += lidp->lid_size;
  765. IOP_PIN(lidp->lid_item);
  766. lidp = xfs_trans_next_item(tp, lidp);
  767. }
  768. /*
  769. * Now that we've counted the number of items in this transaction, fill
  770. * in the transaction header. Note that the transaction header does not
  771. * have a log item.
  772. */
  773. tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
  774. tp->t_header.th_type = tp->t_type;
  775. tp->t_header.th_num_items = nitems;
  776. log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
  777. log_vector->i_len = sizeof(xfs_trans_header_t);
  778. log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
  779. }
  780. /*
  781. * Format the transaction direct to the iclog. This isolates the physical
  782. * transaction commit operation from the logical operation and hence allows
  783. * other methods to be introduced without affecting the existing commit path.
  784. */
  785. static int
  786. xfs_trans_commit_iclog(
  787. struct xfs_mount *mp,
  788. struct xfs_trans *tp,
  789. xfs_lsn_t *commit_lsn,
  790. int flags)
  791. {
  792. int shutdown;
  793. int error;
  794. int log_flags = 0;
  795. struct xlog_in_core *commit_iclog;
  796. #define XFS_TRANS_LOGVEC_COUNT 16
  797. struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
  798. struct xfs_log_iovec *log_vector;
  799. uint nvec;
  800. /*
  801. * Ask each log item how many log_vector entries it will
  802. * need so we can figure out how many to allocate.
  803. * Try to avoid the kmem_alloc() call in the common case
  804. * by using a vector from the stack when it fits.
  805. */
  806. nvec = xfs_trans_count_vecs(tp);
  807. if (nvec == 0) {
  808. return ENOMEM; /* triggers a shutdown! */
  809. } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
  810. log_vector = log_vector_fast;
  811. } else {
  812. log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
  813. sizeof(xfs_log_iovec_t),
  814. KM_SLEEP);
  815. }
  816. /*
  817. * Fill in the log_vector and pin the logged items, and
  818. * then write the transaction to the log.
  819. */
  820. xfs_trans_fill_vecs(tp, log_vector);
  821. if (flags & XFS_TRANS_RELEASE_LOG_RES)
  822. log_flags = XFS_LOG_REL_PERM_RESERV;
  823. error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
  824. /*
  825. * The transaction is committed incore here, and can go out to disk
  826. * at any time after this call. However, all the items associated
  827. * with the transaction are still locked and pinned in memory.
  828. */
  829. *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
  830. tp->t_commit_lsn = *commit_lsn;
  831. if (nvec > XFS_TRANS_LOGVEC_COUNT)
  832. kmem_free(log_vector);
  833. /*
  834. * If we got a log write error. Unpin the logitems that we
  835. * had pinned, clean up, free trans structure, and return error.
  836. */
  837. if (error || *commit_lsn == -1) {
  838. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  839. xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
  840. return XFS_ERROR(EIO);
  841. }
  842. /*
  843. * Once the transaction has committed, unused
  844. * reservations need to be released and changes to
  845. * the superblock need to be reflected in the in-core
  846. * version. Do that now.
  847. */
  848. xfs_trans_unreserve_and_mod_sb(tp);
  849. /*
  850. * Tell the LM to call the transaction completion routine
  851. * when the log write with LSN commit_lsn completes (e.g.
  852. * when the transaction commit really hits the on-disk log).
  853. * After this call we cannot reference tp, because the call
  854. * can happen at any time and the call will free the transaction
  855. * structure pointed to by tp. The only case where we call
  856. * the completion routine (xfs_trans_committed) directly is
  857. * if the log is turned off on a debug kernel or we're
  858. * running in simulation mode (the log is explicitly turned
  859. * off).
  860. */
  861. tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
  862. tp->t_logcb.cb_arg = tp;
  863. /*
  864. * We need to pass the iclog buffer which was used for the
  865. * transaction commit record into this function, and attach
  866. * the callback to it. The callback must be attached before
  867. * the items are unlocked to avoid racing with other threads
  868. * waiting for an item to unlock.
  869. */
  870. shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
  871. /*
  872. * Mark this thread as no longer being in a transaction
  873. */
  874. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  875. /*
  876. * Once all the items of the transaction have been copied
  877. * to the in core log and the callback is attached, the
  878. * items can be unlocked.
  879. *
  880. * This will free descriptors pointing to items which were
  881. * not logged since there is nothing more to do with them.
  882. * For items which were logged, we will keep pointers to them
  883. * so they can be unpinned after the transaction commits to disk.
  884. * This will also stamp each modified meta-data item with
  885. * the commit lsn of this transaction for dependency tracking
  886. * purposes.
  887. */
  888. xfs_trans_unlock_items(tp, *commit_lsn);
  889. /*
  890. * If we detected a log error earlier, finish committing
  891. * the transaction now (unpin log items, etc).
  892. *
  893. * Order is critical here, to avoid using the transaction
  894. * pointer after its been freed (by xfs_trans_committed
  895. * either here now, or as a callback). We cannot do this
  896. * step inside xfs_log_notify as was done earlier because
  897. * of this issue.
  898. */
  899. if (shutdown)
  900. xfs_trans_committed(tp, XFS_LI_ABORTED);
  901. /*
  902. * Now that the xfs_trans_committed callback has been attached,
  903. * and the items are released we can finally allow the iclog to
  904. * go to disk.
  905. */
  906. return xfs_log_release_iclog(mp, commit_iclog);
  907. }
  908. /*
  909. * xfs_trans_commit
  910. *
  911. * Commit the given transaction to the log a/synchronously.
  912. *
  913. * XFS disk error handling mechanism is not based on a typical
  914. * transaction abort mechanism. Logically after the filesystem
  915. * gets marked 'SHUTDOWN', we can't let any new transactions
  916. * be durable - ie. committed to disk - because some metadata might
  917. * be inconsistent. In such cases, this returns an error, and the
  918. * caller may assume that all locked objects joined to the transaction
  919. * have already been unlocked as if the commit had succeeded.
  920. * Do not reference the transaction structure after this call.
  921. */
  922. /*ARGSUSED*/
  923. int
  924. _xfs_trans_commit(
  925. xfs_trans_t *tp,
  926. uint flags,
  927. int *log_flushed)
  928. {
  929. xfs_mount_t *mp = tp->t_mountp;
  930. xfs_lsn_t commit_lsn = -1;
  931. int error;
  932. int log_flags = 0;
  933. int sync = tp->t_flags & XFS_TRANS_SYNC;
  934. int shutdown;
  935. /*
  936. * Determine whether this commit is releasing a permanent
  937. * log reservation or not.
  938. */
  939. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  940. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  941. log_flags = XFS_LOG_REL_PERM_RESERV;
  942. }
  943. /*
  944. * If there is nothing to be logged by the transaction,
  945. * then unlock all of the items associated with the
  946. * transaction and free the transaction structure.
  947. * Also make sure to return any reserved blocks to
  948. * the free pool.
  949. */
  950. shut_us_down:
  951. shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
  952. if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
  953. xfs_trans_unreserve_and_mod_sb(tp);
  954. /*
  955. * It is indeed possible for the transaction to be
  956. * not dirty but the dqinfo portion to be. All that
  957. * means is that we have some (non-persistent) quota
  958. * reservations that need to be unreserved.
  959. */
  960. xfs_trans_unreserve_and_mod_dquots(tp);
  961. if (tp->t_ticket) {
  962. commit_lsn = xfs_log_done(mp, tp->t_ticket,
  963. NULL, log_flags);
  964. if (commit_lsn == -1 && !shutdown)
  965. shutdown = XFS_ERROR(EIO);
  966. }
  967. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  968. xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
  969. xfs_trans_free_busy(tp);
  970. xfs_trans_free(tp);
  971. XFS_STATS_INC(xs_trans_empty);
  972. return (shutdown);
  973. }
  974. ASSERT(tp->t_ticket != NULL);
  975. /*
  976. * If we need to update the superblock, then do it now.
  977. */
  978. if (tp->t_flags & XFS_TRANS_SB_DIRTY)
  979. xfs_trans_apply_sb_deltas(tp);
  980. xfs_trans_apply_dquot_deltas(tp);
  981. error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
  982. if (error == ENOMEM) {
  983. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  984. goto shut_us_down;
  985. }
  986. /*
  987. * If the transaction needs to be synchronous, then force the
  988. * log out now and wait for it.
  989. */
  990. if (sync) {
  991. if (!error) {
  992. error = _xfs_log_force_lsn(mp, commit_lsn,
  993. XFS_LOG_SYNC, log_flushed);
  994. }
  995. XFS_STATS_INC(xs_trans_sync);
  996. } else {
  997. XFS_STATS_INC(xs_trans_async);
  998. }
  999. return (error);
  1000. }
  1001. /*
  1002. * Called from the trans_commit code when we notice that
  1003. * the filesystem is in the middle of a forced shutdown.
  1004. */
  1005. STATIC void
  1006. xfs_trans_uncommit(
  1007. xfs_trans_t *tp,
  1008. uint flags)
  1009. {
  1010. xfs_log_item_desc_t *lidp;
  1011. for (lidp = xfs_trans_first_item(tp);
  1012. lidp != NULL;
  1013. lidp = xfs_trans_next_item(tp, lidp)) {
  1014. /*
  1015. * Unpin all but those that aren't dirty.
  1016. */
  1017. if (lidp->lid_flags & XFS_LID_DIRTY)
  1018. IOP_UNPIN_REMOVE(lidp->lid_item, tp);
  1019. }
  1020. xfs_trans_unreserve_and_mod_sb(tp);
  1021. xfs_trans_unreserve_and_mod_dquots(tp);
  1022. xfs_trans_free_items(tp, flags);
  1023. xfs_trans_free_busy(tp);
  1024. xfs_trans_free(tp);
  1025. }
  1026. /*
  1027. * Unlock all of the transaction's items and free the transaction.
  1028. * The transaction must not have modified any of its items, because
  1029. * there is no way to restore them to their previous state.
  1030. *
  1031. * If the transaction has made a log reservation, make sure to release
  1032. * it as well.
  1033. */
  1034. void
  1035. xfs_trans_cancel(
  1036. xfs_trans_t *tp,
  1037. int flags)
  1038. {
  1039. int log_flags;
  1040. #ifdef DEBUG
  1041. xfs_log_item_chunk_t *licp;
  1042. xfs_log_item_desc_t *lidp;
  1043. xfs_log_item_t *lip;
  1044. int i;
  1045. #endif
  1046. xfs_mount_t *mp = tp->t_mountp;
  1047. /*
  1048. * See if the caller is being too lazy to figure out if
  1049. * the transaction really needs an abort.
  1050. */
  1051. if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
  1052. flags &= ~XFS_TRANS_ABORT;
  1053. /*
  1054. * See if the caller is relying on us to shut down the
  1055. * filesystem. This happens in paths where we detect
  1056. * corruption and decide to give up.
  1057. */
  1058. if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
  1059. XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
  1060. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1061. }
  1062. #ifdef DEBUG
  1063. if (!(flags & XFS_TRANS_ABORT)) {
  1064. licp = &(tp->t_items);
  1065. while (licp != NULL) {
  1066. lidp = licp->lic_descs;
  1067. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  1068. if (xfs_lic_isfree(licp, i)) {
  1069. continue;
  1070. }
  1071. lip = lidp->lid_item;
  1072. if (!XFS_FORCED_SHUTDOWN(mp))
  1073. ASSERT(!(lip->li_type == XFS_LI_EFD));
  1074. }
  1075. licp = licp->lic_next;
  1076. }
  1077. }
  1078. #endif
  1079. xfs_trans_unreserve_and_mod_sb(tp);
  1080. xfs_trans_unreserve_and_mod_dquots(tp);
  1081. if (tp->t_ticket) {
  1082. if (flags & XFS_TRANS_RELEASE_LOG_RES) {
  1083. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1084. log_flags = XFS_LOG_REL_PERM_RESERV;
  1085. } else {
  1086. log_flags = 0;
  1087. }
  1088. xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
  1089. }
  1090. /* mark this thread as no longer being in a transaction */
  1091. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  1092. xfs_trans_free_items(tp, flags);
  1093. xfs_trans_free_busy(tp);
  1094. xfs_trans_free(tp);
  1095. }
  1096. /*
  1097. * Free the transaction structure. If there is more clean up
  1098. * to do when the structure is freed, add it here.
  1099. */
  1100. STATIC void
  1101. xfs_trans_free(
  1102. xfs_trans_t *tp)
  1103. {
  1104. atomic_dec(&tp->t_mountp->m_active_trans);
  1105. xfs_trans_free_dqinfo(tp);
  1106. kmem_zone_free(xfs_trans_zone, tp);
  1107. }
  1108. /*
  1109. * Roll from one trans in the sequence of PERMANENT transactions to
  1110. * the next: permanent transactions are only flushed out when
  1111. * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
  1112. * as possible to let chunks of it go to the log. So we commit the
  1113. * chunk we've been working on and get a new transaction to continue.
  1114. */
  1115. int
  1116. xfs_trans_roll(
  1117. struct xfs_trans **tpp,
  1118. struct xfs_inode *dp)
  1119. {
  1120. struct xfs_trans *trans;
  1121. unsigned int logres, count;
  1122. int error;
  1123. /*
  1124. * Ensure that the inode is always logged.
  1125. */
  1126. trans = *tpp;
  1127. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  1128. /*
  1129. * Copy the critical parameters from one trans to the next.
  1130. */
  1131. logres = trans->t_log_res;
  1132. count = trans->t_log_count;
  1133. *tpp = xfs_trans_dup(trans);
  1134. /*
  1135. * Commit the current transaction.
  1136. * If this commit failed, then it'd just unlock those items that
  1137. * are not marked ihold. That also means that a filesystem shutdown
  1138. * is in progress. The caller takes the responsibility to cancel
  1139. * the duplicate transaction that gets returned.
  1140. */
  1141. error = xfs_trans_commit(trans, 0);
  1142. if (error)
  1143. return (error);
  1144. trans = *tpp;
  1145. /*
  1146. * transaction commit worked ok so we can drop the extra ticket
  1147. * reference that we gained in xfs_trans_dup()
  1148. */
  1149. xfs_log_ticket_put(trans->t_ticket);
  1150. /*
  1151. * Reserve space in the log for th next transaction.
  1152. * This also pushes items in the "AIL", the list of logged items,
  1153. * out to disk if they are taking up space at the tail of the log
  1154. * that we want to use. This requires that either nothing be locked
  1155. * across this call, or that anything that is locked be logged in
  1156. * the prior and the next transactions.
  1157. */
  1158. error = xfs_trans_reserve(trans, 0, logres, 0,
  1159. XFS_TRANS_PERM_LOG_RES, count);
  1160. /*
  1161. * Ensure that the inode is in the new transaction and locked.
  1162. */
  1163. if (error)
  1164. return error;
  1165. xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
  1166. xfs_trans_ihold(trans, dp);
  1167. return 0;
  1168. }
  1169. /*
  1170. * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
  1171. *
  1172. * This is typically called by the LM when a transaction has been fully
  1173. * committed to disk. It needs to unpin the items which have
  1174. * been logged by the transaction and update their positions
  1175. * in the AIL if necessary.
  1176. * This also gets called when the transactions didn't get written out
  1177. * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
  1178. *
  1179. * Call xfs_trans_chunk_committed() to process the items in
  1180. * each chunk.
  1181. */
  1182. STATIC void
  1183. xfs_trans_committed(
  1184. xfs_trans_t *tp,
  1185. int abortflag)
  1186. {
  1187. xfs_log_item_chunk_t *licp;
  1188. xfs_log_item_chunk_t *next_licp;
  1189. xfs_log_busy_chunk_t *lbcp;
  1190. xfs_log_busy_slot_t *lbsp;
  1191. int i;
  1192. /*
  1193. * Call the transaction's completion callback if there
  1194. * is one.
  1195. */
  1196. if (tp->t_callback != NULL) {
  1197. tp->t_callback(tp, tp->t_callarg);
  1198. }
  1199. /*
  1200. * Special case the chunk embedded in the transaction.
  1201. */
  1202. licp = &(tp->t_items);
  1203. if (!(xfs_lic_are_all_free(licp))) {
  1204. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1205. }
  1206. /*
  1207. * Process the items in each chunk in turn.
  1208. */
  1209. licp = licp->lic_next;
  1210. while (licp != NULL) {
  1211. ASSERT(!xfs_lic_are_all_free(licp));
  1212. xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
  1213. next_licp = licp->lic_next;
  1214. kmem_free(licp);
  1215. licp = next_licp;
  1216. }
  1217. /*
  1218. * Clear all the per-AG busy list items listed in this transaction
  1219. */
  1220. lbcp = &tp->t_busy;
  1221. while (lbcp != NULL) {
  1222. for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
  1223. if (!XFS_LBC_ISFREE(lbcp, i)) {
  1224. xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
  1225. lbsp->lbc_idx);
  1226. }
  1227. }
  1228. lbcp = lbcp->lbc_next;
  1229. }
  1230. xfs_trans_free_busy(tp);
  1231. /*
  1232. * That's it for the transaction structure. Free it.
  1233. */
  1234. xfs_trans_free(tp);
  1235. }
  1236. /*
  1237. * This is called to perform the commit processing for each
  1238. * item described by the given chunk.
  1239. *
  1240. * The commit processing consists of unlocking items which were
  1241. * held locked with the SYNC_UNLOCK attribute, calling the committed
  1242. * routine of each logged item, updating the item's position in the AIL
  1243. * if necessary, and unpinning each item. If the committed routine
  1244. * returns -1, then do nothing further with the item because it
  1245. * may have been freed.
  1246. *
  1247. * Since items are unlocked when they are copied to the incore
  1248. * log, it is possible for two transactions to be completing
  1249. * and manipulating the same item simultaneously. The AIL lock
  1250. * will protect the lsn field of each item. The value of this
  1251. * field can never go backwards.
  1252. *
  1253. * We unpin the items after repositioning them in the AIL, because
  1254. * otherwise they could be immediately flushed and we'd have to race
  1255. * with the flusher trying to pull the item from the AIL as we add it.
  1256. */
  1257. STATIC void
  1258. xfs_trans_chunk_committed(
  1259. xfs_log_item_chunk_t *licp,
  1260. xfs_lsn_t lsn,
  1261. int aborted)
  1262. {
  1263. xfs_log_item_desc_t *lidp;
  1264. xfs_log_item_t *lip;
  1265. xfs_lsn_t item_lsn;
  1266. int i;
  1267. lidp = licp->lic_descs;
  1268. for (i = 0; i < licp->lic_unused; i++, lidp++) {
  1269. struct xfs_ail *ailp;
  1270. if (xfs_lic_isfree(licp, i)) {
  1271. continue;
  1272. }
  1273. lip = lidp->lid_item;
  1274. if (aborted)
  1275. lip->li_flags |= XFS_LI_ABORTED;
  1276. /*
  1277. * Send in the ABORTED flag to the COMMITTED routine
  1278. * so that it knows whether the transaction was aborted
  1279. * or not.
  1280. */
  1281. item_lsn = IOP_COMMITTED(lip, lsn);
  1282. /*
  1283. * If the committed routine returns -1, make
  1284. * no more references to the item.
  1285. */
  1286. if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
  1287. continue;
  1288. }
  1289. /*
  1290. * If the returned lsn is greater than what it
  1291. * contained before, update the location of the
  1292. * item in the AIL. If it is not, then do nothing.
  1293. * Items can never move backwards in the AIL.
  1294. *
  1295. * While the new lsn should usually be greater, it
  1296. * is possible that a later transaction completing
  1297. * simultaneously with an earlier one using the
  1298. * same item could complete first with a higher lsn.
  1299. * This would cause the earlier transaction to fail
  1300. * the test below.
  1301. */
  1302. ailp = lip->li_ailp;
  1303. spin_lock(&ailp->xa_lock);
  1304. if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
  1305. /*
  1306. * This will set the item's lsn to item_lsn
  1307. * and update the position of the item in
  1308. * the AIL.
  1309. *
  1310. * xfs_trans_ail_update() drops the AIL lock.
  1311. */
  1312. xfs_trans_ail_update(ailp, lip, item_lsn);
  1313. } else {
  1314. spin_unlock(&ailp->xa_lock);
  1315. }
  1316. /*
  1317. * Now that we've repositioned the item in the AIL,
  1318. * unpin it so it can be flushed. Pass information
  1319. * about buffer stale state down from the log item
  1320. * flags, if anyone else stales the buffer we do not
  1321. * want to pay any attention to it.
  1322. */
  1323. IOP_UNPIN(lip);
  1324. }
  1325. }