log.c 25 KB

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