lops.c 19 KB

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