lops.c 20 KB

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