log.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 (!bh){
  72. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  73. continue;
  74. }
  75. if (!buffer_busy(bh)) {
  76. if (!buffer_uptodate(bh)) {
  77. gfs2_log_unlock(sdp);
  78. gfs2_io_error_bh(sdp, bh);
  79. gfs2_log_lock(sdp);
  80. }
  81. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  82. continue;
  83. }
  84. if (!buffer_dirty(bh))
  85. continue;
  86. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  87. gfs2_log_unlock(sdp);
  88. wait_on_buffer(bh);
  89. ll_rw_block(WRITE, 1, &bh);
  90. gfs2_log_lock(sdp);
  91. retry = 1;
  92. break;
  93. }
  94. } while (retry);
  95. }
  96. /**
  97. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  98. * @sdp: the filesystem
  99. * @ai: the AIL entry
  100. *
  101. */
  102. static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
  103. {
  104. struct gfs2_bufdata *bd, *s;
  105. struct buffer_head *bh;
  106. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  107. bd_ail_st_list) {
  108. bh = bd->bd_bh;
  109. if (!bh){
  110. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  111. continue;
  112. }
  113. gfs2_assert(sdp, bd->bd_ail == ai);
  114. if (buffer_busy(bh)) {
  115. if (flags & DIO_ALL)
  116. continue;
  117. else
  118. break;
  119. }
  120. if (!buffer_uptodate(bh))
  121. gfs2_io_error_bh(sdp, bh);
  122. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  123. }
  124. return list_empty(&ai->ai_ail1_list);
  125. }
  126. static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
  127. {
  128. struct list_head *head = &sdp->sd_ail1_list;
  129. u64 sync_gen;
  130. struct list_head *first;
  131. struct gfs2_ail *first_ai, *ai, *tmp;
  132. int done = 0;
  133. gfs2_log_lock(sdp);
  134. if (list_empty(head)) {
  135. gfs2_log_unlock(sdp);
  136. return;
  137. }
  138. sync_gen = sdp->sd_ail_sync_gen++;
  139. first = head->prev;
  140. first_ai = list_entry(first, struct gfs2_ail, ai_list);
  141. first_ai->ai_sync_gen = sync_gen;
  142. gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
  143. if (flags & DIO_ALL)
  144. first = NULL;
  145. while(!done) {
  146. if (first && (head->prev != first ||
  147. gfs2_ail1_empty_one(sdp, first_ai, 0)))
  148. break;
  149. done = 1;
  150. list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
  151. if (ai->ai_sync_gen >= sync_gen)
  152. continue;
  153. ai->ai_sync_gen = sync_gen;
  154. gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
  155. done = 0;
  156. break;
  157. }
  158. }
  159. gfs2_log_unlock(sdp);
  160. }
  161. int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
  162. {
  163. struct gfs2_ail *ai, *s;
  164. int ret;
  165. gfs2_log_lock(sdp);
  166. list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
  167. if (gfs2_ail1_empty_one(sdp, ai, flags))
  168. list_move(&ai->ai_list, &sdp->sd_ail2_list);
  169. else if (!(flags & DIO_ALL))
  170. break;
  171. }
  172. ret = list_empty(&sdp->sd_ail1_list);
  173. gfs2_log_unlock(sdp);
  174. return ret;
  175. }
  176. /**
  177. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  178. * @sdp: the filesystem
  179. * @ai: the AIL entry
  180. *
  181. */
  182. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  183. {
  184. struct list_head *head = &ai->ai_ail2_list;
  185. struct gfs2_bufdata *bd;
  186. while (!list_empty(head)) {
  187. bd = list_entry(head->prev, struct gfs2_bufdata,
  188. bd_ail_st_list);
  189. gfs2_assert(sdp, bd->bd_ail == ai);
  190. bd->bd_ail = NULL;
  191. list_del(&bd->bd_ail_st_list);
  192. list_del(&bd->bd_ail_gl_list);
  193. atomic_dec(&bd->bd_gl->gl_ail_count);
  194. if (bd->bd_bh)
  195. brelse(bd->bd_bh);
  196. else
  197. kmem_cache_free(gfs2_bufdata_cachep, bd);
  198. }
  199. }
  200. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  201. {
  202. struct gfs2_ail *ai, *safe;
  203. unsigned int old_tail = sdp->sd_log_tail;
  204. int wrap = (new_tail < old_tail);
  205. int a, b, rm;
  206. gfs2_log_lock(sdp);
  207. list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
  208. a = (old_tail <= ai->ai_first);
  209. b = (ai->ai_first < new_tail);
  210. rm = (wrap) ? (a || b) : (a && b);
  211. if (!rm)
  212. continue;
  213. gfs2_ail2_empty_one(sdp, ai);
  214. list_del(&ai->ai_list);
  215. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
  216. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
  217. kfree(ai);
  218. }
  219. gfs2_log_unlock(sdp);
  220. }
  221. /**
  222. * gfs2_log_reserve - Make a log reservation
  223. * @sdp: The GFS2 superblock
  224. * @blks: The number of blocks to reserve
  225. *
  226. * Note that we never give out the last few blocks of the journal. Thats
  227. * due to the fact that there is a small number of header blocks
  228. * associated with each log flush. The exact number can't be known until
  229. * flush time, so we ensure that we have just enough free blocks at all
  230. * times to avoid running out during a log flush.
  231. *
  232. * Returns: errno
  233. */
  234. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  235. {
  236. unsigned int try = 0;
  237. unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
  238. if (gfs2_assert_warn(sdp, blks) ||
  239. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  240. return -EINVAL;
  241. mutex_lock(&sdp->sd_log_reserve_mutex);
  242. gfs2_log_lock(sdp);
  243. while(sdp->sd_log_blks_free <= (blks + reserved_blks)) {
  244. gfs2_log_unlock(sdp);
  245. gfs2_ail1_empty(sdp, 0);
  246. gfs2_log_flush(sdp, NULL);
  247. if (try++)
  248. gfs2_ail1_start(sdp, 0);
  249. gfs2_log_lock(sdp);
  250. }
  251. sdp->sd_log_blks_free -= blks;
  252. gfs2_log_unlock(sdp);
  253. mutex_unlock(&sdp->sd_log_reserve_mutex);
  254. down_read(&sdp->sd_log_flush_lock);
  255. return 0;
  256. }
  257. /**
  258. * gfs2_log_release - Release a given number of log blocks
  259. * @sdp: The GFS2 superblock
  260. * @blks: The number of blocks
  261. *
  262. */
  263. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  264. {
  265. gfs2_log_lock(sdp);
  266. sdp->sd_log_blks_free += blks;
  267. gfs2_assert_withdraw(sdp,
  268. sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
  269. gfs2_log_unlock(sdp);
  270. up_read(&sdp->sd_log_flush_lock);
  271. }
  272. static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
  273. {
  274. struct inode *inode = sdp->sd_jdesc->jd_inode;
  275. int error;
  276. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  277. bh_map.b_size = 1 << inode->i_blkbits;
  278. error = gfs2_block_map(inode, lbn, 0, &bh_map);
  279. if (error || !bh_map.b_blocknr)
  280. printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
  281. (unsigned long long)bh_map.b_blocknr, lbn);
  282. gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
  283. return bh_map.b_blocknr;
  284. }
  285. /**
  286. * log_distance - Compute distance between two journal blocks
  287. * @sdp: The GFS2 superblock
  288. * @newer: The most recent journal block of the pair
  289. * @older: The older journal block of the pair
  290. *
  291. * Compute the distance (in the journal direction) between two
  292. * blocks in the journal
  293. *
  294. * Returns: the distance in blocks
  295. */
  296. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  297. unsigned int older)
  298. {
  299. int dist;
  300. dist = newer - older;
  301. if (dist < 0)
  302. dist += sdp->sd_jdesc->jd_blocks;
  303. return dist;
  304. }
  305. /**
  306. * calc_reserved - Calculate the number of blocks to reserve when
  307. * refunding a transaction's unused buffers.
  308. * @sdp: The GFS2 superblock
  309. *
  310. * This is complex. We need to reserve room for all our currently used
  311. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  312. * all our journaled data buffers for journaled files (e.g. files in the
  313. * meta_fs like rindex, or files for which chattr +j was done.)
  314. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  315. * will count it as free space (sd_log_blks_free) and corruption will follow.
  316. *
  317. * We can have metadata bufs and jdata bufs in the same journal. So each
  318. * type gets its own log header, for which we need to reserve a block.
  319. * In fact, each type has the potential for needing more than one header
  320. * in cases where we have more buffers than will fit on a journal page.
  321. * Metadata journal entries take up half the space of journaled buffer entries.
  322. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  323. * databuf_limit (251) before they cause a wrap around.
  324. *
  325. * Also, we need to reserve blocks for revoke journal entries and one for an
  326. * overall header for the lot.
  327. *
  328. * Returns: the number of blocks reserved
  329. */
  330. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  331. {
  332. unsigned int reserved = 0;
  333. unsigned int mbuf_limit, metabufhdrs_needed;
  334. unsigned int dbuf_limit, databufhdrs_needed;
  335. unsigned int revokes = 0;
  336. mbuf_limit = buf_limit(sdp);
  337. metabufhdrs_needed = (sdp->sd_log_commited_buf +
  338. (mbuf_limit - 1)) / mbuf_limit;
  339. dbuf_limit = databuf_limit(sdp);
  340. databufhdrs_needed = (sdp->sd_log_commited_databuf +
  341. (dbuf_limit - 1)) / dbuf_limit;
  342. if (sdp->sd_log_commited_revoke)
  343. revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  344. sizeof(u64));
  345. reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
  346. sdp->sd_log_commited_databuf + databufhdrs_needed +
  347. revokes;
  348. /* One for the overall header */
  349. if (reserved)
  350. reserved++;
  351. return reserved;
  352. }
  353. static unsigned int current_tail(struct gfs2_sbd *sdp)
  354. {
  355. struct gfs2_ail *ai;
  356. unsigned int tail;
  357. gfs2_log_lock(sdp);
  358. if (list_empty(&sdp->sd_ail1_list)) {
  359. tail = sdp->sd_log_head;
  360. } else {
  361. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  362. tail = ai->ai_first;
  363. }
  364. gfs2_log_unlock(sdp);
  365. return tail;
  366. }
  367. static inline void log_incr_head(struct gfs2_sbd *sdp)
  368. {
  369. if (sdp->sd_log_flush_head == sdp->sd_log_tail)
  370. gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
  371. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  372. sdp->sd_log_flush_head = 0;
  373. sdp->sd_log_flush_wrapped = 1;
  374. }
  375. }
  376. /**
  377. * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
  378. * @sdp: The GFS2 superblock
  379. *
  380. * Returns: the buffer_head
  381. */
  382. struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
  383. {
  384. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  385. struct gfs2_log_buf *lb;
  386. struct buffer_head *bh;
  387. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  388. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  389. bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
  390. lock_buffer(bh);
  391. memset(bh->b_data, 0, bh->b_size);
  392. set_buffer_uptodate(bh);
  393. clear_buffer_dirty(bh);
  394. unlock_buffer(bh);
  395. log_incr_head(sdp);
  396. return bh;
  397. }
  398. /**
  399. * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
  400. * @sdp: the filesystem
  401. * @data: the data the buffer_head should point to
  402. *
  403. * Returns: the log buffer descriptor
  404. */
  405. struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
  406. struct buffer_head *real)
  407. {
  408. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  409. struct gfs2_log_buf *lb;
  410. struct buffer_head *bh;
  411. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  412. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  413. lb->lb_real = real;
  414. bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
  415. atomic_set(&bh->b_count, 1);
  416. bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
  417. set_bh_page(bh, real->b_page, bh_offset(real));
  418. bh->b_blocknr = blkno;
  419. bh->b_size = sdp->sd_sb.sb_bsize;
  420. bh->b_bdev = sdp->sd_vfs->s_bdev;
  421. log_incr_head(sdp);
  422. return bh;
  423. }
  424. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  425. {
  426. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  427. ail2_empty(sdp, new_tail);
  428. gfs2_log_lock(sdp);
  429. sdp->sd_log_blks_free += dist;
  430. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
  431. gfs2_log_unlock(sdp);
  432. sdp->sd_log_tail = new_tail;
  433. }
  434. /**
  435. * log_write_header - Get and initialize a journal header buffer
  436. * @sdp: The GFS2 superblock
  437. *
  438. * Returns: the initialized log buffer descriptor
  439. */
  440. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  441. {
  442. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  443. struct buffer_head *bh;
  444. struct gfs2_log_header *lh;
  445. unsigned int tail;
  446. u32 hash;
  447. bh = sb_getblk(sdp->sd_vfs, blkno);
  448. lock_buffer(bh);
  449. memset(bh->b_data, 0, bh->b_size);
  450. set_buffer_uptodate(bh);
  451. clear_buffer_dirty(bh);
  452. unlock_buffer(bh);
  453. gfs2_ail1_empty(sdp, 0);
  454. tail = current_tail(sdp);
  455. lh = (struct gfs2_log_header *)bh->b_data;
  456. memset(lh, 0, sizeof(struct gfs2_log_header));
  457. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  458. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  459. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  460. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  461. lh->lh_flags = cpu_to_be32(flags);
  462. lh->lh_tail = cpu_to_be32(tail);
  463. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  464. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  465. lh->lh_hash = cpu_to_be32(hash);
  466. set_buffer_dirty(bh);
  467. if (sync_dirty_buffer(bh))
  468. gfs2_io_error_bh(sdp, bh);
  469. brelse(bh);
  470. if (sdp->sd_log_tail != tail)
  471. log_pull_tail(sdp, tail);
  472. else
  473. gfs2_assert_withdraw(sdp, !pull);
  474. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  475. log_incr_head(sdp);
  476. }
  477. static void log_flush_commit(struct gfs2_sbd *sdp)
  478. {
  479. struct list_head *head = &sdp->sd_log_flush_list;
  480. struct gfs2_log_buf *lb;
  481. struct buffer_head *bh;
  482. while (!list_empty(head)) {
  483. lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
  484. list_del(&lb->lb_list);
  485. bh = lb->lb_bh;
  486. wait_on_buffer(bh);
  487. if (!buffer_uptodate(bh))
  488. gfs2_io_error_bh(sdp, bh);
  489. if (lb->lb_real) {
  490. while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
  491. schedule();
  492. free_buffer_head(bh);
  493. } else
  494. brelse(bh);
  495. kfree(lb);
  496. }
  497. log_write_header(sdp, 0, 0);
  498. }
  499. /**
  500. * gfs2_log_flush - flush incore transaction(s)
  501. * @sdp: the filesystem
  502. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  503. *
  504. */
  505. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  506. {
  507. struct gfs2_ail *ai;
  508. down_write(&sdp->sd_log_flush_lock);
  509. if (gl) {
  510. gfs2_log_lock(sdp);
  511. if (list_empty(&gl->gl_le.le_list)) {
  512. gfs2_log_unlock(sdp);
  513. up_write(&sdp->sd_log_flush_lock);
  514. return;
  515. }
  516. gfs2_log_unlock(sdp);
  517. }
  518. ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
  519. INIT_LIST_HEAD(&ai->ai_ail1_list);
  520. INIT_LIST_HEAD(&ai->ai_ail2_list);
  521. gfs2_assert_withdraw(sdp,
  522. sdp->sd_log_num_buf + sdp->sd_log_num_jdata ==
  523. sdp->sd_log_commited_buf +
  524. sdp->sd_log_commited_databuf);
  525. gfs2_assert_withdraw(sdp,
  526. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  527. sdp->sd_log_flush_head = sdp->sd_log_head;
  528. sdp->sd_log_flush_wrapped = 0;
  529. ai->ai_first = sdp->sd_log_flush_head;
  530. lops_before_commit(sdp);
  531. if (!list_empty(&sdp->sd_log_flush_list))
  532. log_flush_commit(sdp);
  533. else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  534. gfs2_log_lock(sdp);
  535. sdp->sd_log_blks_free--; /* Adjust for unreserved buffer */
  536. gfs2_log_unlock(sdp);
  537. log_write_header(sdp, 0, PULL);
  538. }
  539. lops_after_commit(sdp, ai);
  540. gfs2_log_lock(sdp);
  541. sdp->sd_log_head = sdp->sd_log_flush_head;
  542. sdp->sd_log_blks_reserved = 0;
  543. sdp->sd_log_commited_buf = 0;
  544. sdp->sd_log_commited_databuf = 0;
  545. sdp->sd_log_commited_revoke = 0;
  546. if (!list_empty(&ai->ai_ail1_list)) {
  547. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  548. ai = NULL;
  549. }
  550. gfs2_log_unlock(sdp);
  551. sdp->sd_vfs->s_dirt = 0;
  552. up_write(&sdp->sd_log_flush_lock);
  553. kfree(ai);
  554. }
  555. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  556. {
  557. unsigned int reserved;
  558. unsigned int old;
  559. gfs2_log_lock(sdp);
  560. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  561. sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
  562. tr->tr_num_databuf_rm;
  563. gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
  564. (((int)sdp->sd_log_commited_databuf) >= 0));
  565. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  566. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
  567. reserved = calc_reserved(sdp);
  568. old = sdp->sd_log_blks_free;
  569. sdp->sd_log_blks_free += tr->tr_reserved -
  570. (reserved - sdp->sd_log_blks_reserved);
  571. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
  572. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <=
  573. sdp->sd_jdesc->jd_blocks);
  574. sdp->sd_log_blks_reserved = reserved;
  575. gfs2_log_unlock(sdp);
  576. }
  577. /**
  578. * gfs2_log_commit - Commit a transaction to the log
  579. * @sdp: the filesystem
  580. * @tr: the transaction
  581. *
  582. * Returns: errno
  583. */
  584. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  585. {
  586. log_refund(sdp, tr);
  587. lops_incore_commit(sdp, tr);
  588. sdp->sd_vfs->s_dirt = 1;
  589. up_read(&sdp->sd_log_flush_lock);
  590. gfs2_log_lock(sdp);
  591. if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
  592. wake_up_process(sdp->sd_logd_process);
  593. gfs2_log_unlock(sdp);
  594. }
  595. /**
  596. * gfs2_log_shutdown - write a shutdown header into a journal
  597. * @sdp: the filesystem
  598. *
  599. */
  600. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  601. {
  602. down_write(&sdp->sd_log_flush_lock);
  603. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  604. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
  605. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  606. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
  607. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  608. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  609. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  610. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  611. sdp->sd_log_flush_head = sdp->sd_log_head;
  612. sdp->sd_log_flush_wrapped = 0;
  613. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
  614. (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
  615. gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
  616. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  617. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  618. sdp->sd_log_head = sdp->sd_log_flush_head;
  619. sdp->sd_log_tail = sdp->sd_log_head;
  620. up_write(&sdp->sd_log_flush_lock);
  621. }
  622. /**
  623. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  624. * @sdp: the filesystem
  625. *
  626. */
  627. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  628. {
  629. gfs2_log_flush(sdp, NULL);
  630. for (;;) {
  631. gfs2_ail1_start(sdp, DIO_ALL);
  632. if (gfs2_ail1_empty(sdp, DIO_ALL))
  633. break;
  634. msleep(10);
  635. }
  636. }