lops.c 19 KB

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