lops.c 20 KB

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