log.c 23 KB

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