lops.c 21 KB

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