log.c 22 KB

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