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. unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
  297. unsigned wanted = blks + reserved_blks;
  298. DEFINE_WAIT(wait);
  299. int did_wait = 0;
  300. unsigned int free_blocks;
  301. if (gfs2_assert_warn(sdp, blks) ||
  302. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  303. return -EINVAL;
  304. retry:
  305. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  306. if (unlikely(free_blocks <= wanted)) {
  307. do {
  308. prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
  309. TASK_UNINTERRUPTIBLE);
  310. wake_up(&sdp->sd_logd_waitq);
  311. did_wait = 1;
  312. if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
  313. io_schedule();
  314. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  315. } while(free_blocks <= wanted);
  316. finish_wait(&sdp->sd_log_waitq, &wait);
  317. }
  318. if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
  319. free_blocks - blks) != free_blocks)
  320. goto retry;
  321. trace_gfs2_log_blocks(sdp, -blks);
  322. /*
  323. * If we waited, then so might others, wake them up _after_ we get
  324. * our share of the log.
  325. */
  326. if (unlikely(did_wait))
  327. wake_up(&sdp->sd_log_waitq);
  328. down_read(&sdp->sd_log_flush_lock);
  329. if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
  330. gfs2_log_release(sdp, blks);
  331. return -EROFS;
  332. }
  333. return 0;
  334. }
  335. /**
  336. * log_distance - Compute distance between two journal blocks
  337. * @sdp: The GFS2 superblock
  338. * @newer: The most recent journal block of the pair
  339. * @older: The older journal block of the pair
  340. *
  341. * Compute the distance (in the journal direction) between two
  342. * blocks in the journal
  343. *
  344. * Returns: the distance in blocks
  345. */
  346. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  347. unsigned int older)
  348. {
  349. int dist;
  350. dist = newer - older;
  351. if (dist < 0)
  352. dist += sdp->sd_jdesc->jd_blocks;
  353. return dist;
  354. }
  355. /**
  356. * calc_reserved - Calculate the number of blocks to reserve when
  357. * refunding a transaction's unused buffers.
  358. * @sdp: The GFS2 superblock
  359. *
  360. * This is complex. We need to reserve room for all our currently used
  361. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  362. * all our journaled data buffers for journaled files (e.g. files in the
  363. * meta_fs like rindex, or files for which chattr +j was done.)
  364. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  365. * will count it as free space (sd_log_blks_free) and corruption will follow.
  366. *
  367. * We can have metadata bufs and jdata bufs in the same journal. So each
  368. * type gets its own log header, for which we need to reserve a block.
  369. * In fact, each type has the potential for needing more than one header
  370. * in cases where we have more buffers than will fit on a journal page.
  371. * Metadata journal entries take up half the space of journaled buffer entries.
  372. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  373. * databuf_limit (251) before they cause a wrap around.
  374. *
  375. * Also, we need to reserve blocks for revoke journal entries and one for an
  376. * overall header for the lot.
  377. *
  378. * Returns: the number of blocks reserved
  379. */
  380. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  381. {
  382. unsigned int reserved = 0;
  383. unsigned int mbuf;
  384. unsigned int dbuf;
  385. struct gfs2_trans *tr = sdp->sd_log_tr;
  386. if (tr) {
  387. mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
  388. dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
  389. reserved = mbuf + dbuf;
  390. /* Account for header blocks */
  391. reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
  392. reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
  393. }
  394. if (sdp->sd_log_commited_revoke > 0)
  395. reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  396. sizeof(u64));
  397. /* One for the overall header */
  398. if (reserved)
  399. reserved++;
  400. return reserved;
  401. }
  402. static unsigned int current_tail(struct gfs2_sbd *sdp)
  403. {
  404. struct gfs2_trans *tr;
  405. unsigned int tail;
  406. spin_lock(&sdp->sd_ail_lock);
  407. if (list_empty(&sdp->sd_ail1_list)) {
  408. tail = sdp->sd_log_head;
  409. } else {
  410. tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
  411. tr_list);
  412. tail = tr->tr_first;
  413. }
  414. spin_unlock(&sdp->sd_ail_lock);
  415. return tail;
  416. }
  417. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  418. {
  419. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  420. ail2_empty(sdp, new_tail);
  421. atomic_add(dist, &sdp->sd_log_blks_free);
  422. trace_gfs2_log_blocks(sdp, dist);
  423. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  424. sdp->sd_jdesc->jd_blocks);
  425. sdp->sd_log_tail = new_tail;
  426. }
  427. static void log_flush_wait(struct gfs2_sbd *sdp)
  428. {
  429. DEFINE_WAIT(wait);
  430. if (atomic_read(&sdp->sd_log_in_flight)) {
  431. do {
  432. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  433. TASK_UNINTERRUPTIBLE);
  434. if (atomic_read(&sdp->sd_log_in_flight))
  435. io_schedule();
  436. } while(atomic_read(&sdp->sd_log_in_flight));
  437. finish_wait(&sdp->sd_log_flush_wait, &wait);
  438. }
  439. }
  440. static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
  441. {
  442. struct gfs2_inode *ipa, *ipb;
  443. ipa = list_entry(a, struct gfs2_inode, i_ordered);
  444. ipb = list_entry(b, struct gfs2_inode, i_ordered);
  445. if (ipa->i_no_addr < ipb->i_no_addr)
  446. return -1;
  447. if (ipa->i_no_addr > ipb->i_no_addr)
  448. return 1;
  449. return 0;
  450. }
  451. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  452. {
  453. struct gfs2_inode *ip;
  454. LIST_HEAD(written);
  455. spin_lock(&sdp->sd_ordered_lock);
  456. list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
  457. while (!list_empty(&sdp->sd_log_le_ordered)) {
  458. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  459. list_move(&ip->i_ordered, &written);
  460. if (ip->i_inode.i_mapping->nrpages == 0)
  461. continue;
  462. spin_unlock(&sdp->sd_ordered_lock);
  463. filemap_fdatawrite(ip->i_inode.i_mapping);
  464. spin_lock(&sdp->sd_ordered_lock);
  465. }
  466. list_splice(&written, &sdp->sd_log_le_ordered);
  467. spin_unlock(&sdp->sd_ordered_lock);
  468. }
  469. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  470. {
  471. struct gfs2_inode *ip;
  472. spin_lock(&sdp->sd_ordered_lock);
  473. while (!list_empty(&sdp->sd_log_le_ordered)) {
  474. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  475. list_del(&ip->i_ordered);
  476. WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
  477. if (ip->i_inode.i_mapping->nrpages == 0)
  478. continue;
  479. spin_unlock(&sdp->sd_ordered_lock);
  480. filemap_fdatawait(ip->i_inode.i_mapping);
  481. spin_lock(&sdp->sd_ordered_lock);
  482. }
  483. spin_unlock(&sdp->sd_ordered_lock);
  484. }
  485. void gfs2_ordered_del_inode(struct gfs2_inode *ip)
  486. {
  487. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  488. spin_lock(&sdp->sd_ordered_lock);
  489. if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
  490. list_del(&ip->i_ordered);
  491. spin_unlock(&sdp->sd_ordered_lock);
  492. }
  493. void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  494. {
  495. struct buffer_head *bh = bd->bd_bh;
  496. struct gfs2_glock *gl = bd->bd_gl;
  497. bh->b_private = NULL;
  498. bd->bd_blkno = bh->b_blocknr;
  499. gfs2_remove_from_ail(bd); /* drops ref on bh */
  500. bd->bd_bh = NULL;
  501. bd->bd_ops = &gfs2_revoke_lops;
  502. sdp->sd_log_num_revoke++;
  503. atomic_inc(&gl->gl_revokes);
  504. set_bit(GLF_LFLUSH, &gl->gl_flags);
  505. list_add(&bd->bd_list, &sdp->sd_log_le_revoke);
  506. }
  507. void gfs2_write_revokes(struct gfs2_sbd *sdp)
  508. {
  509. struct gfs2_trans *tr;
  510. struct gfs2_bufdata *bd, *tmp;
  511. int have_revokes = 0;
  512. int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
  513. gfs2_ail1_empty(sdp);
  514. spin_lock(&sdp->sd_ail_lock);
  515. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  516. list_for_each_entry(bd, &tr->tr_ail2_list, bd_ail_st_list) {
  517. if (list_empty(&bd->bd_list)) {
  518. have_revokes = 1;
  519. goto done;
  520. }
  521. }
  522. }
  523. done:
  524. spin_unlock(&sdp->sd_ail_lock);
  525. if (have_revokes == 0)
  526. return;
  527. while (sdp->sd_log_num_revoke > max_revokes)
  528. max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
  529. max_revokes -= sdp->sd_log_num_revoke;
  530. if (!sdp->sd_log_num_revoke) {
  531. atomic_dec(&sdp->sd_log_blks_free);
  532. /* If no blocks have been reserved, we need to also
  533. * reserve a block for the header */
  534. if (!sdp->sd_log_blks_reserved)
  535. atomic_dec(&sdp->sd_log_blks_free);
  536. }
  537. gfs2_log_lock(sdp);
  538. spin_lock(&sdp->sd_ail_lock);
  539. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  540. list_for_each_entry_safe(bd, tmp, &tr->tr_ail2_list, bd_ail_st_list) {
  541. if (max_revokes == 0)
  542. goto out_of_blocks;
  543. if (!list_empty(&bd->bd_list))
  544. continue;
  545. gfs2_add_revoke(sdp, bd);
  546. max_revokes--;
  547. }
  548. }
  549. out_of_blocks:
  550. spin_unlock(&sdp->sd_ail_lock);
  551. gfs2_log_unlock(sdp);
  552. if (!sdp->sd_log_num_revoke) {
  553. atomic_inc(&sdp->sd_log_blks_free);
  554. if (!sdp->sd_log_blks_reserved)
  555. atomic_inc(&sdp->sd_log_blks_free);
  556. }
  557. }
  558. /**
  559. * log_write_header - Get and initialize a journal header buffer
  560. * @sdp: The GFS2 superblock
  561. *
  562. * Returns: the initialized log buffer descriptor
  563. */
  564. static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
  565. {
  566. struct gfs2_log_header *lh;
  567. unsigned int tail;
  568. u32 hash;
  569. int rw = WRITE_FLUSH_FUA | REQ_META;
  570. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  571. lh = page_address(page);
  572. clear_page(lh);
  573. tail = current_tail(sdp);
  574. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  575. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  576. lh->lh_header.__pad0 = cpu_to_be64(0);
  577. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  578. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  579. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  580. lh->lh_flags = cpu_to_be32(flags);
  581. lh->lh_tail = cpu_to_be32(tail);
  582. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  583. hash = gfs2_disk_hash(page_address(page), sizeof(struct gfs2_log_header));
  584. lh->lh_hash = cpu_to_be32(hash);
  585. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
  586. gfs2_ordered_wait(sdp);
  587. log_flush_wait(sdp);
  588. rw = WRITE_SYNC | REQ_META | REQ_PRIO;
  589. }
  590. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  591. gfs2_log_write_page(sdp, page);
  592. gfs2_log_flush_bio(sdp, rw);
  593. log_flush_wait(sdp);
  594. if (sdp->sd_log_tail != tail)
  595. log_pull_tail(sdp, tail);
  596. }
  597. /**
  598. * gfs2_log_flush - flush incore transaction(s)
  599. * @sdp: the filesystem
  600. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  601. *
  602. */
  603. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl,
  604. enum gfs2_flush_type type)
  605. {
  606. struct gfs2_trans *tr;
  607. down_write(&sdp->sd_log_flush_lock);
  608. /* Log might have been flushed while we waited for the flush lock */
  609. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  610. up_write(&sdp->sd_log_flush_lock);
  611. return;
  612. }
  613. trace_gfs2_log_flush(sdp, 1);
  614. sdp->sd_log_flush_head = sdp->sd_log_head;
  615. sdp->sd_log_flush_wrapped = 0;
  616. tr = sdp->sd_log_tr;
  617. if (tr) {
  618. sdp->sd_log_tr = NULL;
  619. INIT_LIST_HEAD(&tr->tr_ail1_list);
  620. INIT_LIST_HEAD(&tr->tr_ail2_list);
  621. tr->tr_first = sdp->sd_log_flush_head;
  622. }
  623. gfs2_assert_withdraw(sdp,
  624. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  625. gfs2_ordered_write(sdp);
  626. lops_before_commit(sdp, tr);
  627. gfs2_log_flush_bio(sdp, WRITE);
  628. if (sdp->sd_log_head != sdp->sd_log_flush_head) {
  629. log_flush_wait(sdp);
  630. log_write_header(sdp, 0);
  631. } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  632. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  633. trace_gfs2_log_blocks(sdp, -1);
  634. log_write_header(sdp, 0);
  635. }
  636. lops_after_commit(sdp, tr);
  637. gfs2_log_lock(sdp);
  638. sdp->sd_log_head = sdp->sd_log_flush_head;
  639. sdp->sd_log_blks_reserved = 0;
  640. sdp->sd_log_commited_revoke = 0;
  641. spin_lock(&sdp->sd_ail_lock);
  642. if (tr && !list_empty(&tr->tr_ail1_list)) {
  643. list_add(&tr->tr_list, &sdp->sd_ail1_list);
  644. tr = NULL;
  645. }
  646. spin_unlock(&sdp->sd_ail_lock);
  647. gfs2_log_unlock(sdp);
  648. if (atomic_read(&sdp->sd_log_freeze))
  649. type = FREEZE_FLUSH;
  650. if (type != NORMAL_FLUSH) {
  651. if (!sdp->sd_log_idle) {
  652. for (;;) {
  653. gfs2_ail1_start(sdp);
  654. gfs2_ail1_wait(sdp);
  655. if (gfs2_ail1_empty(sdp))
  656. break;
  657. }
  658. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  659. trace_gfs2_log_blocks(sdp, -1);
  660. sdp->sd_log_flush_wrapped = 0;
  661. log_write_header(sdp, 0);
  662. sdp->sd_log_head = sdp->sd_log_flush_head;
  663. }
  664. if (type == SHUTDOWN_FLUSH || type == FREEZE_FLUSH)
  665. gfs2_log_shutdown(sdp);
  666. if (type == FREEZE_FLUSH) {
  667. int error;
  668. atomic_set(&sdp->sd_log_freeze, 0);
  669. wake_up(&sdp->sd_log_frozen_wait);
  670. error = gfs2_glock_nq_init(sdp->sd_freeze_gl,
  671. LM_ST_SHARED, 0,
  672. &sdp->sd_thaw_gh);
  673. if (error) {
  674. printk(KERN_INFO "GFS2: couln't get freeze lock : %d\n", error);
  675. gfs2_assert_withdraw(sdp, 0);
  676. }
  677. else
  678. gfs2_glock_dq_uninit(&sdp->sd_thaw_gh);
  679. }
  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) || atomic_read(&sdp->sd_log_freeze));
  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. }