lops.c 19 KB

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