lops.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 == 0)
  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. }
  79. /**
  80. * gfs2_unpin - Unpin a buffer
  81. * @sdp: the filesystem the buffer belongs to
  82. * @bh: The buffer to unpin
  83. * @ai:
  84. * @flags: The inode dirty flags
  85. *
  86. */
  87. static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  88. struct gfs2_trans *tr)
  89. {
  90. struct gfs2_bufdata *bd = bh->b_private;
  91. BUG_ON(!buffer_uptodate(bh));
  92. BUG_ON(!buffer_pinned(bh));
  93. lock_buffer(bh);
  94. mark_buffer_dirty(bh);
  95. clear_buffer_pinned(bh);
  96. if (buffer_is_rgrp(bd))
  97. maybe_release_space(bd);
  98. spin_lock(&sdp->sd_ail_lock);
  99. if (bd->bd_tr) {
  100. list_del(&bd->bd_ail_st_list);
  101. brelse(bh);
  102. } else {
  103. struct gfs2_glock *gl = bd->bd_gl;
  104. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  105. atomic_inc(&gl->gl_ail_count);
  106. }
  107. bd->bd_tr = tr;
  108. list_add(&bd->bd_ail_st_list, &tr->tr_ail1_list);
  109. spin_unlock(&sdp->sd_ail_lock);
  110. clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  111. trace_gfs2_pin(bd, 0);
  112. unlock_buffer(bh);
  113. atomic_dec(&sdp->sd_log_pinned);
  114. }
  115. static void gfs2_log_incr_head(struct gfs2_sbd *sdp)
  116. {
  117. BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) &&
  118. (sdp->sd_log_flush_head != sdp->sd_log_head));
  119. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
  120. sdp->sd_log_flush_head = 0;
  121. sdp->sd_log_flush_wrapped = 1;
  122. }
  123. }
  124. static 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, extent_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 relavent buffers and unlocks then 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. int 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. set_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, int error)
  180. {
  181. struct gfs2_sbd *sdp = bio->bi_private;
  182. struct bio_vec *bvec;
  183. struct page *page;
  184. int i;
  185. if (error) {
  186. sdp->sd_log_error = error;
  187. fs_err(sdp, "Error %d writing to log\n", error);
  188. }
  189. bio_for_each_segment_all(bvec, bio, i) {
  190. page = bvec->bv_page;
  191. if (page_has_buffers(page))
  192. gfs2_end_log_write_bh(sdp, bvec, error);
  193. else
  194. mempool_free(page, gfs2_page_pool);
  195. }
  196. bio_put(bio);
  197. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  198. wake_up(&sdp->sd_log_flush_wait);
  199. }
  200. /**
  201. * gfs2_log_flush_bio - Submit any pending log bio
  202. * @sdp: The superblock
  203. * @rw: The rw flags
  204. *
  205. * Submit any pending part-built or full bio to the block device. If
  206. * there is no pending bio, then this is a no-op.
  207. */
  208. void gfs2_log_flush_bio(struct gfs2_sbd *sdp, int rw)
  209. {
  210. if (sdp->sd_log_bio) {
  211. atomic_inc(&sdp->sd_log_in_flight);
  212. submit_bio(rw, sdp->sd_log_bio);
  213. sdp->sd_log_bio = NULL;
  214. }
  215. }
  216. /**
  217. * gfs2_log_alloc_bio - Allocate a new bio for log writing
  218. * @sdp: The superblock
  219. * @blkno: The next device block number we want to write to
  220. *
  221. * This should never be called when there is a cached bio in the
  222. * super block. When it returns, there will be a cached bio in the
  223. * super block which will have as many bio_vecs as the device is
  224. * happy to handle.
  225. *
  226. * Returns: Newly allocated bio
  227. */
  228. static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno)
  229. {
  230. struct super_block *sb = sdp->sd_vfs;
  231. unsigned nrvecs = bio_get_nr_vecs(sb->s_bdev);
  232. struct bio *bio;
  233. BUG_ON(sdp->sd_log_bio);
  234. while (1) {
  235. bio = bio_alloc(GFP_NOIO, nrvecs);
  236. if (likely(bio))
  237. break;
  238. nrvecs = max(nrvecs/2, 1U);
  239. }
  240. bio->bi_sector = blkno * (sb->s_blocksize >> 9);
  241. bio->bi_bdev = sb->s_bdev;
  242. bio->bi_end_io = gfs2_end_log_write;
  243. bio->bi_private = sdp;
  244. sdp->sd_log_bio = bio;
  245. return bio;
  246. }
  247. /**
  248. * gfs2_log_get_bio - Get cached log bio, or allocate a new one
  249. * @sdp: The superblock
  250. * @blkno: The device block number we want to write to
  251. *
  252. * If there is a cached bio, then if the next block number is sequential
  253. * with the previous one, return it, otherwise flush the bio to the
  254. * device. If there is not a cached bio, or we just flushed it, then
  255. * allocate a new one.
  256. *
  257. * Returns: The bio to use for log writes
  258. */
  259. static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno)
  260. {
  261. struct bio *bio = sdp->sd_log_bio;
  262. u64 nblk;
  263. if (bio) {
  264. nblk = bio_end_sector(bio);
  265. nblk >>= sdp->sd_fsb2bb_shift;
  266. if (blkno == nblk)
  267. return bio;
  268. gfs2_log_flush_bio(sdp, WRITE);
  269. }
  270. return gfs2_log_alloc_bio(sdp, blkno);
  271. }
  272. /**
  273. * gfs2_log_write - write to log
  274. * @sdp: the filesystem
  275. * @page: the page to write
  276. * @size: the size of the data to write
  277. * @offset: the offset within the page
  278. *
  279. * Try and add the page segment to the current bio. If that fails,
  280. * submit the current bio to the device and create a new one, and
  281. * then add the page segment to that.
  282. */
  283. static void gfs2_log_write(struct gfs2_sbd *sdp, struct page *page,
  284. unsigned size, unsigned offset)
  285. {
  286. u64 blkno = gfs2_log_bmap(sdp);
  287. struct bio *bio;
  288. int ret;
  289. bio = gfs2_log_get_bio(sdp, blkno);
  290. ret = bio_add_page(bio, page, size, offset);
  291. if (ret == 0) {
  292. gfs2_log_flush_bio(sdp, WRITE);
  293. bio = gfs2_log_alloc_bio(sdp, blkno);
  294. ret = bio_add_page(bio, page, size, offset);
  295. WARN_ON(ret == 0);
  296. }
  297. }
  298. /**
  299. * gfs2_log_write_bh - write a buffer's content to the log
  300. * @sdp: The super block
  301. * @bh: The buffer pointing to the in-place location
  302. *
  303. * This writes the content of the buffer to the next available location
  304. * in the log. The buffer will be unlocked once the i/o to the log has
  305. * completed.
  306. */
  307. static void gfs2_log_write_bh(struct gfs2_sbd *sdp, struct buffer_head *bh)
  308. {
  309. gfs2_log_write(sdp, bh->b_page, bh->b_size, bh_offset(bh));
  310. }
  311. /**
  312. * gfs2_log_write_page - write one block stored in a page, into the log
  313. * @sdp: The superblock
  314. * @page: The struct page
  315. *
  316. * This writes the first block-sized part of the page into the log. Note
  317. * that the page must have been allocated from the gfs2_page_pool mempool
  318. * and that after this has been called, ownership has been transferred and
  319. * the page may be freed at any time.
  320. */
  321. void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page)
  322. {
  323. struct super_block *sb = sdp->sd_vfs;
  324. gfs2_log_write(sdp, page, sb->s_blocksize, 0);
  325. }
  326. static struct page *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type,
  327. u32 ld_length, u32 ld_data1)
  328. {
  329. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  330. struct gfs2_log_descriptor *ld = page_address(page);
  331. clear_page(ld);
  332. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  333. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  334. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  335. ld->ld_type = cpu_to_be32(ld_type);
  336. ld->ld_length = cpu_to_be32(ld_length);
  337. ld->ld_data1 = cpu_to_be32(ld_data1);
  338. ld->ld_data2 = 0;
  339. return page;
  340. }
  341. static void gfs2_check_magic(struct buffer_head *bh)
  342. {
  343. void *kaddr;
  344. __be32 *ptr;
  345. clear_buffer_escaped(bh);
  346. kaddr = kmap_atomic(bh->b_page);
  347. ptr = kaddr + bh_offset(bh);
  348. if (*ptr == cpu_to_be32(GFS2_MAGIC))
  349. set_buffer_escaped(bh);
  350. kunmap_atomic(kaddr);
  351. }
  352. static int blocknr_cmp(void *priv, struct list_head *a, struct list_head *b)
  353. {
  354. struct gfs2_bufdata *bda, *bdb;
  355. bda = list_entry(a, struct gfs2_bufdata, bd_list);
  356. bdb = list_entry(b, struct gfs2_bufdata, bd_list);
  357. if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr)
  358. return -1;
  359. if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr)
  360. return 1;
  361. return 0;
  362. }
  363. static void gfs2_before_commit(struct gfs2_sbd *sdp, unsigned int limit,
  364. unsigned int total, struct list_head *blist,
  365. bool is_databuf)
  366. {
  367. struct gfs2_log_descriptor *ld;
  368. struct gfs2_bufdata *bd1 = NULL, *bd2;
  369. struct page *page;
  370. unsigned int num;
  371. unsigned n;
  372. __be64 *ptr;
  373. gfs2_log_lock(sdp);
  374. list_sort(NULL, blist, blocknr_cmp);
  375. bd1 = bd2 = list_prepare_entry(bd1, blist, bd_list);
  376. while(total) {
  377. num = total;
  378. if (total > limit)
  379. num = limit;
  380. gfs2_log_unlock(sdp);
  381. page = gfs2_get_log_desc(sdp,
  382. is_databuf ? GFS2_LOG_DESC_JDATA :
  383. GFS2_LOG_DESC_METADATA, num + 1, num);
  384. ld = page_address(page);
  385. gfs2_log_lock(sdp);
  386. ptr = (__be64 *)(ld + 1);
  387. n = 0;
  388. list_for_each_entry_continue(bd1, blist, bd_list) {
  389. *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
  390. if (is_databuf) {
  391. gfs2_check_magic(bd1->bd_bh);
  392. *ptr++ = cpu_to_be64(buffer_escaped(bd1->bd_bh) ? 1 : 0);
  393. }
  394. if (++n >= num)
  395. break;
  396. }
  397. gfs2_log_unlock(sdp);
  398. gfs2_log_write_page(sdp, page);
  399. gfs2_log_lock(sdp);
  400. n = 0;
  401. list_for_each_entry_continue(bd2, blist, bd_list) {
  402. get_bh(bd2->bd_bh);
  403. gfs2_log_unlock(sdp);
  404. lock_buffer(bd2->bd_bh);
  405. if (buffer_escaped(bd2->bd_bh)) {
  406. void *kaddr;
  407. page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  408. ptr = page_address(page);
  409. kaddr = kmap_atomic(bd2->bd_bh->b_page);
  410. memcpy(ptr, kaddr + bh_offset(bd2->bd_bh),
  411. bd2->bd_bh->b_size);
  412. kunmap_atomic(kaddr);
  413. *(__be32 *)ptr = 0;
  414. clear_buffer_escaped(bd2->bd_bh);
  415. unlock_buffer(bd2->bd_bh);
  416. brelse(bd2->bd_bh);
  417. gfs2_log_write_page(sdp, page);
  418. } else {
  419. gfs2_log_write_bh(sdp, bd2->bd_bh);
  420. }
  421. gfs2_log_lock(sdp);
  422. if (++n >= num)
  423. break;
  424. }
  425. BUG_ON(total < num);
  426. total -= num;
  427. }
  428. gfs2_log_unlock(sdp);
  429. }
  430. static void buf_lo_before_commit(struct gfs2_sbd *sdp)
  431. {
  432. unsigned int limit = buf_limit(sdp); /* 503 for 4k blocks */
  433. gfs2_before_commit(sdp, limit, sdp->sd_log_num_buf,
  434. &sdp->sd_log_le_buf, 0);
  435. }
  436. static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  437. {
  438. struct list_head *head = &sdp->sd_log_le_buf;
  439. struct gfs2_bufdata *bd;
  440. if (tr == NULL) {
  441. gfs2_assert(sdp, list_empty(head));
  442. return;
  443. }
  444. while (!list_empty(head)) {
  445. bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  446. list_del_init(&bd->bd_list);
  447. sdp->sd_log_num_buf--;
  448. gfs2_unpin(sdp, bd->bd_bh, tr);
  449. }
  450. gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
  451. }
  452. static void buf_lo_before_scan(struct gfs2_jdesc *jd,
  453. struct gfs2_log_header_host *head, int pass)
  454. {
  455. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  456. if (pass != 0)
  457. return;
  458. sdp->sd_found_blocks = 0;
  459. sdp->sd_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. sdp->sd_found_blocks++;
  478. if (gfs2_revoke_check(sdp, 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. sdp->sd_replayed_blocks++;
  494. }
  495. return error;
  496. }
  497. static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  498. {
  499. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  500. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  501. if (error) {
  502. gfs2_meta_sync(ip->i_gl);
  503. return;
  504. }
  505. if (pass != 1)
  506. return;
  507. gfs2_meta_sync(ip->i_gl);
  508. fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
  509. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  510. }
  511. static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
  512. {
  513. struct gfs2_meta_header *mh;
  514. unsigned int offset;
  515. struct list_head *head = &sdp->sd_log_le_revoke;
  516. struct gfs2_bufdata *bd;
  517. struct page *page;
  518. unsigned int length;
  519. gfs2_write_revokes(sdp);
  520. if (!sdp->sd_log_num_revoke)
  521. return;
  522. length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, sizeof(u64));
  523. page = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE, length, sdp->sd_log_num_revoke);
  524. offset = sizeof(struct gfs2_log_descriptor);
  525. list_for_each_entry(bd, head, bd_list) {
  526. sdp->sd_log_num_revoke--;
  527. if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
  528. gfs2_log_write_page(sdp, page);
  529. page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  530. mh = page_address(page);
  531. clear_page(mh);
  532. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  533. mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
  534. mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
  535. offset = sizeof(struct gfs2_meta_header);
  536. }
  537. *(__be64 *)(page_address(page) + offset) = cpu_to_be64(bd->bd_blkno);
  538. offset += sizeof(u64);
  539. }
  540. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  541. gfs2_log_write_page(sdp, page);
  542. }
  543. static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  544. {
  545. struct list_head *head = &sdp->sd_log_le_revoke;
  546. struct gfs2_bufdata *bd;
  547. struct gfs2_glock *gl;
  548. while (!list_empty(head)) {
  549. bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  550. list_del_init(&bd->bd_list);
  551. gl = bd->bd_gl;
  552. atomic_dec(&gl->gl_revokes);
  553. clear_bit(GLF_LFLUSH, &gl->gl_flags);
  554. kmem_cache_free(gfs2_bufdata_cachep, bd);
  555. }
  556. }
  557. static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
  558. struct gfs2_log_header_host *head, int pass)
  559. {
  560. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  561. if (pass != 0)
  562. return;
  563. sdp->sd_found_revokes = 0;
  564. sdp->sd_replay_tail = head->lh_tail;
  565. }
  566. static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  567. struct gfs2_log_descriptor *ld, __be64 *ptr,
  568. int pass)
  569. {
  570. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  571. unsigned int blks = be32_to_cpu(ld->ld_length);
  572. unsigned int revokes = be32_to_cpu(ld->ld_data1);
  573. struct buffer_head *bh;
  574. unsigned int offset;
  575. u64 blkno;
  576. int first = 1;
  577. int error;
  578. if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
  579. return 0;
  580. offset = sizeof(struct gfs2_log_descriptor);
  581. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  582. error = gfs2_replay_read_block(jd, start, &bh);
  583. if (error)
  584. return error;
  585. if (!first)
  586. gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
  587. while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
  588. blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
  589. error = gfs2_revoke_add(sdp, blkno, start);
  590. if (error < 0) {
  591. brelse(bh);
  592. return error;
  593. }
  594. else if (error)
  595. sdp->sd_found_revokes++;
  596. if (!--revokes)
  597. break;
  598. offset += sizeof(u64);
  599. }
  600. brelse(bh);
  601. offset = sizeof(struct gfs2_meta_header);
  602. first = 0;
  603. }
  604. return 0;
  605. }
  606. static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  607. {
  608. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  609. if (error) {
  610. gfs2_revoke_clean(sdp);
  611. return;
  612. }
  613. if (pass != 1)
  614. return;
  615. fs_info(sdp, "jid=%u: Found %u revoke tags\n",
  616. jd->jd_jid, sdp->sd_found_revokes);
  617. gfs2_revoke_clean(sdp);
  618. }
  619. /**
  620. * databuf_lo_before_commit - Scan the data buffers, writing as we go
  621. *
  622. */
  623. static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
  624. {
  625. unsigned int limit = buf_limit(sdp) / 2;
  626. gfs2_before_commit(sdp, limit, sdp->sd_log_num_databuf,
  627. &sdp->sd_log_le_databuf, 1);
  628. }
  629. static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  630. struct gfs2_log_descriptor *ld,
  631. __be64 *ptr, int pass)
  632. {
  633. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  634. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  635. struct gfs2_glock *gl = ip->i_gl;
  636. unsigned int blks = be32_to_cpu(ld->ld_data1);
  637. struct buffer_head *bh_log, *bh_ip;
  638. u64 blkno;
  639. u64 esc;
  640. int error = 0;
  641. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  642. return 0;
  643. gfs2_replay_incr_blk(sdp, &start);
  644. for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
  645. blkno = be64_to_cpu(*ptr++);
  646. esc = be64_to_cpu(*ptr++);
  647. sdp->sd_found_blocks++;
  648. if (gfs2_revoke_check(sdp, blkno, start))
  649. continue;
  650. error = gfs2_replay_read_block(jd, start, &bh_log);
  651. if (error)
  652. return error;
  653. bh_ip = gfs2_meta_new(gl, blkno);
  654. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  655. /* Unescape */
  656. if (esc) {
  657. __be32 *eptr = (__be32 *)bh_ip->b_data;
  658. *eptr = cpu_to_be32(GFS2_MAGIC);
  659. }
  660. mark_buffer_dirty(bh_ip);
  661. brelse(bh_log);
  662. brelse(bh_ip);
  663. sdp->sd_replayed_blocks++;
  664. }
  665. return error;
  666. }
  667. /* FIXME: sort out accounting for log blocks etc. */
  668. static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  669. {
  670. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  671. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  672. if (error) {
  673. gfs2_meta_sync(ip->i_gl);
  674. return;
  675. }
  676. if (pass != 1)
  677. return;
  678. /* data sync? */
  679. gfs2_meta_sync(ip->i_gl);
  680. fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
  681. jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
  682. }
  683. static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  684. {
  685. struct list_head *head = &sdp->sd_log_le_databuf;
  686. struct gfs2_bufdata *bd;
  687. if (tr == NULL) {
  688. gfs2_assert(sdp, list_empty(head));
  689. return;
  690. }
  691. while (!list_empty(head)) {
  692. bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  693. list_del_init(&bd->bd_list);
  694. sdp->sd_log_num_databuf--;
  695. gfs2_unpin(sdp, bd->bd_bh, tr);
  696. }
  697. gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
  698. }
  699. const struct gfs2_log_operations gfs2_buf_lops = {
  700. .lo_before_commit = buf_lo_before_commit,
  701. .lo_after_commit = buf_lo_after_commit,
  702. .lo_before_scan = buf_lo_before_scan,
  703. .lo_scan_elements = buf_lo_scan_elements,
  704. .lo_after_scan = buf_lo_after_scan,
  705. .lo_name = "buf",
  706. };
  707. const struct gfs2_log_operations gfs2_revoke_lops = {
  708. .lo_before_commit = revoke_lo_before_commit,
  709. .lo_after_commit = revoke_lo_after_commit,
  710. .lo_before_scan = revoke_lo_before_scan,
  711. .lo_scan_elements = revoke_lo_scan_elements,
  712. .lo_after_scan = revoke_lo_after_scan,
  713. .lo_name = "revoke",
  714. };
  715. const struct gfs2_log_operations gfs2_databuf_lops = {
  716. .lo_before_commit = databuf_lo_before_commit,
  717. .lo_after_commit = databuf_lo_after_commit,
  718. .lo_scan_elements = databuf_lo_scan_elements,
  719. .lo_after_scan = databuf_lo_after_scan,
  720. .lo_name = "databuf",
  721. };
  722. const struct gfs2_log_operations *gfs2_log_ops[] = {
  723. &gfs2_databuf_lops,
  724. &gfs2_buf_lops,
  725. &gfs2_revoke_lops,
  726. NULL,
  727. };