log.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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 "gfs2.h"
  21. #include "incore.h"
  22. #include "bmap.h"
  23. #include "glock.h"
  24. #include "log.h"
  25. #include "lops.h"
  26. #include "meta_io.h"
  27. #include "util.h"
  28. #include "dir.h"
  29. #include "trace_gfs2.h"
  30. #define PULL 1
  31. /**
  32. * gfs2_struct2blk - compute stuff
  33. * @sdp: the filesystem
  34. * @nstruct: the number of structures
  35. * @ssize: the size of the structures
  36. *
  37. * Compute the number of log descriptor blocks needed to hold a certain number
  38. * of structures of a certain size.
  39. *
  40. * Returns: the number of blocks needed (minimum is always 1)
  41. */
  42. unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
  43. unsigned int ssize)
  44. {
  45. unsigned int blks;
  46. unsigned int first, second;
  47. blks = 1;
  48. first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
  49. if (nstruct > first) {
  50. second = (sdp->sd_sb.sb_bsize -
  51. sizeof(struct gfs2_meta_header)) / ssize;
  52. blks += DIV_ROUND_UP(nstruct - first, second);
  53. }
  54. return blks;
  55. }
  56. /**
  57. * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
  58. * @mapping: The associated mapping (maybe NULL)
  59. * @bd: The gfs2_bufdata to remove
  60. *
  61. * The log lock _must_ be held when calling this function
  62. *
  63. */
  64. void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
  65. {
  66. bd->bd_ail = NULL;
  67. list_del_init(&bd->bd_ail_st_list);
  68. list_del_init(&bd->bd_ail_gl_list);
  69. atomic_dec(&bd->bd_gl->gl_ail_count);
  70. brelse(bd->bd_bh);
  71. }
  72. /**
  73. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  74. * @sdp: the filesystem
  75. * @tr: the part of the AIL
  76. *
  77. */
  78. static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  79. __releases(&sdp->sd_log_lock)
  80. __acquires(&sdp->sd_log_lock)
  81. {
  82. struct gfs2_bufdata *bd, *s;
  83. struct buffer_head *bh;
  84. int retry;
  85. do {
  86. retry = 0;
  87. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  88. bd_ail_st_list) {
  89. bh = bd->bd_bh;
  90. gfs2_assert(sdp, bd->bd_ail == ai);
  91. if (!buffer_busy(bh)) {
  92. if (!buffer_uptodate(bh))
  93. gfs2_io_error_bh(sdp, bh);
  94. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  95. continue;
  96. }
  97. if (!buffer_dirty(bh))
  98. continue;
  99. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  100. get_bh(bh);
  101. gfs2_log_unlock(sdp);
  102. lock_buffer(bh);
  103. if (test_clear_buffer_dirty(bh)) {
  104. bh->b_end_io = end_buffer_write_sync;
  105. submit_bh(WRITE_SYNC_PLUG, bh);
  106. } else {
  107. unlock_buffer(bh);
  108. brelse(bh);
  109. }
  110. gfs2_log_lock(sdp);
  111. retry = 1;
  112. break;
  113. }
  114. } while (retry);
  115. }
  116. /**
  117. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  118. * @sdp: the filesystem
  119. * @ai: the AIL entry
  120. *
  121. */
  122. static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
  123. {
  124. struct gfs2_bufdata *bd, *s;
  125. struct buffer_head *bh;
  126. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  127. bd_ail_st_list) {
  128. bh = bd->bd_bh;
  129. gfs2_assert(sdp, bd->bd_ail == ai);
  130. if (buffer_busy(bh)) {
  131. if (flags & DIO_ALL)
  132. continue;
  133. else
  134. break;
  135. }
  136. if (!buffer_uptodate(bh))
  137. gfs2_io_error_bh(sdp, bh);
  138. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  139. }
  140. return list_empty(&ai->ai_ail1_list);
  141. }
  142. static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
  143. {
  144. struct list_head *head;
  145. u64 sync_gen;
  146. struct list_head *first;
  147. struct gfs2_ail *first_ai, *ai, *tmp;
  148. int done = 0;
  149. gfs2_log_lock(sdp);
  150. head = &sdp->sd_ail1_list;
  151. if (list_empty(head)) {
  152. gfs2_log_unlock(sdp);
  153. return;
  154. }
  155. sync_gen = sdp->sd_ail_sync_gen++;
  156. first = head->prev;
  157. first_ai = list_entry(first, struct gfs2_ail, ai_list);
  158. first_ai->ai_sync_gen = sync_gen;
  159. gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
  160. if (flags & DIO_ALL)
  161. first = NULL;
  162. while(!done) {
  163. if (first && (head->prev != first ||
  164. gfs2_ail1_empty_one(sdp, first_ai, 0)))
  165. break;
  166. done = 1;
  167. list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
  168. if (ai->ai_sync_gen >= sync_gen)
  169. continue;
  170. ai->ai_sync_gen = sync_gen;
  171. gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
  172. done = 0;
  173. break;
  174. }
  175. }
  176. gfs2_log_unlock(sdp);
  177. }
  178. static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
  179. {
  180. struct gfs2_ail *ai, *s;
  181. int ret;
  182. gfs2_log_lock(sdp);
  183. list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
  184. if (gfs2_ail1_empty_one(sdp, ai, flags))
  185. list_move(&ai->ai_list, &sdp->sd_ail2_list);
  186. else if (!(flags & DIO_ALL))
  187. break;
  188. }
  189. ret = list_empty(&sdp->sd_ail1_list);
  190. gfs2_log_unlock(sdp);
  191. return ret;
  192. }
  193. /**
  194. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  195. * @sdp: the filesystem
  196. * @ai: the AIL entry
  197. *
  198. */
  199. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  200. {
  201. struct list_head *head = &ai->ai_ail2_list;
  202. struct gfs2_bufdata *bd;
  203. while (!list_empty(head)) {
  204. bd = list_entry(head->prev, struct gfs2_bufdata,
  205. bd_ail_st_list);
  206. gfs2_assert(sdp, bd->bd_ail == ai);
  207. gfs2_remove_from_ail(bd);
  208. }
  209. }
  210. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  211. {
  212. struct gfs2_ail *ai, *safe;
  213. unsigned int old_tail = sdp->sd_log_tail;
  214. int wrap = (new_tail < old_tail);
  215. int a, b, rm;
  216. gfs2_log_lock(sdp);
  217. list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
  218. a = (old_tail <= ai->ai_first);
  219. b = (ai->ai_first < new_tail);
  220. rm = (wrap) ? (a || b) : (a && b);
  221. if (!rm)
  222. continue;
  223. gfs2_ail2_empty_one(sdp, ai);
  224. list_del(&ai->ai_list);
  225. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
  226. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
  227. kfree(ai);
  228. }
  229. gfs2_log_unlock(sdp);
  230. }
  231. /**
  232. * gfs2_log_reserve - Make a log reservation
  233. * @sdp: The GFS2 superblock
  234. * @blks: The number of blocks to reserve
  235. *
  236. * Note that we never give out the last few blocks of the journal. Thats
  237. * due to the fact that there is a small number of header blocks
  238. * associated with each log flush. The exact number can't be known until
  239. * flush time, so we ensure that we have just enough free blocks at all
  240. * times to avoid running out during a log flush.
  241. *
  242. * Returns: errno
  243. */
  244. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  245. {
  246. unsigned int try = 0;
  247. unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
  248. if (gfs2_assert_warn(sdp, blks) ||
  249. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  250. return -EINVAL;
  251. mutex_lock(&sdp->sd_log_reserve_mutex);
  252. gfs2_log_lock(sdp);
  253. while(atomic_read(&sdp->sd_log_blks_free) <= (blks + reserved_blks)) {
  254. gfs2_log_unlock(sdp);
  255. gfs2_ail1_empty(sdp, 0);
  256. gfs2_log_flush(sdp, NULL);
  257. if (try++)
  258. gfs2_ail1_start(sdp, 0);
  259. gfs2_log_lock(sdp);
  260. }
  261. atomic_sub(blks, &sdp->sd_log_blks_free);
  262. trace_gfs2_log_blocks(sdp, -blks);
  263. gfs2_log_unlock(sdp);
  264. mutex_unlock(&sdp->sd_log_reserve_mutex);
  265. down_read(&sdp->sd_log_flush_lock);
  266. return 0;
  267. }
  268. /**
  269. * gfs2_log_release - Release a given number of log blocks
  270. * @sdp: The GFS2 superblock
  271. * @blks: The number of blocks
  272. *
  273. */
  274. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  275. {
  276. gfs2_log_lock(sdp);
  277. atomic_add(blks, &sdp->sd_log_blks_free);
  278. trace_gfs2_log_blocks(sdp, blks);
  279. gfs2_assert_withdraw(sdp,
  280. atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
  281. gfs2_log_unlock(sdp);
  282. up_read(&sdp->sd_log_flush_lock);
  283. }
  284. static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
  285. {
  286. struct gfs2_journal_extent *je;
  287. list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
  288. if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
  289. return je->dblock + lbn - je->lblock;
  290. }
  291. return -1;
  292. }
  293. /**
  294. * log_distance - Compute distance between two journal blocks
  295. * @sdp: The GFS2 superblock
  296. * @newer: The most recent journal block of the pair
  297. * @older: The older journal block of the pair
  298. *
  299. * Compute the distance (in the journal direction) between two
  300. * blocks in the journal
  301. *
  302. * Returns: the distance in blocks
  303. */
  304. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  305. unsigned int older)
  306. {
  307. int dist;
  308. dist = newer - older;
  309. if (dist < 0)
  310. dist += sdp->sd_jdesc->jd_blocks;
  311. return dist;
  312. }
  313. /**
  314. * calc_reserved - Calculate the number of blocks to reserve when
  315. * refunding a transaction's unused buffers.
  316. * @sdp: The GFS2 superblock
  317. *
  318. * This is complex. We need to reserve room for all our currently used
  319. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  320. * all our journaled data buffers for journaled files (e.g. files in the
  321. * meta_fs like rindex, or files for which chattr +j was done.)
  322. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  323. * will count it as free space (sd_log_blks_free) and corruption will follow.
  324. *
  325. * We can have metadata bufs and jdata bufs in the same journal. So each
  326. * type gets its own log header, for which we need to reserve a block.
  327. * In fact, each type has the potential for needing more than one header
  328. * in cases where we have more buffers than will fit on a journal page.
  329. * Metadata journal entries take up half the space of journaled buffer entries.
  330. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  331. * databuf_limit (251) before they cause a wrap around.
  332. *
  333. * Also, we need to reserve blocks for revoke journal entries and one for an
  334. * overall header for the lot.
  335. *
  336. * Returns: the number of blocks reserved
  337. */
  338. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  339. {
  340. unsigned int reserved = 0;
  341. unsigned int mbuf_limit, metabufhdrs_needed;
  342. unsigned int dbuf_limit, databufhdrs_needed;
  343. unsigned int revokes = 0;
  344. mbuf_limit = buf_limit(sdp);
  345. metabufhdrs_needed = (sdp->sd_log_commited_buf +
  346. (mbuf_limit - 1)) / mbuf_limit;
  347. dbuf_limit = databuf_limit(sdp);
  348. databufhdrs_needed = (sdp->sd_log_commited_databuf +
  349. (dbuf_limit - 1)) / dbuf_limit;
  350. if (sdp->sd_log_commited_revoke)
  351. revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  352. sizeof(u64));
  353. reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
  354. sdp->sd_log_commited_databuf + databufhdrs_needed +
  355. revokes;
  356. /* One for the overall header */
  357. if (reserved)
  358. reserved++;
  359. return reserved;
  360. }
  361. static unsigned int current_tail(struct gfs2_sbd *sdp)
  362. {
  363. struct gfs2_ail *ai;
  364. unsigned int tail;
  365. gfs2_log_lock(sdp);
  366. if (list_empty(&sdp->sd_ail1_list)) {
  367. tail = sdp->sd_log_head;
  368. } else {
  369. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  370. tail = ai->ai_first;
  371. }
  372. gfs2_log_unlock(sdp);
  373. return tail;
  374. }
  375. void gfs2_log_incr_head(struct gfs2_sbd *sdp)
  376. {
  377. if (sdp->sd_log_flush_head == sdp->sd_log_tail)
  378. BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
  379. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  380. sdp->sd_log_flush_head = 0;
  381. sdp->sd_log_flush_wrapped = 1;
  382. }
  383. }
  384. /**
  385. * gfs2_log_write_endio - End of I/O for a log buffer
  386. * @bh: The buffer head
  387. * @uptodate: I/O Status
  388. *
  389. */
  390. static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
  391. {
  392. struct gfs2_sbd *sdp = bh->b_private;
  393. bh->b_private = NULL;
  394. end_buffer_write_sync(bh, uptodate);
  395. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  396. wake_up(&sdp->sd_log_flush_wait);
  397. }
  398. /**
  399. * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
  400. * @sdp: The GFS2 superblock
  401. *
  402. * Returns: the buffer_head
  403. */
  404. struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
  405. {
  406. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  407. struct buffer_head *bh;
  408. bh = sb_getblk(sdp->sd_vfs, blkno);
  409. lock_buffer(bh);
  410. memset(bh->b_data, 0, bh->b_size);
  411. set_buffer_uptodate(bh);
  412. clear_buffer_dirty(bh);
  413. gfs2_log_incr_head(sdp);
  414. atomic_inc(&sdp->sd_log_in_flight);
  415. bh->b_private = sdp;
  416. bh->b_end_io = gfs2_log_write_endio;
  417. return bh;
  418. }
  419. /**
  420. * gfs2_fake_write_endio -
  421. * @bh: The buffer head
  422. * @uptodate: The I/O Status
  423. *
  424. */
  425. static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
  426. {
  427. struct buffer_head *real_bh = bh->b_private;
  428. struct gfs2_bufdata *bd = real_bh->b_private;
  429. struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
  430. end_buffer_write_sync(bh, uptodate);
  431. free_buffer_head(bh);
  432. unlock_buffer(real_bh);
  433. brelse(real_bh);
  434. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  435. wake_up(&sdp->sd_log_flush_wait);
  436. }
  437. /**
  438. * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
  439. * @sdp: the filesystem
  440. * @data: the data the buffer_head should point to
  441. *
  442. * Returns: the log buffer descriptor
  443. */
  444. struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
  445. struct buffer_head *real)
  446. {
  447. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  448. struct buffer_head *bh;
  449. bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
  450. atomic_set(&bh->b_count, 1);
  451. bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
  452. set_bh_page(bh, real->b_page, bh_offset(real));
  453. bh->b_blocknr = blkno;
  454. bh->b_size = sdp->sd_sb.sb_bsize;
  455. bh->b_bdev = sdp->sd_vfs->s_bdev;
  456. bh->b_private = real;
  457. bh->b_end_io = gfs2_fake_write_endio;
  458. gfs2_log_incr_head(sdp);
  459. atomic_inc(&sdp->sd_log_in_flight);
  460. return bh;
  461. }
  462. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  463. {
  464. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  465. ail2_empty(sdp, new_tail);
  466. gfs2_log_lock(sdp);
  467. atomic_add(dist, &sdp->sd_log_blks_free);
  468. trace_gfs2_log_blocks(sdp, dist);
  469. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
  470. gfs2_log_unlock(sdp);
  471. sdp->sd_log_tail = new_tail;
  472. }
  473. /**
  474. * log_write_header - Get and initialize a journal header buffer
  475. * @sdp: The GFS2 superblock
  476. *
  477. * Returns: the initialized log buffer descriptor
  478. */
  479. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  480. {
  481. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  482. struct buffer_head *bh;
  483. struct gfs2_log_header *lh;
  484. unsigned int tail;
  485. u32 hash;
  486. bh = sb_getblk(sdp->sd_vfs, blkno);
  487. lock_buffer(bh);
  488. memset(bh->b_data, 0, bh->b_size);
  489. set_buffer_uptodate(bh);
  490. clear_buffer_dirty(bh);
  491. gfs2_ail1_empty(sdp, 0);
  492. tail = current_tail(sdp);
  493. lh = (struct gfs2_log_header *)bh->b_data;
  494. memset(lh, 0, sizeof(struct gfs2_log_header));
  495. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  496. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  497. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  498. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  499. lh->lh_flags = cpu_to_be32(flags);
  500. lh->lh_tail = cpu_to_be32(tail);
  501. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  502. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  503. lh->lh_hash = cpu_to_be32(hash);
  504. bh->b_end_io = end_buffer_write_sync;
  505. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
  506. goto skip_barrier;
  507. get_bh(bh);
  508. submit_bh(WRITE_SYNC | (1 << BIO_RW_BARRIER) | (1 << BIO_RW_META), bh);
  509. wait_on_buffer(bh);
  510. if (buffer_eopnotsupp(bh)) {
  511. clear_buffer_eopnotsupp(bh);
  512. set_buffer_uptodate(bh);
  513. set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
  514. lock_buffer(bh);
  515. skip_barrier:
  516. get_bh(bh);
  517. submit_bh(WRITE_SYNC | (1 << BIO_RW_META), bh);
  518. wait_on_buffer(bh);
  519. }
  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_PLUG, 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. if (!list_empty(&ai->ai_ail1_list)) {
  645. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  646. ai = NULL;
  647. }
  648. gfs2_log_unlock(sdp);
  649. trace_gfs2_log_flush(sdp, 0);
  650. up_write(&sdp->sd_log_flush_lock);
  651. kfree(ai);
  652. }
  653. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  654. {
  655. unsigned int reserved;
  656. unsigned int unused;
  657. gfs2_log_lock(sdp);
  658. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  659. sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
  660. tr->tr_num_databuf_rm;
  661. gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
  662. (((int)sdp->sd_log_commited_databuf) >= 0));
  663. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  664. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
  665. reserved = calc_reserved(sdp);
  666. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
  667. unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
  668. atomic_add(unused, &sdp->sd_log_blks_free);
  669. trace_gfs2_log_blocks(sdp, unused);
  670. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  671. sdp->sd_jdesc->jd_blocks);
  672. sdp->sd_log_blks_reserved = reserved;
  673. gfs2_log_unlock(sdp);
  674. }
  675. static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  676. {
  677. struct list_head *head = &tr->tr_list_buf;
  678. struct gfs2_bufdata *bd;
  679. gfs2_log_lock(sdp);
  680. while (!list_empty(head)) {
  681. bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
  682. list_del_init(&bd->bd_list_tr);
  683. tr->tr_num_buf--;
  684. }
  685. gfs2_log_unlock(sdp);
  686. gfs2_assert_warn(sdp, !tr->tr_num_buf);
  687. }
  688. /**
  689. * gfs2_log_commit - Commit a transaction to the log
  690. * @sdp: the filesystem
  691. * @tr: the transaction
  692. *
  693. * Returns: errno
  694. */
  695. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  696. {
  697. log_refund(sdp, tr);
  698. buf_lo_incore_commit(sdp, tr);
  699. up_read(&sdp->sd_log_flush_lock);
  700. gfs2_log_lock(sdp);
  701. if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
  702. wake_up_process(sdp->sd_logd_process);
  703. gfs2_log_unlock(sdp);
  704. }
  705. /**
  706. * gfs2_log_shutdown - write a shutdown header into a journal
  707. * @sdp: the filesystem
  708. *
  709. */
  710. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  711. {
  712. down_write(&sdp->sd_log_flush_lock);
  713. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  714. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  715. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  716. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  717. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  718. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  719. sdp->sd_log_flush_head = sdp->sd_log_head;
  720. sdp->sd_log_flush_wrapped = 0;
  721. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
  722. (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
  723. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  724. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  725. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  726. sdp->sd_log_head = sdp->sd_log_flush_head;
  727. sdp->sd_log_tail = sdp->sd_log_head;
  728. up_write(&sdp->sd_log_flush_lock);
  729. }
  730. /**
  731. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  732. * @sdp: the filesystem
  733. *
  734. */
  735. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  736. {
  737. gfs2_log_flush(sdp, NULL);
  738. for (;;) {
  739. gfs2_ail1_start(sdp, DIO_ALL);
  740. if (gfs2_ail1_empty(sdp, DIO_ALL))
  741. break;
  742. msleep(10);
  743. }
  744. }
  745. /**
  746. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  747. * @sdp: Pointer to GFS2 superblock
  748. *
  749. * Also, periodically check to make sure that we're using the most recent
  750. * journal index.
  751. */
  752. int gfs2_logd(void *data)
  753. {
  754. struct gfs2_sbd *sdp = data;
  755. unsigned long t;
  756. int need_flush;
  757. while (!kthread_should_stop()) {
  758. /* Advance the log tail */
  759. t = sdp->sd_log_flush_time +
  760. gfs2_tune_get(sdp, gt_log_flush_secs) * HZ;
  761. gfs2_ail1_empty(sdp, DIO_ALL);
  762. gfs2_log_lock(sdp);
  763. need_flush = sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks);
  764. gfs2_log_unlock(sdp);
  765. if (need_flush || time_after_eq(jiffies, t)) {
  766. gfs2_log_flush(sdp, NULL);
  767. sdp->sd_log_flush_time = jiffies;
  768. }
  769. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  770. if (freezing(current))
  771. refrigerator();
  772. schedule_timeout_interruptible(t);
  773. }
  774. return 0;
  775. }