lops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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 "gfs2.h"
  16. #include "lm_interface.h"
  17. #include "incore.h"
  18. #include "glock.h"
  19. #include "log.h"
  20. #include "lops.h"
  21. #include "meta_io.h"
  22. #include "recovery.h"
  23. #include "rgrp.h"
  24. #include "trans.h"
  25. #include "util.h"
  26. static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  27. {
  28. struct gfs2_glock *gl;
  29. struct gfs2_trans *tr = current->journal_info;
  30. tr->tr_touched = 1;
  31. if (!list_empty(&le->le_list))
  32. return;
  33. gl = container_of(le, struct gfs2_glock, gl_le);
  34. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
  35. return;
  36. gfs2_glock_hold(gl);
  37. set_bit(GLF_DIRTY, &gl->gl_flags);
  38. gfs2_log_lock(sdp);
  39. sdp->sd_log_num_gl++;
  40. list_add(&le->le_list, &sdp->sd_log_le_gl);
  41. gfs2_log_unlock(sdp);
  42. }
  43. static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  44. {
  45. struct list_head *head = &sdp->sd_log_le_gl;
  46. struct gfs2_glock *gl;
  47. while (!list_empty(head)) {
  48. gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list);
  49. list_del_init(&gl->gl_le.le_list);
  50. sdp->sd_log_num_gl--;
  51. gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
  52. gfs2_glock_put(gl);
  53. }
  54. gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
  55. }
  56. static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
  57. {
  58. struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
  59. struct gfs2_trans *tr;
  60. if (!list_empty(&bd->bd_list_tr))
  61. return;
  62. tr = current->journal_info;
  63. tr->tr_touched = 1;
  64. tr->tr_num_buf++;
  65. list_add(&bd->bd_list_tr, &tr->tr_list_buf);
  66. if (!list_empty(&le->le_list))
  67. return;
  68. gfs2_trans_add_gl(bd->bd_gl);
  69. gfs2_meta_check(sdp, bd->bd_bh);
  70. gfs2_pin(sdp, bd->bd_bh);
  71. gfs2_log_lock(sdp);
  72. sdp->sd_log_num_buf++;
  73. list_add(&le->le_list, &sdp->sd_log_le_buf);
  74. gfs2_log_unlock(sdp);
  75. tr->tr_num_buf_new++;
  76. }
  77. static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  78. {
  79. struct list_head *head = &tr->tr_list_buf;
  80. struct gfs2_bufdata *bd;
  81. while (!list_empty(head)) {
  82. bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
  83. list_del_init(&bd->bd_list_tr);
  84. tr->tr_num_buf--;
  85. }
  86. gfs2_assert_warn(sdp, !tr->tr_num_buf);
  87. }
  88. static void buf_lo_before_commit(struct gfs2_sbd *sdp)
  89. {
  90. struct buffer_head *bh;
  91. struct gfs2_log_descriptor *ld;
  92. struct gfs2_bufdata *bd1 = NULL, *bd2;
  93. unsigned int total = sdp->sd_log_num_buf;
  94. unsigned int offset = sizeof(struct gfs2_log_descriptor);
  95. unsigned int limit;
  96. unsigned int num;
  97. unsigned n;
  98. __be64 *ptr;
  99. offset += (sizeof(__be64) - 1);
  100. offset &= ~(sizeof(__be64) - 1);
  101. limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
  102. /* for 4k blocks, limit = 503 */
  103. bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
  104. while(total) {
  105. num = total;
  106. if (total > limit)
  107. num = limit;
  108. bh = gfs2_log_get_buf(sdp);
  109. sdp->sd_log_num_hdrs++;
  110. ld = (struct gfs2_log_descriptor *)bh->b_data;
  111. ptr = (__be64 *)(bh->b_data + offset);
  112. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  113. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  114. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  115. ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
  116. ld->ld_length = cpu_to_be32(num + 1);
  117. ld->ld_data1 = cpu_to_be32(num);
  118. ld->ld_data2 = cpu_to_be32(0);
  119. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  120. n = 0;
  121. list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
  122. bd_le.le_list) {
  123. *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
  124. if (++n >= num)
  125. break;
  126. }
  127. set_buffer_dirty(bh);
  128. ll_rw_block(WRITE, 1, &bh);
  129. n = 0;
  130. list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
  131. bd_le.le_list) {
  132. bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
  133. set_buffer_dirty(bh);
  134. ll_rw_block(WRITE, 1, &bh);
  135. if (++n >= num)
  136. break;
  137. }
  138. total -= num;
  139. }
  140. }
  141. static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  142. {
  143. struct list_head *head = &sdp->sd_log_le_buf;
  144. struct gfs2_bufdata *bd;
  145. while (!list_empty(head)) {
  146. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  147. list_del_init(&bd->bd_le.le_list);
  148. sdp->sd_log_num_buf--;
  149. gfs2_unpin(sdp, bd->bd_bh, ai);
  150. }
  151. gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
  152. }
  153. static void buf_lo_before_scan(struct gfs2_jdesc *jd,
  154. struct gfs2_log_header *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_be32(GFS2_METATYPE_LD);
  239. ld->ld_header.mh_format = cpu_to_be32(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_be32(GFS2_METATYPE_LB);
  258. mh->mh_format = cpu_to_be32(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. tr->tr_num_buf_new++;
  389. }
  390. gfs2_log_lock(sdp);
  391. if (!list_empty(&le->le_list)) {
  392. if (ip->i_di.di_flags & GFS2_DIF_JDATA)
  393. sdp->sd_log_num_jdata++;
  394. sdp->sd_log_num_databuf++;
  395. list_add(&le->le_list, &sdp->sd_log_le_databuf);
  396. }
  397. gfs2_log_unlock(sdp);
  398. }
  399. static int gfs2_check_magic(struct buffer_head *bh)
  400. {
  401. struct page *page = bh->b_page;
  402. void *kaddr;
  403. __be32 *ptr;
  404. int rv = 0;
  405. kaddr = kmap_atomic(page, KM_USER0);
  406. ptr = kaddr + bh_offset(bh);
  407. if (*ptr == cpu_to_be32(GFS2_MAGIC))
  408. rv = 1;
  409. kunmap_atomic(page, KM_USER0);
  410. return rv;
  411. }
  412. /**
  413. * databuf_lo_before_commit - Scan the data buffers, writing as we go
  414. *
  415. * Here we scan through the lists of buffers and make the assumption
  416. * that any buffer thats been pinned is being journaled, and that
  417. * any unpinned buffer is an ordered write data buffer and therefore
  418. * will be written back rather than journaled.
  419. */
  420. static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
  421. {
  422. LIST_HEAD(started);
  423. struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
  424. struct buffer_head *bh = NULL;
  425. unsigned int offset = sizeof(struct gfs2_log_descriptor);
  426. struct gfs2_log_descriptor *ld;
  427. unsigned int limit;
  428. unsigned int total_dbuf = sdp->sd_log_num_databuf;
  429. unsigned int total_jdata = sdp->sd_log_num_jdata;
  430. unsigned int num, n;
  431. __be64 *ptr = NULL;
  432. offset += (2*sizeof(__be64) - 1);
  433. offset &= ~(2*sizeof(__be64) - 1);
  434. limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
  435. /*
  436. * Start writing ordered buffers, write journaled buffers
  437. * into the log along with a header
  438. */
  439. gfs2_log_lock(sdp);
  440. bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
  441. bd_le.le_list);
  442. while(total_dbuf) {
  443. num = total_jdata;
  444. if (num > limit)
  445. num = limit;
  446. n = 0;
  447. list_for_each_entry_safe_continue(bd1, bdt,
  448. &sdp->sd_log_le_databuf,
  449. bd_le.le_list) {
  450. /* An ordered write buffer */
  451. if (bd1->bd_bh && !buffer_pinned(bd1->bd_bh)) {
  452. list_move(&bd1->bd_le.le_list, &started);
  453. if (bd1 == bd2) {
  454. bd2 = NULL;
  455. bd2 = list_prepare_entry(bd2,
  456. &sdp->sd_log_le_databuf,
  457. bd_le.le_list);
  458. }
  459. total_dbuf--;
  460. if (bd1->bd_bh) {
  461. get_bh(bd1->bd_bh);
  462. if (buffer_dirty(bd1->bd_bh)) {
  463. gfs2_log_unlock(sdp);
  464. wait_on_buffer(bd1->bd_bh);
  465. ll_rw_block(WRITE, 1,
  466. &bd1->bd_bh);
  467. gfs2_log_lock(sdp);
  468. }
  469. brelse(bd1->bd_bh);
  470. continue;
  471. }
  472. continue;
  473. } else if (bd1->bd_bh) { /* A journaled buffer */
  474. int magic;
  475. gfs2_log_unlock(sdp);
  476. if (!bh) {
  477. bh = gfs2_log_get_buf(sdp);
  478. sdp->sd_log_num_hdrs++;
  479. ld = (struct gfs2_log_descriptor *)
  480. bh->b_data;
  481. ptr = (__be64 *)(bh->b_data + offset);
  482. ld->ld_header.mh_magic =
  483. cpu_to_be32(GFS2_MAGIC);
  484. ld->ld_header.mh_type =
  485. cpu_to_be32(GFS2_METATYPE_LD);
  486. ld->ld_header.mh_format =
  487. cpu_to_be32(GFS2_FORMAT_LD);
  488. ld->ld_type =
  489. cpu_to_be32(GFS2_LOG_DESC_JDATA);
  490. ld->ld_length = cpu_to_be32(num + 1);
  491. ld->ld_data1 = cpu_to_be32(num);
  492. ld->ld_data2 = cpu_to_be32(0);
  493. memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
  494. }
  495. magic = gfs2_check_magic(bd1->bd_bh);
  496. *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
  497. *ptr++ = cpu_to_be64((__u64)magic);
  498. clear_buffer_escaped(bd1->bd_bh);
  499. if (unlikely(magic != 0))
  500. set_buffer_escaped(bd1->bd_bh);
  501. gfs2_log_lock(sdp);
  502. if (n++ > num)
  503. break;
  504. }
  505. }
  506. gfs2_log_unlock(sdp);
  507. if (bh) {
  508. set_buffer_dirty(bh);
  509. ll_rw_block(WRITE, 1, &bh);
  510. bh = NULL;
  511. }
  512. n = 0;
  513. gfs2_log_lock(sdp);
  514. list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
  515. bd_le.le_list) {
  516. if (!bd2->bd_bh)
  517. continue;
  518. /* copy buffer if it needs escaping */
  519. gfs2_log_unlock(sdp);
  520. if (unlikely(buffer_escaped(bd2->bd_bh))) {
  521. void *kaddr;
  522. struct page *page = bd2->bd_bh->b_page;
  523. bh = gfs2_log_get_buf(sdp);
  524. kaddr = kmap_atomic(page, KM_USER0);
  525. memcpy(bh->b_data,
  526. kaddr + bh_offset(bd2->bd_bh),
  527. sdp->sd_sb.sb_bsize);
  528. kunmap_atomic(page, KM_USER0);
  529. *(__be32 *)bh->b_data = 0;
  530. } else {
  531. bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
  532. }
  533. set_buffer_dirty(bh);
  534. ll_rw_block(WRITE, 1, &bh);
  535. gfs2_log_lock(sdp);
  536. if (++n >= num)
  537. break;
  538. }
  539. bh = NULL;
  540. total_dbuf -= num;
  541. total_jdata -= num;
  542. }
  543. gfs2_log_unlock(sdp);
  544. /* Wait on all ordered buffers */
  545. while (!list_empty(&started)) {
  546. gfs2_log_lock(sdp);
  547. bd1 = list_entry(started.next, struct gfs2_bufdata,
  548. bd_le.le_list);
  549. list_del(&bd1->bd_le.le_list);
  550. sdp->sd_log_num_databuf--;
  551. bh = bd1->bd_bh;
  552. if (bh) {
  553. bh->b_private = NULL;
  554. gfs2_log_unlock(sdp);
  555. wait_on_buffer(bh);
  556. brelse(bh);
  557. } else
  558. gfs2_log_unlock(sdp);
  559. kfree(bd1);
  560. }
  561. /* We've removed all the ordered write bufs here, so only jdata left */
  562. gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
  563. }
  564. static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  565. struct gfs2_log_descriptor *ld,
  566. __be64 *ptr, int pass)
  567. {
  568. struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
  569. struct gfs2_sbd *sdp = ip->i_sbd;
  570. struct gfs2_glock *gl = ip->i_gl;
  571. unsigned int blks = be32_to_cpu(ld->ld_data1);
  572. struct buffer_head *bh_log, *bh_ip;
  573. uint64_t blkno;
  574. uint64_t esc;
  575. int error = 0;
  576. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  577. return 0;
  578. gfs2_replay_incr_blk(sdp, &start);
  579. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  580. blkno = be64_to_cpu(*ptr++);
  581. esc = be64_to_cpu(*ptr++);
  582. sdp->sd_found_blocks++;
  583. if (gfs2_revoke_check(sdp, blkno, start))
  584. continue;
  585. error = gfs2_replay_read_block(jd, start, &bh_log);
  586. if (error)
  587. return error;
  588. bh_ip = gfs2_meta_new(gl, blkno);
  589. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  590. /* Unescape */
  591. if (esc) {
  592. __be32 *eptr = (__be32 *)bh_ip->b_data;
  593. *eptr = cpu_to_be32(GFS2_MAGIC);
  594. }
  595. mark_buffer_dirty(bh_ip);
  596. brelse(bh_log);
  597. brelse(bh_ip);
  598. if (error)
  599. break;
  600. sdp->sd_replayed_blocks++;
  601. }
  602. return error;
  603. }
  604. /* FIXME: sort out accounting for log blocks etc. */
  605. static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  606. {
  607. struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
  608. struct gfs2_sbd *sdp = ip->i_sbd;
  609. if (error) {
  610. gfs2_meta_sync(ip->i_gl,
  611. DIO_START | DIO_WAIT);
  612. return;
  613. }
  614. if (pass != 1)
  615. return;
  616. /* data sync? */
  617. gfs2_meta_sync(ip->i_gl, DIO_START | DIO_WAIT);
  618. fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
  619. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  620. }
  621. static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  622. {
  623. struct list_head *head = &sdp->sd_log_le_databuf;
  624. struct gfs2_bufdata *bd;
  625. while (!list_empty(head)) {
  626. bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
  627. list_del(&bd->bd_le.le_list);
  628. sdp->sd_log_num_databuf--;
  629. sdp->sd_log_num_jdata--;
  630. gfs2_unpin(sdp, bd->bd_bh, ai);
  631. }
  632. gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
  633. gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
  634. }
  635. const struct gfs2_log_operations gfs2_glock_lops = {
  636. .lo_add = glock_lo_add,
  637. .lo_after_commit = glock_lo_after_commit,
  638. .lo_name = "glock"
  639. };
  640. const struct gfs2_log_operations gfs2_buf_lops = {
  641. .lo_add = buf_lo_add,
  642. .lo_incore_commit = buf_lo_incore_commit,
  643. .lo_before_commit = buf_lo_before_commit,
  644. .lo_after_commit = buf_lo_after_commit,
  645. .lo_before_scan = buf_lo_before_scan,
  646. .lo_scan_elements = buf_lo_scan_elements,
  647. .lo_after_scan = buf_lo_after_scan,
  648. .lo_name = "buf"
  649. };
  650. const struct gfs2_log_operations gfs2_revoke_lops = {
  651. .lo_add = revoke_lo_add,
  652. .lo_before_commit = revoke_lo_before_commit,
  653. .lo_before_scan = revoke_lo_before_scan,
  654. .lo_scan_elements = revoke_lo_scan_elements,
  655. .lo_after_scan = revoke_lo_after_scan,
  656. .lo_name = "revoke"
  657. };
  658. const struct gfs2_log_operations gfs2_rg_lops = {
  659. .lo_add = rg_lo_add,
  660. .lo_after_commit = rg_lo_after_commit,
  661. .lo_name = "rg"
  662. };
  663. const struct gfs2_log_operations gfs2_databuf_lops = {
  664. .lo_add = databuf_lo_add,
  665. .lo_incore_commit = buf_lo_incore_commit,
  666. .lo_before_commit = databuf_lo_before_commit,
  667. .lo_after_commit = databuf_lo_after_commit,
  668. .lo_scan_elements = databuf_lo_scan_elements,
  669. .lo_after_scan = databuf_lo_after_scan,
  670. .lo_name = "databuf"
  671. };
  672. const struct gfs2_log_operations *gfs2_log_ops[] = {
  673. &gfs2_glock_lops,
  674. &gfs2_buf_lops,
  675. &gfs2_revoke_lops,
  676. &gfs2_rg_lops,
  677. &gfs2_databuf_lops,
  678. NULL
  679. };