lops.c 19 KB

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