lops.c 21 KB

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