log.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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.__pad0 = cpu_to_be64(0);
  498. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  499. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  500. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  501. lh->lh_flags = cpu_to_be32(flags);
  502. lh->lh_tail = cpu_to_be32(tail);
  503. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  504. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  505. lh->lh_hash = cpu_to_be32(hash);
  506. bh->b_end_io = end_buffer_write_sync;
  507. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
  508. goto skip_barrier;
  509. get_bh(bh);
  510. submit_bh(WRITE_SYNC | (1 << BIO_RW_BARRIER) | (1 << BIO_RW_META), bh);
  511. wait_on_buffer(bh);
  512. if (buffer_eopnotsupp(bh)) {
  513. clear_buffer_eopnotsupp(bh);
  514. set_buffer_uptodate(bh);
  515. set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
  516. lock_buffer(bh);
  517. skip_barrier:
  518. get_bh(bh);
  519. submit_bh(WRITE_SYNC | (1 << BIO_RW_META), bh);
  520. wait_on_buffer(bh);
  521. }
  522. if (!buffer_uptodate(bh))
  523. gfs2_io_error_bh(sdp, bh);
  524. brelse(bh);
  525. if (sdp->sd_log_tail != tail)
  526. log_pull_tail(sdp, tail);
  527. else
  528. gfs2_assert_withdraw(sdp, !pull);
  529. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  530. gfs2_log_incr_head(sdp);
  531. }
  532. static void log_flush_commit(struct gfs2_sbd *sdp)
  533. {
  534. DEFINE_WAIT(wait);
  535. if (atomic_read(&sdp->sd_log_in_flight)) {
  536. do {
  537. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  538. TASK_UNINTERRUPTIBLE);
  539. if (atomic_read(&sdp->sd_log_in_flight))
  540. io_schedule();
  541. } while(atomic_read(&sdp->sd_log_in_flight));
  542. finish_wait(&sdp->sd_log_flush_wait, &wait);
  543. }
  544. log_write_header(sdp, 0, 0);
  545. }
  546. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  547. {
  548. struct gfs2_bufdata *bd;
  549. struct buffer_head *bh;
  550. LIST_HEAD(written);
  551. gfs2_log_lock(sdp);
  552. while (!list_empty(&sdp->sd_log_le_ordered)) {
  553. bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
  554. list_move(&bd->bd_le.le_list, &written);
  555. bh = bd->bd_bh;
  556. if (!buffer_dirty(bh))
  557. continue;
  558. get_bh(bh);
  559. gfs2_log_unlock(sdp);
  560. lock_buffer(bh);
  561. if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
  562. bh->b_end_io = end_buffer_write_sync;
  563. submit_bh(WRITE_SYNC_PLUG, bh);
  564. } else {
  565. unlock_buffer(bh);
  566. brelse(bh);
  567. }
  568. gfs2_log_lock(sdp);
  569. }
  570. list_splice(&written, &sdp->sd_log_le_ordered);
  571. gfs2_log_unlock(sdp);
  572. }
  573. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  574. {
  575. struct gfs2_bufdata *bd;
  576. struct buffer_head *bh;
  577. gfs2_log_lock(sdp);
  578. while (!list_empty(&sdp->sd_log_le_ordered)) {
  579. bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
  580. bh = bd->bd_bh;
  581. if (buffer_locked(bh)) {
  582. get_bh(bh);
  583. gfs2_log_unlock(sdp);
  584. wait_on_buffer(bh);
  585. brelse(bh);
  586. gfs2_log_lock(sdp);
  587. continue;
  588. }
  589. list_del_init(&bd->bd_le.le_list);
  590. }
  591. gfs2_log_unlock(sdp);
  592. }
  593. /**
  594. * gfs2_log_flush - flush incore transaction(s)
  595. * @sdp: the filesystem
  596. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  597. *
  598. */
  599. void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  600. {
  601. struct gfs2_ail *ai;
  602. down_write(&sdp->sd_log_flush_lock);
  603. /* Log might have been flushed while we waited for the flush lock */
  604. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  605. up_write(&sdp->sd_log_flush_lock);
  606. return;
  607. }
  608. trace_gfs2_log_flush(sdp, 1);
  609. ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
  610. INIT_LIST_HEAD(&ai->ai_ail1_list);
  611. INIT_LIST_HEAD(&ai->ai_ail2_list);
  612. if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
  613. printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
  614. sdp->sd_log_commited_buf);
  615. gfs2_assert_withdraw(sdp, 0);
  616. }
  617. if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
  618. printk(KERN_INFO "GFS2: log databuf %u %u\n",
  619. sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
  620. gfs2_assert_withdraw(sdp, 0);
  621. }
  622. gfs2_assert_withdraw(sdp,
  623. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  624. sdp->sd_log_flush_head = sdp->sd_log_head;
  625. sdp->sd_log_flush_wrapped = 0;
  626. ai->ai_first = sdp->sd_log_flush_head;
  627. gfs2_ordered_write(sdp);
  628. lops_before_commit(sdp);
  629. gfs2_ordered_wait(sdp);
  630. if (sdp->sd_log_head != sdp->sd_log_flush_head)
  631. log_flush_commit(sdp);
  632. else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  633. gfs2_log_lock(sdp);
  634. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  635. trace_gfs2_log_blocks(sdp, -1);
  636. gfs2_log_unlock(sdp);
  637. log_write_header(sdp, 0, PULL);
  638. }
  639. lops_after_commit(sdp, ai);
  640. gfs2_log_lock(sdp);
  641. sdp->sd_log_head = sdp->sd_log_flush_head;
  642. sdp->sd_log_blks_reserved = 0;
  643. sdp->sd_log_commited_buf = 0;
  644. sdp->sd_log_commited_databuf = 0;
  645. sdp->sd_log_commited_revoke = 0;
  646. if (!list_empty(&ai->ai_ail1_list)) {
  647. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  648. ai = NULL;
  649. }
  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. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
  667. reserved = calc_reserved(sdp);
  668. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
  669. unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
  670. atomic_add(unused, &sdp->sd_log_blks_free);
  671. trace_gfs2_log_blocks(sdp, unused);
  672. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  673. sdp->sd_jdesc->jd_blocks);
  674. sdp->sd_log_blks_reserved = reserved;
  675. gfs2_log_unlock(sdp);
  676. }
  677. static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  678. {
  679. struct list_head *head = &tr->tr_list_buf;
  680. struct gfs2_bufdata *bd;
  681. gfs2_log_lock(sdp);
  682. while (!list_empty(head)) {
  683. bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
  684. list_del_init(&bd->bd_list_tr);
  685. tr->tr_num_buf--;
  686. }
  687. gfs2_log_unlock(sdp);
  688. gfs2_assert_warn(sdp, !tr->tr_num_buf);
  689. }
  690. /**
  691. * gfs2_log_commit - Commit a transaction to the log
  692. * @sdp: the filesystem
  693. * @tr: the transaction
  694. *
  695. * Returns: errno
  696. */
  697. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  698. {
  699. log_refund(sdp, tr);
  700. buf_lo_incore_commit(sdp, tr);
  701. up_read(&sdp->sd_log_flush_lock);
  702. gfs2_log_lock(sdp);
  703. if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
  704. wake_up_process(sdp->sd_logd_process);
  705. gfs2_log_unlock(sdp);
  706. }
  707. /**
  708. * gfs2_log_shutdown - write a shutdown header into a journal
  709. * @sdp: the filesystem
  710. *
  711. */
  712. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  713. {
  714. down_write(&sdp->sd_log_flush_lock);
  715. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  716. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  717. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  718. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  719. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  720. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  721. sdp->sd_log_flush_head = sdp->sd_log_head;
  722. sdp->sd_log_flush_wrapped = 0;
  723. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
  724. (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
  725. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  726. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  727. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  728. sdp->sd_log_head = sdp->sd_log_flush_head;
  729. sdp->sd_log_tail = sdp->sd_log_head;
  730. up_write(&sdp->sd_log_flush_lock);
  731. }
  732. /**
  733. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  734. * @sdp: the filesystem
  735. *
  736. */
  737. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  738. {
  739. gfs2_log_flush(sdp, NULL);
  740. for (;;) {
  741. gfs2_ail1_start(sdp, DIO_ALL);
  742. if (gfs2_ail1_empty(sdp, DIO_ALL))
  743. break;
  744. msleep(10);
  745. }
  746. }
  747. /**
  748. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  749. * @sdp: Pointer to GFS2 superblock
  750. *
  751. * Also, periodically check to make sure that we're using the most recent
  752. * journal index.
  753. */
  754. int gfs2_logd(void *data)
  755. {
  756. struct gfs2_sbd *sdp = data;
  757. unsigned long t;
  758. int need_flush;
  759. while (!kthread_should_stop()) {
  760. /* Advance the log tail */
  761. t = sdp->sd_log_flush_time +
  762. gfs2_tune_get(sdp, gt_log_flush_secs) * HZ;
  763. gfs2_ail1_empty(sdp, DIO_ALL);
  764. gfs2_log_lock(sdp);
  765. need_flush = sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks);
  766. gfs2_log_unlock(sdp);
  767. if (need_flush || time_after_eq(jiffies, t)) {
  768. gfs2_log_flush(sdp, NULL);
  769. sdp->sd_log_flush_time = jiffies;
  770. }
  771. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  772. if (freezing(current))
  773. refrigerator();
  774. schedule_timeout_interruptible(t);
  775. }
  776. return 0;
  777. }