log.c 35 KB

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