lops.c 19 KB

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