lops.c 19 KB

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