log.c 24 KB

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