log.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/gfs2_ondisk.h>
  12. #include <linux/crc32.h>
  13. #include <linux/crc32c.h>
  14. #include <linux/delay.h>
  15. #include <linux/kthread.h>
  16. #include <linux/freezer.h>
  17. #include <linux/bio.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/writeback.h>
  20. #include <linux/list_sort.h>
  21. #include "gfs2.h"
  22. #include "incore.h"
  23. #include "bmap.h"
  24. #include "glock.h"
  25. #include "log.h"
  26. #include "lops.h"
  27. #include "meta_io.h"
  28. #include "util.h"
  29. #include "dir.h"
  30. #include "trace_gfs2.h"
  31. #include "trans.h"
  32. #include "aops.h"
  33. static void gfs2_log_shutdown(struct gfs2_sbd *sdp);
  34. /**
  35. * gfs2_struct2blk - compute stuff
  36. * @sdp: the filesystem
  37. * @nstruct: the number of structures
  38. *
  39. * Compute the number of log descriptor blocks needed to hold a certain number
  40. * of structures of a certain size.
  41. *
  42. * Returns: the number of blocks needed (minimum is always 1)
  43. */
  44. unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct)
  45. {
  46. unsigned int blks;
  47. unsigned int first, second;
  48. /* The initial struct gfs2_log_descriptor block */
  49. blks = 1;
  50. first = sdp->sd_ldptrs;
  51. if (nstruct > first) {
  52. /* Subsequent struct gfs2_meta_header blocks */
  53. second = sdp->sd_inptrs;
  54. blks += DIV_ROUND_UP(nstruct - first, second);
  55. }
  56. return blks;
  57. }
  58. /**
  59. * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
  60. * @bd: The gfs2_bufdata to remove
  61. *
  62. * The ail lock _must_ be held when calling this function
  63. *
  64. */
  65. void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
  66. {
  67. bd->bd_tr = NULL;
  68. list_del_init(&bd->bd_ail_st_list);
  69. list_del_init(&bd->bd_ail_gl_list);
  70. atomic_dec(&bd->bd_gl->gl_ail_count);
  71. brelse(bd->bd_bh);
  72. }
  73. /**
  74. * gfs2_ail1_start_one - Start I/O on a transaction
  75. * @sdp: The superblock
  76. * @wbc: The writeback control structure
  77. * @tr: The transaction to start I/O on
  78. * @plug: The block plug currently active
  79. */
  80. static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
  81. struct writeback_control *wbc,
  82. struct gfs2_trans *tr, struct blk_plug *plug)
  83. __releases(&sdp->sd_ail_lock)
  84. __acquires(&sdp->sd_ail_lock)
  85. {
  86. struct gfs2_glock *gl = NULL;
  87. struct address_space *mapping;
  88. struct gfs2_bufdata *bd, *s;
  89. struct buffer_head *bh;
  90. int ret = 0;
  91. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
  92. bh = bd->bd_bh;
  93. gfs2_assert(sdp, bd->bd_tr == tr);
  94. if (!buffer_busy(bh)) {
  95. if (buffer_uptodate(bh)) {
  96. list_move(&bd->bd_ail_st_list,
  97. &tr->tr_ail2_list);
  98. continue;
  99. }
  100. if (!cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
  101. gfs2_io_error_bh(sdp, bh);
  102. gfs2_withdraw_delayed(sdp);
  103. }
  104. }
  105. if (gfs2_withdrawing_or_withdrawn(sdp)) {
  106. gfs2_remove_from_ail(bd);
  107. continue;
  108. }
  109. if (!buffer_dirty(bh))
  110. continue;
  111. if (gl == bd->bd_gl)
  112. continue;
  113. gl = bd->bd_gl;
  114. list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
  115. mapping = bh->b_folio->mapping;
  116. if (!mapping)
  117. continue;
  118. spin_unlock(&sdp->sd_ail_lock);
  119. BUG_ON(GFS2_SB(mapping->host) != sdp);
  120. if (gfs2_is_jdata(GFS2_I(mapping->host)))
  121. ret = gfs2_jdata_writeback(mapping, wbc);
  122. else
  123. ret = mapping->a_ops->writepages(mapping, wbc);
  124. if (need_resched()) {
  125. blk_finish_plug(plug);
  126. cond_resched();
  127. blk_start_plug(plug);
  128. }
  129. spin_lock(&sdp->sd_ail_lock);
  130. if (ret == -ENODATA) /* if a jdata write into a new hole */
  131. ret = 0; /* ignore it */
  132. mapping_set_error(mapping, ret);
  133. if (ret || wbc->nr_to_write <= 0)
  134. break;
  135. return -EBUSY;
  136. }
  137. return ret;
  138. }
  139. static void dump_ail_list(struct gfs2_sbd *sdp)
  140. {
  141. struct gfs2_trans *tr;
  142. struct gfs2_bufdata *bd;
  143. struct buffer_head *bh;
  144. list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
  145. list_for_each_entry_reverse(bd, &tr->tr_ail1_list,
  146. bd_ail_st_list) {
  147. bh = bd->bd_bh;
  148. fs_err(sdp, "bd %p: blk:0x%llx bh=%p ", bd,
  149. (unsigned long long)bd->bd_blkno, bh);
  150. if (!bh) {
  151. fs_err(sdp, "\n");
  152. continue;
  153. }
  154. fs_err(sdp, "0x%llx up2:%d dirt:%d lkd:%d req:%d "
  155. "map:%d new:%d ar:%d aw:%d delay:%d "
  156. "io err:%d unwritten:%d dfr:%d pin:%d esc:%d\n",
  157. (unsigned long long)bh->b_blocknr,
  158. buffer_uptodate(bh), buffer_dirty(bh),
  159. buffer_locked(bh), buffer_req(bh),
  160. buffer_mapped(bh), buffer_new(bh),
  161. buffer_async_read(bh), buffer_async_write(bh),
  162. buffer_delay(bh), buffer_write_io_error(bh),
  163. buffer_unwritten(bh),
  164. buffer_defer_completion(bh),
  165. buffer_pinned(bh), buffer_escaped(bh));
  166. }
  167. }
  168. }
  169. /**
  170. * gfs2_ail1_flush - start writeback of some ail1 entries
  171. * @sdp: The super block
  172. * @wbc: The writeback control structure
  173. *
  174. * Writes back some ail1 entries, according to the limits in the
  175. * writeback control structure
  176. */
  177. void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
  178. {
  179. struct list_head *head = &sdp->sd_ail1_list;
  180. struct gfs2_trans *tr;
  181. struct blk_plug plug;
  182. int ret;
  183. unsigned long flush_start = jiffies;
  184. trace_gfs2_ail_flush(sdp, wbc, 1);
  185. blk_start_plug(&plug);
  186. spin_lock(&sdp->sd_ail_lock);
  187. restart:
  188. ret = 0;
  189. if (time_after(jiffies, flush_start + (HZ * 600))) {
  190. fs_err(sdp, "Error: In %s for ten minutes! t=%d\n",
  191. __func__, current->journal_info ? 1 : 0);
  192. dump_ail_list(sdp);
  193. goto out;
  194. }
  195. list_for_each_entry_reverse(tr, head, tr_list) {
  196. if (wbc->nr_to_write <= 0)
  197. break;
  198. ret = gfs2_ail1_start_one(sdp, wbc, tr, &plug);
  199. if (ret) {
  200. if (ret == -EBUSY)
  201. goto restart;
  202. break;
  203. }
  204. }
  205. out:
  206. spin_unlock(&sdp->sd_ail_lock);
  207. blk_finish_plug(&plug);
  208. if (ret) {
  209. gfs2_lm(sdp, "gfs2_ail1_start_one returned: %d\n", ret);
  210. gfs2_withdraw(sdp);
  211. }
  212. trace_gfs2_ail_flush(sdp, wbc, 0);
  213. }
  214. /**
  215. * gfs2_ail1_start - start writeback of all ail1 entries
  216. * @sdp: The superblock
  217. */
  218. static void gfs2_ail1_start(struct gfs2_sbd *sdp)
  219. {
  220. struct writeback_control wbc = {
  221. .sync_mode = WB_SYNC_NONE,
  222. .nr_to_write = LONG_MAX,
  223. .range_start = 0,
  224. .range_end = LLONG_MAX,
  225. };
  226. return gfs2_ail1_flush(sdp, &wbc);
  227. }
  228. static void gfs2_log_update_flush_tail(struct gfs2_sbd *sdp)
  229. {
  230. unsigned int new_flush_tail = sdp->sd_log_head;
  231. struct gfs2_trans *tr;
  232. if (!list_empty(&sdp->sd_ail1_list)) {
  233. tr = list_last_entry(&sdp->sd_ail1_list,
  234. struct gfs2_trans, tr_list);
  235. new_flush_tail = tr->tr_first;
  236. }
  237. sdp->sd_log_flush_tail = new_flush_tail;
  238. }
  239. static void gfs2_log_update_head(struct gfs2_sbd *sdp)
  240. {
  241. unsigned int new_head = sdp->sd_log_flush_head;
  242. if (sdp->sd_log_flush_tail == sdp->sd_log_head)
  243. sdp->sd_log_flush_tail = new_head;
  244. sdp->sd_log_head = new_head;
  245. }
  246. /*
  247. * gfs2_ail_empty_tr - empty one of the ail lists of a transaction
  248. */
  249. static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
  250. struct list_head *head)
  251. {
  252. struct gfs2_bufdata *bd;
  253. while (!list_empty(head)) {
  254. bd = list_first_entry(head, struct gfs2_bufdata,
  255. bd_ail_st_list);
  256. gfs2_assert(sdp, bd->bd_tr == tr);
  257. gfs2_remove_from_ail(bd);
  258. }
  259. }
  260. /**
  261. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  262. * @sdp: the filesystem
  263. * @tr: the transaction
  264. * @max_revokes: If nonzero, issue revokes for the bd items for written buffers
  265. *
  266. * returns: the transaction's count of remaining active items
  267. */
  268. static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
  269. int *max_revokes)
  270. {
  271. struct gfs2_bufdata *bd, *s;
  272. struct buffer_head *bh;
  273. int active_count = 0;
  274. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
  275. bd_ail_st_list) {
  276. bh = bd->bd_bh;
  277. gfs2_assert(sdp, bd->bd_tr == tr);
  278. /*
  279. * If another process flagged an io error, e.g. writing to the
  280. * journal, error all other bhs and move them off the ail1 to
  281. * prevent a tight loop when unmount tries to flush ail1,
  282. * regardless of whether they're still busy. If no outside
  283. * errors were found and the buffer is busy, move to the next.
  284. * If the ail buffer is not busy and caught an error, flag it
  285. * for others.
  286. */
  287. if (!sdp->sd_log_error && buffer_busy(bh)) {
  288. active_count++;
  289. continue;
  290. }
  291. if (!buffer_uptodate(bh) &&
  292. !cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
  293. gfs2_io_error_bh(sdp, bh);
  294. gfs2_withdraw_delayed(sdp);
  295. }
  296. /*
  297. * If we have space for revokes and the bd is no longer on any
  298. * buf list, we can just add a revoke for it immediately and
  299. * avoid having to put it on the ail2 list, where it would need
  300. * to be revoked later.
  301. */
  302. if (*max_revokes && list_empty(&bd->bd_list)) {
  303. gfs2_add_revoke(sdp, bd);
  304. (*max_revokes)--;
  305. continue;
  306. }
  307. list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
  308. }
  309. return active_count;
  310. }
  311. /**
  312. * gfs2_ail1_empty - Try to empty the ail1 lists
  313. * @sdp: The superblock
  314. * @max_revokes: If non-zero, add revokes where appropriate
  315. *
  316. * Tries to empty the ail1 lists, starting with the oldest first.
  317. * Returns %true if the ail1 list is now empty.
  318. */
  319. static bool gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes)
  320. {
  321. struct gfs2_trans *tr, *s;
  322. int oldest_tr = 1;
  323. bool empty;
  324. spin_lock(&sdp->sd_ail_lock);
  325. list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
  326. if (!gfs2_ail1_empty_one(sdp, tr, &max_revokes) && oldest_tr)
  327. list_move(&tr->tr_list, &sdp->sd_ail2_list);
  328. else
  329. oldest_tr = 0;
  330. }
  331. gfs2_log_update_flush_tail(sdp);
  332. empty = list_empty(&sdp->sd_ail1_list);
  333. spin_unlock(&sdp->sd_ail_lock);
  334. return empty;
  335. }
  336. static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
  337. {
  338. struct gfs2_trans *tr;
  339. struct gfs2_bufdata *bd;
  340. struct buffer_head *bh;
  341. spin_lock(&sdp->sd_ail_lock);
  342. list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
  343. list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
  344. bh = bd->bd_bh;
  345. if (!buffer_locked(bh))
  346. continue;
  347. get_bh(bh);
  348. spin_unlock(&sdp->sd_ail_lock);
  349. wait_on_buffer(bh);
  350. brelse(bh);
  351. return;
  352. }
  353. }
  354. spin_unlock(&sdp->sd_ail_lock);
  355. }
  356. static void __ail2_empty(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  357. {
  358. gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
  359. list_del(&tr->tr_list);
  360. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
  361. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
  362. gfs2_trans_free(sdp, tr);
  363. }
  364. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  365. {
  366. struct list_head *ail2_list = &sdp->sd_ail2_list;
  367. unsigned int old_tail = sdp->sd_log_tail;
  368. struct gfs2_trans *tr, *safe;
  369. spin_lock(&sdp->sd_ail_lock);
  370. if (old_tail <= new_tail) {
  371. list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
  372. if (old_tail <= tr->tr_first && tr->tr_first < new_tail)
  373. __ail2_empty(sdp, tr);
  374. }
  375. } else {
  376. list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
  377. if (old_tail <= tr->tr_first || tr->tr_first < new_tail)
  378. __ail2_empty(sdp, tr);
  379. }
  380. }
  381. spin_unlock(&sdp->sd_ail_lock);
  382. }
  383. /**
  384. * gfs2_log_is_empty - Check if the log is empty
  385. * @sdp: The GFS2 superblock
  386. */
  387. bool gfs2_log_is_empty(struct gfs2_sbd *sdp) {
  388. return atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks;
  389. }
  390. static bool __gfs2_log_try_reserve_revokes(struct gfs2_sbd *sdp, unsigned int revokes)
  391. {
  392. unsigned int available;
  393. available = atomic_read(&sdp->sd_log_revokes_available);
  394. while (available >= revokes) {
  395. if (atomic_try_cmpxchg(&sdp->sd_log_revokes_available,
  396. &available, available - revokes))
  397. return true;
  398. }
  399. return false;
  400. }
  401. /**
  402. * gfs2_log_release_revokes - Release a given number of revokes
  403. * @sdp: The GFS2 superblock
  404. * @revokes: The number of revokes to release
  405. *
  406. * sdp->sd_log_flush_lock must be held.
  407. */
  408. void gfs2_log_release_revokes(struct gfs2_sbd *sdp, unsigned int revokes)
  409. {
  410. if (revokes)
  411. atomic_add(revokes, &sdp->sd_log_revokes_available);
  412. }
  413. /**
  414. * gfs2_log_release - Release a given number of log blocks
  415. * @sdp: The GFS2 superblock
  416. * @blks: The number of blocks
  417. *
  418. */
  419. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  420. {
  421. atomic_add(blks, &sdp->sd_log_blks_free);
  422. trace_gfs2_log_blocks(sdp, blks);
  423. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  424. sdp->sd_jdesc->jd_blocks);
  425. if (atomic_read(&sdp->sd_log_blks_needed))
  426. wake_up(&sdp->sd_log_waitq);
  427. }
  428. /**
  429. * __gfs2_log_try_reserve - Try to make a log reservation
  430. * @sdp: The GFS2 superblock
  431. * @blks: The number of blocks to reserve
  432. * @taboo_blks: The number of blocks to leave free
  433. *
  434. * Try to do the same as __gfs2_log_reserve(), but fail if no more log
  435. * space is immediately available.
  436. */
  437. static bool __gfs2_log_try_reserve(struct gfs2_sbd *sdp, unsigned int blks,
  438. unsigned int taboo_blks)
  439. {
  440. unsigned wanted = blks + taboo_blks;
  441. unsigned int free_blocks;
  442. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  443. while (free_blocks >= wanted) {
  444. if (atomic_try_cmpxchg(&sdp->sd_log_blks_free, &free_blocks,
  445. free_blocks - blks)) {
  446. trace_gfs2_log_blocks(sdp, -blks);
  447. return true;
  448. }
  449. }
  450. return false;
  451. }
  452. /**
  453. * __gfs2_log_reserve - Make a log reservation
  454. * @sdp: The GFS2 superblock
  455. * @blks: The number of blocks to reserve
  456. * @taboo_blks: The number of blocks to leave free
  457. *
  458. * @taboo_blks is set to 0 for logd, and to GFS2_LOG_FLUSH_MIN_BLOCKS
  459. * for all other processes. This ensures that when the log is almost full,
  460. * logd will still be able to call gfs2_log_flush one more time without
  461. * blocking, which will advance the tail and make some more log space
  462. * available.
  463. *
  464. * We no longer flush the log here, instead we wake up logd to do that
  465. * for us. To avoid the thundering herd and to ensure that we deal fairly
  466. * with queued waiters, we use an exclusive wait. This means that when we
  467. * get woken with enough journal space to get our reservation, we need to
  468. * wake the next waiter on the list.
  469. */
  470. static void __gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks,
  471. unsigned int taboo_blks)
  472. {
  473. unsigned wanted = blks + taboo_blks;
  474. unsigned int free_blocks;
  475. atomic_add(blks, &sdp->sd_log_blks_needed);
  476. for (;;) {
  477. if (current != sdp->sd_logd_process)
  478. wake_up(&sdp->sd_logd_waitq);
  479. io_wait_event(sdp->sd_log_waitq,
  480. (free_blocks = atomic_read(&sdp->sd_log_blks_free),
  481. free_blocks >= wanted));
  482. do {
  483. if (atomic_try_cmpxchg(&sdp->sd_log_blks_free,
  484. &free_blocks,
  485. free_blocks - blks))
  486. goto reserved;
  487. } while (free_blocks >= wanted);
  488. }
  489. reserved:
  490. trace_gfs2_log_blocks(sdp, -blks);
  491. if (atomic_sub_return(blks, &sdp->sd_log_blks_needed))
  492. wake_up(&sdp->sd_log_waitq);
  493. }
  494. /**
  495. * gfs2_log_try_reserve - Try to make a log reservation
  496. * @sdp: The GFS2 superblock
  497. * @tr: The transaction
  498. * @extra_revokes: The number of additional revokes reserved (output)
  499. *
  500. * This is similar to gfs2_log_reserve, but sdp->sd_log_flush_lock must be
  501. * held for correct revoke accounting.
  502. */
  503. bool gfs2_log_try_reserve(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
  504. unsigned int *extra_revokes)
  505. {
  506. unsigned int blks = tr->tr_reserved;
  507. unsigned int revokes = tr->tr_revokes;
  508. unsigned int revoke_blks = 0;
  509. *extra_revokes = 0;
  510. if (revokes && !__gfs2_log_try_reserve_revokes(sdp, revokes)) {
  511. revoke_blks = DIV_ROUND_UP(revokes, sdp->sd_inptrs);
  512. *extra_revokes = revoke_blks * sdp->sd_inptrs - revokes;
  513. blks += revoke_blks;
  514. }
  515. if (!blks)
  516. return true;
  517. if (__gfs2_log_try_reserve(sdp, blks, GFS2_LOG_FLUSH_MIN_BLOCKS))
  518. return true;
  519. if (!revoke_blks)
  520. gfs2_log_release_revokes(sdp, revokes);
  521. return false;
  522. }
  523. /**
  524. * gfs2_log_reserve - Make a log reservation
  525. * @sdp: The GFS2 superblock
  526. * @tr: The transaction
  527. * @extra_revokes: The number of additional revokes reserved (output)
  528. *
  529. * sdp->sd_log_flush_lock must not be held.
  530. */
  531. void gfs2_log_reserve(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
  532. unsigned int *extra_revokes)
  533. {
  534. unsigned int blks = tr->tr_reserved;
  535. unsigned int revokes = tr->tr_revokes;
  536. unsigned int revoke_blks;
  537. *extra_revokes = 0;
  538. if (revokes) {
  539. revoke_blks = DIV_ROUND_UP(revokes, sdp->sd_inptrs);
  540. *extra_revokes = revoke_blks * sdp->sd_inptrs - revokes;
  541. blks += revoke_blks;
  542. }
  543. __gfs2_log_reserve(sdp, blks, GFS2_LOG_FLUSH_MIN_BLOCKS);
  544. }
  545. /**
  546. * log_distance - Compute distance between two journal blocks
  547. * @sdp: The GFS2 superblock
  548. * @newer: The most recent journal block of the pair
  549. * @older: The older journal block of the pair
  550. *
  551. * Compute the distance (in the journal direction) between two
  552. * blocks in the journal
  553. *
  554. * Returns: the distance in blocks
  555. */
  556. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  557. unsigned int older)
  558. {
  559. int dist;
  560. dist = newer - older;
  561. if (dist < 0)
  562. dist += sdp->sd_jdesc->jd_blocks;
  563. return dist;
  564. }
  565. /**
  566. * calc_reserved - Calculate the number of blocks to keep reserved
  567. * @sdp: The GFS2 superblock
  568. *
  569. * This is complex. We need to reserve room for all our currently used
  570. * metadata blocks (e.g. normal file I/O rewriting file time stamps) and
  571. * all our journaled data blocks for journaled files (e.g. files in the
  572. * meta_fs like rindex, or files for which chattr +j was done.)
  573. * If we don't reserve enough space, corruption will follow.
  574. *
  575. * We can have metadata blocks and jdata blocks in the same journal. Each
  576. * type gets its own log descriptor, for which we need to reserve a block.
  577. * In fact, each type has the potential for needing more than one log descriptor
  578. * in cases where we have more blocks than will fit in a log descriptor.
  579. * Metadata journal entries take up half the space of journaled buffer entries.
  580. *
  581. * Also, we need to reserve blocks for revoke journal entries and one for an
  582. * overall header for the lot.
  583. *
  584. * Returns: the number of blocks reserved
  585. */
  586. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  587. {
  588. unsigned int reserved = GFS2_LOG_FLUSH_MIN_BLOCKS;
  589. unsigned int blocks;
  590. struct gfs2_trans *tr = sdp->sd_log_tr;
  591. if (tr) {
  592. blocks = tr->tr_num_buf_new - tr->tr_num_buf_rm;
  593. reserved += blocks + DIV_ROUND_UP(blocks, buf_limit(sdp));
  594. blocks = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
  595. reserved += blocks + DIV_ROUND_UP(blocks, databuf_limit(sdp));
  596. }
  597. return reserved;
  598. }
  599. static void log_pull_tail(struct gfs2_sbd *sdp)
  600. {
  601. unsigned int new_tail = sdp->sd_log_flush_tail;
  602. unsigned int dist;
  603. if (new_tail == sdp->sd_log_tail)
  604. return;
  605. dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  606. ail2_empty(sdp, new_tail);
  607. gfs2_log_release(sdp, dist);
  608. sdp->sd_log_tail = new_tail;
  609. }
  610. void log_flush_wait(struct gfs2_sbd *sdp)
  611. {
  612. DEFINE_WAIT(wait);
  613. if (atomic_read(&sdp->sd_log_in_flight)) {
  614. do {
  615. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  616. TASK_UNINTERRUPTIBLE);
  617. if (atomic_read(&sdp->sd_log_in_flight))
  618. io_schedule();
  619. } while(atomic_read(&sdp->sd_log_in_flight));
  620. finish_wait(&sdp->sd_log_flush_wait, &wait);
  621. }
  622. }
  623. static int ip_cmp(void *priv, const struct list_head *a, const struct list_head *b)
  624. {
  625. struct gfs2_inode *ipa, *ipb;
  626. ipa = list_entry(a, struct gfs2_inode, i_ordered);
  627. ipb = list_entry(b, struct gfs2_inode, i_ordered);
  628. if (ipa->i_no_addr < ipb->i_no_addr)
  629. return -1;
  630. if (ipa->i_no_addr > ipb->i_no_addr)
  631. return 1;
  632. return 0;
  633. }
  634. static void __ordered_del_inode(struct gfs2_inode *ip)
  635. {
  636. if (!list_empty(&ip->i_ordered))
  637. list_del_init(&ip->i_ordered);
  638. }
  639. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  640. {
  641. struct gfs2_inode *ip;
  642. LIST_HEAD(written);
  643. spin_lock(&sdp->sd_ordered_lock);
  644. list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp);
  645. while (!list_empty(&sdp->sd_log_ordered)) {
  646. ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
  647. if (ip->i_inode.i_mapping->nrpages == 0) {
  648. __ordered_del_inode(ip);
  649. continue;
  650. }
  651. list_move(&ip->i_ordered, &written);
  652. spin_unlock(&sdp->sd_ordered_lock);
  653. filemap_fdatawrite(ip->i_inode.i_mapping);
  654. spin_lock(&sdp->sd_ordered_lock);
  655. }
  656. list_splice(&written, &sdp->sd_log_ordered);
  657. spin_unlock(&sdp->sd_ordered_lock);
  658. }
  659. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  660. {
  661. struct gfs2_inode *ip;
  662. spin_lock(&sdp->sd_ordered_lock);
  663. while (!list_empty(&sdp->sd_log_ordered)) {
  664. ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
  665. __ordered_del_inode(ip);
  666. if (ip->i_inode.i_mapping->nrpages == 0)
  667. continue;
  668. spin_unlock(&sdp->sd_ordered_lock);
  669. filemap_fdatawait(ip->i_inode.i_mapping);
  670. spin_lock(&sdp->sd_ordered_lock);
  671. }
  672. spin_unlock(&sdp->sd_ordered_lock);
  673. }
  674. void gfs2_ordered_del_inode(struct gfs2_inode *ip)
  675. {
  676. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  677. spin_lock(&sdp->sd_ordered_lock);
  678. __ordered_del_inode(ip);
  679. spin_unlock(&sdp->sd_ordered_lock);
  680. }
  681. void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  682. {
  683. struct buffer_head *bh = bd->bd_bh;
  684. struct gfs2_glock *gl = bd->bd_gl;
  685. sdp->sd_log_num_revoke++;
  686. if (atomic_inc_return(&gl->gl_revokes) == 1)
  687. gfs2_glock_hold(gl);
  688. bh->b_private = NULL;
  689. bd->bd_blkno = bh->b_blocknr;
  690. gfs2_remove_from_ail(bd); /* drops ref on bh */
  691. bd->bd_bh = NULL;
  692. set_bit(GLF_LFLUSH, &gl->gl_flags);
  693. list_add(&bd->bd_list, &sdp->sd_log_revokes);
  694. }
  695. void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
  696. {
  697. if (atomic_dec_return(&gl->gl_revokes) == 0) {
  698. clear_bit(GLF_LFLUSH, &gl->gl_flags);
  699. gfs2_glock_put_async(gl);
  700. }
  701. }
  702. /**
  703. * gfs2_flush_revokes - Add as many revokes to the system transaction as we can
  704. * @sdp: The GFS2 superblock
  705. *
  706. * Our usual strategy is to defer writing revokes as much as we can in the hope
  707. * that we'll eventually overwrite the journal, which will make those revokes
  708. * go away. This changes when we flush the log: at that point, there will
  709. * likely be some left-over space in the last revoke block of that transaction.
  710. * We can fill that space with additional revokes for blocks that have already
  711. * been written back. This will basically come at no cost now, and will save
  712. * us from having to keep track of those blocks on the AIL2 list later.
  713. */
  714. void gfs2_flush_revokes(struct gfs2_sbd *sdp)
  715. {
  716. /* number of revokes we still have room for */
  717. unsigned int max_revokes = atomic_read(&sdp->sd_log_revokes_available);
  718. gfs2_log_lock(sdp);
  719. gfs2_ail1_empty(sdp, max_revokes);
  720. gfs2_log_unlock(sdp);
  721. if (gfs2_withdrawing(sdp))
  722. gfs2_withdraw(sdp);
  723. }
  724. /**
  725. * gfs2_write_log_header - Write a journal log header buffer at lblock
  726. * @sdp: The GFS2 superblock
  727. * @jd: journal descriptor of the journal to which we are writing
  728. * @seq: sequence number
  729. * @tail: tail of the log
  730. * @lblock: value for lh_blkno (block number relative to start of journal)
  731. * @flags: log header flags GFS2_LOG_HEAD_*
  732. * @op_flags: flags to pass to the bio
  733. *
  734. * Returns: the initialized log buffer descriptor
  735. */
  736. void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
  737. u64 seq, u32 tail, u32 lblock, u32 flags,
  738. blk_opf_t op_flags)
  739. {
  740. struct gfs2_log_header *lh;
  741. u32 hash, crc;
  742. struct page *page;
  743. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  744. struct timespec64 tv;
  745. struct super_block *sb = sdp->sd_vfs;
  746. u64 dblock;
  747. if (gfs2_withdrawing_or_withdrawn(sdp))
  748. return;
  749. page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  750. lh = page_address(page);
  751. clear_page(lh);
  752. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  753. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  754. lh->lh_header.__pad0 = cpu_to_be64(0);
  755. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  756. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  757. lh->lh_sequence = cpu_to_be64(seq);
  758. lh->lh_flags = cpu_to_be32(flags);
  759. lh->lh_tail = cpu_to_be32(tail);
  760. lh->lh_blkno = cpu_to_be32(lblock);
  761. hash = ~crc32(~0, lh, LH_V1_SIZE);
  762. lh->lh_hash = cpu_to_be32(hash);
  763. ktime_get_coarse_real_ts64(&tv);
  764. lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
  765. lh->lh_sec = cpu_to_be64(tv.tv_sec);
  766. if (!list_empty(&jd->extent_list))
  767. dblock = gfs2_log_bmap(jd, lblock);
  768. else {
  769. unsigned int extlen;
  770. int ret;
  771. extlen = 1;
  772. ret = gfs2_get_extent(jd->jd_inode, lblock, &dblock, &extlen);
  773. if (gfs2_assert_withdraw(sdp, ret == 0))
  774. return;
  775. }
  776. lh->lh_addr = cpu_to_be64(dblock);
  777. lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
  778. /* We may only write local statfs, quota, etc., when writing to our
  779. own journal. The values are left 0 when recovering a journal
  780. different from our own. */
  781. if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
  782. lh->lh_statfs_addr =
  783. cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
  784. lh->lh_quota_addr =
  785. cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
  786. spin_lock(&sdp->sd_statfs_spin);
  787. lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
  788. lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
  789. lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
  790. spin_unlock(&sdp->sd_statfs_spin);
  791. }
  792. BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
  793. crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
  794. sb->s_blocksize - LH_V1_SIZE - 4);
  795. lh->lh_crc = cpu_to_be32(crc);
  796. gfs2_log_write(sdp, jd, page, sb->s_blocksize, 0, dblock);
  797. gfs2_log_submit_bio(&jd->jd_log_bio, REQ_OP_WRITE | op_flags);
  798. }
  799. /**
  800. * log_write_header - Get and initialize a journal header buffer
  801. * @sdp: The GFS2 superblock
  802. * @flags: The log header flags, including log header origin
  803. *
  804. * Returns: the initialized log buffer descriptor
  805. */
  806. static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
  807. {
  808. blk_opf_t op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
  809. struct super_block *sb = sdp->sd_vfs;
  810. gfs2_assert_withdraw(sdp, sb->s_writers.frozen != SB_FREEZE_COMPLETE);
  811. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
  812. gfs2_ordered_wait(sdp);
  813. log_flush_wait(sdp);
  814. op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
  815. }
  816. sdp->sd_log_idle = (sdp->sd_log_flush_tail == sdp->sd_log_flush_head);
  817. gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++,
  818. sdp->sd_log_flush_tail, sdp->sd_log_flush_head,
  819. flags, op_flags);
  820. gfs2_log_incr_head(sdp);
  821. log_flush_wait(sdp);
  822. log_pull_tail(sdp);
  823. gfs2_log_update_head(sdp);
  824. }
  825. /**
  826. * gfs2_ail_drain - drain the ail lists after a withdraw
  827. * @sdp: Pointer to GFS2 superblock
  828. */
  829. void gfs2_ail_drain(struct gfs2_sbd *sdp)
  830. {
  831. struct gfs2_trans *tr;
  832. spin_lock(&sdp->sd_ail_lock);
  833. /*
  834. * For transactions on the sd_ail1_list we need to drain both the
  835. * ail1 and ail2 lists. That's because function gfs2_ail1_start_one
  836. * (temporarily) moves items from its tr_ail1 list to tr_ail2 list
  837. * before revokes are sent for that block. Items on the sd_ail2_list
  838. * should have already gotten beyond that point, so no need.
  839. */
  840. while (!list_empty(&sdp->sd_ail1_list)) {
  841. tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans,
  842. tr_list);
  843. gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
  844. gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
  845. list_del(&tr->tr_list);
  846. gfs2_trans_free(sdp, tr);
  847. }
  848. while (!list_empty(&sdp->sd_ail2_list)) {
  849. tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
  850. tr_list);
  851. gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
  852. list_del(&tr->tr_list);
  853. gfs2_trans_free(sdp, tr);
  854. }
  855. gfs2_drain_revokes(sdp);
  856. spin_unlock(&sdp->sd_ail_lock);
  857. }
  858. /**
  859. * empty_ail1_list - try to start IO and empty the ail1 list
  860. * @sdp: Pointer to GFS2 superblock
  861. */
  862. static void empty_ail1_list(struct gfs2_sbd *sdp)
  863. {
  864. unsigned long start = jiffies;
  865. bool empty = false;
  866. while (!empty) {
  867. if (time_after(jiffies, start + (HZ * 600))) {
  868. fs_err(sdp, "Error: In %s for 10 minutes! t=%d\n",
  869. __func__, current->journal_info ? 1 : 0);
  870. dump_ail_list(sdp);
  871. return;
  872. }
  873. gfs2_ail1_start(sdp);
  874. gfs2_ail1_wait(sdp);
  875. empty = gfs2_ail1_empty(sdp, 0);
  876. if (gfs2_withdrawing_or_withdrawn(sdp))
  877. break;
  878. }
  879. if (gfs2_withdrawing(sdp))
  880. gfs2_withdraw(sdp);
  881. }
  882. /**
  883. * trans_drain - drain the buf and databuf queue for a failed transaction
  884. * @tr: the transaction to drain
  885. *
  886. * When this is called, we're taking an error exit for a log write that failed
  887. * but since we bypassed the after_commit functions, we need to remove the
  888. * items from the buf and databuf queue.
  889. */
  890. static void trans_drain(struct gfs2_trans *tr)
  891. {
  892. struct gfs2_bufdata *bd;
  893. struct list_head *head;
  894. if (!tr)
  895. return;
  896. head = &tr->tr_buf;
  897. while (!list_empty(head)) {
  898. bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
  899. list_del_init(&bd->bd_list);
  900. if (!list_empty(&bd->bd_ail_st_list))
  901. gfs2_remove_from_ail(bd);
  902. kmem_cache_free(gfs2_bufdata_cachep, bd);
  903. }
  904. head = &tr->tr_databuf;
  905. while (!list_empty(head)) {
  906. bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
  907. list_del_init(&bd->bd_list);
  908. if (!list_empty(&bd->bd_ail_st_list))
  909. gfs2_remove_from_ail(bd);
  910. kmem_cache_free(gfs2_bufdata_cachep, bd);
  911. }
  912. }
  913. /**
  914. * gfs2_log_flush - flush incore transaction(s)
  915. * @sdp: The filesystem
  916. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  917. * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
  918. *
  919. */
  920. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
  921. {
  922. struct gfs2_trans *tr = NULL;
  923. unsigned int reserved_blocks = 0, used_blocks = 0;
  924. bool frozen = test_bit(SDF_FROZEN, &sdp->sd_flags);
  925. unsigned int first_log_head;
  926. unsigned int reserved_revokes = 0;
  927. down_write(&sdp->sd_log_flush_lock);
  928. trace_gfs2_log_flush(sdp, 1, flags);
  929. repeat:
  930. /*
  931. * Do this check while holding the log_flush_lock to prevent new
  932. * buffers from being added to the ail via gfs2_pin()
  933. */
  934. if (gfs2_withdrawing_or_withdrawn(sdp) ||
  935. !test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  936. goto out;
  937. /* Log might have been flushed while we waited for the flush lock */
  938. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags))
  939. goto out;
  940. first_log_head = sdp->sd_log_head;
  941. sdp->sd_log_flush_head = first_log_head;
  942. tr = sdp->sd_log_tr;
  943. if (tr || sdp->sd_log_num_revoke) {
  944. if (reserved_blocks)
  945. gfs2_log_release(sdp, reserved_blocks);
  946. reserved_blocks = sdp->sd_log_blks_reserved;
  947. reserved_revokes = sdp->sd_log_num_revoke;
  948. if (tr) {
  949. sdp->sd_log_tr = NULL;
  950. tr->tr_first = first_log_head;
  951. if (unlikely(frozen)) {
  952. if (gfs2_assert_withdraw_delayed(sdp,
  953. !tr->tr_num_buf_new && !tr->tr_num_databuf_new))
  954. goto out_withdraw;
  955. }
  956. }
  957. } else if (!reserved_blocks) {
  958. unsigned int taboo_blocks = GFS2_LOG_FLUSH_MIN_BLOCKS;
  959. reserved_blocks = GFS2_LOG_FLUSH_MIN_BLOCKS;
  960. if (current == sdp->sd_logd_process)
  961. taboo_blocks = 0;
  962. if (!__gfs2_log_try_reserve(sdp, reserved_blocks, taboo_blocks)) {
  963. up_write(&sdp->sd_log_flush_lock);
  964. __gfs2_log_reserve(sdp, reserved_blocks, taboo_blocks);
  965. down_write(&sdp->sd_log_flush_lock);
  966. goto repeat;
  967. }
  968. BUG_ON(sdp->sd_log_num_revoke);
  969. }
  970. if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
  971. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  972. if (unlikely(frozen))
  973. if (gfs2_assert_withdraw_delayed(sdp, !reserved_revokes))
  974. goto out_withdraw;
  975. gfs2_ordered_write(sdp);
  976. if (gfs2_withdrawing_or_withdrawn(sdp))
  977. goto out_withdraw;
  978. lops_before_commit(sdp, tr);
  979. if (gfs2_withdrawing_or_withdrawn(sdp))
  980. goto out_withdraw;
  981. if (sdp->sd_jdesc)
  982. gfs2_log_submit_bio(&sdp->sd_jdesc->jd_log_bio, REQ_OP_WRITE);
  983. if (gfs2_withdrawing_or_withdrawn(sdp))
  984. goto out_withdraw;
  985. if (sdp->sd_log_head != sdp->sd_log_flush_head) {
  986. log_write_header(sdp, flags);
  987. } else if (sdp->sd_log_tail != sdp->sd_log_flush_tail && !sdp->sd_log_idle) {
  988. log_write_header(sdp, flags);
  989. }
  990. if (gfs2_withdrawing_or_withdrawn(sdp))
  991. goto out_withdraw;
  992. lops_after_commit(sdp, tr);
  993. gfs2_log_lock(sdp);
  994. sdp->sd_log_blks_reserved = 0;
  995. spin_lock(&sdp->sd_ail_lock);
  996. if (tr && !list_empty(&tr->tr_ail1_list)) {
  997. list_add(&tr->tr_list, &sdp->sd_ail1_list);
  998. tr = NULL;
  999. }
  1000. spin_unlock(&sdp->sd_ail_lock);
  1001. gfs2_log_unlock(sdp);
  1002. if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
  1003. if (!sdp->sd_log_idle) {
  1004. empty_ail1_list(sdp);
  1005. if (gfs2_withdrawing_or_withdrawn(sdp))
  1006. goto out_withdraw;
  1007. log_write_header(sdp, flags);
  1008. }
  1009. if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
  1010. GFS2_LOG_HEAD_FLUSH_FREEZE))
  1011. gfs2_log_shutdown(sdp);
  1012. }
  1013. out_end:
  1014. used_blocks = log_distance(sdp, sdp->sd_log_flush_head, first_log_head);
  1015. reserved_revokes += atomic_read(&sdp->sd_log_revokes_available);
  1016. atomic_set(&sdp->sd_log_revokes_available, sdp->sd_ldptrs);
  1017. gfs2_assert_withdraw(sdp, reserved_revokes % sdp->sd_inptrs == sdp->sd_ldptrs);
  1018. if (reserved_revokes > sdp->sd_ldptrs)
  1019. reserved_blocks += (reserved_revokes - sdp->sd_ldptrs) / sdp->sd_inptrs;
  1020. out:
  1021. if (used_blocks != reserved_blocks) {
  1022. gfs2_assert_withdraw_delayed(sdp, used_blocks < reserved_blocks);
  1023. gfs2_log_release(sdp, reserved_blocks - used_blocks);
  1024. }
  1025. up_write(&sdp->sd_log_flush_lock);
  1026. gfs2_trans_free(sdp, tr);
  1027. if (gfs2_withdrawing(sdp))
  1028. gfs2_withdraw(sdp);
  1029. trace_gfs2_log_flush(sdp, 0, flags);
  1030. return;
  1031. out_withdraw:
  1032. trans_drain(tr);
  1033. /**
  1034. * If the tr_list is empty, we're withdrawing during a log
  1035. * flush that targets a transaction, but the transaction was
  1036. * never queued onto any of the ail lists. Here we add it to
  1037. * ail1 just so that ail_drain() will find and free it.
  1038. */
  1039. spin_lock(&sdp->sd_ail_lock);
  1040. if (tr && list_empty(&tr->tr_list))
  1041. list_add(&tr->tr_list, &sdp->sd_ail1_list);
  1042. spin_unlock(&sdp->sd_ail_lock);
  1043. tr = NULL;
  1044. goto out_end;
  1045. }
  1046. /**
  1047. * gfs2_merge_trans - Merge a new transaction into a cached transaction
  1048. * @sdp: the filesystem
  1049. * @new: New transaction to be merged
  1050. */
  1051. static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new)
  1052. {
  1053. struct gfs2_trans *old = sdp->sd_log_tr;
  1054. WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
  1055. old->tr_num_buf_new += new->tr_num_buf_new;
  1056. old->tr_num_databuf_new += new->tr_num_databuf_new;
  1057. old->tr_num_buf_rm += new->tr_num_buf_rm;
  1058. old->tr_num_databuf_rm += new->tr_num_databuf_rm;
  1059. old->tr_revokes += new->tr_revokes;
  1060. old->tr_num_revoke += new->tr_num_revoke;
  1061. list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
  1062. list_splice_tail_init(&new->tr_buf, &old->tr_buf);
  1063. spin_lock(&sdp->sd_ail_lock);
  1064. list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list);
  1065. list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list);
  1066. spin_unlock(&sdp->sd_ail_lock);
  1067. }
  1068. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  1069. {
  1070. unsigned int reserved;
  1071. unsigned int unused;
  1072. unsigned int maxres;
  1073. gfs2_log_lock(sdp);
  1074. if (sdp->sd_log_tr) {
  1075. gfs2_merge_trans(sdp, tr);
  1076. } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
  1077. gfs2_assert_withdraw(sdp, !test_bit(TR_ONSTACK, &tr->tr_flags));
  1078. sdp->sd_log_tr = tr;
  1079. set_bit(TR_ATTACHED, &tr->tr_flags);
  1080. }
  1081. reserved = calc_reserved(sdp);
  1082. maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
  1083. gfs2_assert_withdraw(sdp, maxres >= reserved);
  1084. unused = maxres - reserved;
  1085. if (unused)
  1086. gfs2_log_release(sdp, unused);
  1087. sdp->sd_log_blks_reserved = reserved;
  1088. gfs2_log_unlock(sdp);
  1089. }
  1090. static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
  1091. {
  1092. return atomic_read(&sdp->sd_log_pinned) +
  1093. atomic_read(&sdp->sd_log_blks_needed) >=
  1094. atomic_read(&sdp->sd_log_thresh1);
  1095. }
  1096. static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
  1097. {
  1098. return sdp->sd_jdesc->jd_blocks -
  1099. atomic_read(&sdp->sd_log_blks_free) +
  1100. atomic_read(&sdp->sd_log_blks_needed) >=
  1101. atomic_read(&sdp->sd_log_thresh2);
  1102. }
  1103. /**
  1104. * gfs2_log_commit - Commit a transaction to the log
  1105. * @sdp: the filesystem
  1106. * @tr: the transaction
  1107. *
  1108. * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
  1109. * or the total number of used blocks (pinned blocks plus AIL blocks)
  1110. * is greater than thresh2.
  1111. *
  1112. * At mount time thresh1 is 2/5ths of journal size, thresh2 is 4/5ths of
  1113. * journal size.
  1114. *
  1115. * Returns: errno
  1116. */
  1117. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  1118. {
  1119. log_refund(sdp, tr);
  1120. if (gfs2_ail_flush_reqd(sdp) || gfs2_jrnl_flush_reqd(sdp))
  1121. wake_up(&sdp->sd_logd_waitq);
  1122. }
  1123. /**
  1124. * gfs2_log_shutdown - write a shutdown header into a journal
  1125. * @sdp: the filesystem
  1126. *
  1127. */
  1128. static void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  1129. {
  1130. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  1131. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  1132. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  1133. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
  1134. log_pull_tail(sdp);
  1135. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  1136. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  1137. }
  1138. /**
  1139. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  1140. * @data: Pointer to GFS2 superblock
  1141. *
  1142. * Also, periodically check to make sure that we're using the most recent
  1143. * journal index.
  1144. */
  1145. int gfs2_logd(void *data)
  1146. {
  1147. struct gfs2_sbd *sdp = data;
  1148. unsigned long t = 1;
  1149. set_freezable();
  1150. while (!kthread_should_stop()) {
  1151. if (gfs2_withdrawing_or_withdrawn(sdp))
  1152. break;
  1153. /* Check for errors writing to the journal */
  1154. if (sdp->sd_log_error) {
  1155. gfs2_lm(sdp,
  1156. "GFS2: fsid=%s: error %d: "
  1157. "withdrawing the file system to "
  1158. "prevent further damage.\n",
  1159. sdp->sd_fsname, sdp->sd_log_error);
  1160. gfs2_withdraw(sdp);
  1161. break;
  1162. }
  1163. if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
  1164. gfs2_ail1_empty(sdp, 0);
  1165. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  1166. GFS2_LFC_LOGD_JFLUSH_REQD);
  1167. }
  1168. if (test_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags) ||
  1169. gfs2_ail_flush_reqd(sdp)) {
  1170. clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
  1171. gfs2_ail1_start(sdp);
  1172. gfs2_ail1_wait(sdp);
  1173. gfs2_ail1_empty(sdp, 0);
  1174. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  1175. GFS2_LFC_LOGD_AIL_FLUSH_REQD);
  1176. }
  1177. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  1178. t = wait_event_freezable_timeout(sdp->sd_logd_waitq,
  1179. test_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags) ||
  1180. gfs2_ail_flush_reqd(sdp) ||
  1181. gfs2_jrnl_flush_reqd(sdp) ||
  1182. sdp->sd_log_error ||
  1183. gfs2_withdrawing_or_withdrawn(sdp) ||
  1184. kthread_should_stop(),
  1185. t);
  1186. }
  1187. if (gfs2_withdrawing(sdp))
  1188. gfs2_withdraw(sdp);
  1189. return 0;
  1190. }