lops.c 19 KB

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