log.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 <linux/bio.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. #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, 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. gfs2_log_unlock(sdp);
  263. mutex_unlock(&sdp->sd_log_reserve_mutex);
  264. down_read(&sdp->sd_log_flush_lock);
  265. return 0;
  266. }
  267. /**
  268. * gfs2_log_release - Release a given number of log blocks
  269. * @sdp: The GFS2 superblock
  270. * @blks: The number of blocks
  271. *
  272. */
  273. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  274. {
  275. gfs2_log_lock(sdp);
  276. atomic_add(blks, &sdp->sd_log_blks_free);
  277. gfs2_assert_withdraw(sdp,
  278. atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
  279. gfs2_log_unlock(sdp);
  280. up_read(&sdp->sd_log_flush_lock);
  281. }
  282. static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
  283. {
  284. struct gfs2_journal_extent *je;
  285. list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
  286. if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
  287. return je->dblock + lbn - je->lblock;
  288. }
  289. return -1;
  290. }
  291. /**
  292. * log_distance - Compute distance between two journal blocks
  293. * @sdp: The GFS2 superblock
  294. * @newer: The most recent journal block of the pair
  295. * @older: The older journal block of the pair
  296. *
  297. * Compute the distance (in the journal direction) between two
  298. * blocks in the journal
  299. *
  300. * Returns: the distance in blocks
  301. */
  302. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  303. unsigned int older)
  304. {
  305. int dist;
  306. dist = newer - older;
  307. if (dist < 0)
  308. dist += sdp->sd_jdesc->jd_blocks;
  309. return dist;
  310. }
  311. /**
  312. * calc_reserved - Calculate the number of blocks to reserve when
  313. * refunding a transaction's unused buffers.
  314. * @sdp: The GFS2 superblock
  315. *
  316. * This is complex. We need to reserve room for all our currently used
  317. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  318. * all our journaled data buffers for journaled files (e.g. files in the
  319. * meta_fs like rindex, or files for which chattr +j was done.)
  320. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  321. * will count it as free space (sd_log_blks_free) and corruption will follow.
  322. *
  323. * We can have metadata bufs and jdata bufs in the same journal. So each
  324. * type gets its own log header, for which we need to reserve a block.
  325. * In fact, each type has the potential for needing more than one header
  326. * in cases where we have more buffers than will fit on a journal page.
  327. * Metadata journal entries take up half the space of journaled buffer entries.
  328. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  329. * databuf_limit (251) before they cause a wrap around.
  330. *
  331. * Also, we need to reserve blocks for revoke journal entries and one for an
  332. * overall header for the lot.
  333. *
  334. * Returns: the number of blocks reserved
  335. */
  336. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  337. {
  338. unsigned int reserved = 0;
  339. unsigned int mbuf_limit, metabufhdrs_needed;
  340. unsigned int dbuf_limit, databufhdrs_needed;
  341. unsigned int revokes = 0;
  342. mbuf_limit = buf_limit(sdp);
  343. metabufhdrs_needed = (sdp->sd_log_commited_buf +
  344. (mbuf_limit - 1)) / mbuf_limit;
  345. dbuf_limit = databuf_limit(sdp);
  346. databufhdrs_needed = (sdp->sd_log_commited_databuf +
  347. (dbuf_limit - 1)) / dbuf_limit;
  348. if (sdp->sd_log_commited_revoke)
  349. revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  350. sizeof(u64));
  351. reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
  352. sdp->sd_log_commited_databuf + databufhdrs_needed +
  353. revokes;
  354. /* One for the overall header */
  355. if (reserved)
  356. reserved++;
  357. return reserved;
  358. }
  359. static unsigned int current_tail(struct gfs2_sbd *sdp)
  360. {
  361. struct gfs2_ail *ai;
  362. unsigned int tail;
  363. gfs2_log_lock(sdp);
  364. if (list_empty(&sdp->sd_ail1_list)) {
  365. tail = sdp->sd_log_head;
  366. } else {
  367. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  368. tail = ai->ai_first;
  369. }
  370. gfs2_log_unlock(sdp);
  371. return tail;
  372. }
  373. void gfs2_log_incr_head(struct gfs2_sbd *sdp)
  374. {
  375. if (sdp->sd_log_flush_head == sdp->sd_log_tail)
  376. BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
  377. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  378. sdp->sd_log_flush_head = 0;
  379. sdp->sd_log_flush_wrapped = 1;
  380. }
  381. }
  382. /**
  383. * gfs2_log_write_endio - End of I/O for a log buffer
  384. * @bh: The buffer head
  385. * @uptodate: I/O Status
  386. *
  387. */
  388. static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
  389. {
  390. struct gfs2_sbd *sdp = bh->b_private;
  391. bh->b_private = NULL;
  392. end_buffer_write_sync(bh, uptodate);
  393. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  394. wake_up(&sdp->sd_log_flush_wait);
  395. }
  396. /**
  397. * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
  398. * @sdp: The GFS2 superblock
  399. *
  400. * Returns: the buffer_head
  401. */
  402. struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
  403. {
  404. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  405. struct buffer_head *bh;
  406. bh = sb_getblk(sdp->sd_vfs, blkno);
  407. lock_buffer(bh);
  408. memset(bh->b_data, 0, bh->b_size);
  409. set_buffer_uptodate(bh);
  410. clear_buffer_dirty(bh);
  411. gfs2_log_incr_head(sdp);
  412. atomic_inc(&sdp->sd_log_in_flight);
  413. bh->b_private = sdp;
  414. bh->b_end_io = gfs2_log_write_endio;
  415. return bh;
  416. }
  417. /**
  418. * gfs2_fake_write_endio -
  419. * @bh: The buffer head
  420. * @uptodate: The I/O Status
  421. *
  422. */
  423. static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
  424. {
  425. struct buffer_head *real_bh = bh->b_private;
  426. struct gfs2_bufdata *bd = real_bh->b_private;
  427. struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
  428. end_buffer_write_sync(bh, uptodate);
  429. free_buffer_head(bh);
  430. unlock_buffer(real_bh);
  431. brelse(real_bh);
  432. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  433. wake_up(&sdp->sd_log_flush_wait);
  434. }
  435. /**
  436. * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
  437. * @sdp: the filesystem
  438. * @data: the data the buffer_head should point to
  439. *
  440. * Returns: the log buffer descriptor
  441. */
  442. struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
  443. struct buffer_head *real)
  444. {
  445. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  446. struct buffer_head *bh;
  447. bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
  448. atomic_set(&bh->b_count, 1);
  449. bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
  450. set_bh_page(bh, real->b_page, bh_offset(real));
  451. bh->b_blocknr = blkno;
  452. bh->b_size = sdp->sd_sb.sb_bsize;
  453. bh->b_bdev = sdp->sd_vfs->s_bdev;
  454. bh->b_private = real;
  455. bh->b_end_io = gfs2_fake_write_endio;
  456. gfs2_log_incr_head(sdp);
  457. atomic_inc(&sdp->sd_log_in_flight);
  458. return bh;
  459. }
  460. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  461. {
  462. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  463. ail2_empty(sdp, new_tail);
  464. gfs2_log_lock(sdp);
  465. atomic_add(dist, &sdp->sd_log_blks_free);
  466. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
  467. gfs2_log_unlock(sdp);
  468. sdp->sd_log_tail = new_tail;
  469. }
  470. /**
  471. * log_write_header - Get and initialize a journal header buffer
  472. * @sdp: The GFS2 superblock
  473. *
  474. * Returns: the initialized log buffer descriptor
  475. */
  476. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  477. {
  478. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  479. struct buffer_head *bh;
  480. struct gfs2_log_header *lh;
  481. unsigned int tail;
  482. u32 hash;
  483. bh = sb_getblk(sdp->sd_vfs, blkno);
  484. lock_buffer(bh);
  485. memset(bh->b_data, 0, bh->b_size);
  486. set_buffer_uptodate(bh);
  487. clear_buffer_dirty(bh);
  488. gfs2_ail1_empty(sdp, 0);
  489. tail = current_tail(sdp);
  490. lh = (struct gfs2_log_header *)bh->b_data;
  491. memset(lh, 0, sizeof(struct gfs2_log_header));
  492. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  493. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  494. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  495. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  496. lh->lh_flags = cpu_to_be32(flags);
  497. lh->lh_tail = cpu_to_be32(tail);
  498. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  499. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  500. lh->lh_hash = cpu_to_be32(hash);
  501. bh->b_end_io = end_buffer_write_sync;
  502. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
  503. goto skip_barrier;
  504. get_bh(bh);
  505. submit_bh(WRITE_BARRIER | (1 << BIO_RW_META), bh);
  506. wait_on_buffer(bh);
  507. if (buffer_eopnotsupp(bh)) {
  508. clear_buffer_eopnotsupp(bh);
  509. set_buffer_uptodate(bh);
  510. set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
  511. lock_buffer(bh);
  512. skip_barrier:
  513. get_bh(bh);
  514. submit_bh(WRITE_SYNC | (1 << BIO_RW_META), bh);
  515. wait_on_buffer(bh);
  516. }
  517. if (!buffer_uptodate(bh))
  518. gfs2_io_error_bh(sdp, bh);
  519. brelse(bh);
  520. if (sdp->sd_log_tail != tail)
  521. log_pull_tail(sdp, tail);
  522. else
  523. gfs2_assert_withdraw(sdp, !pull);
  524. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  525. gfs2_log_incr_head(sdp);
  526. }
  527. static void log_flush_commit(struct gfs2_sbd *sdp)
  528. {
  529. DEFINE_WAIT(wait);
  530. if (atomic_read(&sdp->sd_log_in_flight)) {
  531. do {
  532. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  533. TASK_UNINTERRUPTIBLE);
  534. if (atomic_read(&sdp->sd_log_in_flight))
  535. io_schedule();
  536. } while(atomic_read(&sdp->sd_log_in_flight));
  537. finish_wait(&sdp->sd_log_flush_wait, &wait);
  538. }
  539. log_write_header(sdp, 0, 0);
  540. }
  541. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  542. {
  543. struct gfs2_bufdata *bd;
  544. struct buffer_head *bh;
  545. LIST_HEAD(written);
  546. gfs2_log_lock(sdp);
  547. while (!list_empty(&sdp->sd_log_le_ordered)) {
  548. bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
  549. list_move(&bd->bd_le.le_list, &written);
  550. bh = bd->bd_bh;
  551. if (!buffer_dirty(bh))
  552. continue;
  553. get_bh(bh);
  554. gfs2_log_unlock(sdp);
  555. lock_buffer(bh);
  556. if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
  557. bh->b_end_io = end_buffer_write_sync;
  558. submit_bh(WRITE, bh);
  559. } else {
  560. unlock_buffer(bh);
  561. brelse(bh);
  562. }
  563. gfs2_log_lock(sdp);
  564. }
  565. list_splice(&written, &sdp->sd_log_le_ordered);
  566. gfs2_log_unlock(sdp);
  567. }
  568. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  569. {
  570. struct gfs2_bufdata *bd;
  571. struct buffer_head *bh;
  572. gfs2_log_lock(sdp);
  573. while (!list_empty(&sdp->sd_log_le_ordered)) {
  574. bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
  575. bh = bd->bd_bh;
  576. if (buffer_locked(bh)) {
  577. get_bh(bh);
  578. gfs2_log_unlock(sdp);
  579. wait_on_buffer(bh);
  580. brelse(bh);
  581. gfs2_log_lock(sdp);
  582. continue;
  583. }
  584. list_del_init(&bd->bd_le.le_list);
  585. }
  586. gfs2_log_unlock(sdp);
  587. }
  588. /**
  589. * gfs2_log_flush - flush incore transaction(s)
  590. * @sdp: the filesystem
  591. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  592. *
  593. */
  594. void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  595. {
  596. struct gfs2_ail *ai;
  597. down_write(&sdp->sd_log_flush_lock);
  598. /* Log might have been flushed while we waited for the flush lock */
  599. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  600. up_write(&sdp->sd_log_flush_lock);
  601. return;
  602. }
  603. ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
  604. INIT_LIST_HEAD(&ai->ai_ail1_list);
  605. INIT_LIST_HEAD(&ai->ai_ail2_list);
  606. if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
  607. printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
  608. sdp->sd_log_commited_buf);
  609. gfs2_assert_withdraw(sdp, 0);
  610. }
  611. if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
  612. printk(KERN_INFO "GFS2: log databuf %u %u\n",
  613. sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
  614. gfs2_assert_withdraw(sdp, 0);
  615. }
  616. gfs2_assert_withdraw(sdp,
  617. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  618. sdp->sd_log_flush_head = sdp->sd_log_head;
  619. sdp->sd_log_flush_wrapped = 0;
  620. ai->ai_first = sdp->sd_log_flush_head;
  621. gfs2_ordered_write(sdp);
  622. lops_before_commit(sdp);
  623. gfs2_ordered_wait(sdp);
  624. if (sdp->sd_log_head != sdp->sd_log_flush_head)
  625. log_flush_commit(sdp);
  626. else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  627. gfs2_log_lock(sdp);
  628. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  629. gfs2_log_unlock(sdp);
  630. log_write_header(sdp, 0, PULL);
  631. }
  632. lops_after_commit(sdp, ai);
  633. gfs2_log_lock(sdp);
  634. sdp->sd_log_head = sdp->sd_log_flush_head;
  635. sdp->sd_log_blks_reserved = 0;
  636. sdp->sd_log_commited_buf = 0;
  637. sdp->sd_log_commited_databuf = 0;
  638. sdp->sd_log_commited_revoke = 0;
  639. if (!list_empty(&ai->ai_ail1_list)) {
  640. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  641. ai = NULL;
  642. }
  643. gfs2_log_unlock(sdp);
  644. sdp->sd_vfs->s_dirt = 0;
  645. up_write(&sdp->sd_log_flush_lock);
  646. kfree(ai);
  647. }
  648. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  649. {
  650. unsigned int reserved;
  651. unsigned int unused;
  652. gfs2_log_lock(sdp);
  653. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  654. sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
  655. tr->tr_num_databuf_rm;
  656. gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
  657. (((int)sdp->sd_log_commited_databuf) >= 0));
  658. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  659. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
  660. reserved = calc_reserved(sdp);
  661. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
  662. unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
  663. atomic_add(unused, &sdp->sd_log_blks_free);
  664. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  665. sdp->sd_jdesc->jd_blocks);
  666. sdp->sd_log_blks_reserved = reserved;
  667. gfs2_log_unlock(sdp);
  668. }
  669. static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  670. {
  671. struct list_head *head = &tr->tr_list_buf;
  672. struct gfs2_bufdata *bd;
  673. gfs2_log_lock(sdp);
  674. while (!list_empty(head)) {
  675. bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
  676. list_del_init(&bd->bd_list_tr);
  677. tr->tr_num_buf--;
  678. }
  679. gfs2_log_unlock(sdp);
  680. gfs2_assert_warn(sdp, !tr->tr_num_buf);
  681. }
  682. /**
  683. * gfs2_log_commit - Commit a transaction to the log
  684. * @sdp: the filesystem
  685. * @tr: the transaction
  686. *
  687. * Returns: errno
  688. */
  689. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  690. {
  691. log_refund(sdp, tr);
  692. buf_lo_incore_commit(sdp, tr);
  693. sdp->sd_vfs->s_dirt = 1;
  694. up_read(&sdp->sd_log_flush_lock);
  695. gfs2_log_lock(sdp);
  696. if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
  697. wake_up_process(sdp->sd_logd_process);
  698. gfs2_log_unlock(sdp);
  699. }
  700. /**
  701. * gfs2_log_shutdown - write a shutdown header into a journal
  702. * @sdp: the filesystem
  703. *
  704. */
  705. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  706. {
  707. down_write(&sdp->sd_log_flush_lock);
  708. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  709. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  710. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  711. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  712. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  713. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  714. sdp->sd_log_flush_head = sdp->sd_log_head;
  715. sdp->sd_log_flush_wrapped = 0;
  716. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
  717. (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
  718. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  719. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  720. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  721. sdp->sd_log_head = sdp->sd_log_flush_head;
  722. sdp->sd_log_tail = sdp->sd_log_head;
  723. up_write(&sdp->sd_log_flush_lock);
  724. }
  725. /**
  726. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  727. * @sdp: the filesystem
  728. *
  729. */
  730. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  731. {
  732. gfs2_log_flush(sdp, NULL);
  733. for (;;) {
  734. gfs2_ail1_start(sdp, DIO_ALL);
  735. if (gfs2_ail1_empty(sdp, DIO_ALL))
  736. break;
  737. msleep(10);
  738. }
  739. }
  740. /**
  741. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  742. * @sdp: Pointer to GFS2 superblock
  743. *
  744. * Also, periodically check to make sure that we're using the most recent
  745. * journal index.
  746. */
  747. int gfs2_logd(void *data)
  748. {
  749. struct gfs2_sbd *sdp = data;
  750. unsigned long t;
  751. int need_flush;
  752. while (!kthread_should_stop()) {
  753. /* Advance the log tail */
  754. t = sdp->sd_log_flush_time +
  755. gfs2_tune_get(sdp, gt_log_flush_secs) * HZ;
  756. gfs2_ail1_empty(sdp, DIO_ALL);
  757. gfs2_log_lock(sdp);
  758. need_flush = sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks);
  759. gfs2_log_unlock(sdp);
  760. if (need_flush || time_after_eq(jiffies, t)) {
  761. gfs2_log_flush(sdp, NULL);
  762. sdp->sd_log_flush_time = jiffies;
  763. }
  764. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  765. if (freezing(current))
  766. refrigerator();
  767. schedule_timeout_interruptible(t);
  768. }
  769. return 0;
  770. }