lops.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/bio.h>
  16. #include <linux/fs.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "inode.h"
  20. #include "glock.h"
  21. #include "log.h"
  22. #include "lops.h"
  23. #include "meta_io.h"
  24. #include "recovery.h"
  25. #include "rgrp.h"
  26. #include "trans.h"
  27. #include "util.h"
  28. #include "trace_gfs2.h"
  29. /**
  30. * gfs2_pin - Pin a buffer in memory
  31. * @sdp: The superblock
  32. * @bh: The buffer to be pinned
  33. *
  34. * The log lock must be held when calling this function
  35. */
  36. static void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
  37. {
  38. struct gfs2_bufdata *bd;
  39. gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
  40. clear_buffer_dirty(bh);
  41. if (test_set_buffer_pinned(bh))
  42. gfs2_assert_withdraw(sdp, 0);
  43. if (!buffer_uptodate(bh))
  44. gfs2_io_error_bh(sdp, bh);
  45. bd = bh->b_private;
  46. /* If this buffer is in the AIL and it has already been written
  47. * to in-place disk block, remove it from the AIL.
  48. */
  49. spin_lock(&sdp->sd_ail_lock);
  50. if (bd->bd_ail)
  51. list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
  52. spin_unlock(&sdp->sd_ail_lock);
  53. get_bh(bh);
  54. atomic_inc(&sdp->sd_log_pinned);
  55. trace_gfs2_pin(bd, 1);
  56. }
  57. /**
  58. * gfs2_unpin - Unpin a buffer
  59. * @sdp: the filesystem the buffer belongs to
  60. * @bh: The buffer to unpin
  61. * @ai:
  62. *
  63. */
  64. static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  65. struct gfs2_ail *ai)
  66. {
  67. struct gfs2_bufdata *bd = bh->b_private;
  68. gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
  69. if (!buffer_pinned(bh))
  70. gfs2_assert_withdraw(sdp, 0);
  71. lock_buffer(bh);
  72. mark_buffer_dirty(bh);
  73. clear_buffer_pinned(bh);
  74. spin_lock(&sdp->sd_ail_lock);
  75. if (bd->bd_ail) {
  76. list_del(&bd->bd_ail_st_list);
  77. brelse(bh);
  78. } else {
  79. struct gfs2_glock *gl = bd->bd_gl;
  80. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  81. atomic_inc(&gl->gl_ail_count);
  82. }
  83. bd->bd_ail = ai;
  84. list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  85. spin_unlock(&sdp->sd_ail_lock);
  86. if (test_and_clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags))
  87. gfs2_glock_schedule_for_reclaim(bd->bd_gl);
  88. trace_gfs2_pin(bd, 0);
  89. unlock_buffer(bh);
  90. atomic_dec(&sdp->sd_log_pinned);
  91. }
  92. static inline struct gfs2_log_descriptor *bh_log_desc(struct buffer_head *bh)
  93. {
  94. return (struct gfs2_log_descriptor *)bh->b_data;
  95. }
  96. static inline __be64 *bh_log_ptr(struct buffer_head *bh)
  97. {
  98. struct gfs2_log_descriptor *ld = bh_log_desc(bh);
  99. return (__force __be64 *)(ld + 1);
  100. }
  101. static inline __be64 *bh_ptr_end(struct buffer_head *bh)
  102. {
  103. return (__force __be64 *)(bh->b_data + bh->b_size);
  104. }
  105. static struct buffer_head *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type)
  106. {
  107. struct buffer_head *bh = gfs2_log_get_buf(sdp);
  108. struct gfs2_log_descriptor *ld = bh_log_desc(bh);
  109. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  110. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  111. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  112. ld->ld_type = cpu_to_be32(ld_type);
  113. ld->ld_length = 0;
  114. ld->ld_data1 = 0;
  115. ld->ld_data2 = 0;
  116. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  117. return bh;
  118. }
  119. static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  120. {
  121. struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
  122. struct gfs2_meta_header *mh;
  123. struct gfs2_trans *tr;
  124. lock_buffer(bd->bd_bh);
  125. gfs2_log_lock(sdp);
  126. if (!list_empty(&bd->bd_list_tr))
  127. goto out;
  128. tr = current->journal_info;
  129. tr->tr_touched = 1;
  130. tr->tr_num_buf++;
  131. list_add(&bd->bd_list_tr, &tr->tr_list_buf);
  132. if (!list_empty(&le->le_list))
  133. goto out;
  134. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  135. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  136. gfs2_meta_check(sdp, bd->bd_bh);
  137. gfs2_pin(sdp, bd->bd_bh);
  138. mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
  139. mh->__pad0 = cpu_to_be64(0);
  140. mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  141. sdp->sd_log_num_buf++;
  142. list_add(&le->le_list, &sdp->sd_log_le_buf);
  143. tr->tr_num_buf_new++;
  144. out:
  145. gfs2_log_unlock(sdp);
  146. unlock_buffer(bd->bd_bh);
  147. }
  148. static void buf_lo_before_commit(struct gfs2_sbd *sdp)
  149. {
  150. struct buffer_head *bh;
  151. struct gfs2_log_descriptor *ld;
  152. struct gfs2_bufdata *bd1 = NULL, *bd2;
  153. unsigned int total;
  154. unsigned int limit;
  155. unsigned int num;
  156. unsigned n;
  157. __be64 *ptr;
  158. limit = buf_limit(sdp);
  159. /* for 4k blocks, limit = 503 */
  160. gfs2_log_lock(sdp);
  161. total = sdp->sd_log_num_buf;
  162. bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
  163. while(total) {
  164. num = total;
  165. if (total > limit)
  166. num = limit;
  167. gfs2_log_unlock(sdp);
  168. bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_METADATA);
  169. gfs2_log_lock(sdp);
  170. ld = bh_log_desc(bh);
  171. ptr = bh_log_ptr(bh);
  172. ld->ld_length = cpu_to_be32(num + 1);
  173. ld->ld_data1 = cpu_to_be32(num);
  174. n = 0;
  175. list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
  176. bd_le.le_list) {
  177. *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
  178. if (++n >= num)
  179. break;
  180. }
  181. gfs2_log_unlock(sdp);
  182. submit_bh(WRITE_SYNC, bh);
  183. gfs2_log_lock(sdp);
  184. n = 0;
  185. list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
  186. bd_le.le_list) {
  187. get_bh(bd2->bd_bh);
  188. gfs2_log_unlock(sdp);
  189. lock_buffer(bd2->bd_bh);
  190. bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
  191. submit_bh(WRITE_SYNC, bh);
  192. gfs2_log_lock(sdp);
  193. if (++n >= num)
  194. break;
  195. }
  196. BUG_ON(total < num);
  197. total -= num;
  198. }
  199. gfs2_log_unlock(sdp);
  200. }
  201. static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  202. {
  203. struct list_head *head = &sdp->sd_log_le_buf;
  204. struct gfs2_bufdata *bd;
  205. while (!list_empty(head)) {
  206. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  207. list_del_init(&bd->bd_le.le_list);
  208. sdp->sd_log_num_buf--;
  209. gfs2_unpin(sdp, bd->bd_bh, ai);
  210. }
  211. gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
  212. }
  213. static void buf_lo_before_scan(struct gfs2_jdesc *jd,
  214. struct gfs2_log_header_host *head, int pass)
  215. {
  216. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  217. if (pass != 0)
  218. return;
  219. sdp->sd_found_blocks = 0;
  220. sdp->sd_replayed_blocks = 0;
  221. }
  222. static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  223. struct gfs2_log_descriptor *ld, __be64 *ptr,
  224. int pass)
  225. {
  226. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  227. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  228. struct gfs2_glock *gl = ip->i_gl;
  229. unsigned int blks = be32_to_cpu(ld->ld_data1);
  230. struct buffer_head *bh_log, *bh_ip;
  231. u64 blkno;
  232. int error = 0;
  233. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
  234. return 0;
  235. gfs2_replay_incr_blk(sdp, &start);
  236. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  237. blkno = be64_to_cpu(*ptr++);
  238. sdp->sd_found_blocks++;
  239. if (gfs2_revoke_check(sdp, blkno, start))
  240. continue;
  241. error = gfs2_replay_read_block(jd, start, &bh_log);
  242. if (error)
  243. return error;
  244. bh_ip = gfs2_meta_new(gl, blkno);
  245. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  246. if (gfs2_meta_check(sdp, bh_ip))
  247. error = -EIO;
  248. else
  249. mark_buffer_dirty(bh_ip);
  250. brelse(bh_log);
  251. brelse(bh_ip);
  252. if (error)
  253. break;
  254. sdp->sd_replayed_blocks++;
  255. }
  256. return error;
  257. }
  258. static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  259. {
  260. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  261. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  262. if (error) {
  263. gfs2_meta_sync(ip->i_gl);
  264. return;
  265. }
  266. if (pass != 1)
  267. return;
  268. gfs2_meta_sync(ip->i_gl);
  269. fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
  270. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  271. }
  272. static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  273. {
  274. struct gfs2_trans *tr;
  275. tr = current->journal_info;
  276. tr->tr_touched = 1;
  277. tr->tr_num_revoke++;
  278. sdp->sd_log_num_revoke++;
  279. list_add(&le->le_list, &sdp->sd_log_le_revoke);
  280. }
  281. static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
  282. {
  283. struct gfs2_log_descriptor *ld;
  284. struct gfs2_meta_header *mh;
  285. struct buffer_head *bh;
  286. unsigned int offset;
  287. struct list_head *head = &sdp->sd_log_le_revoke;
  288. struct gfs2_bufdata *bd;
  289. if (!sdp->sd_log_num_revoke)
  290. return;
  291. bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE);
  292. ld = bh_log_desc(bh);
  293. ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
  294. sizeof(u64)));
  295. ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
  296. offset = sizeof(struct gfs2_log_descriptor);
  297. while (!list_empty(head)) {
  298. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  299. list_del_init(&bd->bd_le.le_list);
  300. sdp->sd_log_num_revoke--;
  301. if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
  302. submit_bh(WRITE_SYNC, bh);
  303. bh = gfs2_log_get_buf(sdp);
  304. mh = (struct gfs2_meta_header *)bh->b_data;
  305. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  306. mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
  307. mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
  308. offset = sizeof(struct gfs2_meta_header);
  309. }
  310. *(__be64 *)(bh->b_data + offset) = cpu_to_be64(bd->bd_blkno);
  311. kmem_cache_free(gfs2_bufdata_cachep, bd);
  312. offset += sizeof(u64);
  313. }
  314. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  315. submit_bh(WRITE_SYNC, bh);
  316. }
  317. static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
  318. struct gfs2_log_header_host *head, int pass)
  319. {
  320. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  321. if (pass != 0)
  322. return;
  323. sdp->sd_found_revokes = 0;
  324. sdp->sd_replay_tail = head->lh_tail;
  325. }
  326. static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  327. struct gfs2_log_descriptor *ld, __be64 *ptr,
  328. int pass)
  329. {
  330. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  331. unsigned int blks = be32_to_cpu(ld->ld_length);
  332. unsigned int revokes = be32_to_cpu(ld->ld_data1);
  333. struct buffer_head *bh;
  334. unsigned int offset;
  335. u64 blkno;
  336. int first = 1;
  337. int error;
  338. if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
  339. return 0;
  340. offset = sizeof(struct gfs2_log_descriptor);
  341. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  342. error = gfs2_replay_read_block(jd, start, &bh);
  343. if (error)
  344. return error;
  345. if (!first)
  346. gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
  347. while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
  348. blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
  349. error = gfs2_revoke_add(sdp, blkno, start);
  350. if (error < 0) {
  351. brelse(bh);
  352. return error;
  353. }
  354. else if (error)
  355. sdp->sd_found_revokes++;
  356. if (!--revokes)
  357. break;
  358. offset += sizeof(u64);
  359. }
  360. brelse(bh);
  361. offset = sizeof(struct gfs2_meta_header);
  362. first = 0;
  363. }
  364. return 0;
  365. }
  366. static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  367. {
  368. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  369. if (error) {
  370. gfs2_revoke_clean(sdp);
  371. return;
  372. }
  373. if (pass != 1)
  374. return;
  375. fs_info(sdp, "jid=%u: Found %u revoke tags\n",
  376. jd->jd_jid, sdp->sd_found_revokes);
  377. gfs2_revoke_clean(sdp);
  378. }
  379. static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  380. {
  381. struct gfs2_rgrpd *rgd;
  382. struct gfs2_trans *tr = current->journal_info;
  383. tr->tr_touched = 1;
  384. rgd = container_of(le, struct gfs2_rgrpd, rd_le);
  385. gfs2_log_lock(sdp);
  386. if (!list_empty(&le->le_list)){
  387. gfs2_log_unlock(sdp);
  388. return;
  389. }
  390. gfs2_rgrp_bh_hold(rgd);
  391. sdp->sd_log_num_rg++;
  392. list_add(&le->le_list, &sdp->sd_log_le_rg);
  393. gfs2_log_unlock(sdp);
  394. }
  395. static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  396. {
  397. struct list_head *head = &sdp->sd_log_le_rg;
  398. struct gfs2_rgrpd *rgd;
  399. while (!list_empty(head)) {
  400. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
  401. list_del_init(&rgd->rd_le.le_list);
  402. sdp->sd_log_num_rg--;
  403. gfs2_rgrp_repolish_clones(rgd);
  404. gfs2_rgrp_bh_put(rgd);
  405. }
  406. gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
  407. }
  408. /**
  409. * databuf_lo_add - Add a databuf to the transaction.
  410. *
  411. * This is used in two distinct cases:
  412. * i) In ordered write mode
  413. * We put the data buffer on a list so that we can ensure that its
  414. * synced to disk at the right time
  415. * ii) In journaled data mode
  416. * We need to journal the data block in the same way as metadata in
  417. * the functions above. The difference is that here we have a tag
  418. * which is two __be64's being the block number (as per meta data)
  419. * and a flag which says whether the data block needs escaping or
  420. * not. This means we need a new log entry for each 251 or so data
  421. * blocks, which isn't an enormous overhead but twice as much as
  422. * for normal metadata blocks.
  423. */
  424. static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  425. {
  426. struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
  427. struct gfs2_trans *tr = current->journal_info;
  428. struct address_space *mapping = bd->bd_bh->b_page->mapping;
  429. struct gfs2_inode *ip = GFS2_I(mapping->host);
  430. lock_buffer(bd->bd_bh);
  431. gfs2_log_lock(sdp);
  432. if (tr) {
  433. if (!list_empty(&bd->bd_list_tr))
  434. goto out;
  435. tr->tr_touched = 1;
  436. if (gfs2_is_jdata(ip)) {
  437. tr->tr_num_buf++;
  438. list_add(&bd->bd_list_tr, &tr->tr_list_buf);
  439. }
  440. }
  441. if (!list_empty(&le->le_list))
  442. goto out;
  443. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  444. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  445. if (gfs2_is_jdata(ip)) {
  446. gfs2_pin(sdp, bd->bd_bh);
  447. tr->tr_num_databuf_new++;
  448. sdp->sd_log_num_databuf++;
  449. list_add_tail(&le->le_list, &sdp->sd_log_le_databuf);
  450. } else {
  451. list_add_tail(&le->le_list, &sdp->sd_log_le_ordered);
  452. }
  453. out:
  454. gfs2_log_unlock(sdp);
  455. unlock_buffer(bd->bd_bh);
  456. }
  457. static void gfs2_check_magic(struct buffer_head *bh)
  458. {
  459. void *kaddr;
  460. __be32 *ptr;
  461. clear_buffer_escaped(bh);
  462. kaddr = kmap_atomic(bh->b_page, KM_USER0);
  463. ptr = kaddr + bh_offset(bh);
  464. if (*ptr == cpu_to_be32(GFS2_MAGIC))
  465. set_buffer_escaped(bh);
  466. kunmap_atomic(kaddr, KM_USER0);
  467. }
  468. static void gfs2_write_blocks(struct gfs2_sbd *sdp, struct buffer_head *bh,
  469. struct list_head *list, struct list_head *done,
  470. unsigned int n)
  471. {
  472. struct buffer_head *bh1;
  473. struct gfs2_log_descriptor *ld;
  474. struct gfs2_bufdata *bd;
  475. __be64 *ptr;
  476. if (!bh)
  477. return;
  478. ld = bh_log_desc(bh);
  479. ld->ld_length = cpu_to_be32(n + 1);
  480. ld->ld_data1 = cpu_to_be32(n);
  481. ptr = bh_log_ptr(bh);
  482. get_bh(bh);
  483. submit_bh(WRITE_SYNC, bh);
  484. gfs2_log_lock(sdp);
  485. while(!list_empty(list)) {
  486. bd = list_entry(list->next, struct gfs2_bufdata, bd_le.le_list);
  487. list_move_tail(&bd->bd_le.le_list, done);
  488. get_bh(bd->bd_bh);
  489. while (be64_to_cpu(*ptr) != bd->bd_bh->b_blocknr) {
  490. gfs2_log_incr_head(sdp);
  491. ptr += 2;
  492. }
  493. gfs2_log_unlock(sdp);
  494. lock_buffer(bd->bd_bh);
  495. if (buffer_escaped(bd->bd_bh)) {
  496. void *kaddr;
  497. bh1 = gfs2_log_get_buf(sdp);
  498. kaddr = kmap_atomic(bd->bd_bh->b_page, KM_USER0);
  499. memcpy(bh1->b_data, kaddr + bh_offset(bd->bd_bh),
  500. bh1->b_size);
  501. kunmap_atomic(kaddr, KM_USER0);
  502. *(__be32 *)bh1->b_data = 0;
  503. clear_buffer_escaped(bd->bd_bh);
  504. unlock_buffer(bd->bd_bh);
  505. brelse(bd->bd_bh);
  506. } else {
  507. bh1 = gfs2_log_fake_buf(sdp, bd->bd_bh);
  508. }
  509. submit_bh(WRITE_SYNC, bh1);
  510. gfs2_log_lock(sdp);
  511. ptr += 2;
  512. }
  513. gfs2_log_unlock(sdp);
  514. brelse(bh);
  515. }
  516. /**
  517. * databuf_lo_before_commit - Scan the data buffers, writing as we go
  518. *
  519. */
  520. static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
  521. {
  522. struct gfs2_bufdata *bd = NULL;
  523. struct buffer_head *bh = NULL;
  524. unsigned int n = 0;
  525. __be64 *ptr = NULL, *end = NULL;
  526. LIST_HEAD(processed);
  527. LIST_HEAD(in_progress);
  528. gfs2_log_lock(sdp);
  529. while (!list_empty(&sdp->sd_log_le_databuf)) {
  530. if (ptr == end) {
  531. gfs2_log_unlock(sdp);
  532. gfs2_write_blocks(sdp, bh, &in_progress, &processed, n);
  533. n = 0;
  534. bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_JDATA);
  535. ptr = bh_log_ptr(bh);
  536. end = bh_ptr_end(bh) - 1;
  537. gfs2_log_lock(sdp);
  538. continue;
  539. }
  540. bd = list_entry(sdp->sd_log_le_databuf.next, struct gfs2_bufdata, bd_le.le_list);
  541. list_move_tail(&bd->bd_le.le_list, &in_progress);
  542. gfs2_check_magic(bd->bd_bh);
  543. *ptr++ = cpu_to_be64(bd->bd_bh->b_blocknr);
  544. *ptr++ = cpu_to_be64(buffer_escaped(bh) ? 1 : 0);
  545. n++;
  546. }
  547. gfs2_log_unlock(sdp);
  548. gfs2_write_blocks(sdp, bh, &in_progress, &processed, n);
  549. gfs2_log_lock(sdp);
  550. list_splice(&processed, &sdp->sd_log_le_databuf);
  551. gfs2_log_unlock(sdp);
  552. }
  553. static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  554. struct gfs2_log_descriptor *ld,
  555. __be64 *ptr, int pass)
  556. {
  557. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  558. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  559. struct gfs2_glock *gl = ip->i_gl;
  560. unsigned int blks = be32_to_cpu(ld->ld_data1);
  561. struct buffer_head *bh_log, *bh_ip;
  562. u64 blkno;
  563. u64 esc;
  564. int error = 0;
  565. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  566. return 0;
  567. gfs2_replay_incr_blk(sdp, &start);
  568. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  569. blkno = be64_to_cpu(*ptr++);
  570. esc = be64_to_cpu(*ptr++);
  571. sdp->sd_found_blocks++;
  572. if (gfs2_revoke_check(sdp, blkno, start))
  573. continue;
  574. error = gfs2_replay_read_block(jd, start, &bh_log);
  575. if (error)
  576. return error;
  577. bh_ip = gfs2_meta_new(gl, blkno);
  578. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  579. /* Unescape */
  580. if (esc) {
  581. __be32 *eptr = (__be32 *)bh_ip->b_data;
  582. *eptr = cpu_to_be32(GFS2_MAGIC);
  583. }
  584. mark_buffer_dirty(bh_ip);
  585. brelse(bh_log);
  586. brelse(bh_ip);
  587. if (error)
  588. break;
  589. sdp->sd_replayed_blocks++;
  590. }
  591. return error;
  592. }
  593. /* FIXME: sort out accounting for log blocks etc. */
  594. static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  595. {
  596. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  597. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  598. if (error) {
  599. gfs2_meta_sync(ip->i_gl);
  600. return;
  601. }
  602. if (pass != 1)
  603. return;
  604. /* data sync? */
  605. gfs2_meta_sync(ip->i_gl);
  606. fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
  607. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  608. }
  609. static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  610. {
  611. struct list_head *head = &sdp->sd_log_le_databuf;
  612. struct gfs2_bufdata *bd;
  613. while (!list_empty(head)) {
  614. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  615. list_del_init(&bd->bd_le.le_list);
  616. sdp->sd_log_num_databuf--;
  617. gfs2_unpin(sdp, bd->bd_bh, ai);
  618. }
  619. gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
  620. }
  621. const struct gfs2_log_operations gfs2_buf_lops = {
  622. .lo_add = buf_lo_add,
  623. .lo_before_commit = buf_lo_before_commit,
  624. .lo_after_commit = buf_lo_after_commit,
  625. .lo_before_scan = buf_lo_before_scan,
  626. .lo_scan_elements = buf_lo_scan_elements,
  627. .lo_after_scan = buf_lo_after_scan,
  628. .lo_name = "buf",
  629. };
  630. const struct gfs2_log_operations gfs2_revoke_lops = {
  631. .lo_add = revoke_lo_add,
  632. .lo_before_commit = revoke_lo_before_commit,
  633. .lo_before_scan = revoke_lo_before_scan,
  634. .lo_scan_elements = revoke_lo_scan_elements,
  635. .lo_after_scan = revoke_lo_after_scan,
  636. .lo_name = "revoke",
  637. };
  638. const struct gfs2_log_operations gfs2_rg_lops = {
  639. .lo_add = rg_lo_add,
  640. .lo_after_commit = rg_lo_after_commit,
  641. .lo_name = "rg",
  642. };
  643. const struct gfs2_log_operations gfs2_databuf_lops = {
  644. .lo_add = databuf_lo_add,
  645. .lo_before_commit = databuf_lo_before_commit,
  646. .lo_after_commit = databuf_lo_after_commit,
  647. .lo_scan_elements = databuf_lo_scan_elements,
  648. .lo_after_scan = databuf_lo_after_scan,
  649. .lo_name = "databuf",
  650. };
  651. const struct gfs2_log_operations *gfs2_log_ops[] = {
  652. &gfs2_databuf_lops,
  653. &gfs2_buf_lops,
  654. &gfs2_rg_lops,
  655. &gfs2_revoke_lops,
  656. NULL,
  657. };