lops.c 20 KB

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