log.c 24 KB

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