log.c 19 KB

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