log.c 19 KB

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