lops.c 20 KB

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