lops.c 19 KB

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