lops.c 20 KB

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