log.c 21 KB

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