lops.c 20 KB

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