log.c 14 KB

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