gcinode.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * gcinode.c - dummy inodes to buffer blocks for garbage collection
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Written by Seiji Kihara, Amagai Yoshiji, and Ryusuke Konishi.
  17. * Revised by Ryusuke Konishi.
  18. *
  19. */
  20. /*
  21. * This file adds the cache of on-disk blocks to be moved in garbage
  22. * collection. The disk blocks are held with dummy inodes (called
  23. * gcinodes), and this file provides lookup function of the dummy
  24. * inodes and their buffer read function.
  25. *
  26. * Buffers and pages held by the dummy inodes will be released each
  27. * time after they are copied to a new log. Dirty blocks made on the
  28. * current generation and the blocks to be moved by GC never overlap
  29. * because the dirty blocks make a new generation; they rather must be
  30. * written individually.
  31. */
  32. #include <linux/buffer_head.h>
  33. #include <linux/mpage.h>
  34. #include <linux/hash.h>
  35. #include <linux/slab.h>
  36. #include <linux/swap.h>
  37. #include "nilfs.h"
  38. #include "btree.h"
  39. #include "btnode.h"
  40. #include "page.h"
  41. #include "mdt.h"
  42. #include "dat.h"
  43. #include "ifile.h"
  44. /*
  45. * nilfs_gccache_submit_read_data() - add data buffer and submit read request
  46. * @inode - gc inode
  47. * @blkoff - dummy offset treated as the key for the page cache
  48. * @pbn - physical block number of the block
  49. * @vbn - virtual block number of the block, 0 for non-virtual block
  50. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  51. *
  52. * Description: nilfs_gccache_submit_read_data() registers the data buffer
  53. * specified by @pbn to the GC pagecache with the key @blkoff.
  54. * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
  55. *
  56. * Return Value: On success, 0 is returned. On Error, one of the following
  57. * negative error code is returned.
  58. *
  59. * %-EIO - I/O error.
  60. *
  61. * %-ENOMEM - Insufficient amount of memory available.
  62. *
  63. * %-ENOENT - The block specified with @pbn does not exist.
  64. */
  65. int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  66. sector_t pbn, __u64 vbn,
  67. struct buffer_head **out_bh)
  68. {
  69. struct buffer_head *bh;
  70. int err;
  71. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  72. if (unlikely(!bh))
  73. return -ENOMEM;
  74. if (buffer_uptodate(bh))
  75. goto out;
  76. if (pbn == 0) {
  77. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  78. err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
  79. if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
  80. brelse(bh);
  81. goto failed;
  82. }
  83. }
  84. lock_buffer(bh);
  85. if (buffer_uptodate(bh)) {
  86. unlock_buffer(bh);
  87. goto out;
  88. }
  89. if (!buffer_mapped(bh)) {
  90. bh->b_bdev = inode->i_sb->s_bdev;
  91. set_buffer_mapped(bh);
  92. }
  93. bh->b_blocknr = pbn;
  94. bh->b_end_io = end_buffer_read_sync;
  95. get_bh(bh);
  96. submit_bh(REQ_OP_READ, 0, bh);
  97. if (vbn)
  98. bh->b_blocknr = vbn;
  99. out:
  100. err = 0;
  101. *out_bh = bh;
  102. failed:
  103. unlock_page(bh->b_page);
  104. put_page(bh->b_page);
  105. return err;
  106. }
  107. /*
  108. * nilfs_gccache_submit_read_node() - add node buffer and submit read request
  109. * @inode - gc inode
  110. * @pbn - physical block number for the block
  111. * @vbn - virtual block number for the block
  112. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  113. *
  114. * Description: nilfs_gccache_submit_read_node() registers the node buffer
  115. * specified by @vbn to the GC pagecache. @pbn can be supplied by the
  116. * caller to avoid translation of the disk block address.
  117. *
  118. * Return Value: On success, 0 is returned. On Error, one of the following
  119. * negative error code is returned.
  120. *
  121. * %-EIO - I/O error.
  122. *
  123. * %-ENOMEM - Insufficient amount of memory available.
  124. */
  125. int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
  126. __u64 vbn, struct buffer_head **out_bh)
  127. {
  128. int ret;
  129. ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
  130. vbn ? : pbn, pbn, REQ_OP_READ, 0,
  131. out_bh, &pbn);
  132. if (ret == -EEXIST) /* internal code (cache hit) */
  133. ret = 0;
  134. return ret;
  135. }
  136. int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
  137. {
  138. wait_on_buffer(bh);
  139. if (!buffer_uptodate(bh)) {
  140. struct inode *inode = bh->b_page->mapping->host;
  141. nilfs_msg(inode->i_sb, KERN_ERR,
  142. "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
  143. buffer_nilfs_node(bh) ? "node" : "data",
  144. inode->i_ino, (unsigned long long)bh->b_blocknr);
  145. return -EIO;
  146. }
  147. if (buffer_dirty(bh))
  148. return -EEXIST;
  149. if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
  150. clear_buffer_uptodate(bh);
  151. return -EIO;
  152. }
  153. mark_buffer_dirty(bh);
  154. return 0;
  155. }
  156. int nilfs_init_gcinode(struct inode *inode)
  157. {
  158. struct nilfs_inode_info *ii = NILFS_I(inode);
  159. inode->i_mode = S_IFREG;
  160. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  161. inode->i_mapping->a_ops = &empty_aops;
  162. ii->i_flags = 0;
  163. nilfs_bmap_init_gc(ii->i_bmap);
  164. return 0;
  165. }
  166. /**
  167. * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
  168. */
  169. void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
  170. {
  171. struct list_head *head = &nilfs->ns_gc_inodes;
  172. struct nilfs_inode_info *ii;
  173. while (!list_empty(head)) {
  174. ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
  175. list_del_init(&ii->i_dirty);
  176. truncate_inode_pages(&ii->vfs_inode.i_data, 0);
  177. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  178. iput(&ii->vfs_inode);
  179. }
  180. }