lops.c 21 KB

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