xfs_trans.c 37 KB

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