log.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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/lm_interface.h>
  17. #include <linux/delay.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "bmap.h"
  21. #include "glock.h"
  22. #include "log.h"
  23. #include "lops.h"
  24. #include "meta_io.h"
  25. #include "util.h"
  26. #include "dir.h"
  27. #define PULL 1
  28. /**
  29. * gfs2_struct2blk - compute stuff
  30. * @sdp: the filesystem
  31. * @nstruct: the number of structures
  32. * @ssize: the size of the structures
  33. *
  34. * Compute the number of log descriptor blocks needed to hold a certain number
  35. * of structures of a certain size.
  36. *
  37. * Returns: the number of blocks needed (minimum is always 1)
  38. */
  39. unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
  40. unsigned int ssize)
  41. {
  42. unsigned int blks;
  43. unsigned int first, second;
  44. blks = 1;
  45. first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
  46. if (nstruct > first) {
  47. second = (sdp->sd_sb.sb_bsize -
  48. sizeof(struct gfs2_meta_header)) / ssize;
  49. blks += DIV_ROUND_UP(nstruct - first, second);
  50. }
  51. return blks;
  52. }
  53. /**
  54. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  55. * @sdp: the filesystem
  56. * @tr: the part of the AIL
  57. *
  58. */
  59. static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  60. {
  61. struct gfs2_bufdata *bd, *s;
  62. struct buffer_head *bh;
  63. int retry;
  64. BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
  65. do {
  66. retry = 0;
  67. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  68. bd_ail_st_list) {
  69. bh = bd->bd_bh;
  70. gfs2_assert(sdp, bd->bd_ail == ai);
  71. if (!buffer_busy(bh)) {
  72. if (!buffer_uptodate(bh)) {
  73. gfs2_log_unlock(sdp);
  74. gfs2_io_error_bh(sdp, bh);
  75. gfs2_log_lock(sdp);
  76. }
  77. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  78. continue;
  79. }
  80. if (!buffer_dirty(bh))
  81. continue;
  82. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  83. gfs2_log_unlock(sdp);
  84. wait_on_buffer(bh);
  85. ll_rw_block(WRITE, 1, &bh);
  86. gfs2_log_lock(sdp);
  87. retry = 1;
  88. break;
  89. }
  90. } while (retry);
  91. }
  92. /**
  93. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  94. * @sdp: the filesystem
  95. * @ai: the AIL entry
  96. *
  97. */
  98. static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
  99. {
  100. struct gfs2_bufdata *bd, *s;
  101. struct buffer_head *bh;
  102. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  103. bd_ail_st_list) {
  104. bh = bd->bd_bh;
  105. gfs2_assert(sdp, bd->bd_ail == ai);
  106. if (buffer_busy(bh)) {
  107. if (flags & DIO_ALL)
  108. continue;
  109. else
  110. break;
  111. }
  112. if (!buffer_uptodate(bh))
  113. gfs2_io_error_bh(sdp, bh);
  114. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  115. }
  116. return list_empty(&ai->ai_ail1_list);
  117. }
  118. static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
  119. {
  120. struct list_head *head = &sdp->sd_ail1_list;
  121. u64 sync_gen;
  122. struct list_head *first;
  123. struct gfs2_ail *first_ai, *ai, *tmp;
  124. int done = 0;
  125. gfs2_log_lock(sdp);
  126. if (list_empty(head)) {
  127. gfs2_log_unlock(sdp);
  128. return;
  129. }
  130. sync_gen = sdp->sd_ail_sync_gen++;
  131. first = head->prev;
  132. first_ai = list_entry(first, struct gfs2_ail, ai_list);
  133. first_ai->ai_sync_gen = sync_gen;
  134. gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
  135. if (flags & DIO_ALL)
  136. first = NULL;
  137. while(!done) {
  138. if (first && (head->prev != first ||
  139. gfs2_ail1_empty_one(sdp, first_ai, 0)))
  140. break;
  141. done = 1;
  142. list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
  143. if (ai->ai_sync_gen >= sync_gen)
  144. continue;
  145. ai->ai_sync_gen = sync_gen;
  146. gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
  147. done = 0;
  148. break;
  149. }
  150. }
  151. gfs2_log_unlock(sdp);
  152. }
  153. int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
  154. {
  155. struct gfs2_ail *ai, *s;
  156. int ret;
  157. gfs2_log_lock(sdp);
  158. list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
  159. if (gfs2_ail1_empty_one(sdp, ai, flags))
  160. list_move(&ai->ai_list, &sdp->sd_ail2_list);
  161. else if (!(flags & DIO_ALL))
  162. break;
  163. }
  164. ret = list_empty(&sdp->sd_ail1_list);
  165. gfs2_log_unlock(sdp);
  166. return ret;
  167. }
  168. /**
  169. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  170. * @sdp: the filesystem
  171. * @ai: the AIL entry
  172. *
  173. */
  174. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  175. {
  176. struct list_head *head = &ai->ai_ail2_list;
  177. struct gfs2_bufdata *bd;
  178. while (!list_empty(head)) {
  179. bd = list_entry(head->prev, struct gfs2_bufdata,
  180. bd_ail_st_list);
  181. gfs2_assert(sdp, bd->bd_ail == ai);
  182. bd->bd_ail = NULL;
  183. list_del(&bd->bd_ail_st_list);
  184. list_del(&bd->bd_ail_gl_list);
  185. atomic_dec(&bd->bd_gl->gl_ail_count);
  186. brelse(bd->bd_bh);
  187. }
  188. }
  189. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  190. {
  191. struct gfs2_ail *ai, *safe;
  192. unsigned int old_tail = sdp->sd_log_tail;
  193. int wrap = (new_tail < old_tail);
  194. int a, b, rm;
  195. gfs2_log_lock(sdp);
  196. list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
  197. a = (old_tail <= ai->ai_first);
  198. b = (ai->ai_first < new_tail);
  199. rm = (wrap) ? (a || b) : (a && b);
  200. if (!rm)
  201. continue;
  202. gfs2_ail2_empty_one(sdp, ai);
  203. list_del(&ai->ai_list);
  204. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
  205. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
  206. kfree(ai);
  207. }
  208. gfs2_log_unlock(sdp);
  209. }
  210. /**
  211. * gfs2_log_reserve - Make a log reservation
  212. * @sdp: The GFS2 superblock
  213. * @blks: The number of blocks to reserve
  214. *
  215. * Note that we never give out the last few blocks of the journal. Thats
  216. * due to the fact that there is are a small number of header blocks
  217. * associated with each log flush. The exact number can't be known until
  218. * flush time, so we ensure that we have just enough free blocks at all
  219. * times to avoid running out during a log flush.
  220. *
  221. * Returns: errno
  222. */
  223. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  224. {
  225. unsigned int try = 0;
  226. unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
  227. if (gfs2_assert_warn(sdp, blks) ||
  228. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  229. return -EINVAL;
  230. mutex_lock(&sdp->sd_log_reserve_mutex);
  231. gfs2_log_lock(sdp);
  232. while(sdp->sd_log_blks_free <= (blks + reserved_blks)) {
  233. gfs2_log_unlock(sdp);
  234. gfs2_ail1_empty(sdp, 0);
  235. gfs2_log_flush(sdp, NULL);
  236. if (try++)
  237. gfs2_ail1_start(sdp, 0);
  238. gfs2_log_lock(sdp);
  239. }
  240. sdp->sd_log_blks_free -= blks;
  241. gfs2_log_unlock(sdp);
  242. mutex_unlock(&sdp->sd_log_reserve_mutex);
  243. down_read(&sdp->sd_log_flush_lock);
  244. return 0;
  245. }
  246. /**
  247. * gfs2_log_release - Release a given number of log blocks
  248. * @sdp: The GFS2 superblock
  249. * @blks: The number of blocks
  250. *
  251. */
  252. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  253. {
  254. gfs2_log_lock(sdp);
  255. sdp->sd_log_blks_free += blks;
  256. gfs2_assert_withdraw(sdp,
  257. sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
  258. gfs2_log_unlock(sdp);
  259. up_read(&sdp->sd_log_flush_lock);
  260. }
  261. static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
  262. {
  263. struct inode *inode = sdp->sd_jdesc->jd_inode;
  264. int error;
  265. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  266. bh_map.b_size = 1 << inode->i_blkbits;
  267. error = gfs2_block_map(inode, lbn, 0, &bh_map);
  268. if (error || !bh_map.b_blocknr)
  269. printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
  270. (unsigned long long)bh_map.b_blocknr, lbn);
  271. gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
  272. return bh_map.b_blocknr;
  273. }
  274. /**
  275. * log_distance - Compute distance between two journal blocks
  276. * @sdp: The GFS2 superblock
  277. * @newer: The most recent journal block of the pair
  278. * @older: The older journal block of the pair
  279. *
  280. * Compute the distance (in the journal direction) between two
  281. * blocks in the journal
  282. *
  283. * Returns: the distance in blocks
  284. */
  285. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  286. unsigned int older)
  287. {
  288. int dist;
  289. dist = newer - older;
  290. if (dist < 0)
  291. dist += sdp->sd_jdesc->jd_blocks;
  292. return dist;
  293. }
  294. static unsigned int current_tail(struct gfs2_sbd *sdp)
  295. {
  296. struct gfs2_ail *ai;
  297. unsigned int tail;
  298. gfs2_log_lock(sdp);
  299. if (list_empty(&sdp->sd_ail1_list)) {
  300. tail = sdp->sd_log_head;
  301. } else {
  302. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  303. tail = ai->ai_first;
  304. }
  305. gfs2_log_unlock(sdp);
  306. return tail;
  307. }
  308. static inline void log_incr_head(struct gfs2_sbd *sdp)
  309. {
  310. if (sdp->sd_log_flush_head == sdp->sd_log_tail)
  311. gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
  312. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  313. sdp->sd_log_flush_head = 0;
  314. sdp->sd_log_flush_wrapped = 1;
  315. }
  316. }
  317. /**
  318. * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
  319. * @sdp: The GFS2 superblock
  320. *
  321. * Returns: the buffer_head
  322. */
  323. struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
  324. {
  325. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  326. struct gfs2_log_buf *lb;
  327. struct buffer_head *bh;
  328. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  329. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  330. bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
  331. lock_buffer(bh);
  332. memset(bh->b_data, 0, bh->b_size);
  333. set_buffer_uptodate(bh);
  334. clear_buffer_dirty(bh);
  335. unlock_buffer(bh);
  336. log_incr_head(sdp);
  337. return bh;
  338. }
  339. /**
  340. * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
  341. * @sdp: the filesystem
  342. * @data: the data the buffer_head should point to
  343. *
  344. * Returns: the log buffer descriptor
  345. */
  346. struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
  347. struct buffer_head *real)
  348. {
  349. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  350. struct gfs2_log_buf *lb;
  351. struct buffer_head *bh;
  352. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  353. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  354. lb->lb_real = real;
  355. bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
  356. atomic_set(&bh->b_count, 1);
  357. bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
  358. set_bh_page(bh, real->b_page, bh_offset(real));
  359. bh->b_blocknr = blkno;
  360. bh->b_size = sdp->sd_sb.sb_bsize;
  361. bh->b_bdev = sdp->sd_vfs->s_bdev;
  362. log_incr_head(sdp);
  363. return bh;
  364. }
  365. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
  366. {
  367. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  368. ail2_empty(sdp, new_tail);
  369. gfs2_log_lock(sdp);
  370. sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
  371. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
  372. gfs2_log_unlock(sdp);
  373. sdp->sd_log_tail = new_tail;
  374. }
  375. /**
  376. * log_write_header - Get and initialize a journal header buffer
  377. * @sdp: The GFS2 superblock
  378. *
  379. * Returns: the initialized log buffer descriptor
  380. */
  381. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  382. {
  383. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  384. struct buffer_head *bh;
  385. struct gfs2_log_header *lh;
  386. unsigned int tail;
  387. u32 hash;
  388. bh = sb_getblk(sdp->sd_vfs, blkno);
  389. lock_buffer(bh);
  390. memset(bh->b_data, 0, bh->b_size);
  391. set_buffer_uptodate(bh);
  392. clear_buffer_dirty(bh);
  393. unlock_buffer(bh);
  394. gfs2_ail1_empty(sdp, 0);
  395. tail = current_tail(sdp);
  396. lh = (struct gfs2_log_header *)bh->b_data;
  397. memset(lh, 0, sizeof(struct gfs2_log_header));
  398. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  399. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  400. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  401. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  402. lh->lh_flags = cpu_to_be32(flags);
  403. lh->lh_tail = cpu_to_be32(tail);
  404. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  405. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  406. lh->lh_hash = cpu_to_be32(hash);
  407. set_buffer_dirty(bh);
  408. if (sync_dirty_buffer(bh))
  409. gfs2_io_error_bh(sdp, bh);
  410. brelse(bh);
  411. if (sdp->sd_log_tail != tail)
  412. log_pull_tail(sdp, tail, pull);
  413. else
  414. gfs2_assert_withdraw(sdp, !pull);
  415. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  416. log_incr_head(sdp);
  417. }
  418. static void log_flush_commit(struct gfs2_sbd *sdp)
  419. {
  420. struct list_head *head = &sdp->sd_log_flush_list;
  421. struct gfs2_log_buf *lb;
  422. struct buffer_head *bh;
  423. while (!list_empty(head)) {
  424. lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
  425. list_del(&lb->lb_list);
  426. bh = lb->lb_bh;
  427. wait_on_buffer(bh);
  428. if (!buffer_uptodate(bh))
  429. gfs2_io_error_bh(sdp, bh);
  430. if (lb->lb_real) {
  431. while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
  432. schedule();
  433. free_buffer_head(bh);
  434. } else
  435. brelse(bh);
  436. kfree(lb);
  437. }
  438. log_write_header(sdp, 0, 0);
  439. }
  440. /**
  441. * gfs2_log_flush - flush incore transaction(s)
  442. * @sdp: the filesystem
  443. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  444. *
  445. */
  446. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  447. {
  448. struct gfs2_ail *ai;
  449. down_write(&sdp->sd_log_flush_lock);
  450. if (gl) {
  451. gfs2_log_lock(sdp);
  452. if (list_empty(&gl->gl_le.le_list)) {
  453. gfs2_log_unlock(sdp);
  454. up_write(&sdp->sd_log_flush_lock);
  455. return;
  456. }
  457. gfs2_log_unlock(sdp);
  458. }
  459. ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
  460. INIT_LIST_HEAD(&ai->ai_ail1_list);
  461. INIT_LIST_HEAD(&ai->ai_ail2_list);
  462. gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
  463. gfs2_assert_withdraw(sdp,
  464. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  465. sdp->sd_log_flush_head = sdp->sd_log_head;
  466. sdp->sd_log_flush_wrapped = 0;
  467. ai->ai_first = sdp->sd_log_flush_head;
  468. lops_before_commit(sdp);
  469. if (!list_empty(&sdp->sd_log_flush_list))
  470. log_flush_commit(sdp);
  471. else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
  472. log_write_header(sdp, 0, PULL);
  473. lops_after_commit(sdp, ai);
  474. gfs2_log_lock(sdp);
  475. sdp->sd_log_head = sdp->sd_log_flush_head;
  476. sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
  477. sdp->sd_log_blks_reserved = 0;
  478. sdp->sd_log_commited_buf = 0;
  479. sdp->sd_log_num_hdrs = 0;
  480. sdp->sd_log_commited_revoke = 0;
  481. if (!list_empty(&ai->ai_ail1_list)) {
  482. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  483. ai = NULL;
  484. }
  485. gfs2_log_unlock(sdp);
  486. sdp->sd_vfs->s_dirt = 0;
  487. up_write(&sdp->sd_log_flush_lock);
  488. kfree(ai);
  489. }
  490. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  491. {
  492. unsigned int reserved = 0;
  493. unsigned int old;
  494. gfs2_log_lock(sdp);
  495. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  496. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
  497. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  498. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
  499. if (sdp->sd_log_commited_buf)
  500. reserved += sdp->sd_log_commited_buf;
  501. if (sdp->sd_log_commited_revoke)
  502. reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  503. sizeof(u64));
  504. if (reserved)
  505. reserved++;
  506. old = sdp->sd_log_blks_free;
  507. sdp->sd_log_blks_free += tr->tr_reserved -
  508. (reserved - sdp->sd_log_blks_reserved);
  509. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
  510. gfs2_assert_withdraw(sdp,
  511. sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
  512. sdp->sd_log_num_hdrs);
  513. sdp->sd_log_blks_reserved = reserved;
  514. gfs2_log_unlock(sdp);
  515. }
  516. /**
  517. * gfs2_log_commit - Commit a transaction to the log
  518. * @sdp: the filesystem
  519. * @tr: the transaction
  520. *
  521. * Returns: errno
  522. */
  523. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  524. {
  525. log_refund(sdp, tr);
  526. lops_incore_commit(sdp, tr);
  527. sdp->sd_vfs->s_dirt = 1;
  528. up_read(&sdp->sd_log_flush_lock);
  529. gfs2_log_lock(sdp);
  530. if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
  531. wake_up_process(sdp->sd_logd_process);
  532. gfs2_log_unlock(sdp);
  533. }
  534. /**
  535. * gfs2_log_shutdown - write a shutdown header into a journal
  536. * @sdp: the filesystem
  537. *
  538. */
  539. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  540. {
  541. down_write(&sdp->sd_log_flush_lock);
  542. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  543. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
  544. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  545. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
  546. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  547. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  548. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  549. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
  550. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  551. sdp->sd_log_flush_head = sdp->sd_log_head;
  552. sdp->sd_log_flush_wrapped = 0;
  553. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
  554. gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
  555. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  556. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  557. sdp->sd_log_head = sdp->sd_log_flush_head;
  558. sdp->sd_log_tail = sdp->sd_log_head;
  559. up_write(&sdp->sd_log_flush_lock);
  560. }
  561. /**
  562. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  563. * @sdp: the filesystem
  564. *
  565. */
  566. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  567. {
  568. gfs2_log_flush(sdp, NULL);
  569. for (;;) {
  570. gfs2_ail1_start(sdp, DIO_ALL);
  571. if (gfs2_ail1_empty(sdp, DIO_ALL))
  572. break;
  573. msleep(10);
  574. }
  575. }