log.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include <linux/lm_interface.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "bmap.h"
  20. #include "glock.h"
  21. #include "log.h"
  22. #include "lops.h"
  23. #include "meta_io.h"
  24. #include "util.h"
  25. #include "dir.h"
  26. #define PULL 1
  27. /**
  28. * gfs2_struct2blk - compute stuff
  29. * @sdp: the filesystem
  30. * @nstruct: the number of structures
  31. * @ssize: the size of the structures
  32. *
  33. * Compute the number of log descriptor blocks needed to hold a certain number
  34. * of structures of a certain size.
  35. *
  36. * Returns: the number of blocks needed (minimum is always 1)
  37. */
  38. unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
  39. unsigned int ssize)
  40. {
  41. unsigned int blks;
  42. unsigned int first, second;
  43. blks = 1;
  44. first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
  45. if (nstruct > first) {
  46. second = (sdp->sd_sb.sb_bsize -
  47. sizeof(struct gfs2_meta_header)) / ssize;
  48. blks += DIV_ROUND_UP(nstruct - first, second);
  49. }
  50. return blks;
  51. }
  52. void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
  53. {
  54. struct list_head *head = &sdp->sd_ail1_list;
  55. u64 sync_gen;
  56. struct list_head *first;
  57. struct gfs2_ail *first_ai, *ai, *tmp;
  58. int done = 0;
  59. gfs2_log_lock(sdp);
  60. if (list_empty(head)) {
  61. gfs2_log_unlock(sdp);
  62. return;
  63. }
  64. sync_gen = sdp->sd_ail_sync_gen++;
  65. first = head->prev;
  66. first_ai = list_entry(first, struct gfs2_ail, ai_list);
  67. first_ai->ai_sync_gen = sync_gen;
  68. gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
  69. if (flags & DIO_ALL)
  70. first = NULL;
  71. while(!done) {
  72. if (first && (head->prev != first ||
  73. gfs2_ail1_empty_one(sdp, first_ai, 0)))
  74. break;
  75. done = 1;
  76. list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
  77. if (ai->ai_sync_gen >= sync_gen)
  78. continue;
  79. ai->ai_sync_gen = sync_gen;
  80. gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
  81. done = 0;
  82. break;
  83. }
  84. }
  85. gfs2_log_unlock(sdp);
  86. }
  87. int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
  88. {
  89. struct gfs2_ail *ai, *s;
  90. int ret;
  91. gfs2_log_lock(sdp);
  92. list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
  93. if (gfs2_ail1_empty_one(sdp, ai, flags))
  94. list_move(&ai->ai_list, &sdp->sd_ail2_list);
  95. else if (!(flags & DIO_ALL))
  96. break;
  97. }
  98. ret = list_empty(&sdp->sd_ail1_list);
  99. gfs2_log_unlock(sdp);
  100. return ret;
  101. }
  102. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  103. {
  104. struct gfs2_ail *ai, *safe;
  105. unsigned int old_tail = sdp->sd_log_tail;
  106. int wrap = (new_tail < old_tail);
  107. int a, b, rm;
  108. gfs2_log_lock(sdp);
  109. list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
  110. a = (old_tail <= ai->ai_first);
  111. b = (ai->ai_first < new_tail);
  112. rm = (wrap) ? (a || b) : (a && b);
  113. if (!rm)
  114. continue;
  115. gfs2_ail2_empty_one(sdp, ai);
  116. list_del(&ai->ai_list);
  117. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
  118. gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
  119. kfree(ai);
  120. }
  121. gfs2_log_unlock(sdp);
  122. }
  123. /**
  124. * gfs2_log_reserve - Make a log reservation
  125. * @sdp: The GFS2 superblock
  126. * @blks: The number of blocks to reserve
  127. *
  128. * Returns: errno
  129. */
  130. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  131. {
  132. unsigned int try = 0;
  133. if (gfs2_assert_warn(sdp, blks) ||
  134. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  135. return -EINVAL;
  136. mutex_lock(&sdp->sd_log_reserve_mutex);
  137. gfs2_log_lock(sdp);
  138. while(sdp->sd_log_blks_free <= blks) {
  139. gfs2_log_unlock(sdp);
  140. gfs2_ail1_empty(sdp, 0);
  141. gfs2_log_flush(sdp, NULL);
  142. if (try++)
  143. gfs2_ail1_start(sdp, 0);
  144. gfs2_log_lock(sdp);
  145. }
  146. sdp->sd_log_blks_free -= blks;
  147. gfs2_log_unlock(sdp);
  148. mutex_unlock(&sdp->sd_log_reserve_mutex);
  149. down_read(&sdp->sd_log_flush_lock);
  150. return 0;
  151. }
  152. /**
  153. * gfs2_log_release - Release a given number of log blocks
  154. * @sdp: The GFS2 superblock
  155. * @blks: The number of blocks
  156. *
  157. */
  158. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  159. {
  160. gfs2_log_lock(sdp);
  161. sdp->sd_log_blks_free += blks;
  162. gfs2_assert_withdraw(sdp,
  163. sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
  164. gfs2_log_unlock(sdp);
  165. up_read(&sdp->sd_log_flush_lock);
  166. }
  167. static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
  168. {
  169. int error;
  170. struct buffer_head bh_map;
  171. error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1);
  172. if (error || !bh_map.b_blocknr)
  173. printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
  174. gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
  175. return bh_map.b_blocknr;
  176. }
  177. /**
  178. * log_distance - Compute distance between two journal blocks
  179. * @sdp: The GFS2 superblock
  180. * @newer: The most recent journal block of the pair
  181. * @older: The older journal block of the pair
  182. *
  183. * Compute the distance (in the journal direction) between two
  184. * blocks in the journal
  185. *
  186. * Returns: the distance in blocks
  187. */
  188. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  189. unsigned int older)
  190. {
  191. int dist;
  192. dist = newer - older;
  193. if (dist < 0)
  194. dist += sdp->sd_jdesc->jd_blocks;
  195. return dist;
  196. }
  197. static unsigned int current_tail(struct gfs2_sbd *sdp)
  198. {
  199. struct gfs2_ail *ai;
  200. unsigned int tail;
  201. gfs2_log_lock(sdp);
  202. if (list_empty(&sdp->sd_ail1_list)) {
  203. tail = sdp->sd_log_head;
  204. } else {
  205. ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
  206. tail = ai->ai_first;
  207. }
  208. gfs2_log_unlock(sdp);
  209. return tail;
  210. }
  211. static inline void log_incr_head(struct gfs2_sbd *sdp)
  212. {
  213. if (sdp->sd_log_flush_head == sdp->sd_log_tail)
  214. gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
  215. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  216. sdp->sd_log_flush_head = 0;
  217. sdp->sd_log_flush_wrapped = 1;
  218. }
  219. }
  220. /**
  221. * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
  222. * @sdp: The GFS2 superblock
  223. *
  224. * Returns: the buffer_head
  225. */
  226. struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
  227. {
  228. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  229. struct gfs2_log_buf *lb;
  230. struct buffer_head *bh;
  231. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  232. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  233. bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
  234. lock_buffer(bh);
  235. memset(bh->b_data, 0, bh->b_size);
  236. set_buffer_uptodate(bh);
  237. clear_buffer_dirty(bh);
  238. unlock_buffer(bh);
  239. log_incr_head(sdp);
  240. return bh;
  241. }
  242. /**
  243. * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
  244. * @sdp: the filesystem
  245. * @data: the data the buffer_head should point to
  246. *
  247. * Returns: the log buffer descriptor
  248. */
  249. struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
  250. struct buffer_head *real)
  251. {
  252. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  253. struct gfs2_log_buf *lb;
  254. struct buffer_head *bh;
  255. lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
  256. list_add(&lb->lb_list, &sdp->sd_log_flush_list);
  257. lb->lb_real = real;
  258. bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
  259. atomic_set(&bh->b_count, 1);
  260. bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
  261. set_bh_page(bh, real->b_page, bh_offset(real));
  262. bh->b_blocknr = blkno;
  263. bh->b_size = sdp->sd_sb.sb_bsize;
  264. bh->b_bdev = sdp->sd_vfs->s_bdev;
  265. log_incr_head(sdp);
  266. return bh;
  267. }
  268. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
  269. {
  270. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  271. ail2_empty(sdp, new_tail);
  272. gfs2_log_lock(sdp);
  273. sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
  274. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
  275. gfs2_log_unlock(sdp);
  276. sdp->sd_log_tail = new_tail;
  277. }
  278. /**
  279. * log_write_header - Get and initialize a journal header buffer
  280. * @sdp: The GFS2 superblock
  281. *
  282. * Returns: the initialized log buffer descriptor
  283. */
  284. static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
  285. {
  286. u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
  287. struct buffer_head *bh;
  288. struct gfs2_log_header *lh;
  289. unsigned int tail;
  290. u32 hash;
  291. bh = sb_getblk(sdp->sd_vfs, blkno);
  292. lock_buffer(bh);
  293. memset(bh->b_data, 0, bh->b_size);
  294. set_buffer_uptodate(bh);
  295. clear_buffer_dirty(bh);
  296. unlock_buffer(bh);
  297. gfs2_ail1_empty(sdp, 0);
  298. tail = current_tail(sdp);
  299. lh = (struct gfs2_log_header *)bh->b_data;
  300. memset(lh, 0, sizeof(struct gfs2_log_header));
  301. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  302. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  303. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  304. lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
  305. lh->lh_flags = cpu_to_be32(flags);
  306. lh->lh_tail = cpu_to_be32(tail);
  307. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  308. hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
  309. lh->lh_hash = cpu_to_be32(hash);
  310. set_buffer_dirty(bh);
  311. if (sync_dirty_buffer(bh))
  312. gfs2_io_error_bh(sdp, bh);
  313. brelse(bh);
  314. if (sdp->sd_log_tail != tail)
  315. log_pull_tail(sdp, tail, pull);
  316. else
  317. gfs2_assert_withdraw(sdp, !pull);
  318. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  319. log_incr_head(sdp);
  320. }
  321. static void log_flush_commit(struct gfs2_sbd *sdp)
  322. {
  323. struct list_head *head = &sdp->sd_log_flush_list;
  324. struct gfs2_log_buf *lb;
  325. struct buffer_head *bh;
  326. while (!list_empty(head)) {
  327. lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
  328. list_del(&lb->lb_list);
  329. bh = lb->lb_bh;
  330. wait_on_buffer(bh);
  331. if (!buffer_uptodate(bh))
  332. gfs2_io_error_bh(sdp, bh);
  333. if (lb->lb_real) {
  334. while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
  335. schedule();
  336. free_buffer_head(bh);
  337. } else
  338. brelse(bh);
  339. kfree(lb);
  340. }
  341. log_write_header(sdp, 0, 0);
  342. }
  343. /**
  344. * gfs2_log_flush - flush incore transaction(s)
  345. * @sdp: the filesystem
  346. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  347. *
  348. */
  349. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
  350. {
  351. struct gfs2_ail *ai;
  352. down_write(&sdp->sd_log_flush_lock);
  353. if (gl) {
  354. gfs2_log_lock(sdp);
  355. if (list_empty(&gl->gl_le.le_list)) {
  356. gfs2_log_unlock(sdp);
  357. up_write(&sdp->sd_log_flush_lock);
  358. return;
  359. }
  360. gfs2_log_unlock(sdp);
  361. }
  362. ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
  363. INIT_LIST_HEAD(&ai->ai_ail1_list);
  364. INIT_LIST_HEAD(&ai->ai_ail2_list);
  365. gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
  366. gfs2_assert_withdraw(sdp,
  367. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  368. sdp->sd_log_flush_head = sdp->sd_log_head;
  369. sdp->sd_log_flush_wrapped = 0;
  370. ai->ai_first = sdp->sd_log_flush_head;
  371. lops_before_commit(sdp);
  372. if (!list_empty(&sdp->sd_log_flush_list))
  373. log_flush_commit(sdp);
  374. else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
  375. log_write_header(sdp, 0, PULL);
  376. lops_after_commit(sdp, ai);
  377. sdp->sd_log_head = sdp->sd_log_flush_head;
  378. sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
  379. sdp->sd_log_blks_reserved = 0;
  380. sdp->sd_log_commited_buf = 0;
  381. sdp->sd_log_num_hdrs = 0;
  382. sdp->sd_log_commited_revoke = 0;
  383. gfs2_log_lock(sdp);
  384. if (!list_empty(&ai->ai_ail1_list)) {
  385. list_add(&ai->ai_list, &sdp->sd_ail1_list);
  386. ai = NULL;
  387. }
  388. gfs2_log_unlock(sdp);
  389. sdp->sd_vfs->s_dirt = 0;
  390. up_write(&sdp->sd_log_flush_lock);
  391. kfree(ai);
  392. }
  393. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  394. {
  395. unsigned int reserved = 0;
  396. unsigned int old;
  397. gfs2_log_lock(sdp);
  398. sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
  399. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
  400. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  401. gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
  402. if (sdp->sd_log_commited_buf)
  403. reserved += sdp->sd_log_commited_buf;
  404. if (sdp->sd_log_commited_revoke)
  405. reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  406. sizeof(u64));
  407. if (reserved)
  408. reserved++;
  409. old = sdp->sd_log_blks_free;
  410. sdp->sd_log_blks_free += tr->tr_reserved -
  411. (reserved - sdp->sd_log_blks_reserved);
  412. gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
  413. gfs2_assert_withdraw(sdp,
  414. sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
  415. sdp->sd_log_num_hdrs);
  416. sdp->sd_log_blks_reserved = reserved;
  417. gfs2_log_unlock(sdp);
  418. }
  419. /**
  420. * gfs2_log_commit - Commit a transaction to the log
  421. * @sdp: the filesystem
  422. * @tr: the transaction
  423. *
  424. * Returns: errno
  425. */
  426. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  427. {
  428. log_refund(sdp, tr);
  429. lops_incore_commit(sdp, tr);
  430. sdp->sd_vfs->s_dirt = 1;
  431. up_read(&sdp->sd_log_flush_lock);
  432. gfs2_log_lock(sdp);
  433. if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
  434. gfs2_log_unlock(sdp);
  435. gfs2_log_flush(sdp, NULL);
  436. } else {
  437. gfs2_log_unlock(sdp);
  438. }
  439. }
  440. /**
  441. * gfs2_log_shutdown - write a shutdown header into a journal
  442. * @sdp: the filesystem
  443. *
  444. */
  445. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  446. {
  447. down_write(&sdp->sd_log_flush_lock);
  448. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  449. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
  450. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
  451. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
  452. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  453. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
  454. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
  455. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
  456. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  457. sdp->sd_log_flush_head = sdp->sd_log_head;
  458. sdp->sd_log_flush_wrapped = 0;
  459. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
  460. gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
  461. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  462. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  463. sdp->sd_log_head = sdp->sd_log_flush_head;
  464. sdp->sd_log_tail = sdp->sd_log_head;
  465. up_write(&sdp->sd_log_flush_lock);
  466. }