lops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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_host *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_host *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,*bh1 = 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. /* store off the buffer head in a local ptr since
  446. * gfs2_bufdata might change when we drop the log lock
  447. */
  448. bh1 = bd1->bd_bh;
  449. /* An ordered write buffer */
  450. if (bh1 && !buffer_pinned(bh1)) {
  451. list_move(&bd1->bd_le.le_list, &started);
  452. if (bd1 == bd2) {
  453. bd2 = NULL;
  454. bd2 = list_prepare_entry(bd2,
  455. &sdp->sd_log_le_databuf,
  456. bd_le.le_list);
  457. }
  458. total_dbuf--;
  459. if (bh1) {
  460. if (buffer_dirty(bh1)) {
  461. get_bh(bh1);
  462. gfs2_log_unlock(sdp);
  463. ll_rw_block(SWRITE, 1, &bh1);
  464. brelse(bh1);
  465. gfs2_log_lock(sdp);
  466. }
  467. continue;
  468. }
  469. continue;
  470. } else if (bh1) { /* A journaled buffer */
  471. int magic;
  472. gfs2_log_unlock(sdp);
  473. if (!bh) {
  474. bh = gfs2_log_get_buf(sdp);
  475. sdp->sd_log_num_hdrs++;
  476. ld = (struct gfs2_log_descriptor *)
  477. bh->b_data;
  478. ptr = (__be64 *)(bh->b_data + offset);
  479. ld->ld_header.mh_magic =
  480. cpu_to_be32(GFS2_MAGIC);
  481. ld->ld_header.mh_type =
  482. cpu_to_be32(GFS2_METATYPE_LD);
  483. ld->ld_header.mh_format =
  484. cpu_to_be32(GFS2_FORMAT_LD);
  485. ld->ld_type =
  486. cpu_to_be32(GFS2_LOG_DESC_JDATA);
  487. ld->ld_length = cpu_to_be32(num + 1);
  488. ld->ld_data1 = cpu_to_be32(num);
  489. ld->ld_data2 = cpu_to_be32(0);
  490. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  491. }
  492. magic = gfs2_check_magic(bh1);
  493. *ptr++ = cpu_to_be64(bh1->b_blocknr);
  494. *ptr++ = cpu_to_be64((__u64)magic);
  495. clear_buffer_escaped(bh1);
  496. if (unlikely(magic != 0))
  497. set_buffer_escaped(bh1);
  498. gfs2_log_lock(sdp);
  499. if (n++ > num)
  500. break;
  501. } else if (!bh1) {
  502. total_dbuf--;
  503. sdp->sd_log_num_databuf--;
  504. list_del_init(&bd1->bd_le.le_list);
  505. if (bd1 == bd2) {
  506. bd2 = NULL;
  507. bd2 = list_prepare_entry(bd2,
  508. &sdp->sd_log_le_databuf,
  509. bd_le.le_list);
  510. }
  511. kmem_cache_free(gfs2_bufdata_cachep, bd1);
  512. }
  513. }
  514. gfs2_log_unlock(sdp);
  515. if (bh) {
  516. set_buffer_dirty(bh);
  517. ll_rw_block(WRITE, 1, &bh);
  518. bh = NULL;
  519. }
  520. n = 0;
  521. gfs2_log_lock(sdp);
  522. list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
  523. bd_le.le_list) {
  524. if (!bd2->bd_bh)
  525. continue;
  526. /* copy buffer if it needs escaping */
  527. gfs2_log_unlock(sdp);
  528. if (unlikely(buffer_escaped(bd2->bd_bh))) {
  529. void *kaddr;
  530. struct page *page = bd2->bd_bh->b_page;
  531. bh = gfs2_log_get_buf(sdp);
  532. kaddr = kmap_atomic(page, KM_USER0);
  533. memcpy(bh->b_data,
  534. kaddr + bh_offset(bd2->bd_bh),
  535. sdp->sd_sb.sb_bsize);
  536. kunmap_atomic(kaddr, KM_USER0);
  537. *(__be32 *)bh->b_data = 0;
  538. } else {
  539. bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
  540. }
  541. set_buffer_dirty(bh);
  542. ll_rw_block(WRITE, 1, &bh);
  543. gfs2_log_lock(sdp);
  544. if (++n >= num)
  545. break;
  546. }
  547. bh = NULL;
  548. total_dbuf -= num;
  549. total_jdata -= num;
  550. }
  551. gfs2_log_unlock(sdp);
  552. /* Wait on all ordered buffers */
  553. while (!list_empty(&started)) {
  554. gfs2_log_lock(sdp);
  555. bd1 = list_entry(started.next, struct gfs2_bufdata,
  556. bd_le.le_list);
  557. list_del_init(&bd1->bd_le.le_list);
  558. sdp->sd_log_num_databuf--;
  559. bh = bd1->bd_bh;
  560. if (bh) {
  561. bh->b_private = NULL;
  562. get_bh(bh);
  563. gfs2_log_unlock(sdp);
  564. wait_on_buffer(bh);
  565. brelse(bh);
  566. } else
  567. gfs2_log_unlock(sdp);
  568. kmem_cache_free(gfs2_bufdata_cachep, bd1);
  569. }
  570. /* We've removed all the ordered write bufs here, so only jdata left */
  571. gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
  572. }
  573. static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  574. struct gfs2_log_descriptor *ld,
  575. __be64 *ptr, int pass)
  576. {
  577. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  578. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  579. struct gfs2_glock *gl = ip->i_gl;
  580. unsigned int blks = be32_to_cpu(ld->ld_data1);
  581. struct buffer_head *bh_log, *bh_ip;
  582. u64 blkno;
  583. u64 esc;
  584. int error = 0;
  585. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  586. return 0;
  587. gfs2_replay_incr_blk(sdp, &start);
  588. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  589. blkno = be64_to_cpu(*ptr++);
  590. esc = be64_to_cpu(*ptr++);
  591. sdp->sd_found_blocks++;
  592. if (gfs2_revoke_check(sdp, blkno, start))
  593. continue;
  594. error = gfs2_replay_read_block(jd, start, &bh_log);
  595. if (error)
  596. return error;
  597. bh_ip = gfs2_meta_new(gl, blkno);
  598. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  599. /* Unescape */
  600. if (esc) {
  601. __be32 *eptr = (__be32 *)bh_ip->b_data;
  602. *eptr = cpu_to_be32(GFS2_MAGIC);
  603. }
  604. mark_buffer_dirty(bh_ip);
  605. brelse(bh_log);
  606. brelse(bh_ip);
  607. if (error)
  608. break;
  609. sdp->sd_replayed_blocks++;
  610. }
  611. return error;
  612. }
  613. /* FIXME: sort out accounting for log blocks etc. */
  614. static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  615. {
  616. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  617. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  618. if (error) {
  619. gfs2_meta_sync(ip->i_gl);
  620. return;
  621. }
  622. if (pass != 1)
  623. return;
  624. /* data sync? */
  625. gfs2_meta_sync(ip->i_gl);
  626. fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
  627. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  628. }
  629. static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  630. {
  631. struct list_head *head = &sdp->sd_log_le_databuf;
  632. struct gfs2_bufdata *bd;
  633. while (!list_empty(head)) {
  634. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  635. list_del_init(&bd->bd_le.le_list);
  636. sdp->sd_log_num_databuf--;
  637. sdp->sd_log_num_jdata--;
  638. gfs2_unpin(sdp, bd->bd_bh, ai);
  639. }
  640. gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
  641. gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
  642. }
  643. const struct gfs2_log_operations gfs2_glock_lops = {
  644. .lo_add = glock_lo_add,
  645. .lo_after_commit = glock_lo_after_commit,
  646. .lo_name = "glock",
  647. };
  648. const struct gfs2_log_operations gfs2_buf_lops = {
  649. .lo_add = buf_lo_add,
  650. .lo_incore_commit = buf_lo_incore_commit,
  651. .lo_before_commit = buf_lo_before_commit,
  652. .lo_after_commit = buf_lo_after_commit,
  653. .lo_before_scan = buf_lo_before_scan,
  654. .lo_scan_elements = buf_lo_scan_elements,
  655. .lo_after_scan = buf_lo_after_scan,
  656. .lo_name = "buf",
  657. };
  658. const struct gfs2_log_operations gfs2_revoke_lops = {
  659. .lo_add = revoke_lo_add,
  660. .lo_before_commit = revoke_lo_before_commit,
  661. .lo_before_scan = revoke_lo_before_scan,
  662. .lo_scan_elements = revoke_lo_scan_elements,
  663. .lo_after_scan = revoke_lo_after_scan,
  664. .lo_name = "revoke",
  665. };
  666. const struct gfs2_log_operations gfs2_rg_lops = {
  667. .lo_add = rg_lo_add,
  668. .lo_after_commit = rg_lo_after_commit,
  669. .lo_name = "rg",
  670. };
  671. const struct gfs2_log_operations gfs2_databuf_lops = {
  672. .lo_add = databuf_lo_add,
  673. .lo_incore_commit = buf_lo_incore_commit,
  674. .lo_before_commit = databuf_lo_before_commit,
  675. .lo_after_commit = databuf_lo_after_commit,
  676. .lo_scan_elements = databuf_lo_scan_elements,
  677. .lo_after_scan = databuf_lo_after_scan,
  678. .lo_name = "databuf",
  679. };
  680. const struct gfs2_log_operations *gfs2_log_ops[] = {
  681. &gfs2_glock_lops,
  682. &gfs2_buf_lops,
  683. &gfs2_revoke_lops,
  684. &gfs2_rg_lops,
  685. &gfs2_databuf_lops,
  686. NULL,
  687. };