log.c 22 KB

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