log.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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 <linux/writeback.h>
  21. #include <linux/list_sort.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "bmap.h"
  25. #include "glock.h"
  26. #include "log.h"
  27. #include "lops.h"
  28. #include "meta_io.h"
  29. #include "util.h"
  30. #include "dir.h"
  31. #include "trace_gfs2.h"
  32. /**
  33. * gfs2_struct2blk - compute stuff
  34. * @sdp: the filesystem
  35. * @nstruct: the number of structures
  36. * @ssize: the size of the structures
  37. *
  38. * Compute the number of log descriptor blocks needed to hold a certain number
  39. * of structures of a certain size.
  40. *
  41. * Returns: the number of blocks needed (minimum is always 1)
  42. */
  43. unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
  44. unsigned int ssize)
  45. {
  46. unsigned int blks;
  47. unsigned int first, second;
  48. blks = 1;
  49. first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
  50. if (nstruct > first) {
  51. second = (sdp->sd_sb.sb_bsize -
  52. sizeof(struct gfs2_meta_header)) / ssize;
  53. blks += DIV_ROUND_UP(nstruct - first, second);
  54. }
  55. return blks;
  56. }
  57. /**
  58. * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
  59. * @mapping: The associated mapping (maybe NULL)
  60. * @bd: The gfs2_bufdata to remove
  61. *
  62. * The ail lock _must_ be held when calling this function
  63. *
  64. */
  65. void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
  66. {
  67. bd->bd_tr = NULL;
  68. list_del_init(&bd->bd_ail_st_list);
  69. list_del_init(&bd->bd_ail_gl_list);
  70. atomic_dec(&bd->bd_gl->gl_ail_count);
  71. brelse(bd->bd_bh);
  72. }
  73. /**
  74. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  75. * @sdp: the filesystem
  76. * @wbc: The writeback control structure
  77. * @ai: The ail structure
  78. *
  79. */
  80. static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
  81. struct writeback_control *wbc,
  82. struct gfs2_trans *tr)
  83. __releases(&sdp->sd_ail_lock)
  84. __acquires(&sdp->sd_ail_lock)
  85. {
  86. struct gfs2_glock *gl = NULL;
  87. struct address_space *mapping;
  88. struct gfs2_bufdata *bd, *s;
  89. struct buffer_head *bh;
  90. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
  91. bh = bd->bd_bh;
  92. gfs2_assert(sdp, bd->bd_tr == tr);
  93. if (!buffer_busy(bh)) {
  94. if (!buffer_uptodate(bh))
  95. gfs2_io_error_bh(sdp, bh);
  96. list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
  97. continue;
  98. }
  99. if (!buffer_dirty(bh))
  100. continue;
  101. if (gl == bd->bd_gl)
  102. continue;
  103. gl = bd->bd_gl;
  104. list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
  105. mapping = bh->b_page->mapping;
  106. if (!mapping)
  107. continue;
  108. spin_unlock(&sdp->sd_ail_lock);
  109. generic_writepages(mapping, wbc);
  110. spin_lock(&sdp->sd_ail_lock);
  111. if (wbc->nr_to_write <= 0)
  112. break;
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. /**
  118. * gfs2_ail1_flush - start writeback of some ail1 entries
  119. * @sdp: The super block
  120. * @wbc: The writeback control structure
  121. *
  122. * Writes back some ail1 entries, according to the limits in the
  123. * writeback control structure
  124. */
  125. void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
  126. {
  127. struct list_head *head = &sdp->sd_ail1_list;
  128. struct gfs2_trans *tr;
  129. trace_gfs2_ail_flush(sdp, wbc, 1);
  130. spin_lock(&sdp->sd_ail_lock);
  131. restart:
  132. list_for_each_entry_reverse(tr, head, tr_list) {
  133. if (wbc->nr_to_write <= 0)
  134. break;
  135. if (gfs2_ail1_start_one(sdp, wbc, tr))
  136. goto restart;
  137. }
  138. spin_unlock(&sdp->sd_ail_lock);
  139. trace_gfs2_ail_flush(sdp, wbc, 0);
  140. }
  141. /**
  142. * gfs2_ail1_start - start writeback of all ail1 entries
  143. * @sdp: The superblock
  144. */
  145. static void gfs2_ail1_start(struct gfs2_sbd *sdp)
  146. {
  147. struct writeback_control wbc = {
  148. .sync_mode = WB_SYNC_NONE,
  149. .nr_to_write = LONG_MAX,
  150. .range_start = 0,
  151. .range_end = LLONG_MAX,
  152. };
  153. return gfs2_ail1_flush(sdp, &wbc);
  154. }
  155. /**
  156. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  157. * @sdp: the filesystem
  158. * @ai: the AIL entry
  159. *
  160. */
  161. static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  162. {
  163. struct gfs2_bufdata *bd, *s;
  164. struct buffer_head *bh;
  165. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
  166. bd_ail_st_list) {
  167. bh = bd->bd_bh;
  168. gfs2_assert(sdp, bd->bd_tr == tr);
  169. if (buffer_busy(bh))
  170. continue;
  171. if (!buffer_uptodate(bh))
  172. gfs2_io_error_bh(sdp, bh);
  173. list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
  174. }
  175. }
  176. /**
  177. * gfs2_ail1_empty - Try to empty the ail1 lists
  178. * @sdp: The superblock
  179. *
  180. * Tries to empty the ail1 lists, starting with the oldest first
  181. */
  182. static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
  183. {
  184. struct gfs2_trans *tr, *s;
  185. int ret;
  186. spin_lock(&sdp->sd_ail_lock);
  187. list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
  188. gfs2_ail1_empty_one(sdp, tr);
  189. if (list_empty(&tr->tr_ail1_list))
  190. list_move(&tr->tr_list, &sdp->sd_ail2_list);
  191. else
  192. break;
  193. }
  194. ret = list_empty(&sdp->sd_ail1_list);
  195. spin_unlock(&sdp->sd_ail_lock);
  196. return ret;
  197. }
  198. static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
  199. {
  200. struct gfs2_trans *tr;
  201. struct gfs2_bufdata *bd;
  202. struct buffer_head *bh;
  203. spin_lock(&sdp->sd_ail_lock);
  204. list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
  205. list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
  206. bh = bd->bd_bh;
  207. if (!buffer_locked(bh))
  208. continue;
  209. get_bh(bh);
  210. spin_unlock(&sdp->sd_ail_lock);
  211. wait_on_buffer(bh);
  212. brelse(bh);
  213. return;
  214. }
  215. }
  216. spin_unlock(&sdp->sd_ail_lock);
  217. }
  218. /**
  219. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  220. * @sdp: the filesystem
  221. * @ai: the AIL entry
  222. *
  223. */
  224. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  225. {
  226. struct list_head *head = &tr->tr_ail2_list;
  227. struct gfs2_bufdata *bd;
  228. while (!list_empty(head)) {
  229. bd = list_entry(head->prev, struct gfs2_bufdata,
  230. bd_ail_st_list);
  231. gfs2_assert(sdp, bd->bd_tr == tr);
  232. gfs2_remove_from_ail(bd);
  233. }
  234. }
  235. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  236. {
  237. struct gfs2_trans *tr, *safe;
  238. unsigned int old_tail = sdp->sd_log_tail;
  239. int wrap = (new_tail < old_tail);
  240. int a, b, rm;
  241. spin_lock(&sdp->sd_ail_lock);
  242. list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
  243. a = (old_tail <= tr->tr_first);
  244. b = (tr->tr_first < new_tail);
  245. rm = (wrap) ? (a || b) : (a && b);
  246. if (!rm)
  247. continue;
  248. gfs2_ail2_empty_one(sdp, tr);
  249. list_del(&tr->tr_list);
  250. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
  251. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
  252. kfree(tr);
  253. }
  254. spin_unlock(&sdp->sd_ail_lock);
  255. }
  256. /**
  257. * gfs2_log_reserve - Make a log reservation
  258. * @sdp: The GFS2 superblock
  259. * @blks: The number of blocks to reserve
  260. *
  261. * Note that we never give out the last few blocks of the journal. Thats
  262. * due to the fact that there is a small number of header blocks
  263. * associated with each log flush. The exact number can't be known until
  264. * flush time, so we ensure that we have just enough free blocks at all
  265. * times to avoid running out during a log flush.
  266. *
  267. * We no longer flush the log here, instead we wake up logd to do that
  268. * for us. To avoid the thundering herd and to ensure that we deal fairly
  269. * with queued waiters, we use an exclusive wait. This means that when we
  270. * get woken with enough journal space to get our reservation, we need to
  271. * wake the next waiter on the list.
  272. *
  273. * Returns: errno
  274. */
  275. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  276. {
  277. unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
  278. unsigned wanted = blks + reserved_blks;
  279. DEFINE_WAIT(wait);
  280. int did_wait = 0;
  281. unsigned int free_blocks;
  282. if (gfs2_assert_warn(sdp, blks) ||
  283. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  284. return -EINVAL;
  285. retry:
  286. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  287. if (unlikely(free_blocks <= wanted)) {
  288. do {
  289. prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
  290. TASK_UNINTERRUPTIBLE);
  291. wake_up(&sdp->sd_logd_waitq);
  292. did_wait = 1;
  293. if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
  294. io_schedule();
  295. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  296. } while(free_blocks <= wanted);
  297. finish_wait(&sdp->sd_log_waitq, &wait);
  298. }
  299. if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
  300. free_blocks - blks) != free_blocks)
  301. goto retry;
  302. trace_gfs2_log_blocks(sdp, -blks);
  303. /*
  304. * If we waited, then so might others, wake them up _after_ we get
  305. * our share of the log.
  306. */
  307. if (unlikely(did_wait))
  308. wake_up(&sdp->sd_log_waitq);
  309. down_read(&sdp->sd_log_flush_lock);
  310. return 0;
  311. }
  312. /**
  313. * log_distance - Compute distance between two journal blocks
  314. * @sdp: The GFS2 superblock
  315. * @newer: The most recent journal block of the pair
  316. * @older: The older journal block of the pair
  317. *
  318. * Compute the distance (in the journal direction) between two
  319. * blocks in the journal
  320. *
  321. * Returns: the distance in blocks
  322. */
  323. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  324. unsigned int older)
  325. {
  326. int dist;
  327. dist = newer - older;
  328. if (dist < 0)
  329. dist += sdp->sd_jdesc->jd_blocks;
  330. return dist;
  331. }
  332. /**
  333. * calc_reserved - Calculate the number of blocks to reserve when
  334. * refunding a transaction's unused buffers.
  335. * @sdp: The GFS2 superblock
  336. *
  337. * This is complex. We need to reserve room for all our currently used
  338. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  339. * all our journaled data buffers for journaled files (e.g. files in the
  340. * meta_fs like rindex, or files for which chattr +j was done.)
  341. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  342. * will count it as free space (sd_log_blks_free) and corruption will follow.
  343. *
  344. * We can have metadata bufs and jdata bufs in the same journal. So each
  345. * type gets its own log header, for which we need to reserve a block.
  346. * In fact, each type has the potential for needing more than one header
  347. * in cases where we have more buffers than will fit on a journal page.
  348. * Metadata journal entries take up half the space of journaled buffer entries.
  349. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  350. * databuf_limit (251) before they cause a wrap around.
  351. *
  352. * Also, we need to reserve blocks for revoke journal entries and one for an
  353. * overall header for the lot.
  354. *
  355. * Returns: the number of blocks reserved
  356. */
  357. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  358. {
  359. unsigned int reserved = 0;
  360. unsigned int mbuf_limit, metabufhdrs_needed;
  361. unsigned int dbuf_limit, databufhdrs_needed;
  362. unsigned int revokes = 0;
  363. mbuf_limit = buf_limit(sdp);
  364. metabufhdrs_needed = (sdp->sd_log_commited_buf +
  365. (mbuf_limit - 1)) / mbuf_limit;
  366. dbuf_limit = databuf_limit(sdp);
  367. databufhdrs_needed = (sdp->sd_log_commited_databuf +
  368. (dbuf_limit - 1)) / dbuf_limit;
  369. if (sdp->sd_log_commited_revoke > 0)
  370. revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  371. sizeof(u64));
  372. reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
  373. sdp->sd_log_commited_databuf + databufhdrs_needed +
  374. revokes;
  375. /* One for the overall header */
  376. if (reserved)
  377. reserved++;
  378. return reserved;
  379. }
  380. static unsigned int current_tail(struct gfs2_sbd *sdp)
  381. {
  382. struct gfs2_trans *tr;
  383. unsigned int tail;
  384. spin_lock(&sdp->sd_ail_lock);
  385. if (list_empty(&sdp->sd_ail1_list)) {
  386. tail = sdp->sd_log_head;
  387. } else {
  388. tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
  389. tr_list);
  390. tail = tr->tr_first;
  391. }
  392. spin_unlock(&sdp->sd_ail_lock);
  393. return tail;
  394. }
  395. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  396. {
  397. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  398. ail2_empty(sdp, new_tail);
  399. atomic_add(dist, &sdp->sd_log_blks_free);
  400. trace_gfs2_log_blocks(sdp, dist);
  401. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  402. sdp->sd_jdesc->jd_blocks);
  403. sdp->sd_log_tail = new_tail;
  404. }
  405. static void log_flush_wait(struct gfs2_sbd *sdp)
  406. {
  407. DEFINE_WAIT(wait);
  408. if (atomic_read(&sdp->sd_log_in_flight)) {
  409. do {
  410. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  411. TASK_UNINTERRUPTIBLE);
  412. if (atomic_read(&sdp->sd_log_in_flight))
  413. io_schedule();
  414. } while(atomic_read(&sdp->sd_log_in_flight));
  415. finish_wait(&sdp->sd_log_flush_wait, &wait);
  416. }
  417. }
  418. static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
  419. {
  420. struct gfs2_inode *ipa, *ipb;
  421. ipa = list_entry(a, struct gfs2_inode, i_ordered);
  422. ipb = list_entry(b, struct gfs2_inode, i_ordered);
  423. if (ipa->i_no_addr < ipb->i_no_addr)
  424. return -1;
  425. if (ipa->i_no_addr > ipb->i_no_addr)
  426. return 1;
  427. return 0;
  428. }
  429. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  430. {
  431. struct gfs2_inode *ip;
  432. LIST_HEAD(written);
  433. spin_lock(&sdp->sd_ordered_lock);
  434. list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
  435. while (!list_empty(&sdp->sd_log_le_ordered)) {
  436. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  437. list_move(&ip->i_ordered, &written);
  438. if (ip->i_inode.i_mapping->nrpages == 0)
  439. continue;
  440. spin_unlock(&sdp->sd_ordered_lock);
  441. filemap_fdatawrite(ip->i_inode.i_mapping);
  442. spin_lock(&sdp->sd_ordered_lock);
  443. }
  444. list_splice(&written, &sdp->sd_log_le_ordered);
  445. spin_unlock(&sdp->sd_ordered_lock);
  446. }
  447. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  448. {
  449. struct gfs2_inode *ip;
  450. spin_lock(&sdp->sd_ordered_lock);
  451. while (!list_empty(&sdp->sd_log_le_ordered)) {
  452. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  453. list_del(&ip->i_ordered);
  454. WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
  455. if (ip->i_inode.i_mapping->nrpages == 0)
  456. continue;
  457. spin_unlock(&sdp->sd_ordered_lock);
  458. filemap_fdatawait(ip->i_inode.i_mapping);
  459. spin_lock(&sdp->sd_ordered_lock);
  460. }
  461. spin_unlock(&sdp->sd_ordered_lock);
  462. }
  463. void gfs2_ordered_del_inode(struct gfs2_inode *ip)
  464. {
  465. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  466. spin_lock(&sdp->sd_ordered_lock);
  467. if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
  468. list_del(&ip->i_ordered);
  469. spin_unlock(&sdp->sd_ordered_lock);
  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)
  478. {
  479. struct gfs2_log_header *lh;
  480. unsigned int tail;
  481. u32 hash;
  482. int rw = WRITE_FLUSH_FUA | REQ_META;
  483. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  484. lh = page_address(page);
  485. clear_page(lh);
  486. gfs2_ail1_empty(sdp);
  487. tail = current_tail(sdp);
  488. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  489. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  490. lh->lh_header.__pad0 = cpu_to_be64(0);
  491. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  492. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  493. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  494. lh->lh_flags = cpu_to_be32(flags);
  495. lh->lh_tail = cpu_to_be32(tail);
  496. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  497. hash = gfs2_disk_hash(page_address(page), sizeof(struct gfs2_log_header));
  498. lh->lh_hash = cpu_to_be32(hash);
  499. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
  500. gfs2_ordered_wait(sdp);
  501. log_flush_wait(sdp);
  502. rw = WRITE_SYNC | REQ_META | REQ_PRIO;
  503. }
  504. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  505. gfs2_log_write_page(sdp, page);
  506. gfs2_log_flush_bio(sdp, rw);
  507. log_flush_wait(sdp);
  508. if (sdp->sd_log_tail != tail)
  509. log_pull_tail(sdp, tail);
  510. }
  511. /**
  512. * gfs2_log_flush - flush incore transaction(s)
  513. * @sdp: the filesystem
  514. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  515. *
  516. */
  517. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  518. {
  519. struct gfs2_trans *tr;
  520. down_write(&sdp->sd_log_flush_lock);
  521. /* Log might have been flushed while we waited for the flush lock */
  522. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  523. up_write(&sdp->sd_log_flush_lock);
  524. return;
  525. }
  526. trace_gfs2_log_flush(sdp, 1);
  527. tr = sdp->sd_log_tr;
  528. if (tr) {
  529. sdp->sd_log_tr = NULL;
  530. INIT_LIST_HEAD(&tr->tr_ail1_list);
  531. INIT_LIST_HEAD(&tr->tr_ail2_list);
  532. }
  533. if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
  534. printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
  535. sdp->sd_log_commited_buf);
  536. gfs2_assert_withdraw(sdp, 0);
  537. }
  538. if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
  539. printk(KERN_INFO "GFS2: log databuf %u %u\n",
  540. sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
  541. gfs2_assert_withdraw(sdp, 0);
  542. }
  543. gfs2_assert_withdraw(sdp,
  544. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  545. sdp->sd_log_flush_head = sdp->sd_log_head;
  546. sdp->sd_log_flush_wrapped = 0;
  547. if (tr)
  548. tr->tr_first = sdp->sd_log_flush_head;
  549. gfs2_ordered_write(sdp);
  550. lops_before_commit(sdp);
  551. gfs2_log_flush_bio(sdp, WRITE);
  552. if (sdp->sd_log_head != sdp->sd_log_flush_head) {
  553. log_write_header(sdp, 0);
  554. } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  555. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  556. trace_gfs2_log_blocks(sdp, -1);
  557. log_write_header(sdp, 0);
  558. }
  559. lops_after_commit(sdp, tr);
  560. gfs2_log_lock(sdp);
  561. sdp->sd_log_head = sdp->sd_log_flush_head;
  562. sdp->sd_log_blks_reserved = 0;
  563. sdp->sd_log_commited_buf = 0;
  564. sdp->sd_log_commited_databuf = 0;
  565. sdp->sd_log_commited_revoke = 0;
  566. spin_lock(&sdp->sd_ail_lock);
  567. if (tr && !list_empty(&tr->tr_ail1_list)) {
  568. list_add(&tr->tr_list, &sdp->sd_ail1_list);
  569. tr = NULL;
  570. }
  571. spin_unlock(&sdp->sd_ail_lock);
  572. gfs2_log_unlock(sdp);
  573. trace_gfs2_log_flush(sdp, 0);
  574. up_write(&sdp->sd_log_flush_lock);
  575. kfree(tr);
  576. }
  577. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  578. {
  579. unsigned int reserved;
  580. unsigned int unused;
  581. gfs2_log_lock(sdp);
  582. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  583. sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
  584. tr->tr_num_databuf_rm;
  585. gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
  586. (((int)sdp->sd_log_commited_databuf) >= 0));
  587. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  588. reserved = calc_reserved(sdp);
  589. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
  590. unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
  591. atomic_add(unused, &sdp->sd_log_blks_free);
  592. trace_gfs2_log_blocks(sdp, unused);
  593. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  594. sdp->sd_jdesc->jd_blocks);
  595. sdp->sd_log_blks_reserved = reserved;
  596. if (sdp->sd_log_tr == NULL &&
  597. (tr->tr_num_buf_new || tr->tr_num_databuf_new)) {
  598. gfs2_assert_withdraw(sdp, tr->tr_t_gh.gh_gl);
  599. sdp->sd_log_tr = tr;
  600. tr->tr_attached = 1;
  601. }
  602. gfs2_log_unlock(sdp);
  603. }
  604. /**
  605. * gfs2_log_commit - Commit a transaction to the log
  606. * @sdp: the filesystem
  607. * @tr: the transaction
  608. *
  609. * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
  610. * or the total number of used blocks (pinned blocks plus AIL blocks)
  611. * is greater than thresh2.
  612. *
  613. * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
  614. * journal size.
  615. *
  616. * Returns: errno
  617. */
  618. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  619. {
  620. log_refund(sdp, tr);
  621. if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
  622. ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
  623. atomic_read(&sdp->sd_log_thresh2)))
  624. wake_up(&sdp->sd_logd_waitq);
  625. }
  626. /**
  627. * gfs2_log_shutdown - write a shutdown header into a journal
  628. * @sdp: the filesystem
  629. *
  630. */
  631. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  632. {
  633. down_write(&sdp->sd_log_flush_lock);
  634. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  635. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  636. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  637. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  638. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  639. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  640. sdp->sd_log_flush_head = sdp->sd_log_head;
  641. sdp->sd_log_flush_wrapped = 0;
  642. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT);
  643. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  644. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  645. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  646. sdp->sd_log_head = sdp->sd_log_flush_head;
  647. sdp->sd_log_tail = sdp->sd_log_head;
  648. up_write(&sdp->sd_log_flush_lock);
  649. }
  650. /**
  651. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  652. * @sdp: the filesystem
  653. *
  654. */
  655. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  656. {
  657. gfs2_log_flush(sdp, NULL);
  658. for (;;) {
  659. gfs2_ail1_start(sdp);
  660. gfs2_ail1_wait(sdp);
  661. if (gfs2_ail1_empty(sdp))
  662. break;
  663. }
  664. gfs2_log_flush(sdp, NULL);
  665. }
  666. static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
  667. {
  668. return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
  669. }
  670. static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
  671. {
  672. unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
  673. return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
  674. }
  675. /**
  676. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  677. * @sdp: Pointer to GFS2 superblock
  678. *
  679. * Also, periodically check to make sure that we're using the most recent
  680. * journal index.
  681. */
  682. int gfs2_logd(void *data)
  683. {
  684. struct gfs2_sbd *sdp = data;
  685. unsigned long t = 1;
  686. DEFINE_WAIT(wait);
  687. while (!kthread_should_stop()) {
  688. if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
  689. gfs2_ail1_empty(sdp);
  690. gfs2_log_flush(sdp, NULL);
  691. }
  692. if (gfs2_ail_flush_reqd(sdp)) {
  693. gfs2_ail1_start(sdp);
  694. gfs2_ail1_wait(sdp);
  695. gfs2_ail1_empty(sdp);
  696. gfs2_log_flush(sdp, NULL);
  697. }
  698. if (!gfs2_ail_flush_reqd(sdp))
  699. wake_up(&sdp->sd_log_waitq);
  700. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  701. try_to_freeze();
  702. do {
  703. prepare_to_wait(&sdp->sd_logd_waitq, &wait,
  704. TASK_INTERRUPTIBLE);
  705. if (!gfs2_ail_flush_reqd(sdp) &&
  706. !gfs2_jrnl_flush_reqd(sdp) &&
  707. !kthread_should_stop())
  708. t = schedule_timeout(t);
  709. } while(t && !gfs2_ail_flush_reqd(sdp) &&
  710. !gfs2_jrnl_flush_reqd(sdp) &&
  711. !kthread_should_stop());
  712. finish_wait(&sdp->sd_logd_waitq, &wait);
  713. }
  714. return 0;
  715. }