lops.c 21 KB

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