log.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. struct inode *inode = sdp->sd_jdesc->jd_inode;
  256. int error;
  257. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  258. bh_map.b_size = 1 << inode->i_blkbits;
  259. error = gfs2_block_map(inode, lbn, 0, &bh_map);
  260. if (error || !bh_map.b_blocknr)
  261. printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
  262. gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
  263. return bh_map.b_blocknr;
  264. }
  265. /**
  266. * log_distance - Compute distance between two journal blocks
  267. * @sdp: The GFS2 superblock
  268. * @newer: The most recent journal block of the pair
  269. * @older: The older journal block of the pair
  270. *
  271. * Compute the distance (in the journal direction) between two
  272. * blocks in the journal
  273. *
  274. * Returns: the distance in blocks
  275. */
  276. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  277. unsigned int older)
  278. {
  279. int dist;
  280. dist = newer - older;
  281. if (dist < 0)
  282. dist += sdp->sd_jdesc->jd_blocks;
  283. return dist;
  284. }
  285. static unsigned int current_tail(struct gfs2_sbd *sdp)
  286. {
  287. struct gfs2_ail *ai;
  288. unsigned int tail;
  289. gfs2_log_lock(sdp);
  290. if (list_empty(&sdp->sd_ail1_list)) {
  291. tail = sdp->sd_log_head;
  292. } else {
  293. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  294. tail = ai->ai_first;
  295. }
  296. gfs2_log_unlock(sdp);
  297. return tail;
  298. }
  299. static inline void log_incr_head(struct gfs2_sbd *sdp)
  300. {
  301. if (sdp->sd_log_flush_head == sdp->sd_log_tail)
  302. gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
  303. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  304. sdp->sd_log_flush_head = 0;
  305. sdp->sd_log_flush_wrapped = 1;
  306. }
  307. }
  308. /**
  309. * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
  310. * @sdp: The GFS2 superblock
  311. *
  312. * Returns: the buffer_head
  313. */
  314. struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
  315. {
  316. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  317. struct gfs2_log_buf *lb;
  318. struct buffer_head *bh;
  319. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  320. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  321. bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
  322. lock_buffer(bh);
  323. memset(bh->b_data, 0, bh->b_size);
  324. set_buffer_uptodate(bh);
  325. clear_buffer_dirty(bh);
  326. unlock_buffer(bh);
  327. log_incr_head(sdp);
  328. return bh;
  329. }
  330. /**
  331. * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
  332. * @sdp: the filesystem
  333. * @data: the data the buffer_head should point to
  334. *
  335. * Returns: the log buffer descriptor
  336. */
  337. struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
  338. struct buffer_head *real)
  339. {
  340. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  341. struct gfs2_log_buf *lb;
  342. struct buffer_head *bh;
  343. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  344. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  345. lb->lb_real = real;
  346. bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
  347. atomic_set(&bh->b_count, 1);
  348. bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
  349. set_bh_page(bh, real->b_page, bh_offset(real));
  350. bh->b_blocknr = blkno;
  351. bh->b_size = sdp->sd_sb.sb_bsize;
  352. bh->b_bdev = sdp->sd_vfs->s_bdev;
  353. log_incr_head(sdp);
  354. return bh;
  355. }
  356. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
  357. {
  358. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  359. ail2_empty(sdp, new_tail);
  360. gfs2_log_lock(sdp);
  361. sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
  362. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
  363. gfs2_log_unlock(sdp);
  364. sdp->sd_log_tail = new_tail;
  365. }
  366. /**
  367. * log_write_header - Get and initialize a journal header buffer
  368. * @sdp: The GFS2 superblock
  369. *
  370. * Returns: the initialized log buffer descriptor
  371. */
  372. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  373. {
  374. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  375. struct buffer_head *bh;
  376. struct gfs2_log_header *lh;
  377. unsigned int tail;
  378. u32 hash;
  379. bh = sb_getblk(sdp->sd_vfs, blkno);
  380. lock_buffer(bh);
  381. memset(bh->b_data, 0, bh->b_size);
  382. set_buffer_uptodate(bh);
  383. clear_buffer_dirty(bh);
  384. unlock_buffer(bh);
  385. gfs2_ail1_empty(sdp, 0);
  386. tail = current_tail(sdp);
  387. lh = (struct gfs2_log_header *)bh->b_data;
  388. memset(lh, 0, sizeof(struct gfs2_log_header));
  389. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  390. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  391. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  392. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  393. lh->lh_flags = cpu_to_be32(flags);
  394. lh->lh_tail = cpu_to_be32(tail);
  395. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  396. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  397. lh->lh_hash = cpu_to_be32(hash);
  398. set_buffer_dirty(bh);
  399. if (sync_dirty_buffer(bh))
  400. gfs2_io_error_bh(sdp, bh);
  401. brelse(bh);
  402. if (sdp->sd_log_tail != tail)
  403. log_pull_tail(sdp, tail, pull);
  404. else
  405. gfs2_assert_withdraw(sdp, !pull);
  406. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  407. log_incr_head(sdp);
  408. }
  409. static void log_flush_commit(struct gfs2_sbd *sdp)
  410. {
  411. struct list_head *head = &sdp->sd_log_flush_list;
  412. struct gfs2_log_buf *lb;
  413. struct buffer_head *bh;
  414. while (!list_empty(head)) {
  415. lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
  416. list_del(&lb->lb_list);
  417. bh = lb->lb_bh;
  418. wait_on_buffer(bh);
  419. if (!buffer_uptodate(bh))
  420. gfs2_io_error_bh(sdp, bh);
  421. if (lb->lb_real) {
  422. while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
  423. schedule();
  424. free_buffer_head(bh);
  425. } else
  426. brelse(bh);
  427. kfree(lb);
  428. }
  429. log_write_header(sdp, 0, 0);
  430. }
  431. /**
  432. * gfs2_log_flush - flush incore transaction(s)
  433. * @sdp: the filesystem
  434. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  435. *
  436. */
  437. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  438. {
  439. struct gfs2_ail *ai;
  440. down_write(&sdp->sd_log_flush_lock);
  441. if (gl) {
  442. gfs2_log_lock(sdp);
  443. if (list_empty(&gl->gl_le.le_list)) {
  444. gfs2_log_unlock(sdp);
  445. up_write(&sdp->sd_log_flush_lock);
  446. return;
  447. }
  448. gfs2_log_unlock(sdp);
  449. }
  450. ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
  451. INIT_LIST_HEAD(&ai->ai_ail1_list);
  452. INIT_LIST_HEAD(&ai->ai_ail2_list);
  453. gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
  454. gfs2_assert_withdraw(sdp,
  455. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  456. sdp->sd_log_flush_head = sdp->sd_log_head;
  457. sdp->sd_log_flush_wrapped = 0;
  458. ai->ai_first = sdp->sd_log_flush_head;
  459. lops_before_commit(sdp);
  460. if (!list_empty(&sdp->sd_log_flush_list))
  461. log_flush_commit(sdp);
  462. else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
  463. log_write_header(sdp, 0, PULL);
  464. lops_after_commit(sdp, ai);
  465. gfs2_log_lock(sdp);
  466. sdp->sd_log_head = sdp->sd_log_flush_head;
  467. sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
  468. sdp->sd_log_blks_reserved = 0;
  469. sdp->sd_log_commited_buf = 0;
  470. sdp->sd_log_num_hdrs = 0;
  471. sdp->sd_log_commited_revoke = 0;
  472. if (!list_empty(&ai->ai_ail1_list)) {
  473. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  474. ai = NULL;
  475. }
  476. gfs2_log_unlock(sdp);
  477. sdp->sd_vfs->s_dirt = 0;
  478. up_write(&sdp->sd_log_flush_lock);
  479. kfree(ai);
  480. }
  481. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  482. {
  483. unsigned int reserved = 0;
  484. unsigned int old;
  485. gfs2_log_lock(sdp);
  486. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  487. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
  488. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  489. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
  490. if (sdp->sd_log_commited_buf)
  491. reserved += sdp->sd_log_commited_buf;
  492. if (sdp->sd_log_commited_revoke)
  493. reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  494. sizeof(u64));
  495. if (reserved)
  496. reserved++;
  497. old = sdp->sd_log_blks_free;
  498. sdp->sd_log_blks_free += tr->tr_reserved -
  499. (reserved - sdp->sd_log_blks_reserved);
  500. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
  501. gfs2_assert_withdraw(sdp,
  502. sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
  503. sdp->sd_log_num_hdrs);
  504. sdp->sd_log_blks_reserved = reserved;
  505. gfs2_log_unlock(sdp);
  506. }
  507. /**
  508. * gfs2_log_commit - Commit a transaction to the log
  509. * @sdp: the filesystem
  510. * @tr: the transaction
  511. *
  512. * Returns: errno
  513. */
  514. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  515. {
  516. log_refund(sdp, tr);
  517. lops_incore_commit(sdp, tr);
  518. sdp->sd_vfs->s_dirt = 1;
  519. up_read(&sdp->sd_log_flush_lock);
  520. gfs2_log_lock(sdp);
  521. if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
  522. gfs2_log_unlock(sdp);
  523. gfs2_log_flush(sdp, NULL);
  524. } else {
  525. gfs2_log_unlock(sdp);
  526. }
  527. }
  528. /**
  529. * gfs2_log_shutdown - write a shutdown header into a journal
  530. * @sdp: the filesystem
  531. *
  532. */
  533. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  534. {
  535. down_write(&sdp->sd_log_flush_lock);
  536. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  537. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
  538. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  539. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
  540. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  541. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  542. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  543. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
  544. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  545. sdp->sd_log_flush_head = sdp->sd_log_head;
  546. sdp->sd_log_flush_wrapped = 0;
  547. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
  548. gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
  549. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  550. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  551. sdp->sd_log_head = sdp->sd_log_flush_head;
  552. sdp->sd_log_tail = sdp->sd_log_head;
  553. up_write(&sdp->sd_log_flush_lock);
  554. }