lops.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. gfs2_log_lock(sdp);
  291. sdp->sd_log_num_revoke++;
  292. list_add(&le->le_list, &sdp->sd_log_le_revoke);
  293. gfs2_log_unlock(sdp);
  294. }
  295. static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
  296. {
  297. struct gfs2_log_descriptor *ld;
  298. struct gfs2_meta_header *mh;
  299. struct buffer_head *bh;
  300. unsigned int offset;
  301. struct list_head *head = &sdp->sd_log_le_revoke;
  302. struct gfs2_revoke *rv;
  303. if (!sdp->sd_log_num_revoke)
  304. return;
  305. bh = gfs2_log_get_buf(sdp);
  306. ld = (struct gfs2_log_descriptor *)bh->b_data;
  307. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  308. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  309. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  310. ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
  311. ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
  312. sizeof(u64)));
  313. ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
  314. ld->ld_data2 = cpu_to_be32(0);
  315. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  316. offset = sizeof(struct gfs2_log_descriptor);
  317. while (!list_empty(head)) {
  318. rv = list_entry(head->next, struct gfs2_revoke, rv_le.le_list);
  319. list_del_init(&rv->rv_le.le_list);
  320. sdp->sd_log_num_revoke--;
  321. if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
  322. set_buffer_dirty(bh);
  323. ll_rw_block(WRITE, 1, &bh);
  324. bh = gfs2_log_get_buf(sdp);
  325. mh = (struct gfs2_meta_header *)bh->b_data;
  326. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  327. mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
  328. mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
  329. offset = sizeof(struct gfs2_meta_header);
  330. }
  331. *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno);
  332. kfree(rv);
  333. offset += sizeof(u64);
  334. }
  335. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  336. set_buffer_dirty(bh);
  337. ll_rw_block(WRITE, 1, &bh);
  338. }
  339. static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
  340. struct gfs2_log_header_host *head, int pass)
  341. {
  342. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  343. if (pass != 0)
  344. return;
  345. sdp->sd_found_revokes = 0;
  346. sdp->sd_replay_tail = head->lh_tail;
  347. }
  348. static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  349. struct gfs2_log_descriptor *ld, __be64 *ptr,
  350. int pass)
  351. {
  352. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  353. unsigned int blks = be32_to_cpu(ld->ld_length);
  354. unsigned int revokes = be32_to_cpu(ld->ld_data1);
  355. struct buffer_head *bh;
  356. unsigned int offset;
  357. u64 blkno;
  358. int first = 1;
  359. int error;
  360. if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
  361. return 0;
  362. offset = sizeof(struct gfs2_log_descriptor);
  363. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  364. error = gfs2_replay_read_block(jd, start, &bh);
  365. if (error)
  366. return error;
  367. if (!first)
  368. gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
  369. while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
  370. blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
  371. error = gfs2_revoke_add(sdp, blkno, start);
  372. if (error < 0)
  373. return error;
  374. else if (error)
  375. sdp->sd_found_revokes++;
  376. if (!--revokes)
  377. break;
  378. offset += sizeof(u64);
  379. }
  380. brelse(bh);
  381. offset = sizeof(struct gfs2_meta_header);
  382. first = 0;
  383. }
  384. return 0;
  385. }
  386. static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  387. {
  388. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  389. if (error) {
  390. gfs2_revoke_clean(sdp);
  391. return;
  392. }
  393. if (pass != 1)
  394. return;
  395. fs_info(sdp, "jid=%u: Found %u revoke tags\n",
  396. jd->jd_jid, sdp->sd_found_revokes);
  397. gfs2_revoke_clean(sdp);
  398. }
  399. static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  400. {
  401. struct gfs2_rgrpd *rgd;
  402. struct gfs2_trans *tr = current->journal_info;
  403. tr->tr_touched = 1;
  404. rgd = container_of(le, struct gfs2_rgrpd, rd_le);
  405. gfs2_log_lock(sdp);
  406. if (!list_empty(&le->le_list)){
  407. gfs2_log_unlock(sdp);
  408. return;
  409. }
  410. gfs2_rgrp_bh_hold(rgd);
  411. sdp->sd_log_num_rg++;
  412. list_add(&le->le_list, &sdp->sd_log_le_rg);
  413. gfs2_log_unlock(sdp);
  414. }
  415. static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  416. {
  417. struct list_head *head = &sdp->sd_log_le_rg;
  418. struct gfs2_rgrpd *rgd;
  419. while (!list_empty(head)) {
  420. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
  421. list_del_init(&rgd->rd_le.le_list);
  422. sdp->sd_log_num_rg--;
  423. gfs2_rgrp_repolish_clones(rgd);
  424. gfs2_rgrp_bh_put(rgd);
  425. }
  426. gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
  427. }
  428. /**
  429. * databuf_lo_add - Add a databuf to the transaction.
  430. *
  431. * This is used in two distinct cases:
  432. * i) In ordered write mode
  433. * We put the data buffer on a list so that we can ensure that its
  434. * synced to disk at the right time
  435. * ii) In journaled data mode
  436. * We need to journal the data block in the same way as metadata in
  437. * the functions above. The difference is that here we have a tag
  438. * which is two __be64's being the block number (as per meta data)
  439. * and a flag which says whether the data block needs escaping or
  440. * not. This means we need a new log entry for each 251 or so data
  441. * blocks, which isn't an enormous overhead but twice as much as
  442. * for normal metadata blocks.
  443. */
  444. static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  445. {
  446. struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
  447. struct gfs2_trans *tr = current->journal_info;
  448. struct address_space *mapping = bd->bd_bh->b_page->mapping;
  449. struct gfs2_inode *ip = GFS2_I(mapping->host);
  450. lock_buffer(bd->bd_bh);
  451. gfs2_log_lock(sdp);
  452. if (!list_empty(&bd->bd_list_tr))
  453. goto out;
  454. tr->tr_touched = 1;
  455. if (gfs2_is_jdata(ip)) {
  456. tr->tr_num_buf++;
  457. list_add(&bd->bd_list_tr, &tr->tr_list_buf);
  458. }
  459. if (!list_empty(&le->le_list))
  460. goto out;
  461. __glock_lo_add(sdp, &bd->bd_gl->gl_le);
  462. if (gfs2_is_jdata(ip)) {
  463. gfs2_pin(sdp, bd->bd_bh);
  464. tr->tr_num_databuf_new++;
  465. sdp->sd_log_num_jdata++;
  466. }
  467. sdp->sd_log_num_databuf++;
  468. list_add(&le->le_list, &sdp->sd_log_le_databuf);
  469. out:
  470. gfs2_log_unlock(sdp);
  471. unlock_buffer(bd->bd_bh);
  472. }
  473. static int gfs2_check_magic(struct buffer_head *bh)
  474. {
  475. struct page *page = bh->b_page;
  476. void *kaddr;
  477. __be32 *ptr;
  478. int rv = 0;
  479. kaddr = kmap_atomic(page, KM_USER0);
  480. ptr = kaddr + bh_offset(bh);
  481. if (*ptr == cpu_to_be32(GFS2_MAGIC))
  482. rv = 1;
  483. kunmap_atomic(kaddr, KM_USER0);
  484. return rv;
  485. }
  486. /**
  487. * databuf_lo_before_commit - Scan the data buffers, writing as we go
  488. *
  489. * Here we scan through the lists of buffers and make the assumption
  490. * that any buffer thats been pinned is being journaled, and that
  491. * any unpinned buffer is an ordered write data buffer and therefore
  492. * will be written back rather than journaled.
  493. */
  494. static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
  495. {
  496. LIST_HEAD(started);
  497. struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
  498. struct buffer_head *bh = NULL,*bh1 = NULL;
  499. struct gfs2_log_descriptor *ld;
  500. unsigned int limit;
  501. unsigned int total_dbuf;
  502. unsigned int total_jdata;
  503. unsigned int num, n;
  504. __be64 *ptr = NULL;
  505. limit = databuf_limit(sdp);
  506. /*
  507. * Start writing ordered buffers, write journaled buffers
  508. * into the log along with a header
  509. */
  510. gfs2_log_lock(sdp);
  511. total_dbuf = sdp->sd_log_num_databuf;
  512. total_jdata = sdp->sd_log_num_jdata;
  513. bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
  514. bd_le.le_list);
  515. while(total_dbuf) {
  516. num = total_jdata;
  517. if (num > limit)
  518. num = limit;
  519. n = 0;
  520. list_for_each_entry_safe_continue(bd1, bdt,
  521. &sdp->sd_log_le_databuf,
  522. bd_le.le_list) {
  523. /* store off the buffer head in a local ptr since
  524. * gfs2_bufdata might change when we drop the log lock
  525. */
  526. bh1 = bd1->bd_bh;
  527. /* An ordered write buffer */
  528. if (bh1 && !buffer_pinned(bh1)) {
  529. list_move(&bd1->bd_le.le_list, &started);
  530. if (bd1 == bd2) {
  531. bd2 = NULL;
  532. bd2 = list_prepare_entry(bd2,
  533. &sdp->sd_log_le_databuf,
  534. bd_le.le_list);
  535. }
  536. total_dbuf--;
  537. if (bh1) {
  538. if (buffer_dirty(bh1)) {
  539. get_bh(bh1);
  540. gfs2_log_unlock(sdp);
  541. ll_rw_block(SWRITE, 1, &bh1);
  542. brelse(bh1);
  543. gfs2_log_lock(sdp);
  544. }
  545. continue;
  546. }
  547. continue;
  548. } else if (bh1) { /* A journaled buffer */
  549. int magic;
  550. gfs2_log_unlock(sdp);
  551. if (!bh) {
  552. bh = gfs2_log_get_buf(sdp);
  553. ld = (struct gfs2_log_descriptor *)
  554. bh->b_data;
  555. ptr = (__be64 *)(bh->b_data +
  556. DATABUF_OFFSET);
  557. ld->ld_header.mh_magic =
  558. cpu_to_be32(GFS2_MAGIC);
  559. ld->ld_header.mh_type =
  560. cpu_to_be32(GFS2_METATYPE_LD);
  561. ld->ld_header.mh_format =
  562. cpu_to_be32(GFS2_FORMAT_LD);
  563. ld->ld_type =
  564. cpu_to_be32(GFS2_LOG_DESC_JDATA);
  565. ld->ld_length = cpu_to_be32(num + 1);
  566. ld->ld_data1 = cpu_to_be32(num);
  567. ld->ld_data2 = cpu_to_be32(0);
  568. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  569. }
  570. magic = gfs2_check_magic(bh1);
  571. *ptr++ = cpu_to_be64(bh1->b_blocknr);
  572. *ptr++ = cpu_to_be64((__u64)magic);
  573. clear_buffer_escaped(bh1);
  574. if (unlikely(magic != 0))
  575. set_buffer_escaped(bh1);
  576. gfs2_log_lock(sdp);
  577. if (++n >= num)
  578. break;
  579. } else if (!bh1) {
  580. total_dbuf--;
  581. sdp->sd_log_num_databuf--;
  582. list_del_init(&bd1->bd_le.le_list);
  583. if (bd1 == bd2) {
  584. bd2 = NULL;
  585. bd2 = list_prepare_entry(bd2,
  586. &sdp->sd_log_le_databuf,
  587. bd_le.le_list);
  588. }
  589. kmem_cache_free(gfs2_bufdata_cachep, bd1);
  590. }
  591. }
  592. gfs2_log_unlock(sdp);
  593. if (bh) {
  594. set_buffer_dirty(bh);
  595. ll_rw_block(WRITE, 1, &bh);
  596. bh = NULL;
  597. ptr = NULL;
  598. }
  599. n = 0;
  600. gfs2_log_lock(sdp);
  601. list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
  602. bd_le.le_list) {
  603. if (!bd2->bd_bh)
  604. continue;
  605. /* copy buffer if it needs escaping */
  606. gfs2_log_unlock(sdp);
  607. if (unlikely(buffer_escaped(bd2->bd_bh))) {
  608. void *kaddr;
  609. struct page *page = bd2->bd_bh->b_page;
  610. bh = gfs2_log_get_buf(sdp);
  611. kaddr = kmap_atomic(page, KM_USER0);
  612. memcpy(bh->b_data,
  613. kaddr + bh_offset(bd2->bd_bh),
  614. sdp->sd_sb.sb_bsize);
  615. kunmap_atomic(kaddr, KM_USER0);
  616. *(__be32 *)bh->b_data = 0;
  617. } else {
  618. bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
  619. }
  620. set_buffer_dirty(bh);
  621. ll_rw_block(WRITE, 1, &bh);
  622. gfs2_log_lock(sdp);
  623. if (++n >= num)
  624. break;
  625. }
  626. bh = NULL;
  627. BUG_ON(total_dbuf < num);
  628. total_dbuf -= num;
  629. total_jdata -= num;
  630. }
  631. gfs2_log_unlock(sdp);
  632. /* Wait on all ordered buffers */
  633. while (!list_empty(&started)) {
  634. gfs2_log_lock(sdp);
  635. bd1 = list_entry(started.next, struct gfs2_bufdata,
  636. bd_le.le_list);
  637. list_del_init(&bd1->bd_le.le_list);
  638. sdp->sd_log_num_databuf--;
  639. bh = bd1->bd_bh;
  640. if (bh) {
  641. bh->b_private = NULL;
  642. get_bh(bh);
  643. gfs2_log_unlock(sdp);
  644. wait_on_buffer(bh);
  645. brelse(bh);
  646. } else
  647. gfs2_log_unlock(sdp);
  648. kmem_cache_free(gfs2_bufdata_cachep, bd1);
  649. }
  650. /* We've removed all the ordered write bufs here, so only jdata left */
  651. gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
  652. }
  653. static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  654. struct gfs2_log_descriptor *ld,
  655. __be64 *ptr, int pass)
  656. {
  657. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  658. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  659. struct gfs2_glock *gl = ip->i_gl;
  660. unsigned int blks = be32_to_cpu(ld->ld_data1);
  661. struct buffer_head *bh_log, *bh_ip;
  662. u64 blkno;
  663. u64 esc;
  664. int error = 0;
  665. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  666. return 0;
  667. gfs2_replay_incr_blk(sdp, &start);
  668. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  669. blkno = be64_to_cpu(*ptr++);
  670. esc = be64_to_cpu(*ptr++);
  671. sdp->sd_found_blocks++;
  672. if (gfs2_revoke_check(sdp, blkno, start))
  673. continue;
  674. error = gfs2_replay_read_block(jd, start, &bh_log);
  675. if (error)
  676. return error;
  677. bh_ip = gfs2_meta_new(gl, blkno);
  678. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  679. /* Unescape */
  680. if (esc) {
  681. __be32 *eptr = (__be32 *)bh_ip->b_data;
  682. *eptr = cpu_to_be32(GFS2_MAGIC);
  683. }
  684. mark_buffer_dirty(bh_ip);
  685. brelse(bh_log);
  686. brelse(bh_ip);
  687. if (error)
  688. break;
  689. sdp->sd_replayed_blocks++;
  690. }
  691. return error;
  692. }
  693. /* FIXME: sort out accounting for log blocks etc. */
  694. static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  695. {
  696. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  697. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  698. if (error) {
  699. gfs2_meta_sync(ip->i_gl);
  700. return;
  701. }
  702. if (pass != 1)
  703. return;
  704. /* data sync? */
  705. gfs2_meta_sync(ip->i_gl);
  706. fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
  707. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  708. }
  709. static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  710. {
  711. struct list_head *head = &sdp->sd_log_le_databuf;
  712. struct gfs2_bufdata *bd;
  713. while (!list_empty(head)) {
  714. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  715. list_del_init(&bd->bd_le.le_list);
  716. sdp->sd_log_num_databuf--;
  717. sdp->sd_log_num_jdata--;
  718. gfs2_unpin(sdp, bd->bd_bh, ai);
  719. }
  720. gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
  721. gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
  722. }
  723. const struct gfs2_log_operations gfs2_glock_lops = {
  724. .lo_add = glock_lo_add,
  725. .lo_after_commit = glock_lo_after_commit,
  726. .lo_name = "glock",
  727. };
  728. const struct gfs2_log_operations gfs2_buf_lops = {
  729. .lo_add = buf_lo_add,
  730. .lo_incore_commit = buf_lo_incore_commit,
  731. .lo_before_commit = buf_lo_before_commit,
  732. .lo_after_commit = buf_lo_after_commit,
  733. .lo_before_scan = buf_lo_before_scan,
  734. .lo_scan_elements = buf_lo_scan_elements,
  735. .lo_after_scan = buf_lo_after_scan,
  736. .lo_name = "buf",
  737. };
  738. const struct gfs2_log_operations gfs2_revoke_lops = {
  739. .lo_add = revoke_lo_add,
  740. .lo_before_commit = revoke_lo_before_commit,
  741. .lo_before_scan = revoke_lo_before_scan,
  742. .lo_scan_elements = revoke_lo_scan_elements,
  743. .lo_after_scan = revoke_lo_after_scan,
  744. .lo_name = "revoke",
  745. };
  746. const struct gfs2_log_operations gfs2_rg_lops = {
  747. .lo_add = rg_lo_add,
  748. .lo_after_commit = rg_lo_after_commit,
  749. .lo_name = "rg",
  750. };
  751. const struct gfs2_log_operations gfs2_databuf_lops = {
  752. .lo_add = databuf_lo_add,
  753. .lo_incore_commit = buf_lo_incore_commit,
  754. .lo_before_commit = databuf_lo_before_commit,
  755. .lo_after_commit = databuf_lo_after_commit,
  756. .lo_scan_elements = databuf_lo_scan_elements,
  757. .lo_after_scan = databuf_lo_after_scan,
  758. .lo_name = "databuf",
  759. };
  760. const struct gfs2_log_operations *gfs2_log_ops[] = {
  761. &gfs2_glock_lops,
  762. &gfs2_buf_lops,
  763. &gfs2_revoke_lops,
  764. &gfs2_rg_lops,
  765. &gfs2_databuf_lops,
  766. NULL,
  767. };