gcinode.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Seiji Kihara <kihara@osrg.net>, Amagai Yoshiji <amagai@osrg.net>,
  21. * and Ryusuke Konishi <ryusuke@osrg.net>.
  22. * Revised by Ryusuke Konishi <ryusuke@osrg.net>.
  23. *
  24. */
  25. /*
  26. * This file adds the cache of on-disk blocks to be moved in garbage
  27. * collection. The disk blocks are held with dummy inodes (called
  28. * gcinodes), and this file provides lookup function of the dummy
  29. * inodes and their buffer read function.
  30. *
  31. * Buffers and pages held by the dummy inodes will be released each
  32. * time after they are copied to a new log. Dirty blocks made on the
  33. * current generation and the blocks to be moved by GC never overlap
  34. * because the dirty blocks make a new generation; they rather must be
  35. * written individually.
  36. */
  37. #include <linux/buffer_head.h>
  38. #include <linux/mpage.h>
  39. #include <linux/hash.h>
  40. #include <linux/slab.h>
  41. #include <linux/swap.h>
  42. #include "nilfs.h"
  43. #include "btree.h"
  44. #include "btnode.h"
  45. #include "page.h"
  46. #include "mdt.h"
  47. #include "dat.h"
  48. #include "ifile.h"
  49. static const struct address_space_operations def_gcinode_aops = {
  50. };
  51. /*
  52. * nilfs_gccache_submit_read_data() - add data buffer and submit read request
  53. * @inode - gc inode
  54. * @blkoff - dummy offset treated as the key for the page cache
  55. * @pbn - physical block number of the block
  56. * @vbn - virtual block number of the block, 0 for non-virtual block
  57. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  58. *
  59. * Description: nilfs_gccache_submit_read_data() registers the data buffer
  60. * specified by @pbn to the GC pagecache with the key @blkoff.
  61. * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
  62. *
  63. * Return Value: On success, 0 is returned. On Error, one of the following
  64. * negative error code is returned.
  65. *
  66. * %-EIO - I/O error.
  67. *
  68. * %-ENOMEM - Insufficient amount of memory available.
  69. *
  70. * %-ENOENT - The block specified with @pbn does not exist.
  71. */
  72. int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  73. sector_t pbn, __u64 vbn,
  74. struct buffer_head **out_bh)
  75. {
  76. struct buffer_head *bh;
  77. int err;
  78. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  79. if (unlikely(!bh))
  80. return -ENOMEM;
  81. if (buffer_uptodate(bh))
  82. goto out;
  83. if (pbn == 0) {
  84. struct inode *dat_inode = NILFS_I_NILFS(inode)->ns_dat;
  85. /* use original dat, not gc dat. */
  86. err = nilfs_dat_translate(dat_inode, vbn, &pbn);
  87. if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
  88. brelse(bh);
  89. goto failed;
  90. }
  91. }
  92. lock_buffer(bh);
  93. if (buffer_uptodate(bh)) {
  94. unlock_buffer(bh);
  95. goto out;
  96. }
  97. if (!buffer_mapped(bh)) {
  98. bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
  99. set_buffer_mapped(bh);
  100. }
  101. bh->b_blocknr = pbn;
  102. bh->b_end_io = end_buffer_read_sync;
  103. get_bh(bh);
  104. submit_bh(READ, bh);
  105. if (vbn)
  106. bh->b_blocknr = vbn;
  107. out:
  108. err = 0;
  109. *out_bh = bh;
  110. failed:
  111. unlock_page(bh->b_page);
  112. page_cache_release(bh->b_page);
  113. return err;
  114. }
  115. /*
  116. * nilfs_gccache_submit_read_node() - add node buffer and submit read request
  117. * @inode - gc inode
  118. * @pbn - physical block number for the block
  119. * @vbn - virtual block number for the block
  120. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  121. *
  122. * Description: nilfs_gccache_submit_read_node() registers the node buffer
  123. * specified by @vbn to the GC pagecache. @pbn can be supplied by the
  124. * caller to avoid translation of the disk block address.
  125. *
  126. * Return Value: On success, 0 is returned. On Error, one of the following
  127. * negative error code is returned.
  128. *
  129. * %-EIO - I/O error.
  130. *
  131. * %-ENOMEM - Insufficient amount of memory available.
  132. */
  133. int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
  134. __u64 vbn, struct buffer_head **out_bh)
  135. {
  136. int ret;
  137. ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
  138. vbn ? : pbn, pbn, READ, out_bh, &pbn);
  139. if (ret == -EEXIST) /* internal code (cache hit) */
  140. ret = 0;
  141. return ret;
  142. }
  143. int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
  144. {
  145. wait_on_buffer(bh);
  146. if (!buffer_uptodate(bh))
  147. return -EIO;
  148. if (buffer_dirty(bh))
  149. return -EEXIST;
  150. if (buffer_nilfs_node(bh)) {
  151. if (nilfs_btree_broken_node_block(bh)) {
  152. clear_buffer_uptodate(bh);
  153. return -EIO;
  154. }
  155. nilfs_btnode_mark_dirty(bh);
  156. } else {
  157. nilfs_mark_buffer_dirty(bh);
  158. }
  159. return 0;
  160. }
  161. int nilfs_init_gcinode(struct inode *inode)
  162. {
  163. struct nilfs_inode_info *ii = NILFS_I(inode);
  164. inode->i_mode = S_IFREG;
  165. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  166. inode->i_mapping->a_ops = &def_gcinode_aops;
  167. inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
  168. ii->i_flags = 0;
  169. nilfs_bmap_init_gc(ii->i_bmap);
  170. return 0;
  171. }
  172. /**
  173. * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
  174. */
  175. void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
  176. {
  177. struct list_head *head = &nilfs->ns_gc_inodes;
  178. struct nilfs_inode_info *ii;
  179. while (!list_empty(head)) {
  180. ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
  181. list_del_init(&ii->i_dirty);
  182. iput(&ii->vfs_inode);
  183. }
  184. }