lops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/lm_interface.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "inode.h"
  19. #include "glock.h"
  20. #include "log.h"
  21. #include "lops.h"
  22. #include "meta_io.h"
  23. #include "recovery.h"
  24. #include "rgrp.h"
  25. #include "trans.h"
  26. #include "util.h"
  27. static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  28. {
  29. struct gfs2_glock *gl;
  30. struct gfs2_trans *tr = current->journal_info;
  31. tr->tr_touched = 1;
  32. gl = container_of(le, struct gfs2_glock, gl_le);
  33. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
  34. return;
  35. gfs2_log_lock(sdp);
  36. if (!list_empty(&le->le_list)){
  37. gfs2_log_unlock(sdp);
  38. return;
  39. }
  40. gfs2_glock_hold(gl);
  41. set_bit(GLF_DIRTY, &gl->gl_flags);
  42. sdp->sd_log_num_gl++;
  43. list_add(&le->le_list, &sdp->sd_log_le_gl);
  44. gfs2_log_unlock(sdp);
  45. }
  46. static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  47. {
  48. struct list_head *head = &sdp->sd_log_le_gl;
  49. struct gfs2_glock *gl;
  50. while (!list_empty(head)) {
  51. gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list);
  52. list_del_init(&gl->gl_le.le_list);
  53. sdp->sd_log_num_gl--;
  54. gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
  55. gfs2_glock_put(gl);
  56. }
  57. gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
  58. }
  59. static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  60. {
  61. struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
  62. struct gfs2_trans *tr;
  63. gfs2_log_lock(sdp);
  64. if (!list_empty(&bd->bd_list_tr)) {
  65. gfs2_log_unlock(sdp);
  66. return;
  67. }
  68. tr = current->journal_info;
  69. tr->tr_touched = 1;
  70. tr->tr_num_buf++;
  71. list_add(&bd->bd_list_tr, &tr->tr_list_buf);
  72. gfs2_log_unlock(sdp);
  73. if (!list_empty(&le->le_list))
  74. return;
  75. gfs2_trans_add_gl(bd->bd_gl);
  76. gfs2_meta_check(sdp, bd->bd_bh);
  77. gfs2_pin(sdp, bd->bd_bh);
  78. gfs2_log_lock(sdp);
  79. sdp->sd_log_num_buf++;
  80. list_add(&le->le_list, &sdp->sd_log_le_buf);
  81. gfs2_log_unlock(sdp);
  82. tr->tr_num_buf_new++;
  83. }
  84. static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  85. {
  86. struct list_head *head = &tr->tr_list_buf;
  87. struct gfs2_bufdata *bd;
  88. gfs2_log_lock(sdp);
  89. while (!list_empty(head)) {
  90. bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
  91. list_del_init(&bd->bd_list_tr);
  92. tr->tr_num_buf--;
  93. }
  94. gfs2_log_unlock(sdp);
  95. gfs2_assert_warn(sdp, !tr->tr_num_buf);
  96. }
  97. static void buf_lo_before_commit(struct gfs2_sbd *sdp)
  98. {
  99. struct buffer_head *bh;
  100. struct gfs2_log_descriptor *ld;
  101. struct gfs2_bufdata *bd1 = NULL, *bd2;
  102. unsigned int total = sdp->sd_log_num_buf;
  103. unsigned int offset = BUF_OFFSET;
  104. unsigned int limit;
  105. unsigned int num;
  106. unsigned n;
  107. __be64 *ptr;
  108. limit = buf_limit(sdp);
  109. /* for 4k blocks, limit = 503 */
  110. bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
  111. while(total) {
  112. num = total;
  113. if (total > limit)
  114. num = limit;
  115. bh = gfs2_log_get_buf(sdp);
  116. ld = (struct gfs2_log_descriptor *)bh->b_data;
  117. ptr = (__be64 *)(bh->b_data + offset);
  118. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  119. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  120. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  121. ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
  122. ld->ld_length = cpu_to_be32(num + 1);
  123. ld->ld_data1 = cpu_to_be32(num);
  124. ld->ld_data2 = cpu_to_be32(0);
  125. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  126. n = 0;
  127. list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
  128. bd_le.le_list) {
  129. *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
  130. if (++n >= num)
  131. break;
  132. }
  133. set_buffer_dirty(bh);
  134. ll_rw_block(WRITE, 1, &bh);
  135. n = 0;
  136. list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
  137. bd_le.le_list) {
  138. bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
  139. set_buffer_dirty(bh);
  140. ll_rw_block(WRITE, 1, &bh);
  141. if (++n >= num)
  142. break;
  143. }
  144. total -= num;
  145. }
  146. }
  147. static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  148. {
  149. struct list_head *head = &sdp->sd_log_le_buf;
  150. struct gfs2_bufdata *bd;
  151. while (!list_empty(head)) {
  152. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  153. list_del_init(&bd->bd_le.le_list);
  154. sdp->sd_log_num_buf--;
  155. gfs2_unpin(sdp, bd->bd_bh, ai);
  156. }
  157. gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
  158. }
  159. static void buf_lo_before_scan(struct gfs2_jdesc *jd,
  160. struct gfs2_log_header_host *head, int pass)
  161. {
  162. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  163. if (pass != 0)
  164. return;
  165. sdp->sd_found_blocks = 0;
  166. sdp->sd_replayed_blocks = 0;
  167. }
  168. static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  169. struct gfs2_log_descriptor *ld, __be64 *ptr,
  170. int pass)
  171. {
  172. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  173. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  174. struct gfs2_glock *gl = ip->i_gl;
  175. unsigned int blks = be32_to_cpu(ld->ld_data1);
  176. struct buffer_head *bh_log, *bh_ip;
  177. u64 blkno;
  178. int error = 0;
  179. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
  180. return 0;
  181. gfs2_replay_incr_blk(sdp, &start);
  182. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  183. blkno = be64_to_cpu(*ptr++);
  184. sdp->sd_found_blocks++;
  185. if (gfs2_revoke_check(sdp, blkno, start))
  186. continue;
  187. error = gfs2_replay_read_block(jd, start, &bh_log);
  188. if (error)
  189. return error;
  190. bh_ip = gfs2_meta_new(gl, blkno);
  191. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  192. if (gfs2_meta_check(sdp, bh_ip))
  193. error = -EIO;
  194. else
  195. mark_buffer_dirty(bh_ip);
  196. brelse(bh_log);
  197. brelse(bh_ip);
  198. if (error)
  199. break;
  200. sdp->sd_replayed_blocks++;
  201. }
  202. return error;
  203. }
  204. static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  205. {
  206. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  207. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  208. if (error) {
  209. gfs2_meta_sync(ip->i_gl);
  210. return;
  211. }
  212. if (pass != 1)
  213. return;
  214. gfs2_meta_sync(ip->i_gl);
  215. fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
  216. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  217. }
  218. static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  219. {
  220. struct gfs2_trans *tr;
  221. tr = current->journal_info;
  222. tr->tr_touched = 1;
  223. tr->tr_num_revoke++;
  224. gfs2_log_lock(sdp);
  225. sdp->sd_log_num_revoke++;
  226. list_add(&le->le_list, &sdp->sd_log_le_revoke);
  227. gfs2_log_unlock(sdp);
  228. }
  229. static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
  230. {
  231. struct gfs2_log_descriptor *ld;
  232. struct gfs2_meta_header *mh;
  233. struct buffer_head *bh;
  234. unsigned int offset;
  235. struct list_head *head = &sdp->sd_log_le_revoke;
  236. struct gfs2_revoke *rv;
  237. if (!sdp->sd_log_num_revoke)
  238. return;
  239. bh = gfs2_log_get_buf(sdp);
  240. ld = (struct gfs2_log_descriptor *)bh->b_data;
  241. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  242. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  243. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  244. ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
  245. ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
  246. sizeof(u64)));
  247. ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
  248. ld->ld_data2 = cpu_to_be32(0);
  249. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  250. offset = sizeof(struct gfs2_log_descriptor);
  251. while (!list_empty(head)) {
  252. rv = list_entry(head->next, struct gfs2_revoke, rv_le.le_list);
  253. list_del_init(&rv->rv_le.le_list);
  254. sdp->sd_log_num_revoke--;
  255. if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
  256. set_buffer_dirty(bh);
  257. ll_rw_block(WRITE, 1, &bh);
  258. bh = gfs2_log_get_buf(sdp);
  259. mh = (struct gfs2_meta_header *)bh->b_data;
  260. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  261. mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
  262. mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
  263. offset = sizeof(struct gfs2_meta_header);
  264. }
  265. *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno);
  266. kfree(rv);
  267. offset += sizeof(u64);
  268. }
  269. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  270. set_buffer_dirty(bh);
  271. ll_rw_block(WRITE, 1, &bh);
  272. }
  273. static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
  274. struct gfs2_log_header_host *head, int pass)
  275. {
  276. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  277. if (pass != 0)
  278. return;
  279. sdp->sd_found_revokes = 0;
  280. sdp->sd_replay_tail = head->lh_tail;
  281. }
  282. static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  283. struct gfs2_log_descriptor *ld, __be64 *ptr,
  284. int pass)
  285. {
  286. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  287. unsigned int blks = be32_to_cpu(ld->ld_length);
  288. unsigned int revokes = be32_to_cpu(ld->ld_data1);
  289. struct buffer_head *bh;
  290. unsigned int offset;
  291. u64 blkno;
  292. int first = 1;
  293. int error;
  294. if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
  295. return 0;
  296. offset = sizeof(struct gfs2_log_descriptor);
  297. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  298. error = gfs2_replay_read_block(jd, start, &bh);
  299. if (error)
  300. return error;
  301. if (!first)
  302. gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
  303. while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
  304. blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
  305. error = gfs2_revoke_add(sdp, blkno, start);
  306. if (error < 0)
  307. return error;
  308. else if (error)
  309. sdp->sd_found_revokes++;
  310. if (!--revokes)
  311. break;
  312. offset += sizeof(u64);
  313. }
  314. brelse(bh);
  315. offset = sizeof(struct gfs2_meta_header);
  316. first = 0;
  317. }
  318. return 0;
  319. }
  320. static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  321. {
  322. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  323. if (error) {
  324. gfs2_revoke_clean(sdp);
  325. return;
  326. }
  327. if (pass != 1)
  328. return;
  329. fs_info(sdp, "jid=%u: Found %u revoke tags\n",
  330. jd->jd_jid, sdp->sd_found_revokes);
  331. gfs2_revoke_clean(sdp);
  332. }
  333. static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  334. {
  335. struct gfs2_rgrpd *rgd;
  336. struct gfs2_trans *tr = current->journal_info;
  337. tr->tr_touched = 1;
  338. rgd = container_of(le, struct gfs2_rgrpd, rd_le);
  339. gfs2_log_lock(sdp);
  340. if (!list_empty(&le->le_list)){
  341. gfs2_log_unlock(sdp);
  342. return;
  343. }
  344. gfs2_rgrp_bh_hold(rgd);
  345. sdp->sd_log_num_rg++;
  346. list_add(&le->le_list, &sdp->sd_log_le_rg);
  347. gfs2_log_unlock(sdp);
  348. }
  349. static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  350. {
  351. struct list_head *head = &sdp->sd_log_le_rg;
  352. struct gfs2_rgrpd *rgd;
  353. while (!list_empty(head)) {
  354. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
  355. list_del_init(&rgd->rd_le.le_list);
  356. sdp->sd_log_num_rg--;
  357. gfs2_rgrp_repolish_clones(rgd);
  358. gfs2_rgrp_bh_put(rgd);
  359. }
  360. gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
  361. }
  362. /**
  363. * databuf_lo_add - Add a databuf to the transaction.
  364. *
  365. * This is used in two distinct cases:
  366. * i) In ordered write mode
  367. * We put the data buffer on a list so that we can ensure that its
  368. * synced to disk at the right time
  369. * ii) In journaled data mode
  370. * We need to journal the data block in the same way as metadata in
  371. * the functions above. The difference is that here we have a tag
  372. * which is two __be64's being the block number (as per meta data)
  373. * and a flag which says whether the data block needs escaping or
  374. * not. This means we need a new log entry for each 251 or so data
  375. * blocks, which isn't an enormous overhead but twice as much as
  376. * for normal metadata blocks.
  377. */
  378. static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  379. {
  380. struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
  381. struct gfs2_trans *tr = current->journal_info;
  382. struct address_space *mapping = bd->bd_bh->b_page->mapping;
  383. struct gfs2_inode *ip = GFS2_I(mapping->host);
  384. gfs2_log_lock(sdp);
  385. if (!list_empty(&bd->bd_list_tr)) {
  386. gfs2_log_unlock(sdp);
  387. return;
  388. }
  389. tr->tr_touched = 1;
  390. if (gfs2_is_jdata(ip)) {
  391. tr->tr_num_buf++;
  392. list_add(&bd->bd_list_tr, &tr->tr_list_buf);
  393. }
  394. gfs2_log_unlock(sdp);
  395. if (!list_empty(&le->le_list))
  396. return;
  397. gfs2_trans_add_gl(bd->bd_gl);
  398. if (gfs2_is_jdata(ip)) {
  399. sdp->sd_log_num_jdata++;
  400. gfs2_pin(sdp, bd->bd_bh);
  401. tr->tr_num_databuf_new++;
  402. }
  403. gfs2_log_lock(sdp);
  404. sdp->sd_log_num_databuf++;
  405. list_add(&le->le_list, &sdp->sd_log_le_databuf);
  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. struct gfs2_log_descriptor *ld;
  435. unsigned int limit;
  436. unsigned int total_dbuf;
  437. unsigned int total_jdata = sdp->sd_log_num_jdata;
  438. unsigned int num, n;
  439. __be64 *ptr = NULL;
  440. limit = databuf_limit(sdp);
  441. /*
  442. * Start writing ordered buffers, write journaled buffers
  443. * into the log along with a header
  444. */
  445. gfs2_log_lock(sdp);
  446. total_dbuf = sdp->sd_log_num_databuf;
  447. bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
  448. bd_le.le_list);
  449. while(total_dbuf) {
  450. num = total_jdata;
  451. if (num > limit)
  452. num = limit;
  453. n = 0;
  454. list_for_each_entry_safe_continue(bd1, bdt,
  455. &sdp->sd_log_le_databuf,
  456. bd_le.le_list) {
  457. /* store off the buffer head in a local ptr since
  458. * gfs2_bufdata might change when we drop the log lock
  459. */
  460. bh1 = bd1->bd_bh;
  461. /* An ordered write buffer */
  462. if (bh1 && !buffer_pinned(bh1)) {
  463. list_move(&bd1->bd_le.le_list, &started);
  464. if (bd1 == bd2) {
  465. bd2 = NULL;
  466. bd2 = list_prepare_entry(bd2,
  467. &sdp->sd_log_le_databuf,
  468. bd_le.le_list);
  469. }
  470. total_dbuf--;
  471. if (bh1) {
  472. if (buffer_dirty(bh1)) {
  473. get_bh(bh1);
  474. gfs2_log_unlock(sdp);
  475. ll_rw_block(SWRITE, 1, &bh1);
  476. brelse(bh1);
  477. gfs2_log_lock(sdp);
  478. }
  479. continue;
  480. }
  481. continue;
  482. } else if (bh1) { /* A journaled buffer */
  483. int magic;
  484. gfs2_log_unlock(sdp);
  485. if (!bh) {
  486. bh = gfs2_log_get_buf(sdp);
  487. ld = (struct gfs2_log_descriptor *)
  488. bh->b_data;
  489. ptr = (__be64 *)(bh->b_data +
  490. DATABUF_OFFSET);
  491. ld->ld_header.mh_magic =
  492. cpu_to_be32(GFS2_MAGIC);
  493. ld->ld_header.mh_type =
  494. cpu_to_be32(GFS2_METATYPE_LD);
  495. ld->ld_header.mh_format =
  496. cpu_to_be32(GFS2_FORMAT_LD);
  497. ld->ld_type =
  498. cpu_to_be32(GFS2_LOG_DESC_JDATA);
  499. ld->ld_length = cpu_to_be32(num + 1);
  500. ld->ld_data1 = cpu_to_be32(num);
  501. ld->ld_data2 = cpu_to_be32(0);
  502. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  503. }
  504. magic = gfs2_check_magic(bh1);
  505. *ptr++ = cpu_to_be64(bh1->b_blocknr);
  506. *ptr++ = cpu_to_be64((__u64)magic);
  507. clear_buffer_escaped(bh1);
  508. if (unlikely(magic != 0))
  509. set_buffer_escaped(bh1);
  510. gfs2_log_lock(sdp);
  511. if (++n >= num)
  512. break;
  513. } else if (!bh1) {
  514. total_dbuf--;
  515. sdp->sd_log_num_databuf--;
  516. list_del_init(&bd1->bd_le.le_list);
  517. if (bd1 == bd2) {
  518. bd2 = NULL;
  519. bd2 = list_prepare_entry(bd2,
  520. &sdp->sd_log_le_databuf,
  521. bd_le.le_list);
  522. }
  523. kmem_cache_free(gfs2_bufdata_cachep, bd1);
  524. }
  525. }
  526. gfs2_log_unlock(sdp);
  527. if (bh) {
  528. set_buffer_mapped(bh);
  529. set_buffer_dirty(bh);
  530. ll_rw_block(WRITE, 1, &bh);
  531. bh = NULL;
  532. }
  533. n = 0;
  534. gfs2_log_lock(sdp);
  535. list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
  536. bd_le.le_list) {
  537. if (!bd2->bd_bh)
  538. continue;
  539. /* copy buffer if it needs escaping */
  540. gfs2_log_unlock(sdp);
  541. if (unlikely(buffer_escaped(bd2->bd_bh))) {
  542. void *kaddr;
  543. struct page *page = bd2->bd_bh->b_page;
  544. bh = gfs2_log_get_buf(sdp);
  545. kaddr = kmap_atomic(page, KM_USER0);
  546. memcpy(bh->b_data,
  547. kaddr + bh_offset(bd2->bd_bh),
  548. sdp->sd_sb.sb_bsize);
  549. kunmap_atomic(kaddr, KM_USER0);
  550. *(__be32 *)bh->b_data = 0;
  551. } else {
  552. bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
  553. }
  554. set_buffer_dirty(bh);
  555. ll_rw_block(WRITE, 1, &bh);
  556. gfs2_log_lock(sdp);
  557. if (++n >= num)
  558. break;
  559. }
  560. bh = NULL;
  561. BUG_ON(total_dbuf < num);
  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. };