xfs_trans.c 38 KB

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