log.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 oldest_tr = 1;
  186. int ret;
  187. spin_lock(&sdp->sd_ail_lock);
  188. list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
  189. gfs2_ail1_empty_one(sdp, tr);
  190. if (list_empty(&tr->tr_ail1_list) && oldest_tr)
  191. list_move(&tr->tr_list, &sdp->sd_ail2_list);
  192. else
  193. oldest_tr = 0;
  194. }
  195. ret = list_empty(&sdp->sd_ail1_list);
  196. spin_unlock(&sdp->sd_ail_lock);
  197. return ret;
  198. }
  199. static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
  200. {
  201. struct gfs2_trans *tr;
  202. struct gfs2_bufdata *bd;
  203. struct buffer_head *bh;
  204. spin_lock(&sdp->sd_ail_lock);
  205. list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
  206. list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
  207. bh = bd->bd_bh;
  208. if (!buffer_locked(bh))
  209. continue;
  210. get_bh(bh);
  211. spin_unlock(&sdp->sd_ail_lock);
  212. wait_on_buffer(bh);
  213. brelse(bh);
  214. return;
  215. }
  216. }
  217. spin_unlock(&sdp->sd_ail_lock);
  218. }
  219. /**
  220. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  221. * @sdp: the filesystem
  222. * @ai: the AIL entry
  223. *
  224. */
  225. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  226. {
  227. struct list_head *head = &tr->tr_ail2_list;
  228. struct gfs2_bufdata *bd;
  229. while (!list_empty(head)) {
  230. bd = list_entry(head->prev, struct gfs2_bufdata,
  231. bd_ail_st_list);
  232. gfs2_assert(sdp, bd->bd_tr == tr);
  233. gfs2_remove_from_ail(bd);
  234. }
  235. }
  236. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  237. {
  238. struct gfs2_trans *tr, *safe;
  239. unsigned int old_tail = sdp->sd_log_tail;
  240. int wrap = (new_tail < old_tail);
  241. int a, b, rm;
  242. spin_lock(&sdp->sd_ail_lock);
  243. list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
  244. a = (old_tail <= tr->tr_first);
  245. b = (tr->tr_first < new_tail);
  246. rm = (wrap) ? (a || b) : (a && b);
  247. if (!rm)
  248. continue;
  249. gfs2_ail2_empty_one(sdp, tr);
  250. list_del(&tr->tr_list);
  251. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
  252. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
  253. kfree(tr);
  254. }
  255. spin_unlock(&sdp->sd_ail_lock);
  256. }
  257. /**
  258. * gfs2_log_reserve - Make a log reservation
  259. * @sdp: The GFS2 superblock
  260. * @blks: The number of blocks to reserve
  261. *
  262. * Note that we never give out the last few blocks of the journal. Thats
  263. * due to the fact that there is a small number of header blocks
  264. * associated with each log flush. The exact number can't be known until
  265. * flush time, so we ensure that we have just enough free blocks at all
  266. * times to avoid running out during a log flush.
  267. *
  268. * We no longer flush the log here, instead we wake up logd to do that
  269. * for us. To avoid the thundering herd and to ensure that we deal fairly
  270. * with queued waiters, we use an exclusive wait. This means that when we
  271. * get woken with enough journal space to get our reservation, we need to
  272. * wake the next waiter on the list.
  273. *
  274. * Returns: errno
  275. */
  276. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  277. {
  278. unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
  279. unsigned wanted = blks + reserved_blks;
  280. DEFINE_WAIT(wait);
  281. int did_wait = 0;
  282. unsigned int free_blocks;
  283. if (gfs2_assert_warn(sdp, blks) ||
  284. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  285. return -EINVAL;
  286. retry:
  287. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  288. if (unlikely(free_blocks <= wanted)) {
  289. do {
  290. prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
  291. TASK_UNINTERRUPTIBLE);
  292. wake_up(&sdp->sd_logd_waitq);
  293. did_wait = 1;
  294. if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
  295. io_schedule();
  296. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  297. } while(free_blocks <= wanted);
  298. finish_wait(&sdp->sd_log_waitq, &wait);
  299. }
  300. if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
  301. free_blocks - blks) != free_blocks)
  302. goto retry;
  303. trace_gfs2_log_blocks(sdp, -blks);
  304. /*
  305. * If we waited, then so might others, wake them up _after_ we get
  306. * our share of the log.
  307. */
  308. if (unlikely(did_wait))
  309. wake_up(&sdp->sd_log_waitq);
  310. down_read(&sdp->sd_log_flush_lock);
  311. return 0;
  312. }
  313. /**
  314. * log_distance - Compute distance between two journal blocks
  315. * @sdp: The GFS2 superblock
  316. * @newer: The most recent journal block of the pair
  317. * @older: The older journal block of the pair
  318. *
  319. * Compute the distance (in the journal direction) between two
  320. * blocks in the journal
  321. *
  322. * Returns: the distance in blocks
  323. */
  324. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  325. unsigned int older)
  326. {
  327. int dist;
  328. dist = newer - older;
  329. if (dist < 0)
  330. dist += sdp->sd_jdesc->jd_blocks;
  331. return dist;
  332. }
  333. /**
  334. * calc_reserved - Calculate the number of blocks to reserve when
  335. * refunding a transaction's unused buffers.
  336. * @sdp: The GFS2 superblock
  337. *
  338. * This is complex. We need to reserve room for all our currently used
  339. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  340. * all our journaled data buffers for journaled files (e.g. files in the
  341. * meta_fs like rindex, or files for which chattr +j was done.)
  342. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  343. * will count it as free space (sd_log_blks_free) and corruption will follow.
  344. *
  345. * We can have metadata bufs and jdata bufs in the same journal. So each
  346. * type gets its own log header, for which we need to reserve a block.
  347. * In fact, each type has the potential for needing more than one header
  348. * in cases where we have more buffers than will fit on a journal page.
  349. * Metadata journal entries take up half the space of journaled buffer entries.
  350. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  351. * databuf_limit (251) before they cause a wrap around.
  352. *
  353. * Also, we need to reserve blocks for revoke journal entries and one for an
  354. * overall header for the lot.
  355. *
  356. * Returns: the number of blocks reserved
  357. */
  358. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  359. {
  360. unsigned int reserved = 0;
  361. unsigned int mbuf_limit, metabufhdrs_needed;
  362. unsigned int dbuf_limit, databufhdrs_needed;
  363. unsigned int revokes = 0;
  364. mbuf_limit = buf_limit(sdp);
  365. metabufhdrs_needed = (sdp->sd_log_commited_buf +
  366. (mbuf_limit - 1)) / mbuf_limit;
  367. dbuf_limit = databuf_limit(sdp);
  368. databufhdrs_needed = (sdp->sd_log_commited_databuf +
  369. (dbuf_limit - 1)) / dbuf_limit;
  370. if (sdp->sd_log_commited_revoke > 0)
  371. revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  372. sizeof(u64));
  373. reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
  374. sdp->sd_log_commited_databuf + databufhdrs_needed +
  375. revokes;
  376. /* One for the overall header */
  377. if (reserved)
  378. reserved++;
  379. return reserved;
  380. }
  381. static unsigned int current_tail(struct gfs2_sbd *sdp)
  382. {
  383. struct gfs2_trans *tr;
  384. unsigned int tail;
  385. spin_lock(&sdp->sd_ail_lock);
  386. if (list_empty(&sdp->sd_ail1_list)) {
  387. tail = sdp->sd_log_head;
  388. } else {
  389. tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
  390. tr_list);
  391. tail = tr->tr_first;
  392. }
  393. spin_unlock(&sdp->sd_ail_lock);
  394. return tail;
  395. }
  396. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  397. {
  398. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  399. ail2_empty(sdp, new_tail);
  400. atomic_add(dist, &sdp->sd_log_blks_free);
  401. trace_gfs2_log_blocks(sdp, dist);
  402. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  403. sdp->sd_jdesc->jd_blocks);
  404. sdp->sd_log_tail = new_tail;
  405. }
  406. static void log_flush_wait(struct gfs2_sbd *sdp)
  407. {
  408. DEFINE_WAIT(wait);
  409. if (atomic_read(&sdp->sd_log_in_flight)) {
  410. do {
  411. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  412. TASK_UNINTERRUPTIBLE);
  413. if (atomic_read(&sdp->sd_log_in_flight))
  414. io_schedule();
  415. } while(atomic_read(&sdp->sd_log_in_flight));
  416. finish_wait(&sdp->sd_log_flush_wait, &wait);
  417. }
  418. }
  419. static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
  420. {
  421. struct gfs2_inode *ipa, *ipb;
  422. ipa = list_entry(a, struct gfs2_inode, i_ordered);
  423. ipb = list_entry(b, struct gfs2_inode, i_ordered);
  424. if (ipa->i_no_addr < ipb->i_no_addr)
  425. return -1;
  426. if (ipa->i_no_addr > ipb->i_no_addr)
  427. return 1;
  428. return 0;
  429. }
  430. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  431. {
  432. struct gfs2_inode *ip;
  433. LIST_HEAD(written);
  434. spin_lock(&sdp->sd_ordered_lock);
  435. list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
  436. while (!list_empty(&sdp->sd_log_le_ordered)) {
  437. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  438. list_move(&ip->i_ordered, &written);
  439. if (ip->i_inode.i_mapping->nrpages == 0)
  440. continue;
  441. spin_unlock(&sdp->sd_ordered_lock);
  442. filemap_fdatawrite(ip->i_inode.i_mapping);
  443. spin_lock(&sdp->sd_ordered_lock);
  444. }
  445. list_splice(&written, &sdp->sd_log_le_ordered);
  446. spin_unlock(&sdp->sd_ordered_lock);
  447. }
  448. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  449. {
  450. struct gfs2_inode *ip;
  451. spin_lock(&sdp->sd_ordered_lock);
  452. while (!list_empty(&sdp->sd_log_le_ordered)) {
  453. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  454. list_del(&ip->i_ordered);
  455. WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
  456. if (ip->i_inode.i_mapping->nrpages == 0)
  457. continue;
  458. spin_unlock(&sdp->sd_ordered_lock);
  459. filemap_fdatawait(ip->i_inode.i_mapping);
  460. spin_lock(&sdp->sd_ordered_lock);
  461. }
  462. spin_unlock(&sdp->sd_ordered_lock);
  463. }
  464. void gfs2_ordered_del_inode(struct gfs2_inode *ip)
  465. {
  466. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  467. spin_lock(&sdp->sd_ordered_lock);
  468. if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
  469. list_del(&ip->i_ordered);
  470. spin_unlock(&sdp->sd_ordered_lock);
  471. }
  472. void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  473. {
  474. struct buffer_head *bh = bd->bd_bh;
  475. struct gfs2_glock *gl = bd->bd_gl;
  476. gfs2_remove_from_ail(bd);
  477. bd->bd_bh = NULL;
  478. bh->b_private = NULL;
  479. bd->bd_blkno = bh->b_blocknr;
  480. bd->bd_ops = &gfs2_revoke_lops;
  481. sdp->sd_log_num_revoke++;
  482. atomic_inc(&gl->gl_revokes);
  483. set_bit(GLF_LFLUSH, &gl->gl_flags);
  484. list_add(&bd->bd_list, &sdp->sd_log_le_revoke);
  485. }
  486. void gfs2_write_revokes(struct gfs2_sbd *sdp)
  487. {
  488. struct gfs2_trans *tr;
  489. struct gfs2_bufdata *bd, *tmp;
  490. int have_revokes = 0;
  491. int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
  492. gfs2_ail1_empty(sdp);
  493. spin_lock(&sdp->sd_ail_lock);
  494. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  495. list_for_each_entry(bd, &tr->tr_ail2_list, bd_ail_st_list) {
  496. if (list_empty(&bd->bd_list)) {
  497. have_revokes = 1;
  498. goto done;
  499. }
  500. }
  501. }
  502. done:
  503. spin_unlock(&sdp->sd_ail_lock);
  504. if (have_revokes == 0)
  505. return;
  506. while (sdp->sd_log_num_revoke > max_revokes)
  507. max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
  508. max_revokes -= sdp->sd_log_num_revoke;
  509. if (!sdp->sd_log_num_revoke) {
  510. atomic_dec(&sdp->sd_log_blks_free);
  511. /* If no blocks have been reserved, we need to also
  512. * reserve a block for the header */
  513. if (!sdp->sd_log_blks_reserved)
  514. atomic_dec(&sdp->sd_log_blks_free);
  515. }
  516. gfs2_log_lock(sdp);
  517. spin_lock(&sdp->sd_ail_lock);
  518. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  519. list_for_each_entry_safe(bd, tmp, &tr->tr_ail2_list, bd_ail_st_list) {
  520. if (max_revokes == 0)
  521. goto out_of_blocks;
  522. if (!list_empty(&bd->bd_list))
  523. continue;
  524. gfs2_add_revoke(sdp, bd);
  525. max_revokes--;
  526. }
  527. }
  528. out_of_blocks:
  529. spin_unlock(&sdp->sd_ail_lock);
  530. gfs2_log_unlock(sdp);
  531. if (!sdp->sd_log_num_revoke) {
  532. atomic_inc(&sdp->sd_log_blks_free);
  533. if (!sdp->sd_log_blks_reserved)
  534. atomic_inc(&sdp->sd_log_blks_free);
  535. }
  536. }
  537. /**
  538. * log_write_header - Get and initialize a journal header buffer
  539. * @sdp: The GFS2 superblock
  540. *
  541. * Returns: the initialized log buffer descriptor
  542. */
  543. static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
  544. {
  545. struct gfs2_log_header *lh;
  546. unsigned int tail;
  547. u32 hash;
  548. int rw = WRITE_FLUSH_FUA | REQ_META;
  549. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  550. lh = page_address(page);
  551. clear_page(lh);
  552. tail = current_tail(sdp);
  553. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  554. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  555. lh->lh_header.__pad0 = cpu_to_be64(0);
  556. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  557. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  558. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  559. lh->lh_flags = cpu_to_be32(flags);
  560. lh->lh_tail = cpu_to_be32(tail);
  561. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  562. hash = gfs2_disk_hash(page_address(page), sizeof(struct gfs2_log_header));
  563. lh->lh_hash = cpu_to_be32(hash);
  564. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
  565. gfs2_ordered_wait(sdp);
  566. log_flush_wait(sdp);
  567. rw = WRITE_SYNC | REQ_META | REQ_PRIO;
  568. }
  569. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  570. gfs2_log_write_page(sdp, page);
  571. gfs2_log_flush_bio(sdp, rw);
  572. log_flush_wait(sdp);
  573. if (sdp->sd_log_tail != tail)
  574. log_pull_tail(sdp, tail);
  575. }
  576. /**
  577. * gfs2_log_flush - flush incore transaction(s)
  578. * @sdp: the filesystem
  579. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  580. *
  581. */
  582. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  583. {
  584. struct gfs2_trans *tr;
  585. down_write(&sdp->sd_log_flush_lock);
  586. /* Log might have been flushed while we waited for the flush lock */
  587. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  588. up_write(&sdp->sd_log_flush_lock);
  589. return;
  590. }
  591. trace_gfs2_log_flush(sdp, 1);
  592. tr = sdp->sd_log_tr;
  593. if (tr) {
  594. sdp->sd_log_tr = NULL;
  595. INIT_LIST_HEAD(&tr->tr_ail1_list);
  596. INIT_LIST_HEAD(&tr->tr_ail2_list);
  597. }
  598. if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
  599. printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
  600. sdp->sd_log_commited_buf);
  601. gfs2_assert_withdraw(sdp, 0);
  602. }
  603. if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
  604. printk(KERN_INFO "GFS2: log databuf %u %u\n",
  605. sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
  606. gfs2_assert_withdraw(sdp, 0);
  607. }
  608. gfs2_assert_withdraw(sdp,
  609. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  610. sdp->sd_log_flush_head = sdp->sd_log_head;
  611. sdp->sd_log_flush_wrapped = 0;
  612. if (tr)
  613. tr->tr_first = sdp->sd_log_flush_head;
  614. gfs2_ordered_write(sdp);
  615. lops_before_commit(sdp);
  616. gfs2_log_flush_bio(sdp, WRITE);
  617. if (sdp->sd_log_head != sdp->sd_log_flush_head) {
  618. log_write_header(sdp, 0);
  619. } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  620. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  621. trace_gfs2_log_blocks(sdp, -1);
  622. log_write_header(sdp, 0);
  623. }
  624. lops_after_commit(sdp, tr);
  625. gfs2_log_lock(sdp);
  626. sdp->sd_log_head = sdp->sd_log_flush_head;
  627. sdp->sd_log_blks_reserved = 0;
  628. sdp->sd_log_commited_buf = 0;
  629. sdp->sd_log_commited_databuf = 0;
  630. sdp->sd_log_commited_revoke = 0;
  631. spin_lock(&sdp->sd_ail_lock);
  632. if (tr && !list_empty(&tr->tr_ail1_list)) {
  633. list_add(&tr->tr_list, &sdp->sd_ail1_list);
  634. tr = NULL;
  635. }
  636. spin_unlock(&sdp->sd_ail_lock);
  637. gfs2_log_unlock(sdp);
  638. trace_gfs2_log_flush(sdp, 0);
  639. up_write(&sdp->sd_log_flush_lock);
  640. kfree(tr);
  641. }
  642. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  643. {
  644. unsigned int reserved;
  645. unsigned int unused;
  646. gfs2_log_lock(sdp);
  647. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  648. sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
  649. tr->tr_num_databuf_rm;
  650. gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
  651. (((int)sdp->sd_log_commited_databuf) >= 0));
  652. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  653. reserved = calc_reserved(sdp);
  654. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
  655. unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
  656. atomic_add(unused, &sdp->sd_log_blks_free);
  657. trace_gfs2_log_blocks(sdp, unused);
  658. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  659. sdp->sd_jdesc->jd_blocks);
  660. sdp->sd_log_blks_reserved = reserved;
  661. if (sdp->sd_log_tr == NULL &&
  662. (tr->tr_num_buf_new || tr->tr_num_databuf_new)) {
  663. gfs2_assert_withdraw(sdp, tr->tr_t_gh.gh_gl);
  664. sdp->sd_log_tr = tr;
  665. tr->tr_attached = 1;
  666. }
  667. gfs2_log_unlock(sdp);
  668. }
  669. /**
  670. * gfs2_log_commit - Commit a transaction to the log
  671. * @sdp: the filesystem
  672. * @tr: the transaction
  673. *
  674. * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
  675. * or the total number of used blocks (pinned blocks plus AIL blocks)
  676. * is greater than thresh2.
  677. *
  678. * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
  679. * journal size.
  680. *
  681. * Returns: errno
  682. */
  683. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  684. {
  685. log_refund(sdp, tr);
  686. if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
  687. ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
  688. atomic_read(&sdp->sd_log_thresh2)))
  689. wake_up(&sdp->sd_logd_waitq);
  690. }
  691. /**
  692. * gfs2_log_shutdown - write a shutdown header into a journal
  693. * @sdp: the filesystem
  694. *
  695. */
  696. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  697. {
  698. down_write(&sdp->sd_log_flush_lock);
  699. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  700. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  701. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  702. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  703. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  704. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  705. sdp->sd_log_flush_head = sdp->sd_log_head;
  706. sdp->sd_log_flush_wrapped = 0;
  707. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT);
  708. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  709. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  710. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  711. sdp->sd_log_head = sdp->sd_log_flush_head;
  712. sdp->sd_log_tail = sdp->sd_log_head;
  713. up_write(&sdp->sd_log_flush_lock);
  714. }
  715. /**
  716. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  717. * @sdp: the filesystem
  718. *
  719. */
  720. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  721. {
  722. gfs2_log_flush(sdp, NULL);
  723. for (;;) {
  724. gfs2_ail1_start(sdp);
  725. gfs2_ail1_wait(sdp);
  726. if (gfs2_ail1_empty(sdp))
  727. break;
  728. }
  729. gfs2_log_flush(sdp, NULL);
  730. }
  731. static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
  732. {
  733. return (atomic_read(&sdp->sd_log_pinned) >= atomic_read(&sdp->sd_log_thresh1));
  734. }
  735. static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
  736. {
  737. unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
  738. return used_blocks >= atomic_read(&sdp->sd_log_thresh2);
  739. }
  740. /**
  741. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  742. * @sdp: Pointer to GFS2 superblock
  743. *
  744. * Also, periodically check to make sure that we're using the most recent
  745. * journal index.
  746. */
  747. int gfs2_logd(void *data)
  748. {
  749. struct gfs2_sbd *sdp = data;
  750. unsigned long t = 1;
  751. DEFINE_WAIT(wait);
  752. while (!kthread_should_stop()) {
  753. if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
  754. gfs2_ail1_empty(sdp);
  755. gfs2_log_flush(sdp, NULL);
  756. }
  757. if (gfs2_ail_flush_reqd(sdp)) {
  758. gfs2_ail1_start(sdp);
  759. gfs2_ail1_wait(sdp);
  760. gfs2_ail1_empty(sdp);
  761. gfs2_log_flush(sdp, NULL);
  762. }
  763. if (!gfs2_ail_flush_reqd(sdp))
  764. wake_up(&sdp->sd_log_waitq);
  765. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  766. try_to_freeze();
  767. do {
  768. prepare_to_wait(&sdp->sd_logd_waitq, &wait,
  769. TASK_INTERRUPTIBLE);
  770. if (!gfs2_ail_flush_reqd(sdp) &&
  771. !gfs2_jrnl_flush_reqd(sdp) &&
  772. !kthread_should_stop())
  773. t = schedule_timeout(t);
  774. } while(t && !gfs2_ail_flush_reqd(sdp) &&
  775. !gfs2_jrnl_flush_reqd(sdp) &&
  776. !kthread_should_stop());
  777. finish_wait(&sdp->sd_logd_waitq, &wait);
  778. }
  779. return 0;
  780. }