log.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include <linux/delay.h>
  17. #include <linux/kthread.h>
  18. #include <linux/freezer.h>
  19. #include <linux/bio.h>
  20. #include <linux/writeback.h>
  21. #include <linux/list_sort.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "bmap.h"
  25. #include "glock.h"
  26. #include "log.h"
  27. #include "lops.h"
  28. #include "meta_io.h"
  29. #include "util.h"
  30. #include "dir.h"
  31. #include "trace_gfs2.h"
  32. #define PULL 1
  33. /**
  34. * gfs2_struct2blk - compute stuff
  35. * @sdp: the filesystem
  36. * @nstruct: the number of structures
  37. * @ssize: the size of the 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. unsigned int ssize)
  46. {
  47. unsigned int blks;
  48. unsigned int first, second;
  49. blks = 1;
  50. first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
  51. if (nstruct > first) {
  52. second = (sdp->sd_sb.sb_bsize -
  53. sizeof(struct gfs2_meta_header)) / ssize;
  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. * @mapping: The associated mapping (maybe NULL)
  61. * @bd: The gfs2_bufdata to remove
  62. *
  63. * The ail lock _must_ be held when calling this function
  64. *
  65. */
  66. void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
  67. {
  68. bd->bd_ail = NULL;
  69. list_del_init(&bd->bd_ail_st_list);
  70. list_del_init(&bd->bd_ail_gl_list);
  71. atomic_dec(&bd->bd_gl->gl_ail_count);
  72. brelse(bd->bd_bh);
  73. }
  74. /**
  75. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  76. * @sdp: the filesystem
  77. * @wbc: The writeback control structure
  78. * @ai: The ail structure
  79. *
  80. */
  81. static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
  82. struct writeback_control *wbc,
  83. struct gfs2_ail *ai)
  84. __releases(&sdp->sd_ail_lock)
  85. __acquires(&sdp->sd_ail_lock)
  86. {
  87. struct gfs2_glock *gl = NULL;
  88. struct address_space *mapping;
  89. struct gfs2_bufdata *bd, *s;
  90. struct buffer_head *bh;
  91. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, bd_ail_st_list) {
  92. bh = bd->bd_bh;
  93. gfs2_assert(sdp, bd->bd_ail == ai);
  94. if (!buffer_busy(bh)) {
  95. if (!buffer_uptodate(bh))
  96. gfs2_io_error_bh(sdp, bh);
  97. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  98. continue;
  99. }
  100. if (!buffer_dirty(bh))
  101. continue;
  102. if (gl == bd->bd_gl)
  103. continue;
  104. gl = bd->bd_gl;
  105. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  106. mapping = bh->b_page->mapping;
  107. if (!mapping)
  108. continue;
  109. spin_unlock(&sdp->sd_ail_lock);
  110. generic_writepages(mapping, wbc);
  111. spin_lock(&sdp->sd_ail_lock);
  112. if (wbc->nr_to_write <= 0)
  113. break;
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. /**
  119. * gfs2_ail1_flush - start writeback of some ail1 entries
  120. * @sdp: The super block
  121. * @wbc: The writeback control structure
  122. *
  123. * Writes back some ail1 entries, according to the limits in the
  124. * writeback control structure
  125. */
  126. void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
  127. {
  128. struct list_head *head = &sdp->sd_ail1_list;
  129. struct gfs2_ail *ai;
  130. trace_gfs2_ail_flush(sdp, wbc, 1);
  131. spin_lock(&sdp->sd_ail_lock);
  132. restart:
  133. list_for_each_entry_reverse(ai, head, ai_list) {
  134. if (wbc->nr_to_write <= 0)
  135. break;
  136. if (gfs2_ail1_start_one(sdp, wbc, ai))
  137. goto restart;
  138. }
  139. spin_unlock(&sdp->sd_ail_lock);
  140. trace_gfs2_ail_flush(sdp, wbc, 0);
  141. }
  142. /**
  143. * gfs2_ail1_start - start writeback of all ail1 entries
  144. * @sdp: The superblock
  145. */
  146. static void gfs2_ail1_start(struct gfs2_sbd *sdp)
  147. {
  148. struct writeback_control wbc = {
  149. .sync_mode = WB_SYNC_NONE,
  150. .nr_to_write = LONG_MAX,
  151. .range_start = 0,
  152. .range_end = LLONG_MAX,
  153. };
  154. return gfs2_ail1_flush(sdp, &wbc);
  155. }
  156. /**
  157. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  158. * @sdp: the filesystem
  159. * @ai: the AIL entry
  160. *
  161. */
  162. static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  163. {
  164. struct gfs2_bufdata *bd, *s;
  165. struct buffer_head *bh;
  166. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  167. bd_ail_st_list) {
  168. bh = bd->bd_bh;
  169. gfs2_assert(sdp, bd->bd_ail == ai);
  170. if (buffer_busy(bh))
  171. continue;
  172. if (!buffer_uptodate(bh))
  173. gfs2_io_error_bh(sdp, bh);
  174. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  175. }
  176. }
  177. /**
  178. * gfs2_ail1_empty - Try to empty the ail1 lists
  179. * @sdp: The superblock
  180. *
  181. * Tries to empty the ail1 lists, starting with the oldest first
  182. */
  183. static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
  184. {
  185. struct gfs2_ail *ai, *s;
  186. int ret;
  187. spin_lock(&sdp->sd_ail_lock);
  188. list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
  189. gfs2_ail1_empty_one(sdp, ai);
  190. if (list_empty(&ai->ai_ail1_list))
  191. list_move(&ai->ai_list, &sdp->sd_ail2_list);
  192. else
  193. break;
  194. }
  195. ret = list_empty(&sdp->sd_ail1_list);
  196. spin_unlock(&sdp->sd_ail_lock);
  197. return ret;
  198. }
  199. static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
  200. {
  201. struct gfs2_ail *ai;
  202. struct gfs2_bufdata *bd;
  203. struct buffer_head *bh;
  204. spin_lock(&sdp->sd_ail_lock);
  205. list_for_each_entry_reverse(ai, &sdp->sd_ail1_list, ai_list) {
  206. list_for_each_entry(bd, &ai->ai_ail1_list, bd_ail_st_list) {
  207. bh = bd->bd_bh;
  208. if (!buffer_locked(bh))
  209. continue;
  210. get_bh(bh);
  211. spin_unlock(&sdp->sd_ail_lock);
  212. wait_on_buffer(bh);
  213. brelse(bh);
  214. return;
  215. }
  216. }
  217. spin_unlock(&sdp->sd_ail_lock);
  218. }
  219. /**
  220. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  221. * @sdp: the filesystem
  222. * @ai: the AIL entry
  223. *
  224. */
  225. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  226. {
  227. struct list_head *head = &ai->ai_ail2_list;
  228. struct gfs2_bufdata *bd;
  229. while (!list_empty(head)) {
  230. bd = list_entry(head->prev, struct gfs2_bufdata,
  231. bd_ail_st_list);
  232. gfs2_assert(sdp, bd->bd_ail == ai);
  233. gfs2_remove_from_ail(bd);
  234. }
  235. }
  236. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  237. {
  238. struct gfs2_ail *ai, *safe;
  239. unsigned int old_tail = sdp->sd_log_tail;
  240. int wrap = (new_tail < old_tail);
  241. int a, b, rm;
  242. spin_lock(&sdp->sd_ail_lock);
  243. list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
  244. a = (old_tail <= ai->ai_first);
  245. b = (ai->ai_first < new_tail);
  246. rm = (wrap) ? (a || b) : (a && b);
  247. if (!rm)
  248. continue;
  249. gfs2_ail2_empty_one(sdp, ai);
  250. list_del(&ai->ai_list);
  251. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
  252. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
  253. kfree(ai);
  254. }
  255. spin_unlock(&sdp->sd_ail_lock);
  256. }
  257. /**
  258. * gfs2_log_reserve - Make a log reservation
  259. * @sdp: The GFS2 superblock
  260. * @blks: The number of blocks to reserve
  261. *
  262. * Note that we never give out the last few blocks of the journal. Thats
  263. * due to the fact that there is a small number of header blocks
  264. * associated with each log flush. The exact number can't be known until
  265. * flush time, so we ensure that we have just enough free blocks at all
  266. * times to avoid running out during a log flush.
  267. *
  268. * We no longer flush the log here, instead we wake up logd to do that
  269. * for us. To avoid the thundering herd and to ensure that we deal fairly
  270. * with queued waiters, we use an exclusive wait. This means that when we
  271. * get woken with enough journal space to get our reservation, we need to
  272. * wake the next waiter on the list.
  273. *
  274. * Returns: errno
  275. */
  276. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  277. {
  278. unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
  279. unsigned wanted = blks + reserved_blks;
  280. DEFINE_WAIT(wait);
  281. int did_wait = 0;
  282. unsigned int free_blocks;
  283. if (gfs2_assert_warn(sdp, blks) ||
  284. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  285. return -EINVAL;
  286. retry:
  287. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  288. if (unlikely(free_blocks <= wanted)) {
  289. do {
  290. prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
  291. TASK_UNINTERRUPTIBLE);
  292. wake_up(&sdp->sd_logd_waitq);
  293. did_wait = 1;
  294. if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
  295. io_schedule();
  296. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  297. } while(free_blocks <= wanted);
  298. finish_wait(&sdp->sd_log_waitq, &wait);
  299. }
  300. if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
  301. free_blocks - blks) != free_blocks)
  302. goto retry;
  303. trace_gfs2_log_blocks(sdp, -blks);
  304. /*
  305. * If we waited, then so might others, wake them up _after_ we get
  306. * our share of the log.
  307. */
  308. if (unlikely(did_wait))
  309. wake_up(&sdp->sd_log_waitq);
  310. down_read(&sdp->sd_log_flush_lock);
  311. return 0;
  312. }
  313. u64 gfs2_log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
  314. {
  315. struct gfs2_journal_extent *je;
  316. list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
  317. if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
  318. return je->dblock + lbn - je->lblock;
  319. }
  320. return -1;
  321. }
  322. /**
  323. * log_distance - Compute distance between two journal blocks
  324. * @sdp: The GFS2 superblock
  325. * @newer: The most recent journal block of the pair
  326. * @older: The older journal block of the pair
  327. *
  328. * Compute the distance (in the journal direction) between two
  329. * blocks in the journal
  330. *
  331. * Returns: the distance in blocks
  332. */
  333. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  334. unsigned int older)
  335. {
  336. int dist;
  337. dist = newer - older;
  338. if (dist < 0)
  339. dist += sdp->sd_jdesc->jd_blocks;
  340. return dist;
  341. }
  342. /**
  343. * calc_reserved - Calculate the number of blocks to reserve when
  344. * refunding a transaction's unused buffers.
  345. * @sdp: The GFS2 superblock
  346. *
  347. * This is complex. We need to reserve room for all our currently used
  348. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  349. * all our journaled data buffers for journaled files (e.g. files in the
  350. * meta_fs like rindex, or files for which chattr +j was done.)
  351. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  352. * will count it as free space (sd_log_blks_free) and corruption will follow.
  353. *
  354. * We can have metadata bufs and jdata bufs in the same journal. So each
  355. * type gets its own log header, for which we need to reserve a block.
  356. * In fact, each type has the potential for needing more than one header
  357. * in cases where we have more buffers than will fit on a journal page.
  358. * Metadata journal entries take up half the space of journaled buffer entries.
  359. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  360. * databuf_limit (251) before they cause a wrap around.
  361. *
  362. * Also, we need to reserve blocks for revoke journal entries and one for an
  363. * overall header for the lot.
  364. *
  365. * Returns: the number of blocks reserved
  366. */
  367. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  368. {
  369. unsigned int reserved = 0;
  370. unsigned int mbuf_limit, metabufhdrs_needed;
  371. unsigned int dbuf_limit, databufhdrs_needed;
  372. unsigned int revokes = 0;
  373. mbuf_limit = buf_limit(sdp);
  374. metabufhdrs_needed = (sdp->sd_log_commited_buf +
  375. (mbuf_limit - 1)) / mbuf_limit;
  376. dbuf_limit = databuf_limit(sdp);
  377. databufhdrs_needed = (sdp->sd_log_commited_databuf +
  378. (dbuf_limit - 1)) / dbuf_limit;
  379. if (sdp->sd_log_commited_revoke > 0)
  380. revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  381. sizeof(u64));
  382. reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
  383. sdp->sd_log_commited_databuf + databufhdrs_needed +
  384. revokes;
  385. /* One for the overall header */
  386. if (reserved)
  387. reserved++;
  388. return reserved;
  389. }
  390. static unsigned int current_tail(struct gfs2_sbd *sdp)
  391. {
  392. struct gfs2_ail *ai;
  393. unsigned int tail;
  394. spin_lock(&sdp->sd_ail_lock);
  395. if (list_empty(&sdp->sd_ail1_list)) {
  396. tail = sdp->sd_log_head;
  397. } else {
  398. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  399. tail = ai->ai_first;
  400. }
  401. spin_unlock(&sdp->sd_ail_lock);
  402. return tail;
  403. }
  404. void gfs2_log_incr_head(struct gfs2_sbd *sdp)
  405. {
  406. BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) &&
  407. (sdp->sd_log_flush_head != sdp->sd_log_head));
  408. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  409. sdp->sd_log_flush_head = 0;
  410. sdp->sd_log_flush_wrapped = 1;
  411. }
  412. }
  413. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  414. {
  415. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  416. ail2_empty(sdp, new_tail);
  417. atomic_add(dist, &sdp->sd_log_blks_free);
  418. trace_gfs2_log_blocks(sdp, dist);
  419. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  420. sdp->sd_jdesc->jd_blocks);
  421. sdp->sd_log_tail = new_tail;
  422. }
  423. static void log_flush_wait(struct gfs2_sbd *sdp)
  424. {
  425. DEFINE_WAIT(wait);
  426. if (atomic_read(&sdp->sd_log_in_flight)) {
  427. do {
  428. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  429. TASK_UNINTERRUPTIBLE);
  430. if (atomic_read(&sdp->sd_log_in_flight))
  431. io_schedule();
  432. } while(atomic_read(&sdp->sd_log_in_flight));
  433. finish_wait(&sdp->sd_log_flush_wait, &wait);
  434. }
  435. }
  436. static int bd_cmp(void *priv, struct list_head *a, struct list_head *b)
  437. {
  438. struct gfs2_bufdata *bda, *bdb;
  439. bda = list_entry(a, struct gfs2_bufdata, bd_le.le_list);
  440. bdb = list_entry(b, struct gfs2_bufdata, bd_le.le_list);
  441. if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr)
  442. return -1;
  443. if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr)
  444. return 1;
  445. return 0;
  446. }
  447. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  448. {
  449. struct gfs2_bufdata *bd;
  450. struct buffer_head *bh;
  451. LIST_HEAD(written);
  452. gfs2_log_lock(sdp);
  453. list_sort(NULL, &sdp->sd_log_le_ordered, &bd_cmp);
  454. while (!list_empty(&sdp->sd_log_le_ordered)) {
  455. bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
  456. list_move(&bd->bd_le.le_list, &written);
  457. bh = bd->bd_bh;
  458. if (!buffer_dirty(bh))
  459. continue;
  460. get_bh(bh);
  461. gfs2_log_unlock(sdp);
  462. lock_buffer(bh);
  463. if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
  464. bh->b_end_io = end_buffer_write_sync;
  465. submit_bh(WRITE_SYNC, bh);
  466. } else {
  467. unlock_buffer(bh);
  468. brelse(bh);
  469. }
  470. gfs2_log_lock(sdp);
  471. }
  472. list_splice(&written, &sdp->sd_log_le_ordered);
  473. gfs2_log_unlock(sdp);
  474. }
  475. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  476. {
  477. struct gfs2_bufdata *bd;
  478. struct buffer_head *bh;
  479. gfs2_log_lock(sdp);
  480. while (!list_empty(&sdp->sd_log_le_ordered)) {
  481. bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
  482. bh = bd->bd_bh;
  483. if (buffer_locked(bh)) {
  484. get_bh(bh);
  485. gfs2_log_unlock(sdp);
  486. wait_on_buffer(bh);
  487. brelse(bh);
  488. gfs2_log_lock(sdp);
  489. continue;
  490. }
  491. list_del_init(&bd->bd_le.le_list);
  492. }
  493. gfs2_log_unlock(sdp);
  494. }
  495. /**
  496. * log_write_header - Get and initialize a journal header buffer
  497. * @sdp: The GFS2 superblock
  498. *
  499. * Returns: the initialized log buffer descriptor
  500. */
  501. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  502. {
  503. u64 blkno = gfs2_log_bmap(sdp, sdp->sd_log_flush_head);
  504. struct buffer_head *bh;
  505. struct gfs2_log_header *lh;
  506. unsigned int tail;
  507. u32 hash;
  508. bh = sb_getblk(sdp->sd_vfs, blkno);
  509. lock_buffer(bh);
  510. memset(bh->b_data, 0, bh->b_size);
  511. set_buffer_uptodate(bh);
  512. clear_buffer_dirty(bh);
  513. gfs2_ail1_empty(sdp);
  514. tail = current_tail(sdp);
  515. lh = (struct gfs2_log_header *)bh->b_data;
  516. memset(lh, 0, sizeof(struct gfs2_log_header));
  517. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  518. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  519. lh->lh_header.__pad0 = cpu_to_be64(0);
  520. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  521. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  522. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  523. lh->lh_flags = cpu_to_be32(flags);
  524. lh->lh_tail = cpu_to_be32(tail);
  525. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  526. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  527. lh->lh_hash = cpu_to_be32(hash);
  528. bh->b_end_io = end_buffer_write_sync;
  529. get_bh(bh);
  530. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
  531. gfs2_ordered_wait(sdp);
  532. log_flush_wait(sdp);
  533. submit_bh(WRITE_SYNC | REQ_META | REQ_PRIO, bh);
  534. } else {
  535. submit_bh(WRITE_FLUSH_FUA | REQ_META, bh);
  536. }
  537. wait_on_buffer(bh);
  538. if (!buffer_uptodate(bh))
  539. gfs2_io_error_bh(sdp, bh);
  540. brelse(bh);
  541. if (sdp->sd_log_tail != tail)
  542. log_pull_tail(sdp, tail);
  543. else
  544. gfs2_assert_withdraw(sdp, !pull);
  545. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  546. gfs2_log_incr_head(sdp);
  547. }
  548. /**
  549. * gfs2_log_flush - flush incore transaction(s)
  550. * @sdp: the filesystem
  551. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  552. *
  553. */
  554. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  555. {
  556. struct gfs2_ail *ai;
  557. down_write(&sdp->sd_log_flush_lock);
  558. /* Log might have been flushed while we waited for the flush lock */
  559. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  560. up_write(&sdp->sd_log_flush_lock);
  561. return;
  562. }
  563. trace_gfs2_log_flush(sdp, 1);
  564. ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
  565. INIT_LIST_HEAD(&ai->ai_ail1_list);
  566. INIT_LIST_HEAD(&ai->ai_ail2_list);
  567. if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
  568. printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
  569. sdp->sd_log_commited_buf);
  570. gfs2_assert_withdraw(sdp, 0);
  571. }
  572. if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
  573. printk(KERN_INFO "GFS2: log databuf %u %u\n",
  574. sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
  575. gfs2_assert_withdraw(sdp, 0);
  576. }
  577. gfs2_assert_withdraw(sdp,
  578. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  579. sdp->sd_log_flush_head = sdp->sd_log_head;
  580. sdp->sd_log_flush_wrapped = 0;
  581. ai->ai_first = sdp->sd_log_flush_head;
  582. gfs2_ordered_write(sdp);
  583. lops_before_commit(sdp);
  584. if (sdp->sd_log_head != sdp->sd_log_flush_head) {
  585. log_write_header(sdp, 0, 0);
  586. } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  587. gfs2_log_lock(sdp);
  588. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  589. trace_gfs2_log_blocks(sdp, -1);
  590. gfs2_log_unlock(sdp);
  591. log_write_header(sdp, 0, PULL);
  592. }
  593. lops_after_commit(sdp, ai);
  594. gfs2_log_lock(sdp);
  595. sdp->sd_log_head = sdp->sd_log_flush_head;
  596. sdp->sd_log_blks_reserved = 0;
  597. sdp->sd_log_commited_buf = 0;
  598. sdp->sd_log_commited_databuf = 0;
  599. sdp->sd_log_commited_revoke = 0;
  600. spin_lock(&sdp->sd_ail_lock);
  601. if (!list_empty(&ai->ai_ail1_list)) {
  602. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  603. ai = NULL;
  604. }
  605. spin_unlock(&sdp->sd_ail_lock);
  606. gfs2_log_unlock(sdp);
  607. trace_gfs2_log_flush(sdp, 0);
  608. up_write(&sdp->sd_log_flush_lock);
  609. kfree(ai);
  610. }
  611. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  612. {
  613. unsigned int reserved;
  614. unsigned int unused;
  615. gfs2_log_lock(sdp);
  616. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  617. sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
  618. tr->tr_num_databuf_rm;
  619. gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
  620. (((int)sdp->sd_log_commited_databuf) >= 0));
  621. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  622. reserved = calc_reserved(sdp);
  623. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
  624. unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
  625. atomic_add(unused, &sdp->sd_log_blks_free);
  626. trace_gfs2_log_blocks(sdp, unused);
  627. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  628. sdp->sd_jdesc->jd_blocks);
  629. sdp->sd_log_blks_reserved = reserved;
  630. gfs2_log_unlock(sdp);
  631. }
  632. static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  633. {
  634. struct list_head *head = &tr->tr_list_buf;
  635. struct gfs2_bufdata *bd;
  636. gfs2_log_lock(sdp);
  637. while (!list_empty(head)) {
  638. bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
  639. list_del_init(&bd->bd_list_tr);
  640. tr->tr_num_buf--;
  641. }
  642. gfs2_log_unlock(sdp);
  643. gfs2_assert_warn(sdp, !tr->tr_num_buf);
  644. }
  645. /**
  646. * gfs2_log_commit - Commit a transaction to the log
  647. * @sdp: the filesystem
  648. * @tr: the transaction
  649. *
  650. * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
  651. * or the total number of used blocks (pinned blocks plus AIL blocks)
  652. * is greater than thresh2.
  653. *
  654. * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
  655. * journal size.
  656. *
  657. * Returns: errno
  658. */
  659. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  660. {
  661. log_refund(sdp, tr);
  662. buf_lo_incore_commit(sdp, tr);
  663. up_read(&sdp->sd_log_flush_lock);
  664. if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
  665. ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
  666. atomic_read(&sdp->sd_log_thresh2)))
  667. wake_up(&sdp->sd_logd_waitq);
  668. }
  669. /**
  670. * gfs2_log_shutdown - write a shutdown header into a journal
  671. * @sdp: the filesystem
  672. *
  673. */
  674. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  675. {
  676. down_write(&sdp->sd_log_flush_lock);
  677. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  678. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  679. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  680. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  681. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  682. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  683. sdp->sd_log_flush_head = sdp->sd_log_head;
  684. sdp->sd_log_flush_wrapped = 0;
  685. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
  686. (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
  687. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  688. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  689. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  690. sdp->sd_log_head = sdp->sd_log_flush_head;
  691. sdp->sd_log_tail = sdp->sd_log_head;
  692. up_write(&sdp->sd_log_flush_lock);
  693. }
  694. /**
  695. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  696. * @sdp: the filesystem
  697. *
  698. */
  699. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  700. {
  701. gfs2_log_flush(sdp, NULL);
  702. for (;;) {
  703. gfs2_ail1_start(sdp);
  704. gfs2_ail1_wait(sdp);
  705. if (gfs2_ail1_empty(sdp))
  706. break;
  707. }
  708. gfs2_log_flush(sdp, NULL);
  709. }
  710. static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
  711. {
  712. return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
  713. }
  714. static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
  715. {
  716. unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
  717. return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
  718. }
  719. /**
  720. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  721. * @sdp: Pointer to GFS2 superblock
  722. *
  723. * Also, periodically check to make sure that we're using the most recent
  724. * journal index.
  725. */
  726. int gfs2_logd(void *data)
  727. {
  728. struct gfs2_sbd *sdp = data;
  729. unsigned long t = 1;
  730. DEFINE_WAIT(wait);
  731. unsigned preflush;
  732. while (!kthread_should_stop()) {
  733. preflush = atomic_read(&sdp->sd_log_pinned);
  734. if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
  735. gfs2_ail1_empty(sdp);
  736. gfs2_log_flush(sdp, NULL);
  737. }
  738. if (gfs2_ail_flush_reqd(sdp)) {
  739. gfs2_ail1_start(sdp);
  740. gfs2_ail1_wait(sdp);
  741. gfs2_ail1_empty(sdp);
  742. gfs2_log_flush(sdp, NULL);
  743. }
  744. if (!gfs2_ail_flush_reqd(sdp))
  745. wake_up(&sdp->sd_log_waitq);
  746. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  747. try_to_freeze();
  748. do {
  749. prepare_to_wait(&sdp->sd_logd_waitq, &wait,
  750. TASK_INTERRUPTIBLE);
  751. if (!gfs2_ail_flush_reqd(sdp) &&
  752. !gfs2_jrnl_flush_reqd(sdp) &&
  753. !kthread_should_stop())
  754. t = schedule_timeout(t);
  755. } while(t && !gfs2_ail_flush_reqd(sdp) &&
  756. !gfs2_jrnl_flush_reqd(sdp) &&
  757. !kthread_should_stop());
  758. finish_wait(&sdp->sd_logd_waitq, &wait);
  759. }
  760. return 0;
  761. }