xfs_trans.c 38 KB

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