log.c 16 KB

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