log.c 25 KB

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