log.c 24 KB

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