log.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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)
  143. {
  144. struct list_head *head;
  145. u64 sync_gen;
  146. struct gfs2_ail *ai;
  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. while(!done) {
  156. done = 1;
  157. list_for_each_entry_reverse(ai, head, ai_list) {
  158. if (ai->ai_sync_gen >= sync_gen)
  159. continue;
  160. ai->ai_sync_gen = sync_gen;
  161. gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
  162. done = 0;
  163. break;
  164. }
  165. }
  166. gfs2_log_unlock(sdp);
  167. }
  168. static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
  169. {
  170. struct gfs2_ail *ai, *s;
  171. int ret;
  172. gfs2_log_lock(sdp);
  173. list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
  174. if (gfs2_ail1_empty_one(sdp, ai, flags))
  175. list_move(&ai->ai_list, &sdp->sd_ail2_list);
  176. else if (!(flags & DIO_ALL))
  177. break;
  178. }
  179. ret = list_empty(&sdp->sd_ail1_list);
  180. gfs2_log_unlock(sdp);
  181. return ret;
  182. }
  183. /**
  184. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  185. * @sdp: the filesystem
  186. * @ai: the AIL entry
  187. *
  188. */
  189. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  190. {
  191. struct list_head *head = &ai->ai_ail2_list;
  192. struct gfs2_bufdata *bd;
  193. while (!list_empty(head)) {
  194. bd = list_entry(head->prev, struct gfs2_bufdata,
  195. bd_ail_st_list);
  196. gfs2_assert(sdp, bd->bd_ail == ai);
  197. gfs2_remove_from_ail(bd);
  198. }
  199. }
  200. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  201. {
  202. struct gfs2_ail *ai, *safe;
  203. unsigned int old_tail = sdp->sd_log_tail;
  204. int wrap = (new_tail < old_tail);
  205. int a, b, rm;
  206. gfs2_log_lock(sdp);
  207. list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
  208. a = (old_tail <= ai->ai_first);
  209. b = (ai->ai_first < new_tail);
  210. rm = (wrap) ? (a || b) : (a && b);
  211. if (!rm)
  212. continue;
  213. gfs2_ail2_empty_one(sdp, ai);
  214. list_del(&ai->ai_list);
  215. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
  216. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
  217. kfree(ai);
  218. }
  219. gfs2_log_unlock(sdp);
  220. }
  221. /**
  222. * gfs2_log_reserve - Make a log reservation
  223. * @sdp: The GFS2 superblock
  224. * @blks: The number of blocks to reserve
  225. *
  226. * Note that we never give out the last few blocks of the journal. Thats
  227. * due to the fact that there is a small number of header blocks
  228. * associated with each log flush. The exact number can't be known until
  229. * flush time, so we ensure that we have just enough free blocks at all
  230. * times to avoid running out during a log flush.
  231. *
  232. * We no longer flush the log here, instead we wake up logd to do that
  233. * for us. To avoid the thundering herd and to ensure that we deal fairly
  234. * with queued waiters, we use an exclusive wait. This means that when we
  235. * get woken with enough journal space to get our reservation, we need to
  236. * wake the next waiter on the list.
  237. *
  238. * Returns: errno
  239. */
  240. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  241. {
  242. unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
  243. unsigned wanted = blks + reserved_blks;
  244. DEFINE_WAIT(wait);
  245. int did_wait = 0;
  246. unsigned int free_blocks;
  247. if (gfs2_assert_warn(sdp, blks) ||
  248. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  249. return -EINVAL;
  250. retry:
  251. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  252. if (unlikely(free_blocks <= wanted)) {
  253. do {
  254. prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
  255. TASK_UNINTERRUPTIBLE);
  256. wake_up(&sdp->sd_logd_waitq);
  257. did_wait = 1;
  258. if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
  259. io_schedule();
  260. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  261. } while(free_blocks <= wanted);
  262. finish_wait(&sdp->sd_log_waitq, &wait);
  263. }
  264. if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
  265. free_blocks - blks) != free_blocks)
  266. goto retry;
  267. trace_gfs2_log_blocks(sdp, -blks);
  268. /*
  269. * If we waited, then so might others, wake them up _after_ we get
  270. * our share of the log.
  271. */
  272. if (unlikely(did_wait))
  273. wake_up(&sdp->sd_log_waitq);
  274. down_read(&sdp->sd_log_flush_lock);
  275. return 0;
  276. }
  277. static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
  278. {
  279. struct gfs2_journal_extent *je;
  280. list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
  281. if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
  282. return je->dblock + lbn - je->lblock;
  283. }
  284. return -1;
  285. }
  286. /**
  287. * log_distance - Compute distance between two journal blocks
  288. * @sdp: The GFS2 superblock
  289. * @newer: The most recent journal block of the pair
  290. * @older: The older journal block of the pair
  291. *
  292. * Compute the distance (in the journal direction) between two
  293. * blocks in the journal
  294. *
  295. * Returns: the distance in blocks
  296. */
  297. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  298. unsigned int older)
  299. {
  300. int dist;
  301. dist = newer - older;
  302. if (dist < 0)
  303. dist += sdp->sd_jdesc->jd_blocks;
  304. return dist;
  305. }
  306. /**
  307. * calc_reserved - Calculate the number of blocks to reserve when
  308. * refunding a transaction's unused buffers.
  309. * @sdp: The GFS2 superblock
  310. *
  311. * This is complex. We need to reserve room for all our currently used
  312. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  313. * all our journaled data buffers for journaled files (e.g. files in the
  314. * meta_fs like rindex, or files for which chattr +j was done.)
  315. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  316. * will count it as free space (sd_log_blks_free) and corruption will follow.
  317. *
  318. * We can have metadata bufs and jdata bufs in the same journal. So each
  319. * type gets its own log header, for which we need to reserve a block.
  320. * In fact, each type has the potential for needing more than one header
  321. * in cases where we have more buffers than will fit on a journal page.
  322. * Metadata journal entries take up half the space of journaled buffer entries.
  323. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  324. * databuf_limit (251) before they cause a wrap around.
  325. *
  326. * Also, we need to reserve blocks for revoke journal entries and one for an
  327. * overall header for the lot.
  328. *
  329. * Returns: the number of blocks reserved
  330. */
  331. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  332. {
  333. unsigned int reserved = 0;
  334. unsigned int mbuf_limit, metabufhdrs_needed;
  335. unsigned int dbuf_limit, databufhdrs_needed;
  336. unsigned int revokes = 0;
  337. mbuf_limit = buf_limit(sdp);
  338. metabufhdrs_needed = (sdp->sd_log_commited_buf +
  339. (mbuf_limit - 1)) / mbuf_limit;
  340. dbuf_limit = databuf_limit(sdp);
  341. databufhdrs_needed = (sdp->sd_log_commited_databuf +
  342. (dbuf_limit - 1)) / dbuf_limit;
  343. if (sdp->sd_log_commited_revoke > 0)
  344. revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  345. sizeof(u64));
  346. reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
  347. sdp->sd_log_commited_databuf + databufhdrs_needed +
  348. revokes;
  349. /* One for the overall header */
  350. if (reserved)
  351. reserved++;
  352. return reserved;
  353. }
  354. static unsigned int current_tail(struct gfs2_sbd *sdp)
  355. {
  356. struct gfs2_ail *ai;
  357. unsigned int tail;
  358. gfs2_log_lock(sdp);
  359. if (list_empty(&sdp->sd_ail1_list)) {
  360. tail = sdp->sd_log_head;
  361. } else {
  362. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  363. tail = ai->ai_first;
  364. }
  365. gfs2_log_unlock(sdp);
  366. return tail;
  367. }
  368. void gfs2_log_incr_head(struct gfs2_sbd *sdp)
  369. {
  370. if (sdp->sd_log_flush_head == sdp->sd_log_tail)
  371. BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
  372. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  373. sdp->sd_log_flush_head = 0;
  374. sdp->sd_log_flush_wrapped = 1;
  375. }
  376. }
  377. /**
  378. * gfs2_log_write_endio - End of I/O for a log buffer
  379. * @bh: The buffer head
  380. * @uptodate: I/O Status
  381. *
  382. */
  383. static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
  384. {
  385. struct gfs2_sbd *sdp = bh->b_private;
  386. bh->b_private = NULL;
  387. end_buffer_write_sync(bh, uptodate);
  388. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  389. wake_up(&sdp->sd_log_flush_wait);
  390. }
  391. /**
  392. * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
  393. * @sdp: The GFS2 superblock
  394. *
  395. * Returns: the buffer_head
  396. */
  397. struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
  398. {
  399. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  400. struct buffer_head *bh;
  401. bh = sb_getblk(sdp->sd_vfs, blkno);
  402. lock_buffer(bh);
  403. memset(bh->b_data, 0, bh->b_size);
  404. set_buffer_uptodate(bh);
  405. clear_buffer_dirty(bh);
  406. gfs2_log_incr_head(sdp);
  407. atomic_inc(&sdp->sd_log_in_flight);
  408. bh->b_private = sdp;
  409. bh->b_end_io = gfs2_log_write_endio;
  410. return bh;
  411. }
  412. /**
  413. * gfs2_fake_write_endio -
  414. * @bh: The buffer head
  415. * @uptodate: The I/O Status
  416. *
  417. */
  418. static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
  419. {
  420. struct buffer_head *real_bh = bh->b_private;
  421. struct gfs2_bufdata *bd = real_bh->b_private;
  422. struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
  423. end_buffer_write_sync(bh, uptodate);
  424. free_buffer_head(bh);
  425. unlock_buffer(real_bh);
  426. brelse(real_bh);
  427. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  428. wake_up(&sdp->sd_log_flush_wait);
  429. }
  430. /**
  431. * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
  432. * @sdp: the filesystem
  433. * @data: the data the buffer_head should point to
  434. *
  435. * Returns: the log buffer descriptor
  436. */
  437. struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
  438. struct buffer_head *real)
  439. {
  440. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  441. struct buffer_head *bh;
  442. bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
  443. atomic_set(&bh->b_count, 1);
  444. bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
  445. set_bh_page(bh, real->b_page, bh_offset(real));
  446. bh->b_blocknr = blkno;
  447. bh->b_size = sdp->sd_sb.sb_bsize;
  448. bh->b_bdev = sdp->sd_vfs->s_bdev;
  449. bh->b_private = real;
  450. bh->b_end_io = gfs2_fake_write_endio;
  451. gfs2_log_incr_head(sdp);
  452. atomic_inc(&sdp->sd_log_in_flight);
  453. return bh;
  454. }
  455. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  456. {
  457. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  458. ail2_empty(sdp, new_tail);
  459. atomic_add(dist, &sdp->sd_log_blks_free);
  460. trace_gfs2_log_blocks(sdp, dist);
  461. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  462. sdp->sd_jdesc->jd_blocks);
  463. sdp->sd_log_tail = new_tail;
  464. }
  465. /**
  466. * log_write_header - Get and initialize a journal header buffer
  467. * @sdp: The GFS2 superblock
  468. *
  469. * Returns: the initialized log buffer descriptor
  470. */
  471. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  472. {
  473. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  474. struct buffer_head *bh;
  475. struct gfs2_log_header *lh;
  476. unsigned int tail;
  477. u32 hash;
  478. bh = sb_getblk(sdp->sd_vfs, blkno);
  479. lock_buffer(bh);
  480. memset(bh->b_data, 0, bh->b_size);
  481. set_buffer_uptodate(bh);
  482. clear_buffer_dirty(bh);
  483. gfs2_ail1_empty(sdp, 0);
  484. tail = current_tail(sdp);
  485. lh = (struct gfs2_log_header *)bh->b_data;
  486. memset(lh, 0, sizeof(struct gfs2_log_header));
  487. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  488. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  489. lh->lh_header.__pad0 = cpu_to_be64(0);
  490. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  491. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  492. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  493. lh->lh_flags = cpu_to_be32(flags);
  494. lh->lh_tail = cpu_to_be32(tail);
  495. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  496. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  497. lh->lh_hash = cpu_to_be32(hash);
  498. bh->b_end_io = end_buffer_write_sync;
  499. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
  500. goto skip_barrier;
  501. get_bh(bh);
  502. submit_bh(WRITE_BARRIER | REQ_META, bh);
  503. wait_on_buffer(bh);
  504. if (buffer_eopnotsupp(bh)) {
  505. clear_buffer_eopnotsupp(bh);
  506. set_buffer_uptodate(bh);
  507. fs_info(sdp, "barrier sync failed - disabling barriers\n");
  508. set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
  509. lock_buffer(bh);
  510. skip_barrier:
  511. get_bh(bh);
  512. submit_bh(WRITE_SYNC | REQ_META, bh);
  513. wait_on_buffer(bh);
  514. }
  515. if (!buffer_uptodate(bh))
  516. gfs2_io_error_bh(sdp, bh);
  517. brelse(bh);
  518. if (sdp->sd_log_tail != tail)
  519. log_pull_tail(sdp, tail);
  520. else
  521. gfs2_assert_withdraw(sdp, !pull);
  522. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  523. gfs2_log_incr_head(sdp);
  524. }
  525. static void log_flush_commit(struct gfs2_sbd *sdp)
  526. {
  527. DEFINE_WAIT(wait);
  528. if (atomic_read(&sdp->sd_log_in_flight)) {
  529. do {
  530. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  531. TASK_UNINTERRUPTIBLE);
  532. if (atomic_read(&sdp->sd_log_in_flight))
  533. io_schedule();
  534. } while(atomic_read(&sdp->sd_log_in_flight));
  535. finish_wait(&sdp->sd_log_flush_wait, &wait);
  536. }
  537. log_write_header(sdp, 0, 0);
  538. }
  539. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  540. {
  541. struct gfs2_bufdata *bd;
  542. struct buffer_head *bh;
  543. LIST_HEAD(written);
  544. gfs2_log_lock(sdp);
  545. while (!list_empty(&sdp->sd_log_le_ordered)) {
  546. bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
  547. list_move(&bd->bd_le.le_list, &written);
  548. bh = bd->bd_bh;
  549. if (!buffer_dirty(bh))
  550. continue;
  551. get_bh(bh);
  552. gfs2_log_unlock(sdp);
  553. lock_buffer(bh);
  554. if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
  555. bh->b_end_io = end_buffer_write_sync;
  556. submit_bh(WRITE_SYNC_PLUG, bh);
  557. } else {
  558. unlock_buffer(bh);
  559. brelse(bh);
  560. }
  561. gfs2_log_lock(sdp);
  562. }
  563. list_splice(&written, &sdp->sd_log_le_ordered);
  564. gfs2_log_unlock(sdp);
  565. }
  566. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  567. {
  568. struct gfs2_bufdata *bd;
  569. struct buffer_head *bh;
  570. gfs2_log_lock(sdp);
  571. while (!list_empty(&sdp->sd_log_le_ordered)) {
  572. bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
  573. bh = bd->bd_bh;
  574. if (buffer_locked(bh)) {
  575. get_bh(bh);
  576. gfs2_log_unlock(sdp);
  577. wait_on_buffer(bh);
  578. brelse(bh);
  579. gfs2_log_lock(sdp);
  580. continue;
  581. }
  582. list_del_init(&bd->bd_le.le_list);
  583. }
  584. gfs2_log_unlock(sdp);
  585. }
  586. /**
  587. * gfs2_log_flush - flush incore transaction(s)
  588. * @sdp: the filesystem
  589. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  590. *
  591. */
  592. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  593. {
  594. struct gfs2_ail *ai;
  595. down_write(&sdp->sd_log_flush_lock);
  596. /* Log might have been flushed while we waited for the flush lock */
  597. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  598. up_write(&sdp->sd_log_flush_lock);
  599. return;
  600. }
  601. trace_gfs2_log_flush(sdp, 1);
  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. trace_gfs2_log_blocks(sdp, -1);
  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. trace_gfs2_log_flush(sdp, 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. 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. trace_gfs2_log_blocks(sdp, unused);
  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. * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
  688. * or the total number of used blocks (pinned blocks plus AIL blocks)
  689. * is greater than thresh2.
  690. *
  691. * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
  692. * journal size.
  693. *
  694. * Returns: errno
  695. */
  696. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  697. {
  698. log_refund(sdp, tr);
  699. buf_lo_incore_commit(sdp, tr);
  700. up_read(&sdp->sd_log_flush_lock);
  701. if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
  702. ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
  703. atomic_read(&sdp->sd_log_thresh2)))
  704. wake_up(&sdp->sd_logd_waitq);
  705. }
  706. /**
  707. * gfs2_log_shutdown - write a shutdown header into a journal
  708. * @sdp: the filesystem
  709. *
  710. */
  711. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  712. {
  713. down_write(&sdp->sd_log_flush_lock);
  714. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  715. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  716. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  717. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  718. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  719. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  720. sdp->sd_log_flush_head = sdp->sd_log_head;
  721. sdp->sd_log_flush_wrapped = 0;
  722. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
  723. (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
  724. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  725. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  726. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  727. sdp->sd_log_head = sdp->sd_log_flush_head;
  728. sdp->sd_log_tail = sdp->sd_log_head;
  729. up_write(&sdp->sd_log_flush_lock);
  730. }
  731. /**
  732. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  733. * @sdp: the filesystem
  734. *
  735. */
  736. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  737. {
  738. gfs2_log_flush(sdp, NULL);
  739. for (;;) {
  740. gfs2_ail1_start(sdp);
  741. if (gfs2_ail1_empty(sdp, DIO_ALL))
  742. break;
  743. msleep(10);
  744. }
  745. }
  746. static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
  747. {
  748. return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
  749. }
  750. static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
  751. {
  752. unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
  753. return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
  754. }
  755. /**
  756. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  757. * @sdp: Pointer to GFS2 superblock
  758. *
  759. * Also, periodically check to make sure that we're using the most recent
  760. * journal index.
  761. */
  762. int gfs2_logd(void *data)
  763. {
  764. struct gfs2_sbd *sdp = data;
  765. unsigned long t = 1;
  766. DEFINE_WAIT(wait);
  767. unsigned preflush;
  768. while (!kthread_should_stop()) {
  769. preflush = atomic_read(&sdp->sd_log_pinned);
  770. if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
  771. gfs2_ail1_empty(sdp, DIO_ALL);
  772. gfs2_log_flush(sdp, NULL);
  773. gfs2_ail1_empty(sdp, DIO_ALL);
  774. }
  775. if (gfs2_ail_flush_reqd(sdp)) {
  776. gfs2_ail1_start(sdp);
  777. io_schedule();
  778. gfs2_ail1_empty(sdp, 0);
  779. gfs2_log_flush(sdp, NULL);
  780. gfs2_ail1_empty(sdp, DIO_ALL);
  781. }
  782. wake_up(&sdp->sd_log_waitq);
  783. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  784. if (freezing(current))
  785. refrigerator();
  786. do {
  787. prepare_to_wait(&sdp->sd_logd_waitq, &wait,
  788. TASK_INTERRUPTIBLE);
  789. if (!gfs2_ail_flush_reqd(sdp) &&
  790. !gfs2_jrnl_flush_reqd(sdp) &&
  791. !kthread_should_stop())
  792. t = schedule_timeout(t);
  793. } while(t && !gfs2_ail_flush_reqd(sdp) &&
  794. !gfs2_jrnl_flush_reqd(sdp) &&
  795. !kthread_should_stop());
  796. finish_wait(&sdp->sd_logd_waitq, &wait);
  797. }
  798. return 0;
  799. }