gcinode.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /*
  50. * nilfs_gccache_submit_read_data() - add data buffer and submit read request
  51. * @inode - gc inode
  52. * @blkoff - dummy offset treated as the key for the page cache
  53. * @pbn - physical block number of the block
  54. * @vbn - virtual block number of the block, 0 for non-virtual block
  55. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  56. *
  57. * Description: nilfs_gccache_submit_read_data() registers the data buffer
  58. * specified by @pbn to the GC pagecache with the key @blkoff.
  59. * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
  60. *
  61. * Return Value: On success, 0 is returned. On Error, one of the following
  62. * negative error code is returned.
  63. *
  64. * %-EIO - I/O error.
  65. *
  66. * %-ENOMEM - Insufficient amount of memory available.
  67. *
  68. * %-ENOENT - The block specified with @pbn does not exist.
  69. */
  70. int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  71. sector_t pbn, __u64 vbn,
  72. struct buffer_head **out_bh)
  73. {
  74. struct buffer_head *bh;
  75. int err;
  76. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  77. if (unlikely(!bh))
  78. return -ENOMEM;
  79. if (buffer_uptodate(bh))
  80. goto out;
  81. if (pbn == 0) {
  82. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  83. err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
  84. if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
  85. brelse(bh);
  86. goto failed;
  87. }
  88. }
  89. lock_buffer(bh);
  90. if (buffer_uptodate(bh)) {
  91. unlock_buffer(bh);
  92. goto out;
  93. }
  94. if (!buffer_mapped(bh)) {
  95. bh->b_bdev = inode->i_sb->s_bdev;
  96. set_buffer_mapped(bh);
  97. }
  98. bh->b_blocknr = pbn;
  99. bh->b_end_io = end_buffer_read_sync;
  100. get_bh(bh);
  101. submit_bh(READ, bh);
  102. if (vbn)
  103. bh->b_blocknr = vbn;
  104. out:
  105. err = 0;
  106. *out_bh = bh;
  107. failed:
  108. unlock_page(bh->b_page);
  109. page_cache_release(bh->b_page);
  110. return err;
  111. }
  112. /*
  113. * nilfs_gccache_submit_read_node() - add node buffer and submit read request
  114. * @inode - gc inode
  115. * @pbn - physical block number for the block
  116. * @vbn - virtual block number for the block
  117. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  118. *
  119. * Description: nilfs_gccache_submit_read_node() registers the node buffer
  120. * specified by @vbn to the GC pagecache. @pbn can be supplied by the
  121. * caller to avoid translation of the disk block address.
  122. *
  123. * Return Value: On success, 0 is returned. On Error, one of the following
  124. * negative error code is returned.
  125. *
  126. * %-EIO - I/O error.
  127. *
  128. * %-ENOMEM - Insufficient amount of memory available.
  129. */
  130. int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
  131. __u64 vbn, struct buffer_head **out_bh)
  132. {
  133. int ret;
  134. ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
  135. vbn ? : pbn, pbn, READ, out_bh, &pbn);
  136. if (ret == -EEXIST) /* internal code (cache hit) */
  137. ret = 0;
  138. return ret;
  139. }
  140. int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
  141. {
  142. wait_on_buffer(bh);
  143. if (!buffer_uptodate(bh))
  144. return -EIO;
  145. if (buffer_dirty(bh))
  146. return -EEXIST;
  147. if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
  148. clear_buffer_uptodate(bh);
  149. return -EIO;
  150. }
  151. mark_buffer_dirty(bh);
  152. return 0;
  153. }
  154. int nilfs_init_gcinode(struct inode *inode)
  155. {
  156. struct nilfs_inode_info *ii = NILFS_I(inode);
  157. inode->i_mode = S_IFREG;
  158. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  159. inode->i_mapping->a_ops = &empty_aops;
  160. ii->i_flags = 0;
  161. nilfs_bmap_init_gc(ii->i_bmap);
  162. return 0;
  163. }
  164. /**
  165. * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
  166. */
  167. void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
  168. {
  169. struct list_head *head = &nilfs->ns_gc_inodes;
  170. struct nilfs_inode_info *ii;
  171. while (!list_empty(head)) {
  172. ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
  173. list_del_init(&ii->i_dirty);
  174. truncate_inode_pages(&ii->vfs_inode.i_data, 0);
  175. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  176. iput(&ii->vfs_inode);
  177. }
  178. }