xfs_trans.c 39 KB

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