lops.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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/mempool.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <linux/bio.h>
  17. #include <linux/fs.h>
  18. #include <linux/list_sort.h>
  19. #include "dir.h"
  20. #include "gfs2.h"
  21. #include "incore.h"
  22. #include "inode.h"
  23. #include "glock.h"
  24. #include "log.h"
  25. #include "lops.h"
  26. #include "meta_io.h"
  27. #include "recovery.h"
  28. #include "rgrp.h"
  29. #include "trans.h"
  30. #include "util.h"
  31. #include "trace_gfs2.h"
  32. /**
  33. * gfs2_pin - Pin a buffer in memory
  34. * @sdp: The superblock
  35. * @bh: The buffer to be pinned
  36. *
  37. * The log lock must be held when calling this function
  38. */
  39. void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
  40. {
  41. struct gfs2_bufdata *bd;
  42. BUG_ON(!current->journal_info);
  43. clear_buffer_dirty(bh);
  44. if (test_set_buffer_pinned(bh))
  45. gfs2_assert_withdraw(sdp, 0);
  46. if (!buffer_uptodate(bh))
  47. gfs2_io_error_bh_wd(sdp, bh);
  48. bd = bh->b_private;
  49. /* If this buffer is in the AIL and it has already been written
  50. * to in-place disk block, remove it from the AIL.
  51. */
  52. spin_lock(&sdp->sd_ail_lock);
  53. if (bd->bd_tr)
  54. list_move(&bd->bd_ail_st_list, &bd->bd_tr->tr_ail2_list);
  55. spin_unlock(&sdp->sd_ail_lock);
  56. get_bh(bh);
  57. atomic_inc(&sdp->sd_log_pinned);
  58. trace_gfs2_pin(bd, 1);
  59. }
  60. static bool buffer_is_rgrp(const struct gfs2_bufdata *bd)
  61. {
  62. return bd->bd_gl->gl_name.ln_type == LM_TYPE_RGRP;
  63. }
  64. static void maybe_release_space(struct gfs2_bufdata *bd)
  65. {
  66. struct gfs2_glock *gl = bd->bd_gl;
  67. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  68. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  69. unsigned int index = bd->bd_bh->b_blocknr - gl->gl_name.ln_number;
  70. struct gfs2_bitmap *bi = rgd->rd_bits + index;
  71. if (bi->bi_clone == NULL)
  72. return;
  73. if (sdp->sd_args.ar_discard)
  74. gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bd->bd_bh, bi, 1, NULL);
  75. memcpy(bi->bi_clone + bi->bi_offset,
  76. bd->bd_bh->b_data + bi->bi_offset, bi->bi_len);
  77. clear_bit(GBF_FULL, &bi->bi_flags);
  78. rgd->rd_free_clone = rgd->rd_free;
  79. rgd->rd_extfail_pt = rgd->rd_free;
  80. }
  81. /**
  82. * gfs2_unpin - Unpin a buffer
  83. * @sdp: the filesystem the buffer belongs to
  84. * @bh: The buffer to unpin
  85. * @ai:
  86. * @flags: The inode dirty flags
  87. *
  88. */
  89. static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  90. struct gfs2_trans *tr)
  91. {
  92. struct gfs2_bufdata *bd = bh->b_private;
  93. BUG_ON(!buffer_uptodate(bh));
  94. BUG_ON(!buffer_pinned(bh));
  95. lock_buffer(bh);
  96. mark_buffer_dirty(bh);
  97. clear_buffer_pinned(bh);
  98. if (buffer_is_rgrp(bd))
  99. maybe_release_space(bd);
  100. spin_lock(&sdp->sd_ail_lock);
  101. if (bd->bd_tr) {
  102. list_del(&bd->bd_ail_st_list);
  103. brelse(bh);
  104. } else {
  105. struct gfs2_glock *gl = bd->bd_gl;
  106. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  107. atomic_inc(&gl->gl_ail_count);
  108. }
  109. bd->bd_tr = tr;
  110. list_add(&bd->bd_ail_st_list, &tr->tr_ail1_list);
  111. spin_unlock(&sdp->sd_ail_lock);
  112. clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  113. trace_gfs2_pin(bd, 0);
  114. unlock_buffer(bh);
  115. atomic_dec(&sdp->sd_log_pinned);
  116. }
  117. static void gfs2_log_incr_head(struct gfs2_sbd *sdp)
  118. {
  119. BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) &&
  120. (sdp->sd_log_flush_head != sdp->sd_log_head));
  121. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks)
  122. sdp->sd_log_flush_head = 0;
  123. }
  124. u64 gfs2_log_bmap(struct gfs2_sbd *sdp)
  125. {
  126. unsigned int lbn = sdp->sd_log_flush_head;
  127. struct gfs2_journal_extent *je;
  128. u64 block;
  129. list_for_each_entry(je, &sdp->sd_jdesc->extent_list, list) {
  130. if ((lbn >= je->lblock) && (lbn < (je->lblock + je->blocks))) {
  131. block = je->dblock + lbn - je->lblock;
  132. gfs2_log_incr_head(sdp);
  133. return block;
  134. }
  135. }
  136. return -1;
  137. }
  138. /**
  139. * gfs2_end_log_write_bh - end log write of pagecache data with buffers
  140. * @sdp: The superblock
  141. * @bvec: The bio_vec
  142. * @error: The i/o status
  143. *
  144. * This finds the relevant buffers and unlocks them and sets the
  145. * error flag according to the status of the i/o request. This is
  146. * used when the log is writing data which has an in-place version
  147. * that is pinned in the pagecache.
  148. */
  149. static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp, struct bio_vec *bvec,
  150. blk_status_t error)
  151. {
  152. struct buffer_head *bh, *next;
  153. struct page *page = bvec->bv_page;
  154. unsigned size;
  155. bh = page_buffers(page);
  156. size = bvec->bv_len;
  157. while (bh_offset(bh) < bvec->bv_offset)
  158. bh = bh->b_this_page;
  159. do {
  160. if (error)
  161. mark_buffer_write_io_error(bh);
  162. unlock_buffer(bh);
  163. next = bh->b_this_page;
  164. size -= bh->b_size;
  165. brelse(bh);
  166. bh = next;
  167. } while(bh && size);
  168. }
  169. /**
  170. * gfs2_end_log_write - end of i/o to the log
  171. * @bio: The bio
  172. * @error: Status of i/o request
  173. *
  174. * Each bio_vec contains either data from the pagecache or data
  175. * relating to the log itself. Here we iterate over the bio_vec
  176. * array, processing both kinds of data.
  177. *
  178. */
  179. static void gfs2_end_log_write(struct bio *bio)
  180. {
  181. struct gfs2_sbd *sdp = bio->bi_private;
  182. struct bio_vec *bvec;
  183. struct page *page;
  184. int i;
  185. if (bio->bi_status) {
  186. fs_err(sdp, "Error %d writing to journal, jid=%u\n",
  187. bio->bi_status, sdp->sd_jdesc->jd_jid);
  188. wake_up(&sdp->sd_logd_waitq);
  189. }
  190. bio_for_each_segment_all(bvec, bio, i) {
  191. page = bvec->bv_page;
  192. if (page_has_buffers(page))
  193. gfs2_end_log_write_bh(sdp, bvec, bio->bi_status);
  194. else
  195. mempool_free(page, gfs2_page_pool);
  196. }
  197. bio_put(bio);
  198. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  199. wake_up(&sdp->sd_log_flush_wait);
  200. }
  201. /**
  202. * gfs2_log_flush_bio - Submit any pending log bio
  203. * @sdp: The superblock
  204. * @op: REQ_OP
  205. * @op_flags: req_flag_bits
  206. *
  207. * Submit any pending part-built or full bio to the block device. If
  208. * there is no pending bio, then this is a no-op.
  209. */
  210. void gfs2_log_flush_bio(struct gfs2_sbd *sdp, int op, int op_flags)
  211. {
  212. if (sdp->sd_log_bio) {
  213. atomic_inc(&sdp->sd_log_in_flight);
  214. bio_set_op_attrs(sdp->sd_log_bio, op, op_flags);
  215. submit_bio(sdp->sd_log_bio);
  216. sdp->sd_log_bio = NULL;
  217. }
  218. }
  219. /**
  220. * gfs2_log_alloc_bio - Allocate a new bio for log writing
  221. * @sdp: The superblock
  222. * @blkno: The next device block number we want to write to
  223. *
  224. * This should never be called when there is a cached bio in the
  225. * super block. When it returns, there will be a cached bio in the
  226. * super block which will have as many bio_vecs as the device is
  227. * happy to handle.
  228. *
  229. * Returns: Newly allocated bio
  230. */
  231. static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno)
  232. {
  233. struct super_block *sb = sdp->sd_vfs;
  234. struct bio *bio;
  235. BUG_ON(sdp->sd_log_bio);
  236. bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
  237. bio->bi_iter.bi_sector = blkno * (sb->s_blocksize >> 9);
  238. bio_set_dev(bio, sb->s_bdev);
  239. bio->bi_end_io = gfs2_end_log_write;
  240. bio->bi_private = sdp;
  241. sdp->sd_log_bio = bio;
  242. return bio;
  243. }
  244. /**
  245. * gfs2_log_get_bio - Get cached log bio, or allocate a new one
  246. * @sdp: The superblock
  247. * @blkno: The device block number we want to write to
  248. *
  249. * If there is a cached bio, then if the next block number is sequential
  250. * with the previous one, return it, otherwise flush the bio to the
  251. * device. If there is not a cached bio, or we just flushed it, then
  252. * allocate a new one.
  253. *
  254. * Returns: The bio to use for log writes
  255. */
  256. static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno)
  257. {
  258. struct bio *bio = sdp->sd_log_bio;
  259. u64 nblk;
  260. if (bio) {
  261. nblk = bio_end_sector(bio);
  262. nblk >>= sdp->sd_fsb2bb_shift;
  263. if (blkno == nblk)
  264. return bio;
  265. gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
  266. }
  267. return gfs2_log_alloc_bio(sdp, blkno);
  268. }
  269. /**
  270. * gfs2_log_write - write to log
  271. * @sdp: the filesystem
  272. * @page: the page to write
  273. * @size: the size of the data to write
  274. * @offset: the offset within the page
  275. * @blkno: block number of the log entry
  276. *
  277. * Try and add the page segment to the current bio. If that fails,
  278. * submit the current bio to the device and create a new one, and
  279. * then add the page segment to that.
  280. */
  281. void gfs2_log_write(struct gfs2_sbd *sdp, struct page *page,
  282. unsigned size, unsigned offset, u64 blkno)
  283. {
  284. struct bio *bio;
  285. int ret;
  286. bio = gfs2_log_get_bio(sdp, blkno);
  287. ret = bio_add_page(bio, page, size, offset);
  288. if (ret == 0) {
  289. gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
  290. bio = gfs2_log_alloc_bio(sdp, blkno);
  291. ret = bio_add_page(bio, page, size, offset);
  292. WARN_ON(ret == 0);
  293. }
  294. }
  295. /**
  296. * gfs2_log_write_bh - write a buffer's content to the log
  297. * @sdp: The super block
  298. * @bh: The buffer pointing to the in-place location
  299. *
  300. * This writes the content of the buffer to the next available location
  301. * in the log. The buffer will be unlocked once the i/o to the log has
  302. * completed.
  303. */
  304. static void gfs2_log_write_bh(struct gfs2_sbd *sdp, struct buffer_head *bh)
  305. {
  306. gfs2_log_write(sdp, bh->b_page, bh->b_size, bh_offset(bh),
  307. gfs2_log_bmap(sdp));
  308. }
  309. /**
  310. * gfs2_log_write_page - write one block stored in a page, into the log
  311. * @sdp: The superblock
  312. * @page: The struct page
  313. *
  314. * This writes the first block-sized part of the page into the log. Note
  315. * that the page must have been allocated from the gfs2_page_pool mempool
  316. * and that after this has been called, ownership has been transferred and
  317. * the page may be freed at any time.
  318. */
  319. void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page)
  320. {
  321. struct super_block *sb = sdp->sd_vfs;
  322. gfs2_log_write(sdp, page, sb->s_blocksize, 0,
  323. gfs2_log_bmap(sdp));
  324. }
  325. static struct page *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type,
  326. u32 ld_length, u32 ld_data1)
  327. {
  328. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  329. struct gfs2_log_descriptor *ld = page_address(page);
  330. clear_page(ld);
  331. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  332. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  333. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  334. ld->ld_type = cpu_to_be32(ld_type);
  335. ld->ld_length = cpu_to_be32(ld_length);
  336. ld->ld_data1 = cpu_to_be32(ld_data1);
  337. ld->ld_data2 = 0;
  338. return page;
  339. }
  340. static void gfs2_check_magic(struct buffer_head *bh)
  341. {
  342. void *kaddr;
  343. __be32 *ptr;
  344. clear_buffer_escaped(bh);
  345. kaddr = kmap_atomic(bh->b_page);
  346. ptr = kaddr + bh_offset(bh);
  347. if (*ptr == cpu_to_be32(GFS2_MAGIC))
  348. set_buffer_escaped(bh);
  349. kunmap_atomic(kaddr);
  350. }
  351. static int blocknr_cmp(void *priv, struct list_head *a, struct list_head *b)
  352. {
  353. struct gfs2_bufdata *bda, *bdb;
  354. bda = list_entry(a, struct gfs2_bufdata, bd_list);
  355. bdb = list_entry(b, struct gfs2_bufdata, bd_list);
  356. if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr)
  357. return -1;
  358. if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr)
  359. return 1;
  360. return 0;
  361. }
  362. static void gfs2_before_commit(struct gfs2_sbd *sdp, unsigned int limit,
  363. unsigned int total, struct list_head *blist,
  364. bool is_databuf)
  365. {
  366. struct gfs2_log_descriptor *ld;
  367. struct gfs2_bufdata *bd1 = NULL, *bd2;
  368. struct page *page;
  369. unsigned int num;
  370. unsigned n;
  371. __be64 *ptr;
  372. gfs2_log_lock(sdp);
  373. list_sort(NULL, blist, blocknr_cmp);
  374. bd1 = bd2 = list_prepare_entry(bd1, blist, bd_list);
  375. while(total) {
  376. num = total;
  377. if (total > limit)
  378. num = limit;
  379. gfs2_log_unlock(sdp);
  380. page = gfs2_get_log_desc(sdp,
  381. is_databuf ? GFS2_LOG_DESC_JDATA :
  382. GFS2_LOG_DESC_METADATA, num + 1, num);
  383. ld = page_address(page);
  384. gfs2_log_lock(sdp);
  385. ptr = (__be64 *)(ld + 1);
  386. n = 0;
  387. list_for_each_entry_continue(bd1, blist, bd_list) {
  388. *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
  389. if (is_databuf) {
  390. gfs2_check_magic(bd1->bd_bh);
  391. *ptr++ = cpu_to_be64(buffer_escaped(bd1->bd_bh) ? 1 : 0);
  392. }
  393. if (++n >= num)
  394. break;
  395. }
  396. gfs2_log_unlock(sdp);
  397. gfs2_log_write_page(sdp, page);
  398. gfs2_log_lock(sdp);
  399. n = 0;
  400. list_for_each_entry_continue(bd2, blist, bd_list) {
  401. get_bh(bd2->bd_bh);
  402. gfs2_log_unlock(sdp);
  403. lock_buffer(bd2->bd_bh);
  404. if (buffer_escaped(bd2->bd_bh)) {
  405. void *kaddr;
  406. page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  407. ptr = page_address(page);
  408. kaddr = kmap_atomic(bd2->bd_bh->b_page);
  409. memcpy(ptr, kaddr + bh_offset(bd2->bd_bh),
  410. bd2->bd_bh->b_size);
  411. kunmap_atomic(kaddr);
  412. *(__be32 *)ptr = 0;
  413. clear_buffer_escaped(bd2->bd_bh);
  414. unlock_buffer(bd2->bd_bh);
  415. brelse(bd2->bd_bh);
  416. gfs2_log_write_page(sdp, page);
  417. } else {
  418. gfs2_log_write_bh(sdp, bd2->bd_bh);
  419. }
  420. gfs2_log_lock(sdp);
  421. if (++n >= num)
  422. break;
  423. }
  424. BUG_ON(total < num);
  425. total -= num;
  426. }
  427. gfs2_log_unlock(sdp);
  428. }
  429. static void buf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  430. {
  431. unsigned int limit = buf_limit(sdp); /* 503 for 4k blocks */
  432. unsigned int nbuf;
  433. if (tr == NULL)
  434. return;
  435. nbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
  436. gfs2_before_commit(sdp, limit, nbuf, &tr->tr_buf, 0);
  437. }
  438. static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  439. {
  440. struct list_head *head;
  441. struct gfs2_bufdata *bd;
  442. if (tr == NULL)
  443. return;
  444. head = &tr->tr_buf;
  445. while (!list_empty(head)) {
  446. bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  447. list_del_init(&bd->bd_list);
  448. gfs2_unpin(sdp, bd->bd_bh, tr);
  449. }
  450. }
  451. static void buf_lo_before_scan(struct gfs2_jdesc *jd,
  452. struct gfs2_log_header_host *head, int pass)
  453. {
  454. if (pass != 0)
  455. return;
  456. jd->jd_found_blocks = 0;
  457. jd->jd_replayed_blocks = 0;
  458. }
  459. static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  460. struct gfs2_log_descriptor *ld, __be64 *ptr,
  461. int pass)
  462. {
  463. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  464. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  465. struct gfs2_glock *gl = ip->i_gl;
  466. unsigned int blks = be32_to_cpu(ld->ld_data1);
  467. struct buffer_head *bh_log, *bh_ip;
  468. u64 blkno;
  469. int error = 0;
  470. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
  471. return 0;
  472. gfs2_replay_incr_blk(jd, &start);
  473. for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
  474. blkno = be64_to_cpu(*ptr++);
  475. jd->jd_found_blocks++;
  476. if (gfs2_revoke_check(jd, blkno, start))
  477. continue;
  478. error = gfs2_replay_read_block(jd, start, &bh_log);
  479. if (error)
  480. return error;
  481. bh_ip = gfs2_meta_new(gl, blkno);
  482. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  483. if (gfs2_meta_check(sdp, bh_ip))
  484. error = -EIO;
  485. else
  486. mark_buffer_dirty(bh_ip);
  487. brelse(bh_log);
  488. brelse(bh_ip);
  489. if (error)
  490. break;
  491. jd->jd_replayed_blocks++;
  492. }
  493. return error;
  494. }
  495. /**
  496. * gfs2_meta_sync - Sync all buffers associated with a glock
  497. * @gl: The glock
  498. *
  499. */
  500. static void gfs2_meta_sync(struct gfs2_glock *gl)
  501. {
  502. struct address_space *mapping = gfs2_glock2aspace(gl);
  503. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  504. int error;
  505. if (mapping == NULL)
  506. mapping = &sdp->sd_aspace;
  507. filemap_fdatawrite(mapping);
  508. error = filemap_fdatawait(mapping);
  509. if (error)
  510. gfs2_io_error(gl->gl_name.ln_sbd);
  511. }
  512. static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  513. {
  514. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  515. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  516. if (error) {
  517. gfs2_meta_sync(ip->i_gl);
  518. return;
  519. }
  520. if (pass != 1)
  521. return;
  522. gfs2_meta_sync(ip->i_gl);
  523. fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
  524. jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
  525. }
  526. static void revoke_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  527. {
  528. struct gfs2_meta_header *mh;
  529. unsigned int offset;
  530. struct list_head *head = &sdp->sd_log_le_revoke;
  531. struct gfs2_bufdata *bd;
  532. struct page *page;
  533. unsigned int length;
  534. gfs2_write_revokes(sdp);
  535. if (!sdp->sd_log_num_revoke)
  536. return;
  537. length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, sizeof(u64));
  538. page = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE, length, sdp->sd_log_num_revoke);
  539. offset = sizeof(struct gfs2_log_descriptor);
  540. list_for_each_entry(bd, head, bd_list) {
  541. sdp->sd_log_num_revoke--;
  542. if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
  543. gfs2_log_write_page(sdp, page);
  544. page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  545. mh = page_address(page);
  546. clear_page(mh);
  547. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  548. mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
  549. mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
  550. offset = sizeof(struct gfs2_meta_header);
  551. }
  552. *(__be64 *)(page_address(page) + offset) = cpu_to_be64(bd->bd_blkno);
  553. offset += sizeof(u64);
  554. }
  555. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  556. gfs2_log_write_page(sdp, page);
  557. }
  558. static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  559. {
  560. struct list_head *head = &sdp->sd_log_le_revoke;
  561. struct gfs2_bufdata *bd;
  562. struct gfs2_glock *gl;
  563. while (!list_empty(head)) {
  564. bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  565. list_del_init(&bd->bd_list);
  566. gl = bd->bd_gl;
  567. gfs2_glock_remove_revoke(gl);
  568. kmem_cache_free(gfs2_bufdata_cachep, bd);
  569. }
  570. }
  571. static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
  572. struct gfs2_log_header_host *head, int pass)
  573. {
  574. if (pass != 0)
  575. return;
  576. jd->jd_found_revokes = 0;
  577. jd->jd_replay_tail = head->lh_tail;
  578. }
  579. static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  580. struct gfs2_log_descriptor *ld, __be64 *ptr,
  581. int pass)
  582. {
  583. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  584. unsigned int blks = be32_to_cpu(ld->ld_length);
  585. unsigned int revokes = be32_to_cpu(ld->ld_data1);
  586. struct buffer_head *bh;
  587. unsigned int offset;
  588. u64 blkno;
  589. int first = 1;
  590. int error;
  591. if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
  592. return 0;
  593. offset = sizeof(struct gfs2_log_descriptor);
  594. for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
  595. error = gfs2_replay_read_block(jd, start, &bh);
  596. if (error)
  597. return error;
  598. if (!first)
  599. gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
  600. while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
  601. blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
  602. error = gfs2_revoke_add(jd, blkno, start);
  603. if (error < 0) {
  604. brelse(bh);
  605. return error;
  606. }
  607. else if (error)
  608. jd->jd_found_revokes++;
  609. if (!--revokes)
  610. break;
  611. offset += sizeof(u64);
  612. }
  613. brelse(bh);
  614. offset = sizeof(struct gfs2_meta_header);
  615. first = 0;
  616. }
  617. return 0;
  618. }
  619. static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  620. {
  621. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  622. if (error) {
  623. gfs2_revoke_clean(jd);
  624. return;
  625. }
  626. if (pass != 1)
  627. return;
  628. fs_info(sdp, "jid=%u: Found %u revoke tags\n",
  629. jd->jd_jid, jd->jd_found_revokes);
  630. gfs2_revoke_clean(jd);
  631. }
  632. /**
  633. * databuf_lo_before_commit - Scan the data buffers, writing as we go
  634. *
  635. */
  636. static void databuf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  637. {
  638. unsigned int limit = databuf_limit(sdp);
  639. unsigned int nbuf;
  640. if (tr == NULL)
  641. return;
  642. nbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
  643. gfs2_before_commit(sdp, limit, nbuf, &tr->tr_databuf, 1);
  644. }
  645. static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  646. struct gfs2_log_descriptor *ld,
  647. __be64 *ptr, int pass)
  648. {
  649. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  650. struct gfs2_glock *gl = ip->i_gl;
  651. unsigned int blks = be32_to_cpu(ld->ld_data1);
  652. struct buffer_head *bh_log, *bh_ip;
  653. u64 blkno;
  654. u64 esc;
  655. int error = 0;
  656. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  657. return 0;
  658. gfs2_replay_incr_blk(jd, &start);
  659. for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
  660. blkno = be64_to_cpu(*ptr++);
  661. esc = be64_to_cpu(*ptr++);
  662. jd->jd_found_blocks++;
  663. if (gfs2_revoke_check(jd, blkno, start))
  664. continue;
  665. error = gfs2_replay_read_block(jd, start, &bh_log);
  666. if (error)
  667. return error;
  668. bh_ip = gfs2_meta_new(gl, blkno);
  669. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  670. /* Unescape */
  671. if (esc) {
  672. __be32 *eptr = (__be32 *)bh_ip->b_data;
  673. *eptr = cpu_to_be32(GFS2_MAGIC);
  674. }
  675. mark_buffer_dirty(bh_ip);
  676. brelse(bh_log);
  677. brelse(bh_ip);
  678. jd->jd_replayed_blocks++;
  679. }
  680. return error;
  681. }
  682. /* FIXME: sort out accounting for log blocks etc. */
  683. static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  684. {
  685. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  686. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  687. if (error) {
  688. gfs2_meta_sync(ip->i_gl);
  689. return;
  690. }
  691. if (pass != 1)
  692. return;
  693. /* data sync? */
  694. gfs2_meta_sync(ip->i_gl);
  695. fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
  696. jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
  697. }
  698. static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  699. {
  700. struct list_head *head;
  701. struct gfs2_bufdata *bd;
  702. if (tr == NULL)
  703. return;
  704. head = &tr->tr_databuf;
  705. while (!list_empty(head)) {
  706. bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  707. list_del_init(&bd->bd_list);
  708. gfs2_unpin(sdp, bd->bd_bh, tr);
  709. }
  710. }
  711. const struct gfs2_log_operations gfs2_buf_lops = {
  712. .lo_before_commit = buf_lo_before_commit,
  713. .lo_after_commit = buf_lo_after_commit,
  714. .lo_before_scan = buf_lo_before_scan,
  715. .lo_scan_elements = buf_lo_scan_elements,
  716. .lo_after_scan = buf_lo_after_scan,
  717. .lo_name = "buf",
  718. };
  719. const struct gfs2_log_operations gfs2_revoke_lops = {
  720. .lo_before_commit = revoke_lo_before_commit,
  721. .lo_after_commit = revoke_lo_after_commit,
  722. .lo_before_scan = revoke_lo_before_scan,
  723. .lo_scan_elements = revoke_lo_scan_elements,
  724. .lo_after_scan = revoke_lo_after_scan,
  725. .lo_name = "revoke",
  726. };
  727. const struct gfs2_log_operations gfs2_databuf_lops = {
  728. .lo_before_commit = databuf_lo_before_commit,
  729. .lo_after_commit = databuf_lo_after_commit,
  730. .lo_scan_elements = databuf_lo_scan_elements,
  731. .lo_after_scan = databuf_lo_after_scan,
  732. .lo_name = "databuf",
  733. };
  734. const struct gfs2_log_operations *gfs2_log_ops[] = {
  735. &gfs2_databuf_lops,
  736. &gfs2_buf_lops,
  737. &gfs2_revoke_lops,
  738. NULL,
  739. };